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 lookup code fills in vfs inode contents based on info obtained
82 * from directory entry inside gfs2_inode_lookup(). This has caused issues
83 * with NFS code path since its get_dentry routine doesn't have the relevant
84 * directory entry when gfs2_inode_lookup() is invoked. Part of the code
85 * segment inside gfs2_inode_lookup code needs to get moved around.
87 * Clean up I_LOCK and I_NEW as well.
90 void gfs2_set_iop(struct inode *inode)
92 umode_t mode = inode->i_mode;
95 inode->i_op = &gfs2_file_iops;
96 inode->i_fop = &gfs2_file_fops;
97 inode->i_mapping->a_ops = &gfs2_file_aops;
98 } else if (S_ISDIR(mode)) {
99 inode->i_op = &gfs2_dir_iops;
100 inode->i_fop = &gfs2_dir_fops;
101 } else if (S_ISLNK(mode)) {
102 inode->i_op = &gfs2_symlink_iops;
104 inode->i_op = &gfs2_dev_iops;
107 unlock_new_inode(inode);
111 * gfs2_inode_lookup - Lookup an inode
112 * @sb: The super block
113 * @no_addr: The inode number
114 * @type: The type of the inode
116 * Returns: A VFS inode, or an error
119 struct inode *gfs2_inode_lookup(struct super_block *sb,
124 struct inode *inode = gfs2_iget(sb, no_addr);
125 struct gfs2_inode *ip = GFS2_I(inode);
126 struct gfs2_glock *io_gl;
130 return ERR_PTR(-ENOBUFS);
132 if (inode->i_state & I_NEW) {
133 struct gfs2_sbd *sdp = GFS2_SB(inode);
134 inode->i_private = ip;
135 ip->i_no_formal_ino = no_formal_ino;
137 error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &ip->i_gl);
140 ip->i_gl->gl_object = ip;
142 error = gfs2_glock_get(sdp, no_addr, &gfs2_iopen_glops, CREATE, &io_gl);
146 set_bit(GIF_INVALID, &ip->i_flags);
147 error = gfs2_glock_nq_init(io_gl, LM_ST_SHARED, GL_EXACT, &ip->i_iopen_gh);
150 ip->i_iopen_gh.gh_gl->gl_object = ip;
152 gfs2_glock_put(io_gl);
154 if ((type == DT_UNKNOWN) && (no_formal_ino == 0))
157 inode->i_mode = DT2IF(type);
160 * We must read the inode in order to work out its type in
161 * this case. Note that this doesn't happen often as we normally
162 * know the type beforehand. This code path only occurs during
163 * unlinked inode recovery (where it is safe to do this glock,
164 * which is not true in the general case).
166 if (type == DT_UNKNOWN) {
167 struct gfs2_holder gh;
168 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
171 /* Inode is now uptodate */
172 gfs2_glock_dq_uninit(&gh);
181 gfs2_glock_dq(&ip->i_iopen_gh);
183 gfs2_glock_put(io_gl);
185 ip->i_gl->gl_object = NULL;
186 gfs2_glock_put(ip->i_gl);
189 return ERR_PTR(error);
192 static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
194 struct gfs2_dinode_host *di = &ip->i_di;
195 const struct gfs2_dinode *str = buf;
197 if (ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)) {
198 if (gfs2_consist_inode(ip))
199 gfs2_dinode_print(ip);
202 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
203 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
204 ip->i_inode.i_rdev = 0;
205 switch (ip->i_inode.i_mode & S_IFMT) {
208 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
209 be32_to_cpu(str->di_minor));
213 ip->i_inode.i_uid = be32_to_cpu(str->di_uid);
214 ip->i_inode.i_gid = be32_to_cpu(str->di_gid);
216 * We will need to review setting the nlink count here in the
217 * light of the forthcoming ro bind mount work. This is a reminder
220 ip->i_inode.i_nlink = be32_to_cpu(str->di_nlink);
221 di->di_size = be64_to_cpu(str->di_size);
222 i_size_write(&ip->i_inode, di->di_size);
223 di->di_blocks = be64_to_cpu(str->di_blocks);
224 gfs2_set_inode_blocks(&ip->i_inode);
225 ip->i_inode.i_atime.tv_sec = be64_to_cpu(str->di_atime);
226 ip->i_inode.i_atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
227 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
228 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
229 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
230 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
232 di->di_goal_meta = be64_to_cpu(str->di_goal_meta);
233 di->di_goal_data = be64_to_cpu(str->di_goal_data);
234 di->di_generation = be64_to_cpu(str->di_generation);
236 di->di_flags = be32_to_cpu(str->di_flags);
237 gfs2_set_inode_flags(&ip->i_inode);
238 di->di_height = be16_to_cpu(str->di_height);
240 di->di_depth = be16_to_cpu(str->di_depth);
241 di->di_entries = be32_to_cpu(str->di_entries);
243 di->di_eattr = be64_to_cpu(str->di_eattr);
247 static void gfs2_inode_bh(struct gfs2_inode *ip, struct buffer_head *bh)
253 * gfs2_inode_refresh - Refresh the incore copy of the dinode
254 * @ip: The GFS2 inode
259 int gfs2_inode_refresh(struct gfs2_inode *ip)
261 struct buffer_head *dibh;
264 error = gfs2_meta_inode_buffer(ip, &dibh);
268 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), dibh, GFS2_METATYPE_DI)) {
273 error = gfs2_dinode_in(ip, dibh->b_data);
275 clear_bit(GIF_INVALID, &ip->i_flags);
280 int gfs2_dinode_dealloc(struct gfs2_inode *ip)
282 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
283 struct gfs2_alloc *al;
284 struct gfs2_rgrpd *rgd;
287 if (ip->i_di.di_blocks != 1) {
288 if (gfs2_consist_inode(ip))
289 gfs2_dinode_print(ip);
293 al = gfs2_alloc_get(ip);
295 error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
299 error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
303 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
305 gfs2_consist_inode(ip);
307 goto out_rindex_relse;
310 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0,
313 goto out_rindex_relse;
315 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, 1);
319 gfs2_trans_add_gl(ip->i_gl);
321 gfs2_free_di(rgd, ip);
324 clear_bit(GLF_STICKY, &ip->i_gl->gl_flags);
327 gfs2_glock_dq_uninit(&al->al_rgd_gh);
329 gfs2_glock_dq_uninit(&al->al_ri_gh);
331 gfs2_quota_unhold(ip);
338 * gfs2_change_nlink - Change nlink count on inode
339 * @ip: The GFS2 inode
340 * @diff: The change in the nlink count required
344 int gfs2_change_nlink(struct gfs2_inode *ip, int diff)
346 struct buffer_head *dibh;
350 BUG_ON(diff != 1 && diff != -1);
351 nlink = ip->i_inode.i_nlink + diff;
353 /* If we are reducing the nlink count, but the new value ends up being
354 bigger than the old one, we must have underflowed. */
355 if (diff < 0 && nlink > ip->i_inode.i_nlink) {
356 if (gfs2_consist_inode(ip))
357 gfs2_dinode_print(ip);
361 error = gfs2_meta_inode_buffer(ip, &dibh);
366 inc_nlink(&ip->i_inode);
368 drop_nlink(&ip->i_inode);
370 ip->i_inode.i_ctime = CURRENT_TIME;
372 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
373 gfs2_dinode_out(ip, dibh->b_data);
375 mark_inode_dirty(&ip->i_inode);
377 if (ip->i_inode.i_nlink == 0)
378 gfs2_unlink_di(&ip->i_inode); /* mark inode unlinked */
383 struct inode *gfs2_lookup_simple(struct inode *dip, const char *name)
387 gfs2_str2qstr(&qstr, name);
388 inode = gfs2_lookupi(dip, &qstr, 1, NULL);
389 /* gfs2_lookupi has inconsistent callers: vfs
390 * related routines expect NULL for no entry found,
391 * gfs2_lookup_simple callers expect ENOENT
392 * and do not check for NULL.
395 return ERR_PTR(-ENOENT);
402 * gfs2_lookupi - Look up a filename in a directory and return its inode
403 * @d_gh: An initialized holder for the directory glock
404 * @name: The name of the inode to look for
405 * @is_root: If 1, ignore the caller's permissions
406 * @i_gh: An uninitialized holder for the new inode glock
408 * This can be called via the VFS filldir function when NFS is doing
409 * a readdirplus and the inode which its intending to stat isn't
410 * already in cache. In this case we must not take the directory glock
411 * again, since the readdir call will have already taken that lock.
416 struct inode *gfs2_lookupi(struct inode *dir, const struct qstr *name,
417 int is_root, struct nameidata *nd)
419 struct super_block *sb = dir->i_sb;
420 struct gfs2_inode *dip = GFS2_I(dir);
421 struct gfs2_holder d_gh;
423 struct inode *inode = NULL;
426 if (!name->len || name->len > GFS2_FNAMESIZE)
427 return ERR_PTR(-ENAMETOOLONG);
429 if ((name->len == 1 && memcmp(name->name, ".", 1) == 0) ||
430 (name->len == 2 && memcmp(name->name, "..", 2) == 0 &&
431 dir == sb->s_root->d_inode)) {
436 if (gfs2_glock_is_locked_by_me(dip->i_gl) == 0) {
437 error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &d_gh);
439 return ERR_PTR(error);
444 error = permission(dir, MAY_EXEC, NULL);
449 inode = gfs2_dir_search(dir, name);
451 error = PTR_ERR(inode);
454 gfs2_glock_dq_uninit(&d_gh);
455 if (error == -ENOENT)
457 return inode ? inode : ERR_PTR(error);
460 static void gfs2_inum_range_in(struct gfs2_inum_range_host *ir, const void *buf)
462 const struct gfs2_inum_range *str = buf;
464 ir->ir_start = be64_to_cpu(str->ir_start);
465 ir->ir_length = be64_to_cpu(str->ir_length);
468 static void gfs2_inum_range_out(const struct gfs2_inum_range_host *ir, void *buf)
470 struct gfs2_inum_range *str = buf;
472 str->ir_start = cpu_to_be64(ir->ir_start);
473 str->ir_length = cpu_to_be64(ir->ir_length);
476 static int pick_formal_ino_1(struct gfs2_sbd *sdp, u64 *formal_ino)
478 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
479 struct buffer_head *bh;
480 struct gfs2_inum_range_host ir;
483 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
486 mutex_lock(&sdp->sd_inum_mutex);
488 error = gfs2_meta_inode_buffer(ip, &bh);
490 mutex_unlock(&sdp->sd_inum_mutex);
495 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
498 *formal_ino = ir.ir_start++;
500 gfs2_trans_add_bh(ip->i_gl, bh, 1);
501 gfs2_inum_range_out(&ir,
502 bh->b_data + sizeof(struct gfs2_dinode));
504 mutex_unlock(&sdp->sd_inum_mutex);
511 mutex_unlock(&sdp->sd_inum_mutex);
517 static int pick_formal_ino_2(struct gfs2_sbd *sdp, u64 *formal_ino)
519 struct gfs2_inode *ip = GFS2_I(sdp->sd_ir_inode);
520 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_inum_inode);
521 struct gfs2_holder gh;
522 struct buffer_head *bh;
523 struct gfs2_inum_range_host ir;
526 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
530 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
533 mutex_lock(&sdp->sd_inum_mutex);
535 error = gfs2_meta_inode_buffer(ip, &bh);
539 gfs2_inum_range_in(&ir, bh->b_data + sizeof(struct gfs2_dinode));
542 struct buffer_head *m_bh;
546 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
550 z = *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode));
551 x = y = be64_to_cpu(z);
553 ir.ir_length = GFS2_INUM_QUANTUM;
554 x += GFS2_INUM_QUANTUM;
556 gfs2_consist_inode(m_ip);
558 gfs2_trans_add_bh(m_ip->i_gl, m_bh, 1);
559 *(__be64 *)(m_bh->b_data + sizeof(struct gfs2_dinode)) = z;
564 *formal_ino = ir.ir_start++;
567 gfs2_trans_add_bh(ip->i_gl, bh, 1);
568 gfs2_inum_range_out(&ir, bh->b_data + sizeof(struct gfs2_dinode));
573 mutex_unlock(&sdp->sd_inum_mutex);
576 gfs2_glock_dq_uninit(&gh);
580 static int pick_formal_ino(struct gfs2_sbd *sdp, u64 *inum)
584 error = pick_formal_ino_1(sdp, inum);
588 error = pick_formal_ino_2(sdp, inum);
594 * create_ok - OK to create a new on-disk inode here?
595 * @dip: Directory in which dinode is to be created
596 * @name: Name of new dinode
602 static int create_ok(struct gfs2_inode *dip, const struct qstr *name,
607 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
611 /* Don't create entries in an unlinked directory */
612 if (!dip->i_inode.i_nlink)
615 error = gfs2_dir_check(&dip->i_inode, name, NULL);
626 if (dip->i_di.di_entries == (u32)-1)
628 if (S_ISDIR(mode) && dip->i_inode.i_nlink == (u32)-1)
634 static void munge_mode_uid_gid(struct gfs2_inode *dip, unsigned int *mode,
635 unsigned int *uid, unsigned int *gid)
637 if (GFS2_SB(&dip->i_inode)->sd_args.ar_suiddir &&
638 (dip->i_inode.i_mode & S_ISUID) && dip->i_inode.i_uid) {
641 else if (dip->i_inode.i_uid != current->fsuid)
643 *uid = dip->i_inode.i_uid;
645 *uid = current->fsuid;
647 if (dip->i_inode.i_mode & S_ISGID) {
650 *gid = dip->i_inode.i_gid;
652 *gid = current->fsgid;
655 static int alloc_dinode(struct gfs2_inode *dip, u64 *no_addr, u64 *generation)
657 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
662 dip->i_alloc.al_requested = RES_DINODE;
663 error = gfs2_inplace_reserve(dip);
667 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS, 0);
671 *no_addr = gfs2_alloc_di(dip, generation);
676 gfs2_inplace_release(dip);
683 * init_dinode - Fill in a new dinode structure
684 * @dip: the directory this inode is being created in
685 * @gl: The glock covering the new inode
686 * @inum: the inode number
687 * @mode: the file permissions
693 static void init_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
694 const struct gfs2_inum_host *inum, unsigned int mode,
695 unsigned int uid, unsigned int gid,
696 const u64 *generation, dev_t dev, struct buffer_head **bhp)
698 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
699 struct gfs2_dinode *di;
700 struct buffer_head *dibh;
701 struct timespec tv = CURRENT_TIME;
703 dibh = gfs2_meta_new(gl, inum->no_addr);
704 gfs2_trans_add_bh(gl, dibh, 1);
705 gfs2_metatype_set(dibh, GFS2_METATYPE_DI, GFS2_FORMAT_DI);
706 gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
707 di = (struct gfs2_dinode *)dibh->b_data;
709 di->di_num.no_formal_ino = cpu_to_be64(inum->no_formal_ino);
710 di->di_num.no_addr = cpu_to_be64(inum->no_addr);
711 di->di_mode = cpu_to_be32(mode);
712 di->di_uid = cpu_to_be32(uid);
713 di->di_gid = cpu_to_be32(gid);
716 di->di_blocks = cpu_to_be64(1);
717 di->di_atime = di->di_mtime = di->di_ctime = cpu_to_be64(tv.tv_sec);
718 di->di_major = cpu_to_be32(MAJOR(dev));
719 di->di_minor = cpu_to_be32(MINOR(dev));
720 di->di_goal_meta = di->di_goal_data = cpu_to_be64(inum->no_addr);
721 di->di_generation = cpu_to_be64(*generation);
725 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_JDATA) ||
726 gfs2_tune_get(sdp, gt_new_files_jdata))
727 di->di_flags |= cpu_to_be32(GFS2_DIF_JDATA);
728 if ((dip->i_di.di_flags & GFS2_DIF_INHERIT_DIRECTIO) ||
729 gfs2_tune_get(sdp, gt_new_files_directio))
730 di->di_flags |= cpu_to_be32(GFS2_DIF_DIRECTIO);
731 } else if (S_ISDIR(mode)) {
732 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
733 GFS2_DIF_INHERIT_DIRECTIO);
734 di->di_flags |= cpu_to_be32(dip->i_di.di_flags &
735 GFS2_DIF_INHERIT_JDATA);
739 di->di_payload_format = cpu_to_be32(S_ISDIR(mode) ? GFS2_FORMAT_DE : 0);
745 memset(&di->__pad4, 0, sizeof(di->__pad4));
747 di->di_atime_nsec = cpu_to_be32(tv.tv_nsec);
748 di->di_mtime_nsec = cpu_to_be32(tv.tv_nsec);
749 di->di_ctime_nsec = cpu_to_be32(tv.tv_nsec);
750 memset(&di->di_reserved, 0, sizeof(di->di_reserved));
752 set_buffer_uptodate(dibh);
757 static int make_dinode(struct gfs2_inode *dip, struct gfs2_glock *gl,
758 unsigned int mode, const struct gfs2_inum_host *inum,
759 const u64 *generation, dev_t dev, struct buffer_head **bhp)
761 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
762 unsigned int uid, gid;
765 munge_mode_uid_gid(dip, &mode, &uid, &gid);
768 error = gfs2_quota_lock(dip, uid, gid);
772 error = gfs2_quota_check(dip, uid, gid);
776 error = gfs2_trans_begin(sdp, RES_DINODE + RES_QUOTA, 0);
780 init_dinode(dip, gl, inum, mode, uid, gid, generation, dev, bhp);
781 gfs2_quota_change(dip, +1, uid, gid);
785 gfs2_quota_unlock(dip);
791 static int link_dinode(struct gfs2_inode *dip, const struct qstr *name,
792 struct gfs2_inode *ip)
794 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
795 struct gfs2_alloc *al;
797 struct buffer_head *dibh;
800 al = gfs2_alloc_get(dip);
802 error = gfs2_quota_lock(dip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
806 error = alloc_required = gfs2_diradd_alloc_required(&dip->i_inode, name);
807 if (alloc_required < 0)
809 if (alloc_required) {
810 error = gfs2_quota_check(dip, dip->i_inode.i_uid, dip->i_inode.i_gid);
812 goto fail_quota_locks;
814 al->al_requested = sdp->sd_max_dirres;
816 error = gfs2_inplace_reserve(dip);
818 goto fail_quota_locks;
820 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
821 al->al_rgd->rd_length +
823 RES_STATFS + RES_QUOTA, 0);
827 error = gfs2_trans_begin(sdp, RES_LEAF + 2 * RES_DINODE, 0);
829 goto fail_quota_locks;
832 error = gfs2_dir_add(&dip->i_inode, name, ip, IF2DT(ip->i_inode.i_mode));
836 error = gfs2_meta_inode_buffer(ip, &dibh);
839 ip->i_inode.i_nlink = 1;
840 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
841 gfs2_dinode_out(ip, dibh->b_data);
849 if (dip->i_alloc.al_rgd)
850 gfs2_inplace_release(dip);
853 gfs2_quota_unlock(dip);
860 static int gfs2_security_init(struct gfs2_inode *dip, struct gfs2_inode *ip)
866 struct gfs2_ea_request er;
868 err = security_inode_init_security(&ip->i_inode, &dip->i_inode,
869 &name, &value, &len);
872 if (err == -EOPNOTSUPP)
877 memset(&er, 0, sizeof(struct gfs2_ea_request));
879 er.er_type = GFS2_EATYPE_SECURITY;
882 er.er_name_len = strlen(name);
883 er.er_data_len = len;
885 err = gfs2_ea_set_i(ip, &er);
894 * gfs2_createi - Create a new inode
895 * @ghs: An array of two holders
896 * @name: The name of the new file
897 * @mode: the permissions on the new inode
899 * @ghs[0] is an initialized holder for the directory
900 * @ghs[1] is the holder for the inode lock
902 * If the return value is not NULL, the glocks on both the directory and the new
903 * file are held. A transaction has been started and an inplace reservation
909 struct inode *gfs2_createi(struct gfs2_holder *ghs, const struct qstr *name,
910 unsigned int mode, dev_t dev)
912 struct inode *inode = NULL;
913 struct gfs2_inode *dip = ghs->gh_gl->gl_object;
914 struct inode *dir = &dip->i_inode;
915 struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
916 struct gfs2_inum_host inum = { .no_addr = 0, .no_formal_ino = 0 };
919 struct buffer_head *bh=NULL;
921 if (!name->len || name->len > GFS2_FNAMESIZE)
922 return ERR_PTR(-ENAMETOOLONG);
924 gfs2_holder_reinit(LM_ST_EXCLUSIVE, 0, ghs);
925 error = gfs2_glock_nq(ghs);
929 error = create_ok(dip, name, mode);
933 error = pick_formal_ino(sdp, &inum.no_formal_ino);
937 error = alloc_dinode(dip, &inum.no_addr, &generation);
941 error = gfs2_glock_nq_num(sdp, inum.no_addr, &gfs2_inode_glops,
942 LM_ST_EXCLUSIVE, GL_SKIP, ghs + 1);
946 error = make_dinode(dip, ghs[1].gh_gl, mode, &inum, &generation, dev, &bh);
950 inode = gfs2_inode_lookup(dir->i_sb, IF2DT(mode),
956 gfs2_inode_bh(GFS2_I(inode), bh);
958 error = gfs2_inode_refresh(GFS2_I(inode));
962 error = gfs2_acl_create(dip, GFS2_I(inode));
966 error = gfs2_security_init(dip, GFS2_I(inode));
970 error = link_dinode(dip, name, GFS2_I(inode));
975 return ERR_PTR(-ENOMEM);
979 gfs2_glock_dq_uninit(ghs + 1);
985 return ERR_PTR(error);
989 * gfs2_rmdiri - Remove a directory
990 * @dip: The parent directory of the directory to be removed
991 * @name: The name of the directory to be removed
992 * @ip: The GFS2 inode of the directory to be removed
994 * Assumes Glocks on dip and ip are held
999 int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
1000 struct gfs2_inode *ip)
1002 struct qstr dotname;
1005 if (ip->i_di.di_entries != 2) {
1006 if (gfs2_consist_inode(ip))
1007 gfs2_dinode_print(ip);
1011 error = gfs2_dir_del(dip, name);
1015 error = gfs2_change_nlink(dip, -1);
1019 gfs2_str2qstr(&dotname, ".");
1020 error = gfs2_dir_del(ip, &dotname);
1024 gfs2_str2qstr(&dotname, "..");
1025 error = gfs2_dir_del(ip, &dotname);
1029 /* It looks odd, but it really should be done twice */
1030 error = gfs2_change_nlink(ip, -1);
1034 error = gfs2_change_nlink(ip, -1);
1042 * gfs2_unlink_ok - check to see that a inode is still in a directory
1043 * @dip: the directory
1044 * @name: the name of the file
1047 * Assumes that the lock on (at least) @dip is held.
1049 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
1052 int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
1053 const struct gfs2_inode *ip)
1057 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
1060 if ((dip->i_inode.i_mode & S_ISVTX) &&
1061 dip->i_inode.i_uid != current->fsuid &&
1062 ip->i_inode.i_uid != current->fsuid && !capable(CAP_FOWNER))
1065 if (IS_APPEND(&dip->i_inode))
1068 error = permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, NULL);
1072 error = gfs2_dir_check(&dip->i_inode, name, ip);
1080 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
1084 * Follow @to back to the root and make sure we don't encounter @this
1085 * Assumes we already hold the rename lock.
1090 int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
1092 struct inode *dir = &to->i_inode;
1093 struct super_block *sb = dir->i_sb;
1098 gfs2_str2qstr(&dotdot, "..");
1103 if (dir == &this->i_inode) {
1107 if (dir == sb->s_root->d_inode) {
1112 tmp = gfs2_lookupi(dir, &dotdot, 1, NULL);
1114 error = PTR_ERR(tmp);
1128 * gfs2_readlinki - return the contents of a symlink
1129 * @ip: the symlink's inode
1130 * @buf: a pointer to the buffer to be filled
1131 * @len: a pointer to the length of @buf
1133 * If @buf is too small, a piece of memory is kmalloc()ed and needs
1134 * to be freed by the caller.
1139 int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
1141 struct gfs2_holder i_gh;
1142 struct buffer_head *dibh;
1146 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
1147 error = gfs2_glock_nq_atime(&i_gh);
1149 gfs2_holder_uninit(&i_gh);
1153 if (!ip->i_di.di_size) {
1154 gfs2_consist_inode(ip);
1159 error = gfs2_meta_inode_buffer(ip, &dibh);
1163 x = ip->i_di.di_size + 1;
1165 *buf = kmalloc(x, GFP_KERNEL);
1172 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1178 gfs2_glock_dq_uninit(&i_gh);
1183 * gfs2_glock_nq_atime - Acquire a hold on an inode's glock, and
1184 * conditionally update the inode's atime
1185 * @gh: the holder to acquire
1187 * Tests atime (access time) for gfs2_read, gfs2_readdir and gfs2_mmap
1188 * Update if the difference between the current time and the inode's current
1189 * atime is greater than an interval specified at mount.
1194 int gfs2_glock_nq_atime(struct gfs2_holder *gh)
1196 struct gfs2_glock *gl = gh->gh_gl;
1197 struct gfs2_sbd *sdp = gl->gl_sbd;
1198 struct gfs2_inode *ip = gl->gl_object;
1199 s64 quantum = gfs2_tune_get(sdp, gt_atime_quantum);
1203 struct timespec tv = CURRENT_TIME;
1205 if (gfs2_assert_warn(sdp, gh->gh_flags & GL_ATIME) ||
1206 gfs2_assert_warn(sdp, !(gh->gh_flags & GL_ASYNC)) ||
1207 gfs2_assert_warn(sdp, gl->gl_ops == &gfs2_inode_glops))
1210 state = gh->gh_state;
1211 flags = gh->gh_flags;
1213 error = gfs2_glock_nq(gh);
1217 if (test_bit(SDF_NOATIME, &sdp->sd_flags) ||
1218 (sdp->sd_vfs->s_flags & MS_RDONLY))
1221 if (tv.tv_sec - ip->i_inode.i_atime.tv_sec >= quantum) {
1223 gfs2_holder_reinit(LM_ST_EXCLUSIVE, gh->gh_flags & ~LM_FLAG_ANY,
1225 error = gfs2_glock_nq(gh);
1229 /* Verify that atime hasn't been updated while we were
1230 trying to get exclusive lock. */
1233 if (tv.tv_sec - ip->i_inode.i_atime.tv_sec >= quantum) {
1234 struct buffer_head *dibh;
1235 struct gfs2_dinode *di;
1237 error = gfs2_trans_begin(sdp, RES_DINODE, 0);
1238 if (error == -EROFS)
1243 error = gfs2_meta_inode_buffer(ip, &dibh);
1245 goto fail_end_trans;
1247 ip->i_inode.i_atime = tv;
1249 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1250 di = (struct gfs2_dinode *)dibh->b_data;
1251 di->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
1252 di->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
1255 gfs2_trans_end(sdp);
1258 /* If someone else has asked for the glock,
1259 unlock and let them have it. Then reacquire
1260 in the original state. */
1261 if (gfs2_glock_is_blocking(gl)) {
1263 gfs2_holder_reinit(state, flags, gh);
1264 return gfs2_glock_nq(gh);
1271 gfs2_trans_end(sdp);
1278 __gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1280 struct buffer_head *dibh;
1283 error = gfs2_meta_inode_buffer(ip, &dibh);
1285 error = inode_setattr(&ip->i_inode, attr);
1286 gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error);
1287 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1288 gfs2_dinode_out(ip, dibh->b_data);
1295 * gfs2_setattr_simple -
1299 * Called with a reference on the vnode.
1304 int gfs2_setattr_simple(struct gfs2_inode *ip, struct iattr *attr)
1308 if (current->journal_info)
1309 return __gfs2_setattr_simple(ip, attr);
1311 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE, 0);
1315 error = __gfs2_setattr_simple(ip, attr);
1316 gfs2_trans_end(GFS2_SB(&ip->i_inode));
1320 void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
1322 const struct gfs2_dinode_host *di = &ip->i_di;
1323 struct gfs2_dinode *str = buf;
1325 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
1326 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
1327 str->di_header.__pad0 = 0;
1328 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
1329 str->di_header.__pad1 = 0;
1330 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
1331 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
1332 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
1333 str->di_uid = cpu_to_be32(ip->i_inode.i_uid);
1334 str->di_gid = cpu_to_be32(ip->i_inode.i_gid);
1335 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
1336 str->di_size = cpu_to_be64(di->di_size);
1337 str->di_blocks = cpu_to_be64(di->di_blocks);
1338 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
1339 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
1340 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
1342 str->di_goal_meta = cpu_to_be64(di->di_goal_meta);
1343 str->di_goal_data = cpu_to_be64(di->di_goal_data);
1344 str->di_generation = cpu_to_be64(di->di_generation);
1346 str->di_flags = cpu_to_be32(di->di_flags);
1347 str->di_height = cpu_to_be16(di->di_height);
1348 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
1349 !(ip->i_di.di_flags & GFS2_DIF_EXHASH) ?
1350 GFS2_FORMAT_DE : 0);
1351 str->di_depth = cpu_to_be16(di->di_depth);
1352 str->di_entries = cpu_to_be32(di->di_entries);
1354 str->di_eattr = cpu_to_be64(di->di_eattr);
1355 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
1356 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
1357 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
1360 void gfs2_dinode_print(const struct gfs2_inode *ip)
1362 const struct gfs2_dinode_host *di = &ip->i_di;
1364 printk(KERN_INFO " no_formal_ino = %llu\n",
1365 (unsigned long long)ip->i_no_formal_ino);
1366 printk(KERN_INFO " no_addr = %llu\n",
1367 (unsigned long long)ip->i_no_addr);
1368 printk(KERN_INFO " di_size = %llu\n", (unsigned long long)di->di_size);
1369 printk(KERN_INFO " di_blocks = %llu\n",
1370 (unsigned long long)di->di_blocks);
1371 printk(KERN_INFO " di_goal_meta = %llu\n",
1372 (unsigned long long)di->di_goal_meta);
1373 printk(KERN_INFO " di_goal_data = %llu\n",
1374 (unsigned long long)di->di_goal_data);
1375 printk(KERN_INFO " di_flags = 0x%.8X\n", di->di_flags);
1376 printk(KERN_INFO " di_height = %u\n", di->di_height);
1377 printk(KERN_INFO " di_depth = %u\n", di->di_depth);
1378 printk(KERN_INFO " di_entries = %u\n", di->di_entries);
1379 printk(KERN_INFO " di_eattr = %llu\n",
1380 (unsigned long long)di->di_eattr);