nfs4layouts.c 19 KB

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