kernel.org linux-2.6.9
[linux-2.6.git] / fs / xfs / linux-2.6 / xfs_super.c
1 /*
2  * Copyright (c) 2000-2004 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 #include "xfs_ioctl32.h"
70
71 #include <linux/namei.h>
72 #include <linux/init.h>
73 #include <linux/mount.h>
74 #include <linux/suspend.h>
75 #include <linux/writeback.h>
76
77 STATIC struct quotactl_ops linvfs_qops;
78 STATIC struct super_operations linvfs_sops;
79 STATIC struct export_operations linvfs_export_ops;
80 STATIC kmem_zone_t *linvfs_inode_zone;
81 STATIC kmem_shaker_t xfs_inode_shaker;
82
83 STATIC struct xfs_mount_args *
84 xfs_args_allocate(
85         struct super_block      *sb)
86 {
87         struct xfs_mount_args   *args;
88
89         args = kmem_zalloc(sizeof(struct xfs_mount_args), KM_SLEEP);
90         args->logbufs = args->logbufsize = -1;
91         strncpy(args->fsname, sb->s_id, MAXNAMELEN);
92
93         /* Copy the already-parsed mount(2) flags we're interested in */
94         if (sb->s_flags & MS_NOATIME)
95                 args->flags |= XFSMNT_NOATIME;
96
97         /* Default to 32 bit inodes on Linux all the time */
98         args->flags |= XFSMNT_32BITINODES;
99
100         return args;
101 }
102
103 __uint64_t
104 xfs_max_file_offset(
105         unsigned int            blockshift)
106 {
107         unsigned int            pagefactor = 1;
108         unsigned int            bitshift = BITS_PER_LONG - 1;
109
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.
122          */
123
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;
129 # else
130         pagefactor = PAGE_CACHE_SIZE >> (PAGE_CACHE_SHIFT - blockshift);
131 # endif
132 #endif
133
134         return (((__uint64_t)pagefactor) << bitshift) - 1;
135 }
136
137 STATIC __inline__ void
138 xfs_set_inodeops(
139         struct inode            *inode)
140 {
141         vnode_t                 *vp = LINVFS_GET_VP(inode);
142
143         if (vp->v_type == VNON) {
144                 vn_mark_bad(vp);
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;
154                 if (inode->i_blocks)
155                         inode->i_mapping->a_ops = &linvfs_aops;
156         } else {
157                 inode->i_op = &linvfs_file_inode_operations;
158                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
159         }
160 }
161
162 STATIC __inline__ void
163 xfs_revalidate_inode(
164         xfs_mount_t             *mp,
165         vnode_t                 *vp,
166         xfs_inode_t             *ip)
167 {
168         struct inode            *inode = LINVFS_GET_IP(vp);
169
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         if (((1 << vp->v_type) & ((1<<VBLK) | (1<<VCHR))) == 0) {
175                 inode->i_rdev = 0;
176         } else {
177                 xfs_dev_t dev = ip->i_df.if_u2.if_rdev;
178                 inode->i_rdev = MKDEV(sysv_major(dev) & 0x1ff, sysv_minor(dev));
179         }
180         inode->i_blksize = PAGE_CACHE_SIZE;
181         inode->i_generation = ip->i_d.di_gen;
182         i_size_write(inode, ip->i_d.di_size);
183         inode->i_blocks =
184                 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
185         inode->i_atime.tv_sec   = ip->i_d.di_atime.t_sec;
186         inode->i_atime.tv_nsec  = ip->i_d.di_atime.t_nsec;
187         inode->i_mtime.tv_sec   = ip->i_d.di_mtime.t_sec;
188         inode->i_mtime.tv_nsec  = ip->i_d.di_mtime.t_nsec;
189         inode->i_ctime.tv_sec   = ip->i_d.di_ctime.t_sec;
190         inode->i_ctime.tv_nsec  = ip->i_d.di_ctime.t_nsec;
191         if (ip->i_d.di_flags & XFS_DIFLAG_IMMUTABLE)
192                 inode->i_flags |= S_IMMUTABLE;
193         else
194                 inode->i_flags &= ~S_IMMUTABLE;
195         if (ip->i_d.di_flags & XFS_DIFLAG_APPEND)
196                 inode->i_flags |= S_APPEND;
197         else
198                 inode->i_flags &= ~S_APPEND;
199         if (ip->i_d.di_flags & XFS_DIFLAG_SYNC)
200                 inode->i_flags |= S_SYNC;
201         else
202                 inode->i_flags &= ~S_SYNC;
203         if (ip->i_d.di_flags & XFS_DIFLAG_NOATIME)
204                 inode->i_flags |= S_NOATIME;
205         else
206                 inode->i_flags &= ~S_NOATIME;
207         vp->v_flag &= ~VMODIFIED;
208 }
209
210 void
211 xfs_initialize_vnode(
212         bhv_desc_t              *bdp,
213         vnode_t                 *vp,
214         bhv_desc_t              *inode_bhv,
215         int                     unlock)
216 {
217         xfs_inode_t             *ip = XFS_BHVTOI(inode_bhv);
218         struct inode            *inode = LINVFS_GET_IP(vp);
219
220         if (!inode_bhv->bd_vobj) {
221                 vp->v_vfsp = bhvtovfs(bdp);
222                 bhv_desc_init(inode_bhv, ip, vp, &xfs_vnodeops);
223                 bhv_insert(VN_BHV_HEAD(vp), inode_bhv);
224         }
225
226         /*
227          * We need to set the ops vectors, and unlock the inode, but if
228          * we have been called during the new inode create process, it is
229          * too early to fill in the Linux inode.  We will get called a
230          * second time once the inode is properly set up, and then we can
231          * finish our work.
232          */
233         if (ip->i_d.di_mode != 0 && unlock && (inode->i_state & I_NEW)) {
234                 vp->v_type = IFTOVT(ip->i_d.di_mode);
235                 xfs_revalidate_inode(XFS_BHVTOM(bdp), vp, ip);
236                 xfs_set_inodeops(inode);
237                 unlock_new_inode(inode);
238         }
239 }
240
241 int
242 xfs_blkdev_get(
243         xfs_mount_t             *mp,
244         const char              *name,
245         struct block_device     **bdevp)
246 {
247         int                     error = 0;
248
249         *bdevp = open_bdev_excl(name, 0, mp);
250         if (IS_ERR(*bdevp)) {
251                 error = PTR_ERR(*bdevp);
252                 printk("XFS: Invalid device [%s], error=%d\n", name, error);
253         }
254
255         return -error;
256 }
257
258 void
259 xfs_blkdev_put(
260         struct block_device     *bdev)
261 {
262         if (bdev)
263                 close_bdev_excl(bdev);
264 }
265
266
267 STATIC struct inode *
268 linvfs_alloc_inode(
269         struct super_block      *sb)
270 {
271         vnode_t                 *vp;
272
273         vp = (vnode_t *)kmem_cache_alloc(linvfs_inode_zone, 
274                 kmem_flags_convert(KM_SLEEP));
275         if (!vp)
276                 return NULL;
277         return LINVFS_GET_IP(vp);
278 }
279
280 STATIC void
281 linvfs_destroy_inode(
282         struct inode            *inode)
283 {
284         kmem_cache_free(linvfs_inode_zone, LINVFS_GET_VP(inode));
285 }
286
287 int
288 xfs_inode_shake(
289         int             priority,
290         unsigned int    gfp_mask)
291 {
292         int             pages;
293
294         pages = kmem_zone_shrink(linvfs_inode_zone);
295         pages += kmem_zone_shrink(xfs_inode_zone);
296         return pages;
297 }
298
299 STATIC void
300 init_once(
301         void                    *data,
302         kmem_cache_t            *cachep,
303         unsigned long           flags)
304 {
305         vnode_t                 *vp = (vnode_t *)data;
306
307         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
308             SLAB_CTOR_CONSTRUCTOR)
309                 inode_init_once(LINVFS_GET_IP(vp));
310 }
311
312 STATIC int
313 init_inodecache( void )
314 {
315         linvfs_inode_zone = kmem_cache_create("linvfs_icache",
316                                 sizeof(vnode_t), 0, SLAB_RECLAIM_ACCOUNT,
317                                 init_once, NULL);
318         if (linvfs_inode_zone == NULL)
319                 return -ENOMEM;
320         return 0;
321 }
322
323 STATIC void
324 destroy_inodecache( void )
325 {
326         if (kmem_cache_destroy(linvfs_inode_zone))
327                 printk(KERN_WARNING "%s: cache still in use!\n", __FUNCTION__);
328 }
329
330 /*
331  * Attempt to flush the inode, this will actually fail
332  * if the inode is pinned, but we dirty the inode again
333  * at the point when it is unpinned after a log write,
334  * since this is when the inode itself becomes flushable. 
335  */
336 STATIC int
337 linvfs_write_inode(
338         struct inode            *inode,
339         int                     sync)
340 {
341         vnode_t                 *vp = LINVFS_GET_VP(inode);
342         int                     error = 0, flags = FLUSH_INODE;
343
344         if (vp) {
345                 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
346                 if (sync)
347                         flags |= FLUSH_SYNC;
348                 VOP_IFLUSH(vp, flags, error);
349         }
350
351         return -error;
352 }
353
354 STATIC void
355 linvfs_clear_inode(
356         struct inode            *inode)
357 {
358         vnode_t                 *vp = LINVFS_GET_VP(inode);
359
360         if (vp) {
361                 vn_rele(vp);
362                 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
363                 /*
364                  * Do all our cleanup, and remove this vnode.
365                  */
366                 vn_remove(vp);
367         }
368 }
369
370
371 /*
372  * Enqueue a work item to be picked up by the vfs xfssyncd thread.
373  * Doing this has two advantages:
374  * - It saves on stack space, which is tight in certain situations
375  * - It can be used (with care) as a mechanism to avoid deadlocks.
376  * Flushing while allocating in a full filesystem requires both.
377  */
378 STATIC void
379 xfs_syncd_queue_work(
380         struct vfs      *vfs,
381         void            *data,
382         void            (*syncer)(vfs_t *, void *))
383 {
384         vfs_sync_work_t *work;
385
386         work = kmem_alloc(sizeof(struct vfs_sync_work), KM_SLEEP);
387         INIT_LIST_HEAD(&work->w_list);
388         work->w_syncer = syncer;
389         work->w_data = data;
390         work->w_vfs = vfs;
391         spin_lock(&vfs->vfs_sync_lock);
392         list_add_tail(&work->w_list, &vfs->vfs_sync_list);
393         spin_unlock(&vfs->vfs_sync_lock);
394         wake_up_process(vfs->vfs_sync_task);
395 }
396
397 /*
398  * Flush delayed allocate data, attempting to free up reserved space
399  * from existing allocations.  At this point a new allocation attempt
400  * has failed with ENOSPC and we are in the process of scratching our
401  * heads, looking about for more room...
402  */
403 STATIC void
404 xfs_flush_inode_work(
405         vfs_t           *vfs,
406         void            *inode)
407 {
408         filemap_flush(((struct inode *)inode)->i_mapping);
409         iput((struct inode *)inode);
410 }
411
412 void
413 xfs_flush_inode(
414         xfs_inode_t     *ip)
415 {
416         struct inode    *inode = LINVFS_GET_IP(XFS_ITOV(ip));
417         struct vfs      *vfs = XFS_MTOVFS(ip->i_mount);
418
419         igrab(inode);
420         xfs_syncd_queue_work(vfs, inode, xfs_flush_inode_work);
421         delay(HZ/2);
422 }
423
424 /*
425  * This is the "bigger hammer" version of xfs_flush_inode_work...
426  * (IOW, "If at first you don't succeed, use a Bigger Hammer").
427  */
428 STATIC void
429 xfs_flush_device_work(
430         vfs_t           *vfs,
431         void            *inode)
432 {
433         sync_blockdev(vfs->vfs_super->s_bdev);
434         iput((struct inode *)inode);
435 }
436
437 void
438 xfs_flush_device(
439         xfs_inode_t     *ip)
440 {
441         struct inode    *inode = LINVFS_GET_IP(XFS_ITOV(ip));
442         struct vfs      *vfs = XFS_MTOVFS(ip->i_mount);
443
444         igrab(inode);
445         xfs_syncd_queue_work(vfs, inode, xfs_flush_device_work);
446         delay(HZ/2);
447         xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
448 }
449
450 #define SYNCD_FLAGS     (SYNC_FSDATA|SYNC_BDFLUSH|SYNC_ATTR)
451 STATIC void
452 vfs_sync_worker(
453         vfs_t           *vfsp,
454         void            *unused)
455 {
456         int             error;
457
458         if (!(vfsp->vfs_flag & VFS_RDONLY))
459                 VFS_SYNC(vfsp, SYNCD_FLAGS, NULL, error);
460         vfsp->vfs_sync_seq++;
461         wmb();
462         wake_up(&vfsp->vfs_wait_single_sync_task);
463 }
464
465 STATIC int
466 xfssyncd(
467         void                    *arg)
468 {
469         long                    timeleft;
470         vfs_t                   *vfsp = (vfs_t *) arg;
471         struct list_head        tmp;
472         struct vfs_sync_work    *work, *n;
473
474         daemonize("xfssyncd");
475
476         vfsp->vfs_sync_work.w_vfs = vfsp;
477         vfsp->vfs_sync_work.w_syncer = vfs_sync_worker;
478         vfsp->vfs_sync_task = current;
479         wmb();
480         wake_up(&vfsp->vfs_wait_sync_task);
481
482         INIT_LIST_HEAD(&tmp);
483         timeleft = (xfs_syncd_centisecs * HZ) / 100;
484         for (;;) {
485                 set_current_state(TASK_INTERRUPTIBLE);
486                 timeleft = schedule_timeout(timeleft);
487                 /* swsusp */
488                 if (current->flags & PF_FREEZE)
489                         refrigerator(PF_FREEZE);
490                 if (vfsp->vfs_flag & VFS_UMOUNT)
491                         break;
492
493                 spin_lock(&vfsp->vfs_sync_lock);
494                 /*
495                  * We can get woken by laptop mode, to do a sync -
496                  * that's the (only!) case where the list would be
497                  * empty with time remaining.
498                  */
499                 if (!timeleft || list_empty(&vfsp->vfs_sync_list)) {
500                         if (!timeleft)
501                                 timeleft = (xfs_syncd_centisecs * HZ) / 100;
502                         INIT_LIST_HEAD(&vfsp->vfs_sync_work.w_list);
503                         list_add_tail(&vfsp->vfs_sync_work.w_list,
504                                         &vfsp->vfs_sync_list);
505                 }
506                 list_for_each_entry_safe(work, n, &vfsp->vfs_sync_list, w_list)
507                         list_move(&work->w_list, &tmp);
508                 spin_unlock(&vfsp->vfs_sync_lock);
509
510                 list_for_each_entry_safe(work, n, &tmp, w_list) {
511                         (*work->w_syncer)(vfsp, work->w_data);
512                         list_del(&work->w_list);
513                         if (work == &vfsp->vfs_sync_work)
514                                 continue;
515                         kmem_free(work, sizeof(struct vfs_sync_work));
516                 }
517         }
518
519         vfsp->vfs_sync_task = NULL;
520         wmb();
521         wake_up(&vfsp->vfs_wait_sync_task);
522
523         return 0;
524 }
525
526 STATIC int
527 linvfs_start_syncd(
528         vfs_t                   *vfsp)
529 {
530         int                     pid;
531
532         pid = kernel_thread(xfssyncd, (void *) vfsp,
533                         CLONE_VM | CLONE_FS | CLONE_FILES);
534         if (pid < 0)
535                 return -pid;
536         wait_event(vfsp->vfs_wait_sync_task, vfsp->vfs_sync_task);
537         return 0;
538 }
539
540 STATIC void
541 linvfs_stop_syncd(
542         vfs_t                   *vfsp)
543 {
544         vfsp->vfs_flag |= VFS_UMOUNT;
545         wmb();
546
547         wake_up_process(vfsp->vfs_sync_task);
548         wait_event(vfsp->vfs_wait_sync_task, !vfsp->vfs_sync_task);
549 }
550
551 STATIC void
552 linvfs_put_super(
553         struct super_block      *sb)
554 {
555         vfs_t                   *vfsp = LINVFS_GET_VFS(sb);
556         int                     error;
557
558         linvfs_stop_syncd(vfsp);
559         VFS_SYNC(vfsp, SYNC_ATTR|SYNC_DELWRI, NULL, error);
560         if (!error)
561                 VFS_UNMOUNT(vfsp, 0, NULL, error);
562         if (error) {
563                 printk("XFS unmount got error %d\n", error);
564                 printk("%s: vfsp/0x%p left dangling!\n", __FUNCTION__, vfsp);
565                 return;
566         }
567
568         vfs_deallocate(vfsp);
569 }
570
571 STATIC void
572 linvfs_write_super(
573         struct super_block      *sb)
574 {
575         vfs_t                   *vfsp = LINVFS_GET_VFS(sb);
576         int                     error;
577
578         if (sb->s_flags & MS_RDONLY) {
579                 sb->s_dirt = 0; /* paranoia */
580                 return;
581         }
582         /* Push the log and superblock a little */
583         VFS_SYNC(vfsp, SYNC_FSDATA, NULL, error);
584         sb->s_dirt = 0;
585 }
586
587 STATIC int
588 linvfs_sync_super(
589         struct super_block      *sb,
590         int                     wait)
591 {
592         vfs_t           *vfsp = LINVFS_GET_VFS(sb);
593         int             error;
594         int             flags = SYNC_FSDATA;
595
596         if (wait)
597                 flags |= SYNC_WAIT;
598
599         VFS_SYNC(vfsp, flags, NULL, error);
600         sb->s_dirt = 0;
601
602         if (unlikely(laptop_mode)) {
603                 int     prev_sync_seq = vfsp->vfs_sync_seq;
604
605                 /*
606                  * The disk must be active because we're syncing.
607                  * We schedule xfssyncd now (now that the disk is
608                  * active) instead of later (when it might not be).
609                  */
610                 wake_up_process(vfsp->vfs_sync_task);
611                 /*
612                  * We have to wait for the sync iteration to complete.
613                  * If we don't, the disk activity caused by the sync
614                  * will come after the sync is completed, and that
615                  * triggers another sync from laptop mode.
616                  */
617                 wait_event(vfsp->vfs_wait_single_sync_task,
618                                 vfsp->vfs_sync_seq != prev_sync_seq);
619         }
620
621         return -error;
622 }
623
624 STATIC int
625 linvfs_statfs(
626         struct super_block      *sb,
627         struct kstatfs          *statp)
628 {
629         vfs_t                   *vfsp = LINVFS_GET_VFS(sb);
630         int                     error;
631
632         VFS_STATVFS(vfsp, statp, NULL, error);
633         return -error;
634 }
635
636 STATIC int
637 linvfs_remount(
638         struct super_block      *sb,
639         int                     *flags,
640         char                    *options)
641 {
642         vfs_t                   *vfsp = LINVFS_GET_VFS(sb);
643         struct xfs_mount_args   *args = xfs_args_allocate(sb);
644         int                     error;
645
646         VFS_PARSEARGS(vfsp, options, args, 1, error);
647         if (!error)
648                 VFS_MNTUPDATE(vfsp, flags, args, error);
649         kmem_free(args, sizeof(*args));
650         return -error;
651 }
652
653 STATIC void
654 linvfs_freeze_fs(
655         struct super_block      *sb)
656 {
657         VFS_FREEZE(LINVFS_GET_VFS(sb));
658 }
659
660 STATIC struct dentry *
661 linvfs_get_parent(
662         struct dentry           *child)
663 {
664         int                     error;
665         vnode_t                 *vp, *cvp;
666         struct dentry           *parent;
667         struct dentry           dotdot;
668
669         dotdot.d_name.name = "..";
670         dotdot.d_name.len = 2;
671         dotdot.d_inode = NULL;
672
673         cvp = NULL;
674         vp = LINVFS_GET_VP(child->d_inode);
675         VOP_LOOKUP(vp, &dotdot, &cvp, 0, NULL, NULL, error);
676         if (unlikely(error))
677                 return ERR_PTR(-error);
678
679         parent = d_alloc_anon(LINVFS_GET_IP(cvp));
680         if (unlikely(!parent)) {
681                 VN_RELE(cvp);
682                 return ERR_PTR(-ENOMEM);
683         }
684         return parent;
685 }
686
687 STATIC struct dentry *
688 linvfs_get_dentry(
689         struct super_block      *sb,
690         void                    *data)
691 {
692         vnode_t                 *vp;
693         struct inode            *inode;
694         struct dentry           *result;
695         xfs_fid2_t              xfid;
696         vfs_t                   *vfsp = LINVFS_GET_VFS(sb);
697         int                     error;
698
699         xfid.fid_len = sizeof(xfs_fid2_t) - sizeof(xfid.fid_len);
700         xfid.fid_pad = 0;
701         xfid.fid_gen = ((__u32 *)data)[1];
702         xfid.fid_ino = ((__u32 *)data)[0];
703
704         VFS_VGET(vfsp, &vp, (fid_t *)&xfid, error);
705         if (error || vp == NULL)
706                 return ERR_PTR(-ESTALE) ;
707
708         inode = LINVFS_GET_IP(vp);
709         result = d_alloc_anon(inode);
710         if (!result) {
711                 iput(inode);
712                 return ERR_PTR(-ENOMEM);
713         }
714         return result;
715 }
716
717 STATIC int
718 linvfs_show_options(
719         struct seq_file         *m,
720         struct vfsmount         *mnt)
721 {
722         struct vfs              *vfsp = LINVFS_GET_VFS(mnt->mnt_sb);
723         int                     error;
724
725         VFS_SHOWARGS(vfsp, m, error);
726         return error;
727 }
728
729 STATIC int
730 linvfs_getxstate(
731         struct super_block      *sb,
732         struct fs_quota_stat    *fqs)
733 {
734         struct vfs              *vfsp = LINVFS_GET_VFS(sb);
735         int                     error;
736
737         VFS_QUOTACTL(vfsp, Q_XGETQSTAT, 0, (caddr_t)fqs, error);
738         return -error;
739 }
740
741 STATIC int
742 linvfs_setxstate(
743         struct super_block      *sb,
744         unsigned int            flags,
745         int                     op)
746 {
747         struct vfs              *vfsp = LINVFS_GET_VFS(sb);
748         int                     error;
749
750         VFS_QUOTACTL(vfsp, op, 0, (caddr_t)&flags, error);
751         return -error;
752 }
753
754 STATIC int
755 linvfs_getxquota(
756         struct super_block      *sb,
757         int                     type,
758         qid_t                   id,
759         struct fs_disk_quota    *fdq)
760 {
761         struct vfs              *vfsp = LINVFS_GET_VFS(sb);
762         int                     error, getmode;
763
764         getmode = (type == GRPQUOTA) ? Q_XGETGQUOTA : Q_XGETQUOTA;
765         VFS_QUOTACTL(vfsp, getmode, id, (caddr_t)fdq, error);
766         return -error;
767 }
768
769 STATIC int
770 linvfs_setxquota(
771         struct super_block      *sb,
772         int                     type,
773         qid_t                   id,
774         struct fs_disk_quota    *fdq)
775 {
776         struct vfs              *vfsp = LINVFS_GET_VFS(sb);
777         int                     error, setmode;
778
779         setmode = (type == GRPQUOTA) ? Q_XSETGQLIM : Q_XSETQLIM;
780         VFS_QUOTACTL(vfsp, setmode, id, (caddr_t)fdq, error);
781         return -error;
782 }
783
784 STATIC int
785 linvfs_fill_super(
786         struct super_block      *sb,
787         void                    *data,
788         int                     silent)
789 {
790         vnode_t                 *rootvp;
791         struct vfs              *vfsp = vfs_allocate();
792         struct xfs_mount_args   *args = xfs_args_allocate(sb);
793         struct kstatfs          statvfs;
794         int                     error, error2;
795
796         vfsp->vfs_super = sb;
797         LINVFS_SET_VFS(sb, vfsp);
798         if (sb->s_flags & MS_RDONLY)
799                 vfsp->vfs_flag |= VFS_RDONLY;
800         bhv_insert_all_vfsops(vfsp);
801
802         VFS_PARSEARGS(vfsp, (char *)data, args, 0, error);
803         if (error) {
804                 bhv_remove_all_vfsops(vfsp, 1);
805                 goto fail_vfsop;
806         }
807
808         sb_min_blocksize(sb, BBSIZE);
809         sb->s_export_op = &linvfs_export_ops;
810         sb->s_qcop = &linvfs_qops;
811         sb->s_op = &linvfs_sops;
812
813         VFS_MOUNT(vfsp, args, NULL, error);
814         if (error) {
815                 bhv_remove_all_vfsops(vfsp, 1);
816                 goto fail_vfsop;
817         }
818
819         VFS_STATVFS(vfsp, &statvfs, NULL, error);
820         if (error)
821                 goto fail_unmount;
822
823         sb->s_dirt = 1;
824         sb->s_magic = statvfs.f_type;
825         sb->s_blocksize = statvfs.f_bsize;
826         sb->s_blocksize_bits = ffs(statvfs.f_bsize) - 1;
827         sb->s_maxbytes = xfs_max_file_offset(sb->s_blocksize_bits);
828         set_posix_acl_flag(sb);
829
830         VFS_ROOT(vfsp, &rootvp, error);
831         if (error)
832                 goto fail_unmount;
833
834         sb->s_root = d_alloc_root(LINVFS_GET_IP(rootvp));
835         if (!sb->s_root) {
836                 error = ENOMEM;
837                 goto fail_vnrele;
838         }
839         if (is_bad_inode(sb->s_root->d_inode)) {
840                 error = EINVAL;
841                 goto fail_vnrele;
842         }
843         if ((error = linvfs_start_syncd(vfsp)))
844                 goto fail_vnrele;
845         vn_trace_exit(rootvp, __FUNCTION__, (inst_t *)__return_address);
846
847         kmem_free(args, sizeof(*args));
848         return 0;
849
850 fail_vnrele:
851         if (sb->s_root) {
852                 dput(sb->s_root);
853                 sb->s_root = NULL;
854         } else {
855                 VN_RELE(rootvp);
856         }
857
858 fail_unmount:
859         VFS_UNMOUNT(vfsp, 0, NULL, error2);
860
861 fail_vfsop:
862         vfs_deallocate(vfsp);
863         kmem_free(args, sizeof(*args));
864         return -error;
865 }
866
867 STATIC struct super_block *
868 linvfs_get_sb(
869         struct file_system_type *fs_type,
870         int                     flags,
871         const char              *dev_name,
872         void                    *data)
873 {
874         return get_sb_bdev(fs_type, flags, dev_name, data, linvfs_fill_super);
875 }
876
877
878 STATIC struct export_operations linvfs_export_ops = {
879         .get_parent             = linvfs_get_parent,
880         .get_dentry             = linvfs_get_dentry,
881 };
882
883 STATIC struct super_operations linvfs_sops = {
884         .alloc_inode            = linvfs_alloc_inode,
885         .destroy_inode          = linvfs_destroy_inode,
886         .write_inode            = linvfs_write_inode,
887         .clear_inode            = linvfs_clear_inode,
888         .put_super              = linvfs_put_super,
889         .write_super            = linvfs_write_super,
890         .sync_fs                = linvfs_sync_super,
891         .write_super_lockfs     = linvfs_freeze_fs,
892         .statfs                 = linvfs_statfs,
893         .remount_fs             = linvfs_remount,
894         .show_options           = linvfs_show_options,
895 };
896
897 STATIC struct quotactl_ops linvfs_qops = {
898         .get_xstate             = linvfs_getxstate,
899         .set_xstate             = linvfs_setxstate,
900         .get_xquota             = linvfs_getxquota,
901         .set_xquota             = linvfs_setxquota,
902 };
903
904 STATIC struct file_system_type xfs_fs_type = {
905         .owner                  = THIS_MODULE,
906         .name                   = "xfs",
907         .get_sb                 = linvfs_get_sb,
908         .kill_sb                = kill_block_super,
909         .fs_flags               = FS_REQUIRES_DEV,
910 };
911
912
913 STATIC int __init
914 init_xfs_fs( void )
915 {
916         int                     error;
917         struct sysinfo          si;
918         static char             message[] __initdata = KERN_INFO \
919                 XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled\n";
920
921         printk(message);
922
923         si_meminfo(&si);
924         xfs_physmem = si.totalram;
925
926         ktrace_init(64);
927
928         error = init_inodecache();
929         if (error < 0)
930                 goto undo_inodecache;
931
932         error = pagebuf_init();
933         if (error < 0)
934                 goto undo_pagebuf;
935
936         vn_init();
937         xfs_init();
938         uuid_init();
939         vfs_initquota();
940
941         xfs_inode_shaker = kmem_shake_register(xfs_inode_shake);
942         if (!xfs_inode_shaker) {
943                 error = -ENOMEM;
944                 goto undo_shaker;
945         }
946
947         error = xfs_ioctl32_init();
948         if (error)
949                 goto undo_ioctl32;
950
951         error = register_filesystem(&xfs_fs_type);
952         if (error)
953                 goto undo_register;
954         XFS_DM_INIT(&xfs_fs_type);
955         return 0;
956
957 undo_register:
958         xfs_ioctl32_exit();
959
960 undo_ioctl32:
961         kmem_shake_deregister(xfs_inode_shaker);
962
963 undo_shaker:
964         pagebuf_terminate();
965
966 undo_pagebuf:
967         destroy_inodecache();
968
969 undo_inodecache:
970         return error;
971 }
972
973 STATIC void __exit
974 exit_xfs_fs( void )
975 {
976         vfs_exitquota();
977         XFS_DM_EXIT(&xfs_fs_type);
978         unregister_filesystem(&xfs_fs_type);
979         xfs_ioctl32_exit();
980         kmem_shake_deregister(xfs_inode_shaker);
981         xfs_cleanup();
982         pagebuf_terminate();
983         destroy_inodecache();
984         ktrace_uninit();
985 }
986
987 module_init(init_xfs_fs);
988 module_exit(exit_xfs_fs);
989
990 MODULE_AUTHOR("Silicon Graphics, Inc.");
991 MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
992 MODULE_LICENSE("GPL");