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