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_inum_range_host
{
43 static int iget_test(struct inode
*inode
, void *opaque
)
45 struct gfs2_inode
*ip
= GFS2_I(inode
);
46 u64
*no_addr
= opaque
;
48 if (ip
->i_no_addr
== *no_addr
)
54 static int iget_set(struct inode
*inode
, void *opaque
)
56 struct gfs2_inode
*ip
= GFS2_I(inode
);
57 u64
*no_addr
= opaque
;
59 inode
->i_ino
= (unsigned long)*no_addr
;
60 ip
->i_no_addr
= *no_addr
;
64 struct inode
*gfs2_ilookup(struct super_block
*sb
, u64 no_addr
)
66 unsigned long hash
= (unsigned long)no_addr
;
67 return ilookup5(sb
, hash
, iget_test
, &no_addr
);
70 static struct inode
*gfs2_iget(struct super_block
*sb
, u64 no_addr
)
72 unsigned long hash
= (unsigned long)no_addr
;
73 return iget5_locked(sb
, hash
, iget_test
, iget_set
, &no_addr
);
76 struct gfs2_skip_data
{
81 static int iget_skip_test(struct inode
*inode
, void *opaque
)
83 struct gfs2_inode
*ip
= GFS2_I(inode
);
84 struct gfs2_skip_data
*data
= opaque
;
86 if (ip
->i_no_addr
== data
->no_addr
) {
87 if (inode
->i_state
& (I_FREEING
|I_WILL_FREE
)){
96 static int iget_skip_set(struct inode
*inode
, void *opaque
)
98 struct gfs2_inode
*ip
= GFS2_I(inode
);
99 struct gfs2_skip_data
*data
= opaque
;
103 inode
->i_ino
= (unsigned long)(data
->no_addr
);
104 ip
->i_no_addr
= data
->no_addr
;
108 static struct inode
*gfs2_iget_skip(struct super_block
*sb
,
111 struct gfs2_skip_data data
;
112 unsigned long hash
= (unsigned long)no_addr
;
114 data
.no_addr
= no_addr
;
116 return iget5_locked(sb
, hash
, iget_skip_test
, iget_skip_set
, &data
);
120 * GFS2 lookup code fills in vfs inode contents based on info obtained
121 * from directory entry inside gfs2_inode_lookup(). This has caused issues
122 * with NFS code path since its get_dentry routine doesn't have the relevant
123 * directory entry when gfs2_inode_lookup() is invoked. Part of the code
124 * segment inside gfs2_inode_lookup code needs to get moved around.
126 * Clears I_NEW as well.
129 void gfs2_set_iop(struct inode
*inode
)
131 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
132 umode_t mode
= inode
->i_mode
;
135 inode
->i_op
= &gfs2_file_iops
;
136 if (gfs2_localflocks(sdp
))
137 inode
->i_fop
= &gfs2_file_fops_nolock
;
139 inode
->i_fop
= &gfs2_file_fops
;
140 } else if (S_ISDIR(mode
)) {
141 inode
->i_op
= &gfs2_dir_iops
;
142 if (gfs2_localflocks(sdp
))
143 inode
->i_fop
= &gfs2_dir_fops_nolock
;
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_file_iops
;
150 init_special_inode(inode
, inode
->i_mode
, inode
->i_rdev
);
153 unlock_new_inode(inode
);
157 * gfs2_inode_lookup - Lookup an inode
158 * @sb: The super block
159 * @no_addr: The inode number
160 * @type: The type of the inode
162 * Returns: A VFS inode, or an error
165 struct inode
*gfs2_inode_lookup(struct super_block
*sb
,
171 struct gfs2_inode
*ip
;
172 struct gfs2_glock
*io_gl
= NULL
;
175 inode
= gfs2_iget(sb
, no_addr
);
179 return ERR_PTR(-ENOBUFS
);
181 if (inode
->i_state
& I_NEW
) {
182 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
183 ip
->i_no_formal_ino
= no_formal_ino
;
185 error
= gfs2_glock_get(sdp
, no_addr
, &gfs2_inode_glops
, CREATE
, &ip
->i_gl
);
188 ip
->i_gl
->gl_object
= ip
;
190 error
= gfs2_glock_get(sdp
, no_addr
, &gfs2_iopen_glops
, CREATE
, &io_gl
);
194 set_bit(GIF_INVALID
, &ip
->i_flags
);
195 error
= gfs2_glock_nq_init(io_gl
, LM_ST_SHARED
, GL_EXACT
, &ip
->i_iopen_gh
);
198 ip
->i_iopen_gh
.gh_gl
->gl_object
= ip
;
200 gfs2_glock_put(io_gl
);
203 if ((type
== DT_UNKNOWN
) && (no_formal_ino
== 0))
206 inode
->i_mode
= DT2IF(type
);
209 * We must read the inode in order to work out its type in
210 * this case. Note that this doesn't happen often as we normally
211 * know the type beforehand. This code path only occurs during
212 * unlinked inode recovery (where it is safe to do this glock,
213 * which is not true in the general case).
215 if (type
== DT_UNKNOWN
) {
216 struct gfs2_holder gh
;
217 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &gh
);
220 /* Inode is now uptodate */
221 gfs2_glock_dq_uninit(&gh
);
230 gfs2_glock_dq(&ip
->i_iopen_gh
);
233 gfs2_glock_put(io_gl
);
235 if (inode
->i_state
& I_NEW
)
236 ip
->i_gl
->gl_object
= NULL
;
237 gfs2_glock_put(ip
->i_gl
);
239 if (inode
->i_state
& I_NEW
)
243 return ERR_PTR(error
);
247 * gfs2_process_unlinked_inode - Lookup an unlinked inode for reclamation
248 * and try to reclaim it by doing iput.
250 * This function assumes no rgrp locks are currently held.
252 * @sb: The super block
253 * no_addr: The inode number
257 void gfs2_process_unlinked_inode(struct super_block
*sb
, u64 no_addr
)
259 struct gfs2_sbd
*sdp
;
260 struct gfs2_inode
*ip
;
261 struct gfs2_glock
*io_gl
= NULL
;
263 struct gfs2_holder gh
;
266 inode
= gfs2_iget_skip(sb
, no_addr
);
271 /* If it's not a new inode, someone's using it, so leave it alone. */
272 if (!(inode
->i_state
& I_NEW
)) {
278 sdp
= GFS2_SB(inode
);
279 ip
->i_no_formal_ino
= -1;
281 error
= gfs2_glock_get(sdp
, no_addr
, &gfs2_inode_glops
, CREATE
, &ip
->i_gl
);
284 ip
->i_gl
->gl_object
= ip
;
286 error
= gfs2_glock_get(sdp
, no_addr
, &gfs2_iopen_glops
, CREATE
, &io_gl
);
290 set_bit(GIF_INVALID
, &ip
->i_flags
);
291 error
= gfs2_glock_nq_init(io_gl
, LM_ST_SHARED
, LM_FLAG_TRY
| GL_EXACT
,
296 ip
->i_iopen_gh
.gh_gl
->gl_object
= ip
;
297 gfs2_glock_put(io_gl
);
300 inode
->i_mode
= DT2IF(DT_UNKNOWN
);
303 * We must read the inode in order to work out its type in
304 * this case. Note that this doesn't happen often as we normally
305 * know the type beforehand. This code path only occurs during
306 * unlinked inode recovery (where it is safe to do this glock,
307 * which is not true in the general case).
309 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, LM_FLAG_TRY
,
314 /* Inode is now uptodate */
315 gfs2_glock_dq_uninit(&gh
);
318 /* The iput will cause it to be deleted. */
323 gfs2_glock_dq(&ip
->i_iopen_gh
);
326 gfs2_glock_put(io_gl
);
328 ip
->i_gl
->gl_object
= NULL
;
329 gfs2_glock_put(ip
->i_gl
);
335 static int gfs2_dinode_in(struct gfs2_inode
*ip
, const void *buf
)
337 const struct gfs2_dinode
*str
= buf
;
338 struct timespec atime
;
341 if (unlikely(ip
->i_no_addr
!= be64_to_cpu(str
->di_num
.no_addr
)))
343 ip
->i_no_formal_ino
= be64_to_cpu(str
->di_num
.no_formal_ino
);
344 ip
->i_inode
.i_mode
= be32_to_cpu(str
->di_mode
);
345 ip
->i_inode
.i_rdev
= 0;
346 switch (ip
->i_inode
.i_mode
& S_IFMT
) {
349 ip
->i_inode
.i_rdev
= MKDEV(be32_to_cpu(str
->di_major
),
350 be32_to_cpu(str
->di_minor
));
354 ip
->i_inode
.i_uid
= be32_to_cpu(str
->di_uid
);
355 ip
->i_inode
.i_gid
= be32_to_cpu(str
->di_gid
);
357 * We will need to review setting the nlink count here in the
358 * light of the forthcoming ro bind mount work. This is a reminder
361 ip
->i_inode
.i_nlink
= be32_to_cpu(str
->di_nlink
);
362 ip
->i_disksize
= be64_to_cpu(str
->di_size
);
363 i_size_write(&ip
->i_inode
, ip
->i_disksize
);
364 gfs2_set_inode_blocks(&ip
->i_inode
, be64_to_cpu(str
->di_blocks
));
365 atime
.tv_sec
= be64_to_cpu(str
->di_atime
);
366 atime
.tv_nsec
= be32_to_cpu(str
->di_atime_nsec
);
367 if (timespec_compare(&ip
->i_inode
.i_atime
, &atime
) < 0)
368 ip
->i_inode
.i_atime
= atime
;
369 ip
->i_inode
.i_mtime
.tv_sec
= be64_to_cpu(str
->di_mtime
);
370 ip
->i_inode
.i_mtime
.tv_nsec
= be32_to_cpu(str
->di_mtime_nsec
);
371 ip
->i_inode
.i_ctime
.tv_sec
= be64_to_cpu(str
->di_ctime
);
372 ip
->i_inode
.i_ctime
.tv_nsec
= be32_to_cpu(str
->di_ctime_nsec
);
374 ip
->i_goal
= be64_to_cpu(str
->di_goal_meta
);
375 ip
->i_generation
= be64_to_cpu(str
->di_generation
);
377 ip
->i_diskflags
= be32_to_cpu(str
->di_flags
);
378 gfs2_set_inode_flags(&ip
->i_inode
);
379 height
= be16_to_cpu(str
->di_height
);
380 if (unlikely(height
> GFS2_MAX_META_HEIGHT
))
382 ip
->i_height
= (u8
)height
;
384 depth
= be16_to_cpu(str
->di_depth
);
385 if (unlikely(depth
> GFS2_DIR_MAX_DEPTH
))
387 ip
->i_depth
= (u8
)depth
;
388 ip
->i_entries
= be32_to_cpu(str
->di_entries
);
390 ip
->i_eattr
= be64_to_cpu(str
->di_eattr
);
391 if (S_ISREG(ip
->i_inode
.i_mode
))
392 gfs2_set_aops(&ip
->i_inode
);
396 if (gfs2_consist_inode(ip
))
397 gfs2_dinode_print(ip
);
402 * gfs2_inode_refresh - Refresh the incore copy of the dinode
403 * @ip: The GFS2 inode
408 int gfs2_inode_refresh(struct gfs2_inode
*ip
)
410 struct buffer_head
*dibh
;
413 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
417 if (gfs2_metatype_check(GFS2_SB(&ip
->i_inode
), dibh
, GFS2_METATYPE_DI
)) {
422 error
= gfs2_dinode_in(ip
, dibh
->b_data
);
424 clear_bit(GIF_INVALID
, &ip
->i_flags
);
429 int gfs2_dinode_dealloc(struct gfs2_inode
*ip
)
431 struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
432 struct gfs2_alloc
*al
;
433 struct gfs2_rgrpd
*rgd
;
436 if (gfs2_get_inode_blocks(&ip
->i_inode
) != 1) {
437 if (gfs2_consist_inode(ip
))
438 gfs2_dinode_print(ip
);
442 al
= gfs2_alloc_get(ip
);
446 error
= gfs2_quota_hold(ip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
450 error
= gfs2_rindex_hold(sdp
, &al
->al_ri_gh
);
454 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_no_addr
);
456 gfs2_consist_inode(ip
);
458 goto out_rindex_relse
;
461 error
= gfs2_glock_nq_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0,
464 goto out_rindex_relse
;
466 error
= gfs2_trans_begin(sdp
, RES_RG_BIT
+ RES_STATFS
+ RES_QUOTA
, 1);
470 set_bit(GLF_DIRTY
, &ip
->i_gl
->gl_flags
);
471 set_bit(GLF_LFLUSH
, &ip
->i_gl
->gl_flags
);
473 gfs2_free_di(rgd
, ip
);
478 gfs2_glock_dq_uninit(&al
->al_rgd_gh
);
480 gfs2_glock_dq_uninit(&al
->al_ri_gh
);
482 gfs2_quota_unhold(ip
);
489 * gfs2_change_nlink - Change nlink count on inode
490 * @ip: The GFS2 inode
491 * @diff: The change in the nlink count required
495 int gfs2_change_nlink(struct gfs2_inode
*ip
, int diff
)
497 struct buffer_head
*dibh
;
501 BUG_ON(diff
!= 1 && diff
!= -1);
502 nlink
= ip
->i_inode
.i_nlink
+ diff
;
504 /* If we are reducing the nlink count, but the new value ends up being
505 bigger than the old one, we must have underflowed. */
506 if (diff
< 0 && nlink
> ip
->i_inode
.i_nlink
) {
507 if (gfs2_consist_inode(ip
))
508 gfs2_dinode_print(ip
);
512 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
517 inc_nlink(&ip
->i_inode
);
519 drop_nlink(&ip
->i_inode
);
521 ip
->i_inode
.i_ctime
= CURRENT_TIME
;
523 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
524 gfs2_dinode_out(ip
, dibh
->b_data
);
526 mark_inode_dirty(&ip
->i_inode
);
528 if (ip
->i_inode
.i_nlink
== 0)
529 gfs2_unlink_di(&ip
->i_inode
); /* mark inode unlinked */
534 struct inode
*gfs2_lookup_simple(struct inode
*dip
, const char *name
)
538 gfs2_str2qstr(&qstr
, name
);
539 inode
= gfs2_lookupi(dip
, &qstr
, 1);
540 /* gfs2_lookupi has inconsistent callers: vfs
541 * related routines expect NULL for no entry found,
542 * gfs2_lookup_simple callers expect ENOENT
543 * and do not check for NULL.
546 return ERR_PTR(-ENOENT
);
553 * gfs2_lookupi - Look up a filename in a directory and return its inode
554 * @d_gh: An initialized holder for the directory glock
555 * @name: The name of the inode to look for
556 * @is_root: If 1, ignore the caller's permissions
557 * @i_gh: An uninitialized holder for the new inode glock
559 * This can be called via the VFS filldir function when NFS is doing
560 * a readdirplus and the inode which its intending to stat isn't
561 * already in cache. In this case we must not take the directory glock
562 * again, since the readdir call will have already taken that lock.
567 struct inode
*gfs2_lookupi(struct inode
*dir
, const struct qstr
*name
,
570 struct super_block
*sb
= dir
->i_sb
;
571 struct gfs2_inode
*dip
= GFS2_I(dir
);
572 struct gfs2_holder d_gh
;
574 struct inode
*inode
= NULL
;
577 if (!name
->len
|| name
->len
> GFS2_FNAMESIZE
)
578 return ERR_PTR(-ENAMETOOLONG
);
580 if ((name
->len
== 1 && memcmp(name
->name
, ".", 1) == 0) ||
581 (name
->len
== 2 && memcmp(name
->name
, "..", 2) == 0 &&
582 dir
== sb
->s_root
->d_inode
)) {
587 if (gfs2_glock_is_locked_by_me(dip
->i_gl
) == NULL
) {
588 error
= gfs2_glock_nq_init(dip
->i_gl
, LM_ST_SHARED
, 0, &d_gh
);
590 return ERR_PTR(error
);
595 error
= gfs2_permission(dir
, MAY_EXEC
);
600 inode
= gfs2_dir_search(dir
, name
);
602 error
= PTR_ERR(inode
);
605 gfs2_glock_dq_uninit(&d_gh
);
606 if (error
== -ENOENT
)
608 return inode
? inode
: ERR_PTR(error
);
612 * create_ok - OK to create a new on-disk inode here?
613 * @dip: Directory in which dinode is to be created
614 * @name: Name of new dinode
620 static int create_ok(struct gfs2_inode
*dip
, const struct qstr
*name
,
625 error
= gfs2_permission(&dip
->i_inode
, MAY_WRITE
| MAY_EXEC
);
629 /* Don't create entries in an unlinked directory */
630 if (!dip
->i_inode
.i_nlink
)
633 error
= gfs2_dir_check(&dip
->i_inode
, name
, NULL
);
644 if (dip
->i_entries
== (u32
)-1)
646 if (S_ISDIR(mode
) && dip
->i_inode
.i_nlink
== (u32
)-1)
652 static void munge_mode_uid_gid(struct gfs2_inode
*dip
, unsigned int *mode
,
653 unsigned int *uid
, unsigned int *gid
)
655 if (GFS2_SB(&dip
->i_inode
)->sd_args
.ar_suiddir
&&
656 (dip
->i_inode
.i_mode
& S_ISUID
) && dip
->i_inode
.i_uid
) {
659 else if (dip
->i_inode
.i_uid
!= current_fsuid())
661 *uid
= dip
->i_inode
.i_uid
;
663 *uid
= current_fsuid();
665 if (dip
->i_inode
.i_mode
& S_ISGID
) {
668 *gid
= dip
->i_inode
.i_gid
;
670 *gid
= current_fsgid();
673 static int alloc_dinode(struct gfs2_inode
*dip
, u64
*no_addr
, u64
*generation
)
675 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
678 if (gfs2_alloc_get(dip
) == NULL
)
681 dip
->i_alloc
->al_requested
= RES_DINODE
;
682 error
= gfs2_inplace_reserve(dip
);
686 error
= gfs2_trans_begin(sdp
, RES_RG_BIT
+ RES_STATFS
, 0);
690 error
= gfs2_alloc_di(dip
, no_addr
, generation
);
695 gfs2_inplace_release(dip
);
702 * init_dinode - Fill in a new dinode structure
703 * @dip: the directory this inode is being created in
704 * @gl: The glock covering the new inode
705 * @inum: the inode number
706 * @mode: the file permissions
712 static void init_dinode(struct gfs2_inode
*dip
, struct gfs2_glock
*gl
,
713 const struct gfs2_inum_host
*inum
, unsigned int mode
,
714 unsigned int uid
, unsigned int gid
,
715 const u64
*generation
, dev_t dev
, struct buffer_head
**bhp
)
717 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
718 struct gfs2_dinode
*di
;
719 struct buffer_head
*dibh
;
720 struct timespec tv
= CURRENT_TIME
;
722 dibh
= gfs2_meta_new(gl
, inum
->no_addr
);
723 gfs2_trans_add_bh(gl
, dibh
, 1);
724 gfs2_metatype_set(dibh
, GFS2_METATYPE_DI
, GFS2_FORMAT_DI
);
725 gfs2_buffer_clear_tail(dibh
, sizeof(struct gfs2_dinode
));
726 di
= (struct gfs2_dinode
*)dibh
->b_data
;
728 di
->di_num
.no_formal_ino
= cpu_to_be64(inum
->no_formal_ino
);
729 di
->di_num
.no_addr
= cpu_to_be64(inum
->no_addr
);
730 di
->di_mode
= cpu_to_be32(mode
);
731 di
->di_uid
= cpu_to_be32(uid
);
732 di
->di_gid
= cpu_to_be32(gid
);
735 di
->di_blocks
= cpu_to_be64(1);
736 di
->di_atime
= di
->di_mtime
= di
->di_ctime
= cpu_to_be64(tv
.tv_sec
);
737 di
->di_major
= cpu_to_be32(MAJOR(dev
));
738 di
->di_minor
= cpu_to_be32(MINOR(dev
));
739 di
->di_goal_meta
= di
->di_goal_data
= cpu_to_be64(inum
->no_addr
);
740 di
->di_generation
= cpu_to_be64(*generation
);
744 if ((dip
->i_diskflags
& GFS2_DIF_INHERIT_JDATA
) ||
745 gfs2_tune_get(sdp
, gt_new_files_jdata
))
746 di
->di_flags
|= cpu_to_be32(GFS2_DIF_JDATA
);
747 } else if (S_ISDIR(mode
)) {
748 di
->di_flags
|= cpu_to_be32(dip
->i_diskflags
&
749 GFS2_DIF_INHERIT_JDATA
);
753 di
->di_payload_format
= cpu_to_be32(S_ISDIR(mode
) ? GFS2_FORMAT_DE
: 0);
759 memset(&di
->__pad4
, 0, sizeof(di
->__pad4
));
761 di
->di_atime_nsec
= cpu_to_be32(tv
.tv_nsec
);
762 di
->di_mtime_nsec
= cpu_to_be32(tv
.tv_nsec
);
763 di
->di_ctime_nsec
= cpu_to_be32(tv
.tv_nsec
);
764 memset(&di
->di_reserved
, 0, sizeof(di
->di_reserved
));
766 set_buffer_uptodate(dibh
);
771 static int make_dinode(struct gfs2_inode
*dip
, struct gfs2_glock
*gl
,
772 unsigned int mode
, const struct gfs2_inum_host
*inum
,
773 const u64
*generation
, dev_t dev
, struct buffer_head
**bhp
)
775 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
776 unsigned int uid
, gid
;
779 munge_mode_uid_gid(dip
, &mode
, &uid
, &gid
);
780 if (!gfs2_alloc_get(dip
))
783 error
= gfs2_quota_lock(dip
, uid
, gid
);
787 error
= gfs2_quota_check(dip
, uid
, gid
);
791 error
= gfs2_trans_begin(sdp
, RES_DINODE
+ RES_QUOTA
, 0);
795 init_dinode(dip
, gl
, inum
, mode
, uid
, gid
, generation
, dev
, bhp
);
796 gfs2_quota_change(dip
, +1, uid
, gid
);
800 gfs2_quota_unlock(dip
);
806 static int link_dinode(struct gfs2_inode
*dip
, const struct qstr
*name
,
807 struct gfs2_inode
*ip
)
809 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
810 struct gfs2_alloc
*al
;
812 struct buffer_head
*dibh
;
815 al
= gfs2_alloc_get(dip
);
819 error
= gfs2_quota_lock(dip
, NO_QUOTA_CHANGE
, NO_QUOTA_CHANGE
);
823 error
= alloc_required
= gfs2_diradd_alloc_required(&dip
->i_inode
, name
);
824 if (alloc_required
< 0)
825 goto fail_quota_locks
;
826 if (alloc_required
) {
827 error
= gfs2_quota_check(dip
, dip
->i_inode
.i_uid
, dip
->i_inode
.i_gid
);
829 goto fail_quota_locks
;
831 al
->al_requested
= sdp
->sd_max_dirres
;
833 error
= gfs2_inplace_reserve(dip
);
835 goto fail_quota_locks
;
837 error
= gfs2_trans_begin(sdp
, sdp
->sd_max_dirres
+
838 al
->al_rgd
->rd_length
+
840 RES_STATFS
+ RES_QUOTA
, 0);
844 error
= gfs2_trans_begin(sdp
, RES_LEAF
+ 2 * RES_DINODE
, 0);
846 goto fail_quota_locks
;
849 error
= gfs2_dir_add(&dip
->i_inode
, name
, ip
, IF2DT(ip
->i_inode
.i_mode
));
853 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
856 ip
->i_inode
.i_nlink
= 1;
857 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
858 gfs2_dinode_out(ip
, dibh
->b_data
);
866 if (dip
->i_alloc
->al_rgd
)
867 gfs2_inplace_release(dip
);
870 gfs2_quota_unlock(dip
);
877 static int gfs2_security_init(struct gfs2_inode
*dip
, struct gfs2_inode
*ip
)
884 err
= security_inode_init_security(&ip
->i_inode
, &dip
->i_inode
,
885 &name
, &value
, &len
);
888 if (err
== -EOPNOTSUPP
)
893 err
= __gfs2_xattr_set(&ip
->i_inode
, name
, value
, len
, 0,
894 GFS2_EATYPE_SECURITY
);
902 * gfs2_createi - Create a new inode
903 * @ghs: An array of two holders
904 * @name: The name of the new file
905 * @mode: the permissions on the new inode
907 * @ghs[0] is an initialized holder for the directory
908 * @ghs[1] is the holder for the inode lock
910 * If the return value is not NULL, the glocks on both the directory and the new
911 * file are held. A transaction has been started and an inplace reservation
917 struct inode
*gfs2_createi(struct gfs2_holder
*ghs
, const struct qstr
*name
,
918 unsigned int mode
, dev_t dev
)
920 struct inode
*inode
= NULL
;
921 struct gfs2_inode
*dip
= ghs
->gh_gl
->gl_object
;
922 struct inode
*dir
= &dip
->i_inode
;
923 struct gfs2_sbd
*sdp
= GFS2_SB(&dip
->i_inode
);
924 struct gfs2_inum_host inum
= { .no_addr
= 0, .no_formal_ino
= 0 };
927 struct buffer_head
*bh
= NULL
;
929 if (!name
->len
|| name
->len
> GFS2_FNAMESIZE
)
930 return ERR_PTR(-ENAMETOOLONG
);
932 gfs2_holder_reinit(LM_ST_EXCLUSIVE
, 0, ghs
);
933 error
= gfs2_glock_nq(ghs
);
937 error
= create_ok(dip
, name
, mode
);
941 error
= alloc_dinode(dip
, &inum
.no_addr
, &generation
);
944 inum
.no_formal_ino
= generation
;
946 error
= gfs2_glock_nq_num(sdp
, inum
.no_addr
, &gfs2_inode_glops
,
947 LM_ST_EXCLUSIVE
, GL_SKIP
, ghs
+ 1);
951 error
= make_dinode(dip
, ghs
[1].gh_gl
, mode
, &inum
, &generation
, dev
, &bh
);
955 inode
= gfs2_inode_lookup(dir
->i_sb
, IF2DT(mode
), inum
.no_addr
,
960 error
= gfs2_inode_refresh(GFS2_I(inode
));
964 error
= gfs2_acl_create(dip
, inode
);
968 error
= gfs2_security_init(dip
, GFS2_I(inode
));
972 error
= link_dinode(dip
, name
, GFS2_I(inode
));
981 gfs2_glock_dq_uninit(ghs
+ 1);
982 if (inode
&& !IS_ERR(inode
))
989 return ERR_PTR(error
);
992 static int __gfs2_setattr_simple(struct gfs2_inode
*ip
, struct iattr
*attr
)
994 struct inode
*inode
= &ip
->i_inode
;
995 struct buffer_head
*dibh
;
998 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1002 if ((attr
->ia_valid
& ATTR_SIZE
) &&
1003 attr
->ia_size
!= i_size_read(inode
)) {
1004 error
= vmtruncate(inode
, attr
->ia_size
);
1009 setattr_copy(inode
, attr
);
1010 mark_inode_dirty(inode
);
1012 gfs2_assert_warn(GFS2_SB(inode
), !error
);
1013 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1014 gfs2_dinode_out(ip
, dibh
->b_data
);
1020 * gfs2_setattr_simple -
1024 * Called with a reference on the vnode.
1029 int gfs2_setattr_simple(struct gfs2_inode
*ip
, struct iattr
*attr
)
1033 if (current
->journal_info
)
1034 return __gfs2_setattr_simple(ip
, attr
);
1036 error
= gfs2_trans_begin(GFS2_SB(&ip
->i_inode
), RES_DINODE
, 0);
1040 error
= __gfs2_setattr_simple(ip
, attr
);
1041 gfs2_trans_end(GFS2_SB(&ip
->i_inode
));
1045 void gfs2_dinode_out(const struct gfs2_inode
*ip
, void *buf
)
1047 struct gfs2_dinode
*str
= buf
;
1049 str
->di_header
.mh_magic
= cpu_to_be32(GFS2_MAGIC
);
1050 str
->di_header
.mh_type
= cpu_to_be32(GFS2_METATYPE_DI
);
1051 str
->di_header
.mh_format
= cpu_to_be32(GFS2_FORMAT_DI
);
1052 str
->di_num
.no_addr
= cpu_to_be64(ip
->i_no_addr
);
1053 str
->di_num
.no_formal_ino
= cpu_to_be64(ip
->i_no_formal_ino
);
1054 str
->di_mode
= cpu_to_be32(ip
->i_inode
.i_mode
);
1055 str
->di_uid
= cpu_to_be32(ip
->i_inode
.i_uid
);
1056 str
->di_gid
= cpu_to_be32(ip
->i_inode
.i_gid
);
1057 str
->di_nlink
= cpu_to_be32(ip
->i_inode
.i_nlink
);
1058 str
->di_size
= cpu_to_be64(ip
->i_disksize
);
1059 str
->di_blocks
= cpu_to_be64(gfs2_get_inode_blocks(&ip
->i_inode
));
1060 str
->di_atime
= cpu_to_be64(ip
->i_inode
.i_atime
.tv_sec
);
1061 str
->di_mtime
= cpu_to_be64(ip
->i_inode
.i_mtime
.tv_sec
);
1062 str
->di_ctime
= cpu_to_be64(ip
->i_inode
.i_ctime
.tv_sec
);
1064 str
->di_goal_meta
= cpu_to_be64(ip
->i_goal
);
1065 str
->di_goal_data
= cpu_to_be64(ip
->i_goal
);
1066 str
->di_generation
= cpu_to_be64(ip
->i_generation
);
1068 str
->di_flags
= cpu_to_be32(ip
->i_diskflags
);
1069 str
->di_height
= cpu_to_be16(ip
->i_height
);
1070 str
->di_payload_format
= cpu_to_be32(S_ISDIR(ip
->i_inode
.i_mode
) &&
1071 !(ip
->i_diskflags
& GFS2_DIF_EXHASH
) ?
1072 GFS2_FORMAT_DE
: 0);
1073 str
->di_depth
= cpu_to_be16(ip
->i_depth
);
1074 str
->di_entries
= cpu_to_be32(ip
->i_entries
);
1076 str
->di_eattr
= cpu_to_be64(ip
->i_eattr
);
1077 str
->di_atime_nsec
= cpu_to_be32(ip
->i_inode
.i_atime
.tv_nsec
);
1078 str
->di_mtime_nsec
= cpu_to_be32(ip
->i_inode
.i_mtime
.tv_nsec
);
1079 str
->di_ctime_nsec
= cpu_to_be32(ip
->i_inode
.i_ctime
.tv_nsec
);
1082 void gfs2_dinode_print(const struct gfs2_inode
*ip
)
1084 printk(KERN_INFO
" no_formal_ino = %llu\n",
1085 (unsigned long long)ip
->i_no_formal_ino
);
1086 printk(KERN_INFO
" no_addr = %llu\n",
1087 (unsigned long long)ip
->i_no_addr
);
1088 printk(KERN_INFO
" i_disksize = %llu\n",
1089 (unsigned long long)ip
->i_disksize
);
1090 printk(KERN_INFO
" blocks = %llu\n",
1091 (unsigned long long)gfs2_get_inode_blocks(&ip
->i_inode
));
1092 printk(KERN_INFO
" i_goal = %llu\n",
1093 (unsigned long long)ip
->i_goal
);
1094 printk(KERN_INFO
" i_diskflags = 0x%.8X\n", ip
->i_diskflags
);
1095 printk(KERN_INFO
" i_height = %u\n", ip
->i_height
);
1096 printk(KERN_INFO
" i_depth = %u\n", ip
->i_depth
);
1097 printk(KERN_INFO
" i_entries = %u\n", ip
->i_entries
);
1098 printk(KERN_INFO
" i_eattr = %llu\n",
1099 (unsigned long long)ip
->i_eattr
);