nfs4layouts.c 17 KB

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