2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
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.
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 <asm/semaphore.h>
21 #include "lm_interface.h"
32 #include "ops_address.h"
34 #include "ops_inode.h"
42 * inode_attr_in - Copy attributes from the dinode into the VFS inode
43 * @ip: The GFS2 inode (with embedded disk inode data)
44 * @inode: The Linux VFS inode
48 static void inode_attr_in(struct gfs2_inode *ip, struct inode *inode)
50 inode->i_ino = ip->i_num.no_formal_ino;
52 switch (ip->i_di.di_mode & S_IFMT) {
55 inode->i_rdev = MKDEV(ip->i_di.di_major, ip->i_di.di_minor);
62 inode->i_mode = ip->i_di.di_mode;
63 inode->i_nlink = ip->i_di.di_nlink;
64 inode->i_uid = ip->i_di.di_uid;
65 inode->i_gid = ip->i_di.di_gid;
66 i_size_write(inode, ip->i_di.di_size);
67 inode->i_atime.tv_sec = ip->i_di.di_atime;
68 inode->i_mtime.tv_sec = ip->i_di.di_mtime;
69 inode->i_ctime.tv_sec = ip->i_di.di_ctime;
70 inode->i_atime.tv_nsec = 0;
71 inode->i_mtime.tv_nsec = 0;
72 inode->i_ctime.tv_nsec = 0;
73 inode->i_blksize = PAGE_SIZE;
74 inode->i_blocks = ip->i_di.di_blocks <<
75 (ip->i_sbd->sd_sb.sb_bsize_shift - GFS2_BASIC_BLOCK_SHIFT);
77 if (ip->i_di.di_flags & GFS2_DIF_IMMUTABLE)
78 inode->i_flags |= S_IMMUTABLE;
80 inode->i_flags &= ~S_IMMUTABLE;
82 if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY)
83 inode->i_flags |= S_APPEND;
85 inode->i_flags &= ~S_APPEND;
89 * gfs2_inode_attr_in - Copy attributes from the dinode into the VFS inode
90 * @ip: The GFS2 inode (with embedded disk inode data)
94 void gfs2_inode_attr_in(struct gfs2_inode *ip)
98 inode = gfs2_ip2v_lookup(ip);
100 inode_attr_in(ip, inode);
106 * gfs2_inode_attr_out - Copy attributes from VFS inode into the dinode
107 * @ip: The GFS2 inode
109 * Only copy out the attributes that we want the VFS layer
110 * to be able to modify.
113 void gfs2_inode_attr_out(struct gfs2_inode *ip)
115 struct inode *inode = ip->i_vnode;
117 gfs2_assert_withdraw(ip->i_sbd,
118 (ip->i_di.di_mode & S_IFMT) == (inode->i_mode & S_IFMT));
119 ip->i_di.di_mode = inode->i_mode;
120 ip->i_di.di_uid = inode->i_uid;
121 ip->i_di.di_gid = inode->i_gid;
122 ip->i_di.di_atime = inode->i_atime.tv_sec;
123 ip->i_di.di_mtime = inode->i_mtime.tv_sec;
124 ip->i_di.di_ctime = inode->i_ctime.tv_sec;
128 * gfs2_ip2v_lookup - Get the struct inode for a struct gfs2_inode
129 * @ip: the struct gfs2_inode to get the struct inode for
131 * Returns: A VFS inode, or NULL if none
134 struct inode *gfs2_ip2v_lookup(struct gfs2_inode *ip)
136 struct inode *inode = NULL;
138 gfs2_assert_warn(ip->i_sbd, test_bit(GIF_MIN_INIT, &ip->i_flags));
140 spin_lock(&ip->i_spin);
142 inode = igrab(ip->i_vnode);
143 spin_unlock(&ip->i_spin);
149 * gfs2_ip2v - Get/Create a struct inode for a struct gfs2_inode
150 * @ip: the struct gfs2_inode to get the struct inode for
152 * Returns: A VFS inode, or NULL if no mem
155 struct inode *gfs2_ip2v(struct gfs2_inode *ip)
157 struct inode *inode, *tmp;
159 inode = gfs2_ip2v_lookup(ip);
163 tmp = new_inode(ip->i_sbd->sd_vfs);
167 inode_attr_in(ip, tmp);
169 if (S_ISREG(ip->i_di.di_mode)) {
170 tmp->i_op = &gfs2_file_iops;
171 tmp->i_fop = &gfs2_file_fops;
172 tmp->i_mapping->a_ops = &gfs2_file_aops;
173 } else if (S_ISDIR(ip->i_di.di_mode)) {
174 tmp->i_op = &gfs2_dir_iops;
175 tmp->i_fop = &gfs2_dir_fops;
176 } else if (S_ISLNK(ip->i_di.di_mode)) {
177 tmp->i_op = &gfs2_symlink_iops;
179 tmp->i_op = &gfs2_dev_iops;
180 init_special_inode(tmp, tmp->i_mode, tmp->i_rdev);
183 tmp->u.generic_ip = NULL;
186 spin_lock(&ip->i_spin);
189 inode = igrab(ip->i_vnode);
190 spin_unlock(&ip->i_spin);
203 inode->u.generic_ip = ip;
205 spin_unlock(&ip->i_spin);
207 insert_inode_hash(inode);
212 static int iget_test(struct inode *inode, void *opaque)
214 struct gfs2_inode *ip = inode->u.generic_ip;
215 struct gfs2_inum *inum = (struct gfs2_inum *)opaque;
217 if (ip && ip->i_num.no_addr == inum->no_addr)
223 struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum *inum)
225 return ilookup5(sb, (unsigned long)inum->no_formal_ino,
229 void gfs2_inode_min_init(struct gfs2_inode *ip, unsigned int type)
231 spin_lock(&ip->i_spin);
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);
236 spin_unlock(&ip->i_spin);
240 * gfs2_inode_refresh - Refresh the incore copy of the dinode
241 * @ip: The GFS2 inode
246 int gfs2_inode_refresh(struct gfs2_inode *ip)
248 struct buffer_head *dibh;
251 error = gfs2_meta_inode_buffer(ip, &dibh);
255 if (gfs2_metatype_check(ip->i_sbd, dibh, GFS2_METATYPE_DI)) {
260 spin_lock(&ip->i_spin);
261 gfs2_dinode_in(&ip->i_di, dibh->b_data);
262 set_bit(GIF_MIN_INIT, &ip->i_flags);
263 spin_unlock(&ip->i_spin);
267 if (ip->i_num.no_addr != ip->i_di.di_num.no_addr) {
268 if (gfs2_consist_inode(ip))
269 gfs2_dinode_print(&ip->i_di);
272 if (ip->i_num.no_formal_ino != ip->i_di.di_num.no_formal_ino)
275 ip->i_vn = ip->i_gl->gl_vn;
281 * inode_create - create a struct gfs2_inode
282 * @i_gl: The glock covering the inode
283 * @inum: The inode number
284 * @io_gl: the iopen glock to acquire/hold (using holder in new gfs2_inode)
285 * @io_state: the state the iopen glock should be acquired in
286 * @ipp: pointer to put the returned inode in
291 static int inode_create(struct gfs2_glock *i_gl, const struct gfs2_inum *inum,
292 struct gfs2_glock *io_gl, unsigned int io_state,
293 struct gfs2_inode **ipp)
295 struct gfs2_sbd *sdp = i_gl->gl_sbd;
296 struct gfs2_inode *ip;
299 ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
302 memset(ip, 0, sizeof(struct gfs2_inode));
306 atomic_set(&ip->i_count, 1);
308 ip->i_vn = i_gl->gl_vn - 1;
313 spin_lock_init(&ip->i_spin);
314 init_rwsem(&ip->i_rw_mutex);
316 ip->i_greedy = gfs2_tune_get(sdp, gt_greedy_default);
318 error = gfs2_glock_nq_init(io_gl,
319 io_state, GL_LOCAL_EXCL | GL_EXACT,
323 ip->i_iopen_gh.gh_owner = NULL;
325 spin_lock(&io_gl->gl_spin);
326 gfs2_glock_hold(i_gl);
327 io_gl->gl_object = i_gl;
328 spin_unlock(&io_gl->gl_spin);
330 gfs2_glock_hold(i_gl);
331 i_gl->gl_object = ip;
333 atomic_inc(&sdp->sd_inode_count);
340 gfs2_meta_cache_flush(ip);
341 kmem_cache_free(gfs2_inode_cachep, ip);
348 * gfs2_inode_get - Create or get a reference on an inode
349 * @i_gl: The glock covering the inode
350 * @inum: The inode number
352 * @ipp: pointer to put the returned inode in
357 int gfs2_inode_get(struct gfs2_glock *i_gl, const struct gfs2_inum *inum,
358 int create, struct gfs2_inode **ipp)
360 struct gfs2_sbd *sdp = i_gl->gl_sbd;
361 struct gfs2_glock *io_gl;
364 gfs2_glmutex_lock(i_gl);
366 *ipp = i_gl->gl_object;
369 if ((*ipp)->i_num.no_formal_ino != inum->no_formal_ino)
371 atomic_inc(&(*ipp)->i_count);
379 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops,
382 error = inode_create(i_gl, inum, io_gl, LM_ST_SHARED, ipp);
383 gfs2_glock_put(io_gl);
387 gfs2_glmutex_unlock(i_gl);
392 void gfs2_inode_hold(struct gfs2_inode *ip)
394 gfs2_assert(ip->i_sbd, atomic_read(&ip->i_count) > 0);
395 atomic_inc(&ip->i_count);
398 void gfs2_inode_put(struct gfs2_inode *ip)
400 gfs2_assert(ip->i_sbd, atomic_read(&ip->i_count) > 0);
401 atomic_dec(&ip->i_count);
404 void gfs2_inode_destroy(struct gfs2_inode *ip)
406 struct gfs2_sbd *sdp = ip->i_sbd;
407 struct gfs2_glock *io_gl = ip->i_iopen_gh.gh_gl;
408 struct gfs2_glock *i_gl = ip->i_gl;
410 gfs2_assert_warn(sdp, !atomic_read(&ip->i_count));
411 gfs2_assert(sdp, io_gl->gl_object == i_gl);
413 spin_lock(&io_gl->gl_spin);
414 io_gl->gl_object = NULL;
415 gfs2_glock_put(i_gl);
416 spin_unlock(&io_gl->gl_spin);
418 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
420 gfs2_meta_cache_flush(ip);
421 kmem_cache_free(gfs2_inode_cachep, ip);
423 i_gl->gl_object = NULL;
424 gfs2_glock_put(i_gl);
426 atomic_dec(&sdp->sd_inode_count);
429 static int dinode_dealloc(struct gfs2_inode *ip, struct gfs2_unlinked *ul)
431 struct gfs2_sbd *sdp = ip->i_sbd;
432 struct gfs2_alloc *al;
433 struct gfs2_rgrpd *rgd;
436 if (ip->i_di.di_blocks != 1) {
437 if (gfs2_consist_inode(ip))
438 gfs2_dinode_print(&ip->i_di);
442 al = gfs2_alloc_get(ip);
444 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
448 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
452 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
454 gfs2_consist_inode(ip);
456 goto out_rindex_relse;
459 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
462 goto out_rindex_relse;
464 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED +
465 RES_STATFS + RES_QUOTA, 1);
469 gfs2_trans_add_gl(ip->i_gl);
471 gfs2_free_di(rgd, ip);
473 error = gfs2_unlinked_ondisk_rm(sdp, ul);
476 clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
479 gfs2_glock_dq_uninit(&al->al_rgd_gh);
482 gfs2_glock_dq_uninit(&al->al_ri_gh);
485 gfs2_quota_unhold(ip);
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
502 static int inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul,
503 struct gfs2_holder *io_gh)
505 struct gfs2_inode *ip;
506 struct gfs2_holder i_gh;
509 error = gfs2_glock_nq_num(sdp,
510 ul->ul_ut.ut_inum.no_addr, &gfs2_inode_glops,
511 LM_ST_EXCLUSIVE, 0, &i_gh);
515 /* We reacquire the iopen lock here to avoid a race with the NFS server
516 calling gfs2_read_inode() with the inode number of a inode we're in
517 the process of deallocating. And we can't keep our hold on the lock
518 from inode_dealloc_init() for deadlock reasons. */
520 gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY, io_gh);
521 error = gfs2_glock_nq(io_gh);
531 gfs2_assert_warn(sdp, !i_gh.gh_gl->gl_object);
532 error = inode_create(i_gh.gh_gl, &ul->ul_ut.ut_inum, io_gh->gh_gl,
533 LM_ST_EXCLUSIVE, &ip);
535 gfs2_glock_dq(io_gh);
540 error = gfs2_inode_refresh(ip);
544 if (ip->i_di.di_nlink) {
545 if (gfs2_consist_inode(ip))
546 gfs2_dinode_print(&ip->i_di);
551 if (S_ISDIR(ip->i_di.di_mode) &&
552 (ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
553 error = gfs2_dir_exhash_dealloc(ip);
558 if (ip->i_di.di_eattr) {
559 error = gfs2_ea_dealloc(ip);
564 if (!gfs2_is_stuffed(ip)) {
565 error = gfs2_file_dealloc(ip);
570 error = dinode_dealloc(ip, ul);
575 gfs2_glmutex_lock(i_gh.gh_gl);
577 gfs2_inode_destroy(ip);
578 gfs2_glmutex_unlock(i_gh.gh_gl);
581 gfs2_glock_dq_uninit(&i_gh);
587 * try_inode_dealloc - Try to deallocate an inode and all its blocks
588 * @sdp: the filesystem
590 * Returns: 0 on success, -errno on error, 1 on busy (inode open)
593 static int try_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
595 struct gfs2_holder io_gh;
598 gfs2_try_toss_inode(sdp, &ul->ul_ut.ut_inum);
600 error = gfs2_glock_nq_num(sdp,
601 ul->ul_ut.ut_inum.no_addr, &gfs2_iopen_glops,
602 LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB, &io_gh);
612 gfs2_glock_dq(&io_gh);
613 error = inode_dealloc(sdp, ul, &io_gh);
614 gfs2_holder_uninit(&io_gh);
619 static int inode_dealloc_uninit(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
621 struct gfs2_rgrpd *rgd;
622 struct gfs2_holder ri_gh, rgd_gh;
625 error = gfs2_rindex_hold(sdp, &ri_gh);
629 rgd = gfs2_blk2rgrpd(sdp, ul->ul_ut.ut_inum.no_addr);
636 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rgd_gh);
640 error = gfs2_trans_begin(sdp,
641 RES_RG_BIT + RES_UNLINKED + RES_STATFS,
646 gfs2_free_uninit_di(rgd, ul->ul_ut.ut_inum.no_addr);
647 gfs2_unlinked_ondisk_rm(sdp, ul);
652 gfs2_glock_dq_uninit(&rgd_gh);
654 gfs2_glock_dq_uninit(&ri_gh);
659 int gfs2_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
661 if (ul->ul_ut.ut_flags & GFS2_UTF_UNINIT)
662 return inode_dealloc_uninit(sdp, ul);
664 return try_inode_dealloc(sdp, ul);
668 * gfs2_change_nlink - Change nlink count on inode
669 * @ip: The GFS2 inode
670 * @diff: The change in the nlink count required
675 int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
677 struct buffer_head *dibh;
681 nlink = ip->i_di.di_nlink + diff;
683 /* If we are reducing the nlink count, but the new value ends up being
684 bigger than the old one, we must have underflowed. */
685 if (diff < 0 && nlink > ip->i_di.di_nlink) {
686 if (gfs2_consist_inode(ip))
687 gfs2_dinode_print(&ip->i_di);
691 error = gfs2_meta_inode_buffer(ip, &dibh);
695 ip->i_di.di_nlink = nlink;
696 ip->i_di.di_ctime = get_seconds();
698 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
699 gfs2_dinode_out(&ip->i_di, dibh->b_data);
706 * gfs2_lookupi - Look up a filename in a directory and return its inode
707 * @d_gh: An initialized holder for the directory glock
708 * @name: The name of the inode to look for
709 * @is_root: If 1, ignore the caller's permissions
710 * @i_gh: An uninitialized holder for the new inode glock
712 * There will always be a vnode (Linux VFS inode) for the d_gh inode unless
718 int gfs2_lookupi(struct inode *dir, struct qstr *name, int is_root,
719 struct inode **inodep)
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;
728 struct gfs2_glock *gl;
733 if (!name->len || name->len > GFS2_FNAMESIZE)
734 return -ENAMETOOLONG;
736 if (gfs2_filecmp(name, ".", 1) ||
737 (gfs2_filecmp(name, "..", 2) && dir == sb->s_root->d_inode)) {
738 gfs2_inode_hold(dip);
743 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
748 error = gfs2_repermission(dip->i_vnode, MAY_EXEC, NULL);
753 error = gfs2_dir_search(dip, name, &inum, &type);
757 error = gfs2_glock_get(sdp, inum.no_addr, &gfs2_inode_glops,
762 error = gfs2_inode_get(gl, &inum, CREATE, &ipp);
764 gfs2_inode_min_init(ipp, type);
769 gfs2_glock_dq_uninit(&d_gh);
772 *inodep = gfs2_ip2v(ipp);
780 static int pick_formal_ino_1(struct gfs2_sbd *sdp, uint64_t *formal_ino)
782 struct gfs2_inode *ip = sdp->sd_ir_inode->u.generic_ip;
783 struct buffer_head *bh;
784 struct gfs2_inum_range ir;
787 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
790 mutex_lock(&sdp->sd_inum_mutex);
792 error = gfs2_meta_inode_buffer(ip, &bh);
794 mutex_unlock(&sdp->sd_inum_mutex);
799 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
802 *formal_ino = ir.ir_start++;
804 gfs2_trans_add_bh(ip->i_gl, bh, 1);
805 gfs2_inum_range_out(&ir,
806 bh->b_data + sizeof(struct gfs2_dinode));
808 mutex_unlock(&sdp->sd_inum_mutex);
815 mutex_unlock(&sdp->sd_inum_mutex);
821 static int pick_formal_ino_2(struct gfs2_sbd *sdp, uint64_t *formal_ino)
823 struct gfs2_inode *ip = sdp->sd_ir_inode->u.generic_ip;
824 struct gfs2_inode *m_ip = sdp->sd_inum_inode->u.generic_ip;
825 struct gfs2_holder gh;
826 struct buffer_head *bh;
827 struct gfs2_inum_range ir;
830 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
834 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
837 mutex_lock(&sdp->sd_inum_mutex);
839 error = gfs2_meta_inode_buffer(ip, &bh);
843 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
846 struct buffer_head *m_bh;
849 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
853 x = *(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode));
854 x = y = be64_to_cpu(x);
856 ir.ir_length = GFS2_INUM_QUANTUM;
857 x += GFS2_INUM_QUANTUM;
859 gfs2_consist_inode(m_ip);
861 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
862 *(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = x;
867 *formal_ino = ir.ir_start++;
870 gfs2_trans_add_bh(ip->i_gl, bh, 1);
871 gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
877 mutex_unlock(&sdp->sd_inum_mutex);
881 gfs2_glock_dq_uninit(&gh);
886 static int pick_formal_ino(struct gfs2_sbd *sdp, uint64_t *inum)
890 error = pick_formal_ino_1(sdp, inum);
894 error = pick_formal_ino_2(sdp, inum);
900 * create_ok - OK to create a new on-disk inode here?
901 * @dip: Directory in which dinode is to be created
902 * @name: Name of new dinode
908 static int create_ok(struct gfs2_inode *dip, struct qstr *name,
913 error = gfs2_repermission(dip->i_vnode, MAY_WRITE | MAY_EXEC, NULL);
917 /* Don't create entries in an unlinked directory */
918 if (!dip->i_di.di_nlink)
921 error = gfs2_dir_search(dip, name, NULL, NULL);
932 if (dip->i_di.di_entries == (uint32_t)-1)
934 if (S_ISDIR(mode) && dip->i_di.di_nlink == (uint32_t)-1)
940 static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
941 unsigned int *uid, unsigned int *gid)
943 if (dip->i_sbd->sd_args.ar_suiddir &&
944 (dip->i_di.di_mode & S_ISUID) &&
948 else if (dip->i_di.di_uid != current->fsuid)
950 *uid = dip->i_di.di_uid;
952 *uid = current->fsuid;
954 if (dip->i_di.di_mode & S_ISGID) {
957 *gid = dip->i_di.di_gid;
959 *gid = current->fsgid;
962 static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_unlinked *ul)
964 struct gfs2_sbd *sdp = dip->i_sbd;
969 dip->i_alloc.al_requested = RES_DINODE;
970 error = gfs2_inplace_reserve(dip);
974 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED +
979 ul->ul_ut.ut_inum.no_addr = gfs2_alloc_di(dip);
981 ul->ul_ut.ut_flags = GFS2_UTF_UNINIT;
982 error = gfs2_unlinked_ondisk_add(sdp, ul);
987 gfs2_inplace_release(dip);
996 * init_dinode - Fill in a new dinode structure
997 * @dip: the directory this inode is being created in
998 * @gl: The glock covering the new inode
999 * @inum: the inode number
1000 * @mode: the file permissions
1006 static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
1007 struct gfs2_inum *inum, unsigned int mode,
1008 unsigned int uid, unsigned int gid)
1010 struct gfs2_sbd *sdp = dip->i_sbd;
1011 struct gfs2_dinode *di;
1012 struct buffer_head *dibh;
1014 dibh = gfs2_meta_new(gl, inum->no_addr);
1015 gfs2_trans_add_bh(gl, dibh, 1);
1016 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
1017 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1018 di = (struct gfs2_dinode *)dibh->b_data;
1020 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
1021 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
1022 di->di_mode = cpu_to_be32(mode);
1023 di->di_uid = cpu_to_be32(uid);
1024 di->di_gid = cpu_to_be32(gid);
1025 di->di_nlink = cpu_to_be32(0);
1026 di->di_size = cpu_to_be64(0);
1027 di->di_blocks = cpu_to_be64(1);
1028 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds());
1029 di->di_major = di->di_minor = cpu_to_be32(0);
1030 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
1031 di->__pad[0] = di->__pad[1] = 0;
1032 di->di_flags = cpu_to_be32(0);
1034 if (S_ISREG(mode)) {
1035 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
1036 gfs2_tune_get(sdp, gt_new_files_jdata))
1037 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
1038 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
1039 gfs2_tune_get(sdp, gt_new_files_directio))
1040 di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
1041 } else if (S_ISDIR(mode)) {
1042 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
1043 GFS2_DIF_INHERIT_DIRECTIO);
1044 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
1045 GFS2_DIF_INHERIT_JDATA);
1049 di->di_height = cpu_to_be32(0);
1052 di->di_depth = cpu_to_be16(0);
1053 di->di_entries = cpu_to_be32(0);
1054 memset(&di->__pad4, 0, sizeof(di->__pad4));
1055 di->di_eattr = cpu_to_be64(0);
1056 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
1061 static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
1062 unsigned int mode, struct gfs2_unlinked *ul)
1064 struct gfs2_sbd *sdp = dip->i_sbd;
1065 unsigned int uid, gid;
1068 munge_mode_uid_gid(dip, &mode, &uid, &gid);
1070 gfs2_alloc_get(dip);
1072 error = gfs2_quota_lock(dip, uid, gid);
1076 error = gfs2_quota_check(dip, uid, gid);
1080 error = gfs2_trans_begin(sdp, RES_DINODE + RES_UNLINKED +
1085 ul->ul_ut.ut_flags = 0;
1086 error = gfs2_unlinked_ondisk_munge(sdp, ul);
1088 init_dinode(dip, gl, &ul->ul_ut.ut_inum,
1091 gfs2_quota_change(dip, +1, uid, gid);
1093 gfs2_trans_end(sdp);
1096 gfs2_quota_unlock(dip);
1099 gfs2_alloc_put(dip);
1104 static int link_dinode(struct gfs2_inode *dip, struct qstr *name,
1105 struct gfs2_inode *ip, struct gfs2_unlinked *ul)
1107 struct gfs2_sbd *sdp = dip->i_sbd;
1108 struct gfs2_alloc *al;
1110 struct buffer_head *dibh;
1113 al = gfs2_alloc_get(dip);
1115 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1119 error = gfs2_diradd_alloc_required(dip, name, &alloc_required);
1120 if (alloc_required) {
1121 error = gfs2_quota_check(dip, dip->i_di.di_uid,
1124 goto fail_quota_locks;
1126 al->al_requested = sdp->sd_max_dirres;
1128 error = gfs2_inplace_reserve(dip);
1130 goto fail_quota_locks;
1132 error = gfs2_trans_begin(sdp,
1133 sdp->sd_max_dirres +
1134 al->al_rgd->rd_ri.ri_length +
1135 2 * RES_DINODE + RES_UNLINKED +
1136 RES_STATFS + RES_QUOTA, 0);
1140 error = gfs2_trans_begin(sdp,
1145 goto fail_quota_locks;
1148 error = gfs2_dir_add(dip, name, &ip->i_num, IF2DT(ip->i_di.di_mode));
1150 goto fail_end_trans;
1152 error = gfs2_meta_inode_buffer(ip, &dibh);
1154 goto fail_end_trans;
1155 ip->i_di.di_nlink = 1;
1156 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1157 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1160 error = gfs2_unlinked_ondisk_rm(sdp, ul);
1162 goto fail_end_trans;
1167 gfs2_trans_end(sdp);
1170 if (dip->i_alloc.al_rgd)
1171 gfs2_inplace_release(dip);
1174 gfs2_quota_unlock(dip);
1177 gfs2_alloc_put(dip);
1183 * gfs2_createi - Create a new inode
1184 * @ghs: An array of two holders
1185 * @name: The name of the new file
1186 * @mode: the permissions on the new inode
1188 * @ghs[0] is an initialized holder for the directory
1189 * @ghs[1] is the holder for the inode lock
1191 * If the return value is not NULL, the glocks on both the directory and the new
1192 * file are held. A transaction has been started and an inplace reservation
1198 struct inode *gfs2_createi(struct gfs2_holder *ghs, struct qstr *name,
1201 struct inode *inode;
1202 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
1203 struct gfs2_sbd *sdp = dip->i_sbd;
1204 struct gfs2_unlinked *ul;
1205 struct gfs2_inode *ip;
1208 if (!name->len || name->len > GFS2_FNAMESIZE)
1209 return ERR_PTR(-ENAMETOOLONG);
1211 error = gfs2_unlinked_get(sdp, &ul);
1213 return ERR_PTR(error);
1215 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
1216 error = gfs2_glock_nq(ghs);
1220 error = create_ok(dip, name, mode);
1224 error = pick_formal_ino(sdp, &ul->ul_ut.ut_inum.no_formal_ino);
1228 error = alloc_dinode(dip, ul);
1232 if (ul->ul_ut.ut_inum.no_addr < dip->i_num.no_addr) {
1235 error = gfs2_glock_nq_num(sdp,
1236 ul->ul_ut.ut_inum.no_addr,
1238 LM_ST_EXCLUSIVE, GL_SKIP,
1241 gfs2_unlinked_put(sdp, ul);
1242 return ERR_PTR(error);
1245 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
1246 error = gfs2_glock_nq(ghs);
1248 gfs2_glock_dq_uninit(ghs + 1);
1249 gfs2_unlinked_put(sdp, ul);
1250 return ERR_PTR(error);
1253 error = create_ok(dip, name, mode);
1257 error = gfs2_glock_nq_num(sdp,
1258 ul->ul_ut.ut_inum.no_addr,
1260 LM_ST_EXCLUSIVE, GL_SKIP,
1266 error = make_dinode(dip, ghs[1].gh_gl, mode, ul);
1270 error = gfs2_inode_get(ghs[1].gh_gl, &ul->ul_ut.ut_inum, CREATE, &ip);
1274 error = gfs2_inode_refresh(ip);
1278 error = gfs2_acl_create(dip, ip);
1282 error = link_dinode(dip, name, ip, ul);
1286 gfs2_unlinked_put(sdp, ul);
1288 inode = gfs2_ip2v(ip);
1291 return ERR_PTR(-ENOMEM);
1298 gfs2_glock_dq_uninit(ghs + 1);
1304 gfs2_unlinked_put(sdp, ul);
1306 return ERR_PTR(error);
1310 * gfs2_unlinki - Unlink a file
1311 * @dip: The inode of the directory
1312 * @name: The name of the file to be unlinked
1313 * @ip: The inode of the file to be removed
1315 * Assumes Glocks on both dip and ip are held.
1320 int gfs2_unlinki(struct gfs2_inode *dip, struct qstr *name,
1321 struct gfs2_inode *ip, struct gfs2_unlinked *ul)
1323 struct gfs2_sbd *sdp = dip->i_sbd;
1326 error = gfs2_dir_del(dip, name);
1330 error = gfs2_change_nlink(ip, -1);
1334 /* If this inode is being unlinked from the directory structure,
1335 we need to mark that in the log so that it isn't lost during
1338 if (!ip->i_di.di_nlink) {
1339 ul->ul_ut.ut_inum = ip->i_num;
1340 error = gfs2_unlinked_ondisk_add(sdp, ul);
1342 set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
1349 * gfs2_rmdiri - Remove a directory
1350 * @dip: The parent directory of the directory to be removed
1351 * @name: The name of the directory to be removed
1352 * @ip: The GFS2 inode of the directory to be removed
1354 * Assumes Glocks on dip and ip are held
1359 int gfs2_rmdiri(struct gfs2_inode *dip, struct qstr *name,
1360 struct gfs2_inode *ip, struct gfs2_unlinked *ul)
1362 struct gfs2_sbd *sdp = dip->i_sbd;
1363 struct qstr dotname;
1366 if (ip->i_di.di_entries != 2) {
1367 if (gfs2_consist_inode(ip))
1368 gfs2_dinode_print(&ip->i_di);
1372 error = gfs2_dir_del(dip, name);
1376 error = gfs2_change_nlink(dip, -1);
1382 error = gfs2_dir_del(ip, &dotname);
1387 dotname.name = "..";
1388 error = gfs2_dir_del(ip, &dotname);
1392 error = gfs2_change_nlink(ip, -2);
1396 /* This inode is being unlinked from the directory structure and
1397 we need to mark that in the log so that it isn't lost during
1400 ul->ul_ut.ut_inum = ip->i_num;
1401 error = gfs2_unlinked_ondisk_add(sdp, ul);
1403 set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
1409 * gfs2_unlink_ok - check to see that a inode is still in a directory
1410 * @dip: the directory
1411 * @name: the name of the file
1414 * Assumes that the lock on (at least) @dip is held.
1416 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1419 int gfs2_unlink_ok(struct gfs2_inode *dip, struct qstr *name,
1420 struct gfs2_inode *ip)
1422 struct gfs2_inum inum;
1426 if (IS_IMMUTABLE(ip->i_vnode) || IS_APPEND(ip->i_vnode))
1429 if ((dip->i_di.di_mode & S_ISVTX) &&
1430 dip->i_di.di_uid != current->fsuid &&
1431 ip->i_di.di_uid != current->fsuid &&
1432 !capable(CAP_FOWNER))
1435 if (IS_APPEND(dip->i_vnode))
1438 error = gfs2_repermission(dip->i_vnode, MAY_WRITE | MAY_EXEC, NULL);
1442 error = gfs2_dir_search(dip, name, &inum, &type);
1446 if (!gfs2_inum_equal(&inum, &ip->i_num))
1449 if (IF2DT(ip->i_di.di_mode) != type) {
1450 gfs2_consist_inode(dip);
1458 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1462 * Follow @to back to the root and make sure we don't encounter @this
1463 * Assumes we already hold the rename lock.
1468 int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1470 struct inode *dir = to->i_vnode;
1471 struct super_block *sb = dir->i_sb;
1476 memset(&dotdot, 0, sizeof(struct qstr));
1483 if (dir == this->i_vnode) {
1487 if (dir == sb->s_root->d_inode) {
1492 error = gfs2_lookupi(dir, &dotdot, 1, &tmp);
1506 * gfs2_readlinki - return the contents of a symlink
1507 * @ip: the symlink's inode
1508 * @buf: a pointer to the buffer to be filled
1509 * @len: a pointer to the length of @buf
1511 * If @buf is too small, a piece of memory is kmalloc()ed and needs
1512 * to be freed by the caller.
1517 int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
1519 struct gfs2_holder i_gh;
1520 struct buffer_head *dibh;
1524 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
1525 error = gfs2_glock_nq_atime(&i_gh);
1527 gfs2_holder_uninit(&i_gh);
1531 if (!ip->i_di.di_size) {
1532 gfs2_consist_inode(ip);
1537 error = gfs2_meta_inode_buffer(ip, &dibh);
1541 x = ip->i_di.di_size + 1;
1543 *buf = kmalloc(x, GFP_KERNEL);
1550 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1557 gfs2_glock_dq_uninit(&i_gh);
1563 * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
1564 * conditionally update the inode's atime
1565 * @gh: the holder to acquire
1567 * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
1568 * Update if the difference between the current time and the inode's current
1569 * atime is greater than an interval specified at mount.
1574 int gfs2_glock_nq_atime(struct gfs2_holder *gh)
1576 struct gfs2_glock *gl = gh->gh_gl;
1577 struct gfs2_sbd *sdp = gl->gl_sbd;
1578 struct gfs2_inode *ip = gl->gl_object;
1579 int64_t curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum);
1584 if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
1585 gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
1586 gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
1589 state = gh->gh_state;
1590 flags = gh->gh_flags;
1592 error = gfs2_glock_nq(gh);
1596 if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
1597 (sdp->sd_vfs->s_flags & MS_RDONLY))
1600 curtime = get_seconds();
1601 if (curtime - ip->i_di.di_atime >= quantum) {
1603 gfs2_holder_reinit(LM_ST_EXCLUSIVE,
1604 gh->gh_flags & ~LM_FLAG_ANY,
1606 error = gfs2_glock_nq(gh);
1610 /* Verify that atime hasn't been updated while we were
1611 trying to get exclusive lock. */
1613 curtime = get_seconds();
1614 if (curtime - ip->i_di.di_atime >= quantum) {
1615 struct buffer_head *dibh;
1617 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1618 if (error == -EROFS)
1623 error = gfs2_meta_inode_buffer(ip, &dibh);
1625 goto fail_end_trans;
1627 ip->i_di.di_atime = curtime;
1629 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1630 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1633 gfs2_trans_end(sdp);
1636 /* If someone else has asked for the glock,
1637 unlock and let them have it. Then reacquire
1638 in the original state. */
1639 if (gfs2_glock_is_blocking(gl)) {
1641 gfs2_holder_reinit(state, flags, gh);
1642 return gfs2_glock_nq(gh);
1649 gfs2_trans_end(sdp);
1658 * glock_compare_atime - Compare two struct gfs2_glock structures for sort
1659 * @arg_a: the first structure
1660 * @arg_b: the second structure
1662 * Returns: 1 if A > B
1667 static int glock_compare_atime(const void *arg_a, const void *arg_b)
1669 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1670 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1671 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1672 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1675 if (a->ln_number > b->ln_number)
1677 else if (a->ln_number < b->ln_number)
1680 if (gh_a->gh_state == LM_ST_SHARED &&
1681 gh_b->gh_state == LM_ST_EXCLUSIVE)
1683 else if (gh_a->gh_state == LM_ST_SHARED &&
1684 (gh_b->gh_flags & GL_ATIME))
1692 * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an
1694 * @num_gh: the number of structures
1695 * @ghs: an array of struct gfs2_holder structures
1697 * Returns: 0 on success (all glocks acquired),
1698 * errno on failure (no glocks acquired)
1701 int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs)
1703 struct gfs2_holder **p;
1711 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1712 if (ghs->gh_flags & GL_ATIME)
1713 error = gfs2_glock_nq_atime(ghs);
1715 error = gfs2_glock_nq(ghs);
1719 p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1723 for (x = 0; x < num_gh; x++)
1726 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL);
1728 for (x = 0; x < num_gh; x++) {
1729 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1731 if (p[x]->gh_flags & GL_ATIME)
1732 error = gfs2_glock_nq_atime(p[x]);
1734 error = gfs2_glock_nq(p[x]);
1738 gfs2_glock_dq(p[x]);
1749 * gfs2_try_toss_vnode - See if we can toss a vnode from memory
1752 * Returns: 1 if the vnode was tossed
1755 void gfs2_try_toss_vnode(struct gfs2_inode *ip)
1757 struct inode *inode;
1759 inode = gfs2_ip2v_lookup(ip);
1763 d_prune_aliases(inode);
1765 if (S_ISDIR(ip->i_di.di_mode)) {
1766 struct list_head *head = &inode->i_dentry;
1767 struct dentry *d = NULL;
1769 spin_lock(&dcache_lock);
1770 if (list_empty(head))
1771 spin_unlock(&dcache_lock);
1773 d = list_entry(head->next, struct dentry, d_alias);
1775 spin_unlock(&dcache_lock);
1777 if (have_submounts(d))
1780 shrink_dcache_parent(d);
1782 d_prune_aliases(inode);
1793 __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1795 struct buffer_head *dibh;
1798 error = gfs2_meta_inode_buffer(ip, &dibh);
1800 error = inode_setattr(ip->i_vnode, attr);
1801 gfs2_assert_warn(ip->i_sbd, !error);
1802 gfs2_inode_attr_out(ip);
1804 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1805 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1812 * gfs2_setattr_simple -
1816 * Called with a reference on the vnode.
1821 int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1825 if (current->journal_info)
1826 return __gfs2_setattr_simple(ip, attr);
1828 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
1832 error = __gfs2_setattr_simple(ip, attr);
1834 gfs2_trans_end(ip->i_sbd);
1839 int gfs2_repermission(struct inode *inode, int mask, struct nameidata *nd)
1841 return permission(inode, mask, nd);