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