SUNRPC: Replace pointer values with task->tk_pid and rpc_clnt->cl_clid
[linux-2.6-block.git] / fs / nfs / nfs4proc.c
... / ...
CommitLineData
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/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
42#include <linux/ratelimit.h>
43#include <linux/printk.h>
44#include <linux/slab.h>
45#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
50#include <linux/nfs_mount.h>
51#include <linux/namei.h>
52#include <linux/mount.h>
53#include <linux/module.h>
54#include <linux/nfs_idmap.h>
55#include <linux/xattr.h>
56#include <linux/utsname.h>
57#include <linux/freezer.h>
58
59#include "nfs4_fs.h"
60#include "delegation.h"
61#include "internal.h"
62#include "iostat.h"
63#include "callback.h"
64#include "pnfs.h"
65#include "netns.h"
66#include "nfs4session.h"
67#include "fscache.h"
68
69#include "nfs4trace.h"
70
71#define NFSDBG_FACILITY NFSDBG_PROC
72
73#define NFS4_POLL_RETRY_MIN (HZ/10)
74#define NFS4_POLL_RETRY_MAX (15*HZ)
75
76struct nfs4_opendata;
77static int _nfs4_proc_open(struct nfs4_opendata *data);
78static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
79static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
80static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
81static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
82static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
84static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
86 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
88#ifdef CONFIG_NFS_V4_1
89static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
90 struct rpc_cred *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
92 struct rpc_cred *);
93#endif
94
95#ifdef CONFIG_NFS_V4_SECURITY_LABEL
96static inline struct nfs4_label *
97nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
99{
100 int err;
101
102 if (label == NULL)
103 return NULL;
104
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
106 return NULL;
107
108 if (NFS_SERVER(dir)->nfs_client->cl_minorversion < 2)
109 return NULL;
110
111 err = security_dentry_init_security(dentry, sattr->ia_mode,
112 &dentry->d_name, (void **)&label->label, &label->len);
113 if (err == 0)
114 return label;
115
116 return NULL;
117}
118static inline void
119nfs4_label_release_security(struct nfs4_label *label)
120{
121 if (label)
122 security_release_secctx(label->label, label->len);
123}
124static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
125{
126 if (label)
127 return server->attr_bitmask;
128
129 return server->attr_bitmask_nl;
130}
131#else
132static inline struct nfs4_label *
133nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
134 struct iattr *sattr, struct nfs4_label *l)
135{ return NULL; }
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{ return; }
139static inline u32 *
140nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
141{ return server->attr_bitmask; }
142#endif
143
144/* Prevent leaks of NFSv4 errors into userland */
145static int nfs4_map_errors(int err)
146{
147 if (err >= -1000)
148 return err;
149 switch (err) {
150 case -NFS4ERR_RESOURCE:
151 case -NFS4ERR_LAYOUTTRYLATER:
152 case -NFS4ERR_RECALLCONFLICT:
153 return -EREMOTEIO;
154 case -NFS4ERR_WRONGSEC:
155 return -EPERM;
156 case -NFS4ERR_BADOWNER:
157 case -NFS4ERR_BADNAME:
158 return -EINVAL;
159 case -NFS4ERR_SHARE_DENIED:
160 return -EACCES;
161 case -NFS4ERR_MINOR_VERS_MISMATCH:
162 return -EPROTONOSUPPORT;
163 case -NFS4ERR_ACCESS:
164 return -EACCES;
165 case -NFS4ERR_FILE_OPEN:
166 return -EBUSY;
167 default:
168 dprintk("%s could not handle NFSv4 error %d\n",
169 __func__, -err);
170 break;
171 }
172 return -EIO;
173}
174
175/*
176 * This is our standard bitmap for GETATTR requests.
177 */
178const u32 nfs4_fattr_bitmap[3] = {
179 FATTR4_WORD0_TYPE
180 | FATTR4_WORD0_CHANGE
181 | FATTR4_WORD0_SIZE
182 | FATTR4_WORD0_FSID
183 | FATTR4_WORD0_FILEID,
184 FATTR4_WORD1_MODE
185 | FATTR4_WORD1_NUMLINKS
186 | FATTR4_WORD1_OWNER
187 | FATTR4_WORD1_OWNER_GROUP
188 | FATTR4_WORD1_RAWDEV
189 | FATTR4_WORD1_SPACE_USED
190 | FATTR4_WORD1_TIME_ACCESS
191 | FATTR4_WORD1_TIME_METADATA
192 | FATTR4_WORD1_TIME_MODIFY,
193#ifdef CONFIG_NFS_V4_SECURITY_LABEL
194 FATTR4_WORD2_SECURITY_LABEL
195#endif
196};
197
198static const u32 nfs4_pnfs_open_bitmap[3] = {
199 FATTR4_WORD0_TYPE
200 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_SIZE
202 | FATTR4_WORD0_FSID
203 | FATTR4_WORD0_FILEID,
204 FATTR4_WORD1_MODE
205 | FATTR4_WORD1_NUMLINKS
206 | FATTR4_WORD1_OWNER
207 | FATTR4_WORD1_OWNER_GROUP
208 | FATTR4_WORD1_RAWDEV
209 | FATTR4_WORD1_SPACE_USED
210 | FATTR4_WORD1_TIME_ACCESS
211 | FATTR4_WORD1_TIME_METADATA
212 | FATTR4_WORD1_TIME_MODIFY,
213 FATTR4_WORD2_MDSTHRESHOLD
214};
215
216static const u32 nfs4_open_noattr_bitmap[3] = {
217 FATTR4_WORD0_TYPE
218 | FATTR4_WORD0_CHANGE
219 | FATTR4_WORD0_FILEID,
220};
221
222const u32 nfs4_statfs_bitmap[3] = {
223 FATTR4_WORD0_FILES_AVAIL
224 | FATTR4_WORD0_FILES_FREE
225 | FATTR4_WORD0_FILES_TOTAL,
226 FATTR4_WORD1_SPACE_AVAIL
227 | FATTR4_WORD1_SPACE_FREE
228 | FATTR4_WORD1_SPACE_TOTAL
229};
230
231const u32 nfs4_pathconf_bitmap[3] = {
232 FATTR4_WORD0_MAXLINK
233 | FATTR4_WORD0_MAXNAME,
234 0
235};
236
237const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
238 | FATTR4_WORD0_MAXREAD
239 | FATTR4_WORD0_MAXWRITE
240 | FATTR4_WORD0_LEASE_TIME,
241 FATTR4_WORD1_TIME_DELTA
242 | FATTR4_WORD1_FS_LAYOUT_TYPES,
243 FATTR4_WORD2_LAYOUT_BLKSIZE
244};
245
246const u32 nfs4_fs_locations_bitmap[3] = {
247 FATTR4_WORD0_TYPE
248 | FATTR4_WORD0_CHANGE
249 | FATTR4_WORD0_SIZE
250 | FATTR4_WORD0_FSID
251 | FATTR4_WORD0_FILEID
252 | FATTR4_WORD0_FS_LOCATIONS,
253 FATTR4_WORD1_MODE
254 | FATTR4_WORD1_NUMLINKS
255 | FATTR4_WORD1_OWNER
256 | FATTR4_WORD1_OWNER_GROUP
257 | FATTR4_WORD1_RAWDEV
258 | FATTR4_WORD1_SPACE_USED
259 | FATTR4_WORD1_TIME_ACCESS
260 | FATTR4_WORD1_TIME_METADATA
261 | FATTR4_WORD1_TIME_MODIFY
262 | FATTR4_WORD1_MOUNTED_ON_FILEID,
263};
264
265static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
266 struct nfs4_readdir_arg *readdir)
267{
268 __be32 *start, *p;
269
270 if (cookie > 2) {
271 readdir->cookie = cookie;
272 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
273 return;
274 }
275
276 readdir->cookie = 0;
277 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
278 if (cookie == 2)
279 return;
280
281 /*
282 * NFSv4 servers do not return entries for '.' and '..'
283 * Therefore, we fake these entries here. We let '.'
284 * have cookie 0 and '..' have cookie 1. Note that
285 * when talking to the server, we always send cookie 0
286 * instead of 1 or 2.
287 */
288 start = p = kmap_atomic(*readdir->pages);
289
290 if (cookie == 0) {
291 *p++ = xdr_one; /* next */
292 *p++ = xdr_zero; /* cookie, first word */
293 *p++ = xdr_one; /* cookie, second word */
294 *p++ = xdr_one; /* entry len */
295 memcpy(p, ".\0\0\0", 4); /* entry */
296 p++;
297 *p++ = xdr_one; /* bitmap length */
298 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
299 *p++ = htonl(8); /* attribute buffer length */
300 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
301 }
302
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_two; /* cookie, second word */
306 *p++ = xdr_two; /* entry len */
307 memcpy(p, "..\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
312 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
313
314 readdir->pgbase = (char *)p - (char *)start;
315 readdir->count -= readdir->pgbase;
316 kunmap_atomic(start);
317}
318
319static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
320{
321 int res = 0;
322
323 might_sleep();
324
325 if (*timeout <= 0)
326 *timeout = NFS4_POLL_RETRY_MIN;
327 if (*timeout > NFS4_POLL_RETRY_MAX)
328 *timeout = NFS4_POLL_RETRY_MAX;
329 freezable_schedule_timeout_killable_unsafe(*timeout);
330 if (fatal_signal_pending(current))
331 res = -ERESTARTSYS;
332 *timeout <<= 1;
333 return res;
334}
335
336/* This is the error handling routine for processes that are allowed
337 * to sleep.
338 */
339static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
340{
341 struct nfs_client *clp = server->nfs_client;
342 struct nfs4_state *state = exception->state;
343 struct inode *inode = exception->inode;
344 int ret = errorcode;
345
346 exception->retry = 0;
347 switch(errorcode) {
348 case 0:
349 return 0;
350 case -NFS4ERR_OPENMODE:
351 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
352 nfs4_inode_return_delegation(inode);
353 exception->retry = 1;
354 return 0;
355 }
356 if (state == NULL)
357 break;
358 ret = nfs4_schedule_stateid_recovery(server, state);
359 if (ret < 0)
360 break;
361 goto wait_on_recovery;
362 case -NFS4ERR_DELEG_REVOKED:
363 case -NFS4ERR_ADMIN_REVOKED:
364 case -NFS4ERR_BAD_STATEID:
365 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
366 nfs_remove_bad_delegation(inode);
367 exception->retry = 1;
368 break;
369 }
370 if (state == NULL)
371 break;
372 ret = nfs4_schedule_stateid_recovery(server, state);
373 if (ret < 0)
374 break;
375 goto wait_on_recovery;
376 case -NFS4ERR_EXPIRED:
377 if (state != NULL) {
378 ret = nfs4_schedule_stateid_recovery(server, state);
379 if (ret < 0)
380 break;
381 }
382 case -NFS4ERR_STALE_STATEID:
383 case -NFS4ERR_STALE_CLIENTID:
384 nfs4_schedule_lease_recovery(clp);
385 goto wait_on_recovery;
386#if defined(CONFIG_NFS_V4_1)
387 case -NFS4ERR_BADSESSION:
388 case -NFS4ERR_BADSLOT:
389 case -NFS4ERR_BAD_HIGH_SLOT:
390 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
391 case -NFS4ERR_DEADSESSION:
392 case -NFS4ERR_SEQ_FALSE_RETRY:
393 case -NFS4ERR_SEQ_MISORDERED:
394 dprintk("%s ERROR: %d Reset session\n", __func__,
395 errorcode);
396 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
397 goto wait_on_recovery;
398#endif /* defined(CONFIG_NFS_V4_1) */
399 case -NFS4ERR_FILE_OPEN:
400 if (exception->timeout > HZ) {
401 /* We have retried a decent amount, time to
402 * fail
403 */
404 ret = -EBUSY;
405 break;
406 }
407 case -NFS4ERR_GRACE:
408 case -NFS4ERR_DELAY:
409 ret = nfs4_delay(server->client, &exception->timeout);
410 if (ret != 0)
411 break;
412 case -NFS4ERR_RETRY_UNCACHED_REP:
413 case -NFS4ERR_OLD_STATEID:
414 exception->retry = 1;
415 break;
416 case -NFS4ERR_BADOWNER:
417 /* The following works around a Linux server bug! */
418 case -NFS4ERR_BADNAME:
419 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
420 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
421 exception->retry = 1;
422 printk(KERN_WARNING "NFS: v4 server %s "
423 "does not accept raw "
424 "uid/gids. "
425 "Reenabling the idmapper.\n",
426 server->nfs_client->cl_hostname);
427 }
428 }
429 /* We failed to handle the error */
430 return nfs4_map_errors(ret);
431wait_on_recovery:
432 ret = nfs4_wait_clnt_recover(clp);
433 if (ret == 0)
434 exception->retry = 1;
435 return ret;
436}
437
438/*
439 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
440 * or 'false' otherwise.
441 */
442static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
443{
444 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
445
446 if (flavor == RPC_AUTH_GSS_KRB5I ||
447 flavor == RPC_AUTH_GSS_KRB5P)
448 return true;
449
450 return false;
451}
452
453static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
454{
455 spin_lock(&clp->cl_lock);
456 if (time_before(clp->cl_last_renewal,timestamp))
457 clp->cl_last_renewal = timestamp;
458 spin_unlock(&clp->cl_lock);
459}
460
461static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
462{
463 do_renew_lease(server->nfs_client, timestamp);
464}
465
466struct nfs4_call_sync_data {
467 const struct nfs_server *seq_server;
468 struct nfs4_sequence_args *seq_args;
469 struct nfs4_sequence_res *seq_res;
470};
471
472static void nfs4_init_sequence(struct nfs4_sequence_args *args,
473 struct nfs4_sequence_res *res, int cache_reply)
474{
475 args->sa_slot = NULL;
476 args->sa_cache_this = cache_reply;
477 args->sa_privileged = 0;
478
479 res->sr_slot = NULL;
480}
481
482static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
483{
484 args->sa_privileged = 1;
485}
486
487static int nfs40_setup_sequence(const struct nfs_server *server,
488 struct nfs4_sequence_args *args,
489 struct nfs4_sequence_res *res,
490 struct rpc_task *task)
491{
492 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
493 struct nfs4_slot *slot;
494
495 /* slot already allocated? */
496 if (res->sr_slot != NULL)
497 goto out_start;
498
499 spin_lock(&tbl->slot_tbl_lock);
500 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
501 goto out_sleep;
502
503 slot = nfs4_alloc_slot(tbl);
504 if (IS_ERR(slot)) {
505 if (slot == ERR_PTR(-ENOMEM))
506 task->tk_timeout = HZ >> 2;
507 goto out_sleep;
508 }
509 spin_unlock(&tbl->slot_tbl_lock);
510
511 args->sa_slot = slot;
512 res->sr_slot = slot;
513
514out_start:
515 rpc_call_start(task);
516 return 0;
517
518out_sleep:
519 if (args->sa_privileged)
520 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
521 NULL, RPC_PRIORITY_PRIVILEGED);
522 else
523 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
524 spin_unlock(&tbl->slot_tbl_lock);
525 return -EAGAIN;
526}
527
528static int nfs40_sequence_done(struct rpc_task *task,
529 struct nfs4_sequence_res *res)
530{
531 struct nfs4_slot *slot = res->sr_slot;
532 struct nfs4_slot_table *tbl;
533
534 if (!RPC_WAS_SENT(task))
535 goto out;
536
537 tbl = slot->table;
538 spin_lock(&tbl->slot_tbl_lock);
539 if (!nfs41_wake_and_assign_slot(tbl, slot))
540 nfs4_free_slot(tbl, slot);
541 spin_unlock(&tbl->slot_tbl_lock);
542
543 res->sr_slot = NULL;
544out:
545 return 1;
546}
547
548#if defined(CONFIG_NFS_V4_1)
549
550static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
551{
552 struct nfs4_session *session;
553 struct nfs4_slot_table *tbl;
554 bool send_new_highest_used_slotid = false;
555
556 if (!res->sr_slot) {
557 /* just wake up the next guy waiting since
558 * we may have not consumed a slot after all */
559 dprintk("%s: No slot\n", __func__);
560 return;
561 }
562 tbl = res->sr_slot->table;
563 session = tbl->session;
564
565 spin_lock(&tbl->slot_tbl_lock);
566 /* Be nice to the server: try to ensure that the last transmitted
567 * value for highest_user_slotid <= target_highest_slotid
568 */
569 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
570 send_new_highest_used_slotid = true;
571
572 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
573 send_new_highest_used_slotid = false;
574 goto out_unlock;
575 }
576 nfs4_free_slot(tbl, res->sr_slot);
577
578 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
579 send_new_highest_used_slotid = false;
580out_unlock:
581 spin_unlock(&tbl->slot_tbl_lock);
582 res->sr_slot = NULL;
583 if (send_new_highest_used_slotid)
584 nfs41_server_notify_highest_slotid_update(session->clp);
585}
586
587static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
588{
589 struct nfs4_session *session;
590 struct nfs4_slot *slot;
591 struct nfs_client *clp;
592 bool interrupted = false;
593 int ret = 1;
594
595 /* don't increment the sequence number if the task wasn't sent */
596 if (!RPC_WAS_SENT(task))
597 goto out;
598
599 slot = res->sr_slot;
600 session = slot->table->session;
601
602 if (slot->interrupted) {
603 slot->interrupted = 0;
604 interrupted = true;
605 }
606
607 trace_nfs4_sequence_done(session, res);
608 /* Check the SEQUENCE operation status */
609 switch (res->sr_status) {
610 case 0:
611 /* Update the slot's sequence and clientid lease timer */
612 ++slot->seq_nr;
613 clp = session->clp;
614 do_renew_lease(clp, res->sr_timestamp);
615 /* Check sequence flags */
616 if (res->sr_status_flags != 0)
617 nfs4_schedule_lease_recovery(clp);
618 nfs41_update_target_slotid(slot->table, slot, res);
619 break;
620 case 1:
621 /*
622 * sr_status remains 1 if an RPC level error occurred.
623 * The server may or may not have processed the sequence
624 * operation..
625 * Mark the slot as having hosted an interrupted RPC call.
626 */
627 slot->interrupted = 1;
628 goto out;
629 case -NFS4ERR_DELAY:
630 /* The server detected a resend of the RPC call and
631 * returned NFS4ERR_DELAY as per Section 2.10.6.2
632 * of RFC5661.
633 */
634 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
635 __func__,
636 slot->slot_nr,
637 slot->seq_nr);
638 goto out_retry;
639 case -NFS4ERR_BADSLOT:
640 /*
641 * The slot id we used was probably retired. Try again
642 * using a different slot id.
643 */
644 goto retry_nowait;
645 case -NFS4ERR_SEQ_MISORDERED:
646 /*
647 * Was the last operation on this sequence interrupted?
648 * If so, retry after bumping the sequence number.
649 */
650 if (interrupted) {
651 ++slot->seq_nr;
652 goto retry_nowait;
653 }
654 /*
655 * Could this slot have been previously retired?
656 * If so, then the server may be expecting seq_nr = 1!
657 */
658 if (slot->seq_nr != 1) {
659 slot->seq_nr = 1;
660 goto retry_nowait;
661 }
662 break;
663 case -NFS4ERR_SEQ_FALSE_RETRY:
664 ++slot->seq_nr;
665 goto retry_nowait;
666 default:
667 /* Just update the slot sequence no. */
668 ++slot->seq_nr;
669 }
670out:
671 /* The session may be reset by one of the error handlers. */
672 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
673 nfs41_sequence_free_slot(res);
674 return ret;
675retry_nowait:
676 if (rpc_restart_call_prepare(task)) {
677 task->tk_status = 0;
678 ret = 0;
679 }
680 goto out;
681out_retry:
682 if (!rpc_restart_call(task))
683 goto out;
684 rpc_delay(task, NFS4_POLL_RETRY_MAX);
685 return 0;
686}
687
688static int nfs4_sequence_done(struct rpc_task *task,
689 struct nfs4_sequence_res *res)
690{
691 if (res->sr_slot == NULL)
692 return 1;
693 if (!res->sr_slot->table->session)
694 return nfs40_sequence_done(task, res);
695 return nfs41_sequence_done(task, res);
696}
697
698int nfs41_setup_sequence(struct nfs4_session *session,
699 struct nfs4_sequence_args *args,
700 struct nfs4_sequence_res *res,
701 struct rpc_task *task)
702{
703 struct nfs4_slot *slot;
704 struct nfs4_slot_table *tbl;
705
706 dprintk("--> %s\n", __func__);
707 /* slot already allocated? */
708 if (res->sr_slot != NULL)
709 goto out_success;
710
711 tbl = &session->fc_slot_table;
712
713 task->tk_timeout = 0;
714
715 spin_lock(&tbl->slot_tbl_lock);
716 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
717 !args->sa_privileged) {
718 /* The state manager will wait until the slot table is empty */
719 dprintk("%s session is draining\n", __func__);
720 goto out_sleep;
721 }
722
723 slot = nfs4_alloc_slot(tbl);
724 if (IS_ERR(slot)) {
725 /* If out of memory, try again in 1/4 second */
726 if (slot == ERR_PTR(-ENOMEM))
727 task->tk_timeout = HZ >> 2;
728 dprintk("<-- %s: no free slots\n", __func__);
729 goto out_sleep;
730 }
731 spin_unlock(&tbl->slot_tbl_lock);
732
733 args->sa_slot = slot;
734
735 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
736 slot->slot_nr, slot->seq_nr);
737
738 res->sr_slot = slot;
739 res->sr_timestamp = jiffies;
740 res->sr_status_flags = 0;
741 /*
742 * sr_status is only set in decode_sequence, and so will remain
743 * set to 1 if an rpc level failure occurs.
744 */
745 res->sr_status = 1;
746 trace_nfs4_setup_sequence(session, args);
747out_success:
748 rpc_call_start(task);
749 return 0;
750out_sleep:
751 /* Privileged tasks are queued with top priority */
752 if (args->sa_privileged)
753 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
754 NULL, RPC_PRIORITY_PRIVILEGED);
755 else
756 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
757 spin_unlock(&tbl->slot_tbl_lock);
758 return -EAGAIN;
759}
760EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
761
762static int nfs4_setup_sequence(const struct nfs_server *server,
763 struct nfs4_sequence_args *args,
764 struct nfs4_sequence_res *res,
765 struct rpc_task *task)
766{
767 struct nfs4_session *session = nfs4_get_session(server);
768 int ret = 0;
769
770 if (!session)
771 return nfs40_setup_sequence(server, args, res, task);
772
773 dprintk("--> %s clp %p session %p sr_slot %u\n",
774 __func__, session->clp, session, res->sr_slot ?
775 res->sr_slot->slot_nr : NFS4_NO_SLOT);
776
777 ret = nfs41_setup_sequence(session, args, res, task);
778
779 dprintk("<-- %s status=%d\n", __func__, ret);
780 return ret;
781}
782
783static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
784{
785 struct nfs4_call_sync_data *data = calldata;
786 struct nfs4_session *session = nfs4_get_session(data->seq_server);
787
788 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
789
790 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
791}
792
793static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
794{
795 struct nfs4_call_sync_data *data = calldata;
796
797 nfs41_sequence_done(task, data->seq_res);
798}
799
800static const struct rpc_call_ops nfs41_call_sync_ops = {
801 .rpc_call_prepare = nfs41_call_sync_prepare,
802 .rpc_call_done = nfs41_call_sync_done,
803};
804
805#else /* !CONFIG_NFS_V4_1 */
806
807static int nfs4_setup_sequence(const struct nfs_server *server,
808 struct nfs4_sequence_args *args,
809 struct nfs4_sequence_res *res,
810 struct rpc_task *task)
811{
812 return nfs40_setup_sequence(server, args, res, task);
813}
814
815static int nfs4_sequence_done(struct rpc_task *task,
816 struct nfs4_sequence_res *res)
817{
818 return nfs40_sequence_done(task, res);
819}
820
821#endif /* !CONFIG_NFS_V4_1 */
822
823static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
824{
825 struct nfs4_call_sync_data *data = calldata;
826 nfs4_setup_sequence(data->seq_server,
827 data->seq_args, data->seq_res, task);
828}
829
830static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
831{
832 struct nfs4_call_sync_data *data = calldata;
833 nfs4_sequence_done(task, data->seq_res);
834}
835
836static const struct rpc_call_ops nfs40_call_sync_ops = {
837 .rpc_call_prepare = nfs40_call_sync_prepare,
838 .rpc_call_done = nfs40_call_sync_done,
839};
840
841static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
842 struct nfs_server *server,
843 struct rpc_message *msg,
844 struct nfs4_sequence_args *args,
845 struct nfs4_sequence_res *res)
846{
847 int ret;
848 struct rpc_task *task;
849 struct nfs_client *clp = server->nfs_client;
850 struct nfs4_call_sync_data data = {
851 .seq_server = server,
852 .seq_args = args,
853 .seq_res = res,
854 };
855 struct rpc_task_setup task_setup = {
856 .rpc_client = clnt,
857 .rpc_message = msg,
858 .callback_ops = clp->cl_mvops->call_sync_ops,
859 .callback_data = &data
860 };
861
862 task = rpc_run_task(&task_setup);
863 if (IS_ERR(task))
864 ret = PTR_ERR(task);
865 else {
866 ret = task->tk_status;
867 rpc_put_task(task);
868 }
869 return ret;
870}
871
872static
873int nfs4_call_sync(struct rpc_clnt *clnt,
874 struct nfs_server *server,
875 struct rpc_message *msg,
876 struct nfs4_sequence_args *args,
877 struct nfs4_sequence_res *res,
878 int cache_reply)
879{
880 nfs4_init_sequence(args, res, cache_reply);
881 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
882}
883
884static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
885{
886 struct nfs_inode *nfsi = NFS_I(dir);
887
888 spin_lock(&dir->i_lock);
889 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
890 if (!cinfo->atomic || cinfo->before != dir->i_version)
891 nfs_force_lookup_revalidate(dir);
892 dir->i_version = cinfo->after;
893 nfs_fscache_invalidate(dir);
894 spin_unlock(&dir->i_lock);
895}
896
897struct nfs4_opendata {
898 struct kref kref;
899 struct nfs_openargs o_arg;
900 struct nfs_openres o_res;
901 struct nfs_open_confirmargs c_arg;
902 struct nfs_open_confirmres c_res;
903 struct nfs4_string owner_name;
904 struct nfs4_string group_name;
905 struct nfs_fattr f_attr;
906 struct nfs4_label *f_label;
907 struct dentry *dir;
908 struct dentry *dentry;
909 struct nfs4_state_owner *owner;
910 struct nfs4_state *state;
911 struct iattr attrs;
912 unsigned long timestamp;
913 unsigned int rpc_done : 1;
914 unsigned int is_recover : 1;
915 int rpc_status;
916 int cancelled;
917};
918
919static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
920 int err, struct nfs4_exception *exception)
921{
922 if (err != -EINVAL)
923 return false;
924 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
925 return false;
926 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
927 exception->retry = 1;
928 return true;
929}
930
931static enum open_claim_type4
932nfs4_map_atomic_open_claim(struct nfs_server *server,
933 enum open_claim_type4 claim)
934{
935 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
936 return claim;
937 switch (claim) {
938 default:
939 return claim;
940 case NFS4_OPEN_CLAIM_FH:
941 return NFS4_OPEN_CLAIM_NULL;
942 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
943 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
944 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
945 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
946 }
947}
948
949static void nfs4_init_opendata_res(struct nfs4_opendata *p)
950{
951 p->o_res.f_attr = &p->f_attr;
952 p->o_res.f_label = p->f_label;
953 p->o_res.seqid = p->o_arg.seqid;
954 p->c_res.seqid = p->c_arg.seqid;
955 p->o_res.server = p->o_arg.server;
956 p->o_res.access_request = p->o_arg.access;
957 nfs_fattr_init(&p->f_attr);
958 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
959}
960
961static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
962 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
963 const struct iattr *attrs,
964 struct nfs4_label *label,
965 enum open_claim_type4 claim,
966 gfp_t gfp_mask)
967{
968 struct dentry *parent = dget_parent(dentry);
969 struct inode *dir = parent->d_inode;
970 struct nfs_server *server = NFS_SERVER(dir);
971 struct nfs4_opendata *p;
972
973 p = kzalloc(sizeof(*p), gfp_mask);
974 if (p == NULL)
975 goto err;
976
977 p->f_label = nfs4_label_alloc(server, gfp_mask);
978 if (IS_ERR(p->f_label))
979 goto err_free_p;
980
981 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
982 if (p->o_arg.seqid == NULL)
983 goto err_free_label;
984 nfs_sb_active(dentry->d_sb);
985 p->dentry = dget(dentry);
986 p->dir = parent;
987 p->owner = sp;
988 atomic_inc(&sp->so_count);
989 p->o_arg.open_flags = flags;
990 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
991 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
992 * will return permission denied for all bits until close */
993 if (!(flags & O_EXCL)) {
994 /* ask server to check for all possible rights as results
995 * are cached */
996 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
997 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
998 }
999 p->o_arg.clientid = server->nfs_client->cl_clientid;
1000 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1001 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1002 p->o_arg.name = &dentry->d_name;
1003 p->o_arg.server = server;
1004 p->o_arg.bitmask = nfs4_bitmask(server, label);
1005 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1006 p->o_arg.label = label;
1007 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1008 switch (p->o_arg.claim) {
1009 case NFS4_OPEN_CLAIM_NULL:
1010 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1011 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1012 p->o_arg.fh = NFS_FH(dir);
1013 break;
1014 case NFS4_OPEN_CLAIM_PREVIOUS:
1015 case NFS4_OPEN_CLAIM_FH:
1016 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1017 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1018 p->o_arg.fh = NFS_FH(dentry->d_inode);
1019 }
1020 if (attrs != NULL && attrs->ia_valid != 0) {
1021 __u32 verf[2];
1022
1023 p->o_arg.u.attrs = &p->attrs;
1024 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1025
1026 verf[0] = jiffies;
1027 verf[1] = current->pid;
1028 memcpy(p->o_arg.u.verifier.data, verf,
1029 sizeof(p->o_arg.u.verifier.data));
1030 }
1031 p->c_arg.fh = &p->o_res.fh;
1032 p->c_arg.stateid = &p->o_res.stateid;
1033 p->c_arg.seqid = p->o_arg.seqid;
1034 nfs4_init_opendata_res(p);
1035 kref_init(&p->kref);
1036 return p;
1037
1038err_free_label:
1039 nfs4_label_free(p->f_label);
1040err_free_p:
1041 kfree(p);
1042err:
1043 dput(parent);
1044 return NULL;
1045}
1046
1047static void nfs4_opendata_free(struct kref *kref)
1048{
1049 struct nfs4_opendata *p = container_of(kref,
1050 struct nfs4_opendata, kref);
1051 struct super_block *sb = p->dentry->d_sb;
1052
1053 nfs_free_seqid(p->o_arg.seqid);
1054 if (p->state != NULL)
1055 nfs4_put_open_state(p->state);
1056 nfs4_put_state_owner(p->owner);
1057
1058 nfs4_label_free(p->f_label);
1059
1060 dput(p->dir);
1061 dput(p->dentry);
1062 nfs_sb_deactive(sb);
1063 nfs_fattr_free_names(&p->f_attr);
1064 kfree(p);
1065}
1066
1067static void nfs4_opendata_put(struct nfs4_opendata *p)
1068{
1069 if (p != NULL)
1070 kref_put(&p->kref, nfs4_opendata_free);
1071}
1072
1073static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1074{
1075 int ret;
1076
1077 ret = rpc_wait_for_completion_task(task);
1078 return ret;
1079}
1080
1081static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1082{
1083 int ret = 0;
1084
1085 if (open_mode & (O_EXCL|O_TRUNC))
1086 goto out;
1087 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1088 case FMODE_READ:
1089 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1090 && state->n_rdonly != 0;
1091 break;
1092 case FMODE_WRITE:
1093 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1094 && state->n_wronly != 0;
1095 break;
1096 case FMODE_READ|FMODE_WRITE:
1097 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1098 && state->n_rdwr != 0;
1099 }
1100out:
1101 return ret;
1102}
1103
1104static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
1105{
1106 if (delegation == NULL)
1107 return 0;
1108 if ((delegation->type & fmode) != fmode)
1109 return 0;
1110 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1111 return 0;
1112 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1113 return 0;
1114 nfs_mark_delegation_referenced(delegation);
1115 return 1;
1116}
1117
1118static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1119{
1120 switch (fmode) {
1121 case FMODE_WRITE:
1122 state->n_wronly++;
1123 break;
1124 case FMODE_READ:
1125 state->n_rdonly++;
1126 break;
1127 case FMODE_READ|FMODE_WRITE:
1128 state->n_rdwr++;
1129 }
1130 nfs4_state_set_mode_locked(state, state->state | fmode);
1131}
1132
1133static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1134{
1135 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1136 nfs4_stateid_copy(&state->stateid, stateid);
1137 nfs4_stateid_copy(&state->open_stateid, stateid);
1138 set_bit(NFS_OPEN_STATE, &state->flags);
1139 switch (fmode) {
1140 case FMODE_READ:
1141 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1142 break;
1143 case FMODE_WRITE:
1144 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1145 break;
1146 case FMODE_READ|FMODE_WRITE:
1147 set_bit(NFS_O_RDWR_STATE, &state->flags);
1148 }
1149}
1150
1151static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1152{
1153 write_seqlock(&state->seqlock);
1154 nfs_set_open_stateid_locked(state, stateid, fmode);
1155 write_sequnlock(&state->seqlock);
1156}
1157
1158static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1159{
1160 /*
1161 * Protect the call to nfs4_state_set_mode_locked and
1162 * serialise the stateid update
1163 */
1164 write_seqlock(&state->seqlock);
1165 if (deleg_stateid != NULL) {
1166 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1167 set_bit(NFS_DELEGATED_STATE, &state->flags);
1168 }
1169 if (open_stateid != NULL)
1170 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1171 write_sequnlock(&state->seqlock);
1172 spin_lock(&state->owner->so_lock);
1173 update_open_stateflags(state, fmode);
1174 spin_unlock(&state->owner->so_lock);
1175}
1176
1177static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1178{
1179 struct nfs_inode *nfsi = NFS_I(state->inode);
1180 struct nfs_delegation *deleg_cur;
1181 int ret = 0;
1182
1183 fmode &= (FMODE_READ|FMODE_WRITE);
1184
1185 rcu_read_lock();
1186 deleg_cur = rcu_dereference(nfsi->delegation);
1187 if (deleg_cur == NULL)
1188 goto no_delegation;
1189
1190 spin_lock(&deleg_cur->lock);
1191 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1192 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1193 (deleg_cur->type & fmode) != fmode)
1194 goto no_delegation_unlock;
1195
1196 if (delegation == NULL)
1197 delegation = &deleg_cur->stateid;
1198 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1199 goto no_delegation_unlock;
1200
1201 nfs_mark_delegation_referenced(deleg_cur);
1202 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1203 ret = 1;
1204no_delegation_unlock:
1205 spin_unlock(&deleg_cur->lock);
1206no_delegation:
1207 rcu_read_unlock();
1208
1209 if (!ret && open_stateid != NULL) {
1210 __update_open_stateid(state, open_stateid, NULL, fmode);
1211 ret = 1;
1212 }
1213
1214 return ret;
1215}
1216
1217
1218static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1219{
1220 struct nfs_delegation *delegation;
1221
1222 rcu_read_lock();
1223 delegation = rcu_dereference(NFS_I(inode)->delegation);
1224 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1225 rcu_read_unlock();
1226 return;
1227 }
1228 rcu_read_unlock();
1229 nfs4_inode_return_delegation(inode);
1230}
1231
1232static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1233{
1234 struct nfs4_state *state = opendata->state;
1235 struct nfs_inode *nfsi = NFS_I(state->inode);
1236 struct nfs_delegation *delegation;
1237 int open_mode = opendata->o_arg.open_flags;
1238 fmode_t fmode = opendata->o_arg.fmode;
1239 nfs4_stateid stateid;
1240 int ret = -EAGAIN;
1241
1242 for (;;) {
1243 if (can_open_cached(state, fmode, open_mode)) {
1244 spin_lock(&state->owner->so_lock);
1245 if (can_open_cached(state, fmode, open_mode)) {
1246 update_open_stateflags(state, fmode);
1247 spin_unlock(&state->owner->so_lock);
1248 goto out_return_state;
1249 }
1250 spin_unlock(&state->owner->so_lock);
1251 }
1252 rcu_read_lock();
1253 delegation = rcu_dereference(nfsi->delegation);
1254 if (!can_open_delegated(delegation, fmode)) {
1255 rcu_read_unlock();
1256 break;
1257 }
1258 /* Save the delegation */
1259 nfs4_stateid_copy(&stateid, &delegation->stateid);
1260 rcu_read_unlock();
1261 nfs_release_seqid(opendata->o_arg.seqid);
1262 if (!opendata->is_recover) {
1263 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1264 if (ret != 0)
1265 goto out;
1266 }
1267 ret = -EAGAIN;
1268
1269 /* Try to update the stateid using the delegation */
1270 if (update_open_stateid(state, NULL, &stateid, fmode))
1271 goto out_return_state;
1272 }
1273out:
1274 return ERR_PTR(ret);
1275out_return_state:
1276 atomic_inc(&state->count);
1277 return state;
1278}
1279
1280static void
1281nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1282{
1283 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1284 struct nfs_delegation *delegation;
1285 int delegation_flags = 0;
1286
1287 rcu_read_lock();
1288 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1289 if (delegation)
1290 delegation_flags = delegation->flags;
1291 rcu_read_unlock();
1292 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1293 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1294 "returning a delegation for "
1295 "OPEN(CLAIM_DELEGATE_CUR)\n",
1296 clp->cl_hostname);
1297 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1298 nfs_inode_set_delegation(state->inode,
1299 data->owner->so_cred,
1300 &data->o_res);
1301 else
1302 nfs_inode_reclaim_delegation(state->inode,
1303 data->owner->so_cred,
1304 &data->o_res);
1305}
1306
1307/*
1308 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1309 * and update the nfs4_state.
1310 */
1311static struct nfs4_state *
1312_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1313{
1314 struct inode *inode = data->state->inode;
1315 struct nfs4_state *state = data->state;
1316 int ret;
1317
1318 if (!data->rpc_done) {
1319 ret = data->rpc_status;
1320 goto err;
1321 }
1322
1323 ret = -ESTALE;
1324 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1325 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1326 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1327 goto err;
1328
1329 ret = -ENOMEM;
1330 state = nfs4_get_open_state(inode, data->owner);
1331 if (state == NULL)
1332 goto err;
1333
1334 ret = nfs_refresh_inode(inode, &data->f_attr);
1335 if (ret)
1336 goto err;
1337
1338 nfs_setsecurity(inode, &data->f_attr, data->f_label);
1339
1340 if (data->o_res.delegation_type != 0)
1341 nfs4_opendata_check_deleg(data, state);
1342 update_open_stateid(state, &data->o_res.stateid, NULL,
1343 data->o_arg.fmode);
1344
1345 return state;
1346err:
1347 return ERR_PTR(ret);
1348
1349}
1350
1351static struct nfs4_state *
1352_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1353{
1354 struct inode *inode;
1355 struct nfs4_state *state = NULL;
1356 int ret;
1357
1358 if (!data->rpc_done) {
1359 state = nfs4_try_open_cached(data);
1360 goto out;
1361 }
1362
1363 ret = -EAGAIN;
1364 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1365 goto err;
1366 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1367 ret = PTR_ERR(inode);
1368 if (IS_ERR(inode))
1369 goto err;
1370 ret = -ENOMEM;
1371 state = nfs4_get_open_state(inode, data->owner);
1372 if (state == NULL)
1373 goto err_put_inode;
1374 if (data->o_res.delegation_type != 0)
1375 nfs4_opendata_check_deleg(data, state);
1376 update_open_stateid(state, &data->o_res.stateid, NULL,
1377 data->o_arg.fmode);
1378 iput(inode);
1379out:
1380 nfs_release_seqid(data->o_arg.seqid);
1381 return state;
1382err_put_inode:
1383 iput(inode);
1384err:
1385 return ERR_PTR(ret);
1386}
1387
1388static struct nfs4_state *
1389nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1390{
1391 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1392 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1393 return _nfs4_opendata_to_nfs4_state(data);
1394}
1395
1396static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1397{
1398 struct nfs_inode *nfsi = NFS_I(state->inode);
1399 struct nfs_open_context *ctx;
1400
1401 spin_lock(&state->inode->i_lock);
1402 list_for_each_entry(ctx, &nfsi->open_files, list) {
1403 if (ctx->state != state)
1404 continue;
1405 get_nfs_open_context(ctx);
1406 spin_unlock(&state->inode->i_lock);
1407 return ctx;
1408 }
1409 spin_unlock(&state->inode->i_lock);
1410 return ERR_PTR(-ENOENT);
1411}
1412
1413static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1414 struct nfs4_state *state, enum open_claim_type4 claim)
1415{
1416 struct nfs4_opendata *opendata;
1417
1418 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1419 NULL, NULL, claim, GFP_NOFS);
1420 if (opendata == NULL)
1421 return ERR_PTR(-ENOMEM);
1422 opendata->state = state;
1423 atomic_inc(&state->count);
1424 return opendata;
1425}
1426
1427static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1428{
1429 struct nfs4_state *newstate;
1430 int ret;
1431
1432 opendata->o_arg.open_flags = 0;
1433 opendata->o_arg.fmode = fmode;
1434 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1435 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1436 nfs4_init_opendata_res(opendata);
1437 ret = _nfs4_recover_proc_open(opendata);
1438 if (ret != 0)
1439 return ret;
1440 newstate = nfs4_opendata_to_nfs4_state(opendata);
1441 if (IS_ERR(newstate))
1442 return PTR_ERR(newstate);
1443 nfs4_close_state(newstate, fmode);
1444 *res = newstate;
1445 return 0;
1446}
1447
1448static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1449{
1450 struct nfs4_state *newstate;
1451 int ret;
1452
1453 /* memory barrier prior to reading state->n_* */
1454 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1455 clear_bit(NFS_OPEN_STATE, &state->flags);
1456 smp_rmb();
1457 if (state->n_rdwr != 0) {
1458 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1459 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1460 if (ret != 0)
1461 return ret;
1462 if (newstate != state)
1463 return -ESTALE;
1464 }
1465 if (state->n_wronly != 0) {
1466 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1467 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1468 if (ret != 0)
1469 return ret;
1470 if (newstate != state)
1471 return -ESTALE;
1472 }
1473 if (state->n_rdonly != 0) {
1474 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1475 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1476 if (ret != 0)
1477 return ret;
1478 if (newstate != state)
1479 return -ESTALE;
1480 }
1481 /*
1482 * We may have performed cached opens for all three recoveries.
1483 * Check if we need to update the current stateid.
1484 */
1485 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1486 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1487 write_seqlock(&state->seqlock);
1488 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1489 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1490 write_sequnlock(&state->seqlock);
1491 }
1492 return 0;
1493}
1494
1495/*
1496 * OPEN_RECLAIM:
1497 * reclaim state on the server after a reboot.
1498 */
1499static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1500{
1501 struct nfs_delegation *delegation;
1502 struct nfs4_opendata *opendata;
1503 fmode_t delegation_type = 0;
1504 int status;
1505
1506 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1507 NFS4_OPEN_CLAIM_PREVIOUS);
1508 if (IS_ERR(opendata))
1509 return PTR_ERR(opendata);
1510 rcu_read_lock();
1511 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1512 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1513 delegation_type = delegation->type;
1514 rcu_read_unlock();
1515 opendata->o_arg.u.delegation_type = delegation_type;
1516 status = nfs4_open_recover(opendata, state);
1517 nfs4_opendata_put(opendata);
1518 return status;
1519}
1520
1521static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1522{
1523 struct nfs_server *server = NFS_SERVER(state->inode);
1524 struct nfs4_exception exception = { };
1525 int err;
1526 do {
1527 err = _nfs4_do_open_reclaim(ctx, state);
1528 trace_nfs4_open_reclaim(ctx, 0, err);
1529 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1530 continue;
1531 if (err != -NFS4ERR_DELAY)
1532 break;
1533 nfs4_handle_exception(server, err, &exception);
1534 } while (exception.retry);
1535 return err;
1536}
1537
1538static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1539{
1540 struct nfs_open_context *ctx;
1541 int ret;
1542
1543 ctx = nfs4_state_find_open_context(state);
1544 if (IS_ERR(ctx))
1545 return -EAGAIN;
1546 ret = nfs4_do_open_reclaim(ctx, state);
1547 put_nfs_open_context(ctx);
1548 return ret;
1549}
1550
1551static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1552{
1553 switch (err) {
1554 default:
1555 printk(KERN_ERR "NFS: %s: unhandled error "
1556 "%d.\n", __func__, err);
1557 case 0:
1558 case -ENOENT:
1559 case -ESTALE:
1560 break;
1561 case -NFS4ERR_BADSESSION:
1562 case -NFS4ERR_BADSLOT:
1563 case -NFS4ERR_BAD_HIGH_SLOT:
1564 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1565 case -NFS4ERR_DEADSESSION:
1566 set_bit(NFS_DELEGATED_STATE, &state->flags);
1567 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1568 return -EAGAIN;
1569 case -NFS4ERR_STALE_CLIENTID:
1570 case -NFS4ERR_STALE_STATEID:
1571 set_bit(NFS_DELEGATED_STATE, &state->flags);
1572 case -NFS4ERR_EXPIRED:
1573 /* Don't recall a delegation if it was lost */
1574 nfs4_schedule_lease_recovery(server->nfs_client);
1575 return -EAGAIN;
1576 case -NFS4ERR_DELEG_REVOKED:
1577 case -NFS4ERR_ADMIN_REVOKED:
1578 case -NFS4ERR_BAD_STATEID:
1579 case -NFS4ERR_OPENMODE:
1580 nfs_inode_find_state_and_recover(state->inode,
1581 stateid);
1582 nfs4_schedule_stateid_recovery(server, state);
1583 return 0;
1584 case -NFS4ERR_DELAY:
1585 case -NFS4ERR_GRACE:
1586 set_bit(NFS_DELEGATED_STATE, &state->flags);
1587 ssleep(1);
1588 return -EAGAIN;
1589 case -ENOMEM:
1590 case -NFS4ERR_DENIED:
1591 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1592 return 0;
1593 }
1594 return err;
1595}
1596
1597int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1598{
1599 struct nfs_server *server = NFS_SERVER(state->inode);
1600 struct nfs4_opendata *opendata;
1601 int err;
1602
1603 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1604 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1605 if (IS_ERR(opendata))
1606 return PTR_ERR(opendata);
1607 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1608 err = nfs4_open_recover(opendata, state);
1609 nfs4_opendata_put(opendata);
1610 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1611}
1612
1613static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1614{
1615 struct nfs4_opendata *data = calldata;
1616
1617 nfs40_setup_sequence(data->o_arg.server, &data->o_arg.seq_args,
1618 &data->o_res.seq_res, task);
1619}
1620
1621static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1622{
1623 struct nfs4_opendata *data = calldata;
1624
1625 nfs40_sequence_done(task, &data->o_res.seq_res);
1626
1627 data->rpc_status = task->tk_status;
1628 if (data->rpc_status == 0) {
1629 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1630 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1631 renew_lease(data->o_res.server, data->timestamp);
1632 data->rpc_done = 1;
1633 }
1634}
1635
1636static void nfs4_open_confirm_release(void *calldata)
1637{
1638 struct nfs4_opendata *data = calldata;
1639 struct nfs4_state *state = NULL;
1640
1641 /* If this request hasn't been cancelled, do nothing */
1642 if (data->cancelled == 0)
1643 goto out_free;
1644 /* In case of error, no cleanup! */
1645 if (!data->rpc_done)
1646 goto out_free;
1647 state = nfs4_opendata_to_nfs4_state(data);
1648 if (!IS_ERR(state))
1649 nfs4_close_state(state, data->o_arg.fmode);
1650out_free:
1651 nfs4_opendata_put(data);
1652}
1653
1654static const struct rpc_call_ops nfs4_open_confirm_ops = {
1655 .rpc_call_prepare = nfs4_open_confirm_prepare,
1656 .rpc_call_done = nfs4_open_confirm_done,
1657 .rpc_release = nfs4_open_confirm_release,
1658};
1659
1660/*
1661 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1662 */
1663static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1664{
1665 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1666 struct rpc_task *task;
1667 struct rpc_message msg = {
1668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1669 .rpc_argp = &data->c_arg,
1670 .rpc_resp = &data->c_res,
1671 .rpc_cred = data->owner->so_cred,
1672 };
1673 struct rpc_task_setup task_setup_data = {
1674 .rpc_client = server->client,
1675 .rpc_message = &msg,
1676 .callback_ops = &nfs4_open_confirm_ops,
1677 .callback_data = data,
1678 .workqueue = nfsiod_workqueue,
1679 .flags = RPC_TASK_ASYNC,
1680 };
1681 int status;
1682
1683 nfs4_init_sequence(&data->o_arg.seq_args, &data->o_res.seq_res, 1);
1684 kref_get(&data->kref);
1685 data->rpc_done = 0;
1686 data->rpc_status = 0;
1687 data->timestamp = jiffies;
1688 task = rpc_run_task(&task_setup_data);
1689 if (IS_ERR(task))
1690 return PTR_ERR(task);
1691 status = nfs4_wait_for_completion_rpc_task(task);
1692 if (status != 0) {
1693 data->cancelled = 1;
1694 smp_wmb();
1695 } else
1696 status = data->rpc_status;
1697 rpc_put_task(task);
1698 return status;
1699}
1700
1701static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1702{
1703 struct nfs4_opendata *data = calldata;
1704 struct nfs4_state_owner *sp = data->owner;
1705 struct nfs_client *clp = sp->so_server->nfs_client;
1706
1707 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1708 goto out_wait;
1709 /*
1710 * Check if we still need to send an OPEN call, or if we can use
1711 * a delegation instead.
1712 */
1713 if (data->state != NULL) {
1714 struct nfs_delegation *delegation;
1715
1716 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1717 goto out_no_action;
1718 rcu_read_lock();
1719 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1720 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1721 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
1722 can_open_delegated(delegation, data->o_arg.fmode))
1723 goto unlock_no_action;
1724 rcu_read_unlock();
1725 }
1726 /* Update client id. */
1727 data->o_arg.clientid = clp->cl_clientid;
1728 switch (data->o_arg.claim) {
1729 case NFS4_OPEN_CLAIM_PREVIOUS:
1730 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1731 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1732 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1733 case NFS4_OPEN_CLAIM_FH:
1734 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1735 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1736 }
1737 data->timestamp = jiffies;
1738 if (nfs4_setup_sequence(data->o_arg.server,
1739 &data->o_arg.seq_args,
1740 &data->o_res.seq_res,
1741 task) != 0)
1742 nfs_release_seqid(data->o_arg.seqid);
1743
1744 /* Set the create mode (note dependency on the session type) */
1745 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1746 if (data->o_arg.open_flags & O_EXCL) {
1747 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1748 if (nfs4_has_persistent_session(clp))
1749 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1750 else if (clp->cl_mvops->minor_version > 0)
1751 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1752 }
1753 return;
1754unlock_no_action:
1755 rcu_read_unlock();
1756out_no_action:
1757 task->tk_action = NULL;
1758out_wait:
1759 nfs4_sequence_done(task, &data->o_res.seq_res);
1760}
1761
1762static void nfs4_open_done(struct rpc_task *task, void *calldata)
1763{
1764 struct nfs4_opendata *data = calldata;
1765
1766 data->rpc_status = task->tk_status;
1767
1768 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1769 return;
1770
1771 if (task->tk_status == 0) {
1772 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1773 switch (data->o_res.f_attr->mode & S_IFMT) {
1774 case S_IFREG:
1775 break;
1776 case S_IFLNK:
1777 data->rpc_status = -ELOOP;
1778 break;
1779 case S_IFDIR:
1780 data->rpc_status = -EISDIR;
1781 break;
1782 default:
1783 data->rpc_status = -ENOTDIR;
1784 }
1785 }
1786 renew_lease(data->o_res.server, data->timestamp);
1787 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1788 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1789 }
1790 data->rpc_done = 1;
1791}
1792
1793static void nfs4_open_release(void *calldata)
1794{
1795 struct nfs4_opendata *data = calldata;
1796 struct nfs4_state *state = NULL;
1797
1798 /* If this request hasn't been cancelled, do nothing */
1799 if (data->cancelled == 0)
1800 goto out_free;
1801 /* In case of error, no cleanup! */
1802 if (data->rpc_status != 0 || !data->rpc_done)
1803 goto out_free;
1804 /* In case we need an open_confirm, no cleanup! */
1805 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1806 goto out_free;
1807 state = nfs4_opendata_to_nfs4_state(data);
1808 if (!IS_ERR(state))
1809 nfs4_close_state(state, data->o_arg.fmode);
1810out_free:
1811 nfs4_opendata_put(data);
1812}
1813
1814static const struct rpc_call_ops nfs4_open_ops = {
1815 .rpc_call_prepare = nfs4_open_prepare,
1816 .rpc_call_done = nfs4_open_done,
1817 .rpc_release = nfs4_open_release,
1818};
1819
1820static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1821{
1822 struct inode *dir = data->dir->d_inode;
1823 struct nfs_server *server = NFS_SERVER(dir);
1824 struct nfs_openargs *o_arg = &data->o_arg;
1825 struct nfs_openres *o_res = &data->o_res;
1826 struct rpc_task *task;
1827 struct rpc_message msg = {
1828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1829 .rpc_argp = o_arg,
1830 .rpc_resp = o_res,
1831 .rpc_cred = data->owner->so_cred,
1832 };
1833 struct rpc_task_setup task_setup_data = {
1834 .rpc_client = server->client,
1835 .rpc_message = &msg,
1836 .callback_ops = &nfs4_open_ops,
1837 .callback_data = data,
1838 .workqueue = nfsiod_workqueue,
1839 .flags = RPC_TASK_ASYNC,
1840 };
1841 int status;
1842
1843 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1844 kref_get(&data->kref);
1845 data->rpc_done = 0;
1846 data->rpc_status = 0;
1847 data->cancelled = 0;
1848 data->is_recover = 0;
1849 if (isrecover) {
1850 nfs4_set_sequence_privileged(&o_arg->seq_args);
1851 data->is_recover = 1;
1852 }
1853 task = rpc_run_task(&task_setup_data);
1854 if (IS_ERR(task))
1855 return PTR_ERR(task);
1856 status = nfs4_wait_for_completion_rpc_task(task);
1857 if (status != 0) {
1858 data->cancelled = 1;
1859 smp_wmb();
1860 } else
1861 status = data->rpc_status;
1862 rpc_put_task(task);
1863
1864 return status;
1865}
1866
1867static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1868{
1869 struct inode *dir = data->dir->d_inode;
1870 struct nfs_openres *o_res = &data->o_res;
1871 int status;
1872
1873 status = nfs4_run_open_task(data, 1);
1874 if (status != 0 || !data->rpc_done)
1875 return status;
1876
1877 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1878
1879 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1880 status = _nfs4_proc_open_confirm(data);
1881 if (status != 0)
1882 return status;
1883 }
1884
1885 return status;
1886}
1887
1888static int nfs4_opendata_access(struct rpc_cred *cred,
1889 struct nfs4_opendata *opendata,
1890 struct nfs4_state *state, fmode_t fmode,
1891 int openflags)
1892{
1893 struct nfs_access_entry cache;
1894 u32 mask;
1895
1896 /* access call failed or for some reason the server doesn't
1897 * support any access modes -- defer access call until later */
1898 if (opendata->o_res.access_supported == 0)
1899 return 0;
1900
1901 mask = 0;
1902 /* don't check MAY_WRITE - a newly created file may not have
1903 * write mode bits, but POSIX allows the creating process to write.
1904 * use openflags to check for exec, because fmode won't
1905 * always have FMODE_EXEC set when file open for exec. */
1906 if (openflags & __FMODE_EXEC) {
1907 /* ONLY check for exec rights */
1908 mask = MAY_EXEC;
1909 } else if (fmode & FMODE_READ)
1910 mask = MAY_READ;
1911
1912 cache.cred = cred;
1913 cache.jiffies = jiffies;
1914 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1915 nfs_access_add_cache(state->inode, &cache);
1916
1917 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1918 return 0;
1919
1920 /* even though OPEN succeeded, access is denied. Close the file */
1921 nfs4_close_state(state, fmode);
1922 return -EACCES;
1923}
1924
1925/*
1926 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1927 */
1928static int _nfs4_proc_open(struct nfs4_opendata *data)
1929{
1930 struct inode *dir = data->dir->d_inode;
1931 struct nfs_server *server = NFS_SERVER(dir);
1932 struct nfs_openargs *o_arg = &data->o_arg;
1933 struct nfs_openres *o_res = &data->o_res;
1934 int status;
1935
1936 status = nfs4_run_open_task(data, 0);
1937 if (!data->rpc_done)
1938 return status;
1939 if (status != 0) {
1940 if (status == -NFS4ERR_BADNAME &&
1941 !(o_arg->open_flags & O_CREAT))
1942 return -ENOENT;
1943 return status;
1944 }
1945
1946 nfs_fattr_map_and_free_names(server, &data->f_attr);
1947
1948 if (o_arg->open_flags & O_CREAT)
1949 update_changeattr(dir, &o_res->cinfo);
1950 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1951 server->caps &= ~NFS_CAP_POSIX_LOCK;
1952 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1953 status = _nfs4_proc_open_confirm(data);
1954 if (status != 0)
1955 return status;
1956 }
1957 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1958 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
1959 return 0;
1960}
1961
1962static int nfs4_recover_expired_lease(struct nfs_server *server)
1963{
1964 return nfs4_client_recover_expired_lease(server->nfs_client);
1965}
1966
1967/*
1968 * OPEN_EXPIRED:
1969 * reclaim state on the server after a network partition.
1970 * Assumes caller holds the appropriate lock
1971 */
1972static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1973{
1974 struct nfs4_opendata *opendata;
1975 int ret;
1976
1977 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1978 NFS4_OPEN_CLAIM_FH);
1979 if (IS_ERR(opendata))
1980 return PTR_ERR(opendata);
1981 ret = nfs4_open_recover(opendata, state);
1982 if (ret == -ESTALE)
1983 d_drop(ctx->dentry);
1984 nfs4_opendata_put(opendata);
1985 return ret;
1986}
1987
1988static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1989{
1990 struct nfs_server *server = NFS_SERVER(state->inode);
1991 struct nfs4_exception exception = { };
1992 int err;
1993
1994 do {
1995 err = _nfs4_open_expired(ctx, state);
1996 trace_nfs4_open_expired(ctx, 0, err);
1997 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1998 continue;
1999 switch (err) {
2000 default:
2001 goto out;
2002 case -NFS4ERR_GRACE:
2003 case -NFS4ERR_DELAY:
2004 nfs4_handle_exception(server, err, &exception);
2005 err = 0;
2006 }
2007 } while (exception.retry);
2008out:
2009 return err;
2010}
2011
2012static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2013{
2014 struct nfs_open_context *ctx;
2015 int ret;
2016
2017 ctx = nfs4_state_find_open_context(state);
2018 if (IS_ERR(ctx))
2019 return -EAGAIN;
2020 ret = nfs4_do_open_expired(ctx, state);
2021 put_nfs_open_context(ctx);
2022 return ret;
2023}
2024
2025#if defined(CONFIG_NFS_V4_1)
2026static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
2027{
2028 struct nfs_server *server = NFS_SERVER(state->inode);
2029 nfs4_stateid *stateid = &state->stateid;
2030 struct nfs_delegation *delegation;
2031 struct rpc_cred *cred = NULL;
2032 int status = -NFS4ERR_BAD_STATEID;
2033
2034 /* If a state reset has been done, test_stateid is unneeded */
2035 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2036 return;
2037
2038 /* Get the delegation credential for use by test/free_stateid */
2039 rcu_read_lock();
2040 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2041 if (delegation != NULL &&
2042 nfs4_stateid_match(&delegation->stateid, stateid)) {
2043 cred = get_rpccred(delegation->cred);
2044 rcu_read_unlock();
2045 status = nfs41_test_stateid(server, stateid, cred);
2046 trace_nfs4_test_delegation_stateid(state, NULL, status);
2047 } else
2048 rcu_read_unlock();
2049
2050 if (status != NFS_OK) {
2051 /* Free the stateid unless the server explicitly
2052 * informs us the stateid is unrecognized. */
2053 if (status != -NFS4ERR_BAD_STATEID)
2054 nfs41_free_stateid(server, stateid, cred);
2055 nfs_remove_bad_delegation(state->inode);
2056
2057 write_seqlock(&state->seqlock);
2058 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2059 write_sequnlock(&state->seqlock);
2060 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2061 }
2062
2063 if (cred != NULL)
2064 put_rpccred(cred);
2065}
2066
2067/**
2068 * nfs41_check_open_stateid - possibly free an open stateid
2069 *
2070 * @state: NFSv4 state for an inode
2071 *
2072 * Returns NFS_OK if recovery for this stateid is now finished.
2073 * Otherwise a negative NFS4ERR value is returned.
2074 */
2075static int nfs41_check_open_stateid(struct nfs4_state *state)
2076{
2077 struct nfs_server *server = NFS_SERVER(state->inode);
2078 nfs4_stateid *stateid = &state->open_stateid;
2079 struct rpc_cred *cred = state->owner->so_cred;
2080 int status;
2081
2082 /* If a state reset has been done, test_stateid is unneeded */
2083 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2084 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2085 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2086 return -NFS4ERR_BAD_STATEID;
2087
2088 status = nfs41_test_stateid(server, stateid, cred);
2089 trace_nfs4_test_open_stateid(state, NULL, status);
2090 if (status != NFS_OK) {
2091 /* Free the stateid unless the server explicitly
2092 * informs us the stateid is unrecognized. */
2093 if (status != -NFS4ERR_BAD_STATEID)
2094 nfs41_free_stateid(server, stateid, cred);
2095
2096 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2097 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2098 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2099 clear_bit(NFS_OPEN_STATE, &state->flags);
2100 }
2101 return status;
2102}
2103
2104static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2105{
2106 int status;
2107
2108 nfs41_clear_delegation_stateid(state);
2109 status = nfs41_check_open_stateid(state);
2110 if (status != NFS_OK)
2111 status = nfs4_open_expired(sp, state);
2112 return status;
2113}
2114#endif
2115
2116/*
2117 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2118 * fields corresponding to attributes that were used to store the verifier.
2119 * Make sure we clobber those fields in the later setattr call
2120 */
2121static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2122{
2123 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2124 !(sattr->ia_valid & ATTR_ATIME_SET))
2125 sattr->ia_valid |= ATTR_ATIME;
2126
2127 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2128 !(sattr->ia_valid & ATTR_MTIME_SET))
2129 sattr->ia_valid |= ATTR_MTIME;
2130}
2131
2132static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2133 fmode_t fmode,
2134 int flags,
2135 struct nfs_open_context *ctx)
2136{
2137 struct nfs4_state_owner *sp = opendata->owner;
2138 struct nfs_server *server = sp->so_server;
2139 struct dentry *dentry;
2140 struct nfs4_state *state;
2141 unsigned int seq;
2142 int ret;
2143
2144 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2145
2146 ret = _nfs4_proc_open(opendata);
2147 if (ret != 0)
2148 goto out;
2149
2150 state = nfs4_opendata_to_nfs4_state(opendata);
2151 ret = PTR_ERR(state);
2152 if (IS_ERR(state))
2153 goto out;
2154 if (server->caps & NFS_CAP_POSIX_LOCK)
2155 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2156
2157 dentry = opendata->dentry;
2158 if (dentry->d_inode == NULL) {
2159 /* FIXME: Is this d_drop() ever needed? */
2160 d_drop(dentry);
2161 dentry = d_add_unique(dentry, igrab(state->inode));
2162 if (dentry == NULL) {
2163 dentry = opendata->dentry;
2164 } else if (dentry != ctx->dentry) {
2165 dput(ctx->dentry);
2166 ctx->dentry = dget(dentry);
2167 }
2168 nfs_set_verifier(dentry,
2169 nfs_save_change_attribute(opendata->dir->d_inode));
2170 }
2171
2172 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2173 if (ret != 0)
2174 goto out;
2175
2176 ctx->state = state;
2177 if (dentry->d_inode == state->inode) {
2178 nfs_inode_attach_open_context(ctx);
2179 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2180 nfs4_schedule_stateid_recovery(server, state);
2181 }
2182out:
2183 return ret;
2184}
2185
2186/*
2187 * Returns a referenced nfs4_state
2188 */
2189static int _nfs4_do_open(struct inode *dir,
2190 struct nfs_open_context *ctx,
2191 int flags,
2192 struct iattr *sattr,
2193 struct nfs4_label *label)
2194{
2195 struct nfs4_state_owner *sp;
2196 struct nfs4_state *state = NULL;
2197 struct nfs_server *server = NFS_SERVER(dir);
2198 struct nfs4_opendata *opendata;
2199 struct dentry *dentry = ctx->dentry;
2200 struct rpc_cred *cred = ctx->cred;
2201 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2202 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2203 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2204 struct nfs4_label *olabel = NULL;
2205 int status;
2206
2207 /* Protect against reboot recovery conflicts */
2208 status = -ENOMEM;
2209 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2210 if (sp == NULL) {
2211 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2212 goto out_err;
2213 }
2214 status = nfs4_recover_expired_lease(server);
2215 if (status != 0)
2216 goto err_put_state_owner;
2217 if (dentry->d_inode != NULL)
2218 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
2219 status = -ENOMEM;
2220 if (dentry->d_inode)
2221 claim = NFS4_OPEN_CLAIM_FH;
2222 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2223 label, claim, GFP_KERNEL);
2224 if (opendata == NULL)
2225 goto err_put_state_owner;
2226
2227 if (label) {
2228 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2229 if (IS_ERR(olabel)) {
2230 status = PTR_ERR(olabel);
2231 goto err_opendata_put;
2232 }
2233 }
2234
2235 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2236 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2237 if (!opendata->f_attr.mdsthreshold)
2238 goto err_free_label;
2239 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2240 }
2241 if (dentry->d_inode != NULL)
2242 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2243
2244 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2245 if (status != 0)
2246 goto err_free_label;
2247 state = ctx->state;
2248
2249 if ((opendata->o_arg.open_flags & O_EXCL) &&
2250 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2251 nfs4_exclusive_attrset(opendata, sattr);
2252
2253 nfs_fattr_init(opendata->o_res.f_attr);
2254 status = nfs4_do_setattr(state->inode, cred,
2255 opendata->o_res.f_attr, sattr,
2256 state, label, olabel);
2257 if (status == 0) {
2258 nfs_setattr_update_inode(state->inode, sattr);
2259 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2260 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2261 }
2262 }
2263
2264 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2265 *ctx_th = opendata->f_attr.mdsthreshold;
2266 else
2267 kfree(opendata->f_attr.mdsthreshold);
2268 opendata->f_attr.mdsthreshold = NULL;
2269
2270 nfs4_label_free(olabel);
2271
2272 nfs4_opendata_put(opendata);
2273 nfs4_put_state_owner(sp);
2274 return 0;
2275err_free_label:
2276 nfs4_label_free(olabel);
2277err_opendata_put:
2278 kfree(opendata->f_attr.mdsthreshold);
2279 nfs4_opendata_put(opendata);
2280err_put_state_owner:
2281 nfs4_put_state_owner(sp);
2282out_err:
2283 return status;
2284}
2285
2286
2287static struct nfs4_state *nfs4_do_open(struct inode *dir,
2288 struct nfs_open_context *ctx,
2289 int flags,
2290 struct iattr *sattr,
2291 struct nfs4_label *label)
2292{
2293 struct nfs_server *server = NFS_SERVER(dir);
2294 struct nfs4_exception exception = { };
2295 struct nfs4_state *res;
2296 int status;
2297
2298 do {
2299 status = _nfs4_do_open(dir, ctx, flags, sattr, label);
2300 res = ctx->state;
2301 trace_nfs4_open_file(ctx, flags, status);
2302 if (status == 0)
2303 break;
2304 /* NOTE: BAD_SEQID means the server and client disagree about the
2305 * book-keeping w.r.t. state-changing operations
2306 * (OPEN/CLOSE/LOCK/LOCKU...)
2307 * It is actually a sign of a bug on the client or on the server.
2308 *
2309 * If we receive a BAD_SEQID error in the particular case of
2310 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2311 * have unhashed the old state_owner for us, and that we can
2312 * therefore safely retry using a new one. We should still warn
2313 * the user though...
2314 */
2315 if (status == -NFS4ERR_BAD_SEQID) {
2316 pr_warn_ratelimited("NFS: v4 server %s "
2317 " returned a bad sequence-id error!\n",
2318 NFS_SERVER(dir)->nfs_client->cl_hostname);
2319 exception.retry = 1;
2320 continue;
2321 }
2322 /*
2323 * BAD_STATEID on OPEN means that the server cancelled our
2324 * state before it received the OPEN_CONFIRM.
2325 * Recover by retrying the request as per the discussion
2326 * on Page 181 of RFC3530.
2327 */
2328 if (status == -NFS4ERR_BAD_STATEID) {
2329 exception.retry = 1;
2330 continue;
2331 }
2332 if (status == -EAGAIN) {
2333 /* We must have found a delegation */
2334 exception.retry = 1;
2335 continue;
2336 }
2337 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2338 continue;
2339 res = ERR_PTR(nfs4_handle_exception(server,
2340 status, &exception));
2341 } while (exception.retry);
2342 return res;
2343}
2344
2345static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2346 struct nfs_fattr *fattr, struct iattr *sattr,
2347 struct nfs4_state *state, struct nfs4_label *ilabel,
2348 struct nfs4_label *olabel)
2349{
2350 struct nfs_server *server = NFS_SERVER(inode);
2351 struct nfs_setattrargs arg = {
2352 .fh = NFS_FH(inode),
2353 .iap = sattr,
2354 .server = server,
2355 .bitmask = server->attr_bitmask,
2356 .label = ilabel,
2357 };
2358 struct nfs_setattrres res = {
2359 .fattr = fattr,
2360 .label = olabel,
2361 .server = server,
2362 };
2363 struct rpc_message msg = {
2364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2365 .rpc_argp = &arg,
2366 .rpc_resp = &res,
2367 .rpc_cred = cred,
2368 };
2369 unsigned long timestamp = jiffies;
2370 fmode_t fmode;
2371 bool truncate;
2372 int status;
2373
2374 arg.bitmask = nfs4_bitmask(server, ilabel);
2375 if (ilabel)
2376 arg.bitmask = nfs4_bitmask(server, olabel);
2377
2378 nfs_fattr_init(fattr);
2379
2380 /* Servers should only apply open mode checks for file size changes */
2381 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2382 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2383
2384 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2385 /* Use that stateid */
2386 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
2387 struct nfs_lockowner lockowner = {
2388 .l_owner = current->files,
2389 .l_pid = current->tgid,
2390 };
2391 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2392 &lockowner);
2393 } else
2394 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2395
2396 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2397 if (status == 0 && state != NULL)
2398 renew_lease(server, timestamp);
2399 return status;
2400}
2401
2402static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2403 struct nfs_fattr *fattr, struct iattr *sattr,
2404 struct nfs4_state *state, struct nfs4_label *ilabel,
2405 struct nfs4_label *olabel)
2406{
2407 struct nfs_server *server = NFS_SERVER(inode);
2408 struct nfs4_exception exception = {
2409 .state = state,
2410 .inode = inode,
2411 };
2412 int err;
2413 do {
2414 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2415 trace_nfs4_setattr(inode, err);
2416 switch (err) {
2417 case -NFS4ERR_OPENMODE:
2418 if (!(sattr->ia_valid & ATTR_SIZE)) {
2419 pr_warn_once("NFSv4: server %s is incorrectly "
2420 "applying open mode checks to "
2421 "a SETATTR that is not "
2422 "changing file size.\n",
2423 server->nfs_client->cl_hostname);
2424 }
2425 if (state && !(state->state & FMODE_WRITE)) {
2426 err = -EBADF;
2427 if (sattr->ia_valid & ATTR_OPEN)
2428 err = -EACCES;
2429 goto out;
2430 }
2431 }
2432 err = nfs4_handle_exception(server, err, &exception);
2433 } while (exception.retry);
2434out:
2435 return err;
2436}
2437
2438struct nfs4_closedata {
2439 struct inode *inode;
2440 struct nfs4_state *state;
2441 struct nfs_closeargs arg;
2442 struct nfs_closeres res;
2443 struct nfs_fattr fattr;
2444 unsigned long timestamp;
2445 bool roc;
2446 u32 roc_barrier;
2447};
2448
2449static void nfs4_free_closedata(void *data)
2450{
2451 struct nfs4_closedata *calldata = data;
2452 struct nfs4_state_owner *sp = calldata->state->owner;
2453 struct super_block *sb = calldata->state->inode->i_sb;
2454
2455 if (calldata->roc)
2456 pnfs_roc_release(calldata->state->inode);
2457 nfs4_put_open_state(calldata->state);
2458 nfs_free_seqid(calldata->arg.seqid);
2459 nfs4_put_state_owner(sp);
2460 nfs_sb_deactive(sb);
2461 kfree(calldata);
2462}
2463
2464static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2465 fmode_t fmode)
2466{
2467 spin_lock(&state->owner->so_lock);
2468 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2469 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2470 case FMODE_WRITE:
2471 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2472 break;
2473 case FMODE_READ:
2474 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2475 break;
2476 case 0:
2477 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2478 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2479 clear_bit(NFS_OPEN_STATE, &state->flags);
2480 }
2481 spin_unlock(&state->owner->so_lock);
2482}
2483
2484static void nfs4_close_done(struct rpc_task *task, void *data)
2485{
2486 struct nfs4_closedata *calldata = data;
2487 struct nfs4_state *state = calldata->state;
2488 struct nfs_server *server = NFS_SERVER(calldata->inode);
2489
2490 dprintk("%s: begin!\n", __func__);
2491 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2492 return;
2493 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2494 /* hmm. we are done with the inode, and in the process of freeing
2495 * the state_owner. we keep this around to process errors
2496 */
2497 switch (task->tk_status) {
2498 case 0:
2499 if (calldata->roc)
2500 pnfs_roc_set_barrier(state->inode,
2501 calldata->roc_barrier);
2502 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2503 renew_lease(server, calldata->timestamp);
2504 nfs4_close_clear_stateid_flags(state,
2505 calldata->arg.fmode);
2506 break;
2507 case -NFS4ERR_STALE_STATEID:
2508 case -NFS4ERR_OLD_STATEID:
2509 case -NFS4ERR_BAD_STATEID:
2510 case -NFS4ERR_EXPIRED:
2511 if (calldata->arg.fmode == 0)
2512 break;
2513 default:
2514 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2515 rpc_restart_call_prepare(task);
2516 }
2517 nfs_release_seqid(calldata->arg.seqid);
2518 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2519 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2520}
2521
2522static void nfs4_close_prepare(struct rpc_task *task, void *data)
2523{
2524 struct nfs4_closedata *calldata = data;
2525 struct nfs4_state *state = calldata->state;
2526 struct inode *inode = calldata->inode;
2527 int call_close = 0;
2528
2529 dprintk("%s: begin!\n", __func__);
2530 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2531 goto out_wait;
2532
2533 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2534 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2535 spin_lock(&state->owner->so_lock);
2536 /* Calculate the change in open mode */
2537 if (state->n_rdwr == 0) {
2538 if (state->n_rdonly == 0) {
2539 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2540 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2541 calldata->arg.fmode &= ~FMODE_READ;
2542 }
2543 if (state->n_wronly == 0) {
2544 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2545 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2546 calldata->arg.fmode &= ~FMODE_WRITE;
2547 }
2548 }
2549 if (!nfs4_valid_open_stateid(state))
2550 call_close = 0;
2551 spin_unlock(&state->owner->so_lock);
2552
2553 if (!call_close) {
2554 /* Note: exit _without_ calling nfs4_close_done */
2555 goto out_no_action;
2556 }
2557
2558 if (calldata->arg.fmode == 0) {
2559 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2560 if (calldata->roc &&
2561 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2562 nfs_release_seqid(calldata->arg.seqid);
2563 goto out_wait;
2564 }
2565 }
2566
2567 nfs_fattr_init(calldata->res.fattr);
2568 calldata->timestamp = jiffies;
2569 if (nfs4_setup_sequence(NFS_SERVER(inode),
2570 &calldata->arg.seq_args,
2571 &calldata->res.seq_res,
2572 task) != 0)
2573 nfs_release_seqid(calldata->arg.seqid);
2574 dprintk("%s: done!\n", __func__);
2575 return;
2576out_no_action:
2577 task->tk_action = NULL;
2578out_wait:
2579 nfs4_sequence_done(task, &calldata->res.seq_res);
2580}
2581
2582static const struct rpc_call_ops nfs4_close_ops = {
2583 .rpc_call_prepare = nfs4_close_prepare,
2584 .rpc_call_done = nfs4_close_done,
2585 .rpc_release = nfs4_free_closedata,
2586};
2587
2588/*
2589 * It is possible for data to be read/written from a mem-mapped file
2590 * after the sys_close call (which hits the vfs layer as a flush).
2591 * This means that we can't safely call nfsv4 close on a file until
2592 * the inode is cleared. This in turn means that we are not good
2593 * NFSv4 citizens - we do not indicate to the server to update the file's
2594 * share state even when we are done with one of the three share
2595 * stateid's in the inode.
2596 *
2597 * NOTE: Caller must be holding the sp->so_owner semaphore!
2598 */
2599int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2600{
2601 struct nfs_server *server = NFS_SERVER(state->inode);
2602 struct nfs4_closedata *calldata;
2603 struct nfs4_state_owner *sp = state->owner;
2604 struct rpc_task *task;
2605 struct rpc_message msg = {
2606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2607 .rpc_cred = state->owner->so_cred,
2608 };
2609 struct rpc_task_setup task_setup_data = {
2610 .rpc_client = server->client,
2611 .rpc_message = &msg,
2612 .callback_ops = &nfs4_close_ops,
2613 .workqueue = nfsiod_workqueue,
2614 .flags = RPC_TASK_ASYNC,
2615 };
2616 int status = -ENOMEM;
2617
2618 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2619 if (calldata == NULL)
2620 goto out;
2621 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2622 calldata->inode = state->inode;
2623 calldata->state = state;
2624 calldata->arg.fh = NFS_FH(state->inode);
2625 calldata->arg.stateid = &state->open_stateid;
2626 /* Serialization for the sequence id */
2627 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2628 if (calldata->arg.seqid == NULL)
2629 goto out_free_calldata;
2630 calldata->arg.fmode = 0;
2631 calldata->arg.bitmask = server->cache_consistency_bitmask;
2632 calldata->res.fattr = &calldata->fattr;
2633 calldata->res.seqid = calldata->arg.seqid;
2634 calldata->res.server = server;
2635 calldata->roc = pnfs_roc(state->inode);
2636 nfs_sb_active(calldata->inode->i_sb);
2637
2638 msg.rpc_argp = &calldata->arg;
2639 msg.rpc_resp = &calldata->res;
2640 task_setup_data.callback_data = calldata;
2641 task = rpc_run_task(&task_setup_data);
2642 if (IS_ERR(task))
2643 return PTR_ERR(task);
2644 status = 0;
2645 if (wait)
2646 status = rpc_wait_for_completion_task(task);
2647 rpc_put_task(task);
2648 return status;
2649out_free_calldata:
2650 kfree(calldata);
2651out:
2652 nfs4_put_open_state(state);
2653 nfs4_put_state_owner(sp);
2654 return status;
2655}
2656
2657static struct inode *
2658nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2659{
2660 struct nfs4_state *state;
2661 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2662
2663 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2664
2665 /* Protect against concurrent sillydeletes */
2666 state = nfs4_do_open(dir, ctx, open_flags, attr, label);
2667
2668 nfs4_label_release_security(label);
2669
2670 if (IS_ERR(state))
2671 return ERR_CAST(state);
2672 return state->inode;
2673}
2674
2675static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2676{
2677 if (ctx->state == NULL)
2678 return;
2679 if (is_sync)
2680 nfs4_close_sync(ctx->state, ctx->mode);
2681 else
2682 nfs4_close_state(ctx->state, ctx->mode);
2683}
2684
2685static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2686{
2687 struct nfs4_server_caps_arg args = {
2688 .fhandle = fhandle,
2689 };
2690 struct nfs4_server_caps_res res = {};
2691 struct rpc_message msg = {
2692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2693 .rpc_argp = &args,
2694 .rpc_resp = &res,
2695 };
2696 int status;
2697
2698 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2699 if (status == 0) {
2700 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2701 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2702 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2703 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2704 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2705 NFS_CAP_CTIME|NFS_CAP_MTIME);
2706 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2707 server->caps |= NFS_CAP_ACLS;
2708 if (res.has_links != 0)
2709 server->caps |= NFS_CAP_HARDLINKS;
2710 if (res.has_symlinks != 0)
2711 server->caps |= NFS_CAP_SYMLINKS;
2712 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2713 server->caps |= NFS_CAP_FILEID;
2714 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2715 server->caps |= NFS_CAP_MODE;
2716 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2717 server->caps |= NFS_CAP_NLINK;
2718 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2719 server->caps |= NFS_CAP_OWNER;
2720 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2721 server->caps |= NFS_CAP_OWNER_GROUP;
2722 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2723 server->caps |= NFS_CAP_ATIME;
2724 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2725 server->caps |= NFS_CAP_CTIME;
2726 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2727 server->caps |= NFS_CAP_MTIME;
2728#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2729 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2730 server->caps |= NFS_CAP_SECURITY_LABEL;
2731#endif
2732 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2733 sizeof(server->attr_bitmask));
2734
2735 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2736 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2737 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2738 }
2739 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2740 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2741 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2742 server->acl_bitmask = res.acl_bitmask;
2743 server->fh_expire_type = res.fh_expire_type;
2744 }
2745
2746 return status;
2747}
2748
2749int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2750{
2751 struct nfs4_exception exception = { };
2752 int err;
2753 do {
2754 err = nfs4_handle_exception(server,
2755 _nfs4_server_capabilities(server, fhandle),
2756 &exception);
2757 } while (exception.retry);
2758 return err;
2759}
2760
2761static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2762 struct nfs_fsinfo *info)
2763{
2764 u32 bitmask[3];
2765 struct nfs4_lookup_root_arg args = {
2766 .bitmask = bitmask,
2767 };
2768 struct nfs4_lookup_res res = {
2769 .server = server,
2770 .fattr = info->fattr,
2771 .fh = fhandle,
2772 };
2773 struct rpc_message msg = {
2774 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2775 .rpc_argp = &args,
2776 .rpc_resp = &res,
2777 };
2778
2779 bitmask[0] = nfs4_fattr_bitmap[0];
2780 bitmask[1] = nfs4_fattr_bitmap[1];
2781 /*
2782 * Process the label in the upcoming getfattr
2783 */
2784 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2785
2786 nfs_fattr_init(info->fattr);
2787 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2788}
2789
2790static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2791 struct nfs_fsinfo *info)
2792{
2793 struct nfs4_exception exception = { };
2794 int err;
2795 do {
2796 err = _nfs4_lookup_root(server, fhandle, info);
2797 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
2798 switch (err) {
2799 case 0:
2800 case -NFS4ERR_WRONGSEC:
2801 goto out;
2802 default:
2803 err = nfs4_handle_exception(server, err, &exception);
2804 }
2805 } while (exception.retry);
2806out:
2807 return err;
2808}
2809
2810static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2811 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2812{
2813 struct rpc_auth_create_args auth_args = {
2814 .pseudoflavor = flavor,
2815 };
2816 struct rpc_auth *auth;
2817 int ret;
2818
2819 auth = rpcauth_create(&auth_args, server->client);
2820 if (IS_ERR(auth)) {
2821 ret = -EACCES;
2822 goto out;
2823 }
2824 ret = nfs4_lookup_root(server, fhandle, info);
2825out:
2826 return ret;
2827}
2828
2829/*
2830 * Retry pseudoroot lookup with various security flavors. We do this when:
2831 *
2832 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2833 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2834 *
2835 * Returns zero on success, or a negative NFS4ERR value, or a
2836 * negative errno value.
2837 */
2838static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2839 struct nfs_fsinfo *info)
2840{
2841 /* Per 3530bis 15.33.5 */
2842 static const rpc_authflavor_t flav_array[] = {
2843 RPC_AUTH_GSS_KRB5P,
2844 RPC_AUTH_GSS_KRB5I,
2845 RPC_AUTH_GSS_KRB5,
2846 RPC_AUTH_UNIX, /* courtesy */
2847 RPC_AUTH_NULL,
2848 };
2849 int status = -EPERM;
2850 size_t i;
2851
2852 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
2853 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2854 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2855 continue;
2856 break;
2857 }
2858
2859 /*
2860 * -EACCESS could mean that the user doesn't have correct permissions
2861 * to access the mount. It could also mean that we tried to mount
2862 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2863 * existing mount programs don't handle -EACCES very well so it should
2864 * be mapped to -EPERM instead.
2865 */
2866 if (status == -EACCES)
2867 status = -EPERM;
2868 return status;
2869}
2870
2871static int nfs4_do_find_root_sec(struct nfs_server *server,
2872 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2873{
2874 int mv = server->nfs_client->cl_minorversion;
2875 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2876}
2877
2878/**
2879 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2880 * @server: initialized nfs_server handle
2881 * @fhandle: we fill in the pseudo-fs root file handle
2882 * @info: we fill in an FSINFO struct
2883 *
2884 * Returns zero on success, or a negative errno.
2885 */
2886int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2887 struct nfs_fsinfo *info)
2888{
2889 int status;
2890
2891 status = nfs4_lookup_root(server, fhandle, info);
2892 if ((status == -NFS4ERR_WRONGSEC) &&
2893 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2894 status = nfs4_do_find_root_sec(server, fhandle, info);
2895
2896 if (status == 0)
2897 status = nfs4_server_capabilities(server, fhandle);
2898 if (status == 0)
2899 status = nfs4_do_fsinfo(server, fhandle, info);
2900
2901 return nfs4_map_errors(status);
2902}
2903
2904static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2905 struct nfs_fsinfo *info)
2906{
2907 int error;
2908 struct nfs_fattr *fattr = info->fattr;
2909 struct nfs4_label *label = NULL;
2910
2911 error = nfs4_server_capabilities(server, mntfh);
2912 if (error < 0) {
2913 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2914 return error;
2915 }
2916
2917 label = nfs4_label_alloc(server, GFP_KERNEL);
2918 if (IS_ERR(label))
2919 return PTR_ERR(label);
2920
2921 error = nfs4_proc_getattr(server, mntfh, fattr, label);
2922 if (error < 0) {
2923 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2924 goto err_free_label;
2925 }
2926
2927 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2928 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2929 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2930
2931err_free_label:
2932 nfs4_label_free(label);
2933
2934 return error;
2935}
2936
2937/*
2938 * Get locations and (maybe) other attributes of a referral.
2939 * Note that we'll actually follow the referral later when
2940 * we detect fsid mismatch in inode revalidation
2941 */
2942static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2943 const struct qstr *name, struct nfs_fattr *fattr,
2944 struct nfs_fh *fhandle)
2945{
2946 int status = -ENOMEM;
2947 struct page *page = NULL;
2948 struct nfs4_fs_locations *locations = NULL;
2949
2950 page = alloc_page(GFP_KERNEL);
2951 if (page == NULL)
2952 goto out;
2953 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2954 if (locations == NULL)
2955 goto out;
2956
2957 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2958 if (status != 0)
2959 goto out;
2960 /* Make sure server returned a different fsid for the referral */
2961 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2962 dprintk("%s: server did not return a different fsid for"
2963 " a referral at %s\n", __func__, name->name);
2964 status = -EIO;
2965 goto out;
2966 }
2967 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2968 nfs_fixup_referral_attributes(&locations->fattr);
2969
2970 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2971 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2972 memset(fhandle, 0, sizeof(struct nfs_fh));
2973out:
2974 if (page)
2975 __free_page(page);
2976 kfree(locations);
2977 return status;
2978}
2979
2980static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2981 struct nfs_fattr *fattr, struct nfs4_label *label)
2982{
2983 struct nfs4_getattr_arg args = {
2984 .fh = fhandle,
2985 .bitmask = server->attr_bitmask,
2986 };
2987 struct nfs4_getattr_res res = {
2988 .fattr = fattr,
2989 .label = label,
2990 .server = server,
2991 };
2992 struct rpc_message msg = {
2993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2994 .rpc_argp = &args,
2995 .rpc_resp = &res,
2996 };
2997
2998 args.bitmask = nfs4_bitmask(server, label);
2999
3000 nfs_fattr_init(fattr);
3001 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3002}
3003
3004static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3005 struct nfs_fattr *fattr, struct nfs4_label *label)
3006{
3007 struct nfs4_exception exception = { };
3008 int err;
3009 do {
3010 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3011 trace_nfs4_getattr(server, fhandle, fattr, err);
3012 err = nfs4_handle_exception(server, err,
3013 &exception);
3014 } while (exception.retry);
3015 return err;
3016}
3017
3018/*
3019 * The file is not closed if it is opened due to the a request to change
3020 * the size of the file. The open call will not be needed once the
3021 * VFS layer lookup-intents are implemented.
3022 *
3023 * Close is called when the inode is destroyed.
3024 * If we haven't opened the file for O_WRONLY, we
3025 * need to in the size_change case to obtain a stateid.
3026 *
3027 * Got race?
3028 * Because OPEN is always done by name in nfsv4, it is
3029 * possible that we opened a different file by the same
3030 * name. We can recognize this race condition, but we
3031 * can't do anything about it besides returning an error.
3032 *
3033 * This will be fixed with VFS changes (lookup-intent).
3034 */
3035static int
3036nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3037 struct iattr *sattr)
3038{
3039 struct inode *inode = dentry->d_inode;
3040 struct rpc_cred *cred = NULL;
3041 struct nfs4_state *state = NULL;
3042 struct nfs4_label *label = NULL;
3043 int status;
3044
3045 if (pnfs_ld_layoutret_on_setattr(inode))
3046 pnfs_commit_and_return_layout(inode);
3047
3048 nfs_fattr_init(fattr);
3049
3050 /* Deal with open(O_TRUNC) */
3051 if (sattr->ia_valid & ATTR_OPEN)
3052 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3053
3054 /* Optimization: if the end result is no change, don't RPC */
3055 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3056 return 0;
3057
3058 /* Search for an existing open(O_WRITE) file */
3059 if (sattr->ia_valid & ATTR_FILE) {
3060 struct nfs_open_context *ctx;
3061
3062 ctx = nfs_file_open_context(sattr->ia_file);
3063 if (ctx) {
3064 cred = ctx->cred;
3065 state = ctx->state;
3066 }
3067 }
3068
3069 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3070 if (IS_ERR(label))
3071 return PTR_ERR(label);
3072
3073 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3074 if (status == 0) {
3075 nfs_setattr_update_inode(inode, sattr);
3076 nfs_setsecurity(inode, fattr, label);
3077 }
3078 nfs4_label_free(label);
3079 return status;
3080}
3081
3082static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3083 const struct qstr *name, struct nfs_fh *fhandle,
3084 struct nfs_fattr *fattr, struct nfs4_label *label)
3085{
3086 struct nfs_server *server = NFS_SERVER(dir);
3087 int status;
3088 struct nfs4_lookup_arg args = {
3089 .bitmask = server->attr_bitmask,
3090 .dir_fh = NFS_FH(dir),
3091 .name = name,
3092 };
3093 struct nfs4_lookup_res res = {
3094 .server = server,
3095 .fattr = fattr,
3096 .label = label,
3097 .fh = fhandle,
3098 };
3099 struct rpc_message msg = {
3100 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3101 .rpc_argp = &args,
3102 .rpc_resp = &res,
3103 };
3104
3105 args.bitmask = nfs4_bitmask(server, label);
3106
3107 nfs_fattr_init(fattr);
3108
3109 dprintk("NFS call lookup %s\n", name->name);
3110 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3111 dprintk("NFS reply lookup: %d\n", status);
3112 return status;
3113}
3114
3115static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3116{
3117 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3118 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3119 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3120 fattr->nlink = 2;
3121}
3122
3123static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3124 struct qstr *name, struct nfs_fh *fhandle,
3125 struct nfs_fattr *fattr, struct nfs4_label *label)
3126{
3127 struct nfs4_exception exception = { };
3128 struct rpc_clnt *client = *clnt;
3129 int err;
3130 do {
3131 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3132 trace_nfs4_lookup(dir, name, err);
3133 switch (err) {
3134 case -NFS4ERR_BADNAME:
3135 err = -ENOENT;
3136 goto out;
3137 case -NFS4ERR_MOVED:
3138 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3139 goto out;
3140 case -NFS4ERR_WRONGSEC:
3141 err = -EPERM;
3142 if (client != *clnt)
3143 goto out;
3144
3145 client = nfs4_create_sec_client(client, dir, name);
3146 if (IS_ERR(client))
3147 return PTR_ERR(client);
3148
3149 exception.retry = 1;
3150 break;
3151 default:
3152 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3153 }
3154 } while (exception.retry);
3155
3156out:
3157 if (err == 0)
3158 *clnt = client;
3159 else if (client != *clnt)
3160 rpc_shutdown_client(client);
3161
3162 return err;
3163}
3164
3165static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3166 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3167 struct nfs4_label *label)
3168{
3169 int status;
3170 struct rpc_clnt *client = NFS_CLIENT(dir);
3171
3172 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3173 if (client != NFS_CLIENT(dir)) {
3174 rpc_shutdown_client(client);
3175 nfs_fixup_secinfo_attributes(fattr);
3176 }
3177 return status;
3178}
3179
3180struct rpc_clnt *
3181nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3182 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3183{
3184 int status;
3185 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
3186
3187 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3188 if (status < 0) {
3189 rpc_shutdown_client(client);
3190 return ERR_PTR(status);
3191 }
3192 return client;
3193}
3194
3195static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3196{
3197 struct nfs_server *server = NFS_SERVER(inode);
3198 struct nfs4_accessargs args = {
3199 .fh = NFS_FH(inode),
3200 .bitmask = server->cache_consistency_bitmask,
3201 };
3202 struct nfs4_accessres res = {
3203 .server = server,
3204 };
3205 struct rpc_message msg = {
3206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3207 .rpc_argp = &args,
3208 .rpc_resp = &res,
3209 .rpc_cred = entry->cred,
3210 };
3211 int mode = entry->mask;
3212 int status = 0;
3213
3214 /*
3215 * Determine which access bits we want to ask for...
3216 */
3217 if (mode & MAY_READ)
3218 args.access |= NFS4_ACCESS_READ;
3219 if (S_ISDIR(inode->i_mode)) {
3220 if (mode & MAY_WRITE)
3221 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3222 if (mode & MAY_EXEC)
3223 args.access |= NFS4_ACCESS_LOOKUP;
3224 } else {
3225 if (mode & MAY_WRITE)
3226 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3227 if (mode & MAY_EXEC)
3228 args.access |= NFS4_ACCESS_EXECUTE;
3229 }
3230
3231 res.fattr = nfs_alloc_fattr();
3232 if (res.fattr == NULL)
3233 return -ENOMEM;
3234
3235 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3236 if (!status) {
3237 nfs_access_set_mask(entry, res.access);
3238 nfs_refresh_inode(inode, res.fattr);
3239 }
3240 nfs_free_fattr(res.fattr);
3241 return status;
3242}
3243
3244static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3245{
3246 struct nfs4_exception exception = { };
3247 int err;
3248 do {
3249 err = _nfs4_proc_access(inode, entry);
3250 trace_nfs4_access(inode, err);
3251 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3252 &exception);
3253 } while (exception.retry);
3254 return err;
3255}
3256
3257/*
3258 * TODO: For the time being, we don't try to get any attributes
3259 * along with any of the zero-copy operations READ, READDIR,
3260 * READLINK, WRITE.
3261 *
3262 * In the case of the first three, we want to put the GETATTR
3263 * after the read-type operation -- this is because it is hard
3264 * to predict the length of a GETATTR response in v4, and thus
3265 * align the READ data correctly. This means that the GETATTR
3266 * may end up partially falling into the page cache, and we should
3267 * shift it into the 'tail' of the xdr_buf before processing.
3268 * To do this efficiently, we need to know the total length
3269 * of data received, which doesn't seem to be available outside
3270 * of the RPC layer.
3271 *
3272 * In the case of WRITE, we also want to put the GETATTR after
3273 * the operation -- in this case because we want to make sure
3274 * we get the post-operation mtime and size.
3275 *
3276 * Both of these changes to the XDR layer would in fact be quite
3277 * minor, but I decided to leave them for a subsequent patch.
3278 */
3279static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3280 unsigned int pgbase, unsigned int pglen)
3281{
3282 struct nfs4_readlink args = {
3283 .fh = NFS_FH(inode),
3284 .pgbase = pgbase,
3285 .pglen = pglen,
3286 .pages = &page,
3287 };
3288 struct nfs4_readlink_res res;
3289 struct rpc_message msg = {
3290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3291 .rpc_argp = &args,
3292 .rpc_resp = &res,
3293 };
3294
3295 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3296}
3297
3298static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3299 unsigned int pgbase, unsigned int pglen)
3300{
3301 struct nfs4_exception exception = { };
3302 int err;
3303 do {
3304 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3305 trace_nfs4_readlink(inode, err);
3306 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3307 &exception);
3308 } while (exception.retry);
3309 return err;
3310}
3311
3312/*
3313 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3314 */
3315static int
3316nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3317 int flags)
3318{
3319 struct nfs4_label l, *ilabel = NULL;
3320 struct nfs_open_context *ctx;
3321 struct nfs4_state *state;
3322 int status = 0;
3323
3324 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3325 if (IS_ERR(ctx))
3326 return PTR_ERR(ctx);
3327
3328 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3329
3330 sattr->ia_mode &= ~current_umask();
3331 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel);
3332 if (IS_ERR(state)) {
3333 status = PTR_ERR(state);
3334 goto out;
3335 }
3336out:
3337 nfs4_label_release_security(ilabel);
3338 put_nfs_open_context(ctx);
3339 return status;
3340}
3341
3342static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3343{
3344 struct nfs_server *server = NFS_SERVER(dir);
3345 struct nfs_removeargs args = {
3346 .fh = NFS_FH(dir),
3347 .name = *name,
3348 };
3349 struct nfs_removeres res = {
3350 .server = server,
3351 };
3352 struct rpc_message msg = {
3353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3354 .rpc_argp = &args,
3355 .rpc_resp = &res,
3356 };
3357 int status;
3358
3359 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3360 if (status == 0)
3361 update_changeattr(dir, &res.cinfo);
3362 return status;
3363}
3364
3365static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3366{
3367 struct nfs4_exception exception = { };
3368 int err;
3369 do {
3370 err = _nfs4_proc_remove(dir, name);
3371 trace_nfs4_remove(dir, name, err);
3372 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3373 &exception);
3374 } while (exception.retry);
3375 return err;
3376}
3377
3378static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3379{
3380 struct nfs_server *server = NFS_SERVER(dir);
3381 struct nfs_removeargs *args = msg->rpc_argp;
3382 struct nfs_removeres *res = msg->rpc_resp;
3383
3384 res->server = server;
3385 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3386 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3387
3388 nfs_fattr_init(res->dir_attr);
3389}
3390
3391static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3392{
3393 nfs4_setup_sequence(NFS_SERVER(data->dir),
3394 &data->args.seq_args,
3395 &data->res.seq_res,
3396 task);
3397}
3398
3399static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3400{
3401 struct nfs_unlinkdata *data = task->tk_calldata;
3402 struct nfs_removeres *res = &data->res;
3403
3404 if (!nfs4_sequence_done(task, &res->seq_res))
3405 return 0;
3406 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3407 return 0;
3408 update_changeattr(dir, &res->cinfo);
3409 return 1;
3410}
3411
3412static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3413{
3414 struct nfs_server *server = NFS_SERVER(dir);
3415 struct nfs_renameargs *arg = msg->rpc_argp;
3416 struct nfs_renameres *res = msg->rpc_resp;
3417
3418 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3419 res->server = server;
3420 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3421}
3422
3423static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3424{
3425 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3426 &data->args.seq_args,
3427 &data->res.seq_res,
3428 task);
3429}
3430
3431static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3432 struct inode *new_dir)
3433{
3434 struct nfs_renamedata *data = task->tk_calldata;
3435 struct nfs_renameres *res = &data->res;
3436
3437 if (!nfs4_sequence_done(task, &res->seq_res))
3438 return 0;
3439 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3440 return 0;
3441
3442 update_changeattr(old_dir, &res->old_cinfo);
3443 update_changeattr(new_dir, &res->new_cinfo);
3444 return 1;
3445}
3446
3447static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3448 struct inode *new_dir, struct qstr *new_name)
3449{
3450 struct nfs_server *server = NFS_SERVER(old_dir);
3451 struct nfs_renameargs arg = {
3452 .old_dir = NFS_FH(old_dir),
3453 .new_dir = NFS_FH(new_dir),
3454 .old_name = old_name,
3455 .new_name = new_name,
3456 };
3457 struct nfs_renameres res = {
3458 .server = server,
3459 };
3460 struct rpc_message msg = {
3461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3462 .rpc_argp = &arg,
3463 .rpc_resp = &res,
3464 };
3465 int status = -ENOMEM;
3466
3467 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3468 if (!status) {
3469 update_changeattr(old_dir, &res.old_cinfo);
3470 update_changeattr(new_dir, &res.new_cinfo);
3471 }
3472 return status;
3473}
3474
3475static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3476 struct inode *new_dir, struct qstr *new_name)
3477{
3478 struct nfs4_exception exception = { };
3479 int err;
3480 do {
3481 err = _nfs4_proc_rename(old_dir, old_name,
3482 new_dir, new_name);
3483 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3484 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
3485 &exception);
3486 } while (exception.retry);
3487 return err;
3488}
3489
3490static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3491{
3492 struct nfs_server *server = NFS_SERVER(inode);
3493 struct nfs4_link_arg arg = {
3494 .fh = NFS_FH(inode),
3495 .dir_fh = NFS_FH(dir),
3496 .name = name,
3497 .bitmask = server->attr_bitmask,
3498 };
3499 struct nfs4_link_res res = {
3500 .server = server,
3501 .label = NULL,
3502 };
3503 struct rpc_message msg = {
3504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3505 .rpc_argp = &arg,
3506 .rpc_resp = &res,
3507 };
3508 int status = -ENOMEM;
3509
3510 res.fattr = nfs_alloc_fattr();
3511 if (res.fattr == NULL)
3512 goto out;
3513
3514 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3515 if (IS_ERR(res.label)) {
3516 status = PTR_ERR(res.label);
3517 goto out;
3518 }
3519 arg.bitmask = nfs4_bitmask(server, res.label);
3520
3521 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3522 if (!status) {
3523 update_changeattr(dir, &res.cinfo);
3524 status = nfs_post_op_update_inode(inode, res.fattr);
3525 if (!status)
3526 nfs_setsecurity(inode, res.fattr, res.label);
3527 }
3528
3529
3530 nfs4_label_free(res.label);
3531
3532out:
3533 nfs_free_fattr(res.fattr);
3534 return status;
3535}
3536
3537static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3538{
3539 struct nfs4_exception exception = { };
3540 int err;
3541 do {
3542 err = nfs4_handle_exception(NFS_SERVER(inode),
3543 _nfs4_proc_link(inode, dir, name),
3544 &exception);
3545 } while (exception.retry);
3546 return err;
3547}
3548
3549struct nfs4_createdata {
3550 struct rpc_message msg;
3551 struct nfs4_create_arg arg;
3552 struct nfs4_create_res res;
3553 struct nfs_fh fh;
3554 struct nfs_fattr fattr;
3555 struct nfs4_label *label;
3556};
3557
3558static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3559 struct qstr *name, struct iattr *sattr, u32 ftype)
3560{
3561 struct nfs4_createdata *data;
3562
3563 data = kzalloc(sizeof(*data), GFP_KERNEL);
3564 if (data != NULL) {
3565 struct nfs_server *server = NFS_SERVER(dir);
3566
3567 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3568 if (IS_ERR(data->label))
3569 goto out_free;
3570
3571 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3572 data->msg.rpc_argp = &data->arg;
3573 data->msg.rpc_resp = &data->res;
3574 data->arg.dir_fh = NFS_FH(dir);
3575 data->arg.server = server;
3576 data->arg.name = name;
3577 data->arg.attrs = sattr;
3578 data->arg.ftype = ftype;
3579 data->arg.bitmask = nfs4_bitmask(server, data->label);
3580 data->res.server = server;
3581 data->res.fh = &data->fh;
3582 data->res.fattr = &data->fattr;
3583 data->res.label = data->label;
3584 nfs_fattr_init(data->res.fattr);
3585 }
3586 return data;
3587out_free:
3588 kfree(data);
3589 return NULL;
3590}
3591
3592static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3593{
3594 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3595 &data->arg.seq_args, &data->res.seq_res, 1);
3596 if (status == 0) {
3597 update_changeattr(dir, &data->res.dir_cinfo);
3598 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3599 }
3600 return status;
3601}
3602
3603static void nfs4_free_createdata(struct nfs4_createdata *data)
3604{
3605 nfs4_label_free(data->label);
3606 kfree(data);
3607}
3608
3609static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3610 struct page *page, unsigned int len, struct iattr *sattr,
3611 struct nfs4_label *label)
3612{
3613 struct nfs4_createdata *data;
3614 int status = -ENAMETOOLONG;
3615
3616 if (len > NFS4_MAXPATHLEN)
3617 goto out;
3618
3619 status = -ENOMEM;
3620 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3621 if (data == NULL)
3622 goto out;
3623
3624 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3625 data->arg.u.symlink.pages = &page;
3626 data->arg.u.symlink.len = len;
3627 data->arg.label = label;
3628
3629 status = nfs4_do_create(dir, dentry, data);
3630
3631 nfs4_free_createdata(data);
3632out:
3633 return status;
3634}
3635
3636static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3637 struct page *page, unsigned int len, struct iattr *sattr)
3638{
3639 struct nfs4_exception exception = { };
3640 struct nfs4_label l, *label = NULL;
3641 int err;
3642
3643 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3644
3645 do {
3646 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3647 trace_nfs4_symlink(dir, &dentry->d_name, err);
3648 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3649 &exception);
3650 } while (exception.retry);
3651
3652 nfs4_label_release_security(label);
3653 return err;
3654}
3655
3656static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3657 struct iattr *sattr, struct nfs4_label *label)
3658{
3659 struct nfs4_createdata *data;
3660 int status = -ENOMEM;
3661
3662 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3663 if (data == NULL)
3664 goto out;
3665
3666 data->arg.label = label;
3667 status = nfs4_do_create(dir, dentry, data);
3668
3669 nfs4_free_createdata(data);
3670out:
3671 return status;
3672}
3673
3674static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3675 struct iattr *sattr)
3676{
3677 struct nfs4_exception exception = { };
3678 struct nfs4_label l, *label = NULL;
3679 int err;
3680
3681 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3682
3683 sattr->ia_mode &= ~current_umask();
3684 do {
3685 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3686 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3687 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3688 &exception);
3689 } while (exception.retry);
3690 nfs4_label_release_security(label);
3691
3692 return err;
3693}
3694
3695static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3696 u64 cookie, struct page **pages, unsigned int count, int plus)
3697{
3698 struct inode *dir = dentry->d_inode;
3699 struct nfs4_readdir_arg args = {
3700 .fh = NFS_FH(dir),
3701 .pages = pages,
3702 .pgbase = 0,
3703 .count = count,
3704 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3705 .plus = plus,
3706 };
3707 struct nfs4_readdir_res res;
3708 struct rpc_message msg = {
3709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3710 .rpc_argp = &args,
3711 .rpc_resp = &res,
3712 .rpc_cred = cred,
3713 };
3714 int status;
3715
3716 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3717 dentry->d_parent->d_name.name,
3718 dentry->d_name.name,
3719 (unsigned long long)cookie);
3720 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3721 res.pgbase = args.pgbase;
3722 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3723 if (status >= 0) {
3724 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3725 status += args.pgbase;
3726 }
3727
3728 nfs_invalidate_atime(dir);
3729
3730 dprintk("%s: returns %d\n", __func__, status);
3731 return status;
3732}
3733
3734static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3735 u64 cookie, struct page **pages, unsigned int count, int plus)
3736{
3737 struct nfs4_exception exception = { };
3738 int err;
3739 do {
3740 err = _nfs4_proc_readdir(dentry, cred, cookie,
3741 pages, count, plus);
3742 trace_nfs4_readdir(dentry->d_inode, err);
3743 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
3744 &exception);
3745 } while (exception.retry);
3746 return err;
3747}
3748
3749static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3750 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
3751{
3752 struct nfs4_createdata *data;
3753 int mode = sattr->ia_mode;
3754 int status = -ENOMEM;
3755
3756 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3757 if (data == NULL)
3758 goto out;
3759
3760 if (S_ISFIFO(mode))
3761 data->arg.ftype = NF4FIFO;
3762 else if (S_ISBLK(mode)) {
3763 data->arg.ftype = NF4BLK;
3764 data->arg.u.device.specdata1 = MAJOR(rdev);
3765 data->arg.u.device.specdata2 = MINOR(rdev);
3766 }
3767 else if (S_ISCHR(mode)) {
3768 data->arg.ftype = NF4CHR;
3769 data->arg.u.device.specdata1 = MAJOR(rdev);
3770 data->arg.u.device.specdata2 = MINOR(rdev);
3771 } else if (!S_ISSOCK(mode)) {
3772 status = -EINVAL;
3773 goto out_free;
3774 }
3775
3776 data->arg.label = label;
3777 status = nfs4_do_create(dir, dentry, data);
3778out_free:
3779 nfs4_free_createdata(data);
3780out:
3781 return status;
3782}
3783
3784static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3785 struct iattr *sattr, dev_t rdev)
3786{
3787 struct nfs4_exception exception = { };
3788 struct nfs4_label l, *label = NULL;
3789 int err;
3790
3791 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3792
3793 sattr->ia_mode &= ~current_umask();
3794 do {
3795 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3796 trace_nfs4_mknod(dir, &dentry->d_name, err);
3797 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3798 &exception);
3799 } while (exception.retry);
3800
3801 nfs4_label_release_security(label);
3802
3803 return err;
3804}
3805
3806static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3807 struct nfs_fsstat *fsstat)
3808{
3809 struct nfs4_statfs_arg args = {
3810 .fh = fhandle,
3811 .bitmask = server->attr_bitmask,
3812 };
3813 struct nfs4_statfs_res res = {
3814 .fsstat = fsstat,
3815 };
3816 struct rpc_message msg = {
3817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3818 .rpc_argp = &args,
3819 .rpc_resp = &res,
3820 };
3821
3822 nfs_fattr_init(fsstat->fattr);
3823 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3824}
3825
3826static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3827{
3828 struct nfs4_exception exception = { };
3829 int err;
3830 do {
3831 err = nfs4_handle_exception(server,
3832 _nfs4_proc_statfs(server, fhandle, fsstat),
3833 &exception);
3834 } while (exception.retry);
3835 return err;
3836}
3837
3838static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3839 struct nfs_fsinfo *fsinfo)
3840{
3841 struct nfs4_fsinfo_arg args = {
3842 .fh = fhandle,
3843 .bitmask = server->attr_bitmask,
3844 };
3845 struct nfs4_fsinfo_res res = {
3846 .fsinfo = fsinfo,
3847 };
3848 struct rpc_message msg = {
3849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3850 .rpc_argp = &args,
3851 .rpc_resp = &res,
3852 };
3853
3854 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3855}
3856
3857static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3858{
3859 struct nfs4_exception exception = { };
3860 unsigned long now = jiffies;
3861 int err;
3862
3863 do {
3864 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3865 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
3866 if (err == 0) {
3867 struct nfs_client *clp = server->nfs_client;
3868
3869 spin_lock(&clp->cl_lock);
3870 clp->cl_lease_time = fsinfo->lease_time * HZ;
3871 clp->cl_last_renewal = now;
3872 spin_unlock(&clp->cl_lock);
3873 break;
3874 }
3875 err = nfs4_handle_exception(server, err, &exception);
3876 } while (exception.retry);
3877 return err;
3878}
3879
3880static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3881{
3882 int error;
3883
3884 nfs_fattr_init(fsinfo->fattr);
3885 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3886 if (error == 0) {
3887 /* block layout checks this! */
3888 server->pnfs_blksize = fsinfo->blksize;
3889 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3890 }
3891
3892 return error;
3893}
3894
3895static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3896 struct nfs_pathconf *pathconf)
3897{
3898 struct nfs4_pathconf_arg args = {
3899 .fh = fhandle,
3900 .bitmask = server->attr_bitmask,
3901 };
3902 struct nfs4_pathconf_res res = {
3903 .pathconf = pathconf,
3904 };
3905 struct rpc_message msg = {
3906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3907 .rpc_argp = &args,
3908 .rpc_resp = &res,
3909 };
3910
3911 /* None of the pathconf attributes are mandatory to implement */
3912 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3913 memset(pathconf, 0, sizeof(*pathconf));
3914 return 0;
3915 }
3916
3917 nfs_fattr_init(pathconf->fattr);
3918 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3919}
3920
3921static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3922 struct nfs_pathconf *pathconf)
3923{
3924 struct nfs4_exception exception = { };
3925 int err;
3926
3927 do {
3928 err = nfs4_handle_exception(server,
3929 _nfs4_proc_pathconf(server, fhandle, pathconf),
3930 &exception);
3931 } while (exception.retry);
3932 return err;
3933}
3934
3935int nfs4_set_rw_stateid(nfs4_stateid *stateid,
3936 const struct nfs_open_context *ctx,
3937 const struct nfs_lock_context *l_ctx,
3938 fmode_t fmode)
3939{
3940 const struct nfs_lockowner *lockowner = NULL;
3941
3942 if (l_ctx != NULL)
3943 lockowner = &l_ctx->lockowner;
3944 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
3945}
3946EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3947
3948static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3949 const struct nfs_open_context *ctx,
3950 const struct nfs_lock_context *l_ctx,
3951 fmode_t fmode)
3952{
3953 nfs4_stateid current_stateid;
3954
3955 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3956 return false;
3957 return nfs4_stateid_match(stateid, &current_stateid);
3958}
3959
3960static bool nfs4_error_stateid_expired(int err)
3961{
3962 switch (err) {
3963 case -NFS4ERR_DELEG_REVOKED:
3964 case -NFS4ERR_ADMIN_REVOKED:
3965 case -NFS4ERR_BAD_STATEID:
3966 case -NFS4ERR_STALE_STATEID:
3967 case -NFS4ERR_OLD_STATEID:
3968 case -NFS4ERR_OPENMODE:
3969 case -NFS4ERR_EXPIRED:
3970 return true;
3971 }
3972 return false;
3973}
3974
3975void __nfs4_read_done_cb(struct nfs_read_data *data)
3976{
3977 nfs_invalidate_atime(data->header->inode);
3978}
3979
3980static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3981{
3982 struct nfs_server *server = NFS_SERVER(data->header->inode);
3983
3984 trace_nfs4_read(data, task->tk_status);
3985 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3986 rpc_restart_call_prepare(task);
3987 return -EAGAIN;
3988 }
3989
3990 __nfs4_read_done_cb(data);
3991 if (task->tk_status > 0)
3992 renew_lease(server, data->timestamp);
3993 return 0;
3994}
3995
3996static bool nfs4_read_stateid_changed(struct rpc_task *task,
3997 struct nfs_readargs *args)
3998{
3999
4000 if (!nfs4_error_stateid_expired(task->tk_status) ||
4001 nfs4_stateid_is_current(&args->stateid,
4002 args->context,
4003 args->lock_context,
4004 FMODE_READ))
4005 return false;
4006 rpc_restart_call_prepare(task);
4007 return true;
4008}
4009
4010static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4011{
4012
4013 dprintk("--> %s\n", __func__);
4014
4015 if (!nfs4_sequence_done(task, &data->res.seq_res))
4016 return -EAGAIN;
4017 if (nfs4_read_stateid_changed(task, &data->args))
4018 return -EAGAIN;
4019 return data->read_done_cb ? data->read_done_cb(task, data) :
4020 nfs4_read_done_cb(task, data);
4021}
4022
4023static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
4024{
4025 data->timestamp = jiffies;
4026 data->read_done_cb = nfs4_read_done_cb;
4027 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4028 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
4029}
4030
4031static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
4032{
4033 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
4034 &data->args.seq_args,
4035 &data->res.seq_res,
4036 task))
4037 return 0;
4038 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4039 data->args.lock_context, FMODE_READ) == -EIO)
4040 return -EIO;
4041 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4042 return -EIO;
4043 return 0;
4044}
4045
4046static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
4047{
4048 struct inode *inode = data->header->inode;
4049
4050 trace_nfs4_write(data, task->tk_status);
4051 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
4052 rpc_restart_call_prepare(task);
4053 return -EAGAIN;
4054 }
4055 if (task->tk_status >= 0) {
4056 renew_lease(NFS_SERVER(inode), data->timestamp);
4057 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4058 }
4059 return 0;
4060}
4061
4062static bool nfs4_write_stateid_changed(struct rpc_task *task,
4063 struct nfs_writeargs *args)
4064{
4065
4066 if (!nfs4_error_stateid_expired(task->tk_status) ||
4067 nfs4_stateid_is_current(&args->stateid,
4068 args->context,
4069 args->lock_context,
4070 FMODE_WRITE))
4071 return false;
4072 rpc_restart_call_prepare(task);
4073 return true;
4074}
4075
4076static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4077{
4078 if (!nfs4_sequence_done(task, &data->res.seq_res))
4079 return -EAGAIN;
4080 if (nfs4_write_stateid_changed(task, &data->args))
4081 return -EAGAIN;
4082 return data->write_done_cb ? data->write_done_cb(task, data) :
4083 nfs4_write_done_cb(task, data);
4084}
4085
4086static
4087bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
4088{
4089 const struct nfs_pgio_header *hdr = data->header;
4090
4091 /* Don't request attributes for pNFS or O_DIRECT writes */
4092 if (data->ds_clp != NULL || hdr->dreq != NULL)
4093 return false;
4094 /* Otherwise, request attributes if and only if we don't hold
4095 * a delegation
4096 */
4097 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4098}
4099
4100static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
4101{
4102 struct nfs_server *server = NFS_SERVER(data->header->inode);
4103
4104 if (!nfs4_write_need_cache_consistency_data(data)) {
4105 data->args.bitmask = NULL;
4106 data->res.fattr = NULL;
4107 } else
4108 data->args.bitmask = server->cache_consistency_bitmask;
4109
4110 if (!data->write_done_cb)
4111 data->write_done_cb = nfs4_write_done_cb;
4112 data->res.server = server;
4113 data->timestamp = jiffies;
4114
4115 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4116 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4117}
4118
4119static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
4120{
4121 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
4122 &data->args.seq_args,
4123 &data->res.seq_res,
4124 task))
4125 return 0;
4126 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4127 data->args.lock_context, FMODE_WRITE) == -EIO)
4128 return -EIO;
4129 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4130 return -EIO;
4131 return 0;
4132}
4133
4134static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4135{
4136 nfs4_setup_sequence(NFS_SERVER(data->inode),
4137 &data->args.seq_args,
4138 &data->res.seq_res,
4139 task);
4140}
4141
4142static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4143{
4144 struct inode *inode = data->inode;
4145
4146 trace_nfs4_commit(data, task->tk_status);
4147 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
4148 rpc_restart_call_prepare(task);
4149 return -EAGAIN;
4150 }
4151 return 0;
4152}
4153
4154static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4155{
4156 if (!nfs4_sequence_done(task, &data->res.seq_res))
4157 return -EAGAIN;
4158 return data->commit_done_cb(task, data);
4159}
4160
4161static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4162{
4163 struct nfs_server *server = NFS_SERVER(data->inode);
4164
4165 if (data->commit_done_cb == NULL)
4166 data->commit_done_cb = nfs4_commit_done_cb;
4167 data->res.server = server;
4168 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4169 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4170}
4171
4172struct nfs4_renewdata {
4173 struct nfs_client *client;
4174 unsigned long timestamp;
4175};
4176
4177/*
4178 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4179 * standalone procedure for queueing an asynchronous RENEW.
4180 */
4181static void nfs4_renew_release(void *calldata)
4182{
4183 struct nfs4_renewdata *data = calldata;
4184 struct nfs_client *clp = data->client;
4185
4186 if (atomic_read(&clp->cl_count) > 1)
4187 nfs4_schedule_state_renewal(clp);
4188 nfs_put_client(clp);
4189 kfree(data);
4190}
4191
4192static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4193{
4194 struct nfs4_renewdata *data = calldata;
4195 struct nfs_client *clp = data->client;
4196 unsigned long timestamp = data->timestamp;
4197
4198 trace_nfs4_renew_async(clp, task->tk_status);
4199 if (task->tk_status < 0) {
4200 /* Unless we're shutting down, schedule state recovery! */
4201 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4202 return;
4203 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4204 nfs4_schedule_lease_recovery(clp);
4205 return;
4206 }
4207 nfs4_schedule_path_down_recovery(clp);
4208 }
4209 do_renew_lease(clp, timestamp);
4210}
4211
4212static const struct rpc_call_ops nfs4_renew_ops = {
4213 .rpc_call_done = nfs4_renew_done,
4214 .rpc_release = nfs4_renew_release,
4215};
4216
4217static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4218{
4219 struct rpc_message msg = {
4220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4221 .rpc_argp = clp,
4222 .rpc_cred = cred,
4223 };
4224 struct nfs4_renewdata *data;
4225
4226 if (renew_flags == 0)
4227 return 0;
4228 if (!atomic_inc_not_zero(&clp->cl_count))
4229 return -EIO;
4230 data = kmalloc(sizeof(*data), GFP_NOFS);
4231 if (data == NULL)
4232 return -ENOMEM;
4233 data->client = clp;
4234 data->timestamp = jiffies;
4235 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4236 &nfs4_renew_ops, data);
4237}
4238
4239static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4240{
4241 struct rpc_message msg = {
4242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4243 .rpc_argp = clp,
4244 .rpc_cred = cred,
4245 };
4246 unsigned long now = jiffies;
4247 int status;
4248
4249 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4250 if (status < 0)
4251 return status;
4252 do_renew_lease(clp, now);
4253 return 0;
4254}
4255
4256static inline int nfs4_server_supports_acls(struct nfs_server *server)
4257{
4258 return (server->caps & NFS_CAP_ACLS)
4259 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4260 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4261}
4262
4263/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4264 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4265 * the stack.
4266 */
4267#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4268
4269static int buf_to_pages_noslab(const void *buf, size_t buflen,
4270 struct page **pages, unsigned int *pgbase)
4271{
4272 struct page *newpage, **spages;
4273 int rc = 0;
4274 size_t len;
4275 spages = pages;
4276
4277 do {
4278 len = min_t(size_t, PAGE_SIZE, buflen);
4279 newpage = alloc_page(GFP_KERNEL);
4280
4281 if (newpage == NULL)
4282 goto unwind;
4283 memcpy(page_address(newpage), buf, len);
4284 buf += len;
4285 buflen -= len;
4286 *pages++ = newpage;
4287 rc++;
4288 } while (buflen != 0);
4289
4290 return rc;
4291
4292unwind:
4293 for(; rc > 0; rc--)
4294 __free_page(spages[rc-1]);
4295 return -ENOMEM;
4296}
4297
4298struct nfs4_cached_acl {
4299 int cached;
4300 size_t len;
4301 char data[0];
4302};
4303
4304static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4305{
4306 struct nfs_inode *nfsi = NFS_I(inode);
4307
4308 spin_lock(&inode->i_lock);
4309 kfree(nfsi->nfs4_acl);
4310 nfsi->nfs4_acl = acl;
4311 spin_unlock(&inode->i_lock);
4312}
4313
4314static void nfs4_zap_acl_attr(struct inode *inode)
4315{
4316 nfs4_set_cached_acl(inode, NULL);
4317}
4318
4319static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4320{
4321 struct nfs_inode *nfsi = NFS_I(inode);
4322 struct nfs4_cached_acl *acl;
4323 int ret = -ENOENT;
4324
4325 spin_lock(&inode->i_lock);
4326 acl = nfsi->nfs4_acl;
4327 if (acl == NULL)
4328 goto out;
4329 if (buf == NULL) /* user is just asking for length */
4330 goto out_len;
4331 if (acl->cached == 0)
4332 goto out;
4333 ret = -ERANGE; /* see getxattr(2) man page */
4334 if (acl->len > buflen)
4335 goto out;
4336 memcpy(buf, acl->data, acl->len);
4337out_len:
4338 ret = acl->len;
4339out:
4340 spin_unlock(&inode->i_lock);
4341 return ret;
4342}
4343
4344static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4345{
4346 struct nfs4_cached_acl *acl;
4347 size_t buflen = sizeof(*acl) + acl_len;
4348
4349 if (buflen <= PAGE_SIZE) {
4350 acl = kmalloc(buflen, GFP_KERNEL);
4351 if (acl == NULL)
4352 goto out;
4353 acl->cached = 1;
4354 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4355 } else {
4356 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4357 if (acl == NULL)
4358 goto out;
4359 acl->cached = 0;
4360 }
4361 acl->len = acl_len;
4362out:
4363 nfs4_set_cached_acl(inode, acl);
4364}
4365
4366/*
4367 * The getxattr API returns the required buffer length when called with a
4368 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4369 * the required buf. On a NULL buf, we send a page of data to the server
4370 * guessing that the ACL request can be serviced by a page. If so, we cache
4371 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4372 * the cache. If not so, we throw away the page, and cache the required
4373 * length. The next getxattr call will then produce another round trip to
4374 * the server, this time with the input buf of the required size.
4375 */
4376static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4377{
4378 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4379 struct nfs_getaclargs args = {
4380 .fh = NFS_FH(inode),
4381 .acl_pages = pages,
4382 .acl_len = buflen,
4383 };
4384 struct nfs_getaclres res = {
4385 .acl_len = buflen,
4386 };
4387 struct rpc_message msg = {
4388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4389 .rpc_argp = &args,
4390 .rpc_resp = &res,
4391 };
4392 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4393 int ret = -ENOMEM, i;
4394
4395 /* As long as we're doing a round trip to the server anyway,
4396 * let's be prepared for a page of acl data. */
4397 if (npages == 0)
4398 npages = 1;
4399 if (npages > ARRAY_SIZE(pages))
4400 return -ERANGE;
4401
4402 for (i = 0; i < npages; i++) {
4403 pages[i] = alloc_page(GFP_KERNEL);
4404 if (!pages[i])
4405 goto out_free;
4406 }
4407
4408 /* for decoding across pages */
4409 res.acl_scratch = alloc_page(GFP_KERNEL);
4410 if (!res.acl_scratch)
4411 goto out_free;
4412
4413 args.acl_len = npages * PAGE_SIZE;
4414 args.acl_pgbase = 0;
4415
4416 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4417 __func__, buf, buflen, npages, args.acl_len);
4418 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4419 &msg, &args.seq_args, &res.seq_res, 0);
4420 if (ret)
4421 goto out_free;
4422
4423 /* Handle the case where the passed-in buffer is too short */
4424 if (res.acl_flags & NFS4_ACL_TRUNC) {
4425 /* Did the user only issue a request for the acl length? */
4426 if (buf == NULL)
4427 goto out_ok;
4428 ret = -ERANGE;
4429 goto out_free;
4430 }
4431 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4432 if (buf) {
4433 if (res.acl_len > buflen) {
4434 ret = -ERANGE;
4435 goto out_free;
4436 }
4437 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4438 }
4439out_ok:
4440 ret = res.acl_len;
4441out_free:
4442 for (i = 0; i < npages; i++)
4443 if (pages[i])
4444 __free_page(pages[i]);
4445 if (res.acl_scratch)
4446 __free_page(res.acl_scratch);
4447 return ret;
4448}
4449
4450static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4451{
4452 struct nfs4_exception exception = { };
4453 ssize_t ret;
4454 do {
4455 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4456 trace_nfs4_get_acl(inode, ret);
4457 if (ret >= 0)
4458 break;
4459 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4460 } while (exception.retry);
4461 return ret;
4462}
4463
4464static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4465{
4466 struct nfs_server *server = NFS_SERVER(inode);
4467 int ret;
4468
4469 if (!nfs4_server_supports_acls(server))
4470 return -EOPNOTSUPP;
4471 ret = nfs_revalidate_inode(server, inode);
4472 if (ret < 0)
4473 return ret;
4474 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4475 nfs_zap_acl_cache(inode);
4476 ret = nfs4_read_cached_acl(inode, buf, buflen);
4477 if (ret != -ENOENT)
4478 /* -ENOENT is returned if there is no ACL or if there is an ACL
4479 * but no cached acl data, just the acl length */
4480 return ret;
4481 return nfs4_get_acl_uncached(inode, buf, buflen);
4482}
4483
4484static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4485{
4486 struct nfs_server *server = NFS_SERVER(inode);
4487 struct page *pages[NFS4ACL_MAXPAGES];
4488 struct nfs_setaclargs arg = {
4489 .fh = NFS_FH(inode),
4490 .acl_pages = pages,
4491 .acl_len = buflen,
4492 };
4493 struct nfs_setaclres res;
4494 struct rpc_message msg = {
4495 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4496 .rpc_argp = &arg,
4497 .rpc_resp = &res,
4498 };
4499 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4500 int ret, i;
4501
4502 if (!nfs4_server_supports_acls(server))
4503 return -EOPNOTSUPP;
4504 if (npages > ARRAY_SIZE(pages))
4505 return -ERANGE;
4506 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4507 if (i < 0)
4508 return i;
4509 nfs4_inode_return_delegation(inode);
4510 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4511
4512 /*
4513 * Free each page after tx, so the only ref left is
4514 * held by the network stack
4515 */
4516 for (; i > 0; i--)
4517 put_page(pages[i-1]);
4518
4519 /*
4520 * Acl update can result in inode attribute update.
4521 * so mark the attribute cache invalid.
4522 */
4523 spin_lock(&inode->i_lock);
4524 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4525 spin_unlock(&inode->i_lock);
4526 nfs_access_zap_cache(inode);
4527 nfs_zap_acl_cache(inode);
4528 return ret;
4529}
4530
4531static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4532{
4533 struct nfs4_exception exception = { };
4534 int err;
4535 do {
4536 err = __nfs4_proc_set_acl(inode, buf, buflen);
4537 trace_nfs4_set_acl(inode, err);
4538 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4539 &exception);
4540 } while (exception.retry);
4541 return err;
4542}
4543
4544#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4545static int _nfs4_get_security_label(struct inode *inode, void *buf,
4546 size_t buflen)
4547{
4548 struct nfs_server *server = NFS_SERVER(inode);
4549 struct nfs_fattr fattr;
4550 struct nfs4_label label = {0, 0, buflen, buf};
4551
4552 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4553 struct nfs4_getattr_arg args = {
4554 .fh = NFS_FH(inode),
4555 .bitmask = bitmask,
4556 };
4557 struct nfs4_getattr_res res = {
4558 .fattr = &fattr,
4559 .label = &label,
4560 .server = server,
4561 };
4562 struct rpc_message msg = {
4563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4564 .rpc_argp = &args,
4565 .rpc_resp = &res,
4566 };
4567 int ret;
4568
4569 nfs_fattr_init(&fattr);
4570
4571 ret = rpc_call_sync(server->client, &msg, 0);
4572 if (ret)
4573 return ret;
4574 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4575 return -ENOENT;
4576 if (buflen < label.len)
4577 return -ERANGE;
4578 return 0;
4579}
4580
4581static int nfs4_get_security_label(struct inode *inode, void *buf,
4582 size_t buflen)
4583{
4584 struct nfs4_exception exception = { };
4585 int err;
4586
4587 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4588 return -EOPNOTSUPP;
4589
4590 do {
4591 err = _nfs4_get_security_label(inode, buf, buflen);
4592 trace_nfs4_get_security_label(inode, err);
4593 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4594 &exception);
4595 } while (exception.retry);
4596 return err;
4597}
4598
4599static int _nfs4_do_set_security_label(struct inode *inode,
4600 struct nfs4_label *ilabel,
4601 struct nfs_fattr *fattr,
4602 struct nfs4_label *olabel)
4603{
4604
4605 struct iattr sattr = {0};
4606 struct nfs_server *server = NFS_SERVER(inode);
4607 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4608 struct nfs_setattrargs args = {
4609 .fh = NFS_FH(inode),
4610 .iap = &sattr,
4611 .server = server,
4612 .bitmask = bitmask,
4613 .label = ilabel,
4614 };
4615 struct nfs_setattrres res = {
4616 .fattr = fattr,
4617 .label = olabel,
4618 .server = server,
4619 };
4620 struct rpc_message msg = {
4621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4622 .rpc_argp = &args,
4623 .rpc_resp = &res,
4624 };
4625 int status;
4626
4627 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4628
4629 status = rpc_call_sync(server->client, &msg, 0);
4630 if (status)
4631 dprintk("%s failed: %d\n", __func__, status);
4632
4633 return status;
4634}
4635
4636static int nfs4_do_set_security_label(struct inode *inode,
4637 struct nfs4_label *ilabel,
4638 struct nfs_fattr *fattr,
4639 struct nfs4_label *olabel)
4640{
4641 struct nfs4_exception exception = { };
4642 int err;
4643
4644 do {
4645 err = _nfs4_do_set_security_label(inode, ilabel,
4646 fattr, olabel);
4647 trace_nfs4_set_security_label(inode, err);
4648 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4649 &exception);
4650 } while (exception.retry);
4651 return err;
4652}
4653
4654static int
4655nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4656{
4657 struct nfs4_label ilabel, *olabel = NULL;
4658 struct nfs_fattr fattr;
4659 struct rpc_cred *cred;
4660 struct inode *inode = dentry->d_inode;
4661 int status;
4662
4663 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4664 return -EOPNOTSUPP;
4665
4666 nfs_fattr_init(&fattr);
4667
4668 ilabel.pi = 0;
4669 ilabel.lfs = 0;
4670 ilabel.label = (char *)buf;
4671 ilabel.len = buflen;
4672
4673 cred = rpc_lookup_cred();
4674 if (IS_ERR(cred))
4675 return PTR_ERR(cred);
4676
4677 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4678 if (IS_ERR(olabel)) {
4679 status = -PTR_ERR(olabel);
4680 goto out;
4681 }
4682
4683 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4684 if (status == 0)
4685 nfs_setsecurity(inode, &fattr, olabel);
4686
4687 nfs4_label_free(olabel);
4688out:
4689 put_rpccred(cred);
4690 return status;
4691}
4692#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4693
4694
4695static int
4696nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4697{
4698 struct nfs_client *clp = server->nfs_client;
4699
4700 if (task->tk_status >= 0)
4701 return 0;
4702 switch(task->tk_status) {
4703 case -NFS4ERR_DELEG_REVOKED:
4704 case -NFS4ERR_ADMIN_REVOKED:
4705 case -NFS4ERR_BAD_STATEID:
4706 if (state == NULL)
4707 break;
4708 nfs_remove_bad_delegation(state->inode);
4709 case -NFS4ERR_OPENMODE:
4710 if (state == NULL)
4711 break;
4712 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4713 goto stateid_invalid;
4714 goto wait_on_recovery;
4715 case -NFS4ERR_EXPIRED:
4716 if (state != NULL) {
4717 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4718 goto stateid_invalid;
4719 }
4720 case -NFS4ERR_STALE_STATEID:
4721 case -NFS4ERR_STALE_CLIENTID:
4722 nfs4_schedule_lease_recovery(clp);
4723 goto wait_on_recovery;
4724#if defined(CONFIG_NFS_V4_1)
4725 case -NFS4ERR_BADSESSION:
4726 case -NFS4ERR_BADSLOT:
4727 case -NFS4ERR_BAD_HIGH_SLOT:
4728 case -NFS4ERR_DEADSESSION:
4729 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4730 case -NFS4ERR_SEQ_FALSE_RETRY:
4731 case -NFS4ERR_SEQ_MISORDERED:
4732 dprintk("%s ERROR %d, Reset session\n", __func__,
4733 task->tk_status);
4734 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4735 task->tk_status = 0;
4736 return -EAGAIN;
4737#endif /* CONFIG_NFS_V4_1 */
4738 case -NFS4ERR_DELAY:
4739 nfs_inc_server_stats(server, NFSIOS_DELAY);
4740 case -NFS4ERR_GRACE:
4741 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4742 task->tk_status = 0;
4743 return -EAGAIN;
4744 case -NFS4ERR_RETRY_UNCACHED_REP:
4745 case -NFS4ERR_OLD_STATEID:
4746 task->tk_status = 0;
4747 return -EAGAIN;
4748 }
4749 task->tk_status = nfs4_map_errors(task->tk_status);
4750 return 0;
4751stateid_invalid:
4752 task->tk_status = -EIO;
4753 return 0;
4754wait_on_recovery:
4755 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4756 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4757 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4758 task->tk_status = 0;
4759 return -EAGAIN;
4760}
4761
4762static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4763 nfs4_verifier *bootverf)
4764{
4765 __be32 verf[2];
4766
4767 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4768 /* An impossible timestamp guarantees this value
4769 * will never match a generated boot time. */
4770 verf[0] = 0;
4771 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
4772 } else {
4773 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4774 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4775 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
4776 }
4777 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4778}
4779
4780static unsigned int
4781nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4782 char *buf, size_t len)
4783{
4784 unsigned int result;
4785
4786 rcu_read_lock();
4787 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4788 clp->cl_ipaddr,
4789 rpc_peeraddr2str(clp->cl_rpcclient,
4790 RPC_DISPLAY_ADDR),
4791 rpc_peeraddr2str(clp->cl_rpcclient,
4792 RPC_DISPLAY_PROTO));
4793 rcu_read_unlock();
4794 return result;
4795}
4796
4797static unsigned int
4798nfs4_init_uniform_client_string(const struct nfs_client *clp,
4799 char *buf, size_t len)
4800{
4801 const char *nodename = clp->cl_rpcclient->cl_nodename;
4802
4803 if (nfs4_client_id_uniquifier[0] != '\0')
4804 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4805 clp->rpc_ops->version,
4806 clp->cl_minorversion,
4807 nfs4_client_id_uniquifier,
4808 nodename);
4809 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4810 clp->rpc_ops->version, clp->cl_minorversion,
4811 nodename);
4812}
4813
4814/**
4815 * nfs4_proc_setclientid - Negotiate client ID
4816 * @clp: state data structure
4817 * @program: RPC program for NFSv4 callback service
4818 * @port: IP port number for NFS4 callback service
4819 * @cred: RPC credential to use for this call
4820 * @res: where to place the result
4821 *
4822 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4823 */
4824int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4825 unsigned short port, struct rpc_cred *cred,
4826 struct nfs4_setclientid_res *res)
4827{
4828 nfs4_verifier sc_verifier;
4829 struct nfs4_setclientid setclientid = {
4830 .sc_verifier = &sc_verifier,
4831 .sc_prog = program,
4832 .sc_cb_ident = clp->cl_cb_ident,
4833 };
4834 struct rpc_message msg = {
4835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4836 .rpc_argp = &setclientid,
4837 .rpc_resp = res,
4838 .rpc_cred = cred,
4839 };
4840 int status;
4841
4842 /* nfs_client_id4 */
4843 nfs4_init_boot_verifier(clp, &sc_verifier);
4844 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4845 setclientid.sc_name_len =
4846 nfs4_init_uniform_client_string(clp,
4847 setclientid.sc_name,
4848 sizeof(setclientid.sc_name));
4849 else
4850 setclientid.sc_name_len =
4851 nfs4_init_nonuniform_client_string(clp,
4852 setclientid.sc_name,
4853 sizeof(setclientid.sc_name));
4854 /* cb_client4 */
4855 rcu_read_lock();
4856 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4857 sizeof(setclientid.sc_netid), "%s",
4858 rpc_peeraddr2str(clp->cl_rpcclient,
4859 RPC_DISPLAY_NETID));
4860 rcu_read_unlock();
4861 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4862 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4863 clp->cl_ipaddr, port >> 8, port & 255);
4864
4865 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4866 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4867 setclientid.sc_name_len, setclientid.sc_name);
4868 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4869 trace_nfs4_setclientid(clp, status);
4870 dprintk("NFS reply setclientid: %d\n", status);
4871 return status;
4872}
4873
4874/**
4875 * nfs4_proc_setclientid_confirm - Confirm client ID
4876 * @clp: state data structure
4877 * @res: result of a previous SETCLIENTID
4878 * @cred: RPC credential to use for this call
4879 *
4880 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4881 */
4882int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4883 struct nfs4_setclientid_res *arg,
4884 struct rpc_cred *cred)
4885{
4886 struct rpc_message msg = {
4887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4888 .rpc_argp = arg,
4889 .rpc_cred = cred,
4890 };
4891 int status;
4892
4893 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4894 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4895 clp->cl_clientid);
4896 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4897 trace_nfs4_setclientid_confirm(clp, status);
4898 dprintk("NFS reply setclientid_confirm: %d\n", status);
4899 return status;
4900}
4901
4902struct nfs4_delegreturndata {
4903 struct nfs4_delegreturnargs args;
4904 struct nfs4_delegreturnres res;
4905 struct nfs_fh fh;
4906 nfs4_stateid stateid;
4907 unsigned long timestamp;
4908 struct nfs_fattr fattr;
4909 int rpc_status;
4910};
4911
4912static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4913{
4914 struct nfs4_delegreturndata *data = calldata;
4915
4916 if (!nfs4_sequence_done(task, &data->res.seq_res))
4917 return;
4918
4919 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
4920 switch (task->tk_status) {
4921 case -NFS4ERR_STALE_STATEID:
4922 case -NFS4ERR_EXPIRED:
4923 case 0:
4924 renew_lease(data->res.server, data->timestamp);
4925 break;
4926 default:
4927 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4928 -EAGAIN) {
4929 rpc_restart_call_prepare(task);
4930 return;
4931 }
4932 }
4933 data->rpc_status = task->tk_status;
4934}
4935
4936static void nfs4_delegreturn_release(void *calldata)
4937{
4938 kfree(calldata);
4939}
4940
4941static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4942{
4943 struct nfs4_delegreturndata *d_data;
4944
4945 d_data = (struct nfs4_delegreturndata *)data;
4946
4947 nfs4_setup_sequence(d_data->res.server,
4948 &d_data->args.seq_args,
4949 &d_data->res.seq_res,
4950 task);
4951}
4952
4953static const struct rpc_call_ops nfs4_delegreturn_ops = {
4954 .rpc_call_prepare = nfs4_delegreturn_prepare,
4955 .rpc_call_done = nfs4_delegreturn_done,
4956 .rpc_release = nfs4_delegreturn_release,
4957};
4958
4959static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4960{
4961 struct nfs4_delegreturndata *data;
4962 struct nfs_server *server = NFS_SERVER(inode);
4963 struct rpc_task *task;
4964 struct rpc_message msg = {
4965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4966 .rpc_cred = cred,
4967 };
4968 struct rpc_task_setup task_setup_data = {
4969 .rpc_client = server->client,
4970 .rpc_message = &msg,
4971 .callback_ops = &nfs4_delegreturn_ops,
4972 .flags = RPC_TASK_ASYNC,
4973 };
4974 int status = 0;
4975
4976 data = kzalloc(sizeof(*data), GFP_NOFS);
4977 if (data == NULL)
4978 return -ENOMEM;
4979 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4980 data->args.fhandle = &data->fh;
4981 data->args.stateid = &data->stateid;
4982 data->args.bitmask = server->cache_consistency_bitmask;
4983 nfs_copy_fh(&data->fh, NFS_FH(inode));
4984 nfs4_stateid_copy(&data->stateid, stateid);
4985 data->res.fattr = &data->fattr;
4986 data->res.server = server;
4987 nfs_fattr_init(data->res.fattr);
4988 data->timestamp = jiffies;
4989 data->rpc_status = 0;
4990
4991 task_setup_data.callback_data = data;
4992 msg.rpc_argp = &data->args;
4993 msg.rpc_resp = &data->res;
4994 task = rpc_run_task(&task_setup_data);
4995 if (IS_ERR(task))
4996 return PTR_ERR(task);
4997 if (!issync)
4998 goto out;
4999 status = nfs4_wait_for_completion_rpc_task(task);
5000 if (status != 0)
5001 goto out;
5002 status = data->rpc_status;
5003 if (status == 0)
5004 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5005 else
5006 nfs_refresh_inode(inode, &data->fattr);
5007out:
5008 rpc_put_task(task);
5009 return status;
5010}
5011
5012int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5013{
5014 struct nfs_server *server = NFS_SERVER(inode);
5015 struct nfs4_exception exception = { };
5016 int err;
5017 do {
5018 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5019 trace_nfs4_delegreturn(inode, err);
5020 switch (err) {
5021 case -NFS4ERR_STALE_STATEID:
5022 case -NFS4ERR_EXPIRED:
5023 case 0:
5024 return 0;
5025 }
5026 err = nfs4_handle_exception(server, err, &exception);
5027 } while (exception.retry);
5028 return err;
5029}
5030
5031#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5032#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5033
5034/*
5035 * sleep, with exponential backoff, and retry the LOCK operation.
5036 */
5037static unsigned long
5038nfs4_set_lock_task_retry(unsigned long timeout)
5039{
5040 freezable_schedule_timeout_killable_unsafe(timeout);
5041 timeout <<= 1;
5042 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5043 return NFS4_LOCK_MAXTIMEOUT;
5044 return timeout;
5045}
5046
5047static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5048{
5049 struct inode *inode = state->inode;
5050 struct nfs_server *server = NFS_SERVER(inode);
5051 struct nfs_client *clp = server->nfs_client;
5052 struct nfs_lockt_args arg = {
5053 .fh = NFS_FH(inode),
5054 .fl = request,
5055 };
5056 struct nfs_lockt_res res = {
5057 .denied = request,
5058 };
5059 struct rpc_message msg = {
5060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5061 .rpc_argp = &arg,
5062 .rpc_resp = &res,
5063 .rpc_cred = state->owner->so_cred,
5064 };
5065 struct nfs4_lock_state *lsp;
5066 int status;
5067
5068 arg.lock_owner.clientid = clp->cl_clientid;
5069 status = nfs4_set_lock_state(state, request);
5070 if (status != 0)
5071 goto out;
5072 lsp = request->fl_u.nfs4_fl.owner;
5073 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5074 arg.lock_owner.s_dev = server->s_dev;
5075 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5076 switch (status) {
5077 case 0:
5078 request->fl_type = F_UNLCK;
5079 break;
5080 case -NFS4ERR_DENIED:
5081 status = 0;
5082 }
5083 request->fl_ops->fl_release_private(request);
5084out:
5085 return status;
5086}
5087
5088static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5089{
5090 struct nfs4_exception exception = { };
5091 int err;
5092
5093 do {
5094 err = _nfs4_proc_getlk(state, cmd, request);
5095 trace_nfs4_get_lock(request, state, cmd, err);
5096 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5097 &exception);
5098 } while (exception.retry);
5099 return err;
5100}
5101
5102static int do_vfs_lock(struct file *file, struct file_lock *fl)
5103{
5104 int res = 0;
5105 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5106 case FL_POSIX:
5107 res = posix_lock_file_wait(file, fl);
5108 break;
5109 case FL_FLOCK:
5110 res = flock_lock_file_wait(file, fl);
5111 break;
5112 default:
5113 BUG();
5114 }
5115 return res;
5116}
5117
5118struct nfs4_unlockdata {
5119 struct nfs_locku_args arg;
5120 struct nfs_locku_res res;
5121 struct nfs4_lock_state *lsp;
5122 struct nfs_open_context *ctx;
5123 struct file_lock fl;
5124 const struct nfs_server *server;
5125 unsigned long timestamp;
5126};
5127
5128static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5129 struct nfs_open_context *ctx,
5130 struct nfs4_lock_state *lsp,
5131 struct nfs_seqid *seqid)
5132{
5133 struct nfs4_unlockdata *p;
5134 struct inode *inode = lsp->ls_state->inode;
5135
5136 p = kzalloc(sizeof(*p), GFP_NOFS);
5137 if (p == NULL)
5138 return NULL;
5139 p->arg.fh = NFS_FH(inode);
5140 p->arg.fl = &p->fl;
5141 p->arg.seqid = seqid;
5142 p->res.seqid = seqid;
5143 p->arg.stateid = &lsp->ls_stateid;
5144 p->lsp = lsp;
5145 atomic_inc(&lsp->ls_count);
5146 /* Ensure we don't close file until we're done freeing locks! */
5147 p->ctx = get_nfs_open_context(ctx);
5148 memcpy(&p->fl, fl, sizeof(p->fl));
5149 p->server = NFS_SERVER(inode);
5150 return p;
5151}
5152
5153static void nfs4_locku_release_calldata(void *data)
5154{
5155 struct nfs4_unlockdata *calldata = data;
5156 nfs_free_seqid(calldata->arg.seqid);
5157 nfs4_put_lock_state(calldata->lsp);
5158 put_nfs_open_context(calldata->ctx);
5159 kfree(calldata);
5160}
5161
5162static void nfs4_locku_done(struct rpc_task *task, void *data)
5163{
5164 struct nfs4_unlockdata *calldata = data;
5165
5166 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5167 return;
5168 switch (task->tk_status) {
5169 case 0:
5170 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5171 &calldata->res.stateid);
5172 renew_lease(calldata->server, calldata->timestamp);
5173 break;
5174 case -NFS4ERR_BAD_STATEID:
5175 case -NFS4ERR_OLD_STATEID:
5176 case -NFS4ERR_STALE_STATEID:
5177 case -NFS4ERR_EXPIRED:
5178 break;
5179 default:
5180 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
5181 rpc_restart_call_prepare(task);
5182 }
5183 nfs_release_seqid(calldata->arg.seqid);
5184}
5185
5186static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5187{
5188 struct nfs4_unlockdata *calldata = data;
5189
5190 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5191 goto out_wait;
5192 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5193 /* Note: exit _without_ running nfs4_locku_done */
5194 goto out_no_action;
5195 }
5196 calldata->timestamp = jiffies;
5197 if (nfs4_setup_sequence(calldata->server,
5198 &calldata->arg.seq_args,
5199 &calldata->res.seq_res,
5200 task) != 0)
5201 nfs_release_seqid(calldata->arg.seqid);
5202 return;
5203out_no_action:
5204 task->tk_action = NULL;
5205out_wait:
5206 nfs4_sequence_done(task, &calldata->res.seq_res);
5207}
5208
5209static const struct rpc_call_ops nfs4_locku_ops = {
5210 .rpc_call_prepare = nfs4_locku_prepare,
5211 .rpc_call_done = nfs4_locku_done,
5212 .rpc_release = nfs4_locku_release_calldata,
5213};
5214
5215static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5216 struct nfs_open_context *ctx,
5217 struct nfs4_lock_state *lsp,
5218 struct nfs_seqid *seqid)
5219{
5220 struct nfs4_unlockdata *data;
5221 struct rpc_message msg = {
5222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5223 .rpc_cred = ctx->cred,
5224 };
5225 struct rpc_task_setup task_setup_data = {
5226 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5227 .rpc_message = &msg,
5228 .callback_ops = &nfs4_locku_ops,
5229 .workqueue = nfsiod_workqueue,
5230 .flags = RPC_TASK_ASYNC,
5231 };
5232
5233 /* Ensure this is an unlock - when canceling a lock, the
5234 * canceled lock is passed in, and it won't be an unlock.
5235 */
5236 fl->fl_type = F_UNLCK;
5237
5238 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5239 if (data == NULL) {
5240 nfs_free_seqid(seqid);
5241 return ERR_PTR(-ENOMEM);
5242 }
5243
5244 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5245 msg.rpc_argp = &data->arg;
5246 msg.rpc_resp = &data->res;
5247 task_setup_data.callback_data = data;
5248 return rpc_run_task(&task_setup_data);
5249}
5250
5251static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5252{
5253 struct inode *inode = state->inode;
5254 struct nfs4_state_owner *sp = state->owner;
5255 struct nfs_inode *nfsi = NFS_I(inode);
5256 struct nfs_seqid *seqid;
5257 struct nfs4_lock_state *lsp;
5258 struct rpc_task *task;
5259 int status = 0;
5260 unsigned char fl_flags = request->fl_flags;
5261
5262 status = nfs4_set_lock_state(state, request);
5263 /* Unlock _before_ we do the RPC call */
5264 request->fl_flags |= FL_EXISTS;
5265 /* Exclude nfs_delegation_claim_locks() */
5266 mutex_lock(&sp->so_delegreturn_mutex);
5267 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5268 down_read(&nfsi->rwsem);
5269 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5270 up_read(&nfsi->rwsem);
5271 mutex_unlock(&sp->so_delegreturn_mutex);
5272 goto out;
5273 }
5274 up_read(&nfsi->rwsem);
5275 mutex_unlock(&sp->so_delegreturn_mutex);
5276 if (status != 0)
5277 goto out;
5278 /* Is this a delegated lock? */
5279 lsp = request->fl_u.nfs4_fl.owner;
5280 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5281 goto out;
5282 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5283 status = -ENOMEM;
5284 if (seqid == NULL)
5285 goto out;
5286 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5287 status = PTR_ERR(task);
5288 if (IS_ERR(task))
5289 goto out;
5290 status = nfs4_wait_for_completion_rpc_task(task);
5291 rpc_put_task(task);
5292out:
5293 request->fl_flags = fl_flags;
5294 trace_nfs4_unlock(request, state, F_SETLK, status);
5295 return status;
5296}
5297
5298struct nfs4_lockdata {
5299 struct nfs_lock_args arg;
5300 struct nfs_lock_res res;
5301 struct nfs4_lock_state *lsp;
5302 struct nfs_open_context *ctx;
5303 struct file_lock fl;
5304 unsigned long timestamp;
5305 int rpc_status;
5306 int cancelled;
5307 struct nfs_server *server;
5308};
5309
5310static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5311 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5312 gfp_t gfp_mask)
5313{
5314 struct nfs4_lockdata *p;
5315 struct inode *inode = lsp->ls_state->inode;
5316 struct nfs_server *server = NFS_SERVER(inode);
5317
5318 p = kzalloc(sizeof(*p), gfp_mask);
5319 if (p == NULL)
5320 return NULL;
5321
5322 p->arg.fh = NFS_FH(inode);
5323 p->arg.fl = &p->fl;
5324 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5325 if (p->arg.open_seqid == NULL)
5326 goto out_free;
5327 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
5328 if (p->arg.lock_seqid == NULL)
5329 goto out_free_seqid;
5330 p->arg.lock_stateid = &lsp->ls_stateid;
5331 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5332 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5333 p->arg.lock_owner.s_dev = server->s_dev;
5334 p->res.lock_seqid = p->arg.lock_seqid;
5335 p->lsp = lsp;
5336 p->server = server;
5337 atomic_inc(&lsp->ls_count);
5338 p->ctx = get_nfs_open_context(ctx);
5339 memcpy(&p->fl, fl, sizeof(p->fl));
5340 return p;
5341out_free_seqid:
5342 nfs_free_seqid(p->arg.open_seqid);
5343out_free:
5344 kfree(p);
5345 return NULL;
5346}
5347
5348static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5349{
5350 struct nfs4_lockdata *data = calldata;
5351 struct nfs4_state *state = data->lsp->ls_state;
5352
5353 dprintk("%s: begin!\n", __func__);
5354 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5355 goto out_wait;
5356 /* Do we need to do an open_to_lock_owner? */
5357 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
5358 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5359 goto out_release_lock_seqid;
5360 }
5361 data->arg.open_stateid = &state->open_stateid;
5362 data->arg.new_lock_owner = 1;
5363 data->res.open_seqid = data->arg.open_seqid;
5364 } else
5365 data->arg.new_lock_owner = 0;
5366 if (!nfs4_valid_open_stateid(state)) {
5367 data->rpc_status = -EBADF;
5368 task->tk_action = NULL;
5369 goto out_release_open_seqid;
5370 }
5371 data->timestamp = jiffies;
5372 if (nfs4_setup_sequence(data->server,
5373 &data->arg.seq_args,
5374 &data->res.seq_res,
5375 task) == 0)
5376 return;
5377out_release_open_seqid:
5378 nfs_release_seqid(data->arg.open_seqid);
5379out_release_lock_seqid:
5380 nfs_release_seqid(data->arg.lock_seqid);
5381out_wait:
5382 nfs4_sequence_done(task, &data->res.seq_res);
5383 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5384}
5385
5386static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5387{
5388 struct nfs4_lockdata *data = calldata;
5389
5390 dprintk("%s: begin!\n", __func__);
5391
5392 if (!nfs4_sequence_done(task, &data->res.seq_res))
5393 return;
5394
5395 data->rpc_status = task->tk_status;
5396 if (data->arg.new_lock_owner != 0) {
5397 if (data->rpc_status == 0)
5398 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5399 else
5400 goto out;
5401 }
5402 if (data->rpc_status == 0) {
5403 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
5404 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
5405 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
5406 }
5407out:
5408 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5409}
5410
5411static void nfs4_lock_release(void *calldata)
5412{
5413 struct nfs4_lockdata *data = calldata;
5414
5415 dprintk("%s: begin!\n", __func__);
5416 nfs_free_seqid(data->arg.open_seqid);
5417 if (data->cancelled != 0) {
5418 struct rpc_task *task;
5419 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5420 data->arg.lock_seqid);
5421 if (!IS_ERR(task))
5422 rpc_put_task_async(task);
5423 dprintk("%s: cancelling lock!\n", __func__);
5424 } else
5425 nfs_free_seqid(data->arg.lock_seqid);
5426 nfs4_put_lock_state(data->lsp);
5427 put_nfs_open_context(data->ctx);
5428 kfree(data);
5429 dprintk("%s: done!\n", __func__);
5430}
5431
5432static const struct rpc_call_ops nfs4_lock_ops = {
5433 .rpc_call_prepare = nfs4_lock_prepare,
5434 .rpc_call_done = nfs4_lock_done,
5435 .rpc_release = nfs4_lock_release,
5436};
5437
5438static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5439{
5440 switch (error) {
5441 case -NFS4ERR_ADMIN_REVOKED:
5442 case -NFS4ERR_BAD_STATEID:
5443 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5444 if (new_lock_owner != 0 ||
5445 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5446 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5447 break;
5448 case -NFS4ERR_STALE_STATEID:
5449 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5450 case -NFS4ERR_EXPIRED:
5451 nfs4_schedule_lease_recovery(server->nfs_client);
5452 };
5453}
5454
5455static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5456{
5457 struct nfs4_lockdata *data;
5458 struct rpc_task *task;
5459 struct rpc_message msg = {
5460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5461 .rpc_cred = state->owner->so_cred,
5462 };
5463 struct rpc_task_setup task_setup_data = {
5464 .rpc_client = NFS_CLIENT(state->inode),
5465 .rpc_message = &msg,
5466 .callback_ops = &nfs4_lock_ops,
5467 .workqueue = nfsiod_workqueue,
5468 .flags = RPC_TASK_ASYNC,
5469 };
5470 int ret;
5471
5472 dprintk("%s: begin!\n", __func__);
5473 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5474 fl->fl_u.nfs4_fl.owner,
5475 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5476 if (data == NULL)
5477 return -ENOMEM;
5478 if (IS_SETLKW(cmd))
5479 data->arg.block = 1;
5480 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5481 msg.rpc_argp = &data->arg;
5482 msg.rpc_resp = &data->res;
5483 task_setup_data.callback_data = data;
5484 if (recovery_type > NFS_LOCK_NEW) {
5485 if (recovery_type == NFS_LOCK_RECLAIM)
5486 data->arg.reclaim = NFS_LOCK_RECLAIM;
5487 nfs4_set_sequence_privileged(&data->arg.seq_args);
5488 }
5489 task = rpc_run_task(&task_setup_data);
5490 if (IS_ERR(task))
5491 return PTR_ERR(task);
5492 ret = nfs4_wait_for_completion_rpc_task(task);
5493 if (ret == 0) {
5494 ret = data->rpc_status;
5495 if (ret)
5496 nfs4_handle_setlk_error(data->server, data->lsp,
5497 data->arg.new_lock_owner, ret);
5498 } else
5499 data->cancelled = 1;
5500 rpc_put_task(task);
5501 dprintk("%s: done, ret = %d!\n", __func__, ret);
5502 return ret;
5503}
5504
5505static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5506{
5507 struct nfs_server *server = NFS_SERVER(state->inode);
5508 struct nfs4_exception exception = {
5509 .inode = state->inode,
5510 };
5511 int err;
5512
5513 do {
5514 /* Cache the lock if possible... */
5515 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5516 return 0;
5517 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5518 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
5519 if (err != -NFS4ERR_DELAY)
5520 break;
5521 nfs4_handle_exception(server, err, &exception);
5522 } while (exception.retry);
5523 return err;
5524}
5525
5526static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5527{
5528 struct nfs_server *server = NFS_SERVER(state->inode);
5529 struct nfs4_exception exception = {
5530 .inode = state->inode,
5531 };
5532 int err;
5533
5534 err = nfs4_set_lock_state(state, request);
5535 if (err != 0)
5536 return err;
5537 if (!recover_lost_locks) {
5538 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5539 return 0;
5540 }
5541 do {
5542 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5543 return 0;
5544 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
5545 trace_nfs4_lock_expired(request, state, F_SETLK, err);
5546 switch (err) {
5547 default:
5548 goto out;
5549 case -NFS4ERR_GRACE:
5550 case -NFS4ERR_DELAY:
5551 nfs4_handle_exception(server, err, &exception);
5552 err = 0;
5553 }
5554 } while (exception.retry);
5555out:
5556 return err;
5557}
5558
5559#if defined(CONFIG_NFS_V4_1)
5560/**
5561 * nfs41_check_expired_locks - possibly free a lock stateid
5562 *
5563 * @state: NFSv4 state for an inode
5564 *
5565 * Returns NFS_OK if recovery for this stateid is now finished.
5566 * Otherwise a negative NFS4ERR value is returned.
5567 */
5568static int nfs41_check_expired_locks(struct nfs4_state *state)
5569{
5570 int status, ret = -NFS4ERR_BAD_STATEID;
5571 struct nfs4_lock_state *lsp;
5572 struct nfs_server *server = NFS_SERVER(state->inode);
5573
5574 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
5575 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
5576 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5577
5578 status = nfs41_test_stateid(server,
5579 &lsp->ls_stateid,
5580 cred);
5581 trace_nfs4_test_lock_stateid(state, lsp, status);
5582 if (status != NFS_OK) {
5583 /* Free the stateid unless the server
5584 * informs us the stateid is unrecognized. */
5585 if (status != -NFS4ERR_BAD_STATEID)
5586 nfs41_free_stateid(server,
5587 &lsp->ls_stateid,
5588 cred);
5589 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5590 ret = status;
5591 }
5592 }
5593 };
5594
5595 return ret;
5596}
5597
5598static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5599{
5600 int status = NFS_OK;
5601
5602 if (test_bit(LK_STATE_IN_USE, &state->flags))
5603 status = nfs41_check_expired_locks(state);
5604 if (status != NFS_OK)
5605 status = nfs4_lock_expired(state, request);
5606 return status;
5607}
5608#endif
5609
5610static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5611{
5612 struct nfs4_state_owner *sp = state->owner;
5613 struct nfs_inode *nfsi = NFS_I(state->inode);
5614 unsigned char fl_flags = request->fl_flags;
5615 unsigned int seq;
5616 int status = -ENOLCK;
5617
5618 if ((fl_flags & FL_POSIX) &&
5619 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5620 goto out;
5621 /* Is this a delegated open? */
5622 status = nfs4_set_lock_state(state, request);
5623 if (status != 0)
5624 goto out;
5625 request->fl_flags |= FL_ACCESS;
5626 status = do_vfs_lock(request->fl_file, request);
5627 if (status < 0)
5628 goto out;
5629 down_read(&nfsi->rwsem);
5630 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5631 /* Yes: cache locks! */
5632 /* ...but avoid races with delegation recall... */
5633 request->fl_flags = fl_flags & ~FL_SLEEP;
5634 status = do_vfs_lock(request->fl_file, request);
5635 goto out_unlock;
5636 }
5637 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5638 up_read(&nfsi->rwsem);
5639 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5640 if (status != 0)
5641 goto out;
5642 down_read(&nfsi->rwsem);
5643 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5644 status = -NFS4ERR_DELAY;
5645 goto out_unlock;
5646 }
5647 /* Note: we always want to sleep here! */
5648 request->fl_flags = fl_flags | FL_SLEEP;
5649 if (do_vfs_lock(request->fl_file, request) < 0)
5650 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5651 "manager!\n", __func__);
5652out_unlock:
5653 up_read(&nfsi->rwsem);
5654out:
5655 request->fl_flags = fl_flags;
5656 return status;
5657}
5658
5659static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5660{
5661 struct nfs4_exception exception = {
5662 .state = state,
5663 .inode = state->inode,
5664 };
5665 int err;
5666
5667 do {
5668 err = _nfs4_proc_setlk(state, cmd, request);
5669 trace_nfs4_set_lock(request, state, cmd, err);
5670 if (err == -NFS4ERR_DENIED)
5671 err = -EAGAIN;
5672 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5673 err, &exception);
5674 } while (exception.retry);
5675 return err;
5676}
5677
5678static int
5679nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5680{
5681 struct nfs_open_context *ctx;
5682 struct nfs4_state *state;
5683 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5684 int status;
5685
5686 /* verify open state */
5687 ctx = nfs_file_open_context(filp);
5688 state = ctx->state;
5689
5690 if (request->fl_start < 0 || request->fl_end < 0)
5691 return -EINVAL;
5692
5693 if (IS_GETLK(cmd)) {
5694 if (state != NULL)
5695 return nfs4_proc_getlk(state, F_GETLK, request);
5696 return 0;
5697 }
5698
5699 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5700 return -EINVAL;
5701
5702 if (request->fl_type == F_UNLCK) {
5703 if (state != NULL)
5704 return nfs4_proc_unlck(state, cmd, request);
5705 return 0;
5706 }
5707
5708 if (state == NULL)
5709 return -ENOLCK;
5710 /*
5711 * Don't rely on the VFS having checked the file open mode,
5712 * since it won't do this for flock() locks.
5713 */
5714 switch (request->fl_type) {
5715 case F_RDLCK:
5716 if (!(filp->f_mode & FMODE_READ))
5717 return -EBADF;
5718 break;
5719 case F_WRLCK:
5720 if (!(filp->f_mode & FMODE_WRITE))
5721 return -EBADF;
5722 }
5723
5724 do {
5725 status = nfs4_proc_setlk(state, cmd, request);
5726 if ((status != -EAGAIN) || IS_SETLK(cmd))
5727 break;
5728 timeout = nfs4_set_lock_task_retry(timeout);
5729 status = -ERESTARTSYS;
5730 if (signalled())
5731 break;
5732 } while(status < 0);
5733 return status;
5734}
5735
5736int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
5737{
5738 struct nfs_server *server = NFS_SERVER(state->inode);
5739 int err;
5740
5741 err = nfs4_set_lock_state(state, fl);
5742 if (err != 0)
5743 return err;
5744 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5745 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
5746}
5747
5748struct nfs_release_lockowner_data {
5749 struct nfs4_lock_state *lsp;
5750 struct nfs_server *server;
5751 struct nfs_release_lockowner_args args;
5752 struct nfs4_sequence_args seq_args;
5753 struct nfs4_sequence_res seq_res;
5754};
5755
5756static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5757{
5758 struct nfs_release_lockowner_data *data = calldata;
5759 nfs40_setup_sequence(data->server,
5760 &data->seq_args, &data->seq_res, task);
5761}
5762
5763static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5764{
5765 struct nfs_release_lockowner_data *data = calldata;
5766 nfs40_sequence_done(task, &data->seq_res);
5767}
5768
5769static void nfs4_release_lockowner_release(void *calldata)
5770{
5771 struct nfs_release_lockowner_data *data = calldata;
5772 nfs4_free_lock_state(data->server, data->lsp);
5773 kfree(calldata);
5774}
5775
5776static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5777 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5778 .rpc_call_done = nfs4_release_lockowner_done,
5779 .rpc_release = nfs4_release_lockowner_release,
5780};
5781
5782static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
5783{
5784 struct nfs_release_lockowner_data *data;
5785 struct rpc_message msg = {
5786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5787 };
5788
5789 if (server->nfs_client->cl_mvops->minor_version != 0)
5790 return -EINVAL;
5791
5792 data = kmalloc(sizeof(*data), GFP_NOFS);
5793 if (!data)
5794 return -ENOMEM;
5795 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
5796 data->lsp = lsp;
5797 data->server = server;
5798 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5799 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5800 data->args.lock_owner.s_dev = server->s_dev;
5801
5802 msg.rpc_argp = &data->args;
5803 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5804 return 0;
5805}
5806
5807#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5808
5809static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5810 const void *buf, size_t buflen,
5811 int flags, int type)
5812{
5813 if (strcmp(key, "") != 0)
5814 return -EINVAL;
5815
5816 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5817}
5818
5819static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5820 void *buf, size_t buflen, int type)
5821{
5822 if (strcmp(key, "") != 0)
5823 return -EINVAL;
5824
5825 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5826}
5827
5828static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5829 size_t list_len, const char *name,
5830 size_t name_len, int type)
5831{
5832 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5833
5834 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5835 return 0;
5836
5837 if (list && len <= list_len)
5838 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5839 return len;
5840}
5841
5842#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5843static inline int nfs4_server_supports_labels(struct nfs_server *server)
5844{
5845 return server->caps & NFS_CAP_SECURITY_LABEL;
5846}
5847
5848static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5849 const void *buf, size_t buflen,
5850 int flags, int type)
5851{
5852 if (security_ismaclabel(key))
5853 return nfs4_set_security_label(dentry, buf, buflen);
5854
5855 return -EOPNOTSUPP;
5856}
5857
5858static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5859 void *buf, size_t buflen, int type)
5860{
5861 if (security_ismaclabel(key))
5862 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5863 return -EOPNOTSUPP;
5864}
5865
5866static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5867 size_t list_len, const char *name,
5868 size_t name_len, int type)
5869{
5870 size_t len = 0;
5871
5872 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5873 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5874 if (list && len <= list_len)
5875 security_inode_listsecurity(dentry->d_inode, list, len);
5876 }
5877 return len;
5878}
5879
5880static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5881 .prefix = XATTR_SECURITY_PREFIX,
5882 .list = nfs4_xattr_list_nfs4_label,
5883 .get = nfs4_xattr_get_nfs4_label,
5884 .set = nfs4_xattr_set_nfs4_label,
5885};
5886#endif
5887
5888
5889/*
5890 * nfs_fhget will use either the mounted_on_fileid or the fileid
5891 */
5892static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5893{
5894 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5895 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5896 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5897 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5898 return;
5899
5900 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5901 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5902 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5903 fattr->nlink = 2;
5904}
5905
5906static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5907 const struct qstr *name,
5908 struct nfs4_fs_locations *fs_locations,
5909 struct page *page)
5910{
5911 struct nfs_server *server = NFS_SERVER(dir);
5912 u32 bitmask[3] = {
5913 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5914 };
5915 struct nfs4_fs_locations_arg args = {
5916 .dir_fh = NFS_FH(dir),
5917 .name = name,
5918 .page = page,
5919 .bitmask = bitmask,
5920 };
5921 struct nfs4_fs_locations_res res = {
5922 .fs_locations = fs_locations,
5923 };
5924 struct rpc_message msg = {
5925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5926 .rpc_argp = &args,
5927 .rpc_resp = &res,
5928 };
5929 int status;
5930
5931 dprintk("%s: start\n", __func__);
5932
5933 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5934 * is not supported */
5935 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5936 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5937 else
5938 bitmask[0] |= FATTR4_WORD0_FILEID;
5939
5940 nfs_fattr_init(&fs_locations->fattr);
5941 fs_locations->server = server;
5942 fs_locations->nlocations = 0;
5943 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5944 dprintk("%s: returned status = %d\n", __func__, status);
5945 return status;
5946}
5947
5948int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5949 const struct qstr *name,
5950 struct nfs4_fs_locations *fs_locations,
5951 struct page *page)
5952{
5953 struct nfs4_exception exception = { };
5954 int err;
5955 do {
5956 err = _nfs4_proc_fs_locations(client, dir, name,
5957 fs_locations, page);
5958 trace_nfs4_get_fs_locations(dir, name, err);
5959 err = nfs4_handle_exception(NFS_SERVER(dir), err,
5960 &exception);
5961 } while (exception.retry);
5962 return err;
5963}
5964
5965/**
5966 * If 'use_integrity' is true and the state managment nfs_client
5967 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
5968 * and the machine credential as per RFC3530bis and RFC5661 Security
5969 * Considerations sections. Otherwise, just use the user cred with the
5970 * filesystem's rpc_client.
5971 */
5972static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
5973{
5974 int status;
5975 struct nfs4_secinfo_arg args = {
5976 .dir_fh = NFS_FH(dir),
5977 .name = name,
5978 };
5979 struct nfs4_secinfo_res res = {
5980 .flavors = flavors,
5981 };
5982 struct rpc_message msg = {
5983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5984 .rpc_argp = &args,
5985 .rpc_resp = &res,
5986 };
5987 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
5988
5989 if (use_integrity) {
5990 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
5991 msg.rpc_cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
5992 }
5993
5994 dprintk("NFS call secinfo %s\n", name->name);
5995 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
5996 &res.seq_res, 0);
5997 dprintk("NFS reply secinfo: %d\n", status);
5998
5999 if (msg.rpc_cred)
6000 put_rpccred(msg.rpc_cred);
6001
6002 return status;
6003}
6004
6005int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6006 struct nfs4_secinfo_flavors *flavors)
6007{
6008 struct nfs4_exception exception = { };
6009 int err;
6010 do {
6011 err = -NFS4ERR_WRONGSEC;
6012
6013 /* try to use integrity protection with machine cred */
6014 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6015 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6016
6017 /*
6018 * if unable to use integrity protection, or SECINFO with
6019 * integrity protection returns NFS4ERR_WRONGSEC (which is
6020 * disallowed by spec, but exists in deployed servers) use
6021 * the current filesystem's rpc_client and the user cred.
6022 */
6023 if (err == -NFS4ERR_WRONGSEC)
6024 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6025
6026 trace_nfs4_secinfo(dir, name, err);
6027 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6028 &exception);
6029 } while (exception.retry);
6030 return err;
6031}
6032
6033#ifdef CONFIG_NFS_V4_1
6034/*
6035 * Check the exchange flags returned by the server for invalid flags, having
6036 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6037 * DS flags set.
6038 */
6039static int nfs4_check_cl_exchange_flags(u32 flags)
6040{
6041 if (flags & ~EXCHGID4_FLAG_MASK_R)
6042 goto out_inval;
6043 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6044 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6045 goto out_inval;
6046 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6047 goto out_inval;
6048 return NFS_OK;
6049out_inval:
6050 return -NFS4ERR_INVAL;
6051}
6052
6053static bool
6054nfs41_same_server_scope(struct nfs41_server_scope *a,
6055 struct nfs41_server_scope *b)
6056{
6057 if (a->server_scope_sz == b->server_scope_sz &&
6058 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6059 return true;
6060
6061 return false;
6062}
6063
6064/*
6065 * nfs4_proc_bind_conn_to_session()
6066 *
6067 * The 4.1 client currently uses the same TCP connection for the
6068 * fore and backchannel.
6069 */
6070int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6071{
6072 int status;
6073 struct nfs41_bind_conn_to_session_res res;
6074 struct rpc_message msg = {
6075 .rpc_proc =
6076 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6077 .rpc_argp = clp,
6078 .rpc_resp = &res,
6079 .rpc_cred = cred,
6080 };
6081
6082 dprintk("--> %s\n", __func__);
6083
6084 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6085 if (unlikely(res.session == NULL)) {
6086 status = -ENOMEM;
6087 goto out;
6088 }
6089
6090 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6091 trace_nfs4_bind_conn_to_session(clp, status);
6092 if (status == 0) {
6093 if (memcmp(res.session->sess_id.data,
6094 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6095 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6096 status = -EIO;
6097 goto out_session;
6098 }
6099 if (res.dir != NFS4_CDFS4_BOTH) {
6100 dprintk("NFS: %s: Unexpected direction from server\n",
6101 __func__);
6102 status = -EIO;
6103 goto out_session;
6104 }
6105 if (res.use_conn_in_rdma_mode) {
6106 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6107 __func__);
6108 status = -EIO;
6109 goto out_session;
6110 }
6111 }
6112out_session:
6113 kfree(res.session);
6114out:
6115 dprintk("<-- %s status= %d\n", __func__, status);
6116 return status;
6117}
6118
6119/*
6120 * nfs4_proc_exchange_id()
6121 *
6122 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6123 *
6124 * Since the clientid has expired, all compounds using sessions
6125 * associated with the stale clientid will be returning
6126 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6127 * be in some phase of session reset.
6128 */
6129int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6130{
6131 nfs4_verifier verifier;
6132 struct nfs41_exchange_id_args args = {
6133 .verifier = &verifier,
6134 .client = clp,
6135 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6136 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6137 };
6138 struct nfs41_exchange_id_res res = {
6139 0
6140 };
6141 int status;
6142 struct rpc_message msg = {
6143 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6144 .rpc_argp = &args,
6145 .rpc_resp = &res,
6146 .rpc_cred = cred,
6147 };
6148
6149 nfs4_init_boot_verifier(clp, &verifier);
6150 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6151 sizeof(args.id));
6152 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6153 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6154 args.id_len, args.id);
6155
6156 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
6157 GFP_NOFS);
6158 if (unlikely(res.server_owner == NULL)) {
6159 status = -ENOMEM;
6160 goto out;
6161 }
6162
6163 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
6164 GFP_NOFS);
6165 if (unlikely(res.server_scope == NULL)) {
6166 status = -ENOMEM;
6167 goto out_server_owner;
6168 }
6169
6170 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
6171 if (unlikely(res.impl_id == NULL)) {
6172 status = -ENOMEM;
6173 goto out_server_scope;
6174 }
6175
6176 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6177 trace_nfs4_exchange_id(clp, status);
6178 if (status == 0)
6179 status = nfs4_check_cl_exchange_flags(res.flags);
6180
6181 if (status == 0) {
6182 clp->cl_clientid = res.clientid;
6183 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6184 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6185 clp->cl_seqid = res.seqid;
6186
6187 kfree(clp->cl_serverowner);
6188 clp->cl_serverowner = res.server_owner;
6189 res.server_owner = NULL;
6190
6191 /* use the most recent implementation id */
6192 kfree(clp->cl_implid);
6193 clp->cl_implid = res.impl_id;
6194
6195 if (clp->cl_serverscope != NULL &&
6196 !nfs41_same_server_scope(clp->cl_serverscope,
6197 res.server_scope)) {
6198 dprintk("%s: server_scope mismatch detected\n",
6199 __func__);
6200 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
6201 kfree(clp->cl_serverscope);
6202 clp->cl_serverscope = NULL;
6203 }
6204
6205 if (clp->cl_serverscope == NULL) {
6206 clp->cl_serverscope = res.server_scope;
6207 goto out;
6208 }
6209 } else
6210 kfree(res.impl_id);
6211
6212out_server_owner:
6213 kfree(res.server_owner);
6214out_server_scope:
6215 kfree(res.server_scope);
6216out:
6217 if (clp->cl_implid != NULL)
6218 dprintk("NFS reply exchange_id: Server Implementation ID: "
6219 "domain: %s, name: %s, date: %llu,%u\n",
6220 clp->cl_implid->domain, clp->cl_implid->name,
6221 clp->cl_implid->date.seconds,
6222 clp->cl_implid->date.nseconds);
6223 dprintk("NFS reply exchange_id: %d\n", status);
6224 return status;
6225}
6226
6227static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6228 struct rpc_cred *cred)
6229{
6230 struct rpc_message msg = {
6231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6232 .rpc_argp = clp,
6233 .rpc_cred = cred,
6234 };
6235 int status;
6236
6237 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6238 trace_nfs4_destroy_clientid(clp, status);
6239 if (status)
6240 dprintk("NFS: Got error %d from the server %s on "
6241 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6242 return status;
6243}
6244
6245static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6246 struct rpc_cred *cred)
6247{
6248 unsigned int loop;
6249 int ret;
6250
6251 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6252 ret = _nfs4_proc_destroy_clientid(clp, cred);
6253 switch (ret) {
6254 case -NFS4ERR_DELAY:
6255 case -NFS4ERR_CLIENTID_BUSY:
6256 ssleep(1);
6257 break;
6258 default:
6259 return ret;
6260 }
6261 }
6262 return 0;
6263}
6264
6265int nfs4_destroy_clientid(struct nfs_client *clp)
6266{
6267 struct rpc_cred *cred;
6268 int ret = 0;
6269
6270 if (clp->cl_mvops->minor_version < 1)
6271 goto out;
6272 if (clp->cl_exchange_flags == 0)
6273 goto out;
6274 if (clp->cl_preserve_clid)
6275 goto out;
6276 cred = nfs4_get_clid_cred(clp);
6277 ret = nfs4_proc_destroy_clientid(clp, cred);
6278 if (cred)
6279 put_rpccred(cred);
6280 switch (ret) {
6281 case 0:
6282 case -NFS4ERR_STALE_CLIENTID:
6283 clp->cl_exchange_flags = 0;
6284 }
6285out:
6286 return ret;
6287}
6288
6289struct nfs4_get_lease_time_data {
6290 struct nfs4_get_lease_time_args *args;
6291 struct nfs4_get_lease_time_res *res;
6292 struct nfs_client *clp;
6293};
6294
6295static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6296 void *calldata)
6297{
6298 struct nfs4_get_lease_time_data *data =
6299 (struct nfs4_get_lease_time_data *)calldata;
6300
6301 dprintk("--> %s\n", __func__);
6302 /* just setup sequence, do not trigger session recovery
6303 since we're invoked within one */
6304 nfs41_setup_sequence(data->clp->cl_session,
6305 &data->args->la_seq_args,
6306 &data->res->lr_seq_res,
6307 task);
6308 dprintk("<-- %s\n", __func__);
6309}
6310
6311/*
6312 * Called from nfs4_state_manager thread for session setup, so don't recover
6313 * from sequence operation or clientid errors.
6314 */
6315static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6316{
6317 struct nfs4_get_lease_time_data *data =
6318 (struct nfs4_get_lease_time_data *)calldata;
6319
6320 dprintk("--> %s\n", __func__);
6321 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6322 return;
6323 switch (task->tk_status) {
6324 case -NFS4ERR_DELAY:
6325 case -NFS4ERR_GRACE:
6326 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6327 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6328 task->tk_status = 0;
6329 /* fall through */
6330 case -NFS4ERR_RETRY_UNCACHED_REP:
6331 rpc_restart_call_prepare(task);
6332 return;
6333 }
6334 dprintk("<-- %s\n", __func__);
6335}
6336
6337static const struct rpc_call_ops nfs4_get_lease_time_ops = {
6338 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6339 .rpc_call_done = nfs4_get_lease_time_done,
6340};
6341
6342int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6343{
6344 struct rpc_task *task;
6345 struct nfs4_get_lease_time_args args;
6346 struct nfs4_get_lease_time_res res = {
6347 .lr_fsinfo = fsinfo,
6348 };
6349 struct nfs4_get_lease_time_data data = {
6350 .args = &args,
6351 .res = &res,
6352 .clp = clp,
6353 };
6354 struct rpc_message msg = {
6355 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6356 .rpc_argp = &args,
6357 .rpc_resp = &res,
6358 };
6359 struct rpc_task_setup task_setup = {
6360 .rpc_client = clp->cl_rpcclient,
6361 .rpc_message = &msg,
6362 .callback_ops = &nfs4_get_lease_time_ops,
6363 .callback_data = &data,
6364 .flags = RPC_TASK_TIMEOUT,
6365 };
6366 int status;
6367
6368 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
6369 nfs4_set_sequence_privileged(&args.la_seq_args);
6370 dprintk("--> %s\n", __func__);
6371 task = rpc_run_task(&task_setup);
6372
6373 if (IS_ERR(task))
6374 status = PTR_ERR(task);
6375 else {
6376 status = task->tk_status;
6377 rpc_put_task(task);
6378 }
6379 dprintk("<-- %s return %d\n", __func__, status);
6380
6381 return status;
6382}
6383
6384/*
6385 * Initialize the values to be used by the client in CREATE_SESSION
6386 * If nfs4_init_session set the fore channel request and response sizes,
6387 * use them.
6388 *
6389 * Set the back channel max_resp_sz_cached to zero to force the client to
6390 * always set csa_cachethis to FALSE because the current implementation
6391 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6392 */
6393static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6394{
6395 unsigned int max_rqst_sz, max_resp_sz;
6396
6397 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6398 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6399
6400 /* Fore channel attributes */
6401 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6402 args->fc_attrs.max_resp_sz = max_resp_sz;
6403 args->fc_attrs.max_ops = NFS4_MAX_OPS;
6404 args->fc_attrs.max_reqs = max_session_slots;
6405
6406 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
6407 "max_ops=%u max_reqs=%u\n",
6408 __func__,
6409 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
6410 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
6411
6412 /* Back channel attributes */
6413 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6414 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6415 args->bc_attrs.max_resp_sz_cached = 0;
6416 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6417 args->bc_attrs.max_reqs = 1;
6418
6419 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6420 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6421 __func__,
6422 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6423 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6424 args->bc_attrs.max_reqs);
6425}
6426
6427static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6428{
6429 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6430 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6431
6432 if (rcvd->max_resp_sz > sent->max_resp_sz)
6433 return -EINVAL;
6434 /*
6435 * Our requested max_ops is the minimum we need; we're not
6436 * prepared to break up compounds into smaller pieces than that.
6437 * So, no point even trying to continue if the server won't
6438 * cooperate:
6439 */
6440 if (rcvd->max_ops < sent->max_ops)
6441 return -EINVAL;
6442 if (rcvd->max_reqs == 0)
6443 return -EINVAL;
6444 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6445 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
6446 return 0;
6447}
6448
6449static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6450{
6451 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6452 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
6453
6454 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6455 return -EINVAL;
6456 if (rcvd->max_resp_sz < sent->max_resp_sz)
6457 return -EINVAL;
6458 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6459 return -EINVAL;
6460 /* These would render the backchannel useless: */
6461 if (rcvd->max_ops != sent->max_ops)
6462 return -EINVAL;
6463 if (rcvd->max_reqs != sent->max_reqs)
6464 return -EINVAL;
6465 return 0;
6466}
6467
6468static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6469 struct nfs4_session *session)
6470{
6471 int ret;
6472
6473 ret = nfs4_verify_fore_channel_attrs(args, session);
6474 if (ret)
6475 return ret;
6476 return nfs4_verify_back_channel_attrs(args, session);
6477}
6478
6479static int _nfs4_proc_create_session(struct nfs_client *clp,
6480 struct rpc_cred *cred)
6481{
6482 struct nfs4_session *session = clp->cl_session;
6483 struct nfs41_create_session_args args = {
6484 .client = clp,
6485 .cb_program = NFS4_CALLBACK,
6486 };
6487 struct nfs41_create_session_res res = {
6488 .client = clp,
6489 };
6490 struct rpc_message msg = {
6491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6492 .rpc_argp = &args,
6493 .rpc_resp = &res,
6494 .rpc_cred = cred,
6495 };
6496 int status;
6497
6498 nfs4_init_channel_attrs(&args);
6499 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
6500
6501 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6502 trace_nfs4_create_session(clp, status);
6503
6504 if (!status) {
6505 /* Verify the session's negotiated channel_attrs values */
6506 status = nfs4_verify_channel_attrs(&args, session);
6507 /* Increment the clientid slot sequence id */
6508 clp->cl_seqid++;
6509 }
6510
6511 return status;
6512}
6513
6514/*
6515 * Issues a CREATE_SESSION operation to the server.
6516 * It is the responsibility of the caller to verify the session is
6517 * expired before calling this routine.
6518 */
6519int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
6520{
6521 int status;
6522 unsigned *ptr;
6523 struct nfs4_session *session = clp->cl_session;
6524
6525 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6526
6527 status = _nfs4_proc_create_session(clp, cred);
6528 if (status)
6529 goto out;
6530
6531 /* Init or reset the session slot tables */
6532 status = nfs4_setup_session_slot_tables(session);
6533 dprintk("slot table setup returned %d\n", status);
6534 if (status)
6535 goto out;
6536
6537 ptr = (unsigned *)&session->sess_id.data[0];
6538 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6539 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
6540out:
6541 dprintk("<-- %s\n", __func__);
6542 return status;
6543}
6544
6545/*
6546 * Issue the over-the-wire RPC DESTROY_SESSION.
6547 * The caller must serialize access to this routine.
6548 */
6549int nfs4_proc_destroy_session(struct nfs4_session *session,
6550 struct rpc_cred *cred)
6551{
6552 struct rpc_message msg = {
6553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6554 .rpc_argp = session,
6555 .rpc_cred = cred,
6556 };
6557 int status = 0;
6558
6559 dprintk("--> nfs4_proc_destroy_session\n");
6560
6561 /* session is still being setup */
6562 if (session->clp->cl_cons_state != NFS_CS_READY)
6563 return status;
6564
6565 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6566 trace_nfs4_destroy_session(session->clp, status);
6567
6568 if (status)
6569 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
6570 "Session has been destroyed regardless...\n", status);
6571
6572 dprintk("<-- nfs4_proc_destroy_session\n");
6573 return status;
6574}
6575
6576/*
6577 * Renew the cl_session lease.
6578 */
6579struct nfs4_sequence_data {
6580 struct nfs_client *clp;
6581 struct nfs4_sequence_args args;
6582 struct nfs4_sequence_res res;
6583};
6584
6585static void nfs41_sequence_release(void *data)
6586{
6587 struct nfs4_sequence_data *calldata = data;
6588 struct nfs_client *clp = calldata->clp;
6589
6590 if (atomic_read(&clp->cl_count) > 1)
6591 nfs4_schedule_state_renewal(clp);
6592 nfs_put_client(clp);
6593 kfree(calldata);
6594}
6595
6596static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6597{
6598 switch(task->tk_status) {
6599 case -NFS4ERR_DELAY:
6600 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6601 return -EAGAIN;
6602 default:
6603 nfs4_schedule_lease_recovery(clp);
6604 }
6605 return 0;
6606}
6607
6608static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
6609{
6610 struct nfs4_sequence_data *calldata = data;
6611 struct nfs_client *clp = calldata->clp;
6612
6613 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6614 return;
6615
6616 trace_nfs4_sequence(clp, task->tk_status);
6617 if (task->tk_status < 0) {
6618 dprintk("%s ERROR %d\n", __func__, task->tk_status);
6619 if (atomic_read(&clp->cl_count) == 1)
6620 goto out;
6621
6622 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6623 rpc_restart_call_prepare(task);
6624 return;
6625 }
6626 }
6627 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
6628out:
6629 dprintk("<-- %s\n", __func__);
6630}
6631
6632static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6633{
6634 struct nfs4_sequence_data *calldata = data;
6635 struct nfs_client *clp = calldata->clp;
6636 struct nfs4_sequence_args *args;
6637 struct nfs4_sequence_res *res;
6638
6639 args = task->tk_msg.rpc_argp;
6640 res = task->tk_msg.rpc_resp;
6641
6642 nfs41_setup_sequence(clp->cl_session, args, res, task);
6643}
6644
6645static const struct rpc_call_ops nfs41_sequence_ops = {
6646 .rpc_call_done = nfs41_sequence_call_done,
6647 .rpc_call_prepare = nfs41_sequence_prepare,
6648 .rpc_release = nfs41_sequence_release,
6649};
6650
6651static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6652 struct rpc_cred *cred,
6653 bool is_privileged)
6654{
6655 struct nfs4_sequence_data *calldata;
6656 struct rpc_message msg = {
6657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6658 .rpc_cred = cred,
6659 };
6660 struct rpc_task_setup task_setup_data = {
6661 .rpc_client = clp->cl_rpcclient,
6662 .rpc_message = &msg,
6663 .callback_ops = &nfs41_sequence_ops,
6664 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6665 };
6666
6667 if (!atomic_inc_not_zero(&clp->cl_count))
6668 return ERR_PTR(-EIO);
6669 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6670 if (calldata == NULL) {
6671 nfs_put_client(clp);
6672 return ERR_PTR(-ENOMEM);
6673 }
6674 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
6675 if (is_privileged)
6676 nfs4_set_sequence_privileged(&calldata->args);
6677 msg.rpc_argp = &calldata->args;
6678 msg.rpc_resp = &calldata->res;
6679 calldata->clp = clp;
6680 task_setup_data.callback_data = calldata;
6681
6682 return rpc_run_task(&task_setup_data);
6683}
6684
6685static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
6686{
6687 struct rpc_task *task;
6688 int ret = 0;
6689
6690 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6691 return 0;
6692 task = _nfs41_proc_sequence(clp, cred, false);
6693 if (IS_ERR(task))
6694 ret = PTR_ERR(task);
6695 else
6696 rpc_put_task_async(task);
6697 dprintk("<-- %s status=%d\n", __func__, ret);
6698 return ret;
6699}
6700
6701static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6702{
6703 struct rpc_task *task;
6704 int ret;
6705
6706 task = _nfs41_proc_sequence(clp, cred, true);
6707 if (IS_ERR(task)) {
6708 ret = PTR_ERR(task);
6709 goto out;
6710 }
6711 ret = rpc_wait_for_completion_task(task);
6712 if (!ret) {
6713 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6714
6715 if (task->tk_status == 0)
6716 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
6717 ret = task->tk_status;
6718 }
6719 rpc_put_task(task);
6720out:
6721 dprintk("<-- %s status=%d\n", __func__, ret);
6722 return ret;
6723}
6724
6725struct nfs4_reclaim_complete_data {
6726 struct nfs_client *clp;
6727 struct nfs41_reclaim_complete_args arg;
6728 struct nfs41_reclaim_complete_res res;
6729};
6730
6731static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6732{
6733 struct nfs4_reclaim_complete_data *calldata = data;
6734
6735 nfs41_setup_sequence(calldata->clp->cl_session,
6736 &calldata->arg.seq_args,
6737 &calldata->res.seq_res,
6738 task);
6739}
6740
6741static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6742{
6743 switch(task->tk_status) {
6744 case 0:
6745 case -NFS4ERR_COMPLETE_ALREADY:
6746 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6747 break;
6748 case -NFS4ERR_DELAY:
6749 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6750 /* fall through */
6751 case -NFS4ERR_RETRY_UNCACHED_REP:
6752 return -EAGAIN;
6753 default:
6754 nfs4_schedule_lease_recovery(clp);
6755 }
6756 return 0;
6757}
6758
6759static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6760{
6761 struct nfs4_reclaim_complete_data *calldata = data;
6762 struct nfs_client *clp = calldata->clp;
6763 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6764
6765 dprintk("--> %s\n", __func__);
6766 if (!nfs41_sequence_done(task, res))
6767 return;
6768
6769 trace_nfs4_reclaim_complete(clp, task->tk_status);
6770 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6771 rpc_restart_call_prepare(task);
6772 return;
6773 }
6774 dprintk("<-- %s\n", __func__);
6775}
6776
6777static void nfs4_free_reclaim_complete_data(void *data)
6778{
6779 struct nfs4_reclaim_complete_data *calldata = data;
6780
6781 kfree(calldata);
6782}
6783
6784static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6785 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6786 .rpc_call_done = nfs4_reclaim_complete_done,
6787 .rpc_release = nfs4_free_reclaim_complete_data,
6788};
6789
6790/*
6791 * Issue a global reclaim complete.
6792 */
6793static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6794 struct rpc_cred *cred)
6795{
6796 struct nfs4_reclaim_complete_data *calldata;
6797 struct rpc_task *task;
6798 struct rpc_message msg = {
6799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6800 .rpc_cred = cred,
6801 };
6802 struct rpc_task_setup task_setup_data = {
6803 .rpc_client = clp->cl_rpcclient,
6804 .rpc_message = &msg,
6805 .callback_ops = &nfs4_reclaim_complete_call_ops,
6806 .flags = RPC_TASK_ASYNC,
6807 };
6808 int status = -ENOMEM;
6809
6810 dprintk("--> %s\n", __func__);
6811 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6812 if (calldata == NULL)
6813 goto out;
6814 calldata->clp = clp;
6815 calldata->arg.one_fs = 0;
6816
6817 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
6818 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
6819 msg.rpc_argp = &calldata->arg;
6820 msg.rpc_resp = &calldata->res;
6821 task_setup_data.callback_data = calldata;
6822 task = rpc_run_task(&task_setup_data);
6823 if (IS_ERR(task)) {
6824 status = PTR_ERR(task);
6825 goto out;
6826 }
6827 status = nfs4_wait_for_completion_rpc_task(task);
6828 if (status == 0)
6829 status = task->tk_status;
6830 rpc_put_task(task);
6831 return 0;
6832out:
6833 dprintk("<-- %s status=%d\n", __func__, status);
6834 return status;
6835}
6836
6837static void
6838nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6839{
6840 struct nfs4_layoutget *lgp = calldata;
6841 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6842 struct nfs4_session *session = nfs4_get_session(server);
6843
6844 dprintk("--> %s\n", __func__);
6845 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6846 * right now covering the LAYOUTGET we are about to send.
6847 * However, that is not so catastrophic, and there seems
6848 * to be no way to prevent it completely.
6849 */
6850 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
6851 &lgp->res.seq_res, task))
6852 return;
6853 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6854 NFS_I(lgp->args.inode)->layout,
6855 lgp->args.ctx->state)) {
6856 rpc_exit(task, NFS4_OK);
6857 }
6858}
6859
6860static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6861{
6862 struct nfs4_layoutget *lgp = calldata;
6863 struct inode *inode = lgp->args.inode;
6864 struct nfs_server *server = NFS_SERVER(inode);
6865 struct pnfs_layout_hdr *lo;
6866 struct nfs4_state *state = NULL;
6867 unsigned long timeo, giveup;
6868
6869 dprintk("--> %s\n", __func__);
6870
6871 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
6872 goto out;
6873
6874 switch (task->tk_status) {
6875 case 0:
6876 goto out;
6877 case -NFS4ERR_LAYOUTTRYLATER:
6878 case -NFS4ERR_RECALLCONFLICT:
6879 timeo = rpc_get_timeout(task->tk_client);
6880 giveup = lgp->args.timestamp + timeo;
6881 if (time_after(giveup, jiffies))
6882 task->tk_status = -NFS4ERR_DELAY;
6883 break;
6884 case -NFS4ERR_EXPIRED:
6885 case -NFS4ERR_BAD_STATEID:
6886 spin_lock(&inode->i_lock);
6887 lo = NFS_I(inode)->layout;
6888 if (!lo || list_empty(&lo->plh_segs)) {
6889 spin_unlock(&inode->i_lock);
6890 /* If the open stateid was bad, then recover it. */
6891 state = lgp->args.ctx->state;
6892 } else {
6893 LIST_HEAD(head);
6894
6895 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6896 spin_unlock(&inode->i_lock);
6897 /* Mark the bad layout state as invalid, then
6898 * retry using the open stateid. */
6899 pnfs_free_lseg_list(&head);
6900 }
6901 }
6902 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6903 rpc_restart_call_prepare(task);
6904out:
6905 dprintk("<-- %s\n", __func__);
6906}
6907
6908static size_t max_response_pages(struct nfs_server *server)
6909{
6910 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6911 return nfs_page_array_len(0, max_resp_sz);
6912}
6913
6914static void nfs4_free_pages(struct page **pages, size_t size)
6915{
6916 int i;
6917
6918 if (!pages)
6919 return;
6920
6921 for (i = 0; i < size; i++) {
6922 if (!pages[i])
6923 break;
6924 __free_page(pages[i]);
6925 }
6926 kfree(pages);
6927}
6928
6929static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6930{
6931 struct page **pages;
6932 int i;
6933
6934 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6935 if (!pages) {
6936 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6937 return NULL;
6938 }
6939
6940 for (i = 0; i < size; i++) {
6941 pages[i] = alloc_page(gfp_flags);
6942 if (!pages[i]) {
6943 dprintk("%s: failed to allocate page\n", __func__);
6944 nfs4_free_pages(pages, size);
6945 return NULL;
6946 }
6947 }
6948
6949 return pages;
6950}
6951
6952static void nfs4_layoutget_release(void *calldata)
6953{
6954 struct nfs4_layoutget *lgp = calldata;
6955 struct inode *inode = lgp->args.inode;
6956 struct nfs_server *server = NFS_SERVER(inode);
6957 size_t max_pages = max_response_pages(server);
6958
6959 dprintk("--> %s\n", __func__);
6960 nfs4_free_pages(lgp->args.layout.pages, max_pages);
6961 pnfs_put_layout_hdr(NFS_I(inode)->layout);
6962 put_nfs_open_context(lgp->args.ctx);
6963 kfree(calldata);
6964 dprintk("<-- %s\n", __func__);
6965}
6966
6967static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6968 .rpc_call_prepare = nfs4_layoutget_prepare,
6969 .rpc_call_done = nfs4_layoutget_done,
6970 .rpc_release = nfs4_layoutget_release,
6971};
6972
6973struct pnfs_layout_segment *
6974nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6975{
6976 struct inode *inode = lgp->args.inode;
6977 struct nfs_server *server = NFS_SERVER(inode);
6978 size_t max_pages = max_response_pages(server);
6979 struct rpc_task *task;
6980 struct rpc_message msg = {
6981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6982 .rpc_argp = &lgp->args,
6983 .rpc_resp = &lgp->res,
6984 .rpc_cred = lgp->cred,
6985 };
6986 struct rpc_task_setup task_setup_data = {
6987 .rpc_client = server->client,
6988 .rpc_message = &msg,
6989 .callback_ops = &nfs4_layoutget_call_ops,
6990 .callback_data = lgp,
6991 .flags = RPC_TASK_ASYNC,
6992 };
6993 struct pnfs_layout_segment *lseg = NULL;
6994 int status = 0;
6995
6996 dprintk("--> %s\n", __func__);
6997
6998 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6999 if (!lgp->args.layout.pages) {
7000 nfs4_layoutget_release(lgp);
7001 return ERR_PTR(-ENOMEM);
7002 }
7003 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
7004 lgp->args.timestamp = jiffies;
7005
7006 lgp->res.layoutp = &lgp->args.layout;
7007 lgp->res.seq_res.sr_slot = NULL;
7008 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
7009
7010 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7011 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7012
7013 task = rpc_run_task(&task_setup_data);
7014 if (IS_ERR(task))
7015 return ERR_CAST(task);
7016 status = nfs4_wait_for_completion_rpc_task(task);
7017 if (status == 0)
7018 status = task->tk_status;
7019 trace_nfs4_layoutget(lgp->args.ctx,
7020 &lgp->args.range,
7021 &lgp->res.range,
7022 status);
7023 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7024 if (status == 0 && lgp->res.layoutp->len)
7025 lseg = pnfs_layout_process(lgp);
7026 rpc_put_task(task);
7027 dprintk("<-- %s status=%d\n", __func__, status);
7028 if (status)
7029 return ERR_PTR(status);
7030 return lseg;
7031}
7032
7033static void
7034nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7035{
7036 struct nfs4_layoutreturn *lrp = calldata;
7037
7038 dprintk("--> %s\n", __func__);
7039 nfs41_setup_sequence(lrp->clp->cl_session,
7040 &lrp->args.seq_args,
7041 &lrp->res.seq_res,
7042 task);
7043}
7044
7045static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7046{
7047 struct nfs4_layoutreturn *lrp = calldata;
7048 struct nfs_server *server;
7049
7050 dprintk("--> %s\n", __func__);
7051
7052 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
7053 return;
7054
7055 server = NFS_SERVER(lrp->args.inode);
7056 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7057 rpc_restart_call_prepare(task);
7058 return;
7059 }
7060 dprintk("<-- %s\n", __func__);
7061}
7062
7063static void nfs4_layoutreturn_release(void *calldata)
7064{
7065 struct nfs4_layoutreturn *lrp = calldata;
7066 struct pnfs_layout_hdr *lo = lrp->args.layout;
7067
7068 dprintk("--> %s\n", __func__);
7069 spin_lock(&lo->plh_inode->i_lock);
7070 if (lrp->res.lrs_present)
7071 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7072 lo->plh_block_lgets--;
7073 spin_unlock(&lo->plh_inode->i_lock);
7074 pnfs_put_layout_hdr(lrp->args.layout);
7075 kfree(calldata);
7076 dprintk("<-- %s\n", __func__);
7077}
7078
7079static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7080 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7081 .rpc_call_done = nfs4_layoutreturn_done,
7082 .rpc_release = nfs4_layoutreturn_release,
7083};
7084
7085int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7086{
7087 struct rpc_task *task;
7088 struct rpc_message msg = {
7089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7090 .rpc_argp = &lrp->args,
7091 .rpc_resp = &lrp->res,
7092 .rpc_cred = lrp->cred,
7093 };
7094 struct rpc_task_setup task_setup_data = {
7095 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
7096 .rpc_message = &msg,
7097 .callback_ops = &nfs4_layoutreturn_call_ops,
7098 .callback_data = lrp,
7099 };
7100 int status;
7101
7102 dprintk("--> %s\n", __func__);
7103 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
7104 task = rpc_run_task(&task_setup_data);
7105 if (IS_ERR(task))
7106 return PTR_ERR(task);
7107 status = task->tk_status;
7108 trace_nfs4_layoutreturn(lrp->args.inode, status);
7109 dprintk("<-- %s status=%d\n", __func__, status);
7110 rpc_put_task(task);
7111 return status;
7112}
7113
7114/*
7115 * Retrieve the list of Data Server devices from the MDS.
7116 */
7117static int _nfs4_getdevicelist(struct nfs_server *server,
7118 const struct nfs_fh *fh,
7119 struct pnfs_devicelist *devlist)
7120{
7121 struct nfs4_getdevicelist_args args = {
7122 .fh = fh,
7123 .layoutclass = server->pnfs_curr_ld->id,
7124 };
7125 struct nfs4_getdevicelist_res res = {
7126 .devlist = devlist,
7127 };
7128 struct rpc_message msg = {
7129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7130 .rpc_argp = &args,
7131 .rpc_resp = &res,
7132 };
7133 int status;
7134
7135 dprintk("--> %s\n", __func__);
7136 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7137 &res.seq_res, 0);
7138 dprintk("<-- %s status=%d\n", __func__, status);
7139 return status;
7140}
7141
7142int nfs4_proc_getdevicelist(struct nfs_server *server,
7143 const struct nfs_fh *fh,
7144 struct pnfs_devicelist *devlist)
7145{
7146 struct nfs4_exception exception = { };
7147 int err;
7148
7149 do {
7150 err = nfs4_handle_exception(server,
7151 _nfs4_getdevicelist(server, fh, devlist),
7152 &exception);
7153 } while (exception.retry);
7154
7155 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7156 err, devlist->num_devs);
7157
7158 return err;
7159}
7160EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7161
7162static int
7163_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7164 struct pnfs_device *pdev,
7165 struct rpc_cred *cred)
7166{
7167 struct nfs4_getdeviceinfo_args args = {
7168 .pdev = pdev,
7169 };
7170 struct nfs4_getdeviceinfo_res res = {
7171 .pdev = pdev,
7172 };
7173 struct rpc_message msg = {
7174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7175 .rpc_argp = &args,
7176 .rpc_resp = &res,
7177 .rpc_cred = cred,
7178 };
7179 int status;
7180
7181 dprintk("--> %s\n", __func__);
7182 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
7183 dprintk("<-- %s status=%d\n", __func__, status);
7184
7185 return status;
7186}
7187
7188int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7189 struct pnfs_device *pdev,
7190 struct rpc_cred *cred)
7191{
7192 struct nfs4_exception exception = { };
7193 int err;
7194
7195 do {
7196 err = nfs4_handle_exception(server,
7197 _nfs4_proc_getdeviceinfo(server, pdev, cred),
7198 &exception);
7199 } while (exception.retry);
7200 return err;
7201}
7202EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7203
7204static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7205{
7206 struct nfs4_layoutcommit_data *data = calldata;
7207 struct nfs_server *server = NFS_SERVER(data->args.inode);
7208 struct nfs4_session *session = nfs4_get_session(server);
7209
7210 nfs41_setup_sequence(session,
7211 &data->args.seq_args,
7212 &data->res.seq_res,
7213 task);
7214}
7215
7216static void
7217nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7218{
7219 struct nfs4_layoutcommit_data *data = calldata;
7220 struct nfs_server *server = NFS_SERVER(data->args.inode);
7221
7222 if (!nfs41_sequence_done(task, &data->res.seq_res))
7223 return;
7224
7225 switch (task->tk_status) { /* Just ignore these failures */
7226 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7227 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7228 case -NFS4ERR_BADLAYOUT: /* no layout */
7229 case -NFS4ERR_GRACE: /* loca_recalim always false */
7230 task->tk_status = 0;
7231 break;
7232 case 0:
7233 nfs_post_op_update_inode_force_wcc(data->args.inode,
7234 data->res.fattr);
7235 break;
7236 default:
7237 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7238 rpc_restart_call_prepare(task);
7239 return;
7240 }
7241 }
7242}
7243
7244static void nfs4_layoutcommit_release(void *calldata)
7245{
7246 struct nfs4_layoutcommit_data *data = calldata;
7247
7248 pnfs_cleanup_layoutcommit(data);
7249 put_rpccred(data->cred);
7250 kfree(data);
7251}
7252
7253static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7254 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7255 .rpc_call_done = nfs4_layoutcommit_done,
7256 .rpc_release = nfs4_layoutcommit_release,
7257};
7258
7259int
7260nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
7261{
7262 struct rpc_message msg = {
7263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7264 .rpc_argp = &data->args,
7265 .rpc_resp = &data->res,
7266 .rpc_cred = data->cred,
7267 };
7268 struct rpc_task_setup task_setup_data = {
7269 .task = &data->task,
7270 .rpc_client = NFS_CLIENT(data->args.inode),
7271 .rpc_message = &msg,
7272 .callback_ops = &nfs4_layoutcommit_ops,
7273 .callback_data = data,
7274 .flags = RPC_TASK_ASYNC,
7275 };
7276 struct rpc_task *task;
7277 int status = 0;
7278
7279 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7280 "lbw: %llu inode %lu\n",
7281 data->task.tk_pid, sync,
7282 data->args.lastbytewritten,
7283 data->args.inode->i_ino);
7284
7285 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
7286 task = rpc_run_task(&task_setup_data);
7287 if (IS_ERR(task))
7288 return PTR_ERR(task);
7289 if (sync == false)
7290 goto out;
7291 status = nfs4_wait_for_completion_rpc_task(task);
7292 if (status != 0)
7293 goto out;
7294 status = task->tk_status;
7295 trace_nfs4_layoutcommit(data->args.inode, status);
7296out:
7297 dprintk("%s: status %d\n", __func__, status);
7298 rpc_put_task(task);
7299 return status;
7300}
7301
7302/**
7303 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7304 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7305 */
7306static int
7307_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7308 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7309{
7310 struct nfs41_secinfo_no_name_args args = {
7311 .style = SECINFO_STYLE_CURRENT_FH,
7312 };
7313 struct nfs4_secinfo_res res = {
7314 .flavors = flavors,
7315 };
7316 struct rpc_message msg = {
7317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7318 .rpc_argp = &args,
7319 .rpc_resp = &res,
7320 };
7321 return nfs4_call_sync(server->nfs_client->cl_rpcclient, server, &msg,
7322 &args.seq_args, &res.seq_res, 0);
7323}
7324
7325static int
7326nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7327 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7328{
7329 struct nfs4_exception exception = { };
7330 int err;
7331 do {
7332 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7333 switch (err) {
7334 case 0:
7335 case -NFS4ERR_WRONGSEC:
7336 case -NFS4ERR_NOTSUPP:
7337 goto out;
7338 default:
7339 err = nfs4_handle_exception(server, err, &exception);
7340 }
7341 } while (exception.retry);
7342out:
7343 return err;
7344}
7345
7346static int
7347nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7348 struct nfs_fsinfo *info)
7349{
7350 int err;
7351 struct page *page;
7352 rpc_authflavor_t flavor;
7353 struct nfs4_secinfo_flavors *flavors;
7354
7355 page = alloc_page(GFP_KERNEL);
7356 if (!page) {
7357 err = -ENOMEM;
7358 goto out;
7359 }
7360
7361 flavors = page_address(page);
7362 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7363
7364 /*
7365 * Fall back on "guess and check" method if
7366 * the server doesn't support SECINFO_NO_NAME
7367 */
7368 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7369 err = nfs4_find_root_sec(server, fhandle, info);
7370 goto out_freepage;
7371 }
7372 if (err)
7373 goto out_freepage;
7374
7375 flavor = nfs_find_best_sec(flavors);
7376 if (err == 0)
7377 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
7378
7379out_freepage:
7380 put_page(page);
7381 if (err == -EACCES)
7382 return -EPERM;
7383out:
7384 return err;
7385}
7386
7387static int _nfs41_test_stateid(struct nfs_server *server,
7388 nfs4_stateid *stateid,
7389 struct rpc_cred *cred)
7390{
7391 int status;
7392 struct nfs41_test_stateid_args args = {
7393 .stateid = stateid,
7394 };
7395 struct nfs41_test_stateid_res res;
7396 struct rpc_message msg = {
7397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7398 .rpc_argp = &args,
7399 .rpc_resp = &res,
7400 .rpc_cred = cred,
7401 };
7402
7403 dprintk("NFS call test_stateid %p\n", stateid);
7404 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7405 nfs4_set_sequence_privileged(&args.seq_args);
7406 status = nfs4_call_sync_sequence(server->client, server, &msg,
7407 &args.seq_args, &res.seq_res);
7408 if (status != NFS_OK) {
7409 dprintk("NFS reply test_stateid: failed, %d\n", status);
7410 return status;
7411 }
7412 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
7413 return -res.status;
7414}
7415
7416/**
7417 * nfs41_test_stateid - perform a TEST_STATEID operation
7418 *
7419 * @server: server / transport on which to perform the operation
7420 * @stateid: state ID to test
7421 * @cred: credential
7422 *
7423 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7424 * Otherwise a negative NFS4ERR value is returned if the operation
7425 * failed or the state ID is not currently valid.
7426 */
7427static int nfs41_test_stateid(struct nfs_server *server,
7428 nfs4_stateid *stateid,
7429 struct rpc_cred *cred)
7430{
7431 struct nfs4_exception exception = { };
7432 int err;
7433 do {
7434 err = _nfs41_test_stateid(server, stateid, cred);
7435 if (err != -NFS4ERR_DELAY)
7436 break;
7437 nfs4_handle_exception(server, err, &exception);
7438 } while (exception.retry);
7439 return err;
7440}
7441
7442struct nfs_free_stateid_data {
7443 struct nfs_server *server;
7444 struct nfs41_free_stateid_args args;
7445 struct nfs41_free_stateid_res res;
7446};
7447
7448static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7449{
7450 struct nfs_free_stateid_data *data = calldata;
7451 nfs41_setup_sequence(nfs4_get_session(data->server),
7452 &data->args.seq_args,
7453 &data->res.seq_res,
7454 task);
7455}
7456
7457static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7458{
7459 struct nfs_free_stateid_data *data = calldata;
7460
7461 nfs41_sequence_done(task, &data->res.seq_res);
7462
7463 switch (task->tk_status) {
7464 case -NFS4ERR_DELAY:
7465 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7466 rpc_restart_call_prepare(task);
7467 }
7468}
7469
7470static void nfs41_free_stateid_release(void *calldata)
7471{
7472 kfree(calldata);
7473}
7474
7475static const struct rpc_call_ops nfs41_free_stateid_ops = {
7476 .rpc_call_prepare = nfs41_free_stateid_prepare,
7477 .rpc_call_done = nfs41_free_stateid_done,
7478 .rpc_release = nfs41_free_stateid_release,
7479};
7480
7481static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7482 nfs4_stateid *stateid,
7483 struct rpc_cred *cred,
7484 bool privileged)
7485{
7486 struct rpc_message msg = {
7487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
7488 .rpc_cred = cred,
7489 };
7490 struct rpc_task_setup task_setup = {
7491 .rpc_client = server->client,
7492 .rpc_message = &msg,
7493 .callback_ops = &nfs41_free_stateid_ops,
7494 .flags = RPC_TASK_ASYNC,
7495 };
7496 struct nfs_free_stateid_data *data;
7497
7498 dprintk("NFS call free_stateid %p\n", stateid);
7499 data = kmalloc(sizeof(*data), GFP_NOFS);
7500 if (!data)
7501 return ERR_PTR(-ENOMEM);
7502 data->server = server;
7503 nfs4_stateid_copy(&data->args.stateid, stateid);
7504
7505 task_setup.callback_data = data;
7506
7507 msg.rpc_argp = &data->args;
7508 msg.rpc_resp = &data->res;
7509 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
7510 if (privileged)
7511 nfs4_set_sequence_privileged(&data->args.seq_args);
7512
7513 return rpc_run_task(&task_setup);
7514}
7515
7516/**
7517 * nfs41_free_stateid - perform a FREE_STATEID operation
7518 *
7519 * @server: server / transport on which to perform the operation
7520 * @stateid: state ID to release
7521 * @cred: credential
7522 *
7523 * Returns NFS_OK if the server freed "stateid". Otherwise a
7524 * negative NFS4ERR value is returned.
7525 */
7526static int nfs41_free_stateid(struct nfs_server *server,
7527 nfs4_stateid *stateid,
7528 struct rpc_cred *cred)
7529{
7530 struct rpc_task *task;
7531 int ret;
7532
7533 task = _nfs41_free_stateid(server, stateid, cred, true);
7534 if (IS_ERR(task))
7535 return PTR_ERR(task);
7536 ret = rpc_wait_for_completion_task(task);
7537 if (!ret)
7538 ret = task->tk_status;
7539 rpc_put_task(task);
7540 return ret;
7541}
7542
7543static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7544{
7545 struct rpc_task *task;
7546 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
7547
7548 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
7549 nfs4_free_lock_state(server, lsp);
7550 if (IS_ERR(task))
7551 return PTR_ERR(task);
7552 rpc_put_task(task);
7553 return 0;
7554}
7555
7556static bool nfs41_match_stateid(const nfs4_stateid *s1,
7557 const nfs4_stateid *s2)
7558{
7559 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
7560 return false;
7561
7562 if (s1->seqid == s2->seqid)
7563 return true;
7564 if (s1->seqid == 0 || s2->seqid == 0)
7565 return true;
7566
7567 return false;
7568}
7569
7570#endif /* CONFIG_NFS_V4_1 */
7571
7572static bool nfs4_match_stateid(const nfs4_stateid *s1,
7573 const nfs4_stateid *s2)
7574{
7575 return nfs4_stateid_match(s1, s2);
7576}
7577
7578
7579static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
7580 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7581 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7582 .recover_open = nfs4_open_reclaim,
7583 .recover_lock = nfs4_lock_reclaim,
7584 .establish_clid = nfs4_init_clientid,
7585 .detect_trunking = nfs40_discover_server_trunking,
7586};
7587
7588#if defined(CONFIG_NFS_V4_1)
7589static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
7590 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7591 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7592 .recover_open = nfs4_open_reclaim,
7593 .recover_lock = nfs4_lock_reclaim,
7594 .establish_clid = nfs41_init_clientid,
7595 .reclaim_complete = nfs41_proc_reclaim_complete,
7596 .detect_trunking = nfs41_discover_server_trunking,
7597};
7598#endif /* CONFIG_NFS_V4_1 */
7599
7600static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
7601 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7602 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
7603 .recover_open = nfs4_open_expired,
7604 .recover_lock = nfs4_lock_expired,
7605 .establish_clid = nfs4_init_clientid,
7606};
7607
7608#if defined(CONFIG_NFS_V4_1)
7609static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
7610 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7611 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
7612 .recover_open = nfs41_open_expired,
7613 .recover_lock = nfs41_lock_expired,
7614 .establish_clid = nfs41_init_clientid,
7615};
7616#endif /* CONFIG_NFS_V4_1 */
7617
7618static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
7619 .sched_state_renewal = nfs4_proc_async_renew,
7620 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
7621 .renew_lease = nfs4_proc_renew,
7622};
7623
7624#if defined(CONFIG_NFS_V4_1)
7625static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
7626 .sched_state_renewal = nfs41_proc_async_sequence,
7627 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
7628 .renew_lease = nfs4_proc_sequence,
7629};
7630#endif
7631
7632static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7633 .minor_version = 0,
7634 .init_caps = NFS_CAP_READDIRPLUS
7635 | NFS_CAP_ATOMIC_OPEN
7636 | NFS_CAP_CHANGE_ATTR
7637 | NFS_CAP_POSIX_LOCK,
7638 .init_client = nfs40_init_client,
7639 .shutdown_client = nfs40_shutdown_client,
7640 .match_stateid = nfs4_match_stateid,
7641 .find_root_sec = nfs4_find_root_sec,
7642 .free_lock_state = nfs4_release_lockowner,
7643 .call_sync_ops = &nfs40_call_sync_ops,
7644 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7645 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7646 .state_renewal_ops = &nfs40_state_renewal_ops,
7647};
7648
7649#if defined(CONFIG_NFS_V4_1)
7650static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7651 .minor_version = 1,
7652 .init_caps = NFS_CAP_READDIRPLUS
7653 | NFS_CAP_ATOMIC_OPEN
7654 | NFS_CAP_CHANGE_ATTR
7655 | NFS_CAP_POSIX_LOCK
7656 | NFS_CAP_STATEID_NFSV41
7657 | NFS_CAP_ATOMIC_OPEN_V1,
7658 .init_client = nfs41_init_client,
7659 .shutdown_client = nfs41_shutdown_client,
7660 .match_stateid = nfs41_match_stateid,
7661 .find_root_sec = nfs41_find_root_sec,
7662 .free_lock_state = nfs41_free_lock_state,
7663 .call_sync_ops = &nfs41_call_sync_ops,
7664 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7665 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7666 .state_renewal_ops = &nfs41_state_renewal_ops,
7667};
7668#endif
7669
7670#if defined(CONFIG_NFS_V4_2)
7671static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7672 .minor_version = 2,
7673 .init_caps = NFS_CAP_READDIRPLUS
7674 | NFS_CAP_ATOMIC_OPEN
7675 | NFS_CAP_CHANGE_ATTR
7676 | NFS_CAP_POSIX_LOCK
7677 | NFS_CAP_STATEID_NFSV41
7678 | NFS_CAP_ATOMIC_OPEN_V1,
7679 .init_client = nfs41_init_client,
7680 .shutdown_client = nfs41_shutdown_client,
7681 .match_stateid = nfs41_match_stateid,
7682 .find_root_sec = nfs41_find_root_sec,
7683 .free_lock_state = nfs41_free_lock_state,
7684 .call_sync_ops = &nfs41_call_sync_ops,
7685 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7686 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7687 .state_renewal_ops = &nfs41_state_renewal_ops,
7688};
7689#endif
7690
7691const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7692 [0] = &nfs_v4_0_minor_ops,
7693#if defined(CONFIG_NFS_V4_1)
7694 [1] = &nfs_v4_1_minor_ops,
7695#endif
7696#if defined(CONFIG_NFS_V4_2)
7697 [2] = &nfs_v4_2_minor_ops,
7698#endif
7699};
7700
7701static const struct inode_operations nfs4_dir_inode_operations = {
7702 .create = nfs_create,
7703 .lookup = nfs_lookup,
7704 .atomic_open = nfs_atomic_open,
7705 .link = nfs_link,
7706 .unlink = nfs_unlink,
7707 .symlink = nfs_symlink,
7708 .mkdir = nfs_mkdir,
7709 .rmdir = nfs_rmdir,
7710 .mknod = nfs_mknod,
7711 .rename = nfs_rename,
7712 .permission = nfs_permission,
7713 .getattr = nfs_getattr,
7714 .setattr = nfs_setattr,
7715 .getxattr = generic_getxattr,
7716 .setxattr = generic_setxattr,
7717 .listxattr = generic_listxattr,
7718 .removexattr = generic_removexattr,
7719};
7720
7721static const struct inode_operations nfs4_file_inode_operations = {
7722 .permission = nfs_permission,
7723 .getattr = nfs_getattr,
7724 .setattr = nfs_setattr,
7725 .getxattr = generic_getxattr,
7726 .setxattr = generic_setxattr,
7727 .listxattr = generic_listxattr,
7728 .removexattr = generic_removexattr,
7729};
7730
7731const struct nfs_rpc_ops nfs_v4_clientops = {
7732 .version = 4, /* protocol version */
7733 .dentry_ops = &nfs4_dentry_operations,
7734 .dir_inode_ops = &nfs4_dir_inode_operations,
7735 .file_inode_ops = &nfs4_file_inode_operations,
7736 .file_ops = &nfs4_file_operations,
7737 .getroot = nfs4_proc_get_root,
7738 .submount = nfs4_submount,
7739 .try_mount = nfs4_try_mount,
7740 .getattr = nfs4_proc_getattr,
7741 .setattr = nfs4_proc_setattr,
7742 .lookup = nfs4_proc_lookup,
7743 .access = nfs4_proc_access,
7744 .readlink = nfs4_proc_readlink,
7745 .create = nfs4_proc_create,
7746 .remove = nfs4_proc_remove,
7747 .unlink_setup = nfs4_proc_unlink_setup,
7748 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
7749 .unlink_done = nfs4_proc_unlink_done,
7750 .rename = nfs4_proc_rename,
7751 .rename_setup = nfs4_proc_rename_setup,
7752 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
7753 .rename_done = nfs4_proc_rename_done,
7754 .link = nfs4_proc_link,
7755 .symlink = nfs4_proc_symlink,
7756 .mkdir = nfs4_proc_mkdir,
7757 .rmdir = nfs4_proc_remove,
7758 .readdir = nfs4_proc_readdir,
7759 .mknod = nfs4_proc_mknod,
7760 .statfs = nfs4_proc_statfs,
7761 .fsinfo = nfs4_proc_fsinfo,
7762 .pathconf = nfs4_proc_pathconf,
7763 .set_capabilities = nfs4_server_capabilities,
7764 .decode_dirent = nfs4_decode_dirent,
7765 .read_setup = nfs4_proc_read_setup,
7766 .read_pageio_init = pnfs_pageio_init_read,
7767 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
7768 .read_done = nfs4_read_done,
7769 .write_setup = nfs4_proc_write_setup,
7770 .write_pageio_init = pnfs_pageio_init_write,
7771 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
7772 .write_done = nfs4_write_done,
7773 .commit_setup = nfs4_proc_commit_setup,
7774 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
7775 .commit_done = nfs4_commit_done,
7776 .lock = nfs4_proc_lock,
7777 .clear_acl_cache = nfs4_zap_acl_attr,
7778 .close_context = nfs4_close_context,
7779 .open_context = nfs4_atomic_open,
7780 .have_delegation = nfs4_have_delegation,
7781 .return_delegation = nfs4_inode_return_delegation,
7782 .alloc_client = nfs4_alloc_client,
7783 .init_client = nfs4_init_client,
7784 .free_client = nfs4_free_client,
7785 .create_server = nfs4_create_server,
7786 .clone_server = nfs_clone_server,
7787};
7788
7789static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7790 .prefix = XATTR_NAME_NFSV4_ACL,
7791 .list = nfs4_xattr_list_nfs4_acl,
7792 .get = nfs4_xattr_get_nfs4_acl,
7793 .set = nfs4_xattr_set_nfs4_acl,
7794};
7795
7796const struct xattr_handler *nfs4_xattr_handlers[] = {
7797 &nfs4_xattr_nfs4_acl_handler,
7798#ifdef CONFIG_NFS_V4_SECURITY_LABEL
7799 &nfs4_xattr_nfs4_label_handler,
7800#endif
7801 NULL
7802};
7803
7804/*
7805 * Local variables:
7806 * c-basic-offset: 8
7807 * End:
7808 */