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