2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 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 version 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 <linux/lm_interface.h>
20 #include <linux/security.h>
33 #include "ops_address.h"
35 #include "ops_inode.h"
41 struct gfs2_inum_range_host {
46 static int iget_test(struct inode *inode, void *opaque)
48 struct gfs2_inode *ip = GFS2_I(inode);
49 u64 *no_addr = opaque;
51 if (ip->i_no_addr == *no_addr &&
52 inode->i_private != NULL)
58 static int iget_set(struct inode *inode, void *opaque)
60 struct gfs2_inode *ip = GFS2_I(inode);
61 u64 *no_addr = opaque;
63 inode->i_ino = (unsigned long)*no_addr;
64 ip->i_no_addr = *no_addr;
68 struct inode *gfs2_ilookup(struct super_block *sb, u64 no_addr)
70 unsigned long hash = (unsigned long)no_addr;
71 return ilookup5(sb, hash, iget_test, &no_addr);
74 static struct inode *gfs2_iget(struct super_block *sb, u64 no_addr)
76 unsigned long hash = (unsigned long)no_addr;
77 return iget5_locked(sb, hash, iget_test, iget_set, &no_addr);
81 * gfs2_inode_lookup - Lookup an inode
82 * @sb: The super block
83 * @no_addr: The inode number
84 * @type: The type of the inode
86 * Returns: A VFS inode, or an error
89 struct inode *gfs2_inode_lookup(struct super_block *sb, u64 no_addr, unsigned int type)
91 struct inode *inode = gfs2_iget(sb, no_addr);
92 struct gfs2_inode *ip = GFS2_I(inode);
93 struct gfs2_glock *io_gl;
97 return ERR_PTR(-ENOBUFS);
99 if (inode->i_state & I_NEW) {
100 struct gfs2_sbd *sdp = GFS2_SB(inode);
102 inode->i_private = ip;
104 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
107 ip->i_gl->gl_object = ip;
109 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
113 set_bit(GIF_INVALID, &ip->i_flags);
114 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
117 ip->i_iopen_gh.gh_gl->gl_object = ip;
119 gfs2_glock_put(io_gl);
122 * We must read the inode in order to work out its type in
123 * this case. Note that this doesn't happen often as we normally
124 * know the type beforehand. This code path only occurs during
125 * unlinked inode recovery (where it is safe to do this glock,
126 * which is not true in the general case).
128 inode->i_mode = mode = DT2IF(type);
129 if (type == DT_UNKNOWN) {
130 struct gfs2_holder gh;
131 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
134 /* Inode is now uptodate */
135 mode = inode->i_mode;
136 gfs2_glock_dq_uninit(&gh);
140 inode->i_op = &gfs2_file_iops;
141 inode->i_fop = &gfs2_file_fops;
142 inode->i_mapping->a_ops = &gfs2_file_aops;
143 } else if (S_ISDIR(mode)) {
144 inode->i_op = &gfs2_dir_iops;
145 inode->i_fop = &gfs2_dir_fops;
146 } else if (S_ISLNK(mode)) {
147 inode->i_op = &gfs2_symlink_iops;
149 inode->i_op = &gfs2_dev_iops;
152 unlock_new_inode(inode);
157 gfs2_glock_dq(&ip->i_iopen_gh);
159 gfs2_glock_put(io_gl);
161 ip->i_gl->gl_object = NULL;
162 gfs2_glock_put(ip->i_gl);
165 return ERR_PTR(error);
168 static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
170 struct gfs2_dinode_host *di = &ip->i_di;
171 const struct gfs2_dinode *str = buf;
173 if (ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)) {
174 if (gfs2_consist_inode(ip))
175 gfs2_dinode_print(ip);
178 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
179 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
180 ip->i_inode.i_rdev = 0;
181 switch (ip->i_inode.i_mode & S_IFMT) {
184 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
185 be32_to_cpu(str->di_minor));
189 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
190 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
192 * We will need to review setting the nlink count here in the
193 * light of the forthcoming ro bind mount work. This is a reminder
196 ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
197 di->di_size = be64_to_cpu(str->di_size);
198 i_size_write(&ip->i_inode, di->di_size);
199 di->di_blocks = be64_to_cpu(str->di_blocks);
200 gfs2_set_inode_blocks(&ip->i_inode);
201 ip->i_inode.i_atime.tv_sec = be64_to_cpu(str->di_atime);
202 ip->i_inode.i_atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
203 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
204 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
205 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
206 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
208 di->di_goal_meta = be64_to_cpu(str->di_goal_meta);
209 di->di_goal_data = be64_to_cpu(str->di_goal_data);
210 di->di_generation = be64_to_cpu(str->di_generation);
212 di->di_flags = be32_to_cpu(str->di_flags);
213 gfs2_set_inode_flags(&ip->i_inode);
214 di->di_height = be16_to_cpu(str->di_height);
216 di->di_depth = be16_to_cpu(str->di_depth);
217 di->di_entries = be32_to_cpu(str->di_entries);
219 di->di_eattr = be64_to_cpu(str->di_eattr);
224 * gfs2_inode_refresh - Refresh the incore copy of the dinode
225 * @ip: The GFS2 inode
230 int gfs2_inode_refresh(struct gfs2_inode *ip)
232 struct buffer_head *dibh;
235 error = gfs2_meta_inode_buffer(ip, &dibh);
239 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
244 error = gfs2_dinode_in(ip, dibh->b_data);
246 clear_bit(GIF_INVALID, &ip->i_flags);
251 int gfs2_dinode_dealloc(struct gfs2_inode *ip)
253 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
254 struct gfs2_alloc *al;
255 struct gfs2_rgrpd *rgd;
258 if (ip->i_di.di_blocks != 1) {
259 if (gfs2_consist_inode(ip))
260 gfs2_dinode_print(ip);
264 al = gfs2_alloc_get(ip);
266 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
270 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
274 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
276 gfs2_consist_inode(ip);
278 goto out_rindex_relse;
281 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
284 goto out_rindex_relse;
286 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
290 gfs2_trans_add_gl(ip->i_gl);
292 gfs2_free_di(rgd, ip);
295 clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
298 gfs2_glock_dq_uninit(&al->al_rgd_gh);
300 gfs2_glock_dq_uninit(&al->al_ri_gh);
302 gfs2_quota_unhold(ip);
309 * gfs2_change_nlink - Change nlink count on inode
310 * @ip: The GFS2 inode
311 * @diff: The change in the nlink count required
315 int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
317 struct buffer_head *dibh;
321 BUG_ON(diff != 1 && diff != -1);
322 nlink = ip->i_inode.i_nlink + diff;
324 /* If we are reducing the nlink count, but the new value ends up being
325 bigger than the old one, we must have underflowed. */
326 if (diff < 0 && nlink > ip->i_inode.i_nlink) {
327 if (gfs2_consist_inode(ip))
328 gfs2_dinode_print(ip);
332 error = gfs2_meta_inode_buffer(ip, &dibh);
337 inc_nlink(&ip->i_inode);
339 drop_nlink(&ip->i_inode);
341 ip->i_inode.i_ctime = CURRENT_TIME;
343 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
344 gfs2_dinode_out(ip, dibh->b_data);
346 mark_inode_dirty(&ip->i_inode);
348 if (ip->i_inode.i_nlink == 0)
349 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
354 struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
358 gfs2_str2qstr(&qstr, name);
359 inode = gfs2_lookupi(dip, &qstr, 1, NULL);
360 /* gfs2_lookupi has inconsistent callers: vfs
361 * related routines expect NULL for no entry found,
362 * gfs2_lookup_simple callers expect ENOENT
363 * and do not check for NULL.
366 return ERR_PTR(-ENOENT);
373 * gfs2_lookupi - Look up a filename in a directory and return its inode
374 * @d_gh: An initialized holder for the directory glock
375 * @name: The name of the inode to look for
376 * @is_root: If 1, ignore the caller's permissions
377 * @i_gh: An uninitialized holder for the new inode glock
379 * This can be called via the VFS filldir function when NFS is doing
380 * a readdirplus and the inode which its intending to stat isn't
381 * already in cache. In this case we must not take the directory glock
382 * again, since the readdir call will have already taken that lock.
387 struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
388 int is_root, struct nameidata *nd)
390 struct super_block *sb = dir->i_sb;
391 struct gfs2_inode *dip = GFS2_I(dir);
392 struct gfs2_holder d_gh;
394 struct inode *inode = NULL;
397 if (!name->len || name->len > GFS2_FNAMESIZE)
398 return ERR_PTR(-ENAMETOOLONG);
400 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
401 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
402 dir == sb->s_root->d_inode)) {
407 if (gfs2_glock_is_locked_by_me(dip->i_gl) == 0) {
408 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
410 return ERR_PTR(error);
415 error = permission(dir, MAY_EXEC, NULL);
420 inode = gfs2_dir_search(dir, name);
422 error = PTR_ERR(inode);
425 gfs2_glock_dq_uninit(&d_gh);
426 if (error == -ENOENT)
428 return inode ? inode : ERR_PTR(error);
431 static void gfs2_inum_range_in(struct gfs2_inum_range_host *ir, const void *buf)
433 const struct gfs2_inum_range *str = buf;
435 ir->ir_start = be64_to_cpu(str->ir_start);
436 ir->ir_length = be64_to_cpu(str->ir_length);
439 static void gfs2_inum_range_out(const struct gfs2_inum_range_host *ir, void *buf)
441 struct gfs2_inum_range *str = buf;
443 str->ir_start = cpu_to_be64(ir->ir_start);
444 str->ir_length = cpu_to_be64(ir->ir_length);
447 static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino)
449 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
450 struct buffer_head *bh;
451 struct gfs2_inum_range_host ir;
454 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
457 mutex_lock(&sdp->sd_inum_mutex);
459 error = gfs2_meta_inode_buffer(ip, &bh);
461 mutex_unlock(&sdp->sd_inum_mutex);
466 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
469 *formal_ino = ir.ir_start++;
471 gfs2_trans_add_bh(ip->i_gl, bh, 1);
472 gfs2_inum_range_out(&ir,
473 bh->b_data + sizeof(struct gfs2_dinode));
475 mutex_unlock(&sdp->sd_inum_mutex);
482 mutex_unlock(&sdp->sd_inum_mutex);
488 static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino)
490 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
491 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
492 struct gfs2_holder gh;
493 struct buffer_head *bh;
494 struct gfs2_inum_range_host ir;
497 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
501 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
504 mutex_lock(&sdp->sd_inum_mutex);
506 error = gfs2_meta_inode_buffer(ip, &bh);
510 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
513 struct buffer_head *m_bh;
517 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
521 z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode));
522 x = y = be64_to_cpu(z);
524 ir.ir_length = GFS2_INUM_QUANTUM;
525 x += GFS2_INUM_QUANTUM;
527 gfs2_consist_inode(m_ip);
529 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
530 *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z;
535 *formal_ino = ir.ir_start++;
538 gfs2_trans_add_bh(ip->i_gl, bh, 1);
539 gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
544 mutex_unlock(&sdp->sd_inum_mutex);
547 gfs2_glock_dq_uninit(&gh);
551 static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum)
555 error = pick_formal_ino_1(sdp, inum);
559 error = pick_formal_ino_2(sdp, inum);
565 * create_ok - OK to create a new on-disk inode here?
566 * @dip: Directory in which dinode is to be created
567 * @name: Name of new dinode
573 static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
578 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
582 /* Don't create entries in an unlinked directory */
583 if (!dip->i_inode.i_nlink)
586 error = gfs2_dir_check(&dip->i_inode, name, NULL);
597 if (dip->i_di.di_entries == (u32)-1)
599 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
605 static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
606 unsigned int *uid, unsigned int *gid)
608 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
609 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
612 else if (dip->i_inode.i_uid != current->fsuid)
614 *uid = dip->i_inode.i_uid;
616 *uid = current->fsuid;
618 if (dip->i_inode.i_mode & S_ISGID) {
621 *gid = dip->i_inode.i_gid;
623 *gid = current->fsgid;
626 static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
628 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
633 dip->i_alloc.al_requested = RES_DINODE;
634 error = gfs2_inplace_reserve(dip);
638 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
642 *no_addr = gfs2_alloc_di(dip, generation);
647 gfs2_inplace_release(dip);
654 * init_dinode - Fill in a new dinode structure
655 * @dip: the directory this inode is being created in
656 * @gl: The glock covering the new inode
657 * @inum: the inode number
658 * @mode: the file permissions
664 static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
665 const struct gfs2_inum_host *inum, unsigned int mode,
666 unsigned int uid, unsigned int gid,
667 const u64 *generation, dev_t dev)
669 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
670 struct gfs2_dinode *di;
671 struct buffer_head *dibh;
672 struct timespec tv = CURRENT_TIME;
674 dibh = gfs2_meta_new(gl, inum->no_addr);
675 gfs2_trans_add_bh(gl, dibh, 1);
676 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
677 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
678 di = (struct gfs2_dinode *)dibh->b_data;
680 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
681 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
682 di->di_mode = cpu_to_be32(mode);
683 di->di_uid = cpu_to_be32(uid);
684 di->di_gid = cpu_to_be32(gid);
687 di->di_blocks = cpu_to_be64(1);
688 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
689 di->di_major = cpu_to_be32(MAJOR(dev));
690 di->di_minor = cpu_to_be32(MINOR(dev));
691 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
692 di->di_generation = cpu_to_be64(*generation);
696 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
697 gfs2_tune_get(sdp, gt_new_files_jdata))
698 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
699 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
700 gfs2_tune_get(sdp, gt_new_files_directio))
701 di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
702 } else if (S_ISDIR(mode)) {
703 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
704 GFS2_DIF_INHERIT_DIRECTIO);
705 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
706 GFS2_DIF_INHERIT_JDATA);
710 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
716 memset(&di->__pad4, 0, sizeof(di->__pad4));
718 di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
719 di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
720 di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
721 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
726 static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
727 unsigned int mode, const struct gfs2_inum_host *inum,
728 const u64 *generation, dev_t dev)
730 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
731 unsigned int uid, gid;
734 munge_mode_uid_gid(dip, &mode, &uid, &gid);
737 error = gfs2_quota_lock(dip, uid, gid);
741 error = gfs2_quota_check(dip, uid, gid);
745 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
749 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev);
750 gfs2_quota_change(dip, +1, uid, gid);
754 gfs2_quota_unlock(dip);
760 static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
761 struct gfs2_inode *ip)
763 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
764 struct gfs2_alloc *al;
766 struct buffer_head *dibh;
769 al = gfs2_alloc_get(dip);
771 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
775 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
776 if (alloc_required < 0)
778 if (alloc_required) {
779 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
781 goto fail_quota_locks;
783 al->al_requested = sdp->sd_max_dirres;
785 error = gfs2_inplace_reserve(dip);
787 goto fail_quota_locks;
789 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
790 al->al_rgd->rd_length +
792 RES_STATFS + RES_QUOTA, 0);
796 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
798 goto fail_quota_locks;
801 error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
805 error = gfs2_meta_inode_buffer(ip, &dibh);
808 ip->i_inode.i_nlink = 1;
809 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
810 gfs2_dinode_out(ip, dibh->b_data);
818 if (dip->i_alloc.al_rgd)
819 gfs2_inplace_release(dip);
822 gfs2_quota_unlock(dip);
829 static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
835 struct gfs2_ea_request er;
837 err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
838 &name, &value, &len);
841 if (err == -EOPNOTSUPP)
846 memset(&er, 0, sizeof(struct gfs2_ea_request));
848 er.er_type = GFS2_EATYPE_SECURITY;
851 er.er_name_len = strlen(name);
852 er.er_data_len = len;
854 err = gfs2_ea_set_i(ip, &er);
863 * gfs2_createi - Create a new inode
864 * @ghs: An array of two holders
865 * @name: The name of the new file
866 * @mode: the permissions on the new inode
868 * @ghs[0] is an initialized holder for the directory
869 * @ghs[1] is the holder for the inode lock
871 * If the return value is not NULL, the glocks on both the directory and the new
872 * file are held. A transaction has been started and an inplace reservation
878 struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
879 unsigned int mode, dev_t dev)
881 struct inode *inode = NULL;
882 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
883 struct inode *dir = &dip->i_inode;
884 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
885 struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
889 if (!name->len || name->len > GFS2_FNAMESIZE)
890 return ERR_PTR(-ENAMETOOLONG);
892 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
893 error = gfs2_glock_nq(ghs);
897 error = create_ok(dip, name, mode);
901 error = pick_formal_ino(sdp, &inum.no_formal_ino);
905 error = alloc_dinode(dip, &inum.no_addr, &generation);
909 error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
910 LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
914 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev);
918 inode = gfs2_inode_lookup(dir->i_sb, inum.no_addr, IF2DT(mode));
922 error = gfs2_inode_refresh(GFS2_I(inode));
926 error = gfs2_acl_create(dip, GFS2_I(inode));
930 error = gfs2_security_init(dip, GFS2_I(inode));
934 error = link_dinode(dip, name, GFS2_I(inode));
939 return ERR_PTR(-ENOMEM);
943 gfs2_glock_dq_uninit(ghs + 1);
949 return ERR_PTR(error);
953 * gfs2_rmdiri - Remove a directory
954 * @dip: The parent directory of the directory to be removed
955 * @name: The name of the directory to be removed
956 * @ip: The GFS2 inode of the directory to be removed
958 * Assumes Glocks on dip and ip are held
963 int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
964 struct gfs2_inode *ip)
969 if (ip->i_di.di_entries != 2) {
970 if (gfs2_consist_inode(ip))
971 gfs2_dinode_print(ip);
975 error = gfs2_dir_del(dip, name);
979 error = gfs2_change_nlink(dip, -1);
983 gfs2_str2qstr(&dotname, ".");
984 error = gfs2_dir_del(ip, &dotname);
988 gfs2_str2qstr(&dotname, "..");
989 error = gfs2_dir_del(ip, &dotname);
993 /* It looks odd, but it really should be done twice */
994 error = gfs2_change_nlink(ip, -1);
998 error = gfs2_change_nlink(ip, -1);
1006 * gfs2_unlink_ok - check to see that a inode is still in a directory
1007 * @dip: the directory
1008 * @name: the name of the file
1011 * Assumes that the lock on (at least) @dip is held.
1013 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1016 int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
1017 const struct gfs2_inode *ip)
1021 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1024 if ((dip->i_inode.i_mode & S_ISVTX) &&
1025 dip->i_inode.i_uid != current->fsuid &&
1026 ip->i_inode.i_uid != current->fsuid && !capable(CAP_FOWNER))
1029 if (IS_APPEND(&dip->i_inode))
1032 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
1036 error = gfs2_dir_check(&dip->i_inode, name, ip);
1044 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1048 * Follow @to back to the root and make sure we don't encounter @this
1049 * Assumes we already hold the rename lock.
1054 int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1056 struct inode *dir = &to->i_inode;
1057 struct super_block *sb = dir->i_sb;
1062 gfs2_str2qstr(&dotdot, "..");
1067 if (dir == &this->i_inode) {
1071 if (dir == sb->s_root->d_inode) {
1076 tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
1078 error = PTR_ERR(tmp);
1092 * gfs2_readlinki - return the contents of a symlink
1093 * @ip: the symlink's inode
1094 * @buf: a pointer to the buffer to be filled
1095 * @len: a pointer to the length of @buf
1097 * If @buf is too small, a piece of memory is kmalloc()ed and needs
1098 * to be freed by the caller.
1103 int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
1105 struct gfs2_holder i_gh;
1106 struct buffer_head *dibh;
1110 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
1111 error = gfs2_glock_nq_atime(&i_gh);
1113 gfs2_holder_uninit(&i_gh);
1117 if (!ip->i_di.di_size) {
1118 gfs2_consist_inode(ip);
1123 error = gfs2_meta_inode_buffer(ip, &dibh);
1127 x = ip->i_di.di_size + 1;
1129 *buf = kmalloc(x, GFP_KERNEL);
1136 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1142 gfs2_glock_dq_uninit(&i_gh);
1147 * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
1148 * conditionally update the inode's atime
1149 * @gh: the holder to acquire
1151 * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
1152 * Update if the difference between the current time and the inode's current
1153 * atime is greater than an interval specified at mount.
1158 int gfs2_glock_nq_atime(struct gfs2_holder *gh)
1160 struct gfs2_glock *gl = gh->gh_gl;
1161 struct gfs2_sbd *sdp = gl->gl_sbd;
1162 struct gfs2_inode *ip = gl->gl_object;
1163 s64 quantum = gfs2_tune_get(sdp, gt_atime_quantum);
1167 struct timespec tv = CURRENT_TIME;
1169 if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
1170 gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
1171 gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
1174 state = gh->gh_state;
1175 flags = gh->gh_flags;
1177 error = gfs2_glock_nq(gh);
1181 if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
1182 (sdp->sd_vfs->s_flags & MS_RDONLY))
1185 if (tv.tv_sec - ip->i_inode.i_atime.tv_sec >= quantum) {
1187 gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
1189 error = gfs2_glock_nq(gh);
1193 /* Verify that atime hasn't been updated while we were
1194 trying to get exclusive lock. */
1197 if (tv.tv_sec - ip->i_inode.i_atime.tv_sec >= quantum) {
1198 struct buffer_head *dibh;
1199 struct gfs2_dinode *di;
1201 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1202 if (error == -EROFS)
1207 error = gfs2_meta_inode_buffer(ip, &dibh);
1209 goto fail_end_trans;
1211 ip->i_inode.i_atime = tv;
1213 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1214 di = (struct gfs2_dinode *)dibh->b_data;
1215 di->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
1216 di->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
1219 gfs2_trans_end(sdp);
1222 /* If someone else has asked for the glock,
1223 unlock and let them have it. Then reacquire
1224 in the original state. */
1225 if (gfs2_glock_is_blocking(gl)) {
1227 gfs2_holder_reinit(state, flags, gh);
1228 return gfs2_glock_nq(gh);
1235 gfs2_trans_end(sdp);
1242 __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1244 struct buffer_head *dibh;
1247 error = gfs2_meta_inode_buffer(ip, &dibh);
1249 error = inode_setattr(&ip->i_inode, attr);
1250 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
1251 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1252 gfs2_dinode_out(ip, dibh->b_data);
1259 * gfs2_setattr_simple -
1263 * Called with a reference on the vnode.
1268 int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1272 if (current->journal_info)
1273 return __gfs2_setattr_simple(ip, attr);
1275 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
1279 error = __gfs2_setattr_simple(ip, attr);
1280 gfs2_trans_end(GFS2_SB(&ip->i_inode));
1284 void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
1286 const struct gfs2_dinode_host *di = &ip->i_di;
1287 struct gfs2_dinode *str = buf;
1289 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
1290 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
1291 str->di_header.__pad0 = 0;
1292 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
1293 str->di_header.__pad1 = 0;
1294 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
1295 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
1296 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
1297 str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
1298 str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
1299 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
1300 str->di_size = cpu_to_be64(di->di_size);
1301 str->di_blocks = cpu_to_be64(di->di_blocks);
1302 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
1303 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
1304 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
1306 str->di_goal_meta = cpu_to_be64(di->di_goal_meta);
1307 str->di_goal_data = cpu_to_be64(di->di_goal_data);
1308 str->di_generation = cpu_to_be64(di->di_generation);
1310 str->di_flags = cpu_to_be32(di->di_flags);
1311 str->di_height = cpu_to_be16(di->di_height);
1312 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
1313 !(ip->i_di.di_flags & GFS2_DIF_EXHASH) ?
1314 GFS2_FORMAT_DE : 0);
1315 str->di_depth = cpu_to_be16(di->di_depth);
1316 str->di_entries = cpu_to_be32(di->di_entries);
1318 str->di_eattr = cpu_to_be64(di->di_eattr);
1319 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
1320 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
1321 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
1324 void gfs2_dinode_print(const struct gfs2_inode *ip)
1326 const struct gfs2_dinode_host *di = &ip->i_di;
1328 printk(KERN_INFO " no_formal_ino = %llu\n",
1329 (unsigned long long)ip->i_no_formal_ino);
1330 printk(KERN_INFO " no_addr = %llu\n",
1331 (unsigned long long)ip->i_no_addr);
1332 printk(KERN_INFO " di_size = %llu\n", (unsigned long long)di->di_size);
1333 printk(KERN_INFO " di_blocks = %llu\n",
1334 (unsigned long long)di->di_blocks);
1335 printk(KERN_INFO " di_goal_meta = %llu\n",
1336 (unsigned long long)di->di_goal_meta);
1337 printk(KERN_INFO " di_goal_data = %llu\n",
1338 (unsigned long long)di->di_goal_data);
1339 printk(KERN_INFO " di_flags = 0x%.8X\n", di->di_flags);
1340 printk(KERN_INFO " di_height = %u\n", di->di_height);
1341 printk(KERN_INFO " di_depth = %u\n", di->di_depth);
1342 printk(KERN_INFO " di_entries = %u\n", di->di_entries);
1343 printk(KERN_INFO " di_eattr = %llu\n",
1344 (unsigned long long)di->di_eattr);