nfs4layouts.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * Copyright (c) 2014 Christoph Hellwig.
  3. */
  4. #include <linux/blkdev.h>
  5. #include <linux/kmod.h>
  6. #include <linux/file.h>
  7. #include <linux/jhash.h>
  8. #include <linux/sched.h>
  9. #include <linux/sunrpc/addr.h>
  10. #include "pnfs.h"
  11. #include "netns.h"
  12. #include "trace.h"
  13. #define NFSDDBG_FACILITY NFSDDBG_PNFS
  14. struct nfs4_layout {
  15. struct list_head lo_perstate;
  16. struct nfs4_layout_stateid *lo_state;
  17. struct nfsd4_layout_seg lo_seg;
  18. };
  19. static struct kmem_cache *nfs4_layout_cache;
  20. static struct kmem_cache *nfs4_layout_stateid_cache;
  21. static const struct nfsd4_callback_ops nfsd4_cb_layout_ops;
  22. static const struct lock_manager_operations nfsd4_layouts_lm_ops;
  23. const struct nfsd4_layout_ops *nfsd4_layout_ops[LAYOUT_TYPE_MAX] = {
  24. #ifdef CONFIG_NFSD_FLEXFILELAYOUT
  25. [LAYOUT_FLEX_FILES] = &ff_layout_ops,
  26. #endif
  27. #ifdef CONFIG_NFSD_BLOCKLAYOUT
  28. [LAYOUT_BLOCK_VOLUME] = &bl_layout_ops,
  29. #endif
  30. #ifdef CONFIG_NFSD_SCSILAYOUT
  31. [LAYOUT_SCSI] = &scsi_layout_ops,
  32. #endif
  33. };
  34. /* pNFS device ID to export fsid mapping */
  35. #define DEVID_HASH_BITS 8
  36. #define DEVID_HASH_SIZE (1 << DEVID_HASH_BITS)
  37. #define DEVID_HASH_MASK (DEVID_HASH_SIZE - 1)
  38. static u64 nfsd_devid_seq = 1;
  39. static struct list_head nfsd_devid_hash[DEVID_HASH_SIZE];
  40. static DEFINE_SPINLOCK(nfsd_devid_lock);
  41. static inline u32 devid_hashfn(u64 idx)
  42. {
  43. return jhash_2words(idx, idx >> 32, 0) & DEVID_HASH_MASK;
  44. }
  45. static void
  46. nfsd4_alloc_devid_map(const struct svc_fh *fhp)
  47. {
  48. const struct knfsd_fh *fh = &fhp->fh_handle;
  49. size_t fsid_len = key_len(fh->fh_fsid_type);
  50. struct nfsd4_deviceid_map *map, *old;
  51. int i;
  52. map = kzalloc(sizeof(*map) + fsid_len, GFP_KERNEL);
  53. if (!map)
  54. return;
  55. map->fsid_type = fh->fh_fsid_type;
  56. memcpy(&map->fsid, fh->fh_fsid, fsid_len);
  57. spin_lock(&nfsd_devid_lock);
  58. if (fhp->fh_export->ex_devid_map)
  59. goto out_unlock;
  60. for (i = 0; i < DEVID_HASH_SIZE; i++) {
  61. list_for_each_entry(old, &nfsd_devid_hash[i], hash) {
  62. if (old->fsid_type != fh->fh_fsid_type)
  63. continue;
  64. if (memcmp(old->fsid, fh->fh_fsid,
  65. key_len(old->fsid_type)))
  66. continue;
  67. fhp->fh_export->ex_devid_map = old;
  68. goto out_unlock;
  69. }
  70. }
  71. map->idx = nfsd_devid_seq++;
  72. list_add_tail_rcu(&map->hash, &nfsd_devid_hash[devid_hashfn(map->idx)]);
  73. fhp->fh_export->ex_devid_map = map;
  74. map = NULL;
  75. out_unlock:
  76. spin_unlock(&nfsd_devid_lock);
  77. kfree(map);
  78. }
  79. struct nfsd4_deviceid_map *
  80. nfsd4_find_devid_map(int idx)
  81. {
  82. struct nfsd4_deviceid_map *map, *ret = NULL;
  83. rcu_read_lock();
  84. list_for_each_entry_rcu(map, &nfsd_devid_hash[devid_hashfn(idx)], hash)
  85. if (map->idx == idx)
  86. ret = map;
  87. rcu_read_unlock();
  88. return ret;
  89. }
  90. int
  91. nfsd4_set_deviceid(struct nfsd4_deviceid *id, const struct svc_fh *fhp,
  92. u32 device_generation)
  93. {
  94. if (!fhp->fh_export->ex_devid_map) {
  95. nfsd4_alloc_devid_map(fhp);
  96. if (!fhp->fh_export->ex_devid_map)
  97. return -ENOMEM;
  98. }
  99. id->fsid_idx = fhp->fh_export->ex_devid_map->idx;
  100. id->generation = device_generation;
  101. id->pad = 0;
  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. /*
  112. * If flex file is configured, use it by default. Otherwise
  113. * check if the file system supports exporting a block-like layout.
  114. * If the block device supports reservations prefer the SCSI layout,
  115. * otherwise advertise the block layout.
  116. */
  117. #ifdef CONFIG_NFSD_FLEXFILELAYOUT
  118. exp->ex_layout_types |= 1 << LAYOUT_FLEX_FILES;
  119. #endif
  120. #ifdef CONFIG_NFSD_BLOCKLAYOUT
  121. /* overwrite flex file layout selection if needed */
  122. if (sb->s_export_op->get_uuid &&
  123. sb->s_export_op->map_blocks &&
  124. sb->s_export_op->commit_blocks)
  125. exp->ex_layout_types |= 1 << LAYOUT_BLOCK_VOLUME;
  126. #endif
  127. #ifdef CONFIG_NFSD_SCSILAYOUT
  128. /* overwrite block layout selection if needed */
  129. if (sb->s_export_op->map_blocks &&
  130. sb->s_export_op->commit_blocks &&
  131. sb->s_bdev && sb->s_bdev->bd_disk->fops->pr_ops)
  132. exp->ex_layout_types |= 1 << LAYOUT_SCSI;
  133. #endif
  134. }
  135. static void
  136. nfsd4_free_layout_stateid(struct nfs4_stid *stid)
  137. {
  138. struct nfs4_layout_stateid *ls = layoutstateid(stid);
  139. struct nfs4_client *clp = ls->ls_stid.sc_client;
  140. struct nfs4_file *fp = ls->ls_stid.sc_file;
  141. trace_layoutstate_free(&ls->ls_stid.sc_stateid);
  142. spin_lock(&clp->cl_lock);
  143. list_del_init(&ls->ls_perclnt);
  144. spin_unlock(&clp->cl_lock);
  145. spin_lock(&fp->fi_lock);
  146. list_del_init(&ls->ls_perfile);
  147. spin_unlock(&fp->fi_lock);
  148. vfs_setlease(ls->ls_file, F_UNLCK, NULL, (void **)&ls);
  149. fput(ls->ls_file);
  150. if (ls->ls_recalled)
  151. atomic_dec(&ls->ls_stid.sc_file->fi_lo_recalls);
  152. kmem_cache_free(nfs4_layout_stateid_cache, ls);
  153. }
  154. static int
  155. nfsd4_layout_setlease(struct nfs4_layout_stateid *ls)
  156. {
  157. struct file_lock *fl;
  158. int status;
  159. fl = locks_alloc_lock();
  160. if (!fl)
  161. return -ENOMEM;
  162. locks_init_lock(fl);
  163. fl->fl_lmops = &nfsd4_layouts_lm_ops;
  164. fl->fl_flags = FL_LAYOUT;
  165. fl->fl_type = F_RDLCK;
  166. fl->fl_end = OFFSET_MAX;
  167. fl->fl_owner = ls;
  168. fl->fl_pid = current->tgid;
  169. fl->fl_file = ls->ls_file;
  170. status = vfs_setlease(fl->fl_file, fl->fl_type, &fl, NULL);
  171. if (status) {
  172. locks_free_lock(fl);
  173. return status;
  174. }
  175. BUG_ON(fl != NULL);
  176. return 0;
  177. }
  178. static struct nfs4_layout_stateid *
  179. nfsd4_alloc_layout_stateid(struct nfsd4_compound_state *cstate,
  180. struct nfs4_stid *parent, u32 layout_type)
  181. {
  182. struct nfs4_client *clp = cstate->clp;
  183. struct nfs4_file *fp = parent->sc_file;
  184. struct nfs4_layout_stateid *ls;
  185. struct nfs4_stid *stp;
  186. stp = nfs4_alloc_stid(cstate->clp, nfs4_layout_stateid_cache);
  187. if (!stp)
  188. return NULL;
  189. stp->sc_free = nfsd4_free_layout_stateid;
  190. get_nfs4_file(fp);
  191. stp->sc_file = fp;
  192. ls = layoutstateid(stp);
  193. INIT_LIST_HEAD(&ls->ls_perclnt);
  194. INIT_LIST_HEAD(&ls->ls_perfile);
  195. spin_lock_init(&ls->ls_lock);
  196. INIT_LIST_HEAD(&ls->ls_layouts);
  197. mutex_init(&ls->ls_mutex);
  198. ls->ls_layout_type = layout_type;
  199. nfsd4_init_cb(&ls->ls_recall, clp, &nfsd4_cb_layout_ops,
  200. NFSPROC4_CLNT_CB_LAYOUT);
  201. if (parent->sc_type == NFS4_DELEG_STID)
  202. ls->ls_file = get_file(fp->fi_deleg_file);
  203. else
  204. ls->ls_file = find_any_file(fp);
  205. BUG_ON(!ls->ls_file);
  206. if (nfsd4_layout_setlease(ls)) {
  207. fput(ls->ls_file);
  208. put_nfs4_file(fp);
  209. kmem_cache_free(nfs4_layout_stateid_cache, ls);
  210. return NULL;
  211. }
  212. spin_lock(&clp->cl_lock);
  213. stp->sc_type = NFS4_LAYOUT_STID;
  214. list_add(&ls->ls_perclnt, &clp->cl_lo_states);
  215. spin_unlock(&clp->cl_lock);
  216. spin_lock(&fp->fi_lock);
  217. list_add(&ls->ls_perfile, &fp->fi_lo_states);
  218. spin_unlock(&fp->fi_lock);
  219. trace_layoutstate_alloc(&ls->ls_stid.sc_stateid);
  220. return ls;
  221. }
  222. __be32
  223. nfsd4_preprocess_layout_stateid(struct svc_rqst *rqstp,
  224. struct nfsd4_compound_state *cstate, stateid_t *stateid,
  225. bool create, u32 layout_type, struct nfs4_layout_stateid **lsp)
  226. {
  227. struct nfs4_layout_stateid *ls;
  228. struct nfs4_stid *stid;
  229. unsigned char typemask = NFS4_LAYOUT_STID;
  230. __be32 status;
  231. if (create)
  232. typemask |= (NFS4_OPEN_STID | NFS4_LOCK_STID | NFS4_DELEG_STID);
  233. status = nfsd4_lookup_stateid(cstate, stateid, typemask, &stid,
  234. net_generic(SVC_NET(rqstp), nfsd_net_id));
  235. if (status)
  236. goto out;
  237. if (!fh_match(&cstate->current_fh.fh_handle,
  238. &stid->sc_file->fi_fhandle)) {
  239. status = nfserr_bad_stateid;
  240. goto out_put_stid;
  241. }
  242. if (stid->sc_type != NFS4_LAYOUT_STID) {
  243. ls = nfsd4_alloc_layout_stateid(cstate, stid, layout_type);
  244. nfs4_put_stid(stid);
  245. status = nfserr_jukebox;
  246. if (!ls)
  247. goto out;
  248. mutex_lock(&ls->ls_mutex);
  249. } else {
  250. ls = container_of(stid, struct nfs4_layout_stateid, ls_stid);
  251. status = nfserr_bad_stateid;
  252. mutex_lock(&ls->ls_mutex);
  253. if (nfsd4_stateid_generation_after(stateid, &stid->sc_stateid))
  254. goto out_unlock_stid;
  255. if (layout_type != ls->ls_layout_type)
  256. goto out_unlock_stid;
  257. }
  258. *lsp = ls;
  259. return 0;
  260. out_unlock_stid:
  261. mutex_unlock(&ls->ls_mutex);
  262. out_put_stid:
  263. nfs4_put_stid(stid);
  264. out:
  265. return status;
  266. }
  267. static void
  268. nfsd4_recall_file_layout(struct nfs4_layout_stateid *ls)
  269. {
  270. spin_lock(&ls->ls_lock);
  271. if (ls->ls_recalled)
  272. goto out_unlock;
  273. ls->ls_recalled = true;
  274. atomic_inc(&ls->ls_stid.sc_file->fi_lo_recalls);
  275. if (list_empty(&ls->ls_layouts))
  276. goto out_unlock;
  277. trace_layout_recall(&ls->ls_stid.sc_stateid);
  278. atomic_inc(&ls->ls_stid.sc_count);
  279. nfsd4_run_cb(&ls->ls_recall);
  280. out_unlock:
  281. spin_unlock(&ls->ls_lock);
  282. }
  283. static inline u64
  284. layout_end(struct nfsd4_layout_seg *seg)
  285. {
  286. u64 end = seg->offset + seg->length;
  287. return end >= seg->offset ? end : NFS4_MAX_UINT64;
  288. }
  289. static void
  290. layout_update_len(struct nfsd4_layout_seg *lo, u64 end)
  291. {
  292. if (end == NFS4_MAX_UINT64)
  293. lo->length = NFS4_MAX_UINT64;
  294. else
  295. lo->length = end - lo->offset;
  296. }
  297. static bool
  298. layouts_overlapping(struct nfs4_layout *lo, struct nfsd4_layout_seg *s)
  299. {
  300. if (s->iomode != IOMODE_ANY && s->iomode != lo->lo_seg.iomode)
  301. return false;
  302. if (layout_end(&lo->lo_seg) <= s->offset)
  303. return false;
  304. if (layout_end(s) <= lo->lo_seg.offset)
  305. return false;
  306. return true;
  307. }
  308. static bool
  309. layouts_try_merge(struct nfsd4_layout_seg *lo, struct nfsd4_layout_seg *new)
  310. {
  311. if (lo->iomode != new->iomode)
  312. return false;
  313. if (layout_end(new) < lo->offset)
  314. return false;
  315. if (layout_end(lo) < new->offset)
  316. return false;
  317. lo->offset = min(lo->offset, new->offset);
  318. layout_update_len(lo, max(layout_end(lo), layout_end(new)));
  319. return true;
  320. }
  321. static __be32
  322. nfsd4_recall_conflict(struct nfs4_layout_stateid *ls)
  323. {
  324. struct nfs4_file *fp = ls->ls_stid.sc_file;
  325. struct nfs4_layout_stateid *l, *n;
  326. __be32 nfserr = nfs_ok;
  327. assert_spin_locked(&fp->fi_lock);
  328. list_for_each_entry_safe(l, n, &fp->fi_lo_states, ls_perfile) {
  329. if (l != ls) {
  330. nfsd4_recall_file_layout(l);
  331. nfserr = nfserr_recallconflict;
  332. }
  333. }
  334. return nfserr;
  335. }
  336. __be32
  337. nfsd4_insert_layout(struct nfsd4_layoutget *lgp, struct nfs4_layout_stateid *ls)
  338. {
  339. struct nfsd4_layout_seg *seg = &lgp->lg_seg;
  340. struct nfs4_file *fp = ls->ls_stid.sc_file;
  341. struct nfs4_layout *lp, *new = NULL;
  342. __be32 nfserr;
  343. spin_lock(&fp->fi_lock);
  344. nfserr = nfsd4_recall_conflict(ls);
  345. if (nfserr)
  346. goto out;
  347. spin_lock(&ls->ls_lock);
  348. list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
  349. if (layouts_try_merge(&lp->lo_seg, seg))
  350. goto done;
  351. }
  352. spin_unlock(&ls->ls_lock);
  353. spin_unlock(&fp->fi_lock);
  354. new = kmem_cache_alloc(nfs4_layout_cache, GFP_KERNEL);
  355. if (!new)
  356. return nfserr_jukebox;
  357. memcpy(&new->lo_seg, seg, sizeof(lp->lo_seg));
  358. new->lo_state = ls;
  359. spin_lock(&fp->fi_lock);
  360. nfserr = nfsd4_recall_conflict(ls);
  361. if (nfserr)
  362. goto out;
  363. spin_lock(&ls->ls_lock);
  364. list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
  365. if (layouts_try_merge(&lp->lo_seg, seg))
  366. goto done;
  367. }
  368. atomic_inc(&ls->ls_stid.sc_count);
  369. list_add_tail(&new->lo_perstate, &ls->ls_layouts);
  370. new = NULL;
  371. done:
  372. nfs4_inc_and_copy_stateid(&lgp->lg_sid, &ls->ls_stid);
  373. spin_unlock(&ls->ls_lock);
  374. out:
  375. spin_unlock(&fp->fi_lock);
  376. if (new)
  377. kmem_cache_free(nfs4_layout_cache, new);
  378. return nfserr;
  379. }
  380. static void
  381. nfsd4_free_layouts(struct list_head *reaplist)
  382. {
  383. while (!list_empty(reaplist)) {
  384. struct nfs4_layout *lp = list_first_entry(reaplist,
  385. struct nfs4_layout, lo_perstate);
  386. list_del(&lp->lo_perstate);
  387. nfs4_put_stid(&lp->lo_state->ls_stid);
  388. kmem_cache_free(nfs4_layout_cache, lp);
  389. }
  390. }
  391. static void
  392. nfsd4_return_file_layout(struct nfs4_layout *lp, struct nfsd4_layout_seg *seg,
  393. struct list_head *reaplist)
  394. {
  395. struct nfsd4_layout_seg *lo = &lp->lo_seg;
  396. u64 end = layout_end(lo);
  397. if (seg->offset <= lo->offset) {
  398. if (layout_end(seg) >= end) {
  399. list_move_tail(&lp->lo_perstate, reaplist);
  400. return;
  401. }
  402. lo->offset = layout_end(seg);
  403. } else {
  404. /* retain the whole layout segment on a split. */
  405. if (layout_end(seg) < end) {
  406. dprintk("%s: split not supported\n", __func__);
  407. return;
  408. }
  409. end = seg->offset;
  410. }
  411. layout_update_len(lo, end);
  412. }
  413. __be32
  414. nfsd4_return_file_layouts(struct svc_rqst *rqstp,
  415. struct nfsd4_compound_state *cstate,
  416. struct nfsd4_layoutreturn *lrp)
  417. {
  418. struct nfs4_layout_stateid *ls;
  419. struct nfs4_layout *lp, *n;
  420. LIST_HEAD(reaplist);
  421. __be32 nfserr;
  422. int found = 0;
  423. nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lrp->lr_sid,
  424. false, lrp->lr_layout_type,
  425. &ls);
  426. if (nfserr) {
  427. trace_layout_return_lookup_fail(&lrp->lr_sid);
  428. return nfserr;
  429. }
  430. spin_lock(&ls->ls_lock);
  431. list_for_each_entry_safe(lp, n, &ls->ls_layouts, lo_perstate) {
  432. if (layouts_overlapping(lp, &lrp->lr_seg)) {
  433. nfsd4_return_file_layout(lp, &lrp->lr_seg, &reaplist);
  434. found++;
  435. }
  436. }
  437. if (!list_empty(&ls->ls_layouts)) {
  438. if (found)
  439. nfs4_inc_and_copy_stateid(&lrp->lr_sid, &ls->ls_stid);
  440. lrp->lrs_present = 1;
  441. } else {
  442. trace_layoutstate_unhash(&ls->ls_stid.sc_stateid);
  443. nfs4_unhash_stid(&ls->ls_stid);
  444. lrp->lrs_present = 0;
  445. }
  446. spin_unlock(&ls->ls_lock);
  447. mutex_unlock(&ls->ls_mutex);
  448. nfs4_put_stid(&ls->ls_stid);
  449. nfsd4_free_layouts(&reaplist);
  450. return nfs_ok;
  451. }
  452. __be32
  453. nfsd4_return_client_layouts(struct svc_rqst *rqstp,
  454. struct nfsd4_compound_state *cstate,
  455. struct nfsd4_layoutreturn *lrp)
  456. {
  457. struct nfs4_layout_stateid *ls, *n;
  458. struct nfs4_client *clp = cstate->clp;
  459. struct nfs4_layout *lp, *t;
  460. LIST_HEAD(reaplist);
  461. lrp->lrs_present = 0;
  462. spin_lock(&clp->cl_lock);
  463. list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt) {
  464. if (ls->ls_layout_type != lrp->lr_layout_type)
  465. continue;
  466. if (lrp->lr_return_type == RETURN_FSID &&
  467. !fh_fsid_match(&ls->ls_stid.sc_file->fi_fhandle,
  468. &cstate->current_fh.fh_handle))
  469. continue;
  470. spin_lock(&ls->ls_lock);
  471. list_for_each_entry_safe(lp, t, &ls->ls_layouts, lo_perstate) {
  472. if (lrp->lr_seg.iomode == IOMODE_ANY ||
  473. lrp->lr_seg.iomode == lp->lo_seg.iomode)
  474. list_move_tail(&lp->lo_perstate, &reaplist);
  475. }
  476. spin_unlock(&ls->ls_lock);
  477. }
  478. spin_unlock(&clp->cl_lock);
  479. nfsd4_free_layouts(&reaplist);
  480. return 0;
  481. }
  482. static void
  483. nfsd4_return_all_layouts(struct nfs4_layout_stateid *ls,
  484. struct list_head *reaplist)
  485. {
  486. spin_lock(&ls->ls_lock);
  487. list_splice_init(&ls->ls_layouts, reaplist);
  488. spin_unlock(&ls->ls_lock);
  489. }
  490. void
  491. nfsd4_return_all_client_layouts(struct nfs4_client *clp)
  492. {
  493. struct nfs4_layout_stateid *ls, *n;
  494. LIST_HEAD(reaplist);
  495. spin_lock(&clp->cl_lock);
  496. list_for_each_entry_safe(ls, n, &clp->cl_lo_states, ls_perclnt)
  497. nfsd4_return_all_layouts(ls, &reaplist);
  498. spin_unlock(&clp->cl_lock);
  499. nfsd4_free_layouts(&reaplist);
  500. }
  501. void
  502. nfsd4_return_all_file_layouts(struct nfs4_client *clp, struct nfs4_file *fp)
  503. {
  504. struct nfs4_layout_stateid *ls, *n;
  505. LIST_HEAD(reaplist);
  506. spin_lock(&fp->fi_lock);
  507. list_for_each_entry_safe(ls, n, &fp->fi_lo_states, ls_perfile) {
  508. if (ls->ls_stid.sc_client == clp)
  509. nfsd4_return_all_layouts(ls, &reaplist);
  510. }
  511. spin_unlock(&fp->fi_lock);
  512. nfsd4_free_layouts(&reaplist);
  513. }
  514. static void
  515. nfsd4_cb_layout_fail(struct nfs4_layout_stateid *ls)
  516. {
  517. struct nfs4_client *clp = ls->ls_stid.sc_client;
  518. char addr_str[INET6_ADDRSTRLEN];
  519. static char *envp[] = {
  520. "HOME=/",
  521. "TERM=linux",
  522. "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
  523. NULL
  524. };
  525. char *argv[8];
  526. int error;
  527. rpc_ntop((struct sockaddr *)&clp->cl_addr, addr_str, sizeof(addr_str));
  528. printk(KERN_WARNING
  529. "nfsd: client %s failed to respond to layout recall. "
  530. " Fencing..\n", addr_str);
  531. argv[0] = "/sbin/nfsd-recall-failed";
  532. argv[1] = addr_str;
  533. argv[2] = ls->ls_file->f_path.mnt->mnt_sb->s_id;
  534. argv[3] = NULL;
  535. error = call_usermodehelper(argv[0], argv, envp, 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. LIST_HEAD(reaplist);
  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. 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. case -NFS4ERR_NOMATCHING_LAYOUT:
  581. trace_layout_recall_done(&ls->ls_stid.sc_stateid);
  582. task->tk_status = 0;
  583. return 1;
  584. default:
  585. /*
  586. * Unknown error or non-responding client, we'll need to fence.
  587. */
  588. trace_layout_recall_fail(&ls->ls_stid.sc_stateid);
  589. ops = nfsd4_layout_ops[ls->ls_layout_type];
  590. if (ops->fence_client)
  591. ops->fence_client(ls);
  592. else
  593. nfsd4_cb_layout_fail(ls);
  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_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. }