2 * Copyright (c) 2000-2003 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/blkdev.h>
71 #include <linux/namei.h>
72 #include <linux/init.h>
73 #include <linux/mount.h>
74 #include <linux/suspend.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,
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_APPEND)
195 inode->i_flags |= S_APPEND;
197 inode->i_flags &= ~S_APPEND;
198 if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
199 inode->i_flags |= S_SYNC;
201 inode->i_flags &= ~S_SYNC;
202 if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
203 inode->i_flags |= S_NOATIME;
205 inode->i_flags &= ~S_NOATIME;
206 vp->v_flag &= ~VMODIFIED;
210 xfs_initialize_vnode(
213 bhv_desc_t *inode_bhv,
216 xfs_inode_t *ip = XFS_BHVTOI(inode_bhv);
217 struct inode *inode = LINVFS_GET_IP(vp);
219 if (!inode_bhv->bd_vobj) {
220 vp->v_vfsp = bhvtovfs(bdp);
221 bhv_desc_init(inode_bhv, ip, vp, &xfs_vnodeops);
222 bhv_insert(VN_BHV_HEAD(vp), inode_bhv);
225 vp->v_type = IFTOVT(ip->i_d.di_mode);
227 /* Have we been called during the new inode create process,
228 * in which case we are too early to fill in the Linux inode.
230 if (vp->v_type == VNON)
233 xfs_revalidate_inode(XFS_BHVTOM(bdp), vp, ip);
235 /* For new inodes we need to set the ops vectors,
236 * and unlock the inode.
238 if (unlock && (inode->i_state & I_NEW)) {
239 xfs_set_inodeops(inode);
240 unlock_new_inode(inode);
250 struct vfs *vfsp = bhvtovfs(bdp);
252 if (flags & IGET_NOALLOC)
253 return ilookup(vfsp->vfs_super, ino);
254 return iget_locked(vfsp->vfs_super, ino);
261 struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
263 filemap_fdatawrite(inode->i_mapping);
270 sync_blockdev(XFS_ITOV(ip)->v_vfsp->vfs_super->s_bdev);
271 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
278 struct block_device **bdevp)
282 *bdevp = open_bdev_excl(name, 0, BDEV_FS, mp);
283 if (IS_ERR(*bdevp)) {
284 error = PTR_ERR(*bdevp);
285 printk("XFS: Invalid device [%s], error=%d\n", name, error);
293 struct block_device *bdev)
296 close_bdev_excl(bdev, BDEV_FS);
303 pagebuf_delwri_flush(btp, PBDF_WAIT, NULL);
310 xfs_flush_buftarg(btp);
311 kmem_free(btp, sizeof(*btp));
315 xfs_readonly_buftarg(
318 return bdev_read_only(btp->pbr_bdev);
325 invalidate_bdev(btp->pbr_bdev, 1);
326 truncate_inode_pages(btp->pbr_mapping, 0LL);
333 return block_size(btp->pbr_bdev);
339 unsigned int blocksize,
340 unsigned int sectorsize)
342 btp->pbr_bsize = blocksize;
343 btp->pbr_sshift = ffs(sectorsize) - 1;
344 btp->pbr_smask = sectorsize - 1;
346 if (set_blocksize(btp->pbr_bdev, sectorsize)) {
348 "XFS: Cannot set_blocksize to %u on device %s\n",
349 sectorsize, XFS_BUFTARG_NAME(btp));
355 struct block_device *bdev)
359 btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
361 btp->pbr_dev = bdev->bd_dev;
362 btp->pbr_bdev = bdev;
363 btp->pbr_mapping = bdev->bd_inode->i_mapping;
364 xfs_setsize_buftarg(btp, PAGE_CACHE_SIZE, bdev_hardsect_size(bdev));
369 STATIC struct inode *
371 struct super_block *sb)
375 vp = (vnode_t *)kmem_cache_alloc(linvfs_inode_cachep,
376 kmem_flags_convert(KM_SLEEP));
379 return LINVFS_GET_IP(vp);
383 linvfs_destroy_inode(
386 kmem_cache_free(linvfs_inode_cachep, LINVFS_GET_VP(inode));
392 kmem_cache_t *cachep,
395 vnode_t *vp = (vnode_t *)data;
397 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
398 SLAB_CTOR_CONSTRUCTOR)
399 inode_init_once(LINVFS_GET_IP(vp));
403 init_inodecache( void )
405 linvfs_inode_cachep = kmem_cache_create("linvfs_icache",
407 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
410 if (linvfs_inode_cachep == NULL)
416 destroy_inodecache( void )
418 if (kmem_cache_destroy(linvfs_inode_cachep))
419 printk(KERN_WARNING "%s: cache still in use!\n", __FUNCTION__);
423 * Attempt to flush the inode, this will actually fail
424 * if the inode is pinned, but we dirty the inode again
425 * at the point when it is unpinned after a log write,
426 * since this is when the inode itself becomes flushable.
433 vnode_t *vp = LINVFS_GET_VP(inode);
434 int error, flags = FLUSH_INODE;
437 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
440 VOP_IFLUSH(vp, flags, error);
448 vnode_t *vp = LINVFS_GET_VP(inode);
452 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
454 * Do all our cleanup, and remove this vnode.
461 #define SYNCD_FLAGS (SYNC_FSDATA|SYNC_BDFLUSH|SYNC_ATTR)
466 vfs_t *vfsp = (vfs_t *) arg;
469 daemonize("xfssyncd");
471 vfsp->vfs_sync_task = current;
473 wake_up(&vfsp->vfs_wait_sync_task);
476 set_current_state(TASK_INTERRUPTIBLE);
477 schedule_timeout(xfs_syncd_interval);
479 if (current->flags & PF_FREEZE)
480 refrigerator(PF_IOTHREAD);
481 if (vfsp->vfs_flag & VFS_UMOUNT)
483 if (vfsp->vfs_flag & VFS_RDONLY)
485 VFS_SYNC(vfsp, SYNCD_FLAGS, NULL, error);
488 vfsp->vfs_sync_task = NULL;
490 wake_up(&vfsp->vfs_wait_sync_task);
496 linvfs_start_syncd(vfs_t *vfsp)
500 pid = kernel_thread(syncd, (void *) vfsp,
501 CLONE_VM | CLONE_FS | CLONE_FILES);
504 wait_event(vfsp->vfs_wait_sync_task, vfsp->vfs_sync_task);
509 linvfs_stop_syncd(vfs_t *vfsp)
511 vfsp->vfs_flag |= VFS_UMOUNT;
514 wake_up_process(vfsp->vfs_sync_task);
515 wait_event(vfsp->vfs_wait_sync_task, !vfsp->vfs_sync_task);
520 struct super_block *sb)
522 vfs_t *vfsp = LINVFS_GET_VFS(sb);
525 linvfs_stop_syncd(vfsp);
526 VFS_SYNC(vfsp, SYNC_ATTR|SYNC_DELWRI, NULL, error);
528 VFS_UNMOUNT(vfsp, 0, NULL, error);
530 printk("XFS unmount got error %d\n", error);
531 printk("%s: vfsp/0x%p left dangling!\n", __FUNCTION__, vfsp);
535 vfs_deallocate(vfsp);
540 struct super_block *sb)
542 vfs_t *vfsp = LINVFS_GET_VFS(sb);
545 if (sb->s_flags & MS_RDONLY) {
546 sb->s_dirt = 0; /* paranoia */
549 /* Push the log and superblock a little */
550 VFS_SYNC(vfsp, SYNC_FSDATA, NULL, error);
556 struct super_block *sb,
559 vfs_t *vfsp = LINVFS_GET_VFS(sb);
561 int flags = SYNC_FSDATA;
566 VFS_SYNC(vfsp, flags, NULL, error);
574 struct super_block *sb,
575 struct kstatfs *statp)
577 vfs_t *vfsp = LINVFS_GET_VFS(sb);
580 VFS_STATVFS(vfsp, statp, NULL, error);
586 struct super_block *sb,
590 vfs_t *vfsp = LINVFS_GET_VFS(sb);
591 struct xfs_mount_args *args = xfs_args_allocate(sb);
594 VFS_PARSEARGS(vfsp, options, args, 1, error);
596 VFS_MNTUPDATE(vfsp, flags, args, error);
597 kmem_free(args, sizeof(*args));
603 struct super_block *sb)
605 vfs_t *vfsp = LINVFS_GET_VFS(sb);
609 if (sb->s_flags & MS_RDONLY)
611 VFS_ROOT(vfsp, &vp, error);
612 VOP_IOCTL(vp, LINVFS_GET_IP(vp), NULL, 0, XFS_IOC_FREEZE, 0, error);
618 struct super_block *sb)
620 vfs_t *vfsp = LINVFS_GET_VFS(sb);
624 VFS_ROOT(vfsp, &vp, error);
625 VOP_IOCTL(vp, LINVFS_GET_IP(vp), NULL, 0, XFS_IOC_THAW, 0, error);
629 STATIC struct dentry *
631 struct dentry *child)
635 struct dentry *parent;
636 struct inode *ip = NULL;
637 struct dentry dotdot;
639 dotdot.d_name.name = "..";
640 dotdot.d_name.len = 2;
644 vp = LINVFS_GET_VP(child->d_inode);
645 VOP_LOOKUP(vp, &dotdot, &cvp, 0, NULL, NULL, error);
649 ip = LINVFS_GET_IP(cvp);
652 return ERR_PTR(-EACCES);
656 return ERR_PTR(-error);
657 parent = d_alloc_anon(ip);
660 parent = ERR_PTR(-ENOMEM);
665 STATIC struct dentry *
667 struct super_block *sb,
672 struct dentry *result;
674 vfs_t *vfsp = LINVFS_GET_VFS(sb);
677 xfid.fid_len = sizeof(xfs_fid2_t) - sizeof(xfid.fid_len);
679 xfid.fid_gen = ((__u32 *)data)[1];
680 xfid.fid_ino = ((__u32 *)data)[0];
682 VFS_VGET(vfsp, &vp, (fid_t *)&xfid, error);
683 if (error || vp == NULL)
684 return ERR_PTR(-ESTALE) ;
686 inode = LINVFS_GET_IP(vp);
687 result = d_alloc_anon(inode);
690 return ERR_PTR(-ENOMEM);
698 struct vfsmount *mnt)
700 struct vfs *vfsp = LINVFS_GET_VFS(mnt->mnt_sb);
703 VFS_SHOWARGS(vfsp, m, error);
709 struct super_block *sb,
710 struct fs_quota_stat *fqs)
712 struct vfs *vfsp = LINVFS_GET_VFS(sb);
715 VFS_QUOTACTL(vfsp, Q_XGETQSTAT, 0, (caddr_t)fqs, error);
721 struct super_block *sb,
725 struct vfs *vfsp = LINVFS_GET_VFS(sb);
728 VFS_QUOTACTL(vfsp, op, 0, (caddr_t)&flags, error);
734 struct super_block *sb,
737 struct fs_disk_quota *fdq)
739 struct vfs *vfsp = LINVFS_GET_VFS(sb);
742 getmode = (type == GRPQUOTA) ? Q_XGETGQUOTA : Q_XGETQUOTA;
743 VFS_QUOTACTL(vfsp, getmode, id, (caddr_t)fdq, error);
749 struct super_block *sb,
752 struct fs_disk_quota *fdq)
754 struct vfs *vfsp = LINVFS_GET_VFS(sb);
757 setmode = (type == GRPQUOTA) ? Q_XSETGQLIM : Q_XSETQLIM;
758 VFS_QUOTACTL(vfsp, setmode, id, (caddr_t)fdq, error);
764 struct super_block *sb,
769 struct vfs *vfsp = vfs_allocate();
770 struct xfs_mount_args *args = xfs_args_allocate(sb);
771 struct kstatfs statvfs;
774 vfsp->vfs_super = sb;
775 LINVFS_SET_VFS(sb, vfsp);
776 if (sb->s_flags & MS_RDONLY)
777 vfsp->vfs_flag |= VFS_RDONLY;
778 bhv_insert_all_vfsops(vfsp);
780 VFS_PARSEARGS(vfsp, (char *)data, args, 0, error);
782 bhv_remove_all_vfsops(vfsp, 1);
786 sb_min_blocksize(sb, BBSIZE);
787 sb->s_export_op = &linvfs_export_ops;
788 sb->s_qcop = &linvfs_qops;
789 sb->s_op = &linvfs_sops;
791 VFS_MOUNT(vfsp, args, NULL, error);
793 bhv_remove_all_vfsops(vfsp, 1);
797 VFS_STATVFS(vfsp, &statvfs, NULL, error);
802 sb->s_magic = statvfs.f_type;
803 sb->s_blocksize = statvfs.f_bsize;
804 sb->s_blocksize_bits = ffs(statvfs.f_bsize) - 1;
805 sb->s_maxbytes = xfs_max_file_offset(sb->s_blocksize_bits);
806 set_posix_acl_flag(sb);
808 VFS_ROOT(vfsp, &rootvp, error);
812 sb->s_root = d_alloc_root(LINVFS_GET_IP(rootvp));
815 if (is_bad_inode(sb->s_root->d_inode))
817 if (linvfs_start_syncd(vfsp))
819 vn_trace_exit(rootvp, __FUNCTION__, (inst_t *)__return_address);
821 kmem_free(args, sizeof(*args));
833 VFS_UNMOUNT(vfsp, 0, NULL, error);
836 vfs_deallocate(vfsp);
837 kmem_free(args, sizeof(*args));
841 STATIC struct super_block *
843 struct file_system_type *fs_type,
845 const char *dev_name,
848 return get_sb_bdev(fs_type, flags, dev_name, data, linvfs_fill_super);
852 STATIC struct export_operations linvfs_export_ops = {
853 .get_parent = linvfs_get_parent,
854 .get_dentry = linvfs_get_dentry,
857 STATIC struct super_operations linvfs_sops = {
858 .alloc_inode = linvfs_alloc_inode,
859 .destroy_inode = linvfs_destroy_inode,
860 .write_inode = linvfs_write_inode,
861 .clear_inode = linvfs_clear_inode,
862 .put_super = linvfs_put_super,
863 .write_super = linvfs_write_super,
864 .sync_fs = linvfs_sync_super,
865 .write_super_lockfs = linvfs_freeze_fs,
866 .unlockfs = linvfs_unfreeze_fs,
867 .statfs = linvfs_statfs,
868 .remount_fs = linvfs_remount,
869 .show_options = linvfs_show_options,
872 STATIC struct quotactl_ops linvfs_qops = {
873 .get_xstate = linvfs_getxstate,
874 .set_xstate = linvfs_setxstate,
875 .get_xquota = linvfs_getxquota,
876 .set_xquota = linvfs_setxquota,
879 STATIC struct file_system_type xfs_fs_type = {
880 .owner = THIS_MODULE,
882 .get_sb = linvfs_get_sb,
883 .kill_sb = kill_block_super,
884 .fs_flags = FS_REQUIRES_DEV,
893 static char message[] __initdata = KERN_INFO \
894 XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled\n";
899 xfs_physmem = si.totalram;
903 error = init_inodecache();
905 goto undo_inodecache;
907 error = pagebuf_init();
915 error = register_filesystem(&xfs_fs_type);
924 destroy_inodecache();
933 unregister_filesystem(&xfs_fs_type);
936 destroy_inodecache();
940 module_init(init_xfs_fs);
941 module_exit(exit_xfs_fs);
943 MODULE_AUTHOR("Silicon Graphics, Inc.");
944 MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
945 MODULE_LICENSE("GPL");