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