libfs: return error code on failed attr set
[safe/jmp/linux-2.6] / fs / gfs2 / ops_inode.c
index 1e24b65..247436c 100644 (file)
 #include <linux/completion.h>
 #include <linux/buffer_head.h>
 #include <linux/namei.h>
-#include <linux/utsname.h>
 #include <linux/mm.h>
 #include <linux/xattr.h>
 #include <linux/posix_acl.h>
 #include <linux/gfs2_ondisk.h>
 #include <linux/crc32.h>
-#include <linux/lm_interface.h>
 #include <linux/fiemap.h>
 #include <asm/uaccess.h>
 
 #include "acl.h"
 #include "bmap.h"
 #include "dir.h"
-#include "eaops.h"
-#include "eattr.h"
+#include "xattr.h"
 #include "glock.h"
 #include "inode.h"
 #include "meta_io.h"
-#include "ops_dentry.h"
-#include "ops_inode.h"
 #include "quota.h"
 #include "rgrp.h"
 #include "trans.h"
 #include "util.h"
+#include "super.h"
 
 /**
  * gfs2_create - Create a file
@@ -186,7 +182,7 @@ static int gfs2_link(struct dentry *old_dentry, struct inode *dir,
        if (!dip->i_inode.i_nlink)
                goto out_gunlock;
        error = -EFBIG;
-       if (dip->i_di.di_entries == (u32)-1)
+       if (dip->i_entries == (u32)-1)
                goto out_gunlock;
        error = -EPERM;
        if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
@@ -264,6 +260,44 @@ out_parent:
        return error;
 }
 
+/*
+ * gfs2_unlink_ok - check to see that a inode is still in a directory
+ * @dip: the directory
+ * @name: the name of the file
+ * @ip: the inode
+ *
+ * Assumes that the lock on (at least) @dip is held.
+ *
+ * Returns: 0 if the parent/child relationship is correct, errno if it isn't
+ */
+
+static int gfs2_unlink_ok(struct gfs2_inode *dip, const struct qstr *name,
+                         const struct gfs2_inode *ip)
+{
+       int error;
+
+       if (IS_IMMUTABLE(&ip->i_inode) || IS_APPEND(&ip->i_inode))
+               return -EPERM;
+
+       if ((dip->i_inode.i_mode & S_ISVTX) &&
+           dip->i_inode.i_uid != current_fsuid() &&
+           ip->i_inode.i_uid != current_fsuid() && !capable(CAP_FOWNER))
+               return -EPERM;
+
+       if (IS_APPEND(&dip->i_inode))
+               return -EPERM;
+
+       error = gfs2_permission(&dip->i_inode, MAY_WRITE | MAY_EXEC);
+       if (error)
+               return error;
+
+       error = gfs2_dir_check(&dip->i_inode, name, ip);
+       if (error)
+               return error;
+
+       return 0;
+}
+
 /**
  * gfs2_unlink - Unlink a file
  * @dir: The inode of the directory containing the file to unlink
@@ -313,7 +347,7 @@ static int gfs2_unlink(struct inode *dir, struct dentry *dentry)
 
        error = gfs2_trans_begin(sdp, 2*RES_DINODE + RES_LEAF + RES_RG_BIT, 0);
        if (error)
-               goto out_rgrp;
+               goto out_gunlock;
 
        error = gfs2_dir_del(dip, &dentry->d_name);
         if (error)
@@ -372,7 +406,8 @@ static int gfs2_symlink(struct inode *dir, struct dentry *dentry,
 
        ip = ghs[1].gh_gl->gl_object;
 
-       ip->i_di.di_size = size;
+       ip->i_disksize = size;
+       i_size_write(inode, size);
 
        error = gfs2_meta_inode_buffer(ip, &dibh);
 
@@ -426,9 +461,9 @@ static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
        ip = ghs[1].gh_gl->gl_object;
 
        ip->i_inode.i_nlink = 2;
-       ip->i_di.di_size = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
-       ip->i_di.di_flags |= GFS2_DIF_JDATA;
-       ip->i_di.di_entries = 2;
+       ip->i_disksize = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode);
+       ip->i_diskflags |= GFS2_DIF_JDATA;
+       ip->i_entries = 2;
 
        error = gfs2_meta_inode_buffer(ip, &dibh);
 
@@ -474,6 +509,59 @@ static int gfs2_mkdir(struct inode *dir, struct dentry *dentry, int mode)
 }
 
 /**
+ * gfs2_rmdiri - Remove a directory
+ * @dip: The parent directory of the directory to be removed
+ * @name: The name of the directory to be removed
+ * @ip: The GFS2 inode of the directory to be removed
+ *
+ * Assumes Glocks on dip and ip are held
+ *
+ * Returns: errno
+ */
+
+static int gfs2_rmdiri(struct gfs2_inode *dip, const struct qstr *name,
+                      struct gfs2_inode *ip)
+{
+       struct qstr dotname;
+       int error;
+
+       if (ip->i_entries != 2) {
+               if (gfs2_consist_inode(ip))
+                       gfs2_dinode_print(ip);
+               return -EIO;
+       }
+
+       error = gfs2_dir_del(dip, name);
+       if (error)
+               return error;
+
+       error = gfs2_change_nlink(dip, -1);
+       if (error)
+               return error;
+
+       gfs2_str2qstr(&dotname, ".");
+       error = gfs2_dir_del(ip, &dotname);
+       if (error)
+               return error;
+
+       gfs2_str2qstr(&dotname, "..");
+       error = gfs2_dir_del(ip, &dotname);
+       if (error)
+               return error;
+
+       /* It looks odd, but it really should be done twice */
+       error = gfs2_change_nlink(ip, -1);
+       if (error)
+               return error;
+
+       error = gfs2_change_nlink(ip, -1);
+       if (error)
+               return error;
+
+       return error;
+}
+
+/**
  * gfs2_rmdir - Remove a directory
  * @dir: The parent directory of the directory to be removed
  * @dentry: The dentry of the directory to remove
@@ -518,13 +606,13 @@ static int gfs2_rmdir(struct inode *dir, struct dentry *dentry)
        if (error)
                goto out_gunlock;
 
-       if (ip->i_di.di_entries < 2) {
+       if (ip->i_entries < 2) {
                if (gfs2_consist_inode(ip))
                        gfs2_dinode_print(ip);
                error = -EIO;
                goto out_gunlock;
        }
-       if (ip->i_di.di_entries > 2) {
+       if (ip->i_entries > 2) {
                error = -ENOTEMPTY;
                goto out_gunlock;
        }
@@ -727,13 +815,13 @@ static int gfs2_rename(struct inode *odir, struct dentry *odentry,
                        goto out_gunlock;
 
                if (S_ISDIR(nip->i_inode.i_mode)) {
-                       if (nip->i_di.di_entries < 2) {
+                       if (nip->i_entries < 2) {
                                if (gfs2_consist_inode(nip))
                                        gfs2_dinode_print(nip);
                                error = -EIO;
                                goto out_gunlock;
                        }
-                       if (nip->i_di.di_entries > 2) {
+                       if (nip->i_entries > 2) {
                                error = -ENOTEMPTY;
                                goto out_gunlock;
                        }
@@ -759,7 +847,7 @@ static int gfs2_rename(struct inode *odir, struct dentry *odentry,
                                error = -EINVAL;
                                goto out_gunlock;
                        }
-                       if (ndip->i_di.di_entries == (u32)-1) {
+                       if (ndip->i_entries == (u32)-1) {
                                error = -EFBIG;
                                goto out_gunlock;
                        }
@@ -886,6 +974,61 @@ out:
 }
 
 /**
+ * gfs2_readlinki - return the contents of a symlink
+ * @ip: the symlink's inode
+ * @buf: a pointer to the buffer to be filled
+ * @len: a pointer to the length of @buf
+ *
+ * If @buf is too small, a piece of memory is kmalloc()ed and needs
+ * to be freed by the caller.
+ *
+ * Returns: errno
+ */
+
+static int gfs2_readlinki(struct gfs2_inode *ip, char **buf, unsigned int *len)
+{
+       struct gfs2_holder i_gh;
+       struct buffer_head *dibh;
+       unsigned int x;
+       int error;
+
+       gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &i_gh);
+       error = gfs2_glock_nq(&i_gh);
+       if (error) {
+               gfs2_holder_uninit(&i_gh);
+               return error;
+       }
+
+       if (!ip->i_disksize) {
+               gfs2_consist_inode(ip);
+               error = -EIO;
+               goto out;
+       }
+
+       error = gfs2_meta_inode_buffer(ip, &dibh);
+       if (error)
+               goto out;
+
+       x = ip->i_disksize + 1;
+       if (x > *len) {
+               *buf = kmalloc(x, GFP_NOFS);
+               if (!*buf) {
+                       error = -ENOMEM;
+                       goto out_brelse;
+               }
+       }
+
+       memcpy(*buf, dibh->b_data + sizeof(struct gfs2_dinode), x);
+       *len = x;
+
+out_brelse:
+       brelse(dibh);
+out:
+       gfs2_glock_dq_uninit(&i_gh);
+       return error;
+}
+
+/**
  * gfs2_readlink - Read the value of a symlink
  * @dentry: the symlink
  * @buf: the buffer to read the symlink data into
@@ -991,7 +1134,7 @@ static int setattr_size(struct inode *inode, struct iattr *attr)
        struct gfs2_sbd *sdp = GFS2_SB(inode);
        int error;
 
-       if (attr->ia_size != ip->i_di.di_size) {
+       if (attr->ia_size != ip->i_disksize) {
                error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
                if (error)
                        return error;
@@ -1002,8 +1145,8 @@ static int setattr_size(struct inode *inode, struct iattr *attr)
        }
 
        error = gfs2_truncatei(ip, attr->ia_size);
-       if (error && (inode->i_size != ip->i_di.di_size))
-               i_size_write(inode, ip->i_di.di_size);
+       if (error && (inode->i_size != ip->i_disksize))
+               i_size_write(inode, ip->i_disksize);
 
        return error;
 }
@@ -1157,60 +1300,53 @@ static int gfs2_setxattr(struct dentry *dentry, const char *name,
                         const void *data, size_t size, int flags)
 {
        struct inode *inode = dentry->d_inode;
-       struct gfs2_ea_request er;
-
-       memset(&er, 0, sizeof(struct gfs2_ea_request));
-       er.er_type = gfs2_ea_name2type(name, &er.er_name);
-       if (er.er_type == GFS2_EATYPE_UNUSED)
-               return -EOPNOTSUPP;
-       er.er_data = (char *)data;
-       er.er_name_len = strlen(er.er_name);
-       er.er_data_len = size;
-       er.er_flags = flags;
-
-       gfs2_assert_warn(GFS2_SB(inode), !(er.er_flags & GFS2_ERF_MODE));
+       struct gfs2_inode *ip = GFS2_I(inode);
+       struct gfs2_holder gh;
+       int ret;
 
-       return gfs2_ea_set(GFS2_I(inode), &er);
+       gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
+       ret = gfs2_glock_nq(&gh);
+       if (ret == 0) {
+               ret = generic_setxattr(dentry, name, data, size, flags);
+               gfs2_glock_dq(&gh);
+       }
+       gfs2_holder_uninit(&gh);
+       return ret;
 }
 
 static ssize_t gfs2_getxattr(struct dentry *dentry, const char *name,
                             void *data, size_t size)
 {
-       struct gfs2_ea_request er;
-
-       memset(&er, 0, sizeof(struct gfs2_ea_request));
-       er.er_type = gfs2_ea_name2type(name, &er.er_name);
-       if (er.er_type == GFS2_EATYPE_UNUSED)
-               return -EOPNOTSUPP;
-       er.er_data = data;
-       er.er_name_len = strlen(er.er_name);
-       er.er_data_len = size;
-
-       return gfs2_ea_get(GFS2_I(dentry->d_inode), &er);
-}
-
-static ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
-{
-       struct gfs2_ea_request er;
-
-       memset(&er, 0, sizeof(struct gfs2_ea_request));
-       er.er_data = (size) ? buffer : NULL;
-       er.er_data_len = size;
+       struct inode *inode = dentry->d_inode;
+       struct gfs2_inode *ip = GFS2_I(inode);
+       struct gfs2_holder gh;
+       int ret;
 
-       return gfs2_ea_list(GFS2_I(dentry->d_inode), &er);
+       gfs2_holder_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh);
+       ret = gfs2_glock_nq(&gh);
+       if (ret == 0) {
+               ret = generic_getxattr(dentry, name, data, size);
+               gfs2_glock_dq(&gh);
+       }
+       gfs2_holder_uninit(&gh);
+       return ret;
 }
 
 static int gfs2_removexattr(struct dentry *dentry, const char *name)
 {
-       struct gfs2_ea_request er;
-
-       memset(&er, 0, sizeof(struct gfs2_ea_request));
-       er.er_type = gfs2_ea_name2type(name, &er.er_name);
-       if (er.er_type == GFS2_EATYPE_UNUSED)
-               return -EOPNOTSUPP;
-       er.er_name_len = strlen(er.er_name);
+       struct inode *inode = dentry->d_inode;
+       struct gfs2_inode *ip = GFS2_I(inode);
+       struct gfs2_holder gh;
+       int ret;
 
-       return gfs2_ea_remove(GFS2_I(dentry->d_inode), &er);
+       gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
+       ret = gfs2_glock_nq(&gh);
+       if (ret == 0) {
+               ret = generic_removexattr(dentry, name);
+               gfs2_glock_dq(&gh);
+       }
+       gfs2_holder_uninit(&gh);
+       return ret;
 }
 
 static int gfs2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,