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 dentry->d_op = &gfs2_dops;
111 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
112 if (inode && IS_ERR(inode))
113 return ERR_CAST(inode);
116 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
117 struct gfs2_holder gh;
119 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
122 return ERR_PTR(error);
124 gfs2_glock_dq_uninit(&gh);
125 return d_splice_alias(inode, dentry);
127 d_add(dentry, inode);
133 * gfs2_link - Link to a file
134 * @old_dentry: The inode to link
135 * @dir: Add link to this directory
136 * @dentry: The name of the link
138 * Link the inode in "old_dentry" into the directory "dir" with the
144 static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
145 struct dentry *dentry)
147 struct gfs2_inode *dip = GFS2_I(dir);
148 struct gfs2_sbd *sdp = GFS2_SB(dir);
149 struct inode *inode = old_dentry->d_inode;
150 struct gfs2_inode *ip = GFS2_I(inode);
151 struct gfs2_holder ghs[2];
155 if (S_ISDIR(inode->i_mode))
158 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
159 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
161 error = gfs2_glock_nq(ghs); /* parent */
165 error = gfs2_glock_nq(ghs + 1); /* child */
169 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
173 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
184 if (!dip->i_inode.i_nlink)
187 if (dip->i_entries == (u32)-1)
190 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
193 if (!ip->i_inode.i_nlink)
196 if (ip->i_inode.i_nlink == (u32)-1)
199 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
204 if (alloc_required) {
205 struct gfs2_alloc *al = gfs2_alloc_get(dip);
211 error = gfs2_quota_lock_check(dip);
215 al->al_requested = sdp->sd_max_dirres;
217 error = gfs2_inplace_reserve(dip);
221 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
222 al->al_rgd->rd_length +
223 2 * RES_DINODE + RES_STATFS +
228 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
233 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
237 error = gfs2_change_nlink(ip, +1);
243 gfs2_inplace_release(dip);
246 gfs2_quota_unlock(dip);
251 gfs2_glock_dq(ghs + 1);
255 gfs2_holder_uninit(ghs);
256 gfs2_holder_uninit(ghs + 1);
258 atomic_inc(&inode->i_count);
259 d_instantiate(dentry, inode);
260 mark_inode_dirty(inode);
266 * gfs2_unlink_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);
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);
476 gfs2_str2qstr(&str, ".");
477 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
478 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
479 dent->de_inum = di->di_num; /* already GFS2 endian */
480 dent->de_type = cpu_to_be16(DT_DIR);
481 di->di_entries = cpu_to_be32(1);
483 gfs2_str2qstr(&str, "..");
484 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
485 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
487 gfs2_inum_out(dip, dent);
488 dent->de_type = cpu_to_be16(DT_DIR);
490 gfs2_dinode_out(ip, di);
495 error = gfs2_change_nlink(dip, +1);
496 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
499 if (dip->i_alloc->al_rgd)
500 gfs2_inplace_release(dip);
501 gfs2_quota_unlock(dip);
504 gfs2_glock_dq_uninit_m(2, ghs);
506 d_instantiate(dentry, inode);
507 mark_inode_dirty(inode);
513 * gfs2_rmdiri - Remove a directory
514 * @dip: The parent directory of the directory to be removed
515 * @name: The name of the directory to be removed
516 * @ip: The GFS2 inode of the directory to be removed
518 * Assumes Glocks on dip and ip are held
523 static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
524 struct gfs2_inode *ip)
529 if (ip->i_entries != 2) {
530 if (gfs2_consist_inode(ip))
531 gfs2_dinode_print(ip);
535 error = gfs2_dir_del(dip, name);
539 error = gfs2_change_nlink(dip, -1);
543 gfs2_str2qstr(&dotname, ".");
544 error = gfs2_dir_del(ip, &dotname);
548 gfs2_str2qstr(&dotname, "..");
549 error = gfs2_dir_del(ip, &dotname);
553 /* It looks odd, but it really should be done twice */
554 error = gfs2_change_nlink(ip, -1);
558 error = gfs2_change_nlink(ip, -1);
566 * gfs2_rmdir - Remove a directory
567 * @dir: The parent directory of the directory to be removed
568 * @dentry: The dentry of the directory to remove
570 * Remove a directory. Call gfs2_rmdiri()
575 static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
577 struct gfs2_inode *dip = GFS2_I(dir);
578 struct gfs2_sbd *sdp = GFS2_SB(dir);
579 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
580 struct gfs2_holder ghs[3];
581 struct gfs2_rgrpd *rgd;
582 struct gfs2_holder ri_gh;
585 error = gfs2_rindex_hold(sdp, &ri_gh);
588 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
589 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
591 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
592 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
594 error = gfs2_glock_nq(ghs); /* parent */
598 error = gfs2_glock_nq(ghs + 1); /* child */
602 error = gfs2_glock_nq(ghs + 2); /* rgrp */
606 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
610 if (ip->i_entries < 2) {
611 if (gfs2_consist_inode(ip))
612 gfs2_dinode_print(ip);
616 if (ip->i_entries > 2) {
621 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
625 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
630 gfs2_glock_dq(ghs + 2);
632 gfs2_holder_uninit(ghs + 2);
633 gfs2_glock_dq(ghs + 1);
635 gfs2_holder_uninit(ghs + 1);
638 gfs2_holder_uninit(ghs);
639 gfs2_glock_dq_uninit(&ri_gh);
644 * gfs2_mknod - Make a special file
645 * @dir: The directory in which the special file will reside
646 * @dentry: The dentry of the special file
647 * @mode: The mode of the special file
648 * @rdev: The device specification of the special file
652 static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
655 struct gfs2_inode *dip = GFS2_I(dir);
656 struct gfs2_sbd *sdp = GFS2_SB(dir);
657 struct gfs2_holder ghs[2];
660 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
662 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
664 gfs2_holder_uninit(ghs);
665 return PTR_ERR(inode);
669 if (dip->i_alloc->al_rgd)
670 gfs2_inplace_release(dip);
671 gfs2_quota_unlock(dip);
674 gfs2_glock_dq_uninit_m(2, ghs);
676 d_instantiate(dentry, inode);
677 mark_inode_dirty(inode);
683 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
687 * Follow @to back to the root and make sure we don't encounter @this
688 * Assumes we already hold the rename lock.
693 static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
695 struct inode *dir = &to->i_inode;
696 struct super_block *sb = dir->i_sb;
701 gfs2_str2qstr(&dotdot, "..");
706 if (dir == &this->i_inode) {
710 if (dir == sb->s_root->d_inode) {
715 tmp = gfs2_lookupi(dir, &dotdot, 1);
717 error = PTR_ERR(tmp);
731 * gfs2_rename - Rename a file
732 * @odir: Parent directory of old file name
733 * @odentry: The old dentry of the file
734 * @ndir: Parent directory of new file name
735 * @ndentry: The new dentry of the file
740 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
741 struct inode *ndir, struct dentry *ndentry)
743 struct gfs2_inode *odip = GFS2_I(odir);
744 struct gfs2_inode *ndip = GFS2_I(ndir);
745 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
746 struct gfs2_inode *nip = NULL;
747 struct gfs2_sbd *sdp = GFS2_SB(odir);
748 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
749 struct gfs2_rgrpd *nrgd;
752 int alloc_required = 0;
756 if (ndentry->d_inode) {
757 nip = GFS2_I(ndentry->d_inode);
764 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
769 if (S_ISDIR(ip->i_inode.i_mode)) {
771 /* don't move a dirctory into it's subdir */
772 error = gfs2_ok_to_move(ip, ndip);
779 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
781 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
784 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
788 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
790 /* grab the resource lock for unlink flag twiddling
791 * this is the case of the target file already existing
792 * so we unlink before doing the rename
794 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
796 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
799 for (x = 0; x < num_gh; x++) {
800 error = gfs2_glock_nq(ghs + x);
805 /* Check out the old directory */
807 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
811 /* Check out the new directory */
814 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
818 if (S_ISDIR(nip->i_inode.i_mode)) {
819 if (nip->i_entries < 2) {
820 if (gfs2_consist_inode(nip))
821 gfs2_dinode_print(nip);
825 if (nip->i_entries > 2) {
831 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
835 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
847 if (!ndip->i_inode.i_nlink) {
851 if (ndip->i_entries == (u32)-1) {
855 if (S_ISDIR(ip->i_inode.i_mode) &&
856 ndip->i_inode.i_nlink == (u32)-1) {
863 /* Check out the dir to be renamed */
866 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
872 alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
873 error = alloc_required;
878 if (alloc_required) {
879 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
885 error = gfs2_quota_lock_check(ndip);
889 al->al_requested = sdp->sd_max_dirres;
891 error = gfs2_inplace_reserve(ndip);
895 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
896 al->al_rgd->rd_length +
897 4 * RES_DINODE + 4 * RES_LEAF +
898 RES_STATFS + RES_QUOTA + 4, 0);
902 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
903 5 * RES_LEAF + 4, 0);
908 /* Remove the target file, if it exists */
911 if (S_ISDIR(nip->i_inode.i_mode))
912 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
914 error = gfs2_dir_del(ndip, &ndentry->d_name);
917 error = gfs2_change_nlink(nip, -1);
925 gfs2_str2qstr(&name, "..");
927 error = gfs2_change_nlink(ndip, +1);
930 error = gfs2_change_nlink(odip, -1);
934 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
938 struct buffer_head *dibh;
939 error = gfs2_meta_inode_buffer(ip, &dibh);
942 ip->i_inode.i_ctime = CURRENT_TIME;
943 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
944 gfs2_dinode_out(ip, dibh->b_data);
948 error = gfs2_dir_del(odip, &odentry->d_name);
952 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
960 gfs2_inplace_release(ndip);
963 gfs2_quota_unlock(ndip);
966 gfs2_alloc_put(ndip);
969 gfs2_glock_dq(ghs + x);
970 gfs2_holder_uninit(ghs + x);
974 gfs2_glock_dq_uninit(&r_gh);
980 * gfs2_follow_link - Follow a symbolic link
981 * @dentry: The dentry of the link
982 * @nd: Data that we pass to vfs_follow_link()
984 * This can handle symlinks of any size.
986 * Returns: 0 on success or error code
989 static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
991 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
992 struct gfs2_holder i_gh;
993 struct buffer_head *dibh;
994 unsigned int x, size;
998 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
999 error = gfs2_glock_nq(&i_gh);
1001 gfs2_holder_uninit(&i_gh);
1002 nd_set_link(nd, ERR_PTR(error));
1006 size = (unsigned int)i_size_read(&ip->i_inode);
1008 gfs2_consist_inode(ip);
1009 buf = ERR_PTR(-EIO);
1013 error = gfs2_meta_inode_buffer(ip, &dibh);
1015 buf = ERR_PTR(error);
1020 buf = kmalloc(x, GFP_NOFS);
1022 buf = ERR_PTR(-ENOMEM);
1024 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1027 gfs2_glock_dq_uninit(&i_gh);
1028 nd_set_link(nd, buf);
1032 static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
1034 char *s = nd_get_link(nd);
1043 * @nd: passed from Linux VFS, ignored by us
1045 * This may be called from the VFS directly, or from within GFS2 with the
1046 * inode locked, so we look to see if the glock is already locked and only
1047 * lock the glock if its not already been done.
1052 int gfs2_permission(struct inode *inode, int mask)
1054 struct gfs2_inode *ip = GFS2_I(inode);
1055 struct gfs2_holder i_gh;
1059 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1060 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1066 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1069 error = generic_permission(inode, mask, gfs2_check_acl);
1071 gfs2_glock_dq_uninit(&i_gh);
1076 static int setattr_chown(struct inode *inode, struct iattr *attr)
1078 struct gfs2_inode *ip = GFS2_I(inode);
1079 struct gfs2_sbd *sdp = GFS2_SB(inode);
1080 struct buffer_head *dibh;
1081 u32 ouid, ogid, nuid, ngid;
1084 ouid = inode->i_uid;
1085 ogid = inode->i_gid;
1086 nuid = attr->ia_uid;
1087 ngid = attr->ia_gid;
1089 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1090 ouid = nuid = NO_QUOTA_CHANGE;
1091 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1092 ogid = ngid = NO_QUOTA_CHANGE;
1094 if (!gfs2_alloc_get(ip))
1097 error = gfs2_quota_lock(ip, nuid, ngid);
1101 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1102 error = gfs2_quota_check(ip, nuid, ngid);
1107 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1111 error = gfs2_meta_inode_buffer(ip, &dibh);
1115 if ((attr->ia_valid & ATTR_SIZE) &&
1116 attr->ia_size != i_size_read(inode)) {
1119 error = vmtruncate(inode, attr->ia_size);
1120 gfs2_assert_warn(sdp, !error);
1123 setattr_copy(inode, attr);
1124 mark_inode_dirty(inode);
1126 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1127 gfs2_dinode_out(ip, dibh->b_data);
1130 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1131 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1132 gfs2_quota_change(ip, -blocks, ouid, ogid);
1133 gfs2_quota_change(ip, blocks, nuid, ngid);
1137 gfs2_trans_end(sdp);
1139 gfs2_quota_unlock(ip);
1146 * gfs2_setattr - Change attributes on an inode
1147 * @dentry: The dentry which is changing
1148 * @attr: The structure describing the change
1150 * The VFS layer wants to change one or more of an inodes attributes. Write
1151 * that change out to disk.
1156 static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1158 struct inode *inode = dentry->d_inode;
1159 struct gfs2_inode *ip = GFS2_I(inode);
1160 struct gfs2_holder i_gh;
1163 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1168 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1171 error = inode_change_ok(inode, attr);
1175 if (attr->ia_valid & ATTR_SIZE)
1176 error = gfs2_setattr_size(inode, attr->ia_size);
1177 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1178 error = setattr_chown(inode, attr);
1179 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1180 error = gfs2_acl_chmod(ip, attr);
1182 error = gfs2_setattr_simple(ip, attr);
1185 gfs2_glock_dq_uninit(&i_gh);
1187 mark_inode_dirty(inode);
1192 * gfs2_getattr - Read out an inode's attributes
1193 * @mnt: The vfsmount the inode is being accessed from
1194 * @dentry: The dentry to stat
1195 * @stat: The inode's stats
1197 * This may be called from the VFS directly, or from within GFS2 with the
1198 * inode locked, so we look to see if the glock is already locked and only
1199 * lock the glock if its not already been done. Note that its the NFS
1200 * readdirplus operation which causes this to be called (from filldir)
1201 * with the glock already held.
1206 static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1209 struct inode *inode = dentry->d_inode;
1210 struct gfs2_inode *ip = GFS2_I(inode);
1211 struct gfs2_holder gh;
1215 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1216 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1222 generic_fillattr(inode, stat);
1224 gfs2_glock_dq_uninit(&gh);
1229 static int gfs2_setxattr(struct dentry *dentry, const char *name,
1230 const void *data, size_t size, int flags)
1232 struct inode *inode = dentry->d_inode;
1233 struct gfs2_inode *ip = GFS2_I(inode);
1234 struct gfs2_holder gh;
1237 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1238 ret = gfs2_glock_nq(&gh);
1240 ret = generic_setxattr(dentry, name, data, size, flags);
1243 gfs2_holder_uninit(&gh);
1247 static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1248 void *data, size_t size)
1250 struct inode *inode = dentry->d_inode;
1251 struct gfs2_inode *ip = GFS2_I(inode);
1252 struct gfs2_holder gh;
1255 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1256 ret = gfs2_glock_nq(&gh);
1258 ret = generic_getxattr(dentry, name, data, size);
1261 gfs2_holder_uninit(&gh);
1265 static int gfs2_removexattr(struct dentry *dentry, const char *name)
1267 struct inode *inode = dentry->d_inode;
1268 struct gfs2_inode *ip = GFS2_I(inode);
1269 struct gfs2_holder gh;
1272 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1273 ret = gfs2_glock_nq(&gh);
1275 ret = generic_removexattr(dentry, name);
1278 gfs2_holder_uninit(&gh);
1282 static void empty_write_end(struct page *page, unsigned from,
1285 struct gfs2_inode *ip = GFS2_I(page->mapping->host);
1287 page_zero_new_buffers(page, from, to);
1288 flush_dcache_page(page);
1289 mark_page_accessed(page);
1291 if (!gfs2_is_writeback(ip))
1292 gfs2_page_add_databufs(ip, page, from, to);
1294 block_commit_write(page, from, to);
1298 static int write_empty_blocks(struct page *page, unsigned from, unsigned to)
1300 unsigned start, end, next;
1301 struct buffer_head *bh, *head;
1304 if (!page_has_buffers(page)) {
1305 error = block_prepare_write(page, from, to, gfs2_block_map);
1306 if (unlikely(error))
1309 empty_write_end(page, from, to);
1313 bh = head = page_buffers(page);
1315 while (next < from) {
1317 bh = bh->b_this_page;
1322 if (buffer_mapped(bh)) {
1324 error = block_prepare_write(page, start, end,
1326 if (unlikely(error))
1328 empty_write_end(page, start, end);
1335 bh = bh->b_this_page;
1336 } while (next < to);
1339 error = block_prepare_write(page, start, end, gfs2_block_map);
1340 if (unlikely(error))
1342 empty_write_end(page, start, end);
1348 static int fallocate_chunk(struct inode *inode, loff_t offset, loff_t len,
1351 struct gfs2_inode *ip = GFS2_I(inode);
1352 struct buffer_head *dibh;
1354 u64 start = offset >> PAGE_CACHE_SHIFT;
1355 unsigned int start_offset = offset & ~PAGE_CACHE_MASK;
1356 u64 end = (offset + len - 1) >> PAGE_CACHE_SHIFT;
1359 unsigned int end_offset = (offset + len) & ~PAGE_CACHE_MASK;
1360 unsigned int from, to;
1363 end_offset = PAGE_CACHE_SIZE;
1365 error = gfs2_meta_inode_buffer(ip, &dibh);
1366 if (unlikely(error))
1369 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1371 if (gfs2_is_stuffed(ip)) {
1372 error = gfs2_unstuff_dinode(ip, NULL);
1373 if (unlikely(error))
1378 offset = start << PAGE_CACHE_SHIFT;
1379 from = start_offset;
1380 to = PAGE_CACHE_SIZE;
1381 while (curr <= end) {
1382 page = grab_cache_page_write_begin(inode->i_mapping, curr,
1384 if (unlikely(!page)) {
1391 error = write_empty_blocks(page, from, to);
1392 if (!error && offset + to > inode->i_size &&
1393 !(mode & FALLOC_FL_KEEP_SIZE)) {
1394 i_size_write(inode, offset + to);
1397 page_cache_release(page);
1401 offset += PAGE_CACHE_SIZE;
1405 gfs2_dinode_out(ip, dibh->b_data);
1406 mark_inode_dirty(inode);
1414 static void calc_max_reserv(struct gfs2_inode *ip, loff_t max, loff_t *len,
1415 unsigned int *data_blocks, unsigned int *ind_blocks)
1417 const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1418 unsigned int max_blocks = ip->i_alloc->al_rgd->rd_free_clone;
1419 unsigned int tmp, max_data = max_blocks - 3 * (sdp->sd_max_height - 1);
1421 for (tmp = max_data; tmp > sdp->sd_diptrs;) {
1422 tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
1425 /* This calculation isn't the exact reverse of gfs2_write_calc_reserve,
1426 so it might end up with fewer data blocks */
1427 if (max_data <= *data_blocks)
1429 *data_blocks = max_data;
1430 *ind_blocks = max_blocks - max_data;
1431 *len = ((loff_t)max_data - 3) << sdp->sd_sb.sb_bsize_shift;
1434 gfs2_write_calc_reserv(ip, max, data_blocks, ind_blocks);
1438 static long gfs2_fallocate(struct inode *inode, int mode, loff_t offset,
1441 struct gfs2_sbd *sdp = GFS2_SB(inode);
1442 struct gfs2_inode *ip = GFS2_I(inode);
1443 unsigned int data_blocks = 0, ind_blocks = 0, rblocks;
1444 loff_t bytes, max_bytes;
1445 struct gfs2_alloc *al;
1447 loff_t next = (offset + len - 1) >> sdp->sd_sb.sb_bsize_shift;
1448 next = (next + 1) << sdp->sd_sb.sb_bsize_shift;
1450 offset = (offset >> sdp->sd_sb.sb_bsize_shift) <<
1451 sdp->sd_sb.sb_bsize_shift;
1453 len = next - offset;
1454 bytes = sdp->sd_max_rg_data * sdp->sd_sb.sb_bsize / 2;
1458 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
1459 error = gfs2_glock_nq(&ip->i_gh);
1460 if (unlikely(error))
1463 if (!gfs2_write_alloc_required(ip, offset, len))
1469 al = gfs2_alloc_get(ip);
1475 error = gfs2_quota_lock_check(ip);
1480 gfs2_write_calc_reserv(ip, bytes, &data_blocks, &ind_blocks);
1482 al->al_requested = data_blocks + ind_blocks;
1483 error = gfs2_inplace_reserve(ip);
1485 if (error == -ENOSPC && bytes > sdp->sd_sb.sb_bsize) {
1492 calc_max_reserv(ip, len, &max_bytes, &data_blocks, &ind_blocks);
1493 al->al_requested = data_blocks + ind_blocks;
1495 rblocks = RES_DINODE + ind_blocks + RES_STATFS + RES_QUOTA +
1496 RES_RG_HDR + ip->i_alloc->al_rgd->rd_length;
1497 if (gfs2_is_jdata(ip))
1498 rblocks += data_blocks ? data_blocks : 1;
1500 error = gfs2_trans_begin(sdp, rblocks,
1501 PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
1503 goto out_trans_fail;
1505 error = fallocate_chunk(inode, offset, max_bytes, mode);
1506 gfs2_trans_end(sdp);
1509 goto out_trans_fail;
1512 offset += max_bytes;
1513 gfs2_inplace_release(ip);
1514 gfs2_quota_unlock(ip);
1520 gfs2_inplace_release(ip);
1522 gfs2_quota_unlock(ip);
1526 gfs2_glock_dq(&ip->i_gh);
1528 gfs2_holder_uninit(&ip->i_gh);
1533 static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1536 struct gfs2_inode *ip = GFS2_I(inode);
1537 struct gfs2_holder gh;
1540 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1544 mutex_lock(&inode->i_mutex);
1546 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1550 if (gfs2_is_stuffed(ip)) {
1551 u64 phys = ip->i_no_addr << inode->i_blkbits;
1552 u64 size = i_size_read(inode);
1553 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1554 FIEMAP_EXTENT_DATA_INLINE;
1555 phys += sizeof(struct gfs2_dinode);
1557 if (start + len > size)
1560 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1565 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1569 gfs2_glock_dq_uninit(&gh);
1571 mutex_unlock(&inode->i_mutex);
1575 const struct inode_operations gfs2_file_iops = {
1576 .permission = gfs2_permission,
1577 .setattr = gfs2_setattr,
1578 .getattr = gfs2_getattr,
1579 .setxattr = gfs2_setxattr,
1580 .getxattr = gfs2_getxattr,
1581 .listxattr = gfs2_listxattr,
1582 .removexattr = gfs2_removexattr,
1583 .fallocate = gfs2_fallocate,
1584 .fiemap = gfs2_fiemap,
1587 const struct inode_operations gfs2_dir_iops = {
1588 .create = gfs2_create,
1589 .lookup = gfs2_lookup,
1591 .unlink = gfs2_unlink,
1592 .symlink = gfs2_symlink,
1593 .mkdir = gfs2_mkdir,
1594 .rmdir = gfs2_rmdir,
1595 .mknod = gfs2_mknod,
1596 .rename = gfs2_rename,
1597 .permission = gfs2_permission,
1598 .setattr = gfs2_setattr,
1599 .getattr = gfs2_getattr,
1600 .setxattr = gfs2_setxattr,
1601 .getxattr = gfs2_getxattr,
1602 .listxattr = gfs2_listxattr,
1603 .removexattr = gfs2_removexattr,
1604 .fiemap = gfs2_fiemap,
1607 const struct inode_operations gfs2_symlink_iops = {
1608 .readlink = generic_readlink,
1609 .follow_link = gfs2_follow_link,
1610 .put_link = gfs2_put_link,
1611 .permission = gfs2_permission,
1612 .setattr = gfs2_setattr,
1613 .getattr = gfs2_getattr,
1614 .setxattr = gfs2_setxattr,
1615 .getxattr = gfs2_getxattr,
1616 .listxattr = gfs2_listxattr,
1617 .removexattr = gfs2_removexattr,
1618 .fiemap = gfs2_fiemap,