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