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