[PATCH] NLM: fix a client-side race on blocking locks.
[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
08e9eac4 759 if (state != NULL) {
1da177e4 760 msg.rpc_cred = state->owner->so_cred;
08e9eac4
TM
761 nfs4_copy_stateid(&arg.stateid, state, current->files);
762 } else
1da177e4
LT
763 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
764
765 return rpc_call_sync(server->client, &msg, 0);
766}
767
768static int nfs4_do_setattr(struct nfs_server *server, struct nfs_fattr *fattr,
769 struct nfs_fh *fhandle, struct iattr *sattr,
770 struct nfs4_state *state)
771{
772 struct nfs4_exception exception = { };
773 int err;
774 do {
775 err = nfs4_handle_exception(server,
776 _nfs4_do_setattr(server, fattr, fhandle, sattr,
777 state),
778 &exception);
779 } while (exception.retry);
780 return err;
781}
782
783struct nfs4_closedata {
784 struct inode *inode;
785 struct nfs4_state *state;
786 struct nfs_closeargs arg;
787 struct nfs_closeres res;
788};
789
790static void nfs4_close_done(struct rpc_task *task)
791{
792 struct nfs4_closedata *calldata = (struct nfs4_closedata *)task->tk_calldata;
793 struct nfs4_state *state = calldata->state;
794 struct nfs4_state_owner *sp = state->owner;
795 struct nfs_server *server = NFS_SERVER(calldata->inode);
796
797 /* hmm. we are done with the inode, and in the process of freeing
798 * the state_owner. we keep this around to process errors
799 */
800 nfs4_increment_seqid(task->tk_status, sp);
801 switch (task->tk_status) {
802 case 0:
803 memcpy(&state->stateid, &calldata->res.stateid,
804 sizeof(state->stateid));
805 break;
806 case -NFS4ERR_STALE_STATEID:
807 case -NFS4ERR_EXPIRED:
808 state->state = calldata->arg.open_flags;
809 nfs4_schedule_state_recovery(server->nfs4_state);
810 break;
811 default:
812 if (nfs4_async_handle_error(task, server) == -EAGAIN) {
813 rpc_restart_call(task);
814 return;
815 }
816 }
817 state->state = calldata->arg.open_flags;
818 nfs4_put_open_state(state);
819 up(&sp->so_sema);
820 nfs4_put_state_owner(sp);
821 up_read(&server->nfs4_state->cl_sem);
822 kfree(calldata);
823}
824
825static inline int nfs4_close_call(struct rpc_clnt *clnt, struct nfs4_closedata *calldata)
826{
827 struct rpc_message msg = {
828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
829 .rpc_argp = &calldata->arg,
830 .rpc_resp = &calldata->res,
831 .rpc_cred = calldata->state->owner->so_cred,
832 };
833 if (calldata->arg.open_flags != 0)
834 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
835 return rpc_call_async(clnt, &msg, 0, nfs4_close_done, calldata);
836}
837
838/*
839 * It is possible for data to be read/written from a mem-mapped file
840 * after the sys_close call (which hits the vfs layer as a flush).
841 * This means that we can't safely call nfsv4 close on a file until
842 * the inode is cleared. This in turn means that we are not good
843 * NFSv4 citizens - we do not indicate to the server to update the file's
844 * share state even when we are done with one of the three share
845 * stateid's in the inode.
846 *
847 * NOTE: Caller must be holding the sp->so_owner semaphore!
848 */
849int nfs4_do_close(struct inode *inode, struct nfs4_state *state, mode_t mode)
850{
851 struct nfs4_closedata *calldata;
852 int status;
853
854 /* Tell caller we're done */
855 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
856 state->state = mode;
857 return 0;
858 }
859 calldata = (struct nfs4_closedata *)kmalloc(sizeof(*calldata), GFP_KERNEL);
860 if (calldata == NULL)
861 return -ENOMEM;
862 calldata->inode = inode;
863 calldata->state = state;
864 calldata->arg.fh = NFS_FH(inode);
865 /* Serialization for the sequence id */
866 calldata->arg.seqid = state->owner->so_seqid;
867 calldata->arg.open_flags = mode;
868 memcpy(&calldata->arg.stateid, &state->stateid,
869 sizeof(calldata->arg.stateid));
870 status = nfs4_close_call(NFS_SERVER(inode)->client, calldata);
871 /*
872 * Return -EINPROGRESS on success in order to indicate to the
873 * caller that an asynchronous RPC call has been launched, and
874 * that it will release the semaphores on completion.
875 */
876 return (status == 0) ? -EINPROGRESS : status;
877}
878
879struct inode *
880nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
881{
882 struct iattr attr;
883 struct rpc_cred *cred;
884 struct nfs4_state *state;
885
886 if (nd->flags & LOOKUP_CREATE) {
887 attr.ia_mode = nd->intent.open.create_mode;
888 attr.ia_valid = ATTR_MODE;
889 if (!IS_POSIXACL(dir))
890 attr.ia_mode &= ~current->fs->umask;
891 } else {
892 attr.ia_valid = 0;
893 BUG_ON(nd->intent.open.flags & O_CREAT);
894 }
895
896 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
897 if (IS_ERR(cred))
898 return (struct inode *)cred;
899 state = nfs4_do_open(dir, dentry, nd->intent.open.flags, &attr, cred);
900 put_rpccred(cred);
901 if (IS_ERR(state))
902 return (struct inode *)state;
903 return state->inode;
904}
905
906int
907nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags)
908{
909 struct rpc_cred *cred;
910 struct nfs4_state *state;
911 struct inode *inode;
912
913 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
914 if (IS_ERR(cred))
915 return PTR_ERR(cred);
916 state = nfs4_open_delegated(dentry->d_inode, openflags, cred);
917 if (IS_ERR(state))
918 state = nfs4_do_open(dir, dentry, openflags, NULL, cred);
919 put_rpccred(cred);
920 if (state == ERR_PTR(-ENOENT) && dentry->d_inode == 0)
921 return 1;
922 if (IS_ERR(state))
923 return 0;
924 inode = state->inode;
925 if (inode == dentry->d_inode) {
926 iput(inode);
927 return 1;
928 }
929 d_drop(dentry);
930 nfs4_close_state(state, openflags);
931 iput(inode);
932 return 0;
933}
934
935
936static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
937{
938 struct nfs4_server_caps_res res = {};
939 struct rpc_message msg = {
940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
941 .rpc_argp = fhandle,
942 .rpc_resp = &res,
943 };
944 int status;
945
946 status = rpc_call_sync(server->client, &msg, 0);
947 if (status == 0) {
948 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
949 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
950 server->caps |= NFS_CAP_ACLS;
951 if (res.has_links != 0)
952 server->caps |= NFS_CAP_HARDLINKS;
953 if (res.has_symlinks != 0)
954 server->caps |= NFS_CAP_SYMLINKS;
955 server->acl_bitmask = res.acl_bitmask;
956 }
957 return status;
958}
959
960static int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
961{
962 struct nfs4_exception exception = { };
963 int err;
964 do {
965 err = nfs4_handle_exception(server,
966 _nfs4_server_capabilities(server, fhandle),
967 &exception);
968 } while (exception.retry);
969 return err;
970}
971
972static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
973 struct nfs_fsinfo *info)
974{
975 struct nfs_fattr * fattr = info->fattr;
976 struct nfs4_lookup_root_arg args = {
977 .bitmask = nfs4_fattr_bitmap,
978 };
979 struct nfs4_lookup_res res = {
980 .server = server,
981 .fattr = fattr,
982 .fh = fhandle,
983 };
984 struct rpc_message msg = {
985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
986 .rpc_argp = &args,
987 .rpc_resp = &res,
988 };
989 fattr->valid = 0;
990 return rpc_call_sync(server->client, &msg, 0);
991}
992
993static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
994 struct nfs_fsinfo *info)
995{
996 struct nfs4_exception exception = { };
997 int err;
998 do {
999 err = nfs4_handle_exception(server,
1000 _nfs4_lookup_root(server, fhandle, info),
1001 &exception);
1002 } while (exception.retry);
1003 return err;
1004}
1005
1006static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
1007 struct nfs_fsinfo *info)
1008{
1009 struct nfs_fattr * fattr = info->fattr;
1010 unsigned char * p;
1011 struct qstr q;
1012 struct nfs4_lookup_arg args = {
1013 .dir_fh = fhandle,
1014 .name = &q,
1015 .bitmask = nfs4_fattr_bitmap,
1016 };
1017 struct nfs4_lookup_res res = {
1018 .server = server,
1019 .fattr = fattr,
1020 .fh = fhandle,
1021 };
1022 struct rpc_message msg = {
1023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1024 .rpc_argp = &args,
1025 .rpc_resp = &res,
1026 };
1027 int status;
1028
1029 /*
1030 * Now we do a separate LOOKUP for each component of the mount path.
1031 * The LOOKUPs are done separately so that we can conveniently
1032 * catch an ERR_WRONGSEC if it occurs along the way...
1033 */
1034 status = nfs4_lookup_root(server, fhandle, info);
1035 if (status)
1036 goto out;
1037
1038 p = server->mnt_path;
1039 for (;;) {
1040 struct nfs4_exception exception = { };
1041
1042 while (*p == '/')
1043 p++;
1044 if (!*p)
1045 break;
1046 q.name = p;
1047 while (*p && (*p != '/'))
1048 p++;
1049 q.len = p - q.name;
1050
1051 do {
1052 fattr->valid = 0;
1053 status = nfs4_handle_exception(server,
1054 rpc_call_sync(server->client, &msg, 0),
1055 &exception);
1056 } while (exception.retry);
1057 if (status == 0)
1058 continue;
1059 if (status == -ENOENT) {
1060 printk(KERN_NOTICE "NFS: mount path %s does not exist!\n", server->mnt_path);
1061 printk(KERN_NOTICE "NFS: suggestion: try mounting '/' instead.\n");
1062 }
1063 break;
1064 }
1065 if (status == 0)
1066 status = nfs4_server_capabilities(server, fhandle);
1067 if (status == 0)
1068 status = nfs4_do_fsinfo(server, fhandle, info);
1069out:
1070 return status;
1071}
1072
1073static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1074{
1075 struct nfs4_getattr_arg args = {
1076 .fh = fhandle,
1077 .bitmask = server->attr_bitmask,
1078 };
1079 struct nfs4_getattr_res res = {
1080 .fattr = fattr,
1081 .server = server,
1082 };
1083 struct rpc_message msg = {
1084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
1085 .rpc_argp = &args,
1086 .rpc_resp = &res,
1087 };
1088
1089 fattr->valid = 0;
1090 return rpc_call_sync(server->client, &msg, 0);
1091}
1092
1093static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1094{
1095 struct nfs4_exception exception = { };
1096 int err;
1097 do {
1098 err = nfs4_handle_exception(server,
1099 _nfs4_proc_getattr(server, fhandle, fattr),
1100 &exception);
1101 } while (exception.retry);
1102 return err;
1103}
1104
1105/*
1106 * The file is not closed if it is opened due to the a request to change
1107 * the size of the file. The open call will not be needed once the
1108 * VFS layer lookup-intents are implemented.
1109 *
1110 * Close is called when the inode is destroyed.
1111 * If we haven't opened the file for O_WRONLY, we
1112 * need to in the size_change case to obtain a stateid.
1113 *
1114 * Got race?
1115 * Because OPEN is always done by name in nfsv4, it is
1116 * possible that we opened a different file by the same
1117 * name. We can recognize this race condition, but we
1118 * can't do anything about it besides returning an error.
1119 *
1120 * This will be fixed with VFS changes (lookup-intent).
1121 */
1122static int
1123nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
1124 struct iattr *sattr)
1125{
08e9eac4
TM
1126 struct rpc_cred *cred;
1127 struct inode *inode = dentry->d_inode;
1128 struct nfs4_state *state;
1da177e4
LT
1129 int status;
1130
1131 fattr->valid = 0;
1132
08e9eac4
TM
1133 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
1134 if (IS_ERR(cred))
1135 return PTR_ERR(cred);
1136 /* Search for an existing WRITE delegation first */
1137 state = nfs4_open_delegated(inode, FMODE_WRITE, cred);
1138 if (!IS_ERR(state)) {
1139 /* NB: nfs4_open_delegated() bumps the inode->i_count */
1140 iput(inode);
1141 } else {
1142 /* Search for an existing open(O_WRITE) stateid */
1da177e4 1143 state = nfs4_find_state(inode, cred, FMODE_WRITE);
1da177e4 1144 }
08e9eac4 1145
1da177e4
LT
1146 status = nfs4_do_setattr(NFS_SERVER(inode), fattr,
1147 NFS_FH(inode), sattr, state);
08e9eac4 1148 if (state != NULL)
1da177e4 1149 nfs4_close_state(state, FMODE_WRITE);
08e9eac4 1150 put_rpccred(cred);
1da177e4
LT
1151 return status;
1152}
1153
1154static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name,
1155 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1156{
1157 int status;
1158 struct nfs_server *server = NFS_SERVER(dir);
1159 struct nfs4_lookup_arg args = {
1160 .bitmask = server->attr_bitmask,
1161 .dir_fh = NFS_FH(dir),
1162 .name = name,
1163 };
1164 struct nfs4_lookup_res res = {
1165 .server = server,
1166 .fattr = fattr,
1167 .fh = fhandle,
1168 };
1169 struct rpc_message msg = {
1170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
1171 .rpc_argp = &args,
1172 .rpc_resp = &res,
1173 };
1174
1175 fattr->valid = 0;
1176
1177 dprintk("NFS call lookup %s\n", name->name);
1178 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1179 dprintk("NFS reply lookup: %d\n", status);
1180 return status;
1181}
1182
1183static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
1184{
1185 struct nfs4_exception exception = { };
1186 int err;
1187 do {
1188 err = nfs4_handle_exception(NFS_SERVER(dir),
1189 _nfs4_proc_lookup(dir, name, fhandle, fattr),
1190 &exception);
1191 } while (exception.retry);
1192 return err;
1193}
1194
1195static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1196{
1197 struct nfs4_accessargs args = {
1198 .fh = NFS_FH(inode),
1199 };
1200 struct nfs4_accessres res = { 0 };
1201 struct rpc_message msg = {
1202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
1203 .rpc_argp = &args,
1204 .rpc_resp = &res,
1205 .rpc_cred = entry->cred,
1206 };
1207 int mode = entry->mask;
1208 int status;
1209
1210 /*
1211 * Determine which access bits we want to ask for...
1212 */
1213 if (mode & MAY_READ)
1214 args.access |= NFS4_ACCESS_READ;
1215 if (S_ISDIR(inode->i_mode)) {
1216 if (mode & MAY_WRITE)
1217 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
1218 if (mode & MAY_EXEC)
1219 args.access |= NFS4_ACCESS_LOOKUP;
1220 } else {
1221 if (mode & MAY_WRITE)
1222 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
1223 if (mode & MAY_EXEC)
1224 args.access |= NFS4_ACCESS_EXECUTE;
1225 }
1226 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1227 if (!status) {
1228 entry->mask = 0;
1229 if (res.access & NFS4_ACCESS_READ)
1230 entry->mask |= MAY_READ;
1231 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
1232 entry->mask |= MAY_WRITE;
1233 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
1234 entry->mask |= MAY_EXEC;
1235 }
1236 return status;
1237}
1238
1239static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
1240{
1241 struct nfs4_exception exception = { };
1242 int err;
1243 do {
1244 err = nfs4_handle_exception(NFS_SERVER(inode),
1245 _nfs4_proc_access(inode, entry),
1246 &exception);
1247 } while (exception.retry);
1248 return err;
1249}
1250
1251/*
1252 * TODO: For the time being, we don't try to get any attributes
1253 * along with any of the zero-copy operations READ, READDIR,
1254 * READLINK, WRITE.
1255 *
1256 * In the case of the first three, we want to put the GETATTR
1257 * after the read-type operation -- this is because it is hard
1258 * to predict the length of a GETATTR response in v4, and thus
1259 * align the READ data correctly. This means that the GETATTR
1260 * may end up partially falling into the page cache, and we should
1261 * shift it into the 'tail' of the xdr_buf before processing.
1262 * To do this efficiently, we need to know the total length
1263 * of data received, which doesn't seem to be available outside
1264 * of the RPC layer.
1265 *
1266 * In the case of WRITE, we also want to put the GETATTR after
1267 * the operation -- in this case because we want to make sure
1268 * we get the post-operation mtime and size. This means that
1269 * we can't use xdr_encode_pages() as written: we need a variant
1270 * of it which would leave room in the 'tail' iovec.
1271 *
1272 * Both of these changes to the XDR layer would in fact be quite
1273 * minor, but I decided to leave them for a subsequent patch.
1274 */
1275static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
1276 unsigned int pgbase, unsigned int pglen)
1277{
1278 struct nfs4_readlink args = {
1279 .fh = NFS_FH(inode),
1280 .pgbase = pgbase,
1281 .pglen = pglen,
1282 .pages = &page,
1283 };
1284 struct rpc_message msg = {
1285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
1286 .rpc_argp = &args,
1287 .rpc_resp = NULL,
1288 };
1289
1290 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1291}
1292
1293static int nfs4_proc_readlink(struct inode *inode, struct page *page,
1294 unsigned int pgbase, unsigned int pglen)
1295{
1296 struct nfs4_exception exception = { };
1297 int err;
1298 do {
1299 err = nfs4_handle_exception(NFS_SERVER(inode),
1300 _nfs4_proc_readlink(inode, page, pgbase, pglen),
1301 &exception);
1302 } while (exception.retry);
1303 return err;
1304}
1305
1306static int _nfs4_proc_read(struct nfs_read_data *rdata)
1307{
1308 int flags = rdata->flags;
1309 struct inode *inode = rdata->inode;
1310 struct nfs_fattr *fattr = rdata->res.fattr;
1311 struct nfs_server *server = NFS_SERVER(inode);
1312 struct rpc_message msg = {
1313 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1314 .rpc_argp = &rdata->args,
1315 .rpc_resp = &rdata->res,
1316 .rpc_cred = rdata->cred,
1317 };
1318 unsigned long timestamp = jiffies;
1319 int status;
1320
1321 dprintk("NFS call read %d @ %Ld\n", rdata->args.count,
1322 (long long) rdata->args.offset);
1323
1324 fattr->valid = 0;
1325 status = rpc_call_sync(server->client, &msg, flags);
1326 if (!status)
1327 renew_lease(server, timestamp);
1328 dprintk("NFS reply read: %d\n", status);
1329 return status;
1330}
1331
1332static int nfs4_proc_read(struct nfs_read_data *rdata)
1333{
1334 struct nfs4_exception exception = { };
1335 int err;
1336 do {
1337 err = nfs4_handle_exception(NFS_SERVER(rdata->inode),
1338 _nfs4_proc_read(rdata),
1339 &exception);
1340 } while (exception.retry);
1341 return err;
1342}
1343
1344static int _nfs4_proc_write(struct nfs_write_data *wdata)
1345{
1346 int rpcflags = wdata->flags;
1347 struct inode *inode = wdata->inode;
1348 struct nfs_fattr *fattr = wdata->res.fattr;
1349 struct nfs_server *server = NFS_SERVER(inode);
1350 struct rpc_message msg = {
1351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1352 .rpc_argp = &wdata->args,
1353 .rpc_resp = &wdata->res,
1354 .rpc_cred = wdata->cred,
1355 };
1356 int status;
1357
1358 dprintk("NFS call write %d @ %Ld\n", wdata->args.count,
1359 (long long) wdata->args.offset);
1360
1361 fattr->valid = 0;
1362 status = rpc_call_sync(server->client, &msg, rpcflags);
1363 dprintk("NFS reply write: %d\n", status);
1364 return status;
1365}
1366
1367static int nfs4_proc_write(struct nfs_write_data *wdata)
1368{
1369 struct nfs4_exception exception = { };
1370 int err;
1371 do {
1372 err = nfs4_handle_exception(NFS_SERVER(wdata->inode),
1373 _nfs4_proc_write(wdata),
1374 &exception);
1375 } while (exception.retry);
1376 return err;
1377}
1378
1379static int _nfs4_proc_commit(struct nfs_write_data *cdata)
1380{
1381 struct inode *inode = cdata->inode;
1382 struct nfs_fattr *fattr = cdata->res.fattr;
1383 struct nfs_server *server = NFS_SERVER(inode);
1384 struct rpc_message msg = {
1385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
1386 .rpc_argp = &cdata->args,
1387 .rpc_resp = &cdata->res,
1388 .rpc_cred = cdata->cred,
1389 };
1390 int status;
1391
1392 dprintk("NFS call commit %d @ %Ld\n", cdata->args.count,
1393 (long long) cdata->args.offset);
1394
1395 fattr->valid = 0;
1396 status = rpc_call_sync(server->client, &msg, 0);
1397 dprintk("NFS reply commit: %d\n", status);
1398 return status;
1399}
1400
1401static int nfs4_proc_commit(struct nfs_write_data *cdata)
1402{
1403 struct nfs4_exception exception = { };
1404 int err;
1405 do {
1406 err = nfs4_handle_exception(NFS_SERVER(cdata->inode),
1407 _nfs4_proc_commit(cdata),
1408 &exception);
1409 } while (exception.retry);
1410 return err;
1411}
1412
1413/*
1414 * Got race?
1415 * We will need to arrange for the VFS layer to provide an atomic open.
1416 * Until then, this create/open method is prone to inefficiency and race
1417 * conditions due to the lookup, create, and open VFS calls from sys_open()
1418 * placed on the wire.
1419 *
1420 * Given the above sorry state of affairs, I'm simply sending an OPEN.
1421 * The file will be opened again in the subsequent VFS open call
1422 * (nfs4_proc_file_open).
1423 *
1424 * The open for read will just hang around to be used by any process that
1425 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
1426 */
1427
1428static int
1429nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
1430 int flags)
1431{
1432 struct nfs4_state *state;
1433 struct rpc_cred *cred;
1434 int status = 0;
1435
1436 cred = rpcauth_lookupcred(NFS_SERVER(dir)->client->cl_auth, 0);
1437 if (IS_ERR(cred)) {
1438 status = PTR_ERR(cred);
1439 goto out;
1440 }
1441 state = nfs4_do_open(dir, dentry, flags, sattr, cred);
1442 put_rpccred(cred);
1443 if (IS_ERR(state)) {
1444 status = PTR_ERR(state);
1445 goto out;
1446 }
1447 d_instantiate(dentry, state->inode);
1448 if (flags & O_EXCL) {
1449 struct nfs_fattr fattr;
1450 status = nfs4_do_setattr(NFS_SERVER(dir), &fattr,
1451 NFS_FH(state->inode), sattr, state);
1452 if (status == 0)
1453 goto out;
1454 } else if (flags != 0)
1455 goto out;
1456 nfs4_close_state(state, flags);
1457out:
1458 return status;
1459}
1460
1461static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
1462{
1463 struct nfs4_remove_arg args = {
1464 .fh = NFS_FH(dir),
1465 .name = name,
1466 };
1467 struct nfs4_change_info res;
1468 struct rpc_message msg = {
1469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
1470 .rpc_argp = &args,
1471 .rpc_resp = &res,
1472 };
1473 int status;
1474
1475 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1476 if (status == 0)
1477 update_changeattr(dir, &res);
1478 return status;
1479}
1480
1481static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
1482{
1483 struct nfs4_exception exception = { };
1484 int err;
1485 do {
1486 err = nfs4_handle_exception(NFS_SERVER(dir),
1487 _nfs4_proc_remove(dir, name),
1488 &exception);
1489 } while (exception.retry);
1490 return err;
1491}
1492
1493struct unlink_desc {
1494 struct nfs4_remove_arg args;
1495 struct nfs4_change_info res;
1496};
1497
1498static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir,
1499 struct qstr *name)
1500{
1501 struct unlink_desc *up;
1502
1503 up = (struct unlink_desc *) kmalloc(sizeof(*up), GFP_KERNEL);
1504 if (!up)
1505 return -ENOMEM;
1506
1507 up->args.fh = NFS_FH(dir->d_inode);
1508 up->args.name = name;
1509
1510 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
1511 msg->rpc_argp = &up->args;
1512 msg->rpc_resp = &up->res;
1513 return 0;
1514}
1515
1516static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task)
1517{
1518 struct rpc_message *msg = &task->tk_msg;
1519 struct unlink_desc *up;
1520
1521 if (msg->rpc_resp != NULL) {
1522 up = container_of(msg->rpc_resp, struct unlink_desc, res);
1523 update_changeattr(dir->d_inode, &up->res);
1524 kfree(up);
1525 msg->rpc_resp = NULL;
1526 msg->rpc_argp = NULL;
1527 }
1528 return 0;
1529}
1530
1531static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1532 struct inode *new_dir, struct qstr *new_name)
1533{
1534 struct nfs4_rename_arg arg = {
1535 .old_dir = NFS_FH(old_dir),
1536 .new_dir = NFS_FH(new_dir),
1537 .old_name = old_name,
1538 .new_name = new_name,
1539 };
1540 struct nfs4_rename_res res = { };
1541 struct rpc_message msg = {
1542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
1543 .rpc_argp = &arg,
1544 .rpc_resp = &res,
1545 };
1546 int status;
1547
1548 status = rpc_call_sync(NFS_CLIENT(old_dir), &msg, 0);
1549
1550 if (!status) {
1551 update_changeattr(old_dir, &res.old_cinfo);
1552 update_changeattr(new_dir, &res.new_cinfo);
1553 }
1554 return status;
1555}
1556
1557static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
1558 struct inode *new_dir, struct qstr *new_name)
1559{
1560 struct nfs4_exception exception = { };
1561 int err;
1562 do {
1563 err = nfs4_handle_exception(NFS_SERVER(old_dir),
1564 _nfs4_proc_rename(old_dir, old_name,
1565 new_dir, new_name),
1566 &exception);
1567 } while (exception.retry);
1568 return err;
1569}
1570
1571static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1572{
1573 struct nfs4_link_arg arg = {
1574 .fh = NFS_FH(inode),
1575 .dir_fh = NFS_FH(dir),
1576 .name = name,
1577 };
1578 struct nfs4_change_info cinfo = { };
1579 struct rpc_message msg = {
1580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
1581 .rpc_argp = &arg,
1582 .rpc_resp = &cinfo,
1583 };
1584 int status;
1585
1586 status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
1587 if (!status)
1588 update_changeattr(dir, &cinfo);
1589
1590 return status;
1591}
1592
1593static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
1594{
1595 struct nfs4_exception exception = { };
1596 int err;
1597 do {
1598 err = nfs4_handle_exception(NFS_SERVER(inode),
1599 _nfs4_proc_link(inode, dir, name),
1600 &exception);
1601 } while (exception.retry);
1602 return err;
1603}
1604
1605static int _nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1606 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1607 struct nfs_fattr *fattr)
1608{
1609 struct nfs_server *server = NFS_SERVER(dir);
1610 struct nfs4_create_arg arg = {
1611 .dir_fh = NFS_FH(dir),
1612 .server = server,
1613 .name = name,
1614 .attrs = sattr,
1615 .ftype = NF4LNK,
1616 .bitmask = server->attr_bitmask,
1617 };
1618 struct nfs4_create_res res = {
1619 .server = server,
1620 .fh = fhandle,
1621 .fattr = fattr,
1622 };
1623 struct rpc_message msg = {
1624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK],
1625 .rpc_argp = &arg,
1626 .rpc_resp = &res,
1627 };
1628 int status;
1629
1630 if (path->len > NFS4_MAXPATHLEN)
1631 return -ENAMETOOLONG;
1632 arg.u.symlink = path;
1633 fattr->valid = 0;
1634
1635 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1636 if (!status)
1637 update_changeattr(dir, &res.dir_cinfo);
1638 return status;
1639}
1640
1641static int nfs4_proc_symlink(struct inode *dir, struct qstr *name,
1642 struct qstr *path, struct iattr *sattr, struct nfs_fh *fhandle,
1643 struct nfs_fattr *fattr)
1644{
1645 struct nfs4_exception exception = { };
1646 int err;
1647 do {
1648 err = nfs4_handle_exception(NFS_SERVER(dir),
1649 _nfs4_proc_symlink(dir, name, path, sattr,
1650 fhandle, fattr),
1651 &exception);
1652 } while (exception.retry);
1653 return err;
1654}
1655
1656static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1657 struct iattr *sattr)
1658{
1659 struct nfs_server *server = NFS_SERVER(dir);
1660 struct nfs_fh fhandle;
1661 struct nfs_fattr fattr;
1662 struct nfs4_create_arg arg = {
1663 .dir_fh = NFS_FH(dir),
1664 .server = server,
1665 .name = &dentry->d_name,
1666 .attrs = sattr,
1667 .ftype = NF4DIR,
1668 .bitmask = server->attr_bitmask,
1669 };
1670 struct nfs4_create_res res = {
1671 .server = server,
1672 .fh = &fhandle,
1673 .fattr = &fattr,
1674 };
1675 struct rpc_message msg = {
1676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1677 .rpc_argp = &arg,
1678 .rpc_resp = &res,
1679 };
1680 int status;
1681
1682 fattr.valid = 0;
1683
1684 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1685 if (!status) {
1686 update_changeattr(dir, &res.dir_cinfo);
1687 status = nfs_instantiate(dentry, &fhandle, &fattr);
1688 }
1689 return status;
1690}
1691
1692static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
1693 struct iattr *sattr)
1694{
1695 struct nfs4_exception exception = { };
1696 int err;
1697 do {
1698 err = nfs4_handle_exception(NFS_SERVER(dir),
1699 _nfs4_proc_mkdir(dir, dentry, sattr),
1700 &exception);
1701 } while (exception.retry);
1702 return err;
1703}
1704
1705static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1706 u64 cookie, struct page *page, unsigned int count, int plus)
1707{
1708 struct inode *dir = dentry->d_inode;
1709 struct nfs4_readdir_arg args = {
1710 .fh = NFS_FH(dir),
1711 .pages = &page,
1712 .pgbase = 0,
1713 .count = count,
1714 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
1715 };
1716 struct nfs4_readdir_res res;
1717 struct rpc_message msg = {
1718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
1719 .rpc_argp = &args,
1720 .rpc_resp = &res,
1721 .rpc_cred = cred,
1722 };
1723 int status;
1724
1725 lock_kernel();
1726 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
1727 res.pgbase = args.pgbase;
1728 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1729 if (status == 0)
1730 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
1731 unlock_kernel();
1732 return status;
1733}
1734
1735static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
1736 u64 cookie, struct page *page, unsigned int count, int plus)
1737{
1738 struct nfs4_exception exception = { };
1739 int err;
1740 do {
1741 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
1742 _nfs4_proc_readdir(dentry, cred, cookie,
1743 page, count, plus),
1744 &exception);
1745 } while (exception.retry);
1746 return err;
1747}
1748
1749static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1750 struct iattr *sattr, dev_t rdev)
1751{
1752 struct nfs_server *server = NFS_SERVER(dir);
1753 struct nfs_fh fh;
1754 struct nfs_fattr fattr;
1755 struct nfs4_create_arg arg = {
1756 .dir_fh = NFS_FH(dir),
1757 .server = server,
1758 .name = &dentry->d_name,
1759 .attrs = sattr,
1760 .bitmask = server->attr_bitmask,
1761 };
1762 struct nfs4_create_res res = {
1763 .server = server,
1764 .fh = &fh,
1765 .fattr = &fattr,
1766 };
1767 struct rpc_message msg = {
1768 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE],
1769 .rpc_argp = &arg,
1770 .rpc_resp = &res,
1771 };
1772 int status;
1773 int mode = sattr->ia_mode;
1774
1775 fattr.valid = 0;
1776
1777 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
1778 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
1779 if (S_ISFIFO(mode))
1780 arg.ftype = NF4FIFO;
1781 else if (S_ISBLK(mode)) {
1782 arg.ftype = NF4BLK;
1783 arg.u.device.specdata1 = MAJOR(rdev);
1784 arg.u.device.specdata2 = MINOR(rdev);
1785 }
1786 else if (S_ISCHR(mode)) {
1787 arg.ftype = NF4CHR;
1788 arg.u.device.specdata1 = MAJOR(rdev);
1789 arg.u.device.specdata2 = MINOR(rdev);
1790 }
1791 else
1792 arg.ftype = NF4SOCK;
1793
1794 status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0);
1795 if (status == 0) {
1796 update_changeattr(dir, &res.dir_cinfo);
1797 status = nfs_instantiate(dentry, &fh, &fattr);
1798 }
1799 return status;
1800}
1801
1802static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
1803 struct iattr *sattr, dev_t rdev)
1804{
1805 struct nfs4_exception exception = { };
1806 int err;
1807 do {
1808 err = nfs4_handle_exception(NFS_SERVER(dir),
1809 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
1810 &exception);
1811 } while (exception.retry);
1812 return err;
1813}
1814
1815static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
1816 struct nfs_fsstat *fsstat)
1817{
1818 struct nfs4_statfs_arg args = {
1819 .fh = fhandle,
1820 .bitmask = server->attr_bitmask,
1821 };
1822 struct rpc_message msg = {
1823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
1824 .rpc_argp = &args,
1825 .rpc_resp = fsstat,
1826 };
1827
1828 fsstat->fattr->valid = 0;
1829 return rpc_call_sync(server->client, &msg, 0);
1830}
1831
1832static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
1833{
1834 struct nfs4_exception exception = { };
1835 int err;
1836 do {
1837 err = nfs4_handle_exception(server,
1838 _nfs4_proc_statfs(server, fhandle, fsstat),
1839 &exception);
1840 } while (exception.retry);
1841 return err;
1842}
1843
1844static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
1845 struct nfs_fsinfo *fsinfo)
1846{
1847 struct nfs4_fsinfo_arg args = {
1848 .fh = fhandle,
1849 .bitmask = server->attr_bitmask,
1850 };
1851 struct rpc_message msg = {
1852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
1853 .rpc_argp = &args,
1854 .rpc_resp = fsinfo,
1855 };
1856
1857 return rpc_call_sync(server->client, &msg, 0);
1858}
1859
1860static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1861{
1862 struct nfs4_exception exception = { };
1863 int err;
1864
1865 do {
1866 err = nfs4_handle_exception(server,
1867 _nfs4_do_fsinfo(server, fhandle, fsinfo),
1868 &exception);
1869 } while (exception.retry);
1870 return err;
1871}
1872
1873static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
1874{
1875 fsinfo->fattr->valid = 0;
1876 return nfs4_do_fsinfo(server, fhandle, fsinfo);
1877}
1878
1879static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1880 struct nfs_pathconf *pathconf)
1881{
1882 struct nfs4_pathconf_arg args = {
1883 .fh = fhandle,
1884 .bitmask = server->attr_bitmask,
1885 };
1886 struct rpc_message msg = {
1887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
1888 .rpc_argp = &args,
1889 .rpc_resp = pathconf,
1890 };
1891
1892 /* None of the pathconf attributes are mandatory to implement */
1893 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
1894 memset(pathconf, 0, sizeof(*pathconf));
1895 return 0;
1896 }
1897
1898 pathconf->fattr->valid = 0;
1899 return rpc_call_sync(server->client, &msg, 0);
1900}
1901
1902static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
1903 struct nfs_pathconf *pathconf)
1904{
1905 struct nfs4_exception exception = { };
1906 int err;
1907
1908 do {
1909 err = nfs4_handle_exception(server,
1910 _nfs4_proc_pathconf(server, fhandle, pathconf),
1911 &exception);
1912 } while (exception.retry);
1913 return err;
1914}
1915
1916static void
1917nfs4_read_done(struct rpc_task *task)
1918{
1919 struct nfs_read_data *data = (struct nfs_read_data *) task->tk_calldata;
1920 struct inode *inode = data->inode;
1921
1922 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1923 rpc_restart_call(task);
1924 return;
1925 }
1926 if (task->tk_status > 0)
1927 renew_lease(NFS_SERVER(inode), data->timestamp);
1928 /* Call back common NFS readpage processing */
1929 nfs_readpage_result(task);
1930}
1931
1932static void
1933nfs4_proc_read_setup(struct nfs_read_data *data)
1934{
1935 struct rpc_task *task = &data->task;
1936 struct rpc_message msg = {
1937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ],
1938 .rpc_argp = &data->args,
1939 .rpc_resp = &data->res,
1940 .rpc_cred = data->cred,
1941 };
1942 struct inode *inode = data->inode;
1943 int flags;
1944
1945 data->timestamp = jiffies;
1946
1947 /* N.B. Do we need to test? Never called for swapfile inode */
1948 flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
1949
1950 /* Finalize the task. */
1951 rpc_init_task(task, NFS_CLIENT(inode), nfs4_read_done, flags);
1952 rpc_call_setup(task, &msg, 0);
1953}
1954
1955static void
1956nfs4_write_done(struct rpc_task *task)
1957{
1958 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
1959 struct inode *inode = data->inode;
1960
1961 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
1962 rpc_restart_call(task);
1963 return;
1964 }
1965 if (task->tk_status >= 0)
1966 renew_lease(NFS_SERVER(inode), data->timestamp);
1967 /* Call back common NFS writeback processing */
1968 nfs_writeback_done(task);
1969}
1970
1971static void
1972nfs4_proc_write_setup(struct nfs_write_data *data, int how)
1973{
1974 struct rpc_task *task = &data->task;
1975 struct rpc_message msg = {
1976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE],
1977 .rpc_argp = &data->args,
1978 .rpc_resp = &data->res,
1979 .rpc_cred = data->cred,
1980 };
1981 struct inode *inode = data->inode;
1982 int stable;
1983 int flags;
1984
1985 if (how & FLUSH_STABLE) {
1986 if (!NFS_I(inode)->ncommit)
1987 stable = NFS_FILE_SYNC;
1988 else
1989 stable = NFS_DATA_SYNC;
1990 } else
1991 stable = NFS_UNSTABLE;
1992 data->args.stable = stable;
1993
1994 data->timestamp = jiffies;
1995
1996 /* Set the initial flags for the task. */
1997 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
1998
1999 /* Finalize the task. */
2000 rpc_init_task(task, NFS_CLIENT(inode), nfs4_write_done, flags);
2001 rpc_call_setup(task, &msg, 0);
2002}
2003
2004static void
2005nfs4_commit_done(struct rpc_task *task)
2006{
2007 struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata;
2008 struct inode *inode = data->inode;
2009
2010 if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) {
2011 rpc_restart_call(task);
2012 return;
2013 }
2014 /* Call back common NFS writeback processing */
2015 nfs_commit_done(task);
2016}
2017
2018static void
2019nfs4_proc_commit_setup(struct nfs_write_data *data, int how)
2020{
2021 struct rpc_task *task = &data->task;
2022 struct rpc_message msg = {
2023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
2024 .rpc_argp = &data->args,
2025 .rpc_resp = &data->res,
2026 .rpc_cred = data->cred,
2027 };
2028 struct inode *inode = data->inode;
2029 int flags;
2030
2031 /* Set the initial flags for the task. */
2032 flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
2033
2034 /* Finalize the task. */
2035 rpc_init_task(task, NFS_CLIENT(inode), nfs4_commit_done, flags);
2036 rpc_call_setup(task, &msg, 0);
2037}
2038
2039/*
2040 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
2041 * standalone procedure for queueing an asynchronous RENEW.
2042 */
2043static void
2044renew_done(struct rpc_task *task)
2045{
2046 struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
2047 unsigned long timestamp = (unsigned long)task->tk_calldata;
2048
2049 if (task->tk_status < 0) {
2050 switch (task->tk_status) {
2051 case -NFS4ERR_STALE_CLIENTID:
2052 case -NFS4ERR_EXPIRED:
2053 case -NFS4ERR_CB_PATH_DOWN:
2054 nfs4_schedule_state_recovery(clp);
2055 }
2056 return;
2057 }
2058 spin_lock(&clp->cl_lock);
2059 if (time_before(clp->cl_last_renewal,timestamp))
2060 clp->cl_last_renewal = timestamp;
2061 spin_unlock(&clp->cl_lock);
2062}
2063
2064int
2065nfs4_proc_async_renew(struct nfs4_client *clp)
2066{
2067 struct rpc_message msg = {
2068 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2069 .rpc_argp = clp,
2070 .rpc_cred = clp->cl_cred,
2071 };
2072
2073 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
2074 renew_done, (void *)jiffies);
2075}
2076
2077int
2078nfs4_proc_renew(struct nfs4_client *clp)
2079{
2080 struct rpc_message msg = {
2081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
2082 .rpc_argp = clp,
2083 .rpc_cred = clp->cl_cred,
2084 };
2085 unsigned long now = jiffies;
2086 int status;
2087
2088 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2089 if (status < 0)
2090 return status;
2091 spin_lock(&clp->cl_lock);
2092 if (time_before(clp->cl_last_renewal,now))
2093 clp->cl_last_renewal = now;
2094 spin_unlock(&clp->cl_lock);
2095 return 0;
2096}
2097
2098/*
2099 * We will need to arrange for the VFS layer to provide an atomic open.
2100 * Until then, this open method is prone to inefficiency and race conditions
2101 * due to the lookup, potential create, and open VFS calls from sys_open()
2102 * placed on the wire.
2103 */
2104static int
2105nfs4_proc_file_open(struct inode *inode, struct file *filp)
2106{
2107 struct dentry *dentry = filp->f_dentry;
2108 struct nfs_open_context *ctx;
2109 struct nfs4_state *state = NULL;
2110 struct rpc_cred *cred;
2111 int status = -ENOMEM;
2112
2113 dprintk("nfs4_proc_file_open: starting on (%.*s/%.*s)\n",
2114 (int)dentry->d_parent->d_name.len,
2115 dentry->d_parent->d_name.name,
2116 (int)dentry->d_name.len, dentry->d_name.name);
2117
2118
2119 /* Find our open stateid */
2120 cred = rpcauth_lookupcred(NFS_SERVER(inode)->client->cl_auth, 0);
2121 if (IS_ERR(cred))
2122 return PTR_ERR(cred);
2123 ctx = alloc_nfs_open_context(dentry, cred);
2124 put_rpccred(cred);
2125 if (unlikely(ctx == NULL))
2126 return -ENOMEM;
2127 status = -EIO; /* ERACE actually */
2128 state = nfs4_find_state(inode, cred, filp->f_mode);
2129 if (unlikely(state == NULL))
2130 goto no_state;
2131 ctx->state = state;
2132 nfs4_close_state(state, filp->f_mode);
2133 ctx->mode = filp->f_mode;
2134 nfs_file_set_open_context(filp, ctx);
2135 put_nfs_open_context(ctx);
2136 if (filp->f_mode & FMODE_WRITE)
2137 nfs_begin_data_update(inode);
2138 return 0;
2139no_state:
2140 printk(KERN_WARNING "NFS: v4 raced in function %s\n", __FUNCTION__);
2141 put_nfs_open_context(ctx);
2142 return status;
2143}
2144
2145/*
2146 * Release our state
2147 */
2148static int
2149nfs4_proc_file_release(struct inode *inode, struct file *filp)
2150{
2151 if (filp->f_mode & FMODE_WRITE)
2152 nfs_end_data_update(inode);
2153 nfs_file_clear_open_context(filp);
2154 return 0;
2155}
2156
aa1870af
BF
2157static inline int nfs4_server_supports_acls(struct nfs_server *server)
2158{
2159 return (server->caps & NFS_CAP_ACLS)
2160 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2161 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
2162}
2163
2164/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
2165 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
2166 * the stack.
2167 */
2168#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
2169
2170static void buf_to_pages(const void *buf, size_t buflen,
2171 struct page **pages, unsigned int *pgbase)
2172{
2173 const void *p = buf;
2174
2175 *pgbase = offset_in_page(buf);
2176 p -= *pgbase;
2177 while (p < buf + buflen) {
2178 *(pages++) = virt_to_page(p);
2179 p += PAGE_CACHE_SIZE;
2180 }
2181}
2182
e50a1c2e
BF
2183struct nfs4_cached_acl {
2184 int cached;
2185 size_t len;
3e9d4154 2186 char data[0];
e50a1c2e
BF
2187};
2188
2189static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
2190{
2191 struct nfs_inode *nfsi = NFS_I(inode);
2192
2193 spin_lock(&inode->i_lock);
2194 kfree(nfsi->nfs4_acl);
2195 nfsi->nfs4_acl = acl;
2196 spin_unlock(&inode->i_lock);
2197}
2198
2199static void nfs4_zap_acl_attr(struct inode *inode)
2200{
2201 nfs4_set_cached_acl(inode, NULL);
2202}
2203
2204static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
2205{
2206 struct nfs_inode *nfsi = NFS_I(inode);
2207 struct nfs4_cached_acl *acl;
2208 int ret = -ENOENT;
2209
2210 spin_lock(&inode->i_lock);
2211 acl = nfsi->nfs4_acl;
2212 if (acl == NULL)
2213 goto out;
2214 if (buf == NULL) /* user is just asking for length */
2215 goto out_len;
2216 if (acl->cached == 0)
2217 goto out;
2218 ret = -ERANGE; /* see getxattr(2) man page */
2219 if (acl->len > buflen)
2220 goto out;
2221 memcpy(buf, acl->data, acl->len);
2222out_len:
2223 ret = acl->len;
2224out:
2225 spin_unlock(&inode->i_lock);
2226 return ret;
2227}
2228
2229static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
2230{
2231 struct nfs4_cached_acl *acl;
2232
2233 if (buf && acl_len <= PAGE_SIZE) {
2234 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
2235 if (acl == NULL)
2236 goto out;
2237 acl->cached = 1;
2238 memcpy(acl->data, buf, acl_len);
2239 } else {
2240 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
2241 if (acl == NULL)
2242 goto out;
2243 acl->cached = 0;
2244 }
2245 acl->len = acl_len;
2246out:
2247 nfs4_set_cached_acl(inode, acl);
2248}
2249
2250static inline ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
aa1870af 2251{
aa1870af
BF
2252 struct page *pages[NFS4ACL_MAXPAGES];
2253 struct nfs_getaclargs args = {
2254 .fh = NFS_FH(inode),
2255 .acl_pages = pages,
2256 .acl_len = buflen,
2257 };
2258 size_t resp_len = buflen;
e50a1c2e 2259 void *resp_buf;
aa1870af
BF
2260 struct rpc_message msg = {
2261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
2262 .rpc_argp = &args,
2263 .rpc_resp = &resp_len,
2264 };
e50a1c2e 2265 struct page *localpage = NULL;
aa1870af
BF
2266 int ret;
2267
e50a1c2e
BF
2268 if (buflen < PAGE_SIZE) {
2269 /* As long as we're doing a round trip to the server anyway,
2270 * let's be prepared for a page of acl data. */
2271 localpage = alloc_page(GFP_KERNEL);
2272 resp_buf = page_address(localpage);
2273 if (localpage == NULL)
2274 return -ENOMEM;
2275 args.acl_pages[0] = localpage;
2276 args.acl_pgbase = 0;
2277 args.acl_len = PAGE_SIZE;
2278 } else {
2279 resp_buf = buf;
2280 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
2281 }
aa1870af 2282 ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
e50a1c2e
BF
2283 if (ret)
2284 goto out_free;
2285 if (resp_len > args.acl_len)
2286 nfs4_write_cached_acl(inode, NULL, resp_len);
2287 else
2288 nfs4_write_cached_acl(inode, resp_buf, resp_len);
2289 if (buf) {
2290 ret = -ERANGE;
2291 if (resp_len > buflen)
2292 goto out_free;
2293 if (localpage)
2294 memcpy(buf, resp_buf, resp_len);
2295 }
2296 ret = resp_len;
2297out_free:
2298 if (localpage)
2299 __free_page(localpage);
aa1870af
BF
2300 return ret;
2301}
2302
e50a1c2e
BF
2303static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
2304{
2305 struct nfs_server *server = NFS_SERVER(inode);
2306 int ret;
2307
2308 if (!nfs4_server_supports_acls(server))
2309 return -EOPNOTSUPP;
2310 ret = nfs_revalidate_inode(server, inode);
2311 if (ret < 0)
2312 return ret;
2313 ret = nfs4_read_cached_acl(inode, buf, buflen);
2314 if (ret != -ENOENT)
2315 return ret;
2316 return nfs4_get_acl_uncached(inode, buf, buflen);
2317}
2318
4b580ee3
BF
2319static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
2320{
2321 struct nfs_server *server = NFS_SERVER(inode);
2322 struct page *pages[NFS4ACL_MAXPAGES];
2323 struct nfs_setaclargs arg = {
2324 .fh = NFS_FH(inode),
2325 .acl_pages = pages,
2326 .acl_len = buflen,
2327 };
2328 struct rpc_message msg = {
2329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
2330 .rpc_argp = &arg,
2331 .rpc_resp = NULL,
2332 };
2333 int ret;
2334
2335 if (!nfs4_server_supports_acls(server))
2336 return -EOPNOTSUPP;
2337 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
2338 ret = rpc_call_sync(NFS_SERVER(inode)->client, &msg, 0);
e50a1c2e
BF
2339 if (ret == 0)
2340 nfs4_write_cached_acl(inode, buf, buflen);
4b580ee3
BF
2341 return ret;
2342}
2343
1da177e4
LT
2344static int
2345nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server)
2346{
2347 struct nfs4_client *clp = server->nfs4_state;
2348
2349 if (!clp || task->tk_status >= 0)
2350 return 0;
2351 switch(task->tk_status) {
2352 case -NFS4ERR_STALE_CLIENTID:
2353 case -NFS4ERR_STALE_STATEID:
2354 case -NFS4ERR_EXPIRED:
2355 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL);
2356 nfs4_schedule_state_recovery(clp);
2357 if (test_bit(NFS4CLNT_OK, &clp->cl_state))
2358 rpc_wake_up_task(task);
2359 task->tk_status = 0;
2360 return -EAGAIN;
2361 case -NFS4ERR_GRACE:
2362 case -NFS4ERR_DELAY:
2363 rpc_delay(task, NFS4_POLL_RETRY_MAX);
2364 task->tk_status = 0;
2365 return -EAGAIN;
2366 case -NFS4ERR_OLD_STATEID:
2367 task->tk_status = 0;
2368 return -EAGAIN;
2369 }
2370 task->tk_status = nfs4_map_errors(task->tk_status);
2371 return 0;
2372}
2373
2374static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs4_client *clp)
2375{
2376 DEFINE_WAIT(wait);
2377 sigset_t oldset;
2378 int interruptible, res = 0;
2379
2380 might_sleep();
2381
2382 rpc_clnt_sigmask(clnt, &oldset);
2383 interruptible = TASK_UNINTERRUPTIBLE;
2384 if (clnt->cl_intr)
2385 interruptible = TASK_INTERRUPTIBLE;
2386 prepare_to_wait(&clp->cl_waitq, &wait, interruptible);
2387 nfs4_schedule_state_recovery(clp);
2388 if (clnt->cl_intr && signalled())
2389 res = -ERESTARTSYS;
2390 else if (!test_bit(NFS4CLNT_OK, &clp->cl_state))
2391 schedule();
2392 finish_wait(&clp->cl_waitq, &wait);
2393 rpc_clnt_sigunmask(clnt, &oldset);
2394 return res;
2395}
2396
2397static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
2398{
2399 sigset_t oldset;
2400 int res = 0;
2401
2402 might_sleep();
2403
2404 if (*timeout <= 0)
2405 *timeout = NFS4_POLL_RETRY_MIN;
2406 if (*timeout > NFS4_POLL_RETRY_MAX)
2407 *timeout = NFS4_POLL_RETRY_MAX;
2408 rpc_clnt_sigmask(clnt, &oldset);
2409 if (clnt->cl_intr) {
2410 set_current_state(TASK_INTERRUPTIBLE);
2411 schedule_timeout(*timeout);
2412 if (signalled())
2413 res = -ERESTARTSYS;
2414 } else {
2415 set_current_state(TASK_UNINTERRUPTIBLE);
2416 schedule_timeout(*timeout);
2417 }
2418 rpc_clnt_sigunmask(clnt, &oldset);
2419 *timeout <<= 1;
2420 return res;
2421}
2422
2423/* This is the error handling routine for processes that are allowed
2424 * to sleep.
2425 */
2426int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
2427{
2428 struct nfs4_client *clp = server->nfs4_state;
2429 int ret = errorcode;
2430
2431 exception->retry = 0;
2432 switch(errorcode) {
2433 case 0:
2434 return 0;
2435 case -NFS4ERR_STALE_CLIENTID:
2436 case -NFS4ERR_STALE_STATEID:
2437 case -NFS4ERR_EXPIRED:
2438 ret = nfs4_wait_clnt_recover(server->client, clp);
2439 if (ret == 0)
2440 exception->retry = 1;
2441 break;
2442 case -NFS4ERR_GRACE:
2443 case -NFS4ERR_DELAY:
2444 ret = nfs4_delay(server->client, &exception->timeout);
2445 if (ret == 0)
2446 exception->retry = 1;
2447 break;
2448 case -NFS4ERR_OLD_STATEID:
2449 if (ret == 0)
2450 exception->retry = 1;
2451 }
2452 /* We failed to handle the error */
2453 return nfs4_map_errors(ret);
2454}
2455
2456int nfs4_proc_setclientid(struct nfs4_client *clp, u32 program, unsigned short port)
2457{
2458 nfs4_verifier sc_verifier;
2459 struct nfs4_setclientid setclientid = {
2460 .sc_verifier = &sc_verifier,
2461 .sc_prog = program,
2462 };
2463 struct rpc_message msg = {
2464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
2465 .rpc_argp = &setclientid,
2466 .rpc_resp = clp,
2467 .rpc_cred = clp->cl_cred,
2468 };
2469 u32 *p;
2470 int loop = 0;
2471 int status;
2472
2473 p = (u32*)sc_verifier.data;
2474 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
2475 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
2476
2477 for(;;) {
2478 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
2479 sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u",
2480 clp->cl_ipaddr, NIPQUAD(clp->cl_addr.s_addr),
2481 clp->cl_cred->cr_ops->cr_name,
2482 clp->cl_id_uniquifier);
2483 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
2484 sizeof(setclientid.sc_netid), "tcp");
2485 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
2486 sizeof(setclientid.sc_uaddr), "%s.%d.%d",
2487 clp->cl_ipaddr, port >> 8, port & 255);
2488
2489 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2490 if (status != -NFS4ERR_CLID_INUSE)
2491 break;
2492 if (signalled())
2493 break;
2494 if (loop++ & 1)
2495 ssleep(clp->cl_lease_time + 1);
2496 else
2497 if (++clp->cl_id_uniquifier == 0)
2498 break;
2499 }
2500 return status;
2501}
2502
2503int
2504nfs4_proc_setclientid_confirm(struct nfs4_client *clp)
2505{
2506 struct nfs_fsinfo fsinfo;
2507 struct rpc_message msg = {
2508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
2509 .rpc_argp = clp,
2510 .rpc_resp = &fsinfo,
2511 .rpc_cred = clp->cl_cred,
2512 };
2513 unsigned long now;
2514 int status;
2515
2516 now = jiffies;
2517 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
2518 if (status == 0) {
2519 spin_lock(&clp->cl_lock);
2520 clp->cl_lease_time = fsinfo.lease_time * HZ;
2521 clp->cl_last_renewal = now;
2522 spin_unlock(&clp->cl_lock);
2523 }
2524 return status;
2525}
2526
2527static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2528{
2529 struct nfs4_delegreturnargs args = {
2530 .fhandle = NFS_FH(inode),
2531 .stateid = stateid,
2532 };
2533 struct rpc_message msg = {
2534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
2535 .rpc_argp = &args,
2536 .rpc_cred = cred,
2537 };
2538
2539 return rpc_call_sync(NFS_CLIENT(inode), &msg, 0);
2540}
2541
2542int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid)
2543{
2544 struct nfs_server *server = NFS_SERVER(inode);
2545 struct nfs4_exception exception = { };
2546 int err;
2547 do {
2548 err = _nfs4_proc_delegreturn(inode, cred, stateid);
2549 switch (err) {
2550 case -NFS4ERR_STALE_STATEID:
2551 case -NFS4ERR_EXPIRED:
2552 nfs4_schedule_state_recovery(server->nfs4_state);
2553 case 0:
2554 return 0;
2555 }
2556 err = nfs4_handle_exception(server, err, &exception);
2557 } while (exception.retry);
2558 return err;
2559}
2560
2561#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
2562#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
2563
2564/*
2565 * sleep, with exponential backoff, and retry the LOCK operation.
2566 */
2567static unsigned long
2568nfs4_set_lock_task_retry(unsigned long timeout)
2569{
2570 current->state = TASK_INTERRUPTIBLE;
2571 schedule_timeout(timeout);
2572 timeout <<= 1;
2573 if (timeout > NFS4_LOCK_MAXTIMEOUT)
2574 return NFS4_LOCK_MAXTIMEOUT;
2575 return timeout;
2576}
2577
2578static inline int
2579nfs4_lck_type(int cmd, struct file_lock *request)
2580{
2581 /* set lock type */
2582 switch (request->fl_type) {
2583 case F_RDLCK:
2584 return IS_SETLKW(cmd) ? NFS4_READW_LT : NFS4_READ_LT;
2585 case F_WRLCK:
2586 return IS_SETLKW(cmd) ? NFS4_WRITEW_LT : NFS4_WRITE_LT;
2587 case F_UNLCK:
2588 return NFS4_WRITE_LT;
2589 }
2590 BUG();
2591 return 0;
2592}
2593
2594static inline uint64_t
2595nfs4_lck_length(struct file_lock *request)
2596{
2597 if (request->fl_end == OFFSET_MAX)
2598 return ~(uint64_t)0;
2599 return request->fl_end - request->fl_start + 1;
2600}
2601
2602static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2603{
2604 struct inode *inode = state->inode;
2605 struct nfs_server *server = NFS_SERVER(inode);
2606 struct nfs4_client *clp = server->nfs4_state;
2607 struct nfs_lockargs arg = {
2608 .fh = NFS_FH(inode),
2609 .type = nfs4_lck_type(cmd, request),
2610 .offset = request->fl_start,
2611 .length = nfs4_lck_length(request),
2612 };
2613 struct nfs_lockres res = {
2614 .server = server,
2615 };
2616 struct rpc_message msg = {
2617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
2618 .rpc_argp = &arg,
2619 .rpc_resp = &res,
2620 .rpc_cred = state->owner->so_cred,
2621 };
2622 struct nfs_lowner nlo;
2623 struct nfs4_lock_state *lsp;
2624 int status;
2625
2626 down_read(&clp->cl_sem);
2627 nlo.clientid = clp->cl_clientid;
2628 down(&state->lock_sema);
2629 lsp = nfs4_find_lock_state(state, request->fl_owner);
2630 if (lsp)
2631 nlo.id = lsp->ls_id;
2632 else {
2633 spin_lock(&clp->cl_lock);
2634 nlo.id = nfs4_alloc_lockowner_id(clp);
2635 spin_unlock(&clp->cl_lock);
2636 }
2637 arg.u.lockt = &nlo;
2638 status = rpc_call_sync(server->client, &msg, 0);
2639 if (!status) {
2640 request->fl_type = F_UNLCK;
2641 } else if (status == -NFS4ERR_DENIED) {
2642 int64_t len, start, end;
2643 start = res.u.denied.offset;
2644 len = res.u.denied.length;
2645 end = start + len - 1;
2646 if (end < 0 || len == 0)
2647 request->fl_end = OFFSET_MAX;
2648 else
2649 request->fl_end = (loff_t)end;
2650 request->fl_start = (loff_t)start;
2651 request->fl_type = F_WRLCK;
2652 if (res.u.denied.type & 1)
2653 request->fl_type = F_RDLCK;
2654 request->fl_pid = 0;
2655 status = 0;
2656 }
2657 if (lsp)
2658 nfs4_put_lock_state(lsp);
2659 up(&state->lock_sema);
2660 up_read(&clp->cl_sem);
2661 return status;
2662}
2663
2664static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2665{
2666 struct nfs4_exception exception = { };
2667 int err;
2668
2669 do {
2670 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2671 _nfs4_proc_getlk(state, cmd, request),
2672 &exception);
2673 } while (exception.retry);
2674 return err;
2675}
2676
2677static int do_vfs_lock(struct file *file, struct file_lock *fl)
2678{
2679 int res = 0;
2680 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
2681 case FL_POSIX:
2682 res = posix_lock_file_wait(file, fl);
2683 break;
2684 case FL_FLOCK:
2685 res = flock_lock_file_wait(file, fl);
2686 break;
2687 default:
2688 BUG();
2689 }
2690 if (res < 0)
2691 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n",
2692 __FUNCTION__);
2693 return res;
2694}
2695
2696static int _nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2697{
2698 struct inode *inode = state->inode;
2699 struct nfs_server *server = NFS_SERVER(inode);
2700 struct nfs4_client *clp = server->nfs4_state;
2701 struct nfs_lockargs arg = {
2702 .fh = NFS_FH(inode),
2703 .type = nfs4_lck_type(cmd, request),
2704 .offset = request->fl_start,
2705 .length = nfs4_lck_length(request),
2706 };
2707 struct nfs_lockres res = {
2708 .server = server,
2709 };
2710 struct rpc_message msg = {
2711 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
2712 .rpc_argp = &arg,
2713 .rpc_resp = &res,
2714 .rpc_cred = state->owner->so_cred,
2715 };
2716 struct nfs4_lock_state *lsp;
2717 struct nfs_locku_opargs luargs;
2718 int status = 0;
2719
2720 down_read(&clp->cl_sem);
2721 down(&state->lock_sema);
2722 lsp = nfs4_find_lock_state(state, request->fl_owner);
2723 if (!lsp)
2724 goto out;
2725 /* We might have lost the locks! */
2726 if ((lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0) {
2727 luargs.seqid = lsp->ls_seqid;
2728 memcpy(&luargs.stateid, &lsp->ls_stateid, sizeof(luargs.stateid));
2729 arg.u.locku = &luargs;
2730 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2731 nfs4_increment_lock_seqid(status, lsp);
2732 }
2733
2734 if (status == 0) {
2735 memcpy(&lsp->ls_stateid, &res.u.stateid,
2736 sizeof(lsp->ls_stateid));
2737 nfs4_notify_unlck(state, request, lsp);
2738 }
2739 nfs4_put_lock_state(lsp);
2740out:
2741 up(&state->lock_sema);
2742 if (status == 0)
2743 do_vfs_lock(request->fl_file, request);
2744 up_read(&clp->cl_sem);
2745 return status;
2746}
2747
2748static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
2749{
2750 struct nfs4_exception exception = { };
2751 int err;
2752
2753 do {
2754 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2755 _nfs4_proc_unlck(state, cmd, request),
2756 &exception);
2757 } while (exception.retry);
2758 return err;
2759}
2760
2761static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *request, int reclaim)
2762{
2763 struct inode *inode = state->inode;
2764 struct nfs_server *server = NFS_SERVER(inode);
2765 struct nfs4_lock_state *lsp;
2766 struct nfs_lockargs arg = {
2767 .fh = NFS_FH(inode),
2768 .type = nfs4_lck_type(cmd, request),
2769 .offset = request->fl_start,
2770 .length = nfs4_lck_length(request),
2771 };
2772 struct nfs_lockres res = {
2773 .server = server,
2774 };
2775 struct rpc_message msg = {
2776 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
2777 .rpc_argp = &arg,
2778 .rpc_resp = &res,
2779 .rpc_cred = state->owner->so_cred,
2780 };
2781 struct nfs_lock_opargs largs = {
2782 .reclaim = reclaim,
2783 .new_lock_owner = 0,
2784 };
2785 int status;
2786
2787 lsp = nfs4_get_lock_state(state, request->fl_owner);
2788 if (lsp == NULL)
2789 return -ENOMEM;
2790 if (!(lsp->ls_flags & NFS_LOCK_INITIALIZED)) {
2791 struct nfs4_state_owner *owner = state->owner;
2792 struct nfs_open_to_lock otl = {
2793 .lock_owner = {
2794 .clientid = server->nfs4_state->cl_clientid,
2795 },
2796 };
2797
2798 otl.lock_seqid = lsp->ls_seqid;
2799 otl.lock_owner.id = lsp->ls_id;
2800 memcpy(&otl.open_stateid, &state->stateid, sizeof(otl.open_stateid));
2801 largs.u.open_lock = &otl;
2802 largs.new_lock_owner = 1;
2803 arg.u.lock = &largs;
2804 down(&owner->so_sema);
2805 otl.open_seqid = owner->so_seqid;
2806 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2807 /* increment open_owner seqid on success, and
2808 * seqid mutating errors */
2809 nfs4_increment_seqid(status, owner);
2810 up(&owner->so_sema);
2811 } else {
2812 struct nfs_exist_lock el = {
2813 .seqid = lsp->ls_seqid,
2814 };
2815 memcpy(&el.stateid, &lsp->ls_stateid, sizeof(el.stateid));
2816 largs.u.exist_lock = &el;
2817 largs.new_lock_owner = 0;
2818 arg.u.lock = &largs;
2819 status = rpc_call_sync(server->client, &msg, RPC_TASK_NOINTR);
2820 }
2821 /* increment seqid on success, and * seqid mutating errors*/
2822 nfs4_increment_lock_seqid(status, lsp);
2823 /* save the returned stateid. */
2824 if (status == 0) {
2825 memcpy(&lsp->ls_stateid, &res.u.stateid, sizeof(nfs4_stateid));
2826 lsp->ls_flags |= NFS_LOCK_INITIALIZED;
2827 if (!reclaim)
2828 nfs4_notify_setlk(state, request, lsp);
2829 } else if (status == -NFS4ERR_DENIED)
2830 status = -EAGAIN;
2831 nfs4_put_lock_state(lsp);
2832 return status;
2833}
2834
2835static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
2836{
202b50dc
TM
2837 struct nfs_server *server = NFS_SERVER(state->inode);
2838 struct nfs4_exception exception = { };
2839 int err;
2840
2841 do {
2842 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
2843 if (err != -NFS4ERR_DELAY)
2844 break;
2845 nfs4_handle_exception(server, err, &exception);
2846 } while (exception.retry);
2847 return err;
1da177e4
LT
2848}
2849
2850static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
2851{
202b50dc
TM
2852 struct nfs_server *server = NFS_SERVER(state->inode);
2853 struct nfs4_exception exception = { };
2854 int err;
2855
2856 do {
2857 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
2858 if (err != -NFS4ERR_DELAY)
2859 break;
2860 nfs4_handle_exception(server, err, &exception);
2861 } while (exception.retry);
2862 return err;
1da177e4
LT
2863}
2864
2865static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2866{
2867 struct nfs4_client *clp = state->owner->so_client;
2868 int status;
2869
2870 down_read(&clp->cl_sem);
2871 down(&state->lock_sema);
2872 status = _nfs4_do_setlk(state, cmd, request, 0);
2873 up(&state->lock_sema);
2874 if (status == 0) {
2875 /* Note: we always want to sleep here! */
2876 request->fl_flags |= FL_SLEEP;
2877 if (do_vfs_lock(request->fl_file, request) < 0)
2878 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__);
2879 }
2880 up_read(&clp->cl_sem);
2881 return status;
2882}
2883
2884static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
2885{
2886 struct nfs4_exception exception = { };
2887 int err;
2888
2889 do {
2890 err = nfs4_handle_exception(NFS_SERVER(state->inode),
2891 _nfs4_proc_setlk(state, cmd, request),
2892 &exception);
2893 } while (exception.retry);
2894 return err;
2895}
2896
2897static int
2898nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
2899{
2900 struct nfs_open_context *ctx;
2901 struct nfs4_state *state;
2902 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
2903 int status;
2904
2905 /* verify open state */
2906 ctx = (struct nfs_open_context *)filp->private_data;
2907 state = ctx->state;
2908
2909 if (request->fl_start < 0 || request->fl_end < 0)
2910 return -EINVAL;
2911
2912 if (IS_GETLK(cmd))
2913 return nfs4_proc_getlk(state, F_GETLK, request);
2914
2915 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
2916 return -EINVAL;
2917
2918 if (request->fl_type == F_UNLCK)
2919 return nfs4_proc_unlck(state, cmd, request);
2920
2921 do {
2922 status = nfs4_proc_setlk(state, cmd, request);
2923 if ((status != -EAGAIN) || IS_SETLK(cmd))
2924 break;
2925 timeout = nfs4_set_lock_task_retry(timeout);
2926 status = -ERESTARTSYS;
2927 if (signalled())
2928 break;
2929 } while(status < 0);
2930
2931 return status;
2932}
2933
6b3b5496 2934
aa1870af
BF
2935#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
2936
6b3b5496
BF
2937int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
2938 size_t buflen, int flags)
2939{
4b580ee3
BF
2940 struct inode *inode = dentry->d_inode;
2941
2942 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
2943 return -EOPNOTSUPP;
2944
2945 if (!S_ISREG(inode->i_mode) &&
2946 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
2947 return -EPERM;
2948
2949 return nfs4_proc_set_acl(inode, buf, buflen);
6b3b5496
BF
2950}
2951
2952/* The getxattr man page suggests returning -ENODATA for unknown attributes,
2953 * and that's what we'll do for e.g. user attributes that haven't been set.
2954 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
2955 * attributes in kernel-managed attribute namespaces. */
2956ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
2957 size_t buflen)
2958{
aa1870af
BF
2959 struct inode *inode = dentry->d_inode;
2960
2961 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
2962 return -EOPNOTSUPP;
2963
2964 return nfs4_proc_get_acl(inode, buf, buflen);
6b3b5496
BF
2965}
2966
2967ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
2968{
aa1870af 2969 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
6b3b5496
BF
2970
2971 if (buf && buflen < len)
2972 return -ERANGE;
2973 if (buf)
aa1870af
BF
2974 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
2975 return len;
6b3b5496
BF
2976}
2977
1da177e4
LT
2978struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = {
2979 .recover_open = nfs4_open_reclaim,
2980 .recover_lock = nfs4_lock_reclaim,
2981};
2982
2983struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = {
2984 .recover_open = nfs4_open_expired,
2985 .recover_lock = nfs4_lock_expired,
2986};
2987
6b3b5496
BF
2988static struct inode_operations nfs4_file_inode_operations = {
2989 .permission = nfs_permission,
2990 .getattr = nfs_getattr,
2991 .setattr = nfs_setattr,
2992 .getxattr = nfs4_getxattr,
2993 .setxattr = nfs4_setxattr,
2994 .listxattr = nfs4_listxattr,
2995};
2996
1da177e4
LT
2997struct nfs_rpc_ops nfs_v4_clientops = {
2998 .version = 4, /* protocol version */
2999 .dentry_ops = &nfs4_dentry_operations,
3000 .dir_inode_ops = &nfs4_dir_inode_operations,
6b3b5496 3001 .file_inode_ops = &nfs4_file_inode_operations,
1da177e4
LT
3002 .getroot = nfs4_proc_get_root,
3003 .getattr = nfs4_proc_getattr,
3004 .setattr = nfs4_proc_setattr,
3005 .lookup = nfs4_proc_lookup,
3006 .access = nfs4_proc_access,
3007 .readlink = nfs4_proc_readlink,
3008 .read = nfs4_proc_read,
3009 .write = nfs4_proc_write,
3010 .commit = nfs4_proc_commit,
3011 .create = nfs4_proc_create,
3012 .remove = nfs4_proc_remove,
3013 .unlink_setup = nfs4_proc_unlink_setup,
3014 .unlink_done = nfs4_proc_unlink_done,
3015 .rename = nfs4_proc_rename,
3016 .link = nfs4_proc_link,
3017 .symlink = nfs4_proc_symlink,
3018 .mkdir = nfs4_proc_mkdir,
3019 .rmdir = nfs4_proc_remove,
3020 .readdir = nfs4_proc_readdir,
3021 .mknod = nfs4_proc_mknod,
3022 .statfs = nfs4_proc_statfs,
3023 .fsinfo = nfs4_proc_fsinfo,
3024 .pathconf = nfs4_proc_pathconf,
3025 .decode_dirent = nfs4_decode_dirent,
3026 .read_setup = nfs4_proc_read_setup,
3027 .write_setup = nfs4_proc_write_setup,
3028 .commit_setup = nfs4_proc_commit_setup,
3029 .file_open = nfs4_proc_file_open,
3030 .file_release = nfs4_proc_file_release,
3031 .lock = nfs4_proc_lock,
e50a1c2e 3032 .clear_acl_cache = nfs4_zap_acl_attr,
1da177e4
LT
3033};
3034
3035/*
3036 * Local variables:
3037 * c-basic-offset: 8
3038 * End:
3039 */