1 /* 2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. 3 * Copyright (C) 2004-2008 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/security.h> 20 #include <linux/time.h> 21 22 #include "gfs2.h" 23 #include "incore.h" 24 #include "acl.h" 25 #include "bmap.h" 26 #include "dir.h" 27 #include "xattr.h" 28 #include "glock.h" 29 #include "glops.h" 30 #include "inode.h" 31 #include "log.h" 32 #include "meta_io.h" 33 #include "quota.h" 34 #include "rgrp.h" 35 #include "trans.h" 36 #include "util.h" 37 38 struct gfs2_inum_range_host { 39 u64 ir_start; 40 u64 ir_length; 41 }; 42 43 static int iget_test(struct inode *inode, void *opaque) 44 { 45 struct gfs2_inode *ip = GFS2_I(inode); 46 u64 *no_addr = opaque; 47 48 if (ip->i_no_addr == *no_addr) 49 return 1; 50 51 return 0; 52 } 53 54 static int iget_set(struct inode *inode, void *opaque) 55 { 56 struct gfs2_inode *ip = GFS2_I(inode); 57 u64 *no_addr = opaque; 58 59 inode->i_ino = (unsigned long)*no_addr; 60 ip->i_no_addr = *no_addr; 61 return 0; 62 } 63 64 struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr) 65 { 66 unsigned long hash = (unsigned long)no_addr; 67 return ilookup5(sb, hash, iget_test, &no_addr); 68 } 69 70 static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr) 71 { 72 unsigned long hash = (unsigned long)no_addr; 73 return iget5_locked(sb, hash, iget_test, iget_set, &no_addr); 74 } 75 76 struct gfs2_skip_data { 77 u64 no_addr; 78 int skipped; 79 }; 80 81 static int iget_skip_test(struct inode *inode, void *opaque) 82 { 83 struct gfs2_inode *ip = GFS2_I(inode); 84 struct gfs2_skip_data *data = opaque; 85 86 if (ip->i_no_addr == data->no_addr) { 87 if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)){ 88 data->skipped = 1; 89 return 0; 90 } 91 return 1; 92 } 93 return 0; 94 } 95 96 static int iget_skip_set(struct inode *inode, void *opaque) 97 { 98 struct gfs2_inode *ip = GFS2_I(inode); 99 struct gfs2_skip_data *data = opaque; 100 101 if (data->skipped) 102 return 1; 103 inode->i_ino = (unsigned long)(data->no_addr); 104 ip->i_no_addr = data->no_addr; 105 return 0; 106 } 107 108 static struct inode *gfs2_iget_skip(struct super_block *sb, 109 u64 no_addr) 110 { 111 struct gfs2_skip_data data; 112 unsigned long hash = (unsigned long)no_addr; 113 114 data.no_addr = no_addr; 115 data.skipped = 0; 116 return iget5_locked(sb, hash, iget_skip_test, iget_skip_set, &data); 117 } 118 119 /** 120 * GFS2 lookup code fills in vfs inode contents based on info obtained 121 * from directory entry inside gfs2_inode_lookup(). This has caused issues 122 * with NFS code path since its get_dentry routine doesn't have the relevant 123 * directory entry when gfs2_inode_lookup() is invoked. Part of the code 124 * segment inside gfs2_inode_lookup code needs to get moved around. 125 * 126 * Clears I_NEW as well. 127 **/ 128 129 void gfs2_set_iop(struct inode *inode) 130 { 131 struct gfs2_sbd *sdp = GFS2_SB(inode); 132 umode_t mode = inode->i_mode; 133 134 if (S_ISREG(mode)) { 135 inode->i_op = &gfs2_file_iops; 136 if (gfs2_localflocks(sdp)) 137 inode->i_fop = &gfs2_file_fops_nolock; 138 else 139 inode->i_fop = &gfs2_file_fops; 140 } else if (S_ISDIR(mode)) { 141 inode->i_op = &gfs2_dir_iops; 142 if (gfs2_localflocks(sdp)) 143 inode->i_fop = &gfs2_dir_fops_nolock; 144 else 145 inode->i_fop = &gfs2_dir_fops; 146 } else if (S_ISLNK(mode)) { 147 inode->i_op = &gfs2_symlink_iops; 148 } else { 149 inode->i_op = &gfs2_file_iops; 150 init_special_inode(inode, inode->i_mode, inode->i_rdev); 151 } 152 153 unlock_new_inode(inode); 154 } 155 156 /** 157 * gfs2_inode_lookup - Lookup an inode 158 * @sb: The super block 159 * @no_addr: The inode number 160 * @type: The type of the inode 161 * 162 * Returns: A VFS inode, or an error 163 */ 164 165 struct inode *gfs2_inode_lookup(struct super_block *sb, 166 unsigned int type, 167 u64 no_addr, 168 u64 no_formal_ino) 169 { 170 struct inode *inode; 171 struct gfs2_inode *ip; 172 struct gfs2_glock *io_gl; 173 int error; 174 175 inode = gfs2_iget(sb, no_addr); 176 ip = GFS2_I(inode); 177 178 if (!inode) 179 return ERR_PTR(-ENOBUFS); 180 181 if (inode->i_state & I_NEW) { 182 struct gfs2_sbd *sdp = GFS2_SB(inode); 183 ip->i_no_formal_ino = no_formal_ino; 184 185 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl); 186 if (unlikely(error)) 187 goto fail; 188 ip->i_gl->gl_object = ip; 189 190 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl); 191 if (unlikely(error)) 192 goto fail_put; 193 194 set_bit(GIF_INVALID, &ip->i_flags); 195 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh); 196 if (unlikely(error)) 197 goto fail_iopen; 198 ip->i_iopen_gh.gh_gl->gl_object = ip; 199 200 gfs2_glock_put(io_gl); 201 202 if ((type == DT_UNKNOWN) && (no_formal_ino == 0)) 203 goto gfs2_nfsbypass; 204 205 inode->i_mode = DT2IF(type); 206 207 /* 208 * We must read the inode in order to work out its type in 209 * this case. Note that this doesn't happen often as we normally 210 * know the type beforehand. This code path only occurs during 211 * unlinked inode recovery (where it is safe to do this glock, 212 * which is not true in the general case). 213 */ 214 if (type == DT_UNKNOWN) { 215 struct gfs2_holder gh; 216 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); 217 if (unlikely(error)) 218 goto fail_glock; 219 /* Inode is now uptodate */ 220 gfs2_glock_dq_uninit(&gh); 221 } 222 223 gfs2_set_iop(inode); 224 } 225 226 gfs2_nfsbypass: 227 return inode; 228 fail_glock: 229 gfs2_glock_dq(&ip->i_iopen_gh); 230 fail_iopen: 231 gfs2_glock_put(io_gl); 232 fail_put: 233 if (inode->i_state & I_NEW) 234 ip->i_gl->gl_object = NULL; 235 gfs2_glock_put(ip->i_gl); 236 fail: 237 if (inode->i_state & I_NEW) 238 iget_failed(inode); 239 else 240 iput(inode); 241 return ERR_PTR(error); 242 } 243 244 /** 245 * gfs2_process_unlinked_inode - Lookup an unlinked inode for reclamation 246 * and try to reclaim it by doing iput. 247 * 248 * This function assumes no rgrp locks are currently held. 249 * 250 * @sb: The super block 251 * no_addr: The inode number 252 * 253 */ 254 255 void gfs2_process_unlinked_inode(struct super_block *sb, u64 no_addr) 256 { 257 struct gfs2_sbd *sdp; 258 struct gfs2_inode *ip; 259 struct gfs2_glock *io_gl; 260 int error; 261 struct gfs2_holder gh; 262 struct inode *inode; 263 264 inode = gfs2_iget_skip(sb, no_addr); 265 266 if (!inode) 267 return; 268 269 /* If it's not a new inode, someone's using it, so leave it alone. */ 270 if (!(inode->i_state & I_NEW)) { 271 iput(inode); 272 return; 273 } 274 275 ip = GFS2_I(inode); 276 sdp = GFS2_SB(inode); 277 ip->i_no_formal_ino = -1; 278 279 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl); 280 if (unlikely(error)) 281 goto fail; 282 ip->i_gl->gl_object = ip; 283 284 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl); 285 if (unlikely(error)) 286 goto fail_put; 287 288 set_bit(GIF_INVALID, &ip->i_flags); 289 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, LM_FLAG_TRY | GL_EXACT, 290 &ip->i_iopen_gh); 291 if (unlikely(error)) 292 goto fail_iopen; 293 294 ip->i_iopen_gh.gh_gl->gl_object = ip; 295 gfs2_glock_put(io_gl); 296 297 inode->i_mode = DT2IF(DT_UNKNOWN); 298 299 /* 300 * We must read the inode in order to work out its type in 301 * this case. Note that this doesn't happen often as we normally 302 * know the type beforehand. This code path only occurs during 303 * unlinked inode recovery (where it is safe to do this glock, 304 * which is not true in the general case). 305 */ 306 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, LM_FLAG_TRY, 307 &gh); 308 if (unlikely(error)) 309 goto fail_glock; 310 311 /* Inode is now uptodate */ 312 gfs2_glock_dq_uninit(&gh); 313 gfs2_set_iop(inode); 314 315 /* The iput will cause it to be deleted. */ 316 iput(inode); 317 return; 318 319 fail_glock: 320 gfs2_glock_dq(&ip->i_iopen_gh); 321 fail_iopen: 322 gfs2_glock_put(io_gl); 323 fail_put: 324 ip->i_gl->gl_object = NULL; 325 gfs2_glock_put(ip->i_gl); 326 fail: 327 iget_failed(inode); 328 return; 329 } 330 331 static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf) 332 { 333 const struct gfs2_dinode *str = buf; 334 struct timespec atime; 335 u16 height, depth; 336 337 if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr))) 338 goto corrupt; 339 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino); 340 ip->i_inode.i_mode = be32_to_cpu(str->di_mode); 341 ip->i_inode.i_rdev = 0; 342 switch (ip->i_inode.i_mode & S_IFMT) { 343 case S_IFBLK: 344 case S_IFCHR: 345 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major), 346 be32_to_cpu(str->di_minor)); 347 break; 348 }; 349 350 ip->i_inode.i_uid = be32_to_cpu(str->di_uid); 351 ip->i_inode.i_gid = be32_to_cpu(str->di_gid); 352 /* 353 * We will need to review setting the nlink count here in the 354 * light of the forthcoming ro bind mount work. This is a reminder 355 * to do that. 356 */ 357 ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink); 358 ip->i_disksize = be64_to_cpu(str->di_size); 359 i_size_write(&ip->i_inode, ip->i_disksize); 360 gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks)); 361 atime.tv_sec = be64_to_cpu(str->di_atime); 362 atime.tv_nsec = be32_to_cpu(str->di_atime_nsec); 363 if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0) 364 ip->i_inode.i_atime = atime; 365 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime); 366 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec); 367 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime); 368 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec); 369 370 ip->i_goal = be64_to_cpu(str->di_goal_meta); 371 ip->i_generation = be64_to_cpu(str->di_generation); 372 373 ip->i_diskflags = be32_to_cpu(str->di_flags); 374 gfs2_set_inode_flags(&ip->i_inode); 375 height = be16_to_cpu(str->di_height); 376 if (unlikely(height > GFS2_MAX_META_HEIGHT)) 377 goto corrupt; 378 ip->i_height = (u8)height; 379 380 depth = be16_to_cpu(str->di_depth); 381 if (unlikely(depth > GFS2_DIR_MAX_DEPTH)) 382 goto corrupt; 383 ip->i_depth = (u8)depth; 384 ip->i_entries = be32_to_cpu(str->di_entries); 385 386 ip->i_eattr = be64_to_cpu(str->di_eattr); 387 if (S_ISREG(ip->i_inode.i_mode)) 388 gfs2_set_aops(&ip->i_inode); 389 390 return 0; 391 corrupt: 392 if (gfs2_consist_inode(ip)) 393 gfs2_dinode_print(ip); 394 return -EIO; 395 } 396 397 /** 398 * gfs2_inode_refresh - Refresh the incore copy of the dinode 399 * @ip: The GFS2 inode 400 * 401 * Returns: errno 402 */ 403 404 int gfs2_inode_refresh(struct gfs2_inode *ip) 405 { 406 struct buffer_head *dibh; 407 int error; 408 409 error = gfs2_meta_inode_buffer(ip, &dibh); 410 if (error) 411 return error; 412 413 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) { 414 brelse(dibh); 415 return -EIO; 416 } 417 418 error = gfs2_dinode_in(ip, dibh->b_data); 419 brelse(dibh); 420 clear_bit(GIF_INVALID, &ip->i_flags); 421 422 return error; 423 } 424 425 int gfs2_dinode_dealloc(struct gfs2_inode *ip) 426 { 427 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 428 struct gfs2_alloc *al; 429 struct gfs2_rgrpd *rgd; 430 int error; 431 432 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) { 433 if (gfs2_consist_inode(ip)) 434 gfs2_dinode_print(ip); 435 return -EIO; 436 } 437 438 al = gfs2_alloc_get(ip); 439 if (!al) 440 return -ENOMEM; 441 442 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); 443 if (error) 444 goto out; 445 446 error = gfs2_rindex_hold(sdp, &al->al_ri_gh); 447 if (error) 448 goto out_qs; 449 450 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr); 451 if (!rgd) { 452 gfs2_consist_inode(ip); 453 error = -EIO; 454 goto out_rindex_relse; 455 } 456 457 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, 458 &al->al_rgd_gh); 459 if (error) 460 goto out_rindex_relse; 461 462 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1); 463 if (error) 464 goto out_rg_gunlock; 465 466 set_bit(GLF_DIRTY, &ip->i_gl->gl_flags); 467 set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags); 468 469 gfs2_free_di(rgd, ip); 470 471 gfs2_trans_end(sdp); 472 473 out_rg_gunlock: 474 gfs2_glock_dq_uninit(&al->al_rgd_gh); 475 out_rindex_relse: 476 gfs2_glock_dq_uninit(&al->al_ri_gh); 477 out_qs: 478 gfs2_quota_unhold(ip); 479 out: 480 gfs2_alloc_put(ip); 481 return error; 482 } 483 484 /** 485 * gfs2_change_nlink - Change nlink count on inode 486 * @ip: The GFS2 inode 487 * @diff: The change in the nlink count required 488 * 489 * Returns: errno 490 */ 491 int gfs2_change_nlink(struct gfs2_inode *ip, int diff) 492 { 493 struct buffer_head *dibh; 494 u32 nlink; 495 int error; 496 497 BUG_ON(diff != 1 && diff != -1); 498 nlink = ip->i_inode.i_nlink + diff; 499 500 /* If we are reducing the nlink count, but the new value ends up being 501 bigger than the old one, we must have underflowed. */ 502 if (diff < 0 && nlink > ip->i_inode.i_nlink) { 503 if (gfs2_consist_inode(ip)) 504 gfs2_dinode_print(ip); 505 return -EIO; 506 } 507 508 error = gfs2_meta_inode_buffer(ip, &dibh); 509 if (error) 510 return error; 511 512 if (diff > 0) 513 inc_nlink(&ip->i_inode); 514 else 515 drop_nlink(&ip->i_inode); 516 517 ip->i_inode.i_ctime = CURRENT_TIME; 518 519 gfs2_trans_add_bh(ip->i_gl, dibh, 1); 520 gfs2_dinode_out(ip, dibh->b_data); 521 brelse(dibh); 522 mark_inode_dirty(&ip->i_inode); 523 524 if (ip->i_inode.i_nlink == 0) 525 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */ 526 527 return error; 528 } 529 530 struct inode *gfs2_lookup_simple(struct inode *dip, const char *name) 531 { 532 struct qstr qstr; 533 struct inode *inode; 534 gfs2_str2qstr(&qstr, name); 535 inode = gfs2_lookupi(dip, &qstr, 1); 536 /* gfs2_lookupi has inconsistent callers: vfs 537 * related routines expect NULL for no entry found, 538 * gfs2_lookup_simple callers expect ENOENT 539 * and do not check for NULL. 540 */ 541 if (inode == NULL) 542 return ERR_PTR(-ENOENT); 543 else 544 return inode; 545 } 546 547 548 /** 549 * gfs2_lookupi - Look up a filename in a directory and return its inode 550 * @d_gh: An initialized holder for the directory glock 551 * @name: The name of the inode to look for 552 * @is_root: If 1, ignore the caller's permissions 553 * @i_gh: An uninitialized holder for the new inode glock 554 * 555 * This can be called via the VFS filldir function when NFS is doing 556 * a readdirplus and the inode which its intending to stat isn't 557 * already in cache. In this case we must not take the directory glock 558 * again, since the readdir call will have already taken that lock. 559 * 560 * Returns: errno 561 */ 562 563 struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name, 564 int is_root) 565 { 566 struct super_block *sb = dir->i_sb; 567 struct gfs2_inode *dip = GFS2_I(dir); 568 struct gfs2_holder d_gh; 569 int error = 0; 570 struct inode *inode = NULL; 571 int unlock = 0; 572 573 if (!name->len || name->len > GFS2_FNAMESIZE) 574 return ERR_PTR(-ENAMETOOLONG); 575 576 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) || 577 (name->len == 2 && memcmp(name->name, "..", 2) == 0 && 578 dir == sb->s_root->d_inode)) { 579 igrab(dir); 580 return dir; 581 } 582 583 if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) { 584 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh); 585 if (error) 586 return ERR_PTR(error); 587 unlock = 1; 588 } 589 590 if (!is_root) { 591 error = gfs2_permission(dir, MAY_EXEC); 592 if (error) 593 goto out; 594 } 595 596 inode = gfs2_dir_search(dir, name); 597 if (IS_ERR(inode)) 598 error = PTR_ERR(inode); 599 out: 600 if (unlock) 601 gfs2_glock_dq_uninit(&d_gh); 602 if (error == -ENOENT) 603 return NULL; 604 return inode ? inode : ERR_PTR(error); 605 } 606 607 /** 608 * create_ok - OK to create a new on-disk inode here? 609 * @dip: Directory in which dinode is to be created 610 * @name: Name of new dinode 611 * @mode: 612 * 613 * Returns: errno 614 */ 615 616 static int create_ok(struct gfs2_inode *dip, const struct qstr *name, 617 unsigned int mode) 618 { 619 int error; 620 621 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC); 622 if (error) 623 return error; 624 625 /* Don't create entries in an unlinked directory */ 626 if (!dip->i_inode.i_nlink) 627 return -EPERM; 628 629 error = gfs2_dir_check(&dip->i_inode, name, NULL); 630 switch (error) { 631 case -ENOENT: 632 error = 0; 633 break; 634 case 0: 635 return -EEXIST; 636 default: 637 return error; 638 } 639 640 if (dip->i_entries == (u32)-1) 641 return -EFBIG; 642 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1) 643 return -EMLINK; 644 645 return 0; 646 } 647 648 static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode, 649 unsigned int *uid, unsigned int *gid) 650 { 651 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir && 652 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) { 653 if (S_ISDIR(*mode)) 654 *mode |= S_ISUID; 655 else if (dip->i_inode.i_uid != current_fsuid()) 656 *mode &= ~07111; 657 *uid = dip->i_inode.i_uid; 658 } else 659 *uid = current_fsuid(); 660 661 if (dip->i_inode.i_mode & S_ISGID) { 662 if (S_ISDIR(*mode)) 663 *mode |= S_ISGID; 664 *gid = dip->i_inode.i_gid; 665 } else 666 *gid = current_fsgid(); 667 } 668 669 static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation) 670 { 671 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 672 int error; 673 674 if (gfs2_alloc_get(dip) == NULL) 675 return -ENOMEM; 676 677 dip->i_alloc->al_requested = RES_DINODE; 678 error = gfs2_inplace_reserve(dip); 679 if (error) 680 goto out; 681 682 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0); 683 if (error) 684 goto out_ipreserv; 685 686 error = gfs2_alloc_di(dip, no_addr, generation); 687 688 gfs2_trans_end(sdp); 689 690 out_ipreserv: 691 gfs2_inplace_release(dip); 692 out: 693 gfs2_alloc_put(dip); 694 return error; 695 } 696 697 /** 698 * init_dinode - Fill in a new dinode structure 699 * @dip: the directory this inode is being created in 700 * @gl: The glock covering the new inode 701 * @inum: the inode number 702 * @mode: the file permissions 703 * @uid: 704 * @gid: 705 * 706 */ 707 708 static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl, 709 const struct gfs2_inum_host *inum, unsigned int mode, 710 unsigned int uid, unsigned int gid, 711 const u64 *generation, dev_t dev, struct buffer_head **bhp) 712 { 713 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 714 struct gfs2_dinode *di; 715 struct buffer_head *dibh; 716 struct timespec tv = CURRENT_TIME; 717 718 dibh = gfs2_meta_new(gl, inum->no_addr); 719 gfs2_trans_add_bh(gl, dibh, 1); 720 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI); 721 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); 722 di = (struct gfs2_dinode *)dibh->b_data; 723 724 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino); 725 di->di_num.no_addr = cpu_to_be64(inum->no_addr); 726 di->di_mode = cpu_to_be32(mode); 727 di->di_uid = cpu_to_be32(uid); 728 di->di_gid = cpu_to_be32(gid); 729 di->di_nlink = 0; 730 di->di_size = 0; 731 di->di_blocks = cpu_to_be64(1); 732 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec); 733 di->di_major = cpu_to_be32(MAJOR(dev)); 734 di->di_minor = cpu_to_be32(MINOR(dev)); 735 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr); 736 di->di_generation = cpu_to_be64(*generation); 737 di->di_flags = 0; 738 739 if (S_ISREG(mode)) { 740 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) || 741 gfs2_tune_get(sdp, gt_new_files_jdata)) 742 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA); 743 } else if (S_ISDIR(mode)) { 744 di->di_flags |= cpu_to_be32(dip->i_diskflags & 745 GFS2_DIF_INHERIT_JDATA); 746 } 747 748 di->__pad1 = 0; 749 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0); 750 di->di_height = 0; 751 di->__pad2 = 0; 752 di->__pad3 = 0; 753 di->di_depth = 0; 754 di->di_entries = 0; 755 memset(&di->__pad4, 0, sizeof(di->__pad4)); 756 di->di_eattr = 0; 757 di->di_atime_nsec = cpu_to_be32(tv.tv_nsec); 758 di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec); 759 di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec); 760 memset(&di->di_reserved, 0, sizeof(di->di_reserved)); 761 762 set_buffer_uptodate(dibh); 763 764 *bhp = dibh; 765 } 766 767 static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl, 768 unsigned int mode, const struct gfs2_inum_host *inum, 769 const u64 *generation, dev_t dev, struct buffer_head **bhp) 770 { 771 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 772 unsigned int uid, gid; 773 int error; 774 775 munge_mode_uid_gid(dip, &mode, &uid, &gid); 776 if (!gfs2_alloc_get(dip)) 777 return -ENOMEM; 778 779 error = gfs2_quota_lock(dip, uid, gid); 780 if (error) 781 goto out; 782 783 error = gfs2_quota_check(dip, uid, gid); 784 if (error) 785 goto out_quota; 786 787 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0); 788 if (error) 789 goto out_quota; 790 791 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp); 792 gfs2_quota_change(dip, +1, uid, gid); 793 gfs2_trans_end(sdp); 794 795 out_quota: 796 gfs2_quota_unlock(dip); 797 out: 798 gfs2_alloc_put(dip); 799 return error; 800 } 801 802 static int link_dinode(struct gfs2_inode *dip, const struct qstr *name, 803 struct gfs2_inode *ip) 804 { 805 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 806 struct gfs2_alloc *al; 807 int alloc_required; 808 struct buffer_head *dibh; 809 int error; 810 811 al = gfs2_alloc_get(dip); 812 if (!al) 813 return -ENOMEM; 814 815 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE); 816 if (error) 817 goto fail; 818 819 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name); 820 if (alloc_required < 0) 821 goto fail_quota_locks; 822 if (alloc_required) { 823 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid); 824 if (error) 825 goto fail_quota_locks; 826 827 al->al_requested = sdp->sd_max_dirres; 828 829 error = gfs2_inplace_reserve(dip); 830 if (error) 831 goto fail_quota_locks; 832 833 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres + 834 al->al_rgd->rd_length + 835 2 * RES_DINODE + 836 RES_STATFS + RES_QUOTA, 0); 837 if (error) 838 goto fail_ipreserv; 839 } else { 840 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0); 841 if (error) 842 goto fail_quota_locks; 843 } 844 845 error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode)); 846 if (error) 847 goto fail_end_trans; 848 849 error = gfs2_meta_inode_buffer(ip, &dibh); 850 if (error) 851 goto fail_end_trans; 852 ip->i_inode.i_nlink = 1; 853 gfs2_trans_add_bh(ip->i_gl, dibh, 1); 854 gfs2_dinode_out(ip, dibh->b_data); 855 brelse(dibh); 856 return 0; 857 858 fail_end_trans: 859 gfs2_trans_end(sdp); 860 861 fail_ipreserv: 862 if (dip->i_alloc->al_rgd) 863 gfs2_inplace_release(dip); 864 865 fail_quota_locks: 866 gfs2_quota_unlock(dip); 867 868 fail: 869 gfs2_alloc_put(dip); 870 return error; 871 } 872 873 static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip) 874 { 875 int err; 876 size_t len; 877 void *value; 878 char *name; 879 880 err = security_inode_init_security(&ip->i_inode, &dip->i_inode, 881 &name, &value, &len); 882 883 if (err) { 884 if (err == -EOPNOTSUPP) 885 return 0; 886 return err; 887 } 888 889 err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0, 890 GFS2_EATYPE_SECURITY); 891 kfree(value); 892 kfree(name); 893 894 return err; 895 } 896 897 /** 898 * gfs2_createi - Create a new inode 899 * @ghs: An array of two holders 900 * @name: The name of the new file 901 * @mode: the permissions on the new inode 902 * 903 * @ghs[0] is an initialized holder for the directory 904 * @ghs[1] is the holder for the inode lock 905 * 906 * If the return value is not NULL, the glocks on both the directory and the new 907 * file are held. A transaction has been started and an inplace reservation 908 * is held, as well. 909 * 910 * Returns: An inode 911 */ 912 913 struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name, 914 unsigned int mode, dev_t dev) 915 { 916 struct inode *inode = NULL; 917 struct gfs2_inode *dip = ghs->gh_gl->gl_object; 918 struct inode *dir = &dip->i_inode; 919 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 920 struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 }; 921 int error; 922 u64 generation; 923 struct buffer_head *bh = NULL; 924 925 if (!name->len || name->len > GFS2_FNAMESIZE) 926 return ERR_PTR(-ENAMETOOLONG); 927 928 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs); 929 error = gfs2_glock_nq(ghs); 930 if (error) 931 goto fail; 932 933 error = create_ok(dip, name, mode); 934 if (error) 935 goto fail_gunlock; 936 937 error = alloc_dinode(dip, &inum.no_addr, &generation); 938 if (error) 939 goto fail_gunlock; 940 inum.no_formal_ino = generation; 941 942 error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops, 943 LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1); 944 if (error) 945 goto fail_gunlock; 946 947 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh); 948 if (error) 949 goto fail_gunlock2; 950 951 inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr, 952 inum.no_formal_ino); 953 if (IS_ERR(inode)) 954 goto fail_gunlock2; 955 956 error = gfs2_inode_refresh(GFS2_I(inode)); 957 if (error) 958 goto fail_gunlock2; 959 960 error = gfs2_acl_create(dip, inode); 961 if (error) 962 goto fail_gunlock2; 963 964 error = gfs2_security_init(dip, GFS2_I(inode)); 965 if (error) 966 goto fail_gunlock2; 967 968 error = link_dinode(dip, name, GFS2_I(inode)); 969 if (error) 970 goto fail_gunlock2; 971 972 if (bh) 973 brelse(bh); 974 return inode; 975 976 fail_gunlock2: 977 gfs2_glock_dq_uninit(ghs + 1); 978 if (inode && !IS_ERR(inode)) 979 iput(inode); 980 fail_gunlock: 981 gfs2_glock_dq(ghs); 982 fail: 983 if (bh) 984 brelse(bh); 985 return ERR_PTR(error); 986 } 987 988 static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr) 989 { 990 struct buffer_head *dibh; 991 int error; 992 993 error = gfs2_meta_inode_buffer(ip, &dibh); 994 if (!error) { 995 error = inode_setattr(&ip->i_inode, attr); 996 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error); 997 gfs2_trans_add_bh(ip->i_gl, dibh, 1); 998 gfs2_dinode_out(ip, dibh->b_data); 999 brelse(dibh); 1000 } 1001 return error; 1002 } 1003 1004 /** 1005 * gfs2_setattr_simple - 1006 * @ip: 1007 * @attr: 1008 * 1009 * Called with a reference on the vnode. 1010 * 1011 * Returns: errno 1012 */ 1013 1014 int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr) 1015 { 1016 int error; 1017 1018 if (current->journal_info) 1019 return __gfs2_setattr_simple(ip, attr); 1020 1021 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0); 1022 if (error) 1023 return error; 1024 1025 error = __gfs2_setattr_simple(ip, attr); 1026 gfs2_trans_end(GFS2_SB(&ip->i_inode)); 1027 return error; 1028 } 1029 1030 void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf) 1031 { 1032 struct gfs2_dinode *str = buf; 1033 1034 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC); 1035 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI); 1036 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI); 1037 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr); 1038 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino); 1039 str->di_mode = cpu_to_be32(ip->i_inode.i_mode); 1040 str->di_uid = cpu_to_be32(ip->i_inode.i_uid); 1041 str->di_gid = cpu_to_be32(ip->i_inode.i_gid); 1042 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink); 1043 str->di_size = cpu_to_be64(ip->i_disksize); 1044 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode)); 1045 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec); 1046 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec); 1047 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec); 1048 1049 str->di_goal_meta = cpu_to_be64(ip->i_goal); 1050 str->di_goal_data = cpu_to_be64(ip->i_goal); 1051 str->di_generation = cpu_to_be64(ip->i_generation); 1052 1053 str->di_flags = cpu_to_be32(ip->i_diskflags); 1054 str->di_height = cpu_to_be16(ip->i_height); 1055 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) && 1056 !(ip->i_diskflags & GFS2_DIF_EXHASH) ? 1057 GFS2_FORMAT_DE : 0); 1058 str->di_depth = cpu_to_be16(ip->i_depth); 1059 str->di_entries = cpu_to_be32(ip->i_entries); 1060 1061 str->di_eattr = cpu_to_be64(ip->i_eattr); 1062 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec); 1063 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec); 1064 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec); 1065 } 1066 1067 void gfs2_dinode_print(const struct gfs2_inode *ip) 1068 { 1069 printk(KERN_INFO " no_formal_ino = %llu\n", 1070 (unsigned long long)ip->i_no_formal_ino); 1071 printk(KERN_INFO " no_addr = %llu\n", 1072 (unsigned long long)ip->i_no_addr); 1073 printk(KERN_INFO " i_disksize = %llu\n", 1074 (unsigned long long)ip->i_disksize); 1075 printk(KERN_INFO " blocks = %llu\n", 1076 (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode)); 1077 printk(KERN_INFO " i_goal = %llu\n", 1078 (unsigned long long)ip->i_goal); 1079 printk(KERN_INFO " i_diskflags = 0x%.8X\n", ip->i_diskflags); 1080 printk(KERN_INFO " i_height = %u\n", ip->i_height); 1081 printk(KERN_INFO " i_depth = %u\n", ip->i_depth); 1082 printk(KERN_INFO " i_entries = %u\n", ip->i_entries); 1083 printk(KERN_INFO " i_eattr = %llu\n", 1084 (unsigned long long)ip->i_eattr); 1085 } 1086 1087