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