nfs4layouts.c 19 KB

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