xref: /linux/fs/gfs2/inode.c (revision d6fd48ef)
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 ghs[2];
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, ghs);
957 	gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
958 
959 	error = gfs2_glock_nq(ghs); /* parent */
960 	if (error)
961 		goto out_parent;
962 
963 	error = gfs2_glock_nq(ghs + 1); /* child */
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 = -EINVAL;
996 	if (!ip->i_inode.i_nlink)
997 		goto out_gunlock;
998 	error = -EMLINK;
999 	if (ip->i_inode.i_nlink == (u32)-1)
1000 		goto out_gunlock;
1001 
1002 	error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da);
1003 	if (error < 0)
1004 		goto out_gunlock;
1005 
1006 	if (da.nr_blocks) {
1007 		struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
1008 		error = gfs2_quota_lock_check(dip, &ap);
1009 		if (error)
1010 			goto out_gunlock;
1011 
1012 		error = gfs2_inplace_reserve(dip, &ap);
1013 		if (error)
1014 			goto out_gunlock_q;
1015 
1016 		error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0);
1017 		if (error)
1018 			goto out_ipres;
1019 	} else {
1020 		error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
1021 		if (error)
1022 			goto out_ipres;
1023 	}
1024 
1025 	error = gfs2_meta_inode_buffer(ip, &dibh);
1026 	if (error)
1027 		goto out_end_trans;
1028 
1029 	error = gfs2_dir_add(dir, &dentry->d_name, ip, &da);
1030 	if (error)
1031 		goto out_brelse;
1032 
1033 	gfs2_trans_add_meta(ip->i_gl, dibh);
1034 	inc_nlink(&ip->i_inode);
1035 	ip->i_inode.i_ctime = current_time(&ip->i_inode);
1036 	ihold(inode);
1037 	d_instantiate(dentry, inode);
1038 	mark_inode_dirty(inode);
1039 
1040 out_brelse:
1041 	brelse(dibh);
1042 out_end_trans:
1043 	gfs2_trans_end(sdp);
1044 out_ipres:
1045 	if (da.nr_blocks)
1046 		gfs2_inplace_release(dip);
1047 out_gunlock_q:
1048 	if (da.nr_blocks)
1049 		gfs2_quota_unlock(dip);
1050 out_gunlock:
1051 	gfs2_dir_no_add(&da);
1052 	gfs2_glock_dq(ghs + 1);
1053 out_child:
1054 	gfs2_glock_dq(ghs);
1055 out_parent:
1056 	gfs2_qa_put(dip);
1057 	gfs2_holder_uninit(ghs);
1058 	gfs2_holder_uninit(ghs + 1);
1059 	return error;
1060 }
1061 
1062 /*
1063  * gfs2_unlink_ok - check to see that a inode is still in a directory
1064  * @dip: the directory
1065  * @name: the name of the file
1066  * @ip: the inode
1067  *
1068  * Assumes that the lock on (at least) @dip is held.
1069  *
1070  * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1071  */
1072 
1073 static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
1074 			  const struct gfs2_inode *ip)
1075 {
1076 	int error;
1077 
1078 	if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1079 		return -EPERM;
1080 
1081 	if ((dip->i_inode.i_mode & S_ISVTX) &&
1082 	    !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
1083 	    !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
1084 		return -EPERM;
1085 
1086 	if (IS_APPEND(&dip->i_inode))
1087 		return -EPERM;
1088 
1089 	error = gfs2_permission(&nop_mnt_idmap, &dip->i_inode,
1090 				MAY_WRITE | MAY_EXEC);
1091 	if (error)
1092 		return error;
1093 
1094 	return gfs2_dir_check(&dip->i_inode, name, ip);
1095 }
1096 
1097 /**
1098  * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
1099  * @dip: The parent directory
1100  * @dentry: The dentry to unlink
1101  *
1102  * Called with all the locks and in a transaction. This will only be
1103  * called for a directory after it has been checked to ensure it is empty.
1104  *
1105  * Returns: 0 on success, or an error
1106  */
1107 
1108 static int gfs2_unlink_inode(struct gfs2_inode *dip,
1109 			     const struct dentry *dentry)
1110 {
1111 	struct inode *inode = d_inode(dentry);
1112 	struct gfs2_inode *ip = GFS2_I(inode);
1113 	int error;
1114 
1115 	error = gfs2_dir_del(dip, dentry);
1116 	if (error)
1117 		return error;
1118 
1119 	ip->i_entries = 0;
1120 	inode->i_ctime = current_time(inode);
1121 	if (S_ISDIR(inode->i_mode))
1122 		clear_nlink(inode);
1123 	else
1124 		drop_nlink(inode);
1125 	mark_inode_dirty(inode);
1126 	if (inode->i_nlink == 0)
1127 		gfs2_unlink_di(inode);
1128 	return 0;
1129 }
1130 
1131 
1132 /**
1133  * gfs2_unlink - Unlink an inode (this does rmdir as well)
1134  * @dir: The inode of the directory containing the inode to unlink
1135  * @dentry: The file itself
1136  *
1137  * This routine uses the type of the inode as a flag to figure out
1138  * whether this is an unlink or an rmdir.
1139  *
1140  * Returns: errno
1141  */
1142 
1143 static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
1144 {
1145 	struct gfs2_inode *dip = GFS2_I(dir);
1146 	struct gfs2_sbd *sdp = GFS2_SB(dir);
1147 	struct inode *inode = d_inode(dentry);
1148 	struct gfs2_inode *ip = GFS2_I(inode);
1149 	struct gfs2_holder ghs[3];
1150 	struct gfs2_rgrpd *rgd;
1151 	int error;
1152 
1153 	error = gfs2_rindex_update(sdp);
1154 	if (error)
1155 		return error;
1156 
1157 	error = -EROFS;
1158 
1159 	gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
1160 	gfs2_holder_init(ip->i_gl,  LM_ST_EXCLUSIVE, 0, ghs + 1);
1161 
1162 	rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
1163 	if (!rgd)
1164 		goto out_inodes;
1165 
1166 	gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, LM_FLAG_NODE_SCOPE, ghs + 2);
1167 
1168 
1169 	error = gfs2_glock_nq(ghs); /* parent */
1170 	if (error)
1171 		goto out_parent;
1172 
1173 	error = gfs2_glock_nq(ghs + 1); /* child */
1174 	if (error)
1175 		goto out_child;
1176 
1177 	error = -ENOENT;
1178 	if (inode->i_nlink == 0)
1179 		goto out_rgrp;
1180 
1181 	if (S_ISDIR(inode->i_mode)) {
1182 		error = -ENOTEMPTY;
1183 		if (ip->i_entries > 2 || inode->i_nlink > 2)
1184 			goto out_rgrp;
1185 	}
1186 
1187 	error = gfs2_glock_nq(ghs + 2); /* rgrp */
1188 	if (error)
1189 		goto out_rgrp;
1190 
1191 	error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
1192 	if (error)
1193 		goto out_gunlock;
1194 
1195 	error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
1196 	if (error)
1197 		goto out_gunlock;
1198 
1199 	error = gfs2_unlink_inode(dip, dentry);
1200 	gfs2_trans_end(sdp);
1201 
1202 out_gunlock:
1203 	gfs2_glock_dq(ghs + 2);
1204 out_rgrp:
1205 	gfs2_glock_dq(ghs + 1);
1206 out_child:
1207 	gfs2_glock_dq(ghs);
1208 out_parent:
1209 	gfs2_holder_uninit(ghs + 2);
1210 out_inodes:
1211 	gfs2_holder_uninit(ghs + 1);
1212 	gfs2_holder_uninit(ghs);
1213 	return error;
1214 }
1215 
1216 /**
1217  * gfs2_symlink - Create a symlink
1218  * @idmap: idmap of the mount the inode was found from
1219  * @dir: The directory to create the symlink in
1220  * @dentry: The dentry to put the symlink in
1221  * @symname: The thing which the link points to
1222  *
1223  * Returns: errno
1224  */
1225 
1226 static int gfs2_symlink(struct mnt_idmap *idmap, struct inode *dir,
1227 			struct dentry *dentry, const char *symname)
1228 {
1229 	unsigned int size;
1230 
1231 	size = strlen(symname);
1232 	if (size >= gfs2_max_stuffed_size(GFS2_I(dir)))
1233 		return -ENAMETOOLONG;
1234 
1235 	return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
1236 }
1237 
1238 /**
1239  * gfs2_mkdir - Make a directory
1240  * @idmap: idmap of the mount the inode was found from
1241  * @dir: The parent directory of the new one
1242  * @dentry: The dentry of the new directory
1243  * @mode: The mode of the new directory
1244  *
1245  * Returns: errno
1246  */
1247 
1248 static int gfs2_mkdir(struct mnt_idmap *idmap, struct inode *dir,
1249 		      struct dentry *dentry, umode_t mode)
1250 {
1251 	unsigned dsize = gfs2_max_stuffed_size(GFS2_I(dir));
1252 	return gfs2_create_inode(dir, dentry, NULL, S_IFDIR | mode, 0, NULL, dsize, 0);
1253 }
1254 
1255 /**
1256  * gfs2_mknod - Make a special file
1257  * @idmap: idmap of the mount the inode was found from
1258  * @dir: The directory in which the special file will reside
1259  * @dentry: The dentry of the special file
1260  * @mode: The mode of the special file
1261  * @dev: The device specification of the special file
1262  *
1263  */
1264 
1265 static int gfs2_mknod(struct mnt_idmap *idmap, struct inode *dir,
1266 		      struct dentry *dentry, umode_t mode, dev_t dev)
1267 {
1268 	return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0);
1269 }
1270 
1271 /**
1272  * gfs2_atomic_open - Atomically open a file
1273  * @dir: The directory
1274  * @dentry: The proposed new entry
1275  * @file: The proposed new struct file
1276  * @flags: open flags
1277  * @mode: File mode
1278  *
1279  * Returns: error code or 0 for success
1280  */
1281 
1282 static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry,
1283 			    struct file *file, unsigned flags,
1284 			    umode_t mode)
1285 {
1286 	struct dentry *d;
1287 	bool excl = !!(flags & O_EXCL);
1288 
1289 	if (!d_in_lookup(dentry))
1290 		goto skip_lookup;
1291 
1292 	d = __gfs2_lookup(dir, dentry, file);
1293 	if (IS_ERR(d))
1294 		return PTR_ERR(d);
1295 	if (d != NULL)
1296 		dentry = d;
1297 	if (d_really_is_positive(dentry)) {
1298 		if (!(file->f_mode & FMODE_OPENED))
1299 			return finish_no_open(file, d);
1300 		dput(d);
1301 		return excl && (flags & O_CREAT) ? -EEXIST : 0;
1302 	}
1303 
1304 	BUG_ON(d != NULL);
1305 
1306 skip_lookup:
1307 	if (!(flags & O_CREAT))
1308 		return -ENOENT;
1309 
1310 	return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl);
1311 }
1312 
1313 /*
1314  * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1315  * @this: move this
1316  * @to: to here
1317  *
1318  * Follow @to back to the root and make sure we don't encounter @this
1319  * Assumes we already hold the rename lock.
1320  *
1321  * Returns: errno
1322  */
1323 
1324 static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1325 {
1326 	struct inode *dir = &to->i_inode;
1327 	struct super_block *sb = dir->i_sb;
1328 	struct inode *tmp;
1329 	int error = 0;
1330 
1331 	igrab(dir);
1332 
1333 	for (;;) {
1334 		if (dir == &this->i_inode) {
1335 			error = -EINVAL;
1336 			break;
1337 		}
1338 		if (dir == d_inode(sb->s_root)) {
1339 			error = 0;
1340 			break;
1341 		}
1342 
1343 		tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
1344 		if (!tmp) {
1345 			error = -ENOENT;
1346 			break;
1347 		}
1348 		if (IS_ERR(tmp)) {
1349 			error = PTR_ERR(tmp);
1350 			break;
1351 		}
1352 
1353 		iput(dir);
1354 		dir = tmp;
1355 	}
1356 
1357 	iput(dir);
1358 
1359 	return error;
1360 }
1361 
1362 /**
1363  * update_moved_ino - Update an inode that's being moved
1364  * @ip: The inode being moved
1365  * @ndip: The parent directory of the new filename
1366  * @dir_rename: True of ip is a directory
1367  *
1368  * Returns: errno
1369  */
1370 
1371 static int update_moved_ino(struct gfs2_inode *ip, struct gfs2_inode *ndip,
1372 			    int dir_rename)
1373 {
1374 	if (dir_rename)
1375 		return gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
1376 
1377 	ip->i_inode.i_ctime = current_time(&ip->i_inode);
1378 	mark_inode_dirty_sync(&ip->i_inode);
1379 	return 0;
1380 }
1381 
1382 
1383 /**
1384  * gfs2_rename - Rename a file
1385  * @odir: Parent directory of old file name
1386  * @odentry: The old dentry of the file
1387  * @ndir: Parent directory of new file name
1388  * @ndentry: The new dentry of the file
1389  *
1390  * Returns: errno
1391  */
1392 
1393 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
1394 		       struct inode *ndir, struct dentry *ndentry)
1395 {
1396 	struct gfs2_inode *odip = GFS2_I(odir);
1397 	struct gfs2_inode *ndip = GFS2_I(ndir);
1398 	struct gfs2_inode *ip = GFS2_I(d_inode(odentry));
1399 	struct gfs2_inode *nip = NULL;
1400 	struct gfs2_sbd *sdp = GFS2_SB(odir);
1401 	struct gfs2_holder ghs[4], r_gh, rd_gh;
1402 	struct gfs2_rgrpd *nrgd;
1403 	unsigned int num_gh;
1404 	int dir_rename = 0;
1405 	struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, };
1406 	unsigned int x;
1407 	int error;
1408 
1409 	gfs2_holder_mark_uninitialized(&r_gh);
1410 	gfs2_holder_mark_uninitialized(&rd_gh);
1411 	if (d_really_is_positive(ndentry)) {
1412 		nip = GFS2_I(d_inode(ndentry));
1413 		if (ip == nip)
1414 			return 0;
1415 	}
1416 
1417 	error = gfs2_rindex_update(sdp);
1418 	if (error)
1419 		return error;
1420 
1421 	error = gfs2_qa_get(ndip);
1422 	if (error)
1423 		return error;
1424 
1425 	if (odip != ndip) {
1426 		error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1427 					   0, &r_gh);
1428 		if (error)
1429 			goto out;
1430 
1431 		if (S_ISDIR(ip->i_inode.i_mode)) {
1432 			dir_rename = 1;
1433 			/* don't move a directory into its subdir */
1434 			error = gfs2_ok_to_move(ip, ndip);
1435 			if (error)
1436 				goto out_gunlock_r;
1437 		}
1438 	}
1439 
1440 	num_gh = 1;
1441 	gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
1442 	if (odip != ndip) {
1443 		gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE,GL_ASYNC,
1444 				 ghs + num_gh);
1445 		num_gh++;
1446 	}
1447 	gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
1448 	num_gh++;
1449 
1450 	if (nip) {
1451 		gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1452 				 ghs + num_gh);
1453 		num_gh++;
1454 	}
1455 
1456 	for (x = 0; x < num_gh; x++) {
1457 		error = gfs2_glock_nq(ghs + x);
1458 		if (error)
1459 			goto out_gunlock;
1460 	}
1461 	error = gfs2_glock_async_wait(num_gh, ghs);
1462 	if (error)
1463 		goto out_gunlock;
1464 
1465 	if (nip) {
1466 		/* Grab the resource group glock for unlink flag twiddling.
1467 		 * This is the case where the target dinode already exists
1468 		 * so we unlink before doing the rename.
1469 		 */
1470 		nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
1471 		if (!nrgd) {
1472 			error = -ENOENT;
1473 			goto out_gunlock;
1474 		}
1475 		error = gfs2_glock_nq_init(nrgd->rd_gl, LM_ST_EXCLUSIVE,
1476 					   LM_FLAG_NODE_SCOPE, &rd_gh);
1477 		if (error)
1478 			goto out_gunlock;
1479 	}
1480 
1481 	error = -ENOENT;
1482 	if (ip->i_inode.i_nlink == 0)
1483 		goto out_gunlock;
1484 
1485 	/* Check out the old directory */
1486 
1487 	error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
1488 	if (error)
1489 		goto out_gunlock;
1490 
1491 	/* Check out the new directory */
1492 
1493 	if (nip) {
1494 		error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1495 		if (error)
1496 			goto out_gunlock;
1497 
1498 		if (nip->i_inode.i_nlink == 0) {
1499 			error = -EAGAIN;
1500 			goto out_gunlock;
1501 		}
1502 
1503 		if (S_ISDIR(nip->i_inode.i_mode)) {
1504 			if (nip->i_entries < 2) {
1505 				gfs2_consist_inode(nip);
1506 				error = -EIO;
1507 				goto out_gunlock;
1508 			}
1509 			if (nip->i_entries > 2) {
1510 				error = -ENOTEMPTY;
1511 				goto out_gunlock;
1512 			}
1513 		}
1514 	} else {
1515 		error = gfs2_permission(&nop_mnt_idmap, ndir,
1516 					MAY_WRITE | MAY_EXEC);
1517 		if (error)
1518 			goto out_gunlock;
1519 
1520 		error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
1521 		switch (error) {
1522 		case -ENOENT:
1523 			error = 0;
1524 			break;
1525 		case 0:
1526 			error = -EEXIST;
1527 			goto out_gunlock;
1528 		default:
1529 			goto out_gunlock;
1530 		}
1531 
1532 		if (odip != ndip) {
1533 			if (!ndip->i_inode.i_nlink) {
1534 				error = -ENOENT;
1535 				goto out_gunlock;
1536 			}
1537 			if (ndip->i_entries == (u32)-1) {
1538 				error = -EFBIG;
1539 				goto out_gunlock;
1540 			}
1541 			if (S_ISDIR(ip->i_inode.i_mode) &&
1542 			    ndip->i_inode.i_nlink == (u32)-1) {
1543 				error = -EMLINK;
1544 				goto out_gunlock;
1545 			}
1546 		}
1547 	}
1548 
1549 	/* Check out the dir to be renamed */
1550 
1551 	if (dir_rename) {
1552 		error = gfs2_permission(&nop_mnt_idmap, d_inode(odentry),
1553 					MAY_WRITE);
1554 		if (error)
1555 			goto out_gunlock;
1556 	}
1557 
1558 	if (nip == NULL) {
1559 		error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
1560 		if (error)
1561 			goto out_gunlock;
1562 	}
1563 
1564 	if (da.nr_blocks) {
1565 		struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
1566 		error = gfs2_quota_lock_check(ndip, &ap);
1567 		if (error)
1568 			goto out_gunlock;
1569 
1570 		error = gfs2_inplace_reserve(ndip, &ap);
1571 		if (error)
1572 			goto out_gunlock_q;
1573 
1574 		error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
1575 					 4 * RES_LEAF + 4, 0);
1576 		if (error)
1577 			goto out_ipreserv;
1578 	} else {
1579 		error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
1580 					 5 * RES_LEAF + 4, 0);
1581 		if (error)
1582 			goto out_gunlock;
1583 	}
1584 
1585 	/* Remove the target file, if it exists */
1586 
1587 	if (nip)
1588 		error = gfs2_unlink_inode(ndip, ndentry);
1589 
1590 	error = update_moved_ino(ip, ndip, dir_rename);
1591 	if (error)
1592 		goto out_end_trans;
1593 
1594 	error = gfs2_dir_del(odip, odentry);
1595 	if (error)
1596 		goto out_end_trans;
1597 
1598 	error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
1599 	if (error)
1600 		goto out_end_trans;
1601 
1602 out_end_trans:
1603 	gfs2_trans_end(sdp);
1604 out_ipreserv:
1605 	if (da.nr_blocks)
1606 		gfs2_inplace_release(ndip);
1607 out_gunlock_q:
1608 	if (da.nr_blocks)
1609 		gfs2_quota_unlock(ndip);
1610 out_gunlock:
1611 	gfs2_dir_no_add(&da);
1612 	if (gfs2_holder_initialized(&rd_gh))
1613 		gfs2_glock_dq_uninit(&rd_gh);
1614 
1615 	while (x--) {
1616 		if (gfs2_holder_queued(ghs + x))
1617 			gfs2_glock_dq(ghs + x);
1618 		gfs2_holder_uninit(ghs + x);
1619 	}
1620 out_gunlock_r:
1621 	if (gfs2_holder_initialized(&r_gh))
1622 		gfs2_glock_dq_uninit(&r_gh);
1623 out:
1624 	gfs2_qa_put(ndip);
1625 	return error;
1626 }
1627 
1628 /**
1629  * gfs2_exchange - exchange two files
1630  * @odir: Parent directory of old file name
1631  * @odentry: The old dentry of the file
1632  * @ndir: Parent directory of new file name
1633  * @ndentry: The new dentry of the file
1634  * @flags: The rename flags
1635  *
1636  * Returns: errno
1637  */
1638 
1639 static int gfs2_exchange(struct inode *odir, struct dentry *odentry,
1640 			 struct inode *ndir, struct dentry *ndentry,
1641 			 unsigned int flags)
1642 {
1643 	struct gfs2_inode *odip = GFS2_I(odir);
1644 	struct gfs2_inode *ndip = GFS2_I(ndir);
1645 	struct gfs2_inode *oip = GFS2_I(odentry->d_inode);
1646 	struct gfs2_inode *nip = GFS2_I(ndentry->d_inode);
1647 	struct gfs2_sbd *sdp = GFS2_SB(odir);
1648 	struct gfs2_holder ghs[4], r_gh;
1649 	unsigned int num_gh;
1650 	unsigned int x;
1651 	umode_t old_mode = oip->i_inode.i_mode;
1652 	umode_t new_mode = nip->i_inode.i_mode;
1653 	int error;
1654 
1655 	gfs2_holder_mark_uninitialized(&r_gh);
1656 	error = gfs2_rindex_update(sdp);
1657 	if (error)
1658 		return error;
1659 
1660 	if (odip != ndip) {
1661 		error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1662 					   0, &r_gh);
1663 		if (error)
1664 			goto out;
1665 
1666 		if (S_ISDIR(old_mode)) {
1667 			/* don't move a directory into its subdir */
1668 			error = gfs2_ok_to_move(oip, ndip);
1669 			if (error)
1670 				goto out_gunlock_r;
1671 		}
1672 
1673 		if (S_ISDIR(new_mode)) {
1674 			/* don't move a directory into its subdir */
1675 			error = gfs2_ok_to_move(nip, odip);
1676 			if (error)
1677 				goto out_gunlock_r;
1678 		}
1679 	}
1680 
1681 	num_gh = 1;
1682 	gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
1683 	if (odip != ndip) {
1684 		gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1685 				 ghs + num_gh);
1686 		num_gh++;
1687 	}
1688 	gfs2_holder_init(oip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
1689 	num_gh++;
1690 
1691 	gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
1692 	num_gh++;
1693 
1694 	for (x = 0; x < num_gh; x++) {
1695 		error = gfs2_glock_nq(ghs + x);
1696 		if (error)
1697 			goto out_gunlock;
1698 	}
1699 
1700 	error = gfs2_glock_async_wait(num_gh, ghs);
1701 	if (error)
1702 		goto out_gunlock;
1703 
1704 	error = -ENOENT;
1705 	if (oip->i_inode.i_nlink == 0 || nip->i_inode.i_nlink == 0)
1706 		goto out_gunlock;
1707 
1708 	error = gfs2_unlink_ok(odip, &odentry->d_name, oip);
1709 	if (error)
1710 		goto out_gunlock;
1711 	error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1712 	if (error)
1713 		goto out_gunlock;
1714 
1715 	if (S_ISDIR(old_mode)) {
1716 		error = gfs2_permission(&nop_mnt_idmap, odentry->d_inode,
1717 					MAY_WRITE);
1718 		if (error)
1719 			goto out_gunlock;
1720 	}
1721 	if (S_ISDIR(new_mode)) {
1722 		error = gfs2_permission(&nop_mnt_idmap, ndentry->d_inode,
1723 					MAY_WRITE);
1724 		if (error)
1725 			goto out_gunlock;
1726 	}
1727 	error = gfs2_trans_begin(sdp, 4 * RES_DINODE + 4 * RES_LEAF, 0);
1728 	if (error)
1729 		goto out_gunlock;
1730 
1731 	error = update_moved_ino(oip, ndip, S_ISDIR(old_mode));
1732 	if (error)
1733 		goto out_end_trans;
1734 
1735 	error = update_moved_ino(nip, odip, S_ISDIR(new_mode));
1736 	if (error)
1737 		goto out_end_trans;
1738 
1739 	error = gfs2_dir_mvino(ndip, &ndentry->d_name, oip,
1740 			       IF2DT(old_mode));
1741 	if (error)
1742 		goto out_end_trans;
1743 
1744 	error = gfs2_dir_mvino(odip, &odentry->d_name, nip,
1745 			       IF2DT(new_mode));
1746 	if (error)
1747 		goto out_end_trans;
1748 
1749 	if (odip != ndip) {
1750 		if (S_ISDIR(new_mode) && !S_ISDIR(old_mode)) {
1751 			inc_nlink(&odip->i_inode);
1752 			drop_nlink(&ndip->i_inode);
1753 		} else if (S_ISDIR(old_mode) && !S_ISDIR(new_mode)) {
1754 			inc_nlink(&ndip->i_inode);
1755 			drop_nlink(&odip->i_inode);
1756 		}
1757 	}
1758 	mark_inode_dirty(&ndip->i_inode);
1759 	if (odip != ndip)
1760 		mark_inode_dirty(&odip->i_inode);
1761 
1762 out_end_trans:
1763 	gfs2_trans_end(sdp);
1764 out_gunlock:
1765 	while (x--) {
1766 		if (gfs2_holder_queued(ghs + x))
1767 			gfs2_glock_dq(ghs + x);
1768 		gfs2_holder_uninit(ghs + x);
1769 	}
1770 out_gunlock_r:
1771 	if (gfs2_holder_initialized(&r_gh))
1772 		gfs2_glock_dq_uninit(&r_gh);
1773 out:
1774 	return error;
1775 }
1776 
1777 static int gfs2_rename2(struct mnt_idmap *idmap, struct inode *odir,
1778 			struct dentry *odentry, struct inode *ndir,
1779 			struct dentry *ndentry, unsigned int flags)
1780 {
1781 	flags &= ~RENAME_NOREPLACE;
1782 
1783 	if (flags & ~RENAME_EXCHANGE)
1784 		return -EINVAL;
1785 
1786 	if (flags & RENAME_EXCHANGE)
1787 		return gfs2_exchange(odir, odentry, ndir, ndentry, flags);
1788 
1789 	return gfs2_rename(odir, odentry, ndir, ndentry);
1790 }
1791 
1792 /**
1793  * gfs2_get_link - Follow a symbolic link
1794  * @dentry: The dentry of the link
1795  * @inode: The inode of the link
1796  * @done: destructor for return value
1797  *
1798  * This can handle symlinks of any size.
1799  *
1800  * Returns: 0 on success or error code
1801  */
1802 
1803 static const char *gfs2_get_link(struct dentry *dentry,
1804 				 struct inode *inode,
1805 				 struct delayed_call *done)
1806 {
1807 	struct gfs2_inode *ip = GFS2_I(inode);
1808 	struct gfs2_holder i_gh;
1809 	struct buffer_head *dibh;
1810 	unsigned int size;
1811 	char *buf;
1812 	int error;
1813 
1814 	if (!dentry)
1815 		return ERR_PTR(-ECHILD);
1816 
1817 	gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1818 	error = gfs2_glock_nq(&i_gh);
1819 	if (error) {
1820 		gfs2_holder_uninit(&i_gh);
1821 		return ERR_PTR(error);
1822 	}
1823 
1824 	size = (unsigned int)i_size_read(&ip->i_inode);
1825 	if (size == 0) {
1826 		gfs2_consist_inode(ip);
1827 		buf = ERR_PTR(-EIO);
1828 		goto out;
1829 	}
1830 
1831 	error = gfs2_meta_inode_buffer(ip, &dibh);
1832 	if (error) {
1833 		buf = ERR_PTR(error);
1834 		goto out;
1835 	}
1836 
1837 	buf = kzalloc(size + 1, GFP_NOFS);
1838 	if (!buf)
1839 		buf = ERR_PTR(-ENOMEM);
1840 	else
1841 		memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
1842 	brelse(dibh);
1843 out:
1844 	gfs2_glock_dq_uninit(&i_gh);
1845 	if (!IS_ERR(buf))
1846 		set_delayed_call(done, kfree_link, buf);
1847 	return buf;
1848 }
1849 
1850 /**
1851  * gfs2_permission
1852  * @idmap: idmap of the mount the inode was found from
1853  * @inode: The inode
1854  * @mask: The mask to be tested
1855  *
1856  * This may be called from the VFS directly, or from within GFS2 with the
1857  * inode locked, so we look to see if the glock is already locked and only
1858  * lock the glock if its not already been done.
1859  *
1860  * Returns: errno
1861  */
1862 
1863 int gfs2_permission(struct mnt_idmap *idmap, struct inode *inode,
1864 		    int mask)
1865 {
1866 	struct gfs2_inode *ip;
1867 	struct gfs2_holder i_gh;
1868 	int error;
1869 
1870 	gfs2_holder_mark_uninitialized(&i_gh);
1871 	ip = GFS2_I(inode);
1872 	if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1873 		if (mask & MAY_NOT_BLOCK)
1874 			return -ECHILD;
1875 		error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1876 		if (error)
1877 			return error;
1878 	}
1879 
1880 	if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1881 		error = -EPERM;
1882 	else
1883 		error = generic_permission(&nop_mnt_idmap, inode, mask);
1884 	if (gfs2_holder_initialized(&i_gh))
1885 		gfs2_glock_dq_uninit(&i_gh);
1886 
1887 	return error;
1888 }
1889 
1890 static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
1891 {
1892 	setattr_copy(&nop_mnt_idmap, inode, attr);
1893 	mark_inode_dirty(inode);
1894 	return 0;
1895 }
1896 
1897 static int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
1898 {
1899 	int error;
1900 
1901 	if (current->journal_info)
1902 		return __gfs2_setattr_simple(inode, attr);
1903 
1904 	error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
1905 	if (error)
1906 		return error;
1907 
1908 	error = __gfs2_setattr_simple(inode, attr);
1909 	gfs2_trans_end(GFS2_SB(inode));
1910 	return error;
1911 }
1912 
1913 static int setattr_chown(struct inode *inode, struct iattr *attr)
1914 {
1915 	struct gfs2_inode *ip = GFS2_I(inode);
1916 	struct gfs2_sbd *sdp = GFS2_SB(inode);
1917 	kuid_t ouid, nuid;
1918 	kgid_t ogid, ngid;
1919 	int error;
1920 	struct gfs2_alloc_parms ap;
1921 
1922 	ouid = inode->i_uid;
1923 	ogid = inode->i_gid;
1924 	nuid = attr->ia_uid;
1925 	ngid = attr->ia_gid;
1926 
1927 	if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
1928 		ouid = nuid = NO_UID_QUOTA_CHANGE;
1929 	if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
1930 		ogid = ngid = NO_GID_QUOTA_CHANGE;
1931 	error = gfs2_qa_get(ip);
1932 	if (error)
1933 		return error;
1934 
1935 	error = gfs2_rindex_update(sdp);
1936 	if (error)
1937 		goto out;
1938 
1939 	error = gfs2_quota_lock(ip, nuid, ngid);
1940 	if (error)
1941 		goto out;
1942 
1943 	ap.target = gfs2_get_inode_blocks(&ip->i_inode);
1944 
1945 	if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1946 	    !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
1947 		error = gfs2_quota_check(ip, nuid, ngid, &ap);
1948 		if (error)
1949 			goto out_gunlock_q;
1950 	}
1951 
1952 	error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1953 	if (error)
1954 		goto out_gunlock_q;
1955 
1956 	error = gfs2_setattr_simple(inode, attr);
1957 	if (error)
1958 		goto out_end_trans;
1959 
1960 	if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
1961 	    !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
1962 		gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid);
1963 		gfs2_quota_change(ip, ap.target, nuid, ngid);
1964 	}
1965 
1966 out_end_trans:
1967 	gfs2_trans_end(sdp);
1968 out_gunlock_q:
1969 	gfs2_quota_unlock(ip);
1970 out:
1971 	gfs2_qa_put(ip);
1972 	return error;
1973 }
1974 
1975 /**
1976  * gfs2_setattr - Change attributes on an inode
1977  * @idmap: idmap of the mount the inode was found from
1978  * @dentry: The dentry which is changing
1979  * @attr: The structure describing the change
1980  *
1981  * The VFS layer wants to change one or more of an inodes attributes.  Write
1982  * that change out to disk.
1983  *
1984  * Returns: errno
1985  */
1986 
1987 static int gfs2_setattr(struct mnt_idmap *idmap,
1988 			struct dentry *dentry, struct iattr *attr)
1989 {
1990 	struct inode *inode = d_inode(dentry);
1991 	struct gfs2_inode *ip = GFS2_I(inode);
1992 	struct gfs2_holder i_gh;
1993 	int error;
1994 
1995 	error = gfs2_qa_get(ip);
1996 	if (error)
1997 		return error;
1998 
1999 	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
2000 	if (error)
2001 		goto out;
2002 
2003 	error = may_setattr(&nop_mnt_idmap, inode, attr->ia_valid);
2004 	if (error)
2005 		goto error;
2006 
2007 	error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
2008 	if (error)
2009 		goto error;
2010 
2011 	if (attr->ia_valid & ATTR_SIZE)
2012 		error = gfs2_setattr_size(inode, attr->ia_size);
2013 	else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
2014 		error = setattr_chown(inode, attr);
2015 	else {
2016 		error = gfs2_setattr_simple(inode, attr);
2017 		if (!error && attr->ia_valid & ATTR_MODE)
2018 			error = posix_acl_chmod(&nop_mnt_idmap, dentry,
2019 						inode->i_mode);
2020 	}
2021 
2022 error:
2023 	if (!error)
2024 		mark_inode_dirty(inode);
2025 	gfs2_glock_dq_uninit(&i_gh);
2026 out:
2027 	gfs2_qa_put(ip);
2028 	return error;
2029 }
2030 
2031 /**
2032  * gfs2_getattr - Read out an inode's attributes
2033  * @idmap: idmap of the mount the inode was found from
2034  * @path: Object to query
2035  * @stat: The inode's stats
2036  * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
2037  * @flags: AT_STATX_xxx setting
2038  *
2039  * This may be called from the VFS directly, or from within GFS2 with the
2040  * inode locked, so we look to see if the glock is already locked and only
2041  * lock the glock if its not already been done. Note that its the NFS
2042  * readdirplus operation which causes this to be called (from filldir)
2043  * with the glock already held.
2044  *
2045  * Returns: errno
2046  */
2047 
2048 static int gfs2_getattr(struct mnt_idmap *idmap,
2049 			const struct path *path, struct kstat *stat,
2050 			u32 request_mask, unsigned int flags)
2051 {
2052 	struct inode *inode = d_inode(path->dentry);
2053 	struct gfs2_inode *ip = GFS2_I(inode);
2054 	struct gfs2_holder gh;
2055 	u32 gfsflags;
2056 	int error;
2057 
2058 	gfs2_holder_mark_uninitialized(&gh);
2059 	if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
2060 		error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
2061 		if (error)
2062 			return error;
2063 	}
2064 
2065 	gfsflags = ip->i_diskflags;
2066 	if (gfsflags & GFS2_DIF_APPENDONLY)
2067 		stat->attributes |= STATX_ATTR_APPEND;
2068 	if (gfsflags & GFS2_DIF_IMMUTABLE)
2069 		stat->attributes |= STATX_ATTR_IMMUTABLE;
2070 
2071 	stat->attributes_mask |= (STATX_ATTR_APPEND |
2072 				  STATX_ATTR_COMPRESSED |
2073 				  STATX_ATTR_ENCRYPTED |
2074 				  STATX_ATTR_IMMUTABLE |
2075 				  STATX_ATTR_NODUMP);
2076 
2077 	generic_fillattr(&nop_mnt_idmap, inode, stat);
2078 
2079 	if (gfs2_holder_initialized(&gh))
2080 		gfs2_glock_dq_uninit(&gh);
2081 
2082 	return 0;
2083 }
2084 
2085 static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2086 		       u64 start, u64 len)
2087 {
2088 	struct gfs2_inode *ip = GFS2_I(inode);
2089 	struct gfs2_holder gh;
2090 	int ret;
2091 
2092 	inode_lock_shared(inode);
2093 
2094 	ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2095 	if (ret)
2096 		goto out;
2097 
2098 	ret = iomap_fiemap(inode, fieinfo, start, len, &gfs2_iomap_ops);
2099 
2100 	gfs2_glock_dq_uninit(&gh);
2101 
2102 out:
2103 	inode_unlock_shared(inode);
2104 	return ret;
2105 }
2106 
2107 loff_t gfs2_seek_data(struct file *file, loff_t offset)
2108 {
2109 	struct inode *inode = file->f_mapping->host;
2110 	struct gfs2_inode *ip = GFS2_I(inode);
2111 	struct gfs2_holder gh;
2112 	loff_t ret;
2113 
2114 	inode_lock_shared(inode);
2115 	ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2116 	if (!ret)
2117 		ret = iomap_seek_data(inode, offset, &gfs2_iomap_ops);
2118 	gfs2_glock_dq_uninit(&gh);
2119 	inode_unlock_shared(inode);
2120 
2121 	if (ret < 0)
2122 		return ret;
2123 	return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
2124 }
2125 
2126 loff_t gfs2_seek_hole(struct file *file, loff_t offset)
2127 {
2128 	struct inode *inode = file->f_mapping->host;
2129 	struct gfs2_inode *ip = GFS2_I(inode);
2130 	struct gfs2_holder gh;
2131 	loff_t ret;
2132 
2133 	inode_lock_shared(inode);
2134 	ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2135 	if (!ret)
2136 		ret = iomap_seek_hole(inode, offset, &gfs2_iomap_ops);
2137 	gfs2_glock_dq_uninit(&gh);
2138 	inode_unlock_shared(inode);
2139 
2140 	if (ret < 0)
2141 		return ret;
2142 	return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
2143 }
2144 
2145 static int gfs2_update_time(struct inode *inode, struct timespec64 *time,
2146 			    int flags)
2147 {
2148 	struct gfs2_inode *ip = GFS2_I(inode);
2149 	struct gfs2_glock *gl = ip->i_gl;
2150 	struct gfs2_holder *gh;
2151 	int error;
2152 
2153 	gh = gfs2_glock_is_locked_by_me(gl);
2154 	if (gh && !gfs2_glock_is_held_excl(gl)) {
2155 		gfs2_glock_dq(gh);
2156 		gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, gh);
2157 		error = gfs2_glock_nq(gh);
2158 		if (error)
2159 			return error;
2160 	}
2161 	return generic_update_time(inode, time, flags);
2162 }
2163 
2164 static const struct inode_operations gfs2_file_iops = {
2165 	.permission = gfs2_permission,
2166 	.setattr = gfs2_setattr,
2167 	.getattr = gfs2_getattr,
2168 	.listxattr = gfs2_listxattr,
2169 	.fiemap = gfs2_fiemap,
2170 	.get_inode_acl = gfs2_get_acl,
2171 	.set_acl = gfs2_set_acl,
2172 	.update_time = gfs2_update_time,
2173 	.fileattr_get = gfs2_fileattr_get,
2174 	.fileattr_set = gfs2_fileattr_set,
2175 };
2176 
2177 static const struct inode_operations gfs2_dir_iops = {
2178 	.create = gfs2_create,
2179 	.lookup = gfs2_lookup,
2180 	.link = gfs2_link,
2181 	.unlink = gfs2_unlink,
2182 	.symlink = gfs2_symlink,
2183 	.mkdir = gfs2_mkdir,
2184 	.rmdir = gfs2_unlink,
2185 	.mknod = gfs2_mknod,
2186 	.rename = gfs2_rename2,
2187 	.permission = gfs2_permission,
2188 	.setattr = gfs2_setattr,
2189 	.getattr = gfs2_getattr,
2190 	.listxattr = gfs2_listxattr,
2191 	.fiemap = gfs2_fiemap,
2192 	.get_inode_acl = gfs2_get_acl,
2193 	.set_acl = gfs2_set_acl,
2194 	.update_time = gfs2_update_time,
2195 	.atomic_open = gfs2_atomic_open,
2196 	.fileattr_get = gfs2_fileattr_get,
2197 	.fileattr_set = gfs2_fileattr_set,
2198 };
2199 
2200 static const struct inode_operations gfs2_symlink_iops = {
2201 	.get_link = gfs2_get_link,
2202 	.permission = gfs2_permission,
2203 	.setattr = gfs2_setattr,
2204 	.getattr = gfs2_getattr,
2205 	.listxattr = gfs2_listxattr,
2206 	.fiemap = gfs2_fiemap,
2207 };
2208 
2209