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