blob: 6d1b5bb051c566fa5f3f864e468b541302c15c07 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02002/*
3 * Copyright (c) 2014 Christoph Hellwig.
4 */
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +01005#include <linux/blkdev.h>
Christoph Hellwigc5c707f2014-09-23 12:38:48 +02006#include <linux/kmod.h>
7#include <linux/file.h>
Christoph Hellwig9cf514c2014-05-05 13:11:59 +02008#include <linux/jhash.h>
9#include <linux/sched.h>
Christoph Hellwigc5c707f2014-09-23 12:38:48 +020010#include <linux/sunrpc/addr.h>
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020011
12#include "pnfs.h"
13#include "netns.h"
Christoph Hellwig31ef83d2014-08-16 19:02:22 -050014#include "trace.h"
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020015
16#define NFSDDBG_FACILITY NFSDDBG_PNFS
17
18struct nfs4_layout {
19 struct list_head lo_perstate;
20 struct nfs4_layout_stateid *lo_state;
21 struct nfsd4_layout_seg lo_seg;
22};
23
24static struct kmem_cache *nfs4_layout_cache;
25static struct kmem_cache *nfs4_layout_stateid_cache;
26
Julia Lawallc4cb8972015-11-21 22:57:39 +010027static const struct nfsd4_callback_ops nfsd4_cb_layout_ops;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +020028static const struct lock_manager_operations nfsd4_layouts_lm_ops;
29
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020030const struct nfsd4_layout_ops *nfsd4_layout_ops[LAYOUT_TYPE_MAX] = {
Tom Haynes9b9960a2016-06-14 13:41:28 -070031#ifdef CONFIG_NFSD_FLEXFILELAYOUT
32 [LAYOUT_FLEX_FILES] = &ff_layout_ops,
33#endif
Christoph Hellwig81c39322016-03-04 20:46:16 +010034#ifdef CONFIG_NFSD_BLOCKLAYOUT
Christoph Hellwig8650b8a2015-01-21 11:40:00 +010035 [LAYOUT_BLOCK_VOLUME] = &bl_layout_ops,
Christoph Hellwig81c39322016-03-04 20:46:16 +010036#endif
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +010037#ifdef CONFIG_NFSD_SCSILAYOUT
38 [LAYOUT_SCSI] = &scsi_layout_ops,
39#endif
Christoph Hellwig9cf514c2014-05-05 13:11:59 +020040};
41
42/* pNFS device ID to export fsid mapping */
43#define DEVID_HASH_BITS 8
44#define DEVID_HASH_SIZE (1 << DEVID_HASH_BITS)
45#define DEVID_HASH_MASK (DEVID_HASH_SIZE - 1)
46static u64 nfsd_devid_seq = 1;
47static struct list_head nfsd_devid_hash[DEVID_HASH_SIZE];
48static DEFINE_SPINLOCK(nfsd_devid_lock);
49
50static inline u32 devid_hashfn(u64 idx)
51{
52 return jhash_2words(idx, idx >> 32, 0) & DEVID_HASH_MASK;
53}
54
55static void
56nfsd4_alloc_devid_map(const struct svc_fh *fhp)
57{
58 const struct knfsd_fh *fh = &fhp->fh_handle;
59 size_t fsid_len = key_len(fh->fh_fsid_type);
60 struct nfsd4_deviceid_map *map, *old;
61 int i;
62
63 map = kzalloc(sizeof(*map) + fsid_len, GFP_KERNEL);
64 if (!map)
65 return;
66
67 map->fsid_type = fh->fh_fsid_type;
68 memcpy(&map->fsid, fh->fh_fsid, fsid_len);
69
70 spin_lock(&nfsd_devid_lock);
71 if (fhp->fh_export->ex_devid_map)
72 goto out_unlock;
73
74 for (i = 0; i < DEVID_HASH_SIZE; i++) {
75 list_for_each_entry(old, &nfsd_devid_hash[i], hash) {
76 if (old->fsid_type != fh->fh_fsid_type)
77 continue;
78 if (memcmp(old->fsid, fh->fh_fsid,
79 key_len(old->fsid_type)))
80 continue;
81
82 fhp->fh_export->ex_devid_map = old;
83 goto out_unlock;
84 }
85 }
86
87 map->idx = nfsd_devid_seq++;
88 list_add_tail_rcu(&map->hash, &nfsd_devid_hash[devid_hashfn(map->idx)]);
89 fhp->fh_export->ex_devid_map = map;
90 map = NULL;
91
92out_unlock:
93 spin_unlock(&nfsd_devid_lock);
94 kfree(map);
95}
96
97struct nfsd4_deviceid_map *
98nfsd4_find_devid_map(int idx)
99{
100 struct nfsd4_deviceid_map *map, *ret = NULL;
101
102 rcu_read_lock();
103 list_for_each_entry_rcu(map, &nfsd_devid_hash[devid_hashfn(idx)], hash)
104 if (map->idx == idx)
105 ret = map;
106 rcu_read_unlock();
107
108 return ret;
109}
110
111int
112nfsd4_set_deviceid(struct nfsd4_deviceid *id, const struct svc_fh *fhp,
113 u32 device_generation)
114{
115 if (!fhp->fh_export->ex_devid_map) {
116 nfsd4_alloc_devid_map(fhp);
117 if (!fhp->fh_export->ex_devid_map)
118 return -ENOMEM;
119 }
120
121 id->fsid_idx = fhp->fh_export->ex_devid_map->idx;
122 id->generation = device_generation;
123 id->pad = 0;
124 return 0;
125}
126
127void nfsd4_setup_layout_type(struct svc_export *exp)
128{
Tom Haynes9b9960a2016-06-14 13:41:28 -0700129#if defined(CONFIG_NFSD_BLOCKLAYOUT) || defined(CONFIG_NFSD_SCSILAYOUT)
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100130 struct super_block *sb = exp->ex_path.mnt->mnt_sb;
Tom Haynes9b9960a2016-06-14 13:41:28 -0700131#endif
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100132
Christoph Hellwigf3f03332015-03-30 18:46:29 +0200133 if (!(exp->ex_flags & NFSEXP_PNFS))
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200134 return;
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100135
Tom Haynes9b9960a2016-06-14 13:41:28 -0700136#ifdef CONFIG_NFSD_FLEXFILELAYOUT
Jeff Layton8a4c3922016-07-10 15:55:58 -0400137 exp->ex_layout_types |= 1 << LAYOUT_FLEX_FILES;
Tom Haynes9b9960a2016-06-14 13:41:28 -0700138#endif
Christoph Hellwig81c39322016-03-04 20:46:16 +0100139#ifdef CONFIG_NFSD_BLOCKLAYOUT
Christoph Hellwig8650b8a2015-01-21 11:40:00 +0100140 if (sb->s_export_op->get_uuid &&
141 sb->s_export_op->map_blocks &&
142 sb->s_export_op->commit_blocks)
Jeff Layton8a4c3922016-07-10 15:55:58 -0400143 exp->ex_layout_types |= 1 << LAYOUT_BLOCK_VOLUME;
Christoph Hellwig81c39322016-03-04 20:46:16 +0100144#endif
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100145#ifdef CONFIG_NFSD_SCSILAYOUT
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100146 if (sb->s_export_op->map_blocks &&
147 sb->s_export_op->commit_blocks &&
Christoph Hellwig8c6aabd2021-10-21 08:06:03 +0200148 sb->s_bdev &&
149 sb->s_bdev->bd_disk->fops->pr_ops &&
150 sb->s_bdev->bd_disk->fops->get_unique_id)
Jeff Layton8a4c3922016-07-10 15:55:58 -0400151 exp->ex_layout_types |= 1 << LAYOUT_SCSI;
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100152#endif
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200153}
154
155static void
156nfsd4_free_layout_stateid(struct nfs4_stid *stid)
157{
158 struct nfs4_layout_stateid *ls = layoutstateid(stid);
159 struct nfs4_client *clp = ls->ls_stid.sc_client;
160 struct nfs4_file *fp = ls->ls_stid.sc_file;
161
Chuck Leverf394b622018-03-27 10:53:11 -0400162 trace_nfsd_layoutstate_free(&ls->ls_stid.sc_stateid);
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500163
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200164 spin_lock(&clp->cl_lock);
165 list_del_init(&ls->ls_perclnt);
166 spin_unlock(&clp->cl_lock);
167
168 spin_lock(&fp->fi_lock);
169 list_del_init(&ls->ls_perfile);
170 spin_unlock(&fp->fi_lock);
171
Jeff Layton1983a662016-08-11 13:36:22 -0400172 if (!nfsd4_layout_ops[ls->ls_layout_type]->disable_recalls)
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400173 vfs_setlease(ls->ls_file->nf_file, F_UNLCK, NULL, (void **)&ls);
174 nfsd_file_put(ls->ls_file);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200175
176 if (ls->ls_recalled)
177 atomic_dec(&ls->ls_stid.sc_file->fi_lo_recalls);
178
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200179 kmem_cache_free(nfs4_layout_stateid_cache, ls);
180}
181
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200182static int
183nfsd4_layout_setlease(struct nfs4_layout_stateid *ls)
184{
185 struct file_lock *fl;
186 int status;
187
Jeff Layton1983a662016-08-11 13:36:22 -0400188 if (nfsd4_layout_ops[ls->ls_layout_type]->disable_recalls)
189 return 0;
190
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200191 fl = locks_alloc_lock();
192 if (!fl)
193 return -ENOMEM;
194 locks_init_lock(fl);
195 fl->fl_lmops = &nfsd4_layouts_lm_ops;
196 fl->fl_flags = FL_LAYOUT;
197 fl->fl_type = F_RDLCK;
198 fl->fl_end = OFFSET_MAX;
199 fl->fl_owner = ls;
200 fl->fl_pid = current->tgid;
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400201 fl->fl_file = ls->ls_file->nf_file;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200202
203 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl, NULL);
204 if (status) {
205 locks_free_lock(fl);
206 return status;
207 }
208 BUG_ON(fl != NULL);
209 return 0;
210}
211
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200212static struct nfs4_layout_stateid *
213nfsd4_alloc_layout_stateid(struct nfsd4_compound_state *cstate,
214 struct nfs4_stid *parent, u32 layout_type)
215{
216 struct nfs4_client *clp = cstate->clp;
217 struct nfs4_file *fp = parent->sc_file;
218 struct nfs4_layout_stateid *ls;
219 struct nfs4_stid *stp;
220
Kinglong Meed19fb702017-01-18 19:04:42 +0800221 stp = nfs4_alloc_stid(cstate->clp, nfs4_layout_stateid_cache,
222 nfsd4_free_layout_stateid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200223 if (!stp)
224 return NULL;
Kinglong Meed19fb702017-01-18 19:04:42 +0800225
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200226 get_nfs4_file(fp);
227 stp->sc_file = fp;
228
229 ls = layoutstateid(stp);
230 INIT_LIST_HEAD(&ls->ls_perclnt);
231 INIT_LIST_HEAD(&ls->ls_perfile);
232 spin_lock_init(&ls->ls_lock);
233 INIT_LIST_HEAD(&ls->ls_layouts);
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400234 mutex_init(&ls->ls_mutex);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200235 ls->ls_layout_type = layout_type;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200236 nfsd4_init_cb(&ls->ls_recall, clp, &nfsd4_cb_layout_ops,
237 NFSPROC4_CLNT_CB_LAYOUT);
238
239 if (parent->sc_type == NFS4_DELEG_STID)
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400240 ls->ls_file = nfsd_file_get(fp->fi_deleg_file);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200241 else
242 ls->ls_file = find_any_file(fp);
243 BUG_ON(!ls->ls_file);
244
245 if (nfsd4_layout_setlease(ls)) {
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400246 nfsd_file_put(ls->ls_file);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200247 put_nfs4_file(fp);
248 kmem_cache_free(nfs4_layout_stateid_cache, ls);
249 return NULL;
250 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200251
252 spin_lock(&clp->cl_lock);
253 stp->sc_type = NFS4_LAYOUT_STID;
254 list_add(&ls->ls_perclnt, &clp->cl_lo_states);
255 spin_unlock(&clp->cl_lock);
256
257 spin_lock(&fp->fi_lock);
258 list_add(&ls->ls_perfile, &fp->fi_lo_states);
259 spin_unlock(&fp->fi_lock);
260
Chuck Leverf394b622018-03-27 10:53:11 -0400261 trace_nfsd_layoutstate_alloc(&ls->ls_stid.sc_stateid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200262 return ls;
263}
264
265__be32
266nfsd4_preprocess_layout_stateid(struct svc_rqst *rqstp,
267 struct nfsd4_compound_state *cstate, stateid_t *stateid,
268 bool create, u32 layout_type, struct nfs4_layout_stateid **lsp)
269{
270 struct nfs4_layout_stateid *ls;
271 struct nfs4_stid *stid;
272 unsigned char typemask = NFS4_LAYOUT_STID;
273 __be32 status;
274
275 if (create)
276 typemask |= (NFS4_OPEN_STID | NFS4_LOCK_STID | NFS4_DELEG_STID);
277
278 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &stid,
279 net_generic(SVC_NET(rqstp), nfsd_net_id));
280 if (status)
281 goto out;
282
283 if (!fh_match(&cstate->current_fh.fh_handle,
284 &stid->sc_file->fi_fhandle)) {
285 status = nfserr_bad_stateid;
286 goto out_put_stid;
287 }
288
289 if (stid->sc_type != NFS4_LAYOUT_STID) {
290 ls = nfsd4_alloc_layout_stateid(cstate, stid, layout_type);
291 nfs4_put_stid(stid);
292
293 status = nfserr_jukebox;
294 if (!ls)
295 goto out;
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400296 mutex_lock(&ls->ls_mutex);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200297 } else {
298 ls = container_of(stid, struct nfs4_layout_stateid, ls_stid);
299
300 status = nfserr_bad_stateid;
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400301 mutex_lock(&ls->ls_mutex);
Jeff Layton14b7f4a2016-05-05 06:53:47 -0400302 if (nfsd4_stateid_generation_after(stateid, &stid->sc_stateid))
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400303 goto out_unlock_stid;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200304 if (layout_type != ls->ls_layout_type)
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400305 goto out_unlock_stid;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200306 }
307
308 *lsp = ls;
309 return 0;
310
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400311out_unlock_stid:
312 mutex_unlock(&ls->ls_mutex);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200313out_put_stid:
314 nfs4_put_stid(stid);
315out:
316 return status;
317}
318
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200319static void
320nfsd4_recall_file_layout(struct nfs4_layout_stateid *ls)
321{
322 spin_lock(&ls->ls_lock);
323 if (ls->ls_recalled)
324 goto out_unlock;
325
326 ls->ls_recalled = true;
327 atomic_inc(&ls->ls_stid.sc_file->fi_lo_recalls);
328 if (list_empty(&ls->ls_layouts))
329 goto out_unlock;
330
Chuck Leverf394b622018-03-27 10:53:11 -0400331 trace_nfsd_layout_recall(&ls->ls_stid.sc_stateid);
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500332
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300333 refcount_inc(&ls->ls_stid.sc_count);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200334 nfsd4_run_cb(&ls->ls_recall);
335
336out_unlock:
337 spin_unlock(&ls->ls_lock);
338}
339
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200340static inline u64
341layout_end(struct nfsd4_layout_seg *seg)
342{
343 u64 end = seg->offset + seg->length;
344 return end >= seg->offset ? end : NFS4_MAX_UINT64;
345}
346
347static void
348layout_update_len(struct nfsd4_layout_seg *lo, u64 end)
349{
350 if (end == NFS4_MAX_UINT64)
351 lo->length = NFS4_MAX_UINT64;
352 else
353 lo->length = end - lo->offset;
354}
355
356static bool
357layouts_overlapping(struct nfs4_layout *lo, struct nfsd4_layout_seg *s)
358{
359 if (s->iomode != IOMODE_ANY && s->iomode != lo->lo_seg.iomode)
360 return false;
361 if (layout_end(&lo->lo_seg) <= s->offset)
362 return false;
363 if (layout_end(s) <= lo->lo_seg.offset)
364 return false;
365 return true;
366}
367
368static bool
369layouts_try_merge(struct nfsd4_layout_seg *lo, struct nfsd4_layout_seg *new)
370{
371 if (lo->iomode != new->iomode)
372 return false;
373 if (layout_end(new) < lo->offset)
374 return false;
375 if (layout_end(lo) < new->offset)
376 return false;
377
378 lo->offset = min(lo->offset, new->offset);
379 layout_update_len(lo, max(layout_end(lo), layout_end(new)));
380 return true;
381}
382
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200383static __be32
384nfsd4_recall_conflict(struct nfs4_layout_stateid *ls)
385{
386 struct nfs4_file *fp = ls->ls_stid.sc_file;
387 struct nfs4_layout_stateid *l, *n;
388 __be32 nfserr = nfs_ok;
389
390 assert_spin_locked(&fp->fi_lock);
391
392 list_for_each_entry_safe(l, n, &fp->fi_lo_states, ls_perfile) {
393 if (l != ls) {
394 nfsd4_recall_file_layout(l);
395 nfserr = nfserr_recallconflict;
396 }
397 }
398
399 return nfserr;
400}
401
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200402__be32
403nfsd4_insert_layout(struct nfsd4_layoutget *lgp, struct nfs4_layout_stateid *ls)
404{
405 struct nfsd4_layout_seg *seg = &lgp->lg_seg;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200406 struct nfs4_file *fp = ls->ls_stid.sc_file;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200407 struct nfs4_layout *lp, *new = NULL;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200408 __be32 nfserr;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200409
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200410 spin_lock(&fp->fi_lock);
411 nfserr = nfsd4_recall_conflict(ls);
412 if (nfserr)
413 goto out;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200414 spin_lock(&ls->ls_lock);
415 list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
416 if (layouts_try_merge(&lp->lo_seg, seg))
417 goto done;
418 }
419 spin_unlock(&ls->ls_lock);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200420 spin_unlock(&fp->fi_lock);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200421
422 new = kmem_cache_alloc(nfs4_layout_cache, GFP_KERNEL);
423 if (!new)
424 return nfserr_jukebox;
425 memcpy(&new->lo_seg, seg, sizeof(lp->lo_seg));
426 new->lo_state = ls;
427
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200428 spin_lock(&fp->fi_lock);
429 nfserr = nfsd4_recall_conflict(ls);
430 if (nfserr)
431 goto out;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200432 spin_lock(&ls->ls_lock);
433 list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
434 if (layouts_try_merge(&lp->lo_seg, seg))
435 goto done;
436 }
437
Elena Reshetovaa15dfcd2017-10-20 12:53:28 +0300438 refcount_inc(&ls->ls_stid.sc_count);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200439 list_add_tail(&new->lo_perstate, &ls->ls_layouts);
440 new = NULL;
441done:
Jeff Layton9767feb2015-10-01 09:05:50 -0400442 nfs4_inc_and_copy_stateid(&lgp->lg_sid, &ls->ls_stid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200443 spin_unlock(&ls->ls_lock);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200444out:
445 spin_unlock(&fp->fi_lock);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200446 if (new)
447 kmem_cache_free(nfs4_layout_cache, new);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200448 return nfserr;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200449}
450
451static void
452nfsd4_free_layouts(struct list_head *reaplist)
453{
454 while (!list_empty(reaplist)) {
455 struct nfs4_layout *lp = list_first_entry(reaplist,
456 struct nfs4_layout, lo_perstate);
457
458 list_del(&lp->lo_perstate);
459 nfs4_put_stid(&lp->lo_state->ls_stid);
460 kmem_cache_free(nfs4_layout_cache, lp);
461 }
462}
463
464static void
465nfsd4_return_file_layout(struct nfs4_layout *lp, struct nfsd4_layout_seg *seg,
466 struct list_head *reaplist)
467{
468 struct nfsd4_layout_seg *lo = &lp->lo_seg;
469 u64 end = layout_end(lo);
470
471 if (seg->offset <= lo->offset) {
472 if (layout_end(seg) >= end) {
473 list_move_tail(&lp->lo_perstate, reaplist);
474 return;
475 }
Kinglong Mee78902032015-03-22 22:17:20 +0800476 lo->offset = layout_end(seg);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200477 } else {
478 /* retain the whole layout segment on a split. */
479 if (layout_end(seg) < end) {
480 dprintk("%s: split not supported\n", __func__);
481 return;
482 }
Kinglong Mee78902032015-03-22 22:17:20 +0800483 end = seg->offset;
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200484 }
485
486 layout_update_len(lo, end);
487}
488
489__be32
490nfsd4_return_file_layouts(struct svc_rqst *rqstp,
491 struct nfsd4_compound_state *cstate,
492 struct nfsd4_layoutreturn *lrp)
493{
494 struct nfs4_layout_stateid *ls;
495 struct nfs4_layout *lp, *n;
496 LIST_HEAD(reaplist);
497 __be32 nfserr;
498 int found = 0;
499
500 nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lrp->lr_sid,
501 false, lrp->lr_layout_type,
502 &ls);
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500503 if (nfserr) {
Chuck Leverf394b622018-03-27 10:53:11 -0400504 trace_nfsd_layout_return_lookup_fail(&lrp->lr_sid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200505 return nfserr;
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500506 }
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200507
508 spin_lock(&ls->ls_lock);
509 list_for_each_entry_safe(lp, n, &ls->ls_layouts, lo_perstate) {
510 if (layouts_overlapping(lp, &lrp->lr_seg)) {
511 nfsd4_return_file_layout(lp, &lrp->lr_seg, &reaplist);
512 found++;
513 }
514 }
515 if (!list_empty(&ls->ls_layouts)) {
Jeff Layton9767feb2015-10-01 09:05:50 -0400516 if (found)
517 nfs4_inc_and_copy_stateid(&lrp->lr_sid, &ls->ls_stid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200518 lrp->lrs_present = 1;
519 } else {
Chuck Leverf394b622018-03-27 10:53:11 -0400520 trace_nfsd_layoutstate_unhash(&ls->ls_stid.sc_stateid);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200521 nfs4_unhash_stid(&ls->ls_stid);
522 lrp->lrs_present = 0;
523 }
524 spin_unlock(&ls->ls_lock);
525
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400526 mutex_unlock(&ls->ls_mutex);
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200527 nfs4_put_stid(&ls->ls_stid);
528 nfsd4_free_layouts(&reaplist);
529 return nfs_ok;
530}
531
532__be32
533nfsd4_return_client_layouts(struct svc_rqst *rqstp,
534 struct nfsd4_compound_state *cstate,
535 struct nfsd4_layoutreturn *lrp)
536{
537 struct nfs4_layout_stateid *ls, *n;
538 struct nfs4_client *clp = cstate->clp;
539 struct nfs4_layout *lp, *t;
540 LIST_HEAD(reaplist);
541
542 lrp->lrs_present = 0;
543
544 spin_lock(&clp->cl_lock);
545 list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt) {
Kinglong Mee6f8f28e2015-03-19 19:04:14 +0800546 if (ls->ls_layout_type != lrp->lr_layout_type)
547 continue;
548
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200549 if (lrp->lr_return_type == RETURN_FSID &&
550 !fh_fsid_match(&ls->ls_stid.sc_file->fi_fhandle,
551 &cstate->current_fh.fh_handle))
552 continue;
553
554 spin_lock(&ls->ls_lock);
555 list_for_each_entry_safe(lp, t, &ls->ls_layouts, lo_perstate) {
556 if (lrp->lr_seg.iomode == IOMODE_ANY ||
557 lrp->lr_seg.iomode == lp->lo_seg.iomode)
558 list_move_tail(&lp->lo_perstate, &reaplist);
559 }
560 spin_unlock(&ls->ls_lock);
561 }
562 spin_unlock(&clp->cl_lock);
563
564 nfsd4_free_layouts(&reaplist);
565 return 0;
566}
567
568static void
569nfsd4_return_all_layouts(struct nfs4_layout_stateid *ls,
570 struct list_head *reaplist)
571{
572 spin_lock(&ls->ls_lock);
573 list_splice_init(&ls->ls_layouts, reaplist);
574 spin_unlock(&ls->ls_lock);
575}
576
577void
578nfsd4_return_all_client_layouts(struct nfs4_client *clp)
579{
580 struct nfs4_layout_stateid *ls, *n;
581 LIST_HEAD(reaplist);
582
583 spin_lock(&clp->cl_lock);
584 list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt)
585 nfsd4_return_all_layouts(ls, &reaplist);
586 spin_unlock(&clp->cl_lock);
587
588 nfsd4_free_layouts(&reaplist);
589}
590
591void
592nfsd4_return_all_file_layouts(struct nfs4_client *clp, struct nfs4_file *fp)
593{
594 struct nfs4_layout_stateid *ls, *n;
595 LIST_HEAD(reaplist);
596
597 spin_lock(&fp->fi_lock);
598 list_for_each_entry_safe(ls, n, &fp->fi_lo_states, ls_perfile) {
599 if (ls->ls_stid.sc_client == clp)
600 nfsd4_return_all_layouts(ls, &reaplist);
601 }
602 spin_unlock(&fp->fi_lock);
603
604 nfsd4_free_layouts(&reaplist);
605}
606
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200607static void
608nfsd4_cb_layout_fail(struct nfs4_layout_stateid *ls)
609{
610 struct nfs4_client *clp = ls->ls_stid.sc_client;
611 char addr_str[INET6_ADDRSTRLEN];
Greg Kroah-Hartman377e7a22016-12-11 18:00:43 +0100612 static char const nfsd_recall_failed[] = "/sbin/nfsd-recall-failed";
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200613 static char *envp[] = {
614 "HOME=/",
615 "TERM=linux",
616 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
617 NULL
618 };
619 char *argv[8];
620 int error;
621
622 rpc_ntop((struct sockaddr *)&clp->cl_addr, addr_str, sizeof(addr_str));
623
624 printk(KERN_WARNING
625 "nfsd: client %s failed to respond to layout recall. "
626 " Fencing..\n", addr_str);
627
Greg Kroah-Hartman377e7a22016-12-11 18:00:43 +0100628 argv[0] = (char *)nfsd_recall_failed;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200629 argv[1] = addr_str;
Jeff Laytoneb82dd32019-08-18 14:18:53 -0400630 argv[2] = ls->ls_file->nf_file->f_path.mnt->mnt_sb->s_id;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200631 argv[3] = NULL;
632
Greg Kroah-Hartman377e7a22016-12-11 18:00:43 +0100633 error = call_usermodehelper(nfsd_recall_failed, argv, envp,
634 UMH_WAIT_PROC);
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200635 if (error) {
636 printk(KERN_ERR "nfsd: fence failed for client %s: %d!\n",
637 addr_str, error);
638 }
639}
640
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400641static void
642nfsd4_cb_layout_prepare(struct nfsd4_callback *cb)
643{
644 struct nfs4_layout_stateid *ls =
645 container_of(cb, struct nfs4_layout_stateid, ls_recall);
646
647 mutex_lock(&ls->ls_mutex);
Jeff Layton9767feb2015-10-01 09:05:50 -0400648 nfs4_inc_and_copy_stateid(&ls->ls_recall_sid, &ls->ls_stid);
Jeff Laytonbe20aa02015-11-29 08:46:14 -0500649 mutex_unlock(&ls->ls_mutex);
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400650}
651
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200652static int
653nfsd4_cb_layout_done(struct nfsd4_callback *cb, struct rpc_task *task)
654{
655 struct nfs4_layout_stateid *ls =
656 container_of(cb, struct nfs4_layout_stateid, ls_recall);
Jeff Layton6b9b2102015-12-08 07:23:48 -0500657 struct nfsd_net *nn;
658 ktime_t now, cutoff;
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100659 const struct nfsd4_layout_ops *ops;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200660
Jeff Layton6b9b2102015-12-08 07:23:48 -0500661
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200662 switch (task->tk_status) {
663 case 0:
Jeff Layton6b9b2102015-12-08 07:23:48 -0500664 case -NFS4ERR_DELAY:
665 /*
666 * Anything left? If not, then call it done. Note that we don't
667 * take the spinlock since this is an optimization and nothing
668 * should get added until the cb counter goes to zero.
669 */
670 if (list_empty(&ls->ls_layouts))
671 return 1;
672
673 /* Poll the client until it's done with the layout */
674 now = ktime_get();
675 nn = net_generic(ls->ls_stid.sc_client->net, nfsd_net_id);
676
677 /* Client gets 2 lease periods to return it */
678 cutoff = ktime_add_ns(task->tk_start,
Arnd Bergmann2561c922019-11-03 22:32:20 +0100679 (u64)nn->nfsd4_lease * NSEC_PER_SEC * 2);
Jeff Layton6b9b2102015-12-08 07:23:48 -0500680
681 if (ktime_before(now, cutoff)) {
682 rpc_delay(task, HZ/100); /* 10 mili-seconds */
683 return 0;
684 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500685 fallthrough;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200686 default:
687 /*
688 * Unknown error or non-responding client, we'll need to fence.
689 */
Chuck Leverf394b622018-03-27 10:53:11 -0400690 trace_nfsd_layout_recall_fail(&ls->ls_stid.sc_stateid);
Christoph Hellwigf99d4fb2016-03-04 20:46:17 +0100691
692 ops = nfsd4_layout_ops[ls->ls_layout_type];
693 if (ops->fence_client)
694 ops->fence_client(ls);
695 else
696 nfsd4_cb_layout_fail(ls);
Scott Mayhew1c73b9d2019-05-02 13:32:12 -0400697 return 1;
Jeff Layton851238a2016-10-20 12:21:34 -0400698 case -NFS4ERR_NOMATCHING_LAYOUT:
Chuck Leverf394b622018-03-27 10:53:11 -0400699 trace_nfsd_layout_recall_done(&ls->ls_stid.sc_stateid);
Jeff Layton851238a2016-10-20 12:21:34 -0400700 task->tk_status = 0;
701 return 1;
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200702 }
703}
704
705static void
706nfsd4_cb_layout_release(struct nfsd4_callback *cb)
707{
708 struct nfs4_layout_stateid *ls =
709 container_of(cb, struct nfs4_layout_stateid, ls_recall);
710 LIST_HEAD(reaplist);
711
Chuck Leverf394b622018-03-27 10:53:11 -0400712 trace_nfsd_layout_recall_release(&ls->ls_stid.sc_stateid);
Christoph Hellwig31ef83d2014-08-16 19:02:22 -0500713
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200714 nfsd4_return_all_layouts(ls, &reaplist);
715 nfsd4_free_layouts(&reaplist);
716 nfs4_put_stid(&ls->ls_stid);
717}
718
Julia Lawallc4cb8972015-11-21 22:57:39 +0100719static const struct nfsd4_callback_ops nfsd4_cb_layout_ops = {
Jeff Laytoncc8a5532015-09-17 07:58:24 -0400720 .prepare = nfsd4_cb_layout_prepare,
Christoph Hellwigc5c707f2014-09-23 12:38:48 +0200721 .done = nfsd4_cb_layout_done,
722 .release = nfsd4_cb_layout_release,
723};
724
725static bool
726nfsd4_layout_lm_break(struct file_lock *fl)
727{
728 /*
729 * We don't want the locks code to timeout the lease for us;
730 * we'll remove it ourself if a layout isn't returned
731 * in time:
732 */
733 fl->fl_break_time = 0;
734 nfsd4_recall_file_layout(fl->fl_owner);
735 return false;
736}
737
738static int
739nfsd4_layout_lm_change(struct file_lock *onlist, int arg,
740 struct list_head *dispose)
741{
742 BUG_ON(!(arg & F_UNLCK));
743 return lease_modify(onlist, arg, dispose);
744}
745
746static const struct lock_manager_operations nfsd4_layouts_lm_ops = {
747 .lm_break = nfsd4_layout_lm_break,
748 .lm_change = nfsd4_layout_lm_change,
749};
750
Christoph Hellwig9cf514c2014-05-05 13:11:59 +0200751int
752nfsd4_init_pnfs(void)
753{
754 int i;
755
756 for (i = 0; i < DEVID_HASH_SIZE; i++)
757 INIT_LIST_HEAD(&nfsd_devid_hash[i]);
758
759 nfs4_layout_cache = kmem_cache_create("nfs4_layout",
760 sizeof(struct nfs4_layout), 0, 0, NULL);
761 if (!nfs4_layout_cache)
762 return -ENOMEM;
763
764 nfs4_layout_stateid_cache = kmem_cache_create("nfs4_layout_stateid",
765 sizeof(struct nfs4_layout_stateid), 0, 0, NULL);
766 if (!nfs4_layout_stateid_cache) {
767 kmem_cache_destroy(nfs4_layout_cache);
768 return -ENOMEM;
769 }
770 return 0;
771}
772
773void
774nfsd4_exit_pnfs(void)
775{
776 int i;
777
778 kmem_cache_destroy(nfs4_layout_cache);
779 kmem_cache_destroy(nfs4_layout_stateid_cache);
780
781 for (i = 0; i < DEVID_HASH_SIZE; i++) {
782 struct nfsd4_deviceid_map *map, *n;
783
784 list_for_each_entry_safe(map, n, &nfsd_devid_hash[i], hash)
785 kfree(map);
786 }
787}