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