1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2008 Red Hat, Inc.  All rights reserved.
4  *
5  * This copyrighted material is made available to anyone wishing to use,
6  * modify, copy, or redistribute it subject to the terms and conditions
7  * of the GNU General Public License version 2.
8  */
9 
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/completion.h>
14 #include <linux/buffer_head.h>
15 #include <linux/posix_acl.h>
16 #include <linux/sort.h>
17 #include <linux/gfs2_ondisk.h>
18 #include <linux/crc32.h>
19 #include <linux/security.h>
20 #include <linux/time.h>
21 
22 #include "gfs2.h"
23 #include "incore.h"
24 #include "acl.h"
25 #include "bmap.h"
26 #include "dir.h"
27 #include "xattr.h"
28 #include "glock.h"
29 #include "glops.h"
30 #include "inode.h"
31 #include "log.h"
32 #include "meta_io.h"
33 #include "quota.h"
34 #include "rgrp.h"
35 #include "trans.h"
36 #include "util.h"
37 
38 struct gfs2_inum_range_host {
39 	u64 ir_start;
40 	u64 ir_length;
41 };
42 
43 struct gfs2_skip_data {
44 	u64 no_addr;
45 	int skipped;
46 	int non_block;
47 };
48 
iget_test(struct inode * inode,void * opaque)49 static int iget_test(struct inode *inode, void *opaque)
50 {
51 	struct gfs2_inode *ip = GFS2_I(inode);
52 	struct gfs2_skip_data *data = opaque;
53 
54 	if (ip->i_no_addr == data->no_addr) {
55 		if (data->non_block &&
56 		    inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) {
57 			data->skipped = 1;
58 			return 0;
59 		}
60 		return 1;
61 	}
62 	return 0;
63 }
64 
iget_set(struct inode * inode,void * opaque)65 static int iget_set(struct inode *inode, void *opaque)
66 {
67 	struct gfs2_inode *ip = GFS2_I(inode);
68 	struct gfs2_skip_data *data = opaque;
69 
70 	if (data->skipped)
71 		return -ENOENT;
72 	inode->i_ino = (unsigned long)(data->no_addr);
73 	ip->i_no_addr = data->no_addr;
74 	return 0;
75 }
76 
gfs2_ilookup(struct super_block * sb,u64 no_addr)77 struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
78 {
79 	unsigned long hash = (unsigned long)no_addr;
80 	struct gfs2_skip_data data;
81 
82 	data.no_addr = no_addr;
83 	data.skipped = 0;
84 	data.non_block = 0;
85 	return ilookup5(sb, hash, iget_test, &data);
86 }
87 
gfs2_iget(struct super_block * sb,u64 no_addr,int non_block)88 static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr,
89 			       int non_block)
90 {
91 	struct gfs2_skip_data data;
92 	unsigned long hash = (unsigned long)no_addr;
93 
94 	data.no_addr = no_addr;
95 	data.skipped = 0;
96 	data.non_block = non_block;
97 	return iget5_locked(sb, hash, iget_test, iget_set, &data);
98 }
99 
100 /**
101  * gfs2_set_iop - Sets inode operations
102  * @inode: The inode with correct i_mode filled in
103  *
104  * GFS2 lookup code fills in vfs inode contents based on info obtained
105  * from directory entry inside gfs2_inode_lookup().
106  */
107 
gfs2_set_iop(struct inode * inode)108 static void gfs2_set_iop(struct inode *inode)
109 {
110 	struct gfs2_sbd *sdp = GFS2_SB(inode);
111 	umode_t mode = inode->i_mode;
112 
113 	if (S_ISREG(mode)) {
114 		inode->i_op = &gfs2_file_iops;
115 		if (gfs2_localflocks(sdp))
116 			inode->i_fop = &gfs2_file_fops_nolock;
117 		else
118 			inode->i_fop = &gfs2_file_fops;
119 	} else if (S_ISDIR(mode)) {
120 		inode->i_op = &gfs2_dir_iops;
121 		if (gfs2_localflocks(sdp))
122 			inode->i_fop = &gfs2_dir_fops_nolock;
123 		else
124 			inode->i_fop = &gfs2_dir_fops;
125 	} else if (S_ISLNK(mode)) {
126 		inode->i_op = &gfs2_symlink_iops;
127 	} else {
128 		inode->i_op = &gfs2_file_iops;
129 		init_special_inode(inode, inode->i_mode, inode->i_rdev);
130 	}
131 }
132 
133 /**
134  * gfs2_inode_lookup - Lookup an inode
135  * @sb: The super block
136  * @no_addr: The inode number
137  * @type: The type of the inode
138  * non_block: Can we block on inodes that are being freed?
139  *
140  * Returns: A VFS inode, or an error
141  */
142 
gfs2_inode_lookup(struct super_block * sb,unsigned int type,u64 no_addr,u64 no_formal_ino,int non_block)143 struct inode *gfs2_inode_lookup(struct super_block *sb, unsigned int type,
144 				u64 no_addr, u64 no_formal_ino, int non_block)
145 {
146 	struct inode *inode;
147 	struct gfs2_inode *ip;
148 	struct gfs2_glock *io_gl = NULL;
149 	int error;
150 
151 	inode = gfs2_iget(sb, no_addr, non_block);
152 	ip = GFS2_I(inode);
153 
154 	if (!inode)
155 		return ERR_PTR(-ENOBUFS);
156 
157 	if (inode->i_state & I_NEW) {
158 		struct gfs2_sbd *sdp = GFS2_SB(inode);
159 		ip->i_no_formal_ino = no_formal_ino;
160 
161 		error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
162 		if (unlikely(error))
163 			goto fail;
164 		ip->i_gl->gl_object = ip;
165 
166 		error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
167 		if (unlikely(error))
168 			goto fail_put;
169 
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_iopen;
174 
175 		ip->i_iopen_gh.gh_gl->gl_object = ip;
176 		gfs2_glock_put(io_gl);
177 		io_gl = NULL;
178 
179 		if (type == DT_UNKNOWN) {
180 			/* Inode glock must be locked already */
181 			error = gfs2_inode_refresh(GFS2_I(inode));
182 			if (error)
183 				goto fail_refresh;
184 		} else {
185 			inode->i_mode = DT2IF(type);
186 		}
187 
188 		gfs2_set_iop(inode);
189 		unlock_new_inode(inode);
190 	}
191 
192 	return inode;
193 
194 fail_refresh:
195 	ip->i_iopen_gh.gh_gl->gl_object = NULL;
196 	gfs2_glock_dq_uninit(&ip->i_iopen_gh);
197 fail_iopen:
198 	if (io_gl)
199 		gfs2_glock_put(io_gl);
200 fail_put:
201 	ip->i_gl->gl_object = NULL;
202 	gfs2_glock_put(ip->i_gl);
203 fail:
204 	iget_failed(inode);
205 	return ERR_PTR(error);
206 }
207 
gfs2_lookup_by_inum(struct gfs2_sbd * sdp,u64 no_addr,u64 * no_formal_ino,unsigned int blktype)208 struct inode *gfs2_lookup_by_inum(struct gfs2_sbd *sdp, u64 no_addr,
209 				  u64 *no_formal_ino, unsigned int blktype)
210 {
211 	struct super_block *sb = sdp->sd_vfs;
212 	struct gfs2_holder i_gh;
213 	struct inode *inode = NULL;
214 	int error;
215 
216 	/* Must not read in block until block type is verified */
217 	error = gfs2_glock_nq_num(sdp, no_addr, &gfs2_inode_glops,
218 				  LM_ST_EXCLUSIVE, GL_SKIP, &i_gh);
219 	if (error)
220 		return ERR_PTR(error);
221 
222 	error = gfs2_check_blk_type(sdp, no_addr, blktype);
223 	if (error)
224 		goto fail;
225 
226 	inode = gfs2_inode_lookup(sb, DT_UNKNOWN, no_addr, 0, 1);
227 	if (IS_ERR(inode))
228 		goto fail;
229 
230 	/* Two extra checks for NFS only */
231 	if (no_formal_ino) {
232 		error = -ESTALE;
233 		if (GFS2_I(inode)->i_no_formal_ino != *no_formal_ino)
234 			goto fail_iput;
235 
236 		error = -EIO;
237 		if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM)
238 			goto fail_iput;
239 
240 		error = 0;
241 	}
242 
243 fail:
244 	gfs2_glock_dq_uninit(&i_gh);
245 	return error ? ERR_PTR(error) : inode;
246 fail_iput:
247 	iput(inode);
248 	goto fail;
249 }
250 
gfs2_dinode_in(struct gfs2_inode * ip,const void * buf)251 static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
252 {
253 	const struct gfs2_dinode *str = buf;
254 	struct timespec atime;
255 	u16 height, depth;
256 
257 	if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
258 		goto corrupt;
259 	ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
260 	ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
261 	ip->i_inode.i_rdev = 0;
262 	switch (ip->i_inode.i_mode & S_IFMT) {
263 	case S_IFBLK:
264 	case S_IFCHR:
265 		ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
266 					   be32_to_cpu(str->di_minor));
267 		break;
268 	};
269 
270 	ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
271 	ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
272 	/*
273 	 * We will need to review setting the nlink count here in the
274 	 * light of the forthcoming ro bind mount work. This is a reminder
275 	 * to do that.
276 	 */
277 	ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
278 	i_size_write(&ip->i_inode, be64_to_cpu(str->di_size));
279 	gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
280 	atime.tv_sec = be64_to_cpu(str->di_atime);
281 	atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
282 	if (timespec_compare(&ip->i_inode.i_atime, &atime) < 0)
283 		ip->i_inode.i_atime = atime;
284 	ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
285 	ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
286 	ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
287 	ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
288 
289 	ip->i_goal = be64_to_cpu(str->di_goal_meta);
290 	ip->i_generation = be64_to_cpu(str->di_generation);
291 
292 	ip->i_diskflags = be32_to_cpu(str->di_flags);
293 	gfs2_set_inode_flags(&ip->i_inode);
294 	height = be16_to_cpu(str->di_height);
295 	if (unlikely(height > GFS2_MAX_META_HEIGHT))
296 		goto corrupt;
297 	ip->i_height = (u8)height;
298 
299 	depth = be16_to_cpu(str->di_depth);
300 	if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
301 		goto corrupt;
302 	ip->i_depth = (u8)depth;
303 	ip->i_entries = be32_to_cpu(str->di_entries);
304 
305 	ip->i_eattr = be64_to_cpu(str->di_eattr);
306 	if (S_ISREG(ip->i_inode.i_mode))
307 		gfs2_set_aops(&ip->i_inode);
308 
309 	return 0;
310 corrupt:
311 	if (gfs2_consist_inode(ip))
312 		gfs2_dinode_print(ip);
313 	return -EIO;
314 }
315 
316 /**
317  * gfs2_inode_refresh - Refresh the incore copy of the dinode
318  * @ip: The GFS2 inode
319  *
320  * Returns: errno
321  */
322 
gfs2_inode_refresh(struct gfs2_inode * ip)323 int gfs2_inode_refresh(struct gfs2_inode *ip)
324 {
325 	struct buffer_head *dibh;
326 	int error;
327 
328 	error = gfs2_meta_inode_buffer(ip, &dibh);
329 	if (error)
330 		return error;
331 
332 	if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
333 		brelse(dibh);
334 		return -EIO;
335 	}
336 
337 	error = gfs2_dinode_in(ip, dibh->b_data);
338 	brelse(dibh);
339 	clear_bit(GIF_INVALID, &ip->i_flags);
340 
341 	return error;
342 }
343 
gfs2_dinode_dealloc(struct gfs2_inode * ip)344 int gfs2_dinode_dealloc(struct gfs2_inode *ip)
345 {
346 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
347 	struct gfs2_alloc *al;
348 	struct gfs2_rgrpd *rgd;
349 	int error;
350 
351 	if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
352 		if (gfs2_consist_inode(ip))
353 			gfs2_dinode_print(ip);
354 		return -EIO;
355 	}
356 
357 	al = gfs2_alloc_get(ip);
358 	if (!al)
359 		return -ENOMEM;
360 
361 	error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
362 	if (error)
363 		goto out;
364 
365 	error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
366 	if (error)
367 		goto out_qs;
368 
369 	rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
370 	if (!rgd) {
371 		gfs2_consist_inode(ip);
372 		error = -EIO;
373 		goto out_rindex_relse;
374 	}
375 
376 	error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
377 				   &al->al_rgd_gh);
378 	if (error)
379 		goto out_rindex_relse;
380 
381 	error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
382 	if (error)
383 		goto out_rg_gunlock;
384 
385 	set_bit(GLF_DIRTY, &ip->i_gl->gl_flags);
386 	set_bit(GLF_LFLUSH, &ip->i_gl->gl_flags);
387 
388 	gfs2_free_di(rgd, ip);
389 
390 	gfs2_trans_end(sdp);
391 
392 out_rg_gunlock:
393 	gfs2_glock_dq_uninit(&al->al_rgd_gh);
394 out_rindex_relse:
395 	gfs2_glock_dq_uninit(&al->al_ri_gh);
396 out_qs:
397 	gfs2_quota_unhold(ip);
398 out:
399 	gfs2_alloc_put(ip);
400 	return error;
401 }
402 
403 /**
404  * gfs2_change_nlink - Change nlink count on inode
405  * @ip: The GFS2 inode
406  * @diff: The change in the nlink count required
407  *
408  * Returns: errno
409  */
gfs2_change_nlink(struct gfs2_inode * ip,int diff)410 int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
411 {
412 	struct buffer_head *dibh;
413 	u32 nlink;
414 	int error;
415 
416 	BUG_ON(diff != 1 && diff != -1);
417 	nlink = ip->i_inode.i_nlink + diff;
418 
419 	/* If we are reducing the nlink count, but the new value ends up being
420 	   bigger than the old one, we must have underflowed. */
421 	if (diff < 0 && nlink > ip->i_inode.i_nlink) {
422 		if (gfs2_consist_inode(ip))
423 			gfs2_dinode_print(ip);
424 		return -EIO;
425 	}
426 
427 	error = gfs2_meta_inode_buffer(ip, &dibh);
428 	if (error)
429 		return error;
430 
431 	if (diff > 0)
432 		inc_nlink(&ip->i_inode);
433 	else
434 		drop_nlink(&ip->i_inode);
435 
436 	ip->i_inode.i_ctime = CURRENT_TIME;
437 
438 	gfs2_trans_add_bh(ip->i_gl, dibh, 1);
439 	gfs2_dinode_out(ip, dibh->b_data);
440 	brelse(dibh);
441 	mark_inode_dirty(&ip->i_inode);
442 
443 	if (ip->i_inode.i_nlink == 0)
444 		gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
445 
446 	return error;
447 }
448 
gfs2_lookup_simple(struct inode * dip,const char * name)449 struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
450 {
451 	struct qstr qstr;
452 	struct inode *inode;
453 	gfs2_str2qstr(&qstr, name);
454 	inode = gfs2_lookupi(dip, &qstr, 1);
455 	/* gfs2_lookupi has inconsistent callers: vfs
456 	 * related routines expect NULL for no entry found,
457 	 * gfs2_lookup_simple callers expect ENOENT
458 	 * and do not check for NULL.
459 	 */
460 	if (inode == NULL)
461 		return ERR_PTR(-ENOENT);
462 	else
463 		return inode;
464 }
465 
466 
467 /**
468  * gfs2_lookupi - Look up a filename in a directory and return its inode
469  * @d_gh: An initialized holder for the directory glock
470  * @name: The name of the inode to look for
471  * @is_root: If 1, ignore the caller's permissions
472  * @i_gh: An uninitialized holder for the new inode glock
473  *
474  * This can be called via the VFS filldir function when NFS is doing
475  * a readdirplus and the inode which its intending to stat isn't
476  * already in cache. In this case we must not take the directory glock
477  * again, since the readdir call will have already taken that lock.
478  *
479  * Returns: errno
480  */
481 
gfs2_lookupi(struct inode * dir,const struct qstr * name,int is_root)482 struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
483 			   int is_root)
484 {
485 	struct super_block *sb = dir->i_sb;
486 	struct gfs2_inode *dip = GFS2_I(dir);
487 	struct gfs2_holder d_gh;
488 	int error = 0;
489 	struct inode *inode = NULL;
490 	int unlock = 0;
491 
492 	if (!name->len || name->len > GFS2_FNAMESIZE)
493 		return ERR_PTR(-ENAMETOOLONG);
494 
495 	if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
496 	    (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
497 	     dir == sb->s_root->d_inode)) {
498 		igrab(dir);
499 		return dir;
500 	}
501 
502 	if (gfs2_glock_is_locked_by_me(dip->i_gl) == NULL) {
503 		error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
504 		if (error)
505 			return ERR_PTR(error);
506 		unlock = 1;
507 	}
508 
509 	if (!is_root) {
510 		error = gfs2_permission(dir, MAY_EXEC, 0);
511 		if (error)
512 			goto out;
513 	}
514 
515 	inode = gfs2_dir_search(dir, name);
516 	if (IS_ERR(inode))
517 		error = PTR_ERR(inode);
518 out:
519 	if (unlock)
520 		gfs2_glock_dq_uninit(&d_gh);
521 	if (error == -ENOENT)
522 		return NULL;
523 	return inode ? inode : ERR_PTR(error);
524 }
525 
526 /**
527  * create_ok - OK to create a new on-disk inode here?
528  * @dip:  Directory in which dinode is to be created
529  * @name:  Name of new dinode
530  * @mode:
531  *
532  * Returns: errno
533  */
534 
create_ok(struct gfs2_inode * dip,const struct qstr * name,unsigned int mode)535 static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
536 		     unsigned int mode)
537 {
538 	int error;
539 
540 	error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
541 	if (error)
542 		return error;
543 
544 	/*  Don't create entries in an unlinked directory  */
545 	if (!dip->i_inode.i_nlink)
546 		return -EPERM;
547 
548 	error = gfs2_dir_check(&dip->i_inode, name, NULL);
549 	switch (error) {
550 	case -ENOENT:
551 		error = 0;
552 		break;
553 	case 0:
554 		return -EEXIST;
555 	default:
556 		return error;
557 	}
558 
559 	if (dip->i_entries == (u32)-1)
560 		return -EFBIG;
561 	if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
562 		return -EMLINK;
563 
564 	return 0;
565 }
566 
munge_mode_uid_gid(struct gfs2_inode * dip,unsigned int * mode,unsigned int * uid,unsigned int * gid)567 static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
568 			       unsigned int *uid, unsigned int *gid)
569 {
570 	if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
571 	    (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
572 		if (S_ISDIR(*mode))
573 			*mode |= S_ISUID;
574 		else if (dip->i_inode.i_uid != current_fsuid())
575 			*mode &= ~07111;
576 		*uid = dip->i_inode.i_uid;
577 	} else
578 		*uid = current_fsuid();
579 
580 	if (dip->i_inode.i_mode & S_ISGID) {
581 		if (S_ISDIR(*mode))
582 			*mode |= S_ISGID;
583 		*gid = dip->i_inode.i_gid;
584 	} else
585 		*gid = current_fsgid();
586 }
587 
alloc_dinode(struct gfs2_inode * dip,u64 * no_addr,u64 * generation)588 static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
589 {
590 	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
591 	int error;
592 
593 	if (gfs2_alloc_get(dip) == NULL)
594 		return -ENOMEM;
595 
596 	dip->i_alloc->al_requested = RES_DINODE;
597 	error = gfs2_inplace_reserve(dip);
598 	if (error)
599 		goto out;
600 
601 	error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
602 	if (error)
603 		goto out_ipreserv;
604 
605 	error = gfs2_alloc_di(dip, no_addr, generation);
606 
607 	gfs2_trans_end(sdp);
608 
609 out_ipreserv:
610 	gfs2_inplace_release(dip);
611 out:
612 	gfs2_alloc_put(dip);
613 	return error;
614 }
615 
616 /**
617  * init_dinode - Fill in a new dinode structure
618  * @dip: the directory this inode is being created in
619  * @gl: The glock covering the new inode
620  * @inum: the inode number
621  * @mode: the file permissions
622  * @uid:
623  * @gid:
624  *
625  */
626 
init_dinode(struct gfs2_inode * dip,struct gfs2_glock * gl,const struct gfs2_inum_host * inum,unsigned int mode,unsigned int uid,unsigned int gid,const u64 * generation,dev_t dev,struct buffer_head ** bhp)627 static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
628 			const struct gfs2_inum_host *inum, unsigned int mode,
629 			unsigned int uid, unsigned int gid,
630 			const u64 *generation, dev_t dev, struct buffer_head **bhp)
631 {
632 	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
633 	struct gfs2_dinode *di;
634 	struct buffer_head *dibh;
635 	struct timespec tv = CURRENT_TIME;
636 
637 	dibh = gfs2_meta_new(gl, inum->no_addr);
638 	gfs2_trans_add_bh(gl, dibh, 1);
639 	gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
640 	gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
641 	di = (struct gfs2_dinode *)dibh->b_data;
642 
643 	di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
644 	di->di_num.no_addr = cpu_to_be64(inum->no_addr);
645 	di->di_mode = cpu_to_be32(mode);
646 	di->di_uid = cpu_to_be32(uid);
647 	di->di_gid = cpu_to_be32(gid);
648 	di->di_nlink = 0;
649 	di->di_size = 0;
650 	di->di_blocks = cpu_to_be64(1);
651 	di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
652 	di->di_major = cpu_to_be32(MAJOR(dev));
653 	di->di_minor = cpu_to_be32(MINOR(dev));
654 	di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
655 	di->di_generation = cpu_to_be64(*generation);
656 	di->di_flags = 0;
657 
658 	if (S_ISREG(mode)) {
659 		if ((dip->i_diskflags & GFS2_DIF_INHERIT_JDATA) ||
660 		    gfs2_tune_get(sdp, gt_new_files_jdata))
661 			di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
662 	} else if (S_ISDIR(mode)) {
663 		di->di_flags |= cpu_to_be32(dip->i_diskflags &
664 					    GFS2_DIF_INHERIT_JDATA);
665 	}
666 
667 	di->__pad1 = 0;
668 	di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
669 	di->di_height = 0;
670 	di->__pad2 = 0;
671 	di->__pad3 = 0;
672 	di->di_depth = 0;
673 	di->di_entries = 0;
674 	memset(&di->__pad4, 0, sizeof(di->__pad4));
675 	di->di_eattr = 0;
676 	di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
677 	di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
678 	di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
679 	memset(&di->di_reserved, 0, sizeof(di->di_reserved));
680 
681 	set_buffer_uptodate(dibh);
682 
683 	*bhp = dibh;
684 }
685 
make_dinode(struct gfs2_inode * dip,struct gfs2_glock * gl,unsigned int mode,const struct gfs2_inum_host * inum,const u64 * generation,dev_t dev,struct buffer_head ** bhp)686 static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
687 		       unsigned int mode, const struct gfs2_inum_host *inum,
688 		       const u64 *generation, dev_t dev, struct buffer_head **bhp)
689 {
690 	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
691 	unsigned int uid, gid;
692 	int error;
693 
694 	munge_mode_uid_gid(dip, &mode, &uid, &gid);
695 	if (!gfs2_alloc_get(dip))
696 		return -ENOMEM;
697 
698 	error = gfs2_quota_lock(dip, uid, gid);
699 	if (error)
700 		goto out;
701 
702 	error = gfs2_quota_check(dip, uid, gid);
703 	if (error)
704 		goto out_quota;
705 
706 	error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
707 	if (error)
708 		goto out_quota;
709 
710 	init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
711 	gfs2_quota_change(dip, +1, uid, gid);
712 	gfs2_trans_end(sdp);
713 
714 out_quota:
715 	gfs2_quota_unlock(dip);
716 out:
717 	gfs2_alloc_put(dip);
718 	return error;
719 }
720 
link_dinode(struct gfs2_inode * dip,const struct qstr * name,struct gfs2_inode * ip)721 static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
722 		       struct gfs2_inode *ip)
723 {
724 	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
725 	struct gfs2_alloc *al;
726 	int alloc_required;
727 	struct buffer_head *dibh;
728 	int error;
729 
730 	al = gfs2_alloc_get(dip);
731 	if (!al)
732 		return -ENOMEM;
733 
734 	error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
735 	if (error)
736 		goto fail;
737 
738 	error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
739 	if (alloc_required < 0)
740 		goto fail_quota_locks;
741 	if (alloc_required) {
742 		error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
743 		if (error)
744 			goto fail_quota_locks;
745 
746 		al->al_requested = sdp->sd_max_dirres;
747 
748 		error = gfs2_inplace_reserve(dip);
749 		if (error)
750 			goto fail_quota_locks;
751 
752 		error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
753 					 al->al_rgd->rd_length +
754 					 2 * RES_DINODE +
755 					 RES_STATFS + RES_QUOTA, 0);
756 		if (error)
757 			goto fail_ipreserv;
758 	} else {
759 		error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
760 		if (error)
761 			goto fail_quota_locks;
762 	}
763 
764 	error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
765 	if (error)
766 		goto fail_end_trans;
767 
768 	error = gfs2_meta_inode_buffer(ip, &dibh);
769 	if (error)
770 		goto fail_end_trans;
771 	ip->i_inode.i_nlink = 1;
772 	gfs2_trans_add_bh(ip->i_gl, dibh, 1);
773 	gfs2_dinode_out(ip, dibh->b_data);
774 	brelse(dibh);
775 	return 0;
776 
777 fail_end_trans:
778 	gfs2_trans_end(sdp);
779 
780 fail_ipreserv:
781 	if (dip->i_alloc->al_rgd)
782 		gfs2_inplace_release(dip);
783 
784 fail_quota_locks:
785 	gfs2_quota_unlock(dip);
786 
787 fail:
788 	gfs2_alloc_put(dip);
789 	return error;
790 }
791 
gfs2_security_init(struct gfs2_inode * dip,struct gfs2_inode * ip,const struct qstr * qstr)792 static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip,
793 			      const struct qstr *qstr)
794 {
795 	int err;
796 	size_t len;
797 	void *value;
798 	char *name;
799 
800 	err = security_inode_init_security(&ip->i_inode, &dip->i_inode, qstr,
801 					   &name, &value, &len);
802 
803 	if (err) {
804 		if (err == -EOPNOTSUPP)
805 			return 0;
806 		return err;
807 	}
808 
809 	err = __gfs2_xattr_set(&ip->i_inode, name, value, len, 0,
810 			       GFS2_EATYPE_SECURITY);
811 	kfree(value);
812 	kfree(name);
813 
814 	return err;
815 }
816 
817 /**
818  * gfs2_createi - Create a new inode
819  * @ghs: An array of two holders
820  * @name: The name of the new file
821  * @mode: the permissions on the new inode
822  *
823  * @ghs[0] is an initialized holder for the directory
824  * @ghs[1] is the holder for the inode lock
825  *
826  * If the return value is not NULL, the glocks on both the directory and the new
827  * file are held.  A transaction has been started and an inplace reservation
828  * is held, as well.
829  *
830  * Returns: An inode
831  */
832 
gfs2_createi(struct gfs2_holder * ghs,const struct qstr * name,unsigned int mode,dev_t dev)833 struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
834 			   unsigned int mode, dev_t dev)
835 {
836 	struct inode *inode = NULL;
837 	struct gfs2_inode *dip = ghs->gh_gl->gl_object;
838 	struct inode *dir = &dip->i_inode;
839 	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
840 	struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
841 	int error;
842 	u64 generation;
843 	struct buffer_head *bh = NULL;
844 
845 	if (!name->len || name->len > GFS2_FNAMESIZE)
846 		return ERR_PTR(-ENAMETOOLONG);
847 
848 	gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
849 	error = gfs2_glock_nq(ghs);
850 	if (error)
851 		goto fail;
852 
853 	error = create_ok(dip, name, mode);
854 	if (error)
855 		goto fail_gunlock;
856 
857 	error = alloc_dinode(dip, &inum.no_addr, &generation);
858 	if (error)
859 		goto fail_gunlock;
860 	inum.no_formal_ino = generation;
861 
862 	error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
863 				  LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
864 	if (error)
865 		goto fail_gunlock;
866 
867 	error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
868 	if (error)
869 		goto fail_gunlock2;
870 
871 	inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode), inum.no_addr,
872 				  inum.no_formal_ino, 0);
873 	if (IS_ERR(inode))
874 		goto fail_gunlock2;
875 
876 	error = gfs2_inode_refresh(GFS2_I(inode));
877 	if (error)
878 		goto fail_gunlock2;
879 
880 	error = gfs2_acl_create(dip, inode);
881 	if (error)
882 		goto fail_gunlock2;
883 
884 	error = gfs2_security_init(dip, GFS2_I(inode), name);
885 	if (error)
886 		goto fail_gunlock2;
887 
888 	error = link_dinode(dip, name, GFS2_I(inode));
889 	if (error)
890 		goto fail_gunlock2;
891 
892 	if (bh)
893 		brelse(bh);
894 	return inode;
895 
896 fail_gunlock2:
897 	gfs2_glock_dq_uninit(ghs + 1);
898 	if (inode && !IS_ERR(inode))
899 		iput(inode);
900 fail_gunlock:
901 	gfs2_glock_dq(ghs);
902 fail:
903 	if (bh)
904 		brelse(bh);
905 	return ERR_PTR(error);
906 }
907 
__gfs2_setattr_simple(struct gfs2_inode * ip,struct iattr * attr)908 static int __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
909 {
910 	struct inode *inode = &ip->i_inode;
911 	struct buffer_head *dibh;
912 	int error;
913 
914 	error = gfs2_meta_inode_buffer(ip, &dibh);
915 	if (error)
916 		return error;
917 
918 	setattr_copy(inode, attr);
919 	mark_inode_dirty(inode);
920 	gfs2_trans_add_bh(ip->i_gl, dibh, 1);
921 	gfs2_dinode_out(ip, dibh->b_data);
922 	brelse(dibh);
923 	return 0;
924 }
925 
926 /**
927  * gfs2_setattr_simple -
928  * @ip:
929  * @attr:
930  *
931  * Called with a reference on the vnode.
932  *
933  * Returns: errno
934  */
935 
gfs2_setattr_simple(struct gfs2_inode * ip,struct iattr * attr)936 int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
937 {
938 	int error;
939 
940 	if (current->journal_info)
941 		return __gfs2_setattr_simple(ip, attr);
942 
943 	error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
944 	if (error)
945 		return error;
946 
947 	error = __gfs2_setattr_simple(ip, attr);
948 	gfs2_trans_end(GFS2_SB(&ip->i_inode));
949 	return error;
950 }
951 
gfs2_dinode_out(const struct gfs2_inode * ip,void * buf)952 void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
953 {
954 	struct gfs2_dinode *str = buf;
955 
956 	str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
957 	str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
958 	str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
959 	str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
960 	str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
961 	str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
962 	str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
963 	str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
964 	str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
965 	str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
966 	str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
967 	str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
968 	str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
969 	str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
970 
971 	str->di_goal_meta = cpu_to_be64(ip->i_goal);
972 	str->di_goal_data = cpu_to_be64(ip->i_goal);
973 	str->di_generation = cpu_to_be64(ip->i_generation);
974 
975 	str->di_flags = cpu_to_be32(ip->i_diskflags);
976 	str->di_height = cpu_to_be16(ip->i_height);
977 	str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
978 					     !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
979 					     GFS2_FORMAT_DE : 0);
980 	str->di_depth = cpu_to_be16(ip->i_depth);
981 	str->di_entries = cpu_to_be32(ip->i_entries);
982 
983 	str->di_eattr = cpu_to_be64(ip->i_eattr);
984 	str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
985 	str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
986 	str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
987 }
988 
gfs2_dinode_print(const struct gfs2_inode * ip)989 void gfs2_dinode_print(const struct gfs2_inode *ip)
990 {
991 	printk(KERN_INFO "  no_formal_ino = %llu\n",
992 	       (unsigned long long)ip->i_no_formal_ino);
993 	printk(KERN_INFO "  no_addr = %llu\n",
994 	       (unsigned long long)ip->i_no_addr);
995 	printk(KERN_INFO "  i_size = %llu\n",
996 	       (unsigned long long)i_size_read(&ip->i_inode));
997 	printk(KERN_INFO "  blocks = %llu\n",
998 	       (unsigned long long)gfs2_get_inode_blocks(&ip->i_inode));
999 	printk(KERN_INFO "  i_goal = %llu\n",
1000 	       (unsigned long long)ip->i_goal);
1001 	printk(KERN_INFO "  i_diskflags = 0x%.8X\n", ip->i_diskflags);
1002 	printk(KERN_INFO "  i_height = %u\n", ip->i_height);
1003 	printk(KERN_INFO "  i_depth = %u\n", ip->i_depth);
1004 	printk(KERN_INFO "  i_entries = %u\n", ip->i_entries);
1005 	printk(KERN_INFO "  i_eattr = %llu\n",
1006 	       (unsigned long long)ip->i_eattr);
1007 }
1008 
1009