pagelist.c 34 KB

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