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