xref: /linux/fs/nfsd/nfs4layouts.c (revision d141ec2825b4d3ec52f27c43bdd864090159273a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2014 Christoph Hellwig.
4  */
5 #include <linux/exportfs_block.h>
6 #include <linux/kmod.h>
7 #include <linux/file.h>
8 #include <linux/jhash.h>
9 #include <linux/sched.h>
10 #include <linux/sunrpc/addr.h>
11 
12 #include "pnfs.h"
13 #include "netns.h"
14 #include "trace.h"
15 
16 #define NFSDDBG_FACILITY                NFSDDBG_PNFS
17 
18 struct nfs4_layout {
19 	struct list_head		lo_perstate;
20 	struct nfs4_layout_stateid	*lo_state;
21 	struct nfsd4_layout_seg		lo_seg;
22 };
23 
24 static struct kmem_cache *nfs4_layout_cache;
25 static struct kmem_cache *nfs4_layout_stateid_cache;
26 
27 static const struct nfsd4_callback_ops nfsd4_cb_layout_ops;
28 static const struct lease_manager_operations nfsd4_layouts_lm_ops;
29 
30 static void nfsd4_layout_fence_worker(struct work_struct *work);
31 
32 const struct nfsd4_layout_ops *nfsd4_layout_ops[LAYOUT_TYPE_MAX] =  {
33 #ifdef CONFIG_NFSD_FLEXFILELAYOUT
34 	[LAYOUT_FLEX_FILES]	= &ff_layout_ops,
35 #endif
36 #ifdef CONFIG_NFSD_BLOCKLAYOUT
37 	[LAYOUT_BLOCK_VOLUME]	= &bl_layout_ops,
38 #endif
39 #ifdef CONFIG_NFSD_SCSILAYOUT
40 	[LAYOUT_SCSI]		= &scsi_layout_ops,
41 #endif
42 };
43 
44 /* pNFS device ID to export fsid mapping */
45 #define DEVID_HASH_BITS	8
46 #define DEVID_HASH_SIZE	(1 << DEVID_HASH_BITS)
47 #define DEVID_HASH_MASK	(DEVID_HASH_SIZE - 1)
48 static u64 nfsd_devid_seq = 1;
49 static struct list_head nfsd_devid_hash[DEVID_HASH_SIZE];
50 static DEFINE_SPINLOCK(nfsd_devid_lock);
51 
devid_hashfn(u64 idx)52 static inline u32 devid_hashfn(u64 idx)
53 {
54 	return jhash_2words(idx, idx >> 32, 0) & DEVID_HASH_MASK;
55 }
56 
57 static void
nfsd4_alloc_devid_map(const struct svc_fh * fhp)58 nfsd4_alloc_devid_map(const struct svc_fh *fhp)
59 {
60 	const struct knfsd_fh *fh = &fhp->fh_handle;
61 	size_t fsid_len = key_len(fh->fh_fsid_type);
62 	struct nfsd4_deviceid_map *map, *old;
63 	int i;
64 
65 	map = kzalloc(sizeof(*map) + fsid_len, GFP_KERNEL);
66 	if (!map)
67 		return;
68 
69 	map->fsid_type = fh->fh_fsid_type;
70 	memcpy(&map->fsid, fh_fsid(fh), fsid_len);
71 
72 	spin_lock(&nfsd_devid_lock);
73 	if (fhp->fh_export->ex_devid_map)
74 		goto out_unlock;
75 
76 	for (i = 0; i < DEVID_HASH_SIZE; i++) {
77 		list_for_each_entry(old, &nfsd_devid_hash[i], hash) {
78 			if (old->fsid_type != fh->fh_fsid_type)
79 				continue;
80 			if (memcmp(old->fsid, fh_fsid(fh),
81 					key_len(old->fsid_type)))
82 				continue;
83 
84 			fhp->fh_export->ex_devid_map = old;
85 			goto out_unlock;
86 		}
87 	}
88 
89 	map->idx = nfsd_devid_seq++;
90 	list_add_tail_rcu(&map->hash, &nfsd_devid_hash[devid_hashfn(map->idx)]);
91 	fhp->fh_export->ex_devid_map = map;
92 	map = NULL;
93 
94 out_unlock:
95 	spin_unlock(&nfsd_devid_lock);
96 	kfree(map);
97 }
98 
99 struct nfsd4_deviceid_map *
nfsd4_find_devid_map(int idx)100 nfsd4_find_devid_map(int idx)
101 {
102 	struct nfsd4_deviceid_map *map, *ret = NULL;
103 
104 	rcu_read_lock();
105 	list_for_each_entry_rcu(map, &nfsd_devid_hash[devid_hashfn(idx)], hash)
106 		if (map->idx == idx)
107 			ret = map;
108 	rcu_read_unlock();
109 
110 	return ret;
111 }
112 
113 int
nfsd4_set_deviceid(struct nfsd4_deviceid * id,const struct svc_fh * fhp,u32 device_generation)114 nfsd4_set_deviceid(struct nfsd4_deviceid *id, const struct svc_fh *fhp,
115 		u32 device_generation)
116 {
117 	if (!fhp->fh_export->ex_devid_map) {
118 		nfsd4_alloc_devid_map(fhp);
119 		if (!fhp->fh_export->ex_devid_map)
120 			return -ENOMEM;
121 	}
122 
123 	id->fsid_idx = fhp->fh_export->ex_devid_map->idx;
124 	id->generation = device_generation;
125 	return 0;
126 }
127 
nfsd4_setup_layout_type(struct svc_export * exp)128 void nfsd4_setup_layout_type(struct svc_export *exp)
129 {
130 	struct super_block *sb = exp->ex_path.mnt->mnt_sb;
131 	expfs_block_layouts_t block_supported = exportfs_layouts_supported(sb);
132 
133 	if (IS_ENABLED(CONFIG_NFSD_FLEXFILELAYOUT))
134 		exp->ex_layout_types |= 1 << LAYOUT_FLEX_FILES;
135 	if (IS_ENABLED(CONFIG_NFSD_BLOCKLAYOUT) &&
136 	    (block_supported & EXPFS_BLOCK_IN_BAND_ID))
137 		exp->ex_layout_types |= 1 << LAYOUT_BLOCK_VOLUME;
138 	if (IS_ENABLED(CONFIG_NFSD_SCSILAYOUT) &&
139 	    (block_supported & EXPFS_BLOCK_OUT_OF_BAND_ID))
140 		exp->ex_layout_types |= 1 << LAYOUT_SCSI;
141 }
142 
nfsd4_close_layout(struct nfs4_layout_stateid * ls)143 void nfsd4_close_layout(struct nfs4_layout_stateid *ls)
144 {
145 	struct nfsd_file *fl;
146 
147 	spin_lock(&ls->ls_stid.sc_file->fi_lock);
148 	fl = ls->ls_file;
149 	ls->ls_file = NULL;
150 	spin_unlock(&ls->ls_stid.sc_file->fi_lock);
151 
152 	if (fl) {
153 		if (!nfsd4_layout_ops[ls->ls_layout_type]->disable_recalls)
154 			kernel_setlease(fl->nf_file, F_UNLCK, NULL,
155 					(void **)&ls);
156 		nfsd_file_put(fl);
157 	}
158 }
159 
160 static void
nfsd4_free_layout_stateid(struct nfs4_stid * stid)161 nfsd4_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 
167 	trace_nfsd_layoutstate_free(&ls->ls_stid.sc_stateid);
168 
169 	spin_lock(&ls->ls_lock);
170 	if (delayed_work_pending(&ls->ls_fence_work)) {
171 		spin_unlock(&ls->ls_lock);
172 		cancel_delayed_work_sync(&ls->ls_fence_work);
173 	} else
174 		spin_unlock(&ls->ls_lock);
175 
176 	spin_lock(&clp->cl_lock);
177 	list_del_init(&ls->ls_perclnt);
178 	spin_unlock(&clp->cl_lock);
179 
180 	spin_lock(&fp->fi_lock);
181 	list_del_init(&ls->ls_perfile);
182 	spin_unlock(&fp->fi_lock);
183 
184 	nfsd4_close_layout(ls);
185 
186 	if (ls->ls_recalled)
187 		atomic_dec(&ls->ls_stid.sc_file->fi_lo_recalls);
188 
189 	kmem_cache_free(nfs4_layout_stateid_cache, ls);
190 }
191 
192 static int
nfsd4_layout_setlease(struct nfs4_layout_stateid * ls)193 nfsd4_layout_setlease(struct nfs4_layout_stateid *ls)
194 {
195 	struct file_lease *fl;
196 	int status;
197 
198 	if (nfsd4_layout_ops[ls->ls_layout_type]->disable_recalls)
199 		return 0;
200 
201 	fl = locks_alloc_lease();
202 	if (!fl)
203 		return -ENOMEM;
204 	locks_init_lease(fl);
205 	fl->fl_lmops = &nfsd4_layouts_lm_ops;
206 	fl->c.flc_flags = FL_LAYOUT;
207 	fl->c.flc_type = F_RDLCK;
208 	fl->c.flc_owner = ls;
209 	fl->c.flc_pid = current->tgid;
210 	fl->c.flc_file = ls->ls_file->nf_file;
211 
212 	status = kernel_setlease(fl->c.flc_file, fl->c.flc_type, &fl, NULL);
213 	if (status) {
214 		locks_free_lease(fl);
215 		return status;
216 	}
217 	BUG_ON(fl != NULL);
218 	return 0;
219 }
220 
221 static struct nfs4_layout_stateid *
nfsd4_alloc_layout_stateid(struct nfsd4_compound_state * cstate,struct nfs4_stid * parent,u32 layout_type)222 nfsd4_alloc_layout_stateid(struct nfsd4_compound_state *cstate,
223 		struct nfs4_stid *parent, u32 layout_type)
224 {
225 	struct nfs4_client *clp = cstate->clp;
226 	struct nfs4_file *fp = parent->sc_file;
227 	struct nfs4_layout_stateid *ls;
228 	struct nfs4_stid *stp;
229 
230 	stp = nfs4_alloc_stid(cstate->clp, nfs4_layout_stateid_cache,
231 					nfsd4_free_layout_stateid);
232 	if (!stp)
233 		return NULL;
234 
235 	get_nfs4_file(fp);
236 	stp->sc_file = fp;
237 	if (parent->sc_export)
238 		stp->sc_export = exp_get(parent->sc_export);
239 
240 	ls = layoutstateid(stp);
241 	INIT_LIST_HEAD(&ls->ls_perclnt);
242 	INIT_LIST_HEAD(&ls->ls_perfile);
243 	spin_lock_init(&ls->ls_lock);
244 	INIT_LIST_HEAD(&ls->ls_layouts);
245 	mutex_init(&ls->ls_mutex);
246 	ls->ls_layout_type = layout_type;
247 	nfsd4_init_cb(&ls->ls_recall, clp, &nfsd4_cb_layout_ops,
248 			NFSPROC4_CLNT_CB_LAYOUT);
249 
250 	if (parent->sc_type == SC_TYPE_DELEG) {
251 		rcu_read_lock();
252 		ls->ls_file = nfsd_file_get(rcu_dereference(fp->fi_deleg_file));
253 		rcu_read_unlock();
254 	} else {
255 		ls->ls_file = find_any_file(fp);
256 	}
257 
258 	if (!ls->ls_file) {
259 		nfs4_put_stid(stp);
260 		return NULL;
261 	}
262 
263 	ls->ls_fenced = false;
264 	ls->ls_fence_inflight = false;
265 	ls->ls_fence_delay = 0;
266 	INIT_DELAYED_WORK(&ls->ls_fence_work, nfsd4_layout_fence_worker);
267 
268 	if (nfsd4_layout_setlease(ls)) {
269 		nfs4_put_stid(stp);
270 		return NULL;
271 	}
272 
273 	spin_lock(&clp->cl_lock);
274 	stp->sc_type = SC_TYPE_LAYOUT;
275 	list_add(&ls->ls_perclnt, &clp->cl_lo_states);
276 	spin_unlock(&clp->cl_lock);
277 
278 	spin_lock(&fp->fi_lock);
279 	list_add(&ls->ls_perfile, &fp->fi_lo_states);
280 	spin_unlock(&fp->fi_lock);
281 
282 	trace_nfsd_layoutstate_alloc(&ls->ls_stid.sc_stateid);
283 	return ls;
284 }
285 
286 __be32
nfsd4_preprocess_layout_stateid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,stateid_t * stateid,bool create,u32 layout_type,struct nfs4_layout_stateid ** lsp)287 nfsd4_preprocess_layout_stateid(struct svc_rqst *rqstp,
288 		struct nfsd4_compound_state *cstate, stateid_t *stateid,
289 		bool create, u32 layout_type, struct nfs4_layout_stateid **lsp)
290 {
291 	struct nfs4_layout_stateid *ls;
292 	struct nfs4_stid *stid;
293 	unsigned short typemask = SC_TYPE_LAYOUT;
294 	__be32 status;
295 
296 	if (create)
297 		typemask |= (SC_TYPE_OPEN | SC_TYPE_LOCK | SC_TYPE_DELEG);
298 
299 	status = nfsd4_lookup_stateid(cstate, stateid, typemask, 0, &stid,
300 			net_generic(SVC_NET(rqstp), nfsd_net_id));
301 	if (status)
302 		goto out;
303 
304 	if (!fh_match(&cstate->current_fh.fh_handle,
305 		      &stid->sc_file->fi_fhandle)) {
306 		status = nfserr_bad_stateid;
307 		goto out_put_stid;
308 	}
309 
310 	if (stid->sc_type != SC_TYPE_LAYOUT) {
311 		ls = nfsd4_alloc_layout_stateid(cstate, stid, layout_type);
312 		nfs4_put_stid(stid);
313 
314 		status = nfserr_jukebox;
315 		if (!ls)
316 			goto out;
317 		mutex_lock(&ls->ls_mutex);
318 	} else {
319 		ls = container_of(stid, struct nfs4_layout_stateid, ls_stid);
320 
321 		status = nfserr_bad_stateid;
322 		mutex_lock(&ls->ls_mutex);
323 		if (nfsd4_stateid_generation_after(stateid, &stid->sc_stateid))
324 			goto out_unlock_stid;
325 		if (layout_type != ls->ls_layout_type)
326 			goto out_unlock_stid;
327 	}
328 
329 	*lsp = ls;
330 	return 0;
331 
332 out_unlock_stid:
333 	mutex_unlock(&ls->ls_mutex);
334 out_put_stid:
335 	nfs4_put_stid(stid);
336 out:
337 	return status;
338 }
339 
340 static void
nfsd4_recall_file_layout(struct nfs4_layout_stateid * ls)341 nfsd4_recall_file_layout(struct nfs4_layout_stateid *ls)
342 {
343 	spin_lock(&ls->ls_lock);
344 	if (ls->ls_recalled)
345 		goto out_unlock;
346 
347 	if (list_empty(&ls->ls_layouts))
348 		goto out_unlock;
349 
350 	ls->ls_recalled = true;
351 	atomic_inc(&ls->ls_stid.sc_file->fi_lo_recalls);
352 	trace_nfsd_layout_recall(&ls->ls_stid.sc_stateid);
353 
354 	if (!test_and_set_bit(NFSD4_CALLBACK_RUNNING, &ls->ls_recall.cb_flags)) {
355 		refcount_inc(&ls->ls_stid.sc_count);
356 		nfsd4_run_cb(&ls->ls_recall);
357 	}
358 out_unlock:
359 	spin_unlock(&ls->ls_lock);
360 }
361 
362 static inline u64
layout_end(struct nfsd4_layout_seg * seg)363 layout_end(struct nfsd4_layout_seg *seg)
364 {
365 	u64 end = seg->offset + seg->length;
366 	return end >= seg->offset ? end : NFS4_MAX_UINT64;
367 }
368 
369 static void
layout_update_len(struct nfsd4_layout_seg * lo,u64 end)370 layout_update_len(struct nfsd4_layout_seg *lo, u64 end)
371 {
372 	if (end == NFS4_MAX_UINT64)
373 		lo->length = NFS4_MAX_UINT64;
374 	else
375 		lo->length = end - lo->offset;
376 }
377 
378 static bool
layouts_overlapping(struct nfs4_layout * lo,struct nfsd4_layout_seg * s)379 layouts_overlapping(struct nfs4_layout *lo, struct nfsd4_layout_seg *s)
380 {
381 	if (s->iomode != IOMODE_ANY && s->iomode != lo->lo_seg.iomode)
382 		return false;
383 	if (layout_end(&lo->lo_seg) <= s->offset)
384 		return false;
385 	if (layout_end(s) <= lo->lo_seg.offset)
386 		return false;
387 	return true;
388 }
389 
390 static bool
layouts_try_merge(struct nfsd4_layout_seg * lo,struct nfsd4_layout_seg * new)391 layouts_try_merge(struct nfsd4_layout_seg *lo, struct nfsd4_layout_seg *new)
392 {
393 	if (lo->iomode != new->iomode)
394 		return false;
395 	if (layout_end(new) < lo->offset)
396 		return false;
397 	if (layout_end(lo) < new->offset)
398 		return false;
399 
400 	lo->offset = min(lo->offset, new->offset);
401 	layout_update_len(lo, max(layout_end(lo), layout_end(new)));
402 	return true;
403 }
404 
405 static __be32
nfsd4_recall_conflict(struct nfs4_layout_stateid * ls)406 nfsd4_recall_conflict(struct nfs4_layout_stateid *ls)
407 {
408 	struct nfs4_file *fp = ls->ls_stid.sc_file;
409 	struct nfs4_layout_stateid *l, *n;
410 	__be32 nfserr = nfs_ok;
411 
412 	assert_spin_locked(&fp->fi_lock);
413 
414 	list_for_each_entry_safe(l, n, &fp->fi_lo_states, ls_perfile) {
415 		if (l != ls) {
416 			nfsd4_recall_file_layout(l);
417 			nfserr = nfserr_recallconflict;
418 		}
419 	}
420 
421 	return nfserr;
422 }
423 
424 __be32
nfsd4_insert_layout(struct nfsd4_layoutget * lgp,struct nfs4_layout_stateid * ls)425 nfsd4_insert_layout(struct nfsd4_layoutget *lgp, struct nfs4_layout_stateid *ls)
426 {
427 	struct nfsd4_layout_seg *seg = &lgp->lg_seg;
428 	struct nfs4_file *fp = ls->ls_stid.sc_file;
429 	struct nfs4_layout *lp, *new = NULL;
430 	__be32 nfserr;
431 
432 	spin_lock(&fp->fi_lock);
433 	nfserr = nfsd4_recall_conflict(ls);
434 	if (nfserr)
435 		goto out;
436 	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 	spin_unlock(&ls->ls_lock);
442 	spin_unlock(&fp->fi_lock);
443 
444 	new = kmem_cache_alloc(nfs4_layout_cache, GFP_KERNEL);
445 	if (!new)
446 		return nfserr_jukebox;
447 	memcpy(&new->lo_seg, seg, sizeof(new->lo_seg));
448 	new->lo_state = ls;
449 
450 	spin_lock(&fp->fi_lock);
451 	nfserr = nfsd4_recall_conflict(ls);
452 	if (nfserr)
453 		goto out;
454 	spin_lock(&ls->ls_lock);
455 	list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
456 		if (layouts_try_merge(&lp->lo_seg, seg))
457 			goto done;
458 	}
459 
460 	refcount_inc(&ls->ls_stid.sc_count);
461 	list_add_tail(&new->lo_perstate, &ls->ls_layouts);
462 	new = NULL;
463 done:
464 	nfs4_inc_and_copy_stateid(&lgp->lg_sid, &ls->ls_stid);
465 	spin_unlock(&ls->ls_lock);
466 out:
467 	spin_unlock(&fp->fi_lock);
468 	if (new)
469 		kmem_cache_free(nfs4_layout_cache, new);
470 	return nfserr;
471 }
472 
473 static void
nfsd4_free_layouts(struct list_head * reaplist)474 nfsd4_free_layouts(struct list_head *reaplist)
475 {
476 	while (!list_empty(reaplist)) {
477 		struct nfs4_layout *lp = list_first_entry(reaplist,
478 				struct nfs4_layout, lo_perstate);
479 
480 		list_del(&lp->lo_perstate);
481 		nfs4_put_stid(&lp->lo_state->ls_stid);
482 		kmem_cache_free(nfs4_layout_cache, lp);
483 	}
484 }
485 
486 static void
nfsd4_return_file_layout(struct nfs4_layout * lp,struct nfsd4_layout_seg * seg,struct list_head * reaplist)487 nfsd4_return_file_layout(struct nfs4_layout *lp, struct nfsd4_layout_seg *seg,
488 		struct list_head *reaplist)
489 {
490 	struct nfsd4_layout_seg *lo = &lp->lo_seg;
491 	u64 end = layout_end(lo);
492 
493 	if (seg->offset <= lo->offset) {
494 		if (layout_end(seg) >= end) {
495 			list_move_tail(&lp->lo_perstate, reaplist);
496 			return;
497 		}
498 		lo->offset = layout_end(seg);
499 	} else {
500 		/* retain the whole layout segment on a split. */
501 		if (layout_end(seg) < end) {
502 			dprintk("%s: split not supported\n", __func__);
503 			return;
504 		}
505 		end = seg->offset;
506 	}
507 
508 	layout_update_len(lo, end);
509 }
510 
511 __be32
nfsd4_return_file_layouts(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_layoutreturn * lrp)512 nfsd4_return_file_layouts(struct svc_rqst *rqstp,
513 		struct nfsd4_compound_state *cstate,
514 		struct nfsd4_layoutreturn *lrp)
515 {
516 	struct nfs4_layout_stateid *ls;
517 	struct nfs4_layout *lp, *n;
518 	LIST_HEAD(reaplist);
519 	__be32 nfserr;
520 	int found = 0;
521 
522 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lrp->lr_sid,
523 						false, lrp->lr_layout_type,
524 						&ls);
525 	if (nfserr) {
526 		trace_nfsd_layout_return_lookup_fail(&lrp->lr_sid);
527 		return nfserr;
528 	}
529 
530 	spin_lock(&ls->ls_lock);
531 	list_for_each_entry_safe(lp, n, &ls->ls_layouts, lo_perstate) {
532 		if (layouts_overlapping(lp, &lrp->lr_seg)) {
533 			nfsd4_return_file_layout(lp, &lrp->lr_seg, &reaplist);
534 			found++;
535 		}
536 	}
537 	if (!list_empty(&ls->ls_layouts)) {
538 		if (found)
539 			nfs4_inc_and_copy_stateid(&lrp->lr_sid, &ls->ls_stid);
540 		lrp->lrs_present = true;
541 	} else {
542 		trace_nfsd_layoutstate_unhash(&ls->ls_stid.sc_stateid);
543 		ls->ls_stid.sc_status |= SC_STATUS_CLOSED;
544 		lrp->lrs_present = false;
545 	}
546 	spin_unlock(&ls->ls_lock);
547 
548 	mutex_unlock(&ls->ls_mutex);
549 	nfs4_put_stid(&ls->ls_stid);
550 	nfsd4_free_layouts(&reaplist);
551 	return nfs_ok;
552 }
553 
554 __be32
nfsd4_return_client_layouts(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct nfsd4_layoutreturn * lrp)555 nfsd4_return_client_layouts(struct svc_rqst *rqstp,
556 		struct nfsd4_compound_state *cstate,
557 		struct nfsd4_layoutreturn *lrp)
558 {
559 	struct nfs4_layout_stateid *ls, *n;
560 	struct nfs4_client *clp = cstate->clp;
561 	struct nfs4_layout *lp, *t;
562 	LIST_HEAD(reaplist);
563 
564 	lrp->lrs_present = false;
565 
566 	spin_lock(&clp->cl_lock);
567 	list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt) {
568 		if (ls->ls_layout_type != lrp->lr_layout_type)
569 			continue;
570 
571 		if (lrp->lr_return_type == RETURN_FSID &&
572 		    !fh_fsid_match(&ls->ls_stid.sc_file->fi_fhandle,
573 				   &cstate->current_fh.fh_handle))
574 			continue;
575 
576 		spin_lock(&ls->ls_lock);
577 		list_for_each_entry_safe(lp, t, &ls->ls_layouts, lo_perstate) {
578 			if (lrp->lr_seg.iomode == IOMODE_ANY ||
579 			    lrp->lr_seg.iomode == lp->lo_seg.iomode)
580 				list_move_tail(&lp->lo_perstate, &reaplist);
581 		}
582 		spin_unlock(&ls->ls_lock);
583 	}
584 	spin_unlock(&clp->cl_lock);
585 
586 	nfsd4_free_layouts(&reaplist);
587 	return 0;
588 }
589 
590 static void
nfsd4_return_all_layouts(struct nfs4_layout_stateid * ls,struct list_head * reaplist)591 nfsd4_return_all_layouts(struct nfs4_layout_stateid *ls,
592 		struct list_head *reaplist)
593 {
594 	spin_lock(&ls->ls_lock);
595 	list_splice_init(&ls->ls_layouts, reaplist);
596 	spin_unlock(&ls->ls_lock);
597 }
598 
599 void
nfsd4_return_all_client_layouts(struct nfs4_client * clp)600 nfsd4_return_all_client_layouts(struct nfs4_client *clp)
601 {
602 	struct nfs4_layout_stateid *ls, *n;
603 	LIST_HEAD(reaplist);
604 
605 	spin_lock(&clp->cl_lock);
606 	list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt)
607 		nfsd4_return_all_layouts(ls, &reaplist);
608 	spin_unlock(&clp->cl_lock);
609 
610 	nfsd4_free_layouts(&reaplist);
611 }
612 
613 void
nfsd4_return_all_file_layouts(struct nfs4_client * clp,struct nfs4_file * fp)614 nfsd4_return_all_file_layouts(struct nfs4_client *clp, struct nfs4_file *fp)
615 {
616 	struct nfs4_layout_stateid *ls, *n;
617 	LIST_HEAD(reaplist);
618 
619 	spin_lock(&fp->fi_lock);
620 	list_for_each_entry_safe(ls, n, &fp->fi_lo_states, ls_perfile) {
621 		if (ls->ls_stid.sc_client == clp)
622 			nfsd4_return_all_layouts(ls, &reaplist);
623 	}
624 	spin_unlock(&fp->fi_lock);
625 
626 	nfsd4_free_layouts(&reaplist);
627 }
628 
629 static void
nfsd4_cb_layout_fail(struct nfs4_layout_stateid * ls,struct nfsd_file * file)630 nfsd4_cb_layout_fail(struct nfs4_layout_stateid *ls, struct nfsd_file *file)
631 {
632 	struct nfs4_client *clp = ls->ls_stid.sc_client;
633 	char addr_str[INET6_ADDRSTRLEN];
634 	static char const nfsd_recall_failed[] = "/sbin/nfsd-recall-failed";
635 	static char *envp[] = {
636 		"HOME=/",
637 		"TERM=linux",
638 		"PATH=/sbin:/usr/sbin:/bin:/usr/bin",
639 		NULL
640 	};
641 	char *argv[8];
642 	int error;
643 
644 	rpc_ntop((struct sockaddr *)&clp->cl_addr, addr_str, sizeof(addr_str));
645 
646 	printk(KERN_WARNING
647 		"nfsd: client %s failed to respond to layout recall. "
648 		"  Fencing..\n", addr_str);
649 
650 	argv[0] = (char *)nfsd_recall_failed;
651 	argv[1] = addr_str;
652 	argv[2] = file->nf_file->f_path.mnt->mnt_sb->s_id;
653 	argv[3] = NULL;
654 
655 	error = call_usermodehelper(nfsd_recall_failed, argv, envp,
656 				    UMH_WAIT_PROC);
657 	if (error) {
658 		printk(KERN_ERR "nfsd: fence failed for client %s: %d!\n",
659 			addr_str, error);
660 	}
661 }
662 
663 static bool
nfsd4_cb_layout_prepare(struct nfsd4_callback * cb)664 nfsd4_cb_layout_prepare(struct nfsd4_callback *cb)
665 {
666 	struct nfs4_layout_stateid *ls =
667 		container_of(cb, struct nfs4_layout_stateid, ls_recall);
668 
669 	mutex_lock(&ls->ls_mutex);
670 	nfs4_inc_and_copy_stateid(&ls->ls_recall_sid, &ls->ls_stid);
671 	mutex_unlock(&ls->ls_mutex);
672 	return true;
673 }
674 
675 static int
nfsd4_cb_layout_done(struct nfsd4_callback * cb,struct rpc_task * task)676 nfsd4_cb_layout_done(struct nfsd4_callback *cb, struct rpc_task *task)
677 {
678 	struct nfs4_layout_stateid *ls =
679 		container_of(cb, struct nfs4_layout_stateid, ls_recall);
680 	struct nfsd_net *nn;
681 	ktime_t now, cutoff;
682 	const struct nfsd4_layout_ops *ops;
683 	struct nfsd_file *fl;
684 
685 	trace_nfsd_cb_layout_done(&ls->ls_stid.sc_stateid, task);
686 	switch (task->tk_status) {
687 	case 0:
688 	case -NFS4ERR_DELAY:
689 		/*
690 		 * Anything left? If not, then call it done. Note that we don't
691 		 * take the spinlock since this is an optimization and nothing
692 		 * should get added until the cb counter goes to zero.
693 		 */
694 		if (list_empty(&ls->ls_layouts))
695 			return 1;
696 
697 		/* Poll the client until it's done with the layout */
698 		now = ktime_get();
699 		nn = net_generic(ls->ls_stid.sc_client->net, nfsd_net_id);
700 
701 		/* Client gets 2 lease periods to return it */
702 		cutoff = ktime_add_ns(task->tk_start,
703 					 (u64)nn->nfsd4_lease * NSEC_PER_SEC * 2);
704 
705 		if (ktime_before(now, cutoff)) {
706 			rpc_delay(task, HZ/100); /* 10 mili-seconds */
707 			return 0;
708 		}
709 		fallthrough;
710 	default:
711 		/*
712 		 * Unknown error or non-responding client, we'll need to fence.
713 		 */
714 		trace_nfsd_layout_recall_fail(&ls->ls_stid.sc_stateid);
715 		rcu_read_lock();
716 		fl = nfsd_file_get(ls->ls_file);
717 		rcu_read_unlock();
718 		if (fl) {
719 			ops = nfsd4_layout_ops[ls->ls_layout_type];
720 			if (ops->fence_client)
721 				ops->fence_client(ls, fl);
722 			else
723 				nfsd4_cb_layout_fail(ls, fl);
724 			nfsd_file_put(fl);
725 		}
726 		return 1;
727 	case -NFS4ERR_NOMATCHING_LAYOUT:
728 		trace_nfsd_layout_recall_done(&ls->ls_stid.sc_stateid);
729 		task->tk_status = 0;
730 		return 1;
731 	}
732 }
733 
734 static void
nfsd4_cb_layout_release(struct nfsd4_callback * cb)735 nfsd4_cb_layout_release(struct nfsd4_callback *cb)
736 {
737 	struct nfs4_layout_stateid *ls =
738 		container_of(cb, struct nfs4_layout_stateid, ls_recall);
739 	LIST_HEAD(reaplist);
740 
741 	trace_nfsd_layout_recall_release(&ls->ls_stid.sc_stateid);
742 
743 	nfsd4_return_all_layouts(ls, &reaplist);
744 	nfsd4_free_layouts(&reaplist);
745 	nfs4_put_stid(&ls->ls_stid);
746 }
747 
748 static const struct nfsd4_callback_ops nfsd4_cb_layout_ops = {
749 	.prepare	= nfsd4_cb_layout_prepare,
750 	.done		= nfsd4_cb_layout_done,
751 	.release	= nfsd4_cb_layout_release,
752 	.opcode		= OP_CB_LAYOUTRECALL,
753 };
754 
755 static bool
nfsd4_layout_lm_break(struct file_lease * fl)756 nfsd4_layout_lm_break(struct file_lease *fl)
757 {
758 	/*
759 	 * Enforce break lease timeout to prevent NFSD
760 	 * thread from hanging in __break_lease.
761 	 */
762 	nfsd4_recall_file_layout(fl->c.flc_owner);
763 	return false;
764 }
765 
766 static int
nfsd4_layout_lm_change(struct file_lease * onlist,int arg,struct list_head * dispose)767 nfsd4_layout_lm_change(struct file_lease *onlist, int arg,
768 		struct list_head *dispose)
769 {
770 	BUG_ON(!(arg & F_UNLCK));
771 	return lease_modify(onlist, arg, dispose);
772 }
773 
774 /**
775  *  nfsd4_layout_lm_open_conflict - see if the given file points to an inode that has
776  *				    an existing open that would conflict with the
777  *				    desired lease.
778  * @filp:	file to check
779  * @arg:	type of lease that we're trying to acquire
780  *
781  * The kernel will call into this operation to determine whether there
782  * are conflicting opens that may prevent the layout from being granted.
783  * For nfsd, that check is done at a higher level, so this trivially
784  * returns 0.
785  */
786 static int
nfsd4_layout_lm_open_conflict(struct file * filp,int arg)787 nfsd4_layout_lm_open_conflict(struct file *filp, int arg)
788 {
789 	return 0;
790 }
791 
792 static void
nfsd4_layout_fence_worker(struct work_struct * work)793 nfsd4_layout_fence_worker(struct work_struct *work)
794 {
795 	struct delayed_work *dwork = to_delayed_work(work);
796 	struct nfs4_layout_stateid *ls = container_of(dwork,
797 			struct nfs4_layout_stateid, ls_fence_work);
798 	struct nfsd_file *nf;
799 	struct block_device *bdev;
800 	struct nfs4_client *clp;
801 	struct nfsd_net *nn;
802 
803 	spin_lock(&ls->ls_lock);
804 	if (list_empty(&ls->ls_layouts)) {
805 		spin_unlock(&ls->ls_lock);
806 dispose:
807 		cancel_delayed_work(&ls->ls_fence_work);
808 		/* unlock the lease so that tasks waiting on it can proceed */
809 		nfsd4_close_layout(ls);
810 
811 		ls->ls_fenced = true;
812 		spin_lock(&ls->ls_lock);
813 		ls->ls_fence_inflight = false;
814 		spin_unlock(&ls->ls_lock);
815 		nfs4_put_stid(&ls->ls_stid);
816 		return;
817 	}
818 	spin_unlock(&ls->ls_lock);
819 
820 	rcu_read_lock();
821 	nf = nfsd_file_get(ls->ls_file);
822 	rcu_read_unlock();
823 	if (!nf)
824 		goto dispose;
825 
826 	clp = ls->ls_stid.sc_client;
827 	nn = net_generic(clp->net, nfsd_net_id);
828 	bdev = nf->nf_file->f_path.mnt->mnt_sb->s_bdev;
829 	if (nfsd4_layout_ops[ls->ls_layout_type]->fence_client(ls, nf)) {
830 		/* fenced ok */
831 		nfsd_file_put(nf);
832 		pr_warn("%s: FENCED client[%pISpc] clid[%d] to device[%s]\n",
833 			__func__, (struct sockaddr *)&clp->cl_addr,
834 			clp->cl_clientid.cl_id - nn->clientid_base,
835 			bdev->bd_disk->disk_name);
836 		goto dispose;
837 	}
838 	/* fence failed */
839 	nfsd_file_put(nf);
840 
841 	if (!clp->cl_fence_retry_warn) {
842 		pr_warn("%s: FENCE failed client[%pISpc] clid[%d] device[%s]\n",
843 			__func__, (struct sockaddr *)&clp->cl_addr,
844 			clp->cl_clientid.cl_id - nn->clientid_base,
845 			bdev->bd_disk->disk_name);
846 		clp->cl_fence_retry_warn = true;
847 	}
848 	/*
849 	 * The fence worker retries the fencing operation indefinitely to
850 	 * prevent data corruption. The admin needs to take the following
851 	 * actions to restore access to the file for other clients:
852 	 *
853 	 *  . shutdown or power off the client being fenced.
854 	 *  . manually expire the client to release all its state on the server;
855 	 *    echo 'expire' > /proc/fs/nfsd/clients/clid/ctl'.
856 	 *
857 	 *    Where:
858 	 *
859 	 *    clid: is the unique client identifier displayed in
860 	 *          the warning message above.
861 	 */
862 	if (!ls->ls_fence_delay)
863 		ls->ls_fence_delay = HZ;
864 	else
865 		ls->ls_fence_delay = min(ls->ls_fence_delay << 1,
866 					 MAX_FENCE_DELAY);
867 	mod_delayed_work(system_dfl_wq, &ls->ls_fence_work, ls->ls_fence_delay);
868 }
869 
870 /**
871  * nfsd4_layout_lm_breaker_timedout - The layout recall has timed out.
872  * @fl: file to check
873  *
874  * If the layout type supports a fence operation, schedule a worker to
875  * fence the client from accessing the block device.
876  *
877  * This function runs under the protection of the spin_lock flc_lock.
878  * At this time, the file_lease associated with the layout stateid is
879  * on the flc_list. A reference count is incremented on the layout
880  * stateid to prevent it from being freed while the fence worker is
881  * executing. Once the fence worker finishes its operation, it releases
882  * this reference.
883  *
884  * The fence worker continues to run until either the client has been
885  * fenced or the layout becomes invalid. The layout can become invalid
886  * as a result of a LAYOUTRETURN or when the CB_LAYOUT recall callback
887  * has completed.
888  *
889  * Return true if the file_lease should be disposed of by the caller;
890  * otherwise, return false.
891  */
892 static bool
nfsd4_layout_lm_breaker_timedout(struct file_lease * fl)893 nfsd4_layout_lm_breaker_timedout(struct file_lease *fl)
894 {
895 	struct nfs4_layout_stateid *ls = fl->c.flc_owner;
896 
897 	if ((!nfsd4_layout_ops[ls->ls_layout_type]->fence_client) ||
898 			ls->ls_fenced)
899 		return true;
900 	/*
901 	 * Make sure layout has not been returned yet before
902 	 * taking a reference count on the layout stateid. The
903 	 * ls_fence_inflight flag is set together with the sc_count
904 	 * increment under ls_lock so that a fence worker invocation
905 	 * already in progress (which has cleared WORK_STRUCT_PENDING
906 	 * but not yet reached dispose:) cannot be coalesced with a
907 	 * fresh schedule that takes an extra unmatched reference.
908 	 */
909 	spin_lock(&ls->ls_lock);
910 	if (ls->ls_fence_inflight) {
911 		spin_unlock(&ls->ls_lock);
912 		return false;
913 	}
914 	if (list_empty(&ls->ls_layouts) ||
915 			!refcount_inc_not_zero(&ls->ls_stid.sc_count)) {
916 		spin_unlock(&ls->ls_lock);
917 		return true;
918 	}
919 	ls->ls_fence_inflight = true;
920 	spin_unlock(&ls->ls_lock);
921 
922 	mod_delayed_work(system_dfl_wq, &ls->ls_fence_work, 0);
923 	return false;
924 }
925 
926 static const struct lease_manager_operations nfsd4_layouts_lm_ops = {
927 	.lm_break		= nfsd4_layout_lm_break,
928 	.lm_change		= nfsd4_layout_lm_change,
929 	.lm_open_conflict	= nfsd4_layout_lm_open_conflict,
930 	.lm_breaker_timedout	= nfsd4_layout_lm_breaker_timedout,
931 };
932 
933 int
nfsd4_init_pnfs(void)934 nfsd4_init_pnfs(void)
935 {
936 	int i;
937 
938 	for (i = 0; i < DEVID_HASH_SIZE; i++)
939 		INIT_LIST_HEAD(&nfsd_devid_hash[i]);
940 
941 	nfs4_layout_cache = KMEM_CACHE(nfs4_layout, 0);
942 	if (!nfs4_layout_cache)
943 		return -ENOMEM;
944 
945 	nfs4_layout_stateid_cache = KMEM_CACHE(nfs4_layout_stateid, 0);
946 	if (!nfs4_layout_stateid_cache) {
947 		kmem_cache_destroy(nfs4_layout_cache);
948 		return -ENOMEM;
949 	}
950 	return 0;
951 }
952 
953 void
nfsd4_exit_pnfs(void)954 nfsd4_exit_pnfs(void)
955 {
956 	int i;
957 
958 	kmem_cache_destroy(nfs4_layout_cache);
959 	kmem_cache_destroy(nfs4_layout_stateid_cache);
960 
961 	for (i = 0; i < DEVID_HASH_SIZE; i++) {
962 		struct nfsd4_deviceid_map *map, *n;
963 
964 		list_for_each_entry_safe(map, n, &nfsd_devid_hash[i], hash)
965 			kfree(map);
966 	}
967 }
968