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>
16 #include <linux/xattr.h>
17 #include <linux/posix_acl.h>
18 #include <linux/gfs2_ondisk.h>
19 #include <linux/crc32.h>
20 #include <linux/fiemap.h>
21 #include <asm/uaccess.h>
39 * gfs2_create - Create a file
40 * @dir: The directory in which to create the file
41 * @dentry: The dentry of the new file
42 * @mode: The mode of the new file
47 static int gfs2_create(struct inode
*dir
, struct dentry
*dentry
,
48 int mode
, struct nameidata
*nd
)
50 struct gfs2_inode
*dip
= GFS2_I(dir
);
51 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
52 struct gfs2_holder ghs
[2];
55 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
58 inode
= gfs2_createi(ghs
, &dentry
->d_name
, S_IFREG
| mode
, 0);
61 if (dip
->i_alloc
->al_rgd
)
62 gfs2_inplace_release(dip
);
63 gfs2_quota_unlock(dip
);
65 gfs2_glock_dq_uninit_m(2, ghs
);
66 mark_inode_dirty(inode
);
68 } else if (PTR_ERR(inode
) != -EEXIST
||
69 (nd
&& nd
->flags
& LOOKUP_EXCL
)) {
70 gfs2_holder_uninit(ghs
);
71 return PTR_ERR(inode
);
74 inode
= gfs2_lookupi(dir
, &dentry
->d_name
, 0);
77 gfs2_holder_uninit(ghs
);
80 gfs2_holder_uninit(ghs
);
81 return PTR_ERR(inode
);
86 d_instantiate(dentry
, inode
);
92 * gfs2_lookup - Look up a filename in a directory and return its inode
93 * @dir: The directory inode
94 * @dentry: The dentry of the new inode
95 * @nd: passed from Linux VFS, ignored by us
97 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
102 static struct dentry
*gfs2_lookup(struct inode
*dir
, struct dentry
*dentry
,
103 struct nameidata
*nd
)
105 struct inode
*inode
= NULL
;
107 inode
= gfs2_lookupi(dir
, &dentry
->d_name
, 0);
108 if (inode
&& IS_ERR(inode
))
109 return ERR_CAST(inode
);
112 struct gfs2_glock
*gl
= GFS2_I(inode
)->i_gl
;
113 struct gfs2_holder gh
;
115 error
= gfs2_glock_nq_init(gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &gh
);
118 return ERR_PTR(error
);
120 gfs2_glock_dq_uninit(&gh
);
121 return d_splice_alias(inode
, dentry
);
123 d_add(dentry
, inode
);
129 * gfs2_link - Link to a file
130 * @old_dentry: The inode to link
131 * @dir: Add link to this directory
132 * @dentry: The name of the link
134 * Link the inode in "old_dentry" into the directory "dir" with the
140 static int gfs2_link(struct dentry
*old_dentry
, struct inode
*dir
,
141 struct dentry
*dentry
)
143 struct gfs2_inode
*dip
= GFS2_I(dir
);
144 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
145 struct inode
*inode
= old_dentry
->d_inode
;
146 struct gfs2_inode
*ip
= GFS2_I(inode
);
147 struct gfs2_holder ghs
[2];
151 if (S_ISDIR(inode
->i_mode
))
154 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
155 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
157 error
= gfs2_glock_nq(ghs
); /* parent */
161 error
= gfs2_glock_nq(ghs
+ 1); /* child */
165 error
= gfs2_permission(dir
, MAY_WRITE
| MAY_EXEC
, 0);
169 error
= gfs2_dir_check(dir
, &dentry
->d_name
, NULL
);
180 if (!dip
->i_inode
.i_nlink
)
183 if (dip
->i_entries
== (u32
)-1)
186 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
189 if (!ip
->i_inode
.i_nlink
)
192 if (ip
->i_inode
.i_nlink
== (u32
)-1)
195 alloc_required
= error
= gfs2_diradd_alloc_required(dir
, &dentry
->d_name
);
200 if (alloc_required
) {
201 struct gfs2_alloc
*al
= gfs2_alloc_get(dip
);
207 error
= gfs2_quota_lock_check(dip
);
211 al
->al_requested
= sdp
->sd_max_dirres
;
213 error
= gfs2_inplace_reserve(dip
);
217 error
= gfs2_trans_begin(sdp
, sdp
->sd_max_dirres
+
219 2 * RES_DINODE
+ RES_STATFS
+
224 error
= gfs2_trans_begin(sdp
, 2 * RES_DINODE
+ RES_LEAF
, 0);
229 error
= gfs2_dir_add(dir
, &dentry
->d_name
, ip
, IF2DT(inode
->i_mode
));
233 error
= gfs2_change_nlink(ip
, +1);
239 gfs2_inplace_release(dip
);
242 gfs2_quota_unlock(dip
);
247 gfs2_glock_dq(ghs
+ 1);
251 gfs2_holder_uninit(ghs
);
252 gfs2_holder_uninit(ghs
+ 1);
255 d_instantiate(dentry
, inode
);
256 mark_inode_dirty(inode
);
262 * gfs2_unlink_ok - check to see that a inode is still in a directory
263 * @dip: the directory
264 * @name: the name of the file
267 * Assumes that the lock on (at least) @dip is held.
269 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
272 static int gfs2_unlink_ok(struct gfs2_inode
*dip
, const struct qstr
*name
,
273 const struct gfs2_inode
*ip
)
277 if (IS_IMMUTABLE(&ip
->i_inode
) || IS_APPEND(&ip
->i_inode
))
280 if ((dip
->i_inode
.i_mode
& S_ISVTX
) &&
281 dip
->i_inode
.i_uid
!= current_fsuid() &&
282 ip
->i_inode
.i_uid
!= current_fsuid() && !capable(CAP_FOWNER
))
285 if (IS_APPEND(&dip
->i_inode
))
288 error
= gfs2_permission(&dip
->i_inode
, MAY_WRITE
| MAY_EXEC
, 0);
292 error
= gfs2_dir_check(&dip
->i_inode
, name
, ip
);
300 * gfs2_unlink - Unlink a file
301 * @dir: The inode of the directory containing the file to unlink
302 * @dentry: The file itself
304 * Unlink a file. Call gfs2_unlinki()
309 static int gfs2_unlink(struct inode
*dir
, struct dentry
*dentry
)
311 struct gfs2_inode
*dip
= GFS2_I(dir
);
312 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
313 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
314 struct gfs2_holder ghs
[3];
315 struct gfs2_rgrpd
*rgd
;
316 struct gfs2_holder ri_gh
;
319 error
= gfs2_rindex_hold(sdp
, &ri_gh
);
323 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
324 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
326 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_no_addr
);
327 gfs2_holder_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 2);
330 error
= gfs2_glock_nq(ghs
); /* parent */
334 error
= gfs2_glock_nq(ghs
+ 1); /* child */
338 error
= gfs2_glock_nq(ghs
+ 2); /* rgrp */
342 error
= gfs2_unlink_ok(dip
, &dentry
->d_name
, ip
);
346 error
= gfs2_trans_begin(sdp
, 2*RES_DINODE
+ RES_LEAF
+ RES_RG_BIT
, 0);
350 error
= gfs2_dir_del(dip
, &dentry
->d_name
);
354 error
= gfs2_change_nlink(ip
, -1);
359 gfs2_glock_dq(ghs
+ 2);
361 gfs2_holder_uninit(ghs
+ 2);
362 gfs2_glock_dq(ghs
+ 1);
364 gfs2_holder_uninit(ghs
+ 1);
367 gfs2_holder_uninit(ghs
);
368 gfs2_glock_dq_uninit(&ri_gh
);
373 * gfs2_symlink - Create a symlink
374 * @dir: The directory to create the symlink in
375 * @dentry: The dentry to put the symlink in
376 * @symname: The thing which the link points to
381 static int gfs2_symlink(struct inode
*dir
, struct dentry
*dentry
,
384 struct gfs2_inode
*dip
= GFS2_I(dir
), *ip
;
385 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
386 struct gfs2_holder ghs
[2];
388 struct buffer_head
*dibh
;
392 /* Must be stuffed with a null terminator for gfs2_follow_link() */
393 size
= strlen(symname
);
394 if (size
> sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
) - 1)
395 return -ENAMETOOLONG
;
397 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
399 inode
= gfs2_createi(ghs
, &dentry
->d_name
, S_IFLNK
| S_IRWXUGO
, 0);
401 gfs2_holder_uninit(ghs
);
402 return PTR_ERR(inode
);
405 ip
= ghs
[1].gh_gl
->gl_object
;
407 i_size_write(inode
, size
);
409 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
411 if (!gfs2_assert_withdraw(sdp
, !error
)) {
412 gfs2_dinode_out(ip
, dibh
->b_data
);
413 memcpy(dibh
->b_data
+ sizeof(struct gfs2_dinode
), symname
,
419 if (dip
->i_alloc
->al_rgd
)
420 gfs2_inplace_release(dip
);
421 gfs2_quota_unlock(dip
);
424 gfs2_glock_dq_uninit_m(2, ghs
);
426 d_instantiate(dentry
, inode
);
427 mark_inode_dirty(inode
);
433 * gfs2_mkdir - Make a directory
434 * @dir: The parent directory of the new one
435 * @dentry: The dentry of the new directory
436 * @mode: The mode of the new directory
441 static int gfs2_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
443 struct gfs2_inode
*dip
= GFS2_I(dir
), *ip
;
444 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
445 struct gfs2_holder ghs
[2];
447 struct buffer_head
*dibh
;
450 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
452 inode
= gfs2_createi(ghs
, &dentry
->d_name
, S_IFDIR
| mode
, 0);
454 gfs2_holder_uninit(ghs
);
455 return PTR_ERR(inode
);
458 ip
= ghs
[1].gh_gl
->gl_object
;
460 ip
->i_inode
.i_nlink
= 2;
461 i_size_write(inode
, sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
));
462 ip
->i_diskflags
|= GFS2_DIF_JDATA
;
465 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
467 if (!gfs2_assert_withdraw(sdp
, !error
)) {
468 struct gfs2_dinode
*di
= (struct gfs2_dinode
*)dibh
->b_data
;
469 struct gfs2_dirent
*dent
= (struct gfs2_dirent
*)(di
+1);
471 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
472 gfs2_qstr2dirent(&gfs2_qdot
, GFS2_DIRENT_SIZE(gfs2_qdot
.len
), dent
);
473 dent
->de_inum
= di
->di_num
; /* already GFS2 endian */
474 dent
->de_type
= cpu_to_be16(DT_DIR
);
475 di
->di_entries
= cpu_to_be32(1);
477 dent
= (struct gfs2_dirent
*)((char*)dent
+ GFS2_DIRENT_SIZE(1));
478 gfs2_qstr2dirent(&gfs2_qdotdot
, dibh
->b_size
- GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode
), dent
);
480 gfs2_inum_out(dip
, dent
);
481 dent
->de_type
= cpu_to_be16(DT_DIR
);
483 gfs2_dinode_out(ip
, di
);
488 error
= gfs2_change_nlink(dip
, +1);
489 gfs2_assert_withdraw(sdp
, !error
); /* dip already pinned */
492 if (dip
->i_alloc
->al_rgd
)
493 gfs2_inplace_release(dip
);
494 gfs2_quota_unlock(dip
);
497 gfs2_glock_dq_uninit_m(2, ghs
);
499 d_instantiate(dentry
, inode
);
500 mark_inode_dirty(inode
);
506 * gfs2_rmdiri - Remove a directory
507 * @dip: The parent directory of the directory to be removed
508 * @name: The name of the directory to be removed
509 * @ip: The GFS2 inode of the directory to be removed
511 * Assumes Glocks on dip and ip are held
516 static int gfs2_rmdiri(struct gfs2_inode
*dip
, const struct qstr
*name
,
517 struct gfs2_inode
*ip
)
521 if (ip
->i_entries
!= 2) {
522 if (gfs2_consist_inode(ip
))
523 gfs2_dinode_print(ip
);
527 error
= gfs2_dir_del(dip
, name
);
531 error
= gfs2_change_nlink(dip
, -1);
535 error
= gfs2_dir_del(ip
, &gfs2_qdot
);
539 error
= gfs2_dir_del(ip
, &gfs2_qdotdot
);
543 /* It looks odd, but it really should be done twice */
544 error
= gfs2_change_nlink(ip
, -1);
548 error
= gfs2_change_nlink(ip
, -1);
556 * gfs2_rmdir - Remove a directory
557 * @dir: The parent directory of the directory to be removed
558 * @dentry: The dentry of the directory to remove
560 * Remove a directory. Call gfs2_rmdiri()
565 static int gfs2_rmdir(struct inode
*dir
, struct dentry
*dentry
)
567 struct gfs2_inode
*dip
= GFS2_I(dir
);
568 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
569 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
570 struct gfs2_holder ghs
[3];
571 struct gfs2_rgrpd
*rgd
;
572 struct gfs2_holder ri_gh
;
575 error
= gfs2_rindex_hold(sdp
, &ri_gh
);
578 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
579 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
581 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_no_addr
);
582 gfs2_holder_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 2);
584 error
= gfs2_glock_nq(ghs
); /* parent */
588 error
= gfs2_glock_nq(ghs
+ 1); /* child */
592 error
= gfs2_glock_nq(ghs
+ 2); /* rgrp */
596 error
= gfs2_unlink_ok(dip
, &dentry
->d_name
, ip
);
600 if (ip
->i_entries
< 2) {
601 if (gfs2_consist_inode(ip
))
602 gfs2_dinode_print(ip
);
606 if (ip
->i_entries
> 2) {
611 error
= gfs2_trans_begin(sdp
, 2 * RES_DINODE
+ 3 * RES_LEAF
+ RES_RG_BIT
, 0);
615 error
= gfs2_rmdiri(dip
, &dentry
->d_name
, ip
);
620 gfs2_glock_dq(ghs
+ 2);
622 gfs2_holder_uninit(ghs
+ 2);
623 gfs2_glock_dq(ghs
+ 1);
625 gfs2_holder_uninit(ghs
+ 1);
628 gfs2_holder_uninit(ghs
);
629 gfs2_glock_dq_uninit(&ri_gh
);
634 * gfs2_mknod - Make a special file
635 * @dir: The directory in which the special file will reside
636 * @dentry: The dentry of the special file
637 * @mode: The mode of the special file
638 * @rdev: The device specification of the special file
642 static int gfs2_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
,
645 struct gfs2_inode
*dip
= GFS2_I(dir
);
646 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
647 struct gfs2_holder ghs
[2];
650 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
652 inode
= gfs2_createi(ghs
, &dentry
->d_name
, mode
, dev
);
654 gfs2_holder_uninit(ghs
);
655 return PTR_ERR(inode
);
659 if (dip
->i_alloc
->al_rgd
)
660 gfs2_inplace_release(dip
);
661 gfs2_quota_unlock(dip
);
664 gfs2_glock_dq_uninit_m(2, ghs
);
666 d_instantiate(dentry
, inode
);
667 mark_inode_dirty(inode
);
673 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
677 * Follow @to back to the root and make sure we don't encounter @this
678 * Assumes we already hold the rename lock.
683 static int gfs2_ok_to_move(struct gfs2_inode
*this, struct gfs2_inode
*to
)
685 struct inode
*dir
= &to
->i_inode
;
686 struct super_block
*sb
= dir
->i_sb
;
693 if (dir
== &this->i_inode
) {
697 if (dir
== sb
->s_root
->d_inode
) {
702 tmp
= gfs2_lookupi(dir
, &gfs2_qdotdot
, 1);
704 error
= PTR_ERR(tmp
);
718 * gfs2_rename - Rename a file
719 * @odir: Parent directory of old file name
720 * @odentry: The old dentry of the file
721 * @ndir: Parent directory of new file name
722 * @ndentry: The new dentry of the file
727 static int gfs2_rename(struct inode
*odir
, struct dentry
*odentry
,
728 struct inode
*ndir
, struct dentry
*ndentry
)
730 struct gfs2_inode
*odip
= GFS2_I(odir
);
731 struct gfs2_inode
*ndip
= GFS2_I(ndir
);
732 struct gfs2_inode
*ip
= GFS2_I(odentry
->d_inode
);
733 struct gfs2_inode
*nip
= NULL
;
734 struct gfs2_sbd
*sdp
= GFS2_SB(odir
);
735 struct gfs2_holder ghs
[5], r_gh
= { .gh_gl
= NULL
, }, ri_gh
;
736 struct gfs2_rgrpd
*nrgd
;
739 int alloc_required
= 0;
743 if (ndentry
->d_inode
) {
744 nip
= GFS2_I(ndentry
->d_inode
);
749 error
= gfs2_rindex_hold(sdp
, &ri_gh
);
754 error
= gfs2_glock_nq_init(sdp
->sd_rename_gl
, LM_ST_EXCLUSIVE
,
759 if (S_ISDIR(ip
->i_inode
.i_mode
)) {
761 /* don't move a dirctory into it's subdir */
762 error
= gfs2_ok_to_move(ip
, ndip
);
769 gfs2_holder_init(odip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
771 gfs2_holder_init(ndip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
774 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
778 gfs2_holder_init(nip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
780 /* grab the resource lock for unlink flag twiddling
781 * this is the case of the target file already existing
782 * so we unlink before doing the rename
784 nrgd
= gfs2_blk2rgrpd(sdp
, nip
->i_no_addr
);
786 gfs2_holder_init(nrgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
++);
789 for (x
= 0; x
< num_gh
; x
++) {
790 error
= gfs2_glock_nq(ghs
+ x
);
795 /* Check out the old directory */
797 error
= gfs2_unlink_ok(odip
, &odentry
->d_name
, ip
);
801 /* Check out the new directory */
804 error
= gfs2_unlink_ok(ndip
, &ndentry
->d_name
, nip
);
808 if (S_ISDIR(nip
->i_inode
.i_mode
)) {
809 if (nip
->i_entries
< 2) {
810 if (gfs2_consist_inode(nip
))
811 gfs2_dinode_print(nip
);
815 if (nip
->i_entries
> 2) {
821 error
= gfs2_permission(ndir
, MAY_WRITE
| MAY_EXEC
, 0);
825 error
= gfs2_dir_check(ndir
, &ndentry
->d_name
, NULL
);
837 if (!ndip
->i_inode
.i_nlink
) {
841 if (ndip
->i_entries
== (u32
)-1) {
845 if (S_ISDIR(ip
->i_inode
.i_mode
) &&
846 ndip
->i_inode
.i_nlink
== (u32
)-1) {
853 /* Check out the dir to be renamed */
856 error
= gfs2_permission(odentry
->d_inode
, MAY_WRITE
, 0);
862 alloc_required
= gfs2_diradd_alloc_required(ndir
, &ndentry
->d_name
);
863 error
= alloc_required
;
868 if (alloc_required
) {
869 struct gfs2_alloc
*al
= gfs2_alloc_get(ndip
);
875 error
= gfs2_quota_lock_check(ndip
);
879 al
->al_requested
= sdp
->sd_max_dirres
;
881 error
= gfs2_inplace_reserve_ri(ndip
);
885 error
= gfs2_trans_begin(sdp
, sdp
->sd_max_dirres
+
887 4 * RES_DINODE
+ 4 * RES_LEAF
+
888 RES_STATFS
+ RES_QUOTA
+ 4, 0);
892 error
= gfs2_trans_begin(sdp
, 4 * RES_DINODE
+
893 5 * RES_LEAF
+ 4, 0);
898 /* Remove the target file, if it exists */
901 if (S_ISDIR(nip
->i_inode
.i_mode
))
902 error
= gfs2_rmdiri(ndip
, &ndentry
->d_name
, nip
);
904 error
= gfs2_dir_del(ndip
, &ndentry
->d_name
);
907 error
= gfs2_change_nlink(nip
, -1);
914 error
= gfs2_change_nlink(ndip
, +1);
917 error
= gfs2_change_nlink(odip
, -1);
921 error
= gfs2_dir_mvino(ip
, &gfs2_qdotdot
, ndip
, DT_DIR
);
925 struct buffer_head
*dibh
;
926 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
929 ip
->i_inode
.i_ctime
= CURRENT_TIME
;
930 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
931 gfs2_dinode_out(ip
, dibh
->b_data
);
935 error
= gfs2_dir_del(odip
, &odentry
->d_name
);
939 error
= gfs2_dir_add(ndir
, &ndentry
->d_name
, ip
, IF2DT(ip
->i_inode
.i_mode
));
947 gfs2_inplace_release(ndip
);
950 gfs2_quota_unlock(ndip
);
953 gfs2_alloc_put(ndip
);
956 gfs2_glock_dq(ghs
+ x
);
957 gfs2_holder_uninit(ghs
+ x
);
961 gfs2_glock_dq_uninit(&r_gh
);
963 gfs2_glock_dq_uninit(&ri_gh
);
968 * gfs2_follow_link - Follow a symbolic link
969 * @dentry: The dentry of the link
970 * @nd: Data that we pass to vfs_follow_link()
972 * This can handle symlinks of any size.
974 * Returns: 0 on success or error code
977 static void *gfs2_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
979 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
980 struct gfs2_holder i_gh
;
981 struct buffer_head
*dibh
;
982 unsigned int x
, size
;
986 gfs2_holder_init(ip
->i_gl
, LM_ST_SHARED
, 0, &i_gh
);
987 error
= gfs2_glock_nq(&i_gh
);
989 gfs2_holder_uninit(&i_gh
);
990 nd_set_link(nd
, ERR_PTR(error
));
994 size
= (unsigned int)i_size_read(&ip
->i_inode
);
996 gfs2_consist_inode(ip
);
1001 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1003 buf
= ERR_PTR(error
);
1008 buf
= kmalloc(x
, GFP_NOFS
);
1010 buf
= ERR_PTR(-ENOMEM
);
1012 memcpy(buf
, dibh
->b_data
+ sizeof(struct gfs2_dinode
), x
);
1015 gfs2_glock_dq_uninit(&i_gh
);
1016 nd_set_link(nd
, buf
);
1020 static void gfs2_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *p
)
1022 char *s
= nd_get_link(nd
);
1031 * @nd: passed from Linux VFS, ignored by us
1033 * This may be called from the VFS directly, or from within GFS2 with the
1034 * inode locked, so we look to see if the glock is already locked and only
1035 * lock the glock if its not already been done.
1040 int gfs2_permission(struct inode
*inode
, int mask
, unsigned int flags
)
1042 struct gfs2_inode
*ip
;
1043 struct gfs2_holder i_gh
;
1047 if (flags
& IPERM_FLAG_RCU
)
1051 if (gfs2_glock_is_locked_by_me(ip
->i_gl
) == NULL
) {
1052 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &i_gh
);
1058 if ((mask
& MAY_WRITE
) && IS_IMMUTABLE(inode
))
1061 error
= generic_permission(inode
, mask
, flags
, gfs2_check_acl
);
1063 gfs2_glock_dq_uninit(&i_gh
);
1068 static int setattr_chown(struct inode
*inode
, struct iattr
*attr
)
1070 struct gfs2_inode
*ip
= GFS2_I(inode
);
1071 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1072 u32 ouid
, ogid
, nuid
, ngid
;
1075 ouid
= inode
->i_uid
;
1076 ogid
= inode
->i_gid
;
1077 nuid
= attr
->ia_uid
;
1078 ngid
= attr
->ia_gid
;
1080 if (!(attr
->ia_valid
& ATTR_UID
) || ouid
== nuid
)
1081 ouid
= nuid
= NO_QUOTA_CHANGE
;
1082 if (!(attr
->ia_valid
& ATTR_GID
) || ogid
== ngid
)
1083 ogid
= ngid
= NO_QUOTA_CHANGE
;
1085 if (!gfs2_alloc_get(ip
))
1088 error
= gfs2_quota_lock(ip
, nuid
, ngid
);
1092 if (ouid
!= NO_QUOTA_CHANGE
|| ogid
!= NO_QUOTA_CHANGE
) {
1093 error
= gfs2_quota_check(ip
, nuid
, ngid
);
1098 error
= gfs2_trans_begin(sdp
, RES_DINODE
+ 2 * RES_QUOTA
, 0);
1102 error
= gfs2_setattr_simple(ip
, attr
);
1106 if (ouid
!= NO_QUOTA_CHANGE
|| ogid
!= NO_QUOTA_CHANGE
) {
1107 u64 blocks
= gfs2_get_inode_blocks(&ip
->i_inode
);
1108 gfs2_quota_change(ip
, -blocks
, ouid
, ogid
);
1109 gfs2_quota_change(ip
, blocks
, nuid
, ngid
);
1113 gfs2_trans_end(sdp
);
1115 gfs2_quota_unlock(ip
);
1122 * gfs2_setattr - Change attributes on an inode
1123 * @dentry: The dentry which is changing
1124 * @attr: The structure describing the change
1126 * The VFS layer wants to change one or more of an inodes attributes. Write
1127 * that change out to disk.
1132 static int gfs2_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1134 struct inode
*inode
= dentry
->d_inode
;
1135 struct gfs2_inode
*ip
= GFS2_I(inode
);
1136 struct gfs2_holder i_gh
;
1139 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &i_gh
);
1144 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
1147 error
= inode_change_ok(inode
, attr
);
1151 if (attr
->ia_valid
& ATTR_SIZE
)
1152 error
= gfs2_setattr_size(inode
, attr
->ia_size
);
1153 else if (attr
->ia_valid
& (ATTR_UID
| ATTR_GID
))
1154 error
= setattr_chown(inode
, attr
);
1155 else if ((attr
->ia_valid
& ATTR_MODE
) && IS_POSIXACL(inode
))
1156 error
= gfs2_acl_chmod(ip
, attr
);
1158 error
= gfs2_setattr_simple(ip
, attr
);
1161 gfs2_glock_dq_uninit(&i_gh
);
1163 mark_inode_dirty(inode
);
1168 * gfs2_getattr - Read out an inode's attributes
1169 * @mnt: The vfsmount the inode is being accessed from
1170 * @dentry: The dentry to stat
1171 * @stat: The inode's stats
1173 * This may be called from the VFS directly, or from within GFS2 with the
1174 * inode locked, so we look to see if the glock is already locked and only
1175 * lock the glock if its not already been done. Note that its the NFS
1176 * readdirplus operation which causes this to be called (from filldir)
1177 * with the glock already held.
1182 static int gfs2_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
1185 struct inode
*inode
= dentry
->d_inode
;
1186 struct gfs2_inode
*ip
= GFS2_I(inode
);
1187 struct gfs2_holder gh
;
1191 if (gfs2_glock_is_locked_by_me(ip
->i_gl
) == NULL
) {
1192 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &gh
);
1198 generic_fillattr(inode
, stat
);
1200 gfs2_glock_dq_uninit(&gh
);
1205 static int gfs2_setxattr(struct dentry
*dentry
, const char *name
,
1206 const void *data
, size_t size
, int flags
)
1208 struct inode
*inode
= dentry
->d_inode
;
1209 struct gfs2_inode
*ip
= GFS2_I(inode
);
1210 struct gfs2_holder gh
;
1213 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &gh
);
1214 ret
= gfs2_glock_nq(&gh
);
1216 ret
= generic_setxattr(dentry
, name
, data
, size
, flags
);
1219 gfs2_holder_uninit(&gh
);
1223 static ssize_t
gfs2_getxattr(struct dentry
*dentry
, const char *name
,
1224 void *data
, size_t size
)
1226 struct inode
*inode
= dentry
->d_inode
;
1227 struct gfs2_inode
*ip
= GFS2_I(inode
);
1228 struct gfs2_holder gh
;
1231 gfs2_holder_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &gh
);
1232 ret
= gfs2_glock_nq(&gh
);
1234 ret
= generic_getxattr(dentry
, name
, data
, size
);
1237 gfs2_holder_uninit(&gh
);
1241 static int gfs2_removexattr(struct dentry
*dentry
, const char *name
)
1243 struct inode
*inode
= dentry
->d_inode
;
1244 struct gfs2_inode
*ip
= GFS2_I(inode
);
1245 struct gfs2_holder gh
;
1248 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &gh
);
1249 ret
= gfs2_glock_nq(&gh
);
1251 ret
= generic_removexattr(dentry
, name
);
1254 gfs2_holder_uninit(&gh
);
1258 static int gfs2_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
1261 struct gfs2_inode
*ip
= GFS2_I(inode
);
1262 struct gfs2_holder gh
;
1265 ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
);
1269 mutex_lock(&inode
->i_mutex
);
1271 ret
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, 0, &gh
);
1275 if (gfs2_is_stuffed(ip
)) {
1276 u64 phys
= ip
->i_no_addr
<< inode
->i_blkbits
;
1277 u64 size
= i_size_read(inode
);
1278 u32 flags
= FIEMAP_EXTENT_LAST
|FIEMAP_EXTENT_NOT_ALIGNED
|
1279 FIEMAP_EXTENT_DATA_INLINE
;
1280 phys
+= sizeof(struct gfs2_dinode
);
1282 if (start
+ len
> size
)
1285 ret
= fiemap_fill_next_extent(fieinfo
, start
, phys
,
1290 ret
= __generic_block_fiemap(inode
, fieinfo
, start
, len
,
1294 gfs2_glock_dq_uninit(&gh
);
1296 mutex_unlock(&inode
->i_mutex
);
1300 const struct inode_operations gfs2_file_iops
= {
1301 .permission
= gfs2_permission
,
1302 .setattr
= gfs2_setattr
,
1303 .getattr
= gfs2_getattr
,
1304 .setxattr
= gfs2_setxattr
,
1305 .getxattr
= gfs2_getxattr
,
1306 .listxattr
= gfs2_listxattr
,
1307 .removexattr
= gfs2_removexattr
,
1308 .fiemap
= gfs2_fiemap
,
1311 const struct inode_operations gfs2_dir_iops
= {
1312 .create
= gfs2_create
,
1313 .lookup
= gfs2_lookup
,
1315 .unlink
= gfs2_unlink
,
1316 .symlink
= gfs2_symlink
,
1317 .mkdir
= gfs2_mkdir
,
1318 .rmdir
= gfs2_rmdir
,
1319 .mknod
= gfs2_mknod
,
1320 .rename
= gfs2_rename
,
1321 .permission
= gfs2_permission
,
1322 .setattr
= gfs2_setattr
,
1323 .getattr
= gfs2_getattr
,
1324 .setxattr
= gfs2_setxattr
,
1325 .getxattr
= gfs2_getxattr
,
1326 .listxattr
= gfs2_listxattr
,
1327 .removexattr
= gfs2_removexattr
,
1328 .fiemap
= gfs2_fiemap
,
1331 const struct inode_operations gfs2_symlink_iops
= {
1332 .readlink
= generic_readlink
,
1333 .follow_link
= gfs2_follow_link
,
1334 .put_link
= gfs2_put_link
,
1335 .permission
= gfs2_permission
,
1336 .setattr
= gfs2_setattr
,
1337 .getattr
= gfs2_getattr
,
1338 .setxattr
= gfs2_setxattr
,
1339 .getxattr
= gfs2_getxattr
,
1340 .listxattr
= gfs2_listxattr
,
1341 .removexattr
= gfs2_removexattr
,
1342 .fiemap
= gfs2_fiemap
,