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>
41 * gfs2_create - Create a file
42 * @dir: The directory in which to create the file
43 * @dentry: The dentry of the new file
44 * @mode: The mode of the new file
49 static int gfs2_create(struct inode
*dir
, struct dentry
*dentry
,
50 int mode
, struct nameidata
*nd
)
52 struct gfs2_inode
*dip
= GFS2_I(dir
);
53 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
54 struct gfs2_holder ghs
[2];
57 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
60 inode
= gfs2_createi(ghs
, &dentry
->d_name
, S_IFREG
| mode
, 0);
63 if (dip
->i_alloc
->al_rgd
)
64 gfs2_inplace_release(dip
);
65 gfs2_quota_unlock(dip
);
67 gfs2_glock_dq_uninit_m(2, ghs
);
68 mark_inode_dirty(inode
);
70 } else if (PTR_ERR(inode
) != -EEXIST
||
71 (nd
&& nd
->flags
& LOOKUP_EXCL
)) {
72 gfs2_holder_uninit(ghs
);
73 return PTR_ERR(inode
);
76 inode
= gfs2_lookupi(dir
, &dentry
->d_name
, 0);
79 gfs2_holder_uninit(ghs
);
82 gfs2_holder_uninit(ghs
);
83 return PTR_ERR(inode
);
88 d_instantiate(dentry
, inode
);
94 * gfs2_lookup - Look up a filename in a directory and return its inode
95 * @dir: The directory inode
96 * @dentry: The dentry of the new inode
97 * @nd: passed from Linux VFS, ignored by us
99 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
104 static struct dentry
*gfs2_lookup(struct inode
*dir
, struct dentry
*dentry
,
105 struct nameidata
*nd
)
107 struct inode
*inode
= NULL
;
109 dentry
->d_op
= &gfs2_dops
;
111 inode
= gfs2_lookupi(dir
, &dentry
->d_name
, 0);
112 if (inode
&& IS_ERR(inode
))
113 return ERR_CAST(inode
);
116 struct gfs2_glock
*gl
= GFS2_I(inode
)->i_gl
;
117 struct gfs2_holder gh
;
119 error
= gfs2_glock_nq_init(gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &gh
);
122 return ERR_PTR(error
);
124 gfs2_glock_dq_uninit(&gh
);
125 return d_splice_alias(inode
, dentry
);
127 d_add(dentry
, inode
);
133 * gfs2_link - Link to a file
134 * @old_dentry: The inode to link
135 * @dir: Add link to this directory
136 * @dentry: The name of the link
138 * Link the inode in "old_dentry" into the directory "dir" with the
144 static int gfs2_link(struct dentry
*old_dentry
, struct inode
*dir
,
145 struct dentry
*dentry
)
147 struct gfs2_inode
*dip
= GFS2_I(dir
);
148 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
149 struct inode
*inode
= old_dentry
->d_inode
;
150 struct gfs2_inode
*ip
= GFS2_I(inode
);
151 struct gfs2_holder ghs
[2];
155 if (S_ISDIR(inode
->i_mode
))
158 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
159 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
161 error
= gfs2_glock_nq(ghs
); /* parent */
165 error
= gfs2_glock_nq(ghs
+ 1); /* child */
169 error
= gfs2_permission(dir
, MAY_WRITE
| MAY_EXEC
);
173 error
= gfs2_dir_check(dir
, &dentry
->d_name
, NULL
);
184 if (!dip
->i_inode
.i_nlink
)
187 if (dip
->i_entries
== (u32
)-1)
190 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
193 if (!ip
->i_inode
.i_nlink
)
196 if (ip
->i_inode
.i_nlink
== (u32
)-1)
199 alloc_required
= error
= gfs2_diradd_alloc_required(dir
, &dentry
->d_name
);
204 if (alloc_required
) {
205 struct gfs2_alloc
*al
= gfs2_alloc_get(dip
);
211 error
= gfs2_quota_lock_check(dip
);
215 al
->al_requested
= sdp
->sd_max_dirres
;
217 error
= gfs2_inplace_reserve(dip
);
221 error
= gfs2_trans_begin(sdp
, sdp
->sd_max_dirres
+
222 al
->al_rgd
->rd_length
+
223 2 * RES_DINODE
+ RES_STATFS
+
228 error
= gfs2_trans_begin(sdp
, 2 * RES_DINODE
+ RES_LEAF
, 0);
233 error
= gfs2_dir_add(dir
, &dentry
->d_name
, ip
, IF2DT(inode
->i_mode
));
237 error
= gfs2_change_nlink(ip
, +1);
243 gfs2_inplace_release(dip
);
246 gfs2_quota_unlock(dip
);
251 gfs2_glock_dq(ghs
+ 1);
255 gfs2_holder_uninit(ghs
);
256 gfs2_holder_uninit(ghs
+ 1);
258 atomic_inc(&inode
->i_count
);
259 d_instantiate(dentry
, inode
);
260 mark_inode_dirty(inode
);
266 * gfs2_unlink - Unlink a file
267 * @dir: The inode of the directory containing the file to unlink
268 * @dentry: The file itself
270 * Unlink a file. Call gfs2_unlinki()
275 static int gfs2_unlink(struct inode
*dir
, struct dentry
*dentry
)
277 struct gfs2_inode
*dip
= GFS2_I(dir
);
278 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
279 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
280 struct gfs2_holder ghs
[3];
281 struct gfs2_rgrpd
*rgd
;
282 struct gfs2_holder ri_gh
;
285 error
= gfs2_rindex_hold(sdp
, &ri_gh
);
289 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
290 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
292 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_no_addr
);
293 gfs2_holder_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 2);
296 error
= gfs2_glock_nq(ghs
); /* parent */
300 error
= gfs2_glock_nq(ghs
+ 1); /* child */
304 error
= gfs2_glock_nq(ghs
+ 2); /* rgrp */
308 error
= gfs2_unlink_ok(dip
, &dentry
->d_name
, ip
);
312 error
= gfs2_trans_begin(sdp
, 2*RES_DINODE
+ RES_LEAF
+ RES_RG_BIT
, 0);
316 error
= gfs2_dir_del(dip
, &dentry
->d_name
);
320 error
= gfs2_change_nlink(ip
, -1);
325 gfs2_glock_dq(ghs
+ 2);
327 gfs2_holder_uninit(ghs
+ 2);
328 gfs2_glock_dq(ghs
+ 1);
330 gfs2_holder_uninit(ghs
+ 1);
333 gfs2_holder_uninit(ghs
);
334 gfs2_glock_dq_uninit(&ri_gh
);
339 * gfs2_symlink - Create a symlink
340 * @dir: The directory to create the symlink in
341 * @dentry: The dentry to put the symlink in
342 * @symname: The thing which the link points to
347 static int gfs2_symlink(struct inode
*dir
, struct dentry
*dentry
,
350 struct gfs2_inode
*dip
= GFS2_I(dir
), *ip
;
351 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
352 struct gfs2_holder ghs
[2];
354 struct buffer_head
*dibh
;
358 /* Must be stuffed with a null terminator for gfs2_follow_link() */
359 size
= strlen(symname
);
360 if (size
> sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
) - 1)
361 return -ENAMETOOLONG
;
363 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
365 inode
= gfs2_createi(ghs
, &dentry
->d_name
, S_IFLNK
| S_IRWXUGO
, 0);
367 gfs2_holder_uninit(ghs
);
368 return PTR_ERR(inode
);
371 ip
= ghs
[1].gh_gl
->gl_object
;
373 ip
->i_disksize
= size
;
375 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
377 if (!gfs2_assert_withdraw(sdp
, !error
)) {
378 gfs2_dinode_out(ip
, dibh
->b_data
);
379 memcpy(dibh
->b_data
+ sizeof(struct gfs2_dinode
), symname
,
385 if (dip
->i_alloc
->al_rgd
)
386 gfs2_inplace_release(dip
);
387 gfs2_quota_unlock(dip
);
390 gfs2_glock_dq_uninit_m(2, ghs
);
392 d_instantiate(dentry
, inode
);
393 mark_inode_dirty(inode
);
399 * gfs2_mkdir - Make a directory
400 * @dir: The parent directory of the new one
401 * @dentry: The dentry of the new directory
402 * @mode: The mode of the new directory
407 static int gfs2_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
409 struct gfs2_inode
*dip
= GFS2_I(dir
), *ip
;
410 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
411 struct gfs2_holder ghs
[2];
413 struct buffer_head
*dibh
;
416 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
418 inode
= gfs2_createi(ghs
, &dentry
->d_name
, S_IFDIR
| mode
, 0);
420 gfs2_holder_uninit(ghs
);
421 return PTR_ERR(inode
);
424 ip
= ghs
[1].gh_gl
->gl_object
;
426 ip
->i_inode
.i_nlink
= 2;
427 ip
->i_disksize
= sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
);
428 ip
->i_diskflags
|= GFS2_DIF_JDATA
;
431 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
433 if (!gfs2_assert_withdraw(sdp
, !error
)) {
434 struct gfs2_dinode
*di
= (struct gfs2_dinode
*)dibh
->b_data
;
435 struct gfs2_dirent
*dent
= (struct gfs2_dirent
*)(di
+1);
438 gfs2_str2qstr(&str
, ".");
439 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
440 gfs2_qstr2dirent(&str
, GFS2_DIRENT_SIZE(str
.len
), dent
);
441 dent
->de_inum
= di
->di_num
; /* already GFS2 endian */
442 dent
->de_type
= cpu_to_be16(DT_DIR
);
443 di
->di_entries
= cpu_to_be32(1);
445 gfs2_str2qstr(&str
, "..");
446 dent
= (struct gfs2_dirent
*)((char*)dent
+ GFS2_DIRENT_SIZE(1));
447 gfs2_qstr2dirent(&str
, dibh
->b_size
- GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode
), dent
);
449 gfs2_inum_out(dip
, dent
);
450 dent
->de_type
= cpu_to_be16(DT_DIR
);
452 gfs2_dinode_out(ip
, di
);
457 error
= gfs2_change_nlink(dip
, +1);
458 gfs2_assert_withdraw(sdp
, !error
); /* dip already pinned */
461 if (dip
->i_alloc
->al_rgd
)
462 gfs2_inplace_release(dip
);
463 gfs2_quota_unlock(dip
);
466 gfs2_glock_dq_uninit_m(2, ghs
);
468 d_instantiate(dentry
, inode
);
469 mark_inode_dirty(inode
);
475 * gfs2_rmdir - Remove a directory
476 * @dir: The parent directory of the directory to be removed
477 * @dentry: The dentry of the directory to remove
479 * Remove a directory. Call gfs2_rmdiri()
484 static int gfs2_rmdir(struct inode
*dir
, struct dentry
*dentry
)
486 struct gfs2_inode
*dip
= GFS2_I(dir
);
487 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
488 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
489 struct gfs2_holder ghs
[3];
490 struct gfs2_rgrpd
*rgd
;
491 struct gfs2_holder ri_gh
;
494 error
= gfs2_rindex_hold(sdp
, &ri_gh
);
497 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
498 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
500 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_no_addr
);
501 gfs2_holder_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 2);
503 error
= gfs2_glock_nq(ghs
); /* parent */
507 error
= gfs2_glock_nq(ghs
+ 1); /* child */
511 error
= gfs2_glock_nq(ghs
+ 2); /* rgrp */
515 error
= gfs2_unlink_ok(dip
, &dentry
->d_name
, ip
);
519 if (ip
->i_entries
< 2) {
520 if (gfs2_consist_inode(ip
))
521 gfs2_dinode_print(ip
);
525 if (ip
->i_entries
> 2) {
530 error
= gfs2_trans_begin(sdp
, 2 * RES_DINODE
+ 3 * RES_LEAF
+ RES_RG_BIT
, 0);
534 error
= gfs2_rmdiri(dip
, &dentry
->d_name
, ip
);
539 gfs2_glock_dq(ghs
+ 2);
541 gfs2_holder_uninit(ghs
+ 2);
542 gfs2_glock_dq(ghs
+ 1);
544 gfs2_holder_uninit(ghs
+ 1);
547 gfs2_holder_uninit(ghs
);
548 gfs2_glock_dq_uninit(&ri_gh
);
553 * gfs2_mknod - Make a special file
554 * @dir: The directory in which the special file will reside
555 * @dentry: The dentry of the special file
556 * @mode: The mode of the special file
557 * @rdev: The device specification of the special file
561 static int gfs2_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
,
564 struct gfs2_inode
*dip
= GFS2_I(dir
);
565 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
566 struct gfs2_holder ghs
[2];
569 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
571 inode
= gfs2_createi(ghs
, &dentry
->d_name
, mode
, dev
);
573 gfs2_holder_uninit(ghs
);
574 return PTR_ERR(inode
);
578 if (dip
->i_alloc
->al_rgd
)
579 gfs2_inplace_release(dip
);
580 gfs2_quota_unlock(dip
);
583 gfs2_glock_dq_uninit_m(2, ghs
);
585 d_instantiate(dentry
, inode
);
586 mark_inode_dirty(inode
);
592 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
596 * Follow @to back to the root and make sure we don't encounter @this
597 * Assumes we already hold the rename lock.
602 static int gfs2_ok_to_move(struct gfs2_inode
*this, struct gfs2_inode
*to
)
604 struct inode
*dir
= &to
->i_inode
;
605 struct super_block
*sb
= dir
->i_sb
;
610 gfs2_str2qstr(&dotdot
, "..");
615 if (dir
== &this->i_inode
) {
619 if (dir
== sb
->s_root
->d_inode
) {
624 tmp
= gfs2_lookupi(dir
, &dotdot
, 1);
626 error
= PTR_ERR(tmp
);
640 * gfs2_rename - Rename a file
641 * @odir: Parent directory of old file name
642 * @odentry: The old dentry of the file
643 * @ndir: Parent directory of new file name
644 * @ndentry: The new dentry of the file
649 static int gfs2_rename(struct inode
*odir
, struct dentry
*odentry
,
650 struct inode
*ndir
, struct dentry
*ndentry
)
652 struct gfs2_inode
*odip
= GFS2_I(odir
);
653 struct gfs2_inode
*ndip
= GFS2_I(ndir
);
654 struct gfs2_inode
*ip
= GFS2_I(odentry
->d_inode
);
655 struct gfs2_inode
*nip
= NULL
;
656 struct gfs2_sbd
*sdp
= GFS2_SB(odir
);
657 struct gfs2_holder ghs
[5], r_gh
= { .gh_gl
= NULL
, };
658 struct gfs2_rgrpd
*nrgd
;
665 if (ndentry
->d_inode
) {
666 nip
= GFS2_I(ndentry
->d_inode
);
673 error
= gfs2_glock_nq_init(sdp
->sd_rename_gl
, LM_ST_EXCLUSIVE
,
678 if (S_ISDIR(ip
->i_inode
.i_mode
)) {
680 /* don't move a dirctory into it's subdir */
681 error
= gfs2_ok_to_move(ip
, ndip
);
688 gfs2_holder_init(odip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
690 gfs2_holder_init(ndip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
693 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
697 gfs2_holder_init(nip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
699 /* grab the resource lock for unlink flag twiddling
700 * this is the case of the target file already existing
701 * so we unlink before doing the rename
703 nrgd
= gfs2_blk2rgrpd(sdp
, nip
->i_no_addr
);
705 gfs2_holder_init(nrgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
++);
708 for (x
= 0; x
< num_gh
; x
++) {
709 error
= gfs2_glock_nq(ghs
+ x
);
714 /* Check out the old directory */
716 error
= gfs2_unlink_ok(odip
, &odentry
->d_name
, ip
);
720 /* Check out the new directory */
723 error
= gfs2_unlink_ok(ndip
, &ndentry
->d_name
, nip
);
727 if (S_ISDIR(nip
->i_inode
.i_mode
)) {
728 if (nip
->i_entries
< 2) {
729 if (gfs2_consist_inode(nip
))
730 gfs2_dinode_print(nip
);
734 if (nip
->i_entries
> 2) {
740 error
= gfs2_permission(ndir
, MAY_WRITE
| MAY_EXEC
);
744 error
= gfs2_dir_check(ndir
, &ndentry
->d_name
, NULL
);
756 if (!ndip
->i_inode
.i_nlink
) {
760 if (ndip
->i_entries
== (u32
)-1) {
764 if (S_ISDIR(ip
->i_inode
.i_mode
) &&
765 ndip
->i_inode
.i_nlink
== (u32
)-1) {
772 /* Check out the dir to be renamed */
775 error
= gfs2_permission(odentry
->d_inode
, MAY_WRITE
);
780 alloc_required
= error
= gfs2_diradd_alloc_required(ndir
, &ndentry
->d_name
);
785 if (alloc_required
) {
786 struct gfs2_alloc
*al
= gfs2_alloc_get(ndip
);
792 error
= gfs2_quota_lock_check(ndip
);
796 al
->al_requested
= sdp
->sd_max_dirres
;
798 error
= gfs2_inplace_reserve(ndip
);
802 error
= gfs2_trans_begin(sdp
, sdp
->sd_max_dirres
+
803 al
->al_rgd
->rd_length
+
804 4 * RES_DINODE
+ 4 * RES_LEAF
+
805 RES_STATFS
+ RES_QUOTA
+ 4, 0);
809 error
= gfs2_trans_begin(sdp
, 4 * RES_DINODE
+
810 5 * RES_LEAF
+ 4, 0);
815 /* Remove the target file, if it exists */
818 if (S_ISDIR(nip
->i_inode
.i_mode
))
819 error
= gfs2_rmdiri(ndip
, &ndentry
->d_name
, nip
);
821 error
= gfs2_dir_del(ndip
, &ndentry
->d_name
);
824 error
= gfs2_change_nlink(nip
, -1);
832 gfs2_str2qstr(&name
, "..");
834 error
= gfs2_change_nlink(ndip
, +1);
837 error
= gfs2_change_nlink(odip
, -1);
841 error
= gfs2_dir_mvino(ip
, &name
, ndip
, DT_DIR
);
845 struct buffer_head
*dibh
;
846 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
849 ip
->i_inode
.i_ctime
= CURRENT_TIME
;
850 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
851 gfs2_dinode_out(ip
, dibh
->b_data
);
855 error
= gfs2_dir_del(odip
, &odentry
->d_name
);
859 error
= gfs2_dir_add(ndir
, &ndentry
->d_name
, ip
, IF2DT(ip
->i_inode
.i_mode
));
867 gfs2_inplace_release(ndip
);
870 gfs2_quota_unlock(ndip
);
873 gfs2_alloc_put(ndip
);
876 gfs2_glock_dq(ghs
+ x
);
877 gfs2_holder_uninit(ghs
+ x
);
881 gfs2_glock_dq_uninit(&r_gh
);
887 * gfs2_readlink - Read the value of a symlink
888 * @dentry: the symlink
889 * @buf: the buffer to read the symlink data into
890 * @size: the size of the buffer
895 static int gfs2_readlink(struct dentry
*dentry
, char __user
*user_buf
,
898 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
899 char array
[GFS2_FAST_NAME_SIZE
], *buf
= array
;
900 unsigned int len
= GFS2_FAST_NAME_SIZE
;
903 error
= gfs2_readlinki(ip
, &buf
, &len
);
907 if (user_size
> len
- 1)
910 if (copy_to_user(user_buf
, buf
, user_size
))
922 * gfs2_follow_link - Follow a symbolic link
923 * @dentry: The dentry of the link
924 * @nd: Data that we pass to vfs_follow_link()
926 * This can handle symlinks of any size. It is optimised for symlinks
927 * under GFS2_FAST_NAME_SIZE.
929 * Returns: 0 on success or error code
932 static void *gfs2_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
934 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
935 char array
[GFS2_FAST_NAME_SIZE
], *buf
= array
;
936 unsigned int len
= GFS2_FAST_NAME_SIZE
;
939 error
= gfs2_readlinki(ip
, &buf
, &len
);
941 error
= vfs_follow_link(nd
, buf
);
946 return ERR_PTR(error
);
953 * @nd: passed from Linux VFS, ignored by us
955 * This may be called from the VFS directly, or from within GFS2 with the
956 * inode locked, so we look to see if the glock is already locked and only
957 * lock the glock if its not already been done.
962 int gfs2_permission(struct inode
*inode
, int mask
)
964 struct gfs2_inode
*ip
= GFS2_I(inode
);
965 struct gfs2_holder i_gh
;
969 if (gfs2_glock_is_locked_by_me(ip
->i_gl
) == NULL
) {
970 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &i_gh
);
976 if ((mask
& MAY_WRITE
) && IS_IMMUTABLE(inode
))
979 error
= generic_permission(inode
, mask
, gfs2_check_acl
);
981 gfs2_glock_dq_uninit(&i_gh
);
986 static int setattr_size(struct inode
*inode
, struct iattr
*attr
)
988 struct gfs2_inode
*ip
= GFS2_I(inode
);
989 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
992 if (attr
->ia_size
!= ip
->i_disksize
) {
993 error
= gfs2_trans_begin(sdp
, 0, sdp
->sd_jdesc
->jd_blocks
);
996 error
= vmtruncate(inode
, attr
->ia_size
);
1002 error
= gfs2_truncatei(ip
, attr
->ia_size
);
1003 if (error
&& (inode
->i_size
!= ip
->i_disksize
))
1004 i_size_write(inode
, ip
->i_disksize
);
1009 static int setattr_chown(struct inode
*inode
, struct iattr
*attr
)
1011 struct gfs2_inode
*ip
= GFS2_I(inode
);
1012 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1013 struct buffer_head
*dibh
;
1014 u32 ouid
, ogid
, nuid
, ngid
;
1017 ouid
= inode
->i_uid
;
1018 ogid
= inode
->i_gid
;
1019 nuid
= attr
->ia_uid
;
1020 ngid
= attr
->ia_gid
;
1022 if (!(attr
->ia_valid
& ATTR_UID
) || ouid
== nuid
)
1023 ouid
= nuid
= NO_QUOTA_CHANGE
;
1024 if (!(attr
->ia_valid
& ATTR_GID
) || ogid
== ngid
)
1025 ogid
= ngid
= NO_QUOTA_CHANGE
;
1027 if (!gfs2_alloc_get(ip
))
1030 error
= gfs2_quota_lock(ip
, nuid
, ngid
);
1034 if (ouid
!= NO_QUOTA_CHANGE
|| ogid
!= NO_QUOTA_CHANGE
) {
1035 error
= gfs2_quota_check(ip
, nuid
, ngid
);
1040 error
= gfs2_trans_begin(sdp
, RES_DINODE
+ 2 * RES_QUOTA
, 0);
1044 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1048 error
= inode_setattr(inode
, attr
);
1049 gfs2_assert_warn(sdp
, !error
);
1051 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1052 gfs2_dinode_out(ip
, dibh
->b_data
);
1055 if (ouid
!= NO_QUOTA_CHANGE
|| ogid
!= NO_QUOTA_CHANGE
) {
1056 u64 blocks
= gfs2_get_inode_blocks(&ip
->i_inode
);
1057 gfs2_quota_change(ip
, -blocks
, ouid
, ogid
);
1058 gfs2_quota_change(ip
, blocks
, nuid
, ngid
);
1062 gfs2_trans_end(sdp
);
1064 gfs2_quota_unlock(ip
);
1071 * gfs2_setattr - Change attributes on an inode
1072 * @dentry: The dentry which is changing
1073 * @attr: The structure describing the change
1075 * The VFS layer wants to change one or more of an inodes attributes. Write
1076 * that change out to disk.
1081 static int gfs2_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1083 struct inode
*inode
= dentry
->d_inode
;
1084 struct gfs2_inode
*ip
= GFS2_I(inode
);
1085 struct gfs2_holder i_gh
;
1088 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &i_gh
);
1093 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
1096 error
= inode_change_ok(inode
, attr
);
1100 if (attr
->ia_valid
& ATTR_SIZE
)
1101 error
= setattr_size(inode
, attr
);
1102 else if (attr
->ia_valid
& (ATTR_UID
| ATTR_GID
))
1103 error
= setattr_chown(inode
, attr
);
1104 else if ((attr
->ia_valid
& ATTR_MODE
) && IS_POSIXACL(inode
))
1105 error
= gfs2_acl_chmod(ip
, attr
);
1107 error
= gfs2_setattr_simple(ip
, attr
);
1110 gfs2_glock_dq_uninit(&i_gh
);
1112 mark_inode_dirty(inode
);
1117 * gfs2_getattr - Read out an inode's attributes
1118 * @mnt: The vfsmount the inode is being accessed from
1119 * @dentry: The dentry to stat
1120 * @stat: The inode's stats
1122 * This may be called from the VFS directly, or from within GFS2 with the
1123 * inode locked, so we look to see if the glock is already locked and only
1124 * lock the glock if its not already been done. Note that its the NFS
1125 * readdirplus operation which causes this to be called (from filldir)
1126 * with the glock already held.
1131 static int gfs2_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
1134 struct inode
*inode
= dentry
->d_inode
;
1135 struct gfs2_inode
*ip
= GFS2_I(inode
);
1136 struct gfs2_holder gh
;
1140 if (gfs2_glock_is_locked_by_me(ip
->i_gl
) == NULL
) {
1141 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &gh
);
1147 generic_fillattr(inode
, stat
);
1149 gfs2_glock_dq_uninit(&gh
);
1154 static int gfs2_setxattr(struct dentry
*dentry
, const char *name
,
1155 const void *data
, size_t size
, int flags
)
1157 struct inode
*inode
= dentry
->d_inode
;
1158 struct gfs2_ea_request er
;
1160 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
1161 er
.er_type
= gfs2_ea_name2type(name
, &er
.er_name
);
1162 if (er
.er_type
== GFS2_EATYPE_UNUSED
)
1164 er
.er_data
= (char *)data
;
1165 er
.er_name_len
= strlen(er
.er_name
);
1166 er
.er_data_len
= size
;
1167 er
.er_flags
= flags
;
1169 gfs2_assert_warn(GFS2_SB(inode
), !(er
.er_flags
& GFS2_ERF_MODE
));
1171 return gfs2_ea_set(GFS2_I(inode
), &er
);
1174 static ssize_t
gfs2_getxattr(struct dentry
*dentry
, const char *name
,
1175 void *data
, size_t size
)
1177 struct gfs2_ea_request er
;
1179 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
1180 er
.er_type
= gfs2_ea_name2type(name
, &er
.er_name
);
1181 if (er
.er_type
== GFS2_EATYPE_UNUSED
)
1184 er
.er_name_len
= strlen(er
.er_name
);
1185 er
.er_data_len
= size
;
1187 return gfs2_ea_get(GFS2_I(dentry
->d_inode
), &er
);
1190 static ssize_t
gfs2_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
1192 struct gfs2_ea_request er
;
1194 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
1195 er
.er_data
= (size
) ? buffer
: NULL
;
1196 er
.er_data_len
= size
;
1198 return gfs2_ea_list(GFS2_I(dentry
->d_inode
), &er
);
1201 static int gfs2_removexattr(struct dentry
*dentry
, const char *name
)
1203 struct gfs2_ea_request er
;
1205 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
1206 er
.er_type
= gfs2_ea_name2type(name
, &er
.er_name
);
1207 if (er
.er_type
== GFS2_EATYPE_UNUSED
)
1209 er
.er_name_len
= strlen(er
.er_name
);
1211 return gfs2_ea_remove(GFS2_I(dentry
->d_inode
), &er
);
1214 static int gfs2_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
1217 struct gfs2_inode
*ip
= GFS2_I(inode
);
1218 struct gfs2_holder gh
;
1221 ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
);
1225 mutex_lock(&inode
->i_mutex
);
1227 ret
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, 0, &gh
);
1231 if (gfs2_is_stuffed(ip
)) {
1232 u64 phys
= ip
->i_no_addr
<< inode
->i_blkbits
;
1233 u64 size
= i_size_read(inode
);
1234 u32 flags
= FIEMAP_EXTENT_LAST
|FIEMAP_EXTENT_NOT_ALIGNED
|
1235 FIEMAP_EXTENT_DATA_INLINE
;
1236 phys
+= sizeof(struct gfs2_dinode
);
1238 if (start
+ len
> size
)
1241 ret
= fiemap_fill_next_extent(fieinfo
, start
, phys
,
1246 ret
= __generic_block_fiemap(inode
, fieinfo
, start
, len
,
1250 gfs2_glock_dq_uninit(&gh
);
1252 mutex_unlock(&inode
->i_mutex
);
1256 const struct inode_operations gfs2_file_iops
= {
1257 .permission
= gfs2_permission
,
1258 .setattr
= gfs2_setattr
,
1259 .getattr
= gfs2_getattr
,
1260 .setxattr
= gfs2_setxattr
,
1261 .getxattr
= gfs2_getxattr
,
1262 .listxattr
= gfs2_listxattr
,
1263 .removexattr
= gfs2_removexattr
,
1264 .fiemap
= gfs2_fiemap
,
1267 const struct inode_operations gfs2_dir_iops
= {
1268 .create
= gfs2_create
,
1269 .lookup
= gfs2_lookup
,
1271 .unlink
= gfs2_unlink
,
1272 .symlink
= gfs2_symlink
,
1273 .mkdir
= gfs2_mkdir
,
1274 .rmdir
= gfs2_rmdir
,
1275 .mknod
= gfs2_mknod
,
1276 .rename
= gfs2_rename
,
1277 .permission
= gfs2_permission
,
1278 .setattr
= gfs2_setattr
,
1279 .getattr
= gfs2_getattr
,
1280 .setxattr
= gfs2_setxattr
,
1281 .getxattr
= gfs2_getxattr
,
1282 .listxattr
= gfs2_listxattr
,
1283 .removexattr
= gfs2_removexattr
,
1284 .fiemap
= gfs2_fiemap
,
1287 const struct inode_operations gfs2_symlink_iops
= {
1288 .readlink
= gfs2_readlink
,
1289 .follow_link
= gfs2_follow_link
,
1290 .permission
= gfs2_permission
,
1291 .setattr
= gfs2_setattr
,
1292 .getattr
= gfs2_getattr
,
1293 .setxattr
= gfs2_setxattr
,
1294 .getxattr
= gfs2_getxattr
,
1295 .listxattr
= gfs2_listxattr
,
1296 .removexattr
= gfs2_removexattr
,
1297 .fiemap
= gfs2_fiemap
,