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