1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. 4 * Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved. 5 */ 6 7 #include <linux/slab.h> 8 #include <linux/spinlock.h> 9 #include <linux/completion.h> 10 #include <linux/buffer_head.h> 11 #include <linux/namei.h> 12 #include <linux/mm.h> 13 #include <linux/cred.h> 14 #include <linux/xattr.h> 15 #include <linux/posix_acl.h> 16 #include <linux/gfs2_ondisk.h> 17 #include <linux/crc32.h> 18 #include <linux/iomap.h> 19 #include <linux/security.h> 20 #include <linux/fiemap.h> 21 #include <linux/uaccess.h> 22 23 #include "gfs2.h" 24 #include "incore.h" 25 #include "acl.h" 26 #include "bmap.h" 27 #include "dir.h" 28 #include "xattr.h" 29 #include "glock.h" 30 #include "inode.h" 31 #include "meta_io.h" 32 #include "quota.h" 33 #include "rgrp.h" 34 #include "trans.h" 35 #include "util.h" 36 #include "super.h" 37 #include "glops.h" 38 39 static const struct inode_operations gfs2_file_iops; 40 static const struct inode_operations gfs2_dir_iops; 41 static const struct inode_operations gfs2_symlink_iops; 42 43 /** 44 * gfs2_set_iop - Sets inode operations 45 * @inode: The inode with correct i_mode filled in 46 * 47 * GFS2 lookup code fills in vfs inode contents based on info obtained 48 * from directory entry inside gfs2_inode_lookup(). 49 */ 50 51 static void gfs2_set_iop(struct inode *inode) 52 { 53 struct gfs2_sbd *sdp = GFS2_SB(inode); 54 umode_t mode = inode->i_mode; 55 56 if (S_ISREG(mode)) { 57 inode->i_op = &gfs2_file_iops; 58 if (gfs2_localflocks(sdp)) 59 inode->i_fop = &gfs2_file_fops_nolock; 60 else 61 inode->i_fop = &gfs2_file_fops; 62 } else if (S_ISDIR(mode)) { 63 inode->i_op = &gfs2_dir_iops; 64 if (gfs2_localflocks(sdp)) 65 inode->i_fop = &gfs2_dir_fops_nolock; 66 else 67 inode->i_fop = &gfs2_dir_fops; 68 } else if (S_ISLNK(mode)) { 69 inode->i_op = &gfs2_symlink_iops; 70 } else { 71 inode->i_op = &gfs2_file_iops; 72 init_special_inode(inode, inode->i_mode, inode->i_rdev); 73 } 74 } 75 76 static int iget_test(struct inode *inode, void *opaque) 77 { 78 u64 no_addr = *(u64 *)opaque; 79 80 return GFS2_I(inode)->i_no_addr == no_addr; 81 } 82 83 static int iget_set(struct inode *inode, void *opaque) 84 { 85 u64 no_addr = *(u64 *)opaque; 86 87 GFS2_I(inode)->i_no_addr = no_addr; 88 inode->i_ino = no_addr; 89 return 0; 90 } 91 92 void gfs2_setup_inode(struct inode *inode) 93 { 94 gfp_t gfp_mask; 95 96 /* 97 * Ensure all page cache allocations are done from GFP_NOFS context to 98 * prevent direct reclaim recursion back into the filesystem and blowing 99 * stacks or deadlocking. 100 */ 101 gfp_mask = mapping_gfp_mask(inode->i_mapping); 102 mapping_set_gfp_mask(inode->i_mapping, gfp_mask & ~__GFP_FS); 103 } 104 105 /** 106 * gfs2_inode_lookup - Lookup an inode 107 * @sb: The super block 108 * @type: The type of the inode 109 * @no_addr: The inode number 110 * @no_formal_ino: The inode generation number 111 * @blktype: Requested block type (GFS2_BLKST_DINODE or GFS2_BLKST_UNLINKED; 112 * GFS2_BLKST_FREE to indicate not to verify) 113 * 114 * If @type is DT_UNKNOWN, the inode type is fetched from disk. 115 * 116 * If @blktype is anything other than GFS2_BLKST_FREE (which is used as a 117 * placeholder because it doesn't otherwise make sense), the on-disk block type 118 * is verified to be @blktype. 119 * 120 * When @no_formal_ino is non-zero, this function will return ERR_PTR(-ESTALE) 121 * if it detects that @no_formal_ino doesn't match the actual inode generation 122 * number. However, it doesn't always know unless @type is DT_UNKNOWN. 123 * 124 * Returns: A VFS inode, or an error 125 */ 126 127 struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type, 128 u64 no_addr, u64 no_formal_ino, 129 unsigned int blktype) 130 { 131 struct inode *inode; 132 struct gfs2_inode *ip; 133 struct gfs2_holder i_gh; 134 int error; 135 136 gfs2_holder_mark_uninitialized(&i_gh); 137 inode = iget5_locked(sb, no_addr, iget_test, iget_set, &no_addr); 138 if (!inode) 139 return ERR_PTR(-ENOMEM); 140 141 ip = GFS2_I(inode); 142 143 if (inode_state_read_once(inode) & I_NEW) { 144 struct gfs2_sbd *sdp = GFS2_SB(inode); 145 struct gfs2_glock *io_gl; 146 int extra_flags = 0; 147 148 gfs2_setup_inode(inode); 149 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, 150 &ip->i_gl); 151 if (unlikely(error)) 152 goto fail; 153 154 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, 155 &io_gl); 156 if (unlikely(error)) 157 goto fail; 158 159 /* 160 * The only caller that sets @blktype to GFS2_BLKST_UNLINKED is 161 * delete_work_func(). Make sure not to cancel the delete work 162 * from within itself here. 163 */ 164 if (blktype == GFS2_BLKST_UNLINKED) 165 extra_flags |= LM_FLAG_TRY; 166 else 167 gfs2_cancel_delete_work(io_gl); 168 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, 169 GL_EXACT | GL_NOPID | extra_flags, 170 &ip->i_iopen_gh); 171 gfs2_glock_put(io_gl); 172 if (unlikely(error)) 173 goto fail; 174 175 if (type == DT_UNKNOWN || blktype != GFS2_BLKST_FREE) { 176 /* 177 * The GL_SKIP flag indicates to skip reading the inode 178 * block. We read the inode when instantiating it 179 * after possibly checking the block type. 180 */ 181 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 182 GL_SKIP, &i_gh); 183 if (error) 184 goto fail; 185 186 error = -ESTALE; 187 if (no_formal_ino && 188 gfs2_inode_already_deleted(ip->i_gl, no_formal_ino)) 189 goto fail; 190 191 if (blktype != GFS2_BLKST_FREE) { 192 error = gfs2_check_blk_type(sdp, no_addr, 193 blktype); 194 if (error) 195 goto fail; 196 } 197 } 198 199 set_bit(GLF_INSTANTIATE_NEEDED, &ip->i_gl->gl_flags); 200 201 /* Lowest possible timestamp; will be overwritten in gfs2_dinode_in. */ 202 inode_set_atime(inode, 203 1LL << (8 * sizeof(inode_get_atime_sec(inode)) - 1), 204 0); 205 206 glock_set_object(ip->i_gl, ip); 207 208 if (type == DT_UNKNOWN) { 209 /* Inode glock must be locked already */ 210 error = gfs2_instantiate(&i_gh); 211 if (error) { 212 glock_clear_object(ip->i_gl, ip); 213 goto fail; 214 } 215 } else { 216 ip->i_no_formal_ino = no_formal_ino; 217 inode->i_mode = DT2IF(type); 218 } 219 220 if (gfs2_holder_initialized(&i_gh)) 221 gfs2_glock_dq_uninit(&i_gh); 222 glock_set_object(ip->i_iopen_gh.gh_gl, ip); 223 224 gfs2_set_iop(inode); 225 unlock_new_inode(inode); 226 } 227 228 if (no_formal_ino && ip->i_no_formal_ino && 229 no_formal_ino != ip->i_no_formal_ino) { 230 iput(inode); 231 return ERR_PTR(-ESTALE); 232 } 233 234 return inode; 235 236 fail: 237 if (error == GLR_TRYFAILED) 238 error = -EAGAIN; 239 if (gfs2_holder_initialized(&ip->i_iopen_gh)) 240 gfs2_glock_dq_uninit(&ip->i_iopen_gh); 241 if (gfs2_holder_initialized(&i_gh)) 242 gfs2_glock_dq_uninit(&i_gh); 243 if (ip->i_gl) { 244 gfs2_glock_put(ip->i_gl); 245 ip->i_gl = NULL; 246 } 247 iget_failed(inode); 248 return ERR_PTR(error); 249 } 250 251 /** 252 * gfs2_lookup_by_inum - look up an inode by inode number 253 * @sdp: The super block 254 * @no_addr: The inode number 255 * @no_formal_ino: The inode generation number (0 for any) 256 * @blktype: Requested block type (see gfs2_inode_lookup) 257 */ 258 struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr, 259 u64 no_formal_ino, unsigned int blktype) 260 { 261 struct super_block *sb = sdp->sd_vfs; 262 struct inode *inode; 263 int error; 264 265 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, no_formal_ino, 266 blktype); 267 if (IS_ERR(inode)) 268 return inode; 269 270 if (no_formal_ino) { 271 error = -EIO; 272 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM) 273 goto fail_iput; 274 } 275 return inode; 276 277 fail_iput: 278 iput(inode); 279 return ERR_PTR(error); 280 } 281 282 283 /** 284 * gfs2_lookup_meta - Look up an inode in a metadata directory 285 * @dip: The directory 286 * @name: The name of the inode 287 */ 288 struct inode *gfs2_lookup_meta(struct inode *dip, const char *name) 289 { 290 struct qstr qstr; 291 struct inode *inode; 292 293 gfs2_str2qstr(&qstr, name); 294 inode = gfs2_lookupi(dip, &qstr, 1); 295 if (IS_ERR_OR_NULL(inode)) 296 return inode ? inode : ERR_PTR(-ENOENT); 297 298 /* 299 * Must not call back into the filesystem when allocating 300 * pages in the metadata inode's address space. 301 */ 302 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS); 303 304 return inode; 305 } 306 307 308 /** 309 * gfs2_lookupi - Look up a filename in a directory and return its inode 310 * @dir: The inode of the directory containing the inode to look-up 311 * @name: The name of the inode to look for 312 * @is_root: If 1, ignore the caller's permissions 313 * 314 * This can be called via the VFS filldir function when NFS is doing 315 * a readdirplus and the inode which its intending to stat isn't 316 * already in cache. In this case we must not take the directory glock 317 * again, since the readdir call will have already taken that lock. 318 * 319 * Returns: errno 320 */ 321 322 struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name, 323 int is_root) 324 { 325 struct super_block *sb = dir->i_sb; 326 struct gfs2_inode *dip = GFS2_I(dir); 327 struct gfs2_holder d_gh; 328 int error = 0; 329 struct inode *inode = NULL; 330 331 gfs2_holder_mark_uninitialized(&d_gh); 332 if (!name->len || name->len > GFS2_FNAMESIZE) 333 return ERR_PTR(-ENAMETOOLONG); 334 335 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) || 336 (name->len == 2 && memcmp(name->name, "..", 2) == 0 && 337 dir == d_inode(sb->s_root))) { 338 igrab(dir); 339 return dir; 340 } 341 342 if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) { 343 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh); 344 if (error) 345 return ERR_PTR(error); 346 } 347 348 if (!is_root) { 349 error = gfs2_permission(&nop_mnt_idmap, dir, MAY_EXEC); 350 if (error) 351 goto out; 352 } 353 354 inode = gfs2_dir_search(dir, name, false); 355 if (IS_ERR(inode)) 356 error = PTR_ERR(inode); 357 out: 358 if (gfs2_holder_initialized(&d_gh)) 359 gfs2_glock_dq_uninit(&d_gh); 360 if (error == -ENOENT) 361 return NULL; 362 return inode ? inode : ERR_PTR(error); 363 } 364 365 /** 366 * create_ok - OK to create a new on-disk inode here? 367 * @dip: Directory in which dinode is to be created 368 * @name: Name of new dinode 369 * @mode: 370 * 371 * Returns: errno 372 */ 373 374 static int create_ok(struct gfs2_inode *dip, const struct qstr *name, 375 umode_t mode) 376 { 377 int error; 378 379 error = gfs2_permission(&nop_mnt_idmap, &dip->i_inode, 380 MAY_WRITE | MAY_EXEC); 381 if (error) 382 return error; 383 384 /* Don't create entries in an unlinked directory */ 385 if (!dip->i_inode.i_nlink) 386 return -ENOENT; 387 388 if (dip->i_entries == (u32)-1) 389 return -EFBIG; 390 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1) 391 return -EMLINK; 392 393 return 0; 394 } 395 396 static void munge_mode_uid_gid(const struct gfs2_inode *dip, 397 struct inode *inode) 398 { 399 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir && 400 (dip->i_inode.i_mode & S_ISUID) && 401 !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) { 402 if (S_ISDIR(inode->i_mode)) 403 inode->i_mode |= S_ISUID; 404 else if (!uid_eq(dip->i_inode.i_uid, current_fsuid())) 405 inode->i_mode &= ~07111; 406 inode->i_uid = dip->i_inode.i_uid; 407 } else 408 inode->i_uid = current_fsuid(); 409 410 if (dip->i_inode.i_mode & S_ISGID) { 411 if (S_ISDIR(inode->i_mode)) 412 inode->i_mode |= S_ISGID; 413 inode->i_gid = dip->i_inode.i_gid; 414 } else 415 inode->i_gid = current_fsgid(); 416 } 417 418 static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks) 419 { 420 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 421 struct gfs2_alloc_parms ap = { .target = *dblocks, .aflags = flags, }; 422 int error; 423 424 error = gfs2_quota_lock_check(ip, &ap); 425 if (error) 426 goto out; 427 428 error = gfs2_inplace_reserve(ip, &ap); 429 if (error) 430 goto out_quota; 431 432 error = gfs2_trans_begin(sdp, (*dblocks * RES_RG_BIT) + RES_STATFS + RES_QUOTA, 0); 433 if (error) 434 goto out_ipreserv; 435 436 error = gfs2_alloc_blocks(ip, &ip->i_no_addr, dblocks, 1); 437 if (error) 438 goto out_trans_end; 439 440 ip->i_no_formal_ino = ip->i_generation; 441 ip->i_inode.i_ino = ip->i_no_addr; 442 ip->i_goal = ip->i_no_addr; 443 if (*dblocks > 1) 444 ip->i_eattr = ip->i_no_addr + 1; 445 446 out_trans_end: 447 gfs2_trans_end(sdp); 448 out_ipreserv: 449 gfs2_inplace_release(ip); 450 out_quota: 451 gfs2_quota_unlock(ip); 452 out: 453 return error; 454 } 455 456 static void gfs2_final_release_pages(struct gfs2_inode *ip) 457 { 458 struct inode *inode = &ip->i_inode; 459 struct gfs2_glock *gl = ip->i_gl; 460 461 /* This can only happen during incomplete inode creation. */ 462 if (unlikely(!gl)) 463 return; 464 465 truncate_inode_pages(gfs2_glock2aspace(gl), 0); 466 truncate_inode_pages(&inode->i_data, 0); 467 468 if (atomic_read(&gl->gl_revokes) == 0) { 469 clear_bit(GLF_LFLUSH, &gl->gl_flags); 470 clear_bit(GLF_DIRTY, &gl->gl_flags); 471 } 472 } 473 474 int gfs2_dinode_dealloc(struct gfs2_inode *ip) 475 { 476 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 477 struct gfs2_rgrpd *rgd; 478 struct gfs2_holder gh; 479 int error; 480 481 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) { 482 gfs2_consist_inode(ip); 483 return -EIO; 484 } 485 486 gfs2_rindex_update(sdp); 487 488 error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE); 489 if (error) 490 return error; 491 492 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1); 493 if (!rgd) { 494 gfs2_consist_inode(ip); 495 error = -EIO; 496 goto out_qs; 497 } 498 499 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 500 LM_FLAG_NODE_SCOPE, &gh); 501 if (error) 502 goto out_qs; 503 504 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 505 sdp->sd_jdesc->jd_blocks); 506 if (error) 507 goto out_rg_gunlock; 508 509 gfs2_free_di(rgd, ip); 510 511 gfs2_final_release_pages(ip); 512 513 gfs2_trans_end(sdp); 514 515 out_rg_gunlock: 516 gfs2_glock_dq_uninit(&gh); 517 out_qs: 518 gfs2_quota_unhold(ip); 519 return error; 520 } 521 522 static void gfs2_init_dir(struct buffer_head *dibh, 523 const struct gfs2_inode *parent) 524 { 525 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data; 526 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1); 527 528 gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent); 529 dent->de_inum = di->di_num; /* already GFS2 endian */ 530 dent->de_type = cpu_to_be16(DT_DIR); 531 532 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1)); 533 gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent); 534 gfs2_inum_out(parent, dent); 535 dent->de_type = cpu_to_be16(DT_DIR); 536 537 } 538 539 /** 540 * gfs2_init_xattr - Initialise an xattr block for a new inode 541 * @ip: The inode in question 542 * 543 * This sets up an empty xattr block for a new inode, ready to 544 * take any ACLs, LSM xattrs, etc. 545 */ 546 547 static void gfs2_init_xattr(struct gfs2_inode *ip) 548 { 549 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 550 struct buffer_head *bh; 551 struct gfs2_ea_header *ea; 552 553 bh = gfs2_meta_new(ip->i_gl, ip->i_eattr); 554 gfs2_trans_add_meta(ip->i_gl, bh); 555 gfs2_metatype_set(bh, GFS2_METATYPE_EA, GFS2_FORMAT_EA); 556 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header)); 557 558 ea = GFS2_EA_BH2FIRST(bh); 559 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize); 560 ea->ea_type = GFS2_EATYPE_UNUSED; 561 ea->ea_flags = GFS2_EAFLAG_LAST; 562 563 brelse(bh); 564 } 565 566 /** 567 * init_dinode - Fill in a new dinode structure 568 * @dip: The directory this inode is being created in 569 * @ip: The inode 570 * @symname: The symlink destination (if a symlink) 571 * 572 */ 573 574 static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip, 575 const char *symname) 576 { 577 struct gfs2_dinode *di; 578 struct buffer_head *dibh; 579 580 dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr); 581 gfs2_trans_add_meta(ip->i_gl, dibh); 582 di = (struct gfs2_dinode *)dibh->b_data; 583 gfs2_dinode_out(ip, di); 584 585 di->di_major = cpu_to_be32(imajor(&ip->i_inode)); 586 di->di_minor = cpu_to_be32(iminor(&ip->i_inode)); 587 di->__pad1 = 0; 588 di->__pad2 = 0; 589 di->__pad3 = 0; 590 memset(&di->__pad4, 0, sizeof(di->__pad4)); 591 memset(&di->di_reserved, 0, sizeof(di->di_reserved)); 592 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode)); 593 594 switch(ip->i_inode.i_mode & S_IFMT) { 595 case S_IFDIR: 596 gfs2_init_dir(dibh, dip); 597 break; 598 case S_IFLNK: 599 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size); 600 break; 601 } 602 603 set_buffer_uptodate(dibh); 604 brelse(dibh); 605 } 606 607 /** 608 * gfs2_trans_da_blks - Calculate number of blocks to link inode 609 * @dip: The directory we are linking into 610 * @da: The dir add information 611 * @nr_inodes: The number of inodes involved 612 * 613 * This calculate the number of blocks we need to reserve in a 614 * transaction to link @nr_inodes into a directory. In most cases 615 * @nr_inodes will be 2 (the directory plus the inode being linked in) 616 * but in case of rename, 4 may be required. 617 * 618 * Returns: Number of blocks 619 */ 620 621 static unsigned gfs2_trans_da_blks(const struct gfs2_inode *dip, 622 const struct gfs2_diradd *da, 623 unsigned nr_inodes) 624 { 625 return da->nr_blocks + gfs2_rg_blocks(dip, da->nr_blocks) + 626 (nr_inodes * RES_DINODE) + RES_QUOTA + RES_STATFS; 627 } 628 629 static int link_dinode(struct gfs2_inode *dip, const struct qstr *name, 630 struct gfs2_inode *ip, struct gfs2_diradd *da) 631 { 632 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 633 struct gfs2_alloc_parms ap = { .target = da->nr_blocks, }; 634 int error; 635 636 if (da->nr_blocks) { 637 error = gfs2_quota_lock_check(dip, &ap); 638 if (error) 639 goto fail_quota_locks; 640 641 error = gfs2_inplace_reserve(dip, &ap); 642 if (error) 643 goto fail_quota_locks; 644 645 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0); 646 if (error) 647 goto fail_ipreserv; 648 } else { 649 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0); 650 if (error) 651 goto fail_quota_locks; 652 } 653 654 error = gfs2_dir_add(&dip->i_inode, name, ip, da); 655 656 gfs2_trans_end(sdp); 657 fail_ipreserv: 658 gfs2_inplace_release(dip); 659 fail_quota_locks: 660 gfs2_quota_unlock(dip); 661 return error; 662 } 663 664 static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array, 665 void *fs_info) 666 { 667 const struct xattr *xattr; 668 int err = 0; 669 670 for (xattr = xattr_array; xattr->name != NULL; xattr++) { 671 err = __gfs2_xattr_set(inode, xattr->name, xattr->value, 672 xattr->value_len, 0, 673 GFS2_EATYPE_SECURITY); 674 if (err < 0) 675 break; 676 } 677 return err; 678 } 679 680 /** 681 * gfs2_create_inode - Create a new inode 682 * @dir: The parent directory 683 * @dentry: The new dentry 684 * @file: If non-NULL, the file which is being opened 685 * @mode: The permissions on the new inode 686 * @dev: For device nodes, this is the device number 687 * @symname: For symlinks, this is the link destination 688 * @size: The initial size of the inode (ignored for directories) 689 * @excl: Force fail if inode exists 690 * 691 * FIXME: Change to allocate the disk blocks and write them out in the same 692 * transaction. That way, we can no longer end up in a situation in which an 693 * inode is allocated, the node crashes, and the block looks like a valid 694 * inode. (With atomic creates in place, we will also no longer need to zero 695 * the link count and dirty the inode here on failure.) 696 * 697 * Returns: 0 on success, or error code 698 */ 699 700 static int gfs2_create_inode(struct inode *dir, struct dentry *dentry, 701 struct file *file, 702 umode_t mode, dev_t dev, const char *symname, 703 unsigned int size, int excl) 704 { 705 const struct qstr *name = &dentry->d_name; 706 struct posix_acl *default_acl, *acl; 707 struct gfs2_holder d_gh, gh; 708 struct inode *inode = NULL; 709 struct gfs2_inode *dip = GFS2_I(dir), *ip; 710 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode); 711 struct gfs2_glock *io_gl; 712 int error, dealloc_error; 713 u32 aflags = 0; 714 unsigned blocks = 1; 715 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, }; 716 bool xattr_initialized = false; 717 718 if (!name->len || name->len > GFS2_FNAMESIZE) 719 return -ENAMETOOLONG; 720 721 error = gfs2_qa_get(dip); 722 if (error) 723 return error; 724 725 error = gfs2_rindex_update(sdp); 726 if (error) 727 goto fail; 728 729 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, &d_gh); 730 if (error) 731 goto fail; 732 gfs2_holder_mark_uninitialized(&gh); 733 734 error = create_ok(dip, name, mode); 735 if (error) 736 goto fail_gunlock; 737 738 inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl); 739 error = PTR_ERR(inode); 740 if (!IS_ERR(inode)) { 741 if (file && (file->f_flags & __O_REGULAR) && 742 !S_ISREG(inode->i_mode)) { 743 iput(inode); 744 inode = NULL; 745 error = -EFTYPE; 746 goto fail_gunlock; 747 } 748 if (S_ISDIR(inode->i_mode)) { 749 iput(inode); 750 inode = NULL; 751 error = -EISDIR; 752 goto fail_gunlock; 753 } 754 d_instantiate(dentry, inode); 755 error = 0; 756 if (file) { 757 if (S_ISREG(inode->i_mode)) 758 error = finish_open(file, dentry, gfs2_open_common); 759 else 760 error = finish_no_open(file, NULL); 761 } 762 gfs2_glock_dq_uninit(&d_gh); 763 goto fail; 764 } else if (error != -ENOENT) { 765 goto fail_gunlock; 766 } 767 768 error = gfs2_diradd_alloc_required(dir, name, &da); 769 if (error < 0) 770 goto fail_gunlock; 771 772 inode = new_inode(sdp->sd_vfs); 773 error = -ENOMEM; 774 if (!inode) 775 goto fail_gunlock; 776 gfs2_setup_inode(inode); 777 ip = GFS2_I(inode); 778 779 error = posix_acl_create(dir, &mode, &default_acl, &acl); 780 if (error) 781 goto fail_gunlock; 782 783 error = gfs2_qa_get(ip); 784 if (error) 785 goto fail_free_acls; 786 787 inode->i_mode = mode; 788 set_nlink(inode, S_ISDIR(mode) ? 2 : 1); 789 inode->i_rdev = dev; 790 inode->i_size = size; 791 simple_inode_init_ts(inode); 792 munge_mode_uid_gid(dip, inode); 793 check_and_update_goal(dip); 794 ip->i_goal = dip->i_goal; 795 ip->i_diskflags = 0; 796 ip->i_eattr = 0; 797 ip->i_height = 0; 798 ip->i_depth = 0; 799 ip->i_entries = 0; 800 ip->i_no_addr = 0; /* Temporarily zero until real addr is assigned */ 801 802 switch(mode & S_IFMT) { 803 case S_IFREG: 804 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) || 805 gfs2_tune_get(sdp, gt_new_files_jdata)) 806 ip->i_diskflags |= GFS2_DIF_JDATA; 807 gfs2_set_aops(inode); 808 break; 809 case S_IFDIR: 810 ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA); 811 ip->i_diskflags |= GFS2_DIF_JDATA; 812 ip->i_entries = 2; 813 break; 814 } 815 816 /* Force SYSTEM flag on all files and subdirs of a SYSTEM directory */ 817 if (dip->i_diskflags & GFS2_DIF_SYSTEM) 818 ip->i_diskflags |= GFS2_DIF_SYSTEM; 819 820 gfs2_set_inode_flags(inode); 821 822 if ((GFS2_I(d_inode(sdp->sd_root_dir)) == dip) || 823 (dip->i_diskflags & GFS2_DIF_TOPDIR)) 824 aflags |= GFS2_AF_ORLOV; 825 826 if (default_acl || acl) 827 blocks++; 828 829 error = alloc_dinode(ip, aflags, &blocks); 830 if (error) 831 goto fail_free_inode; 832 833 gfs2_set_inode_blocks(inode, blocks); 834 835 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl); 836 if (error) 837 goto fail_dealloc_inode; 838 839 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl); 840 if (error) 841 goto fail_dealloc_inode; 842 gfs2_cancel_delete_work(io_gl); 843 io_gl->gl_no_formal_ino = ip->i_no_formal_ino; 844 845 retry: 846 error = insert_inode_locked4(inode, ip->i_no_addr, iget_test, &ip->i_no_addr); 847 if (error == -EBUSY) 848 goto retry; 849 if (error) 850 goto fail_gunlock2; 851 852 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT | GL_NOPID, 853 &ip->i_iopen_gh); 854 if (error) 855 goto fail_gunlock2; 856 857 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, &gh); 858 if (error) 859 goto fail_gunlock3; 860 clear_bit(GLF_INSTANTIATE_NEEDED, &ip->i_gl->gl_flags); 861 862 error = gfs2_trans_begin(sdp, blocks, 0); 863 if (error) 864 goto fail_gunlock3; 865 866 if (blocks > 1) { 867 gfs2_init_xattr(ip); 868 xattr_initialized = true; 869 } 870 init_dinode(dip, ip, symname); 871 gfs2_trans_end(sdp); 872 873 glock_set_object(ip->i_gl, ip); 874 glock_set_object(io_gl, ip); 875 gfs2_set_iop(inode); 876 877 if (default_acl) { 878 error = __gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT); 879 if (error) 880 goto fail_gunlock4; 881 posix_acl_release(default_acl); 882 default_acl = NULL; 883 } 884 if (acl) { 885 error = __gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS); 886 if (error) 887 goto fail_gunlock4; 888 posix_acl_release(acl); 889 acl = NULL; 890 } 891 892 error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name, 893 &gfs2_initxattrs, NULL); 894 if (error) 895 goto fail_gunlock4; 896 897 error = link_dinode(dip, name, ip, &da); 898 if (error) 899 goto fail_gunlock4; 900 901 mark_inode_dirty(inode); 902 d_instantiate_new(dentry, inode); 903 /* After instantiate, errors should result in evict which will destroy 904 * both inode and iopen glocks properly. */ 905 if (file) { 906 file->f_mode |= FMODE_CREATED; 907 error = finish_open(file, dentry, gfs2_open_common); 908 } 909 gfs2_glock_dq_uninit(&d_gh); 910 gfs2_qa_put(ip); 911 gfs2_glock_dq_uninit(&gh); 912 gfs2_glock_put(io_gl); 913 gfs2_qa_put(dip); 914 return error; 915 916 fail_gunlock4: 917 glock_clear_object(ip->i_gl, ip); 918 glock_clear_object(io_gl, ip); 919 fail_gunlock3: 920 gfs2_glock_dq_uninit(&ip->i_iopen_gh); 921 fail_gunlock2: 922 gfs2_glock_put(io_gl); 923 fail_dealloc_inode: 924 dealloc_error = 0; 925 if (ip->i_eattr) 926 dealloc_error = gfs2_ea_dealloc(ip, xattr_initialized); 927 clear_nlink(inode); 928 mark_inode_dirty(inode); 929 if (!dealloc_error) 930 dealloc_error = gfs2_dinode_dealloc(ip); 931 if (dealloc_error) 932 fs_warn(sdp, "%s: %d\n", __func__, dealloc_error); 933 ip->i_no_addr = 0; 934 fail_free_inode: 935 if (ip->i_gl) { 936 gfs2_glock_put(ip->i_gl); 937 ip->i_gl = NULL; 938 } 939 gfs2_rs_deltree(&ip->i_res); 940 gfs2_qa_put(ip); 941 fail_free_acls: 942 posix_acl_release(default_acl); 943 posix_acl_release(acl); 944 fail_gunlock: 945 gfs2_dir_no_add(&da); 946 gfs2_glock_dq_uninit(&d_gh); 947 if (!IS_ERR_OR_NULL(inode)) { 948 if (inode_state_read_once(inode) & I_NEW) 949 iget_failed(inode); 950 else 951 iput(inode); 952 } 953 if (gfs2_holder_initialized(&gh)) 954 gfs2_glock_dq_uninit(&gh); 955 fail: 956 gfs2_qa_put(dip); 957 return error; 958 } 959 960 /** 961 * gfs2_create - Create a file 962 * @idmap: idmap of the mount the inode was found from 963 * @dir: The directory in which to create the file 964 * @dentry: The dentry of the new file 965 * @mode: The mode of the new file 966 * @excl: Force fail if inode exists 967 * 968 * Returns: errno 969 */ 970 971 static int gfs2_create(struct mnt_idmap *idmap, struct inode *dir, 972 struct dentry *dentry, umode_t mode, bool excl) 973 { 974 return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl); 975 } 976 977 /** 978 * __gfs2_lookup - Look up a filename in a directory and return its inode 979 * @dir: The directory inode 980 * @dentry: The dentry of the new inode 981 * @file: File to be opened 982 * 983 * 984 * Returns: errno 985 */ 986 987 static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry, 988 struct file *file) 989 { 990 struct inode *inode; 991 struct dentry *d; 992 struct gfs2_holder gh; 993 struct gfs2_glock *gl; 994 int error; 995 996 inode = gfs2_lookupi(dir, &dentry->d_name, 0); 997 if (inode == NULL || IS_ERR(inode)) 998 return d_splice_alias(inode, dentry); 999 1000 gl = GFS2_I(inode)->i_gl; 1001 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); 1002 if (error) { 1003 iput(inode); 1004 return ERR_PTR(error); 1005 } 1006 1007 d = d_splice_alias(inode, dentry); 1008 if (IS_ERR(d)) { 1009 gfs2_glock_dq_uninit(&gh); 1010 return d; 1011 } 1012 if (file && S_ISREG(inode->i_mode)) 1013 error = finish_open(file, dentry, gfs2_open_common); 1014 1015 gfs2_glock_dq_uninit(&gh); 1016 if (error) { 1017 dput(d); 1018 return ERR_PTR(error); 1019 } 1020 return d; 1021 } 1022 1023 static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry, 1024 unsigned flags) 1025 { 1026 return __gfs2_lookup(dir, dentry, NULL); 1027 } 1028 1029 /** 1030 * gfs2_link - Link to a file 1031 * @old_dentry: The inode to link 1032 * @dir: Add link to this directory 1033 * @dentry: The name of the link 1034 * 1035 * Link the inode in "old_dentry" into the directory "dir" with the 1036 * name in "dentry". 1037 * 1038 * Returns: errno 1039 */ 1040 1041 static int gfs2_link(struct dentry *old_dentry, struct inode *dir, 1042 struct dentry *dentry) 1043 { 1044 struct gfs2_inode *dip = GFS2_I(dir); 1045 struct gfs2_sbd *sdp = GFS2_SB(dir); 1046 struct inode *inode = d_inode(old_dentry); 1047 struct gfs2_inode *ip = GFS2_I(inode); 1048 struct gfs2_holder d_gh, gh; 1049 struct buffer_head *dibh; 1050 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, }; 1051 int error; 1052 1053 if (S_ISDIR(inode->i_mode)) 1054 return -EPERM; 1055 1056 error = gfs2_qa_get(dip); 1057 if (error) 1058 return error; 1059 1060 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, &d_gh); 1061 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); 1062 1063 error = gfs2_glock_nq(&d_gh); 1064 if (error) 1065 goto out_parent; 1066 1067 error = gfs2_glock_nq(&gh); 1068 if (error) 1069 goto out_child; 1070 1071 error = -ENOENT; 1072 if (inode->i_nlink == 0) 1073 goto out_gunlock; 1074 1075 error = gfs2_permission(&nop_mnt_idmap, dir, MAY_WRITE | MAY_EXEC); 1076 if (error) 1077 goto out_gunlock; 1078 1079 error = gfs2_dir_check(dir, &dentry->d_name, NULL); 1080 switch (error) { 1081 case -ENOENT: 1082 break; 1083 case 0: 1084 error = -EEXIST; 1085 goto out_gunlock; 1086 default: 1087 goto out_gunlock; 1088 } 1089 1090 error = -EINVAL; 1091 if (!dip->i_inode.i_nlink) 1092 goto out_gunlock; 1093 error = -EFBIG; 1094 if (dip->i_entries == (u32)-1) 1095 goto out_gunlock; 1096 error = -EPERM; 1097 if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) 1098 goto out_gunlock; 1099 error = -EMLINK; 1100 if (ip->i_inode.i_nlink == (u32)-1) 1101 goto out_gunlock; 1102 1103 error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da); 1104 if (error < 0) 1105 goto out_gunlock; 1106 1107 if (da.nr_blocks) { 1108 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, }; 1109 error = gfs2_quota_lock_check(dip, &ap); 1110 if (error) 1111 goto out_gunlock; 1112 1113 error = gfs2_inplace_reserve(dip, &ap); 1114 if (error) 1115 goto out_gunlock_q; 1116 1117 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0); 1118 if (error) 1119 goto out_ipres; 1120 } else { 1121 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0); 1122 if (error) 1123 goto out_ipres; 1124 } 1125 1126 error = gfs2_meta_inode_buffer(ip, &dibh); 1127 if (error) 1128 goto out_end_trans; 1129 1130 error = gfs2_dir_add(dir, &dentry->d_name, ip, &da); 1131 if (error) 1132 goto out_brelse; 1133 1134 gfs2_trans_add_meta(ip->i_gl, dibh); 1135 inc_nlink(&ip->i_inode); 1136 inode_set_ctime_current(&ip->i_inode); 1137 ihold(inode); 1138 d_instantiate(dentry, inode); 1139 mark_inode_dirty(inode); 1140 1141 out_brelse: 1142 brelse(dibh); 1143 out_end_trans: 1144 gfs2_trans_end(sdp); 1145 out_ipres: 1146 if (da.nr_blocks) 1147 gfs2_inplace_release(dip); 1148 out_gunlock_q: 1149 if (da.nr_blocks) 1150 gfs2_quota_unlock(dip); 1151 out_gunlock: 1152 gfs2_dir_no_add(&da); 1153 gfs2_glock_dq(&gh); 1154 out_child: 1155 gfs2_glock_dq(&d_gh); 1156 out_parent: 1157 gfs2_qa_put(dip); 1158 gfs2_holder_uninit(&d_gh); 1159 gfs2_holder_uninit(&gh); 1160 return error; 1161 } 1162 1163 /* 1164 * gfs2_unlink_ok - check to see that a inode is still in a directory 1165 * @dip: the directory 1166 * @name: the name of the file 1167 * @ip: the inode 1168 * 1169 * Assumes that the lock on (at least) @dip is held. 1170 * 1171 * Returns: 0 if the parent/child relationship is correct, errno if it isn't 1172 */ 1173 1174 static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name, 1175 const struct gfs2_inode *ip) 1176 { 1177 int error; 1178 1179 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode)) 1180 return -EPERM; 1181 1182 if ((dip->i_inode.i_mode & S_ISVTX) && 1183 !uid_eq(dip->i_inode.i_uid, current_fsuid()) && 1184 !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER)) 1185 return -EPERM; 1186 1187 if (IS_APPEND(&dip->i_inode)) 1188 return -EPERM; 1189 1190 error = gfs2_permission(&nop_mnt_idmap, &dip->i_inode, 1191 MAY_WRITE | MAY_EXEC); 1192 if (error) 1193 return error; 1194 1195 return gfs2_dir_check(&dip->i_inode, name, ip); 1196 } 1197 1198 /** 1199 * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it 1200 * @dip: The parent directory 1201 * @dentry: The dentry to unlink 1202 * 1203 * Called with all the locks and in a transaction. This will only be 1204 * called for a directory after it has been checked to ensure it is empty. 1205 * 1206 * Returns: 0 on success, or an error 1207 */ 1208 1209 static int gfs2_unlink_inode(struct gfs2_inode *dip, 1210 const struct dentry *dentry) 1211 { 1212 struct inode *inode = d_inode(dentry); 1213 struct gfs2_inode *ip = GFS2_I(inode); 1214 int error; 1215 1216 error = gfs2_dir_del(dip, dentry); 1217 if (error) 1218 return error; 1219 1220 ip->i_entries = 0; 1221 inode_set_ctime_current(inode); 1222 if (S_ISDIR(inode->i_mode)) 1223 clear_nlink(inode); 1224 else 1225 drop_nlink(inode); 1226 mark_inode_dirty(inode); 1227 if (inode->i_nlink == 0) 1228 gfs2_unlink_di(inode); 1229 return 0; 1230 } 1231 1232 1233 /** 1234 * gfs2_unlink - Unlink an inode (this does rmdir as well) 1235 * @dir: The inode of the directory containing the inode to unlink 1236 * @dentry: The file itself 1237 * 1238 * This routine uses the type of the inode as a flag to figure out 1239 * whether this is an unlink or an rmdir. 1240 * 1241 * Returns: errno 1242 */ 1243 1244 static int gfs2_unlink(struct inode *dir, struct dentry *dentry) 1245 { 1246 struct gfs2_inode *dip = GFS2_I(dir); 1247 struct gfs2_sbd *sdp = GFS2_SB(dir); 1248 struct inode *inode = d_inode(dentry); 1249 struct gfs2_inode *ip = GFS2_I(inode); 1250 struct gfs2_holder d_gh, r_gh, gh; 1251 struct gfs2_rgrpd *rgd; 1252 int error; 1253 1254 error = gfs2_rindex_update(sdp); 1255 if (error) 1256 return error; 1257 1258 error = -EROFS; 1259 1260 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, &d_gh); 1261 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); 1262 1263 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1); 1264 if (!rgd) 1265 goto out_inodes; 1266 1267 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, LM_FLAG_NODE_SCOPE, &r_gh); 1268 1269 1270 error = gfs2_glock_nq(&d_gh); 1271 if (error) 1272 goto out_parent; 1273 1274 error = gfs2_glock_nq(&gh); 1275 if (error) 1276 goto out_child; 1277 1278 error = -ENOENT; 1279 if (inode->i_nlink == 0) 1280 goto out_rgrp; 1281 1282 if (S_ISDIR(inode->i_mode)) { 1283 error = -ENOTEMPTY; 1284 if (ip->i_entries > 2 || inode->i_nlink > 2) 1285 goto out_rgrp; 1286 } 1287 1288 error = gfs2_glock_nq(&r_gh); /* rgrp */ 1289 if (error) 1290 goto out_rgrp; 1291 1292 error = gfs2_unlink_ok(dip, &dentry->d_name, ip); 1293 if (error) 1294 goto out_gunlock; 1295 1296 error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0); 1297 if (error) 1298 goto out_gunlock; 1299 1300 error = gfs2_unlink_inode(dip, dentry); 1301 gfs2_trans_end(sdp); 1302 1303 out_gunlock: 1304 gfs2_glock_dq(&r_gh); 1305 out_rgrp: 1306 gfs2_glock_dq(&gh); 1307 out_child: 1308 gfs2_glock_dq(&d_gh); 1309 out_parent: 1310 gfs2_holder_uninit(&r_gh); 1311 out_inodes: 1312 gfs2_holder_uninit(&gh); 1313 gfs2_holder_uninit(&d_gh); 1314 return error; 1315 } 1316 1317 /** 1318 * gfs2_symlink - Create a symlink 1319 * @idmap: idmap of the mount the inode was found from 1320 * @dir: The directory to create the symlink in 1321 * @dentry: The dentry to put the symlink in 1322 * @symname: The thing which the link points to 1323 * 1324 * Returns: errno 1325 */ 1326 1327 static int gfs2_symlink(struct mnt_idmap *idmap, struct inode *dir, 1328 struct dentry *dentry, const char *symname) 1329 { 1330 unsigned int size; 1331 1332 size = strlen(symname); 1333 if (size >= gfs2_max_stuffed_size(GFS2_I(dir))) 1334 return -ENAMETOOLONG; 1335 1336 return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0); 1337 } 1338 1339 /** 1340 * gfs2_mkdir - Make a directory 1341 * @idmap: idmap of the mount the inode was found from 1342 * @dir: The parent directory of the new one 1343 * @dentry: The dentry of the new directory 1344 * @mode: The mode of the new directory 1345 * 1346 * Returns: the dentry, or ERR_PTR(errno) 1347 */ 1348 1349 static struct dentry *gfs2_mkdir(struct mnt_idmap *idmap, struct inode *dir, 1350 struct dentry *dentry, umode_t mode) 1351 { 1352 unsigned dsize = gfs2_max_stuffed_size(GFS2_I(dir)); 1353 1354 return ERR_PTR(gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0)); 1355 } 1356 1357 /** 1358 * gfs2_mknod - Make a special file 1359 * @idmap: idmap of the mount the inode was found from 1360 * @dir: The directory in which the special file will reside 1361 * @dentry: The dentry of the special file 1362 * @mode: The mode of the special file 1363 * @dev: The device specification of the special file 1364 * 1365 */ 1366 1367 static int gfs2_mknod(struct mnt_idmap *idmap, struct inode *dir, 1368 struct dentry *dentry, umode_t mode, dev_t dev) 1369 { 1370 return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0); 1371 } 1372 1373 /** 1374 * gfs2_atomic_open - Atomically open a file 1375 * @dir: The directory 1376 * @dentry: The proposed new entry 1377 * @file: The proposed new struct file 1378 * @flags: open flags 1379 * @mode: File mode 1380 * 1381 * Returns: error code or 0 for success 1382 */ 1383 1384 static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry, 1385 struct file *file, unsigned flags, 1386 umode_t mode) 1387 { 1388 bool excl = !!(flags & O_EXCL); 1389 1390 if (d_in_lookup(dentry)) { 1391 struct dentry *d = __gfs2_lookup(dir, dentry, file); 1392 if (file->f_mode & FMODE_OPENED) { 1393 if (IS_ERR(d)) 1394 return PTR_ERR(d); 1395 dput(d); 1396 return excl && (flags & O_CREAT) ? -EEXIST : 0; 1397 } 1398 if (d || d_really_is_positive(dentry)) 1399 return finish_no_open(file, d); 1400 } 1401 if (!(flags & O_CREAT)) 1402 return -ENOENT; 1403 1404 return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl); 1405 } 1406 1407 /* 1408 * gfs2_ok_to_move - check if it's ok to move a directory to another directory 1409 * @this: move this 1410 * @to: to here 1411 * 1412 * Follow @to back to the root and make sure we don't encounter @this 1413 * Assumes we already hold the rename lock. 1414 * 1415 * Returns: errno 1416 */ 1417 1418 static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to) 1419 { 1420 struct inode *dir = &to->i_inode; 1421 struct super_block *sb = dir->i_sb; 1422 struct inode *tmp; 1423 int error = 0; 1424 1425 igrab(dir); 1426 1427 for (;;) { 1428 if (dir == &this->i_inode) { 1429 error = -EINVAL; 1430 break; 1431 } 1432 if (dir == d_inode(sb->s_root)) { 1433 error = 0; 1434 break; 1435 } 1436 1437 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1); 1438 if (!tmp) { 1439 error = -ENOENT; 1440 break; 1441 } 1442 if (IS_ERR(tmp)) { 1443 error = PTR_ERR(tmp); 1444 break; 1445 } 1446 1447 iput(dir); 1448 dir = tmp; 1449 } 1450 1451 iput(dir); 1452 1453 return error; 1454 } 1455 1456 /** 1457 * update_moved_ino - Update an inode that's being moved 1458 * @ip: The inode being moved 1459 * @ndip: The parent directory of the new filename 1460 * @dir_rename: True of ip is a directory 1461 * 1462 * Returns: errno 1463 */ 1464 1465 static int update_moved_ino(struct gfs2_inode *ip, struct gfs2_inode *ndip, 1466 int dir_rename) 1467 { 1468 if (dir_rename) 1469 return gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR); 1470 1471 inode_set_ctime_current(&ip->i_inode); 1472 mark_inode_dirty_sync(&ip->i_inode); 1473 return 0; 1474 } 1475 1476 1477 /** 1478 * gfs2_rename - Rename a file 1479 * @odir: Parent directory of old file name 1480 * @odentry: The old dentry of the file 1481 * @ndir: Parent directory of new file name 1482 * @ndentry: The new dentry of the file 1483 * 1484 * Returns: errno 1485 */ 1486 1487 static int gfs2_rename(struct inode *odir, struct dentry *odentry, 1488 struct inode *ndir, struct dentry *ndentry) 1489 { 1490 struct gfs2_inode *odip = GFS2_I(odir); 1491 struct gfs2_inode *ndip = GFS2_I(ndir); 1492 struct gfs2_inode *ip = GFS2_I(d_inode(odentry)); 1493 struct gfs2_inode *nip = NULL; 1494 struct gfs2_sbd *sdp = GFS2_SB(odir); 1495 struct gfs2_holder ghs[4], r_gh, rd_gh; 1496 struct gfs2_rgrpd *nrgd; 1497 unsigned int num_gh; 1498 int dir_rename = 0; 1499 struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, }; 1500 unsigned int retries = 0, x; 1501 int error; 1502 1503 gfs2_holder_mark_uninitialized(&r_gh); 1504 gfs2_holder_mark_uninitialized(&rd_gh); 1505 if (d_really_is_positive(ndentry)) { 1506 nip = GFS2_I(d_inode(ndentry)); 1507 if (ip == nip) 1508 return 0; 1509 } 1510 1511 error = gfs2_rindex_update(sdp); 1512 if (error) 1513 return error; 1514 1515 error = gfs2_qa_get(ndip); 1516 if (error) 1517 return error; 1518 1519 if (odip != ndip) { 1520 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 1521 0, &r_gh); 1522 if (error) 1523 goto out; 1524 1525 if (S_ISDIR(ip->i_inode.i_mode)) { 1526 dir_rename = 1; 1527 /* don't move a directory into its subdir */ 1528 error = gfs2_ok_to_move(ip, ndip); 1529 if (error) 1530 goto out_gunlock_r; 1531 } 1532 } 1533 1534 num_gh = 1; 1535 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs); 1536 if (odip != ndip) { 1537 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE,GL_ASYNC, 1538 ghs + num_gh); 1539 num_gh++; 1540 } 1541 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh); 1542 num_gh++; 1543 1544 if (nip) { 1545 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, 1546 ghs + num_gh); 1547 num_gh++; 1548 } 1549 1550 again: 1551 for (x = 0; x < num_gh; x++) { 1552 error = gfs2_glock_nq(ghs + x); 1553 if (error) 1554 goto out_gunlock; 1555 } 1556 error = gfs2_glock_async_wait(num_gh, ghs, retries); 1557 if (error == -ESTALE) { 1558 retries++; 1559 goto again; 1560 } 1561 if (error) 1562 goto out_gunlock; 1563 1564 if (nip) { 1565 /* Grab the resource group glock for unlink flag twiddling. 1566 * This is the case where the target dinode already exists 1567 * so we unlink before doing the rename. 1568 */ 1569 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1); 1570 if (!nrgd) { 1571 error = -ENOENT; 1572 goto out_gunlock; 1573 } 1574 error = gfs2_glock_nq_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 1575 LM_FLAG_NODE_SCOPE, &rd_gh); 1576 if (error) 1577 goto out_gunlock; 1578 } 1579 1580 error = -ENOENT; 1581 if (ip->i_inode.i_nlink == 0) 1582 goto out_gunlock; 1583 1584 /* Check out the old directory */ 1585 1586 error = gfs2_unlink_ok(odip, &odentry->d_name, ip); 1587 if (error) 1588 goto out_gunlock; 1589 1590 /* Check out the new directory */ 1591 1592 if (nip) { 1593 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip); 1594 if (error) 1595 goto out_gunlock; 1596 1597 if (nip->i_inode.i_nlink == 0) { 1598 error = -EAGAIN; 1599 goto out_gunlock; 1600 } 1601 1602 if (S_ISDIR(nip->i_inode.i_mode)) { 1603 if (nip->i_entries < 2) { 1604 gfs2_consist_inode(nip); 1605 error = -EIO; 1606 goto out_gunlock; 1607 } 1608 if (nip->i_entries > 2) { 1609 error = -ENOTEMPTY; 1610 goto out_gunlock; 1611 } 1612 } 1613 } else { 1614 error = gfs2_permission(&nop_mnt_idmap, ndir, 1615 MAY_WRITE | MAY_EXEC); 1616 if (error) 1617 goto out_gunlock; 1618 1619 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL); 1620 switch (error) { 1621 case -ENOENT: 1622 error = 0; 1623 break; 1624 case 0: 1625 error = -EEXIST; 1626 goto out_gunlock; 1627 default: 1628 goto out_gunlock; 1629 } 1630 1631 if (odip != ndip) { 1632 if (!ndip->i_inode.i_nlink) { 1633 error = -ENOENT; 1634 goto out_gunlock; 1635 } 1636 if (ndip->i_entries == (u32)-1) { 1637 error = -EFBIG; 1638 goto out_gunlock; 1639 } 1640 if (S_ISDIR(ip->i_inode.i_mode) && 1641 ndip->i_inode.i_nlink == (u32)-1) { 1642 error = -EMLINK; 1643 goto out_gunlock; 1644 } 1645 } 1646 } 1647 1648 /* Check out the dir to be renamed */ 1649 1650 if (dir_rename) { 1651 error = gfs2_permission(&nop_mnt_idmap, d_inode(odentry), 1652 MAY_WRITE); 1653 if (error) 1654 goto out_gunlock; 1655 } 1656 1657 if (nip == NULL) { 1658 error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da); 1659 if (error) 1660 goto out_gunlock; 1661 } 1662 1663 if (da.nr_blocks) { 1664 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, }; 1665 error = gfs2_quota_lock_check(ndip, &ap); 1666 if (error) 1667 goto out_gunlock; 1668 1669 error = gfs2_inplace_reserve(ndip, &ap); 1670 if (error) 1671 goto out_gunlock_q; 1672 1673 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) + 1674 4 * RES_LEAF + 4, 0); 1675 if (error) 1676 goto out_ipreserv; 1677 } else { 1678 error = gfs2_trans_begin(sdp, 4 * RES_DINODE + 1679 5 * RES_LEAF + 4, 0); 1680 if (error) 1681 goto out_gunlock; 1682 } 1683 1684 /* Remove the target file, if it exists */ 1685 1686 if (nip) 1687 error = gfs2_unlink_inode(ndip, ndentry); 1688 1689 error = update_moved_ino(ip, ndip, dir_rename); 1690 if (error) 1691 goto out_end_trans; 1692 1693 error = gfs2_dir_del(odip, odentry); 1694 if (error) 1695 goto out_end_trans; 1696 1697 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da); 1698 if (error) 1699 goto out_end_trans; 1700 1701 out_end_trans: 1702 gfs2_trans_end(sdp); 1703 out_ipreserv: 1704 if (da.nr_blocks) 1705 gfs2_inplace_release(ndip); 1706 out_gunlock_q: 1707 if (da.nr_blocks) 1708 gfs2_quota_unlock(ndip); 1709 out_gunlock: 1710 gfs2_dir_no_add(&da); 1711 if (gfs2_holder_initialized(&rd_gh)) 1712 gfs2_glock_dq_uninit(&rd_gh); 1713 1714 while (x--) { 1715 if (gfs2_holder_queued(ghs + x)) 1716 gfs2_glock_dq(ghs + x); 1717 gfs2_holder_uninit(ghs + x); 1718 } 1719 out_gunlock_r: 1720 if (gfs2_holder_initialized(&r_gh)) 1721 gfs2_glock_dq_uninit(&r_gh); 1722 out: 1723 gfs2_qa_put(ndip); 1724 return error; 1725 } 1726 1727 /** 1728 * gfs2_exchange - exchange two files 1729 * @odir: Parent directory of old file name 1730 * @odentry: The old dentry of the file 1731 * @ndir: Parent directory of new file name 1732 * @ndentry: The new dentry of the file 1733 * @flags: The rename flags 1734 * 1735 * Returns: errno 1736 */ 1737 1738 static int gfs2_exchange(struct inode *odir, struct dentry *odentry, 1739 struct inode *ndir, struct dentry *ndentry, 1740 unsigned int flags) 1741 { 1742 struct gfs2_inode *odip = GFS2_I(odir); 1743 struct gfs2_inode *ndip = GFS2_I(ndir); 1744 struct gfs2_inode *oip = GFS2_I(odentry->d_inode); 1745 struct gfs2_inode *nip = GFS2_I(ndentry->d_inode); 1746 struct gfs2_sbd *sdp = GFS2_SB(odir); 1747 struct gfs2_holder ghs[4], r_gh; 1748 unsigned int num_gh; 1749 unsigned int retries = 0, x; 1750 umode_t old_mode = oip->i_inode.i_mode; 1751 umode_t new_mode = nip->i_inode.i_mode; 1752 int error; 1753 1754 gfs2_holder_mark_uninitialized(&r_gh); 1755 error = gfs2_rindex_update(sdp); 1756 if (error) 1757 return error; 1758 1759 if (odip != ndip) { 1760 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE, 1761 0, &r_gh); 1762 if (error) 1763 goto out; 1764 1765 if (S_ISDIR(old_mode)) { 1766 /* don't move a directory into its subdir */ 1767 error = gfs2_ok_to_move(oip, ndip); 1768 if (error) 1769 goto out_gunlock_r; 1770 } 1771 1772 if (S_ISDIR(new_mode)) { 1773 /* don't move a directory into its subdir */ 1774 error = gfs2_ok_to_move(nip, odip); 1775 if (error) 1776 goto out_gunlock_r; 1777 } 1778 } 1779 1780 num_gh = 1; 1781 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs); 1782 if (odip != ndip) { 1783 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, 1784 ghs + num_gh); 1785 num_gh++; 1786 } 1787 gfs2_holder_init(oip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh); 1788 num_gh++; 1789 1790 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh); 1791 num_gh++; 1792 1793 again: 1794 for (x = 0; x < num_gh; x++) { 1795 error = gfs2_glock_nq(ghs + x); 1796 if (error) 1797 goto out_gunlock; 1798 } 1799 1800 error = gfs2_glock_async_wait(num_gh, ghs, retries); 1801 if (error == -ESTALE) { 1802 retries++; 1803 goto again; 1804 } 1805 if (error) 1806 goto out_gunlock; 1807 1808 error = -ENOENT; 1809 if (oip->i_inode.i_nlink == 0 || nip->i_inode.i_nlink == 0) 1810 goto out_gunlock; 1811 1812 error = gfs2_unlink_ok(odip, &odentry->d_name, oip); 1813 if (error) 1814 goto out_gunlock; 1815 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip); 1816 if (error) 1817 goto out_gunlock; 1818 1819 if (S_ISDIR(old_mode)) { 1820 error = gfs2_permission(&nop_mnt_idmap, odentry->d_inode, 1821 MAY_WRITE); 1822 if (error) 1823 goto out_gunlock; 1824 } 1825 if (S_ISDIR(new_mode)) { 1826 error = gfs2_permission(&nop_mnt_idmap, ndentry->d_inode, 1827 MAY_WRITE); 1828 if (error) 1829 goto out_gunlock; 1830 } 1831 error = gfs2_trans_begin(sdp, 4 * RES_DINODE + 4 * RES_LEAF, 0); 1832 if (error) 1833 goto out_gunlock; 1834 1835 error = update_moved_ino(oip, ndip, S_ISDIR(old_mode)); 1836 if (error) 1837 goto out_end_trans; 1838 1839 error = update_moved_ino(nip, odip, S_ISDIR(new_mode)); 1840 if (error) 1841 goto out_end_trans; 1842 1843 error = gfs2_dir_mvino(ndip, &ndentry->d_name, oip, 1844 IF2DT(old_mode)); 1845 if (error) 1846 goto out_end_trans; 1847 1848 error = gfs2_dir_mvino(odip, &odentry->d_name, nip, 1849 IF2DT(new_mode)); 1850 if (error) 1851 goto out_end_trans; 1852 1853 if (odip != ndip) { 1854 if (S_ISDIR(new_mode) && !S_ISDIR(old_mode)) { 1855 inc_nlink(&odip->i_inode); 1856 drop_nlink(&ndip->i_inode); 1857 } else if (S_ISDIR(old_mode) && !S_ISDIR(new_mode)) { 1858 inc_nlink(&ndip->i_inode); 1859 drop_nlink(&odip->i_inode); 1860 } 1861 } 1862 mark_inode_dirty(&ndip->i_inode); 1863 if (odip != ndip) 1864 mark_inode_dirty(&odip->i_inode); 1865 1866 out_end_trans: 1867 gfs2_trans_end(sdp); 1868 out_gunlock: 1869 while (x--) { 1870 if (gfs2_holder_queued(ghs + x)) 1871 gfs2_glock_dq(ghs + x); 1872 gfs2_holder_uninit(ghs + x); 1873 } 1874 out_gunlock_r: 1875 if (gfs2_holder_initialized(&r_gh)) 1876 gfs2_glock_dq_uninit(&r_gh); 1877 out: 1878 return error; 1879 } 1880 1881 static int gfs2_rename2(struct mnt_idmap *idmap, struct inode *odir, 1882 struct dentry *odentry, struct inode *ndir, 1883 struct dentry *ndentry, unsigned int flags) 1884 { 1885 flags &= ~RENAME_NOREPLACE; 1886 1887 if (flags & ~RENAME_EXCHANGE) 1888 return -EINVAL; 1889 1890 if (flags & RENAME_EXCHANGE) 1891 return gfs2_exchange(odir, odentry, ndir, ndentry, flags); 1892 1893 return gfs2_rename(odir, odentry, ndir, ndentry); 1894 } 1895 1896 /** 1897 * gfs2_get_link - Follow a symbolic link 1898 * @dentry: The dentry of the link 1899 * @inode: The inode of the link 1900 * @done: destructor for return value 1901 * 1902 * This can handle symlinks of any size. 1903 * 1904 * Returns: 0 on success or error code 1905 */ 1906 1907 static const char *gfs2_get_link(struct dentry *dentry, 1908 struct inode *inode, 1909 struct delayed_call *done) 1910 { 1911 struct gfs2_inode *ip = GFS2_I(inode); 1912 struct gfs2_holder i_gh; 1913 struct buffer_head *dibh; 1914 unsigned int size; 1915 char *buf; 1916 int error; 1917 1918 if (!dentry) 1919 return ERR_PTR(-ECHILD); 1920 1921 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh); 1922 error = gfs2_glock_nq(&i_gh); 1923 if (error) { 1924 gfs2_holder_uninit(&i_gh); 1925 return ERR_PTR(error); 1926 } 1927 1928 size = (unsigned int)i_size_read(&ip->i_inode); 1929 if (size == 0) { 1930 gfs2_consist_inode(ip); 1931 buf = ERR_PTR(-EIO); 1932 goto out; 1933 } 1934 1935 error = gfs2_meta_inode_buffer(ip, &dibh); 1936 if (error) { 1937 buf = ERR_PTR(error); 1938 goto out; 1939 } 1940 1941 buf = kzalloc(size + 1, GFP_NOFS); 1942 if (!buf) 1943 buf = ERR_PTR(-ENOMEM); 1944 else 1945 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size); 1946 brelse(dibh); 1947 out: 1948 gfs2_glock_dq_uninit(&i_gh); 1949 if (!IS_ERR(buf)) 1950 set_delayed_call(done, kfree_link, buf); 1951 return buf; 1952 } 1953 1954 /** 1955 * gfs2_permission 1956 * @idmap: idmap of the mount the inode was found from 1957 * @inode: The inode 1958 * @mask: The mask to be tested 1959 * 1960 * This may be called from the VFS directly, or from within GFS2 with the 1961 * inode locked, so we look to see if the glock is already locked and only 1962 * lock the glock if its not already been done. 1963 * 1964 * Returns: errno 1965 */ 1966 1967 int gfs2_permission(struct mnt_idmap *idmap, struct inode *inode, 1968 int mask) 1969 { 1970 int may_not_block = mask & MAY_NOT_BLOCK; 1971 struct gfs2_inode *ip; 1972 struct gfs2_holder i_gh; 1973 struct gfs2_glock *gl; 1974 int error; 1975 1976 gfs2_holder_mark_uninitialized(&i_gh); 1977 ip = GFS2_I(inode); 1978 gl = rcu_dereference_check(ip->i_gl, !may_not_block); 1979 if (unlikely(!gl)) { 1980 /* inode is getting torn down, must be RCU mode */ 1981 WARN_ON_ONCE(!may_not_block); 1982 return -ECHILD; 1983 } 1984 if (gfs2_glock_is_locked_by_me(gl) == NULL) { 1985 if (may_not_block) 1986 return -ECHILD; 1987 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh); 1988 if (error) 1989 return error; 1990 } 1991 1992 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode)) 1993 error = -EPERM; 1994 else 1995 error = generic_permission(&nop_mnt_idmap, inode, mask); 1996 if (gfs2_holder_initialized(&i_gh)) 1997 gfs2_glock_dq_uninit(&i_gh); 1998 1999 return error; 2000 } 2001 2002 static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr) 2003 { 2004 setattr_copy(&nop_mnt_idmap, inode, attr); 2005 mark_inode_dirty(inode); 2006 return 0; 2007 } 2008 2009 static int gfs2_setattr_simple(struct inode *inode, struct iattr *attr) 2010 { 2011 int error; 2012 2013 if (current->journal_info) 2014 return __gfs2_setattr_simple(inode, attr); 2015 2016 error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0); 2017 if (error) 2018 return error; 2019 2020 error = __gfs2_setattr_simple(inode, attr); 2021 gfs2_trans_end(GFS2_SB(inode)); 2022 return error; 2023 } 2024 2025 static int setattr_chown(struct inode *inode, struct iattr *attr) 2026 { 2027 struct gfs2_inode *ip = GFS2_I(inode); 2028 struct gfs2_sbd *sdp = GFS2_SB(inode); 2029 kuid_t ouid, nuid; 2030 kgid_t ogid, ngid; 2031 int error; 2032 struct gfs2_alloc_parms ap = {}; 2033 2034 ouid = inode->i_uid; 2035 ogid = inode->i_gid; 2036 nuid = attr->ia_uid; 2037 ngid = attr->ia_gid; 2038 2039 if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid)) 2040 ouid = nuid = NO_UID_QUOTA_CHANGE; 2041 if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid)) 2042 ogid = ngid = NO_GID_QUOTA_CHANGE; 2043 error = gfs2_qa_get(ip); 2044 if (error) 2045 return error; 2046 2047 error = gfs2_rindex_update(sdp); 2048 if (error) 2049 goto out; 2050 2051 error = gfs2_quota_lock(ip, nuid, ngid); 2052 if (error) 2053 goto out; 2054 2055 ap.target = gfs2_get_inode_blocks(&ip->i_inode); 2056 2057 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) || 2058 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) { 2059 error = gfs2_quota_check(ip, nuid, ngid, &ap); 2060 if (error) 2061 goto out_gunlock_q; 2062 } 2063 2064 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0); 2065 if (error) 2066 goto out_gunlock_q; 2067 2068 error = gfs2_setattr_simple(inode, attr); 2069 if (error) 2070 goto out_end_trans; 2071 2072 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) || 2073 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) { 2074 gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid); 2075 gfs2_quota_change(ip, ap.target, nuid, ngid); 2076 } 2077 2078 out_end_trans: 2079 gfs2_trans_end(sdp); 2080 out_gunlock_q: 2081 gfs2_quota_unlock(ip); 2082 out: 2083 gfs2_qa_put(ip); 2084 return error; 2085 } 2086 2087 /** 2088 * gfs2_setattr - Change attributes on an inode 2089 * @idmap: idmap of the mount the inode was found from 2090 * @dentry: The dentry which is changing 2091 * @attr: The structure describing the change 2092 * 2093 * The VFS layer wants to change one or more of an inodes attributes. Write 2094 * that change out to disk. 2095 * 2096 * Returns: errno 2097 */ 2098 2099 static int gfs2_setattr(struct mnt_idmap *idmap, 2100 struct dentry *dentry, struct iattr *attr) 2101 { 2102 struct inode *inode = d_inode(dentry); 2103 struct gfs2_inode *ip = GFS2_I(inode); 2104 struct gfs2_holder i_gh; 2105 int error; 2106 2107 error = gfs2_qa_get(ip); 2108 if (error) 2109 return error; 2110 2111 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh); 2112 if (error) 2113 goto out; 2114 2115 error = may_setattr(&nop_mnt_idmap, inode, attr->ia_valid); 2116 if (error) 2117 goto error; 2118 2119 error = setattr_prepare(&nop_mnt_idmap, dentry, attr); 2120 if (error) 2121 goto error; 2122 2123 if (attr->ia_valid & ATTR_SIZE) 2124 error = gfs2_setattr_size(inode, attr->ia_size); 2125 else if (attr->ia_valid & (ATTR_UID | ATTR_GID)) 2126 error = setattr_chown(inode, attr); 2127 else { 2128 error = gfs2_setattr_simple(inode, attr); 2129 if (!error && attr->ia_valid & ATTR_MODE) 2130 error = posix_acl_chmod(&nop_mnt_idmap, dentry, 2131 inode->i_mode); 2132 } 2133 2134 error: 2135 if (!error) 2136 mark_inode_dirty(inode); 2137 gfs2_glock_dq_uninit(&i_gh); 2138 out: 2139 gfs2_qa_put(ip); 2140 return error; 2141 } 2142 2143 /** 2144 * gfs2_getattr - Read out an inode's attributes 2145 * @idmap: idmap of the mount the inode was found from 2146 * @path: Object to query 2147 * @stat: The inode's stats 2148 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests 2149 * @flags: AT_STATX_xxx setting 2150 * 2151 * This may be called from the VFS directly, or from within GFS2 with the 2152 * inode locked, so we look to see if the glock is already locked and only 2153 * lock the glock if its not already been done. Note that its the NFS 2154 * readdirplus operation which causes this to be called (from filldir) 2155 * with the glock already held. 2156 * 2157 * Returns: errno 2158 */ 2159 2160 static int gfs2_getattr(struct mnt_idmap *idmap, 2161 const struct path *path, struct kstat *stat, 2162 u32 request_mask, unsigned int flags) 2163 { 2164 struct inode *inode = d_inode(path->dentry); 2165 struct gfs2_inode *ip = GFS2_I(inode); 2166 struct gfs2_holder gh; 2167 u32 gfsflags; 2168 int error; 2169 2170 gfs2_holder_mark_uninitialized(&gh); 2171 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) { 2172 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); 2173 if (error) 2174 return error; 2175 } 2176 2177 gfsflags = ip->i_diskflags; 2178 if (gfsflags & GFS2_DIF_APPENDONLY) 2179 stat->attributes |= STATX_ATTR_APPEND; 2180 if (gfsflags & GFS2_DIF_IMMUTABLE) 2181 stat->attributes |= STATX_ATTR_IMMUTABLE; 2182 2183 stat->attributes_mask |= (STATX_ATTR_APPEND | 2184 STATX_ATTR_COMPRESSED | 2185 STATX_ATTR_ENCRYPTED | 2186 STATX_ATTR_IMMUTABLE | 2187 STATX_ATTR_NODUMP); 2188 2189 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat); 2190 2191 if (gfs2_holder_initialized(&gh)) 2192 gfs2_glock_dq_uninit(&gh); 2193 2194 return 0; 2195 } 2196 2197 static bool fault_in_fiemap(struct fiemap_extent_info *fi) 2198 { 2199 struct fiemap_extent __user *dest = fi->fi_extents_start; 2200 size_t size = sizeof(*dest) * fi->fi_extents_max; 2201 2202 return fault_in_safe_writeable((char __user *)dest, size) == 0; 2203 } 2204 2205 static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 2206 u64 start, u64 len) 2207 { 2208 struct gfs2_inode *ip = GFS2_I(inode); 2209 struct gfs2_holder gh; 2210 int ret; 2211 2212 inode_lock_shared(inode); 2213 2214 retry: 2215 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh); 2216 if (ret) 2217 goto out; 2218 2219 pagefault_disable(); 2220 ret = iomap_fiemap(inode, fieinfo, start, len, &gfs2_iomap_ops); 2221 pagefault_enable(); 2222 2223 gfs2_glock_dq_uninit(&gh); 2224 2225 if (ret == -EFAULT && fault_in_fiemap(fieinfo)) { 2226 fieinfo->fi_extents_mapped = 0; 2227 goto retry; 2228 } 2229 2230 out: 2231 inode_unlock_shared(inode); 2232 return ret; 2233 } 2234 2235 loff_t gfs2_seek_data(struct file *file, loff_t offset) 2236 { 2237 struct inode *inode = file->f_mapping->host; 2238 struct gfs2_inode *ip = GFS2_I(inode); 2239 struct gfs2_holder gh; 2240 loff_t ret; 2241 2242 inode_lock_shared(inode); 2243 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh); 2244 if (!ret) 2245 ret = iomap_seek_data(inode, offset, &gfs2_iomap_ops); 2246 gfs2_glock_dq_uninit(&gh); 2247 inode_unlock_shared(inode); 2248 2249 if (ret < 0) 2250 return ret; 2251 return vfs_setpos(file, ret, inode->i_sb->s_maxbytes); 2252 } 2253 2254 loff_t gfs2_seek_hole(struct file *file, loff_t offset) 2255 { 2256 struct inode *inode = file->f_mapping->host; 2257 struct gfs2_inode *ip = GFS2_I(inode); 2258 struct gfs2_holder gh; 2259 loff_t ret; 2260 2261 inode_lock_shared(inode); 2262 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh); 2263 if (!ret) 2264 ret = iomap_seek_hole(inode, offset, &gfs2_iomap_ops); 2265 gfs2_glock_dq_uninit(&gh); 2266 inode_unlock_shared(inode); 2267 2268 if (ret < 0) 2269 return ret; 2270 return vfs_setpos(file, ret, inode->i_sb->s_maxbytes); 2271 } 2272 2273 static int gfs2_update_time(struct inode *inode, enum fs_update_time type, 2274 unsigned int flags) 2275 { 2276 struct gfs2_inode *ip = GFS2_I(inode); 2277 struct gfs2_glock *gl = ip->i_gl; 2278 struct gfs2_holder *gh; 2279 int error; 2280 2281 if (flags & IOCB_NOWAIT) 2282 return -EAGAIN; 2283 2284 gh = gfs2_glock_is_locked_by_me(gl); 2285 if (gh && gl->gl_state != LM_ST_EXCLUSIVE) { 2286 gfs2_glock_dq(gh); 2287 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, gh); 2288 error = gfs2_glock_nq(gh); 2289 if (error) 2290 return error; 2291 } 2292 return generic_update_time(inode, type, flags); 2293 } 2294 2295 static const struct inode_operations gfs2_file_iops = { 2296 .permission = gfs2_permission, 2297 .setattr = gfs2_setattr, 2298 .getattr = gfs2_getattr, 2299 .listxattr = gfs2_listxattr, 2300 .fiemap = gfs2_fiemap, 2301 .get_inode_acl = gfs2_get_acl, 2302 .set_acl = gfs2_set_acl, 2303 .update_time = gfs2_update_time, 2304 .fileattr_get = gfs2_fileattr_get, 2305 .fileattr_set = gfs2_fileattr_set, 2306 }; 2307 2308 static const struct inode_operations gfs2_dir_iops = { 2309 .create = gfs2_create, 2310 .lookup = gfs2_lookup, 2311 .link = gfs2_link, 2312 .unlink = gfs2_unlink, 2313 .symlink = gfs2_symlink, 2314 .mkdir = gfs2_mkdir, 2315 .rmdir = gfs2_unlink, 2316 .mknod = gfs2_mknod, 2317 .rename = gfs2_rename2, 2318 .permission = gfs2_permission, 2319 .setattr = gfs2_setattr, 2320 .getattr = gfs2_getattr, 2321 .listxattr = gfs2_listxattr, 2322 .fiemap = gfs2_fiemap, 2323 .get_inode_acl = gfs2_get_acl, 2324 .set_acl = gfs2_set_acl, 2325 .update_time = gfs2_update_time, 2326 .atomic_open = gfs2_atomic_open, 2327 .fileattr_get = gfs2_fileattr_get, 2328 .fileattr_set = gfs2_fileattr_set, 2329 }; 2330 2331 static const struct inode_operations gfs2_symlink_iops = { 2332 .get_link = gfs2_get_link, 2333 .permission = gfs2_permission, 2334 .setattr = gfs2_setattr, 2335 .getattr = gfs2_getattr, 2336 .listxattr = gfs2_listxattr, 2337 .fiemap = gfs2_fiemap, 2338 }; 2339 2340