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