blob: 3d77f29703491abd2a0256323778fe3b5daa1bfe [file] [log] [blame]
Thomas Gleixner7336d0e2019-05-31 01:09:56 -07001// SPDX-License-Identifier: GPL-2.0-only
David Teiglandb3b94fa2006-01-16 16:50:04 +00002/*
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehousef2741d92011-05-13 12:11:17 +01004 * Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00005 */
6
David Teiglandb3b94fa2006-01-16 16:50:04 +00007#include <linux/slab.h>
8#include <linux/spinlock.h>
9#include <linux/completion.h>
10#include <linux/buffer_head.h>
11#include <linux/namei.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000012#include <linux/mm.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010013#include <linux/cred.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000014#include <linux/xattr.h>
15#include <linux/posix_acl.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050016#include <linux/gfs2_ondisk.h>
Steven Whitehouse71b86f52006-03-28 14:14:04 -050017#include <linux/crc32.h>
Bob Petersonaac1a552017-02-16 21:13:54 +010018#include <linux/iomap.h>
Steven Whitehouse194c0112011-05-09 14:06:38 +010019#include <linux/security.h>
Christoph Hellwig10c5db22020-05-23 09:30:11 +020020#include <linux/fiemap.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080021#include <linux/uaccess.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000022
23#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050024#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000025#include "acl.h"
26#include "bmap.h"
27#include "dir.h"
Steven Whitehouse307cf6e2009-08-26 18:51:04 +010028#include "xattr.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029#include "glock.h"
30#include "inode.h"
31#include "meta_io.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000032#include "quota.h"
33#include "rgrp.h"
34#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050035#include "util.h"
Steven Whitehouseb2760582008-10-14 16:05:55 +010036#include "super.h"
Steven Whitehouse194c0112011-05-09 14:06:38 +010037#include "glops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000038
Andreas Gruenbachere3a77ee2020-11-25 21:14:15 +010039static const struct inode_operations gfs2_file_iops;
40static const struct inode_operations gfs2_dir_iops;
41static const struct inode_operations gfs2_symlink_iops;
42
Andreas Gruenbachercda9dd42016-06-14 12:24:50 -050043static int iget_test(struct inode *inode, void *opaque)
Steven Whitehouse194c0112011-05-09 14:06:38 +010044{
Andreas Gruenbachercda9dd42016-06-14 12:24:50 -050045 u64 no_addr = *(u64 *)opaque;
46
47 return GFS2_I(inode)->i_no_addr == no_addr;
48}
49
50static int iget_set(struct inode *inode, void *opaque)
51{
52 u64 no_addr = *(u64 *)opaque;
53
54 GFS2_I(inode)->i_no_addr = no_addr;
55 inode->i_ino = no_addr;
56 return 0;
57}
58
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -050059static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
60{
61 struct inode *inode;
62
63repeat:
Andreas Gruenbachercda9dd42016-06-14 12:24:50 -050064 inode = iget5_locked(sb, no_addr, iget_test, iget_set, &no_addr);
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -050065 if (!inode)
66 return inode;
67 if (is_bad_inode(inode)) {
68 iput(inode);
69 goto repeat;
70 }
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -050071 return inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +010072}
73
74/**
75 * gfs2_set_iop - Sets inode operations
76 * @inode: The inode with correct i_mode filled in
77 *
78 * GFS2 lookup code fills in vfs inode contents based on info obtained
79 * from directory entry inside gfs2_inode_lookup().
80 */
81
82static void gfs2_set_iop(struct inode *inode)
83{
84 struct gfs2_sbd *sdp = GFS2_SB(inode);
85 umode_t mode = inode->i_mode;
86
87 if (S_ISREG(mode)) {
88 inode->i_op = &gfs2_file_iops;
89 if (gfs2_localflocks(sdp))
90 inode->i_fop = &gfs2_file_fops_nolock;
91 else
92 inode->i_fop = &gfs2_file_fops;
93 } else if (S_ISDIR(mode)) {
94 inode->i_op = &gfs2_dir_iops;
95 if (gfs2_localflocks(sdp))
96 inode->i_fop = &gfs2_dir_fops_nolock;
97 else
98 inode->i_fop = &gfs2_dir_fops;
99 } else if (S_ISLNK(mode)) {
100 inode->i_op = &gfs2_symlink_iops;
101 } else {
102 inode->i_op = &gfs2_file_iops;
103 init_special_inode(inode, inode->i_mode, inode->i_rdev);
104 }
105}
106
107/**
108 * gfs2_inode_lookup - Lookup an inode
109 * @sb: The super block
Steven Whitehouse194c0112011-05-09 14:06:38 +0100110 * @type: The type of the inode
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500111 * @no_addr: The inode number
112 * @no_formal_ino: The inode generation number
113 * @blktype: Requested block type (GFS2_BLKST_DINODE or GFS2_BLKST_UNLINKED;
Andreas Gruenbacher61b91cf2017-08-01 09:54:33 -0500114 * GFS2_BLKST_FREE to indicate not to verify)
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500115 *
116 * If @type is DT_UNKNOWN, the inode type is fetched from disk.
117 *
118 * If @blktype is anything other than GFS2_BLKST_FREE (which is used as a
119 * placeholder because it doesn't otherwise make sense), the on-disk block type
120 * is verified to be @blktype.
Steven Whitehouse194c0112011-05-09 14:06:38 +0100121 *
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100122 * When @no_formal_ino is non-zero, this function will return ERR_PTR(-ESTALE)
123 * if it detects that @no_formal_ino doesn't match the actual inode generation
124 * number. However, it doesn't always know unless @type is DT_UNKNOWN.
125 *
Steven Whitehouse194c0112011-05-09 14:06:38 +0100126 * Returns: A VFS inode, or an error
127 */
128
129struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500130 u64 no_addr, u64 no_formal_ino,
131 unsigned int blktype)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100132{
133 struct inode *inode;
134 struct gfs2_inode *ip;
135 struct gfs2_glock *io_gl = NULL;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500136 struct gfs2_holder i_gh;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100137 int error;
138
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500139 gfs2_holder_mark_uninitialized(&i_gh);
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500140 inode = gfs2_iget(sb, no_addr);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100141 if (!inode)
Steven Whitehouseac3beb62014-01-16 10:31:13 +0000142 return ERR_PTR(-ENOMEM);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100143
Bob Petersone97321f2016-04-12 16:14:26 -0400144 ip = GFS2_I(inode);
Bob Petersone97321f2016-04-12 16:14:26 -0400145
Steven Whitehouse194c0112011-05-09 14:06:38 +0100146 if (inode->i_state & I_NEW) {
147 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100148
149 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
150 if (unlikely(error))
151 goto fail;
Andreas Gruenbacher4fd1a572017-06-30 07:47:15 -0500152 flush_delayed_work(&ip->i_gl->gl_work);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100153
154 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
155 if (unlikely(error))
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100156 goto fail;
Andreas Gruenbacherdd0ecf542020-11-30 16:07:25 +0100157 if (blktype != GFS2_BLKST_UNLINKED)
158 gfs2_cancel_delete_work(io_gl);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100159
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500160 if (type == DT_UNKNOWN || blktype != GFS2_BLKST_FREE) {
161 /*
162 * The GL_SKIP flag indicates to skip reading the inode
163 * block. We read the inode with gfs2_inode_refresh
164 * after possibly checking the block type.
165 */
166 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE,
167 GL_SKIP, &i_gh);
168 if (error)
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100169 goto fail;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500170
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100171 error = -ESTALE;
172 if (no_formal_ino &&
173 gfs2_inode_already_deleted(ip->i_gl, no_formal_ino))
174 goto fail;
175
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500176 if (blktype != GFS2_BLKST_FREE) {
177 error = gfs2_check_blk_type(sdp, no_addr,
178 blktype);
179 if (error)
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100180 goto fail;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500181 }
182 }
183
Bob Peterson4d7c18c2017-07-18 12:15:01 -0500184 glock_set_object(ip->i_gl, ip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100185 set_bit(GIF_INVALID, &ip->i_flags);
186 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
187 if (unlikely(error))
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100188 goto fail;
Andreas Gruenbacher4fd1a572017-06-30 07:47:15 -0500189 glock_set_object(ip->i_iopen_gh.gh_gl, ip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100190 gfs2_glock_put(io_gl);
191 io_gl = NULL;
192
Andreas Gruenbacher2b0fb352020-01-15 06:26:00 +0100193 /* Lowest possible timestamp; will be overwritten in gfs2_dinode_in. */
194 inode->i_atime.tv_sec = 1LL << (8 * sizeof(inode->i_atime.tv_sec) - 1);
195 inode->i_atime.tv_nsec = 0;
196
Steven Whitehouse194c0112011-05-09 14:06:38 +0100197 if (type == DT_UNKNOWN) {
198 /* Inode glock must be locked already */
199 error = gfs2_inode_refresh(GFS2_I(inode));
200 if (error)
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100201 goto fail;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100202 } else {
Andreas Gruenbacher2b0fb352020-01-15 06:26:00 +0100203 ip->i_no_formal_ino = no_formal_ino;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100204 inode->i_mode = DT2IF(type);
205 }
206
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100207 if (gfs2_holder_initialized(&i_gh))
208 gfs2_glock_dq_uninit(&i_gh);
Andreas Gruenbacher332f51d2016-09-26 13:24:34 -0500209
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100210 gfs2_set_iop(inode);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100211 }
212
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100213 if (no_formal_ino && ip->i_no_formal_ino &&
214 no_formal_ino != ip->i_no_formal_ino) {
Andreas Gruenbacher5902f4d2020-06-09 14:33:11 +0200215 error = -ESTALE;
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100216 if (inode->i_state & I_NEW)
217 goto fail;
218 iput(inode);
Andreas Gruenbacher5902f4d2020-06-09 14:33:11 +0200219 return ERR_PTR(error);
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100220 }
221
222 if (inode->i_state & I_NEW)
223 unlock_new_inode(inode);
224
Steven Whitehouse194c0112011-05-09 14:06:38 +0100225 return inode;
226
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100227fail:
Steven Whitehouse194c0112011-05-09 14:06:38 +0100228 if (io_gl)
229 gfs2_glock_put(io_gl);
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500230 if (gfs2_holder_initialized(&i_gh))
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500231 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100232 iget_failed(inode);
233 return ERR_PTR(error);
234}
235
Andreas Gruenbacher6bdcade2020-01-15 05:31:38 +0100236/**
237 * gfs2_lookup_by_inum - look up an inode by inode number
238 * @sdp: The super block
239 * @no_addr: The inode number
240 * @no_formal_ino: The inode generation number (0 for any)
241 * @blktype: Requested block type (see gfs2_inode_lookup)
242 */
Steven Whitehouse194c0112011-05-09 14:06:38 +0100243struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
Andreas Gruenbacher6bdcade2020-01-15 05:31:38 +0100244 u64 no_formal_ino, unsigned int blktype)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100245{
246 struct super_block *sb = sdp->sd_vfs;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500247 struct inode *inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100248 int error;
249
Andreas Gruenbacherb66648a2020-01-15 06:21:42 +0100250 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, no_formal_ino,
251 blktype);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100252 if (IS_ERR(inode))
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500253 return inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100254
Steven Whitehouse194c0112011-05-09 14:06:38 +0100255 if (no_formal_ino) {
Steven Whitehouse194c0112011-05-09 14:06:38 +0100256 error = -EIO;
257 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
258 goto fail_iput;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100259 }
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500260 return inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100261
Steven Whitehouse194c0112011-05-09 14:06:38 +0100262fail_iput:
263 iput(inode);
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500264 return ERR_PTR(error);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100265}
266
267
268struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
269{
270 struct qstr qstr;
271 struct inode *inode;
272 gfs2_str2qstr(&qstr, name);
273 inode = gfs2_lookupi(dip, &qstr, 1);
274 /* gfs2_lookupi has inconsistent callers: vfs
275 * related routines expect NULL for no entry found,
276 * gfs2_lookup_simple callers expect ENOENT
277 * and do not check for NULL.
278 */
279 if (inode == NULL)
280 return ERR_PTR(-ENOENT);
281 else
282 return inode;
283}
284
285
286/**
287 * gfs2_lookupi - Look up a filename in a directory and return its inode
288 * @d_gh: An initialized holder for the directory glock
289 * @name: The name of the inode to look for
290 * @is_root: If 1, ignore the caller's permissions
291 * @i_gh: An uninitialized holder for the new inode glock
292 *
293 * This can be called via the VFS filldir function when NFS is doing
294 * a readdirplus and the inode which its intending to stat isn't
295 * already in cache. In this case we must not take the directory glock
296 * again, since the readdir call will have already taken that lock.
297 *
298 * Returns: errno
299 */
300
301struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
302 int is_root)
303{
304 struct super_block *sb = dir->i_sb;
305 struct gfs2_inode *dip = GFS2_I(dir);
306 struct gfs2_holder d_gh;
307 int error = 0;
308 struct inode *inode = NULL;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100309
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500310 gfs2_holder_mark_uninitialized(&d_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100311 if (!name->len || name->len > GFS2_FNAMESIZE)
312 return ERR_PTR(-ENAMETOOLONG);
313
314 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
315 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
David Howells2b0143b2015-03-17 22:25:59 +0000316 dir == d_inode(sb->s_root))) {
Steven Whitehouse194c0112011-05-09 14:06:38 +0100317 igrab(dir);
318 return dir;
319 }
320
321 if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
322 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
323 if (error)
324 return ERR_PTR(error);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100325 }
326
327 if (!is_root) {
Al Viro10556cb22011-06-20 19:28:19 -0400328 error = gfs2_permission(dir, MAY_EXEC);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100329 if (error)
330 goto out;
331 }
332
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100333 inode = gfs2_dir_search(dir, name, false);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100334 if (IS_ERR(inode))
335 error = PTR_ERR(inode);
336out:
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500337 if (gfs2_holder_initialized(&d_gh))
Steven Whitehouse194c0112011-05-09 14:06:38 +0100338 gfs2_glock_dq_uninit(&d_gh);
339 if (error == -ENOENT)
340 return NULL;
341 return inode ? inode : ERR_PTR(error);
342}
343
344/**
345 * create_ok - OK to create a new on-disk inode here?
346 * @dip: Directory in which dinode is to be created
347 * @name: Name of new dinode
348 * @mode:
349 *
350 * Returns: errno
351 */
352
353static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
Al Viro175a4eb2011-07-26 03:30:54 -0400354 umode_t mode)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100355{
356 int error;
357
Al Viro10556cb22011-06-20 19:28:19 -0400358 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100359 if (error)
360 return error;
361
362 /* Don't create entries in an unlinked directory */
363 if (!dip->i_inode.i_nlink)
364 return -ENOENT;
365
Steven Whitehouse194c0112011-05-09 14:06:38 +0100366 if (dip->i_entries == (u32)-1)
367 return -EFBIG;
368 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
369 return -EMLINK;
370
371 return 0;
372}
373
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000374static void munge_mode_uid_gid(const struct gfs2_inode *dip,
375 struct inode *inode)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100376{
377 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800378 (dip->i_inode.i_mode & S_ISUID) &&
379 !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000380 if (S_ISDIR(inode->i_mode))
381 inode->i_mode |= S_ISUID;
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800382 else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000383 inode->i_mode &= ~07111;
384 inode->i_uid = dip->i_inode.i_uid;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100385 } else
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000386 inode->i_uid = current_fsuid();
Steven Whitehouse194c0112011-05-09 14:06:38 +0100387
388 if (dip->i_inode.i_mode & S_ISGID) {
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000389 if (S_ISDIR(inode->i_mode))
390 inode->i_mode |= S_ISGID;
391 inode->i_gid = dip->i_inode.i_gid;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100392 } else
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000393 inode->i_gid = current_fsgid();
Steven Whitehouse194c0112011-05-09 14:06:38 +0100394}
395
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000396static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100397{
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000398 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000399 struct gfs2_alloc_parms ap = { .target = *dblocks, .aflags = flags, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100400 int error;
401
Abhi Dasb8fbf472015-03-18 12:03:41 -0500402 error = gfs2_quota_lock_check(ip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100403 if (error)
404 goto out;
405
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100406 error = gfs2_inplace_reserve(ip, &ap);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000407 if (error)
408 goto out_quota;
409
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000410 error = gfs2_trans_begin(sdp, (*dblocks * RES_RG_BIT) + RES_STATFS + RES_QUOTA, 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100411 if (error)
412 goto out_ipreserv;
413
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000414 error = gfs2_alloc_blocks(ip, &ip->i_no_addr, dblocks, 1, &ip->i_generation);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000415 ip->i_no_formal_ino = ip->i_generation;
416 ip->i_inode.i_ino = ip->i_no_addr;
417 ip->i_goal = ip->i_no_addr;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100418
419 gfs2_trans_end(sdp);
420
421out_ipreserv:
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000422 gfs2_inplace_release(ip);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000423out_quota:
424 gfs2_quota_unlock(ip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100425out:
Steven Whitehouse194c0112011-05-09 14:06:38 +0100426 return error;
427}
428
Steven Whitehousef2741d92011-05-13 12:11:17 +0100429static void gfs2_init_dir(struct buffer_head *dibh,
430 const struct gfs2_inode *parent)
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100431{
432 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
433 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
434
435 gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
436 dent->de_inum = di->di_num; /* already GFS2 endian */
437 dent->de_type = cpu_to_be16(DT_DIR);
438
439 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
440 gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
441 gfs2_inum_out(parent, dent);
442 dent->de_type = cpu_to_be16(DT_DIR);
443
444}
445
Steven Whitehouse194c0112011-05-09 14:06:38 +0100446/**
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000447 * gfs2_init_xattr - Initialise an xattr block for a new inode
448 * @ip: The inode in question
449 *
450 * This sets up an empty xattr block for a new inode, ready to
451 * take any ACLs, LSM xattrs, etc.
452 */
453
454static void gfs2_init_xattr(struct gfs2_inode *ip)
455{
456 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
457 struct buffer_head *bh;
458 struct gfs2_ea_header *ea;
459
460 bh = gfs2_meta_new(ip->i_gl, ip->i_eattr);
461 gfs2_trans_add_meta(ip->i_gl, bh);
462 gfs2_metatype_set(bh, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
463 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
464
465 ea = GFS2_EA_BH2FIRST(bh);
466 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
467 ea->ea_type = GFS2_EATYPE_UNUSED;
468 ea->ea_flags = GFS2_EAFLAG_LAST;
469
470 brelse(bh);
471}
472
473/**
Steven Whitehouse194c0112011-05-09 14:06:38 +0100474 * init_dinode - Fill in a new dinode structure
Steven Whitehousef2741d92011-05-13 12:11:17 +0100475 * @dip: The directory this inode is being created in
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000476 * @ip: The inode
Steven Whitehousef2741d92011-05-13 12:11:17 +0100477 * @symname: The symlink destination (if a symlink)
Steven Whitehousef2741d92011-05-13 12:11:17 +0100478 * @bhp: The buffer head (returned to caller)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100479 *
480 */
481
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000482static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000483 const char *symname)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100484{
Steven Whitehouse194c0112011-05-09 14:06:38 +0100485 struct gfs2_dinode *di;
486 struct buffer_head *dibh;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100487
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000488 dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000489 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100490 di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000491 gfs2_dinode_out(ip, di);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100492
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000493 di->di_major = cpu_to_be32(MAJOR(ip->i_inode.i_rdev));
494 di->di_minor = cpu_to_be32(MINOR(ip->i_inode.i_rdev));
Steven Whitehouse194c0112011-05-09 14:06:38 +0100495 di->__pad1 = 0;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100496 di->__pad2 = 0;
497 di->__pad3 = 0;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100498 memset(&di->__pad4, 0, sizeof(di->__pad4));
Steven Whitehouse194c0112011-05-09 14:06:38 +0100499 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000500 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouse160b4022011-05-13 10:34:59 +0100501
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000502 switch(ip->i_inode.i_mode & S_IFMT) {
Steven Whitehouse160b4022011-05-13 10:34:59 +0100503 case S_IFDIR:
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100504 gfs2_init_dir(dibh, dip);
Steven Whitehouse160b4022011-05-13 10:34:59 +0100505 break;
506 case S_IFLNK:
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000507 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
Steven Whitehouse160b4022011-05-13 10:34:59 +0100508 break;
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100509 }
510
Steven Whitehouse194c0112011-05-09 14:06:38 +0100511 set_buffer_uptodate(dibh);
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000512 brelse(dibh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100513}
514
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000515/**
516 * gfs2_trans_da_blocks - Calculate number of blocks to link inode
517 * @dip: The directory we are linking into
518 * @da: The dir add information
519 * @nr_inodes: The number of inodes involved
520 *
521 * This calculate the number of blocks we need to reserve in a
522 * transaction to link @nr_inodes into a directory. In most cases
523 * @nr_inodes will be 2 (the directory plus the inode being linked in)
524 * but in case of rename, 4 may be required.
525 *
526 * Returns: Number of blocks
527 */
528
529static unsigned gfs2_trans_da_blks(const struct gfs2_inode *dip,
530 const struct gfs2_diradd *da,
531 unsigned nr_inodes)
532{
533 return da->nr_blocks + gfs2_rg_blocks(dip, da->nr_blocks) +
534 (nr_inodes * RES_DINODE) + RES_QUOTA + RES_STATFS;
535}
536
Steven Whitehouse194c0112011-05-09 14:06:38 +0100537static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000538 struct gfs2_inode *ip, struct gfs2_diradd *da)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100539{
540 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000541 struct gfs2_alloc_parms ap = { .target = da->nr_blocks, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100542 int error;
543
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000544 if (da->nr_blocks) {
Abhi Dasb8fbf472015-03-18 12:03:41 -0500545 error = gfs2_quota_lock_check(dip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100546 if (error)
547 goto fail_quota_locks;
548
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100549 error = gfs2_inplace_reserve(dip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100550 if (error)
551 goto fail_quota_locks;
552
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000553 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100554 if (error)
555 goto fail_ipreserv;
556 } else {
557 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
558 if (error)
559 goto fail_quota_locks;
560 }
561
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000562 error = gfs2_dir_add(&dip->i_inode, name, ip, da);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100563
Steven Whitehouse194c0112011-05-09 14:06:38 +0100564 gfs2_trans_end(sdp);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100565fail_ipreserv:
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000566 gfs2_inplace_release(dip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100567fail_quota_locks:
568 gfs2_quota_unlock(dip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100569 return error;
570}
571
H Hartley Sweeten46cc1e52011-09-23 15:51:32 -0700572static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -0400573 void *fs_info)
574{
575 const struct xattr *xattr;
576 int err = 0;
577
578 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
579 err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
580 xattr->value_len, 0,
581 GFS2_EATYPE_SECURITY);
582 if (err < 0)
583 break;
584 }
585 return err;
586}
587
Steven Whitehouse194c0112011-05-09 14:06:38 +0100588/**
Steven Whitehousef2741d92011-05-13 12:11:17 +0100589 * gfs2_create_inode - Create a new inode
590 * @dir: The parent directory
591 * @dentry: The new dentry
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100592 * @file: If non-NULL, the file which is being opened
Steven Whitehousef2741d92011-05-13 12:11:17 +0100593 * @mode: The permissions on the new inode
594 * @dev: For device nodes, this is the device number
595 * @symname: For symlinks, this is the link destination
596 * @size: The initial size of the inode (ignored for directories)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100597 *
Steven Whitehousef2741d92011-05-13 12:11:17 +0100598 * Returns: 0 on success, or error code
Steven Whitehouse194c0112011-05-09 14:06:38 +0100599 */
600
Steven Whitehousef2741d92011-05-13 12:11:17 +0100601static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100602 struct file *file,
Al Viro175a4eb2011-07-26 03:30:54 -0400603 umode_t mode, dev_t dev, const char *symname,
Al Virob452a452018-06-08 13:06:28 -0400604 unsigned int size, int excl)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100605{
Steven Whitehousef2741d92011-05-13 12:11:17 +0100606 const struct qstr *name = &dentry->d_name;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800607 struct posix_acl *default_acl, *acl;
Steven Whitehousef2741d92011-05-13 12:11:17 +0100608 struct gfs2_holder ghs[2];
Steven Whitehouse194c0112011-05-09 14:06:38 +0100609 struct inode *inode = NULL;
Bob Peterson8e2e0042012-07-19 08:12:40 -0400610 struct gfs2_inode *dip = GFS2_I(dir), *ip;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100611 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Bob Petersona4923862015-12-07 16:24:27 -0600612 struct gfs2_glock *io_gl = NULL;
Bob Peterson783013c2015-12-04 10:19:14 -0600613 int error, free_vfs_inode = 1;
Steven Whitehouse9dbe9612012-10-31 10:37:10 +0000614 u32 aflags = 0;
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000615 unsigned blocks = 1;
Bob Peterson19aeb5a62014-09-29 08:52:04 -0400616 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100617
618 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousef2741d92011-05-13 12:11:17 +0100619 return -ENAMETOOLONG;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100620
Bob Peterson2fba46a2020-02-27 12:47:53 -0600621 error = gfs2_qa_get(dip);
Bob Peterson0a305e42012-06-06 11:17:59 +0100622 if (error)
623 return error;
624
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000625 error = gfs2_rindex_update(sdp);
626 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -0600627 goto fail;
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000628
Steven Whitehousef2741d92011-05-13 12:11:17 +0100629 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100630 if (error)
631 goto fail;
Andreas Gruenbachere0b62e22017-06-30 08:16:46 -0500632 gfs2_holder_mark_uninitialized(ghs + 1);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100633
634 error = create_ok(dip, name, mode);
635 if (error)
636 goto fail_gunlock;
637
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100638 inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
639 error = PTR_ERR(inode);
640 if (!IS_ERR(inode)) {
Al Viro571a4b52014-11-19 19:34:49 +0000641 if (S_ISDIR(inode->i_mode)) {
642 iput(inode);
643 inode = ERR_PTR(-EISDIR);
644 goto fail_gunlock;
645 }
Al Viro44bb31b2014-11-19 19:35:24 +0000646 d_instantiate(dentry, inode);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100647 error = 0;
Miklos Szeredi0d0d1102013-09-16 14:52:00 +0200648 if (file) {
Al Viro44bb31b2014-11-19 19:35:24 +0000649 if (S_ISREG(inode->i_mode))
Al Virobe12af32018-06-08 11:44:56 -0400650 error = finish_open(file, dentry, gfs2_open_common);
Al Viro44bb31b2014-11-19 19:35:24 +0000651 else
652 error = finish_no_open(file, NULL);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100653 }
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100654 gfs2_glock_dq_uninit(ghs);
Bob Peterson2297ab62020-05-04 10:18:43 -0500655 goto fail;
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100656 } else if (error != -ENOENT) {
657 goto fail_gunlock;
658 }
659
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000660 error = gfs2_diradd_alloc_required(dir, name, &da);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000661 if (error < 0)
662 goto fail_gunlock;
663
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000664 inode = new_inode(sdp->sd_vfs);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000665 error = -ENOMEM;
666 if (!inode)
667 goto fail_gunlock;
668
Christoph Hellwige01580b2013-12-20 05:16:52 -0800669 error = posix_acl_create(dir, &mode, &default_acl, &acl);
670 if (error)
Bob Peterson783013c2015-12-04 10:19:14 -0600671 goto fail_gunlock;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800672
Bob Peterson8e2e0042012-07-19 08:12:40 -0400673 ip = GFS2_I(inode);
Bob Peterson2fba46a2020-02-27 12:47:53 -0600674 error = gfs2_qa_get(ip);
Steven Whitehouseff7f4cb2012-09-10 10:03:50 +0100675 if (error)
Christoph Hellwige01580b2013-12-20 05:16:52 -0800676 goto fail_free_acls;
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000677
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000678 inode->i_mode = mode;
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000679 set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000680 inode->i_rdev = dev;
681 inode->i_size = size;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700682 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000683 munge_mode_uid_gid(dip, inode);
Abhi Das00a158b2014-09-18 21:40:28 -0500684 check_and_update_goal(dip);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000685 ip->i_goal = dip->i_goal;
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000686 ip->i_diskflags = 0;
687 ip->i_eattr = 0;
688 ip->i_height = 0;
689 ip->i_depth = 0;
690 ip->i_entries = 0;
Bob Petersoncc963a12017-03-16 15:29:13 -0400691 ip->i_no_addr = 0; /* Temporarily zero until real addr is assigned */
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000692
693 switch(mode & S_IFMT) {
694 case S_IFREG:
695 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
696 gfs2_tune_get(sdp, gt_new_files_jdata))
697 ip->i_diskflags |= GFS2_DIF_JDATA;
698 gfs2_set_aops(inode);
699 break;
700 case S_IFDIR:
701 ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA);
702 ip->i_diskflags |= GFS2_DIF_JDATA;
703 ip->i_entries = 2;
704 break;
705 }
Abhi Dasacc546f2015-11-10 15:07:26 -0600706
707 /* Force SYSTEM flag on all files and subdirs of a SYSTEM directory */
708 if (dip->i_diskflags & GFS2_DIF_SYSTEM)
709 ip->i_diskflags |= GFS2_DIF_SYSTEM;
710
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000711 gfs2_set_inode_flags(inode);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000712
David Howells2b0143b2015-03-17 22:25:59 +0000713 if ((GFS2_I(d_inode(sdp->sd_root_dir)) == dip) ||
Steven Whitehouse9dbe9612012-10-31 10:37:10 +0000714 (dip->i_diskflags & GFS2_DIF_TOPDIR))
715 aflags |= GFS2_AF_ORLOV;
716
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000717 if (default_acl || acl)
718 blocks++;
719
720 error = alloc_dinode(ip, aflags, &blocks);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000721 if (error)
722 goto fail_free_inode;
723
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000724 gfs2_set_inode_blocks(inode, blocks);
725
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000726 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
727 if (error)
728 goto fail_free_inode;
Andreas Gruenbacher98e5a912017-07-19 11:10:19 -0500729 flush_delayed_work(&ip->i_gl->gl_work);
Andreas Gruenbacher6f6597ba2017-06-30 07:55:08 -0500730 glock_set_object(ip->i_gl, ip);
Andreas Gruenbacher98e5a912017-07-19 11:10:19 -0500731
Andreas Gruenbacherdd0ecf542020-11-30 16:07:25 +0100732 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000733 if (error)
734 goto fail_free_inode;
Andreas Gruenbacherdd0ecf542020-11-30 16:07:25 +0100735 gfs2_cancel_delete_work(io_gl);
736 glock_set_object(io_gl, ip);
737
738 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
739 if (error)
740 goto fail_gunlock2;
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000741
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000742 error = gfs2_trans_begin(sdp, blocks, 0);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000743 if (error)
Andreas Gruenbacherdd0ecf542020-11-30 16:07:25 +0100744 goto fail_gunlock2;
Bob Peterson0a305e42012-06-06 11:17:59 +0100745
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000746 if (blocks > 1) {
747 ip->i_eattr = ip->i_no_addr + 1;
748 gfs2_init_xattr(ip);
749 }
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000750 init_dinode(dip, ip, symname);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000751 gfs2_trans_end(sdp);
752
Bob Petersona4923862015-12-07 16:24:27 -0600753 BUG_ON(test_and_set_bit(GLF_INODE_CREATING, &io_gl->gl_flags));
754
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000755 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
756 if (error)
757 goto fail_gunlock2;
758
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000759 gfs2_set_iop(inode);
760 insert_inode_hash(inode);
761
Bob Peterson783013c2015-12-04 10:19:14 -0600762 free_vfs_inode = 0; /* After this point, the inode is no longer
763 considered free. Any failures need to undo
764 the gfs2 structures. */
Christoph Hellwige01580b2013-12-20 05:16:52 -0800765 if (default_acl) {
Al Viro1a39ba92016-05-13 03:59:17 +0200766 error = __gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100767 if (error)
768 goto fail_gunlock3;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800769 posix_acl_release(default_acl);
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100770 default_acl = NULL;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800771 }
772 if (acl) {
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100773 error = __gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS);
774 if (error)
775 goto fail_gunlock3;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800776 posix_acl_release(acl);
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100777 acl = NULL;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800778 }
779
Bob Petersonf45dc262014-03-19 09:37:00 -0400780 error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name,
781 &gfs2_initxattrs, NULL);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100782 if (error)
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000783 goto fail_gunlock3;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100784
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000785 error = link_dinode(dip, name, ip, &da);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100786 if (error)
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000787 goto fail_gunlock3;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100788
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000789 mark_inode_dirty(inode);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100790 d_instantiate(dentry, inode);
Bob Peterson2c47c1b2019-11-19 11:40:46 -0500791 /* After instantiate, errors should result in evict which will destroy
792 * both inode and iopen glocks properly. */
Miklos Szeredic5bf8fe2013-09-16 14:52:03 +0200793 if (file) {
Al Viro73a09dd2018-06-08 13:22:02 -0400794 file->f_mode |= FMODE_CREATED;
Al Virobe12af32018-06-08 11:44:56 -0400795 error = finish_open(file, dentry, gfs2_open_common);
Miklos Szeredic5bf8fe2013-09-16 14:52:03 +0200796 }
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000797 gfs2_glock_dq_uninit(ghs);
Bob Peterson2297ab62020-05-04 10:18:43 -0500798 gfs2_qa_put(ip);
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000799 gfs2_glock_dq_uninit(ghs + 1);
Bob Petersona4923862015-12-07 16:24:27 -0600800 clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
Bob Peterson2c47c1b2019-11-19 11:40:46 -0500801 gfs2_glock_put(io_gl);
Bob Peterson2297ab62020-05-04 10:18:43 -0500802 gfs2_qa_put(dip);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100803 return error;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100804
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000805fail_gunlock3:
Bob Peterson9c1b2802017-07-18 12:26:07 -0500806 glock_clear_object(io_gl, ip);
Bob Peterson783013c2015-12-04 10:19:14 -0600807 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100808fail_gunlock2:
Bob Peterson2c47c1b2019-11-19 11:40:46 -0500809 clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
Andreas Gruenbacherdd0ecf542020-11-30 16:07:25 +0100810 glock_clear_object(io_gl, ip);
Bob Peterson2c47c1b2019-11-19 11:40:46 -0500811 gfs2_glock_put(io_gl);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000812fail_free_inode:
Bob Peterson9c1b2802017-07-18 12:26:07 -0500813 if (ip->i_gl) {
814 glock_clear_object(ip->i_gl, ip);
Bob Peterson1a0b00d2020-06-01 11:37:09 -0400815 if (free_vfs_inode) /* else evict will do the put for us */
816 gfs2_glock_put(ip->i_gl);
Bob Peterson9c1b2802017-07-18 12:26:07 -0500817 }
Andreas Gruenbacher15955482020-03-06 10:32:35 -0600818 gfs2_rs_delete(ip, NULL);
819 gfs2_qa_put(ip);
Christoph Hellwige01580b2013-12-20 05:16:52 -0800820fail_free_acls:
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100821 posix_acl_release(default_acl);
822 posix_acl_release(acl);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100823fail_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000824 gfs2_dir_no_add(&da);
Steven Whitehousef2741d92011-05-13 12:11:17 +0100825 gfs2_glock_dq_uninit(ghs);
Kefeng Wang15a798f2019-06-05 22:24:24 +0800826 if (!IS_ERR_OR_NULL(inode)) {
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000827 clear_nlink(inode);
Abhi Das05978802014-03-31 10:33:17 -0500828 if (!free_vfs_inode)
829 mark_inode_dirty(inode);
830 set_bit(free_vfs_inode ? GIF_FREE_VFS_INODE : GIF_ALLOC_FAILED,
831 &GFS2_I(inode)->i_flags);
Steven Whitehouse40ac2182011-08-02 13:17:27 +0100832 iput(inode);
833 }
Andreas Gruenbachere0b62e22017-06-30 08:16:46 -0500834 if (gfs2_holder_initialized(ghs + 1))
835 gfs2_glock_dq_uninit(ghs + 1);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100836fail:
Bob Peterson2fba46a2020-02-27 12:47:53 -0600837 gfs2_qa_put(dip);
Steven Whitehousef2741d92011-05-13 12:11:17 +0100838 return error;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100839}
Steven Whitehousef2741d92011-05-13 12:11:17 +0100840
David Teiglandb3b94fa2006-01-16 16:50:04 +0000841/**
842 * gfs2_create - Create a file
843 * @dir: The directory in which to create the file
844 * @dentry: The dentry of the new file
845 * @mode: The mode of the new file
846 *
847 * Returns: errno
848 */
849
850static int gfs2_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -0400851 umode_t mode, bool excl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000852{
Al Virob452a452018-06-08 13:06:28 -0400853 return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000854}
855
856/**
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100857 * __gfs2_lookup - Look up a filename in a directory and return its inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000858 * @dir: The directory inode
859 * @dentry: The dentry of the new inode
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100860 * @file: File to be opened
David Teiglandb3b94fa2006-01-16 16:50:04 +0000861 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000862 *
863 * Returns: errno
864 */
865
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100866static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry,
Al Virob452a452018-06-08 13:06:28 -0400867 struct file *file)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000868{
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100869 struct inode *inode;
870 struct dentry *d;
871 struct gfs2_holder gh;
872 struct gfs2_glock *gl;
873 int error;
874
875 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
Benjamin Coddington7b7a9112014-09-10 14:09:20 -0400876 if (inode == NULL) {
877 d_add(dentry, NULL);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100878 return NULL;
Benjamin Coddington7b7a9112014-09-10 14:09:20 -0400879 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100880 if (IS_ERR(inode))
881 return ERR_CAST(inode);
882
883 gl = GFS2_I(inode)->i_gl;
884 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
885 if (error) {
886 iput(inode);
887 return ERR_PTR(error);
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000888 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100889
890 d = d_splice_alias(inode, dentry);
J. Bruce Fieldsd57b9c92014-01-16 13:51:07 -0500891 if (IS_ERR(d)) {
J. Bruce Fieldsd57b9c92014-01-16 13:51:07 -0500892 gfs2_glock_dq_uninit(&gh);
893 return d;
894 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100895 if (file && S_ISREG(inode->i_mode))
Al Virobe12af32018-06-08 11:44:56 -0400896 error = finish_open(file, dentry, gfs2_open_common);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100897
898 gfs2_glock_dq_uninit(&gh);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +0100899 if (error) {
900 dput(d);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100901 return ERR_PTR(error);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +0100902 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100903 return d;
904}
905
906static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
907 unsigned flags)
908{
Al Virob452a452018-06-08 13:06:28 -0400909 return __gfs2_lookup(dir, dentry, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000910}
911
912/**
913 * gfs2_link - Link to a file
914 * @old_dentry: The inode to link
915 * @dir: Add link to this directory
916 * @dentry: The name of the link
917 *
918 * Link the inode in "old_dentry" into the directory "dir" with the
919 * name in "dentry".
920 *
921 * Returns: errno
922 */
923
924static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
925 struct dentry *dentry)
926{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400927 struct gfs2_inode *dip = GFS2_I(dir);
928 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000929 struct inode *inode = d_inode(old_dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400930 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000931 struct gfs2_holder ghs[2];
Steven Whitehouse2baee032011-05-09 12:08:36 +0100932 struct buffer_head *dibh;
Bob Peterson19aeb5a62014-09-29 08:52:04 -0400933 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000934 int error;
935
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500936 if (S_ISDIR(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000937 return -EPERM;
938
Bob Peterson2fba46a2020-02-27 12:47:53 -0600939 error = gfs2_qa_get(dip);
Bob Peterson0a305e42012-06-06 11:17:59 +0100940 if (error)
941 return error;
942
David Teiglandb3b94fa2006-01-16 16:50:04 +0000943 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
944 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
945
Bob Peterson72dbf472008-08-12 13:39:29 -0500946 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000947 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500948 goto out_parent;
949
950 error = gfs2_glock_nq(ghs + 1); /* child */
951 if (error)
952 goto out_child;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000953
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100954 error = -ENOENT;
955 if (inode->i_nlink == 0)
956 goto out_gunlock;
957
Al Viro10556cb22011-06-20 19:28:19 -0400958 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000959 if (error)
960 goto out_gunlock;
961
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100962 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000963 switch (error) {
964 case -ENOENT:
965 break;
966 case 0:
967 error = -EEXIST;
968 default:
969 goto out_gunlock;
970 }
971
972 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500973 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000974 goto out_gunlock;
975 error = -EFBIG;
Steven Whitehousead6203f22008-11-03 13:59:19 +0000976 if (dip->i_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000977 goto out_gunlock;
978 error = -EPERM;
979 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
980 goto out_gunlock;
981 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500982 if (!ip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000983 goto out_gunlock;
984 error = -EMLINK;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500985 if (ip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000986 goto out_gunlock;
987
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000988 error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da);
Steven Whitehousec7526662006-03-20 12:30:04 -0500989 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000990 goto out_gunlock;
991
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000992 if (da.nr_blocks) {
993 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
Abhi Dasb8fbf472015-03-18 12:03:41 -0500994 error = gfs2_quota_lock_check(dip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000995 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -0400996 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000997
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100998 error = gfs2_inplace_reserve(dip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000999 if (error)
1000 goto out_gunlock_q;
1001
Steven Whitehouse534cf9c2014-01-06 12:03:05 +00001002 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001003 if (error)
1004 goto out_ipres;
1005 } else {
1006 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
1007 if (error)
1008 goto out_ipres;
1009 }
1010
Steven Whitehouse2baee032011-05-09 12:08:36 +01001011 error = gfs2_meta_inode_buffer(ip, &dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001012 if (error)
1013 goto out_end_trans;
1014
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001015 error = gfs2_dir_add(dir, &dentry->d_name, ip, &da);
Steven Whitehouse2baee032011-05-09 12:08:36 +01001016 if (error)
1017 goto out_brelse;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001018
Steven Whitehouse350a9b02012-12-14 12:36:02 +00001019 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse2baee032011-05-09 12:08:36 +01001020 inc_nlink(&ip->i_inode);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001021 ip->i_inode.i_ctime = current_time(&ip->i_inode);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001022 ihold(inode);
1023 d_instantiate(dentry, inode);
1024 mark_inode_dirty(inode);
Steven Whitehouse2baee032011-05-09 12:08:36 +01001025
1026out_brelse:
1027 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001028out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001029 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001030out_ipres:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001031 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001032 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001033out_gunlock_q:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001034 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001035 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001036out_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001037 gfs2_dir_no_add(&da);
Bob Peterson72dbf472008-08-12 13:39:29 -05001038 gfs2_glock_dq(ghs + 1);
1039out_child:
1040 gfs2_glock_dq(ghs);
1041out_parent:
Bob Peterson2297ab62020-05-04 10:18:43 -05001042 gfs2_qa_put(dip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001043 gfs2_holder_uninit(ghs);
1044 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001045 return error;
1046}
1047
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001048/*
1049 * gfs2_unlink_ok - check to see that a inode is still in a directory
1050 * @dip: the directory
1051 * @name: the name of the file
1052 * @ip: the inode
1053 *
1054 * Assumes that the lock on (at least) @dip is held.
1055 *
1056 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1057 */
1058
1059static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
1060 const struct gfs2_inode *ip)
1061{
1062 int error;
1063
1064 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1065 return -EPERM;
1066
1067 if ((dip->i_inode.i_mode & S_ISVTX) &&
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001068 !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
1069 !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001070 return -EPERM;
1071
1072 if (IS_APPEND(&dip->i_inode))
1073 return -EPERM;
1074
Al Viro10556cb22011-06-20 19:28:19 -04001075 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001076 if (error)
1077 return error;
1078
Fabian Frederick37975f12014-10-09 22:49:21 +02001079 return gfs2_dir_check(&dip->i_inode, name, ip);
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001080}
1081
David Teiglandb3b94fa2006-01-16 16:50:04 +00001082/**
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001083 * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
1084 * @dip: The parent directory
1085 * @name: The name of the entry in the parent directory
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001086 * @inode: The inode to be removed
1087 *
1088 * Called with all the locks and in a transaction. This will only be
1089 * called for a directory after it has been checked to ensure it is empty.
1090 *
1091 * Returns: 0 on success, or an error
1092 */
1093
1094static int gfs2_unlink_inode(struct gfs2_inode *dip,
Bob Peterson4327a9b2012-11-12 13:03:29 -05001095 const struct dentry *dentry)
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001096{
David Howells2b0143b2015-03-17 22:25:59 +00001097 struct inode *inode = d_inode(dentry);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001098 struct gfs2_inode *ip = GFS2_I(inode);
1099 int error;
1100
1101 error = gfs2_dir_del(dip, dentry);
1102 if (error)
1103 return error;
1104
1105 ip->i_entries = 0;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001106 inode->i_ctime = current_time(inode);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001107 if (S_ISDIR(inode->i_mode))
1108 clear_nlink(inode);
1109 else
1110 drop_nlink(inode);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001111 mark_inode_dirty(inode);
1112 if (inode->i_nlink == 0)
1113 gfs2_unlink_di(inode);
1114 return 0;
1115}
1116
1117
1118/**
1119 * gfs2_unlink - Unlink an inode (this does rmdir as well)
1120 * @dir: The inode of the directory containing the inode to unlink
David Teiglandb3b94fa2006-01-16 16:50:04 +00001121 * @dentry: The file itself
1122 *
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001123 * This routine uses the type of the inode as a flag to figure out
1124 * whether this is an unlink or an rmdir.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001125 *
1126 * Returns: errno
1127 */
1128
1129static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
1130{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001131 struct gfs2_inode *dip = GFS2_I(dir);
1132 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +00001133 struct inode *inode = d_inode(dentry);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001134 struct gfs2_inode *ip = GFS2_I(inode);
Russell Cattelanddee7602007-01-29 17:13:44 -06001135 struct gfs2_holder ghs[3];
1136 struct gfs2_rgrpd *rgd;
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001137 int error;
1138
1139 error = gfs2_rindex_update(sdp);
1140 if (error)
1141 return error;
1142
1143 error = -EROFS;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001144
Russell Cattelanddee7602007-01-29 17:13:44 -06001145 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
1146 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
1147
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001148 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
Steven Whitehousea365fbf2012-02-24 15:09:14 +00001149 if (!rgd)
Steven Whitehouse87654892011-11-08 14:04:20 +00001150 goto out_inodes;
Steven Whitehousea365fbf2012-02-24 15:09:14 +00001151
Russell Cattelanddee7602007-01-29 17:13:44 -06001152 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
1153
1154
Steven Whitehouse8497a462007-08-26 14:23:56 +01001155 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001156 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +01001157 goto out_parent;
1158
1159 error = gfs2_glock_nq(ghs + 1); /* child */
1160 if (error)
1161 goto out_child;
1162
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001163 error = -ENOENT;
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001164 if (inode->i_nlink == 0)
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001165 goto out_rgrp;
1166
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001167 if (S_ISDIR(inode->i_mode)) {
1168 error = -ENOTEMPTY;
1169 if (ip->i_entries > 2 || inode->i_nlink > 2)
1170 goto out_rgrp;
1171 }
1172
Steven Whitehouse8497a462007-08-26 14:23:56 +01001173 error = gfs2_glock_nq(ghs + 2); /* rgrp */
1174 if (error)
1175 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001176
1177 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
1178 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -05001179 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001180
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001181 error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001182 if (error)
Bob Peterson2eb59092018-01-29 10:00:23 -07001183 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001184
Bob Peterson4327a9b2012-11-12 13:03:29 -05001185 error = gfs2_unlink_inode(dip, dentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001186 gfs2_trans_end(sdp);
Bob Peterson2eb59092018-01-29 10:00:23 -07001187
Bob Peterson72dbf472008-08-12 13:39:29 -05001188out_gunlock:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001189 gfs2_glock_dq(ghs + 2);
1190out_rgrp:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001191 gfs2_glock_dq(ghs + 1);
1192out_child:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001193 gfs2_glock_dq(ghs);
1194out_parent:
Steven Whitehouse87654892011-11-08 14:04:20 +00001195 gfs2_holder_uninit(ghs + 2);
1196out_inodes:
1197 gfs2_holder_uninit(ghs + 1);
Steven Whitehouse8497a462007-08-26 14:23:56 +01001198 gfs2_holder_uninit(ghs);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001199 return error;
1200}
1201
1202/**
1203 * gfs2_symlink - Create a symlink
1204 * @dir: The directory to create the symlink in
1205 * @dentry: The dentry to put the symlink in
1206 * @symname: The thing which the link points to
1207 *
1208 * Returns: errno
1209 */
1210
1211static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
1212 const char *symname)
1213{
Steven Whitehouse160b4022011-05-13 10:34:59 +01001214 unsigned int size;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001215
David Teiglandb3b94fa2006-01-16 16:50:04 +00001216 size = strlen(symname);
Andreas Gruenbacher235628c2017-11-14 16:53:12 +01001217 if (size >= gfs2_max_stuffed_size(GFS2_I(dir)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001218 return -ENAMETOOLONG;
1219
Al Virob452a452018-06-08 13:06:28 -04001220 return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001221}
1222
1223/**
1224 * gfs2_mkdir - Make a directory
1225 * @dir: The parent directory of the new one
1226 * @dentry: The dentry of the new directory
1227 * @mode: The mode of the new directory
1228 *
1229 * Returns: errno
1230 */
1231
Al Viro18bb1db2011-07-26 01:41:39 -04001232static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001233{
Andreas Gruenbacher235628c2017-11-14 16:53:12 +01001234 unsigned dsize = gfs2_max_stuffed_size(GFS2_I(dir));
Al Virob452a452018-06-08 13:06:28 -04001235 return gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001236}
1237
1238/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001239 * gfs2_mknod - Make a special file
1240 * @dir: The directory in which the special file will reside
1241 * @dentry: The dentry of the special file
1242 * @mode: The mode of the special file
Steven Whitehousef2741d92011-05-13 12:11:17 +01001243 * @dev: The device specification of the special file
David Teiglandb3b94fa2006-01-16 16:50:04 +00001244 *
1245 */
1246
Al Viro1a67aaf2011-07-26 01:52:52 -04001247static int gfs2_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001248 dev_t dev)
1249{
Al Virob452a452018-06-08 13:06:28 -04001250 return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001251}
1252
1253/**
1254 * gfs2_atomic_open - Atomically open a file
1255 * @dir: The directory
1256 * @dentry: The proposed new entry
1257 * @file: The proposed new struct file
1258 * @flags: open flags
1259 * @mode: File mode
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001260 *
1261 * Returns: error code or 0 for success
1262 */
1263
1264static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry,
Antonio Ospite86fbca42015-05-01 12:54:38 -05001265 struct file *file, unsigned flags,
Al Viro44907d72018-06-08 13:32:02 -04001266 umode_t mode)
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001267{
1268 struct dentry *d;
1269 bool excl = !!(flags & O_EXCL);
1270
Al Viro00699ad2016-07-05 09:44:53 -04001271 if (!d_in_lookup(dentry))
Al Viro4d93bc32014-09-12 18:21:05 -04001272 goto skip_lookup;
1273
Al Virob452a452018-06-08 13:06:28 -04001274 d = __gfs2_lookup(dir, dentry, file);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001275 if (IS_ERR(d))
1276 return PTR_ERR(d);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001277 if (d != NULL)
1278 dentry = d;
David Howells2b0143b2015-03-17 22:25:59 +00001279 if (d_really_is_positive(dentry)) {
Al Viroaad888f2018-06-08 12:58:04 -04001280 if (!(file->f_mode & FMODE_OPENED))
Al Viroec7d8792014-11-19 19:35:58 +00001281 return finish_no_open(file, d);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001282 dput(d);
Al Viro21039132020-03-10 09:31:41 -04001283 return excl && (flags & O_CREAT) ? -EEXIST : 0;
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001284 }
1285
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001286 BUG_ON(d != NULL);
Al Viro4d93bc32014-09-12 18:21:05 -04001287
1288skip_lookup:
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001289 if (!(flags & O_CREAT))
1290 return -ENOENT;
1291
Al Virob452a452018-06-08 13:06:28 -04001292 return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001293}
1294
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001295/*
1296 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1297 * @this: move this
1298 * @to: to here
1299 *
1300 * Follow @to back to the root and make sure we don't encounter @this
1301 * Assumes we already hold the rename lock.
1302 *
1303 * Returns: errno
1304 */
1305
1306static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1307{
1308 struct inode *dir = &to->i_inode;
1309 struct super_block *sb = dir->i_sb;
1310 struct inode *tmp;
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001311 int error = 0;
1312
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001313 igrab(dir);
1314
1315 for (;;) {
1316 if (dir == &this->i_inode) {
1317 error = -EINVAL;
1318 break;
1319 }
David Howells2b0143b2015-03-17 22:25:59 +00001320 if (dir == d_inode(sb->s_root)) {
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001321 error = 0;
1322 break;
1323 }
1324
Steven Whitehouse8d123582010-09-17 12:30:23 +01001325 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
Abhi Das48f8f712014-03-12 03:41:44 -05001326 if (!tmp) {
1327 error = -ENOENT;
1328 break;
1329 }
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001330 if (IS_ERR(tmp)) {
1331 error = PTR_ERR(tmp);
1332 break;
1333 }
1334
1335 iput(dir);
1336 dir = tmp;
1337 }
1338
1339 iput(dir);
1340
1341 return error;
1342}
1343
David Teiglandb3b94fa2006-01-16 16:50:04 +00001344/**
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001345 * update_moved_ino - Update an inode that's being moved
1346 * @ip: The inode being moved
1347 * @ndip: The parent directory of the new filename
1348 * @dir_rename: True of ip is a directory
1349 *
1350 * Returns: errno
1351 */
1352
1353static int update_moved_ino(struct gfs2_inode *ip, struct gfs2_inode *ndip,
1354 int dir_rename)
1355{
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001356 if (dir_rename)
1357 return gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
1358
Deepa Dinamani078cd822016-09-14 07:48:04 -07001359 ip->i_inode.i_ctime = current_time(&ip->i_inode);
Andreas Gruenbacher83998cc2018-02-28 12:48:53 -07001360 mark_inode_dirty_sync(&ip->i_inode);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001361 return 0;
1362}
1363
1364
1365/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001366 * gfs2_rename - Rename a file
1367 * @odir: Parent directory of old file name
1368 * @odentry: The old dentry of the file
1369 * @ndir: Parent directory of new file name
1370 * @ndentry: The new dentry of the file
1371 *
1372 * Returns: errno
1373 */
1374
1375static int gfs2_rename(struct inode *odir, struct dentry *odentry,
1376 struct inode *ndir, struct dentry *ndentry)
1377{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001378 struct gfs2_inode *odip = GFS2_I(odir);
1379 struct gfs2_inode *ndip = GFS2_I(ndir);
David Howells2b0143b2015-03-17 22:25:59 +00001380 struct gfs2_inode *ip = GFS2_I(d_inode(odentry));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001381 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001382 struct gfs2_sbd *sdp = GFS2_SB(odir);
Bob Petersonbc74aae2019-08-30 12:31:00 -05001383 struct gfs2_holder ghs[4], r_gh, rd_gh;
Russell Cattelanddee7602007-01-29 17:13:44 -06001384 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001385 unsigned int num_gh;
1386 int dir_rename = 0;
Bob Peterson19aeb5a62014-09-29 08:52:04 -04001387 struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, };
David Teiglandb3b94fa2006-01-16 16:50:04 +00001388 unsigned int x;
1389 int error;
1390
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001391 gfs2_holder_mark_uninitialized(&r_gh);
Bob Petersonbc74aae2019-08-30 12:31:00 -05001392 gfs2_holder_mark_uninitialized(&rd_gh);
David Howells2b0143b2015-03-17 22:25:59 +00001393 if (d_really_is_positive(ndentry)) {
1394 nip = GFS2_I(d_inode(ndentry));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001395 if (ip == nip)
1396 return 0;
1397 }
1398
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001399 error = gfs2_rindex_update(sdp);
1400 if (error)
1401 return error;
1402
Bob Peterson2fba46a2020-02-27 12:47:53 -06001403 error = gfs2_qa_get(ndip);
Bob Peterson0a305e42012-06-06 11:17:59 +01001404 if (error)
1405 return error;
1406
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001407 if (odip != ndip) {
1408 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1409 0, &r_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001410 if (error)
1411 goto out;
1412
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001413 if (S_ISDIR(ip->i_inode.i_mode)) {
1414 dir_rename = 1;
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001415 /* don't move a directory into its subdir */
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001416 error = gfs2_ok_to_move(ip, ndip);
1417 if (error)
1418 goto out_gunlock_r;
1419 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001420 }
1421
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001422 num_gh = 1;
Bob Petersonad269672019-08-30 12:31:02 -05001423 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001424 if (odip != ndip) {
Bob Petersonad269672019-08-30 12:31:02 -05001425 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE,GL_ASYNC,
1426 ghs + num_gh);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001427 num_gh++;
1428 }
Bob Petersonad269672019-08-30 12:31:02 -05001429 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001430 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001431
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001432 if (nip) {
Bob Petersonad269672019-08-30 12:31:02 -05001433 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1434 ghs + num_gh);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001435 num_gh++;
1436 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001437
Bob Peterson72dbf472008-08-12 13:39:29 -05001438 for (x = 0; x < num_gh; x++) {
1439 error = gfs2_glock_nq(ghs + x);
1440 if (error)
1441 goto out_gunlock;
1442 }
Bob Petersonad269672019-08-30 12:31:02 -05001443 error = gfs2_glock_async_wait(num_gh, ghs);
1444 if (error)
1445 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001446
Bob Petersonbc74aae2019-08-30 12:31:00 -05001447 if (nip) {
1448 /* Grab the resource group glock for unlink flag twiddling.
1449 * This is the case where the target dinode already exists
1450 * so we unlink before doing the rename.
1451 */
1452 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
1453 if (!nrgd) {
1454 error = -ENOENT;
1455 goto out_gunlock;
1456 }
1457 error = gfs2_glock_nq_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0,
1458 &rd_gh);
1459 if (error)
1460 goto out_gunlock;
1461 }
1462
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001463 error = -ENOENT;
1464 if (ip->i_inode.i_nlink == 0)
1465 goto out_gunlock;
1466
David Teiglandb3b94fa2006-01-16 16:50:04 +00001467 /* Check out the old directory */
1468
1469 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
1470 if (error)
1471 goto out_gunlock;
1472
1473 /* Check out the new directory */
1474
1475 if (nip) {
1476 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1477 if (error)
1478 goto out_gunlock;
1479
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001480 if (nip->i_inode.i_nlink == 0) {
1481 error = -EAGAIN;
1482 goto out_gunlock;
1483 }
1484
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001485 if (S_ISDIR(nip->i_inode.i_mode)) {
Steven Whitehousead6203f22008-11-03 13:59:19 +00001486 if (nip->i_entries < 2) {
Steven Whitehouse94fb7632011-05-09 13:36:10 +01001487 gfs2_consist_inode(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001488 error = -EIO;
1489 goto out_gunlock;
1490 }
Steven Whitehousead6203f22008-11-03 13:59:19 +00001491 if (nip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001492 error = -ENOTEMPTY;
1493 goto out_gunlock;
1494 }
1495 }
1496 } else {
Al Viro10556cb22011-06-20 19:28:19 -04001497 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001498 if (error)
1499 goto out_gunlock;
1500
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001501 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001502 switch (error) {
1503 case -ENOENT:
1504 error = 0;
1505 break;
1506 case 0:
1507 error = -EEXIST;
1508 default:
1509 goto out_gunlock;
Aliasgar Surti098b9c12019-10-04 10:55:29 -05001510 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001511
1512 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -05001513 if (!ndip->i_inode.i_nlink) {
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001514 error = -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001515 goto out_gunlock;
1516 }
Steven Whitehousead6203f22008-11-03 13:59:19 +00001517 if (ndip->i_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001518 error = -EFBIG;
1519 goto out_gunlock;
1520 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001521 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -05001522 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001523 error = -EMLINK;
1524 goto out_gunlock;
1525 }
1526 }
1527 }
1528
1529 /* Check out the dir to be renamed */
1530
1531 if (dir_rename) {
David Howells2b0143b2015-03-17 22:25:59 +00001532 error = gfs2_permission(d_inode(odentry), MAY_WRITE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001533 if (error)
1534 goto out_gunlock;
1535 }
1536
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001537 if (nip == NULL) {
1538 error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
1539 if (error)
1540 goto out_gunlock;
1541 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001542
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001543 if (da.nr_blocks) {
1544 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
Abhi Dasb8fbf472015-03-18 12:03:41 -05001545 error = gfs2_quota_lock_check(ndip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001546 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001547 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001548
Steven Whitehouse7b9cff42013-10-02 11:13:25 +01001549 error = gfs2_inplace_reserve(ndip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001550 if (error)
1551 goto out_gunlock_q;
1552
Steven Whitehouse534cf9c2014-01-06 12:03:05 +00001553 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
1554 4 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001555 if (error)
1556 goto out_ipreserv;
1557 } else {
1558 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -05001559 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001560 if (error)
1561 goto out_gunlock;
1562 }
1563
1564 /* Remove the target file, if it exists */
1565
Bob Peterson4327a9b2012-11-12 13:03:29 -05001566 if (nip)
1567 error = gfs2_unlink_inode(ndip, ndentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001568
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001569 error = update_moved_ino(ip, ndip, dir_rename);
1570 if (error)
1571 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001572
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001573 error = gfs2_dir_del(odip, odentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001574 if (error)
1575 goto out_end_trans;
1576
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001577 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001578 if (error)
1579 goto out_end_trans;
1580
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001581out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001582 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001583out_ipreserv:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001584 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001585 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001586out_gunlock_q:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001587 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001588 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001589out_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001590 gfs2_dir_no_add(&da);
Bob Petersonbc74aae2019-08-30 12:31:00 -05001591 if (gfs2_holder_initialized(&rd_gh))
1592 gfs2_glock_dq_uninit(&rd_gh);
1593
Bob Peterson72dbf472008-08-12 13:39:29 -05001594 while (x--) {
Bob Petersonad269672019-08-30 12:31:02 -05001595 if (gfs2_holder_queued(ghs + x))
1596 gfs2_glock_dq(ghs + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001597 gfs2_holder_uninit(ghs + x);
Bob Peterson72dbf472008-08-12 13:39:29 -05001598 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001599out_gunlock_r:
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001600 if (gfs2_holder_initialized(&r_gh))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001601 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001602out:
Bob Peterson2fba46a2020-02-27 12:47:53 -06001603 gfs2_qa_put(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001604 return error;
1605}
1606
1607/**
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001608 * gfs2_exchange - exchange two files
1609 * @odir: Parent directory of old file name
1610 * @odentry: The old dentry of the file
1611 * @ndir: Parent directory of new file name
1612 * @ndentry: The new dentry of the file
1613 * @flags: The rename flags
1614 *
1615 * Returns: errno
1616 */
1617
1618static int gfs2_exchange(struct inode *odir, struct dentry *odentry,
1619 struct inode *ndir, struct dentry *ndentry,
1620 unsigned int flags)
1621{
1622 struct gfs2_inode *odip = GFS2_I(odir);
1623 struct gfs2_inode *ndip = GFS2_I(ndir);
1624 struct gfs2_inode *oip = GFS2_I(odentry->d_inode);
1625 struct gfs2_inode *nip = GFS2_I(ndentry->d_inode);
1626 struct gfs2_sbd *sdp = GFS2_SB(odir);
Bob Petersonad269672019-08-30 12:31:02 -05001627 struct gfs2_holder ghs[4], r_gh;
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001628 unsigned int num_gh;
1629 unsigned int x;
1630 umode_t old_mode = oip->i_inode.i_mode;
1631 umode_t new_mode = nip->i_inode.i_mode;
1632 int error;
1633
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001634 gfs2_holder_mark_uninitialized(&r_gh);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001635 error = gfs2_rindex_update(sdp);
1636 if (error)
1637 return error;
1638
1639 if (odip != ndip) {
1640 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1641 0, &r_gh);
1642 if (error)
1643 goto out;
1644
1645 if (S_ISDIR(old_mode)) {
1646 /* don't move a directory into its subdir */
1647 error = gfs2_ok_to_move(oip, ndip);
1648 if (error)
1649 goto out_gunlock_r;
1650 }
1651
1652 if (S_ISDIR(new_mode)) {
1653 /* don't move a directory into its subdir */
1654 error = gfs2_ok_to_move(nip, odip);
1655 if (error)
1656 goto out_gunlock_r;
1657 }
1658 }
1659
1660 num_gh = 1;
Bob Petersonad269672019-08-30 12:31:02 -05001661 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001662 if (odip != ndip) {
Bob Petersonad269672019-08-30 12:31:02 -05001663 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1664 ghs + num_gh);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001665 num_gh++;
1666 }
Bob Petersonad269672019-08-30 12:31:02 -05001667 gfs2_holder_init(oip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001668 num_gh++;
1669
Bob Petersonad269672019-08-30 12:31:02 -05001670 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001671 num_gh++;
1672
1673 for (x = 0; x < num_gh; x++) {
1674 error = gfs2_glock_nq(ghs + x);
1675 if (error)
1676 goto out_gunlock;
1677 }
1678
Bob Petersonad269672019-08-30 12:31:02 -05001679 error = gfs2_glock_async_wait(num_gh, ghs);
1680 if (error)
1681 goto out_gunlock;
1682
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001683 error = -ENOENT;
1684 if (oip->i_inode.i_nlink == 0 || nip->i_inode.i_nlink == 0)
1685 goto out_gunlock;
1686
1687 error = gfs2_unlink_ok(odip, &odentry->d_name, oip);
1688 if (error)
1689 goto out_gunlock;
1690 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1691 if (error)
1692 goto out_gunlock;
1693
1694 if (S_ISDIR(old_mode)) {
1695 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
1696 if (error)
1697 goto out_gunlock;
1698 }
1699 if (S_ISDIR(new_mode)) {
1700 error = gfs2_permission(ndentry->d_inode, MAY_WRITE);
1701 if (error)
1702 goto out_gunlock;
1703 }
1704 error = gfs2_trans_begin(sdp, 4 * RES_DINODE + 4 * RES_LEAF, 0);
1705 if (error)
1706 goto out_gunlock;
1707
1708 error = update_moved_ino(oip, ndip, S_ISDIR(old_mode));
1709 if (error)
1710 goto out_end_trans;
1711
1712 error = update_moved_ino(nip, odip, S_ISDIR(new_mode));
1713 if (error)
1714 goto out_end_trans;
1715
1716 error = gfs2_dir_mvino(ndip, &ndentry->d_name, oip,
1717 IF2DT(old_mode));
1718 if (error)
1719 goto out_end_trans;
1720
1721 error = gfs2_dir_mvino(odip, &odentry->d_name, nip,
1722 IF2DT(new_mode));
1723 if (error)
1724 goto out_end_trans;
1725
1726 if (odip != ndip) {
1727 if (S_ISDIR(new_mode) && !S_ISDIR(old_mode)) {
1728 inc_nlink(&odip->i_inode);
1729 drop_nlink(&ndip->i_inode);
1730 } else if (S_ISDIR(old_mode) && !S_ISDIR(new_mode)) {
1731 inc_nlink(&ndip->i_inode);
1732 drop_nlink(&odip->i_inode);
1733 }
1734 }
1735 mark_inode_dirty(&ndip->i_inode);
1736 if (odip != ndip)
1737 mark_inode_dirty(&odip->i_inode);
1738
1739out_end_trans:
1740 gfs2_trans_end(sdp);
1741out_gunlock:
1742 while (x--) {
Bob Petersonad269672019-08-30 12:31:02 -05001743 if (gfs2_holder_queued(ghs + x))
1744 gfs2_glock_dq(ghs + x);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001745 gfs2_holder_uninit(ghs + x);
1746 }
1747out_gunlock_r:
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001748 if (gfs2_holder_initialized(&r_gh))
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001749 gfs2_glock_dq_uninit(&r_gh);
1750out:
1751 return error;
1752}
1753
1754static int gfs2_rename2(struct inode *odir, struct dentry *odentry,
1755 struct inode *ndir, struct dentry *ndentry,
1756 unsigned int flags)
1757{
1758 flags &= ~RENAME_NOREPLACE;
1759
1760 if (flags & ~RENAME_EXCHANGE)
1761 return -EINVAL;
1762
1763 if (flags & RENAME_EXCHANGE)
1764 return gfs2_exchange(odir, odentry, ndir, ndentry, flags);
1765
1766 return gfs2_rename(odir, odentry, ndir, ndentry);
1767}
1768
1769/**
Al Viro6b255392015-11-17 10:20:54 -05001770 * gfs2_get_link - Follow a symbolic link
David Teiglandb3b94fa2006-01-16 16:50:04 +00001771 * @dentry: The dentry of the link
Al Viro6b255392015-11-17 10:20:54 -05001772 * @inode: The inode of the link
Al Virofceef392015-12-29 15:58:39 -05001773 * @done: destructor for return value
David Teiglandb3b94fa2006-01-16 16:50:04 +00001774 *
Al Viroc177c2a2010-01-14 00:59:16 -05001775 * This can handle symlinks of any size.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001776 *
1777 * Returns: 0 on success or error code
1778 */
1779
Al Viro6b255392015-11-17 10:20:54 -05001780static const char *gfs2_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001781 struct inode *inode,
1782 struct delayed_call *done)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001783{
Al Viro6b255392015-11-17 10:20:54 -05001784 struct gfs2_inode *ip = GFS2_I(inode);
Al Viroc177c2a2010-01-14 00:59:16 -05001785 struct gfs2_holder i_gh;
1786 struct buffer_head *dibh;
Steven Whitehouse160b4022011-05-13 10:34:59 +01001787 unsigned int size;
Al Viroc177c2a2010-01-14 00:59:16 -05001788 char *buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001789 int error;
1790
Al Viro6b255392015-11-17 10:20:54 -05001791 if (!dentry)
1792 return ERR_PTR(-ECHILD);
1793
Al Viroc177c2a2010-01-14 00:59:16 -05001794 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1795 error = gfs2_glock_nq(&i_gh);
1796 if (error) {
1797 gfs2_holder_uninit(&i_gh);
Al Viro680baac2015-05-02 13:32:22 -04001798 return ERR_PTR(error);
Al Viroc177c2a2010-01-14 00:59:16 -05001799 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001800
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001801 size = (unsigned int)i_size_read(&ip->i_inode);
1802 if (size == 0) {
Al Viroc177c2a2010-01-14 00:59:16 -05001803 gfs2_consist_inode(ip);
1804 buf = ERR_PTR(-EIO);
1805 goto out;
1806 }
1807
1808 error = gfs2_meta_inode_buffer(ip, &dibh);
1809 if (error) {
1810 buf = ERR_PTR(error);
1811 goto out;
1812 }
1813
Steven Whitehouse160b4022011-05-13 10:34:59 +01001814 buf = kzalloc(size + 1, GFP_NOFS);
Al Viroc177c2a2010-01-14 00:59:16 -05001815 if (!buf)
1816 buf = ERR_PTR(-ENOMEM);
1817 else
Steven Whitehouse160b4022011-05-13 10:34:59 +01001818 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
Al Viroc177c2a2010-01-14 00:59:16 -05001819 brelse(dibh);
1820out:
1821 gfs2_glock_dq_uninit(&i_gh);
Al Viro680baac2015-05-02 13:32:22 -04001822 if (!IS_ERR(buf))
Al Virofceef392015-12-29 15:58:39 -05001823 set_delayed_call(done, kfree_link, buf);
Al Viro680baac2015-05-02 13:32:22 -04001824 return buf;
Al Viroc177c2a2010-01-14 00:59:16 -05001825}
1826
David Teiglandb3b94fa2006-01-16 16:50:04 +00001827/**
1828 * gfs2_permission -
Steven Whitehouse75d5cfb2011-01-19 09:42:40 +00001829 * @inode: The inode
1830 * @mask: The mask to be tested
1831 * @flags: Indicates whether this is an RCU path walk or not
David Teiglandb3b94fa2006-01-16 16:50:04 +00001832 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001833 * This may be called from the VFS directly, or from within GFS2 with the
1834 * inode locked, so we look to see if the glock is already locked and only
1835 * lock the glock if its not already been done.
1836 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001837 * Returns: errno
1838 */
1839
Al Viro10556cb22011-06-20 19:28:19 -04001840int gfs2_permission(struct inode *inode, int mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001841{
Nick Pigginb74c79e2011-01-07 17:49:58 +11001842 struct gfs2_inode *ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001843 struct gfs2_holder i_gh;
1844 int error;
1845
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001846 gfs2_holder_mark_uninitialized(&i_gh);
Nick Pigginb74c79e2011-01-07 17:49:58 +11001847 ip = GFS2_I(inode);
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001848 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001849 if (mask & MAY_NOT_BLOCK)
1850 return -ECHILD;
1851 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1852 if (error)
1853 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001854 }
1855
Miklos Szeredif58ba882008-07-02 21:12:01 +02001856 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
Eryu Guan337684a2016-08-02 19:58:28 +08001857 error = -EPERM;
Miklos Szeredif58ba882008-07-02 21:12:01 +02001858 else
Al Viro2830ba72011-06-20 19:16:29 -04001859 error = generic_permission(inode, mask);
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001860 if (gfs2_holder_initialized(&i_gh))
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001861 gfs2_glock_dq_uninit(&i_gh);
1862
David Teiglandb3b94fa2006-01-16 16:50:04 +00001863 return error;
1864}
1865
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001866static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
Steven Whitehouse194c0112011-05-09 14:06:38 +01001867{
Steven Whitehouse194c0112011-05-09 14:06:38 +01001868 setattr_copy(inode, attr);
1869 mark_inode_dirty(inode);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001870 return 0;
1871}
1872
1873/**
1874 * gfs2_setattr_simple -
1875 * @ip:
1876 * @attr:
1877 *
1878 * Returns: errno
1879 */
1880
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001881int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
Steven Whitehouse194c0112011-05-09 14:06:38 +01001882{
1883 int error;
1884
1885 if (current->journal_info)
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001886 return __gfs2_setattr_simple(inode, attr);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001887
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001888 error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001889 if (error)
1890 return error;
1891
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001892 error = __gfs2_setattr_simple(inode, attr);
1893 gfs2_trans_end(GFS2_SB(inode));
Steven Whitehouse194c0112011-05-09 14:06:38 +01001894 return error;
1895}
1896
David Teiglandb3b94fa2006-01-16 16:50:04 +00001897static int setattr_chown(struct inode *inode, struct iattr *attr)
1898{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001899 struct gfs2_inode *ip = GFS2_I(inode);
1900 struct gfs2_sbd *sdp = GFS2_SB(inode);
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -08001901 kuid_t ouid, nuid;
1902 kgid_t ogid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001903 int error;
Abhi Dasb8fbf472015-03-18 12:03:41 -05001904 struct gfs2_alloc_parms ap;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001905
Steven Whitehouse2933f922006-11-01 13:23:29 -05001906 ouid = inode->i_uid;
1907 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001908 nuid = attr->ia_uid;
1909 ngid = attr->ia_gid;
1910
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001911 if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001912 ouid = nuid = NO_UID_QUOTA_CHANGE;
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001913 if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001914 ogid = ngid = NO_GID_QUOTA_CHANGE;
Bob Peterson2fba46a2020-02-27 12:47:53 -06001915 error = gfs2_qa_get(ip);
Bob Peterson62e96cf2014-01-06 17:16:01 -05001916 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -06001917 return error;
Bob Peterson62e96cf2014-01-06 17:16:01 -05001918
1919 error = gfs2_rindex_update(sdp);
1920 if (error)
1921 goto out;
1922
1923 error = gfs2_quota_lock(ip, nuid, ngid);
1924 if (error)
1925 goto out;
1926
Abhi Dasb8fbf472015-03-18 12:03:41 -05001927 ap.target = gfs2_get_inode_blocks(&ip->i_inode);
1928
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001929 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1930 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
Abhi Dasb8fbf472015-03-18 12:03:41 -05001931 error = gfs2_quota_check(ip, nuid, ngid, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001932 if (error)
1933 goto out_gunlock_q;
1934 }
1935
1936 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1937 if (error)
1938 goto out_gunlock_q;
1939
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001940 error = gfs2_setattr_simple(inode, attr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001941 if (error)
1942 goto out_end_trans;
1943
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001944 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1945 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
Abhi Das39a72582015-06-02 11:02:24 -05001946 gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid);
Abhi Dasb8fbf472015-03-18 12:03:41 -05001947 gfs2_quota_change(ip, ap.target, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001948 }
1949
Steven Whitehousea91ea692006-09-04 12:04:26 -04001950out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001951 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001952out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001953 gfs2_quota_unlock(ip);
Bob Peterson62e96cf2014-01-06 17:16:01 -05001954out:
Bob Peterson2fba46a2020-02-27 12:47:53 -06001955 gfs2_qa_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001956 return error;
1957}
1958
1959/**
1960 * gfs2_setattr - Change attributes on an inode
1961 * @dentry: The dentry which is changing
1962 * @attr: The structure describing the change
1963 *
1964 * The VFS layer wants to change one or more of an inodes attributes. Write
1965 * that change out to disk.
1966 *
1967 * Returns: errno
1968 */
1969
1970static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1971{
David Howells2b0143b2015-03-17 22:25:59 +00001972 struct inode *inode = d_inode(dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001973 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001974 struct gfs2_holder i_gh;
1975 int error;
1976
Bob Peterson2fba46a2020-02-27 12:47:53 -06001977 error = gfs2_qa_get(ip);
Bob Peterson0a305e42012-06-06 11:17:59 +01001978 if (error)
1979 return error;
1980
David Teiglandb3b94fa2006-01-16 16:50:04 +00001981 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1982 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -06001983 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001984
1985 error = -EPERM;
1986 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Bob Peterson2fba46a2020-02-27 12:47:53 -06001987 goto error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001988
Jan Kara31051c82016-05-26 16:55:18 +02001989 error = setattr_prepare(dentry, attr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001990 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -06001991 goto error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001992
1993 if (attr->ia_valid & ATTR_SIZE)
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001994 error = gfs2_setattr_size(inode, attr->ia_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001995 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1996 error = setattr_chown(inode, attr);
Christoph Hellwige01580b2013-12-20 05:16:52 -08001997 else {
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001998 error = gfs2_setattr_simple(inode, attr);
Christoph Hellwige01580b2013-12-20 05:16:52 -08001999 if (!error && attr->ia_valid & ATTR_MODE)
2000 error = posix_acl_chmod(inode, inode->i_mode);
2001 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00002002
Bob Peterson2fba46a2020-02-27 12:47:53 -06002003error:
David Teiglandb3b94fa2006-01-16 16:50:04 +00002004 if (!error)
2005 mark_inode_dirty(inode);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01002006 gfs2_glock_dq_uninit(&i_gh);
Bob Peterson2fba46a2020-02-27 12:47:53 -06002007out:
2008 gfs2_qa_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002009 return error;
2010}
2011
2012/**
2013 * gfs2_getattr - Read out an inode's attributes
David Howellsa528d352017-01-31 16:46:22 +00002014 * @path: Object to query
David Teiglandb3b94fa2006-01-16 16:50:04 +00002015 * @stat: The inode's stats
David Howellsa528d352017-01-31 16:46:22 +00002016 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
2017 * @flags: AT_STATX_xxx setting
David Teiglandb3b94fa2006-01-16 16:50:04 +00002018 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05002019 * This may be called from the VFS directly, or from within GFS2 with the
2020 * inode locked, so we look to see if the glock is already locked and only
2021 * lock the glock if its not already been done. Note that its the NFS
2022 * readdirplus operation which causes this to be called (from filldir)
2023 * with the glock already held.
2024 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00002025 * Returns: errno
2026 */
2027
David Howellsa528d352017-01-31 16:46:22 +00002028static int gfs2_getattr(const struct path *path, struct kstat *stat,
2029 u32 request_mask, unsigned int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +00002030{
David Howellsa528d352017-01-31 16:46:22 +00002031 struct inode *inode = d_inode(path->dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04002032 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002033 struct gfs2_holder gh;
Andreas Gruenbacherb2623c22017-10-09 17:55:58 +02002034 u32 gfsflags;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002035 int error;
2036
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05002037 gfs2_holder_mark_uninitialized(&gh);
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00002038 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06002039 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
2040 if (error)
2041 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002042 }
2043
Andreas Gruenbacherb2623c22017-10-09 17:55:58 +02002044 gfsflags = ip->i_diskflags;
2045 if (gfsflags & GFS2_DIF_APPENDONLY)
2046 stat->attributes |= STATX_ATTR_APPEND;
2047 if (gfsflags & GFS2_DIF_IMMUTABLE)
2048 stat->attributes |= STATX_ATTR_IMMUTABLE;
2049
2050 stat->attributes_mask |= (STATX_ATTR_APPEND |
2051 STATX_ATTR_COMPRESSED |
2052 STATX_ATTR_ENCRYPTED |
2053 STATX_ATTR_IMMUTABLE |
2054 STATX_ATTR_NODUMP);
2055
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05002056 generic_fillattr(inode, stat);
Andreas Gruenbacherb2623c22017-10-09 17:55:58 +02002057
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05002058 if (gfs2_holder_initialized(&gh))
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05002059 gfs2_glock_dq_uninit(&gh);
2060
2061 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002062}
2063
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002064static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2065 u64 start, u64 len)
2066{
2067 struct gfs2_inode *ip = GFS2_I(inode);
2068 struct gfs2_holder gh;
2069 int ret;
2070
Bob Petersonaac1a552017-02-16 21:13:54 +01002071 inode_lock_shared(inode);
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002072
2073 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2074 if (ret)
2075 goto out;
2076
Bob Petersonaac1a552017-02-16 21:13:54 +01002077 ret = iomap_fiemap(inode, fieinfo, start, len, &gfs2_iomap_ops);
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002078
2079 gfs2_glock_dq_uninit(&gh);
Bob Petersonaac1a552017-02-16 21:13:54 +01002080
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002081out:
Bob Petersonaac1a552017-02-16 21:13:54 +01002082 inode_unlock_shared(inode);
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002083 return ret;
2084}
2085
Andreas Gruenbacher3a274112017-03-15 19:12:59 +01002086loff_t gfs2_seek_data(struct file *file, loff_t offset)
2087{
2088 struct inode *inode = file->f_mapping->host;
2089 struct gfs2_inode *ip = GFS2_I(inode);
2090 struct gfs2_holder gh;
2091 loff_t ret;
2092
2093 inode_lock_shared(inode);
2094 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2095 if (!ret)
2096 ret = iomap_seek_data(inode, offset, &gfs2_iomap_ops);
2097 gfs2_glock_dq_uninit(&gh);
2098 inode_unlock_shared(inode);
2099
2100 if (ret < 0)
2101 return ret;
2102 return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
2103}
2104
2105loff_t gfs2_seek_hole(struct file *file, loff_t offset)
2106{
2107 struct inode *inode = file->f_mapping->host;
2108 struct gfs2_inode *ip = GFS2_I(inode);
2109 struct gfs2_holder gh;
2110 loff_t ret;
2111
2112 inode_lock_shared(inode);
2113 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2114 if (!ret)
2115 ret = iomap_seek_hole(inode, offset, &gfs2_iomap_ops);
2116 gfs2_glock_dq_uninit(&gh);
2117 inode_unlock_shared(inode);
2118
2119 if (ret < 0)
2120 return ret;
2121 return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
2122}
2123
Andreas Gruenbacher82e938b2020-11-25 23:37:18 +01002124static int gfs2_update_time(struct inode *inode, struct timespec64 *time,
2125 int flags)
2126{
2127 struct gfs2_inode *ip = GFS2_I(inode);
2128 struct gfs2_glock *gl = ip->i_gl;
2129 struct gfs2_holder *gh;
2130 int error;
2131
2132 gh = gfs2_glock_is_locked_by_me(gl);
2133 if (gh && !gfs2_glock_is_held_excl(gl)) {
2134 gfs2_glock_dq(gh);
2135 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, gh);
2136 error = gfs2_glock_nq(gh);
2137 if (error)
2138 return error;
2139 }
2140 return generic_update_time(inode, time, flags);
2141}
2142
Andreas Gruenbachere3a77ee2020-11-25 21:14:15 +01002143static const struct inode_operations gfs2_file_iops = {
Al Viroe6305c42008-07-15 21:03:57 -04002144 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002145 .setattr = gfs2_setattr,
2146 .getattr = gfs2_getattr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002147 .listxattr = gfs2_listxattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002148 .fiemap = gfs2_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02002149 .get_acl = gfs2_get_acl,
Christoph Hellwige01580b2013-12-20 05:16:52 -08002150 .set_acl = gfs2_set_acl,
Andreas Gruenbacher82e938b2020-11-25 23:37:18 +01002151 .update_time = gfs2_update_time,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002152};
2153
Andreas Gruenbachere3a77ee2020-11-25 21:14:15 +01002154static const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00002155 .create = gfs2_create,
2156 .lookup = gfs2_lookup,
2157 .link = gfs2_link,
2158 .unlink = gfs2_unlink,
2159 .symlink = gfs2_symlink,
2160 .mkdir = gfs2_mkdir,
Steven Whitehouse855d23c2011-05-09 16:42:37 +01002161 .rmdir = gfs2_unlink,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002162 .mknod = gfs2_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02002163 .rename = gfs2_rename2,
Al Viroe6305c42008-07-15 21:03:57 -04002164 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002165 .setattr = gfs2_setattr,
2166 .getattr = gfs2_getattr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002167 .listxattr = gfs2_listxattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002168 .fiemap = gfs2_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02002169 .get_acl = gfs2_get_acl,
Christoph Hellwige01580b2013-12-20 05:16:52 -08002170 .set_acl = gfs2_set_acl,
Andreas Gruenbacher82e938b2020-11-25 23:37:18 +01002171 .update_time = gfs2_update_time,
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01002172 .atomic_open = gfs2_atomic_open,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002173};
2174
Andreas Gruenbachere3a77ee2020-11-25 21:14:15 +01002175static const struct inode_operations gfs2_symlink_iops = {
Al Viro6b255392015-11-17 10:20:54 -05002176 .get_link = gfs2_get_link,
Al Viroe6305c42008-07-15 21:03:57 -04002177 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002178 .setattr = gfs2_setattr,
2179 .getattr = gfs2_getattr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002180 .listxattr = gfs2_listxattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002181 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002182};
2183