xref: /linux/fs/nfsd/nfs4layouts.c (revision 1035d65446a018ca2dd179e29a2fcd6d29057781)
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 
1559cf514ccSChristoph Hellwig static void
1569cf514ccSChristoph Hellwig nfsd4_free_layout_stateid(struct nfs4_stid *stid)
1579cf514ccSChristoph Hellwig {
1589cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls = layoutstateid(stid);
1599cf514ccSChristoph Hellwig 	struct nfs4_client *clp = ls->ls_stid.sc_client;
1609cf514ccSChristoph Hellwig 	struct nfs4_file *fp = ls->ls_stid.sc_file;
1619cf514ccSChristoph Hellwig 
162f394b62bSChuck Lever 	trace_nfsd_layoutstate_free(&ls->ls_stid.sc_stateid);
16331ef83dcSChristoph Hellwig 
1649cf514ccSChristoph Hellwig 	spin_lock(&clp->cl_lock);
1659cf514ccSChristoph Hellwig 	list_del_init(&ls->ls_perclnt);
1669cf514ccSChristoph Hellwig 	spin_unlock(&clp->cl_lock);
1679cf514ccSChristoph Hellwig 
1689cf514ccSChristoph Hellwig 	spin_lock(&fp->fi_lock);
1699cf514ccSChristoph Hellwig 	list_del_init(&ls->ls_perfile);
1709cf514ccSChristoph Hellwig 	spin_unlock(&fp->fi_lock);
1719cf514ccSChristoph Hellwig 
1721983a66fSJeff Layton 	if (!nfsd4_layout_ops[ls->ls_layout_type]->disable_recalls)
173eb82dd39SJeff Layton 		vfs_setlease(ls->ls_file->nf_file, F_UNLCK, NULL, (void **)&ls);
174eb82dd39SJeff Layton 	nfsd_file_put(ls->ls_file);
175c5c707f9SChristoph Hellwig 
176c5c707f9SChristoph Hellwig 	if (ls->ls_recalled)
177c5c707f9SChristoph Hellwig 		atomic_dec(&ls->ls_stid.sc_file->fi_lo_recalls);
178c5c707f9SChristoph Hellwig 
1799cf514ccSChristoph Hellwig 	kmem_cache_free(nfs4_layout_stateid_cache, ls);
1809cf514ccSChristoph Hellwig }
1819cf514ccSChristoph Hellwig 
182c5c707f9SChristoph Hellwig static int
183c5c707f9SChristoph Hellwig nfsd4_layout_setlease(struct nfs4_layout_stateid *ls)
184c5c707f9SChristoph Hellwig {
185c5c707f9SChristoph Hellwig 	struct file_lock *fl;
186c5c707f9SChristoph Hellwig 	int status;
187c5c707f9SChristoph Hellwig 
1881983a66fSJeff Layton 	if (nfsd4_layout_ops[ls->ls_layout_type]->disable_recalls)
1891983a66fSJeff Layton 		return 0;
1901983a66fSJeff Layton 
191c5c707f9SChristoph Hellwig 	fl = locks_alloc_lock();
192c5c707f9SChristoph Hellwig 	if (!fl)
193c5c707f9SChristoph Hellwig 		return -ENOMEM;
194c5c707f9SChristoph Hellwig 	locks_init_lock(fl);
195c5c707f9SChristoph Hellwig 	fl->fl_lmops = &nfsd4_layouts_lm_ops;
196c5c707f9SChristoph Hellwig 	fl->fl_flags = FL_LAYOUT;
197c5c707f9SChristoph Hellwig 	fl->fl_type = F_RDLCK;
198c5c707f9SChristoph Hellwig 	fl->fl_end = OFFSET_MAX;
199c5c707f9SChristoph Hellwig 	fl->fl_owner = ls;
200c5c707f9SChristoph Hellwig 	fl->fl_pid = current->tgid;
201eb82dd39SJeff Layton 	fl->fl_file = ls->ls_file->nf_file;
202c5c707f9SChristoph Hellwig 
203c5c707f9SChristoph Hellwig 	status = vfs_setlease(fl->fl_file, fl->fl_type, &fl, NULL);
204c5c707f9SChristoph Hellwig 	if (status) {
205c5c707f9SChristoph Hellwig 		locks_free_lock(fl);
206c5c707f9SChristoph Hellwig 		return status;
207c5c707f9SChristoph Hellwig 	}
208c5c707f9SChristoph Hellwig 	BUG_ON(fl != NULL);
209c5c707f9SChristoph Hellwig 	return 0;
210c5c707f9SChristoph Hellwig }
211c5c707f9SChristoph Hellwig 
2129cf514ccSChristoph Hellwig static struct nfs4_layout_stateid *
2139cf514ccSChristoph Hellwig nfsd4_alloc_layout_stateid(struct nfsd4_compound_state *cstate,
2149cf514ccSChristoph Hellwig 		struct nfs4_stid *parent, u32 layout_type)
2159cf514ccSChristoph Hellwig {
2169cf514ccSChristoph Hellwig 	struct nfs4_client *clp = cstate->clp;
2179cf514ccSChristoph Hellwig 	struct nfs4_file *fp = parent->sc_file;
2189cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
2199cf514ccSChristoph Hellwig 	struct nfs4_stid *stp;
2209cf514ccSChristoph Hellwig 
221d19fb70dSKinglong Mee 	stp = nfs4_alloc_stid(cstate->clp, nfs4_layout_stateid_cache,
222d19fb70dSKinglong Mee 					nfsd4_free_layout_stateid);
2239cf514ccSChristoph Hellwig 	if (!stp)
2249cf514ccSChristoph Hellwig 		return NULL;
225d19fb70dSKinglong Mee 
2269cf514ccSChristoph Hellwig 	get_nfs4_file(fp);
2279cf514ccSChristoph Hellwig 	stp->sc_file = fp;
2289cf514ccSChristoph Hellwig 
2299cf514ccSChristoph Hellwig 	ls = layoutstateid(stp);
2309cf514ccSChristoph Hellwig 	INIT_LIST_HEAD(&ls->ls_perclnt);
2319cf514ccSChristoph Hellwig 	INIT_LIST_HEAD(&ls->ls_perfile);
2329cf514ccSChristoph Hellwig 	spin_lock_init(&ls->ls_lock);
2339cf514ccSChristoph Hellwig 	INIT_LIST_HEAD(&ls->ls_layouts);
234cc8a5532SJeff Layton 	mutex_init(&ls->ls_mutex);
2359cf514ccSChristoph Hellwig 	ls->ls_layout_type = layout_type;
236c5c707f9SChristoph Hellwig 	nfsd4_init_cb(&ls->ls_recall, clp, &nfsd4_cb_layout_ops,
237c5c707f9SChristoph Hellwig 			NFSPROC4_CLNT_CB_LAYOUT);
238c5c707f9SChristoph Hellwig 
239c5c707f9SChristoph Hellwig 	if (parent->sc_type == NFS4_DELEG_STID)
240eb82dd39SJeff Layton 		ls->ls_file = nfsd_file_get(fp->fi_deleg_file);
241c5c707f9SChristoph Hellwig 	else
242c5c707f9SChristoph Hellwig 		ls->ls_file = find_any_file(fp);
243c5c707f9SChristoph Hellwig 	BUG_ON(!ls->ls_file);
244c5c707f9SChristoph Hellwig 
245c5c707f9SChristoph Hellwig 	if (nfsd4_layout_setlease(ls)) {
246eb82dd39SJeff Layton 		nfsd_file_put(ls->ls_file);
247c5c707f9SChristoph Hellwig 		put_nfs4_file(fp);
248c5c707f9SChristoph Hellwig 		kmem_cache_free(nfs4_layout_stateid_cache, ls);
249c5c707f9SChristoph Hellwig 		return NULL;
250c5c707f9SChristoph Hellwig 	}
2519cf514ccSChristoph Hellwig 
2529cf514ccSChristoph Hellwig 	spin_lock(&clp->cl_lock);
2539cf514ccSChristoph Hellwig 	stp->sc_type = NFS4_LAYOUT_STID;
2549cf514ccSChristoph Hellwig 	list_add(&ls->ls_perclnt, &clp->cl_lo_states);
2559cf514ccSChristoph Hellwig 	spin_unlock(&clp->cl_lock);
2569cf514ccSChristoph Hellwig 
2579cf514ccSChristoph Hellwig 	spin_lock(&fp->fi_lock);
2589cf514ccSChristoph Hellwig 	list_add(&ls->ls_perfile, &fp->fi_lo_states);
2599cf514ccSChristoph Hellwig 	spin_unlock(&fp->fi_lock);
2609cf514ccSChristoph Hellwig 
261f394b62bSChuck Lever 	trace_nfsd_layoutstate_alloc(&ls->ls_stid.sc_stateid);
2629cf514ccSChristoph Hellwig 	return ls;
2639cf514ccSChristoph Hellwig }
2649cf514ccSChristoph Hellwig 
2659cf514ccSChristoph Hellwig __be32
2669cf514ccSChristoph Hellwig nfsd4_preprocess_layout_stateid(struct svc_rqst *rqstp,
2679cf514ccSChristoph Hellwig 		struct nfsd4_compound_state *cstate, stateid_t *stateid,
2689cf514ccSChristoph Hellwig 		bool create, u32 layout_type, struct nfs4_layout_stateid **lsp)
2699cf514ccSChristoph Hellwig {
2709cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
2719cf514ccSChristoph Hellwig 	struct nfs4_stid *stid;
2729cf514ccSChristoph Hellwig 	unsigned char typemask = NFS4_LAYOUT_STID;
2739cf514ccSChristoph Hellwig 	__be32 status;
2749cf514ccSChristoph Hellwig 
2759cf514ccSChristoph Hellwig 	if (create)
2769cf514ccSChristoph Hellwig 		typemask |= (NFS4_OPEN_STID | NFS4_LOCK_STID | NFS4_DELEG_STID);
2779cf514ccSChristoph Hellwig 
2789cf514ccSChristoph Hellwig 	status = nfsd4_lookup_stateid(cstate, stateid, typemask, &stid,
2799cf514ccSChristoph Hellwig 			net_generic(SVC_NET(rqstp), nfsd_net_id));
2809cf514ccSChristoph Hellwig 	if (status)
2819cf514ccSChristoph Hellwig 		goto out;
2829cf514ccSChristoph Hellwig 
2839cf514ccSChristoph Hellwig 	if (!fh_match(&cstate->current_fh.fh_handle,
2849cf514ccSChristoph Hellwig 		      &stid->sc_file->fi_fhandle)) {
2859cf514ccSChristoph Hellwig 		status = nfserr_bad_stateid;
2869cf514ccSChristoph Hellwig 		goto out_put_stid;
2879cf514ccSChristoph Hellwig 	}
2889cf514ccSChristoph Hellwig 
2899cf514ccSChristoph Hellwig 	if (stid->sc_type != NFS4_LAYOUT_STID) {
2909cf514ccSChristoph Hellwig 		ls = nfsd4_alloc_layout_stateid(cstate, stid, layout_type);
2919cf514ccSChristoph Hellwig 		nfs4_put_stid(stid);
2929cf514ccSChristoph Hellwig 
2939cf514ccSChristoph Hellwig 		status = nfserr_jukebox;
2949cf514ccSChristoph Hellwig 		if (!ls)
2959cf514ccSChristoph Hellwig 			goto out;
296cc8a5532SJeff Layton 		mutex_lock(&ls->ls_mutex);
2979cf514ccSChristoph Hellwig 	} else {
2989cf514ccSChristoph Hellwig 		ls = container_of(stid, struct nfs4_layout_stateid, ls_stid);
2999cf514ccSChristoph Hellwig 
3009cf514ccSChristoph Hellwig 		status = nfserr_bad_stateid;
301cc8a5532SJeff Layton 		mutex_lock(&ls->ls_mutex);
30214b7f4a1SJeff Layton 		if (nfsd4_stateid_generation_after(stateid, &stid->sc_stateid))
303cc8a5532SJeff Layton 			goto out_unlock_stid;
3049cf514ccSChristoph Hellwig 		if (layout_type != ls->ls_layout_type)
305cc8a5532SJeff Layton 			goto out_unlock_stid;
3069cf514ccSChristoph Hellwig 	}
3079cf514ccSChristoph Hellwig 
3089cf514ccSChristoph Hellwig 	*lsp = ls;
3099cf514ccSChristoph Hellwig 	return 0;
3109cf514ccSChristoph Hellwig 
311cc8a5532SJeff Layton out_unlock_stid:
312cc8a5532SJeff Layton 	mutex_unlock(&ls->ls_mutex);
3139cf514ccSChristoph Hellwig out_put_stid:
3149cf514ccSChristoph Hellwig 	nfs4_put_stid(stid);
3159cf514ccSChristoph Hellwig out:
3169cf514ccSChristoph Hellwig 	return status;
3179cf514ccSChristoph Hellwig }
3189cf514ccSChristoph Hellwig 
319c5c707f9SChristoph Hellwig static void
320c5c707f9SChristoph Hellwig nfsd4_recall_file_layout(struct nfs4_layout_stateid *ls)
321c5c707f9SChristoph Hellwig {
322c5c707f9SChristoph Hellwig 	spin_lock(&ls->ls_lock);
323c5c707f9SChristoph Hellwig 	if (ls->ls_recalled)
324c5c707f9SChristoph Hellwig 		goto out_unlock;
325c5c707f9SChristoph Hellwig 
326c5c707f9SChristoph Hellwig 	ls->ls_recalled = true;
327c5c707f9SChristoph Hellwig 	atomic_inc(&ls->ls_stid.sc_file->fi_lo_recalls);
328c5c707f9SChristoph Hellwig 	if (list_empty(&ls->ls_layouts))
329c5c707f9SChristoph Hellwig 		goto out_unlock;
330c5c707f9SChristoph Hellwig 
331f394b62bSChuck Lever 	trace_nfsd_layout_recall(&ls->ls_stid.sc_stateid);
33231ef83dcSChristoph Hellwig 
333a15dfcd5SElena Reshetova 	refcount_inc(&ls->ls_stid.sc_count);
334c5c707f9SChristoph Hellwig 	nfsd4_run_cb(&ls->ls_recall);
335c5c707f9SChristoph Hellwig 
336c5c707f9SChristoph Hellwig out_unlock:
337c5c707f9SChristoph Hellwig 	spin_unlock(&ls->ls_lock);
338c5c707f9SChristoph Hellwig }
339c5c707f9SChristoph Hellwig 
3409cf514ccSChristoph Hellwig static inline u64
3419cf514ccSChristoph Hellwig layout_end(struct nfsd4_layout_seg *seg)
3429cf514ccSChristoph Hellwig {
3439cf514ccSChristoph Hellwig 	u64 end = seg->offset + seg->length;
3449cf514ccSChristoph Hellwig 	return end >= seg->offset ? end : NFS4_MAX_UINT64;
3459cf514ccSChristoph Hellwig }
3469cf514ccSChristoph Hellwig 
3479cf514ccSChristoph Hellwig static void
3489cf514ccSChristoph Hellwig layout_update_len(struct nfsd4_layout_seg *lo, u64 end)
3499cf514ccSChristoph Hellwig {
3509cf514ccSChristoph Hellwig 	if (end == NFS4_MAX_UINT64)
3519cf514ccSChristoph Hellwig 		lo->length = NFS4_MAX_UINT64;
3529cf514ccSChristoph Hellwig 	else
3539cf514ccSChristoph Hellwig 		lo->length = end - lo->offset;
3549cf514ccSChristoph Hellwig }
3559cf514ccSChristoph Hellwig 
3569cf514ccSChristoph Hellwig static bool
3579cf514ccSChristoph Hellwig layouts_overlapping(struct nfs4_layout *lo, struct nfsd4_layout_seg *s)
3589cf514ccSChristoph Hellwig {
3599cf514ccSChristoph Hellwig 	if (s->iomode != IOMODE_ANY && s->iomode != lo->lo_seg.iomode)
3609cf514ccSChristoph Hellwig 		return false;
3619cf514ccSChristoph Hellwig 	if (layout_end(&lo->lo_seg) <= s->offset)
3629cf514ccSChristoph Hellwig 		return false;
3639cf514ccSChristoph Hellwig 	if (layout_end(s) <= lo->lo_seg.offset)
3649cf514ccSChristoph Hellwig 		return false;
3659cf514ccSChristoph Hellwig 	return true;
3669cf514ccSChristoph Hellwig }
3679cf514ccSChristoph Hellwig 
3689cf514ccSChristoph Hellwig static bool
3699cf514ccSChristoph Hellwig layouts_try_merge(struct nfsd4_layout_seg *lo, struct nfsd4_layout_seg *new)
3709cf514ccSChristoph Hellwig {
3719cf514ccSChristoph Hellwig 	if (lo->iomode != new->iomode)
3729cf514ccSChristoph Hellwig 		return false;
3739cf514ccSChristoph Hellwig 	if (layout_end(new) < lo->offset)
3749cf514ccSChristoph Hellwig 		return false;
3759cf514ccSChristoph Hellwig 	if (layout_end(lo) < new->offset)
3769cf514ccSChristoph Hellwig 		return false;
3779cf514ccSChristoph Hellwig 
3789cf514ccSChristoph Hellwig 	lo->offset = min(lo->offset, new->offset);
3799cf514ccSChristoph Hellwig 	layout_update_len(lo, max(layout_end(lo), layout_end(new)));
3809cf514ccSChristoph Hellwig 	return true;
3819cf514ccSChristoph Hellwig }
3829cf514ccSChristoph Hellwig 
383c5c707f9SChristoph Hellwig static __be32
384c5c707f9SChristoph Hellwig nfsd4_recall_conflict(struct nfs4_layout_stateid *ls)
385c5c707f9SChristoph Hellwig {
386c5c707f9SChristoph Hellwig 	struct nfs4_file *fp = ls->ls_stid.sc_file;
387c5c707f9SChristoph Hellwig 	struct nfs4_layout_stateid *l, *n;
388c5c707f9SChristoph Hellwig 	__be32 nfserr = nfs_ok;
389c5c707f9SChristoph Hellwig 
390c5c707f9SChristoph Hellwig 	assert_spin_locked(&fp->fi_lock);
391c5c707f9SChristoph Hellwig 
392c5c707f9SChristoph Hellwig 	list_for_each_entry_safe(l, n, &fp->fi_lo_states, ls_perfile) {
393c5c707f9SChristoph Hellwig 		if (l != ls) {
394c5c707f9SChristoph Hellwig 			nfsd4_recall_file_layout(l);
395c5c707f9SChristoph Hellwig 			nfserr = nfserr_recallconflict;
396c5c707f9SChristoph Hellwig 		}
397c5c707f9SChristoph Hellwig 	}
398c5c707f9SChristoph Hellwig 
399c5c707f9SChristoph Hellwig 	return nfserr;
400c5c707f9SChristoph Hellwig }
401c5c707f9SChristoph Hellwig 
4029cf514ccSChristoph Hellwig __be32
4039cf514ccSChristoph Hellwig nfsd4_insert_layout(struct nfsd4_layoutget *lgp, struct nfs4_layout_stateid *ls)
4049cf514ccSChristoph Hellwig {
4059cf514ccSChristoph Hellwig 	struct nfsd4_layout_seg *seg = &lgp->lg_seg;
406c5c707f9SChristoph Hellwig 	struct nfs4_file *fp = ls->ls_stid.sc_file;
4079cf514ccSChristoph Hellwig 	struct nfs4_layout *lp, *new = NULL;
408c5c707f9SChristoph Hellwig 	__be32 nfserr;
4099cf514ccSChristoph Hellwig 
410c5c707f9SChristoph Hellwig 	spin_lock(&fp->fi_lock);
411c5c707f9SChristoph Hellwig 	nfserr = nfsd4_recall_conflict(ls);
412c5c707f9SChristoph Hellwig 	if (nfserr)
413c5c707f9SChristoph Hellwig 		goto out;
4149cf514ccSChristoph Hellwig 	spin_lock(&ls->ls_lock);
4159cf514ccSChristoph Hellwig 	list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
4169cf514ccSChristoph Hellwig 		if (layouts_try_merge(&lp->lo_seg, seg))
4179cf514ccSChristoph Hellwig 			goto done;
4189cf514ccSChristoph Hellwig 	}
4199cf514ccSChristoph Hellwig 	spin_unlock(&ls->ls_lock);
420c5c707f9SChristoph Hellwig 	spin_unlock(&fp->fi_lock);
4219cf514ccSChristoph Hellwig 
4229cf514ccSChristoph Hellwig 	new = kmem_cache_alloc(nfs4_layout_cache, GFP_KERNEL);
4239cf514ccSChristoph Hellwig 	if (!new)
4249cf514ccSChristoph Hellwig 		return nfserr_jukebox;
4254fc5f534SJakob Koschel 	memcpy(&new->lo_seg, seg, sizeof(new->lo_seg));
4269cf514ccSChristoph Hellwig 	new->lo_state = ls;
4279cf514ccSChristoph Hellwig 
428c5c707f9SChristoph Hellwig 	spin_lock(&fp->fi_lock);
429c5c707f9SChristoph Hellwig 	nfserr = nfsd4_recall_conflict(ls);
430c5c707f9SChristoph Hellwig 	if (nfserr)
431c5c707f9SChristoph Hellwig 		goto out;
4329cf514ccSChristoph Hellwig 	spin_lock(&ls->ls_lock);
4339cf514ccSChristoph Hellwig 	list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
4349cf514ccSChristoph Hellwig 		if (layouts_try_merge(&lp->lo_seg, seg))
4359cf514ccSChristoph Hellwig 			goto done;
4369cf514ccSChristoph Hellwig 	}
4379cf514ccSChristoph Hellwig 
438a15dfcd5SElena Reshetova 	refcount_inc(&ls->ls_stid.sc_count);
4399cf514ccSChristoph Hellwig 	list_add_tail(&new->lo_perstate, &ls->ls_layouts);
4409cf514ccSChristoph Hellwig 	new = NULL;
4419cf514ccSChristoph Hellwig done:
4429767feb2SJeff Layton 	nfs4_inc_and_copy_stateid(&lgp->lg_sid, &ls->ls_stid);
4439cf514ccSChristoph Hellwig 	spin_unlock(&ls->ls_lock);
444c5c707f9SChristoph Hellwig out:
445c5c707f9SChristoph Hellwig 	spin_unlock(&fp->fi_lock);
4469cf514ccSChristoph Hellwig 	if (new)
4479cf514ccSChristoph Hellwig 		kmem_cache_free(nfs4_layout_cache, new);
448c5c707f9SChristoph Hellwig 	return nfserr;
4499cf514ccSChristoph Hellwig }
4509cf514ccSChristoph Hellwig 
4519cf514ccSChristoph Hellwig static void
4529cf514ccSChristoph Hellwig nfsd4_free_layouts(struct list_head *reaplist)
4539cf514ccSChristoph Hellwig {
4549cf514ccSChristoph Hellwig 	while (!list_empty(reaplist)) {
4559cf514ccSChristoph Hellwig 		struct nfs4_layout *lp = list_first_entry(reaplist,
4569cf514ccSChristoph Hellwig 				struct nfs4_layout, lo_perstate);
4579cf514ccSChristoph Hellwig 
4589cf514ccSChristoph Hellwig 		list_del(&lp->lo_perstate);
4599cf514ccSChristoph Hellwig 		nfs4_put_stid(&lp->lo_state->ls_stid);
4609cf514ccSChristoph Hellwig 		kmem_cache_free(nfs4_layout_cache, lp);
4619cf514ccSChristoph Hellwig 	}
4629cf514ccSChristoph Hellwig }
4639cf514ccSChristoph Hellwig 
4649cf514ccSChristoph Hellwig static void
4659cf514ccSChristoph Hellwig nfsd4_return_file_layout(struct nfs4_layout *lp, struct nfsd4_layout_seg *seg,
4669cf514ccSChristoph Hellwig 		struct list_head *reaplist)
4679cf514ccSChristoph Hellwig {
4689cf514ccSChristoph Hellwig 	struct nfsd4_layout_seg *lo = &lp->lo_seg;
4699cf514ccSChristoph Hellwig 	u64 end = layout_end(lo);
4709cf514ccSChristoph Hellwig 
4719cf514ccSChristoph Hellwig 	if (seg->offset <= lo->offset) {
4729cf514ccSChristoph Hellwig 		if (layout_end(seg) >= end) {
4739cf514ccSChristoph Hellwig 			list_move_tail(&lp->lo_perstate, reaplist);
4749cf514ccSChristoph Hellwig 			return;
4759cf514ccSChristoph Hellwig 		}
4767890203dSKinglong Mee 		lo->offset = layout_end(seg);
4779cf514ccSChristoph Hellwig 	} else {
4789cf514ccSChristoph Hellwig 		/* retain the whole layout segment on a split. */
4799cf514ccSChristoph Hellwig 		if (layout_end(seg) < end) {
4809cf514ccSChristoph Hellwig 			dprintk("%s: split not supported\n", __func__);
4819cf514ccSChristoph Hellwig 			return;
4829cf514ccSChristoph Hellwig 		}
4837890203dSKinglong Mee 		end = seg->offset;
4849cf514ccSChristoph Hellwig 	}
4859cf514ccSChristoph Hellwig 
4869cf514ccSChristoph Hellwig 	layout_update_len(lo, end);
4879cf514ccSChristoph Hellwig }
4889cf514ccSChristoph Hellwig 
4899cf514ccSChristoph Hellwig __be32
4909cf514ccSChristoph Hellwig nfsd4_return_file_layouts(struct svc_rqst *rqstp,
4919cf514ccSChristoph Hellwig 		struct nfsd4_compound_state *cstate,
4929cf514ccSChristoph Hellwig 		struct nfsd4_layoutreturn *lrp)
4939cf514ccSChristoph Hellwig {
4949cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls;
4959cf514ccSChristoph Hellwig 	struct nfs4_layout *lp, *n;
4969cf514ccSChristoph Hellwig 	LIST_HEAD(reaplist);
4979cf514ccSChristoph Hellwig 	__be32 nfserr;
4989cf514ccSChristoph Hellwig 	int found = 0;
4999cf514ccSChristoph Hellwig 
5009cf514ccSChristoph Hellwig 	nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lrp->lr_sid,
5019cf514ccSChristoph Hellwig 						false, lrp->lr_layout_type,
5029cf514ccSChristoph Hellwig 						&ls);
50331ef83dcSChristoph Hellwig 	if (nfserr) {
504f394b62bSChuck Lever 		trace_nfsd_layout_return_lookup_fail(&lrp->lr_sid);
5059cf514ccSChristoph Hellwig 		return nfserr;
50631ef83dcSChristoph Hellwig 	}
5079cf514ccSChristoph Hellwig 
5089cf514ccSChristoph Hellwig 	spin_lock(&ls->ls_lock);
5099cf514ccSChristoph Hellwig 	list_for_each_entry_safe(lp, n, &ls->ls_layouts, lo_perstate) {
5109cf514ccSChristoph Hellwig 		if (layouts_overlapping(lp, &lrp->lr_seg)) {
5119cf514ccSChristoph Hellwig 			nfsd4_return_file_layout(lp, &lrp->lr_seg, &reaplist);
5129cf514ccSChristoph Hellwig 			found++;
5139cf514ccSChristoph Hellwig 		}
5149cf514ccSChristoph Hellwig 	}
5159cf514ccSChristoph Hellwig 	if (!list_empty(&ls->ls_layouts)) {
5169767feb2SJeff Layton 		if (found)
5179767feb2SJeff Layton 			nfs4_inc_and_copy_stateid(&lrp->lr_sid, &ls->ls_stid);
5189cf514ccSChristoph Hellwig 		lrp->lrs_present = 1;
5199cf514ccSChristoph Hellwig 	} else {
520f394b62bSChuck Lever 		trace_nfsd_layoutstate_unhash(&ls->ls_stid.sc_stateid);
5219cf514ccSChristoph Hellwig 		nfs4_unhash_stid(&ls->ls_stid);
5229cf514ccSChristoph Hellwig 		lrp->lrs_present = 0;
5239cf514ccSChristoph Hellwig 	}
5249cf514ccSChristoph Hellwig 	spin_unlock(&ls->ls_lock);
5259cf514ccSChristoph Hellwig 
526cc8a5532SJeff Layton 	mutex_unlock(&ls->ls_mutex);
5279cf514ccSChristoph Hellwig 	nfs4_put_stid(&ls->ls_stid);
5289cf514ccSChristoph Hellwig 	nfsd4_free_layouts(&reaplist);
5299cf514ccSChristoph Hellwig 	return nfs_ok;
5309cf514ccSChristoph Hellwig }
5319cf514ccSChristoph Hellwig 
5329cf514ccSChristoph Hellwig __be32
5339cf514ccSChristoph Hellwig nfsd4_return_client_layouts(struct svc_rqst *rqstp,
5349cf514ccSChristoph Hellwig 		struct nfsd4_compound_state *cstate,
5359cf514ccSChristoph Hellwig 		struct nfsd4_layoutreturn *lrp)
5369cf514ccSChristoph Hellwig {
5379cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls, *n;
5389cf514ccSChristoph Hellwig 	struct nfs4_client *clp = cstate->clp;
5399cf514ccSChristoph Hellwig 	struct nfs4_layout *lp, *t;
5409cf514ccSChristoph Hellwig 	LIST_HEAD(reaplist);
5419cf514ccSChristoph Hellwig 
5429cf514ccSChristoph Hellwig 	lrp->lrs_present = 0;
5439cf514ccSChristoph Hellwig 
5449cf514ccSChristoph Hellwig 	spin_lock(&clp->cl_lock);
5459cf514ccSChristoph Hellwig 	list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt) {
5466f8f28ecSKinglong Mee 		if (ls->ls_layout_type != lrp->lr_layout_type)
5476f8f28ecSKinglong Mee 			continue;
5486f8f28ecSKinglong Mee 
5499cf514ccSChristoph Hellwig 		if (lrp->lr_return_type == RETURN_FSID &&
5509cf514ccSChristoph Hellwig 		    !fh_fsid_match(&ls->ls_stid.sc_file->fi_fhandle,
5519cf514ccSChristoph Hellwig 				   &cstate->current_fh.fh_handle))
5529cf514ccSChristoph Hellwig 			continue;
5539cf514ccSChristoph Hellwig 
5549cf514ccSChristoph Hellwig 		spin_lock(&ls->ls_lock);
5559cf514ccSChristoph Hellwig 		list_for_each_entry_safe(lp, t, &ls->ls_layouts, lo_perstate) {
5569cf514ccSChristoph Hellwig 			if (lrp->lr_seg.iomode == IOMODE_ANY ||
5579cf514ccSChristoph Hellwig 			    lrp->lr_seg.iomode == lp->lo_seg.iomode)
5589cf514ccSChristoph Hellwig 				list_move_tail(&lp->lo_perstate, &reaplist);
5599cf514ccSChristoph Hellwig 		}
5609cf514ccSChristoph Hellwig 		spin_unlock(&ls->ls_lock);
5619cf514ccSChristoph Hellwig 	}
5629cf514ccSChristoph Hellwig 	spin_unlock(&clp->cl_lock);
5639cf514ccSChristoph Hellwig 
5649cf514ccSChristoph Hellwig 	nfsd4_free_layouts(&reaplist);
5659cf514ccSChristoph Hellwig 	return 0;
5669cf514ccSChristoph Hellwig }
5679cf514ccSChristoph Hellwig 
5689cf514ccSChristoph Hellwig static void
5699cf514ccSChristoph Hellwig nfsd4_return_all_layouts(struct nfs4_layout_stateid *ls,
5709cf514ccSChristoph Hellwig 		struct list_head *reaplist)
5719cf514ccSChristoph Hellwig {
5729cf514ccSChristoph Hellwig 	spin_lock(&ls->ls_lock);
5739cf514ccSChristoph Hellwig 	list_splice_init(&ls->ls_layouts, reaplist);
5749cf514ccSChristoph Hellwig 	spin_unlock(&ls->ls_lock);
5759cf514ccSChristoph Hellwig }
5769cf514ccSChristoph Hellwig 
5779cf514ccSChristoph Hellwig void
5789cf514ccSChristoph Hellwig nfsd4_return_all_client_layouts(struct nfs4_client *clp)
5799cf514ccSChristoph Hellwig {
5809cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls, *n;
5819cf514ccSChristoph Hellwig 	LIST_HEAD(reaplist);
5829cf514ccSChristoph Hellwig 
5839cf514ccSChristoph Hellwig 	spin_lock(&clp->cl_lock);
5849cf514ccSChristoph Hellwig 	list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt)
5859cf514ccSChristoph Hellwig 		nfsd4_return_all_layouts(ls, &reaplist);
5869cf514ccSChristoph Hellwig 	spin_unlock(&clp->cl_lock);
5879cf514ccSChristoph Hellwig 
5889cf514ccSChristoph Hellwig 	nfsd4_free_layouts(&reaplist);
5899cf514ccSChristoph Hellwig }
5909cf514ccSChristoph Hellwig 
5919cf514ccSChristoph Hellwig void
5929cf514ccSChristoph Hellwig nfsd4_return_all_file_layouts(struct nfs4_client *clp, struct nfs4_file *fp)
5939cf514ccSChristoph Hellwig {
5949cf514ccSChristoph Hellwig 	struct nfs4_layout_stateid *ls, *n;
5959cf514ccSChristoph Hellwig 	LIST_HEAD(reaplist);
5969cf514ccSChristoph Hellwig 
5979cf514ccSChristoph Hellwig 	spin_lock(&fp->fi_lock);
5989cf514ccSChristoph Hellwig 	list_for_each_entry_safe(ls, n, &fp->fi_lo_states, ls_perfile) {
5999cf514ccSChristoph Hellwig 		if (ls->ls_stid.sc_client == clp)
6009cf514ccSChristoph Hellwig 			nfsd4_return_all_layouts(ls, &reaplist);
6019cf514ccSChristoph Hellwig 	}
6029cf514ccSChristoph Hellwig 	spin_unlock(&fp->fi_lock);
6039cf514ccSChristoph Hellwig 
6049cf514ccSChristoph Hellwig 	nfsd4_free_layouts(&reaplist);
6059cf514ccSChristoph Hellwig }
6069cf514ccSChristoph Hellwig 
607c5c707f9SChristoph Hellwig static void
608c5c707f9SChristoph Hellwig nfsd4_cb_layout_fail(struct nfs4_layout_stateid *ls)
609c5c707f9SChristoph Hellwig {
610c5c707f9SChristoph Hellwig 	struct nfs4_client *clp = ls->ls_stid.sc_client;
611c5c707f9SChristoph Hellwig 	char addr_str[INET6_ADDRSTRLEN];
612377e7a27SGreg Kroah-Hartman 	static char const nfsd_recall_failed[] = "/sbin/nfsd-recall-failed";
613c5c707f9SChristoph Hellwig 	static char *envp[] = {
614c5c707f9SChristoph Hellwig 		"HOME=/",
615c5c707f9SChristoph Hellwig 		"TERM=linux",
616c5c707f9SChristoph Hellwig 		"PATH=/sbin:/usr/sbin:/bin:/usr/bin",
617c5c707f9SChristoph Hellwig 		NULL
618c5c707f9SChristoph Hellwig 	};
619c5c707f9SChristoph Hellwig 	char *argv[8];
620c5c707f9SChristoph Hellwig 	int error;
621c5c707f9SChristoph Hellwig 
622c5c707f9SChristoph Hellwig 	rpc_ntop((struct sockaddr *)&clp->cl_addr, addr_str, sizeof(addr_str));
623c5c707f9SChristoph Hellwig 
624c5c707f9SChristoph Hellwig 	printk(KERN_WARNING
625c5c707f9SChristoph Hellwig 		"nfsd: client %s failed to respond to layout recall. "
626c5c707f9SChristoph Hellwig 		"  Fencing..\n", addr_str);
627c5c707f9SChristoph Hellwig 
628377e7a27SGreg Kroah-Hartman 	argv[0] = (char *)nfsd_recall_failed;
629c5c707f9SChristoph Hellwig 	argv[1] = addr_str;
630eb82dd39SJeff Layton 	argv[2] = ls->ls_file->nf_file->f_path.mnt->mnt_sb->s_id;
631c5c707f9SChristoph Hellwig 	argv[3] = NULL;
632c5c707f9SChristoph Hellwig 
633377e7a27SGreg Kroah-Hartman 	error = call_usermodehelper(nfsd_recall_failed, argv, envp,
634377e7a27SGreg Kroah-Hartman 				    UMH_WAIT_PROC);
635c5c707f9SChristoph Hellwig 	if (error) {
636c5c707f9SChristoph Hellwig 		printk(KERN_ERR "nfsd: fence failed for client %s: %d!\n",
637c5c707f9SChristoph Hellwig 			addr_str, error);
638c5c707f9SChristoph Hellwig 	}
639c5c707f9SChristoph Hellwig }
640c5c707f9SChristoph Hellwig 
641cc8a5532SJeff Layton static void
642cc8a5532SJeff Layton nfsd4_cb_layout_prepare(struct nfsd4_callback *cb)
643cc8a5532SJeff Layton {
644cc8a5532SJeff Layton 	struct nfs4_layout_stateid *ls =
645cc8a5532SJeff Layton 		container_of(cb, struct nfs4_layout_stateid, ls_recall);
646cc8a5532SJeff Layton 
647cc8a5532SJeff Layton 	mutex_lock(&ls->ls_mutex);
6489767feb2SJeff Layton 	nfs4_inc_and_copy_stateid(&ls->ls_recall_sid, &ls->ls_stid);
649be20aa00SJeff Layton 	mutex_unlock(&ls->ls_mutex);
650cc8a5532SJeff Layton }
651cc8a5532SJeff Layton 
652c5c707f9SChristoph Hellwig static int
653c5c707f9SChristoph Hellwig nfsd4_cb_layout_done(struct nfsd4_callback *cb, struct rpc_task *task)
654c5c707f9SChristoph Hellwig {
655c5c707f9SChristoph Hellwig 	struct nfs4_layout_stateid *ls =
656c5c707f9SChristoph Hellwig 		container_of(cb, struct nfs4_layout_stateid, ls_recall);
6576b9b2107SJeff Layton 	struct nfsd_net *nn;
6586b9b2107SJeff Layton 	ktime_t now, cutoff;
659f99d4fbdSChristoph Hellwig 	const struct nfsd4_layout_ops *ops;
660c5c707f9SChristoph Hellwig 
661*1035d654SChuck Lever 	trace_nfsd_cb_layout_done(&ls->ls_stid.sc_stateid, task);
662c5c707f9SChristoph Hellwig 	switch (task->tk_status) {
663c5c707f9SChristoph Hellwig 	case 0:
6646b9b2107SJeff Layton 	case -NFS4ERR_DELAY:
6656b9b2107SJeff Layton 		/*
6666b9b2107SJeff Layton 		 * Anything left? If not, then call it done. Note that we don't
6676b9b2107SJeff Layton 		 * take the spinlock since this is an optimization and nothing
6686b9b2107SJeff Layton 		 * should get added until the cb counter goes to zero.
6696b9b2107SJeff Layton 		 */
6706b9b2107SJeff Layton 		if (list_empty(&ls->ls_layouts))
671c5c707f9SChristoph Hellwig 			return 1;
6726b9b2107SJeff Layton 
6736b9b2107SJeff Layton 		/* Poll the client until it's done with the layout */
6746b9b2107SJeff Layton 		now = ktime_get();
6756b9b2107SJeff Layton 		nn = net_generic(ls->ls_stid.sc_client->net, nfsd_net_id);
6766b9b2107SJeff Layton 
6776b9b2107SJeff Layton 		/* Client gets 2 lease periods to return it */
6786b9b2107SJeff Layton 		cutoff = ktime_add_ns(task->tk_start,
6792561c92bSArnd Bergmann 					 (u64)nn->nfsd4_lease * NSEC_PER_SEC * 2);
6806b9b2107SJeff Layton 
6816b9b2107SJeff Layton 		if (ktime_before(now, cutoff)) {
6826b9b2107SJeff Layton 			rpc_delay(task, HZ/100); /* 10 mili-seconds */
6836b9b2107SJeff Layton 			return 0;
6846b9b2107SJeff Layton 		}
685df561f66SGustavo A. R. Silva 		fallthrough;
686c5c707f9SChristoph Hellwig 	default:
687c5c707f9SChristoph Hellwig 		/*
688c5c707f9SChristoph Hellwig 		 * Unknown error or non-responding client, we'll need to fence.
689c5c707f9SChristoph Hellwig 		 */
690f394b62bSChuck Lever 		trace_nfsd_layout_recall_fail(&ls->ls_stid.sc_stateid);
691f99d4fbdSChristoph Hellwig 
692f99d4fbdSChristoph Hellwig 		ops = nfsd4_layout_ops[ls->ls_layout_type];
693f99d4fbdSChristoph Hellwig 		if (ops->fence_client)
694f99d4fbdSChristoph Hellwig 			ops->fence_client(ls);
695f99d4fbdSChristoph Hellwig 		else
696c5c707f9SChristoph Hellwig 			nfsd4_cb_layout_fail(ls);
6971c73b9d2SScott Mayhew 		return 1;
698851238a2SJeff Layton 	case -NFS4ERR_NOMATCHING_LAYOUT:
699f394b62bSChuck Lever 		trace_nfsd_layout_recall_done(&ls->ls_stid.sc_stateid);
700851238a2SJeff Layton 		task->tk_status = 0;
701851238a2SJeff Layton 		return 1;
702c5c707f9SChristoph Hellwig 	}
703c5c707f9SChristoph Hellwig }
704c5c707f9SChristoph Hellwig 
705c5c707f9SChristoph Hellwig static void
706c5c707f9SChristoph Hellwig nfsd4_cb_layout_release(struct nfsd4_callback *cb)
707c5c707f9SChristoph Hellwig {
708c5c707f9SChristoph Hellwig 	struct nfs4_layout_stateid *ls =
709c5c707f9SChristoph Hellwig 		container_of(cb, struct nfs4_layout_stateid, ls_recall);
710c5c707f9SChristoph Hellwig 	LIST_HEAD(reaplist);
711c5c707f9SChristoph Hellwig 
712f394b62bSChuck Lever 	trace_nfsd_layout_recall_release(&ls->ls_stid.sc_stateid);
71331ef83dcSChristoph Hellwig 
714c5c707f9SChristoph Hellwig 	nfsd4_return_all_layouts(ls, &reaplist);
715c5c707f9SChristoph Hellwig 	nfsd4_free_layouts(&reaplist);
716c5c707f9SChristoph Hellwig 	nfs4_put_stid(&ls->ls_stid);
717c5c707f9SChristoph Hellwig }
718c5c707f9SChristoph Hellwig 
719c4cb8974SJulia Lawall static const struct nfsd4_callback_ops nfsd4_cb_layout_ops = {
720cc8a5532SJeff Layton 	.prepare	= nfsd4_cb_layout_prepare,
721c5c707f9SChristoph Hellwig 	.done		= nfsd4_cb_layout_done,
722c5c707f9SChristoph Hellwig 	.release	= nfsd4_cb_layout_release,
723c5c707f9SChristoph Hellwig };
724c5c707f9SChristoph Hellwig 
725c5c707f9SChristoph Hellwig static bool
726c5c707f9SChristoph Hellwig nfsd4_layout_lm_break(struct file_lock *fl)
727c5c707f9SChristoph Hellwig {
728c5c707f9SChristoph Hellwig 	/*
729c5c707f9SChristoph Hellwig 	 * We don't want the locks code to timeout the lease for us;
730c5c707f9SChristoph Hellwig 	 * we'll remove it ourself if a layout isn't returned
731c5c707f9SChristoph Hellwig 	 * in time:
732c5c707f9SChristoph Hellwig 	 */
733c5c707f9SChristoph Hellwig 	fl->fl_break_time = 0;
734c5c707f9SChristoph Hellwig 	nfsd4_recall_file_layout(fl->fl_owner);
735c5c707f9SChristoph Hellwig 	return false;
736c5c707f9SChristoph Hellwig }
737c5c707f9SChristoph Hellwig 
738c5c707f9SChristoph Hellwig static int
739c5c707f9SChristoph Hellwig nfsd4_layout_lm_change(struct file_lock *onlist, int arg,
740c5c707f9SChristoph Hellwig 		struct list_head *dispose)
741c5c707f9SChristoph Hellwig {
742c5c707f9SChristoph Hellwig 	BUG_ON(!(arg & F_UNLCK));
743c5c707f9SChristoph Hellwig 	return lease_modify(onlist, arg, dispose);
744c5c707f9SChristoph Hellwig }
745c5c707f9SChristoph Hellwig 
746c5c707f9SChristoph Hellwig static const struct lock_manager_operations nfsd4_layouts_lm_ops = {
747c5c707f9SChristoph Hellwig 	.lm_break	= nfsd4_layout_lm_break,
748c5c707f9SChristoph Hellwig 	.lm_change	= nfsd4_layout_lm_change,
749c5c707f9SChristoph Hellwig };
750c5c707f9SChristoph Hellwig 
7519cf514ccSChristoph Hellwig int
7529cf514ccSChristoph Hellwig nfsd4_init_pnfs(void)
7539cf514ccSChristoph Hellwig {
7549cf514ccSChristoph Hellwig 	int i;
7559cf514ccSChristoph Hellwig 
7569cf514ccSChristoph Hellwig 	for (i = 0; i < DEVID_HASH_SIZE; i++)
7579cf514ccSChristoph Hellwig 		INIT_LIST_HEAD(&nfsd_devid_hash[i]);
7589cf514ccSChristoph Hellwig 
7599cf514ccSChristoph Hellwig 	nfs4_layout_cache = kmem_cache_create("nfs4_layout",
7609cf514ccSChristoph Hellwig 			sizeof(struct nfs4_layout), 0, 0, NULL);
7619cf514ccSChristoph Hellwig 	if (!nfs4_layout_cache)
7629cf514ccSChristoph Hellwig 		return -ENOMEM;
7639cf514ccSChristoph Hellwig 
7649cf514ccSChristoph Hellwig 	nfs4_layout_stateid_cache = kmem_cache_create("nfs4_layout_stateid",
7659cf514ccSChristoph Hellwig 			sizeof(struct nfs4_layout_stateid), 0, 0, NULL);
7669cf514ccSChristoph Hellwig 	if (!nfs4_layout_stateid_cache) {
7679cf514ccSChristoph Hellwig 		kmem_cache_destroy(nfs4_layout_cache);
7689cf514ccSChristoph Hellwig 		return -ENOMEM;
7699cf514ccSChristoph Hellwig 	}
7709cf514ccSChristoph Hellwig 	return 0;
7719cf514ccSChristoph Hellwig }
7729cf514ccSChristoph Hellwig 
7739cf514ccSChristoph Hellwig void
7749cf514ccSChristoph Hellwig nfsd4_exit_pnfs(void)
7759cf514ccSChristoph Hellwig {
7769cf514ccSChristoph Hellwig 	int i;
7779cf514ccSChristoph Hellwig 
7789cf514ccSChristoph Hellwig 	kmem_cache_destroy(nfs4_layout_cache);
7799cf514ccSChristoph Hellwig 	kmem_cache_destroy(nfs4_layout_stateid_cache);
7809cf514ccSChristoph Hellwig 
7819cf514ccSChristoph Hellwig 	for (i = 0; i < DEVID_HASH_SIZE; i++) {
7829cf514ccSChristoph Hellwig 		struct nfsd4_deviceid_map *map, *n;
7839cf514ccSChristoph Hellwig 
7849cf514ccSChristoph Hellwig 		list_for_each_entry_safe(map, n, &nfsd_devid_hash[i], hash)
7859cf514ccSChristoph Hellwig 			kfree(map);
7869cf514ccSChristoph Hellwig 	}
7879cf514ccSChristoph Hellwig }
788