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