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 <linux/swap.h>
22 #include <linux/falloc.h>
23 #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 inode
= gfs2_lookupi(dir
, &dentry
->d_name
, 0);
110 if (inode
&& IS_ERR(inode
))
111 return ERR_CAST(inode
);
114 struct gfs2_glock
*gl
= GFS2_I(inode
)->i_gl
;
115 struct gfs2_holder gh
;
117 error
= gfs2_glock_nq_init(gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &gh
);
120 return ERR_PTR(error
);
122 gfs2_glock_dq_uninit(&gh
);
123 return d_splice_alias(inode
, dentry
);
125 d_add(dentry
, inode
);
131 * gfs2_link - Link to a file
132 * @old_dentry: The inode to link
133 * @dir: Add link to this directory
134 * @dentry: The name of the link
136 * Link the inode in "old_dentry" into the directory "dir" with the
142 static int gfs2_link(struct dentry
*old_dentry
, struct inode
*dir
,
143 struct dentry
*dentry
)
145 struct gfs2_inode
*dip
= GFS2_I(dir
);
146 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
147 struct inode
*inode
= old_dentry
->d_inode
;
148 struct gfs2_inode
*ip
= GFS2_I(inode
);
149 struct gfs2_holder ghs
[2];
153 if (S_ISDIR(inode
->i_mode
))
156 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
157 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
159 error
= gfs2_glock_nq(ghs
); /* parent */
163 error
= gfs2_glock_nq(ghs
+ 1); /* child */
167 error
= gfs2_permission(dir
, MAY_WRITE
| MAY_EXEC
, 0);
171 error
= gfs2_dir_check(dir
, &dentry
->d_name
, NULL
);
182 if (!dip
->i_inode
.i_nlink
)
185 if (dip
->i_entries
== (u32
)-1)
188 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
191 if (!ip
->i_inode
.i_nlink
)
194 if (ip
->i_inode
.i_nlink
== (u32
)-1)
197 alloc_required
= error
= gfs2_diradd_alloc_required(dir
, &dentry
->d_name
);
202 if (alloc_required
) {
203 struct gfs2_alloc
*al
= gfs2_alloc_get(dip
);
209 error
= gfs2_quota_lock_check(dip
);
213 al
->al_requested
= sdp
->sd_max_dirres
;
215 error
= gfs2_inplace_reserve(dip
);
219 error
= gfs2_trans_begin(sdp
, sdp
->sd_max_dirres
+
221 2 * RES_DINODE
+ RES_STATFS
+
226 error
= gfs2_trans_begin(sdp
, 2 * RES_DINODE
+ RES_LEAF
, 0);
231 error
= gfs2_dir_add(dir
, &dentry
->d_name
, ip
, IF2DT(inode
->i_mode
));
235 error
= gfs2_change_nlink(ip
, +1);
241 gfs2_inplace_release(dip
);
244 gfs2_quota_unlock(dip
);
249 gfs2_glock_dq(ghs
+ 1);
253 gfs2_holder_uninit(ghs
);
254 gfs2_holder_uninit(ghs
+ 1);
257 d_instantiate(dentry
, inode
);
258 mark_inode_dirty(inode
);
264 * gfs2_unlink_ok - check to see that a inode is still in a directory
265 * @dip: the directory
266 * @name: the name of the file
269 * Assumes that the lock on (at least) @dip is held.
271 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
274 static int gfs2_unlink_ok(struct gfs2_inode
*dip
, const struct qstr
*name
,
275 const struct gfs2_inode
*ip
)
279 if (IS_IMMUTABLE(&ip
->i_inode
) || IS_APPEND(&ip
->i_inode
))
282 if ((dip
->i_inode
.i_mode
& S_ISVTX
) &&
283 dip
->i_inode
.i_uid
!= current_fsuid() &&
284 ip
->i_inode
.i_uid
!= current_fsuid() && !capable(CAP_FOWNER
))
287 if (IS_APPEND(&dip
->i_inode
))
290 error
= gfs2_permission(&dip
->i_inode
, MAY_WRITE
| MAY_EXEC
, 0);
294 error
= gfs2_dir_check(&dip
->i_inode
, name
, ip
);
302 * gfs2_unlink - Unlink a file
303 * @dir: The inode of the directory containing the file to unlink
304 * @dentry: The file itself
306 * Unlink a file. Call gfs2_unlinki()
311 static int gfs2_unlink(struct inode
*dir
, struct dentry
*dentry
)
313 struct gfs2_inode
*dip
= GFS2_I(dir
);
314 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
315 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
316 struct gfs2_holder ghs
[3];
317 struct gfs2_rgrpd
*rgd
;
318 struct gfs2_holder ri_gh
;
321 error
= gfs2_rindex_hold(sdp
, &ri_gh
);
325 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
326 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
328 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_no_addr
);
329 gfs2_holder_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 2);
332 error
= gfs2_glock_nq(ghs
); /* parent */
336 error
= gfs2_glock_nq(ghs
+ 1); /* child */
340 error
= gfs2_glock_nq(ghs
+ 2); /* rgrp */
344 error
= gfs2_unlink_ok(dip
, &dentry
->d_name
, ip
);
348 error
= gfs2_trans_begin(sdp
, 2*RES_DINODE
+ RES_LEAF
+ RES_RG_BIT
, 0);
352 error
= gfs2_dir_del(dip
, &dentry
->d_name
);
356 error
= gfs2_change_nlink(ip
, -1);
361 gfs2_glock_dq(ghs
+ 2);
363 gfs2_holder_uninit(ghs
+ 2);
364 gfs2_glock_dq(ghs
+ 1);
366 gfs2_holder_uninit(ghs
+ 1);
369 gfs2_holder_uninit(ghs
);
370 gfs2_glock_dq_uninit(&ri_gh
);
375 * gfs2_symlink - Create a symlink
376 * @dir: The directory to create the symlink in
377 * @dentry: The dentry to put the symlink in
378 * @symname: The thing which the link points to
383 static int gfs2_symlink(struct inode
*dir
, struct dentry
*dentry
,
386 struct gfs2_inode
*dip
= GFS2_I(dir
), *ip
;
387 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
388 struct gfs2_holder ghs
[2];
390 struct buffer_head
*dibh
;
394 /* Must be stuffed with a null terminator for gfs2_follow_link() */
395 size
= strlen(symname
);
396 if (size
> sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
) - 1)
397 return -ENAMETOOLONG
;
399 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
401 inode
= gfs2_createi(ghs
, &dentry
->d_name
, S_IFLNK
| S_IRWXUGO
, 0);
403 gfs2_holder_uninit(ghs
);
404 return PTR_ERR(inode
);
407 ip
= ghs
[1].gh_gl
->gl_object
;
409 i_size_write(inode
, size
);
411 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
413 if (!gfs2_assert_withdraw(sdp
, !error
)) {
414 gfs2_dinode_out(ip
, dibh
->b_data
);
415 memcpy(dibh
->b_data
+ sizeof(struct gfs2_dinode
), symname
,
421 if (dip
->i_alloc
->al_rgd
)
422 gfs2_inplace_release(dip
);
423 gfs2_quota_unlock(dip
);
426 gfs2_glock_dq_uninit_m(2, ghs
);
428 d_instantiate(dentry
, inode
);
429 mark_inode_dirty(inode
);
435 * gfs2_mkdir - Make a directory
436 * @dir: The parent directory of the new one
437 * @dentry: The dentry of the new directory
438 * @mode: The mode of the new directory
443 static int gfs2_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
445 struct gfs2_inode
*dip
= GFS2_I(dir
), *ip
;
446 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
447 struct gfs2_holder ghs
[2];
449 struct buffer_head
*dibh
;
452 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
454 inode
= gfs2_createi(ghs
, &dentry
->d_name
, S_IFDIR
| mode
, 0);
456 gfs2_holder_uninit(ghs
);
457 return PTR_ERR(inode
);
460 ip
= ghs
[1].gh_gl
->gl_object
;
462 ip
->i_inode
.i_nlink
= 2;
463 i_size_write(inode
, sdp
->sd_sb
.sb_bsize
- sizeof(struct gfs2_dinode
));
464 ip
->i_diskflags
|= GFS2_DIF_JDATA
;
467 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
469 if (!gfs2_assert_withdraw(sdp
, !error
)) {
470 struct gfs2_dinode
*di
= (struct gfs2_dinode
*)dibh
->b_data
;
471 struct gfs2_dirent
*dent
= (struct gfs2_dirent
*)(di
+1);
473 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
474 gfs2_qstr2dirent(&gfs2_qdot
, GFS2_DIRENT_SIZE(gfs2_qdot
.len
), dent
);
475 dent
->de_inum
= di
->di_num
; /* already GFS2 endian */
476 dent
->de_type
= cpu_to_be16(DT_DIR
);
477 di
->di_entries
= cpu_to_be32(1);
479 dent
= (struct gfs2_dirent
*)((char*)dent
+ GFS2_DIRENT_SIZE(1));
480 gfs2_qstr2dirent(&gfs2_qdotdot
, dibh
->b_size
- GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode
), dent
);
482 gfs2_inum_out(dip
, dent
);
483 dent
->de_type
= cpu_to_be16(DT_DIR
);
485 gfs2_dinode_out(ip
, di
);
490 error
= gfs2_change_nlink(dip
, +1);
491 gfs2_assert_withdraw(sdp
, !error
); /* dip already pinned */
494 if (dip
->i_alloc
->al_rgd
)
495 gfs2_inplace_release(dip
);
496 gfs2_quota_unlock(dip
);
499 gfs2_glock_dq_uninit_m(2, ghs
);
501 d_instantiate(dentry
, inode
);
502 mark_inode_dirty(inode
);
508 * gfs2_rmdiri - Remove a directory
509 * @dip: The parent directory of the directory to be removed
510 * @name: The name of the directory to be removed
511 * @ip: The GFS2 inode of the directory to be removed
513 * Assumes Glocks on dip and ip are held
518 static int gfs2_rmdiri(struct gfs2_inode
*dip
, const struct qstr
*name
,
519 struct gfs2_inode
*ip
)
523 if (ip
->i_entries
!= 2) {
524 if (gfs2_consist_inode(ip
))
525 gfs2_dinode_print(ip
);
529 error
= gfs2_dir_del(dip
, name
);
533 error
= gfs2_change_nlink(dip
, -1);
537 error
= gfs2_dir_del(ip
, &gfs2_qdot
);
541 error
= gfs2_dir_del(ip
, &gfs2_qdotdot
);
545 /* It looks odd, but it really should be done twice */
546 error
= gfs2_change_nlink(ip
, -1);
550 error
= gfs2_change_nlink(ip
, -1);
558 * gfs2_rmdir - Remove a directory
559 * @dir: The parent directory of the directory to be removed
560 * @dentry: The dentry of the directory to remove
562 * Remove a directory. Call gfs2_rmdiri()
567 static int gfs2_rmdir(struct inode
*dir
, struct dentry
*dentry
)
569 struct gfs2_inode
*dip
= GFS2_I(dir
);
570 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
571 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
572 struct gfs2_holder ghs
[3];
573 struct gfs2_rgrpd
*rgd
;
574 struct gfs2_holder ri_gh
;
577 error
= gfs2_rindex_hold(sdp
, &ri_gh
);
580 gfs2_holder_init(dip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
581 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 1);
583 rgd
= gfs2_blk2rgrpd(sdp
, ip
->i_no_addr
);
584 gfs2_holder_init(rgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ 2);
586 error
= gfs2_glock_nq(ghs
); /* parent */
590 error
= gfs2_glock_nq(ghs
+ 1); /* child */
594 error
= gfs2_glock_nq(ghs
+ 2); /* rgrp */
598 error
= gfs2_unlink_ok(dip
, &dentry
->d_name
, ip
);
602 if (ip
->i_entries
< 2) {
603 if (gfs2_consist_inode(ip
))
604 gfs2_dinode_print(ip
);
608 if (ip
->i_entries
> 2) {
613 error
= gfs2_trans_begin(sdp
, 2 * RES_DINODE
+ 3 * RES_LEAF
+ RES_RG_BIT
, 0);
617 error
= gfs2_rmdiri(dip
, &dentry
->d_name
, ip
);
622 gfs2_glock_dq(ghs
+ 2);
624 gfs2_holder_uninit(ghs
+ 2);
625 gfs2_glock_dq(ghs
+ 1);
627 gfs2_holder_uninit(ghs
+ 1);
630 gfs2_holder_uninit(ghs
);
631 gfs2_glock_dq_uninit(&ri_gh
);
636 * gfs2_mknod - Make a special file
637 * @dir: The directory in which the special file will reside
638 * @dentry: The dentry of the special file
639 * @mode: The mode of the special file
640 * @rdev: The device specification of the special file
644 static int gfs2_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
,
647 struct gfs2_inode
*dip
= GFS2_I(dir
);
648 struct gfs2_sbd
*sdp
= GFS2_SB(dir
);
649 struct gfs2_holder ghs
[2];
652 gfs2_holder_init(dip
->i_gl
, 0, 0, ghs
);
654 inode
= gfs2_createi(ghs
, &dentry
->d_name
, mode
, dev
);
656 gfs2_holder_uninit(ghs
);
657 return PTR_ERR(inode
);
661 if (dip
->i_alloc
->al_rgd
)
662 gfs2_inplace_release(dip
);
663 gfs2_quota_unlock(dip
);
666 gfs2_glock_dq_uninit_m(2, ghs
);
668 d_instantiate(dentry
, inode
);
669 mark_inode_dirty(inode
);
675 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
679 * Follow @to back to the root and make sure we don't encounter @this
680 * Assumes we already hold the rename lock.
685 static int gfs2_ok_to_move(struct gfs2_inode
*this, struct gfs2_inode
*to
)
687 struct inode
*dir
= &to
->i_inode
;
688 struct super_block
*sb
= dir
->i_sb
;
695 if (dir
== &this->i_inode
) {
699 if (dir
== sb
->s_root
->d_inode
) {
704 tmp
= gfs2_lookupi(dir
, &gfs2_qdotdot
, 1);
706 error
= PTR_ERR(tmp
);
720 * gfs2_rename - Rename a file
721 * @odir: Parent directory of old file name
722 * @odentry: The old dentry of the file
723 * @ndir: Parent directory of new file name
724 * @ndentry: The new dentry of the file
729 static int gfs2_rename(struct inode
*odir
, struct dentry
*odentry
,
730 struct inode
*ndir
, struct dentry
*ndentry
)
732 struct gfs2_inode
*odip
= GFS2_I(odir
);
733 struct gfs2_inode
*ndip
= GFS2_I(ndir
);
734 struct gfs2_inode
*ip
= GFS2_I(odentry
->d_inode
);
735 struct gfs2_inode
*nip
= NULL
;
736 struct gfs2_sbd
*sdp
= GFS2_SB(odir
);
737 struct gfs2_holder ghs
[5], r_gh
= { .gh_gl
= NULL
, }, ri_gh
;
738 struct gfs2_rgrpd
*nrgd
;
741 int alloc_required
= 0;
745 if (ndentry
->d_inode
) {
746 nip
= GFS2_I(ndentry
->d_inode
);
751 error
= gfs2_rindex_hold(sdp
, &ri_gh
);
756 error
= gfs2_glock_nq_init(sdp
->sd_rename_gl
, LM_ST_EXCLUSIVE
,
761 if (S_ISDIR(ip
->i_inode
.i_mode
)) {
763 /* don't move a dirctory into it's subdir */
764 error
= gfs2_ok_to_move(ip
, ndip
);
771 gfs2_holder_init(odip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
);
773 gfs2_holder_init(ndip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
776 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
780 gfs2_holder_init(nip
->i_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
);
782 /* grab the resource lock for unlink flag twiddling
783 * this is the case of the target file already existing
784 * so we unlink before doing the rename
786 nrgd
= gfs2_blk2rgrpd(sdp
, nip
->i_no_addr
);
788 gfs2_holder_init(nrgd
->rd_gl
, LM_ST_EXCLUSIVE
, 0, ghs
+ num_gh
++);
791 for (x
= 0; x
< num_gh
; x
++) {
792 error
= gfs2_glock_nq(ghs
+ x
);
797 /* Check out the old directory */
799 error
= gfs2_unlink_ok(odip
, &odentry
->d_name
, ip
);
803 /* Check out the new directory */
806 error
= gfs2_unlink_ok(ndip
, &ndentry
->d_name
, nip
);
810 if (S_ISDIR(nip
->i_inode
.i_mode
)) {
811 if (nip
->i_entries
< 2) {
812 if (gfs2_consist_inode(nip
))
813 gfs2_dinode_print(nip
);
817 if (nip
->i_entries
> 2) {
823 error
= gfs2_permission(ndir
, MAY_WRITE
| MAY_EXEC
, 0);
827 error
= gfs2_dir_check(ndir
, &ndentry
->d_name
, NULL
);
839 if (!ndip
->i_inode
.i_nlink
) {
843 if (ndip
->i_entries
== (u32
)-1) {
847 if (S_ISDIR(ip
->i_inode
.i_mode
) &&
848 ndip
->i_inode
.i_nlink
== (u32
)-1) {
855 /* Check out the dir to be renamed */
858 error
= gfs2_permission(odentry
->d_inode
, MAY_WRITE
, 0);
864 alloc_required
= gfs2_diradd_alloc_required(ndir
, &ndentry
->d_name
);
865 error
= alloc_required
;
870 if (alloc_required
) {
871 struct gfs2_alloc
*al
= gfs2_alloc_get(ndip
);
877 error
= gfs2_quota_lock_check(ndip
);
881 al
->al_requested
= sdp
->sd_max_dirres
;
883 error
= gfs2_inplace_reserve_ri(ndip
);
887 error
= gfs2_trans_begin(sdp
, sdp
->sd_max_dirres
+
889 4 * RES_DINODE
+ 4 * RES_LEAF
+
890 RES_STATFS
+ RES_QUOTA
+ 4, 0);
894 error
= gfs2_trans_begin(sdp
, 4 * RES_DINODE
+
895 5 * RES_LEAF
+ 4, 0);
900 /* Remove the target file, if it exists */
903 if (S_ISDIR(nip
->i_inode
.i_mode
))
904 error
= gfs2_rmdiri(ndip
, &ndentry
->d_name
, nip
);
906 error
= gfs2_dir_del(ndip
, &ndentry
->d_name
);
909 error
= gfs2_change_nlink(nip
, -1);
916 error
= gfs2_change_nlink(ndip
, +1);
919 error
= gfs2_change_nlink(odip
, -1);
923 error
= gfs2_dir_mvino(ip
, &gfs2_qdotdot
, ndip
, DT_DIR
);
927 struct buffer_head
*dibh
;
928 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
931 ip
->i_inode
.i_ctime
= CURRENT_TIME
;
932 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
933 gfs2_dinode_out(ip
, dibh
->b_data
);
937 error
= gfs2_dir_del(odip
, &odentry
->d_name
);
941 error
= gfs2_dir_add(ndir
, &ndentry
->d_name
, ip
, IF2DT(ip
->i_inode
.i_mode
));
949 gfs2_inplace_release(ndip
);
952 gfs2_quota_unlock(ndip
);
955 gfs2_alloc_put(ndip
);
958 gfs2_glock_dq(ghs
+ x
);
959 gfs2_holder_uninit(ghs
+ x
);
963 gfs2_glock_dq_uninit(&r_gh
);
965 gfs2_glock_dq_uninit(&ri_gh
);
970 * gfs2_follow_link - Follow a symbolic link
971 * @dentry: The dentry of the link
972 * @nd: Data that we pass to vfs_follow_link()
974 * This can handle symlinks of any size.
976 * Returns: 0 on success or error code
979 static void *gfs2_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
981 struct gfs2_inode
*ip
= GFS2_I(dentry
->d_inode
);
982 struct gfs2_holder i_gh
;
983 struct buffer_head
*dibh
;
984 unsigned int x
, size
;
988 gfs2_holder_init(ip
->i_gl
, LM_ST_SHARED
, 0, &i_gh
);
989 error
= gfs2_glock_nq(&i_gh
);
991 gfs2_holder_uninit(&i_gh
);
992 nd_set_link(nd
, ERR_PTR(error
));
996 size
= (unsigned int)i_size_read(&ip
->i_inode
);
998 gfs2_consist_inode(ip
);
1003 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1005 buf
= ERR_PTR(error
);
1010 buf
= kmalloc(x
, GFP_NOFS
);
1012 buf
= ERR_PTR(-ENOMEM
);
1014 memcpy(buf
, dibh
->b_data
+ sizeof(struct gfs2_dinode
), x
);
1017 gfs2_glock_dq_uninit(&i_gh
);
1018 nd_set_link(nd
, buf
);
1022 static void gfs2_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *p
)
1024 char *s
= nd_get_link(nd
);
1033 * @nd: passed from Linux VFS, ignored by us
1035 * This may be called from the VFS directly, or from within GFS2 with the
1036 * inode locked, so we look to see if the glock is already locked and only
1037 * lock the glock if its not already been done.
1042 int gfs2_permission(struct inode
*inode
, int mask
, unsigned int flags
)
1044 struct gfs2_inode
*ip
;
1045 struct gfs2_holder i_gh
;
1049 if (flags
& IPERM_FLAG_RCU
)
1053 if (gfs2_glock_is_locked_by_me(ip
->i_gl
) == NULL
) {
1054 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &i_gh
);
1060 if ((mask
& MAY_WRITE
) && IS_IMMUTABLE(inode
))
1063 error
= generic_permission(inode
, mask
, flags
, gfs2_check_acl
);
1065 gfs2_glock_dq_uninit(&i_gh
);
1070 static int setattr_chown(struct inode
*inode
, struct iattr
*attr
)
1072 struct gfs2_inode
*ip
= GFS2_I(inode
);
1073 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1074 u32 ouid
, ogid
, nuid
, ngid
;
1077 ouid
= inode
->i_uid
;
1078 ogid
= inode
->i_gid
;
1079 nuid
= attr
->ia_uid
;
1080 ngid
= attr
->ia_gid
;
1082 if (!(attr
->ia_valid
& ATTR_UID
) || ouid
== nuid
)
1083 ouid
= nuid
= NO_QUOTA_CHANGE
;
1084 if (!(attr
->ia_valid
& ATTR_GID
) || ogid
== ngid
)
1085 ogid
= ngid
= NO_QUOTA_CHANGE
;
1087 if (!gfs2_alloc_get(ip
))
1090 error
= gfs2_quota_lock(ip
, nuid
, ngid
);
1094 if (ouid
!= NO_QUOTA_CHANGE
|| ogid
!= NO_QUOTA_CHANGE
) {
1095 error
= gfs2_quota_check(ip
, nuid
, ngid
);
1100 error
= gfs2_trans_begin(sdp
, RES_DINODE
+ 2 * RES_QUOTA
, 0);
1104 error
= gfs2_setattr_simple(ip
, attr
);
1108 if (ouid
!= NO_QUOTA_CHANGE
|| ogid
!= NO_QUOTA_CHANGE
) {
1109 u64 blocks
= gfs2_get_inode_blocks(&ip
->i_inode
);
1110 gfs2_quota_change(ip
, -blocks
, ouid
, ogid
);
1111 gfs2_quota_change(ip
, blocks
, nuid
, ngid
);
1115 gfs2_trans_end(sdp
);
1117 gfs2_quota_unlock(ip
);
1124 * gfs2_setattr - Change attributes on an inode
1125 * @dentry: The dentry which is changing
1126 * @attr: The structure describing the change
1128 * The VFS layer wants to change one or more of an inodes attributes. Write
1129 * that change out to disk.
1134 static int gfs2_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1136 struct inode
*inode
= dentry
->d_inode
;
1137 struct gfs2_inode
*ip
= GFS2_I(inode
);
1138 struct gfs2_holder i_gh
;
1141 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &i_gh
);
1146 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
1149 error
= inode_change_ok(inode
, attr
);
1153 if (attr
->ia_valid
& ATTR_SIZE
)
1154 error
= gfs2_setattr_size(inode
, attr
->ia_size
);
1155 else if (attr
->ia_valid
& (ATTR_UID
| ATTR_GID
))
1156 error
= setattr_chown(inode
, attr
);
1157 else if ((attr
->ia_valid
& ATTR_MODE
) && IS_POSIXACL(inode
))
1158 error
= gfs2_acl_chmod(ip
, attr
);
1160 error
= gfs2_setattr_simple(ip
, attr
);
1163 gfs2_glock_dq_uninit(&i_gh
);
1165 mark_inode_dirty(inode
);
1170 * gfs2_getattr - Read out an inode's attributes
1171 * @mnt: The vfsmount the inode is being accessed from
1172 * @dentry: The dentry to stat
1173 * @stat: The inode's stats
1175 * This may be called from the VFS directly, or from within GFS2 with the
1176 * inode locked, so we look to see if the glock is already locked and only
1177 * lock the glock if its not already been done. Note that its the NFS
1178 * readdirplus operation which causes this to be called (from filldir)
1179 * with the glock already held.
1184 static int gfs2_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
,
1187 struct inode
*inode
= dentry
->d_inode
;
1188 struct gfs2_inode
*ip
= GFS2_I(inode
);
1189 struct gfs2_holder gh
;
1193 if (gfs2_glock_is_locked_by_me(ip
->i_gl
) == NULL
) {
1194 error
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &gh
);
1200 generic_fillattr(inode
, stat
);
1202 gfs2_glock_dq_uninit(&gh
);
1207 static int gfs2_setxattr(struct dentry
*dentry
, const char *name
,
1208 const void *data
, size_t size
, int flags
)
1210 struct inode
*inode
= dentry
->d_inode
;
1211 struct gfs2_inode
*ip
= GFS2_I(inode
);
1212 struct gfs2_holder gh
;
1215 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &gh
);
1216 ret
= gfs2_glock_nq(&gh
);
1218 ret
= generic_setxattr(dentry
, name
, data
, size
, flags
);
1221 gfs2_holder_uninit(&gh
);
1225 static ssize_t
gfs2_getxattr(struct dentry
*dentry
, const char *name
,
1226 void *data
, size_t size
)
1228 struct inode
*inode
= dentry
->d_inode
;
1229 struct gfs2_inode
*ip
= GFS2_I(inode
);
1230 struct gfs2_holder gh
;
1233 gfs2_holder_init(ip
->i_gl
, LM_ST_SHARED
, LM_FLAG_ANY
, &gh
);
1234 ret
= gfs2_glock_nq(&gh
);
1236 ret
= generic_getxattr(dentry
, name
, data
, size
);
1239 gfs2_holder_uninit(&gh
);
1243 static int gfs2_removexattr(struct dentry
*dentry
, const char *name
)
1245 struct inode
*inode
= dentry
->d_inode
;
1246 struct gfs2_inode
*ip
= GFS2_I(inode
);
1247 struct gfs2_holder gh
;
1250 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &gh
);
1251 ret
= gfs2_glock_nq(&gh
);
1253 ret
= generic_removexattr(dentry
, name
);
1256 gfs2_holder_uninit(&gh
);
1260 static void empty_write_end(struct page
*page
, unsigned from
,
1263 struct gfs2_inode
*ip
= GFS2_I(page
->mapping
->host
);
1265 page_zero_new_buffers(page
, from
, to
);
1266 flush_dcache_page(page
);
1267 mark_page_accessed(page
);
1269 if (!gfs2_is_writeback(ip
))
1270 gfs2_page_add_databufs(ip
, page
, from
, to
);
1272 block_commit_write(page
, from
, to
);
1276 static int write_empty_blocks(struct page
*page
, unsigned from
, unsigned to
)
1278 unsigned start
, end
, next
;
1279 struct buffer_head
*bh
, *head
;
1282 if (!page_has_buffers(page
)) {
1283 error
= __block_write_begin(page
, from
, to
- from
, gfs2_block_map
);
1284 if (unlikely(error
))
1287 empty_write_end(page
, from
, to
);
1291 bh
= head
= page_buffers(page
);
1293 while (next
< from
) {
1295 bh
= bh
->b_this_page
;
1300 if (buffer_mapped(bh
)) {
1302 error
= __block_write_begin(page
, start
, end
- start
,
1304 if (unlikely(error
))
1306 empty_write_end(page
, start
, end
);
1313 bh
= bh
->b_this_page
;
1314 } while (next
< to
);
1317 error
= __block_write_begin(page
, start
, end
- start
, gfs2_block_map
);
1318 if (unlikely(error
))
1320 empty_write_end(page
, start
, end
);
1326 static int fallocate_chunk(struct inode
*inode
, loff_t offset
, loff_t len
,
1329 struct gfs2_inode
*ip
= GFS2_I(inode
);
1330 struct buffer_head
*dibh
;
1332 u64 start
= offset
>> PAGE_CACHE_SHIFT
;
1333 unsigned int start_offset
= offset
& ~PAGE_CACHE_MASK
;
1334 u64 end
= (offset
+ len
- 1) >> PAGE_CACHE_SHIFT
;
1337 unsigned int end_offset
= (offset
+ len
) & ~PAGE_CACHE_MASK
;
1338 unsigned int from
, to
;
1341 end_offset
= PAGE_CACHE_SIZE
;
1343 error
= gfs2_meta_inode_buffer(ip
, &dibh
);
1344 if (unlikely(error
))
1347 gfs2_trans_add_bh(ip
->i_gl
, dibh
, 1);
1349 if (gfs2_is_stuffed(ip
)) {
1350 error
= gfs2_unstuff_dinode(ip
, NULL
);
1351 if (unlikely(error
))
1356 offset
= start
<< PAGE_CACHE_SHIFT
;
1357 from
= start_offset
;
1358 to
= PAGE_CACHE_SIZE
;
1359 while (curr
<= end
) {
1360 page
= grab_cache_page_write_begin(inode
->i_mapping
, curr
,
1362 if (unlikely(!page
)) {
1369 error
= write_empty_blocks(page
, from
, to
);
1370 if (!error
&& offset
+ to
> inode
->i_size
&&
1371 !(mode
& FALLOC_FL_KEEP_SIZE
)) {
1372 i_size_write(inode
, offset
+ to
);
1375 page_cache_release(page
);
1379 offset
+= PAGE_CACHE_SIZE
;
1383 gfs2_dinode_out(ip
, dibh
->b_data
);
1384 mark_inode_dirty(inode
);
1392 static void calc_max_reserv(struct gfs2_inode
*ip
, loff_t max
, loff_t
*len
,
1393 unsigned int *data_blocks
, unsigned int *ind_blocks
)
1395 const struct gfs2_sbd
*sdp
= GFS2_SB(&ip
->i_inode
);
1396 unsigned int max_blocks
= ip
->i_alloc
->al_rgd
->rd_free_clone
;
1397 unsigned int tmp
, max_data
= max_blocks
- 3 * (sdp
->sd_max_height
- 1);
1399 for (tmp
= max_data
; tmp
> sdp
->sd_diptrs
;) {
1400 tmp
= DIV_ROUND_UP(tmp
, sdp
->sd_inptrs
);
1403 /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
1404 so it might end up with fewer data blocks */
1405 if (max_data
<= *data_blocks
)
1407 *data_blocks
= max_data
;
1408 *ind_blocks
= max_blocks
- max_data
;
1409 *len
= ((loff_t
)max_data
- 3) << sdp
->sd_sb
.sb_bsize_shift
;
1412 gfs2_write_calc_reserv(ip
, max
, data_blocks
, ind_blocks
);
1416 static long gfs2_fallocate(struct inode
*inode
, int mode
, loff_t offset
,
1419 struct gfs2_sbd
*sdp
= GFS2_SB(inode
);
1420 struct gfs2_inode
*ip
= GFS2_I(inode
);
1421 unsigned int data_blocks
= 0, ind_blocks
= 0, rblocks
;
1422 loff_t bytes
, max_bytes
;
1423 struct gfs2_alloc
*al
;
1425 loff_t next
= (offset
+ len
- 1) >> sdp
->sd_sb
.sb_bsize_shift
;
1426 next
= (next
+ 1) << sdp
->sd_sb
.sb_bsize_shift
;
1428 /* We only support the FALLOC_FL_KEEP_SIZE mode */
1429 if (mode
& ~FALLOC_FL_KEEP_SIZE
)
1432 offset
= (offset
>> sdp
->sd_sb
.sb_bsize_shift
) <<
1433 sdp
->sd_sb
.sb_bsize_shift
;
1435 len
= next
- offset
;
1436 bytes
= sdp
->sd_max_rg_data
* sdp
->sd_sb
.sb_bsize
/ 2;
1440 gfs2_holder_init(ip
->i_gl
, LM_ST_EXCLUSIVE
, 0, &ip
->i_gh
);
1441 error
= gfs2_glock_nq(&ip
->i_gh
);
1442 if (unlikely(error
))
1445 if (!gfs2_write_alloc_required(ip
, offset
, len
))
1451 al
= gfs2_alloc_get(ip
);
1457 error
= gfs2_quota_lock_check(ip
);
1462 gfs2_write_calc_reserv(ip
, bytes
, &data_blocks
, &ind_blocks
);
1464 al
->al_requested
= data_blocks
+ ind_blocks
;
1465 error
= gfs2_inplace_reserve(ip
);
1467 if (error
== -ENOSPC
&& bytes
> sdp
->sd_sb
.sb_bsize
) {
1474 calc_max_reserv(ip
, len
, &max_bytes
, &data_blocks
, &ind_blocks
);
1475 al
->al_requested
= data_blocks
+ ind_blocks
;
1477 rblocks
= RES_DINODE
+ ind_blocks
+ RES_STATFS
+ RES_QUOTA
+
1478 RES_RG_HDR
+ gfs2_rg_blocks(al
);
1479 if (gfs2_is_jdata(ip
))
1480 rblocks
+= data_blocks
? data_blocks
: 1;
1482 error
= gfs2_trans_begin(sdp
, rblocks
,
1483 PAGE_CACHE_SIZE
/sdp
->sd_sb
.sb_bsize
);
1485 goto out_trans_fail
;
1487 error
= fallocate_chunk(inode
, offset
, max_bytes
, mode
);
1488 gfs2_trans_end(sdp
);
1491 goto out_trans_fail
;
1494 offset
+= max_bytes
;
1495 gfs2_inplace_release(ip
);
1496 gfs2_quota_unlock(ip
);
1502 gfs2_inplace_release(ip
);
1504 gfs2_quota_unlock(ip
);
1508 gfs2_glock_dq(&ip
->i_gh
);
1510 gfs2_holder_uninit(&ip
->i_gh
);
1515 static int gfs2_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
1518 struct gfs2_inode
*ip
= GFS2_I(inode
);
1519 struct gfs2_holder gh
;
1522 ret
= fiemap_check_flags(fieinfo
, FIEMAP_FLAG_SYNC
);
1526 mutex_lock(&inode
->i_mutex
);
1528 ret
= gfs2_glock_nq_init(ip
->i_gl
, LM_ST_SHARED
, 0, &gh
);
1532 if (gfs2_is_stuffed(ip
)) {
1533 u64 phys
= ip
->i_no_addr
<< inode
->i_blkbits
;
1534 u64 size
= i_size_read(inode
);
1535 u32 flags
= FIEMAP_EXTENT_LAST
|FIEMAP_EXTENT_NOT_ALIGNED
|
1536 FIEMAP_EXTENT_DATA_INLINE
;
1537 phys
+= sizeof(struct gfs2_dinode
);
1539 if (start
+ len
> size
)
1542 ret
= fiemap_fill_next_extent(fieinfo
, start
, phys
,
1547 ret
= __generic_block_fiemap(inode
, fieinfo
, start
, len
,
1551 gfs2_glock_dq_uninit(&gh
);
1553 mutex_unlock(&inode
->i_mutex
);
1557 const struct inode_operations gfs2_file_iops
= {
1558 .permission
= gfs2_permission
,
1559 .setattr
= gfs2_setattr
,
1560 .getattr
= gfs2_getattr
,
1561 .setxattr
= gfs2_setxattr
,
1562 .getxattr
= gfs2_getxattr
,
1563 .listxattr
= gfs2_listxattr
,
1564 .removexattr
= gfs2_removexattr
,
1565 .fallocate
= gfs2_fallocate
,
1566 .fiemap
= gfs2_fiemap
,
1569 const struct inode_operations gfs2_dir_iops
= {
1570 .create
= gfs2_create
,
1571 .lookup
= gfs2_lookup
,
1573 .unlink
= gfs2_unlink
,
1574 .symlink
= gfs2_symlink
,
1575 .mkdir
= gfs2_mkdir
,
1576 .rmdir
= gfs2_rmdir
,
1577 .mknod
= gfs2_mknod
,
1578 .rename
= gfs2_rename
,
1579 .permission
= gfs2_permission
,
1580 .setattr
= gfs2_setattr
,
1581 .getattr
= gfs2_getattr
,
1582 .setxattr
= gfs2_setxattr
,
1583 .getxattr
= gfs2_getxattr
,
1584 .listxattr
= gfs2_listxattr
,
1585 .removexattr
= gfs2_removexattr
,
1586 .fiemap
= gfs2_fiemap
,
1589 const struct inode_operations gfs2_symlink_iops
= {
1590 .readlink
= generic_readlink
,
1591 .follow_link
= gfs2_follow_link
,
1592 .put_link
= gfs2_put_link
,
1593 .permission
= gfs2_permission
,
1594 .setattr
= gfs2_setattr
,
1595 .getattr
= gfs2_getattr
,
1596 .setxattr
= gfs2_setxattr
,
1597 .getxattr
= gfs2_getxattr
,
1598 .listxattr
= gfs2_listxattr
,
1599 .removexattr
= gfs2_removexattr
,
1600 .fiemap
= gfs2_fiemap
,