pagelist.c 27 KB

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