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