]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/nfs/callback_proc.c
Merge tag 'mfd-arizona-v3.10-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-zesty-kernel.git] / fs / nfs / callback_proc.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/nfs/callback_proc.c
3 *
4 * Copyright (C) 2004 Trond Myklebust
5 *
6 * NFSv4 callback procedures
7 */
1da177e4
LT
8#include <linux/nfs4.h>
9#include <linux/nfs_fs.h>
5a0e3ad6 10#include <linux/slab.h>
2446ab60 11#include <linux/rcupdate.h>
4ce79717 12#include "nfs4_fs.h"
1da177e4
LT
13#include "callback.h"
14#include "delegation.h"
24c8dbbb 15#include "internal.h"
43f1b3da 16#include "pnfs.h"
73e39aaa 17#include "nfs4session.h"
1da177e4 18
1d98fe67 19#ifdef NFS_DEBUG
1da177e4 20#define NFSDBG_FACILITY NFSDBG_CALLBACK
1d98fe67 21#endif
c36fca52
AA
22
23__be32 nfs4_callback_getattr(struct cb_getattrargs *args,
24 struct cb_getattrres *res,
25 struct cb_process_state *cps)
1da177e4 26{
1da177e4
LT
27 struct nfs_delegation *delegation;
28 struct nfs_inode *nfsi;
29 struct inode *inode;
1d98fe67 30
c36fca52
AA
31 res->status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
32 if (!cps->clp) /* Always set for v4.0. Set in cb_sequence for v4.1 */
33 goto out;
34
1da177e4
LT
35 res->bitmap[0] = res->bitmap[1] = 0;
36 res->status = htonl(NFS4ERR_BADHANDLE);
1d98fe67 37
2446ab60 38 dprintk_rcu("NFS: GETATTR callback request from %s\n",
c36fca52 39 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1d98fe67 40
c36fca52 41 inode = nfs_delegation_find_inode(cps->clp, &args->fh);
1da177e4 42 if (inode == NULL)
c36fca52 43 goto out;
1da177e4 44 nfsi = NFS_I(inode);
761fe93c
TM
45 rcu_read_lock();
46 delegation = rcu_dereference(nfsi->delegation);
1da177e4
LT
47 if (delegation == NULL || (delegation->type & FMODE_WRITE) == 0)
48 goto out_iput;
49 res->size = i_size_read(inode);
beb2a5ec
TM
50 res->change_attr = delegation->change_attr;
51 if (nfsi->npages != 0)
52 res->change_attr++;
1da177e4
LT
53 res->ctime = inode->i_ctime;
54 res->mtime = inode->i_mtime;
55 res->bitmap[0] = (FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE) &
56 args->bitmap[0];
57 res->bitmap[1] = (FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY) &
58 args->bitmap[1];
59 res->status = 0;
60out_iput:
761fe93c 61 rcu_read_unlock();
1da177e4 62 iput(inode);
1da177e4 63out:
3110ff80 64 dprintk("%s: exit with status = %d\n", __func__, ntohl(res->status));
1da177e4
LT
65 return res->status;
66}
67
c36fca52
AA
68__be32 nfs4_callback_recall(struct cb_recallargs *args, void *dummy,
69 struct cb_process_state *cps)
1da177e4 70{
1da177e4 71 struct inode *inode;
e6f684f6 72 __be32 res;
1da177e4 73
c36fca52
AA
74 res = htonl(NFS4ERR_OP_NOT_IN_SESSION);
75 if (!cps->clp) /* Always set for v4.0. Set in cb_sequence for v4.1 */
1da177e4 76 goto out;
1d98fe67 77
2446ab60 78 dprintk_rcu("NFS: RECALL callback request from %s\n",
c36fca52
AA
79 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
80
81 res = htonl(NFS4ERR_BADHANDLE);
82 inode = nfs_delegation_find_inode(cps->clp, &args->fh);
83 if (inode == NULL)
84 goto out;
85 /* Set up a helper thread to actually return the delegation */
86 switch (nfs_async_inode_return_delegation(inode, &args->stateid)) {
87 case 0:
88 res = 0;
89 break;
90 case -ENOENT:
8e663f0e 91 res = htonl(NFS4ERR_BAD_STATEID);
c36fca52
AA
92 break;
93 default:
94 res = htonl(NFS4ERR_RESOURCE);
95 }
96 iput(inode);
1da177e4 97out:
3110ff80 98 dprintk("%s: exit with status = %d\n", __func__, ntohl(res));
1da177e4
LT
99 return res;
100}
d49433e1
BH
101
102#if defined(CONFIG_NFS_V4_1)
103
d36b7cf7
BH
104/*
105 * Lookup a layout by filehandle.
106 *
107 * Note: gets a refcount on the layout hdr and on its respective inode.
108 * Caller must put the layout hdr and the inode.
109 *
110 * TODO: keep track of all layouts (and delegations) in a hash table
111 * hashed by filehandle.
112 */
113static struct pnfs_layout_hdr * get_layout_by_fh_locked(struct nfs_client *clp, struct nfs_fh *fh)
43f1b3da 114{
6382a441 115 struct nfs_server *server;
43f1b3da 116 struct inode *ino;
d36b7cf7 117 struct pnfs_layout_hdr *lo;
43f1b3da 118
6382a441
WAA
119 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
120 list_for_each_entry(lo, &server->layouts, plh_layouts) {
d36b7cf7 121 if (nfs_compare_fh(fh, &NFS_I(lo->plh_inode)->fh))
6382a441
WAA
122 continue;
123 ino = igrab(lo->plh_inode);
124 if (!ino)
125 continue;
9c626381
TM
126 spin_lock(&ino->i_lock);
127 /* Is this layout in the process of being freed? */
128 if (NFS_I(ino)->layout != lo) {
129 spin_unlock(&ino->i_lock);
130 iput(ino);
131 continue;
132 }
70c3bd2b 133 pnfs_get_layout_hdr(lo);
9c626381 134 spin_unlock(&ino->i_lock);
d36b7cf7 135 return lo;
6382a441 136 }
43f1b3da 137 }
d36b7cf7
BH
138
139 return NULL;
140}
141
142static struct pnfs_layout_hdr * get_layout_by_fh(struct nfs_client *clp, struct nfs_fh *fh)
143{
144 struct pnfs_layout_hdr *lo;
145
146 spin_lock(&clp->cl_lock);
147 rcu_read_lock();
148 lo = get_layout_by_fh_locked(clp, fh);
6382a441 149 rcu_read_unlock();
43f1b3da 150 spin_unlock(&clp->cl_lock);
6382a441 151
d36b7cf7
BH
152 return lo;
153}
154
155static u32 initiate_file_draining(struct nfs_client *clp,
156 struct cb_layoutrecallargs *args)
157{
158 struct inode *ino;
159 struct pnfs_layout_hdr *lo;
160 u32 rv = NFS4ERR_NOMATCHING_LAYOUT;
161 LIST_HEAD(free_me_list);
162
163 lo = get_layout_by_fh(clp, &args->cbl_fh);
164 if (!lo)
43f1b3da
FI
165 return NFS4ERR_NOMATCHING_LAYOUT;
166
d36b7cf7 167 ino = lo->plh_inode;
43f1b3da
FI
168 spin_lock(&ino->i_lock);
169 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
49a85061 170 pnfs_mark_matching_lsegs_invalid(lo, &free_me_list,
778b5502 171 &args->cbl_range))
43f1b3da
FI
172 rv = NFS4ERR_DELAY;
173 else
174 rv = NFS4ERR_NOMATCHING_LAYOUT;
175 pnfs_set_layout_stateid(lo, &args->cbl_stateid, true);
176 spin_unlock(&ino->i_lock);
177 pnfs_free_lseg_list(&free_me_list);
70c3bd2b 178 pnfs_put_layout_hdr(lo);
43f1b3da
FI
179 iput(ino);
180 return rv;
181}
182
183static u32 initiate_bulk_draining(struct nfs_client *clp,
184 struct cb_layoutrecallargs *args)
185{
fd9a8d71 186 int stat;
6382a441 187
fd9a8d71
TM
188 if (args->cbl_recall_type == RETURN_FSID)
189 stat = pnfs_destroy_layouts_byfsid(clp, &args->cbl_fsid, true);
190 else
191 stat = pnfs_destroy_layouts_byclid(clp, true);
192 if (stat != 0)
193 return NFS4ERR_DELAY;
194 return NFS4ERR_NOMATCHING_LAYOUT;
43f1b3da
FI
195}
196
197static u32 do_callback_layoutrecall(struct nfs_client *clp,
198 struct cb_layoutrecallargs *args)
199{
0cb3284b 200 u32 res;
43f1b3da
FI
201
202 dprintk("%s enter, type=%i\n", __func__, args->cbl_recall_type);
43f1b3da
FI
203 if (args->cbl_recall_type == RETURN_FILE)
204 res = initiate_file_draining(clp, args);
205 else
206 res = initiate_bulk_draining(clp, args);
43f1b3da
FI
207 dprintk("%s returning %i\n", __func__, res);
208 return res;
209
210}
211
f2a62561
FI
212__be32 nfs4_callback_layoutrecall(struct cb_layoutrecallargs *args,
213 void *dummy, struct cb_process_state *cps)
214{
43f1b3da
FI
215 u32 res;
216
217 dprintk("%s: -->\n", __func__);
218
219 if (cps->clp)
220 res = do_callback_layoutrecall(cps->clp, args);
221 else
222 res = NFS4ERR_OP_NOT_IN_SESSION;
223
224 dprintk("%s: exit with status = %d\n", __func__, res);
225 return cpu_to_be32(res);
f2a62561
FI
226}
227
36840370
AB
228static void pnfs_recall_all_layouts(struct nfs_client *clp)
229{
230 struct cb_layoutrecallargs args;
231
232 /* Pretend we got a CB_LAYOUTRECALL(ALL) */
233 memset(&args, 0, sizeof(args));
234 args.cbl_recall_type = RETURN_ALL;
235 /* FIXME we ignore errors, what should we do? */
236 do_callback_layoutrecall(clp, &args);
237}
238
1be5683b
ME
239__be32 nfs4_callback_devicenotify(struct cb_devicenotifyargs *args,
240 void *dummy, struct cb_process_state *cps)
241{
242 int i;
243 __be32 res = 0;
244 struct nfs_client *clp = cps->clp;
245 struct nfs_server *server = NULL;
246
247 dprintk("%s: -->\n", __func__);
248
249 if (!clp) {
250 res = cpu_to_be32(NFS4ERR_OP_NOT_IN_SESSION);
251 goto out;
252 }
253
254 for (i = 0; i < args->ndevs; i++) {
255 struct cb_devicenotifyitem *dev = &args->devs[i];
256
257 if (!server ||
258 server->pnfs_curr_ld->id != dev->cbd_layout_type) {
259 rcu_read_lock();
260 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
261 if (server->pnfs_curr_ld &&
262 server->pnfs_curr_ld->id == dev->cbd_layout_type) {
263 rcu_read_unlock();
264 goto found;
265 }
266 rcu_read_unlock();
267 dprintk("%s: layout type %u not found\n",
268 __func__, dev->cbd_layout_type);
269 continue;
270 }
271
272 found:
273 if (dev->cbd_notify_type == NOTIFY_DEVICEID4_CHANGE)
274 dprintk("%s: NOTIFY_DEVICEID4_CHANGE not supported, "
275 "deleting instead\n", __func__);
35c8bb54 276 nfs4_delete_deviceid(server->pnfs_curr_ld, clp, &dev->cbd_dev_id);
1be5683b
ME
277 }
278
279out:
280 kfree(args->devs);
281 dprintk("%s: exit with status = %u\n",
282 __func__, be32_to_cpu(res));
283 return res;
284}
285
68f3f901
RL
286/*
287 * Validate the sequenceID sent by the server.
288 * Return success if the sequenceID is one more than what we last saw on
289 * this slot, accounting for wraparound. Increments the slot's sequence.
290 *
4911096f
AA
291 * We don't yet implement a duplicate request cache, instead we set the
292 * back channel ca_maxresponsesize_cached to zero. This is OK for now
68f3f901
RL
293 * since we only currently implement idempotent callbacks anyway.
294 *
295 * We have a single slot backchannel at this time, so we don't bother
296 * checking the used_slots bit array on the table. The lower layer guarantees
297 * a single outstanding callback request at a time.
298 */
9733f0d9 299static __be32
b2f28bd7 300validate_seqid(struct nfs4_slot_table *tbl, struct cb_sequenceargs * args)
68f3f901
RL
301{
302 struct nfs4_slot *slot;
303
304 dprintk("%s enter. slotid %d seqid %d\n",
b2f28bd7 305 __func__, args->csa_slotid, args->csa_sequenceid);
68f3f901 306
61f2e510 307 if (args->csa_slotid >= NFS41_BC_MAX_CALLBACKS)
68f3f901
RL
308 return htonl(NFS4ERR_BADSLOT);
309
b2f28bd7 310 slot = tbl->slots + args->csa_slotid;
68f3f901
RL
311 dprintk("%s slot table seqid: %d\n", __func__, slot->seq_nr);
312
313 /* Normal */
b2f28bd7 314 if (likely(args->csa_sequenceid == slot->seq_nr + 1)) {
68f3f901 315 slot->seq_nr++;
55a67399 316 goto out_ok;
68f3f901
RL
317 }
318
319 /* Replay */
b2f28bd7 320 if (args->csa_sequenceid == slot->seq_nr) {
4911096f 321 dprintk("%s seqid %d is a replay\n",
b2f28bd7 322 __func__, args->csa_sequenceid);
4911096f
AA
323 /* Signal process_op to set this error on next op */
324 if (args->csa_cachethis == 0)
325 return htonl(NFS4ERR_RETRY_UNCACHED_REP);
326
327 /* The ca_maxresponsesize_cached is 0 with no DRC */
328 else if (args->csa_cachethis == 1)
329 return htonl(NFS4ERR_REP_TOO_BIG_TO_CACHE);
68f3f901
RL
330 }
331
332 /* Wraparound */
b2f28bd7 333 if (args->csa_sequenceid == 1 && (slot->seq_nr + 1) == 0) {
68f3f901 334 slot->seq_nr = 1;
55a67399 335 goto out_ok;
68f3f901
RL
336 }
337
338 /* Misordered request */
339 return htonl(NFS4ERR_SEQ_MISORDERED);
55a67399
TM
340out_ok:
341 tbl->highest_used_slotid = args->csa_slotid;
342 return htonl(NFS4_OK);
68f3f901
RL
343}
344
a7989c3e
MS
345/*
346 * For each referring call triple, check the session's slot table for
347 * a match. If the slot is in use and the sequence numbers match, the
348 * client is still waiting for a response to the original request.
349 */
350static bool referring_call_exists(struct nfs_client *clp,
351 uint32_t nrclists,
352 struct referring_call_list *rclists)
353{
354 bool status = 0;
355 int i, j;
356 struct nfs4_session *session;
357 struct nfs4_slot_table *tbl;
358 struct referring_call_list *rclist;
359 struct referring_call *ref;
360
361 /*
362 * XXX When client trunking is implemented, this becomes
363 * a session lookup from within the loop
364 */
365 session = clp->cl_session;
366 tbl = &session->fc_slot_table;
367
368 for (i = 0; i < nrclists; i++) {
369 rclist = &rclists[i];
370 if (memcmp(session->sess_id.data,
371 rclist->rcl_sessionid.data,
372 NFS4_MAX_SESSIONID_LEN) != 0)
373 continue;
374
375 for (j = 0; j < rclist->rcl_nrefcalls; j++) {
376 ref = &rclist->rcl_refcalls[j];
377
378 dprintk("%s: sessionid %x:%x:%x:%x sequenceid %u "
379 "slotid %u\n", __func__,
380 ((u32 *)&rclist->rcl_sessionid.data)[0],
381 ((u32 *)&rclist->rcl_sessionid.data)[1],
382 ((u32 *)&rclist->rcl_sessionid.data)[2],
383 ((u32 *)&rclist->rcl_sessionid.data)[3],
384 ref->rc_sequenceid, ref->rc_slotid);
385
386 spin_lock(&tbl->slot_tbl_lock);
387 status = (test_bit(ref->rc_slotid, tbl->used_slots) &&
388 tbl->slots[ref->rc_slotid].seq_nr ==
389 ref->rc_sequenceid);
390 spin_unlock(&tbl->slot_tbl_lock);
391 if (status)
392 goto out;
393 }
394 }
395
396out:
397 return status;
398}
399
9733f0d9 400__be32 nfs4_callback_sequence(struct cb_sequenceargs *args,
c36fca52
AA
401 struct cb_sequenceres *res,
402 struct cb_process_state *cps)
d49433e1 403{
55a67399 404 struct nfs4_slot_table *tbl;
68f3f901 405 struct nfs_client *clp;
9733f0d9 406 int i;
778be232 407 __be32 status = htonl(NFS4ERR_BADSESSION);
d49433e1 408
c7add9a9 409 clp = nfs4_find_client_sessionid(cps->net, args->csa_addr, &args->csa_sessionid);
68f3f901
RL
410 if (clp == NULL)
411 goto out;
412
55a67399
TM
413 tbl = &clp->cl_session->bc_slot_table;
414
415 spin_lock(&tbl->slot_tbl_lock);
42acd021 416 /* state manager is resetting the session */
774d5f14 417 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) {
55a67399
TM
418 spin_unlock(&tbl->slot_tbl_lock);
419 status = htonl(NFS4ERR_DELAY);
910ac68a
TM
420 /* Return NFS4ERR_BADSESSION if we're draining the session
421 * in order to reset it.
422 */
423 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
424 status = htonl(NFS4ERR_BADSESSION);
42acd021
AA
425 goto out;
426 }
427
b2f28bd7 428 status = validate_seqid(&clp->cl_session->bc_slot_table, args);
55a67399 429 spin_unlock(&tbl->slot_tbl_lock);
68f3f901 430 if (status)
c36fca52 431 goto out;
68f3f901 432
55a67399
TM
433 cps->slotid = args->csa_slotid;
434
72ce2b3c
MS
435 /*
436 * Check for pending referring calls. If a match is found, a
437 * related callback was received before the response to the original
438 * call.
439 */
440 if (referring_call_exists(clp, args->csa_nrclists, args->csa_rclists)) {
441 status = htonl(NFS4ERR_DELAY);
c36fca52 442 goto out;
72ce2b3c
MS
443 }
444
d49433e1
BH
445 memcpy(&res->csr_sessionid, &args->csa_sessionid,
446 sizeof(res->csr_sessionid));
447 res->csr_sequenceid = args->csa_sequenceid;
448 res->csr_slotid = args->csa_slotid;
449 res->csr_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
450 res->csr_target_highestslotid = NFS41_BC_MAX_CALLBACKS - 1;
451
68f3f901 452out:
2c4cdf8f 453 cps->clp = clp; /* put in nfs4_callback_compound */
72ce2b3c
MS
454 for (i = 0; i < args->csa_nrclists; i++)
455 kfree(args->csa_rclists[i].rcl_refcalls);
456 kfree(args->csa_rclists);
457
c36fca52
AA
458 if (status == htonl(NFS4ERR_RETRY_UNCACHED_REP)) {
459 cps->drc_status = status;
460 status = 0;
461 } else
4911096f 462 res->csr_status = status;
c36fca52 463
4911096f
AA
464 dprintk("%s: exit with status = %d res->csr_status %d\n", __func__,
465 ntohl(status), ntohl(res->csr_status));
466 return status;
d49433e1
BH
467}
468
36840370
AB
469static bool
470validate_bitmap_values(unsigned long mask)
471{
472 return (mask & ~RCA4_TYPE_MASK_ALL) == 0;
473}
474
c36fca52
AA
475__be32 nfs4_callback_recallany(struct cb_recallanyargs *args, void *dummy,
476 struct cb_process_state *cps)
31f09607 477{
9733f0d9 478 __be32 status;
31f09607
AB
479 fmode_t flags = 0;
480
36840370 481 status = cpu_to_be32(NFS4ERR_OP_NOT_IN_SESSION);
c36fca52 482 if (!cps->clp) /* set in cb_sequence */
31f09607
AB
483 goto out;
484
2446ab60 485 dprintk_rcu("NFS: RECALL_ANY callback request from %s\n",
c36fca52 486 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR));
31f09607 487
36840370
AB
488 status = cpu_to_be32(NFS4ERR_INVAL);
489 if (!validate_bitmap_values(args->craa_type_mask))
490 goto out;
491
492 status = cpu_to_be32(NFS4_OK);
31f09607
AB
493 if (test_bit(RCA4_TYPE_MASK_RDATA_DLG, (const unsigned long *)
494 &args->craa_type_mask))
495 flags = FMODE_READ;
496 if (test_bit(RCA4_TYPE_MASK_WDATA_DLG, (const unsigned long *)
497 &args->craa_type_mask))
498 flags |= FMODE_WRITE;
36840370
AB
499 if (test_bit(RCA4_TYPE_MASK_FILE_LAYOUT, (const unsigned long *)
500 &args->craa_type_mask))
501 pnfs_recall_all_layouts(cps->clp);
31f09607 502 if (flags)
826e0013 503 nfs_expire_unused_delegation_types(cps->clp, flags);
31f09607
AB
504out:
505 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
506 return status;
507}
b9efa1b2
AA
508
509/* Reduce the fore channel's max_slots to the target value */
c36fca52
AA
510__be32 nfs4_callback_recallslot(struct cb_recallslotargs *args, void *dummy,
511 struct cb_process_state *cps)
b9efa1b2 512{
b9efa1b2 513 struct nfs4_slot_table *fc_tbl;
9733f0d9 514 __be32 status;
b9efa1b2
AA
515
516 status = htonl(NFS4ERR_OP_NOT_IN_SESSION);
c36fca52 517 if (!cps->clp) /* set in cb_sequence */
b9efa1b2
AA
518 goto out;
519
d5fb4ce3 520 dprintk_rcu("NFS: CB_RECALL_SLOT request from %s target highest slotid %d\n",
c36fca52 521 rpc_peeraddr2str(cps->clp->cl_rpcclient, RPC_DISPLAY_ADDR),
d5fb4ce3 522 args->crsa_target_highest_slotid);
b9efa1b2 523
c36fca52 524 fc_tbl = &cps->clp->cl_session->fc_slot_table;
b9efa1b2 525
bae0ac0e 526 status = htonl(NFS4_OK);
b9efa1b2 527
d5fb4ce3 528 nfs41_set_target_slotid(fc_tbl, args->crsa_target_highest_slotid);
ac074835 529 nfs41_server_notify_target_slotid_update(cps->clp);
b9efa1b2
AA
530out:
531 dprintk("%s: exit with status = %d\n", __func__, ntohl(status));
532 return status;
533}
d49433e1 534#endif /* CONFIG_NFS_V4_1 */