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