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_cache_t * linvfs_inode_cachep;
81 STATIC struct xfs_mount_args *
83 struct super_block *sb)
85 struct xfs_mount_args *args;
87 args = kmem_zalloc(sizeof(struct xfs_mount_args), KM_SLEEP);
88 args->logbufs = args->logbufsize = -1;
89 strncpy(args->fsname, sb->s_id, MAXNAMELEN);
91 /* Copy the already-parsed mount(2) flags we're interested in */
92 if (sb->s_flags & MS_NOATIME)
93 args->flags |= XFSMNT_NOATIME;
95 /* Default to 32 bit inodes on Linux all the time */
96 args->flags |= XFSMNT_32BITINODES;
103 unsigned int blockshift)
105 unsigned int pagefactor = 1;
106 unsigned int bitshift = BITS_PER_LONG - 1;
108 /* Figure out maximum filesize, on Linux this can depend on
109 * the filesystem blocksize (on 32 bit platforms).
110 * __block_prepare_write does this in an [unsigned] long...
111 * page->index << (PAGE_CACHE_SHIFT - bbits)
112 * So, for page sized blocks (4K on 32 bit platforms),
113 * this wraps at around 8Tb (hence MAX_LFS_FILESIZE which is
114 * (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
115 * but for smaller blocksizes it is less (bbits = log2 bsize).
116 * Note1: get_block_t takes a long (implicit cast from above)
117 * Note2: The Large Block Device (LBD and HAVE_SECTOR_T) patch
118 * can optionally convert the [unsigned] long from above into
119 * an [unsigned] long long.
122 #if BITS_PER_LONG == 32
123 # if defined(CONFIG_LBD)
124 ASSERT(sizeof(sector_t) == 8);
125 pagefactor = PAGE_CACHE_SIZE;
126 bitshift = BITS_PER_LONG;
128 pagefactor = PAGE_CACHE_SIZE >> (PAGE_CACHE_SHIFT - blockshift);
132 return (((__uint64_t)pagefactor) << bitshift) - 1;
135 STATIC __inline__ void
139 vnode_t *vp = LINVFS_GET_VP(inode);
141 if (vp->v_type == VNON) {
142 make_bad_inode(inode);
143 } else if (S_ISREG(inode->i_mode)) {
144 inode->i_op = &linvfs_file_inode_operations;
145 inode->i_fop = &linvfs_file_operations;
146 inode->i_mapping->a_ops = &linvfs_aops;
147 } else if (S_ISDIR(inode->i_mode)) {
148 inode->i_op = &linvfs_dir_inode_operations;
149 inode->i_fop = &linvfs_dir_operations;
150 } else if (S_ISLNK(inode->i_mode)) {
151 inode->i_op = &linvfs_symlink_inode_operations;
153 inode->i_mapping->a_ops = &linvfs_aops;
155 inode->i_op = &linvfs_file_inode_operations;
156 init_special_inode(inode, inode->i_mode, inode->i_rdev);
160 STATIC __inline__ void
161 xfs_revalidate_inode(
166 struct inode *inode = LINVFS_GET_IP(vp);
168 inode->i_mode = (ip->i_d.di_mode & MODEMASK) | VTTOIF(vp->v_type);
169 inode->i_nlink = ip->i_d.di_nlink;
170 inode->i_uid = ip->i_d.di_uid;
171 inode->i_gid = ip->i_d.di_gid;
172 if (((1 << vp->v_type) & ((1<<VBLK) | (1<<VCHR))) == 0) {
175 xfs_dev_t dev = ip->i_df.if_u2.if_rdev;
176 inode->i_rdev = MKDEV(sysv_major(dev) & 0x1ff, sysv_minor(dev));
178 inode->i_blksize = PAGE_CACHE_SIZE;
179 inode->i_generation = ip->i_d.di_gen;
180 i_size_write(inode, ip->i_d.di_size);
182 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
183 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
184 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
185 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
186 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
187 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
188 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
189 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
190 inode->i_flags |= S_IMMUTABLE;
192 inode->i_flags &= ~S_IMMUTABLE;
193 if (ip->i_d.di_flags & XFS_DIFLAG_IUNLINK)
194 inode->i_flags |= S_IUNLINK;
196 inode->i_flags &= ~S_IUNLINK;
197 if (ip->i_d.di_flags & XFS_DIFLAG_BARRIER)
198 inode->i_flags |= S_BARRIER;
200 inode->i_flags &= ~S_BARRIER;
201 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
202 inode->i_flags |= S_APPEND;
204 inode->i_flags &= ~S_APPEND;
205 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
206 inode->i_flags |= S_SYNC;
208 inode->i_flags &= ~S_SYNC;
209 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
210 inode->i_flags |= S_NOATIME;
212 inode->i_flags &= ~S_NOATIME;
213 vp->v_flag &= ~VMODIFIED;
217 xfs_initialize_vnode(
220 bhv_desc_t *inode_bhv,
223 xfs_inode_t *ip = XFS_BHVTOI(inode_bhv);
224 struct inode *inode = LINVFS_GET_IP(vp);
226 if (!inode_bhv->bd_vobj) {
227 vp->v_vfsp = bhvtovfs(bdp);
228 bhv_desc_init(inode_bhv, ip, vp, &xfs_vnodeops);
229 bhv_insert(VN_BHV_HEAD(vp), inode_bhv);
232 vp->v_type = IFTOVT(ip->i_d.di_mode);
234 /* Have we been called during the new inode create process,
235 * in which case we are too early to fill in the Linux inode.
237 if (vp->v_type == VNON)
240 xfs_revalidate_inode(XFS_BHVTOM(bdp), vp, ip);
242 /* For new inodes we need to set the ops vectors,
243 * and unlock the inode.
245 if (unlock && (inode->i_state & I_NEW)) {
246 xfs_set_inodeops(inode);
247 unlock_new_inode(inode);
255 struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
257 filemap_flush(inode->i_mapping);
264 sync_blockdev(XFS_ITOV(ip)->v_vfsp->vfs_super->s_bdev);
265 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
272 struct block_device **bdevp)
276 *bdevp = open_bdev_excl(name, 0, mp);
277 if (IS_ERR(*bdevp)) {
278 error = PTR_ERR(*bdevp);
279 printk("XFS: Invalid device [%s], error=%d\n", name, error);
287 struct block_device *bdev)
290 close_bdev_excl(bdev);
294 STATIC struct inode *
296 struct super_block *sb)
300 vp = (vnode_t *)kmem_cache_alloc(linvfs_inode_cachep,
301 kmem_flags_convert(KM_SLEEP));
304 return LINVFS_GET_IP(vp);
308 linvfs_destroy_inode(
311 kmem_cache_free(linvfs_inode_cachep, LINVFS_GET_VP(inode));
317 kmem_cache_t *cachep,
320 vnode_t *vp = (vnode_t *)data;
322 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
323 SLAB_CTOR_CONSTRUCTOR)
324 inode_init_once(LINVFS_GET_IP(vp));
328 init_inodecache( void )
330 linvfs_inode_cachep = kmem_cache_create("linvfs_icache",
332 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
335 if (linvfs_inode_cachep == NULL)
341 destroy_inodecache( void )
343 if (kmem_cache_destroy(linvfs_inode_cachep))
344 printk(KERN_WARNING "%s: cache still in use!\n", __FUNCTION__);
348 * Attempt to flush the inode, this will actually fail
349 * if the inode is pinned, but we dirty the inode again
350 * at the point when it is unpinned after a log write,
351 * since this is when the inode itself becomes flushable.
358 vnode_t *vp = LINVFS_GET_VP(inode);
359 int error, flags = FLUSH_INODE;
362 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
365 VOP_IFLUSH(vp, flags, error);
373 vnode_t *vp = LINVFS_GET_VP(inode);
377 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
379 * Do all our cleanup, and remove this vnode.
386 #define SYNCD_FLAGS (SYNC_FSDATA|SYNC_BDFLUSH|SYNC_ATTR)
392 vfs_t *vfsp = (vfs_t *) arg;
395 daemonize("xfssyncd");
397 vfsp->vfs_sync_task = current;
399 wake_up(&vfsp->vfs_wait_sync_task);
402 set_current_state(TASK_INTERRUPTIBLE);
403 schedule_timeout((xfs_syncd_centisecs * HZ) / 100);
405 if (current->flags & PF_FREEZE)
406 refrigerator(PF_FREEZE);
407 if (vfsp->vfs_flag & VFS_UMOUNT)
409 if (vfsp->vfs_flag & VFS_RDONLY)
411 VFS_SYNC(vfsp, SYNCD_FLAGS, NULL, error);
413 vfsp->vfs_sync_seq++;
415 wake_up(&vfsp->vfs_wait_single_sync_task);
418 vfsp->vfs_sync_task = NULL;
420 wake_up(&vfsp->vfs_wait_sync_task);
431 pid = kernel_thread(xfssyncd, (void *) vfsp,
432 CLONE_VM | CLONE_FS | CLONE_FILES);
435 wait_event(vfsp->vfs_wait_sync_task, vfsp->vfs_sync_task);
443 vfsp->vfs_flag |= VFS_UMOUNT;
446 wake_up_process(vfsp->vfs_sync_task);
447 wait_event(vfsp->vfs_wait_sync_task, !vfsp->vfs_sync_task);
452 struct super_block *sb)
454 vfs_t *vfsp = LINVFS_GET_VFS(sb);
457 linvfs_stop_syncd(vfsp);
458 VFS_SYNC(vfsp, SYNC_ATTR|SYNC_DELWRI, NULL, error);
460 VFS_UNMOUNT(vfsp, 0, NULL, error);
462 printk("XFS unmount got error %d\n", error);
463 printk("%s: vfsp/0x%p left dangling!\n", __FUNCTION__, vfsp);
467 vfs_deallocate(vfsp);
472 struct super_block *sb)
474 vfs_t *vfsp = LINVFS_GET_VFS(sb);
477 if (sb->s_flags & MS_RDONLY) {
478 sb->s_dirt = 0; /* paranoia */
481 /* Push the log and superblock a little */
482 VFS_SYNC(vfsp, SYNC_FSDATA, NULL, error);
488 struct super_block *sb,
491 vfs_t *vfsp = LINVFS_GET_VFS(sb);
493 int flags = SYNC_FSDATA;
498 VFS_SYNC(vfsp, flags, NULL, error);
501 if (unlikely(laptop_mode)) {
502 int prev_sync_seq = vfsp->vfs_sync_seq;
504 * The disk must be active because we're syncing.
505 * We schedule syncd now (now that the disk is
506 * active) instead of later (when it might not be).
508 wake_up_process(vfsp->vfs_sync_task);
510 * We have to wait for the sync iteration to complete.
511 * If we don't, the disk activity caused by the sync
512 * will come after the sync is completed, and that
513 * triggers another sync from laptop mode.
515 wait_event(vfsp->vfs_wait_single_sync_task,
516 vfsp->vfs_sync_seq != prev_sync_seq);
524 struct super_block *sb,
525 struct kstatfs *statp)
527 vfs_t *vfsp = LINVFS_GET_VFS(sb);
530 VFS_STATVFS(vfsp, statp, NULL, error);
536 struct super_block *sb,
540 vfs_t *vfsp = LINVFS_GET_VFS(sb);
541 struct xfs_mount_args *args = xfs_args_allocate(sb);
544 VFS_PARSEARGS(vfsp, options, args, 1, error);
546 VFS_MNTUPDATE(vfsp, flags, args, error);
547 kmem_free(args, sizeof(*args));
553 struct super_block *sb)
555 VFS_FREEZE(LINVFS_GET_VFS(sb));
558 STATIC struct dentry *
560 struct dentry *child)
564 struct dentry *parent;
565 struct inode *ip = NULL;
566 struct dentry dotdot;
568 dotdot.d_name.name = "..";
569 dotdot.d_name.len = 2;
573 vp = LINVFS_GET_VP(child->d_inode);
574 VOP_LOOKUP(vp, &dotdot, &cvp, 0, NULL, NULL, error);
578 ip = LINVFS_GET_IP(cvp);
581 return ERR_PTR(-EACCES);
585 return ERR_PTR(-error);
586 parent = d_alloc_anon(ip);
589 parent = ERR_PTR(-ENOMEM);
594 STATIC struct dentry *
596 struct super_block *sb,
601 struct dentry *result;
603 vfs_t *vfsp = LINVFS_GET_VFS(sb);
606 xfid.fid_len = sizeof(xfs_fid2_t) - sizeof(xfid.fid_len);
608 xfid.fid_gen = ((__u32 *)data)[1];
609 xfid.fid_ino = ((__u32 *)data)[0];
611 VFS_VGET(vfsp, &vp, (fid_t *)&xfid, error);
612 if (error || vp == NULL)
613 return ERR_PTR(-ESTALE) ;
615 inode = LINVFS_GET_IP(vp);
616 result = d_alloc_anon(inode);
619 return ERR_PTR(-ENOMEM);
627 struct vfsmount *mnt)
629 struct vfs *vfsp = LINVFS_GET_VFS(mnt->mnt_sb);
632 VFS_SHOWARGS(vfsp, m, error);
638 struct super_block *sb,
639 struct fs_quota_stat *fqs)
641 struct vfs *vfsp = LINVFS_GET_VFS(sb);
644 VFS_QUOTACTL(vfsp, Q_XGETQSTAT, 0, (caddr_t)fqs, error);
650 struct super_block *sb,
654 struct vfs *vfsp = LINVFS_GET_VFS(sb);
657 VFS_QUOTACTL(vfsp, op, 0, (caddr_t)&flags, error);
663 struct super_block *sb,
666 struct fs_disk_quota *fdq)
668 struct vfs *vfsp = LINVFS_GET_VFS(sb);
671 getmode = (type == GRPQUOTA) ? Q_XGETGQUOTA : Q_XGETQUOTA;
672 VFS_QUOTACTL(vfsp, getmode, id, (caddr_t)fdq, error);
678 struct super_block *sb,
681 struct fs_disk_quota *fdq)
683 struct vfs *vfsp = LINVFS_GET_VFS(sb);
686 setmode = (type == GRPQUOTA) ? Q_XSETGQLIM : Q_XSETQLIM;
687 VFS_QUOTACTL(vfsp, setmode, id, (caddr_t)fdq, error);
693 struct super_block *sb,
698 struct vfs *vfsp = vfs_allocate();
699 struct xfs_mount_args *args = xfs_args_allocate(sb);
700 struct kstatfs statvfs;
703 vfsp->vfs_super = sb;
704 LINVFS_SET_VFS(sb, vfsp);
705 if (sb->s_flags & MS_RDONLY)
706 vfsp->vfs_flag |= VFS_RDONLY;
707 bhv_insert_all_vfsops(vfsp);
709 VFS_PARSEARGS(vfsp, (char *)data, args, 0, error);
711 bhv_remove_all_vfsops(vfsp, 1);
715 sb_min_blocksize(sb, BBSIZE);
716 sb->s_export_op = &linvfs_export_ops;
717 sb->s_qcop = &linvfs_qops;
718 sb->s_op = &linvfs_sops;
720 VFS_MOUNT(vfsp, args, NULL, error);
722 bhv_remove_all_vfsops(vfsp, 1);
726 VFS_STATVFS(vfsp, &statvfs, NULL, error);
731 sb->s_magic = statvfs.f_type;
732 sb->s_blocksize = statvfs.f_bsize;
733 sb->s_blocksize_bits = ffs(statvfs.f_bsize) - 1;
734 sb->s_maxbytes = xfs_max_file_offset(sb->s_blocksize_bits);
735 set_posix_acl_flag(sb);
737 VFS_ROOT(vfsp, &rootvp, error);
741 sb->s_root = d_alloc_root(LINVFS_GET_IP(rootvp));
746 if (is_bad_inode(sb->s_root->d_inode)) {
750 if ((error = linvfs_start_syncd(vfsp)))
752 vn_trace_exit(rootvp, __FUNCTION__, (inst_t *)__return_address);
754 kmem_free(args, sizeof(*args));
766 VFS_UNMOUNT(vfsp, 0, NULL, error2);
769 vfs_deallocate(vfsp);
770 kmem_free(args, sizeof(*args));
774 STATIC struct super_block *
776 struct file_system_type *fs_type,
778 const char *dev_name,
781 return get_sb_bdev(fs_type, flags, dev_name, data, linvfs_fill_super);
785 STATIC struct export_operations linvfs_export_ops = {
786 .get_parent = linvfs_get_parent,
787 .get_dentry = linvfs_get_dentry,
790 STATIC struct super_operations linvfs_sops = {
791 .alloc_inode = linvfs_alloc_inode,
792 .destroy_inode = linvfs_destroy_inode,
793 .write_inode = linvfs_write_inode,
794 .clear_inode = linvfs_clear_inode,
795 .put_super = linvfs_put_super,
796 .write_super = linvfs_write_super,
797 .sync_fs = linvfs_sync_super,
798 .write_super_lockfs = linvfs_freeze_fs,
799 .statfs = linvfs_statfs,
800 .remount_fs = linvfs_remount,
801 .show_options = linvfs_show_options,
804 STATIC struct quotactl_ops linvfs_qops = {
805 .get_xstate = linvfs_getxstate,
806 .set_xstate = linvfs_setxstate,
807 .get_xquota = linvfs_getxquota,
808 .set_xquota = linvfs_setxquota,
811 STATIC struct file_system_type xfs_fs_type = {
812 .owner = THIS_MODULE,
814 .get_sb = linvfs_get_sb,
815 .kill_sb = kill_block_super,
816 .fs_flags = FS_REQUIRES_DEV,
825 static char message[] __initdata = KERN_INFO \
826 XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled\n";
831 xfs_physmem = si.totalram;
835 error = init_inodecache();
837 goto undo_inodecache;
839 error = pagebuf_init();
849 error = register_filesystem(&xfs_fs_type);
858 destroy_inodecache();
869 unregister_filesystem(&xfs_fs_type);
872 destroy_inodecache();
876 module_init(init_xfs_fs);
877 module_exit(exit_xfs_fs);
879 MODULE_AUTHOR("Silicon Graphics, Inc.");
880 MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
881 MODULE_LICENSE("GPL");