Commit | Line | Data |
---|---|---|
9cf514cc CH |
1 | /* |
2 | * Copyright (c) 2014 Christoph Hellwig. | |
3 | */ | |
f99d4fbd | 4 | #include <linux/blkdev.h> |
c5c707f9 CH |
5 | #include <linux/kmod.h> |
6 | #include <linux/file.h> | |
9cf514cc CH |
7 | #include <linux/jhash.h> |
8 | #include <linux/sched.h> | |
c5c707f9 | 9 | #include <linux/sunrpc/addr.h> |
9cf514cc CH |
10 | |
11 | #include "pnfs.h" | |
12 | #include "netns.h" | |
31ef83dc | 13 | #include "trace.h" |
9cf514cc CH |
14 | |
15 | #define NFSDDBG_FACILITY NFSDDBG_PNFS | |
16 | ||
17 | struct nfs4_layout { | |
18 | struct list_head lo_perstate; | |
19 | struct nfs4_layout_stateid *lo_state; | |
20 | struct nfsd4_layout_seg lo_seg; | |
21 | }; | |
22 | ||
23 | static struct kmem_cache *nfs4_layout_cache; | |
24 | static struct kmem_cache *nfs4_layout_stateid_cache; | |
25 | ||
c4cb8974 | 26 | static const struct nfsd4_callback_ops nfsd4_cb_layout_ops; |
c5c707f9 CH |
27 | static const struct lock_manager_operations nfsd4_layouts_lm_ops; |
28 | ||
9cf514cc | 29 | const struct nfsd4_layout_ops *nfsd4_layout_ops[LAYOUT_TYPE_MAX] = { |
81c39329 | 30 | #ifdef CONFIG_NFSD_BLOCKLAYOUT |
8650b8a0 | 31 | [LAYOUT_BLOCK_VOLUME] = &bl_layout_ops, |
81c39329 | 32 | #endif |
f99d4fbd CH |
33 | #ifdef CONFIG_NFSD_SCSILAYOUT |
34 | [LAYOUT_SCSI] = &scsi_layout_ops, | |
35 | #endif | |
9cf514cc CH |
36 | }; |
37 | ||
38 | /* pNFS device ID to export fsid mapping */ | |
39 | #define DEVID_HASH_BITS 8 | |
40 | #define DEVID_HASH_SIZE (1 << DEVID_HASH_BITS) | |
41 | #define DEVID_HASH_MASK (DEVID_HASH_SIZE - 1) | |
42 | static u64 nfsd_devid_seq = 1; | |
43 | static struct list_head nfsd_devid_hash[DEVID_HASH_SIZE]; | |
44 | static DEFINE_SPINLOCK(nfsd_devid_lock); | |
45 | ||
46 | static inline u32 devid_hashfn(u64 idx) | |
47 | { | |
48 | return jhash_2words(idx, idx >> 32, 0) & DEVID_HASH_MASK; | |
49 | } | |
50 | ||
51 | static void | |
52 | nfsd4_alloc_devid_map(const struct svc_fh *fhp) | |
53 | { | |
54 | const struct knfsd_fh *fh = &fhp->fh_handle; | |
55 | size_t fsid_len = key_len(fh->fh_fsid_type); | |
56 | struct nfsd4_deviceid_map *map, *old; | |
57 | int i; | |
58 | ||
59 | map = kzalloc(sizeof(*map) + fsid_len, GFP_KERNEL); | |
60 | if (!map) | |
61 | return; | |
62 | ||
63 | map->fsid_type = fh->fh_fsid_type; | |
64 | memcpy(&map->fsid, fh->fh_fsid, fsid_len); | |
65 | ||
66 | spin_lock(&nfsd_devid_lock); | |
67 | if (fhp->fh_export->ex_devid_map) | |
68 | goto out_unlock; | |
69 | ||
70 | for (i = 0; i < DEVID_HASH_SIZE; i++) { | |
71 | list_for_each_entry(old, &nfsd_devid_hash[i], hash) { | |
72 | if (old->fsid_type != fh->fh_fsid_type) | |
73 | continue; | |
74 | if (memcmp(old->fsid, fh->fh_fsid, | |
75 | key_len(old->fsid_type))) | |
76 | continue; | |
77 | ||
78 | fhp->fh_export->ex_devid_map = old; | |
79 | goto out_unlock; | |
80 | } | |
81 | } | |
82 | ||
83 | map->idx = nfsd_devid_seq++; | |
84 | list_add_tail_rcu(&map->hash, &nfsd_devid_hash[devid_hashfn(map->idx)]); | |
85 | fhp->fh_export->ex_devid_map = map; | |
86 | map = NULL; | |
87 | ||
88 | out_unlock: | |
89 | spin_unlock(&nfsd_devid_lock); | |
90 | kfree(map); | |
91 | } | |
92 | ||
93 | struct nfsd4_deviceid_map * | |
94 | nfsd4_find_devid_map(int idx) | |
95 | { | |
96 | struct nfsd4_deviceid_map *map, *ret = NULL; | |
97 | ||
98 | rcu_read_lock(); | |
99 | list_for_each_entry_rcu(map, &nfsd_devid_hash[devid_hashfn(idx)], hash) | |
100 | if (map->idx == idx) | |
101 | ret = map; | |
102 | rcu_read_unlock(); | |
103 | ||
104 | return ret; | |
105 | } | |
106 | ||
107 | int | |
108 | nfsd4_set_deviceid(struct nfsd4_deviceid *id, const struct svc_fh *fhp, | |
109 | u32 device_generation) | |
110 | { | |
111 | if (!fhp->fh_export->ex_devid_map) { | |
112 | nfsd4_alloc_devid_map(fhp); | |
113 | if (!fhp->fh_export->ex_devid_map) | |
114 | return -ENOMEM; | |
115 | } | |
116 | ||
117 | id->fsid_idx = fhp->fh_export->ex_devid_map->idx; | |
118 | id->generation = device_generation; | |
119 | id->pad = 0; | |
120 | return 0; | |
121 | } | |
122 | ||
123 | void nfsd4_setup_layout_type(struct svc_export *exp) | |
124 | { | |
8650b8a0 CH |
125 | struct super_block *sb = exp->ex_path.mnt->mnt_sb; |
126 | ||
f3f03330 | 127 | if (!(exp->ex_flags & NFSEXP_PNFS)) |
9cf514cc | 128 | return; |
8650b8a0 | 129 | |
f99d4fbd CH |
130 | /* |
131 | * Check if the file system supports exporting a block-like layout. | |
132 | * If the block device supports reservations prefer the SCSI layout, | |
133 | * otherwise advertise the block layout. | |
134 | */ | |
81c39329 | 135 | #ifdef CONFIG_NFSD_BLOCKLAYOUT |
8650b8a0 CH |
136 | if (sb->s_export_op->get_uuid && |
137 | sb->s_export_op->map_blocks && | |
138 | sb->s_export_op->commit_blocks) | |
139 | exp->ex_layout_type = LAYOUT_BLOCK_VOLUME; | |
81c39329 | 140 | #endif |
f99d4fbd CH |
141 | #ifdef CONFIG_NFSD_SCSILAYOUT |
142 | /* overwrite block layout selection if needed */ | |
143 | if (sb->s_export_op->map_blocks && | |
144 | sb->s_export_op->commit_blocks && | |
145 | sb->s_bdev && sb->s_bdev->bd_disk->fops->pr_ops) | |
146 | exp->ex_layout_type = LAYOUT_SCSI; | |
147 | #endif | |
9cf514cc CH |
148 | } |
149 | ||
150 | static void | |
151 | nfsd4_free_layout_stateid(struct nfs4_stid *stid) | |
152 | { | |
153 | struct nfs4_layout_stateid *ls = layoutstateid(stid); | |
154 | struct nfs4_client *clp = ls->ls_stid.sc_client; | |
155 | struct nfs4_file *fp = ls->ls_stid.sc_file; | |
156 | ||
31ef83dc CH |
157 | trace_layoutstate_free(&ls->ls_stid.sc_stateid); |
158 | ||
9cf514cc CH |
159 | spin_lock(&clp->cl_lock); |
160 | list_del_init(&ls->ls_perclnt); | |
161 | spin_unlock(&clp->cl_lock); | |
162 | ||
163 | spin_lock(&fp->fi_lock); | |
164 | list_del_init(&ls->ls_perfile); | |
165 | spin_unlock(&fp->fi_lock); | |
166 | ||
c5c707f9 CH |
167 | vfs_setlease(ls->ls_file, F_UNLCK, NULL, (void **)&ls); |
168 | fput(ls->ls_file); | |
169 | ||
170 | if (ls->ls_recalled) | |
171 | atomic_dec(&ls->ls_stid.sc_file->fi_lo_recalls); | |
172 | ||
9cf514cc CH |
173 | kmem_cache_free(nfs4_layout_stateid_cache, ls); |
174 | } | |
175 | ||
c5c707f9 CH |
176 | static int |
177 | nfsd4_layout_setlease(struct nfs4_layout_stateid *ls) | |
178 | { | |
179 | struct file_lock *fl; | |
180 | int status; | |
181 | ||
182 | fl = locks_alloc_lock(); | |
183 | if (!fl) | |
184 | return -ENOMEM; | |
185 | locks_init_lock(fl); | |
186 | fl->fl_lmops = &nfsd4_layouts_lm_ops; | |
187 | fl->fl_flags = FL_LAYOUT; | |
188 | fl->fl_type = F_RDLCK; | |
189 | fl->fl_end = OFFSET_MAX; | |
190 | fl->fl_owner = ls; | |
191 | fl->fl_pid = current->tgid; | |
192 | fl->fl_file = ls->ls_file; | |
193 | ||
194 | status = vfs_setlease(fl->fl_file, fl->fl_type, &fl, NULL); | |
195 | if (status) { | |
196 | locks_free_lock(fl); | |
197 | return status; | |
198 | } | |
199 | BUG_ON(fl != NULL); | |
200 | return 0; | |
201 | } | |
202 | ||
9cf514cc CH |
203 | static struct nfs4_layout_stateid * |
204 | nfsd4_alloc_layout_stateid(struct nfsd4_compound_state *cstate, | |
205 | struct nfs4_stid *parent, u32 layout_type) | |
206 | { | |
207 | struct nfs4_client *clp = cstate->clp; | |
208 | struct nfs4_file *fp = parent->sc_file; | |
209 | struct nfs4_layout_stateid *ls; | |
210 | struct nfs4_stid *stp; | |
211 | ||
212 | stp = nfs4_alloc_stid(cstate->clp, nfs4_layout_stateid_cache); | |
213 | if (!stp) | |
214 | return NULL; | |
215 | stp->sc_free = nfsd4_free_layout_stateid; | |
216 | get_nfs4_file(fp); | |
217 | stp->sc_file = fp; | |
218 | ||
219 | ls = layoutstateid(stp); | |
220 | INIT_LIST_HEAD(&ls->ls_perclnt); | |
221 | INIT_LIST_HEAD(&ls->ls_perfile); | |
222 | spin_lock_init(&ls->ls_lock); | |
223 | INIT_LIST_HEAD(&ls->ls_layouts); | |
cc8a5532 | 224 | mutex_init(&ls->ls_mutex); |
9cf514cc | 225 | ls->ls_layout_type = layout_type; |
c5c707f9 CH |
226 | nfsd4_init_cb(&ls->ls_recall, clp, &nfsd4_cb_layout_ops, |
227 | NFSPROC4_CLNT_CB_LAYOUT); | |
228 | ||
229 | if (parent->sc_type == NFS4_DELEG_STID) | |
230 | ls->ls_file = get_file(fp->fi_deleg_file); | |
231 | else | |
232 | ls->ls_file = find_any_file(fp); | |
233 | BUG_ON(!ls->ls_file); | |
234 | ||
235 | if (nfsd4_layout_setlease(ls)) { | |
1ca4b88e | 236 | fput(ls->ls_file); |
c5c707f9 CH |
237 | put_nfs4_file(fp); |
238 | kmem_cache_free(nfs4_layout_stateid_cache, ls); | |
239 | return NULL; | |
240 | } | |
9cf514cc CH |
241 | |
242 | spin_lock(&clp->cl_lock); | |
243 | stp->sc_type = NFS4_LAYOUT_STID; | |
244 | list_add(&ls->ls_perclnt, &clp->cl_lo_states); | |
245 | spin_unlock(&clp->cl_lock); | |
246 | ||
247 | spin_lock(&fp->fi_lock); | |
248 | list_add(&ls->ls_perfile, &fp->fi_lo_states); | |
249 | spin_unlock(&fp->fi_lock); | |
250 | ||
31ef83dc | 251 | trace_layoutstate_alloc(&ls->ls_stid.sc_stateid); |
9cf514cc CH |
252 | return ls; |
253 | } | |
254 | ||
255 | __be32 | |
256 | nfsd4_preprocess_layout_stateid(struct svc_rqst *rqstp, | |
257 | struct nfsd4_compound_state *cstate, stateid_t *stateid, | |
258 | bool create, u32 layout_type, struct nfs4_layout_stateid **lsp) | |
259 | { | |
260 | struct nfs4_layout_stateid *ls; | |
261 | struct nfs4_stid *stid; | |
262 | unsigned char typemask = NFS4_LAYOUT_STID; | |
263 | __be32 status; | |
264 | ||
265 | if (create) | |
266 | typemask |= (NFS4_OPEN_STID | NFS4_LOCK_STID | NFS4_DELEG_STID); | |
267 | ||
268 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &stid, | |
269 | net_generic(SVC_NET(rqstp), nfsd_net_id)); | |
270 | if (status) | |
271 | goto out; | |
272 | ||
273 | if (!fh_match(&cstate->current_fh.fh_handle, | |
274 | &stid->sc_file->fi_fhandle)) { | |
275 | status = nfserr_bad_stateid; | |
276 | goto out_put_stid; | |
277 | } | |
278 | ||
279 | if (stid->sc_type != NFS4_LAYOUT_STID) { | |
280 | ls = nfsd4_alloc_layout_stateid(cstate, stid, layout_type); | |
281 | nfs4_put_stid(stid); | |
282 | ||
283 | status = nfserr_jukebox; | |
284 | if (!ls) | |
285 | goto out; | |
cc8a5532 | 286 | mutex_lock(&ls->ls_mutex); |
9cf514cc CH |
287 | } else { |
288 | ls = container_of(stid, struct nfs4_layout_stateid, ls_stid); | |
289 | ||
290 | status = nfserr_bad_stateid; | |
cc8a5532 | 291 | mutex_lock(&ls->ls_mutex); |
14b7f4a1 | 292 | if (nfsd4_stateid_generation_after(stateid, &stid->sc_stateid)) |
cc8a5532 | 293 | goto out_unlock_stid; |
9cf514cc | 294 | if (layout_type != ls->ls_layout_type) |
cc8a5532 | 295 | goto out_unlock_stid; |
9cf514cc CH |
296 | } |
297 | ||
298 | *lsp = ls; | |
299 | return 0; | |
300 | ||
cc8a5532 JL |
301 | out_unlock_stid: |
302 | mutex_unlock(&ls->ls_mutex); | |
9cf514cc CH |
303 | out_put_stid: |
304 | nfs4_put_stid(stid); | |
305 | out: | |
306 | return status; | |
307 | } | |
308 | ||
c5c707f9 CH |
309 | static void |
310 | nfsd4_recall_file_layout(struct nfs4_layout_stateid *ls) | |
311 | { | |
312 | spin_lock(&ls->ls_lock); | |
313 | if (ls->ls_recalled) | |
314 | goto out_unlock; | |
315 | ||
316 | ls->ls_recalled = true; | |
317 | atomic_inc(&ls->ls_stid.sc_file->fi_lo_recalls); | |
318 | if (list_empty(&ls->ls_layouts)) | |
319 | goto out_unlock; | |
320 | ||
31ef83dc CH |
321 | trace_layout_recall(&ls->ls_stid.sc_stateid); |
322 | ||
c5c707f9 | 323 | atomic_inc(&ls->ls_stid.sc_count); |
c5c707f9 CH |
324 | nfsd4_run_cb(&ls->ls_recall); |
325 | ||
326 | out_unlock: | |
327 | spin_unlock(&ls->ls_lock); | |
328 | } | |
329 | ||
9cf514cc CH |
330 | static inline u64 |
331 | layout_end(struct nfsd4_layout_seg *seg) | |
332 | { | |
333 | u64 end = seg->offset + seg->length; | |
334 | return end >= seg->offset ? end : NFS4_MAX_UINT64; | |
335 | } | |
336 | ||
337 | static void | |
338 | layout_update_len(struct nfsd4_layout_seg *lo, u64 end) | |
339 | { | |
340 | if (end == NFS4_MAX_UINT64) | |
341 | lo->length = NFS4_MAX_UINT64; | |
342 | else | |
343 | lo->length = end - lo->offset; | |
344 | } | |
345 | ||
346 | static bool | |
347 | layouts_overlapping(struct nfs4_layout *lo, struct nfsd4_layout_seg *s) | |
348 | { | |
349 | if (s->iomode != IOMODE_ANY && s->iomode != lo->lo_seg.iomode) | |
350 | return false; | |
351 | if (layout_end(&lo->lo_seg) <= s->offset) | |
352 | return false; | |
353 | if (layout_end(s) <= lo->lo_seg.offset) | |
354 | return false; | |
355 | return true; | |
356 | } | |
357 | ||
358 | static bool | |
359 | layouts_try_merge(struct nfsd4_layout_seg *lo, struct nfsd4_layout_seg *new) | |
360 | { | |
361 | if (lo->iomode != new->iomode) | |
362 | return false; | |
363 | if (layout_end(new) < lo->offset) | |
364 | return false; | |
365 | if (layout_end(lo) < new->offset) | |
366 | return false; | |
367 | ||
368 | lo->offset = min(lo->offset, new->offset); | |
369 | layout_update_len(lo, max(layout_end(lo), layout_end(new))); | |
370 | return true; | |
371 | } | |
372 | ||
c5c707f9 CH |
373 | static __be32 |
374 | nfsd4_recall_conflict(struct nfs4_layout_stateid *ls) | |
375 | { | |
376 | struct nfs4_file *fp = ls->ls_stid.sc_file; | |
377 | struct nfs4_layout_stateid *l, *n; | |
378 | __be32 nfserr = nfs_ok; | |
379 | ||
380 | assert_spin_locked(&fp->fi_lock); | |
381 | ||
382 | list_for_each_entry_safe(l, n, &fp->fi_lo_states, ls_perfile) { | |
383 | if (l != ls) { | |
384 | nfsd4_recall_file_layout(l); | |
385 | nfserr = nfserr_recallconflict; | |
386 | } | |
387 | } | |
388 | ||
389 | return nfserr; | |
390 | } | |
391 | ||
9cf514cc CH |
392 | __be32 |
393 | nfsd4_insert_layout(struct nfsd4_layoutget *lgp, struct nfs4_layout_stateid *ls) | |
394 | { | |
395 | struct nfsd4_layout_seg *seg = &lgp->lg_seg; | |
c5c707f9 | 396 | struct nfs4_file *fp = ls->ls_stid.sc_file; |
9cf514cc | 397 | struct nfs4_layout *lp, *new = NULL; |
c5c707f9 | 398 | __be32 nfserr; |
9cf514cc | 399 | |
c5c707f9 CH |
400 | spin_lock(&fp->fi_lock); |
401 | nfserr = nfsd4_recall_conflict(ls); | |
402 | if (nfserr) | |
403 | goto out; | |
9cf514cc CH |
404 | spin_lock(&ls->ls_lock); |
405 | list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) { | |
406 | if (layouts_try_merge(&lp->lo_seg, seg)) | |
407 | goto done; | |
408 | } | |
409 | spin_unlock(&ls->ls_lock); | |
c5c707f9 | 410 | spin_unlock(&fp->fi_lock); |
9cf514cc CH |
411 | |
412 | new = kmem_cache_alloc(nfs4_layout_cache, GFP_KERNEL); | |
413 | if (!new) | |
414 | return nfserr_jukebox; | |
415 | memcpy(&new->lo_seg, seg, sizeof(lp->lo_seg)); | |
416 | new->lo_state = ls; | |
417 | ||
c5c707f9 CH |
418 | spin_lock(&fp->fi_lock); |
419 | nfserr = nfsd4_recall_conflict(ls); | |
420 | if (nfserr) | |
421 | goto out; | |
9cf514cc CH |
422 | spin_lock(&ls->ls_lock); |
423 | list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) { | |
424 | if (layouts_try_merge(&lp->lo_seg, seg)) | |
425 | goto done; | |
426 | } | |
427 | ||
428 | atomic_inc(&ls->ls_stid.sc_count); | |
429 | list_add_tail(&new->lo_perstate, &ls->ls_layouts); | |
430 | new = NULL; | |
431 | done: | |
9767feb2 | 432 | nfs4_inc_and_copy_stateid(&lgp->lg_sid, &ls->ls_stid); |
9cf514cc | 433 | spin_unlock(&ls->ls_lock); |
c5c707f9 CH |
434 | out: |
435 | spin_unlock(&fp->fi_lock); | |
9cf514cc CH |
436 | if (new) |
437 | kmem_cache_free(nfs4_layout_cache, new); | |
c5c707f9 | 438 | return nfserr; |
9cf514cc CH |
439 | } |
440 | ||
441 | static void | |
442 | nfsd4_free_layouts(struct list_head *reaplist) | |
443 | { | |
444 | while (!list_empty(reaplist)) { | |
445 | struct nfs4_layout *lp = list_first_entry(reaplist, | |
446 | struct nfs4_layout, lo_perstate); | |
447 | ||
448 | list_del(&lp->lo_perstate); | |
449 | nfs4_put_stid(&lp->lo_state->ls_stid); | |
450 | kmem_cache_free(nfs4_layout_cache, lp); | |
451 | } | |
452 | } | |
453 | ||
454 | static void | |
455 | nfsd4_return_file_layout(struct nfs4_layout *lp, struct nfsd4_layout_seg *seg, | |
456 | struct list_head *reaplist) | |
457 | { | |
458 | struct nfsd4_layout_seg *lo = &lp->lo_seg; | |
459 | u64 end = layout_end(lo); | |
460 | ||
461 | if (seg->offset <= lo->offset) { | |
462 | if (layout_end(seg) >= end) { | |
463 | list_move_tail(&lp->lo_perstate, reaplist); | |
464 | return; | |
465 | } | |
7890203d | 466 | lo->offset = layout_end(seg); |
9cf514cc CH |
467 | } else { |
468 | /* retain the whole layout segment on a split. */ | |
469 | if (layout_end(seg) < end) { | |
470 | dprintk("%s: split not supported\n", __func__); | |
471 | return; | |
472 | } | |
7890203d | 473 | end = seg->offset; |
9cf514cc CH |
474 | } |
475 | ||
476 | layout_update_len(lo, end); | |
477 | } | |
478 | ||
479 | __be32 | |
480 | nfsd4_return_file_layouts(struct svc_rqst *rqstp, | |
481 | struct nfsd4_compound_state *cstate, | |
482 | struct nfsd4_layoutreturn *lrp) | |
483 | { | |
484 | struct nfs4_layout_stateid *ls; | |
485 | struct nfs4_layout *lp, *n; | |
486 | LIST_HEAD(reaplist); | |
487 | __be32 nfserr; | |
488 | int found = 0; | |
489 | ||
490 | nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lrp->lr_sid, | |
491 | false, lrp->lr_layout_type, | |
492 | &ls); | |
31ef83dc CH |
493 | if (nfserr) { |
494 | trace_layout_return_lookup_fail(&lrp->lr_sid); | |
9cf514cc | 495 | return nfserr; |
31ef83dc | 496 | } |
9cf514cc CH |
497 | |
498 | spin_lock(&ls->ls_lock); | |
499 | list_for_each_entry_safe(lp, n, &ls->ls_layouts, lo_perstate) { | |
500 | if (layouts_overlapping(lp, &lrp->lr_seg)) { | |
501 | nfsd4_return_file_layout(lp, &lrp->lr_seg, &reaplist); | |
502 | found++; | |
503 | } | |
504 | } | |
505 | if (!list_empty(&ls->ls_layouts)) { | |
9767feb2 JL |
506 | if (found) |
507 | nfs4_inc_and_copy_stateid(&lrp->lr_sid, &ls->ls_stid); | |
9cf514cc CH |
508 | lrp->lrs_present = 1; |
509 | } else { | |
31ef83dc | 510 | trace_layoutstate_unhash(&ls->ls_stid.sc_stateid); |
9cf514cc CH |
511 | nfs4_unhash_stid(&ls->ls_stid); |
512 | lrp->lrs_present = 0; | |
513 | } | |
514 | spin_unlock(&ls->ls_lock); | |
515 | ||
cc8a5532 | 516 | mutex_unlock(&ls->ls_mutex); |
9cf514cc CH |
517 | nfs4_put_stid(&ls->ls_stid); |
518 | nfsd4_free_layouts(&reaplist); | |
519 | return nfs_ok; | |
520 | } | |
521 | ||
522 | __be32 | |
523 | nfsd4_return_client_layouts(struct svc_rqst *rqstp, | |
524 | struct nfsd4_compound_state *cstate, | |
525 | struct nfsd4_layoutreturn *lrp) | |
526 | { | |
527 | struct nfs4_layout_stateid *ls, *n; | |
528 | struct nfs4_client *clp = cstate->clp; | |
529 | struct nfs4_layout *lp, *t; | |
530 | LIST_HEAD(reaplist); | |
531 | ||
532 | lrp->lrs_present = 0; | |
533 | ||
534 | spin_lock(&clp->cl_lock); | |
535 | list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt) { | |
6f8f28ec KM |
536 | if (ls->ls_layout_type != lrp->lr_layout_type) |
537 | continue; | |
538 | ||
9cf514cc CH |
539 | if (lrp->lr_return_type == RETURN_FSID && |
540 | !fh_fsid_match(&ls->ls_stid.sc_file->fi_fhandle, | |
541 | &cstate->current_fh.fh_handle)) | |
542 | continue; | |
543 | ||
544 | spin_lock(&ls->ls_lock); | |
545 | list_for_each_entry_safe(lp, t, &ls->ls_layouts, lo_perstate) { | |
546 | if (lrp->lr_seg.iomode == IOMODE_ANY || | |
547 | lrp->lr_seg.iomode == lp->lo_seg.iomode) | |
548 | list_move_tail(&lp->lo_perstate, &reaplist); | |
549 | } | |
550 | spin_unlock(&ls->ls_lock); | |
551 | } | |
552 | spin_unlock(&clp->cl_lock); | |
553 | ||
554 | nfsd4_free_layouts(&reaplist); | |
555 | return 0; | |
556 | } | |
557 | ||
558 | static void | |
559 | nfsd4_return_all_layouts(struct nfs4_layout_stateid *ls, | |
560 | struct list_head *reaplist) | |
561 | { | |
562 | spin_lock(&ls->ls_lock); | |
563 | list_splice_init(&ls->ls_layouts, reaplist); | |
564 | spin_unlock(&ls->ls_lock); | |
565 | } | |
566 | ||
567 | void | |
568 | nfsd4_return_all_client_layouts(struct nfs4_client *clp) | |
569 | { | |
570 | struct nfs4_layout_stateid *ls, *n; | |
571 | LIST_HEAD(reaplist); | |
572 | ||
573 | spin_lock(&clp->cl_lock); | |
574 | list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt) | |
575 | nfsd4_return_all_layouts(ls, &reaplist); | |
576 | spin_unlock(&clp->cl_lock); | |
577 | ||
578 | nfsd4_free_layouts(&reaplist); | |
579 | } | |
580 | ||
581 | void | |
582 | nfsd4_return_all_file_layouts(struct nfs4_client *clp, struct nfs4_file *fp) | |
583 | { | |
584 | struct nfs4_layout_stateid *ls, *n; | |
585 | LIST_HEAD(reaplist); | |
586 | ||
587 | spin_lock(&fp->fi_lock); | |
588 | list_for_each_entry_safe(ls, n, &fp->fi_lo_states, ls_perfile) { | |
589 | if (ls->ls_stid.sc_client == clp) | |
590 | nfsd4_return_all_layouts(ls, &reaplist); | |
591 | } | |
592 | spin_unlock(&fp->fi_lock); | |
593 | ||
594 | nfsd4_free_layouts(&reaplist); | |
595 | } | |
596 | ||
c5c707f9 CH |
597 | static void |
598 | nfsd4_cb_layout_fail(struct nfs4_layout_stateid *ls) | |
599 | { | |
600 | struct nfs4_client *clp = ls->ls_stid.sc_client; | |
601 | char addr_str[INET6_ADDRSTRLEN]; | |
602 | static char *envp[] = { | |
603 | "HOME=/", | |
604 | "TERM=linux", | |
605 | "PATH=/sbin:/usr/sbin:/bin:/usr/bin", | |
606 | NULL | |
607 | }; | |
608 | char *argv[8]; | |
609 | int error; | |
610 | ||
611 | rpc_ntop((struct sockaddr *)&clp->cl_addr, addr_str, sizeof(addr_str)); | |
612 | ||
613 | printk(KERN_WARNING | |
614 | "nfsd: client %s failed to respond to layout recall. " | |
615 | " Fencing..\n", addr_str); | |
616 | ||
617 | argv[0] = "/sbin/nfsd-recall-failed"; | |
618 | argv[1] = addr_str; | |
619 | argv[2] = ls->ls_file->f_path.mnt->mnt_sb->s_id; | |
620 | argv[3] = NULL; | |
621 | ||
622 | error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC); | |
623 | if (error) { | |
624 | printk(KERN_ERR "nfsd: fence failed for client %s: %d!\n", | |
625 | addr_str, error); | |
626 | } | |
627 | } | |
628 | ||
cc8a5532 JL |
629 | static void |
630 | nfsd4_cb_layout_prepare(struct nfsd4_callback *cb) | |
631 | { | |
632 | struct nfs4_layout_stateid *ls = | |
633 | container_of(cb, struct nfs4_layout_stateid, ls_recall); | |
634 | ||
635 | mutex_lock(&ls->ls_mutex); | |
9767feb2 | 636 | nfs4_inc_and_copy_stateid(&ls->ls_recall_sid, &ls->ls_stid); |
be20aa00 | 637 | mutex_unlock(&ls->ls_mutex); |
cc8a5532 JL |
638 | } |
639 | ||
c5c707f9 CH |
640 | static int |
641 | nfsd4_cb_layout_done(struct nfsd4_callback *cb, struct rpc_task *task) | |
642 | { | |
643 | struct nfs4_layout_stateid *ls = | |
644 | container_of(cb, struct nfs4_layout_stateid, ls_recall); | |
6b9b2107 JL |
645 | struct nfsd_net *nn; |
646 | ktime_t now, cutoff; | |
f99d4fbd | 647 | const struct nfsd4_layout_ops *ops; |
c5c707f9 CH |
648 | LIST_HEAD(reaplist); |
649 | ||
6b9b2107 | 650 | |
c5c707f9 CH |
651 | switch (task->tk_status) { |
652 | case 0: | |
6b9b2107 JL |
653 | case -NFS4ERR_DELAY: |
654 | /* | |
655 | * Anything left? If not, then call it done. Note that we don't | |
656 | * take the spinlock since this is an optimization and nothing | |
657 | * should get added until the cb counter goes to zero. | |
658 | */ | |
659 | if (list_empty(&ls->ls_layouts)) | |
660 | return 1; | |
661 | ||
662 | /* Poll the client until it's done with the layout */ | |
663 | now = ktime_get(); | |
664 | nn = net_generic(ls->ls_stid.sc_client->net, nfsd_net_id); | |
665 | ||
666 | /* Client gets 2 lease periods to return it */ | |
667 | cutoff = ktime_add_ns(task->tk_start, | |
668 | nn->nfsd4_lease * NSEC_PER_SEC * 2); | |
669 | ||
670 | if (ktime_before(now, cutoff)) { | |
671 | rpc_delay(task, HZ/100); /* 10 mili-seconds */ | |
672 | return 0; | |
673 | } | |
674 | /* Fallthrough */ | |
c5c707f9 | 675 | case -NFS4ERR_NOMATCHING_LAYOUT: |
31ef83dc | 676 | trace_layout_recall_done(&ls->ls_stid.sc_stateid); |
c5c707f9 CH |
677 | task->tk_status = 0; |
678 | return 1; | |
c5c707f9 CH |
679 | default: |
680 | /* | |
681 | * Unknown error or non-responding client, we'll need to fence. | |
682 | */ | |
f99d4fbd CH |
683 | trace_layout_recall_fail(&ls->ls_stid.sc_stateid); |
684 | ||
685 | ops = nfsd4_layout_ops[ls->ls_layout_type]; | |
686 | if (ops->fence_client) | |
687 | ops->fence_client(ls); | |
688 | else | |
689 | nfsd4_cb_layout_fail(ls); | |
c5c707f9 CH |
690 | return -1; |
691 | } | |
692 | } | |
693 | ||
694 | static void | |
695 | nfsd4_cb_layout_release(struct nfsd4_callback *cb) | |
696 | { | |
697 | struct nfs4_layout_stateid *ls = | |
698 | container_of(cb, struct nfs4_layout_stateid, ls_recall); | |
699 | LIST_HEAD(reaplist); | |
700 | ||
31ef83dc CH |
701 | trace_layout_recall_release(&ls->ls_stid.sc_stateid); |
702 | ||
c5c707f9 CH |
703 | nfsd4_return_all_layouts(ls, &reaplist); |
704 | nfsd4_free_layouts(&reaplist); | |
705 | nfs4_put_stid(&ls->ls_stid); | |
706 | } | |
707 | ||
c4cb8974 | 708 | static const struct nfsd4_callback_ops nfsd4_cb_layout_ops = { |
cc8a5532 | 709 | .prepare = nfsd4_cb_layout_prepare, |
c5c707f9 CH |
710 | .done = nfsd4_cb_layout_done, |
711 | .release = nfsd4_cb_layout_release, | |
712 | }; | |
713 | ||
714 | static bool | |
715 | nfsd4_layout_lm_break(struct file_lock *fl) | |
716 | { | |
717 | /* | |
718 | * We don't want the locks code to timeout the lease for us; | |
719 | * we'll remove it ourself if a layout isn't returned | |
720 | * in time: | |
721 | */ | |
722 | fl->fl_break_time = 0; | |
723 | nfsd4_recall_file_layout(fl->fl_owner); | |
724 | return false; | |
725 | } | |
726 | ||
727 | static int | |
728 | nfsd4_layout_lm_change(struct file_lock *onlist, int arg, | |
729 | struct list_head *dispose) | |
730 | { | |
731 | BUG_ON(!(arg & F_UNLCK)); | |
732 | return lease_modify(onlist, arg, dispose); | |
733 | } | |
734 | ||
735 | static const struct lock_manager_operations nfsd4_layouts_lm_ops = { | |
736 | .lm_break = nfsd4_layout_lm_break, | |
737 | .lm_change = nfsd4_layout_lm_change, | |
738 | }; | |
739 | ||
9cf514cc CH |
740 | int |
741 | nfsd4_init_pnfs(void) | |
742 | { | |
743 | int i; | |
744 | ||
745 | for (i = 0; i < DEVID_HASH_SIZE; i++) | |
746 | INIT_LIST_HEAD(&nfsd_devid_hash[i]); | |
747 | ||
748 | nfs4_layout_cache = kmem_cache_create("nfs4_layout", | |
749 | sizeof(struct nfs4_layout), 0, 0, NULL); | |
750 | if (!nfs4_layout_cache) | |
751 | return -ENOMEM; | |
752 | ||
753 | nfs4_layout_stateid_cache = kmem_cache_create("nfs4_layout_stateid", | |
754 | sizeof(struct nfs4_layout_stateid), 0, 0, NULL); | |
755 | if (!nfs4_layout_stateid_cache) { | |
756 | kmem_cache_destroy(nfs4_layout_cache); | |
757 | return -ENOMEM; | |
758 | } | |
759 | return 0; | |
760 | } | |
761 | ||
762 | void | |
763 | nfsd4_exit_pnfs(void) | |
764 | { | |
765 | int i; | |
766 | ||
767 | kmem_cache_destroy(nfs4_layout_cache); | |
768 | kmem_cache_destroy(nfs4_layout_stateid_cache); | |
769 | ||
770 | for (i = 0; i < DEVID_HASH_SIZE; i++) { | |
771 | struct nfsd4_deviceid_map *map, *n; | |
772 | ||
773 | list_for_each_entry_safe(map, n, &nfsd_devid_hash[i], hash) | |
774 | kfree(map); | |
775 | } | |
776 | } |