pagelist.c 28 KB

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