nfs4layouts.c 19 KB

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