2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2008 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/security.h>
20 #include <linux/time.h>
38 struct gfs2_skip_data
{
44 static int iget_test(struct inode
*inode
, void *opaque
)
46 struct gfs2_inode
*ip
= GFS2_I(inode
);
47 struct gfs2_skip_data
*data
= opaque
;
49 if (ip
->i_no_addr
== data
->no_addr
) {
50 if (data
->non_block
&&
51 inode
->i_state
& (I_FREEING
|I_CLEAR
|I_WILL_FREE
)) {
60 static int iget_set(struct inode
*inode
, void *opaque
)
62 struct gfs2_inode
*ip
= GFS2_I(inode
);
63 struct gfs2_skip_data
*data
= opaque
;
67 inode
->i_ino
= (unsigned long)(data
->no_addr
);
68 ip
->i_no_addr
= data
->no_addr
;
72 struct inode
*gfs2_ilookup(struct super_block
*sb
, u64 no_addr
, int non_block
)
74 unsigned long hash
= (unsigned long)no_addr
;
75 struct gfs2_skip_data data
;
77 data
.no_addr
= no_addr
;
79 data
.non_block
= non_block
;
80 return ilookup5(sb
, hash
, iget_test
, &data
);
83 static struct inode
*gfs2_iget(struct super_block
*sb
, u64 no_addr
,
86 struct gfs2_skip_data data
;
87 unsigned long hash
= (unsigned long)no_addr
;
89 data
.no_addr
= no_addr
;
91 data
.non_block
= non_block
;
92 return iget5_locked(sb
, hash
, iget_test
, iget_set
, &data
);
96 * gfs2_set_iop - Sets inode operations
97 * @inode: The inode with correct i_mode filled in
99 * GFS2 lookup code fills in vfs inode contents based on info obtained
100 * from directory entry inside gfs2_inode_lookup().
103 static void gfs2_set_iop(struct inode
*inode
)
105 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
106 umode_t mode
= inode
->i_mode
;
109 inode
->i_op
= &gfs2_file_iops
;
110 if (gfs2_localflocks(sdp
))
111 inode
->i_fop
= &gfs2_file_fops_nolock
;
113 inode
->i_fop
= &gfs2_file_fops
;
114 } else if (S_ISDIR(mode
)) {
115 inode
->i_op
= &gfs2_dir_iops
;
116 if (gfs2_localflocks(sdp
))
117 inode
->i_fop
= &gfs2_dir_fops_nolock
;
119 inode
->i_fop
= &gfs2_dir_fops
;
120 } else if (S_ISLNK(mode
)) {
121 inode
->i_op
= &gfs2_symlink_iops
;
123 inode
->i_op
= &gfs2_file_iops
;
124 init_special_inode(inode
, inode
->i_mode
, inode
->i_rdev
);
129 * gfs2_inode_lookup - Lookup an inode
130 * @sb: The super block
131 * @no_addr: The inode number
132 * @type: The type of the inode
133 * non_block: Can we block on inodes that are being freed?
135 * Returns: A VFS inode, or an error
138 struct inode
*gfs2_inode_lookup(struct super_block
*sb
, unsigned int type
,
139 u64 no_addr
, u64 no_formal_ino
, int non_block
)
142 struct gfs2_inode
*ip
;
143 struct gfs2_glock
*io_gl
= NULL
;
146 inode
= gfs2_iget(sb
, no_addr
, non_block
);
150 return ERR_PTR(-ENOBUFS
);
152 if (inode
->i_state
& I_NEW
) {
153 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
154 ip
->i_no_formal_ino
= no_formal_ino
;
156 error
= gfs2_glock_get(sdp
, no_addr
, &gfs2_inode_glops
, CREATE
, &ip
->i_gl
);
159 ip
->i_gl
->gl_object
= ip
;
161 error
= gfs2_glock_get(sdp
, no_addr
, &gfs2_iopen_glops
, CREATE
, &io_gl
);
165 set_bit(GIF_INVALID
, &ip
->i_flags
);
166 error
= gfs2_glock_nq_init(io_gl
, LM_ST_SHARED
, GL_EXACT
, &ip
->i_iopen_gh
);
170 ip
->i_iopen_gh
.gh_gl
->gl_object
= ip
;
171 gfs2_glock_put(io_gl
);
174 if (type
== DT_UNKNOWN
) {
175 /* Inode glock must be locked already */
176 error
= gfs2_inode_refresh(GFS2_I(inode
));
180 inode
->i_mode
= DT2IF(type
);
184 unlock_new_inode(inode
);
190 ip
->i_iopen_gh
.gh_gl
->gl_object
= NULL
;
191 gfs2_glock_dq_uninit(&ip
->i_iopen_gh
);
194 gfs2_glock_put(io_gl
);
196 ip
->i_gl
->gl_object
= NULL
;
197 gfs2_glock_put(ip
->i_gl
);
200 return ERR_PTR(error
);
203 struct inode
*gfs2_lookup_by_inum(struct gfs2_sbd
*sdp
, u64 no_addr
,
204 u64
*no_formal_ino
, unsigned int blktype
)
206 struct super_block
*sb
= sdp
->sd_vfs
;
207 struct gfs2_holder i_gh
;
208 struct inode
*inode
= NULL
;
211 /* Must not read in block until block type is verified */
212 error
= gfs2_glock_nq_num(sdp
, no_addr
, &gfs2_inode_glops
,
213 LM_ST_EXCLUSIVE
, GL_SKIP
, &i_gh
);
215 return ERR_PTR(error
);
217 error
= gfs2_check_blk_type(sdp
, no_addr
, blktype
);
221 inode
= gfs2_inode_lookup(sb
, DT_UNKNOWN
, no_addr
, 0, 1);
225 /* Two extra checks for NFS only */
228 if (GFS2_I(inode
)->i_no_formal_ino
!= *no_formal_ino
)
232 if (GFS2_I(inode
)->i_diskflags
& GFS2_DIF_SYSTEM
)
239 gfs2_glock_dq_uninit(&i_gh
);
240 return error
? ERR_PTR(error
) : inode
;
247 struct inode
*gfs2_lookup_simple(struct inode
*dip
, const char *name
)
251 gfs2_str2qstr(&qstr
, name
);
252 inode
= gfs2_lookupi(dip
, &qstr
, 1);
253 /* gfs2_lookupi has inconsistent callers: vfs
254 * related routines expect NULL for no entry found,
255 * gfs2_lookup_simple callers expect ENOENT
256 * and do not check for NULL.
259 return ERR_PTR(-ENOENT
);
266 * gfs2_lookupi - Look up a filename in a directory and return its inode
267 * @d_gh: An initialized holder for the directory glock
268 * @name: The name of the inode to look for
269 * @is_root: If 1, ignore the caller's permissions
270 * @i_gh: An uninitialized holder for the new inode glock
272 * This can be called via the VFS filldir function when NFS is doing
273 * a readdirplus and the inode which its intending to stat isn't
274 * already in cache. In this case we must not take the directory glock
275 * again, since the readdir call will have already taken that lock.
280 struct inode
*gfs2_lookupi(struct inode
*dir
, const struct qstr
*name
,
283 struct super_block
*sb
= dir
->i_sb
;
284 struct gfs2_inode
*dip
= GFS2_I(dir
);
285 struct gfs2_holder d_gh
;
287 struct inode
*inode
= NULL
;
290 if (!name
->len
|| name
->len
> GFS2_FNAMESIZE
)
291 return ERR_PTR(-ENAMETOOLONG
);
293 if ((name
->len
== 1 && memcmp(name
->name
, ".", 1) == 0) ||
294 (name
->len
== 2 && memcmp(name
->name
, "..", 2) == 0 &&
295 dir
== sb
->s_root
->d_inode
)) {
300 if (gfs2_glock_is_locked_by_me(dip
->i_gl
) == NULL
) {
301 error
= gfs2_glock_nq_init(dip
->i_gl
, LM_ST_SHARED
, 0, &d_gh
);
303 return ERR_PTR(error
);
308 error
= gfs2_permission(dir
, MAY_EXEC
, 0);
313 inode
= gfs2_dir_search(dir
, name
);
315 error
= PTR_ERR(inode
);
318 gfs2_glock_dq_uninit(&d_gh
);
319 if (error
== -ENOENT
)
321 return inode
? inode
: ERR_PTR(error
);
325 * create_ok - OK to create a new on-disk inode here?
326 * @dip: Directory in which dinode is to be created
327 * @name: Name of new dinode
333 static int create_ok(struct gfs2_inode
*dip
, const struct qstr
*name
,
338 error
= gfs2_permission(&dip
->i_inode
, MAY_WRITE
| MAY_EXEC
, 0);
342 /* Don't create entries in an unlinked directory */
343 if (!dip
->i_inode
.i_nlink
)
346 error
= gfs2_dir_check(&dip
->i_inode
, name
, NULL
);
357 if (dip
->i_entries
== (u32
)-1)
359 if (S_ISDIR(mode
) && dip
->i_inode
.i_nlink
== (u32
)-1)
365 static void munge_mode_uid_gid(struct gfs2_inode
*dip
, unsigned int *mode
,
366 unsigned int *uid
, unsigned int *gid
)
368 if (GFS2_SB(&dip
->i_inode
)->sd_args
.ar_suiddir
&&
369 (dip
->i_inode
.i_mode
& S_ISUID
) && dip
->i_inode
.i_uid
) {
372 else if (dip
->i_inode
.i_uid
!= current_fsuid())
374 *uid
= dip
->i_inode
.i_uid
;
376 *uid
= current_fsuid();
378 if (dip
->i_inode
.i_mode
& S_ISGID
) {
381 *gid
= dip
->i_inode
.i_gid
;
383 *gid
= current_fsgid();
386 static int alloc_dinode(struct gfs2_inode
*dip
, u64
*no_addr
, u64
*generation
)
388 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
391 if (gfs2_alloc_get(dip
) == NULL
)
394 dip
->i_alloc
->al_requested
= RES_DINODE
;
395 error
= gfs2_inplace_reserve(dip
);
399 error
= gfs2_trans_begin(sdp
, RES_RG_BIT
+ RES_STATFS
, 0);
403 error
= gfs2_alloc_di(dip
, no_addr
, generation
);
408 gfs2_inplace_release(dip
);
415 * init_dinode - Fill in a new dinode structure
416 * @dip: the directory this inode is being created in
417 * @gl: The glock covering the new inode
418 * @inum: the inode number
419 * @mode: the file permissions
425 static void init_dinode(struct gfs2_inode
*dip
, struct gfs2_glock
*gl
,
426 const struct gfs2_inum_host
*inum
, unsigned int mode
,
427 unsigned int uid
, unsigned int gid
,
428 const u64
*generation
, dev_t dev
, struct buffer_head
**bhp
)
430 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
431 struct gfs2_dinode
*di
;
432 struct buffer_head
*dibh
;
433 struct timespec tv
= CURRENT_TIME
;
435 dibh
= gfs2_meta_new(gl
, inum
->no_addr
);
436 gfs2_trans_add_bh(gl
, dibh
, 1);
437 gfs2_metatype_set(dibh
, GFS2_METATYPE_DI
, GFS2_FORMAT_DI
);
438 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
439 di
= (struct gfs2_dinode
*)dibh
->b_data
;
441 di
->di_num
.no_formal_ino
= cpu_to_be64(inum
->no_formal_ino
);
442 di
->di_num
.no_addr
= cpu_to_be64(inum
->no_addr
);
443 di
->di_mode
= cpu_to_be32(mode
);
444 di
->di_uid
= cpu_to_be32(uid
);
445 di
->di_gid
= cpu_to_be32(gid
);
448 di
->di_blocks
= cpu_to_be64(1);
449 di
->di_atime
= di
->di_mtime
= di
->di_ctime
= cpu_to_be64(tv
.tv_sec
);
450 di
->di_major
= cpu_to_be32(MAJOR(dev
));
451 di
->di_minor
= cpu_to_be32(MINOR(dev
));
452 di
->di_goal_meta
= di
->di_goal_data
= cpu_to_be64(inum
->no_addr
);
453 di
->di_generation
= cpu_to_be64(*generation
);
457 if ((dip
->i_diskflags
& GFS2_DIF_INHERIT_JDATA
) ||
458 gfs2_tune_get(sdp
, gt_new_files_jdata
))
459 di
->di_flags
|= cpu_to_be32(GFS2_DIF_JDATA
);
460 } else if (S_ISDIR(mode
)) {
461 di
->di_flags
|= cpu_to_be32(dip
->i_diskflags
&
462 GFS2_DIF_INHERIT_JDATA
);
466 di
->di_payload_format
= cpu_to_be32(S_ISDIR(mode
) ? GFS2_FORMAT_DE
: 0);
472 memset(&di
->__pad4
, 0, sizeof(di
->__pad4
));
474 di
->di_atime_nsec
= cpu_to_be32(tv
.tv_nsec
);
475 di
->di_mtime_nsec
= cpu_to_be32(tv
.tv_nsec
);
476 di
->di_ctime_nsec
= cpu_to_be32(tv
.tv_nsec
);
477 memset(&di
->di_reserved
, 0, sizeof(di
->di_reserved
));
479 set_buffer_uptodate(dibh
);
484 static int make_dinode(struct gfs2_inode
*dip
, struct gfs2_glock
*gl
,
485 unsigned int mode
, const struct gfs2_inum_host
*inum
,
486 const u64
*generation
, dev_t dev
, struct buffer_head
**bhp
)
488 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
489 unsigned int uid
, gid
;
492 munge_mode_uid_gid(dip
, &mode
, &uid
, &gid
);
493 if (!gfs2_alloc_get(dip
))
496 error
= gfs2_quota_lock(dip
, uid
, gid
);
500 error
= gfs2_quota_check(dip
, uid
, gid
);
504 error
= gfs2_trans_begin(sdp
, RES_DINODE
+ RES_QUOTA
, 0);
508 init_dinode(dip
, gl
, inum
, mode
, uid
, gid
, generation
, dev
, bhp
);
509 gfs2_quota_change(dip
, +1, uid
, gid
);
513 gfs2_quota_unlock(dip
);
519 static int link_dinode(struct gfs2_inode
*dip
, const struct qstr
*name
,
520 struct gfs2_inode
*ip
)
522 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
523 struct gfs2_alloc
*al
;
525 struct buffer_head
*dibh
;
528 al
= gfs2_alloc_get(dip
);
532 error
= gfs2_quota_lock(dip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
536 error
= alloc_required
= gfs2_diradd_alloc_required(&dip
->i_inode
, name
);
537 if (alloc_required
< 0)
538 goto fail_quota_locks
;
539 if (alloc_required
) {
540 error
= gfs2_quota_check(dip
, dip
->i_inode
.i_uid
, dip
->i_inode
.i_gid
);
542 goto fail_quota_locks
;
544 al
->al_requested
= sdp
->sd_max_dirres
;
546 error
= gfs2_inplace_reserve(dip
);
548 goto fail_quota_locks
;
550 error
= gfs2_trans_begin(sdp
, sdp
->sd_max_dirres
+
551 al
->al_rgd
->rd_length
+
553 RES_STATFS
+ RES_QUOTA
, 0);
557 error
= gfs2_trans_begin(sdp
, RES_LEAF
+ 2 * RES_DINODE
, 0);
559 goto fail_quota_locks
;
562 error
= gfs2_dir_add(&dip
->i_inode
, name
, ip
);
566 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
569 ip
->i_inode
.i_nlink
= 1;
570 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
571 gfs2_dinode_out(ip
, dibh
->b_data
);
579 if (dip
->i_alloc
->al_rgd
)
580 gfs2_inplace_release(dip
);
583 gfs2_quota_unlock(dip
);
590 static int gfs2_security_init(struct gfs2_inode
*dip
, struct gfs2_inode
*ip
,
591 const struct qstr
*qstr
)
598 err
= security_inode_init_security(&ip
->i_inode
, &dip
->i_inode
, qstr
,
599 &name
, &value
, &len
);
602 if (err
== -EOPNOTSUPP
)
607 err
= __gfs2_xattr_set(&ip
->i_inode
, name
, value
, len
, 0,
608 GFS2_EATYPE_SECURITY
);
616 * gfs2_createi - Create a new inode
617 * @ghs: An array of two holders
618 * @name: The name of the new file
619 * @mode: the permissions on the new inode
621 * @ghs[0] is an initialized holder for the directory
622 * @ghs[1] is the holder for the inode lock
624 * If the return value is not NULL, the glocks on both the directory and the new
625 * file are held. A transaction has been started and an inplace reservation
631 struct inode
*gfs2_createi(struct gfs2_holder
*ghs
, const struct qstr
*name
,
632 unsigned int mode
, dev_t dev
)
634 struct inode
*inode
= NULL
;
635 struct gfs2_inode
*dip
= ghs
->gh_gl
->gl_object
;
636 struct inode
*dir
= &dip
->i_inode
;
637 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
638 struct gfs2_inum_host inum
= { .no_addr
= 0, .no_formal_ino
= 0 };
641 struct buffer_head
*bh
= NULL
;
643 if (!name
->len
|| name
->len
> GFS2_FNAMESIZE
)
644 return ERR_PTR(-ENAMETOOLONG
);
646 gfs2_holder_reinit(LM_ST_EXCLUSIVE
, 0, ghs
);
647 error
= gfs2_glock_nq(ghs
);
651 error
= create_ok(dip
, name
, mode
);
655 error
= alloc_dinode(dip
, &inum
.no_addr
, &generation
);
658 inum
.no_formal_ino
= generation
;
660 error
= gfs2_glock_nq_num(sdp
, inum
.no_addr
, &gfs2_inode_glops
,
661 LM_ST_EXCLUSIVE
, GL_SKIP
, ghs
+ 1);
665 error
= make_dinode(dip
, ghs
[1].gh_gl
, mode
, &inum
, &generation
, dev
, &bh
);
669 inode
= gfs2_inode_lookup(dir
->i_sb
, IF2DT(mode
), inum
.no_addr
,
670 inum
.no_formal_ino
, 0);
674 error
= gfs2_inode_refresh(GFS2_I(inode
));
678 error
= gfs2_acl_create(dip
, inode
);
682 error
= gfs2_security_init(dip
, GFS2_I(inode
), name
);
686 error
= link_dinode(dip
, name
, GFS2_I(inode
));
695 gfs2_glock_dq_uninit(ghs
+ 1);
696 if (inode
&& !IS_ERR(inode
))
703 return ERR_PTR(error
);
706 static int __gfs2_setattr_simple(struct gfs2_inode
*ip
, struct iattr
*attr
)
708 struct inode
*inode
= &ip
->i_inode
;
709 struct buffer_head
*dibh
;
712 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
716 setattr_copy(inode
, attr
);
717 mark_inode_dirty(inode
);
718 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
719 gfs2_dinode_out(ip
, dibh
->b_data
);
725 * gfs2_setattr_simple -
729 * Called with a reference on the vnode.
734 int gfs2_setattr_simple(struct gfs2_inode
*ip
, struct iattr
*attr
)
738 if (current
->journal_info
)
739 return __gfs2_setattr_simple(ip
, attr
);
741 error
= gfs2_trans_begin(GFS2_SB(&ip
->i_inode
), RES_DINODE
, 0);
745 error
= __gfs2_setattr_simple(ip
, attr
);
746 gfs2_trans_end(GFS2_SB(&ip
->i_inode
));
750 void gfs2_dinode_out(const struct gfs2_inode
*ip
, void *buf
)
752 struct gfs2_dinode
*str
= buf
;
754 str
->di_header
.mh_magic
= cpu_to_be32(GFS2_MAGIC
);
755 str
->di_header
.mh_type
= cpu_to_be32(GFS2_METATYPE_DI
);
756 str
->di_header
.mh_format
= cpu_to_be32(GFS2_FORMAT_DI
);
757 str
->di_num
.no_addr
= cpu_to_be64(ip
->i_no_addr
);
758 str
->di_num
.no_formal_ino
= cpu_to_be64(ip
->i_no_formal_ino
);
759 str
->di_mode
= cpu_to_be32(ip
->i_inode
.i_mode
);
760 str
->di_uid
= cpu_to_be32(ip
->i_inode
.i_uid
);
761 str
->di_gid
= cpu_to_be32(ip
->i_inode
.i_gid
);
762 str
->di_nlink
= cpu_to_be32(ip
->i_inode
.i_nlink
);
763 str
->di_size
= cpu_to_be64(i_size_read(&ip
->i_inode
));
764 str
->di_blocks
= cpu_to_be64(gfs2_get_inode_blocks(&ip
->i_inode
));
765 str
->di_atime
= cpu_to_be64(ip
->i_inode
.i_atime
.tv_sec
);
766 str
->di_mtime
= cpu_to_be64(ip
->i_inode
.i_mtime
.tv_sec
);
767 str
->di_ctime
= cpu_to_be64(ip
->i_inode
.i_ctime
.tv_sec
);
769 str
->di_goal_meta
= cpu_to_be64(ip
->i_goal
);
770 str
->di_goal_data
= cpu_to_be64(ip
->i_goal
);
771 str
->di_generation
= cpu_to_be64(ip
->i_generation
);
773 str
->di_flags
= cpu_to_be32(ip
->i_diskflags
);
774 str
->di_height
= cpu_to_be16(ip
->i_height
);
775 str
->di_payload_format
= cpu_to_be32(S_ISDIR(ip
->i_inode
.i_mode
) &&
776 !(ip
->i_diskflags
& GFS2_DIF_EXHASH
) ?
778 str
->di_depth
= cpu_to_be16(ip
->i_depth
);
779 str
->di_entries
= cpu_to_be32(ip
->i_entries
);
781 str
->di_eattr
= cpu_to_be64(ip
->i_eattr
);
782 str
->di_atime_nsec
= cpu_to_be32(ip
->i_inode
.i_atime
.tv_nsec
);
783 str
->di_mtime_nsec
= cpu_to_be32(ip
->i_inode
.i_mtime
.tv_nsec
);
784 str
->di_ctime_nsec
= cpu_to_be32(ip
->i_inode
.i_ctime
.tv_nsec
);
This page took 0.046356 seconds and 5 git commands to generate.