2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
38 #include "xfs_trans.h"
42 #include "xfs_alloc.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_quota.h"
45 #include "xfs_mount.h"
46 #include "xfs_alloc_btree.h"
47 #include "xfs_bmap_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dir_sf.h"
53 #include "xfs_dir2_sf.h"
54 #include "xfs_dinode.h"
55 #include "xfs_inode.h"
58 #include "xfs_rtalloc.h"
59 #include "xfs_error.h"
60 #include "xfs_itable.h"
66 #include "xfs_buf_item.h"
67 #include "xfs_utils.h"
68 #include "xfs_version.h"
70 #include <linux/namei.h>
71 #include <linux/init.h>
72 #include <linux/mount.h>
73 #include <linux/suspend.h>
74 #include <linux/writeback.h>
76 STATIC struct quotactl_ops linvfs_qops;
77 STATIC struct super_operations linvfs_sops;
78 STATIC struct export_operations linvfs_export_ops;
79 STATIC kmem_zone_t *linvfs_inode_zone;
80 STATIC kmem_shaker_t xfs_inode_shaker;
82 STATIC struct xfs_mount_args *
84 struct super_block *sb)
86 struct xfs_mount_args *args;
88 args = kmem_zalloc(sizeof(struct xfs_mount_args), KM_SLEEP);
89 args->logbufs = args->logbufsize = -1;
90 strncpy(args->fsname, sb->s_id, MAXNAMELEN);
92 /* Copy the already-parsed mount(2) flags we're interested in */
93 if (sb->s_flags & MS_NOATIME)
94 args->flags |= XFSMNT_NOATIME;
96 /* Default to 32 bit inodes on Linux all the time */
97 args->flags |= XFSMNT_32BITINODES;
104 unsigned int blockshift)
106 unsigned int pagefactor = 1;
107 unsigned int bitshift = BITS_PER_LONG - 1;
109 /* Figure out maximum filesize, on Linux this can depend on
110 * the filesystem blocksize (on 32 bit platforms).
111 * __block_prepare_write does this in an [unsigned] long...
112 * page->index << (PAGE_CACHE_SHIFT - bbits)
113 * So, for page sized blocks (4K on 32 bit platforms),
114 * this wraps at around 8Tb (hence MAX_LFS_FILESIZE which is
115 * (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
116 * but for smaller blocksizes it is less (bbits = log2 bsize).
117 * Note1: get_block_t takes a long (implicit cast from above)
118 * Note2: The Large Block Device (LBD and HAVE_SECTOR_T) patch
119 * can optionally convert the [unsigned] long from above into
120 * an [unsigned] long long.
123 #if BITS_PER_LONG == 32
124 # if defined(CONFIG_LBD)
125 ASSERT(sizeof(sector_t) == 8);
126 pagefactor = PAGE_CACHE_SIZE;
127 bitshift = BITS_PER_LONG;
129 pagefactor = PAGE_CACHE_SIZE >> (PAGE_CACHE_SHIFT - blockshift);
133 return (((__uint64_t)pagefactor) << bitshift) - 1;
136 STATIC __inline__ void
140 vnode_t *vp = LINVFS_GET_VP(inode);
142 if (vp->v_type == VNON) {
143 make_bad_inode(inode);
144 } else if (S_ISREG(inode->i_mode)) {
145 inode->i_op = &linvfs_file_inode_operations;
146 inode->i_fop = &linvfs_file_operations;
147 inode->i_mapping->a_ops = &linvfs_aops;
148 } else if (S_ISDIR(inode->i_mode)) {
149 inode->i_op = &linvfs_dir_inode_operations;
150 inode->i_fop = &linvfs_dir_operations;
151 } else if (S_ISLNK(inode->i_mode)) {
152 inode->i_op = &linvfs_symlink_inode_operations;
154 inode->i_mapping->a_ops = &linvfs_aops;
156 inode->i_op = &linvfs_file_inode_operations;
157 init_special_inode(inode, inode->i_mode, inode->i_rdev);
161 STATIC __inline__ void
162 xfs_revalidate_inode(
167 struct inode *inode = LINVFS_GET_IP(vp);
169 inode->i_mode = (ip->i_d.di_mode & MODEMASK) | VTTOIF(vp->v_type);
170 inode->i_nlink = ip->i_d.di_nlink;
171 inode->i_uid = ip->i_d.di_uid;
172 inode->i_gid = ip->i_d.di_gid;
173 if (((1 << vp->v_type) & ((1<<VBLK) | (1<<VCHR))) == 0) {
176 xfs_dev_t dev = ip->i_df.if_u2.if_rdev;
177 inode->i_rdev = MKDEV(sysv_major(dev) & 0x1ff, sysv_minor(dev));
179 inode->i_blksize = PAGE_CACHE_SIZE;
180 inode->i_generation = ip->i_d.di_gen;
181 i_size_write(inode, ip->i_d.di_size);
183 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
184 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
185 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
186 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
187 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
188 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
189 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
190 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
191 inode->i_flags |= S_IMMUTABLE;
193 inode->i_flags &= ~S_IMMUTABLE;
194 if (ip->i_d.di_flags & XFS_DIFLAG_IUNLINK)
195 inode->i_flags |= S_IUNLINK;
197 inode->i_flags &= ~S_IUNLINK;
198 if (ip->i_d.di_flags & XFS_DIFLAG_BARRIER)
199 inode->i_flags |= S_BARRIER;
201 inode->i_flags &= ~S_BARRIER;
202 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
203 inode->i_flags |= S_APPEND;
205 inode->i_flags &= ~S_APPEND;
206 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
207 inode->i_flags |= S_SYNC;
209 inode->i_flags &= ~S_SYNC;
210 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
211 inode->i_flags |= S_NOATIME;
213 inode->i_flags &= ~S_NOATIME;
214 vp->v_flag &= ~VMODIFIED;
218 xfs_initialize_vnode(
221 bhv_desc_t *inode_bhv,
224 xfs_inode_t *ip = XFS_BHVTOI(inode_bhv);
225 struct inode *inode = LINVFS_GET_IP(vp);
227 if (!inode_bhv->bd_vobj) {
228 vp->v_vfsp = bhvtovfs(bdp);
229 bhv_desc_init(inode_bhv, ip, vp, &xfs_vnodeops);
230 bhv_insert(VN_BHV_HEAD(vp), inode_bhv);
233 vp->v_type = IFTOVT(ip->i_d.di_mode);
235 /* Have we been called during the new inode create process,
236 * in which case we are too early to fill in the Linux inode.
238 if (vp->v_type == VNON)
241 xfs_revalidate_inode(XFS_BHVTOM(bdp), vp, ip);
243 /* For new inodes we need to set the ops vectors,
244 * and unlock the inode.
246 if (unlock && (inode->i_state & I_NEW)) {
247 xfs_set_inodeops(inode);
248 unlock_new_inode(inode);
256 struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
258 filemap_flush(inode->i_mapping);
265 sync_blockdev(XFS_ITOV(ip)->v_vfsp->vfs_super->s_bdev);
266 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
273 struct block_device **bdevp)
277 *bdevp = open_bdev_excl(name, 0, mp);
278 if (IS_ERR(*bdevp)) {
279 error = PTR_ERR(*bdevp);
280 printk("XFS: Invalid device [%s], error=%d\n", name, error);
288 struct block_device *bdev)
291 close_bdev_excl(bdev);
295 STATIC struct inode *
297 struct super_block *sb)
301 vp = (vnode_t *)kmem_cache_alloc(linvfs_inode_zone,
302 kmem_flags_convert(KM_SLEEP));
305 return LINVFS_GET_IP(vp);
309 linvfs_destroy_inode(
312 kmem_cache_free(linvfs_inode_zone, LINVFS_GET_VP(inode));
318 unsigned int gfp_mask)
323 pages = kmem_zone_shrink(linvfs_inode_zone);
324 pages += kmem_zone_shrink(xfs_inode_zone);
331 kmem_cache_t *cachep,
334 vnode_t *vp = (vnode_t *)data;
336 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
337 SLAB_CTOR_CONSTRUCTOR)
338 inode_init_once(LINVFS_GET_IP(vp));
342 init_inodecache( void )
344 linvfs_inode_zone = kmem_cache_create("linvfs_icache",
346 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
349 if (linvfs_inode_zone == NULL)
355 destroy_inodecache( void )
357 if (kmem_cache_destroy(linvfs_inode_zone))
358 printk(KERN_WARNING "%s: cache still in use!\n", __FUNCTION__);
362 * Attempt to flush the inode, this will actually fail
363 * if the inode is pinned, but we dirty the inode again
364 * at the point when it is unpinned after a log write,
365 * since this is when the inode itself becomes flushable.
372 vnode_t *vp = LINVFS_GET_VP(inode);
373 int error, flags = FLUSH_INODE;
376 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
379 VOP_IFLUSH(vp, flags, error);
387 vnode_t *vp = LINVFS_GET_VP(inode);
391 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
393 * Do all our cleanup, and remove this vnode.
400 #define SYNCD_FLAGS (SYNC_FSDATA|SYNC_BDFLUSH|SYNC_ATTR)
406 vfs_t *vfsp = (vfs_t *) arg;
409 daemonize("xfssyncd");
411 vfsp->vfs_sync_task = current;
413 wake_up(&vfsp->vfs_wait_sync_task);
416 set_current_state(TASK_INTERRUPTIBLE);
417 schedule_timeout((xfs_syncd_centisecs * HZ) / 100);
419 if (current->flags & PF_FREEZE)
420 refrigerator(PF_FREEZE);
421 if (vfsp->vfs_flag & VFS_UMOUNT)
423 if (vfsp->vfs_flag & VFS_RDONLY)
425 VFS_SYNC(vfsp, SYNCD_FLAGS, NULL, error);
427 vfsp->vfs_sync_seq++;
429 wake_up(&vfsp->vfs_wait_single_sync_task);
432 vfsp->vfs_sync_task = NULL;
434 wake_up(&vfsp->vfs_wait_sync_task);
445 pid = kernel_thread(xfssyncd, (void *) vfsp,
446 CLONE_VM | CLONE_FS | CLONE_FILES);
449 wait_event(vfsp->vfs_wait_sync_task, vfsp->vfs_sync_task);
457 vfsp->vfs_flag |= VFS_UMOUNT;
460 wake_up_process(vfsp->vfs_sync_task);
461 wait_event(vfsp->vfs_wait_sync_task, !vfsp->vfs_sync_task);
466 struct super_block *sb)
468 vfs_t *vfsp = LINVFS_GET_VFS(sb);
471 linvfs_stop_syncd(vfsp);
472 VFS_SYNC(vfsp, SYNC_ATTR|SYNC_DELWRI, NULL, error);
474 VFS_UNMOUNT(vfsp, 0, NULL, error);
476 printk("XFS unmount got error %d\n", error);
477 printk("%s: vfsp/0x%p left dangling!\n", __FUNCTION__, vfsp);
481 vfs_deallocate(vfsp);
486 struct super_block *sb)
488 vfs_t *vfsp = LINVFS_GET_VFS(sb);
491 if (sb->s_flags & MS_RDONLY) {
492 sb->s_dirt = 0; /* paranoia */
495 /* Push the log and superblock a little */
496 VFS_SYNC(vfsp, SYNC_FSDATA, NULL, error);
502 struct super_block *sb,
505 vfs_t *vfsp = LINVFS_GET_VFS(sb);
507 int flags = SYNC_FSDATA;
512 VFS_SYNC(vfsp, flags, NULL, error);
515 if (unlikely(laptop_mode)) {
516 int prev_sync_seq = vfsp->vfs_sync_seq;
518 * The disk must be active because we're syncing.
519 * We schedule syncd now (now that the disk is
520 * active) instead of later (when it might not be).
522 wake_up_process(vfsp->vfs_sync_task);
524 * We have to wait for the sync iteration to complete.
525 * If we don't, the disk activity caused by the sync
526 * will come after the sync is completed, and that
527 * triggers another sync from laptop mode.
529 wait_event(vfsp->vfs_wait_single_sync_task,
530 vfsp->vfs_sync_seq != prev_sync_seq);
538 struct super_block *sb,
539 struct kstatfs *statp)
541 vfs_t *vfsp = LINVFS_GET_VFS(sb);
544 VFS_STATVFS(vfsp, statp, NULL, error);
550 struct super_block *sb,
554 vfs_t *vfsp = LINVFS_GET_VFS(sb);
555 struct xfs_mount_args *args = xfs_args_allocate(sb);
558 VFS_PARSEARGS(vfsp, options, args, 1, error);
560 VFS_MNTUPDATE(vfsp, flags, args, error);
561 kmem_free(args, sizeof(*args));
567 struct super_block *sb)
569 VFS_FREEZE(LINVFS_GET_VFS(sb));
572 STATIC struct dentry *
574 struct dentry *child)
578 struct dentry *parent;
579 struct inode *ip = NULL;
580 struct dentry dotdot;
582 dotdot.d_name.name = "..";
583 dotdot.d_name.len = 2;
587 vp = LINVFS_GET_VP(child->d_inode);
588 VOP_LOOKUP(vp, &dotdot, &cvp, 0, NULL, NULL, error);
592 ip = LINVFS_GET_IP(cvp);
595 return ERR_PTR(-EACCES);
599 return ERR_PTR(-error);
600 parent = d_alloc_anon(ip);
603 parent = ERR_PTR(-ENOMEM);
608 STATIC struct dentry *
610 struct super_block *sb,
615 struct dentry *result;
617 vfs_t *vfsp = LINVFS_GET_VFS(sb);
620 xfid.fid_len = sizeof(xfs_fid2_t) - sizeof(xfid.fid_len);
622 xfid.fid_gen = ((__u32 *)data)[1];
623 xfid.fid_ino = ((__u32 *)data)[0];
625 VFS_VGET(vfsp, &vp, (fid_t *)&xfid, error);
626 if (error || vp == NULL)
627 return ERR_PTR(-ESTALE) ;
629 inode = LINVFS_GET_IP(vp);
630 result = d_alloc_anon(inode);
633 return ERR_PTR(-ENOMEM);
641 struct vfsmount *mnt)
643 struct vfs *vfsp = LINVFS_GET_VFS(mnt->mnt_sb);
646 VFS_SHOWARGS(vfsp, m, error);
652 struct super_block *sb,
653 struct fs_quota_stat *fqs)
655 struct vfs *vfsp = LINVFS_GET_VFS(sb);
658 VFS_QUOTACTL(vfsp, Q_XGETQSTAT, 0, (caddr_t)fqs, error);
664 struct super_block *sb,
668 struct vfs *vfsp = LINVFS_GET_VFS(sb);
671 VFS_QUOTACTL(vfsp, op, 0, (caddr_t)&flags, error);
677 struct super_block *sb,
680 struct fs_disk_quota *fdq)
682 struct vfs *vfsp = LINVFS_GET_VFS(sb);
685 getmode = (type == GRPQUOTA) ? Q_XGETGQUOTA : Q_XGETQUOTA;
686 VFS_QUOTACTL(vfsp, getmode, id, (caddr_t)fdq, error);
692 struct super_block *sb,
695 struct fs_disk_quota *fdq)
697 struct vfs *vfsp = LINVFS_GET_VFS(sb);
700 setmode = (type == GRPQUOTA) ? Q_XSETGQLIM : Q_XSETQLIM;
701 VFS_QUOTACTL(vfsp, setmode, id, (caddr_t)fdq, error);
707 struct super_block *sb,
712 struct vfs *vfsp = vfs_allocate();
713 struct xfs_mount_args *args = xfs_args_allocate(sb);
714 struct kstatfs statvfs;
717 vfsp->vfs_super = sb;
718 LINVFS_SET_VFS(sb, vfsp);
719 if (sb->s_flags & MS_RDONLY)
720 vfsp->vfs_flag |= VFS_RDONLY;
721 bhv_insert_all_vfsops(vfsp);
723 VFS_PARSEARGS(vfsp, (char *)data, args, 0, error);
725 bhv_remove_all_vfsops(vfsp, 1);
729 sb_min_blocksize(sb, BBSIZE);
730 sb->s_export_op = &linvfs_export_ops;
731 sb->s_qcop = &linvfs_qops;
732 sb->s_op = &linvfs_sops;
734 VFS_MOUNT(vfsp, args, NULL, error);
736 bhv_remove_all_vfsops(vfsp, 1);
740 VFS_STATVFS(vfsp, &statvfs, NULL, error);
745 sb->s_magic = statvfs.f_type;
746 sb->s_blocksize = statvfs.f_bsize;
747 sb->s_blocksize_bits = ffs(statvfs.f_bsize) - 1;
748 sb->s_maxbytes = xfs_max_file_offset(sb->s_blocksize_bits);
749 set_posix_acl_flag(sb);
751 VFS_ROOT(vfsp, &rootvp, error);
755 sb->s_root = d_alloc_root(LINVFS_GET_IP(rootvp));
760 if (is_bad_inode(sb->s_root->d_inode)) {
764 if ((error = linvfs_start_syncd(vfsp)))
766 vn_trace_exit(rootvp, __FUNCTION__, (inst_t *)__return_address);
768 kmem_free(args, sizeof(*args));
780 VFS_UNMOUNT(vfsp, 0, NULL, error2);
783 vfs_deallocate(vfsp);
784 kmem_free(args, sizeof(*args));
788 STATIC struct super_block *
790 struct file_system_type *fs_type,
792 const char *dev_name,
795 return get_sb_bdev(fs_type, flags, dev_name, data, linvfs_fill_super);
799 STATIC struct export_operations linvfs_export_ops = {
800 .get_parent = linvfs_get_parent,
801 .get_dentry = linvfs_get_dentry,
804 STATIC struct super_operations linvfs_sops = {
805 .alloc_inode = linvfs_alloc_inode,
806 .destroy_inode = linvfs_destroy_inode,
807 .write_inode = linvfs_write_inode,
808 .clear_inode = linvfs_clear_inode,
809 .put_super = linvfs_put_super,
810 .write_super = linvfs_write_super,
811 .sync_fs = linvfs_sync_super,
812 .write_super_lockfs = linvfs_freeze_fs,
813 .statfs = linvfs_statfs,
814 .remount_fs = linvfs_remount,
815 .show_options = linvfs_show_options,
818 STATIC struct quotactl_ops linvfs_qops = {
819 .get_xstate = linvfs_getxstate,
820 .set_xstate = linvfs_setxstate,
821 .get_xquota = linvfs_getxquota,
822 .set_xquota = linvfs_setxquota,
825 STATIC struct file_system_type xfs_fs_type = {
826 .owner = THIS_MODULE,
828 .get_sb = linvfs_get_sb,
829 .kill_sb = kill_block_super,
830 .fs_flags = FS_REQUIRES_DEV,
839 static char message[] __initdata = KERN_INFO \
840 XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled\n";
845 xfs_physmem = si.totalram;
849 error = init_inodecache();
851 goto undo_inodecache;
853 error = pagebuf_init();
862 xfs_inode_shaker = kmem_shake_register(xfs_inode_shake);
863 if (!xfs_inode_shaker) {
868 error = register_filesystem(&xfs_fs_type);
871 XFS_DM_INIT(&xfs_fs_type);
875 kmem_shake_deregister(xfs_inode_shaker);
881 destroy_inodecache();
891 XFS_DM_EXIT(&xfs_fs_type);
892 unregister_filesystem(&xfs_fs_type);
893 kmem_shake_deregister(xfs_inode_shaker);
896 destroy_inodecache();
900 module_init(init_xfs_fs);
901 module_exit(exit_xfs_fs);
903 MODULE_AUTHOR("Silicon Graphics, Inc.");
904 MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
905 MODULE_LICENSE("GPL");