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