blob: 89905f4f29bb6de91e181373c4f168431f3e8e74 [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
Steven Whitehouse194c0112011-05-09 14:06:38 +010043/**
44 * gfs2_set_iop - Sets inode operations
45 * @inode: The inode with correct i_mode filled in
46 *
47 * GFS2 lookup code fills in vfs inode contents based on info obtained
48 * from directory entry inside gfs2_inode_lookup().
49 */
50
51static void gfs2_set_iop(struct inode *inode)
52{
53 struct gfs2_sbd *sdp = GFS2_SB(inode);
54 umode_t mode = inode->i_mode;
55
56 if (S_ISREG(mode)) {
57 inode->i_op = &gfs2_file_iops;
58 if (gfs2_localflocks(sdp))
59 inode->i_fop = &gfs2_file_fops_nolock;
60 else
61 inode->i_fop = &gfs2_file_fops;
62 } else if (S_ISDIR(mode)) {
63 inode->i_op = &gfs2_dir_iops;
64 if (gfs2_localflocks(sdp))
65 inode->i_fop = &gfs2_dir_fops_nolock;
66 else
67 inode->i_fop = &gfs2_dir_fops;
68 } else if (S_ISLNK(mode)) {
69 inode->i_op = &gfs2_symlink_iops;
70 } else {
71 inode->i_op = &gfs2_file_iops;
72 init_special_inode(inode, inode->i_mode, inode->i_rdev);
73 }
74}
75
Andreas Gruenbacher5f6e13b2021-11-29 10:50:41 +010076static int iget_test(struct inode *inode, void *opaque)
77{
78 u64 no_addr = *(u64 *)opaque;
79
80 return GFS2_I(inode)->i_no_addr == no_addr;
81}
82
83static int iget_set(struct inode *inode, void *opaque)
84{
85 u64 no_addr = *(u64 *)opaque;
86
87 GFS2_I(inode)->i_no_addr = no_addr;
88 inode->i_ino = no_addr;
89 return 0;
90}
91
Steven Whitehouse194c0112011-05-09 14:06:38 +010092/**
93 * gfs2_inode_lookup - Lookup an inode
94 * @sb: The super block
Steven Whitehouse194c0112011-05-09 14:06:38 +010095 * @type: The type of the inode
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -050096 * @no_addr: The inode number
97 * @no_formal_ino: The inode generation number
98 * @blktype: Requested block type (GFS2_BLKST_DINODE or GFS2_BLKST_UNLINKED;
Andreas Gruenbacher61b91cf2017-08-01 09:54:33 -050099 * GFS2_BLKST_FREE to indicate not to verify)
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500100 *
101 * If @type is DT_UNKNOWN, the inode type is fetched from disk.
102 *
103 * If @blktype is anything other than GFS2_BLKST_FREE (which is used as a
104 * placeholder because it doesn't otherwise make sense), the on-disk block type
105 * is verified to be @blktype.
Steven Whitehouse194c0112011-05-09 14:06:38 +0100106 *
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100107 * When @no_formal_ino is non-zero, this function will return ERR_PTR(-ESTALE)
108 * if it detects that @no_formal_ino doesn't match the actual inode generation
109 * number. However, it doesn't always know unless @type is DT_UNKNOWN.
110 *
Steven Whitehouse194c0112011-05-09 14:06:38 +0100111 * Returns: A VFS inode, or an error
112 */
113
114struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500115 u64 no_addr, u64 no_formal_ino,
116 unsigned int blktype)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100117{
118 struct inode *inode;
119 struct gfs2_inode *ip;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500120 struct gfs2_holder i_gh;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100121 int error;
122
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500123 gfs2_holder_mark_uninitialized(&i_gh);
Andreas Gruenbacher5f6e13b2021-11-29 10:50:41 +0100124 inode = iget5_locked(sb, no_addr, iget_test, iget_set, &no_addr);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100125 if (!inode)
Steven Whitehouseac3beb62014-01-16 10:31:13 +0000126 return ERR_PTR(-ENOMEM);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100127
Bob Petersone97321f2016-04-12 16:14:26 -0400128 ip = GFS2_I(inode);
Bob Petersone97321f2016-04-12 16:14:26 -0400129
Steven Whitehouse194c0112011-05-09 14:06:38 +0100130 if (inode->i_state & I_NEW) {
131 struct gfs2_sbd *sdp = GFS2_SB(inode);
Andreas Gruenbacher5f6e13b2021-11-29 10:50:41 +0100132 struct gfs2_glock *io_gl;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100133
134 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
135 if (unlikely(error))
136 goto fail;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100137
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500138 if (type == DT_UNKNOWN || blktype != GFS2_BLKST_FREE) {
139 /*
140 * The GL_SKIP flag indicates to skip reading the inode
Andreas Gruenbacher5f6e13b2021-11-29 10:50:41 +0100141 * block. We read the inode when instantiating it
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500142 * after possibly checking the block type.
143 */
144 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE,
145 GL_SKIP, &i_gh);
146 if (error)
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100147 goto fail;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500148
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100149 error = -ESTALE;
150 if (no_formal_ino &&
151 gfs2_inode_already_deleted(ip->i_gl, no_formal_ino))
152 goto fail;
153
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500154 if (blktype != GFS2_BLKST_FREE) {
155 error = gfs2_check_blk_type(sdp, no_addr,
156 blktype);
157 if (error)
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100158 goto fail;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500159 }
160 }
161
Bob Petersonf2e70d82021-10-06 09:29:18 -0500162 set_bit(GLF_INSTANTIATE_NEEDED, &ip->i_gl->gl_flags);
Andreas Gruenbacher5f6e13b2021-11-29 10:50:41 +0100163
164 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100165 if (unlikely(error))
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100166 goto fail;
Andreas Gruenbacher5f6e13b2021-11-29 10:50:41 +0100167 if (blktype != GFS2_BLKST_UNLINKED)
168 gfs2_cancel_delete_work(io_gl);
169 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100170 gfs2_glock_put(io_gl);
Andreas Gruenbacher5f6e13b2021-11-29 10:50:41 +0100171 if (unlikely(error))
172 goto fail;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100173
Andreas Gruenbacher2b0fb352020-01-15 06:26:00 +0100174 /* Lowest possible timestamp; will be overwritten in gfs2_dinode_in. */
175 inode->i_atime.tv_sec = 1LL << (8 * sizeof(inode->i_atime.tv_sec) - 1);
176 inode->i_atime.tv_nsec = 0;
177
Andreas Gruenbacher5f6e13b2021-11-29 10:50:41 +0100178 glock_set_object(ip->i_gl, ip);
179
Steven Whitehouse194c0112011-05-09 14:06:38 +0100180 if (type == DT_UNKNOWN) {
181 /* Inode glock must be locked already */
Bob Petersonf2e70d82021-10-06 09:29:18 -0500182 error = gfs2_instantiate(&i_gh);
Andreas Gruenbacher5f6e13b2021-11-29 10:50:41 +0100183 if (error) {
184 glock_clear_object(ip->i_gl, ip);
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100185 goto fail;
Andreas Gruenbacher5f6e13b2021-11-29 10:50:41 +0100186 }
Steven Whitehouse194c0112011-05-09 14:06:38 +0100187 } else {
Andreas Gruenbacher2b0fb352020-01-15 06:26:00 +0100188 ip->i_no_formal_ino = no_formal_ino;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100189 inode->i_mode = DT2IF(type);
190 }
191
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100192 if (gfs2_holder_initialized(&i_gh))
193 gfs2_glock_dq_uninit(&i_gh);
Andreas Gruenbacher5f6e13b2021-11-29 10:50:41 +0100194 glock_set_object(ip->i_iopen_gh.gh_gl, ip);
Andreas Gruenbacher332f51d2016-09-26 13:24:34 -0500195
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100196 gfs2_set_iop(inode);
Andreas Gruenbacherb8e12e32021-11-29 03:35:00 +0100197 unlock_new_inode(inode);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100198 }
199
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100200 if (no_formal_ino && ip->i_no_formal_ino &&
201 no_formal_ino != ip->i_no_formal_ino) {
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100202 iput(inode);
Andreas Gruenbacherb8e12e32021-11-29 03:35:00 +0100203 return ERR_PTR(-ESTALE);
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100204 }
205
Steven Whitehouse194c0112011-05-09 14:06:38 +0100206 return inode;
207
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100208fail:
Andreas Gruenbacher5f6e13b2021-11-29 10:50:41 +0100209 if (gfs2_holder_initialized(&ip->i_iopen_gh))
Bob Peterson763766c2021-09-29 14:42:38 -0500210 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500211 if (gfs2_holder_initialized(&i_gh))
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500212 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100213 iget_failed(inode);
214 return ERR_PTR(error);
215}
216
Andreas Gruenbacher6bdcade2020-01-15 05:31:38 +0100217/**
218 * gfs2_lookup_by_inum - look up an inode by inode number
219 * @sdp: The super block
220 * @no_addr: The inode number
221 * @no_formal_ino: The inode generation number (0 for any)
222 * @blktype: Requested block type (see gfs2_inode_lookup)
223 */
Steven Whitehouse194c0112011-05-09 14:06:38 +0100224struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
Andreas Gruenbacher6bdcade2020-01-15 05:31:38 +0100225 u64 no_formal_ino, unsigned int blktype)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100226{
227 struct super_block *sb = sdp->sd_vfs;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500228 struct inode *inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100229 int error;
230
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100231 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, no_formal_ino,
232 blktype);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100233 if (IS_ERR(inode))
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500234 return inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100235
Steven Whitehouse194c0112011-05-09 14:06:38 +0100236 if (no_formal_ino) {
Steven Whitehouse194c0112011-05-09 14:06:38 +0100237 error = -EIO;
238 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
239 goto fail_iput;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100240 }
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500241 return inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100242
Steven Whitehouse194c0112011-05-09 14:06:38 +0100243fail_iput:
244 iput(inode);
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500245 return ERR_PTR(error);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100246}
247
248
249struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
250{
251 struct qstr qstr;
252 struct inode *inode;
253 gfs2_str2qstr(&qstr, name);
254 inode = gfs2_lookupi(dip, &qstr, 1);
255 /* gfs2_lookupi has inconsistent callers: vfs
256 * related routines expect NULL for no entry found,
257 * gfs2_lookup_simple callers expect ENOENT
258 * and do not check for NULL.
259 */
260 if (inode == NULL)
261 return ERR_PTR(-ENOENT);
262 else
263 return inode;
264}
265
266
267/**
268 * gfs2_lookupi - Look up a filename in a directory and return its inode
Lee Jonesc551f662021-03-30 17:44:29 +0100269 * @dir: The inode of the directory containing the inode to look-up
Steven Whitehouse194c0112011-05-09 14:06:38 +0100270 * @name: The name of the inode to look for
271 * @is_root: If 1, ignore the caller's permissions
Steven Whitehouse194c0112011-05-09 14:06:38 +0100272 *
273 * This can be called via the VFS filldir function when NFS is doing
274 * a readdirplus and the inode which its intending to stat isn't
275 * already in cache. In this case we must not take the directory glock
276 * again, since the readdir call will have already taken that lock.
277 *
278 * Returns: errno
279 */
280
281struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
282 int is_root)
283{
284 struct super_block *sb = dir->i_sb;
285 struct gfs2_inode *dip = GFS2_I(dir);
286 struct gfs2_holder d_gh;
287 int error = 0;
288 struct inode *inode = NULL;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100289
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500290 gfs2_holder_mark_uninitialized(&d_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100291 if (!name->len || name->len > GFS2_FNAMESIZE)
292 return ERR_PTR(-ENAMETOOLONG);
293
294 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
295 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
David Howells2b0143b2015-03-17 22:25:59 +0000296 dir == d_inode(sb->s_root))) {
Steven Whitehouse194c0112011-05-09 14:06:38 +0100297 igrab(dir);
298 return dir;
299 }
300
301 if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
302 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
303 if (error)
304 return ERR_PTR(error);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100305 }
306
307 if (!is_root) {
Christian Brauner549c7292021-01-21 14:19:43 +0100308 error = gfs2_permission(&init_user_ns, dir, MAY_EXEC);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100309 if (error)
310 goto out;
311 }
312
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100313 inode = gfs2_dir_search(dir, name, false);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100314 if (IS_ERR(inode))
315 error = PTR_ERR(inode);
316out:
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500317 if (gfs2_holder_initialized(&d_gh))
Steven Whitehouse194c0112011-05-09 14:06:38 +0100318 gfs2_glock_dq_uninit(&d_gh);
319 if (error == -ENOENT)
320 return NULL;
321 return inode ? inode : ERR_PTR(error);
322}
323
324/**
325 * create_ok - OK to create a new on-disk inode here?
326 * @dip: Directory in which dinode is to be created
327 * @name: Name of new dinode
328 * @mode:
329 *
330 * Returns: errno
331 */
332
333static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
Al Viro175a4eb2011-07-26 03:30:54 -0400334 umode_t mode)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100335{
336 int error;
337
Christian Brauner549c7292021-01-21 14:19:43 +0100338 error = gfs2_permission(&init_user_ns, &dip->i_inode,
339 MAY_WRITE | MAY_EXEC);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100340 if (error)
341 return error;
342
343 /* Don't create entries in an unlinked directory */
344 if (!dip->i_inode.i_nlink)
345 return -ENOENT;
346
Steven Whitehouse194c0112011-05-09 14:06:38 +0100347 if (dip->i_entries == (u32)-1)
348 return -EFBIG;
349 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
350 return -EMLINK;
351
352 return 0;
353}
354
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000355static void munge_mode_uid_gid(const struct gfs2_inode *dip,
356 struct inode *inode)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100357{
358 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800359 (dip->i_inode.i_mode & S_ISUID) &&
360 !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000361 if (S_ISDIR(inode->i_mode))
362 inode->i_mode |= S_ISUID;
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800363 else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000364 inode->i_mode &= ~07111;
365 inode->i_uid = dip->i_inode.i_uid;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100366 } else
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000367 inode->i_uid = current_fsuid();
Steven Whitehouse194c0112011-05-09 14:06:38 +0100368
369 if (dip->i_inode.i_mode & S_ISGID) {
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000370 if (S_ISDIR(inode->i_mode))
371 inode->i_mode |= S_ISGID;
372 inode->i_gid = dip->i_inode.i_gid;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100373 } else
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000374 inode->i_gid = current_fsgid();
Steven Whitehouse194c0112011-05-09 14:06:38 +0100375}
376
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000377static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100378{
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000379 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000380 struct gfs2_alloc_parms ap = { .target = *dblocks, .aflags = flags, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100381 int error;
382
Abhi Dasb8fbf472015-03-18 12:03:41 -0500383 error = gfs2_quota_lock_check(ip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100384 if (error)
385 goto out;
386
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100387 error = gfs2_inplace_reserve(ip, &ap);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000388 if (error)
389 goto out_quota;
390
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000391 error = gfs2_trans_begin(sdp, (*dblocks * RES_RG_BIT) + RES_STATFS + RES_QUOTA, 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100392 if (error)
393 goto out_ipreserv;
394
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000395 error = gfs2_alloc_blocks(ip, &ip->i_no_addr, dblocks, 1, &ip->i_generation);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000396 ip->i_no_formal_ino = ip->i_generation;
397 ip->i_inode.i_ino = ip->i_no_addr;
398 ip->i_goal = ip->i_no_addr;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100399
400 gfs2_trans_end(sdp);
401
402out_ipreserv:
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000403 gfs2_inplace_release(ip);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000404out_quota:
405 gfs2_quota_unlock(ip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100406out:
Steven Whitehouse194c0112011-05-09 14:06:38 +0100407 return error;
408}
409
Steven Whitehousef2741d92011-05-13 12:11:17 +0100410static void gfs2_init_dir(struct buffer_head *dibh,
411 const struct gfs2_inode *parent)
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100412{
413 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
414 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
415
416 gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
417 dent->de_inum = di->di_num; /* already GFS2 endian */
418 dent->de_type = cpu_to_be16(DT_DIR);
419
420 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
421 gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
422 gfs2_inum_out(parent, dent);
423 dent->de_type = cpu_to_be16(DT_DIR);
424
425}
426
Steven Whitehouse194c0112011-05-09 14:06:38 +0100427/**
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000428 * gfs2_init_xattr - Initialise an xattr block for a new inode
429 * @ip: The inode in question
430 *
431 * This sets up an empty xattr block for a new inode, ready to
432 * take any ACLs, LSM xattrs, etc.
433 */
434
435static void gfs2_init_xattr(struct gfs2_inode *ip)
436{
437 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
438 struct buffer_head *bh;
439 struct gfs2_ea_header *ea;
440
441 bh = gfs2_meta_new(ip->i_gl, ip->i_eattr);
442 gfs2_trans_add_meta(ip->i_gl, bh);
443 gfs2_metatype_set(bh, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
444 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
445
446 ea = GFS2_EA_BH2FIRST(bh);
447 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
448 ea->ea_type = GFS2_EATYPE_UNUSED;
449 ea->ea_flags = GFS2_EAFLAG_LAST;
450
451 brelse(bh);
452}
453
454/**
Steven Whitehouse194c0112011-05-09 14:06:38 +0100455 * init_dinode - Fill in a new dinode structure
Steven Whitehousef2741d92011-05-13 12:11:17 +0100456 * @dip: The directory this inode is being created in
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000457 * @ip: The inode
Steven Whitehousef2741d92011-05-13 12:11:17 +0100458 * @symname: The symlink destination (if a symlink)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100459 *
460 */
461
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000462static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000463 const char *symname)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100464{
Steven Whitehouse194c0112011-05-09 14:06:38 +0100465 struct gfs2_dinode *di;
466 struct buffer_head *dibh;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100467
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000468 dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000469 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100470 di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000471 gfs2_dinode_out(ip, di);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100472
Al Viro6f247842021-01-31 19:23:55 -0500473 di->di_major = cpu_to_be32(imajor(&ip->i_inode));
474 di->di_minor = cpu_to_be32(iminor(&ip->i_inode));
Steven Whitehouse194c0112011-05-09 14:06:38 +0100475 di->__pad1 = 0;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100476 di->__pad2 = 0;
477 di->__pad3 = 0;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100478 memset(&di->__pad4, 0, sizeof(di->__pad4));
Steven Whitehouse194c0112011-05-09 14:06:38 +0100479 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000480 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouse160b4022011-05-13 10:34:59 +0100481
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000482 switch(ip->i_inode.i_mode & S_IFMT) {
Steven Whitehouse160b4022011-05-13 10:34:59 +0100483 case S_IFDIR:
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100484 gfs2_init_dir(dibh, dip);
Steven Whitehouse160b4022011-05-13 10:34:59 +0100485 break;
486 case S_IFLNK:
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000487 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
Steven Whitehouse160b4022011-05-13 10:34:59 +0100488 break;
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100489 }
490
Steven Whitehouse194c0112011-05-09 14:06:38 +0100491 set_buffer_uptodate(dibh);
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000492 brelse(dibh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100493}
494
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000495/**
Lee Jonesc551f662021-03-30 17:44:29 +0100496 * gfs2_trans_da_blks - Calculate number of blocks to link inode
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000497 * @dip: The directory we are linking into
498 * @da: The dir add information
499 * @nr_inodes: The number of inodes involved
500 *
501 * This calculate the number of blocks we need to reserve in a
502 * transaction to link @nr_inodes into a directory. In most cases
503 * @nr_inodes will be 2 (the directory plus the inode being linked in)
504 * but in case of rename, 4 may be required.
505 *
506 * Returns: Number of blocks
507 */
508
509static unsigned gfs2_trans_da_blks(const struct gfs2_inode *dip,
510 const struct gfs2_diradd *da,
511 unsigned nr_inodes)
512{
513 return da->nr_blocks + gfs2_rg_blocks(dip, da->nr_blocks) +
514 (nr_inodes * RES_DINODE) + RES_QUOTA + RES_STATFS;
515}
516
Steven Whitehouse194c0112011-05-09 14:06:38 +0100517static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000518 struct gfs2_inode *ip, struct gfs2_diradd *da)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100519{
520 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000521 struct gfs2_alloc_parms ap = { .target = da->nr_blocks, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100522 int error;
523
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000524 if (da->nr_blocks) {
Abhi Dasb8fbf472015-03-18 12:03:41 -0500525 error = gfs2_quota_lock_check(dip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100526 if (error)
527 goto fail_quota_locks;
528
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100529 error = gfs2_inplace_reserve(dip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100530 if (error)
531 goto fail_quota_locks;
532
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000533 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100534 if (error)
535 goto fail_ipreserv;
536 } else {
537 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
538 if (error)
539 goto fail_quota_locks;
540 }
541
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000542 error = gfs2_dir_add(&dip->i_inode, name, ip, da);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100543
Steven Whitehouse194c0112011-05-09 14:06:38 +0100544 gfs2_trans_end(sdp);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100545fail_ipreserv:
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000546 gfs2_inplace_release(dip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100547fail_quota_locks:
548 gfs2_quota_unlock(dip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100549 return error;
550}
551
H Hartley Sweeten46cc1e52011-09-23 15:51:32 -0700552static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -0400553 void *fs_info)
554{
555 const struct xattr *xattr;
556 int err = 0;
557
558 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
559 err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
560 xattr->value_len, 0,
561 GFS2_EATYPE_SECURITY);
562 if (err < 0)
563 break;
564 }
565 return err;
566}
567
Steven Whitehouse194c0112011-05-09 14:06:38 +0100568/**
Steven Whitehousef2741d92011-05-13 12:11:17 +0100569 * gfs2_create_inode - Create a new inode
570 * @dir: The parent directory
571 * @dentry: The new dentry
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100572 * @file: If non-NULL, the file which is being opened
Steven Whitehousef2741d92011-05-13 12:11:17 +0100573 * @mode: The permissions on the new inode
574 * @dev: For device nodes, this is the device number
575 * @symname: For symlinks, this is the link destination
576 * @size: The initial size of the inode (ignored for directories)
Lee Jonesc551f662021-03-30 17:44:29 +0100577 * @excl: Force fail if inode exists
Steven Whitehouse194c0112011-05-09 14:06:38 +0100578 *
Steven Whitehousef2741d92011-05-13 12:11:17 +0100579 * Returns: 0 on success, or error code
Steven Whitehouse194c0112011-05-09 14:06:38 +0100580 */
581
Steven Whitehousef2741d92011-05-13 12:11:17 +0100582static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100583 struct file *file,
Al Viro175a4eb2011-07-26 03:30:54 -0400584 umode_t mode, dev_t dev, const char *symname,
Al Virob452a452018-06-08 13:06:28 -0400585 unsigned int size, int excl)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100586{
Steven Whitehousef2741d92011-05-13 12:11:17 +0100587 const struct qstr *name = &dentry->d_name;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800588 struct posix_acl *default_acl, *acl;
Steven Whitehousef2741d92011-05-13 12:11:17 +0100589 struct gfs2_holder ghs[2];
Steven Whitehouse194c0112011-05-09 14:06:38 +0100590 struct inode *inode = NULL;
Bob Peterson8e2e0042012-07-19 08:12:40 -0400591 struct gfs2_inode *dip = GFS2_I(dir), *ip;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100592 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Andreas Gruenbachera55a47a2020-11-27 14:23:04 +0100593 struct gfs2_glock *io_gl;
Bob Peterson783013c2015-12-04 10:19:14 -0600594 int error, free_vfs_inode = 1;
Steven Whitehouse9dbe9612012-10-31 10:37:10 +0000595 u32 aflags = 0;
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000596 unsigned blocks = 1;
Bob Peterson19aeb5a62014-09-29 08:52:04 -0400597 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100598
599 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousef2741d92011-05-13 12:11:17 +0100600 return -ENAMETOOLONG;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100601
Bob Peterson2fba46a2020-02-27 12:47:53 -0600602 error = gfs2_qa_get(dip);
Bob Peterson0a305e42012-06-06 11:17:59 +0100603 if (error)
604 return error;
605
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000606 error = gfs2_rindex_update(sdp);
607 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -0600608 goto fail;
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000609
Steven Whitehousef2741d92011-05-13 12:11:17 +0100610 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100611 if (error)
612 goto fail;
Andreas Gruenbachere0b62e22017-06-30 08:16:46 -0500613 gfs2_holder_mark_uninitialized(ghs + 1);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100614
615 error = create_ok(dip, name, mode);
616 if (error)
617 goto fail_gunlock;
618
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100619 inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
620 error = PTR_ERR(inode);
621 if (!IS_ERR(inode)) {
Al Viro571a4b52014-11-19 19:34:49 +0000622 if (S_ISDIR(inode->i_mode)) {
623 iput(inode);
624 inode = ERR_PTR(-EISDIR);
625 goto fail_gunlock;
626 }
Al Viro44bb31b2014-11-19 19:35:24 +0000627 d_instantiate(dentry, inode);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100628 error = 0;
Miklos Szeredi0d0d1102013-09-16 14:52:00 +0200629 if (file) {
Al Viro44bb31b2014-11-19 19:35:24 +0000630 if (S_ISREG(inode->i_mode))
Al Virobe12af32018-06-08 11:44:56 -0400631 error = finish_open(file, dentry, gfs2_open_common);
Al Viro44bb31b2014-11-19 19:35:24 +0000632 else
633 error = finish_no_open(file, NULL);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100634 }
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100635 gfs2_glock_dq_uninit(ghs);
Bob Peterson2297ab62020-05-04 10:18:43 -0500636 goto fail;
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100637 } else if (error != -ENOENT) {
638 goto fail_gunlock;
639 }
640
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000641 error = gfs2_diradd_alloc_required(dir, name, &da);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000642 if (error < 0)
643 goto fail_gunlock;
644
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000645 inode = new_inode(sdp->sd_vfs);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000646 error = -ENOMEM;
647 if (!inode)
648 goto fail_gunlock;
649
Christoph Hellwige01580b2013-12-20 05:16:52 -0800650 error = posix_acl_create(dir, &mode, &default_acl, &acl);
651 if (error)
Bob Peterson783013c2015-12-04 10:19:14 -0600652 goto fail_gunlock;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800653
Bob Peterson8e2e0042012-07-19 08:12:40 -0400654 ip = GFS2_I(inode);
Bob Peterson2fba46a2020-02-27 12:47:53 -0600655 error = gfs2_qa_get(ip);
Steven Whitehouseff7f4cb2012-09-10 10:03:50 +0100656 if (error)
Christoph Hellwige01580b2013-12-20 05:16:52 -0800657 goto fail_free_acls;
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000658
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000659 inode->i_mode = mode;
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000660 set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000661 inode->i_rdev = dev;
662 inode->i_size = size;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700663 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000664 munge_mode_uid_gid(dip, inode);
Abhi Das00a158b2014-09-18 21:40:28 -0500665 check_and_update_goal(dip);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000666 ip->i_goal = dip->i_goal;
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000667 ip->i_diskflags = 0;
668 ip->i_eattr = 0;
669 ip->i_height = 0;
670 ip->i_depth = 0;
671 ip->i_entries = 0;
Bob Petersoncc963a12017-03-16 15:29:13 -0400672 ip->i_no_addr = 0; /* Temporarily zero until real addr is assigned */
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000673
674 switch(mode & S_IFMT) {
675 case S_IFREG:
676 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
677 gfs2_tune_get(sdp, gt_new_files_jdata))
678 ip->i_diskflags |= GFS2_DIF_JDATA;
679 gfs2_set_aops(inode);
680 break;
681 case S_IFDIR:
682 ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA);
683 ip->i_diskflags |= GFS2_DIF_JDATA;
684 ip->i_entries = 2;
685 break;
686 }
Abhi Dasacc546f2015-11-10 15:07:26 -0600687
688 /* Force SYSTEM flag on all files and subdirs of a SYSTEM directory */
689 if (dip->i_diskflags & GFS2_DIF_SYSTEM)
690 ip->i_diskflags |= GFS2_DIF_SYSTEM;
691
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000692 gfs2_set_inode_flags(inode);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000693
David Howells2b0143b2015-03-17 22:25:59 +0000694 if ((GFS2_I(d_inode(sdp->sd_root_dir)) == dip) ||
Steven Whitehouse9dbe9612012-10-31 10:37:10 +0000695 (dip->i_diskflags & GFS2_DIF_TOPDIR))
696 aflags |= GFS2_AF_ORLOV;
697
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000698 if (default_acl || acl)
699 blocks++;
700
701 error = alloc_dinode(ip, aflags, &blocks);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000702 if (error)
703 goto fail_free_inode;
704
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000705 gfs2_set_inode_blocks(inode, blocks);
706
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000707 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
708 if (error)
709 goto fail_free_inode;
Andreas Gruenbacher98e5a912017-07-19 11:10:19 -0500710
Andreas Gruenbacherdd0ecf542020-11-30 16:07:25 +0100711 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000712 if (error)
713 goto fail_free_inode;
Andreas Gruenbacherdd0ecf542020-11-30 16:07:25 +0100714 gfs2_cancel_delete_work(io_gl);
Andreas Gruenbacherdd0ecf542020-11-30 16:07:25 +0100715
Andreas Gruenbacher3d36e572021-11-30 18:26:15 +0100716 error = insert_inode_locked4(inode, ip->i_no_addr, iget_test, &ip->i_no_addr);
717 BUG_ON(error);
718
Andreas Gruenbacherdd0ecf542020-11-30 16:07:25 +0100719 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
720 if (error)
721 goto fail_gunlock2;
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000722
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000723 error = gfs2_trans_begin(sdp, blocks, 0);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000724 if (error)
Andreas Gruenbacherdd0ecf542020-11-30 16:07:25 +0100725 goto fail_gunlock2;
Bob Peterson0a305e42012-06-06 11:17:59 +0100726
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000727 if (blocks > 1) {
728 ip->i_eattr = ip->i_no_addr + 1;
729 gfs2_init_xattr(ip);
730 }
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000731 init_dinode(dip, ip, symname);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000732 gfs2_trans_end(sdp);
733
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000734 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
735 if (error)
736 goto fail_gunlock2;
737
Andreas Gruenbacher3d36e572021-11-30 18:26:15 +0100738 glock_set_object(ip->i_gl, ip);
Bob Peterson8793e142021-10-01 13:00:21 -0500739 glock_set_object(io_gl, ip);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000740 gfs2_set_iop(inode);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000741
Bob Peterson783013c2015-12-04 10:19:14 -0600742 free_vfs_inode = 0; /* After this point, the inode is no longer
743 considered free. Any failures need to undo
744 the gfs2 structures. */
Christoph Hellwige01580b2013-12-20 05:16:52 -0800745 if (default_acl) {
Al Viro1a39ba92016-05-13 03:59:17 +0200746 error = __gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100747 if (error)
748 goto fail_gunlock3;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800749 posix_acl_release(default_acl);
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100750 default_acl = NULL;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800751 }
752 if (acl) {
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100753 error = __gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS);
754 if (error)
755 goto fail_gunlock3;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800756 posix_acl_release(acl);
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100757 acl = NULL;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800758 }
759
Bob Petersonf45dc262014-03-19 09:37:00 -0400760 error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name,
761 &gfs2_initxattrs, NULL);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100762 if (error)
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000763 goto fail_gunlock3;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100764
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000765 error = link_dinode(dip, name, ip, &da);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100766 if (error)
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000767 goto fail_gunlock3;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100768
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000769 mark_inode_dirty(inode);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100770 d_instantiate(dentry, inode);
Bob Peterson2c47c1b2019-11-19 11:40:46 -0500771 /* After instantiate, errors should result in evict which will destroy
772 * both inode and iopen glocks properly. */
Miklos Szeredic5bf8fe2013-09-16 14:52:03 +0200773 if (file) {
Al Viro73a09dd2018-06-08 13:22:02 -0400774 file->f_mode |= FMODE_CREATED;
Al Virobe12af32018-06-08 11:44:56 -0400775 error = finish_open(file, dentry, gfs2_open_common);
Miklos Szeredic5bf8fe2013-09-16 14:52:03 +0200776 }
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000777 gfs2_glock_dq_uninit(ghs);
Bob Peterson2297ab62020-05-04 10:18:43 -0500778 gfs2_qa_put(ip);
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000779 gfs2_glock_dq_uninit(ghs + 1);
Bob Peterson2c47c1b2019-11-19 11:40:46 -0500780 gfs2_glock_put(io_gl);
Bob Peterson2297ab62020-05-04 10:18:43 -0500781 gfs2_qa_put(dip);
Andreas Gruenbacher3d36e572021-11-30 18:26:15 +0100782 unlock_new_inode(inode);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100783 return error;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100784
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000785fail_gunlock3:
Andreas Gruenbacher3d36e572021-11-30 18:26:15 +0100786 glock_clear_object(ip->i_gl, ip);
Bob Peterson9c1b2802017-07-18 12:26:07 -0500787 glock_clear_object(io_gl, ip);
Bob Peterson783013c2015-12-04 10:19:14 -0600788 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100789fail_gunlock2:
Bob Peterson2c47c1b2019-11-19 11:40:46 -0500790 gfs2_glock_put(io_gl);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000791fail_free_inode:
Bob Peterson9c1b2802017-07-18 12:26:07 -0500792 if (ip->i_gl) {
Bob Peterson1a0b00d2020-06-01 11:37:09 -0400793 if (free_vfs_inode) /* else evict will do the put for us */
794 gfs2_glock_put(ip->i_gl);
Bob Peterson9c1b2802017-07-18 12:26:07 -0500795 }
Andreas Gruenbacher15955482020-03-06 10:32:35 -0600796 gfs2_rs_delete(ip, NULL);
797 gfs2_qa_put(ip);
Christoph Hellwige01580b2013-12-20 05:16:52 -0800798fail_free_acls:
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100799 posix_acl_release(default_acl);
800 posix_acl_release(acl);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100801fail_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000802 gfs2_dir_no_add(&da);
Steven Whitehousef2741d92011-05-13 12:11:17 +0100803 gfs2_glock_dq_uninit(ghs);
Kefeng Wang15a798f2019-06-05 22:24:24 +0800804 if (!IS_ERR_OR_NULL(inode)) {
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000805 clear_nlink(inode);
Abhi Das05978802014-03-31 10:33:17 -0500806 if (!free_vfs_inode)
807 mark_inode_dirty(inode);
808 set_bit(free_vfs_inode ? GIF_FREE_VFS_INODE : GIF_ALLOC_FAILED,
809 &GFS2_I(inode)->i_flags);
Andreas Gruenbacher3d36e572021-11-30 18:26:15 +0100810 if (inode->i_state & I_NEW)
811 iget_failed(inode);
812 else
813 iput(inode);
Steven Whitehouse40ac2182011-08-02 13:17:27 +0100814 }
Andreas Gruenbachere0b62e22017-06-30 08:16:46 -0500815 if (gfs2_holder_initialized(ghs + 1))
816 gfs2_glock_dq_uninit(ghs + 1);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100817fail:
Bob Peterson2fba46a2020-02-27 12:47:53 -0600818 gfs2_qa_put(dip);
Steven Whitehousef2741d92011-05-13 12:11:17 +0100819 return error;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100820}
Steven Whitehousef2741d92011-05-13 12:11:17 +0100821
David Teiglandb3b94fa2006-01-16 16:50:04 +0000822/**
823 * gfs2_create - Create a file
Lee Jonesc551f662021-03-30 17:44:29 +0100824 * @mnt_userns: User namespace of the mount the inode was found from
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 * @dir: The directory in which to create the file
826 * @dentry: The dentry of the new file
827 * @mode: The mode of the new file
Lee Jonesc551f662021-03-30 17:44:29 +0100828 * @excl: Force fail if inode exists
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829 *
830 * Returns: errno
831 */
832
Christian Brauner549c7292021-01-21 14:19:43 +0100833static int gfs2_create(struct user_namespace *mnt_userns, struct inode *dir,
834 struct dentry *dentry, umode_t mode, bool excl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000835{
Al Virob452a452018-06-08 13:06:28 -0400836 return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000837}
838
839/**
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100840 * __gfs2_lookup - Look up a filename in a directory and return its inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000841 * @dir: The directory inode
842 * @dentry: The dentry of the new inode
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100843 * @file: File to be opened
David Teiglandb3b94fa2006-01-16 16:50:04 +0000844 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000845 *
846 * Returns: errno
847 */
848
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100849static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry,
Al Virob452a452018-06-08 13:06:28 -0400850 struct file *file)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000851{
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100852 struct inode *inode;
853 struct dentry *d;
854 struct gfs2_holder gh;
855 struct gfs2_glock *gl;
856 int error;
857
858 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
Benjamin Coddington7b7a9112014-09-10 14:09:20 -0400859 if (inode == NULL) {
860 d_add(dentry, NULL);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100861 return NULL;
Benjamin Coddington7b7a9112014-09-10 14:09:20 -0400862 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100863 if (IS_ERR(inode))
864 return ERR_CAST(inode);
865
866 gl = GFS2_I(inode)->i_gl;
867 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
868 if (error) {
869 iput(inode);
870 return ERR_PTR(error);
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000871 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100872
873 d = d_splice_alias(inode, dentry);
J. Bruce Fieldsd57b9c92014-01-16 13:51:07 -0500874 if (IS_ERR(d)) {
J. Bruce Fieldsd57b9c92014-01-16 13:51:07 -0500875 gfs2_glock_dq_uninit(&gh);
876 return d;
877 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100878 if (file && S_ISREG(inode->i_mode))
Al Virobe12af32018-06-08 11:44:56 -0400879 error = finish_open(file, dentry, gfs2_open_common);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100880
881 gfs2_glock_dq_uninit(&gh);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +0100882 if (error) {
883 dput(d);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100884 return ERR_PTR(error);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +0100885 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100886 return d;
887}
888
889static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
890 unsigned flags)
891{
Al Virob452a452018-06-08 13:06:28 -0400892 return __gfs2_lookup(dir, dentry, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000893}
894
895/**
896 * gfs2_link - Link to a file
897 * @old_dentry: The inode to link
898 * @dir: Add link to this directory
899 * @dentry: The name of the link
900 *
901 * Link the inode in "old_dentry" into the directory "dir" with the
902 * name in "dentry".
903 *
904 * Returns: errno
905 */
906
907static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
908 struct dentry *dentry)
909{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400910 struct gfs2_inode *dip = GFS2_I(dir);
911 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000912 struct inode *inode = d_inode(old_dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400913 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000914 struct gfs2_holder ghs[2];
Steven Whitehouse2baee032011-05-09 12:08:36 +0100915 struct buffer_head *dibh;
Bob Peterson19aeb5a62014-09-29 08:52:04 -0400916 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000917 int error;
918
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500919 if (S_ISDIR(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920 return -EPERM;
921
Bob Peterson2fba46a2020-02-27 12:47:53 -0600922 error = gfs2_qa_get(dip);
Bob Peterson0a305e42012-06-06 11:17:59 +0100923 if (error)
924 return error;
925
David Teiglandb3b94fa2006-01-16 16:50:04 +0000926 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
927 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
928
Bob Peterson72dbf472008-08-12 13:39:29 -0500929 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000930 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500931 goto out_parent;
932
933 error = gfs2_glock_nq(ghs + 1); /* child */
934 if (error)
935 goto out_child;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000936
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100937 error = -ENOENT;
938 if (inode->i_nlink == 0)
939 goto out_gunlock;
940
Christian Brauner549c7292021-01-21 14:19:43 +0100941 error = gfs2_permission(&init_user_ns, dir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000942 if (error)
943 goto out_gunlock;
944
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100945 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000946 switch (error) {
947 case -ENOENT:
948 break;
949 case 0:
950 error = -EEXIST;
Gustavo A. R. Silvae5966cf2020-11-20 12:25:03 -0600951 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000952 default:
953 goto out_gunlock;
954 }
955
956 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500957 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000958 goto out_gunlock;
959 error = -EFBIG;
Steven Whitehousead6203f22008-11-03 13:59:19 +0000960 if (dip->i_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000961 goto out_gunlock;
962 error = -EPERM;
963 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
964 goto out_gunlock;
965 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500966 if (!ip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000967 goto out_gunlock;
968 error = -EMLINK;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500969 if (ip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000970 goto out_gunlock;
971
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000972 error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da);
Steven Whitehousec7526662006-03-20 12:30:04 -0500973 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000974 goto out_gunlock;
975
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000976 if (da.nr_blocks) {
977 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
Abhi Dasb8fbf472015-03-18 12:03:41 -0500978 error = gfs2_quota_lock_check(dip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000979 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -0400980 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000981
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100982 error = gfs2_inplace_reserve(dip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000983 if (error)
984 goto out_gunlock_q;
985
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000986 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000987 if (error)
988 goto out_ipres;
989 } else {
990 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
991 if (error)
992 goto out_ipres;
993 }
994
Steven Whitehouse2baee032011-05-09 12:08:36 +0100995 error = gfs2_meta_inode_buffer(ip, &dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000996 if (error)
997 goto out_end_trans;
998
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000999 error = gfs2_dir_add(dir, &dentry->d_name, ip, &da);
Steven Whitehouse2baee032011-05-09 12:08:36 +01001000 if (error)
1001 goto out_brelse;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001002
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001003 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse2baee032011-05-09 12:08:36 +01001004 inc_nlink(&ip->i_inode);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001005 ip->i_inode.i_ctime = current_time(&ip->i_inode);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001006 ihold(inode);
1007 d_instantiate(dentry, inode);
1008 mark_inode_dirty(inode);
Steven Whitehouse2baee032011-05-09 12:08:36 +01001009
1010out_brelse:
1011 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001012out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001013 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001014out_ipres:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001015 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001016 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001017out_gunlock_q:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001018 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001019 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001020out_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001021 gfs2_dir_no_add(&da);
Bob Peterson72dbf472008-08-12 13:39:29 -05001022 gfs2_glock_dq(ghs + 1);
1023out_child:
1024 gfs2_glock_dq(ghs);
1025out_parent:
Bob Peterson2297ab62020-05-04 10:18:43 -05001026 gfs2_qa_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001027 gfs2_holder_uninit(ghs);
1028 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001029 return error;
1030}
1031
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001032/*
1033 * gfs2_unlink_ok - check to see that a inode is still in a directory
1034 * @dip: the directory
1035 * @name: the name of the file
1036 * @ip: the inode
1037 *
1038 * Assumes that the lock on (at least) @dip is held.
1039 *
1040 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1041 */
1042
1043static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
1044 const struct gfs2_inode *ip)
1045{
1046 int error;
1047
1048 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1049 return -EPERM;
1050
1051 if ((dip->i_inode.i_mode & S_ISVTX) &&
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001052 !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
1053 !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001054 return -EPERM;
1055
1056 if (IS_APPEND(&dip->i_inode))
1057 return -EPERM;
1058
Christian Brauner549c7292021-01-21 14:19:43 +01001059 error = gfs2_permission(&init_user_ns, &dip->i_inode,
1060 MAY_WRITE | MAY_EXEC);
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001061 if (error)
1062 return error;
1063
Fabian Frederick37975f12014-10-09 22:49:21 +02001064 return gfs2_dir_check(&dip->i_inode, name, ip);
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001065}
1066
David Teiglandb3b94fa2006-01-16 16:50:04 +00001067/**
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001068 * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
1069 * @dip: The parent directory
Lee Jonesc551f662021-03-30 17:44:29 +01001070 * @dentry: The dentry to unlink
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001071 *
1072 * Called with all the locks and in a transaction. This will only be
1073 * called for a directory after it has been checked to ensure it is empty.
1074 *
1075 * Returns: 0 on success, or an error
1076 */
1077
1078static int gfs2_unlink_inode(struct gfs2_inode *dip,
Bob Peterson4327a9b2012-11-12 13:03:29 -05001079 const struct dentry *dentry)
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001080{
David Howells2b0143b2015-03-17 22:25:59 +00001081 struct inode *inode = d_inode(dentry);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001082 struct gfs2_inode *ip = GFS2_I(inode);
1083 int error;
1084
1085 error = gfs2_dir_del(dip, dentry);
1086 if (error)
1087 return error;
1088
1089 ip->i_entries = 0;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001090 inode->i_ctime = current_time(inode);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001091 if (S_ISDIR(inode->i_mode))
1092 clear_nlink(inode);
1093 else
1094 drop_nlink(inode);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001095 mark_inode_dirty(inode);
1096 if (inode->i_nlink == 0)
1097 gfs2_unlink_di(inode);
1098 return 0;
1099}
1100
1101
1102/**
1103 * gfs2_unlink - Unlink an inode (this does rmdir as well)
1104 * @dir: The inode of the directory containing the inode to unlink
David Teiglandb3b94fa2006-01-16 16:50:04 +00001105 * @dentry: The file itself
1106 *
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001107 * This routine uses the type of the inode as a flag to figure out
1108 * whether this is an unlink or an rmdir.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001109 *
1110 * Returns: errno
1111 */
1112
1113static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
1114{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001115 struct gfs2_inode *dip = GFS2_I(dir);
1116 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +00001117 struct inode *inode = d_inode(dentry);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001118 struct gfs2_inode *ip = GFS2_I(inode);
Russell Cattelanddee7602007-01-29 17:13:44 -06001119 struct gfs2_holder ghs[3];
1120 struct gfs2_rgrpd *rgd;
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001121 int error;
1122
1123 error = gfs2_rindex_update(sdp);
1124 if (error)
1125 return error;
1126
1127 error = -EROFS;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001128
Russell Cattelanddee7602007-01-29 17:13:44 -06001129 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
1130 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
1131
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001132 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
Steven Whitehousea365fbf2012-02-24 15:09:14 +00001133 if (!rgd)
Steven Whitehouse87654892011-11-08 14:04:20 +00001134 goto out_inodes;
Steven Whitehousea365fbf2012-02-24 15:09:14 +00001135
Bob Peterson4fc7ec32018-04-24 10:35:02 -07001136 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, LM_FLAG_NODE_SCOPE, ghs + 2);
Russell Cattelanddee7602007-01-29 17:13:44 -06001137
1138
Steven Whitehouse8497a462007-08-26 14:23:56 +01001139 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001140 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +01001141 goto out_parent;
1142
1143 error = gfs2_glock_nq(ghs + 1); /* child */
1144 if (error)
1145 goto out_child;
1146
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001147 error = -ENOENT;
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001148 if (inode->i_nlink == 0)
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001149 goto out_rgrp;
1150
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001151 if (S_ISDIR(inode->i_mode)) {
1152 error = -ENOTEMPTY;
1153 if (ip->i_entries > 2 || inode->i_nlink > 2)
1154 goto out_rgrp;
1155 }
1156
Steven Whitehouse8497a462007-08-26 14:23:56 +01001157 error = gfs2_glock_nq(ghs + 2); /* rgrp */
1158 if (error)
1159 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001160
1161 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
1162 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -05001163 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001164
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001165 error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001166 if (error)
Bob Peterson2eb59092018-01-29 10:00:23 -07001167 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001168
Bob Peterson4327a9b2012-11-12 13:03:29 -05001169 error = gfs2_unlink_inode(dip, dentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001170 gfs2_trans_end(sdp);
Bob Peterson2eb59092018-01-29 10:00:23 -07001171
Bob Peterson72dbf472008-08-12 13:39:29 -05001172out_gunlock:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001173 gfs2_glock_dq(ghs + 2);
1174out_rgrp:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001175 gfs2_glock_dq(ghs + 1);
1176out_child:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001177 gfs2_glock_dq(ghs);
1178out_parent:
Steven Whitehouse87654892011-11-08 14:04:20 +00001179 gfs2_holder_uninit(ghs + 2);
1180out_inodes:
1181 gfs2_holder_uninit(ghs + 1);
Steven Whitehouse8497a462007-08-26 14:23:56 +01001182 gfs2_holder_uninit(ghs);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001183 return error;
1184}
1185
1186/**
1187 * gfs2_symlink - Create a symlink
Lee Jonesc551f662021-03-30 17:44:29 +01001188 * @mnt_userns: User namespace of the mount the inode was found from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001189 * @dir: The directory to create the symlink in
1190 * @dentry: The dentry to put the symlink in
1191 * @symname: The thing which the link points to
1192 *
1193 * Returns: errno
1194 */
1195
Christian Brauner549c7292021-01-21 14:19:43 +01001196static int gfs2_symlink(struct user_namespace *mnt_userns, struct inode *dir,
1197 struct dentry *dentry, const char *symname)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001198{
Steven Whitehouse160b4022011-05-13 10:34:59 +01001199 unsigned int size;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001200
David Teiglandb3b94fa2006-01-16 16:50:04 +00001201 size = strlen(symname);
Andreas Gruenbacher235628c2017-11-14 16:53:12 +01001202 if (size >= gfs2_max_stuffed_size(GFS2_I(dir)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001203 return -ENAMETOOLONG;
1204
Al Virob452a452018-06-08 13:06:28 -04001205 return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001206}
1207
1208/**
1209 * gfs2_mkdir - Make a directory
Lee Jonesc551f662021-03-30 17:44:29 +01001210 * @mnt_userns: User namespace of the mount the inode was found from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001211 * @dir: The parent directory of the new one
1212 * @dentry: The dentry of the new directory
1213 * @mode: The mode of the new directory
1214 *
1215 * Returns: errno
1216 */
1217
Christian Brauner549c7292021-01-21 14:19:43 +01001218static int gfs2_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
1219 struct dentry *dentry, umode_t mode)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001220{
Andreas Gruenbacher235628c2017-11-14 16:53:12 +01001221 unsigned dsize = gfs2_max_stuffed_size(GFS2_I(dir));
Al Virob452a452018-06-08 13:06:28 -04001222 return gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001223}
1224
1225/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001226 * gfs2_mknod - Make a special file
Lee Jonesc551f662021-03-30 17:44:29 +01001227 * @mnt_userns: User namespace of the mount the inode was found from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001228 * @dir: The directory in which the special file will reside
1229 * @dentry: The dentry of the special file
1230 * @mode: The mode of the special file
Steven Whitehousef2741d92011-05-13 12:11:17 +01001231 * @dev: The device specification of the special file
David Teiglandb3b94fa2006-01-16 16:50:04 +00001232 *
1233 */
1234
Christian Brauner549c7292021-01-21 14:19:43 +01001235static int gfs2_mknod(struct user_namespace *mnt_userns, struct inode *dir,
1236 struct dentry *dentry, umode_t mode, dev_t dev)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001237{
Al Virob452a452018-06-08 13:06:28 -04001238 return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001239}
1240
1241/**
1242 * gfs2_atomic_open - Atomically open a file
1243 * @dir: The directory
1244 * @dentry: The proposed new entry
1245 * @file: The proposed new struct file
1246 * @flags: open flags
1247 * @mode: File mode
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001248 *
1249 * Returns: error code or 0 for success
1250 */
1251
1252static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry,
Antonio Ospite86fbca42015-05-01 12:54:38 -05001253 struct file *file, unsigned flags,
Al Viro44907d72018-06-08 13:32:02 -04001254 umode_t mode)
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001255{
1256 struct dentry *d;
1257 bool excl = !!(flags & O_EXCL);
1258
Al Viro00699ad2016-07-05 09:44:53 -04001259 if (!d_in_lookup(dentry))
Al Viro4d93bc32014-09-12 18:21:05 -04001260 goto skip_lookup;
1261
Al Virob452a452018-06-08 13:06:28 -04001262 d = __gfs2_lookup(dir, dentry, file);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001263 if (IS_ERR(d))
1264 return PTR_ERR(d);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001265 if (d != NULL)
1266 dentry = d;
David Howells2b0143b2015-03-17 22:25:59 +00001267 if (d_really_is_positive(dentry)) {
Al Viroaad888f2018-06-08 12:58:04 -04001268 if (!(file->f_mode & FMODE_OPENED))
Al Viroec7d8792014-11-19 19:35:58 +00001269 return finish_no_open(file, d);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001270 dput(d);
Al Viro21039132020-03-10 09:31:41 -04001271 return excl && (flags & O_CREAT) ? -EEXIST : 0;
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001272 }
1273
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001274 BUG_ON(d != NULL);
Al Viro4d93bc32014-09-12 18:21:05 -04001275
1276skip_lookup:
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001277 if (!(flags & O_CREAT))
1278 return -ENOENT;
1279
Al Virob452a452018-06-08 13:06:28 -04001280 return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001281}
1282
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001283/*
1284 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1285 * @this: move this
1286 * @to: to here
1287 *
1288 * Follow @to back to the root and make sure we don't encounter @this
1289 * Assumes we already hold the rename lock.
1290 *
1291 * Returns: errno
1292 */
1293
1294static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1295{
1296 struct inode *dir = &to->i_inode;
1297 struct super_block *sb = dir->i_sb;
1298 struct inode *tmp;
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001299 int error = 0;
1300
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001301 igrab(dir);
1302
1303 for (;;) {
1304 if (dir == &this->i_inode) {
1305 error = -EINVAL;
1306 break;
1307 }
David Howells2b0143b2015-03-17 22:25:59 +00001308 if (dir == d_inode(sb->s_root)) {
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001309 error = 0;
1310 break;
1311 }
1312
Steven Whitehouse8d123582010-09-17 12:30:23 +01001313 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
Abhi Das48f8f712014-03-12 03:41:44 -05001314 if (!tmp) {
1315 error = -ENOENT;
1316 break;
1317 }
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001318 if (IS_ERR(tmp)) {
1319 error = PTR_ERR(tmp);
1320 break;
1321 }
1322
1323 iput(dir);
1324 dir = tmp;
1325 }
1326
1327 iput(dir);
1328
1329 return error;
1330}
1331
David Teiglandb3b94fa2006-01-16 16:50:04 +00001332/**
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001333 * update_moved_ino - Update an inode that's being moved
1334 * @ip: The inode being moved
1335 * @ndip: The parent directory of the new filename
1336 * @dir_rename: True of ip is a directory
1337 *
1338 * Returns: errno
1339 */
1340
1341static int update_moved_ino(struct gfs2_inode *ip, struct gfs2_inode *ndip,
1342 int dir_rename)
1343{
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001344 if (dir_rename)
1345 return gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
1346
Deepa Dinamani078cd822016-09-14 07:48:04 -07001347 ip->i_inode.i_ctime = current_time(&ip->i_inode);
Andreas Gruenbacher83998cc2018-02-28 12:48:53 -07001348 mark_inode_dirty_sync(&ip->i_inode);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001349 return 0;
1350}
1351
1352
1353/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001354 * gfs2_rename - Rename a file
1355 * @odir: Parent directory of old file name
1356 * @odentry: The old dentry of the file
1357 * @ndir: Parent directory of new file name
1358 * @ndentry: The new dentry of the file
1359 *
1360 * Returns: errno
1361 */
1362
1363static int gfs2_rename(struct inode *odir, struct dentry *odentry,
1364 struct inode *ndir, struct dentry *ndentry)
1365{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001366 struct gfs2_inode *odip = GFS2_I(odir);
1367 struct gfs2_inode *ndip = GFS2_I(ndir);
David Howells2b0143b2015-03-17 22:25:59 +00001368 struct gfs2_inode *ip = GFS2_I(d_inode(odentry));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001369 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001370 struct gfs2_sbd *sdp = GFS2_SB(odir);
Bob Petersonbc74aae2019-08-30 12:31:00 -05001371 struct gfs2_holder ghs[4], r_gh, rd_gh;
Russell Cattelanddee7602007-01-29 17:13:44 -06001372 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001373 unsigned int num_gh;
1374 int dir_rename = 0;
Bob Peterson19aeb5a62014-09-29 08:52:04 -04001375 struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, };
David Teiglandb3b94fa2006-01-16 16:50:04 +00001376 unsigned int x;
1377 int error;
1378
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001379 gfs2_holder_mark_uninitialized(&r_gh);
Bob Petersonbc74aae2019-08-30 12:31:00 -05001380 gfs2_holder_mark_uninitialized(&rd_gh);
David Howells2b0143b2015-03-17 22:25:59 +00001381 if (d_really_is_positive(ndentry)) {
1382 nip = GFS2_I(d_inode(ndentry));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001383 if (ip == nip)
1384 return 0;
1385 }
1386
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001387 error = gfs2_rindex_update(sdp);
1388 if (error)
1389 return error;
1390
Bob Peterson2fba46a2020-02-27 12:47:53 -06001391 error = gfs2_qa_get(ndip);
Bob Peterson0a305e42012-06-06 11:17:59 +01001392 if (error)
1393 return error;
1394
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001395 if (odip != ndip) {
1396 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1397 0, &r_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001398 if (error)
1399 goto out;
1400
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001401 if (S_ISDIR(ip->i_inode.i_mode)) {
1402 dir_rename = 1;
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001403 /* don't move a directory into its subdir */
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001404 error = gfs2_ok_to_move(ip, ndip);
1405 if (error)
1406 goto out_gunlock_r;
1407 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001408 }
1409
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001410 num_gh = 1;
Bob Petersonad269672019-08-30 12:31:02 -05001411 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001412 if (odip != ndip) {
Bob Petersonad269672019-08-30 12:31:02 -05001413 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE,GL_ASYNC,
1414 ghs + num_gh);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001415 num_gh++;
1416 }
Bob Petersonad269672019-08-30 12:31:02 -05001417 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001418 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001419
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001420 if (nip) {
Bob Petersonad269672019-08-30 12:31:02 -05001421 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1422 ghs + num_gh);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001423 num_gh++;
1424 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001425
Bob Peterson72dbf472008-08-12 13:39:29 -05001426 for (x = 0; x < num_gh; x++) {
1427 error = gfs2_glock_nq(ghs + x);
1428 if (error)
1429 goto out_gunlock;
1430 }
Bob Petersonad269672019-08-30 12:31:02 -05001431 error = gfs2_glock_async_wait(num_gh, ghs);
1432 if (error)
1433 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001434
Bob Petersonbc74aae2019-08-30 12:31:00 -05001435 if (nip) {
1436 /* Grab the resource group glock for unlink flag twiddling.
1437 * This is the case where the target dinode already exists
1438 * so we unlink before doing the rename.
1439 */
1440 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
1441 if (!nrgd) {
1442 error = -ENOENT;
1443 goto out_gunlock;
1444 }
Bob Peterson4fc7ec32018-04-24 10:35:02 -07001445 error = gfs2_glock_nq_init(nrgd->rd_gl, LM_ST_EXCLUSIVE,
1446 LM_FLAG_NODE_SCOPE, &rd_gh);
Bob Petersonbc74aae2019-08-30 12:31:00 -05001447 if (error)
1448 goto out_gunlock;
1449 }
1450
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001451 error = -ENOENT;
1452 if (ip->i_inode.i_nlink == 0)
1453 goto out_gunlock;
1454
David Teiglandb3b94fa2006-01-16 16:50:04 +00001455 /* Check out the old directory */
1456
1457 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
1458 if (error)
1459 goto out_gunlock;
1460
1461 /* Check out the new directory */
1462
1463 if (nip) {
1464 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1465 if (error)
1466 goto out_gunlock;
1467
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001468 if (nip->i_inode.i_nlink == 0) {
1469 error = -EAGAIN;
1470 goto out_gunlock;
1471 }
1472
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001473 if (S_ISDIR(nip->i_inode.i_mode)) {
Steven Whitehousead6203f22008-11-03 13:59:19 +00001474 if (nip->i_entries < 2) {
Steven Whitehouse94fb7632011-05-09 13:36:10 +01001475 gfs2_consist_inode(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001476 error = -EIO;
1477 goto out_gunlock;
1478 }
Steven Whitehousead6203f22008-11-03 13:59:19 +00001479 if (nip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001480 error = -ENOTEMPTY;
1481 goto out_gunlock;
1482 }
1483 }
1484 } else {
Christian Brauner549c7292021-01-21 14:19:43 +01001485 error = gfs2_permission(&init_user_ns, ndir,
1486 MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001487 if (error)
1488 goto out_gunlock;
1489
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001490 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001491 switch (error) {
1492 case -ENOENT:
1493 error = 0;
1494 break;
1495 case 0:
1496 error = -EEXIST;
Gustavo A. R. Silvae5966cf2020-11-20 12:25:03 -06001497 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001498 default:
1499 goto out_gunlock;
Aliasgar Surti098b9c12019-10-04 10:55:29 -05001500 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001501
1502 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -05001503 if (!ndip->i_inode.i_nlink) {
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001504 error = -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001505 goto out_gunlock;
1506 }
Steven Whitehousead6203f22008-11-03 13:59:19 +00001507 if (ndip->i_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001508 error = -EFBIG;
1509 goto out_gunlock;
1510 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001511 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -05001512 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001513 error = -EMLINK;
1514 goto out_gunlock;
1515 }
1516 }
1517 }
1518
1519 /* Check out the dir to be renamed */
1520
1521 if (dir_rename) {
Christian Brauner549c7292021-01-21 14:19:43 +01001522 error = gfs2_permission(&init_user_ns, d_inode(odentry),
1523 MAY_WRITE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001524 if (error)
1525 goto out_gunlock;
1526 }
1527
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001528 if (nip == NULL) {
1529 error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
1530 if (error)
1531 goto out_gunlock;
1532 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001533
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001534 if (da.nr_blocks) {
1535 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
Abhi Dasb8fbf472015-03-18 12:03:41 -05001536 error = gfs2_quota_lock_check(ndip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001537 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001538 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001539
Steven Whitehouse7b9cff42013-10-02 11:13:25 +01001540 error = gfs2_inplace_reserve(ndip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001541 if (error)
1542 goto out_gunlock_q;
1543
Steven Whitehouse534cf9c2014-01-06 12:03:05 +00001544 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
1545 4 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001546 if (error)
1547 goto out_ipreserv;
1548 } else {
1549 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -05001550 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001551 if (error)
1552 goto out_gunlock;
1553 }
1554
1555 /* Remove the target file, if it exists */
1556
Bob Peterson4327a9b2012-11-12 13:03:29 -05001557 if (nip)
1558 error = gfs2_unlink_inode(ndip, ndentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001559
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001560 error = update_moved_ino(ip, ndip, dir_rename);
1561 if (error)
1562 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001563
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001564 error = gfs2_dir_del(odip, odentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001565 if (error)
1566 goto out_end_trans;
1567
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001568 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001569 if (error)
1570 goto out_end_trans;
1571
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001572out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001573 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001574out_ipreserv:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001575 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001576 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001577out_gunlock_q:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001578 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001579 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001580out_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001581 gfs2_dir_no_add(&da);
Bob Petersonbc74aae2019-08-30 12:31:00 -05001582 if (gfs2_holder_initialized(&rd_gh))
1583 gfs2_glock_dq_uninit(&rd_gh);
1584
Bob Peterson72dbf472008-08-12 13:39:29 -05001585 while (x--) {
Bob Petersonad269672019-08-30 12:31:02 -05001586 if (gfs2_holder_queued(ghs + x))
1587 gfs2_glock_dq(ghs + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001588 gfs2_holder_uninit(ghs + x);
Bob Peterson72dbf472008-08-12 13:39:29 -05001589 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001590out_gunlock_r:
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001591 if (gfs2_holder_initialized(&r_gh))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001592 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001593out:
Bob Peterson2fba46a2020-02-27 12:47:53 -06001594 gfs2_qa_put(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001595 return error;
1596}
1597
1598/**
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001599 * gfs2_exchange - exchange two files
1600 * @odir: Parent directory of old file name
1601 * @odentry: The old dentry of the file
1602 * @ndir: Parent directory of new file name
1603 * @ndentry: The new dentry of the file
1604 * @flags: The rename flags
1605 *
1606 * Returns: errno
1607 */
1608
1609static int gfs2_exchange(struct inode *odir, struct dentry *odentry,
1610 struct inode *ndir, struct dentry *ndentry,
1611 unsigned int flags)
1612{
1613 struct gfs2_inode *odip = GFS2_I(odir);
1614 struct gfs2_inode *ndip = GFS2_I(ndir);
1615 struct gfs2_inode *oip = GFS2_I(odentry->d_inode);
1616 struct gfs2_inode *nip = GFS2_I(ndentry->d_inode);
1617 struct gfs2_sbd *sdp = GFS2_SB(odir);
Bob Petersonad269672019-08-30 12:31:02 -05001618 struct gfs2_holder ghs[4], r_gh;
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001619 unsigned int num_gh;
1620 unsigned int x;
1621 umode_t old_mode = oip->i_inode.i_mode;
1622 umode_t new_mode = nip->i_inode.i_mode;
1623 int error;
1624
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001625 gfs2_holder_mark_uninitialized(&r_gh);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001626 error = gfs2_rindex_update(sdp);
1627 if (error)
1628 return error;
1629
1630 if (odip != ndip) {
1631 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1632 0, &r_gh);
1633 if (error)
1634 goto out;
1635
1636 if (S_ISDIR(old_mode)) {
1637 /* don't move a directory into its subdir */
1638 error = gfs2_ok_to_move(oip, ndip);
1639 if (error)
1640 goto out_gunlock_r;
1641 }
1642
1643 if (S_ISDIR(new_mode)) {
1644 /* don't move a directory into its subdir */
1645 error = gfs2_ok_to_move(nip, odip);
1646 if (error)
1647 goto out_gunlock_r;
1648 }
1649 }
1650
1651 num_gh = 1;
Bob Petersonad269672019-08-30 12:31:02 -05001652 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001653 if (odip != ndip) {
Bob Petersonad269672019-08-30 12:31:02 -05001654 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1655 ghs + num_gh);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001656 num_gh++;
1657 }
Bob Petersonad269672019-08-30 12:31:02 -05001658 gfs2_holder_init(oip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001659 num_gh++;
1660
Bob Petersonad269672019-08-30 12:31:02 -05001661 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001662 num_gh++;
1663
1664 for (x = 0; x < num_gh; x++) {
1665 error = gfs2_glock_nq(ghs + x);
1666 if (error)
1667 goto out_gunlock;
1668 }
1669
Bob Petersonad269672019-08-30 12:31:02 -05001670 error = gfs2_glock_async_wait(num_gh, ghs);
1671 if (error)
1672 goto out_gunlock;
1673
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001674 error = -ENOENT;
1675 if (oip->i_inode.i_nlink == 0 || nip->i_inode.i_nlink == 0)
1676 goto out_gunlock;
1677
1678 error = gfs2_unlink_ok(odip, &odentry->d_name, oip);
1679 if (error)
1680 goto out_gunlock;
1681 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1682 if (error)
1683 goto out_gunlock;
1684
1685 if (S_ISDIR(old_mode)) {
Christian Brauner549c7292021-01-21 14:19:43 +01001686 error = gfs2_permission(&init_user_ns, odentry->d_inode,
1687 MAY_WRITE);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001688 if (error)
1689 goto out_gunlock;
1690 }
1691 if (S_ISDIR(new_mode)) {
Christian Brauner549c7292021-01-21 14:19:43 +01001692 error = gfs2_permission(&init_user_ns, ndentry->d_inode,
1693 MAY_WRITE);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001694 if (error)
1695 goto out_gunlock;
1696 }
1697 error = gfs2_trans_begin(sdp, 4 * RES_DINODE + 4 * RES_LEAF, 0);
1698 if (error)
1699 goto out_gunlock;
1700
1701 error = update_moved_ino(oip, ndip, S_ISDIR(old_mode));
1702 if (error)
1703 goto out_end_trans;
1704
1705 error = update_moved_ino(nip, odip, S_ISDIR(new_mode));
1706 if (error)
1707 goto out_end_trans;
1708
1709 error = gfs2_dir_mvino(ndip, &ndentry->d_name, oip,
1710 IF2DT(old_mode));
1711 if (error)
1712 goto out_end_trans;
1713
1714 error = gfs2_dir_mvino(odip, &odentry->d_name, nip,
1715 IF2DT(new_mode));
1716 if (error)
1717 goto out_end_trans;
1718
1719 if (odip != ndip) {
1720 if (S_ISDIR(new_mode) && !S_ISDIR(old_mode)) {
1721 inc_nlink(&odip->i_inode);
1722 drop_nlink(&ndip->i_inode);
1723 } else if (S_ISDIR(old_mode) && !S_ISDIR(new_mode)) {
1724 inc_nlink(&ndip->i_inode);
1725 drop_nlink(&odip->i_inode);
1726 }
1727 }
1728 mark_inode_dirty(&ndip->i_inode);
1729 if (odip != ndip)
1730 mark_inode_dirty(&odip->i_inode);
1731
1732out_end_trans:
1733 gfs2_trans_end(sdp);
1734out_gunlock:
1735 while (x--) {
Bob Petersonad269672019-08-30 12:31:02 -05001736 if (gfs2_holder_queued(ghs + x))
1737 gfs2_glock_dq(ghs + x);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001738 gfs2_holder_uninit(ghs + x);
1739 }
1740out_gunlock_r:
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001741 if (gfs2_holder_initialized(&r_gh))
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001742 gfs2_glock_dq_uninit(&r_gh);
1743out:
1744 return error;
1745}
1746
Christian Brauner549c7292021-01-21 14:19:43 +01001747static int gfs2_rename2(struct user_namespace *mnt_userns, struct inode *odir,
1748 struct dentry *odentry, struct inode *ndir,
1749 struct dentry *ndentry, unsigned int flags)
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001750{
1751 flags &= ~RENAME_NOREPLACE;
1752
1753 if (flags & ~RENAME_EXCHANGE)
1754 return -EINVAL;
1755
1756 if (flags & RENAME_EXCHANGE)
1757 return gfs2_exchange(odir, odentry, ndir, ndentry, flags);
1758
1759 return gfs2_rename(odir, odentry, ndir, ndentry);
1760}
1761
1762/**
Al Viro6b255392015-11-17 10:20:54 -05001763 * gfs2_get_link - Follow a symbolic link
David Teiglandb3b94fa2006-01-16 16:50:04 +00001764 * @dentry: The dentry of the link
Al Viro6b255392015-11-17 10:20:54 -05001765 * @inode: The inode of the link
Al Virofceef392015-12-29 15:58:39 -05001766 * @done: destructor for return value
David Teiglandb3b94fa2006-01-16 16:50:04 +00001767 *
Al Viroc177c2a2010-01-14 00:59:16 -05001768 * This can handle symlinks of any size.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001769 *
1770 * Returns: 0 on success or error code
1771 */
1772
Al Viro6b255392015-11-17 10:20:54 -05001773static const char *gfs2_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001774 struct inode *inode,
1775 struct delayed_call *done)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001776{
Al Viro6b255392015-11-17 10:20:54 -05001777 struct gfs2_inode *ip = GFS2_I(inode);
Al Viroc177c2a2010-01-14 00:59:16 -05001778 struct gfs2_holder i_gh;
1779 struct buffer_head *dibh;
Steven Whitehouse160b4022011-05-13 10:34:59 +01001780 unsigned int size;
Al Viroc177c2a2010-01-14 00:59:16 -05001781 char *buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001782 int error;
1783
Al Viro6b255392015-11-17 10:20:54 -05001784 if (!dentry)
1785 return ERR_PTR(-ECHILD);
1786
Al Viroc177c2a2010-01-14 00:59:16 -05001787 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1788 error = gfs2_glock_nq(&i_gh);
1789 if (error) {
1790 gfs2_holder_uninit(&i_gh);
Al Viro680baac2015-05-02 13:32:22 -04001791 return ERR_PTR(error);
Al Viroc177c2a2010-01-14 00:59:16 -05001792 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001793
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001794 size = (unsigned int)i_size_read(&ip->i_inode);
1795 if (size == 0) {
Al Viroc177c2a2010-01-14 00:59:16 -05001796 gfs2_consist_inode(ip);
1797 buf = ERR_PTR(-EIO);
1798 goto out;
1799 }
1800
1801 error = gfs2_meta_inode_buffer(ip, &dibh);
1802 if (error) {
1803 buf = ERR_PTR(error);
1804 goto out;
1805 }
1806
Steven Whitehouse160b4022011-05-13 10:34:59 +01001807 buf = kzalloc(size + 1, GFP_NOFS);
Al Viroc177c2a2010-01-14 00:59:16 -05001808 if (!buf)
1809 buf = ERR_PTR(-ENOMEM);
1810 else
Steven Whitehouse160b4022011-05-13 10:34:59 +01001811 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
Al Viroc177c2a2010-01-14 00:59:16 -05001812 brelse(dibh);
1813out:
1814 gfs2_glock_dq_uninit(&i_gh);
Al Viro680baac2015-05-02 13:32:22 -04001815 if (!IS_ERR(buf))
Al Virofceef392015-12-29 15:58:39 -05001816 set_delayed_call(done, kfree_link, buf);
Al Viro680baac2015-05-02 13:32:22 -04001817 return buf;
Al Viroc177c2a2010-01-14 00:59:16 -05001818}
1819
David Teiglandb3b94fa2006-01-16 16:50:04 +00001820/**
Lee Jonesc551f662021-03-30 17:44:29 +01001821 * gfs2_permission
1822 * @mnt_userns: User namespace of the mount the inode was found from
Steven Whitehouse75d5cfb2011-01-19 09:42:40 +00001823 * @inode: The inode
1824 * @mask: The mask to be tested
David Teiglandb3b94fa2006-01-16 16:50:04 +00001825 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001826 * This may be called from the VFS directly, or from within GFS2 with the
1827 * inode locked, so we look to see if the glock is already locked and only
1828 * lock the glock if its not already been done.
1829 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001830 * Returns: errno
1831 */
1832
Christian Brauner549c7292021-01-21 14:19:43 +01001833int gfs2_permission(struct user_namespace *mnt_userns, struct inode *inode,
1834 int mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001835{
Nick Pigginb74c79e2011-01-07 17:49:58 +11001836 struct gfs2_inode *ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001837 struct gfs2_holder i_gh;
1838 int error;
1839
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001840 gfs2_holder_mark_uninitialized(&i_gh);
Nick Pigginb74c79e2011-01-07 17:49:58 +11001841 ip = GFS2_I(inode);
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001842 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001843 if (mask & MAY_NOT_BLOCK)
1844 return -ECHILD;
1845 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1846 if (error)
1847 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001848 }
1849
Miklos Szeredif58ba882008-07-02 21:12:01 +02001850 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
Eryu Guan337684a2016-08-02 19:58:28 +08001851 error = -EPERM;
Miklos Szeredif58ba882008-07-02 21:12:01 +02001852 else
Christian Brauner47291ba2021-01-21 14:19:24 +01001853 error = generic_permission(&init_user_ns, inode, mask);
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001854 if (gfs2_holder_initialized(&i_gh))
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001855 gfs2_glock_dq_uninit(&i_gh);
1856
David Teiglandb3b94fa2006-01-16 16:50:04 +00001857 return error;
1858}
1859
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001860static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
Steven Whitehouse194c0112011-05-09 14:06:38 +01001861{
Christian Brauner2f221d62021-01-21 14:19:26 +01001862 setattr_copy(&init_user_ns, inode, attr);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001863 mark_inode_dirty(inode);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001864 return 0;
1865}
1866
Andreas Gruenbachera4122a92021-04-07 00:59:03 +02001867static int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
Steven Whitehouse194c0112011-05-09 14:06:38 +01001868{
1869 int error;
1870
1871 if (current->journal_info)
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001872 return __gfs2_setattr_simple(inode, attr);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001873
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001874 error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001875 if (error)
1876 return error;
1877
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001878 error = __gfs2_setattr_simple(inode, attr);
1879 gfs2_trans_end(GFS2_SB(inode));
Steven Whitehouse194c0112011-05-09 14:06:38 +01001880 return error;
1881}
1882
David Teiglandb3b94fa2006-01-16 16:50:04 +00001883static int setattr_chown(struct inode *inode, struct iattr *attr)
1884{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001885 struct gfs2_inode *ip = GFS2_I(inode);
1886 struct gfs2_sbd *sdp = GFS2_SB(inode);
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -08001887 kuid_t ouid, nuid;
1888 kgid_t ogid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001889 int error;
Abhi Dasb8fbf472015-03-18 12:03:41 -05001890 struct gfs2_alloc_parms ap;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001891
Steven Whitehouse2933f922006-11-01 13:23:29 -05001892 ouid = inode->i_uid;
1893 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001894 nuid = attr->ia_uid;
1895 ngid = attr->ia_gid;
1896
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001897 if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001898 ouid = nuid = NO_UID_QUOTA_CHANGE;
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001899 if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001900 ogid = ngid = NO_GID_QUOTA_CHANGE;
Bob Peterson2fba46a2020-02-27 12:47:53 -06001901 error = gfs2_qa_get(ip);
Bob Peterson62e96cf2014-01-06 17:16:01 -05001902 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -06001903 return error;
Bob Peterson62e96cf2014-01-06 17:16:01 -05001904
1905 error = gfs2_rindex_update(sdp);
1906 if (error)
1907 goto out;
1908
1909 error = gfs2_quota_lock(ip, nuid, ngid);
1910 if (error)
1911 goto out;
1912
Abhi Dasb8fbf472015-03-18 12:03:41 -05001913 ap.target = gfs2_get_inode_blocks(&ip->i_inode);
1914
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001915 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1916 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
Abhi Dasb8fbf472015-03-18 12:03:41 -05001917 error = gfs2_quota_check(ip, nuid, ngid, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001918 if (error)
1919 goto out_gunlock_q;
1920 }
1921
1922 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1923 if (error)
1924 goto out_gunlock_q;
1925
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001926 error = gfs2_setattr_simple(inode, attr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001927 if (error)
1928 goto out_end_trans;
1929
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001930 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1931 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
Abhi Das39a72582015-06-02 11:02:24 -05001932 gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid);
Abhi Dasb8fbf472015-03-18 12:03:41 -05001933 gfs2_quota_change(ip, ap.target, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001934 }
1935
Steven Whitehousea91ea692006-09-04 12:04:26 -04001936out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001937 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001938out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001939 gfs2_quota_unlock(ip);
Bob Peterson62e96cf2014-01-06 17:16:01 -05001940out:
Bob Peterson2fba46a2020-02-27 12:47:53 -06001941 gfs2_qa_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001942 return error;
1943}
1944
1945/**
1946 * gfs2_setattr - Change attributes on an inode
Lee Jonesc551f662021-03-30 17:44:29 +01001947 * @mnt_userns: User namespace of the mount the inode was found from
David Teiglandb3b94fa2006-01-16 16:50:04 +00001948 * @dentry: The dentry which is changing
1949 * @attr: The structure describing the change
1950 *
1951 * The VFS layer wants to change one or more of an inodes attributes. Write
1952 * that change out to disk.
1953 *
1954 * Returns: errno
1955 */
1956
Christian Brauner549c7292021-01-21 14:19:43 +01001957static int gfs2_setattr(struct user_namespace *mnt_userns,
1958 struct dentry *dentry, struct iattr *attr)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001959{
David Howells2b0143b2015-03-17 22:25:59 +00001960 struct inode *inode = d_inode(dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001961 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001962 struct gfs2_holder i_gh;
1963 int error;
1964
Bob Peterson2fba46a2020-02-27 12:47:53 -06001965 error = gfs2_qa_get(ip);
Bob Peterson0a305e42012-06-06 11:17:59 +01001966 if (error)
1967 return error;
1968
David Teiglandb3b94fa2006-01-16 16:50:04 +00001969 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1970 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -06001971 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001972
Andreas Gruenbacherd75b9fa02021-07-28 07:47:34 -05001973 error = may_setattr(&init_user_ns, inode, attr->ia_valid);
1974 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -06001975 goto error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001976
Christian Brauner2f221d62021-01-21 14:19:26 +01001977 error = setattr_prepare(&init_user_ns, dentry, attr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001978 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -06001979 goto error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001980
1981 if (attr->ia_valid & ATTR_SIZE)
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001982 error = gfs2_setattr_size(inode, attr->ia_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001983 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1984 error = setattr_chown(inode, attr);
Christoph Hellwige01580b2013-12-20 05:16:52 -08001985 else {
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001986 error = gfs2_setattr_simple(inode, attr);
Christoph Hellwige01580b2013-12-20 05:16:52 -08001987 if (!error && attr->ia_valid & ATTR_MODE)
Christian Braunere65ce2a2021-01-21 14:19:27 +01001988 error = posix_acl_chmod(&init_user_ns, inode,
1989 inode->i_mode);
Christoph Hellwige01580b2013-12-20 05:16:52 -08001990 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001991
Bob Peterson2fba46a2020-02-27 12:47:53 -06001992error:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001993 if (!error)
1994 mark_inode_dirty(inode);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001995 gfs2_glock_dq_uninit(&i_gh);
Bob Peterson2fba46a2020-02-27 12:47:53 -06001996out:
1997 gfs2_qa_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001998 return error;
1999}
2000
2001/**
2002 * gfs2_getattr - Read out an inode's attributes
Christian Brauner549c7292021-01-21 14:19:43 +01002003 * @mnt_userns: user namespace of the mount the inode was found from
David Howellsa528d352017-01-31 16:46:22 +00002004 * @path: Object to query
David Teiglandb3b94fa2006-01-16 16:50:04 +00002005 * @stat: The inode's stats
David Howellsa528d352017-01-31 16:46:22 +00002006 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
2007 * @flags: AT_STATX_xxx setting
David Teiglandb3b94fa2006-01-16 16:50:04 +00002008 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05002009 * This may be called from the VFS directly, or from within GFS2 with the
2010 * inode locked, so we look to see if the glock is already locked and only
2011 * lock the glock if its not already been done. Note that its the NFS
2012 * readdirplus operation which causes this to be called (from filldir)
2013 * with the glock already held.
2014 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00002015 * Returns: errno
2016 */
2017
Christian Brauner549c7292021-01-21 14:19:43 +01002018static int gfs2_getattr(struct user_namespace *mnt_userns,
2019 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +00002020 u32 request_mask, unsigned int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002021{
David Howellsa528d352017-01-31 16:46:22 +00002022 struct inode *inode = d_inode(path->dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04002023 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002024 struct gfs2_holder gh;
Andreas Gruenbacherb2623c22017-10-09 17:55:58 +02002025 u32 gfsflags;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002026 int error;
2027
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05002028 gfs2_holder_mark_uninitialized(&gh);
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00002029 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06002030 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
2031 if (error)
2032 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002033 }
2034
Andreas Gruenbacherb2623c22017-10-09 17:55:58 +02002035 gfsflags = ip->i_diskflags;
2036 if (gfsflags & GFS2_DIF_APPENDONLY)
2037 stat->attributes |= STATX_ATTR_APPEND;
2038 if (gfsflags & GFS2_DIF_IMMUTABLE)
2039 stat->attributes |= STATX_ATTR_IMMUTABLE;
2040
2041 stat->attributes_mask |= (STATX_ATTR_APPEND |
2042 STATX_ATTR_COMPRESSED |
2043 STATX_ATTR_ENCRYPTED |
2044 STATX_ATTR_IMMUTABLE |
2045 STATX_ATTR_NODUMP);
2046
Christian Brauner0d56a452021-01-21 14:19:30 +01002047 generic_fillattr(&init_user_ns, inode, stat);
Andreas Gruenbacherb2623c22017-10-09 17:55:58 +02002048
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05002049 if (gfs2_holder_initialized(&gh))
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05002050 gfs2_glock_dq_uninit(&gh);
2051
2052 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002053}
2054
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002055static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2056 u64 start, u64 len)
2057{
2058 struct gfs2_inode *ip = GFS2_I(inode);
2059 struct gfs2_holder gh;
2060 int ret;
2061
Bob Petersonaac1a552017-02-16 21:13:54 +01002062 inode_lock_shared(inode);
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002063
2064 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2065 if (ret)
2066 goto out;
2067
Bob Petersonaac1a552017-02-16 21:13:54 +01002068 ret = iomap_fiemap(inode, fieinfo, start, len, &gfs2_iomap_ops);
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002069
2070 gfs2_glock_dq_uninit(&gh);
Bob Petersonaac1a552017-02-16 21:13:54 +01002071
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002072out:
Bob Petersonaac1a552017-02-16 21:13:54 +01002073 inode_unlock_shared(inode);
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002074 return ret;
2075}
2076
Andreas Gruenbacher3a274112017-03-15 19:12:59 +01002077loff_t gfs2_seek_data(struct file *file, loff_t offset)
2078{
2079 struct inode *inode = file->f_mapping->host;
2080 struct gfs2_inode *ip = GFS2_I(inode);
2081 struct gfs2_holder gh;
2082 loff_t ret;
2083
2084 inode_lock_shared(inode);
2085 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2086 if (!ret)
2087 ret = iomap_seek_data(inode, offset, &gfs2_iomap_ops);
2088 gfs2_glock_dq_uninit(&gh);
2089 inode_unlock_shared(inode);
2090
2091 if (ret < 0)
2092 return ret;
2093 return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
2094}
2095
2096loff_t gfs2_seek_hole(struct file *file, loff_t offset)
2097{
2098 struct inode *inode = file->f_mapping->host;
2099 struct gfs2_inode *ip = GFS2_I(inode);
2100 struct gfs2_holder gh;
2101 loff_t ret;
2102
2103 inode_lock_shared(inode);
2104 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2105 if (!ret)
2106 ret = iomap_seek_hole(inode, offset, &gfs2_iomap_ops);
2107 gfs2_glock_dq_uninit(&gh);
2108 inode_unlock_shared(inode);
2109
2110 if (ret < 0)
2111 return ret;
2112 return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
2113}
2114
Andreas Gruenbacher82e938b2020-11-25 23:37:18 +01002115static int gfs2_update_time(struct inode *inode, struct timespec64 *time,
2116 int flags)
2117{
2118 struct gfs2_inode *ip = GFS2_I(inode);
2119 struct gfs2_glock *gl = ip->i_gl;
2120 struct gfs2_holder *gh;
2121 int error;
2122
2123 gh = gfs2_glock_is_locked_by_me(gl);
2124 if (gh && !gfs2_glock_is_held_excl(gl)) {
2125 gfs2_glock_dq(gh);
2126 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, gh);
2127 error = gfs2_glock_nq(gh);
2128 if (error)
2129 return error;
2130 }
2131 return generic_update_time(inode, time, flags);
2132}
2133
Andreas Gruenbachere3a77ee2020-11-25 21:14:15 +01002134static const struct inode_operations gfs2_file_iops = {
Al Viroe6305c42008-07-15 21:03:57 -04002135 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002136 .setattr = gfs2_setattr,
2137 .getattr = gfs2_getattr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002138 .listxattr = gfs2_listxattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002139 .fiemap = gfs2_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02002140 .get_acl = gfs2_get_acl,
Christoph Hellwige01580b2013-12-20 05:16:52 -08002141 .set_acl = gfs2_set_acl,
Andreas Gruenbacher82e938b2020-11-25 23:37:18 +01002142 .update_time = gfs2_update_time,
Miklos Szeredi88b631c2021-04-07 14:36:43 +02002143 .fileattr_get = gfs2_fileattr_get,
2144 .fileattr_set = gfs2_fileattr_set,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002145};
2146
Andreas Gruenbachere3a77ee2020-11-25 21:14:15 +01002147static const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00002148 .create = gfs2_create,
2149 .lookup = gfs2_lookup,
2150 .link = gfs2_link,
2151 .unlink = gfs2_unlink,
2152 .symlink = gfs2_symlink,
2153 .mkdir = gfs2_mkdir,
Steven Whitehouse855d23c2011-05-09 16:42:37 +01002154 .rmdir = gfs2_unlink,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002155 .mknod = gfs2_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02002156 .rename = gfs2_rename2,
Al Viroe6305c42008-07-15 21:03:57 -04002157 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002158 .setattr = gfs2_setattr,
2159 .getattr = gfs2_getattr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002160 .listxattr = gfs2_listxattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002161 .fiemap = gfs2_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02002162 .get_acl = gfs2_get_acl,
Christoph Hellwige01580b2013-12-20 05:16:52 -08002163 .set_acl = gfs2_set_acl,
Andreas Gruenbacher82e938b2020-11-25 23:37:18 +01002164 .update_time = gfs2_update_time,
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01002165 .atomic_open = gfs2_atomic_open,
Miklos Szeredi88b631c2021-04-07 14:36:43 +02002166 .fileattr_get = gfs2_fileattr_get,
2167 .fileattr_set = gfs2_fileattr_set,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002168};
2169
Andreas Gruenbachere3a77ee2020-11-25 21:14:15 +01002170static const struct inode_operations gfs2_symlink_iops = {
Al Viro6b255392015-11-17 10:20:54 -05002171 .get_link = gfs2_get_link,
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,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002177};
2178