2 * Copyright (c) 2000-2005 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/writeback.h>
75 STATIC struct quotactl_ops linvfs_qops;
76 STATIC struct super_operations linvfs_sops;
77 STATIC kmem_zone_t *linvfs_inode_zone;
79 STATIC struct xfs_mount_args *
81 struct super_block *sb)
83 struct xfs_mount_args *args;
85 args = kmem_zalloc(sizeof(struct xfs_mount_args), KM_SLEEP);
86 args->logbufs = args->logbufsize = -1;
87 strncpy(args->fsname, sb->s_id, MAXNAMELEN);
89 /* Copy the already-parsed mount(2) flags we're interested in */
90 if (sb->s_flags & MS_NOATIME)
91 args->flags |= XFSMNT_NOATIME;
92 if (sb->s_flags & MS_DIRSYNC)
93 args->flags |= XFSMNT_DIRSYNC;
94 if (sb->s_flags & MS_SYNCHRONOUS)
95 args->flags |= XFSMNT_WSYNC;
97 /* Default to 32 bit inodes on Linux all the time */
98 args->flags |= XFSMNT_32BITINODES;
105 unsigned int blockshift)
107 unsigned int pagefactor = 1;
108 unsigned int bitshift = BITS_PER_LONG - 1;
110 /* Figure out maximum filesize, on Linux this can depend on
111 * the filesystem blocksize (on 32 bit platforms).
112 * __block_prepare_write does this in an [unsigned] long...
113 * page->index << (PAGE_CACHE_SHIFT - bbits)
114 * So, for page sized blocks (4K on 32 bit platforms),
115 * this wraps at around 8Tb (hence MAX_LFS_FILESIZE which is
116 * (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
117 * but for smaller blocksizes it is less (bbits = log2 bsize).
118 * Note1: get_block_t takes a long (implicit cast from above)
119 * Note2: The Large Block Device (LBD and HAVE_SECTOR_T) patch
120 * can optionally convert the [unsigned] long from above into
121 * an [unsigned] long long.
124 #if BITS_PER_LONG == 32
125 # if defined(CONFIG_LBD)
126 ASSERT(sizeof(sector_t) == 8);
127 pagefactor = PAGE_CACHE_SIZE;
128 bitshift = BITS_PER_LONG;
130 pagefactor = PAGE_CACHE_SIZE >> (PAGE_CACHE_SHIFT - blockshift);
134 return (((__uint64_t)pagefactor) << bitshift) - 1;
137 STATIC __inline__ void
141 vnode_t *vp = LINVFS_GET_VP(inode);
143 if (vp->v_type == VNON) {
145 } else if (S_ISREG(inode->i_mode)) {
146 inode->i_op = &linvfs_file_inode_operations;
147 inode->i_fop = &linvfs_file_operations;
148 inode->i_mapping->a_ops = &linvfs_aops;
149 } else if (S_ISDIR(inode->i_mode)) {
150 inode->i_op = &linvfs_dir_inode_operations;
151 inode->i_fop = &linvfs_dir_operations;
152 } else if (S_ISLNK(inode->i_mode)) {
153 inode->i_op = &linvfs_symlink_inode_operations;
155 inode->i_mapping->a_ops = &linvfs_aops;
157 inode->i_op = &linvfs_file_inode_operations;
158 init_special_inode(inode, inode->i_mode, inode->i_rdev);
162 STATIC __inline__ void
163 xfs_revalidate_inode(
168 struct inode *inode = LINVFS_GET_IP(vp);
170 inode->i_mode = (ip->i_d.di_mode & MODEMASK) | VTTOIF(vp->v_type);
171 inode->i_nlink = ip->i_d.di_nlink;
172 inode->i_uid = ip->i_d.di_uid;
173 inode->i_gid = ip->i_d.di_gid;
174 inode->i_xid = ip->i_d.di_xid;
175 if (((1 << vp->v_type) & ((1<<VBLK) | (1<<VCHR))) == 0) {
178 xfs_dev_t dev = ip->i_df.if_u2.if_rdev;
179 inode->i_rdev = MKDEV(sysv_major(dev) & 0x1ff, sysv_minor(dev));
181 inode->i_blksize = PAGE_CACHE_SIZE;
182 inode->i_generation = ip->i_d.di_gen;
183 i_size_write(inode, ip->i_d.di_size);
185 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
186 inode->i_atime.tv_sec = ip->i_d.di_atime.t_sec;
187 inode->i_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
188 inode->i_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
189 inode->i_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
190 inode->i_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
191 inode->i_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
192 if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
193 inode->i_flags |= S_IMMUTABLE;
195 inode->i_flags &= ~S_IMMUTABLE;
196 if (ip->i_d.di_flags & XFS_DIFLAG_IUNLINK)
197 inode->i_flags |= S_IUNLINK;
199 inode->i_flags &= ~S_IUNLINK;
200 if (ip->i_d.di_flags & XFS_DIFLAG_BARRIER)
201 inode->i_flags |= S_BARRIER;
203 inode->i_flags &= ~S_BARRIER;
204 if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
205 inode->i_flags |= S_APPEND;
207 inode->i_flags &= ~S_APPEND;
208 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
209 inode->i_flags |= S_SYNC;
211 inode->i_flags &= ~S_SYNC;
212 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
213 inode->i_flags |= S_NOATIME;
215 inode->i_flags &= ~S_NOATIME;
216 vp->v_flag &= ~VMODIFIED;
220 xfs_initialize_vnode(
223 bhv_desc_t *inode_bhv,
226 xfs_inode_t *ip = XFS_BHVTOI(inode_bhv);
227 struct inode *inode = LINVFS_GET_IP(vp);
229 if (!inode_bhv->bd_vobj) {
230 vp->v_vfsp = bhvtovfs(bdp);
231 bhv_desc_init(inode_bhv, ip, vp, &xfs_vnodeops);
232 bhv_insert(VN_BHV_HEAD(vp), inode_bhv);
236 * We need to set the ops vectors, and unlock the inode, but if
237 * we have been called during the new inode create process, it is
238 * too early to fill in the Linux inode. We will get called a
239 * second time once the inode is properly set up, and then we can
242 if (ip->i_d.di_mode != 0 && unlock && (inode->i_state & I_NEW)) {
243 vp->v_type = IFTOVT(ip->i_d.di_mode);
244 xfs_revalidate_inode(XFS_BHVTOM(bdp), vp, ip);
245 xfs_set_inodeops(inode);
247 ip->i_flags &= ~XFS_INEW;
250 unlock_new_inode(inode);
258 struct block_device **bdevp)
262 *bdevp = open_bdev_excl(name, 0, mp);
263 if (IS_ERR(*bdevp)) {
264 error = PTR_ERR(*bdevp);
265 printk("XFS: Invalid device [%s], error=%d\n", name, error);
273 struct block_device *bdev)
276 close_bdev_excl(bdev);
280 STATIC struct inode *
282 struct super_block *sb)
286 vp = (vnode_t *)kmem_cache_alloc(linvfs_inode_zone,
287 kmem_flags_convert(KM_SLEEP));
290 return LINVFS_GET_IP(vp);
294 linvfs_destroy_inode(
297 kmem_cache_free(linvfs_inode_zone, LINVFS_GET_VP(inode));
303 kmem_cache_t *cachep,
306 vnode_t *vp = (vnode_t *)data;
308 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
309 SLAB_CTOR_CONSTRUCTOR)
310 inode_init_once(LINVFS_GET_IP(vp));
314 init_inodecache( void )
316 linvfs_inode_zone = kmem_cache_create("linvfs_icache",
317 sizeof(vnode_t), 0, SLAB_RECLAIM_ACCOUNT,
319 if (linvfs_inode_zone == NULL)
325 destroy_inodecache( void )
327 if (kmem_cache_destroy(linvfs_inode_zone))
328 printk(KERN_WARNING "%s: cache still in use!\n", __FUNCTION__);
332 * Attempt to flush the inode, this will actually fail
333 * if the inode is pinned, but we dirty the inode again
334 * at the point when it is unpinned after a log write,
335 * since this is when the inode itself becomes flushable.
342 vnode_t *vp = LINVFS_GET_VP(inode);
343 int error = 0, flags = FLUSH_INODE;
346 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
349 VOP_IFLUSH(vp, flags, error);
350 if (error == EAGAIN) {
352 VOP_IFLUSH(vp, flags | FLUSH_LOG, error);
365 vnode_t *vp = LINVFS_GET_VP(inode);
369 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
371 * Do all our cleanup, and remove this vnode.
379 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
380 * Doing this has two advantages:
381 * - It saves on stack space, which is tight in certain situations
382 * - It can be used (with care) as a mechanism to avoid deadlocks.
383 * Flushing while allocating in a full filesystem requires both.
386 xfs_syncd_queue_work(
389 void (*syncer)(vfs_t *, void *))
391 vfs_sync_work_t *work;
393 work = kmem_alloc(sizeof(struct vfs_sync_work), KM_SLEEP);
394 INIT_LIST_HEAD(&work->w_list);
395 work->w_syncer = syncer;
398 spin_lock(&vfs->vfs_sync_lock);
399 list_add_tail(&work->w_list, &vfs->vfs_sync_list);
400 spin_unlock(&vfs->vfs_sync_lock);
401 wake_up_process(vfs->vfs_sync_task);
405 * Flush delayed allocate data, attempting to free up reserved space
406 * from existing allocations. At this point a new allocation attempt
407 * has failed with ENOSPC and we are in the process of scratching our
408 * heads, looking about for more room...
411 xfs_flush_inode_work(
415 filemap_flush(((struct inode *)inode)->i_mapping);
416 iput((struct inode *)inode);
423 struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
424 struct vfs *vfs = XFS_MTOVFS(ip->i_mount);
427 xfs_syncd_queue_work(vfs, inode, xfs_flush_inode_work);
432 * This is the "bigger hammer" version of xfs_flush_inode_work...
433 * (IOW, "If at first you don't succeed, use a Bigger Hammer").
436 xfs_flush_device_work(
440 sync_blockdev(vfs->vfs_super->s_bdev);
441 iput((struct inode *)inode);
448 struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
449 struct vfs *vfs = XFS_MTOVFS(ip->i_mount);
452 xfs_syncd_queue_work(vfs, inode, xfs_flush_device_work);
454 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
457 #define SYNCD_FLAGS (SYNC_FSDATA|SYNC_BDFLUSH|SYNC_ATTR)
465 if (!(vfsp->vfs_flag & VFS_RDONLY))
466 VFS_SYNC(vfsp, SYNCD_FLAGS, NULL, error);
467 vfsp->vfs_sync_seq++;
469 wake_up(&vfsp->vfs_wait_single_sync_task);
477 vfs_t *vfsp = (vfs_t *) arg;
478 struct list_head tmp;
479 struct vfs_sync_work *work, *n;
481 daemonize("xfssyncd");
483 vfsp->vfs_sync_work.w_vfs = vfsp;
484 vfsp->vfs_sync_work.w_syncer = vfs_sync_worker;
485 vfsp->vfs_sync_task = current;
487 wake_up(&vfsp->vfs_wait_sync_task);
489 INIT_LIST_HEAD(&tmp);
490 timeleft = (xfs_syncd_centisecs * HZ) / 100;
492 set_current_state(TASK_INTERRUPTIBLE);
493 timeleft = schedule_timeout(timeleft);
495 try_to_freeze(PF_FREEZE);
496 if (vfsp->vfs_flag & VFS_UMOUNT)
499 spin_lock(&vfsp->vfs_sync_lock);
501 * We can get woken by laptop mode, to do a sync -
502 * that's the (only!) case where the list would be
503 * empty with time remaining.
505 if (!timeleft || list_empty(&vfsp->vfs_sync_list)) {
507 timeleft = (xfs_syncd_centisecs * HZ) / 100;
508 INIT_LIST_HEAD(&vfsp->vfs_sync_work.w_list);
509 list_add_tail(&vfsp->vfs_sync_work.w_list,
510 &vfsp->vfs_sync_list);
512 list_for_each_entry_safe(work, n, &vfsp->vfs_sync_list, w_list)
513 list_move(&work->w_list, &tmp);
514 spin_unlock(&vfsp->vfs_sync_lock);
516 list_for_each_entry_safe(work, n, &tmp, w_list) {
517 (*work->w_syncer)(vfsp, work->w_data);
518 list_del(&work->w_list);
519 if (work == &vfsp->vfs_sync_work)
521 kmem_free(work, sizeof(struct vfs_sync_work));
525 vfsp->vfs_sync_task = NULL;
527 wake_up(&vfsp->vfs_wait_sync_task);
538 pid = kernel_thread(xfssyncd, (void *) vfsp,
539 CLONE_VM | CLONE_FS | CLONE_FILES);
542 wait_event(vfsp->vfs_wait_sync_task, vfsp->vfs_sync_task);
550 vfsp->vfs_flag |= VFS_UMOUNT;
553 wake_up_process(vfsp->vfs_sync_task);
554 wait_event(vfsp->vfs_wait_sync_task, !vfsp->vfs_sync_task);
559 struct super_block *sb)
561 vfs_t *vfsp = LINVFS_GET_VFS(sb);
564 linvfs_stop_syncd(vfsp);
565 VFS_SYNC(vfsp, SYNC_ATTR|SYNC_DELWRI, NULL, error);
567 VFS_UNMOUNT(vfsp, 0, NULL, error);
569 printk("XFS unmount got error %d\n", error);
570 printk("%s: vfsp/0x%p left dangling!\n", __FUNCTION__, vfsp);
574 vfs_deallocate(vfsp);
579 struct super_block *sb)
581 vfs_t *vfsp = LINVFS_GET_VFS(sb);
584 if (sb->s_flags & MS_RDONLY) {
585 sb->s_dirt = 0; /* paranoia */
588 /* Push the log and superblock a little */
589 VFS_SYNC(vfsp, SYNC_FSDATA, NULL, error);
595 struct super_block *sb,
598 vfs_t *vfsp = LINVFS_GET_VFS(sb);
600 int flags = SYNC_FSDATA;
605 VFS_SYNC(vfsp, flags, NULL, error);
608 if (unlikely(laptop_mode)) {
609 int prev_sync_seq = vfsp->vfs_sync_seq;
612 * The disk must be active because we're syncing.
613 * We schedule xfssyncd now (now that the disk is
614 * active) instead of later (when it might not be).
616 wake_up_process(vfsp->vfs_sync_task);
618 * We have to wait for the sync iteration to complete.
619 * If we don't, the disk activity caused by the sync
620 * will come after the sync is completed, and that
621 * triggers another sync from laptop mode.
623 wait_event(vfsp->vfs_wait_single_sync_task,
624 vfsp->vfs_sync_seq != prev_sync_seq);
632 struct super_block *sb,
633 struct kstatfs *statp)
635 vfs_t *vfsp = LINVFS_GET_VFS(sb);
638 VFS_STATVFS(vfsp, statp, NULL, error);
644 struct super_block *sb,
648 vfs_t *vfsp = LINVFS_GET_VFS(sb);
649 struct xfs_mount_args *args = xfs_args_allocate(sb);
652 VFS_PARSEARGS(vfsp, options, args, 1, error);
654 VFS_MNTUPDATE(vfsp, flags, args, error);
655 kmem_free(args, sizeof(*args));
661 struct super_block *sb)
663 VFS_FREEZE(LINVFS_GET_VFS(sb));
669 struct vfsmount *mnt)
671 struct vfs *vfsp = LINVFS_GET_VFS(mnt->mnt_sb);
674 VFS_SHOWARGS(vfsp, m, error);
680 struct super_block *sb,
681 struct fs_quota_stat *fqs)
683 struct vfs *vfsp = LINVFS_GET_VFS(sb);
686 VFS_QUOTACTL(vfsp, Q_XGETQSTAT, 0, (caddr_t)fqs, error);
692 struct super_block *sb,
696 struct vfs *vfsp = LINVFS_GET_VFS(sb);
699 VFS_QUOTACTL(vfsp, op, 0, (caddr_t)&flags, error);
705 struct super_block *sb,
708 struct fs_disk_quota *fdq)
710 struct vfs *vfsp = LINVFS_GET_VFS(sb);
713 getmode = (type == GRPQUOTA) ? Q_XGETGQUOTA : Q_XGETQUOTA;
714 VFS_QUOTACTL(vfsp, getmode, id, (caddr_t)fdq, error);
720 struct super_block *sb,
723 struct fs_disk_quota *fdq)
725 struct vfs *vfsp = LINVFS_GET_VFS(sb);
728 setmode = (type == GRPQUOTA) ? Q_XSETGQLIM : Q_XSETQLIM;
729 VFS_QUOTACTL(vfsp, setmode, id, (caddr_t)fdq, error);
735 struct super_block *sb,
740 struct vfs *vfsp = vfs_allocate();
741 struct xfs_mount_args *args = xfs_args_allocate(sb);
742 struct kstatfs statvfs;
745 vfsp->vfs_super = sb;
746 LINVFS_SET_VFS(sb, vfsp);
747 if (sb->s_flags & MS_RDONLY)
748 vfsp->vfs_flag |= VFS_RDONLY;
749 bhv_insert_all_vfsops(vfsp);
751 VFS_PARSEARGS(vfsp, (char *)data, args, 0, error);
753 bhv_remove_all_vfsops(vfsp, 1);
757 sb_min_blocksize(sb, BBSIZE);
758 #ifdef CONFIG_XFS_EXPORT
759 sb->s_export_op = &linvfs_export_ops;
761 sb->s_qcop = &linvfs_qops;
762 sb->s_op = &linvfs_sops;
764 VFS_MOUNT(vfsp, args, NULL, error);
766 bhv_remove_all_vfsops(vfsp, 1);
770 VFS_STATVFS(vfsp, &statvfs, NULL, error);
775 sb->s_magic = statvfs.f_type;
776 sb->s_blocksize = statvfs.f_bsize;
777 sb->s_blocksize_bits = ffs(statvfs.f_bsize) - 1;
778 sb->s_maxbytes = xfs_max_file_offset(sb->s_blocksize_bits);
780 set_posix_acl_flag(sb);
782 VFS_ROOT(vfsp, &rootvp, error);
786 sb->s_root = d_alloc_root(LINVFS_GET_IP(rootvp));
791 if (is_bad_inode(sb->s_root->d_inode)) {
795 if ((error = linvfs_start_syncd(vfsp)))
797 vn_trace_exit(rootvp, __FUNCTION__, (inst_t *)__return_address);
799 kmem_free(args, sizeof(*args));
811 VFS_UNMOUNT(vfsp, 0, NULL, error2);
814 vfs_deallocate(vfsp);
815 kmem_free(args, sizeof(*args));
819 STATIC struct super_block *
821 struct file_system_type *fs_type,
823 const char *dev_name,
826 return get_sb_bdev(fs_type, flags, dev_name, data, linvfs_fill_super);
829 STATIC struct super_operations linvfs_sops = {
830 .alloc_inode = linvfs_alloc_inode,
831 .destroy_inode = linvfs_destroy_inode,
832 .write_inode = linvfs_write_inode,
833 .clear_inode = linvfs_clear_inode,
834 .put_super = linvfs_put_super,
835 .write_super = linvfs_write_super,
836 .sync_fs = linvfs_sync_super,
837 .write_super_lockfs = linvfs_freeze_fs,
838 .statfs = linvfs_statfs,
839 .remount_fs = linvfs_remount,
840 .show_options = linvfs_show_options,
843 STATIC struct quotactl_ops linvfs_qops = {
844 .get_xstate = linvfs_getxstate,
845 .set_xstate = linvfs_setxstate,
846 .get_xquota = linvfs_getxquota,
847 .set_xquota = linvfs_setxquota,
850 STATIC struct file_system_type xfs_fs_type = {
851 .owner = THIS_MODULE,
853 .get_sb = linvfs_get_sb,
854 .kill_sb = kill_block_super,
855 .fs_flags = FS_REQUIRES_DEV,
864 static char message[] __initdata = KERN_INFO \
865 XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled\n";
870 xfs_physmem = si.totalram;
874 error = init_inodecache();
876 goto undo_inodecache;
878 error = pagebuf_init();
887 error = register_filesystem(&xfs_fs_type);
890 XFS_DM_INIT(&xfs_fs_type);
897 destroy_inodecache();
907 XFS_DM_EXIT(&xfs_fs_type);
908 unregister_filesystem(&xfs_fs_type);
911 destroy_inodecache();
915 module_init(init_xfs_fs);
916 module_exit(exit_xfs_fs);
918 MODULE_AUTHOR("Silicon Graphics, Inc.");
919 MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
920 MODULE_LICENSE("GPL");