nfs4layouts.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. nfsd4_free_layout_stateid);
  158. if (!stp)
  159. return NULL;
  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. mutex_init(&ls->ls_mutex);
  168. ls->ls_layout_type = layout_type;
  169. nfsd4_init_cb(&ls->ls_recall, clp, &nfsd4_cb_layout_ops,
  170. NFSPROC4_CLNT_CB_LAYOUT);
  171. if (parent->sc_type == NFS4_DELEG_STID)
  172. ls->ls_file = get_file(fp->fi_deleg_file);
  173. else
  174. ls->ls_file = find_any_file(fp);
  175. BUG_ON(!ls->ls_file);
  176. if (nfsd4_layout_setlease(ls)) {
  177. fput(ls->ls_file);
  178. put_nfs4_file(fp);
  179. kmem_cache_free(nfs4_layout_stateid_cache, ls);
  180. return NULL;
  181. }
  182. spin_lock(&clp->cl_lock);
  183. stp->sc_type = NFS4_LAYOUT_STID;
  184. list_add(&ls->ls_perclnt, &clp->cl_lo_states);
  185. spin_unlock(&clp->cl_lock);
  186. spin_lock(&fp->fi_lock);
  187. list_add(&ls->ls_perfile, &fp->fi_lo_states);
  188. spin_unlock(&fp->fi_lock);
  189. trace_layoutstate_alloc(&ls->ls_stid.sc_stateid);
  190. return ls;
  191. }
  192. __be32
  193. nfsd4_preprocess_layout_stateid(struct svc_rqst *rqstp,
  194. struct nfsd4_compound_state *cstate, stateid_t *stateid,
  195. bool create, u32 layout_type, struct nfs4_layout_stateid **lsp)
  196. {
  197. struct nfs4_layout_stateid *ls;
  198. struct nfs4_stid *stid;
  199. unsigned char typemask = NFS4_LAYOUT_STID;
  200. __be32 status;
  201. if (create)
  202. typemask |= (NFS4_OPEN_STID | NFS4_LOCK_STID | NFS4_DELEG_STID);
  203. status = nfsd4_lookup_stateid(cstate, stateid, typemask, &stid,
  204. net_generic(SVC_NET(rqstp), nfsd_net_id));
  205. if (status)
  206. goto out;
  207. if (!fh_match(&cstate->current_fh.fh_handle,
  208. &stid->sc_file->fi_fhandle)) {
  209. status = nfserr_bad_stateid;
  210. goto out_put_stid;
  211. }
  212. if (stid->sc_type != NFS4_LAYOUT_STID) {
  213. ls = nfsd4_alloc_layout_stateid(cstate, stid, layout_type);
  214. nfs4_put_stid(stid);
  215. status = nfserr_jukebox;
  216. if (!ls)
  217. goto out;
  218. mutex_lock(&ls->ls_mutex);
  219. } else {
  220. ls = container_of(stid, struct nfs4_layout_stateid, ls_stid);
  221. status = nfserr_bad_stateid;
  222. mutex_lock(&ls->ls_mutex);
  223. if (stateid->si_generation > stid->sc_stateid.si_generation)
  224. goto out_unlock_stid;
  225. if (layout_type != ls->ls_layout_type)
  226. goto out_unlock_stid;
  227. }
  228. *lsp = ls;
  229. return 0;
  230. out_unlock_stid:
  231. mutex_unlock(&ls->ls_mutex);
  232. out_put_stid:
  233. nfs4_put_stid(stid);
  234. out:
  235. return status;
  236. }
  237. static void
  238. nfsd4_recall_file_layout(struct nfs4_layout_stateid *ls)
  239. {
  240. spin_lock(&ls->ls_lock);
  241. if (ls->ls_recalled)
  242. goto out_unlock;
  243. ls->ls_recalled = true;
  244. atomic_inc(&ls->ls_stid.sc_file->fi_lo_recalls);
  245. if (list_empty(&ls->ls_layouts))
  246. goto out_unlock;
  247. trace_layout_recall(&ls->ls_stid.sc_stateid);
  248. atomic_inc(&ls->ls_stid.sc_count);
  249. nfsd4_run_cb(&ls->ls_recall);
  250. out_unlock:
  251. spin_unlock(&ls->ls_lock);
  252. }
  253. static inline u64
  254. layout_end(struct nfsd4_layout_seg *seg)
  255. {
  256. u64 end = seg->offset + seg->length;
  257. return end >= seg->offset ? end : NFS4_MAX_UINT64;
  258. }
  259. static void
  260. layout_update_len(struct nfsd4_layout_seg *lo, u64 end)
  261. {
  262. if (end == NFS4_MAX_UINT64)
  263. lo->length = NFS4_MAX_UINT64;
  264. else
  265. lo->length = end - lo->offset;
  266. }
  267. static bool
  268. layouts_overlapping(struct nfs4_layout *lo, struct nfsd4_layout_seg *s)
  269. {
  270. if (s->iomode != IOMODE_ANY && s->iomode != lo->lo_seg.iomode)
  271. return false;
  272. if (layout_end(&lo->lo_seg) <= s->offset)
  273. return false;
  274. if (layout_end(s) <= lo->lo_seg.offset)
  275. return false;
  276. return true;
  277. }
  278. static bool
  279. layouts_try_merge(struct nfsd4_layout_seg *lo, struct nfsd4_layout_seg *new)
  280. {
  281. if (lo->iomode != new->iomode)
  282. return false;
  283. if (layout_end(new) < lo->offset)
  284. return false;
  285. if (layout_end(lo) < new->offset)
  286. return false;
  287. lo->offset = min(lo->offset, new->offset);
  288. layout_update_len(lo, max(layout_end(lo), layout_end(new)));
  289. return true;
  290. }
  291. static __be32
  292. nfsd4_recall_conflict(struct nfs4_layout_stateid *ls)
  293. {
  294. struct nfs4_file *fp = ls->ls_stid.sc_file;
  295. struct nfs4_layout_stateid *l, *n;
  296. __be32 nfserr = nfs_ok;
  297. assert_spin_locked(&fp->fi_lock);
  298. list_for_each_entry_safe(l, n, &fp->fi_lo_states, ls_perfile) {
  299. if (l != ls) {
  300. nfsd4_recall_file_layout(l);
  301. nfserr = nfserr_recallconflict;
  302. }
  303. }
  304. return nfserr;
  305. }
  306. __be32
  307. nfsd4_insert_layout(struct nfsd4_layoutget *lgp, struct nfs4_layout_stateid *ls)
  308. {
  309. struct nfsd4_layout_seg *seg = &lgp->lg_seg;
  310. struct nfs4_file *fp = ls->ls_stid.sc_file;
  311. struct nfs4_layout *lp, *new = NULL;
  312. __be32 nfserr;
  313. spin_lock(&fp->fi_lock);
  314. nfserr = nfsd4_recall_conflict(ls);
  315. if (nfserr)
  316. goto out;
  317. spin_lock(&ls->ls_lock);
  318. list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
  319. if (layouts_try_merge(&lp->lo_seg, seg))
  320. goto done;
  321. }
  322. spin_unlock(&ls->ls_lock);
  323. spin_unlock(&fp->fi_lock);
  324. new = kmem_cache_alloc(nfs4_layout_cache, GFP_KERNEL);
  325. if (!new)
  326. return nfserr_jukebox;
  327. memcpy(&new->lo_seg, seg, sizeof(lp->lo_seg));
  328. new->lo_state = ls;
  329. spin_lock(&fp->fi_lock);
  330. nfserr = nfsd4_recall_conflict(ls);
  331. if (nfserr)
  332. goto out;
  333. spin_lock(&ls->ls_lock);
  334. list_for_each_entry(lp, &ls->ls_layouts, lo_perstate) {
  335. if (layouts_try_merge(&lp->lo_seg, seg))
  336. goto done;
  337. }
  338. atomic_inc(&ls->ls_stid.sc_count);
  339. list_add_tail(&new->lo_perstate, &ls->ls_layouts);
  340. new = NULL;
  341. done:
  342. nfs4_inc_and_copy_stateid(&lgp->lg_sid, &ls->ls_stid);
  343. spin_unlock(&ls->ls_lock);
  344. out:
  345. spin_unlock(&fp->fi_lock);
  346. if (new)
  347. kmem_cache_free(nfs4_layout_cache, new);
  348. return nfserr;
  349. }
  350. static void
  351. nfsd4_free_layouts(struct list_head *reaplist)
  352. {
  353. while (!list_empty(reaplist)) {
  354. struct nfs4_layout *lp = list_first_entry(reaplist,
  355. struct nfs4_layout, lo_perstate);
  356. list_del(&lp->lo_perstate);
  357. nfs4_put_stid(&lp->lo_state->ls_stid);
  358. kmem_cache_free(nfs4_layout_cache, lp);
  359. }
  360. }
  361. static void
  362. nfsd4_return_file_layout(struct nfs4_layout *lp, struct nfsd4_layout_seg *seg,
  363. struct list_head *reaplist)
  364. {
  365. struct nfsd4_layout_seg *lo = &lp->lo_seg;
  366. u64 end = layout_end(lo);
  367. if (seg->offset <= lo->offset) {
  368. if (layout_end(seg) >= end) {
  369. list_move_tail(&lp->lo_perstate, reaplist);
  370. return;
  371. }
  372. lo->offset = layout_end(seg);
  373. } else {
  374. /* retain the whole layout segment on a split. */
  375. if (layout_end(seg) < end) {
  376. dprintk("%s: split not supported\n", __func__);
  377. return;
  378. }
  379. end = seg->offset;
  380. }
  381. layout_update_len(lo, end);
  382. }
  383. __be32
  384. nfsd4_return_file_layouts(struct svc_rqst *rqstp,
  385. struct nfsd4_compound_state *cstate,
  386. struct nfsd4_layoutreturn *lrp)
  387. {
  388. struct nfs4_layout_stateid *ls;
  389. struct nfs4_layout *lp, *n;
  390. LIST_HEAD(reaplist);
  391. __be32 nfserr;
  392. int found = 0;
  393. nfserr = nfsd4_preprocess_layout_stateid(rqstp, cstate, &lrp->lr_sid,
  394. false, lrp->lr_layout_type,
  395. &ls);
  396. if (nfserr) {
  397. trace_layout_return_lookup_fail(&lrp->lr_sid);
  398. return nfserr;
  399. }
  400. spin_lock(&ls->ls_lock);
  401. list_for_each_entry_safe(lp, n, &ls->ls_layouts, lo_perstate) {
  402. if (layouts_overlapping(lp, &lrp->lr_seg)) {
  403. nfsd4_return_file_layout(lp, &lrp->lr_seg, &reaplist);
  404. found++;
  405. }
  406. }
  407. if (!list_empty(&ls->ls_layouts)) {
  408. if (found)
  409. nfs4_inc_and_copy_stateid(&lrp->lr_sid, &ls->ls_stid);
  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. mutex_unlock(&ls->ls_mutex);
  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 void
  513. nfsd4_cb_layout_prepare(struct nfsd4_callback *cb)
  514. {
  515. struct nfs4_layout_stateid *ls =
  516. container_of(cb, struct nfs4_layout_stateid, ls_recall);
  517. mutex_lock(&ls->ls_mutex);
  518. nfs4_inc_and_copy_stateid(&ls->ls_recall_sid, &ls->ls_stid);
  519. mutex_unlock(&ls->ls_mutex);
  520. }
  521. static int
  522. nfsd4_cb_layout_done(struct nfsd4_callback *cb, struct rpc_task *task)
  523. {
  524. struct nfs4_layout_stateid *ls =
  525. container_of(cb, struct nfs4_layout_stateid, ls_recall);
  526. LIST_HEAD(reaplist);
  527. switch (task->tk_status) {
  528. case 0:
  529. return 1;
  530. case -NFS4ERR_NOMATCHING_LAYOUT:
  531. trace_layout_recall_done(&ls->ls_stid.sc_stateid);
  532. task->tk_status = 0;
  533. return 1;
  534. case -NFS4ERR_DELAY:
  535. /* Poll the client until it's done with the layout */
  536. /* FIXME: cap number of retries.
  537. * The pnfs standard states that we need to only expire
  538. * the client after at-least "lease time" .eg lease-time * 2
  539. * when failing to communicate a recall
  540. */
  541. rpc_delay(task, HZ/100); /* 10 mili-seconds */
  542. return 0;
  543. default:
  544. /*
  545. * Unknown error or non-responding client, we'll need to fence.
  546. */
  547. nfsd4_cb_layout_fail(ls);
  548. return -1;
  549. }
  550. }
  551. static void
  552. nfsd4_cb_layout_release(struct nfsd4_callback *cb)
  553. {
  554. struct nfs4_layout_stateid *ls =
  555. container_of(cb, struct nfs4_layout_stateid, ls_recall);
  556. LIST_HEAD(reaplist);
  557. trace_layout_recall_release(&ls->ls_stid.sc_stateid);
  558. nfsd4_return_all_layouts(ls, &reaplist);
  559. nfsd4_free_layouts(&reaplist);
  560. nfs4_put_stid(&ls->ls_stid);
  561. }
  562. static struct nfsd4_callback_ops nfsd4_cb_layout_ops = {
  563. .prepare = nfsd4_cb_layout_prepare,
  564. .done = nfsd4_cb_layout_done,
  565. .release = nfsd4_cb_layout_release,
  566. };
  567. static bool
  568. nfsd4_layout_lm_break(struct file_lock *fl)
  569. {
  570. /*
  571. * We don't want the locks code to timeout the lease for us;
  572. * we'll remove it ourself if a layout isn't returned
  573. * in time:
  574. */
  575. fl->fl_break_time = 0;
  576. nfsd4_recall_file_layout(fl->fl_owner);
  577. return false;
  578. }
  579. static int
  580. nfsd4_layout_lm_change(struct file_lock *onlist, int arg,
  581. struct list_head *dispose)
  582. {
  583. BUG_ON(!(arg & F_UNLCK));
  584. return lease_modify(onlist, arg, dispose);
  585. }
  586. static const struct lock_manager_operations nfsd4_layouts_lm_ops = {
  587. .lm_break = nfsd4_layout_lm_break,
  588. .lm_change = nfsd4_layout_lm_change,
  589. };
  590. int
  591. nfsd4_init_pnfs(void)
  592. {
  593. int i;
  594. for (i = 0; i < DEVID_HASH_SIZE; i++)
  595. INIT_LIST_HEAD(&nfsd_devid_hash[i]);
  596. nfs4_layout_cache = kmem_cache_create("nfs4_layout",
  597. sizeof(struct nfs4_layout), 0, 0, NULL);
  598. if (!nfs4_layout_cache)
  599. return -ENOMEM;
  600. nfs4_layout_stateid_cache = kmem_cache_create("nfs4_layout_stateid",
  601. sizeof(struct nfs4_layout_stateid), 0, 0, NULL);
  602. if (!nfs4_layout_stateid_cache) {
  603. kmem_cache_destroy(nfs4_layout_cache);
  604. return -ENOMEM;
  605. }
  606. return 0;
  607. }
  608. void
  609. nfsd4_exit_pnfs(void)
  610. {
  611. int i;
  612. kmem_cache_destroy(nfs4_layout_cache);
  613. kmem_cache_destroy(nfs4_layout_stateid_cache);
  614. for (i = 0; i < DEVID_HASH_SIZE; i++) {
  615. struct nfsd4_deviceid_map *map, *n;
  616. list_for_each_entry_safe(map, n, &nfsd_devid_hash[i], hash)
  617. kfree(map);
  618. }
  619. }