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