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