ee07ddec60ba9286359737c598c6c2815dfd9917
[linux-flexiantxendom0-3.2.10.git] / fs / xfs / linux / xfs_super.c
1 /*
2  * Copyright (c) 2000-2003 Silicon Graphics, Inc.  All Rights Reserved.
3  *
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.
7  *
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.
11  *
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.
18  *
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.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 #include "xfs.h"
34
35 #include "xfs_inum.h"
36 #include "xfs_log.h"
37 #include "xfs_clnt.h"
38 #include "xfs_trans.h"
39 #include "xfs_sb.h"
40 #include "xfs_dir.h"
41 #include "xfs_dir2.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"
56 #include "xfs_bmap.h"
57 #include "xfs_bit.h"
58 #include "xfs_rtalloc.h"
59 #include "xfs_error.h"
60 #include "xfs_itable.h"
61 #include "xfs_rw.h"
62 #include "xfs_acl.h"
63 #include "xfs_cap.h"
64 #include "xfs_mac.h"
65 #include "xfs_attr.h"
66 #include "xfs_buf_item.h"
67 #include "xfs_utils.h"
68 #include "xfs_version.h"
69
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>
75
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;
80
81 STATIC struct xfs_mount_args *
82 xfs_args_allocate(
83         struct super_block      *sb)
84 {
85         struct xfs_mount_args   *args;
86
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);
90
91         /* Copy the already-parsed mount(2) flags we're interested in */
92         if (sb->s_flags & MS_NOATIME)
93                 args->flags |= XFSMNT_NOATIME;
94
95         /* Default to 32 bit inodes on Linux all the time */
96         args->flags |= XFSMNT_32BITINODES;
97
98         return args;
99 }
100
101 __uint64_t
102 xfs_max_file_offset(
103         unsigned int            blockshift)
104 {
105         unsigned int            pagefactor = 1;
106         unsigned int            bitshift = BITS_PER_LONG - 1;
107
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.
120          */
121
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;
127 # else
128         pagefactor = PAGE_CACHE_SIZE >> (PAGE_CACHE_SHIFT - blockshift);
129 # endif
130 #endif
131
132         return (((__uint64_t)pagefactor) << bitshift) - 1;
133 }
134
135 STATIC __inline__ void
136 xfs_set_inodeops(
137         struct inode            *inode)
138 {
139         vnode_t                 *vp = LINVFS_GET_VP(inode);
140
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;
152                 if (inode->i_blocks)
153                         inode->i_mapping->a_ops = &linvfs_aops;
154         } else {
155                 inode->i_op = &linvfs_file_inode_operations;
156                 init_special_inode(inode, inode->i_mode,
157                                         inode->i_rdev);
158         }
159 }
160
161 STATIC __inline__ void
162 xfs_revalidate_inode(
163         xfs_mount_t             *mp,
164         vnode_t                 *vp,
165         xfs_inode_t             *ip)
166 {
167         struct inode            *inode = LINVFS_GET_IP(vp);
168
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) {
174                 inode->i_rdev = 0;
175         } else {
176                 xfs_dev_t dev = ip->i_df.if_u2.if_rdev;
177                 inode->i_rdev = MKDEV(sysv_major(dev) & 0x1ff, sysv_minor(dev));
178         }
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);
182         inode->i_blocks =
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;
192         else
193                 inode->i_flags &= ~S_IMMUTABLE;
194         if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
195                 inode->i_flags |= S_APPEND;
196         else
197                 inode->i_flags &= ~S_APPEND;
198         if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
199                 inode->i_flags |= S_SYNC;
200         else
201                 inode->i_flags &= ~S_SYNC;
202         if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
203                 inode->i_flags |= S_NOATIME;
204         else
205                 inode->i_flags &= ~S_NOATIME;
206         vp->v_flag &= ~VMODIFIED;
207 }
208
209 void
210 xfs_initialize_vnode(
211         bhv_desc_t              *bdp,
212         vnode_t                 *vp,
213         bhv_desc_t              *inode_bhv,
214         int                     unlock)
215 {
216         xfs_inode_t             *ip = XFS_BHVTOI(inode_bhv);
217         struct inode            *inode = LINVFS_GET_IP(vp);
218
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);
223         }
224
225         vp->v_type = IFTOVT(ip->i_d.di_mode);
226
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.
229          */
230         if (vp->v_type == VNON)
231                 return;
232
233         xfs_revalidate_inode(XFS_BHVTOM(bdp), vp, ip);
234
235         /* For new inodes we need to set the ops vectors,
236          * and unlock the inode.
237          */
238         if (unlock && (inode->i_state & I_NEW)) {
239                 xfs_set_inodeops(inode);
240                 unlock_new_inode(inode);
241         }
242 }
243
244 struct inode *
245 xfs_get_inode(
246         bhv_desc_t      *bdp,
247         xfs_ino_t       ino,
248         int             flags)
249 {
250         struct vfs      *vfsp = bhvtovfs(bdp);
251
252         if (flags & IGET_NOALLOC)
253                 return ilookup(vfsp->vfs_super, ino);
254         return iget_locked(vfsp->vfs_super, ino);
255 }
256
257 void
258 xfs_flush_inode(
259         xfs_inode_t     *ip)
260 {
261         struct inode    *inode = LINVFS_GET_IP(XFS_ITOV(ip));
262
263         filemap_fdatawrite(inode->i_mapping);
264 }
265
266 void
267 xfs_flush_device(
268         xfs_inode_t     *ip)
269 {
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);
272 }
273
274 int
275 xfs_blkdev_get(
276         xfs_mount_t             *mp,
277         const char              *name,
278         struct block_device     **bdevp)
279 {
280         int                     error = 0;
281
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);
286         }
287
288         return -error;
289 }
290
291 void
292 xfs_blkdev_put(
293         struct block_device     *bdev)
294 {
295         if (bdev)
296                 close_bdev_excl(bdev, BDEV_FS);
297 }
298
299 void
300 xfs_flush_buftarg(
301         xfs_buftarg_t           *btp)
302 {
303         pagebuf_delwri_flush(btp, PBDF_WAIT, NULL);
304 }
305
306 void
307 xfs_free_buftarg(
308         xfs_buftarg_t           *btp)
309 {
310         xfs_flush_buftarg(btp);
311         kmem_free(btp, sizeof(*btp));
312 }
313
314 int
315 xfs_readonly_buftarg(
316         xfs_buftarg_t           *btp)
317 {
318         return bdev_read_only(btp->pbr_bdev);
319 }
320
321 void
322 xfs_relse_buftarg(
323         xfs_buftarg_t           *btp)
324 {
325         invalidate_bdev(btp->pbr_bdev, 1);
326         truncate_inode_pages(btp->pbr_mapping, 0LL);
327 }
328
329 unsigned int
330 xfs_getsize_buftarg(
331         xfs_buftarg_t           *btp)
332 {
333         return block_size(btp->pbr_bdev);
334 }
335
336 void
337 xfs_setsize_buftarg(
338         xfs_buftarg_t           *btp,
339         unsigned int            blocksize,
340         unsigned int            sectorsize)
341 {
342         btp->pbr_bsize = blocksize;
343         btp->pbr_sshift = ffs(sectorsize) - 1;
344         btp->pbr_smask = sectorsize - 1;
345
346         if (set_blocksize(btp->pbr_bdev, sectorsize)) {
347                 printk(KERN_WARNING
348                         "XFS: Cannot set_blocksize to %u on device %s\n",
349                         sectorsize, XFS_BUFTARG_NAME(btp));
350         }
351 }
352
353 xfs_buftarg_t *
354 xfs_alloc_buftarg(
355         struct block_device     *bdev)
356 {
357         xfs_buftarg_t           *btp;
358
359         btp = kmem_zalloc(sizeof(*btp), KM_SLEEP);
360
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));
365
366         return btp;
367 }
368
369 STATIC struct inode *
370 linvfs_alloc_inode(
371         struct super_block      *sb)
372 {
373         vnode_t                 *vp;
374
375         vp = (vnode_t *)kmem_cache_alloc(linvfs_inode_cachep, 
376                 kmem_flags_convert(KM_SLEEP));
377         if (!vp)
378                 return NULL;
379         return LINVFS_GET_IP(vp);
380 }
381
382 STATIC void
383 linvfs_destroy_inode(
384         struct inode            *inode)
385 {
386         kmem_cache_free(linvfs_inode_cachep, LINVFS_GET_VP(inode));
387 }
388
389 STATIC void
390 init_once(
391         void                    *data,
392         kmem_cache_t            *cachep,
393         unsigned long           flags)
394 {
395         vnode_t                 *vp = (vnode_t *)data;
396
397         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
398             SLAB_CTOR_CONSTRUCTOR)
399                 inode_init_once(LINVFS_GET_IP(vp));
400 }
401
402 STATIC int
403 init_inodecache( void )
404 {
405         linvfs_inode_cachep = kmem_cache_create("linvfs_icache",
406                                 sizeof(vnode_t), 0,
407                                 SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
408                                 init_once, NULL);
409
410         if (linvfs_inode_cachep == NULL)
411                 return -ENOMEM;
412         return 0;
413 }
414
415 STATIC void
416 destroy_inodecache( void )
417 {
418         if (kmem_cache_destroy(linvfs_inode_cachep))
419                 printk(KERN_WARNING "%s: cache still in use!\n", __FUNCTION__);
420 }
421
422 /*
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. 
427  */
428 STATIC void
429 linvfs_write_inode(
430         struct inode            *inode,
431         int                     sync)
432 {
433         vnode_t                 *vp = LINVFS_GET_VP(inode);
434         int                     error, flags = FLUSH_INODE;
435
436         if (vp) {
437                 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
438                 if (sync)
439                         flags |= FLUSH_SYNC;
440                 VOP_IFLUSH(vp, flags, error);
441         }
442 }
443
444 STATIC void
445 linvfs_clear_inode(
446         struct inode            *inode)
447 {
448         vnode_t                 *vp = LINVFS_GET_VP(inode);
449
450         if (vp) {
451                 vn_rele(vp);
452                 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
453                 /*
454                  * Do all our cleanup, and remove this vnode.
455                  */
456                 vn_remove(vp);
457         }
458 }
459
460
461 #define SYNCD_FLAGS     (SYNC_FSDATA|SYNC_BDFLUSH|SYNC_ATTR)
462
463 STATIC int
464 syncd(void *arg)
465 {
466         vfs_t                   *vfsp = (vfs_t *) arg;
467         int                     error;
468
469         daemonize("xfssyncd");
470
471         vfsp->vfs_sync_task = current;
472         wmb();
473         wake_up(&vfsp->vfs_wait_sync_task);
474
475         for (;;) {
476                 set_current_state(TASK_INTERRUPTIBLE);
477                 schedule_timeout(xfs_syncd_interval);
478                 /* swsusp */
479                 if (current->flags & PF_FREEZE)
480                         refrigerator(PF_IOTHREAD);
481                 if (vfsp->vfs_flag & VFS_UMOUNT)
482                         break;
483                 if (vfsp->vfs_flag & VFS_RDONLY)
484                         continue;
485                 VFS_SYNC(vfsp, SYNCD_FLAGS, NULL, error);
486         }
487
488         vfsp->vfs_sync_task = NULL;
489         wmb();
490         wake_up(&vfsp->vfs_wait_sync_task);
491
492         return 0;
493 }
494
495 STATIC int
496 linvfs_start_syncd(vfs_t *vfsp)
497 {
498         int pid;
499
500         pid = kernel_thread(syncd, (void *) vfsp,
501                         CLONE_VM | CLONE_FS | CLONE_FILES);
502         if (pid < 0)
503                 return pid;
504         wait_event(vfsp->vfs_wait_sync_task, vfsp->vfs_sync_task);
505         return 0;
506 }
507
508 STATIC void
509 linvfs_stop_syncd(vfs_t *vfsp)
510 {
511         vfsp->vfs_flag |= VFS_UMOUNT;
512         wmb();
513
514         wake_up_process(vfsp->vfs_sync_task);
515         wait_event(vfsp->vfs_wait_sync_task, !vfsp->vfs_sync_task);
516 }
517
518 STATIC void
519 linvfs_put_super(
520         struct super_block      *sb)
521 {
522         vfs_t                   *vfsp = LINVFS_GET_VFS(sb);
523         int                     error;
524
525         linvfs_stop_syncd(vfsp);
526         VFS_SYNC(vfsp, SYNC_ATTR|SYNC_DELWRI, NULL, error);
527         if (!error)
528                 VFS_UNMOUNT(vfsp, 0, NULL, error);
529         if (error) {
530                 printk("XFS unmount got error %d\n", error);
531                 printk("%s: vfsp/0x%p left dangling!\n", __FUNCTION__, vfsp);
532                 return;
533         }
534
535         vfs_deallocate(vfsp);
536 }
537
538 STATIC void
539 linvfs_write_super(
540         struct super_block      *sb)
541 {
542         vfs_t                   *vfsp = LINVFS_GET_VFS(sb);
543         int                     error;
544
545         if (sb->s_flags & MS_RDONLY) {
546                 sb->s_dirt = 0; /* paranoia */
547                 return;
548         }
549         /* Push the log and superblock a little */
550         VFS_SYNC(vfsp, SYNC_FSDATA, NULL, error);
551         sb->s_dirt = 0;
552 }
553
554 STATIC int
555 linvfs_sync_super(
556         struct super_block      *sb,
557         int                     wait)
558 {
559         vfs_t           *vfsp = LINVFS_GET_VFS(sb);
560         int             error;
561         int             flags = SYNC_FSDATA;
562
563         if (wait)
564                 flags |= SYNC_WAIT;
565
566         VFS_SYNC(vfsp, flags, NULL, error);
567         sb->s_dirt = 0;
568
569         return -error;
570 }
571
572 STATIC int
573 linvfs_statfs(
574         struct super_block      *sb,
575         struct kstatfs          *statp)
576 {
577         vfs_t                   *vfsp = LINVFS_GET_VFS(sb);
578         int                     error;
579
580         VFS_STATVFS(vfsp, statp, NULL, error);
581         return -error;
582 }
583
584 STATIC int
585 linvfs_remount(
586         struct super_block      *sb,
587         int                     *flags,
588         char                    *options)
589 {
590         vfs_t                   *vfsp = LINVFS_GET_VFS(sb);
591         struct xfs_mount_args   *args = xfs_args_allocate(sb);
592         int                     error;
593
594         VFS_PARSEARGS(vfsp, options, args, 1, error);
595         if (!error)
596                 VFS_MNTUPDATE(vfsp, flags, args, error);
597         kmem_free(args, sizeof(*args));
598         return -error;
599 }
600
601 STATIC void
602 linvfs_freeze_fs(
603         struct super_block      *sb)
604 {
605         vfs_t                   *vfsp = LINVFS_GET_VFS(sb);
606         vnode_t                 *vp;
607         int                     error;
608
609         if (sb->s_flags & MS_RDONLY)
610                 return;
611         VFS_ROOT(vfsp, &vp, error);
612         VOP_IOCTL(vp, LINVFS_GET_IP(vp), NULL, 0, XFS_IOC_FREEZE, 0, error);
613         VN_RELE(vp);
614 }
615
616 STATIC void
617 linvfs_unfreeze_fs(
618         struct super_block      *sb)
619 {
620         vfs_t                   *vfsp = LINVFS_GET_VFS(sb);
621         vnode_t                 *vp;
622         int                     error;
623
624         VFS_ROOT(vfsp, &vp, error);
625         VOP_IOCTL(vp, LINVFS_GET_IP(vp), NULL, 0, XFS_IOC_THAW, 0, error);
626         VN_RELE(vp);
627 }
628
629 STATIC struct dentry *
630 linvfs_get_parent(
631         struct dentry           *child)
632 {
633         int                     error;
634         vnode_t                 *vp, *cvp;
635         struct dentry           *parent;
636         struct inode            *ip = NULL;
637         struct dentry           dotdot;
638
639         dotdot.d_name.name = "..";
640         dotdot.d_name.len = 2;
641         dotdot.d_inode = 0;
642
643         cvp = NULL;
644         vp = LINVFS_GET_VP(child->d_inode);
645         VOP_LOOKUP(vp, &dotdot, &cvp, 0, NULL, NULL, error);
646
647         if (!error) {
648                 ASSERT(cvp);
649                 ip = LINVFS_GET_IP(cvp);
650                 if (!ip) {
651                         VN_RELE(cvp);
652                         return ERR_PTR(-EACCES);
653                 }
654         }
655         if (error)
656                 return ERR_PTR(-error);
657         parent = d_alloc_anon(ip);
658         if (!parent) {
659                 VN_RELE(cvp);
660                 parent = ERR_PTR(-ENOMEM);
661         }
662         return parent;
663 }
664
665 STATIC struct dentry *
666 linvfs_get_dentry(
667         struct super_block      *sb,
668         void                    *data)
669 {
670         vnode_t                 *vp;
671         struct inode            *inode;
672         struct dentry           *result;
673         xfs_fid2_t              xfid;
674         vfs_t                   *vfsp = LINVFS_GET_VFS(sb);
675         int                     error;
676
677         xfid.fid_len = sizeof(xfs_fid2_t) - sizeof(xfid.fid_len);
678         xfid.fid_pad = 0;
679         xfid.fid_gen = ((__u32 *)data)[1];
680         xfid.fid_ino = ((__u32 *)data)[0];
681
682         VFS_VGET(vfsp, &vp, (fid_t *)&xfid, error);
683         if (error || vp == NULL)
684                 return ERR_PTR(-ESTALE) ;
685
686         inode = LINVFS_GET_IP(vp);
687         result = d_alloc_anon(inode);
688         if (!result) {
689                 iput(inode);
690                 return ERR_PTR(-ENOMEM);
691         }
692         return result;
693 }
694
695 STATIC int
696 linvfs_show_options(
697         struct seq_file         *m,
698         struct vfsmount         *mnt)
699 {
700         struct vfs              *vfsp = LINVFS_GET_VFS(mnt->mnt_sb);
701         int                     error;
702
703         VFS_SHOWARGS(vfsp, m, error);
704         return error;
705 }
706
707 STATIC int
708 linvfs_getxstate(
709         struct super_block      *sb,
710         struct fs_quota_stat    *fqs)
711 {
712         struct vfs              *vfsp = LINVFS_GET_VFS(sb);
713         int                     error;
714
715         VFS_QUOTACTL(vfsp, Q_XGETQSTAT, 0, (caddr_t)fqs, error);
716         return -error;
717 }
718
719 STATIC int
720 linvfs_setxstate(
721         struct super_block      *sb,
722         unsigned int            flags,
723         int                     op)
724 {
725         struct vfs              *vfsp = LINVFS_GET_VFS(sb);
726         int                     error;
727
728         VFS_QUOTACTL(vfsp, op, 0, (caddr_t)&flags, error);
729         return -error;
730 }
731
732 STATIC int
733 linvfs_getxquota(
734         struct super_block      *sb,
735         int                     type,
736         qid_t                   id,
737         struct fs_disk_quota    *fdq)
738 {
739         struct vfs              *vfsp = LINVFS_GET_VFS(sb);
740         int                     error, getmode;
741
742         getmode = (type == GRPQUOTA) ? Q_XGETGQUOTA : Q_XGETQUOTA;
743         VFS_QUOTACTL(vfsp, getmode, id, (caddr_t)fdq, error);
744         return -error;
745 }
746
747 STATIC int
748 linvfs_setxquota(
749         struct super_block      *sb,
750         int                     type,
751         qid_t                   id,
752         struct fs_disk_quota    *fdq)
753 {
754         struct vfs              *vfsp = LINVFS_GET_VFS(sb);
755         int                     error, setmode;
756
757         setmode = (type == GRPQUOTA) ? Q_XSETGQLIM : Q_XSETQLIM;
758         VFS_QUOTACTL(vfsp, setmode, id, (caddr_t)fdq, error);
759         return -error;
760 }
761
762 STATIC int
763 linvfs_fill_super(
764         struct super_block      *sb,
765         void                    *data,
766         int                     silent)
767 {
768         vnode_t                 *rootvp;
769         struct vfs              *vfsp = vfs_allocate();
770         struct xfs_mount_args   *args = xfs_args_allocate(sb);
771         struct kstatfs          statvfs;
772         int                     error;
773
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);
779
780         VFS_PARSEARGS(vfsp, (char *)data, args, 0, error);
781         if (error) {
782                 bhv_remove_all_vfsops(vfsp, 1);
783                 goto fail_vfsop;
784         }
785
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;
790
791         VFS_MOUNT(vfsp, args, NULL, error);
792         if (error) {
793                 bhv_remove_all_vfsops(vfsp, 1);
794                 goto fail_vfsop;
795         }
796
797         VFS_STATVFS(vfsp, &statvfs, NULL, error);
798         if (error)
799                 goto fail_unmount;
800
801         sb->s_dirt = 1;
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);
807
808         VFS_ROOT(vfsp, &rootvp, error);
809         if (error)
810                 goto fail_unmount;
811
812         sb->s_root = d_alloc_root(LINVFS_GET_IP(rootvp));
813         if (!sb->s_root)
814                 goto fail_vnrele;
815         if (is_bad_inode(sb->s_root->d_inode))
816                 goto fail_vnrele;
817         if (linvfs_start_syncd(vfsp))
818                 goto fail_vnrele;
819         vn_trace_exit(rootvp, __FUNCTION__, (inst_t *)__return_address);
820
821         kmem_free(args, sizeof(*args));
822         return 0;
823
824 fail_vnrele:
825         if (sb->s_root) {
826                 dput(sb->s_root);
827                 sb->s_root = NULL;
828         } else {
829                 VN_RELE(rootvp);
830         }
831
832 fail_unmount:
833         VFS_UNMOUNT(vfsp, 0, NULL, error);
834
835 fail_vfsop:
836         vfs_deallocate(vfsp);
837         kmem_free(args, sizeof(*args));
838         return -error;
839 }
840
841 STATIC struct super_block *
842 linvfs_get_sb(
843         struct file_system_type *fs_type,
844         int                     flags,
845         const char              *dev_name,
846         void                    *data)
847 {
848         return get_sb_bdev(fs_type, flags, dev_name, data, linvfs_fill_super);
849 }
850
851
852 STATIC struct export_operations linvfs_export_ops = {
853         .get_parent             = linvfs_get_parent,
854         .get_dentry             = linvfs_get_dentry,
855 };
856
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,
870 };
871
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,
877 };
878
879 STATIC struct file_system_type xfs_fs_type = {
880         .owner                  = THIS_MODULE,
881         .name                   = "xfs",
882         .get_sb                 = linvfs_get_sb,
883         .kill_sb                = kill_block_super,
884         .fs_flags               = FS_REQUIRES_DEV,
885 };
886
887
888 STATIC int __init
889 init_xfs_fs( void )
890 {
891         int                     error;
892         struct sysinfo          si;
893         static char             message[] __initdata = KERN_INFO \
894                 XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled\n";
895
896         printk(message);
897
898         si_meminfo(&si);
899         xfs_physmem = si.totalram;
900
901         ktrace_init(64);
902
903         error = init_inodecache();
904         if (error < 0)
905                 goto undo_inodecache;
906
907         error = pagebuf_init();
908         if (error < 0)
909                 goto undo_pagebuf;
910
911         vn_init();
912         xfs_init();
913         uuid_init();
914
915         error = register_filesystem(&xfs_fs_type);
916         if (error)
917                 goto undo_register;
918         return 0;
919
920 undo_register:
921         pagebuf_terminate();
922
923 undo_pagebuf:
924         destroy_inodecache();
925
926 undo_inodecache:
927         return error;
928 }
929
930 STATIC void __exit
931 exit_xfs_fs( void )
932 {
933         unregister_filesystem(&xfs_fs_type);
934         xfs_cleanup();
935         pagebuf_terminate();
936         destroy_inodecache();
937         ktrace_uninit();
938 }
939
940 module_init(init_xfs_fs);
941 module_exit(exit_xfs_fs);
942
943 MODULE_AUTHOR("Silicon Graphics, Inc.");
944 MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
945 MODULE_LICENSE("GPL");