blob: 13d6e55d19e4bb2a9afa6c8b80980d878de6a109 [file] [log] [blame]
Thomas Gleixner7336d0e2019-05-31 01:09:56 -07001// SPDX-License-Identifier: GPL-2.0-only
David Teiglandb3b94fa2006-01-16 16:50:04 +00002/*
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehousef2741d92011-05-13 12:11:17 +01004 * Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00005 */
6
David Teiglandb3b94fa2006-01-16 16:50:04 +00007#include <linux/slab.h>
8#include <linux/spinlock.h>
9#include <linux/completion.h>
10#include <linux/buffer_head.h>
11#include <linux/namei.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000012#include <linux/mm.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010013#include <linux/cred.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000014#include <linux/xattr.h>
15#include <linux/posix_acl.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050016#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050017#include <linux/crc32.h>
Bob Petersonaac1a552017-02-16 21:13:54 +010018#include <linux/iomap.h>
Steven Whitehouse194c0112011-05-09 14:06:38 +010019#include <linux/security.h>
Christoph Hellwig10c5db22020-05-23 09:30:11 +020020#include <linux/fiemap.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080021#include <linux/uaccess.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000022
23#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050024#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000025#include "acl.h"
26#include "bmap.h"
27#include "dir.h"
Steven Whitehouse307cf6e2009-08-26 18:51:04 +010028#include "xattr.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029#include "glock.h"
30#include "inode.h"
31#include "meta_io.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000032#include "quota.h"
33#include "rgrp.h"
34#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050035#include "util.h"
Steven Whitehouseb2760582008-10-14 16:05:55 +010036#include "super.h"
Steven Whitehouse194c0112011-05-09 14:06:38 +010037#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000038
Andreas Gruenbachere3a77ee2020-11-25 21:14:15 +010039static const struct inode_operations gfs2_file_iops;
40static const struct inode_operations gfs2_dir_iops;
41static const struct inode_operations gfs2_symlink_iops;
42
Andreas Gruenbachercda9dd42016-06-14 12:24:50 -050043static int iget_test(struct inode *inode, void *opaque)
Steven Whitehouse194c0112011-05-09 14:06:38 +010044{
Andreas Gruenbachercda9dd42016-06-14 12:24:50 -050045 u64 no_addr = *(u64 *)opaque;
46
47 return GFS2_I(inode)->i_no_addr == no_addr;
48}
49
50static 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
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -050059static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
60{
61 struct inode *inode;
62
63repeat:
Andreas Gruenbachercda9dd42016-06-14 12:24:50 -050064 inode = iget5_locked(sb, no_addr, iget_test, iget_set, &no_addr);
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -050065 if (!inode)
66 return inode;
67 if (is_bad_inode(inode)) {
68 iput(inode);
69 goto repeat;
70 }
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -050071 return inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +010072}
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
82static 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
Steven Whitehouse194c0112011-05-09 14:06:38 +0100110 * @type: The type of the inode
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500111 * @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;
Andreas Gruenbacher61b91cf2017-08-01 09:54:33 -0500114 * GFS2_BLKST_FREE to indicate not to verify)
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500115 *
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.
Steven Whitehouse194c0112011-05-09 14:06:38 +0100121 *
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100122 * 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 *
Steven Whitehouse194c0112011-05-09 14:06:38 +0100126 * Returns: A VFS inode, or an error
127 */
128
129struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500130 u64 no_addr, u64 no_formal_ino,
131 unsigned int blktype)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100132{
133 struct inode *inode;
134 struct gfs2_inode *ip;
135 struct gfs2_glock *io_gl = NULL;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500136 struct gfs2_holder i_gh;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100137 int error;
138
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500139 gfs2_holder_mark_uninitialized(&i_gh);
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500140 inode = gfs2_iget(sb, no_addr);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100141 if (!inode)
Steven Whitehouseac3beb62014-01-16 10:31:13 +0000142 return ERR_PTR(-ENOMEM);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100143
Bob Petersone97321f2016-04-12 16:14:26 -0400144 ip = GFS2_I(inode);
Bob Petersone97321f2016-04-12 16:14:26 -0400145
Steven Whitehouse194c0112011-05-09 14:06:38 +0100146 if (inode->i_state & I_NEW) {
147 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100148
149 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
150 if (unlikely(error))
151 goto fail;
Andreas Gruenbacher4fd1a572017-06-30 07:47:15 -0500152 flush_delayed_work(&ip->i_gl->gl_work);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100153
154 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
155 if (unlikely(error))
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100156 goto fail;
Andreas Gruenbacherdd0ecf542020-11-30 16:07:25 +0100157 if (blktype != GFS2_BLKST_UNLINKED)
158 gfs2_cancel_delete_work(io_gl);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100159
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500160 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)
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100169 goto fail;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500170
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100171 error = -ESTALE;
172 if (no_formal_ino &&
173 gfs2_inode_already_deleted(ip->i_gl, no_formal_ino))
174 goto fail;
175
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500176 if (blktype != GFS2_BLKST_FREE) {
177 error = gfs2_check_blk_type(sdp, no_addr,
178 blktype);
179 if (error)
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100180 goto fail;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500181 }
182 }
183
Bob Peterson4d7c18c2017-07-18 12:15:01 -0500184 glock_set_object(ip->i_gl, ip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100185 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))
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100188 goto fail;
Andreas Gruenbacher4fd1a572017-06-30 07:47:15 -0500189 glock_set_object(ip->i_iopen_gh.gh_gl, ip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100190 gfs2_glock_put(io_gl);
191 io_gl = NULL;
192
Andreas Gruenbacher2b0fb352020-01-15 06:26:00 +0100193 /* 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
Steven Whitehouse194c0112011-05-09 14:06:38 +0100197 if (type == DT_UNKNOWN) {
198 /* Inode glock must be locked already */
199 error = gfs2_inode_refresh(GFS2_I(inode));
200 if (error)
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100201 goto fail;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100202 } else {
Andreas Gruenbacher2b0fb352020-01-15 06:26:00 +0100203 ip->i_no_formal_ino = no_formal_ino;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100204 inode->i_mode = DT2IF(type);
205 }
206
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100207 if (gfs2_holder_initialized(&i_gh))
208 gfs2_glock_dq_uninit(&i_gh);
Andreas Gruenbacher332f51d2016-09-26 13:24:34 -0500209
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100210 gfs2_set_iop(inode);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100211 }
212
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100213 if (no_formal_ino && ip->i_no_formal_ino &&
214 no_formal_ino != ip->i_no_formal_ino) {
Andreas Gruenbacher5902f4d2020-06-09 14:33:11 +0200215 error = -ESTALE;
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100216 if (inode->i_state & I_NEW)
217 goto fail;
218 iput(inode);
Andreas Gruenbacher5902f4d2020-06-09 14:33:11 +0200219 return ERR_PTR(error);
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100220 }
221
222 if (inode->i_state & I_NEW)
223 unlock_new_inode(inode);
224
Steven Whitehouse194c0112011-05-09 14:06:38 +0100225 return inode;
226
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100227fail:
Steven Whitehouse194c0112011-05-09 14:06:38 +0100228 if (io_gl)
229 gfs2_glock_put(io_gl);
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500230 if (gfs2_holder_initialized(&i_gh))
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500231 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100232 iget_failed(inode);
233 return ERR_PTR(error);
234}
235
Andreas Gruenbacher6bdcade2020-01-15 05:31:38 +0100236/**
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 */
Steven Whitehouse194c0112011-05-09 14:06:38 +0100243struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
Andreas Gruenbacher6bdcade2020-01-15 05:31:38 +0100244 u64 no_formal_ino, unsigned int blktype)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100245{
246 struct super_block *sb = sdp->sd_vfs;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500247 struct inode *inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100248 int error;
249
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100250 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, no_formal_ino,
251 blktype);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100252 if (IS_ERR(inode))
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500253 return inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100254
Steven Whitehouse194c0112011-05-09 14:06:38 +0100255 if (no_formal_ino) {
Steven Whitehouse194c0112011-05-09 14:06:38 +0100256 error = -EIO;
257 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
258 goto fail_iput;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100259 }
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500260 return inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100261
Steven Whitehouse194c0112011-05-09 14:06:38 +0100262fail_iput:
263 iput(inode);
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500264 return ERR_PTR(error);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100265}
266
267
268struct 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
301struct 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;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100309
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500310 gfs2_holder_mark_uninitialized(&d_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100311 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 &&
David Howells2b0143b2015-03-17 22:25:59 +0000316 dir == d_inode(sb->s_root))) {
Steven Whitehouse194c0112011-05-09 14:06:38 +0100317 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);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100325 }
326
327 if (!is_root) {
Christian Brauner549c7292021-01-21 14:19:43 +0100328 error = gfs2_permission(&init_user_ns, dir, MAY_EXEC);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100329 if (error)
330 goto out;
331 }
332
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100333 inode = gfs2_dir_search(dir, name, false);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100334 if (IS_ERR(inode))
335 error = PTR_ERR(inode);
336out:
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500337 if (gfs2_holder_initialized(&d_gh))
Steven Whitehouse194c0112011-05-09 14:06:38 +0100338 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
353static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
Al Viro175a4eb2011-07-26 03:30:54 -0400354 umode_t mode)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100355{
356 int error;
357
Christian Brauner549c7292021-01-21 14:19:43 +0100358 error = gfs2_permission(&init_user_ns, &dip->i_inode,
359 MAY_WRITE | MAY_EXEC);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100360 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
Steven Whitehouse194c0112011-05-09 14:06:38 +0100367 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
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000375static void munge_mode_uid_gid(const struct gfs2_inode *dip,
376 struct inode *inode)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100377{
378 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800379 (dip->i_inode.i_mode & S_ISUID) &&
380 !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000381 if (S_ISDIR(inode->i_mode))
382 inode->i_mode |= S_ISUID;
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800383 else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000384 inode->i_mode &= ~07111;
385 inode->i_uid = dip->i_inode.i_uid;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100386 } else
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000387 inode->i_uid = current_fsuid();
Steven Whitehouse194c0112011-05-09 14:06:38 +0100388
389 if (dip->i_inode.i_mode & S_ISGID) {
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000390 if (S_ISDIR(inode->i_mode))
391 inode->i_mode |= S_ISGID;
392 inode->i_gid = dip->i_inode.i_gid;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100393 } else
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000394 inode->i_gid = current_fsgid();
Steven Whitehouse194c0112011-05-09 14:06:38 +0100395}
396
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000397static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100398{
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000399 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000400 struct gfs2_alloc_parms ap = { .target = *dblocks, .aflags = flags, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100401 int error;
402
Abhi Dasb8fbf472015-03-18 12:03:41 -0500403 error = gfs2_quota_lock_check(ip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100404 if (error)
405 goto out;
406
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100407 error = gfs2_inplace_reserve(ip, &ap);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000408 if (error)
409 goto out_quota;
410
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000411 error = gfs2_trans_begin(sdp, (*dblocks * RES_RG_BIT) + RES_STATFS + RES_QUOTA, 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100412 if (error)
413 goto out_ipreserv;
414
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000415 error = gfs2_alloc_blocks(ip, &ip->i_no_addr, dblocks, 1, &ip->i_generation);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000416 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;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100419
420 gfs2_trans_end(sdp);
421
422out_ipreserv:
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000423 gfs2_inplace_release(ip);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000424out_quota:
425 gfs2_quota_unlock(ip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100426out:
Steven Whitehouse194c0112011-05-09 14:06:38 +0100427 return error;
428}
429
Steven Whitehousef2741d92011-05-13 12:11:17 +0100430static void gfs2_init_dir(struct buffer_head *dibh,
431 const struct gfs2_inode *parent)
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100432{
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
Steven Whitehouse194c0112011-05-09 14:06:38 +0100447/**
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000448 * 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
455static 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
474/**
Steven Whitehouse194c0112011-05-09 14:06:38 +0100475 * init_dinode - Fill in a new dinode structure
Steven Whitehousef2741d92011-05-13 12:11:17 +0100476 * @dip: The directory this inode is being created in
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000477 * @ip: The inode
Steven Whitehousef2741d92011-05-13 12:11:17 +0100478 * @symname: The symlink destination (if a symlink)
Steven Whitehousef2741d92011-05-13 12:11:17 +0100479 * @bhp: The buffer head (returned to caller)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100480 *
481 */
482
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000483static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000484 const char *symname)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100485{
Steven Whitehouse194c0112011-05-09 14:06:38 +0100486 struct gfs2_dinode *di;
487 struct buffer_head *dibh;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100488
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000489 dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000490 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100491 di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000492 gfs2_dinode_out(ip, di);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100493
Al Viro6f247842021-01-31 19:23:55 -0500494 di->di_major = cpu_to_be32(imajor(&ip->i_inode));
495 di->di_minor = cpu_to_be32(iminor(&ip->i_inode));
Steven Whitehouse194c0112011-05-09 14:06:38 +0100496 di->__pad1 = 0;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100497 di->__pad2 = 0;
498 di->__pad3 = 0;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100499 memset(&di->__pad4, 0, sizeof(di->__pad4));
Steven Whitehouse194c0112011-05-09 14:06:38 +0100500 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000501 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouse160b4022011-05-13 10:34:59 +0100502
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000503 switch(ip->i_inode.i_mode & S_IFMT) {
Steven Whitehouse160b4022011-05-13 10:34:59 +0100504 case S_IFDIR:
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100505 gfs2_init_dir(dibh, dip);
Steven Whitehouse160b4022011-05-13 10:34:59 +0100506 break;
507 case S_IFLNK:
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000508 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
Steven Whitehouse160b4022011-05-13 10:34:59 +0100509 break;
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100510 }
511
Steven Whitehouse194c0112011-05-09 14:06:38 +0100512 set_buffer_uptodate(dibh);
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000513 brelse(dibh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100514}
515
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000516/**
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
530static 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
Steven Whitehouse194c0112011-05-09 14:06:38 +0100538static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000539 struct gfs2_inode *ip, struct gfs2_diradd *da)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100540{
541 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000542 struct gfs2_alloc_parms ap = { .target = da->nr_blocks, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100543 int error;
544
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000545 if (da->nr_blocks) {
Abhi Dasb8fbf472015-03-18 12:03:41 -0500546 error = gfs2_quota_lock_check(dip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100547 if (error)
548 goto fail_quota_locks;
549
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100550 error = gfs2_inplace_reserve(dip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100551 if (error)
552 goto fail_quota_locks;
553
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000554 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100555 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
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000563 error = gfs2_dir_add(&dip->i_inode, name, ip, da);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100564
Steven Whitehouse194c0112011-05-09 14:06:38 +0100565 gfs2_trans_end(sdp);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100566fail_ipreserv:
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000567 gfs2_inplace_release(dip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100568fail_quota_locks:
569 gfs2_quota_unlock(dip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100570 return error;
571}
572
H Hartley Sweeten46cc1e52011-09-23 15:51:32 -0700573static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -0400574 void *fs_info)
575{
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;
585 }
586 return err;
587}
588
Steven Whitehouse194c0112011-05-09 14:06:38 +0100589/**
Steven Whitehousef2741d92011-05-13 12:11:17 +0100590 * gfs2_create_inode - Create a new inode
591 * @dir: The parent directory
592 * @dentry: The new dentry
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100593 * @file: If non-NULL, the file which is being opened
Steven Whitehousef2741d92011-05-13 12:11:17 +0100594 * @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)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100598 *
Steven Whitehousef2741d92011-05-13 12:11:17 +0100599 * Returns: 0 on success, or error code
Steven Whitehouse194c0112011-05-09 14:06:38 +0100600 */
601
Steven Whitehousef2741d92011-05-13 12:11:17 +0100602static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100603 struct file *file,
Al Viro175a4eb2011-07-26 03:30:54 -0400604 umode_t mode, dev_t dev, const char *symname,
Al Virob452a452018-06-08 13:06:28 -0400605 unsigned int size, int excl)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100606{
Steven Whitehousef2741d92011-05-13 12:11:17 +0100607 const struct qstr *name = &dentry->d_name;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800608 struct posix_acl *default_acl, *acl;
Steven Whitehousef2741d92011-05-13 12:11:17 +0100609 struct gfs2_holder ghs[2];
Steven Whitehouse194c0112011-05-09 14:06:38 +0100610 struct inode *inode = NULL;
Bob Peterson8e2e0042012-07-19 08:12:40 -0400611 struct gfs2_inode *dip = GFS2_I(dir), *ip;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100612 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Andreas Gruenbachera55a47a2020-11-27 14:23:04 +0100613 struct gfs2_glock *io_gl;
Bob Peterson783013c2015-12-04 10:19:14 -0600614 int error, free_vfs_inode = 1;
Steven Whitehouse9dbe9612012-10-31 10:37:10 +0000615 u32 aflags = 0;
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000616 unsigned blocks = 1;
Bob Peterson19aeb5a62014-09-29 08:52:04 -0400617 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100618
619 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousef2741d92011-05-13 12:11:17 +0100620 return -ENAMETOOLONG;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100621
Bob Peterson2fba46a2020-02-27 12:47:53 -0600622 error = gfs2_qa_get(dip);
Bob Peterson0a305e42012-06-06 11:17:59 +0100623 if (error)
624 return error;
625
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000626 error = gfs2_rindex_update(sdp);
627 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -0600628 goto fail;
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000629
Steven Whitehousef2741d92011-05-13 12:11:17 +0100630 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100631 if (error)
632 goto fail;
Andreas Gruenbachere0b62e22017-06-30 08:16:46 -0500633 gfs2_holder_mark_uninitialized(ghs + 1);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100634
635 error = create_ok(dip, name, mode);
636 if (error)
637 goto fail_gunlock;
638
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100639 inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
640 error = PTR_ERR(inode);
641 if (!IS_ERR(inode)) {
Al Viro571a4b52014-11-19 19:34:49 +0000642 if (S_ISDIR(inode->i_mode)) {
643 iput(inode);
644 inode = ERR_PTR(-EISDIR);
645 goto fail_gunlock;
646 }
Al Viro44bb31b2014-11-19 19:35:24 +0000647 d_instantiate(dentry, inode);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100648 error = 0;
Miklos Szeredi0d0d1102013-09-16 14:52:00 +0200649 if (file) {
Al Viro44bb31b2014-11-19 19:35:24 +0000650 if (S_ISREG(inode->i_mode))
Al Virobe12af32018-06-08 11:44:56 -0400651 error = finish_open(file, dentry, gfs2_open_common);
Al Viro44bb31b2014-11-19 19:35:24 +0000652 else
653 error = finish_no_open(file, NULL);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100654 }
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100655 gfs2_glock_dq_uninit(ghs);
Bob Peterson2297ab62020-05-04 10:18:43 -0500656 goto fail;
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100657 } else if (error != -ENOENT) {
658 goto fail_gunlock;
659 }
660
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000661 error = gfs2_diradd_alloc_required(dir, name, &da);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000662 if (error < 0)
663 goto fail_gunlock;
664
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000665 inode = new_inode(sdp->sd_vfs);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000666 error = -ENOMEM;
667 if (!inode)
668 goto fail_gunlock;
669
Christoph Hellwige01580b2013-12-20 05:16:52 -0800670 error = posix_acl_create(dir, &mode, &default_acl, &acl);
671 if (error)
Bob Peterson783013c2015-12-04 10:19:14 -0600672 goto fail_gunlock;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800673
Bob Peterson8e2e0042012-07-19 08:12:40 -0400674 ip = GFS2_I(inode);
Bob Peterson2fba46a2020-02-27 12:47:53 -0600675 error = gfs2_qa_get(ip);
Steven Whitehouseff7f4cb2012-09-10 10:03:50 +0100676 if (error)
Christoph Hellwige01580b2013-12-20 05:16:52 -0800677 goto fail_free_acls;
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000678
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000679 inode->i_mode = mode;
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000680 set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000681 inode->i_rdev = dev;
682 inode->i_size = size;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700683 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000684 munge_mode_uid_gid(dip, inode);
Abhi Das00a158b2014-09-18 21:40:28 -0500685 check_and_update_goal(dip);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000686 ip->i_goal = dip->i_goal;
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000687 ip->i_diskflags = 0;
688 ip->i_eattr = 0;
689 ip->i_height = 0;
690 ip->i_depth = 0;
691 ip->i_entries = 0;
Bob Petersoncc963a12017-03-16 15:29:13 -0400692 ip->i_no_addr = 0; /* Temporarily zero until real addr is assigned */
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000693
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 }
Abhi Dasacc546f2015-11-10 15:07:26 -0600707
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
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000712 gfs2_set_inode_flags(inode);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000713
David Howells2b0143b2015-03-17 22:25:59 +0000714 if ((GFS2_I(d_inode(sdp->sd_root_dir)) == dip) ||
Steven Whitehouse9dbe9612012-10-31 10:37:10 +0000715 (dip->i_diskflags & GFS2_DIF_TOPDIR))
716 aflags |= GFS2_AF_ORLOV;
717
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000718 if (default_acl || acl)
719 blocks++;
720
721 error = alloc_dinode(ip, aflags, &blocks);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000722 if (error)
723 goto fail_free_inode;
724
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000725 gfs2_set_inode_blocks(inode, blocks);
726
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000727 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
728 if (error)
729 goto fail_free_inode;
Andreas Gruenbacher98e5a912017-07-19 11:10:19 -0500730 flush_delayed_work(&ip->i_gl->gl_work);
Andreas Gruenbacher6f6597ba2017-06-30 07:55:08 -0500731 glock_set_object(ip->i_gl, ip);
Andreas Gruenbacher98e5a912017-07-19 11:10:19 -0500732
Andreas Gruenbacherdd0ecf542020-11-30 16:07:25 +0100733 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000734 if (error)
735 goto fail_free_inode;
Andreas Gruenbacherdd0ecf542020-11-30 16:07:25 +0100736 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;
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000742
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000743 error = gfs2_trans_begin(sdp, blocks, 0);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000744 if (error)
Andreas Gruenbacherdd0ecf542020-11-30 16:07:25 +0100745 goto fail_gunlock2;
Bob Peterson0a305e42012-06-06 11:17:59 +0100746
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000747 if (blocks > 1) {
748 ip->i_eattr = ip->i_no_addr + 1;
749 gfs2_init_xattr(ip);
750 }
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000751 init_dinode(dip, ip, symname);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000752 gfs2_trans_end(sdp);
753
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000754 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
755 if (error)
756 goto fail_gunlock2;
757
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000758 gfs2_set_iop(inode);
759 insert_inode_hash(inode);
760
Bob Peterson783013c2015-12-04 10:19:14 -0600761 free_vfs_inode = 0; /* After this point, the inode is no longer
762 considered free. Any failures need to undo
763 the gfs2 structures. */
Christoph Hellwige01580b2013-12-20 05:16:52 -0800764 if (default_acl) {
Al Viro1a39ba92016-05-13 03:59:17 +0200765 error = __gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100766 if (error)
767 goto fail_gunlock3;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800768 posix_acl_release(default_acl);
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100769 default_acl = NULL;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800770 }
771 if (acl) {
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100772 error = __gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS);
773 if (error)
774 goto fail_gunlock3;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800775 posix_acl_release(acl);
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100776 acl = NULL;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800777 }
778
Bob Petersonf45dc262014-03-19 09:37:00 -0400779 error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name,
780 &gfs2_initxattrs, NULL);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100781 if (error)
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000782 goto fail_gunlock3;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100783
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000784 error = link_dinode(dip, name, ip, &da);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100785 if (error)
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000786 goto fail_gunlock3;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100787
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000788 mark_inode_dirty(inode);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100789 d_instantiate(dentry, inode);
Bob Peterson2c47c1b2019-11-19 11:40:46 -0500790 /* After instantiate, errors should result in evict which will destroy
791 * both inode and iopen glocks properly. */
Miklos Szeredic5bf8fe2013-09-16 14:52:03 +0200792 if (file) {
Al Viro73a09dd2018-06-08 13:22:02 -0400793 file->f_mode |= FMODE_CREATED;
Al Virobe12af32018-06-08 11:44:56 -0400794 error = finish_open(file, dentry, gfs2_open_common);
Miklos Szeredic5bf8fe2013-09-16 14:52:03 +0200795 }
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000796 gfs2_glock_dq_uninit(ghs);
Bob Peterson2297ab62020-05-04 10:18:43 -0500797 gfs2_qa_put(ip);
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000798 gfs2_glock_dq_uninit(ghs + 1);
Bob Peterson2c47c1b2019-11-19 11:40:46 -0500799 gfs2_glock_put(io_gl);
Bob Peterson2297ab62020-05-04 10:18:43 -0500800 gfs2_qa_put(dip);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100801 return error;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100802
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000803fail_gunlock3:
Bob Peterson9c1b2802017-07-18 12:26:07 -0500804 glock_clear_object(io_gl, ip);
Bob Peterson783013c2015-12-04 10:19:14 -0600805 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100806fail_gunlock2:
Andreas Gruenbacherdd0ecf542020-11-30 16:07:25 +0100807 glock_clear_object(io_gl, ip);
Bob Peterson2c47c1b2019-11-19 11:40:46 -0500808 gfs2_glock_put(io_gl);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000809fail_free_inode:
Bob Peterson9c1b2802017-07-18 12:26:07 -0500810 if (ip->i_gl) {
811 glock_clear_object(ip->i_gl, ip);
Bob Peterson1a0b00d2020-06-01 11:37:09 -0400812 if (free_vfs_inode) /* else evict will do the put for us */
813 gfs2_glock_put(ip->i_gl);
Bob Peterson9c1b2802017-07-18 12:26:07 -0500814 }
Andreas Gruenbacher15955482020-03-06 10:32:35 -0600815 gfs2_rs_delete(ip, NULL);
816 gfs2_qa_put(ip);
Christoph Hellwige01580b2013-12-20 05:16:52 -0800817fail_free_acls:
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100818 posix_acl_release(default_acl);
819 posix_acl_release(acl);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100820fail_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000821 gfs2_dir_no_add(&da);
Steven Whitehousef2741d92011-05-13 12:11:17 +0100822 gfs2_glock_dq_uninit(ghs);
Kefeng Wang15a798f2019-06-05 22:24:24 +0800823 if (!IS_ERR_OR_NULL(inode)) {
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000824 clear_nlink(inode);
Abhi Das05978802014-03-31 10:33:17 -0500825 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);
Steven Whitehouse40ac2182011-08-02 13:17:27 +0100829 iput(inode);
830 }
Andreas Gruenbachere0b62e22017-06-30 08:16:46 -0500831 if (gfs2_holder_initialized(ghs + 1))
832 gfs2_glock_dq_uninit(ghs + 1);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100833fail:
Bob Peterson2fba46a2020-02-27 12:47:53 -0600834 gfs2_qa_put(dip);
Steven Whitehousef2741d92011-05-13 12:11:17 +0100835 return error;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100836}
Steven Whitehousef2741d92011-05-13 12:11:17 +0100837
David Teiglandb3b94fa2006-01-16 16:50:04 +0000838/**
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
Christian Brauner549c7292021-01-21 14:19:43 +0100847static int gfs2_create(struct user_namespace *mnt_userns, struct inode *dir,
848 struct dentry *dentry, umode_t mode, bool excl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000849{
Al Virob452a452018-06-08 13:06:28 -0400850 return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000851}
852
853/**
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100854 * __gfs2_lookup - Look up a filename in a directory and return its inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000855 * @dir: The directory inode
856 * @dentry: The dentry of the new inode
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100857 * @file: File to be opened
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000859 *
860 * Returns: errno
861 */
862
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100863static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry,
Al Virob452a452018-06-08 13:06:28 -0400864 struct file *file)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000865{
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100866 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);
Benjamin Coddington7b7a9112014-09-10 14:09:20 -0400873 if (inode == NULL) {
874 d_add(dentry, NULL);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100875 return NULL;
Benjamin Coddington7b7a9112014-09-10 14:09:20 -0400876 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100877 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);
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000885 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100886
887 d = d_splice_alias(inode, dentry);
J. Bruce Fieldsd57b9c92014-01-16 13:51:07 -0500888 if (IS_ERR(d)) {
J. Bruce Fieldsd57b9c92014-01-16 13:51:07 -0500889 gfs2_glock_dq_uninit(&gh);
890 return d;
891 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100892 if (file && S_ISREG(inode->i_mode))
Al Virobe12af32018-06-08 11:44:56 -0400893 error = finish_open(file, dentry, gfs2_open_common);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100894
895 gfs2_glock_dq_uninit(&gh);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +0100896 if (error) {
897 dput(d);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100898 return ERR_PTR(error);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +0100899 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100900 return d;
901}
902
903static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
904 unsigned flags)
905{
Al Virob452a452018-06-08 13:06:28 -0400906 return __gfs2_lookup(dir, dentry, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000907}
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
921static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
922 struct dentry *dentry)
923{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400924 struct gfs2_inode *dip = GFS2_I(dir);
925 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000926 struct inode *inode = d_inode(old_dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400927 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000928 struct gfs2_holder ghs[2];
Steven Whitehouse2baee032011-05-09 12:08:36 +0100929 struct buffer_head *dibh;
Bob Peterson19aeb5a62014-09-29 08:52:04 -0400930 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931 int error;
932
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500933 if (S_ISDIR(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000934 return -EPERM;
935
Bob Peterson2fba46a2020-02-27 12:47:53 -0600936 error = gfs2_qa_get(dip);
Bob Peterson0a305e42012-06-06 11:17:59 +0100937 if (error)
938 return error;
939
David Teiglandb3b94fa2006-01-16 16:50:04 +0000940 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
Bob Peterson72dbf472008-08-12 13:39:29 -0500943 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000944 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500945 goto out_parent;
946
947 error = gfs2_glock_nq(ghs + 1); /* child */
948 if (error)
949 goto out_child;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000950
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100951 error = -ENOENT;
952 if (inode->i_nlink == 0)
953 goto out_gunlock;
954
Christian Brauner549c7292021-01-21 14:19:43 +0100955 error = gfs2_permission(&init_user_ns, dir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000956 if (error)
957 goto out_gunlock;
958
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100959 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000960 switch (error) {
961 case -ENOENT:
962 break;
963 case 0:
964 error = -EEXIST;
965 default:
966 goto out_gunlock;
967 }
968
969 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500970 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000971 goto out_gunlock;
972 error = -EFBIG;
Steven Whitehousead6203f22008-11-03 13:59:19 +0000973 if (dip->i_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000974 goto out_gunlock;
975 error = -EPERM;
976 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
977 goto out_gunlock;
978 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500979 if (!ip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000980 goto out_gunlock;
981 error = -EMLINK;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500982 if (ip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000983 goto out_gunlock;
984
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000985 error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da);
Steven Whitehousec7526662006-03-20 12:30:04 -0500986 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000987 goto out_gunlock;
988
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000989 if (da.nr_blocks) {
990 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
Abhi Dasb8fbf472015-03-18 12:03:41 -0500991 error = gfs2_quota_lock_check(dip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000992 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -0400993 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000994
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100995 error = gfs2_inplace_reserve(dip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000996 if (error)
997 goto out_gunlock_q;
998
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000999 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001000 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
Steven Whitehouse2baee032011-05-09 12:08:36 +01001008 error = gfs2_meta_inode_buffer(ip, &dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001009 if (error)
1010 goto out_end_trans;
1011
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001012 error = gfs2_dir_add(dir, &dentry->d_name, ip, &da);
Steven Whitehouse2baee032011-05-09 12:08:36 +01001013 if (error)
1014 goto out_brelse;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001015
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001016 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse2baee032011-05-09 12:08:36 +01001017 inc_nlink(&ip->i_inode);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001018 ip->i_inode.i_ctime = current_time(&ip->i_inode);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001019 ihold(inode);
1020 d_instantiate(dentry, inode);
1021 mark_inode_dirty(inode);
Steven Whitehouse2baee032011-05-09 12:08:36 +01001022
1023out_brelse:
1024 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001025out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001026 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001027out_ipres:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001028 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001029 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001030out_gunlock_q:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001031 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001032 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001033out_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001034 gfs2_dir_no_add(&da);
Bob Peterson72dbf472008-08-12 13:39:29 -05001035 gfs2_glock_dq(ghs + 1);
1036out_child:
1037 gfs2_glock_dq(ghs);
1038out_parent:
Bob Peterson2297ab62020-05-04 10:18:43 -05001039 gfs2_qa_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001040 gfs2_holder_uninit(ghs);
1041 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001042 return error;
1043}
1044
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001045/*
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
1056static 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) &&
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001065 !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
1066 !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001067 return -EPERM;
1068
1069 if (IS_APPEND(&dip->i_inode))
1070 return -EPERM;
1071
Christian Brauner549c7292021-01-21 14:19:43 +01001072 error = gfs2_permission(&init_user_ns, &dip->i_inode,
1073 MAY_WRITE | MAY_EXEC);
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001074 if (error)
1075 return error;
1076
Fabian Frederick37975f12014-10-09 22:49:21 +02001077 return gfs2_dir_check(&dip->i_inode, name, ip);
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001078}
1079
David Teiglandb3b94fa2006-01-16 16:50:04 +00001080/**
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001081 * 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
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001084 * @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
1092static int gfs2_unlink_inode(struct gfs2_inode *dip,
Bob Peterson4327a9b2012-11-12 13:03:29 -05001093 const struct dentry *dentry)
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001094{
David Howells2b0143b2015-03-17 22:25:59 +00001095 struct inode *inode = d_inode(dentry);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001096 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;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001104 inode->i_ctime = current_time(inode);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001105 if (S_ISDIR(inode->i_mode))
1106 clear_nlink(inode);
1107 else
1108 drop_nlink(inode);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001109 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
David Teiglandb3b94fa2006-01-16 16:50:04 +00001119 * @dentry: The file itself
1120 *
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001121 * This routine uses the type of the inode as a flag to figure out
1122 * whether this is an unlink or an rmdir.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001123 *
1124 * Returns: errno
1125 */
1126
1127static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
1128{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001129 struct gfs2_inode *dip = GFS2_I(dir);
1130 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +00001131 struct inode *inode = d_inode(dentry);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001132 struct gfs2_inode *ip = GFS2_I(inode);
Russell Cattelanddee7602007-01-29 17:13:44 -06001133 struct gfs2_holder ghs[3];
1134 struct gfs2_rgrpd *rgd;
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001135 int error;
1136
1137 error = gfs2_rindex_update(sdp);
1138 if (error)
1139 return error;
1140
1141 error = -EROFS;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001142
Russell Cattelanddee7602007-01-29 17:13:44 -06001143 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
1144 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
1145
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001146 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
Steven Whitehousea365fbf2012-02-24 15:09:14 +00001147 if (!rgd)
Steven Whitehouse87654892011-11-08 14:04:20 +00001148 goto out_inodes;
Steven Whitehousea365fbf2012-02-24 15:09:14 +00001149
Bob Peterson4fc7ec32018-04-24 10:35:02 -07001150 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, LM_FLAG_NODE_SCOPE, ghs + 2);
Russell Cattelanddee7602007-01-29 17:13:44 -06001151
1152
Steven Whitehouse8497a462007-08-26 14:23:56 +01001153 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001154 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +01001155 goto out_parent;
1156
1157 error = gfs2_glock_nq(ghs + 1); /* child */
1158 if (error)
1159 goto out_child;
1160
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001161 error = -ENOENT;
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001162 if (inode->i_nlink == 0)
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001163 goto out_rgrp;
1164
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001165 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
Steven Whitehouse8497a462007-08-26 14:23:56 +01001171 error = gfs2_glock_nq(ghs + 2); /* rgrp */
1172 if (error)
1173 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001174
1175 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
1176 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -05001177 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001178
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001179 error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001180 if (error)
Bob Peterson2eb59092018-01-29 10:00:23 -07001181 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001182
Bob Peterson4327a9b2012-11-12 13:03:29 -05001183 error = gfs2_unlink_inode(dip, dentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001184 gfs2_trans_end(sdp);
Bob Peterson2eb59092018-01-29 10:00:23 -07001185
Bob Peterson72dbf472008-08-12 13:39:29 -05001186out_gunlock:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001187 gfs2_glock_dq(ghs + 2);
1188out_rgrp:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001189 gfs2_glock_dq(ghs + 1);
1190out_child:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001191 gfs2_glock_dq(ghs);
1192out_parent:
Steven Whitehouse87654892011-11-08 14:04:20 +00001193 gfs2_holder_uninit(ghs + 2);
1194out_inodes:
1195 gfs2_holder_uninit(ghs + 1);
Steven Whitehouse8497a462007-08-26 14:23:56 +01001196 gfs2_holder_uninit(ghs);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001197 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
Christian Brauner549c7292021-01-21 14:19:43 +01001209static int gfs2_symlink(struct user_namespace *mnt_userns, struct inode *dir,
1210 struct dentry *dentry, const char *symname)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001211{
Steven Whitehouse160b4022011-05-13 10:34:59 +01001212 unsigned int size;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001213
David Teiglandb3b94fa2006-01-16 16:50:04 +00001214 size = strlen(symname);
Andreas Gruenbacher235628c2017-11-14 16:53:12 +01001215 if (size >= gfs2_max_stuffed_size(GFS2_I(dir)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001216 return -ENAMETOOLONG;
1217
Al Virob452a452018-06-08 13:06:28 -04001218 return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001219}
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
Christian Brauner549c7292021-01-21 14:19:43 +01001230static int gfs2_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
1231 struct dentry *dentry, umode_t mode)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001232{
Andreas Gruenbacher235628c2017-11-14 16:53:12 +01001233 unsigned dsize = gfs2_max_stuffed_size(GFS2_I(dir));
Al Virob452a452018-06-08 13:06:28 -04001234 return gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001235}
1236
1237/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001238 * 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
Steven Whitehousef2741d92011-05-13 12:11:17 +01001242 * @dev: The device specification of the special file
David Teiglandb3b94fa2006-01-16 16:50:04 +00001243 *
1244 */
1245
Christian Brauner549c7292021-01-21 14:19:43 +01001246static int gfs2_mknod(struct user_namespace *mnt_userns, struct inode *dir,
1247 struct dentry *dentry, umode_t mode, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001248{
Al Virob452a452018-06-08 13:06:28 -04001249 return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001250}
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
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001259 *
1260 * Returns: error code or 0 for success
1261 */
1262
1263static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry,
Antonio Ospite86fbca42015-05-01 12:54:38 -05001264 struct file *file, unsigned flags,
Al Viro44907d72018-06-08 13:32:02 -04001265 umode_t mode)
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001266{
1267 struct dentry *d;
1268 bool excl = !!(flags & O_EXCL);
1269
Al Viro00699ad2016-07-05 09:44:53 -04001270 if (!d_in_lookup(dentry))
Al Viro4d93bc32014-09-12 18:21:05 -04001271 goto skip_lookup;
1272
Al Virob452a452018-06-08 13:06:28 -04001273 d = __gfs2_lookup(dir, dentry, file);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001274 if (IS_ERR(d))
1275 return PTR_ERR(d);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001276 if (d != NULL)
1277 dentry = d;
David Howells2b0143b2015-03-17 22:25:59 +00001278 if (d_really_is_positive(dentry)) {
Al Viroaad888f2018-06-08 12:58:04 -04001279 if (!(file->f_mode & FMODE_OPENED))
Al Viroec7d8792014-11-19 19:35:58 +00001280 return finish_no_open(file, d);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001281 dput(d);
Al Viro21039132020-03-10 09:31:41 -04001282 return excl && (flags & O_CREAT) ? -EEXIST : 0;
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001283 }
1284
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001285 BUG_ON(d != NULL);
Al Viro4d93bc32014-09-12 18:21:05 -04001286
1287skip_lookup:
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001288 if (!(flags & O_CREAT))
1289 return -ENOENT;
1290
Al Virob452a452018-06-08 13:06:28 -04001291 return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001292}
1293
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001294/*
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
1305static 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;
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001310 int error = 0;
1311
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001312 igrab(dir);
1313
1314 for (;;) {
1315 if (dir == &this->i_inode) {
1316 error = -EINVAL;
1317 break;
1318 }
David Howells2b0143b2015-03-17 22:25:59 +00001319 if (dir == d_inode(sb->s_root)) {
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001320 error = 0;
1321 break;
1322 }
1323
Steven Whitehouse8d123582010-09-17 12:30:23 +01001324 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
Abhi Das48f8f712014-03-12 03:41:44 -05001325 if (!tmp) {
1326 error = -ENOENT;
1327 break;
1328 }
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001329 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
David Teiglandb3b94fa2006-01-16 16:50:04 +00001343/**
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001344 * 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
1352static int update_moved_ino(struct gfs2_inode *ip, struct gfs2_inode *ndip,
1353 int dir_rename)
1354{
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001355 if (dir_rename)
1356 return gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
1357
Deepa Dinamani078cd822016-09-14 07:48:04 -07001358 ip->i_inode.i_ctime = current_time(&ip->i_inode);
Andreas Gruenbacher83998cc2018-02-28 12:48:53 -07001359 mark_inode_dirty_sync(&ip->i_inode);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001360 return 0;
1361}
1362
1363
1364/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001365 * 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
1374static int gfs2_rename(struct inode *odir, struct dentry *odentry,
1375 struct inode *ndir, struct dentry *ndentry)
1376{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001377 struct gfs2_inode *odip = GFS2_I(odir);
1378 struct gfs2_inode *ndip = GFS2_I(ndir);
David Howells2b0143b2015-03-17 22:25:59 +00001379 struct gfs2_inode *ip = GFS2_I(d_inode(odentry));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001380 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001381 struct gfs2_sbd *sdp = GFS2_SB(odir);
Bob Petersonbc74aae2019-08-30 12:31:00 -05001382 struct gfs2_holder ghs[4], r_gh, rd_gh;
Russell Cattelanddee7602007-01-29 17:13:44 -06001383 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001384 unsigned int num_gh;
1385 int dir_rename = 0;
Bob Peterson19aeb5a62014-09-29 08:52:04 -04001386 struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, };
David Teiglandb3b94fa2006-01-16 16:50:04 +00001387 unsigned int x;
1388 int error;
1389
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001390 gfs2_holder_mark_uninitialized(&r_gh);
Bob Petersonbc74aae2019-08-30 12:31:00 -05001391 gfs2_holder_mark_uninitialized(&rd_gh);
David Howells2b0143b2015-03-17 22:25:59 +00001392 if (d_really_is_positive(ndentry)) {
1393 nip = GFS2_I(d_inode(ndentry));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001394 if (ip == nip)
1395 return 0;
1396 }
1397
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001398 error = gfs2_rindex_update(sdp);
1399 if (error)
1400 return error;
1401
Bob Peterson2fba46a2020-02-27 12:47:53 -06001402 error = gfs2_qa_get(ndip);
Bob Peterson0a305e42012-06-06 11:17:59 +01001403 if (error)
1404 return error;
1405
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001406 if (odip != ndip) {
1407 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1408 0, &r_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001409 if (error)
1410 goto out;
1411
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001412 if (S_ISDIR(ip->i_inode.i_mode)) {
1413 dir_rename = 1;
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001414 /* don't move a directory into its subdir */
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001415 error = gfs2_ok_to_move(ip, ndip);
1416 if (error)
1417 goto out_gunlock_r;
1418 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001419 }
1420
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001421 num_gh = 1;
Bob Petersonad269672019-08-30 12:31:02 -05001422 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001423 if (odip != ndip) {
Bob Petersonad269672019-08-30 12:31:02 -05001424 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE,GL_ASYNC,
1425 ghs + num_gh);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001426 num_gh++;
1427 }
Bob Petersonad269672019-08-30 12:31:02 -05001428 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001429 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001430
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001431 if (nip) {
Bob Petersonad269672019-08-30 12:31:02 -05001432 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1433 ghs + num_gh);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001434 num_gh++;
1435 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001436
Bob Peterson72dbf472008-08-12 13:39:29 -05001437 for (x = 0; x < num_gh; x++) {
1438 error = gfs2_glock_nq(ghs + x);
1439 if (error)
1440 goto out_gunlock;
1441 }
Bob Petersonad269672019-08-30 12:31:02 -05001442 error = gfs2_glock_async_wait(num_gh, ghs);
1443 if (error)
1444 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001445
Bob Petersonbc74aae2019-08-30 12:31:00 -05001446 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 }
Bob Peterson4fc7ec32018-04-24 10:35:02 -07001456 error = gfs2_glock_nq_init(nrgd->rd_gl, LM_ST_EXCLUSIVE,
1457 LM_FLAG_NODE_SCOPE, &rd_gh);
Bob Petersonbc74aae2019-08-30 12:31:00 -05001458 if (error)
1459 goto out_gunlock;
1460 }
1461
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001462 error = -ENOENT;
1463 if (ip->i_inode.i_nlink == 0)
1464 goto out_gunlock;
1465
David Teiglandb3b94fa2006-01-16 16:50:04 +00001466 /* 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
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001479 if (nip->i_inode.i_nlink == 0) {
1480 error = -EAGAIN;
1481 goto out_gunlock;
1482 }
1483
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001484 if (S_ISDIR(nip->i_inode.i_mode)) {
Steven Whitehousead6203f22008-11-03 13:59:19 +00001485 if (nip->i_entries < 2) {
Steven Whitehouse94fb7632011-05-09 13:36:10 +01001486 gfs2_consist_inode(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001487 error = -EIO;
1488 goto out_gunlock;
1489 }
Steven Whitehousead6203f22008-11-03 13:59:19 +00001490 if (nip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001491 error = -ENOTEMPTY;
1492 goto out_gunlock;
1493 }
1494 }
1495 } else {
Christian Brauner549c7292021-01-21 14:19:43 +01001496 error = gfs2_permission(&init_user_ns, ndir,
1497 MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001498 if (error)
1499 goto out_gunlock;
1500
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001501 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001502 switch (error) {
1503 case -ENOENT:
1504 error = 0;
1505 break;
1506 case 0:
1507 error = -EEXIST;
1508 default:
1509 goto out_gunlock;
Aliasgar Surti098b9c12019-10-04 10:55:29 -05001510 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001511
1512 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -05001513 if (!ndip->i_inode.i_nlink) {
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001514 error = -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001515 goto out_gunlock;
1516 }
Steven Whitehousead6203f22008-11-03 13:59:19 +00001517 if (ndip->i_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001518 error = -EFBIG;
1519 goto out_gunlock;
1520 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001521 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -05001522 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001523 error = -EMLINK;
1524 goto out_gunlock;
1525 }
1526 }
1527 }
1528
1529 /* Check out the dir to be renamed */
1530
1531 if (dir_rename) {
Christian Brauner549c7292021-01-21 14:19:43 +01001532 error = gfs2_permission(&init_user_ns, d_inode(odentry),
1533 MAY_WRITE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001534 if (error)
1535 goto out_gunlock;
1536 }
1537
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001538 if (nip == NULL) {
1539 error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
1540 if (error)
1541 goto out_gunlock;
1542 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001543
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001544 if (da.nr_blocks) {
1545 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
Abhi Dasb8fbf472015-03-18 12:03:41 -05001546 error = gfs2_quota_lock_check(ndip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001547 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001548 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001549
Steven Whitehouse7b9cff42013-10-02 11:13:25 +01001550 error = gfs2_inplace_reserve(ndip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001551 if (error)
1552 goto out_gunlock_q;
1553
Steven Whitehouse534cf9c2014-01-06 12:03:05 +00001554 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
1555 4 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001556 if (error)
1557 goto out_ipreserv;
1558 } else {
1559 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -05001560 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001561 if (error)
1562 goto out_gunlock;
1563 }
1564
1565 /* Remove the target file, if it exists */
1566
Bob Peterson4327a9b2012-11-12 13:03:29 -05001567 if (nip)
1568 error = gfs2_unlink_inode(ndip, ndentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001569
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001570 error = update_moved_ino(ip, ndip, dir_rename);
1571 if (error)
1572 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001573
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001574 error = gfs2_dir_del(odip, odentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001575 if (error)
1576 goto out_end_trans;
1577
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001578 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001579 if (error)
1580 goto out_end_trans;
1581
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001582out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001583 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001584out_ipreserv:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001585 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001586 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001587out_gunlock_q:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001588 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001589 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001590out_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001591 gfs2_dir_no_add(&da);
Bob Petersonbc74aae2019-08-30 12:31:00 -05001592 if (gfs2_holder_initialized(&rd_gh))
1593 gfs2_glock_dq_uninit(&rd_gh);
1594
Bob Peterson72dbf472008-08-12 13:39:29 -05001595 while (x--) {
Bob Petersonad269672019-08-30 12:31:02 -05001596 if (gfs2_holder_queued(ghs + x))
1597 gfs2_glock_dq(ghs + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001598 gfs2_holder_uninit(ghs + x);
Bob Peterson72dbf472008-08-12 13:39:29 -05001599 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001600out_gunlock_r:
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001601 if (gfs2_holder_initialized(&r_gh))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001602 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001603out:
Bob Peterson2fba46a2020-02-27 12:47:53 -06001604 gfs2_qa_put(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001605 return error;
1606}
1607
1608/**
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001609 * 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
1619static 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);
Bob Petersonad269672019-08-30 12:31:02 -05001628 struct gfs2_holder ghs[4], r_gh;
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001629 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
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001635 gfs2_holder_mark_uninitialized(&r_gh);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001636 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;
Bob Petersonad269672019-08-30 12:31:02 -05001662 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001663 if (odip != ndip) {
Bob Petersonad269672019-08-30 12:31:02 -05001664 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1665 ghs + num_gh);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001666 num_gh++;
1667 }
Bob Petersonad269672019-08-30 12:31:02 -05001668 gfs2_holder_init(oip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001669 num_gh++;
1670
Bob Petersonad269672019-08-30 12:31:02 -05001671 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001672 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
Bob Petersonad269672019-08-30 12:31:02 -05001680 error = gfs2_glock_async_wait(num_gh, ghs);
1681 if (error)
1682 goto out_gunlock;
1683
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001684 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)) {
Christian Brauner549c7292021-01-21 14:19:43 +01001696 error = gfs2_permission(&init_user_ns, odentry->d_inode,
1697 MAY_WRITE);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001698 if (error)
1699 goto out_gunlock;
1700 }
1701 if (S_ISDIR(new_mode)) {
Christian Brauner549c7292021-01-21 14:19:43 +01001702 error = gfs2_permission(&init_user_ns, ndentry->d_inode,
1703 MAY_WRITE);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001704 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
1742out_end_trans:
1743 gfs2_trans_end(sdp);
1744out_gunlock:
1745 while (x--) {
Bob Petersonad269672019-08-30 12:31:02 -05001746 if (gfs2_holder_queued(ghs + x))
1747 gfs2_glock_dq(ghs + x);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001748 gfs2_holder_uninit(ghs + x);
1749 }
1750out_gunlock_r:
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001751 if (gfs2_holder_initialized(&r_gh))
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001752 gfs2_glock_dq_uninit(&r_gh);
1753out:
1754 return error;
1755}
1756
Christian Brauner549c7292021-01-21 14:19:43 +01001757static 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)
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001760{
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
1772/**
Al Viro6b255392015-11-17 10:20:54 -05001773 * gfs2_get_link - Follow a symbolic link
David Teiglandb3b94fa2006-01-16 16:50:04 +00001774 * @dentry: The dentry of the link
Al Viro6b255392015-11-17 10:20:54 -05001775 * @inode: The inode of the link
Al Virofceef392015-12-29 15:58:39 -05001776 * @done: destructor for return value
David Teiglandb3b94fa2006-01-16 16:50:04 +00001777 *
Al Viroc177c2a2010-01-14 00:59:16 -05001778 * This can handle symlinks of any size.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001779 *
1780 * Returns: 0 on success or error code
1781 */
1782
Al Viro6b255392015-11-17 10:20:54 -05001783static const char *gfs2_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001784 struct inode *inode,
1785 struct delayed_call *done)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001786{
Al Viro6b255392015-11-17 10:20:54 -05001787 struct gfs2_inode *ip = GFS2_I(inode);
Al Viroc177c2a2010-01-14 00:59:16 -05001788 struct gfs2_holder i_gh;
1789 struct buffer_head *dibh;
Steven Whitehouse160b4022011-05-13 10:34:59 +01001790 unsigned int size;
Al Viroc177c2a2010-01-14 00:59:16 -05001791 char *buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001792 int error;
1793
Al Viro6b255392015-11-17 10:20:54 -05001794 if (!dentry)
1795 return ERR_PTR(-ECHILD);
1796
Al Viroc177c2a2010-01-14 00:59:16 -05001797 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);
Al Viro680baac2015-05-02 13:32:22 -04001801 return ERR_PTR(error);
Al Viroc177c2a2010-01-14 00:59:16 -05001802 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001803
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001804 size = (unsigned int)i_size_read(&ip->i_inode);
1805 if (size == 0) {
Al Viroc177c2a2010-01-14 00:59:16 -05001806 gfs2_consist_inode(ip);
1807 buf = ERR_PTR(-EIO);
1808 goto out;
1809 }
1810
1811 error = gfs2_meta_inode_buffer(ip, &dibh);
1812 if (error) {
1813 buf = ERR_PTR(error);
1814 goto out;
1815 }
1816
Steven Whitehouse160b4022011-05-13 10:34:59 +01001817 buf = kzalloc(size + 1, GFP_NOFS);
Al Viroc177c2a2010-01-14 00:59:16 -05001818 if (!buf)
1819 buf = ERR_PTR(-ENOMEM);
1820 else
Steven Whitehouse160b4022011-05-13 10:34:59 +01001821 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
Al Viroc177c2a2010-01-14 00:59:16 -05001822 brelse(dibh);
1823out:
1824 gfs2_glock_dq_uninit(&i_gh);
Al Viro680baac2015-05-02 13:32:22 -04001825 if (!IS_ERR(buf))
Al Virofceef392015-12-29 15:58:39 -05001826 set_delayed_call(done, kfree_link, buf);
Al Viro680baac2015-05-02 13:32:22 -04001827 return buf;
Al Viroc177c2a2010-01-14 00:59:16 -05001828}
1829
David Teiglandb3b94fa2006-01-16 16:50:04 +00001830/**
1831 * gfs2_permission -
Steven Whitehouse75d5cfb2011-01-19 09:42:40 +00001832 * @inode: The inode
1833 * @mask: The mask to be tested
1834 * @flags: Indicates whether this is an RCU path walk or not
David Teiglandb3b94fa2006-01-16 16:50:04 +00001835 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001836 * 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 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001840 * Returns: errno
1841 */
1842
Christian Brauner549c7292021-01-21 14:19:43 +01001843int gfs2_permission(struct user_namespace *mnt_userns, struct inode *inode,
1844 int mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001845{
Nick Pigginb74c79e2011-01-07 17:49:58 +11001846 struct gfs2_inode *ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001847 struct gfs2_holder i_gh;
1848 int error;
1849
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001850 gfs2_holder_mark_uninitialized(&i_gh);
Nick Pigginb74c79e2011-01-07 17:49:58 +11001851 ip = GFS2_I(inode);
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001852 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001853 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;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001858 }
1859
Miklos Szeredif58ba882008-07-02 21:12:01 +02001860 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
Eryu Guan337684a2016-08-02 19:58:28 +08001861 error = -EPERM;
Miklos Szeredif58ba882008-07-02 21:12:01 +02001862 else
Christian Brauner47291ba2021-01-21 14:19:24 +01001863 error = generic_permission(&init_user_ns, inode, mask);
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001864 if (gfs2_holder_initialized(&i_gh))
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001865 gfs2_glock_dq_uninit(&i_gh);
1866
David Teiglandb3b94fa2006-01-16 16:50:04 +00001867 return error;
1868}
1869
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001870static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
Steven Whitehouse194c0112011-05-09 14:06:38 +01001871{
Christian Brauner2f221d62021-01-21 14:19:26 +01001872 setattr_copy(&init_user_ns, inode, attr);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001873 mark_inode_dirty(inode);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001874 return 0;
1875}
1876
1877/**
1878 * gfs2_setattr_simple -
1879 * @ip:
1880 * @attr:
1881 *
1882 * Returns: errno
1883 */
1884
Andreas Gruenbachera4122a92021-04-07 00:59:03 +02001885static int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
Steven Whitehouse194c0112011-05-09 14:06:38 +01001886{
1887 int error;
1888
1889 if (current->journal_info)
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001890 return __gfs2_setattr_simple(inode, attr);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001891
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001892 error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001893 if (error)
1894 return error;
1895
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001896 error = __gfs2_setattr_simple(inode, attr);
1897 gfs2_trans_end(GFS2_SB(inode));
Steven Whitehouse194c0112011-05-09 14:06:38 +01001898 return error;
1899}
1900
David Teiglandb3b94fa2006-01-16 16:50:04 +00001901static int setattr_chown(struct inode *inode, struct iattr *attr)
1902{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001903 struct gfs2_inode *ip = GFS2_I(inode);
1904 struct gfs2_sbd *sdp = GFS2_SB(inode);
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -08001905 kuid_t ouid, nuid;
1906 kgid_t ogid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001907 int error;
Abhi Dasb8fbf472015-03-18 12:03:41 -05001908 struct gfs2_alloc_parms ap;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001909
Steven Whitehouse2933f922006-11-01 13:23:29 -05001910 ouid = inode->i_uid;
1911 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001912 nuid = attr->ia_uid;
1913 ngid = attr->ia_gid;
1914
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001915 if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001916 ouid = nuid = NO_UID_QUOTA_CHANGE;
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001917 if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001918 ogid = ngid = NO_GID_QUOTA_CHANGE;
Bob Peterson2fba46a2020-02-27 12:47:53 -06001919 error = gfs2_qa_get(ip);
Bob Peterson62e96cf2014-01-06 17:16:01 -05001920 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -06001921 return error;
Bob Peterson62e96cf2014-01-06 17:16:01 -05001922
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
Abhi Dasb8fbf472015-03-18 12:03:41 -05001931 ap.target = gfs2_get_inode_blocks(&ip->i_inode);
1932
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001933 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1934 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
Abhi Dasb8fbf472015-03-18 12:03:41 -05001935 error = gfs2_quota_check(ip, nuid, ngid, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001936 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
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001944 error = gfs2_setattr_simple(inode, attr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001945 if (error)
1946 goto out_end_trans;
1947
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001948 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1949 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
Abhi Das39a72582015-06-02 11:02:24 -05001950 gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid);
Abhi Dasb8fbf472015-03-18 12:03:41 -05001951 gfs2_quota_change(ip, ap.target, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001952 }
1953
Steven Whitehousea91ea692006-09-04 12:04:26 -04001954out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001955 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001956out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001957 gfs2_quota_unlock(ip);
Bob Peterson62e96cf2014-01-06 17:16:01 -05001958out:
Bob Peterson2fba46a2020-02-27 12:47:53 -06001959 gfs2_qa_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001960 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
Christian Brauner549c7292021-01-21 14:19:43 +01001974static int gfs2_setattr(struct user_namespace *mnt_userns,
1975 struct dentry *dentry, struct iattr *attr)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001976{
David Howells2b0143b2015-03-17 22:25:59 +00001977 struct inode *inode = d_inode(dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001978 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001979 struct gfs2_holder i_gh;
1980 int error;
1981
Bob Peterson2fba46a2020-02-27 12:47:53 -06001982 error = gfs2_qa_get(ip);
Bob Peterson0a305e42012-06-06 11:17:59 +01001983 if (error)
1984 return error;
1985
David Teiglandb3b94fa2006-01-16 16:50:04 +00001986 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1987 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -06001988 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001989
1990 error = -EPERM;
1991 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Bob Peterson2fba46a2020-02-27 12:47:53 -06001992 goto error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001993
Christian Brauner2f221d62021-01-21 14:19:26 +01001994 error = setattr_prepare(&init_user_ns, dentry, attr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001995 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -06001996 goto error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001997
1998 if (attr->ia_valid & ATTR_SIZE)
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001999 error = gfs2_setattr_size(inode, attr->ia_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002000 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
2001 error = setattr_chown(inode, attr);
Christoph Hellwige01580b2013-12-20 05:16:52 -08002002 else {
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01002003 error = gfs2_setattr_simple(inode, attr);
Christoph Hellwige01580b2013-12-20 05:16:52 -08002004 if (!error && attr->ia_valid & ATTR_MODE)
Christian Braunere65ce2a2021-01-21 14:19:27 +01002005 error = posix_acl_chmod(&init_user_ns, inode,
2006 inode->i_mode);
Christoph Hellwige01580b2013-12-20 05:16:52 -08002007 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00002008
Bob Peterson2fba46a2020-02-27 12:47:53 -06002009error:
David Teiglandb3b94fa2006-01-16 16:50:04 +00002010 if (!error)
2011 mark_inode_dirty(inode);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01002012 gfs2_glock_dq_uninit(&i_gh);
Bob Peterson2fba46a2020-02-27 12:47:53 -06002013out:
2014 gfs2_qa_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002015 return error;
2016}
2017
2018/**
2019 * gfs2_getattr - Read out an inode's attributes
Christian Brauner549c7292021-01-21 14:19:43 +01002020 * @mnt_userns: user namespace of the mount the inode was found from
David Howellsa528d352017-01-31 16:46:22 +00002021 * @path: Object to query
David Teiglandb3b94fa2006-01-16 16:50:04 +00002022 * @stat: The inode's stats
David Howellsa528d352017-01-31 16:46:22 +00002023 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
2024 * @flags: AT_STATX_xxx setting
David Teiglandb3b94fa2006-01-16 16:50:04 +00002025 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05002026 * 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 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00002032 * Returns: errno
2033 */
2034
Christian Brauner549c7292021-01-21 14:19:43 +01002035static int gfs2_getattr(struct user_namespace *mnt_userns,
2036 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +00002037 u32 request_mask, unsigned int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002038{
David Howellsa528d352017-01-31 16:46:22 +00002039 struct inode *inode = d_inode(path->dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04002040 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002041 struct gfs2_holder gh;
Andreas Gruenbacherb2623c22017-10-09 17:55:58 +02002042 u32 gfsflags;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002043 int error;
2044
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05002045 gfs2_holder_mark_uninitialized(&gh);
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00002046 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06002047 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
2048 if (error)
2049 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002050 }
2051
Andreas Gruenbacherb2623c22017-10-09 17:55:58 +02002052 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
Christian Brauner0d56a452021-01-21 14:19:30 +01002064 generic_fillattr(&init_user_ns, inode, stat);
Andreas Gruenbacherb2623c22017-10-09 17:55:58 +02002065
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05002066 if (gfs2_holder_initialized(&gh))
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05002067 gfs2_glock_dq_uninit(&gh);
2068
2069 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002070}
2071
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002072static 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
Bob Petersonaac1a552017-02-16 21:13:54 +01002079 inode_lock_shared(inode);
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002080
2081 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2082 if (ret)
2083 goto out;
2084
Bob Petersonaac1a552017-02-16 21:13:54 +01002085 ret = iomap_fiemap(inode, fieinfo, start, len, &gfs2_iomap_ops);
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002086
2087 gfs2_glock_dq_uninit(&gh);
Bob Petersonaac1a552017-02-16 21:13:54 +01002088
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002089out:
Bob Petersonaac1a552017-02-16 21:13:54 +01002090 inode_unlock_shared(inode);
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002091 return ret;
2092}
2093
Andreas Gruenbacher3a274112017-03-15 19:12:59 +01002094loff_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
2113loff_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
Andreas Gruenbacher82e938b2020-11-25 23:37:18 +01002132static 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
Andreas Gruenbachere3a77ee2020-11-25 21:14:15 +01002151static const struct inode_operations gfs2_file_iops = {
Al Viroe6305c42008-07-15 21:03:57 -04002152 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002153 .setattr = gfs2_setattr,
2154 .getattr = gfs2_getattr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002155 .listxattr = gfs2_listxattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002156 .fiemap = gfs2_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02002157 .get_acl = gfs2_get_acl,
Christoph Hellwige01580b2013-12-20 05:16:52 -08002158 .set_acl = gfs2_set_acl,
Andreas Gruenbacher82e938b2020-11-25 23:37:18 +01002159 .update_time = gfs2_update_time,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002160};
2161
Andreas Gruenbachere3a77ee2020-11-25 21:14:15 +01002162static const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00002163 .create = gfs2_create,
2164 .lookup = gfs2_lookup,
2165 .link = gfs2_link,
2166 .unlink = gfs2_unlink,
2167 .symlink = gfs2_symlink,
2168 .mkdir = gfs2_mkdir,
Steven Whitehouse855d23c2011-05-09 16:42:37 +01002169 .rmdir = gfs2_unlink,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002170 .mknod = gfs2_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02002171 .rename = gfs2_rename2,
Al Viroe6305c42008-07-15 21:03:57 -04002172 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002173 .setattr = gfs2_setattr,
2174 .getattr = gfs2_getattr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002175 .listxattr = gfs2_listxattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002176 .fiemap = gfs2_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02002177 .get_acl = gfs2_get_acl,
Christoph Hellwige01580b2013-12-20 05:16:52 -08002178 .set_acl = gfs2_set_acl,
Andreas Gruenbacher82e938b2020-11-25 23:37:18 +01002179 .update_time = gfs2_update_time,
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01002180 .atomic_open = gfs2_atomic_open,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002181};
2182
Andreas Gruenbachere3a77ee2020-11-25 21:14:15 +01002183static const struct inode_operations gfs2_symlink_iops = {
Al Viro6b255392015-11-17 10:20:54 -05002184 .get_link = gfs2_get_link,
Al Viroe6305c42008-07-15 21:03:57 -04002185 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002186 .setattr = gfs2_setattr,
2187 .getattr = gfs2_getattr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002188 .listxattr = gfs2_listxattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002189 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002190};
2191