xref: /linux/fs/nfsd/nfs4layouts.c (revision 8c04a6d6e07ce565928ea98ae8c534cac871af19)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
29cf514ccSChristoph Hellwig /*
39cf514ccSChristoph Hellwig  * Copyright (c) 2014 Christoph Hellwig.
49cf514ccSChristoph Hellwig  */
5f99d4fbdSChristoph Hellwig #include <linux/blkdev.h>
6c5c707f9SChristoph Hellwig #include <linux/kmod.h>
7c5c707f9SChristoph Hellwig #include <linux/file.h>
89cf514ccSChristoph Hellwig #include <linux/jhash.h>
99cf514ccSChristoph Hellwig #include <linux/sched.h>
10c5c707f9SChristoph Hellwig #include <linux/sunrpc/addr.h>
119cf514ccSChristoph Hellwig 
129cf514ccSChristoph Hellwig #include "pnfs.h"
139cf514ccSChristoph Hellwig #include "netns.h"
1431ef83dcSChristoph Hellwig #include "trace.h"
159cf514ccSChristoph Hellwig 
169cf514ccSChristoph Hellwig #define NFSDDBG_FACILITY                NFSDDBG_PNFS
179cf514ccSChristoph Hellwig 
189cf514ccSChristoph Hellwig struct nfs4_layout {
199cf514ccSChristoph Hellwig 	struct list_head		lo_perstate;
209cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid	*lo_state;
219cf514ccSChristoph Hellwig 	struct nfsd4_layout_seg		lo_seg;
229cf514ccSChristoph Hellwig };
239cf514ccSChristoph Hellwig 
249cf514ccSChristoph Hellwig static struct kmem_cache *nfs4_layout_cache;
259cf514ccSChristoph Hellwig static struct kmem_cache *nfs4_layout_stateid_cache;
269cf514ccSChristoph Hellwig 
27c4cb8974SJulia Lawall static const struct nfsd4_callback_ops nfsd4_cb_layout_ops;
28c69ff407SJeff Layton static const struct lease_manager_operations nfsd4_layouts_lm_ops;
29c5c707f9SChristoph Hellwig 
309cf514ccSChristoph Hellwig const struct nfsd4_layout_ops *nfsd4_layout_ops[LAYOUT_TYPE_MAX] =  {
319b9960a0STom Haynes #ifdef CONFIG_NFSD_FLEXFILELAYOUT
329b9960a0STom Haynes 	[LAYOUT_FLEX_FILES]	= &ff_layout_ops,
339b9960a0STom Haynes #endif
3481c39329SChristoph Hellwig #ifdef CONFIG_NFSD_BLOCKLAYOUT
358650b8a0SChristoph Hellwig 	[LAYOUT_BLOCK_VOLUME]	= &bl_layout_ops,
3681c39329SChristoph Hellwig #endif
37f99d4fbdSChristoph Hellwig #ifdef CONFIG_NFSD_SCSILAYOUT
38f99d4fbdSChristoph Hellwig 	[LAYOUT_SCSI]		= &scsi_layout_ops,
39f99d4fbdSChristoph Hellwig #endif
409cf514ccSChristoph Hellwig };
419cf514ccSChristoph Hellwig 
429cf514ccSChristoph Hellwig /* pNFS device ID to export fsid mapping */
439cf514ccSChristoph Hellwig #define DEVID_HASH_BITS	8
449cf514ccSChristoph Hellwig #define DEVID_HASH_SIZE	(1 << DEVID_HASH_BITS)
459cf514ccSChristoph Hellwig #define DEVID_HASH_MASK	(DEVID_HASH_SIZE - 1)
469cf514ccSChristoph Hellwig static u64 nfsd_devid_seq = 1;
479cf514ccSChristoph Hellwig static struct list_head nfsd_devid_hash[DEVID_HASH_SIZE];
489cf514ccSChristoph Hellwig static DEFINE_SPINLOCK(nfsd_devid_lock);
499cf514ccSChristoph Hellwig 
509cf514ccSChristoph Hellwig static inline u32 devid_hashfn(u64 idx)
519cf514ccSChristoph Hellwig {
529cf514ccSChristoph Hellwig 	return jhash_2words(idx, idx >> 32, 0) & DEVID_HASH_MASK;
539cf514ccSChristoph Hellwig }
549cf514ccSChristoph Hellwig 
559cf514ccSChristoph Hellwig static void
569cf514ccSChristoph Hellwig nfsd4_alloc_devid_map(const struct svc_fh *fhp)
579cf514ccSChristoph Hellwig {
589cf514ccSChristoph Hellwig 	const struct knfsd_fh *fh = &fhp->fh_handle;
599cf514ccSChristoph Hellwig 	size_t fsid_len = key_len(fh->fh_fsid_type);
609cf514ccSChristoph Hellwig 	struct nfsd4_deviceid_map *map, *old;
619cf514ccSChristoph Hellwig 	int i;
629cf514ccSChristoph Hellwig 
639cf514ccSChristoph Hellwig 	map = kzalloc(sizeof(*map) + fsid_len, GFP_KERNEL);
649cf514ccSChristoph Hellwig 	if (!map)
659cf514ccSChristoph Hellwig 		return;
669cf514ccSChristoph Hellwig 
679cf514ccSChristoph Hellwig 	map->fsid_type = fh->fh_fsid_type;
689cf514ccSChristoph Hellwig 	memcpy(&map->fsid, fh->fh_fsid, fsid_len);
699cf514ccSChristoph Hellwig 
709cf514ccSChristoph Hellwig 	spin_lock(&nfsd_devid_lock);
719cf514ccSChristoph Hellwig 	if (fhp->fh_export->ex_devid_map)
729cf514ccSChristoph Hellwig 		goto out_unlock;
739cf514ccSChristoph Hellwig 
749cf514ccSChristoph Hellwig 	for (i = 0; i < DEVID_HASH_SIZE; i++) {
759cf514ccSChristoph Hellwig 		list_for_each_entry(old, &nfsd_devid_hash[i], hash) {
769cf514ccSChristoph Hellwig 			if (old->fsid_type != fh->fh_fsid_type)
779cf514ccSChristoph Hellwig 				continue;
789cf514ccSChristoph Hellwig 			if (memcmp(old->fsid, fh->fh_fsid,
799cf514ccSChristoph Hellwig 					key_len(old->fsid_type)))
809cf514ccSChristoph Hellwig 				continue;
819cf514ccSChristoph Hellwig 
829cf514ccSChristoph Hellwig 			fhp->fh_export->ex_devid_map = old;
839cf514ccSChristoph Hellwig 			goto out_unlock;
849cf514ccSChristoph Hellwig 		}
859cf514ccSChristoph Hellwig 	}
869cf514ccSChristoph Hellwig 
879cf514ccSChristoph Hellwig 	map->idx = nfsd_devid_seq++;
889cf514ccSChristoph Hellwig 	list_add_tail_rcu(&map->hash, &nfsd_devid_hash[devid_hashfn(map->idx)]);
899cf514ccSChristoph Hellwig 	fhp->fh_export->ex_devid_map = map;
909cf514ccSChristoph Hellwig 	map = NULL;
919cf514ccSChristoph Hellwig 
929cf514ccSChristoph Hellwig out_unlock:
939cf514ccSChristoph Hellwig 	spin_unlock(&nfsd_devid_lock);
949cf514ccSChristoph Hellwig 	kfree(map);
959cf514ccSChristoph Hellwig }
969cf514ccSChristoph Hellwig 
979cf514ccSChristoph Hellwig struct nfsd4_deviceid_map *
989cf514ccSChristoph Hellwig nfsd4_find_devid_map(int idx)
999cf514ccSChristoph Hellwig {
1009cf514ccSChristoph Hellwig 	struct nfsd4_deviceid_map *map, *ret = NULL;
1019cf514ccSChristoph Hellwig 
1029cf514ccSChristoph Hellwig 	rcu_read_lock();
1039cf514ccSChristoph Hellwig 	list_for_each_entry_rcu(map, &nfsd_devid_hash[devid_hashfn(idx)], hash)
1049cf514ccSChristoph Hellwig 		if (map->idx == idx)
1059cf514ccSChristoph Hellwig 			ret = map;
1069cf514ccSChristoph Hellwig 	rcu_read_unlock();
1079cf514ccSChristoph Hellwig 
1089cf514ccSChristoph Hellwig 	return ret;
1099cf514ccSChristoph Hellwig }
1109cf514ccSChristoph Hellwig 
1119cf514ccSChristoph Hellwig int
1129cf514ccSChristoph Hellwig nfsd4_set_deviceid(struct nfsd4_deviceid *id, const struct svc_fh *fhp,
1139cf514ccSChristoph Hellwig 		u32 device_generation)
1149cf514ccSChristoph Hellwig {
1159cf514ccSChristoph Hellwig 	if (!fhp->fh_export->ex_devid_map) {
1169cf514ccSChristoph Hellwig 		nfsd4_alloc_devid_map(fhp);
1179cf514ccSChristoph Hellwig 		if (!fhp->fh_export->ex_devid_map)
1189cf514ccSChristoph Hellwig 			return -ENOMEM;
1199cf514ccSChristoph Hellwig 	}
1209cf514ccSChristoph Hellwig 
1219cf514ccSChristoph Hellwig 	id->fsid_idx = fhp->fh_export->ex_devid_map->idx;
1229cf514ccSChristoph Hellwig 	id->generation = device_generation;
1239cf514ccSChristoph Hellwig 	id->pad = 0;
1249cf514ccSChristoph Hellwig 	return 0;
1259cf514ccSChristoph Hellwig }
1269cf514ccSChristoph Hellwig 
1279cf514ccSChristoph Hellwig void nfsd4_setup_layout_type(struct svc_export *exp)
1289cf514ccSChristoph Hellwig {
1299b9960a0STom Haynes #if defined(CONFIG_NFSD_BLOCKLAYOUT) || defined(CONFIG_NFSD_SCSILAYOUT)
1308650b8a0SChristoph Hellwig 	struct super_block *sb = exp->ex_path.mnt->mnt_sb;
1319b9960a0STom Haynes #endif
1328650b8a0SChristoph Hellwig 
133f3f03330SChristoph Hellwig 	if (!(exp->ex_flags & NFSEXP_PNFS))
1349cf514ccSChristoph Hellwig 		return;
1358650b8a0SChristoph Hellwig 
1369b9960a0STom Haynes #ifdef CONFIG_NFSD_FLEXFILELAYOUT
1378a4c3926SJeff Layton 	exp->ex_layout_types |= 1 << LAYOUT_FLEX_FILES;
1389b9960a0STom Haynes #endif
13981c39329SChristoph Hellwig #ifdef CONFIG_NFSD_BLOCKLAYOUT
1408650b8a0SChristoph Hellwig 	if (sb->s_export_op->get_uuid &&
1418650b8a0SChristoph Hellwig 	    sb->s_export_op->map_blocks &&
1428650b8a0SChristoph Hellwig 	    sb->s_export_op->commit_blocks)
1438a4c3926SJeff Layton 		exp->ex_layout_types |= 1 << LAYOUT_BLOCK_VOLUME;
14481c39329SChristoph Hellwig #endif
145f99d4fbdSChristoph Hellwig #ifdef CONFIG_NFSD_SCSILAYOUT
146f99d4fbdSChristoph Hellwig 	if (sb->s_export_op->map_blocks &&
147f99d4fbdSChristoph Hellwig 	    sb->s_export_op->commit_blocks &&
1488c6aabd1SChristoph Hellwig 	    sb->s_bdev &&
1498c6aabd1SChristoph Hellwig 	    sb->s_bdev->bd_disk->fops->pr_ops &&
1508c6aabd1SChristoph Hellwig 	    sb->s_bdev->bd_disk->fops->get_unique_id)
1518a4c3926SJeff Layton 		exp->ex_layout_types |= 1 << LAYOUT_SCSI;
152f99d4fbdSChristoph Hellwig #endif
1539cf514ccSChristoph Hellwig }
1549cf514ccSChristoph Hellwig 
1551e33e141SNeilBrown void nfsd4_close_layout(struct nfs4_layout_stateid *ls)
1561e33e141SNeilBrown {
1571e33e141SNeilBrown 	struct nfsd_file *fl;
1581e33e141SNeilBrown 
1591e33e141SNeilBrown 	spin_lock(&ls->ls_stid.sc_file->fi_lock);
1601e33e141SNeilBrown 	fl = ls->ls_file;
1611e33e141SNeilBrown 	ls->ls_file = NULL;
1621e33e141SNeilBrown 	spin_unlock(&ls->ls_stid.sc_file->fi_lock);
1631e33e141SNeilBrown 
1641e33e141SNeilBrown 	if (fl) {
1651e33e141SNeilBrown 		if (!nfsd4_layout_ops[ls->ls_layout_type]->disable_recalls)
166a01c9fe3SLinus Torvalds 			kernel_setlease(fl->nf_file, F_UNLCK, NULL,
1671e33e141SNeilBrown 					(void **)&ls);
1681e33e141SNeilBrown 		nfsd_file_put(fl);
1691e33e141SNeilBrown 	}
1701e33e141SNeilBrown }
1711e33e141SNeilBrown 
1729cf514ccSChristoph Hellwig static void
1739cf514ccSChristoph Hellwig nfsd4_free_layout_stateid(struct nfs4_stid *stid)
1749cf514ccSChristoph Hellwig {
1759cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls = layoutstateid(stid);
1769cf514ccSChristoph Hellwig 	struct nfs4_client *clp = ls->ls_stid.sc_client;
1779cf514ccSChristoph Hellwig 	struct nfs4_file *fp = ls->ls_stid.sc_file;
1789cf514ccSChristoph Hellwig 
179f394b62bSChuck Lever 	trace_nfsd_layoutstate_free(&ls->ls_stid.sc_stateid);
18031ef83dcSChristoph Hellwig 
1819cf514ccSChristoph Hellwig 	spin_lock(&clp->cl_lock);
1829cf514ccSChristoph Hellwig 	list_del_init(&ls->ls_perclnt);
1839cf514ccSChristoph Hellwig 	spin_unlock(&clp->cl_lock);
1849cf514ccSChristoph Hellwig 
1859cf514ccSChristoph Hellwig 	spin_lock(&fp->fi_lock);
1869cf514ccSChristoph Hellwig 	list_del_init(&ls->ls_perfile);
1879cf514ccSChristoph Hellwig 	spin_unlock(&fp->fi_lock);
1889cf514ccSChristoph Hellwig 
1891e33e141SNeilBrown 	nfsd4_close_layout(ls);
190c5c707f9SChristoph Hellwig 
191c5c707f9SChristoph Hellwig 	if (ls->ls_recalled)
192c5c707f9SChristoph Hellwig 		atomic_dec(&ls->ls_stid.sc_file->fi_lo_recalls);
193c5c707f9SChristoph Hellwig 
1949cf514ccSChristoph Hellwig 	kmem_cache_free(nfs4_layout_stateid_cache, ls);
1959cf514ccSChristoph Hellwig }
1969cf514ccSChristoph Hellwig 
197c5c707f9SChristoph Hellwig static int
198c5c707f9SChristoph Hellwig nfsd4_layout_setlease(struct nfs4_layout_stateid *ls)
199c5c707f9SChristoph Hellwig {
200c69ff407SJeff Layton 	struct file_lease *fl;
201c5c707f9SChristoph Hellwig 	int status;
202c5c707f9SChristoph Hellwig 
2031983a66fSJeff Layton 	if (nfsd4_layout_ops[ls->ls_layout_type]->disable_recalls)
2041983a66fSJeff Layton 		return 0;
2051983a66fSJeff Layton 
206c69ff407SJeff Layton 	fl = locks_alloc_lease();
207c5c707f9SChristoph Hellwig 	if (!fl)
208c5c707f9SChristoph Hellwig 		return -ENOMEM;
209c69ff407SJeff Layton 	locks_init_lease(fl);
210c5c707f9SChristoph Hellwig 	fl->fl_lmops = &nfsd4_layouts_lm_ops;
21105580bbfSJeff Layton 	fl->c.flc_flags = FL_LAYOUT;
21205580bbfSJeff Layton 	fl->c.flc_type = F_RDLCK;
21305580bbfSJeff Layton 	fl->c.flc_owner = ls;
21405580bbfSJeff Layton 	fl->c.flc_pid = current->tgid;
21505580bbfSJeff Layton 	fl->c.flc_file = ls->ls_file->nf_file;
216c5c707f9SChristoph Hellwig 
2177b800101SJeff Layton 	status = kernel_setlease(fl->c.flc_file, fl->c.flc_type, &fl, NULL);
218c5c707f9SChristoph Hellwig 	if (status) {
219c69ff407SJeff Layton 		locks_free_lease(fl);
220c5c707f9SChristoph Hellwig 		return status;
221c5c707f9SChristoph Hellwig 	}
222c5c707f9SChristoph Hellwig 	BUG_ON(fl != NULL);
223c5c707f9SChristoph Hellwig 	return 0;
224c5c707f9SChristoph Hellwig }
225c5c707f9SChristoph Hellwig 
2269cf514ccSChristoph Hellwig static struct nfs4_layout_stateid *
2279cf514ccSChristoph Hellwig nfsd4_alloc_layout_stateid(struct nfsd4_compound_state *cstate,
2289cf514ccSChristoph Hellwig 		struct nfs4_stid *parent, u32 layout_type)
2299cf514ccSChristoph Hellwig {
2309cf514ccSChristoph Hellwig 	struct nfs4_client *clp = cstate->clp;
2319cf514ccSChristoph Hellwig 	struct nfs4_file *fp = parent->sc_file;
2329cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
2339cf514ccSChristoph Hellwig 	struct nfs4_stid *stp;
2349cf514ccSChristoph Hellwig 
235d19fb70dSKinglong Mee 	stp = nfs4_alloc_stid(cstate->clp, nfs4_layout_stateid_cache,
236d19fb70dSKinglong Mee 					nfsd4_free_layout_stateid);
2379cf514ccSChristoph Hellwig 	if (!stp)
2389cf514ccSChristoph Hellwig 		return NULL;
239d19fb70dSKinglong Mee 
2409cf514ccSChristoph Hellwig 	get_nfs4_file(fp);
2419cf514ccSChristoph Hellwig 	stp->sc_file = fp;
2429cf514ccSChristoph Hellwig 
2439cf514ccSChristoph Hellwig 	ls = layoutstateid(stp);
2449cf514ccSChristoph Hellwig 	INIT_LIST_HEAD(&ls->ls_perclnt);
2459cf514ccSChristoph Hellwig 	INIT_LIST_HEAD(&ls->ls_perfile);
2469cf514ccSChristoph Hellwig 	spin_lock_init(&ls->ls_lock);
2479cf514ccSChristoph Hellwig 	INIT_LIST_HEAD(&ls->ls_layouts);
248cc8a5532SJeff Layton 	mutex_init(&ls->ls_mutex);
2499cf514ccSChristoph Hellwig 	ls->ls_layout_type = layout_type;
250c5c707f9SChristoph Hellwig 	nfsd4_init_cb(&ls->ls_recall, clp, &nfsd4_cb_layout_ops,
251c5c707f9SChristoph Hellwig 			NFSPROC4_CLNT_CB_LAYOUT);
252c5c707f9SChristoph Hellwig 
2533f29cc82SNeilBrown 	if (parent->sc_type == SC_TYPE_DELEG)
254eb82dd39SJeff Layton 		ls->ls_file = nfsd_file_get(fp->fi_deleg_file);
255c5c707f9SChristoph Hellwig 	else
256c5c707f9SChristoph Hellwig 		ls->ls_file = find_any_file(fp);
257c5c707f9SChristoph Hellwig 	BUG_ON(!ls->ls_file);
258c5c707f9SChristoph Hellwig 
259c5c707f9SChristoph Hellwig 	if (nfsd4_layout_setlease(ls)) {
260eb82dd39SJeff Layton 		nfsd_file_put(ls->ls_file);
261c5c707f9SChristoph Hellwig 		put_nfs4_file(fp);
262c5c707f9SChristoph Hellwig 		kmem_cache_free(nfs4_layout_stateid_cache, ls);
263c5c707f9SChristoph Hellwig 		return NULL;
264c5c707f9SChristoph Hellwig 	}
2659cf514ccSChristoph Hellwig 
2669cf514ccSChristoph Hellwig 	spin_lock(&clp->cl_lock);
2673f29cc82SNeilBrown 	stp->sc_type = SC_TYPE_LAYOUT;
2689cf514ccSChristoph Hellwig 	list_add(&ls->ls_perclnt, &clp->cl_lo_states);
2699cf514ccSChristoph Hellwig 	spin_unlock(&clp->cl_lock);
2709cf514ccSChristoph Hellwig 
2719cf514ccSChristoph Hellwig 	spin_lock(&fp->fi_lock);
2729cf514ccSChristoph Hellwig 	list_add(&ls->ls_perfile, &fp->fi_lo_states);
2739cf514ccSChristoph Hellwig 	spin_unlock(&fp->fi_lock);
2749cf514ccSChristoph Hellwig 
275f394b62bSChuck Lever 	trace_nfsd_layoutstate_alloc(&ls->ls_stid.sc_stateid);
2769cf514ccSChristoph Hellwig 	return ls;
2779cf514ccSChristoph Hellwig }
2789cf514ccSChristoph Hellwig 
2799cf514ccSChristoph Hellwig __be32
2809cf514ccSChristoph Hellwig nfsd4_preprocess_layout_stateid(struct svc_rqst *rqstp,
2819cf514ccSChristoph Hellwig 		struct nfsd4_compound_state *cstate, stateid_t *stateid,
2829cf514ccSChristoph Hellwig 		bool create, u32 layout_type, struct nfs4_layout_stateid **lsp)
2839cf514ccSChristoph Hellwig {
2849cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
2859cf514ccSChristoph Hellwig 	struct nfs4_stid *stid;
2863f29cc82SNeilBrown 	unsigned short typemask = SC_TYPE_LAYOUT;
2879cf514ccSChristoph Hellwig 	__be32 status;
2889cf514ccSChristoph Hellwig 
2899cf514ccSChristoph Hellwig 	if (create)
2903f29cc82SNeilBrown 		typemask |= (SC_TYPE_OPEN | SC_TYPE_LOCK | SC_TYPE_DELEG);
2919cf514ccSChristoph Hellwig 
2923f29cc82SNeilBrown 	status = nfsd4_lookup_stateid(cstate, stateid, typemask, 0, &stid,
2939cf514ccSChristoph Hellwig 			net_generic(SVC_NET(rqstp), nfsd_net_id));
2949cf514ccSChristoph Hellwig 	if (status)
2959cf514ccSChristoph Hellwig 		goto out;
2969cf514ccSChristoph Hellwig 
2979cf514ccSChristoph Hellwig 	if (!fh_match(&cstate->current_fh.fh_handle,
2989cf514ccSChristoph Hellwig 		      &stid->sc_file->fi_fhandle)) {
2999cf514ccSChristoph Hellwig 		status = nfserr_bad_stateid;
3009cf514ccSChristoph Hellwig 		goto out_put_stid;
3019cf514ccSChristoph Hellwig 	}
3029cf514ccSChristoph Hellwig 
3033f29cc82SNeilBrown 	if (stid->sc_type != SC_TYPE_LAYOUT) {
3049cf514ccSChristoph Hellwig 		ls = nfsd4_alloc_layout_stateid(cstate, stid, layout_type);
3059cf514ccSChristoph Hellwig 		nfs4_put_stid(stid);
3069cf514ccSChristoph Hellwig 
3079cf514ccSChristoph Hellwig 		status = nfserr_jukebox;
3089cf514ccSChristoph Hellwig 		if (!ls)
3099cf514ccSChristoph Hellwig 			goto out;
310cc8a5532SJeff Layton 		mutex_lock(&ls->ls_mutex);
3119cf514ccSChristoph Hellwig 	} else {
3129cf514ccSChristoph Hellwig 		ls = container_of(stid, struct nfs4_layout_stateid, ls_stid);
3139cf514ccSChristoph Hellwig 
3149cf514ccSChristoph Hellwig 		status = nfserr_bad_stateid;
315cc8a5532SJeff Layton 		mutex_lock(&ls->ls_mutex);
31614b7f4a1SJeff Layton 		if (nfsd4_stateid_generation_after(stateid, &stid->sc_stateid))
317cc8a5532SJeff Layton 			goto out_unlock_stid;
3189cf514ccSChristoph Hellwig 		if (layout_type != ls->ls_layout_type)
319cc8a5532SJeff Layton 			goto out_unlock_stid;
3209cf514ccSChristoph Hellwig 	}
3219cf514ccSChristoph Hellwig 
3229cf514ccSChristoph Hellwig 	*lsp = ls;
3239cf514ccSChristoph Hellwig 	return 0;
3249cf514ccSChristoph Hellwig 
325cc8a5532SJeff Layton out_unlock_stid:
326cc8a5532SJeff Layton 	mutex_unlock(&ls->ls_mutex);
3279cf514ccSChristoph Hellwig out_put_stid:
3289cf514ccSChristoph Hellwig 	nfs4_put_stid(stid);
3299cf514ccSChristoph Hellwig out:
3309cf514ccSChristoph Hellwig 	return status;
3319cf514ccSChristoph Hellwig }
3329cf514ccSChristoph Hellwig 
333c5c707f9SChristoph Hellwig static void
334c5c707f9SChristoph Hellwig nfsd4_recall_file_layout(struct nfs4_layout_stateid *ls)
335c5c707f9SChristoph Hellwig {
336c5c707f9SChristoph Hellwig 	spin_lock(&ls->ls_lock);
337c5c707f9SChristoph Hellwig 	if (ls->ls_recalled)
338c5c707f9SChristoph Hellwig 		goto out_unlock;
339c5c707f9SChristoph Hellwig 
340c5c707f9SChristoph Hellwig 	if (list_empty(&ls->ls_layouts))
341c5c707f9SChristoph Hellwig 		goto out_unlock;
342c5c707f9SChristoph Hellwig 
343fb610c4dSBenjamin Coddington 	ls->ls_recalled = true;
344fb610c4dSBenjamin Coddington 	atomic_inc(&ls->ls_stid.sc_file->fi_lo_recalls);
345f394b62bSChuck Lever 	trace_nfsd_layout_recall(&ls->ls_stid.sc_stateid);
34631ef83dcSChristoph Hellwig 
347a15dfcd5SElena Reshetova 	refcount_inc(&ls->ls_stid.sc_count);
348c5c707f9SChristoph Hellwig 	nfsd4_run_cb(&ls->ls_recall);
349c5c707f9SChristoph Hellwig 
350c5c707f9SChristoph Hellwig out_unlock:
351c5c707f9SChristoph Hellwig 	spin_unlock(&ls->ls_lock);
352c5c707f9SChristoph Hellwig }
353c5c707f9SChristoph Hellwig 
3549cf514ccSChristoph Hellwig static inline u64
3559cf514ccSChristoph Hellwig layout_end(struct nfsd4_layout_seg *seg)
3569cf514ccSChristoph Hellwig {
3579cf514ccSChristoph Hellwig 	u64 end = seg->offset + seg->length;
3589cf514ccSChristoph Hellwig 	return end >= seg->offset ? end : NFS4_MAX_UINT64;
3599cf514ccSChristoph Hellwig }
3609cf514ccSChristoph Hellwig 
3619cf514ccSChristoph Hellwig static void
3629cf514ccSChristoph Hellwig layout_update_len(struct nfsd4_layout_seg *lo, u64 end)
3639cf514ccSChristoph Hellwig {
3649cf514ccSChristoph Hellwig 	if (end == NFS4_MAX_UINT64)
3659cf514ccSChristoph Hellwig 		lo->length = NFS4_MAX_UINT64;
3669cf514ccSChristoph Hellwig 	else
3679cf514ccSChristoph Hellwig 		lo->length = end - lo->offset;
3689cf514ccSChristoph Hellwig }
3699cf514ccSChristoph Hellwig 
3709cf514ccSChristoph Hellwig static bool
3719cf514ccSChristoph Hellwig layouts_overlapping(struct nfs4_layout *lo, struct nfsd4_layout_seg *s)
3729cf514ccSChristoph Hellwig {
3739cf514ccSChristoph Hellwig 	if (s->iomode != IOMODE_ANY && s->iomode != lo->lo_seg.iomode)
3749cf514ccSChristoph Hellwig 		return false;
3759cf514ccSChristoph Hellwig 	if (layout_end(&lo->lo_seg) <= s->offset)
3769cf514ccSChristoph Hellwig 		return false;
3779cf514ccSChristoph Hellwig 	if (layout_end(s) <= lo->lo_seg.offset)
3789cf514ccSChristoph Hellwig 		return false;
3799cf514ccSChristoph Hellwig 	return true;
3809cf514ccSChristoph Hellwig }
3819cf514ccSChristoph Hellwig 
3829cf514ccSChristoph Hellwig static bool
3839cf514ccSChristoph Hellwig layouts_try_merge(struct nfsd4_layout_seg *lo, struct nfsd4_layout_seg *new)
3849cf514ccSChristoph Hellwig {
3859cf514ccSChristoph Hellwig 	if (lo->iomode != new->iomode)
3869cf514ccSChristoph Hellwig 		return false;
3879cf514ccSChristoph Hellwig 	if (layout_end(new) < lo->offset)
3889cf514ccSChristoph Hellwig 		return false;
3899cf514ccSChristoph Hellwig 	if (layout_end(lo) < new->offset)
3909cf514ccSChristoph Hellwig 		return false;
3919cf514ccSChristoph Hellwig 
3929cf514ccSChristoph Hellwig 	lo->offset = min(lo->offset, new->offset);
3939cf514ccSChristoph Hellwig 	layout_update_len(lo, max(layout_end(lo), layout_end(new)));
3949cf514ccSChristoph Hellwig 	return true;
3959cf514ccSChristoph Hellwig }
3969cf514ccSChristoph Hellwig 
397c5c707f9SChristoph Hellwig static __be32
398c5c707f9SChristoph Hellwig nfsd4_recall_conflict(struct nfs4_layout_stateid *ls)
399c5c707f9SChristoph Hellwig {
400c5c707f9SChristoph Hellwig 	struct nfs4_file *fp = ls->ls_stid.sc_file;
401c5c707f9SChristoph Hellwig 	struct nfs4_layout_stateid *l, *n;
402c5c707f9SChristoph Hellwig 	__be32 nfserr = nfs_ok;
403c5c707f9SChristoph Hellwig 
404c5c707f9SChristoph Hellwig 	assert_spin_locked(&fp->fi_lock);
405c5c707f9SChristoph Hellwig 
406c5c707f9SChristoph Hellwig 	list_for_each_entry_safe(l, n, &fp->fi_lo_states, ls_perfile) {
407c5c707f9SChristoph Hellwig 		if (l != ls) {
408c5c707f9SChristoph Hellwig 			nfsd4_recall_file_layout(l);
409c5c707f9SChristoph Hellwig 			nfserr = nfserr_recallconflict;
410c5c707f9SChristoph Hellwig 		}
411c5c707f9SChristoph Hellwig 	}
412c5c707f9SChristoph Hellwig 
413c5c707f9SChristoph Hellwig 	return nfserr;
414c5c707f9SChristoph Hellwig }
415c5c707f9SChristoph Hellwig 
4169cf514ccSChristoph Hellwig __be32
4179cf514ccSChristoph Hellwig nfsd4_insert_layout(struct nfsd4_layoutget *lgp, struct nfs4_layout_stateid *ls)
4189cf514ccSChristoph Hellwig {
4199cf514ccSChristoph Hellwig 	struct nfsd4_layout_seg *seg = &lgp->lg_seg;
420c5c707f9SChristoph Hellwig 	struct nfs4_file *fp = ls->ls_stid.sc_file;
4219cf514ccSChristoph Hellwig 	struct nfs4_layout *lp, *new = NULL;
422c5c707f9SChristoph Hellwig 	__be32 nfserr;
4239cf514ccSChristoph Hellwig 
424c5c707f9SChristoph Hellwig 	spin_lock(&fp->fi_lock);
425c5c707f9SChristoph Hellwig 	nfserr = nfsd4_recall_conflict(ls);
426c5c707f9SChristoph Hellwig 	if (nfserr)
427c5c707f9SChristoph Hellwig 		goto out;
4289cf514ccSChristoph Hellwig 	spin_lock(&ls->ls_lock);
4299cf514ccSChristoph Hellwig 	list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
4309cf514ccSChristoph Hellwig 		if (layouts_try_merge(&lp->lo_seg, seg))
4319cf514ccSChristoph Hellwig 			goto done;
4329cf514ccSChristoph Hellwig 	}
4339cf514ccSChristoph Hellwig 	spin_unlock(&ls->ls_lock);
434c5c707f9SChristoph Hellwig 	spin_unlock(&fp->fi_lock);
4359cf514ccSChristoph Hellwig 
4369cf514ccSChristoph Hellwig 	new = kmem_cache_alloc(nfs4_layout_cache, GFP_KERNEL);
4379cf514ccSChristoph Hellwig 	if (!new)
4389cf514ccSChristoph Hellwig 		return nfserr_jukebox;
4394fc5f534SJakob Koschel 	memcpy(&new->lo_seg, seg, sizeof(new->lo_seg));
4409cf514ccSChristoph Hellwig 	new->lo_state = ls;
4419cf514ccSChristoph Hellwig 
442c5c707f9SChristoph Hellwig 	spin_lock(&fp->fi_lock);
443c5c707f9SChristoph Hellwig 	nfserr = nfsd4_recall_conflict(ls);
444c5c707f9SChristoph Hellwig 	if (nfserr)
445c5c707f9SChristoph Hellwig 		goto out;
4469cf514ccSChristoph Hellwig 	spin_lock(&ls->ls_lock);
4479cf514ccSChristoph Hellwig 	list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
4489cf514ccSChristoph Hellwig 		if (layouts_try_merge(&lp->lo_seg, seg))
4499cf514ccSChristoph Hellwig 			goto done;
4509cf514ccSChristoph Hellwig 	}
4519cf514ccSChristoph Hellwig 
452a15dfcd5SElena Reshetova 	refcount_inc(&ls->ls_stid.sc_count);
4539cf514ccSChristoph Hellwig 	list_add_tail(&new->lo_perstate, &ls->ls_layouts);
4549cf514ccSChristoph Hellwig 	new = NULL;
4559cf514ccSChristoph Hellwig done:
4569767feb2SJeff Layton 	nfs4_inc_and_copy_stateid(&lgp->lg_sid, &ls->ls_stid);
4579cf514ccSChristoph Hellwig 	spin_unlock(&ls->ls_lock);
458c5c707f9SChristoph Hellwig out:
459c5c707f9SChristoph Hellwig 	spin_unlock(&fp->fi_lock);
4609cf514ccSChristoph Hellwig 	if (new)
4619cf514ccSChristoph Hellwig 		kmem_cache_free(nfs4_layout_cache, new);
462c5c707f9SChristoph Hellwig 	return nfserr;
4639cf514ccSChristoph Hellwig }
4649cf514ccSChristoph Hellwig 
4659cf514ccSChristoph Hellwig static void
4669cf514ccSChristoph Hellwig nfsd4_free_layouts(struct list_head *reaplist)
4679cf514ccSChristoph Hellwig {
4689cf514ccSChristoph Hellwig 	while (!list_empty(reaplist)) {
4699cf514ccSChristoph Hellwig 		struct nfs4_layout *lp = list_first_entry(reaplist,
4709cf514ccSChristoph Hellwig 				struct nfs4_layout, lo_perstate);
4719cf514ccSChristoph Hellwig 
4729cf514ccSChristoph Hellwig 		list_del(&lp->lo_perstate);
4739cf514ccSChristoph Hellwig 		nfs4_put_stid(&lp->lo_state->ls_stid);
4749cf514ccSChristoph Hellwig 		kmem_cache_free(nfs4_layout_cache, lp);
4759cf514ccSChristoph Hellwig 	}
4769cf514ccSChristoph Hellwig }
4779cf514ccSChristoph Hellwig 
4789cf514ccSChristoph Hellwig static void
4799cf514ccSChristoph Hellwig nfsd4_return_file_layout(struct nfs4_layout *lp, struct nfsd4_layout_seg *seg,
4809cf514ccSChristoph Hellwig 		struct list_head *reaplist)
4819cf514ccSChristoph Hellwig {
4829cf514ccSChristoph Hellwig 	struct nfsd4_layout_seg *lo = &lp->lo_seg;
4839cf514ccSChristoph Hellwig 	u64 end = layout_end(lo);
4849cf514ccSChristoph Hellwig 
4859cf514ccSChristoph Hellwig 	if (seg->offset <= lo->offset) {
4869cf514ccSChristoph Hellwig 		if (layout_end(seg) >= end) {
4879cf514ccSChristoph Hellwig 			list_move_tail(&lp->lo_perstate, reaplist);
4889cf514ccSChristoph Hellwig 			return;
4899cf514ccSChristoph Hellwig 		}
4907890203dSKinglong Mee 		lo->offset = layout_end(seg);
4919cf514ccSChristoph Hellwig 	} else {
4929cf514ccSChristoph Hellwig 		/* retain the whole layout segment on a split. */
4939cf514ccSChristoph Hellwig 		if (layout_end(seg) < end) {
4949cf514ccSChristoph Hellwig 			dprintk("%s: split not supported\n", __func__);
4959cf514ccSChristoph Hellwig 			return;
4969cf514ccSChristoph Hellwig 		}
4977890203dSKinglong Mee 		end = seg->offset;
4989cf514ccSChristoph Hellwig 	}
4999cf514ccSChristoph Hellwig 
5009cf514ccSChristoph Hellwig 	layout_update_len(lo, end);
5019cf514ccSChristoph Hellwig }
5029cf514ccSChristoph Hellwig 
5039cf514ccSChristoph Hellwig __be32
5049cf514ccSChristoph Hellwig nfsd4_return_file_layouts(struct svc_rqst *rqstp,
5059cf514ccSChristoph Hellwig 		struct nfsd4_compound_state *cstate,
5069cf514ccSChristoph Hellwig 		struct nfsd4_layoutreturn *lrp)
5079cf514ccSChristoph Hellwig {
5089cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
5099cf514ccSChristoph Hellwig 	struct nfs4_layout *lp, *n;
5109cf514ccSChristoph Hellwig 	LIST_HEAD(reaplist);
5119cf514ccSChristoph Hellwig 	__be32 nfserr;
5129cf514ccSChristoph Hellwig 	int found = 0;
5139cf514ccSChristoph Hellwig 
5149cf514ccSChristoph Hellwig 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lrp->lr_sid,
5159cf514ccSChristoph Hellwig 						false, lrp->lr_layout_type,
5169cf514ccSChristoph Hellwig 						&ls);
51731ef83dcSChristoph Hellwig 	if (nfserr) {
518f394b62bSChuck Lever 		trace_nfsd_layout_return_lookup_fail(&lrp->lr_sid);
5199cf514ccSChristoph Hellwig 		return nfserr;
52031ef83dcSChristoph Hellwig 	}
5219cf514ccSChristoph Hellwig 
5229cf514ccSChristoph Hellwig 	spin_lock(&ls->ls_lock);
5239cf514ccSChristoph Hellwig 	list_for_each_entry_safe(lp, n, &ls->ls_layouts, lo_perstate) {
5249cf514ccSChristoph Hellwig 		if (layouts_overlapping(lp, &lrp->lr_seg)) {
5259cf514ccSChristoph Hellwig 			nfsd4_return_file_layout(lp, &lrp->lr_seg, &reaplist);
5269cf514ccSChristoph Hellwig 			found++;
5279cf514ccSChristoph Hellwig 		}
5289cf514ccSChristoph Hellwig 	}
5299cf514ccSChristoph Hellwig 	if (!list_empty(&ls->ls_layouts)) {
5309767feb2SJeff Layton 		if (found)
5319767feb2SJeff Layton 			nfs4_inc_and_copy_stateid(&lrp->lr_sid, &ls->ls_stid);
53285dbc978SChuck Lever 		lrp->lrs_present = true;
5339cf514ccSChristoph Hellwig 	} else {
534f394b62bSChuck Lever 		trace_nfsd_layoutstate_unhash(&ls->ls_stid.sc_stateid);
5353f29cc82SNeilBrown 		ls->ls_stid.sc_status |= SC_STATUS_CLOSED;
53685dbc978SChuck Lever 		lrp->lrs_present = false;
5379cf514ccSChristoph Hellwig 	}
5389cf514ccSChristoph Hellwig 	spin_unlock(&ls->ls_lock);
5399cf514ccSChristoph Hellwig 
540cc8a5532SJeff Layton 	mutex_unlock(&ls->ls_mutex);
5419cf514ccSChristoph Hellwig 	nfs4_put_stid(&ls->ls_stid);
5429cf514ccSChristoph Hellwig 	nfsd4_free_layouts(&reaplist);
5439cf514ccSChristoph Hellwig 	return nfs_ok;
5449cf514ccSChristoph Hellwig }
5459cf514ccSChristoph Hellwig 
5469cf514ccSChristoph Hellwig __be32
5479cf514ccSChristoph Hellwig nfsd4_return_client_layouts(struct svc_rqst *rqstp,
5489cf514ccSChristoph Hellwig 		struct nfsd4_compound_state *cstate,
5499cf514ccSChristoph Hellwig 		struct nfsd4_layoutreturn *lrp)
5509cf514ccSChristoph Hellwig {
5519cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls, *n;
5529cf514ccSChristoph Hellwig 	struct nfs4_client *clp = cstate->clp;
5539cf514ccSChristoph Hellwig 	struct nfs4_layout *lp, *t;
5549cf514ccSChristoph Hellwig 	LIST_HEAD(reaplist);
5559cf514ccSChristoph Hellwig 
55685dbc978SChuck Lever 	lrp->lrs_present = false;
5579cf514ccSChristoph Hellwig 
5589cf514ccSChristoph Hellwig 	spin_lock(&clp->cl_lock);
5599cf514ccSChristoph Hellwig 	list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt) {
5606f8f28ecSKinglong Mee 		if (ls->ls_layout_type != lrp->lr_layout_type)
5616f8f28ecSKinglong Mee 			continue;
5626f8f28ecSKinglong Mee 
5639cf514ccSChristoph Hellwig 		if (lrp->lr_return_type == RETURN_FSID &&
5649cf514ccSChristoph Hellwig 		    !fh_fsid_match(&ls->ls_stid.sc_file->fi_fhandle,
5659cf514ccSChristoph Hellwig 				   &cstate->current_fh.fh_handle))
5669cf514ccSChristoph Hellwig 			continue;
5679cf514ccSChristoph Hellwig 
5689cf514ccSChristoph Hellwig 		spin_lock(&ls->ls_lock);
5699cf514ccSChristoph Hellwig 		list_for_each_entry_safe(lp, t, &ls->ls_layouts, lo_perstate) {
5709cf514ccSChristoph Hellwig 			if (lrp->lr_seg.iomode == IOMODE_ANY ||
5719cf514ccSChristoph Hellwig 			    lrp->lr_seg.iomode == lp->lo_seg.iomode)
5729cf514ccSChristoph Hellwig 				list_move_tail(&lp->lo_perstate, &reaplist);
5739cf514ccSChristoph Hellwig 		}
5749cf514ccSChristoph Hellwig 		spin_unlock(&ls->ls_lock);
5759cf514ccSChristoph Hellwig 	}
5769cf514ccSChristoph Hellwig 	spin_unlock(&clp->cl_lock);
5779cf514ccSChristoph Hellwig 
5789cf514ccSChristoph Hellwig 	nfsd4_free_layouts(&reaplist);
5799cf514ccSChristoph Hellwig 	return 0;
5809cf514ccSChristoph Hellwig }
5819cf514ccSChristoph Hellwig 
5829cf514ccSChristoph Hellwig static void
5839cf514ccSChristoph Hellwig nfsd4_return_all_layouts(struct nfs4_layout_stateid *ls,
5849cf514ccSChristoph Hellwig 		struct list_head *reaplist)
5859cf514ccSChristoph Hellwig {
5869cf514ccSChristoph Hellwig 	spin_lock(&ls->ls_lock);
5879cf514ccSChristoph Hellwig 	list_splice_init(&ls->ls_layouts, reaplist);
5889cf514ccSChristoph Hellwig 	spin_unlock(&ls->ls_lock);
5899cf514ccSChristoph Hellwig }
5909cf514ccSChristoph Hellwig 
5919cf514ccSChristoph Hellwig void
5929cf514ccSChristoph Hellwig nfsd4_return_all_client_layouts(struct nfs4_client *clp)
5939cf514ccSChristoph Hellwig {
5949cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls, *n;
5959cf514ccSChristoph Hellwig 	LIST_HEAD(reaplist);
5969cf514ccSChristoph Hellwig 
5979cf514ccSChristoph Hellwig 	spin_lock(&clp->cl_lock);
5989cf514ccSChristoph Hellwig 	list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt)
5999cf514ccSChristoph Hellwig 		nfsd4_return_all_layouts(ls, &reaplist);
6009cf514ccSChristoph Hellwig 	spin_unlock(&clp->cl_lock);
6019cf514ccSChristoph Hellwig 
6029cf514ccSChristoph Hellwig 	nfsd4_free_layouts(&reaplist);
6039cf514ccSChristoph Hellwig }
6049cf514ccSChristoph Hellwig 
6059cf514ccSChristoph Hellwig void
6069cf514ccSChristoph Hellwig nfsd4_return_all_file_layouts(struct nfs4_client *clp, struct nfs4_file *fp)
6079cf514ccSChristoph Hellwig {
6089cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls, *n;
6099cf514ccSChristoph Hellwig 	LIST_HEAD(reaplist);
6109cf514ccSChristoph Hellwig 
6119cf514ccSChristoph Hellwig 	spin_lock(&fp->fi_lock);
6129cf514ccSChristoph Hellwig 	list_for_each_entry_safe(ls, n, &fp->fi_lo_states, ls_perfile) {
6139cf514ccSChristoph Hellwig 		if (ls->ls_stid.sc_client == clp)
6149cf514ccSChristoph Hellwig 			nfsd4_return_all_layouts(ls, &reaplist);
6159cf514ccSChristoph Hellwig 	}
6169cf514ccSChristoph Hellwig 	spin_unlock(&fp->fi_lock);
6179cf514ccSChristoph Hellwig 
6189cf514ccSChristoph Hellwig 	nfsd4_free_layouts(&reaplist);
6199cf514ccSChristoph Hellwig }
6209cf514ccSChristoph Hellwig 
621c5c707f9SChristoph Hellwig static void
6221e33e141SNeilBrown nfsd4_cb_layout_fail(struct nfs4_layout_stateid *ls, struct nfsd_file *file)
623c5c707f9SChristoph Hellwig {
624c5c707f9SChristoph Hellwig 	struct nfs4_client *clp = ls->ls_stid.sc_client;
625c5c707f9SChristoph Hellwig 	char addr_str[INET6_ADDRSTRLEN];
626377e7a27SGreg Kroah-Hartman 	static char const nfsd_recall_failed[] = "/sbin/nfsd-recall-failed";
627c5c707f9SChristoph Hellwig 	static char *envp[] = {
628c5c707f9SChristoph Hellwig 		"HOME=/",
629c5c707f9SChristoph Hellwig 		"TERM=linux",
630c5c707f9SChristoph Hellwig 		"PATH=/sbin:/usr/sbin:/bin:/usr/bin",
631c5c707f9SChristoph Hellwig 		NULL
632c5c707f9SChristoph Hellwig 	};
633c5c707f9SChristoph Hellwig 	char *argv[8];
634c5c707f9SChristoph Hellwig 	int error;
635c5c707f9SChristoph Hellwig 
636c5c707f9SChristoph Hellwig 	rpc_ntop((struct sockaddr *)&clp->cl_addr, addr_str, sizeof(addr_str));
637c5c707f9SChristoph Hellwig 
638c5c707f9SChristoph Hellwig 	printk(KERN_WARNING
639c5c707f9SChristoph Hellwig 		"nfsd: client %s failed to respond to layout recall. "
640c5c707f9SChristoph Hellwig 		"  Fencing..\n", addr_str);
641c5c707f9SChristoph Hellwig 
642377e7a27SGreg Kroah-Hartman 	argv[0] = (char *)nfsd_recall_failed;
643c5c707f9SChristoph Hellwig 	argv[1] = addr_str;
6441e33e141SNeilBrown 	argv[2] = file->nf_file->f_path.mnt->mnt_sb->s_id;
645c5c707f9SChristoph Hellwig 	argv[3] = NULL;
646c5c707f9SChristoph Hellwig 
647377e7a27SGreg Kroah-Hartman 	error = call_usermodehelper(nfsd_recall_failed, argv, envp,
648377e7a27SGreg Kroah-Hartman 				    UMH_WAIT_PROC);
649c5c707f9SChristoph Hellwig 	if (error) {
650c5c707f9SChristoph Hellwig 		printk(KERN_ERR "nfsd: fence failed for client %s: %d!\n",
651c5c707f9SChristoph Hellwig 			addr_str, error);
652c5c707f9SChristoph Hellwig 	}
653c5c707f9SChristoph Hellwig }
654c5c707f9SChristoph Hellwig 
655cc8a5532SJeff Layton static void
656cc8a5532SJeff Layton nfsd4_cb_layout_prepare(struct nfsd4_callback *cb)
657cc8a5532SJeff Layton {
658cc8a5532SJeff Layton 	struct nfs4_layout_stateid *ls =
659cc8a5532SJeff Layton 		container_of(cb, struct nfs4_layout_stateid, ls_recall);
660cc8a5532SJeff Layton 
661cc8a5532SJeff Layton 	mutex_lock(&ls->ls_mutex);
6629767feb2SJeff Layton 	nfs4_inc_and_copy_stateid(&ls->ls_recall_sid, &ls->ls_stid);
663be20aa00SJeff Layton 	mutex_unlock(&ls->ls_mutex);
664cc8a5532SJeff Layton }
665cc8a5532SJeff Layton 
666c5c707f9SChristoph Hellwig static int
667c5c707f9SChristoph Hellwig nfsd4_cb_layout_done(struct nfsd4_callback *cb, struct rpc_task *task)
668c5c707f9SChristoph Hellwig {
669c5c707f9SChristoph Hellwig 	struct nfs4_layout_stateid *ls =
670c5c707f9SChristoph Hellwig 		container_of(cb, struct nfs4_layout_stateid, ls_recall);
6716b9b2107SJeff Layton 	struct nfsd_net *nn;
6726b9b2107SJeff Layton 	ktime_t now, cutoff;
673f99d4fbdSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
6741e33e141SNeilBrown 	struct nfsd_file *fl;
675c5c707f9SChristoph Hellwig 
6761035d654SChuck Lever 	trace_nfsd_cb_layout_done(&ls->ls_stid.sc_stateid, task);
677c5c707f9SChristoph Hellwig 	switch (task->tk_status) {
678c5c707f9SChristoph Hellwig 	case 0:
6796b9b2107SJeff Layton 	case -NFS4ERR_DELAY:
6806b9b2107SJeff Layton 		/*
6816b9b2107SJeff Layton 		 * Anything left? If not, then call it done. Note that we don't
6826b9b2107SJeff Layton 		 * take the spinlock since this is an optimization and nothing
6836b9b2107SJeff Layton 		 * should get added until the cb counter goes to zero.
6846b9b2107SJeff Layton 		 */
6856b9b2107SJeff Layton 		if (list_empty(&ls->ls_layouts))
686c5c707f9SChristoph Hellwig 			return 1;
6876b9b2107SJeff Layton 
6886b9b2107SJeff Layton 		/* Poll the client until it's done with the layout */
6896b9b2107SJeff Layton 		now = ktime_get();
6906b9b2107SJeff Layton 		nn = net_generic(ls->ls_stid.sc_client->net, nfsd_net_id);
6916b9b2107SJeff Layton 
6926b9b2107SJeff Layton 		/* Client gets 2 lease periods to return it */
6936b9b2107SJeff Layton 		cutoff = ktime_add_ns(task->tk_start,
6942561c92bSArnd Bergmann 					 (u64)nn->nfsd4_lease * NSEC_PER_SEC * 2);
6956b9b2107SJeff Layton 
6966b9b2107SJeff Layton 		if (ktime_before(now, cutoff)) {
6976b9b2107SJeff Layton 			rpc_delay(task, HZ/100); /* 10 mili-seconds */
6986b9b2107SJeff Layton 			return 0;
6996b9b2107SJeff Layton 		}
700df561f66SGustavo A. R. Silva 		fallthrough;
701c5c707f9SChristoph Hellwig 	default:
702c5c707f9SChristoph Hellwig 		/*
703c5c707f9SChristoph Hellwig 		 * Unknown error or non-responding client, we'll need to fence.
704c5c707f9SChristoph Hellwig 		 */
705f394b62bSChuck Lever 		trace_nfsd_layout_recall_fail(&ls->ls_stid.sc_stateid);
7061e33e141SNeilBrown 		rcu_read_lock();
7071e33e141SNeilBrown 		fl = nfsd_file_get(ls->ls_file);
7081e33e141SNeilBrown 		rcu_read_unlock();
7091e33e141SNeilBrown 		if (fl) {
710f99d4fbdSChristoph Hellwig 			ops = nfsd4_layout_ops[ls->ls_layout_type];
711f99d4fbdSChristoph Hellwig 			if (ops->fence_client)
7121e33e141SNeilBrown 				ops->fence_client(ls, fl);
713f99d4fbdSChristoph Hellwig 			else
7141e33e141SNeilBrown 				nfsd4_cb_layout_fail(ls, fl);
7151e33e141SNeilBrown 			nfsd_file_put(fl);
7161e33e141SNeilBrown 		}
7171c73b9d2SScott Mayhew 		return 1;
718851238a2SJeff Layton 	case -NFS4ERR_NOMATCHING_LAYOUT:
719f394b62bSChuck Lever 		trace_nfsd_layout_recall_done(&ls->ls_stid.sc_stateid);
720851238a2SJeff Layton 		task->tk_status = 0;
721851238a2SJeff Layton 		return 1;
722c5c707f9SChristoph Hellwig 	}
723c5c707f9SChristoph Hellwig }
724c5c707f9SChristoph Hellwig 
725c5c707f9SChristoph Hellwig static void
726c5c707f9SChristoph Hellwig nfsd4_cb_layout_release(struct nfsd4_callback *cb)
727c5c707f9SChristoph Hellwig {
728c5c707f9SChristoph Hellwig 	struct nfs4_layout_stateid *ls =
729c5c707f9SChristoph Hellwig 		container_of(cb, struct nfs4_layout_stateid, ls_recall);
730c5c707f9SChristoph Hellwig 	LIST_HEAD(reaplist);
731c5c707f9SChristoph Hellwig 
732f394b62bSChuck Lever 	trace_nfsd_layout_recall_release(&ls->ls_stid.sc_stateid);
73331ef83dcSChristoph Hellwig 
734c5c707f9SChristoph Hellwig 	nfsd4_return_all_layouts(ls, &reaplist);
735c5c707f9SChristoph Hellwig 	nfsd4_free_layouts(&reaplist);
736c5c707f9SChristoph Hellwig 	nfs4_put_stid(&ls->ls_stid);
737c5c707f9SChristoph Hellwig }
738c5c707f9SChristoph Hellwig 
739c4cb8974SJulia Lawall static const struct nfsd4_callback_ops nfsd4_cb_layout_ops = {
740cc8a5532SJeff Layton 	.prepare	= nfsd4_cb_layout_prepare,
741c5c707f9SChristoph Hellwig 	.done		= nfsd4_cb_layout_done,
742c5c707f9SChristoph Hellwig 	.release	= nfsd4_cb_layout_release,
743*c1c9f3eaSJeff Layton 	.opcode		= OP_CB_LAYOUTRECALL,
744c5c707f9SChristoph Hellwig };
745c5c707f9SChristoph Hellwig 
746c5c707f9SChristoph Hellwig static bool
747c69ff407SJeff Layton nfsd4_layout_lm_break(struct file_lease *fl)
748c5c707f9SChristoph Hellwig {
749c5c707f9SChristoph Hellwig 	/*
750c5c707f9SChristoph Hellwig 	 * We don't want the locks code to timeout the lease for us;
751c5c707f9SChristoph Hellwig 	 * we'll remove it ourself if a layout isn't returned
752c5c707f9SChristoph Hellwig 	 * in time:
753c5c707f9SChristoph Hellwig 	 */
754c5c707f9SChristoph Hellwig 	fl->fl_break_time = 0;
75505580bbfSJeff Layton 	nfsd4_recall_file_layout(fl->c.flc_owner);
756c5c707f9SChristoph Hellwig 	return false;
757c5c707f9SChristoph Hellwig }
758c5c707f9SChristoph Hellwig 
759c5c707f9SChristoph Hellwig static int
760c69ff407SJeff Layton nfsd4_layout_lm_change(struct file_lease *onlist, int arg,
761c5c707f9SChristoph Hellwig 		struct list_head *dispose)
762c5c707f9SChristoph Hellwig {
763c5c707f9SChristoph Hellwig 	BUG_ON(!(arg & F_UNLCK));
764c5c707f9SChristoph Hellwig 	return lease_modify(onlist, arg, dispose);
765c5c707f9SChristoph Hellwig }
766c5c707f9SChristoph Hellwig 
767c69ff407SJeff Layton static const struct lease_manager_operations nfsd4_layouts_lm_ops = {
768c5c707f9SChristoph Hellwig 	.lm_break	= nfsd4_layout_lm_break,
769c5c707f9SChristoph Hellwig 	.lm_change	= nfsd4_layout_lm_change,
770c5c707f9SChristoph Hellwig };
771c5c707f9SChristoph Hellwig 
7729cf514ccSChristoph Hellwig int
7739cf514ccSChristoph Hellwig nfsd4_init_pnfs(void)
7749cf514ccSChristoph Hellwig {
7759cf514ccSChristoph Hellwig 	int i;
7769cf514ccSChristoph Hellwig 
7779cf514ccSChristoph Hellwig 	for (i = 0; i < DEVID_HASH_SIZE; i++)
7789cf514ccSChristoph Hellwig 		INIT_LIST_HEAD(&nfsd_devid_hash[i]);
7799cf514ccSChristoph Hellwig 
78010bcc2f1SKunwu Chan 	nfs4_layout_cache = KMEM_CACHE(nfs4_layout, 0);
7819cf514ccSChristoph Hellwig 	if (!nfs4_layout_cache)
7829cf514ccSChristoph Hellwig 		return -ENOMEM;
7839cf514ccSChristoph Hellwig 
78410bcc2f1SKunwu Chan 	nfs4_layout_stateid_cache = KMEM_CACHE(nfs4_layout_stateid, 0);
7859cf514ccSChristoph Hellwig 	if (!nfs4_layout_stateid_cache) {
7869cf514ccSChristoph Hellwig 		kmem_cache_destroy(nfs4_layout_cache);
7879cf514ccSChristoph Hellwig 		return -ENOMEM;
7889cf514ccSChristoph Hellwig 	}
7899cf514ccSChristoph Hellwig 	return 0;
7909cf514ccSChristoph Hellwig }
7919cf514ccSChristoph Hellwig 
7929cf514ccSChristoph Hellwig void
7939cf514ccSChristoph Hellwig nfsd4_exit_pnfs(void)
7949cf514ccSChristoph Hellwig {
7959cf514ccSChristoph Hellwig 	int i;
7969cf514ccSChristoph Hellwig 
7979cf514ccSChristoph Hellwig 	kmem_cache_destroy(nfs4_layout_cache);
7989cf514ccSChristoph Hellwig 	kmem_cache_destroy(nfs4_layout_stateid_cache);
7999cf514ccSChristoph Hellwig 
8009cf514ccSChristoph Hellwig 	for (i = 0; i < DEVID_HASH_SIZE; i++) {
8019cf514ccSChristoph Hellwig 		struct nfsd4_deviceid_map *map, *n;
8029cf514ccSChristoph Hellwig 
8039cf514ccSChristoph Hellwig 		list_for_each_entry_safe(map, n, &nfsd_devid_hash[i], hash)
8049cf514ccSChristoph Hellwig 			kfree(map);
8059cf514ccSChristoph Hellwig 	}
8069cf514ccSChristoph Hellwig }
807