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