blob: 4842f313a8084292900fdb231b664e98fdb6d86d [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 Gruenbachercda9dd42016-06-14 12:24:50 -050039static int iget_test(struct inode *inode, void *opaque)
Steven Whitehouse194c0112011-05-09 14:06:38 +010040{
Andreas Gruenbachercda9dd42016-06-14 12:24:50 -050041 u64 no_addr = *(u64 *)opaque;
42
43 return GFS2_I(inode)->i_no_addr == no_addr;
44}
45
46static int iget_set(struct inode *inode, void *opaque)
47{
48 u64 no_addr = *(u64 *)opaque;
49
50 GFS2_I(inode)->i_no_addr = no_addr;
51 inode->i_ino = no_addr;
52 return 0;
53}
54
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -050055static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
56{
57 struct inode *inode;
58
59repeat:
Andreas Gruenbachercda9dd42016-06-14 12:24:50 -050060 inode = iget5_locked(sb, no_addr, iget_test, iget_set, &no_addr);
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -050061 if (!inode)
62 return inode;
63 if (is_bad_inode(inode)) {
64 iput(inode);
65 goto repeat;
66 }
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -050067 return inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +010068}
69
70/**
71 * gfs2_set_iop - Sets inode operations
72 * @inode: The inode with correct i_mode filled in
73 *
74 * GFS2 lookup code fills in vfs inode contents based on info obtained
75 * from directory entry inside gfs2_inode_lookup().
76 */
77
78static void gfs2_set_iop(struct inode *inode)
79{
80 struct gfs2_sbd *sdp = GFS2_SB(inode);
81 umode_t mode = inode->i_mode;
82
83 if (S_ISREG(mode)) {
84 inode->i_op = &gfs2_file_iops;
85 if (gfs2_localflocks(sdp))
86 inode->i_fop = &gfs2_file_fops_nolock;
87 else
88 inode->i_fop = &gfs2_file_fops;
89 } else if (S_ISDIR(mode)) {
90 inode->i_op = &gfs2_dir_iops;
91 if (gfs2_localflocks(sdp))
92 inode->i_fop = &gfs2_dir_fops_nolock;
93 else
94 inode->i_fop = &gfs2_dir_fops;
95 } else if (S_ISLNK(mode)) {
96 inode->i_op = &gfs2_symlink_iops;
97 } else {
98 inode->i_op = &gfs2_file_iops;
99 init_special_inode(inode, inode->i_mode, inode->i_rdev);
100 }
101}
102
103/**
104 * gfs2_inode_lookup - Lookup an inode
105 * @sb: The super block
Steven Whitehouse194c0112011-05-09 14:06:38 +0100106 * @type: The type of the inode
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500107 * @no_addr: The inode number
108 * @no_formal_ino: The inode generation number
109 * @blktype: Requested block type (GFS2_BLKST_DINODE or GFS2_BLKST_UNLINKED;
Andreas Gruenbacher61b91cf2017-08-01 09:54:33 -0500110 * GFS2_BLKST_FREE to indicate not to verify)
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500111 *
112 * If @type is DT_UNKNOWN, the inode type is fetched from disk.
113 *
114 * If @blktype is anything other than GFS2_BLKST_FREE (which is used as a
115 * placeholder because it doesn't otherwise make sense), the on-disk block type
116 * is verified to be @blktype.
Steven Whitehouse194c0112011-05-09 14:06:38 +0100117 *
118 * Returns: A VFS inode, or an error
119 */
120
121struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500122 u64 no_addr, u64 no_formal_ino,
123 unsigned int blktype)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100124{
125 struct inode *inode;
126 struct gfs2_inode *ip;
127 struct gfs2_glock *io_gl = NULL;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500128 struct gfs2_holder i_gh;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100129 int error;
130
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500131 gfs2_holder_mark_uninitialized(&i_gh);
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500132 inode = gfs2_iget(sb, no_addr);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100133 if (!inode)
Steven Whitehouseac3beb62014-01-16 10:31:13 +0000134 return ERR_PTR(-ENOMEM);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100135
Bob Petersone97321f2016-04-12 16:14:26 -0400136 ip = GFS2_I(inode);
Bob Petersone97321f2016-04-12 16:14:26 -0400137
Steven Whitehouse194c0112011-05-09 14:06:38 +0100138 if (inode->i_state & I_NEW) {
139 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100140
141 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
142 if (unlikely(error))
143 goto fail;
Andreas Gruenbacher4fd1a572017-06-30 07:47:15 -0500144 flush_delayed_work(&ip->i_gl->gl_work);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100145
146 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
147 if (unlikely(error))
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100148 goto fail;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100149
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500150 if (type == DT_UNKNOWN || blktype != GFS2_BLKST_FREE) {
151 /*
152 * The GL_SKIP flag indicates to skip reading the inode
153 * block. We read the inode with gfs2_inode_refresh
154 * after possibly checking the block type.
155 */
156 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE,
157 GL_SKIP, &i_gh);
158 if (error)
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100159 goto fail;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500160
161 if (blktype != GFS2_BLKST_FREE) {
162 error = gfs2_check_blk_type(sdp, no_addr,
163 blktype);
164 if (error)
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100165 goto fail;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500166 }
167 }
168
Bob Peterson4d7c18c2017-07-18 12:15:01 -0500169 glock_set_object(ip->i_gl, ip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100170 set_bit(GIF_INVALID, &ip->i_flags);
171 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
172 if (unlikely(error))
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100173 goto fail;
Andreas Gruenbacher4fd1a572017-06-30 07:47:15 -0500174 glock_set_object(ip->i_iopen_gh.gh_gl, ip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100175 gfs2_glock_put(io_gl);
176 io_gl = NULL;
177
Andreas Gruenbacher2b0fb352020-01-15 06:26:00 +0100178 /* Lowest possible timestamp; will be overwritten in gfs2_dinode_in. */
179 inode->i_atime.tv_sec = 1LL << (8 * sizeof(inode->i_atime.tv_sec) - 1);
180 inode->i_atime.tv_nsec = 0;
181
Steven Whitehouse194c0112011-05-09 14:06:38 +0100182 if (type == DT_UNKNOWN) {
183 /* Inode glock must be locked already */
184 error = gfs2_inode_refresh(GFS2_I(inode));
185 if (error)
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100186 goto fail;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100187 } else {
Andreas Gruenbacher2b0fb352020-01-15 06:26:00 +0100188 ip->i_no_formal_ino = no_formal_ino;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100189 inode->i_mode = DT2IF(type);
190 }
191
192 gfs2_set_iop(inode);
Andreas Gruenbacher332f51d2016-09-26 13:24:34 -0500193
Steven Whitehouse194c0112011-05-09 14:06:38 +0100194 unlock_new_inode(inode);
195 }
196
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500197 if (gfs2_holder_initialized(&i_gh))
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500198 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100199 return inode;
200
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +0100201fail:
Steven Whitehouse194c0112011-05-09 14:06:38 +0100202 if (io_gl)
203 gfs2_glock_put(io_gl);
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500204 if (gfs2_holder_initialized(&i_gh))
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500205 gfs2_glock_dq_uninit(&i_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100206 iget_failed(inode);
207 return ERR_PTR(error);
208}
209
210struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
211 u64 *no_formal_ino, unsigned int blktype)
212{
213 struct super_block *sb = sdp->sd_vfs;
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500214 struct inode *inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100215 int error;
216
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500217 inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, blktype);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100218 if (IS_ERR(inode))
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500219 return inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100220
221 /* Two extra checks for NFS only */
222 if (no_formal_ino) {
223 error = -ESTALE;
224 if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
225 goto fail_iput;
226
227 error = -EIO;
228 if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
229 goto fail_iput;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100230 }
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500231 return inode;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100232
Steven Whitehouse194c0112011-05-09 14:06:38 +0100233fail_iput:
234 iput(inode);
Andreas Gruenbacher3ce37b22016-06-14 12:22:27 -0500235 return ERR_PTR(error);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100236}
237
238
239struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
240{
241 struct qstr qstr;
242 struct inode *inode;
243 gfs2_str2qstr(&qstr, name);
244 inode = gfs2_lookupi(dip, &qstr, 1);
245 /* gfs2_lookupi has inconsistent callers: vfs
246 * related routines expect NULL for no entry found,
247 * gfs2_lookup_simple callers expect ENOENT
248 * and do not check for NULL.
249 */
250 if (inode == NULL)
251 return ERR_PTR(-ENOENT);
252 else
253 return inode;
254}
255
256
257/**
258 * gfs2_lookupi - Look up a filename in a directory and return its inode
259 * @d_gh: An initialized holder for the directory glock
260 * @name: The name of the inode to look for
261 * @is_root: If 1, ignore the caller's permissions
262 * @i_gh: An uninitialized holder for the new inode glock
263 *
264 * This can be called via the VFS filldir function when NFS is doing
265 * a readdirplus and the inode which its intending to stat isn't
266 * already in cache. In this case we must not take the directory glock
267 * again, since the readdir call will have already taken that lock.
268 *
269 * Returns: errno
270 */
271
272struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
273 int is_root)
274{
275 struct super_block *sb = dir->i_sb;
276 struct gfs2_inode *dip = GFS2_I(dir);
277 struct gfs2_holder d_gh;
278 int error = 0;
279 struct inode *inode = NULL;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100280
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500281 gfs2_holder_mark_uninitialized(&d_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100282 if (!name->len || name->len > GFS2_FNAMESIZE)
283 return ERR_PTR(-ENAMETOOLONG);
284
285 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
286 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
David Howells2b0143b2015-03-17 22:25:59 +0000287 dir == d_inode(sb->s_root))) {
Steven Whitehouse194c0112011-05-09 14:06:38 +0100288 igrab(dir);
289 return dir;
290 }
291
292 if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
293 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
294 if (error)
295 return ERR_PTR(error);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100296 }
297
298 if (!is_root) {
Al Viro10556cb22011-06-20 19:28:19 -0400299 error = gfs2_permission(dir, MAY_EXEC);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100300 if (error)
301 goto out;
302 }
303
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100304 inode = gfs2_dir_search(dir, name, false);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100305 if (IS_ERR(inode))
306 error = PTR_ERR(inode);
307out:
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500308 if (gfs2_holder_initialized(&d_gh))
Steven Whitehouse194c0112011-05-09 14:06:38 +0100309 gfs2_glock_dq_uninit(&d_gh);
310 if (error == -ENOENT)
311 return NULL;
312 return inode ? inode : ERR_PTR(error);
313}
314
315/**
316 * create_ok - OK to create a new on-disk inode here?
317 * @dip: Directory in which dinode is to be created
318 * @name: Name of new dinode
319 * @mode:
320 *
321 * Returns: errno
322 */
323
324static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
Al Viro175a4eb2011-07-26 03:30:54 -0400325 umode_t mode)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100326{
327 int error;
328
Al Viro10556cb22011-06-20 19:28:19 -0400329 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100330 if (error)
331 return error;
332
333 /* Don't create entries in an unlinked directory */
334 if (!dip->i_inode.i_nlink)
335 return -ENOENT;
336
Steven Whitehouse194c0112011-05-09 14:06:38 +0100337 if (dip->i_entries == (u32)-1)
338 return -EFBIG;
339 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
340 return -EMLINK;
341
342 return 0;
343}
344
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000345static void munge_mode_uid_gid(const struct gfs2_inode *dip,
346 struct inode *inode)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100347{
348 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800349 (dip->i_inode.i_mode & S_ISUID) &&
350 !uid_eq(dip->i_inode.i_uid, GLOBAL_ROOT_UID)) {
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000351 if (S_ISDIR(inode->i_mode))
352 inode->i_mode |= S_ISUID;
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -0800353 else if (!uid_eq(dip->i_inode.i_uid, current_fsuid()))
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000354 inode->i_mode &= ~07111;
355 inode->i_uid = dip->i_inode.i_uid;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100356 } else
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000357 inode->i_uid = current_fsuid();
Steven Whitehouse194c0112011-05-09 14:06:38 +0100358
359 if (dip->i_inode.i_mode & S_ISGID) {
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000360 if (S_ISDIR(inode->i_mode))
361 inode->i_mode |= S_ISGID;
362 inode->i_gid = dip->i_inode.i_gid;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100363 } else
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000364 inode->i_gid = current_fsgid();
Steven Whitehouse194c0112011-05-09 14:06:38 +0100365}
366
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000367static int alloc_dinode(struct gfs2_inode *ip, u32 flags, unsigned *dblocks)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100368{
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000369 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000370 struct gfs2_alloc_parms ap = { .target = *dblocks, .aflags = flags, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100371 int error;
372
Abhi Dasb8fbf472015-03-18 12:03:41 -0500373 error = gfs2_quota_lock_check(ip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100374 if (error)
375 goto out;
376
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100377 error = gfs2_inplace_reserve(ip, &ap);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000378 if (error)
379 goto out_quota;
380
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000381 error = gfs2_trans_begin(sdp, (*dblocks * RES_RG_BIT) + RES_STATFS + RES_QUOTA, 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100382 if (error)
383 goto out_ipreserv;
384
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000385 error = gfs2_alloc_blocks(ip, &ip->i_no_addr, dblocks, 1, &ip->i_generation);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000386 ip->i_no_formal_ino = ip->i_generation;
387 ip->i_inode.i_ino = ip->i_no_addr;
388 ip->i_goal = ip->i_no_addr;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100389
390 gfs2_trans_end(sdp);
391
392out_ipreserv:
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000393 gfs2_inplace_release(ip);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000394out_quota:
395 gfs2_quota_unlock(ip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100396out:
Steven Whitehouse194c0112011-05-09 14:06:38 +0100397 return error;
398}
399
Steven Whitehousef2741d92011-05-13 12:11:17 +0100400static void gfs2_init_dir(struct buffer_head *dibh,
401 const struct gfs2_inode *parent)
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100402{
403 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
404 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
405
406 gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
407 dent->de_inum = di->di_num; /* already GFS2 endian */
408 dent->de_type = cpu_to_be16(DT_DIR);
409
410 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
411 gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
412 gfs2_inum_out(parent, dent);
413 dent->de_type = cpu_to_be16(DT_DIR);
414
415}
416
Steven Whitehouse194c0112011-05-09 14:06:38 +0100417/**
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000418 * gfs2_init_xattr - Initialise an xattr block for a new inode
419 * @ip: The inode in question
420 *
421 * This sets up an empty xattr block for a new inode, ready to
422 * take any ACLs, LSM xattrs, etc.
423 */
424
425static void gfs2_init_xattr(struct gfs2_inode *ip)
426{
427 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
428 struct buffer_head *bh;
429 struct gfs2_ea_header *ea;
430
431 bh = gfs2_meta_new(ip->i_gl, ip->i_eattr);
432 gfs2_trans_add_meta(ip->i_gl, bh);
433 gfs2_metatype_set(bh, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
434 gfs2_buffer_clear_tail(bh, sizeof(struct gfs2_meta_header));
435
436 ea = GFS2_EA_BH2FIRST(bh);
437 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
438 ea->ea_type = GFS2_EATYPE_UNUSED;
439 ea->ea_flags = GFS2_EAFLAG_LAST;
440
441 brelse(bh);
442}
443
444/**
Steven Whitehouse194c0112011-05-09 14:06:38 +0100445 * init_dinode - Fill in a new dinode structure
Steven Whitehousef2741d92011-05-13 12:11:17 +0100446 * @dip: The directory this inode is being created in
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000447 * @ip: The inode
Steven Whitehousef2741d92011-05-13 12:11:17 +0100448 * @symname: The symlink destination (if a symlink)
Steven Whitehousef2741d92011-05-13 12:11:17 +0100449 * @bhp: The buffer head (returned to caller)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100450 *
451 */
452
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000453static void init_dinode(struct gfs2_inode *dip, struct gfs2_inode *ip,
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000454 const char *symname)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100455{
Steven Whitehouse194c0112011-05-09 14:06:38 +0100456 struct gfs2_dinode *di;
457 struct buffer_head *dibh;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100458
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000459 dibh = gfs2_meta_new(ip->i_gl, ip->i_no_addr);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000460 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100461 di = (struct gfs2_dinode *)dibh->b_data;
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000462 gfs2_dinode_out(ip, di);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100463
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000464 di->di_major = cpu_to_be32(MAJOR(ip->i_inode.i_rdev));
465 di->di_minor = cpu_to_be32(MINOR(ip->i_inode.i_rdev));
Steven Whitehouse194c0112011-05-09 14:06:38 +0100466 di->__pad1 = 0;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100467 di->__pad2 = 0;
468 di->__pad3 = 0;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100469 memset(&di->__pad4, 0, sizeof(di->__pad4));
Steven Whitehouse194c0112011-05-09 14:06:38 +0100470 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000471 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
Steven Whitehouse160b4022011-05-13 10:34:59 +0100472
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000473 switch(ip->i_inode.i_mode & S_IFMT) {
Steven Whitehouse160b4022011-05-13 10:34:59 +0100474 case S_IFDIR:
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100475 gfs2_init_dir(dibh, dip);
Steven Whitehouse160b4022011-05-13 10:34:59 +0100476 break;
477 case S_IFLNK:
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000478 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname, ip->i_inode.i_size);
Steven Whitehouse160b4022011-05-13 10:34:59 +0100479 break;
Steven Whitehousee2d0a132011-05-13 09:55:55 +0100480 }
481
Steven Whitehouse194c0112011-05-09 14:06:38 +0100482 set_buffer_uptodate(dibh);
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000483 brelse(dibh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100484}
485
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000486/**
487 * gfs2_trans_da_blocks - Calculate number of blocks to link inode
488 * @dip: The directory we are linking into
489 * @da: The dir add information
490 * @nr_inodes: The number of inodes involved
491 *
492 * This calculate the number of blocks we need to reserve in a
493 * transaction to link @nr_inodes into a directory. In most cases
494 * @nr_inodes will be 2 (the directory plus the inode being linked in)
495 * but in case of rename, 4 may be required.
496 *
497 * Returns: Number of blocks
498 */
499
500static unsigned gfs2_trans_da_blks(const struct gfs2_inode *dip,
501 const struct gfs2_diradd *da,
502 unsigned nr_inodes)
503{
504 return da->nr_blocks + gfs2_rg_blocks(dip, da->nr_blocks) +
505 (nr_inodes * RES_DINODE) + RES_QUOTA + RES_STATFS;
506}
507
Steven Whitehouse194c0112011-05-09 14:06:38 +0100508static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000509 struct gfs2_inode *ip, struct gfs2_diradd *da)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100510{
511 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000512 struct gfs2_alloc_parms ap = { .target = da->nr_blocks, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100513 int error;
514
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000515 if (da->nr_blocks) {
Abhi Dasb8fbf472015-03-18 12:03:41 -0500516 error = gfs2_quota_lock_check(dip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100517 if (error)
518 goto fail_quota_locks;
519
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100520 error = gfs2_inplace_reserve(dip, &ap);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100521 if (error)
522 goto fail_quota_locks;
523
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000524 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, da, 2), 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100525 if (error)
526 goto fail_ipreserv;
527 } else {
528 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
529 if (error)
530 goto fail_quota_locks;
531 }
532
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000533 error = gfs2_dir_add(&dip->i_inode, name, ip, da);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100534
Steven Whitehouse194c0112011-05-09 14:06:38 +0100535 gfs2_trans_end(sdp);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100536fail_ipreserv:
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000537 gfs2_inplace_release(dip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100538fail_quota_locks:
539 gfs2_quota_unlock(dip);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100540 return error;
541}
542
H Hartley Sweeten46cc1e52011-09-23 15:51:32 -0700543static int gfs2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -0400544 void *fs_info)
545{
546 const struct xattr *xattr;
547 int err = 0;
548
549 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
550 err = __gfs2_xattr_set(inode, xattr->name, xattr->value,
551 xattr->value_len, 0,
552 GFS2_EATYPE_SECURITY);
553 if (err < 0)
554 break;
555 }
556 return err;
557}
558
Steven Whitehouse194c0112011-05-09 14:06:38 +0100559/**
Steven Whitehousef2741d92011-05-13 12:11:17 +0100560 * gfs2_create_inode - Create a new inode
561 * @dir: The parent directory
562 * @dentry: The new dentry
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100563 * @file: If non-NULL, the file which is being opened
Steven Whitehousef2741d92011-05-13 12:11:17 +0100564 * @mode: The permissions on the new inode
565 * @dev: For device nodes, this is the device number
566 * @symname: For symlinks, this is the link destination
567 * @size: The initial size of the inode (ignored for directories)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100568 *
Steven Whitehousef2741d92011-05-13 12:11:17 +0100569 * Returns: 0 on success, or error code
Steven Whitehouse194c0112011-05-09 14:06:38 +0100570 */
571
Steven Whitehousef2741d92011-05-13 12:11:17 +0100572static int gfs2_create_inode(struct inode *dir, struct dentry *dentry,
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100573 struct file *file,
Al Viro175a4eb2011-07-26 03:30:54 -0400574 umode_t mode, dev_t dev, const char *symname,
Al Virob452a452018-06-08 13:06:28 -0400575 unsigned int size, int excl)
Steven Whitehouse194c0112011-05-09 14:06:38 +0100576{
Steven Whitehousef2741d92011-05-13 12:11:17 +0100577 const struct qstr *name = &dentry->d_name;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800578 struct posix_acl *default_acl, *acl;
Steven Whitehousef2741d92011-05-13 12:11:17 +0100579 struct gfs2_holder ghs[2];
Steven Whitehouse194c0112011-05-09 14:06:38 +0100580 struct inode *inode = NULL;
Bob Peterson8e2e0042012-07-19 08:12:40 -0400581 struct gfs2_inode *dip = GFS2_I(dir), *ip;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100582 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
Bob Petersona4923862015-12-07 16:24:27 -0600583 struct gfs2_glock *io_gl = NULL;
Bob Peterson783013c2015-12-04 10:19:14 -0600584 int error, free_vfs_inode = 1;
Steven Whitehouse9dbe9612012-10-31 10:37:10 +0000585 u32 aflags = 0;
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000586 unsigned blocks = 1;
Bob Peterson19aeb5a62014-09-29 08:52:04 -0400587 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
Steven Whitehouse194c0112011-05-09 14:06:38 +0100588
589 if (!name->len || name->len > GFS2_FNAMESIZE)
Steven Whitehousef2741d92011-05-13 12:11:17 +0100590 return -ENAMETOOLONG;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100591
Bob Peterson2fba46a2020-02-27 12:47:53 -0600592 error = gfs2_qa_get(dip);
Bob Peterson0a305e42012-06-06 11:17:59 +0100593 if (error)
594 return error;
595
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000596 error = gfs2_rindex_update(sdp);
597 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -0600598 goto fail;
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000599
Steven Whitehousef2741d92011-05-13 12:11:17 +0100600 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100601 if (error)
602 goto fail;
Andreas Gruenbachere0b62e22017-06-30 08:16:46 -0500603 gfs2_holder_mark_uninitialized(ghs + 1);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100604
605 error = create_ok(dip, name, mode);
606 if (error)
607 goto fail_gunlock;
608
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100609 inode = gfs2_dir_search(dir, &dentry->d_name, !S_ISREG(mode) || excl);
610 error = PTR_ERR(inode);
611 if (!IS_ERR(inode)) {
Al Viro571a4b52014-11-19 19:34:49 +0000612 if (S_ISDIR(inode->i_mode)) {
613 iput(inode);
614 inode = ERR_PTR(-EISDIR);
615 goto fail_gunlock;
616 }
Al Viro44bb31b2014-11-19 19:35:24 +0000617 d_instantiate(dentry, inode);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100618 error = 0;
Miklos Szeredi0d0d1102013-09-16 14:52:00 +0200619 if (file) {
Al Viro44bb31b2014-11-19 19:35:24 +0000620 if (S_ISREG(inode->i_mode))
Al Virobe12af32018-06-08 11:44:56 -0400621 error = finish_open(file, dentry, gfs2_open_common);
Al Viro44bb31b2014-11-19 19:35:24 +0000622 else
623 error = finish_no_open(file, NULL);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100624 }
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100625 gfs2_glock_dq_uninit(ghs);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100626 return error;
Steven Whitehouse5a00f3c2013-06-11 13:45:29 +0100627 } else if (error != -ENOENT) {
628 goto fail_gunlock;
629 }
630
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000631 error = gfs2_diradd_alloc_required(dir, name, &da);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000632 if (error < 0)
633 goto fail_gunlock;
634
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000635 inode = new_inode(sdp->sd_vfs);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000636 error = -ENOMEM;
637 if (!inode)
638 goto fail_gunlock;
639
Christoph Hellwige01580b2013-12-20 05:16:52 -0800640 error = posix_acl_create(dir, &mode, &default_acl, &acl);
641 if (error)
Bob Peterson783013c2015-12-04 10:19:14 -0600642 goto fail_gunlock;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800643
Bob Peterson8e2e0042012-07-19 08:12:40 -0400644 ip = GFS2_I(inode);
Bob Peterson2fba46a2020-02-27 12:47:53 -0600645 error = gfs2_qa_get(ip);
Steven Whitehouseff7f4cb2012-09-10 10:03:50 +0100646 if (error)
Christoph Hellwige01580b2013-12-20 05:16:52 -0800647 goto fail_free_acls;
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000648
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000649 inode->i_mode = mode;
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000650 set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000651 inode->i_rdev = dev;
652 inode->i_size = size;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700653 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000654 munge_mode_uid_gid(dip, inode);
Abhi Das00a158b2014-09-18 21:40:28 -0500655 check_and_update_goal(dip);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000656 ip->i_goal = dip->i_goal;
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000657 ip->i_diskflags = 0;
658 ip->i_eattr = 0;
659 ip->i_height = 0;
660 ip->i_depth = 0;
661 ip->i_entries = 0;
Bob Petersoncc963a12017-03-16 15:29:13 -0400662 ip->i_no_addr = 0; /* Temporarily zero until real addr is assigned */
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000663
664 switch(mode & S_IFMT) {
665 case S_IFREG:
666 if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
667 gfs2_tune_get(sdp, gt_new_files_jdata))
668 ip->i_diskflags |= GFS2_DIF_JDATA;
669 gfs2_set_aops(inode);
670 break;
671 case S_IFDIR:
672 ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA);
673 ip->i_diskflags |= GFS2_DIF_JDATA;
674 ip->i_entries = 2;
675 break;
676 }
Abhi Dasacc546f2015-11-10 15:07:26 -0600677
678 /* Force SYSTEM flag on all files and subdirs of a SYSTEM directory */
679 if (dip->i_diskflags & GFS2_DIF_SYSTEM)
680 ip->i_diskflags |= GFS2_DIF_SYSTEM;
681
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000682 gfs2_set_inode_flags(inode);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000683
David Howells2b0143b2015-03-17 22:25:59 +0000684 if ((GFS2_I(d_inode(sdp->sd_root_dir)) == dip) ||
Steven Whitehouse9dbe9612012-10-31 10:37:10 +0000685 (dip->i_diskflags & GFS2_DIF_TOPDIR))
686 aflags |= GFS2_AF_ORLOV;
687
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000688 if (default_acl || acl)
689 blocks++;
690
691 error = alloc_dinode(ip, aflags, &blocks);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000692 if (error)
693 goto fail_free_inode;
694
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000695 gfs2_set_inode_blocks(inode, blocks);
696
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000697 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
698 if (error)
699 goto fail_free_inode;
Andreas Gruenbacher98e5a912017-07-19 11:10:19 -0500700 flush_delayed_work(&ip->i_gl->gl_work);
Andreas Gruenbacher6f6597ba2017-06-30 07:55:08 -0500701 glock_set_object(ip->i_gl, ip);
Andreas Gruenbacher98e5a912017-07-19 11:10:19 -0500702
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000703 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
704 if (error)
705 goto fail_free_inode;
706
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000707 error = gfs2_trans_begin(sdp, blocks, 0);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000708 if (error)
Bob Peterson2c47c1b2019-11-19 11:40:46 -0500709 goto fail_free_inode;
Bob Peterson0a305e42012-06-06 11:17:59 +0100710
Steven Whitehouseb2c8b3e2014-02-04 15:45:11 +0000711 if (blocks > 1) {
712 ip->i_eattr = ip->i_no_addr + 1;
713 gfs2_init_xattr(ip);
714 }
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000715 init_dinode(dip, ip, symname);
Steven Whitehousefd4b4e02013-02-26 16:15:20 +0000716 gfs2_trans_end(sdp);
717
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000718 error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
719 if (error)
Bob Peterson2c47c1b2019-11-19 11:40:46 -0500720 goto fail_free_inode;
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000721
Bob Petersona4923862015-12-07 16:24:27 -0600722 BUG_ON(test_and_set_bit(GLF_INODE_CREATING, &io_gl->gl_flags));
723
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000724 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
725 if (error)
726 goto fail_gunlock2;
727
Andreas Gruenbacher6f6597ba2017-06-30 07:55:08 -0500728 glock_set_object(ip->i_iopen_gh.gh_gl, ip);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000729 gfs2_set_iop(inode);
730 insert_inode_hash(inode);
731
Bob Peterson783013c2015-12-04 10:19:14 -0600732 free_vfs_inode = 0; /* After this point, the inode is no longer
733 considered free. Any failures need to undo
734 the gfs2 structures. */
Christoph Hellwige01580b2013-12-20 05:16:52 -0800735 if (default_acl) {
Al Viro1a39ba92016-05-13 03:59:17 +0200736 error = __gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100737 if (error)
738 goto fail_gunlock3;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800739 posix_acl_release(default_acl);
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100740 default_acl = NULL;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800741 }
742 if (acl) {
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100743 error = __gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS);
744 if (error)
745 goto fail_gunlock3;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800746 posix_acl_release(acl);
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100747 acl = NULL;
Christoph Hellwige01580b2013-12-20 05:16:52 -0800748 }
749
Bob Petersonf45dc262014-03-19 09:37:00 -0400750 error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name,
751 &gfs2_initxattrs, NULL);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100752 if (error)
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000753 goto fail_gunlock3;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100754
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000755 error = link_dinode(dip, name, ip, &da);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100756 if (error)
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000757 goto fail_gunlock3;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100758
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000759 mark_inode_dirty(inode);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100760 d_instantiate(dentry, inode);
Bob Peterson2c47c1b2019-11-19 11:40:46 -0500761 /* After instantiate, errors should result in evict which will destroy
762 * both inode and iopen glocks properly. */
Miklos Szeredic5bf8fe2013-09-16 14:52:03 +0200763 if (file) {
Al Viro73a09dd2018-06-08 13:22:02 -0400764 file->f_mode |= FMODE_CREATED;
Al Virobe12af32018-06-08 11:44:56 -0400765 error = finish_open(file, dentry, gfs2_open_common);
Miklos Szeredic5bf8fe2013-09-16 14:52:03 +0200766 }
Steven Whitehouse28fb3022013-02-26 19:09:35 +0000767 gfs2_glock_dq_uninit(ghs);
768 gfs2_glock_dq_uninit(ghs + 1);
Bob Petersona4923862015-12-07 16:24:27 -0600769 clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
Bob Peterson2c47c1b2019-11-19 11:40:46 -0500770 gfs2_glock_put(io_gl);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100771 return error;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100772
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000773fail_gunlock3:
Bob Peterson9c1b2802017-07-18 12:26:07 -0500774 glock_clear_object(io_gl, ip);
Bob Peterson783013c2015-12-04 10:19:14 -0600775 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100776fail_gunlock2:
Bob Peterson2c47c1b2019-11-19 11:40:46 -0500777 clear_bit(GLF_INODE_CREATING, &io_gl->gl_flags);
778 gfs2_glock_put(io_gl);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000779fail_free_inode:
Bob Peterson2fba46a2020-02-27 12:47:53 -0600780 gfs2_qa_put(ip);
Bob Peterson9c1b2802017-07-18 12:26:07 -0500781 if (ip->i_gl) {
782 glock_clear_object(ip->i_gl, ip);
Steven Whitehousec9aecf72012-10-31 10:30:22 +0000783 gfs2_glock_put(ip->i_gl);
Bob Peterson9c1b2802017-07-18 12:26:07 -0500784 }
Andreas Gruenbacher15955482020-03-06 10:32:35 -0600785 gfs2_rs_delete(ip, NULL);
786 gfs2_qa_put(ip);
Christoph Hellwige01580b2013-12-20 05:16:52 -0800787fail_free_acls:
Andreas Gruenbacher6ff9b092018-11-26 18:45:35 +0100788 posix_acl_release(default_acl);
789 posix_acl_release(acl);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100790fail_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000791 gfs2_dir_no_add(&da);
Steven Whitehousef2741d92011-05-13 12:11:17 +0100792 gfs2_glock_dq_uninit(ghs);
Kefeng Wang15a798f2019-06-05 22:24:24 +0800793 if (!IS_ERR_OR_NULL(inode)) {
Steven Whitehouse79ba7482013-03-01 09:29:12 +0000794 clear_nlink(inode);
Abhi Das05978802014-03-31 10:33:17 -0500795 if (!free_vfs_inode)
796 mark_inode_dirty(inode);
797 set_bit(free_vfs_inode ? GIF_FREE_VFS_INODE : GIF_ALLOC_FAILED,
798 &GFS2_I(inode)->i_flags);
Steven Whitehouse40ac2182011-08-02 13:17:27 +0100799 iput(inode);
800 }
Andreas Gruenbachere0b62e22017-06-30 08:16:46 -0500801 if (gfs2_holder_initialized(ghs + 1))
802 gfs2_glock_dq_uninit(ghs + 1);
Steven Whitehouse194c0112011-05-09 14:06:38 +0100803fail:
Bob Peterson2fba46a2020-02-27 12:47:53 -0600804 gfs2_qa_put(dip);
Steven Whitehousef2741d92011-05-13 12:11:17 +0100805 return error;
Steven Whitehouse194c0112011-05-09 14:06:38 +0100806}
Steven Whitehousef2741d92011-05-13 12:11:17 +0100807
David Teiglandb3b94fa2006-01-16 16:50:04 +0000808/**
809 * gfs2_create - Create a file
810 * @dir: The directory in which to create the file
811 * @dentry: The dentry of the new file
812 * @mode: The mode of the new file
813 *
814 * Returns: errno
815 */
816
817static int gfs2_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -0400818 umode_t mode, bool excl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000819{
Al Virob452a452018-06-08 13:06:28 -0400820 return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, excl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000821}
822
823/**
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100824 * __gfs2_lookup - Look up a filename in a directory and return its inode
David Teiglandb3b94fa2006-01-16 16:50:04 +0000825 * @dir: The directory inode
826 * @dentry: The dentry of the new inode
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100827 * @file: File to be opened
David Teiglandb3b94fa2006-01-16 16:50:04 +0000828 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000829 *
830 * Returns: errno
831 */
832
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100833static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry,
Al Virob452a452018-06-08 13:06:28 -0400834 struct file *file)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000835{
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100836 struct inode *inode;
837 struct dentry *d;
838 struct gfs2_holder gh;
839 struct gfs2_glock *gl;
840 int error;
841
842 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
Benjamin Coddington7b7a9112014-09-10 14:09:20 -0400843 if (inode == NULL) {
844 d_add(dentry, NULL);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100845 return NULL;
Benjamin Coddington7b7a9112014-09-10 14:09:20 -0400846 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100847 if (IS_ERR(inode))
848 return ERR_CAST(inode);
849
850 gl = GFS2_I(inode)->i_gl;
851 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
852 if (error) {
853 iput(inode);
854 return ERR_PTR(error);
Steven Whitehouse9656b2c2008-01-08 08:14:30 +0000855 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100856
857 d = d_splice_alias(inode, dentry);
J. Bruce Fieldsd57b9c92014-01-16 13:51:07 -0500858 if (IS_ERR(d)) {
J. Bruce Fieldsd57b9c92014-01-16 13:51:07 -0500859 gfs2_glock_dq_uninit(&gh);
860 return d;
861 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100862 if (file && S_ISREG(inode->i_mode))
Al Virobe12af32018-06-08 11:44:56 -0400863 error = finish_open(file, dentry, gfs2_open_common);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100864
865 gfs2_glock_dq_uninit(&gh);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +0100866 if (error) {
867 dput(d);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100868 return ERR_PTR(error);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +0100869 }
Steven Whitehouse6d4ade92013-06-14 11:17:15 +0100870 return d;
871}
872
873static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
874 unsigned flags)
875{
Al Virob452a452018-06-08 13:06:28 -0400876 return __gfs2_lookup(dir, dentry, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000877}
878
879/**
880 * gfs2_link - Link to a file
881 * @old_dentry: The inode to link
882 * @dir: Add link to this directory
883 * @dentry: The name of the link
884 *
885 * Link the inode in "old_dentry" into the directory "dir" with the
886 * name in "dentry".
887 *
888 * Returns: errno
889 */
890
891static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
892 struct dentry *dentry)
893{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400894 struct gfs2_inode *dip = GFS2_I(dir);
895 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +0000896 struct inode *inode = d_inode(old_dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400897 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000898 struct gfs2_holder ghs[2];
Steven Whitehouse2baee032011-05-09 12:08:36 +0100899 struct buffer_head *dibh;
Bob Peterson19aeb5a62014-09-29 08:52:04 -0400900 struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000901 int error;
902
Steven Whitehouseb60623c2006-11-01 12:22:46 -0500903 if (S_ISDIR(inode->i_mode))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000904 return -EPERM;
905
Bob Peterson2fba46a2020-02-27 12:47:53 -0600906 error = gfs2_qa_get(dip);
Bob Peterson0a305e42012-06-06 11:17:59 +0100907 if (error)
908 return error;
909
David Teiglandb3b94fa2006-01-16 16:50:04 +0000910 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
911 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
912
Bob Peterson72dbf472008-08-12 13:39:29 -0500913 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000914 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -0500915 goto out_parent;
916
917 error = gfs2_glock_nq(ghs + 1); /* child */
918 if (error)
919 goto out_child;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000920
Steven Whitehoused192a8e2011-05-05 12:35:40 +0100921 error = -ENOENT;
922 if (inode->i_nlink == 0)
923 goto out_gunlock;
924
Al Viro10556cb22011-06-20 19:28:19 -0400925 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000926 if (error)
927 goto out_gunlock;
928
Steven Whitehousedbb7cae2007-05-15 15:37:50 +0100929 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000930 switch (error) {
931 case -ENOENT:
932 break;
933 case 0:
934 error = -EEXIST;
935 default:
936 goto out_gunlock;
937 }
938
939 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500940 if (!dip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000941 goto out_gunlock;
942 error = -EFBIG;
Steven Whitehousead6203f22008-11-03 13:59:19 +0000943 if (dip->i_entries == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000944 goto out_gunlock;
945 error = -EPERM;
946 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
947 goto out_gunlock;
948 error = -EINVAL;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500949 if (!ip->i_inode.i_nlink)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000950 goto out_gunlock;
951 error = -EMLINK;
Steven Whitehouse4f561102006-11-01 14:04:17 -0500952 if (ip->i_inode.i_nlink == (u32)-1)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000953 goto out_gunlock;
954
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000955 error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da);
Steven Whitehousec7526662006-03-20 12:30:04 -0500956 if (error < 0)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000957 goto out_gunlock;
958
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000959 if (da.nr_blocks) {
960 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
Abhi Dasb8fbf472015-03-18 12:03:41 -0500961 error = gfs2_quota_lock_check(dip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000962 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -0400963 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000964
Steven Whitehouse7b9cff42013-10-02 11:13:25 +0100965 error = gfs2_inplace_reserve(dip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000966 if (error)
967 goto out_gunlock_q;
968
Steven Whitehouse534cf9c2014-01-06 12:03:05 +0000969 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000970 if (error)
971 goto out_ipres;
972 } else {
973 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
974 if (error)
975 goto out_ipres;
976 }
977
Steven Whitehouse2baee032011-05-09 12:08:36 +0100978 error = gfs2_meta_inode_buffer(ip, &dibh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000979 if (error)
980 goto out_end_trans;
981
Steven Whitehouse2b47dad2014-01-06 12:49:43 +0000982 error = gfs2_dir_add(dir, &dentry->d_name, ip, &da);
Steven Whitehouse2baee032011-05-09 12:08:36 +0100983 if (error)
984 goto out_brelse;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000985
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000986 gfs2_trans_add_meta(ip->i_gl, dibh);
Steven Whitehouse2baee032011-05-09 12:08:36 +0100987 inc_nlink(&ip->i_inode);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700988 ip->i_inode.i_ctime = current_time(&ip->i_inode);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100989 ihold(inode);
990 d_instantiate(dentry, inode);
991 mark_inode_dirty(inode);
Steven Whitehouse2baee032011-05-09 12:08:36 +0100992
993out_brelse:
994 brelse(dibh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400995out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000996 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400997out_ipres:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +0000998 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000999 gfs2_inplace_release(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001000out_gunlock_q:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001001 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001002 gfs2_quota_unlock(dip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001003out_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001004 gfs2_dir_no_add(&da);
Bob Peterson72dbf472008-08-12 13:39:29 -05001005 gfs2_glock_dq(ghs + 1);
1006out_child:
1007 gfs2_glock_dq(ghs);
1008out_parent:
Bob Peterson2fba46a2020-02-27 12:47:53 -06001009 gfs2_qa_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001010 gfs2_holder_uninit(ghs);
1011 gfs2_holder_uninit(ghs + 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001012 return error;
1013}
1014
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001015/*
1016 * gfs2_unlink_ok - check to see that a inode is still in a directory
1017 * @dip: the directory
1018 * @name: the name of the file
1019 * @ip: the inode
1020 *
1021 * Assumes that the lock on (at least) @dip is held.
1022 *
1023 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1024 */
1025
1026static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
1027 const struct gfs2_inode *ip)
1028{
1029 int error;
1030
1031 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1032 return -EPERM;
1033
1034 if ((dip->i_inode.i_mode & S_ISVTX) &&
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001035 !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
1036 !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001037 return -EPERM;
1038
1039 if (IS_APPEND(&dip->i_inode))
1040 return -EPERM;
1041
Al Viro10556cb22011-06-20 19:28:19 -04001042 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001043 if (error)
1044 return error;
1045
Fabian Frederick37975f12014-10-09 22:49:21 +02001046 return gfs2_dir_check(&dip->i_inode, name, ip);
Steven Whitehouse87ec2172009-05-22 10:54:50 +01001047}
1048
David Teiglandb3b94fa2006-01-16 16:50:04 +00001049/**
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001050 * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
1051 * @dip: The parent directory
1052 * @name: The name of the entry in the parent directory
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001053 * @inode: The inode to be removed
1054 *
1055 * Called with all the locks and in a transaction. This will only be
1056 * called for a directory after it has been checked to ensure it is empty.
1057 *
1058 * Returns: 0 on success, or an error
1059 */
1060
1061static int gfs2_unlink_inode(struct gfs2_inode *dip,
Bob Peterson4327a9b2012-11-12 13:03:29 -05001062 const struct dentry *dentry)
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001063{
David Howells2b0143b2015-03-17 22:25:59 +00001064 struct inode *inode = d_inode(dentry);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001065 struct gfs2_inode *ip = GFS2_I(inode);
1066 int error;
1067
1068 error = gfs2_dir_del(dip, dentry);
1069 if (error)
1070 return error;
1071
1072 ip->i_entries = 0;
Deepa Dinamani078cd822016-09-14 07:48:04 -07001073 inode->i_ctime = current_time(inode);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001074 if (S_ISDIR(inode->i_mode))
1075 clear_nlink(inode);
1076 else
1077 drop_nlink(inode);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001078 mark_inode_dirty(inode);
1079 if (inode->i_nlink == 0)
1080 gfs2_unlink_di(inode);
1081 return 0;
1082}
1083
1084
1085/**
1086 * gfs2_unlink - Unlink an inode (this does rmdir as well)
1087 * @dir: The inode of the directory containing the inode to unlink
David Teiglandb3b94fa2006-01-16 16:50:04 +00001088 * @dentry: The file itself
1089 *
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001090 * This routine uses the type of the inode as a flag to figure out
1091 * whether this is an unlink or an rmdir.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001092 *
1093 * Returns: errno
1094 */
1095
1096static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
1097{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001098 struct gfs2_inode *dip = GFS2_I(dir);
1099 struct gfs2_sbd *sdp = GFS2_SB(dir);
David Howells2b0143b2015-03-17 22:25:59 +00001100 struct inode *inode = d_inode(dentry);
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001101 struct gfs2_inode *ip = GFS2_I(inode);
Russell Cattelanddee7602007-01-29 17:13:44 -06001102 struct gfs2_holder ghs[3];
1103 struct gfs2_rgrpd *rgd;
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001104 int error;
1105
1106 error = gfs2_rindex_update(sdp);
1107 if (error)
1108 return error;
1109
1110 error = -EROFS;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001111
Russell Cattelanddee7602007-01-29 17:13:44 -06001112 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
1113 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
1114
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001115 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
Steven Whitehousea365fbf2012-02-24 15:09:14 +00001116 if (!rgd)
Steven Whitehouse87654892011-11-08 14:04:20 +00001117 goto out_inodes;
Steven Whitehousea365fbf2012-02-24 15:09:14 +00001118
Russell Cattelanddee7602007-01-29 17:13:44 -06001119 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
1120
1121
Steven Whitehouse8497a462007-08-26 14:23:56 +01001122 error = gfs2_glock_nq(ghs); /* parent */
David Teiglandb3b94fa2006-01-16 16:50:04 +00001123 if (error)
Steven Whitehouse8497a462007-08-26 14:23:56 +01001124 goto out_parent;
1125
1126 error = gfs2_glock_nq(ghs + 1); /* child */
1127 if (error)
1128 goto out_child;
1129
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001130 error = -ENOENT;
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001131 if (inode->i_nlink == 0)
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001132 goto out_rgrp;
1133
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001134 if (S_ISDIR(inode->i_mode)) {
1135 error = -ENOTEMPTY;
1136 if (ip->i_entries > 2 || inode->i_nlink > 2)
1137 goto out_rgrp;
1138 }
1139
Steven Whitehouse8497a462007-08-26 14:23:56 +01001140 error = gfs2_glock_nq(ghs + 2); /* rgrp */
1141 if (error)
1142 goto out_rgrp;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001143
1144 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
1145 if (error)
Bob Peterson72dbf472008-08-12 13:39:29 -05001146 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001147
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001148 error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001149 if (error)
Bob Peterson2eb59092018-01-29 10:00:23 -07001150 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001151
Bob Peterson4327a9b2012-11-12 13:03:29 -05001152 error = gfs2_unlink_inode(dip, dentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001153 gfs2_trans_end(sdp);
Bob Peterson2eb59092018-01-29 10:00:23 -07001154
Bob Peterson72dbf472008-08-12 13:39:29 -05001155out_gunlock:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001156 gfs2_glock_dq(ghs + 2);
1157out_rgrp:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001158 gfs2_glock_dq(ghs + 1);
1159out_child:
Steven Whitehouse8497a462007-08-26 14:23:56 +01001160 gfs2_glock_dq(ghs);
1161out_parent:
Steven Whitehouse87654892011-11-08 14:04:20 +00001162 gfs2_holder_uninit(ghs + 2);
1163out_inodes:
1164 gfs2_holder_uninit(ghs + 1);
Steven Whitehouse8497a462007-08-26 14:23:56 +01001165 gfs2_holder_uninit(ghs);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001166 return error;
1167}
1168
1169/**
1170 * gfs2_symlink - Create a symlink
1171 * @dir: The directory to create the symlink in
1172 * @dentry: The dentry to put the symlink in
1173 * @symname: The thing which the link points to
1174 *
1175 * Returns: errno
1176 */
1177
1178static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
1179 const char *symname)
1180{
Steven Whitehouse160b4022011-05-13 10:34:59 +01001181 unsigned int size;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001182
David Teiglandb3b94fa2006-01-16 16:50:04 +00001183 size = strlen(symname);
Andreas Gruenbacher235628c2017-11-14 16:53:12 +01001184 if (size >= gfs2_max_stuffed_size(GFS2_I(dir)))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001185 return -ENAMETOOLONG;
1186
Al Virob452a452018-06-08 13:06:28 -04001187 return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001188}
1189
1190/**
1191 * gfs2_mkdir - Make a directory
1192 * @dir: The parent directory of the new one
1193 * @dentry: The dentry of the new directory
1194 * @mode: The mode of the new directory
1195 *
1196 * Returns: errno
1197 */
1198
Al Viro18bb1db2011-07-26 01:41:39 -04001199static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001200{
Andreas Gruenbacher235628c2017-11-14 16:53:12 +01001201 unsigned dsize = gfs2_max_stuffed_size(GFS2_I(dir));
Al Virob452a452018-06-08 13:06:28 -04001202 return gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001203}
1204
1205/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001206 * gfs2_mknod - Make a special file
1207 * @dir: The directory in which the special file will reside
1208 * @dentry: The dentry of the special file
1209 * @mode: The mode of the special file
Steven Whitehousef2741d92011-05-13 12:11:17 +01001210 * @dev: The device specification of the special file
David Teiglandb3b94fa2006-01-16 16:50:04 +00001211 *
1212 */
1213
Al Viro1a67aaf2011-07-26 01:52:52 -04001214static int gfs2_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
David Teiglandb3b94fa2006-01-16 16:50:04 +00001215 dev_t dev)
1216{
Al Virob452a452018-06-08 13:06:28 -04001217 return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001218}
1219
1220/**
1221 * gfs2_atomic_open - Atomically open a file
1222 * @dir: The directory
1223 * @dentry: The proposed new entry
1224 * @file: The proposed new struct file
1225 * @flags: open flags
1226 * @mode: File mode
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001227 *
1228 * Returns: error code or 0 for success
1229 */
1230
1231static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry,
Antonio Ospite86fbca42015-05-01 12:54:38 -05001232 struct file *file, unsigned flags,
Al Viro44907d72018-06-08 13:32:02 -04001233 umode_t mode)
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001234{
1235 struct dentry *d;
1236 bool excl = !!(flags & O_EXCL);
1237
Al Viro00699ad2016-07-05 09:44:53 -04001238 if (!d_in_lookup(dentry))
Al Viro4d93bc32014-09-12 18:21:05 -04001239 goto skip_lookup;
1240
Al Virob452a452018-06-08 13:06:28 -04001241 d = __gfs2_lookup(dir, dentry, file);
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001242 if (IS_ERR(d))
1243 return PTR_ERR(d);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001244 if (d != NULL)
1245 dentry = d;
David Howells2b0143b2015-03-17 22:25:59 +00001246 if (d_really_is_positive(dentry)) {
Al Viroaad888f2018-06-08 12:58:04 -04001247 if (!(file->f_mode & FMODE_OPENED))
Al Viroec7d8792014-11-19 19:35:58 +00001248 return finish_no_open(file, d);
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001249 dput(d);
Al Viro21039132020-03-10 09:31:41 -04001250 return excl && (flags & O_CREAT) ? -EEXIST : 0;
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001251 }
1252
Miklos Szeredi5ca1db42013-09-23 13:21:04 +01001253 BUG_ON(d != NULL);
Al Viro4d93bc32014-09-12 18:21:05 -04001254
1255skip_lookup:
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01001256 if (!(flags & O_CREAT))
1257 return -ENOENT;
1258
Al Virob452a452018-06-08 13:06:28 -04001259 return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001260}
1261
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001262/*
1263 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1264 * @this: move this
1265 * @to: to here
1266 *
1267 * Follow @to back to the root and make sure we don't encounter @this
1268 * Assumes we already hold the rename lock.
1269 *
1270 * Returns: errno
1271 */
1272
1273static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1274{
1275 struct inode *dir = &to->i_inode;
1276 struct super_block *sb = dir->i_sb;
1277 struct inode *tmp;
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001278 int error = 0;
1279
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001280 igrab(dir);
1281
1282 for (;;) {
1283 if (dir == &this->i_inode) {
1284 error = -EINVAL;
1285 break;
1286 }
David Howells2b0143b2015-03-17 22:25:59 +00001287 if (dir == d_inode(sb->s_root)) {
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001288 error = 0;
1289 break;
1290 }
1291
Steven Whitehouse8d123582010-09-17 12:30:23 +01001292 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
Abhi Das48f8f712014-03-12 03:41:44 -05001293 if (!tmp) {
1294 error = -ENOENT;
1295 break;
1296 }
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001297 if (IS_ERR(tmp)) {
1298 error = PTR_ERR(tmp);
1299 break;
1300 }
1301
1302 iput(dir);
1303 dir = tmp;
1304 }
1305
1306 iput(dir);
1307
1308 return error;
1309}
1310
David Teiglandb3b94fa2006-01-16 16:50:04 +00001311/**
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001312 * update_moved_ino - Update an inode that's being moved
1313 * @ip: The inode being moved
1314 * @ndip: The parent directory of the new filename
1315 * @dir_rename: True of ip is a directory
1316 *
1317 * Returns: errno
1318 */
1319
1320static int update_moved_ino(struct gfs2_inode *ip, struct gfs2_inode *ndip,
1321 int dir_rename)
1322{
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001323 if (dir_rename)
1324 return gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
1325
Deepa Dinamani078cd822016-09-14 07:48:04 -07001326 ip->i_inode.i_ctime = current_time(&ip->i_inode);
Andreas Gruenbacher83998cc2018-02-28 12:48:53 -07001327 mark_inode_dirty_sync(&ip->i_inode);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001328 return 0;
1329}
1330
1331
1332/**
David Teiglandb3b94fa2006-01-16 16:50:04 +00001333 * gfs2_rename - Rename a file
1334 * @odir: Parent directory of old file name
1335 * @odentry: The old dentry of the file
1336 * @ndir: Parent directory of new file name
1337 * @ndentry: The new dentry of the file
1338 *
1339 * Returns: errno
1340 */
1341
1342static int gfs2_rename(struct inode *odir, struct dentry *odentry,
1343 struct inode *ndir, struct dentry *ndentry)
1344{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001345 struct gfs2_inode *odip = GFS2_I(odir);
1346 struct gfs2_inode *ndip = GFS2_I(ndir);
David Howells2b0143b2015-03-17 22:25:59 +00001347 struct gfs2_inode *ip = GFS2_I(d_inode(odentry));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001348 struct gfs2_inode *nip = NULL;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001349 struct gfs2_sbd *sdp = GFS2_SB(odir);
Bob Petersonbc74aae2019-08-30 12:31:00 -05001350 struct gfs2_holder ghs[4], r_gh, rd_gh;
Russell Cattelanddee7602007-01-29 17:13:44 -06001351 struct gfs2_rgrpd *nrgd;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001352 unsigned int num_gh;
1353 int dir_rename = 0;
Bob Peterson19aeb5a62014-09-29 08:52:04 -04001354 struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, };
David Teiglandb3b94fa2006-01-16 16:50:04 +00001355 unsigned int x;
1356 int error;
1357
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001358 gfs2_holder_mark_uninitialized(&r_gh);
Bob Petersonbc74aae2019-08-30 12:31:00 -05001359 gfs2_holder_mark_uninitialized(&rd_gh);
David Howells2b0143b2015-03-17 22:25:59 +00001360 if (d_really_is_positive(ndentry)) {
1361 nip = GFS2_I(d_inode(ndentry));
David Teiglandb3b94fa2006-01-16 16:50:04 +00001362 if (ip == nip)
1363 return 0;
1364 }
1365
Bob Peterson5e2f7d62012-04-04 22:11:16 -04001366 error = gfs2_rindex_update(sdp);
1367 if (error)
1368 return error;
1369
Bob Peterson2fba46a2020-02-27 12:47:53 -06001370 error = gfs2_qa_get(ndip);
Bob Peterson0a305e42012-06-06 11:17:59 +01001371 if (error)
1372 return error;
1373
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001374 if (odip != ndip) {
1375 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1376 0, &r_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001377 if (error)
1378 goto out;
1379
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001380 if (S_ISDIR(ip->i_inode.i_mode)) {
1381 dir_rename = 1;
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001382 /* don't move a directory into its subdir */
Steven Whitehouse0188d6c2008-08-26 09:38:26 +01001383 error = gfs2_ok_to_move(ip, ndip);
1384 if (error)
1385 goto out_gunlock_r;
1386 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001387 }
1388
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001389 num_gh = 1;
Bob Petersonad269672019-08-30 12:31:02 -05001390 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001391 if (odip != ndip) {
Bob Petersonad269672019-08-30 12:31:02 -05001392 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE,GL_ASYNC,
1393 ghs + num_gh);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001394 num_gh++;
1395 }
Bob Petersonad269672019-08-30 12:31:02 -05001396 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001397 num_gh++;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001398
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001399 if (nip) {
Bob Petersonad269672019-08-30 12:31:02 -05001400 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1401 ghs + num_gh);
Steven Whitehoused9d1ca32006-06-21 15:38:17 -04001402 num_gh++;
1403 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001404
Bob Peterson72dbf472008-08-12 13:39:29 -05001405 for (x = 0; x < num_gh; x++) {
1406 error = gfs2_glock_nq(ghs + x);
1407 if (error)
1408 goto out_gunlock;
1409 }
Bob Petersonad269672019-08-30 12:31:02 -05001410 error = gfs2_glock_async_wait(num_gh, ghs);
1411 if (error)
1412 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001413
Bob Petersonbc74aae2019-08-30 12:31:00 -05001414 if (nip) {
1415 /* Grab the resource group glock for unlink flag twiddling.
1416 * This is the case where the target dinode already exists
1417 * so we unlink before doing the rename.
1418 */
1419 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
1420 if (!nrgd) {
1421 error = -ENOENT;
1422 goto out_gunlock;
1423 }
1424 error = gfs2_glock_nq_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0,
1425 &rd_gh);
1426 if (error)
1427 goto out_gunlock;
1428 }
1429
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001430 error = -ENOENT;
1431 if (ip->i_inode.i_nlink == 0)
1432 goto out_gunlock;
1433
David Teiglandb3b94fa2006-01-16 16:50:04 +00001434 /* Check out the old directory */
1435
1436 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
1437 if (error)
1438 goto out_gunlock;
1439
1440 /* Check out the new directory */
1441
1442 if (nip) {
1443 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1444 if (error)
1445 goto out_gunlock;
1446
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001447 if (nip->i_inode.i_nlink == 0) {
1448 error = -EAGAIN;
1449 goto out_gunlock;
1450 }
1451
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001452 if (S_ISDIR(nip->i_inode.i_mode)) {
Steven Whitehousead6203f22008-11-03 13:59:19 +00001453 if (nip->i_entries < 2) {
Steven Whitehouse94fb7632011-05-09 13:36:10 +01001454 gfs2_consist_inode(nip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001455 error = -EIO;
1456 goto out_gunlock;
1457 }
Steven Whitehousead6203f22008-11-03 13:59:19 +00001458 if (nip->i_entries > 2) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001459 error = -ENOTEMPTY;
1460 goto out_gunlock;
1461 }
1462 }
1463 } else {
Al Viro10556cb22011-06-20 19:28:19 -04001464 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001465 if (error)
1466 goto out_gunlock;
1467
Steven Whitehousedbb7cae2007-05-15 15:37:50 +01001468 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001469 switch (error) {
1470 case -ENOENT:
1471 error = 0;
1472 break;
1473 case 0:
1474 error = -EEXIST;
1475 default:
1476 goto out_gunlock;
Aliasgar Surti098b9c12019-10-04 10:55:29 -05001477 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001478
1479 if (odip != ndip) {
Steven Whitehouse4f561102006-11-01 14:04:17 -05001480 if (!ndip->i_inode.i_nlink) {
Steven Whitehoused192a8e2011-05-05 12:35:40 +01001481 error = -ENOENT;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001482 goto out_gunlock;
1483 }
Steven Whitehousead6203f22008-11-03 13:59:19 +00001484 if (ndip->i_entries == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001485 error = -EFBIG;
1486 goto out_gunlock;
1487 }
Steven Whitehouseb60623c2006-11-01 12:22:46 -05001488 if (S_ISDIR(ip->i_inode.i_mode) &&
Steven Whitehouse4f561102006-11-01 14:04:17 -05001489 ndip->i_inode.i_nlink == (u32)-1) {
David Teiglandb3b94fa2006-01-16 16:50:04 +00001490 error = -EMLINK;
1491 goto out_gunlock;
1492 }
1493 }
1494 }
1495
1496 /* Check out the dir to be renamed */
1497
1498 if (dir_rename) {
David Howells2b0143b2015-03-17 22:25:59 +00001499 error = gfs2_permission(d_inode(odentry), MAY_WRITE);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001500 if (error)
1501 goto out_gunlock;
1502 }
1503
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001504 if (nip == NULL) {
1505 error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
1506 if (error)
1507 goto out_gunlock;
1508 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001509
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001510 if (da.nr_blocks) {
1511 struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
Abhi Dasb8fbf472015-03-18 12:03:41 -05001512 error = gfs2_quota_lock_check(ndip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001513 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001514 goto out_gunlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001515
Steven Whitehouse7b9cff42013-10-02 11:13:25 +01001516 error = gfs2_inplace_reserve(ndip, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001517 if (error)
1518 goto out_gunlock_q;
1519
Steven Whitehouse534cf9c2014-01-06 12:03:05 +00001520 error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
1521 4 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001522 if (error)
1523 goto out_ipreserv;
1524 } else {
1525 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
S. Wendy Cheng87d21e02007-01-18 16:07:03 -05001526 5 * RES_LEAF + 4, 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001527 if (error)
1528 goto out_gunlock;
1529 }
1530
1531 /* Remove the target file, if it exists */
1532
Bob Peterson4327a9b2012-11-12 13:03:29 -05001533 if (nip)
1534 error = gfs2_unlink_inode(ndip, ndentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001535
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001536 error = update_moved_ino(ip, ndip, dir_rename);
1537 if (error)
1538 goto out_end_trans;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001539
Steven Whitehouse855d23c2011-05-09 16:42:37 +01001540 error = gfs2_dir_del(odip, odentry);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001541 if (error)
1542 goto out_end_trans;
1543
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001544 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001545 if (error)
1546 goto out_end_trans;
1547
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001548out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001549 gfs2_trans_end(sdp);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001550out_ipreserv:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001551 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001552 gfs2_inplace_release(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001553out_gunlock_q:
Steven Whitehouse3c1c0ae2014-01-06 11:28:41 +00001554 if (da.nr_blocks)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001555 gfs2_quota_unlock(ndip);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001556out_gunlock:
Steven Whitehouse2b47dad2014-01-06 12:49:43 +00001557 gfs2_dir_no_add(&da);
Bob Petersonbc74aae2019-08-30 12:31:00 -05001558 if (gfs2_holder_initialized(&rd_gh))
1559 gfs2_glock_dq_uninit(&rd_gh);
1560
Bob Peterson72dbf472008-08-12 13:39:29 -05001561 while (x--) {
Bob Petersonad269672019-08-30 12:31:02 -05001562 if (gfs2_holder_queued(ghs + x))
1563 gfs2_glock_dq(ghs + x);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001564 gfs2_holder_uninit(ghs + x);
Bob Peterson72dbf472008-08-12 13:39:29 -05001565 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001566out_gunlock_r:
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001567 if (gfs2_holder_initialized(&r_gh))
David Teiglandb3b94fa2006-01-16 16:50:04 +00001568 gfs2_glock_dq_uninit(&r_gh);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001569out:
Bob Peterson2fba46a2020-02-27 12:47:53 -06001570 gfs2_qa_put(ndip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001571 return error;
1572}
1573
1574/**
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001575 * gfs2_exchange - exchange two files
1576 * @odir: Parent directory of old file name
1577 * @odentry: The old dentry of the file
1578 * @ndir: Parent directory of new file name
1579 * @ndentry: The new dentry of the file
1580 * @flags: The rename flags
1581 *
1582 * Returns: errno
1583 */
1584
1585static int gfs2_exchange(struct inode *odir, struct dentry *odentry,
1586 struct inode *ndir, struct dentry *ndentry,
1587 unsigned int flags)
1588{
1589 struct gfs2_inode *odip = GFS2_I(odir);
1590 struct gfs2_inode *ndip = GFS2_I(ndir);
1591 struct gfs2_inode *oip = GFS2_I(odentry->d_inode);
1592 struct gfs2_inode *nip = GFS2_I(ndentry->d_inode);
1593 struct gfs2_sbd *sdp = GFS2_SB(odir);
Bob Petersonad269672019-08-30 12:31:02 -05001594 struct gfs2_holder ghs[4], r_gh;
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001595 unsigned int num_gh;
1596 unsigned int x;
1597 umode_t old_mode = oip->i_inode.i_mode;
1598 umode_t new_mode = nip->i_inode.i_mode;
1599 int error;
1600
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001601 gfs2_holder_mark_uninitialized(&r_gh);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001602 error = gfs2_rindex_update(sdp);
1603 if (error)
1604 return error;
1605
1606 if (odip != ndip) {
1607 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1608 0, &r_gh);
1609 if (error)
1610 goto out;
1611
1612 if (S_ISDIR(old_mode)) {
1613 /* don't move a directory into its subdir */
1614 error = gfs2_ok_to_move(oip, ndip);
1615 if (error)
1616 goto out_gunlock_r;
1617 }
1618
1619 if (S_ISDIR(new_mode)) {
1620 /* don't move a directory into its subdir */
1621 error = gfs2_ok_to_move(nip, odip);
1622 if (error)
1623 goto out_gunlock_r;
1624 }
1625 }
1626
1627 num_gh = 1;
Bob Petersonad269672019-08-30 12:31:02 -05001628 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001629 if (odip != ndip) {
Bob Petersonad269672019-08-30 12:31:02 -05001630 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1631 ghs + num_gh);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001632 num_gh++;
1633 }
Bob Petersonad269672019-08-30 12:31:02 -05001634 gfs2_holder_init(oip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001635 num_gh++;
1636
Bob Petersonad269672019-08-30 12:31:02 -05001637 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001638 num_gh++;
1639
1640 for (x = 0; x < num_gh; x++) {
1641 error = gfs2_glock_nq(ghs + x);
1642 if (error)
1643 goto out_gunlock;
1644 }
1645
Bob Petersonad269672019-08-30 12:31:02 -05001646 error = gfs2_glock_async_wait(num_gh, ghs);
1647 if (error)
1648 goto out_gunlock;
1649
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001650 error = -ENOENT;
1651 if (oip->i_inode.i_nlink == 0 || nip->i_inode.i_nlink == 0)
1652 goto out_gunlock;
1653
1654 error = gfs2_unlink_ok(odip, &odentry->d_name, oip);
1655 if (error)
1656 goto out_gunlock;
1657 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1658 if (error)
1659 goto out_gunlock;
1660
1661 if (S_ISDIR(old_mode)) {
1662 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
1663 if (error)
1664 goto out_gunlock;
1665 }
1666 if (S_ISDIR(new_mode)) {
1667 error = gfs2_permission(ndentry->d_inode, MAY_WRITE);
1668 if (error)
1669 goto out_gunlock;
1670 }
1671 error = gfs2_trans_begin(sdp, 4 * RES_DINODE + 4 * RES_LEAF, 0);
1672 if (error)
1673 goto out_gunlock;
1674
1675 error = update_moved_ino(oip, ndip, S_ISDIR(old_mode));
1676 if (error)
1677 goto out_end_trans;
1678
1679 error = update_moved_ino(nip, odip, S_ISDIR(new_mode));
1680 if (error)
1681 goto out_end_trans;
1682
1683 error = gfs2_dir_mvino(ndip, &ndentry->d_name, oip,
1684 IF2DT(old_mode));
1685 if (error)
1686 goto out_end_trans;
1687
1688 error = gfs2_dir_mvino(odip, &odentry->d_name, nip,
1689 IF2DT(new_mode));
1690 if (error)
1691 goto out_end_trans;
1692
1693 if (odip != ndip) {
1694 if (S_ISDIR(new_mode) && !S_ISDIR(old_mode)) {
1695 inc_nlink(&odip->i_inode);
1696 drop_nlink(&ndip->i_inode);
1697 } else if (S_ISDIR(old_mode) && !S_ISDIR(new_mode)) {
1698 inc_nlink(&ndip->i_inode);
1699 drop_nlink(&odip->i_inode);
1700 }
1701 }
1702 mark_inode_dirty(&ndip->i_inode);
1703 if (odip != ndip)
1704 mark_inode_dirty(&odip->i_inode);
1705
1706out_end_trans:
1707 gfs2_trans_end(sdp);
1708out_gunlock:
1709 while (x--) {
Bob Petersonad269672019-08-30 12:31:02 -05001710 if (gfs2_holder_queued(ghs + x))
1711 gfs2_glock_dq(ghs + x);
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001712 gfs2_holder_uninit(ghs + x);
1713 }
1714out_gunlock_r:
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001715 if (gfs2_holder_initialized(&r_gh))
Benjamin Marzinskia63b7bb2015-05-05 12:12:19 -05001716 gfs2_glock_dq_uninit(&r_gh);
1717out:
1718 return error;
1719}
1720
1721static int gfs2_rename2(struct inode *odir, struct dentry *odentry,
1722 struct inode *ndir, struct dentry *ndentry,
1723 unsigned int flags)
1724{
1725 flags &= ~RENAME_NOREPLACE;
1726
1727 if (flags & ~RENAME_EXCHANGE)
1728 return -EINVAL;
1729
1730 if (flags & RENAME_EXCHANGE)
1731 return gfs2_exchange(odir, odentry, ndir, ndentry, flags);
1732
1733 return gfs2_rename(odir, odentry, ndir, ndentry);
1734}
1735
1736/**
Al Viro6b255392015-11-17 10:20:54 -05001737 * gfs2_get_link - Follow a symbolic link
David Teiglandb3b94fa2006-01-16 16:50:04 +00001738 * @dentry: The dentry of the link
Al Viro6b255392015-11-17 10:20:54 -05001739 * @inode: The inode of the link
Al Virofceef392015-12-29 15:58:39 -05001740 * @done: destructor for return value
David Teiglandb3b94fa2006-01-16 16:50:04 +00001741 *
Al Viroc177c2a2010-01-14 00:59:16 -05001742 * This can handle symlinks of any size.
David Teiglandb3b94fa2006-01-16 16:50:04 +00001743 *
1744 * Returns: 0 on success or error code
1745 */
1746
Al Viro6b255392015-11-17 10:20:54 -05001747static const char *gfs2_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001748 struct inode *inode,
1749 struct delayed_call *done)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001750{
Al Viro6b255392015-11-17 10:20:54 -05001751 struct gfs2_inode *ip = GFS2_I(inode);
Al Viroc177c2a2010-01-14 00:59:16 -05001752 struct gfs2_holder i_gh;
1753 struct buffer_head *dibh;
Steven Whitehouse160b4022011-05-13 10:34:59 +01001754 unsigned int size;
Al Viroc177c2a2010-01-14 00:59:16 -05001755 char *buf;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001756 int error;
1757
Al Viro6b255392015-11-17 10:20:54 -05001758 if (!dentry)
1759 return ERR_PTR(-ECHILD);
1760
Al Viroc177c2a2010-01-14 00:59:16 -05001761 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1762 error = gfs2_glock_nq(&i_gh);
1763 if (error) {
1764 gfs2_holder_uninit(&i_gh);
Al Viro680baac2015-05-02 13:32:22 -04001765 return ERR_PTR(error);
Al Viroc177c2a2010-01-14 00:59:16 -05001766 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001767
Steven Whitehousea2e0f792010-08-11 09:53:11 +01001768 size = (unsigned int)i_size_read(&ip->i_inode);
1769 if (size == 0) {
Al Viroc177c2a2010-01-14 00:59:16 -05001770 gfs2_consist_inode(ip);
1771 buf = ERR_PTR(-EIO);
1772 goto out;
1773 }
1774
1775 error = gfs2_meta_inode_buffer(ip, &dibh);
1776 if (error) {
1777 buf = ERR_PTR(error);
1778 goto out;
1779 }
1780
Steven Whitehouse160b4022011-05-13 10:34:59 +01001781 buf = kzalloc(size + 1, GFP_NOFS);
Al Viroc177c2a2010-01-14 00:59:16 -05001782 if (!buf)
1783 buf = ERR_PTR(-ENOMEM);
1784 else
Steven Whitehouse160b4022011-05-13 10:34:59 +01001785 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
Al Viroc177c2a2010-01-14 00:59:16 -05001786 brelse(dibh);
1787out:
1788 gfs2_glock_dq_uninit(&i_gh);
Al Viro680baac2015-05-02 13:32:22 -04001789 if (!IS_ERR(buf))
Al Virofceef392015-12-29 15:58:39 -05001790 set_delayed_call(done, kfree_link, buf);
Al Viro680baac2015-05-02 13:32:22 -04001791 return buf;
Al Viroc177c2a2010-01-14 00:59:16 -05001792}
1793
David Teiglandb3b94fa2006-01-16 16:50:04 +00001794/**
1795 * gfs2_permission -
Steven Whitehouse75d5cfb2011-01-19 09:42:40 +00001796 * @inode: The inode
1797 * @mask: The mask to be tested
1798 * @flags: Indicates whether this is an RCU path walk or not
David Teiglandb3b94fa2006-01-16 16:50:04 +00001799 *
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001800 * This may be called from the VFS directly, or from within GFS2 with the
1801 * inode locked, so we look to see if the glock is already locked and only
1802 * lock the glock if its not already been done.
1803 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001804 * Returns: errno
1805 */
1806
Al Viro10556cb22011-06-20 19:28:19 -04001807int gfs2_permission(struct inode *inode, int mask)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001808{
Nick Pigginb74c79e2011-01-07 17:49:58 +11001809 struct gfs2_inode *ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001810 struct gfs2_holder i_gh;
1811 int error;
1812
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001813 gfs2_holder_mark_uninitialized(&i_gh);
Nick Pigginb74c79e2011-01-07 17:49:58 +11001814 ip = GFS2_I(inode);
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00001815 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001816 if (mask & MAY_NOT_BLOCK)
1817 return -ECHILD;
1818 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1819 if (error)
1820 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001821 }
1822
Miklos Szeredif58ba882008-07-02 21:12:01 +02001823 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
Eryu Guan337684a2016-08-02 19:58:28 +08001824 error = -EPERM;
Miklos Szeredif58ba882008-07-02 21:12:01 +02001825 else
Al Viro2830ba72011-06-20 19:16:29 -04001826 error = generic_permission(inode, mask);
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001827 if (gfs2_holder_initialized(&i_gh))
Steven Whitehouse300c7d72006-11-27 09:55:28 -05001828 gfs2_glock_dq_uninit(&i_gh);
1829
David Teiglandb3b94fa2006-01-16 16:50:04 +00001830 return error;
1831}
1832
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001833static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
Steven Whitehouse194c0112011-05-09 14:06:38 +01001834{
Steven Whitehouse194c0112011-05-09 14:06:38 +01001835 setattr_copy(inode, attr);
1836 mark_inode_dirty(inode);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001837 return 0;
1838}
1839
1840/**
1841 * gfs2_setattr_simple -
1842 * @ip:
1843 * @attr:
1844 *
1845 * Returns: errno
1846 */
1847
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001848int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
Steven Whitehouse194c0112011-05-09 14:06:38 +01001849{
1850 int error;
1851
1852 if (current->journal_info)
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001853 return __gfs2_setattr_simple(inode, attr);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001854
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001855 error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
Steven Whitehouse194c0112011-05-09 14:06:38 +01001856 if (error)
1857 return error;
1858
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001859 error = __gfs2_setattr_simple(inode, attr);
1860 gfs2_trans_end(GFS2_SB(inode));
Steven Whitehouse194c0112011-05-09 14:06:38 +01001861 return error;
1862}
1863
David Teiglandb3b94fa2006-01-16 16:50:04 +00001864static int setattr_chown(struct inode *inode, struct iattr *attr)
1865{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001866 struct gfs2_inode *ip = GFS2_I(inode);
1867 struct gfs2_sbd *sdp = GFS2_SB(inode);
Eric W. Biederman7c06b5d2013-01-31 20:27:54 -08001868 kuid_t ouid, nuid;
1869 kgid_t ogid, ngid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001870 int error;
Abhi Dasb8fbf472015-03-18 12:03:41 -05001871 struct gfs2_alloc_parms ap;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001872
Steven Whitehouse2933f922006-11-01 13:23:29 -05001873 ouid = inode->i_uid;
1874 ogid = inode->i_gid;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001875 nuid = attr->ia_uid;
1876 ngid = attr->ia_gid;
1877
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001878 if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001879 ouid = nuid = NO_UID_QUOTA_CHANGE;
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001880 if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001881 ogid = ngid = NO_GID_QUOTA_CHANGE;
Bob Peterson2fba46a2020-02-27 12:47:53 -06001882 error = gfs2_qa_get(ip);
Bob Peterson62e96cf2014-01-06 17:16:01 -05001883 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -06001884 return error;
Bob Peterson62e96cf2014-01-06 17:16:01 -05001885
1886 error = gfs2_rindex_update(sdp);
1887 if (error)
1888 goto out;
1889
1890 error = gfs2_quota_lock(ip, nuid, ngid);
1891 if (error)
1892 goto out;
1893
Abhi Dasb8fbf472015-03-18 12:03:41 -05001894 ap.target = gfs2_get_inode_blocks(&ip->i_inode);
1895
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001896 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1897 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
Abhi Dasb8fbf472015-03-18 12:03:41 -05001898 error = gfs2_quota_check(ip, nuid, ngid, &ap);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001899 if (error)
1900 goto out_gunlock_q;
1901 }
1902
1903 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1904 if (error)
1905 goto out_gunlock_q;
1906
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001907 error = gfs2_setattr_simple(inode, attr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001908 if (error)
1909 goto out_end_trans;
1910
Eric W. Biederman6b24c0d2013-01-31 21:56:13 -08001911 if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1912 !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
Abhi Das39a72582015-06-02 11:02:24 -05001913 gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid);
Abhi Dasb8fbf472015-03-18 12:03:41 -05001914 gfs2_quota_change(ip, ap.target, nuid, ngid);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001915 }
1916
Steven Whitehousea91ea692006-09-04 12:04:26 -04001917out_end_trans:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001918 gfs2_trans_end(sdp);
Steven Whitehousea91ea692006-09-04 12:04:26 -04001919out_gunlock_q:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001920 gfs2_quota_unlock(ip);
Bob Peterson62e96cf2014-01-06 17:16:01 -05001921out:
Bob Peterson2fba46a2020-02-27 12:47:53 -06001922 gfs2_qa_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001923 return error;
1924}
1925
1926/**
1927 * gfs2_setattr - Change attributes on an inode
1928 * @dentry: The dentry which is changing
1929 * @attr: The structure describing the change
1930 *
1931 * The VFS layer wants to change one or more of an inodes attributes. Write
1932 * that change out to disk.
1933 *
1934 * Returns: errno
1935 */
1936
1937static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1938{
David Howells2b0143b2015-03-17 22:25:59 +00001939 struct inode *inode = d_inode(dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001940 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001941 struct gfs2_holder i_gh;
1942 int error;
1943
Bob Peterson2fba46a2020-02-27 12:47:53 -06001944 error = gfs2_qa_get(ip);
Bob Peterson0a305e42012-06-06 11:17:59 +01001945 if (error)
1946 return error;
1947
David Teiglandb3b94fa2006-01-16 16:50:04 +00001948 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1949 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -06001950 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001951
1952 error = -EPERM;
1953 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
Bob Peterson2fba46a2020-02-27 12:47:53 -06001954 goto error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001955
Jan Kara31051c82016-05-26 16:55:18 +02001956 error = setattr_prepare(dentry, attr);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001957 if (error)
Bob Peterson2fba46a2020-02-27 12:47:53 -06001958 goto error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00001959
1960 if (attr->ia_valid & ATTR_SIZE)
Steven Whitehouseff8f33c2010-08-11 09:37:53 +01001961 error = gfs2_setattr_size(inode, attr->ia_size);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001962 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1963 error = setattr_chown(inode, attr);
Christoph Hellwige01580b2013-12-20 05:16:52 -08001964 else {
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001965 error = gfs2_setattr_simple(inode, attr);
Christoph Hellwige01580b2013-12-20 05:16:52 -08001966 if (!error && attr->ia_valid & ATTR_MODE)
1967 error = posix_acl_chmod(inode, inode->i_mode);
1968 }
David Teiglandb3b94fa2006-01-16 16:50:04 +00001969
Bob Peterson2fba46a2020-02-27 12:47:53 -06001970error:
David Teiglandb3b94fa2006-01-16 16:50:04 +00001971 if (!error)
1972 mark_inode_dirty(inode);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001973 gfs2_glock_dq_uninit(&i_gh);
Bob Peterson2fba46a2020-02-27 12:47:53 -06001974out:
1975 gfs2_qa_put(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +00001976 return error;
1977}
1978
1979/**
1980 * gfs2_getattr - Read out an inode's attributes
David Howellsa528d352017-01-31 16:46:22 +00001981 * @path: Object to query
David Teiglandb3b94fa2006-01-16 16:50:04 +00001982 * @stat: The inode's stats
David Howellsa528d352017-01-31 16:46:22 +00001983 * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
1984 * @flags: AT_STATX_xxx setting
David Teiglandb3b94fa2006-01-16 16:50:04 +00001985 *
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05001986 * This may be called from the VFS directly, or from within GFS2 with the
1987 * inode locked, so we look to see if the glock is already locked and only
1988 * lock the glock if its not already been done. Note that its the NFS
1989 * readdirplus operation which causes this to be called (from filldir)
1990 * with the glock already held.
1991 *
David Teiglandb3b94fa2006-01-16 16:50:04 +00001992 * Returns: errno
1993 */
1994
David Howellsa528d352017-01-31 16:46:22 +00001995static int gfs2_getattr(const struct path *path, struct kstat *stat,
1996 u32 request_mask, unsigned int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +00001997{
David Howellsa528d352017-01-31 16:46:22 +00001998 struct inode *inode = d_inode(path->dentry);
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -04001999 struct gfs2_inode *ip = GFS2_I(inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +00002000 struct gfs2_holder gh;
Andreas Gruenbacherb2623c22017-10-09 17:55:58 +02002001 u32 gfsflags;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002002 int error;
2003
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05002004 gfs2_holder_mark_uninitialized(&gh);
Steven Whitehouse7afd88d2008-02-22 16:07:18 +00002005 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06002006 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
2007 if (error)
2008 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002009 }
2010
Andreas Gruenbacherb2623c22017-10-09 17:55:58 +02002011 gfsflags = ip->i_diskflags;
2012 if (gfsflags & GFS2_DIF_APPENDONLY)
2013 stat->attributes |= STATX_ATTR_APPEND;
2014 if (gfsflags & GFS2_DIF_IMMUTABLE)
2015 stat->attributes |= STATX_ATTR_IMMUTABLE;
2016
2017 stat->attributes_mask |= (STATX_ATTR_APPEND |
2018 STATX_ATTR_COMPRESSED |
2019 STATX_ATTR_ENCRYPTED |
2020 STATX_ATTR_IMMUTABLE |
2021 STATX_ATTR_NODUMP);
2022
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05002023 generic_fillattr(inode, stat);
Andreas Gruenbacherb2623c22017-10-09 17:55:58 +02002024
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05002025 if (gfs2_holder_initialized(&gh))
Steven Whitehousedcf3dd82006-11-27 10:12:05 -05002026 gfs2_glock_dq_uninit(&gh);
2027
2028 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +00002029}
2030
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002031static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2032 u64 start, u64 len)
2033{
2034 struct gfs2_inode *ip = GFS2_I(inode);
2035 struct gfs2_holder gh;
2036 int ret;
2037
Bob Petersonaac1a552017-02-16 21:13:54 +01002038 inode_lock_shared(inode);
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002039
2040 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2041 if (ret)
2042 goto out;
2043
Bob Petersonaac1a552017-02-16 21:13:54 +01002044 ret = iomap_fiemap(inode, fieinfo, start, len, &gfs2_iomap_ops);
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002045
2046 gfs2_glock_dq_uninit(&gh);
Bob Petersonaac1a552017-02-16 21:13:54 +01002047
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002048out:
Bob Petersonaac1a552017-02-16 21:13:54 +01002049 inode_unlock_shared(inode);
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002050 return ret;
2051}
2052
Andreas Gruenbacher3a274112017-03-15 19:12:59 +01002053loff_t gfs2_seek_data(struct file *file, loff_t offset)
2054{
2055 struct inode *inode = file->f_mapping->host;
2056 struct gfs2_inode *ip = GFS2_I(inode);
2057 struct gfs2_holder gh;
2058 loff_t ret;
2059
2060 inode_lock_shared(inode);
2061 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2062 if (!ret)
2063 ret = iomap_seek_data(inode, offset, &gfs2_iomap_ops);
2064 gfs2_glock_dq_uninit(&gh);
2065 inode_unlock_shared(inode);
2066
2067 if (ret < 0)
2068 return ret;
2069 return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
2070}
2071
2072loff_t gfs2_seek_hole(struct file *file, loff_t offset)
2073{
2074 struct inode *inode = file->f_mapping->host;
2075 struct gfs2_inode *ip = GFS2_I(inode);
2076 struct gfs2_holder gh;
2077 loff_t ret;
2078
2079 inode_lock_shared(inode);
2080 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2081 if (!ret)
2082 ret = iomap_seek_hole(inode, offset, &gfs2_iomap_ops);
2083 gfs2_glock_dq_uninit(&gh);
2084 inode_unlock_shared(inode);
2085
2086 if (ret < 0)
2087 return ret;
2088 return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
2089}
2090
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002091const struct inode_operations gfs2_file_iops = {
Al Viroe6305c42008-07-15 21:03:57 -04002092 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002093 .setattr = gfs2_setattr,
2094 .getattr = gfs2_getattr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002095 .listxattr = gfs2_listxattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002096 .fiemap = gfs2_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02002097 .get_acl = gfs2_get_acl,
Christoph Hellwige01580b2013-12-20 05:16:52 -08002098 .set_acl = gfs2_set_acl,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002099};
2100
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002101const struct inode_operations gfs2_dir_iops = {
David Teiglandb3b94fa2006-01-16 16:50:04 +00002102 .create = gfs2_create,
2103 .lookup = gfs2_lookup,
2104 .link = gfs2_link,
2105 .unlink = gfs2_unlink,
2106 .symlink = gfs2_symlink,
2107 .mkdir = gfs2_mkdir,
Steven Whitehouse855d23c2011-05-09 16:42:37 +01002108 .rmdir = gfs2_unlink,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002109 .mknod = gfs2_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02002110 .rename = gfs2_rename2,
Al Viroe6305c42008-07-15 21:03:57 -04002111 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002112 .setattr = gfs2_setattr,
2113 .getattr = gfs2_getattr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002114 .listxattr = gfs2_listxattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002115 .fiemap = gfs2_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +02002116 .get_acl = gfs2_get_acl,
Christoph Hellwige01580b2013-12-20 05:16:52 -08002117 .set_acl = gfs2_set_acl,
Steven Whitehouse6d4ade92013-06-14 11:17:15 +01002118 .atomic_open = gfs2_atomic_open,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002119};
2120
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002121const struct inode_operations gfs2_symlink_iops = {
Al Viro6b255392015-11-17 10:20:54 -05002122 .get_link = gfs2_get_link,
Al Viroe6305c42008-07-15 21:03:57 -04002123 .permission = gfs2_permission,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002124 .setattr = gfs2_setattr,
2125 .getattr = gfs2_getattr,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002126 .listxattr = gfs2_listxattr,
Steven Whitehousee9079cc2008-10-14 14:43:29 +01002127 .fiemap = gfs2_fiemap,
David Teiglandb3b94fa2006-01-16 16:50:04 +00002128};
2129