aboutsummaryrefslogtreecommitdiffstats
path: root/fs/nfs/nfs4proc.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /fs/nfs/nfs4proc.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'fs/nfs/nfs4proc.c')
-rw-r--r--fs/nfs/nfs4proc.c2786
1 files changed, 2786 insertions, 0 deletions
diff --git a/fs/nfs/nfs4proc.c b/fs/nfs/nfs4proc.c
new file mode 100644
index 000000000000..1d5cb3e80c3e
--- /dev/null
+++ b/fs/nfs/nfs4proc.c
@@ -0,0 +1,2786 @@
1/*
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>
50
51#include "delegation.h"
52
53#define NFSDBG_FACILITY NFSDBG_PROC
54
55#define NFS4_POLL_RETRY_MIN (1*HZ)
56#define NFS4_POLL_RETRY_MAX (15*HZ)
57
58static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
59static int nfs4_async_handle_error(struct rpc_task *, struct nfs_server *);
60static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry);
61static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception);
62extern u32 *nfs4_decode_dirent(u32 *p, struct nfs_entry *entry, int plus);
63extern struct rpc_procinfo nfs4_procedures[];
64
65extern nfs4_stateid zero_stateid;
66
67/* Prevent leaks of NFSv4 errors into userland */
68int nfs4_map_errors(int err)
69{
70 if (err < -1000) {
71 dprintk("%s could not handle NFSv4 error %d\n",
72 __FUNCTION__, -err);
73 return -EIO;
74 }
75 return err;
76}
77
78/*
79 * This is our standard bitmap for GETATTR requests.
80 */
81const u32 nfs4_fattr_bitmap[2] = {
82 FATTR4_WORD0_TYPE
83 | FATTR4_WORD0_CHANGE
84 | FATTR4_WORD0_SIZE
85 | FATTR4_WORD0_FSID
86 | FATTR4_WORD0_FILEID,
87 FATTR4_WORD1_MODE
88 | FATTR4_WORD1_NUMLINKS
89 | FATTR4_WORD1_OWNER
90 | FATTR4_WORD1_OWNER_GROUP
91 | FATTR4_WORD1_RAWDEV
92 | FATTR4_WORD1_SPACE_USED
93 | FATTR4_WORD1_TIME_ACCESS
94 | FATTR4_WORD1_TIME_METADATA
95 | FATTR4_WORD1_TIME_MODIFY
96};
97
98const u32 nfs4_statfs_bitmap[2] = {
99 FATTR4_WORD0_FILES_AVAIL
100 | FATTR4_WORD0_FILES_FREE
101 | FATTR4_WORD0_FILES_TOTAL,
102 FATTR4_WORD1_SPACE_AVAIL
103 | FATTR4_WORD1_SPACE_FREE
104 | FATTR4_WORD1_SPACE_TOTAL
105};
106
107u32 nfs4_pathconf_bitmap[2] = {
108 FATTR4_WORD0_MAXLINK
109 | FATTR4_WORD0_MAXNAME,
110 0
111};
112
113const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
114 | FATTR4_WORD0_MAXREAD
115 | FATTR4_WORD0_MAXWRITE
116 | FATTR4_WORD0_LEASE_TIME,
117 0
118};
119
120static void nfs4_setup_readdir(u64 cookie, u32 *verifier, struct dentry *dentry,
121 struct nfs4_readdir_arg *readdir)
122{
123 u32 *start, *p;
124
125 BUG_ON(readdir->count < 80);
126 if (cookie > 2) {
127 readdir->cookie = (cookie > 2) ? cookie : 0;
128 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
129 return;
130 }
131
132 readdir->cookie = 0;
133 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
134 if (cookie == 2)
135 return;
136
137 /*
138 * NFSv4 servers do not return entries for '.' and '..'
139 * Therefore, we fake these entries here. We let '.'
140 * have cookie 0 and '..' have cookie 1. Note that
141 * when talking to the server, we always send cookie 0
142 * instead of 1 or 2.
143 */
144 start = p = (u32 *)kmap_atomic(*readdir->pages, KM_USER0);
145
146 if (cookie == 0) {
147 *p++ = xdr_one; /* next */
148 *p++ = xdr_zero; /* cookie, first word */
149 *p++ = xdr_one; /* cookie, second word */
150 *p++ = xdr_one; /* entry len */
151 memcpy(p, ".\0\0\0", 4); /* entry */
152 p++;
153 *p++ = xdr_one; /* bitmap length */
154 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
155 *p++ = htonl(8); /* attribute buffer length */
156 p = xdr_encode_hyper(p, dentry->d_inode->i_ino);
157 }
158
159 *p++ = xdr_one; /* next */
160 *p++ = xdr_zero; /* cookie, first word */
161 *p++ = xdr_two; /* cookie, second word */
162 *p++ = xdr_two; /* entry len */
163 memcpy(p, "..\0\0", 4); /* entry */
164 p++;
165 *p++ = xdr_one; /* bitmap length */
166 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
167 *p++ = htonl(8); /* attribute buffer length */
168 p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino);
169
170 readdir->pgbase = (char *)p - (char *)start;
171 readdir->count -= readdir->pgbase;
172 kunmap_atomic(start, KM_USER0);
173}
174
175static void
176renew_lease(struct nfs_server *server, unsigned long timestamp)
177{
178 struct nfs4_client *clp = server->nfs4_state;
179 spin_lock(&clp->cl_lock);
180 if (time_before(clp->cl_last_renewal,timestamp))
181 clp->cl_last_renewal = timestamp;
182 spin_unlock(&clp->cl_lock);
183}
184
185static void update_changeattr(struct inode *inode, struct nfs4_change_info *cinfo)
186{
187 struct nfs_inode *nfsi = NFS_I(inode);
188
189 if (cinfo->before == nfsi->change_attr && cinfo->atomic)
190 nfsi->change_attr = cinfo->after;
191}
192
193static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags)
194{
195 struct inode *inode = state->inode;
196
197 open_flags &= (FMODE_READ|FMODE_WRITE);
198 /* Protect against nfs4_find_state() */
199 spin_lock(&inode->i_lock);
200 state->state |= open_flags;
201 /* NB! List reordering - see the reclaim code for why. */
202 if ((open_flags & FMODE_WRITE) && 0 == state->nwriters++)
203 list_move(&state->open_states, &state->owner->so_states);
204 if (open_flags & FMODE_READ)
205 state->nreaders++;
206 memcpy(&state->stateid, stateid, sizeof(state->stateid));
207 spin_unlock(&inode->i_lock);
208}
209
210/*
211 * OPEN_RECLAIM:
212 * reclaim state on the server after a reboot.
213 * Assumes caller is holding the sp->so_sem
214 */
215static int _nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
216{
217 struct inode *inode = state->inode;
218 struct nfs_server *server = NFS_SERVER(inode);
219 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
220 struct nfs_openargs o_arg = {
221 .fh = NFS_FH(inode),
222 .seqid = sp->so_seqid,
223 .id = sp->so_id,
224 .open_flags = state->state,
225 .clientid = server->nfs4_state->cl_clientid,
226 .claim = NFS4_OPEN_CLAIM_PREVIOUS,
227 .bitmask = server->attr_bitmask,
228 };
229 struct nfs_openres o_res = {
230 .server = server, /* Grrr */
231 };
232 struct rpc_message msg = {
233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
234 .rpc_argp = &o_arg,
235 .rpc_resp = &o_res,
236 .rpc_cred = sp->so_cred,
237 };
238 int status;
239
240 if (delegation != NULL) {
241 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
242 memcpy(&state->stateid, &delegation->stateid,
243 sizeof(state->stateid));
244 set_bit(NFS_DELEGATED_STATE, &state->flags);
245 return 0;
246 }
247 o_arg.u.delegation_type = delegation->type;
248 }
249 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
250 nfs4_increment_seqid(status, sp);
251 if (status == 0) {
252 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
253 if (o_res.delegation_type != 0) {
254 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
255 /* Did the server issue an immediate delegation recall? */
256 if (o_res.do_recall)
257 nfs_async_inode_return_delegation(inode, &o_res.stateid);
258 }
259 }
260 clear_bit(NFS_DELEGATED_STATE, &state->flags);
261 /* Ensure we update the inode attributes */
262 NFS_CACHEINV(inode);
263 return status;
264}
265
266static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
267{
268 struct nfs_server *server = NFS_SERVER(state->inode);
269 struct nfs4_exception exception = { };
270 int err;
271 do {
272 err = _nfs4_open_reclaim(sp, state);
273 switch (err) {
274 case 0:
275 case -NFS4ERR_STALE_CLIENTID:
276 case -NFS4ERR_STALE_STATEID:
277 case -NFS4ERR_EXPIRED:
278 return err;
279 }
280 err = nfs4_handle_exception(server, err, &exception);
281 } while (exception.retry);
282 return err;
283}
284
285static int _nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
286{
287 struct nfs4_state_owner *sp = state->owner;
288 struct inode *inode = dentry->d_inode;
289 struct nfs_server *server = NFS_SERVER(inode);
290 struct dentry *parent = dget_parent(dentry);
291 struct nfs_openargs arg = {
292 .fh = NFS_FH(parent->d_inode),
293 .clientid = server->nfs4_state->cl_clientid,
294 .name = &dentry->d_name,
295 .id = sp->so_id,
296 .server = server,
297 .bitmask = server->attr_bitmask,
298 .claim = NFS4_OPEN_CLAIM_DELEGATE_CUR,
299 };
300 struct nfs_openres res = {
301 .server = server,
302 };
303 struct rpc_message msg = {
304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR],
305 .rpc_argp = &arg,
306 .rpc_resp = &res,
307 .rpc_cred = sp->so_cred,
308 };
309 int status = 0;
310
311 down(&sp->so_sema);
312 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
313 goto out;
314 if (state->state == 0)
315 goto out;
316 arg.seqid = sp->so_seqid;
317 arg.open_flags = state->state;
318 memcpy(arg.u.delegation.data, state->stateid.data, sizeof(arg.u.delegation.data));
319 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
320 nfs4_increment_seqid(status, sp);
321 if (status >= 0) {
322 memcpy(state->stateid.data, res.stateid.data,
323 sizeof(state->stateid.data));
324 clear_bit(NFS_DELEGATED_STATE, &state->flags);
325 }
326out:
327 up(&sp->so_sema);
328 dput(parent);
329 return status;
330}
331
332int nfs4_open_delegation_recall(struct dentry *dentry, struct nfs4_state *state)
333{
334 struct nfs4_exception exception = { };
335 struct nfs_server *server = NFS_SERVER(dentry->d_inode);
336 int err;
337 do {
338 err = _nfs4_open_delegation_recall(dentry, state);
339 switch (err) {
340 case 0:
341 return err;
342 case -NFS4ERR_STALE_CLIENTID:
343 case -NFS4ERR_STALE_STATEID:
344 case -NFS4ERR_EXPIRED:
345 /* Don't recall a delegation if it was lost */
346 nfs4_schedule_state_recovery(server->nfs4_state);
347 return err;
348 }
349 err = nfs4_handle_exception(server, err, &exception);
350 } while (exception.retry);
351 return err;
352}
353
354static inline int _nfs4_proc_open_confirm(struct rpc_clnt *clnt, const struct nfs_fh *fh, struct nfs4_state_owner *sp, nfs4_stateid *stateid)
355{
356 struct nfs_open_confirmargs arg = {
357 .fh = fh,
358 .seqid = sp->so_seqid,
359 .stateid = *stateid,
360 };
361 struct nfs_open_confirmres res;
362 struct rpc_message msg = {
363 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
364 .rpc_argp = &arg,
365 .rpc_resp = &res,
366 .rpc_cred = sp->so_cred,
367 };
368 int status;
369
370 status = rpc_call_sync(clnt, &msg, RPC_TASK_NOINTR);
371 nfs4_increment_seqid(status, sp);
372 if (status >= 0)
373 memcpy(stateid, &res.stateid, sizeof(*stateid));
374 return status;
375}
376
377static int _nfs4_proc_open(struct inode *dir, struct nfs4_state_owner *sp, struct nfs_openargs *o_arg, struct nfs_openres *o_res)
378{
379 struct nfs_server *server = NFS_SERVER(dir);
380 struct rpc_message msg = {
381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
382 .rpc_argp = o_arg,
383 .rpc_resp = o_res,
384 .rpc_cred = sp->so_cred,
385 };
386 int status;
387
388 /* Update sequence id. The caller must serialize! */
389 o_arg->seqid = sp->so_seqid;
390 o_arg->id = sp->so_id;
391 o_arg->clientid = sp->so_client->cl_clientid;
392
393 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
394 nfs4_increment_seqid(status, sp);
395 if (status != 0)
396 goto out;
397 update_changeattr(dir, &o_res->cinfo);
398 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
399 status = _nfs4_proc_open_confirm(server->client, &o_res->fh,
400 sp, &o_res->stateid);
401 if (status != 0)
402 goto out;
403 }
404 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
405 status = server->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr);
406out:
407 return status;
408}
409
410static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags)
411{
412 struct nfs_access_entry cache;
413 int mask = 0;
414 int status;
415
416 if (openflags & FMODE_READ)
417 mask |= MAY_READ;
418 if (openflags & FMODE_WRITE)
419 mask |= MAY_WRITE;
420 status = nfs_access_get_cached(inode, cred, &cache);
421 if (status == 0)
422 goto out;
423
424 /* Be clever: ask server to check for all possible rights */
425 cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
426 cache.cred = cred;
427 cache.jiffies = jiffies;
428 status = _nfs4_proc_access(inode, &cache);
429 if (status != 0)
430 return status;
431 nfs_access_add_cache(inode, &cache);
432out:
433 if ((cache.mask & mask) == mask)
434 return 0;
435 return -EACCES;
436}
437
438/*
439 * OPEN_EXPIRED:
440 * reclaim state on the server after a network partition.
441 * Assumes caller holds the appropriate lock
442 */
443static int _nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state, struct dentry *dentry)
444{
445 struct dentry *parent = dget_parent(dentry);
446 struct inode *dir = parent->d_inode;
447 struct inode *inode = state->inode;
448 struct nfs_server *server = NFS_SERVER(dir);
449 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
450 struct nfs_fattr f_attr = {
451 .valid = 0,
452 };
453 struct nfs_openargs o_arg = {
454 .fh = NFS_FH(dir),
455 .open_flags = state->state,
456 .name = &dentry->d_name,
457 .bitmask = server->attr_bitmask,
458 .claim = NFS4_OPEN_CLAIM_NULL,
459 };
460 struct nfs_openres o_res = {
461 .f_attr = &f_attr,
462 .server = server,
463 };
464 int status = 0;
465
466 if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) {
467 status = _nfs4_do_access(inode, sp->so_cred, state->state);
468 if (status < 0)
469 goto out;
470 memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid));
471 set_bit(NFS_DELEGATED_STATE, &state->flags);
472 goto out;
473 }
474 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
475 if (status != 0)
476 goto out_nodeleg;
477 /* Check if files differ */
478 if ((f_attr.mode & S_IFMT) != (inode->i_mode & S_IFMT))
479 goto out_stale;
480 /* Has the file handle changed? */
481 if (nfs_compare_fh(&o_res.fh, NFS_FH(inode)) != 0) {
482 /* Verify if the change attributes are the same */
483 if (f_attr.change_attr != NFS_I(inode)->change_attr)
484 goto out_stale;
485 if (nfs_size_to_loff_t(f_attr.size) != inode->i_size)
486 goto out_stale;
487 /* Lets just pretend that this is the same file */
488 nfs_copy_fh(NFS_FH(inode), &o_res.fh);
489 NFS_I(inode)->fileid = f_attr.fileid;
490 }
491 memcpy(&state->stateid, &o_res.stateid, sizeof(state->stateid));
492 if (o_res.delegation_type != 0) {
493 if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM))
494 nfs_inode_set_delegation(inode, sp->so_cred, &o_res);
495 else
496 nfs_inode_reclaim_delegation(inode, sp->so_cred, &o_res);
497 }
498out_nodeleg:
499 clear_bit(NFS_DELEGATED_STATE, &state->flags);
500out:
501 dput(parent);
502 return status;
503out_stale:
504 status = -ESTALE;
505 /* Invalidate the state owner so we don't ever use it again */
506 nfs4_drop_state_owner(sp);
507 d_drop(dentry);
508 /* Should we be trying to close that stateid? */
509 goto out_nodeleg;
510}
511
512static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
513{
514 struct nfs_inode *nfsi = NFS_I(state->inode);
515 struct nfs_open_context *ctx;
516 int status;
517
518 spin_lock(&state->inode->i_lock);
519 list_for_each_entry(ctx, &nfsi->open_files, list) {
520 if (ctx->state != state)
521 continue;
522 get_nfs_open_context(ctx);
523 spin_unlock(&state->inode->i_lock);
524 status = _nfs4_open_expired(sp, state, ctx->dentry);
525 put_nfs_open_context(ctx);
526 return status;
527 }
528 spin_unlock(&state->inode->i_lock);
529 return -ENOENT;
530}
531
532/*
533 * Returns an nfs4_state + an extra reference to the inode
534 */
535static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res)
536{
537 struct nfs_delegation *delegation;
538 struct nfs_server *server = NFS_SERVER(inode);
539 struct nfs4_client *clp = server->nfs4_state;
540 struct nfs_inode *nfsi = NFS_I(inode);
541 struct nfs4_state_owner *sp = NULL;
542 struct nfs4_state *state = NULL;
543 int open_flags = flags & (FMODE_READ|FMODE_WRITE);
544 int err;
545
546 /* Protect against reboot recovery - NOTE ORDER! */
547 down_read(&clp->cl_sem);
548 /* Protect against delegation recall */
549 down_read(&nfsi->rwsem);
550 delegation = NFS_I(inode)->delegation;
551 err = -ENOENT;
552 if (delegation == NULL || (delegation->type & open_flags) != open_flags)
553 goto out_err;
554 err = -ENOMEM;
555 if (!(sp = nfs4_get_state_owner(server, cred))) {
556 dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__);
557 goto out_err;
558 }
559 down(&sp->so_sema);
560 state = nfs4_get_open_state(inode, sp);
561 if (state == NULL)
562 goto out_err;
563
564 err = -ENOENT;
565 if ((state->state & open_flags) == open_flags) {
566 spin_lock(&inode->i_lock);
567 if (open_flags & FMODE_READ)
568 state->nreaders++;
569 if (open_flags & FMODE_WRITE)
570 state->nwriters++;
571 spin_unlock(&inode->i_lock);
572 goto out_ok;
573 } else if (state->state != 0)
574 goto out_err;
575
576 lock_kernel();
577 err = _nfs4_do_access(inode, cred, open_flags);
578 unlock_kernel();
579 if (err != 0)
580 goto out_err;
581 set_bit(NFS_DELEGATED_STATE, &state->flags);
582 update_open_stateid(state, &delegation->stateid, open_flags);
583out_ok:
584 up(&sp->so_sema);
585 nfs4_put_state_owner(sp);
586 up_read(&nfsi->rwsem);
587 up_read(&clp->cl_sem);
588 igrab(inode);
589 *res = state;
590 return 0;
591out_err:
592 if (sp != NULL) {
593 if (state != NULL)
594 nfs4_put_open_state(state);
595 up(&sp->so_sema);
596 nfs4_put_state_owner(sp);
597 }
598 up_read(&nfsi->rwsem);
599 up_read(&clp->cl_sem);
600 return err;
601}
602
603static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred)
604{
605 struct nfs4_exception exception = { };
606 struct nfs4_state *res;
607 int err;
608
609 do {
610 err = _nfs4_open_delegated(inode, flags, cred, &res);
611 if (err == 0)
612 break;
613 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode),
614 err, &exception));
615 } while (exception.retry);
616 return res;
617}
618
619/*
620 * Returns an nfs4_state + an referenced inode
621 */
622static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
623{
624 struct nfs4_state_owner *sp;
625 struct nfs4_state *state = NULL;
626 struct nfs_server *server = NFS_SERVER(dir);
627 struct nfs4_client *clp = server->nfs4_state;
628 struct inode *inode = NULL;
629 int status;
630 struct nfs_fattr f_attr = {
631 .valid = 0,
632 };
633 struct nfs_openargs o_arg = {
634 .fh = NFS_FH(dir),
635 .open_flags = flags,
636 .name = &dentry->d_name,
637 .server = server,
638 .bitmask = server->attr_bitmask,
639 .claim = NFS4_OPEN_CLAIM_NULL,
640 };
641 struct nfs_openres o_res = {
642 .f_attr = &f_attr,
643 .server = server,
644 };
645
646 /* Protect against reboot recovery conflicts */
647 down_read(&clp->cl_sem);
648 status = -ENOMEM;
649 if (!(sp = nfs4_get_state_owner(server, cred))) {
650 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
651 goto out_err;
652 }
653 if (flags & O_EXCL) {
654 u32 *p = (u32 *) o_arg.u.verifier.data;
655 p[0] = jiffies;
656 p[1] = current->pid;
657 } else
658 o_arg.u.attrs = sattr;
659 /* Serialization for the sequence id */
660 down(&sp->so_sema);
661
662 status = _nfs4_proc_open(dir, sp, &o_arg, &o_res);
663 if (status != 0)
664 goto out_err;
665
666 status = -ENOMEM;
667 inode = nfs_fhget(dir->i_sb, &o_res.fh, &f_attr);
668 if (!inode)
669 goto out_err;
670 state = nfs4_get_open_state(inode, sp);
671 if (!state)
672 goto out_err;
673 update_open_stateid(state, &o_res.stateid, flags);
674 if (o_res.delegation_type != 0)
675 nfs_inode_set_delegation(inode, cred, &o_res);
676 up(&sp->so_sema);
677 nfs4_put_state_owner(sp);
678 up_read(&clp->cl_sem);
679 *res = state;
680 return 0;
681out_err:
682 if (sp != NULL) {
683 if (state != NULL)
684 nfs4_put_open_state(state);
685 up(&sp->so_sema);
686 nfs4_put_state_owner(sp);
687 }
688 /* Note: clp->cl_sem must be released before nfs4_put_open_state()! */
689 up_read(&clp->cl_sem);
690 if (inode != NULL)
691 iput(inode);
692 *res = NULL;
693 return status;
694}
695
696
697static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, int flags, struct iattr *sattr, struct rpc_cred *cred)
698{
699 struct nfs4_exception exception = { };
700 struct nfs4_state *res;
701 int status;
702
703 do {
704 status = _nfs4_do_open(dir, dentry, flags, sattr, cred, &res);
705 if (status == 0)
706 break;
707 /* NOTE: BAD_SEQID means the server and client disagree about the
708 * book-keeping w.r.t. state-changing operations
709 * (OPEN/CLOSE/LOCK/LOCKU...)
710 * It is actually a sign of a bug on the client or on the server.
711 *
712 * If we receive a BAD_SEQID error in the particular case of
713 * doing an OPEN, we assume that nfs4_increment_seqid() will
714 * have unhashed the old state_owner for us, and that we can
715 * therefore safely retry using a new one. We should still warn
716 * the user though...
717 */
718 if (status == -NFS4ERR_BAD_SEQID) {
719 printk(KERN_WARNING "NFS: v4 server returned a bad sequence-id error!\n");
720 exception.retry = 1;
721 continue;
722 }
723 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
724 status, &exception));
725 } while (exception.retry);
726 return res;
727}
728
729static int _nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
730 struct nfs_fh *fhandle, struct iattr *sattr,
731 struct nfs4_state *state)
732{
733 struct nfs_setattrargs arg = {
734 .fh = fhandle,
735 .iap = sattr,
736 .server = server,
737 .bitmask = server->attr_bitmask,
738 };
739 struct nfs_setattrres res = {
740 .fattr = fattr,
741 .server = server,
742 };
743 struct rpc_message msg = {
744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
745 .rpc_argp = &arg,
746 .rpc_resp = &res,
747 };
748
749 fattr->valid = 0;
750
751 if (state != NULL)
752 msg.rpc_cred = state->owner->so_cred;
753 if (sattr->ia_valid & ATTR_SIZE)
754 nfs4_copy_stateid(&arg.stateid, state, NULL);
755 else
756 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
757
758 return rpc_call_sync(server->client, &msg, 0);
759}
760
761static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
762 struct nfs_fh *fhandle, struct iattr *sattr,
763 struct nfs4_state *state)
764{
765 struct nfs4_exception exception = { };
766 int err;
767 do {
768 err = nfs4_handle_exception(server,
769 _nfs4_do_setattr(server, fattr, fhandle, sattr,
770 state),
771 &exception);
772 } while (exception.retry);
773 return err;
774}
775
776struct nfs4_closedata {
777 struct inode *inode;
778 struct nfs4_state *state;
779 struct nfs_closeargs arg;
780 struct nfs_closeres res;
781};
782
783static void nfs4_close_done(struct rpc_task *task)
784{
785 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
786 struct nfs4_state *state = calldata->state;
787 struct nfs4_state_owner *sp = state->owner;
788 struct nfs_server *server = NFS_SERVER(calldata->inode);
789
790 /* hmm. we are done with the inode, and in the process of freeing
791 * the state_owner. we keep this around to process errors
792 */
793 nfs4_increment_seqid(task->tk_status, sp);
794 switch (task->tk_status) {
795 case 0:
796 memcpy(&state->stateid, &calldata->res.stateid,
797 sizeof(state->stateid));
798 break;
799 case -NFS4ERR_STALE_STATEID:
800 case -NFS4ERR_EXPIRED:
801 state->state = calldata->arg.open_flags;
802 nfs4_schedule_state_recovery(server->nfs4_state);
803 break;
804 default:
805 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
806 rpc_restart_call(task);
807 return;
808 }
809 }
810 state->state = calldata->arg.open_flags;
811 nfs4_put_open_state(state);
812 up(&sp->so_sema);
813 nfs4_put_state_owner(sp);
814 up_read(&server->nfs4_state->cl_sem);
815 kfree(calldata);
816}
817
818static inline int nfs4_close_call(struct rpc_clnt *clnt, struct nfs4_closedata *calldata)
819{
820 struct rpc_message msg = {
821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
822 .rpc_argp = &calldata->arg,
823 .rpc_resp = &calldata->res,
824 .rpc_cred = calldata->state->owner->so_cred,
825 };
826 if (calldata->arg.open_flags != 0)
827 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
828 return rpc_call_async(clnt, &msg, 0, nfs4_close_done, calldata);
829}
830
831/*
832 * It is possible for data to be read/written from a mem-mapped file
833 * after the sys_close call (which hits the vfs layer as a flush).
834 * This means that we can't safely call nfsv4 close on a file until
835 * the inode is cleared. This in turn means that we are not good
836 * NFSv4 citizens - we do not indicate to the server to update the file's
837 * share state even when we are done with one of the three share
838 * stateid's in the inode.
839 *
840 * NOTE: Caller must be holding the sp->so_owner semaphore!
841 */
842int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode)
843{
844 struct nfs4_closedata *calldata;
845 int status;
846
847 /* Tell caller we're done */
848 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
849 state->state = mode;
850 return 0;
851 }
852 calldata = (struct nfs4_closedata *)kmalloc(sizeof(*calldata), GFP_KERNEL);
853 if (calldata == NULL)
854 return -ENOMEM;
855 calldata->inode = inode;
856 calldata->state = state;
857 calldata->arg.fh = NFS_FH(inode);
858 /* Serialization for the sequence id */
859 calldata->arg.seqid = state->owner->so_seqid;
860 calldata->arg.open_flags = mode;
861 memcpy(&calldata->arg.stateid, &state->stateid,
862 sizeof(calldata->arg.stateid));
863 status = nfs4_close_call(NFS_SERVER(inode)->client, calldata);
864 /*
865 * Return -EINPROGRESS on success in order to indicate to the
866 * caller that an asynchronous RPC call has been launched, and
867 * that it will release the semaphores on completion.
868 */
869 return (status == 0) ? -EINPROGRESS : status;
870}
871
872struct inode *
873nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
874{
875 struct iattr attr;
876 struct rpc_cred *cred;
877 struct nfs4_state *state;
878
879 if (nd->flags & LOOKUP_CREATE) {
880 attr.ia_mode = nd->intent.open.create_mode;
881 attr.ia_valid = ATTR_MODE;
882 if (!IS_POSIXACL(dir))
883 attr.ia_mode &= ~current->fs->umask;
884 } else {
885 attr.ia_valid = 0;
886 BUG_ON(nd->intent.open.flags & O_CREAT);
887 }
888
889 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
890 if (IS_ERR(cred))
891 return (struct inode *)cred;
892 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
893 put_rpccred(cred);
894 if (IS_ERR(state))
895 return (struct inode *)state;
896 return state->inode;
897}
898
899int
900nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags)
901{
902 struct rpc_cred *cred;
903 struct nfs4_state *state;
904 struct inode *inode;
905
906 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
907 if (IS_ERR(cred))
908 return PTR_ERR(cred);
909 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
910 if (IS_ERR(state))
911 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
912 put_rpccred(cred);
913 if (state == ERR_PTR(-ENOENT) && dentry->d_inode == 0)
914 return 1;
915 if (IS_ERR(state))
916 return 0;
917 inode = state->inode;
918 if (inode == dentry->d_inode) {
919 iput(inode);
920 return 1;
921 }
922 d_drop(dentry);
923 nfs4_close_state(state, openflags);
924 iput(inode);
925 return 0;
926}
927
928
929static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
930{
931 struct nfs4_server_caps_res res = {};
932 struct rpc_message msg = {
933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
934 .rpc_argp = fhandle,
935 .rpc_resp = &res,
936 };
937 int status;
938
939 status = rpc_call_sync(server->client, &msg, 0);
940 if (status == 0) {
941 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
942 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
943 server->caps |= NFS_CAP_ACLS;
944 if (res.has_links != 0)
945 server->caps |= NFS_CAP_HARDLINKS;
946 if (res.has_symlinks != 0)
947 server->caps |= NFS_CAP_SYMLINKS;
948 server->acl_bitmask = res.acl_bitmask;
949 }
950 return status;
951}
952
953static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
954{
955 struct nfs4_exception exception = { };
956 int err;
957 do {
958 err = nfs4_handle_exception(server,
959 _nfs4_server_capabilities(server, fhandle),
960 &exception);
961 } while (exception.retry);
962 return err;
963}
964
965static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
966 struct nfs_fsinfo *info)
967{
968 struct nfs_fattr * fattr = info->fattr;
969 struct nfs4_lookup_root_arg args = {
970 .bitmask = nfs4_fattr_bitmap,
971 };
972 struct nfs4_lookup_res res = {
973 .server = server,
974 .fattr = fattr,
975 .fh = fhandle,
976 };
977 struct rpc_message msg = {
978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
979 .rpc_argp = &args,
980 .rpc_resp = &res,
981 };
982 fattr->valid = 0;
983 return rpc_call_sync(server->client, &msg, 0);
984}
985
986static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
987 struct nfs_fsinfo *info)
988{
989 struct nfs4_exception exception = { };
990 int err;
991 do {
992 err = nfs4_handle_exception(server,
993 _nfs4_lookup_root(server, fhandle, info),
994 &exception);
995 } while (exception.retry);
996 return err;
997}
998
999static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1000 struct nfs_fsinfo *info)
1001{
1002 struct nfs_fattr * fattr = info->fattr;
1003 unsigned char * p;
1004 struct qstr q;
1005 struct nfs4_lookup_arg args = {
1006 .dir_fh = fhandle,
1007 .name = &q,
1008 .bitmask = nfs4_fattr_bitmap,
1009 };
1010 struct nfs4_lookup_res res = {
1011 .server = server,
1012 .fattr = fattr,
1013 .fh = fhandle,
1014 };
1015 struct rpc_message msg = {
1016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1017 .rpc_argp = &args,
1018 .rpc_resp = &res,
1019 };
1020 int status;
1021
1022 /*
1023 * Now we do a separate LOOKUP for each component of the mount path.
1024 * The LOOKUPs are done separately so that we can conveniently
1025 * catch an ERR_WRONGSEC if it occurs along the way...
1026 */
1027 status = nfs4_lookup_root(server, fhandle, info);
1028 if (status)
1029 goto out;
1030
1031 p = server->mnt_path;
1032 for (;;) {
1033 struct nfs4_exception exception = { };
1034
1035 while (*p == '/')
1036 p++;
1037 if (!*p)
1038 break;
1039 q.name = p;
1040 while (*p && (*p != '/'))
1041 p++;
1042 q.len = p - q.name;
1043
1044 do {
1045 fattr->valid = 0;
1046 status = nfs4_handle_exception(server,
1047 rpc_call_sync(server->client, &msg, 0),
1048 &exception);
1049 } while (exception.retry);
1050 if (status == 0)
1051 continue;
1052 if (status == -ENOENT) {
1053 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1054 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1055 }
1056 break;
1057 }
1058 if (status == 0)
1059 status = nfs4_server_capabilities(server, fhandle);
1060 if (status == 0)
1061 status = nfs4_do_fsinfo(server, fhandle, info);
1062out:
1063 return status;
1064}
1065
1066static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1067{
1068 struct nfs4_getattr_arg args = {
1069 .fh = fhandle,
1070 .bitmask = server->attr_bitmask,
1071 };
1072 struct nfs4_getattr_res res = {
1073 .fattr = fattr,
1074 .server = server,
1075 };
1076 struct rpc_message msg = {
1077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1078 .rpc_argp = &args,
1079 .rpc_resp = &res,
1080 };
1081
1082 fattr->valid = 0;
1083 return rpc_call_sync(server->client, &msg, 0);
1084}
1085
1086static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1087{
1088 struct nfs4_exception exception = { };
1089 int err;
1090 do {
1091 err = nfs4_handle_exception(server,
1092 _nfs4_proc_getattr(server, fhandle, fattr),
1093 &exception);
1094 } while (exception.retry);
1095 return err;
1096}
1097
1098/*
1099 * The file is not closed if it is opened due to the a request to change
1100 * the size of the file. The open call will not be needed once the
1101 * VFS layer lookup-intents are implemented.
1102 *
1103 * Close is called when the inode is destroyed.
1104 * If we haven't opened the file for O_WRONLY, we
1105 * need to in the size_change case to obtain a stateid.
1106 *
1107 * Got race?
1108 * Because OPEN is always done by name in nfsv4, it is
1109 * possible that we opened a different file by the same
1110 * name. We can recognize this race condition, but we
1111 * can't do anything about it besides returning an error.
1112 *
1113 * This will be fixed with VFS changes (lookup-intent).
1114 */
1115static int
1116nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1117 struct iattr *sattr)
1118{
1119 struct inode * inode = dentry->d_inode;
1120 int size_change = sattr->ia_valid & ATTR_SIZE;
1121 struct nfs4_state *state = NULL;
1122 int need_iput = 0;
1123 int status;
1124
1125 fattr->valid = 0;
1126
1127 if (size_change) {
1128 struct rpc_cred *cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1129 if (IS_ERR(cred))
1130 return PTR_ERR(cred);
1131 state = nfs4_find_state(inode, cred, FMODE_WRITE);
1132 if (state == NULL) {
1133 state = nfs4_open_delegated(dentry->d_inode,
1134 FMODE_WRITE, cred);
1135 if (IS_ERR(state))
1136 state = nfs4_do_open(dentry->d_parent->d_inode,
1137 dentry, FMODE_WRITE,
1138 NULL, cred);
1139 need_iput = 1;
1140 }
1141 put_rpccred(cred);
1142 if (IS_ERR(state))
1143 return PTR_ERR(state);
1144
1145 if (state->inode != inode) {
1146 printk(KERN_WARNING "nfs: raced in setattr (%p != %p), returning -EIO\n", inode, state->inode);
1147 status = -EIO;
1148 goto out;
1149 }
1150 }
1151 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1152 NFS_FH(inode), sattr, state);
1153out:
1154 if (state) {
1155 inode = state->inode;
1156 nfs4_close_state(state, FMODE_WRITE);
1157 if (need_iput)
1158 iput(inode);
1159 }
1160 return status;
1161}
1162
1163static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1164 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1165{
1166 int status;
1167 struct nfs_server *server = NFS_SERVER(dir);
1168 struct nfs4_lookup_arg args = {
1169 .bitmask = server->attr_bitmask,
1170 .dir_fh = NFS_FH(dir),
1171 .name = name,
1172 };
1173 struct nfs4_lookup_res res = {
1174 .server = server,
1175 .fattr = fattr,
1176 .fh = fhandle,
1177 };
1178 struct rpc_message msg = {
1179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1180 .rpc_argp = &args,
1181 .rpc_resp = &res,
1182 };
1183
1184 fattr->valid = 0;
1185
1186 dprintk("NFS call lookup %s\n", name->name);
1187 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1188 dprintk("NFS reply lookup: %d\n", status);
1189 return status;
1190}
1191
1192static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1193{
1194 struct nfs4_exception exception = { };
1195 int err;
1196 do {
1197 err = nfs4_handle_exception(NFS_SERVER(dir),
1198 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1199 &exception);
1200 } while (exception.retry);
1201 return err;
1202}
1203
1204static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1205{
1206 struct nfs4_accessargs args = {
1207 .fh = NFS_FH(inode),
1208 };
1209 struct nfs4_accessres res = { 0 };
1210 struct rpc_message msg = {
1211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1212 .rpc_argp = &args,
1213 .rpc_resp = &res,
1214 .rpc_cred = entry->cred,
1215 };
1216 int mode = entry->mask;
1217 int status;
1218
1219 /*
1220 * Determine which access bits we want to ask for...
1221 */
1222 if (mode & MAY_READ)
1223 args.access |= NFS4_ACCESS_READ;
1224 if (S_ISDIR(inode->i_mode)) {
1225 if (mode & MAY_WRITE)
1226 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1227 if (mode & MAY_EXEC)
1228 args.access |= NFS4_ACCESS_LOOKUP;
1229 } else {
1230 if (mode & MAY_WRITE)
1231 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1232 if (mode & MAY_EXEC)
1233 args.access |= NFS4_ACCESS_EXECUTE;
1234 }
1235 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1236 if (!status) {
1237 entry->mask = 0;
1238 if (res.access & NFS4_ACCESS_READ)
1239 entry->mask |= MAY_READ;
1240 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1241 entry->mask |= MAY_WRITE;
1242 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1243 entry->mask |= MAY_EXEC;
1244 }
1245 return status;
1246}
1247
1248static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1249{
1250 struct nfs4_exception exception = { };
1251 int err;
1252 do {
1253 err = nfs4_handle_exception(NFS_SERVER(inode),
1254 _nfs4_proc_access(inode, entry),
1255 &exception);
1256 } while (exception.retry);
1257 return err;
1258}
1259
1260/*
1261 * TODO: For the time being, we don't try to get any attributes
1262 * along with any of the zero-copy operations READ, READDIR,
1263 * READLINK, WRITE.
1264 *
1265 * In the case of the first three, we want to put the GETATTR
1266 * after the read-type operation -- this is because it is hard
1267 * to predict the length of a GETATTR response in v4, and thus
1268 * align the READ data correctly. This means that the GETATTR
1269 * may end up partially falling into the page cache, and we should
1270 * shift it into the 'tail' of the xdr_buf before processing.
1271 * To do this efficiently, we need to know the total length
1272 * of data received, which doesn't seem to be available outside
1273 * of the RPC layer.
1274 *
1275 * In the case of WRITE, we also want to put the GETATTR after
1276 * the operation -- in this case because we want to make sure
1277 * we get the post-operation mtime and size. This means that
1278 * we can't use xdr_encode_pages() as written: we need a variant
1279 * of it which would leave room in the 'tail' iovec.
1280 *
1281 * Both of these changes to the XDR layer would in fact be quite
1282 * minor, but I decided to leave them for a subsequent patch.
1283 */
1284static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1285 unsigned int pgbase, unsigned int pglen)
1286{
1287 struct nfs4_readlink args = {
1288 .fh = NFS_FH(inode),
1289 .pgbase = pgbase,
1290 .pglen = pglen,
1291 .pages = &page,
1292 };
1293 struct rpc_message msg = {
1294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1295 .rpc_argp = &args,
1296 .rpc_resp = NULL,
1297 };
1298
1299 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1300}
1301
1302static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1303 unsigned int pgbase, unsigned int pglen)
1304{
1305 struct nfs4_exception exception = { };
1306 int err;
1307 do {
1308 err = nfs4_handle_exception(NFS_SERVER(inode),
1309 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1310 &exception);
1311 } while (exception.retry);
1312 return err;
1313}
1314
1315static int _nfs4_proc_read(struct nfs_read_data *rdata)
1316{
1317 int flags = rdata->flags;
1318 struct inode *inode = rdata->inode;
1319 struct nfs_fattr *fattr = rdata->res.fattr;
1320 struct nfs_server *server = NFS_SERVER(inode);
1321 struct rpc_message msg = {
1322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1323 .rpc_argp = &rdata->args,
1324 .rpc_resp = &rdata->res,
1325 .rpc_cred = rdata->cred,
1326 };
1327 unsigned long timestamp = jiffies;
1328 int status;
1329
1330 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1331 (long long) rdata->args.offset);
1332
1333 fattr->valid = 0;
1334 status = rpc_call_sync(server->client, &msg, flags);
1335 if (!status)
1336 renew_lease(server, timestamp);
1337 dprintk("NFS reply read: %d\n", status);
1338 return status;
1339}
1340
1341static int nfs4_proc_read(struct nfs_read_data *rdata)
1342{
1343 struct nfs4_exception exception = { };
1344 int err;
1345 do {
1346 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1347 _nfs4_proc_read(rdata),
1348 &exception);
1349 } while (exception.retry);
1350 return err;
1351}
1352
1353static int _nfs4_proc_write(struct nfs_write_data *wdata)
1354{
1355 int rpcflags = wdata->flags;
1356 struct inode *inode = wdata->inode;
1357 struct nfs_fattr *fattr = wdata->res.fattr;
1358 struct nfs_server *server = NFS_SERVER(inode);
1359 struct rpc_message msg = {
1360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1361 .rpc_argp = &wdata->args,
1362 .rpc_resp = &wdata->res,
1363 .rpc_cred = wdata->cred,
1364 };
1365 int status;
1366
1367 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1368 (long long) wdata->args.offset);
1369
1370 fattr->valid = 0;
1371 status = rpc_call_sync(server->client, &msg, rpcflags);
1372 dprintk("NFS reply write: %d\n", status);
1373 return status;
1374}
1375
1376static int nfs4_proc_write(struct nfs_write_data *wdata)
1377{
1378 struct nfs4_exception exception = { };
1379 int err;
1380 do {
1381 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1382 _nfs4_proc_write(wdata),
1383 &exception);
1384 } while (exception.retry);
1385 return err;
1386}
1387
1388static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1389{
1390 struct inode *inode = cdata->inode;
1391 struct nfs_fattr *fattr = cdata->res.fattr;
1392 struct nfs_server *server = NFS_SERVER(inode);
1393 struct rpc_message msg = {
1394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1395 .rpc_argp = &cdata->args,
1396 .rpc_resp = &cdata->res,
1397 .rpc_cred = cdata->cred,
1398 };
1399 int status;
1400
1401 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1402 (long long) cdata->args.offset);
1403
1404 fattr->valid = 0;
1405 status = rpc_call_sync(server->client, &msg, 0);
1406 dprintk("NFS reply commit: %d\n", status);
1407 return status;
1408}
1409
1410static int nfs4_proc_commit(struct nfs_write_data *cdata)
1411{
1412 struct nfs4_exception exception = { };
1413 int err;
1414 do {
1415 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1416 _nfs4_proc_commit(cdata),
1417 &exception);
1418 } while (exception.retry);
1419 return err;
1420}
1421
1422/*
1423 * Got race?
1424 * We will need to arrange for the VFS layer to provide an atomic open.
1425 * Until then, this create/open method is prone to inefficiency and race
1426 * conditions due to the lookup, create, and open VFS calls from sys_open()
1427 * placed on the wire.
1428 *
1429 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1430 * The file will be opened again in the subsequent VFS open call
1431 * (nfs4_proc_file_open).
1432 *
1433 * The open for read will just hang around to be used by any process that
1434 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1435 */
1436
1437static int
1438nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1439 int flags)
1440{
1441 struct nfs4_state *state;
1442 struct rpc_cred *cred;
1443 int status = 0;
1444
1445 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1446 if (IS_ERR(cred)) {
1447 status = PTR_ERR(cred);
1448 goto out;
1449 }
1450 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1451 put_rpccred(cred);
1452 if (IS_ERR(state)) {
1453 status = PTR_ERR(state);
1454 goto out;
1455 }
1456 d_instantiate(dentry, state->inode);
1457 if (flags & O_EXCL) {
1458 struct nfs_fattr fattr;
1459 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1460 NFS_FH(state->inode), sattr, state);
1461 if (status == 0)
1462 goto out;
1463 } else if (flags != 0)
1464 goto out;
1465 nfs4_close_state(state, flags);
1466out:
1467 return status;
1468}
1469
1470static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1471{
1472 struct nfs4_remove_arg args = {
1473 .fh = NFS_FH(dir),
1474 .name = name,
1475 };
1476 struct nfs4_change_info res;
1477 struct rpc_message msg = {
1478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1479 .rpc_argp = &args,
1480 .rpc_resp = &res,
1481 };
1482 int status;
1483
1484 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1485 if (status == 0)
1486 update_changeattr(dir, &res);
1487 return status;
1488}
1489
1490static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1491{
1492 struct nfs4_exception exception = { };
1493 int err;
1494 do {
1495 err = nfs4_handle_exception(NFS_SERVER(dir),
1496 _nfs4_proc_remove(dir, name),
1497 &exception);
1498 } while (exception.retry);
1499 return err;
1500}
1501
1502struct unlink_desc {
1503 struct nfs4_remove_arg args;
1504 struct nfs4_change_info res;
1505};
1506
1507static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1508 struct qstr *name)
1509{
1510 struct unlink_desc *up;
1511
1512 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1513 if (!up)
1514 return -ENOMEM;
1515
1516 up->args.fh = NFS_FH(dir->d_inode);
1517 up->args.name = name;
1518
1519 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1520 msg->rpc_argp = &up->args;
1521 msg->rpc_resp = &up->res;
1522 return 0;
1523}
1524
1525static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1526{
1527 struct rpc_message *msg = &task->tk_msg;
1528 struct unlink_desc *up;
1529
1530 if (msg->rpc_resp != NULL) {
1531 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1532 update_changeattr(dir->d_inode, &up->res);
1533 kfree(up);
1534 msg->rpc_resp = NULL;
1535 msg->rpc_argp = NULL;
1536 }
1537 return 0;
1538}
1539
1540static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1541 struct inode *new_dir, struct qstr *new_name)
1542{
1543 struct nfs4_rename_arg arg = {
1544 .old_dir = NFS_FH(old_dir),
1545 .new_dir = NFS_FH(new_dir),
1546 .old_name = old_name,
1547 .new_name = new_name,
1548 };
1549 struct nfs4_rename_res res = { };
1550 struct rpc_message msg = {
1551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1552 .rpc_argp = &arg,
1553 .rpc_resp = &res,
1554 };
1555 int status;
1556
1557 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
1558
1559 if (!status) {
1560 update_changeattr(old_dir, &res.old_cinfo);
1561 update_changeattr(new_dir, &res.new_cinfo);
1562 }
1563 return status;
1564}
1565
1566static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1567 struct inode *new_dir, struct qstr *new_name)
1568{
1569 struct nfs4_exception exception = { };
1570 int err;
1571 do {
1572 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1573 _nfs4_proc_rename(old_dir, old_name,
1574 new_dir, new_name),
1575 &exception);
1576 } while (exception.retry);
1577 return err;
1578}
1579
1580static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1581{
1582 struct nfs4_link_arg arg = {
1583 .fh = NFS_FH(inode),
1584 .dir_fh = NFS_FH(dir),
1585 .name = name,
1586 };
1587 struct nfs4_change_info cinfo = { };
1588 struct rpc_message msg = {
1589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1590 .rpc_argp = &arg,
1591 .rpc_resp = &cinfo,
1592 };
1593 int status;
1594
1595 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1596 if (!status)
1597 update_changeattr(dir, &cinfo);
1598
1599 return status;
1600}
1601
1602static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1603{
1604 struct nfs4_exception exception = { };
1605 int err;
1606 do {
1607 err = nfs4_handle_exception(NFS_SERVER(inode),
1608 _nfs4_proc_link(inode, dir, name),
1609 &exception);
1610 } while (exception.retry);
1611 return err;
1612}
1613
1614static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1615 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1616 struct nfs_fattr *fattr)
1617{
1618 struct nfs_server *server = NFS_SERVER(dir);
1619 struct nfs4_create_arg arg = {
1620 .dir_fh = NFS_FH(dir),
1621 .server = server,
1622 .name = name,
1623 .attrs = sattr,
1624 .ftype = NF4LNK,
1625 .bitmask = server->attr_bitmask,
1626 };
1627 struct nfs4_create_res res = {
1628 .server = server,
1629 .fh = fhandle,
1630 .fattr = fattr,
1631 };
1632 struct rpc_message msg = {
1633 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1634 .rpc_argp = &arg,
1635 .rpc_resp = &res,
1636 };
1637 int status;
1638
1639 if (path->len > NFS4_MAXPATHLEN)
1640 return -ENAMETOOLONG;
1641 arg.u.symlink = path;
1642 fattr->valid = 0;
1643
1644 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1645 if (!status)
1646 update_changeattr(dir, &res.dir_cinfo);
1647 return status;
1648}
1649
1650static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1651 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1652 struct nfs_fattr *fattr)
1653{
1654 struct nfs4_exception exception = { };
1655 int err;
1656 do {
1657 err = nfs4_handle_exception(NFS_SERVER(dir),
1658 _nfs4_proc_symlink(dir, name, path, sattr,
1659 fhandle, fattr),
1660 &exception);
1661 } while (exception.retry);
1662 return err;
1663}
1664
1665static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1666 struct iattr *sattr)
1667{
1668 struct nfs_server *server = NFS_SERVER(dir);
1669 struct nfs_fh fhandle;
1670 struct nfs_fattr fattr;
1671 struct nfs4_create_arg arg = {
1672 .dir_fh = NFS_FH(dir),
1673 .server = server,
1674 .name = &dentry->d_name,
1675 .attrs = sattr,
1676 .ftype = NF4DIR,
1677 .bitmask = server->attr_bitmask,
1678 };
1679 struct nfs4_create_res res = {
1680 .server = server,
1681 .fh = &fhandle,
1682 .fattr = &fattr,
1683 };
1684 struct rpc_message msg = {
1685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1686 .rpc_argp = &arg,
1687 .rpc_resp = &res,
1688 };
1689 int status;
1690
1691 fattr.valid = 0;
1692
1693 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1694 if (!status) {
1695 update_changeattr(dir, &res.dir_cinfo);
1696 status = nfs_instantiate(dentry, &fhandle, &fattr);
1697 }
1698 return status;
1699}
1700
1701static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1702 struct iattr *sattr)
1703{
1704 struct nfs4_exception exception = { };
1705 int err;
1706 do {
1707 err = nfs4_handle_exception(NFS_SERVER(dir),
1708 _nfs4_proc_mkdir(dir, dentry, sattr),
1709 &exception);
1710 } while (exception.retry);
1711 return err;
1712}
1713
1714static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1715 u64 cookie, struct page *page, unsigned int count, int plus)
1716{
1717 struct inode *dir = dentry->d_inode;
1718 struct nfs4_readdir_arg args = {
1719 .fh = NFS_FH(dir),
1720 .pages = &page,
1721 .pgbase = 0,
1722 .count = count,
1723 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
1724 };
1725 struct nfs4_readdir_res res;
1726 struct rpc_message msg = {
1727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1728 .rpc_argp = &args,
1729 .rpc_resp = &res,
1730 .rpc_cred = cred,
1731 };
1732 int status;
1733
1734 lock_kernel();
1735 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
1736 res.pgbase = args.pgbase;
1737 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1738 if (status == 0)
1739 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1740 unlock_kernel();
1741 return status;
1742}
1743
1744static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1745 u64 cookie, struct page *page, unsigned int count, int plus)
1746{
1747 struct nfs4_exception exception = { };
1748 int err;
1749 do {
1750 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
1751 _nfs4_proc_readdir(dentry, cred, cookie,
1752 page, count, plus),
1753 &exception);
1754 } while (exception.retry);
1755 return err;
1756}
1757
1758static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1759 struct iattr *sattr, dev_t rdev)
1760{
1761 struct nfs_server *server = NFS_SERVER(dir);
1762 struct nfs_fh fh;
1763 struct nfs_fattr fattr;
1764 struct nfs4_create_arg arg = {
1765 .dir_fh = NFS_FH(dir),
1766 .server = server,
1767 .name = &dentry->d_name,
1768 .attrs = sattr,
1769 .bitmask = server->attr_bitmask,
1770 };
1771 struct nfs4_create_res res = {
1772 .server = server,
1773 .fh = &fh,
1774 .fattr = &fattr,
1775 };
1776 struct rpc_message msg = {
1777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1778 .rpc_argp = &arg,
1779 .rpc_resp = &res,
1780 };
1781 int status;
1782 int mode = sattr->ia_mode;
1783
1784 fattr.valid = 0;
1785
1786 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
1787 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
1788 if (S_ISFIFO(mode))
1789 arg.ftype = NF4FIFO;
1790 else if (S_ISBLK(mode)) {
1791 arg.ftype = NF4BLK;
1792 arg.u.device.specdata1 = MAJOR(rdev);
1793 arg.u.device.specdata2 = MINOR(rdev);
1794 }
1795 else if (S_ISCHR(mode)) {
1796 arg.ftype = NF4CHR;
1797 arg.u.device.specdata1 = MAJOR(rdev);
1798 arg.u.device.specdata2 = MINOR(rdev);
1799 }
1800 else
1801 arg.ftype = NF4SOCK;
1802
1803 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1804 if (status == 0) {
1805 update_changeattr(dir, &res.dir_cinfo);
1806 status = nfs_instantiate(dentry, &fh, &fattr);
1807 }
1808 return status;
1809}
1810
1811static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1812 struct iattr *sattr, dev_t rdev)
1813{
1814 struct nfs4_exception exception = { };
1815 int err;
1816 do {
1817 err = nfs4_handle_exception(NFS_SERVER(dir),
1818 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
1819 &exception);
1820 } while (exception.retry);
1821 return err;
1822}
1823
1824static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
1825 struct nfs_fsstat *fsstat)
1826{
1827 struct nfs4_statfs_arg args = {
1828 .fh = fhandle,
1829 .bitmask = server->attr_bitmask,
1830 };
1831 struct rpc_message msg = {
1832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
1833 .rpc_argp = &args,
1834 .rpc_resp = fsstat,
1835 };
1836
1837 fsstat->fattr->valid = 0;
1838 return rpc_call_sync(server->client, &msg, 0);
1839}
1840
1841static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
1842{
1843 struct nfs4_exception exception = { };
1844 int err;
1845 do {
1846 err = nfs4_handle_exception(server,
1847 _nfs4_proc_statfs(server, fhandle, fsstat),
1848 &exception);
1849 } while (exception.retry);
1850 return err;
1851}
1852
1853static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
1854 struct nfs_fsinfo *fsinfo)
1855{
1856 struct nfs4_fsinfo_arg args = {
1857 .fh = fhandle,
1858 .bitmask = server->attr_bitmask,
1859 };
1860 struct rpc_message msg = {
1861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
1862 .rpc_argp = &args,
1863 .rpc_resp = fsinfo,
1864 };
1865
1866 return rpc_call_sync(server->client, &msg, 0);
1867}
1868
1869static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1870{
1871 struct nfs4_exception exception = { };
1872 int err;
1873
1874 do {
1875 err = nfs4_handle_exception(server,
1876 _nfs4_do_fsinfo(server, fhandle, fsinfo),
1877 &exception);
1878 } while (exception.retry);
1879 return err;
1880}
1881
1882static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1883{
1884 fsinfo->fattr->valid = 0;
1885 return nfs4_do_fsinfo(server, fhandle, fsinfo);
1886}
1887
1888static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1889 struct nfs_pathconf *pathconf)
1890{
1891 struct nfs4_pathconf_arg args = {
1892 .fh = fhandle,
1893 .bitmask = server->attr_bitmask,
1894 };
1895 struct rpc_message msg = {
1896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
1897 .rpc_argp = &args,
1898 .rpc_resp = pathconf,
1899 };
1900
1901 /* None of the pathconf attributes are mandatory to implement */
1902 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
1903 memset(pathconf, 0, sizeof(*pathconf));
1904 return 0;
1905 }
1906
1907 pathconf->fattr->valid = 0;
1908 return rpc_call_sync(server->client, &msg, 0);
1909}
1910
1911static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1912 struct nfs_pathconf *pathconf)
1913{
1914 struct nfs4_exception exception = { };
1915 int err;
1916
1917 do {
1918 err = nfs4_handle_exception(server,
1919 _nfs4_proc_pathconf(server, fhandle, pathconf),
1920 &exception);
1921 } while (exception.retry);
1922 return err;
1923}
1924
1925static void
1926nfs4_read_done(struct rpc_task *task)
1927{
1928 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
1929 struct inode *inode = data->inode;
1930
1931 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1932 rpc_restart_call(task);
1933 return;
1934 }
1935 if (task->tk_status > 0)
1936 renew_lease(NFS_SERVER(inode), data->timestamp);
1937 /* Call back common NFS readpage processing */
1938 nfs_readpage_result(task);
1939}
1940
1941static void
1942nfs4_proc_read_setup(struct nfs_read_data *data)
1943{
1944 struct rpc_task *task = &data->task;
1945 struct rpc_message msg = {
1946 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1947 .rpc_argp = &data->args,
1948 .rpc_resp = &data->res,
1949 .rpc_cred = data->cred,
1950 };
1951 struct inode *inode = data->inode;
1952 int flags;
1953
1954 data->timestamp = jiffies;
1955
1956 /* N.B. Do we need to test? Never called for swapfile inode */
1957 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
1958
1959 /* Finalize the task. */
1960 rpc_init_task(task, NFS_CLIENT(inode), nfs4_read_done, flags);
1961 rpc_call_setup(task, &msg, 0);
1962}
1963
1964static void
1965nfs4_write_done(struct rpc_task *task)
1966{
1967 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
1968 struct inode *inode = data->inode;
1969
1970 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1971 rpc_restart_call(task);
1972 return;
1973 }
1974 if (task->tk_status >= 0)
1975 renew_lease(NFS_SERVER(inode), data->timestamp);
1976 /* Call back common NFS writeback processing */
1977 nfs_writeback_done(task);
1978}
1979
1980static void
1981nfs4_proc_write_setup(struct nfs_write_data *data, int how)
1982{
1983 struct rpc_task *task = &data->task;
1984 struct rpc_message msg = {
1985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1986 .rpc_argp = &data->args,
1987 .rpc_resp = &data->res,
1988 .rpc_cred = data->cred,
1989 };
1990 struct inode *inode = data->inode;
1991 int stable;
1992 int flags;
1993
1994 if (how & FLUSH_STABLE) {
1995 if (!NFS_I(inode)->ncommit)
1996 stable = NFS_FILE_SYNC;
1997 else
1998 stable = NFS_DATA_SYNC;
1999 } else
2000 stable = NFS_UNSTABLE;
2001 data->args.stable = stable;
2002
2003 data->timestamp = jiffies;
2004
2005 /* Set the initial flags for the task. */
2006 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2007
2008 /* Finalize the task. */
2009 rpc_init_task(task, NFS_CLIENT(inode), nfs4_write_done, flags);
2010 rpc_call_setup(task, &msg, 0);
2011}
2012
2013static void
2014nfs4_commit_done(struct rpc_task *task)
2015{
2016 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2017 struct inode *inode = data->inode;
2018
2019 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2020 rpc_restart_call(task);
2021 return;
2022 }
2023 /* Call back common NFS writeback processing */
2024 nfs_commit_done(task);
2025}
2026
2027static void
2028nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2029{
2030 struct rpc_task *task = &data->task;
2031 struct rpc_message msg = {
2032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2033 .rpc_argp = &data->args,
2034 .rpc_resp = &data->res,
2035 .rpc_cred = data->cred,
2036 };
2037 struct inode *inode = data->inode;
2038 int flags;
2039
2040 /* Set the initial flags for the task. */
2041 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2042
2043 /* Finalize the task. */
2044 rpc_init_task(task, NFS_CLIENT(inode), nfs4_commit_done, flags);
2045 rpc_call_setup(task, &msg, 0);
2046}
2047
2048/*
2049 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2050 * standalone procedure for queueing an asynchronous RENEW.
2051 */
2052static void
2053renew_done(struct rpc_task *task)
2054{
2055 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
2056 unsigned long timestamp = (unsigned long)task->tk_calldata;
2057
2058 if (task->tk_status < 0) {
2059 switch (task->tk_status) {
2060 case -NFS4ERR_STALE_CLIENTID:
2061 case -NFS4ERR_EXPIRED:
2062 case -NFS4ERR_CB_PATH_DOWN:
2063 nfs4_schedule_state_recovery(clp);
2064 }
2065 return;
2066 }
2067 spin_lock(&clp->cl_lock);
2068 if (time_before(clp->cl_last_renewal,timestamp))
2069 clp->cl_last_renewal = timestamp;
2070 spin_unlock(&clp->cl_lock);
2071}
2072
2073int
2074nfs4_proc_async_renew(struct nfs4_client *clp)
2075{
2076 struct rpc_message msg = {
2077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2078 .rpc_argp = clp,
2079 .rpc_cred = clp->cl_cred,
2080 };
2081
2082 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2083 renew_done, (void *)jiffies);
2084}
2085
2086int
2087nfs4_proc_renew(struct nfs4_client *clp)
2088{
2089 struct rpc_message msg = {
2090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2091 .rpc_argp = clp,
2092 .rpc_cred = clp->cl_cred,
2093 };
2094 unsigned long now = jiffies;
2095 int status;
2096
2097 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2098 if (status < 0)
2099 return status;
2100 spin_lock(&clp->cl_lock);
2101 if (time_before(clp->cl_last_renewal,now))
2102 clp->cl_last_renewal = now;
2103 spin_unlock(&clp->cl_lock);
2104 return 0;
2105}
2106
2107/*
2108 * We will need to arrange for the VFS layer to provide an atomic open.
2109 * Until then, this open method is prone to inefficiency and race conditions
2110 * due to the lookup, potential create, and open VFS calls from sys_open()
2111 * placed on the wire.
2112 */
2113static int
2114nfs4_proc_file_open(struct inode *inode, struct file *filp)
2115{
2116 struct dentry *dentry = filp->f_dentry;
2117 struct nfs_open_context *ctx;
2118 struct nfs4_state *state = NULL;
2119 struct rpc_cred *cred;
2120 int status = -ENOMEM;
2121
2122 dprintk("nfs4_proc_file_open: starting on (%.*s/%.*s)\n",
2123 (int)dentry->d_parent->d_name.len,
2124 dentry->d_parent->d_name.name,
2125 (int)dentry->d_name.len, dentry->d_name.name);
2126
2127
2128 /* Find our open stateid */
2129 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
2130 if (IS_ERR(cred))
2131 return PTR_ERR(cred);
2132 ctx = alloc_nfs_open_context(dentry, cred);
2133 put_rpccred(cred);
2134 if (unlikely(ctx == NULL))
2135 return -ENOMEM;
2136 status = -EIO; /* ERACE actually */
2137 state = nfs4_find_state(inode, cred, filp->f_mode);
2138 if (unlikely(state == NULL))
2139 goto no_state;
2140 ctx->state = state;
2141 nfs4_close_state(state, filp->f_mode);
2142 ctx->mode = filp->f_mode;
2143 nfs_file_set_open_context(filp, ctx);
2144 put_nfs_open_context(ctx);
2145 if (filp->f_mode & FMODE_WRITE)
2146 nfs_begin_data_update(inode);
2147 return 0;
2148no_state:
2149 printk(KERN_WARNING "NFS: v4 raced in function %s\n", __FUNCTION__);
2150 put_nfs_open_context(ctx);
2151 return status;
2152}
2153
2154/*
2155 * Release our state
2156 */
2157static int
2158nfs4_proc_file_release(struct inode *inode, struct file *filp)
2159{
2160 if (filp->f_mode & FMODE_WRITE)
2161 nfs_end_data_update(inode);
2162 nfs_file_clear_open_context(filp);
2163 return 0;
2164}
2165
2166static int
2167nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server)
2168{
2169 struct nfs4_client *clp = server->nfs4_state;
2170
2171 if (!clp || task->tk_status >= 0)
2172 return 0;
2173 switch(task->tk_status) {
2174 case -NFS4ERR_STALE_CLIENTID:
2175 case -NFS4ERR_STALE_STATEID:
2176 case -NFS4ERR_EXPIRED:
2177 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2178 nfs4_schedule_state_recovery(clp);
2179 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2180 rpc_wake_up_task(task);
2181 task->tk_status = 0;
2182 return -EAGAIN;
2183 case -NFS4ERR_GRACE:
2184 case -NFS4ERR_DELAY:
2185 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2186 task->tk_status = 0;
2187 return -EAGAIN;
2188 case -NFS4ERR_OLD_STATEID:
2189 task->tk_status = 0;
2190 return -EAGAIN;
2191 }
2192 task->tk_status = nfs4_map_errors(task->tk_status);
2193 return 0;
2194}
2195
2196static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2197{
2198 DEFINE_WAIT(wait);
2199 sigset_t oldset;
2200 int interruptible, res = 0;
2201
2202 might_sleep();
2203
2204 rpc_clnt_sigmask(clnt, &oldset);
2205 interruptible = TASK_UNINTERRUPTIBLE;
2206 if (clnt->cl_intr)
2207 interruptible = TASK_INTERRUPTIBLE;
2208 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2209 nfs4_schedule_state_recovery(clp);
2210 if (clnt->cl_intr && signalled())
2211 res = -ERESTARTSYS;
2212 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2213 schedule();
2214 finish_wait(&clp->cl_waitq, &wait);
2215 rpc_clnt_sigunmask(clnt, &oldset);
2216 return res;
2217}
2218
2219static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2220{
2221 sigset_t oldset;
2222 int res = 0;
2223
2224 might_sleep();
2225
2226 if (*timeout <= 0)
2227 *timeout = NFS4_POLL_RETRY_MIN;
2228 if (*timeout > NFS4_POLL_RETRY_MAX)
2229 *timeout = NFS4_POLL_RETRY_MAX;
2230 rpc_clnt_sigmask(clnt, &oldset);
2231 if (clnt->cl_intr) {
2232 set_current_state(TASK_INTERRUPTIBLE);
2233 schedule_timeout(*timeout);
2234 if (signalled())
2235 res = -ERESTARTSYS;
2236 } else {
2237 set_current_state(TASK_UNINTERRUPTIBLE);
2238 schedule_timeout(*timeout);
2239 }
2240 rpc_clnt_sigunmask(clnt, &oldset);
2241 *timeout <<= 1;
2242 return res;
2243}
2244
2245/* This is the error handling routine for processes that are allowed
2246 * to sleep.
2247 */
2248int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2249{
2250 struct nfs4_client *clp = server->nfs4_state;
2251 int ret = errorcode;
2252
2253 exception->retry = 0;
2254 switch(errorcode) {
2255 case 0:
2256 return 0;
2257 case -NFS4ERR_STALE_CLIENTID:
2258 case -NFS4ERR_STALE_STATEID:
2259 case -NFS4ERR_EXPIRED:
2260 ret = nfs4_wait_clnt_recover(server->client, clp);
2261 if (ret == 0)
2262 exception->retry = 1;
2263 break;
2264 case -NFS4ERR_GRACE:
2265 case -NFS4ERR_DELAY:
2266 ret = nfs4_delay(server->client, &exception->timeout);
2267 if (ret == 0)
2268 exception->retry = 1;
2269 break;
2270 case -NFS4ERR_OLD_STATEID:
2271 if (ret == 0)
2272 exception->retry = 1;
2273 }
2274 /* We failed to handle the error */
2275 return nfs4_map_errors(ret);
2276}
2277
2278int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2279{
2280 nfs4_verifier sc_verifier;
2281 struct nfs4_setclientid setclientid = {
2282 .sc_verifier = &sc_verifier,
2283 .sc_prog = program,
2284 };
2285 struct rpc_message msg = {
2286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2287 .rpc_argp = &setclientid,
2288 .rpc_resp = clp,
2289 .rpc_cred = clp->cl_cred,
2290 };
2291 u32 *p;
2292 int loop = 0;
2293 int status;
2294
2295 p = (u32*)sc_verifier.data;
2296 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2297 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2298
2299 for(;;) {
2300 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2301 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2302 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2303 clp->cl_cred->cr_ops->cr_name,
2304 clp->cl_id_uniquifier);
2305 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2306 sizeof(setclientid.sc_netid), "tcp");
2307 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2308 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2309 clp->cl_ipaddr, port >> 8, port & 255);
2310
2311 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2312 if (status != -NFS4ERR_CLID_INUSE)
2313 break;
2314 if (signalled())
2315 break;
2316 if (loop++ & 1)
2317 ssleep(clp->cl_lease_time + 1);
2318 else
2319 if (++clp->cl_id_uniquifier == 0)
2320 break;
2321 }
2322 return status;
2323}
2324
2325int
2326nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2327{
2328 struct nfs_fsinfo fsinfo;
2329 struct rpc_message msg = {
2330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2331 .rpc_argp = clp,
2332 .rpc_resp = &fsinfo,
2333 .rpc_cred = clp->cl_cred,
2334 };
2335 unsigned long now;
2336 int status;
2337
2338 now = jiffies;
2339 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2340 if (status == 0) {
2341 spin_lock(&clp->cl_lock);
2342 clp->cl_lease_time = fsinfo.lease_time * HZ;
2343 clp->cl_last_renewal = now;
2344 spin_unlock(&clp->cl_lock);
2345 }
2346 return status;
2347}
2348
2349static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2350{
2351 struct nfs4_delegreturnargs args = {
2352 .fhandle = NFS_FH(inode),
2353 .stateid = stateid,
2354 };
2355 struct rpc_message msg = {
2356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2357 .rpc_argp = &args,
2358 .rpc_cred = cred,
2359 };
2360
2361 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2362}
2363
2364int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2365{
2366 struct nfs_server *server = NFS_SERVER(inode);
2367 struct nfs4_exception exception = { };
2368 int err;
2369 do {
2370 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2371 switch (err) {
2372 case -NFS4ERR_STALE_STATEID:
2373 case -NFS4ERR_EXPIRED:
2374 nfs4_schedule_state_recovery(server->nfs4_state);
2375 case 0:
2376 return 0;
2377 }
2378 err = nfs4_handle_exception(server, err, &exception);
2379 } while (exception.retry);
2380 return err;
2381}
2382
2383#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2384#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2385
2386/*
2387 * sleep, with exponential backoff, and retry the LOCK operation.
2388 */
2389static unsigned long
2390nfs4_set_lock_task_retry(unsigned long timeout)
2391{
2392 current->state = TASK_INTERRUPTIBLE;
2393 schedule_timeout(timeout);
2394 timeout <<= 1;
2395 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2396 return NFS4_LOCK_MAXTIMEOUT;
2397 return timeout;
2398}
2399
2400static inline int
2401nfs4_lck_type(int cmd, struct file_lock *request)
2402{
2403 /* set lock type */
2404 switch (request->fl_type) {
2405 case F_RDLCK:
2406 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2407 case F_WRLCK:
2408 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2409 case F_UNLCK:
2410 return NFS4_WRITE_LT;
2411 }
2412 BUG();
2413 return 0;
2414}
2415
2416static inline uint64_t
2417nfs4_lck_length(struct file_lock *request)
2418{
2419 if (request->fl_end == OFFSET_MAX)
2420 return ~(uint64_t)0;
2421 return request->fl_end - request->fl_start + 1;
2422}
2423
2424static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2425{
2426 struct inode *inode = state->inode;
2427 struct nfs_server *server = NFS_SERVER(inode);
2428 struct nfs4_client *clp = server->nfs4_state;
2429 struct nfs_lockargs arg = {
2430 .fh = NFS_FH(inode),
2431 .type = nfs4_lck_type(cmd, request),
2432 .offset = request->fl_start,
2433 .length = nfs4_lck_length(request),
2434 };
2435 struct nfs_lockres res = {
2436 .server = server,
2437 };
2438 struct rpc_message msg = {
2439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2440 .rpc_argp = &arg,
2441 .rpc_resp = &res,
2442 .rpc_cred = state->owner->so_cred,
2443 };
2444 struct nfs_lowner nlo;
2445 struct nfs4_lock_state *lsp;
2446 int status;
2447
2448 down_read(&clp->cl_sem);
2449 nlo.clientid = clp->cl_clientid;
2450 down(&state->lock_sema);
2451 lsp = nfs4_find_lock_state(state, request->fl_owner);
2452 if (lsp)
2453 nlo.id = lsp->ls_id;
2454 else {
2455 spin_lock(&clp->cl_lock);
2456 nlo.id = nfs4_alloc_lockowner_id(clp);
2457 spin_unlock(&clp->cl_lock);
2458 }
2459 arg.u.lockt = &nlo;
2460 status = rpc_call_sync(server->client, &msg, 0);
2461 if (!status) {
2462 request->fl_type = F_UNLCK;
2463 } else if (status == -NFS4ERR_DENIED) {
2464 int64_t len, start, end;
2465 start = res.u.denied.offset;
2466 len = res.u.denied.length;
2467 end = start + len - 1;
2468 if (end < 0 || len == 0)
2469 request->fl_end = OFFSET_MAX;
2470 else
2471 request->fl_end = (loff_t)end;
2472 request->fl_start = (loff_t)start;
2473 request->fl_type = F_WRLCK;
2474 if (res.u.denied.type & 1)
2475 request->fl_type = F_RDLCK;
2476 request->fl_pid = 0;
2477 status = 0;
2478 }
2479 if (lsp)
2480 nfs4_put_lock_state(lsp);
2481 up(&state->lock_sema);
2482 up_read(&clp->cl_sem);
2483 return status;
2484}
2485
2486static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2487{
2488 struct nfs4_exception exception = { };
2489 int err;
2490
2491 do {
2492 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2493 _nfs4_proc_getlk(state, cmd, request),
2494 &exception);
2495 } while (exception.retry);
2496 return err;
2497}
2498
2499static int do_vfs_lock(struct file *file, struct file_lock *fl)
2500{
2501 int res = 0;
2502 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2503 case FL_POSIX:
2504 res = posix_lock_file_wait(file, fl);
2505 break;
2506 case FL_FLOCK:
2507 res = flock_lock_file_wait(file, fl);
2508 break;
2509 default:
2510 BUG();
2511 }
2512 if (res < 0)
2513 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
2514 __FUNCTION__);
2515 return res;
2516}
2517
2518static int _nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2519{
2520 struct inode *inode = state->inode;
2521 struct nfs_server *server = NFS_SERVER(inode);
2522 struct nfs4_client *clp = server->nfs4_state;
2523 struct nfs_lockargs arg = {
2524 .fh = NFS_FH(inode),
2525 .type = nfs4_lck_type(cmd, request),
2526 .offset = request->fl_start,
2527 .length = nfs4_lck_length(request),
2528 };
2529 struct nfs_lockres res = {
2530 .server = server,
2531 };
2532 struct rpc_message msg = {
2533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
2534 .rpc_argp = &arg,
2535 .rpc_resp = &res,
2536 .rpc_cred = state->owner->so_cred,
2537 };
2538 struct nfs4_lock_state *lsp;
2539 struct nfs_locku_opargs luargs;
2540 int status = 0;
2541
2542 down_read(&clp->cl_sem);
2543 down(&state->lock_sema);
2544 lsp = nfs4_find_lock_state(state, request->fl_owner);
2545 if (!lsp)
2546 goto out;
2547 /* We might have lost the locks! */
2548 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0) {
2549 luargs.seqid = lsp->ls_seqid;
2550 memcpy(&luargs.stateid, &lsp->ls_stateid, sizeof(luargs.stateid));
2551 arg.u.locku = &luargs;
2552 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2553 nfs4_increment_lock_seqid(status, lsp);
2554 }
2555
2556 if (status == 0) {
2557 memcpy(&lsp->ls_stateid, &res.u.stateid,
2558 sizeof(lsp->ls_stateid));
2559 nfs4_notify_unlck(state, request, lsp);
2560 }
2561 nfs4_put_lock_state(lsp);
2562out:
2563 up(&state->lock_sema);
2564 if (status == 0)
2565 do_vfs_lock(request->fl_file, request);
2566 up_read(&clp->cl_sem);
2567 return status;
2568}
2569
2570static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2571{
2572 struct nfs4_exception exception = { };
2573 int err;
2574
2575 do {
2576 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2577 _nfs4_proc_unlck(state, cmd, request),
2578 &exception);
2579 } while (exception.retry);
2580 return err;
2581}
2582
2583static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
2584{
2585 struct inode *inode = state->inode;
2586 struct nfs_server *server = NFS_SERVER(inode);
2587 struct nfs4_lock_state *lsp;
2588 struct nfs_lockargs arg = {
2589 .fh = NFS_FH(inode),
2590 .type = nfs4_lck_type(cmd, request),
2591 .offset = request->fl_start,
2592 .length = nfs4_lck_length(request),
2593 };
2594 struct nfs_lockres res = {
2595 .server = server,
2596 };
2597 struct rpc_message msg = {
2598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
2599 .rpc_argp = &arg,
2600 .rpc_resp = &res,
2601 .rpc_cred = state->owner->so_cred,
2602 };
2603 struct nfs_lock_opargs largs = {
2604 .reclaim = reclaim,
2605 .new_lock_owner = 0,
2606 };
2607 int status;
2608
2609 lsp = nfs4_get_lock_state(state, request->fl_owner);
2610 if (lsp == NULL)
2611 return -ENOMEM;
2612 if (!(lsp->ls_flags & NFS_LOCK_INITIALIZED)) {
2613 struct nfs4_state_owner *owner = state->owner;
2614 struct nfs_open_to_lock otl = {
2615 .lock_owner = {
2616 .clientid = server->nfs4_state->cl_clientid,
2617 },
2618 };
2619
2620 otl.lock_seqid = lsp->ls_seqid;
2621 otl.lock_owner.id = lsp->ls_id;
2622 memcpy(&otl.open_stateid, &state->stateid, sizeof(otl.open_stateid));
2623 largs.u.open_lock = &otl;
2624 largs.new_lock_owner = 1;
2625 arg.u.lock = &largs;
2626 down(&owner->so_sema);
2627 otl.open_seqid = owner->so_seqid;
2628 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2629 /* increment open_owner seqid on success, and
2630 * seqid mutating errors */
2631 nfs4_increment_seqid(status, owner);
2632 up(&owner->so_sema);
2633 } else {
2634 struct nfs_exist_lock el = {
2635 .seqid = lsp->ls_seqid,
2636 };
2637 memcpy(&el.stateid, &lsp->ls_stateid, sizeof(el.stateid));
2638 largs.u.exist_lock = &el;
2639 largs.new_lock_owner = 0;
2640 arg.u.lock = &largs;
2641 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2642 }
2643 /* increment seqid on success, and * seqid mutating errors*/
2644 nfs4_increment_lock_seqid(status, lsp);
2645 /* save the returned stateid. */
2646 if (status == 0) {
2647 memcpy(&lsp->ls_stateid, &res.u.stateid, sizeof(nfs4_stateid));
2648 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
2649 if (!reclaim)
2650 nfs4_notify_setlk(state, request, lsp);
2651 } else if (status == -NFS4ERR_DENIED)
2652 status = -EAGAIN;
2653 nfs4_put_lock_state(lsp);
2654 return status;
2655}
2656
2657static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
2658{
2659 return _nfs4_do_setlk(state, F_SETLK, request, 1);
2660}
2661
2662static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
2663{
2664 return _nfs4_do_setlk(state, F_SETLK, request, 0);
2665}
2666
2667static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2668{
2669 struct nfs4_client *clp = state->owner->so_client;
2670 int status;
2671
2672 down_read(&clp->cl_sem);
2673 down(&state->lock_sema);
2674 status = _nfs4_do_setlk(state, cmd, request, 0);
2675 up(&state->lock_sema);
2676 if (status == 0) {
2677 /* Note: we always want to sleep here! */
2678 request->fl_flags |= FL_SLEEP;
2679 if (do_vfs_lock(request->fl_file, request) < 0)
2680 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
2681 }
2682 up_read(&clp->cl_sem);
2683 return status;
2684}
2685
2686static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2687{
2688 struct nfs4_exception exception = { };
2689 int err;
2690
2691 do {
2692 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2693 _nfs4_proc_setlk(state, cmd, request),
2694 &exception);
2695 } while (exception.retry);
2696 return err;
2697}
2698
2699static int
2700nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
2701{
2702 struct nfs_open_context *ctx;
2703 struct nfs4_state *state;
2704 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
2705 int status;
2706
2707 /* verify open state */
2708 ctx = (struct nfs_open_context *)filp->private_data;
2709 state = ctx->state;
2710
2711 if (request->fl_start < 0 || request->fl_end < 0)
2712 return -EINVAL;
2713
2714 if (IS_GETLK(cmd))
2715 return nfs4_proc_getlk(state, F_GETLK, request);
2716
2717 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
2718 return -EINVAL;
2719
2720 if (request->fl_type == F_UNLCK)
2721 return nfs4_proc_unlck(state, cmd, request);
2722
2723 do {
2724 status = nfs4_proc_setlk(state, cmd, request);
2725 if ((status != -EAGAIN) || IS_SETLK(cmd))
2726 break;
2727 timeout = nfs4_set_lock_task_retry(timeout);
2728 status = -ERESTARTSYS;
2729 if (signalled())
2730 break;
2731 } while(status < 0);
2732
2733 return status;
2734}
2735
2736struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
2737 .recover_open = nfs4_open_reclaim,
2738 .recover_lock = nfs4_lock_reclaim,
2739};
2740
2741struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
2742 .recover_open = nfs4_open_expired,
2743 .recover_lock = nfs4_lock_expired,
2744};
2745
2746struct nfs_rpc_ops nfs_v4_clientops = {
2747 .version = 4, /* protocol version */
2748 .dentry_ops = &nfs4_dentry_operations,
2749 .dir_inode_ops = &nfs4_dir_inode_operations,
2750 .getroot = nfs4_proc_get_root,
2751 .getattr = nfs4_proc_getattr,
2752 .setattr = nfs4_proc_setattr,
2753 .lookup = nfs4_proc_lookup,
2754 .access = nfs4_proc_access,
2755 .readlink = nfs4_proc_readlink,
2756 .read = nfs4_proc_read,
2757 .write = nfs4_proc_write,
2758 .commit = nfs4_proc_commit,
2759 .create = nfs4_proc_create,
2760 .remove = nfs4_proc_remove,
2761 .unlink_setup = nfs4_proc_unlink_setup,
2762 .unlink_done = nfs4_proc_unlink_done,
2763 .rename = nfs4_proc_rename,
2764 .link = nfs4_proc_link,
2765 .symlink = nfs4_proc_symlink,
2766 .mkdir = nfs4_proc_mkdir,
2767 .rmdir = nfs4_proc_remove,
2768 .readdir = nfs4_proc_readdir,
2769 .mknod = nfs4_proc_mknod,
2770 .statfs = nfs4_proc_statfs,
2771 .fsinfo = nfs4_proc_fsinfo,
2772 .pathconf = nfs4_proc_pathconf,
2773 .decode_dirent = nfs4_decode_dirent,
2774 .read_setup = nfs4_proc_read_setup,
2775 .write_setup = nfs4_proc_write_setup,
2776 .commit_setup = nfs4_proc_commit_setup,
2777 .file_open = nfs4_proc_file_open,
2778 .file_release = nfs4_proc_file_release,
2779 .lock = nfs4_proc_lock,
2780};
2781
2782/*
2783 * Local variables:
2784 * c-basic-offset: 8
2785 * End:
2786 */