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;
135 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE,
140 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE,
146 * The only caller that sets @blktype to GFS2_BLKST_UNLINKED is
147 * delete_work_func(). Make sure not to cancel the delete work
148 * from within itself here.
150 if (blktype == GFS2_BLKST_UNLINKED)
151 extra_flags |= LM_FLAG_TRY;
153 gfs2_cancel_delete_work(io_gl);
154 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED,
155 GL_EXACT | GL_NOPID | extra_flags,
157 gfs2_glock_put(io_gl);
161 if (type == DT_UNKNOWN || blktype != GFS2_BLKST_FREE) {
163 * The GL_SKIP flag indicates to skip reading the inode
164 * block. We read the inode when instantiating it
165 * after possibly checking the block type.
167 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE,
174 gfs2_inode_already_deleted(ip->i_gl, no_formal_ino))
177 if (blktype != GFS2_BLKST_FREE) {
178 error = gfs2_check_blk_type(sdp, no_addr,
185 set_bit(GLF_INSTANTIATE_NEEDED, &ip->i_gl->gl_flags);
187 /* Lowest possible timestamp; will be overwritten in gfs2_dinode_in. */
188 inode->i_atime.tv_sec = 1LL << (8 * sizeof(inode->i_atime.tv_sec) - 1);
189 inode->i_atime.tv_nsec = 0;
191 glock_set_object(ip->i_gl, ip);
193 if (type == DT_UNKNOWN) {
194 /* Inode glock must be locked already */
195 error = gfs2_instantiate(&i_gh);
197 glock_clear_object(ip->i_gl, ip);
201 ip->i_no_formal_ino = no_formal_ino;
202 inode->i_mode = DT2IF(type);
205 if (gfs2_holder_initialized(&i_gh))
206 gfs2_glock_dq_uninit(&i_gh);
207 glock_set_object(ip->i_iopen_gh.gh_gl, ip);
210 unlock_new_inode(inode);
213 if (no_formal_ino && ip->i_no_formal_ino &&
214 no_formal_ino != ip->i_no_formal_ino) {
216 return ERR_PTR(-ESTALE);
222 if (error == GLR_TRYFAILED)
224 if (gfs2_holder_initialized(&ip->i_iopen_gh))
225 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
226 if (gfs2_holder_initialized(&i_gh))
227 gfs2_glock_dq_uninit(&i_gh);
229 gfs2_glock_put(ip->i_gl);
233 return ERR_PTR(error);
237 * gfs2_lookup_by_inum - look up an inode by inode number
238 * @sdp: The super block
239 * @no_addr: The inode number
240 * @no_formal_ino: The inode generation number (0 for any)
241 * @blktype: Requested block type (see gfs2_inode_lookup)
243 struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
244 u64 no_formal_ino, unsigned int blktype)
246 struct super_block *sb = sdp->sd_vfs;
250 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, no_formal_ino,
257 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
264 return ERR_PTR(error);
268 struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
272 gfs2_str2qstr(&qstr, name);
273 inode = gfs2_lookupi(dip, &qstr, 1);
274 /* gfs2_lookupi has inconsistent callers: vfs
275 * related routines expect NULL for no entry found,
276 * gfs2_lookup_simple callers expect ENOENT
277 * and do not check for NULL.
280 return ERR_PTR(-ENOENT);
287 * gfs2_lookupi - Look up a filename in a directory and return its inode
288 * @dir: The inode of the directory containing the inode to look-up
289 * @name: The name of the inode to look for
290 * @is_root: If 1, ignore the caller's permissions
292 * This can be called via the VFS filldir function when NFS is doing
293 * a readdirplus and the inode which its intending to stat isn't
294 * already in cache. In this case we must not take the directory glock
295 * again, since the readdir call will have already taken that lock.
300 struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
303 struct super_block *sb = dir->i_sb;
304 struct gfs2_inode *dip = GFS2_I(dir);
305 struct gfs2_holder d_gh;
307 struct inode *inode = NULL;
309 gfs2_holder_mark_uninitialized(&d_gh);
310 if (!name->len || name->len > GFS2_FNAMESIZE)
311 return ERR_PTR(-ENAMETOOLONG);
313 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
314 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
315 dir == d_inode(sb->s_root))) {
320 if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
321 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
323 return ERR_PTR(error);
327 error = gfs2_permission(&nop_mnt_idmap, dir, MAY_EXEC);
332 inode = gfs2_dir_search(dir, name, false);
334 error = PTR_ERR(inode);
336 if (gfs2_holder_initialized(&d_gh))
337 gfs2_glock_dq_uninit(&d_gh);
338 if (error == -ENOENT)
340 return inode ? inode : ERR_PTR(error);
344 * create_ok - OK to create a new on-disk inode here?
345 * @dip: Directory in which dinode is to be created
346 * @name: Name of new dinode
352 static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
357 error = gfs2_permission(&nop_mnt_idmap, &dip->i_inode,
358 MAY_WRITE | MAY_EXEC);
362 /* Don't create entries in an unlinked directory */
363 if (!dip->i_inode.i_nlink)
366 if (dip->i_entries == (u32)-1)
368 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
374 static void munge_mode_uid_gid(const struct gfs2_inode *dip,
377 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
378 (dip->i_inode.i_mode & S_ISUID) &&
379 !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
380 if (S_ISDIR(inode->i_mode))
381 inode->i_mode |= S_ISUID;
382 else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
383 inode->i_mode &= ~07111;
384 inode->i_uid = dip->i_inode.i_uid;
386 inode->i_uid = current_fsuid();
388 if (dip->i_inode.i_mode & S_ISGID) {
389 if (S_ISDIR(inode->i_mode))
390 inode->i_mode |= S_ISGID;
391 inode->i_gid = dip->i_inode.i_gid;
393 inode->i_gid = current_fsgid();
396 static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks)
398 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
399 struct gfs2_alloc_parms ap = { .target = *dblocks, .aflags = flags, };
402 error = gfs2_quota_lock_check(ip, &ap);
406 error = gfs2_inplace_reserve(ip, &ap);
410 error = gfs2_trans_begin(sdp, (*dblocks * RES_RG_BIT) + RES_STATFS + RES_QUOTA, 0);
414 error = gfs2_alloc_blocks(ip, &ip->i_no_addr, dblocks, 1, &ip->i_generation);
418 ip->i_no_formal_ino = ip->i_generation;
419 ip->i_inode.i_ino = ip->i_no_addr;
420 ip->i_goal = ip->i_no_addr;
422 ip->i_eattr = ip->i_no_addr + 1;
427 gfs2_inplace_release(ip);
429 gfs2_quota_unlock(ip);
434 static void gfs2_init_dir(struct buffer_head *dibh,
435 const struct gfs2_inode *parent)
437 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
438 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
440 gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
441 dent->de_inum = di->di_num; /* already GFS2 endian */
442 dent->de_type = cpu_to_be16(DT_DIR);
444 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
445 gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
446 gfs2_inum_out(parent, dent);
447 dent->de_type = cpu_to_be16(DT_DIR);
452 * gfs2_init_xattr - Initialise an xattr block for a new inode
453 * @ip: The inode in question
455 * This sets up an empty xattr block for a new inode, ready to
456 * take any ACLs, LSM xattrs, etc.
459 static void gfs2_init_xattr(struct gfs2_inode *ip)
461 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
462 struct buffer_head *bh;
463 struct gfs2_ea_header *ea;
465 bh = gfs2_meta_new(ip->i_gl, ip->i_eattr);
466 gfs2_trans_add_meta(ip->i_gl, bh);
467 gfs2_metatype_set(bh, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
468 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
470 ea = GFS2_EA_BH2FIRST(bh);
471 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
472 ea->ea_type = GFS2_EATYPE_UNUSED;
473 ea->ea_flags = GFS2_EAFLAG_LAST;
479 * init_dinode - Fill in a new dinode structure
480 * @dip: The directory this inode is being created in
482 * @symname: The symlink destination (if a symlink)
486 static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
489 struct gfs2_dinode *di;
490 struct buffer_head *dibh;
492 dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
493 gfs2_trans_add_meta(ip->i_gl, dibh);
494 di = (struct gfs2_dinode *)dibh->b_data;
495 gfs2_dinode_out(ip, di);
497 di->di_major = cpu_to_be32(imajor(&ip->i_inode));
498 di->di_minor = cpu_to_be32(iminor(&ip->i_inode));
502 memset(&di->__pad4, 0, sizeof(di->__pad4));
503 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
504 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
506 switch(ip->i_inode.i_mode & S_IFMT) {
508 gfs2_init_dir(dibh, dip);
511 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
515 set_buffer_uptodate(dibh);
520 * gfs2_trans_da_blks - Calculate number of blocks to link inode
521 * @dip: The directory we are linking into
522 * @da: The dir add information
523 * @nr_inodes: The number of inodes involved
525 * This calculate the number of blocks we need to reserve in a
526 * transaction to link @nr_inodes into a directory. In most cases
527 * @nr_inodes will be 2 (the directory plus the inode being linked in)
528 * but in case of rename, 4 may be required.
530 * Returns: Number of blocks
533 static unsigned gfs2_trans_da_blks(const struct gfs2_inode *dip,
534 const struct gfs2_diradd *da,
537 return da->nr_blocks + gfs2_rg_blocks(dip, da->nr_blocks) +
538 (nr_inodes * RES_DINODE) + RES_QUOTA + RES_STATFS;
541 static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
542 struct gfs2_inode *ip, struct gfs2_diradd *da)
544 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
545 struct gfs2_alloc_parms ap = { .target = da->nr_blocks, };
549 error = gfs2_quota_lock_check(dip, &ap);
551 goto fail_quota_locks;
553 error = gfs2_inplace_reserve(dip, &ap);
555 goto fail_quota_locks;
557 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0);
561 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
563 goto fail_quota_locks;
566 error = gfs2_dir_add(&dip->i_inode, name, ip, da);
570 gfs2_inplace_release(dip);
572 gfs2_quota_unlock(dip);
576 static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
579 const struct xattr *xattr;
582 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
583 err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
585 GFS2_EATYPE_SECURITY);
593 * gfs2_create_inode - Create a new inode
594 * @dir: The parent directory
595 * @dentry: The new dentry
596 * @file: If non-NULL, the file which is being opened
597 * @mode: The permissions on the new inode
598 * @dev: For device nodes, this is the device number
599 * @symname: For symlinks, this is the link destination
600 * @size: The initial size of the inode (ignored for directories)
601 * @excl: Force fail if inode exists
603 * FIXME: Change to allocate the disk blocks and write them out in the same
604 * transaction. That way, we can no longer end up in a situation in which an
605 * inode is allocated, the node crashes, and the block looks like a valid
606 * inode. (With atomic creates in place, we will also no longer need to zero
607 * the link count and dirty the inode here on failure.)
609 * Returns: 0 on success, or error code
612 static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
614 umode_t mode, dev_t dev, const char *symname,
615 unsigned int size, int excl)
617 const struct qstr *name = &dentry->d_name;
618 struct posix_acl *default_acl, *acl;
619 struct gfs2_holder d_gh, gh;
620 struct inode *inode = NULL;
621 struct gfs2_inode *dip = GFS2_I(dir), *ip;
622 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
623 struct gfs2_glock *io_gl;
627 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
629 if (!name->len || name->len > GFS2_FNAMESIZE)
630 return -ENAMETOOLONG;
632 error = gfs2_qa_get(dip);
636 error = gfs2_rindex_update(sdp);
640 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, &d_gh);
643 gfs2_holder_mark_uninitialized(&gh);
645 error = create_ok(dip, name, mode);
649 inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
650 error = PTR_ERR(inode);
651 if (!IS_ERR(inode)) {
652 if (S_ISDIR(inode->i_mode)) {
654 inode = ERR_PTR(-EISDIR);
657 d_instantiate(dentry, inode);
660 if (S_ISREG(inode->i_mode))
661 error = finish_open(file, dentry, gfs2_open_common);
663 error = finish_no_open(file, NULL);
665 gfs2_glock_dq_uninit(&d_gh);
667 } else if (error != -ENOENT) {
671 error = gfs2_diradd_alloc_required(dir, name, &da);
675 inode = new_inode(sdp->sd_vfs);
681 error = posix_acl_create(dir, &mode, &default_acl, &acl);
685 error = gfs2_qa_get(ip);
689 inode->i_mode = mode;
690 set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
692 inode->i_size = size;
693 inode->i_atime = inode->i_mtime = inode_set_ctime_current(inode);
694 munge_mode_uid_gid(dip, inode);
695 check_and_update_goal(dip);
696 ip->i_goal = dip->i_goal;
702 ip->i_no_addr = 0; /* Temporarily zero until real addr is assigned */
704 switch(mode & S_IFMT) {
706 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
707 gfs2_tune_get(sdp, gt_new_files_jdata))
708 ip->i_diskflags |= GFS2_DIF_JDATA;
709 gfs2_set_aops(inode);
712 ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA);
713 ip->i_diskflags |= GFS2_DIF_JDATA;
718 /* Force SYSTEM flag on all files and subdirs of a SYSTEM directory */
719 if (dip->i_diskflags & GFS2_DIF_SYSTEM)
720 ip->i_diskflags |= GFS2_DIF_SYSTEM;
722 gfs2_set_inode_flags(inode);
724 if ((GFS2_I(d_inode(sdp->sd_root_dir)) == dip) ||
725 (dip->i_diskflags & GFS2_DIF_TOPDIR))
726 aflags |= GFS2_AF_ORLOV;
728 if (default_acl || acl)
731 error = alloc_dinode(ip, aflags, &blocks);
733 goto fail_free_inode;
735 gfs2_set_inode_blocks(inode, blocks);
737 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
739 goto fail_free_inode;
741 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
743 goto fail_free_inode;
744 gfs2_cancel_delete_work(io_gl);
747 error = insert_inode_locked4(inode, ip->i_no_addr, iget_test, &ip->i_no_addr);
753 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT | GL_NOPID,
758 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, &gh);
762 error = gfs2_trans_begin(sdp, blocks, 0);
768 init_dinode(dip, ip, symname);
771 glock_set_object(ip->i_gl, ip);
772 glock_set_object(io_gl, ip);
776 error = __gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
779 posix_acl_release(default_acl);
783 error = __gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS);
786 posix_acl_release(acl);
790 error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name,
791 &gfs2_initxattrs, NULL);
795 error = link_dinode(dip, name, ip, &da);
799 mark_inode_dirty(inode);
800 d_instantiate(dentry, inode);
801 /* After instantiate, errors should result in evict which will destroy
802 * both inode and iopen glocks properly. */
804 file->f_mode |= FMODE_CREATED;
805 error = finish_open(file, dentry, gfs2_open_common);
807 gfs2_glock_dq_uninit(&d_gh);
809 gfs2_glock_dq_uninit(&gh);
810 gfs2_glock_put(io_gl);
812 unlock_new_inode(inode);
816 glock_clear_object(ip->i_gl, ip);
817 glock_clear_object(io_gl, ip);
819 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
821 gfs2_glock_put(io_gl);
824 gfs2_glock_put(ip->i_gl);
827 gfs2_rs_deltree(&ip->i_res);
830 posix_acl_release(default_acl);
831 posix_acl_release(acl);
833 gfs2_dir_no_add(&da);
834 gfs2_glock_dq_uninit(&d_gh);
835 if (!IS_ERR_OR_NULL(inode)) {
836 set_bit(GIF_ALLOC_FAILED, &ip->i_flags);
839 mark_inode_dirty(inode);
840 if (inode->i_state & I_NEW)
845 if (gfs2_holder_initialized(&gh))
846 gfs2_glock_dq_uninit(&gh);
853 * gfs2_create - Create a file
854 * @idmap: idmap of the mount the inode was found from
855 * @dir: The directory in which to create the file
856 * @dentry: The dentry of the new file
857 * @mode: The mode of the new file
858 * @excl: Force fail if inode exists
863 static int gfs2_create(struct mnt_idmap *idmap, struct inode *dir,
864 struct dentry *dentry, umode_t mode, bool excl)
866 return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl);
870 * __gfs2_lookup - Look up a filename in a directory and return its inode
871 * @dir: The directory inode
872 * @dentry: The dentry of the new inode
873 * @file: File to be opened
879 static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry,
884 struct gfs2_holder gh;
885 struct gfs2_glock *gl;
888 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
894 return ERR_CAST(inode);
896 gl = GFS2_I(inode)->i_gl;
897 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
900 return ERR_PTR(error);
903 d = d_splice_alias(inode, dentry);
905 gfs2_glock_dq_uninit(&gh);
908 if (file && S_ISREG(inode->i_mode))
909 error = finish_open(file, dentry, gfs2_open_common);
911 gfs2_glock_dq_uninit(&gh);
914 return ERR_PTR(error);
919 static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
922 return __gfs2_lookup(dir, dentry, NULL);
926 * gfs2_link - Link to a file
927 * @old_dentry: The inode to link
928 * @dir: Add link to this directory
929 * @dentry: The name of the link
931 * Link the inode in "old_dentry" into the directory "dir" with the
937 static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
938 struct dentry *dentry)
940 struct gfs2_inode *dip = GFS2_I(dir);
941 struct gfs2_sbd *sdp = GFS2_SB(dir);
942 struct inode *inode = d_inode(old_dentry);
943 struct gfs2_inode *ip = GFS2_I(inode);
944 struct gfs2_holder d_gh, gh;
945 struct buffer_head *dibh;
946 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
949 if (S_ISDIR(inode->i_mode))
952 error = gfs2_qa_get(dip);
956 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, &d_gh);
957 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
959 error = gfs2_glock_nq(&d_gh);
963 error = gfs2_glock_nq(&gh);
968 if (inode->i_nlink == 0)
971 error = gfs2_permission(&nop_mnt_idmap, dir, MAY_WRITE | MAY_EXEC);
975 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
987 if (!dip->i_inode.i_nlink)
990 if (dip->i_entries == (u32)-1)
993 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
996 if (ip->i_inode.i_nlink == (u32)-1)
999 error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da);
1004 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
1005 error = gfs2_quota_lock_check(dip, &ap);
1009 error = gfs2_inplace_reserve(dip, &ap);
1013 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0);
1017 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
1022 error = gfs2_meta_inode_buffer(ip, &dibh);
1026 error = gfs2_dir_add(dir, &dentry->d_name, ip, &da);
1030 gfs2_trans_add_meta(ip->i_gl, dibh);
1031 inc_nlink(&ip->i_inode);
1032 inode_set_ctime_current(&ip->i_inode);
1034 d_instantiate(dentry, inode);
1035 mark_inode_dirty(inode);
1040 gfs2_trans_end(sdp);
1043 gfs2_inplace_release(dip);
1046 gfs2_quota_unlock(dip);
1048 gfs2_dir_no_add(&da);
1051 gfs2_glock_dq(&d_gh);
1054 gfs2_holder_uninit(&d_gh);
1055 gfs2_holder_uninit(&gh);
1060 * gfs2_unlink_ok - check to see that a inode is still in a directory
1061 * @dip: the directory
1062 * @name: the name of the file
1065 * Assumes that the lock on (at least) @dip is held.
1067 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1070 static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
1071 const struct gfs2_inode *ip)
1075 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1078 if ((dip->i_inode.i_mode & S_ISVTX) &&
1079 !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
1080 !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
1083 if (IS_APPEND(&dip->i_inode))
1086 error = gfs2_permission(&nop_mnt_idmap, &dip->i_inode,
1087 MAY_WRITE | MAY_EXEC);
1091 return gfs2_dir_check(&dip->i_inode, name, ip);
1095 * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
1096 * @dip: The parent directory
1097 * @dentry: The dentry to unlink
1099 * Called with all the locks and in a transaction. This will only be
1100 * called for a directory after it has been checked to ensure it is empty.
1102 * Returns: 0 on success, or an error
1105 static int gfs2_unlink_inode(struct gfs2_inode *dip,
1106 const struct dentry *dentry)
1108 struct inode *inode = d_inode(dentry);
1109 struct gfs2_inode *ip = GFS2_I(inode);
1112 error = gfs2_dir_del(dip, dentry);
1117 inode_set_ctime_current(inode);
1118 if (S_ISDIR(inode->i_mode))
1122 mark_inode_dirty(inode);
1123 if (inode->i_nlink == 0)
1124 gfs2_unlink_di(inode);
1130 * gfs2_unlink - Unlink an inode (this does rmdir as well)
1131 * @dir: The inode of the directory containing the inode to unlink
1132 * @dentry: The file itself
1134 * This routine uses the type of the inode as a flag to figure out
1135 * whether this is an unlink or an rmdir.
1140 static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
1142 struct gfs2_inode *dip = GFS2_I(dir);
1143 struct gfs2_sbd *sdp = GFS2_SB(dir);
1144 struct inode *inode = d_inode(dentry);
1145 struct gfs2_inode *ip = GFS2_I(inode);
1146 struct gfs2_holder d_gh, r_gh, gh;
1147 struct gfs2_rgrpd *rgd;
1150 error = gfs2_rindex_update(sdp);
1156 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, &d_gh);
1157 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1159 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
1163 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, LM_FLAG_NODE_SCOPE, &r_gh);
1166 error = gfs2_glock_nq(&d_gh);
1170 error = gfs2_glock_nq(&gh);
1175 if (inode->i_nlink == 0)
1178 if (S_ISDIR(inode->i_mode)) {
1180 if (ip->i_entries > 2 || inode->i_nlink > 2)
1184 error = gfs2_glock_nq(&r_gh); /* rgrp */
1188 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
1192 error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
1196 error = gfs2_unlink_inode(dip, dentry);
1197 gfs2_trans_end(sdp);
1200 gfs2_glock_dq(&r_gh);
1204 gfs2_glock_dq(&d_gh);
1206 gfs2_holder_uninit(&r_gh);
1208 gfs2_holder_uninit(&gh);
1209 gfs2_holder_uninit(&d_gh);
1214 * gfs2_symlink - Create a symlink
1215 * @idmap: idmap of the mount the inode was found from
1216 * @dir: The directory to create the symlink in
1217 * @dentry: The dentry to put the symlink in
1218 * @symname: The thing which the link points to
1223 static int gfs2_symlink(struct mnt_idmap *idmap, struct inode *dir,
1224 struct dentry *dentry, const char *symname)
1228 size = strlen(symname);
1229 if (size >= gfs2_max_stuffed_size(GFS2_I(dir)))
1230 return -ENAMETOOLONG;
1232 return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
1236 * gfs2_mkdir - Make a directory
1237 * @idmap: idmap of the mount the inode was found from
1238 * @dir: The parent directory of the new one
1239 * @dentry: The dentry of the new directory
1240 * @mode: The mode of the new directory
1245 static int gfs2_mkdir(struct mnt_idmap *idmap, struct inode *dir,
1246 struct dentry *dentry, umode_t mode)
1248 unsigned dsize = gfs2_max_stuffed_size(GFS2_I(dir));
1249 return gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0);
1253 * gfs2_mknod - Make a special file
1254 * @idmap: idmap of the mount the inode was found from
1255 * @dir: The directory in which the special file will reside
1256 * @dentry: The dentry of the special file
1257 * @mode: The mode of the special file
1258 * @dev: The device specification of the special file
1262 static int gfs2_mknod(struct mnt_idmap *idmap, struct inode *dir,
1263 struct dentry *dentry, umode_t mode, dev_t dev)
1265 return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0);
1269 * gfs2_atomic_open - Atomically open a file
1270 * @dir: The directory
1271 * @dentry: The proposed new entry
1272 * @file: The proposed new struct file
1273 * @flags: open flags
1276 * Returns: error code or 0 for success
1279 static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry,
1280 struct file *file, unsigned flags,
1284 bool excl = !!(flags & O_EXCL);
1286 if (!d_in_lookup(dentry))
1289 d = __gfs2_lookup(dir, dentry, file);
1294 if (d_really_is_positive(dentry)) {
1295 if (!(file->f_mode & FMODE_OPENED))
1296 return finish_no_open(file, d);
1298 return excl && (flags & O_CREAT) ? -EEXIST : 0;
1304 if (!(flags & O_CREAT))
1307 return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl);
1311 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1315 * Follow @to back to the root and make sure we don't encounter @this
1316 * Assumes we already hold the rename lock.
1321 static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1323 struct inode *dir = &to->i_inode;
1324 struct super_block *sb = dir->i_sb;
1331 if (dir == &this->i_inode) {
1335 if (dir == d_inode(sb->s_root)) {
1340 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
1346 error = PTR_ERR(tmp);
1360 * update_moved_ino - Update an inode that's being moved
1361 * @ip: The inode being moved
1362 * @ndip: The parent directory of the new filename
1363 * @dir_rename: True of ip is a directory
1368 static int update_moved_ino(struct gfs2_inode *ip, struct gfs2_inode *ndip,
1372 return gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
1374 inode_set_ctime_current(&ip->i_inode);
1375 mark_inode_dirty_sync(&ip->i_inode);
1381 * gfs2_rename - Rename a file
1382 * @odir: Parent directory of old file name
1383 * @odentry: The old dentry of the file
1384 * @ndir: Parent directory of new file name
1385 * @ndentry: The new dentry of the file
1390 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
1391 struct inode *ndir, struct dentry *ndentry)
1393 struct gfs2_inode *odip = GFS2_I(odir);
1394 struct gfs2_inode *ndip = GFS2_I(ndir);
1395 struct gfs2_inode *ip = GFS2_I(d_inode(odentry));
1396 struct gfs2_inode *nip = NULL;
1397 struct gfs2_sbd *sdp = GFS2_SB(odir);
1398 struct gfs2_holder ghs[4], r_gh, rd_gh;
1399 struct gfs2_rgrpd *nrgd;
1400 unsigned int num_gh;
1402 struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, };
1406 gfs2_holder_mark_uninitialized(&r_gh);
1407 gfs2_holder_mark_uninitialized(&rd_gh);
1408 if (d_really_is_positive(ndentry)) {
1409 nip = GFS2_I(d_inode(ndentry));
1414 error = gfs2_rindex_update(sdp);
1418 error = gfs2_qa_get(ndip);
1423 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1428 if (S_ISDIR(ip->i_inode.i_mode)) {
1430 /* don't move a directory into its subdir */
1431 error = gfs2_ok_to_move(ip, ndip);
1438 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
1440 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE,GL_ASYNC,
1444 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
1448 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1453 for (x = 0; x < num_gh; x++) {
1454 error = gfs2_glock_nq(ghs + x);
1458 error = gfs2_glock_async_wait(num_gh, ghs);
1463 /* Grab the resource group glock for unlink flag twiddling.
1464 * This is the case where the target dinode already exists
1465 * so we unlink before doing the rename.
1467 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
1472 error = gfs2_glock_nq_init(nrgd->rd_gl, LM_ST_EXCLUSIVE,
1473 LM_FLAG_NODE_SCOPE, &rd_gh);
1479 if (ip->i_inode.i_nlink == 0)
1482 /* Check out the old directory */
1484 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
1488 /* Check out the new directory */
1491 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1495 if (nip->i_inode.i_nlink == 0) {
1500 if (S_ISDIR(nip->i_inode.i_mode)) {
1501 if (nip->i_entries < 2) {
1502 gfs2_consist_inode(nip);
1506 if (nip->i_entries > 2) {
1512 error = gfs2_permission(&nop_mnt_idmap, ndir,
1513 MAY_WRITE | MAY_EXEC);
1517 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
1530 if (!ndip->i_inode.i_nlink) {
1534 if (ndip->i_entries == (u32)-1) {
1538 if (S_ISDIR(ip->i_inode.i_mode) &&
1539 ndip->i_inode.i_nlink == (u32)-1) {
1546 /* Check out the dir to be renamed */
1549 error = gfs2_permission(&nop_mnt_idmap, d_inode(odentry),
1556 error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
1562 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
1563 error = gfs2_quota_lock_check(ndip, &ap);
1567 error = gfs2_inplace_reserve(ndip, &ap);
1571 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
1572 4 * RES_LEAF + 4, 0);
1576 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
1577 5 * RES_LEAF + 4, 0);
1582 /* Remove the target file, if it exists */
1585 error = gfs2_unlink_inode(ndip, ndentry);
1587 error = update_moved_ino(ip, ndip, dir_rename);
1591 error = gfs2_dir_del(odip, odentry);
1595 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
1600 gfs2_trans_end(sdp);
1603 gfs2_inplace_release(ndip);
1606 gfs2_quota_unlock(ndip);
1608 gfs2_dir_no_add(&da);
1609 if (gfs2_holder_initialized(&rd_gh))
1610 gfs2_glock_dq_uninit(&rd_gh);
1613 if (gfs2_holder_queued(ghs + x))
1614 gfs2_glock_dq(ghs + x);
1615 gfs2_holder_uninit(ghs + x);
1618 if (gfs2_holder_initialized(&r_gh))
1619 gfs2_glock_dq_uninit(&r_gh);
1626 * gfs2_exchange - exchange two files
1627 * @odir: Parent directory of old file name
1628 * @odentry: The old dentry of the file
1629 * @ndir: Parent directory of new file name
1630 * @ndentry: The new dentry of the file
1631 * @flags: The rename flags
1636 static int gfs2_exchange(struct inode *odir, struct dentry *odentry,
1637 struct inode *ndir, struct dentry *ndentry,
1640 struct gfs2_inode *odip = GFS2_I(odir);
1641 struct gfs2_inode *ndip = GFS2_I(ndir);
1642 struct gfs2_inode *oip = GFS2_I(odentry->d_inode);
1643 struct gfs2_inode *nip = GFS2_I(ndentry->d_inode);
1644 struct gfs2_sbd *sdp = GFS2_SB(odir);
1645 struct gfs2_holder ghs[4], r_gh;
1646 unsigned int num_gh;
1648 umode_t old_mode = oip->i_inode.i_mode;
1649 umode_t new_mode = nip->i_inode.i_mode;
1652 gfs2_holder_mark_uninitialized(&r_gh);
1653 error = gfs2_rindex_update(sdp);
1658 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1663 if (S_ISDIR(old_mode)) {
1664 /* don't move a directory into its subdir */
1665 error = gfs2_ok_to_move(oip, ndip);
1670 if (S_ISDIR(new_mode)) {
1671 /* don't move a directory into its subdir */
1672 error = gfs2_ok_to_move(nip, odip);
1679 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
1681 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1685 gfs2_holder_init(oip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
1688 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
1691 for (x = 0; x < num_gh; x++) {
1692 error = gfs2_glock_nq(ghs + x);
1697 error = gfs2_glock_async_wait(num_gh, ghs);
1702 if (oip->i_inode.i_nlink == 0 || nip->i_inode.i_nlink == 0)
1705 error = gfs2_unlink_ok(odip, &odentry->d_name, oip);
1708 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1712 if (S_ISDIR(old_mode)) {
1713 error = gfs2_permission(&nop_mnt_idmap, odentry->d_inode,
1718 if (S_ISDIR(new_mode)) {
1719 error = gfs2_permission(&nop_mnt_idmap, ndentry->d_inode,
1724 error = gfs2_trans_begin(sdp, 4 * RES_DINODE + 4 * RES_LEAF, 0);
1728 error = update_moved_ino(oip, ndip, S_ISDIR(old_mode));
1732 error = update_moved_ino(nip, odip, S_ISDIR(new_mode));
1736 error = gfs2_dir_mvino(ndip, &ndentry->d_name, oip,
1741 error = gfs2_dir_mvino(odip, &odentry->d_name, nip,
1747 if (S_ISDIR(new_mode) && !S_ISDIR(old_mode)) {
1748 inc_nlink(&odip->i_inode);
1749 drop_nlink(&ndip->i_inode);
1750 } else if (S_ISDIR(old_mode) && !S_ISDIR(new_mode)) {
1751 inc_nlink(&ndip->i_inode);
1752 drop_nlink(&odip->i_inode);
1755 mark_inode_dirty(&ndip->i_inode);
1757 mark_inode_dirty(&odip->i_inode);
1760 gfs2_trans_end(sdp);
1763 if (gfs2_holder_queued(ghs + x))
1764 gfs2_glock_dq(ghs + x);
1765 gfs2_holder_uninit(ghs + x);
1768 if (gfs2_holder_initialized(&r_gh))
1769 gfs2_glock_dq_uninit(&r_gh);
1774 static int gfs2_rename2(struct mnt_idmap *idmap, struct inode *odir,
1775 struct dentry *odentry, struct inode *ndir,
1776 struct dentry *ndentry, unsigned int flags)
1778 flags &= ~RENAME_NOREPLACE;
1780 if (flags & ~RENAME_EXCHANGE)
1783 if (flags & RENAME_EXCHANGE)
1784 return gfs2_exchange(odir, odentry, ndir, ndentry, flags);
1786 return gfs2_rename(odir, odentry, ndir, ndentry);
1790 * gfs2_get_link - Follow a symbolic link
1791 * @dentry: The dentry of the link
1792 * @inode: The inode of the link
1793 * @done: destructor for return value
1795 * This can handle symlinks of any size.
1797 * Returns: 0 on success or error code
1800 static const char *gfs2_get_link(struct dentry *dentry,
1801 struct inode *inode,
1802 struct delayed_call *done)
1804 struct gfs2_inode *ip = GFS2_I(inode);
1805 struct gfs2_holder i_gh;
1806 struct buffer_head *dibh;
1812 return ERR_PTR(-ECHILD);
1814 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1815 error = gfs2_glock_nq(&i_gh);
1817 gfs2_holder_uninit(&i_gh);
1818 return ERR_PTR(error);
1821 size = (unsigned int)i_size_read(&ip->i_inode);
1823 gfs2_consist_inode(ip);
1824 buf = ERR_PTR(-EIO);
1828 error = gfs2_meta_inode_buffer(ip, &dibh);
1830 buf = ERR_PTR(error);
1834 buf = kzalloc(size + 1, GFP_NOFS);
1836 buf = ERR_PTR(-ENOMEM);
1838 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
1841 gfs2_glock_dq_uninit(&i_gh);
1843 set_delayed_call(done, kfree_link, buf);
1849 * @idmap: idmap of the mount the inode was found from
1851 * @mask: The mask to be tested
1853 * This may be called from the VFS directly, or from within GFS2 with the
1854 * inode locked, so we look to see if the glock is already locked and only
1855 * lock the glock if its not already been done.
1860 int gfs2_permission(struct mnt_idmap *idmap, struct inode *inode,
1863 struct gfs2_inode *ip;
1864 struct gfs2_holder i_gh;
1867 gfs2_holder_mark_uninitialized(&i_gh);
1869 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1870 if (mask & MAY_NOT_BLOCK)
1872 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1877 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1880 error = generic_permission(&nop_mnt_idmap, inode, mask);
1881 if (gfs2_holder_initialized(&i_gh))
1882 gfs2_glock_dq_uninit(&i_gh);
1887 static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
1889 setattr_copy(&nop_mnt_idmap, inode, attr);
1890 mark_inode_dirty(inode);
1894 static int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
1898 if (current->journal_info)
1899 return __gfs2_setattr_simple(inode, attr);
1901 error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
1905 error = __gfs2_setattr_simple(inode, attr);
1906 gfs2_trans_end(GFS2_SB(inode));
1910 static int setattr_chown(struct inode *inode, struct iattr *attr)
1912 struct gfs2_inode *ip = GFS2_I(inode);
1913 struct gfs2_sbd *sdp = GFS2_SB(inode);
1917 struct gfs2_alloc_parms ap;
1919 ouid = inode->i_uid;
1920 ogid = inode->i_gid;
1921 nuid = attr->ia_uid;
1922 ngid = attr->ia_gid;
1924 if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
1925 ouid = nuid = NO_UID_QUOTA_CHANGE;
1926 if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
1927 ogid = ngid = NO_GID_QUOTA_CHANGE;
1928 error = gfs2_qa_get(ip);
1932 error = gfs2_rindex_update(sdp);
1936 error = gfs2_quota_lock(ip, nuid, ngid);
1940 ap.target = gfs2_get_inode_blocks(&ip->i_inode);
1942 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1943 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
1944 error = gfs2_quota_check(ip, nuid, ngid, &ap);
1949 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1953 error = gfs2_setattr_simple(inode, attr);
1957 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1958 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
1959 gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid);
1960 gfs2_quota_change(ip, ap.target, nuid, ngid);
1964 gfs2_trans_end(sdp);
1966 gfs2_quota_unlock(ip);
1973 * gfs2_setattr - Change attributes on an inode
1974 * @idmap: idmap of the mount the inode was found from
1975 * @dentry: The dentry which is changing
1976 * @attr: The structure describing the change
1978 * The VFS layer wants to change one or more of an inodes attributes. Write
1979 * that change out to disk.
1984 static int gfs2_setattr(struct mnt_idmap *idmap,
1985 struct dentry *dentry, struct iattr *attr)
1987 struct inode *inode = d_inode(dentry);
1988 struct gfs2_inode *ip = GFS2_I(inode);
1989 struct gfs2_holder i_gh;
1992 error = gfs2_qa_get(ip);
1996 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
2000 error = may_setattr(&nop_mnt_idmap, inode, attr->ia_valid);
2004 error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
2008 if (attr->ia_valid & ATTR_SIZE)
2009 error = gfs2_setattr_size(inode, attr->ia_size);
2010 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
2011 error = setattr_chown(inode, attr);
2013 error = gfs2_setattr_simple(inode, attr);
2014 if (!error && attr->ia_valid & ATTR_MODE)
2015 error = posix_acl_chmod(&nop_mnt_idmap, dentry,
2021 mark_inode_dirty(inode);
2022 gfs2_glock_dq_uninit(&i_gh);
2029 * gfs2_getattr - Read out an inode's attributes
2030 * @idmap: idmap of the mount the inode was found from
2031 * @path: Object to query
2032 * @stat: The inode's stats
2033 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
2034 * @flags: AT_STATX_xxx setting
2036 * This may be called from the VFS directly, or from within GFS2 with the
2037 * inode locked, so we look to see if the glock is already locked and only
2038 * lock the glock if its not already been done. Note that its the NFS
2039 * readdirplus operation which causes this to be called (from filldir)
2040 * with the glock already held.
2045 static int gfs2_getattr(struct mnt_idmap *idmap,
2046 const struct path *path, struct kstat *stat,
2047 u32 request_mask, unsigned int flags)
2049 struct inode *inode = d_inode(path->dentry);
2050 struct gfs2_inode *ip = GFS2_I(inode);
2051 struct gfs2_holder gh;
2055 gfs2_holder_mark_uninitialized(&gh);
2056 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
2057 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
2062 gfsflags = ip->i_diskflags;
2063 if (gfsflags & GFS2_DIF_APPENDONLY)
2064 stat->attributes |= STATX_ATTR_APPEND;
2065 if (gfsflags & GFS2_DIF_IMMUTABLE)
2066 stat->attributes |= STATX_ATTR_IMMUTABLE;
2068 stat->attributes_mask |= (STATX_ATTR_APPEND |
2069 STATX_ATTR_COMPRESSED |
2070 STATX_ATTR_ENCRYPTED |
2071 STATX_ATTR_IMMUTABLE |
2074 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
2076 if (gfs2_holder_initialized(&gh))
2077 gfs2_glock_dq_uninit(&gh);
2082 static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2085 struct gfs2_inode *ip = GFS2_I(inode);
2086 struct gfs2_holder gh;
2089 inode_lock_shared(inode);
2091 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2095 ret = iomap_fiemap(inode, fieinfo, start, len, &gfs2_iomap_ops);
2097 gfs2_glock_dq_uninit(&gh);
2100 inode_unlock_shared(inode);
2104 loff_t gfs2_seek_data(struct file *file, loff_t offset)
2106 struct inode *inode = file->f_mapping->host;
2107 struct gfs2_inode *ip = GFS2_I(inode);
2108 struct gfs2_holder gh;
2111 inode_lock_shared(inode);
2112 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2114 ret = iomap_seek_data(inode, offset, &gfs2_iomap_ops);
2115 gfs2_glock_dq_uninit(&gh);
2116 inode_unlock_shared(inode);
2120 return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
2123 loff_t gfs2_seek_hole(struct file *file, loff_t offset)
2125 struct inode *inode = file->f_mapping->host;
2126 struct gfs2_inode *ip = GFS2_I(inode);
2127 struct gfs2_holder gh;
2130 inode_lock_shared(inode);
2131 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2133 ret = iomap_seek_hole(inode, offset, &gfs2_iomap_ops);
2134 gfs2_glock_dq_uninit(&gh);
2135 inode_unlock_shared(inode);
2139 return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
2142 static int gfs2_update_time(struct inode *inode, int flags)
2144 struct gfs2_inode *ip = GFS2_I(inode);
2145 struct gfs2_glock *gl = ip->i_gl;
2146 struct gfs2_holder *gh;
2149 gh = gfs2_glock_is_locked_by_me(gl);
2150 if (gh && !gfs2_glock_is_held_excl(gl)) {
2152 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, gh);
2153 error = gfs2_glock_nq(gh);
2157 generic_update_time(inode, flags);
2161 static const struct inode_operations gfs2_file_iops = {
2162 .permission = gfs2_permission,
2163 .setattr = gfs2_setattr,
2164 .getattr = gfs2_getattr,
2165 .listxattr = gfs2_listxattr,
2166 .fiemap = gfs2_fiemap,
2167 .get_inode_acl = gfs2_get_acl,
2168 .set_acl = gfs2_set_acl,
2169 .update_time = gfs2_update_time,
2170 .fileattr_get = gfs2_fileattr_get,
2171 .fileattr_set = gfs2_fileattr_set,
2174 static const struct inode_operations gfs2_dir_iops = {
2175 .create = gfs2_create,
2176 .lookup = gfs2_lookup,
2178 .unlink = gfs2_unlink,
2179 .symlink = gfs2_symlink,
2180 .mkdir = gfs2_mkdir,
2181 .rmdir = gfs2_unlink,
2182 .mknod = gfs2_mknod,
2183 .rename = gfs2_rename2,
2184 .permission = gfs2_permission,
2185 .setattr = gfs2_setattr,
2186 .getattr = gfs2_getattr,
2187 .listxattr = gfs2_listxattr,
2188 .fiemap = gfs2_fiemap,
2189 .get_inode_acl = gfs2_get_acl,
2190 .set_acl = gfs2_set_acl,
2191 .update_time = gfs2_update_time,
2192 .atomic_open = gfs2_atomic_open,
2193 .fileattr_get = gfs2_fileattr_get,
2194 .fileattr_set = gfs2_fileattr_set,
2197 static const struct inode_operations gfs2_symlink_iops = {
2198 .get_link = gfs2_get_link,
2199 .permission = gfs2_permission,
2200 .setattr = gfs2_setattr,
2201 .getattr = gfs2_getattr,
2202 .listxattr = gfs2_listxattr,
2203 .fiemap = gfs2_fiemap,