pagelist.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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 (!test_and_set_bit(NFS_IOHDR_ERROR, &hdr->flags)
  70. || pos < hdr->io_start + hdr->good_bytes) {
  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, TASK_KILLABLE);
  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_data_destroy - make @hdr suitable for reuse
  467. *
  468. * Frees memory and releases refs from nfs_generic_pgio, so that it may
  469. * be called again.
  470. *
  471. * @hdr: A header that has had nfs_generic_pgio called
  472. */
  473. static void nfs_pgio_data_destroy(struct nfs_pgio_header *hdr)
  474. {
  475. if (hdr->args.context)
  476. put_nfs_open_context(hdr->args.context);
  477. if (hdr->page_array.pagevec != hdr->page_array.page_array)
  478. kfree(hdr->page_array.pagevec);
  479. }
  480. /*
  481. * nfs_pgio_header_free - Free a read or write header
  482. * @hdr: The header to free
  483. */
  484. void nfs_pgio_header_free(struct nfs_pgio_header *hdr)
  485. {
  486. nfs_pgio_data_destroy(hdr);
  487. hdr->rw_ops->rw_free_header(hdr);
  488. }
  489. EXPORT_SYMBOL_GPL(nfs_pgio_header_free);
  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. hdr->completion_ops->completion(hdr);
  597. /* TODO: Make sure it's right to clean up all mirrors here
  598. * and not just hdr->pgio_mirror_idx */
  599. for (midx = 0; midx < desc->pg_mirror_count; midx++) {
  600. mirror = &desc->pg_mirrors[midx];
  601. desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
  602. }
  603. return -ENOMEM;
  604. }
  605. /**
  606. * nfs_pgio_release - Release pageio data
  607. * @calldata: The pageio header to release
  608. */
  609. static void nfs_pgio_release(void *calldata)
  610. {
  611. struct nfs_pgio_header *hdr = calldata;
  612. hdr->completion_ops->completion(hdr);
  613. }
  614. static void nfs_pageio_mirror_init(struct nfs_pgio_mirror *mirror,
  615. unsigned int bsize)
  616. {
  617. INIT_LIST_HEAD(&mirror->pg_list);
  618. mirror->pg_bytes_written = 0;
  619. mirror->pg_count = 0;
  620. mirror->pg_bsize = bsize;
  621. mirror->pg_base = 0;
  622. mirror->pg_recoalesce = 0;
  623. }
  624. /**
  625. * nfs_pageio_init - initialise a page io descriptor
  626. * @desc: pointer to descriptor
  627. * @inode: pointer to inode
  628. * @pg_ops: pointer to pageio operations
  629. * @compl_ops: pointer to pageio completion operations
  630. * @rw_ops: pointer to nfs read/write operations
  631. * @bsize: io block size
  632. * @io_flags: extra parameters for the io function
  633. */
  634. void nfs_pageio_init(struct nfs_pageio_descriptor *desc,
  635. struct inode *inode,
  636. const struct nfs_pageio_ops *pg_ops,
  637. const struct nfs_pgio_completion_ops *compl_ops,
  638. const struct nfs_rw_ops *rw_ops,
  639. size_t bsize,
  640. int io_flags)
  641. {
  642. struct nfs_pgio_mirror *new;
  643. int i;
  644. desc->pg_moreio = 0;
  645. desc->pg_inode = inode;
  646. desc->pg_ops = pg_ops;
  647. desc->pg_completion_ops = compl_ops;
  648. desc->pg_rw_ops = rw_ops;
  649. desc->pg_ioflags = io_flags;
  650. desc->pg_error = 0;
  651. desc->pg_lseg = NULL;
  652. desc->pg_dreq = NULL;
  653. desc->pg_layout_private = NULL;
  654. desc->pg_bsize = bsize;
  655. desc->pg_mirror_count = 1;
  656. desc->pg_mirror_idx = 0;
  657. if (pg_ops->pg_get_mirror_count) {
  658. /* until we have a request, we don't have an lseg and no
  659. * idea how many mirrors there will be */
  660. new = kcalloc(NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX,
  661. sizeof(struct nfs_pgio_mirror), GFP_KERNEL);
  662. desc->pg_mirrors_dynamic = new;
  663. desc->pg_mirrors = new;
  664. for (i = 0; i < NFS_PAGEIO_DESCRIPTOR_MIRROR_MAX; i++)
  665. nfs_pageio_mirror_init(&desc->pg_mirrors[i], bsize);
  666. } else {
  667. desc->pg_mirrors_dynamic = NULL;
  668. desc->pg_mirrors = desc->pg_mirrors_static;
  669. nfs_pageio_mirror_init(&desc->pg_mirrors[0], bsize);
  670. }
  671. }
  672. EXPORT_SYMBOL_GPL(nfs_pageio_init);
  673. /**
  674. * nfs_pgio_result - Basic pageio error handling
  675. * @task: The task that ran
  676. * @calldata: Pageio header to check
  677. */
  678. static void nfs_pgio_result(struct rpc_task *task, void *calldata)
  679. {
  680. struct nfs_pgio_header *hdr = calldata;
  681. struct inode *inode = hdr->inode;
  682. dprintk("NFS: %s: %5u, (status %d)\n", __func__,
  683. task->tk_pid, task->tk_status);
  684. if (hdr->rw_ops->rw_done(task, hdr, inode) != 0)
  685. return;
  686. if (task->tk_status < 0)
  687. nfs_set_pgio_error(hdr, task->tk_status, hdr->args.offset);
  688. else
  689. hdr->rw_ops->rw_result(task, hdr);
  690. }
  691. /*
  692. * Create an RPC task for the given read or write request and kick it.
  693. * The page must have been locked by the caller.
  694. *
  695. * It may happen that the page we're passed is not marked dirty.
  696. * This is the case if nfs_updatepage detects a conflicting request
  697. * that has been written but not committed.
  698. */
  699. int nfs_generic_pgio(struct nfs_pageio_descriptor *desc,
  700. struct nfs_pgio_header *hdr)
  701. {
  702. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  703. struct nfs_page *req;
  704. struct page **pages,
  705. *last_page;
  706. struct list_head *head = &mirror->pg_list;
  707. struct nfs_commit_info cinfo;
  708. unsigned int pagecount, pageused;
  709. pagecount = nfs_page_array_len(mirror->pg_base, mirror->pg_count);
  710. if (!nfs_pgarray_set(&hdr->page_array, pagecount))
  711. return nfs_pgio_error(desc, hdr);
  712. nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq);
  713. pages = hdr->page_array.pagevec;
  714. last_page = NULL;
  715. pageused = 0;
  716. while (!list_empty(head)) {
  717. req = nfs_list_entry(head->next);
  718. nfs_list_remove_request(req);
  719. nfs_list_add_request(req, &hdr->pages);
  720. if (!last_page || last_page != req->wb_page) {
  721. pageused++;
  722. if (pageused > pagecount)
  723. break;
  724. *pages++ = last_page = req->wb_page;
  725. }
  726. }
  727. if (WARN_ON_ONCE(pageused != pagecount))
  728. return nfs_pgio_error(desc, hdr);
  729. if ((desc->pg_ioflags & FLUSH_COND_STABLE) &&
  730. (desc->pg_moreio || nfs_reqs_to_commit(&cinfo)))
  731. desc->pg_ioflags &= ~FLUSH_COND_STABLE;
  732. /* Set up the argument struct */
  733. nfs_pgio_rpcsetup(hdr, mirror->pg_count, 0, desc->pg_ioflags, &cinfo);
  734. desc->pg_rpc_callops = &nfs_pgio_common_ops;
  735. return 0;
  736. }
  737. EXPORT_SYMBOL_GPL(nfs_generic_pgio);
  738. static int nfs_generic_pg_pgios(struct nfs_pageio_descriptor *desc)
  739. {
  740. struct nfs_pgio_mirror *mirror;
  741. struct nfs_pgio_header *hdr;
  742. int ret;
  743. mirror = nfs_pgio_current_mirror(desc);
  744. hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
  745. if (!hdr) {
  746. /* TODO: make sure this is right with mirroring - or
  747. * should it back out all mirrors? */
  748. desc->pg_completion_ops->error_cleanup(&mirror->pg_list);
  749. return -ENOMEM;
  750. }
  751. nfs_pgheader_init(desc, hdr, nfs_pgio_header_free);
  752. ret = nfs_generic_pgio(desc, hdr);
  753. if (ret == 0)
  754. ret = nfs_initiate_pgio(NFS_CLIENT(hdr->inode),
  755. hdr,
  756. hdr->cred,
  757. NFS_PROTO(hdr->inode),
  758. desc->pg_rpc_callops,
  759. desc->pg_ioflags, 0);
  760. return ret;
  761. }
  762. /*
  763. * nfs_pageio_setup_mirroring - determine if mirroring is to be used
  764. * by calling the pg_get_mirror_count op
  765. */
  766. static int nfs_pageio_setup_mirroring(struct nfs_pageio_descriptor *pgio,
  767. struct nfs_page *req)
  768. {
  769. int mirror_count = 1;
  770. if (!pgio->pg_ops->pg_get_mirror_count)
  771. return 0;
  772. mirror_count = pgio->pg_ops->pg_get_mirror_count(pgio, req);
  773. if (pgio->pg_error < 0)
  774. return pgio->pg_error;
  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. if (desc->pg_error < 0)
  872. return 0;
  873. mirror->pg_base = req->wb_pgbase;
  874. }
  875. if (!nfs_can_coalesce_requests(prev, req, desc))
  876. return 0;
  877. nfs_list_remove_request(req);
  878. nfs_list_add_request(req, &mirror->pg_list);
  879. mirror->pg_count += req->wb_bytes;
  880. return 1;
  881. }
  882. /*
  883. * Helper for nfs_pageio_add_request and nfs_pageio_complete
  884. */
  885. static void nfs_pageio_doio(struct nfs_pageio_descriptor *desc)
  886. {
  887. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  888. if (!list_empty(&mirror->pg_list)) {
  889. int error = desc->pg_ops->pg_doio(desc);
  890. if (error < 0)
  891. desc->pg_error = error;
  892. else
  893. mirror->pg_bytes_written += mirror->pg_count;
  894. }
  895. if (list_empty(&mirror->pg_list)) {
  896. mirror->pg_count = 0;
  897. mirror->pg_base = 0;
  898. }
  899. }
  900. /**
  901. * nfs_pageio_add_request - Attempt to coalesce a request into a page list.
  902. * @desc: destination io descriptor
  903. * @req: request
  904. *
  905. * This may split a request into subrequests which are all part of the
  906. * same page group.
  907. *
  908. * Returns true if the request 'req' was successfully coalesced into the
  909. * existing list of pages 'desc'.
  910. */
  911. static int __nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  912. struct nfs_page *req)
  913. {
  914. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  915. struct nfs_page *subreq;
  916. unsigned int bytes_left = 0;
  917. unsigned int offset, pgbase;
  918. nfs_page_group_lock(req, false);
  919. subreq = req;
  920. bytes_left = subreq->wb_bytes;
  921. offset = subreq->wb_offset;
  922. pgbase = subreq->wb_pgbase;
  923. do {
  924. if (!nfs_pageio_do_add_request(desc, subreq)) {
  925. /* make sure pg_test call(s) did nothing */
  926. WARN_ON_ONCE(subreq->wb_bytes != bytes_left);
  927. WARN_ON_ONCE(subreq->wb_offset != offset);
  928. WARN_ON_ONCE(subreq->wb_pgbase != pgbase);
  929. nfs_page_group_unlock(req);
  930. desc->pg_moreio = 1;
  931. nfs_pageio_doio(desc);
  932. if (desc->pg_error < 0)
  933. return 0;
  934. if (mirror->pg_recoalesce)
  935. return 0;
  936. /* retry add_request for this subreq */
  937. nfs_page_group_lock(req, false);
  938. continue;
  939. }
  940. /* check for buggy pg_test call(s) */
  941. WARN_ON_ONCE(subreq->wb_bytes + subreq->wb_pgbase > PAGE_SIZE);
  942. WARN_ON_ONCE(subreq->wb_bytes > bytes_left);
  943. WARN_ON_ONCE(subreq->wb_bytes == 0);
  944. bytes_left -= subreq->wb_bytes;
  945. offset += subreq->wb_bytes;
  946. pgbase += subreq->wb_bytes;
  947. if (bytes_left) {
  948. subreq = nfs_create_request(req->wb_context,
  949. req->wb_page,
  950. subreq, pgbase, bytes_left);
  951. if (IS_ERR(subreq))
  952. goto err_ptr;
  953. nfs_lock_request(subreq);
  954. subreq->wb_offset = offset;
  955. subreq->wb_index = req->wb_index;
  956. }
  957. } while (bytes_left > 0);
  958. nfs_page_group_unlock(req);
  959. return 1;
  960. err_ptr:
  961. desc->pg_error = PTR_ERR(subreq);
  962. nfs_page_group_unlock(req);
  963. return 0;
  964. }
  965. static int nfs_do_recoalesce(struct nfs_pageio_descriptor *desc)
  966. {
  967. struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
  968. LIST_HEAD(head);
  969. do {
  970. list_splice_init(&mirror->pg_list, &head);
  971. mirror->pg_bytes_written -= mirror->pg_count;
  972. mirror->pg_count = 0;
  973. mirror->pg_base = 0;
  974. mirror->pg_recoalesce = 0;
  975. while (!list_empty(&head)) {
  976. struct nfs_page *req;
  977. req = list_first_entry(&head, struct nfs_page, wb_list);
  978. if (__nfs_pageio_add_request(desc, req))
  979. continue;
  980. if (desc->pg_error < 0) {
  981. list_splice_tail(&head, &mirror->pg_list);
  982. mirror->pg_recoalesce = 1;
  983. return 0;
  984. }
  985. break;
  986. }
  987. } while (mirror->pg_recoalesce);
  988. return 1;
  989. }
  990. static int nfs_pageio_add_request_mirror(struct nfs_pageio_descriptor *desc,
  991. struct nfs_page *req)
  992. {
  993. int ret;
  994. do {
  995. ret = __nfs_pageio_add_request(desc, req);
  996. if (ret)
  997. break;
  998. if (desc->pg_error < 0)
  999. break;
  1000. ret = nfs_do_recoalesce(desc);
  1001. } while (ret);
  1002. return ret;
  1003. }
  1004. int nfs_pageio_add_request(struct nfs_pageio_descriptor *desc,
  1005. struct nfs_page *req)
  1006. {
  1007. u32 midx;
  1008. unsigned int pgbase, offset, bytes;
  1009. struct nfs_page *dupreq, *lastreq;
  1010. pgbase = req->wb_pgbase;
  1011. offset = req->wb_offset;
  1012. bytes = req->wb_bytes;
  1013. nfs_pageio_setup_mirroring(desc, req);
  1014. if (desc->pg_error < 0)
  1015. return 0;
  1016. for (midx = 0; midx < desc->pg_mirror_count; midx++) {
  1017. if (midx) {
  1018. nfs_page_group_lock(req, false);
  1019. /* find the last request */
  1020. for (lastreq = req->wb_head;
  1021. lastreq->wb_this_page != req->wb_head;
  1022. lastreq = lastreq->wb_this_page)
  1023. ;
  1024. dupreq = nfs_create_request(req->wb_context,
  1025. req->wb_page, lastreq, pgbase, bytes);
  1026. if (IS_ERR(dupreq)) {
  1027. nfs_page_group_unlock(req);
  1028. return 0;
  1029. }
  1030. nfs_lock_request(dupreq);
  1031. nfs_page_group_unlock(req);
  1032. dupreq->wb_offset = offset;
  1033. dupreq->wb_index = req->wb_index;
  1034. } else
  1035. dupreq = req;
  1036. if (nfs_pgio_has_mirroring(desc))
  1037. desc->pg_mirror_idx = midx;
  1038. if (!nfs_pageio_add_request_mirror(desc, dupreq))
  1039. return 0;
  1040. }
  1041. return 1;
  1042. }
  1043. /*
  1044. * nfs_pageio_complete_mirror - Complete I/O on the current mirror of an
  1045. * nfs_pageio_descriptor
  1046. * @desc: pointer to io descriptor
  1047. * @mirror_idx: pointer to mirror index
  1048. */
  1049. static void nfs_pageio_complete_mirror(struct nfs_pageio_descriptor *desc,
  1050. u32 mirror_idx)
  1051. {
  1052. struct nfs_pgio_mirror *mirror = &desc->pg_mirrors[mirror_idx];
  1053. u32 restore_idx = desc->pg_mirror_idx;
  1054. if (nfs_pgio_has_mirroring(desc))
  1055. desc->pg_mirror_idx = mirror_idx;
  1056. for (;;) {
  1057. nfs_pageio_doio(desc);
  1058. if (desc->pg_error < 0 || !mirror->pg_recoalesce)
  1059. break;
  1060. if (!nfs_do_recoalesce(desc))
  1061. break;
  1062. }
  1063. desc->pg_mirror_idx = restore_idx;
  1064. }
  1065. /*
  1066. * nfs_pageio_resend - Transfer requests to new descriptor and resend
  1067. * @hdr - the pgio header to move request from
  1068. * @desc - the pageio descriptor to add requests to
  1069. *
  1070. * Try to move each request (nfs_page) from @hdr to @desc then attempt
  1071. * to send them.
  1072. *
  1073. * Returns 0 on success and < 0 on error.
  1074. */
  1075. int nfs_pageio_resend(struct nfs_pageio_descriptor *desc,
  1076. struct nfs_pgio_header *hdr)
  1077. {
  1078. LIST_HEAD(failed);
  1079. desc->pg_dreq = hdr->dreq;
  1080. while (!list_empty(&hdr->pages)) {
  1081. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  1082. nfs_list_remove_request(req);
  1083. if (!nfs_pageio_add_request(desc, req))
  1084. nfs_list_add_request(req, &failed);
  1085. }
  1086. nfs_pageio_complete(desc);
  1087. if (!list_empty(&failed)) {
  1088. list_move(&failed, &hdr->pages);
  1089. return desc->pg_error < 0 ? desc->pg_error : -EIO;
  1090. }
  1091. return 0;
  1092. }
  1093. EXPORT_SYMBOL_GPL(nfs_pageio_resend);
  1094. /**
  1095. * nfs_pageio_complete - Complete I/O then cleanup an nfs_pageio_descriptor
  1096. * @desc: pointer to io descriptor
  1097. */
  1098. void nfs_pageio_complete(struct nfs_pageio_descriptor *desc)
  1099. {
  1100. u32 midx;
  1101. for (midx = 0; midx < desc->pg_mirror_count; midx++)
  1102. nfs_pageio_complete_mirror(desc, midx);
  1103. if (desc->pg_ops->pg_cleanup)
  1104. desc->pg_ops->pg_cleanup(desc);
  1105. nfs_pageio_cleanup_mirroring(desc);
  1106. }
  1107. /**
  1108. * nfs_pageio_cond_complete - Conditional I/O completion
  1109. * @desc: pointer to io descriptor
  1110. * @index: page index
  1111. *
  1112. * It is important to ensure that processes don't try to take locks
  1113. * on non-contiguous ranges of pages as that might deadlock. This
  1114. * function should be called before attempting to wait on a locked
  1115. * nfs_page. It will complete the I/O if the page index 'index'
  1116. * is not contiguous with the existing list of pages in 'desc'.
  1117. */
  1118. void nfs_pageio_cond_complete(struct nfs_pageio_descriptor *desc, pgoff_t index)
  1119. {
  1120. struct nfs_pgio_mirror *mirror;
  1121. struct nfs_page *prev;
  1122. u32 midx;
  1123. for (midx = 0; midx < desc->pg_mirror_count; midx++) {
  1124. mirror = &desc->pg_mirrors[midx];
  1125. if (!list_empty(&mirror->pg_list)) {
  1126. prev = nfs_list_entry(mirror->pg_list.prev);
  1127. if (index != prev->wb_index + 1) {
  1128. nfs_pageio_complete(desc);
  1129. break;
  1130. }
  1131. }
  1132. }
  1133. }
  1134. int __init nfs_init_nfspagecache(void)
  1135. {
  1136. nfs_page_cachep = kmem_cache_create("nfs_page",
  1137. sizeof(struct nfs_page),
  1138. 0, SLAB_HWCACHE_ALIGN,
  1139. NULL);
  1140. if (nfs_page_cachep == NULL)
  1141. return -ENOMEM;
  1142. return 0;
  1143. }
  1144. void nfs_destroy_nfspagecache(void)
  1145. {
  1146. kmem_cache_destroy(nfs_page_cachep);
  1147. }
  1148. static const struct rpc_call_ops nfs_pgio_common_ops = {
  1149. .rpc_call_prepare = nfs_pgio_prepare,
  1150. .rpc_call_done = nfs_pgio_result,
  1151. .rpc_release = nfs_pgio_release,
  1152. };
  1153. const struct nfs_pageio_ops nfs_pgio_rw_ops = {
  1154. .pg_test = nfs_generic_pg_test,
  1155. .pg_doio = nfs_generic_pg_pgios,
  1156. };