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 <asm/semaphore.h>
29 #include "ops_address.h"
31 #include "ops_inode.h"
38 * inode_attr_in - Copy attributes from the dinode into the VFS inode
39 * @ip: The GFS2 inode (with embedded disk inode data)
40 * @inode: The Linux VFS inode
44 static void inode_attr_in(struct gfs2_inode *ip, struct inode *inode)
46 inode->i_ino = ip->i_num.no_formal_ino;
48 switch (ip->i_di.di_mode & S_IFMT) {
51 inode->i_rdev = MKDEV(ip->i_di.di_major, ip->i_di.di_minor);
58 inode->i_mode = ip->i_di.di_mode;
59 inode->i_nlink = ip->i_di.di_nlink;
60 inode->i_uid = ip->i_di.di_uid;
61 inode->i_gid = ip->i_di.di_gid;
62 i_size_write(inode, ip->i_di.di_size);
63 inode->i_atime.tv_sec = ip->i_di.di_atime;
64 inode->i_mtime.tv_sec = ip->i_di.di_mtime;
65 inode->i_ctime.tv_sec = ip->i_di.di_ctime;
66 inode->i_atime.tv_nsec = 0;
67 inode->i_mtime.tv_nsec = 0;
68 inode->i_ctime.tv_nsec = 0;
69 inode->i_blksize = PAGE_SIZE;
70 inode->i_blocks = ip->i_di.di_blocks <<
71 (ip->i_sbd->sd_sb.sb_bsize_shift - GFS2_BASIC_BLOCK_SHIFT);
73 if (ip->i_di.di_flags & GFS2_DIF_IMMUTABLE)
74 inode->i_flags |= S_IMMUTABLE;
76 inode->i_flags &= ~S_IMMUTABLE;
78 if (ip->i_di.di_flags & GFS2_DIF_APPENDONLY)
79 inode->i_flags |= S_APPEND;
81 inode->i_flags &= ~S_APPEND;
85 * gfs2_inode_attr_in - Copy attributes from the dinode into the VFS inode
86 * @ip: The GFS2 inode (with embedded disk inode data)
90 void gfs2_inode_attr_in(struct gfs2_inode *ip)
94 inode = gfs2_ip2v_lookup(ip);
96 inode_attr_in(ip, inode);
102 * gfs2_inode_attr_out - Copy attributes from VFS inode into the dinode
103 * @ip: The GFS2 inode
105 * Only copy out the attributes that we want the VFS layer
106 * to be able to modify.
109 void gfs2_inode_attr_out(struct gfs2_inode *ip)
111 struct inode *inode = ip->i_vnode;
113 gfs2_assert_withdraw(ip->i_sbd,
114 (ip->i_di.di_mode & S_IFMT) == (inode->i_mode & S_IFMT));
115 ip->i_di.di_mode = inode->i_mode;
116 ip->i_di.di_uid = inode->i_uid;
117 ip->i_di.di_gid = inode->i_gid;
118 ip->i_di.di_atime = inode->i_atime.tv_sec;
119 ip->i_di.di_mtime = inode->i_mtime.tv_sec;
120 ip->i_di.di_ctime = inode->i_ctime.tv_sec;
124 * gfs2_ip2v_lookup - Get the struct inode for a struct gfs2_inode
125 * @ip: the struct gfs2_inode to get the struct inode for
127 * Returns: A VFS inode, or NULL if none
130 struct inode *gfs2_ip2v_lookup(struct gfs2_inode *ip)
132 struct inode *inode = NULL;
134 gfs2_assert_warn(ip->i_sbd, test_bit(GIF_MIN_INIT, &ip->i_flags));
136 spin_lock(&ip->i_spin);
138 inode = igrab(ip->i_vnode);
139 spin_unlock(&ip->i_spin);
145 * gfs2_ip2v - Get/Create a struct inode for a struct gfs2_inode
146 * @ip: the struct gfs2_inode to get the struct inode for
148 * Returns: A VFS inode, or NULL if no mem
151 struct inode *gfs2_ip2v(struct gfs2_inode *ip)
153 struct inode *inode, *tmp;
155 inode = gfs2_ip2v_lookup(ip);
159 tmp = new_inode(ip->i_sbd->sd_vfs);
163 inode_attr_in(ip, tmp);
165 if (S_ISREG(ip->i_di.di_mode)) {
166 tmp->i_op = &gfs2_file_iops;
167 tmp->i_fop = &gfs2_file_fops;
168 tmp->i_mapping->a_ops = &gfs2_file_aops;
169 } else if (S_ISDIR(ip->i_di.di_mode)) {
170 tmp->i_op = &gfs2_dir_iops;
171 tmp->i_fop = &gfs2_dir_fops;
172 } else if (S_ISLNK(ip->i_di.di_mode)) {
173 tmp->i_op = &gfs2_symlink_iops;
175 tmp->i_op = &gfs2_dev_iops;
176 init_special_inode(tmp, tmp->i_mode, tmp->i_rdev);
182 spin_lock(&ip->i_spin);
185 inode = igrab(ip->i_vnode);
186 spin_unlock(&ip->i_spin);
201 spin_unlock(&ip->i_spin);
203 insert_inode_hash(inode);
208 static int iget_test(struct inode *inode, void *opaque)
210 struct gfs2_inode *ip = get_v2ip(inode);
211 struct gfs2_inum *inum = (struct gfs2_inum *)opaque;
213 if (ip && ip->i_num.no_addr == inum->no_addr)
219 struct inode *gfs2_iget(struct super_block *sb, struct gfs2_inum *inum)
221 return ilookup5(sb, (unsigned long)inum->no_formal_ino,
225 void gfs2_inode_min_init(struct gfs2_inode *ip, unsigned int type)
227 spin_lock(&ip->i_spin);
228 if (!test_and_set_bit(GIF_MIN_INIT, &ip->i_flags)) {
229 ip->i_di.di_nlink = 1;
230 ip->i_di.di_mode = DT2IF(type);
232 spin_unlock(&ip->i_spin);
236 * gfs2_inode_refresh - Refresh the incore copy of the dinode
237 * @ip: The GFS2 inode
242 int gfs2_inode_refresh(struct gfs2_inode *ip)
244 struct buffer_head *dibh;
247 error = gfs2_meta_inode_buffer(ip, &dibh);
251 if (gfs2_metatype_check(ip->i_sbd, dibh, GFS2_METATYPE_DI)) {
256 spin_lock(&ip->i_spin);
257 gfs2_dinode_in(&ip->i_di, dibh->b_data);
258 set_bit(GIF_MIN_INIT, &ip->i_flags);
259 spin_unlock(&ip->i_spin);
263 if (ip->i_num.no_addr != ip->i_di.di_num.no_addr) {
264 if (gfs2_consist_inode(ip))
265 gfs2_dinode_print(&ip->i_di);
268 if (ip->i_num.no_formal_ino != ip->i_di.di_num.no_formal_ino)
271 ip->i_vn = ip->i_gl->gl_vn;
277 * inode_create - create a struct gfs2_inode
278 * @i_gl: The glock covering the inode
279 * @inum: The inode number
280 * @io_gl: the iopen glock to acquire/hold (using holder in new gfs2_inode)
281 * @io_state: the state the iopen glock should be acquired in
282 * @ipp: pointer to put the returned inode in
287 static int inode_create(struct gfs2_glock *i_gl, struct gfs2_inum *inum,
288 struct gfs2_glock *io_gl, unsigned int io_state,
289 struct gfs2_inode **ipp)
291 struct gfs2_sbd *sdp = i_gl->gl_sbd;
292 struct gfs2_inode *ip;
295 ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
298 memset(ip, 0, sizeof(struct gfs2_inode));
302 atomic_set(&ip->i_count, 1);
304 ip->i_vn = i_gl->gl_vn - 1;
309 spin_lock_init(&ip->i_spin);
310 init_rwsem(&ip->i_rw_mutex);
312 ip->i_greedy = gfs2_tune_get(sdp, gt_greedy_default);
314 error = gfs2_glock_nq_init(io_gl,
315 io_state, GL_LOCAL_EXCL | GL_EXACT,
319 ip->i_iopen_gh.gh_owner = NULL;
321 spin_lock(&io_gl->gl_spin);
322 gfs2_glock_hold(i_gl);
323 set_gl2gl(io_gl, i_gl);
324 spin_unlock(&io_gl->gl_spin);
326 gfs2_glock_hold(i_gl);
329 atomic_inc(&sdp->sd_inode_count);
336 gfs2_meta_cache_flush(ip);
337 kmem_cache_free(gfs2_inode_cachep, ip);
344 * gfs2_inode_get - Create or get a reference on an inode
345 * @i_gl: The glock covering the inode
346 * @inum: The inode number
348 * @ipp: pointer to put the returned inode in
353 int gfs2_inode_get(struct gfs2_glock *i_gl, struct gfs2_inum *inum, int create,
354 struct gfs2_inode **ipp)
356 struct gfs2_sbd *sdp = i_gl->gl_sbd;
357 struct gfs2_glock *io_gl;
360 gfs2_glmutex_lock(i_gl);
362 *ipp = get_gl2ip(i_gl);
365 if ((*ipp)->i_num.no_formal_ino != inum->no_formal_ino)
367 atomic_inc(&(*ipp)->i_count);
375 error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_iopen_glops,
378 error = inode_create(i_gl, inum, io_gl, LM_ST_SHARED, ipp);
379 gfs2_glock_put(io_gl);
383 gfs2_glmutex_unlock(i_gl);
388 void gfs2_inode_hold(struct gfs2_inode *ip)
390 gfs2_assert(ip->i_sbd, atomic_read(&ip->i_count) > 0);
391 atomic_inc(&ip->i_count);
394 void gfs2_inode_put(struct gfs2_inode *ip)
396 gfs2_assert(ip->i_sbd, atomic_read(&ip->i_count) > 0);
397 atomic_dec(&ip->i_count);
400 void gfs2_inode_destroy(struct gfs2_inode *ip)
402 struct gfs2_sbd *sdp = ip->i_sbd;
403 struct gfs2_glock *io_gl = ip->i_iopen_gh.gh_gl;
404 struct gfs2_glock *i_gl = ip->i_gl;
406 gfs2_assert_warn(sdp, !atomic_read(&ip->i_count));
407 gfs2_assert(sdp, get_gl2gl(io_gl) == i_gl);
409 spin_lock(&io_gl->gl_spin);
410 set_gl2gl(io_gl, NULL);
411 gfs2_glock_put(i_gl);
412 spin_unlock(&io_gl->gl_spin);
414 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
416 gfs2_meta_cache_flush(ip);
417 kmem_cache_free(gfs2_inode_cachep, ip);
419 set_gl2ip(i_gl, NULL);
420 gfs2_glock_put(i_gl);
422 atomic_dec(&sdp->sd_inode_count);
425 static int dinode_dealloc(struct gfs2_inode *ip, struct gfs2_unlinked *ul)
427 struct gfs2_sbd *sdp = ip->i_sbd;
428 struct gfs2_alloc *al;
429 struct gfs2_rgrpd *rgd;
432 if (ip->i_di.di_blocks != 1) {
433 if (gfs2_consist_inode(ip))
434 gfs2_dinode_print(&ip->i_di);
438 al = gfs2_alloc_get(ip);
440 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
444 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
448 rgd = gfs2_blk2rgrpd(sdp, ip->i_num.no_addr);
450 gfs2_consist_inode(ip);
452 goto out_rindex_relse;
455 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
458 goto out_rindex_relse;
460 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED +
461 RES_STATFS + RES_QUOTA, 1);
465 gfs2_trans_add_gl(ip->i_gl);
467 gfs2_free_di(rgd, ip);
469 error = gfs2_unlinked_ondisk_rm(sdp, ul);
472 clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
475 gfs2_glock_dq_uninit(&al->al_rgd_gh);
478 gfs2_glock_dq_uninit(&al->al_ri_gh);
481 gfs2_quota_unhold(ip);
490 * inode_dealloc - Deallocate all on-disk blocks for an inode (dinode)
491 * @sdp: the filesystem
492 * @inum: the inode number to deallocate
493 * @io_gh: a holder for the iopen glock for this inode
498 static int inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul,
499 struct gfs2_holder *io_gh)
501 struct gfs2_inode *ip;
502 struct gfs2_holder i_gh;
505 error = gfs2_glock_nq_num(sdp,
506 ul->ul_ut.ut_inum.no_addr, &gfs2_inode_glops,
507 LM_ST_EXCLUSIVE, 0, &i_gh);
511 /* We reacquire the iopen lock here to avoid a race with the NFS server
512 calling gfs2_read_inode() with the inode number of a inode we're in
513 the process of deallocating. And we can't keep our hold on the lock
514 from inode_dealloc_init() for deadlock reasons. */
516 gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY, io_gh);
517 error = gfs2_glock_nq(io_gh);
527 gfs2_assert_warn(sdp, !get_gl2ip(i_gh.gh_gl));
528 error = inode_create(i_gh.gh_gl, &ul->ul_ut.ut_inum, io_gh->gh_gl,
529 LM_ST_EXCLUSIVE, &ip);
531 gfs2_glock_dq(io_gh);
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);
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)
591 struct gfs2_holder io_gh;
594 gfs2_try_toss_inode(sdp, &ul->ul_ut.ut_inum);
596 error = gfs2_glock_nq_num(sdp,
597 ul->ul_ut.ut_inum.no_addr, &gfs2_iopen_glops,
598 LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB, &io_gh);
608 gfs2_glock_dq(&io_gh);
609 error = inode_dealloc(sdp, ul, &io_gh);
610 gfs2_holder_uninit(&io_gh);
615 static int inode_dealloc_uninit(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
617 struct gfs2_rgrpd *rgd;
618 struct gfs2_holder ri_gh, rgd_gh;
621 error = gfs2_rindex_hold(sdp, &ri_gh);
625 rgd = gfs2_blk2rgrpd(sdp, ul->ul_ut.ut_inum.no_addr);
632 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rgd_gh);
636 error = gfs2_trans_begin(sdp,
637 RES_RG_BIT + RES_UNLINKED + RES_STATFS,
642 gfs2_free_uninit_di(rgd, ul->ul_ut.ut_inum.no_addr);
643 gfs2_unlinked_ondisk_rm(sdp, ul);
648 gfs2_glock_dq_uninit(&rgd_gh);
650 gfs2_glock_dq_uninit(&ri_gh);
655 int gfs2_inode_dealloc(struct gfs2_sbd *sdp, struct gfs2_unlinked *ul)
657 if (ul->ul_ut.ut_flags & GFS2_UTF_UNINIT)
658 return inode_dealloc_uninit(sdp, ul);
660 return try_inode_dealloc(sdp, ul);
664 * gfs2_change_nlink - Change nlink count on inode
665 * @ip: The GFS2 inode
666 * @diff: The change in the nlink count required
671 int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
673 struct buffer_head *dibh;
677 nlink = ip->i_di.di_nlink + diff;
679 /* If we are reducing the nlink count, but the new value ends up being
680 bigger than the old one, we must have underflowed. */
681 if (diff < 0 && nlink > ip->i_di.di_nlink) {
682 if (gfs2_consist_inode(ip))
683 gfs2_dinode_print(&ip->i_di);
687 error = gfs2_meta_inode_buffer(ip, &dibh);
691 ip->i_di.di_nlink = nlink;
692 ip->i_di.di_ctime = get_seconds();
694 gfs2_trans_add_bh(ip->i_gl, dibh);
695 gfs2_dinode_out(&ip->i_di, dibh->b_data);
702 * gfs2_lookupi - Look up a filename in a directory and return its inode
703 * @d_gh: An initialized holder for the directory glock
704 * @name: The name of the inode to look for
705 * @is_root: If 1, ignore the caller's permissions
706 * @i_gh: An uninitialized holder for the new inode glock
708 * There will always be a vnode (Linux VFS inode) for the d_gh inode unless
714 int gfs2_lookupi(struct gfs2_inode *dip, struct qstr *name, int is_root,
715 struct gfs2_inode **ipp)
717 struct gfs2_sbd *sdp = dip->i_sbd;
718 struct gfs2_holder d_gh;
719 struct gfs2_inum inum;
721 struct gfs2_glock *gl;
724 if (!name->len || name->len > GFS2_FNAMESIZE)
725 return -ENAMETOOLONG;
727 if (gfs2_filecmp(name, ".", 1) ||
728 (gfs2_filecmp(name, "..", 2) && dip == sdp->sd_root_dir)) {
729 gfs2_inode_hold(dip);
734 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
739 error = gfs2_repermission(dip->i_vnode, MAY_EXEC, NULL);
744 error = gfs2_dir_search(dip, name, &inum, &type);
748 error = gfs2_glock_get(sdp, inum.no_addr, &gfs2_inode_glops,
753 error = gfs2_inode_get(gl, &inum, CREATE, ipp);
755 gfs2_inode_min_init(*ipp, type);
760 gfs2_glock_dq_uninit(&d_gh);
765 static int pick_formal_ino_1(struct gfs2_sbd *sdp, uint64_t *formal_ino)
767 struct gfs2_inode *ip = sdp->sd_ir_inode;
768 struct buffer_head *bh;
769 struct gfs2_inum_range ir;
772 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
775 down(&sdp->sd_inum_mutex);
777 error = gfs2_meta_inode_buffer(ip, &bh);
779 up(&sdp->sd_inum_mutex);
784 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
787 *formal_ino = ir.ir_start++;
789 gfs2_trans_add_bh(ip->i_gl, bh);
790 gfs2_inum_range_out(&ir,
791 bh->b_data + sizeof(struct gfs2_dinode));
793 up(&sdp->sd_inum_mutex);
800 up(&sdp->sd_inum_mutex);
806 static int pick_formal_ino_2(struct gfs2_sbd *sdp, uint64_t *formal_ino)
808 struct gfs2_inode *ip = sdp->sd_ir_inode;
809 struct gfs2_inode *m_ip = sdp->sd_inum_inode;
810 struct gfs2_holder gh;
811 struct buffer_head *bh;
812 struct gfs2_inum_range ir;
815 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
819 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
822 down(&sdp->sd_inum_mutex);
824 error = gfs2_meta_inode_buffer(ip, &bh);
828 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
831 struct buffer_head *m_bh;
834 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
838 x = *(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode));
839 x = y = be64_to_cpu(x);
841 ir.ir_length = GFS2_INUM_QUANTUM;
842 x += GFS2_INUM_QUANTUM;
844 gfs2_consist_inode(m_ip);
846 gfs2_trans_add_bh(m_ip->i_gl, m_bh);
847 *(uint64_t *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = x;
852 *formal_ino = ir.ir_start++;
855 gfs2_trans_add_bh(ip->i_gl, bh);
856 gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
862 up(&sdp->sd_inum_mutex);
866 gfs2_glock_dq_uninit(&gh);
871 static int pick_formal_ino(struct gfs2_sbd *sdp, uint64_t *inum)
875 error = pick_formal_ino_1(sdp, inum);
879 error = pick_formal_ino_2(sdp, inum);
885 * create_ok - OK to create a new on-disk inode here?
886 * @dip: Directory in which dinode is to be created
887 * @name: Name of new dinode
893 static int create_ok(struct gfs2_inode *dip, struct qstr *name,
898 error = gfs2_repermission(dip->i_vnode, MAY_WRITE | MAY_EXEC, NULL);
902 /* Don't create entries in an unlinked directory */
903 if (!dip->i_di.di_nlink)
906 error = gfs2_dir_search(dip, name, NULL, NULL);
917 if (dip->i_di.di_entries == (uint32_t)-1)
919 if (S_ISDIR(mode) && dip->i_di.di_nlink == (uint32_t)-1)
925 static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
926 unsigned int *uid, unsigned int *gid)
928 if (dip->i_sbd->sd_args.ar_suiddir &&
929 (dip->i_di.di_mode & S_ISUID) &&
933 else if (dip->i_di.di_uid != current->fsuid)
935 *uid = dip->i_di.di_uid;
937 *uid = current->fsuid;
939 if (dip->i_di.di_mode & S_ISGID) {
942 *gid = dip->i_di.di_gid;
944 *gid = current->fsgid;
947 static int alloc_dinode(struct gfs2_inode *dip, struct gfs2_unlinked *ul)
949 struct gfs2_sbd *sdp = dip->i_sbd;
954 dip->i_alloc.al_requested = RES_DINODE;
955 error = gfs2_inplace_reserve(dip);
959 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_UNLINKED +
964 ul->ul_ut.ut_inum.no_addr = gfs2_alloc_di(dip);
966 ul->ul_ut.ut_flags = GFS2_UTF_UNINIT;
967 error = gfs2_unlinked_ondisk_add(sdp, ul);
972 gfs2_inplace_release(dip);
981 * init_dinode - Fill in a new dinode structure
982 * @dip: the directory this inode is being created in
983 * @gl: The glock covering the new inode
984 * @inum: the inode number
985 * @mode: the file permissions
991 static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
992 struct gfs2_inum *inum, unsigned int mode,
993 unsigned int uid, unsigned int gid)
995 struct gfs2_sbd *sdp = dip->i_sbd;
996 struct gfs2_dinode di;
997 struct buffer_head *dibh;
999 dibh = gfs2_meta_new(gl, inum->no_addr);
1000 gfs2_trans_add_bh(gl, dibh);
1001 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
1002 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
1004 memset(&di, 0, sizeof(struct gfs2_dinode));
1005 gfs2_meta_header_in(&di.di_header, dibh->b_data);
1011 di.di_atime = di.di_mtime = di.di_ctime = get_seconds();
1012 di.di_goal_meta = di.di_goal_data = inum->no_addr;
1014 if (S_ISREG(mode)) {
1015 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
1016 gfs2_tune_get(sdp, gt_new_files_jdata))
1017 di.di_flags |= GFS2_DIF_JDATA;
1018 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
1019 gfs2_tune_get(sdp, gt_new_files_directio))
1020 di.di_flags |= GFS2_DIF_DIRECTIO;
1021 } else if (S_ISDIR(mode)) {
1022 di.di_flags |= (dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO);
1023 di.di_flags |= (dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA);
1026 gfs2_dinode_out(&di, dibh->b_data);
1030 static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
1031 unsigned int mode, struct gfs2_unlinked *ul)
1033 struct gfs2_sbd *sdp = dip->i_sbd;
1034 unsigned int uid, gid;
1037 munge_mode_uid_gid(dip, &mode, &uid, &gid);
1039 gfs2_alloc_get(dip);
1041 error = gfs2_quota_lock(dip, uid, gid);
1045 error = gfs2_quota_check(dip, uid, gid);
1049 error = gfs2_trans_begin(sdp, RES_DINODE + RES_UNLINKED +
1054 ul->ul_ut.ut_flags = 0;
1055 error = gfs2_unlinked_ondisk_munge(sdp, ul);
1057 init_dinode(dip, gl, &ul->ul_ut.ut_inum,
1060 gfs2_quota_change(dip, +1, uid, gid);
1062 gfs2_trans_end(sdp);
1065 gfs2_quota_unlock(dip);
1068 gfs2_alloc_put(dip);
1073 static int link_dinode(struct gfs2_inode *dip, struct qstr *name,
1074 struct gfs2_inode *ip, struct gfs2_unlinked *ul)
1076 struct gfs2_sbd *sdp = dip->i_sbd;
1077 struct gfs2_alloc *al;
1079 struct buffer_head *dibh;
1082 al = gfs2_alloc_get(dip);
1084 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
1088 error = gfs2_diradd_alloc_required(dip, name, &alloc_required);
1089 if (alloc_required) {
1090 error = gfs2_quota_check(dip, dip->i_di.di_uid,
1093 goto fail_quota_locks;
1095 al->al_requested = sdp->sd_max_dirres;
1097 error = gfs2_inplace_reserve(dip);
1099 goto fail_quota_locks;
1101 error = gfs2_trans_begin(sdp,
1102 sdp->sd_max_dirres +
1103 al->al_rgd->rd_ri.ri_length +
1104 2 * RES_DINODE + RES_UNLINKED +
1105 RES_STATFS + RES_QUOTA, 0);
1109 error = gfs2_trans_begin(sdp,
1114 goto fail_quota_locks;
1117 error = gfs2_dir_add(dip, name, &ip->i_num, IF2DT(ip->i_di.di_mode));
1119 goto fail_end_trans;
1121 error = gfs2_meta_inode_buffer(ip, &dibh);
1123 goto fail_end_trans;
1124 ip->i_di.di_nlink = 1;
1125 gfs2_trans_add_bh(ip->i_gl, dibh);
1126 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1129 error = gfs2_unlinked_ondisk_rm(sdp, ul);
1131 goto fail_end_trans;
1136 gfs2_trans_end(sdp);
1139 if (dip->i_alloc.al_rgd)
1140 gfs2_inplace_release(dip);
1143 gfs2_quota_unlock(dip);
1146 gfs2_alloc_put(dip);
1152 * gfs2_createi - Create a new inode
1153 * @ghs: An array of two holders
1154 * @name: The name of the new file
1155 * @mode: the permissions on the new inode
1157 * @ghs[0] is an initialized holder for the directory
1158 * @ghs[1] is the holder for the inode lock
1160 * If the return value is 0, the glocks on both the directory and the new
1161 * file are held. A transaction has been started and an inplace reservation
1167 int gfs2_createi(struct gfs2_holder *ghs, struct qstr *name, unsigned int mode)
1169 struct gfs2_inode *dip = get_gl2ip(ghs->gh_gl);
1170 struct gfs2_sbd *sdp = dip->i_sbd;
1171 struct gfs2_unlinked *ul;
1172 struct gfs2_inode *ip;
1175 if (!name->len || name->len > GFS2_FNAMESIZE)
1176 return -ENAMETOOLONG;
1178 error = gfs2_unlinked_get(sdp, &ul);
1182 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
1183 error = gfs2_glock_nq(ghs);
1187 error = create_ok(dip, name, mode);
1191 error = pick_formal_ino(sdp, &ul->ul_ut.ut_inum.no_formal_ino);
1195 error = alloc_dinode(dip, ul);
1199 if (ul->ul_ut.ut_inum.no_addr < dip->i_num.no_addr) {
1202 error = gfs2_glock_nq_num(sdp,
1203 ul->ul_ut.ut_inum.no_addr,
1205 LM_ST_EXCLUSIVE, GL_SKIP,
1208 gfs2_unlinked_put(sdp, ul);
1212 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
1213 error = gfs2_glock_nq(ghs);
1215 gfs2_glock_dq_uninit(ghs + 1);
1216 gfs2_unlinked_put(sdp, ul);
1220 error = create_ok(dip, name, mode);
1224 error = gfs2_glock_nq_num(sdp,
1225 ul->ul_ut.ut_inum.no_addr,
1227 LM_ST_EXCLUSIVE, GL_SKIP,
1233 error = make_dinode(dip, ghs[1].gh_gl, mode, ul);
1237 error = gfs2_inode_get(ghs[1].gh_gl, &ul->ul_ut.ut_inum, CREATE, &ip);
1241 error = gfs2_inode_refresh(ip);
1245 error = gfs2_acl_create(dip, ip);
1249 error = link_dinode(dip, name, ip, ul);
1253 gfs2_unlinked_put(sdp, ul);
1261 gfs2_glock_dq_uninit(ghs + 1);
1267 gfs2_unlinked_put(sdp, ul);
1273 * gfs2_unlinki - Unlink a file
1274 * @dip: The inode of the directory
1275 * @name: The name of the file to be unlinked
1276 * @ip: The inode of the file to be removed
1278 * Assumes Glocks on both dip and ip are held.
1283 int gfs2_unlinki(struct gfs2_inode *dip, struct qstr *name,
1284 struct gfs2_inode *ip, struct gfs2_unlinked *ul)
1286 struct gfs2_sbd *sdp = dip->i_sbd;
1289 error = gfs2_dir_del(dip, name);
1293 error = gfs2_change_nlink(ip, -1);
1297 /* If this inode is being unlinked from the directory structure,
1298 we need to mark that in the log so that it isn't lost during
1301 if (!ip->i_di.di_nlink) {
1302 ul->ul_ut.ut_inum = ip->i_num;
1303 error = gfs2_unlinked_ondisk_add(sdp, ul);
1305 set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
1312 * gfs2_rmdiri - Remove a directory
1313 * @dip: The parent directory of the directory to be removed
1314 * @name: The name of the directory to be removed
1315 * @ip: The GFS2 inode of the directory to be removed
1317 * Assumes Glocks on dip and ip are held
1322 int gfs2_rmdiri(struct gfs2_inode *dip, struct qstr *name,
1323 struct gfs2_inode *ip, struct gfs2_unlinked *ul)
1325 struct gfs2_sbd *sdp = dip->i_sbd;
1326 struct qstr dotname;
1329 if (ip->i_di.di_entries != 2) {
1330 if (gfs2_consist_inode(ip))
1331 gfs2_dinode_print(&ip->i_di);
1335 error = gfs2_dir_del(dip, name);
1339 error = gfs2_change_nlink(dip, -1);
1345 error = gfs2_dir_del(ip, &dotname);
1350 dotname.name = "..";
1351 error = gfs2_dir_del(ip, &dotname);
1355 error = gfs2_change_nlink(ip, -2);
1359 /* This inode is being unlinked from the directory structure and
1360 we need to mark that in the log so that it isn't lost during
1363 ul->ul_ut.ut_inum = ip->i_num;
1364 error = gfs2_unlinked_ondisk_add(sdp, ul);
1366 set_bit(GLF_STICKY, &ip->i_gl->gl_flags);
1372 * gfs2_unlink_ok - check to see that a inode is still in a directory
1373 * @dip: the directory
1374 * @name: the name of the file
1377 * Assumes that the lock on (at least) @dip is held.
1379 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1382 int gfs2_unlink_ok(struct gfs2_inode *dip, struct qstr *name,
1383 struct gfs2_inode *ip)
1385 struct gfs2_inum inum;
1389 if (IS_IMMUTABLE(ip->i_vnode) || IS_APPEND(ip->i_vnode))
1392 if ((dip->i_di.di_mode & S_ISVTX) &&
1393 dip->i_di.di_uid != current->fsuid &&
1394 ip->i_di.di_uid != current->fsuid &&
1395 !capable(CAP_FOWNER))
1398 if (IS_APPEND(dip->i_vnode))
1401 error = gfs2_repermission(dip->i_vnode, MAY_WRITE | MAY_EXEC, NULL);
1405 error = gfs2_dir_search(dip, name, &inum, &type);
1409 if (!gfs2_inum_equal(&inum, &ip->i_num))
1412 if (IF2DT(ip->i_di.di_mode) != type) {
1413 gfs2_consist_inode(dip);
1421 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1425 * Follow @to back to the root and make sure we don't encounter @this
1426 * Assumes we already hold the rename lock.
1431 int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1433 struct gfs2_sbd *sdp = this->i_sbd;
1434 struct gfs2_inode *tmp;
1438 memset(&dotdot, 0, sizeof(struct qstr));
1442 gfs2_inode_hold(to);
1449 if (to == sdp->sd_root_dir) {
1454 error = gfs2_lookupi(to, &dotdot, 1, &tmp);
1468 * gfs2_readlinki - return the contents of a symlink
1469 * @ip: the symlink's inode
1470 * @buf: a pointer to the buffer to be filled
1471 * @len: a pointer to the length of @buf
1473 * If @buf is too small, a piece of memory is kmalloc()ed and needs
1474 * to be freed by the caller.
1479 int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
1481 struct gfs2_holder i_gh;
1482 struct buffer_head *dibh;
1486 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
1487 error = gfs2_glock_nq_atime(&i_gh);
1489 gfs2_holder_uninit(&i_gh);
1493 if (!ip->i_di.di_size) {
1494 gfs2_consist_inode(ip);
1499 error = gfs2_meta_inode_buffer(ip, &dibh);
1503 x = ip->i_di.di_size + 1;
1505 *buf = kmalloc(x, GFP_KERNEL);
1512 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1519 gfs2_glock_dq_uninit(&i_gh);
1525 * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
1526 * conditionally update the inode's atime
1527 * @gh: the holder to acquire
1529 * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
1530 * Update if the difference between the current time and the inode's current
1531 * atime is greater than an interval specified at mount.
1536 int gfs2_glock_nq_atime(struct gfs2_holder *gh)
1538 struct gfs2_glock *gl = gh->gh_gl;
1539 struct gfs2_sbd *sdp = gl->gl_sbd;
1540 struct gfs2_inode *ip = get_gl2ip(gl);
1541 int64_t curtime, quantum = gfs2_tune_get(sdp, gt_atime_quantum);
1546 if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
1547 gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
1548 gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
1551 state = gh->gh_state;
1552 flags = gh->gh_flags;
1554 error = gfs2_glock_nq(gh);
1558 if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
1559 (sdp->sd_vfs->s_flags & MS_RDONLY))
1562 curtime = get_seconds();
1563 if (curtime - ip->i_di.di_atime >= quantum) {
1565 gfs2_holder_reinit(LM_ST_EXCLUSIVE,
1566 gh->gh_flags & ~LM_FLAG_ANY,
1568 error = gfs2_glock_nq(gh);
1572 /* Verify that atime hasn't been updated while we were
1573 trying to get exclusive lock. */
1575 curtime = get_seconds();
1576 if (curtime - ip->i_di.di_atime >= quantum) {
1577 struct buffer_head *dibh;
1579 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1580 if (error == -EROFS)
1585 error = gfs2_meta_inode_buffer(ip, &dibh);
1587 goto fail_end_trans;
1589 ip->i_di.di_atime = curtime;
1591 gfs2_trans_add_bh(ip->i_gl, dibh);
1592 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1595 gfs2_trans_end(sdp);
1598 /* If someone else has asked for the glock,
1599 unlock and let them have it. Then reacquire
1600 in the original state. */
1601 if (gfs2_glock_is_blocking(gl)) {
1603 gfs2_holder_reinit(state, flags, gh);
1604 return gfs2_glock_nq(gh);
1611 gfs2_trans_end(sdp);
1620 * glock_compare_atime - Compare two struct gfs2_glock structures for sort
1621 * @arg_a: the first structure
1622 * @arg_b: the second structure
1624 * Returns: 1 if A > B
1629 static int glock_compare_atime(const void *arg_a, const void *arg_b)
1631 struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a;
1632 struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b;
1633 struct lm_lockname *a = &gh_a->gh_gl->gl_name;
1634 struct lm_lockname *b = &gh_b->gh_gl->gl_name;
1637 if (a->ln_number > b->ln_number)
1639 else if (a->ln_number < b->ln_number)
1642 if (gh_a->gh_state == LM_ST_SHARED &&
1643 gh_b->gh_state == LM_ST_EXCLUSIVE)
1645 else if (gh_a->gh_state == LM_ST_SHARED &&
1646 (gh_b->gh_flags & GL_ATIME))
1654 * gfs2_glock_nq_m_atime - acquire multiple glocks where one may need an
1656 * @num_gh: the number of structures
1657 * @ghs: an array of struct gfs2_holder structures
1659 * Returns: 0 on success (all glocks acquired),
1660 * errno on failure (no glocks acquired)
1663 int gfs2_glock_nq_m_atime(unsigned int num_gh, struct gfs2_holder *ghs)
1665 struct gfs2_holder **p;
1673 ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1674 if (ghs->gh_flags & GL_ATIME)
1675 error = gfs2_glock_nq_atime(ghs);
1677 error = gfs2_glock_nq(ghs);
1681 p = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL);
1685 for (x = 0; x < num_gh; x++)
1688 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare_atime,NULL);
1690 for (x = 0; x < num_gh; x++) {
1691 p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC);
1693 if (p[x]->gh_flags & GL_ATIME)
1694 error = gfs2_glock_nq_atime(p[x]);
1696 error = gfs2_glock_nq(p[x]);
1700 gfs2_glock_dq(p[x]);
1711 * gfs2_try_toss_vnode - See if we can toss a vnode from memory
1714 * Returns: 1 if the vnode was tossed
1717 void gfs2_try_toss_vnode(struct gfs2_inode *ip)
1719 struct inode *inode;
1721 inode = gfs2_ip2v_lookup(ip);
1725 d_prune_aliases(inode);
1727 if (S_ISDIR(ip->i_di.di_mode)) {
1728 struct list_head *head = &inode->i_dentry;
1729 struct dentry *d = NULL;
1731 spin_lock(&dcache_lock);
1732 if (list_empty(head))
1733 spin_unlock(&dcache_lock);
1735 d = list_entry(head->next, struct dentry, d_alias);
1737 spin_unlock(&dcache_lock);
1739 if (have_submounts(d))
1742 shrink_dcache_parent(d);
1744 d_prune_aliases(inode);
1755 __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1757 struct buffer_head *dibh;
1760 error = gfs2_meta_inode_buffer(ip, &dibh);
1762 error = inode_setattr(ip->i_vnode, attr);
1763 gfs2_assert_warn(ip->i_sbd, !error);
1764 gfs2_inode_attr_out(ip);
1766 gfs2_trans_add_bh(ip->i_gl, dibh);
1767 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1774 * gfs2_setattr_simple -
1778 * Called with a reference on the vnode.
1783 int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1787 if (get_transaction)
1788 return __gfs2_setattr_simple(ip, attr);
1790 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
1794 error = __gfs2_setattr_simple(ip, attr);
1796 gfs2_trans_end(ip->i_sbd);
1801 int gfs2_repermission(struct inode *inode, int mask, struct nameidata *nd)
1803 return permission(inode, mask, nd);