David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
| 3 | * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. |
| 4 | * |
| 5 | * This copyrighted material is made available to anyone wishing to use, |
| 6 | * modify, copy, or redistribute it subject to the terms and conditions |
| 7 | * of the GNU General Public License v.2. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/completion.h> |
| 14 | #include <linux/buffer_head.h> |
| 15 | #include <linux/posix_acl.h> |
| 16 | #include <linux/sort.h> |
| 17 | #include <asm/semaphore.h> |
| 18 | |
| 19 | #include "gfs2.h" |
| 20 | #include "acl.h" |
| 21 | #include "bmap.h" |
| 22 | #include "dir.h" |
| 23 | #include "eattr.h" |
| 24 | #include "glock.h" |
| 25 | #include "glops.h" |
| 26 | #include "inode.h" |
| 27 | #include "log.h" |
| 28 | #include "meta_io.h" |
| 29 | #include "ops_address.h" |
| 30 | #include "ops_file.h" |
| 31 | #include "ops_inode.h" |
| 32 | #include "quota.h" |
| 33 | #include "rgrp.h" |
| 34 | #include "trans.h" |
| 35 | #include "unlinked.h" |
| 36 | |
| 37 | /** |
| 38 | * inode_attr_in - Copy attributes from the dinode into the VFS inode |
| 39 | * @ip: The GFS2 inode (with embedded disk inode data) |
| 40 | * @inode: The Linux VFS inode |
| 41 | * |
| 42 | */ |
| 43 | |
| 44 | static void inode_attr_in(struct gfs2_inode *ip, struct inode *inode) |
| 45 | { |
| 46 | inode->i_ino = ip->i_num.no_formal_ino; |
| 47 | |
| 48 | switch (ip->i_di.di_mode & S_IFMT) { |
| 49 | case S_IFBLK: |
| 50 | case S_IFCHR: |
| 51 | inode->i_rdev = MKDEV(ip->i_di.di_major, ip->i_di.di_minor); |
| 52 | break; |
| 53 | default: |
| 54 | inode->i_rdev = 0; |
| 55 | break; |
| 56 | }; |
| 57 | |
| 58 | inode->i_mode = ip->i_di.di_mode; |
| 59 | inode->i_nlink = ip->i_di.di_nlink; |
| 60 | inode->i_uid = ip->i_di.di_uid; |
| 61 | inode->i_gid = ip->i_di.di_gid; |
| 62 | i_size_write(inode, ip->i_di.di_size); |
| 63 | inode->i_atime.tv_sec = ip->i_di.di_atime; |
| 64 | inode->i_mtime.tv_sec = ip->i_di.di_mtime; |
| 65 | inode->i_ctime.tv_sec = ip->i_di.di_ctime; |
| 66 | inode->i_atime.tv_nsec = 0; |
| 67 | inode->i_mtime.tv_nsec = 0; |
| 68 | inode->i_ctime.tv_nsec = 0; |
| 69 | inode->i_blksize = PAGE_SIZE; |
| 70 | inode->i_blocks = ip->i_di.di_blocks << |
| 71 | (ip->i_sbd->sd_sb.sb_bsize_shift - GFS2_BASIC_BLOCK_SHIFT); |
| 72 | |
| 73 | if (ip->i_di.di_flags & GFS2_DIF_IMMUTABLE) |
| 74 | inode->i_flags |= S_IMMUTABLE; |
| 75 | else |
| 76 | inode->i_flags &= ~S_IMMUTABLE; |
| 77 | |
| 78 | if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY) |
| 79 | inode->i_flags |= S_APPEND; |
| 80 | else |
| 81 | inode->i_flags &= ~S_APPEND; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * gfs2_inode_attr_in - Copy attributes from the dinode into the VFS inode |
| 86 | * @ip: The GFS2 inode (with embedded disk inode data) |
| 87 | * |
| 88 | */ |
| 89 | |
| 90 | void gfs2_inode_attr_in(struct gfs2_inode *ip) |
| 91 | { |
| 92 | struct inode *inode; |
| 93 | |
| 94 | inode = gfs2_ip2v_lookup(ip); |
| 95 | if (inode) { |
| 96 | inode_attr_in(ip, inode); |
| 97 | iput(inode); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * gfs2_inode_attr_out - Copy attributes from VFS inode into the dinode |
| 103 | * @ip: The GFS2 inode |
| 104 | * |
| 105 | * Only copy out the attributes that we want the VFS layer |
| 106 | * to be able to modify. |
| 107 | */ |
| 108 | |
| 109 | void gfs2_inode_attr_out(struct gfs2_inode *ip) |
| 110 | { |
| 111 | struct inode *inode = ip->i_vnode; |
| 112 | |
| 113 | gfs2_assert_withdraw(ip->i_sbd, |
| 114 | (ip->i_di.di_mode & S_IFMT) == (inode->i_mode & S_IFMT)); |
| 115 | ip->i_di.di_mode = inode->i_mode; |
| 116 | ip->i_di.di_uid = inode->i_uid; |
| 117 | ip->i_di.di_gid = inode->i_gid; |
| 118 | ip->i_di.di_atime = inode->i_atime.tv_sec; |
| 119 | ip->i_di.di_mtime = inode->i_mtime.tv_sec; |
| 120 | ip->i_di.di_ctime = inode->i_ctime.tv_sec; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * gfs2_ip2v_lookup - Get the struct inode for a struct gfs2_inode |
| 125 | * @ip: the struct gfs2_inode to get the struct inode for |
| 126 | * |
| 127 | * Returns: A VFS inode, or NULL if none |
| 128 | */ |
| 129 | |
| 130 | struct inode *gfs2_ip2v_lookup(struct gfs2_inode *ip) |
| 131 | { |
| 132 | struct inode *inode = NULL; |
| 133 | |
| 134 | gfs2_assert_warn(ip->i_sbd, test_bit(GIF_MIN_INIT, &ip->i_flags)); |
| 135 | |
| 136 | spin_lock(&ip->i_spin); |
| 137 | if (ip->i_vnode) |
| 138 | inode = igrab(ip->i_vnode); |
| 139 | spin_unlock(&ip->i_spin); |
| 140 | |
| 141 | return inode; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * gfs2_ip2v - Get/Create a struct inode for a struct gfs2_inode |
| 146 | * @ip: the struct gfs2_inode to get the struct inode for |
| 147 | * |
| 148 | * Returns: A VFS inode, or NULL if no mem |
| 149 | */ |
| 150 | |
| 151 | struct inode *gfs2_ip2v(struct gfs2_inode *ip) |
| 152 | { |
| 153 | struct inode *inode, *tmp; |
| 154 | |
| 155 | inode = gfs2_ip2v_lookup(ip); |
| 156 | if (inode) |
| 157 | return inode; |
| 158 | |
| 159 | tmp = new_inode(ip->i_sbd->sd_vfs); |
| 160 | if (!tmp) |
| 161 | return NULL; |
| 162 | |
| 163 | inode_attr_in(ip, tmp); |
| 164 | |
| 165 | if (S_ISREG(ip->i_di.di_mode)) { |
| 166 | tmp->i_op = &gfs2_file_iops; |
| 167 | tmp->i_fop = &gfs2_file_fops; |
| 168 | tmp->i_mapping->a_ops = &gfs2_file_aops; |
| 169 | } else if (S_ISDIR(ip->i_di.di_mode)) { |
| 170 | tmp->i_op = &gfs2_dir_iops; |
| 171 | tmp->i_fop = &gfs2_dir_fops; |
| 172 | } else if (S_ISLNK(ip->i_di.di_mode)) { |
| 173 | tmp->i_op = &gfs2_symlink_iops; |
| 174 | } else { |
| 175 | tmp->i_op = &gfs2_dev_iops; |
| 176 | init_special_inode(tmp, tmp->i_mode, tmp->i_rdev); |
| 177 | } |
| 178 | |
| 179 | set_v2ip(tmp, NULL); |
| 180 | |
| 181 | for (;;) { |
| 182 | spin_lock(&ip->i_spin); |
| 183 | if (!ip->i_vnode) |
| 184 | break; |
| 185 | inode = igrab(ip->i_vnode); |
| 186 | spin_unlock(&ip->i_spin); |
| 187 | |
| 188 | if (inode) { |
| 189 | iput(tmp); |
| 190 | return inode; |
| 191 | } |
| 192 | yield(); |
| 193 | } |
| 194 | |
| 195 | inode = tmp; |
| 196 | |
| 197 | gfs2_inode_hold(ip); |
| 198 | ip->i_vnode = inode; |
| 199 | set_v2ip(inode, ip); |
| 200 | |
| 201 | spin_unlock(&ip->i_spin); |
| 202 | |
| 203 | insert_inode_hash(inode); |
| 204 | |
| 205 | return inode; |
| 206 | } |
| 207 | |
| 208 | static int iget_test(struct inode *inode, void *opaque) |
| 209 | { |
| 210 | struct gfs2_inode *ip = get_v2ip(inode); |
| 211 | struct gfs2_inum *inum = (struct gfs2_inum *)opaque; |
| 212 | |
| 213 | if (ip && ip->i_num.no_addr == inum->no_addr) |
| 214 | return 1; |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum *inum) |
| 220 | { |
| 221 | return ilookup5(sb, (unsigned long)inum->no_formal_ino, |
| 222 | iget_test, inum); |
| 223 | } |
| 224 | |
| 225 | void gfs2_inode_min_init(struct gfs2_inode *ip, unsigned int type) |
| 226 | { |
| 227 | spin_lock(&ip->i_spin); |
| 228 | if (!test_and_set_bit(GIF_MIN_INIT, &ip->i_flags)) { |
| 229 | ip->i_di.di_nlink = 1; |
| 230 | ip->i_di.di_mode = DT2IF(type); |
| 231 | } |
| 232 | spin_unlock(&ip->i_spin); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * gfs2_inode_refresh - Refresh the incore copy of the dinode |
| 237 | * @ip: The GFS2 inode |
| 238 | * |
| 239 | * Returns: errno |
| 240 | */ |
| 241 | |
| 242 | int gfs2_inode_refresh(struct gfs2_inode *ip) |
| 243 | { |
| 244 | struct buffer_head *dibh; |
| 245 | int error; |
| 246 | |
| 247 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 248 | if (error) |
| 249 | return error; |
| 250 | |
| 251 | if (gfs2_metatype_check(ip->i_sbd, dibh, GFS2_METATYPE_DI)) { |
| 252 | brelse(dibh); |
| 253 | return -EIO; |
| 254 | } |
| 255 | |
| 256 | spin_lock(&ip->i_spin); |
| 257 | gfs2_dinode_in(&ip->i_di, dibh->b_data); |
| 258 | set_bit(GIF_MIN_INIT, &ip->i_flags); |
| 259 | spin_unlock(&ip->i_spin); |
| 260 | |
| 261 | brelse(dibh); |
| 262 | |
| 263 | if (ip->i_num.no_addr != ip->i_di.di_num.no_addr) { |
| 264 | if (gfs2_consist_inode(ip)) |
| 265 | gfs2_dinode_print(&ip->i_di); |
| 266 | return -EIO; |
| 267 | } |
| 268 | if (ip->i_num.no_formal_ino != ip->i_di.di_num.no_formal_ino) |
| 269 | return -ESTALE; |
| 270 | |
| 271 | ip->i_vn = ip->i_gl->gl_vn; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * inode_create - create a struct gfs2_inode |
| 278 | * @i_gl: The glock covering the inode |
| 279 | * @inum: The inode number |
| 280 | * @io_gl: the iopen glock to acquire/hold (using holder in new gfs2_inode) |
| 281 | * @io_state: the state the iopen glock should be acquired in |
| 282 | * @ipp: pointer to put the returned inode in |
| 283 | * |
| 284 | * Returns: errno |
| 285 | */ |
| 286 | |
| 287 | static int inode_create(struct gfs2_glock *i_gl, struct gfs2_inum *inum, |
| 288 | struct gfs2_glock *io_gl, unsigned int io_state, |
| 289 | struct gfs2_inode **ipp) |
| 290 | { |
| 291 | struct gfs2_sbd *sdp = i_gl->gl_sbd; |
| 292 | struct gfs2_inode *ip; |
| 293 | int error = 0; |
| 294 | |
| 295 | ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL); |
| 296 | if (!ip) |
| 297 | return -ENOMEM; |
| 298 | memset(ip, 0, sizeof(struct gfs2_inode)); |
| 299 | |
| 300 | ip->i_num = *inum; |
| 301 | |
| 302 | atomic_set(&ip->i_count, 1); |
| 303 | |
| 304 | ip->i_vn = i_gl->gl_vn - 1; |
| 305 | |
| 306 | ip->i_gl = i_gl; |
| 307 | ip->i_sbd = sdp; |
| 308 | |
| 309 | spin_lock_init(&ip->i_spin); |
| 310 | init_rwsem(&ip->i_rw_mutex); |
| 311 | |
| 312 | ip->i_greedy = gfs2_tune_get(sdp, gt_greedy_default); |
| 313 | |
| 314 | error = gfs2_glock_nq_init(io_gl, |
| 315 | io_state, GL_LOCAL_EXCL | GL_EXACT, |
| 316 | &ip->i_iopen_gh); |
| 317 | if (error) |
| 318 | goto fail; |
| 319 | ip->i_iopen_gh.gh_owner = NULL; |
| 320 | |
| 321 | spin_lock(&io_gl->gl_spin); |
| 322 | gfs2_glock_hold(i_gl); |
| 323 | set_gl2gl(io_gl, i_gl); |
| 324 | spin_unlock(&io_gl->gl_spin); |
| 325 | |
| 326 | gfs2_glock_hold(i_gl); |
| 327 | set_gl2ip(i_gl, ip); |
| 328 | |
| 329 | atomic_inc(&sdp->sd_inode_count); |
| 330 | |
| 331 | *ipp = ip; |
| 332 | |
| 333 | return 0; |
| 334 | |
| 335 | fail: |
| 336 | gfs2_meta_cache_flush(ip); |
| 337 | kmem_cache_free(gfs2_inode_cachep, ip); |
| 338 | *ipp = NULL; |
| 339 | |
| 340 | return error; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * gfs2_inode_get - Create or get a reference on an inode |
| 345 | * @i_gl: The glock covering the inode |
| 346 | * @inum: The inode number |
| 347 | * @create: |
| 348 | * @ipp: pointer to put the returned inode in |
| 349 | * |
| 350 | * Returns: errno |
| 351 | */ |
| 352 | |
| 353 | int gfs2_inode_get(struct gfs2_glock *i_gl, struct gfs2_inum *inum, int create, |
| 354 | struct gfs2_inode **ipp) |
| 355 | { |
| 356 | struct gfs2_sbd *sdp = i_gl->gl_sbd; |
| 357 | struct gfs2_glock *io_gl; |
| 358 | int error = 0; |
| 359 | |
| 360 | gfs2_glmutex_lock(i_gl); |
| 361 | |
| 362 | *ipp = get_gl2ip(i_gl); |
| 363 | if (*ipp) { |
| 364 | error = -ESTALE; |
| 365 | if ((*ipp)->i_num.no_formal_ino != inum->no_formal_ino) |
| 366 | goto out; |
| 367 | atomic_inc(&(*ipp)->i_count); |
| 368 | error = 0; |
| 369 | goto out; |
| 370 | } |
| 371 | |
| 372 | if (!create) |
| 373 | goto out; |
| 374 | |
| 375 | error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops, |
| 376 | CREATE, &io_gl); |
| 377 | if (!error) { |
| 378 | error = inode_create(i_gl, inum, io_gl, LM_ST_SHARED, ipp); |
| 379 | gfs2_glock_put(io_gl); |
| 380 | } |
| 381 | |
| 382 | out: |
| 383 | gfs2_glmutex_unlock(i_gl); |
| 384 | |
| 385 | return error; |
| 386 | } |
| 387 | |
| 388 | void gfs2_inode_hold(struct gfs2_inode *ip) |
| 389 | { |
| 390 | gfs2_assert(ip->i_sbd, atomic_read(&ip->i_count) > 0); |
| 391 | atomic_inc(&ip->i_count); |
| 392 | } |
| 393 | |
| 394 | void gfs2_inode_put(struct gfs2_inode *ip) |
| 395 | { |
| 396 | gfs2_assert(ip->i_sbd, atomic_read(&ip->i_count) > 0); |
| 397 | atomic_dec(&ip->i_count); |
| 398 | } |
| 399 | |
| 400 | void gfs2_inode_destroy(struct gfs2_inode *ip) |
| 401 | { |
| 402 | struct gfs2_sbd *sdp = ip->i_sbd; |
| 403 | struct gfs2_glock *io_gl = ip->i_iopen_gh.gh_gl; |
| 404 | struct gfs2_glock *i_gl = ip->i_gl; |
| 405 | |
| 406 | gfs2_assert_warn(sdp, !atomic_read(&ip->i_count)); |
| 407 | gfs2_assert(sdp, get_gl2gl(io_gl) == i_gl); |
| 408 | |
| 409 | spin_lock(&io_gl->gl_spin); |
| 410 | set_gl2gl(io_gl, NULL); |
| 411 | gfs2_glock_put(i_gl); |
| 412 | spin_unlock(&io_gl->gl_spin); |
| 413 | |
| 414 | gfs2_glock_dq_uninit(&ip->i_iopen_gh); |
| 415 | |
| 416 | gfs2_meta_cache_flush(ip); |
| 417 | kmem_cache_free(gfs2_inode_cachep, ip); |
| 418 | |
| 419 | set_gl2ip(i_gl, NULL); |
| 420 | gfs2_glock_put(i_gl); |
| 421 | |
| 422 | atomic_dec(&sdp->sd_inode_count); |
| 423 | } |
| 424 | |
| 425 | static int dinode_dealloc(struct gfs2_inode *ip, struct gfs2_unlinked *ul) |
| 426 | { |
| 427 | struct gfs2_sbd *sdp = ip->i_sbd; |
| 428 | struct gfs2_alloc *al; |
| 429 | struct gfs2_rgrpd *rgd; |
| 430 | int error; |
| 431 | |
| 432 | if (ip->i_di.di_blocks != 1) { |
| 433 | if (gfs2_consist_inode(ip)) |
| 434 | gfs2_dinode_print(&ip->i_di); |
| 435 | return -EIO; |
| 436 | } |
| 437 | |
| 438 | al = gfs2_alloc_get(ip); |
| 439 | |
| 440 | error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); |
| 441 | if (error) |
| 442 | goto out; |
| 443 | |
| 444 | error = gfs2_rindex_hold(sdp, &al->al_ri_gh); |
| 445 | if (error) |
| 446 | goto out_qs; |
| 447 | |
| 448 | rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr); |
| 449 | if (!rgd) { |
| 450 | gfs2_consist_inode(ip); |
| 451 | error = -EIO; |
| 452 | goto out_rindex_relse; |
| 453 | } |
| 454 | |
| 455 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, |
| 456 | &al->al_rgd_gh); |
| 457 | if (error) |
| 458 | goto out_rindex_relse; |
| 459 | |
| 460 | error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED + |
| 461 | RES_STATFS + RES_QUOTA, 1); |
| 462 | if (error) |
| 463 | goto out_rg_gunlock; |
| 464 | |
| 465 | gfs2_trans_add_gl(ip->i_gl); |
| 466 | |
| 467 | gfs2_free_di(rgd, ip); |
| 468 | |
| 469 | error = gfs2_unlinked_ondisk_rm(sdp, ul); |
| 470 | |
| 471 | gfs2_trans_end(sdp); |
| 472 | clear_bit(GLF_STICKY, &ip->i_gl->gl_flags); |
| 473 | |
| 474 | out_rg_gunlock: |
| 475 | gfs2_glock_dq_uninit(&al->al_rgd_gh); |
| 476 | |
| 477 | out_rindex_relse: |
| 478 | gfs2_glock_dq_uninit(&al->al_ri_gh); |
| 479 | |
| 480 | out_qs: |
| 481 | gfs2_quota_unhold(ip); |
| 482 | |
| 483 | out: |
| 484 | gfs2_alloc_put(ip); |
| 485 | |
| 486 | return error; |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * inode_dealloc - Deallocate all on-disk blocks for an inode (dinode) |
| 491 | * @sdp: the filesystem |
| 492 | * @inum: the inode number to deallocate |
| 493 | * @io_gh: a holder for the iopen glock for this inode |
| 494 | * |
| 495 | * Returns: errno |
| 496 | */ |
| 497 | |
| 498 | static int inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul, |
| 499 | struct gfs2_holder *io_gh) |
| 500 | { |
| 501 | struct gfs2_inode *ip; |
| 502 | struct gfs2_holder i_gh; |
| 503 | int error; |
| 504 | |
| 505 | error = gfs2_glock_nq_num(sdp, |
| 506 | ul->ul_ut.ut_inum.no_addr, &gfs2_inode_glops, |
| 507 | LM_ST_EXCLUSIVE, 0, &i_gh); |
| 508 | if (error) |
| 509 | return error; |
| 510 | |
| 511 | /* We reacquire the iopen lock here to avoid a race with the NFS server |
| 512 | calling gfs2_read_inode() with the inode number of a inode we're in |
| 513 | the process of deallocating. And we can't keep our hold on the lock |
| 514 | from inode_dealloc_init() for deadlock reasons. */ |
| 515 | |
| 516 | gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY, io_gh); |
| 517 | error = gfs2_glock_nq(io_gh); |
| 518 | switch (error) { |
| 519 | case 0: |
| 520 | break; |
| 521 | case GLR_TRYFAILED: |
| 522 | error = 1; |
| 523 | default: |
| 524 | goto out; |
| 525 | } |
| 526 | |
| 527 | gfs2_assert_warn(sdp, !get_gl2ip(i_gh.gh_gl)); |
| 528 | error = inode_create(i_gh.gh_gl, &ul->ul_ut.ut_inum, io_gh->gh_gl, |
| 529 | LM_ST_EXCLUSIVE, &ip); |
| 530 | |
| 531 | gfs2_glock_dq(io_gh); |
| 532 | |
| 533 | if (error) |
| 534 | goto out; |
| 535 | |
| 536 | error = gfs2_inode_refresh(ip); |
| 537 | if (error) |
| 538 | goto out_iput; |
| 539 | |
| 540 | if (ip->i_di.di_nlink) { |
| 541 | if (gfs2_consist_inode(ip)) |
| 542 | gfs2_dinode_print(&ip->i_di); |
| 543 | error = -EIO; |
| 544 | goto out_iput; |
| 545 | } |
| 546 | |
| 547 | if (S_ISDIR(ip->i_di.di_mode) && |
| 548 | (ip->i_di.di_flags & GFS2_DIF_EXHASH)) { |
| 549 | error = gfs2_dir_exhash_dealloc(ip); |
| 550 | if (error) |
| 551 | goto out_iput; |
| 552 | } |
| 553 | |
| 554 | if (ip->i_di.di_eattr) { |
| 555 | error = gfs2_ea_dealloc(ip); |
| 556 | if (error) |
| 557 | goto out_iput; |
| 558 | } |
| 559 | |
| 560 | if (!gfs2_is_stuffed(ip)) { |
| 561 | error = gfs2_file_dealloc(ip); |
| 562 | if (error) |
| 563 | goto out_iput; |
| 564 | } |
| 565 | |
| 566 | error = dinode_dealloc(ip, ul); |
| 567 | if (error) |
| 568 | goto out_iput; |
| 569 | |
| 570 | out_iput: |
| 571 | gfs2_glmutex_lock(i_gh.gh_gl); |
| 572 | gfs2_inode_put(ip); |
| 573 | gfs2_inode_destroy(ip); |
| 574 | gfs2_glmutex_unlock(i_gh.gh_gl); |
| 575 | |
| 576 | out: |
| 577 | gfs2_glock_dq_uninit(&i_gh); |
| 578 | |
| 579 | return error; |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * try_inode_dealloc - Try to deallocate an inode and all its blocks |
| 584 | * @sdp: the filesystem |
| 585 | * |
| 586 | * Returns: 0 on success, -errno on error, 1 on busy (inode open) |
| 587 | */ |
| 588 | |
| 589 | static int try_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul) |
| 590 | { |
| 591 | struct gfs2_holder io_gh; |
| 592 | int error = 0; |
| 593 | |
| 594 | gfs2_try_toss_inode(sdp, &ul->ul_ut.ut_inum); |
| 595 | |
| 596 | error = gfs2_glock_nq_num(sdp, |
| 597 | ul->ul_ut.ut_inum.no_addr, &gfs2_iopen_glops, |
| 598 | LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB, &io_gh); |
| 599 | switch (error) { |
| 600 | case 0: |
| 601 | break; |
| 602 | case GLR_TRYFAILED: |
| 603 | return 1; |
| 604 | default: |
| 605 | return error; |
| 606 | } |
| 607 | |
| 608 | gfs2_glock_dq(&io_gh); |
| 609 | error = inode_dealloc(sdp, ul, &io_gh); |
| 610 | gfs2_holder_uninit(&io_gh); |
| 611 | |
| 612 | return error; |
| 613 | } |
| 614 | |
| 615 | static int inode_dealloc_uninit(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul) |
| 616 | { |
| 617 | struct gfs2_rgrpd *rgd; |
| 618 | struct gfs2_holder ri_gh, rgd_gh; |
| 619 | int error; |
| 620 | |
| 621 | error = gfs2_rindex_hold(sdp, &ri_gh); |
| 622 | if (error) |
| 623 | return error; |
| 624 | |
| 625 | rgd = gfs2_blk2rgrpd(sdp, ul->ul_ut.ut_inum.no_addr); |
| 626 | if (!rgd) { |
| 627 | gfs2_consist(sdp); |
| 628 | error = -EIO; |
| 629 | goto out; |
| 630 | } |
| 631 | |
| 632 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rgd_gh); |
| 633 | if (error) |
| 634 | goto out; |
| 635 | |
| 636 | error = gfs2_trans_begin(sdp, |
| 637 | RES_RG_BIT + RES_UNLINKED + RES_STATFS, |
| 638 | 0); |
| 639 | if (error) |
| 640 | goto out_gunlock; |
| 641 | |
| 642 | gfs2_free_uninit_di(rgd, ul->ul_ut.ut_inum.no_addr); |
| 643 | gfs2_unlinked_ondisk_rm(sdp, ul); |
| 644 | |
| 645 | gfs2_trans_end(sdp); |
| 646 | |
| 647 | out_gunlock: |
| 648 | gfs2_glock_dq_uninit(&rgd_gh); |
| 649 | out: |
| 650 | gfs2_glock_dq_uninit(&ri_gh); |
| 651 | |
| 652 | return error; |
| 653 | } |
| 654 | |
| 655 | int gfs2_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul) |
| 656 | { |
| 657 | if (ul->ul_ut.ut_flags & GFS2_UTF_UNINIT) |
| 658 | return inode_dealloc_uninit(sdp, ul); |
| 659 | else |
| 660 | return try_inode_dealloc(sdp, ul); |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * gfs2_change_nlink - Change nlink count on inode |
| 665 | * @ip: The GFS2 inode |
| 666 | * @diff: The change in the nlink count required |
| 667 | * |
| 668 | * Returns: errno |
| 669 | */ |
| 670 | |
| 671 | int gfs2_change_nlink(struct gfs2_inode *ip, int diff) |
| 672 | { |
| 673 | struct buffer_head *dibh; |
| 674 | uint32_t nlink; |
| 675 | int error; |
| 676 | |
| 677 | nlink = ip->i_di.di_nlink + diff; |
| 678 | |
| 679 | /* If we are reducing the nlink count, but the new value ends up being |
| 680 | bigger than the old one, we must have underflowed. */ |
| 681 | if (diff < 0 && nlink > ip->i_di.di_nlink) { |
| 682 | if (gfs2_consist_inode(ip)) |
| 683 | gfs2_dinode_print(&ip->i_di); |
| 684 | return -EIO; |
| 685 | } |
| 686 | |
| 687 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 688 | if (error) |
| 689 | return error; |
| 690 | |
| 691 | ip->i_di.di_nlink = nlink; |
| 692 | ip->i_di.di_ctime = get_seconds(); |
| 693 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 694 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 695 | gfs2_dinode_out(&ip->i_di, dibh->b_data); |
| 696 | brelse(dibh); |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * gfs2_lookupi - Look up a filename in a directory and return its inode |
| 703 | * @d_gh: An initialized holder for the directory glock |
| 704 | * @name: The name of the inode to look for |
| 705 | * @is_root: If 1, ignore the caller's permissions |
| 706 | * @i_gh: An uninitialized holder for the new inode glock |
| 707 | * |
| 708 | * There will always be a vnode (Linux VFS inode) for the d_gh inode unless |
| 709 | * @is_root is true. |
| 710 | * |
| 711 | * Returns: errno |
| 712 | */ |
| 713 | |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 714 | int gfs2_lookupi(struct inode *dir, struct qstr *name, int is_root, |
| 715 | struct inode **inodep) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 716 | { |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 717 | struct gfs2_inode *ipp; |
| 718 | struct gfs2_inode *dip = get_v2ip(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 719 | struct gfs2_sbd *sdp = dip->i_sbd; |
| 720 | struct gfs2_holder d_gh; |
| 721 | struct gfs2_inum inum; |
| 722 | unsigned int type; |
| 723 | struct gfs2_glock *gl; |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 724 | int error = 0; |
| 725 | |
| 726 | *inodep = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 727 | |
| 728 | if (!name->len || name->len > GFS2_FNAMESIZE) |
| 729 | return -ENAMETOOLONG; |
| 730 | |
| 731 | if (gfs2_filecmp(name, ".", 1) || |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 732 | (gfs2_filecmp(name, "..", 2) && dir == sdp->sd_root_dir)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 733 | gfs2_inode_hold(dip); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 734 | ipp = dip; |
| 735 | goto done; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh); |
| 739 | if (error) |
| 740 | return error; |
| 741 | |
| 742 | if (!is_root) { |
| 743 | error = gfs2_repermission(dip->i_vnode, MAY_EXEC, NULL); |
| 744 | if (error) |
| 745 | goto out; |
| 746 | } |
| 747 | |
| 748 | error = gfs2_dir_search(dip, name, &inum, &type); |
| 749 | if (error) |
| 750 | goto out; |
| 751 | |
| 752 | error = gfs2_glock_get(sdp, inum.no_addr, &gfs2_inode_glops, |
| 753 | CREATE, &gl); |
| 754 | if (error) |
| 755 | goto out; |
| 756 | |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 757 | error = gfs2_inode_get(gl, &inum, CREATE, &ipp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 758 | if (!error) |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 759 | gfs2_inode_min_init(ipp, type); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 760 | |
| 761 | gfs2_glock_put(gl); |
| 762 | |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 763 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 764 | gfs2_glock_dq_uninit(&d_gh); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 765 | done: |
| 766 | if (error == 0) { |
| 767 | *inodep = gfs2_ip2v(ipp); |
| 768 | if (!*inodep) |
| 769 | error = -ENOMEM; |
| 770 | gfs2_inode_put(ipp); |
| 771 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 772 | return error; |
| 773 | } |
| 774 | |
| 775 | static int pick_formal_ino_1(struct gfs2_sbd *sdp, uint64_t *formal_ino) |
| 776 | { |
Steven Whitehouse | f42faf4 | 2006-01-30 18:34:10 +0000 | [diff] [blame] | 777 | struct gfs2_inode *ip = get_v2ip(sdp->sd_ir_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 778 | struct buffer_head *bh; |
| 779 | struct gfs2_inum_range ir; |
| 780 | int error; |
| 781 | |
| 782 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); |
| 783 | if (error) |
| 784 | return error; |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 785 | mutex_lock(&sdp->sd_inum_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 786 | |
| 787 | error = gfs2_meta_inode_buffer(ip, &bh); |
| 788 | if (error) { |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 789 | mutex_unlock(&sdp->sd_inum_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 790 | gfs2_trans_end(sdp); |
| 791 | return error; |
| 792 | } |
| 793 | |
| 794 | gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode)); |
| 795 | |
| 796 | if (ir.ir_length) { |
| 797 | *formal_ino = ir.ir_start++; |
| 798 | ir.ir_length--; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 799 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 800 | gfs2_inum_range_out(&ir, |
| 801 | bh->b_data + sizeof(struct gfs2_dinode)); |
| 802 | brelse(bh); |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 803 | mutex_unlock(&sdp->sd_inum_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 804 | gfs2_trans_end(sdp); |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | brelse(bh); |
| 809 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 810 | mutex_unlock(&sdp->sd_inum_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 811 | gfs2_trans_end(sdp); |
| 812 | |
| 813 | return 1; |
| 814 | } |
| 815 | |
| 816 | static int pick_formal_ino_2(struct gfs2_sbd *sdp, uint64_t *formal_ino) |
| 817 | { |
Steven Whitehouse | f42faf4 | 2006-01-30 18:34:10 +0000 | [diff] [blame] | 818 | struct gfs2_inode *ip = get_v2ip(sdp->sd_ir_inode); |
| 819 | struct gfs2_inode *m_ip = get_v2ip(sdp->sd_inum_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 820 | struct gfs2_holder gh; |
| 821 | struct buffer_head *bh; |
| 822 | struct gfs2_inum_range ir; |
| 823 | int error; |
| 824 | |
| 825 | error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); |
| 826 | if (error) |
| 827 | return error; |
| 828 | |
| 829 | error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0); |
| 830 | if (error) |
| 831 | goto out; |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 832 | mutex_lock(&sdp->sd_inum_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 833 | |
| 834 | error = gfs2_meta_inode_buffer(ip, &bh); |
| 835 | if (error) |
| 836 | goto out_end_trans; |
| 837 | |
| 838 | gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode)); |
| 839 | |
| 840 | if (!ir.ir_length) { |
| 841 | struct buffer_head *m_bh; |
| 842 | uint64_t x, y; |
| 843 | |
| 844 | error = gfs2_meta_inode_buffer(m_ip, &m_bh); |
| 845 | if (error) |
| 846 | goto out_brelse; |
| 847 | |
| 848 | x = *(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode)); |
| 849 | x = y = be64_to_cpu(x); |
| 850 | ir.ir_start = x; |
| 851 | ir.ir_length = GFS2_INUM_QUANTUM; |
| 852 | x += GFS2_INUM_QUANTUM; |
| 853 | if (x < y) |
| 854 | gfs2_consist_inode(m_ip); |
| 855 | x = cpu_to_be64(x); |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 856 | gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 857 | *(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = x; |
| 858 | |
| 859 | brelse(m_bh); |
| 860 | } |
| 861 | |
| 862 | *formal_ino = ir.ir_start++; |
| 863 | ir.ir_length--; |
| 864 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 865 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 866 | gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode)); |
| 867 | |
| 868 | out_brelse: |
| 869 | brelse(bh); |
| 870 | |
| 871 | out_end_trans: |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 872 | mutex_unlock(&sdp->sd_inum_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 873 | gfs2_trans_end(sdp); |
| 874 | |
| 875 | out: |
| 876 | gfs2_glock_dq_uninit(&gh); |
| 877 | |
| 878 | return error; |
| 879 | } |
| 880 | |
| 881 | static int pick_formal_ino(struct gfs2_sbd *sdp, uint64_t *inum) |
| 882 | { |
| 883 | int error; |
| 884 | |
| 885 | error = pick_formal_ino_1(sdp, inum); |
| 886 | if (error <= 0) |
| 887 | return error; |
| 888 | |
| 889 | error = pick_formal_ino_2(sdp, inum); |
| 890 | |
| 891 | return error; |
| 892 | } |
| 893 | |
| 894 | /** |
| 895 | * create_ok - OK to create a new on-disk inode here? |
| 896 | * @dip: Directory in which dinode is to be created |
| 897 | * @name: Name of new dinode |
| 898 | * @mode: |
| 899 | * |
| 900 | * Returns: errno |
| 901 | */ |
| 902 | |
| 903 | static int create_ok(struct gfs2_inode *dip, struct qstr *name, |
| 904 | unsigned int mode) |
| 905 | { |
| 906 | int error; |
| 907 | |
| 908 | error = gfs2_repermission(dip->i_vnode, MAY_WRITE | MAY_EXEC, NULL); |
| 909 | if (error) |
| 910 | return error; |
| 911 | |
| 912 | /* Don't create entries in an unlinked directory */ |
| 913 | if (!dip->i_di.di_nlink) |
| 914 | return -EPERM; |
| 915 | |
| 916 | error = gfs2_dir_search(dip, name, NULL, NULL); |
| 917 | switch (error) { |
| 918 | case -ENOENT: |
| 919 | error = 0; |
| 920 | break; |
| 921 | case 0: |
| 922 | return -EEXIST; |
| 923 | default: |
| 924 | return error; |
| 925 | } |
| 926 | |
| 927 | if (dip->i_di.di_entries == (uint32_t)-1) |
| 928 | return -EFBIG; |
| 929 | if (S_ISDIR(mode) && dip->i_di.di_nlink == (uint32_t)-1) |
| 930 | return -EMLINK; |
| 931 | |
| 932 | return 0; |
| 933 | } |
| 934 | |
| 935 | static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode, |
| 936 | unsigned int *uid, unsigned int *gid) |
| 937 | { |
| 938 | if (dip->i_sbd->sd_args.ar_suiddir && |
| 939 | (dip->i_di.di_mode & S_ISUID) && |
| 940 | dip->i_di.di_uid) { |
| 941 | if (S_ISDIR(*mode)) |
| 942 | *mode |= S_ISUID; |
| 943 | else if (dip->i_di.di_uid != current->fsuid) |
| 944 | *mode &= ~07111; |
| 945 | *uid = dip->i_di.di_uid; |
| 946 | } else |
| 947 | *uid = current->fsuid; |
| 948 | |
| 949 | if (dip->i_di.di_mode & S_ISGID) { |
| 950 | if (S_ISDIR(*mode)) |
| 951 | *mode |= S_ISGID; |
| 952 | *gid = dip->i_di.di_gid; |
| 953 | } else |
| 954 | *gid = current->fsgid; |
| 955 | } |
| 956 | |
| 957 | static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_unlinked *ul) |
| 958 | { |
| 959 | struct gfs2_sbd *sdp = dip->i_sbd; |
| 960 | int error; |
| 961 | |
| 962 | gfs2_alloc_get(dip); |
| 963 | |
| 964 | dip->i_alloc.al_requested = RES_DINODE; |
| 965 | error = gfs2_inplace_reserve(dip); |
| 966 | if (error) |
| 967 | goto out; |
| 968 | |
| 969 | error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED + |
| 970 | RES_STATFS, 0); |
| 971 | if (error) |
| 972 | goto out_ipreserv; |
| 973 | |
| 974 | ul->ul_ut.ut_inum.no_addr = gfs2_alloc_di(dip); |
| 975 | |
| 976 | ul->ul_ut.ut_flags = GFS2_UTF_UNINIT; |
| 977 | error = gfs2_unlinked_ondisk_add(sdp, ul); |
| 978 | |
| 979 | gfs2_trans_end(sdp); |
| 980 | |
| 981 | out_ipreserv: |
| 982 | gfs2_inplace_release(dip); |
| 983 | |
| 984 | out: |
| 985 | gfs2_alloc_put(dip); |
| 986 | |
| 987 | return error; |
| 988 | } |
| 989 | |
| 990 | /** |
| 991 | * init_dinode - Fill in a new dinode structure |
| 992 | * @dip: the directory this inode is being created in |
| 993 | * @gl: The glock covering the new inode |
| 994 | * @inum: the inode number |
| 995 | * @mode: the file permissions |
| 996 | * @uid: |
| 997 | * @gid: |
| 998 | * |
| 999 | */ |
| 1000 | |
| 1001 | static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl, |
| 1002 | struct gfs2_inum *inum, unsigned int mode, |
| 1003 | unsigned int uid, unsigned int gid) |
| 1004 | { |
| 1005 | struct gfs2_sbd *sdp = dip->i_sbd; |
Steven Whitehouse | b96ca4f | 2006-01-18 10:57:10 +0000 | [diff] [blame] | 1006 | struct gfs2_dinode *di; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1007 | struct buffer_head *dibh; |
| 1008 | |
| 1009 | dibh = gfs2_meta_new(gl, inum->no_addr); |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1010 | gfs2_trans_add_bh(gl, dibh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1011 | gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI); |
| 1012 | gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); |
Steven Whitehouse | b96ca4f | 2006-01-18 10:57:10 +0000 | [diff] [blame] | 1013 | di = (struct gfs2_dinode *)dibh->b_data; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1014 | |
Steven Whitehouse | 2442a09 | 2006-01-30 11:49:32 +0000 | [diff] [blame] | 1015 | di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino); |
| 1016 | di->di_num.no_addr = cpu_to_be64(inum->no_addr); |
Steven Whitehouse | b96ca4f | 2006-01-18 10:57:10 +0000 | [diff] [blame] | 1017 | di->di_mode = cpu_to_be32(mode); |
| 1018 | di->di_uid = cpu_to_be32(uid); |
| 1019 | di->di_gid = cpu_to_be32(gid); |
| 1020 | di->di_nlink = cpu_to_be32(0); |
| 1021 | di->di_size = cpu_to_be64(0); |
| 1022 | di->di_blocks = cpu_to_be64(1); |
| 1023 | di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds()); |
| 1024 | di->di_major = di->di_minor = cpu_to_be32(0); |
| 1025 | di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr); |
| 1026 | di->__pad[0] = di->__pad[1] = 0; |
| 1027 | di->di_flags = cpu_to_be32(0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1028 | |
| 1029 | if (S_ISREG(mode)) { |
| 1030 | if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) || |
| 1031 | gfs2_tune_get(sdp, gt_new_files_jdata)) |
Steven Whitehouse | b96ca4f | 2006-01-18 10:57:10 +0000 | [diff] [blame] | 1032 | di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1033 | if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) || |
| 1034 | gfs2_tune_get(sdp, gt_new_files_directio)) |
Steven Whitehouse | b96ca4f | 2006-01-18 10:57:10 +0000 | [diff] [blame] | 1035 | di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1036 | } else if (S_ISDIR(mode)) { |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame^] | 1037 | di->di_flags |= cpu_to_be32(dip->i_di.di_flags & |
| 1038 | GFS2_DIF_INHERIT_DIRECTIO); |
| 1039 | di->di_flags |= cpu_to_be32(dip->i_di.di_flags & |
| 1040 | GFS2_DIF_INHERIT_JDATA); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Steven Whitehouse | b96ca4f | 2006-01-18 10:57:10 +0000 | [diff] [blame] | 1043 | di->__pad1 = 0; |
| 1044 | di->di_height = cpu_to_be32(0); |
| 1045 | di->__pad2 = 0; |
| 1046 | di->__pad3 = 0; |
| 1047 | di->di_depth = cpu_to_be16(0); |
| 1048 | di->di_entries = cpu_to_be32(0); |
| 1049 | memset(&di->__pad4, 0, sizeof(di->__pad4)); |
| 1050 | di->di_eattr = cpu_to_be64(0); |
| 1051 | memset(&di->di_reserved, 0, sizeof(di->di_reserved)); |
| 1052 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1053 | brelse(dibh); |
| 1054 | } |
| 1055 | |
| 1056 | static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl, |
| 1057 | unsigned int mode, struct gfs2_unlinked *ul) |
| 1058 | { |
| 1059 | struct gfs2_sbd *sdp = dip->i_sbd; |
| 1060 | unsigned int uid, gid; |
| 1061 | int error; |
| 1062 | |
| 1063 | munge_mode_uid_gid(dip, &mode, &uid, &gid); |
| 1064 | |
| 1065 | gfs2_alloc_get(dip); |
| 1066 | |
| 1067 | error = gfs2_quota_lock(dip, uid, gid); |
| 1068 | if (error) |
| 1069 | goto out; |
| 1070 | |
| 1071 | error = gfs2_quota_check(dip, uid, gid); |
| 1072 | if (error) |
| 1073 | goto out_quota; |
| 1074 | |
| 1075 | error = gfs2_trans_begin(sdp, RES_DINODE + RES_UNLINKED + |
| 1076 | RES_QUOTA, 0); |
| 1077 | if (error) |
| 1078 | goto out_quota; |
| 1079 | |
| 1080 | ul->ul_ut.ut_flags = 0; |
| 1081 | error = gfs2_unlinked_ondisk_munge(sdp, ul); |
| 1082 | |
| 1083 | init_dinode(dip, gl, &ul->ul_ut.ut_inum, |
| 1084 | mode, uid, gid); |
| 1085 | |
| 1086 | gfs2_quota_change(dip, +1, uid, gid); |
| 1087 | |
| 1088 | gfs2_trans_end(sdp); |
| 1089 | |
| 1090 | out_quota: |
| 1091 | gfs2_quota_unlock(dip); |
| 1092 | |
| 1093 | out: |
| 1094 | gfs2_alloc_put(dip); |
| 1095 | |
| 1096 | return error; |
| 1097 | } |
| 1098 | |
| 1099 | static int link_dinode(struct gfs2_inode *dip, struct qstr *name, |
| 1100 | struct gfs2_inode *ip, struct gfs2_unlinked *ul) |
| 1101 | { |
| 1102 | struct gfs2_sbd *sdp = dip->i_sbd; |
| 1103 | struct gfs2_alloc *al; |
| 1104 | int alloc_required; |
| 1105 | struct buffer_head *dibh; |
| 1106 | int error; |
| 1107 | |
| 1108 | al = gfs2_alloc_get(dip); |
| 1109 | |
| 1110 | error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); |
| 1111 | if (error) |
| 1112 | goto fail; |
| 1113 | |
| 1114 | error = gfs2_diradd_alloc_required(dip, name, &alloc_required); |
| 1115 | if (alloc_required) { |
| 1116 | error = gfs2_quota_check(dip, dip->i_di.di_uid, |
| 1117 | dip->i_di.di_gid); |
| 1118 | if (error) |
| 1119 | goto fail_quota_locks; |
| 1120 | |
| 1121 | al->al_requested = sdp->sd_max_dirres; |
| 1122 | |
| 1123 | error = gfs2_inplace_reserve(dip); |
| 1124 | if (error) |
| 1125 | goto fail_quota_locks; |
| 1126 | |
| 1127 | error = gfs2_trans_begin(sdp, |
| 1128 | sdp->sd_max_dirres + |
| 1129 | al->al_rgd->rd_ri.ri_length + |
| 1130 | 2 * RES_DINODE + RES_UNLINKED + |
| 1131 | RES_STATFS + RES_QUOTA, 0); |
| 1132 | if (error) |
| 1133 | goto fail_ipreserv; |
| 1134 | } else { |
| 1135 | error = gfs2_trans_begin(sdp, |
| 1136 | RES_LEAF + |
| 1137 | 2 * RES_DINODE + |
| 1138 | RES_UNLINKED, 0); |
| 1139 | if (error) |
| 1140 | goto fail_quota_locks; |
| 1141 | } |
| 1142 | |
| 1143 | error = gfs2_dir_add(dip, name, &ip->i_num, IF2DT(ip->i_di.di_mode)); |
| 1144 | if (error) |
| 1145 | goto fail_end_trans; |
| 1146 | |
| 1147 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1148 | if (error) |
| 1149 | goto fail_end_trans; |
| 1150 | ip->i_di.di_nlink = 1; |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1151 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1152 | gfs2_dinode_out(&ip->i_di, dibh->b_data); |
| 1153 | brelse(dibh); |
| 1154 | |
| 1155 | error = gfs2_unlinked_ondisk_rm(sdp, ul); |
| 1156 | if (error) |
| 1157 | goto fail_end_trans; |
| 1158 | |
| 1159 | return 0; |
| 1160 | |
| 1161 | fail_end_trans: |
| 1162 | gfs2_trans_end(sdp); |
| 1163 | |
| 1164 | fail_ipreserv: |
| 1165 | if (dip->i_alloc.al_rgd) |
| 1166 | gfs2_inplace_release(dip); |
| 1167 | |
| 1168 | fail_quota_locks: |
| 1169 | gfs2_quota_unlock(dip); |
| 1170 | |
| 1171 | fail: |
| 1172 | gfs2_alloc_put(dip); |
| 1173 | |
| 1174 | return error; |
| 1175 | } |
| 1176 | |
| 1177 | /** |
| 1178 | * gfs2_createi - Create a new inode |
| 1179 | * @ghs: An array of two holders |
| 1180 | * @name: The name of the new file |
| 1181 | * @mode: the permissions on the new inode |
| 1182 | * |
| 1183 | * @ghs[0] is an initialized holder for the directory |
| 1184 | * @ghs[1] is the holder for the inode lock |
| 1185 | * |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1186 | * If the return value is not NULL, the glocks on both the directory and the new |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1187 | * file are held. A transaction has been started and an inplace reservation |
| 1188 | * is held, as well. |
| 1189 | * |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1190 | * Returns: An inode |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1191 | */ |
| 1192 | |
Steven Whitehouse | 568f4c9 | 2006-02-27 12:00:42 -0500 | [diff] [blame^] | 1193 | struct inode *gfs2_createi(struct gfs2_holder *ghs, struct qstr *name, |
| 1194 | unsigned int mode) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1195 | { |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1196 | struct inode *inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1197 | struct gfs2_inode *dip = get_gl2ip(ghs->gh_gl); |
| 1198 | struct gfs2_sbd *sdp = dip->i_sbd; |
| 1199 | struct gfs2_unlinked *ul; |
| 1200 | struct gfs2_inode *ip; |
| 1201 | int error; |
| 1202 | |
| 1203 | if (!name->len || name->len > GFS2_FNAMESIZE) |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1204 | return ERR_PTR(-ENAMETOOLONG); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1205 | |
| 1206 | error = gfs2_unlinked_get(sdp, &ul); |
| 1207 | if (error) |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1208 | return ERR_PTR(error); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1209 | |
| 1210 | gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs); |
| 1211 | error = gfs2_glock_nq(ghs); |
| 1212 | if (error) |
| 1213 | goto fail; |
| 1214 | |
| 1215 | error = create_ok(dip, name, mode); |
| 1216 | if (error) |
| 1217 | goto fail_gunlock; |
| 1218 | |
| 1219 | error = pick_formal_ino(sdp, &ul->ul_ut.ut_inum.no_formal_ino); |
| 1220 | if (error) |
| 1221 | goto fail_gunlock; |
| 1222 | |
| 1223 | error = alloc_dinode(dip, ul); |
| 1224 | if (error) |
| 1225 | goto fail_gunlock; |
| 1226 | |
| 1227 | if (ul->ul_ut.ut_inum.no_addr < dip->i_num.no_addr) { |
| 1228 | gfs2_glock_dq(ghs); |
| 1229 | |
| 1230 | error = gfs2_glock_nq_num(sdp, |
| 1231 | ul->ul_ut.ut_inum.no_addr, |
| 1232 | &gfs2_inode_glops, |
| 1233 | LM_ST_EXCLUSIVE, GL_SKIP, |
| 1234 | ghs + 1); |
| 1235 | if (error) { |
| 1236 | gfs2_unlinked_put(sdp, ul); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1237 | return ERR_PTR(error); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs); |
| 1241 | error = gfs2_glock_nq(ghs); |
| 1242 | if (error) { |
| 1243 | gfs2_glock_dq_uninit(ghs + 1); |
| 1244 | gfs2_unlinked_put(sdp, ul); |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1245 | return ERR_PTR(error); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | error = create_ok(dip, name, mode); |
| 1249 | if (error) |
| 1250 | goto fail_gunlock2; |
| 1251 | } else { |
| 1252 | error = gfs2_glock_nq_num(sdp, |
| 1253 | ul->ul_ut.ut_inum.no_addr, |
| 1254 | &gfs2_inode_glops, |
| 1255 | LM_ST_EXCLUSIVE, GL_SKIP, |
| 1256 | ghs + 1); |
| 1257 | if (error) |
| 1258 | goto fail_gunlock; |
| 1259 | } |
| 1260 | |
| 1261 | error = make_dinode(dip, ghs[1].gh_gl, mode, ul); |
| 1262 | if (error) |
| 1263 | goto fail_gunlock2; |
| 1264 | |
| 1265 | error = gfs2_inode_get(ghs[1].gh_gl, &ul->ul_ut.ut_inum, CREATE, &ip); |
| 1266 | if (error) |
| 1267 | goto fail_gunlock2; |
| 1268 | |
| 1269 | error = gfs2_inode_refresh(ip); |
| 1270 | if (error) |
| 1271 | goto fail_iput; |
| 1272 | |
| 1273 | error = gfs2_acl_create(dip, ip); |
| 1274 | if (error) |
| 1275 | goto fail_iput; |
| 1276 | |
| 1277 | error = link_dinode(dip, name, ip, ul); |
| 1278 | if (error) |
| 1279 | goto fail_iput; |
| 1280 | |
| 1281 | gfs2_unlinked_put(sdp, ul); |
| 1282 | |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1283 | inode = gfs2_ip2v(ip); |
| 1284 | gfs2_inode_put(ip); |
| 1285 | if (!inode) |
| 1286 | return ERR_PTR(-ENOMEM); |
| 1287 | return inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1288 | |
| 1289 | fail_iput: |
| 1290 | gfs2_inode_put(ip); |
| 1291 | |
| 1292 | fail_gunlock2: |
| 1293 | gfs2_glock_dq_uninit(ghs + 1); |
| 1294 | |
| 1295 | fail_gunlock: |
| 1296 | gfs2_glock_dq(ghs); |
| 1297 | |
| 1298 | fail: |
| 1299 | gfs2_unlinked_put(sdp, ul); |
| 1300 | |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1301 | return ERR_PTR(error); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | /** |
| 1305 | * gfs2_unlinki - Unlink a file |
| 1306 | * @dip: The inode of the directory |
| 1307 | * @name: The name of the file to be unlinked |
| 1308 | * @ip: The inode of the file to be removed |
| 1309 | * |
| 1310 | * Assumes Glocks on both dip and ip are held. |
| 1311 | * |
| 1312 | * Returns: errno |
| 1313 | */ |
| 1314 | |
| 1315 | int gfs2_unlinki(struct gfs2_inode *dip, struct qstr *name, |
| 1316 | struct gfs2_inode *ip, struct gfs2_unlinked *ul) |
| 1317 | { |
| 1318 | struct gfs2_sbd *sdp = dip->i_sbd; |
| 1319 | int error; |
| 1320 | |
| 1321 | error = gfs2_dir_del(dip, name); |
| 1322 | if (error) |
| 1323 | return error; |
| 1324 | |
| 1325 | error = gfs2_change_nlink(ip, -1); |
| 1326 | if (error) |
| 1327 | return error; |
| 1328 | |
| 1329 | /* If this inode is being unlinked from the directory structure, |
| 1330 | we need to mark that in the log so that it isn't lost during |
| 1331 | a crash. */ |
| 1332 | |
| 1333 | if (!ip->i_di.di_nlink) { |
| 1334 | ul->ul_ut.ut_inum = ip->i_num; |
| 1335 | error = gfs2_unlinked_ondisk_add(sdp, ul); |
| 1336 | if (!error) |
| 1337 | set_bit(GLF_STICKY, &ip->i_gl->gl_flags); |
| 1338 | } |
| 1339 | |
| 1340 | return error; |
| 1341 | } |
| 1342 | |
| 1343 | /** |
| 1344 | * gfs2_rmdiri - Remove a directory |
| 1345 | * @dip: The parent directory of the directory to be removed |
| 1346 | * @name: The name of the directory to be removed |
| 1347 | * @ip: The GFS2 inode of the directory to be removed |
| 1348 | * |
| 1349 | * Assumes Glocks on dip and ip are held |
| 1350 | * |
| 1351 | * Returns: errno |
| 1352 | */ |
| 1353 | |
| 1354 | int gfs2_rmdiri(struct gfs2_inode *dip, struct qstr *name, |
| 1355 | struct gfs2_inode *ip, struct gfs2_unlinked *ul) |
| 1356 | { |
| 1357 | struct gfs2_sbd *sdp = dip->i_sbd; |
| 1358 | struct qstr dotname; |
| 1359 | int error; |
| 1360 | |
| 1361 | if (ip->i_di.di_entries != 2) { |
| 1362 | if (gfs2_consist_inode(ip)) |
| 1363 | gfs2_dinode_print(&ip->i_di); |
| 1364 | return -EIO; |
| 1365 | } |
| 1366 | |
| 1367 | error = gfs2_dir_del(dip, name); |
| 1368 | if (error) |
| 1369 | return error; |
| 1370 | |
| 1371 | error = gfs2_change_nlink(dip, -1); |
| 1372 | if (error) |
| 1373 | return error; |
| 1374 | |
| 1375 | dotname.len = 1; |
| 1376 | dotname.name = "."; |
| 1377 | error = gfs2_dir_del(ip, &dotname); |
| 1378 | if (error) |
| 1379 | return error; |
| 1380 | |
| 1381 | dotname.len = 2; |
| 1382 | dotname.name = ".."; |
| 1383 | error = gfs2_dir_del(ip, &dotname); |
| 1384 | if (error) |
| 1385 | return error; |
| 1386 | |
| 1387 | error = gfs2_change_nlink(ip, -2); |
| 1388 | if (error) |
| 1389 | return error; |
| 1390 | |
| 1391 | /* This inode is being unlinked from the directory structure and |
| 1392 | we need to mark that in the log so that it isn't lost during |
| 1393 | a crash. */ |
| 1394 | |
| 1395 | ul->ul_ut.ut_inum = ip->i_num; |
| 1396 | error = gfs2_unlinked_ondisk_add(sdp, ul); |
| 1397 | if (!error) |
| 1398 | set_bit(GLF_STICKY, &ip->i_gl->gl_flags); |
| 1399 | |
| 1400 | return error; |
| 1401 | } |
| 1402 | |
| 1403 | /* |
| 1404 | * gfs2_unlink_ok - check to see that a inode is still in a directory |
| 1405 | * @dip: the directory |
| 1406 | * @name: the name of the file |
| 1407 | * @ip: the inode |
| 1408 | * |
| 1409 | * Assumes that the lock on (at least) @dip is held. |
| 1410 | * |
| 1411 | * Returns: 0 if the parent/child relationship is correct, errno if it isn't |
| 1412 | */ |
| 1413 | |
| 1414 | int gfs2_unlink_ok(struct gfs2_inode *dip, struct qstr *name, |
| 1415 | struct gfs2_inode *ip) |
| 1416 | { |
| 1417 | struct gfs2_inum inum; |
| 1418 | unsigned int type; |
| 1419 | int error; |
| 1420 | |
| 1421 | if (IS_IMMUTABLE(ip->i_vnode) || IS_APPEND(ip->i_vnode)) |
| 1422 | return -EPERM; |
| 1423 | |
| 1424 | if ((dip->i_di.di_mode & S_ISVTX) && |
| 1425 | dip->i_di.di_uid != current->fsuid && |
| 1426 | ip->i_di.di_uid != current->fsuid && |
| 1427 | !capable(CAP_FOWNER)) |
| 1428 | return -EPERM; |
| 1429 | |
| 1430 | if (IS_APPEND(dip->i_vnode)) |
| 1431 | return -EPERM; |
| 1432 | |
| 1433 | error = gfs2_repermission(dip->i_vnode, MAY_WRITE | MAY_EXEC, NULL); |
| 1434 | if (error) |
| 1435 | return error; |
| 1436 | |
| 1437 | error = gfs2_dir_search(dip, name, &inum, &type); |
| 1438 | if (error) |
| 1439 | return error; |
| 1440 | |
| 1441 | if (!gfs2_inum_equal(&inum, &ip->i_num)) |
| 1442 | return -ENOENT; |
| 1443 | |
| 1444 | if (IF2DT(ip->i_di.di_mode) != type) { |
| 1445 | gfs2_consist_inode(dip); |
| 1446 | return -EIO; |
| 1447 | } |
| 1448 | |
| 1449 | return 0; |
| 1450 | } |
| 1451 | |
| 1452 | /* |
| 1453 | * gfs2_ok_to_move - check if it's ok to move a directory to another directory |
| 1454 | * @this: move this |
| 1455 | * @to: to here |
| 1456 | * |
| 1457 | * Follow @to back to the root and make sure we don't encounter @this |
| 1458 | * Assumes we already hold the rename lock. |
| 1459 | * |
| 1460 | * Returns: errno |
| 1461 | */ |
| 1462 | |
| 1463 | int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to) |
| 1464 | { |
| 1465 | struct gfs2_sbd *sdp = this->i_sbd; |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1466 | struct inode *dir = to->i_vnode; |
| 1467 | struct inode *tmp; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1468 | struct qstr dotdot; |
| 1469 | int error = 0; |
| 1470 | |
| 1471 | memset(&dotdot, 0, sizeof(struct qstr)); |
| 1472 | dotdot.name = ".."; |
| 1473 | dotdot.len = 2; |
| 1474 | |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1475 | igrab(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1476 | |
| 1477 | for (;;) { |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1478 | if (dir == this->i_vnode) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1479 | error = -EINVAL; |
| 1480 | break; |
| 1481 | } |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1482 | if (dir == sdp->sd_root_dir) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1483 | error = 0; |
| 1484 | break; |
| 1485 | } |
| 1486 | |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1487 | error = gfs2_lookupi(dir, &dotdot, 1, &tmp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1488 | if (error) |
| 1489 | break; |
| 1490 | |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1491 | iput(dir); |
| 1492 | dir = tmp; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Steven Whitehouse | 7359a19 | 2006-02-13 12:27:43 +0000 | [diff] [blame] | 1495 | iput(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1496 | |
| 1497 | return error; |
| 1498 | } |
| 1499 | |
| 1500 | /** |
| 1501 | * gfs2_readlinki - return the contents of a symlink |
| 1502 | * @ip: the symlink's inode |
| 1503 | * @buf: a pointer to the buffer to be filled |
| 1504 | * @len: a pointer to the length of @buf |
| 1505 | * |
| 1506 | * If @buf is too small, a piece of memory is kmalloc()ed and needs |
| 1507 | * to be freed by the caller. |
| 1508 | * |
| 1509 | * Returns: errno |
| 1510 | */ |
| 1511 | |
| 1512 | int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len) |
| 1513 | { |
| 1514 | struct gfs2_holder i_gh; |
| 1515 | struct buffer_head *dibh; |
| 1516 | unsigned int x; |
| 1517 | int error; |
| 1518 | |
| 1519 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh); |
| 1520 | error = gfs2_glock_nq_atime(&i_gh); |
| 1521 | if (error) { |
| 1522 | gfs2_holder_uninit(&i_gh); |
| 1523 | return error; |
| 1524 | } |
| 1525 | |
| 1526 | if (!ip->i_di.di_size) { |
| 1527 | gfs2_consist_inode(ip); |
| 1528 | error = -EIO; |
| 1529 | goto out; |
| 1530 | } |
| 1531 | |
| 1532 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1533 | if (error) |
| 1534 | goto out; |
| 1535 | |
| 1536 | x = ip->i_di.di_size + 1; |
| 1537 | if (x > *len) { |
| 1538 | *buf = kmalloc(x, GFP_KERNEL); |
| 1539 | if (!*buf) { |
| 1540 | error = -ENOMEM; |
| 1541 | goto out_brelse; |
| 1542 | } |
| 1543 | } |
| 1544 | |
| 1545 | memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x); |
| 1546 | *len = x; |
| 1547 | |
| 1548 | out_brelse: |
| 1549 | brelse(dibh); |
| 1550 | |
| 1551 | out: |
| 1552 | gfs2_glock_dq_uninit(&i_gh); |
| 1553 | |
| 1554 | return error; |
| 1555 | } |
| 1556 | |
| 1557 | /** |
| 1558 | * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and |
| 1559 | * conditionally update the inode's atime |
| 1560 | * @gh: the holder to acquire |
| 1561 | * |
| 1562 | * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap |
| 1563 | * Update if the difference between the current time and the inode's current |
| 1564 | * atime is greater than an interval specified at mount. |
| 1565 | * |
| 1566 | * Returns: errno |
| 1567 | */ |
| 1568 | |
| 1569 | int gfs2_glock_nq_atime(struct gfs2_holder *gh) |
| 1570 | { |
| 1571 | struct gfs2_glock *gl = gh->gh_gl; |
| 1572 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 1573 | struct gfs2_inode *ip = get_gl2ip(gl); |
| 1574 | int64_t curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum); |
| 1575 | unsigned int state; |
| 1576 | int flags; |
| 1577 | int error; |
| 1578 | |
| 1579 | if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) || |
| 1580 | gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) || |
| 1581 | gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops)) |
| 1582 | return -EINVAL; |
| 1583 | |
| 1584 | state = gh->gh_state; |
| 1585 | flags = gh->gh_flags; |
| 1586 | |
| 1587 | error = gfs2_glock_nq(gh); |
| 1588 | if (error) |
| 1589 | return error; |
| 1590 | |
| 1591 | if (test_bit(SDF_NOATIME, &sdp->sd_flags) || |
| 1592 | (sdp->sd_vfs->s_flags & MS_RDONLY)) |
| 1593 | return 0; |
| 1594 | |
| 1595 | curtime = get_seconds(); |
| 1596 | if (curtime - ip->i_di.di_atime >= quantum) { |
| 1597 | gfs2_glock_dq(gh); |
| 1598 | gfs2_holder_reinit(LM_ST_EXCLUSIVE, |
| 1599 | gh->gh_flags & ~LM_FLAG_ANY, |
| 1600 | gh); |
| 1601 | error = gfs2_glock_nq(gh); |
| 1602 | if (error) |
| 1603 | return error; |
| 1604 | |
| 1605 | /* Verify that atime hasn't been updated while we were |
| 1606 | trying to get exclusive lock. */ |
| 1607 | |
| 1608 | curtime = get_seconds(); |
| 1609 | if (curtime - ip->i_di.di_atime >= quantum) { |
| 1610 | struct buffer_head *dibh; |
| 1611 | |
| 1612 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); |
| 1613 | if (error == -EROFS) |
| 1614 | return 0; |
| 1615 | if (error) |
| 1616 | goto fail; |
| 1617 | |
| 1618 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1619 | if (error) |
| 1620 | goto fail_end_trans; |
| 1621 | |
| 1622 | ip->i_di.di_atime = curtime; |
| 1623 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1624 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1625 | gfs2_dinode_out(&ip->i_di, dibh->b_data); |
| 1626 | brelse(dibh); |
| 1627 | |
| 1628 | gfs2_trans_end(sdp); |
| 1629 | } |
| 1630 | |
| 1631 | /* If someone else has asked for the glock, |
| 1632 | unlock and let them have it. Then reacquire |
| 1633 | in the original state. */ |
| 1634 | if (gfs2_glock_is_blocking(gl)) { |
| 1635 | gfs2_glock_dq(gh); |
| 1636 | gfs2_holder_reinit(state, flags, gh); |
| 1637 | return gfs2_glock_nq(gh); |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | return 0; |
| 1642 | |
| 1643 | fail_end_trans: |
| 1644 | gfs2_trans_end(sdp); |
| 1645 | |
| 1646 | fail: |
| 1647 | gfs2_glock_dq(gh); |
| 1648 | |
| 1649 | return error; |
| 1650 | } |
| 1651 | |
| 1652 | /** |
| 1653 | * glock_compare_atime - Compare two struct gfs2_glock structures for sort |
| 1654 | * @arg_a: the first structure |
| 1655 | * @arg_b: the second structure |
| 1656 | * |
| 1657 | * Returns: 1 if A > B |
| 1658 | * -1 if A < B |
| 1659 | * 0 if A = B |
| 1660 | */ |
| 1661 | |
| 1662 | static int glock_compare_atime(const void *arg_a, const void *arg_b) |
| 1663 | { |
| 1664 | struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a; |
| 1665 | struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b; |
| 1666 | struct lm_lockname *a = &gh_a->gh_gl->gl_name; |
| 1667 | struct lm_lockname *b = &gh_b->gh_gl->gl_name; |
| 1668 | int ret = 0; |
| 1669 | |
| 1670 | if (a->ln_number > b->ln_number) |
| 1671 | ret = 1; |
| 1672 | else if (a->ln_number < b->ln_number) |
| 1673 | ret = -1; |
| 1674 | else { |
| 1675 | if (gh_a->gh_state == LM_ST_SHARED && |
| 1676 | gh_b->gh_state == LM_ST_EXCLUSIVE) |
| 1677 | ret = 1; |
| 1678 | else if (gh_a->gh_state == LM_ST_SHARED && |
| 1679 | (gh_b->gh_flags & GL_ATIME)) |
| 1680 | ret = 1; |
| 1681 | } |
| 1682 | |
| 1683 | return ret; |
| 1684 | } |
| 1685 | |
| 1686 | /** |
| 1687 | * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an |
| 1688 | * atime update |
| 1689 | * @num_gh: the number of structures |
| 1690 | * @ghs: an array of struct gfs2_holder structures |
| 1691 | * |
| 1692 | * Returns: 0 on success (all glocks acquired), |
| 1693 | * errno on failure (no glocks acquired) |
| 1694 | */ |
| 1695 | |
| 1696 | int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs) |
| 1697 | { |
| 1698 | struct gfs2_holder **p; |
| 1699 | unsigned int x; |
| 1700 | int error = 0; |
| 1701 | |
| 1702 | if (!num_gh) |
| 1703 | return 0; |
| 1704 | |
| 1705 | if (num_gh == 1) { |
| 1706 | ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC); |
| 1707 | if (ghs->gh_flags & GL_ATIME) |
| 1708 | error = gfs2_glock_nq_atime(ghs); |
| 1709 | else |
| 1710 | error = gfs2_glock_nq(ghs); |
| 1711 | return error; |
| 1712 | } |
| 1713 | |
| 1714 | p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL); |
| 1715 | if (!p) |
| 1716 | return -ENOMEM; |
| 1717 | |
| 1718 | for (x = 0; x < num_gh; x++) |
| 1719 | p[x] = &ghs[x]; |
| 1720 | |
| 1721 | sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL); |
| 1722 | |
| 1723 | for (x = 0; x < num_gh; x++) { |
| 1724 | p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC); |
| 1725 | |
| 1726 | if (p[x]->gh_flags & GL_ATIME) |
| 1727 | error = gfs2_glock_nq_atime(p[x]); |
| 1728 | else |
| 1729 | error = gfs2_glock_nq(p[x]); |
| 1730 | |
| 1731 | if (error) { |
| 1732 | while (x--) |
| 1733 | gfs2_glock_dq(p[x]); |
| 1734 | break; |
| 1735 | } |
| 1736 | } |
| 1737 | |
| 1738 | kfree(p); |
| 1739 | |
| 1740 | return error; |
| 1741 | } |
| 1742 | |
| 1743 | /** |
| 1744 | * gfs2_try_toss_vnode - See if we can toss a vnode from memory |
| 1745 | * @ip: the inode |
| 1746 | * |
| 1747 | * Returns: 1 if the vnode was tossed |
| 1748 | */ |
| 1749 | |
| 1750 | void gfs2_try_toss_vnode(struct gfs2_inode *ip) |
| 1751 | { |
| 1752 | struct inode *inode; |
| 1753 | |
| 1754 | inode = gfs2_ip2v_lookup(ip); |
| 1755 | if (!inode) |
| 1756 | return; |
| 1757 | |
| 1758 | d_prune_aliases(inode); |
| 1759 | |
| 1760 | if (S_ISDIR(ip->i_di.di_mode)) { |
| 1761 | struct list_head *head = &inode->i_dentry; |
| 1762 | struct dentry *d = NULL; |
| 1763 | |
| 1764 | spin_lock(&dcache_lock); |
| 1765 | if (list_empty(head)) |
| 1766 | spin_unlock(&dcache_lock); |
| 1767 | else { |
| 1768 | d = list_entry(head->next, struct dentry, d_alias); |
| 1769 | dget_locked(d); |
| 1770 | spin_unlock(&dcache_lock); |
| 1771 | |
| 1772 | if (have_submounts(d)) |
| 1773 | dput(d); |
| 1774 | else { |
| 1775 | shrink_dcache_parent(d); |
| 1776 | dput(d); |
| 1777 | d_prune_aliases(inode); |
| 1778 | } |
| 1779 | } |
| 1780 | } |
| 1781 | |
| 1782 | inode->i_nlink = 0; |
| 1783 | iput(inode); |
| 1784 | } |
| 1785 | |
| 1786 | |
| 1787 | static int |
| 1788 | __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr) |
| 1789 | { |
| 1790 | struct buffer_head *dibh; |
| 1791 | int error; |
| 1792 | |
| 1793 | error = gfs2_meta_inode_buffer(ip, &dibh); |
| 1794 | if (!error) { |
| 1795 | error = inode_setattr(ip->i_vnode, attr); |
| 1796 | gfs2_assert_warn(ip->i_sbd, !error); |
| 1797 | gfs2_inode_attr_out(ip); |
| 1798 | |
Steven Whitehouse | d4e9c4c | 2006-01-18 11:19:28 +0000 | [diff] [blame] | 1799 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1800 | gfs2_dinode_out(&ip->i_di, dibh->b_data); |
| 1801 | brelse(dibh); |
| 1802 | } |
| 1803 | return error; |
| 1804 | } |
| 1805 | |
| 1806 | /** |
| 1807 | * gfs2_setattr_simple - |
| 1808 | * @ip: |
| 1809 | * @attr: |
| 1810 | * |
| 1811 | * Called with a reference on the vnode. |
| 1812 | * |
| 1813 | * Returns: errno |
| 1814 | */ |
| 1815 | |
| 1816 | int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr) |
| 1817 | { |
| 1818 | int error; |
| 1819 | |
| 1820 | if (get_transaction) |
| 1821 | return __gfs2_setattr_simple(ip, attr); |
| 1822 | |
| 1823 | error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0); |
| 1824 | if (error) |
| 1825 | return error; |
| 1826 | |
| 1827 | error = __gfs2_setattr_simple(ip, attr); |
| 1828 | |
| 1829 | gfs2_trans_end(ip->i_sbd); |
| 1830 | |
| 1831 | return error; |
| 1832 | } |
| 1833 | |
| 1834 | int gfs2_repermission(struct inode *inode, int mask, struct nameidata *nd) |
| 1835 | { |
| 1836 | return permission(inode, mask, nd); |
| 1837 | } |
| 1838 | |