nfs4layouts.c 19 KB

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