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