]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blobdiff - fs/nfs/nfs4proc.c
NFS: Replace file->private_data with calls to nfs_file_open_context()
[mirror_ubuntu-artful-kernel.git] / fs / nfs / nfs4proc.c
index 1de0766192539d6d2dd14fdfc8d23bb93a43b428..2919271a983a9915ed74538a9eb2d6f3cc3b6c0d 100644 (file)
@@ -66,6 +66,8 @@ static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry
 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
 static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp);
 static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags);
+static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
+static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
 
 /* Prevent leaks of NFSv4 errors into userland */
 int nfs4_map_errors(int err)
@@ -175,7 +177,7 @@ static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dent
                *p++ = xdr_one;                         /* bitmap length */
                *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
                *p++ = htonl(8);              /* attribute buffer length */
-               p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
+               p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
        }
        
        *p++ = xdr_one;                                  /* next */
@@ -187,7 +189,7 @@ static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dent
        *p++ = xdr_one;                         /* bitmap length */
        *p++ = htonl(FATTR4_WORD0_FILEID);             /* bitmap */
        *p++ = htonl(8);              /* attribute buffer length */
-       p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
+       p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
 
        readdir->pgbase = (char *)p - (char *)start;
        readdir->count -= readdir->pgbase;
@@ -330,11 +332,9 @@ static int can_open_cached(struct nfs4_state *state, int mode)
        switch (mode & (FMODE_READ|FMODE_WRITE|O_EXCL)) {
                case FMODE_READ:
                        ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
-                       ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
                        break;
                case FMODE_WRITE:
                        ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
-                       ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
                        break;
                case FMODE_READ|FMODE_WRITE:
                        ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
@@ -385,29 +385,28 @@ static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *
 
 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
 {
-       spin_lock(&state->owner->so_lock);
-       spin_lock(&state->inode->i_lock);
+       write_seqlock(&state->seqlock);
        nfs_set_open_stateid_locked(state, stateid, open_flags);
-       spin_unlock(&state->inode->i_lock);
-       spin_unlock(&state->owner->so_lock);
+       write_sequnlock(&state->seqlock);
 }
 
 static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *deleg_stateid, int open_flags)
 {
-       struct inode *inode = state->inode;
-
        open_flags &= (FMODE_READ|FMODE_WRITE);
-       /* Protect against nfs4_find_state_byowner() */
-       spin_lock(&state->owner->so_lock);
-       spin_lock(&inode->i_lock);
+       /*
+        * Protect the call to nfs4_state_set_mode_locked and
+        * serialise the stateid update
+        */
+       write_seqlock(&state->seqlock);
        if (deleg_stateid != NULL) {
                memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
                set_bit(NFS_DELEGATED_STATE, &state->flags);
        }
        if (open_stateid != NULL)
                nfs_set_open_stateid_locked(state, open_stateid, open_flags);
+       write_sequnlock(&state->seqlock);
+       spin_lock(&state->owner->so_lock);
        update_open_stateflags(state, open_flags);
-       spin_unlock(&inode->i_lock);
        spin_unlock(&state->owner->so_lock);
 }
 
@@ -553,6 +552,18 @@ static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *
        return ERR_PTR(-ENOENT);
 }
 
+static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
+{
+       struct nfs4_opendata *opendata;
+
+       opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
+       if (opendata == NULL)
+               return ERR_PTR(-ENOMEM);
+       opendata->state = state;
+       atomic_inc(&state->count);
+       return opendata;
+}
+
 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, struct nfs4_state **res)
 {
        struct nfs4_state *newstate;
@@ -602,6 +613,17 @@ static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *
                if (newstate != state)
                        return -ESTALE;
        }
+       /*
+        * We may have performed cached opens for all three recoveries.
+        * Check if we need to update the current stateid.
+        */
+       if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
+           memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
+               write_seqlock(&state->seqlock);
+               if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
+                       memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
+               write_sequnlock(&state->seqlock);
+       }
        return 0;
 }
 
@@ -611,26 +633,21 @@ static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *
  */
 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
 {
-       struct nfs_delegation *delegation = NFS_I(state->inode)->delegation;
+       struct nfs_delegation *delegation;
        struct nfs4_opendata *opendata;
        int delegation_type = 0;
        int status;
 
-       if (delegation != NULL) {
-               if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
-                       memcpy(&state->stateid, &delegation->stateid,
-                                       sizeof(state->stateid));
-                       set_bit(NFS_DELEGATED_STATE, &state->flags);
-                       return 0;
-               }
-               delegation_type = delegation->type;
-       }
-       opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL);
-       if (opendata == NULL)
-               return -ENOMEM;
+       opendata = nfs4_open_recoverdata_alloc(ctx, state);
+       if (IS_ERR(opendata))
+               return PTR_ERR(opendata);
        opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
        opendata->o_arg.fh = NFS_FH(state->inode);
-       nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh);
+       rcu_read_lock();
+       delegation = rcu_dereference(NFS_I(state->inode)->delegation);
+       if (delegation != NULL && (delegation->flags & NFS_DELEGATION_NEED_RECLAIM) != 0)
+               delegation_type = delegation->type;
+       rcu_read_unlock();
        opendata->o_arg.u.delegation_type = delegation_type;
        status = nfs4_open_recover(opendata, state);
        nfs4_opendata_put(opendata);
@@ -666,13 +683,12 @@ static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *sta
 
 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
 {
-       struct nfs4_state_owner  *sp  = state->owner;
        struct nfs4_opendata *opendata;
        int ret;
 
-       opendata = nfs4_opendata_alloc(&ctx->path, sp, 0, NULL);
-       if (opendata == NULL)
-               return -ENOMEM;
+       opendata = nfs4_open_recoverdata_alloc(ctx, state);
+       if (IS_ERR(opendata))
+               return PTR_ERR(opendata);
        opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
        memcpy(opendata->o_arg.u.delegation.data, stateid->data,
                        sizeof(opendata->o_arg.u.delegation.data));
@@ -817,8 +833,10 @@ static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
        /* Update sequence id. */
        data->o_arg.id = sp->so_owner_id.id;
        data->o_arg.clientid = sp->so_client->cl_clientid;
-       if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
+       if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
                msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
+               nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
+       }
        data->timestamp = jiffies;
        rpc_call_setup(task, &msg, 0);
        return;
@@ -912,6 +930,9 @@ static int _nfs4_proc_open(struct nfs4_opendata *data)
        if (status != 0 || !data->rpc_done)
                return status;
 
+       if (o_res->fh.size == 0)
+               _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
+
        if (o_arg->open_flags & O_CREAT) {
                update_changeattr(dir, &o_res->cinfo);
                nfs_post_op_update_inode(dir, o_res->dir_attr);
@@ -923,7 +944,7 @@ static int _nfs4_proc_open(struct nfs4_opendata *data)
                        return status;
        }
        if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
-               return server->nfs_client->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
+               _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
        return 0;
 }
 
@@ -980,23 +1001,12 @@ static int nfs4_recover_expired_lease(struct nfs_server *server)
  */
 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
 {
-       struct inode *inode = state->inode;
-       struct nfs_delegation *delegation = NFS_I(inode)->delegation;
        struct nfs4_opendata *opendata;
-       int openflags = state->state & (FMODE_READ|FMODE_WRITE);
        int ret;
 
-       if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
-               ret = _nfs4_do_access(inode, ctx->cred, openflags);
-               if (ret < 0)
-                       return ret;
-               memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
-               set_bit(NFS_DELEGATED_STATE, &state->flags);
-               return 0;
-       }
-       opendata = nfs4_opendata_alloc(&ctx->path, state->owner, openflags, NULL);
-       if (opendata == NULL)
-               return -ENOMEM;
+       opendata = nfs4_open_recoverdata_alloc(ctx, state);
+       if (IS_ERR(opendata))
+               return PTR_ERR(opendata);
        ret = nfs4_open_recover(opendata, state);
        if (ret == -ESTALE) {
                /* Invalidate the state owner so we don't ever use it again */
@@ -1248,7 +1258,7 @@ static void nfs4_close_done(struct rpc_task *task, void *data)
        nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
        switch (task->tk_status) {
                case 0:
-                       nfs_set_open_stateid(state, &calldata->res.stateid, calldata->arg.open_flags);
+                       nfs_set_open_stateid(state, &calldata->res.stateid, 0);
                        renew_lease(server, calldata->timestamp);
                        break;
                case -NFS4ERR_STALE_STATEID:
@@ -1274,29 +1284,23 @@ static void nfs4_close_prepare(struct rpc_task *task, void *data)
                .rpc_cred = state->owner->so_cred,
        };
        int clear_rd, clear_wr, clear_rdwr;
-       int mode;
 
        if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
                return;
 
-       mode = FMODE_READ|FMODE_WRITE;
        clear_rd = clear_wr = clear_rdwr = 0;
        spin_lock(&state->owner->so_lock);
-       spin_lock(&calldata->inode->i_lock);
        /* Calculate the change in open mode */
        if (state->n_rdwr == 0) {
                if (state->n_rdonly == 0) {
-                       mode &= ~FMODE_READ;
                        clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
                        clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
                }
                if (state->n_wronly == 0) {
-                       mode &= ~FMODE_WRITE;
                        clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
                        clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
                }
        }
-       spin_unlock(&calldata->inode->i_lock);
        spin_unlock(&state->owner->so_lock);
        if (!clear_rd && !clear_wr && !clear_rdwr) {
                /* Note: exit _without_ calling nfs4_close_done */
@@ -1304,9 +1308,13 @@ static void nfs4_close_prepare(struct rpc_task *task, void *data)
                return;
        }
        nfs_fattr_init(calldata->res.fattr);
-       if (mode != 0)
+       if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
+               msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
+               calldata->arg.open_flags = FMODE_READ;
+       } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
                msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
-       calldata->arg.open_flags = mode;
+               calldata->arg.open_flags = FMODE_WRITE;
+       }
        calldata->timestamp = jiffies;
        rpc_call_setup(task, &msg, 0);
 }
@@ -1382,7 +1390,7 @@ static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct
        filp = lookup_instantiate_filp(nd, path->dentry, NULL);
        if (!IS_ERR(filp)) {
                struct nfs_open_context *ctx;
-               ctx = (struct nfs_open_context *)filp->private_data;
+               ctx = nfs_file_open_context(filp);
                ctx->state = state;
                return 0;
        }
@@ -1426,7 +1434,7 @@ nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
        }
        res = d_add_unique(dentry, igrab(state->inode));
        if (res != NULL)
-               dentry = res;
+               path.dentry = res;
        nfs4_intent_set_file(nd, &path, state);
        return res;
 }
@@ -1560,7 +1568,7 @@ static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
  * Note that we'll actually follow the referral later when
  * we detect fsid mismatch in inode revalidation
  */
-static int nfs4_get_referral(struct inode *dir, struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
+static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
 {
        int status = -ENOMEM;
        struct page *page = NULL;
@@ -1675,8 +1683,8 @@ nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
        return status;
 }
 
-static int _nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
-               struct qstr *name, struct nfs_fh *fhandle,
+static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
+               const struct qstr *name, struct nfs_fh *fhandle,
                struct nfs_fattr *fattr)
 {
        int                    status;
@@ -1722,7 +1730,7 @@ static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
        return err;
 }
 
-static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
+static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
                struct nfs_fh *fhandle, struct nfs_fattr *fattr)
 {
        int status;
@@ -1915,28 +1923,27 @@ out:
 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
 {
        struct nfs_server *server = NFS_SERVER(dir);
-       struct nfs4_remove_arg args = {
+       struct nfs_removeargs args = {
                .fh = NFS_FH(dir),
-               .name = name,
+               .name.len = name->len,
+               .name.name = name->name,
                .bitmask = server->attr_bitmask,
        };
-       struct nfs_fattr dir_attr;
-       struct nfs4_remove_res  res = {
+       struct nfs_removeres res = {
                .server = server,
-               .dir_attr = &dir_attr,
        };
        struct rpc_message msg = {
-               .rpc_proc       = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
-               .rpc_argp       = &args,
-               .rpc_resp       = &res,
+               .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
+               .rpc_argp = &args,
+               .rpc_resp = &res,
        };
        int                     status;
 
-       nfs_fattr_init(res.dir_attr);
+       nfs_fattr_init(&res.dir_attr);
        status = rpc_call_sync(server->client, &msg, 0);
        if (status == 0) {
                update_changeattr(dir, &res.cinfo);
-               nfs_post_op_update_inode(dir, res.dir_attr);
+               nfs_post_op_update_inode(dir, &res.dir_attr);
        }
        return status;
 }
@@ -1953,48 +1960,26 @@ static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
        return err;
 }
 
-struct unlink_desc {
-       struct nfs4_remove_arg  args;
-       struct nfs4_remove_res  res;
-       struct nfs_fattr dir_attr;
-};
-
-static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
-               struct qstr *name)
+static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
 {
-       struct nfs_server *server = NFS_SERVER(dir->d_inode);
-       struct unlink_desc *up;
+       struct nfs_server *server = NFS_SERVER(dir);
+       struct nfs_removeargs *args = msg->rpc_argp;
+       struct nfs_removeres *res = msg->rpc_resp;
 
-       up = kmalloc(sizeof(*up), GFP_KERNEL);
-       if (!up)
-               return -ENOMEM;
-       
-       up->args.fh = NFS_FH(dir->d_inode);
-       up->args.name = name;
-       up->args.bitmask = server->attr_bitmask;
-       up->res.server = server;
-       up->res.dir_attr = &up->dir_attr;
-       
+       args->bitmask = server->attr_bitmask;
+       res->server = server;
        msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
-       msg->rpc_argp = &up->args;
-       msg->rpc_resp = &up->res;
-       return 0;
 }
 
-static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
+static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
 {
-       struct rpc_message *msg = &task->tk_msg;
-       struct unlink_desc *up;
-       
-       if (msg->rpc_resp != NULL) {
-               up = container_of(msg->rpc_resp, struct unlink_desc, res);
-               update_changeattr(dir->d_inode, &up->res.cinfo);
-               nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
-               kfree(up);
-               msg->rpc_resp = NULL;
-               msg->rpc_argp = NULL;
-       }
-       return 0;
+       struct nfs_removeres *res = task->tk_msg.rpc_resp;
+
+       if (nfs4_async_handle_error(task, res->server) == -EAGAIN)
+               return 0;
+       update_changeattr(dir, &res->cinfo);
+       nfs_post_op_update_inode(dir, &res->dir_attr);
+       return 1;
 }
 
 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
@@ -3282,6 +3267,11 @@ static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
 {
        struct nfs4_unlockdata *data;
 
+       /* Ensure this is an unlock - when canceling a lock, the
+        * canceled lock is passed in, and it won't be an unlock.
+        */
+       fl->fl_type = F_UNLCK;
+
        data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
        if (data == NULL) {
                nfs_free_seqid(seqid);
@@ -3313,7 +3303,7 @@ static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *
        status = -ENOMEM;
        if (seqid == NULL)
                goto out;
-       task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid);
+       task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
        status = PTR_ERR(task);
        if (IS_ERR(task))
                goto out;
@@ -3457,7 +3447,7 @@ static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *f
        int ret;
 
        dprintk("%s: begin!\n", __FUNCTION__);
-       data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data,
+       data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
                        fl->fl_u.nfs4_fl.owner);
        if (data == NULL)
                return -ENOMEM;
@@ -3583,7 +3573,7 @@ nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
        int status;
 
        /* verify open state */
-       ctx = (struct nfs_open_context *)filp->private_data;
+       ctx = nfs_file_open_context(filp);
        state = ctx->state;
 
        if (request->fl_start < 0 || request->fl_end < 0)
@@ -3674,7 +3664,7 @@ ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
        return len;
 }
 
-int nfs4_proc_fs_locations(struct inode *dir, struct qstr *name,
+int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
                struct nfs4_fs_locations *fs_locations, struct page *page)
 {
        struct nfs_server *server = NFS_SERVER(dir);