nfs4layouts.c 19 KB

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