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