pagelist.c 33 KB

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