pagelist.c 27 KB

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