xref: /openbmc/linux/fs/gfs2/inode.c (revision 9801f646)
1 /*
2  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3  * Copyright (C) 2004-2005 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 v.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 <asm/semaphore.h>
20 
21 #include "gfs2.h"
22 #include "lm_interface.h"
23 #include "incore.h"
24 #include "acl.h"
25 #include "bmap.h"
26 #include "dir.h"
27 #include "eattr.h"
28 #include "glock.h"
29 #include "glops.h"
30 #include "inode.h"
31 #include "log.h"
32 #include "meta_io.h"
33 #include "ops_address.h"
34 #include "ops_file.h"
35 #include "ops_inode.h"
36 #include "quota.h"
37 #include "rgrp.h"
38 #include "trans.h"
39 #include "unlinked.h"
40 #include "util.h"
41 
42 /**
43  * inode_attr_in - Copy attributes from the dinode into the VFS inode
44  * @ip: The GFS2 inode (with embedded disk inode data)
45  * @inode:  The Linux VFS inode
46  *
47  */
48 
49 static void inode_attr_in(struct gfs2_inode *ip, struct inode *inode)
50 {
51 	inode->i_ino = ip->i_num.no_formal_ino;
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 		(ip->i_sbd->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_in - Copy attributes from the dinode into the VFS inode
91  * @ip: The GFS2 inode (with embedded disk inode data)
92  *
93  */
94 
95 void gfs2_inode_attr_in(struct gfs2_inode *ip)
96 {
97 	struct inode *inode;
98 
99 	inode = gfs2_ip2v_lookup(ip);
100 	if (inode) {
101 		inode_attr_in(ip, inode);
102 		iput(inode);
103 	}
104 }
105 
106 /**
107  * gfs2_inode_attr_out - Copy attributes from VFS inode into the dinode
108  * @ip: The GFS2 inode
109  *
110  * Only copy out the attributes that we want the VFS layer
111  * to be able to modify.
112  */
113 
114 void gfs2_inode_attr_out(struct gfs2_inode *ip)
115 {
116 	struct inode *inode = ip->i_vnode;
117 
118 	gfs2_assert_withdraw(ip->i_sbd,
119 		(ip->i_di.di_mode & S_IFMT) == (inode->i_mode & S_IFMT));
120 	ip->i_di.di_mode = inode->i_mode;
121 	ip->i_di.di_uid = inode->i_uid;
122 	ip->i_di.di_gid = inode->i_gid;
123 	ip->i_di.di_atime = inode->i_atime.tv_sec;
124 	ip->i_di.di_mtime = inode->i_mtime.tv_sec;
125 	ip->i_di.di_ctime = inode->i_ctime.tv_sec;
126 }
127 
128 /**
129  * gfs2_ip2v_lookup - Get the struct inode for a struct gfs2_inode
130  * @ip: the struct gfs2_inode to get the struct inode for
131  *
132  * Returns: A VFS inode, or NULL if none
133  */
134 
135 struct inode *gfs2_ip2v_lookup(struct gfs2_inode *ip)
136 {
137 	struct inode *inode = NULL;
138 
139 	gfs2_assert_warn(ip->i_sbd, test_bit(GIF_MIN_INIT, &ip->i_flags));
140 
141 	spin_lock(&ip->i_spin);
142 	if (ip->i_vnode)
143 		inode = igrab(ip->i_vnode);
144 	spin_unlock(&ip->i_spin);
145 
146 	return inode;
147 }
148 
149 /**
150  * gfs2_ip2v - Get/Create a struct inode for a struct gfs2_inode
151  * @ip: the struct gfs2_inode to get the struct inode for
152  *
153  * Returns: A VFS inode, or NULL if no mem
154  */
155 
156 struct inode *gfs2_ip2v(struct gfs2_inode *ip)
157 {
158 	struct inode *inode, *tmp;
159 
160 	inode = gfs2_ip2v_lookup(ip);
161 	if (inode)
162 		return inode;
163 
164 	tmp = new_inode(ip->i_sbd->sd_vfs);
165 	if (!tmp)
166 		return NULL;
167 
168 	inode_attr_in(ip, tmp);
169 
170 	if (S_ISREG(ip->i_di.di_mode)) {
171 		tmp->i_op = &gfs2_file_iops;
172 		tmp->i_fop = &gfs2_file_fops;
173 		tmp->i_mapping->a_ops = &gfs2_file_aops;
174 	} else if (S_ISDIR(ip->i_di.di_mode)) {
175 		tmp->i_op = &gfs2_dir_iops;
176 		tmp->i_fop = &gfs2_dir_fops;
177 	} else if (S_ISLNK(ip->i_di.di_mode)) {
178 		tmp->i_op = &gfs2_symlink_iops;
179 	} else {
180 		tmp->i_op = &gfs2_dev_iops;
181 		init_special_inode(tmp, tmp->i_mode, tmp->i_rdev);
182 	}
183 
184 	tmp->u.generic_ip = NULL;
185 
186 	for (;;) {
187 		spin_lock(&ip->i_spin);
188 		if (!ip->i_vnode)
189 			break;
190 		inode = igrab(ip->i_vnode);
191 		spin_unlock(&ip->i_spin);
192 
193 		if (inode) {
194 			iput(tmp);
195 			return inode;
196 		}
197 		yield();
198 	}
199 
200 	inode = tmp;
201 
202 	gfs2_inode_hold(ip);
203 	ip->i_vnode = inode;
204 	inode->u.generic_ip = ip;
205 
206 	spin_unlock(&ip->i_spin);
207 
208 	insert_inode_hash(inode);
209 
210 	return inode;
211 }
212 
213 static int iget_test(struct inode *inode, void *opaque)
214 {
215 	struct gfs2_inode *ip = inode->u.generic_ip;
216 	struct gfs2_inum *inum = (struct gfs2_inum *)opaque;
217 
218 	if (ip && ip->i_num.no_addr == inum->no_addr)
219 		return 1;
220 
221 	return 0;
222 }
223 
224 struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum *inum)
225 {
226 	return ilookup5(sb, (unsigned long)inum->no_formal_ino,
227 			iget_test, inum);
228 }
229 
230 void gfs2_inode_min_init(struct gfs2_inode *ip, unsigned int type)
231 {
232 	if (!test_and_set_bit(GIF_MIN_INIT, &ip->i_flags)) {
233 		ip->i_di.di_nlink = 1;
234 		ip->i_di.di_mode = DT2IF(type);
235 	}
236 }
237 
238 /**
239  * gfs2_inode_refresh - Refresh the incore copy of the dinode
240  * @ip: The GFS2 inode
241  *
242  * Returns: errno
243  */
244 
245 int gfs2_inode_refresh(struct gfs2_inode *ip)
246 {
247 	struct buffer_head *dibh;
248 	int error;
249 
250 	error = gfs2_meta_inode_buffer(ip, &dibh);
251 	if (error)
252 		return error;
253 
254 	if (gfs2_metatype_check(ip->i_sbd, dibh, GFS2_METATYPE_DI)) {
255 		brelse(dibh);
256 		return -EIO;
257 	}
258 
259 	gfs2_dinode_in(&ip->i_di, dibh->b_data);
260 	set_bit(GIF_MIN_INIT, &ip->i_flags);
261 
262 	brelse(dibh);
263 
264 	if (ip->i_num.no_addr != ip->i_di.di_num.no_addr) {
265 		if (gfs2_consist_inode(ip))
266 			gfs2_dinode_print(&ip->i_di);
267 		return -EIO;
268 	}
269 	if (ip->i_num.no_formal_ino != ip->i_di.di_num.no_formal_ino)
270 		return -ESTALE;
271 
272 	ip->i_vn = ip->i_gl->gl_vn;
273 
274 	return 0;
275 }
276 
277 /**
278  * inode_create - create a struct gfs2_inode
279  * @i_gl: The glock covering the inode
280  * @inum: The inode number
281  * @io_gl: the iopen glock to acquire/hold (using holder in new gfs2_inode)
282  * @io_state: the state the iopen glock should be acquired in
283  * @ipp: pointer to put the returned inode in
284  *
285  * Returns: errno
286  */
287 
288 static int inode_create(struct gfs2_glock *i_gl, const struct gfs2_inum *inum,
289 			struct gfs2_glock *io_gl, unsigned int io_state,
290 			struct gfs2_inode **ipp, int need_lock)
291 {
292 	struct gfs2_sbd *sdp = i_gl->gl_sbd;
293 	struct gfs2_inode *ip;
294 	int error = 0;
295 
296 	ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
297 	if (!ip)
298 		return -ENOMEM;
299 	memset(ip, 0, sizeof(struct gfs2_inode));
300 
301 	ip->i_num = *inum;
302 
303 	atomic_set(&ip->i_count, 1);
304 
305 	ip->i_vn = i_gl->gl_vn - 1;
306 
307 	ip->i_gl = i_gl;
308 	ip->i_sbd = sdp;
309 
310 	spin_lock_init(&ip->i_spin);
311 	init_rwsem(&ip->i_rw_mutex);
312 
313 	ip->i_greedy = gfs2_tune_get(sdp, gt_greedy_default);
314 
315 	if (need_lock) {
316 		error = gfs2_glock_nq_init(io_gl,
317 					   io_state, GL_LOCAL_EXCL | GL_EXACT,
318 					   &ip->i_iopen_gh);
319 		if (error)
320 			goto fail;
321 	}
322 
323 	spin_lock(&io_gl->gl_spin);
324 	gfs2_glock_hold(i_gl);
325 	io_gl->gl_object = i_gl;
326 	spin_unlock(&io_gl->gl_spin);
327 
328 	gfs2_glock_hold(i_gl);
329 	i_gl->gl_object = ip;
330 
331 	atomic_inc(&sdp->sd_inode_count);
332 
333 	*ipp = ip;
334 
335 	return 0;
336 
337  fail:
338 	gfs2_meta_cache_flush(ip);
339 	kmem_cache_free(gfs2_inode_cachep, ip);
340 	*ipp = NULL;
341 
342 	return error;
343 }
344 
345 /**
346  * gfs2_inode_get - Create or get a reference on an inode
347  * @i_gl: The glock covering the inode
348  * @inum: The inode number
349  * @create:
350  * @ipp: pointer to put the returned inode in
351  *
352  * Returns: errno
353  */
354 
355 int gfs2_inode_get(struct gfs2_glock *i_gl, const struct gfs2_inum *inum,
356 		   int create, struct gfs2_inode **ipp)
357 {
358 	struct gfs2_sbd *sdp = i_gl->gl_sbd;
359 	struct gfs2_glock *io_gl;
360 	int error = 0;
361 
362 	gfs2_glmutex_lock(i_gl);
363 
364 	*ipp = i_gl->gl_object;
365 	if (*ipp) {
366 		error = -ESTALE;
367 		if ((*ipp)->i_num.no_formal_ino != inum->no_formal_ino)
368 			goto out;
369 		atomic_inc(&(*ipp)->i_count);
370 		error = 0;
371 		goto out;
372 	}
373 
374 	if (!create)
375 		goto out;
376 
377 	error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops,
378 			       CREATE, &io_gl);
379 	if (!error) {
380 		error = inode_create(i_gl, inum, io_gl, LM_ST_SHARED, ipp, 1);
381 		gfs2_glock_put(io_gl);
382 	}
383 
384  out:
385 	gfs2_glmutex_unlock(i_gl);
386 
387 	return error;
388 }
389 
390 void gfs2_inode_hold(struct gfs2_inode *ip)
391 {
392 	gfs2_assert(ip->i_sbd, atomic_read(&ip->i_count) > 0);
393 	atomic_inc(&ip->i_count);
394 }
395 
396 void gfs2_inode_put(struct gfs2_inode *ip)
397 {
398 	gfs2_assert(ip->i_sbd, atomic_read(&ip->i_count) > 0);
399 	atomic_dec(&ip->i_count);
400 }
401 
402 void gfs2_inode_destroy(struct gfs2_inode *ip, int unlock)
403 {
404 	struct gfs2_sbd *sdp = ip->i_sbd;
405 	struct gfs2_glock *i_gl = ip->i_gl;
406 
407 	gfs2_assert_warn(sdp, !atomic_read(&ip->i_count));
408 	if (unlock) {
409 		struct gfs2_glock *io_gl = ip->i_iopen_gh.gh_gl;
410 		gfs2_assert(sdp, io_gl->gl_object == i_gl);
411 
412 		spin_lock(&io_gl->gl_spin);
413 		io_gl->gl_object = NULL;
414 		spin_unlock(&io_gl->gl_spin);
415 		gfs2_glock_put(i_gl);
416 
417 		gfs2_glock_dq_uninit(&ip->i_iopen_gh);
418 	}
419 
420 	gfs2_meta_cache_flush(ip);
421 	kmem_cache_free(gfs2_inode_cachep, ip);
422 
423 	i_gl->gl_object = NULL;
424 	gfs2_glock_put(i_gl);
425 
426 	atomic_dec(&sdp->sd_inode_count);
427 }
428 
429 static int dinode_dealloc(struct gfs2_inode *ip, struct gfs2_unlinked *ul)
430 {
431 	struct gfs2_sbd *sdp = ip->i_sbd;
432 	struct gfs2_alloc *al;
433 	struct gfs2_rgrpd *rgd;
434 	int error;
435 
436 	if (ip->i_di.di_blocks != 1) {
437 		if (gfs2_consist_inode(ip))
438 			gfs2_dinode_print(&ip->i_di);
439 		return -EIO;
440 	}
441 
442 	al = gfs2_alloc_get(ip);
443 
444 	error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
445 	if (error)
446 		goto out;
447 
448 	error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
449 	if (error)
450 		goto out_qs;
451 
452 	rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
453 	if (!rgd) {
454 		gfs2_consist_inode(ip);
455 		error = -EIO;
456 		goto out_rindex_relse;
457 	}
458 
459 	error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
460 				   &al->al_rgd_gh);
461 	if (error)
462 		goto out_rindex_relse;
463 
464 	error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED +
465 				 RES_STATFS + RES_QUOTA, 1);
466 	if (error)
467 		goto out_rg_gunlock;
468 
469 	gfs2_trans_add_gl(ip->i_gl);
470 
471 	gfs2_free_di(rgd, ip);
472 
473 	error = gfs2_unlinked_ondisk_rm(sdp, ul);
474 
475 	gfs2_trans_end(sdp);
476 	clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
477 
478  out_rg_gunlock:
479 	gfs2_glock_dq_uninit(&al->al_rgd_gh);
480 
481  out_rindex_relse:
482 	gfs2_glock_dq_uninit(&al->al_ri_gh);
483 
484  out_qs:
485 	gfs2_quota_unhold(ip);
486 
487  out:
488 	gfs2_alloc_put(ip);
489 
490 	return error;
491 }
492 
493 /**
494  * inode_dealloc - Deallocate all on-disk blocks for an inode (dinode)
495  * @sdp: the filesystem
496  * @inum: the inode number to deallocate
497  * @io_gh: a holder for the iopen glock for this inode
498  *
499  * N.B. When we enter this we already hold the iopen glock and getting
500  * the glock for the inode means that we are grabbing the locks in the
501  * "wrong" order so we must only so a try lock operation and fail if we
502  * don't get the lock. Thats ok, since if we fail it means someone else
503  * is using the inode still and thus we shouldn't be deallocating it
504  * anyway.
505  *
506  * Returns: errno
507  */
508 
509 static int inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul,
510 			 struct gfs2_holder *io_gh)
511 {
512 	struct gfs2_inode *ip;
513 	struct gfs2_holder i_gh;
514 	int error;
515 
516 	error = gfs2_glock_nq_num(sdp, ul->ul_ut.ut_inum.no_addr,
517 				  &gfs2_inode_glops, LM_ST_EXCLUSIVE,
518 				  LM_FLAG_TRY_1CB, &i_gh);
519 	switch(error) {
520 	case 0:
521 		break;
522 	case GLR_TRYFAILED:
523 		return 1; /* or back off and relock in different order? */
524 	default:
525 		return error;
526 	}
527 
528 	gfs2_assert_warn(sdp, !i_gh.gh_gl->gl_object);
529 	error = inode_create(i_gh.gh_gl, &ul->ul_ut.ut_inum, io_gh->gh_gl,
530 			     LM_ST_EXCLUSIVE, &ip, 0);
531 
532 	if (error)
533 		goto out;
534 
535 	error = gfs2_inode_refresh(ip);
536 	if (error)
537 		goto out_iput;
538 
539 	if (ip->i_di.di_nlink) {
540 		if (gfs2_consist_inode(ip))
541 			gfs2_dinode_print(&ip->i_di);
542 		error = -EIO;
543 		goto out_iput;
544 	}
545 
546 	if (S_ISDIR(ip->i_di.di_mode) &&
547 	    (ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
548 		error = gfs2_dir_exhash_dealloc(ip);
549 		if (error)
550 			goto out_iput;
551 	}
552 
553 	if (ip->i_di.di_eattr) {
554 		error = gfs2_ea_dealloc(ip);
555 		if (error)
556 			goto out_iput;
557 	}
558 
559 	if (!gfs2_is_stuffed(ip)) {
560 		error = gfs2_file_dealloc(ip);
561 		if (error)
562 			goto out_iput;
563 	}
564 
565 	error = dinode_dealloc(ip, ul);
566 	if (error)
567 		goto out_iput;
568 
569 out_iput:
570 	gfs2_glmutex_lock(i_gh.gh_gl);
571 	gfs2_inode_put(ip);
572 	gfs2_inode_destroy(ip, 0);
573 	gfs2_glmutex_unlock(i_gh.gh_gl);
574 
575 out:
576 	gfs2_glock_dq_uninit(&i_gh);
577 
578 	return error;
579 }
580 
581 /**
582  * try_inode_dealloc - Try to deallocate an inode and all its blocks
583  * @sdp: the filesystem
584  *
585  * Returns: 0 on success, -errno on error, 1 on busy (inode open)
586  */
587 
588 static int try_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
589 {
590 	int error = 0;
591 	struct gfs2_holder iogh;
592 
593 	gfs2_try_toss_inode(sdp, &ul->ul_ut.ut_inum);
594 	error = gfs2_glock_nq_num(sdp, ul->ul_ut.ut_inum.no_addr,
595 				  &gfs2_iopen_glops, LM_ST_EXCLUSIVE,
596 				  LM_FLAG_TRY_1CB, &iogh);
597 	switch (error) {
598 	case 0:
599 		break;
600 	case GLR_TRYFAILED:
601 		return 1;
602 	default:
603 		return error;
604 	}
605 
606 	error = inode_dealloc(sdp, ul, &iogh);
607 	gfs2_glock_dq_uninit(&iogh);
608 
609 	return error;
610 }
611 
612 static int inode_dealloc_uninit(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
613 {
614 	struct gfs2_rgrpd *rgd;
615 	struct gfs2_holder ri_gh, rgd_gh;
616 	int error;
617 
618 	error = gfs2_rindex_hold(sdp, &ri_gh);
619 	if (error)
620 		return error;
621 
622 	rgd = gfs2_blk2rgrpd(sdp, ul->ul_ut.ut_inum.no_addr);
623 	if (!rgd) {
624 		gfs2_consist(sdp);
625 		error = -EIO;
626 		goto out;
627 	}
628 
629 	error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rgd_gh);
630 	if (error)
631 		goto out;
632 
633 	error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED + RES_STATFS, 0);
634 	if (error)
635 		goto out_gunlock;
636 
637 	gfs2_free_uninit_di(rgd, ul->ul_ut.ut_inum.no_addr);
638 	gfs2_unlinked_ondisk_rm(sdp, ul);
639 
640 	gfs2_trans_end(sdp);
641 
642  out_gunlock:
643 	gfs2_glock_dq_uninit(&rgd_gh);
644  out:
645 	gfs2_glock_dq_uninit(&ri_gh);
646 
647 	return error;
648 }
649 
650 int gfs2_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
651 {
652 	if (ul->ul_ut.ut_flags & GFS2_UTF_UNINIT)
653 		return inode_dealloc_uninit(sdp, ul);
654 	else
655 		return try_inode_dealloc(sdp, ul);
656 }
657 
658 /**
659  * gfs2_change_nlink - Change nlink count on inode
660  * @ip: The GFS2 inode
661  * @diff: The change in the nlink count required
662  *
663  * Returns: errno
664  */
665 
666 int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
667 {
668 	struct buffer_head *dibh;
669 	uint32_t nlink;
670 	int error;
671 
672 	nlink = ip->i_di.di_nlink + diff;
673 
674 	/* If we are reducing the nlink count, but the new value ends up being
675 	   bigger than the old one, we must have underflowed. */
676 	if (diff < 0 && nlink > ip->i_di.di_nlink) {
677 		if (gfs2_consist_inode(ip))
678 			gfs2_dinode_print(&ip->i_di);
679 		return -EIO;
680 	}
681 
682 	error = gfs2_meta_inode_buffer(ip, &dibh);
683 	if (error)
684 		return error;
685 
686 	ip->i_di.di_nlink = nlink;
687 	ip->i_di.di_ctime = get_seconds();
688 
689 	gfs2_trans_add_bh(ip->i_gl, dibh, 1);
690 	gfs2_dinode_out(&ip->i_di, dibh->b_data);
691 	brelse(dibh);
692 
693 	return 0;
694 }
695 
696 struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
697 {
698 	struct qstr qstr;
699 	gfs2_str2qstr(&qstr, name);
700 	return gfs2_lookupi(dip, &qstr, 1, NULL);
701 }
702 
703 
704 /**
705  * gfs2_lookupi - Look up a filename in a directory and return its inode
706  * @d_gh: An initialized holder for the directory glock
707  * @name: The name of the inode to look for
708  * @is_root: If 1, ignore the caller's permissions
709  * @i_gh: An uninitialized holder for the new inode glock
710  *
711  * There will always be a vnode (Linux VFS inode) for the d_gh inode unless
712  * @is_root is true.
713  *
714  * Returns: errno
715  */
716 
717 struct inode *gfs2_lookupi(struct inode *dir, struct qstr *name, int is_root,
718 			   struct nameidata *nd)
719 
720 {
721 	struct super_block *sb = dir->i_sb;
722 	struct gfs2_inode *ipp;
723 	struct gfs2_inode *dip = dir->u.generic_ip;
724 	struct gfs2_sbd *sdp = dip->i_sbd;
725 	struct gfs2_holder d_gh;
726 	struct gfs2_inum inum;
727 	unsigned int type;
728 	struct gfs2_glock *gl;
729 	int error = 0;
730 	struct inode *inode = NULL;
731 
732 	if (!name->len || name->len > GFS2_FNAMESIZE)
733 		return ERR_PTR(-ENAMETOOLONG);
734 
735 	if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
736 	    (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
737 	     dir == sb->s_root->d_inode)) {
738 		gfs2_inode_hold(dip);
739 		ipp = dip;
740 		goto done;
741 	}
742 
743 	error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
744 	if (error)
745 		return ERR_PTR(error);
746 
747 	if (!is_root) {
748 		error = gfs2_repermission(dip->i_vnode, MAY_EXEC, NULL);
749 		if (error)
750 			goto out;
751 	}
752 
753 	error = gfs2_dir_search(dir, name, &inum, &type);
754 	if (error)
755 		goto out;
756 
757 	error = gfs2_glock_get(sdp, inum.no_addr, &gfs2_inode_glops,
758 			       CREATE, &gl);
759 	if (error)
760 		goto out;
761 
762 	error = gfs2_inode_get(gl, &inum, CREATE, &ipp);
763 	if (!error)
764 		gfs2_inode_min_init(ipp, type);
765 
766 	gfs2_glock_put(gl);
767 
768 out:
769 	gfs2_glock_dq_uninit(&d_gh);
770 done:
771 	if (error == -ENOENT)
772 		return NULL;
773 	if (error == 0) {
774 		inode = gfs2_ip2v(ipp);
775 		gfs2_inode_put(ipp);
776 		if (!inode)
777 			return ERR_PTR(-ENOMEM);
778 		return inode;
779 	}
780 	return ERR_PTR(error);
781 }
782 
783 static int pick_formal_ino_1(struct gfs2_sbd *sdp, uint64_t *formal_ino)
784 {
785 	struct gfs2_inode *ip = sdp->sd_ir_inode->u.generic_ip;
786 	struct buffer_head *bh;
787 	struct gfs2_inum_range ir;
788 	int error;
789 
790 	error = gfs2_trans_begin(sdp, RES_DINODE, 0);
791 	if (error)
792 		return error;
793 	mutex_lock(&sdp->sd_inum_mutex);
794 
795 	error = gfs2_meta_inode_buffer(ip, &bh);
796 	if (error) {
797 		mutex_unlock(&sdp->sd_inum_mutex);
798 		gfs2_trans_end(sdp);
799 		return error;
800 	}
801 
802 	gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
803 
804 	if (ir.ir_length) {
805 		*formal_ino = ir.ir_start++;
806 		ir.ir_length--;
807 		gfs2_trans_add_bh(ip->i_gl, bh, 1);
808 		gfs2_inum_range_out(&ir,
809 				    bh->b_data + sizeof(struct gfs2_dinode));
810 		brelse(bh);
811 		mutex_unlock(&sdp->sd_inum_mutex);
812 		gfs2_trans_end(sdp);
813 		return 0;
814 	}
815 
816 	brelse(bh);
817 
818 	mutex_unlock(&sdp->sd_inum_mutex);
819 	gfs2_trans_end(sdp);
820 
821 	return 1;
822 }
823 
824 static int pick_formal_ino_2(struct gfs2_sbd *sdp, uint64_t *formal_ino)
825 {
826 	struct gfs2_inode *ip = sdp->sd_ir_inode->u.generic_ip;
827 	struct gfs2_inode *m_ip = sdp->sd_inum_inode->u.generic_ip;
828 	struct gfs2_holder gh;
829 	struct buffer_head *bh;
830 	struct gfs2_inum_range ir;
831 	int error;
832 
833 	error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
834 	if (error)
835 		return error;
836 
837 	error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
838 	if (error)
839 		goto out;
840 	mutex_lock(&sdp->sd_inum_mutex);
841 
842 	error = gfs2_meta_inode_buffer(ip, &bh);
843 	if (error)
844 		goto out_end_trans;
845 
846 	gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
847 
848 	if (!ir.ir_length) {
849 		struct buffer_head *m_bh;
850 		uint64_t x, y;
851 
852 		error = gfs2_meta_inode_buffer(m_ip, &m_bh);
853 		if (error)
854 			goto out_brelse;
855 
856 		x = *(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode));
857 		x = y = be64_to_cpu(x);
858 		ir.ir_start = x;
859 		ir.ir_length = GFS2_INUM_QUANTUM;
860 		x += GFS2_INUM_QUANTUM;
861 		if (x < y)
862 			gfs2_consist_inode(m_ip);
863 		x = cpu_to_be64(x);
864 		gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
865 		*(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = x;
866 
867 		brelse(m_bh);
868 	}
869 
870 	*formal_ino = ir.ir_start++;
871 	ir.ir_length--;
872 
873 	gfs2_trans_add_bh(ip->i_gl, bh, 1);
874 	gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
875 
876  out_brelse:
877 	brelse(bh);
878 
879  out_end_trans:
880 	mutex_unlock(&sdp->sd_inum_mutex);
881 	gfs2_trans_end(sdp);
882 
883  out:
884 	gfs2_glock_dq_uninit(&gh);
885 
886 	return error;
887 }
888 
889 static int pick_formal_ino(struct gfs2_sbd *sdp, uint64_t *inum)
890 {
891 	int error;
892 
893 	error = pick_formal_ino_1(sdp, inum);
894 	if (error <= 0)
895 		return error;
896 
897 	error = pick_formal_ino_2(sdp, inum);
898 
899 	return error;
900 }
901 
902 /**
903  * create_ok - OK to create a new on-disk inode here?
904  * @dip:  Directory in which dinode is to be created
905  * @name:  Name of new dinode
906  * @mode:
907  *
908  * Returns: errno
909  */
910 
911 static int create_ok(struct gfs2_inode *dip, struct qstr *name,
912 		     unsigned int mode)
913 {
914 	int error;
915 
916 	error = gfs2_repermission(dip->i_vnode, MAY_WRITE | MAY_EXEC, NULL);
917 	if (error)
918 		return error;
919 
920 	/*  Don't create entries in an unlinked directory  */
921 	if (!dip->i_di.di_nlink)
922 		return -EPERM;
923 
924 	error = gfs2_dir_search(dip->i_vnode, name, NULL, NULL);
925 	switch (error) {
926 	case -ENOENT:
927 		error = 0;
928 		break;
929 	case 0:
930 		return -EEXIST;
931 	default:
932 		return error;
933 	}
934 
935 	if (dip->i_di.di_entries == (uint32_t)-1)
936 		return -EFBIG;
937 	if (S_ISDIR(mode) && dip->i_di.di_nlink == (uint32_t)-1)
938 		return -EMLINK;
939 
940 	return 0;
941 }
942 
943 static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
944 			       unsigned int *uid, unsigned int *gid)
945 {
946 	if (dip->i_sbd->sd_args.ar_suiddir &&
947 	    (dip->i_di.di_mode & S_ISUID) &&
948 	    dip->i_di.di_uid) {
949 		if (S_ISDIR(*mode))
950 			*mode |= S_ISUID;
951 		else if (dip->i_di.di_uid != current->fsuid)
952 			*mode &= ~07111;
953 		*uid = dip->i_di.di_uid;
954 	} else
955 		*uid = current->fsuid;
956 
957 	if (dip->i_di.di_mode & S_ISGID) {
958 		if (S_ISDIR(*mode))
959 			*mode |= S_ISGID;
960 		*gid = dip->i_di.di_gid;
961 	} else
962 		*gid = current->fsgid;
963 }
964 
965 static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_unlinked *ul)
966 {
967 	struct gfs2_sbd *sdp = dip->i_sbd;
968 	int error;
969 
970 	gfs2_alloc_get(dip);
971 
972 	dip->i_alloc.al_requested = RES_DINODE;
973 	error = gfs2_inplace_reserve(dip);
974 	if (error)
975 		goto out;
976 
977 	error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED +
978 				 RES_STATFS, 0);
979 	if (error)
980 		goto out_ipreserv;
981 
982 	ul->ul_ut.ut_inum.no_addr = gfs2_alloc_di(dip);
983 
984 	ul->ul_ut.ut_flags = GFS2_UTF_UNINIT;
985 	error = gfs2_unlinked_ondisk_add(sdp, ul);
986 
987 	gfs2_trans_end(sdp);
988 
989  out_ipreserv:
990 	gfs2_inplace_release(dip);
991 
992  out:
993 	gfs2_alloc_put(dip);
994 
995 	return error;
996 }
997 
998 /**
999  * init_dinode - Fill in a new dinode structure
1000  * @dip: the directory this inode is being created in
1001  * @gl: The glock covering the new inode
1002  * @inum: the inode number
1003  * @mode: the file permissions
1004  * @uid:
1005  * @gid:
1006  *
1007  */
1008 
1009 static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
1010 			struct gfs2_inum *inum, unsigned int mode,
1011 			unsigned int uid, unsigned int gid)
1012 {
1013 	struct gfs2_sbd *sdp = dip->i_sbd;
1014 	struct gfs2_dinode *di;
1015 	struct buffer_head *dibh;
1016 
1017 	dibh = gfs2_meta_new(gl, inum->no_addr);
1018 	gfs2_trans_add_bh(gl, dibh, 1);
1019 	gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
1020 	gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1021 	di = (struct gfs2_dinode *)dibh->b_data;
1022 
1023 	di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
1024 	di->di_num.no_addr = cpu_to_be64(inum->no_addr);
1025 	di->di_mode = cpu_to_be32(mode);
1026 	di->di_uid = cpu_to_be32(uid);
1027 	di->di_gid = cpu_to_be32(gid);
1028 	di->di_nlink = cpu_to_be32(0);
1029 	di->di_size = cpu_to_be64(0);
1030 	di->di_blocks = cpu_to_be64(1);
1031 	di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds());
1032 	di->di_major = di->di_minor = cpu_to_be32(0);
1033 	di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
1034 	di->__pad[0] = di->__pad[1] = 0;
1035 	di->di_flags = cpu_to_be32(0);
1036 
1037 	if (S_ISREG(mode)) {
1038 		if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
1039 		    gfs2_tune_get(sdp, gt_new_files_jdata))
1040 			di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
1041 		if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
1042 		    gfs2_tune_get(sdp, gt_new_files_directio))
1043 			di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
1044 	} else if (S_ISDIR(mode)) {
1045 		di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
1046 					    GFS2_DIF_INHERIT_DIRECTIO);
1047 		di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
1048 					    GFS2_DIF_INHERIT_JDATA);
1049 	}
1050 
1051 	di->__pad1 = 0;
1052 	di->di_height = cpu_to_be32(0);
1053 	di->__pad2 = 0;
1054 	di->__pad3 = 0;
1055 	di->di_depth = cpu_to_be16(0);
1056 	di->di_entries = cpu_to_be32(0);
1057 	memset(&di->__pad4, 0, sizeof(di->__pad4));
1058 	di->di_eattr = cpu_to_be64(0);
1059 	memset(&di->di_reserved, 0, sizeof(di->di_reserved));
1060 
1061 	brelse(dibh);
1062 }
1063 
1064 static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
1065 		       unsigned int mode, struct gfs2_unlinked *ul)
1066 {
1067 	struct gfs2_sbd *sdp = dip->i_sbd;
1068 	unsigned int uid, gid;
1069 	int error;
1070 
1071 	munge_mode_uid_gid(dip, &mode, &uid, &gid);
1072 
1073 	gfs2_alloc_get(dip);
1074 
1075 	error = gfs2_quota_lock(dip, uid, gid);
1076 	if (error)
1077 		goto out;
1078 
1079 	error = gfs2_quota_check(dip, uid, gid);
1080 	if (error)
1081 		goto out_quota;
1082 
1083 	error = gfs2_trans_begin(sdp, RES_DINODE + RES_UNLINKED +
1084 				 RES_QUOTA, 0);
1085 	if (error)
1086 		goto out_quota;
1087 
1088 	ul->ul_ut.ut_flags = 0;
1089 	error = gfs2_unlinked_ondisk_munge(sdp, ul);
1090 
1091 	init_dinode(dip, gl, &ul->ul_ut.ut_inum,
1092 		     mode, uid, gid);
1093 
1094 	gfs2_quota_change(dip, +1, uid, gid);
1095 
1096 	gfs2_trans_end(sdp);
1097 
1098  out_quota:
1099 	gfs2_quota_unlock(dip);
1100 
1101  out:
1102 	gfs2_alloc_put(dip);
1103 
1104 	return error;
1105 }
1106 
1107 static int link_dinode(struct gfs2_inode *dip, struct qstr *name,
1108 		       struct gfs2_inode *ip, struct gfs2_unlinked *ul)
1109 {
1110 	struct gfs2_sbd *sdp = dip->i_sbd;
1111 	struct gfs2_alloc *al;
1112 	int alloc_required;
1113 	struct buffer_head *dibh;
1114 	int error;
1115 
1116 	al = gfs2_alloc_get(dip);
1117 
1118 	error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1119 	if (error)
1120 		goto fail;
1121 
1122 	error = alloc_required = gfs2_diradd_alloc_required(dip->i_vnode, name);
1123 	if (alloc_required < 0)
1124 		goto fail;
1125 	if (alloc_required) {
1126 		error = gfs2_quota_check(dip, dip->i_di.di_uid,
1127 					 dip->i_di.di_gid);
1128 		if (error)
1129 			goto fail_quota_locks;
1130 
1131 		al->al_requested = sdp->sd_max_dirres;
1132 
1133 		error = gfs2_inplace_reserve(dip);
1134 		if (error)
1135 			goto fail_quota_locks;
1136 
1137 		error = gfs2_trans_begin(sdp,
1138 					 sdp->sd_max_dirres +
1139 					 al->al_rgd->rd_ri.ri_length +
1140 					 2 * RES_DINODE + RES_UNLINKED +
1141 					 RES_STATFS + RES_QUOTA, 0);
1142 		if (error)
1143 			goto fail_ipreserv;
1144 	} else {
1145 		error = gfs2_trans_begin(sdp,
1146 					 RES_LEAF +
1147 					 2 * RES_DINODE +
1148 					 RES_UNLINKED, 0);
1149 		if (error)
1150 			goto fail_quota_locks;
1151 	}
1152 
1153 	error = gfs2_dir_add(dip->i_vnode, name, &ip->i_num, IF2DT(ip->i_di.di_mode));
1154 	if (error)
1155 		goto fail_end_trans;
1156 
1157 	error = gfs2_meta_inode_buffer(ip, &dibh);
1158 	if (error)
1159 		goto fail_end_trans;
1160 	ip->i_di.di_nlink = 1;
1161 	gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1162 	gfs2_dinode_out(&ip->i_di, dibh->b_data);
1163 	brelse(dibh);
1164 
1165 	error = gfs2_unlinked_ondisk_rm(sdp, ul);
1166 	if (error)
1167 		goto fail_end_trans;
1168 
1169 	return 0;
1170 
1171  fail_end_trans:
1172 	gfs2_trans_end(sdp);
1173 
1174  fail_ipreserv:
1175 	if (dip->i_alloc.al_rgd)
1176 		gfs2_inplace_release(dip);
1177 
1178  fail_quota_locks:
1179 	gfs2_quota_unlock(dip);
1180 
1181  fail:
1182 	gfs2_alloc_put(dip);
1183 
1184 	return error;
1185 }
1186 
1187 /**
1188  * gfs2_createi - Create a new inode
1189  * @ghs: An array of two holders
1190  * @name: The name of the new file
1191  * @mode: the permissions on the new inode
1192  *
1193  * @ghs[0] is an initialized holder for the directory
1194  * @ghs[1] is the holder for the inode lock
1195  *
1196  * If the return value is not NULL, the glocks on both the directory and the new
1197  * file are held.  A transaction has been started and an inplace reservation
1198  * is held, as well.
1199  *
1200  * Returns: An inode
1201  */
1202 
1203 struct inode *gfs2_createi(struct gfs2_holder *ghs, struct qstr *name,
1204 			   unsigned int mode)
1205 {
1206 	struct inode *inode;
1207 	struct gfs2_inode *dip = ghs->gh_gl->gl_object;
1208 	struct gfs2_sbd *sdp = dip->i_sbd;
1209 	struct gfs2_unlinked *ul;
1210 	struct gfs2_inode *ip;
1211 	int error;
1212 
1213 	if (!name->len || name->len > GFS2_FNAMESIZE)
1214 		return ERR_PTR(-ENAMETOOLONG);
1215 
1216 	error = gfs2_unlinked_get(sdp, &ul);
1217 	if (error)
1218 		return ERR_PTR(error);
1219 
1220 	gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
1221 	error = gfs2_glock_nq(ghs);
1222 	if (error)
1223 		goto fail;
1224 
1225 	error = create_ok(dip, name, mode);
1226 	if (error)
1227 		goto fail_gunlock;
1228 
1229 	error = pick_formal_ino(sdp, &ul->ul_ut.ut_inum.no_formal_ino);
1230 	if (error)
1231 		goto fail_gunlock;
1232 
1233 	error = alloc_dinode(dip, ul);
1234 	if (error)
1235 		goto fail_gunlock;
1236 
1237 	if (ul->ul_ut.ut_inum.no_addr < dip->i_num.no_addr) {
1238 		gfs2_glock_dq(ghs);
1239 
1240 		error = gfs2_glock_nq_num(sdp,
1241 					  ul->ul_ut.ut_inum.no_addr,
1242 					  &gfs2_inode_glops,
1243 					  LM_ST_EXCLUSIVE, GL_SKIP,
1244 					  ghs + 1);
1245 		if (error) {
1246 			gfs2_unlinked_put(sdp, ul);
1247 			return ERR_PTR(error);
1248 		}
1249 
1250 		gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
1251 		error = gfs2_glock_nq(ghs);
1252 		if (error) {
1253 			gfs2_glock_dq_uninit(ghs + 1);
1254 			gfs2_unlinked_put(sdp, ul);
1255 			return ERR_PTR(error);
1256 		}
1257 
1258 		error = create_ok(dip, name, mode);
1259 		if (error)
1260 			goto fail_gunlock2;
1261 	} else {
1262 		error = gfs2_glock_nq_num(sdp,
1263 					  ul->ul_ut.ut_inum.no_addr,
1264 					  &gfs2_inode_glops,
1265 					  LM_ST_EXCLUSIVE, GL_SKIP,
1266 					  ghs + 1);
1267 		if (error)
1268 			goto fail_gunlock;
1269 	}
1270 
1271 	error = make_dinode(dip, ghs[1].gh_gl, mode, ul);
1272 	if (error)
1273 		goto fail_gunlock2;
1274 
1275 	error = gfs2_inode_get(ghs[1].gh_gl, &ul->ul_ut.ut_inum, CREATE, &ip);
1276 	if (error)
1277 		goto fail_gunlock2;
1278 
1279 	error = gfs2_inode_refresh(ip);
1280 	if (error)
1281 		goto fail_iput;
1282 
1283 	error = gfs2_acl_create(dip, ip);
1284 	if (error)
1285 		goto fail_iput;
1286 
1287 	error = link_dinode(dip, name, ip, ul);
1288 	if (error)
1289 		goto fail_iput;
1290 
1291 	gfs2_unlinked_put(sdp, ul);
1292 
1293 	inode = gfs2_ip2v(ip);
1294 	gfs2_inode_put(ip);
1295 	if (!inode)
1296 		return ERR_PTR(-ENOMEM);
1297 	return inode;
1298 
1299  fail_iput:
1300 	gfs2_inode_put(ip);
1301 
1302  fail_gunlock2:
1303 	gfs2_glock_dq_uninit(ghs + 1);
1304 
1305  fail_gunlock:
1306 	gfs2_glock_dq(ghs);
1307 
1308  fail:
1309 	gfs2_unlinked_put(sdp, ul);
1310 
1311 	return ERR_PTR(error);
1312 }
1313 
1314 /**
1315  * gfs2_unlinki - Unlink a file
1316  * @dip: The inode of the directory
1317  * @name: The name of the file to be unlinked
1318  * @ip: The inode of the file to be removed
1319  *
1320  * Assumes Glocks on both dip and ip are held.
1321  *
1322  * Returns: errno
1323  */
1324 
1325 int gfs2_unlinki(struct gfs2_inode *dip, struct qstr *name,
1326 		 struct gfs2_inode *ip, struct gfs2_unlinked *ul)
1327 {
1328 	struct gfs2_sbd *sdp = dip->i_sbd;
1329 	int error;
1330 
1331 	error = gfs2_dir_del(dip, name);
1332 	if (error)
1333 		return error;
1334 
1335 	error = gfs2_change_nlink(ip, -1);
1336 	if (error)
1337 		return error;
1338 
1339 	/* If this inode is being unlinked from the directory structure,
1340 	   we need to mark that in the log so that it isn't lost during
1341 	   a crash. */
1342 
1343 	if (!ip->i_di.di_nlink) {
1344 		ul->ul_ut.ut_inum = ip->i_num;
1345 		error = gfs2_unlinked_ondisk_add(sdp, ul);
1346 		if (!error)
1347 			set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
1348 	}
1349 
1350 	return error;
1351 }
1352 
1353 /**
1354  * gfs2_rmdiri - Remove a directory
1355  * @dip: The parent directory of the directory to be removed
1356  * @name: The name of the directory to be removed
1357  * @ip: The GFS2 inode of the directory to be removed
1358  *
1359  * Assumes Glocks on dip and ip are held
1360  *
1361  * Returns: errno
1362  */
1363 
1364 int gfs2_rmdiri(struct gfs2_inode *dip, struct qstr *name,
1365 		struct gfs2_inode *ip, struct gfs2_unlinked *ul)
1366 {
1367 	struct gfs2_sbd *sdp = dip->i_sbd;
1368 	struct qstr dotname;
1369 	int error;
1370 
1371 	if (ip->i_di.di_entries != 2) {
1372 		if (gfs2_consist_inode(ip))
1373 			gfs2_dinode_print(&ip->i_di);
1374 		return -EIO;
1375 	}
1376 
1377 	error = gfs2_dir_del(dip, name);
1378 	if (error)
1379 		return error;
1380 
1381 	error = gfs2_change_nlink(dip, -1);
1382 	if (error)
1383 		return error;
1384 
1385 	gfs2_str2qstr(&dotname, ".");
1386 	error = gfs2_dir_del(ip, &dotname);
1387 	if (error)
1388 		return error;
1389 
1390 	dotname.len = 2;
1391 	dotname.name = "..";
1392 	dotname.hash = gfs2_disk_hash(dotname.name, dotname.len);
1393 	error = gfs2_dir_del(ip, &dotname);
1394 	if (error)
1395 		return error;
1396 
1397 	error = gfs2_change_nlink(ip, -2);
1398 	if (error)
1399 		return error;
1400 
1401 	/* This inode is being unlinked from the directory structure and
1402 	   we need to mark that in the log so that it isn't lost during
1403 	   a crash. */
1404 
1405 	ul->ul_ut.ut_inum = ip->i_num;
1406 	error = gfs2_unlinked_ondisk_add(sdp, ul);
1407 	if (!error)
1408 		set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
1409 
1410 	return error;
1411 }
1412 
1413 /*
1414  * gfs2_unlink_ok - check to see that a inode is still in a directory
1415  * @dip: the directory
1416  * @name: the name of the file
1417  * @ip: the inode
1418  *
1419  * Assumes that the lock on (at least) @dip is held.
1420  *
1421  * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1422  */
1423 
1424 int gfs2_unlink_ok(struct gfs2_inode *dip, struct qstr *name,
1425 		   struct gfs2_inode *ip)
1426 {
1427 	struct gfs2_inum inum;
1428 	unsigned int type;
1429 	int error;
1430 
1431 	if (IS_IMMUTABLE(ip->i_vnode) || IS_APPEND(ip->i_vnode))
1432 		return -EPERM;
1433 
1434 	if ((dip->i_di.di_mode & S_ISVTX) &&
1435 	    dip->i_di.di_uid != current->fsuid &&
1436 	    ip->i_di.di_uid != current->fsuid &&
1437 	    !capable(CAP_FOWNER))
1438 		return -EPERM;
1439 
1440 	if (IS_APPEND(dip->i_vnode))
1441 		return -EPERM;
1442 
1443 	error = gfs2_repermission(dip->i_vnode, MAY_WRITE | MAY_EXEC, NULL);
1444 	if (error)
1445 		return error;
1446 
1447 	error = gfs2_dir_search(dip->i_vnode, name, &inum, &type);
1448 	if (error)
1449 		return error;
1450 
1451 	if (!gfs2_inum_equal(&inum, &ip->i_num))
1452 		return -ENOENT;
1453 
1454 	if (IF2DT(ip->i_di.di_mode) != type) {
1455 		gfs2_consist_inode(dip);
1456 		return -EIO;
1457 	}
1458 
1459 	return 0;
1460 }
1461 
1462 /*
1463  * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1464  * @this: move this
1465  * @to: to here
1466  *
1467  * Follow @to back to the root and make sure we don't encounter @this
1468  * Assumes we already hold the rename lock.
1469  *
1470  * Returns: errno
1471  */
1472 
1473 int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1474 {
1475 	struct inode *dir = to->i_vnode;
1476 	struct super_block *sb = dir->i_sb;
1477 	struct inode *tmp;
1478 	struct qstr dotdot;
1479 	int error = 0;
1480 
1481 	gfs2_str2qstr(&dotdot, "..");
1482 
1483 	igrab(dir);
1484 
1485 	for (;;) {
1486 		if (dir == this->i_vnode) {
1487 			error = -EINVAL;
1488 			break;
1489 		}
1490 		if (dir == sb->s_root->d_inode) {
1491 			error = 0;
1492 			break;
1493 		}
1494 
1495 		tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
1496 		if (IS_ERR(tmp)) {
1497 			error = PTR_ERR(tmp);
1498 			break;
1499 		}
1500 
1501 		iput(dir);
1502 		dir = tmp;
1503 	}
1504 
1505 	iput(dir);
1506 
1507 	return error;
1508 }
1509 
1510 /**
1511  * gfs2_readlinki - return the contents of a symlink
1512  * @ip: the symlink's inode
1513  * @buf: a pointer to the buffer to be filled
1514  * @len: a pointer to the length of @buf
1515  *
1516  * If @buf is too small, a piece of memory is kmalloc()ed and needs
1517  * to be freed by the caller.
1518  *
1519  * Returns: errno
1520  */
1521 
1522 int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
1523 {
1524 	struct gfs2_holder i_gh;
1525 	struct buffer_head *dibh;
1526 	unsigned int x;
1527 	int error;
1528 
1529 	gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
1530 	error = gfs2_glock_nq_atime(&i_gh);
1531 	if (error) {
1532 		gfs2_holder_uninit(&i_gh);
1533 		return error;
1534 	}
1535 
1536 	if (!ip->i_di.di_size) {
1537 		gfs2_consist_inode(ip);
1538 		error = -EIO;
1539 		goto out;
1540 	}
1541 
1542 	error = gfs2_meta_inode_buffer(ip, &dibh);
1543 	if (error)
1544 		goto out;
1545 
1546 	x = ip->i_di.di_size + 1;
1547 	if (x > *len) {
1548 		*buf = kmalloc(x, GFP_KERNEL);
1549 		if (!*buf) {
1550 			error = -ENOMEM;
1551 			goto out_brelse;
1552 		}
1553 	}
1554 
1555 	memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1556 	*len = x;
1557 
1558  out_brelse:
1559 	brelse(dibh);
1560 
1561  out:
1562 	gfs2_glock_dq_uninit(&i_gh);
1563 
1564 	return error;
1565 }
1566 
1567 /**
1568  * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
1569  *       conditionally update the inode's atime
1570  * @gh: the holder to acquire
1571  *
1572  * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
1573  * Update if the difference between the current time and the inode's current
1574  * atime is greater than an interval specified at mount.
1575  *
1576  * Returns: errno
1577  */
1578 
1579 int gfs2_glock_nq_atime(struct gfs2_holder *gh)
1580 {
1581 	struct gfs2_glock *gl = gh->gh_gl;
1582 	struct gfs2_sbd *sdp = gl->gl_sbd;
1583 	struct gfs2_inode *ip = gl->gl_object;
1584 	int64_t curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum);
1585 	unsigned int state;
1586 	int flags;
1587 	int error;
1588 
1589 	if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
1590 	    gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
1591 	    gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
1592 		return -EINVAL;
1593 
1594 	state = gh->gh_state;
1595 	flags = gh->gh_flags;
1596 
1597 	error = gfs2_glock_nq(gh);
1598 	if (error)
1599 		return error;
1600 
1601 	if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
1602 	    (sdp->sd_vfs->s_flags & MS_RDONLY))
1603 		return 0;
1604 
1605 	curtime = get_seconds();
1606 	if (curtime - ip->i_di.di_atime >= quantum) {
1607 		gfs2_glock_dq(gh);
1608 		gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
1609 				   gh);
1610 		error = gfs2_glock_nq(gh);
1611 		if (error)
1612 			return error;
1613 
1614 		/* Verify that atime hasn't been updated while we were
1615 		   trying to get exclusive lock. */
1616 
1617 		curtime = get_seconds();
1618 		if (curtime - ip->i_di.di_atime >= quantum) {
1619 			struct buffer_head *dibh;
1620 
1621 			error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1622 			if (error == -EROFS)
1623 				return 0;
1624 			if (error)
1625 				goto fail;
1626 
1627 			error = gfs2_meta_inode_buffer(ip, &dibh);
1628 			if (error)
1629 				goto fail_end_trans;
1630 
1631 			ip->i_di.di_atime = curtime;
1632 
1633 			gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1634 			gfs2_dinode_out(&ip->i_di, dibh->b_data);
1635 			brelse(dibh);
1636 
1637 			gfs2_trans_end(sdp);
1638 		}
1639 
1640 		/* If someone else has asked for the glock,
1641 		   unlock and let them have it. Then reacquire
1642 		   in the original state. */
1643 		if (gfs2_glock_is_blocking(gl)) {
1644 			gfs2_glock_dq(gh);
1645 			gfs2_holder_reinit(state, flags, gh);
1646 			return gfs2_glock_nq(gh);
1647 		}
1648 	}
1649 
1650 	return 0;
1651 
1652  fail_end_trans:
1653 	gfs2_trans_end(sdp);
1654 
1655  fail:
1656 	gfs2_glock_dq(gh);
1657 
1658 	return error;
1659 }
1660 
1661 /**
1662  * glock_compare_atime - Compare two struct gfs2_glock structures for sort
1663  * @arg_a: the first structure
1664  * @arg_b: the second structure
1665  *
1666  * Returns: 1 if A > B
1667  *         -1 if A < B
1668  *          0 if A = B
1669  */
1670 
1671 static int glock_compare_atime(const void *arg_a, const void *arg_b)
1672 {
1673 	struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1674 	struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1675 	struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1676 	struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1677 	int ret = 0;
1678 
1679 	if (a->ln_number > b->ln_number)
1680 		ret = 1;
1681 	else if (a->ln_number < b->ln_number)
1682 		ret = -1;
1683 	else {
1684 		if (gh_a->gh_state == LM_ST_SHARED &&
1685 		    gh_b->gh_state == LM_ST_EXCLUSIVE)
1686 			ret = 1;
1687 		else if (gh_a->gh_state == LM_ST_SHARED &&
1688 			 (gh_b->gh_flags & GL_ATIME))
1689 			ret = 1;
1690 	}
1691 
1692 	return ret;
1693 }
1694 
1695 /**
1696  * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an
1697  *      atime update
1698  * @num_gh: the number of structures
1699  * @ghs: an array of struct gfs2_holder structures
1700  *
1701  * Returns: 0 on success (all glocks acquired),
1702  *          errno on failure (no glocks acquired)
1703  */
1704 
1705 int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs)
1706 {
1707 	struct gfs2_holder **p;
1708 	unsigned int x;
1709 	int error = 0;
1710 
1711 	if (!num_gh)
1712 		return 0;
1713 
1714 	if (num_gh == 1) {
1715 		ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1716 		if (ghs->gh_flags & GL_ATIME)
1717 			error = gfs2_glock_nq_atime(ghs);
1718 		else
1719 			error = gfs2_glock_nq(ghs);
1720 		return error;
1721 	}
1722 
1723 	p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1724 	if (!p)
1725 		return -ENOMEM;
1726 
1727 	for (x = 0; x < num_gh; x++)
1728 		p[x] = &ghs[x];
1729 
1730 	sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL);
1731 
1732 	for (x = 0; x < num_gh; x++) {
1733 		p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1734 
1735 		if (p[x]->gh_flags & GL_ATIME)
1736 			error = gfs2_glock_nq_atime(p[x]);
1737 		else
1738 			error = gfs2_glock_nq(p[x]);
1739 
1740 		if (error) {
1741 			while (x--)
1742 				gfs2_glock_dq(p[x]);
1743 			break;
1744 		}
1745 	}
1746 
1747 	kfree(p);
1748 
1749 	return error;
1750 }
1751 
1752 /**
1753  * gfs2_try_toss_vnode - See if we can toss a vnode from memory
1754  * @ip: the inode
1755  *
1756  * Returns:  1 if the vnode was tossed
1757  */
1758 
1759 void gfs2_try_toss_vnode(struct gfs2_inode *ip)
1760 {
1761 	struct inode *inode;
1762 
1763 	inode = gfs2_ip2v_lookup(ip);
1764 	if (!inode)
1765 		return;
1766 
1767 	d_prune_aliases(inode);
1768 
1769 	if (S_ISDIR(ip->i_di.di_mode)) {
1770 		struct list_head *head = &inode->i_dentry;
1771 		struct dentry *d = NULL;
1772 
1773 		spin_lock(&dcache_lock);
1774 		if (list_empty(head))
1775 			spin_unlock(&dcache_lock);
1776 		else {
1777 			d = list_entry(head->next, struct dentry, d_alias);
1778 			dget_locked(d);
1779 			spin_unlock(&dcache_lock);
1780 
1781 			if (have_submounts(d))
1782 				dput(d);
1783 			else {
1784 				shrink_dcache_parent(d);
1785 				dput(d);
1786 				d_prune_aliases(inode);
1787 			}
1788 		}
1789 	}
1790 
1791 	inode->i_nlink = 0;
1792 	iput(inode);
1793 }
1794 
1795 
1796 static int
1797 __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1798 {
1799 	struct buffer_head *dibh;
1800 	int error;
1801 
1802 	error = gfs2_meta_inode_buffer(ip, &dibh);
1803 	if (!error) {
1804 		error = inode_setattr(ip->i_vnode, attr);
1805 		gfs2_assert_warn(ip->i_sbd, !error);
1806 		gfs2_inode_attr_out(ip);
1807 
1808 		gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1809 		gfs2_dinode_out(&ip->i_di, dibh->b_data);
1810 		brelse(dibh);
1811 	}
1812 	return error;
1813 }
1814 
1815 /**
1816  * gfs2_setattr_simple -
1817  * @ip:
1818  * @attr:
1819  *
1820  * Called with a reference on the vnode.
1821  *
1822  * Returns: errno
1823  */
1824 
1825 int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1826 {
1827 	int error;
1828 
1829 	if (current->journal_info)
1830 		return __gfs2_setattr_simple(ip, attr);
1831 
1832 	error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
1833 	if (error)
1834 		return error;
1835 
1836 	error = __gfs2_setattr_simple(ip, attr);
1837 
1838 	gfs2_trans_end(ip->i_sbd);
1839 
1840 	return error;
1841 }
1842 
1843 int gfs2_repermission(struct inode *inode, int mask, struct nameidata *nd)
1844 {
1845 	return permission(inode, mask, nd);
1846 }
1847 
1848