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 <asm/uaccess.h>
39 * gfs2_create - Create a file
40 * @dir: The directory in which to create the file
41 * @dentry: The dentry of the new file
42 * @mode: The mode of the new file
47 static int gfs2_create(struct inode *dir, struct dentry *dentry,
48 int mode, struct nameidata *nd)
50 struct gfs2_inode *dip = GFS2_I(dir);
51 struct gfs2_sbd *sdp = GFS2_SB(dir);
52 struct gfs2_holder ghs[2];
55 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
58 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
61 if (dip->i_alloc->al_rgd)
62 gfs2_inplace_release(dip);
63 gfs2_quota_unlock(dip);
65 gfs2_glock_dq_uninit_m(2, ghs);
66 mark_inode_dirty(inode);
68 } else if (PTR_ERR(inode) != -EEXIST ||
69 (nd && nd->flags & LOOKUP_EXCL)) {
70 gfs2_holder_uninit(ghs);
71 return PTR_ERR(inode);
74 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
77 gfs2_holder_uninit(ghs);
80 gfs2_holder_uninit(ghs);
81 return PTR_ERR(inode);
86 d_instantiate(dentry, inode);
92 * gfs2_lookup - Look up a filename in a directory and return its inode
93 * @dir: The directory inode
94 * @dentry: The dentry of the new inode
95 * @nd: passed from Linux VFS, ignored by us
97 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
102 static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
103 struct nameidata *nd)
105 struct inode *inode = NULL;
107 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
108 if (inode && IS_ERR(inode))
109 return ERR_CAST(inode);
112 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
113 struct gfs2_holder gh;
115 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
118 return ERR_PTR(error);
120 gfs2_glock_dq_uninit(&gh);
121 return d_splice_alias(inode, dentry);
123 d_add(dentry, inode);
129 * gfs2_link - Link to a file
130 * @old_dentry: The inode to link
131 * @dir: Add link to this directory
132 * @dentry: The name of the link
134 * Link the inode in "old_dentry" into the directory "dir" with the
140 static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
141 struct dentry *dentry)
143 struct gfs2_inode *dip = GFS2_I(dir);
144 struct gfs2_sbd *sdp = GFS2_SB(dir);
145 struct inode *inode = old_dentry->d_inode;
146 struct gfs2_inode *ip = GFS2_I(inode);
147 struct gfs2_holder ghs[2];
148 struct buffer_head *dibh;
152 if (S_ISDIR(inode->i_mode))
155 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
156 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
158 error = gfs2_glock_nq(ghs); /* parent */
162 error = gfs2_glock_nq(ghs + 1); /* child */
167 if (inode->i_nlink == 0)
170 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC, 0);
174 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
185 if (!dip->i_inode.i_nlink)
188 if (dip->i_entries == (u32)-1)
191 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
194 if (!ip->i_inode.i_nlink)
197 if (ip->i_inode.i_nlink == (u32)-1)
200 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
205 if (alloc_required) {
206 struct gfs2_alloc *al = gfs2_alloc_get(dip);
212 error = gfs2_quota_lock_check(dip);
216 al->al_requested = sdp->sd_max_dirres;
218 error = gfs2_inplace_reserve(dip);
222 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
224 2 * RES_DINODE + RES_STATFS +
229 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
234 error = gfs2_meta_inode_buffer(ip, &dibh);
238 error = gfs2_dir_add(dir, &dentry->d_name, ip);
242 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
243 inc_nlink(&ip->i_inode);
244 ip->i_inode.i_ctime = CURRENT_TIME;
245 gfs2_dinode_out(ip, dibh->b_data);
246 mark_inode_dirty(&ip->i_inode);
254 gfs2_inplace_release(dip);
257 gfs2_quota_unlock(dip);
262 gfs2_glock_dq(ghs + 1);
266 gfs2_holder_uninit(ghs);
267 gfs2_holder_uninit(ghs + 1);
270 d_instantiate(dentry, inode);
271 mark_inode_dirty(inode);
277 * gfs2_unlink_ok - check to see that a inode is still in a directory
278 * @dip: the directory
279 * @name: the name of the file
282 * Assumes that the lock on (at least) @dip is held.
284 * Returns: 0 if the parent/child relationship is correct, errno if it isn't
287 static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
288 const struct gfs2_inode *ip)
292 if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
295 if ((dip->i_inode.i_mode & S_ISVTX) &&
296 dip->i_inode.i_uid != current_fsuid() &&
297 ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
300 if (IS_APPEND(&dip->i_inode))
303 error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC, 0);
307 error = gfs2_dir_check(&dip->i_inode, name, ip);
315 * gfs2_unlink_inode - Removes an inode from its parent dir and unlinks it
316 * @dip: The parent directory
317 * @name: The name of the entry in the parent directory
318 * @bh: The inode buffer for the inode to be removed
319 * @inode: The inode to be removed
321 * Called with all the locks and in a transaction. This will only be
322 * called for a directory after it has been checked to ensure it is empty.
324 * Returns: 0 on success, or an error
327 static int gfs2_unlink_inode(struct gfs2_inode *dip,
328 const struct dentry *dentry,
329 struct buffer_head *bh)
331 struct inode *inode = dentry->d_inode;
332 struct gfs2_inode *ip = GFS2_I(inode);
335 error = gfs2_dir_del(dip, dentry);
340 inode->i_ctime = CURRENT_TIME;
341 if (S_ISDIR(inode->i_mode))
345 gfs2_trans_add_bh(ip->i_gl, bh, 1);
346 gfs2_dinode_out(ip, bh->b_data);
347 mark_inode_dirty(inode);
348 if (inode->i_nlink == 0)
349 gfs2_unlink_di(inode);
355 * gfs2_unlink - Unlink an inode (this does rmdir as well)
356 * @dir: The inode of the directory containing the inode to unlink
357 * @dentry: The file itself
359 * This routine uses the type of the inode as a flag to figure out
360 * whether this is an unlink or an rmdir.
365 static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
367 struct gfs2_inode *dip = GFS2_I(dir);
368 struct gfs2_sbd *sdp = GFS2_SB(dir);
369 struct inode *inode = dentry->d_inode;
370 struct gfs2_inode *ip = GFS2_I(inode);
371 struct buffer_head *bh;
372 struct gfs2_holder ghs[3];
373 struct gfs2_rgrpd *rgd;
374 struct gfs2_holder ri_gh;
377 error = gfs2_rindex_hold(sdp, &ri_gh);
381 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
382 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
384 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
385 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
388 error = gfs2_glock_nq(ghs); /* parent */
392 error = gfs2_glock_nq(ghs + 1); /* child */
397 if (inode->i_nlink == 0)
400 if (S_ISDIR(inode->i_mode)) {
402 if (ip->i_entries > 2 || inode->i_nlink > 2)
406 error = gfs2_glock_nq(ghs + 2); /* rgrp */
410 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
414 error = gfs2_trans_begin(sdp, 2*RES_DINODE + 3*RES_LEAF + RES_RG_BIT, 0);
418 error = gfs2_meta_inode_buffer(ip, &bh);
422 error = gfs2_unlink_inode(dip, dentry, bh);
428 gfs2_glock_dq(ghs + 2);
430 gfs2_holder_uninit(ghs + 2);
431 gfs2_glock_dq(ghs + 1);
433 gfs2_holder_uninit(ghs + 1);
436 gfs2_holder_uninit(ghs);
437 gfs2_glock_dq_uninit(&ri_gh);
442 * gfs2_symlink - Create a symlink
443 * @dir: The directory to create the symlink in
444 * @dentry: The dentry to put the symlink in
445 * @symname: The thing which the link points to
450 static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
453 struct gfs2_inode *dip = GFS2_I(dir), *ip;
454 struct gfs2_sbd *sdp = GFS2_SB(dir);
455 struct gfs2_holder ghs[2];
457 struct buffer_head *dibh;
461 /* Must be stuffed with a null terminator for gfs2_follow_link() */
462 size = strlen(symname);
463 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
464 return -ENAMETOOLONG;
466 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
468 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
470 gfs2_holder_uninit(ghs);
471 return PTR_ERR(inode);
474 ip = ghs[1].gh_gl->gl_object;
476 i_size_write(inode, size);
478 error = gfs2_meta_inode_buffer(ip, &dibh);
480 if (!gfs2_assert_withdraw(sdp, !error)) {
481 gfs2_dinode_out(ip, dibh->b_data);
482 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
488 if (dip->i_alloc->al_rgd)
489 gfs2_inplace_release(dip);
490 gfs2_quota_unlock(dip);
493 gfs2_glock_dq_uninit_m(2, ghs);
495 d_instantiate(dentry, inode);
496 mark_inode_dirty(inode);
502 * gfs2_mkdir - Make a directory
503 * @dir: The parent directory of the new one
504 * @dentry: The dentry of the new directory
505 * @mode: The mode of the new directory
510 static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
512 struct gfs2_inode *dip = GFS2_I(dir), *ip;
513 struct gfs2_sbd *sdp = GFS2_SB(dir);
514 struct gfs2_holder ghs[2];
516 struct buffer_head *dibh;
519 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
521 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
523 gfs2_holder_uninit(ghs);
524 return PTR_ERR(inode);
527 ip = ghs[1].gh_gl->gl_object;
529 ip->i_inode.i_nlink = 2;
530 i_size_write(inode, sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode));
531 ip->i_diskflags |= GFS2_DIF_JDATA;
534 error = gfs2_meta_inode_buffer(ip, &dibh);
536 if (!gfs2_assert_withdraw(sdp, !error)) {
537 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
538 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
540 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
541 gfs2_qstr2dirent(&gfs2_qdot, GFS2_DIRENT_SIZE(gfs2_qdot.len), dent);
542 dent->de_inum = di->di_num; /* already GFS2 endian */
543 dent->de_type = cpu_to_be16(DT_DIR);
544 di->di_entries = cpu_to_be32(1);
546 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
547 gfs2_qstr2dirent(&gfs2_qdotdot, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
549 gfs2_inum_out(dip, dent);
550 dent->de_type = cpu_to_be16(DT_DIR);
552 gfs2_dinode_out(ip, di);
558 if (dip->i_alloc->al_rgd)
559 gfs2_inplace_release(dip);
560 gfs2_quota_unlock(dip);
563 gfs2_glock_dq_uninit_m(2, ghs);
565 d_instantiate(dentry, inode);
566 mark_inode_dirty(inode);
572 * gfs2_mknod - Make a special file
573 * @dir: The directory in which the special file will reside
574 * @dentry: The dentry of the special file
575 * @mode: The mode of the special file
576 * @rdev: The device specification of the special file
580 static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
583 struct gfs2_inode *dip = GFS2_I(dir);
584 struct gfs2_sbd *sdp = GFS2_SB(dir);
585 struct gfs2_holder ghs[2];
588 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
590 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
592 gfs2_holder_uninit(ghs);
593 return PTR_ERR(inode);
597 if (dip->i_alloc->al_rgd)
598 gfs2_inplace_release(dip);
599 gfs2_quota_unlock(dip);
602 gfs2_glock_dq_uninit_m(2, ghs);
604 d_instantiate(dentry, inode);
605 mark_inode_dirty(inode);
611 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
615 * Follow @to back to the root and make sure we don't encounter @this
616 * Assumes we already hold the rename lock.
621 static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
623 struct inode *dir = &to->i_inode;
624 struct super_block *sb = dir->i_sb;
631 if (dir == &this->i_inode) {
635 if (dir == sb->s_root->d_inode) {
640 tmp = gfs2_lookupi(dir, &gfs2_qdotdot, 1);
642 error = PTR_ERR(tmp);
656 * gfs2_rename - Rename a file
657 * @odir: Parent directory of old file name
658 * @odentry: The old dentry of the file
659 * @ndir: Parent directory of new file name
660 * @ndentry: The new dentry of the file
665 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
666 struct inode *ndir, struct dentry *ndentry)
668 struct gfs2_inode *odip = GFS2_I(odir);
669 struct gfs2_inode *ndip = GFS2_I(ndir);
670 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
671 struct gfs2_inode *nip = NULL;
672 struct gfs2_sbd *sdp = GFS2_SB(odir);
673 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, }, ri_gh;
674 struct gfs2_rgrpd *nrgd;
677 int alloc_required = 0;
681 if (ndentry->d_inode) {
682 nip = GFS2_I(ndentry->d_inode);
687 error = gfs2_rindex_hold(sdp, &ri_gh);
692 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
697 if (S_ISDIR(ip->i_inode.i_mode)) {
699 /* don't move a dirctory into it's subdir */
700 error = gfs2_ok_to_move(ip, ndip);
707 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
709 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
712 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
716 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
718 /* grab the resource lock for unlink flag twiddling
719 * this is the case of the target file already existing
720 * so we unlink before doing the rename
722 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
724 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
727 for (x = 0; x < num_gh; x++) {
728 error = gfs2_glock_nq(ghs + x);
734 if (ip->i_inode.i_nlink == 0)
737 /* Check out the old directory */
739 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
743 /* Check out the new directory */
746 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
750 if (nip->i_inode.i_nlink == 0) {
755 if (S_ISDIR(nip->i_inode.i_mode)) {
756 if (nip->i_entries < 2) {
757 gfs2_consist_inode(nip);
761 if (nip->i_entries > 2) {
767 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC, 0);
771 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
783 if (!ndip->i_inode.i_nlink) {
787 if (ndip->i_entries == (u32)-1) {
791 if (S_ISDIR(ip->i_inode.i_mode) &&
792 ndip->i_inode.i_nlink == (u32)-1) {
799 /* Check out the dir to be renamed */
802 error = gfs2_permission(odentry->d_inode, MAY_WRITE, 0);
808 alloc_required = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
809 error = alloc_required;
814 if (alloc_required) {
815 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
821 error = gfs2_quota_lock_check(ndip);
825 al->al_requested = sdp->sd_max_dirres;
827 error = gfs2_inplace_reserve_ri(ndip);
831 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
833 4 * RES_DINODE + 4 * RES_LEAF +
834 RES_STATFS + RES_QUOTA + 4, 0);
838 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
839 5 * RES_LEAF + 4, 0);
844 /* Remove the target file, if it exists */
847 struct buffer_head *bh;
848 error = gfs2_meta_inode_buffer(nip, &bh);
851 error = gfs2_unlink_inode(ndip, ndentry, bh);
856 error = gfs2_dir_mvino(ip, &gfs2_qdotdot, ndip, DT_DIR);
860 struct buffer_head *dibh;
861 error = gfs2_meta_inode_buffer(ip, &dibh);
864 ip->i_inode.i_ctime = CURRENT_TIME;
865 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
866 gfs2_dinode_out(ip, dibh->b_data);
870 error = gfs2_dir_del(odip, odentry);
874 error = gfs2_dir_add(ndir, &ndentry->d_name, ip);
882 gfs2_inplace_release(ndip);
885 gfs2_quota_unlock(ndip);
888 gfs2_alloc_put(ndip);
891 gfs2_glock_dq(ghs + x);
892 gfs2_holder_uninit(ghs + x);
896 gfs2_glock_dq_uninit(&r_gh);
898 gfs2_glock_dq_uninit(&ri_gh);
903 * gfs2_follow_link - Follow a symbolic link
904 * @dentry: The dentry of the link
905 * @nd: Data that we pass to vfs_follow_link()
907 * This can handle symlinks of any size.
909 * Returns: 0 on success or error code
912 static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
914 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
915 struct gfs2_holder i_gh;
916 struct buffer_head *dibh;
917 unsigned int x, size;
921 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
922 error = gfs2_glock_nq(&i_gh);
924 gfs2_holder_uninit(&i_gh);
925 nd_set_link(nd, ERR_PTR(error));
929 size = (unsigned int)i_size_read(&ip->i_inode);
931 gfs2_consist_inode(ip);
936 error = gfs2_meta_inode_buffer(ip, &dibh);
938 buf = ERR_PTR(error);
943 buf = kmalloc(x, GFP_NOFS);
945 buf = ERR_PTR(-ENOMEM);
947 memcpy(buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
950 gfs2_glock_dq_uninit(&i_gh);
951 nd_set_link(nd, buf);
955 static void gfs2_put_link(struct dentry *dentry, struct nameidata *nd, void *p)
957 char *s = nd_get_link(nd);
965 * @mask: The mask to be tested
966 * @flags: Indicates whether this is an RCU path walk or not
968 * This may be called from the VFS directly, or from within GFS2 with the
969 * inode locked, so we look to see if the glock is already locked and only
970 * lock the glock if its not already been done.
975 int gfs2_permission(struct inode *inode, int mask, unsigned int flags)
977 struct gfs2_inode *ip;
978 struct gfs2_holder i_gh;
984 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
985 if (flags & IPERM_FLAG_RCU)
987 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
993 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
996 error = generic_permission(inode, mask, flags, gfs2_check_acl);
998 gfs2_glock_dq_uninit(&i_gh);
1003 static int setattr_chown(struct inode *inode, struct iattr *attr)
1005 struct gfs2_inode *ip = GFS2_I(inode);
1006 struct gfs2_sbd *sdp = GFS2_SB(inode);
1007 u32 ouid, ogid, nuid, ngid;
1010 ouid = inode->i_uid;
1011 ogid = inode->i_gid;
1012 nuid = attr->ia_uid;
1013 ngid = attr->ia_gid;
1015 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1016 ouid = nuid = NO_QUOTA_CHANGE;
1017 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1018 ogid = ngid = NO_QUOTA_CHANGE;
1020 if (!gfs2_alloc_get(ip))
1023 error = gfs2_quota_lock(ip, nuid, ngid);
1027 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1028 error = gfs2_quota_check(ip, nuid, ngid);
1033 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1037 error = gfs2_setattr_simple(ip, attr);
1041 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1042 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1043 gfs2_quota_change(ip, -blocks, ouid, ogid);
1044 gfs2_quota_change(ip, blocks, nuid, ngid);
1048 gfs2_trans_end(sdp);
1050 gfs2_quota_unlock(ip);
1057 * gfs2_setattr - Change attributes on an inode
1058 * @dentry: The dentry which is changing
1059 * @attr: The structure describing the change
1061 * The VFS layer wants to change one or more of an inodes attributes. Write
1062 * that change out to disk.
1067 static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1069 struct inode *inode = dentry->d_inode;
1070 struct gfs2_inode *ip = GFS2_I(inode);
1071 struct gfs2_holder i_gh;
1074 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1079 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1082 error = inode_change_ok(inode, attr);
1086 if (attr->ia_valid & ATTR_SIZE)
1087 error = gfs2_setattr_size(inode, attr->ia_size);
1088 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1089 error = setattr_chown(inode, attr);
1090 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1091 error = gfs2_acl_chmod(ip, attr);
1093 error = gfs2_setattr_simple(ip, attr);
1096 gfs2_glock_dq_uninit(&i_gh);
1098 mark_inode_dirty(inode);
1103 * gfs2_getattr - Read out an inode's attributes
1104 * @mnt: The vfsmount the inode is being accessed from
1105 * @dentry: The dentry to stat
1106 * @stat: The inode's stats
1108 * This may be called from the VFS directly, or from within GFS2 with the
1109 * inode locked, so we look to see if the glock is already locked and only
1110 * lock the glock if its not already been done. Note that its the NFS
1111 * readdirplus operation which causes this to be called (from filldir)
1112 * with the glock already held.
1117 static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1120 struct inode *inode = dentry->d_inode;
1121 struct gfs2_inode *ip = GFS2_I(inode);
1122 struct gfs2_holder gh;
1126 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1127 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1133 generic_fillattr(inode, stat);
1135 gfs2_glock_dq_uninit(&gh);
1140 static int gfs2_setxattr(struct dentry *dentry, const char *name,
1141 const void *data, size_t size, int flags)
1143 struct inode *inode = dentry->d_inode;
1144 struct gfs2_inode *ip = GFS2_I(inode);
1145 struct gfs2_holder gh;
1148 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1149 ret = gfs2_glock_nq(&gh);
1151 ret = generic_setxattr(dentry, name, data, size, flags);
1154 gfs2_holder_uninit(&gh);
1158 static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1159 void *data, size_t size)
1161 struct inode *inode = dentry->d_inode;
1162 struct gfs2_inode *ip = GFS2_I(inode);
1163 struct gfs2_holder gh;
1166 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1167 ret = gfs2_glock_nq(&gh);
1169 ret = generic_getxattr(dentry, name, data, size);
1172 gfs2_holder_uninit(&gh);
1176 static int gfs2_removexattr(struct dentry *dentry, const char *name)
1178 struct inode *inode = dentry->d_inode;
1179 struct gfs2_inode *ip = GFS2_I(inode);
1180 struct gfs2_holder gh;
1183 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
1184 ret = gfs2_glock_nq(&gh);
1186 ret = generic_removexattr(dentry, name);
1189 gfs2_holder_uninit(&gh);
1193 static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1196 struct gfs2_inode *ip = GFS2_I(inode);
1197 struct gfs2_holder gh;
1200 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1204 mutex_lock(&inode->i_mutex);
1206 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1210 if (gfs2_is_stuffed(ip)) {
1211 u64 phys = ip->i_no_addr << inode->i_blkbits;
1212 u64 size = i_size_read(inode);
1213 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1214 FIEMAP_EXTENT_DATA_INLINE;
1215 phys += sizeof(struct gfs2_dinode);
1217 if (start + len > size)
1220 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1225 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1229 gfs2_glock_dq_uninit(&gh);
1231 mutex_unlock(&inode->i_mutex);
1235 const struct inode_operations gfs2_file_iops = {
1236 .permission = gfs2_permission,
1237 .setattr = gfs2_setattr,
1238 .getattr = gfs2_getattr,
1239 .setxattr = gfs2_setxattr,
1240 .getxattr = gfs2_getxattr,
1241 .listxattr = gfs2_listxattr,
1242 .removexattr = gfs2_removexattr,
1243 .fiemap = gfs2_fiemap,
1246 const struct inode_operations gfs2_dir_iops = {
1247 .create = gfs2_create,
1248 .lookup = gfs2_lookup,
1250 .unlink = gfs2_unlink,
1251 .symlink = gfs2_symlink,
1252 .mkdir = gfs2_mkdir,
1253 .rmdir = gfs2_unlink,
1254 .mknod = gfs2_mknod,
1255 .rename = gfs2_rename,
1256 .permission = gfs2_permission,
1257 .setattr = gfs2_setattr,
1258 .getattr = gfs2_getattr,
1259 .setxattr = gfs2_setxattr,
1260 .getxattr = gfs2_getxattr,
1261 .listxattr = gfs2_listxattr,
1262 .removexattr = gfs2_removexattr,
1263 .fiemap = gfs2_fiemap,
1266 const struct inode_operations gfs2_symlink_iops = {
1267 .readlink = generic_readlink,
1268 .follow_link = gfs2_follow_link,
1269 .put_link = gfs2_put_link,
1270 .permission = gfs2_permission,
1271 .setattr = gfs2_setattr,
1272 .getattr = gfs2_getattr,
1273 .setxattr = gfs2_setxattr,
1274 .getxattr = gfs2_getxattr,
1275 .listxattr = gfs2_listxattr,
1276 .removexattr = gfs2_removexattr,
1277 .fiemap = gfs2_fiemap,