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