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