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/fiemap.h>
22 #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 ip->i_disksize = size;
412 i_size_write(inode, size);
414 error = gfs2_meta_inode_buffer(ip, &dibh);
416 if (!gfs2_assert_withdraw(sdp, !error)) {
417 gfs2_dinode_out(ip, dibh->b_data);
418 memcpy(dibh->b_data + sizeof(struct gfs2_dinode), symname,
424 if (dip->i_alloc->al_rgd)
425 gfs2_inplace_release(dip);
426 gfs2_quota_unlock(dip);
429 gfs2_glock_dq_uninit_m(2, ghs);
431 d_instantiate(dentry, inode);
432 mark_inode_dirty(inode);
438 * gfs2_mkdir - Make a directory
439 * @dir: The parent directory of the new one
440 * @dentry: The dentry of the new directory
441 * @mode: The mode of the new directory
446 static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
448 struct gfs2_inode *dip = GFS2_I(dir), *ip;
449 struct gfs2_sbd *sdp = GFS2_SB(dir);
450 struct gfs2_holder ghs[2];
452 struct buffer_head *dibh;
455 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
457 inode = gfs2_createi(ghs, &dentry->d_name, S_IFDIR | mode, 0);
459 gfs2_holder_uninit(ghs);
460 return PTR_ERR(inode);
463 ip = ghs[1].gh_gl->gl_object;
465 ip->i_inode.i_nlink = 2;
466 ip->i_disksize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
467 ip->i_diskflags |= GFS2_DIF_JDATA;
470 error = gfs2_meta_inode_buffer(ip, &dibh);
472 if (!gfs2_assert_withdraw(sdp, !error)) {
473 struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
474 struct gfs2_dirent *dent = (struct gfs2_dirent *)(di+1);
477 gfs2_str2qstr(&str, ".");
478 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
479 gfs2_qstr2dirent(&str, GFS2_DIRENT_SIZE(str.len), dent);
480 dent->de_inum = di->di_num; /* already GFS2 endian */
481 dent->de_type = cpu_to_be16(DT_DIR);
482 di->di_entries = cpu_to_be32(1);
484 gfs2_str2qstr(&str, "..");
485 dent = (struct gfs2_dirent *)((char*)dent + GFS2_DIRENT_SIZE(1));
486 gfs2_qstr2dirent(&str, dibh->b_size - GFS2_DIRENT_SIZE(1) - sizeof(struct gfs2_dinode), dent);
488 gfs2_inum_out(dip, dent);
489 dent->de_type = cpu_to_be16(DT_DIR);
491 gfs2_dinode_out(ip, di);
496 error = gfs2_change_nlink(dip, +1);
497 gfs2_assert_withdraw(sdp, !error); /* dip already pinned */
500 if (dip->i_alloc->al_rgd)
501 gfs2_inplace_release(dip);
502 gfs2_quota_unlock(dip);
505 gfs2_glock_dq_uninit_m(2, ghs);
507 d_instantiate(dentry, inode);
508 mark_inode_dirty(inode);
514 * gfs2_rmdiri - Remove a directory
515 * @dip: The parent directory of the directory to be removed
516 * @name: The name of the directory to be removed
517 * @ip: The GFS2 inode of the directory to be removed
519 * Assumes Glocks on dip and ip are held
524 static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
525 struct gfs2_inode *ip)
530 if (ip->i_entries != 2) {
531 if (gfs2_consist_inode(ip))
532 gfs2_dinode_print(ip);
536 error = gfs2_dir_del(dip, name);
540 error = gfs2_change_nlink(dip, -1);
544 gfs2_str2qstr(&dotname, ".");
545 error = gfs2_dir_del(ip, &dotname);
549 gfs2_str2qstr(&dotname, "..");
550 error = gfs2_dir_del(ip, &dotname);
554 /* It looks odd, but it really should be done twice */
555 error = gfs2_change_nlink(ip, -1);
559 error = gfs2_change_nlink(ip, -1);
567 * gfs2_rmdir - Remove a directory
568 * @dir: The parent directory of the directory to be removed
569 * @dentry: The dentry of the directory to remove
571 * Remove a directory. Call gfs2_rmdiri()
576 static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
578 struct gfs2_inode *dip = GFS2_I(dir);
579 struct gfs2_sbd *sdp = GFS2_SB(dir);
580 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
581 struct gfs2_holder ghs[3];
582 struct gfs2_rgrpd *rgd;
583 struct gfs2_holder ri_gh;
586 error = gfs2_rindex_hold(sdp, &ri_gh);
589 gfs2_holder_init(dip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
590 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + 1);
592 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr);
593 gfs2_holder_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + 2);
595 error = gfs2_glock_nq(ghs); /* parent */
599 error = gfs2_glock_nq(ghs + 1); /* child */
603 error = gfs2_glock_nq(ghs + 2); /* rgrp */
607 error = gfs2_unlink_ok(dip, &dentry->d_name, ip);
611 if (ip->i_entries < 2) {
612 if (gfs2_consist_inode(ip))
613 gfs2_dinode_print(ip);
617 if (ip->i_entries > 2) {
622 error = gfs2_trans_begin(sdp, 2 * RES_DINODE + 3 * RES_LEAF + RES_RG_BIT, 0);
626 error = gfs2_rmdiri(dip, &dentry->d_name, ip);
631 gfs2_glock_dq(ghs + 2);
633 gfs2_holder_uninit(ghs + 2);
634 gfs2_glock_dq(ghs + 1);
636 gfs2_holder_uninit(ghs + 1);
639 gfs2_holder_uninit(ghs);
640 gfs2_glock_dq_uninit(&ri_gh);
645 * gfs2_mknod - Make a special file
646 * @dir: The directory in which the special file will reside
647 * @dentry: The dentry of the special file
648 * @mode: The mode of the special file
649 * @rdev: The device specification of the special file
653 static int gfs2_mknod(struct inode *dir, struct dentry *dentry, int mode,
656 struct gfs2_inode *dip = GFS2_I(dir);
657 struct gfs2_sbd *sdp = GFS2_SB(dir);
658 struct gfs2_holder ghs[2];
661 gfs2_holder_init(dip->i_gl, 0, 0, ghs);
663 inode = gfs2_createi(ghs, &dentry->d_name, mode, dev);
665 gfs2_holder_uninit(ghs);
666 return PTR_ERR(inode);
670 if (dip->i_alloc->al_rgd)
671 gfs2_inplace_release(dip);
672 gfs2_quota_unlock(dip);
675 gfs2_glock_dq_uninit_m(2, ghs);
677 d_instantiate(dentry, inode);
678 mark_inode_dirty(inode);
684 * gfs2_ok_to_move - check if it's ok to move a directory to another directory
688 * Follow @to back to the root and make sure we don't encounter @this
689 * Assumes we already hold the rename lock.
694 static int gfs2_ok_to_move(struct gfs2_inode *this, struct gfs2_inode *to)
696 struct inode *dir = &to->i_inode;
697 struct super_block *sb = dir->i_sb;
702 gfs2_str2qstr(&dotdot, "..");
707 if (dir == &this->i_inode) {
711 if (dir == sb->s_root->d_inode) {
716 tmp = gfs2_lookupi(dir, &dotdot, 1);
718 error = PTR_ERR(tmp);
732 * gfs2_rename - Rename a file
733 * @odir: Parent directory of old file name
734 * @odentry: The old dentry of the file
735 * @ndir: Parent directory of new file name
736 * @ndentry: The new dentry of the file
741 static int gfs2_rename(struct inode *odir, struct dentry *odentry,
742 struct inode *ndir, struct dentry *ndentry)
744 struct gfs2_inode *odip = GFS2_I(odir);
745 struct gfs2_inode *ndip = GFS2_I(ndir);
746 struct gfs2_inode *ip = GFS2_I(odentry->d_inode);
747 struct gfs2_inode *nip = NULL;
748 struct gfs2_sbd *sdp = GFS2_SB(odir);
749 struct gfs2_holder ghs[5], r_gh = { .gh_gl = NULL, };
750 struct gfs2_rgrpd *nrgd;
757 if (ndentry->d_inode) {
758 nip = GFS2_I(ndentry->d_inode);
765 error = gfs2_glock_nq_init(sdp->sd_rename_gl, LM_ST_EXCLUSIVE,
770 if (S_ISDIR(ip->i_inode.i_mode)) {
772 /* don't move a dirctory into it's subdir */
773 error = gfs2_ok_to_move(ip, ndip);
780 gfs2_holder_init(odip->i_gl, LM_ST_EXCLUSIVE, 0, ghs);
782 gfs2_holder_init(ndip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
785 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
789 gfs2_holder_init(nip->i_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh);
791 /* grab the resource lock for unlink flag twiddling
792 * this is the case of the target file already existing
793 * so we unlink before doing the rename
795 nrgd = gfs2_blk2rgrpd(sdp, nip->i_no_addr);
797 gfs2_holder_init(nrgd->rd_gl, LM_ST_EXCLUSIVE, 0, ghs + num_gh++);
800 for (x = 0; x < num_gh; x++) {
801 error = gfs2_glock_nq(ghs + x);
806 /* Check out the old directory */
808 error = gfs2_unlink_ok(odip, &odentry->d_name, ip);
812 /* Check out the new directory */
815 error = gfs2_unlink_ok(ndip, &ndentry->d_name, nip);
819 if (S_ISDIR(nip->i_inode.i_mode)) {
820 if (nip->i_entries < 2) {
821 if (gfs2_consist_inode(nip))
822 gfs2_dinode_print(nip);
826 if (nip->i_entries > 2) {
832 error = gfs2_permission(ndir, MAY_WRITE | MAY_EXEC);
836 error = gfs2_dir_check(ndir, &ndentry->d_name, NULL);
848 if (!ndip->i_inode.i_nlink) {
852 if (ndip->i_entries == (u32)-1) {
856 if (S_ISDIR(ip->i_inode.i_mode) &&
857 ndip->i_inode.i_nlink == (u32)-1) {
864 /* Check out the dir to be renamed */
867 error = gfs2_permission(odentry->d_inode, MAY_WRITE);
872 alloc_required = error = gfs2_diradd_alloc_required(ndir, &ndentry->d_name);
877 if (alloc_required) {
878 struct gfs2_alloc *al = gfs2_alloc_get(ndip);
884 error = gfs2_quota_lock_check(ndip);
888 al->al_requested = sdp->sd_max_dirres;
890 error = gfs2_inplace_reserve(ndip);
894 error = gfs2_trans_begin(sdp, sdp->sd_max_dirres +
895 al->al_rgd->rd_length +
896 4 * RES_DINODE + 4 * RES_LEAF +
897 RES_STATFS + RES_QUOTA + 4, 0);
901 error = gfs2_trans_begin(sdp, 4 * RES_DINODE +
902 5 * RES_LEAF + 4, 0);
907 /* Remove the target file, if it exists */
910 if (S_ISDIR(nip->i_inode.i_mode))
911 error = gfs2_rmdiri(ndip, &ndentry->d_name, nip);
913 error = gfs2_dir_del(ndip, &ndentry->d_name);
916 error = gfs2_change_nlink(nip, -1);
924 gfs2_str2qstr(&name, "..");
926 error = gfs2_change_nlink(ndip, +1);
929 error = gfs2_change_nlink(odip, -1);
933 error = gfs2_dir_mvino(ip, &name, ndip, DT_DIR);
937 struct buffer_head *dibh;
938 error = gfs2_meta_inode_buffer(ip, &dibh);
941 ip->i_inode.i_ctime = CURRENT_TIME;
942 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
943 gfs2_dinode_out(ip, dibh->b_data);
947 error = gfs2_dir_del(odip, &odentry->d_name);
951 error = gfs2_dir_add(ndir, &ndentry->d_name, ip, IF2DT(ip->i_inode.i_mode));
959 gfs2_inplace_release(ndip);
962 gfs2_quota_unlock(ndip);
965 gfs2_alloc_put(ndip);
968 gfs2_glock_dq(ghs + x);
969 gfs2_holder_uninit(ghs + x);
973 gfs2_glock_dq_uninit(&r_gh);
979 * gfs2_readlinki - return the contents of a symlink
980 * @ip: the symlink's inode
981 * @buf: a pointer to the buffer to be filled
982 * @len: a pointer to the length of @buf
984 * If @buf is too small, a piece of memory is kmalloc()ed and needs
985 * to be freed by the caller.
990 static int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
992 struct gfs2_holder i_gh;
993 struct buffer_head *dibh;
997 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
998 error = gfs2_glock_nq(&i_gh);
1000 gfs2_holder_uninit(&i_gh);
1004 if (!ip->i_disksize) {
1005 gfs2_consist_inode(ip);
1010 error = gfs2_meta_inode_buffer(ip, &dibh);
1014 x = ip->i_disksize + 1;
1016 *buf = kmalloc(x, GFP_NOFS);
1023 memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
1029 gfs2_glock_dq_uninit(&i_gh);
1034 * gfs2_readlink - Read the value of a symlink
1035 * @dentry: the symlink
1036 * @buf: the buffer to read the symlink data into
1037 * @size: the size of the buffer
1042 static int gfs2_readlink(struct dentry *dentry, char __user *user_buf,
1045 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
1046 char array[GFS2_FAST_NAME_SIZE], *buf = array;
1047 unsigned int len = GFS2_FAST_NAME_SIZE;
1050 error = gfs2_readlinki(ip, &buf, &len);
1054 if (user_size > len - 1)
1055 user_size = len - 1;
1057 if (copy_to_user(user_buf, buf, user_size))
1069 * gfs2_follow_link - Follow a symbolic link
1070 * @dentry: The dentry of the link
1071 * @nd: Data that we pass to vfs_follow_link()
1073 * This can handle symlinks of any size. It is optimised for symlinks
1074 * under GFS2_FAST_NAME_SIZE.
1076 * Returns: 0 on success or error code
1079 static void *gfs2_follow_link(struct dentry *dentry, struct nameidata *nd)
1081 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
1082 char array[GFS2_FAST_NAME_SIZE], *buf = array;
1083 unsigned int len = GFS2_FAST_NAME_SIZE;
1086 error = gfs2_readlinki(ip, &buf, &len);
1088 error = vfs_follow_link(nd, buf);
1093 return ERR_PTR(error);
1100 * @nd: passed from Linux VFS, ignored by us
1102 * This may be called from the VFS directly, or from within GFS2 with the
1103 * inode locked, so we look to see if the glock is already locked and only
1104 * lock the glock if its not already been done.
1109 int gfs2_permission(struct inode *inode, int mask)
1111 struct gfs2_inode *ip = GFS2_I(inode);
1112 struct gfs2_holder i_gh;
1116 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1117 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
1123 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1126 error = generic_permission(inode, mask, gfs2_check_acl);
1128 gfs2_glock_dq_uninit(&i_gh);
1133 static int setattr_size(struct inode *inode, struct iattr *attr)
1135 struct gfs2_inode *ip = GFS2_I(inode);
1136 struct gfs2_sbd *sdp = GFS2_SB(inode);
1139 if (attr->ia_size != ip->i_disksize) {
1140 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
1143 error = vmtruncate(inode, attr->ia_size);
1144 gfs2_trans_end(sdp);
1149 error = gfs2_truncatei(ip, attr->ia_size);
1150 if (error && (inode->i_size != ip->i_disksize))
1151 i_size_write(inode, ip->i_disksize);
1156 static int setattr_chown(struct inode *inode, struct iattr *attr)
1158 struct gfs2_inode *ip = GFS2_I(inode);
1159 struct gfs2_sbd *sdp = GFS2_SB(inode);
1160 struct buffer_head *dibh;
1161 u32 ouid, ogid, nuid, ngid;
1164 ouid = inode->i_uid;
1165 ogid = inode->i_gid;
1166 nuid = attr->ia_uid;
1167 ngid = attr->ia_gid;
1169 if (!(attr->ia_valid & ATTR_UID) || ouid == nuid)
1170 ouid = nuid = NO_QUOTA_CHANGE;
1171 if (!(attr->ia_valid & ATTR_GID) || ogid == ngid)
1172 ogid = ngid = NO_QUOTA_CHANGE;
1174 if (!gfs2_alloc_get(ip))
1177 error = gfs2_quota_lock(ip, nuid, ngid);
1181 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1182 error = gfs2_quota_check(ip, nuid, ngid);
1187 error = gfs2_trans_begin(sdp, RES_DINODE + 2 * RES_QUOTA, 0);
1191 error = gfs2_meta_inode_buffer(ip, &dibh);
1195 error = inode_setattr(inode, attr);
1196 gfs2_assert_warn(sdp, !error);
1198 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
1199 gfs2_dinode_out(ip, dibh->b_data);
1202 if (ouid != NO_QUOTA_CHANGE || ogid != NO_QUOTA_CHANGE) {
1203 u64 blocks = gfs2_get_inode_blocks(&ip->i_inode);
1204 gfs2_quota_change(ip, -blocks, ouid, ogid);
1205 gfs2_quota_change(ip, blocks, nuid, ngid);
1209 gfs2_trans_end(sdp);
1211 gfs2_quota_unlock(ip);
1218 * gfs2_setattr - Change attributes on an inode
1219 * @dentry: The dentry which is changing
1220 * @attr: The structure describing the change
1222 * The VFS layer wants to change one or more of an inodes attributes. Write
1223 * that change out to disk.
1228 static int gfs2_setattr(struct dentry *dentry, struct iattr *attr)
1230 struct inode *inode = dentry->d_inode;
1231 struct gfs2_inode *ip = GFS2_I(inode);
1232 struct gfs2_holder i_gh;
1235 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &i_gh);
1240 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1243 error = inode_change_ok(inode, attr);
1247 if (attr->ia_valid & ATTR_SIZE)
1248 error = setattr_size(inode, attr);
1249 else if (attr->ia_valid & (ATTR_UID | ATTR_GID))
1250 error = setattr_chown(inode, attr);
1251 else if ((attr->ia_valid & ATTR_MODE) && IS_POSIXACL(inode))
1252 error = gfs2_acl_chmod(ip, attr);
1254 error = gfs2_setattr_simple(ip, attr);
1257 gfs2_glock_dq_uninit(&i_gh);
1259 mark_inode_dirty(inode);
1264 * gfs2_getattr - Read out an inode's attributes
1265 * @mnt: The vfsmount the inode is being accessed from
1266 * @dentry: The dentry to stat
1267 * @stat: The inode's stats
1269 * This may be called from the VFS directly, or from within GFS2 with the
1270 * inode locked, so we look to see if the glock is already locked and only
1271 * lock the glock if its not already been done. Note that its the NFS
1272 * readdirplus operation which causes this to be called (from filldir)
1273 * with the glock already held.
1278 static int gfs2_getattr(struct vfsmount *mnt, struct dentry *dentry,
1281 struct inode *inode = dentry->d_inode;
1282 struct gfs2_inode *ip = GFS2_I(inode);
1283 struct gfs2_holder gh;
1287 if (gfs2_glock_is_locked_by_me(ip->i_gl) == NULL) {
1288 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
1294 generic_fillattr(inode, stat);
1296 gfs2_glock_dq_uninit(&gh);
1301 static int gfs2_setxattr(struct dentry *dentry, const char *name,
1302 const void *data, size_t size, int flags)
1304 struct inode *inode = dentry->d_inode;
1305 struct gfs2_ea_request er;
1307 memset(&er, 0, sizeof(struct gfs2_ea_request));
1308 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1309 if (er.er_type == GFS2_EATYPE_UNUSED)
1311 er.er_data = (char *)data;
1312 er.er_name_len = strlen(er.er_name);
1313 er.er_data_len = size;
1314 er.er_flags = flags;
1316 gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
1318 return gfs2_ea_set(GFS2_I(inode), &er);
1321 static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
1322 void *data, size_t size)
1324 struct gfs2_ea_request er;
1326 memset(&er, 0, sizeof(struct gfs2_ea_request));
1327 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1328 if (er.er_type == GFS2_EATYPE_UNUSED)
1331 er.er_name_len = strlen(er.er_name);
1332 er.er_data_len = size;
1334 return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
1337 static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
1339 struct gfs2_ea_request er;
1341 memset(&er, 0, sizeof(struct gfs2_ea_request));
1342 er.er_data = (size) ? buffer : NULL;
1343 er.er_data_len = size;
1345 return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
1348 static int gfs2_removexattr(struct dentry *dentry, const char *name)
1350 struct gfs2_ea_request er;
1352 memset(&er, 0, sizeof(struct gfs2_ea_request));
1353 er.er_type = gfs2_ea_name2type(name, &er.er_name);
1354 if (er.er_type == GFS2_EATYPE_UNUSED)
1356 er.er_name_len = strlen(er.er_name);
1358 return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
1361 static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1364 struct gfs2_inode *ip = GFS2_I(inode);
1365 struct gfs2_holder gh;
1368 ret = fiemap_check_flags(fieinfo, FIEMAP_FLAG_SYNC);
1372 mutex_lock(&inode->i_mutex);
1374 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
1378 if (gfs2_is_stuffed(ip)) {
1379 u64 phys = ip->i_no_addr << inode->i_blkbits;
1380 u64 size = i_size_read(inode);
1381 u32 flags = FIEMAP_EXTENT_LAST|FIEMAP_EXTENT_NOT_ALIGNED|
1382 FIEMAP_EXTENT_DATA_INLINE;
1383 phys += sizeof(struct gfs2_dinode);
1385 if (start + len > size)
1388 ret = fiemap_fill_next_extent(fieinfo, start, phys,
1393 ret = __generic_block_fiemap(inode, fieinfo, start, len,
1397 gfs2_glock_dq_uninit(&gh);
1399 mutex_unlock(&inode->i_mutex);
1403 const struct inode_operations gfs2_file_iops = {
1404 .permission = gfs2_permission,
1405 .setattr = gfs2_setattr,
1406 .getattr = gfs2_getattr,
1407 .setxattr = gfs2_setxattr,
1408 .getxattr = gfs2_getxattr,
1409 .listxattr = gfs2_listxattr,
1410 .removexattr = gfs2_removexattr,
1411 .fiemap = gfs2_fiemap,
1414 const struct inode_operations gfs2_dir_iops = {
1415 .create = gfs2_create,
1416 .lookup = gfs2_lookup,
1418 .unlink = gfs2_unlink,
1419 .symlink = gfs2_symlink,
1420 .mkdir = gfs2_mkdir,
1421 .rmdir = gfs2_rmdir,
1422 .mknod = gfs2_mknod,
1423 .rename = gfs2_rename,
1424 .permission = gfs2_permission,
1425 .setattr = gfs2_setattr,
1426 .getattr = gfs2_getattr,
1427 .setxattr = gfs2_setxattr,
1428 .getxattr = gfs2_getxattr,
1429 .listxattr = gfs2_listxattr,
1430 .removexattr = gfs2_removexattr,
1431 .fiemap = gfs2_fiemap,
1434 const struct inode_operations gfs2_symlink_iops = {
1435 .readlink = gfs2_readlink,
1436 .follow_link = gfs2_follow_link,
1437 .permission = gfs2_permission,
1438 .setattr = gfs2_setattr,
1439 .getattr = gfs2_getattr,
1440 .setxattr = gfs2_setxattr,
1441 .getxattr = gfs2_getxattr,
1442 .listxattr = gfs2_listxattr,
1443 .removexattr = gfs2_removexattr,
1444 .fiemap = gfs2_fiemap,