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/lm_interface.h>
22 #include <linux/fiemap.h>
23 #include <asm/uaccess.h>
42 * gfs2_create - Create a file
43 * @dir: The directory in which to create the file
44 * @dentry: The dentry of the new file
45 * @mode: The mode of the new file
50 static int gfs2_create(struct inode
*dir
, struct dentry
*dentry
,
51 int mode
, struct nameidata
*nd
)
53 struct gfs2_inode
*dip
= GFS2_I(dir
);
54 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
55 struct gfs2_holder ghs
[2];
58 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
61 inode
= gfs2_createi(ghs
, &dentry
->d_name
, S_IFREG
| mode
, 0);
64 if (dip
->i_alloc
->al_rgd
)
65 gfs2_inplace_release(dip
);
66 gfs2_quota_unlock(dip
);
68 gfs2_glock_dq_uninit_m(2, ghs
);
69 mark_inode_dirty(inode
);
71 } else if (PTR_ERR(inode
) != -EEXIST
||
72 (nd
&& nd
->flags
& LOOKUP_EXCL
)) {
73 gfs2_holder_uninit(ghs
);
74 return PTR_ERR(inode
);
77 inode
= gfs2_lookupi(dir
, &dentry
->d_name
, 0);
80 gfs2_holder_uninit(ghs
);
83 gfs2_holder_uninit(ghs
);
84 return PTR_ERR(inode
);
89 d_instantiate(dentry
, inode
);
95 * gfs2_lookup - Look up a filename in a directory and return its inode
96 * @dir: The directory inode
97 * @dentry: The dentry of the new inode
98 * @nd: passed from Linux VFS, ignored by us
100 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
105 static struct dentry
*gfs2_lookup(struct inode
*dir
, struct dentry
*dentry
,
106 struct nameidata
*nd
)
108 struct inode
*inode
= NULL
;
110 dentry
->d_op
= &gfs2_dops
;
112 inode
= gfs2_lookupi(dir
, &dentry
->d_name
, 0);
113 if (inode
&& IS_ERR(inode
))
114 return ERR_CAST(inode
);
117 struct gfs2_glock
*gl
= GFS2_I(inode
)->i_gl
;
118 struct gfs2_holder gh
;
120 error
= gfs2_glock_nq_init(gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &gh
);
123 return ERR_PTR(error
);
125 gfs2_glock_dq_uninit(&gh
);
126 return d_splice_alias(inode
, dentry
);
128 d_add(dentry
, inode
);
134 * gfs2_link - Link to a file
135 * @old_dentry: The inode to link
136 * @dir: Add link to this directory
137 * @dentry: The name of the link
139 * Link the inode in "old_dentry" into the directory "dir" with the
145 static int gfs2_link(struct dentry
*old_dentry
, struct inode
*dir
,
146 struct dentry
*dentry
)
148 struct gfs2_inode
*dip
= GFS2_I(dir
);
149 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
150 struct inode
*inode
= old_dentry
->d_inode
;
151 struct gfs2_inode
*ip
= GFS2_I(inode
);
152 struct gfs2_holder ghs
[2];
156 if (S_ISDIR(inode
->i_mode
))
159 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
160 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
162 error
= gfs2_glock_nq(ghs
); /* parent */
166 error
= gfs2_glock_nq(ghs
+ 1); /* child */
170 error
= gfs2_permission(dir
, MAY_WRITE
| MAY_EXEC
);
174 error
= gfs2_dir_check(dir
, &dentry
->d_name
, NULL
);
185 if (!dip
->i_inode
.i_nlink
)
188 if (dip
->i_entries
== (u32
)-1)
191 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
194 if (!ip
->i_inode
.i_nlink
)
197 if (ip
->i_inode
.i_nlink
== (u32
)-1)
200 alloc_required
= error
= gfs2_diradd_alloc_required(dir
, &dentry
->d_name
);
205 if (alloc_required
) {
206 struct gfs2_alloc
*al
= gfs2_alloc_get(dip
);
212 error
= gfs2_quota_lock_check(dip
);
216 al
->al_requested
= sdp
->sd_max_dirres
;
218 error
= gfs2_inplace_reserve(dip
);
222 error
= gfs2_trans_begin(sdp
, sdp
->sd_max_dirres
+
223 al
->al_rgd
->rd_length
+
224 2 * RES_DINODE
+ RES_STATFS
+
229 error
= gfs2_trans_begin(sdp
, 2 * RES_DINODE
+ RES_LEAF
, 0);
234 error
= gfs2_dir_add(dir
, &dentry
->d_name
, ip
, IF2DT(inode
->i_mode
));
238 error
= gfs2_change_nlink(ip
, +1);
244 gfs2_inplace_release(dip
);
247 gfs2_quota_unlock(dip
);
252 gfs2_glock_dq(ghs
+ 1);
256 gfs2_holder_uninit(ghs
);
257 gfs2_holder_uninit(ghs
+ 1);
259 atomic_inc(&inode
->i_count
);
260 d_instantiate(dentry
, inode
);
261 mark_inode_dirty(inode
);
267 * gfs2_unlink - Unlink a file
268 * @dir: The inode of the directory containing the file to unlink
269 * @dentry: The file itself
271 * Unlink a file. Call gfs2_unlinki()
276 static int gfs2_unlink(struct inode
*dir
, struct dentry
*dentry
)
278 struct gfs2_inode
*dip
= GFS2_I(dir
);
279 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
280 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
281 struct gfs2_holder ghs
[3];
282 struct gfs2_rgrpd
*rgd
;
283 struct gfs2_holder ri_gh
;
286 error
= gfs2_rindex_hold(sdp
, &ri_gh
);
290 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
291 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
293 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_no_addr
);
294 gfs2_holder_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 2);
297 error
= gfs2_glock_nq(ghs
); /* parent */
301 error
= gfs2_glock_nq(ghs
+ 1); /* child */
305 error
= gfs2_glock_nq(ghs
+ 2); /* rgrp */
309 error
= gfs2_unlink_ok(dip
, &dentry
->d_name
, ip
);
313 error
= gfs2_trans_begin(sdp
, 2*RES_DINODE
+ RES_LEAF
+ RES_RG_BIT
, 0);
317 error
= gfs2_dir_del(dip
, &dentry
->d_name
);
321 error
= gfs2_change_nlink(ip
, -1);
326 gfs2_glock_dq(ghs
+ 2);
328 gfs2_holder_uninit(ghs
+ 2);
329 gfs2_glock_dq(ghs
+ 1);
331 gfs2_holder_uninit(ghs
+ 1);
334 gfs2_holder_uninit(ghs
);
335 gfs2_glock_dq_uninit(&ri_gh
);
340 * gfs2_symlink - Create a symlink
341 * @dir: The directory to create the symlink in
342 * @dentry: The dentry to put the symlink in
343 * @symname: The thing which the link points to
348 static int gfs2_symlink(struct inode
*dir
, struct dentry
*dentry
,
351 struct gfs2_inode
*dip
= GFS2_I(dir
), *ip
;
352 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
353 struct gfs2_holder ghs
[2];
355 struct buffer_head
*dibh
;
359 /* Must be stuffed with a null terminator for gfs2_follow_link() */
360 size
= strlen(symname
);
361 if (size
> sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
) - 1)
362 return -ENAMETOOLONG
;
364 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
366 inode
= gfs2_createi(ghs
, &dentry
->d_name
, S_IFLNK
| S_IRWXUGO
, 0);
368 gfs2_holder_uninit(ghs
);
369 return PTR_ERR(inode
);
372 ip
= ghs
[1].gh_gl
->gl_object
;
374 ip
->i_disksize
= size
;
376 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
378 if (!gfs2_assert_withdraw(sdp
, !error
)) {
379 gfs2_dinode_out(ip
, dibh
->b_data
);
380 memcpy(dibh
->b_data
+ sizeof(struct gfs2_dinode
), symname
,
386 if (dip
->i_alloc
->al_rgd
)
387 gfs2_inplace_release(dip
);
388 gfs2_quota_unlock(dip
);
391 gfs2_glock_dq_uninit_m(2, ghs
);
393 d_instantiate(dentry
, inode
);
394 mark_inode_dirty(inode
);
400 * gfs2_mkdir - Make a directory
401 * @dir: The parent directory of the new one
402 * @dentry: The dentry of the new directory
403 * @mode: The mode of the new directory
408 static int gfs2_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
410 struct gfs2_inode
*dip
= GFS2_I(dir
), *ip
;
411 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
412 struct gfs2_holder ghs
[2];
414 struct buffer_head
*dibh
;
417 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
419 inode
= gfs2_createi(ghs
, &dentry
->d_name
, S_IFDIR
| mode
, 0);
421 gfs2_holder_uninit(ghs
);
422 return PTR_ERR(inode
);
425 ip
= ghs
[1].gh_gl
->gl_object
;
427 ip
->i_inode
.i_nlink
= 2;
428 ip
->i_disksize
= sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
);
429 ip
->i_diskflags
|= GFS2_DIF_JDATA
;
432 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
434 if (!gfs2_assert_withdraw(sdp
, !error
)) {
435 struct gfs2_dinode
*di
= (struct gfs2_dinode
*)dibh
->b_data
;
436 struct gfs2_dirent
*dent
= (struct gfs2_dirent
*)(di
+1);
439 gfs2_str2qstr(&str
, ".");
440 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
441 gfs2_qstr2dirent(&str
, GFS2_DIRENT_SIZE(str
.len
), dent
);
442 dent
->de_inum
= di
->di_num
; /* already GFS2 endian */
443 dent
->de_type
= cpu_to_be16(DT_DIR
);
444 di
->di_entries
= cpu_to_be32(1);
446 gfs2_str2qstr(&str
, "..");
447 dent
= (struct gfs2_dirent
*)((char*)dent
+ GFS2_DIRENT_SIZE(1));
448 gfs2_qstr2dirent(&str
, dibh
->b_size
- GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode
), dent
);
450 gfs2_inum_out(dip
, dent
);
451 dent
->de_type
= cpu_to_be16(DT_DIR
);
453 gfs2_dinode_out(ip
, di
);
458 error
= gfs2_change_nlink(dip
, +1);
459 gfs2_assert_withdraw(sdp
, !error
); /* dip already pinned */
462 if (dip
->i_alloc
->al_rgd
)
463 gfs2_inplace_release(dip
);
464 gfs2_quota_unlock(dip
);
467 gfs2_glock_dq_uninit_m(2, ghs
);
469 d_instantiate(dentry
, inode
);
470 mark_inode_dirty(inode
);
476 * gfs2_rmdir - Remove a directory
477 * @dir: The parent directory of the directory to be removed
478 * @dentry: The dentry of the directory to remove
480 * Remove a directory. Call gfs2_rmdiri()
485 static int gfs2_rmdir(struct inode
*dir
, struct dentry
*dentry
)
487 struct gfs2_inode
*dip
= GFS2_I(dir
);
488 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
489 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
490 struct gfs2_holder ghs
[3];
491 struct gfs2_rgrpd
*rgd
;
492 struct gfs2_holder ri_gh
;
495 error
= gfs2_rindex_hold(sdp
, &ri_gh
);
498 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
499 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
501 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_no_addr
);
502 gfs2_holder_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 2);
504 error
= gfs2_glock_nq(ghs
); /* parent */
508 error
= gfs2_glock_nq(ghs
+ 1); /* child */
512 error
= gfs2_glock_nq(ghs
+ 2); /* rgrp */
516 error
= gfs2_unlink_ok(dip
, &dentry
->d_name
, ip
);
520 if (ip
->i_entries
< 2) {
521 if (gfs2_consist_inode(ip
))
522 gfs2_dinode_print(ip
);
526 if (ip
->i_entries
> 2) {
531 error
= gfs2_trans_begin(sdp
, 2 * RES_DINODE
+ 3 * RES_LEAF
+ RES_RG_BIT
, 0);
535 error
= gfs2_rmdiri(dip
, &dentry
->d_name
, ip
);
540 gfs2_glock_dq(ghs
+ 2);
542 gfs2_holder_uninit(ghs
+ 2);
543 gfs2_glock_dq(ghs
+ 1);
545 gfs2_holder_uninit(ghs
+ 1);
548 gfs2_holder_uninit(ghs
);
549 gfs2_glock_dq_uninit(&ri_gh
);
554 * gfs2_mknod - Make a special file
555 * @dir: The directory in which the special file will reside
556 * @dentry: The dentry of the special file
557 * @mode: The mode of the special file
558 * @rdev: The device specification of the special file
562 static int gfs2_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
,
565 struct gfs2_inode
*dip
= GFS2_I(dir
);
566 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
567 struct gfs2_holder ghs
[2];
570 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
572 inode
= gfs2_createi(ghs
, &dentry
->d_name
, mode
, dev
);
574 gfs2_holder_uninit(ghs
);
575 return PTR_ERR(inode
);
579 if (dip
->i_alloc
->al_rgd
)
580 gfs2_inplace_release(dip
);
581 gfs2_quota_unlock(dip
);
584 gfs2_glock_dq_uninit_m(2, ghs
);
586 d_instantiate(dentry
, inode
);
587 mark_inode_dirty(inode
);
593 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
597 * Follow @to back to the root and make sure we don't encounter @this
598 * Assumes we already hold the rename lock.
603 static int gfs2_ok_to_move(struct gfs2_inode
*this, struct gfs2_inode
*to
)
605 struct inode
*dir
= &to
->i_inode
;
606 struct super_block
*sb
= dir
->i_sb
;
611 gfs2_str2qstr(&dotdot
, "..");
616 if (dir
== &this->i_inode
) {
620 if (dir
== sb
->s_root
->d_inode
) {
625 tmp
= gfs2_lookupi(dir
, &dotdot
, 1);
627 error
= PTR_ERR(tmp
);
641 * gfs2_rename - Rename a file
642 * @odir: Parent directory of old file name
643 * @odentry: The old dentry of the file
644 * @ndir: Parent directory of new file name
645 * @ndentry: The new dentry of the file
650 static int gfs2_rename(struct inode
*odir
, struct dentry
*odentry
,
651 struct inode
*ndir
, struct dentry
*ndentry
)
653 struct gfs2_inode
*odip
= GFS2_I(odir
);
654 struct gfs2_inode
*ndip
= GFS2_I(ndir
);
655 struct gfs2_inode
*ip
= GFS2_I(odentry
->d_inode
);
656 struct gfs2_inode
*nip
= NULL
;
657 struct gfs2_sbd
*sdp
= GFS2_SB(odir
);
658 struct gfs2_holder ghs
[5], r_gh
= { .gh_gl
= NULL
, };
659 struct gfs2_rgrpd
*nrgd
;
666 if (ndentry
->d_inode
) {
667 nip
= GFS2_I(ndentry
->d_inode
);
674 error
= gfs2_glock_nq_init(sdp
->sd_rename_gl
, LM_ST_EXCLUSIVE
,
679 if (S_ISDIR(ip
->i_inode
.i_mode
)) {
681 /* don't move a dirctory into it's subdir */
682 error
= gfs2_ok_to_move(ip
, ndip
);
689 gfs2_holder_init(odip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
691 gfs2_holder_init(ndip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
694 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
698 gfs2_holder_init(nip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
700 /* grab the resource lock for unlink flag twiddling
701 * this is the case of the target file already existing
702 * so we unlink before doing the rename
704 nrgd
= gfs2_blk2rgrpd(sdp
, nip
->i_no_addr
);
706 gfs2_holder_init(nrgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
++);
709 for (x
= 0; x
< num_gh
; x
++) {
710 error
= gfs2_glock_nq(ghs
+ x
);
715 /* Check out the old directory */
717 error
= gfs2_unlink_ok(odip
, &odentry
->d_name
, ip
);
721 /* Check out the new directory */
724 error
= gfs2_unlink_ok(ndip
, &ndentry
->d_name
, nip
);
728 if (S_ISDIR(nip
->i_inode
.i_mode
)) {
729 if (nip
->i_entries
< 2) {
730 if (gfs2_consist_inode(nip
))
731 gfs2_dinode_print(nip
);
735 if (nip
->i_entries
> 2) {
741 error
= gfs2_permission(ndir
, MAY_WRITE
| MAY_EXEC
);
745 error
= gfs2_dir_check(ndir
, &ndentry
->d_name
, NULL
);
757 if (!ndip
->i_inode
.i_nlink
) {
761 if (ndip
->i_entries
== (u32
)-1) {
765 if (S_ISDIR(ip
->i_inode
.i_mode
) &&
766 ndip
->i_inode
.i_nlink
== (u32
)-1) {
773 /* Check out the dir to be renamed */
776 error
= gfs2_permission(odentry
->d_inode
, MAY_WRITE
);
781 alloc_required
= error
= gfs2_diradd_alloc_required(ndir
, &ndentry
->d_name
);
786 if (alloc_required
) {
787 struct gfs2_alloc
*al
= gfs2_alloc_get(ndip
);
793 error
= gfs2_quota_lock_check(ndip
);
797 al
->al_requested
= sdp
->sd_max_dirres
;
799 error
= gfs2_inplace_reserve(ndip
);
803 error
= gfs2_trans_begin(sdp
, sdp
->sd_max_dirres
+
804 al
->al_rgd
->rd_length
+
805 4 * RES_DINODE
+ 4 * RES_LEAF
+
806 RES_STATFS
+ RES_QUOTA
+ 4, 0);
810 error
= gfs2_trans_begin(sdp
, 4 * RES_DINODE
+
811 5 * RES_LEAF
+ 4, 0);
816 /* Remove the target file, if it exists */
819 if (S_ISDIR(nip
->i_inode
.i_mode
))
820 error
= gfs2_rmdiri(ndip
, &ndentry
->d_name
, nip
);
822 error
= gfs2_dir_del(ndip
, &ndentry
->d_name
);
825 error
= gfs2_change_nlink(nip
, -1);
833 gfs2_str2qstr(&name
, "..");
835 error
= gfs2_change_nlink(ndip
, +1);
838 error
= gfs2_change_nlink(odip
, -1);
842 error
= gfs2_dir_mvino(ip
, &name
, ndip
, DT_DIR
);
846 struct buffer_head
*dibh
;
847 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
850 ip
->i_inode
.i_ctime
= CURRENT_TIME
;
851 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
852 gfs2_dinode_out(ip
, dibh
->b_data
);
856 error
= gfs2_dir_del(odip
, &odentry
->d_name
);
860 error
= gfs2_dir_add(ndir
, &ndentry
->d_name
, ip
, IF2DT(ip
->i_inode
.i_mode
));
868 gfs2_inplace_release(ndip
);
871 gfs2_quota_unlock(ndip
);
874 gfs2_alloc_put(ndip
);
877 gfs2_glock_dq(ghs
+ x
);
878 gfs2_holder_uninit(ghs
+ x
);
882 gfs2_glock_dq_uninit(&r_gh
);
888 * gfs2_readlink - Read the value of a symlink
889 * @dentry: the symlink
890 * @buf: the buffer to read the symlink data into
891 * @size: the size of the buffer
896 static int gfs2_readlink(struct dentry
*dentry
, char __user
*user_buf
,
899 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
900 char array
[GFS2_FAST_NAME_SIZE
], *buf
= array
;
901 unsigned int len
= GFS2_FAST_NAME_SIZE
;
904 error
= gfs2_readlinki(ip
, &buf
, &len
);
908 if (user_size
> len
- 1)
911 if (copy_to_user(user_buf
, buf
, user_size
))
923 * gfs2_follow_link - Follow a symbolic link
924 * @dentry: The dentry of the link
925 * @nd: Data that we pass to vfs_follow_link()
927 * This can handle symlinks of any size. It is optimised for symlinks
928 * under GFS2_FAST_NAME_SIZE.
930 * Returns: 0 on success or error code
933 static void *gfs2_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
935 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
936 char array
[GFS2_FAST_NAME_SIZE
], *buf
= array
;
937 unsigned int len
= GFS2_FAST_NAME_SIZE
;
940 error
= gfs2_readlinki(ip
, &buf
, &len
);
942 error
= vfs_follow_link(nd
, buf
);
947 return ERR_PTR(error
);
954 * @nd: passed from Linux VFS, ignored by us
956 * This may be called from the VFS directly, or from within GFS2 with the
957 * inode locked, so we look to see if the glock is already locked and only
958 * lock the glock if its not already been done.
963 int gfs2_permission(struct inode
*inode
, int mask
)
965 struct gfs2_inode
*ip
= GFS2_I(inode
);
966 struct gfs2_holder i_gh
;
970 if (gfs2_glock_is_locked_by_me(ip
->i_gl
) == NULL
) {
971 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &i_gh
);
977 if ((mask
& MAY_WRITE
) && IS_IMMUTABLE(inode
))
980 error
= generic_permission(inode
, mask
, gfs2_check_acl
);
982 gfs2_glock_dq_uninit(&i_gh
);
987 static int setattr_size(struct inode
*inode
, struct iattr
*attr
)
989 struct gfs2_inode
*ip
= GFS2_I(inode
);
990 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
993 if (attr
->ia_size
!= ip
->i_disksize
) {
994 error
= gfs2_trans_begin(sdp
, 0, sdp
->sd_jdesc
->jd_blocks
);
997 error
= vmtruncate(inode
, attr
->ia_size
);
1003 error
= gfs2_truncatei(ip
, attr
->ia_size
);
1004 if (error
&& (inode
->i_size
!= ip
->i_disksize
))
1005 i_size_write(inode
, ip
->i_disksize
);
1010 static int setattr_chown(struct inode
*inode
, struct iattr
*attr
)
1012 struct gfs2_inode
*ip
= GFS2_I(inode
);
1013 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1014 struct buffer_head
*dibh
;
1015 u32 ouid
, ogid
, nuid
, ngid
;
1018 ouid
= inode
->i_uid
;
1019 ogid
= inode
->i_gid
;
1020 nuid
= attr
->ia_uid
;
1021 ngid
= attr
->ia_gid
;
1023 if (!(attr
->ia_valid
& ATTR_UID
) || ouid
== nuid
)
1024 ouid
= nuid
= NO_QUOTA_CHANGE
;
1025 if (!(attr
->ia_valid
& ATTR_GID
) || ogid
== ngid
)
1026 ogid
= ngid
= NO_QUOTA_CHANGE
;
1028 if (!gfs2_alloc_get(ip
))
1031 error
= gfs2_quota_lock(ip
, nuid
, ngid
);
1035 if (ouid
!= NO_QUOTA_CHANGE
|| ogid
!= NO_QUOTA_CHANGE
) {
1036 error
= gfs2_quota_check(ip
, nuid
, ngid
);
1041 error
= gfs2_trans_begin(sdp
, RES_DINODE
+ 2 * RES_QUOTA
, 0);
1045 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1049 error
= inode_setattr(inode
, attr
);
1050 gfs2_assert_warn(sdp
, !error
);
1052 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1053 gfs2_dinode_out(ip
, dibh
->b_data
);
1056 if (ouid
!= NO_QUOTA_CHANGE
|| ogid
!= NO_QUOTA_CHANGE
) {
1057 u64 blocks
= gfs2_get_inode_blocks(&ip
->i_inode
);
1058 gfs2_quota_change(ip
, -blocks
, ouid
, ogid
);
1059 gfs2_quota_change(ip
, blocks
, nuid
, ngid
);
1063 gfs2_trans_end(sdp
);
1065 gfs2_quota_unlock(ip
);
1072 * gfs2_setattr - Change attributes on an inode
1073 * @dentry: The dentry which is changing
1074 * @attr: The structure describing the change
1076 * The VFS layer wants to change one or more of an inodes attributes. Write
1077 * that change out to disk.
1082 static int gfs2_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1084 struct inode
*inode
= dentry
->d_inode
;
1085 struct gfs2_inode
*ip
= GFS2_I(inode
);
1086 struct gfs2_holder i_gh
;
1089 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &i_gh
);
1094 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
1097 error
= inode_change_ok(inode
, attr
);
1101 if (attr
->ia_valid
& ATTR_SIZE
)
1102 error
= setattr_size(inode
, attr
);
1103 else if (attr
->ia_valid
& (ATTR_UID
| ATTR_GID
))
1104 error
= setattr_chown(inode
, attr
);
1105 else if ((attr
->ia_valid
& ATTR_MODE
) && IS_POSIXACL(inode
))
1106 error
= gfs2_acl_chmod(ip
, attr
);
1108 error
= gfs2_setattr_simple(ip
, attr
);
1111 gfs2_glock_dq_uninit(&i_gh
);
1113 mark_inode_dirty(inode
);
1118 * gfs2_getattr - Read out an inode's attributes
1119 * @mnt: The vfsmount the inode is being accessed from
1120 * @dentry: The dentry to stat
1121 * @stat: The inode's stats
1123 * This may be called from the VFS directly, or from within GFS2 with the
1124 * inode locked, so we look to see if the glock is already locked and only
1125 * lock the glock if its not already been done. Note that its the NFS
1126 * readdirplus operation which causes this to be called (from filldir)
1127 * with the glock already held.
1132 static int gfs2_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
1135 struct inode
*inode
= dentry
->d_inode
;
1136 struct gfs2_inode
*ip
= GFS2_I(inode
);
1137 struct gfs2_holder gh
;
1141 if (gfs2_glock_is_locked_by_me(ip
->i_gl
) == NULL
) {
1142 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &gh
);
1148 generic_fillattr(inode
, stat
);
1150 gfs2_glock_dq_uninit(&gh
);
1155 static int gfs2_setxattr(struct dentry
*dentry
, const char *name
,
1156 const void *data
, size_t size
, int flags
)
1158 struct inode
*inode
= dentry
->d_inode
;
1159 struct gfs2_ea_request er
;
1161 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
1162 er
.er_type
= gfs2_ea_name2type(name
, &er
.er_name
);
1163 if (er
.er_type
== GFS2_EATYPE_UNUSED
)
1165 er
.er_data
= (char *)data
;
1166 er
.er_name_len
= strlen(er
.er_name
);
1167 er
.er_data_len
= size
;
1168 er
.er_flags
= flags
;
1170 gfs2_assert_warn(GFS2_SB(inode
), !(er
.er_flags
& GFS2_ERF_MODE
));
1172 return gfs2_ea_set(GFS2_I(inode
), &er
);
1175 static ssize_t
gfs2_getxattr(struct dentry
*dentry
, const char *name
,
1176 void *data
, size_t size
)
1178 struct gfs2_ea_request er
;
1180 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
1181 er
.er_type
= gfs2_ea_name2type(name
, &er
.er_name
);
1182 if (er
.er_type
== GFS2_EATYPE_UNUSED
)
1185 er
.er_name_len
= strlen(er
.er_name
);
1186 er
.er_data_len
= size
;
1188 return gfs2_ea_get(GFS2_I(dentry
->d_inode
), &er
);
1191 static ssize_t
gfs2_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
1193 struct gfs2_ea_request er
;
1195 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
1196 er
.er_data
= (size
) ? buffer
: NULL
;
1197 er
.er_data_len
= size
;
1199 return gfs2_ea_list(GFS2_I(dentry
->d_inode
), &er
);
1202 static int gfs2_removexattr(struct dentry
*dentry
, const char *name
)
1204 struct gfs2_ea_request er
;
1206 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
1207 er
.er_type
= gfs2_ea_name2type(name
, &er
.er_name
);
1208 if (er
.er_type
== GFS2_EATYPE_UNUSED
)
1210 er
.er_name_len
= strlen(er
.er_name
);
1212 return gfs2_ea_remove(GFS2_I(dentry
->d_inode
), &er
);
1215 static int gfs2_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
1218 struct gfs2_inode
*ip
= GFS2_I(inode
);
1219 struct gfs2_holder gh
;
1222 ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
);
1226 mutex_lock(&inode
->i_mutex
);
1228 ret
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, 0, &gh
);
1232 if (gfs2_is_stuffed(ip
)) {
1233 u64 phys
= ip
->i_no_addr
<< inode
->i_blkbits
;
1234 u64 size
= i_size_read(inode
);
1235 u32 flags
= FIEMAP_EXTENT_LAST
|FIEMAP_EXTENT_NOT_ALIGNED
|
1236 FIEMAP_EXTENT_DATA_INLINE
;
1237 phys
+= sizeof(struct gfs2_dinode
);
1239 if (start
+ len
> size
)
1242 ret
= fiemap_fill_next_extent(fieinfo
, start
, phys
,
1247 ret
= __generic_block_fiemap(inode
, fieinfo
, start
, len
,
1251 gfs2_glock_dq_uninit(&gh
);
1253 mutex_unlock(&inode
->i_mutex
);
1257 const struct inode_operations gfs2_file_iops
= {
1258 .permission
= gfs2_permission
,
1259 .setattr
= gfs2_setattr
,
1260 .getattr
= gfs2_getattr
,
1261 .setxattr
= gfs2_setxattr
,
1262 .getxattr
= gfs2_getxattr
,
1263 .listxattr
= gfs2_listxattr
,
1264 .removexattr
= gfs2_removexattr
,
1265 .fiemap
= gfs2_fiemap
,
1268 const struct inode_operations gfs2_dir_iops
= {
1269 .create
= gfs2_create
,
1270 .lookup
= gfs2_lookup
,
1272 .unlink
= gfs2_unlink
,
1273 .symlink
= gfs2_symlink
,
1274 .mkdir
= gfs2_mkdir
,
1275 .rmdir
= gfs2_rmdir
,
1276 .mknod
= gfs2_mknod
,
1277 .rename
= gfs2_rename
,
1278 .permission
= gfs2_permission
,
1279 .setattr
= gfs2_setattr
,
1280 .getattr
= gfs2_getattr
,
1281 .setxattr
= gfs2_setxattr
,
1282 .getxattr
= gfs2_getxattr
,
1283 .listxattr
= gfs2_listxattr
,
1284 .removexattr
= gfs2_removexattr
,
1285 .fiemap
= gfs2_fiemap
,
1288 const struct inode_operations gfs2_symlink_iops
= {
1289 .readlink
= gfs2_readlink
,
1290 .follow_link
= gfs2_follow_link
,
1291 .permission
= gfs2_permission
,
1292 .setattr
= gfs2_setattr
,
1293 .getattr
= gfs2_getattr
,
1294 .setxattr
= gfs2_setxattr
,
1295 .getxattr
= gfs2_getxattr
,
1296 .listxattr
= gfs2_listxattr
,
1297 .removexattr
= gfs2_removexattr
,
1298 .fiemap
= gfs2_fiemap
,