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