1 /* 2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. 3 * Copyright (C) 2004-2006 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 version 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 <linux/lm_interface.h> 20 #include <linux/security.h> 21 22 #include "gfs2.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 "util.h" 40 41 /** 42 * gfs2_inode_attr_in - Copy attributes from the dinode into the VFS inode 43 * @ip: The GFS2 inode (with embedded disk inode data) 44 * @inode: The Linux VFS inode 45 * 46 */ 47 48 void gfs2_inode_attr_in(struct gfs2_inode *ip) 49 { 50 struct inode *inode = &ip->i_inode; 51 struct gfs2_dinode_host *di = &ip->i_di; 52 53 inode->i_ino = ip->i_num.no_addr; 54 inode->i_nlink = di->di_nlink; 55 i_size_write(inode, di->di_size); 56 inode->i_atime.tv_sec = di->di_atime; 57 inode->i_mtime.tv_sec = di->di_mtime; 58 inode->i_ctime.tv_sec = di->di_ctime; 59 inode->i_atime.tv_nsec = 0; 60 inode->i_mtime.tv_nsec = 0; 61 inode->i_ctime.tv_nsec = 0; 62 inode->i_blocks = di->di_blocks << 63 (GFS2_SB(inode)->sd_sb.sb_bsize_shift - GFS2_BASIC_BLOCK_SHIFT); 64 65 if (di->di_flags & GFS2_DIF_IMMUTABLE) 66 inode->i_flags |= S_IMMUTABLE; 67 else 68 inode->i_flags &= ~S_IMMUTABLE; 69 70 if (di->di_flags & GFS2_DIF_APPENDONLY) 71 inode->i_flags |= S_APPEND; 72 else 73 inode->i_flags &= ~S_APPEND; 74 } 75 76 /** 77 * gfs2_inode_attr_out - Copy attributes from VFS inode into the dinode 78 * @ip: The GFS2 inode 79 * 80 * Only copy out the attributes that we want the VFS layer 81 * to be able to modify. 82 */ 83 84 void gfs2_inode_attr_out(struct gfs2_inode *ip) 85 { 86 struct inode *inode = &ip->i_inode; 87 struct gfs2_dinode_host *di = &ip->i_di; 88 di->di_atime = inode->i_atime.tv_sec; 89 di->di_mtime = inode->i_mtime.tv_sec; 90 di->di_ctime = inode->i_ctime.tv_sec; 91 } 92 93 static int iget_test(struct inode *inode, void *opaque) 94 { 95 struct gfs2_inode *ip = GFS2_I(inode); 96 struct gfs2_inum_host *inum = opaque; 97 98 if (ip && ip->i_num.no_addr == inum->no_addr) 99 return 1; 100 101 return 0; 102 } 103 104 static int iget_set(struct inode *inode, void *opaque) 105 { 106 struct gfs2_inode *ip = GFS2_I(inode); 107 struct gfs2_inum_host *inum = opaque; 108 109 ip->i_num = *inum; 110 return 0; 111 } 112 113 struct inode *gfs2_ilookup(struct super_block *sb, struct gfs2_inum_host *inum) 114 { 115 return ilookup5(sb, (unsigned long)inum->no_formal_ino, 116 iget_test, inum); 117 } 118 119 static struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum_host *inum) 120 { 121 return iget5_locked(sb, (unsigned long)inum->no_formal_ino, 122 iget_test, iget_set, inum); 123 } 124 125 /** 126 * gfs2_inode_lookup - Lookup an inode 127 * @sb: The super block 128 * @inum: The inode number 129 * @type: The type of the inode 130 * 131 * Returns: A VFS inode, or an error 132 */ 133 134 struct inode *gfs2_inode_lookup(struct super_block *sb, struct gfs2_inum_host *inum, unsigned int type) 135 { 136 struct inode *inode = gfs2_iget(sb, inum); 137 struct gfs2_inode *ip = GFS2_I(inode); 138 struct gfs2_glock *io_gl; 139 int error; 140 141 if (!inode) 142 return ERR_PTR(-ENOBUFS); 143 144 if (inode->i_state & I_NEW) { 145 struct gfs2_sbd *sdp = GFS2_SB(inode); 146 umode_t mode = DT2IF(type); 147 inode->i_private = ip; 148 inode->i_mode = mode; 149 150 if (S_ISREG(mode)) { 151 inode->i_op = &gfs2_file_iops; 152 inode->i_fop = &gfs2_file_fops; 153 inode->i_mapping->a_ops = &gfs2_file_aops; 154 } else if (S_ISDIR(mode)) { 155 inode->i_op = &gfs2_dir_iops; 156 inode->i_fop = &gfs2_dir_fops; 157 } else if (S_ISLNK(mode)) { 158 inode->i_op = &gfs2_symlink_iops; 159 } else { 160 inode->i_op = &gfs2_dev_iops; 161 } 162 163 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl); 164 if (unlikely(error)) 165 goto fail; 166 ip->i_gl->gl_object = ip; 167 168 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops, CREATE, &io_gl); 169 if (unlikely(error)) 170 goto fail_put; 171 172 ip->i_vn = ip->i_gl->gl_vn - 1; 173 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh); 174 if (unlikely(error)) 175 goto fail_iopen; 176 177 gfs2_glock_put(io_gl); 178 unlock_new_inode(inode); 179 } 180 181 return inode; 182 fail_iopen: 183 gfs2_glock_put(io_gl); 184 fail_put: 185 ip->i_gl->gl_object = NULL; 186 gfs2_glock_put(ip->i_gl); 187 fail: 188 iput(inode); 189 return ERR_PTR(error); 190 } 191 192 static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf) 193 { 194 struct gfs2_dinode_host *di = &ip->i_di; 195 const struct gfs2_dinode *str = buf; 196 197 if (ip->i_num.no_addr != be64_to_cpu(str->di_num.no_addr)) { 198 if (gfs2_consist_inode(ip)) 199 gfs2_dinode_print(ip); 200 return -EIO; 201 } 202 if (ip->i_num.no_formal_ino != be64_to_cpu(str->di_num.no_formal_ino)) 203 return -ESTALE; 204 205 ip->i_inode.i_mode = be32_to_cpu(str->di_mode); 206 ip->i_inode.i_rdev = 0; 207 switch (ip->i_inode.i_mode & S_IFMT) { 208 case S_IFBLK: 209 case S_IFCHR: 210 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major), 211 be32_to_cpu(str->di_minor)); 212 break; 213 }; 214 215 ip->i_inode.i_uid = be32_to_cpu(str->di_uid); 216 ip->i_inode.i_gid = be32_to_cpu(str->di_gid); 217 di->di_nlink = be32_to_cpu(str->di_nlink); 218 di->di_size = be64_to_cpu(str->di_size); 219 di->di_blocks = be64_to_cpu(str->di_blocks); 220 di->di_atime = be64_to_cpu(str->di_atime); 221 di->di_mtime = be64_to_cpu(str->di_mtime); 222 di->di_ctime = be64_to_cpu(str->di_ctime); 223 224 di->di_goal_meta = be64_to_cpu(str->di_goal_meta); 225 di->di_goal_data = be64_to_cpu(str->di_goal_data); 226 di->di_generation = be64_to_cpu(str->di_generation); 227 228 di->di_flags = be32_to_cpu(str->di_flags); 229 di->di_payload_format = be32_to_cpu(str->di_payload_format); 230 di->di_height = be16_to_cpu(str->di_height); 231 232 di->di_depth = be16_to_cpu(str->di_depth); 233 di->di_entries = be32_to_cpu(str->di_entries); 234 235 di->di_eattr = be64_to_cpu(str->di_eattr); 236 return 0; 237 } 238 239 /** 240 * gfs2_inode_refresh - Refresh the incore copy of the dinode 241 * @ip: The GFS2 inode 242 * 243 * Returns: errno 244 */ 245 246 int gfs2_inode_refresh(struct gfs2_inode *ip) 247 { 248 struct buffer_head *dibh; 249 int error; 250 251 error = gfs2_meta_inode_buffer(ip, &dibh); 252 if (error) 253 return error; 254 255 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) { 256 brelse(dibh); 257 return -EIO; 258 } 259 260 error = gfs2_dinode_in(ip, dibh->b_data); 261 brelse(dibh); 262 ip->i_vn = ip->i_gl->gl_vn; 263 264 return error; 265 } 266 267 int gfs2_dinode_dealloc(struct gfs2_inode *ip) 268 { 269 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 270 struct gfs2_alloc *al; 271 struct gfs2_rgrpd *rgd; 272 int error; 273 274 if (ip->i_di.di_blocks != 1) { 275 if (gfs2_consist_inode(ip)) 276 gfs2_dinode_print(ip); 277 return -EIO; 278 } 279 280 al = gfs2_alloc_get(ip); 281 282 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); 283 if (error) 284 goto out; 285 286 error = gfs2_rindex_hold(sdp, &al->al_ri_gh); 287 if (error) 288 goto out_qs; 289 290 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr); 291 if (!rgd) { 292 gfs2_consist_inode(ip); 293 error = -EIO; 294 goto out_rindex_relse; 295 } 296 297 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, 298 &al->al_rgd_gh); 299 if (error) 300 goto out_rindex_relse; 301 302 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1); 303 if (error) 304 goto out_rg_gunlock; 305 306 gfs2_trans_add_gl(ip->i_gl); 307 308 gfs2_free_di(rgd, ip); 309 310 gfs2_trans_end(sdp); 311 clear_bit(GLF_STICKY, &ip->i_gl->gl_flags); 312 313 out_rg_gunlock: 314 gfs2_glock_dq_uninit(&al->al_rgd_gh); 315 out_rindex_relse: 316 gfs2_glock_dq_uninit(&al->al_ri_gh); 317 out_qs: 318 gfs2_quota_unhold(ip); 319 out: 320 gfs2_alloc_put(ip); 321 return error; 322 } 323 324 /** 325 * gfs2_change_nlink - Change nlink count on inode 326 * @ip: The GFS2 inode 327 * @diff: The change in the nlink count required 328 * 329 * Returns: errno 330 */ 331 332 int gfs2_change_nlink(struct gfs2_inode *ip, int diff) 333 { 334 struct gfs2_sbd *sdp = ip->i_inode.i_sb->s_fs_info; 335 struct buffer_head *dibh; 336 u32 nlink; 337 int error; 338 339 BUG_ON(ip->i_di.di_nlink != ip->i_inode.i_nlink); 340 nlink = ip->i_di.di_nlink + diff; 341 342 /* If we are reducing the nlink count, but the new value ends up being 343 bigger than the old one, we must have underflowed. */ 344 if (diff < 0 && nlink > ip->i_di.di_nlink) { 345 if (gfs2_consist_inode(ip)) 346 gfs2_dinode_print(ip); 347 return -EIO; 348 } 349 350 error = gfs2_meta_inode_buffer(ip, &dibh); 351 if (error) 352 return error; 353 354 ip->i_di.di_nlink = nlink; 355 ip->i_di.di_ctime = get_seconds(); 356 ip->i_inode.i_nlink = nlink; 357 358 gfs2_trans_add_bh(ip->i_gl, dibh, 1); 359 gfs2_dinode_out(ip, dibh->b_data); 360 brelse(dibh); 361 mark_inode_dirty(&ip->i_inode); 362 363 if (ip->i_di.di_nlink == 0) { 364 struct gfs2_rgrpd *rgd; 365 struct gfs2_holder ri_gh, rg_gh; 366 367 error = gfs2_rindex_hold(sdp, &ri_gh); 368 if (error) 369 goto out; 370 error = -EIO; 371 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr); 372 if (!rgd) 373 goto out_norgrp; 374 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh); 375 if (error) 376 goto out_norgrp; 377 378 clear_nlink(&ip->i_inode); 379 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */ 380 gfs2_glock_dq_uninit(&rg_gh); 381 out_norgrp: 382 gfs2_glock_dq_uninit(&ri_gh); 383 } 384 out: 385 return error; 386 } 387 388 struct inode *gfs2_lookup_simple(struct inode *dip, const char *name) 389 { 390 struct qstr qstr; 391 gfs2_str2qstr(&qstr, name); 392 return gfs2_lookupi(dip, &qstr, 1, NULL); 393 } 394 395 396 /** 397 * gfs2_lookupi - Look up a filename in a directory and return its inode 398 * @d_gh: An initialized holder for the directory glock 399 * @name: The name of the inode to look for 400 * @is_root: If 1, ignore the caller's permissions 401 * @i_gh: An uninitialized holder for the new inode glock 402 * 403 * There will always be a vnode (Linux VFS inode) for the d_gh inode unless 404 * @is_root is true. 405 * 406 * Returns: errno 407 */ 408 409 struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name, 410 int is_root, struct nameidata *nd) 411 { 412 struct super_block *sb = dir->i_sb; 413 struct gfs2_inode *dip = GFS2_I(dir); 414 struct gfs2_holder d_gh; 415 struct gfs2_inum_host inum; 416 unsigned int type; 417 int error = 0; 418 struct inode *inode = NULL; 419 420 if (!name->len || name->len > GFS2_FNAMESIZE) 421 return ERR_PTR(-ENAMETOOLONG); 422 423 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) || 424 (name->len == 2 && memcmp(name->name, "..", 2) == 0 && 425 dir == sb->s_root->d_inode)) { 426 igrab(dir); 427 return dir; 428 } 429 430 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh); 431 if (error) 432 return ERR_PTR(error); 433 434 if (!is_root) { 435 error = permission(dir, MAY_EXEC, NULL); 436 if (error) 437 goto out; 438 } 439 440 error = gfs2_dir_search(dir, name, &inum, &type); 441 if (error) 442 goto out; 443 444 inode = gfs2_inode_lookup(sb, &inum, type); 445 446 out: 447 gfs2_glock_dq_uninit(&d_gh); 448 if (error == -ENOENT) 449 return NULL; 450 return inode; 451 } 452 453 static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino) 454 { 455 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode); 456 struct buffer_head *bh; 457 struct gfs2_inum_range_host ir; 458 int error; 459 460 error = gfs2_trans_begin(sdp, RES_DINODE, 0); 461 if (error) 462 return error; 463 mutex_lock(&sdp->sd_inum_mutex); 464 465 error = gfs2_meta_inode_buffer(ip, &bh); 466 if (error) { 467 mutex_unlock(&sdp->sd_inum_mutex); 468 gfs2_trans_end(sdp); 469 return error; 470 } 471 472 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode)); 473 474 if (ir.ir_length) { 475 *formal_ino = ir.ir_start++; 476 ir.ir_length--; 477 gfs2_trans_add_bh(ip->i_gl, bh, 1); 478 gfs2_inum_range_out(&ir, 479 bh->b_data + sizeof(struct gfs2_dinode)); 480 brelse(bh); 481 mutex_unlock(&sdp->sd_inum_mutex); 482 gfs2_trans_end(sdp); 483 return 0; 484 } 485 486 brelse(bh); 487 488 mutex_unlock(&sdp->sd_inum_mutex); 489 gfs2_trans_end(sdp); 490 491 return 1; 492 } 493 494 static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino) 495 { 496 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode); 497 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode); 498 struct gfs2_holder gh; 499 struct buffer_head *bh; 500 struct gfs2_inum_range_host ir; 501 int error; 502 503 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); 504 if (error) 505 return error; 506 507 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0); 508 if (error) 509 goto out; 510 mutex_lock(&sdp->sd_inum_mutex); 511 512 error = gfs2_meta_inode_buffer(ip, &bh); 513 if (error) 514 goto out_end_trans; 515 516 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode)); 517 518 if (!ir.ir_length) { 519 struct buffer_head *m_bh; 520 u64 x, y; 521 __be64 z; 522 523 error = gfs2_meta_inode_buffer(m_ip, &m_bh); 524 if (error) 525 goto out_brelse; 526 527 z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)); 528 x = y = be64_to_cpu(z); 529 ir.ir_start = x; 530 ir.ir_length = GFS2_INUM_QUANTUM; 531 x += GFS2_INUM_QUANTUM; 532 if (x < y) 533 gfs2_consist_inode(m_ip); 534 z = cpu_to_be64(x); 535 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1); 536 *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z; 537 538 brelse(m_bh); 539 } 540 541 *formal_ino = ir.ir_start++; 542 ir.ir_length--; 543 544 gfs2_trans_add_bh(ip->i_gl, bh, 1); 545 gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode)); 546 547 out_brelse: 548 brelse(bh); 549 out_end_trans: 550 mutex_unlock(&sdp->sd_inum_mutex); 551 gfs2_trans_end(sdp); 552 out: 553 gfs2_glock_dq_uninit(&gh); 554 return error; 555 } 556 557 static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum) 558 { 559 int error; 560 561 error = pick_formal_ino_1(sdp, inum); 562 if (error <= 0) 563 return error; 564 565 error = pick_formal_ino_2(sdp, inum); 566 567 return error; 568 } 569 570 /** 571 * create_ok - OK to create a new on-disk inode here? 572 * @dip: Directory in which dinode is to be created 573 * @name: Name of new dinode 574 * @mode: 575 * 576 * Returns: errno 577 */ 578 579 static int create_ok(struct gfs2_inode *dip, const struct qstr *name, 580 unsigned int mode) 581 { 582 int error; 583 584 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL); 585 if (error) 586 return error; 587 588 /* Don't create entries in an unlinked directory */ 589 if (!dip->i_di.di_nlink) 590 return -EPERM; 591 592 error = gfs2_dir_search(&dip->i_inode, name, NULL, NULL); 593 switch (error) { 594 case -ENOENT: 595 error = 0; 596 break; 597 case 0: 598 return -EEXIST; 599 default: 600 return error; 601 } 602 603 if (dip->i_di.di_entries == (u32)-1) 604 return -EFBIG; 605 if (S_ISDIR(mode) && dip->i_di.di_nlink == (u32)-1) 606 return -EMLINK; 607 608 return 0; 609 } 610 611 static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode, 612 unsigned int *uid, unsigned int *gid) 613 { 614 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir && 615 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) { 616 if (S_ISDIR(*mode)) 617 *mode |= S_ISUID; 618 else if (dip->i_inode.i_uid != current->fsuid) 619 *mode &= ~07111; 620 *uid = dip->i_inode.i_uid; 621 } else 622 *uid = current->fsuid; 623 624 if (dip->i_inode.i_mode & S_ISGID) { 625 if (S_ISDIR(*mode)) 626 *mode |= S_ISGID; 627 *gid = dip->i_inode.i_gid; 628 } else 629 *gid = current->fsgid; 630 } 631 632 static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_inum_host *inum, 633 u64 *generation) 634 { 635 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 636 int error; 637 638 gfs2_alloc_get(dip); 639 640 dip->i_alloc.al_requested = RES_DINODE; 641 error = gfs2_inplace_reserve(dip); 642 if (error) 643 goto out; 644 645 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0); 646 if (error) 647 goto out_ipreserv; 648 649 inum->no_addr = gfs2_alloc_di(dip, generation); 650 651 gfs2_trans_end(sdp); 652 653 out_ipreserv: 654 gfs2_inplace_release(dip); 655 out: 656 gfs2_alloc_put(dip); 657 return error; 658 } 659 660 /** 661 * init_dinode - Fill in a new dinode structure 662 * @dip: the directory this inode is being created in 663 * @gl: The glock covering the new inode 664 * @inum: the inode number 665 * @mode: the file permissions 666 * @uid: 667 * @gid: 668 * 669 */ 670 671 static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl, 672 const struct gfs2_inum_host *inum, unsigned int mode, 673 unsigned int uid, unsigned int gid, 674 const u64 *generation, dev_t dev) 675 { 676 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 677 struct gfs2_dinode *di; 678 struct buffer_head *dibh; 679 680 dibh = gfs2_meta_new(gl, inum->no_addr); 681 gfs2_trans_add_bh(gl, dibh, 1); 682 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI); 683 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); 684 di = (struct gfs2_dinode *)dibh->b_data; 685 686 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino); 687 di->di_num.no_addr = cpu_to_be64(inum->no_addr); 688 di->di_mode = cpu_to_be32(mode); 689 di->di_uid = cpu_to_be32(uid); 690 di->di_gid = cpu_to_be32(gid); 691 di->di_nlink = cpu_to_be32(0); 692 di->di_size = cpu_to_be64(0); 693 di->di_blocks = cpu_to_be64(1); 694 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds()); 695 di->di_major = cpu_to_be32(MAJOR(dev)); 696 di->di_minor = cpu_to_be32(MINOR(dev)); 697 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr); 698 di->di_generation = cpu_to_be64(*generation); 699 di->di_flags = cpu_to_be32(0); 700 701 if (S_ISREG(mode)) { 702 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) || 703 gfs2_tune_get(sdp, gt_new_files_jdata)) 704 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA); 705 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) || 706 gfs2_tune_get(sdp, gt_new_files_directio)) 707 di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO); 708 } else if (S_ISDIR(mode)) { 709 di->di_flags |= cpu_to_be32(dip->i_di.di_flags & 710 GFS2_DIF_INHERIT_DIRECTIO); 711 di->di_flags |= cpu_to_be32(dip->i_di.di_flags & 712 GFS2_DIF_INHERIT_JDATA); 713 } 714 715 di->__pad1 = 0; 716 di->di_payload_format = cpu_to_be32(0); 717 di->di_height = cpu_to_be32(0); 718 di->__pad2 = 0; 719 di->__pad3 = 0; 720 di->di_depth = cpu_to_be16(0); 721 di->di_entries = cpu_to_be32(0); 722 memset(&di->__pad4, 0, sizeof(di->__pad4)); 723 di->di_eattr = cpu_to_be64(0); 724 memset(&di->di_reserved, 0, sizeof(di->di_reserved)); 725 726 brelse(dibh); 727 } 728 729 static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl, 730 unsigned int mode, const struct gfs2_inum_host *inum, 731 const u64 *generation, dev_t dev) 732 { 733 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 734 unsigned int uid, gid; 735 int error; 736 737 munge_mode_uid_gid(dip, &mode, &uid, &gid); 738 gfs2_alloc_get(dip); 739 740 error = gfs2_quota_lock(dip, uid, gid); 741 if (error) 742 goto out; 743 744 error = gfs2_quota_check(dip, uid, gid); 745 if (error) 746 goto out_quota; 747 748 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0); 749 if (error) 750 goto out_quota; 751 752 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev); 753 gfs2_quota_change(dip, +1, uid, gid); 754 gfs2_trans_end(sdp); 755 756 out_quota: 757 gfs2_quota_unlock(dip); 758 out: 759 gfs2_alloc_put(dip); 760 return error; 761 } 762 763 static int link_dinode(struct gfs2_inode *dip, const struct qstr *name, 764 struct gfs2_inode *ip) 765 { 766 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 767 struct gfs2_alloc *al; 768 int alloc_required; 769 struct buffer_head *dibh; 770 int error; 771 772 al = gfs2_alloc_get(dip); 773 774 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); 775 if (error) 776 goto fail; 777 778 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name); 779 if (alloc_required < 0) 780 goto fail; 781 if (alloc_required) { 782 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid); 783 if (error) 784 goto fail_quota_locks; 785 786 al->al_requested = sdp->sd_max_dirres; 787 788 error = gfs2_inplace_reserve(dip); 789 if (error) 790 goto fail_quota_locks; 791 792 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres + 793 al->al_rgd->rd_ri.ri_length + 794 2 * RES_DINODE + 795 RES_STATFS + RES_QUOTA, 0); 796 if (error) 797 goto fail_ipreserv; 798 } else { 799 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0); 800 if (error) 801 goto fail_quota_locks; 802 } 803 804 error = gfs2_dir_add(&dip->i_inode, name, &ip->i_num, IF2DT(ip->i_inode.i_mode)); 805 if (error) 806 goto fail_end_trans; 807 808 error = gfs2_meta_inode_buffer(ip, &dibh); 809 if (error) 810 goto fail_end_trans; 811 ip->i_di.di_nlink = 1; 812 gfs2_trans_add_bh(ip->i_gl, dibh, 1); 813 gfs2_dinode_out(ip, dibh->b_data); 814 brelse(dibh); 815 return 0; 816 817 fail_end_trans: 818 gfs2_trans_end(sdp); 819 820 fail_ipreserv: 821 if (dip->i_alloc.al_rgd) 822 gfs2_inplace_release(dip); 823 824 fail_quota_locks: 825 gfs2_quota_unlock(dip); 826 827 fail: 828 gfs2_alloc_put(dip); 829 return error; 830 } 831 832 static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip) 833 { 834 int err; 835 size_t len; 836 void *value; 837 char *name; 838 struct gfs2_ea_request er; 839 840 err = security_inode_init_security(&ip->i_inode, &dip->i_inode, 841 &name, &value, &len); 842 843 if (err) { 844 if (err == -EOPNOTSUPP) 845 return 0; 846 return err; 847 } 848 849 memset(&er, 0, sizeof(struct gfs2_ea_request)); 850 851 er.er_type = GFS2_EATYPE_SECURITY; 852 er.er_name = name; 853 er.er_data = value; 854 er.er_name_len = strlen(name); 855 er.er_data_len = len; 856 857 err = gfs2_ea_set_i(ip, &er); 858 859 kfree(value); 860 kfree(name); 861 862 return err; 863 } 864 865 /** 866 * gfs2_createi - Create a new inode 867 * @ghs: An array of two holders 868 * @name: The name of the new file 869 * @mode: the permissions on the new inode 870 * 871 * @ghs[0] is an initialized holder for the directory 872 * @ghs[1] is the holder for the inode lock 873 * 874 * If the return value is not NULL, the glocks on both the directory and the new 875 * file are held. A transaction has been started and an inplace reservation 876 * is held, as well. 877 * 878 * Returns: An inode 879 */ 880 881 struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name, 882 unsigned int mode, dev_t dev) 883 { 884 struct inode *inode; 885 struct gfs2_inode *dip = ghs->gh_gl->gl_object; 886 struct inode *dir = &dip->i_inode; 887 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 888 struct gfs2_inum_host inum; 889 int error; 890 u64 generation; 891 892 if (!name->len || name->len > GFS2_FNAMESIZE) 893 return ERR_PTR(-ENAMETOOLONG); 894 895 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs); 896 error = gfs2_glock_nq(ghs); 897 if (error) 898 goto fail; 899 900 error = create_ok(dip, name, mode); 901 if (error) 902 goto fail_gunlock; 903 904 error = pick_formal_ino(sdp, &inum.no_formal_ino); 905 if (error) 906 goto fail_gunlock; 907 908 error = alloc_dinode(dip, &inum, &generation); 909 if (error) 910 goto fail_gunlock; 911 912 if (inum.no_addr < dip->i_num.no_addr) { 913 gfs2_glock_dq(ghs); 914 915 error = gfs2_glock_nq_num(sdp, inum.no_addr, 916 &gfs2_inode_glops, LM_ST_EXCLUSIVE, 917 GL_SKIP, ghs + 1); 918 if (error) { 919 return ERR_PTR(error); 920 } 921 922 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs); 923 error = gfs2_glock_nq(ghs); 924 if (error) { 925 gfs2_glock_dq_uninit(ghs + 1); 926 return ERR_PTR(error); 927 } 928 929 error = create_ok(dip, name, mode); 930 if (error) 931 goto fail_gunlock2; 932 } else { 933 error = gfs2_glock_nq_num(sdp, inum.no_addr, 934 &gfs2_inode_glops, LM_ST_EXCLUSIVE, 935 GL_SKIP, ghs + 1); 936 if (error) 937 goto fail_gunlock; 938 } 939 940 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev); 941 if (error) 942 goto fail_gunlock2; 943 944 inode = gfs2_inode_lookup(dir->i_sb, &inum, IF2DT(mode)); 945 if (IS_ERR(inode)) 946 goto fail_gunlock2; 947 948 error = gfs2_inode_refresh(GFS2_I(inode)); 949 if (error) 950 goto fail_iput; 951 952 error = gfs2_acl_create(dip, GFS2_I(inode)); 953 if (error) 954 goto fail_iput; 955 956 error = gfs2_security_init(dip, GFS2_I(inode)); 957 if (error) 958 goto fail_iput; 959 960 error = link_dinode(dip, name, GFS2_I(inode)); 961 if (error) 962 goto fail_iput; 963 964 if (!inode) 965 return ERR_PTR(-ENOMEM); 966 return inode; 967 968 fail_iput: 969 iput(inode); 970 fail_gunlock2: 971 gfs2_glock_dq_uninit(ghs + 1); 972 fail_gunlock: 973 gfs2_glock_dq(ghs); 974 fail: 975 return ERR_PTR(error); 976 } 977 978 /** 979 * gfs2_rmdiri - Remove a directory 980 * @dip: The parent directory of the directory to be removed 981 * @name: The name of the directory to be removed 982 * @ip: The GFS2 inode of the directory to be removed 983 * 984 * Assumes Glocks on dip and ip are held 985 * 986 * Returns: errno 987 */ 988 989 int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name, 990 struct gfs2_inode *ip) 991 { 992 struct qstr dotname; 993 int error; 994 995 if (ip->i_di.di_entries != 2) { 996 if (gfs2_consist_inode(ip)) 997 gfs2_dinode_print(ip); 998 return -EIO; 999 } 1000 1001 error = gfs2_dir_del(dip, name); 1002 if (error) 1003 return error; 1004 1005 error = gfs2_change_nlink(dip, -1); 1006 if (error) 1007 return error; 1008 1009 gfs2_str2qstr(&dotname, "."); 1010 error = gfs2_dir_del(ip, &dotname); 1011 if (error) 1012 return error; 1013 1014 gfs2_str2qstr(&dotname, ".."); 1015 error = gfs2_dir_del(ip, &dotname); 1016 if (error) 1017 return error; 1018 1019 error = gfs2_change_nlink(ip, -2); 1020 if (error) 1021 return error; 1022 1023 return error; 1024 } 1025 1026 /* 1027 * gfs2_unlink_ok - check to see that a inode is still in a directory 1028 * @dip: the directory 1029 * @name: the name of the file 1030 * @ip: the inode 1031 * 1032 * Assumes that the lock on (at least) @dip is held. 1033 * 1034 * Returns: 0 if the parent/child relationship is correct, errno if it isn't 1035 */ 1036 1037 int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name, 1038 struct gfs2_inode *ip) 1039 { 1040 struct gfs2_inum_host inum; 1041 unsigned int type; 1042 int error; 1043 1044 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode)) 1045 return -EPERM; 1046 1047 if ((dip->i_inode.i_mode & S_ISVTX) && 1048 dip->i_inode.i_uid != current->fsuid && 1049 ip->i_inode.i_uid != current->fsuid && !capable(CAP_FOWNER)) 1050 return -EPERM; 1051 1052 if (IS_APPEND(&dip->i_inode)) 1053 return -EPERM; 1054 1055 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL); 1056 if (error) 1057 return error; 1058 1059 error = gfs2_dir_search(&dip->i_inode, name, &inum, &type); 1060 if (error) 1061 return error; 1062 1063 if (!gfs2_inum_equal(&inum, &ip->i_num)) 1064 return -ENOENT; 1065 1066 if (IF2DT(ip->i_inode.i_mode) != type) { 1067 gfs2_consist_inode(dip); 1068 return -EIO; 1069 } 1070 1071 return 0; 1072 } 1073 1074 /* 1075 * gfs2_ok_to_move - check if it's ok to move a directory to another directory 1076 * @this: move this 1077 * @to: to here 1078 * 1079 * Follow @to back to the root and make sure we don't encounter @this 1080 * Assumes we already hold the rename lock. 1081 * 1082 * Returns: errno 1083 */ 1084 1085 int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to) 1086 { 1087 struct inode *dir = &to->i_inode; 1088 struct super_block *sb = dir->i_sb; 1089 struct inode *tmp; 1090 struct qstr dotdot; 1091 int error = 0; 1092 1093 gfs2_str2qstr(&dotdot, ".."); 1094 1095 igrab(dir); 1096 1097 for (;;) { 1098 if (dir == &this->i_inode) { 1099 error = -EINVAL; 1100 break; 1101 } 1102 if (dir == sb->s_root->d_inode) { 1103 error = 0; 1104 break; 1105 } 1106 1107 tmp = gfs2_lookupi(dir, &dotdot, 1, NULL); 1108 if (IS_ERR(tmp)) { 1109 error = PTR_ERR(tmp); 1110 break; 1111 } 1112 1113 iput(dir); 1114 dir = tmp; 1115 } 1116 1117 iput(dir); 1118 1119 return error; 1120 } 1121 1122 /** 1123 * gfs2_readlinki - return the contents of a symlink 1124 * @ip: the symlink's inode 1125 * @buf: a pointer to the buffer to be filled 1126 * @len: a pointer to the length of @buf 1127 * 1128 * If @buf is too small, a piece of memory is kmalloc()ed and needs 1129 * to be freed by the caller. 1130 * 1131 * Returns: errno 1132 */ 1133 1134 int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len) 1135 { 1136 struct gfs2_holder i_gh; 1137 struct buffer_head *dibh; 1138 unsigned int x; 1139 int error; 1140 1141 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh); 1142 error = gfs2_glock_nq_atime(&i_gh); 1143 if (error) { 1144 gfs2_holder_uninit(&i_gh); 1145 return error; 1146 } 1147 1148 if (!ip->i_di.di_size) { 1149 gfs2_consist_inode(ip); 1150 error = -EIO; 1151 goto out; 1152 } 1153 1154 error = gfs2_meta_inode_buffer(ip, &dibh); 1155 if (error) 1156 goto out; 1157 1158 x = ip->i_di.di_size + 1; 1159 if (x > *len) { 1160 *buf = kmalloc(x, GFP_KERNEL); 1161 if (!*buf) { 1162 error = -ENOMEM; 1163 goto out_brelse; 1164 } 1165 } 1166 1167 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x); 1168 *len = x; 1169 1170 out_brelse: 1171 brelse(dibh); 1172 out: 1173 gfs2_glock_dq_uninit(&i_gh); 1174 return error; 1175 } 1176 1177 /** 1178 * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and 1179 * conditionally update the inode's atime 1180 * @gh: the holder to acquire 1181 * 1182 * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap 1183 * Update if the difference between the current time and the inode's current 1184 * atime is greater than an interval specified at mount. 1185 * 1186 * Returns: errno 1187 */ 1188 1189 int gfs2_glock_nq_atime(struct gfs2_holder *gh) 1190 { 1191 struct gfs2_glock *gl = gh->gh_gl; 1192 struct gfs2_sbd *sdp = gl->gl_sbd; 1193 struct gfs2_inode *ip = gl->gl_object; 1194 s64 curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum); 1195 unsigned int state; 1196 int flags; 1197 int error; 1198 1199 if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) || 1200 gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) || 1201 gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops)) 1202 return -EINVAL; 1203 1204 state = gh->gh_state; 1205 flags = gh->gh_flags; 1206 1207 error = gfs2_glock_nq(gh); 1208 if (error) 1209 return error; 1210 1211 if (test_bit(SDF_NOATIME, &sdp->sd_flags) || 1212 (sdp->sd_vfs->s_flags & MS_RDONLY)) 1213 return 0; 1214 1215 curtime = get_seconds(); 1216 if (curtime - ip->i_di.di_atime >= quantum) { 1217 gfs2_glock_dq(gh); 1218 gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY, 1219 gh); 1220 error = gfs2_glock_nq(gh); 1221 if (error) 1222 return error; 1223 1224 /* Verify that atime hasn't been updated while we were 1225 trying to get exclusive lock. */ 1226 1227 curtime = get_seconds(); 1228 if (curtime - ip->i_di.di_atime >= quantum) { 1229 struct buffer_head *dibh; 1230 struct gfs2_dinode *di; 1231 1232 error = gfs2_trans_begin(sdp, RES_DINODE, 0); 1233 if (error == -EROFS) 1234 return 0; 1235 if (error) 1236 goto fail; 1237 1238 error = gfs2_meta_inode_buffer(ip, &dibh); 1239 if (error) 1240 goto fail_end_trans; 1241 1242 ip->i_di.di_atime = curtime; 1243 1244 gfs2_trans_add_bh(ip->i_gl, dibh, 1); 1245 di = (struct gfs2_dinode *)dibh->b_data; 1246 di->di_atime = cpu_to_be64(ip->i_di.di_atime); 1247 brelse(dibh); 1248 1249 gfs2_trans_end(sdp); 1250 } 1251 1252 /* If someone else has asked for the glock, 1253 unlock and let them have it. Then reacquire 1254 in the original state. */ 1255 if (gfs2_glock_is_blocking(gl)) { 1256 gfs2_glock_dq(gh); 1257 gfs2_holder_reinit(state, flags, gh); 1258 return gfs2_glock_nq(gh); 1259 } 1260 } 1261 1262 return 0; 1263 1264 fail_end_trans: 1265 gfs2_trans_end(sdp); 1266 fail: 1267 gfs2_glock_dq(gh); 1268 return error; 1269 } 1270 1271 /** 1272 * glock_compare_atime - Compare two struct gfs2_glock structures for sort 1273 * @arg_a: the first structure 1274 * @arg_b: the second structure 1275 * 1276 * Returns: 1 if A > B 1277 * -1 if A < B 1278 * 0 if A == B 1279 */ 1280 1281 static int glock_compare_atime(const void *arg_a, const void *arg_b) 1282 { 1283 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a; 1284 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b; 1285 const struct lm_lockname *a = &gh_a->gh_gl->gl_name; 1286 const struct lm_lockname *b = &gh_b->gh_gl->gl_name; 1287 1288 if (a->ln_number > b->ln_number) 1289 return 1; 1290 if (a->ln_number < b->ln_number) 1291 return -1; 1292 if (gh_a->gh_state == LM_ST_SHARED && gh_b->gh_state == LM_ST_EXCLUSIVE) 1293 return 1; 1294 if (gh_a->gh_state == LM_ST_SHARED && (gh_b->gh_flags & GL_ATIME)) 1295 return 1; 1296 1297 return 0; 1298 } 1299 1300 /** 1301 * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an 1302 * atime update 1303 * @num_gh: the number of structures 1304 * @ghs: an array of struct gfs2_holder structures 1305 * 1306 * Returns: 0 on success (all glocks acquired), 1307 * errno on failure (no glocks acquired) 1308 */ 1309 1310 int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs) 1311 { 1312 struct gfs2_holder **p; 1313 unsigned int x; 1314 int error = 0; 1315 1316 if (!num_gh) 1317 return 0; 1318 1319 if (num_gh == 1) { 1320 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC); 1321 if (ghs->gh_flags & GL_ATIME) 1322 error = gfs2_glock_nq_atime(ghs); 1323 else 1324 error = gfs2_glock_nq(ghs); 1325 return error; 1326 } 1327 1328 p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL); 1329 if (!p) 1330 return -ENOMEM; 1331 1332 for (x = 0; x < num_gh; x++) 1333 p[x] = &ghs[x]; 1334 1335 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL); 1336 1337 for (x = 0; x < num_gh; x++) { 1338 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC); 1339 1340 if (p[x]->gh_flags & GL_ATIME) 1341 error = gfs2_glock_nq_atime(p[x]); 1342 else 1343 error = gfs2_glock_nq(p[x]); 1344 1345 if (error) { 1346 while (x--) 1347 gfs2_glock_dq(p[x]); 1348 break; 1349 } 1350 } 1351 1352 kfree(p); 1353 return error; 1354 } 1355 1356 1357 static int 1358 __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr) 1359 { 1360 struct buffer_head *dibh; 1361 int error; 1362 1363 error = gfs2_meta_inode_buffer(ip, &dibh); 1364 if (!error) { 1365 error = inode_setattr(&ip->i_inode, attr); 1366 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error); 1367 gfs2_inode_attr_out(ip); 1368 1369 gfs2_trans_add_bh(ip->i_gl, dibh, 1); 1370 gfs2_dinode_out(ip, dibh->b_data); 1371 brelse(dibh); 1372 } 1373 return error; 1374 } 1375 1376 /** 1377 * gfs2_setattr_simple - 1378 * @ip: 1379 * @attr: 1380 * 1381 * Called with a reference on the vnode. 1382 * 1383 * Returns: errno 1384 */ 1385 1386 int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr) 1387 { 1388 int error; 1389 1390 if (current->journal_info) 1391 return __gfs2_setattr_simple(ip, attr); 1392 1393 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0); 1394 if (error) 1395 return error; 1396 1397 error = __gfs2_setattr_simple(ip, attr); 1398 gfs2_trans_end(GFS2_SB(&ip->i_inode)); 1399 return error; 1400 } 1401 1402