pagelist.c 34 KB

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