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