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