xref: /openbmc/linux/fs/gfs2/inode.c (revision 75d3b817)
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2006 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 
20 #include "gfs2.h"
21 #include "lm_interface.h"
22 #include "incore.h"
23 #include "acl.h"
24 #include "bmap.h"
25 #include "dir.h"
26 #include "eattr.h"
27 #include "glock.h"
28 #include "glops.h"
29 #include "inode.h"
30 #include "log.h"
31 #include "meta_io.h"
32 #include "ops_address.h"
33 #include "ops_file.h"
34 #include "ops_inode.h"
35 #include "quota.h"
36 #include "rgrp.h"
37 #include "trans.h"
38 #include "util.h"
39 
40 /**
41  * gfs2_inode_attr_in - Copy attributes from the dinode into the VFS inode
42  * @ip: The GFS2 inode (with embedded disk inode data)
43  * @inode:  The Linux VFS inode
44  *
45  */
46 
47 void gfs2_inode_attr_in(struct gfs2_inode *ip)
48 {
49 	struct inode *inode = &ip->i_inode;
50 
51 	inode->i_ino = ip->i_num.no_addr;
52 
53 	switch (ip->i_di.di_mode & S_IFMT) {
54 	case S_IFBLK:
55 	case S_IFCHR:
56 		inode->i_rdev = MKDEV(ip->i_di.di_major, ip->i_di.di_minor);
57 		break;
58 	default:
59 		inode->i_rdev = 0;
60 		break;
61 	};
62 
63 	inode->i_mode = ip->i_di.di_mode;
64 	inode->i_nlink = ip->i_di.di_nlink;
65 	inode->i_uid = ip->i_di.di_uid;
66 	inode->i_gid = ip->i_di.di_gid;
67 	i_size_write(inode, ip->i_di.di_size);
68 	inode->i_atime.tv_sec = ip->i_di.di_atime;
69 	inode->i_mtime.tv_sec = ip->i_di.di_mtime;
70 	inode->i_ctime.tv_sec = ip->i_di.di_ctime;
71 	inode->i_atime.tv_nsec = 0;
72 	inode->i_mtime.tv_nsec = 0;
73 	inode->i_ctime.tv_nsec = 0;
74 	inode->i_blksize = PAGE_SIZE;
75 	inode->i_blocks = ip->i_di.di_blocks <<
76 		(GFS2_SB(inode)->sd_sb.sb_bsize_shift - GFS2_BASIC_BLOCK_SHIFT);
77 
78 	if (ip->i_di.di_flags & GFS2_DIF_IMMUTABLE)
79 		inode->i_flags |= S_IMMUTABLE;
80 	else
81 		inode->i_flags &= ~S_IMMUTABLE;
82 
83 	if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY)
84 		inode->i_flags |= S_APPEND;
85 	else
86 		inode->i_flags &= ~S_APPEND;
87 }
88 
89 /**
90  * gfs2_inode_attr_out - Copy attributes from VFS inode into the dinode
91  * @ip: The GFS2 inode
92  *
93  * Only copy out the attributes that we want the VFS layer
94  * to be able to modify.
95  */
96 
97 void gfs2_inode_attr_out(struct gfs2_inode *ip)
98 {
99 	struct inode *inode = &ip->i_inode;
100 	struct gfs2_dinode *di = &ip->i_di;
101 	gfs2_assert_withdraw(GFS2_SB(inode),
102 		(di->di_mode & S_IFMT) == (inode->i_mode & S_IFMT));
103 	di->di_mode = inode->i_mode;
104 	di->di_uid = inode->i_uid;
105 	di->di_gid = inode->i_gid;
106 	di->di_atime = inode->i_atime.tv_sec;
107 	di->di_mtime = inode->i_mtime.tv_sec;
108 	di->di_ctime = inode->i_ctime.tv_sec;
109 }
110 
111 static int iget_test(struct inode *inode, void *opaque)
112 {
113 	struct gfs2_inode *ip = GFS2_I(inode);
114 	struct gfs2_inum *inum = opaque;
115 
116 	if (ip && ip->i_num.no_addr == inum->no_addr)
117 		return 1;
118 
119 	return 0;
120 }
121 
122 static int iget_set(struct inode *inode, void *opaque)
123 {
124 	struct gfs2_inode *ip = GFS2_I(inode);
125 	struct gfs2_inum *inum = opaque;
126 
127 	ip->i_num = *inum;
128 	return 0;
129 }
130 
131 struct inode *gfs2_ilookup(struct super_block *sb, struct gfs2_inum *inum)
132 {
133 	return ilookup5(sb, (unsigned long)inum->no_formal_ino,
134 			iget_test, inum);
135 }
136 
137 static struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum *inum)
138 {
139 	return iget5_locked(sb, (unsigned long)inum->no_formal_ino,
140 		     iget_test, iget_set, inum);
141 }
142 
143 /**
144  * gfs2_inode_lookup - Lookup an inode
145  * @sb: The super block
146  * @inum: The inode number
147  * @type: The type of the inode
148  *
149  * Returns: A VFS inode, or an error
150  */
151 
152 struct inode *gfs2_inode_lookup(struct super_block *sb, struct gfs2_inum *inum, unsigned int type)
153 {
154 	struct inode *inode = gfs2_iget(sb, inum);
155 	struct gfs2_inode *ip = GFS2_I(inode);
156 	struct gfs2_glock *io_gl;
157 	int error;
158 
159 	if (inode->i_state & I_NEW) {
160 		struct gfs2_sbd *sdp = GFS2_SB(inode);
161 		umode_t mode = DT2IF(type);
162 		inode->u.generic_ip = ip;
163 		inode->i_mode = mode;
164 
165 		if (S_ISREG(mode)) {
166 			inode->i_op = &gfs2_file_iops;
167 			inode->i_fop = &gfs2_file_fops;
168 			inode->i_mapping->a_ops = &gfs2_file_aops;
169 		} else if (S_ISDIR(mode)) {
170 			inode->i_op = &gfs2_dir_iops;
171 			inode->i_fop = &gfs2_dir_fops;
172 		} else if (S_ISLNK(mode)) {
173 			inode->i_op = &gfs2_symlink_iops;
174 		} else {
175 			inode->i_op = &gfs2_dev_iops;
176 		}
177 
178 		error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
179 		if (unlikely(error))
180 			goto fail;
181 		ip->i_gl->gl_object = ip;
182 
183 		error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
184 		if (unlikely(error))
185 			goto fail_put;
186 
187 		ip->i_vn = ip->i_gl->gl_vn - 1;
188 		error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
189 		if (unlikely(error))
190 			goto fail_iopen;
191 
192 		gfs2_glock_put(io_gl);
193 		unlock_new_inode(inode);
194 	}
195 
196 	return inode;
197 fail_iopen:
198 	gfs2_glock_put(io_gl);
199 fail_put:
200 	ip->i_gl->gl_object = NULL;
201 	gfs2_glock_put(ip->i_gl);
202 fail:
203 	iput(inode);
204 	return ERR_PTR(error);
205 }
206 
207 /**
208  * gfs2_inode_refresh - Refresh the incore copy of the dinode
209  * @ip: The GFS2 inode
210  *
211  * Returns: errno
212  */
213 
214 int gfs2_inode_refresh(struct gfs2_inode *ip)
215 {
216 	struct buffer_head *dibh;
217 	int error;
218 
219 	error = gfs2_meta_inode_buffer(ip, &dibh);
220 	if (error)
221 		return error;
222 
223 	if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
224 		brelse(dibh);
225 		return -EIO;
226 	}
227 
228 	gfs2_dinode_in(&ip->i_di, dibh->b_data);
229 
230 	brelse(dibh);
231 
232 	if (ip->i_num.no_addr != ip->i_di.di_num.no_addr) {
233 		if (gfs2_consist_inode(ip))
234 			gfs2_dinode_print(&ip->i_di);
235 		return -EIO;
236 	}
237 	if (ip->i_num.no_formal_ino != ip->i_di.di_num.no_formal_ino)
238 		return -ESTALE;
239 
240 	ip->i_vn = ip->i_gl->gl_vn;
241 
242 	return 0;
243 }
244 
245 int gfs2_dinode_dealloc(struct gfs2_inode *ip)
246 {
247 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
248 	struct gfs2_alloc *al;
249 	struct gfs2_rgrpd *rgd;
250 	int error;
251 
252 	if (ip->i_di.di_blocks != 1) {
253 		if (gfs2_consist_inode(ip))
254 			gfs2_dinode_print(&ip->i_di);
255 		return -EIO;
256 	}
257 
258 	al = gfs2_alloc_get(ip);
259 
260 	error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
261 	if (error)
262 		goto out;
263 
264 	error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
265 	if (error)
266 		goto out_qs;
267 
268 	rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
269 	if (!rgd) {
270 		gfs2_consist_inode(ip);
271 		error = -EIO;
272 		goto out_rindex_relse;
273 	}
274 
275 	error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
276 				   &al->al_rgd_gh);
277 	if (error)
278 		goto out_rindex_relse;
279 
280 	error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
281 	if (error)
282 		goto out_rg_gunlock;
283 
284 	gfs2_trans_add_gl(ip->i_gl);
285 
286 	gfs2_free_di(rgd, ip);
287 
288 	gfs2_trans_end(sdp);
289 	clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
290 
291 out_rg_gunlock:
292 	gfs2_glock_dq_uninit(&al->al_rgd_gh);
293 out_rindex_relse:
294 	gfs2_glock_dq_uninit(&al->al_ri_gh);
295 out_qs:
296 	gfs2_quota_unhold(ip);
297 out:
298 	gfs2_alloc_put(ip);
299 	return error;
300 }
301 
302 /**
303  * gfs2_change_nlink - Change nlink count on inode
304  * @ip: The GFS2 inode
305  * @diff: The change in the nlink count required
306  *
307  * Returns: errno
308  */
309 
310 int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
311 {
312 	struct gfs2_sbd *sdp = ip->i_inode.i_sb->s_fs_info;
313 	struct buffer_head *dibh;
314 	uint32_t nlink;
315 	int error;
316 
317 	BUG_ON(ip->i_di.di_nlink != ip->i_inode.i_nlink);
318 	nlink = ip->i_di.di_nlink + diff;
319 
320 	/* If we are reducing the nlink count, but the new value ends up being
321 	   bigger than the old one, we must have underflowed. */
322 	if (diff < 0 && nlink > ip->i_di.di_nlink) {
323 		if (gfs2_consist_inode(ip))
324 			gfs2_dinode_print(&ip->i_di);
325 		return -EIO;
326 	}
327 
328 	error = gfs2_meta_inode_buffer(ip, &dibh);
329 	if (error)
330 		return error;
331 
332 	ip->i_di.di_nlink = nlink;
333 	ip->i_di.di_ctime = get_seconds();
334 	ip->i_inode.i_nlink = nlink;
335 
336 	gfs2_trans_add_bh(ip->i_gl, dibh, 1);
337 	gfs2_dinode_out(&ip->i_di, dibh->b_data);
338 	brelse(dibh);
339 	mark_inode_dirty(&ip->i_inode);
340 
341 	if (ip->i_di.di_nlink == 0) {
342 		struct gfs2_rgrpd *rgd;
343 		struct gfs2_holder ri_gh, rg_gh;
344 
345 		error = gfs2_rindex_hold(sdp, &ri_gh);
346 		if (error)
347 			goto out;
348 		error = -EIO;
349 		rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
350 		if (!rgd)
351 			goto out_norgrp;
352 		error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
353 		if (error)
354 			goto out_norgrp;
355 
356 		gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
357 		gfs2_glock_dq_uninit(&rg_gh);
358 out_norgrp:
359 		gfs2_glock_dq_uninit(&ri_gh);
360 	}
361 out:
362 	return error;
363 }
364 
365 struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
366 {
367 	struct qstr qstr;
368 	gfs2_str2qstr(&qstr, name);
369 	return gfs2_lookupi(dip, &qstr, 1, NULL);
370 }
371 
372 
373 /**
374  * gfs2_lookupi - Look up a filename in a directory and return its inode
375  * @d_gh: An initialized holder for the directory glock
376  * @name: The name of the inode to look for
377  * @is_root: If 1, ignore the caller's permissions
378  * @i_gh: An uninitialized holder for the new inode glock
379  *
380  * There will always be a vnode (Linux VFS inode) for the d_gh inode unless
381  * @is_root is true.
382  *
383  * Returns: errno
384  */
385 
386 struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
387 			   int is_root, struct nameidata *nd)
388 
389 {
390 	struct super_block *sb = dir->i_sb;
391 	struct gfs2_inode *dip = GFS2_I(dir);
392 	struct gfs2_holder d_gh;
393 	struct gfs2_inum inum;
394 	unsigned int type;
395 	int error = 0;
396 	struct inode *inode = NULL;
397 
398 	if (!name->len || name->len > GFS2_FNAMESIZE)
399 		return ERR_PTR(-ENAMETOOLONG);
400 
401 	if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
402 	    (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
403 	     dir == sb->s_root->d_inode)) {
404 		igrab(dir);
405 		return dir;
406 	}
407 
408 	error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
409 	if (error)
410 		return ERR_PTR(error);
411 
412 	if (!is_root) {
413 		error = permission(dir, MAY_EXEC, NULL);
414 		if (error)
415 			goto out;
416 	}
417 
418 	error = gfs2_dir_search(dir, name, &inum, &type);
419 	if (error)
420 		goto out;
421 
422 	inode = gfs2_inode_lookup(sb, &inum, type);
423 
424 out:
425 	gfs2_glock_dq_uninit(&d_gh);
426 	if (error == -ENOENT)
427 		return NULL;
428 	return inode;
429 }
430 
431 static int pick_formal_ino_1(struct gfs2_sbd *sdp, uint64_t *formal_ino)
432 {
433 	struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
434 	struct buffer_head *bh;
435 	struct gfs2_inum_range ir;
436 	int error;
437 
438 	error = gfs2_trans_begin(sdp, RES_DINODE, 0);
439 	if (error)
440 		return error;
441 	mutex_lock(&sdp->sd_inum_mutex);
442 
443 	error = gfs2_meta_inode_buffer(ip, &bh);
444 	if (error) {
445 		mutex_unlock(&sdp->sd_inum_mutex);
446 		gfs2_trans_end(sdp);
447 		return error;
448 	}
449 
450 	gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
451 
452 	if (ir.ir_length) {
453 		*formal_ino = ir.ir_start++;
454 		ir.ir_length--;
455 		gfs2_trans_add_bh(ip->i_gl, bh, 1);
456 		gfs2_inum_range_out(&ir,
457 				    bh->b_data + sizeof(struct gfs2_dinode));
458 		brelse(bh);
459 		mutex_unlock(&sdp->sd_inum_mutex);
460 		gfs2_trans_end(sdp);
461 		return 0;
462 	}
463 
464 	brelse(bh);
465 
466 	mutex_unlock(&sdp->sd_inum_mutex);
467 	gfs2_trans_end(sdp);
468 
469 	return 1;
470 }
471 
472 static int pick_formal_ino_2(struct gfs2_sbd *sdp, uint64_t *formal_ino)
473 {
474 	struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
475 	struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
476 	struct gfs2_holder gh;
477 	struct buffer_head *bh;
478 	struct gfs2_inum_range ir;
479 	int error;
480 
481 	error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
482 	if (error)
483 		return error;
484 
485 	error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
486 	if (error)
487 		goto out;
488 	mutex_lock(&sdp->sd_inum_mutex);
489 
490 	error = gfs2_meta_inode_buffer(ip, &bh);
491 	if (error)
492 		goto out_end_trans;
493 
494 	gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
495 
496 	if (!ir.ir_length) {
497 		struct buffer_head *m_bh;
498 		uint64_t x, y;
499 
500 		error = gfs2_meta_inode_buffer(m_ip, &m_bh);
501 		if (error)
502 			goto out_brelse;
503 
504 		x = *(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode));
505 		x = y = be64_to_cpu(x);
506 		ir.ir_start = x;
507 		ir.ir_length = GFS2_INUM_QUANTUM;
508 		x += GFS2_INUM_QUANTUM;
509 		if (x < y)
510 			gfs2_consist_inode(m_ip);
511 		x = cpu_to_be64(x);
512 		gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
513 		*(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = x;
514 
515 		brelse(m_bh);
516 	}
517 
518 	*formal_ino = ir.ir_start++;
519 	ir.ir_length--;
520 
521 	gfs2_trans_add_bh(ip->i_gl, bh, 1);
522 	gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
523 
524 out_brelse:
525 	brelse(bh);
526 out_end_trans:
527 	mutex_unlock(&sdp->sd_inum_mutex);
528 	gfs2_trans_end(sdp);
529 out:
530 	gfs2_glock_dq_uninit(&gh);
531 	return error;
532 }
533 
534 static int pick_formal_ino(struct gfs2_sbd *sdp, uint64_t *inum)
535 {
536 	int error;
537 
538 	error = pick_formal_ino_1(sdp, inum);
539 	if (error <= 0)
540 		return error;
541 
542 	error = pick_formal_ino_2(sdp, inum);
543 
544 	return error;
545 }
546 
547 /**
548  * create_ok - OK to create a new on-disk inode here?
549  * @dip:  Directory in which dinode is to be created
550  * @name:  Name of new dinode
551  * @mode:
552  *
553  * Returns: errno
554  */
555 
556 static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
557 		     unsigned int mode)
558 {
559 	int error;
560 
561 	error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
562 	if (error)
563 		return error;
564 
565 	/*  Don't create entries in an unlinked directory  */
566 	if (!dip->i_di.di_nlink)
567 		return -EPERM;
568 
569 	error = gfs2_dir_search(&dip->i_inode, name, NULL, NULL);
570 	switch (error) {
571 	case -ENOENT:
572 		error = 0;
573 		break;
574 	case 0:
575 		return -EEXIST;
576 	default:
577 		return error;
578 	}
579 
580 	if (dip->i_di.di_entries == (uint32_t)-1)
581 		return -EFBIG;
582 	if (S_ISDIR(mode) && dip->i_di.di_nlink == (uint32_t)-1)
583 		return -EMLINK;
584 
585 	return 0;
586 }
587 
588 static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
589 			       unsigned int *uid, unsigned int *gid)
590 {
591 	if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
592 	    (dip->i_di.di_mode & S_ISUID) && dip->i_di.di_uid) {
593 		if (S_ISDIR(*mode))
594 			*mode |= S_ISUID;
595 		else if (dip->i_di.di_uid != current->fsuid)
596 			*mode &= ~07111;
597 		*uid = dip->i_di.di_uid;
598 	} else
599 		*uid = current->fsuid;
600 
601 	if (dip->i_di.di_mode & S_ISGID) {
602 		if (S_ISDIR(*mode))
603 			*mode |= S_ISGID;
604 		*gid = dip->i_di.di_gid;
605 	} else
606 		*gid = current->fsgid;
607 }
608 
609 static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_inum *inum,
610 			u64 *generation)
611 {
612 	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
613 	int error;
614 
615 	gfs2_alloc_get(dip);
616 
617 	dip->i_alloc.al_requested = RES_DINODE;
618 	error = gfs2_inplace_reserve(dip);
619 	if (error)
620 		goto out;
621 
622 	error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
623 	if (error)
624 		goto out_ipreserv;
625 
626 	inum->no_addr = gfs2_alloc_di(dip, generation);
627 
628 	gfs2_trans_end(sdp);
629 
630 out_ipreserv:
631 	gfs2_inplace_release(dip);
632 out:
633 	gfs2_alloc_put(dip);
634 	return error;
635 }
636 
637 /**
638  * init_dinode - Fill in a new dinode structure
639  * @dip: the directory this inode is being created in
640  * @gl: The glock covering the new inode
641  * @inum: the inode number
642  * @mode: the file permissions
643  * @uid:
644  * @gid:
645  *
646  */
647 
648 static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
649 			const struct gfs2_inum *inum, unsigned int mode,
650 			unsigned int uid, unsigned int gid,
651 			const u64 *generation)
652 {
653 	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
654 	struct gfs2_dinode *di;
655 	struct buffer_head *dibh;
656 
657 	dibh = gfs2_meta_new(gl, inum->no_addr);
658 	gfs2_trans_add_bh(gl, dibh, 1);
659 	gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
660 	gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
661 	di = (struct gfs2_dinode *)dibh->b_data;
662 
663 	di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
664 	di->di_num.no_addr = cpu_to_be64(inum->no_addr);
665 	di->di_mode = cpu_to_be32(mode);
666 	di->di_uid = cpu_to_be32(uid);
667 	di->di_gid = cpu_to_be32(gid);
668 	di->di_nlink = cpu_to_be32(0);
669 	di->di_size = cpu_to_be64(0);
670 	di->di_blocks = cpu_to_be64(1);
671 	di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds());
672 	di->di_major = di->di_minor = cpu_to_be32(0);
673 	di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
674 	di->di_generation = cpu_to_be64(*generation);
675 	di->di_flags = cpu_to_be32(0);
676 
677 	if (S_ISREG(mode)) {
678 		if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
679 		    gfs2_tune_get(sdp, gt_new_files_jdata))
680 			di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
681 		if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
682 		    gfs2_tune_get(sdp, gt_new_files_directio))
683 			di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
684 	} else if (S_ISDIR(mode)) {
685 		di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
686 					    GFS2_DIF_INHERIT_DIRECTIO);
687 		di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
688 					    GFS2_DIF_INHERIT_JDATA);
689 	}
690 
691 	di->__pad1 = 0;
692 	di->di_payload_format = cpu_to_be32(0);
693 	di->di_height = cpu_to_be32(0);
694 	di->__pad2 = 0;
695 	di->__pad3 = 0;
696 	di->di_depth = cpu_to_be16(0);
697 	di->di_entries = cpu_to_be32(0);
698 	memset(&di->__pad4, 0, sizeof(di->__pad4));
699 	di->di_eattr = cpu_to_be64(0);
700 	memset(&di->di_reserved, 0, sizeof(di->di_reserved));
701 
702 	brelse(dibh);
703 }
704 
705 static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
706 		       unsigned int mode, const struct gfs2_inum *inum,
707 		       const u64 *generation)
708 {
709 	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
710 	unsigned int uid, gid;
711 	int error;
712 
713 	munge_mode_uid_gid(dip, &mode, &uid, &gid);
714 	gfs2_alloc_get(dip);
715 
716 	error = gfs2_quota_lock(dip, uid, gid);
717 	if (error)
718 		goto out;
719 
720 	error = gfs2_quota_check(dip, uid, gid);
721 	if (error)
722 		goto out_quota;
723 
724 	error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
725 	if (error)
726 		goto out_quota;
727 
728 	init_dinode(dip, gl, inum, mode, uid, gid, generation);
729 	gfs2_quota_change(dip, +1, uid, gid);
730 	gfs2_trans_end(sdp);
731 
732 out_quota:
733 	gfs2_quota_unlock(dip);
734 out:
735 	gfs2_alloc_put(dip);
736 	return error;
737 }
738 
739 static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
740 		       struct gfs2_inode *ip)
741 {
742 	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
743 	struct gfs2_alloc *al;
744 	int alloc_required;
745 	struct buffer_head *dibh;
746 	int error;
747 
748 	al = gfs2_alloc_get(dip);
749 
750 	error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
751 	if (error)
752 		goto fail;
753 
754 	error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
755 	if (alloc_required < 0)
756 		goto fail;
757 	if (alloc_required) {
758 		error = gfs2_quota_check(dip, dip->i_di.di_uid,
759 					 dip->i_di.di_gid);
760 		if (error)
761 			goto fail_quota_locks;
762 
763 		al->al_requested = sdp->sd_max_dirres;
764 
765 		error = gfs2_inplace_reserve(dip);
766 		if (error)
767 			goto fail_quota_locks;
768 
769 		error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
770 					 al->al_rgd->rd_ri.ri_length +
771 					 2 * RES_DINODE +
772 					 RES_STATFS + RES_QUOTA, 0);
773 		if (error)
774 			goto fail_ipreserv;
775 	} else {
776 		error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
777 		if (error)
778 			goto fail_quota_locks;
779 	}
780 
781 	error = gfs2_dir_add(&dip->i_inode, name, &ip->i_num, IF2DT(ip->i_di.di_mode));
782 	if (error)
783 		goto fail_end_trans;
784 
785 	error = gfs2_meta_inode_buffer(ip, &dibh);
786 	if (error)
787 		goto fail_end_trans;
788 	ip->i_di.di_nlink = 1;
789 	gfs2_trans_add_bh(ip->i_gl, dibh, 1);
790 	gfs2_dinode_out(&ip->i_di, dibh->b_data);
791 	brelse(dibh);
792 	return 0;
793 
794 fail_end_trans:
795 	gfs2_trans_end(sdp);
796 
797 fail_ipreserv:
798 	if (dip->i_alloc.al_rgd)
799 		gfs2_inplace_release(dip);
800 
801 fail_quota_locks:
802 	gfs2_quota_unlock(dip);
803 
804 fail:
805 	gfs2_alloc_put(dip);
806 	return error;
807 }
808 
809 /**
810  * gfs2_createi - Create a new inode
811  * @ghs: An array of two holders
812  * @name: The name of the new file
813  * @mode: the permissions on the new inode
814  *
815  * @ghs[0] is an initialized holder for the directory
816  * @ghs[1] is the holder for the inode lock
817  *
818  * If the return value is not NULL, the glocks on both the directory and the new
819  * file are held.  A transaction has been started and an inplace reservation
820  * is held, as well.
821  *
822  * Returns: An inode
823  */
824 
825 struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
826 			   unsigned int mode)
827 {
828 	struct inode *inode;
829 	struct gfs2_inode *dip = ghs->gh_gl->gl_object;
830 	struct inode *dir = &dip->i_inode;
831 	struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
832 	struct gfs2_inum inum;
833 	int error;
834 	u64 generation;
835 
836 	if (!name->len || name->len > GFS2_FNAMESIZE)
837 		return ERR_PTR(-ENAMETOOLONG);
838 
839 	gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
840 	error = gfs2_glock_nq(ghs);
841 	if (error)
842 		goto fail;
843 
844 	error = create_ok(dip, name, mode);
845 	if (error)
846 		goto fail_gunlock;
847 
848 	error = pick_formal_ino(sdp, &inum.no_formal_ino);
849 	if (error)
850 		goto fail_gunlock;
851 
852 	error = alloc_dinode(dip, &inum, &generation);
853 	if (error)
854 		goto fail_gunlock;
855 
856 	if (inum.no_addr < dip->i_num.no_addr) {
857 		gfs2_glock_dq(ghs);
858 
859 		error = gfs2_glock_nq_num(sdp, inum.no_addr,
860 					  &gfs2_inode_glops, LM_ST_EXCLUSIVE,
861 					  GL_SKIP, ghs + 1);
862 		if (error) {
863 			return ERR_PTR(error);
864 		}
865 
866 		gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
867 		error = gfs2_glock_nq(ghs);
868 		if (error) {
869 			gfs2_glock_dq_uninit(ghs + 1);
870 			return ERR_PTR(error);
871 		}
872 
873 		error = create_ok(dip, name, mode);
874 		if (error)
875 			goto fail_gunlock2;
876 	} else {
877 		error = gfs2_glock_nq_num(sdp, inum.no_addr,
878 					  &gfs2_inode_glops, LM_ST_EXCLUSIVE,
879 					  GL_SKIP, ghs + 1);
880 		if (error)
881 			goto fail_gunlock;
882 	}
883 
884 	error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation);
885 	if (error)
886 		goto fail_gunlock2;
887 
888 	inode = gfs2_inode_lookup(dir->i_sb, &inum, IF2DT(mode));
889 	if (IS_ERR(inode))
890 		goto fail_gunlock2;
891 
892 	error = gfs2_inode_refresh(GFS2_I(inode));
893 	if (error)
894 		goto fail_iput;
895 
896 	error = gfs2_acl_create(dip, GFS2_I(inode));
897 	if (error)
898 		goto fail_iput;
899 
900 	error = link_dinode(dip, name, GFS2_I(inode));
901 	if (error)
902 		goto fail_iput;
903 
904 	if (!inode)
905 		return ERR_PTR(-ENOMEM);
906 	return inode;
907 
908 fail_iput:
909 	iput(inode);
910 fail_gunlock2:
911 	gfs2_glock_dq_uninit(ghs + 1);
912 fail_gunlock:
913 	gfs2_glock_dq(ghs);
914 fail:
915 	return ERR_PTR(error);
916 }
917 
918 /**
919  * gfs2_rmdiri - Remove a directory
920  * @dip: The parent directory of the directory to be removed
921  * @name: The name of the directory to be removed
922  * @ip: The GFS2 inode of the directory to be removed
923  *
924  * Assumes Glocks on dip and ip are held
925  *
926  * Returns: errno
927  */
928 
929 int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
930 		struct gfs2_inode *ip)
931 {
932 	struct qstr dotname;
933 	int error;
934 
935 	if (ip->i_di.di_entries != 2) {
936 		if (gfs2_consist_inode(ip))
937 			gfs2_dinode_print(&ip->i_di);
938 		return -EIO;
939 	}
940 
941 	error = gfs2_dir_del(dip, name);
942 	if (error)
943 		return error;
944 
945 	error = gfs2_change_nlink(dip, -1);
946 	if (error)
947 		return error;
948 
949 	gfs2_str2qstr(&dotname, ".");
950 	error = gfs2_dir_del(ip, &dotname);
951 	if (error)
952 		return error;
953 
954 	gfs2_str2qstr(&dotname, "..");
955 	error = gfs2_dir_del(ip, &dotname);
956 	if (error)
957 		return error;
958 
959 	error = gfs2_change_nlink(ip, -2);
960 	if (error)
961 		return error;
962 
963 	return error;
964 }
965 
966 /*
967  * gfs2_unlink_ok - check to see that a inode is still in a directory
968  * @dip: the directory
969  * @name: the name of the file
970  * @ip: the inode
971  *
972  * Assumes that the lock on (at least) @dip is held.
973  *
974  * Returns: 0 if the parent/child relationship is correct, errno if it isn't
975  */
976 
977 int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
978 		   struct gfs2_inode *ip)
979 {
980 	struct gfs2_inum inum;
981 	unsigned int type;
982 	int error;
983 
984 	if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
985 		return -EPERM;
986 
987 	if ((dip->i_di.di_mode & S_ISVTX) &&
988 	    dip->i_di.di_uid != current->fsuid &&
989 	    ip->i_di.di_uid != current->fsuid && !capable(CAP_FOWNER))
990 		return -EPERM;
991 
992 	if (IS_APPEND(&dip->i_inode))
993 		return -EPERM;
994 
995 	error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
996 	if (error)
997 		return error;
998 
999 	error = gfs2_dir_search(&dip->i_inode, name, &inum, &type);
1000 	if (error)
1001 		return error;
1002 
1003 	if (!gfs2_inum_equal(&inum, &ip->i_num))
1004 		return -ENOENT;
1005 
1006 	if (IF2DT(ip->i_di.di_mode) != type) {
1007 		gfs2_consist_inode(dip);
1008 		return -EIO;
1009 	}
1010 
1011 	return 0;
1012 }
1013 
1014 /*
1015  * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1016  * @this: move this
1017  * @to: to here
1018  *
1019  * Follow @to back to the root and make sure we don't encounter @this
1020  * Assumes we already hold the rename lock.
1021  *
1022  * Returns: errno
1023  */
1024 
1025 int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1026 {
1027 	struct inode *dir = &to->i_inode;
1028 	struct super_block *sb = dir->i_sb;
1029 	struct inode *tmp;
1030 	struct qstr dotdot;
1031 	int error = 0;
1032 
1033 	gfs2_str2qstr(&dotdot, "..");
1034 
1035 	igrab(dir);
1036 
1037 	for (;;) {
1038 		if (dir == &this->i_inode) {
1039 			error = -EINVAL;
1040 			break;
1041 		}
1042 		if (dir == sb->s_root->d_inode) {
1043 			error = 0;
1044 			break;
1045 		}
1046 
1047 		tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
1048 		if (IS_ERR(tmp)) {
1049 			error = PTR_ERR(tmp);
1050 			break;
1051 		}
1052 
1053 		iput(dir);
1054 		dir = tmp;
1055 	}
1056 
1057 	iput(dir);
1058 
1059 	return error;
1060 }
1061 
1062 /**
1063  * gfs2_readlinki - return the contents of a symlink
1064  * @ip: the symlink's inode
1065  * @buf: a pointer to the buffer to be filled
1066  * @len: a pointer to the length of @buf
1067  *
1068  * If @buf is too small, a piece of memory is kmalloc()ed and needs
1069  * to be freed by the caller.
1070  *
1071  * Returns: errno
1072  */
1073 
1074 int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
1075 {
1076 	struct gfs2_holder i_gh;
1077 	struct buffer_head *dibh;
1078 	unsigned int x;
1079 	int error;
1080 
1081 	gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
1082 	error = gfs2_glock_nq_atime(&i_gh);
1083 	if (error) {
1084 		gfs2_holder_uninit(&i_gh);
1085 		return error;
1086 	}
1087 
1088 	if (!ip->i_di.di_size) {
1089 		gfs2_consist_inode(ip);
1090 		error = -EIO;
1091 		goto out;
1092 	}
1093 
1094 	error = gfs2_meta_inode_buffer(ip, &dibh);
1095 	if (error)
1096 		goto out;
1097 
1098 	x = ip->i_di.di_size + 1;
1099 	if (x > *len) {
1100 		*buf = kmalloc(x, GFP_KERNEL);
1101 		if (!*buf) {
1102 			error = -ENOMEM;
1103 			goto out_brelse;
1104 		}
1105 	}
1106 
1107 	memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1108 	*len = x;
1109 
1110 out_brelse:
1111 	brelse(dibh);
1112 out:
1113 	gfs2_glock_dq_uninit(&i_gh);
1114 	return error;
1115 }
1116 
1117 /**
1118  * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
1119  *       conditionally update the inode's atime
1120  * @gh: the holder to acquire
1121  *
1122  * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
1123  * Update if the difference between the current time and the inode's current
1124  * atime is greater than an interval specified at mount.
1125  *
1126  * Returns: errno
1127  */
1128 
1129 int gfs2_glock_nq_atime(struct gfs2_holder *gh)
1130 {
1131 	struct gfs2_glock *gl = gh->gh_gl;
1132 	struct gfs2_sbd *sdp = gl->gl_sbd;
1133 	struct gfs2_inode *ip = gl->gl_object;
1134 	int64_t curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum);
1135 	unsigned int state;
1136 	int flags;
1137 	int error;
1138 
1139 	if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
1140 	    gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
1141 	    gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
1142 		return -EINVAL;
1143 
1144 	state = gh->gh_state;
1145 	flags = gh->gh_flags;
1146 
1147 	error = gfs2_glock_nq(gh);
1148 	if (error)
1149 		return error;
1150 
1151 	if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
1152 	    (sdp->sd_vfs->s_flags & MS_RDONLY))
1153 		return 0;
1154 
1155 	curtime = get_seconds();
1156 	if (curtime - ip->i_di.di_atime >= quantum) {
1157 		gfs2_glock_dq(gh);
1158 		gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
1159 				   gh);
1160 		error = gfs2_glock_nq(gh);
1161 		if (error)
1162 			return error;
1163 
1164 		/* Verify that atime hasn't been updated while we were
1165 		   trying to get exclusive lock. */
1166 
1167 		curtime = get_seconds();
1168 		if (curtime - ip->i_di.di_atime >= quantum) {
1169 			struct buffer_head *dibh;
1170 
1171 			error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1172 			if (error == -EROFS)
1173 				return 0;
1174 			if (error)
1175 				goto fail;
1176 
1177 			error = gfs2_meta_inode_buffer(ip, &dibh);
1178 			if (error)
1179 				goto fail_end_trans;
1180 
1181 			ip->i_di.di_atime = curtime;
1182 
1183 			gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1184 			gfs2_dinode_out(&ip->i_di, dibh->b_data);
1185 			brelse(dibh);
1186 
1187 			gfs2_trans_end(sdp);
1188 		}
1189 
1190 		/* If someone else has asked for the glock,
1191 		   unlock and let them have it. Then reacquire
1192 		   in the original state. */
1193 		if (gfs2_glock_is_blocking(gl)) {
1194 			gfs2_glock_dq(gh);
1195 			gfs2_holder_reinit(state, flags, gh);
1196 			return gfs2_glock_nq(gh);
1197 		}
1198 	}
1199 
1200 	return 0;
1201 
1202 fail_end_trans:
1203 	gfs2_trans_end(sdp);
1204 fail:
1205 	gfs2_glock_dq(gh);
1206 	return error;
1207 }
1208 
1209 /**
1210  * glock_compare_atime - Compare two struct gfs2_glock structures for sort
1211  * @arg_a: the first structure
1212  * @arg_b: the second structure
1213  *
1214  * Returns: 1 if A > B
1215  *         -1 if A < B
1216  *          0 if A == B
1217  */
1218 
1219 static int glock_compare_atime(const void *arg_a, const void *arg_b)
1220 {
1221 	const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a;
1222 	const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b;
1223 	const struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1224 	const struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1225 
1226 	if (a->ln_number > b->ln_number)
1227 		return 1;
1228 	if (a->ln_number < b->ln_number)
1229 		return -1;
1230 	if (gh_a->gh_state == LM_ST_SHARED && gh_b->gh_state == LM_ST_EXCLUSIVE)
1231 		return 1;
1232 	if (gh_a->gh_state == LM_ST_SHARED && (gh_b->gh_flags & GL_ATIME))
1233 		return 1;
1234 
1235 	return 0;
1236 }
1237 
1238 /**
1239  * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an
1240  *      atime update
1241  * @num_gh: the number of structures
1242  * @ghs: an array of struct gfs2_holder structures
1243  *
1244  * Returns: 0 on success (all glocks acquired),
1245  *          errno on failure (no glocks acquired)
1246  */
1247 
1248 int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs)
1249 {
1250 	struct gfs2_holder **p;
1251 	unsigned int x;
1252 	int error = 0;
1253 
1254 	if (!num_gh)
1255 		return 0;
1256 
1257 	if (num_gh == 1) {
1258 		ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1259 		if (ghs->gh_flags & GL_ATIME)
1260 			error = gfs2_glock_nq_atime(ghs);
1261 		else
1262 			error = gfs2_glock_nq(ghs);
1263 		return error;
1264 	}
1265 
1266 	p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1267 	if (!p)
1268 		return -ENOMEM;
1269 
1270 	for (x = 0; x < num_gh; x++)
1271 		p[x] = &ghs[x];
1272 
1273 	sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL);
1274 
1275 	for (x = 0; x < num_gh; x++) {
1276 		p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1277 
1278 		if (p[x]->gh_flags & GL_ATIME)
1279 			error = gfs2_glock_nq_atime(p[x]);
1280 		else
1281 			error = gfs2_glock_nq(p[x]);
1282 
1283 		if (error) {
1284 			while (x--)
1285 				gfs2_glock_dq(p[x]);
1286 			break;
1287 		}
1288 	}
1289 
1290 	kfree(p);
1291 	return error;
1292 }
1293 
1294 
1295 static int
1296 __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1297 {
1298 	struct buffer_head *dibh;
1299 	int error;
1300 
1301 	error = gfs2_meta_inode_buffer(ip, &dibh);
1302 	if (!error) {
1303 		error = inode_setattr(&ip->i_inode, attr);
1304 		gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
1305 		gfs2_inode_attr_out(ip);
1306 
1307 		gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1308 		gfs2_dinode_out(&ip->i_di, dibh->b_data);
1309 		brelse(dibh);
1310 	}
1311 	return error;
1312 }
1313 
1314 /**
1315  * gfs2_setattr_simple -
1316  * @ip:
1317  * @attr:
1318  *
1319  * Called with a reference on the vnode.
1320  *
1321  * Returns: errno
1322  */
1323 
1324 int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1325 {
1326 	int error;
1327 
1328 	if (current->journal_info)
1329 		return __gfs2_setattr_simple(ip, attr);
1330 
1331 	error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
1332 	if (error)
1333 		return error;
1334 
1335 	error = __gfs2_setattr_simple(ip, attr);
1336 	gfs2_trans_end(GFS2_SB(&ip->i_inode));
1337 	return error;
1338 }
1339 
1340