pagelist.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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: initiated pgio call "
  565. "(req %s/%llu, %u bytes @ offset %llu)\n",
  566. hdr->inode->i_sb->s_id,
  567. (unsigned long long)NFS_FILEID(hdr->inode),
  568. hdr->args.count,
  569. (unsigned long long)hdr->args.offset);
  570. task = rpc_run_task(&task_setup_data);
  571. if (IS_ERR(task)) {
  572. ret = PTR_ERR(task);
  573. goto out;
  574. }
  575. if (how & FLUSH_SYNC) {
  576. ret = rpc_wait_for_completion_task(task);
  577. if (ret == 0)
  578. ret = task->tk_status;
  579. }
  580. rpc_put_task(task);
  581. out:
  582. return ret;
  583. }
  584. EXPORT_SYMBOL_GPL(nfs_initiate_pgio);
  585. /**
  586. * nfs_pgio_error - Clean up from a pageio error
  587. * @desc: IO descriptor
  588. * @hdr: pageio header
  589. */
  590. static int nfs_pgio_error(struct nfs_pageio_descriptor *desc,
  591. struct nfs_pgio_header *hdr)
  592. {
  593. struct nfs_pgio_mirror *mirror;
  594. u32 midx;
  595. set_bit(NFS_IOHDR_REDO, &hdr->flags);
  596. nfs_pgio_data_destroy(hdr);
  597. hdr->completion_ops->completion(hdr);
  598. /* TODO: Make sure it's right to clean up all mirrors here
  599. * and not just hdr->pgio_mirror_idx */
  600. for (midx = 0; midx < desc->pg_mirror_count; midx++) {
  601. mirror = &desc->pg_mirrors[midx];
  602. desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
  603. }
  604. return -ENOMEM;
  605. }
  606. /**
  607. * nfs_pgio_release - Release pageio data
  608. * @calldata: The pageio header to release
  609. */
  610. static void nfs_pgio_release(void *calldata)
  611. {
  612. struct nfs_pgio_header *hdr = calldata;
  613. nfs_pgio_data_destroy(hdr);
  614. hdr->completion_ops->completion(hdr);
  615. }
  616. static void nfs_pageio_mirror_init(struct nfs_pgio_mirror *mirror,
  617. unsigned int bsize)
  618. {
  619. INIT_LIST_HEAD(&mirror->pg_list);
  620. mirror->pg_bytes_written = 0;
  621. mirror->pg_count = 0;
  622. mirror->pg_bsize = bsize;
  623. mirror->pg_base = 0;
  624. mirror->pg_recoalesce = 0;
  625. }
  626. /**
  627. * nfs_pageio_init - initialise a page io descriptor
  628. * @desc: pointer to descriptor
  629. * @inode: pointer to inode
  630. * @pg_ops: pointer to pageio operations
  631. * @compl_ops: pointer to pageio completion operations
  632. * @rw_ops: pointer to nfs read/write operations
  633. * @bsize: io block size
  634. * @io_flags: extra parameters for the io function
  635. */
  636. void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
  637. struct inode *inode,
  638. const struct nfs_pageio_ops *pg_ops,
  639. const struct nfs_pgio_completion_ops *compl_ops,
  640. const struct nfs_rw_ops *rw_ops,
  641. size_t bsize,
  642. int io_flags)
  643. {
  644. struct nfs_pgio_mirror *new;
  645. int i;
  646. desc->pg_moreio = 0;
  647. desc->pg_inode = inode;
  648. desc->pg_ops = pg_ops;
  649. desc->pg_completion_ops = compl_ops;
  650. desc->pg_rw_ops = rw_ops;
  651. desc->pg_ioflags = io_flags;
  652. desc->pg_error = 0;
  653. desc->pg_lseg = NULL;
  654. desc->pg_dreq = NULL;
  655. desc->pg_layout_private = NULL;
  656. desc->pg_bsize = bsize;
  657. desc->pg_mirror_count = 1;
  658. desc->pg_mirror_idx = 0;
  659. if (pg_ops->pg_get_mirror_count) {
  660. /* until we have a request, we don't have an lseg and no
  661. * idea how many mirrors there will be */
  662. new = kcalloc(NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX,
  663. sizeof(struct nfs_pgio_mirror), GFP_KERNEL);
  664. desc->pg_mirrors_dynamic = new;
  665. desc->pg_mirrors = new;
  666. for (i = 0; i < NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX; i++)
  667. nfs_pageio_mirror_init(&desc->pg_mirrors[i], bsize);
  668. } else {
  669. desc->pg_mirrors_dynamic = NULL;
  670. desc->pg_mirrors = desc->pg_mirrors_static;
  671. nfs_pageio_mirror_init(&desc->pg_mirrors[0], bsize);
  672. }
  673. }
  674. EXPORT_SYMBOL_GPL(nfs_pageio_init);
  675. /**
  676. * nfs_pgio_result - Basic pageio error handling
  677. * @task: The task that ran
  678. * @calldata: Pageio header to check
  679. */
  680. static void nfs_pgio_result(struct rpc_task *task, void *calldata)
  681. {
  682. struct nfs_pgio_header *hdr = calldata;
  683. struct inode *inode = hdr->inode;
  684. dprintk("NFS: %s: %5u, (status %d)\n", __func__,
  685. task->tk_pid, task->tk_status);
  686. if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
  687. return;
  688. if (task->tk_status < 0)
  689. nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
  690. else
  691. hdr->rw_ops->rw_result(task, hdr);
  692. }
  693. /*
  694. * Create an RPC task for the given read or write request and kick it.
  695. * The page must have been locked by the caller.
  696. *
  697. * It may happen that the page we're passed is not marked dirty.
  698. * This is the case if nfs_updatepage detects a conflicting request
  699. * that has been written but not committed.
  700. */
  701. int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
  702. struct nfs_pgio_header *hdr)
  703. {
  704. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  705. struct nfs_page *req;
  706. struct page **pages,
  707. *last_page;
  708. struct list_head *head = &mirror->pg_list;
  709. struct nfs_commit_info cinfo;
  710. unsigned int pagecount, pageused;
  711. pagecount = nfs_page_array_len(mirror->pg_base, mirror->pg_count);
  712. if (!nfs_pgarray_set(&hdr->page_array, pagecount))
  713. return nfs_pgio_error(desc, hdr);
  714. nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
  715. pages = hdr->page_array.pagevec;
  716. last_page = NULL;
  717. pageused = 0;
  718. while (!list_empty(head)) {
  719. req = nfs_list_entry(head->next);
  720. nfs_list_remove_request(req);
  721. nfs_list_add_request(req, &hdr->pages);
  722. if (!last_page || last_page != req->wb_page) {
  723. pageused++;
  724. if (pageused > pagecount)
  725. break;
  726. *pages++ = last_page = req->wb_page;
  727. }
  728. }
  729. if (WARN_ON_ONCE(pageused != pagecount))
  730. return nfs_pgio_error(desc, hdr);
  731. if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
  732. (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
  733. desc->pg_ioflags &= ~FLUSH_COND_STABLE;
  734. /* Set up the argument struct */
  735. nfs_pgio_rpcsetup(hdr, mirror->pg_count, 0, desc->pg_ioflags, &cinfo);
  736. desc->pg_rpc_callops = &nfs_pgio_common_ops;
  737. return 0;
  738. }
  739. EXPORT_SYMBOL_GPL(nfs_generic_pgio);
  740. static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
  741. {
  742. struct nfs_pgio_mirror *mirror;
  743. struct nfs_pgio_header *hdr;
  744. int ret;
  745. mirror = nfs_pgio_current_mirror(desc);
  746. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  747. if (!hdr) {
  748. /* TODO: make sure this is right with mirroring - or
  749. * should it back out all mirrors? */
  750. desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
  751. return -ENOMEM;
  752. }
  753. nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
  754. ret = nfs_generic_pgio(desc, hdr);
  755. if (ret == 0)
  756. ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
  757. hdr,
  758. hdr->cred,
  759. NFS_PROTO(hdr->inode),
  760. desc->pg_rpc_callops,
  761. desc->pg_ioflags, 0);
  762. return ret;
  763. }
  764. /*
  765. * nfs_pageio_setup_mirroring - determine if mirroring is to be used
  766. * by calling the pg_get_mirror_count op
  767. */
  768. static int nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio,
  769. struct nfs_page *req)
  770. {
  771. int mirror_count = 1;
  772. if (!pgio->pg_ops->pg_get_mirror_count)
  773. return 0;
  774. mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
  775. if (!mirror_count || mirror_count > NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX)
  776. return -EINVAL;
  777. if (WARN_ON_ONCE(!pgio->pg_mirrors_dynamic))
  778. return -EINVAL;
  779. pgio->pg_mirror_count = mirror_count;
  780. return 0;
  781. }
  782. /*
  783. * nfs_pageio_stop_mirroring - stop using mirroring (set mirror count to 1)
  784. */
  785. void nfs_pageio_stop_mirroring(struct nfs_pageio_descriptor *pgio)
  786. {
  787. pgio->pg_mirror_count = 1;
  788. pgio->pg_mirror_idx = 0;
  789. }
  790. static void nfs_pageio_cleanup_mirroring(struct nfs_pageio_descriptor *pgio)
  791. {
  792. pgio->pg_mirror_count = 1;
  793. pgio->pg_mirror_idx = 0;
  794. pgio->pg_mirrors = pgio->pg_mirrors_static;
  795. kfree(pgio->pg_mirrors_dynamic);
  796. pgio->pg_mirrors_dynamic = NULL;
  797. }
  798. static bool nfs_match_open_context(const struct nfs_open_context *ctx1,
  799. const struct nfs_open_context *ctx2)
  800. {
  801. return ctx1->cred == ctx2->cred && ctx1->state == ctx2->state;
  802. }
  803. static bool nfs_match_lock_context(const struct nfs_lock_context *l1,
  804. const struct nfs_lock_context *l2)
  805. {
  806. return l1->lockowner.l_owner == l2->lockowner.l_owner
  807. && l1->lockowner.l_pid == l2->lockowner.l_pid;
  808. }
  809. /**
  810. * nfs_can_coalesce_requests - test two requests for compatibility
  811. * @prev: pointer to nfs_page
  812. * @req: pointer to nfs_page
  813. *
  814. * The nfs_page structures 'prev' and 'req' are compared to ensure that the
  815. * page data area they describe is contiguous, and that their RPC
  816. * credentials, NFSv4 open state, and lockowners are the same.
  817. *
  818. * Return 'true' if this is the case, else return 'false'.
  819. */
  820. static bool nfs_can_coalesce_requests(struct nfs_page *prev,
  821. struct nfs_page *req,
  822. struct nfs_pageio_descriptor *pgio)
  823. {
  824. size_t size;
  825. struct file_lock_context *flctx;
  826. if (prev) {
  827. if (!nfs_match_open_context(req->wb_context, prev->wb_context))
  828. return false;
  829. flctx = d_inode(req->wb_context->dentry)->i_flctx;
  830. if (flctx != NULL &&
  831. !(list_empty_careful(&flctx->flc_posix) &&
  832. list_empty_careful(&flctx->flc_flock)) &&
  833. !nfs_match_lock_context(req->wb_lock_context,
  834. prev->wb_lock_context))
  835. return false;
  836. if (req_offset(req) != req_offset(prev) + prev->wb_bytes)
  837. return false;
  838. if (req->wb_page == prev->wb_page) {
  839. if (req->wb_pgbase != prev->wb_pgbase + prev->wb_bytes)
  840. return false;
  841. } else {
  842. if (req->wb_pgbase != 0 ||
  843. prev->wb_pgbase + prev->wb_bytes != PAGE_CACHE_SIZE)
  844. return false;
  845. }
  846. }
  847. size = pgio->pg_ops->pg_test(pgio, prev, req);
  848. WARN_ON_ONCE(size > req->wb_bytes);
  849. if (size && size < req->wb_bytes)
  850. req->wb_bytes = size;
  851. return size > 0;
  852. }
  853. /**
  854. * nfs_pageio_do_add_request - Attempt to coalesce a request into a page list.
  855. * @desc: destination io descriptor
  856. * @req: request
  857. *
  858. * Returns true if the request 'req' was successfully coalesced into the
  859. * existing list of pages 'desc'.
  860. */
  861. static int nfs_pageio_do_add_request(struct nfs_pageio_descriptor *desc,
  862. struct nfs_page *req)
  863. {
  864. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  865. struct nfs_page *prev = NULL;
  866. if (mirror->pg_count != 0) {
  867. prev = nfs_list_entry(mirror->pg_list.prev);
  868. } else {
  869. if (desc->pg_ops->pg_init)
  870. desc->pg_ops->pg_init(desc, req);
  871. mirror->pg_base = req->wb_pgbase;
  872. }
  873. if (!nfs_can_coalesce_requests(prev, req, desc))
  874. return 0;
  875. nfs_list_remove_request(req);
  876. nfs_list_add_request(req, &mirror->pg_list);
  877. mirror->pg_count += req->wb_bytes;
  878. return 1;
  879. }
  880. /*
  881. * Helper for nfs_pageio_add_request and nfs_pageio_complete
  882. */
  883. static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
  884. {
  885. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  886. if (!list_empty(&mirror->pg_list)) {
  887. int error = desc->pg_ops->pg_doio(desc);
  888. if (error < 0)
  889. desc->pg_error = error;
  890. else
  891. mirror->pg_bytes_written += mirror->pg_count;
  892. }
  893. if (list_empty(&mirror->pg_list)) {
  894. mirror->pg_count = 0;
  895. mirror->pg_base = 0;
  896. }
  897. }
  898. /**
  899. * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
  900. * @desc: destination io descriptor
  901. * @req: request
  902. *
  903. * This may split a request into subrequests which are all part of the
  904. * same page group.
  905. *
  906. * Returns true if the request 'req' was successfully coalesced into the
  907. * existing list of pages 'desc'.
  908. */
  909. static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  910. struct nfs_page *req)
  911. {
  912. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  913. struct nfs_page *subreq;
  914. unsigned int bytes_left = 0;
  915. unsigned int offset, pgbase;
  916. nfs_page_group_lock(req, false);
  917. subreq = req;
  918. bytes_left = subreq->wb_bytes;
  919. offset = subreq->wb_offset;
  920. pgbase = subreq->wb_pgbase;
  921. do {
  922. if (!nfs_pageio_do_add_request(desc, subreq)) {
  923. /* make sure pg_test call(s) did nothing */
  924. WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
  925. WARN_ON_ONCE(subreq->wb_offset != offset);
  926. WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
  927. nfs_page_group_unlock(req);
  928. desc->pg_moreio = 1;
  929. nfs_pageio_doio(desc);
  930. if (desc->pg_error < 0)
  931. return 0;
  932. if (mirror->pg_recoalesce)
  933. return 0;
  934. /* retry add_request for this subreq */
  935. nfs_page_group_lock(req, false);
  936. continue;
  937. }
  938. /* check for buggy pg_test call(s) */
  939. WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
  940. WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
  941. WARN_ON_ONCE(subreq->wb_bytes == 0);
  942. bytes_left -= subreq->wb_bytes;
  943. offset += subreq->wb_bytes;
  944. pgbase += subreq->wb_bytes;
  945. if (bytes_left) {
  946. subreq = nfs_create_request(req->wb_context,
  947. req->wb_page,
  948. subreq, pgbase, bytes_left);
  949. if (IS_ERR(subreq))
  950. goto err_ptr;
  951. nfs_lock_request(subreq);
  952. subreq->wb_offset = offset;
  953. subreq->wb_index = req->wb_index;
  954. }
  955. } while (bytes_left > 0);
  956. nfs_page_group_unlock(req);
  957. return 1;
  958. err_ptr:
  959. desc->pg_error = PTR_ERR(subreq);
  960. nfs_page_group_unlock(req);
  961. return 0;
  962. }
  963. static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
  964. {
  965. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  966. LIST_HEAD(head);
  967. do {
  968. list_splice_init(&mirror->pg_list, &head);
  969. mirror->pg_bytes_written -= mirror->pg_count;
  970. mirror->pg_count = 0;
  971. mirror->pg_base = 0;
  972. mirror->pg_recoalesce = 0;
  973. desc->pg_moreio = 0;
  974. while (!list_empty(&head)) {
  975. struct nfs_page *req;
  976. req = list_first_entry(&head, struct nfs_page, wb_list);
  977. nfs_list_remove_request(req);
  978. if (__nfs_pageio_add_request(desc, req))
  979. continue;
  980. if (desc->pg_error < 0)
  981. return 0;
  982. break;
  983. }
  984. } while (mirror->pg_recoalesce);
  985. return 1;
  986. }
  987. static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc,
  988. struct nfs_page *req)
  989. {
  990. int ret;
  991. do {
  992. ret = __nfs_pageio_add_request(desc, req);
  993. if (ret)
  994. break;
  995. if (desc->pg_error < 0)
  996. break;
  997. ret = nfs_do_recoalesce(desc);
  998. } while (ret);
  999. return ret;
  1000. }
  1001. int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  1002. struct nfs_page *req)
  1003. {
  1004. u32 midx;
  1005. unsigned int pgbase, offset, bytes;
  1006. struct nfs_page *dupreq, *lastreq;
  1007. pgbase = req->wb_pgbase;
  1008. offset = req->wb_offset;
  1009. bytes = req->wb_bytes;
  1010. nfs_pageio_setup_mirroring(desc, req);
  1011. for (midx = 0; midx < desc->pg_mirror_count; midx++) {
  1012. if (midx) {
  1013. nfs_page_group_lock(req, false);
  1014. /* find the last request */
  1015. for (lastreq = req->wb_head;
  1016. lastreq->wb_this_page != req->wb_head;
  1017. lastreq = lastreq->wb_this_page)
  1018. ;
  1019. dupreq = nfs_create_request(req->wb_context,
  1020. req->wb_page, lastreq, pgbase, bytes);
  1021. if (IS_ERR(dupreq)) {
  1022. nfs_page_group_unlock(req);
  1023. return 0;
  1024. }
  1025. nfs_lock_request(dupreq);
  1026. nfs_page_group_unlock(req);
  1027. dupreq->wb_offset = offset;
  1028. dupreq->wb_index = req->wb_index;
  1029. } else
  1030. dupreq = req;
  1031. if (nfs_pgio_has_mirroring(desc))
  1032. desc->pg_mirror_idx = midx;
  1033. if (!nfs_pageio_add_request_mirror(desc, dupreq))
  1034. return 0;
  1035. }
  1036. return 1;
  1037. }
  1038. /*
  1039. * nfs_pageio_complete_mirror - Complete I/O on the current mirror of an
  1040. * nfs_pageio_descriptor
  1041. * @desc: pointer to io descriptor
  1042. * @mirror_idx: pointer to mirror index
  1043. */
  1044. static void nfs_pageio_complete_mirror(struct nfs_pageio_descriptor *desc,
  1045. u32 mirror_idx)
  1046. {
  1047. struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[mirror_idx];
  1048. u32 restore_idx = desc->pg_mirror_idx;
  1049. if (nfs_pgio_has_mirroring(desc))
  1050. desc->pg_mirror_idx = mirror_idx;
  1051. for (;;) {
  1052. nfs_pageio_doio(desc);
  1053. if (!mirror->pg_recoalesce)
  1054. break;
  1055. if (!nfs_do_recoalesce(desc))
  1056. break;
  1057. }
  1058. desc->pg_mirror_idx = restore_idx;
  1059. }
  1060. /*
  1061. * nfs_pageio_resend - Transfer requests to new descriptor and resend
  1062. * @hdr - the pgio header to move request from
  1063. * @desc - the pageio descriptor to add requests to
  1064. *
  1065. * Try to move each request (nfs_page) from @hdr to @desc then attempt
  1066. * to send them.
  1067. *
  1068. * Returns 0 on success and < 0 on error.
  1069. */
  1070. int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
  1071. struct nfs_pgio_header *hdr)
  1072. {
  1073. LIST_HEAD(failed);
  1074. desc->pg_dreq = hdr->dreq;
  1075. while (!list_empty(&hdr->pages)) {
  1076. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  1077. nfs_list_remove_request(req);
  1078. if (!nfs_pageio_add_request(desc, req))
  1079. nfs_list_add_request(req, &failed);
  1080. }
  1081. nfs_pageio_complete(desc);
  1082. if (!list_empty(&failed)) {
  1083. list_move(&failed, &hdr->pages);
  1084. return -EIO;
  1085. }
  1086. return 0;
  1087. }
  1088. EXPORT_SYMBOL_GPL(nfs_pageio_resend);
  1089. /**
  1090. * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor
  1091. * @desc: pointer to io descriptor
  1092. */
  1093. void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
  1094. {
  1095. u32 midx;
  1096. for (midx = 0; midx < desc->pg_mirror_count; midx++)
  1097. nfs_pageio_complete_mirror(desc, midx);
  1098. if (desc->pg_ops->pg_cleanup)
  1099. desc->pg_ops->pg_cleanup(desc);
  1100. nfs_pageio_cleanup_mirroring(desc);
  1101. }
  1102. /**
  1103. * nfs_pageio_cond_complete - Conditional I/O completion
  1104. * @desc: pointer to io descriptor
  1105. * @index: page index
  1106. *
  1107. * It is important to ensure that processes don't try to take locks
  1108. * on non-contiguous ranges of pages as that might deadlock. This
  1109. * function should be called before attempting to wait on a locked
  1110. * nfs_page. It will complete the I/O if the page index 'index'
  1111. * is not contiguous with the existing list of pages in 'desc'.
  1112. */
  1113. void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
  1114. {
  1115. struct nfs_pgio_mirror *mirror;
  1116. struct nfs_page *prev;
  1117. u32 midx;
  1118. for (midx = 0; midx < desc->pg_mirror_count; midx++) {
  1119. mirror = &desc->pg_mirrors[midx];
  1120. if (!list_empty(&mirror->pg_list)) {
  1121. prev = nfs_list_entry(mirror->pg_list.prev);
  1122. if (index != prev->wb_index + 1)
  1123. nfs_pageio_complete_mirror(desc, midx);
  1124. }
  1125. }
  1126. }
  1127. int __init nfs_init_nfspagecache(void)
  1128. {
  1129. nfs_page_cachep = kmem_cache_create("nfs_page",
  1130. sizeof(struct nfs_page),
  1131. 0, SLAB_HWCACHE_ALIGN,
  1132. NULL);
  1133. if (nfs_page_cachep == NULL)
  1134. return -ENOMEM;
  1135. return 0;
  1136. }
  1137. void nfs_destroy_nfspagecache(void)
  1138. {
  1139. kmem_cache_destroy(nfs_page_cachep);
  1140. }
  1141. static const struct rpc_call_ops nfs_pgio_common_ops = {
  1142. .rpc_call_prepare = nfs_pgio_prepare,
  1143. .rpc_call_done = nfs_pgio_result,
  1144. .rpc_release = nfs_pgio_release,
  1145. };
  1146. const struct nfs_pageio_ops nfs_pgio_rw_ops = {
  1147. .pg_test = nfs_generic_pg_test,
  1148. .pg_doio = nfs_generic_pg_pgios,
  1149. };