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