pagelist.c 34 KB

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