Skip to content
Snippets Groups Projects
nfs4proc.c 161 KiB
Newer Older
  • Learn to ignore specific revisions
  • 
    	rcu_read_lock();
    	delegation = rcu_dereference(NFS_I(inode)->delegation);
    
    	if (delegation == NULL || (delegation->type & fmode) == fmode) {
    
    		rcu_read_unlock();
    		return;
    	}
    	rcu_read_unlock();
    	nfs_inode_return_delegation(inode);
    }
    
    
    static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
    
    {
    	struct nfs4_state *state = opendata->state;
    	struct nfs_inode *nfsi = NFS_I(state->inode);
    	struct nfs_delegation *delegation;
    
    	int open_mode = opendata->o_arg.open_flags & O_EXCL;
    	fmode_t fmode = opendata->o_arg.fmode;
    
    	nfs4_stateid stateid;
    	int ret = -EAGAIN;
    
    	for (;;) {
    
    		if (can_open_cached(state, fmode, open_mode)) {
    
    			spin_lock(&state->owner->so_lock);
    
    			if (can_open_cached(state, fmode, open_mode)) {
    				update_open_stateflags(state, fmode);
    
    				spin_unlock(&state->owner->so_lock);
    				goto out_return_state;
    			}
    			spin_unlock(&state->owner->so_lock);
    		}
    
    		rcu_read_lock();
    		delegation = rcu_dereference(nfsi->delegation);
    		if (delegation == NULL ||
    
    		    !can_open_delegated(delegation, fmode)) {
    
    		/* Save the delegation */
    		memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
    		rcu_read_unlock();
    
    		ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
    
    
    		/* Try to update the stateid using the delegation */
    
    		if (update_open_stateid(state, NULL, &stateid, fmode))
    
    			goto out_return_state;
    
    	}
    out:
    	return ERR_PTR(ret);
    out_return_state:
    	atomic_inc(&state->count);
    	return state;
    }
    
    
    static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
    {
    	struct inode *inode;
    	struct nfs4_state *state = NULL;
    
    	struct nfs_delegation *delegation;
    
    		state = nfs4_try_open_cached(data);
    
    	if (!(data->f_attr.valid & NFS_ATTR_FATTR))
    
    	inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
    
    	state = nfs4_get_open_state(inode, data->owner);
    	if (state == NULL)
    
    	if (data->o_res.delegation_type != 0) {
    		int delegation_flags = 0;
    
    
    		rcu_read_lock();
    		delegation = rcu_dereference(NFS_I(inode)->delegation);
    		if (delegation)
    			delegation_flags = delegation->flags;
    		rcu_read_unlock();
    
    		if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
    
    			nfs_inode_set_delegation(state->inode,
    					data->owner->so_cred,
    					&data->o_res);
    		else
    			nfs_inode_reclaim_delegation(state->inode,
    					data->owner->so_cred,
    					&data->o_res);
    	}
    
    
    	update_open_stateid(state, &data->o_res.stateid, NULL,
    
    err_put_inode:
    	iput(inode);
    err:
    	return ERR_PTR(ret);
    
    static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
    {
    	struct nfs_inode *nfsi = NFS_I(state->inode);
    	struct nfs_open_context *ctx;
    
    	spin_lock(&state->inode->i_lock);
    	list_for_each_entry(ctx, &nfsi->open_files, list) {
    		if (ctx->state != state)
    			continue;
    		get_nfs_open_context(ctx);
    		spin_unlock(&state->inode->i_lock);
    		return ctx;
    	}
    	spin_unlock(&state->inode->i_lock);
    	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, 0, NULL, GFP_NOFS);
    
    	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, fmode_t fmode, struct nfs4_state **res)
    
    	struct nfs4_state *newstate;
    
    	opendata->o_arg.open_flags = 0;
    	opendata->o_arg.fmode = fmode;
    
    	memset(&opendata->o_res, 0, sizeof(opendata->o_res));
    	memset(&opendata->c_res, 0, sizeof(opendata->c_res));
    	nfs4_init_opendata_res(opendata);
    
    	ret = _nfs4_recover_proc_open(opendata);
    
    	newstate = nfs4_opendata_to_nfs4_state(opendata);
    
    	if (IS_ERR(newstate))
    		return PTR_ERR(newstate);
    
    	nfs4_close_state(&opendata->path, newstate, fmode);
    
    	return 0;
    }
    
    static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
    {
    	struct nfs4_state *newstate;
    	int ret;
    
    	/* memory barrier prior to reading state->n_* */
    
    	clear_bit(NFS_DELEGATED_STATE, &state->flags);
    
    		clear_bit(NFS_O_RDWR_STATE, &state->flags);
    
    		ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
    
    		if (newstate != state)
    			return -ESTALE;
    
    		clear_bit(NFS_O_WRONLY_STATE, &state->flags);
    
    		ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
    
    		if (newstate != state)
    			return -ESTALE;
    
    		clear_bit(NFS_O_RDONLY_STATE, &state->flags);
    
    		ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
    
    		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);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * OPEN_RECLAIM:
     * 	reclaim state on the server after a reboot.
     */
    
    static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct nfs_delegation *delegation;
    
    	fmode_t delegation_type = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	int status;
    
    
    	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);
    
    	rcu_read_lock();
    	delegation = rcu_dereference(NFS_I(state->inode)->delegation);
    
    	if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
    
    		delegation_type = delegation->type;
    
    	opendata->o_arg.u.delegation_type = delegation_type;
    	status = nfs4_open_recover(opendata, state);
    
    	nfs4_opendata_put(opendata);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return status;
    }
    
    
    static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct nfs_server *server = NFS_SERVER(state->inode);
    	struct nfs4_exception exception = { };
    	int err;
    	do {
    
    		err = _nfs4_do_open_reclaim(ctx, state);
    
    		if (err != -NFS4ERR_DELAY)
    
    			break;
    		nfs4_handle_exception(server, err, &exception);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	} while (exception.retry);
    	return err;
    }
    
    
    static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
    {
    	struct nfs_open_context *ctx;
    	int ret;
    
    	ctx = nfs4_state_find_open_context(state);
    	if (IS_ERR(ctx))
    		return PTR_ERR(ctx);
    
    	ret = nfs4_do_open_reclaim(ctx, state);
    
    static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct nfs4_opendata *opendata;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	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));
    
    	ret = nfs4_open_recover(opendata, state);
    
    	nfs4_opendata_put(opendata);
    
    int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct nfs4_exception exception = { };
    
    	struct nfs_server *server = NFS_SERVER(state->inode);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	int err;
    	do {
    
    		err = _nfs4_open_delegation_recall(ctx, state, stateid);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		switch (err) {
    			case 0:
    
    			case -NFS4ERR_BADSESSION:
    			case -NFS4ERR_BADSLOT:
    			case -NFS4ERR_BAD_HIGH_SLOT:
    			case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
    			case -NFS4ERR_DEADSESSION:
    
    				nfs4_schedule_session_recovery(server->nfs_client->cl_session);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			case -NFS4ERR_STALE_CLIENTID:
    			case -NFS4ERR_STALE_STATEID:
    			case -NFS4ERR_EXPIRED:
    				/* Don't recall a delegation if it was lost */
    
    				nfs4_schedule_lease_recovery(server->nfs_client);
    
    				goto out;
    			case -ERESTARTSYS:
    				/*
    				 * The show must go on: exit, but mark the
    				 * stateid as needing recovery.
    				 */
    			case -NFS4ERR_ADMIN_REVOKED:
    			case -NFS4ERR_BAD_STATEID:
    
    				nfs4_schedule_stateid_recovery(server, state);
    
    			case -EKEYEXPIRED:
    				/*
    				 * User RPCSEC_GSS context has expired.
    				 * We cannot recover this stateid now, so
    				 * skip it and allow recovery thread to
    				 * proceed.
    				 */
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		}
    		err = nfs4_handle_exception(server, err, &exception);
    	} while (exception.retry);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return err;
    }
    
    
    static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
    {
    	struct nfs4_opendata *data = calldata;
    
    	data->rpc_status = task->tk_status;
    
    		memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
    				sizeof(data->o_res.stateid.data));
    
    		nfs_confirm_seqid(&data->owner->so_seqid, 0);
    
    		renew_lease(data->o_res.server, data->timestamp);
    
    		data->rpc_done = 1;
    
    }
    
    static void nfs4_open_confirm_release(void *calldata)
    {
    	struct nfs4_opendata *data = calldata;
    	struct nfs4_state *state = NULL;
    
    	/* If this request hasn't been cancelled, do nothing */
    	if (data->cancelled == 0)
    		goto out_free;
    	/* In case of error, no cleanup! */
    
    	if (!data->rpc_done)
    
    		goto out_free;
    	state = nfs4_opendata_to_nfs4_state(data);
    
    		nfs4_close_state(&data->path, state, data->o_arg.fmode);
    
    }
    
    static const struct rpc_call_ops nfs4_open_confirm_ops = {
    	.rpc_call_done = nfs4_open_confirm_done,
    	.rpc_release = nfs4_open_confirm_release,
    };
    
    /*
     * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
     */
    static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
    {
    	struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
    	struct rpc_task *task;
    
    	struct  rpc_message msg = {
    		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
    		.rpc_argp = &data->c_arg,
    		.rpc_resp = &data->c_res,
    		.rpc_cred = data->owner->so_cred,
    	};
    
    	struct rpc_task_setup task_setup_data = {
    		.rpc_client = server->client,
    
    		.callback_ops = &nfs4_open_confirm_ops,
    		.callback_data = data,
    
    		.flags = RPC_TASK_ASYNC,
    	};
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	int status;
    
    
    	data->rpc_done = 0;
    	data->rpc_status = 0;
    
    	data->timestamp = jiffies;
    
    	task = rpc_run_task(&task_setup_data);
    
    		return PTR_ERR(task);
    	status = nfs4_wait_for_completion_rpc_task(task);
    	if (status != 0) {
    		data->cancelled = 1;
    		smp_wmb();
    	} else
    		status = data->rpc_status;
    
    	rpc_put_task(task);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	return status;
    }
    
    
    static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct nfs4_opendata *data = calldata;
    	struct nfs4_state_owner *sp = data->owner;
    
    	if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
    		return;
    
    	/*
    	 * Check if we still need to send an OPEN call, or if we can use
    	 * a delegation instead.
    	 */
    	if (data->state != NULL) {
    		struct nfs_delegation *delegation;
    
    
    		if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
    
    		rcu_read_lock();
    		delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
    		if (delegation != NULL &&
    
    		    test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
    
    	/* Update sequence id. */
    
    	data->o_arg.id = sp->so_owner_id.id;
    
    	data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
    
    	if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
    
    		task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
    
    		nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
    	}
    
    	if (nfs4_setup_sequence(data->o_arg.server,
    
    				&data->o_arg.seq_args,
    				&data->o_res.seq_res, 1, task))
    		return;
    
    	rpc_call_start(task);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
    {
    	rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
    	nfs4_open_prepare(task, calldata);
    }
    
    
    static void nfs4_open_done(struct rpc_task *task, void *calldata)
    {
    	struct nfs4_opendata *data = calldata;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	data->rpc_status = task->tk_status;
    
    	if (!nfs4_sequence_done(task, &data->o_res.seq_res))
    		return;
    
    	if (task->tk_status == 0) {
    		switch (data->o_res.f_attr->mode & S_IFMT) {
    
    				data->rpc_status = -ELOOP;
    
    				data->rpc_status = -EISDIR;
    
    				data->rpc_status = -ENOTDIR;
    
    		renew_lease(data->o_res.server, data->timestamp);
    
    		if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
    			nfs_confirm_seqid(&data->owner->so_seqid, 0);
    
    	data->rpc_done = 1;
    
    static void nfs4_open_release(void *calldata)
    {
    	struct nfs4_opendata *data = calldata;
    	struct nfs4_state *state = NULL;
    
    	/* If this request hasn't been cancelled, do nothing */
    	if (data->cancelled == 0)
    		goto out_free;
    	/* In case of error, no cleanup! */
    
    	if (data->rpc_status != 0 || !data->rpc_done)
    
    		goto out_free;
    	/* In case we need an open_confirm, no cleanup! */
    	if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
    		goto out_free;
    	state = nfs4_opendata_to_nfs4_state(data);
    
    		nfs4_close_state(&data->path, state, data->o_arg.fmode);
    
    }
    
    static const struct rpc_call_ops nfs4_open_ops = {
    	.rpc_call_prepare = nfs4_open_prepare,
    	.rpc_call_done = nfs4_open_done,
    	.rpc_release = nfs4_open_release,
    };
    
    
    static const struct rpc_call_ops nfs4_recover_open_ops = {
    	.rpc_call_prepare = nfs4_recover_open_prepare,
    	.rpc_call_done = nfs4_open_done,
    	.rpc_release = nfs4_open_release,
    };
    
    static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
    
    {
    	struct inode *dir = data->dir->d_inode;
    	struct nfs_server *server = NFS_SERVER(dir);
    	struct nfs_openargs *o_arg = &data->o_arg;
    	struct nfs_openres *o_res = &data->o_res;
    	struct rpc_task *task;
    
    	struct rpc_message msg = {
    		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
    		.rpc_argp = o_arg,
    		.rpc_resp = o_res,
    		.rpc_cred = data->owner->so_cred,
    	};
    
    	struct rpc_task_setup task_setup_data = {
    		.rpc_client = server->client,
    
    		.callback_ops = &nfs4_open_ops,
    		.callback_data = data,
    
    		.flags = RPC_TASK_ASYNC,
    	};
    
    	data->rpc_done = 0;
    	data->rpc_status = 0;
    
    	data->cancelled = 0;
    
    	if (isrecover)
    		task_setup_data.callback_ops = &nfs4_recover_open_ops;
    
    	task = rpc_run_task(&task_setup_data);
    
            if (IS_ERR(task))
                    return PTR_ERR(task);
            status = nfs4_wait_for_completion_rpc_task(task);
            if (status != 0) {
                    data->cancelled = 1;
                    smp_wmb();
            } else
                    status = data->rpc_status;
            rpc_put_task(task);
    
    	return status;
    }
    
    static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
    {
    	struct inode *dir = data->dir->d_inode;
    	struct nfs_openres *o_res = &data->o_res;
            int status;
    
    	status = nfs4_run_open_task(data, 1);
    	if (status != 0 || !data->rpc_done)
    		return status;
    
    	nfs_refresh_inode(dir, o_res->dir_attr);
    
    	if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
    		status = _nfs4_proc_open_confirm(data);
    		if (status != 0)
    			return status;
    	}
    
    	return status;
    }
    
    /*
     * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
     */
    static int _nfs4_proc_open(struct nfs4_opendata *data)
    {
    	struct inode *dir = data->dir->d_inode;
    	struct nfs_server *server = NFS_SERVER(dir);
    	struct nfs_openargs *o_arg = &data->o_arg;
    	struct nfs_openres *o_res = &data->o_res;
    	int status;
    
    	status = nfs4_run_open_task(data, 0);
    
    	if (status != 0 || !data->rpc_done)
    
    	if (o_arg->open_flags & O_CREAT) {
    		update_changeattr(dir, &o_res->cinfo);
    		nfs_post_op_update_inode(dir, o_res->dir_attr);
    	} else
    		nfs_refresh_inode(dir, o_res->dir_attr);
    
    	if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
    		server->caps &= ~NFS_CAP_POSIX_LOCK;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
    
    		status = _nfs4_proc_open_confirm(data);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		if (status != 0)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    	if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
    
    		_nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
    
    static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
    
    	for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
    
    		ret = nfs4_wait_clnt_recover(clp);
    
    		if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
    		    !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
    
    		nfs4_schedule_state_manager(clp);
    
    static int nfs4_recover_expired_lease(struct nfs_server *server)
    {
    	return nfs4_client_recover_expired_lease(server->nfs_client);
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * OPEN_EXPIRED:
     * 	reclaim state on the server after a network partition.
     * 	Assumes caller holds the appropriate lock
     */
    
    static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct nfs4_opendata *opendata;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	opendata = nfs4_open_recoverdata_alloc(ctx, state);
    	if (IS_ERR(opendata))
    		return PTR_ERR(opendata);
    
    	ret = nfs4_open_recover(opendata, state);
    
    	nfs4_opendata_put(opendata);
    
    static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
    
    	struct nfs_server *server = NFS_SERVER(state->inode);
    
    		err = _nfs4_open_expired(ctx, state);
    
    		switch (err) {
    		default:
    			goto out;
    		case -NFS4ERR_GRACE:
    		case -NFS4ERR_DELAY:
    			nfs4_handle_exception(server, err, &exception);
    			err = 0;
    		}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
    {
    	struct nfs_open_context *ctx;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	ctx = nfs4_state_find_open_context(state);
    	if (IS_ERR(ctx))
    		return PTR_ERR(ctx);
    
    	ret = nfs4_do_open_expired(ctx, state);
    
    /*
     * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
     * fields corresponding to attributes that were used to store the verifier.
     * Make sure we clobber those fields in the later setattr call
     */
    static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
    {
    	if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
    	    !(sattr->ia_valid & ATTR_ATIME_SET))
    		sattr->ia_valid |= ATTR_ATIME;
    
    	if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
    	    !(sattr->ia_valid & ATTR_MTIME_SET))
    		sattr->ia_valid |= ATTR_MTIME;
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
    
     * Returns a referenced nfs4_state
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    
    static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct nfs4_state_owner  *sp;
    	struct nfs4_state     *state = NULL;
    	struct nfs_server       *server = NFS_SERVER(dir);
    
    	struct nfs4_opendata *opendata;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    	/* Protect against reboot recovery conflicts */
    	status = -ENOMEM;
    	if (!(sp = nfs4_get_state_owner(server, cred))) {
    		dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
    		goto out_err;
    	}
    
    	status = nfs4_recover_expired_lease(server);
    	if (status != 0)
    
    		nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
    
    	opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
    
    		goto err_put_state_owner;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (path->dentry->d_inode != NULL)
    		opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
    
    
    	status = _nfs4_proc_open(opendata);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	if (status != 0)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	state = nfs4_opendata_to_nfs4_state(opendata);
    
    	status = PTR_ERR(state);
    	if (IS_ERR(state))
    
    	if (server->caps & NFS_CAP_POSIX_LOCK)
    
    		set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
    
    
    	if (opendata->o_arg.open_flags & O_EXCL) {
    		nfs4_exclusive_attrset(opendata, sattr);
    
    		nfs_fattr_init(opendata->o_res.f_attr);
    		status = nfs4_do_setattr(state->inode, cred,
    				opendata->o_res.f_attr, sattr,
    				state);
    		if (status == 0)
    			nfs_setattr_update_inode(state->inode, sattr);
    		nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
    	}
    
    	nfs4_opendata_put(opendata);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	nfs4_put_state_owner(sp);
    	*res = state;
    	return 0;
    
    err_opendata_put:
    	nfs4_opendata_put(opendata);
    
    err_put_state_owner:
    	nfs4_put_state_owner(sp);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    out_err:
    	*res = NULL;
    	return status;
    }
    
    
    
    static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    	struct nfs4_exception exception = { };
    	struct nfs4_state *res;
    	int status;
    
    	do {
    
    		status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		if (status == 0)
    			break;
    		/* NOTE: BAD_SEQID means the server and client disagree about the
    		 * book-keeping w.r.t. state-changing operations
    		 * (OPEN/CLOSE/LOCK/LOCKU...)
    		 * It is actually a sign of a bug on the client or on the server.
    		 *
    		 * If we receive a BAD_SEQID error in the particular case of
    
    		 * doing an OPEN, we assume that nfs_increment_open_seqid() will
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		 * have unhashed the old state_owner for us, and that we can
    		 * therefore safely retry using a new one. We should still warn
    		 * the user though...
    		 */
    		if (status == -NFS4ERR_BAD_SEQID) {
    
    			printk(KERN_WARNING "NFS: v4 server %s "
    					" returned a bad sequence-id error!\n",
    					NFS_SERVER(dir)->nfs_client->cl_hostname);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			exception.retry = 1;
    			continue;
    		}
    
    		/*
    		 * BAD_STATEID on OPEN means that the server cancelled our
    		 * state before it received the OPEN_CONFIRM.
    		 * Recover by retrying the request as per the discussion
    		 * on Page 181 of RFC3530.
    		 */
    		if (status == -NFS4ERR_BAD_STATEID) {
    			exception.retry = 1;
    			continue;
    		}
    
    		if (status == -EAGAIN) {
    			/* We must have found a delegation */
    			exception.retry = 1;
    			continue;
    		}
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
    					status, &exception));
    	} while (exception.retry);
    	return res;
    }
    
    
    static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
    			    struct nfs_fattr *fattr, struct iattr *sattr,
    			    struct nfs4_state *state)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct nfs_server *server = NFS_SERVER(inode);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
            struct nfs_setattrargs  arg = {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
                    .iap            = sattr,
    		.server		= server,
    		.bitmask = server->attr_bitmask,
            };
            struct nfs_setattrres  res = {
    		.fattr		= fattr,
    		.server		= server,
            };
            struct rpc_message msg = {
    
    		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
    		.rpc_argp	= &arg,
    		.rpc_resp	= &res,
    		.rpc_cred	= cred,
    
    Linus Torvalds's avatar
    Linus Torvalds committed
            };
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	nfs_fattr_init(fattr);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
    		/* Use that stateid */
    	} else if (state != NULL) {
    
    		nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
    
    
    	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
    
    	if (status == 0 && state != NULL)
    		renew_lease(server, timestamp);
    
    static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
    			   struct nfs_fattr *fattr, struct iattr *sattr,
    			   struct nfs4_state *state)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct nfs_server *server = NFS_SERVER(inode);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct nfs4_exception exception = { };
    	int err;
    	do {
    		err = nfs4_handle_exception(server,
    
    				_nfs4_do_setattr(inode, cred, fattr, sattr, state),
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    				&exception);
    	} while (exception.retry);
    	return err;
    }
    
    struct nfs4_closedata {
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct inode *inode;
    	struct nfs4_state *state;
    	struct nfs_closeargs arg;
    	struct nfs_closeres res;
    
    Fred Isaman's avatar
    Fred Isaman committed
    	bool roc;
    	u32 roc_barrier;
    
    static void nfs4_free_closedata(void *data)
    
    	struct nfs4_closedata *calldata = data;
    	struct nfs4_state_owner *sp = calldata->state->owner;
    
    Fred Isaman's avatar
    Fred Isaman committed
    	if (calldata->roc)
    		pnfs_roc_release(calldata->state->inode);
    
    	nfs4_put_open_state(calldata->state);
    	nfs_free_seqid(calldata->arg.seqid);
    	nfs4_put_state_owner(sp);
    
    	path_put(&calldata->path);
    
    static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
    		fmode_t fmode)
    {
    	spin_lock(&state->owner->so_lock);
    	if (!(fmode & FMODE_READ))
    		clear_bit(NFS_O_RDONLY_STATE, &state->flags);
    	if (!(fmode & FMODE_WRITE))
    		clear_bit(NFS_O_WRONLY_STATE, &state->flags);
    	clear_bit(NFS_O_RDWR_STATE, &state->flags);
    	spin_unlock(&state->owner->so_lock);
    }
    
    
    static void nfs4_close_done(struct rpc_task *task, void *data)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct nfs4_closedata *calldata = data;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	struct nfs4_state *state = calldata->state;
    	struct nfs_server *server = NFS_SERVER(calldata->inode);
    
    
    	if (!nfs4_sequence_done(task, &calldata->res.seq_res))
    		return;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
            /* hmm. we are done with the inode, and in the process of freeing
    	 * the state_owner. we keep this around to process errors
    	 */
    	switch (task->tk_status) {
    		case 0:
    
    Fred Isaman's avatar
    Fred Isaman committed
    			if (calldata->roc)
    				pnfs_roc_set_barrier(state->inode,
    						     calldata->roc_barrier);
    
    			nfs_set_open_stateid(state, &calldata->res.stateid, 0);
    
    			renew_lease(server, calldata->timestamp);
    
    			nfs4_close_clear_stateid_flags(state,
    					calldata->arg.fmode);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    			break;
    		case -NFS4ERR_STALE_STATEID:
    
    		case -NFS4ERR_OLD_STATEID:
    		case -NFS4ERR_BAD_STATEID:
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		case -NFS4ERR_EXPIRED:
    
    			if (calldata->arg.fmode == 0)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		default:
    
    			if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
    				rpc_restart_call_prepare(task);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	}
    
    	nfs_release_seqid(calldata->arg.seqid);
    
    	nfs_refresh_inode(calldata->inode, calldata->res.fattr);
    
    static void nfs4_close_prepare(struct rpc_task *task, void *data)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct nfs4_closedata *calldata = data;
    
    	struct nfs4_state *state = calldata->state;
    
    	if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
    
    	task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
    	calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
    
    	spin_lock(&state->owner->so_lock);
    
    	/* Calculate the change in open mode */
    
    		if (state->n_rdonly == 0) {
    
    			call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
    			call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
    			calldata->arg.fmode &= ~FMODE_READ;
    
    		}
    		if (state->n_wronly == 0) {
    
    			call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
    			call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
    			calldata->arg.fmode &= ~FMODE_WRITE;
    
    	spin_unlock(&state->owner->so_lock);
    
    		/* Note: exit _without_ calling nfs4_close_done */
    		task->tk_action = NULL;
    
    Fred Isaman's avatar
    Fred Isaman committed
    	if (calldata->arg.fmode == 0) {
    
    		task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
    
    Fred Isaman's avatar
    Fred Isaman committed
    		if (calldata->roc &&
    		    pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
    			rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
    				     task, NULL);
    			return;
    		}
    	}
    
    	nfs_fattr_init(calldata->res.fattr);
    
    	if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
    
    				&calldata->arg.seq_args, &calldata->res.seq_res,
    				1, task))
    		return;
    
    	rpc_call_start(task);
    
    static const struct rpc_call_ops nfs4_close_ops = {
    
    	.rpc_call_prepare = nfs4_close_prepare,
    
    	.rpc_call_done = nfs4_close_done,