xref: /linux/fs/nfsd/nfs4layouts.c (revision 10bcc2f1c875973f8c6ee295a827ace58bd61648)
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;
28c5c707f9SChristoph Hellwig static const struct lock_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)
1661e33e141SNeilBrown 			vfs_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 {
200c5c707f9SChristoph Hellwig 	struct file_lock *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 
206c5c707f9SChristoph Hellwig 	fl = locks_alloc_lock();
207c5c707f9SChristoph Hellwig 	if (!fl)
208c5c707f9SChristoph Hellwig 		return -ENOMEM;
209c5c707f9SChristoph Hellwig 	locks_init_lock(fl);
210c5c707f9SChristoph Hellwig 	fl->fl_lmops = &nfsd4_layouts_lm_ops;
211c5c707f9SChristoph Hellwig 	fl->fl_flags = FL_LAYOUT;
212c5c707f9SChristoph Hellwig 	fl->fl_type = F_RDLCK;
213c5c707f9SChristoph Hellwig 	fl->fl_end = OFFSET_MAX;
214c5c707f9SChristoph Hellwig 	fl->fl_owner = ls;
215c5c707f9SChristoph Hellwig 	fl->fl_pid = current->tgid;
216eb82dd39SJeff Layton 	fl->fl_file = ls->ls_file->nf_file;
217c5c707f9SChristoph Hellwig 
218c5c707f9SChristoph Hellwig 	status = vfs_setlease(fl->fl_file, fl->fl_type, &fl, NULL);
219c5c707f9SChristoph Hellwig 	if (status) {
220c5c707f9SChristoph Hellwig 		locks_free_lock(fl);
221c5c707f9SChristoph Hellwig 		return status;
222c5c707f9SChristoph Hellwig 	}
223c5c707f9SChristoph Hellwig 	BUG_ON(fl != NULL);
224c5c707f9SChristoph Hellwig 	return 0;
225c5c707f9SChristoph Hellwig }
226c5c707f9SChristoph Hellwig 
2279cf514ccSChristoph Hellwig static struct nfs4_layout_stateid *
2289cf514ccSChristoph Hellwig nfsd4_alloc_layout_stateid(struct nfsd4_compound_state *cstate,
2299cf514ccSChristoph Hellwig 		struct nfs4_stid *parent, u32 layout_type)
2309cf514ccSChristoph Hellwig {
2319cf514ccSChristoph Hellwig 	struct nfs4_client *clp = cstate->clp;
2329cf514ccSChristoph Hellwig 	struct nfs4_file *fp = parent->sc_file;
2339cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
2349cf514ccSChristoph Hellwig 	struct nfs4_stid *stp;
2359cf514ccSChristoph Hellwig 
236d19fb70dSKinglong Mee 	stp = nfs4_alloc_stid(cstate->clp, nfs4_layout_stateid_cache,
237d19fb70dSKinglong Mee 					nfsd4_free_layout_stateid);
2389cf514ccSChristoph Hellwig 	if (!stp)
2399cf514ccSChristoph Hellwig 		return NULL;
240d19fb70dSKinglong Mee 
2419cf514ccSChristoph Hellwig 	get_nfs4_file(fp);
2429cf514ccSChristoph Hellwig 	stp->sc_file = fp;
2439cf514ccSChristoph Hellwig 
2449cf514ccSChristoph Hellwig 	ls = layoutstateid(stp);
2459cf514ccSChristoph Hellwig 	INIT_LIST_HEAD(&ls->ls_perclnt);
2469cf514ccSChristoph Hellwig 	INIT_LIST_HEAD(&ls->ls_perfile);
2479cf514ccSChristoph Hellwig 	spin_lock_init(&ls->ls_lock);
2489cf514ccSChristoph Hellwig 	INIT_LIST_HEAD(&ls->ls_layouts);
249cc8a5532SJeff Layton 	mutex_init(&ls->ls_mutex);
2509cf514ccSChristoph Hellwig 	ls->ls_layout_type = layout_type;
251c5c707f9SChristoph Hellwig 	nfsd4_init_cb(&ls->ls_recall, clp, &nfsd4_cb_layout_ops,
252c5c707f9SChristoph Hellwig 			NFSPROC4_CLNT_CB_LAYOUT);
253c5c707f9SChristoph Hellwig 
2543f29cc82SNeilBrown 	if (parent->sc_type == SC_TYPE_DELEG)
255eb82dd39SJeff Layton 		ls->ls_file = nfsd_file_get(fp->fi_deleg_file);
256c5c707f9SChristoph Hellwig 	else
257c5c707f9SChristoph Hellwig 		ls->ls_file = find_any_file(fp);
258c5c707f9SChristoph Hellwig 	BUG_ON(!ls->ls_file);
259c5c707f9SChristoph Hellwig 
260c5c707f9SChristoph Hellwig 	if (nfsd4_layout_setlease(ls)) {
261eb82dd39SJeff Layton 		nfsd_file_put(ls->ls_file);
262c5c707f9SChristoph Hellwig 		put_nfs4_file(fp);
263c5c707f9SChristoph Hellwig 		kmem_cache_free(nfs4_layout_stateid_cache, ls);
264c5c707f9SChristoph Hellwig 		return NULL;
265c5c707f9SChristoph Hellwig 	}
2669cf514ccSChristoph Hellwig 
2679cf514ccSChristoph Hellwig 	spin_lock(&clp->cl_lock);
2683f29cc82SNeilBrown 	stp->sc_type = SC_TYPE_LAYOUT;
2699cf514ccSChristoph Hellwig 	list_add(&ls->ls_perclnt, &clp->cl_lo_states);
2709cf514ccSChristoph Hellwig 	spin_unlock(&clp->cl_lock);
2719cf514ccSChristoph Hellwig 
2729cf514ccSChristoph Hellwig 	spin_lock(&fp->fi_lock);
2739cf514ccSChristoph Hellwig 	list_add(&ls->ls_perfile, &fp->fi_lo_states);
2749cf514ccSChristoph Hellwig 	spin_unlock(&fp->fi_lock);
2759cf514ccSChristoph Hellwig 
276f394b62bSChuck Lever 	trace_nfsd_layoutstate_alloc(&ls->ls_stid.sc_stateid);
2779cf514ccSChristoph Hellwig 	return ls;
2789cf514ccSChristoph Hellwig }
2799cf514ccSChristoph Hellwig 
2809cf514ccSChristoph Hellwig __be32
2819cf514ccSChristoph Hellwig nfsd4_preprocess_layout_stateid(struct svc_rqst *rqstp,
2829cf514ccSChristoph Hellwig 		struct nfsd4_compound_state *cstate, stateid_t *stateid,
2839cf514ccSChristoph Hellwig 		bool create, u32 layout_type, struct nfs4_layout_stateid **lsp)
2849cf514ccSChristoph Hellwig {
2859cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
2869cf514ccSChristoph Hellwig 	struct nfs4_stid *stid;
2873f29cc82SNeilBrown 	unsigned short typemask = SC_TYPE_LAYOUT;
2889cf514ccSChristoph Hellwig 	__be32 status;
2899cf514ccSChristoph Hellwig 
2909cf514ccSChristoph Hellwig 	if (create)
2913f29cc82SNeilBrown 		typemask |= (SC_TYPE_OPEN | SC_TYPE_LOCK | SC_TYPE_DELEG);
2929cf514ccSChristoph Hellwig 
2933f29cc82SNeilBrown 	status = nfsd4_lookup_stateid(cstate, stateid, typemask, 0, &stid,
2949cf514ccSChristoph Hellwig 			net_generic(SVC_NET(rqstp), nfsd_net_id));
2959cf514ccSChristoph Hellwig 	if (status)
2969cf514ccSChristoph Hellwig 		goto out;
2979cf514ccSChristoph Hellwig 
2989cf514ccSChristoph Hellwig 	if (!fh_match(&cstate->current_fh.fh_handle,
2999cf514ccSChristoph Hellwig 		      &stid->sc_file->fi_fhandle)) {
3009cf514ccSChristoph Hellwig 		status = nfserr_bad_stateid;
3019cf514ccSChristoph Hellwig 		goto out_put_stid;
3029cf514ccSChristoph Hellwig 	}
3039cf514ccSChristoph Hellwig 
3043f29cc82SNeilBrown 	if (stid->sc_type != SC_TYPE_LAYOUT) {
3059cf514ccSChristoph Hellwig 		ls = nfsd4_alloc_layout_stateid(cstate, stid, layout_type);
3069cf514ccSChristoph Hellwig 		nfs4_put_stid(stid);
3079cf514ccSChristoph Hellwig 
3089cf514ccSChristoph Hellwig 		status = nfserr_jukebox;
3099cf514ccSChristoph Hellwig 		if (!ls)
3109cf514ccSChristoph Hellwig 			goto out;
311cc8a5532SJeff Layton 		mutex_lock(&ls->ls_mutex);
3129cf514ccSChristoph Hellwig 	} else {
3139cf514ccSChristoph Hellwig 		ls = container_of(stid, struct nfs4_layout_stateid, ls_stid);
3149cf514ccSChristoph Hellwig 
3159cf514ccSChristoph Hellwig 		status = nfserr_bad_stateid;
316cc8a5532SJeff Layton 		mutex_lock(&ls->ls_mutex);
31714b7f4a1SJeff Layton 		if (nfsd4_stateid_generation_after(stateid, &stid->sc_stateid))
318cc8a5532SJeff Layton 			goto out_unlock_stid;
3199cf514ccSChristoph Hellwig 		if (layout_type != ls->ls_layout_type)
320cc8a5532SJeff Layton 			goto out_unlock_stid;
3219cf514ccSChristoph Hellwig 	}
3229cf514ccSChristoph Hellwig 
3239cf514ccSChristoph Hellwig 	*lsp = ls;
3249cf514ccSChristoph Hellwig 	return 0;
3259cf514ccSChristoph Hellwig 
326cc8a5532SJeff Layton out_unlock_stid:
327cc8a5532SJeff Layton 	mutex_unlock(&ls->ls_mutex);
3289cf514ccSChristoph Hellwig out_put_stid:
3299cf514ccSChristoph Hellwig 	nfs4_put_stid(stid);
3309cf514ccSChristoph Hellwig out:
3319cf514ccSChristoph Hellwig 	return status;
3329cf514ccSChristoph Hellwig }
3339cf514ccSChristoph Hellwig 
334c5c707f9SChristoph Hellwig static void
335c5c707f9SChristoph Hellwig nfsd4_recall_file_layout(struct nfs4_layout_stateid *ls)
336c5c707f9SChristoph Hellwig {
337c5c707f9SChristoph Hellwig 	spin_lock(&ls->ls_lock);
338c5c707f9SChristoph Hellwig 	if (ls->ls_recalled)
339c5c707f9SChristoph Hellwig 		goto out_unlock;
340c5c707f9SChristoph Hellwig 
341c5c707f9SChristoph Hellwig 	if (list_empty(&ls->ls_layouts))
342c5c707f9SChristoph Hellwig 		goto out_unlock;
343c5c707f9SChristoph Hellwig 
344fb610c4dSBenjamin Coddington 	ls->ls_recalled = true;
345fb610c4dSBenjamin Coddington 	atomic_inc(&ls->ls_stid.sc_file->fi_lo_recalls);
346f394b62bSChuck Lever 	trace_nfsd_layout_recall(&ls->ls_stid.sc_stateid);
34731ef83dcSChristoph Hellwig 
348a15dfcd5SElena Reshetova 	refcount_inc(&ls->ls_stid.sc_count);
349c5c707f9SChristoph Hellwig 	nfsd4_run_cb(&ls->ls_recall);
350c5c707f9SChristoph Hellwig 
351c5c707f9SChristoph Hellwig out_unlock:
352c5c707f9SChristoph Hellwig 	spin_unlock(&ls->ls_lock);
353c5c707f9SChristoph Hellwig }
354c5c707f9SChristoph Hellwig 
3559cf514ccSChristoph Hellwig static inline u64
3569cf514ccSChristoph Hellwig layout_end(struct nfsd4_layout_seg *seg)
3579cf514ccSChristoph Hellwig {
3589cf514ccSChristoph Hellwig 	u64 end = seg->offset + seg->length;
3599cf514ccSChristoph Hellwig 	return end >= seg->offset ? end : NFS4_MAX_UINT64;
3609cf514ccSChristoph Hellwig }
3619cf514ccSChristoph Hellwig 
3629cf514ccSChristoph Hellwig static void
3639cf514ccSChristoph Hellwig layout_update_len(struct nfsd4_layout_seg *lo, u64 end)
3649cf514ccSChristoph Hellwig {
3659cf514ccSChristoph Hellwig 	if (end == NFS4_MAX_UINT64)
3669cf514ccSChristoph Hellwig 		lo->length = NFS4_MAX_UINT64;
3679cf514ccSChristoph Hellwig 	else
3689cf514ccSChristoph Hellwig 		lo->length = end - lo->offset;
3699cf514ccSChristoph Hellwig }
3709cf514ccSChristoph Hellwig 
3719cf514ccSChristoph Hellwig static bool
3729cf514ccSChristoph Hellwig layouts_overlapping(struct nfs4_layout *lo, struct nfsd4_layout_seg *s)
3739cf514ccSChristoph Hellwig {
3749cf514ccSChristoph Hellwig 	if (s->iomode != IOMODE_ANY && s->iomode != lo->lo_seg.iomode)
3759cf514ccSChristoph Hellwig 		return false;
3769cf514ccSChristoph Hellwig 	if (layout_end(&lo->lo_seg) <= s->offset)
3779cf514ccSChristoph Hellwig 		return false;
3789cf514ccSChristoph Hellwig 	if (layout_end(s) <= lo->lo_seg.offset)
3799cf514ccSChristoph Hellwig 		return false;
3809cf514ccSChristoph Hellwig 	return true;
3819cf514ccSChristoph Hellwig }
3829cf514ccSChristoph Hellwig 
3839cf514ccSChristoph Hellwig static bool
3849cf514ccSChristoph Hellwig layouts_try_merge(struct nfsd4_layout_seg *lo, struct nfsd4_layout_seg *new)
3859cf514ccSChristoph Hellwig {
3869cf514ccSChristoph Hellwig 	if (lo->iomode != new->iomode)
3879cf514ccSChristoph Hellwig 		return false;
3889cf514ccSChristoph Hellwig 	if (layout_end(new) < lo->offset)
3899cf514ccSChristoph Hellwig 		return false;
3909cf514ccSChristoph Hellwig 	if (layout_end(lo) < new->offset)
3919cf514ccSChristoph Hellwig 		return false;
3929cf514ccSChristoph Hellwig 
3939cf514ccSChristoph Hellwig 	lo->offset = min(lo->offset, new->offset);
3949cf514ccSChristoph Hellwig 	layout_update_len(lo, max(layout_end(lo), layout_end(new)));
3959cf514ccSChristoph Hellwig 	return true;
3969cf514ccSChristoph Hellwig }
3979cf514ccSChristoph Hellwig 
398c5c707f9SChristoph Hellwig static __be32
399c5c707f9SChristoph Hellwig nfsd4_recall_conflict(struct nfs4_layout_stateid *ls)
400c5c707f9SChristoph Hellwig {
401c5c707f9SChristoph Hellwig 	struct nfs4_file *fp = ls->ls_stid.sc_file;
402c5c707f9SChristoph Hellwig 	struct nfs4_layout_stateid *l, *n;
403c5c707f9SChristoph Hellwig 	__be32 nfserr = nfs_ok;
404c5c707f9SChristoph Hellwig 
405c5c707f9SChristoph Hellwig 	assert_spin_locked(&fp->fi_lock);
406c5c707f9SChristoph Hellwig 
407c5c707f9SChristoph Hellwig 	list_for_each_entry_safe(l, n, &fp->fi_lo_states, ls_perfile) {
408c5c707f9SChristoph Hellwig 		if (l != ls) {
409c5c707f9SChristoph Hellwig 			nfsd4_recall_file_layout(l);
410c5c707f9SChristoph Hellwig 			nfserr = nfserr_recallconflict;
411c5c707f9SChristoph Hellwig 		}
412c5c707f9SChristoph Hellwig 	}
413c5c707f9SChristoph Hellwig 
414c5c707f9SChristoph Hellwig 	return nfserr;
415c5c707f9SChristoph Hellwig }
416c5c707f9SChristoph Hellwig 
4179cf514ccSChristoph Hellwig __be32
4189cf514ccSChristoph Hellwig nfsd4_insert_layout(struct nfsd4_layoutget *lgp, struct nfs4_layout_stateid *ls)
4199cf514ccSChristoph Hellwig {
4209cf514ccSChristoph Hellwig 	struct nfsd4_layout_seg *seg = &lgp->lg_seg;
421c5c707f9SChristoph Hellwig 	struct nfs4_file *fp = ls->ls_stid.sc_file;
4229cf514ccSChristoph Hellwig 	struct nfs4_layout *lp, *new = NULL;
423c5c707f9SChristoph Hellwig 	__be32 nfserr;
4249cf514ccSChristoph Hellwig 
425c5c707f9SChristoph Hellwig 	spin_lock(&fp->fi_lock);
426c5c707f9SChristoph Hellwig 	nfserr = nfsd4_recall_conflict(ls);
427c5c707f9SChristoph Hellwig 	if (nfserr)
428c5c707f9SChristoph Hellwig 		goto out;
4299cf514ccSChristoph Hellwig 	spin_lock(&ls->ls_lock);
4309cf514ccSChristoph Hellwig 	list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
4319cf514ccSChristoph Hellwig 		if (layouts_try_merge(&lp->lo_seg, seg))
4329cf514ccSChristoph Hellwig 			goto done;
4339cf514ccSChristoph Hellwig 	}
4349cf514ccSChristoph Hellwig 	spin_unlock(&ls->ls_lock);
435c5c707f9SChristoph Hellwig 	spin_unlock(&fp->fi_lock);
4369cf514ccSChristoph Hellwig 
4379cf514ccSChristoph Hellwig 	new = kmem_cache_alloc(nfs4_layout_cache, GFP_KERNEL);
4389cf514ccSChristoph Hellwig 	if (!new)
4399cf514ccSChristoph Hellwig 		return nfserr_jukebox;
4404fc5f534SJakob Koschel 	memcpy(&new->lo_seg, seg, sizeof(new->lo_seg));
4419cf514ccSChristoph Hellwig 	new->lo_state = ls;
4429cf514ccSChristoph Hellwig 
443c5c707f9SChristoph Hellwig 	spin_lock(&fp->fi_lock);
444c5c707f9SChristoph Hellwig 	nfserr = nfsd4_recall_conflict(ls);
445c5c707f9SChristoph Hellwig 	if (nfserr)
446c5c707f9SChristoph Hellwig 		goto out;
4479cf514ccSChristoph Hellwig 	spin_lock(&ls->ls_lock);
4489cf514ccSChristoph Hellwig 	list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
4499cf514ccSChristoph Hellwig 		if (layouts_try_merge(&lp->lo_seg, seg))
4509cf514ccSChristoph Hellwig 			goto done;
4519cf514ccSChristoph Hellwig 	}
4529cf514ccSChristoph Hellwig 
453a15dfcd5SElena Reshetova 	refcount_inc(&ls->ls_stid.sc_count);
4549cf514ccSChristoph Hellwig 	list_add_tail(&new->lo_perstate, &ls->ls_layouts);
4559cf514ccSChristoph Hellwig 	new = NULL;
4569cf514ccSChristoph Hellwig done:
4579767feb2SJeff Layton 	nfs4_inc_and_copy_stateid(&lgp->lg_sid, &ls->ls_stid);
4589cf514ccSChristoph Hellwig 	spin_unlock(&ls->ls_lock);
459c5c707f9SChristoph Hellwig out:
460c5c707f9SChristoph Hellwig 	spin_unlock(&fp->fi_lock);
4619cf514ccSChristoph Hellwig 	if (new)
4629cf514ccSChristoph Hellwig 		kmem_cache_free(nfs4_layout_cache, new);
463c5c707f9SChristoph Hellwig 	return nfserr;
4649cf514ccSChristoph Hellwig }
4659cf514ccSChristoph Hellwig 
4669cf514ccSChristoph Hellwig static void
4679cf514ccSChristoph Hellwig nfsd4_free_layouts(struct list_head *reaplist)
4689cf514ccSChristoph Hellwig {
4699cf514ccSChristoph Hellwig 	while (!list_empty(reaplist)) {
4709cf514ccSChristoph Hellwig 		struct nfs4_layout *lp = list_first_entry(reaplist,
4719cf514ccSChristoph Hellwig 				struct nfs4_layout, lo_perstate);
4729cf514ccSChristoph Hellwig 
4739cf514ccSChristoph Hellwig 		list_del(&lp->lo_perstate);
4749cf514ccSChristoph Hellwig 		nfs4_put_stid(&lp->lo_state->ls_stid);
4759cf514ccSChristoph Hellwig 		kmem_cache_free(nfs4_layout_cache, lp);
4769cf514ccSChristoph Hellwig 	}
4779cf514ccSChristoph Hellwig }
4789cf514ccSChristoph Hellwig 
4799cf514ccSChristoph Hellwig static void
4809cf514ccSChristoph Hellwig nfsd4_return_file_layout(struct nfs4_layout *lp, struct nfsd4_layout_seg *seg,
4819cf514ccSChristoph Hellwig 		struct list_head *reaplist)
4829cf514ccSChristoph Hellwig {
4839cf514ccSChristoph Hellwig 	struct nfsd4_layout_seg *lo = &lp->lo_seg;
4849cf514ccSChristoph Hellwig 	u64 end = layout_end(lo);
4859cf514ccSChristoph Hellwig 
4869cf514ccSChristoph Hellwig 	if (seg->offset <= lo->offset) {
4879cf514ccSChristoph Hellwig 		if (layout_end(seg) >= end) {
4889cf514ccSChristoph Hellwig 			list_move_tail(&lp->lo_perstate, reaplist);
4899cf514ccSChristoph Hellwig 			return;
4909cf514ccSChristoph Hellwig 		}
4917890203dSKinglong Mee 		lo->offset = layout_end(seg);
4929cf514ccSChristoph Hellwig 	} else {
4939cf514ccSChristoph Hellwig 		/* retain the whole layout segment on a split. */
4949cf514ccSChristoph Hellwig 		if (layout_end(seg) < end) {
4959cf514ccSChristoph Hellwig 			dprintk("%s: split not supported\n", __func__);
4969cf514ccSChristoph Hellwig 			return;
4979cf514ccSChristoph Hellwig 		}
4987890203dSKinglong Mee 		end = seg->offset;
4999cf514ccSChristoph Hellwig 	}
5009cf514ccSChristoph Hellwig 
5019cf514ccSChristoph Hellwig 	layout_update_len(lo, end);
5029cf514ccSChristoph Hellwig }
5039cf514ccSChristoph Hellwig 
5049cf514ccSChristoph Hellwig __be32
5059cf514ccSChristoph Hellwig nfsd4_return_file_layouts(struct svc_rqst *rqstp,
5069cf514ccSChristoph Hellwig 		struct nfsd4_compound_state *cstate,
5079cf514ccSChristoph Hellwig 		struct nfsd4_layoutreturn *lrp)
5089cf514ccSChristoph Hellwig {
5099cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
5109cf514ccSChristoph Hellwig 	struct nfs4_layout *lp, *n;
5119cf514ccSChristoph Hellwig 	LIST_HEAD(reaplist);
5129cf514ccSChristoph Hellwig 	__be32 nfserr;
5139cf514ccSChristoph Hellwig 	int found = 0;
5149cf514ccSChristoph Hellwig 
5159cf514ccSChristoph Hellwig 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lrp->lr_sid,
5169cf514ccSChristoph Hellwig 						false, lrp->lr_layout_type,
5179cf514ccSChristoph Hellwig 						&ls);
51831ef83dcSChristoph Hellwig 	if (nfserr) {
519f394b62bSChuck Lever 		trace_nfsd_layout_return_lookup_fail(&lrp->lr_sid);
5209cf514ccSChristoph Hellwig 		return nfserr;
52131ef83dcSChristoph Hellwig 	}
5229cf514ccSChristoph Hellwig 
5239cf514ccSChristoph Hellwig 	spin_lock(&ls->ls_lock);
5249cf514ccSChristoph Hellwig 	list_for_each_entry_safe(lp, n, &ls->ls_layouts, lo_perstate) {
5259cf514ccSChristoph Hellwig 		if (layouts_overlapping(lp, &lrp->lr_seg)) {
5269cf514ccSChristoph Hellwig 			nfsd4_return_file_layout(lp, &lrp->lr_seg, &reaplist);
5279cf514ccSChristoph Hellwig 			found++;
5289cf514ccSChristoph Hellwig 		}
5299cf514ccSChristoph Hellwig 	}
5309cf514ccSChristoph Hellwig 	if (!list_empty(&ls->ls_layouts)) {
5319767feb2SJeff Layton 		if (found)
5329767feb2SJeff Layton 			nfs4_inc_and_copy_stateid(&lrp->lr_sid, &ls->ls_stid);
53385dbc978SChuck Lever 		lrp->lrs_present = true;
5349cf514ccSChristoph Hellwig 	} else {
535f394b62bSChuck Lever 		trace_nfsd_layoutstate_unhash(&ls->ls_stid.sc_stateid);
5363f29cc82SNeilBrown 		ls->ls_stid.sc_status |= SC_STATUS_CLOSED;
53785dbc978SChuck Lever 		lrp->lrs_present = false;
5389cf514ccSChristoph Hellwig 	}
5399cf514ccSChristoph Hellwig 	spin_unlock(&ls->ls_lock);
5409cf514ccSChristoph Hellwig 
541cc8a5532SJeff Layton 	mutex_unlock(&ls->ls_mutex);
5429cf514ccSChristoph Hellwig 	nfs4_put_stid(&ls->ls_stid);
5439cf514ccSChristoph Hellwig 	nfsd4_free_layouts(&reaplist);
5449cf514ccSChristoph Hellwig 	return nfs_ok;
5459cf514ccSChristoph Hellwig }
5469cf514ccSChristoph Hellwig 
5479cf514ccSChristoph Hellwig __be32
5489cf514ccSChristoph Hellwig nfsd4_return_client_layouts(struct svc_rqst *rqstp,
5499cf514ccSChristoph Hellwig 		struct nfsd4_compound_state *cstate,
5509cf514ccSChristoph Hellwig 		struct nfsd4_layoutreturn *lrp)
5519cf514ccSChristoph Hellwig {
5529cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls, *n;
5539cf514ccSChristoph Hellwig 	struct nfs4_client *clp = cstate->clp;
5549cf514ccSChristoph Hellwig 	struct nfs4_layout *lp, *t;
5559cf514ccSChristoph Hellwig 	LIST_HEAD(reaplist);
5569cf514ccSChristoph Hellwig 
55785dbc978SChuck Lever 	lrp->lrs_present = false;
5589cf514ccSChristoph Hellwig 
5599cf514ccSChristoph Hellwig 	spin_lock(&clp->cl_lock);
5609cf514ccSChristoph Hellwig 	list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt) {
5616f8f28ecSKinglong Mee 		if (ls->ls_layout_type != lrp->lr_layout_type)
5626f8f28ecSKinglong Mee 			continue;
5636f8f28ecSKinglong Mee 
5649cf514ccSChristoph Hellwig 		if (lrp->lr_return_type == RETURN_FSID &&
5659cf514ccSChristoph Hellwig 		    !fh_fsid_match(&ls->ls_stid.sc_file->fi_fhandle,
5669cf514ccSChristoph Hellwig 				   &cstate->current_fh.fh_handle))
5679cf514ccSChristoph Hellwig 			continue;
5689cf514ccSChristoph Hellwig 
5699cf514ccSChristoph Hellwig 		spin_lock(&ls->ls_lock);
5709cf514ccSChristoph Hellwig 		list_for_each_entry_safe(lp, t, &ls->ls_layouts, lo_perstate) {
5719cf514ccSChristoph Hellwig 			if (lrp->lr_seg.iomode == IOMODE_ANY ||
5729cf514ccSChristoph Hellwig 			    lrp->lr_seg.iomode == lp->lo_seg.iomode)
5739cf514ccSChristoph Hellwig 				list_move_tail(&lp->lo_perstate, &reaplist);
5749cf514ccSChristoph Hellwig 		}
5759cf514ccSChristoph Hellwig 		spin_unlock(&ls->ls_lock);
5769cf514ccSChristoph Hellwig 	}
5779cf514ccSChristoph Hellwig 	spin_unlock(&clp->cl_lock);
5789cf514ccSChristoph Hellwig 
5799cf514ccSChristoph Hellwig 	nfsd4_free_layouts(&reaplist);
5809cf514ccSChristoph Hellwig 	return 0;
5819cf514ccSChristoph Hellwig }
5829cf514ccSChristoph Hellwig 
5839cf514ccSChristoph Hellwig static void
5849cf514ccSChristoph Hellwig nfsd4_return_all_layouts(struct nfs4_layout_stateid *ls,
5859cf514ccSChristoph Hellwig 		struct list_head *reaplist)
5869cf514ccSChristoph Hellwig {
5879cf514ccSChristoph Hellwig 	spin_lock(&ls->ls_lock);
5889cf514ccSChristoph Hellwig 	list_splice_init(&ls->ls_layouts, reaplist);
5899cf514ccSChristoph Hellwig 	spin_unlock(&ls->ls_lock);
5909cf514ccSChristoph Hellwig }
5919cf514ccSChristoph Hellwig 
5929cf514ccSChristoph Hellwig void
5939cf514ccSChristoph Hellwig nfsd4_return_all_client_layouts(struct nfs4_client *clp)
5949cf514ccSChristoph Hellwig {
5959cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls, *n;
5969cf514ccSChristoph Hellwig 	LIST_HEAD(reaplist);
5979cf514ccSChristoph Hellwig 
5989cf514ccSChristoph Hellwig 	spin_lock(&clp->cl_lock);
5999cf514ccSChristoph Hellwig 	list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt)
6009cf514ccSChristoph Hellwig 		nfsd4_return_all_layouts(ls, &reaplist);
6019cf514ccSChristoph Hellwig 	spin_unlock(&clp->cl_lock);
6029cf514ccSChristoph Hellwig 
6039cf514ccSChristoph Hellwig 	nfsd4_free_layouts(&reaplist);
6049cf514ccSChristoph Hellwig }
6059cf514ccSChristoph Hellwig 
6069cf514ccSChristoph Hellwig void
6079cf514ccSChristoph Hellwig nfsd4_return_all_file_layouts(struct nfs4_client *clp, struct nfs4_file *fp)
6089cf514ccSChristoph Hellwig {
6099cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls, *n;
6109cf514ccSChristoph Hellwig 	LIST_HEAD(reaplist);
6119cf514ccSChristoph Hellwig 
6129cf514ccSChristoph Hellwig 	spin_lock(&fp->fi_lock);
6139cf514ccSChristoph Hellwig 	list_for_each_entry_safe(ls, n, &fp->fi_lo_states, ls_perfile) {
6149cf514ccSChristoph Hellwig 		if (ls->ls_stid.sc_client == clp)
6159cf514ccSChristoph Hellwig 			nfsd4_return_all_layouts(ls, &reaplist);
6169cf514ccSChristoph Hellwig 	}
6179cf514ccSChristoph Hellwig 	spin_unlock(&fp->fi_lock);
6189cf514ccSChristoph Hellwig 
6199cf514ccSChristoph Hellwig 	nfsd4_free_layouts(&reaplist);
6209cf514ccSChristoph Hellwig }
6219cf514ccSChristoph Hellwig 
622c5c707f9SChristoph Hellwig static void
6231e33e141SNeilBrown nfsd4_cb_layout_fail(struct nfs4_layout_stateid *ls, struct nfsd_file *file)
624c5c707f9SChristoph Hellwig {
625c5c707f9SChristoph Hellwig 	struct nfs4_client *clp = ls->ls_stid.sc_client;
626c5c707f9SChristoph Hellwig 	char addr_str[INET6_ADDRSTRLEN];
627377e7a27SGreg Kroah-Hartman 	static char const nfsd_recall_failed[] = "/sbin/nfsd-recall-failed";
628c5c707f9SChristoph Hellwig 	static char *envp[] = {
629c5c707f9SChristoph Hellwig 		"HOME=/",
630c5c707f9SChristoph Hellwig 		"TERM=linux",
631c5c707f9SChristoph Hellwig 		"PATH=/sbin:/usr/sbin:/bin:/usr/bin",
632c5c707f9SChristoph Hellwig 		NULL
633c5c707f9SChristoph Hellwig 	};
634c5c707f9SChristoph Hellwig 	char *argv[8];
635c5c707f9SChristoph Hellwig 	int error;
636c5c707f9SChristoph Hellwig 
637c5c707f9SChristoph Hellwig 	rpc_ntop((struct sockaddr *)&clp->cl_addr, addr_str, sizeof(addr_str));
638c5c707f9SChristoph Hellwig 
639c5c707f9SChristoph Hellwig 	printk(KERN_WARNING
640c5c707f9SChristoph Hellwig 		"nfsd: client %s failed to respond to layout recall. "
641c5c707f9SChristoph Hellwig 		"  Fencing..\n", addr_str);
642c5c707f9SChristoph Hellwig 
643377e7a27SGreg Kroah-Hartman 	argv[0] = (char *)nfsd_recall_failed;
644c5c707f9SChristoph Hellwig 	argv[1] = addr_str;
6451e33e141SNeilBrown 	argv[2] = file->nf_file->f_path.mnt->mnt_sb->s_id;
646c5c707f9SChristoph Hellwig 	argv[3] = NULL;
647c5c707f9SChristoph Hellwig 
648377e7a27SGreg Kroah-Hartman 	error = call_usermodehelper(nfsd_recall_failed, argv, envp,
649377e7a27SGreg Kroah-Hartman 				    UMH_WAIT_PROC);
650c5c707f9SChristoph Hellwig 	if (error) {
651c5c707f9SChristoph Hellwig 		printk(KERN_ERR "nfsd: fence failed for client %s: %d!\n",
652c5c707f9SChristoph Hellwig 			addr_str, error);
653c5c707f9SChristoph Hellwig 	}
654c5c707f9SChristoph Hellwig }
655c5c707f9SChristoph Hellwig 
656cc8a5532SJeff Layton static void
657cc8a5532SJeff Layton nfsd4_cb_layout_prepare(struct nfsd4_callback *cb)
658cc8a5532SJeff Layton {
659cc8a5532SJeff Layton 	struct nfs4_layout_stateid *ls =
660cc8a5532SJeff Layton 		container_of(cb, struct nfs4_layout_stateid, ls_recall);
661cc8a5532SJeff Layton 
662cc8a5532SJeff Layton 	mutex_lock(&ls->ls_mutex);
6639767feb2SJeff Layton 	nfs4_inc_and_copy_stateid(&ls->ls_recall_sid, &ls->ls_stid);
664be20aa00SJeff Layton 	mutex_unlock(&ls->ls_mutex);
665cc8a5532SJeff Layton }
666cc8a5532SJeff Layton 
667c5c707f9SChristoph Hellwig static int
668c5c707f9SChristoph Hellwig nfsd4_cb_layout_done(struct nfsd4_callback *cb, struct rpc_task *task)
669c5c707f9SChristoph Hellwig {
670c5c707f9SChristoph Hellwig 	struct nfs4_layout_stateid *ls =
671c5c707f9SChristoph Hellwig 		container_of(cb, struct nfs4_layout_stateid, ls_recall);
6726b9b2107SJeff Layton 	struct nfsd_net *nn;
6736b9b2107SJeff Layton 	ktime_t now, cutoff;
674f99d4fbdSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
6751e33e141SNeilBrown 	struct nfsd_file *fl;
676c5c707f9SChristoph Hellwig 
6771035d654SChuck Lever 	trace_nfsd_cb_layout_done(&ls->ls_stid.sc_stateid, task);
678c5c707f9SChristoph Hellwig 	switch (task->tk_status) {
679c5c707f9SChristoph Hellwig 	case 0:
6806b9b2107SJeff Layton 	case -NFS4ERR_DELAY:
6816b9b2107SJeff Layton 		/*
6826b9b2107SJeff Layton 		 * Anything left? If not, then call it done. Note that we don't
6836b9b2107SJeff Layton 		 * take the spinlock since this is an optimization and nothing
6846b9b2107SJeff Layton 		 * should get added until the cb counter goes to zero.
6856b9b2107SJeff Layton 		 */
6866b9b2107SJeff Layton 		if (list_empty(&ls->ls_layouts))
687c5c707f9SChristoph Hellwig 			return 1;
6886b9b2107SJeff Layton 
6896b9b2107SJeff Layton 		/* Poll the client until it's done with the layout */
6906b9b2107SJeff Layton 		now = ktime_get();
6916b9b2107SJeff Layton 		nn = net_generic(ls->ls_stid.sc_client->net, nfsd_net_id);
6926b9b2107SJeff Layton 
6936b9b2107SJeff Layton 		/* Client gets 2 lease periods to return it */
6946b9b2107SJeff Layton 		cutoff = ktime_add_ns(task->tk_start,
6952561c92bSArnd Bergmann 					 (u64)nn->nfsd4_lease * NSEC_PER_SEC * 2);
6966b9b2107SJeff Layton 
6976b9b2107SJeff Layton 		if (ktime_before(now, cutoff)) {
6986b9b2107SJeff Layton 			rpc_delay(task, HZ/100); /* 10 mili-seconds */
6996b9b2107SJeff Layton 			return 0;
7006b9b2107SJeff Layton 		}
701df561f66SGustavo A. R. Silva 		fallthrough;
702c5c707f9SChristoph Hellwig 	default:
703c5c707f9SChristoph Hellwig 		/*
704c5c707f9SChristoph Hellwig 		 * Unknown error or non-responding client, we'll need to fence.
705c5c707f9SChristoph Hellwig 		 */
706f394b62bSChuck Lever 		trace_nfsd_layout_recall_fail(&ls->ls_stid.sc_stateid);
7071e33e141SNeilBrown 		rcu_read_lock();
7081e33e141SNeilBrown 		fl = nfsd_file_get(ls->ls_file);
7091e33e141SNeilBrown 		rcu_read_unlock();
7101e33e141SNeilBrown 		if (fl) {
711f99d4fbdSChristoph Hellwig 			ops = nfsd4_layout_ops[ls->ls_layout_type];
712f99d4fbdSChristoph Hellwig 			if (ops->fence_client)
7131e33e141SNeilBrown 				ops->fence_client(ls, fl);
714f99d4fbdSChristoph Hellwig 			else
7151e33e141SNeilBrown 				nfsd4_cb_layout_fail(ls, fl);
7161e33e141SNeilBrown 			nfsd_file_put(fl);
7171e33e141SNeilBrown 		}
7181c73b9d2SScott Mayhew 		return 1;
719851238a2SJeff Layton 	case -NFS4ERR_NOMATCHING_LAYOUT:
720f394b62bSChuck Lever 		trace_nfsd_layout_recall_done(&ls->ls_stid.sc_stateid);
721851238a2SJeff Layton 		task->tk_status = 0;
722851238a2SJeff Layton 		return 1;
723c5c707f9SChristoph Hellwig 	}
724c5c707f9SChristoph Hellwig }
725c5c707f9SChristoph Hellwig 
726c5c707f9SChristoph Hellwig static void
727c5c707f9SChristoph Hellwig nfsd4_cb_layout_release(struct nfsd4_callback *cb)
728c5c707f9SChristoph Hellwig {
729c5c707f9SChristoph Hellwig 	struct nfs4_layout_stateid *ls =
730c5c707f9SChristoph Hellwig 		container_of(cb, struct nfs4_layout_stateid, ls_recall);
731c5c707f9SChristoph Hellwig 	LIST_HEAD(reaplist);
732c5c707f9SChristoph Hellwig 
733f394b62bSChuck Lever 	trace_nfsd_layout_recall_release(&ls->ls_stid.sc_stateid);
73431ef83dcSChristoph Hellwig 
735c5c707f9SChristoph Hellwig 	nfsd4_return_all_layouts(ls, &reaplist);
736c5c707f9SChristoph Hellwig 	nfsd4_free_layouts(&reaplist);
737c5c707f9SChristoph Hellwig 	nfs4_put_stid(&ls->ls_stid);
738c5c707f9SChristoph Hellwig }
739c5c707f9SChristoph Hellwig 
740c4cb8974SJulia Lawall static const struct nfsd4_callback_ops nfsd4_cb_layout_ops = {
741cc8a5532SJeff Layton 	.prepare	= nfsd4_cb_layout_prepare,
742c5c707f9SChristoph Hellwig 	.done		= nfsd4_cb_layout_done,
743c5c707f9SChristoph Hellwig 	.release	= nfsd4_cb_layout_release,
744c5c707f9SChristoph Hellwig };
745c5c707f9SChristoph Hellwig 
746c5c707f9SChristoph Hellwig static bool
747c5c707f9SChristoph Hellwig nfsd4_layout_lm_break(struct file_lock *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;
755c5c707f9SChristoph Hellwig 	nfsd4_recall_file_layout(fl->fl_owner);
756c5c707f9SChristoph Hellwig 	return false;
757c5c707f9SChristoph Hellwig }
758c5c707f9SChristoph Hellwig 
759c5c707f9SChristoph Hellwig static int
760c5c707f9SChristoph Hellwig nfsd4_layout_lm_change(struct file_lock *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 
767c5c707f9SChristoph Hellwig static const struct lock_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 
780*10bcc2f1SKunwu Chan 	nfs4_layout_cache = KMEM_CACHE(nfs4_layout, 0);
7819cf514ccSChristoph Hellwig 	if (!nfs4_layout_cache)
7829cf514ccSChristoph Hellwig 		return -ENOMEM;
7839cf514ccSChristoph Hellwig 
784*10bcc2f1SKunwu 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