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 <linux/crc32.h>
19 #include <asm/semaphore.h>
22 #include "lm_interface.h"
33 #include "ops_address.h"
35 #include "ops_inode.h"
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
49 static void inode_attr_in(struct gfs2_inode *ip, struct inode *inode)
51 inode->i_ino = ip->i_num.no_formal_ino;
53 switch (ip->i_di.di_mode & S_IFMT) {
56 inode->i_rdev = MKDEV(ip->i_di.di_major, ip->i_di.di_minor);
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);
78 if (ip->i_di.di_flags & GFS2_DIF_IMMUTABLE)
79 inode->i_flags |= S_IMMUTABLE;
81 inode->i_flags &= ~S_IMMUTABLE;
83 if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY)
84 inode->i_flags |= S_APPEND;
86 inode->i_flags &= ~S_APPEND;
90 * gfs2_inode_attr_in - Copy attributes from the dinode into the VFS inode
91 * @ip: The GFS2 inode (with embedded disk inode data)
95 void gfs2_inode_attr_in(struct gfs2_inode *ip)
99 inode = gfs2_ip2v_lookup(ip);
101 inode_attr_in(ip, inode);
107 * gfs2_inode_attr_out - Copy attributes from VFS inode into the dinode
108 * @ip: The GFS2 inode
110 * Only copy out the attributes that we want the VFS layer
111 * to be able to modify.
114 void gfs2_inode_attr_out(struct gfs2_inode *ip)
116 struct inode *inode = ip->i_vnode;
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;
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
132 * Returns: A VFS inode, or NULL if none
135 struct inode *gfs2_ip2v_lookup(struct gfs2_inode *ip)
137 struct inode *inode = NULL;
139 gfs2_assert_warn(ip->i_sbd, test_bit(GIF_MIN_INIT, &ip->i_flags));
141 spin_lock(&ip->i_spin);
143 inode = igrab(ip->i_vnode);
144 spin_unlock(&ip->i_spin);
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
153 * Returns: A VFS inode, or NULL if no mem
156 struct inode *gfs2_ip2v(struct gfs2_inode *ip)
158 struct inode *inode, *tmp;
160 inode = gfs2_ip2v_lookup(ip);
164 tmp = new_inode(ip->i_sbd->sd_vfs);
168 inode_attr_in(ip, tmp);
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;
180 tmp->i_op = &gfs2_dev_iops;
181 init_special_inode(tmp, tmp->i_mode, tmp->i_rdev);
184 tmp->u.generic_ip = NULL;
187 spin_lock(&ip->i_spin);
190 inode = igrab(ip->i_vnode);
191 spin_unlock(&ip->i_spin);
204 inode->u.generic_ip = ip;
206 spin_unlock(&ip->i_spin);
208 insert_inode_hash(inode);
213 static int iget_test(struct inode *inode, void *opaque)
215 struct gfs2_inode *ip = inode->u.generic_ip;
216 struct gfs2_inum *inum = (struct gfs2_inum *)opaque;
218 if (ip && ip->i_num.no_addr == inum->no_addr)
224 struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum *inum)
226 return ilookup5(sb, (unsigned long)inum->no_formal_ino,
230 void gfs2_inode_min_init(struct gfs2_inode *ip, unsigned int type)
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);
239 * gfs2_inode_refresh - Refresh the incore copy of the dinode
240 * @ip: The GFS2 inode
245 int gfs2_inode_refresh(struct gfs2_inode *ip)
247 struct buffer_head *dibh;
250 error = gfs2_meta_inode_buffer(ip, &dibh);
254 if (gfs2_metatype_check(ip->i_sbd, dibh, GFS2_METATYPE_DI)) {
259 gfs2_dinode_in(&ip->i_di, dibh->b_data);
260 set_bit(GIF_MIN_INIT, &ip->i_flags);
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);
269 if (ip->i_num.no_formal_ino != ip->i_di.di_num.no_formal_ino)
272 ip->i_vn = ip->i_gl->gl_vn;
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
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)
292 struct gfs2_sbd *sdp = i_gl->gl_sbd;
293 struct gfs2_inode *ip;
296 ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
299 memset(ip, 0, sizeof(struct gfs2_inode));
303 atomic_set(&ip->i_count, 1);
305 ip->i_vn = i_gl->gl_vn - 1;
310 spin_lock_init(&ip->i_spin);
311 init_rwsem(&ip->i_rw_mutex);
313 ip->i_greedy = gfs2_tune_get(sdp, gt_greedy_default);
316 error = gfs2_glock_nq_init(io_gl,
317 io_state, GL_LOCAL_EXCL | GL_EXACT,
322 ip->i_iopen_gh.gh_owner = NULL;
324 spin_lock(&io_gl->gl_spin);
325 gfs2_glock_hold(i_gl);
326 io_gl->gl_object = i_gl;
327 spin_unlock(&io_gl->gl_spin);
329 gfs2_glock_hold(i_gl);
330 i_gl->gl_object = ip;
332 atomic_inc(&sdp->sd_inode_count);
339 gfs2_meta_cache_flush(ip);
340 kmem_cache_free(gfs2_inode_cachep, ip);
347 * gfs2_inode_get - Create or get a reference on an inode
348 * @i_gl: The glock covering the inode
349 * @inum: The inode number
351 * @ipp: pointer to put the returned inode in
356 int gfs2_inode_get(struct gfs2_glock *i_gl, const struct gfs2_inum *inum,
357 int create, struct gfs2_inode **ipp)
359 struct gfs2_sbd *sdp = i_gl->gl_sbd;
360 struct gfs2_glock *io_gl;
363 gfs2_glmutex_lock(i_gl);
365 *ipp = i_gl->gl_object;
368 if ((*ipp)->i_num.no_formal_ino != inum->no_formal_ino)
370 atomic_inc(&(*ipp)->i_count);
378 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops,
381 error = inode_create(i_gl, inum, io_gl, LM_ST_SHARED, ipp, 1);
382 gfs2_glock_put(io_gl);
386 gfs2_glmutex_unlock(i_gl);
391 void gfs2_inode_hold(struct gfs2_inode *ip)
393 gfs2_assert(ip->i_sbd, atomic_read(&ip->i_count) > 0);
394 atomic_inc(&ip->i_count);
397 void gfs2_inode_put(struct gfs2_inode *ip)
399 gfs2_assert(ip->i_sbd, atomic_read(&ip->i_count) > 0);
400 atomic_dec(&ip->i_count);
403 void gfs2_inode_destroy(struct gfs2_inode *ip, int unlock)
405 struct gfs2_sbd *sdp = ip->i_sbd;
406 struct gfs2_glock *i_gl = ip->i_gl;
408 gfs2_assert_warn(sdp, !atomic_read(&ip->i_count));
410 struct gfs2_glock *io_gl = ip->i_iopen_gh.gh_gl;
411 gfs2_assert(sdp, io_gl->gl_object == i_gl);
413 spin_lock(&io_gl->gl_spin);
414 io_gl->gl_object = NULL;
415 spin_unlock(&io_gl->gl_spin);
416 gfs2_glock_put(i_gl);
418 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
421 gfs2_meta_cache_flush(ip);
422 kmem_cache_free(gfs2_inode_cachep, ip);
424 i_gl->gl_object = NULL;
425 gfs2_glock_put(i_gl);
427 atomic_dec(&sdp->sd_inode_count);
430 static int dinode_dealloc(struct gfs2_inode *ip, struct gfs2_unlinked *ul)
432 struct gfs2_sbd *sdp = ip->i_sbd;
433 struct gfs2_alloc *al;
434 struct gfs2_rgrpd *rgd;
437 if (ip->i_di.di_blocks != 1) {
438 if (gfs2_consist_inode(ip))
439 gfs2_dinode_print(&ip->i_di);
443 al = gfs2_alloc_get(ip);
445 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
449 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
453 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
455 gfs2_consist_inode(ip);
457 goto out_rindex_relse;
460 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
463 goto out_rindex_relse;
465 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED +
466 RES_STATFS + RES_QUOTA, 1);
470 gfs2_trans_add_gl(ip->i_gl);
472 gfs2_free_di(rgd, ip);
474 error = gfs2_unlinked_ondisk_rm(sdp, ul);
477 clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
480 gfs2_glock_dq_uninit(&al->al_rgd_gh);
483 gfs2_glock_dq_uninit(&al->al_ri_gh);
486 gfs2_quota_unhold(ip);
495 * inode_dealloc - Deallocate all on-disk blocks for an inode (dinode)
496 * @sdp: the filesystem
497 * @inum: the inode number to deallocate
498 * @io_gh: a holder for the iopen glock for this inode
500 * N.B. When we enter this we already hold the iopen glock and getting
501 * the glock for the inode means that we are grabbing the locks in the
502 * "wrong" order so we must only so a try lock operation and fail if we
503 * don't get the lock. Thats ok, since if we fail it means someone else
504 * is using the inode still and thus we shouldn't be deallocating it
510 static int inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul,
511 struct gfs2_holder *io_gh)
513 struct gfs2_inode *ip;
514 struct gfs2_holder i_gh;
517 error = gfs2_glock_nq_num(sdp, ul->ul_ut.ut_inum.no_addr,
518 &gfs2_inode_glops, LM_ST_EXCLUSIVE,
519 LM_FLAG_TRY_1CB, &i_gh);
524 return 1; /* or back off and relock in different order? */
529 gfs2_assert_warn(sdp, !i_gh.gh_gl->gl_object);
530 error = inode_create(i_gh.gh_gl, &ul->ul_ut.ut_inum, io_gh->gh_gl,
531 LM_ST_EXCLUSIVE, &ip, 0);
536 error = gfs2_inode_refresh(ip);
540 if (ip->i_di.di_nlink) {
541 if (gfs2_consist_inode(ip))
542 gfs2_dinode_print(&ip->i_di);
547 if (S_ISDIR(ip->i_di.di_mode) &&
548 (ip->i_di.di_flags & GFS2_DIF_EXHASH)) {
549 error = gfs2_dir_exhash_dealloc(ip);
554 if (ip->i_di.di_eattr) {
555 error = gfs2_ea_dealloc(ip);
560 if (!gfs2_is_stuffed(ip)) {
561 error = gfs2_file_dealloc(ip);
566 error = dinode_dealloc(ip, ul);
571 gfs2_glmutex_lock(i_gh.gh_gl);
573 gfs2_inode_destroy(ip, 0);
574 gfs2_glmutex_unlock(i_gh.gh_gl);
577 gfs2_glock_dq_uninit(&i_gh);
583 * try_inode_dealloc - Try to deallocate an inode and all its blocks
584 * @sdp: the filesystem
586 * Returns: 0 on success, -errno on error, 1 on busy (inode open)
589 static int try_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
592 struct gfs2_holder iogh;
594 gfs2_try_toss_inode(sdp, &ul->ul_ut.ut_inum);
595 error = gfs2_glock_nq_num(sdp, ul->ul_ut.ut_inum.no_addr,
596 &gfs2_iopen_glops, LM_ST_EXCLUSIVE,
597 LM_FLAG_TRY_1CB, &iogh);
607 error = inode_dealloc(sdp, ul, &iogh);
608 gfs2_glock_dq_uninit(&iogh);
613 static int inode_dealloc_uninit(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
615 struct gfs2_rgrpd *rgd;
616 struct gfs2_holder ri_gh, rgd_gh;
619 error = gfs2_rindex_hold(sdp, &ri_gh);
623 rgd = gfs2_blk2rgrpd(sdp, ul->ul_ut.ut_inum.no_addr);
630 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rgd_gh);
634 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED + RES_STATFS, 0);
638 gfs2_free_uninit_di(rgd, ul->ul_ut.ut_inum.no_addr);
639 gfs2_unlinked_ondisk_rm(sdp, ul);
644 gfs2_glock_dq_uninit(&rgd_gh);
646 gfs2_glock_dq_uninit(&ri_gh);
651 int gfs2_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
653 if (ul->ul_ut.ut_flags & GFS2_UTF_UNINIT)
654 return inode_dealloc_uninit(sdp, ul);
656 return try_inode_dealloc(sdp, ul);
660 * gfs2_change_nlink - Change nlink count on inode
661 * @ip: The GFS2 inode
662 * @diff: The change in the nlink count required
667 int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
669 struct buffer_head *dibh;
673 nlink = ip->i_di.di_nlink + diff;
675 /* If we are reducing the nlink count, but the new value ends up being
676 bigger than the old one, we must have underflowed. */
677 if (diff < 0 && nlink > ip->i_di.di_nlink) {
678 if (gfs2_consist_inode(ip))
679 gfs2_dinode_print(&ip->i_di);
683 error = gfs2_meta_inode_buffer(ip, &dibh);
687 ip->i_di.di_nlink = nlink;
688 ip->i_di.di_ctime = get_seconds();
690 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
691 gfs2_dinode_out(&ip->i_di, dibh->b_data);
697 struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
700 gfs2_str2qstr(&qstr, name);
701 return gfs2_lookupi(dip, &qstr, 1, NULL);
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 struct inode *gfs2_lookupi(struct inode *dir, struct qstr *name, int is_root,
719 struct nameidata *nd)
722 struct super_block *sb = dir->i_sb;
723 struct gfs2_inode *ipp;
724 struct gfs2_inode *dip = dir->u.generic_ip;
725 struct gfs2_sbd *sdp = dip->i_sbd;
726 struct gfs2_holder d_gh;
727 struct gfs2_inum inum;
729 struct gfs2_glock *gl;
731 struct inode *inode = NULL;
733 if (!name->len || name->len > GFS2_FNAMESIZE)
734 return ERR_PTR(-ENAMETOOLONG);
736 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
737 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
738 dir == sb->s_root->d_inode)) {
739 gfs2_inode_hold(dip);
744 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
746 return ERR_PTR(error);
749 error = gfs2_repermission(dip->i_vnode, MAY_EXEC, NULL);
754 error = gfs2_dir_search(dir, name, &inum, &type);
758 error = gfs2_glock_get(sdp, inum.no_addr, &gfs2_inode_glops,
763 error = gfs2_inode_get(gl, &inum, CREATE, &ipp);
765 gfs2_inode_min_init(ipp, type);
770 gfs2_glock_dq_uninit(&d_gh);
772 if (error == -ENOENT)
775 inode = gfs2_ip2v(ipp);
778 return ERR_PTR(-ENOMEM);
781 return ERR_PTR(error);
784 static int pick_formal_ino_1(struct gfs2_sbd *sdp, uint64_t *formal_ino)
786 struct gfs2_inode *ip = sdp->sd_ir_inode->u.generic_ip;
787 struct buffer_head *bh;
788 struct gfs2_inum_range ir;
791 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
794 mutex_lock(&sdp->sd_inum_mutex);
796 error = gfs2_meta_inode_buffer(ip, &bh);
798 mutex_unlock(&sdp->sd_inum_mutex);
803 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
806 *formal_ino = ir.ir_start++;
808 gfs2_trans_add_bh(ip->i_gl, bh, 1);
809 gfs2_inum_range_out(&ir,
810 bh->b_data + sizeof(struct gfs2_dinode));
812 mutex_unlock(&sdp->sd_inum_mutex);
819 mutex_unlock(&sdp->sd_inum_mutex);
825 static int pick_formal_ino_2(struct gfs2_sbd *sdp, uint64_t *formal_ino)
827 struct gfs2_inode *ip = sdp->sd_ir_inode->u.generic_ip;
828 struct gfs2_inode *m_ip = sdp->sd_inum_inode->u.generic_ip;
829 struct gfs2_holder gh;
830 struct buffer_head *bh;
831 struct gfs2_inum_range ir;
834 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
838 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
841 mutex_lock(&sdp->sd_inum_mutex);
843 error = gfs2_meta_inode_buffer(ip, &bh);
847 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
850 struct buffer_head *m_bh;
853 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
857 x = *(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode));
858 x = y = be64_to_cpu(x);
860 ir.ir_length = GFS2_INUM_QUANTUM;
861 x += GFS2_INUM_QUANTUM;
863 gfs2_consist_inode(m_ip);
865 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
866 *(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = x;
871 *formal_ino = ir.ir_start++;
874 gfs2_trans_add_bh(ip->i_gl, bh, 1);
875 gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
881 mutex_unlock(&sdp->sd_inum_mutex);
885 gfs2_glock_dq_uninit(&gh);
890 static int pick_formal_ino(struct gfs2_sbd *sdp, uint64_t *inum)
894 error = pick_formal_ino_1(sdp, inum);
898 error = pick_formal_ino_2(sdp, inum);
904 * create_ok - OK to create a new on-disk inode here?
905 * @dip: Directory in which dinode is to be created
906 * @name: Name of new dinode
912 static int create_ok(struct gfs2_inode *dip, struct qstr *name,
917 error = gfs2_repermission(dip->i_vnode, MAY_WRITE | MAY_EXEC, NULL);
921 /* Don't create entries in an unlinked directory */
922 if (!dip->i_di.di_nlink)
925 error = gfs2_dir_search(dip->i_vnode, name, NULL, NULL);
936 if (dip->i_di.di_entries == (uint32_t)-1)
938 if (S_ISDIR(mode) && dip->i_di.di_nlink == (uint32_t)-1)
944 static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
945 unsigned int *uid, unsigned int *gid)
947 if (dip->i_sbd->sd_args.ar_suiddir &&
948 (dip->i_di.di_mode & S_ISUID) &&
952 else if (dip->i_di.di_uid != current->fsuid)
954 *uid = dip->i_di.di_uid;
956 *uid = current->fsuid;
958 if (dip->i_di.di_mode & S_ISGID) {
961 *gid = dip->i_di.di_gid;
963 *gid = current->fsgid;
966 static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_unlinked *ul)
968 struct gfs2_sbd *sdp = dip->i_sbd;
973 dip->i_alloc.al_requested = RES_DINODE;
974 error = gfs2_inplace_reserve(dip);
978 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED +
983 ul->ul_ut.ut_inum.no_addr = gfs2_alloc_di(dip);
985 ul->ul_ut.ut_flags = GFS2_UTF_UNINIT;
986 error = gfs2_unlinked_ondisk_add(sdp, ul);
991 gfs2_inplace_release(dip);
1000 * init_dinode - Fill in a new dinode structure
1001 * @dip: the directory this inode is being created in
1002 * @gl: The glock covering the new inode
1003 * @inum: the inode number
1004 * @mode: the file permissions
1010 static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
1011 struct gfs2_inum *inum, unsigned int mode,
1012 unsigned int uid, unsigned int gid)
1014 struct gfs2_sbd *sdp = dip->i_sbd;
1015 struct gfs2_dinode *di;
1016 struct buffer_head *dibh;
1018 dibh = gfs2_meta_new(gl, inum->no_addr);
1019 gfs2_trans_add_bh(gl, dibh, 1);
1020 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
1021 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1022 di = (struct gfs2_dinode *)dibh->b_data;
1024 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
1025 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
1026 di->di_mode = cpu_to_be32(mode);
1027 di->di_uid = cpu_to_be32(uid);
1028 di->di_gid = cpu_to_be32(gid);
1029 di->di_nlink = cpu_to_be32(0);
1030 di->di_size = cpu_to_be64(0);
1031 di->di_blocks = cpu_to_be64(1);
1032 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(get_seconds());
1033 di->di_major = di->di_minor = cpu_to_be32(0);
1034 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
1035 di->__pad[0] = di->__pad[1] = 0;
1036 di->di_flags = cpu_to_be32(0);
1038 if (S_ISREG(mode)) {
1039 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
1040 gfs2_tune_get(sdp, gt_new_files_jdata))
1041 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
1042 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
1043 gfs2_tune_get(sdp, gt_new_files_directio))
1044 di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
1045 } else if (S_ISDIR(mode)) {
1046 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
1047 GFS2_DIF_INHERIT_DIRECTIO);
1048 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
1049 GFS2_DIF_INHERIT_JDATA);
1053 di->di_height = cpu_to_be32(0);
1056 di->di_depth = cpu_to_be16(0);
1057 di->di_entries = cpu_to_be32(0);
1058 memset(&di->__pad4, 0, sizeof(di->__pad4));
1059 di->di_eattr = cpu_to_be64(0);
1060 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
1065 static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
1066 unsigned int mode, struct gfs2_unlinked *ul)
1068 struct gfs2_sbd *sdp = dip->i_sbd;
1069 unsigned int uid, gid;
1072 munge_mode_uid_gid(dip, &mode, &uid, &gid);
1074 gfs2_alloc_get(dip);
1076 error = gfs2_quota_lock(dip, uid, gid);
1080 error = gfs2_quota_check(dip, uid, gid);
1084 error = gfs2_trans_begin(sdp, RES_DINODE + RES_UNLINKED +
1089 ul->ul_ut.ut_flags = 0;
1090 error = gfs2_unlinked_ondisk_munge(sdp, ul);
1092 init_dinode(dip, gl, &ul->ul_ut.ut_inum,
1095 gfs2_quota_change(dip, +1, uid, gid);
1097 gfs2_trans_end(sdp);
1100 gfs2_quota_unlock(dip);
1103 gfs2_alloc_put(dip);
1108 static int link_dinode(struct gfs2_inode *dip, struct qstr *name,
1109 struct gfs2_inode *ip, struct gfs2_unlinked *ul)
1111 struct gfs2_sbd *sdp = dip->i_sbd;
1112 struct gfs2_alloc *al;
1114 struct buffer_head *dibh;
1117 al = gfs2_alloc_get(dip);
1119 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1123 error = alloc_required = gfs2_diradd_alloc_required(dip->i_vnode, name);
1124 if (alloc_required < 0)
1126 if (alloc_required) {
1127 error = gfs2_quota_check(dip, dip->i_di.di_uid,
1130 goto fail_quota_locks;
1132 al->al_requested = sdp->sd_max_dirres;
1134 error = gfs2_inplace_reserve(dip);
1136 goto fail_quota_locks;
1138 error = gfs2_trans_begin(sdp,
1139 sdp->sd_max_dirres +
1140 al->al_rgd->rd_ri.ri_length +
1141 2 * RES_DINODE + RES_UNLINKED +
1142 RES_STATFS + RES_QUOTA, 0);
1146 error = gfs2_trans_begin(sdp,
1151 goto fail_quota_locks;
1154 error = gfs2_dir_add(dip->i_vnode, name, &ip->i_num, IF2DT(ip->i_di.di_mode));
1156 goto fail_end_trans;
1158 error = gfs2_meta_inode_buffer(ip, &dibh);
1160 goto fail_end_trans;
1161 ip->i_di.di_nlink = 1;
1162 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1163 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1166 error = gfs2_unlinked_ondisk_rm(sdp, ul);
1168 goto fail_end_trans;
1173 gfs2_trans_end(sdp);
1176 if (dip->i_alloc.al_rgd)
1177 gfs2_inplace_release(dip);
1180 gfs2_quota_unlock(dip);
1183 gfs2_alloc_put(dip);
1189 * gfs2_createi - Create a new inode
1190 * @ghs: An array of two holders
1191 * @name: The name of the new file
1192 * @mode: the permissions on the new inode
1194 * @ghs[0] is an initialized holder for the directory
1195 * @ghs[1] is the holder for the inode lock
1197 * If the return value is not NULL, the glocks on both the directory and the new
1198 * file are held. A transaction has been started and an inplace reservation
1204 struct inode *gfs2_createi(struct gfs2_holder *ghs, struct qstr *name,
1207 struct inode *inode;
1208 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
1209 struct gfs2_sbd *sdp = dip->i_sbd;
1210 struct gfs2_unlinked *ul;
1211 struct gfs2_inode *ip;
1214 if (!name->len || name->len > GFS2_FNAMESIZE)
1215 return ERR_PTR(-ENAMETOOLONG);
1217 error = gfs2_unlinked_get(sdp, &ul);
1219 return ERR_PTR(error);
1221 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
1222 error = gfs2_glock_nq(ghs);
1226 error = create_ok(dip, name, mode);
1230 error = pick_formal_ino(sdp, &ul->ul_ut.ut_inum.no_formal_ino);
1234 error = alloc_dinode(dip, ul);
1238 if (ul->ul_ut.ut_inum.no_addr < dip->i_num.no_addr) {
1241 error = gfs2_glock_nq_num(sdp,
1242 ul->ul_ut.ut_inum.no_addr,
1244 LM_ST_EXCLUSIVE, GL_SKIP,
1247 gfs2_unlinked_put(sdp, ul);
1248 return ERR_PTR(error);
1251 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
1252 error = gfs2_glock_nq(ghs);
1254 gfs2_glock_dq_uninit(ghs + 1);
1255 gfs2_unlinked_put(sdp, ul);
1256 return ERR_PTR(error);
1259 error = create_ok(dip, name, mode);
1263 error = gfs2_glock_nq_num(sdp,
1264 ul->ul_ut.ut_inum.no_addr,
1266 LM_ST_EXCLUSIVE, GL_SKIP,
1272 error = make_dinode(dip, ghs[1].gh_gl, mode, ul);
1276 error = gfs2_inode_get(ghs[1].gh_gl, &ul->ul_ut.ut_inum, CREATE, &ip);
1280 error = gfs2_inode_refresh(ip);
1284 error = gfs2_acl_create(dip, ip);
1288 error = link_dinode(dip, name, ip, ul);
1292 gfs2_unlinked_put(sdp, ul);
1294 inode = gfs2_ip2v(ip);
1297 return ERR_PTR(-ENOMEM);
1304 gfs2_glock_dq_uninit(ghs + 1);
1310 gfs2_unlinked_put(sdp, ul);
1312 return ERR_PTR(error);
1316 * gfs2_unlinki - Unlink a file
1317 * @dip: The inode of the directory
1318 * @name: The name of the file to be unlinked
1319 * @ip: The inode of the file to be removed
1321 * Assumes Glocks on both dip and ip are held.
1326 int gfs2_unlinki(struct gfs2_inode *dip, struct qstr *name,
1327 struct gfs2_inode *ip, struct gfs2_unlinked *ul)
1329 struct gfs2_sbd *sdp = dip->i_sbd;
1332 error = gfs2_dir_del(dip, name);
1336 error = gfs2_change_nlink(ip, -1);
1340 /* If this inode is being unlinked from the directory structure,
1341 we need to mark that in the log so that it isn't lost during
1344 if (!ip->i_di.di_nlink) {
1345 ul->ul_ut.ut_inum = ip->i_num;
1346 error = gfs2_unlinked_ondisk_add(sdp, ul);
1348 set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
1355 * gfs2_rmdiri - Remove a directory
1356 * @dip: The parent directory of the directory to be removed
1357 * @name: The name of the directory to be removed
1358 * @ip: The GFS2 inode of the directory to be removed
1360 * Assumes Glocks on dip and ip are held
1365 int gfs2_rmdiri(struct gfs2_inode *dip, struct qstr *name,
1366 struct gfs2_inode *ip, struct gfs2_unlinked *ul)
1368 struct gfs2_sbd *sdp = dip->i_sbd;
1369 struct qstr dotname;
1372 if (ip->i_di.di_entries != 2) {
1373 if (gfs2_consist_inode(ip))
1374 gfs2_dinode_print(&ip->i_di);
1378 error = gfs2_dir_del(dip, name);
1382 error = gfs2_change_nlink(dip, -1);
1386 gfs2_str2qstr(&dotname, ".");
1387 error = gfs2_dir_del(ip, &dotname);
1392 dotname.name = "..";
1393 dotname.hash = gfs2_disk_hash(dotname.name, dotname.len);
1394 error = gfs2_dir_del(ip, &dotname);
1398 error = gfs2_change_nlink(ip, -2);
1402 /* This inode is being unlinked from the directory structure and
1403 we need to mark that in the log so that it isn't lost during
1406 ul->ul_ut.ut_inum = ip->i_num;
1407 error = gfs2_unlinked_ondisk_add(sdp, ul);
1409 set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
1415 * gfs2_unlink_ok - check to see that a inode is still in a directory
1416 * @dip: the directory
1417 * @name: the name of the file
1420 * Assumes that the lock on (at least) @dip is held.
1422 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1425 int gfs2_unlink_ok(struct gfs2_inode *dip, struct qstr *name,
1426 struct gfs2_inode *ip)
1428 struct gfs2_inum inum;
1432 if (IS_IMMUTABLE(ip->i_vnode) || IS_APPEND(ip->i_vnode))
1435 if ((dip->i_di.di_mode & S_ISVTX) &&
1436 dip->i_di.di_uid != current->fsuid &&
1437 ip->i_di.di_uid != current->fsuid &&
1438 !capable(CAP_FOWNER))
1441 if (IS_APPEND(dip->i_vnode))
1444 error = gfs2_repermission(dip->i_vnode, MAY_WRITE | MAY_EXEC, NULL);
1448 error = gfs2_dir_search(dip->i_vnode, name, &inum, &type);
1452 if (!gfs2_inum_equal(&inum, &ip->i_num))
1455 if (IF2DT(ip->i_di.di_mode) != type) {
1456 gfs2_consist_inode(dip);
1464 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1468 * Follow @to back to the root and make sure we don't encounter @this
1469 * Assumes we already hold the rename lock.
1474 int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1476 struct inode *dir = to->i_vnode;
1477 struct super_block *sb = dir->i_sb;
1482 gfs2_str2qstr(&dotdot, "..");
1487 if (dir == this->i_vnode) {
1491 if (dir == sb->s_root->d_inode) {
1496 tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
1498 error = PTR_ERR(tmp);
1512 * gfs2_readlinki - return the contents of a symlink
1513 * @ip: the symlink's inode
1514 * @buf: a pointer to the buffer to be filled
1515 * @len: a pointer to the length of @buf
1517 * If @buf is too small, a piece of memory is kmalloc()ed and needs
1518 * to be freed by the caller.
1523 int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
1525 struct gfs2_holder i_gh;
1526 struct buffer_head *dibh;
1530 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
1531 error = gfs2_glock_nq_atime(&i_gh);
1533 gfs2_holder_uninit(&i_gh);
1537 if (!ip->i_di.di_size) {
1538 gfs2_consist_inode(ip);
1543 error = gfs2_meta_inode_buffer(ip, &dibh);
1547 x = ip->i_di.di_size + 1;
1549 *buf = kmalloc(x, GFP_KERNEL);
1556 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1563 gfs2_glock_dq_uninit(&i_gh);
1569 * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
1570 * conditionally update the inode's atime
1571 * @gh: the holder to acquire
1573 * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
1574 * Update if the difference between the current time and the inode's current
1575 * atime is greater than an interval specified at mount.
1580 int gfs2_glock_nq_atime(struct gfs2_holder *gh)
1582 struct gfs2_glock *gl = gh->gh_gl;
1583 struct gfs2_sbd *sdp = gl->gl_sbd;
1584 struct gfs2_inode *ip = gl->gl_object;
1585 int64_t curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum);
1590 if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
1591 gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
1592 gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
1595 state = gh->gh_state;
1596 flags = gh->gh_flags;
1598 error = gfs2_glock_nq(gh);
1602 if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
1603 (sdp->sd_vfs->s_flags & MS_RDONLY))
1606 curtime = get_seconds();
1607 if (curtime - ip->i_di.di_atime >= quantum) {
1609 gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
1611 error = gfs2_glock_nq(gh);
1615 /* Verify that atime hasn't been updated while we were
1616 trying to get exclusive lock. */
1618 curtime = get_seconds();
1619 if (curtime - ip->i_di.di_atime >= quantum) {
1620 struct buffer_head *dibh;
1622 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1623 if (error == -EROFS)
1628 error = gfs2_meta_inode_buffer(ip, &dibh);
1630 goto fail_end_trans;
1632 ip->i_di.di_atime = curtime;
1634 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1635 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1638 gfs2_trans_end(sdp);
1641 /* If someone else has asked for the glock,
1642 unlock and let them have it. Then reacquire
1643 in the original state. */
1644 if (gfs2_glock_is_blocking(gl)) {
1646 gfs2_holder_reinit(state, flags, gh);
1647 return gfs2_glock_nq(gh);
1654 gfs2_trans_end(sdp);
1663 * glock_compare_atime - Compare two struct gfs2_glock structures for sort
1664 * @arg_a: the first structure
1665 * @arg_b: the second structure
1667 * Returns: 1 if A > B
1672 static int glock_compare_atime(const void *arg_a, const void *arg_b)
1674 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1675 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1676 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1677 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1680 if (a->ln_number > b->ln_number)
1682 else if (a->ln_number < b->ln_number)
1685 if (gh_a->gh_state == LM_ST_SHARED &&
1686 gh_b->gh_state == LM_ST_EXCLUSIVE)
1688 else if (gh_a->gh_state == LM_ST_SHARED &&
1689 (gh_b->gh_flags & GL_ATIME))
1697 * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an
1699 * @num_gh: the number of structures
1700 * @ghs: an array of struct gfs2_holder structures
1702 * Returns: 0 on success (all glocks acquired),
1703 * errno on failure (no glocks acquired)
1706 int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs)
1708 struct gfs2_holder **p;
1716 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1717 if (ghs->gh_flags & GL_ATIME)
1718 error = gfs2_glock_nq_atime(ghs);
1720 error = gfs2_glock_nq(ghs);
1724 p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1728 for (x = 0; x < num_gh; x++)
1731 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL);
1733 for (x = 0; x < num_gh; x++) {
1734 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1736 if (p[x]->gh_flags & GL_ATIME)
1737 error = gfs2_glock_nq_atime(p[x]);
1739 error = gfs2_glock_nq(p[x]);
1743 gfs2_glock_dq(p[x]);
1754 * gfs2_try_toss_vnode - See if we can toss a vnode from memory
1757 * Returns: 1 if the vnode was tossed
1760 void gfs2_try_toss_vnode(struct gfs2_inode *ip)
1762 struct inode *inode;
1764 inode = gfs2_ip2v_lookup(ip);
1768 d_prune_aliases(inode);
1770 if (S_ISDIR(ip->i_di.di_mode)) {
1771 struct list_head *head = &inode->i_dentry;
1772 struct dentry *d = NULL;
1774 spin_lock(&dcache_lock);
1775 if (list_empty(head))
1776 spin_unlock(&dcache_lock);
1778 d = list_entry(head->next, struct dentry, d_alias);
1780 spin_unlock(&dcache_lock);
1782 if (have_submounts(d))
1785 shrink_dcache_parent(d);
1787 d_prune_aliases(inode);
1798 __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1800 struct buffer_head *dibh;
1803 error = gfs2_meta_inode_buffer(ip, &dibh);
1805 error = inode_setattr(ip->i_vnode, attr);
1806 gfs2_assert_warn(ip->i_sbd, !error);
1807 gfs2_inode_attr_out(ip);
1809 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1810 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1817 * gfs2_setattr_simple -
1821 * Called with a reference on the vnode.
1826 int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1830 if (current->journal_info)
1831 return __gfs2_setattr_simple(ip, attr);
1833 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
1837 error = __gfs2_setattr_simple(ip, attr);
1839 gfs2_trans_end(ip->i_sbd);
1844 int gfs2_repermission(struct inode *inode, int mask, struct nameidata *nd)
1846 return permission(inode, mask, nd);