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/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
14 #include <linux/namei.h>
15 #include <linux/utsname.h>
17 #include <linux/xattr.h>
18 #include <linux/posix_acl.h>
19 #include <linux/gfs2_ondisk.h>
20 #include <linux/crc32.h>
21 #include <linux/fiemap.h>
22 #include <asm/uaccess.h>
40 * gfs2_create - Create a file
41 * @dir: The directory in which to create the file
42 * @dentry: The dentry of the new file
43 * @mode: The mode of the new file
48 static int gfs2_create(struct inode
*dir
, struct dentry
*dentry
,
49 int mode
, struct nameidata
*nd
)
51 struct gfs2_inode
*dip
= GFS2_I(dir
);
52 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
53 struct gfs2_holder ghs
[2];
56 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
59 inode
= gfs2_createi(ghs
, &dentry
->d_name
, S_IFREG
| mode
, 0);
62 if (dip
->i_alloc
->al_rgd
)
63 gfs2_inplace_release(dip
);
64 gfs2_quota_unlock(dip
);
66 gfs2_glock_dq_uninit_m(2, ghs
);
67 mark_inode_dirty(inode
);
69 } else if (PTR_ERR(inode
) != -EEXIST
||
70 (nd
&& nd
->flags
& LOOKUP_EXCL
)) {
71 gfs2_holder_uninit(ghs
);
72 return PTR_ERR(inode
);
75 inode
= gfs2_lookupi(dir
, &dentry
->d_name
, 0);
78 gfs2_holder_uninit(ghs
);
81 gfs2_holder_uninit(ghs
);
82 return PTR_ERR(inode
);
87 d_instantiate(dentry
, inode
);
93 * gfs2_lookup - Look up a filename in a directory and return its inode
94 * @dir: The directory inode
95 * @dentry: The dentry of the new inode
96 * @nd: passed from Linux VFS, ignored by us
98 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
103 static struct dentry
*gfs2_lookup(struct inode
*dir
, struct dentry
*dentry
,
104 struct nameidata
*nd
)
106 struct inode
*inode
= NULL
;
108 dentry
->d_op
= &gfs2_dops
;
110 inode
= gfs2_lookupi(dir
, &dentry
->d_name
, 0);
111 if (inode
&& IS_ERR(inode
))
112 return ERR_CAST(inode
);
115 struct gfs2_glock
*gl
= GFS2_I(inode
)->i_gl
;
116 struct gfs2_holder gh
;
118 error
= gfs2_glock_nq_init(gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &gh
);
121 return ERR_PTR(error
);
123 gfs2_glock_dq_uninit(&gh
);
124 return d_splice_alias(inode
, dentry
);
126 d_add(dentry
, inode
);
132 * gfs2_link - Link to a file
133 * @old_dentry: The inode to link
134 * @dir: Add link to this directory
135 * @dentry: The name of the link
137 * Link the inode in "old_dentry" into the directory "dir" with the
143 static int gfs2_link(struct dentry
*old_dentry
, struct inode
*dir
,
144 struct dentry
*dentry
)
146 struct gfs2_inode
*dip
= GFS2_I(dir
);
147 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
148 struct inode
*inode
= old_dentry
->d_inode
;
149 struct gfs2_inode
*ip
= GFS2_I(inode
);
150 struct gfs2_holder ghs
[2];
154 if (S_ISDIR(inode
->i_mode
))
157 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
158 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
160 error
= gfs2_glock_nq(ghs
); /* parent */
164 error
= gfs2_glock_nq(ghs
+ 1); /* child */
168 error
= gfs2_permission(dir
, MAY_WRITE
| MAY_EXEC
);
172 error
= gfs2_dir_check(dir
, &dentry
->d_name
, NULL
);
183 if (!dip
->i_inode
.i_nlink
)
186 if (dip
->i_entries
== (u32
)-1)
189 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
192 if (!ip
->i_inode
.i_nlink
)
195 if (ip
->i_inode
.i_nlink
== (u32
)-1)
198 alloc_required
= error
= gfs2_diradd_alloc_required(dir
, &dentry
->d_name
);
203 if (alloc_required
) {
204 struct gfs2_alloc
*al
= gfs2_alloc_get(dip
);
210 error
= gfs2_quota_lock_check(dip
);
214 al
->al_requested
= sdp
->sd_max_dirres
;
216 error
= gfs2_inplace_reserve(dip
);
220 error
= gfs2_trans_begin(sdp
, sdp
->sd_max_dirres
+
221 al
->al_rgd
->rd_length
+
222 2 * RES_DINODE
+ RES_STATFS
+
227 error
= gfs2_trans_begin(sdp
, 2 * RES_DINODE
+ RES_LEAF
, 0);
232 error
= gfs2_dir_add(dir
, &dentry
->d_name
, ip
, IF2DT(inode
->i_mode
));
236 error
= gfs2_change_nlink(ip
, +1);
242 gfs2_inplace_release(dip
);
245 gfs2_quota_unlock(dip
);
250 gfs2_glock_dq(ghs
+ 1);
254 gfs2_holder_uninit(ghs
);
255 gfs2_holder_uninit(ghs
+ 1);
257 atomic_inc(&inode
->i_count
);
258 d_instantiate(dentry
, inode
);
259 mark_inode_dirty(inode
);
265 * gfs2_unlink_ok - check to see that a inode is still in a directory
266 * @dip: the directory
267 * @name: the name of the file
270 * Assumes that the lock on (at least) @dip is held.
272 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
275 static int gfs2_unlink_ok(struct gfs2_inode
*dip
, const struct qstr
*name
,
276 const struct gfs2_inode
*ip
)
280 if (IS_IMMUTABLE(&ip
->i_inode
) || IS_APPEND(&ip
->i_inode
))
283 if ((dip
->i_inode
.i_mode
& S_ISVTX
) &&
284 dip
->i_inode
.i_uid
!= current_fsuid() &&
285 ip
->i_inode
.i_uid
!= current_fsuid() && !capable(CAP_FOWNER
))
288 if (IS_APPEND(&dip
->i_inode
))
291 error
= gfs2_permission(&dip
->i_inode
, MAY_WRITE
| MAY_EXEC
);
295 error
= gfs2_dir_check(&dip
->i_inode
, name
, ip
);
303 * gfs2_unlink - Unlink a file
304 * @dir: The inode of the directory containing the file to unlink
305 * @dentry: The file itself
307 * Unlink a file. Call gfs2_unlinki()
312 static int gfs2_unlink(struct inode
*dir
, struct dentry
*dentry
)
314 struct gfs2_inode
*dip
= GFS2_I(dir
);
315 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
316 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
317 struct gfs2_holder ghs
[3];
318 struct gfs2_rgrpd
*rgd
;
319 struct gfs2_holder ri_gh
;
322 error
= gfs2_rindex_hold(sdp
, &ri_gh
);
326 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
327 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
329 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_no_addr
);
330 gfs2_holder_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 2);
333 error
= gfs2_glock_nq(ghs
); /* parent */
337 error
= gfs2_glock_nq(ghs
+ 1); /* child */
341 error
= gfs2_glock_nq(ghs
+ 2); /* rgrp */
345 error
= gfs2_unlink_ok(dip
, &dentry
->d_name
, ip
);
349 error
= gfs2_trans_begin(sdp
, 2*RES_DINODE
+ RES_LEAF
+ RES_RG_BIT
, 0);
353 error
= gfs2_dir_del(dip
, &dentry
->d_name
);
357 error
= gfs2_change_nlink(ip
, -1);
362 gfs2_glock_dq(ghs
+ 2);
364 gfs2_holder_uninit(ghs
+ 2);
365 gfs2_glock_dq(ghs
+ 1);
367 gfs2_holder_uninit(ghs
+ 1);
370 gfs2_holder_uninit(ghs
);
371 gfs2_glock_dq_uninit(&ri_gh
);
376 * gfs2_symlink - Create a symlink
377 * @dir: The directory to create the symlink in
378 * @dentry: The dentry to put the symlink in
379 * @symname: The thing which the link points to
384 static int gfs2_symlink(struct inode
*dir
, struct dentry
*dentry
,
387 struct gfs2_inode
*dip
= GFS2_I(dir
), *ip
;
388 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
389 struct gfs2_holder ghs
[2];
391 struct buffer_head
*dibh
;
395 /* Must be stuffed with a null terminator for gfs2_follow_link() */
396 size
= strlen(symname
);
397 if (size
> sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
) - 1)
398 return -ENAMETOOLONG
;
400 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
402 inode
= gfs2_createi(ghs
, &dentry
->d_name
, S_IFLNK
| S_IRWXUGO
, 0);
404 gfs2_holder_uninit(ghs
);
405 return PTR_ERR(inode
);
408 ip
= ghs
[1].gh_gl
->gl_object
;
410 ip
->i_disksize
= size
;
411 i_size_write(inode
, size
);
413 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
415 if (!gfs2_assert_withdraw(sdp
, !error
)) {
416 gfs2_dinode_out(ip
, dibh
->b_data
);
417 memcpy(dibh
->b_data
+ sizeof(struct gfs2_dinode
), symname
,
423 if (dip
->i_alloc
->al_rgd
)
424 gfs2_inplace_release(dip
);
425 gfs2_quota_unlock(dip
);
428 gfs2_glock_dq_uninit_m(2, ghs
);
430 d_instantiate(dentry
, inode
);
431 mark_inode_dirty(inode
);
437 * gfs2_mkdir - Make a directory
438 * @dir: The parent directory of the new one
439 * @dentry: The dentry of the new directory
440 * @mode: The mode of the new directory
445 static int gfs2_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
447 struct gfs2_inode
*dip
= GFS2_I(dir
), *ip
;
448 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
449 struct gfs2_holder ghs
[2];
451 struct buffer_head
*dibh
;
454 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
456 inode
= gfs2_createi(ghs
, &dentry
->d_name
, S_IFDIR
| mode
, 0);
458 gfs2_holder_uninit(ghs
);
459 return PTR_ERR(inode
);
462 ip
= ghs
[1].gh_gl
->gl_object
;
464 ip
->i_inode
.i_nlink
= 2;
465 ip
->i_disksize
= sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
);
466 ip
->i_diskflags
|= GFS2_DIF_JDATA
;
469 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
471 if (!gfs2_assert_withdraw(sdp
, !error
)) {
472 struct gfs2_dinode
*di
= (struct gfs2_dinode
*)dibh
->b_data
;
473 struct gfs2_dirent
*dent
= (struct gfs2_dirent
*)(di
+1);
476 gfs2_str2qstr(&str
, ".");
477 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
478 gfs2_qstr2dirent(&str
, GFS2_DIRENT_SIZE(str
.len
), dent
);
479 dent
->de_inum
= di
->di_num
; /* already GFS2 endian */
480 dent
->de_type
= cpu_to_be16(DT_DIR
);
481 di
->di_entries
= cpu_to_be32(1);
483 gfs2_str2qstr(&str
, "..");
484 dent
= (struct gfs2_dirent
*)((char*)dent
+ GFS2_DIRENT_SIZE(1));
485 gfs2_qstr2dirent(&str
, dibh
->b_size
- GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode
), dent
);
487 gfs2_inum_out(dip
, dent
);
488 dent
->de_type
= cpu_to_be16(DT_DIR
);
490 gfs2_dinode_out(ip
, di
);
495 error
= gfs2_change_nlink(dip
, +1);
496 gfs2_assert_withdraw(sdp
, !error
); /* dip already pinned */
499 if (dip
->i_alloc
->al_rgd
)
500 gfs2_inplace_release(dip
);
501 gfs2_quota_unlock(dip
);
504 gfs2_glock_dq_uninit_m(2, ghs
);
506 d_instantiate(dentry
, inode
);
507 mark_inode_dirty(inode
);
513 * gfs2_rmdiri - Remove a directory
514 * @dip: The parent directory of the directory to be removed
515 * @name: The name of the directory to be removed
516 * @ip: The GFS2 inode of the directory to be removed
518 * Assumes Glocks on dip and ip are held
523 static int gfs2_rmdiri(struct gfs2_inode
*dip
, const struct qstr
*name
,
524 struct gfs2_inode
*ip
)
529 if (ip
->i_entries
!= 2) {
530 if (gfs2_consist_inode(ip
))
531 gfs2_dinode_print(ip
);
535 error
= gfs2_dir_del(dip
, name
);
539 error
= gfs2_change_nlink(dip
, -1);
543 gfs2_str2qstr(&dotname
, ".");
544 error
= gfs2_dir_del(ip
, &dotname
);
548 gfs2_str2qstr(&dotname
, "..");
549 error
= gfs2_dir_del(ip
, &dotname
);
553 /* It looks odd, but it really should be done twice */
554 error
= gfs2_change_nlink(ip
, -1);
558 error
= gfs2_change_nlink(ip
, -1);
566 * gfs2_rmdir - Remove a directory
567 * @dir: The parent directory of the directory to be removed
568 * @dentry: The dentry of the directory to remove
570 * Remove a directory. Call gfs2_rmdiri()
575 static int gfs2_rmdir(struct inode
*dir
, struct dentry
*dentry
)
577 struct gfs2_inode
*dip
= GFS2_I(dir
);
578 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
579 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
580 struct gfs2_holder ghs
[3];
581 struct gfs2_rgrpd
*rgd
;
582 struct gfs2_holder ri_gh
;
585 error
= gfs2_rindex_hold(sdp
, &ri_gh
);
588 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
589 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
591 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_no_addr
);
592 gfs2_holder_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 2);
594 error
= gfs2_glock_nq(ghs
); /* parent */
598 error
= gfs2_glock_nq(ghs
+ 1); /* child */
602 error
= gfs2_glock_nq(ghs
+ 2); /* rgrp */
606 error
= gfs2_unlink_ok(dip
, &dentry
->d_name
, ip
);
610 if (ip
->i_entries
< 2) {
611 if (gfs2_consist_inode(ip
))
612 gfs2_dinode_print(ip
);
616 if (ip
->i_entries
> 2) {
621 error
= gfs2_trans_begin(sdp
, 2 * RES_DINODE
+ 3 * RES_LEAF
+ RES_RG_BIT
, 0);
625 error
= gfs2_rmdiri(dip
, &dentry
->d_name
, ip
);
630 gfs2_glock_dq(ghs
+ 2);
632 gfs2_holder_uninit(ghs
+ 2);
633 gfs2_glock_dq(ghs
+ 1);
635 gfs2_holder_uninit(ghs
+ 1);
638 gfs2_holder_uninit(ghs
);
639 gfs2_glock_dq_uninit(&ri_gh
);
644 * gfs2_mknod - Make a special file
645 * @dir: The directory in which the special file will reside
646 * @dentry: The dentry of the special file
647 * @mode: The mode of the special file
648 * @rdev: The device specification of the special file
652 static int gfs2_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
,
655 struct gfs2_inode
*dip
= GFS2_I(dir
);
656 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
657 struct gfs2_holder ghs
[2];
660 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
662 inode
= gfs2_createi(ghs
, &dentry
->d_name
, mode
, dev
);
664 gfs2_holder_uninit(ghs
);
665 return PTR_ERR(inode
);
669 if (dip
->i_alloc
->al_rgd
)
670 gfs2_inplace_release(dip
);
671 gfs2_quota_unlock(dip
);
674 gfs2_glock_dq_uninit_m(2, ghs
);
676 d_instantiate(dentry
, inode
);
677 mark_inode_dirty(inode
);
683 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
687 * Follow @to back to the root and make sure we don't encounter @this
688 * Assumes we already hold the rename lock.
693 static int gfs2_ok_to_move(struct gfs2_inode
*this, struct gfs2_inode
*to
)
695 struct inode
*dir
= &to
->i_inode
;
696 struct super_block
*sb
= dir
->i_sb
;
701 gfs2_str2qstr(&dotdot
, "..");
706 if (dir
== &this->i_inode
) {
710 if (dir
== sb
->s_root
->d_inode
) {
715 tmp
= gfs2_lookupi(dir
, &dotdot
, 1);
717 error
= PTR_ERR(tmp
);
731 * gfs2_rename - Rename a file
732 * @odir: Parent directory of old file name
733 * @odentry: The old dentry of the file
734 * @ndir: Parent directory of new file name
735 * @ndentry: The new dentry of the file
740 static int gfs2_rename(struct inode
*odir
, struct dentry
*odentry
,
741 struct inode
*ndir
, struct dentry
*ndentry
)
743 struct gfs2_inode
*odip
= GFS2_I(odir
);
744 struct gfs2_inode
*ndip
= GFS2_I(ndir
);
745 struct gfs2_inode
*ip
= GFS2_I(odentry
->d_inode
);
746 struct gfs2_inode
*nip
= NULL
;
747 struct gfs2_sbd
*sdp
= GFS2_SB(odir
);
748 struct gfs2_holder ghs
[5], r_gh
= { .gh_gl
= NULL
, };
749 struct gfs2_rgrpd
*nrgd
;
756 if (ndentry
->d_inode
) {
757 nip
= GFS2_I(ndentry
->d_inode
);
764 error
= gfs2_glock_nq_init(sdp
->sd_rename_gl
, LM_ST_EXCLUSIVE
,
769 if (S_ISDIR(ip
->i_inode
.i_mode
)) {
771 /* don't move a dirctory into it's subdir */
772 error
= gfs2_ok_to_move(ip
, ndip
);
779 gfs2_holder_init(odip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
781 gfs2_holder_init(ndip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
784 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
788 gfs2_holder_init(nip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
790 /* grab the resource lock for unlink flag twiddling
791 * this is the case of the target file already existing
792 * so we unlink before doing the rename
794 nrgd
= gfs2_blk2rgrpd(sdp
, nip
->i_no_addr
);
796 gfs2_holder_init(nrgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
++);
799 for (x
= 0; x
< num_gh
; x
++) {
800 error
= gfs2_glock_nq(ghs
+ x
);
805 /* Check out the old directory */
807 error
= gfs2_unlink_ok(odip
, &odentry
->d_name
, ip
);
811 /* Check out the new directory */
814 error
= gfs2_unlink_ok(ndip
, &ndentry
->d_name
, nip
);
818 if (S_ISDIR(nip
->i_inode
.i_mode
)) {
819 if (nip
->i_entries
< 2) {
820 if (gfs2_consist_inode(nip
))
821 gfs2_dinode_print(nip
);
825 if (nip
->i_entries
> 2) {
831 error
= gfs2_permission(ndir
, MAY_WRITE
| MAY_EXEC
);
835 error
= gfs2_dir_check(ndir
, &ndentry
->d_name
, NULL
);
847 if (!ndip
->i_inode
.i_nlink
) {
851 if (ndip
->i_entries
== (u32
)-1) {
855 if (S_ISDIR(ip
->i_inode
.i_mode
) &&
856 ndip
->i_inode
.i_nlink
== (u32
)-1) {
863 /* Check out the dir to be renamed */
866 error
= gfs2_permission(odentry
->d_inode
, MAY_WRITE
);
871 alloc_required
= error
= gfs2_diradd_alloc_required(ndir
, &ndentry
->d_name
);
876 if (alloc_required
) {
877 struct gfs2_alloc
*al
= gfs2_alloc_get(ndip
);
883 error
= gfs2_quota_lock_check(ndip
);
887 al
->al_requested
= sdp
->sd_max_dirres
;
889 error
= gfs2_inplace_reserve(ndip
);
893 error
= gfs2_trans_begin(sdp
, sdp
->sd_max_dirres
+
894 al
->al_rgd
->rd_length
+
895 4 * RES_DINODE
+ 4 * RES_LEAF
+
896 RES_STATFS
+ RES_QUOTA
+ 4, 0);
900 error
= gfs2_trans_begin(sdp
, 4 * RES_DINODE
+
901 5 * RES_LEAF
+ 4, 0);
906 /* Remove the target file, if it exists */
909 if (S_ISDIR(nip
->i_inode
.i_mode
))
910 error
= gfs2_rmdiri(ndip
, &ndentry
->d_name
, nip
);
912 error
= gfs2_dir_del(ndip
, &ndentry
->d_name
);
915 error
= gfs2_change_nlink(nip
, -1);
923 gfs2_str2qstr(&name
, "..");
925 error
= gfs2_change_nlink(ndip
, +1);
928 error
= gfs2_change_nlink(odip
, -1);
932 error
= gfs2_dir_mvino(ip
, &name
, ndip
, DT_DIR
);
936 struct buffer_head
*dibh
;
937 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
940 ip
->i_inode
.i_ctime
= CURRENT_TIME
;
941 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
942 gfs2_dinode_out(ip
, dibh
->b_data
);
946 error
= gfs2_dir_del(odip
, &odentry
->d_name
);
950 error
= gfs2_dir_add(ndir
, &ndentry
->d_name
, ip
, IF2DT(ip
->i_inode
.i_mode
));
958 gfs2_inplace_release(ndip
);
961 gfs2_quota_unlock(ndip
);
964 gfs2_alloc_put(ndip
);
967 gfs2_glock_dq(ghs
+ x
);
968 gfs2_holder_uninit(ghs
+ x
);
972 gfs2_glock_dq_uninit(&r_gh
);
978 * gfs2_readlinki - return the contents of a symlink
979 * @ip: the symlink's inode
980 * @buf: a pointer to the buffer to be filled
981 * @len: a pointer to the length of @buf
983 * If @buf is too small, a piece of memory is kmalloc()ed and needs
984 * to be freed by the caller.
989 static int gfs2_readlinki(struct gfs2_inode
*ip
, char **buf
, unsigned int *len
)
991 struct gfs2_holder i_gh
;
992 struct buffer_head
*dibh
;
996 gfs2_holder_init(ip
->i_gl
, LM_ST_SHARED
, 0, &i_gh
);
997 error
= gfs2_glock_nq(&i_gh
);
999 gfs2_holder_uninit(&i_gh
);
1003 if (!ip
->i_disksize
) {
1004 gfs2_consist_inode(ip
);
1009 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1013 x
= ip
->i_disksize
+ 1;
1015 *buf
= kmalloc(x
, GFP_NOFS
);
1022 memcpy(*buf
, dibh
->b_data
+ sizeof(struct gfs2_dinode
), x
);
1028 gfs2_glock_dq_uninit(&i_gh
);
1033 * gfs2_readlink - Read the value of a symlink
1034 * @dentry: the symlink
1035 * @buf: the buffer to read the symlink data into
1036 * @size: the size of the buffer
1041 static int gfs2_readlink(struct dentry
*dentry
, char __user
*user_buf
,
1044 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
1045 char array
[GFS2_FAST_NAME_SIZE
], *buf
= array
;
1046 unsigned int len
= GFS2_FAST_NAME_SIZE
;
1049 error
= gfs2_readlinki(ip
, &buf
, &len
);
1053 if (user_size
> len
- 1)
1054 user_size
= len
- 1;
1056 if (copy_to_user(user_buf
, buf
, user_size
))
1068 * gfs2_follow_link - Follow a symbolic link
1069 * @dentry: The dentry of the link
1070 * @nd: Data that we pass to vfs_follow_link()
1072 * This can handle symlinks of any size. It is optimised for symlinks
1073 * under GFS2_FAST_NAME_SIZE.
1075 * Returns: 0 on success or error code
1078 static void *gfs2_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1080 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
1081 char array
[GFS2_FAST_NAME_SIZE
], *buf
= array
;
1082 unsigned int len
= GFS2_FAST_NAME_SIZE
;
1085 error
= gfs2_readlinki(ip
, &buf
, &len
);
1087 error
= vfs_follow_link(nd
, buf
);
1092 return ERR_PTR(error
);
1099 * @nd: passed from Linux VFS, ignored by us
1101 * This may be called from the VFS directly, or from within GFS2 with the
1102 * inode locked, so we look to see if the glock is already locked and only
1103 * lock the glock if its not already been done.
1108 int gfs2_permission(struct inode
*inode
, int mask
)
1110 struct gfs2_inode
*ip
= GFS2_I(inode
);
1111 struct gfs2_holder i_gh
;
1115 if (gfs2_glock_is_locked_by_me(ip
->i_gl
) == NULL
) {
1116 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &i_gh
);
1122 if ((mask
& MAY_WRITE
) && IS_IMMUTABLE(inode
))
1125 error
= generic_permission(inode
, mask
, gfs2_check_acl
);
1127 gfs2_glock_dq_uninit(&i_gh
);
1132 static int setattr_size(struct inode
*inode
, struct iattr
*attr
)
1134 struct gfs2_inode
*ip
= GFS2_I(inode
);
1135 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1138 if (attr
->ia_size
!= ip
->i_disksize
) {
1139 error
= gfs2_trans_begin(sdp
, 0, sdp
->sd_jdesc
->jd_blocks
);
1142 error
= vmtruncate(inode
, attr
->ia_size
);
1143 gfs2_trans_end(sdp
);
1148 error
= gfs2_truncatei(ip
, attr
->ia_size
);
1149 if (error
&& (inode
->i_size
!= ip
->i_disksize
))
1150 i_size_write(inode
, ip
->i_disksize
);
1155 static int setattr_chown(struct inode
*inode
, struct iattr
*attr
)
1157 struct gfs2_inode
*ip
= GFS2_I(inode
);
1158 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1159 struct buffer_head
*dibh
;
1160 u32 ouid
, ogid
, nuid
, ngid
;
1163 ouid
= inode
->i_uid
;
1164 ogid
= inode
->i_gid
;
1165 nuid
= attr
->ia_uid
;
1166 ngid
= attr
->ia_gid
;
1168 if (!(attr
->ia_valid
& ATTR_UID
) || ouid
== nuid
)
1169 ouid
= nuid
= NO_QUOTA_CHANGE
;
1170 if (!(attr
->ia_valid
& ATTR_GID
) || ogid
== ngid
)
1171 ogid
= ngid
= NO_QUOTA_CHANGE
;
1173 if (!gfs2_alloc_get(ip
))
1176 error
= gfs2_quota_lock(ip
, nuid
, ngid
);
1180 if (ouid
!= NO_QUOTA_CHANGE
|| ogid
!= NO_QUOTA_CHANGE
) {
1181 error
= gfs2_quota_check(ip
, nuid
, ngid
);
1186 error
= gfs2_trans_begin(sdp
, RES_DINODE
+ 2 * RES_QUOTA
, 0);
1190 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1194 error
= inode_setattr(inode
, attr
);
1195 gfs2_assert_warn(sdp
, !error
);
1197 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1198 gfs2_dinode_out(ip
, dibh
->b_data
);
1201 if (ouid
!= NO_QUOTA_CHANGE
|| ogid
!= NO_QUOTA_CHANGE
) {
1202 u64 blocks
= gfs2_get_inode_blocks(&ip
->i_inode
);
1203 gfs2_quota_change(ip
, -blocks
, ouid
, ogid
);
1204 gfs2_quota_change(ip
, blocks
, nuid
, ngid
);
1208 gfs2_trans_end(sdp
);
1210 gfs2_quota_unlock(ip
);
1217 * gfs2_setattr - Change attributes on an inode
1218 * @dentry: The dentry which is changing
1219 * @attr: The structure describing the change
1221 * The VFS layer wants to change one or more of an inodes attributes. Write
1222 * that change out to disk.
1227 static int gfs2_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1229 struct inode
*inode
= dentry
->d_inode
;
1230 struct gfs2_inode
*ip
= GFS2_I(inode
);
1231 struct gfs2_holder i_gh
;
1234 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &i_gh
);
1239 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
1242 error
= inode_change_ok(inode
, attr
);
1246 if (attr
->ia_valid
& ATTR_SIZE
)
1247 error
= setattr_size(inode
, attr
);
1248 else if (attr
->ia_valid
& (ATTR_UID
| ATTR_GID
))
1249 error
= setattr_chown(inode
, attr
);
1250 else if ((attr
->ia_valid
& ATTR_MODE
) && IS_POSIXACL(inode
))
1251 error
= gfs2_acl_chmod(ip
, attr
);
1253 error
= gfs2_setattr_simple(ip
, attr
);
1256 gfs2_glock_dq_uninit(&i_gh
);
1258 mark_inode_dirty(inode
);
1263 * gfs2_getattr - Read out an inode's attributes
1264 * @mnt: The vfsmount the inode is being accessed from
1265 * @dentry: The dentry to stat
1266 * @stat: The inode's stats
1268 * This may be called from the VFS directly, or from within GFS2 with the
1269 * inode locked, so we look to see if the glock is already locked and only
1270 * lock the glock if its not already been done. Note that its the NFS
1271 * readdirplus operation which causes this to be called (from filldir)
1272 * with the glock already held.
1277 static int gfs2_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
1280 struct inode
*inode
= dentry
->d_inode
;
1281 struct gfs2_inode
*ip
= GFS2_I(inode
);
1282 struct gfs2_holder gh
;
1286 if (gfs2_glock_is_locked_by_me(ip
->i_gl
) == NULL
) {
1287 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &gh
);
1293 generic_fillattr(inode
, stat
);
1295 gfs2_glock_dq_uninit(&gh
);
1300 static int gfs2_setxattr(struct dentry
*dentry
, const char *name
,
1301 const void *data
, size_t size
, int flags
)
1303 struct inode
*inode
= dentry
->d_inode
;
1304 struct gfs2_inode
*ip
= GFS2_I(inode
);
1305 struct gfs2_holder gh
;
1308 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &gh
);
1309 ret
= gfs2_glock_nq(&gh
);
1311 ret
= generic_setxattr(dentry
, name
, data
, size
, flags
);
1314 gfs2_holder_uninit(&gh
);
1318 static ssize_t
gfs2_getxattr(struct dentry
*dentry
, const char *name
,
1319 void *data
, size_t size
)
1321 struct inode
*inode
= dentry
->d_inode
;
1322 struct gfs2_inode
*ip
= GFS2_I(inode
);
1323 struct gfs2_holder gh
;
1326 gfs2_holder_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &gh
);
1327 ret
= gfs2_glock_nq(&gh
);
1329 ret
= generic_getxattr(dentry
, name
, data
, size
);
1332 gfs2_holder_uninit(&gh
);
1336 static int gfs2_removexattr(struct dentry
*dentry
, const char *name
)
1338 struct inode
*inode
= dentry
->d_inode
;
1339 struct gfs2_inode
*ip
= GFS2_I(inode
);
1340 struct gfs2_holder gh
;
1343 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &gh
);
1344 ret
= gfs2_glock_nq(&gh
);
1346 ret
= generic_removexattr(dentry
, name
);
1349 gfs2_holder_uninit(&gh
);
1353 static int gfs2_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
1356 struct gfs2_inode
*ip
= GFS2_I(inode
);
1357 struct gfs2_holder gh
;
1360 ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
);
1364 mutex_lock(&inode
->i_mutex
);
1366 ret
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, 0, &gh
);
1370 if (gfs2_is_stuffed(ip
)) {
1371 u64 phys
= ip
->i_no_addr
<< inode
->i_blkbits
;
1372 u64 size
= i_size_read(inode
);
1373 u32 flags
= FIEMAP_EXTENT_LAST
|FIEMAP_EXTENT_NOT_ALIGNED
|
1374 FIEMAP_EXTENT_DATA_INLINE
;
1375 phys
+= sizeof(struct gfs2_dinode
);
1377 if (start
+ len
> size
)
1380 ret
= fiemap_fill_next_extent(fieinfo
, start
, phys
,
1385 ret
= __generic_block_fiemap(inode
, fieinfo
, start
, len
,
1389 gfs2_glock_dq_uninit(&gh
);
1391 mutex_unlock(&inode
->i_mutex
);
1395 const struct inode_operations gfs2_file_iops
= {
1396 .permission
= gfs2_permission
,
1397 .setattr
= gfs2_setattr
,
1398 .getattr
= gfs2_getattr
,
1399 .setxattr
= gfs2_setxattr
,
1400 .getxattr
= gfs2_getxattr
,
1401 .listxattr
= gfs2_listxattr
,
1402 .removexattr
= gfs2_removexattr
,
1403 .fiemap
= gfs2_fiemap
,
1406 const struct inode_operations gfs2_dir_iops
= {
1407 .create
= gfs2_create
,
1408 .lookup
= gfs2_lookup
,
1410 .unlink
= gfs2_unlink
,
1411 .symlink
= gfs2_symlink
,
1412 .mkdir
= gfs2_mkdir
,
1413 .rmdir
= gfs2_rmdir
,
1414 .mknod
= gfs2_mknod
,
1415 .rename
= gfs2_rename
,
1416 .permission
= gfs2_permission
,
1417 .setattr
= gfs2_setattr
,
1418 .getattr
= gfs2_getattr
,
1419 .setxattr
= gfs2_setxattr
,
1420 .getxattr
= gfs2_getxattr
,
1421 .listxattr
= gfs2_listxattr
,
1422 .removexattr
= gfs2_removexattr
,
1423 .fiemap
= gfs2_fiemap
,
1426 const struct inode_operations gfs2_symlink_iops
= {
1427 .readlink
= gfs2_readlink
,
1428 .follow_link
= gfs2_follow_link
,
1429 .permission
= gfs2_permission
,
1430 .setattr
= gfs2_setattr
,
1431 .getattr
= gfs2_getattr
,
1432 .setxattr
= gfs2_setxattr
,
1433 .getxattr
= gfs2_getxattr
,
1434 .listxattr
= gfs2_listxattr
,
1435 .removexattr
= gfs2_removexattr
,
1436 .fiemap
= gfs2_fiemap
,