1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
4 * Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
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>
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>
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;
44 * gfs2_set_iop - Sets inode operations
45 * @inode: The inode with correct i_mode filled in
47 * GFS2 lookup code fills in vfs inode contents based on info obtained
48 * from directory entry inside gfs2_inode_lookup().
51 static void gfs2_set_iop(struct inode *inode)
53 struct gfs2_sbd *sdp = GFS2_SB(inode);
54 umode_t mode = inode->i_mode;
57 inode->i_op = &gfs2_file_iops;
58 if (gfs2_localflocks(sdp))
59 inode->i_fop = &gfs2_file_fops_nolock;
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;
67 inode->i_fop = &gfs2_dir_fops;
68 } else if (S_ISLNK(mode)) {
69 inode->i_op = &gfs2_symlink_iops;
71 inode->i_op = &gfs2_file_iops;
72 init_special_inode(inode, inode->i_mode, inode->i_rdev);
76 static int iget_test(struct inode *inode, void *opaque)
78 u64 no_addr = *(u64 *)opaque;
80 return GFS2_I(inode)->i_no_addr == no_addr;
83 static int iget_set(struct inode *inode, void *opaque)
85 u64 no_addr = *(u64 *)opaque;
87 GFS2_I(inode)->i_no_addr = no_addr;
88 inode->i_ino = no_addr;
93 * gfs2_inode_lookup - Lookup an inode
94 * @sb: The super block
95 * @type: The type of the inode
96 * @no_addr: The inode number
97 * @no_formal_ino: The inode generation number
98 * @blktype: Requested block type (GFS2_BLKST_DINODE or GFS2_BLKST_UNLINKED;
99 * GFS2_BLKST_FREE to indicate not to verify)
101 * If @type is DT_UNKNOWN, the inode type is fetched from disk.
103 * If @blktype is anything other than GFS2_BLKST_FREE (which is used as a
104 * placeholder because it doesn't otherwise make sense), the on-disk block type
105 * is verified to be @blktype.
107 * When @no_formal_ino is non-zero, this function will return ERR_PTR(-ESTALE)
108 * if it detects that @no_formal_ino doesn't match the actual inode generation
109 * number. However, it doesn't always know unless @type is DT_UNKNOWN.
111 * Returns: A VFS inode, or an error
114 struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
115 u64 no_addr, u64 no_formal_ino,
116 unsigned int blktype)
119 struct gfs2_inode *ip;
120 struct gfs2_holder i_gh;
123 gfs2_holder_mark_uninitialized(&i_gh);
124 inode = iget5_locked(sb, no_addr, iget_test, iget_set, &no_addr);
126 return ERR_PTR(-ENOMEM);
130 if (inode->i_state & I_NEW) {
131 struct gfs2_sbd *sdp = GFS2_SB(inode);
132 struct gfs2_glock *io_gl;
134 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE,
139 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE,
144 if (blktype != GFS2_BLKST_UNLINKED)
145 gfs2_cancel_delete_work(io_gl);
146 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT,
148 gfs2_glock_put(io_gl);
152 if (type == DT_UNKNOWN || blktype != GFS2_BLKST_FREE) {
154 * The GL_SKIP flag indicates to skip reading the inode
155 * block. We read the inode when instantiating it
156 * after possibly checking the block type.
158 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE,
165 gfs2_inode_already_deleted(ip->i_gl, no_formal_ino))
168 if (blktype != GFS2_BLKST_FREE) {
169 error = gfs2_check_blk_type(sdp, no_addr,
176 set_bit(GLF_INSTANTIATE_NEEDED, &ip->i_gl->gl_flags);
178 /* Lowest possible timestamp; will be overwritten in gfs2_dinode_in. */
179 inode->i_atime.tv_sec = 1LL << (8 * sizeof(inode->i_atime.tv_sec) - 1);
180 inode->i_atime.tv_nsec = 0;
182 glock_set_object(ip->i_gl, ip);
184 if (type == DT_UNKNOWN) {
185 /* Inode glock must be locked already */
186 error = gfs2_instantiate(&i_gh);
188 glock_clear_object(ip->i_gl, ip);
192 ip->i_no_formal_ino = no_formal_ino;
193 inode->i_mode = DT2IF(type);
196 if (gfs2_holder_initialized(&i_gh))
197 gfs2_glock_dq_uninit(&i_gh);
198 glock_set_object(ip->i_iopen_gh.gh_gl, ip);
201 unlock_new_inode(inode);
204 if (no_formal_ino && ip->i_no_formal_ino &&
205 no_formal_ino != ip->i_no_formal_ino) {
207 return ERR_PTR(-ESTALE);
213 if (gfs2_holder_initialized(&ip->i_iopen_gh))
214 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
215 if (gfs2_holder_initialized(&i_gh))
216 gfs2_glock_dq_uninit(&i_gh);
218 return ERR_PTR(error);
222 * gfs2_lookup_by_inum - look up an inode by inode number
223 * @sdp: The super block
224 * @no_addr: The inode number
225 * @no_formal_ino: The inode generation number (0 for any)
226 * @blktype: Requested block type (see gfs2_inode_lookup)
228 struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
229 u64 no_formal_ino, unsigned int blktype)
231 struct super_block *sb = sdp->sd_vfs;
235 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, no_formal_ino,
242 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
249 return ERR_PTR(error);
253 struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
257 gfs2_str2qstr(&qstr, name);
258 inode = gfs2_lookupi(dip, &qstr, 1);
259 /* gfs2_lookupi has inconsistent callers: vfs
260 * related routines expect NULL for no entry found,
261 * gfs2_lookup_simple callers expect ENOENT
262 * and do not check for NULL.
265 return ERR_PTR(-ENOENT);
272 * gfs2_lookupi - Look up a filename in a directory and return its inode
273 * @dir: The inode of the directory containing the inode to look-up
274 * @name: The name of the inode to look for
275 * @is_root: If 1, ignore the caller's permissions
277 * This can be called via the VFS filldir function when NFS is doing
278 * a readdirplus and the inode which its intending to stat isn't
279 * already in cache. In this case we must not take the directory glock
280 * again, since the readdir call will have already taken that lock.
285 struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
288 struct super_block *sb = dir->i_sb;
289 struct gfs2_inode *dip = GFS2_I(dir);
290 struct gfs2_holder d_gh;
292 struct inode *inode = NULL;
294 gfs2_holder_mark_uninitialized(&d_gh);
295 if (!name->len || name->len > GFS2_FNAMESIZE)
296 return ERR_PTR(-ENAMETOOLONG);
298 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
299 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
300 dir == d_inode(sb->s_root))) {
305 if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
306 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
308 return ERR_PTR(error);
312 error = gfs2_permission(&init_user_ns, dir, MAY_EXEC);
317 inode = gfs2_dir_search(dir, name, false);
319 error = PTR_ERR(inode);
321 if (gfs2_holder_initialized(&d_gh))
322 gfs2_glock_dq_uninit(&d_gh);
323 if (error == -ENOENT)
325 return inode ? inode : ERR_PTR(error);
329 * create_ok - OK to create a new on-disk inode here?
330 * @dip: Directory in which dinode is to be created
331 * @name: Name of new dinode
337 static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
342 error = gfs2_permission(&init_user_ns, &dip->i_inode,
343 MAY_WRITE | MAY_EXEC);
347 /* Don't create entries in an unlinked directory */
348 if (!dip->i_inode.i_nlink)
351 if (dip->i_entries == (u32)-1)
353 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
359 static void munge_mode_uid_gid(const struct gfs2_inode *dip,
362 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
363 (dip->i_inode.i_mode & S_ISUID) &&
364 !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
365 if (S_ISDIR(inode->i_mode))
366 inode->i_mode |= S_ISUID;
367 else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
368 inode->i_mode &= ~07111;
369 inode->i_uid = dip->i_inode.i_uid;
371 inode->i_uid = current_fsuid();
373 if (dip->i_inode.i_mode & S_ISGID) {
374 if (S_ISDIR(inode->i_mode))
375 inode->i_mode |= S_ISGID;
376 inode->i_gid = dip->i_inode.i_gid;
378 inode->i_gid = current_fsgid();
381 static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks)
383 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
384 struct gfs2_alloc_parms ap = { .target = *dblocks, .aflags = flags, };
387 error = gfs2_quota_lock_check(ip, &ap);
391 error = gfs2_inplace_reserve(ip, &ap);
395 error = gfs2_trans_begin(sdp, (*dblocks * RES_RG_BIT) + RES_STATFS + RES_QUOTA, 0);
399 error = gfs2_alloc_blocks(ip, &ip->i_no_addr, dblocks, 1, &ip->i_generation);
400 ip->i_no_formal_ino = ip->i_generation;
401 ip->i_inode.i_ino = ip->i_no_addr;
402 ip->i_goal = ip->i_no_addr;
407 gfs2_inplace_release(ip);
409 gfs2_quota_unlock(ip);
414 static void gfs2_init_dir(struct buffer_head *dibh,
415 const struct gfs2_inode *parent)
417 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
418 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
420 gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
421 dent->de_inum = di->di_num; /* already GFS2 endian */
422 dent->de_type = cpu_to_be16(DT_DIR);
424 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
425 gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
426 gfs2_inum_out(parent, dent);
427 dent->de_type = cpu_to_be16(DT_DIR);
432 * gfs2_init_xattr - Initialise an xattr block for a new inode
433 * @ip: The inode in question
435 * This sets up an empty xattr block for a new inode, ready to
436 * take any ACLs, LSM xattrs, etc.
439 static void gfs2_init_xattr(struct gfs2_inode *ip)
441 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
442 struct buffer_head *bh;
443 struct gfs2_ea_header *ea;
445 bh = gfs2_meta_new(ip->i_gl, ip->i_eattr);
446 gfs2_trans_add_meta(ip->i_gl, bh);
447 gfs2_metatype_set(bh, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
448 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
450 ea = GFS2_EA_BH2FIRST(bh);
451 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
452 ea->ea_type = GFS2_EATYPE_UNUSED;
453 ea->ea_flags = GFS2_EAFLAG_LAST;
459 * init_dinode - Fill in a new dinode structure
460 * @dip: The directory this inode is being created in
462 * @symname: The symlink destination (if a symlink)
466 static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
469 struct gfs2_dinode *di;
470 struct buffer_head *dibh;
472 dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
473 gfs2_trans_add_meta(ip->i_gl, dibh);
474 di = (struct gfs2_dinode *)dibh->b_data;
475 gfs2_dinode_out(ip, di);
477 di->di_major = cpu_to_be32(imajor(&ip->i_inode));
478 di->di_minor = cpu_to_be32(iminor(&ip->i_inode));
482 memset(&di->__pad4, 0, sizeof(di->__pad4));
483 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
484 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
486 switch(ip->i_inode.i_mode & S_IFMT) {
488 gfs2_init_dir(dibh, dip);
491 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
495 set_buffer_uptodate(dibh);
500 * gfs2_trans_da_blks - Calculate number of blocks to link inode
501 * @dip: The directory we are linking into
502 * @da: The dir add information
503 * @nr_inodes: The number of inodes involved
505 * This calculate the number of blocks we need to reserve in a
506 * transaction to link @nr_inodes into a directory. In most cases
507 * @nr_inodes will be 2 (the directory plus the inode being linked in)
508 * but in case of rename, 4 may be required.
510 * Returns: Number of blocks
513 static unsigned gfs2_trans_da_blks(const struct gfs2_inode *dip,
514 const struct gfs2_diradd *da,
517 return da->nr_blocks + gfs2_rg_blocks(dip, da->nr_blocks) +
518 (nr_inodes * RES_DINODE) + RES_QUOTA + RES_STATFS;
521 static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
522 struct gfs2_inode *ip, struct gfs2_diradd *da)
524 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
525 struct gfs2_alloc_parms ap = { .target = da->nr_blocks, };
529 error = gfs2_quota_lock_check(dip, &ap);
531 goto fail_quota_locks;
533 error = gfs2_inplace_reserve(dip, &ap);
535 goto fail_quota_locks;
537 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0);
541 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
543 goto fail_quota_locks;
546 error = gfs2_dir_add(&dip->i_inode, name, ip, da);
550 gfs2_inplace_release(dip);
552 gfs2_quota_unlock(dip);
556 static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
559 const struct xattr *xattr;
562 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
563 err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
565 GFS2_EATYPE_SECURITY);
573 * gfs2_create_inode - Create a new inode
574 * @dir: The parent directory
575 * @dentry: The new dentry
576 * @file: If non-NULL, the file which is being opened
577 * @mode: The permissions on the new inode
578 * @dev: For device nodes, this is the device number
579 * @symname: For symlinks, this is the link destination
580 * @size: The initial size of the inode (ignored for directories)
581 * @excl: Force fail if inode exists
583 * Returns: 0 on success, or error code
586 static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
588 umode_t mode, dev_t dev, const char *symname,
589 unsigned int size, int excl)
591 const struct qstr *name = &dentry->d_name;
592 struct posix_acl *default_acl, *acl;
593 struct gfs2_holder ghs[2];
594 struct inode *inode = NULL;
595 struct gfs2_inode *dip = GFS2_I(dir), *ip;
596 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
597 struct gfs2_glock *io_gl;
598 int error, free_vfs_inode = 1;
601 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
603 if (!name->len || name->len > GFS2_FNAMESIZE)
604 return -ENAMETOOLONG;
606 error = gfs2_qa_get(dip);
610 error = gfs2_rindex_update(sdp);
614 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
617 gfs2_holder_mark_uninitialized(ghs + 1);
619 error = create_ok(dip, name, mode);
623 inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
624 error = PTR_ERR(inode);
625 if (!IS_ERR(inode)) {
626 if (S_ISDIR(inode->i_mode)) {
628 inode = ERR_PTR(-EISDIR);
631 d_instantiate(dentry, inode);
634 if (S_ISREG(inode->i_mode))
635 error = finish_open(file, dentry, gfs2_open_common);
637 error = finish_no_open(file, NULL);
639 gfs2_glock_dq_uninit(ghs);
641 } else if (error != -ENOENT) {
645 error = gfs2_diradd_alloc_required(dir, name, &da);
649 inode = new_inode(sdp->sd_vfs);
654 error = posix_acl_create(dir, &mode, &default_acl, &acl);
659 error = gfs2_qa_get(ip);
663 inode->i_mode = mode;
664 set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
666 inode->i_size = size;
667 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
668 munge_mode_uid_gid(dip, inode);
669 check_and_update_goal(dip);
670 ip->i_goal = dip->i_goal;
676 ip->i_no_addr = 0; /* Temporarily zero until real addr is assigned */
678 switch(mode & S_IFMT) {
680 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
681 gfs2_tune_get(sdp, gt_new_files_jdata))
682 ip->i_diskflags |= GFS2_DIF_JDATA;
683 gfs2_set_aops(inode);
686 ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA);
687 ip->i_diskflags |= GFS2_DIF_JDATA;
692 /* Force SYSTEM flag on all files and subdirs of a SYSTEM directory */
693 if (dip->i_diskflags & GFS2_DIF_SYSTEM)
694 ip->i_diskflags |= GFS2_DIF_SYSTEM;
696 gfs2_set_inode_flags(inode);
698 if ((GFS2_I(d_inode(sdp->sd_root_dir)) == dip) ||
699 (dip->i_diskflags & GFS2_DIF_TOPDIR))
700 aflags |= GFS2_AF_ORLOV;
702 if (default_acl || acl)
705 error = alloc_dinode(ip, aflags, &blocks);
707 goto fail_free_inode;
709 gfs2_set_inode_blocks(inode, blocks);
711 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
713 goto fail_free_inode;
715 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
717 goto fail_free_inode;
718 gfs2_cancel_delete_work(io_gl);
720 error = insert_inode_locked4(inode, ip->i_no_addr, iget_test, &ip->i_no_addr);
723 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
727 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
731 error = gfs2_trans_begin(sdp, blocks, 0);
736 ip->i_eattr = ip->i_no_addr + 1;
739 init_dinode(dip, ip, symname);
742 glock_set_object(ip->i_gl, ip);
743 glock_set_object(io_gl, ip);
746 free_vfs_inode = 0; /* After this point, the inode is no longer
747 considered free. Any failures need to undo
748 the gfs2 structures. */
750 error = __gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
753 posix_acl_release(default_acl);
757 error = __gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS);
760 posix_acl_release(acl);
764 error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name,
765 &gfs2_initxattrs, NULL);
769 error = link_dinode(dip, name, ip, &da);
773 mark_inode_dirty(inode);
774 d_instantiate(dentry, inode);
775 /* After instantiate, errors should result in evict which will destroy
776 * both inode and iopen glocks properly. */
778 file->f_mode |= FMODE_CREATED;
779 error = finish_open(file, dentry, gfs2_open_common);
781 gfs2_glock_dq_uninit(ghs);
783 gfs2_glock_dq_uninit(ghs + 1);
784 gfs2_glock_put(io_gl);
786 unlock_new_inode(inode);
790 glock_clear_object(ip->i_gl, ip);
791 glock_clear_object(io_gl, ip);
793 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
795 gfs2_glock_put(io_gl);
798 if (free_vfs_inode) /* else evict will do the put for us */
799 gfs2_glock_put(ip->i_gl);
801 gfs2_rs_deltree(&ip->i_res);
804 posix_acl_release(default_acl);
805 posix_acl_release(acl);
807 gfs2_dir_no_add(&da);
808 gfs2_glock_dq_uninit(ghs);
809 if (!IS_ERR_OR_NULL(inode)) {
812 mark_inode_dirty(inode);
813 set_bit(free_vfs_inode ? GIF_FREE_VFS_INODE : GIF_ALLOC_FAILED,
814 &GFS2_I(inode)->i_flags);
815 if (inode->i_state & I_NEW)
820 if (gfs2_holder_initialized(ghs + 1))
821 gfs2_glock_dq_uninit(ghs + 1);
828 * gfs2_create - Create a file
829 * @mnt_userns: User namespace of the mount the inode was found from
830 * @dir: The directory in which to create the file
831 * @dentry: The dentry of the new file
832 * @mode: The mode of the new file
833 * @excl: Force fail if inode exists
838 static int gfs2_create(struct user_namespace *mnt_userns, struct inode *dir,
839 struct dentry *dentry, umode_t mode, bool excl)
841 return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl);
845 * __gfs2_lookup - Look up a filename in a directory and return its inode
846 * @dir: The directory inode
847 * @dentry: The dentry of the new inode
848 * @file: File to be opened
854 static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry,
859 struct gfs2_holder gh;
860 struct gfs2_glock *gl;
863 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
869 return ERR_CAST(inode);
871 gl = GFS2_I(inode)->i_gl;
872 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
875 return ERR_PTR(error);
878 d = d_splice_alias(inode, dentry);
880 gfs2_glock_dq_uninit(&gh);
883 if (file && S_ISREG(inode->i_mode))
884 error = finish_open(file, dentry, gfs2_open_common);
886 gfs2_glock_dq_uninit(&gh);
889 return ERR_PTR(error);
894 static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
897 return __gfs2_lookup(dir, dentry, NULL);
901 * gfs2_link - Link to a file
902 * @old_dentry: The inode to link
903 * @dir: Add link to this directory
904 * @dentry: The name of the link
906 * Link the inode in "old_dentry" into the directory "dir" with the
912 static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
913 struct dentry *dentry)
915 struct gfs2_inode *dip = GFS2_I(dir);
916 struct gfs2_sbd *sdp = GFS2_SB(dir);
917 struct inode *inode = d_inode(old_dentry);
918 struct gfs2_inode *ip = GFS2_I(inode);
919 struct gfs2_holder ghs[2];
920 struct buffer_head *dibh;
921 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
924 if (S_ISDIR(inode->i_mode))
927 error = gfs2_qa_get(dip);
931 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
932 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
934 error = gfs2_glock_nq(ghs); /* parent */
938 error = gfs2_glock_nq(ghs + 1); /* child */
943 if (inode->i_nlink == 0)
946 error = gfs2_permission(&init_user_ns, dir, MAY_WRITE | MAY_EXEC);
950 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
962 if (!dip->i_inode.i_nlink)
965 if (dip->i_entries == (u32)-1)
968 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
971 if (!ip->i_inode.i_nlink)
974 if (ip->i_inode.i_nlink == (u32)-1)
977 error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da);
982 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
983 error = gfs2_quota_lock_check(dip, &ap);
987 error = gfs2_inplace_reserve(dip, &ap);
991 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0);
995 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
1000 error = gfs2_meta_inode_buffer(ip, &dibh);
1004 error = gfs2_dir_add(dir, &dentry->d_name, ip, &da);
1008 gfs2_trans_add_meta(ip->i_gl, dibh);
1009 inc_nlink(&ip->i_inode);
1010 ip->i_inode.i_ctime = current_time(&ip->i_inode);
1012 d_instantiate(dentry, inode);
1013 mark_inode_dirty(inode);
1018 gfs2_trans_end(sdp);
1021 gfs2_inplace_release(dip);
1024 gfs2_quota_unlock(dip);
1026 gfs2_dir_no_add(&da);
1027 gfs2_glock_dq(ghs + 1);
1032 gfs2_holder_uninit(ghs);
1033 gfs2_holder_uninit(ghs + 1);
1038 * gfs2_unlink_ok - check to see that a inode is still in a directory
1039 * @dip: the directory
1040 * @name: the name of the file
1043 * Assumes that the lock on (at least) @dip is held.
1045 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1048 static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
1049 const struct gfs2_inode *ip)
1053 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1056 if ((dip->i_inode.i_mode & S_ISVTX) &&
1057 !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
1058 !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
1061 if (IS_APPEND(&dip->i_inode))
1064 error = gfs2_permission(&init_user_ns, &dip->i_inode,
1065 MAY_WRITE | MAY_EXEC);
1069 return gfs2_dir_check(&dip->i_inode, name, ip);
1073 * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
1074 * @dip: The parent directory
1075 * @dentry: The dentry to unlink
1077 * Called with all the locks and in a transaction. This will only be
1078 * called for a directory after it has been checked to ensure it is empty.
1080 * Returns: 0 on success, or an error
1083 static int gfs2_unlink_inode(struct gfs2_inode *dip,
1084 const struct dentry *dentry)
1086 struct inode *inode = d_inode(dentry);
1087 struct gfs2_inode *ip = GFS2_I(inode);
1090 error = gfs2_dir_del(dip, dentry);
1095 inode->i_ctime = current_time(inode);
1096 if (S_ISDIR(inode->i_mode))
1100 mark_inode_dirty(inode);
1101 if (inode->i_nlink == 0)
1102 gfs2_unlink_di(inode);
1108 * gfs2_unlink - Unlink an inode (this does rmdir as well)
1109 * @dir: The inode of the directory containing the inode to unlink
1110 * @dentry: The file itself
1112 * This routine uses the type of the inode as a flag to figure out
1113 * whether this is an unlink or an rmdir.
1118 static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
1120 struct gfs2_inode *dip = GFS2_I(dir);
1121 struct gfs2_sbd *sdp = GFS2_SB(dir);
1122 struct inode *inode = d_inode(dentry);
1123 struct gfs2_inode *ip = GFS2_I(inode);
1124 struct gfs2_holder ghs[3];
1125 struct gfs2_rgrpd *rgd;
1128 error = gfs2_rindex_update(sdp);
1134 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
1135 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
1137 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
1141 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, LM_FLAG_NODE_SCOPE, ghs + 2);
1144 error = gfs2_glock_nq(ghs); /* parent */
1148 error = gfs2_glock_nq(ghs + 1); /* child */
1153 if (inode->i_nlink == 0)
1156 if (S_ISDIR(inode->i_mode)) {
1158 if (ip->i_entries > 2 || inode->i_nlink > 2)
1162 error = gfs2_glock_nq(ghs + 2); /* rgrp */
1166 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
1170 error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
1174 error = gfs2_unlink_inode(dip, dentry);
1175 gfs2_trans_end(sdp);
1178 gfs2_glock_dq(ghs + 2);
1180 gfs2_glock_dq(ghs + 1);
1184 gfs2_holder_uninit(ghs + 2);
1186 gfs2_holder_uninit(ghs + 1);
1187 gfs2_holder_uninit(ghs);
1192 * gfs2_symlink - Create a symlink
1193 * @mnt_userns: User namespace of the mount the inode was found from
1194 * @dir: The directory to create the symlink in
1195 * @dentry: The dentry to put the symlink in
1196 * @symname: The thing which the link points to
1201 static int gfs2_symlink(struct user_namespace *mnt_userns, struct inode *dir,
1202 struct dentry *dentry, const char *symname)
1206 size = strlen(symname);
1207 if (size >= gfs2_max_stuffed_size(GFS2_I(dir)))
1208 return -ENAMETOOLONG;
1210 return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
1214 * gfs2_mkdir - Make a directory
1215 * @mnt_userns: User namespace of the mount the inode was found from
1216 * @dir: The parent directory of the new one
1217 * @dentry: The dentry of the new directory
1218 * @mode: The mode of the new directory
1223 static int gfs2_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
1224 struct dentry *dentry, umode_t mode)
1226 unsigned dsize = gfs2_max_stuffed_size(GFS2_I(dir));
1227 return gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0);
1231 * gfs2_mknod - Make a special file
1232 * @mnt_userns: User namespace of the mount the inode was found from
1233 * @dir: The directory in which the special file will reside
1234 * @dentry: The dentry of the special file
1235 * @mode: The mode of the special file
1236 * @dev: The device specification of the special file
1240 static int gfs2_mknod(struct user_namespace *mnt_userns, struct inode *dir,
1241 struct dentry *dentry, umode_t mode, dev_t dev)
1243 return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0);
1247 * gfs2_atomic_open - Atomically open a file
1248 * @dir: The directory
1249 * @dentry: The proposed new entry
1250 * @file: The proposed new struct file
1251 * @flags: open flags
1254 * Returns: error code or 0 for success
1257 static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry,
1258 struct file *file, unsigned flags,
1262 bool excl = !!(flags & O_EXCL);
1264 if (!d_in_lookup(dentry))
1267 d = __gfs2_lookup(dir, dentry, file);
1272 if (d_really_is_positive(dentry)) {
1273 if (!(file->f_mode & FMODE_OPENED))
1274 return finish_no_open(file, d);
1276 return excl && (flags & O_CREAT) ? -EEXIST : 0;
1282 if (!(flags & O_CREAT))
1285 return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl);
1289 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1293 * Follow @to back to the root and make sure we don't encounter @this
1294 * Assumes we already hold the rename lock.
1299 static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1301 struct inode *dir = &to->i_inode;
1302 struct super_block *sb = dir->i_sb;
1309 if (dir == &this->i_inode) {
1313 if (dir == d_inode(sb->s_root)) {
1318 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
1324 error = PTR_ERR(tmp);
1338 * update_moved_ino - Update an inode that's being moved
1339 * @ip: The inode being moved
1340 * @ndip: The parent directory of the new filename
1341 * @dir_rename: True of ip is a directory
1346 static int update_moved_ino(struct gfs2_inode *ip, struct gfs2_inode *ndip,
1350 return gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
1352 ip->i_inode.i_ctime = current_time(&ip->i_inode);
1353 mark_inode_dirty_sync(&ip->i_inode);
1359 * gfs2_rename - Rename a file
1360 * @odir: Parent directory of old file name
1361 * @odentry: The old dentry of the file
1362 * @ndir: Parent directory of new file name
1363 * @ndentry: The new dentry of the file
1368 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
1369 struct inode *ndir, struct dentry *ndentry)
1371 struct gfs2_inode *odip = GFS2_I(odir);
1372 struct gfs2_inode *ndip = GFS2_I(ndir);
1373 struct gfs2_inode *ip = GFS2_I(d_inode(odentry));
1374 struct gfs2_inode *nip = NULL;
1375 struct gfs2_sbd *sdp = GFS2_SB(odir);
1376 struct gfs2_holder ghs[4], r_gh, rd_gh;
1377 struct gfs2_rgrpd *nrgd;
1378 unsigned int num_gh;
1380 struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, };
1384 gfs2_holder_mark_uninitialized(&r_gh);
1385 gfs2_holder_mark_uninitialized(&rd_gh);
1386 if (d_really_is_positive(ndentry)) {
1387 nip = GFS2_I(d_inode(ndentry));
1392 error = gfs2_rindex_update(sdp);
1396 error = gfs2_qa_get(ndip);
1401 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1406 if (S_ISDIR(ip->i_inode.i_mode)) {
1408 /* don't move a directory into its subdir */
1409 error = gfs2_ok_to_move(ip, ndip);
1416 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
1418 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE,GL_ASYNC,
1422 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
1426 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1431 for (x = 0; x < num_gh; x++) {
1432 error = gfs2_glock_nq(ghs + x);
1436 error = gfs2_glock_async_wait(num_gh, ghs);
1441 /* Grab the resource group glock for unlink flag twiddling.
1442 * This is the case where the target dinode already exists
1443 * so we unlink before doing the rename.
1445 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
1450 error = gfs2_glock_nq_init(nrgd->rd_gl, LM_ST_EXCLUSIVE,
1451 LM_FLAG_NODE_SCOPE, &rd_gh);
1457 if (ip->i_inode.i_nlink == 0)
1460 /* Check out the old directory */
1462 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
1466 /* Check out the new directory */
1469 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1473 if (nip->i_inode.i_nlink == 0) {
1478 if (S_ISDIR(nip->i_inode.i_mode)) {
1479 if (nip->i_entries < 2) {
1480 gfs2_consist_inode(nip);
1484 if (nip->i_entries > 2) {
1490 error = gfs2_permission(&init_user_ns, ndir,
1491 MAY_WRITE | MAY_EXEC);
1495 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
1508 if (!ndip->i_inode.i_nlink) {
1512 if (ndip->i_entries == (u32)-1) {
1516 if (S_ISDIR(ip->i_inode.i_mode) &&
1517 ndip->i_inode.i_nlink == (u32)-1) {
1524 /* Check out the dir to be renamed */
1527 error = gfs2_permission(&init_user_ns, d_inode(odentry),
1534 error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
1540 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
1541 error = gfs2_quota_lock_check(ndip, &ap);
1545 error = gfs2_inplace_reserve(ndip, &ap);
1549 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
1550 4 * RES_LEAF + 4, 0);
1554 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
1555 5 * RES_LEAF + 4, 0);
1560 /* Remove the target file, if it exists */
1563 error = gfs2_unlink_inode(ndip, ndentry);
1565 error = update_moved_ino(ip, ndip, dir_rename);
1569 error = gfs2_dir_del(odip, odentry);
1573 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
1578 gfs2_trans_end(sdp);
1581 gfs2_inplace_release(ndip);
1584 gfs2_quota_unlock(ndip);
1586 gfs2_dir_no_add(&da);
1587 if (gfs2_holder_initialized(&rd_gh))
1588 gfs2_glock_dq_uninit(&rd_gh);
1591 if (gfs2_holder_queued(ghs + x))
1592 gfs2_glock_dq(ghs + x);
1593 gfs2_holder_uninit(ghs + x);
1596 if (gfs2_holder_initialized(&r_gh))
1597 gfs2_glock_dq_uninit(&r_gh);
1604 * gfs2_exchange - exchange two files
1605 * @odir: Parent directory of old file name
1606 * @odentry: The old dentry of the file
1607 * @ndir: Parent directory of new file name
1608 * @ndentry: The new dentry of the file
1609 * @flags: The rename flags
1614 static int gfs2_exchange(struct inode *odir, struct dentry *odentry,
1615 struct inode *ndir, struct dentry *ndentry,
1618 struct gfs2_inode *odip = GFS2_I(odir);
1619 struct gfs2_inode *ndip = GFS2_I(ndir);
1620 struct gfs2_inode *oip = GFS2_I(odentry->d_inode);
1621 struct gfs2_inode *nip = GFS2_I(ndentry->d_inode);
1622 struct gfs2_sbd *sdp = GFS2_SB(odir);
1623 struct gfs2_holder ghs[4], r_gh;
1624 unsigned int num_gh;
1626 umode_t old_mode = oip->i_inode.i_mode;
1627 umode_t new_mode = nip->i_inode.i_mode;
1630 gfs2_holder_mark_uninitialized(&r_gh);
1631 error = gfs2_rindex_update(sdp);
1636 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1641 if (S_ISDIR(old_mode)) {
1642 /* don't move a directory into its subdir */
1643 error = gfs2_ok_to_move(oip, ndip);
1648 if (S_ISDIR(new_mode)) {
1649 /* don't move a directory into its subdir */
1650 error = gfs2_ok_to_move(nip, odip);
1657 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
1659 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1663 gfs2_holder_init(oip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
1666 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
1669 for (x = 0; x < num_gh; x++) {
1670 error = gfs2_glock_nq(ghs + x);
1675 error = gfs2_glock_async_wait(num_gh, ghs);
1680 if (oip->i_inode.i_nlink == 0 || nip->i_inode.i_nlink == 0)
1683 error = gfs2_unlink_ok(odip, &odentry->d_name, oip);
1686 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1690 if (S_ISDIR(old_mode)) {
1691 error = gfs2_permission(&init_user_ns, odentry->d_inode,
1696 if (S_ISDIR(new_mode)) {
1697 error = gfs2_permission(&init_user_ns, ndentry->d_inode,
1702 error = gfs2_trans_begin(sdp, 4 * RES_DINODE + 4 * RES_LEAF, 0);
1706 error = update_moved_ino(oip, ndip, S_ISDIR(old_mode));
1710 error = update_moved_ino(nip, odip, S_ISDIR(new_mode));
1714 error = gfs2_dir_mvino(ndip, &ndentry->d_name, oip,
1719 error = gfs2_dir_mvino(odip, &odentry->d_name, nip,
1725 if (S_ISDIR(new_mode) && !S_ISDIR(old_mode)) {
1726 inc_nlink(&odip->i_inode);
1727 drop_nlink(&ndip->i_inode);
1728 } else if (S_ISDIR(old_mode) && !S_ISDIR(new_mode)) {
1729 inc_nlink(&ndip->i_inode);
1730 drop_nlink(&odip->i_inode);
1733 mark_inode_dirty(&ndip->i_inode);
1735 mark_inode_dirty(&odip->i_inode);
1738 gfs2_trans_end(sdp);
1741 if (gfs2_holder_queued(ghs + x))
1742 gfs2_glock_dq(ghs + x);
1743 gfs2_holder_uninit(ghs + x);
1746 if (gfs2_holder_initialized(&r_gh))
1747 gfs2_glock_dq_uninit(&r_gh);
1752 static int gfs2_rename2(struct user_namespace *mnt_userns, struct inode *odir,
1753 struct dentry *odentry, struct inode *ndir,
1754 struct dentry *ndentry, unsigned int flags)
1756 flags &= ~RENAME_NOREPLACE;
1758 if (flags & ~RENAME_EXCHANGE)
1761 if (flags & RENAME_EXCHANGE)
1762 return gfs2_exchange(odir, odentry, ndir, ndentry, flags);
1764 return gfs2_rename(odir, odentry, ndir, ndentry);
1768 * gfs2_get_link - Follow a symbolic link
1769 * @dentry: The dentry of the link
1770 * @inode: The inode of the link
1771 * @done: destructor for return value
1773 * This can handle symlinks of any size.
1775 * Returns: 0 on success or error code
1778 static const char *gfs2_get_link(struct dentry *dentry,
1779 struct inode *inode,
1780 struct delayed_call *done)
1782 struct gfs2_inode *ip = GFS2_I(inode);
1783 struct gfs2_holder i_gh;
1784 struct buffer_head *dibh;
1790 return ERR_PTR(-ECHILD);
1792 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1793 error = gfs2_glock_nq(&i_gh);
1795 gfs2_holder_uninit(&i_gh);
1796 return ERR_PTR(error);
1799 size = (unsigned int)i_size_read(&ip->i_inode);
1801 gfs2_consist_inode(ip);
1802 buf = ERR_PTR(-EIO);
1806 error = gfs2_meta_inode_buffer(ip, &dibh);
1808 buf = ERR_PTR(error);
1812 buf = kzalloc(size + 1, GFP_NOFS);
1814 buf = ERR_PTR(-ENOMEM);
1816 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
1819 gfs2_glock_dq_uninit(&i_gh);
1821 set_delayed_call(done, kfree_link, buf);
1827 * @mnt_userns: User namespace of the mount the inode was found from
1829 * @mask: The mask to be tested
1831 * This may be called from the VFS directly, or from within GFS2 with the
1832 * inode locked, so we look to see if the glock is already locked and only
1833 * lock the glock if its not already been done.
1838 int gfs2_permission(struct user_namespace *mnt_userns, struct inode *inode,
1841 struct gfs2_inode *ip;
1842 struct gfs2_holder i_gh;
1845 gfs2_holder_mark_uninitialized(&i_gh);
1847 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1848 if (mask & MAY_NOT_BLOCK)
1850 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1855 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1858 error = generic_permission(&init_user_ns, inode, mask);
1859 if (gfs2_holder_initialized(&i_gh))
1860 gfs2_glock_dq_uninit(&i_gh);
1865 static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
1867 setattr_copy(&init_user_ns, inode, attr);
1868 mark_inode_dirty(inode);
1872 static int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
1876 if (current->journal_info)
1877 return __gfs2_setattr_simple(inode, attr);
1879 error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
1883 error = __gfs2_setattr_simple(inode, attr);
1884 gfs2_trans_end(GFS2_SB(inode));
1888 static int setattr_chown(struct inode *inode, struct iattr *attr)
1890 struct gfs2_inode *ip = GFS2_I(inode);
1891 struct gfs2_sbd *sdp = GFS2_SB(inode);
1895 struct gfs2_alloc_parms ap;
1897 ouid = inode->i_uid;
1898 ogid = inode->i_gid;
1899 nuid = attr->ia_uid;
1900 ngid = attr->ia_gid;
1902 if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
1903 ouid = nuid = NO_UID_QUOTA_CHANGE;
1904 if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
1905 ogid = ngid = NO_GID_QUOTA_CHANGE;
1906 error = gfs2_qa_get(ip);
1910 error = gfs2_rindex_update(sdp);
1914 error = gfs2_quota_lock(ip, nuid, ngid);
1918 ap.target = gfs2_get_inode_blocks(&ip->i_inode);
1920 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1921 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
1922 error = gfs2_quota_check(ip, nuid, ngid, &ap);
1927 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1931 error = gfs2_setattr_simple(inode, attr);
1935 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1936 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
1937 gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid);
1938 gfs2_quota_change(ip, ap.target, nuid, ngid);
1942 gfs2_trans_end(sdp);
1944 gfs2_quota_unlock(ip);
1951 * gfs2_setattr - Change attributes on an inode
1952 * @mnt_userns: User namespace of the mount the inode was found from
1953 * @dentry: The dentry which is changing
1954 * @attr: The structure describing the change
1956 * The VFS layer wants to change one or more of an inodes attributes. Write
1957 * that change out to disk.
1962 static int gfs2_setattr(struct user_namespace *mnt_userns,
1963 struct dentry *dentry, struct iattr *attr)
1965 struct inode *inode = d_inode(dentry);
1966 struct gfs2_inode *ip = GFS2_I(inode);
1967 struct gfs2_holder i_gh;
1970 error = gfs2_qa_get(ip);
1974 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1978 error = may_setattr(&init_user_ns, inode, attr->ia_valid);
1982 error = setattr_prepare(&init_user_ns, dentry, attr);
1986 if (attr->ia_valid & ATTR_SIZE)
1987 error = gfs2_setattr_size(inode, attr->ia_size);
1988 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1989 error = setattr_chown(inode, attr);
1991 error = gfs2_setattr_simple(inode, attr);
1992 if (!error && attr->ia_valid & ATTR_MODE)
1993 error = posix_acl_chmod(&init_user_ns, inode,
1999 mark_inode_dirty(inode);
2000 gfs2_glock_dq_uninit(&i_gh);
2007 * gfs2_getattr - Read out an inode's attributes
2008 * @mnt_userns: user namespace of the mount the inode was found from
2009 * @path: Object to query
2010 * @stat: The inode's stats
2011 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
2012 * @flags: AT_STATX_xxx setting
2014 * This may be called from the VFS directly, or from within GFS2 with the
2015 * inode locked, so we look to see if the glock is already locked and only
2016 * lock the glock if its not already been done. Note that its the NFS
2017 * readdirplus operation which causes this to be called (from filldir)
2018 * with the glock already held.
2023 static int gfs2_getattr(struct user_namespace *mnt_userns,
2024 const struct path *path, struct kstat *stat,
2025 u32 request_mask, unsigned int flags)
2027 struct inode *inode = d_inode(path->dentry);
2028 struct gfs2_inode *ip = GFS2_I(inode);
2029 struct gfs2_holder gh;
2033 gfs2_holder_mark_uninitialized(&gh);
2034 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
2035 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
2040 gfsflags = ip->i_diskflags;
2041 if (gfsflags & GFS2_DIF_APPENDONLY)
2042 stat->attributes |= STATX_ATTR_APPEND;
2043 if (gfsflags & GFS2_DIF_IMMUTABLE)
2044 stat->attributes |= STATX_ATTR_IMMUTABLE;
2046 stat->attributes_mask |= (STATX_ATTR_APPEND |
2047 STATX_ATTR_COMPRESSED |
2048 STATX_ATTR_ENCRYPTED |
2049 STATX_ATTR_IMMUTABLE |
2052 generic_fillattr(&init_user_ns, inode, stat);
2054 if (gfs2_holder_initialized(&gh))
2055 gfs2_glock_dq_uninit(&gh);
2060 static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2063 struct gfs2_inode *ip = GFS2_I(inode);
2064 struct gfs2_holder gh;
2067 inode_lock_shared(inode);
2069 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2073 ret = iomap_fiemap(inode, fieinfo, start, len, &gfs2_iomap_ops);
2075 gfs2_glock_dq_uninit(&gh);
2078 inode_unlock_shared(inode);
2082 loff_t gfs2_seek_data(struct file *file, loff_t offset)
2084 struct inode *inode = file->f_mapping->host;
2085 struct gfs2_inode *ip = GFS2_I(inode);
2086 struct gfs2_holder gh;
2089 inode_lock_shared(inode);
2090 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2092 ret = iomap_seek_data(inode, offset, &gfs2_iomap_ops);
2093 gfs2_glock_dq_uninit(&gh);
2094 inode_unlock_shared(inode);
2098 return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
2101 loff_t gfs2_seek_hole(struct file *file, loff_t offset)
2103 struct inode *inode = file->f_mapping->host;
2104 struct gfs2_inode *ip = GFS2_I(inode);
2105 struct gfs2_holder gh;
2108 inode_lock_shared(inode);
2109 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2111 ret = iomap_seek_hole(inode, offset, &gfs2_iomap_ops);
2112 gfs2_glock_dq_uninit(&gh);
2113 inode_unlock_shared(inode);
2117 return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
2120 static int gfs2_update_time(struct inode *inode, struct timespec64 *time,
2123 struct gfs2_inode *ip = GFS2_I(inode);
2124 struct gfs2_glock *gl = ip->i_gl;
2125 struct gfs2_holder *gh;
2128 gh = gfs2_glock_is_locked_by_me(gl);
2129 if (gh && !gfs2_glock_is_held_excl(gl)) {
2131 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, gh);
2132 error = gfs2_glock_nq(gh);
2136 return generic_update_time(inode, time, flags);
2139 static const struct inode_operations gfs2_file_iops = {
2140 .permission = gfs2_permission,
2141 .setattr = gfs2_setattr,
2142 .getattr = gfs2_getattr,
2143 .listxattr = gfs2_listxattr,
2144 .fiemap = gfs2_fiemap,
2145 .get_acl = gfs2_get_acl,
2146 .set_acl = gfs2_set_acl,
2147 .update_time = gfs2_update_time,
2148 .fileattr_get = gfs2_fileattr_get,
2149 .fileattr_set = gfs2_fileattr_set,
2152 static const struct inode_operations gfs2_dir_iops = {
2153 .create = gfs2_create,
2154 .lookup = gfs2_lookup,
2156 .unlink = gfs2_unlink,
2157 .symlink = gfs2_symlink,
2158 .mkdir = gfs2_mkdir,
2159 .rmdir = gfs2_unlink,
2160 .mknod = gfs2_mknod,
2161 .rename = gfs2_rename2,
2162 .permission = gfs2_permission,
2163 .setattr = gfs2_setattr,
2164 .getattr = gfs2_getattr,
2165 .listxattr = gfs2_listxattr,
2166 .fiemap = gfs2_fiemap,
2167 .get_acl = gfs2_get_acl,
2168 .set_acl = gfs2_set_acl,
2169 .update_time = gfs2_update_time,
2170 .atomic_open = gfs2_atomic_open,
2171 .fileattr_get = gfs2_fileattr_get,
2172 .fileattr_set = gfs2_fileattr_set,
2175 static const struct inode_operations gfs2_symlink_iops = {
2176 .get_link = gfs2_get_link,
2177 .permission = gfs2_permission,
2178 .setattr = gfs2_setattr,
2179 .getattr = gfs2_getattr,
2180 .listxattr = gfs2_listxattr,
2181 .fiemap = gfs2_fiemap,