xref: /linux/fs/gfs2/inode.c (revision 368cf60c36a3a311474de08e52dc6314df3b06ce)
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 (file && (file->f_flags & __O_REGULAR) &&
742 		    !S_ISREG(inode->i_mode)) {
743 			iput(inode);
744 			inode = NULL;
745 			error = -EFTYPE;
746 			goto fail_gunlock;
747 		}
748 		if (S_ISDIR(inode->i_mode)) {
749 			iput(inode);
750 			inode = NULL;
751 			error = -EISDIR;
752 			goto fail_gunlock;
753 		}
754 		d_instantiate(dentry, inode);
755 		error = 0;
756 		if (file) {
757 			if (S_ISREG(inode->i_mode))
758 				error = finish_open(file, dentry, gfs2_open_common);
759 			else
760 				error = finish_no_open(file, NULL);
761 		}
762 		gfs2_glock_dq_uninit(&d_gh);
763 		goto fail;
764 	} else if (error != -ENOENT) {
765 		goto fail_gunlock;
766 	}
767 
768 	error = gfs2_diradd_alloc_required(dir, name, &da);
769 	if (error < 0)
770 		goto fail_gunlock;
771 
772 	inode = new_inode(sdp->sd_vfs);
773 	error = -ENOMEM;
774 	if (!inode)
775 		goto fail_gunlock;
776 	gfs2_setup_inode(inode);
777 	ip = GFS2_I(inode);
778 
779 	error = posix_acl_create(dir, &mode, &default_acl, &acl);
780 	if (error)
781 		goto fail_gunlock;
782 
783 	error = gfs2_qa_get(ip);
784 	if (error)
785 		goto fail_free_acls;
786 
787 	inode->i_mode = mode;
788 	set_nlink(inode, S_ISDIR(mode) ? 2 : 1);
789 	inode->i_rdev = dev;
790 	inode->i_size = size;
791 	simple_inode_init_ts(inode);
792 	munge_mode_uid_gid(dip, inode);
793 	check_and_update_goal(dip);
794 	ip->i_goal = dip->i_goal;
795 	ip->i_diskflags = 0;
796 	ip->i_eattr = 0;
797 	ip->i_height = 0;
798 	ip->i_depth = 0;
799 	ip->i_entries = 0;
800 	ip->i_no_addr = 0; /* Temporarily zero until real addr is assigned */
801 
802 	switch(mode & S_IFMT) {
803 	case S_IFREG:
804 		if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
805 		    gfs2_tune_get(sdp, gt_new_files_jdata))
806 			ip->i_diskflags |= GFS2_DIF_JDATA;
807 		gfs2_set_aops(inode);
808 		break;
809 	case S_IFDIR:
810 		ip->i_diskflags |= (dip->i_diskflags & GFS2_DIF_INHERIT_JDATA);
811 		ip->i_diskflags |= GFS2_DIF_JDATA;
812 		ip->i_entries = 2;
813 		break;
814 	}
815 
816 	/* Force SYSTEM flag on all files and subdirs of a SYSTEM directory */
817 	if (dip->i_diskflags & GFS2_DIF_SYSTEM)
818 		ip->i_diskflags |= GFS2_DIF_SYSTEM;
819 
820 	gfs2_set_inode_flags(inode);
821 
822 	if ((GFS2_I(d_inode(sdp->sd_root_dir)) == dip) ||
823 	    (dip->i_diskflags & GFS2_DIF_TOPDIR))
824 		aflags |= GFS2_AF_ORLOV;
825 
826 	if (default_acl || acl)
827 		blocks++;
828 
829 	error = alloc_dinode(ip, aflags, &blocks);
830 	if (error)
831 		goto fail_free_inode;
832 
833 	gfs2_set_inode_blocks(inode, blocks);
834 
835 	error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
836 	if (error)
837 		goto fail_dealloc_inode;
838 
839 	error = gfs2_glock_get(sdp, ip->i_no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
840 	if (error)
841 		goto fail_dealloc_inode;
842 	gfs2_cancel_delete_work(io_gl);
843 	io_gl->gl_no_formal_ino = ip->i_no_formal_ino;
844 
845 retry:
846 	error = insert_inode_locked4(inode, ip->i_no_addr, iget_test, &ip->i_no_addr);
847 	if (error == -EBUSY)
848 		goto retry;
849 	if (error)
850 		goto fail_gunlock2;
851 
852 	error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT | GL_NOPID,
853 				   &ip->i_iopen_gh);
854 	if (error)
855 		goto fail_gunlock2;
856 
857 	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, &gh);
858 	if (error)
859 		goto fail_gunlock3;
860 	clear_bit(GLF_INSTANTIATE_NEEDED, &ip->i_gl->gl_flags);
861 
862 	error = gfs2_trans_begin(sdp, blocks, 0);
863 	if (error)
864 		goto fail_gunlock3;
865 
866 	if (blocks > 1) {
867 		gfs2_init_xattr(ip);
868 		xattr_initialized = true;
869 	}
870 	init_dinode(dip, ip, symname);
871 	gfs2_trans_end(sdp);
872 
873 	glock_set_object(ip->i_gl, ip);
874 	glock_set_object(io_gl, ip);
875 	gfs2_set_iop(inode);
876 
877 	if (default_acl) {
878 		error = __gfs2_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
879 		if (error)
880 			goto fail_gunlock4;
881 		posix_acl_release(default_acl);
882 		default_acl = NULL;
883 	}
884 	if (acl) {
885 		error = __gfs2_set_acl(inode, acl, ACL_TYPE_ACCESS);
886 		if (error)
887 			goto fail_gunlock4;
888 		posix_acl_release(acl);
889 		acl = NULL;
890 	}
891 
892 	error = security_inode_init_security(&ip->i_inode, &dip->i_inode, name,
893 					     &gfs2_initxattrs, NULL);
894 	if (error)
895 		goto fail_gunlock4;
896 
897 	error = link_dinode(dip, name, ip, &da);
898 	if (error)
899 		goto fail_gunlock4;
900 
901 	mark_inode_dirty(inode);
902 	d_instantiate_new(dentry, inode);
903 	/* After instantiate, errors should result in evict which will destroy
904 	 * both inode and iopen glocks properly. */
905 	if (file) {
906 		file->f_mode |= FMODE_CREATED;
907 		error = finish_open(file, dentry, gfs2_open_common);
908 	}
909 	gfs2_glock_dq_uninit(&d_gh);
910 	gfs2_qa_put(ip);
911 	gfs2_glock_dq_uninit(&gh);
912 	gfs2_glock_put(io_gl);
913 	gfs2_qa_put(dip);
914 	return error;
915 
916 fail_gunlock4:
917 	glock_clear_object(ip->i_gl, ip);
918 	glock_clear_object(io_gl, ip);
919 fail_gunlock3:
920 	gfs2_glock_dq_uninit(&ip->i_iopen_gh);
921 fail_gunlock2:
922 	gfs2_glock_put(io_gl);
923 fail_dealloc_inode:
924 	dealloc_error = 0;
925 	if (ip->i_eattr)
926 		dealloc_error = gfs2_ea_dealloc(ip, xattr_initialized);
927 	clear_nlink(inode);
928 	mark_inode_dirty(inode);
929 	if (!dealloc_error)
930 		dealloc_error = gfs2_dinode_dealloc(ip);
931 	if (dealloc_error)
932 		fs_warn(sdp, "%s: %d\n", __func__, dealloc_error);
933 	ip->i_no_addr = 0;
934 fail_free_inode:
935 	if (ip->i_gl) {
936 		gfs2_glock_put(ip->i_gl);
937 		ip->i_gl = NULL;
938 	}
939 	gfs2_rs_deltree(&ip->i_res);
940 	gfs2_qa_put(ip);
941 fail_free_acls:
942 	posix_acl_release(default_acl);
943 	posix_acl_release(acl);
944 fail_gunlock:
945 	gfs2_dir_no_add(&da);
946 	gfs2_glock_dq_uninit(&d_gh);
947 	if (!IS_ERR_OR_NULL(inode)) {
948 		if (inode_state_read_once(inode) & I_NEW)
949 			iget_failed(inode);
950 		else
951 			iput(inode);
952 	}
953 	if (gfs2_holder_initialized(&gh))
954 		gfs2_glock_dq_uninit(&gh);
955 fail:
956 	gfs2_qa_put(dip);
957 	return error;
958 }
959 
960 /**
961  * gfs2_create - Create a file
962  * @idmap: idmap of the mount the inode was found from
963  * @dir: The directory in which to create the file
964  * @dentry: The dentry of the new file
965  * @mode: The mode of the new file
966  *
967  * Returns: errno
968  */
969 
970 static int gfs2_create(struct mnt_idmap *idmap, struct inode *dir,
971 		       struct dentry *dentry, umode_t mode)
972 {
973 	return gfs2_create_inode(dir, dentry, NULL, S_IFREG | mode, 0, NULL, 0, 1);
974 }
975 
976 /**
977  * __gfs2_lookup - Look up a filename in a directory and return its inode
978  * @dir: The directory inode
979  * @dentry: The dentry of the new inode
980  * @file: File to be opened
981  *
982  *
983  * Returns: errno
984  */
985 
986 static struct dentry *__gfs2_lookup(struct inode *dir, struct dentry *dentry,
987 				    struct file *file)
988 {
989 	struct inode *inode;
990 	struct dentry *d;
991 	struct gfs2_holder gh;
992 	struct gfs2_glock *gl;
993 	int error;
994 
995 	inode = gfs2_lookupi(dir, &dentry->d_name, 0);
996 	if (inode == NULL || IS_ERR(inode))
997 		return d_splice_alias(inode, dentry);
998 
999 	gl = GFS2_I(inode)->i_gl;
1000 	error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1001 	if (error) {
1002 		iput(inode);
1003 		return ERR_PTR(error);
1004 	}
1005 
1006 	d = d_splice_alias(inode, dentry);
1007 	if (IS_ERR(d)) {
1008 		gfs2_glock_dq_uninit(&gh);
1009 		return d;
1010 	}
1011 	if (file && S_ISREG(inode->i_mode))
1012 		error = finish_open(file, dentry, gfs2_open_common);
1013 
1014 	gfs2_glock_dq_uninit(&gh);
1015 	if (error) {
1016 		dput(d);
1017 		return ERR_PTR(error);
1018 	}
1019 	return d;
1020 }
1021 
1022 static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
1023 				  unsigned flags)
1024 {
1025 	return __gfs2_lookup(dir, dentry, NULL);
1026 }
1027 
1028 /**
1029  * gfs2_link - Link to a file
1030  * @old_dentry: The inode to link
1031  * @dir: Add link to this directory
1032  * @dentry: The name of the link
1033  *
1034  * Link the inode in "old_dentry" into the directory "dir" with the
1035  * name in "dentry".
1036  *
1037  * Returns: errno
1038  */
1039 
1040 static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
1041 		     struct dentry *dentry)
1042 {
1043 	struct gfs2_inode *dip = GFS2_I(dir);
1044 	struct gfs2_sbd *sdp = GFS2_SB(dir);
1045 	struct inode *inode = d_inode(old_dentry);
1046 	struct gfs2_inode *ip = GFS2_I(inode);
1047 	struct gfs2_holder d_gh, gh;
1048 	struct buffer_head *dibh;
1049 	struct gfs2_diradd da = { .bh = NULL, .save_loc = 1, };
1050 	int error;
1051 
1052 	if (S_ISDIR(inode->i_mode))
1053 		return -EPERM;
1054 
1055 	error = gfs2_qa_get(dip);
1056 	if (error)
1057 		return error;
1058 
1059 	gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, &d_gh);
1060 	gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1061 
1062 	error = gfs2_glock_nq(&d_gh);
1063 	if (error)
1064 		goto out_parent;
1065 
1066 	error = gfs2_glock_nq(&gh);
1067 	if (error)
1068 		goto out_child;
1069 
1070 	error = -ENOENT;
1071 	if (inode->i_nlink == 0)
1072 		goto out_gunlock;
1073 
1074 	error = gfs2_permission(&nop_mnt_idmap, dir, MAY_WRITE | MAY_EXEC);
1075 	if (error)
1076 		goto out_gunlock;
1077 
1078 	error = gfs2_dir_check(dir, &dentry->d_name, NULL);
1079 	switch (error) {
1080 	case -ENOENT:
1081 		break;
1082 	case 0:
1083 		error = -EEXIST;
1084 		goto out_gunlock;
1085 	default:
1086 		goto out_gunlock;
1087 	}
1088 
1089 	error = -EINVAL;
1090 	if (!dip->i_inode.i_nlink)
1091 		goto out_gunlock;
1092 	error = -EFBIG;
1093 	if (dip->i_entries == (u32)-1)
1094 		goto out_gunlock;
1095 	error = -EPERM;
1096 	if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1097 		goto out_gunlock;
1098 	error = -EMLINK;
1099 	if (ip->i_inode.i_nlink == (u32)-1)
1100 		goto out_gunlock;
1101 
1102 	error = gfs2_diradd_alloc_required(dir, &dentry->d_name, &da);
1103 	if (error < 0)
1104 		goto out_gunlock;
1105 
1106 	if (da.nr_blocks) {
1107 		struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
1108 		error = gfs2_quota_lock_check(dip, &ap);
1109 		if (error)
1110 			goto out_gunlock;
1111 
1112 		error = gfs2_inplace_reserve(dip, &ap);
1113 		if (error)
1114 			goto out_gunlock_q;
1115 
1116 		error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(dip, &da, 2), 0);
1117 		if (error)
1118 			goto out_ipres;
1119 	} else {
1120 		error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
1121 		if (error)
1122 			goto out_ipres;
1123 	}
1124 
1125 	error = gfs2_meta_inode_buffer(ip, &dibh);
1126 	if (error)
1127 		goto out_end_trans;
1128 
1129 	error = gfs2_dir_add(dir, &dentry->d_name, ip, &da);
1130 	if (error)
1131 		goto out_brelse;
1132 
1133 	gfs2_trans_add_meta(ip->i_gl, dibh);
1134 	inc_nlink(&ip->i_inode);
1135 	inode_set_ctime_current(&ip->i_inode);
1136 	ihold(inode);
1137 	d_instantiate(dentry, inode);
1138 	mark_inode_dirty(inode);
1139 
1140 out_brelse:
1141 	brelse(dibh);
1142 out_end_trans:
1143 	gfs2_trans_end(sdp);
1144 out_ipres:
1145 	if (da.nr_blocks)
1146 		gfs2_inplace_release(dip);
1147 out_gunlock_q:
1148 	if (da.nr_blocks)
1149 		gfs2_quota_unlock(dip);
1150 out_gunlock:
1151 	gfs2_dir_no_add(&da);
1152 	gfs2_glock_dq(&gh);
1153 out_child:
1154 	gfs2_glock_dq(&d_gh);
1155 out_parent:
1156 	gfs2_qa_put(dip);
1157 	gfs2_holder_uninit(&d_gh);
1158 	gfs2_holder_uninit(&gh);
1159 	return error;
1160 }
1161 
1162 /*
1163  * gfs2_unlink_ok - check to see that a inode is still in a directory
1164  * @dip: the directory
1165  * @name: the name of the file
1166  * @ip: the inode
1167  *
1168  * Assumes that the lock on (at least) @dip is held.
1169  *
1170  * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1171  */
1172 
1173 static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
1174 			  const struct gfs2_inode *ip)
1175 {
1176 	int error;
1177 
1178 	if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1179 		return -EPERM;
1180 
1181 	if ((dip->i_inode.i_mode & S_ISVTX) &&
1182 	    !uid_eq(dip->i_inode.i_uid, current_fsuid()) &&
1183 	    !uid_eq(ip->i_inode.i_uid, current_fsuid()) && !capable(CAP_FOWNER))
1184 		return -EPERM;
1185 
1186 	if (IS_APPEND(&dip->i_inode))
1187 		return -EPERM;
1188 
1189 	error = gfs2_permission(&nop_mnt_idmap, &dip->i_inode,
1190 				MAY_WRITE | MAY_EXEC);
1191 	if (error)
1192 		return error;
1193 
1194 	return gfs2_dir_check(&dip->i_inode, name, ip);
1195 }
1196 
1197 /**
1198  * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
1199  * @dip: The parent directory
1200  * @dentry: The dentry to unlink
1201  *
1202  * Called with all the locks and in a transaction. This will only be
1203  * called for a directory after it has been checked to ensure it is empty.
1204  *
1205  * Returns: 0 on success, or an error
1206  */
1207 
1208 static int gfs2_unlink_inode(struct gfs2_inode *dip,
1209 			     const struct dentry *dentry)
1210 {
1211 	struct inode *inode = d_inode(dentry);
1212 	struct gfs2_inode *ip = GFS2_I(inode);
1213 	int error;
1214 
1215 	error = gfs2_dir_del(dip, dentry);
1216 	if (error)
1217 		return error;
1218 
1219 	ip->i_entries = 0;
1220 	inode_set_ctime_current(inode);
1221 	if (S_ISDIR(inode->i_mode))
1222 		clear_nlink(inode);
1223 	else
1224 		drop_nlink(inode);
1225 	mark_inode_dirty(inode);
1226 	if (inode->i_nlink == 0)
1227 		gfs2_unlink_di(inode);
1228 	return 0;
1229 }
1230 
1231 
1232 /**
1233  * gfs2_unlink - Unlink an inode (this does rmdir as well)
1234  * @dir: The inode of the directory containing the inode to unlink
1235  * @dentry: The file itself
1236  *
1237  * This routine uses the type of the inode as a flag to figure out
1238  * whether this is an unlink or an rmdir.
1239  *
1240  * Returns: errno
1241  */
1242 
1243 static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
1244 {
1245 	struct gfs2_inode *dip = GFS2_I(dir);
1246 	struct gfs2_sbd *sdp = GFS2_SB(dir);
1247 	struct inode *inode = d_inode(dentry);
1248 	struct gfs2_inode *ip = GFS2_I(inode);
1249 	struct gfs2_holder d_gh, r_gh, gh;
1250 	struct gfs2_rgrpd *rgd;
1251 	int error;
1252 
1253 	error = gfs2_rindex_update(sdp);
1254 	if (error)
1255 		return error;
1256 
1257 	error = -EROFS;
1258 
1259 	gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, &d_gh);
1260 	gfs2_holder_init(ip->i_gl,  LM_ST_EXCLUSIVE, 0, &gh);
1261 
1262 	rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
1263 	if (!rgd)
1264 		goto out_inodes;
1265 
1266 	gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, LM_FLAG_NODE_SCOPE, &r_gh);
1267 
1268 
1269 	error = gfs2_glock_nq(&d_gh);
1270 	if (error)
1271 		goto out_parent;
1272 
1273 	error = gfs2_glock_nq(&gh);
1274 	if (error)
1275 		goto out_child;
1276 
1277 	error = -ENOENT;
1278 	if (inode->i_nlink == 0)
1279 		goto out_rgrp;
1280 
1281 	if (S_ISDIR(inode->i_mode)) {
1282 		error = -ENOTEMPTY;
1283 		if (ip->i_entries > 2 || inode->i_nlink > 2)
1284 			goto out_rgrp;
1285 	}
1286 
1287 	error = gfs2_glock_nq(&r_gh); /* rgrp */
1288 	if (error)
1289 		goto out_rgrp;
1290 
1291 	error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
1292 	if (error)
1293 		goto out_gunlock;
1294 
1295 	error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
1296 	if (error)
1297 		goto out_gunlock;
1298 
1299 	error = gfs2_unlink_inode(dip, dentry);
1300 	gfs2_trans_end(sdp);
1301 
1302 out_gunlock:
1303 	gfs2_glock_dq(&r_gh);
1304 out_rgrp:
1305 	gfs2_glock_dq(&gh);
1306 out_child:
1307 	gfs2_glock_dq(&d_gh);
1308 out_parent:
1309 	gfs2_holder_uninit(&r_gh);
1310 out_inodes:
1311 	gfs2_holder_uninit(&gh);
1312 	gfs2_holder_uninit(&d_gh);
1313 	return error;
1314 }
1315 
1316 /**
1317  * gfs2_symlink - Create a symlink
1318  * @idmap: idmap of the mount the inode was found from
1319  * @dir: The directory to create the symlink in
1320  * @dentry: The dentry to put the symlink in
1321  * @symname: The thing which the link points to
1322  *
1323  * Returns: errno
1324  */
1325 
1326 static int gfs2_symlink(struct mnt_idmap *idmap, struct inode *dir,
1327 			struct dentry *dentry, const char *symname)
1328 {
1329 	unsigned int size;
1330 
1331 	size = strlen(symname);
1332 	if (size >= gfs2_max_stuffed_size(GFS2_I(dir)))
1333 		return -ENAMETOOLONG;
1334 
1335 	return gfs2_create_inode(dir, dentry, NULL, S_IFLNK | S_IRWXUGO, 0, symname, size, 0);
1336 }
1337 
1338 /**
1339  * gfs2_mkdir - Make a directory
1340  * @idmap: idmap of the mount the inode was found from
1341  * @dir: The parent directory of the new one
1342  * @dentry: The dentry of the new directory
1343  * @mode: The mode of the new directory
1344  *
1345  * Returns: the dentry, or ERR_PTR(errno)
1346  */
1347 
1348 static struct dentry *gfs2_mkdir(struct mnt_idmap *idmap, struct inode *dir,
1349 				 struct dentry *dentry, umode_t mode)
1350 {
1351 	unsigned dsize = gfs2_max_stuffed_size(GFS2_I(dir));
1352 
1353 	return ERR_PTR(gfs2_create_inode(dir, dentry, NULL, mode, 0, NULL, dsize, 0));
1354 }
1355 
1356 /**
1357  * gfs2_mknod - Make a special file
1358  * @idmap: idmap of the mount the inode was found from
1359  * @dir: The directory in which the special file will reside
1360  * @dentry: The dentry of the special file
1361  * @mode: The mode of the special file
1362  * @dev: The device specification of the special file
1363  *
1364  */
1365 
1366 static int gfs2_mknod(struct mnt_idmap *idmap, struct inode *dir,
1367 		      struct dentry *dentry, umode_t mode, dev_t dev)
1368 {
1369 	return gfs2_create_inode(dir, dentry, NULL, mode, dev, NULL, 0, 0);
1370 }
1371 
1372 /**
1373  * gfs2_atomic_open - Atomically open a file
1374  * @dir: The directory
1375  * @dentry: The proposed new entry
1376  * @file: The proposed new struct file
1377  * @flags: open flags
1378  * @mode: File mode
1379  *
1380  * Returns: error code or 0 for success
1381  */
1382 
1383 static int gfs2_atomic_open(struct inode *dir, struct dentry *dentry,
1384 			    struct file *file, unsigned flags,
1385 			    umode_t mode)
1386 {
1387 	bool excl = !!(flags & O_EXCL);
1388 
1389 	if (d_in_lookup(dentry)) {
1390 		struct dentry *d = __gfs2_lookup(dir, dentry, file);
1391 		if (file->f_mode & FMODE_OPENED) {
1392 			if (IS_ERR(d))
1393 				return PTR_ERR(d);
1394 			dput(d);
1395 			return excl && (flags & O_CREAT) ? -EEXIST : 0;
1396 		}
1397 		if (d || d_really_is_positive(dentry))
1398 			return finish_no_open(file, d);
1399 	}
1400 	if (!(flags & O_CREAT))
1401 		return -ENOENT;
1402 
1403 	return gfs2_create_inode(dir, dentry, file, S_IFREG | mode, 0, NULL, 0, excl);
1404 }
1405 
1406 /*
1407  * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1408  * @this: move this
1409  * @to: to here
1410  *
1411  * Follow @to back to the root and make sure we don't encounter @this
1412  * Assumes we already hold the rename lock.
1413  *
1414  * Returns: errno
1415  */
1416 
1417 static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1418 {
1419 	struct inode *dir = &to->i_inode;
1420 	struct super_block *sb = dir->i_sb;
1421 	struct inode *tmp;
1422 	int error = 0;
1423 
1424 	igrab(dir);
1425 
1426 	for (;;) {
1427 		if (dir == &this->i_inode) {
1428 			error = -EINVAL;
1429 			break;
1430 		}
1431 		if (dir == d_inode(sb->s_root)) {
1432 			error = 0;
1433 			break;
1434 		}
1435 
1436 		tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
1437 		if (!tmp) {
1438 			error = -ENOENT;
1439 			break;
1440 		}
1441 		if (IS_ERR(tmp)) {
1442 			error = PTR_ERR(tmp);
1443 			break;
1444 		}
1445 
1446 		iput(dir);
1447 		dir = tmp;
1448 	}
1449 
1450 	iput(dir);
1451 
1452 	return error;
1453 }
1454 
1455 /**
1456  * update_moved_ino - Update an inode that's being moved
1457  * @ip: The inode being moved
1458  * @ndip: The parent directory of the new filename
1459  * @dir_rename: True of ip is a directory
1460  *
1461  * Returns: errno
1462  */
1463 
1464 static int update_moved_ino(struct gfs2_inode *ip, struct gfs2_inode *ndip,
1465 			    int dir_rename)
1466 {
1467 	if (dir_rename)
1468 		return gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
1469 
1470 	inode_set_ctime_current(&ip->i_inode);
1471 	mark_inode_dirty_sync(&ip->i_inode);
1472 	return 0;
1473 }
1474 
1475 
1476 /**
1477  * gfs2_rename - Rename a file
1478  * @odir: Parent directory of old file name
1479  * @odentry: The old dentry of the file
1480  * @ndir: Parent directory of new file name
1481  * @ndentry: The new dentry of the file
1482  *
1483  * Returns: errno
1484  */
1485 
1486 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
1487 		       struct inode *ndir, struct dentry *ndentry)
1488 {
1489 	struct gfs2_inode *odip = GFS2_I(odir);
1490 	struct gfs2_inode *ndip = GFS2_I(ndir);
1491 	struct gfs2_inode *ip = GFS2_I(d_inode(odentry));
1492 	struct gfs2_inode *nip = NULL;
1493 	struct gfs2_sbd *sdp = GFS2_SB(odir);
1494 	struct gfs2_holder ghs[4], r_gh, rd_gh;
1495 	struct gfs2_rgrpd *nrgd;
1496 	unsigned int num_gh;
1497 	int dir_rename = 0;
1498 	struct gfs2_diradd da = { .nr_blocks = 0, .save_loc = 0, };
1499 	unsigned int retries = 0, x;
1500 	int error;
1501 
1502 	gfs2_holder_mark_uninitialized(&r_gh);
1503 	gfs2_holder_mark_uninitialized(&rd_gh);
1504 	if (d_really_is_positive(ndentry)) {
1505 		nip = GFS2_I(d_inode(ndentry));
1506 		if (ip == nip)
1507 			return 0;
1508 	}
1509 
1510 	error = gfs2_rindex_update(sdp);
1511 	if (error)
1512 		return error;
1513 
1514 	error = gfs2_qa_get(ndip);
1515 	if (error)
1516 		return error;
1517 
1518 	if (odip != ndip) {
1519 		error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1520 					   0, &r_gh);
1521 		if (error)
1522 			goto out;
1523 
1524 		if (S_ISDIR(ip->i_inode.i_mode)) {
1525 			dir_rename = 1;
1526 			/* don't move a directory into its subdir */
1527 			error = gfs2_ok_to_move(ip, ndip);
1528 			if (error)
1529 				goto out_gunlock_r;
1530 		}
1531 	}
1532 
1533 	num_gh = 1;
1534 	gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
1535 	if (odip != ndip) {
1536 		gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE,GL_ASYNC,
1537 				 ghs + num_gh);
1538 		num_gh++;
1539 	}
1540 	gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
1541 	num_gh++;
1542 
1543 	if (nip) {
1544 		gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1545 				 ghs + num_gh);
1546 		num_gh++;
1547 	}
1548 
1549 again:
1550 	for (x = 0; x < num_gh; x++) {
1551 		error = gfs2_glock_nq(ghs + x);
1552 		if (error)
1553 			goto out_gunlock;
1554 	}
1555 	error = gfs2_glock_async_wait(num_gh, ghs, retries);
1556 	if (error == -ESTALE) {
1557 		retries++;
1558 		goto again;
1559 	}
1560 	if (error)
1561 		goto out_gunlock;
1562 
1563 	if (nip) {
1564 		/* Grab the resource group glock for unlink flag twiddling.
1565 		 * This is the case where the target dinode already exists
1566 		 * so we unlink before doing the rename.
1567 		 */
1568 		nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr, 1);
1569 		if (!nrgd) {
1570 			error = -ENOENT;
1571 			goto out_gunlock;
1572 		}
1573 		error = gfs2_glock_nq_init(nrgd->rd_gl, LM_ST_EXCLUSIVE,
1574 					   LM_FLAG_NODE_SCOPE, &rd_gh);
1575 		if (error)
1576 			goto out_gunlock;
1577 	}
1578 
1579 	error = -ENOENT;
1580 	if (ip->i_inode.i_nlink == 0)
1581 		goto out_gunlock;
1582 
1583 	/* Check out the old directory */
1584 
1585 	error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
1586 	if (error)
1587 		goto out_gunlock;
1588 
1589 	/* Check out the new directory */
1590 
1591 	if (nip) {
1592 		error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1593 		if (error)
1594 			goto out_gunlock;
1595 
1596 		if (nip->i_inode.i_nlink == 0) {
1597 			error = -EAGAIN;
1598 			goto out_gunlock;
1599 		}
1600 
1601 		if (S_ISDIR(nip->i_inode.i_mode)) {
1602 			if (nip->i_entries < 2) {
1603 				gfs2_consist_inode(nip);
1604 				error = -EIO;
1605 				goto out_gunlock;
1606 			}
1607 			if (nip->i_entries > 2) {
1608 				error = -ENOTEMPTY;
1609 				goto out_gunlock;
1610 			}
1611 		}
1612 	} else {
1613 		error = gfs2_permission(&nop_mnt_idmap, ndir,
1614 					MAY_WRITE | MAY_EXEC);
1615 		if (error)
1616 			goto out_gunlock;
1617 
1618 		error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
1619 		switch (error) {
1620 		case -ENOENT:
1621 			error = 0;
1622 			break;
1623 		case 0:
1624 			error = -EEXIST;
1625 			goto out_gunlock;
1626 		default:
1627 			goto out_gunlock;
1628 		}
1629 
1630 		if (odip != ndip) {
1631 			if (!ndip->i_inode.i_nlink) {
1632 				error = -ENOENT;
1633 				goto out_gunlock;
1634 			}
1635 			if (ndip->i_entries == (u32)-1) {
1636 				error = -EFBIG;
1637 				goto out_gunlock;
1638 			}
1639 			if (S_ISDIR(ip->i_inode.i_mode) &&
1640 			    ndip->i_inode.i_nlink == (u32)-1) {
1641 				error = -EMLINK;
1642 				goto out_gunlock;
1643 			}
1644 		}
1645 	}
1646 
1647 	/* Check out the dir to be renamed */
1648 
1649 	if (dir_rename) {
1650 		error = gfs2_permission(&nop_mnt_idmap, d_inode(odentry),
1651 					MAY_WRITE);
1652 		if (error)
1653 			goto out_gunlock;
1654 	}
1655 
1656 	if (nip == NULL) {
1657 		error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name, &da);
1658 		if (error)
1659 			goto out_gunlock;
1660 	}
1661 
1662 	if (da.nr_blocks) {
1663 		struct gfs2_alloc_parms ap = { .target = da.nr_blocks, };
1664 		error = gfs2_quota_lock_check(ndip, &ap);
1665 		if (error)
1666 			goto out_gunlock;
1667 
1668 		error = gfs2_inplace_reserve(ndip, &ap);
1669 		if (error)
1670 			goto out_gunlock_q;
1671 
1672 		error = gfs2_trans_begin(sdp, gfs2_trans_da_blks(ndip, &da, 4) +
1673 					 4 * RES_LEAF + 4, 0);
1674 		if (error)
1675 			goto out_ipreserv;
1676 	} else {
1677 		error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
1678 					 5 * RES_LEAF + 4, 0);
1679 		if (error)
1680 			goto out_gunlock;
1681 	}
1682 
1683 	/* Remove the target file, if it exists */
1684 
1685 	if (nip)
1686 		error = gfs2_unlink_inode(ndip, ndentry);
1687 
1688 	error = update_moved_ino(ip, ndip, dir_rename);
1689 	if (error)
1690 		goto out_end_trans;
1691 
1692 	error = gfs2_dir_del(odip, odentry);
1693 	if (error)
1694 		goto out_end_trans;
1695 
1696 	error = gfs2_dir_add(ndir, &ndentry->d_name, ip, &da);
1697 	if (error)
1698 		goto out_end_trans;
1699 
1700 out_end_trans:
1701 	gfs2_trans_end(sdp);
1702 out_ipreserv:
1703 	if (da.nr_blocks)
1704 		gfs2_inplace_release(ndip);
1705 out_gunlock_q:
1706 	if (da.nr_blocks)
1707 		gfs2_quota_unlock(ndip);
1708 out_gunlock:
1709 	gfs2_dir_no_add(&da);
1710 	if (gfs2_holder_initialized(&rd_gh))
1711 		gfs2_glock_dq_uninit(&rd_gh);
1712 
1713 	while (x--) {
1714 		if (gfs2_holder_queued(ghs + x))
1715 			gfs2_glock_dq(ghs + x);
1716 		gfs2_holder_uninit(ghs + x);
1717 	}
1718 out_gunlock_r:
1719 	if (gfs2_holder_initialized(&r_gh))
1720 		gfs2_glock_dq_uninit(&r_gh);
1721 out:
1722 	gfs2_qa_put(ndip);
1723 	return error;
1724 }
1725 
1726 /**
1727  * gfs2_exchange - exchange two files
1728  * @odir: Parent directory of old file name
1729  * @odentry: The old dentry of the file
1730  * @ndir: Parent directory of new file name
1731  * @ndentry: The new dentry of the file
1732  * @flags: The rename flags
1733  *
1734  * Returns: errno
1735  */
1736 
1737 static int gfs2_exchange(struct inode *odir, struct dentry *odentry,
1738 			 struct inode *ndir, struct dentry *ndentry,
1739 			 unsigned int flags)
1740 {
1741 	struct gfs2_inode *odip = GFS2_I(odir);
1742 	struct gfs2_inode *ndip = GFS2_I(ndir);
1743 	struct gfs2_inode *oip = GFS2_I(odentry->d_inode);
1744 	struct gfs2_inode *nip = GFS2_I(ndentry->d_inode);
1745 	struct gfs2_sbd *sdp = GFS2_SB(odir);
1746 	struct gfs2_holder ghs[4], r_gh;
1747 	unsigned int num_gh;
1748 	unsigned int retries = 0, x;
1749 	umode_t old_mode = oip->i_inode.i_mode;
1750 	umode_t new_mode = nip->i_inode.i_mode;
1751 	int error;
1752 
1753 	gfs2_holder_mark_uninitialized(&r_gh);
1754 	error = gfs2_rindex_update(sdp);
1755 	if (error)
1756 		return error;
1757 
1758 	if (odip != ndip) {
1759 		error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
1760 					   0, &r_gh);
1761 		if (error)
1762 			goto out;
1763 
1764 		if (S_ISDIR(old_mode)) {
1765 			/* don't move a directory into its subdir */
1766 			error = gfs2_ok_to_move(oip, ndip);
1767 			if (error)
1768 				goto out_gunlock_r;
1769 		}
1770 
1771 		if (S_ISDIR(new_mode)) {
1772 			/* don't move a directory into its subdir */
1773 			error = gfs2_ok_to_move(nip, odip);
1774 			if (error)
1775 				goto out_gunlock_r;
1776 		}
1777 	}
1778 
1779 	num_gh = 1;
1780 	gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs);
1781 	if (odip != ndip) {
1782 		gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC,
1783 				 ghs + num_gh);
1784 		num_gh++;
1785 	}
1786 	gfs2_holder_init(oip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
1787 	num_gh++;
1788 
1789 	gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, GL_ASYNC, ghs + num_gh);
1790 	num_gh++;
1791 
1792 again:
1793 	for (x = 0; x < num_gh; x++) {
1794 		error = gfs2_glock_nq(ghs + x);
1795 		if (error)
1796 			goto out_gunlock;
1797 	}
1798 
1799 	error = gfs2_glock_async_wait(num_gh, ghs, retries);
1800 	if (error == -ESTALE) {
1801 		retries++;
1802 		goto again;
1803 	}
1804 	if (error)
1805 		goto out_gunlock;
1806 
1807 	error = -ENOENT;
1808 	if (oip->i_inode.i_nlink == 0 || nip->i_inode.i_nlink == 0)
1809 		goto out_gunlock;
1810 
1811 	error = gfs2_unlink_ok(odip, &odentry->d_name, oip);
1812 	if (error)
1813 		goto out_gunlock;
1814 	error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
1815 	if (error)
1816 		goto out_gunlock;
1817 
1818 	if (S_ISDIR(old_mode)) {
1819 		error = gfs2_permission(&nop_mnt_idmap, odentry->d_inode,
1820 					MAY_WRITE);
1821 		if (error)
1822 			goto out_gunlock;
1823 	}
1824 	if (S_ISDIR(new_mode)) {
1825 		error = gfs2_permission(&nop_mnt_idmap, ndentry->d_inode,
1826 					MAY_WRITE);
1827 		if (error)
1828 			goto out_gunlock;
1829 	}
1830 	error = gfs2_trans_begin(sdp, 4 * RES_DINODE + 4 * RES_LEAF, 0);
1831 	if (error)
1832 		goto out_gunlock;
1833 
1834 	error = update_moved_ino(oip, ndip, S_ISDIR(old_mode));
1835 	if (error)
1836 		goto out_end_trans;
1837 
1838 	error = update_moved_ino(nip, odip, S_ISDIR(new_mode));
1839 	if (error)
1840 		goto out_end_trans;
1841 
1842 	error = gfs2_dir_mvino(ndip, &ndentry->d_name, oip,
1843 			       IF2DT(old_mode));
1844 	if (error)
1845 		goto out_end_trans;
1846 
1847 	error = gfs2_dir_mvino(odip, &odentry->d_name, nip,
1848 			       IF2DT(new_mode));
1849 	if (error)
1850 		goto out_end_trans;
1851 
1852 	if (odip != ndip) {
1853 		if (S_ISDIR(new_mode) && !S_ISDIR(old_mode)) {
1854 			inc_nlink(&odip->i_inode);
1855 			drop_nlink(&ndip->i_inode);
1856 		} else if (S_ISDIR(old_mode) && !S_ISDIR(new_mode)) {
1857 			inc_nlink(&ndip->i_inode);
1858 			drop_nlink(&odip->i_inode);
1859 		}
1860 	}
1861 	mark_inode_dirty(&ndip->i_inode);
1862 	if (odip != ndip)
1863 		mark_inode_dirty(&odip->i_inode);
1864 
1865 out_end_trans:
1866 	gfs2_trans_end(sdp);
1867 out_gunlock:
1868 	while (x--) {
1869 		if (gfs2_holder_queued(ghs + x))
1870 			gfs2_glock_dq(ghs + x);
1871 		gfs2_holder_uninit(ghs + x);
1872 	}
1873 out_gunlock_r:
1874 	if (gfs2_holder_initialized(&r_gh))
1875 		gfs2_glock_dq_uninit(&r_gh);
1876 out:
1877 	return error;
1878 }
1879 
1880 static int gfs2_rename2(struct mnt_idmap *idmap, struct inode *odir,
1881 			struct dentry *odentry, struct inode *ndir,
1882 			struct dentry *ndentry, unsigned int flags)
1883 {
1884 	flags &= ~RENAME_NOREPLACE;
1885 
1886 	if (flags & ~RENAME_EXCHANGE)
1887 		return -EINVAL;
1888 
1889 	if (flags & RENAME_EXCHANGE)
1890 		return gfs2_exchange(odir, odentry, ndir, ndentry, flags);
1891 
1892 	return gfs2_rename(odir, odentry, ndir, ndentry);
1893 }
1894 
1895 /**
1896  * gfs2_get_link - Follow a symbolic link
1897  * @dentry: The dentry of the link
1898  * @inode: The inode of the link
1899  * @done: destructor for return value
1900  *
1901  * This can handle symlinks of any size.
1902  *
1903  * Returns: 0 on success or error code
1904  */
1905 
1906 static const char *gfs2_get_link(struct dentry *dentry,
1907 				 struct inode *inode,
1908 				 struct delayed_call *done)
1909 {
1910 	struct gfs2_inode *ip = GFS2_I(inode);
1911 	struct gfs2_holder i_gh;
1912 	struct buffer_head *dibh;
1913 	unsigned int size;
1914 	char *buf;
1915 	int error;
1916 
1917 	if (!dentry)
1918 		return ERR_PTR(-ECHILD);
1919 
1920 	gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
1921 	error = gfs2_glock_nq(&i_gh);
1922 	if (error) {
1923 		gfs2_holder_uninit(&i_gh);
1924 		return ERR_PTR(error);
1925 	}
1926 
1927 	size = (unsigned int)i_size_read(&ip->i_inode);
1928 	if (size == 0) {
1929 		gfs2_consist_inode(ip);
1930 		buf = ERR_PTR(-EIO);
1931 		goto out;
1932 	}
1933 
1934 	error = gfs2_meta_inode_buffer(ip, &dibh);
1935 	if (error) {
1936 		buf = ERR_PTR(error);
1937 		goto out;
1938 	}
1939 
1940 	buf = kzalloc(size + 1, GFP_NOFS);
1941 	if (!buf)
1942 		buf = ERR_PTR(-ENOMEM);
1943 	else
1944 		memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), size);
1945 	brelse(dibh);
1946 out:
1947 	gfs2_glock_dq_uninit(&i_gh);
1948 	if (!IS_ERR(buf))
1949 		set_delayed_call(done, kfree_link, buf);
1950 	return buf;
1951 }
1952 
1953 /**
1954  * gfs2_permission
1955  * @idmap: idmap of the mount the inode was found from
1956  * @inode: The inode
1957  * @mask: The mask to be tested
1958  *
1959  * This may be called from the VFS directly, or from within GFS2 with the
1960  * inode locked, so we look to see if the glock is already locked and only
1961  * lock the glock if its not already been done.
1962  *
1963  * Returns: errno
1964  */
1965 
1966 int gfs2_permission(struct mnt_idmap *idmap, struct inode *inode,
1967 		    int mask)
1968 {
1969 	int may_not_block = mask & MAY_NOT_BLOCK;
1970 	struct gfs2_inode *ip;
1971 	struct gfs2_holder i_gh;
1972 	struct gfs2_glock *gl;
1973 	int error;
1974 
1975 	gfs2_holder_mark_uninitialized(&i_gh);
1976 	ip = GFS2_I(inode);
1977 	gl = rcu_dereference_check(ip->i_gl, !may_not_block);
1978 	if (unlikely(!gl)) {
1979 		/* inode is getting torn down, must be RCU mode */
1980 		WARN_ON_ONCE(!may_not_block);
1981 		return -ECHILD;
1982         }
1983 	if (gfs2_glock_is_locked_by_me(gl) == NULL) {
1984 		if (may_not_block)
1985 			return -ECHILD;
1986 		error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1987 		if (error)
1988 			return error;
1989 	}
1990 
1991 	if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1992 		error = -EPERM;
1993 	else
1994 		error = generic_permission(&nop_mnt_idmap, inode, mask);
1995 	if (gfs2_holder_initialized(&i_gh))
1996 		gfs2_glock_dq_uninit(&i_gh);
1997 
1998 	return error;
1999 }
2000 
2001 static int __gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
2002 {
2003 	setattr_copy(&nop_mnt_idmap, inode, attr);
2004 	mark_inode_dirty(inode);
2005 	return 0;
2006 }
2007 
2008 static int gfs2_setattr_simple(struct inode *inode, struct iattr *attr)
2009 {
2010 	int error;
2011 
2012 	if (current->journal_info)
2013 		return __gfs2_setattr_simple(inode, attr);
2014 
2015 	error = gfs2_trans_begin(GFS2_SB(inode), RES_DINODE, 0);
2016 	if (error)
2017 		return error;
2018 
2019 	error = __gfs2_setattr_simple(inode, attr);
2020 	gfs2_trans_end(GFS2_SB(inode));
2021 	return error;
2022 }
2023 
2024 static int setattr_chown(struct inode *inode, struct iattr *attr)
2025 {
2026 	struct gfs2_inode *ip = GFS2_I(inode);
2027 	struct gfs2_sbd *sdp = GFS2_SB(inode);
2028 	kuid_t ouid, nuid;
2029 	kgid_t ogid, ngid;
2030 	int error;
2031 	struct gfs2_alloc_parms ap = {};
2032 
2033 	ouid = inode->i_uid;
2034 	ogid = inode->i_gid;
2035 	nuid = attr->ia_uid;
2036 	ngid = attr->ia_gid;
2037 
2038 	if (!(attr->ia_valid & ATTR_UID) || uid_eq(ouid, nuid))
2039 		ouid = nuid = NO_UID_QUOTA_CHANGE;
2040 	if (!(attr->ia_valid & ATTR_GID) || gid_eq(ogid, ngid))
2041 		ogid = ngid = NO_GID_QUOTA_CHANGE;
2042 	error = gfs2_qa_get(ip);
2043 	if (error)
2044 		return error;
2045 
2046 	error = gfs2_rindex_update(sdp);
2047 	if (error)
2048 		goto out;
2049 
2050 	error = gfs2_quota_lock(ip, nuid, ngid);
2051 	if (error)
2052 		goto out;
2053 
2054 	ap.target = gfs2_get_inode_blocks(&ip->i_inode);
2055 
2056 	if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
2057 	    !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
2058 		error = gfs2_quota_check(ip, nuid, ngid, &ap);
2059 		if (error)
2060 			goto out_gunlock_q;
2061 	}
2062 
2063 	error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
2064 	if (error)
2065 		goto out_gunlock_q;
2066 
2067 	error = gfs2_setattr_simple(inode, attr);
2068 	if (error)
2069 		goto out_end_trans;
2070 
2071 	if (!uid_eq(ouid, NO_UID_QUOTA_CHANGE) ||
2072 	    !gid_eq(ogid, NO_GID_QUOTA_CHANGE)) {
2073 		gfs2_quota_change(ip, -(s64)ap.target, ouid, ogid);
2074 		gfs2_quota_change(ip, ap.target, nuid, ngid);
2075 	}
2076 
2077 out_end_trans:
2078 	gfs2_trans_end(sdp);
2079 out_gunlock_q:
2080 	gfs2_quota_unlock(ip);
2081 out:
2082 	gfs2_qa_put(ip);
2083 	return error;
2084 }
2085 
2086 /**
2087  * gfs2_setattr - Change attributes on an inode
2088  * @idmap: idmap of the mount the inode was found from
2089  * @dentry: The dentry which is changing
2090  * @attr: The structure describing the change
2091  *
2092  * The VFS layer wants to change one or more of an inodes attributes.  Write
2093  * that change out to disk.
2094  *
2095  * Returns: errno
2096  */
2097 
2098 static int gfs2_setattr(struct mnt_idmap *idmap,
2099 			struct dentry *dentry, struct iattr *attr)
2100 {
2101 	struct inode *inode = d_inode(dentry);
2102 	struct gfs2_inode *ip = GFS2_I(inode);
2103 	struct gfs2_holder i_gh;
2104 	int error;
2105 
2106 	error = gfs2_qa_get(ip);
2107 	if (error)
2108 		return error;
2109 
2110 	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
2111 	if (error)
2112 		goto out;
2113 
2114 	error = may_setattr(&nop_mnt_idmap, inode, attr->ia_valid);
2115 	if (error)
2116 		goto error;
2117 
2118 	error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
2119 	if (error)
2120 		goto error;
2121 
2122 	if (attr->ia_valid & ATTR_SIZE)
2123 		error = gfs2_setattr_size(inode, attr->ia_size);
2124 	else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
2125 		error = setattr_chown(inode, attr);
2126 	else {
2127 		error = gfs2_setattr_simple(inode, attr);
2128 		if (!error && attr->ia_valid & ATTR_MODE)
2129 			error = posix_acl_chmod(&nop_mnt_idmap, dentry,
2130 						inode->i_mode);
2131 	}
2132 
2133 error:
2134 	if (!error)
2135 		mark_inode_dirty(inode);
2136 	gfs2_glock_dq_uninit(&i_gh);
2137 out:
2138 	gfs2_qa_put(ip);
2139 	return error;
2140 }
2141 
2142 /**
2143  * gfs2_getattr - Read out an inode's attributes
2144  * @idmap: idmap of the mount the inode was found from
2145  * @path: Object to query
2146  * @stat: The inode's stats
2147  * @request_mask: Mask of STATX_xxx flags indicating the caller's interests
2148  * @flags: AT_STATX_xxx setting
2149  *
2150  * This may be called from the VFS directly, or from within GFS2 with the
2151  * inode locked, so we look to see if the glock is already locked and only
2152  * lock the glock if its not already been done. Note that its the NFS
2153  * readdirplus operation which causes this to be called (from filldir)
2154  * with the glock already held.
2155  *
2156  * Returns: errno
2157  */
2158 
2159 static int gfs2_getattr(struct mnt_idmap *idmap,
2160 			const struct path *path, struct kstat *stat,
2161 			u32 request_mask, unsigned int flags)
2162 {
2163 	struct inode *inode = d_inode(path->dentry);
2164 	struct gfs2_inode *ip = GFS2_I(inode);
2165 	struct gfs2_holder gh;
2166 	u32 gfsflags;
2167 	int error;
2168 
2169 	gfs2_holder_mark_uninitialized(&gh);
2170 	if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
2171 		error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
2172 		if (error)
2173 			return error;
2174 	}
2175 
2176 	gfsflags = ip->i_diskflags;
2177 	if (gfsflags & GFS2_DIF_APPENDONLY)
2178 		stat->attributes |= STATX_ATTR_APPEND;
2179 	if (gfsflags & GFS2_DIF_IMMUTABLE)
2180 		stat->attributes |= STATX_ATTR_IMMUTABLE;
2181 
2182 	stat->attributes_mask |= (STATX_ATTR_APPEND |
2183 				  STATX_ATTR_COMPRESSED |
2184 				  STATX_ATTR_ENCRYPTED |
2185 				  STATX_ATTR_IMMUTABLE |
2186 				  STATX_ATTR_NODUMP);
2187 
2188 	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
2189 
2190 	if (gfs2_holder_initialized(&gh))
2191 		gfs2_glock_dq_uninit(&gh);
2192 
2193 	return 0;
2194 }
2195 
2196 static bool fault_in_fiemap(struct fiemap_extent_info *fi)
2197 {
2198 	struct fiemap_extent __user *dest = fi->fi_extents_start;
2199 	size_t size = sizeof(*dest) * fi->fi_extents_max;
2200 
2201 	return fault_in_safe_writeable((char __user *)dest, size) == 0;
2202 }
2203 
2204 static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2205 		       u64 start, u64 len)
2206 {
2207 	struct gfs2_inode *ip = GFS2_I(inode);
2208 	struct gfs2_holder gh;
2209 	int ret;
2210 
2211 	inode_lock_shared(inode);
2212 
2213 retry:
2214 	ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2215 	if (ret)
2216 		goto out;
2217 
2218 	pagefault_disable();
2219 	ret = iomap_fiemap(inode, fieinfo, start, len, &gfs2_iomap_ops);
2220 	pagefault_enable();
2221 
2222 	gfs2_glock_dq_uninit(&gh);
2223 
2224 	if (ret == -EFAULT && fault_in_fiemap(fieinfo)) {
2225 		fieinfo->fi_extents_mapped = 0;
2226 		goto retry;
2227 	}
2228 
2229 out:
2230 	inode_unlock_shared(inode);
2231 	return ret;
2232 }
2233 
2234 loff_t gfs2_seek_data(struct file *file, loff_t offset)
2235 {
2236 	struct inode *inode = file->f_mapping->host;
2237 	struct gfs2_inode *ip = GFS2_I(inode);
2238 	struct gfs2_holder gh;
2239 	loff_t ret;
2240 
2241 	inode_lock_shared(inode);
2242 	ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2243 	if (!ret)
2244 		ret = iomap_seek_data(inode, offset, &gfs2_iomap_ops);
2245 	gfs2_glock_dq_uninit(&gh);
2246 	inode_unlock_shared(inode);
2247 
2248 	if (ret < 0)
2249 		return ret;
2250 	return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
2251 }
2252 
2253 loff_t gfs2_seek_hole(struct file *file, loff_t offset)
2254 {
2255 	struct inode *inode = file->f_mapping->host;
2256 	struct gfs2_inode *ip = GFS2_I(inode);
2257 	struct gfs2_holder gh;
2258 	loff_t ret;
2259 
2260 	inode_lock_shared(inode);
2261 	ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
2262 	if (!ret)
2263 		ret = iomap_seek_hole(inode, offset, &gfs2_iomap_ops);
2264 	gfs2_glock_dq_uninit(&gh);
2265 	inode_unlock_shared(inode);
2266 
2267 	if (ret < 0)
2268 		return ret;
2269 	return vfs_setpos(file, ret, inode->i_sb->s_maxbytes);
2270 }
2271 
2272 static int gfs2_update_time(struct inode *inode, enum fs_update_time type,
2273 		unsigned int flags)
2274 {
2275 	struct gfs2_inode *ip = GFS2_I(inode);
2276 	struct gfs2_glock *gl = ip->i_gl;
2277 	struct gfs2_holder *gh;
2278 	int error;
2279 
2280 	if (flags & IOCB_NOWAIT)
2281 		return -EAGAIN;
2282 
2283 	gh = gfs2_glock_is_locked_by_me(gl);
2284 	if (gh && gl->gl_state != LM_ST_EXCLUSIVE) {
2285 		gfs2_glock_dq(gh);
2286 		gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, gh);
2287 		error = gfs2_glock_nq(gh);
2288 		if (error)
2289 			return error;
2290 	}
2291 	return generic_update_time(inode, type, flags);
2292 }
2293 
2294 static const struct inode_operations gfs2_file_iops = {
2295 	.permission = gfs2_permission,
2296 	.setattr = gfs2_setattr,
2297 	.getattr = gfs2_getattr,
2298 	.listxattr = gfs2_listxattr,
2299 	.fiemap = gfs2_fiemap,
2300 	.get_inode_acl = gfs2_get_acl,
2301 	.set_acl = gfs2_set_acl,
2302 	.update_time = gfs2_update_time,
2303 	.fileattr_get = gfs2_fileattr_get,
2304 	.fileattr_set = gfs2_fileattr_set,
2305 };
2306 
2307 static const struct inode_operations gfs2_dir_iops = {
2308 	.create = gfs2_create,
2309 	.lookup = gfs2_lookup,
2310 	.link = gfs2_link,
2311 	.unlink = gfs2_unlink,
2312 	.symlink = gfs2_symlink,
2313 	.mkdir = gfs2_mkdir,
2314 	.rmdir = gfs2_unlink,
2315 	.mknod = gfs2_mknod,
2316 	.rename = gfs2_rename2,
2317 	.permission = gfs2_permission,
2318 	.setattr = gfs2_setattr,
2319 	.getattr = gfs2_getattr,
2320 	.listxattr = gfs2_listxattr,
2321 	.fiemap = gfs2_fiemap,
2322 	.get_inode_acl = gfs2_get_acl,
2323 	.set_acl = gfs2_set_acl,
2324 	.update_time = gfs2_update_time,
2325 	.atomic_open = gfs2_atomic_open,
2326 	.fileattr_get = gfs2_fileattr_get,
2327 	.fileattr_set = gfs2_fileattr_set,
2328 };
2329 
2330 static const struct inode_operations gfs2_symlink_iops = {
2331 	.get_link = gfs2_get_link,
2332 	.permission = gfs2_permission,
2333 	.setattr = gfs2_setattr,
2334 	.getattr = gfs2_getattr,
2335 	.listxattr = gfs2_listxattr,
2336 	.fiemap = gfs2_fiemap,
2337 };
2338 
2339