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