pnfs.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /*
  2. * pNFS functions to call and manage layout drivers.
  3. *
  4. * Copyright (c) 2002 [year of first publication]
  5. * The Regents of the University of Michigan
  6. * All Rights Reserved
  7. *
  8. * Dean Hildebrand <dhildebz@umich.edu>
  9. *
  10. * Permission is granted to use, copy, create derivative works, and
  11. * redistribute this software and such derivative works for any purpose,
  12. * so long as the name of the University of Michigan is not used in
  13. * any advertising or publicity pertaining to the use or distribution
  14. * of this software without specific, written prior authorization. If
  15. * the above copyright notice or any other identification of the
  16. * University of Michigan is included in any copy of any portion of
  17. * this software, then the disclaimer below must also be included.
  18. *
  19. * This software is provided as is, without representation or warranty
  20. * of any kind either express or implied, including without limitation
  21. * the implied warranties of merchantability, fitness for a particular
  22. * purpose, or noninfringement. The Regents of the University of
  23. * Michigan shall not be liable for any damages, including special,
  24. * indirect, incidental, or consequential damages, with respect to any
  25. * claim arising out of or in connection with the use of the software,
  26. * even if it has been or is hereafter advised of the possibility of
  27. * such damages.
  28. */
  29. #include <linux/nfs_fs.h>
  30. #include "internal.h"
  31. #include "pnfs.h"
  32. #define NFSDBG_FACILITY NFSDBG_PNFS
  33. /* Locking:
  34. *
  35. * pnfs_spinlock:
  36. * protects pnfs_modules_tbl.
  37. */
  38. static DEFINE_SPINLOCK(pnfs_spinlock);
  39. /*
  40. * pnfs_modules_tbl holds all pnfs modules
  41. */
  42. static LIST_HEAD(pnfs_modules_tbl);
  43. /* Return the registered pnfs layout driver module matching given id */
  44. static struct pnfs_layoutdriver_type *
  45. find_pnfs_driver_locked(u32 id)
  46. {
  47. struct pnfs_layoutdriver_type *local;
  48. list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
  49. if (local->id == id)
  50. goto out;
  51. local = NULL;
  52. out:
  53. dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
  54. return local;
  55. }
  56. static struct pnfs_layoutdriver_type *
  57. find_pnfs_driver(u32 id)
  58. {
  59. struct pnfs_layoutdriver_type *local;
  60. spin_lock(&pnfs_spinlock);
  61. local = find_pnfs_driver_locked(id);
  62. spin_unlock(&pnfs_spinlock);
  63. return local;
  64. }
  65. void
  66. unset_pnfs_layoutdriver(struct nfs_server *nfss)
  67. {
  68. if (nfss->pnfs_curr_ld) {
  69. nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
  70. module_put(nfss->pnfs_curr_ld->owner);
  71. }
  72. nfss->pnfs_curr_ld = NULL;
  73. }
  74. /*
  75. * Try to set the server's pnfs module to the pnfs layout type specified by id.
  76. * Currently only one pNFS layout driver per filesystem is supported.
  77. *
  78. * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
  79. */
  80. void
  81. set_pnfs_layoutdriver(struct nfs_server *server, u32 id)
  82. {
  83. struct pnfs_layoutdriver_type *ld_type = NULL;
  84. if (id == 0)
  85. goto out_no_driver;
  86. if (!(server->nfs_client->cl_exchange_flags &
  87. (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
  88. printk(KERN_ERR "%s: id %u cl_exchange_flags 0x%x\n", __func__,
  89. id, server->nfs_client->cl_exchange_flags);
  90. goto out_no_driver;
  91. }
  92. ld_type = find_pnfs_driver(id);
  93. if (!ld_type) {
  94. request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
  95. ld_type = find_pnfs_driver(id);
  96. if (!ld_type) {
  97. dprintk("%s: No pNFS module found for %u.\n",
  98. __func__, id);
  99. goto out_no_driver;
  100. }
  101. }
  102. if (!try_module_get(ld_type->owner)) {
  103. dprintk("%s: Could not grab reference on module\n", __func__);
  104. goto out_no_driver;
  105. }
  106. server->pnfs_curr_ld = ld_type;
  107. if (ld_type->set_layoutdriver(server)) {
  108. printk(KERN_ERR
  109. "%s: Error initializing mount point for layout driver %u.\n",
  110. __func__, id);
  111. module_put(ld_type->owner);
  112. goto out_no_driver;
  113. }
  114. dprintk("%s: pNFS module for %u set\n", __func__, id);
  115. return;
  116. out_no_driver:
  117. dprintk("%s: Using NFSv4 I/O\n", __func__);
  118. server->pnfs_curr_ld = NULL;
  119. }
  120. int
  121. pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  122. {
  123. int status = -EINVAL;
  124. struct pnfs_layoutdriver_type *tmp;
  125. if (ld_type->id == 0) {
  126. printk(KERN_ERR "%s id 0 is reserved\n", __func__);
  127. return status;
  128. }
  129. if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
  130. printk(KERN_ERR "%s Layout driver must provide "
  131. "alloc_lseg and free_lseg.\n", __func__);
  132. return status;
  133. }
  134. spin_lock(&pnfs_spinlock);
  135. tmp = find_pnfs_driver_locked(ld_type->id);
  136. if (!tmp) {
  137. list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
  138. status = 0;
  139. dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
  140. ld_type->name);
  141. } else {
  142. printk(KERN_ERR "%s Module with id %d already loaded!\n",
  143. __func__, ld_type->id);
  144. }
  145. spin_unlock(&pnfs_spinlock);
  146. return status;
  147. }
  148. EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
  149. void
  150. pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
  151. {
  152. dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
  153. spin_lock(&pnfs_spinlock);
  154. list_del(&ld_type->pnfs_tblid);
  155. spin_unlock(&pnfs_spinlock);
  156. }
  157. EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
  158. /*
  159. * pNFS client layout cache
  160. */
  161. /* Need to hold i_lock if caller does not already hold reference */
  162. void
  163. get_layout_hdr(struct pnfs_layout_hdr *lo)
  164. {
  165. atomic_inc(&lo->plh_refcount);
  166. }
  167. static void
  168. destroy_layout_hdr(struct pnfs_layout_hdr *lo)
  169. {
  170. dprintk("%s: freeing layout cache %p\n", __func__, lo);
  171. BUG_ON(!list_empty(&lo->plh_layouts));
  172. NFS_I(lo->plh_inode)->layout = NULL;
  173. kfree(lo);
  174. }
  175. static void
  176. put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
  177. {
  178. if (atomic_dec_and_test(&lo->plh_refcount))
  179. destroy_layout_hdr(lo);
  180. }
  181. void
  182. put_layout_hdr(struct pnfs_layout_hdr *lo)
  183. {
  184. struct inode *inode = lo->plh_inode;
  185. if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
  186. destroy_layout_hdr(lo);
  187. spin_unlock(&inode->i_lock);
  188. }
  189. }
  190. static void
  191. init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
  192. {
  193. INIT_LIST_HEAD(&lseg->pls_list);
  194. atomic_set(&lseg->pls_refcount, 1);
  195. smp_mb();
  196. set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
  197. lseg->pls_layout = lo;
  198. }
  199. static void free_lseg(struct pnfs_layout_segment *lseg)
  200. {
  201. struct inode *ino = lseg->pls_layout->plh_inode;
  202. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  203. /* Matched by get_layout_hdr in pnfs_insert_layout */
  204. put_layout_hdr(NFS_I(ino)->layout);
  205. }
  206. /* The use of tmp_list is necessary because pnfs_curr_ld->free_lseg
  207. * could sleep, so must be called outside of the lock.
  208. * Returns 1 if object was removed, otherwise return 0.
  209. */
  210. static int
  211. put_lseg_locked(struct pnfs_layout_segment *lseg,
  212. struct list_head *tmp_list)
  213. {
  214. dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
  215. atomic_read(&lseg->pls_refcount),
  216. test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  217. if (atomic_dec_and_test(&lseg->pls_refcount)) {
  218. struct inode *ino = lseg->pls_layout->plh_inode;
  219. BUG_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
  220. list_del(&lseg->pls_list);
  221. if (list_empty(&lseg->pls_layout->plh_segs)) {
  222. set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
  223. /* Matched by initial refcount set in alloc_init_layout_hdr */
  224. put_layout_hdr_locked(lseg->pls_layout);
  225. }
  226. rpc_wake_up(&NFS_SERVER(ino)->roc_rpcwaitq);
  227. list_add(&lseg->pls_list, tmp_list);
  228. return 1;
  229. }
  230. return 0;
  231. }
  232. static bool
  233. should_free_lseg(u32 lseg_iomode, u32 recall_iomode)
  234. {
  235. return (recall_iomode == IOMODE_ANY ||
  236. lseg_iomode == recall_iomode);
  237. }
  238. /* Returns 1 if lseg is removed from list, 0 otherwise */
  239. static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
  240. struct list_head *tmp_list)
  241. {
  242. int rv = 0;
  243. if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
  244. /* Remove the reference keeping the lseg in the
  245. * list. It will now be removed when all
  246. * outstanding io is finished.
  247. */
  248. rv = put_lseg_locked(lseg, tmp_list);
  249. }
  250. return rv;
  251. }
  252. /* Returns count of number of matching invalid lsegs remaining in list
  253. * after call.
  254. */
  255. int
  256. mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
  257. struct list_head *tmp_list,
  258. u32 iomode)
  259. {
  260. struct pnfs_layout_segment *lseg, *next;
  261. int invalid = 0, removed = 0;
  262. dprintk("%s:Begin lo %p\n", __func__, lo);
  263. if (list_empty(&lo->plh_segs)) {
  264. if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
  265. put_layout_hdr_locked(lo);
  266. return 0;
  267. }
  268. list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
  269. if (should_free_lseg(lseg->pls_range.iomode, iomode)) {
  270. dprintk("%s: freeing lseg %p iomode %d "
  271. "offset %llu length %llu\n", __func__,
  272. lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
  273. lseg->pls_range.length);
  274. invalid++;
  275. removed += mark_lseg_invalid(lseg, tmp_list);
  276. }
  277. dprintk("%s:Return %i\n", __func__, invalid - removed);
  278. return invalid - removed;
  279. }
  280. /* note free_me must contain lsegs from a single layout_hdr */
  281. void
  282. pnfs_free_lseg_list(struct list_head *free_me)
  283. {
  284. struct pnfs_layout_segment *lseg, *tmp;
  285. struct pnfs_layout_hdr *lo;
  286. if (list_empty(free_me))
  287. return;
  288. lo = list_first_entry(free_me, struct pnfs_layout_segment,
  289. pls_list)->pls_layout;
  290. if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
  291. struct nfs_client *clp;
  292. clp = NFS_SERVER(lo->plh_inode)->nfs_client;
  293. spin_lock(&clp->cl_lock);
  294. list_del_init(&lo->plh_layouts);
  295. spin_unlock(&clp->cl_lock);
  296. }
  297. list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
  298. list_del(&lseg->pls_list);
  299. free_lseg(lseg);
  300. }
  301. }
  302. void
  303. pnfs_destroy_layout(struct nfs_inode *nfsi)
  304. {
  305. struct pnfs_layout_hdr *lo;
  306. LIST_HEAD(tmp_list);
  307. spin_lock(&nfsi->vfs_inode.i_lock);
  308. lo = nfsi->layout;
  309. if (lo) {
  310. lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
  311. mark_matching_lsegs_invalid(lo, &tmp_list, IOMODE_ANY);
  312. }
  313. spin_unlock(&nfsi->vfs_inode.i_lock);
  314. pnfs_free_lseg_list(&tmp_list);
  315. }
  316. /*
  317. * Called by the state manger to remove all layouts established under an
  318. * expired lease.
  319. */
  320. void
  321. pnfs_destroy_all_layouts(struct nfs_client *clp)
  322. {
  323. struct pnfs_layout_hdr *lo;
  324. LIST_HEAD(tmp_list);
  325. spin_lock(&clp->cl_lock);
  326. list_splice_init(&clp->cl_layouts, &tmp_list);
  327. spin_unlock(&clp->cl_lock);
  328. while (!list_empty(&tmp_list)) {
  329. lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
  330. plh_layouts);
  331. dprintk("%s freeing layout for inode %lu\n", __func__,
  332. lo->plh_inode->i_ino);
  333. pnfs_destroy_layout(NFS_I(lo->plh_inode));
  334. }
  335. }
  336. /* update lo->plh_stateid with new if is more recent */
  337. void
  338. pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
  339. bool update_barrier)
  340. {
  341. u32 oldseq, newseq;
  342. oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
  343. newseq = be32_to_cpu(new->stateid.seqid);
  344. if ((int)(newseq - oldseq) > 0) {
  345. memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
  346. if (update_barrier) {
  347. u32 new_barrier = be32_to_cpu(new->stateid.seqid);
  348. if ((int)(new_barrier - lo->plh_barrier))
  349. lo->plh_barrier = new_barrier;
  350. } else {
  351. /* Because of wraparound, we want to keep the barrier
  352. * "close" to the current seqids. It needs to be
  353. * within 2**31 to count as "behind", so if it
  354. * gets too near that limit, give us a litle leeway
  355. * and bring it to within 2**30.
  356. * NOTE - and yes, this is all unsigned arithmetic.
  357. */
  358. if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
  359. lo->plh_barrier = newseq - (1 << 30);
  360. }
  361. }
  362. }
  363. /* lget is set to 1 if called from inside send_layoutget call chain */
  364. static bool
  365. pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
  366. int lget)
  367. {
  368. if ((stateid) &&
  369. (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
  370. return true;
  371. return lo->plh_block_lgets ||
  372. test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
  373. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
  374. (list_empty(&lo->plh_segs) &&
  375. (atomic_read(&lo->plh_outstanding) > lget));
  376. }
  377. int
  378. pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
  379. struct nfs4_state *open_state)
  380. {
  381. int status = 0;
  382. dprintk("--> %s\n", __func__);
  383. spin_lock(&lo->plh_inode->i_lock);
  384. if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
  385. status = -EAGAIN;
  386. } else if (list_empty(&lo->plh_segs)) {
  387. int seq;
  388. do {
  389. seq = read_seqbegin(&open_state->seqlock);
  390. memcpy(dst->data, open_state->stateid.data,
  391. sizeof(open_state->stateid.data));
  392. } while (read_seqretry(&open_state->seqlock, seq));
  393. } else
  394. memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
  395. spin_unlock(&lo->plh_inode->i_lock);
  396. dprintk("<-- %s\n", __func__);
  397. return status;
  398. }
  399. /*
  400. * Get layout from server.
  401. * for now, assume that whole file layouts are requested.
  402. * arg->offset: 0
  403. * arg->length: all ones
  404. */
  405. static struct pnfs_layout_segment *
  406. send_layoutget(struct pnfs_layout_hdr *lo,
  407. struct nfs_open_context *ctx,
  408. u32 iomode)
  409. {
  410. struct inode *ino = lo->plh_inode;
  411. struct nfs_server *server = NFS_SERVER(ino);
  412. struct nfs4_layoutget *lgp;
  413. struct pnfs_layout_segment *lseg = NULL;
  414. dprintk("--> %s\n", __func__);
  415. BUG_ON(ctx == NULL);
  416. lgp = kzalloc(sizeof(*lgp), GFP_KERNEL);
  417. if (lgp == NULL)
  418. return NULL;
  419. lgp->args.minlength = NFS4_MAX_UINT64;
  420. lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
  421. lgp->args.range.iomode = iomode;
  422. lgp->args.range.offset = 0;
  423. lgp->args.range.length = NFS4_MAX_UINT64;
  424. lgp->args.type = server->pnfs_curr_ld->id;
  425. lgp->args.inode = ino;
  426. lgp->args.ctx = get_nfs_open_context(ctx);
  427. lgp->lsegpp = &lseg;
  428. /* Synchronously retrieve layout information from server and
  429. * store in lseg.
  430. */
  431. nfs4_proc_layoutget(lgp);
  432. if (!lseg) {
  433. /* remember that LAYOUTGET failed and suspend trying */
  434. set_bit(lo_fail_bit(iomode), &lo->plh_flags);
  435. }
  436. return lseg;
  437. }
  438. bool pnfs_roc(struct inode *ino)
  439. {
  440. struct pnfs_layout_hdr *lo;
  441. struct pnfs_layout_segment *lseg, *tmp;
  442. LIST_HEAD(tmp_list);
  443. bool found = false;
  444. spin_lock(&ino->i_lock);
  445. lo = NFS_I(ino)->layout;
  446. if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
  447. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
  448. goto out_nolayout;
  449. list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
  450. if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  451. mark_lseg_invalid(lseg, &tmp_list);
  452. found = true;
  453. }
  454. if (!found)
  455. goto out_nolayout;
  456. lo->plh_block_lgets++;
  457. get_layout_hdr(lo); /* matched in pnfs_roc_release */
  458. spin_unlock(&ino->i_lock);
  459. pnfs_free_lseg_list(&tmp_list);
  460. return true;
  461. out_nolayout:
  462. spin_unlock(&ino->i_lock);
  463. return false;
  464. }
  465. void pnfs_roc_release(struct inode *ino)
  466. {
  467. struct pnfs_layout_hdr *lo;
  468. spin_lock(&ino->i_lock);
  469. lo = NFS_I(ino)->layout;
  470. lo->plh_block_lgets--;
  471. put_layout_hdr_locked(lo);
  472. spin_unlock(&ino->i_lock);
  473. }
  474. void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
  475. {
  476. struct pnfs_layout_hdr *lo;
  477. spin_lock(&ino->i_lock);
  478. lo = NFS_I(ino)->layout;
  479. if ((int)(barrier - lo->plh_barrier) > 0)
  480. lo->plh_barrier = barrier;
  481. spin_unlock(&ino->i_lock);
  482. }
  483. bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
  484. {
  485. struct nfs_inode *nfsi = NFS_I(ino);
  486. struct pnfs_layout_segment *lseg;
  487. bool found = false;
  488. spin_lock(&ino->i_lock);
  489. list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
  490. if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
  491. found = true;
  492. break;
  493. }
  494. if (!found) {
  495. struct pnfs_layout_hdr *lo = nfsi->layout;
  496. u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid);
  497. /* Since close does not return a layout stateid for use as
  498. * a barrier, we choose the worst-case barrier.
  499. */
  500. *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
  501. }
  502. spin_unlock(&ino->i_lock);
  503. return found;
  504. }
  505. /*
  506. * Compare two layout segments for sorting into layout cache.
  507. * We want to preferentially return RW over RO layouts, so ensure those
  508. * are seen first.
  509. */
  510. static s64
  511. cmp_layout(u32 iomode1, u32 iomode2)
  512. {
  513. /* read > read/write */
  514. return (int)(iomode2 == IOMODE_READ) - (int)(iomode1 == IOMODE_READ);
  515. }
  516. static void
  517. pnfs_insert_layout(struct pnfs_layout_hdr *lo,
  518. struct pnfs_layout_segment *lseg)
  519. {
  520. struct pnfs_layout_segment *lp;
  521. int found = 0;
  522. dprintk("%s:Begin\n", __func__);
  523. assert_spin_locked(&lo->plh_inode->i_lock);
  524. list_for_each_entry(lp, &lo->plh_segs, pls_list) {
  525. if (cmp_layout(lp->pls_range.iomode, lseg->pls_range.iomode) > 0)
  526. continue;
  527. list_add_tail(&lseg->pls_list, &lp->pls_list);
  528. dprintk("%s: inserted lseg %p "
  529. "iomode %d offset %llu length %llu before "
  530. "lp %p iomode %d offset %llu length %llu\n",
  531. __func__, lseg, lseg->pls_range.iomode,
  532. lseg->pls_range.offset, lseg->pls_range.length,
  533. lp, lp->pls_range.iomode, lp->pls_range.offset,
  534. lp->pls_range.length);
  535. found = 1;
  536. break;
  537. }
  538. if (!found) {
  539. list_add_tail(&lseg->pls_list, &lo->plh_segs);
  540. dprintk("%s: inserted lseg %p "
  541. "iomode %d offset %llu length %llu at tail\n",
  542. __func__, lseg, lseg->pls_range.iomode,
  543. lseg->pls_range.offset, lseg->pls_range.length);
  544. }
  545. get_layout_hdr(lo);
  546. dprintk("%s:Return\n", __func__);
  547. }
  548. static struct pnfs_layout_hdr *
  549. alloc_init_layout_hdr(struct inode *ino)
  550. {
  551. struct pnfs_layout_hdr *lo;
  552. lo = kzalloc(sizeof(struct pnfs_layout_hdr), GFP_KERNEL);
  553. if (!lo)
  554. return NULL;
  555. atomic_set(&lo->plh_refcount, 1);
  556. INIT_LIST_HEAD(&lo->plh_layouts);
  557. INIT_LIST_HEAD(&lo->plh_segs);
  558. INIT_LIST_HEAD(&lo->plh_bulk_recall);
  559. lo->plh_inode = ino;
  560. return lo;
  561. }
  562. static struct pnfs_layout_hdr *
  563. pnfs_find_alloc_layout(struct inode *ino)
  564. {
  565. struct nfs_inode *nfsi = NFS_I(ino);
  566. struct pnfs_layout_hdr *new = NULL;
  567. dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
  568. assert_spin_locked(&ino->i_lock);
  569. if (nfsi->layout) {
  570. if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
  571. return NULL;
  572. else
  573. return nfsi->layout;
  574. }
  575. spin_unlock(&ino->i_lock);
  576. new = alloc_init_layout_hdr(ino);
  577. spin_lock(&ino->i_lock);
  578. if (likely(nfsi->layout == NULL)) /* Won the race? */
  579. nfsi->layout = new;
  580. else
  581. kfree(new);
  582. return nfsi->layout;
  583. }
  584. /*
  585. * iomode matching rules:
  586. * iomode lseg match
  587. * ----- ----- -----
  588. * ANY READ true
  589. * ANY RW true
  590. * RW READ false
  591. * RW RW true
  592. * READ READ true
  593. * READ RW true
  594. */
  595. static int
  596. is_matching_lseg(struct pnfs_layout_segment *lseg, u32 iomode)
  597. {
  598. return (iomode != IOMODE_RW || lseg->pls_range.iomode == IOMODE_RW);
  599. }
  600. /*
  601. * lookup range in layout
  602. */
  603. static struct pnfs_layout_segment *
  604. pnfs_find_lseg(struct pnfs_layout_hdr *lo, u32 iomode)
  605. {
  606. struct pnfs_layout_segment *lseg, *ret = NULL;
  607. dprintk("%s:Begin\n", __func__);
  608. assert_spin_locked(&lo->plh_inode->i_lock);
  609. list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
  610. if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
  611. is_matching_lseg(lseg, iomode)) {
  612. ret = lseg;
  613. break;
  614. }
  615. if (cmp_layout(iomode, lseg->pls_range.iomode) > 0)
  616. break;
  617. }
  618. dprintk("%s:Return lseg %p ref %d\n",
  619. __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
  620. return ret;
  621. }
  622. /*
  623. * Layout segment is retreived from the server if not cached.
  624. * The appropriate layout segment is referenced and returned to the caller.
  625. */
  626. struct pnfs_layout_segment *
  627. pnfs_update_layout(struct inode *ino,
  628. struct nfs_open_context *ctx,
  629. enum pnfs_iomode iomode)
  630. {
  631. struct nfs_inode *nfsi = NFS_I(ino);
  632. struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
  633. struct pnfs_layout_hdr *lo;
  634. struct pnfs_layout_segment *lseg = NULL;
  635. bool first = false;
  636. if (!pnfs_enabled_sb(NFS_SERVER(ino)))
  637. return NULL;
  638. spin_lock(&ino->i_lock);
  639. lo = pnfs_find_alloc_layout(ino);
  640. if (lo == NULL) {
  641. dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
  642. goto out_unlock;
  643. }
  644. /* Do we even need to bother with this? */
  645. if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
  646. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  647. dprintk("%s matches recall, use MDS\n", __func__);
  648. goto out_unlock;
  649. }
  650. /* Check to see if the layout for the given range already exists */
  651. lseg = pnfs_find_lseg(lo, iomode);
  652. if (lseg)
  653. goto out_unlock;
  654. /* if LAYOUTGET already failed once we don't try again */
  655. if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
  656. goto out_unlock;
  657. if (pnfs_layoutgets_blocked(lo, NULL, 0))
  658. goto out_unlock;
  659. atomic_inc(&lo->plh_outstanding);
  660. get_layout_hdr(lo);
  661. if (list_empty(&lo->plh_segs))
  662. first = true;
  663. spin_unlock(&ino->i_lock);
  664. if (first) {
  665. /* The lo must be on the clp list if there is any
  666. * chance of a CB_LAYOUTRECALL(FILE) coming in.
  667. */
  668. spin_lock(&clp->cl_lock);
  669. BUG_ON(!list_empty(&lo->plh_layouts));
  670. list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
  671. spin_unlock(&clp->cl_lock);
  672. }
  673. lseg = send_layoutget(lo, ctx, iomode);
  674. if (!lseg && first) {
  675. spin_lock(&clp->cl_lock);
  676. list_del_init(&lo->plh_layouts);
  677. spin_unlock(&clp->cl_lock);
  678. }
  679. atomic_dec(&lo->plh_outstanding);
  680. put_layout_hdr(lo);
  681. out:
  682. dprintk("%s end, state 0x%lx lseg %p\n", __func__,
  683. nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
  684. return lseg;
  685. out_unlock:
  686. spin_unlock(&ino->i_lock);
  687. goto out;
  688. }
  689. int
  690. pnfs_layout_process(struct nfs4_layoutget *lgp)
  691. {
  692. struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
  693. struct nfs4_layoutget_res *res = &lgp->res;
  694. struct pnfs_layout_segment *lseg;
  695. struct inode *ino = lo->plh_inode;
  696. struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
  697. int status = 0;
  698. /* Verify we got what we asked for.
  699. * Note that because the xdr parsing only accepts a single
  700. * element array, this can fail even if the server is behaving
  701. * correctly.
  702. */
  703. if (lgp->args.range.iomode > res->range.iomode ||
  704. res->range.offset != 0 ||
  705. res->range.length != NFS4_MAX_UINT64) {
  706. status = -EINVAL;
  707. goto out;
  708. }
  709. /* Inject layout blob into I/O device driver */
  710. lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res);
  711. if (!lseg || IS_ERR(lseg)) {
  712. if (!lseg)
  713. status = -ENOMEM;
  714. else
  715. status = PTR_ERR(lseg);
  716. dprintk("%s: Could not allocate layout: error %d\n",
  717. __func__, status);
  718. goto out;
  719. }
  720. spin_lock(&ino->i_lock);
  721. if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
  722. test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
  723. dprintk("%s forget reply due to recall\n", __func__);
  724. goto out_forget_reply;
  725. }
  726. if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
  727. dprintk("%s forget reply due to state\n", __func__);
  728. goto out_forget_reply;
  729. }
  730. init_lseg(lo, lseg);
  731. lseg->pls_range = res->range;
  732. *lgp->lsegpp = lseg;
  733. pnfs_insert_layout(lo, lseg);
  734. if (res->return_on_close) {
  735. set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
  736. set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
  737. }
  738. /* Done processing layoutget. Set the layout stateid */
  739. pnfs_set_layout_stateid(lo, &res->stateid, false);
  740. spin_unlock(&ino->i_lock);
  741. out:
  742. return status;
  743. out_forget_reply:
  744. spin_unlock(&ino->i_lock);
  745. lseg->pls_layout = lo;
  746. NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
  747. goto out;
  748. }
  749. /*
  750. * Device ID cache. Currently supports one layout type per struct nfs_client.
  751. * Add layout type to the lookup key to expand to support multiple types.
  752. */
  753. int
  754. pnfs_alloc_init_deviceid_cache(struct nfs_client *clp,
  755. void (*free_callback)(struct pnfs_deviceid_node *))
  756. {
  757. struct pnfs_deviceid_cache *c;
  758. c = kzalloc(sizeof(struct pnfs_deviceid_cache), GFP_KERNEL);
  759. if (!c)
  760. return -ENOMEM;
  761. spin_lock(&clp->cl_lock);
  762. if (clp->cl_devid_cache != NULL) {
  763. atomic_inc(&clp->cl_devid_cache->dc_ref);
  764. dprintk("%s [kref [%d]]\n", __func__,
  765. atomic_read(&clp->cl_devid_cache->dc_ref));
  766. kfree(c);
  767. } else {
  768. /* kzalloc initializes hlists */
  769. spin_lock_init(&c->dc_lock);
  770. atomic_set(&c->dc_ref, 1);
  771. c->dc_free_callback = free_callback;
  772. clp->cl_devid_cache = c;
  773. dprintk("%s [new]\n", __func__);
  774. }
  775. spin_unlock(&clp->cl_lock);
  776. return 0;
  777. }
  778. EXPORT_SYMBOL_GPL(pnfs_alloc_init_deviceid_cache);
  779. /*
  780. * Called from pnfs_layoutdriver_type->free_lseg
  781. * last layout segment reference frees deviceid
  782. */
  783. void
  784. pnfs_put_deviceid(struct pnfs_deviceid_cache *c,
  785. struct pnfs_deviceid_node *devid)
  786. {
  787. struct nfs4_deviceid *id = &devid->de_id;
  788. struct pnfs_deviceid_node *d;
  789. struct hlist_node *n;
  790. long h = nfs4_deviceid_hash(id);
  791. dprintk("%s [%d]\n", __func__, atomic_read(&devid->de_ref));
  792. if (!atomic_dec_and_lock(&devid->de_ref, &c->dc_lock))
  793. return;
  794. hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[h], de_node)
  795. if (!memcmp(&d->de_id, id, sizeof(*id))) {
  796. hlist_del_rcu(&d->de_node);
  797. spin_unlock(&c->dc_lock);
  798. synchronize_rcu();
  799. c->dc_free_callback(devid);
  800. return;
  801. }
  802. spin_unlock(&c->dc_lock);
  803. /* Why wasn't it found in the list? */
  804. BUG();
  805. }
  806. EXPORT_SYMBOL_GPL(pnfs_put_deviceid);
  807. /* Find and reference a deviceid */
  808. struct pnfs_deviceid_node *
  809. pnfs_find_get_deviceid(struct pnfs_deviceid_cache *c, struct nfs4_deviceid *id)
  810. {
  811. struct pnfs_deviceid_node *d;
  812. struct hlist_node *n;
  813. long hash = nfs4_deviceid_hash(id);
  814. dprintk("--> %s hash %ld\n", __func__, hash);
  815. rcu_read_lock();
  816. hlist_for_each_entry_rcu(d, n, &c->dc_deviceids[hash], de_node) {
  817. if (!memcmp(&d->de_id, id, sizeof(*id))) {
  818. if (!atomic_inc_not_zero(&d->de_ref)) {
  819. goto fail;
  820. } else {
  821. rcu_read_unlock();
  822. return d;
  823. }
  824. }
  825. }
  826. fail:
  827. rcu_read_unlock();
  828. return NULL;
  829. }
  830. EXPORT_SYMBOL_GPL(pnfs_find_get_deviceid);
  831. /*
  832. * Add a deviceid to the cache.
  833. * GETDEVICEINFOs for same deviceid can race. If deviceid is found, discard new
  834. */
  835. struct pnfs_deviceid_node *
  836. pnfs_add_deviceid(struct pnfs_deviceid_cache *c, struct pnfs_deviceid_node *new)
  837. {
  838. struct pnfs_deviceid_node *d;
  839. long hash = nfs4_deviceid_hash(&new->de_id);
  840. dprintk("--> %s hash %ld\n", __func__, hash);
  841. spin_lock(&c->dc_lock);
  842. d = pnfs_find_get_deviceid(c, &new->de_id);
  843. if (d) {
  844. spin_unlock(&c->dc_lock);
  845. dprintk("%s [discard]\n", __func__);
  846. c->dc_free_callback(new);
  847. return d;
  848. }
  849. INIT_HLIST_NODE(&new->de_node);
  850. atomic_set(&new->de_ref, 1);
  851. hlist_add_head_rcu(&new->de_node, &c->dc_deviceids[hash]);
  852. spin_unlock(&c->dc_lock);
  853. dprintk("%s [new]\n", __func__);
  854. return new;
  855. }
  856. EXPORT_SYMBOL_GPL(pnfs_add_deviceid);
  857. void
  858. pnfs_put_deviceid_cache(struct nfs_client *clp)
  859. {
  860. struct pnfs_deviceid_cache *local = clp->cl_devid_cache;
  861. dprintk("--> %s ({%d})\n", __func__, atomic_read(&local->dc_ref));
  862. if (atomic_dec_and_lock(&local->dc_ref, &clp->cl_lock)) {
  863. int i;
  864. /* Verify cache is empty */
  865. for (i = 0; i < NFS4_DEVICE_ID_HASH_SIZE; i++)
  866. BUG_ON(!hlist_empty(&local->dc_deviceids[i]));
  867. clp->cl_devid_cache = NULL;
  868. spin_unlock(&clp->cl_lock);
  869. kfree(local);
  870. }
  871. }
  872. EXPORT_SYMBOL_GPL(pnfs_put_deviceid_cache);