blob: aed8701c1a3641d95a7b0ae7bdfe30c34688ae41 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
39#include <linux/utsname.h>
40#include <linux/delay.h>
41#include <linux/errno.h>
42#include <linux/string.h>
43#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
48#include <linux/smp_lock.h>
49#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070050#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Trond Myklebust4ce79712005-06-22 17:16:21 +000052#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "delegation.h"
54
55#define NFSDBG_FACILITY NFSDBG_PROC
56
57#define NFS4_POLL_RETRY_MIN (1*HZ)
58#define NFS4_POLL_RETRY_MAX (15*HZ)
59
Trond Myklebust0a8838f2005-10-18 14:20:14 -070060static int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nfs_fh *fh, struct nfs4_state_owner *sp, nfs4_stateid *stateid, struct nfs_seqid *seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070062static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
Trond Myklebustfaf5f492005-10-18 14:20:15 -070064static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
66extern struct rpc_procinfo nfs4_procedures[];
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* Prevent leaks of NFSv4 errors into userland */
69int nfs4_map_errors(int err)
70{
71 if (err < -1000) {
72 dprintk("%s could not handle NFSv4 error %d\n",
73 __FUNCTION__, -err);
74 return -EIO;
75 }
76 return err;
77}
78
79/*
80 * This is our standard bitmap for GETATTR requests.
81 */
82const u32 nfs4_fattr_bitmap[2] = {
83 FATTR4_WORD0_TYPE
84 | FATTR4_WORD0_CHANGE
85 | FATTR4_WORD0_SIZE
86 | FATTR4_WORD0_FSID
87 | FATTR4_WORD0_FILEID,
88 FATTR4_WORD1_MODE
89 | FATTR4_WORD1_NUMLINKS
90 | FATTR4_WORD1_OWNER
91 | FATTR4_WORD1_OWNER_GROUP
92 | FATTR4_WORD1_RAWDEV
93 | FATTR4_WORD1_SPACE_USED
94 | FATTR4_WORD1_TIME_ACCESS
95 | FATTR4_WORD1_TIME_METADATA
96 | FATTR4_WORD1_TIME_MODIFY
97};
98
99const u32 nfs4_statfs_bitmap[2] = {
100 FATTR4_WORD0_FILES_AVAIL
101 | FATTR4_WORD0_FILES_FREE
102 | FATTR4_WORD0_FILES_TOTAL,
103 FATTR4_WORD1_SPACE_AVAIL
104 | FATTR4_WORD1_SPACE_FREE
105 | FATTR4_WORD1_SPACE_TOTAL
106};
107
Trond Myklebust4ce79712005-06-22 17:16:21 +0000108const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 FATTR4_WORD0_MAXLINK
110 | FATTR4_WORD0_MAXNAME,
111 0
112};
113
114const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
115 | FATTR4_WORD0_MAXREAD
116 | FATTR4_WORD0_MAXWRITE
117 | FATTR4_WORD0_LEASE_TIME,
118 0
119};
120
121static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
122 struct nfs4_readdir_arg *readdir)
123{
124 u32 *start, *p;
125
126 BUG_ON(readdir->count < 80);
127 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000128 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
130 return;
131 }
132
133 readdir->cookie = 0;
134 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
135 if (cookie == 2)
136 return;
137
138 /*
139 * NFSv4 servers do not return entries for '.' and '..'
140 * Therefore, we fake these entries here. We let '.'
141 * have cookie 0 and '..' have cookie 1. Note that
142 * when talking to the server, we always send cookie 0
143 * instead of 1 or 2.
144 */
145 start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
146
147 if (cookie == 0) {
148 *p++ = xdr_one; /* next */
149 *p++ = xdr_zero; /* cookie, first word */
150 *p++ = xdr_one; /* cookie, second word */
151 *p++ = xdr_one; /* entry len */
152 memcpy(p, ".\0\0\0", 4); /* entry */
153 p++;
154 *p++ = xdr_one; /* bitmap length */
155 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
156 *p++ = htonl(8); /* attribute buffer length */
157 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
158 }
159
160 *p++ = xdr_one; /* next */
161 *p++ = xdr_zero; /* cookie, first word */
162 *p++ = xdr_two; /* cookie, second word */
163 *p++ = xdr_two; /* entry len */
164 memcpy(p, "..\0\0", 4); /* entry */
165 p++;
166 *p++ = xdr_one; /* bitmap length */
167 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
168 *p++ = htonl(8); /* attribute buffer length */
169 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
170
171 readdir->pgbase = (char *)p - (char *)start;
172 readdir->count -= readdir->pgbase;
173 kunmap_atomic(start, KM_USER0);
174}
175
176static void
177renew_lease(struct nfs_server *server, unsigned long timestamp)
178{
179 struct nfs4_client *clp = server->nfs4_state;
180 spin_lock(&clp->cl_lock);
181 if (time_before(clp->cl_last_renewal,timestamp))
182 clp->cl_last_renewal = timestamp;
183 spin_unlock(&clp->cl_lock);
184}
185
186static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
187{
188 struct nfs_inode *nfsi = NFS_I(inode);
189
Trond Myklebustdecf4912005-10-27 22:12:39 -0400190 spin_lock(&inode->i_lock);
191 nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
193 nfsi->change_attr = cinfo->after;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400194 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100197struct nfs4_opendata {
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100198 atomic_t count;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100199 struct nfs_openargs o_arg;
200 struct nfs_openres o_res;
201 struct nfs_fattr f_attr;
202 struct nfs_fattr dir_attr;
203 struct dentry *dentry;
204 struct dentry *dir;
205 struct nfs4_state_owner *owner;
206 struct iattr attrs;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100207 int rpc_status;
208 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100209};
210
211static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
212 struct nfs4_state_owner *sp, int flags,
213 const struct iattr *attrs)
214{
215 struct dentry *parent = dget_parent(dentry);
216 struct inode *dir = parent->d_inode;
217 struct nfs_server *server = NFS_SERVER(dir);
218 struct nfs4_opendata *p;
219
220 p = kzalloc(sizeof(*p), GFP_KERNEL);
221 if (p == NULL)
222 goto err;
223 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
224 if (p->o_arg.seqid == NULL)
225 goto err_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100226 atomic_set(&p->count, 1);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100227 p->dentry = dget(dentry);
228 p->dir = parent;
229 p->owner = sp;
230 atomic_inc(&sp->so_count);
231 p->o_arg.fh = NFS_FH(dir);
232 p->o_arg.open_flags = flags,
233 p->o_arg.clientid = server->nfs4_state->cl_clientid;
234 p->o_arg.id = sp->so_id;
235 p->o_arg.name = &dentry->d_name;
236 p->o_arg.server = server;
237 p->o_arg.bitmask = server->attr_bitmask;
238 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
239 p->o_res.f_attr = &p->f_attr;
240 p->o_res.dir_attr = &p->dir_attr;
241 p->o_res.server = server;
242 nfs_fattr_init(&p->f_attr);
243 nfs_fattr_init(&p->dir_attr);
244 if (flags & O_EXCL) {
245 u32 *s = (u32 *) p->o_arg.u.verifier.data;
246 s[0] = jiffies;
247 s[1] = current->pid;
248 } else if (flags & O_CREAT) {
249 p->o_arg.u.attrs = &p->attrs;
250 memcpy(&p->attrs, attrs, sizeof(p->attrs));
251 }
252 return p;
253err_free:
254 kfree(p);
255err:
256 dput(parent);
257 return NULL;
258}
259
260static void nfs4_opendata_free(struct nfs4_opendata *p)
261{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100262 if (p != NULL && atomic_dec_and_test(&p->count)) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100263 nfs_free_seqid(p->o_arg.seqid);
264 nfs4_put_state_owner(p->owner);
265 dput(p->dir);
266 dput(p->dentry);
267 kfree(p);
268 }
269}
270
Trond Myklebust95121352005-10-18 14:20:12 -0700271/* Helper for asynchronous RPC calls */
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100272static int nfs4_call_async(struct rpc_clnt *clnt,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100273 const struct rpc_call_ops *tk_ops, void *calldata)
Trond Myklebust95121352005-10-18 14:20:12 -0700274{
275 struct rpc_task *task;
276
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100277 if (!(task = rpc_new_task(clnt, RPC_TASK_ASYNC, tk_ops, calldata)))
Trond Myklebust95121352005-10-18 14:20:12 -0700278 return -ENOMEM;
Trond Myklebust95121352005-10-18 14:20:12 -0700279 rpc_execute(task);
280 return 0;
281}
282
Trond Myklebust06f814a2006-01-03 09:55:07 +0100283static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
284{
285 sigset_t oldset;
286 int ret;
287
288 rpc_clnt_sigmask(task->tk_client, &oldset);
289 ret = rpc_wait_for_completion_task(task);
290 rpc_clnt_sigunmask(task->tk_client, &oldset);
291 return ret;
292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
295{
296 struct inode *inode = state->inode;
297
298 open_flags &= (FMODE_READ|FMODE_WRITE);
Trond Myklebustd5308382005-11-04 15:33:38 -0500299 /* Protect against nfs4_find_state_byowner() */
Trond Myklebustec073422005-10-20 14:22:47 -0700300 spin_lock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 spin_lock(&inode->i_lock);
Trond Myklebust4cecb762005-11-04 15:32:58 -0500302 memcpy(&state->stateid, stateid, sizeof(state->stateid));
303 if ((open_flags & FMODE_WRITE))
304 state->nwriters++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (open_flags & FMODE_READ)
306 state->nreaders++;
Trond Myklebust4cecb762005-11-04 15:32:58 -0500307 nfs4_state_set_mode_locked(state, state->state | open_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 spin_unlock(&inode->i_lock);
Trond Myklebustec073422005-10-20 14:22:47 -0700309 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100312static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
313{
314 struct inode *inode;
315 struct nfs4_state *state = NULL;
316
317 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
318 goto out;
319 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
320 if (inode == NULL)
321 goto out;
322 state = nfs4_get_open_state(inode, data->owner);
323 if (state == NULL)
324 goto put_inode;
325 update_open_stateid(state, &data->o_res.stateid, data->o_arg.open_flags);
326put_inode:
327 iput(inode);
328out:
329 return state;
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/*
333 * OPEN_RECLAIM:
334 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
336static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
337{
338 struct inode *inode = state->inode;
339 struct nfs_server *server = NFS_SERVER(inode);
340 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
341 struct nfs_openargs o_arg = {
342 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 .id = sp->so_id,
344 .open_flags = state->state,
345 .clientid = server->nfs4_state->cl_clientid,
346 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
347 .bitmask = server->attr_bitmask,
348 };
349 struct nfs_openres o_res = {
350 .server = server, /* Grrr */
351 };
352 struct rpc_message msg = {
353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
354 .rpc_argp = &o_arg,
355 .rpc_resp = &o_res,
356 .rpc_cred = sp->so_cred,
357 };
358 int status;
359
360 if (delegation != NULL) {
361 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
362 memcpy(&state->stateid, &delegation->stateid,
363 sizeof(state->stateid));
364 set_bit(NFS_DELEGATED_STATE, &state->flags);
365 return 0;
366 }
367 o_arg.u.delegation_type = delegation->type;
368 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700369 o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
370 if (o_arg.seqid == NULL)
371 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700373 /* Confirm the sequence as being established */
374 nfs_confirm_seqid(&sp->so_seqid, status);
375 nfs_increment_open_seqid(status, o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (status == 0) {
377 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
378 if (o_res.delegation_type != 0) {
379 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
380 /* Did the server issue an immediate delegation recall? */
381 if (o_res.do_recall)
382 nfs_async_inode_return_delegation(inode, &o_res.stateid);
383 }
384 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700385 nfs_free_seqid(o_arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 clear_bit(NFS_DELEGATED_STATE, &state->flags);
387 /* Ensure we update the inode attributes */
388 NFS_CACHEINV(inode);
389 return status;
390}
391
392static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
393{
394 struct nfs_server *server = NFS_SERVER(state->inode);
395 struct nfs4_exception exception = { };
396 int err;
397 do {
398 err = _nfs4_open_reclaim(sp, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000399 if (err != -NFS4ERR_DELAY)
400 break;
401 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 } while (exception.retry);
403 return err;
404}
405
406static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
407{
408 struct nfs4_state_owner *sp = state->owner;
409 struct inode *inode = dentry->d_inode;
410 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct rpc_message msg = {
412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 .rpc_cred = sp->so_cred,
414 };
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100415 struct nfs4_opendata *opendata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 int status = 0;
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
419 goto out;
420 if (state->state == 0)
421 goto out;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100422 opendata = nfs4_opendata_alloc(dentry, sp, state->state, NULL);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700423 status = -ENOMEM;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100424 if (opendata == NULL)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700425 goto out;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100426 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
427 msg.rpc_argp = &opendata->o_arg;
428 msg.rpc_resp = &opendata->o_res;
429 memcpy(opendata->o_arg.u.delegation.data, state->stateid.data,
430 sizeof(opendata->o_arg.u.delegation.data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100432 nfs_increment_open_seqid(status, opendata->o_arg.seqid);
Trond Myklebust0a8838f2005-10-18 14:20:14 -0700433 if (status != 0)
434 goto out_free;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100435 if(opendata->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebust0a8838f2005-10-18 14:20:14 -0700436 status = _nfs4_proc_open_confirm(server->client, NFS_FH(inode),
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100437 sp, &opendata->o_res.stateid, opendata->o_arg.seqid);
Trond Myklebust0a8838f2005-10-18 14:20:14 -0700438 if (status != 0)
439 goto out_free;
440 }
441 nfs_confirm_seqid(&sp->so_seqid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (status >= 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100443 memcpy(state->stateid.data, opendata->o_res.stateid.data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 sizeof(state->stateid.data));
445 clear_bit(NFS_DELEGATED_STATE, &state->flags);
446 }
Trond Myklebust0a8838f2005-10-18 14:20:14 -0700447out_free:
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100448 nfs4_opendata_free(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return status;
451}
452
453int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
454{
455 struct nfs4_exception exception = { };
456 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
457 int err;
458 do {
459 err = _nfs4_open_delegation_recall(dentry, state);
460 switch (err) {
461 case 0:
462 return err;
463 case -NFS4ERR_STALE_CLIENTID:
464 case -NFS4ERR_STALE_STATEID:
465 case -NFS4ERR_EXPIRED:
466 /* Don't recall a delegation if it was lost */
467 nfs4_schedule_state_recovery(server->nfs4_state);
468 return err;
469 }
470 err = nfs4_handle_exception(server, err, &exception);
471 } while (exception.retry);
472 return err;
473}
474
Trond Myklebust0a8838f2005-10-18 14:20:14 -0700475static int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nfs_fh *fh, struct nfs4_state_owner *sp, nfs4_stateid *stateid, struct nfs_seqid *seqid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct nfs_open_confirmargs arg = {
478 .fh = fh,
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700479 .seqid = seqid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 .stateid = *stateid,
481 };
482 struct nfs_open_confirmres res;
483 struct rpc_message msg = {
484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
485 .rpc_argp = &arg,
486 .rpc_resp = &res,
487 .rpc_cred = sp->so_cred,
488 };
489 int status;
490
491 status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700492 /* Confirm the sequence as being established */
493 nfs_confirm_seqid(&sp->so_seqid, status);
494 nfs_increment_open_seqid(status, seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (status >= 0)
496 memcpy(stateid, &res.stateid, sizeof(*stateid));
497 return status;
498}
499
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100500static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100502 struct nfs4_opendata *data = calldata;
503 struct nfs4_state_owner *sp = data->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct rpc_message msg = {
505 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100506 .rpc_argp = &data->o_arg,
507 .rpc_resp = &data->o_res,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 .rpc_cred = sp->so_cred,
509 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100510
511 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
512 return;
513 /* Update sequence id. */
514 data->o_arg.id = sp->so_id;
515 data->o_arg.clientid = sp->so_client->cl_clientid;
516 rpc_call_setup(task, &msg, 0);
517}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100519static void nfs4_open_done(struct rpc_task *task, void *calldata)
520{
521 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100523 data->rpc_status = task->tk_status;
524 if (RPC_ASSASSINATED(task))
525 return;
526 if (task->tk_status == 0) {
527 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -0700528 case S_IFREG:
529 break;
530 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100531 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700532 break;
533 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100534 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700535 break;
536 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100537 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -0700538 }
539 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100540 nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid);
541}
Trond Myklebust6f926b52005-10-18 14:20:18 -0700542
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100543static void nfs4_open_release(void *calldata)
544{
545 struct nfs4_opendata *data = calldata;
546 struct nfs4_state *state = NULL;
547
548 /* If this request hasn't been cancelled, do nothing */
549 if (data->cancelled == 0)
550 goto out_free;
551 /* In case of error, no cleanup! */
552 if (data->rpc_status != 0)
553 goto out_free;
554 /* In case we need an open_confirm, no cleanup! */
555 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
556 goto out_free;
557 nfs_confirm_seqid(&data->owner->so_seqid, 0);
558 state = nfs4_opendata_to_nfs4_state(data);
559 if (state != NULL)
560 nfs4_close_state(state, data->o_arg.open_flags);
561out_free:
562 nfs4_opendata_free(data);
563}
564
565static const struct rpc_call_ops nfs4_open_ops = {
566 .rpc_call_prepare = nfs4_open_prepare,
567 .rpc_call_done = nfs4_open_done,
568 .rpc_release = nfs4_open_release,
569};
570
571/*
572 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
573 */
574static int _nfs4_proc_open(struct nfs4_opendata *data)
575{
576 struct inode *dir = data->dir->d_inode;
577 struct nfs_server *server = NFS_SERVER(dir);
578 struct nfs_openargs *o_arg = &data->o_arg;
579 struct nfs_openres *o_res = &data->o_res;
580 struct rpc_task *task;
581 int status;
582
583 atomic_inc(&data->count);
584 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data);
585 if (IS_ERR(task)) {
586 nfs4_opendata_free(data);
587 return PTR_ERR(task);
588 }
589 status = nfs4_wait_for_completion_rpc_task(task);
590 if (status != 0) {
591 data->cancelled = 1;
592 smp_wmb();
593 } else
594 status = data->rpc_status;
595 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100597 return status;
598
Trond Myklebust56ae19f2005-10-27 22:12:40 -0400599 if (o_arg->open_flags & O_CREAT) {
600 update_changeattr(dir, &o_res->cinfo);
601 nfs_post_op_update_inode(dir, o_res->dir_attr);
602 } else
603 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
605 status = _nfs4_proc_open_confirm(server->client, &o_res->fh,
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100606 data->owner, &o_res->stateid, o_arg->seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100608 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100610 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100612 return server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
617{
618 struct nfs_access_entry cache;
619 int mask = 0;
620 int status;
621
622 if (openflags & FMODE_READ)
623 mask |= MAY_READ;
624 if (openflags & FMODE_WRITE)
625 mask |= MAY_WRITE;
626 status = nfs_access_get_cached(inode, cred, &cache);
627 if (status == 0)
628 goto out;
629
630 /* Be clever: ask server to check for all possible rights */
631 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
632 cache.cred = cred;
633 cache.jiffies = jiffies;
634 status = _nfs4_proc_access(inode, &cache);
635 if (status != 0)
636 return status;
637 nfs_access_add_cache(inode, &cache);
638out:
639 if ((cache.mask & mask) == mask)
640 return 0;
641 return -EACCES;
642}
643
644/*
645 * OPEN_EXPIRED:
646 * reclaim state on the server after a network partition.
647 * Assumes caller holds the appropriate lock
648 */
649static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
650{
651 struct dentry *parent = dget_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 struct inode *inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100654 struct nfs4_opendata *opendata;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100655 struct nfs4_state *newstate;
656 int openflags = state->state & (FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 int status = 0;
658
659 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100660 status = _nfs4_do_access(inode, sp->so_cred, openflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (status < 0)
662 goto out;
663 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
664 set_bit(NFS_DELEGATED_STATE, &state->flags);
665 goto out;
666 }
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700667 status = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100668 opendata = nfs4_opendata_alloc(dentry, sp, openflags, NULL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100669 if (opendata == NULL)
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700670 goto out;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100671 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (status != 0)
673 goto out_nodeleg;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100674 newstate = nfs4_opendata_to_nfs4_state(opendata);
675 if (newstate != state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 goto out_stale;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100677 if (opendata->o_res.delegation_type != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100679 nfs_inode_set_delegation(inode, sp->so_cred,
680 &opendata->o_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 else
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100682 nfs_inode_reclaim_delegation(inode, sp->so_cred,
683 &opendata->o_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100685out_close_state:
686 nfs4_close_state(newstate, openflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687out_nodeleg:
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100688 nfs4_opendata_free(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 clear_bit(NFS_DELEGATED_STATE, &state->flags);
690out:
691 dput(parent);
692 return status;
693out_stale:
694 status = -ESTALE;
695 /* Invalidate the state owner so we don't ever use it again */
696 nfs4_drop_state_owner(sp);
697 d_drop(dentry);
698 /* Should we be trying to close that stateid? */
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100699 goto out_close_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Trond Myklebust202b50d2005-06-22 17:16:29 +0000702static inline int nfs4_do_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
703{
704 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
705 struct nfs4_exception exception = { };
706 int err;
707
708 do {
709 err = _nfs4_open_expired(sp, state, dentry);
710 if (err == -NFS4ERR_DELAY)
711 nfs4_handle_exception(server, err, &exception);
712 } while (exception.retry);
713 return err;
714}
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
717{
718 struct nfs_inode *nfsi = NFS_I(state->inode);
719 struct nfs_open_context *ctx;
720 int status;
721
722 spin_lock(&state->inode->i_lock);
723 list_for_each_entry(ctx, &nfsi->open_files, list) {
724 if (ctx->state != state)
725 continue;
726 get_nfs_open_context(ctx);
727 spin_unlock(&state->inode->i_lock);
Trond Myklebust202b50d2005-06-22 17:16:29 +0000728 status = nfs4_do_open_expired(sp, state, ctx->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 put_nfs_open_context(ctx);
730 return status;
731 }
732 spin_unlock(&state->inode->i_lock);
733 return -ENOENT;
734}
735
736/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100737 * Returns a referenced nfs4_state if there is an open delegation on the file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 */
739static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
740{
741 struct nfs_delegation *delegation;
742 struct nfs_server *server = NFS_SERVER(inode);
743 struct nfs4_client *clp = server->nfs4_state;
744 struct nfs_inode *nfsi = NFS_I(inode);
745 struct nfs4_state_owner *sp = NULL;
746 struct nfs4_state *state = NULL;
747 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
748 int err;
749
750 /* Protect against reboot recovery - NOTE ORDER! */
751 down_read(&clp->cl_sem);
752 /* Protect against delegation recall */
753 down_read(&nfsi->rwsem);
754 delegation = NFS_I(inode)->delegation;
755 err = -ENOENT;
756 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
757 goto out_err;
758 err = -ENOMEM;
759 if (!(sp = nfs4_get_state_owner(server, cred))) {
760 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
761 goto out_err;
762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 state = nfs4_get_open_state(inode, sp);
764 if (state == NULL)
765 goto out_err;
766
767 err = -ENOENT;
768 if ((state->state & open_flags) == open_flags) {
769 spin_lock(&inode->i_lock);
770 if (open_flags & FMODE_READ)
771 state->nreaders++;
772 if (open_flags & FMODE_WRITE)
773 state->nwriters++;
774 spin_unlock(&inode->i_lock);
775 goto out_ok;
776 } else if (state->state != 0)
777 goto out_err;
778
779 lock_kernel();
780 err = _nfs4_do_access(inode, cred, open_flags);
781 unlock_kernel();
782 if (err != 0)
783 goto out_err;
784 set_bit(NFS_DELEGATED_STATE, &state->flags);
785 update_open_stateid(state, &delegation->stateid, open_flags);
786out_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 nfs4_put_state_owner(sp);
788 up_read(&nfsi->rwsem);
789 up_read(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 *res = state;
791 return 0;
792out_err:
793 if (sp != NULL) {
794 if (state != NULL)
795 nfs4_put_open_state(state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 nfs4_put_state_owner(sp);
797 }
798 up_read(&nfsi->rwsem);
799 up_read(&clp->cl_sem);
Trond Myklebustb8e5c4c2005-10-18 14:20:20 -0700800 if (err != -EACCES)
801 nfs_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return err;
803}
804
805static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
806{
807 struct nfs4_exception exception = { };
808 struct nfs4_state *res;
809 int err;
810
811 do {
812 err = _nfs4_open_delegated(inode, flags, cred, &res);
813 if (err == 0)
814 break;
815 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
816 err, &exception));
817 } while (exception.retry);
818 return res;
819}
820
821/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100822 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 */
824static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
825{
826 struct nfs4_state_owner *sp;
827 struct nfs4_state *state = NULL;
828 struct nfs_server *server = NFS_SERVER(dir);
829 struct nfs4_client *clp = server->nfs4_state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 struct nfs4_opendata *opendata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /* Protect against reboot recovery conflicts */
834 down_read(&clp->cl_sem);
835 status = -ENOMEM;
836 if (!(sp = nfs4_get_state_owner(server, cred))) {
837 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
838 goto out_err;
839 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100840 opendata = nfs4_opendata_alloc(dentry, sp, flags, sattr);
841 if (opendata == NULL)
842 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100844 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (status != 0)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100846 goto err_opendata_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 status = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100849 state = nfs4_opendata_to_nfs4_state(opendata);
850 if (state == NULL)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100851 goto err_opendata_free;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852 if (opendata->o_res.delegation_type != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100853 nfs_inode_set_delegation(state->inode, cred, &opendata->o_res);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100854 nfs4_opendata_free(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 nfs4_put_state_owner(sp);
856 up_read(&clp->cl_sem);
857 *res = state;
858 return 0;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100859err_opendata_free:
860 nfs4_opendata_free(opendata);
861err_put_state_owner:
862 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
865 up_read(&clp->cl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 *res = NULL;
867 return status;
868}
869
870
871static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
872{
873 struct nfs4_exception exception = { };
874 struct nfs4_state *res;
875 int status;
876
877 do {
878 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
879 if (status == 0)
880 break;
881 /* NOTE: BAD_SEQID means the server and client disagree about the
882 * book-keeping w.r.t. state-changing operations
883 * (OPEN/CLOSE/LOCK/LOCKU...)
884 * It is actually a sign of a bug on the client or on the server.
885 *
886 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700887 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 * have unhashed the old state_owner for us, and that we can
889 * therefore safely retry using a new one. We should still warn
890 * the user though...
891 */
892 if (status == -NFS4ERR_BAD_SEQID) {
893 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
894 exception.retry = 1;
895 continue;
896 }
Trond Myklebust550f5742005-10-18 14:20:21 -0700897 /*
898 * BAD_STATEID on OPEN means that the server cancelled our
899 * state before it received the OPEN_CONFIRM.
900 * Recover by retrying the request as per the discussion
901 * on Page 181 of RFC3530.
902 */
903 if (status == -NFS4ERR_BAD_STATEID) {
904 exception.retry = 1;
905 continue;
906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
908 status, &exception));
909 } while (exception.retry);
910 return res;
911}
912
913static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
914 struct nfs_fh *fhandle, struct iattr *sattr,
915 struct nfs4_state *state)
916{
917 struct nfs_setattrargs arg = {
918 .fh = fhandle,
919 .iap = sattr,
920 .server = server,
921 .bitmask = server->attr_bitmask,
922 };
923 struct nfs_setattrres res = {
924 .fattr = fattr,
925 .server = server,
926 };
927 struct rpc_message msg = {
928 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
929 .rpc_argp = &arg,
930 .rpc_resp = &res,
931 };
Trond Myklebust65e43082005-08-16 11:49:44 -0400932 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Trond Myklebust0e574af2005-10-27 22:12:38 -0400934 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Trond Myklebust08e9eac2005-06-22 17:16:29 +0000936 if (state != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 msg.rpc_cred = state->owner->so_cred;
Trond Myklebust08e9eac2005-06-22 17:16:29 +0000938 nfs4_copy_stateid(&arg.stateid, state, current->files);
939 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
941
Trond Myklebust65e43082005-08-16 11:49:44 -0400942 status = rpc_call_sync(server->client, &msg, 0);
943 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
946static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
947 struct nfs_fh *fhandle, struct iattr *sattr,
948 struct nfs4_state *state)
949{
950 struct nfs4_exception exception = { };
951 int err;
952 do {
953 err = nfs4_handle_exception(server,
954 _nfs4_do_setattr(server, fattr, fhandle, sattr,
955 state),
956 &exception);
957 } while (exception.retry);
958 return err;
959}
960
961struct nfs4_closedata {
962 struct inode *inode;
963 struct nfs4_state *state;
964 struct nfs_closeargs arg;
965 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -0400966 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967};
968
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100969static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -0700970{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100971 struct nfs4_closedata *calldata = data;
972 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -0700973
974 nfs4_put_open_state(calldata->state);
975 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -0700976 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -0700977 kfree(calldata);
978}
979
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100980static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100982 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 struct nfs_server *server = NFS_SERVER(calldata->inode);
985
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100986 if (RPC_ASSASSINATED(task))
987 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /* hmm. we are done with the inode, and in the process of freeing
989 * the state_owner. we keep this around to process errors
990 */
Trond Myklebustcee54fc2005-10-18 14:20:12 -0700991 nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 switch (task->tk_status) {
993 case 0:
994 memcpy(&state->stateid, &calldata->res.stateid,
995 sizeof(state->stateid));
996 break;
997 case -NFS4ERR_STALE_STATEID:
998 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 nfs4_schedule_state_recovery(server->nfs4_state);
1000 break;
1001 default:
1002 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
1003 rpc_restart_call(task);
1004 return;
1005 }
1006 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001007 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001010static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001012 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001013 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 struct rpc_message msg = {
1015 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1016 .rpc_argp = &calldata->arg,
1017 .rpc_resp = &calldata->res,
Trond Myklebust95121352005-10-18 14:20:12 -07001018 .rpc_cred = state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 };
Trond Myklebust4cecb762005-11-04 15:32:58 -05001020 int mode = 0, old_mode;
Trond Myklebust95121352005-10-18 14:20:12 -07001021
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001022 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001023 return;
Trond Myklebust95121352005-10-18 14:20:12 -07001024 /* Recalculate the new open mode in case someone reopened the file
1025 * while we were waiting in line to be scheduled.
1026 */
Trond Myklebust4cecb762005-11-04 15:32:58 -05001027 spin_lock(&state->owner->so_lock);
1028 spin_lock(&calldata->inode->i_lock);
1029 mode = old_mode = state->state;
1030 if (state->nreaders == 0)
1031 mode &= ~FMODE_READ;
1032 if (state->nwriters == 0)
1033 mode &= ~FMODE_WRITE;
1034 nfs4_state_set_mode_locked(state, mode);
1035 spin_unlock(&calldata->inode->i_lock);
1036 spin_unlock(&state->owner->so_lock);
1037 if (mode == old_mode || test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001038 /* Note: exit _without_ calling nfs4_close_done */
1039 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001040 return;
1041 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001042 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust95121352005-10-18 14:20:12 -07001043 if (mode != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust95121352005-10-18 14:20:12 -07001045 calldata->arg.open_flags = mode;
1046 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001049static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001050 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001051 .rpc_call_done = nfs4_close_done,
1052 .rpc_release = nfs4_free_closedata,
1053};
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055/*
1056 * It is possible for data to be read/written from a mem-mapped file
1057 * after the sys_close call (which hits the vfs layer as a flush).
1058 * This means that we can't safely call nfsv4 close on a file until
1059 * the inode is cleared. This in turn means that we are not good
1060 * NFSv4 citizens - we do not indicate to the server to update the file's
1061 * share state even when we are done with one of the three share
1062 * stateid's in the inode.
1063 *
1064 * NOTE: Caller must be holding the sp->so_owner semaphore!
1065 */
Trond Myklebust4cecb762005-11-04 15:32:58 -05001066int nfs4_do_close(struct inode *inode, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Trond Myklebust516a6af2005-10-27 22:12:41 -04001068 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 struct nfs4_closedata *calldata;
Trond Myklebust95121352005-10-18 14:20:12 -07001070 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Trond Myklebust95121352005-10-18 14:20:12 -07001072 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001074 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 calldata->inode = inode;
1076 calldata->state = state;
1077 calldata->arg.fh = NFS_FH(inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001078 calldata->arg.stateid = &state->stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001080 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001081 if (calldata->arg.seqid == NULL)
1082 goto out_free_calldata;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001083 calldata->arg.bitmask = server->attr_bitmask;
1084 calldata->res.fattr = &calldata->fattr;
1085 calldata->res.server = server;
Trond Myklebust95121352005-10-18 14:20:12 -07001086
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001087 status = nfs4_call_async(server->client, &nfs4_close_ops, calldata);
Trond Myklebust95121352005-10-18 14:20:12 -07001088 if (status == 0)
1089 goto out;
1090
1091 nfs_free_seqid(calldata->arg.seqid);
1092out_free_calldata:
1093 kfree(calldata);
1094out:
1095 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Trond Myklebust02a913a2005-10-18 14:20:17 -07001098static void nfs4_intent_set_file(struct nameidata *nd, struct dentry *dentry, struct nfs4_state *state)
1099{
1100 struct file *filp;
1101
1102 filp = lookup_instantiate_filp(nd, dentry, NULL);
1103 if (!IS_ERR(filp)) {
1104 struct nfs_open_context *ctx;
1105 ctx = (struct nfs_open_context *)filp->private_data;
1106 ctx->state = state;
1107 } else
1108 nfs4_close_state(state, nd->intent.open.flags);
1109}
1110
1111struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1113{
1114 struct iattr attr;
1115 struct rpc_cred *cred;
1116 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001117 struct dentry *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if (nd->flags & LOOKUP_CREATE) {
1120 attr.ia_mode = nd->intent.open.create_mode;
1121 attr.ia_valid = ATTR_MODE;
1122 if (!IS_POSIXACL(dir))
1123 attr.ia_mode &= ~current->fs->umask;
1124 } else {
1125 attr.ia_valid = 0;
1126 BUG_ON(nd->intent.open.flags & O_CREAT);
1127 }
1128
1129 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1130 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001131 return (struct dentry *)cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
1133 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001134 if (IS_ERR(state)) {
1135 if (PTR_ERR(state) == -ENOENT)
1136 d_add(dentry, NULL);
1137 return (struct dentry *)state;
1138 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001139 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001140 if (res != NULL)
1141 dentry = res;
1142 nfs4_intent_set_file(nd, dentry, state);
1143 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001147nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 struct rpc_cred *cred;
1150 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1153 if (IS_ERR(cred))
1154 return PTR_ERR(cred);
1155 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
1156 if (IS_ERR(state))
1157 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
1158 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001159 if (IS_ERR(state)) {
1160 switch (PTR_ERR(state)) {
1161 case -EPERM:
1162 case -EACCES:
1163 case -EDQUOT:
1164 case -ENOSPC:
1165 case -EROFS:
1166 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1167 return 1;
1168 case -ENOENT:
1169 if (dentry->d_inode == NULL)
1170 return 1;
1171 }
1172 goto out_drop;
1173 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001174 if (state->inode == dentry->d_inode) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001175 nfs4_intent_set_file(nd, dentry, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return 1;
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 nfs4_close_state(state, openflags);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001179out_drop:
1180 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return 0;
1182}
1183
1184
1185static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1186{
1187 struct nfs4_server_caps_res res = {};
1188 struct rpc_message msg = {
1189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
1190 .rpc_argp = fhandle,
1191 .rpc_resp = &res,
1192 };
1193 int status;
1194
1195 status = rpc_call_sync(server->client, &msg, 0);
1196 if (status == 0) {
1197 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
1198 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
1199 server->caps |= NFS_CAP_ACLS;
1200 if (res.has_links != 0)
1201 server->caps |= NFS_CAP_HARDLINKS;
1202 if (res.has_symlinks != 0)
1203 server->caps |= NFS_CAP_SYMLINKS;
1204 server->acl_bitmask = res.acl_bitmask;
1205 }
1206 return status;
1207}
1208
1209static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
1210{
1211 struct nfs4_exception exception = { };
1212 int err;
1213 do {
1214 err = nfs4_handle_exception(server,
1215 _nfs4_server_capabilities(server, fhandle),
1216 &exception);
1217 } while (exception.retry);
1218 return err;
1219}
1220
1221static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1222 struct nfs_fsinfo *info)
1223{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 struct nfs4_lookup_root_arg args = {
1225 .bitmask = nfs4_fattr_bitmap,
1226 };
1227 struct nfs4_lookup_res res = {
1228 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04001229 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 .fh = fhandle,
1231 };
1232 struct rpc_message msg = {
1233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
1234 .rpc_argp = &args,
1235 .rpc_resp = &res,
1236 };
Trond Myklebust0e574af2005-10-27 22:12:38 -04001237 nfs_fattr_init(info->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return rpc_call_sync(server->client, &msg, 0);
1239}
1240
1241static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
1242 struct nfs_fsinfo *info)
1243{
1244 struct nfs4_exception exception = { };
1245 int err;
1246 do {
1247 err = nfs4_handle_exception(server,
1248 _nfs4_lookup_root(server, fhandle, info),
1249 &exception);
1250 } while (exception.retry);
1251 return err;
1252}
1253
1254static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1255 struct nfs_fsinfo *info)
1256{
1257 struct nfs_fattr * fattr = info->fattr;
1258 unsigned char * p;
1259 struct qstr q;
1260 struct nfs4_lookup_arg args = {
1261 .dir_fh = fhandle,
1262 .name = &q,
1263 .bitmask = nfs4_fattr_bitmap,
1264 };
1265 struct nfs4_lookup_res res = {
1266 .server = server,
1267 .fattr = fattr,
1268 .fh = fhandle,
1269 };
1270 struct rpc_message msg = {
1271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1272 .rpc_argp = &args,
1273 .rpc_resp = &res,
1274 };
1275 int status;
1276
1277 /*
1278 * Now we do a separate LOOKUP for each component of the mount path.
1279 * The LOOKUPs are done separately so that we can conveniently
1280 * catch an ERR_WRONGSEC if it occurs along the way...
1281 */
1282 status = nfs4_lookup_root(server, fhandle, info);
1283 if (status)
1284 goto out;
1285
1286 p = server->mnt_path;
1287 for (;;) {
1288 struct nfs4_exception exception = { };
1289
1290 while (*p == '/')
1291 p++;
1292 if (!*p)
1293 break;
1294 q.name = p;
1295 while (*p && (*p != '/'))
1296 p++;
1297 q.len = p - q.name;
1298
1299 do {
Trond Myklebust0e574af2005-10-27 22:12:38 -04001300 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 status = nfs4_handle_exception(server,
1302 rpc_call_sync(server->client, &msg, 0),
1303 &exception);
1304 } while (exception.retry);
1305 if (status == 0)
1306 continue;
1307 if (status == -ENOENT) {
1308 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1309 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1310 }
1311 break;
1312 }
1313 if (status == 0)
1314 status = nfs4_server_capabilities(server, fhandle);
1315 if (status == 0)
1316 status = nfs4_do_fsinfo(server, fhandle, info);
1317out:
1318 return status;
1319}
1320
1321static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1322{
1323 struct nfs4_getattr_arg args = {
1324 .fh = fhandle,
1325 .bitmask = server->attr_bitmask,
1326 };
1327 struct nfs4_getattr_res res = {
1328 .fattr = fattr,
1329 .server = server,
1330 };
1331 struct rpc_message msg = {
1332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1333 .rpc_argp = &args,
1334 .rpc_resp = &res,
1335 };
1336
Trond Myklebust0e574af2005-10-27 22:12:38 -04001337 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return rpc_call_sync(server->client, &msg, 0);
1339}
1340
1341static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1342{
1343 struct nfs4_exception exception = { };
1344 int err;
1345 do {
1346 err = nfs4_handle_exception(server,
1347 _nfs4_proc_getattr(server, fhandle, fattr),
1348 &exception);
1349 } while (exception.retry);
1350 return err;
1351}
1352
1353/*
1354 * The file is not closed if it is opened due to the a request to change
1355 * the size of the file. The open call will not be needed once the
1356 * VFS layer lookup-intents are implemented.
1357 *
1358 * Close is called when the inode is destroyed.
1359 * If we haven't opened the file for O_WRONLY, we
1360 * need to in the size_change case to obtain a stateid.
1361 *
1362 * Got race?
1363 * Because OPEN is always done by name in nfsv4, it is
1364 * possible that we opened a different file by the same
1365 * name. We can recognize this race condition, but we
1366 * can't do anything about it besides returning an error.
1367 *
1368 * This will be fixed with VFS changes (lookup-intent).
1369 */
1370static int
1371nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1372 struct iattr *sattr)
1373{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001374 struct rpc_cred *cred;
1375 struct inode *inode = dentry->d_inode;
Trond Myklebustd5308382005-11-04 15:33:38 -05001376 struct nfs_open_context *ctx;
1377 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 int status;
1379
Trond Myklebust0e574af2005-10-27 22:12:38 -04001380 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001382 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1383 if (IS_ERR(cred))
1384 return PTR_ERR(cred);
Trond Myklebustd5308382005-11-04 15:33:38 -05001385
1386 /* Search for an existing open(O_WRITE) file */
1387 ctx = nfs_find_open_context(inode, cred, FMODE_WRITE);
1388 if (ctx != NULL)
1389 state = ctx->state;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1392 NFS_FH(inode), sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04001393 if (status == 0)
1394 nfs_setattr_update_inode(inode, sattr);
Trond Myklebustd5308382005-11-04 15:33:38 -05001395 if (ctx != NULL)
1396 put_nfs_open_context(ctx);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001397 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return status;
1399}
1400
1401static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1402 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1403{
1404 int status;
1405 struct nfs_server *server = NFS_SERVER(dir);
1406 struct nfs4_lookup_arg args = {
1407 .bitmask = server->attr_bitmask,
1408 .dir_fh = NFS_FH(dir),
1409 .name = name,
1410 };
1411 struct nfs4_lookup_res res = {
1412 .server = server,
1413 .fattr = fattr,
1414 .fh = fhandle,
1415 };
1416 struct rpc_message msg = {
1417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1418 .rpc_argp = &args,
1419 .rpc_resp = &res,
1420 };
1421
Trond Myklebust0e574af2005-10-27 22:12:38 -04001422 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 dprintk("NFS call lookup %s\n", name->name);
1425 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1426 dprintk("NFS reply lookup: %d\n", status);
1427 return status;
1428}
1429
1430static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1431{
1432 struct nfs4_exception exception = { };
1433 int err;
1434 do {
1435 err = nfs4_handle_exception(NFS_SERVER(dir),
1436 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1437 &exception);
1438 } while (exception.retry);
1439 return err;
1440}
1441
1442static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1443{
1444 struct nfs4_accessargs args = {
1445 .fh = NFS_FH(inode),
1446 };
1447 struct nfs4_accessres res = { 0 };
1448 struct rpc_message msg = {
1449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1450 .rpc_argp = &args,
1451 .rpc_resp = &res,
1452 .rpc_cred = entry->cred,
1453 };
1454 int mode = entry->mask;
1455 int status;
1456
1457 /*
1458 * Determine which access bits we want to ask for...
1459 */
1460 if (mode & MAY_READ)
1461 args.access |= NFS4_ACCESS_READ;
1462 if (S_ISDIR(inode->i_mode)) {
1463 if (mode & MAY_WRITE)
1464 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1465 if (mode & MAY_EXEC)
1466 args.access |= NFS4_ACCESS_LOOKUP;
1467 } else {
1468 if (mode & MAY_WRITE)
1469 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1470 if (mode & MAY_EXEC)
1471 args.access |= NFS4_ACCESS_EXECUTE;
1472 }
1473 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1474 if (!status) {
1475 entry->mask = 0;
1476 if (res.access & NFS4_ACCESS_READ)
1477 entry->mask |= MAY_READ;
1478 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1479 entry->mask |= MAY_WRITE;
1480 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1481 entry->mask |= MAY_EXEC;
1482 }
1483 return status;
1484}
1485
1486static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1487{
1488 struct nfs4_exception exception = { };
1489 int err;
1490 do {
1491 err = nfs4_handle_exception(NFS_SERVER(inode),
1492 _nfs4_proc_access(inode, entry),
1493 &exception);
1494 } while (exception.retry);
1495 return err;
1496}
1497
1498/*
1499 * TODO: For the time being, we don't try to get any attributes
1500 * along with any of the zero-copy operations READ, READDIR,
1501 * READLINK, WRITE.
1502 *
1503 * In the case of the first three, we want to put the GETATTR
1504 * after the read-type operation -- this is because it is hard
1505 * to predict the length of a GETATTR response in v4, and thus
1506 * align the READ data correctly. This means that the GETATTR
1507 * may end up partially falling into the page cache, and we should
1508 * shift it into the 'tail' of the xdr_buf before processing.
1509 * To do this efficiently, we need to know the total length
1510 * of data received, which doesn't seem to be available outside
1511 * of the RPC layer.
1512 *
1513 * In the case of WRITE, we also want to put the GETATTR after
1514 * the operation -- in this case because we want to make sure
1515 * we get the post-operation mtime and size. This means that
1516 * we can't use xdr_encode_pages() as written: we need a variant
1517 * of it which would leave room in the 'tail' iovec.
1518 *
1519 * Both of these changes to the XDR layer would in fact be quite
1520 * minor, but I decided to leave them for a subsequent patch.
1521 */
1522static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1523 unsigned int pgbase, unsigned int pglen)
1524{
1525 struct nfs4_readlink args = {
1526 .fh = NFS_FH(inode),
1527 .pgbase = pgbase,
1528 .pglen = pglen,
1529 .pages = &page,
1530 };
1531 struct rpc_message msg = {
1532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1533 .rpc_argp = &args,
1534 .rpc_resp = NULL,
1535 };
1536
1537 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1538}
1539
1540static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1541 unsigned int pgbase, unsigned int pglen)
1542{
1543 struct nfs4_exception exception = { };
1544 int err;
1545 do {
1546 err = nfs4_handle_exception(NFS_SERVER(inode),
1547 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1548 &exception);
1549 } while (exception.retry);
1550 return err;
1551}
1552
1553static int _nfs4_proc_read(struct nfs_read_data *rdata)
1554{
1555 int flags = rdata->flags;
1556 struct inode *inode = rdata->inode;
1557 struct nfs_fattr *fattr = rdata->res.fattr;
1558 struct nfs_server *server = NFS_SERVER(inode);
1559 struct rpc_message msg = {
1560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1561 .rpc_argp = &rdata->args,
1562 .rpc_resp = &rdata->res,
1563 .rpc_cred = rdata->cred,
1564 };
1565 unsigned long timestamp = jiffies;
1566 int status;
1567
1568 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1569 (long long) rdata->args.offset);
1570
Trond Myklebust0e574af2005-10-27 22:12:38 -04001571 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 status = rpc_call_sync(server->client, &msg, flags);
1573 if (!status)
1574 renew_lease(server, timestamp);
1575 dprintk("NFS reply read: %d\n", status);
1576 return status;
1577}
1578
1579static int nfs4_proc_read(struct nfs_read_data *rdata)
1580{
1581 struct nfs4_exception exception = { };
1582 int err;
1583 do {
1584 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1585 _nfs4_proc_read(rdata),
1586 &exception);
1587 } while (exception.retry);
1588 return err;
1589}
1590
1591static int _nfs4_proc_write(struct nfs_write_data *wdata)
1592{
1593 int rpcflags = wdata->flags;
1594 struct inode *inode = wdata->inode;
1595 struct nfs_fattr *fattr = wdata->res.fattr;
1596 struct nfs_server *server = NFS_SERVER(inode);
1597 struct rpc_message msg = {
1598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1599 .rpc_argp = &wdata->args,
1600 .rpc_resp = &wdata->res,
1601 .rpc_cred = wdata->cred,
1602 };
1603 int status;
1604
1605 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1606 (long long) wdata->args.offset);
1607
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001608 wdata->args.bitmask = server->attr_bitmask;
1609 wdata->res.server = server;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001610 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 status = rpc_call_sync(server->client, &msg, rpcflags);
1612 dprintk("NFS reply write: %d\n", status);
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001613 if (status < 0)
1614 return status;
1615 nfs_post_op_update_inode(inode, fattr);
1616 return wdata->res.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
1619static int nfs4_proc_write(struct nfs_write_data *wdata)
1620{
1621 struct nfs4_exception exception = { };
1622 int err;
1623 do {
1624 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1625 _nfs4_proc_write(wdata),
1626 &exception);
1627 } while (exception.retry);
1628 return err;
1629}
1630
1631static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1632{
1633 struct inode *inode = cdata->inode;
1634 struct nfs_fattr *fattr = cdata->res.fattr;
1635 struct nfs_server *server = NFS_SERVER(inode);
1636 struct rpc_message msg = {
1637 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1638 .rpc_argp = &cdata->args,
1639 .rpc_resp = &cdata->res,
1640 .rpc_cred = cdata->cred,
1641 };
1642 int status;
1643
1644 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1645 (long long) cdata->args.offset);
1646
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001647 cdata->args.bitmask = server->attr_bitmask;
1648 cdata->res.server = server;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001649 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 status = rpc_call_sync(server->client, &msg, 0);
1651 dprintk("NFS reply commit: %d\n", status);
Trond Myklebust3b6efee2005-12-03 15:20:21 -05001652 if (status >= 0)
1653 nfs_post_op_update_inode(inode, fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return status;
1655}
1656
1657static int nfs4_proc_commit(struct nfs_write_data *cdata)
1658{
1659 struct nfs4_exception exception = { };
1660 int err;
1661 do {
1662 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1663 _nfs4_proc_commit(cdata),
1664 &exception);
1665 } while (exception.retry);
1666 return err;
1667}
1668
1669/*
1670 * Got race?
1671 * We will need to arrange for the VFS layer to provide an atomic open.
1672 * Until then, this create/open method is prone to inefficiency and race
1673 * conditions due to the lookup, create, and open VFS calls from sys_open()
1674 * placed on the wire.
1675 *
1676 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1677 * The file will be opened again in the subsequent VFS open call
1678 * (nfs4_proc_file_open).
1679 *
1680 * The open for read will just hang around to be used by any process that
1681 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1682 */
1683
1684static int
1685nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07001686 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 struct nfs4_state *state;
1689 struct rpc_cred *cred;
1690 int status = 0;
1691
1692 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1693 if (IS_ERR(cred)) {
1694 status = PTR_ERR(cred);
1695 goto out;
1696 }
1697 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1698 put_rpccred(cred);
1699 if (IS_ERR(state)) {
1700 status = PTR_ERR(state);
1701 goto out;
1702 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703 d_instantiate(dentry, igrab(state->inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (flags & O_EXCL) {
1705 struct nfs_fattr fattr;
1706 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1707 NFS_FH(state->inode), sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001708 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04001709 nfs_setattr_update_inode(state->inode, sattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001710 }
1711 if (status == 0 && nd != NULL && (nd->flags & LOOKUP_OPEN))
1712 nfs4_intent_set_file(nd, dentry, state);
1713 else
1714 nfs4_close_state(state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715out:
1716 return status;
1717}
1718
1719static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1720{
Trond Myklebust16e42952005-10-27 22:12:44 -04001721 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 struct nfs4_remove_arg args = {
1723 .fh = NFS_FH(dir),
1724 .name = name,
Trond Myklebust16e42952005-10-27 22:12:44 -04001725 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 };
Trond Myklebust16e42952005-10-27 22:12:44 -04001727 struct nfs_fattr dir_attr;
1728 struct nfs4_remove_res res = {
1729 .server = server,
1730 .dir_attr = &dir_attr,
1731 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 struct rpc_message msg = {
1733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1734 .rpc_argp = &args,
1735 .rpc_resp = &res,
1736 };
1737 int status;
1738
Trond Myklebust16e42952005-10-27 22:12:44 -04001739 nfs_fattr_init(res.dir_attr);
1740 status = rpc_call_sync(server->client, &msg, 0);
1741 if (status == 0) {
1742 update_changeattr(dir, &res.cinfo);
1743 nfs_post_op_update_inode(dir, res.dir_attr);
1744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return status;
1746}
1747
1748static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1749{
1750 struct nfs4_exception exception = { };
1751 int err;
1752 do {
1753 err = nfs4_handle_exception(NFS_SERVER(dir),
1754 _nfs4_proc_remove(dir, name),
1755 &exception);
1756 } while (exception.retry);
1757 return err;
1758}
1759
1760struct unlink_desc {
1761 struct nfs4_remove_arg args;
Trond Myklebust16e42952005-10-27 22:12:44 -04001762 struct nfs4_remove_res res;
1763 struct nfs_fattr dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764};
1765
1766static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1767 struct qstr *name)
1768{
Trond Myklebust16e42952005-10-27 22:12:44 -04001769 struct nfs_server *server = NFS_SERVER(dir->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 struct unlink_desc *up;
1771
1772 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1773 if (!up)
1774 return -ENOMEM;
1775
1776 up->args.fh = NFS_FH(dir->d_inode);
1777 up->args.name = name;
Trond Myklebust16e42952005-10-27 22:12:44 -04001778 up->args.bitmask = server->attr_bitmask;
1779 up->res.server = server;
1780 up->res.dir_attr = &up->dir_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1783 msg->rpc_argp = &up->args;
1784 msg->rpc_resp = &up->res;
1785 return 0;
1786}
1787
1788static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1789{
1790 struct rpc_message *msg = &task->tk_msg;
1791 struct unlink_desc *up;
1792
1793 if (msg->rpc_resp != NULL) {
1794 up = container_of(msg->rpc_resp, struct unlink_desc, res);
Trond Myklebust16e42952005-10-27 22:12:44 -04001795 update_changeattr(dir->d_inode, &up->res.cinfo);
1796 nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 kfree(up);
1798 msg->rpc_resp = NULL;
1799 msg->rpc_argp = NULL;
1800 }
1801 return 0;
1802}
1803
1804static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1805 struct inode *new_dir, struct qstr *new_name)
1806{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001807 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 struct nfs4_rename_arg arg = {
1809 .old_dir = NFS_FH(old_dir),
1810 .new_dir = NFS_FH(new_dir),
1811 .old_name = old_name,
1812 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001813 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001815 struct nfs_fattr old_fattr, new_fattr;
1816 struct nfs4_rename_res res = {
1817 .server = server,
1818 .old_fattr = &old_fattr,
1819 .new_fattr = &new_fattr,
1820 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 struct rpc_message msg = {
1822 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1823 .rpc_argp = &arg,
1824 .rpc_resp = &res,
1825 };
1826 int status;
1827
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001828 nfs_fattr_init(res.old_fattr);
1829 nfs_fattr_init(res.new_fattr);
1830 status = rpc_call_sync(server->client, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 if (!status) {
1833 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001834 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04001836 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838 return status;
1839}
1840
1841static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1842 struct inode *new_dir, struct qstr *new_name)
1843{
1844 struct nfs4_exception exception = { };
1845 int err;
1846 do {
1847 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1848 _nfs4_proc_rename(old_dir, old_name,
1849 new_dir, new_name),
1850 &exception);
1851 } while (exception.retry);
1852 return err;
1853}
1854
1855static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1856{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001857 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct nfs4_link_arg arg = {
1859 .fh = NFS_FH(inode),
1860 .dir_fh = NFS_FH(dir),
1861 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001862 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001864 struct nfs_fattr fattr, dir_attr;
1865 struct nfs4_link_res res = {
1866 .server = server,
1867 .fattr = &fattr,
1868 .dir_attr = &dir_attr,
1869 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 struct rpc_message msg = {
1871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1872 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001873 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 };
1875 int status;
1876
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04001877 nfs_fattr_init(res.fattr);
1878 nfs_fattr_init(res.dir_attr);
1879 status = rpc_call_sync(server->client, &msg, 0);
1880 if (!status) {
1881 update_changeattr(dir, &res.cinfo);
1882 nfs_post_op_update_inode(dir, res.dir_attr);
1883 nfs_refresh_inode(inode, res.fattr);
1884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 return status;
1887}
1888
1889static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1890{
1891 struct nfs4_exception exception = { };
1892 int err;
1893 do {
1894 err = nfs4_handle_exception(NFS_SERVER(inode),
1895 _nfs4_proc_link(inode, dir, name),
1896 &exception);
1897 } while (exception.retry);
1898 return err;
1899}
1900
1901static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1902 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1903 struct nfs_fattr *fattr)
1904{
1905 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001906 struct nfs_fattr dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 struct nfs4_create_arg arg = {
1908 .dir_fh = NFS_FH(dir),
1909 .server = server,
1910 .name = name,
1911 .attrs = sattr,
1912 .ftype = NF4LNK,
1913 .bitmask = server->attr_bitmask,
1914 };
1915 struct nfs4_create_res res = {
1916 .server = server,
1917 .fh = fhandle,
1918 .fattr = fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001919 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 };
1921 struct rpc_message msg = {
1922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1923 .rpc_argp = &arg,
1924 .rpc_resp = &res,
1925 };
1926 int status;
1927
1928 if (path->len > NFS4_MAXPATHLEN)
1929 return -ENAMETOOLONG;
1930 arg.u.symlink = path;
Trond Myklebust0e574af2005-10-27 22:12:38 -04001931 nfs_fattr_init(fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001932 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1935 if (!status)
1936 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001937 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 return status;
1939}
1940
1941static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1942 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1943 struct nfs_fattr *fattr)
1944{
1945 struct nfs4_exception exception = { };
1946 int err;
1947 do {
1948 err = nfs4_handle_exception(NFS_SERVER(dir),
1949 _nfs4_proc_symlink(dir, name, path, sattr,
1950 fhandle, fattr),
1951 &exception);
1952 } while (exception.retry);
1953 return err;
1954}
1955
1956static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1957 struct iattr *sattr)
1958{
1959 struct nfs_server *server = NFS_SERVER(dir);
1960 struct nfs_fh fhandle;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001961 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 struct nfs4_create_arg arg = {
1963 .dir_fh = NFS_FH(dir),
1964 .server = server,
1965 .name = &dentry->d_name,
1966 .attrs = sattr,
1967 .ftype = NF4DIR,
1968 .bitmask = server->attr_bitmask,
1969 };
1970 struct nfs4_create_res res = {
1971 .server = server,
1972 .fh = &fhandle,
1973 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001974 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 };
1976 struct rpc_message msg = {
1977 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1978 .rpc_argp = &arg,
1979 .rpc_resp = &res,
1980 };
1981 int status;
1982
Trond Myklebust0e574af2005-10-27 22:12:38 -04001983 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001984 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1987 if (!status) {
1988 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001989 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 status = nfs_instantiate(dentry, &fhandle, &fattr);
1991 }
1992 return status;
1993}
1994
1995static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1996 struct iattr *sattr)
1997{
1998 struct nfs4_exception exception = { };
1999 int err;
2000 do {
2001 err = nfs4_handle_exception(NFS_SERVER(dir),
2002 _nfs4_proc_mkdir(dir, dentry, sattr),
2003 &exception);
2004 } while (exception.retry);
2005 return err;
2006}
2007
2008static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2009 u64 cookie, struct page *page, unsigned int count, int plus)
2010{
2011 struct inode *dir = dentry->d_inode;
2012 struct nfs4_readdir_arg args = {
2013 .fh = NFS_FH(dir),
2014 .pages = &page,
2015 .pgbase = 0,
2016 .count = count,
2017 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2018 };
2019 struct nfs4_readdir_res res;
2020 struct rpc_message msg = {
2021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2022 .rpc_argp = &args,
2023 .rpc_resp = &res,
2024 .rpc_cred = cred,
2025 };
2026 int status;
2027
Trond Myklebusteadf4592005-06-22 17:16:39 +00002028 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__,
2029 dentry->d_parent->d_name.name,
2030 dentry->d_name.name,
2031 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 lock_kernel();
2033 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2034 res.pgbase = args.pgbase;
2035 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2036 if (status == 0)
2037 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2038 unlock_kernel();
Trond Myklebusteadf4592005-06-22 17:16:39 +00002039 dprintk("%s: returns %d\n", __FUNCTION__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return status;
2041}
2042
2043static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2044 u64 cookie, struct page *page, unsigned int count, int plus)
2045{
2046 struct nfs4_exception exception = { };
2047 int err;
2048 do {
2049 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2050 _nfs4_proc_readdir(dentry, cred, cookie,
2051 page, count, plus),
2052 &exception);
2053 } while (exception.retry);
2054 return err;
2055}
2056
2057static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2058 struct iattr *sattr, dev_t rdev)
2059{
2060 struct nfs_server *server = NFS_SERVER(dir);
2061 struct nfs_fh fh;
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002062 struct nfs_fattr fattr, dir_fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 struct nfs4_create_arg arg = {
2064 .dir_fh = NFS_FH(dir),
2065 .server = server,
2066 .name = &dentry->d_name,
2067 .attrs = sattr,
2068 .bitmask = server->attr_bitmask,
2069 };
2070 struct nfs4_create_res res = {
2071 .server = server,
2072 .fh = &fh,
2073 .fattr = &fattr,
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002074 .dir_fattr = &dir_fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 };
2076 struct rpc_message msg = {
2077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
2078 .rpc_argp = &arg,
2079 .rpc_resp = &res,
2080 };
2081 int status;
2082 int mode = sattr->ia_mode;
2083
Trond Myklebust0e574af2005-10-27 22:12:38 -04002084 nfs_fattr_init(&fattr);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002085 nfs_fattr_init(&dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2088 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2089 if (S_ISFIFO(mode))
2090 arg.ftype = NF4FIFO;
2091 else if (S_ISBLK(mode)) {
2092 arg.ftype = NF4BLK;
2093 arg.u.device.specdata1 = MAJOR(rdev);
2094 arg.u.device.specdata2 = MINOR(rdev);
2095 }
2096 else if (S_ISCHR(mode)) {
2097 arg.ftype = NF4CHR;
2098 arg.u.device.specdata1 = MAJOR(rdev);
2099 arg.u.device.specdata2 = MINOR(rdev);
2100 }
2101 else
2102 arg.ftype = NF4SOCK;
2103
2104 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
2105 if (status == 0) {
2106 update_changeattr(dir, &res.dir_cinfo);
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002107 nfs_post_op_update_inode(dir, res.dir_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 status = nfs_instantiate(dentry, &fh, &fattr);
2109 }
2110 return status;
2111}
2112
2113static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2114 struct iattr *sattr, dev_t rdev)
2115{
2116 struct nfs4_exception exception = { };
2117 int err;
2118 do {
2119 err = nfs4_handle_exception(NFS_SERVER(dir),
2120 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2121 &exception);
2122 } while (exception.retry);
2123 return err;
2124}
2125
2126static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2127 struct nfs_fsstat *fsstat)
2128{
2129 struct nfs4_statfs_arg args = {
2130 .fh = fhandle,
2131 .bitmask = server->attr_bitmask,
2132 };
2133 struct rpc_message msg = {
2134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2135 .rpc_argp = &args,
2136 .rpc_resp = fsstat,
2137 };
2138
Trond Myklebust0e574af2005-10-27 22:12:38 -04002139 nfs_fattr_init(fsstat->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return rpc_call_sync(server->client, &msg, 0);
2141}
2142
2143static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2144{
2145 struct nfs4_exception exception = { };
2146 int err;
2147 do {
2148 err = nfs4_handle_exception(server,
2149 _nfs4_proc_statfs(server, fhandle, fsstat),
2150 &exception);
2151 } while (exception.retry);
2152 return err;
2153}
2154
2155static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2156 struct nfs_fsinfo *fsinfo)
2157{
2158 struct nfs4_fsinfo_arg args = {
2159 .fh = fhandle,
2160 .bitmask = server->attr_bitmask,
2161 };
2162 struct rpc_message msg = {
2163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2164 .rpc_argp = &args,
2165 .rpc_resp = fsinfo,
2166 };
2167
2168 return rpc_call_sync(server->client, &msg, 0);
2169}
2170
2171static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2172{
2173 struct nfs4_exception exception = { };
2174 int err;
2175
2176 do {
2177 err = nfs4_handle_exception(server,
2178 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2179 &exception);
2180 } while (exception.retry);
2181 return err;
2182}
2183
2184static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2185{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002186 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2188}
2189
2190static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2191 struct nfs_pathconf *pathconf)
2192{
2193 struct nfs4_pathconf_arg args = {
2194 .fh = fhandle,
2195 .bitmask = server->attr_bitmask,
2196 };
2197 struct rpc_message msg = {
2198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2199 .rpc_argp = &args,
2200 .rpc_resp = pathconf,
2201 };
2202
2203 /* None of the pathconf attributes are mandatory to implement */
2204 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2205 memset(pathconf, 0, sizeof(*pathconf));
2206 return 0;
2207 }
2208
Trond Myklebust0e574af2005-10-27 22:12:38 -04002209 nfs_fattr_init(pathconf->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return rpc_call_sync(server->client, &msg, 0);
2211}
2212
2213static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2214 struct nfs_pathconf *pathconf)
2215{
2216 struct nfs4_exception exception = { };
2217 int err;
2218
2219 do {
2220 err = nfs4_handle_exception(server,
2221 _nfs4_proc_pathconf(server, fhandle, pathconf),
2222 &exception);
2223 } while (exception.retry);
2224 return err;
2225}
2226
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002227static void nfs4_read_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002229 struct nfs_read_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 struct inode *inode = data->inode;
2231
2232 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2233 rpc_restart_call(task);
2234 return;
2235 }
2236 if (task->tk_status > 0)
2237 renew_lease(NFS_SERVER(inode), data->timestamp);
2238 /* Call back common NFS readpage processing */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002239 nfs_readpage_result(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002242static const struct rpc_call_ops nfs4_read_ops = {
2243 .rpc_call_done = nfs4_read_done,
2244 .rpc_release = nfs_readdata_release,
2245};
2246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247static void
2248nfs4_proc_read_setup(struct nfs_read_data *data)
2249{
2250 struct rpc_task *task = &data->task;
2251 struct rpc_message msg = {
2252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
2253 .rpc_argp = &data->args,
2254 .rpc_resp = &data->res,
2255 .rpc_cred = data->cred,
2256 };
2257 struct inode *inode = data->inode;
2258 int flags;
2259
2260 data->timestamp = jiffies;
2261
2262 /* N.B. Do we need to test? Never called for swapfile inode */
2263 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
2264
2265 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002266 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_read_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 rpc_call_setup(task, &msg, 0);
2268}
2269
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002270static void nfs4_write_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002272 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 struct inode *inode = data->inode;
2274
2275 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2276 rpc_restart_call(task);
2277 return;
2278 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002279 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002281 nfs_post_op_update_inode(inode, data->res.fattr);
2282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 /* Call back common NFS writeback processing */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002284 nfs_writeback_done(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002287static const struct rpc_call_ops nfs4_write_ops = {
2288 .rpc_call_done = nfs4_write_done,
2289 .rpc_release = nfs_writedata_release,
2290};
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292static void
2293nfs4_proc_write_setup(struct nfs_write_data *data, int how)
2294{
2295 struct rpc_task *task = &data->task;
2296 struct rpc_message msg = {
2297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
2298 .rpc_argp = &data->args,
2299 .rpc_resp = &data->res,
2300 .rpc_cred = data->cred,
2301 };
2302 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002303 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 int stable;
2305 int flags;
2306
2307 if (how & FLUSH_STABLE) {
2308 if (!NFS_I(inode)->ncommit)
2309 stable = NFS_FILE_SYNC;
2310 else
2311 stable = NFS_DATA_SYNC;
2312 } else
2313 stable = NFS_UNSTABLE;
2314 data->args.stable = stable;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002315 data->args.bitmask = server->attr_bitmask;
2316 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 data->timestamp = jiffies;
2319
2320 /* Set the initial flags for the task. */
2321 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2322
2323 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002324 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_write_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 rpc_call_setup(task, &msg, 0);
2326}
2327
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002328static void nfs4_commit_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002330 struct nfs_write_data *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 struct inode *inode = data->inode;
2332
2333 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2334 rpc_restart_call(task);
2335 return;
2336 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002337 if (task->tk_status >= 0)
2338 nfs_post_op_update_inode(inode, data->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /* Call back common NFS writeback processing */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002340 nfs_commit_done(task, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002343static const struct rpc_call_ops nfs4_commit_ops = {
2344 .rpc_call_done = nfs4_commit_done,
2345 .rpc_release = nfs_commit_release,
2346};
2347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348static void
2349nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2350{
2351 struct rpc_task *task = &data->task;
2352 struct rpc_message msg = {
2353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2354 .rpc_argp = &data->args,
2355 .rpc_resp = &data->res,
2356 .rpc_cred = data->cred,
2357 };
2358 struct inode *inode = data->inode;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002359 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 int flags;
2361
Trond Myklebust4f9838c2005-10-27 22:12:44 -04002362 data->args.bitmask = server->attr_bitmask;
2363 data->res.server = server;
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 /* Set the initial flags for the task. */
2366 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2367
2368 /* Finalize the task. */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002369 rpc_init_task(task, NFS_CLIENT(inode), flags, &nfs4_commit_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 rpc_call_setup(task, &msg, 0);
2371}
2372
2373/*
2374 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2375 * standalone procedure for queueing an asynchronous RENEW.
2376 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002377static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
2379 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002380 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 if (task->tk_status < 0) {
2383 switch (task->tk_status) {
2384 case -NFS4ERR_STALE_CLIENTID:
2385 case -NFS4ERR_EXPIRED:
2386 case -NFS4ERR_CB_PATH_DOWN:
2387 nfs4_schedule_state_recovery(clp);
2388 }
2389 return;
2390 }
2391 spin_lock(&clp->cl_lock);
2392 if (time_before(clp->cl_last_renewal,timestamp))
2393 clp->cl_last_renewal = timestamp;
2394 spin_unlock(&clp->cl_lock);
2395}
2396
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002397static const struct rpc_call_ops nfs4_renew_ops = {
2398 .rpc_call_done = nfs4_renew_done,
2399};
2400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401int
2402nfs4_proc_async_renew(struct nfs4_client *clp)
2403{
2404 struct rpc_message msg = {
2405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2406 .rpc_argp = clp,
2407 .rpc_cred = clp->cl_cred,
2408 };
2409
2410 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002411 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412}
2413
2414int
2415nfs4_proc_renew(struct nfs4_client *clp)
2416{
2417 struct rpc_message msg = {
2418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2419 .rpc_argp = clp,
2420 .rpc_cred = clp->cl_cred,
2421 };
2422 unsigned long now = jiffies;
2423 int status;
2424
2425 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2426 if (status < 0)
2427 return status;
2428 spin_lock(&clp->cl_lock);
2429 if (time_before(clp->cl_last_renewal,now))
2430 clp->cl_last_renewal = now;
2431 spin_unlock(&clp->cl_lock);
2432 return 0;
2433}
2434
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002435static inline int nfs4_server_supports_acls(struct nfs_server *server)
2436{
2437 return (server->caps & NFS_CAP_ACLS)
2438 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2439 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2440}
2441
2442/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2443 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2444 * the stack.
2445 */
2446#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2447
2448static void buf_to_pages(const void *buf, size_t buflen,
2449 struct page **pages, unsigned int *pgbase)
2450{
2451 const void *p = buf;
2452
2453 *pgbase = offset_in_page(buf);
2454 p -= *pgbase;
2455 while (p < buf + buflen) {
2456 *(pages++) = virt_to_page(p);
2457 p += PAGE_CACHE_SIZE;
2458 }
2459}
2460
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002461struct nfs4_cached_acl {
2462 int cached;
2463 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00002464 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002465};
2466
2467static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002468{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002469 struct nfs_inode *nfsi = NFS_I(inode);
2470
2471 spin_lock(&inode->i_lock);
2472 kfree(nfsi->nfs4_acl);
2473 nfsi->nfs4_acl = acl;
2474 spin_unlock(&inode->i_lock);
2475}
2476
2477static void nfs4_zap_acl_attr(struct inode *inode)
2478{
2479 nfs4_set_cached_acl(inode, NULL);
2480}
2481
2482static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2483{
2484 struct nfs_inode *nfsi = NFS_I(inode);
2485 struct nfs4_cached_acl *acl;
2486 int ret = -ENOENT;
2487
2488 spin_lock(&inode->i_lock);
2489 acl = nfsi->nfs4_acl;
2490 if (acl == NULL)
2491 goto out;
2492 if (buf == NULL) /* user is just asking for length */
2493 goto out_len;
2494 if (acl->cached == 0)
2495 goto out;
2496 ret = -ERANGE; /* see getxattr(2) man page */
2497 if (acl->len > buflen)
2498 goto out;
2499 memcpy(buf, acl->data, acl->len);
2500out_len:
2501 ret = acl->len;
2502out:
2503 spin_unlock(&inode->i_lock);
2504 return ret;
2505}
2506
2507static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2508{
2509 struct nfs4_cached_acl *acl;
2510
2511 if (buf && acl_len <= PAGE_SIZE) {
2512 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2513 if (acl == NULL)
2514 goto out;
2515 acl->cached = 1;
2516 memcpy(acl->data, buf, acl_len);
2517 } else {
2518 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2519 if (acl == NULL)
2520 goto out;
2521 acl->cached = 0;
2522 }
2523 acl->len = acl_len;
2524out:
2525 nfs4_set_cached_acl(inode, acl);
2526}
2527
2528static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
2529{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002530 struct page *pages[NFS4ACL_MAXPAGES];
2531 struct nfs_getaclargs args = {
2532 .fh = NFS_FH(inode),
2533 .acl_pages = pages,
2534 .acl_len = buflen,
2535 };
2536 size_t resp_len = buflen;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002537 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002538 struct rpc_message msg = {
2539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2540 .rpc_argp = &args,
2541 .rpc_resp = &resp_len,
2542 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002543 struct page *localpage = NULL;
2544 int ret;
2545
2546 if (buflen < PAGE_SIZE) {
2547 /* As long as we're doing a round trip to the server anyway,
2548 * let's be prepared for a page of acl data. */
2549 localpage = alloc_page(GFP_KERNEL);
2550 resp_buf = page_address(localpage);
2551 if (localpage == NULL)
2552 return -ENOMEM;
2553 args.acl_pages[0] = localpage;
2554 args.acl_pgbase = 0;
J. Bruce Fields1d95db82005-10-13 16:54:32 -04002555 resp_len = args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002556 } else {
2557 resp_buf = buf;
2558 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2559 }
2560 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2561 if (ret)
2562 goto out_free;
2563 if (resp_len > args.acl_len)
2564 nfs4_write_cached_acl(inode, NULL, resp_len);
2565 else
2566 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2567 if (buf) {
2568 ret = -ERANGE;
2569 if (resp_len > buflen)
2570 goto out_free;
2571 if (localpage)
2572 memcpy(buf, resp_buf, resp_len);
2573 }
2574 ret = resp_len;
2575out_free:
2576 if (localpage)
2577 __free_page(localpage);
2578 return ret;
2579}
2580
2581static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2582{
2583 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002584 int ret;
2585
2586 if (!nfs4_server_supports_acls(server))
2587 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002588 ret = nfs_revalidate_inode(server, inode);
2589 if (ret < 0)
2590 return ret;
2591 ret = nfs4_read_cached_acl(inode, buf, buflen);
2592 if (ret != -ENOENT)
2593 return ret;
2594 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00002595}
2596
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002597static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2598{
2599 struct nfs_server *server = NFS_SERVER(inode);
2600 struct page *pages[NFS4ACL_MAXPAGES];
2601 struct nfs_setaclargs arg = {
2602 .fh = NFS_FH(inode),
2603 .acl_pages = pages,
2604 .acl_len = buflen,
2605 };
2606 struct rpc_message msg = {
2607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2608 .rpc_argp = &arg,
2609 .rpc_resp = NULL,
2610 };
2611 int ret;
2612
2613 if (!nfs4_server_supports_acls(server))
2614 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07002615 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002616 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2617 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00002618 if (ret == 0)
2619 nfs4_write_cached_acl(inode, buf, buflen);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00002620 return ret;
2621}
2622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623static int
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002624nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
2626 struct nfs4_client *clp = server->nfs4_state;
2627
2628 if (!clp || task->tk_status >= 0)
2629 return 0;
2630 switch(task->tk_status) {
2631 case -NFS4ERR_STALE_CLIENTID:
2632 case -NFS4ERR_STALE_STATEID:
2633 case -NFS4ERR_EXPIRED:
2634 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2635 nfs4_schedule_state_recovery(clp);
2636 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2637 rpc_wake_up_task(task);
2638 task->tk_status = 0;
2639 return -EAGAIN;
2640 case -NFS4ERR_GRACE:
2641 case -NFS4ERR_DELAY:
2642 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2643 task->tk_status = 0;
2644 return -EAGAIN;
2645 case -NFS4ERR_OLD_STATEID:
2646 task->tk_status = 0;
2647 return -EAGAIN;
2648 }
2649 task->tk_status = nfs4_map_errors(task->tk_status);
2650 return 0;
2651}
2652
2653static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2654{
2655 DEFINE_WAIT(wait);
2656 sigset_t oldset;
2657 int interruptible, res = 0;
2658
2659 might_sleep();
2660
2661 rpc_clnt_sigmask(clnt, &oldset);
2662 interruptible = TASK_UNINTERRUPTIBLE;
2663 if (clnt->cl_intr)
2664 interruptible = TASK_INTERRUPTIBLE;
2665 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2666 nfs4_schedule_state_recovery(clp);
2667 if (clnt->cl_intr && signalled())
2668 res = -ERESTARTSYS;
2669 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2670 schedule();
2671 finish_wait(&clp->cl_waitq, &wait);
2672 rpc_clnt_sigunmask(clnt, &oldset);
2673 return res;
2674}
2675
2676static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2677{
2678 sigset_t oldset;
2679 int res = 0;
2680
2681 might_sleep();
2682
2683 if (*timeout <= 0)
2684 *timeout = NFS4_POLL_RETRY_MIN;
2685 if (*timeout > NFS4_POLL_RETRY_MAX)
2686 *timeout = NFS4_POLL_RETRY_MAX;
2687 rpc_clnt_sigmask(clnt, &oldset);
2688 if (clnt->cl_intr) {
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002689 schedule_timeout_interruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 if (signalled())
2691 res = -ERESTARTSYS;
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002692 } else
2693 schedule_timeout_uninterruptible(*timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 rpc_clnt_sigunmask(clnt, &oldset);
2695 *timeout <<= 1;
2696 return res;
2697}
2698
2699/* This is the error handling routine for processes that are allowed
2700 * to sleep.
2701 */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002702int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703{
2704 struct nfs4_client *clp = server->nfs4_state;
2705 int ret = errorcode;
2706
2707 exception->retry = 0;
2708 switch(errorcode) {
2709 case 0:
2710 return 0;
2711 case -NFS4ERR_STALE_CLIENTID:
2712 case -NFS4ERR_STALE_STATEID:
2713 case -NFS4ERR_EXPIRED:
2714 ret = nfs4_wait_clnt_recover(server->client, clp);
2715 if (ret == 0)
2716 exception->retry = 1;
2717 break;
2718 case -NFS4ERR_GRACE:
2719 case -NFS4ERR_DELAY:
2720 ret = nfs4_delay(server->client, &exception->timeout);
Trond Myklebust2c566172005-11-04 15:33:50 -05002721 if (ret != 0)
2722 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 case -NFS4ERR_OLD_STATEID:
Trond Myklebust2c566172005-11-04 15:33:50 -05002724 exception->retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 }
2726 /* We failed to handle the error */
2727 return nfs4_map_errors(ret);
2728}
2729
2730int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2731{
2732 nfs4_verifier sc_verifier;
2733 struct nfs4_setclientid setclientid = {
2734 .sc_verifier = &sc_verifier,
2735 .sc_prog = program,
2736 };
2737 struct rpc_message msg = {
2738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2739 .rpc_argp = &setclientid,
2740 .rpc_resp = clp,
2741 .rpc_cred = clp->cl_cred,
2742 };
2743 u32 *p;
2744 int loop = 0;
2745 int status;
2746
2747 p = (u32*)sc_verifier.data;
2748 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2749 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2750
2751 for(;;) {
2752 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2753 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2754 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2755 clp->cl_cred->cr_ops->cr_name,
2756 clp->cl_id_uniquifier);
2757 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2758 sizeof(setclientid.sc_netid), "tcp");
2759 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2760 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2761 clp->cl_ipaddr, port >> 8, port & 255);
2762
2763 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2764 if (status != -NFS4ERR_CLID_INUSE)
2765 break;
2766 if (signalled())
2767 break;
2768 if (loop++ & 1)
2769 ssleep(clp->cl_lease_time + 1);
2770 else
2771 if (++clp->cl_id_uniquifier == 0)
2772 break;
2773 }
2774 return status;
2775}
2776
2777int
2778nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2779{
2780 struct nfs_fsinfo fsinfo;
2781 struct rpc_message msg = {
2782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2783 .rpc_argp = clp,
2784 .rpc_resp = &fsinfo,
2785 .rpc_cred = clp->cl_cred,
2786 };
2787 unsigned long now;
2788 int status;
2789
2790 now = jiffies;
2791 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2792 if (status == 0) {
2793 spin_lock(&clp->cl_lock);
2794 clp->cl_lease_time = fsinfo.lease_time * HZ;
2795 clp->cl_last_renewal = now;
2796 spin_unlock(&clp->cl_lock);
2797 }
2798 return status;
2799}
2800
2801static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2802{
2803 struct nfs4_delegreturnargs args = {
2804 .fhandle = NFS_FH(inode),
2805 .stateid = stateid,
2806 };
2807 struct rpc_message msg = {
2808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2809 .rpc_argp = &args,
2810 .rpc_cred = cred,
2811 };
2812
2813 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2814}
2815
2816int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2817{
2818 struct nfs_server *server = NFS_SERVER(inode);
2819 struct nfs4_exception exception = { };
2820 int err;
2821 do {
2822 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2823 switch (err) {
2824 case -NFS4ERR_STALE_STATEID:
2825 case -NFS4ERR_EXPIRED:
2826 nfs4_schedule_state_recovery(server->nfs4_state);
2827 case 0:
2828 return 0;
2829 }
2830 err = nfs4_handle_exception(server, err, &exception);
2831 } while (exception.retry);
2832 return err;
2833}
2834
2835#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2836#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2837
2838/*
2839 * sleep, with exponential backoff, and retry the LOCK operation.
2840 */
2841static unsigned long
2842nfs4_set_lock_task_retry(unsigned long timeout)
2843{
Nishanth Aravamudan041e0e32005-09-10 00:27:23 -07002844 schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 timeout <<= 1;
2846 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2847 return NFS4_LOCK_MAXTIMEOUT;
2848 return timeout;
2849}
2850
2851static inline int
2852nfs4_lck_type(int cmd, struct file_lock *request)
2853{
2854 /* set lock type */
2855 switch (request->fl_type) {
2856 case F_RDLCK:
2857 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2858 case F_WRLCK:
2859 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2860 case F_UNLCK:
2861 return NFS4_WRITE_LT;
2862 }
2863 BUG();
2864 return 0;
2865}
2866
2867static inline uint64_t
2868nfs4_lck_length(struct file_lock *request)
2869{
2870 if (request->fl_end == OFFSET_MAX)
2871 return ~(uint64_t)0;
2872 return request->fl_end - request->fl_start + 1;
2873}
2874
2875static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2876{
2877 struct inode *inode = state->inode;
2878 struct nfs_server *server = NFS_SERVER(inode);
2879 struct nfs4_client *clp = server->nfs4_state;
2880 struct nfs_lockargs arg = {
2881 .fh = NFS_FH(inode),
2882 .type = nfs4_lck_type(cmd, request),
2883 .offset = request->fl_start,
2884 .length = nfs4_lck_length(request),
2885 };
2886 struct nfs_lockres res = {
2887 .server = server,
2888 };
2889 struct rpc_message msg = {
2890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2891 .rpc_argp = &arg,
2892 .rpc_resp = &res,
2893 .rpc_cred = state->owner->so_cred,
2894 };
2895 struct nfs_lowner nlo;
2896 struct nfs4_lock_state *lsp;
2897 int status;
2898
2899 down_read(&clp->cl_sem);
2900 nlo.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00002901 status = nfs4_set_lock_state(state, request);
2902 if (status != 0)
2903 goto out;
2904 lsp = request->fl_u.nfs4_fl.owner;
2905 nlo.id = lsp->ls_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 arg.u.lockt = &nlo;
2907 status = rpc_call_sync(server->client, &msg, 0);
2908 if (!status) {
2909 request->fl_type = F_UNLCK;
2910 } else if (status == -NFS4ERR_DENIED) {
2911 int64_t len, start, end;
2912 start = res.u.denied.offset;
2913 len = res.u.denied.length;
2914 end = start + len - 1;
2915 if (end < 0 || len == 0)
2916 request->fl_end = OFFSET_MAX;
2917 else
2918 request->fl_end = (loff_t)end;
2919 request->fl_start = (loff_t)start;
2920 request->fl_type = F_WRLCK;
2921 if (res.u.denied.type & 1)
2922 request->fl_type = F_RDLCK;
2923 request->fl_pid = 0;
2924 status = 0;
2925 }
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00002926out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 up_read(&clp->cl_sem);
2928 return status;
2929}
2930
2931static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2932{
2933 struct nfs4_exception exception = { };
2934 int err;
2935
2936 do {
2937 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2938 _nfs4_proc_getlk(state, cmd, request),
2939 &exception);
2940 } while (exception.retry);
2941 return err;
2942}
2943
2944static int do_vfs_lock(struct file *file, struct file_lock *fl)
2945{
2946 int res = 0;
2947 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2948 case FL_POSIX:
2949 res = posix_lock_file_wait(file, fl);
2950 break;
2951 case FL_FLOCK:
2952 res = flock_lock_file_wait(file, fl);
2953 break;
2954 default:
2955 BUG();
2956 }
2957 if (res < 0)
2958 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
2959 __FUNCTION__);
2960 return res;
2961}
2962
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002963struct nfs4_unlockdata {
2964 struct nfs_lockargs arg;
2965 struct nfs_locku_opargs luargs;
2966 struct nfs_lockres res;
2967 struct nfs4_lock_state *lsp;
2968 struct nfs_open_context *ctx;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002969};
2970
Trond Myklebust06f814a2006-01-03 09:55:07 +01002971static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002972{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002973 struct nfs4_unlockdata *calldata = data;
Trond Myklebust06f814a2006-01-03 09:55:07 +01002974 nfs_free_seqid(calldata->luargs.seqid);
2975 nfs4_put_lock_state(calldata->lsp);
2976 put_nfs_open_context(calldata->ctx);
2977 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002978}
2979
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002980static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002981{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002982 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002983
Trond Myklebust06f814a2006-01-03 09:55:07 +01002984 if (RPC_ASSASSINATED(task))
2985 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07002986 nfs_increment_lock_seqid(task->tk_status, calldata->luargs.seqid);
2987 switch (task->tk_status) {
2988 case 0:
2989 memcpy(calldata->lsp->ls_stateid.data,
2990 calldata->res.u.stateid.data,
2991 sizeof(calldata->lsp->ls_stateid.data));
2992 break;
2993 case -NFS4ERR_STALE_STATEID:
2994 case -NFS4ERR_EXPIRED:
2995 nfs4_schedule_state_recovery(calldata->res.server->nfs4_state);
2996 break;
2997 default:
2998 if (nfs4_async_handle_error(task, calldata->res.server) == -EAGAIN) {
2999 rpc_restart_call(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003000 }
3001 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003002}
3003
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003004static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003005{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003006 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 struct rpc_message msg = {
3008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003009 .rpc_argp = &calldata->arg,
3010 .rpc_resp = &calldata->res,
3011 .rpc_cred = calldata->lsp->ls_state->owner->so_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 };
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003013 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003015 status = nfs_wait_on_sequence(calldata->luargs.seqid, task);
3016 if (status != 0)
3017 return;
3018 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003019 /* Note: exit _without_ running nfs4_locku_done */
3020 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003021 return;
3022 }
3023 rpc_call_setup(task, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024}
3025
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003026static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003027 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003028 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003029 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003030};
3031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3033{
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003034 struct nfs4_unlockdata *calldata;
3035 struct inode *inode = state->inode;
3036 struct nfs_server *server = NFS_SERVER(inode);
3037 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003038 struct rpc_task *task;
3039 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003041 /* Is this a delegated lock? */
3042 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust06f814a2006-01-03 09:55:07 +01003043 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003044
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003045 status = nfs4_set_lock_state(state, request);
3046 if (status != 0)
Trond Myklebust06f814a2006-01-03 09:55:07 +01003047 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003048 lsp = request->fl_u.nfs4_fl.owner;
3049 /* We might have lost the locks! */
3050 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0)
Trond Myklebust06f814a2006-01-03 09:55:07 +01003051 goto out;
3052 status = -ENOMEM;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003053 calldata = kmalloc(sizeof(*calldata), GFP_KERNEL);
3054 if (calldata == NULL)
Trond Myklebust06f814a2006-01-03 09:55:07 +01003055 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003056 calldata->luargs.seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3057 if (calldata->luargs.seqid == NULL) {
3058 kfree(calldata);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003059 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003060 }
3061 calldata->luargs.stateid = &lsp->ls_stateid;
3062 calldata->arg.fh = NFS_FH(inode);
3063 calldata->arg.type = nfs4_lck_type(cmd, request);
3064 calldata->arg.offset = request->fl_start;
3065 calldata->arg.length = nfs4_lck_length(request);
3066 calldata->arg.u.locku = &calldata->luargs;
3067 calldata->res.server = server;
3068 calldata->lsp = lsp;
3069 atomic_inc(&lsp->ls_count);
3070
3071 /* Ensure we don't close file until we're done freeing locks! */
3072 calldata->ctx = get_nfs_open_context((struct nfs_open_context*)request->fl_file->private_data);
3073
Trond Myklebust06f814a2006-01-03 09:55:07 +01003074 task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_locku_ops, calldata);
3075 if (!IS_ERR(task)) {
3076 status = nfs4_wait_for_completion_rpc_task(task);
3077 rpc_release_task(task);
3078 } else {
3079 status = PTR_ERR(task);
3080 nfs4_locku_release_calldata(calldata);
3081 }
3082out:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003083 do_vfs_lock(request->fl_file, request);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003084 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085}
3086
3087static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
3088{
3089 struct inode *inode = state->inode;
3090 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003091 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust06735b32005-10-18 14:20:15 -07003092 struct nfs_lock_opargs largs = {
3093 .lock_stateid = &lsp->ls_stateid,
3094 .open_stateid = &state->stateid,
3095 .lock_owner = {
3096 .clientid = server->nfs4_state->cl_clientid,
3097 .id = lsp->ls_id,
3098 },
3099 .reclaim = reclaim,
3100 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 struct nfs_lockargs arg = {
3102 .fh = NFS_FH(inode),
3103 .type = nfs4_lck_type(cmd, request),
3104 .offset = request->fl_start,
3105 .length = nfs4_lck_length(request),
Trond Myklebust06735b32005-10-18 14:20:15 -07003106 .u = {
3107 .lock = &largs,
3108 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 };
3110 struct nfs_lockres res = {
3111 .server = server,
3112 };
3113 struct rpc_message msg = {
3114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
3115 .rpc_argp = &arg,
3116 .rpc_resp = &res,
3117 .rpc_cred = state->owner->so_cred,
3118 };
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003119 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
Trond Myklebust06735b32005-10-18 14:20:15 -07003121 largs.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3122 if (largs.lock_seqid == NULL)
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003123 return -ENOMEM;
3124 if (!(lsp->ls_seqid.flags & NFS_SEQID_CONFIRMED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 struct nfs4_state_owner *owner = state->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Trond Myklebust06735b32005-10-18 14:20:15 -07003127 largs.open_seqid = nfs_alloc_seqid(&owner->so_seqid);
3128 if (largs.open_seqid == NULL)
3129 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 largs.new_lock_owner = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
Trond Myklebust06735b32005-10-18 14:20:15 -07003132 /* increment open seqid on success, and seqid mutating errors */
3133 if (largs.new_lock_owner != 0) {
3134 nfs_increment_open_seqid(status, largs.open_seqid);
3135 if (status == 0)
3136 nfs_confirm_seqid(&lsp->ls_seqid, 0);
3137 }
3138 nfs_free_seqid(largs.open_seqid);
3139 } else
3140 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
3141 /* increment lock seqid on success, and seqid mutating errors*/
3142 nfs_increment_lock_seqid(status, largs.lock_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 /* save the returned stateid. */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003144 if (status == 0) {
Trond Myklebust06735b32005-10-18 14:20:15 -07003145 memcpy(lsp->ls_stateid.data, res.u.stateid.data,
3146 sizeof(lsp->ls_stateid.data));
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003147 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
3148 } else if (status == -NFS4ERR_DENIED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 status = -EAGAIN;
Trond Myklebust06735b32005-10-18 14:20:15 -07003150out:
3151 nfs_free_seqid(largs.lock_seqid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 return status;
3153}
3154
3155static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
3156{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003157 struct nfs_server *server = NFS_SERVER(state->inode);
3158 struct nfs4_exception exception = { };
3159 int err;
3160
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003161 /* Cache the lock if possible... */
3162 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
3163 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003164 do {
3165 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
3166 if (err != -NFS4ERR_DELAY)
3167 break;
3168 nfs4_handle_exception(server, err, &exception);
3169 } while (exception.retry);
3170 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171}
3172
3173static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
3174{
Trond Myklebust202b50d2005-06-22 17:16:29 +00003175 struct nfs_server *server = NFS_SERVER(state->inode);
3176 struct nfs4_exception exception = { };
3177 int err;
3178
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003179 err = nfs4_set_lock_state(state, request);
3180 if (err != 0)
3181 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00003182 do {
3183 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
3184 if (err != -NFS4ERR_DELAY)
3185 break;
3186 nfs4_handle_exception(server, err, &exception);
3187 } while (exception.retry);
3188 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189}
3190
3191static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3192{
3193 struct nfs4_client *clp = state->owner->so_client;
3194 int status;
3195
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003196 /* Is this a delegated open? */
Trond Myklebustff604062005-11-25 17:10:01 -05003197 if (NFS_I(state->inode)->delegation_state != 0) {
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003198 /* Yes: cache locks! */
3199 status = do_vfs_lock(request->fl_file, request);
3200 /* ...but avoid races with delegation recall... */
3201 if (status < 0 || test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebustff604062005-11-25 17:10:01 -05003202 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 }
Trond Myklebustff604062005-11-25 17:10:01 -05003204 down_read(&clp->cl_sem);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003205 status = nfs4_set_lock_state(state, request);
3206 if (status != 0)
3207 goto out;
3208 status = _nfs4_do_setlk(state, cmd, request, 0);
3209 if (status != 0)
3210 goto out;
3211 /* Note: we always want to sleep here! */
3212 request->fl_flags |= FL_SLEEP;
3213 if (do_vfs_lock(request->fl_file, request) < 0)
3214 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
3215out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 up_read(&clp->cl_sem);
3217 return status;
3218}
3219
3220static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3221{
3222 struct nfs4_exception exception = { };
3223 int err;
3224
3225 do {
3226 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3227 _nfs4_proc_setlk(state, cmd, request),
3228 &exception);
3229 } while (exception.retry);
3230 return err;
3231}
3232
3233static int
3234nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
3235{
3236 struct nfs_open_context *ctx;
3237 struct nfs4_state *state;
3238 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
3239 int status;
3240
3241 /* verify open state */
3242 ctx = (struct nfs_open_context *)filp->private_data;
3243 state = ctx->state;
3244
3245 if (request->fl_start < 0 || request->fl_end < 0)
3246 return -EINVAL;
3247
3248 if (IS_GETLK(cmd))
3249 return nfs4_proc_getlk(state, F_GETLK, request);
3250
3251 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
3252 return -EINVAL;
3253
3254 if (request->fl_type == F_UNLCK)
3255 return nfs4_proc_unlck(state, cmd, request);
3256
3257 do {
3258 status = nfs4_proc_setlk(state, cmd, request);
3259 if ((status != -EAGAIN) || IS_SETLK(cmd))
3260 break;
3261 timeout = nfs4_set_lock_task_retry(timeout);
3262 status = -ERESTARTSYS;
3263 if (signalled())
3264 break;
3265 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 return status;
3267}
3268
Trond Myklebust888e6942005-11-04 15:38:11 -05003269int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
3270{
3271 struct nfs_server *server = NFS_SERVER(state->inode);
3272 struct nfs4_exception exception = { };
3273 int err;
3274
3275 err = nfs4_set_lock_state(state, fl);
3276 if (err != 0)
3277 goto out;
3278 do {
3279 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
3280 if (err != -NFS4ERR_DELAY)
3281 break;
3282 err = nfs4_handle_exception(server, err, &exception);
3283 } while (exception.retry);
3284out:
3285 return err;
3286}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003287
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003288#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
3289
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003290int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
3291 size_t buflen, int flags)
3292{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003293 struct inode *inode = dentry->d_inode;
3294
3295 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3296 return -EOPNOTSUPP;
3297
3298 if (!S_ISREG(inode->i_mode) &&
3299 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
3300 return -EPERM;
3301
3302 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003303}
3304
3305/* The getxattr man page suggests returning -ENODATA for unknown attributes,
3306 * and that's what we'll do for e.g. user attributes that haven't been set.
3307 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
3308 * attributes in kernel-managed attribute namespaces. */
3309ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
3310 size_t buflen)
3311{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003312 struct inode *inode = dentry->d_inode;
3313
3314 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
3315 return -EOPNOTSUPP;
3316
3317 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003318}
3319
3320ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
3321{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003322 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003323
3324 if (buf && buflen < len)
3325 return -ERANGE;
3326 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003327 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
3328 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003329}
3330
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
3332 .recover_open = nfs4_open_reclaim,
3333 .recover_lock = nfs4_lock_reclaim,
3334};
3335
3336struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
3337 .recover_open = nfs4_open_expired,
3338 .recover_lock = nfs4_lock_expired,
3339};
3340
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003341static struct inode_operations nfs4_file_inode_operations = {
3342 .permission = nfs_permission,
3343 .getattr = nfs_getattr,
3344 .setattr = nfs_setattr,
3345 .getxattr = nfs4_getxattr,
3346 .setxattr = nfs4_setxattr,
3347 .listxattr = nfs4_listxattr,
3348};
3349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350struct nfs_rpc_ops nfs_v4_clientops = {
3351 .version = 4, /* protocol version */
3352 .dentry_ops = &nfs4_dentry_operations,
3353 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00003354 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 .getroot = nfs4_proc_get_root,
3356 .getattr = nfs4_proc_getattr,
3357 .setattr = nfs4_proc_setattr,
3358 .lookup = nfs4_proc_lookup,
3359 .access = nfs4_proc_access,
3360 .readlink = nfs4_proc_readlink,
3361 .read = nfs4_proc_read,
3362 .write = nfs4_proc_write,
3363 .commit = nfs4_proc_commit,
3364 .create = nfs4_proc_create,
3365 .remove = nfs4_proc_remove,
3366 .unlink_setup = nfs4_proc_unlink_setup,
3367 .unlink_done = nfs4_proc_unlink_done,
3368 .rename = nfs4_proc_rename,
3369 .link = nfs4_proc_link,
3370 .symlink = nfs4_proc_symlink,
3371 .mkdir = nfs4_proc_mkdir,
3372 .rmdir = nfs4_proc_remove,
3373 .readdir = nfs4_proc_readdir,
3374 .mknod = nfs4_proc_mknod,
3375 .statfs = nfs4_proc_statfs,
3376 .fsinfo = nfs4_proc_fsinfo,
3377 .pathconf = nfs4_proc_pathconf,
3378 .decode_dirent = nfs4_decode_dirent,
3379 .read_setup = nfs4_proc_read_setup,
3380 .write_setup = nfs4_proc_write_setup,
3381 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust02a913a2005-10-18 14:20:17 -07003382 .file_open = nfs_open,
3383 .file_release = nfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003385 .clear_acl_cache = nfs4_zap_acl_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386};
3387
3388/*
3389 * Local variables:
3390 * c-basic-offset: 8
3391 * End:
3392 */