pagelist.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. /*
  2. * linux/fs/nfs/pagelist.c
  3. *
  4. * A set of helper functions for managing NFS read and write requests.
  5. * The main purpose of these routines is to provide support for the
  6. * coalescing of several requests into a single RPC call.
  7. *
  8. * Copyright 2000, 2001 (c) Trond Myklebust <trond.myklebust@fys.uio.no>
  9. *
  10. */
  11. #include <linux/slab.h>
  12. #include <linux/file.h>
  13. #include <linux/sched.h>
  14. #include <linux/sunrpc/clnt.h>
  15. #include <linux/nfs.h>
  16. #include <linux/nfs3.h>
  17. #include <linux/nfs4.h>
  18. #include <linux/nfs_page.h>
  19. #include <linux/nfs_fs.h>
  20. #include <linux/nfs_mount.h>
  21. #include <linux/export.h>
  22. #include "internal.h"
  23. #include "pnfs.h"
  24. #define NFSDBG_FACILITY NFSDBG_PAGECACHE
  25. static struct kmem_cache *nfs_page_cachep;
  26. static const struct rpc_call_ops nfs_pgio_common_ops;
  27. static bool nfs_pgarray_set(struct nfs_page_array *p, unsigned int pagecount)
  28. {
  29. p->npages = pagecount;
  30. if (pagecount <= ARRAY_SIZE(p->page_array))
  31. p->pagevec = p->page_array;
  32. else {
  33. p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
  34. if (!p->pagevec)
  35. p->npages = 0;
  36. }
  37. return p->pagevec != NULL;
  38. }
  39. struct nfs_pgio_mirror *
  40. nfs_pgio_current_mirror(struct nfs_pageio_descriptor *desc)
  41. {
  42. return nfs_pgio_has_mirroring(desc) ?
  43. &desc->pg_mirrors[desc->pg_mirror_idx] :
  44. &desc->pg_mirrors[0];
  45. }
  46. EXPORT_SYMBOL_GPL(nfs_pgio_current_mirror);
  47. void nfs_pgheader_init(struct nfs_pageio_descriptor *desc,
  48. struct nfs_pgio_header *hdr,
  49. void (*release)(struct nfs_pgio_header *hdr))
  50. {
  51. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  52. hdr->req = nfs_list_entry(mirror->pg_list.next);
  53. hdr->inode = desc->pg_inode;
  54. hdr->cred = hdr->req->wb_context->cred;
  55. hdr->io_start = req_offset(hdr->req);
  56. hdr->good_bytes = mirror->pg_count;
  57. hdr->dreq = desc->pg_dreq;
  58. hdr->layout_private = desc->pg_layout_private;
  59. hdr->release = release;
  60. hdr->completion_ops = desc->pg_completion_ops;
  61. if (hdr->completion_ops->init_hdr)
  62. hdr->completion_ops->init_hdr(hdr);
  63. hdr->pgio_mirror_idx = desc->pg_mirror_idx;
  64. }
  65. EXPORT_SYMBOL_GPL(nfs_pgheader_init);
  66. void nfs_set_pgio_error(struct nfs_pgio_header *hdr, int error, loff_t pos)
  67. {
  68. spin_lock(&hdr->lock);
  69. if (pos < hdr->io_start + hdr->good_bytes) {
  70. set_bit(NFS_IOHDR_ERROR, &hdr->flags);
  71. clear_bit(NFS_IOHDR_EOF, &hdr->flags);
  72. hdr->good_bytes = pos - hdr->io_start;
  73. hdr->error = error;
  74. }
  75. spin_unlock(&hdr->lock);
  76. }
  77. static inline struct nfs_page *
  78. nfs_page_alloc(void)
  79. {
  80. struct nfs_page *p = kmem_cache_zalloc(nfs_page_cachep, GFP_NOIO);
  81. if (p)
  82. INIT_LIST_HEAD(&p->wb_list);
  83. return p;
  84. }
  85. static inline void
  86. nfs_page_free(struct nfs_page *p)
  87. {
  88. kmem_cache_free(nfs_page_cachep, p);
  89. }
  90. static void
  91. nfs_iocounter_inc(struct nfs_io_counter *c)
  92. {
  93. atomic_inc(&c->io_count);
  94. }
  95. static void
  96. nfs_iocounter_dec(struct nfs_io_counter *c)
  97. {
  98. if (atomic_dec_and_test(&c->io_count)) {
  99. clear_bit(NFS_IO_INPROGRESS, &c->flags);
  100. smp_mb__after_atomic();
  101. wake_up_bit(&c->flags, NFS_IO_INPROGRESS);
  102. }
  103. }
  104. static int
  105. __nfs_iocounter_wait(struct nfs_io_counter *c)
  106. {
  107. wait_queue_head_t *wq = bit_waitqueue(&c->flags, NFS_IO_INPROGRESS);
  108. DEFINE_WAIT_BIT(q, &c->flags, NFS_IO_INPROGRESS);
  109. int ret = 0;
  110. do {
  111. prepare_to_wait(wq, &q.wait, TASK_KILLABLE);
  112. set_bit(NFS_IO_INPROGRESS, &c->flags);
  113. if (atomic_read(&c->io_count) == 0)
  114. break;
  115. ret = nfs_wait_bit_killable(&q.key);
  116. } while (atomic_read(&c->io_count) != 0 && !ret);
  117. finish_wait(wq, &q.wait);
  118. return ret;
  119. }
  120. /**
  121. * nfs_iocounter_wait - wait for i/o to complete
  122. * @c: nfs_io_counter to use
  123. *
  124. * returns -ERESTARTSYS if interrupted by a fatal signal.
  125. * Otherwise returns 0 once the io_count hits 0.
  126. */
  127. int
  128. nfs_iocounter_wait(struct nfs_io_counter *c)
  129. {
  130. if (atomic_read(&c->io_count) == 0)
  131. return 0;
  132. return __nfs_iocounter_wait(c);
  133. }
  134. /*
  135. * nfs_page_group_lock - lock the head of the page group
  136. * @req - request in group that is to be locked
  137. * @nonblock - if true don't block waiting for lock
  138. *
  139. * this lock must be held if modifying the page group list
  140. *
  141. * return 0 on success, < 0 on error: -EDELAY if nonblocking or the
  142. * result from wait_on_bit_lock
  143. *
  144. * NOTE: calling with nonblock=false should always have set the
  145. * lock bit (see fs/buffer.c and other uses of wait_on_bit_lock
  146. * with TASK_UNINTERRUPTIBLE), so there is no need to check the result.
  147. */
  148. int
  149. nfs_page_group_lock(struct nfs_page *req, bool nonblock)
  150. {
  151. struct nfs_page *head = req->wb_head;
  152. WARN_ON_ONCE(head != head->wb_head);
  153. if (!test_and_set_bit(PG_HEADLOCK, &head->wb_flags))
  154. return 0;
  155. if (!nonblock)
  156. return wait_on_bit_lock(&head->wb_flags, PG_HEADLOCK,
  157. TASK_UNINTERRUPTIBLE);
  158. return -EAGAIN;
  159. }
  160. /*
  161. * nfs_page_group_lock_wait - wait for the lock to clear, but don't grab it
  162. * @req - a request in the group
  163. *
  164. * This is a blocking call to wait for the group lock to be cleared.
  165. */
  166. void
  167. nfs_page_group_lock_wait(struct nfs_page *req)
  168. {
  169. struct nfs_page *head = req->wb_head;
  170. WARN_ON_ONCE(head != head->wb_head);
  171. wait_on_bit(&head->wb_flags, PG_HEADLOCK,
  172. TASK_UNINTERRUPTIBLE);
  173. }
  174. /*
  175. * nfs_page_group_unlock - unlock the head of the page group
  176. * @req - request in group that is to be unlocked
  177. */
  178. void
  179. nfs_page_group_unlock(struct nfs_page *req)
  180. {
  181. struct nfs_page *head = req->wb_head;
  182. WARN_ON_ONCE(head != head->wb_head);
  183. smp_mb__before_atomic();
  184. clear_bit(PG_HEADLOCK, &head->wb_flags);
  185. smp_mb__after_atomic();
  186. wake_up_bit(&head->wb_flags, PG_HEADLOCK);
  187. }
  188. /*
  189. * nfs_page_group_sync_on_bit_locked
  190. *
  191. * must be called with page group lock held
  192. */
  193. static bool
  194. nfs_page_group_sync_on_bit_locked(struct nfs_page *req, unsigned int bit)
  195. {
  196. struct nfs_page *head = req->wb_head;
  197. struct nfs_page *tmp;
  198. WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_flags));
  199. WARN_ON_ONCE(test_and_set_bit(bit, &req->wb_flags));
  200. tmp = req->wb_this_page;
  201. while (tmp != req) {
  202. if (!test_bit(bit, &tmp->wb_flags))
  203. return false;
  204. tmp = tmp->wb_this_page;
  205. }
  206. /* true! reset all bits */
  207. tmp = req;
  208. do {
  209. clear_bit(bit, &tmp->wb_flags);
  210. tmp = tmp->wb_this_page;
  211. } while (tmp != req);
  212. return true;
  213. }
  214. /*
  215. * nfs_page_group_sync_on_bit - set bit on current request, but only
  216. * return true if the bit is set for all requests in page group
  217. * @req - request in page group
  218. * @bit - PG_* bit that is used to sync page group
  219. */
  220. bool nfs_page_group_sync_on_bit(struct nfs_page *req, unsigned int bit)
  221. {
  222. bool ret;
  223. nfs_page_group_lock(req, false);
  224. ret = nfs_page_group_sync_on_bit_locked(req, bit);
  225. nfs_page_group_unlock(req);
  226. return ret;
  227. }
  228. /*
  229. * nfs_page_group_init - Initialize the page group linkage for @req
  230. * @req - a new nfs request
  231. * @prev - the previous request in page group, or NULL if @req is the first
  232. * or only request in the group (the head).
  233. */
  234. static inline void
  235. nfs_page_group_init(struct nfs_page *req, struct nfs_page *prev)
  236. {
  237. struct inode *inode;
  238. WARN_ON_ONCE(prev == req);
  239. if (!prev) {
  240. /* a head request */
  241. req->wb_head = req;
  242. req->wb_this_page = req;
  243. } else {
  244. /* a subrequest */
  245. WARN_ON_ONCE(prev->wb_this_page != prev->wb_head);
  246. WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &prev->wb_head->wb_flags));
  247. req->wb_head = prev->wb_head;
  248. req->wb_this_page = prev->wb_this_page;
  249. prev->wb_this_page = req;
  250. /* All subrequests take a ref on the head request until
  251. * nfs_page_group_destroy is called */
  252. kref_get(&req->wb_head->wb_kref);
  253. /* grab extra ref and bump the request count if head request
  254. * has extra ref from the write/commit path to handle handoff
  255. * between write and commit lists. */
  256. if (test_bit(PG_INODE_REF, &prev->wb_head->wb_flags)) {
  257. inode = page_file_mapping(req->wb_page)->host;
  258. set_bit(PG_INODE_REF, &req->wb_flags);
  259. kref_get(&req->wb_kref);
  260. spin_lock(&inode->i_lock);
  261. NFS_I(inode)->nrequests++;
  262. spin_unlock(&inode->i_lock);
  263. }
  264. }
  265. }
  266. /*
  267. * nfs_page_group_destroy - sync the destruction of page groups
  268. * @req - request that no longer needs the page group
  269. *
  270. * releases the page group reference from each member once all
  271. * members have called this function.
  272. */
  273. static void
  274. nfs_page_group_destroy(struct kref *kref)
  275. {
  276. struct nfs_page *req = container_of(kref, struct nfs_page, wb_kref);
  277. struct nfs_page *tmp, *next;
  278. /* subrequests must release the ref on the head request */
  279. if (req->wb_head != req)
  280. nfs_release_request(req->wb_head);
  281. if (!nfs_page_group_sync_on_bit(req, PG_TEARDOWN))
  282. return;
  283. tmp = req;
  284. do {
  285. next = tmp->wb_this_page;
  286. /* unlink and free */
  287. tmp->wb_this_page = tmp;
  288. tmp->wb_head = tmp;
  289. nfs_free_request(tmp);
  290. tmp = next;
  291. } while (tmp != req);
  292. }
  293. /**
  294. * nfs_create_request - Create an NFS read/write request.
  295. * @ctx: open context to use
  296. * @page: page to write
  297. * @last: last nfs request created for this page group or NULL if head
  298. * @offset: starting offset within the page for the write
  299. * @count: number of bytes to read/write
  300. *
  301. * The page must be locked by the caller. This makes sure we never
  302. * create two different requests for the same page.
  303. * User should ensure it is safe to sleep in this function.
  304. */
  305. struct nfs_page *
  306. nfs_create_request(struct nfs_open_context *ctx, struct page *page,
  307. struct nfs_page *last, unsigned int offset,
  308. unsigned int count)
  309. {
  310. struct nfs_page *req;
  311. struct nfs_lock_context *l_ctx;
  312. if (test_bit(NFS_CONTEXT_BAD, &ctx->flags))
  313. return ERR_PTR(-EBADF);
  314. /* try to allocate the request struct */
  315. req = nfs_page_alloc();
  316. if (req == NULL)
  317. return ERR_PTR(-ENOMEM);
  318. /* get lock context early so we can deal with alloc failures */
  319. l_ctx = nfs_get_lock_context(ctx);
  320. if (IS_ERR(l_ctx)) {
  321. nfs_page_free(req);
  322. return ERR_CAST(l_ctx);
  323. }
  324. req->wb_lock_context = l_ctx;
  325. nfs_iocounter_inc(&l_ctx->io_count);
  326. /* Initialize the request struct. Initially, we assume a
  327. * long write-back delay. This will be adjusted in
  328. * update_nfs_request below if the region is not locked. */
  329. req->wb_page = page;
  330. req->wb_index = page_file_index(page);
  331. page_cache_get(page);
  332. req->wb_offset = offset;
  333. req->wb_pgbase = offset;
  334. req->wb_bytes = count;
  335. req->wb_context = get_nfs_open_context(ctx);
  336. kref_init(&req->wb_kref);
  337. nfs_page_group_init(req, last);
  338. return req;
  339. }
  340. /**
  341. * nfs_unlock_request - Unlock request and wake up sleepers.
  342. * @req:
  343. */
  344. void nfs_unlock_request(struct nfs_page *req)
  345. {
  346. if (!NFS_WBACK_BUSY(req)) {
  347. printk(KERN_ERR "NFS: Invalid unlock attempted\n");
  348. BUG();
  349. }
  350. smp_mb__before_atomic();
  351. clear_bit(PG_BUSY, &req->wb_flags);
  352. smp_mb__after_atomic();
  353. wake_up_bit(&req->wb_flags, PG_BUSY);
  354. }
  355. /**
  356. * nfs_unlock_and_release_request - Unlock request and release the nfs_page
  357. * @req:
  358. */
  359. void nfs_unlock_and_release_request(struct nfs_page *req)
  360. {
  361. nfs_unlock_request(req);
  362. nfs_release_request(req);
  363. }
  364. /*
  365. * nfs_clear_request - Free up all resources allocated to the request
  366. * @req:
  367. *
  368. * Release page and open context resources associated with a read/write
  369. * request after it has completed.
  370. */
  371. static void nfs_clear_request(struct nfs_page *req)
  372. {
  373. struct page *page = req->wb_page;
  374. struct nfs_open_context *ctx = req->wb_context;
  375. struct nfs_lock_context *l_ctx = req->wb_lock_context;
  376. if (page != NULL) {
  377. page_cache_release(page);
  378. req->wb_page = NULL;
  379. }
  380. if (l_ctx != NULL) {
  381. nfs_iocounter_dec(&l_ctx->io_count);
  382. nfs_put_lock_context(l_ctx);
  383. req->wb_lock_context = NULL;
  384. }
  385. if (ctx != NULL) {
  386. put_nfs_open_context(ctx);
  387. req->wb_context = NULL;
  388. }
  389. }
  390. /**
  391. * nfs_release_request - Release the count on an NFS read/write request
  392. * @req: request to release
  393. *
  394. * Note: Should never be called with the spinlock held!
  395. */
  396. void nfs_free_request(struct nfs_page *req)
  397. {
  398. WARN_ON_ONCE(req->wb_this_page != req);
  399. /* extra debug: make sure no sync bits are still set */
  400. WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags));
  401. WARN_ON_ONCE(test_bit(PG_UNLOCKPAGE, &req->wb_flags));
  402. WARN_ON_ONCE(test_bit(PG_UPTODATE, &req->wb_flags));
  403. WARN_ON_ONCE(test_bit(PG_WB_END, &req->wb_flags));
  404. WARN_ON_ONCE(test_bit(PG_REMOVE, &req->wb_flags));
  405. /* Release struct file and open context */
  406. nfs_clear_request(req);
  407. nfs_page_free(req);
  408. }
  409. void nfs_release_request(struct nfs_page *req)
  410. {
  411. kref_put(&req->wb_kref, nfs_page_group_destroy);
  412. }
  413. /**
  414. * nfs_wait_on_request - Wait for a request to complete.
  415. * @req: request to wait upon.
  416. *
  417. * Interruptible by fatal signals only.
  418. * The user is responsible for holding a count on the request.
  419. */
  420. int
  421. nfs_wait_on_request(struct nfs_page *req)
  422. {
  423. return wait_on_bit_io(&req->wb_flags, PG_BUSY,
  424. TASK_UNINTERRUPTIBLE);
  425. }
  426. /*
  427. * nfs_generic_pg_test - determine if requests can be coalesced
  428. * @desc: pointer to descriptor
  429. * @prev: previous request in desc, or NULL
  430. * @req: this request
  431. *
  432. * Returns zero if @req can be coalesced into @desc, otherwise it returns
  433. * the size of the request.
  434. */
  435. size_t nfs_generic_pg_test(struct nfs_pageio_descriptor *desc,
  436. struct nfs_page *prev, struct nfs_page *req)
  437. {
  438. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  439. if (mirror->pg_count > mirror->pg_bsize) {
  440. /* should never happen */
  441. WARN_ON_ONCE(1);
  442. return 0;
  443. }
  444. /*
  445. * Limit the request size so that we can still allocate a page array
  446. * for it without upsetting the slab allocator.
  447. */
  448. if (((mirror->pg_count + req->wb_bytes) >> PAGE_SHIFT) *
  449. sizeof(struct page) > PAGE_SIZE)
  450. return 0;
  451. return min(mirror->pg_bsize - mirror->pg_count, (size_t)req->wb_bytes);
  452. }
  453. EXPORT_SYMBOL_GPL(nfs_generic_pg_test);
  454. struct nfs_pgio_header *nfs_pgio_header_alloc(const struct nfs_rw_ops *ops)
  455. {
  456. struct nfs_pgio_header *hdr = ops->rw_alloc_header();
  457. if (hdr) {
  458. INIT_LIST_HEAD(&hdr->pages);
  459. spin_lock_init(&hdr->lock);
  460. hdr->rw_ops = ops;
  461. }
  462. return hdr;
  463. }
  464. EXPORT_SYMBOL_GPL(nfs_pgio_header_alloc);
  465. /*
  466. * nfs_pgio_header_free - Free a read or write header
  467. * @hdr: The header to free
  468. */
  469. void nfs_pgio_header_free(struct nfs_pgio_header *hdr)
  470. {
  471. hdr->rw_ops->rw_free_header(hdr);
  472. }
  473. EXPORT_SYMBOL_GPL(nfs_pgio_header_free);
  474. /**
  475. * nfs_pgio_data_destroy - make @hdr suitable for reuse
  476. *
  477. * Frees memory and releases refs from nfs_generic_pgio, so that it may
  478. * be called again.
  479. *
  480. * @hdr: A header that has had nfs_generic_pgio called
  481. */
  482. void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr)
  483. {
  484. if (hdr->args.context)
  485. put_nfs_open_context(hdr->args.context);
  486. if (hdr->page_array.pagevec != hdr->page_array.page_array)
  487. kfree(hdr->page_array.pagevec);
  488. }
  489. EXPORT_SYMBOL_GPL(nfs_pgio_data_destroy);
  490. /**
  491. * nfs_pgio_rpcsetup - Set up arguments for a pageio call
  492. * @hdr: The pageio hdr
  493. * @count: Number of bytes to read
  494. * @offset: Initial offset
  495. * @how: How to commit data (writes only)
  496. * @cinfo: Commit information for the call (writes only)
  497. */
  498. static void nfs_pgio_rpcsetup(struct nfs_pgio_header *hdr,
  499. unsigned int count, unsigned int offset,
  500. int how, struct nfs_commit_info *cinfo)
  501. {
  502. struct nfs_page *req = hdr->req;
  503. /* Set up the RPC argument and reply structs
  504. * NB: take care not to mess about with hdr->commit et al. */
  505. hdr->args.fh = NFS_FH(hdr->inode);
  506. hdr->args.offset = req_offset(req) + offset;
  507. /* pnfs_set_layoutcommit needs this */
  508. hdr->mds_offset = hdr->args.offset;
  509. hdr->args.pgbase = req->wb_pgbase + offset;
  510. hdr->args.pages = hdr->page_array.pagevec;
  511. hdr->args.count = count;
  512. hdr->args.context = get_nfs_open_context(req->wb_context);
  513. hdr->args.lock_context = req->wb_lock_context;
  514. hdr->args.stable = NFS_UNSTABLE;
  515. switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) {
  516. case 0:
  517. break;
  518. case FLUSH_COND_STABLE:
  519. if (nfs_reqs_to_commit(cinfo))
  520. break;
  521. default:
  522. hdr->args.stable = NFS_FILE_SYNC;
  523. }
  524. hdr->res.fattr = &hdr->fattr;
  525. hdr->res.count = count;
  526. hdr->res.eof = 0;
  527. hdr->res.verf = &hdr->verf;
  528. nfs_fattr_init(&hdr->fattr);
  529. }
  530. /**
  531. * nfs_pgio_prepare - Prepare pageio hdr to go over the wire
  532. * @task: The current task
  533. * @calldata: pageio header to prepare
  534. */
  535. static void nfs_pgio_prepare(struct rpc_task *task, void *calldata)
  536. {
  537. struct nfs_pgio_header *hdr = calldata;
  538. int err;
  539. err = NFS_PROTO(hdr->inode)->pgio_rpc_prepare(task, hdr);
  540. if (err)
  541. rpc_exit(task, err);
  542. }
  543. int nfs_initiate_pgio(struct rpc_clnt *clnt, struct nfs_pgio_header *hdr,
  544. struct rpc_cred *cred, const struct nfs_rpc_ops *rpc_ops,
  545. const struct rpc_call_ops *call_ops, int how, int flags)
  546. {
  547. struct rpc_task *task;
  548. struct rpc_message msg = {
  549. .rpc_argp = &hdr->args,
  550. .rpc_resp = &hdr->res,
  551. .rpc_cred = cred,
  552. };
  553. struct rpc_task_setup task_setup_data = {
  554. .rpc_client = clnt,
  555. .task = &hdr->task,
  556. .rpc_message = &msg,
  557. .callback_ops = call_ops,
  558. .callback_data = hdr,
  559. .workqueue = nfsiod_workqueue,
  560. .flags = RPC_TASK_ASYNC | flags,
  561. };
  562. int ret = 0;
  563. hdr->rw_ops->rw_initiate(hdr, &msg, rpc_ops, &task_setup_data, how);
  564. dprintk("NFS: %5u initiated pgio call "
  565. "(req %s/%llu, %u bytes @ offset %llu)\n",
  566. hdr->task.tk_pid,
  567. hdr->inode->i_sb->s_id,
  568. (unsigned long long)NFS_FILEID(hdr->inode),
  569. hdr->args.count,
  570. (unsigned long long)hdr->args.offset);
  571. task = rpc_run_task(&task_setup_data);
  572. if (IS_ERR(task)) {
  573. ret = PTR_ERR(task);
  574. goto out;
  575. }
  576. if (how & FLUSH_SYNC) {
  577. ret = rpc_wait_for_completion_task(task);
  578. if (ret == 0)
  579. ret = task->tk_status;
  580. }
  581. rpc_put_task(task);
  582. out:
  583. return ret;
  584. }
  585. EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
  586. /**
  587. * nfs_pgio_error - Clean up from a pageio error
  588. * @desc: IO descriptor
  589. * @hdr: pageio header
  590. */
  591. static int nfs_pgio_error(struct nfs_pageio_descriptor *desc,
  592. struct nfs_pgio_header *hdr)
  593. {
  594. struct nfs_pgio_mirror *mirror;
  595. u32 midx;
  596. set_bit(NFS_IOHDR_REDO, &hdr->flags);
  597. nfs_pgio_data_destroy(hdr);
  598. hdr->completion_ops->completion(hdr);
  599. /* TODO: Make sure it's right to clean up all mirrors here
  600. * and not just hdr->pgio_mirror_idx */
  601. for (midx = 0; midx < desc->pg_mirror_count; midx++) {
  602. mirror = &desc->pg_mirrors[midx];
  603. desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
  604. }
  605. return -ENOMEM;
  606. }
  607. /**
  608. * nfs_pgio_release - Release pageio data
  609. * @calldata: The pageio header to release
  610. */
  611. static void nfs_pgio_release(void *calldata)
  612. {
  613. struct nfs_pgio_header *hdr = calldata;
  614. if (hdr->rw_ops->rw_release)
  615. hdr->rw_ops->rw_release(hdr);
  616. nfs_pgio_data_destroy(hdr);
  617. hdr->completion_ops->completion(hdr);
  618. }
  619. static void nfs_pageio_mirror_init(struct nfs_pgio_mirror *mirror,
  620. unsigned int bsize)
  621. {
  622. INIT_LIST_HEAD(&mirror->pg_list);
  623. mirror->pg_bytes_written = 0;
  624. mirror->pg_count = 0;
  625. mirror->pg_bsize = bsize;
  626. mirror->pg_base = 0;
  627. mirror->pg_recoalesce = 0;
  628. }
  629. /**
  630. * nfs_pageio_init - initialise a page io descriptor
  631. * @desc: pointer to descriptor
  632. * @inode: pointer to inode
  633. * @doio: pointer to io function
  634. * @bsize: io block size
  635. * @io_flags: extra parameters for the io function
  636. */
  637. void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
  638. struct inode *inode,
  639. const struct nfs_pageio_ops *pg_ops,
  640. const struct nfs_pgio_completion_ops *compl_ops,
  641. const struct nfs_rw_ops *rw_ops,
  642. size_t bsize,
  643. int io_flags)
  644. {
  645. struct nfs_pgio_mirror *new;
  646. int i;
  647. desc->pg_moreio = 0;
  648. desc->pg_inode = inode;
  649. desc->pg_ops = pg_ops;
  650. desc->pg_completion_ops = compl_ops;
  651. desc->pg_rw_ops = rw_ops;
  652. desc->pg_ioflags = io_flags;
  653. desc->pg_error = 0;
  654. desc->pg_lseg = NULL;
  655. desc->pg_dreq = NULL;
  656. desc->pg_layout_private = NULL;
  657. desc->pg_bsize = bsize;
  658. desc->pg_mirror_count = 1;
  659. desc->pg_mirror_idx = 0;
  660. if (pg_ops->pg_get_mirror_count) {
  661. /* until we have a request, we don't have an lseg and no
  662. * idea how many mirrors there will be */
  663. new = kcalloc(NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX,
  664. sizeof(struct nfs_pgio_mirror), GFP_KERNEL);
  665. desc->pg_mirrors_dynamic = new;
  666. desc->pg_mirrors = new;
  667. for (i = 0; i < NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX; i++)
  668. nfs_pageio_mirror_init(&desc->pg_mirrors[i], bsize);
  669. } else {
  670. desc->pg_mirrors_dynamic = NULL;
  671. desc->pg_mirrors = desc->pg_mirrors_static;
  672. nfs_pageio_mirror_init(&desc->pg_mirrors[0], bsize);
  673. }
  674. }
  675. EXPORT_SYMBOL_GPL(nfs_pageio_init);
  676. /**
  677. * nfs_pgio_result - Basic pageio error handling
  678. * @task: The task that ran
  679. * @calldata: Pageio header to check
  680. */
  681. static void nfs_pgio_result(struct rpc_task *task, void *calldata)
  682. {
  683. struct nfs_pgio_header *hdr = calldata;
  684. struct inode *inode = hdr->inode;
  685. dprintk("NFS: %s: %5u, (status %d)\n", __func__,
  686. task->tk_pid, task->tk_status);
  687. if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
  688. return;
  689. if (task->tk_status < 0)
  690. nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
  691. else
  692. hdr->rw_ops->rw_result(task, hdr);
  693. }
  694. /*
  695. * Create an RPC task for the given read or write request and kick it.
  696. * The page must have been locked by the caller.
  697. *
  698. * It may happen that the page we're passed is not marked dirty.
  699. * This is the case if nfs_updatepage detects a conflicting request
  700. * that has been written but not committed.
  701. */
  702. int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
  703. struct nfs_pgio_header *hdr)
  704. {
  705. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  706. struct nfs_page *req;
  707. struct page **pages,
  708. *last_page;
  709. struct list_head *head = &mirror->pg_list;
  710. struct nfs_commit_info cinfo;
  711. unsigned int pagecount, pageused;
  712. pagecount = nfs_page_array_len(mirror->pg_base, mirror->pg_count);
  713. if (!nfs_pgarray_set(&hdr->page_array, pagecount))
  714. return nfs_pgio_error(desc, hdr);
  715. nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
  716. pages = hdr->page_array.pagevec;
  717. last_page = NULL;
  718. pageused = 0;
  719. while (!list_empty(head)) {
  720. req = nfs_list_entry(head->next);
  721. nfs_list_remove_request(req);
  722. nfs_list_add_request(req, &hdr->pages);
  723. if (!last_page || last_page != req->wb_page) {
  724. pageused++;
  725. if (pageused > pagecount)
  726. break;
  727. *pages++ = last_page = req->wb_page;
  728. }
  729. }
  730. if (WARN_ON_ONCE(pageused != pagecount))
  731. return nfs_pgio_error(desc, hdr);
  732. if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
  733. (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
  734. desc->pg_ioflags &= ~FLUSH_COND_STABLE;
  735. /* Set up the argument struct */
  736. nfs_pgio_rpcsetup(hdr, mirror->pg_count, 0, desc->pg_ioflags, &cinfo);
  737. desc->pg_rpc_callops = &nfs_pgio_common_ops;
  738. return 0;
  739. }
  740. EXPORT_SYMBOL_GPL(nfs_generic_pgio);
  741. static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
  742. {
  743. struct nfs_pgio_mirror *mirror;
  744. struct nfs_pgio_header *hdr;
  745. int ret;
  746. mirror = nfs_pgio_current_mirror(desc);
  747. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  748. if (!hdr) {
  749. /* TODO: make sure this is right with mirroring - or
  750. * should it back out all mirrors? */
  751. desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
  752. return -ENOMEM;
  753. }
  754. nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
  755. ret = nfs_generic_pgio(desc, hdr);
  756. if (ret == 0)
  757. ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
  758. hdr,
  759. hdr->cred,
  760. NFS_PROTO(hdr->inode),
  761. desc->pg_rpc_callops,
  762. desc->pg_ioflags, 0);
  763. return ret;
  764. }
  765. /*
  766. * nfs_pageio_setup_mirroring - determine if mirroring is to be used
  767. * by calling the pg_get_mirror_count op
  768. */
  769. static int nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio,
  770. struct nfs_page *req)
  771. {
  772. int mirror_count = 1;
  773. if (!pgio->pg_ops->pg_get_mirror_count)
  774. return 0;
  775. mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
  776. if (!mirror_count || mirror_count > NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX)
  777. return -EINVAL;
  778. if (WARN_ON_ONCE(!pgio->pg_mirrors_dynamic))
  779. return -EINVAL;
  780. pgio->pg_mirror_count = mirror_count;
  781. return 0;
  782. }
  783. /*
  784. * nfs_pageio_stop_mirroring - stop using mirroring (set mirror count to 1)
  785. */
  786. void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio)
  787. {
  788. pgio->pg_mirror_count = 1;
  789. pgio->pg_mirror_idx = 0;
  790. }
  791. static void nfs_pageio_cleanup_mirroring(struct nfs_pageio_descriptor *pgio)
  792. {
  793. pgio->pg_mirror_count = 1;
  794. pgio->pg_mirror_idx = 0;
  795. pgio->pg_mirrors = pgio->pg_mirrors_static;
  796. kfree(pgio->pg_mirrors_dynamic);
  797. pgio->pg_mirrors_dynamic = NULL;
  798. }
  799. static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
  800. const struct nfs_open_context *ctx2)
  801. {
  802. return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
  803. }
  804. static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
  805. const struct nfs_lock_context *l2)
  806. {
  807. return l1->lockowner.l_owner == l2->lockowner.l_owner
  808. && l1->lockowner.l_pid == l2->lockowner.l_pid;
  809. }
  810. /**
  811. * nfs_can_coalesce_requests - test two requests for compatibility
  812. * @prev: pointer to nfs_page
  813. * @req: pointer to nfs_page
  814. *
  815. * The nfs_page structures 'prev' and 'req' are compared to ensure that the
  816. * page data area they describe is contiguous, and that their RPC
  817. * credentials, NFSv4 open state, and lockowners are the same.
  818. *
  819. * Return 'true' if this is the case, else return 'false'.
  820. */
  821. static bool nfs_can_coalesce_requests(struct nfs_page *prev,
  822. struct nfs_page *req,
  823. struct nfs_pageio_descriptor *pgio)
  824. {
  825. size_t size;
  826. if (prev) {
  827. if (!nfs_match_open_context(req->wb_context, prev->wb_context))
  828. return false;
  829. if (req->wb_context->dentry->d_inode->i_flock != NULL &&
  830. !nfs_match_lock_context(req->wb_lock_context,
  831. prev->wb_lock_context))
  832. return false;
  833. if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
  834. return false;
  835. if (req->wb_page == prev->wb_page) {
  836. if (req->wb_pgbase != prev->wb_pgbase + prev->wb_bytes)
  837. return false;
  838. } else {
  839. if (req->wb_pgbase != 0 ||
  840. prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
  841. return false;
  842. }
  843. }
  844. size = pgio->pg_ops->pg_test(pgio, prev, req);
  845. WARN_ON_ONCE(size > req->wb_bytes);
  846. if (size && size < req->wb_bytes)
  847. req->wb_bytes = size;
  848. return size > 0;
  849. }
  850. /**
  851. * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
  852. * @desc: destination io descriptor
  853. * @req: request
  854. *
  855. * Returns true if the request 'req' was successfully coalesced into the
  856. * existing list of pages 'desc'.
  857. */
  858. static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
  859. struct nfs_page *req)
  860. {
  861. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  862. struct nfs_page *prev = NULL;
  863. if (mirror->pg_count != 0) {
  864. prev = nfs_list_entry(mirror->pg_list.prev);
  865. } else {
  866. if (desc->pg_ops->pg_init)
  867. desc->pg_ops->pg_init(desc, req);
  868. mirror->pg_base = req->wb_pgbase;
  869. }
  870. if (!nfs_can_coalesce_requests(prev, req, desc))
  871. return 0;
  872. nfs_list_remove_request(req);
  873. nfs_list_add_request(req, &mirror->pg_list);
  874. mirror->pg_count += req->wb_bytes;
  875. return 1;
  876. }
  877. /*
  878. * Helper for nfs_pageio_add_request and nfs_pageio_complete
  879. */
  880. static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
  881. {
  882. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  883. if (!list_empty(&mirror->pg_list)) {
  884. int error = desc->pg_ops->pg_doio(desc);
  885. if (error < 0)
  886. desc->pg_error = error;
  887. else
  888. mirror->pg_bytes_written += mirror->pg_count;
  889. }
  890. if (list_empty(&mirror->pg_list)) {
  891. mirror->pg_count = 0;
  892. mirror->pg_base = 0;
  893. }
  894. }
  895. /**
  896. * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
  897. * @desc: destination io descriptor
  898. * @req: request
  899. *
  900. * This may split a request into subrequests which are all part of the
  901. * same page group.
  902. *
  903. * Returns true if the request 'req' was successfully coalesced into the
  904. * existing list of pages 'desc'.
  905. */
  906. static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  907. struct nfs_page *req)
  908. {
  909. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  910. struct nfs_page *subreq;
  911. unsigned int bytes_left = 0;
  912. unsigned int offset, pgbase;
  913. nfs_page_group_lock(req, false);
  914. subreq = req;
  915. bytes_left = subreq->wb_bytes;
  916. offset = subreq->wb_offset;
  917. pgbase = subreq->wb_pgbase;
  918. do {
  919. if (!nfs_pageio_do_add_request(desc, subreq)) {
  920. /* make sure pg_test call(s) did nothing */
  921. WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
  922. WARN_ON_ONCE(subreq->wb_offset != offset);
  923. WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
  924. nfs_page_group_unlock(req);
  925. desc->pg_moreio = 1;
  926. nfs_pageio_doio(desc);
  927. if (desc->pg_error < 0)
  928. return 0;
  929. if (mirror->pg_recoalesce)
  930. return 0;
  931. /* retry add_request for this subreq */
  932. nfs_page_group_lock(req, false);
  933. continue;
  934. }
  935. /* check for buggy pg_test call(s) */
  936. WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
  937. WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
  938. WARN_ON_ONCE(subreq->wb_bytes == 0);
  939. bytes_left -= subreq->wb_bytes;
  940. offset += subreq->wb_bytes;
  941. pgbase += subreq->wb_bytes;
  942. if (bytes_left) {
  943. subreq = nfs_create_request(req->wb_context,
  944. req->wb_page,
  945. subreq, pgbase, bytes_left);
  946. if (IS_ERR(subreq))
  947. goto err_ptr;
  948. nfs_lock_request(subreq);
  949. subreq->wb_offset = offset;
  950. subreq->wb_index = req->wb_index;
  951. }
  952. } while (bytes_left > 0);
  953. nfs_page_group_unlock(req);
  954. return 1;
  955. err_ptr:
  956. desc->pg_error = PTR_ERR(subreq);
  957. nfs_page_group_unlock(req);
  958. return 0;
  959. }
  960. static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
  961. {
  962. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  963. LIST_HEAD(head);
  964. do {
  965. list_splice_init(&mirror->pg_list, &head);
  966. mirror->pg_bytes_written -= mirror->pg_count;
  967. mirror->pg_count = 0;
  968. mirror->pg_base = 0;
  969. mirror->pg_recoalesce = 0;
  970. desc->pg_moreio = 0;
  971. while (!list_empty(&head)) {
  972. struct nfs_page *req;
  973. req = list_first_entry(&head, struct nfs_page, wb_list);
  974. nfs_list_remove_request(req);
  975. if (__nfs_pageio_add_request(desc, req))
  976. continue;
  977. if (desc->pg_error < 0)
  978. return 0;
  979. break;
  980. }
  981. } while (mirror->pg_recoalesce);
  982. return 1;
  983. }
  984. static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc,
  985. struct nfs_page *req)
  986. {
  987. int ret;
  988. do {
  989. ret = __nfs_pageio_add_request(desc, req);
  990. if (ret)
  991. break;
  992. if (desc->pg_error < 0)
  993. break;
  994. ret = nfs_do_recoalesce(desc);
  995. } while (ret);
  996. return ret;
  997. }
  998. int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  999. struct nfs_page *req)
  1000. {
  1001. u32 midx;
  1002. unsigned int pgbase, offset, bytes;
  1003. struct nfs_page *dupreq, *lastreq;
  1004. pgbase = req->wb_pgbase;
  1005. offset = req->wb_offset;
  1006. bytes = req->wb_bytes;
  1007. nfs_pageio_setup_mirroring(desc, req);
  1008. for (midx = 0; midx < desc->pg_mirror_count; midx++) {
  1009. if (midx) {
  1010. nfs_page_group_lock(req, false);
  1011. /* find the last request */
  1012. for (lastreq = req->wb_head;
  1013. lastreq->wb_this_page != req->wb_head;
  1014. lastreq = lastreq->wb_this_page)
  1015. ;
  1016. dupreq = nfs_create_request(req->wb_context,
  1017. req->wb_page, lastreq, pgbase, bytes);
  1018. if (IS_ERR(dupreq)) {
  1019. nfs_page_group_unlock(req);
  1020. return 0;
  1021. }
  1022. nfs_lock_request(dupreq);
  1023. nfs_page_group_unlock(req);
  1024. dupreq->wb_offset = offset;
  1025. dupreq->wb_index = req->wb_index;
  1026. } else
  1027. dupreq = req;
  1028. if (nfs_pgio_has_mirroring(desc))
  1029. desc->pg_mirror_idx = midx;
  1030. if (!nfs_pageio_add_request_mirror(desc, dupreq))
  1031. return 0;
  1032. }
  1033. return 1;
  1034. }
  1035. /*
  1036. * nfs_pageio_complete_mirror - Complete I/O on the current mirror of an
  1037. * nfs_pageio_descriptor
  1038. * @desc: pointer to io descriptor
  1039. */
  1040. static void nfs_pageio_complete_mirror(struct nfs_pageio_descriptor *desc,
  1041. u32 mirror_idx)
  1042. {
  1043. struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[mirror_idx];
  1044. u32 restore_idx = desc->pg_mirror_idx;
  1045. if (nfs_pgio_has_mirroring(desc))
  1046. desc->pg_mirror_idx = mirror_idx;
  1047. for (;;) {
  1048. nfs_pageio_doio(desc);
  1049. if (!mirror->pg_recoalesce)
  1050. break;
  1051. if (!nfs_do_recoalesce(desc))
  1052. break;
  1053. }
  1054. desc->pg_mirror_idx = restore_idx;
  1055. }
  1056. /*
  1057. * nfs_pageio_resend - Transfer requests to new descriptor and resend
  1058. * @hdr - the pgio header to move request from
  1059. * @desc - the pageio descriptor to add requests to
  1060. *
  1061. * Try to move each request (nfs_page) from @hdr to @desc then attempt
  1062. * to send them.
  1063. *
  1064. * Returns 0 on success and < 0 on error.
  1065. */
  1066. int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
  1067. struct nfs_pgio_header *hdr)
  1068. {
  1069. LIST_HEAD(failed);
  1070. desc->pg_dreq = hdr->dreq;
  1071. while (!list_empty(&hdr->pages)) {
  1072. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  1073. nfs_list_remove_request(req);
  1074. if (!nfs_pageio_add_request(desc, req))
  1075. nfs_list_add_request(req, &failed);
  1076. }
  1077. nfs_pageio_complete(desc);
  1078. if (!list_empty(&failed)) {
  1079. list_move(&failed, &hdr->pages);
  1080. return -EIO;
  1081. }
  1082. return 0;
  1083. }
  1084. EXPORT_SYMBOL_GPL(nfs_pageio_resend);
  1085. /**
  1086. * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor
  1087. * @desc: pointer to io descriptor
  1088. */
  1089. void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
  1090. {
  1091. u32 midx;
  1092. for (midx = 0; midx < desc->pg_mirror_count; midx++)
  1093. nfs_pageio_complete_mirror(desc, midx);
  1094. if (desc->pg_ops->pg_cleanup)
  1095. desc->pg_ops->pg_cleanup(desc);
  1096. nfs_pageio_cleanup_mirroring(desc);
  1097. }
  1098. /**
  1099. * nfs_pageio_cond_complete - Conditional I/O completion
  1100. * @desc: pointer to io descriptor
  1101. * @index: page index
  1102. *
  1103. * It is important to ensure that processes don't try to take locks
  1104. * on non-contiguous ranges of pages as that might deadlock. This
  1105. * function should be called before attempting to wait on a locked
  1106. * nfs_page. It will complete the I/O if the page index 'index'
  1107. * is not contiguous with the existing list of pages in 'desc'.
  1108. */
  1109. void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
  1110. {
  1111. struct nfs_pgio_mirror *mirror;
  1112. struct nfs_page *prev;
  1113. u32 midx;
  1114. for (midx = 0; midx < desc->pg_mirror_count; midx++) {
  1115. mirror = &desc->pg_mirrors[midx];
  1116. if (!list_empty(&mirror->pg_list)) {
  1117. prev = nfs_list_entry(mirror->pg_list.prev);
  1118. if (index != prev->wb_index + 1)
  1119. nfs_pageio_complete_mirror(desc, midx);
  1120. }
  1121. }
  1122. }
  1123. int __init nfs_init_nfspagecache(void)
  1124. {
  1125. nfs_page_cachep = kmem_cache_create("nfs_page",
  1126. sizeof(struct nfs_page),
  1127. 0, SLAB_HWCACHE_ALIGN,
  1128. NULL);
  1129. if (nfs_page_cachep == NULL)
  1130. return -ENOMEM;
  1131. return 0;
  1132. }
  1133. void nfs_destroy_nfspagecache(void)
  1134. {
  1135. kmem_cache_destroy(nfs_page_cachep);
  1136. }
  1137. static const struct rpc_call_ops nfs_pgio_common_ops = {
  1138. .rpc_call_prepare = nfs_pgio_prepare,
  1139. .rpc_call_done = nfs_pgio_result,
  1140. .rpc_release = nfs_pgio_release,
  1141. };
  1142. const struct nfs_pageio_ops nfs_pgio_rw_ops = {
  1143. .pg_test = nfs_generic_pg_test,
  1144. .pg_doio = nfs_generic_pg_pgios,
  1145. };