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 d_set_d_op(dentry, &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, 0);
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 +
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);
259 d_instantiate(dentry, inode);
260 mark_inode_dirty(inode);
266 * gfs2_unlink_ok - check to see that a inode is still in a directory
267 * @dip: the directory
268 * @name: the name of the file
271 * Assumes that the lock on (at least) @dip is held.
273 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
276 static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
277 const struct gfs2_inode *ip)
281 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
284 if ((dip->i_inode.i_mode & S_ISVTX) &&
285 dip->i_inode.i_uid != current_fsuid() &&
286 ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
289 if (IS_APPEND(&dip->i_inode))
292 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
296 error = gfs2_dir_check(&dip->i_inode, name, ip);
304 * gfs2_unlink - Unlink a file
305 * @dir: The inode of the directory containing the file to unlink
306 * @dentry: The file itself
308 * Unlink a file. Call gfs2_unlinki()
313 static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
315 struct gfs2_inode *dip = GFS2_I(dir);
316 struct gfs2_sbd *sdp = GFS2_SB(dir);
317 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
318 struct gfs2_holder ghs[3];
319 struct gfs2_rgrpd *rgd;
320 struct gfs2_holder ri_gh;
323 error = gfs2_rindex_hold(sdp, &ri_gh);
327 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
328 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
330 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
331 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
334 error = gfs2_glock_nq(ghs); /* parent */
338 error = gfs2_glock_nq(ghs + 1); /* child */
342 error = gfs2_glock_nq(ghs + 2); /* rgrp */
346 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
350 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
354 error = gfs2_dir_del(dip, &dentry->d_name);
358 error = gfs2_change_nlink(ip, -1);
363 gfs2_glock_dq(ghs + 2);
365 gfs2_holder_uninit(ghs + 2);
366 gfs2_glock_dq(ghs + 1);
368 gfs2_holder_uninit(ghs + 1);
371 gfs2_holder_uninit(ghs);
372 gfs2_glock_dq_uninit(&ri_gh);
377 * gfs2_symlink - Create a symlink
378 * @dir: The directory to create the symlink in
379 * @dentry: The dentry to put the symlink in
380 * @symname: The thing which the link points to
385 static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
388 struct gfs2_inode *dip = GFS2_I(dir), *ip;
389 struct gfs2_sbd *sdp = GFS2_SB(dir);
390 struct gfs2_holder ghs[2];
392 struct buffer_head *dibh;
396 /* Must be stuffed with a null terminator for gfs2_follow_link() */
397 size = strlen(symname);
398 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
399 return -ENAMETOOLONG;
401 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
403 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
405 gfs2_holder_uninit(ghs);
406 return PTR_ERR(inode);
409 ip = ghs[1].gh_gl->gl_object;
411 i_size_write(inode, size);
413 error = gfs2_meta_inode_buffer(ip, &dibh);
415 if (!gfs2_assert_withdraw(sdp, !error)) {
416 gfs2_dinode_out(ip, dibh->b_data);
417 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
423 if (dip->i_alloc->al_rgd)
424 gfs2_inplace_release(dip);
425 gfs2_quota_unlock(dip);
428 gfs2_glock_dq_uninit_m(2, ghs);
430 d_instantiate(dentry, inode);
431 mark_inode_dirty(inode);
437 * gfs2_mkdir - Make a directory
438 * @dir: The parent directory of the new one
439 * @dentry: The dentry of the new directory
440 * @mode: The mode of the new directory
445 static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
447 struct gfs2_inode *dip = GFS2_I(dir), *ip;
448 struct gfs2_sbd *sdp = GFS2_SB(dir);
449 struct gfs2_holder ghs[2];
451 struct buffer_head *dibh;
454 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
456 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
458 gfs2_holder_uninit(ghs);
459 return PTR_ERR(inode);
462 ip = ghs[1].gh_gl->gl_object;
464 ip->i_inode.i_nlink = 2;
465 i_size_write(inode, sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
466 ip->i_diskflags |= GFS2_DIF_JDATA;
469 error = gfs2_meta_inode_buffer(ip, &dibh);
471 if (!gfs2_assert_withdraw(sdp, !error)) {
472 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
473 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
475 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
476 gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
477 dent->de_inum = di->di_num; /* already GFS2 endian */
478 dent->de_type = cpu_to_be16(DT_DIR);
479 di->di_entries = cpu_to_be32(1);
481 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
482 gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
484 gfs2_inum_out(dip, dent);
485 dent->de_type = cpu_to_be16(DT_DIR);
487 gfs2_dinode_out(ip, di);
492 error = gfs2_change_nlink(dip, +1);
493 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
496 if (dip->i_alloc->al_rgd)
497 gfs2_inplace_release(dip);
498 gfs2_quota_unlock(dip);
501 gfs2_glock_dq_uninit_m(2, ghs);
503 d_instantiate(dentry, inode);
504 mark_inode_dirty(inode);
510 * gfs2_rmdiri - Remove a directory
511 * @dip: The parent directory of the directory to be removed
512 * @name: The name of the directory to be removed
513 * @ip: The GFS2 inode of the directory to be removed
515 * Assumes Glocks on dip and ip are held
520 static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
521 struct gfs2_inode *ip)
525 if (ip->i_entries != 2) {
526 if (gfs2_consist_inode(ip))
527 gfs2_dinode_print(ip);
531 error = gfs2_dir_del(dip, name);
535 error = gfs2_change_nlink(dip, -1);
539 error = gfs2_dir_del(ip, &gfs2_qdot);
543 error = gfs2_dir_del(ip, &gfs2_qdotdot);
547 /* It looks odd, but it really should be done twice */
548 error = gfs2_change_nlink(ip, -1);
552 error = gfs2_change_nlink(ip, -1);
560 * gfs2_rmdir - Remove a directory
561 * @dir: The parent directory of the directory to be removed
562 * @dentry: The dentry of the directory to remove
564 * Remove a directory. Call gfs2_rmdiri()
569 static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
571 struct gfs2_inode *dip = GFS2_I(dir);
572 struct gfs2_sbd *sdp = GFS2_SB(dir);
573 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
574 struct gfs2_holder ghs[3];
575 struct gfs2_rgrpd *rgd;
576 struct gfs2_holder ri_gh;
579 error = gfs2_rindex_hold(sdp, &ri_gh);
582 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
583 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
585 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
586 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
588 error = gfs2_glock_nq(ghs); /* parent */
592 error = gfs2_glock_nq(ghs + 1); /* child */
596 error = gfs2_glock_nq(ghs + 2); /* rgrp */
600 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
604 if (ip->i_entries < 2) {
605 if (gfs2_consist_inode(ip))
606 gfs2_dinode_print(ip);
610 if (ip->i_entries > 2) {
615 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
619 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
624 gfs2_glock_dq(ghs + 2);
626 gfs2_holder_uninit(ghs + 2);
627 gfs2_glock_dq(ghs + 1);
629 gfs2_holder_uninit(ghs + 1);
632 gfs2_holder_uninit(ghs);
633 gfs2_glock_dq_uninit(&ri_gh);
638 * gfs2_mknod - Make a special file
639 * @dir: The directory in which the special file will reside
640 * @dentry: The dentry of the special file
641 * @mode: The mode of the special file
642 * @rdev: The device specification of the special file
646 static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
649 struct gfs2_inode *dip = GFS2_I(dir);
650 struct gfs2_sbd *sdp = GFS2_SB(dir);
651 struct gfs2_holder ghs[2];
654 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
656 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
658 gfs2_holder_uninit(ghs);
659 return PTR_ERR(inode);
663 if (dip->i_alloc->al_rgd)
664 gfs2_inplace_release(dip);
665 gfs2_quota_unlock(dip);
668 gfs2_glock_dq_uninit_m(2, ghs);
670 d_instantiate(dentry, inode);
671 mark_inode_dirty(inode);
677 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
681 * Follow @to back to the root and make sure we don't encounter @this
682 * Assumes we already hold the rename lock.
687 static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
689 struct inode *dir = &to->i_inode;
690 struct super_block *sb = dir->i_sb;
697 if (dir == &this->i_inode) {
701 if (dir == sb->s_root->d_inode) {
706 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
708 error = PTR_ERR(tmp);
722 * gfs2_rename - Rename a file
723 * @odir: Parent directory of old file name
724 * @odentry: The old dentry of the file
725 * @ndir: Parent directory of new file name
726 * @ndentry: The new dentry of the file
731 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
732 struct inode *ndir, struct dentry *ndentry)
734 struct gfs2_inode *odip = GFS2_I(odir);
735 struct gfs2_inode *ndip = GFS2_I(ndir);
736 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
737 struct gfs2_inode *nip = NULL;
738 struct gfs2_sbd *sdp = GFS2_SB(odir);
739 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }, ri_gh;
740 struct gfs2_rgrpd *nrgd;
743 int alloc_required = 0;
747 if (ndentry->d_inode) {
748 nip = GFS2_I(ndentry->d_inode);
753 error = gfs2_rindex_hold(sdp, &ri_gh);
758 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
763 if (S_ISDIR(ip->i_inode.i_mode)) {
765 /* don't move a dirctory into it's subdir */
766 error = gfs2_ok_to_move(ip, ndip);
773 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
775 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
778 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
782 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
784 /* grab the resource lock for unlink flag twiddling
785 * this is the case of the target file already existing
786 * so we unlink before doing the rename
788 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
790 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
793 for (x = 0; x < num_gh; x++) {
794 error = gfs2_glock_nq(ghs + x);
799 /* Check out the old directory */
801 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
805 /* Check out the new directory */
808 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
812 if (S_ISDIR(nip->i_inode.i_mode)) {
813 if (nip->i_entries < 2) {
814 if (gfs2_consist_inode(nip))
815 gfs2_dinode_print(nip);
819 if (nip->i_entries > 2) {
825 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC, 0);
829 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
841 if (!ndip->i_inode.i_nlink) {
845 if (ndip->i_entries == (u32)-1) {
849 if (S_ISDIR(ip->i_inode.i_mode) &&
850 ndip->i_inode.i_nlink == (u32)-1) {
857 /* Check out the dir to be renamed */
860 error = gfs2_permission(odentry->d_inode, MAY_WRITE, 0);
866 alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
867 error = alloc_required;
872 if (alloc_required) {
873 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
879 error = gfs2_quota_lock_check(ndip);
883 al->al_requested = sdp->sd_max_dirres;
885 error = gfs2_inplace_reserve_ri(ndip);
889 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
891 4 * RES_DINODE + 4 * RES_LEAF +
892 RES_STATFS + RES_QUOTA + 4, 0);
896 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
897 5 * RES_LEAF + 4, 0);
902 /* Remove the target file, if it exists */
905 if (S_ISDIR(nip->i_inode.i_mode))
906 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
908 error = gfs2_dir_del(ndip, &ndentry->d_name);
911 error = gfs2_change_nlink(nip, -1);
918 error = gfs2_change_nlink(ndip, +1);
921 error = gfs2_change_nlink(odip, -1);
925 error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
929 struct buffer_head *dibh;
930 error = gfs2_meta_inode_buffer(ip, &dibh);
933 ip->i_inode.i_ctime = CURRENT_TIME;
934 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
935 gfs2_dinode_out(ip, dibh->b_data);
939 error = gfs2_dir_del(odip, &odentry->d_name);
943 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
951 gfs2_inplace_release(ndip);
954 gfs2_quota_unlock(ndip);
957 gfs2_alloc_put(ndip);
960 gfs2_glock_dq(ghs + x);
961 gfs2_holder_uninit(ghs + x);
965 gfs2_glock_dq_uninit(&r_gh);
967 gfs2_glock_dq_uninit(&ri_gh);
972 * gfs2_follow_link - Follow a symbolic link
973 * @dentry: The dentry of the link
974 * @nd: Data that we pass to vfs_follow_link()
976 * This can handle symlinks of any size.
978 * Returns: 0 on success or error code
981 static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
983 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
984 struct gfs2_holder i_gh;
985 struct buffer_head *dibh;
986 unsigned int x, size;
990 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
991 error = gfs2_glock_nq(&i_gh);
993 gfs2_holder_uninit(&i_gh);
994 nd_set_link(nd, ERR_PTR(error));
998 size = (unsigned int)i_size_read(&ip->i_inode);
1000 gfs2_consist_inode(ip);
1001 buf = ERR_PTR(-EIO);
1005 error = gfs2_meta_inode_buffer(ip, &dibh);
1007 buf = ERR_PTR(error);
1012 buf = kmalloc(x, GFP_NOFS);
1014 buf = ERR_PTR(-ENOMEM);
1016 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1019 gfs2_glock_dq_uninit(&i_gh);
1020 nd_set_link(nd, buf);
1024 static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1026 char *s = nd_get_link(nd);
1035 * @nd: passed from Linux VFS, ignored by us
1037 * This may be called from the VFS directly, or from within GFS2 with the
1038 * inode locked, so we look to see if the glock is already locked and only
1039 * lock the glock if its not already been done.
1044 int gfs2_permission(struct inode *inode, int mask, unsigned int flags)
1046 struct gfs2_inode *ip;
1047 struct gfs2_holder i_gh;
1051 if (flags & IPERM_FLAG_RCU)
1055 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1056 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1062 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1065 error = generic_permission(inode, mask, flags, gfs2_check_acl);
1067 gfs2_glock_dq_uninit(&i_gh);
1072 static int setattr_chown(struct inode *inode, struct iattr *attr)
1074 struct gfs2_inode *ip = GFS2_I(inode);
1075 struct gfs2_sbd *sdp = GFS2_SB(inode);
1076 u32 ouid, ogid, nuid, ngid;
1079 ouid = inode->i_uid;
1080 ogid = inode->i_gid;
1081 nuid = attr->ia_uid;
1082 ngid = attr->ia_gid;
1084 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1085 ouid = nuid = NO_QUOTA_CHANGE;
1086 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1087 ogid = ngid = NO_QUOTA_CHANGE;
1089 if (!gfs2_alloc_get(ip))
1092 error = gfs2_quota_lock(ip, nuid, ngid);
1096 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1097 error = gfs2_quota_check(ip, nuid, ngid);
1102 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1106 error = gfs2_setattr_simple(ip, attr);
1110 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1111 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1112 gfs2_quota_change(ip, -blocks, ouid, ogid);
1113 gfs2_quota_change(ip, blocks, nuid, ngid);
1117 gfs2_trans_end(sdp);
1119 gfs2_quota_unlock(ip);
1126 * gfs2_setattr - Change attributes on an inode
1127 * @dentry: The dentry which is changing
1128 * @attr: The structure describing the change
1130 * The VFS layer wants to change one or more of an inodes attributes. Write
1131 * that change out to disk.
1136 static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1138 struct inode *inode = dentry->d_inode;
1139 struct gfs2_inode *ip = GFS2_I(inode);
1140 struct gfs2_holder i_gh;
1143 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1148 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1151 error = inode_change_ok(inode, attr);
1155 if (attr->ia_valid & ATTR_SIZE)
1156 error = gfs2_setattr_size(inode, attr->ia_size);
1157 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1158 error = setattr_chown(inode, attr);
1159 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1160 error = gfs2_acl_chmod(ip, attr);
1162 error = gfs2_setattr_simple(ip, attr);
1165 gfs2_glock_dq_uninit(&i_gh);
1167 mark_inode_dirty(inode);
1172 * gfs2_getattr - Read out an inode's attributes
1173 * @mnt: The vfsmount the inode is being accessed from
1174 * @dentry: The dentry to stat
1175 * @stat: The inode's stats
1177 * This may be called from the VFS directly, or from within GFS2 with the
1178 * inode locked, so we look to see if the glock is already locked and only
1179 * lock the glock if its not already been done. Note that its the NFS
1180 * readdirplus operation which causes this to be called (from filldir)
1181 * with the glock already held.
1186 static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1189 struct inode *inode = dentry->d_inode;
1190 struct gfs2_inode *ip = GFS2_I(inode);
1191 struct gfs2_holder gh;
1195 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1196 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1202 generic_fillattr(inode, stat);
1204 gfs2_glock_dq_uninit(&gh);
1209 static int gfs2_setxattr(struct dentry *dentry, const char *name,
1210 const void *data, size_t size, int flags)
1212 struct inode *inode = dentry->d_inode;
1213 struct gfs2_inode *ip = GFS2_I(inode);
1214 struct gfs2_holder gh;
1217 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1218 ret = gfs2_glock_nq(&gh);
1220 ret = generic_setxattr(dentry, name, data, size, flags);
1223 gfs2_holder_uninit(&gh);
1227 static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1228 void *data, size_t size)
1230 struct inode *inode = dentry->d_inode;
1231 struct gfs2_inode *ip = GFS2_I(inode);
1232 struct gfs2_holder gh;
1235 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1236 ret = gfs2_glock_nq(&gh);
1238 ret = generic_getxattr(dentry, name, data, size);
1241 gfs2_holder_uninit(&gh);
1245 static int gfs2_removexattr(struct dentry *dentry, const char *name)
1247 struct inode *inode = dentry->d_inode;
1248 struct gfs2_inode *ip = GFS2_I(inode);
1249 struct gfs2_holder gh;
1252 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1253 ret = gfs2_glock_nq(&gh);
1255 ret = generic_removexattr(dentry, name);
1258 gfs2_holder_uninit(&gh);
1262 static void empty_write_end(struct page *page, unsigned from,
1265 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
1267 page_zero_new_buffers(page, from, to);
1268 flush_dcache_page(page);
1269 mark_page_accessed(page);
1271 if (!gfs2_is_writeback(ip))
1272 gfs2_page_add_databufs(ip, page, from, to);
1274 block_commit_write(page, from, to);
1278 static int write_empty_blocks(struct page *page, unsigned from, unsigned to)
1280 unsigned start, end, next;
1281 struct buffer_head *bh, *head;
1284 if (!page_has_buffers(page)) {
1285 error = __block_write_begin(page, from, to - from, gfs2_block_map);
1286 if (unlikely(error))
1289 empty_write_end(page, from, to);
1293 bh = head = page_buffers(page);
1295 while (next < from) {
1297 bh = bh->b_this_page;
1302 if (buffer_mapped(bh)) {
1304 error = __block_write_begin(page, start, end - start,
1306 if (unlikely(error))
1308 empty_write_end(page, start, end);
1315 bh = bh->b_this_page;
1316 } while (next < to);
1319 error = __block_write_begin(page, start, end - start, gfs2_block_map);
1320 if (unlikely(error))
1322 empty_write_end(page, start, end);
1328 static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
1331 struct gfs2_inode *ip = GFS2_I(inode);
1332 struct buffer_head *dibh;
1334 u64 start = offset >> PAGE_CACHE_SHIFT;
1335 unsigned int start_offset = offset & ~PAGE_CACHE_MASK;
1336 u64 end = (offset + len - 1) >> PAGE_CACHE_SHIFT;
1339 unsigned int end_offset = (offset + len) & ~PAGE_CACHE_MASK;
1340 unsigned int from, to;
1343 end_offset = PAGE_CACHE_SIZE;
1345 error = gfs2_meta_inode_buffer(ip, &dibh);
1346 if (unlikely(error))
1349 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1351 if (gfs2_is_stuffed(ip)) {
1352 error = gfs2_unstuff_dinode(ip, NULL);
1353 if (unlikely(error))
1358 offset = start << PAGE_CACHE_SHIFT;
1359 from = start_offset;
1360 to = PAGE_CACHE_SIZE;
1361 while (curr <= end) {
1362 page = grab_cache_page_write_begin(inode->i_mapping, curr,
1364 if (unlikely(!page)) {
1371 error = write_empty_blocks(page, from, to);
1372 if (!error && offset + to > inode->i_size &&
1373 !(mode & FALLOC_FL_KEEP_SIZE)) {
1374 i_size_write(inode, offset + to);
1377 page_cache_release(page);
1381 offset += PAGE_CACHE_SIZE;
1385 gfs2_dinode_out(ip, dibh->b_data);
1386 mark_inode_dirty(inode);
1394 static void calc_max_reserv(struct gfs2_inode *ip, loff_t max, loff_t *len,
1395 unsigned int *data_blocks, unsigned int *ind_blocks)
1397 const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1398 unsigned int max_blocks = ip->i_alloc->al_rgd->rd_free_clone;
1399 unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
1401 for (tmp = max_data; tmp > sdp->sd_diptrs;) {
1402 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1405 /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
1406 so it might end up with fewer data blocks */
1407 if (max_data <= *data_blocks)
1409 *data_blocks = max_data;
1410 *ind_blocks = max_blocks - max_data;
1411 *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
1414 gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
1418 static long gfs2_fallocate(struct inode *inode, int mode, loff_t offset,
1421 struct gfs2_sbd *sdp = GFS2_SB(inode);
1422 struct gfs2_inode *ip = GFS2_I(inode);
1423 unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
1424 loff_t bytes, max_bytes;
1425 struct gfs2_alloc *al;
1427 loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
1428 next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
1430 offset = (offset >> sdp->sd_sb.sb_bsize_shift) <<
1431 sdp->sd_sb.sb_bsize_shift;
1433 len = next - offset;
1434 bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
1438 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
1439 error = gfs2_glock_nq(&ip->i_gh);
1440 if (unlikely(error))
1443 if (!gfs2_write_alloc_required(ip, offset, len))
1449 al = gfs2_alloc_get(ip);
1455 error = gfs2_quota_lock_check(ip);
1460 gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
1462 al->al_requested = data_blocks + ind_blocks;
1463 error = gfs2_inplace_reserve(ip);
1465 if (error == -ENOSPC && bytes > sdp->sd_sb.sb_bsize) {
1472 calc_max_reserv(ip, len, &max_bytes, &data_blocks, &ind_blocks);
1473 al->al_requested = data_blocks + ind_blocks;
1475 rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
1476 RES_RG_HDR + gfs2_rg_blocks(al);
1477 if (gfs2_is_jdata(ip))
1478 rblocks += data_blocks ? data_blocks : 1;
1480 error = gfs2_trans_begin(sdp, rblocks,
1481 PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
1483 goto out_trans_fail;
1485 error = fallocate_chunk(inode, offset, max_bytes, mode);
1486 gfs2_trans_end(sdp);
1489 goto out_trans_fail;
1492 offset += max_bytes;
1493 gfs2_inplace_release(ip);
1494 gfs2_quota_unlock(ip);
1500 gfs2_inplace_release(ip);
1502 gfs2_quota_unlock(ip);
1506 gfs2_glock_dq(&ip->i_gh);
1508 gfs2_holder_uninit(&ip->i_gh);
1513 static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1516 struct gfs2_inode *ip = GFS2_I(inode);
1517 struct gfs2_holder gh;
1520 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1524 mutex_lock(&inode->i_mutex);
1526 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1530 if (gfs2_is_stuffed(ip)) {
1531 u64 phys = ip->i_no_addr << inode->i_blkbits;
1532 u64 size = i_size_read(inode);
1533 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1534 FIEMAP_EXTENT_DATA_INLINE;
1535 phys += sizeof(struct gfs2_dinode);
1537 if (start + len > size)
1540 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1545 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1549 gfs2_glock_dq_uninit(&gh);
1551 mutex_unlock(&inode->i_mutex);
1555 const struct inode_operations gfs2_file_iops = {
1556 .permission = gfs2_permission,
1557 .setattr = gfs2_setattr,
1558 .getattr = gfs2_getattr,
1559 .setxattr = gfs2_setxattr,
1560 .getxattr = gfs2_getxattr,
1561 .listxattr = gfs2_listxattr,
1562 .removexattr = gfs2_removexattr,
1563 .fallocate = gfs2_fallocate,
1564 .fiemap = gfs2_fiemap,
1567 const struct inode_operations gfs2_dir_iops = {
1568 .create = gfs2_create,
1569 .lookup = gfs2_lookup,
1571 .unlink = gfs2_unlink,
1572 .symlink = gfs2_symlink,
1573 .mkdir = gfs2_mkdir,
1574 .rmdir = gfs2_rmdir,
1575 .mknod = gfs2_mknod,
1576 .rename = gfs2_rename,
1577 .permission = gfs2_permission,
1578 .setattr = gfs2_setattr,
1579 .getattr = gfs2_getattr,
1580 .setxattr = gfs2_setxattr,
1581 .getxattr = gfs2_getxattr,
1582 .listxattr = gfs2_listxattr,
1583 .removexattr = gfs2_removexattr,
1584 .fiemap = gfs2_fiemap,
1587 const struct inode_operations gfs2_symlink_iops = {
1588 .readlink = generic_readlink,
1589 .follow_link = gfs2_follow_link,
1590 .put_link = gfs2_put_link,
1591 .permission = gfs2_permission,
1592 .setattr = gfs2_setattr,
1593 .getattr = gfs2_getattr,
1594 .setxattr = gfs2_setxattr,
1595 .getxattr = gfs2_getxattr,
1596 .listxattr = gfs2_listxattr,
1597 .removexattr = gfs2_removexattr,
1598 .fiemap = gfs2_fiemap,