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