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 <asm/uaccess.h>
34 #include "ops_dentry.h"
35 #include "ops_inode.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
->intent
.open
.flags
& O_EXCL
))) {
73 gfs2_holder_uninit(ghs
);
74 return PTR_ERR(inode
);
77 inode
= gfs2_lookupi(dir
, &dentry
->d_name
, 0, nd
);
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, nd
);
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_m(2, ghs
);
166 error
= permission(dir
, MAY_WRITE
| MAY_EXEC
, NULL
);
170 error
= gfs2_dir_check(dir
, &dentry
->d_name
, NULL
);
181 if (!dip
->i_inode
.i_nlink
)
184 if (dip
->i_di
.di_entries
== (u32
)-1)
187 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
190 if (!ip
->i_inode
.i_nlink
)
193 if (ip
->i_inode
.i_nlink
== (u32
)-1)
196 alloc_required
= error
= gfs2_diradd_alloc_required(dir
, &dentry
->d_name
);
201 if (alloc_required
) {
202 struct gfs2_alloc
*al
= gfs2_alloc_get(dip
);
208 error
= gfs2_quota_lock_check(dip
);
212 al
->al_requested
= sdp
->sd_max_dirres
;
214 error
= gfs2_inplace_reserve(dip
);
218 error
= gfs2_trans_begin(sdp
, sdp
->sd_max_dirres
+
219 al
->al_rgd
->rd_length
+
220 2 * RES_DINODE
+ RES_STATFS
+
225 error
= gfs2_trans_begin(sdp
, 2 * RES_DINODE
+ RES_LEAF
, 0);
230 error
= gfs2_dir_add(dir
, &dentry
->d_name
, ip
, IF2DT(inode
->i_mode
));
234 error
= gfs2_change_nlink(ip
, +1);
240 gfs2_inplace_release(dip
);
243 gfs2_quota_unlock(dip
);
248 gfs2_glock_dq_m(2, ghs
);
250 gfs2_holder_uninit(ghs
);
251 gfs2_holder_uninit(ghs
+ 1);
253 atomic_inc(&inode
->i_count
);
254 d_instantiate(dentry
, inode
);
255 mark_inode_dirty(inode
);
261 * gfs2_unlink - Unlink a file
262 * @dir: The inode of the directory containing the file to unlink
263 * @dentry: The file itself
265 * Unlink a file. Call gfs2_unlinki()
270 static int gfs2_unlink(struct inode
*dir
, struct dentry
*dentry
)
272 struct gfs2_inode
*dip
= GFS2_I(dir
);
273 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
274 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
275 struct gfs2_holder ghs
[3];
276 struct gfs2_rgrpd
*rgd
;
277 struct gfs2_holder ri_gh
;
280 error
= gfs2_rindex_hold(sdp
, &ri_gh
);
284 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
285 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
287 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_no_addr
);
288 gfs2_holder_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 2);
291 error
= gfs2_glock_nq(ghs
); /* parent */
295 error
= gfs2_glock_nq(ghs
+ 1); /* child */
299 error
= gfs2_glock_nq(ghs
+ 2); /* rgrp */
303 error
= gfs2_unlink_ok(dip
, &dentry
->d_name
, ip
);
307 error
= gfs2_trans_begin(sdp
, 2*RES_DINODE
+ RES_LEAF
+ RES_RG_BIT
, 0);
311 error
= gfs2_dir_del(dip
, &dentry
->d_name
);
315 error
= gfs2_change_nlink(ip
, -1);
319 gfs2_glock_dq(ghs
+ 2);
321 gfs2_holder_uninit(ghs
+ 2);
322 gfs2_glock_dq(ghs
+ 1);
324 gfs2_holder_uninit(ghs
+ 1);
327 gfs2_holder_uninit(ghs
);
328 gfs2_glock_dq_uninit(&ri_gh
);
333 * gfs2_symlink - Create a symlink
334 * @dir: The directory to create the symlink in
335 * @dentry: The dentry to put the symlink in
336 * @symname: The thing which the link points to
341 static int gfs2_symlink(struct inode
*dir
, struct dentry
*dentry
,
344 struct gfs2_inode
*dip
= GFS2_I(dir
), *ip
;
345 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
346 struct gfs2_holder ghs
[2];
348 struct buffer_head
*dibh
;
352 /* Must be stuffed with a null terminator for gfs2_follow_link() */
353 size
= strlen(symname
);
354 if (size
> sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
) - 1)
355 return -ENAMETOOLONG
;
357 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
359 inode
= gfs2_createi(ghs
, &dentry
->d_name
, S_IFLNK
| S_IRWXUGO
, 0);
361 gfs2_holder_uninit(ghs
);
362 return PTR_ERR(inode
);
365 ip
= ghs
[1].gh_gl
->gl_object
;
367 ip
->i_di
.di_size
= size
;
369 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
371 if (!gfs2_assert_withdraw(sdp
, !error
)) {
372 gfs2_dinode_out(ip
, dibh
->b_data
);
373 memcpy(dibh
->b_data
+ sizeof(struct gfs2_dinode
), symname
,
379 if (dip
->i_alloc
->al_rgd
)
380 gfs2_inplace_release(dip
);
381 gfs2_quota_unlock(dip
);
384 gfs2_glock_dq_uninit_m(2, ghs
);
386 d_instantiate(dentry
, inode
);
387 mark_inode_dirty(inode
);
393 * gfs2_mkdir - Make a directory
394 * @dir: The parent directory of the new one
395 * @dentry: The dentry of the new directory
396 * @mode: The mode of the new directory
401 static int gfs2_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
403 struct gfs2_inode
*dip
= GFS2_I(dir
), *ip
;
404 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
405 struct gfs2_holder ghs
[2];
407 struct buffer_head
*dibh
;
410 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
412 inode
= gfs2_createi(ghs
, &dentry
->d_name
, S_IFDIR
| mode
, 0);
414 gfs2_holder_uninit(ghs
);
415 return PTR_ERR(inode
);
418 ip
= ghs
[1].gh_gl
->gl_object
;
420 ip
->i_inode
.i_nlink
= 2;
421 ip
->i_di
.di_size
= sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
);
422 ip
->i_di
.di_flags
|= GFS2_DIF_JDATA
;
423 ip
->i_di
.di_entries
= 2;
425 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
427 if (!gfs2_assert_withdraw(sdp
, !error
)) {
428 struct gfs2_dinode
*di
= (struct gfs2_dinode
*)dibh
->b_data
;
429 struct gfs2_dirent
*dent
= (struct gfs2_dirent
*)(di
+1);
432 gfs2_str2qstr(&str
, ".");
433 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
434 gfs2_qstr2dirent(&str
, GFS2_DIRENT_SIZE(str
.len
), dent
);
435 dent
->de_inum
= di
->di_num
; /* already GFS2 endian */
436 dent
->de_type
= cpu_to_be16(DT_DIR
);
437 di
->di_entries
= cpu_to_be32(1);
439 gfs2_str2qstr(&str
, "..");
440 dent
= (struct gfs2_dirent
*)((char*)dent
+ GFS2_DIRENT_SIZE(1));
441 gfs2_qstr2dirent(&str
, dibh
->b_size
- GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode
), dent
);
443 gfs2_inum_out(dip
, dent
);
444 dent
->de_type
= cpu_to_be16(DT_DIR
);
446 gfs2_dinode_out(ip
, di
);
451 error
= gfs2_change_nlink(dip
, +1);
452 gfs2_assert_withdraw(sdp
, !error
); /* dip already pinned */
455 if (dip
->i_alloc
->al_rgd
)
456 gfs2_inplace_release(dip
);
457 gfs2_quota_unlock(dip
);
460 gfs2_glock_dq_uninit_m(2, ghs
);
462 d_instantiate(dentry
, inode
);
463 mark_inode_dirty(inode
);
469 * gfs2_rmdir - Remove a directory
470 * @dir: The parent directory of the directory to be removed
471 * @dentry: The dentry of the directory to remove
473 * Remove a directory. Call gfs2_rmdiri()
478 static int gfs2_rmdir(struct inode
*dir
, struct dentry
*dentry
)
480 struct gfs2_inode
*dip
= GFS2_I(dir
);
481 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
482 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
483 struct gfs2_holder ghs
[3];
484 struct gfs2_rgrpd
*rgd
;
485 struct gfs2_holder ri_gh
;
489 error
= gfs2_rindex_hold(sdp
, &ri_gh
);
492 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
493 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
495 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_no_addr
);
496 gfs2_holder_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 2);
498 error
= gfs2_glock_nq_m(3, ghs
);
502 error
= gfs2_unlink_ok(dip
, &dentry
->d_name
, ip
);
506 if (ip
->i_di
.di_entries
< 2) {
507 if (gfs2_consist_inode(ip
))
508 gfs2_dinode_print(ip
);
512 if (ip
->i_di
.di_entries
> 2) {
517 error
= gfs2_trans_begin(sdp
, 2 * RES_DINODE
+ 3 * RES_LEAF
+ RES_RG_BIT
, 0);
521 error
= gfs2_rmdiri(dip
, &dentry
->d_name
, ip
);
526 gfs2_glock_dq_m(3, ghs
);
528 gfs2_holder_uninit(ghs
);
529 gfs2_holder_uninit(ghs
+ 1);
530 gfs2_holder_uninit(ghs
+ 2);
531 gfs2_glock_dq_uninit(&ri_gh
);
536 * gfs2_mknod - Make a special file
537 * @dir: The directory in which the special file will reside
538 * @dentry: The dentry of the special file
539 * @mode: The mode of the special file
540 * @rdev: The device specification of the special file
544 static int gfs2_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
,
547 struct gfs2_inode
*dip
= GFS2_I(dir
);
548 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
549 struct gfs2_holder ghs
[2];
552 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
554 inode
= gfs2_createi(ghs
, &dentry
->d_name
, mode
, dev
);
556 gfs2_holder_uninit(ghs
);
557 return PTR_ERR(inode
);
561 if (dip
->i_alloc
->al_rgd
)
562 gfs2_inplace_release(dip
);
563 gfs2_quota_unlock(dip
);
566 gfs2_glock_dq_uninit_m(2, ghs
);
568 d_instantiate(dentry
, inode
);
569 mark_inode_dirty(inode
);
575 * gfs2_rename - Rename a file
576 * @odir: Parent directory of old file name
577 * @odentry: The old dentry of the file
578 * @ndir: Parent directory of new file name
579 * @ndentry: The new dentry of the file
584 static int gfs2_rename(struct inode
*odir
, struct dentry
*odentry
,
585 struct inode
*ndir
, struct dentry
*ndentry
)
587 struct gfs2_inode
*odip
= GFS2_I(odir
);
588 struct gfs2_inode
*ndip
= GFS2_I(ndir
);
589 struct gfs2_inode
*ip
= GFS2_I(odentry
->d_inode
);
590 struct gfs2_inode
*nip
= NULL
;
591 struct gfs2_sbd
*sdp
= GFS2_SB(odir
);
592 struct gfs2_holder ghs
[5], r_gh
;
593 struct gfs2_rgrpd
*nrgd
;
600 if (ndentry
->d_inode
) {
601 nip
= GFS2_I(ndentry
->d_inode
);
606 /* Make sure we aren't trying to move a dirctory into it's subdir */
608 if (S_ISDIR(ip
->i_inode
.i_mode
) && odip
!= ndip
) {
611 error
= gfs2_glock_nq_init(sdp
->sd_rename_gl
, LM_ST_EXCLUSIVE
, 0,
616 error
= gfs2_ok_to_move(ip
, ndip
);
622 gfs2_holder_init(odip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
624 gfs2_holder_init(ndip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
627 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
631 gfs2_holder_init(nip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
633 /* grab the resource lock for unlink flag twiddling
634 * this is the case of the target file already existing
635 * so we unlink before doing the rename
637 nrgd
= gfs2_blk2rgrpd(sdp
, nip
->i_no_addr
);
639 gfs2_holder_init(nrgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
++);
642 error
= gfs2_glock_nq_m(num_gh
, ghs
);
646 /* Check out the old directory */
648 error
= gfs2_unlink_ok(odip
, &odentry
->d_name
, ip
);
652 /* Check out the new directory */
655 error
= gfs2_unlink_ok(ndip
, &ndentry
->d_name
, nip
);
659 if (S_ISDIR(nip
->i_inode
.i_mode
)) {
660 if (nip
->i_di
.di_entries
< 2) {
661 if (gfs2_consist_inode(nip
))
662 gfs2_dinode_print(nip
);
666 if (nip
->i_di
.di_entries
> 2) {
672 error
= permission(ndir
, MAY_WRITE
| MAY_EXEC
, NULL
);
676 error
= gfs2_dir_check(ndir
, &ndentry
->d_name
, NULL
);
688 if (!ndip
->i_inode
.i_nlink
) {
692 if (ndip
->i_di
.di_entries
== (u32
)-1) {
696 if (S_ISDIR(ip
->i_inode
.i_mode
) &&
697 ndip
->i_inode
.i_nlink
== (u32
)-1) {
704 /* Check out the dir to be renamed */
707 error
= permission(odentry
->d_inode
, MAY_WRITE
, NULL
);
712 alloc_required
= error
= gfs2_diradd_alloc_required(ndir
, &ndentry
->d_name
);
717 if (alloc_required
) {
718 struct gfs2_alloc
*al
= gfs2_alloc_get(ndip
);
724 error
= gfs2_quota_lock_check(ndip
);
728 al
->al_requested
= sdp
->sd_max_dirres
;
730 error
= gfs2_inplace_reserve(ndip
);
734 error
= gfs2_trans_begin(sdp
, sdp
->sd_max_dirres
+
735 al
->al_rgd
->rd_length
+
736 4 * RES_DINODE
+ 4 * RES_LEAF
+
737 RES_STATFS
+ RES_QUOTA
+ 4, 0);
741 error
= gfs2_trans_begin(sdp
, 4 * RES_DINODE
+
742 5 * RES_LEAF
+ 4, 0);
747 /* Remove the target file, if it exists */
750 if (S_ISDIR(nip
->i_inode
.i_mode
))
751 error
= gfs2_rmdiri(ndip
, &ndentry
->d_name
, nip
);
753 error
= gfs2_dir_del(ndip
, &ndentry
->d_name
);
756 error
= gfs2_change_nlink(nip
, -1);
764 gfs2_str2qstr(&name
, "..");
766 error
= gfs2_change_nlink(ndip
, +1);
769 error
= gfs2_change_nlink(odip
, -1);
773 error
= gfs2_dir_mvino(ip
, &name
, ndip
, DT_DIR
);
777 struct buffer_head
*dibh
;
778 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
781 ip
->i_inode
.i_ctime
= CURRENT_TIME
;
782 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
783 gfs2_dinode_out(ip
, dibh
->b_data
);
787 error
= gfs2_dir_del(odip
, &odentry
->d_name
);
791 error
= gfs2_dir_add(ndir
, &ndentry
->d_name
, ip
, IF2DT(ip
->i_inode
.i_mode
));
799 gfs2_inplace_release(ndip
);
802 gfs2_quota_unlock(ndip
);
805 gfs2_alloc_put(ndip
);
807 gfs2_glock_dq_m(num_gh
, ghs
);
809 for (x
= 0; x
< num_gh
; x
++)
810 gfs2_holder_uninit(ghs
+ x
);
813 gfs2_glock_dq_uninit(&r_gh
);
819 * gfs2_readlink - Read the value of a symlink
820 * @dentry: the symlink
821 * @buf: the buffer to read the symlink data into
822 * @size: the size of the buffer
827 static int gfs2_readlink(struct dentry
*dentry
, char __user
*user_buf
,
830 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
831 char array
[GFS2_FAST_NAME_SIZE
], *buf
= array
;
832 unsigned int len
= GFS2_FAST_NAME_SIZE
;
835 error
= gfs2_readlinki(ip
, &buf
, &len
);
839 if (user_size
> len
- 1)
842 if (copy_to_user(user_buf
, buf
, user_size
))
854 * gfs2_follow_link - Follow a symbolic link
855 * @dentry: The dentry of the link
856 * @nd: Data that we pass to vfs_follow_link()
858 * This can handle symlinks of any size. It is optimised for symlinks
859 * under GFS2_FAST_NAME_SIZE.
861 * Returns: 0 on success or error code
864 static void *gfs2_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
866 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
867 char array
[GFS2_FAST_NAME_SIZE
], *buf
= array
;
868 unsigned int len
= GFS2_FAST_NAME_SIZE
;
871 error
= gfs2_readlinki(ip
, &buf
, &len
);
873 error
= vfs_follow_link(nd
, buf
);
878 return ERR_PTR(error
);
885 * @nd: passed from Linux VFS, ignored by us
887 * This may be called from the VFS directly, or from within GFS2 with the
888 * inode locked, so we look to see if the glock is already locked and only
889 * lock the glock if its not already been done.
894 static int gfs2_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
896 struct gfs2_inode
*ip
= GFS2_I(inode
);
897 struct gfs2_holder i_gh
;
901 if (gfs2_glock_is_locked_by_me(ip
->i_gl
) == NULL
) {
902 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &i_gh
);
908 error
= generic_permission(inode
, mask
, gfs2_check_acl
);
910 gfs2_glock_dq_uninit(&i_gh
);
915 static int setattr_size(struct inode
*inode
, struct iattr
*attr
)
917 struct gfs2_inode
*ip
= GFS2_I(inode
);
918 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
921 if (attr
->ia_size
!= ip
->i_di
.di_size
) {
922 error
= gfs2_trans_begin(sdp
, 0, sdp
->sd_jdesc
->jd_blocks
);
925 error
= vmtruncate(inode
, attr
->ia_size
);
931 error
= gfs2_truncatei(ip
, attr
->ia_size
);
932 if (error
&& (inode
->i_size
!= ip
->i_di
.di_size
))
933 i_size_write(inode
, ip
->i_di
.di_size
);
938 static int setattr_chown(struct inode
*inode
, struct iattr
*attr
)
940 struct gfs2_inode
*ip
= GFS2_I(inode
);
941 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
942 struct buffer_head
*dibh
;
943 u32 ouid
, ogid
, nuid
, ngid
;
951 if (!(attr
->ia_valid
& ATTR_UID
) || ouid
== nuid
)
952 ouid
= nuid
= NO_QUOTA_CHANGE
;
953 if (!(attr
->ia_valid
& ATTR_GID
) || ogid
== ngid
)
954 ogid
= ngid
= NO_QUOTA_CHANGE
;
956 if (!gfs2_alloc_get(ip
))
959 error
= gfs2_quota_lock(ip
, nuid
, ngid
);
963 if (ouid
!= NO_QUOTA_CHANGE
|| ogid
!= NO_QUOTA_CHANGE
) {
964 error
= gfs2_quota_check(ip
, nuid
, ngid
);
969 error
= gfs2_trans_begin(sdp
, RES_DINODE
+ 2 * RES_QUOTA
, 0);
973 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
977 error
= inode_setattr(inode
, attr
);
978 gfs2_assert_warn(sdp
, !error
);
980 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
981 gfs2_dinode_out(ip
, dibh
->b_data
);
984 if (ouid
!= NO_QUOTA_CHANGE
|| ogid
!= NO_QUOTA_CHANGE
) {
985 u64 blocks
= gfs2_get_inode_blocks(&ip
->i_inode
);
986 gfs2_quota_change(ip
, -blocks
, ouid
, ogid
);
987 gfs2_quota_change(ip
, blocks
, nuid
, ngid
);
993 gfs2_quota_unlock(ip
);
1000 * gfs2_setattr - Change attributes on an inode
1001 * @dentry: The dentry which is changing
1002 * @attr: The structure describing the change
1004 * The VFS layer wants to change one or more of an inodes attributes. Write
1005 * that change out to disk.
1010 static int gfs2_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1012 struct inode
*inode
= dentry
->d_inode
;
1013 struct gfs2_inode
*ip
= GFS2_I(inode
);
1014 struct gfs2_holder i_gh
;
1017 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &i_gh
);
1022 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
1025 error
= inode_change_ok(inode
, attr
);
1029 if (attr
->ia_valid
& ATTR_SIZE
)
1030 error
= setattr_size(inode
, attr
);
1031 else if (attr
->ia_valid
& (ATTR_UID
| ATTR_GID
))
1032 error
= setattr_chown(inode
, attr
);
1033 else if ((attr
->ia_valid
& ATTR_MODE
) && IS_POSIXACL(inode
))
1034 error
= gfs2_acl_chmod(ip
, attr
);
1036 error
= gfs2_setattr_simple(ip
, attr
);
1039 gfs2_glock_dq_uninit(&i_gh
);
1041 mark_inode_dirty(inode
);
1046 * gfs2_getattr - Read out an inode's attributes
1047 * @mnt: The vfsmount the inode is being accessed from
1048 * @dentry: The dentry to stat
1049 * @stat: The inode's stats
1051 * This may be called from the VFS directly, or from within GFS2 with the
1052 * inode locked, so we look to see if the glock is already locked and only
1053 * lock the glock if its not already been done. Note that its the NFS
1054 * readdirplus operation which causes this to be called (from filldir)
1055 * with the glock already held.
1060 static int gfs2_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
1063 struct inode
*inode
= dentry
->d_inode
;
1064 struct gfs2_inode
*ip
= GFS2_I(inode
);
1065 struct gfs2_holder gh
;
1069 if (gfs2_glock_is_locked_by_me(ip
->i_gl
) == NULL
) {
1070 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &gh
);
1076 generic_fillattr(inode
, stat
);
1078 gfs2_glock_dq_uninit(&gh
);
1083 static int gfs2_setxattr(struct dentry
*dentry
, const char *name
,
1084 const void *data
, size_t size
, int flags
)
1086 struct inode
*inode
= dentry
->d_inode
;
1087 struct gfs2_ea_request er
;
1089 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
1090 er
.er_type
= gfs2_ea_name2type(name
, &er
.er_name
);
1091 if (er
.er_type
== GFS2_EATYPE_UNUSED
)
1093 er
.er_data
= (char *)data
;
1094 er
.er_name_len
= strlen(er
.er_name
);
1095 er
.er_data_len
= size
;
1096 er
.er_flags
= flags
;
1098 gfs2_assert_warn(GFS2_SB(inode
), !(er
.er_flags
& GFS2_ERF_MODE
));
1100 return gfs2_ea_set(GFS2_I(inode
), &er
);
1103 static ssize_t
gfs2_getxattr(struct dentry
*dentry
, const char *name
,
1104 void *data
, size_t size
)
1106 struct gfs2_ea_request er
;
1108 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
1109 er
.er_type
= gfs2_ea_name2type(name
, &er
.er_name
);
1110 if (er
.er_type
== GFS2_EATYPE_UNUSED
)
1113 er
.er_name_len
= strlen(er
.er_name
);
1114 er
.er_data_len
= size
;
1116 return gfs2_ea_get(GFS2_I(dentry
->d_inode
), &er
);
1119 static ssize_t
gfs2_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
1121 struct gfs2_ea_request er
;
1123 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
1124 er
.er_data
= (size
) ? buffer
: NULL
;
1125 er
.er_data_len
= size
;
1127 return gfs2_ea_list(GFS2_I(dentry
->d_inode
), &er
);
1130 static int gfs2_removexattr(struct dentry
*dentry
, const char *name
)
1132 struct gfs2_ea_request er
;
1134 memset(&er
, 0, sizeof(struct gfs2_ea_request
));
1135 er
.er_type
= gfs2_ea_name2type(name
, &er
.er_name
);
1136 if (er
.er_type
== GFS2_EATYPE_UNUSED
)
1138 er
.er_name_len
= strlen(er
.er_name
);
1140 return gfs2_ea_remove(GFS2_I(dentry
->d_inode
), &er
);
1143 const struct inode_operations gfs2_file_iops
= {
1144 .permission
= gfs2_permission
,
1145 .setattr
= gfs2_setattr
,
1146 .getattr
= gfs2_getattr
,
1147 .setxattr
= gfs2_setxattr
,
1148 .getxattr
= gfs2_getxattr
,
1149 .listxattr
= gfs2_listxattr
,
1150 .removexattr
= gfs2_removexattr
,
1153 const struct inode_operations gfs2_dir_iops
= {
1154 .create
= gfs2_create
,
1155 .lookup
= gfs2_lookup
,
1157 .unlink
= gfs2_unlink
,
1158 .symlink
= gfs2_symlink
,
1159 .mkdir
= gfs2_mkdir
,
1160 .rmdir
= gfs2_rmdir
,
1161 .mknod
= gfs2_mknod
,
1162 .rename
= gfs2_rename
,
1163 .permission
= gfs2_permission
,
1164 .setattr
= gfs2_setattr
,
1165 .getattr
= gfs2_getattr
,
1166 .setxattr
= gfs2_setxattr
,
1167 .getxattr
= gfs2_getxattr
,
1168 .listxattr
= gfs2_listxattr
,
1169 .removexattr
= gfs2_removexattr
,
1172 const struct inode_operations gfs2_symlink_iops
= {
1173 .readlink
= gfs2_readlink
,
1174 .follow_link
= gfs2_follow_link
,
1175 .permission
= gfs2_permission
,
1176 .setattr
= gfs2_setattr
,
1177 .getattr
= gfs2_getattr
,
1178 .setxattr
= gfs2_setxattr
,
1179 .getxattr
= gfs2_getxattr
,
1180 .listxattr
= gfs2_listxattr
,
1181 .removexattr
= gfs2_removexattr
,