blob: e06a4ae5f3ad2fcdd5e402db485ed56df0ba4833 [file] [log] [blame]
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02001/*
2 * Copyright (c) 2014 Christoph Hellwig.
3 */
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +01004#include <linux/blkdev.h>
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02005#include <linux/kmod.h>
6#include <linux/file.h>
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02007#include <linux/jhash.h>
8#include <linux/sched.h>
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02009#include <linux/sunrpc/addr.h>
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020010
11#include "pnfs.h"
12#include "netns.h"
Christoph Hellwig31ef83d2014-08-16 19:02:22 -050013#include "trace.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020014
15#define NFSDDBG_FACILITY NFSDDBG_PNFS
16
17struct nfs4_layout {
18 struct list_head lo_perstate;
19 struct nfs4_layout_stateid *lo_state;
20 struct nfsd4_layout_seg lo_seg;
21};
22
23static struct kmem_cache *nfs4_layout_cache;
24static struct kmem_cache *nfs4_layout_stateid_cache;
25
Julia Lawallc4cb8972015-11-21 22:57:39 +010026static const struct nfsd4_callback_ops nfsd4_cb_layout_ops;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +020027static const struct lock_manager_operations nfsd4_layouts_lm_ops;
28
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020029const struct nfsd4_layout_ops *nfsd4_layout_ops[LAYOUT_TYPE_MAX] = {
Tom Haynes9b9960a2016-06-14 13:41:28 -070030#ifdef CONFIG_NFSD_FLEXFILELAYOUT
31 [LAYOUT_FLEX_FILES] = &ff_layout_ops,
32#endif
Christoph Hellwig81c39322016-03-04 20:46:16 +010033#ifdef CONFIG_NFSD_BLOCKLAYOUT
Christoph Hellwig8650b8a2015-01-21 11:40:00 +010034 [LAYOUT_BLOCK_VOLUME] = &bl_layout_ops,
Christoph Hellwig81c39322016-03-04 20:46:16 +010035#endif
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +010036#ifdef CONFIG_NFSD_SCSILAYOUT
37 [LAYOUT_SCSI] = &scsi_layout_ops,
38#endif
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020039};
40
41/* pNFS device ID to export fsid mapping */
42#define DEVID_HASH_BITS 8
43#define DEVID_HASH_SIZE (1 << DEVID_HASH_BITS)
44#define DEVID_HASH_MASK (DEVID_HASH_SIZE - 1)
45static u64 nfsd_devid_seq = 1;
46static struct list_head nfsd_devid_hash[DEVID_HASH_SIZE];
47static DEFINE_SPINLOCK(nfsd_devid_lock);
48
49static inline u32 devid_hashfn(u64 idx)
50{
51 return jhash_2words(idx, idx >> 32, 0) & DEVID_HASH_MASK;
52}
53
54static void
55nfsd4_alloc_devid_map(const struct svc_fh *fhp)
56{
57 const struct knfsd_fh *fh = &fhp->fh_handle;
58 size_t fsid_len = key_len(fh->fh_fsid_type);
59 struct nfsd4_deviceid_map *map, *old;
60 int i;
61
62 map = kzalloc(sizeof(*map) + fsid_len, GFP_KERNEL);
63 if (!map)
64 return;
65
66 map->fsid_type = fh->fh_fsid_type;
67 memcpy(&map->fsid, fh->fh_fsid, fsid_len);
68
69 spin_lock(&nfsd_devid_lock);
70 if (fhp->fh_export->ex_devid_map)
71 goto out_unlock;
72
73 for (i = 0; i < DEVID_HASH_SIZE; i++) {
74 list_for_each_entry(old, &nfsd_devid_hash[i], hash) {
75 if (old->fsid_type != fh->fh_fsid_type)
76 continue;
77 if (memcmp(old->fsid, fh->fh_fsid,
78 key_len(old->fsid_type)))
79 continue;
80
81 fhp->fh_export->ex_devid_map = old;
82 goto out_unlock;
83 }
84 }
85
86 map->idx = nfsd_devid_seq++;
87 list_add_tail_rcu(&map->hash, &nfsd_devid_hash[devid_hashfn(map->idx)]);
88 fhp->fh_export->ex_devid_map = map;
89 map = NULL;
90
91out_unlock:
92 spin_unlock(&nfsd_devid_lock);
93 kfree(map);
94}
95
96struct nfsd4_deviceid_map *
97nfsd4_find_devid_map(int idx)
98{
99 struct nfsd4_deviceid_map *map, *ret = NULL;
100
101 rcu_read_lock();
102 list_for_each_entry_rcu(map, &nfsd_devid_hash[devid_hashfn(idx)], hash)
103 if (map->idx == idx)
104 ret = map;
105 rcu_read_unlock();
106
107 return ret;
108}
109
110int
111nfsd4_set_deviceid(struct nfsd4_deviceid *id, const struct svc_fh *fhp,
112 u32 device_generation)
113{
114 if (!fhp->fh_export->ex_devid_map) {
115 nfsd4_alloc_devid_map(fhp);
116 if (!fhp->fh_export->ex_devid_map)
117 return -ENOMEM;
118 }
119
120 id->fsid_idx = fhp->fh_export->ex_devid_map->idx;
121 id->generation = device_generation;
122 id->pad = 0;
123 return 0;
124}
125
126void nfsd4_setup_layout_type(struct svc_export *exp)
127{
Tom Haynes9b9960a2016-06-14 13:41:28 -0700128#if defined(CONFIG_NFSD_BLOCKLAYOUT) || defined(CONFIG_NFSD_SCSILAYOUT)
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100129 struct super_block *sb = exp->ex_path.mnt->mnt_sb;
Tom Haynes9b9960a2016-06-14 13:41:28 -0700130#endif
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100131
Christoph Hellwigf3f03332015-03-30 18:46:29 +0200132 if (!(exp->ex_flags & NFSEXP_PNFS))
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200133 return;
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100134
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100135 /*
Tom Haynes9b9960a2016-06-14 13:41:28 -0700136 * If flex file is configured, use it by default. Otherwise
137 * check if the file system supports exporting a block-like layout.
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100138 * If the block device supports reservations prefer the SCSI layout,
139 * otherwise advertise the block layout.
140 */
Tom Haynes9b9960a2016-06-14 13:41:28 -0700141#ifdef CONFIG_NFSD_FLEXFILELAYOUT
Jeff Layton8a4c3922016-07-10 15:55:58 -0400142 exp->ex_layout_types |= 1 << LAYOUT_FLEX_FILES;
Tom Haynes9b9960a2016-06-14 13:41:28 -0700143#endif
Christoph Hellwig81c39322016-03-04 20:46:16 +0100144#ifdef CONFIG_NFSD_BLOCKLAYOUT
Tom Haynes9b9960a2016-06-14 13:41:28 -0700145 /* overwrite flex file layout selection if needed */
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100146 if (sb->s_export_op->get_uuid &&
147 sb->s_export_op->map_blocks &&
148 sb->s_export_op->commit_blocks)
Jeff Layton8a4c3922016-07-10 15:55:58 -0400149 exp->ex_layout_types |= 1 << LAYOUT_BLOCK_VOLUME;
Christoph Hellwig81c39322016-03-04 20:46:16 +0100150#endif
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100151#ifdef CONFIG_NFSD_SCSILAYOUT
152 /* overwrite block layout selection if needed */
153 if (sb->s_export_op->map_blocks &&
154 sb->s_export_op->commit_blocks &&
155 sb->s_bdev && sb->s_bdev->bd_disk->fops->pr_ops)
Jeff Layton8a4c3922016-07-10 15:55:58 -0400156 exp->ex_layout_types |= 1 << LAYOUT_SCSI;
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100157#endif
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200158}
159
160static void
161nfsd4_free_layout_stateid(struct nfs4_stid *stid)
162{
163 struct nfs4_layout_stateid *ls = layoutstateid(stid);
164 struct nfs4_client *clp = ls->ls_stid.sc_client;
165 struct nfs4_file *fp = ls->ls_stid.sc_file;
166
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500167 trace_layoutstate_free(&ls->ls_stid.sc_stateid);
168
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200169 spin_lock(&clp->cl_lock);
170 list_del_init(&ls->ls_perclnt);
171 spin_unlock(&clp->cl_lock);
172
173 spin_lock(&fp->fi_lock);
174 list_del_init(&ls->ls_perfile);
175 spin_unlock(&fp->fi_lock);
176
Jeff Layton1983a662016-08-11 13:36:22 -0400177 if (!nfsd4_layout_ops[ls->ls_layout_type]->disable_recalls)
178 vfs_setlease(ls->ls_file, F_UNLCK, NULL, (void **)&ls);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200179 fput(ls->ls_file);
180
181 if (ls->ls_recalled)
182 atomic_dec(&ls->ls_stid.sc_file->fi_lo_recalls);
183
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200184 kmem_cache_free(nfs4_layout_stateid_cache, ls);
185}
186
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200187static int
188nfsd4_layout_setlease(struct nfs4_layout_stateid *ls)
189{
190 struct file_lock *fl;
191 int status;
192
Jeff Layton1983a662016-08-11 13:36:22 -0400193 if (nfsd4_layout_ops[ls->ls_layout_type]->disable_recalls)
194 return 0;
195
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200196 fl = locks_alloc_lock();
197 if (!fl)
198 return -ENOMEM;
199 locks_init_lock(fl);
200 fl->fl_lmops = &nfsd4_layouts_lm_ops;
201 fl->fl_flags = FL_LAYOUT;
202 fl->fl_type = F_RDLCK;
203 fl->fl_end = OFFSET_MAX;
204 fl->fl_owner = ls;
205 fl->fl_pid = current->tgid;
206 fl->fl_file = ls->ls_file;
207
208 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl, NULL);
209 if (status) {
210 locks_free_lock(fl);
211 return status;
212 }
213 BUG_ON(fl != NULL);
214 return 0;
215}
216
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200217static struct nfs4_layout_stateid *
218nfsd4_alloc_layout_stateid(struct nfsd4_compound_state *cstate,
219 struct nfs4_stid *parent, u32 layout_type)
220{
221 struct nfs4_client *clp = cstate->clp;
222 struct nfs4_file *fp = parent->sc_file;
223 struct nfs4_layout_stateid *ls;
224 struct nfs4_stid *stp;
225
226 stp = nfs4_alloc_stid(cstate->clp, nfs4_layout_stateid_cache);
227 if (!stp)
228 return NULL;
229 stp->sc_free = nfsd4_free_layout_stateid;
230 get_nfs4_file(fp);
231 stp->sc_file = fp;
232
233 ls = layoutstateid(stp);
234 INIT_LIST_HEAD(&ls->ls_perclnt);
235 INIT_LIST_HEAD(&ls->ls_perfile);
236 spin_lock_init(&ls->ls_lock);
237 INIT_LIST_HEAD(&ls->ls_layouts);
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400238 mutex_init(&ls->ls_mutex);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200239 ls->ls_layout_type = layout_type;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200240 nfsd4_init_cb(&ls->ls_recall, clp, &nfsd4_cb_layout_ops,
241 NFSPROC4_CLNT_CB_LAYOUT);
242
243 if (parent->sc_type == NFS4_DELEG_STID)
244 ls->ls_file = get_file(fp->fi_deleg_file);
245 else
246 ls->ls_file = find_any_file(fp);
247 BUG_ON(!ls->ls_file);
248
249 if (nfsd4_layout_setlease(ls)) {
Kinglong Mee1ca4b882015-07-09 17:38:26 +0800250 fput(ls->ls_file);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200251 put_nfs4_file(fp);
252 kmem_cache_free(nfs4_layout_stateid_cache, ls);
253 return NULL;
254 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200255
256 spin_lock(&clp->cl_lock);
257 stp->sc_type = NFS4_LAYOUT_STID;
258 list_add(&ls->ls_perclnt, &clp->cl_lo_states);
259 spin_unlock(&clp->cl_lock);
260
261 spin_lock(&fp->fi_lock);
262 list_add(&ls->ls_perfile, &fp->fi_lo_states);
263 spin_unlock(&fp->fi_lock);
264
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500265 trace_layoutstate_alloc(&ls->ls_stid.sc_stateid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200266 return ls;
267}
268
269__be32
270nfsd4_preprocess_layout_stateid(struct svc_rqst *rqstp,
271 struct nfsd4_compound_state *cstate, stateid_t *stateid,
272 bool create, u32 layout_type, struct nfs4_layout_stateid **lsp)
273{
274 struct nfs4_layout_stateid *ls;
275 struct nfs4_stid *stid;
276 unsigned char typemask = NFS4_LAYOUT_STID;
277 __be32 status;
278
279 if (create)
280 typemask |= (NFS4_OPEN_STID | NFS4_LOCK_STID | NFS4_DELEG_STID);
281
282 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &stid,
283 net_generic(SVC_NET(rqstp), nfsd_net_id));
284 if (status)
285 goto out;
286
287 if (!fh_match(&cstate->current_fh.fh_handle,
288 &stid->sc_file->fi_fhandle)) {
289 status = nfserr_bad_stateid;
290 goto out_put_stid;
291 }
292
293 if (stid->sc_type != NFS4_LAYOUT_STID) {
294 ls = nfsd4_alloc_layout_stateid(cstate, stid, layout_type);
295 nfs4_put_stid(stid);
296
297 status = nfserr_jukebox;
298 if (!ls)
299 goto out;
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400300 mutex_lock(&ls->ls_mutex);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200301 } else {
302 ls = container_of(stid, struct nfs4_layout_stateid, ls_stid);
303
304 status = nfserr_bad_stateid;
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400305 mutex_lock(&ls->ls_mutex);
Jeff Layton14b7f4a2016-05-05 06:53:47 -0400306 if (nfsd4_stateid_generation_after(stateid, &stid->sc_stateid))
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400307 goto out_unlock_stid;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200308 if (layout_type != ls->ls_layout_type)
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400309 goto out_unlock_stid;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200310 }
311
312 *lsp = ls;
313 return 0;
314
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400315out_unlock_stid:
316 mutex_unlock(&ls->ls_mutex);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200317out_put_stid:
318 nfs4_put_stid(stid);
319out:
320 return status;
321}
322
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200323static void
324nfsd4_recall_file_layout(struct nfs4_layout_stateid *ls)
325{
326 spin_lock(&ls->ls_lock);
327 if (ls->ls_recalled)
328 goto out_unlock;
329
330 ls->ls_recalled = true;
331 atomic_inc(&ls->ls_stid.sc_file->fi_lo_recalls);
332 if (list_empty(&ls->ls_layouts))
333 goto out_unlock;
334
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500335 trace_layout_recall(&ls->ls_stid.sc_stateid);
336
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200337 atomic_inc(&ls->ls_stid.sc_count);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200338 nfsd4_run_cb(&ls->ls_recall);
339
340out_unlock:
341 spin_unlock(&ls->ls_lock);
342}
343
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200344static inline u64
345layout_end(struct nfsd4_layout_seg *seg)
346{
347 u64 end = seg->offset + seg->length;
348 return end >= seg->offset ? end : NFS4_MAX_UINT64;
349}
350
351static void
352layout_update_len(struct nfsd4_layout_seg *lo, u64 end)
353{
354 if (end == NFS4_MAX_UINT64)
355 lo->length = NFS4_MAX_UINT64;
356 else
357 lo->length = end - lo->offset;
358}
359
360static bool
361layouts_overlapping(struct nfs4_layout *lo, struct nfsd4_layout_seg *s)
362{
363 if (s->iomode != IOMODE_ANY && s->iomode != lo->lo_seg.iomode)
364 return false;
365 if (layout_end(&lo->lo_seg) <= s->offset)
366 return false;
367 if (layout_end(s) <= lo->lo_seg.offset)
368 return false;
369 return true;
370}
371
372static bool
373layouts_try_merge(struct nfsd4_layout_seg *lo, struct nfsd4_layout_seg *new)
374{
375 if (lo->iomode != new->iomode)
376 return false;
377 if (layout_end(new) < lo->offset)
378 return false;
379 if (layout_end(lo) < new->offset)
380 return false;
381
382 lo->offset = min(lo->offset, new->offset);
383 layout_update_len(lo, max(layout_end(lo), layout_end(new)));
384 return true;
385}
386
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200387static __be32
388nfsd4_recall_conflict(struct nfs4_layout_stateid *ls)
389{
390 struct nfs4_file *fp = ls->ls_stid.sc_file;
391 struct nfs4_layout_stateid *l, *n;
392 __be32 nfserr = nfs_ok;
393
394 assert_spin_locked(&fp->fi_lock);
395
396 list_for_each_entry_safe(l, n, &fp->fi_lo_states, ls_perfile) {
397 if (l != ls) {
398 nfsd4_recall_file_layout(l);
399 nfserr = nfserr_recallconflict;
400 }
401 }
402
403 return nfserr;
404}
405
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200406__be32
407nfsd4_insert_layout(struct nfsd4_layoutget *lgp, struct nfs4_layout_stateid *ls)
408{
409 struct nfsd4_layout_seg *seg = &lgp->lg_seg;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200410 struct nfs4_file *fp = ls->ls_stid.sc_file;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200411 struct nfs4_layout *lp, *new = NULL;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200412 __be32 nfserr;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200413
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200414 spin_lock(&fp->fi_lock);
415 nfserr = nfsd4_recall_conflict(ls);
416 if (nfserr)
417 goto out;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200418 spin_lock(&ls->ls_lock);
419 list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
420 if (layouts_try_merge(&lp->lo_seg, seg))
421 goto done;
422 }
423 spin_unlock(&ls->ls_lock);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200424 spin_unlock(&fp->fi_lock);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200425
426 new = kmem_cache_alloc(nfs4_layout_cache, GFP_KERNEL);
427 if (!new)
428 return nfserr_jukebox;
429 memcpy(&new->lo_seg, seg, sizeof(lp->lo_seg));
430 new->lo_state = ls;
431
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200432 spin_lock(&fp->fi_lock);
433 nfserr = nfsd4_recall_conflict(ls);
434 if (nfserr)
435 goto out;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200436 spin_lock(&ls->ls_lock);
437 list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
438 if (layouts_try_merge(&lp->lo_seg, seg))
439 goto done;
440 }
441
442 atomic_inc(&ls->ls_stid.sc_count);
443 list_add_tail(&new->lo_perstate, &ls->ls_layouts);
444 new = NULL;
445done:
Jeff Layton9767feb2015-10-01 09:05:50 -0400446 nfs4_inc_and_copy_stateid(&lgp->lg_sid, &ls->ls_stid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200447 spin_unlock(&ls->ls_lock);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200448out:
449 spin_unlock(&fp->fi_lock);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200450 if (new)
451 kmem_cache_free(nfs4_layout_cache, new);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200452 return nfserr;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200453}
454
455static void
456nfsd4_free_layouts(struct list_head *reaplist)
457{
458 while (!list_empty(reaplist)) {
459 struct nfs4_layout *lp = list_first_entry(reaplist,
460 struct nfs4_layout, lo_perstate);
461
462 list_del(&lp->lo_perstate);
463 nfs4_put_stid(&lp->lo_state->ls_stid);
464 kmem_cache_free(nfs4_layout_cache, lp);
465 }
466}
467
468static void
469nfsd4_return_file_layout(struct nfs4_layout *lp, struct nfsd4_layout_seg *seg,
470 struct list_head *reaplist)
471{
472 struct nfsd4_layout_seg *lo = &lp->lo_seg;
473 u64 end = layout_end(lo);
474
475 if (seg->offset <= lo->offset) {
476 if (layout_end(seg) >= end) {
477 list_move_tail(&lp->lo_perstate, reaplist);
478 return;
479 }
Kinglong Mee78902032015-03-22 22:17:20 +0800480 lo->offset = layout_end(seg);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200481 } else {
482 /* retain the whole layout segment on a split. */
483 if (layout_end(seg) < end) {
484 dprintk("%s: split not supported\n", __func__);
485 return;
486 }
Kinglong Mee78902032015-03-22 22:17:20 +0800487 end = seg->offset;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200488 }
489
490 layout_update_len(lo, end);
491}
492
493__be32
494nfsd4_return_file_layouts(struct svc_rqst *rqstp,
495 struct nfsd4_compound_state *cstate,
496 struct nfsd4_layoutreturn *lrp)
497{
498 struct nfs4_layout_stateid *ls;
499 struct nfs4_layout *lp, *n;
500 LIST_HEAD(reaplist);
501 __be32 nfserr;
502 int found = 0;
503
504 nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lrp->lr_sid,
505 false, lrp->lr_layout_type,
506 &ls);
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500507 if (nfserr) {
508 trace_layout_return_lookup_fail(&lrp->lr_sid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200509 return nfserr;
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500510 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200511
512 spin_lock(&ls->ls_lock);
513 list_for_each_entry_safe(lp, n, &ls->ls_layouts, lo_perstate) {
514 if (layouts_overlapping(lp, &lrp->lr_seg)) {
515 nfsd4_return_file_layout(lp, &lrp->lr_seg, &reaplist);
516 found++;
517 }
518 }
519 if (!list_empty(&ls->ls_layouts)) {
Jeff Layton9767feb2015-10-01 09:05:50 -0400520 if (found)
521 nfs4_inc_and_copy_stateid(&lrp->lr_sid, &ls->ls_stid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200522 lrp->lrs_present = 1;
523 } else {
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500524 trace_layoutstate_unhash(&ls->ls_stid.sc_stateid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200525 nfs4_unhash_stid(&ls->ls_stid);
526 lrp->lrs_present = 0;
527 }
528 spin_unlock(&ls->ls_lock);
529
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400530 mutex_unlock(&ls->ls_mutex);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200531 nfs4_put_stid(&ls->ls_stid);
532 nfsd4_free_layouts(&reaplist);
533 return nfs_ok;
534}
535
536__be32
537nfsd4_return_client_layouts(struct svc_rqst *rqstp,
538 struct nfsd4_compound_state *cstate,
539 struct nfsd4_layoutreturn *lrp)
540{
541 struct nfs4_layout_stateid *ls, *n;
542 struct nfs4_client *clp = cstate->clp;
543 struct nfs4_layout *lp, *t;
544 LIST_HEAD(reaplist);
545
546 lrp->lrs_present = 0;
547
548 spin_lock(&clp->cl_lock);
549 list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt) {
Kinglong Mee6f8f28ec2015-03-19 19:04:14 +0800550 if (ls->ls_layout_type != lrp->lr_layout_type)
551 continue;
552
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200553 if (lrp->lr_return_type == RETURN_FSID &&
554 !fh_fsid_match(&ls->ls_stid.sc_file->fi_fhandle,
555 &cstate->current_fh.fh_handle))
556 continue;
557
558 spin_lock(&ls->ls_lock);
559 list_for_each_entry_safe(lp, t, &ls->ls_layouts, lo_perstate) {
560 if (lrp->lr_seg.iomode == IOMODE_ANY ||
561 lrp->lr_seg.iomode == lp->lo_seg.iomode)
562 list_move_tail(&lp->lo_perstate, &reaplist);
563 }
564 spin_unlock(&ls->ls_lock);
565 }
566 spin_unlock(&clp->cl_lock);
567
568 nfsd4_free_layouts(&reaplist);
569 return 0;
570}
571
572static void
573nfsd4_return_all_layouts(struct nfs4_layout_stateid *ls,
574 struct list_head *reaplist)
575{
576 spin_lock(&ls->ls_lock);
577 list_splice_init(&ls->ls_layouts, reaplist);
578 spin_unlock(&ls->ls_lock);
579}
580
581void
582nfsd4_return_all_client_layouts(struct nfs4_client *clp)
583{
584 struct nfs4_layout_stateid *ls, *n;
585 LIST_HEAD(reaplist);
586
587 spin_lock(&clp->cl_lock);
588 list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt)
589 nfsd4_return_all_layouts(ls, &reaplist);
590 spin_unlock(&clp->cl_lock);
591
592 nfsd4_free_layouts(&reaplist);
593}
594
595void
596nfsd4_return_all_file_layouts(struct nfs4_client *clp, struct nfs4_file *fp)
597{
598 struct nfs4_layout_stateid *ls, *n;
599 LIST_HEAD(reaplist);
600
601 spin_lock(&fp->fi_lock);
602 list_for_each_entry_safe(ls, n, &fp->fi_lo_states, ls_perfile) {
603 if (ls->ls_stid.sc_client == clp)
604 nfsd4_return_all_layouts(ls, &reaplist);
605 }
606 spin_unlock(&fp->fi_lock);
607
608 nfsd4_free_layouts(&reaplist);
609}
610
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200611static void
612nfsd4_cb_layout_fail(struct nfs4_layout_stateid *ls)
613{
614 struct nfs4_client *clp = ls->ls_stid.sc_client;
615 char addr_str[INET6_ADDRSTRLEN];
Greg Kroah-Hartman377e7a22016-12-11 18:00:43 +0100616 static char const nfsd_recall_failed[] = "/sbin/nfsd-recall-failed";
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200617 static char *envp[] = {
618 "HOME=/",
619 "TERM=linux",
620 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
621 NULL
622 };
623 char *argv[8];
624 int error;
625
626 rpc_ntop((struct sockaddr *)&clp->cl_addr, addr_str, sizeof(addr_str));
627
628 printk(KERN_WARNING
629 "nfsd: client %s failed to respond to layout recall. "
630 " Fencing..\n", addr_str);
631
Greg Kroah-Hartman377e7a22016-12-11 18:00:43 +0100632 argv[0] = (char *)nfsd_recall_failed;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200633 argv[1] = addr_str;
634 argv[2] = ls->ls_file->f_path.mnt->mnt_sb->s_id;
635 argv[3] = NULL;
636
Greg Kroah-Hartman377e7a22016-12-11 18:00:43 +0100637 error = call_usermodehelper(nfsd_recall_failed, argv, envp,
638 UMH_WAIT_PROC);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200639 if (error) {
640 printk(KERN_ERR "nfsd: fence failed for client %s: %d!\n",
641 addr_str, error);
642 }
643}
644
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400645static void
646nfsd4_cb_layout_prepare(struct nfsd4_callback *cb)
647{
648 struct nfs4_layout_stateid *ls =
649 container_of(cb, struct nfs4_layout_stateid, ls_recall);
650
651 mutex_lock(&ls->ls_mutex);
Jeff Layton9767feb2015-10-01 09:05:50 -0400652 nfs4_inc_and_copy_stateid(&ls->ls_recall_sid, &ls->ls_stid);
Jeff Laytonbe20aa02015-11-29 08:46:14 -0500653 mutex_unlock(&ls->ls_mutex);
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400654}
655
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200656static int
657nfsd4_cb_layout_done(struct nfsd4_callback *cb, struct rpc_task *task)
658{
659 struct nfs4_layout_stateid *ls =
660 container_of(cb, struct nfs4_layout_stateid, ls_recall);
Jeff Layton6b9b2102015-12-08 07:23:48 -0500661 struct nfsd_net *nn;
662 ktime_t now, cutoff;
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100663 const struct nfsd4_layout_ops *ops;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200664 LIST_HEAD(reaplist);
665
Jeff Layton6b9b2102015-12-08 07:23:48 -0500666
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200667 switch (task->tk_status) {
668 case 0:
Jeff Layton6b9b2102015-12-08 07:23:48 -0500669 case -NFS4ERR_DELAY:
670 /*
671 * Anything left? If not, then call it done. Note that we don't
672 * take the spinlock since this is an optimization and nothing
673 * should get added until the cb counter goes to zero.
674 */
675 if (list_empty(&ls->ls_layouts))
676 return 1;
677
678 /* Poll the client until it's done with the layout */
679 now = ktime_get();
680 nn = net_generic(ls->ls_stid.sc_client->net, nfsd_net_id);
681
682 /* Client gets 2 lease periods to return it */
683 cutoff = ktime_add_ns(task->tk_start,
684 nn->nfsd4_lease * NSEC_PER_SEC * 2);
685
686 if (ktime_before(now, cutoff)) {
687 rpc_delay(task, HZ/100); /* 10 mili-seconds */
688 return 0;
689 }
690 /* Fallthrough */
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200691 default:
692 /*
693 * Unknown error or non-responding client, we'll need to fence.
694 */
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100695 trace_layout_recall_fail(&ls->ls_stid.sc_stateid);
696
697 ops = nfsd4_layout_ops[ls->ls_layout_type];
698 if (ops->fence_client)
699 ops->fence_client(ls);
700 else
701 nfsd4_cb_layout_fail(ls);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200702 return -1;
Jeff Layton851238a2016-10-20 12:21:34 -0400703 case -NFS4ERR_NOMATCHING_LAYOUT:
704 trace_layout_recall_done(&ls->ls_stid.sc_stateid);
705 task->tk_status = 0;
706 return 1;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200707 }
708}
709
710static void
711nfsd4_cb_layout_release(struct nfsd4_callback *cb)
712{
713 struct nfs4_layout_stateid *ls =
714 container_of(cb, struct nfs4_layout_stateid, ls_recall);
715 LIST_HEAD(reaplist);
716
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500717 trace_layout_recall_release(&ls->ls_stid.sc_stateid);
718
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200719 nfsd4_return_all_layouts(ls, &reaplist);
720 nfsd4_free_layouts(&reaplist);
721 nfs4_put_stid(&ls->ls_stid);
722}
723
Julia Lawallc4cb8972015-11-21 22:57:39 +0100724static const struct nfsd4_callback_ops nfsd4_cb_layout_ops = {
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400725 .prepare = nfsd4_cb_layout_prepare,
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200726 .done = nfsd4_cb_layout_done,
727 .release = nfsd4_cb_layout_release,
728};
729
730static bool
731nfsd4_layout_lm_break(struct file_lock *fl)
732{
733 /*
734 * We don't want the locks code to timeout the lease for us;
735 * we'll remove it ourself if a layout isn't returned
736 * in time:
737 */
738 fl->fl_break_time = 0;
739 nfsd4_recall_file_layout(fl->fl_owner);
740 return false;
741}
742
743static int
744nfsd4_layout_lm_change(struct file_lock *onlist, int arg,
745 struct list_head *dispose)
746{
747 BUG_ON(!(arg & F_UNLCK));
748 return lease_modify(onlist, arg, dispose);
749}
750
751static const struct lock_manager_operations nfsd4_layouts_lm_ops = {
752 .lm_break = nfsd4_layout_lm_break,
753 .lm_change = nfsd4_layout_lm_change,
754};
755
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200756int
757nfsd4_init_pnfs(void)
758{
759 int i;
760
761 for (i = 0; i < DEVID_HASH_SIZE; i++)
762 INIT_LIST_HEAD(&nfsd_devid_hash[i]);
763
764 nfs4_layout_cache = kmem_cache_create("nfs4_layout",
765 sizeof(struct nfs4_layout), 0, 0, NULL);
766 if (!nfs4_layout_cache)
767 return -ENOMEM;
768
769 nfs4_layout_stateid_cache = kmem_cache_create("nfs4_layout_stateid",
770 sizeof(struct nfs4_layout_stateid), 0, 0, NULL);
771 if (!nfs4_layout_stateid_cache) {
772 kmem_cache_destroy(nfs4_layout_cache);
773 return -ENOMEM;
774 }
775 return 0;
776}
777
778void
779nfsd4_exit_pnfs(void)
780{
781 int i;
782
783 kmem_cache_destroy(nfs4_layout_cache);
784 kmem_cache_destroy(nfs4_layout_stateid_cache);
785
786 for (i = 0; i < DEVID_HASH_SIZE; i++) {
787 struct nfsd4_deviceid_map *map, *n;
788
789 list_for_each_entry_safe(map, n, &nfsd_devid_hash[i], hash)
790 kfree(map);
791 }
792}