mdio-sun4i: oops in error handling in probe
[linux/fpc-iii.git] / fs / nfs / nfs4proc.c
blob864b3214cc8989966e81033c4c2d9e76f2798fe1
1 /*
2 * fs/nfs/nfs4proc.c
4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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.
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.
38 #include <linux/mm.h>
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/file.h>
42 #include <linux/string.h>
43 #include <linux/ratelimit.h>
44 #include <linux/printk.h>
45 #include <linux/slab.h>
46 #include <linux/sunrpc/clnt.h>
47 #include <linux/nfs.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/nfs_mount.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/module.h>
55 #include <linux/nfs_idmap.h>
56 #include <linux/xattr.h>
57 #include <linux/utsname.h>
58 #include <linux/freezer.h>
60 #include "nfs4_fs.h"
61 #include "delegation.h"
62 #include "internal.h"
63 #include "iostat.h"
64 #include "callback.h"
65 #include "pnfs.h"
66 #include "netns.h"
67 #include "nfs4session.h"
68 #include "fscache.h"
70 #include "nfs4trace.h"
72 #define NFSDBG_FACILITY NFSDBG_PROC
74 #define NFS4_POLL_RETRY_MIN (HZ/10)
75 #define NFS4_POLL_RETRY_MAX (15*HZ)
77 struct nfs4_opendata;
78 static int _nfs4_proc_open(struct nfs4_opendata *data);
79 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
80 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
81 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
82 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
83 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
84 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
85 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
86 struct nfs_fattr *fattr, struct iattr *sattr,
87 struct nfs4_state *state, struct nfs4_label *ilabel,
88 struct nfs4_label *olabel);
89 #ifdef CONFIG_NFS_V4_1
90 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
91 struct rpc_cred *);
92 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
93 struct rpc_cred *);
94 #endif
96 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
97 static inline struct nfs4_label *
98 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
99 struct iattr *sattr, struct nfs4_label *label)
101 int err;
103 if (label == NULL)
104 return NULL;
106 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
107 return NULL;
109 err = security_dentry_init_security(dentry, sattr->ia_mode,
110 &dentry->d_name, (void **)&label->label, &label->len);
111 if (err == 0)
112 return label;
114 return NULL;
116 static inline void
117 nfs4_label_release_security(struct nfs4_label *label)
119 if (label)
120 security_release_secctx(label->label, label->len);
122 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
124 if (label)
125 return server->attr_bitmask;
127 return server->attr_bitmask_nl;
129 #else
130 static inline struct nfs4_label *
131 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
132 struct iattr *sattr, struct nfs4_label *l)
133 { return NULL; }
134 static inline void
135 nfs4_label_release_security(struct nfs4_label *label)
136 { return; }
137 static inline u32 *
138 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
139 { return server->attr_bitmask; }
140 #endif
142 /* Prevent leaks of NFSv4 errors into userland */
143 static int nfs4_map_errors(int err)
145 if (err >= -1000)
146 return err;
147 switch (err) {
148 case -NFS4ERR_RESOURCE:
149 case -NFS4ERR_LAYOUTTRYLATER:
150 case -NFS4ERR_RECALLCONFLICT:
151 return -EREMOTEIO;
152 case -NFS4ERR_WRONGSEC:
153 case -NFS4ERR_WRONG_CRED:
154 return -EPERM;
155 case -NFS4ERR_BADOWNER:
156 case -NFS4ERR_BADNAME:
157 return -EINVAL;
158 case -NFS4ERR_SHARE_DENIED:
159 return -EACCES;
160 case -NFS4ERR_MINOR_VERS_MISMATCH:
161 return -EPROTONOSUPPORT;
162 case -NFS4ERR_ACCESS:
163 return -EACCES;
164 case -NFS4ERR_FILE_OPEN:
165 return -EBUSY;
166 default:
167 dprintk("%s could not handle NFSv4 error %d\n",
168 __func__, -err);
169 break;
171 return -EIO;
175 * This is our standard bitmap for GETATTR requests.
177 const u32 nfs4_fattr_bitmap[3] = {
178 FATTR4_WORD0_TYPE
179 | FATTR4_WORD0_CHANGE
180 | FATTR4_WORD0_SIZE
181 | FATTR4_WORD0_FSID
182 | FATTR4_WORD0_FILEID,
183 FATTR4_WORD1_MODE
184 | FATTR4_WORD1_NUMLINKS
185 | FATTR4_WORD1_OWNER
186 | FATTR4_WORD1_OWNER_GROUP
187 | FATTR4_WORD1_RAWDEV
188 | FATTR4_WORD1_SPACE_USED
189 | FATTR4_WORD1_TIME_ACCESS
190 | FATTR4_WORD1_TIME_METADATA
191 | FATTR4_WORD1_TIME_MODIFY,
192 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
193 FATTR4_WORD2_SECURITY_LABEL
194 #endif
197 static const u32 nfs4_pnfs_open_bitmap[3] = {
198 FATTR4_WORD0_TYPE
199 | FATTR4_WORD0_CHANGE
200 | FATTR4_WORD0_SIZE
201 | FATTR4_WORD0_FSID
202 | FATTR4_WORD0_FILEID,
203 FATTR4_WORD1_MODE
204 | FATTR4_WORD1_NUMLINKS
205 | FATTR4_WORD1_OWNER
206 | FATTR4_WORD1_OWNER_GROUP
207 | FATTR4_WORD1_RAWDEV
208 | FATTR4_WORD1_SPACE_USED
209 | FATTR4_WORD1_TIME_ACCESS
210 | FATTR4_WORD1_TIME_METADATA
211 | FATTR4_WORD1_TIME_MODIFY,
212 FATTR4_WORD2_MDSTHRESHOLD
215 static const u32 nfs4_open_noattr_bitmap[3] = {
216 FATTR4_WORD0_TYPE
217 | FATTR4_WORD0_CHANGE
218 | FATTR4_WORD0_FILEID,
221 const u32 nfs4_statfs_bitmap[3] = {
222 FATTR4_WORD0_FILES_AVAIL
223 | FATTR4_WORD0_FILES_FREE
224 | FATTR4_WORD0_FILES_TOTAL,
225 FATTR4_WORD1_SPACE_AVAIL
226 | FATTR4_WORD1_SPACE_FREE
227 | FATTR4_WORD1_SPACE_TOTAL
230 const u32 nfs4_pathconf_bitmap[3] = {
231 FATTR4_WORD0_MAXLINK
232 | FATTR4_WORD0_MAXNAME,
236 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
237 | FATTR4_WORD0_MAXREAD
238 | FATTR4_WORD0_MAXWRITE
239 | FATTR4_WORD0_LEASE_TIME,
240 FATTR4_WORD1_TIME_DELTA
241 | FATTR4_WORD1_FS_LAYOUT_TYPES,
242 FATTR4_WORD2_LAYOUT_BLKSIZE
245 const u32 nfs4_fs_locations_bitmap[3] = {
246 FATTR4_WORD0_TYPE
247 | FATTR4_WORD0_CHANGE
248 | FATTR4_WORD0_SIZE
249 | FATTR4_WORD0_FSID
250 | FATTR4_WORD0_FILEID
251 | FATTR4_WORD0_FS_LOCATIONS,
252 FATTR4_WORD1_MODE
253 | FATTR4_WORD1_NUMLINKS
254 | FATTR4_WORD1_OWNER
255 | FATTR4_WORD1_OWNER_GROUP
256 | FATTR4_WORD1_RAWDEV
257 | FATTR4_WORD1_SPACE_USED
258 | FATTR4_WORD1_TIME_ACCESS
259 | FATTR4_WORD1_TIME_METADATA
260 | FATTR4_WORD1_TIME_MODIFY
261 | FATTR4_WORD1_MOUNTED_ON_FILEID,
264 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
265 struct nfs4_readdir_arg *readdir)
267 __be32 *start, *p;
269 if (cookie > 2) {
270 readdir->cookie = cookie;
271 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
272 return;
275 readdir->cookie = 0;
276 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
277 if (cookie == 2)
278 return;
281 * NFSv4 servers do not return entries for '.' and '..'
282 * Therefore, we fake these entries here. We let '.'
283 * have cookie 0 and '..' have cookie 1. Note that
284 * when talking to the server, we always send cookie 0
285 * instead of 1 or 2.
287 start = p = kmap_atomic(*readdir->pages);
289 if (cookie == 0) {
290 *p++ = xdr_one; /* next */
291 *p++ = xdr_zero; /* cookie, first word */
292 *p++ = xdr_one; /* cookie, second word */
293 *p++ = xdr_one; /* entry len */
294 memcpy(p, ".\0\0\0", 4); /* entry */
295 p++;
296 *p++ = xdr_one; /* bitmap length */
297 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
298 *p++ = htonl(8); /* attribute buffer length */
299 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
302 *p++ = xdr_one; /* next */
303 *p++ = xdr_zero; /* cookie, first word */
304 *p++ = xdr_two; /* cookie, second word */
305 *p++ = xdr_two; /* entry len */
306 memcpy(p, "..\0\0", 4); /* entry */
307 p++;
308 *p++ = xdr_one; /* bitmap length */
309 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
310 *p++ = htonl(8); /* attribute buffer length */
311 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
313 readdir->pgbase = (char *)p - (char *)start;
314 readdir->count -= readdir->pgbase;
315 kunmap_atomic(start);
318 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
320 int res = 0;
322 might_sleep();
324 if (*timeout <= 0)
325 *timeout = NFS4_POLL_RETRY_MIN;
326 if (*timeout > NFS4_POLL_RETRY_MAX)
327 *timeout = NFS4_POLL_RETRY_MAX;
328 freezable_schedule_timeout_killable_unsafe(*timeout);
329 if (fatal_signal_pending(current))
330 res = -ERESTARTSYS;
331 *timeout <<= 1;
332 return res;
335 /* This is the error handling routine for processes that are allowed
336 * to sleep.
338 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
340 struct nfs_client *clp = server->nfs_client;
341 struct nfs4_state *state = exception->state;
342 struct inode *inode = exception->inode;
343 int ret = errorcode;
345 exception->retry = 0;
346 switch(errorcode) {
347 case 0:
348 return 0;
349 case -NFS4ERR_OPENMODE:
350 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
351 nfs4_inode_return_delegation(inode);
352 exception->retry = 1;
353 return 0;
355 if (state == NULL)
356 break;
357 ret = nfs4_schedule_stateid_recovery(server, state);
358 if (ret < 0)
359 break;
360 goto wait_on_recovery;
361 case -NFS4ERR_DELEG_REVOKED:
362 case -NFS4ERR_ADMIN_REVOKED:
363 case -NFS4ERR_BAD_STATEID:
364 if (state == NULL)
365 break;
366 ret = nfs4_schedule_stateid_recovery(server, state);
367 if (ret < 0)
368 break;
369 goto wait_on_recovery;
370 case -NFS4ERR_EXPIRED:
371 if (state != NULL) {
372 ret = nfs4_schedule_stateid_recovery(server, state);
373 if (ret < 0)
374 break;
376 case -NFS4ERR_STALE_STATEID:
377 case -NFS4ERR_STALE_CLIENTID:
378 nfs4_schedule_lease_recovery(clp);
379 goto wait_on_recovery;
380 case -NFS4ERR_MOVED:
381 ret = nfs4_schedule_migration_recovery(server);
382 if (ret < 0)
383 break;
384 goto wait_on_recovery;
385 case -NFS4ERR_LEASE_MOVED:
386 nfs4_schedule_lease_moved_recovery(clp);
387 goto wait_on_recovery;
388 #if defined(CONFIG_NFS_V4_1)
389 case -NFS4ERR_BADSESSION:
390 case -NFS4ERR_BADSLOT:
391 case -NFS4ERR_BAD_HIGH_SLOT:
392 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
393 case -NFS4ERR_DEADSESSION:
394 case -NFS4ERR_SEQ_FALSE_RETRY:
395 case -NFS4ERR_SEQ_MISORDERED:
396 dprintk("%s ERROR: %d Reset session\n", __func__,
397 errorcode);
398 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
399 goto wait_on_recovery;
400 #endif /* defined(CONFIG_NFS_V4_1) */
401 case -NFS4ERR_FILE_OPEN:
402 if (exception->timeout > HZ) {
403 /* We have retried a decent amount, time to
404 * fail
406 ret = -EBUSY;
407 break;
409 case -NFS4ERR_GRACE:
410 case -NFS4ERR_DELAY:
411 ret = nfs4_delay(server->client, &exception->timeout);
412 if (ret != 0)
413 break;
414 case -NFS4ERR_RETRY_UNCACHED_REP:
415 case -NFS4ERR_OLD_STATEID:
416 exception->retry = 1;
417 break;
418 case -NFS4ERR_BADOWNER:
419 /* The following works around a Linux server bug! */
420 case -NFS4ERR_BADNAME:
421 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
422 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
423 exception->retry = 1;
424 printk(KERN_WARNING "NFS: v4 server %s "
425 "does not accept raw "
426 "uid/gids. "
427 "Reenabling the idmapper.\n",
428 server->nfs_client->cl_hostname);
431 /* We failed to handle the error */
432 return nfs4_map_errors(ret);
433 wait_on_recovery:
434 ret = nfs4_wait_clnt_recover(clp);
435 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
436 return -EIO;
437 if (ret == 0)
438 exception->retry = 1;
439 return ret;
443 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
444 * or 'false' otherwise.
446 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
448 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
450 if (flavor == RPC_AUTH_GSS_KRB5I ||
451 flavor == RPC_AUTH_GSS_KRB5P)
452 return true;
454 return false;
457 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
459 spin_lock(&clp->cl_lock);
460 if (time_before(clp->cl_last_renewal,timestamp))
461 clp->cl_last_renewal = timestamp;
462 spin_unlock(&clp->cl_lock);
465 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
467 do_renew_lease(server->nfs_client, timestamp);
470 struct nfs4_call_sync_data {
471 const struct nfs_server *seq_server;
472 struct nfs4_sequence_args *seq_args;
473 struct nfs4_sequence_res *seq_res;
476 static void nfs4_init_sequence(struct nfs4_sequence_args *args,
477 struct nfs4_sequence_res *res, int cache_reply)
479 args->sa_slot = NULL;
480 args->sa_cache_this = cache_reply;
481 args->sa_privileged = 0;
483 res->sr_slot = NULL;
486 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
488 args->sa_privileged = 1;
491 static int nfs40_setup_sequence(const struct nfs_server *server,
492 struct nfs4_sequence_args *args,
493 struct nfs4_sequence_res *res,
494 struct rpc_task *task)
496 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
497 struct nfs4_slot *slot;
499 /* slot already allocated? */
500 if (res->sr_slot != NULL)
501 goto out_start;
503 spin_lock(&tbl->slot_tbl_lock);
504 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
505 goto out_sleep;
507 slot = nfs4_alloc_slot(tbl);
508 if (IS_ERR(slot)) {
509 if (slot == ERR_PTR(-ENOMEM))
510 task->tk_timeout = HZ >> 2;
511 goto out_sleep;
513 spin_unlock(&tbl->slot_tbl_lock);
515 args->sa_slot = slot;
516 res->sr_slot = slot;
518 out_start:
519 rpc_call_start(task);
520 return 0;
522 out_sleep:
523 if (args->sa_privileged)
524 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
525 NULL, RPC_PRIORITY_PRIVILEGED);
526 else
527 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
528 spin_unlock(&tbl->slot_tbl_lock);
529 return -EAGAIN;
532 static int nfs40_sequence_done(struct rpc_task *task,
533 struct nfs4_sequence_res *res)
535 struct nfs4_slot *slot = res->sr_slot;
536 struct nfs4_slot_table *tbl;
538 if (slot == NULL)
539 goto out;
541 tbl = slot->table;
542 spin_lock(&tbl->slot_tbl_lock);
543 if (!nfs41_wake_and_assign_slot(tbl, slot))
544 nfs4_free_slot(tbl, slot);
545 spin_unlock(&tbl->slot_tbl_lock);
547 res->sr_slot = NULL;
548 out:
549 return 1;
552 #if defined(CONFIG_NFS_V4_1)
554 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
556 struct nfs4_session *session;
557 struct nfs4_slot_table *tbl;
558 struct nfs4_slot *slot = res->sr_slot;
559 bool send_new_highest_used_slotid = false;
561 tbl = slot->table;
562 session = tbl->session;
564 spin_lock(&tbl->slot_tbl_lock);
565 /* Be nice to the server: try to ensure that the last transmitted
566 * value for highest_user_slotid <= target_highest_slotid
568 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
569 send_new_highest_used_slotid = true;
571 if (nfs41_wake_and_assign_slot(tbl, slot)) {
572 send_new_highest_used_slotid = false;
573 goto out_unlock;
575 nfs4_free_slot(tbl, slot);
577 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
578 send_new_highest_used_slotid = false;
579 out_unlock:
580 spin_unlock(&tbl->slot_tbl_lock);
581 res->sr_slot = NULL;
582 if (send_new_highest_used_slotid)
583 nfs41_server_notify_highest_slotid_update(session->clp);
586 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
588 struct nfs4_session *session;
589 struct nfs4_slot *slot = res->sr_slot;
590 struct nfs_client *clp;
591 bool interrupted = false;
592 int ret = 1;
594 if (slot == NULL)
595 goto out_noaction;
596 /* don't increment the sequence number if the task wasn't sent */
597 if (!RPC_WAS_SENT(task))
598 goto out;
600 session = slot->table->session;
602 if (slot->interrupted) {
603 slot->interrupted = 0;
604 interrupted = true;
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:
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.
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.
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:
641 * The slot id we used was probably retired. Try again
642 * using a different slot id.
644 goto retry_nowait;
645 case -NFS4ERR_SEQ_MISORDERED:
647 * Was the last operation on this sequence interrupted?
648 * If so, retry after bumping the sequence number.
650 if (interrupted) {
651 ++slot->seq_nr;
652 goto retry_nowait;
655 * Could this slot have been previously retired?
656 * If so, then the server may be expecting seq_nr = 1!
658 if (slot->seq_nr != 1) {
659 slot->seq_nr = 1;
660 goto retry_nowait;
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;
670 out:
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 out_noaction:
675 return ret;
676 retry_nowait:
677 if (rpc_restart_call_prepare(task)) {
678 task->tk_status = 0;
679 ret = 0;
681 goto out;
682 out_retry:
683 if (!rpc_restart_call(task))
684 goto out;
685 rpc_delay(task, NFS4_POLL_RETRY_MAX);
686 return 0;
688 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
690 static int nfs4_sequence_done(struct rpc_task *task,
691 struct nfs4_sequence_res *res)
693 if (res->sr_slot == NULL)
694 return 1;
695 if (!res->sr_slot->table->session)
696 return nfs40_sequence_done(task, res);
697 return nfs41_sequence_done(task, res);
700 int nfs41_setup_sequence(struct nfs4_session *session,
701 struct nfs4_sequence_args *args,
702 struct nfs4_sequence_res *res,
703 struct rpc_task *task)
705 struct nfs4_slot *slot;
706 struct nfs4_slot_table *tbl;
708 dprintk("--> %s\n", __func__);
709 /* slot already allocated? */
710 if (res->sr_slot != NULL)
711 goto out_success;
713 tbl = &session->fc_slot_table;
715 task->tk_timeout = 0;
717 spin_lock(&tbl->slot_tbl_lock);
718 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
719 !args->sa_privileged) {
720 /* The state manager will wait until the slot table is empty */
721 dprintk("%s session is draining\n", __func__);
722 goto out_sleep;
725 slot = nfs4_alloc_slot(tbl);
726 if (IS_ERR(slot)) {
727 /* If out of memory, try again in 1/4 second */
728 if (slot == ERR_PTR(-ENOMEM))
729 task->tk_timeout = HZ >> 2;
730 dprintk("<-- %s: no free slots\n", __func__);
731 goto out_sleep;
733 spin_unlock(&tbl->slot_tbl_lock);
735 args->sa_slot = slot;
737 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
738 slot->slot_nr, slot->seq_nr);
740 res->sr_slot = slot;
741 res->sr_timestamp = jiffies;
742 res->sr_status_flags = 0;
744 * sr_status is only set in decode_sequence, and so will remain
745 * set to 1 if an rpc level failure occurs.
747 res->sr_status = 1;
748 trace_nfs4_setup_sequence(session, args);
749 out_success:
750 rpc_call_start(task);
751 return 0;
752 out_sleep:
753 /* Privileged tasks are queued with top priority */
754 if (args->sa_privileged)
755 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
756 NULL, RPC_PRIORITY_PRIVILEGED);
757 else
758 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
759 spin_unlock(&tbl->slot_tbl_lock);
760 return -EAGAIN;
762 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
764 static int nfs4_setup_sequence(const struct nfs_server *server,
765 struct nfs4_sequence_args *args,
766 struct nfs4_sequence_res *res,
767 struct rpc_task *task)
769 struct nfs4_session *session = nfs4_get_session(server);
770 int ret = 0;
772 if (!session)
773 return nfs40_setup_sequence(server, args, res, task);
775 dprintk("--> %s clp %p session %p sr_slot %u\n",
776 __func__, session->clp, session, res->sr_slot ?
777 res->sr_slot->slot_nr : NFS4_NO_SLOT);
779 ret = nfs41_setup_sequence(session, args, res, task);
781 dprintk("<-- %s status=%d\n", __func__, ret);
782 return ret;
785 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
787 struct nfs4_call_sync_data *data = calldata;
788 struct nfs4_session *session = nfs4_get_session(data->seq_server);
790 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
792 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
795 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
797 struct nfs4_call_sync_data *data = calldata;
799 nfs41_sequence_done(task, data->seq_res);
802 static const struct rpc_call_ops nfs41_call_sync_ops = {
803 .rpc_call_prepare = nfs41_call_sync_prepare,
804 .rpc_call_done = nfs41_call_sync_done,
807 #else /* !CONFIG_NFS_V4_1 */
809 static int nfs4_setup_sequence(const struct nfs_server *server,
810 struct nfs4_sequence_args *args,
811 struct nfs4_sequence_res *res,
812 struct rpc_task *task)
814 return nfs40_setup_sequence(server, args, res, task);
817 static int nfs4_sequence_done(struct rpc_task *task,
818 struct nfs4_sequence_res *res)
820 return nfs40_sequence_done(task, res);
823 #endif /* !CONFIG_NFS_V4_1 */
825 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
827 struct nfs4_call_sync_data *data = calldata;
828 nfs4_setup_sequence(data->seq_server,
829 data->seq_args, data->seq_res, task);
832 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
834 struct nfs4_call_sync_data *data = calldata;
835 nfs4_sequence_done(task, data->seq_res);
838 static const struct rpc_call_ops nfs40_call_sync_ops = {
839 .rpc_call_prepare = nfs40_call_sync_prepare,
840 .rpc_call_done = nfs40_call_sync_done,
843 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
844 struct nfs_server *server,
845 struct rpc_message *msg,
846 struct nfs4_sequence_args *args,
847 struct nfs4_sequence_res *res)
849 int ret;
850 struct rpc_task *task;
851 struct nfs_client *clp = server->nfs_client;
852 struct nfs4_call_sync_data data = {
853 .seq_server = server,
854 .seq_args = args,
855 .seq_res = res,
857 struct rpc_task_setup task_setup = {
858 .rpc_client = clnt,
859 .rpc_message = msg,
860 .callback_ops = clp->cl_mvops->call_sync_ops,
861 .callback_data = &data
864 task = rpc_run_task(&task_setup);
865 if (IS_ERR(task))
866 ret = PTR_ERR(task);
867 else {
868 ret = task->tk_status;
869 rpc_put_task(task);
871 return ret;
874 static
875 int nfs4_call_sync(struct rpc_clnt *clnt,
876 struct nfs_server *server,
877 struct rpc_message *msg,
878 struct nfs4_sequence_args *args,
879 struct nfs4_sequence_res *res,
880 int cache_reply)
882 nfs4_init_sequence(args, res, cache_reply);
883 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
886 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
888 struct nfs_inode *nfsi = NFS_I(dir);
890 spin_lock(&dir->i_lock);
891 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
892 if (!cinfo->atomic || cinfo->before != dir->i_version)
893 nfs_force_lookup_revalidate(dir);
894 dir->i_version = cinfo->after;
895 nfs_fscache_invalidate(dir);
896 spin_unlock(&dir->i_lock);
899 struct nfs4_opendata {
900 struct kref kref;
901 struct nfs_openargs o_arg;
902 struct nfs_openres o_res;
903 struct nfs_open_confirmargs c_arg;
904 struct nfs_open_confirmres c_res;
905 struct nfs4_string owner_name;
906 struct nfs4_string group_name;
907 struct nfs_fattr f_attr;
908 struct nfs4_label *f_label;
909 struct dentry *dir;
910 struct dentry *dentry;
911 struct nfs4_state_owner *owner;
912 struct nfs4_state *state;
913 struct iattr attrs;
914 unsigned long timestamp;
915 unsigned int rpc_done : 1;
916 unsigned int file_created : 1;
917 unsigned int is_recover : 1;
918 int rpc_status;
919 int cancelled;
922 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
923 int err, struct nfs4_exception *exception)
925 if (err != -EINVAL)
926 return false;
927 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
928 return false;
929 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
930 exception->retry = 1;
931 return true;
934 static enum open_claim_type4
935 nfs4_map_atomic_open_claim(struct nfs_server *server,
936 enum open_claim_type4 claim)
938 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
939 return claim;
940 switch (claim) {
941 default:
942 return claim;
943 case NFS4_OPEN_CLAIM_FH:
944 return NFS4_OPEN_CLAIM_NULL;
945 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
946 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
947 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
948 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
952 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
954 p->o_res.f_attr = &p->f_attr;
955 p->o_res.f_label = p->f_label;
956 p->o_res.seqid = p->o_arg.seqid;
957 p->c_res.seqid = p->c_arg.seqid;
958 p->o_res.server = p->o_arg.server;
959 p->o_res.access_request = p->o_arg.access;
960 nfs_fattr_init(&p->f_attr);
961 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
964 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
965 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
966 const struct iattr *attrs,
967 struct nfs4_label *label,
968 enum open_claim_type4 claim,
969 gfp_t gfp_mask)
971 struct dentry *parent = dget_parent(dentry);
972 struct inode *dir = parent->d_inode;
973 struct nfs_server *server = NFS_SERVER(dir);
974 struct nfs4_opendata *p;
976 p = kzalloc(sizeof(*p), gfp_mask);
977 if (p == NULL)
978 goto err;
980 p->f_label = nfs4_label_alloc(server, gfp_mask);
981 if (IS_ERR(p->f_label))
982 goto err_free_p;
984 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
985 if (p->o_arg.seqid == NULL)
986 goto err_free_label;
987 nfs_sb_active(dentry->d_sb);
988 p->dentry = dget(dentry);
989 p->dir = parent;
990 p->owner = sp;
991 atomic_inc(&sp->so_count);
992 p->o_arg.open_flags = flags;
993 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
994 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
995 * will return permission denied for all bits until close */
996 if (!(flags & O_EXCL)) {
997 /* ask server to check for all possible rights as results
998 * are cached */
999 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1000 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1002 p->o_arg.clientid = server->nfs_client->cl_clientid;
1003 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1004 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1005 p->o_arg.name = &dentry->d_name;
1006 p->o_arg.server = server;
1007 p->o_arg.bitmask = nfs4_bitmask(server, label);
1008 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1009 p->o_arg.label = label;
1010 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1011 switch (p->o_arg.claim) {
1012 case NFS4_OPEN_CLAIM_NULL:
1013 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1014 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1015 p->o_arg.fh = NFS_FH(dir);
1016 break;
1017 case NFS4_OPEN_CLAIM_PREVIOUS:
1018 case NFS4_OPEN_CLAIM_FH:
1019 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1020 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1021 p->o_arg.fh = NFS_FH(dentry->d_inode);
1023 if (attrs != NULL && attrs->ia_valid != 0) {
1024 __u32 verf[2];
1026 p->o_arg.u.attrs = &p->attrs;
1027 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1029 verf[0] = jiffies;
1030 verf[1] = current->pid;
1031 memcpy(p->o_arg.u.verifier.data, verf,
1032 sizeof(p->o_arg.u.verifier.data));
1034 p->c_arg.fh = &p->o_res.fh;
1035 p->c_arg.stateid = &p->o_res.stateid;
1036 p->c_arg.seqid = p->o_arg.seqid;
1037 nfs4_init_opendata_res(p);
1038 kref_init(&p->kref);
1039 return p;
1041 err_free_label:
1042 nfs4_label_free(p->f_label);
1043 err_free_p:
1044 kfree(p);
1045 err:
1046 dput(parent);
1047 return NULL;
1050 static void nfs4_opendata_free(struct kref *kref)
1052 struct nfs4_opendata *p = container_of(kref,
1053 struct nfs4_opendata, kref);
1054 struct super_block *sb = p->dentry->d_sb;
1056 nfs_free_seqid(p->o_arg.seqid);
1057 if (p->state != NULL)
1058 nfs4_put_open_state(p->state);
1059 nfs4_put_state_owner(p->owner);
1061 nfs4_label_free(p->f_label);
1063 dput(p->dir);
1064 dput(p->dentry);
1065 nfs_sb_deactive(sb);
1066 nfs_fattr_free_names(&p->f_attr);
1067 kfree(p->f_attr.mdsthreshold);
1068 kfree(p);
1071 static void nfs4_opendata_put(struct nfs4_opendata *p)
1073 if (p != NULL)
1074 kref_put(&p->kref, nfs4_opendata_free);
1077 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1079 int ret;
1081 ret = rpc_wait_for_completion_task(task);
1082 return ret;
1085 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1087 int ret = 0;
1089 if (open_mode & (O_EXCL|O_TRUNC))
1090 goto out;
1091 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1092 case FMODE_READ:
1093 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1094 && state->n_rdonly != 0;
1095 break;
1096 case FMODE_WRITE:
1097 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1098 && state->n_wronly != 0;
1099 break;
1100 case FMODE_READ|FMODE_WRITE:
1101 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1102 && state->n_rdwr != 0;
1104 out:
1105 return ret;
1108 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
1110 if (delegation == NULL)
1111 return 0;
1112 if ((delegation->type & fmode) != fmode)
1113 return 0;
1114 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1115 return 0;
1116 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1117 return 0;
1118 nfs_mark_delegation_referenced(delegation);
1119 return 1;
1122 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1124 switch (fmode) {
1125 case FMODE_WRITE:
1126 state->n_wronly++;
1127 break;
1128 case FMODE_READ:
1129 state->n_rdonly++;
1130 break;
1131 case FMODE_READ|FMODE_WRITE:
1132 state->n_rdwr++;
1134 nfs4_state_set_mode_locked(state, state->state | fmode);
1137 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1139 struct nfs_client *clp = state->owner->so_server->nfs_client;
1140 bool need_recover = false;
1142 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1143 need_recover = true;
1144 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1145 need_recover = true;
1146 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1147 need_recover = true;
1148 if (need_recover)
1149 nfs4_state_mark_reclaim_nograce(clp, state);
1152 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1153 nfs4_stateid *stateid)
1155 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1156 return true;
1157 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1158 nfs_test_and_clear_all_open_stateid(state);
1159 return true;
1161 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1162 return true;
1163 return false;
1166 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1167 nfs4_stateid *stateid, fmode_t fmode)
1169 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1170 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1171 case FMODE_WRITE:
1172 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1173 break;
1174 case FMODE_READ:
1175 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1176 break;
1177 case 0:
1178 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1179 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1180 clear_bit(NFS_OPEN_STATE, &state->flags);
1182 if (stateid == NULL)
1183 return;
1184 if (!nfs_need_update_open_stateid(state, stateid))
1185 return;
1186 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1187 nfs4_stateid_copy(&state->stateid, stateid);
1188 nfs4_stateid_copy(&state->open_stateid, stateid);
1191 static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1193 write_seqlock(&state->seqlock);
1194 nfs_clear_open_stateid_locked(state, stateid, fmode);
1195 write_sequnlock(&state->seqlock);
1196 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1197 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1200 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1202 switch (fmode) {
1203 case FMODE_READ:
1204 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1205 break;
1206 case FMODE_WRITE:
1207 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1208 break;
1209 case FMODE_READ|FMODE_WRITE:
1210 set_bit(NFS_O_RDWR_STATE, &state->flags);
1212 if (!nfs_need_update_open_stateid(state, stateid))
1213 return;
1214 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1215 nfs4_stateid_copy(&state->stateid, stateid);
1216 nfs4_stateid_copy(&state->open_stateid, stateid);
1219 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1222 * Protect the call to nfs4_state_set_mode_locked and
1223 * serialise the stateid update
1225 spin_lock(&state->owner->so_lock);
1226 write_seqlock(&state->seqlock);
1227 if (deleg_stateid != NULL) {
1228 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1229 set_bit(NFS_DELEGATED_STATE, &state->flags);
1231 if (open_stateid != NULL)
1232 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1233 write_sequnlock(&state->seqlock);
1234 update_open_stateflags(state, fmode);
1235 spin_unlock(&state->owner->so_lock);
1238 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1240 struct nfs_inode *nfsi = NFS_I(state->inode);
1241 struct nfs_delegation *deleg_cur;
1242 int ret = 0;
1244 fmode &= (FMODE_READ|FMODE_WRITE);
1246 rcu_read_lock();
1247 deleg_cur = rcu_dereference(nfsi->delegation);
1248 if (deleg_cur == NULL)
1249 goto no_delegation;
1251 spin_lock(&deleg_cur->lock);
1252 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1253 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1254 (deleg_cur->type & fmode) != fmode)
1255 goto no_delegation_unlock;
1257 if (delegation == NULL)
1258 delegation = &deleg_cur->stateid;
1259 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1260 goto no_delegation_unlock;
1262 nfs_mark_delegation_referenced(deleg_cur);
1263 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1264 ret = 1;
1265 no_delegation_unlock:
1266 spin_unlock(&deleg_cur->lock);
1267 no_delegation:
1268 rcu_read_unlock();
1270 if (!ret && open_stateid != NULL) {
1271 __update_open_stateid(state, open_stateid, NULL, fmode);
1272 ret = 1;
1274 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1275 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1277 return ret;
1281 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1283 struct nfs_delegation *delegation;
1285 rcu_read_lock();
1286 delegation = rcu_dereference(NFS_I(inode)->delegation);
1287 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1288 rcu_read_unlock();
1289 return;
1291 rcu_read_unlock();
1292 nfs4_inode_return_delegation(inode);
1295 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1297 struct nfs4_state *state = opendata->state;
1298 struct nfs_inode *nfsi = NFS_I(state->inode);
1299 struct nfs_delegation *delegation;
1300 int open_mode = opendata->o_arg.open_flags;
1301 fmode_t fmode = opendata->o_arg.fmode;
1302 nfs4_stateid stateid;
1303 int ret = -EAGAIN;
1305 for (;;) {
1306 if (can_open_cached(state, fmode, open_mode)) {
1307 spin_lock(&state->owner->so_lock);
1308 if (can_open_cached(state, fmode, open_mode)) {
1309 update_open_stateflags(state, fmode);
1310 spin_unlock(&state->owner->so_lock);
1311 goto out_return_state;
1313 spin_unlock(&state->owner->so_lock);
1315 rcu_read_lock();
1316 delegation = rcu_dereference(nfsi->delegation);
1317 if (!can_open_delegated(delegation, fmode)) {
1318 rcu_read_unlock();
1319 break;
1321 /* Save the delegation */
1322 nfs4_stateid_copy(&stateid, &delegation->stateid);
1323 rcu_read_unlock();
1324 nfs_release_seqid(opendata->o_arg.seqid);
1325 if (!opendata->is_recover) {
1326 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1327 if (ret != 0)
1328 goto out;
1330 ret = -EAGAIN;
1332 /* Try to update the stateid using the delegation */
1333 if (update_open_stateid(state, NULL, &stateid, fmode))
1334 goto out_return_state;
1336 out:
1337 return ERR_PTR(ret);
1338 out_return_state:
1339 atomic_inc(&state->count);
1340 return state;
1343 static void
1344 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1346 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1347 struct nfs_delegation *delegation;
1348 int delegation_flags = 0;
1350 rcu_read_lock();
1351 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1352 if (delegation)
1353 delegation_flags = delegation->flags;
1354 rcu_read_unlock();
1355 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1356 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1357 "returning a delegation for "
1358 "OPEN(CLAIM_DELEGATE_CUR)\n",
1359 clp->cl_hostname);
1360 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1361 nfs_inode_set_delegation(state->inode,
1362 data->owner->so_cred,
1363 &data->o_res);
1364 else
1365 nfs_inode_reclaim_delegation(state->inode,
1366 data->owner->so_cred,
1367 &data->o_res);
1371 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1372 * and update the nfs4_state.
1374 static struct nfs4_state *
1375 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1377 struct inode *inode = data->state->inode;
1378 struct nfs4_state *state = data->state;
1379 int ret;
1381 if (!data->rpc_done) {
1382 if (data->rpc_status) {
1383 ret = data->rpc_status;
1384 goto err;
1386 /* cached opens have already been processed */
1387 goto update;
1390 ret = nfs_refresh_inode(inode, &data->f_attr);
1391 if (ret)
1392 goto err;
1394 if (data->o_res.delegation_type != 0)
1395 nfs4_opendata_check_deleg(data, state);
1396 update:
1397 update_open_stateid(state, &data->o_res.stateid, NULL,
1398 data->o_arg.fmode);
1399 atomic_inc(&state->count);
1401 return state;
1402 err:
1403 return ERR_PTR(ret);
1407 static struct nfs4_state *
1408 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1410 struct inode *inode;
1411 struct nfs4_state *state = NULL;
1412 int ret;
1414 if (!data->rpc_done) {
1415 state = nfs4_try_open_cached(data);
1416 goto out;
1419 ret = -EAGAIN;
1420 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1421 goto err;
1422 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1423 ret = PTR_ERR(inode);
1424 if (IS_ERR(inode))
1425 goto err;
1426 ret = -ENOMEM;
1427 state = nfs4_get_open_state(inode, data->owner);
1428 if (state == NULL)
1429 goto err_put_inode;
1430 if (data->o_res.delegation_type != 0)
1431 nfs4_opendata_check_deleg(data, state);
1432 update_open_stateid(state, &data->o_res.stateid, NULL,
1433 data->o_arg.fmode);
1434 iput(inode);
1435 out:
1436 nfs_release_seqid(data->o_arg.seqid);
1437 return state;
1438 err_put_inode:
1439 iput(inode);
1440 err:
1441 return ERR_PTR(ret);
1444 static struct nfs4_state *
1445 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1447 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1448 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1449 return _nfs4_opendata_to_nfs4_state(data);
1452 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1454 struct nfs_inode *nfsi = NFS_I(state->inode);
1455 struct nfs_open_context *ctx;
1457 spin_lock(&state->inode->i_lock);
1458 list_for_each_entry(ctx, &nfsi->open_files, list) {
1459 if (ctx->state != state)
1460 continue;
1461 get_nfs_open_context(ctx);
1462 spin_unlock(&state->inode->i_lock);
1463 return ctx;
1465 spin_unlock(&state->inode->i_lock);
1466 return ERR_PTR(-ENOENT);
1469 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1470 struct nfs4_state *state, enum open_claim_type4 claim)
1472 struct nfs4_opendata *opendata;
1474 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1475 NULL, NULL, claim, GFP_NOFS);
1476 if (opendata == NULL)
1477 return ERR_PTR(-ENOMEM);
1478 opendata->state = state;
1479 atomic_inc(&state->count);
1480 return opendata;
1483 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1485 struct nfs4_state *newstate;
1486 int ret;
1488 opendata->o_arg.open_flags = 0;
1489 opendata->o_arg.fmode = fmode;
1490 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1491 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1492 nfs4_init_opendata_res(opendata);
1493 ret = _nfs4_recover_proc_open(opendata);
1494 if (ret != 0)
1495 return ret;
1496 newstate = nfs4_opendata_to_nfs4_state(opendata);
1497 if (IS_ERR(newstate))
1498 return PTR_ERR(newstate);
1499 nfs4_close_state(newstate, fmode);
1500 *res = newstate;
1501 return 0;
1504 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1506 struct nfs4_state *newstate;
1507 int ret;
1509 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1510 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1511 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1512 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1513 /* memory barrier prior to reading state->n_* */
1514 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1515 clear_bit(NFS_OPEN_STATE, &state->flags);
1516 smp_rmb();
1517 if (state->n_rdwr != 0) {
1518 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1519 if (ret != 0)
1520 return ret;
1521 if (newstate != state)
1522 return -ESTALE;
1524 if (state->n_wronly != 0) {
1525 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1526 if (ret != 0)
1527 return ret;
1528 if (newstate != state)
1529 return -ESTALE;
1531 if (state->n_rdonly != 0) {
1532 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1533 if (ret != 0)
1534 return ret;
1535 if (newstate != state)
1536 return -ESTALE;
1539 * We may have performed cached opens for all three recoveries.
1540 * Check if we need to update the current stateid.
1542 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1543 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1544 write_seqlock(&state->seqlock);
1545 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1546 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1547 write_sequnlock(&state->seqlock);
1549 return 0;
1553 * OPEN_RECLAIM:
1554 * reclaim state on the server after a reboot.
1556 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1558 struct nfs_delegation *delegation;
1559 struct nfs4_opendata *opendata;
1560 fmode_t delegation_type = 0;
1561 int status;
1563 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1564 NFS4_OPEN_CLAIM_PREVIOUS);
1565 if (IS_ERR(opendata))
1566 return PTR_ERR(opendata);
1567 rcu_read_lock();
1568 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1569 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1570 delegation_type = delegation->type;
1571 rcu_read_unlock();
1572 opendata->o_arg.u.delegation_type = delegation_type;
1573 status = nfs4_open_recover(opendata, state);
1574 nfs4_opendata_put(opendata);
1575 return status;
1578 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1580 struct nfs_server *server = NFS_SERVER(state->inode);
1581 struct nfs4_exception exception = { };
1582 int err;
1583 do {
1584 err = _nfs4_do_open_reclaim(ctx, state);
1585 trace_nfs4_open_reclaim(ctx, 0, err);
1586 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1587 continue;
1588 if (err != -NFS4ERR_DELAY)
1589 break;
1590 nfs4_handle_exception(server, err, &exception);
1591 } while (exception.retry);
1592 return err;
1595 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1597 struct nfs_open_context *ctx;
1598 int ret;
1600 ctx = nfs4_state_find_open_context(state);
1601 if (IS_ERR(ctx))
1602 return -EAGAIN;
1603 ret = nfs4_do_open_reclaim(ctx, state);
1604 put_nfs_open_context(ctx);
1605 return ret;
1608 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1610 switch (err) {
1611 default:
1612 printk(KERN_ERR "NFS: %s: unhandled error "
1613 "%d.\n", __func__, err);
1614 case 0:
1615 case -ENOENT:
1616 case -ESTALE:
1617 break;
1618 case -NFS4ERR_BADSESSION:
1619 case -NFS4ERR_BADSLOT:
1620 case -NFS4ERR_BAD_HIGH_SLOT:
1621 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1622 case -NFS4ERR_DEADSESSION:
1623 set_bit(NFS_DELEGATED_STATE, &state->flags);
1624 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1625 return -EAGAIN;
1626 case -NFS4ERR_STALE_CLIENTID:
1627 case -NFS4ERR_STALE_STATEID:
1628 set_bit(NFS_DELEGATED_STATE, &state->flags);
1629 case -NFS4ERR_EXPIRED:
1630 /* Don't recall a delegation if it was lost */
1631 nfs4_schedule_lease_recovery(server->nfs_client);
1632 return -EAGAIN;
1633 case -NFS4ERR_MOVED:
1634 nfs4_schedule_migration_recovery(server);
1635 return -EAGAIN;
1636 case -NFS4ERR_LEASE_MOVED:
1637 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1638 return -EAGAIN;
1639 case -NFS4ERR_DELEG_REVOKED:
1640 case -NFS4ERR_ADMIN_REVOKED:
1641 case -NFS4ERR_BAD_STATEID:
1642 case -NFS4ERR_OPENMODE:
1643 nfs_inode_find_state_and_recover(state->inode,
1644 stateid);
1645 nfs4_schedule_stateid_recovery(server, state);
1646 return -EAGAIN;
1647 case -NFS4ERR_DELAY:
1648 case -NFS4ERR_GRACE:
1649 set_bit(NFS_DELEGATED_STATE, &state->flags);
1650 ssleep(1);
1651 return -EAGAIN;
1652 case -ENOMEM:
1653 case -NFS4ERR_DENIED:
1654 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1655 return 0;
1657 return err;
1660 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1662 struct nfs_server *server = NFS_SERVER(state->inode);
1663 struct nfs4_opendata *opendata;
1664 int err;
1666 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1667 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1668 if (IS_ERR(opendata))
1669 return PTR_ERR(opendata);
1670 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1671 err = nfs4_open_recover(opendata, state);
1672 nfs4_opendata_put(opendata);
1673 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1676 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1678 struct nfs4_opendata *data = calldata;
1680 nfs40_setup_sequence(data->o_arg.server, &data->c_arg.seq_args,
1681 &data->c_res.seq_res, task);
1684 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1686 struct nfs4_opendata *data = calldata;
1688 nfs40_sequence_done(task, &data->c_res.seq_res);
1690 data->rpc_status = task->tk_status;
1691 if (data->rpc_status == 0) {
1692 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1693 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1694 renew_lease(data->o_res.server, data->timestamp);
1695 data->rpc_done = 1;
1699 static void nfs4_open_confirm_release(void *calldata)
1701 struct nfs4_opendata *data = calldata;
1702 struct nfs4_state *state = NULL;
1704 /* If this request hasn't been cancelled, do nothing */
1705 if (data->cancelled == 0)
1706 goto out_free;
1707 /* In case of error, no cleanup! */
1708 if (!data->rpc_done)
1709 goto out_free;
1710 state = nfs4_opendata_to_nfs4_state(data);
1711 if (!IS_ERR(state))
1712 nfs4_close_state(state, data->o_arg.fmode);
1713 out_free:
1714 nfs4_opendata_put(data);
1717 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1718 .rpc_call_prepare = nfs4_open_confirm_prepare,
1719 .rpc_call_done = nfs4_open_confirm_done,
1720 .rpc_release = nfs4_open_confirm_release,
1724 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1726 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1728 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1729 struct rpc_task *task;
1730 struct rpc_message msg = {
1731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1732 .rpc_argp = &data->c_arg,
1733 .rpc_resp = &data->c_res,
1734 .rpc_cred = data->owner->so_cred,
1736 struct rpc_task_setup task_setup_data = {
1737 .rpc_client = server->client,
1738 .rpc_message = &msg,
1739 .callback_ops = &nfs4_open_confirm_ops,
1740 .callback_data = data,
1741 .workqueue = nfsiod_workqueue,
1742 .flags = RPC_TASK_ASYNC,
1744 int status;
1746 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
1747 kref_get(&data->kref);
1748 data->rpc_done = 0;
1749 data->rpc_status = 0;
1750 data->timestamp = jiffies;
1751 task = rpc_run_task(&task_setup_data);
1752 if (IS_ERR(task))
1753 return PTR_ERR(task);
1754 status = nfs4_wait_for_completion_rpc_task(task);
1755 if (status != 0) {
1756 data->cancelled = 1;
1757 smp_wmb();
1758 } else
1759 status = data->rpc_status;
1760 rpc_put_task(task);
1761 return status;
1764 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1766 struct nfs4_opendata *data = calldata;
1767 struct nfs4_state_owner *sp = data->owner;
1768 struct nfs_client *clp = sp->so_server->nfs_client;
1770 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1771 goto out_wait;
1773 * Check if we still need to send an OPEN call, or if we can use
1774 * a delegation instead.
1776 if (data->state != NULL) {
1777 struct nfs_delegation *delegation;
1779 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1780 goto out_no_action;
1781 rcu_read_lock();
1782 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1783 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1784 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
1785 can_open_delegated(delegation, data->o_arg.fmode))
1786 goto unlock_no_action;
1787 rcu_read_unlock();
1789 /* Update client id. */
1790 data->o_arg.clientid = clp->cl_clientid;
1791 switch (data->o_arg.claim) {
1792 case NFS4_OPEN_CLAIM_PREVIOUS:
1793 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1794 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1795 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1796 case NFS4_OPEN_CLAIM_FH:
1797 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1798 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1800 data->timestamp = jiffies;
1801 if (nfs4_setup_sequence(data->o_arg.server,
1802 &data->o_arg.seq_args,
1803 &data->o_res.seq_res,
1804 task) != 0)
1805 nfs_release_seqid(data->o_arg.seqid);
1807 /* Set the create mode (note dependency on the session type) */
1808 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1809 if (data->o_arg.open_flags & O_EXCL) {
1810 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1811 if (nfs4_has_persistent_session(clp))
1812 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1813 else if (clp->cl_mvops->minor_version > 0)
1814 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1816 return;
1817 unlock_no_action:
1818 rcu_read_unlock();
1819 out_no_action:
1820 task->tk_action = NULL;
1821 out_wait:
1822 nfs4_sequence_done(task, &data->o_res.seq_res);
1825 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1827 struct nfs4_opendata *data = calldata;
1829 data->rpc_status = task->tk_status;
1831 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1832 return;
1834 if (task->tk_status == 0) {
1835 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1836 switch (data->o_res.f_attr->mode & S_IFMT) {
1837 case S_IFREG:
1838 break;
1839 case S_IFLNK:
1840 data->rpc_status = -ELOOP;
1841 break;
1842 case S_IFDIR:
1843 data->rpc_status = -EISDIR;
1844 break;
1845 default:
1846 data->rpc_status = -ENOTDIR;
1849 renew_lease(data->o_res.server, data->timestamp);
1850 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1851 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1853 data->rpc_done = 1;
1856 static void nfs4_open_release(void *calldata)
1858 struct nfs4_opendata *data = calldata;
1859 struct nfs4_state *state = NULL;
1861 /* If this request hasn't been cancelled, do nothing */
1862 if (data->cancelled == 0)
1863 goto out_free;
1864 /* In case of error, no cleanup! */
1865 if (data->rpc_status != 0 || !data->rpc_done)
1866 goto out_free;
1867 /* In case we need an open_confirm, no cleanup! */
1868 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1869 goto out_free;
1870 state = nfs4_opendata_to_nfs4_state(data);
1871 if (!IS_ERR(state))
1872 nfs4_close_state(state, data->o_arg.fmode);
1873 out_free:
1874 nfs4_opendata_put(data);
1877 static const struct rpc_call_ops nfs4_open_ops = {
1878 .rpc_call_prepare = nfs4_open_prepare,
1879 .rpc_call_done = nfs4_open_done,
1880 .rpc_release = nfs4_open_release,
1883 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1885 struct inode *dir = data->dir->d_inode;
1886 struct nfs_server *server = NFS_SERVER(dir);
1887 struct nfs_openargs *o_arg = &data->o_arg;
1888 struct nfs_openres *o_res = &data->o_res;
1889 struct rpc_task *task;
1890 struct rpc_message msg = {
1891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1892 .rpc_argp = o_arg,
1893 .rpc_resp = o_res,
1894 .rpc_cred = data->owner->so_cred,
1896 struct rpc_task_setup task_setup_data = {
1897 .rpc_client = server->client,
1898 .rpc_message = &msg,
1899 .callback_ops = &nfs4_open_ops,
1900 .callback_data = data,
1901 .workqueue = nfsiod_workqueue,
1902 .flags = RPC_TASK_ASYNC,
1904 int status;
1906 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1907 kref_get(&data->kref);
1908 data->rpc_done = 0;
1909 data->rpc_status = 0;
1910 data->cancelled = 0;
1911 data->is_recover = 0;
1912 if (isrecover) {
1913 nfs4_set_sequence_privileged(&o_arg->seq_args);
1914 data->is_recover = 1;
1916 task = rpc_run_task(&task_setup_data);
1917 if (IS_ERR(task))
1918 return PTR_ERR(task);
1919 status = nfs4_wait_for_completion_rpc_task(task);
1920 if (status != 0) {
1921 data->cancelled = 1;
1922 smp_wmb();
1923 } else
1924 status = data->rpc_status;
1925 rpc_put_task(task);
1927 return status;
1930 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1932 struct inode *dir = data->dir->d_inode;
1933 struct nfs_openres *o_res = &data->o_res;
1934 int status;
1936 status = nfs4_run_open_task(data, 1);
1937 if (status != 0 || !data->rpc_done)
1938 return status;
1940 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1942 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1943 status = _nfs4_proc_open_confirm(data);
1944 if (status != 0)
1945 return status;
1948 return status;
1951 static int nfs4_opendata_access(struct rpc_cred *cred,
1952 struct nfs4_opendata *opendata,
1953 struct nfs4_state *state, fmode_t fmode,
1954 int openflags)
1956 struct nfs_access_entry cache;
1957 u32 mask;
1959 /* access call failed or for some reason the server doesn't
1960 * support any access modes -- defer access call until later */
1961 if (opendata->o_res.access_supported == 0)
1962 return 0;
1964 mask = 0;
1965 /* don't check MAY_WRITE - a newly created file may not have
1966 * write mode bits, but POSIX allows the creating process to write.
1967 * use openflags to check for exec, because fmode won't
1968 * always have FMODE_EXEC set when file open for exec. */
1969 if (openflags & __FMODE_EXEC) {
1970 /* ONLY check for exec rights */
1971 mask = MAY_EXEC;
1972 } else if (fmode & FMODE_READ)
1973 mask = MAY_READ;
1975 cache.cred = cred;
1976 cache.jiffies = jiffies;
1977 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1978 nfs_access_add_cache(state->inode, &cache);
1980 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1981 return 0;
1983 /* even though OPEN succeeded, access is denied. Close the file */
1984 nfs4_close_state(state, fmode);
1985 return -EACCES;
1989 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1991 static int _nfs4_proc_open(struct nfs4_opendata *data)
1993 struct inode *dir = data->dir->d_inode;
1994 struct nfs_server *server = NFS_SERVER(dir);
1995 struct nfs_openargs *o_arg = &data->o_arg;
1996 struct nfs_openres *o_res = &data->o_res;
1997 int status;
1999 status = nfs4_run_open_task(data, 0);
2000 if (!data->rpc_done)
2001 return status;
2002 if (status != 0) {
2003 if (status == -NFS4ERR_BADNAME &&
2004 !(o_arg->open_flags & O_CREAT))
2005 return -ENOENT;
2006 return status;
2009 nfs_fattr_map_and_free_names(server, &data->f_attr);
2011 if (o_arg->open_flags & O_CREAT) {
2012 update_changeattr(dir, &o_res->cinfo);
2013 if (o_arg->open_flags & O_EXCL)
2014 data->file_created = 1;
2015 else if (o_res->cinfo.before != o_res->cinfo.after)
2016 data->file_created = 1;
2018 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2019 server->caps &= ~NFS_CAP_POSIX_LOCK;
2020 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2021 status = _nfs4_proc_open_confirm(data);
2022 if (status != 0)
2023 return status;
2025 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
2026 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2027 return 0;
2030 static int nfs4_recover_expired_lease(struct nfs_server *server)
2032 return nfs4_client_recover_expired_lease(server->nfs_client);
2036 * OPEN_EXPIRED:
2037 * reclaim state on the server after a network partition.
2038 * Assumes caller holds the appropriate lock
2040 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2042 struct nfs4_opendata *opendata;
2043 int ret;
2045 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2046 NFS4_OPEN_CLAIM_FH);
2047 if (IS_ERR(opendata))
2048 return PTR_ERR(opendata);
2049 ret = nfs4_open_recover(opendata, state);
2050 if (ret == -ESTALE)
2051 d_drop(ctx->dentry);
2052 nfs4_opendata_put(opendata);
2053 return ret;
2056 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2058 struct nfs_server *server = NFS_SERVER(state->inode);
2059 struct nfs4_exception exception = { };
2060 int err;
2062 do {
2063 err = _nfs4_open_expired(ctx, state);
2064 trace_nfs4_open_expired(ctx, 0, err);
2065 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2066 continue;
2067 switch (err) {
2068 default:
2069 goto out;
2070 case -NFS4ERR_GRACE:
2071 case -NFS4ERR_DELAY:
2072 nfs4_handle_exception(server, err, &exception);
2073 err = 0;
2075 } while (exception.retry);
2076 out:
2077 return err;
2080 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2082 struct nfs_open_context *ctx;
2083 int ret;
2085 ctx = nfs4_state_find_open_context(state);
2086 if (IS_ERR(ctx))
2087 return -EAGAIN;
2088 ret = nfs4_do_open_expired(ctx, state);
2089 put_nfs_open_context(ctx);
2090 return ret;
2093 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2095 nfs_remove_bad_delegation(state->inode);
2096 write_seqlock(&state->seqlock);
2097 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2098 write_sequnlock(&state->seqlock);
2099 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2102 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2104 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2105 nfs_finish_clear_delegation_stateid(state);
2108 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2110 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2111 nfs40_clear_delegation_stateid(state);
2112 return nfs4_open_expired(sp, state);
2115 #if defined(CONFIG_NFS_V4_1)
2116 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2118 struct nfs_server *server = NFS_SERVER(state->inode);
2119 nfs4_stateid stateid;
2120 struct nfs_delegation *delegation;
2121 struct rpc_cred *cred;
2122 int status;
2124 /* Get the delegation credential for use by test/free_stateid */
2125 rcu_read_lock();
2126 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2127 if (delegation == NULL) {
2128 rcu_read_unlock();
2129 return;
2132 nfs4_stateid_copy(&stateid, &delegation->stateid);
2133 cred = get_rpccred(delegation->cred);
2134 rcu_read_unlock();
2135 status = nfs41_test_stateid(server, &stateid, cred);
2136 trace_nfs4_test_delegation_stateid(state, NULL, status);
2138 if (status != NFS_OK) {
2139 /* Free the stateid unless the server explicitly
2140 * informs us the stateid is unrecognized. */
2141 if (status != -NFS4ERR_BAD_STATEID)
2142 nfs41_free_stateid(server, &stateid, cred);
2143 nfs_finish_clear_delegation_stateid(state);
2146 put_rpccred(cred);
2150 * nfs41_check_open_stateid - possibly free an open stateid
2152 * @state: NFSv4 state for an inode
2154 * Returns NFS_OK if recovery for this stateid is now finished.
2155 * Otherwise a negative NFS4ERR value is returned.
2157 static int nfs41_check_open_stateid(struct nfs4_state *state)
2159 struct nfs_server *server = NFS_SERVER(state->inode);
2160 nfs4_stateid *stateid = &state->open_stateid;
2161 struct rpc_cred *cred = state->owner->so_cred;
2162 int status;
2164 /* If a state reset has been done, test_stateid is unneeded */
2165 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2166 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2167 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2168 return -NFS4ERR_BAD_STATEID;
2170 status = nfs41_test_stateid(server, stateid, cred);
2171 trace_nfs4_test_open_stateid(state, NULL, status);
2172 if (status != NFS_OK) {
2173 /* Free the stateid unless the server explicitly
2174 * informs us the stateid is unrecognized. */
2175 if (status != -NFS4ERR_BAD_STATEID)
2176 nfs41_free_stateid(server, stateid, cred);
2178 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2179 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2180 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2181 clear_bit(NFS_OPEN_STATE, &state->flags);
2183 return status;
2186 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2188 int status;
2190 nfs41_check_delegation_stateid(state);
2191 status = nfs41_check_open_stateid(state);
2192 if (status != NFS_OK)
2193 status = nfs4_open_expired(sp, state);
2194 return status;
2196 #endif
2199 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2200 * fields corresponding to attributes that were used to store the verifier.
2201 * Make sure we clobber those fields in the later setattr call
2203 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2205 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2206 !(sattr->ia_valid & ATTR_ATIME_SET))
2207 sattr->ia_valid |= ATTR_ATIME;
2209 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2210 !(sattr->ia_valid & ATTR_MTIME_SET))
2211 sattr->ia_valid |= ATTR_MTIME;
2214 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2215 fmode_t fmode,
2216 int flags,
2217 struct nfs_open_context *ctx)
2219 struct nfs4_state_owner *sp = opendata->owner;
2220 struct nfs_server *server = sp->so_server;
2221 struct dentry *dentry;
2222 struct nfs4_state *state;
2223 unsigned int seq;
2224 int ret;
2226 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2228 ret = _nfs4_proc_open(opendata);
2229 if (ret != 0)
2230 goto out;
2232 state = nfs4_opendata_to_nfs4_state(opendata);
2233 ret = PTR_ERR(state);
2234 if (IS_ERR(state))
2235 goto out;
2236 if (server->caps & NFS_CAP_POSIX_LOCK)
2237 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2239 dentry = opendata->dentry;
2240 if (dentry->d_inode == NULL) {
2241 /* FIXME: Is this d_drop() ever needed? */
2242 d_drop(dentry);
2243 dentry = d_add_unique(dentry, igrab(state->inode));
2244 if (dentry == NULL) {
2245 dentry = opendata->dentry;
2246 } else {
2247 dput(ctx->dentry);
2248 ctx->dentry = dentry;
2250 nfs_set_verifier(dentry,
2251 nfs_save_change_attribute(opendata->dir->d_inode));
2254 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2255 if (ret != 0)
2256 goto out;
2258 ctx->state = state;
2259 if (dentry->d_inode == state->inode) {
2260 nfs_inode_attach_open_context(ctx);
2261 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2262 nfs4_schedule_stateid_recovery(server, state);
2264 out:
2265 return ret;
2269 * Returns a referenced nfs4_state
2271 static int _nfs4_do_open(struct inode *dir,
2272 struct nfs_open_context *ctx,
2273 int flags,
2274 struct iattr *sattr,
2275 struct nfs4_label *label,
2276 int *opened)
2278 struct nfs4_state_owner *sp;
2279 struct nfs4_state *state = NULL;
2280 struct nfs_server *server = NFS_SERVER(dir);
2281 struct nfs4_opendata *opendata;
2282 struct dentry *dentry = ctx->dentry;
2283 struct rpc_cred *cred = ctx->cred;
2284 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2285 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2286 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2287 struct nfs4_label *olabel = NULL;
2288 int status;
2290 /* Protect against reboot recovery conflicts */
2291 status = -ENOMEM;
2292 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2293 if (sp == NULL) {
2294 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2295 goto out_err;
2297 status = nfs4_recover_expired_lease(server);
2298 if (status != 0)
2299 goto err_put_state_owner;
2300 if (dentry->d_inode != NULL)
2301 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
2302 status = -ENOMEM;
2303 if (dentry->d_inode)
2304 claim = NFS4_OPEN_CLAIM_FH;
2305 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2306 label, claim, GFP_KERNEL);
2307 if (opendata == NULL)
2308 goto err_put_state_owner;
2310 if (label) {
2311 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2312 if (IS_ERR(olabel)) {
2313 status = PTR_ERR(olabel);
2314 goto err_opendata_put;
2318 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2319 if (!opendata->f_attr.mdsthreshold) {
2320 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2321 if (!opendata->f_attr.mdsthreshold)
2322 goto err_free_label;
2324 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2326 if (dentry->d_inode != NULL)
2327 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2329 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2330 if (status != 0)
2331 goto err_free_label;
2332 state = ctx->state;
2334 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2335 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2336 nfs4_exclusive_attrset(opendata, sattr);
2338 nfs_fattr_init(opendata->o_res.f_attr);
2339 status = nfs4_do_setattr(state->inode, cred,
2340 opendata->o_res.f_attr, sattr,
2341 state, label, olabel);
2342 if (status == 0) {
2343 nfs_setattr_update_inode(state->inode, sattr);
2344 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2345 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2348 if (opendata->file_created)
2349 *opened |= FILE_CREATED;
2351 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2352 *ctx_th = opendata->f_attr.mdsthreshold;
2353 opendata->f_attr.mdsthreshold = NULL;
2356 nfs4_label_free(olabel);
2358 nfs4_opendata_put(opendata);
2359 nfs4_put_state_owner(sp);
2360 return 0;
2361 err_free_label:
2362 nfs4_label_free(olabel);
2363 err_opendata_put:
2364 nfs4_opendata_put(opendata);
2365 err_put_state_owner:
2366 nfs4_put_state_owner(sp);
2367 out_err:
2368 return status;
2372 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2373 struct nfs_open_context *ctx,
2374 int flags,
2375 struct iattr *sattr,
2376 struct nfs4_label *label,
2377 int *opened)
2379 struct nfs_server *server = NFS_SERVER(dir);
2380 struct nfs4_exception exception = { };
2381 struct nfs4_state *res;
2382 int status;
2384 do {
2385 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2386 res = ctx->state;
2387 trace_nfs4_open_file(ctx, flags, status);
2388 if (status == 0)
2389 break;
2390 /* NOTE: BAD_SEQID means the server and client disagree about the
2391 * book-keeping w.r.t. state-changing operations
2392 * (OPEN/CLOSE/LOCK/LOCKU...)
2393 * It is actually a sign of a bug on the client or on the server.
2395 * If we receive a BAD_SEQID error in the particular case of
2396 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2397 * have unhashed the old state_owner for us, and that we can
2398 * therefore safely retry using a new one. We should still warn
2399 * the user though...
2401 if (status == -NFS4ERR_BAD_SEQID) {
2402 pr_warn_ratelimited("NFS: v4 server %s "
2403 " returned a bad sequence-id error!\n",
2404 NFS_SERVER(dir)->nfs_client->cl_hostname);
2405 exception.retry = 1;
2406 continue;
2409 * BAD_STATEID on OPEN means that the server cancelled our
2410 * state before it received the OPEN_CONFIRM.
2411 * Recover by retrying the request as per the discussion
2412 * on Page 181 of RFC3530.
2414 if (status == -NFS4ERR_BAD_STATEID) {
2415 exception.retry = 1;
2416 continue;
2418 if (status == -EAGAIN) {
2419 /* We must have found a delegation */
2420 exception.retry = 1;
2421 continue;
2423 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2424 continue;
2425 res = ERR_PTR(nfs4_handle_exception(server,
2426 status, &exception));
2427 } while (exception.retry);
2428 return res;
2431 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2432 struct nfs_fattr *fattr, struct iattr *sattr,
2433 struct nfs4_state *state, struct nfs4_label *ilabel,
2434 struct nfs4_label *olabel)
2436 struct nfs_server *server = NFS_SERVER(inode);
2437 struct nfs_setattrargs arg = {
2438 .fh = NFS_FH(inode),
2439 .iap = sattr,
2440 .server = server,
2441 .bitmask = server->attr_bitmask,
2442 .label = ilabel,
2444 struct nfs_setattrres res = {
2445 .fattr = fattr,
2446 .label = olabel,
2447 .server = server,
2449 struct rpc_message msg = {
2450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2451 .rpc_argp = &arg,
2452 .rpc_resp = &res,
2453 .rpc_cred = cred,
2455 unsigned long timestamp = jiffies;
2456 fmode_t fmode;
2457 bool truncate;
2458 int status;
2460 arg.bitmask = nfs4_bitmask(server, ilabel);
2461 if (ilabel)
2462 arg.bitmask = nfs4_bitmask(server, olabel);
2464 nfs_fattr_init(fattr);
2466 /* Servers should only apply open mode checks for file size changes */
2467 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2468 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2470 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2471 /* Use that stateid */
2472 } else if (truncate && state != NULL) {
2473 struct nfs_lockowner lockowner = {
2474 .l_owner = current->files,
2475 .l_pid = current->tgid,
2477 if (!nfs4_valid_open_stateid(state))
2478 return -EBADF;
2479 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2480 &lockowner) == -EIO)
2481 return -EBADF;
2482 } else
2483 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2485 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2486 if (status == 0 && state != NULL)
2487 renew_lease(server, timestamp);
2488 return status;
2491 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2492 struct nfs_fattr *fattr, struct iattr *sattr,
2493 struct nfs4_state *state, struct nfs4_label *ilabel,
2494 struct nfs4_label *olabel)
2496 struct nfs_server *server = NFS_SERVER(inode);
2497 struct nfs4_exception exception = {
2498 .state = state,
2499 .inode = inode,
2501 int err;
2502 do {
2503 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
2504 trace_nfs4_setattr(inode, err);
2505 switch (err) {
2506 case -NFS4ERR_OPENMODE:
2507 if (!(sattr->ia_valid & ATTR_SIZE)) {
2508 pr_warn_once("NFSv4: server %s is incorrectly "
2509 "applying open mode checks to "
2510 "a SETATTR that is not "
2511 "changing file size.\n",
2512 server->nfs_client->cl_hostname);
2514 if (state && !(state->state & FMODE_WRITE)) {
2515 err = -EBADF;
2516 if (sattr->ia_valid & ATTR_OPEN)
2517 err = -EACCES;
2518 goto out;
2521 err = nfs4_handle_exception(server, err, &exception);
2522 } while (exception.retry);
2523 out:
2524 return err;
2527 struct nfs4_closedata {
2528 struct inode *inode;
2529 struct nfs4_state *state;
2530 struct nfs_closeargs arg;
2531 struct nfs_closeres res;
2532 struct nfs_fattr fattr;
2533 unsigned long timestamp;
2534 bool roc;
2535 u32 roc_barrier;
2538 static void nfs4_free_closedata(void *data)
2540 struct nfs4_closedata *calldata = data;
2541 struct nfs4_state_owner *sp = calldata->state->owner;
2542 struct super_block *sb = calldata->state->inode->i_sb;
2544 if (calldata->roc)
2545 pnfs_roc_release(calldata->state->inode);
2546 nfs4_put_open_state(calldata->state);
2547 nfs_free_seqid(calldata->arg.seqid);
2548 nfs4_put_state_owner(sp);
2549 nfs_sb_deactive(sb);
2550 kfree(calldata);
2553 static void nfs4_close_done(struct rpc_task *task, void *data)
2555 struct nfs4_closedata *calldata = data;
2556 struct nfs4_state *state = calldata->state;
2557 struct nfs_server *server = NFS_SERVER(calldata->inode);
2558 nfs4_stateid *res_stateid = NULL;
2560 dprintk("%s: begin!\n", __func__);
2561 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2562 return;
2563 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
2564 /* hmm. we are done with the inode, and in the process of freeing
2565 * the state_owner. we keep this around to process errors
2567 switch (task->tk_status) {
2568 case 0:
2569 res_stateid = &calldata->res.stateid;
2570 if (calldata->arg.fmode == 0 && calldata->roc)
2571 pnfs_roc_set_barrier(state->inode,
2572 calldata->roc_barrier);
2573 renew_lease(server, calldata->timestamp);
2574 break;
2575 case -NFS4ERR_ADMIN_REVOKED:
2576 case -NFS4ERR_STALE_STATEID:
2577 case -NFS4ERR_OLD_STATEID:
2578 case -NFS4ERR_BAD_STATEID:
2579 case -NFS4ERR_EXPIRED:
2580 if (calldata->arg.fmode == 0)
2581 break;
2582 default:
2583 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
2584 rpc_restart_call_prepare(task);
2585 goto out_release;
2588 nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode);
2589 out_release:
2590 nfs_release_seqid(calldata->arg.seqid);
2591 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2592 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2595 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2597 struct nfs4_closedata *calldata = data;
2598 struct nfs4_state *state = calldata->state;
2599 struct inode *inode = calldata->inode;
2600 bool is_rdonly, is_wronly, is_rdwr;
2601 int call_close = 0;
2603 dprintk("%s: begin!\n", __func__);
2604 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2605 goto out_wait;
2607 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2608 spin_lock(&state->owner->so_lock);
2609 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2610 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2611 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
2612 /* Calculate the change in open mode */
2613 calldata->arg.fmode = 0;
2614 if (state->n_rdwr == 0) {
2615 if (state->n_rdonly == 0)
2616 call_close |= is_rdonly;
2617 else if (is_rdonly)
2618 calldata->arg.fmode |= FMODE_READ;
2619 if (state->n_wronly == 0)
2620 call_close |= is_wronly;
2621 else if (is_wronly)
2622 calldata->arg.fmode |= FMODE_WRITE;
2623 } else if (is_rdwr)
2624 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2626 if (calldata->arg.fmode == 0)
2627 call_close |= is_rdwr;
2629 if (!nfs4_valid_open_stateid(state))
2630 call_close = 0;
2631 spin_unlock(&state->owner->so_lock);
2633 if (!call_close) {
2634 /* Note: exit _without_ calling nfs4_close_done */
2635 goto out_no_action;
2638 if (calldata->arg.fmode == 0) {
2639 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2640 if (calldata->roc &&
2641 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2642 nfs_release_seqid(calldata->arg.seqid);
2643 goto out_wait;
2647 nfs_fattr_init(calldata->res.fattr);
2648 calldata->timestamp = jiffies;
2649 if (nfs4_setup_sequence(NFS_SERVER(inode),
2650 &calldata->arg.seq_args,
2651 &calldata->res.seq_res,
2652 task) != 0)
2653 nfs_release_seqid(calldata->arg.seqid);
2654 dprintk("%s: done!\n", __func__);
2655 return;
2656 out_no_action:
2657 task->tk_action = NULL;
2658 out_wait:
2659 nfs4_sequence_done(task, &calldata->res.seq_res);
2662 static const struct rpc_call_ops nfs4_close_ops = {
2663 .rpc_call_prepare = nfs4_close_prepare,
2664 .rpc_call_done = nfs4_close_done,
2665 .rpc_release = nfs4_free_closedata,
2669 * It is possible for data to be read/written from a mem-mapped file
2670 * after the sys_close call (which hits the vfs layer as a flush).
2671 * This means that we can't safely call nfsv4 close on a file until
2672 * the inode is cleared. This in turn means that we are not good
2673 * NFSv4 citizens - we do not indicate to the server to update the file's
2674 * share state even when we are done with one of the three share
2675 * stateid's in the inode.
2677 * NOTE: Caller must be holding the sp->so_owner semaphore!
2679 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2681 struct nfs_server *server = NFS_SERVER(state->inode);
2682 struct nfs4_closedata *calldata;
2683 struct nfs4_state_owner *sp = state->owner;
2684 struct rpc_task *task;
2685 struct rpc_message msg = {
2686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2687 .rpc_cred = state->owner->so_cred,
2689 struct rpc_task_setup task_setup_data = {
2690 .rpc_client = server->client,
2691 .rpc_message = &msg,
2692 .callback_ops = &nfs4_close_ops,
2693 .workqueue = nfsiod_workqueue,
2694 .flags = RPC_TASK_ASYNC,
2696 int status = -ENOMEM;
2698 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2699 &task_setup_data.rpc_client, &msg);
2701 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2702 if (calldata == NULL)
2703 goto out;
2704 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2705 calldata->inode = state->inode;
2706 calldata->state = state;
2707 calldata->arg.fh = NFS_FH(state->inode);
2708 calldata->arg.stateid = &state->open_stateid;
2709 /* Serialization for the sequence id */
2710 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2711 if (calldata->arg.seqid == NULL)
2712 goto out_free_calldata;
2713 calldata->arg.fmode = 0;
2714 calldata->arg.bitmask = server->cache_consistency_bitmask;
2715 calldata->res.fattr = &calldata->fattr;
2716 calldata->res.seqid = calldata->arg.seqid;
2717 calldata->res.server = server;
2718 calldata->roc = pnfs_roc(state->inode);
2719 nfs_sb_active(calldata->inode->i_sb);
2721 msg.rpc_argp = &calldata->arg;
2722 msg.rpc_resp = &calldata->res;
2723 task_setup_data.callback_data = calldata;
2724 task = rpc_run_task(&task_setup_data);
2725 if (IS_ERR(task))
2726 return PTR_ERR(task);
2727 status = 0;
2728 if (wait)
2729 status = rpc_wait_for_completion_task(task);
2730 rpc_put_task(task);
2731 return status;
2732 out_free_calldata:
2733 kfree(calldata);
2734 out:
2735 nfs4_put_open_state(state);
2736 nfs4_put_state_owner(sp);
2737 return status;
2740 static struct inode *
2741 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2742 int open_flags, struct iattr *attr, int *opened)
2744 struct nfs4_state *state;
2745 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2747 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
2749 /* Protect against concurrent sillydeletes */
2750 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
2752 nfs4_label_release_security(label);
2754 if (IS_ERR(state))
2755 return ERR_CAST(state);
2756 return state->inode;
2759 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2761 if (ctx->state == NULL)
2762 return;
2763 if (is_sync)
2764 nfs4_close_sync(ctx->state, ctx->mode);
2765 else
2766 nfs4_close_state(ctx->state, ctx->mode);
2769 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2770 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2771 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
2773 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2775 struct nfs4_server_caps_arg args = {
2776 .fhandle = fhandle,
2778 struct nfs4_server_caps_res res = {};
2779 struct rpc_message msg = {
2780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2781 .rpc_argp = &args,
2782 .rpc_resp = &res,
2784 int status;
2786 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2787 if (status == 0) {
2788 /* Sanity check the server answers */
2789 switch (server->nfs_client->cl_minorversion) {
2790 case 0:
2791 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2792 res.attr_bitmask[2] = 0;
2793 break;
2794 case 1:
2795 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2796 break;
2797 case 2:
2798 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2800 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2801 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2802 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2803 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2804 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2805 NFS_CAP_CTIME|NFS_CAP_MTIME|
2806 NFS_CAP_SECURITY_LABEL);
2807 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2808 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
2809 server->caps |= NFS_CAP_ACLS;
2810 if (res.has_links != 0)
2811 server->caps |= NFS_CAP_HARDLINKS;
2812 if (res.has_symlinks != 0)
2813 server->caps |= NFS_CAP_SYMLINKS;
2814 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2815 server->caps |= NFS_CAP_FILEID;
2816 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2817 server->caps |= NFS_CAP_MODE;
2818 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2819 server->caps |= NFS_CAP_NLINK;
2820 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2821 server->caps |= NFS_CAP_OWNER;
2822 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2823 server->caps |= NFS_CAP_OWNER_GROUP;
2824 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2825 server->caps |= NFS_CAP_ATIME;
2826 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2827 server->caps |= NFS_CAP_CTIME;
2828 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2829 server->caps |= NFS_CAP_MTIME;
2830 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
2831 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2832 server->caps |= NFS_CAP_SECURITY_LABEL;
2833 #endif
2834 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2835 sizeof(server->attr_bitmask));
2836 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2838 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2839 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2840 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2841 server->cache_consistency_bitmask[2] = 0;
2842 server->acl_bitmask = res.acl_bitmask;
2843 server->fh_expire_type = res.fh_expire_type;
2846 return status;
2849 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2851 struct nfs4_exception exception = { };
2852 int err;
2853 do {
2854 err = nfs4_handle_exception(server,
2855 _nfs4_server_capabilities(server, fhandle),
2856 &exception);
2857 } while (exception.retry);
2858 return err;
2861 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2862 struct nfs_fsinfo *info)
2864 u32 bitmask[3];
2865 struct nfs4_lookup_root_arg args = {
2866 .bitmask = bitmask,
2868 struct nfs4_lookup_res res = {
2869 .server = server,
2870 .fattr = info->fattr,
2871 .fh = fhandle,
2873 struct rpc_message msg = {
2874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2875 .rpc_argp = &args,
2876 .rpc_resp = &res,
2879 bitmask[0] = nfs4_fattr_bitmap[0];
2880 bitmask[1] = nfs4_fattr_bitmap[1];
2882 * Process the label in the upcoming getfattr
2884 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2886 nfs_fattr_init(info->fattr);
2887 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2890 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2891 struct nfs_fsinfo *info)
2893 struct nfs4_exception exception = { };
2894 int err;
2895 do {
2896 err = _nfs4_lookup_root(server, fhandle, info);
2897 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
2898 switch (err) {
2899 case 0:
2900 case -NFS4ERR_WRONGSEC:
2901 goto out;
2902 default:
2903 err = nfs4_handle_exception(server, err, &exception);
2905 } while (exception.retry);
2906 out:
2907 return err;
2910 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2911 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2913 struct rpc_auth_create_args auth_args = {
2914 .pseudoflavor = flavor,
2916 struct rpc_auth *auth;
2917 int ret;
2919 auth = rpcauth_create(&auth_args, server->client);
2920 if (IS_ERR(auth)) {
2921 ret = -EACCES;
2922 goto out;
2924 ret = nfs4_lookup_root(server, fhandle, info);
2925 out:
2926 return ret;
2930 * Retry pseudoroot lookup with various security flavors. We do this when:
2932 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2933 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2935 * Returns zero on success, or a negative NFS4ERR value, or a
2936 * negative errno value.
2938 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2939 struct nfs_fsinfo *info)
2941 /* Per 3530bis 15.33.5 */
2942 static const rpc_authflavor_t flav_array[] = {
2943 RPC_AUTH_GSS_KRB5P,
2944 RPC_AUTH_GSS_KRB5I,
2945 RPC_AUTH_GSS_KRB5,
2946 RPC_AUTH_UNIX, /* courtesy */
2947 RPC_AUTH_NULL,
2949 int status = -EPERM;
2950 size_t i;
2952 if (server->auth_info.flavor_len > 0) {
2953 /* try each flavor specified by user */
2954 for (i = 0; i < server->auth_info.flavor_len; i++) {
2955 status = nfs4_lookup_root_sec(server, fhandle, info,
2956 server->auth_info.flavors[i]);
2957 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2958 continue;
2959 break;
2961 } else {
2962 /* no flavors specified by user, try default list */
2963 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
2964 status = nfs4_lookup_root_sec(server, fhandle, info,
2965 flav_array[i]);
2966 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2967 continue;
2968 break;
2973 * -EACCESS could mean that the user doesn't have correct permissions
2974 * to access the mount. It could also mean that we tried to mount
2975 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2976 * existing mount programs don't handle -EACCES very well so it should
2977 * be mapped to -EPERM instead.
2979 if (status == -EACCES)
2980 status = -EPERM;
2981 return status;
2984 static int nfs4_do_find_root_sec(struct nfs_server *server,
2985 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2987 int mv = server->nfs_client->cl_minorversion;
2988 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2992 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2993 * @server: initialized nfs_server handle
2994 * @fhandle: we fill in the pseudo-fs root file handle
2995 * @info: we fill in an FSINFO struct
2996 * @auth_probe: probe the auth flavours
2998 * Returns zero on success, or a negative errno.
3000 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3001 struct nfs_fsinfo *info,
3002 bool auth_probe)
3004 int status;
3006 switch (auth_probe) {
3007 case false:
3008 status = nfs4_lookup_root(server, fhandle, info);
3009 if (status != -NFS4ERR_WRONGSEC)
3010 break;
3011 default:
3012 status = nfs4_do_find_root_sec(server, fhandle, info);
3015 if (status == 0)
3016 status = nfs4_server_capabilities(server, fhandle);
3017 if (status == 0)
3018 status = nfs4_do_fsinfo(server, fhandle, info);
3020 return nfs4_map_errors(status);
3023 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3024 struct nfs_fsinfo *info)
3026 int error;
3027 struct nfs_fattr *fattr = info->fattr;
3028 struct nfs4_label *label = NULL;
3030 error = nfs4_server_capabilities(server, mntfh);
3031 if (error < 0) {
3032 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3033 return error;
3036 label = nfs4_label_alloc(server, GFP_KERNEL);
3037 if (IS_ERR(label))
3038 return PTR_ERR(label);
3040 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3041 if (error < 0) {
3042 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3043 goto err_free_label;
3046 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3047 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3048 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3050 err_free_label:
3051 nfs4_label_free(label);
3053 return error;
3057 * Get locations and (maybe) other attributes of a referral.
3058 * Note that we'll actually follow the referral later when
3059 * we detect fsid mismatch in inode revalidation
3061 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3062 const struct qstr *name, struct nfs_fattr *fattr,
3063 struct nfs_fh *fhandle)
3065 int status = -ENOMEM;
3066 struct page *page = NULL;
3067 struct nfs4_fs_locations *locations = NULL;
3069 page = alloc_page(GFP_KERNEL);
3070 if (page == NULL)
3071 goto out;
3072 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3073 if (locations == NULL)
3074 goto out;
3076 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3077 if (status != 0)
3078 goto out;
3081 * If the fsid didn't change, this is a migration event, not a
3082 * referral. Cause us to drop into the exception handler, which
3083 * will kick off migration recovery.
3085 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3086 dprintk("%s: server did not return a different fsid for"
3087 " a referral at %s\n", __func__, name->name);
3088 status = -NFS4ERR_MOVED;
3089 goto out;
3091 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3092 nfs_fixup_referral_attributes(&locations->fattr);
3094 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3095 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3096 memset(fhandle, 0, sizeof(struct nfs_fh));
3097 out:
3098 if (page)
3099 __free_page(page);
3100 kfree(locations);
3101 return status;
3104 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3105 struct nfs_fattr *fattr, struct nfs4_label *label)
3107 struct nfs4_getattr_arg args = {
3108 .fh = fhandle,
3109 .bitmask = server->attr_bitmask,
3111 struct nfs4_getattr_res res = {
3112 .fattr = fattr,
3113 .label = label,
3114 .server = server,
3116 struct rpc_message msg = {
3117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3118 .rpc_argp = &args,
3119 .rpc_resp = &res,
3122 args.bitmask = nfs4_bitmask(server, label);
3124 nfs_fattr_init(fattr);
3125 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3128 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3129 struct nfs_fattr *fattr, struct nfs4_label *label)
3131 struct nfs4_exception exception = { };
3132 int err;
3133 do {
3134 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3135 trace_nfs4_getattr(server, fhandle, fattr, err);
3136 err = nfs4_handle_exception(server, err,
3137 &exception);
3138 } while (exception.retry);
3139 return err;
3143 * The file is not closed if it is opened due to the a request to change
3144 * the size of the file. The open call will not be needed once the
3145 * VFS layer lookup-intents are implemented.
3147 * Close is called when the inode is destroyed.
3148 * If we haven't opened the file for O_WRONLY, we
3149 * need to in the size_change case to obtain a stateid.
3151 * Got race?
3152 * Because OPEN is always done by name in nfsv4, it is
3153 * possible that we opened a different file by the same
3154 * name. We can recognize this race condition, but we
3155 * can't do anything about it besides returning an error.
3157 * This will be fixed with VFS changes (lookup-intent).
3159 static int
3160 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3161 struct iattr *sattr)
3163 struct inode *inode = dentry->d_inode;
3164 struct rpc_cred *cred = NULL;
3165 struct nfs4_state *state = NULL;
3166 struct nfs4_label *label = NULL;
3167 int status;
3169 if (pnfs_ld_layoutret_on_setattr(inode))
3170 pnfs_commit_and_return_layout(inode);
3172 nfs_fattr_init(fattr);
3174 /* Deal with open(O_TRUNC) */
3175 if (sattr->ia_valid & ATTR_OPEN)
3176 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3178 /* Optimization: if the end result is no change, don't RPC */
3179 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3180 return 0;
3182 /* Search for an existing open(O_WRITE) file */
3183 if (sattr->ia_valid & ATTR_FILE) {
3184 struct nfs_open_context *ctx;
3186 ctx = nfs_file_open_context(sattr->ia_file);
3187 if (ctx) {
3188 cred = ctx->cred;
3189 state = ctx->state;
3193 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3194 if (IS_ERR(label))
3195 return PTR_ERR(label);
3197 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3198 if (status == 0) {
3199 nfs_setattr_update_inode(inode, sattr);
3200 nfs_setsecurity(inode, fattr, label);
3202 nfs4_label_free(label);
3203 return status;
3206 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3207 const struct qstr *name, struct nfs_fh *fhandle,
3208 struct nfs_fattr *fattr, struct nfs4_label *label)
3210 struct nfs_server *server = NFS_SERVER(dir);
3211 int status;
3212 struct nfs4_lookup_arg args = {
3213 .bitmask = server->attr_bitmask,
3214 .dir_fh = NFS_FH(dir),
3215 .name = name,
3217 struct nfs4_lookup_res res = {
3218 .server = server,
3219 .fattr = fattr,
3220 .label = label,
3221 .fh = fhandle,
3223 struct rpc_message msg = {
3224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3225 .rpc_argp = &args,
3226 .rpc_resp = &res,
3229 args.bitmask = nfs4_bitmask(server, label);
3231 nfs_fattr_init(fattr);
3233 dprintk("NFS call lookup %s\n", name->name);
3234 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3235 dprintk("NFS reply lookup: %d\n", status);
3236 return status;
3239 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3241 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3242 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3243 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3244 fattr->nlink = 2;
3247 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3248 struct qstr *name, struct nfs_fh *fhandle,
3249 struct nfs_fattr *fattr, struct nfs4_label *label)
3251 struct nfs4_exception exception = { };
3252 struct rpc_clnt *client = *clnt;
3253 int err;
3254 do {
3255 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3256 trace_nfs4_lookup(dir, name, err);
3257 switch (err) {
3258 case -NFS4ERR_BADNAME:
3259 err = -ENOENT;
3260 goto out;
3261 case -NFS4ERR_MOVED:
3262 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3263 goto out;
3264 case -NFS4ERR_WRONGSEC:
3265 err = -EPERM;
3266 if (client != *clnt)
3267 goto out;
3268 client = nfs4_negotiate_security(client, dir, name);
3269 if (IS_ERR(client))
3270 return PTR_ERR(client);
3272 exception.retry = 1;
3273 break;
3274 default:
3275 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3277 } while (exception.retry);
3279 out:
3280 if (err == 0)
3281 *clnt = client;
3282 else if (client != *clnt)
3283 rpc_shutdown_client(client);
3285 return err;
3288 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
3289 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3290 struct nfs4_label *label)
3292 int status;
3293 struct rpc_clnt *client = NFS_CLIENT(dir);
3295 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3296 if (client != NFS_CLIENT(dir)) {
3297 rpc_shutdown_client(client);
3298 nfs_fixup_secinfo_attributes(fattr);
3300 return status;
3303 struct rpc_clnt *
3304 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3305 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3307 struct rpc_clnt *client = NFS_CLIENT(dir);
3308 int status;
3310 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3311 if (status < 0)
3312 return ERR_PTR(status);
3313 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3316 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3318 struct nfs_server *server = NFS_SERVER(inode);
3319 struct nfs4_accessargs args = {
3320 .fh = NFS_FH(inode),
3321 .bitmask = server->cache_consistency_bitmask,
3323 struct nfs4_accessres res = {
3324 .server = server,
3326 struct rpc_message msg = {
3327 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3328 .rpc_argp = &args,
3329 .rpc_resp = &res,
3330 .rpc_cred = entry->cred,
3332 int mode = entry->mask;
3333 int status = 0;
3336 * Determine which access bits we want to ask for...
3338 if (mode & MAY_READ)
3339 args.access |= NFS4_ACCESS_READ;
3340 if (S_ISDIR(inode->i_mode)) {
3341 if (mode & MAY_WRITE)
3342 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3343 if (mode & MAY_EXEC)
3344 args.access |= NFS4_ACCESS_LOOKUP;
3345 } else {
3346 if (mode & MAY_WRITE)
3347 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3348 if (mode & MAY_EXEC)
3349 args.access |= NFS4_ACCESS_EXECUTE;
3352 res.fattr = nfs_alloc_fattr();
3353 if (res.fattr == NULL)
3354 return -ENOMEM;
3356 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3357 if (!status) {
3358 nfs_access_set_mask(entry, res.access);
3359 nfs_refresh_inode(inode, res.fattr);
3361 nfs_free_fattr(res.fattr);
3362 return status;
3365 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3367 struct nfs4_exception exception = { };
3368 int err;
3369 do {
3370 err = _nfs4_proc_access(inode, entry);
3371 trace_nfs4_access(inode, err);
3372 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3373 &exception);
3374 } while (exception.retry);
3375 return err;
3379 * TODO: For the time being, we don't try to get any attributes
3380 * along with any of the zero-copy operations READ, READDIR,
3381 * READLINK, WRITE.
3383 * In the case of the first three, we want to put the GETATTR
3384 * after the read-type operation -- this is because it is hard
3385 * to predict the length of a GETATTR response in v4, and thus
3386 * align the READ data correctly. This means that the GETATTR
3387 * may end up partially falling into the page cache, and we should
3388 * shift it into the 'tail' of the xdr_buf before processing.
3389 * To do this efficiently, we need to know the total length
3390 * of data received, which doesn't seem to be available outside
3391 * of the RPC layer.
3393 * In the case of WRITE, we also want to put the GETATTR after
3394 * the operation -- in this case because we want to make sure
3395 * we get the post-operation mtime and size.
3397 * Both of these changes to the XDR layer would in fact be quite
3398 * minor, but I decided to leave them for a subsequent patch.
3400 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3401 unsigned int pgbase, unsigned int pglen)
3403 struct nfs4_readlink args = {
3404 .fh = NFS_FH(inode),
3405 .pgbase = pgbase,
3406 .pglen = pglen,
3407 .pages = &page,
3409 struct nfs4_readlink_res res;
3410 struct rpc_message msg = {
3411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3412 .rpc_argp = &args,
3413 .rpc_resp = &res,
3416 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3419 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3420 unsigned int pgbase, unsigned int pglen)
3422 struct nfs4_exception exception = { };
3423 int err;
3424 do {
3425 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3426 trace_nfs4_readlink(inode, err);
3427 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3428 &exception);
3429 } while (exception.retry);
3430 return err;
3434 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3436 static int
3437 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3438 int flags)
3440 struct nfs4_label l, *ilabel = NULL;
3441 struct nfs_open_context *ctx;
3442 struct nfs4_state *state;
3443 int opened = 0;
3444 int status = 0;
3446 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3447 if (IS_ERR(ctx))
3448 return PTR_ERR(ctx);
3450 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3452 sattr->ia_mode &= ~current_umask();
3453 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
3454 if (IS_ERR(state)) {
3455 status = PTR_ERR(state);
3456 goto out;
3458 out:
3459 nfs4_label_release_security(ilabel);
3460 put_nfs_open_context(ctx);
3461 return status;
3464 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3466 struct nfs_server *server = NFS_SERVER(dir);
3467 struct nfs_removeargs args = {
3468 .fh = NFS_FH(dir),
3469 .name = *name,
3471 struct nfs_removeres res = {
3472 .server = server,
3474 struct rpc_message msg = {
3475 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3476 .rpc_argp = &args,
3477 .rpc_resp = &res,
3479 int status;
3481 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3482 if (status == 0)
3483 update_changeattr(dir, &res.cinfo);
3484 return status;
3487 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3489 struct nfs4_exception exception = { };
3490 int err;
3491 do {
3492 err = _nfs4_proc_remove(dir, name);
3493 trace_nfs4_remove(dir, name, err);
3494 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3495 &exception);
3496 } while (exception.retry);
3497 return err;
3500 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3502 struct nfs_server *server = NFS_SERVER(dir);
3503 struct nfs_removeargs *args = msg->rpc_argp;
3504 struct nfs_removeres *res = msg->rpc_resp;
3506 res->server = server;
3507 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3508 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
3510 nfs_fattr_init(res->dir_attr);
3513 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3515 nfs4_setup_sequence(NFS_SERVER(data->dir),
3516 &data->args.seq_args,
3517 &data->res.seq_res,
3518 task);
3521 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3523 struct nfs_unlinkdata *data = task->tk_calldata;
3524 struct nfs_removeres *res = &data->res;
3526 if (!nfs4_sequence_done(task, &res->seq_res))
3527 return 0;
3528 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3529 return 0;
3530 update_changeattr(dir, &res->cinfo);
3531 return 1;
3534 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3536 struct nfs_server *server = NFS_SERVER(dir);
3537 struct nfs_renameargs *arg = msg->rpc_argp;
3538 struct nfs_renameres *res = msg->rpc_resp;
3540 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3541 res->server = server;
3542 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
3545 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3547 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3548 &data->args.seq_args,
3549 &data->res.seq_res,
3550 task);
3553 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3554 struct inode *new_dir)
3556 struct nfs_renamedata *data = task->tk_calldata;
3557 struct nfs_renameres *res = &data->res;
3559 if (!nfs4_sequence_done(task, &res->seq_res))
3560 return 0;
3561 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3562 return 0;
3564 update_changeattr(old_dir, &res->old_cinfo);
3565 update_changeattr(new_dir, &res->new_cinfo);
3566 return 1;
3569 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3571 struct nfs_server *server = NFS_SERVER(inode);
3572 struct nfs4_link_arg arg = {
3573 .fh = NFS_FH(inode),
3574 .dir_fh = NFS_FH(dir),
3575 .name = name,
3576 .bitmask = server->attr_bitmask,
3578 struct nfs4_link_res res = {
3579 .server = server,
3580 .label = NULL,
3582 struct rpc_message msg = {
3583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3584 .rpc_argp = &arg,
3585 .rpc_resp = &res,
3587 int status = -ENOMEM;
3589 res.fattr = nfs_alloc_fattr();
3590 if (res.fattr == NULL)
3591 goto out;
3593 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3594 if (IS_ERR(res.label)) {
3595 status = PTR_ERR(res.label);
3596 goto out;
3598 arg.bitmask = nfs4_bitmask(server, res.label);
3600 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3601 if (!status) {
3602 update_changeattr(dir, &res.cinfo);
3603 status = nfs_post_op_update_inode(inode, res.fattr);
3604 if (!status)
3605 nfs_setsecurity(inode, res.fattr, res.label);
3609 nfs4_label_free(res.label);
3611 out:
3612 nfs_free_fattr(res.fattr);
3613 return status;
3616 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3618 struct nfs4_exception exception = { };
3619 int err;
3620 do {
3621 err = nfs4_handle_exception(NFS_SERVER(inode),
3622 _nfs4_proc_link(inode, dir, name),
3623 &exception);
3624 } while (exception.retry);
3625 return err;
3628 struct nfs4_createdata {
3629 struct rpc_message msg;
3630 struct nfs4_create_arg arg;
3631 struct nfs4_create_res res;
3632 struct nfs_fh fh;
3633 struct nfs_fattr fattr;
3634 struct nfs4_label *label;
3637 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3638 struct qstr *name, struct iattr *sattr, u32 ftype)
3640 struct nfs4_createdata *data;
3642 data = kzalloc(sizeof(*data), GFP_KERNEL);
3643 if (data != NULL) {
3644 struct nfs_server *server = NFS_SERVER(dir);
3646 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3647 if (IS_ERR(data->label))
3648 goto out_free;
3650 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3651 data->msg.rpc_argp = &data->arg;
3652 data->msg.rpc_resp = &data->res;
3653 data->arg.dir_fh = NFS_FH(dir);
3654 data->arg.server = server;
3655 data->arg.name = name;
3656 data->arg.attrs = sattr;
3657 data->arg.ftype = ftype;
3658 data->arg.bitmask = nfs4_bitmask(server, data->label);
3659 data->res.server = server;
3660 data->res.fh = &data->fh;
3661 data->res.fattr = &data->fattr;
3662 data->res.label = data->label;
3663 nfs_fattr_init(data->res.fattr);
3665 return data;
3666 out_free:
3667 kfree(data);
3668 return NULL;
3671 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3673 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3674 &data->arg.seq_args, &data->res.seq_res, 1);
3675 if (status == 0) {
3676 update_changeattr(dir, &data->res.dir_cinfo);
3677 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
3679 return status;
3682 static void nfs4_free_createdata(struct nfs4_createdata *data)
3684 nfs4_label_free(data->label);
3685 kfree(data);
3688 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3689 struct page *page, unsigned int len, struct iattr *sattr,
3690 struct nfs4_label *label)
3692 struct nfs4_createdata *data;
3693 int status = -ENAMETOOLONG;
3695 if (len > NFS4_MAXPATHLEN)
3696 goto out;
3698 status = -ENOMEM;
3699 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3700 if (data == NULL)
3701 goto out;
3703 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3704 data->arg.u.symlink.pages = &page;
3705 data->arg.u.symlink.len = len;
3706 data->arg.label = label;
3708 status = nfs4_do_create(dir, dentry, data);
3710 nfs4_free_createdata(data);
3711 out:
3712 return status;
3715 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3716 struct page *page, unsigned int len, struct iattr *sattr)
3718 struct nfs4_exception exception = { };
3719 struct nfs4_label l, *label = NULL;
3720 int err;
3722 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3724 do {
3725 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3726 trace_nfs4_symlink(dir, &dentry->d_name, err);
3727 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3728 &exception);
3729 } while (exception.retry);
3731 nfs4_label_release_security(label);
3732 return err;
3735 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3736 struct iattr *sattr, struct nfs4_label *label)
3738 struct nfs4_createdata *data;
3739 int status = -ENOMEM;
3741 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3742 if (data == NULL)
3743 goto out;
3745 data->arg.label = label;
3746 status = nfs4_do_create(dir, dentry, data);
3748 nfs4_free_createdata(data);
3749 out:
3750 return status;
3753 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3754 struct iattr *sattr)
3756 struct nfs4_exception exception = { };
3757 struct nfs4_label l, *label = NULL;
3758 int err;
3760 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3762 sattr->ia_mode &= ~current_umask();
3763 do {
3764 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3765 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3766 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3767 &exception);
3768 } while (exception.retry);
3769 nfs4_label_release_security(label);
3771 return err;
3774 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3775 u64 cookie, struct page **pages, unsigned int count, int plus)
3777 struct inode *dir = dentry->d_inode;
3778 struct nfs4_readdir_arg args = {
3779 .fh = NFS_FH(dir),
3780 .pages = pages,
3781 .pgbase = 0,
3782 .count = count,
3783 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3784 .plus = plus,
3786 struct nfs4_readdir_res res;
3787 struct rpc_message msg = {
3788 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3789 .rpc_argp = &args,
3790 .rpc_resp = &res,
3791 .rpc_cred = cred,
3793 int status;
3795 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3796 dentry,
3797 (unsigned long long)cookie);
3798 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3799 res.pgbase = args.pgbase;
3800 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3801 if (status >= 0) {
3802 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3803 status += args.pgbase;
3806 nfs_invalidate_atime(dir);
3808 dprintk("%s: returns %d\n", __func__, status);
3809 return status;
3812 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3813 u64 cookie, struct page **pages, unsigned int count, int plus)
3815 struct nfs4_exception exception = { };
3816 int err;
3817 do {
3818 err = _nfs4_proc_readdir(dentry, cred, cookie,
3819 pages, count, plus);
3820 trace_nfs4_readdir(dentry->d_inode, err);
3821 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
3822 &exception);
3823 } while (exception.retry);
3824 return err;
3827 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3828 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
3830 struct nfs4_createdata *data;
3831 int mode = sattr->ia_mode;
3832 int status = -ENOMEM;
3834 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3835 if (data == NULL)
3836 goto out;
3838 if (S_ISFIFO(mode))
3839 data->arg.ftype = NF4FIFO;
3840 else if (S_ISBLK(mode)) {
3841 data->arg.ftype = NF4BLK;
3842 data->arg.u.device.specdata1 = MAJOR(rdev);
3843 data->arg.u.device.specdata2 = MINOR(rdev);
3845 else if (S_ISCHR(mode)) {
3846 data->arg.ftype = NF4CHR;
3847 data->arg.u.device.specdata1 = MAJOR(rdev);
3848 data->arg.u.device.specdata2 = MINOR(rdev);
3849 } else if (!S_ISSOCK(mode)) {
3850 status = -EINVAL;
3851 goto out_free;
3854 data->arg.label = label;
3855 status = nfs4_do_create(dir, dentry, data);
3856 out_free:
3857 nfs4_free_createdata(data);
3858 out:
3859 return status;
3862 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3863 struct iattr *sattr, dev_t rdev)
3865 struct nfs4_exception exception = { };
3866 struct nfs4_label l, *label = NULL;
3867 int err;
3869 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3871 sattr->ia_mode &= ~current_umask();
3872 do {
3873 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3874 trace_nfs4_mknod(dir, &dentry->d_name, err);
3875 err = nfs4_handle_exception(NFS_SERVER(dir), err,
3876 &exception);
3877 } while (exception.retry);
3879 nfs4_label_release_security(label);
3881 return err;
3884 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3885 struct nfs_fsstat *fsstat)
3887 struct nfs4_statfs_arg args = {
3888 .fh = fhandle,
3889 .bitmask = server->attr_bitmask,
3891 struct nfs4_statfs_res res = {
3892 .fsstat = fsstat,
3894 struct rpc_message msg = {
3895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3896 .rpc_argp = &args,
3897 .rpc_resp = &res,
3900 nfs_fattr_init(fsstat->fattr);
3901 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3904 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3906 struct nfs4_exception exception = { };
3907 int err;
3908 do {
3909 err = nfs4_handle_exception(server,
3910 _nfs4_proc_statfs(server, fhandle, fsstat),
3911 &exception);
3912 } while (exception.retry);
3913 return err;
3916 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3917 struct nfs_fsinfo *fsinfo)
3919 struct nfs4_fsinfo_arg args = {
3920 .fh = fhandle,
3921 .bitmask = server->attr_bitmask,
3923 struct nfs4_fsinfo_res res = {
3924 .fsinfo = fsinfo,
3926 struct rpc_message msg = {
3927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3928 .rpc_argp = &args,
3929 .rpc_resp = &res,
3932 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3935 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3937 struct nfs4_exception exception = { };
3938 unsigned long now = jiffies;
3939 int err;
3941 do {
3942 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3943 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
3944 if (err == 0) {
3945 struct nfs_client *clp = server->nfs_client;
3947 spin_lock(&clp->cl_lock);
3948 clp->cl_lease_time = fsinfo->lease_time * HZ;
3949 clp->cl_last_renewal = now;
3950 spin_unlock(&clp->cl_lock);
3951 break;
3953 err = nfs4_handle_exception(server, err, &exception);
3954 } while (exception.retry);
3955 return err;
3958 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3960 int error;
3962 nfs_fattr_init(fsinfo->fattr);
3963 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3964 if (error == 0) {
3965 /* block layout checks this! */
3966 server->pnfs_blksize = fsinfo->blksize;
3967 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3970 return error;
3973 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3974 struct nfs_pathconf *pathconf)
3976 struct nfs4_pathconf_arg args = {
3977 .fh = fhandle,
3978 .bitmask = server->attr_bitmask,
3980 struct nfs4_pathconf_res res = {
3981 .pathconf = pathconf,
3983 struct rpc_message msg = {
3984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3985 .rpc_argp = &args,
3986 .rpc_resp = &res,
3989 /* None of the pathconf attributes are mandatory to implement */
3990 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3991 memset(pathconf, 0, sizeof(*pathconf));
3992 return 0;
3995 nfs_fattr_init(pathconf->fattr);
3996 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3999 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4000 struct nfs_pathconf *pathconf)
4002 struct nfs4_exception exception = { };
4003 int err;
4005 do {
4006 err = nfs4_handle_exception(server,
4007 _nfs4_proc_pathconf(server, fhandle, pathconf),
4008 &exception);
4009 } while (exception.retry);
4010 return err;
4013 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4014 const struct nfs_open_context *ctx,
4015 const struct nfs_lock_context *l_ctx,
4016 fmode_t fmode)
4018 const struct nfs_lockowner *lockowner = NULL;
4020 if (l_ctx != NULL)
4021 lockowner = &l_ctx->lockowner;
4022 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
4024 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4026 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4027 const struct nfs_open_context *ctx,
4028 const struct nfs_lock_context *l_ctx,
4029 fmode_t fmode)
4031 nfs4_stateid current_stateid;
4033 /* If the current stateid represents a lost lock, then exit */
4034 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4035 return true;
4036 return nfs4_stateid_match(stateid, &current_stateid);
4039 static bool nfs4_error_stateid_expired(int err)
4041 switch (err) {
4042 case -NFS4ERR_DELEG_REVOKED:
4043 case -NFS4ERR_ADMIN_REVOKED:
4044 case -NFS4ERR_BAD_STATEID:
4045 case -NFS4ERR_STALE_STATEID:
4046 case -NFS4ERR_OLD_STATEID:
4047 case -NFS4ERR_OPENMODE:
4048 case -NFS4ERR_EXPIRED:
4049 return true;
4051 return false;
4054 void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
4056 nfs_invalidate_atime(hdr->inode);
4059 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4061 struct nfs_server *server = NFS_SERVER(hdr->inode);
4063 trace_nfs4_read(hdr, task->tk_status);
4064 if (nfs4_async_handle_error(task, server,
4065 hdr->args.context->state) == -EAGAIN) {
4066 rpc_restart_call_prepare(task);
4067 return -EAGAIN;
4070 __nfs4_read_done_cb(hdr);
4071 if (task->tk_status > 0)
4072 renew_lease(server, hdr->timestamp);
4073 return 0;
4076 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4077 struct nfs_pgio_args *args)
4080 if (!nfs4_error_stateid_expired(task->tk_status) ||
4081 nfs4_stateid_is_current(&args->stateid,
4082 args->context,
4083 args->lock_context,
4084 FMODE_READ))
4085 return false;
4086 rpc_restart_call_prepare(task);
4087 return true;
4090 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4093 dprintk("--> %s\n", __func__);
4095 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4096 return -EAGAIN;
4097 if (nfs4_read_stateid_changed(task, &hdr->args))
4098 return -EAGAIN;
4099 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4100 nfs4_read_done_cb(task, hdr);
4103 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4104 struct rpc_message *msg)
4106 hdr->timestamp = jiffies;
4107 hdr->pgio_done_cb = nfs4_read_done_cb;
4108 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4109 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4112 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4113 struct nfs_pgio_header *hdr)
4115 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4116 &hdr->args.seq_args,
4117 &hdr->res.seq_res,
4118 task))
4119 return 0;
4120 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4121 hdr->args.lock_context,
4122 hdr->rw_ops->rw_mode) == -EIO)
4123 return -EIO;
4124 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4125 return -EIO;
4126 return 0;
4129 static int nfs4_write_done_cb(struct rpc_task *task,
4130 struct nfs_pgio_header *hdr)
4132 struct inode *inode = hdr->inode;
4134 trace_nfs4_write(hdr, task->tk_status);
4135 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4136 hdr->args.context->state) == -EAGAIN) {
4137 rpc_restart_call_prepare(task);
4138 return -EAGAIN;
4140 if (task->tk_status >= 0) {
4141 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4142 nfs_post_op_update_inode_force_wcc(inode, &hdr->fattr);
4144 return 0;
4147 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4148 struct nfs_pgio_args *args)
4151 if (!nfs4_error_stateid_expired(task->tk_status) ||
4152 nfs4_stateid_is_current(&args->stateid,
4153 args->context,
4154 args->lock_context,
4155 FMODE_WRITE))
4156 return false;
4157 rpc_restart_call_prepare(task);
4158 return true;
4161 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4163 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4164 return -EAGAIN;
4165 if (nfs4_write_stateid_changed(task, &hdr->args))
4166 return -EAGAIN;
4167 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4168 nfs4_write_done_cb(task, hdr);
4171 static
4172 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4174 /* Don't request attributes for pNFS or O_DIRECT writes */
4175 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4176 return false;
4177 /* Otherwise, request attributes if and only if we don't hold
4178 * a delegation
4180 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4183 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4184 struct rpc_message *msg)
4186 struct nfs_server *server = NFS_SERVER(hdr->inode);
4188 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4189 hdr->args.bitmask = NULL;
4190 hdr->res.fattr = NULL;
4191 } else
4192 hdr->args.bitmask = server->cache_consistency_bitmask;
4194 if (!hdr->pgio_done_cb)
4195 hdr->pgio_done_cb = nfs4_write_done_cb;
4196 hdr->res.server = server;
4197 hdr->timestamp = jiffies;
4199 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4200 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4203 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4205 nfs4_setup_sequence(NFS_SERVER(data->inode),
4206 &data->args.seq_args,
4207 &data->res.seq_res,
4208 task);
4211 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4213 struct inode *inode = data->inode;
4215 trace_nfs4_commit(data, task->tk_status);
4216 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
4217 rpc_restart_call_prepare(task);
4218 return -EAGAIN;
4220 return 0;
4223 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4225 if (!nfs4_sequence_done(task, &data->res.seq_res))
4226 return -EAGAIN;
4227 return data->commit_done_cb(task, data);
4230 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4232 struct nfs_server *server = NFS_SERVER(data->inode);
4234 if (data->commit_done_cb == NULL)
4235 data->commit_done_cb = nfs4_commit_done_cb;
4236 data->res.server = server;
4237 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4238 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4241 struct nfs4_renewdata {
4242 struct nfs_client *client;
4243 unsigned long timestamp;
4247 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4248 * standalone procedure for queueing an asynchronous RENEW.
4250 static void nfs4_renew_release(void *calldata)
4252 struct nfs4_renewdata *data = calldata;
4253 struct nfs_client *clp = data->client;
4255 if (atomic_read(&clp->cl_count) > 1)
4256 nfs4_schedule_state_renewal(clp);
4257 nfs_put_client(clp);
4258 kfree(data);
4261 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4263 struct nfs4_renewdata *data = calldata;
4264 struct nfs_client *clp = data->client;
4265 unsigned long timestamp = data->timestamp;
4267 trace_nfs4_renew_async(clp, task->tk_status);
4268 switch (task->tk_status) {
4269 case 0:
4270 break;
4271 case -NFS4ERR_LEASE_MOVED:
4272 nfs4_schedule_lease_moved_recovery(clp);
4273 break;
4274 default:
4275 /* Unless we're shutting down, schedule state recovery! */
4276 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4277 return;
4278 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4279 nfs4_schedule_lease_recovery(clp);
4280 return;
4282 nfs4_schedule_path_down_recovery(clp);
4284 do_renew_lease(clp, timestamp);
4287 static const struct rpc_call_ops nfs4_renew_ops = {
4288 .rpc_call_done = nfs4_renew_done,
4289 .rpc_release = nfs4_renew_release,
4292 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4294 struct rpc_message msg = {
4295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4296 .rpc_argp = clp,
4297 .rpc_cred = cred,
4299 struct nfs4_renewdata *data;
4301 if (renew_flags == 0)
4302 return 0;
4303 if (!atomic_inc_not_zero(&clp->cl_count))
4304 return -EIO;
4305 data = kmalloc(sizeof(*data), GFP_NOFS);
4306 if (data == NULL)
4307 return -ENOMEM;
4308 data->client = clp;
4309 data->timestamp = jiffies;
4310 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4311 &nfs4_renew_ops, data);
4314 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4316 struct rpc_message msg = {
4317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4318 .rpc_argp = clp,
4319 .rpc_cred = cred,
4321 unsigned long now = jiffies;
4322 int status;
4324 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4325 if (status < 0)
4326 return status;
4327 do_renew_lease(clp, now);
4328 return 0;
4331 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4333 return server->caps & NFS_CAP_ACLS;
4336 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4337 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4338 * the stack.
4340 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4342 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4343 struct page **pages, unsigned int *pgbase)
4345 struct page *newpage, **spages;
4346 int rc = 0;
4347 size_t len;
4348 spages = pages;
4350 do {
4351 len = min_t(size_t, PAGE_SIZE, buflen);
4352 newpage = alloc_page(GFP_KERNEL);
4354 if (newpage == NULL)
4355 goto unwind;
4356 memcpy(page_address(newpage), buf, len);
4357 buf += len;
4358 buflen -= len;
4359 *pages++ = newpage;
4360 rc++;
4361 } while (buflen != 0);
4363 return rc;
4365 unwind:
4366 for(; rc > 0; rc--)
4367 __free_page(spages[rc-1]);
4368 return -ENOMEM;
4371 struct nfs4_cached_acl {
4372 int cached;
4373 size_t len;
4374 char data[0];
4377 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4379 struct nfs_inode *nfsi = NFS_I(inode);
4381 spin_lock(&inode->i_lock);
4382 kfree(nfsi->nfs4_acl);
4383 nfsi->nfs4_acl = acl;
4384 spin_unlock(&inode->i_lock);
4387 static void nfs4_zap_acl_attr(struct inode *inode)
4389 nfs4_set_cached_acl(inode, NULL);
4392 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4394 struct nfs_inode *nfsi = NFS_I(inode);
4395 struct nfs4_cached_acl *acl;
4396 int ret = -ENOENT;
4398 spin_lock(&inode->i_lock);
4399 acl = nfsi->nfs4_acl;
4400 if (acl == NULL)
4401 goto out;
4402 if (buf == NULL) /* user is just asking for length */
4403 goto out_len;
4404 if (acl->cached == 0)
4405 goto out;
4406 ret = -ERANGE; /* see getxattr(2) man page */
4407 if (acl->len > buflen)
4408 goto out;
4409 memcpy(buf, acl->data, acl->len);
4410 out_len:
4411 ret = acl->len;
4412 out:
4413 spin_unlock(&inode->i_lock);
4414 return ret;
4417 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4419 struct nfs4_cached_acl *acl;
4420 size_t buflen = sizeof(*acl) + acl_len;
4422 if (buflen <= PAGE_SIZE) {
4423 acl = kmalloc(buflen, GFP_KERNEL);
4424 if (acl == NULL)
4425 goto out;
4426 acl->cached = 1;
4427 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4428 } else {
4429 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4430 if (acl == NULL)
4431 goto out;
4432 acl->cached = 0;
4434 acl->len = acl_len;
4435 out:
4436 nfs4_set_cached_acl(inode, acl);
4440 * The getxattr API returns the required buffer length when called with a
4441 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4442 * the required buf. On a NULL buf, we send a page of data to the server
4443 * guessing that the ACL request can be serviced by a page. If so, we cache
4444 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4445 * the cache. If not so, we throw away the page, and cache the required
4446 * length. The next getxattr call will then produce another round trip to
4447 * the server, this time with the input buf of the required size.
4449 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4451 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4452 struct nfs_getaclargs args = {
4453 .fh = NFS_FH(inode),
4454 .acl_pages = pages,
4455 .acl_len = buflen,
4457 struct nfs_getaclres res = {
4458 .acl_len = buflen,
4460 struct rpc_message msg = {
4461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4462 .rpc_argp = &args,
4463 .rpc_resp = &res,
4465 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4466 int ret = -ENOMEM, i;
4468 /* As long as we're doing a round trip to the server anyway,
4469 * let's be prepared for a page of acl data. */
4470 if (npages == 0)
4471 npages = 1;
4472 if (npages > ARRAY_SIZE(pages))
4473 return -ERANGE;
4475 for (i = 0; i < npages; i++) {
4476 pages[i] = alloc_page(GFP_KERNEL);
4477 if (!pages[i])
4478 goto out_free;
4481 /* for decoding across pages */
4482 res.acl_scratch = alloc_page(GFP_KERNEL);
4483 if (!res.acl_scratch)
4484 goto out_free;
4486 args.acl_len = npages * PAGE_SIZE;
4487 args.acl_pgbase = 0;
4489 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4490 __func__, buf, buflen, npages, args.acl_len);
4491 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4492 &msg, &args.seq_args, &res.seq_res, 0);
4493 if (ret)
4494 goto out_free;
4496 /* Handle the case where the passed-in buffer is too short */
4497 if (res.acl_flags & NFS4_ACL_TRUNC) {
4498 /* Did the user only issue a request for the acl length? */
4499 if (buf == NULL)
4500 goto out_ok;
4501 ret = -ERANGE;
4502 goto out_free;
4504 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4505 if (buf) {
4506 if (res.acl_len > buflen) {
4507 ret = -ERANGE;
4508 goto out_free;
4510 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4512 out_ok:
4513 ret = res.acl_len;
4514 out_free:
4515 for (i = 0; i < npages; i++)
4516 if (pages[i])
4517 __free_page(pages[i]);
4518 if (res.acl_scratch)
4519 __free_page(res.acl_scratch);
4520 return ret;
4523 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4525 struct nfs4_exception exception = { };
4526 ssize_t ret;
4527 do {
4528 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4529 trace_nfs4_get_acl(inode, ret);
4530 if (ret >= 0)
4531 break;
4532 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4533 } while (exception.retry);
4534 return ret;
4537 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4539 struct nfs_server *server = NFS_SERVER(inode);
4540 int ret;
4542 if (!nfs4_server_supports_acls(server))
4543 return -EOPNOTSUPP;
4544 ret = nfs_revalidate_inode(server, inode);
4545 if (ret < 0)
4546 return ret;
4547 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4548 nfs_zap_acl_cache(inode);
4549 ret = nfs4_read_cached_acl(inode, buf, buflen);
4550 if (ret != -ENOENT)
4551 /* -ENOENT is returned if there is no ACL or if there is an ACL
4552 * but no cached acl data, just the acl length */
4553 return ret;
4554 return nfs4_get_acl_uncached(inode, buf, buflen);
4557 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4559 struct nfs_server *server = NFS_SERVER(inode);
4560 struct page *pages[NFS4ACL_MAXPAGES];
4561 struct nfs_setaclargs arg = {
4562 .fh = NFS_FH(inode),
4563 .acl_pages = pages,
4564 .acl_len = buflen,
4566 struct nfs_setaclres res;
4567 struct rpc_message msg = {
4568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4569 .rpc_argp = &arg,
4570 .rpc_resp = &res,
4572 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4573 int ret, i;
4575 if (!nfs4_server_supports_acls(server))
4576 return -EOPNOTSUPP;
4577 if (npages > ARRAY_SIZE(pages))
4578 return -ERANGE;
4579 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4580 if (i < 0)
4581 return i;
4582 nfs4_inode_return_delegation(inode);
4583 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4586 * Free each page after tx, so the only ref left is
4587 * held by the network stack
4589 for (; i > 0; i--)
4590 put_page(pages[i-1]);
4593 * Acl update can result in inode attribute update.
4594 * so mark the attribute cache invalid.
4596 spin_lock(&inode->i_lock);
4597 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4598 spin_unlock(&inode->i_lock);
4599 nfs_access_zap_cache(inode);
4600 nfs_zap_acl_cache(inode);
4601 return ret;
4604 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4606 struct nfs4_exception exception = { };
4607 int err;
4608 do {
4609 err = __nfs4_proc_set_acl(inode, buf, buflen);
4610 trace_nfs4_set_acl(inode, err);
4611 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4612 &exception);
4613 } while (exception.retry);
4614 return err;
4617 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
4618 static int _nfs4_get_security_label(struct inode *inode, void *buf,
4619 size_t buflen)
4621 struct nfs_server *server = NFS_SERVER(inode);
4622 struct nfs_fattr fattr;
4623 struct nfs4_label label = {0, 0, buflen, buf};
4625 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4626 struct nfs4_getattr_arg arg = {
4627 .fh = NFS_FH(inode),
4628 .bitmask = bitmask,
4630 struct nfs4_getattr_res res = {
4631 .fattr = &fattr,
4632 .label = &label,
4633 .server = server,
4635 struct rpc_message msg = {
4636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4637 .rpc_argp = &arg,
4638 .rpc_resp = &res,
4640 int ret;
4642 nfs_fattr_init(&fattr);
4644 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
4645 if (ret)
4646 return ret;
4647 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4648 return -ENOENT;
4649 if (buflen < label.len)
4650 return -ERANGE;
4651 return 0;
4654 static int nfs4_get_security_label(struct inode *inode, void *buf,
4655 size_t buflen)
4657 struct nfs4_exception exception = { };
4658 int err;
4660 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4661 return -EOPNOTSUPP;
4663 do {
4664 err = _nfs4_get_security_label(inode, buf, buflen);
4665 trace_nfs4_get_security_label(inode, err);
4666 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4667 &exception);
4668 } while (exception.retry);
4669 return err;
4672 static int _nfs4_do_set_security_label(struct inode *inode,
4673 struct nfs4_label *ilabel,
4674 struct nfs_fattr *fattr,
4675 struct nfs4_label *olabel)
4678 struct iattr sattr = {0};
4679 struct nfs_server *server = NFS_SERVER(inode);
4680 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4681 struct nfs_setattrargs arg = {
4682 .fh = NFS_FH(inode),
4683 .iap = &sattr,
4684 .server = server,
4685 .bitmask = bitmask,
4686 .label = ilabel,
4688 struct nfs_setattrres res = {
4689 .fattr = fattr,
4690 .label = olabel,
4691 .server = server,
4693 struct rpc_message msg = {
4694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4695 .rpc_argp = &arg,
4696 .rpc_resp = &res,
4698 int status;
4700 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
4702 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4703 if (status)
4704 dprintk("%s failed: %d\n", __func__, status);
4706 return status;
4709 static int nfs4_do_set_security_label(struct inode *inode,
4710 struct nfs4_label *ilabel,
4711 struct nfs_fattr *fattr,
4712 struct nfs4_label *olabel)
4714 struct nfs4_exception exception = { };
4715 int err;
4717 do {
4718 err = _nfs4_do_set_security_label(inode, ilabel,
4719 fattr, olabel);
4720 trace_nfs4_set_security_label(inode, err);
4721 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4722 &exception);
4723 } while (exception.retry);
4724 return err;
4727 static int
4728 nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4730 struct nfs4_label ilabel, *olabel = NULL;
4731 struct nfs_fattr fattr;
4732 struct rpc_cred *cred;
4733 struct inode *inode = dentry->d_inode;
4734 int status;
4736 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4737 return -EOPNOTSUPP;
4739 nfs_fattr_init(&fattr);
4741 ilabel.pi = 0;
4742 ilabel.lfs = 0;
4743 ilabel.label = (char *)buf;
4744 ilabel.len = buflen;
4746 cred = rpc_lookup_cred();
4747 if (IS_ERR(cred))
4748 return PTR_ERR(cred);
4750 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4751 if (IS_ERR(olabel)) {
4752 status = -PTR_ERR(olabel);
4753 goto out;
4756 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4757 if (status == 0)
4758 nfs_setsecurity(inode, &fattr, olabel);
4760 nfs4_label_free(olabel);
4761 out:
4762 put_rpccred(cred);
4763 return status;
4765 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4768 static int
4769 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4771 struct nfs_client *clp = server->nfs_client;
4773 if (task->tk_status >= 0)
4774 return 0;
4775 switch(task->tk_status) {
4776 case -NFS4ERR_DELEG_REVOKED:
4777 case -NFS4ERR_ADMIN_REVOKED:
4778 case -NFS4ERR_BAD_STATEID:
4779 case -NFS4ERR_OPENMODE:
4780 if (state == NULL)
4781 break;
4782 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4783 goto recovery_failed;
4784 goto wait_on_recovery;
4785 case -NFS4ERR_EXPIRED:
4786 if (state != NULL) {
4787 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4788 goto recovery_failed;
4790 case -NFS4ERR_STALE_STATEID:
4791 case -NFS4ERR_STALE_CLIENTID:
4792 nfs4_schedule_lease_recovery(clp);
4793 goto wait_on_recovery;
4794 case -NFS4ERR_MOVED:
4795 if (nfs4_schedule_migration_recovery(server) < 0)
4796 goto recovery_failed;
4797 goto wait_on_recovery;
4798 case -NFS4ERR_LEASE_MOVED:
4799 nfs4_schedule_lease_moved_recovery(clp);
4800 goto wait_on_recovery;
4801 #if defined(CONFIG_NFS_V4_1)
4802 case -NFS4ERR_BADSESSION:
4803 case -NFS4ERR_BADSLOT:
4804 case -NFS4ERR_BAD_HIGH_SLOT:
4805 case -NFS4ERR_DEADSESSION:
4806 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4807 case -NFS4ERR_SEQ_FALSE_RETRY:
4808 case -NFS4ERR_SEQ_MISORDERED:
4809 dprintk("%s ERROR %d, Reset session\n", __func__,
4810 task->tk_status);
4811 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4812 goto wait_on_recovery;
4813 #endif /* CONFIG_NFS_V4_1 */
4814 case -NFS4ERR_DELAY:
4815 nfs_inc_server_stats(server, NFSIOS_DELAY);
4816 case -NFS4ERR_GRACE:
4817 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4818 case -NFS4ERR_RETRY_UNCACHED_REP:
4819 case -NFS4ERR_OLD_STATEID:
4820 goto restart_call;
4822 task->tk_status = nfs4_map_errors(task->tk_status);
4823 return 0;
4824 recovery_failed:
4825 task->tk_status = -EIO;
4826 return 0;
4827 wait_on_recovery:
4828 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4829 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4830 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4831 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4832 goto recovery_failed;
4833 restart_call:
4834 task->tk_status = 0;
4835 return -EAGAIN;
4838 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4839 nfs4_verifier *bootverf)
4841 __be32 verf[2];
4843 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4844 /* An impossible timestamp guarantees this value
4845 * will never match a generated boot time. */
4846 verf[0] = 0;
4847 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
4848 } else {
4849 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4850 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4851 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
4853 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4856 static unsigned int
4857 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4858 char *buf, size_t len)
4860 unsigned int result;
4862 rcu_read_lock();
4863 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4864 clp->cl_ipaddr,
4865 rpc_peeraddr2str(clp->cl_rpcclient,
4866 RPC_DISPLAY_ADDR),
4867 rpc_peeraddr2str(clp->cl_rpcclient,
4868 RPC_DISPLAY_PROTO));
4869 rcu_read_unlock();
4870 return result;
4873 static unsigned int
4874 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4875 char *buf, size_t len)
4877 const char *nodename = clp->cl_rpcclient->cl_nodename;
4879 if (nfs4_client_id_uniquifier[0] != '\0')
4880 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4881 clp->rpc_ops->version,
4882 clp->cl_minorversion,
4883 nfs4_client_id_uniquifier,
4884 nodename);
4885 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4886 clp->rpc_ops->version, clp->cl_minorversion,
4887 nodename);
4891 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
4892 * services. Advertise one based on the address family of the
4893 * clientaddr.
4895 static unsigned int
4896 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
4898 if (strchr(clp->cl_ipaddr, ':') != NULL)
4899 return scnprintf(buf, len, "tcp6");
4900 else
4901 return scnprintf(buf, len, "tcp");
4905 * nfs4_proc_setclientid - Negotiate client ID
4906 * @clp: state data structure
4907 * @program: RPC program for NFSv4 callback service
4908 * @port: IP port number for NFS4 callback service
4909 * @cred: RPC credential to use for this call
4910 * @res: where to place the result
4912 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4914 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4915 unsigned short port, struct rpc_cred *cred,
4916 struct nfs4_setclientid_res *res)
4918 nfs4_verifier sc_verifier;
4919 struct nfs4_setclientid setclientid = {
4920 .sc_verifier = &sc_verifier,
4921 .sc_prog = program,
4922 .sc_cb_ident = clp->cl_cb_ident,
4924 struct rpc_message msg = {
4925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4926 .rpc_argp = &setclientid,
4927 .rpc_resp = res,
4928 .rpc_cred = cred,
4930 int status;
4932 /* nfs_client_id4 */
4933 nfs4_init_boot_verifier(clp, &sc_verifier);
4934 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4935 setclientid.sc_name_len =
4936 nfs4_init_uniform_client_string(clp,
4937 setclientid.sc_name,
4938 sizeof(setclientid.sc_name));
4939 else
4940 setclientid.sc_name_len =
4941 nfs4_init_nonuniform_client_string(clp,
4942 setclientid.sc_name,
4943 sizeof(setclientid.sc_name));
4944 /* cb_client4 */
4945 setclientid.sc_netid_len =
4946 nfs4_init_callback_netid(clp,
4947 setclientid.sc_netid,
4948 sizeof(setclientid.sc_netid));
4949 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4950 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4951 clp->cl_ipaddr, port >> 8, port & 255);
4953 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4954 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4955 setclientid.sc_name_len, setclientid.sc_name);
4956 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4957 trace_nfs4_setclientid(clp, status);
4958 dprintk("NFS reply setclientid: %d\n", status);
4959 return status;
4963 * nfs4_proc_setclientid_confirm - Confirm client ID
4964 * @clp: state data structure
4965 * @res: result of a previous SETCLIENTID
4966 * @cred: RPC credential to use for this call
4968 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4970 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4971 struct nfs4_setclientid_res *arg,
4972 struct rpc_cred *cred)
4974 struct rpc_message msg = {
4975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4976 .rpc_argp = arg,
4977 .rpc_cred = cred,
4979 int status;
4981 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4982 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4983 clp->cl_clientid);
4984 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4985 trace_nfs4_setclientid_confirm(clp, status);
4986 dprintk("NFS reply setclientid_confirm: %d\n", status);
4987 return status;
4990 struct nfs4_delegreturndata {
4991 struct nfs4_delegreturnargs args;
4992 struct nfs4_delegreturnres res;
4993 struct nfs_fh fh;
4994 nfs4_stateid stateid;
4995 unsigned long timestamp;
4996 struct nfs_fattr fattr;
4997 int rpc_status;
5000 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5002 struct nfs4_delegreturndata *data = calldata;
5004 if (!nfs4_sequence_done(task, &data->res.seq_res))
5005 return;
5007 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5008 switch (task->tk_status) {
5009 case 0:
5010 renew_lease(data->res.server, data->timestamp);
5011 break;
5012 case -NFS4ERR_ADMIN_REVOKED:
5013 case -NFS4ERR_DELEG_REVOKED:
5014 case -NFS4ERR_BAD_STATEID:
5015 case -NFS4ERR_OLD_STATEID:
5016 case -NFS4ERR_STALE_STATEID:
5017 case -NFS4ERR_EXPIRED:
5018 task->tk_status = 0;
5019 break;
5020 default:
5021 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
5022 -EAGAIN) {
5023 rpc_restart_call_prepare(task);
5024 return;
5027 data->rpc_status = task->tk_status;
5030 static void nfs4_delegreturn_release(void *calldata)
5032 kfree(calldata);
5035 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5037 struct nfs4_delegreturndata *d_data;
5039 d_data = (struct nfs4_delegreturndata *)data;
5041 nfs4_setup_sequence(d_data->res.server,
5042 &d_data->args.seq_args,
5043 &d_data->res.seq_res,
5044 task);
5047 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5048 .rpc_call_prepare = nfs4_delegreturn_prepare,
5049 .rpc_call_done = nfs4_delegreturn_done,
5050 .rpc_release = nfs4_delegreturn_release,
5053 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5055 struct nfs4_delegreturndata *data;
5056 struct nfs_server *server = NFS_SERVER(inode);
5057 struct rpc_task *task;
5058 struct rpc_message msg = {
5059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5060 .rpc_cred = cred,
5062 struct rpc_task_setup task_setup_data = {
5063 .rpc_client = server->client,
5064 .rpc_message = &msg,
5065 .callback_ops = &nfs4_delegreturn_ops,
5066 .flags = RPC_TASK_ASYNC,
5068 int status = 0;
5070 data = kzalloc(sizeof(*data), GFP_NOFS);
5071 if (data == NULL)
5072 return -ENOMEM;
5073 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5074 data->args.fhandle = &data->fh;
5075 data->args.stateid = &data->stateid;
5076 data->args.bitmask = server->cache_consistency_bitmask;
5077 nfs_copy_fh(&data->fh, NFS_FH(inode));
5078 nfs4_stateid_copy(&data->stateid, stateid);
5079 data->res.fattr = &data->fattr;
5080 data->res.server = server;
5081 nfs_fattr_init(data->res.fattr);
5082 data->timestamp = jiffies;
5083 data->rpc_status = 0;
5085 task_setup_data.callback_data = data;
5086 msg.rpc_argp = &data->args;
5087 msg.rpc_resp = &data->res;
5088 task = rpc_run_task(&task_setup_data);
5089 if (IS_ERR(task))
5090 return PTR_ERR(task);
5091 if (!issync)
5092 goto out;
5093 status = nfs4_wait_for_completion_rpc_task(task);
5094 if (status != 0)
5095 goto out;
5096 status = data->rpc_status;
5097 if (status == 0)
5098 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5099 else
5100 nfs_refresh_inode(inode, &data->fattr);
5101 out:
5102 rpc_put_task(task);
5103 return status;
5106 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5108 struct nfs_server *server = NFS_SERVER(inode);
5109 struct nfs4_exception exception = { };
5110 int err;
5111 do {
5112 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5113 trace_nfs4_delegreturn(inode, err);
5114 switch (err) {
5115 case -NFS4ERR_STALE_STATEID:
5116 case -NFS4ERR_EXPIRED:
5117 case 0:
5118 return 0;
5120 err = nfs4_handle_exception(server, err, &exception);
5121 } while (exception.retry);
5122 return err;
5125 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5126 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5129 * sleep, with exponential backoff, and retry the LOCK operation.
5131 static unsigned long
5132 nfs4_set_lock_task_retry(unsigned long timeout)
5134 freezable_schedule_timeout_killable_unsafe(timeout);
5135 timeout <<= 1;
5136 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5137 return NFS4_LOCK_MAXTIMEOUT;
5138 return timeout;
5141 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5143 struct inode *inode = state->inode;
5144 struct nfs_server *server = NFS_SERVER(inode);
5145 struct nfs_client *clp = server->nfs_client;
5146 struct nfs_lockt_args arg = {
5147 .fh = NFS_FH(inode),
5148 .fl = request,
5150 struct nfs_lockt_res res = {
5151 .denied = request,
5153 struct rpc_message msg = {
5154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5155 .rpc_argp = &arg,
5156 .rpc_resp = &res,
5157 .rpc_cred = state->owner->so_cred,
5159 struct nfs4_lock_state *lsp;
5160 int status;
5162 arg.lock_owner.clientid = clp->cl_clientid;
5163 status = nfs4_set_lock_state(state, request);
5164 if (status != 0)
5165 goto out;
5166 lsp = request->fl_u.nfs4_fl.owner;
5167 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5168 arg.lock_owner.s_dev = server->s_dev;
5169 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5170 switch (status) {
5171 case 0:
5172 request->fl_type = F_UNLCK;
5173 break;
5174 case -NFS4ERR_DENIED:
5175 status = 0;
5177 request->fl_ops->fl_release_private(request);
5178 request->fl_ops = NULL;
5179 out:
5180 return status;
5183 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5185 struct nfs4_exception exception = { };
5186 int err;
5188 do {
5189 err = _nfs4_proc_getlk(state, cmd, request);
5190 trace_nfs4_get_lock(request, state, cmd, err);
5191 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5192 &exception);
5193 } while (exception.retry);
5194 return err;
5197 static int do_vfs_lock(struct file *file, struct file_lock *fl)
5199 int res = 0;
5200 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5201 case FL_POSIX:
5202 res = posix_lock_file_wait(file, fl);
5203 break;
5204 case FL_FLOCK:
5205 res = flock_lock_file_wait(file, fl);
5206 break;
5207 default:
5208 BUG();
5210 return res;
5213 struct nfs4_unlockdata {
5214 struct nfs_locku_args arg;
5215 struct nfs_locku_res res;
5216 struct nfs4_lock_state *lsp;
5217 struct nfs_open_context *ctx;
5218 struct file_lock fl;
5219 const struct nfs_server *server;
5220 unsigned long timestamp;
5223 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5224 struct nfs_open_context *ctx,
5225 struct nfs4_lock_state *lsp,
5226 struct nfs_seqid *seqid)
5228 struct nfs4_unlockdata *p;
5229 struct inode *inode = lsp->ls_state->inode;
5231 p = kzalloc(sizeof(*p), GFP_NOFS);
5232 if (p == NULL)
5233 return NULL;
5234 p->arg.fh = NFS_FH(inode);
5235 p->arg.fl = &p->fl;
5236 p->arg.seqid = seqid;
5237 p->res.seqid = seqid;
5238 p->arg.stateid = &lsp->ls_stateid;
5239 p->lsp = lsp;
5240 atomic_inc(&lsp->ls_count);
5241 /* Ensure we don't close file until we're done freeing locks! */
5242 p->ctx = get_nfs_open_context(ctx);
5243 memcpy(&p->fl, fl, sizeof(p->fl));
5244 p->server = NFS_SERVER(inode);
5245 return p;
5248 static void nfs4_locku_release_calldata(void *data)
5250 struct nfs4_unlockdata *calldata = data;
5251 nfs_free_seqid(calldata->arg.seqid);
5252 nfs4_put_lock_state(calldata->lsp);
5253 put_nfs_open_context(calldata->ctx);
5254 kfree(calldata);
5257 static void nfs4_locku_done(struct rpc_task *task, void *data)
5259 struct nfs4_unlockdata *calldata = data;
5261 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5262 return;
5263 switch (task->tk_status) {
5264 case 0:
5265 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5266 &calldata->res.stateid);
5267 renew_lease(calldata->server, calldata->timestamp);
5268 break;
5269 case -NFS4ERR_BAD_STATEID:
5270 case -NFS4ERR_OLD_STATEID:
5271 case -NFS4ERR_STALE_STATEID:
5272 case -NFS4ERR_EXPIRED:
5273 break;
5274 default:
5275 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
5276 rpc_restart_call_prepare(task);
5278 nfs_release_seqid(calldata->arg.seqid);
5281 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5283 struct nfs4_unlockdata *calldata = data;
5285 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5286 goto out_wait;
5287 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5288 /* Note: exit _without_ running nfs4_locku_done */
5289 goto out_no_action;
5291 calldata->timestamp = jiffies;
5292 if (nfs4_setup_sequence(calldata->server,
5293 &calldata->arg.seq_args,
5294 &calldata->res.seq_res,
5295 task) != 0)
5296 nfs_release_seqid(calldata->arg.seqid);
5297 return;
5298 out_no_action:
5299 task->tk_action = NULL;
5300 out_wait:
5301 nfs4_sequence_done(task, &calldata->res.seq_res);
5304 static const struct rpc_call_ops nfs4_locku_ops = {
5305 .rpc_call_prepare = nfs4_locku_prepare,
5306 .rpc_call_done = nfs4_locku_done,
5307 .rpc_release = nfs4_locku_release_calldata,
5310 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5311 struct nfs_open_context *ctx,
5312 struct nfs4_lock_state *lsp,
5313 struct nfs_seqid *seqid)
5315 struct nfs4_unlockdata *data;
5316 struct rpc_message msg = {
5317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5318 .rpc_cred = ctx->cred,
5320 struct rpc_task_setup task_setup_data = {
5321 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5322 .rpc_message = &msg,
5323 .callback_ops = &nfs4_locku_ops,
5324 .workqueue = nfsiod_workqueue,
5325 .flags = RPC_TASK_ASYNC,
5328 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5329 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5331 /* Ensure this is an unlock - when canceling a lock, the
5332 * canceled lock is passed in, and it won't be an unlock.
5334 fl->fl_type = F_UNLCK;
5336 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5337 if (data == NULL) {
5338 nfs_free_seqid(seqid);
5339 return ERR_PTR(-ENOMEM);
5342 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5343 msg.rpc_argp = &data->arg;
5344 msg.rpc_resp = &data->res;
5345 task_setup_data.callback_data = data;
5346 return rpc_run_task(&task_setup_data);
5349 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5351 struct inode *inode = state->inode;
5352 struct nfs4_state_owner *sp = state->owner;
5353 struct nfs_inode *nfsi = NFS_I(inode);
5354 struct nfs_seqid *seqid;
5355 struct nfs4_lock_state *lsp;
5356 struct rpc_task *task;
5357 int status = 0;
5358 unsigned char fl_flags = request->fl_flags;
5360 status = nfs4_set_lock_state(state, request);
5361 /* Unlock _before_ we do the RPC call */
5362 request->fl_flags |= FL_EXISTS;
5363 /* Exclude nfs_delegation_claim_locks() */
5364 mutex_lock(&sp->so_delegreturn_mutex);
5365 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5366 down_read(&nfsi->rwsem);
5367 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5368 up_read(&nfsi->rwsem);
5369 mutex_unlock(&sp->so_delegreturn_mutex);
5370 goto out;
5372 up_read(&nfsi->rwsem);
5373 mutex_unlock(&sp->so_delegreturn_mutex);
5374 if (status != 0)
5375 goto out;
5376 /* Is this a delegated lock? */
5377 lsp = request->fl_u.nfs4_fl.owner;
5378 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5379 goto out;
5380 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5381 status = -ENOMEM;
5382 if (seqid == NULL)
5383 goto out;
5384 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5385 status = PTR_ERR(task);
5386 if (IS_ERR(task))
5387 goto out;
5388 status = nfs4_wait_for_completion_rpc_task(task);
5389 rpc_put_task(task);
5390 out:
5391 request->fl_flags = fl_flags;
5392 trace_nfs4_unlock(request, state, F_SETLK, status);
5393 return status;
5396 struct nfs4_lockdata {
5397 struct nfs_lock_args arg;
5398 struct nfs_lock_res res;
5399 struct nfs4_lock_state *lsp;
5400 struct nfs_open_context *ctx;
5401 struct file_lock fl;
5402 unsigned long timestamp;
5403 int rpc_status;
5404 int cancelled;
5405 struct nfs_server *server;
5408 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5409 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5410 gfp_t gfp_mask)
5412 struct nfs4_lockdata *p;
5413 struct inode *inode = lsp->ls_state->inode;
5414 struct nfs_server *server = NFS_SERVER(inode);
5416 p = kzalloc(sizeof(*p), gfp_mask);
5417 if (p == NULL)
5418 return NULL;
5420 p->arg.fh = NFS_FH(inode);
5421 p->arg.fl = &p->fl;
5422 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5423 if (p->arg.open_seqid == NULL)
5424 goto out_free;
5425 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
5426 if (p->arg.lock_seqid == NULL)
5427 goto out_free_seqid;
5428 p->arg.lock_stateid = &lsp->ls_stateid;
5429 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
5430 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
5431 p->arg.lock_owner.s_dev = server->s_dev;
5432 p->res.lock_seqid = p->arg.lock_seqid;
5433 p->lsp = lsp;
5434 p->server = server;
5435 atomic_inc(&lsp->ls_count);
5436 p->ctx = get_nfs_open_context(ctx);
5437 get_file(fl->fl_file);
5438 memcpy(&p->fl, fl, sizeof(p->fl));
5439 return p;
5440 out_free_seqid:
5441 nfs_free_seqid(p->arg.open_seqid);
5442 out_free:
5443 kfree(p);
5444 return NULL;
5447 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5449 struct nfs4_lockdata *data = calldata;
5450 struct nfs4_state *state = data->lsp->ls_state;
5452 dprintk("%s: begin!\n", __func__);
5453 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
5454 goto out_wait;
5455 /* Do we need to do an open_to_lock_owner? */
5456 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
5457 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
5458 goto out_release_lock_seqid;
5460 data->arg.open_stateid = &state->open_stateid;
5461 data->arg.new_lock_owner = 1;
5462 data->res.open_seqid = data->arg.open_seqid;
5463 } else
5464 data->arg.new_lock_owner = 0;
5465 if (!nfs4_valid_open_stateid(state)) {
5466 data->rpc_status = -EBADF;
5467 task->tk_action = NULL;
5468 goto out_release_open_seqid;
5470 data->timestamp = jiffies;
5471 if (nfs4_setup_sequence(data->server,
5472 &data->arg.seq_args,
5473 &data->res.seq_res,
5474 task) == 0)
5475 return;
5476 out_release_open_seqid:
5477 nfs_release_seqid(data->arg.open_seqid);
5478 out_release_lock_seqid:
5479 nfs_release_seqid(data->arg.lock_seqid);
5480 out_wait:
5481 nfs4_sequence_done(task, &data->res.seq_res);
5482 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
5485 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5487 struct nfs4_lockdata *data = calldata;
5489 dprintk("%s: begin!\n", __func__);
5491 if (!nfs4_sequence_done(task, &data->res.seq_res))
5492 return;
5494 data->rpc_status = task->tk_status;
5495 if (data->arg.new_lock_owner != 0) {
5496 if (data->rpc_status == 0)
5497 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5498 else
5499 goto out;
5501 if (data->rpc_status == 0) {
5502 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
5503 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
5504 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
5506 out:
5507 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
5510 static void nfs4_lock_release(void *calldata)
5512 struct nfs4_lockdata *data = calldata;
5514 dprintk("%s: begin!\n", __func__);
5515 nfs_free_seqid(data->arg.open_seqid);
5516 if (data->cancelled != 0) {
5517 struct rpc_task *task;
5518 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5519 data->arg.lock_seqid);
5520 if (!IS_ERR(task))
5521 rpc_put_task_async(task);
5522 dprintk("%s: cancelling lock!\n", __func__);
5523 } else
5524 nfs_free_seqid(data->arg.lock_seqid);
5525 nfs4_put_lock_state(data->lsp);
5526 put_nfs_open_context(data->ctx);
5527 fput(data->fl.fl_file);
5528 kfree(data);
5529 dprintk("%s: done!\n", __func__);
5532 static const struct rpc_call_ops nfs4_lock_ops = {
5533 .rpc_call_prepare = nfs4_lock_prepare,
5534 .rpc_call_done = nfs4_lock_done,
5535 .rpc_release = nfs4_lock_release,
5538 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5540 switch (error) {
5541 case -NFS4ERR_ADMIN_REVOKED:
5542 case -NFS4ERR_BAD_STATEID:
5543 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5544 if (new_lock_owner != 0 ||
5545 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
5546 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
5547 break;
5548 case -NFS4ERR_STALE_STATEID:
5549 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
5550 case -NFS4ERR_EXPIRED:
5551 nfs4_schedule_lease_recovery(server->nfs_client);
5555 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
5557 struct nfs4_lockdata *data;
5558 struct rpc_task *task;
5559 struct rpc_message msg = {
5560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5561 .rpc_cred = state->owner->so_cred,
5563 struct rpc_task_setup task_setup_data = {
5564 .rpc_client = NFS_CLIENT(state->inode),
5565 .rpc_message = &msg,
5566 .callback_ops = &nfs4_lock_ops,
5567 .workqueue = nfsiod_workqueue,
5568 .flags = RPC_TASK_ASYNC,
5570 int ret;
5572 dprintk("%s: begin!\n", __func__);
5573 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
5574 fl->fl_u.nfs4_fl.owner,
5575 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
5576 if (data == NULL)
5577 return -ENOMEM;
5578 if (IS_SETLKW(cmd))
5579 data->arg.block = 1;
5580 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5581 msg.rpc_argp = &data->arg;
5582 msg.rpc_resp = &data->res;
5583 task_setup_data.callback_data = data;
5584 if (recovery_type > NFS_LOCK_NEW) {
5585 if (recovery_type == NFS_LOCK_RECLAIM)
5586 data->arg.reclaim = NFS_LOCK_RECLAIM;
5587 nfs4_set_sequence_privileged(&data->arg.seq_args);
5589 task = rpc_run_task(&task_setup_data);
5590 if (IS_ERR(task))
5591 return PTR_ERR(task);
5592 ret = nfs4_wait_for_completion_rpc_task(task);
5593 if (ret == 0) {
5594 ret = data->rpc_status;
5595 if (ret)
5596 nfs4_handle_setlk_error(data->server, data->lsp,
5597 data->arg.new_lock_owner, ret);
5598 } else
5599 data->cancelled = 1;
5600 rpc_put_task(task);
5601 dprintk("%s: done, ret = %d!\n", __func__, ret);
5602 return ret;
5605 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5607 struct nfs_server *server = NFS_SERVER(state->inode);
5608 struct nfs4_exception exception = {
5609 .inode = state->inode,
5611 int err;
5613 do {
5614 /* Cache the lock if possible... */
5615 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5616 return 0;
5617 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
5618 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
5619 if (err != -NFS4ERR_DELAY)
5620 break;
5621 nfs4_handle_exception(server, err, &exception);
5622 } while (exception.retry);
5623 return err;
5626 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5628 struct nfs_server *server = NFS_SERVER(state->inode);
5629 struct nfs4_exception exception = {
5630 .inode = state->inode,
5632 int err;
5634 err = nfs4_set_lock_state(state, request);
5635 if (err != 0)
5636 return err;
5637 if (!recover_lost_locks) {
5638 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5639 return 0;
5641 do {
5642 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5643 return 0;
5644 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
5645 trace_nfs4_lock_expired(request, state, F_SETLK, err);
5646 switch (err) {
5647 default:
5648 goto out;
5649 case -NFS4ERR_GRACE:
5650 case -NFS4ERR_DELAY:
5651 nfs4_handle_exception(server, err, &exception);
5652 err = 0;
5654 } while (exception.retry);
5655 out:
5656 return err;
5659 #if defined(CONFIG_NFS_V4_1)
5661 * nfs41_check_expired_locks - possibly free a lock stateid
5663 * @state: NFSv4 state for an inode
5665 * Returns NFS_OK if recovery for this stateid is now finished.
5666 * Otherwise a negative NFS4ERR value is returned.
5668 static int nfs41_check_expired_locks(struct nfs4_state *state)
5670 int status, ret = -NFS4ERR_BAD_STATEID;
5671 struct nfs4_lock_state *lsp;
5672 struct nfs_server *server = NFS_SERVER(state->inode);
5674 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
5675 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
5676 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5678 status = nfs41_test_stateid(server,
5679 &lsp->ls_stateid,
5680 cred);
5681 trace_nfs4_test_lock_stateid(state, lsp, status);
5682 if (status != NFS_OK) {
5683 /* Free the stateid unless the server
5684 * informs us the stateid is unrecognized. */
5685 if (status != -NFS4ERR_BAD_STATEID)
5686 nfs41_free_stateid(server,
5687 &lsp->ls_stateid,
5688 cred);
5689 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5690 ret = status;
5695 return ret;
5698 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5700 int status = NFS_OK;
5702 if (test_bit(LK_STATE_IN_USE, &state->flags))
5703 status = nfs41_check_expired_locks(state);
5704 if (status != NFS_OK)
5705 status = nfs4_lock_expired(state, request);
5706 return status;
5708 #endif
5710 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5712 struct nfs4_state_owner *sp = state->owner;
5713 struct nfs_inode *nfsi = NFS_I(state->inode);
5714 unsigned char fl_flags = request->fl_flags;
5715 unsigned int seq;
5716 int status = -ENOLCK;
5718 if ((fl_flags & FL_POSIX) &&
5719 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5720 goto out;
5721 /* Is this a delegated open? */
5722 status = nfs4_set_lock_state(state, request);
5723 if (status != 0)
5724 goto out;
5725 request->fl_flags |= FL_ACCESS;
5726 status = do_vfs_lock(request->fl_file, request);
5727 if (status < 0)
5728 goto out;
5729 down_read(&nfsi->rwsem);
5730 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5731 /* Yes: cache locks! */
5732 /* ...but avoid races with delegation recall... */
5733 request->fl_flags = fl_flags & ~FL_SLEEP;
5734 status = do_vfs_lock(request->fl_file, request);
5735 goto out_unlock;
5737 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5738 up_read(&nfsi->rwsem);
5739 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5740 if (status != 0)
5741 goto out;
5742 down_read(&nfsi->rwsem);
5743 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5744 status = -NFS4ERR_DELAY;
5745 goto out_unlock;
5747 /* Note: we always want to sleep here! */
5748 request->fl_flags = fl_flags | FL_SLEEP;
5749 if (do_vfs_lock(request->fl_file, request) < 0)
5750 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5751 "manager!\n", __func__);
5752 out_unlock:
5753 up_read(&nfsi->rwsem);
5754 out:
5755 request->fl_flags = fl_flags;
5756 return status;
5759 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5761 struct nfs4_exception exception = {
5762 .state = state,
5763 .inode = state->inode,
5765 int err;
5767 do {
5768 err = _nfs4_proc_setlk(state, cmd, request);
5769 trace_nfs4_set_lock(request, state, cmd, err);
5770 if (err == -NFS4ERR_DENIED)
5771 err = -EAGAIN;
5772 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5773 err, &exception);
5774 } while (exception.retry);
5775 return err;
5778 static int
5779 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5781 struct nfs_open_context *ctx;
5782 struct nfs4_state *state;
5783 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5784 int status;
5786 /* verify open state */
5787 ctx = nfs_file_open_context(filp);
5788 state = ctx->state;
5790 if (request->fl_start < 0 || request->fl_end < 0)
5791 return -EINVAL;
5793 if (IS_GETLK(cmd)) {
5794 if (state != NULL)
5795 return nfs4_proc_getlk(state, F_GETLK, request);
5796 return 0;
5799 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5800 return -EINVAL;
5802 if (request->fl_type == F_UNLCK) {
5803 if (state != NULL)
5804 return nfs4_proc_unlck(state, cmd, request);
5805 return 0;
5808 if (state == NULL)
5809 return -ENOLCK;
5811 * Don't rely on the VFS having checked the file open mode,
5812 * since it won't do this for flock() locks.
5814 switch (request->fl_type) {
5815 case F_RDLCK:
5816 if (!(filp->f_mode & FMODE_READ))
5817 return -EBADF;
5818 break;
5819 case F_WRLCK:
5820 if (!(filp->f_mode & FMODE_WRITE))
5821 return -EBADF;
5824 do {
5825 status = nfs4_proc_setlk(state, cmd, request);
5826 if ((status != -EAGAIN) || IS_SETLK(cmd))
5827 break;
5828 timeout = nfs4_set_lock_task_retry(timeout);
5829 status = -ERESTARTSYS;
5830 if (signalled())
5831 break;
5832 } while(status < 0);
5833 return status;
5836 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
5838 struct nfs_server *server = NFS_SERVER(state->inode);
5839 int err;
5841 err = nfs4_set_lock_state(state, fl);
5842 if (err != 0)
5843 return err;
5844 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5845 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
5848 struct nfs_release_lockowner_data {
5849 struct nfs4_lock_state *lsp;
5850 struct nfs_server *server;
5851 struct nfs_release_lockowner_args args;
5852 struct nfs_release_lockowner_res res;
5853 unsigned long timestamp;
5856 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5858 struct nfs_release_lockowner_data *data = calldata;
5859 nfs40_setup_sequence(data->server,
5860 &data->args.seq_args, &data->res.seq_res, task);
5861 data->timestamp = jiffies;
5864 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5866 struct nfs_release_lockowner_data *data = calldata;
5867 struct nfs_server *server = data->server;
5869 nfs40_sequence_done(task, &data->res.seq_res);
5871 switch (task->tk_status) {
5872 case 0:
5873 renew_lease(server, data->timestamp);
5874 break;
5875 case -NFS4ERR_STALE_CLIENTID:
5876 case -NFS4ERR_EXPIRED:
5877 case -NFS4ERR_LEASE_MOVED:
5878 case -NFS4ERR_DELAY:
5879 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN)
5880 rpc_restart_call_prepare(task);
5884 static void nfs4_release_lockowner_release(void *calldata)
5886 struct nfs_release_lockowner_data *data = calldata;
5887 nfs4_free_lock_state(data->server, data->lsp);
5888 kfree(calldata);
5891 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5892 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5893 .rpc_call_done = nfs4_release_lockowner_done,
5894 .rpc_release = nfs4_release_lockowner_release,
5897 static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
5899 struct nfs_release_lockowner_data *data;
5900 struct rpc_message msg = {
5901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5904 if (server->nfs_client->cl_mvops->minor_version != 0)
5905 return -EINVAL;
5907 data = kmalloc(sizeof(*data), GFP_NOFS);
5908 if (!data)
5909 return -ENOMEM;
5910 data->lsp = lsp;
5911 data->server = server;
5912 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5913 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5914 data->args.lock_owner.s_dev = server->s_dev;
5916 msg.rpc_argp = &data->args;
5917 msg.rpc_resp = &data->res;
5918 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
5919 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5920 return 0;
5923 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5925 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5926 const void *buf, size_t buflen,
5927 int flags, int type)
5929 if (strcmp(key, "") != 0)
5930 return -EINVAL;
5932 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5935 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5936 void *buf, size_t buflen, int type)
5938 if (strcmp(key, "") != 0)
5939 return -EINVAL;
5941 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5944 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5945 size_t list_len, const char *name,
5946 size_t name_len, int type)
5948 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5950 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5951 return 0;
5953 if (list && len <= list_len)
5954 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5955 return len;
5958 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5959 static inline int nfs4_server_supports_labels(struct nfs_server *server)
5961 return server->caps & NFS_CAP_SECURITY_LABEL;
5964 static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5965 const void *buf, size_t buflen,
5966 int flags, int type)
5968 if (security_ismaclabel(key))
5969 return nfs4_set_security_label(dentry, buf, buflen);
5971 return -EOPNOTSUPP;
5974 static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5975 void *buf, size_t buflen, int type)
5977 if (security_ismaclabel(key))
5978 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5979 return -EOPNOTSUPP;
5982 static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5983 size_t list_len, const char *name,
5984 size_t name_len, int type)
5986 size_t len = 0;
5988 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5989 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5990 if (list && len <= list_len)
5991 security_inode_listsecurity(dentry->d_inode, list, len);
5993 return len;
5996 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5997 .prefix = XATTR_SECURITY_PREFIX,
5998 .list = nfs4_xattr_list_nfs4_label,
5999 .get = nfs4_xattr_get_nfs4_label,
6000 .set = nfs4_xattr_set_nfs4_label,
6002 #endif
6006 * nfs_fhget will use either the mounted_on_fileid or the fileid
6008 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6010 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6011 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6012 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6013 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6014 return;
6016 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6017 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6018 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6019 fattr->nlink = 2;
6022 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6023 const struct qstr *name,
6024 struct nfs4_fs_locations *fs_locations,
6025 struct page *page)
6027 struct nfs_server *server = NFS_SERVER(dir);
6028 u32 bitmask[3] = {
6029 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6031 struct nfs4_fs_locations_arg args = {
6032 .dir_fh = NFS_FH(dir),
6033 .name = name,
6034 .page = page,
6035 .bitmask = bitmask,
6037 struct nfs4_fs_locations_res res = {
6038 .fs_locations = fs_locations,
6040 struct rpc_message msg = {
6041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6042 .rpc_argp = &args,
6043 .rpc_resp = &res,
6045 int status;
6047 dprintk("%s: start\n", __func__);
6049 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6050 * is not supported */
6051 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6052 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6053 else
6054 bitmask[0] |= FATTR4_WORD0_FILEID;
6056 nfs_fattr_init(&fs_locations->fattr);
6057 fs_locations->server = server;
6058 fs_locations->nlocations = 0;
6059 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6060 dprintk("%s: returned status = %d\n", __func__, status);
6061 return status;
6064 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6065 const struct qstr *name,
6066 struct nfs4_fs_locations *fs_locations,
6067 struct page *page)
6069 struct nfs4_exception exception = { };
6070 int err;
6071 do {
6072 err = _nfs4_proc_fs_locations(client, dir, name,
6073 fs_locations, page);
6074 trace_nfs4_get_fs_locations(dir, name, err);
6075 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6076 &exception);
6077 } while (exception.retry);
6078 return err;
6082 * This operation also signals the server that this client is
6083 * performing migration recovery. The server can stop returning
6084 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6085 * appended to this compound to identify the client ID which is
6086 * performing recovery.
6088 static int _nfs40_proc_get_locations(struct inode *inode,
6089 struct nfs4_fs_locations *locations,
6090 struct page *page, struct rpc_cred *cred)
6092 struct nfs_server *server = NFS_SERVER(inode);
6093 struct rpc_clnt *clnt = server->client;
6094 u32 bitmask[2] = {
6095 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6097 struct nfs4_fs_locations_arg args = {
6098 .clientid = server->nfs_client->cl_clientid,
6099 .fh = NFS_FH(inode),
6100 .page = page,
6101 .bitmask = bitmask,
6102 .migration = 1, /* skip LOOKUP */
6103 .renew = 1, /* append RENEW */
6105 struct nfs4_fs_locations_res res = {
6106 .fs_locations = locations,
6107 .migration = 1,
6108 .renew = 1,
6110 struct rpc_message msg = {
6111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6112 .rpc_argp = &args,
6113 .rpc_resp = &res,
6114 .rpc_cred = cred,
6116 unsigned long now = jiffies;
6117 int status;
6119 nfs_fattr_init(&locations->fattr);
6120 locations->server = server;
6121 locations->nlocations = 0;
6123 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6124 nfs4_set_sequence_privileged(&args.seq_args);
6125 status = nfs4_call_sync_sequence(clnt, server, &msg,
6126 &args.seq_args, &res.seq_res);
6127 if (status)
6128 return status;
6130 renew_lease(server, now);
6131 return 0;
6134 #ifdef CONFIG_NFS_V4_1
6137 * This operation also signals the server that this client is
6138 * performing migration recovery. The server can stop asserting
6139 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6140 * performing this operation is identified in the SEQUENCE
6141 * operation in this compound.
6143 * When the client supports GETATTR(fs_locations_info), it can
6144 * be plumbed in here.
6146 static int _nfs41_proc_get_locations(struct inode *inode,
6147 struct nfs4_fs_locations *locations,
6148 struct page *page, struct rpc_cred *cred)
6150 struct nfs_server *server = NFS_SERVER(inode);
6151 struct rpc_clnt *clnt = server->client;
6152 u32 bitmask[2] = {
6153 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6155 struct nfs4_fs_locations_arg args = {
6156 .fh = NFS_FH(inode),
6157 .page = page,
6158 .bitmask = bitmask,
6159 .migration = 1, /* skip LOOKUP */
6161 struct nfs4_fs_locations_res res = {
6162 .fs_locations = locations,
6163 .migration = 1,
6165 struct rpc_message msg = {
6166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6167 .rpc_argp = &args,
6168 .rpc_resp = &res,
6169 .rpc_cred = cred,
6171 int status;
6173 nfs_fattr_init(&locations->fattr);
6174 locations->server = server;
6175 locations->nlocations = 0;
6177 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6178 nfs4_set_sequence_privileged(&args.seq_args);
6179 status = nfs4_call_sync_sequence(clnt, server, &msg,
6180 &args.seq_args, &res.seq_res);
6181 if (status == NFS4_OK &&
6182 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6183 status = -NFS4ERR_LEASE_MOVED;
6184 return status;
6187 #endif /* CONFIG_NFS_V4_1 */
6190 * nfs4_proc_get_locations - discover locations for a migrated FSID
6191 * @inode: inode on FSID that is migrating
6192 * @locations: result of query
6193 * @page: buffer
6194 * @cred: credential to use for this operation
6196 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6197 * operation failed, or a negative errno if a local error occurred.
6199 * On success, "locations" is filled in, but if the server has
6200 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6201 * asserted.
6203 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6204 * from this client that require migration recovery.
6206 int nfs4_proc_get_locations(struct inode *inode,
6207 struct nfs4_fs_locations *locations,
6208 struct page *page, struct rpc_cred *cred)
6210 struct nfs_server *server = NFS_SERVER(inode);
6211 struct nfs_client *clp = server->nfs_client;
6212 const struct nfs4_mig_recovery_ops *ops =
6213 clp->cl_mvops->mig_recovery_ops;
6214 struct nfs4_exception exception = { };
6215 int status;
6217 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6218 (unsigned long long)server->fsid.major,
6219 (unsigned long long)server->fsid.minor,
6220 clp->cl_hostname);
6221 nfs_display_fhandle(NFS_FH(inode), __func__);
6223 do {
6224 status = ops->get_locations(inode, locations, page, cred);
6225 if (status != -NFS4ERR_DELAY)
6226 break;
6227 nfs4_handle_exception(server, status, &exception);
6228 } while (exception.retry);
6229 return status;
6233 * This operation also signals the server that this client is
6234 * performing "lease moved" recovery. The server can stop
6235 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6236 * is appended to this compound to identify the client ID which is
6237 * performing recovery.
6239 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6241 struct nfs_server *server = NFS_SERVER(inode);
6242 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6243 struct rpc_clnt *clnt = server->client;
6244 struct nfs4_fsid_present_arg args = {
6245 .fh = NFS_FH(inode),
6246 .clientid = clp->cl_clientid,
6247 .renew = 1, /* append RENEW */
6249 struct nfs4_fsid_present_res res = {
6250 .renew = 1,
6252 struct rpc_message msg = {
6253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6254 .rpc_argp = &args,
6255 .rpc_resp = &res,
6256 .rpc_cred = cred,
6258 unsigned long now = jiffies;
6259 int status;
6261 res.fh = nfs_alloc_fhandle();
6262 if (res.fh == NULL)
6263 return -ENOMEM;
6265 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6266 nfs4_set_sequence_privileged(&args.seq_args);
6267 status = nfs4_call_sync_sequence(clnt, server, &msg,
6268 &args.seq_args, &res.seq_res);
6269 nfs_free_fhandle(res.fh);
6270 if (status)
6271 return status;
6273 do_renew_lease(clp, now);
6274 return 0;
6277 #ifdef CONFIG_NFS_V4_1
6280 * This operation also signals the server that this client is
6281 * performing "lease moved" recovery. The server can stop asserting
6282 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6283 * this operation is identified in the SEQUENCE operation in this
6284 * compound.
6286 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6288 struct nfs_server *server = NFS_SERVER(inode);
6289 struct rpc_clnt *clnt = server->client;
6290 struct nfs4_fsid_present_arg args = {
6291 .fh = NFS_FH(inode),
6293 struct nfs4_fsid_present_res res = {
6295 struct rpc_message msg = {
6296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6297 .rpc_argp = &args,
6298 .rpc_resp = &res,
6299 .rpc_cred = cred,
6301 int status;
6303 res.fh = nfs_alloc_fhandle();
6304 if (res.fh == NULL)
6305 return -ENOMEM;
6307 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6308 nfs4_set_sequence_privileged(&args.seq_args);
6309 status = nfs4_call_sync_sequence(clnt, server, &msg,
6310 &args.seq_args, &res.seq_res);
6311 nfs_free_fhandle(res.fh);
6312 if (status == NFS4_OK &&
6313 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6314 status = -NFS4ERR_LEASE_MOVED;
6315 return status;
6318 #endif /* CONFIG_NFS_V4_1 */
6321 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6322 * @inode: inode on FSID to check
6323 * @cred: credential to use for this operation
6325 * Server indicates whether the FSID is present, moved, or not
6326 * recognized. This operation is necessary to clear a LEASE_MOVED
6327 * condition for this client ID.
6329 * Returns NFS4_OK if the FSID is present on this server,
6330 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6331 * NFS4ERR code if some error occurred on the server, or a
6332 * negative errno if a local failure occurred.
6334 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6336 struct nfs_server *server = NFS_SERVER(inode);
6337 struct nfs_client *clp = server->nfs_client;
6338 const struct nfs4_mig_recovery_ops *ops =
6339 clp->cl_mvops->mig_recovery_ops;
6340 struct nfs4_exception exception = { };
6341 int status;
6343 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6344 (unsigned long long)server->fsid.major,
6345 (unsigned long long)server->fsid.minor,
6346 clp->cl_hostname);
6347 nfs_display_fhandle(NFS_FH(inode), __func__);
6349 do {
6350 status = ops->fsid_present(inode, cred);
6351 if (status != -NFS4ERR_DELAY)
6352 break;
6353 nfs4_handle_exception(server, status, &exception);
6354 } while (exception.retry);
6355 return status;
6359 * If 'use_integrity' is true and the state managment nfs_client
6360 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6361 * and the machine credential as per RFC3530bis and RFC5661 Security
6362 * Considerations sections. Otherwise, just use the user cred with the
6363 * filesystem's rpc_client.
6365 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
6367 int status;
6368 struct nfs4_secinfo_arg args = {
6369 .dir_fh = NFS_FH(dir),
6370 .name = name,
6372 struct nfs4_secinfo_res res = {
6373 .flavors = flavors,
6375 struct rpc_message msg = {
6376 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6377 .rpc_argp = &args,
6378 .rpc_resp = &res,
6380 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
6381 struct rpc_cred *cred = NULL;
6383 if (use_integrity) {
6384 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
6385 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6386 msg.rpc_cred = cred;
6389 dprintk("NFS call secinfo %s\n", name->name);
6391 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6392 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6394 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6395 &res.seq_res, 0);
6396 dprintk("NFS reply secinfo: %d\n", status);
6398 if (cred)
6399 put_rpccred(cred);
6401 return status;
6404 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6405 struct nfs4_secinfo_flavors *flavors)
6407 struct nfs4_exception exception = { };
6408 int err;
6409 do {
6410 err = -NFS4ERR_WRONGSEC;
6412 /* try to use integrity protection with machine cred */
6413 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6414 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6417 * if unable to use integrity protection, or SECINFO with
6418 * integrity protection returns NFS4ERR_WRONGSEC (which is
6419 * disallowed by spec, but exists in deployed servers) use
6420 * the current filesystem's rpc_client and the user cred.
6422 if (err == -NFS4ERR_WRONGSEC)
6423 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6425 trace_nfs4_secinfo(dir, name, err);
6426 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6427 &exception);
6428 } while (exception.retry);
6429 return err;
6432 #ifdef CONFIG_NFS_V4_1
6434 * Check the exchange flags returned by the server for invalid flags, having
6435 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6436 * DS flags set.
6438 static int nfs4_check_cl_exchange_flags(u32 flags)
6440 if (flags & ~EXCHGID4_FLAG_MASK_R)
6441 goto out_inval;
6442 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6443 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6444 goto out_inval;
6445 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6446 goto out_inval;
6447 return NFS_OK;
6448 out_inval:
6449 return -NFS4ERR_INVAL;
6452 static bool
6453 nfs41_same_server_scope(struct nfs41_server_scope *a,
6454 struct nfs41_server_scope *b)
6456 if (a->server_scope_sz == b->server_scope_sz &&
6457 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6458 return true;
6460 return false;
6464 * nfs4_proc_bind_conn_to_session()
6466 * The 4.1 client currently uses the same TCP connection for the
6467 * fore and backchannel.
6469 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6471 int status;
6472 struct nfs41_bind_conn_to_session_res res;
6473 struct rpc_message msg = {
6474 .rpc_proc =
6475 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6476 .rpc_argp = clp,
6477 .rpc_resp = &res,
6478 .rpc_cred = cred,
6481 dprintk("--> %s\n", __func__);
6483 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6484 if (unlikely(res.session == NULL)) {
6485 status = -ENOMEM;
6486 goto out;
6489 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6490 trace_nfs4_bind_conn_to_session(clp, status);
6491 if (status == 0) {
6492 if (memcmp(res.session->sess_id.data,
6493 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6494 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6495 status = -EIO;
6496 goto out_session;
6498 if (res.dir != NFS4_CDFS4_BOTH) {
6499 dprintk("NFS: %s: Unexpected direction from server\n",
6500 __func__);
6501 status = -EIO;
6502 goto out_session;
6504 if (res.use_conn_in_rdma_mode) {
6505 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6506 __func__);
6507 status = -EIO;
6508 goto out_session;
6511 out_session:
6512 kfree(res.session);
6513 out:
6514 dprintk("<-- %s status= %d\n", __func__, status);
6515 return status;
6519 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6520 * and operations we'd like to see to enable certain features in the allow map
6522 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6523 .how = SP4_MACH_CRED,
6524 .enforce.u.words = {
6525 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6526 1 << (OP_EXCHANGE_ID - 32) |
6527 1 << (OP_CREATE_SESSION - 32) |
6528 1 << (OP_DESTROY_SESSION - 32) |
6529 1 << (OP_DESTROY_CLIENTID - 32)
6531 .allow.u.words = {
6532 [0] = 1 << (OP_CLOSE) |
6533 1 << (OP_LOCKU) |
6534 1 << (OP_COMMIT),
6535 [1] = 1 << (OP_SECINFO - 32) |
6536 1 << (OP_SECINFO_NO_NAME - 32) |
6537 1 << (OP_TEST_STATEID - 32) |
6538 1 << (OP_FREE_STATEID - 32) |
6539 1 << (OP_WRITE - 32)
6544 * Select the state protection mode for client `clp' given the server results
6545 * from exchange_id in `sp'.
6547 * Returns 0 on success, negative errno otherwise.
6549 static int nfs4_sp4_select_mode(struct nfs_client *clp,
6550 struct nfs41_state_protection *sp)
6552 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6553 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6554 1 << (OP_EXCHANGE_ID - 32) |
6555 1 << (OP_CREATE_SESSION - 32) |
6556 1 << (OP_DESTROY_SESSION - 32) |
6557 1 << (OP_DESTROY_CLIENTID - 32)
6559 unsigned int i;
6561 if (sp->how == SP4_MACH_CRED) {
6562 /* Print state protect result */
6563 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6564 for (i = 0; i <= LAST_NFS4_OP; i++) {
6565 if (test_bit(i, sp->enforce.u.longs))
6566 dfprintk(MOUNT, " enforce op %d\n", i);
6567 if (test_bit(i, sp->allow.u.longs))
6568 dfprintk(MOUNT, " allow op %d\n", i);
6571 /* make sure nothing is on enforce list that isn't supported */
6572 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6573 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6574 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6575 return -EINVAL;
6580 * Minimal mode - state operations are allowed to use machine
6581 * credential. Note this already happens by default, so the
6582 * client doesn't have to do anything more than the negotiation.
6584 * NOTE: we don't care if EXCHANGE_ID is in the list -
6585 * we're already using the machine cred for exchange_id
6586 * and will never use a different cred.
6588 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6589 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6590 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6591 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6592 dfprintk(MOUNT, "sp4_mach_cred:\n");
6593 dfprintk(MOUNT, " minimal mode enabled\n");
6594 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6595 } else {
6596 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6597 return -EINVAL;
6600 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6601 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6602 dfprintk(MOUNT, " cleanup mode enabled\n");
6603 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6606 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6607 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6608 dfprintk(MOUNT, " secinfo mode enabled\n");
6609 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6612 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6613 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6614 dfprintk(MOUNT, " stateid mode enabled\n");
6615 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6618 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6619 dfprintk(MOUNT, " write mode enabled\n");
6620 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6623 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6624 dfprintk(MOUNT, " commit mode enabled\n");
6625 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6629 return 0;
6633 * _nfs4_proc_exchange_id()
6635 * Wrapper for EXCHANGE_ID operation.
6637 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6638 u32 sp4_how)
6640 nfs4_verifier verifier;
6641 struct nfs41_exchange_id_args args = {
6642 .verifier = &verifier,
6643 .client = clp,
6644 #ifdef CONFIG_NFS_V4_1_MIGRATION
6645 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6646 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6647 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6648 #else
6649 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6650 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6651 #endif
6653 struct nfs41_exchange_id_res res = {
6656 int status;
6657 struct rpc_message msg = {
6658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6659 .rpc_argp = &args,
6660 .rpc_resp = &res,
6661 .rpc_cred = cred,
6664 nfs4_init_boot_verifier(clp, &verifier);
6665 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6666 sizeof(args.id));
6667 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6668 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6669 args.id_len, args.id);
6671 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
6672 GFP_NOFS);
6673 if (unlikely(res.server_owner == NULL)) {
6674 status = -ENOMEM;
6675 goto out;
6678 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
6679 GFP_NOFS);
6680 if (unlikely(res.server_scope == NULL)) {
6681 status = -ENOMEM;
6682 goto out_server_owner;
6685 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
6686 if (unlikely(res.impl_id == NULL)) {
6687 status = -ENOMEM;
6688 goto out_server_scope;
6691 switch (sp4_how) {
6692 case SP4_NONE:
6693 args.state_protect.how = SP4_NONE;
6694 break;
6696 case SP4_MACH_CRED:
6697 args.state_protect = nfs4_sp4_mach_cred_request;
6698 break;
6700 default:
6701 /* unsupported! */
6702 WARN_ON_ONCE(1);
6703 status = -EINVAL;
6704 goto out_server_scope;
6707 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6708 trace_nfs4_exchange_id(clp, status);
6709 if (status == 0)
6710 status = nfs4_check_cl_exchange_flags(res.flags);
6712 if (status == 0)
6713 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6715 if (status == 0) {
6716 clp->cl_clientid = res.clientid;
6717 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6718 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6719 clp->cl_seqid = res.seqid;
6721 kfree(clp->cl_serverowner);
6722 clp->cl_serverowner = res.server_owner;
6723 res.server_owner = NULL;
6725 /* use the most recent implementation id */
6726 kfree(clp->cl_implid);
6727 clp->cl_implid = res.impl_id;
6729 if (clp->cl_serverscope != NULL &&
6730 !nfs41_same_server_scope(clp->cl_serverscope,
6731 res.server_scope)) {
6732 dprintk("%s: server_scope mismatch detected\n",
6733 __func__);
6734 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
6735 kfree(clp->cl_serverscope);
6736 clp->cl_serverscope = NULL;
6739 if (clp->cl_serverscope == NULL) {
6740 clp->cl_serverscope = res.server_scope;
6741 goto out;
6743 } else
6744 kfree(res.impl_id);
6746 out_server_owner:
6747 kfree(res.server_owner);
6748 out_server_scope:
6749 kfree(res.server_scope);
6750 out:
6751 if (clp->cl_implid != NULL)
6752 dprintk("NFS reply exchange_id: Server Implementation ID: "
6753 "domain: %s, name: %s, date: %llu,%u\n",
6754 clp->cl_implid->domain, clp->cl_implid->name,
6755 clp->cl_implid->date.seconds,
6756 clp->cl_implid->date.nseconds);
6757 dprintk("NFS reply exchange_id: %d\n", status);
6758 return status;
6762 * nfs4_proc_exchange_id()
6764 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6766 * Since the clientid has expired, all compounds using sessions
6767 * associated with the stale clientid will be returning
6768 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6769 * be in some phase of session reset.
6771 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6773 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6775 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6776 int status;
6778 /* try SP4_MACH_CRED if krb5i/p */
6779 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6780 authflavor == RPC_AUTH_GSS_KRB5P) {
6781 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6782 if (!status)
6783 return 0;
6786 /* try SP4_NONE */
6787 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6790 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6791 struct rpc_cred *cred)
6793 struct rpc_message msg = {
6794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6795 .rpc_argp = clp,
6796 .rpc_cred = cred,
6798 int status;
6800 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6801 trace_nfs4_destroy_clientid(clp, status);
6802 if (status)
6803 dprintk("NFS: Got error %d from the server %s on "
6804 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6805 return status;
6808 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6809 struct rpc_cred *cred)
6811 unsigned int loop;
6812 int ret;
6814 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6815 ret = _nfs4_proc_destroy_clientid(clp, cred);
6816 switch (ret) {
6817 case -NFS4ERR_DELAY:
6818 case -NFS4ERR_CLIENTID_BUSY:
6819 ssleep(1);
6820 break;
6821 default:
6822 return ret;
6825 return 0;
6828 int nfs4_destroy_clientid(struct nfs_client *clp)
6830 struct rpc_cred *cred;
6831 int ret = 0;
6833 if (clp->cl_mvops->minor_version < 1)
6834 goto out;
6835 if (clp->cl_exchange_flags == 0)
6836 goto out;
6837 if (clp->cl_preserve_clid)
6838 goto out;
6839 cred = nfs4_get_clid_cred(clp);
6840 ret = nfs4_proc_destroy_clientid(clp, cred);
6841 if (cred)
6842 put_rpccred(cred);
6843 switch (ret) {
6844 case 0:
6845 case -NFS4ERR_STALE_CLIENTID:
6846 clp->cl_exchange_flags = 0;
6848 out:
6849 return ret;
6852 struct nfs4_get_lease_time_data {
6853 struct nfs4_get_lease_time_args *args;
6854 struct nfs4_get_lease_time_res *res;
6855 struct nfs_client *clp;
6858 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6859 void *calldata)
6861 struct nfs4_get_lease_time_data *data =
6862 (struct nfs4_get_lease_time_data *)calldata;
6864 dprintk("--> %s\n", __func__);
6865 /* just setup sequence, do not trigger session recovery
6866 since we're invoked within one */
6867 nfs41_setup_sequence(data->clp->cl_session,
6868 &data->args->la_seq_args,
6869 &data->res->lr_seq_res,
6870 task);
6871 dprintk("<-- %s\n", __func__);
6875 * Called from nfs4_state_manager thread for session setup, so don't recover
6876 * from sequence operation or clientid errors.
6878 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6880 struct nfs4_get_lease_time_data *data =
6881 (struct nfs4_get_lease_time_data *)calldata;
6883 dprintk("--> %s\n", __func__);
6884 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6885 return;
6886 switch (task->tk_status) {
6887 case -NFS4ERR_DELAY:
6888 case -NFS4ERR_GRACE:
6889 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6890 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6891 task->tk_status = 0;
6892 /* fall through */
6893 case -NFS4ERR_RETRY_UNCACHED_REP:
6894 rpc_restart_call_prepare(task);
6895 return;
6897 dprintk("<-- %s\n", __func__);
6900 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
6901 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6902 .rpc_call_done = nfs4_get_lease_time_done,
6905 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6907 struct rpc_task *task;
6908 struct nfs4_get_lease_time_args args;
6909 struct nfs4_get_lease_time_res res = {
6910 .lr_fsinfo = fsinfo,
6912 struct nfs4_get_lease_time_data data = {
6913 .args = &args,
6914 .res = &res,
6915 .clp = clp,
6917 struct rpc_message msg = {
6918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6919 .rpc_argp = &args,
6920 .rpc_resp = &res,
6922 struct rpc_task_setup task_setup = {
6923 .rpc_client = clp->cl_rpcclient,
6924 .rpc_message = &msg,
6925 .callback_ops = &nfs4_get_lease_time_ops,
6926 .callback_data = &data,
6927 .flags = RPC_TASK_TIMEOUT,
6929 int status;
6931 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
6932 nfs4_set_sequence_privileged(&args.la_seq_args);
6933 dprintk("--> %s\n", __func__);
6934 task = rpc_run_task(&task_setup);
6936 if (IS_ERR(task))
6937 status = PTR_ERR(task);
6938 else {
6939 status = task->tk_status;
6940 rpc_put_task(task);
6942 dprintk("<-- %s return %d\n", __func__, status);
6944 return status;
6948 * Initialize the values to be used by the client in CREATE_SESSION
6949 * If nfs4_init_session set the fore channel request and response sizes,
6950 * use them.
6952 * Set the back channel max_resp_sz_cached to zero to force the client to
6953 * always set csa_cachethis to FALSE because the current implementation
6954 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6956 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6958 unsigned int max_rqst_sz, max_resp_sz;
6960 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6961 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6963 /* Fore channel attributes */
6964 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6965 args->fc_attrs.max_resp_sz = max_resp_sz;
6966 args->fc_attrs.max_ops = NFS4_MAX_OPS;
6967 args->fc_attrs.max_reqs = max_session_slots;
6969 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
6970 "max_ops=%u max_reqs=%u\n",
6971 __func__,
6972 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
6973 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
6975 /* Back channel attributes */
6976 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6977 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6978 args->bc_attrs.max_resp_sz_cached = 0;
6979 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6980 args->bc_attrs.max_reqs = 1;
6982 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6983 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6984 __func__,
6985 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6986 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6987 args->bc_attrs.max_reqs);
6990 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6992 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6993 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6995 if (rcvd->max_resp_sz > sent->max_resp_sz)
6996 return -EINVAL;
6998 * Our requested max_ops is the minimum we need; we're not
6999 * prepared to break up compounds into smaller pieces than that.
7000 * So, no point even trying to continue if the server won't
7001 * cooperate:
7003 if (rcvd->max_ops < sent->max_ops)
7004 return -EINVAL;
7005 if (rcvd->max_reqs == 0)
7006 return -EINVAL;
7007 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7008 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7009 return 0;
7012 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7014 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7015 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
7017 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7018 return -EINVAL;
7019 if (rcvd->max_resp_sz < sent->max_resp_sz)
7020 return -EINVAL;
7021 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7022 return -EINVAL;
7023 /* These would render the backchannel useless: */
7024 if (rcvd->max_ops != sent->max_ops)
7025 return -EINVAL;
7026 if (rcvd->max_reqs != sent->max_reqs)
7027 return -EINVAL;
7028 return 0;
7031 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7032 struct nfs4_session *session)
7034 int ret;
7036 ret = nfs4_verify_fore_channel_attrs(args, session);
7037 if (ret)
7038 return ret;
7039 return nfs4_verify_back_channel_attrs(args, session);
7042 static int _nfs4_proc_create_session(struct nfs_client *clp,
7043 struct rpc_cred *cred)
7045 struct nfs4_session *session = clp->cl_session;
7046 struct nfs41_create_session_args args = {
7047 .client = clp,
7048 .cb_program = NFS4_CALLBACK,
7050 struct nfs41_create_session_res res = {
7051 .client = clp,
7053 struct rpc_message msg = {
7054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7055 .rpc_argp = &args,
7056 .rpc_resp = &res,
7057 .rpc_cred = cred,
7059 int status;
7061 nfs4_init_channel_attrs(&args);
7062 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7064 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7065 trace_nfs4_create_session(clp, status);
7067 if (!status) {
7068 /* Verify the session's negotiated channel_attrs values */
7069 status = nfs4_verify_channel_attrs(&args, session);
7070 /* Increment the clientid slot sequence id */
7071 clp->cl_seqid++;
7074 return status;
7078 * Issues a CREATE_SESSION operation to the server.
7079 * It is the responsibility of the caller to verify the session is
7080 * expired before calling this routine.
7082 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7084 int status;
7085 unsigned *ptr;
7086 struct nfs4_session *session = clp->cl_session;
7088 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7090 status = _nfs4_proc_create_session(clp, cred);
7091 if (status)
7092 goto out;
7094 /* Init or reset the session slot tables */
7095 status = nfs4_setup_session_slot_tables(session);
7096 dprintk("slot table setup returned %d\n", status);
7097 if (status)
7098 goto out;
7100 ptr = (unsigned *)&session->sess_id.data[0];
7101 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7102 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7103 out:
7104 dprintk("<-- %s\n", __func__);
7105 return status;
7109 * Issue the over-the-wire RPC DESTROY_SESSION.
7110 * The caller must serialize access to this routine.
7112 int nfs4_proc_destroy_session(struct nfs4_session *session,
7113 struct rpc_cred *cred)
7115 struct rpc_message msg = {
7116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7117 .rpc_argp = session,
7118 .rpc_cred = cred,
7120 int status = 0;
7122 dprintk("--> nfs4_proc_destroy_session\n");
7124 /* session is still being setup */
7125 if (session->clp->cl_cons_state != NFS_CS_READY)
7126 return status;
7128 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7129 trace_nfs4_destroy_session(session->clp, status);
7131 if (status)
7132 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
7133 "Session has been destroyed regardless...\n", status);
7135 dprintk("<-- nfs4_proc_destroy_session\n");
7136 return status;
7140 * Renew the cl_session lease.
7142 struct nfs4_sequence_data {
7143 struct nfs_client *clp;
7144 struct nfs4_sequence_args args;
7145 struct nfs4_sequence_res res;
7148 static void nfs41_sequence_release(void *data)
7150 struct nfs4_sequence_data *calldata = data;
7151 struct nfs_client *clp = calldata->clp;
7153 if (atomic_read(&clp->cl_count) > 1)
7154 nfs4_schedule_state_renewal(clp);
7155 nfs_put_client(clp);
7156 kfree(calldata);
7159 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7161 switch(task->tk_status) {
7162 case -NFS4ERR_DELAY:
7163 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7164 return -EAGAIN;
7165 default:
7166 nfs4_schedule_lease_recovery(clp);
7168 return 0;
7171 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
7173 struct nfs4_sequence_data *calldata = data;
7174 struct nfs_client *clp = calldata->clp;
7176 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7177 return;
7179 trace_nfs4_sequence(clp, task->tk_status);
7180 if (task->tk_status < 0) {
7181 dprintk("%s ERROR %d\n", __func__, task->tk_status);
7182 if (atomic_read(&clp->cl_count) == 1)
7183 goto out;
7185 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7186 rpc_restart_call_prepare(task);
7187 return;
7190 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
7191 out:
7192 dprintk("<-- %s\n", __func__);
7195 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7197 struct nfs4_sequence_data *calldata = data;
7198 struct nfs_client *clp = calldata->clp;
7199 struct nfs4_sequence_args *args;
7200 struct nfs4_sequence_res *res;
7202 args = task->tk_msg.rpc_argp;
7203 res = task->tk_msg.rpc_resp;
7205 nfs41_setup_sequence(clp->cl_session, args, res, task);
7208 static const struct rpc_call_ops nfs41_sequence_ops = {
7209 .rpc_call_done = nfs41_sequence_call_done,
7210 .rpc_call_prepare = nfs41_sequence_prepare,
7211 .rpc_release = nfs41_sequence_release,
7214 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7215 struct rpc_cred *cred,
7216 bool is_privileged)
7218 struct nfs4_sequence_data *calldata;
7219 struct rpc_message msg = {
7220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7221 .rpc_cred = cred,
7223 struct rpc_task_setup task_setup_data = {
7224 .rpc_client = clp->cl_rpcclient,
7225 .rpc_message = &msg,
7226 .callback_ops = &nfs41_sequence_ops,
7227 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7230 if (!atomic_inc_not_zero(&clp->cl_count))
7231 return ERR_PTR(-EIO);
7232 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7233 if (calldata == NULL) {
7234 nfs_put_client(clp);
7235 return ERR_PTR(-ENOMEM);
7237 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
7238 if (is_privileged)
7239 nfs4_set_sequence_privileged(&calldata->args);
7240 msg.rpc_argp = &calldata->args;
7241 msg.rpc_resp = &calldata->res;
7242 calldata->clp = clp;
7243 task_setup_data.callback_data = calldata;
7245 return rpc_run_task(&task_setup_data);
7248 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
7250 struct rpc_task *task;
7251 int ret = 0;
7253 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7254 return -EAGAIN;
7255 task = _nfs41_proc_sequence(clp, cred, false);
7256 if (IS_ERR(task))
7257 ret = PTR_ERR(task);
7258 else
7259 rpc_put_task_async(task);
7260 dprintk("<-- %s status=%d\n", __func__, ret);
7261 return ret;
7264 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7266 struct rpc_task *task;
7267 int ret;
7269 task = _nfs41_proc_sequence(clp, cred, true);
7270 if (IS_ERR(task)) {
7271 ret = PTR_ERR(task);
7272 goto out;
7274 ret = rpc_wait_for_completion_task(task);
7275 if (!ret) {
7276 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7278 if (task->tk_status == 0)
7279 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
7280 ret = task->tk_status;
7282 rpc_put_task(task);
7283 out:
7284 dprintk("<-- %s status=%d\n", __func__, ret);
7285 return ret;
7288 struct nfs4_reclaim_complete_data {
7289 struct nfs_client *clp;
7290 struct nfs41_reclaim_complete_args arg;
7291 struct nfs41_reclaim_complete_res res;
7294 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7296 struct nfs4_reclaim_complete_data *calldata = data;
7298 nfs41_setup_sequence(calldata->clp->cl_session,
7299 &calldata->arg.seq_args,
7300 &calldata->res.seq_res,
7301 task);
7304 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7306 switch(task->tk_status) {
7307 case 0:
7308 case -NFS4ERR_COMPLETE_ALREADY:
7309 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7310 break;
7311 case -NFS4ERR_DELAY:
7312 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7313 /* fall through */
7314 case -NFS4ERR_RETRY_UNCACHED_REP:
7315 return -EAGAIN;
7316 default:
7317 nfs4_schedule_lease_recovery(clp);
7319 return 0;
7322 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7324 struct nfs4_reclaim_complete_data *calldata = data;
7325 struct nfs_client *clp = calldata->clp;
7326 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7328 dprintk("--> %s\n", __func__);
7329 if (!nfs41_sequence_done(task, res))
7330 return;
7332 trace_nfs4_reclaim_complete(clp, task->tk_status);
7333 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7334 rpc_restart_call_prepare(task);
7335 return;
7337 dprintk("<-- %s\n", __func__);
7340 static void nfs4_free_reclaim_complete_data(void *data)
7342 struct nfs4_reclaim_complete_data *calldata = data;
7344 kfree(calldata);
7347 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7348 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7349 .rpc_call_done = nfs4_reclaim_complete_done,
7350 .rpc_release = nfs4_free_reclaim_complete_data,
7354 * Issue a global reclaim complete.
7356 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7357 struct rpc_cred *cred)
7359 struct nfs4_reclaim_complete_data *calldata;
7360 struct rpc_task *task;
7361 struct rpc_message msg = {
7362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
7363 .rpc_cred = cred,
7365 struct rpc_task_setup task_setup_data = {
7366 .rpc_client = clp->cl_rpcclient,
7367 .rpc_message = &msg,
7368 .callback_ops = &nfs4_reclaim_complete_call_ops,
7369 .flags = RPC_TASK_ASYNC,
7371 int status = -ENOMEM;
7373 dprintk("--> %s\n", __func__);
7374 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7375 if (calldata == NULL)
7376 goto out;
7377 calldata->clp = clp;
7378 calldata->arg.one_fs = 0;
7380 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
7381 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
7382 msg.rpc_argp = &calldata->arg;
7383 msg.rpc_resp = &calldata->res;
7384 task_setup_data.callback_data = calldata;
7385 task = rpc_run_task(&task_setup_data);
7386 if (IS_ERR(task)) {
7387 status = PTR_ERR(task);
7388 goto out;
7390 status = nfs4_wait_for_completion_rpc_task(task);
7391 if (status == 0)
7392 status = task->tk_status;
7393 rpc_put_task(task);
7394 return 0;
7395 out:
7396 dprintk("<-- %s status=%d\n", __func__, status);
7397 return status;
7400 static void
7401 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7403 struct nfs4_layoutget *lgp = calldata;
7404 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
7405 struct nfs4_session *session = nfs4_get_session(server);
7407 dprintk("--> %s\n", __func__);
7408 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7409 * right now covering the LAYOUTGET we are about to send.
7410 * However, that is not so catastrophic, and there seems
7411 * to be no way to prevent it completely.
7413 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
7414 &lgp->res.seq_res, task))
7415 return;
7416 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7417 NFS_I(lgp->args.inode)->layout,
7418 lgp->args.ctx->state)) {
7419 rpc_exit(task, NFS4_OK);
7423 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7425 struct nfs4_layoutget *lgp = calldata;
7426 struct inode *inode = lgp->args.inode;
7427 struct nfs_server *server = NFS_SERVER(inode);
7428 struct pnfs_layout_hdr *lo;
7429 struct nfs4_state *state = NULL;
7430 unsigned long timeo, now, giveup;
7432 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
7434 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
7435 goto out;
7437 switch (task->tk_status) {
7438 case 0:
7439 goto out;
7441 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7442 * (or clients) writing to the same RAID stripe
7444 case -NFS4ERR_LAYOUTTRYLATER:
7446 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7447 * existing layout before getting a new one).
7449 case -NFS4ERR_RECALLCONFLICT:
7450 timeo = rpc_get_timeout(task->tk_client);
7451 giveup = lgp->args.timestamp + timeo;
7452 now = jiffies;
7453 if (time_after(giveup, now)) {
7454 unsigned long delay;
7456 /* Delay for:
7457 * - Not less then NFS4_POLL_RETRY_MIN.
7458 * - One last time a jiffie before we give up
7459 * - exponential backoff (time_now minus start_attempt)
7461 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7462 min((giveup - now - 1),
7463 now - lgp->args.timestamp));
7465 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7466 __func__, delay);
7467 rpc_delay(task, delay);
7468 task->tk_status = 0;
7469 rpc_restart_call_prepare(task);
7470 goto out; /* Do not call nfs4_async_handle_error() */
7472 break;
7473 case -NFS4ERR_EXPIRED:
7474 case -NFS4ERR_BAD_STATEID:
7475 spin_lock(&inode->i_lock);
7476 lo = NFS_I(inode)->layout;
7477 if (!lo || list_empty(&lo->plh_segs)) {
7478 spin_unlock(&inode->i_lock);
7479 /* If the open stateid was bad, then recover it. */
7480 state = lgp->args.ctx->state;
7481 } else {
7482 LIST_HEAD(head);
7484 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7485 spin_unlock(&inode->i_lock);
7486 /* Mark the bad layout state as invalid, then
7487 * retry using the open stateid. */
7488 pnfs_free_lseg_list(&head);
7491 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7492 rpc_restart_call_prepare(task);
7493 out:
7494 dprintk("<-- %s\n", __func__);
7497 static size_t max_response_pages(struct nfs_server *server)
7499 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7500 return nfs_page_array_len(0, max_resp_sz);
7503 static void nfs4_free_pages(struct page **pages, size_t size)
7505 int i;
7507 if (!pages)
7508 return;
7510 for (i = 0; i < size; i++) {
7511 if (!pages[i])
7512 break;
7513 __free_page(pages[i]);
7515 kfree(pages);
7518 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7520 struct page **pages;
7521 int i;
7523 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7524 if (!pages) {
7525 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7526 return NULL;
7529 for (i = 0; i < size; i++) {
7530 pages[i] = alloc_page(gfp_flags);
7531 if (!pages[i]) {
7532 dprintk("%s: failed to allocate page\n", __func__);
7533 nfs4_free_pages(pages, size);
7534 return NULL;
7538 return pages;
7541 static void nfs4_layoutget_release(void *calldata)
7543 struct nfs4_layoutget *lgp = calldata;
7544 struct inode *inode = lgp->args.inode;
7545 struct nfs_server *server = NFS_SERVER(inode);
7546 size_t max_pages = max_response_pages(server);
7548 dprintk("--> %s\n", __func__);
7549 nfs4_free_pages(lgp->args.layout.pages, max_pages);
7550 pnfs_put_layout_hdr(NFS_I(inode)->layout);
7551 put_nfs_open_context(lgp->args.ctx);
7552 kfree(calldata);
7553 dprintk("<-- %s\n", __func__);
7556 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7557 .rpc_call_prepare = nfs4_layoutget_prepare,
7558 .rpc_call_done = nfs4_layoutget_done,
7559 .rpc_release = nfs4_layoutget_release,
7562 struct pnfs_layout_segment *
7563 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
7565 struct inode *inode = lgp->args.inode;
7566 struct nfs_server *server = NFS_SERVER(inode);
7567 size_t max_pages = max_response_pages(server);
7568 struct rpc_task *task;
7569 struct rpc_message msg = {
7570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7571 .rpc_argp = &lgp->args,
7572 .rpc_resp = &lgp->res,
7573 .rpc_cred = lgp->cred,
7575 struct rpc_task_setup task_setup_data = {
7576 .rpc_client = server->client,
7577 .rpc_message = &msg,
7578 .callback_ops = &nfs4_layoutget_call_ops,
7579 .callback_data = lgp,
7580 .flags = RPC_TASK_ASYNC,
7582 struct pnfs_layout_segment *lseg = NULL;
7583 int status = 0;
7585 dprintk("--> %s\n", __func__);
7587 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7588 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7590 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7591 if (!lgp->args.layout.pages) {
7592 nfs4_layoutget_release(lgp);
7593 return ERR_PTR(-ENOMEM);
7595 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
7596 lgp->args.timestamp = jiffies;
7598 lgp->res.layoutp = &lgp->args.layout;
7599 lgp->res.seq_res.sr_slot = NULL;
7600 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
7602 task = rpc_run_task(&task_setup_data);
7603 if (IS_ERR(task))
7604 return ERR_CAST(task);
7605 status = nfs4_wait_for_completion_rpc_task(task);
7606 if (status == 0)
7607 status = task->tk_status;
7608 trace_nfs4_layoutget(lgp->args.ctx,
7609 &lgp->args.range,
7610 &lgp->res.range,
7611 status);
7612 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7613 if (status == 0 && lgp->res.layoutp->len)
7614 lseg = pnfs_layout_process(lgp);
7615 rpc_put_task(task);
7616 dprintk("<-- %s status=%d\n", __func__, status);
7617 if (status)
7618 return ERR_PTR(status);
7619 return lseg;
7622 static void
7623 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7625 struct nfs4_layoutreturn *lrp = calldata;
7627 dprintk("--> %s\n", __func__);
7628 nfs41_setup_sequence(lrp->clp->cl_session,
7629 &lrp->args.seq_args,
7630 &lrp->res.seq_res,
7631 task);
7634 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7636 struct nfs4_layoutreturn *lrp = calldata;
7637 struct nfs_server *server;
7639 dprintk("--> %s\n", __func__);
7641 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
7642 return;
7644 server = NFS_SERVER(lrp->args.inode);
7645 switch (task->tk_status) {
7646 default:
7647 task->tk_status = 0;
7648 case 0:
7649 break;
7650 case -NFS4ERR_DELAY:
7651 if (nfs4_async_handle_error(task, server, NULL) != -EAGAIN)
7652 break;
7653 rpc_restart_call_prepare(task);
7654 return;
7656 dprintk("<-- %s\n", __func__);
7659 static void nfs4_layoutreturn_release(void *calldata)
7661 struct nfs4_layoutreturn *lrp = calldata;
7662 struct pnfs_layout_hdr *lo = lrp->args.layout;
7664 dprintk("--> %s\n", __func__);
7665 spin_lock(&lo->plh_inode->i_lock);
7666 if (lrp->res.lrs_present)
7667 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7668 lo->plh_block_lgets--;
7669 spin_unlock(&lo->plh_inode->i_lock);
7670 pnfs_put_layout_hdr(lrp->args.layout);
7671 kfree(calldata);
7672 dprintk("<-- %s\n", __func__);
7675 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7676 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7677 .rpc_call_done = nfs4_layoutreturn_done,
7678 .rpc_release = nfs4_layoutreturn_release,
7681 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7683 struct rpc_task *task;
7684 struct rpc_message msg = {
7685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7686 .rpc_argp = &lrp->args,
7687 .rpc_resp = &lrp->res,
7688 .rpc_cred = lrp->cred,
7690 struct rpc_task_setup task_setup_data = {
7691 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
7692 .rpc_message = &msg,
7693 .callback_ops = &nfs4_layoutreturn_call_ops,
7694 .callback_data = lrp,
7696 int status;
7698 dprintk("--> %s\n", __func__);
7699 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
7700 task = rpc_run_task(&task_setup_data);
7701 if (IS_ERR(task))
7702 return PTR_ERR(task);
7703 status = task->tk_status;
7704 trace_nfs4_layoutreturn(lrp->args.inode, status);
7705 dprintk("<-- %s status=%d\n", __func__, status);
7706 rpc_put_task(task);
7707 return status;
7711 * Retrieve the list of Data Server devices from the MDS.
7713 static int _nfs4_getdevicelist(struct nfs_server *server,
7714 const struct nfs_fh *fh,
7715 struct pnfs_devicelist *devlist)
7717 struct nfs4_getdevicelist_args args = {
7718 .fh = fh,
7719 .layoutclass = server->pnfs_curr_ld->id,
7721 struct nfs4_getdevicelist_res res = {
7722 .devlist = devlist,
7724 struct rpc_message msg = {
7725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7726 .rpc_argp = &args,
7727 .rpc_resp = &res,
7729 int status;
7731 dprintk("--> %s\n", __func__);
7732 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7733 &res.seq_res, 0);
7734 dprintk("<-- %s status=%d\n", __func__, status);
7735 return status;
7738 int nfs4_proc_getdevicelist(struct nfs_server *server,
7739 const struct nfs_fh *fh,
7740 struct pnfs_devicelist *devlist)
7742 struct nfs4_exception exception = { };
7743 int err;
7745 do {
7746 err = nfs4_handle_exception(server,
7747 _nfs4_getdevicelist(server, fh, devlist),
7748 &exception);
7749 } while (exception.retry);
7751 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7752 err, devlist->num_devs);
7754 return err;
7756 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7758 static int
7759 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
7760 struct pnfs_device *pdev,
7761 struct rpc_cred *cred)
7763 struct nfs4_getdeviceinfo_args args = {
7764 .pdev = pdev,
7766 struct nfs4_getdeviceinfo_res res = {
7767 .pdev = pdev,
7769 struct rpc_message msg = {
7770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7771 .rpc_argp = &args,
7772 .rpc_resp = &res,
7773 .rpc_cred = cred,
7775 int status;
7777 dprintk("--> %s\n", __func__);
7778 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
7779 dprintk("<-- %s status=%d\n", __func__, status);
7781 return status;
7784 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7785 struct pnfs_device *pdev,
7786 struct rpc_cred *cred)
7788 struct nfs4_exception exception = { };
7789 int err;
7791 do {
7792 err = nfs4_handle_exception(server,
7793 _nfs4_proc_getdeviceinfo(server, pdev, cred),
7794 &exception);
7795 } while (exception.retry);
7796 return err;
7798 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7800 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7802 struct nfs4_layoutcommit_data *data = calldata;
7803 struct nfs_server *server = NFS_SERVER(data->args.inode);
7804 struct nfs4_session *session = nfs4_get_session(server);
7806 nfs41_setup_sequence(session,
7807 &data->args.seq_args,
7808 &data->res.seq_res,
7809 task);
7812 static void
7813 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7815 struct nfs4_layoutcommit_data *data = calldata;
7816 struct nfs_server *server = NFS_SERVER(data->args.inode);
7818 if (!nfs41_sequence_done(task, &data->res.seq_res))
7819 return;
7821 switch (task->tk_status) { /* Just ignore these failures */
7822 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7823 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7824 case -NFS4ERR_BADLAYOUT: /* no layout */
7825 case -NFS4ERR_GRACE: /* loca_recalim always false */
7826 task->tk_status = 0;
7827 case 0:
7828 break;
7829 default:
7830 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7831 rpc_restart_call_prepare(task);
7832 return;
7837 static void nfs4_layoutcommit_release(void *calldata)
7839 struct nfs4_layoutcommit_data *data = calldata;
7841 pnfs_cleanup_layoutcommit(data);
7842 nfs_post_op_update_inode_force_wcc(data->args.inode,
7843 data->res.fattr);
7844 put_rpccred(data->cred);
7845 kfree(data);
7848 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7849 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7850 .rpc_call_done = nfs4_layoutcommit_done,
7851 .rpc_release = nfs4_layoutcommit_release,
7855 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
7857 struct rpc_message msg = {
7858 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7859 .rpc_argp = &data->args,
7860 .rpc_resp = &data->res,
7861 .rpc_cred = data->cred,
7863 struct rpc_task_setup task_setup_data = {
7864 .task = &data->task,
7865 .rpc_client = NFS_CLIENT(data->args.inode),
7866 .rpc_message = &msg,
7867 .callback_ops = &nfs4_layoutcommit_ops,
7868 .callback_data = data,
7869 .flags = RPC_TASK_ASYNC,
7871 struct rpc_task *task;
7872 int status = 0;
7874 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7875 "lbw: %llu inode %lu\n",
7876 data->task.tk_pid, sync,
7877 data->args.lastbytewritten,
7878 data->args.inode->i_ino);
7880 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
7881 task = rpc_run_task(&task_setup_data);
7882 if (IS_ERR(task))
7883 return PTR_ERR(task);
7884 if (sync == false)
7885 goto out;
7886 status = nfs4_wait_for_completion_rpc_task(task);
7887 if (status != 0)
7888 goto out;
7889 status = task->tk_status;
7890 trace_nfs4_layoutcommit(data->args.inode, status);
7891 out:
7892 dprintk("%s: status %d\n", __func__, status);
7893 rpc_put_task(task);
7894 return status;
7898 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7899 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7901 static int
7902 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7903 struct nfs_fsinfo *info,
7904 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7906 struct nfs41_secinfo_no_name_args args = {
7907 .style = SECINFO_STYLE_CURRENT_FH,
7909 struct nfs4_secinfo_res res = {
7910 .flavors = flavors,
7912 struct rpc_message msg = {
7913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7914 .rpc_argp = &args,
7915 .rpc_resp = &res,
7917 struct rpc_clnt *clnt = server->client;
7918 struct rpc_cred *cred = NULL;
7919 int status;
7921 if (use_integrity) {
7922 clnt = server->nfs_client->cl_rpcclient;
7923 cred = nfs4_get_clid_cred(server->nfs_client);
7924 msg.rpc_cred = cred;
7927 dprintk("--> %s\n", __func__);
7928 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7929 &res.seq_res, 0);
7930 dprintk("<-- %s status=%d\n", __func__, status);
7932 if (cred)
7933 put_rpccred(cred);
7935 return status;
7938 static int
7939 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7940 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7942 struct nfs4_exception exception = { };
7943 int err;
7944 do {
7945 /* first try using integrity protection */
7946 err = -NFS4ERR_WRONGSEC;
7948 /* try to use integrity protection with machine cred */
7949 if (_nfs4_is_integrity_protected(server->nfs_client))
7950 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7951 flavors, true);
7954 * if unable to use integrity protection, or SECINFO with
7955 * integrity protection returns NFS4ERR_WRONGSEC (which is
7956 * disallowed by spec, but exists in deployed servers) use
7957 * the current filesystem's rpc_client and the user cred.
7959 if (err == -NFS4ERR_WRONGSEC)
7960 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7961 flavors, false);
7963 switch (err) {
7964 case 0:
7965 case -NFS4ERR_WRONGSEC:
7966 case -ENOTSUPP:
7967 goto out;
7968 default:
7969 err = nfs4_handle_exception(server, err, &exception);
7971 } while (exception.retry);
7972 out:
7973 return err;
7976 static int
7977 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7978 struct nfs_fsinfo *info)
7980 int err;
7981 struct page *page;
7982 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
7983 struct nfs4_secinfo_flavors *flavors;
7984 struct nfs4_secinfo4 *secinfo;
7985 int i;
7987 page = alloc_page(GFP_KERNEL);
7988 if (!page) {
7989 err = -ENOMEM;
7990 goto out;
7993 flavors = page_address(page);
7994 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7997 * Fall back on "guess and check" method if
7998 * the server doesn't support SECINFO_NO_NAME
8000 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8001 err = nfs4_find_root_sec(server, fhandle, info);
8002 goto out_freepage;
8004 if (err)
8005 goto out_freepage;
8007 for (i = 0; i < flavors->num_flavors; i++) {
8008 secinfo = &flavors->flavors[i];
8010 switch (secinfo->flavor) {
8011 case RPC_AUTH_NULL:
8012 case RPC_AUTH_UNIX:
8013 case RPC_AUTH_GSS:
8014 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8015 &secinfo->flavor_info);
8016 break;
8017 default:
8018 flavor = RPC_AUTH_MAXFLAVOR;
8019 break;
8022 if (!nfs_auth_info_match(&server->auth_info, flavor))
8023 flavor = RPC_AUTH_MAXFLAVOR;
8025 if (flavor != RPC_AUTH_MAXFLAVOR) {
8026 err = nfs4_lookup_root_sec(server, fhandle,
8027 info, flavor);
8028 if (!err)
8029 break;
8033 if (flavor == RPC_AUTH_MAXFLAVOR)
8034 err = -EPERM;
8036 out_freepage:
8037 put_page(page);
8038 if (err == -EACCES)
8039 return -EPERM;
8040 out:
8041 return err;
8044 static int _nfs41_test_stateid(struct nfs_server *server,
8045 nfs4_stateid *stateid,
8046 struct rpc_cred *cred)
8048 int status;
8049 struct nfs41_test_stateid_args args = {
8050 .stateid = stateid,
8052 struct nfs41_test_stateid_res res;
8053 struct rpc_message msg = {
8054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8055 .rpc_argp = &args,
8056 .rpc_resp = &res,
8057 .rpc_cred = cred,
8059 struct rpc_clnt *rpc_client = server->client;
8061 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8062 &rpc_client, &msg);
8064 dprintk("NFS call test_stateid %p\n", stateid);
8065 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8066 nfs4_set_sequence_privileged(&args.seq_args);
8067 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8068 &args.seq_args, &res.seq_res);
8069 if (status != NFS_OK) {
8070 dprintk("NFS reply test_stateid: failed, %d\n", status);
8071 return status;
8073 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8074 return -res.status;
8078 * nfs41_test_stateid - perform a TEST_STATEID operation
8080 * @server: server / transport on which to perform the operation
8081 * @stateid: state ID to test
8082 * @cred: credential
8084 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8085 * Otherwise a negative NFS4ERR value is returned if the operation
8086 * failed or the state ID is not currently valid.
8088 static int nfs41_test_stateid(struct nfs_server *server,
8089 nfs4_stateid *stateid,
8090 struct rpc_cred *cred)
8092 struct nfs4_exception exception = { };
8093 int err;
8094 do {
8095 err = _nfs41_test_stateid(server, stateid, cred);
8096 if (err != -NFS4ERR_DELAY)
8097 break;
8098 nfs4_handle_exception(server, err, &exception);
8099 } while (exception.retry);
8100 return err;
8103 struct nfs_free_stateid_data {
8104 struct nfs_server *server;
8105 struct nfs41_free_stateid_args args;
8106 struct nfs41_free_stateid_res res;
8109 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8111 struct nfs_free_stateid_data *data = calldata;
8112 nfs41_setup_sequence(nfs4_get_session(data->server),
8113 &data->args.seq_args,
8114 &data->res.seq_res,
8115 task);
8118 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8120 struct nfs_free_stateid_data *data = calldata;
8122 nfs41_sequence_done(task, &data->res.seq_res);
8124 switch (task->tk_status) {
8125 case -NFS4ERR_DELAY:
8126 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
8127 rpc_restart_call_prepare(task);
8131 static void nfs41_free_stateid_release(void *calldata)
8133 kfree(calldata);
8136 static const struct rpc_call_ops nfs41_free_stateid_ops = {
8137 .rpc_call_prepare = nfs41_free_stateid_prepare,
8138 .rpc_call_done = nfs41_free_stateid_done,
8139 .rpc_release = nfs41_free_stateid_release,
8142 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8143 nfs4_stateid *stateid,
8144 struct rpc_cred *cred,
8145 bool privileged)
8147 struct rpc_message msg = {
8148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
8149 .rpc_cred = cred,
8151 struct rpc_task_setup task_setup = {
8152 .rpc_client = server->client,
8153 .rpc_message = &msg,
8154 .callback_ops = &nfs41_free_stateid_ops,
8155 .flags = RPC_TASK_ASYNC,
8157 struct nfs_free_stateid_data *data;
8159 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8160 &task_setup.rpc_client, &msg);
8162 dprintk("NFS call free_stateid %p\n", stateid);
8163 data = kmalloc(sizeof(*data), GFP_NOFS);
8164 if (!data)
8165 return ERR_PTR(-ENOMEM);
8166 data->server = server;
8167 nfs4_stateid_copy(&data->args.stateid, stateid);
8169 task_setup.callback_data = data;
8171 msg.rpc_argp = &data->args;
8172 msg.rpc_resp = &data->res;
8173 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
8174 if (privileged)
8175 nfs4_set_sequence_privileged(&data->args.seq_args);
8177 return rpc_run_task(&task_setup);
8181 * nfs41_free_stateid - perform a FREE_STATEID operation
8183 * @server: server / transport on which to perform the operation
8184 * @stateid: state ID to release
8185 * @cred: credential
8187 * Returns NFS_OK if the server freed "stateid". Otherwise a
8188 * negative NFS4ERR value is returned.
8190 static int nfs41_free_stateid(struct nfs_server *server,
8191 nfs4_stateid *stateid,
8192 struct rpc_cred *cred)
8194 struct rpc_task *task;
8195 int ret;
8197 task = _nfs41_free_stateid(server, stateid, cred, true);
8198 if (IS_ERR(task))
8199 return PTR_ERR(task);
8200 ret = rpc_wait_for_completion_task(task);
8201 if (!ret)
8202 ret = task->tk_status;
8203 rpc_put_task(task);
8204 return ret;
8207 static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8209 struct rpc_task *task;
8210 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
8212 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
8213 nfs4_free_lock_state(server, lsp);
8214 if (IS_ERR(task))
8215 return PTR_ERR(task);
8216 rpc_put_task(task);
8217 return 0;
8220 static bool nfs41_match_stateid(const nfs4_stateid *s1,
8221 const nfs4_stateid *s2)
8223 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
8224 return false;
8226 if (s1->seqid == s2->seqid)
8227 return true;
8228 if (s1->seqid == 0 || s2->seqid == 0)
8229 return true;
8231 return false;
8234 #endif /* CONFIG_NFS_V4_1 */
8236 static bool nfs4_match_stateid(const nfs4_stateid *s1,
8237 const nfs4_stateid *s2)
8239 return nfs4_stateid_match(s1, s2);
8243 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
8244 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8245 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8246 .recover_open = nfs4_open_reclaim,
8247 .recover_lock = nfs4_lock_reclaim,
8248 .establish_clid = nfs4_init_clientid,
8249 .detect_trunking = nfs40_discover_server_trunking,
8252 #if defined(CONFIG_NFS_V4_1)
8253 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
8254 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8255 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8256 .recover_open = nfs4_open_reclaim,
8257 .recover_lock = nfs4_lock_reclaim,
8258 .establish_clid = nfs41_init_clientid,
8259 .reclaim_complete = nfs41_proc_reclaim_complete,
8260 .detect_trunking = nfs41_discover_server_trunking,
8262 #endif /* CONFIG_NFS_V4_1 */
8264 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
8265 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8266 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8267 .recover_open = nfs40_open_expired,
8268 .recover_lock = nfs4_lock_expired,
8269 .establish_clid = nfs4_init_clientid,
8272 #if defined(CONFIG_NFS_V4_1)
8273 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
8274 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8275 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
8276 .recover_open = nfs41_open_expired,
8277 .recover_lock = nfs41_lock_expired,
8278 .establish_clid = nfs41_init_clientid,
8280 #endif /* CONFIG_NFS_V4_1 */
8282 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
8283 .sched_state_renewal = nfs4_proc_async_renew,
8284 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
8285 .renew_lease = nfs4_proc_renew,
8288 #if defined(CONFIG_NFS_V4_1)
8289 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
8290 .sched_state_renewal = nfs41_proc_async_sequence,
8291 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
8292 .renew_lease = nfs4_proc_sequence,
8294 #endif
8296 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
8297 .get_locations = _nfs40_proc_get_locations,
8298 .fsid_present = _nfs40_proc_fsid_present,
8301 #if defined(CONFIG_NFS_V4_1)
8302 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
8303 .get_locations = _nfs41_proc_get_locations,
8304 .fsid_present = _nfs41_proc_fsid_present,
8306 #endif /* CONFIG_NFS_V4_1 */
8308 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8309 .minor_version = 0,
8310 .init_caps = NFS_CAP_READDIRPLUS
8311 | NFS_CAP_ATOMIC_OPEN
8312 | NFS_CAP_CHANGE_ATTR
8313 | NFS_CAP_POSIX_LOCK,
8314 .init_client = nfs40_init_client,
8315 .shutdown_client = nfs40_shutdown_client,
8316 .match_stateid = nfs4_match_stateid,
8317 .find_root_sec = nfs4_find_root_sec,
8318 .free_lock_state = nfs4_release_lockowner,
8319 .call_sync_ops = &nfs40_call_sync_ops,
8320 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8321 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8322 .state_renewal_ops = &nfs40_state_renewal_ops,
8323 .mig_recovery_ops = &nfs40_mig_recovery_ops,
8326 #if defined(CONFIG_NFS_V4_1)
8327 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8328 .minor_version = 1,
8329 .init_caps = NFS_CAP_READDIRPLUS
8330 | NFS_CAP_ATOMIC_OPEN
8331 | NFS_CAP_CHANGE_ATTR
8332 | NFS_CAP_POSIX_LOCK
8333 | NFS_CAP_STATEID_NFSV41
8334 | NFS_CAP_ATOMIC_OPEN_V1,
8335 .init_client = nfs41_init_client,
8336 .shutdown_client = nfs41_shutdown_client,
8337 .match_stateid = nfs41_match_stateid,
8338 .find_root_sec = nfs41_find_root_sec,
8339 .free_lock_state = nfs41_free_lock_state,
8340 .call_sync_ops = &nfs41_call_sync_ops,
8341 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8342 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8343 .state_renewal_ops = &nfs41_state_renewal_ops,
8344 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8346 #endif
8348 #if defined(CONFIG_NFS_V4_2)
8349 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8350 .minor_version = 2,
8351 .init_caps = NFS_CAP_READDIRPLUS
8352 | NFS_CAP_ATOMIC_OPEN
8353 | NFS_CAP_CHANGE_ATTR
8354 | NFS_CAP_POSIX_LOCK
8355 | NFS_CAP_STATEID_NFSV41
8356 | NFS_CAP_ATOMIC_OPEN_V1,
8357 .init_client = nfs41_init_client,
8358 .shutdown_client = nfs41_shutdown_client,
8359 .match_stateid = nfs41_match_stateid,
8360 .find_root_sec = nfs41_find_root_sec,
8361 .free_lock_state = nfs41_free_lock_state,
8362 .call_sync_ops = &nfs41_call_sync_ops,
8363 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8364 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8365 .state_renewal_ops = &nfs41_state_renewal_ops,
8366 .mig_recovery_ops = &nfs41_mig_recovery_ops,
8368 #endif
8370 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8371 [0] = &nfs_v4_0_minor_ops,
8372 #if defined(CONFIG_NFS_V4_1)
8373 [1] = &nfs_v4_1_minor_ops,
8374 #endif
8375 #if defined(CONFIG_NFS_V4_2)
8376 [2] = &nfs_v4_2_minor_ops,
8377 #endif
8380 static const struct inode_operations nfs4_dir_inode_operations = {
8381 .create = nfs_create,
8382 .lookup = nfs_lookup,
8383 .atomic_open = nfs_atomic_open,
8384 .link = nfs_link,
8385 .unlink = nfs_unlink,
8386 .symlink = nfs_symlink,
8387 .mkdir = nfs_mkdir,
8388 .rmdir = nfs_rmdir,
8389 .mknod = nfs_mknod,
8390 .rename = nfs_rename,
8391 .permission = nfs_permission,
8392 .getattr = nfs_getattr,
8393 .setattr = nfs_setattr,
8394 .getxattr = generic_getxattr,
8395 .setxattr = generic_setxattr,
8396 .listxattr = generic_listxattr,
8397 .removexattr = generic_removexattr,
8400 static const struct inode_operations nfs4_file_inode_operations = {
8401 .permission = nfs_permission,
8402 .getattr = nfs_getattr,
8403 .setattr = nfs_setattr,
8404 .getxattr = generic_getxattr,
8405 .setxattr = generic_setxattr,
8406 .listxattr = generic_listxattr,
8407 .removexattr = generic_removexattr,
8410 const struct nfs_rpc_ops nfs_v4_clientops = {
8411 .version = 4, /* protocol version */
8412 .dentry_ops = &nfs4_dentry_operations,
8413 .dir_inode_ops = &nfs4_dir_inode_operations,
8414 .file_inode_ops = &nfs4_file_inode_operations,
8415 .file_ops = &nfs4_file_operations,
8416 .getroot = nfs4_proc_get_root,
8417 .submount = nfs4_submount,
8418 .try_mount = nfs4_try_mount,
8419 .getattr = nfs4_proc_getattr,
8420 .setattr = nfs4_proc_setattr,
8421 .lookup = nfs4_proc_lookup,
8422 .access = nfs4_proc_access,
8423 .readlink = nfs4_proc_readlink,
8424 .create = nfs4_proc_create,
8425 .remove = nfs4_proc_remove,
8426 .unlink_setup = nfs4_proc_unlink_setup,
8427 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
8428 .unlink_done = nfs4_proc_unlink_done,
8429 .rename_setup = nfs4_proc_rename_setup,
8430 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
8431 .rename_done = nfs4_proc_rename_done,
8432 .link = nfs4_proc_link,
8433 .symlink = nfs4_proc_symlink,
8434 .mkdir = nfs4_proc_mkdir,
8435 .rmdir = nfs4_proc_remove,
8436 .readdir = nfs4_proc_readdir,
8437 .mknod = nfs4_proc_mknod,
8438 .statfs = nfs4_proc_statfs,
8439 .fsinfo = nfs4_proc_fsinfo,
8440 .pathconf = nfs4_proc_pathconf,
8441 .set_capabilities = nfs4_server_capabilities,
8442 .decode_dirent = nfs4_decode_dirent,
8443 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
8444 .read_setup = nfs4_proc_read_setup,
8445 .read_done = nfs4_read_done,
8446 .write_setup = nfs4_proc_write_setup,
8447 .write_done = nfs4_write_done,
8448 .commit_setup = nfs4_proc_commit_setup,
8449 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
8450 .commit_done = nfs4_commit_done,
8451 .lock = nfs4_proc_lock,
8452 .clear_acl_cache = nfs4_zap_acl_attr,
8453 .close_context = nfs4_close_context,
8454 .open_context = nfs4_atomic_open,
8455 .have_delegation = nfs4_have_delegation,
8456 .return_delegation = nfs4_inode_return_delegation,
8457 .alloc_client = nfs4_alloc_client,
8458 .init_client = nfs4_init_client,
8459 .free_client = nfs4_free_client,
8460 .create_server = nfs4_create_server,
8461 .clone_server = nfs_clone_server,
8464 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8465 .prefix = XATTR_NAME_NFSV4_ACL,
8466 .list = nfs4_xattr_list_nfs4_acl,
8467 .get = nfs4_xattr_get_nfs4_acl,
8468 .set = nfs4_xattr_set_nfs4_acl,
8471 const struct xattr_handler *nfs4_xattr_handlers[] = {
8472 &nfs4_xattr_nfs4_acl_handler,
8473 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
8474 &nfs4_xattr_nfs4_label_handler,
8475 #endif
8476 NULL
8480 * Local variables:
8481 * c-basic-offset: 8
8482 * End: