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