blob: fac4862d09a6c0fb7179a786636edd56f7473c7d [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_log.h"
35#include "xfs_clnt.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_trans.h"
38#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110039#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110047#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "xfs_dir_sf.h"
50#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110051#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "xfs_dinode.h"
53#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110054#include "xfs_btree.h"
55#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "xfs_rtalloc.h"
58#include "xfs_error.h"
59#include "xfs_itable.h"
60#include "xfs_rw.h"
61#include "xfs_acl.h"
62#include "xfs_cap.h"
63#include "xfs_mac.h"
64#include "xfs_attr.h"
65#include "xfs_buf_item.h"
66#include "xfs_utils.h"
67#include "xfs_version.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#include <linux/namei.h>
70#include <linux/init.h>
71#include <linux/mount.h>
Christoph Hellwig0829c362005-09-02 16:58:49 +100072#include <linux/mempool.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/writeback.h>
Christoph Hellwig4df08c52005-09-05 08:34:18 +100074#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
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
David Chinnere8c8b3a2005-11-02 10:33:05 +1100191 inode->i_blksize = xfs_preferred_iosize(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 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
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100280/*
281 * Try to write out the superblock using barriers.
282 */
283STATIC int
284xfs_barrier_test(
285 xfs_mount_t *mp)
286{
287 xfs_buf_t *sbp = xfs_getsb(mp, 0);
288 int error;
289
290 XFS_BUF_UNDONE(sbp);
291 XFS_BUF_UNREAD(sbp);
292 XFS_BUF_UNDELAYWRITE(sbp);
293 XFS_BUF_WRITE(sbp);
294 XFS_BUF_UNASYNC(sbp);
295 XFS_BUF_ORDERED(sbp);
296
297 xfsbdstrat(mp, sbp);
298 error = xfs_iowait(sbp);
299
300 /*
301 * Clear all the flags we set and possible error state in the
302 * buffer. We only did the write to try out whether barriers
303 * worked and shouldn't leave any traces in the superblock
304 * buffer.
305 */
306 XFS_BUF_DONE(sbp);
307 XFS_BUF_ERROR(sbp, 0);
308 XFS_BUF_UNORDERED(sbp);
309
310 xfs_buf_relse(sbp);
311 return error;
312}
313
314void
315xfs_mountfs_check_barriers(xfs_mount_t *mp)
316{
317 int error;
318
319 if (mp->m_logdev_targp != mp->m_ddev_targp) {
320 xfs_fs_cmn_err(CE_NOTE, mp,
321 "Disabling barriers, not supported with external log device");
322 mp->m_flags &= ~XFS_MOUNT_BARRIER;
323 }
324
325 if (mp->m_ddev_targp->pbr_bdev->bd_disk->queue->ordered ==
326 QUEUE_ORDERED_NONE) {
327 xfs_fs_cmn_err(CE_NOTE, mp,
328 "Disabling barriers, not supported by the underlying device");
329 mp->m_flags &= ~XFS_MOUNT_BARRIER;
330 }
331
332 error = xfs_barrier_test(mp);
333 if (error) {
334 xfs_fs_cmn_err(CE_NOTE, mp,
335 "Disabling barriers, trial barrier write failed");
336 mp->m_flags &= ~XFS_MOUNT_BARRIER;
337 }
338}
339
340void
341xfs_blkdev_issue_flush(
342 xfs_buftarg_t *buftarg)
343{
344 blkdev_issue_flush(buftarg->pbr_bdev, NULL);
345}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347STATIC struct inode *
348linvfs_alloc_inode(
349 struct super_block *sb)
350{
351 vnode_t *vp;
352
Christoph Hellwig0829c362005-09-02 16:58:49 +1000353 vp = kmem_cache_alloc(xfs_vnode_zone, kmem_flags_convert(KM_SLEEP));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!vp)
355 return NULL;
356 return LINVFS_GET_IP(vp);
357}
358
359STATIC void
360linvfs_destroy_inode(
361 struct inode *inode)
362{
Christoph Hellwig0829c362005-09-02 16:58:49 +1000363 kmem_zone_free(xfs_vnode_zone, LINVFS_GET_VP(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366STATIC void
Christoph Hellwig0829c362005-09-02 16:58:49 +1000367linvfs_inode_init_once(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 void *data,
369 kmem_cache_t *cachep,
370 unsigned long flags)
371{
372 vnode_t *vp = (vnode_t *)data;
373
374 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
375 SLAB_CTOR_CONSTRUCTOR)
376 inode_init_once(LINVFS_GET_IP(vp));
377}
378
379STATIC int
Christoph Hellwig0829c362005-09-02 16:58:49 +1000380linvfs_init_zones(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Christoph Hellwig0829c362005-09-02 16:58:49 +1000382 xfs_vnode_zone = kmem_cache_create("xfs_vnode",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 sizeof(vnode_t), 0, SLAB_RECLAIM_ACCOUNT,
Christoph Hellwig0829c362005-09-02 16:58:49 +1000384 linvfs_inode_init_once, NULL);
385 if (!xfs_vnode_zone)
386 goto out;
387
388 xfs_ioend_zone = kmem_zone_init(sizeof(xfs_ioend_t), "xfs_ioend");
389 if (!xfs_ioend_zone)
390 goto out_destroy_vnode_zone;
391
392 xfs_ioend_pool = mempool_create(4 * MAX_BUF_PER_PAGE,
393 mempool_alloc_slab, mempool_free_slab,
394 xfs_ioend_zone);
395 if (!xfs_ioend_pool)
396 goto out_free_ioend_zone;
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 0;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000399
400
401 out_free_ioend_zone:
402 kmem_zone_destroy(xfs_ioend_zone);
403 out_destroy_vnode_zone:
404 kmem_zone_destroy(xfs_vnode_zone);
405 out:
406 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409STATIC void
Christoph Hellwig0829c362005-09-02 16:58:49 +1000410linvfs_destroy_zones(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Christoph Hellwig0829c362005-09-02 16:58:49 +1000412 mempool_destroy(xfs_ioend_pool);
413 kmem_zone_destroy(xfs_vnode_zone);
414 kmem_zone_destroy(xfs_ioend_zone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417/*
418 * Attempt to flush the inode, this will actually fail
419 * if the inode is pinned, but we dirty the inode again
420 * at the point when it is unpinned after a log write,
421 * since this is when the inode itself becomes flushable.
422 */
423STATIC int
424linvfs_write_inode(
425 struct inode *inode,
426 int sync)
427{
428 vnode_t *vp = LINVFS_GET_VP(inode);
429 int error = 0, flags = FLUSH_INODE;
430
431 if (vp) {
432 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
433 if (sync)
434 flags |= FLUSH_SYNC;
435 VOP_IFLUSH(vp, flags, error);
436 if (error == EAGAIN) {
437 if (sync)
438 VOP_IFLUSH(vp, flags | FLUSH_LOG, error);
439 else
440 error = 0;
441 }
442 }
443
444 return -error;
445}
446
447STATIC void
448linvfs_clear_inode(
449 struct inode *inode)
450{
451 vnode_t *vp = LINVFS_GET_VP(inode);
Christoph Hellwig56d433e2005-09-05 08:23:54 +1000452 int error, cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Christoph Hellwig56d433e2005-09-05 08:23:54 +1000454 vn_trace_entry(vp, "clear_inode", (inst_t *)__return_address);
455
Christoph Hellwig56d433e2005-09-05 08:23:54 +1000456 XFS_STATS_INC(vn_rele);
457 XFS_STATS_INC(vn_remove);
458 XFS_STATS_INC(vn_reclaim);
459 XFS_STATS_DEC(vn_active);
460
Christoph Hellwig02ba71d2005-09-05 08:28:02 +1000461 /*
462 * This can happen because xfs_iget_core calls xfs_idestroy if we
463 * find an inode with di_mode == 0 but without IGET_CREATE set.
464 */
465 if (vp->v_fbhv)
466 VOP_INACTIVE(vp, NULL, cache);
Christoph Hellwig56d433e2005-09-05 08:23:54 +1000467
468 VN_LOCK(vp);
469 vp->v_flag &= ~VMODIFIED;
470 VN_UNLOCK(vp, 0);
471
Felix Blyakher0c147f92005-09-05 08:24:49 +1000472 if (vp->v_fbhv) {
473 VOP_RECLAIM(vp, error);
474 if (error)
475 panic("vn_purge: cannot reclaim");
476 }
Christoph Hellwig56d433e2005-09-05 08:23:54 +1000477
478 ASSERT(vp->v_fbhv == NULL);
479
480#ifdef XFS_VNODE_TRACE
481 ktrace_free(vp->v_trace);
482#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/*
486 * Enqueue a work item to be picked up by the vfs xfssyncd thread.
487 * Doing this has two advantages:
488 * - It saves on stack space, which is tight in certain situations
489 * - It can be used (with care) as a mechanism to avoid deadlocks.
490 * Flushing while allocating in a full filesystem requires both.
491 */
492STATIC void
493xfs_syncd_queue_work(
494 struct vfs *vfs,
495 void *data,
496 void (*syncer)(vfs_t *, void *))
497{
498 vfs_sync_work_t *work;
499
500 work = kmem_alloc(sizeof(struct vfs_sync_work), KM_SLEEP);
501 INIT_LIST_HEAD(&work->w_list);
502 work->w_syncer = syncer;
503 work->w_data = data;
504 work->w_vfs = vfs;
505 spin_lock(&vfs->vfs_sync_lock);
506 list_add_tail(&work->w_list, &vfs->vfs_sync_list);
507 spin_unlock(&vfs->vfs_sync_lock);
508 wake_up_process(vfs->vfs_sync_task);
509}
510
511/*
512 * Flush delayed allocate data, attempting to free up reserved space
513 * from existing allocations. At this point a new allocation attempt
514 * has failed with ENOSPC and we are in the process of scratching our
515 * heads, looking about for more room...
516 */
517STATIC void
518xfs_flush_inode_work(
519 vfs_t *vfs,
520 void *inode)
521{
522 filemap_flush(((struct inode *)inode)->i_mapping);
523 iput((struct inode *)inode);
524}
525
526void
527xfs_flush_inode(
528 xfs_inode_t *ip)
529{
530 struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
531 struct vfs *vfs = XFS_MTOVFS(ip->i_mount);
532
533 igrab(inode);
534 xfs_syncd_queue_work(vfs, inode, xfs_flush_inode_work);
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -0700535 delay(msecs_to_jiffies(500));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538/*
539 * This is the "bigger hammer" version of xfs_flush_inode_work...
540 * (IOW, "If at first you don't succeed, use a Bigger Hammer").
541 */
542STATIC void
543xfs_flush_device_work(
544 vfs_t *vfs,
545 void *inode)
546{
547 sync_blockdev(vfs->vfs_super->s_bdev);
548 iput((struct inode *)inode);
549}
550
551void
552xfs_flush_device(
553 xfs_inode_t *ip)
554{
555 struct inode *inode = LINVFS_GET_IP(XFS_ITOV(ip));
556 struct vfs *vfs = XFS_MTOVFS(ip->i_mount);
557
558 igrab(inode);
559 xfs_syncd_queue_work(vfs, inode, xfs_flush_device_work);
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -0700560 delay(msecs_to_jiffies(500));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 xfs_log_force(ip->i_mount, (xfs_lsn_t)0, XFS_LOG_FORCE|XFS_LOG_SYNC);
562}
563
564#define SYNCD_FLAGS (SYNC_FSDATA|SYNC_BDFLUSH|SYNC_ATTR)
565STATIC void
566vfs_sync_worker(
567 vfs_t *vfsp,
568 void *unused)
569{
570 int error;
571
572 if (!(vfsp->vfs_flag & VFS_RDONLY))
573 VFS_SYNC(vfsp, SYNCD_FLAGS, NULL, error);
574 vfsp->vfs_sync_seq++;
575 wmb();
576 wake_up(&vfsp->vfs_wait_single_sync_task);
577}
578
579STATIC int
580xfssyncd(
581 void *arg)
582{
583 long timeleft;
584 vfs_t *vfsp = (vfs_t *) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct vfs_sync_work *work, *n;
Christoph Hellwig4df08c52005-09-05 08:34:18 +1000586 LIST_HEAD (tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -0700588 timeleft = xfs_syncd_centisecs * msecs_to_jiffies(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 for (;;) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -0700590 timeleft = schedule_timeout_interruptible(timeleft);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /* swsusp */
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700592 try_to_freeze();
Christoph Hellwig4df08c52005-09-05 08:34:18 +1000593 if (kthread_should_stop())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 break;
595
596 spin_lock(&vfsp->vfs_sync_lock);
597 /*
598 * We can get woken by laptop mode, to do a sync -
599 * that's the (only!) case where the list would be
600 * empty with time remaining.
601 */
602 if (!timeleft || list_empty(&vfsp->vfs_sync_list)) {
603 if (!timeleft)
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -0700604 timeleft = xfs_syncd_centisecs *
605 msecs_to_jiffies(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 INIT_LIST_HEAD(&vfsp->vfs_sync_work.w_list);
607 list_add_tail(&vfsp->vfs_sync_work.w_list,
608 &vfsp->vfs_sync_list);
609 }
610 list_for_each_entry_safe(work, n, &vfsp->vfs_sync_list, w_list)
611 list_move(&work->w_list, &tmp);
612 spin_unlock(&vfsp->vfs_sync_lock);
613
614 list_for_each_entry_safe(work, n, &tmp, w_list) {
615 (*work->w_syncer)(vfsp, work->w_data);
616 list_del(&work->w_list);
617 if (work == &vfsp->vfs_sync_work)
618 continue;
619 kmem_free(work, sizeof(struct vfs_sync_work));
620 }
621 }
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624}
625
626STATIC int
627linvfs_start_syncd(
628 vfs_t *vfsp)
629{
Christoph Hellwig4df08c52005-09-05 08:34:18 +1000630 vfsp->vfs_sync_work.w_syncer = vfs_sync_worker;
631 vfsp->vfs_sync_work.w_vfs = vfsp;
632 vfsp->vfs_sync_task = kthread_run(xfssyncd, vfsp, "xfssyncd");
633 if (IS_ERR(vfsp->vfs_sync_task))
634 return -PTR_ERR(vfsp->vfs_sync_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return 0;
636}
637
638STATIC void
639linvfs_stop_syncd(
640 vfs_t *vfsp)
641{
Christoph Hellwig4df08c52005-09-05 08:34:18 +1000642 kthread_stop(vfsp->vfs_sync_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645STATIC void
646linvfs_put_super(
647 struct super_block *sb)
648{
649 vfs_t *vfsp = LINVFS_GET_VFS(sb);
650 int error;
651
652 linvfs_stop_syncd(vfsp);
653 VFS_SYNC(vfsp, SYNC_ATTR|SYNC_DELWRI, NULL, error);
654 if (!error)
655 VFS_UNMOUNT(vfsp, 0, NULL, error);
656 if (error) {
657 printk("XFS unmount got error %d\n", error);
658 printk("%s: vfsp/0x%p left dangling!\n", __FUNCTION__, vfsp);
659 return;
660 }
661
662 vfs_deallocate(vfsp);
663}
664
665STATIC void
666linvfs_write_super(
667 struct super_block *sb)
668{
669 vfs_t *vfsp = LINVFS_GET_VFS(sb);
670 int error;
671
672 if (sb->s_flags & MS_RDONLY) {
673 sb->s_dirt = 0; /* paranoia */
674 return;
675 }
676 /* Push the log and superblock a little */
677 VFS_SYNC(vfsp, SYNC_FSDATA, NULL, error);
678 sb->s_dirt = 0;
679}
680
681STATIC int
682linvfs_sync_super(
683 struct super_block *sb,
684 int wait)
685{
686 vfs_t *vfsp = LINVFS_GET_VFS(sb);
687 int error;
688 int flags = SYNC_FSDATA;
689
Christoph Hellwigf898d6c2005-06-21 15:40:48 +1000690 if (unlikely(sb->s_frozen == SB_FREEZE_WRITE))
691 flags = SYNC_QUIESCE;
692 else
693 flags = SYNC_FSDATA | (wait ? SYNC_WAIT : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 VFS_SYNC(vfsp, flags, NULL, error);
696 sb->s_dirt = 0;
697
698 if (unlikely(laptop_mode)) {
699 int prev_sync_seq = vfsp->vfs_sync_seq;
700
701 /*
702 * The disk must be active because we're syncing.
703 * We schedule xfssyncd now (now that the disk is
704 * active) instead of later (when it might not be).
705 */
706 wake_up_process(vfsp->vfs_sync_task);
707 /*
708 * We have to wait for the sync iteration to complete.
709 * If we don't, the disk activity caused by the sync
710 * will come after the sync is completed, and that
711 * triggers another sync from laptop mode.
712 */
713 wait_event(vfsp->vfs_wait_single_sync_task,
714 vfsp->vfs_sync_seq != prev_sync_seq);
715 }
716
717 return -error;
718}
719
720STATIC int
721linvfs_statfs(
722 struct super_block *sb,
723 struct kstatfs *statp)
724{
725 vfs_t *vfsp = LINVFS_GET_VFS(sb);
726 int error;
727
728 VFS_STATVFS(vfsp, statp, NULL, error);
729 return -error;
730}
731
732STATIC int
733linvfs_remount(
734 struct super_block *sb,
735 int *flags,
736 char *options)
737{
738 vfs_t *vfsp = LINVFS_GET_VFS(sb);
739 struct xfs_mount_args *args = xfs_args_allocate(sb);
740 int error;
741
742 VFS_PARSEARGS(vfsp, options, args, 1, error);
743 if (!error)
744 VFS_MNTUPDATE(vfsp, flags, args, error);
745 kmem_free(args, sizeof(*args));
746 return -error;
747}
748
749STATIC void
750linvfs_freeze_fs(
751 struct super_block *sb)
752{
753 VFS_FREEZE(LINVFS_GET_VFS(sb));
754}
755
756STATIC int
757linvfs_show_options(
758 struct seq_file *m,
759 struct vfsmount *mnt)
760{
761 struct vfs *vfsp = LINVFS_GET_VFS(mnt->mnt_sb);
762 int error;
763
764 VFS_SHOWARGS(vfsp, m, error);
765 return error;
766}
767
768STATIC int
Nathan Scottee348072005-11-02 10:32:38 +1100769linvfs_quotasync(
770 struct super_block *sb,
771 int type)
772{
773 struct vfs *vfsp = LINVFS_GET_VFS(sb);
774 int error;
775
776 VFS_QUOTACTL(vfsp, Q_XQUOTASYNC, 0, (caddr_t)NULL, error);
777 return -error;
778}
779
780STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781linvfs_getxstate(
782 struct super_block *sb,
783 struct fs_quota_stat *fqs)
784{
785 struct vfs *vfsp = LINVFS_GET_VFS(sb);
786 int error;
787
788 VFS_QUOTACTL(vfsp, Q_XGETQSTAT, 0, (caddr_t)fqs, error);
789 return -error;
790}
791
792STATIC int
793linvfs_setxstate(
794 struct super_block *sb,
795 unsigned int flags,
796 int op)
797{
798 struct vfs *vfsp = LINVFS_GET_VFS(sb);
799 int error;
800
801 VFS_QUOTACTL(vfsp, op, 0, (caddr_t)&flags, error);
802 return -error;
803}
804
805STATIC int
806linvfs_getxquota(
807 struct super_block *sb,
808 int type,
809 qid_t id,
810 struct fs_disk_quota *fdq)
811{
812 struct vfs *vfsp = LINVFS_GET_VFS(sb);
813 int error, getmode;
814
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000815 getmode = (type == USRQUOTA) ? Q_XGETQUOTA :
816 ((type == GRPQUOTA) ? Q_XGETGQUOTA : Q_XGETPQUOTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 VFS_QUOTACTL(vfsp, getmode, id, (caddr_t)fdq, error);
818 return -error;
819}
820
821STATIC int
822linvfs_setxquota(
823 struct super_block *sb,
824 int type,
825 qid_t id,
826 struct fs_disk_quota *fdq)
827{
828 struct vfs *vfsp = LINVFS_GET_VFS(sb);
829 int error, setmode;
830
Nathan Scottc8ad20f2005-06-21 15:38:48 +1000831 setmode = (type == USRQUOTA) ? Q_XSETQLIM :
832 ((type == GRPQUOTA) ? Q_XSETGQLIM : Q_XSETPQLIM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 VFS_QUOTACTL(vfsp, setmode, id, (caddr_t)fdq, error);
834 return -error;
835}
836
837STATIC int
838linvfs_fill_super(
839 struct super_block *sb,
840 void *data,
841 int silent)
842{
843 vnode_t *rootvp;
844 struct vfs *vfsp = vfs_allocate();
845 struct xfs_mount_args *args = xfs_args_allocate(sb);
846 struct kstatfs statvfs;
847 int error, error2;
848
849 vfsp->vfs_super = sb;
850 LINVFS_SET_VFS(sb, vfsp);
851 if (sb->s_flags & MS_RDONLY)
852 vfsp->vfs_flag |= VFS_RDONLY;
853 bhv_insert_all_vfsops(vfsp);
854
855 VFS_PARSEARGS(vfsp, (char *)data, args, 0, error);
856 if (error) {
857 bhv_remove_all_vfsops(vfsp, 1);
858 goto fail_vfsop;
859 }
860
861 sb_min_blocksize(sb, BBSIZE);
862#ifdef CONFIG_XFS_EXPORT
863 sb->s_export_op = &linvfs_export_ops;
864#endif
865 sb->s_qcop = &linvfs_qops;
866 sb->s_op = &linvfs_sops;
867
868 VFS_MOUNT(vfsp, args, NULL, error);
869 if (error) {
870 bhv_remove_all_vfsops(vfsp, 1);
871 goto fail_vfsop;
872 }
873
874 VFS_STATVFS(vfsp, &statvfs, NULL, error);
875 if (error)
876 goto fail_unmount;
877
878 sb->s_dirt = 1;
879 sb->s_magic = statvfs.f_type;
880 sb->s_blocksize = statvfs.f_bsize;
881 sb->s_blocksize_bits = ffs(statvfs.f_bsize) - 1;
882 sb->s_maxbytes = xfs_max_file_offset(sb->s_blocksize_bits);
883 sb->s_time_gran = 1;
884 set_posix_acl_flag(sb);
885
886 VFS_ROOT(vfsp, &rootvp, error);
887 if (error)
888 goto fail_unmount;
889
890 sb->s_root = d_alloc_root(LINVFS_GET_IP(rootvp));
891 if (!sb->s_root) {
892 error = ENOMEM;
893 goto fail_vnrele;
894 }
895 if (is_bad_inode(sb->s_root->d_inode)) {
896 error = EINVAL;
897 goto fail_vnrele;
898 }
899 if ((error = linvfs_start_syncd(vfsp)))
900 goto fail_vnrele;
901 vn_trace_exit(rootvp, __FUNCTION__, (inst_t *)__return_address);
902
903 kmem_free(args, sizeof(*args));
904 return 0;
905
906fail_vnrele:
907 if (sb->s_root) {
908 dput(sb->s_root);
909 sb->s_root = NULL;
910 } else {
911 VN_RELE(rootvp);
912 }
913
914fail_unmount:
915 VFS_UNMOUNT(vfsp, 0, NULL, error2);
916
917fail_vfsop:
918 vfs_deallocate(vfsp);
919 kmem_free(args, sizeof(*args));
920 return -error;
921}
922
923STATIC struct super_block *
924linvfs_get_sb(
925 struct file_system_type *fs_type,
926 int flags,
927 const char *dev_name,
928 void *data)
929{
930 return get_sb_bdev(fs_type, flags, dev_name, data, linvfs_fill_super);
931}
932
933STATIC struct super_operations linvfs_sops = {
934 .alloc_inode = linvfs_alloc_inode,
935 .destroy_inode = linvfs_destroy_inode,
936 .write_inode = linvfs_write_inode,
937 .clear_inode = linvfs_clear_inode,
938 .put_super = linvfs_put_super,
939 .write_super = linvfs_write_super,
940 .sync_fs = linvfs_sync_super,
941 .write_super_lockfs = linvfs_freeze_fs,
942 .statfs = linvfs_statfs,
943 .remount_fs = linvfs_remount,
944 .show_options = linvfs_show_options,
945};
946
947STATIC struct quotactl_ops linvfs_qops = {
Nathan Scottee348072005-11-02 10:32:38 +1100948 .quota_sync = linvfs_quotasync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 .get_xstate = linvfs_getxstate,
950 .set_xstate = linvfs_setxstate,
951 .get_xquota = linvfs_getxquota,
952 .set_xquota = linvfs_setxquota,
953};
954
955STATIC struct file_system_type xfs_fs_type = {
956 .owner = THIS_MODULE,
957 .name = "xfs",
958 .get_sb = linvfs_get_sb,
959 .kill_sb = kill_block_super,
960 .fs_flags = FS_REQUIRES_DEV,
961};
962
963
964STATIC int __init
965init_xfs_fs( void )
966{
967 int error;
968 struct sysinfo si;
969 static char message[] __initdata = KERN_INFO \
970 XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled\n";
971
972 printk(message);
973
974 si_meminfo(&si);
975 xfs_physmem = si.totalram;
976
977 ktrace_init(64);
978
Christoph Hellwig0829c362005-09-02 16:58:49 +1000979 error = linvfs_init_zones();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (error < 0)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000981 goto undo_zones;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 error = pagebuf_init();
984 if (error < 0)
985 goto undo_pagebuf;
986
987 vn_init();
988 xfs_init();
989 uuid_init();
990 vfs_initquota();
991
992 error = register_filesystem(&xfs_fs_type);
993 if (error)
994 goto undo_register;
995 XFS_DM_INIT(&xfs_fs_type);
996 return 0;
997
998undo_register:
999 pagebuf_terminate();
1000
1001undo_pagebuf:
Christoph Hellwig0829c362005-09-02 16:58:49 +10001002 linvfs_destroy_zones();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Christoph Hellwig0829c362005-09-02 16:58:49 +10001004undo_zones:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return error;
1006}
1007
1008STATIC void __exit
1009exit_xfs_fs( void )
1010{
1011 vfs_exitquota();
1012 XFS_DM_EXIT(&xfs_fs_type);
1013 unregister_filesystem(&xfs_fs_type);
1014 xfs_cleanup();
1015 pagebuf_terminate();
Christoph Hellwig0829c362005-09-02 16:58:49 +10001016 linvfs_destroy_zones();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 ktrace_uninit();
1018}
1019
1020module_init(init_xfs_fs);
1021module_exit(exit_xfs_fs);
1022
1023MODULE_AUTHOR("Silicon Graphics, Inc.");
1024MODULE_DESCRIPTION(XFS_VERSION_STRING " with " XFS_BUILD_OPTIONS " enabled");
1025MODULE_LICENSE("GPL");