blob: 3c1bfa15557116d0e78c87b191a5842bdeca9417 [file] [log] [blame]
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001/*
2 * Copyright (c) 2014 Christoph Hellwig.
3 */
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02004#include <linux/kmod.h>
5#include <linux/file.h>
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02006#include <linux/jhash.h>
7#include <linux/sched.h>
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02008#include <linux/sunrpc/addr.h>
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02009
10#include "pnfs.h"
11#include "netns.h"
Christoph Hellwig31ef83d2014-08-16 19:02:22 -050012#include "trace.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020013
14#define NFSDDBG_FACILITY NFSDDBG_PNFS
15
16struct nfs4_layout {
17 struct list_head lo_perstate;
18 struct nfs4_layout_stateid *lo_state;
19 struct nfsd4_layout_seg lo_seg;
20};
21
22static struct kmem_cache *nfs4_layout_cache;
23static struct kmem_cache *nfs4_layout_stateid_cache;
24
Christoph Hellwigc5c707f2014-09-23 12:38:48 +020025static struct nfsd4_callback_ops nfsd4_cb_layout_ops;
26static const struct lock_manager_operations nfsd4_layouts_lm_ops;
27
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020028const struct nfsd4_layout_ops *nfsd4_layout_ops[LAYOUT_TYPE_MAX] = {
Christoph Hellwig8650b8a2015-01-21 11:40:00 +010029 [LAYOUT_BLOCK_VOLUME] = &bl_layout_ops,
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020030};
31
32/* pNFS device ID to export fsid mapping */
33#define DEVID_HASH_BITS 8
34#define DEVID_HASH_SIZE (1 << DEVID_HASH_BITS)
35#define DEVID_HASH_MASK (DEVID_HASH_SIZE - 1)
36static u64 nfsd_devid_seq = 1;
37static struct list_head nfsd_devid_hash[DEVID_HASH_SIZE];
38static DEFINE_SPINLOCK(nfsd_devid_lock);
39
40static inline u32 devid_hashfn(u64 idx)
41{
42 return jhash_2words(idx, idx >> 32, 0) & DEVID_HASH_MASK;
43}
44
45static void
46nfsd4_alloc_devid_map(const struct svc_fh *fhp)
47{
48 const struct knfsd_fh *fh = &fhp->fh_handle;
49 size_t fsid_len = key_len(fh->fh_fsid_type);
50 struct nfsd4_deviceid_map *map, *old;
51 int i;
52
53 map = kzalloc(sizeof(*map) + fsid_len, GFP_KERNEL);
54 if (!map)
55 return;
56
57 map->fsid_type = fh->fh_fsid_type;
58 memcpy(&map->fsid, fh->fh_fsid, fsid_len);
59
60 spin_lock(&nfsd_devid_lock);
61 if (fhp->fh_export->ex_devid_map)
62 goto out_unlock;
63
64 for (i = 0; i < DEVID_HASH_SIZE; i++) {
65 list_for_each_entry(old, &nfsd_devid_hash[i], hash) {
66 if (old->fsid_type != fh->fh_fsid_type)
67 continue;
68 if (memcmp(old->fsid, fh->fh_fsid,
69 key_len(old->fsid_type)))
70 continue;
71
72 fhp->fh_export->ex_devid_map = old;
73 goto out_unlock;
74 }
75 }
76
77 map->idx = nfsd_devid_seq++;
78 list_add_tail_rcu(&map->hash, &nfsd_devid_hash[devid_hashfn(map->idx)]);
79 fhp->fh_export->ex_devid_map = map;
80 map = NULL;
81
82out_unlock:
83 spin_unlock(&nfsd_devid_lock);
84 kfree(map);
85}
86
87struct nfsd4_deviceid_map *
88nfsd4_find_devid_map(int idx)
89{
90 struct nfsd4_deviceid_map *map, *ret = NULL;
91
92 rcu_read_lock();
93 list_for_each_entry_rcu(map, &nfsd_devid_hash[devid_hashfn(idx)], hash)
94 if (map->idx == idx)
95 ret = map;
96 rcu_read_unlock();
97
98 return ret;
99}
100
101int
102nfsd4_set_deviceid(struct nfsd4_deviceid *id, const struct svc_fh *fhp,
103 u32 device_generation)
104{
105 if (!fhp->fh_export->ex_devid_map) {
106 nfsd4_alloc_devid_map(fhp);
107 if (!fhp->fh_export->ex_devid_map)
108 return -ENOMEM;
109 }
110
111 id->fsid_idx = fhp->fh_export->ex_devid_map->idx;
112 id->generation = device_generation;
113 id->pad = 0;
114 return 0;
115}
116
117void nfsd4_setup_layout_type(struct svc_export *exp)
118{
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100119 struct super_block *sb = exp->ex_path.mnt->mnt_sb;
120
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200121 if (exp->ex_flags & NFSEXP_NOPNFS)
122 return;
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100123
124 if (sb->s_export_op->get_uuid &&
125 sb->s_export_op->map_blocks &&
126 sb->s_export_op->commit_blocks)
127 exp->ex_layout_type = LAYOUT_BLOCK_VOLUME;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200128}
129
130static void
131nfsd4_free_layout_stateid(struct nfs4_stid *stid)
132{
133 struct nfs4_layout_stateid *ls = layoutstateid(stid);
134 struct nfs4_client *clp = ls->ls_stid.sc_client;
135 struct nfs4_file *fp = ls->ls_stid.sc_file;
136
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500137 trace_layoutstate_free(&ls->ls_stid.sc_stateid);
138
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200139 spin_lock(&clp->cl_lock);
140 list_del_init(&ls->ls_perclnt);
141 spin_unlock(&clp->cl_lock);
142
143 spin_lock(&fp->fi_lock);
144 list_del_init(&ls->ls_perfile);
145 spin_unlock(&fp->fi_lock);
146
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200147 vfs_setlease(ls->ls_file, F_UNLCK, NULL, (void **)&ls);
148 fput(ls->ls_file);
149
150 if (ls->ls_recalled)
151 atomic_dec(&ls->ls_stid.sc_file->fi_lo_recalls);
152
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200153 kmem_cache_free(nfs4_layout_stateid_cache, ls);
154}
155
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200156static int
157nfsd4_layout_setlease(struct nfs4_layout_stateid *ls)
158{
159 struct file_lock *fl;
160 int status;
161
162 fl = locks_alloc_lock();
163 if (!fl)
164 return -ENOMEM;
165 locks_init_lock(fl);
166 fl->fl_lmops = &nfsd4_layouts_lm_ops;
167 fl->fl_flags = FL_LAYOUT;
168 fl->fl_type = F_RDLCK;
169 fl->fl_end = OFFSET_MAX;
170 fl->fl_owner = ls;
171 fl->fl_pid = current->tgid;
172 fl->fl_file = ls->ls_file;
173
174 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl, NULL);
175 if (status) {
176 locks_free_lock(fl);
177 return status;
178 }
179 BUG_ON(fl != NULL);
180 return 0;
181}
182
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200183static struct nfs4_layout_stateid *
184nfsd4_alloc_layout_stateid(struct nfsd4_compound_state *cstate,
185 struct nfs4_stid *parent, u32 layout_type)
186{
187 struct nfs4_client *clp = cstate->clp;
188 struct nfs4_file *fp = parent->sc_file;
189 struct nfs4_layout_stateid *ls;
190 struct nfs4_stid *stp;
191
192 stp = nfs4_alloc_stid(cstate->clp, nfs4_layout_stateid_cache);
193 if (!stp)
194 return NULL;
195 stp->sc_free = nfsd4_free_layout_stateid;
196 get_nfs4_file(fp);
197 stp->sc_file = fp;
198
199 ls = layoutstateid(stp);
200 INIT_LIST_HEAD(&ls->ls_perclnt);
201 INIT_LIST_HEAD(&ls->ls_perfile);
202 spin_lock_init(&ls->ls_lock);
203 INIT_LIST_HEAD(&ls->ls_layouts);
204 ls->ls_layout_type = layout_type;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200205 nfsd4_init_cb(&ls->ls_recall, clp, &nfsd4_cb_layout_ops,
206 NFSPROC4_CLNT_CB_LAYOUT);
207
208 if (parent->sc_type == NFS4_DELEG_STID)
209 ls->ls_file = get_file(fp->fi_deleg_file);
210 else
211 ls->ls_file = find_any_file(fp);
212 BUG_ON(!ls->ls_file);
213
214 if (nfsd4_layout_setlease(ls)) {
215 put_nfs4_file(fp);
216 kmem_cache_free(nfs4_layout_stateid_cache, ls);
217 return NULL;
218 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200219
220 spin_lock(&clp->cl_lock);
221 stp->sc_type = NFS4_LAYOUT_STID;
222 list_add(&ls->ls_perclnt, &clp->cl_lo_states);
223 spin_unlock(&clp->cl_lock);
224
225 spin_lock(&fp->fi_lock);
226 list_add(&ls->ls_perfile, &fp->fi_lo_states);
227 spin_unlock(&fp->fi_lock);
228
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500229 trace_layoutstate_alloc(&ls->ls_stid.sc_stateid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200230 return ls;
231}
232
233__be32
234nfsd4_preprocess_layout_stateid(struct svc_rqst *rqstp,
235 struct nfsd4_compound_state *cstate, stateid_t *stateid,
236 bool create, u32 layout_type, struct nfs4_layout_stateid **lsp)
237{
238 struct nfs4_layout_stateid *ls;
239 struct nfs4_stid *stid;
240 unsigned char typemask = NFS4_LAYOUT_STID;
241 __be32 status;
242
243 if (create)
244 typemask |= (NFS4_OPEN_STID | NFS4_LOCK_STID | NFS4_DELEG_STID);
245
246 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &stid,
247 net_generic(SVC_NET(rqstp), nfsd_net_id));
248 if (status)
249 goto out;
250
251 if (!fh_match(&cstate->current_fh.fh_handle,
252 &stid->sc_file->fi_fhandle)) {
253 status = nfserr_bad_stateid;
254 goto out_put_stid;
255 }
256
257 if (stid->sc_type != NFS4_LAYOUT_STID) {
258 ls = nfsd4_alloc_layout_stateid(cstate, stid, layout_type);
259 nfs4_put_stid(stid);
260
261 status = nfserr_jukebox;
262 if (!ls)
263 goto out;
264 } else {
265 ls = container_of(stid, struct nfs4_layout_stateid, ls_stid);
266
267 status = nfserr_bad_stateid;
268 if (stateid->si_generation > stid->sc_stateid.si_generation)
269 goto out_put_stid;
270 if (layout_type != ls->ls_layout_type)
271 goto out_put_stid;
272 }
273
274 *lsp = ls;
275 return 0;
276
277out_put_stid:
278 nfs4_put_stid(stid);
279out:
280 return status;
281}
282
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200283static void
284nfsd4_recall_file_layout(struct nfs4_layout_stateid *ls)
285{
286 spin_lock(&ls->ls_lock);
287 if (ls->ls_recalled)
288 goto out_unlock;
289
290 ls->ls_recalled = true;
291 atomic_inc(&ls->ls_stid.sc_file->fi_lo_recalls);
292 if (list_empty(&ls->ls_layouts))
293 goto out_unlock;
294
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500295 trace_layout_recall(&ls->ls_stid.sc_stateid);
296
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200297 atomic_inc(&ls->ls_stid.sc_count);
298 update_stateid(&ls->ls_stid.sc_stateid);
299 memcpy(&ls->ls_recall_sid, &ls->ls_stid.sc_stateid, sizeof(stateid_t));
300 nfsd4_run_cb(&ls->ls_recall);
301
302out_unlock:
303 spin_unlock(&ls->ls_lock);
304}
305
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200306static inline u64
307layout_end(struct nfsd4_layout_seg *seg)
308{
309 u64 end = seg->offset + seg->length;
310 return end >= seg->offset ? end : NFS4_MAX_UINT64;
311}
312
313static void
314layout_update_len(struct nfsd4_layout_seg *lo, u64 end)
315{
316 if (end == NFS4_MAX_UINT64)
317 lo->length = NFS4_MAX_UINT64;
318 else
319 lo->length = end - lo->offset;
320}
321
322static bool
323layouts_overlapping(struct nfs4_layout *lo, struct nfsd4_layout_seg *s)
324{
325 if (s->iomode != IOMODE_ANY && s->iomode != lo->lo_seg.iomode)
326 return false;
327 if (layout_end(&lo->lo_seg) <= s->offset)
328 return false;
329 if (layout_end(s) <= lo->lo_seg.offset)
330 return false;
331 return true;
332}
333
334static bool
335layouts_try_merge(struct nfsd4_layout_seg *lo, struct nfsd4_layout_seg *new)
336{
337 if (lo->iomode != new->iomode)
338 return false;
339 if (layout_end(new) < lo->offset)
340 return false;
341 if (layout_end(lo) < new->offset)
342 return false;
343
344 lo->offset = min(lo->offset, new->offset);
345 layout_update_len(lo, max(layout_end(lo), layout_end(new)));
346 return true;
347}
348
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200349static __be32
350nfsd4_recall_conflict(struct nfs4_layout_stateid *ls)
351{
352 struct nfs4_file *fp = ls->ls_stid.sc_file;
353 struct nfs4_layout_stateid *l, *n;
354 __be32 nfserr = nfs_ok;
355
356 assert_spin_locked(&fp->fi_lock);
357
358 list_for_each_entry_safe(l, n, &fp->fi_lo_states, ls_perfile) {
359 if (l != ls) {
360 nfsd4_recall_file_layout(l);
361 nfserr = nfserr_recallconflict;
362 }
363 }
364
365 return nfserr;
366}
367
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200368__be32
369nfsd4_insert_layout(struct nfsd4_layoutget *lgp, struct nfs4_layout_stateid *ls)
370{
371 struct nfsd4_layout_seg *seg = &lgp->lg_seg;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200372 struct nfs4_file *fp = ls->ls_stid.sc_file;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200373 struct nfs4_layout *lp, *new = NULL;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200374 __be32 nfserr;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200375
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200376 spin_lock(&fp->fi_lock);
377 nfserr = nfsd4_recall_conflict(ls);
378 if (nfserr)
379 goto out;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200380 spin_lock(&ls->ls_lock);
381 list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
382 if (layouts_try_merge(&lp->lo_seg, seg))
383 goto done;
384 }
385 spin_unlock(&ls->ls_lock);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200386 spin_unlock(&fp->fi_lock);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200387
388 new = kmem_cache_alloc(nfs4_layout_cache, GFP_KERNEL);
389 if (!new)
390 return nfserr_jukebox;
391 memcpy(&new->lo_seg, seg, sizeof(lp->lo_seg));
392 new->lo_state = ls;
393
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200394 spin_lock(&fp->fi_lock);
395 nfserr = nfsd4_recall_conflict(ls);
396 if (nfserr)
397 goto out;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200398 spin_lock(&ls->ls_lock);
399 list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
400 if (layouts_try_merge(&lp->lo_seg, seg))
401 goto done;
402 }
403
404 atomic_inc(&ls->ls_stid.sc_count);
405 list_add_tail(&new->lo_perstate, &ls->ls_layouts);
406 new = NULL;
407done:
408 update_stateid(&ls->ls_stid.sc_stateid);
409 memcpy(&lgp->lg_sid, &ls->ls_stid.sc_stateid, sizeof(stateid_t));
410 spin_unlock(&ls->ls_lock);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200411out:
412 spin_unlock(&fp->fi_lock);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200413 if (new)
414 kmem_cache_free(nfs4_layout_cache, new);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200415 return nfserr;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200416}
417
418static void
419nfsd4_free_layouts(struct list_head *reaplist)
420{
421 while (!list_empty(reaplist)) {
422 struct nfs4_layout *lp = list_first_entry(reaplist,
423 struct nfs4_layout, lo_perstate);
424
425 list_del(&lp->lo_perstate);
426 nfs4_put_stid(&lp->lo_state->ls_stid);
427 kmem_cache_free(nfs4_layout_cache, lp);
428 }
429}
430
431static void
432nfsd4_return_file_layout(struct nfs4_layout *lp, struct nfsd4_layout_seg *seg,
433 struct list_head *reaplist)
434{
435 struct nfsd4_layout_seg *lo = &lp->lo_seg;
436 u64 end = layout_end(lo);
437
438 if (seg->offset <= lo->offset) {
439 if (layout_end(seg) >= end) {
440 list_move_tail(&lp->lo_perstate, reaplist);
441 return;
442 }
443 end = seg->offset;
444 } else {
445 /* retain the whole layout segment on a split. */
446 if (layout_end(seg) < end) {
447 dprintk("%s: split not supported\n", __func__);
448 return;
449 }
450
451 lo->offset = layout_end(seg);
452 }
453
454 layout_update_len(lo, end);
455}
456
457__be32
458nfsd4_return_file_layouts(struct svc_rqst *rqstp,
459 struct nfsd4_compound_state *cstate,
460 struct nfsd4_layoutreturn *lrp)
461{
462 struct nfs4_layout_stateid *ls;
463 struct nfs4_layout *lp, *n;
464 LIST_HEAD(reaplist);
465 __be32 nfserr;
466 int found = 0;
467
468 nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lrp->lr_sid,
469 false, lrp->lr_layout_type,
470 &ls);
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500471 if (nfserr) {
472 trace_layout_return_lookup_fail(&lrp->lr_sid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200473 return nfserr;
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500474 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200475
476 spin_lock(&ls->ls_lock);
477 list_for_each_entry_safe(lp, n, &ls->ls_layouts, lo_perstate) {
478 if (layouts_overlapping(lp, &lrp->lr_seg)) {
479 nfsd4_return_file_layout(lp, &lrp->lr_seg, &reaplist);
480 found++;
481 }
482 }
483 if (!list_empty(&ls->ls_layouts)) {
484 if (found) {
485 update_stateid(&ls->ls_stid.sc_stateid);
486 memcpy(&lrp->lr_sid, &ls->ls_stid.sc_stateid,
487 sizeof(stateid_t));
488 }
489 lrp->lrs_present = 1;
490 } else {
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500491 trace_layoutstate_unhash(&ls->ls_stid.sc_stateid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200492 nfs4_unhash_stid(&ls->ls_stid);
493 lrp->lrs_present = 0;
494 }
495 spin_unlock(&ls->ls_lock);
496
497 nfs4_put_stid(&ls->ls_stid);
498 nfsd4_free_layouts(&reaplist);
499 return nfs_ok;
500}
501
502__be32
503nfsd4_return_client_layouts(struct svc_rqst *rqstp,
504 struct nfsd4_compound_state *cstate,
505 struct nfsd4_layoutreturn *lrp)
506{
507 struct nfs4_layout_stateid *ls, *n;
508 struct nfs4_client *clp = cstate->clp;
509 struct nfs4_layout *lp, *t;
510 LIST_HEAD(reaplist);
511
512 lrp->lrs_present = 0;
513
514 spin_lock(&clp->cl_lock);
515 list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt) {
516 if (lrp->lr_return_type == RETURN_FSID &&
517 !fh_fsid_match(&ls->ls_stid.sc_file->fi_fhandle,
518 &cstate->current_fh.fh_handle))
519 continue;
520
521 spin_lock(&ls->ls_lock);
522 list_for_each_entry_safe(lp, t, &ls->ls_layouts, lo_perstate) {
523 if (lrp->lr_seg.iomode == IOMODE_ANY ||
524 lrp->lr_seg.iomode == lp->lo_seg.iomode)
525 list_move_tail(&lp->lo_perstate, &reaplist);
526 }
527 spin_unlock(&ls->ls_lock);
528 }
529 spin_unlock(&clp->cl_lock);
530
531 nfsd4_free_layouts(&reaplist);
532 return 0;
533}
534
535static void
536nfsd4_return_all_layouts(struct nfs4_layout_stateid *ls,
537 struct list_head *reaplist)
538{
539 spin_lock(&ls->ls_lock);
540 list_splice_init(&ls->ls_layouts, reaplist);
541 spin_unlock(&ls->ls_lock);
542}
543
544void
545nfsd4_return_all_client_layouts(struct nfs4_client *clp)
546{
547 struct nfs4_layout_stateid *ls, *n;
548 LIST_HEAD(reaplist);
549
550 spin_lock(&clp->cl_lock);
551 list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt)
552 nfsd4_return_all_layouts(ls, &reaplist);
553 spin_unlock(&clp->cl_lock);
554
555 nfsd4_free_layouts(&reaplist);
556}
557
558void
559nfsd4_return_all_file_layouts(struct nfs4_client *clp, struct nfs4_file *fp)
560{
561 struct nfs4_layout_stateid *ls, *n;
562 LIST_HEAD(reaplist);
563
564 spin_lock(&fp->fi_lock);
565 list_for_each_entry_safe(ls, n, &fp->fi_lo_states, ls_perfile) {
566 if (ls->ls_stid.sc_client == clp)
567 nfsd4_return_all_layouts(ls, &reaplist);
568 }
569 spin_unlock(&fp->fi_lock);
570
571 nfsd4_free_layouts(&reaplist);
572}
573
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200574static void
575nfsd4_cb_layout_fail(struct nfs4_layout_stateid *ls)
576{
577 struct nfs4_client *clp = ls->ls_stid.sc_client;
578 char addr_str[INET6_ADDRSTRLEN];
579 static char *envp[] = {
580 "HOME=/",
581 "TERM=linux",
582 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
583 NULL
584 };
585 char *argv[8];
586 int error;
587
588 rpc_ntop((struct sockaddr *)&clp->cl_addr, addr_str, sizeof(addr_str));
589
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500590 nfsd4_cb_layout_fail(ls);
591
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200592 printk(KERN_WARNING
593 "nfsd: client %s failed to respond to layout recall. "
594 " Fencing..\n", addr_str);
595
596 argv[0] = "/sbin/nfsd-recall-failed";
597 argv[1] = addr_str;
598 argv[2] = ls->ls_file->f_path.mnt->mnt_sb->s_id;
599 argv[3] = NULL;
600
601 error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
602 if (error) {
603 printk(KERN_ERR "nfsd: fence failed for client %s: %d!\n",
604 addr_str, error);
605 }
606}
607
608static int
609nfsd4_cb_layout_done(struct nfsd4_callback *cb, struct rpc_task *task)
610{
611 struct nfs4_layout_stateid *ls =
612 container_of(cb, struct nfs4_layout_stateid, ls_recall);
613 LIST_HEAD(reaplist);
614
615 switch (task->tk_status) {
616 case 0:
617 return 1;
618 case -NFS4ERR_NOMATCHING_LAYOUT:
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500619 trace_layout_recall_done(&ls->ls_stid.sc_stateid);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200620 task->tk_status = 0;
621 return 1;
622 case -NFS4ERR_DELAY:
623 /* Poll the client until it's done with the layout */
624 /* FIXME: cap number of retries.
625 * The pnfs standard states that we need to only expire
626 * the client after at-least "lease time" .eg lease-time * 2
627 * when failing to communicate a recall
628 */
629 rpc_delay(task, HZ/100); /* 10 mili-seconds */
630 return 0;
631 default:
632 /*
633 * Unknown error or non-responding client, we'll need to fence.
634 */
635 nfsd4_cb_layout_fail(ls);
636 return -1;
637 }
638}
639
640static void
641nfsd4_cb_layout_release(struct nfsd4_callback *cb)
642{
643 struct nfs4_layout_stateid *ls =
644 container_of(cb, struct nfs4_layout_stateid, ls_recall);
645 LIST_HEAD(reaplist);
646
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500647 trace_layout_recall_release(&ls->ls_stid.sc_stateid);
648
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200649 nfsd4_return_all_layouts(ls, &reaplist);
650 nfsd4_free_layouts(&reaplist);
651 nfs4_put_stid(&ls->ls_stid);
652}
653
654static struct nfsd4_callback_ops nfsd4_cb_layout_ops = {
655 .done = nfsd4_cb_layout_done,
656 .release = nfsd4_cb_layout_release,
657};
658
659static bool
660nfsd4_layout_lm_break(struct file_lock *fl)
661{
662 /*
663 * We don't want the locks code to timeout the lease for us;
664 * we'll remove it ourself if a layout isn't returned
665 * in time:
666 */
667 fl->fl_break_time = 0;
668 nfsd4_recall_file_layout(fl->fl_owner);
669 return false;
670}
671
672static int
673nfsd4_layout_lm_change(struct file_lock *onlist, int arg,
674 struct list_head *dispose)
675{
676 BUG_ON(!(arg & F_UNLCK));
677 return lease_modify(onlist, arg, dispose);
678}
679
680static const struct lock_manager_operations nfsd4_layouts_lm_ops = {
681 .lm_break = nfsd4_layout_lm_break,
682 .lm_change = nfsd4_layout_lm_change,
683};
684
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200685int
686nfsd4_init_pnfs(void)
687{
688 int i;
689
690 for (i = 0; i < DEVID_HASH_SIZE; i++)
691 INIT_LIST_HEAD(&nfsd_devid_hash[i]);
692
693 nfs4_layout_cache = kmem_cache_create("nfs4_layout",
694 sizeof(struct nfs4_layout), 0, 0, NULL);
695 if (!nfs4_layout_cache)
696 return -ENOMEM;
697
698 nfs4_layout_stateid_cache = kmem_cache_create("nfs4_layout_stateid",
699 sizeof(struct nfs4_layout_stateid), 0, 0, NULL);
700 if (!nfs4_layout_stateid_cache) {
701 kmem_cache_destroy(nfs4_layout_cache);
702 return -ENOMEM;
703 }
704 return 0;
705}
706
707void
708nfsd4_exit_pnfs(void)
709{
710 int i;
711
712 kmem_cache_destroy(nfs4_layout_cache);
713 kmem_cache_destroy(nfs4_layout_stateid_cache);
714
715 for (i = 0; i < DEVID_HASH_SIZE; i++) {
716 struct nfsd4_deviceid_map *map, *n;
717
718 list_for_each_entry_safe(map, n, &nfsd_devid_hash[i], hash)
719 kfree(map);
720 }
721}