2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License version 2.
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
14 #include <linux/namei.h>
15 #include <linux/utsname.h>
17 #include <linux/xattr.h>
18 #include <linux/posix_acl.h>
19 #include <linux/gfs2_ondisk.h>
20 #include <linux/crc32.h>
21 #include <linux/lm_interface.h>
22 #include <linux/fiemap.h>
23 #include <asm/uaccess.h>
35 #include "ops_dentry.h"
36 #include "ops_inode.h"
43 * gfs2_create - Create a file
44 * @dir: The directory in which to create the file
45 * @dentry: The dentry of the new file
46 * @mode: The mode of the new file
51 static int gfs2_create(struct inode *dir, struct dentry *dentry,
52 int mode, struct nameidata *nd)
54 struct gfs2_inode *dip = GFS2_I(dir);
55 struct gfs2_sbd *sdp = GFS2_SB(dir);
56 struct gfs2_holder ghs[2];
59 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
62 inode = gfs2_createi(ghs, &dentry->d_name, S_IFREG | mode, 0);
65 if (dip->i_alloc->al_rgd)
66 gfs2_inplace_release(dip);
67 gfs2_quota_unlock(dip);
69 gfs2_glock_dq_uninit_m(2, ghs);
70 mark_inode_dirty(inode);
72 } else if (PTR_ERR(inode) != -EEXIST ||
73 (nd && nd->flags & LOOKUP_EXCL)) {
74 gfs2_holder_uninit(ghs);
75 return PTR_ERR(inode);
78 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
81 gfs2_holder_uninit(ghs);
84 gfs2_holder_uninit(ghs);
85 return PTR_ERR(inode);
90 d_instantiate(dentry, inode);
96 * gfs2_lookup - Look up a filename in a directory and return its inode
97 * @dir: The directory inode
98 * @dentry: The dentry of the new inode
99 * @nd: passed from Linux VFS, ignored by us
101 * Called by the VFS layer. Lock dir and call gfs2_lookupi()
106 static struct dentry *gfs2_lookup(struct inode *dir, struct dentry *dentry,
107 struct nameidata *nd)
109 struct inode *inode = NULL;
111 dentry->d_op = &gfs2_dops;
113 inode = gfs2_lookupi(dir, &dentry->d_name, 0);
114 if (inode && IS_ERR(inode))
115 return ERR_CAST(inode);
118 struct gfs2_glock *gl = GFS2_I(inode)->i_gl;
119 struct gfs2_holder gh;
121 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
124 return ERR_PTR(error);
126 gfs2_glock_dq_uninit(&gh);
127 return d_splice_alias(inode, dentry);
129 d_add(dentry, inode);
135 * gfs2_link - Link to a file
136 * @old_dentry: The inode to link
137 * @dir: Add link to this directory
138 * @dentry: The name of the link
140 * Link the inode in "old_dentry" into the directory "dir" with the
146 static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
147 struct dentry *dentry)
149 struct gfs2_inode *dip = GFS2_I(dir);
150 struct gfs2_sbd *sdp = GFS2_SB(dir);
151 struct inode *inode = old_dentry->d_inode;
152 struct gfs2_inode *ip = GFS2_I(inode);
153 struct gfs2_holder ghs[2];
157 if (S_ISDIR(inode->i_mode))
160 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
161 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
163 error = gfs2_glock_nq(ghs); /* parent */
167 error = gfs2_glock_nq(ghs + 1); /* child */
171 error = gfs2_permission(dir, MAY_WRITE | MAY_EXEC);
175 error = gfs2_dir_check(dir, &dentry->d_name, NULL);
186 if (!dip->i_inode.i_nlink)
189 if (dip->i_di.di_entries == (u32)-1)
192 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
195 if (!ip->i_inode.i_nlink)
198 if (ip->i_inode.i_nlink == (u32)-1)
201 alloc_required = error = gfs2_diradd_alloc_required(dir, &dentry->d_name);
206 if (alloc_required) {
207 struct gfs2_alloc *al = gfs2_alloc_get(dip);
213 error = gfs2_quota_lock_check(dip);
217 al->al_requested = sdp->sd_max_dirres;
219 error = gfs2_inplace_reserve(dip);
223 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
224 al->al_rgd->rd_length +
225 2 * RES_DINODE + RES_STATFS +
230 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + RES_LEAF, 0);
235 error = gfs2_dir_add(dir, &dentry->d_name, ip, IF2DT(inode->i_mode));
239 error = gfs2_change_nlink(ip, +1);
245 gfs2_inplace_release(dip);
248 gfs2_quota_unlock(dip);
253 gfs2_glock_dq(ghs + 1);
257 gfs2_holder_uninit(ghs);
258 gfs2_holder_uninit(ghs + 1);
260 atomic_inc(&inode->i_count);
261 d_instantiate(dentry, inode);
262 mark_inode_dirty(inode);
268 * gfs2_unlink - Unlink a file
269 * @dir: The inode of the directory containing the file to unlink
270 * @dentry: The file itself
272 * Unlink a file. Call gfs2_unlinki()
277 static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
279 struct gfs2_inode *dip = GFS2_I(dir);
280 struct gfs2_sbd *sdp = GFS2_SB(dir);
281 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
282 struct gfs2_holder ghs[3];
283 struct gfs2_rgrpd *rgd;
284 struct gfs2_holder ri_gh;
287 error = gfs2_rindex_hold(sdp, &ri_gh);
291 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
292 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
294 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
295 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
298 error = gfs2_glock_nq(ghs); /* parent */
302 error = gfs2_glock_nq(ghs + 1); /* child */
306 error = gfs2_glock_nq(ghs + 2); /* rgrp */
310 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
314 error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
318 error = gfs2_dir_del(dip, &dentry->d_name);
322 error = gfs2_change_nlink(ip, -1);
327 gfs2_glock_dq(ghs + 2);
329 gfs2_holder_uninit(ghs + 2);
330 gfs2_glock_dq(ghs + 1);
332 gfs2_holder_uninit(ghs + 1);
335 gfs2_holder_uninit(ghs);
336 gfs2_glock_dq_uninit(&ri_gh);
341 * gfs2_symlink - Create a symlink
342 * @dir: The directory to create the symlink in
343 * @dentry: The dentry to put the symlink in
344 * @symname: The thing which the link points to
349 static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
352 struct gfs2_inode *dip = GFS2_I(dir), *ip;
353 struct gfs2_sbd *sdp = GFS2_SB(dir);
354 struct gfs2_holder ghs[2];
356 struct buffer_head *dibh;
360 /* Must be stuffed with a null terminator for gfs2_follow_link() */
361 size = strlen(symname);
362 if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode) - 1)
363 return -ENAMETOOLONG;
365 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
367 inode = gfs2_createi(ghs, &dentry->d_name, S_IFLNK | S_IRWXUGO, 0);
369 gfs2_holder_uninit(ghs);
370 return PTR_ERR(inode);
373 ip = ghs[1].gh_gl->gl_object;
375 ip->i_di.di_size = size;
377 error = gfs2_meta_inode_buffer(ip, &dibh);
379 if (!gfs2_assert_withdraw(sdp, !error)) {
380 gfs2_dinode_out(ip, dibh->b_data);
381 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
387 if (dip->i_alloc->al_rgd)
388 gfs2_inplace_release(dip);
389 gfs2_quota_unlock(dip);
392 gfs2_glock_dq_uninit_m(2, ghs);
394 d_instantiate(dentry, inode);
395 mark_inode_dirty(inode);
401 * gfs2_mkdir - Make a directory
402 * @dir: The parent directory of the new one
403 * @dentry: The dentry of the new directory
404 * @mode: The mode of the new directory
409 static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
411 struct gfs2_inode *dip = GFS2_I(dir), *ip;
412 struct gfs2_sbd *sdp = GFS2_SB(dir);
413 struct gfs2_holder ghs[2];
415 struct buffer_head *dibh;
418 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
420 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
422 gfs2_holder_uninit(ghs);
423 return PTR_ERR(inode);
426 ip = ghs[1].gh_gl->gl_object;
428 ip->i_inode.i_nlink = 2;
429 ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
430 ip->i_di.di_flags |= GFS2_DIF_JDATA;
431 ip->i_di.di_entries = 2;
433 error = gfs2_meta_inode_buffer(ip, &dibh);
435 if (!gfs2_assert_withdraw(sdp, !error)) {
436 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
437 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
440 gfs2_str2qstr(&str, ".");
441 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
442 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
443 dent->de_inum = di->di_num; /* already GFS2 endian */
444 dent->de_type = cpu_to_be16(DT_DIR);
445 di->di_entries = cpu_to_be32(1);
447 gfs2_str2qstr(&str, "..");
448 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
449 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
451 gfs2_inum_out(dip, dent);
452 dent->de_type = cpu_to_be16(DT_DIR);
454 gfs2_dinode_out(ip, di);
459 error = gfs2_change_nlink(dip, +1);
460 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
463 if (dip->i_alloc->al_rgd)
464 gfs2_inplace_release(dip);
465 gfs2_quota_unlock(dip);
468 gfs2_glock_dq_uninit_m(2, ghs);
470 d_instantiate(dentry, inode);
471 mark_inode_dirty(inode);
477 * gfs2_rmdir - Remove a directory
478 * @dir: The parent directory of the directory to be removed
479 * @dentry: The dentry of the directory to remove
481 * Remove a directory. Call gfs2_rmdiri()
486 static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
488 struct gfs2_inode *dip = GFS2_I(dir);
489 struct gfs2_sbd *sdp = GFS2_SB(dir);
490 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
491 struct gfs2_holder ghs[3];
492 struct gfs2_rgrpd *rgd;
493 struct gfs2_holder ri_gh;
496 error = gfs2_rindex_hold(sdp, &ri_gh);
499 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
500 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
502 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
503 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
505 error = gfs2_glock_nq(ghs); /* parent */
509 error = gfs2_glock_nq(ghs + 1); /* child */
513 error = gfs2_glock_nq(ghs + 2); /* rgrp */
517 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
521 if (ip->i_di.di_entries < 2) {
522 if (gfs2_consist_inode(ip))
523 gfs2_dinode_print(ip);
527 if (ip->i_di.di_entries > 2) {
532 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
536 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
541 gfs2_glock_dq(ghs + 2);
543 gfs2_holder_uninit(ghs + 2);
544 gfs2_glock_dq(ghs + 1);
546 gfs2_holder_uninit(ghs + 1);
549 gfs2_holder_uninit(ghs);
550 gfs2_glock_dq_uninit(&ri_gh);
555 * gfs2_mknod - Make a special file
556 * @dir: The directory in which the special file will reside
557 * @dentry: The dentry of the special file
558 * @mode: The mode of the special file
559 * @rdev: The device specification of the special file
563 static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
566 struct gfs2_inode *dip = GFS2_I(dir);
567 struct gfs2_sbd *sdp = GFS2_SB(dir);
568 struct gfs2_holder ghs[2];
571 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
573 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
575 gfs2_holder_uninit(ghs);
576 return PTR_ERR(inode);
580 if (dip->i_alloc->al_rgd)
581 gfs2_inplace_release(dip);
582 gfs2_quota_unlock(dip);
585 gfs2_glock_dq_uninit_m(2, ghs);
587 d_instantiate(dentry, inode);
588 mark_inode_dirty(inode);
594 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
598 * Follow @to back to the root and make sure we don't encounter @this
599 * Assumes we already hold the rename lock.
604 static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
606 struct inode *dir = &to->i_inode;
607 struct super_block *sb = dir->i_sb;
612 gfs2_str2qstr(&dotdot, "..");
617 if (dir == &this->i_inode) {
621 if (dir == sb->s_root->d_inode) {
626 tmp = gfs2_lookupi(dir, &dotdot, 1);
628 error = PTR_ERR(tmp);
642 * gfs2_rename - Rename a file
643 * @odir: Parent directory of old file name
644 * @odentry: The old dentry of the file
645 * @ndir: Parent directory of new file name
646 * @ndentry: The new dentry of the file
651 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
652 struct inode *ndir, struct dentry *ndentry)
654 struct gfs2_inode *odip = GFS2_I(odir);
655 struct gfs2_inode *ndip = GFS2_I(ndir);
656 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
657 struct gfs2_inode *nip = NULL;
658 struct gfs2_sbd *sdp = GFS2_SB(odir);
659 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
660 struct gfs2_rgrpd *nrgd;
667 if (ndentry->d_inode) {
668 nip = GFS2_I(ndentry->d_inode);
675 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
680 if (S_ISDIR(ip->i_inode.i_mode)) {
682 /* don't move a dirctory into it's subdir */
683 error = gfs2_ok_to_move(ip, ndip);
690 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
692 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
695 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
699 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
701 /* grab the resource lock for unlink flag twiddling
702 * this is the case of the target file already existing
703 * so we unlink before doing the rename
705 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
707 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
710 for (x = 0; x < num_gh; x++) {
711 error = gfs2_glock_nq(ghs + x);
716 /* Check out the old directory */
718 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
722 /* Check out the new directory */
725 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
729 if (S_ISDIR(nip->i_inode.i_mode)) {
730 if (nip->i_di.di_entries < 2) {
731 if (gfs2_consist_inode(nip))
732 gfs2_dinode_print(nip);
736 if (nip->i_di.di_entries > 2) {
742 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
746 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
758 if (!ndip->i_inode.i_nlink) {
762 if (ndip->i_di.di_entries == (u32)-1) {
766 if (S_ISDIR(ip->i_inode.i_mode) &&
767 ndip->i_inode.i_nlink == (u32)-1) {
774 /* Check out the dir to be renamed */
777 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
782 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
787 if (alloc_required) {
788 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
794 error = gfs2_quota_lock_check(ndip);
798 al->al_requested = sdp->sd_max_dirres;
800 error = gfs2_inplace_reserve(ndip);
804 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
805 al->al_rgd->rd_length +
806 4 * RES_DINODE + 4 * RES_LEAF +
807 RES_STATFS + RES_QUOTA + 4, 0);
811 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
812 5 * RES_LEAF + 4, 0);
817 /* Remove the target file, if it exists */
820 if (S_ISDIR(nip->i_inode.i_mode))
821 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
823 error = gfs2_dir_del(ndip, &ndentry->d_name);
826 error = gfs2_change_nlink(nip, -1);
834 gfs2_str2qstr(&name, "..");
836 error = gfs2_change_nlink(ndip, +1);
839 error = gfs2_change_nlink(odip, -1);
843 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
847 struct buffer_head *dibh;
848 error = gfs2_meta_inode_buffer(ip, &dibh);
851 ip->i_inode.i_ctime = CURRENT_TIME;
852 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
853 gfs2_dinode_out(ip, dibh->b_data);
857 error = gfs2_dir_del(odip, &odentry->d_name);
861 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
869 gfs2_inplace_release(ndip);
872 gfs2_quota_unlock(ndip);
875 gfs2_alloc_put(ndip);
878 gfs2_glock_dq(ghs + x);
879 gfs2_holder_uninit(ghs + x);
883 gfs2_glock_dq_uninit(&r_gh);
889 * gfs2_readlink - Read the value of a symlink
890 * @dentry: the symlink
891 * @buf: the buffer to read the symlink data into
892 * @size: the size of the buffer
897 static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
900 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
901 char array[GFS2_FAST_NAME_SIZE], *buf = array;
902 unsigned int len = GFS2_FAST_NAME_SIZE;
905 error = gfs2_readlinki(ip, &buf, &len);
909 if (user_size > len - 1)
912 if (copy_to_user(user_buf, buf, user_size))
924 * gfs2_follow_link - Follow a symbolic link
925 * @dentry: The dentry of the link
926 * @nd: Data that we pass to vfs_follow_link()
928 * This can handle symlinks of any size. It is optimised for symlinks
929 * under GFS2_FAST_NAME_SIZE.
931 * Returns: 0 on success or error code
934 static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
936 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
937 char array[GFS2_FAST_NAME_SIZE], *buf = array;
938 unsigned int len = GFS2_FAST_NAME_SIZE;
941 error = gfs2_readlinki(ip, &buf, &len);
943 error = vfs_follow_link(nd, buf);
948 return ERR_PTR(error);
955 * @nd: passed from Linux VFS, ignored by us
957 * This may be called from the VFS directly, or from within GFS2 with the
958 * inode locked, so we look to see if the glock is already locked and only
959 * lock the glock if its not already been done.
964 int gfs2_permission(struct inode *inode, int mask)
966 struct gfs2_inode *ip = GFS2_I(inode);
967 struct gfs2_holder i_gh;
971 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
972 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
978 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
981 error = generic_permission(inode, mask, gfs2_check_acl);
983 gfs2_glock_dq_uninit(&i_gh);
988 static int setattr_size(struct inode *inode, struct iattr *attr)
990 struct gfs2_inode *ip = GFS2_I(inode);
991 struct gfs2_sbd *sdp = GFS2_SB(inode);
994 if (attr->ia_size != ip->i_di.di_size) {
995 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
998 error = vmtruncate(inode, attr->ia_size);
1004 error = gfs2_truncatei(ip, attr->ia_size);
1005 if (error && (inode->i_size != ip->i_di.di_size))
1006 i_size_write(inode, ip->i_di.di_size);
1011 static int setattr_chown(struct inode *inode, struct iattr *attr)
1013 struct gfs2_inode *ip = GFS2_I(inode);
1014 struct gfs2_sbd *sdp = GFS2_SB(inode);
1015 struct buffer_head *dibh;
1016 u32 ouid, ogid, nuid, ngid;
1019 ouid = inode->i_uid;
1020 ogid = inode->i_gid;
1021 nuid = attr->ia_uid;
1022 ngid = attr->ia_gid;
1024 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1025 ouid = nuid = NO_QUOTA_CHANGE;
1026 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1027 ogid = ngid = NO_QUOTA_CHANGE;
1029 if (!gfs2_alloc_get(ip))
1032 error = gfs2_quota_lock(ip, nuid, ngid);
1036 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1037 error = gfs2_quota_check(ip, nuid, ngid);
1042 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1046 error = gfs2_meta_inode_buffer(ip, &dibh);
1050 error = inode_setattr(inode, attr);
1051 gfs2_assert_warn(sdp, !error);
1053 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1054 gfs2_dinode_out(ip, dibh->b_data);
1057 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1058 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1059 gfs2_quota_change(ip, -blocks, ouid, ogid);
1060 gfs2_quota_change(ip, blocks, nuid, ngid);
1064 gfs2_trans_end(sdp);
1066 gfs2_quota_unlock(ip);
1073 * gfs2_setattr - Change attributes on an inode
1074 * @dentry: The dentry which is changing
1075 * @attr: The structure describing the change
1077 * The VFS layer wants to change one or more of an inodes attributes. Write
1078 * that change out to disk.
1083 static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1085 struct inode *inode = dentry->d_inode;
1086 struct gfs2_inode *ip = GFS2_I(inode);
1087 struct gfs2_holder i_gh;
1090 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1095 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1098 error = inode_change_ok(inode, attr);
1102 if (attr->ia_valid & ATTR_SIZE)
1103 error = setattr_size(inode, attr);
1104 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1105 error = setattr_chown(inode, attr);
1106 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1107 error = gfs2_acl_chmod(ip, attr);
1109 error = gfs2_setattr_simple(ip, attr);
1112 gfs2_glock_dq_uninit(&i_gh);
1114 mark_inode_dirty(inode);
1119 * gfs2_getattr - Read out an inode's attributes
1120 * @mnt: The vfsmount the inode is being accessed from
1121 * @dentry: The dentry to stat
1122 * @stat: The inode's stats
1124 * This may be called from the VFS directly, or from within GFS2 with the
1125 * inode locked, so we look to see if the glock is already locked and only
1126 * lock the glock if its not already been done. Note that its the NFS
1127 * readdirplus operation which causes this to be called (from filldir)
1128 * with the glock already held.
1133 static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1136 struct inode *inode = dentry->d_inode;
1137 struct gfs2_inode *ip = GFS2_I(inode);
1138 struct gfs2_holder gh;
1142 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1143 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1149 generic_fillattr(inode, stat);
1151 gfs2_glock_dq_uninit(&gh);
1156 static int gfs2_setxattr(struct dentry *dentry, const char *name,
1157 const void *data, size_t size, int flags)
1159 struct inode *inode = dentry->d_inode;
1160 struct gfs2_ea_request er;
1162 memset(&er, 0, sizeof(struct gfs2_ea_request));
1163 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1164 if (er.er_type == GFS2_EATYPE_UNUSED)
1166 er.er_data = (char *)data;
1167 er.er_name_len = strlen(er.er_name);
1168 er.er_data_len = size;
1169 er.er_flags = flags;
1171 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
1173 return gfs2_ea_set(GFS2_I(inode), &er);
1176 static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1177 void *data, size_t size)
1179 struct gfs2_ea_request er;
1181 memset(&er, 0, sizeof(struct gfs2_ea_request));
1182 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1183 if (er.er_type == GFS2_EATYPE_UNUSED)
1186 er.er_name_len = strlen(er.er_name);
1187 er.er_data_len = size;
1189 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
1192 static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1194 struct gfs2_ea_request er;
1196 memset(&er, 0, sizeof(struct gfs2_ea_request));
1197 er.er_data = (size) ? buffer : NULL;
1198 er.er_data_len = size;
1200 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
1203 static int gfs2_removexattr(struct dentry *dentry, const char *name)
1205 struct gfs2_ea_request er;
1207 memset(&er, 0, sizeof(struct gfs2_ea_request));
1208 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1209 if (er.er_type == GFS2_EATYPE_UNUSED)
1211 er.er_name_len = strlen(er.er_name);
1213 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
1216 static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1219 struct gfs2_inode *ip = GFS2_I(inode);
1220 struct gfs2_holder gh;
1223 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1227 mutex_lock(&inode->i_mutex);
1229 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1233 if (gfs2_is_stuffed(ip)) {
1234 u64 phys = ip->i_no_addr << inode->i_blkbits;
1235 u64 size = i_size_read(inode);
1236 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1237 FIEMAP_EXTENT_DATA_INLINE;
1238 phys += sizeof(struct gfs2_dinode);
1240 if (start + len > size)
1243 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1248 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1252 gfs2_glock_dq_uninit(&gh);
1254 mutex_unlock(&inode->i_mutex);
1258 const struct inode_operations gfs2_file_iops = {
1259 .permission = gfs2_permission,
1260 .setattr = gfs2_setattr,
1261 .getattr = gfs2_getattr,
1262 .setxattr = gfs2_setxattr,
1263 .getxattr = gfs2_getxattr,
1264 .listxattr = gfs2_listxattr,
1265 .removexattr = gfs2_removexattr,
1266 .fiemap = gfs2_fiemap,
1269 const struct inode_operations gfs2_dir_iops = {
1270 .create = gfs2_create,
1271 .lookup = gfs2_lookup,
1273 .unlink = gfs2_unlink,
1274 .symlink = gfs2_symlink,
1275 .mkdir = gfs2_mkdir,
1276 .rmdir = gfs2_rmdir,
1277 .mknod = gfs2_mknod,
1278 .rename = gfs2_rename,
1279 .permission = gfs2_permission,
1280 .setattr = gfs2_setattr,
1281 .getattr = gfs2_getattr,
1282 .setxattr = gfs2_setxattr,
1283 .getxattr = gfs2_getxattr,
1284 .listxattr = gfs2_listxattr,
1285 .removexattr = gfs2_removexattr,
1286 .fiemap = gfs2_fiemap,
1289 const struct inode_operations gfs2_symlink_iops = {
1290 .readlink = gfs2_readlink,
1291 .follow_link = gfs2_follow_link,
1292 .permission = gfs2_permission,
1293 .setattr = gfs2_setattr,
1294 .getattr = gfs2_getattr,
1295 .setxattr = gfs2_setxattr,
1296 .getxattr = gfs2_getxattr,
1297 .listxattr = gfs2_listxattr,
1298 .removexattr = gfs2_removexattr,
1299 .fiemap = gfs2_fiemap,