write.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151
  1. /*
  2. * linux/fs/nfs/write.c
  3. *
  4. * Write file data over NFS.
  5. *
  6. * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/slab.h>
  10. #include <linux/mm.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/file.h>
  13. #include <linux/writeback.h>
  14. #include <linux/swap.h>
  15. #include <linux/migrate.h>
  16. #include <linux/sunrpc/clnt.h>
  17. #include <linux/nfs_fs.h>
  18. #include <linux/nfs_mount.h>
  19. #include <linux/nfs_page.h>
  20. #include <linux/backing-dev.h>
  21. #include <linux/export.h>
  22. #include <linux/freezer.h>
  23. #include <linux/wait.h>
  24. #include <linux/iversion.h>
  25. #include <linux/uaccess.h>
  26. #include "delegation.h"
  27. #include "internal.h"
  28. #include "iostat.h"
  29. #include "nfs4_fs.h"
  30. #include "fscache.h"
  31. #include "pnfs.h"
  32. #include "nfstrace.h"
  33. #define NFSDBG_FACILITY NFSDBG_PAGECACHE
  34. #define MIN_POOL_WRITE (32)
  35. #define MIN_POOL_COMMIT (4)
  36. struct nfs_io_completion {
  37. void (*complete)(void *data);
  38. void *data;
  39. struct kref refcount;
  40. };
  41. /*
  42. * Local function declarations
  43. */
  44. static void nfs_redirty_request(struct nfs_page *req);
  45. static const struct rpc_call_ops nfs_commit_ops;
  46. static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops;
  47. static const struct nfs_commit_completion_ops nfs_commit_completion_ops;
  48. static const struct nfs_rw_ops nfs_rw_write_ops;
  49. static void nfs_clear_request_commit(struct nfs_page *req);
  50. static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
  51. struct inode *inode);
  52. static struct nfs_page *
  53. nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
  54. struct page *page);
  55. static struct kmem_cache *nfs_wdata_cachep;
  56. static mempool_t *nfs_wdata_mempool;
  57. static struct kmem_cache *nfs_cdata_cachep;
  58. static mempool_t *nfs_commit_mempool;
  59. struct nfs_commit_data *nfs_commitdata_alloc(bool never_fail)
  60. {
  61. struct nfs_commit_data *p;
  62. if (never_fail)
  63. p = mempool_alloc(nfs_commit_mempool, GFP_NOIO);
  64. else {
  65. /* It is OK to do some reclaim, not no safe to wait
  66. * for anything to be returned to the pool.
  67. * mempool_alloc() cannot handle that particular combination,
  68. * so we need two separate attempts.
  69. */
  70. p = mempool_alloc(nfs_commit_mempool, GFP_NOWAIT);
  71. if (!p)
  72. p = kmem_cache_alloc(nfs_cdata_cachep, GFP_NOIO |
  73. __GFP_NOWARN | __GFP_NORETRY);
  74. if (!p)
  75. return NULL;
  76. }
  77. memset(p, 0, sizeof(*p));
  78. INIT_LIST_HEAD(&p->pages);
  79. return p;
  80. }
  81. EXPORT_SYMBOL_GPL(nfs_commitdata_alloc);
  82. void nfs_commit_free(struct nfs_commit_data *p)
  83. {
  84. mempool_free(p, nfs_commit_mempool);
  85. }
  86. EXPORT_SYMBOL_GPL(nfs_commit_free);
  87. static struct nfs_pgio_header *nfs_writehdr_alloc(void)
  88. {
  89. struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO);
  90. memset(p, 0, sizeof(*p));
  91. p->rw_mode = FMODE_WRITE;
  92. return p;
  93. }
  94. static void nfs_writehdr_free(struct nfs_pgio_header *hdr)
  95. {
  96. mempool_free(hdr, nfs_wdata_mempool);
  97. }
  98. static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags)
  99. {
  100. return kmalloc(sizeof(struct nfs_io_completion), gfp_flags);
  101. }
  102. static void nfs_io_completion_init(struct nfs_io_completion *ioc,
  103. void (*complete)(void *), void *data)
  104. {
  105. ioc->complete = complete;
  106. ioc->data = data;
  107. kref_init(&ioc->refcount);
  108. }
  109. static void nfs_io_completion_release(struct kref *kref)
  110. {
  111. struct nfs_io_completion *ioc = container_of(kref,
  112. struct nfs_io_completion, refcount);
  113. ioc->complete(ioc->data);
  114. kfree(ioc);
  115. }
  116. static void nfs_io_completion_get(struct nfs_io_completion *ioc)
  117. {
  118. if (ioc != NULL)
  119. kref_get(&ioc->refcount);
  120. }
  121. static void nfs_io_completion_put(struct nfs_io_completion *ioc)
  122. {
  123. if (ioc != NULL)
  124. kref_put(&ioc->refcount, nfs_io_completion_release);
  125. }
  126. static struct nfs_page *
  127. nfs_page_private_request(struct page *page)
  128. {
  129. if (!PagePrivate(page))
  130. return NULL;
  131. return (struct nfs_page *)page_private(page);
  132. }
  133. /*
  134. * nfs_page_find_head_request_locked - find head request associated with @page
  135. *
  136. * must be called while holding the inode lock.
  137. *
  138. * returns matching head request with reference held, or NULL if not found.
  139. */
  140. static struct nfs_page *
  141. nfs_page_find_private_request(struct page *page)
  142. {
  143. struct address_space *mapping = page_file_mapping(page);
  144. struct nfs_page *req;
  145. if (!PagePrivate(page))
  146. return NULL;
  147. spin_lock(&mapping->private_lock);
  148. req = nfs_page_private_request(page);
  149. if (req) {
  150. WARN_ON_ONCE(req->wb_head != req);
  151. kref_get(&req->wb_kref);
  152. }
  153. spin_unlock(&mapping->private_lock);
  154. return req;
  155. }
  156. static struct nfs_page *
  157. nfs_page_find_swap_request(struct page *page)
  158. {
  159. struct inode *inode = page_file_mapping(page)->host;
  160. struct nfs_inode *nfsi = NFS_I(inode);
  161. struct nfs_page *req = NULL;
  162. if (!PageSwapCache(page))
  163. return NULL;
  164. mutex_lock(&nfsi->commit_mutex);
  165. if (PageSwapCache(page)) {
  166. req = nfs_page_search_commits_for_head_request_locked(nfsi,
  167. page);
  168. if (req) {
  169. WARN_ON_ONCE(req->wb_head != req);
  170. kref_get(&req->wb_kref);
  171. }
  172. }
  173. mutex_unlock(&nfsi->commit_mutex);
  174. return req;
  175. }
  176. /*
  177. * nfs_page_find_head_request - find head request associated with @page
  178. *
  179. * returns matching head request with reference held, or NULL if not found.
  180. */
  181. static struct nfs_page *nfs_page_find_head_request(struct page *page)
  182. {
  183. struct nfs_page *req;
  184. req = nfs_page_find_private_request(page);
  185. if (!req)
  186. req = nfs_page_find_swap_request(page);
  187. return req;
  188. }
  189. /* Adjust the file length if we're writing beyond the end */
  190. static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
  191. {
  192. struct inode *inode = page_file_mapping(page)->host;
  193. loff_t end, i_size;
  194. pgoff_t end_index;
  195. spin_lock(&inode->i_lock);
  196. i_size = i_size_read(inode);
  197. end_index = (i_size - 1) >> PAGE_SHIFT;
  198. if (i_size > 0 && page_index(page) < end_index)
  199. goto out;
  200. end = page_file_offset(page) + ((loff_t)offset+count);
  201. if (i_size >= end)
  202. goto out;
  203. i_size_write(inode, end);
  204. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE;
  205. nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
  206. out:
  207. spin_unlock(&inode->i_lock);
  208. }
  209. /* A writeback failed: mark the page as bad, and invalidate the page cache */
  210. static void nfs_set_pageerror(struct page *page)
  211. {
  212. nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page));
  213. }
  214. /*
  215. * nfs_page_group_search_locked
  216. * @head - head request of page group
  217. * @page_offset - offset into page
  218. *
  219. * Search page group with head @head to find a request that contains the
  220. * page offset @page_offset.
  221. *
  222. * Returns a pointer to the first matching nfs request, or NULL if no
  223. * match is found.
  224. *
  225. * Must be called with the page group lock held
  226. */
  227. static struct nfs_page *
  228. nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset)
  229. {
  230. struct nfs_page *req;
  231. req = head;
  232. do {
  233. if (page_offset >= req->wb_pgbase &&
  234. page_offset < (req->wb_pgbase + req->wb_bytes))
  235. return req;
  236. req = req->wb_this_page;
  237. } while (req != head);
  238. return NULL;
  239. }
  240. /*
  241. * nfs_page_group_covers_page
  242. * @head - head request of page group
  243. *
  244. * Return true if the page group with head @head covers the whole page,
  245. * returns false otherwise
  246. */
  247. static bool nfs_page_group_covers_page(struct nfs_page *req)
  248. {
  249. struct nfs_page *tmp;
  250. unsigned int pos = 0;
  251. unsigned int len = nfs_page_length(req->wb_page);
  252. nfs_page_group_lock(req);
  253. for (;;) {
  254. tmp = nfs_page_group_search_locked(req->wb_head, pos);
  255. if (!tmp)
  256. break;
  257. pos = tmp->wb_pgbase + tmp->wb_bytes;
  258. }
  259. nfs_page_group_unlock(req);
  260. return pos >= len;
  261. }
  262. /* We can set the PG_uptodate flag if we see that a write request
  263. * covers the full page.
  264. */
  265. static void nfs_mark_uptodate(struct nfs_page *req)
  266. {
  267. if (PageUptodate(req->wb_page))
  268. return;
  269. if (!nfs_page_group_covers_page(req))
  270. return;
  271. SetPageUptodate(req->wb_page);
  272. }
  273. static int wb_priority(struct writeback_control *wbc)
  274. {
  275. int ret = 0;
  276. if (wbc->sync_mode == WB_SYNC_ALL)
  277. ret = FLUSH_COND_STABLE;
  278. return ret;
  279. }
  280. /*
  281. * NFS congestion control
  282. */
  283. int nfs_congestion_kb;
  284. #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10))
  285. #define NFS_CONGESTION_OFF_THRESH \
  286. (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2))
  287. static void nfs_set_page_writeback(struct page *page)
  288. {
  289. struct inode *inode = page_file_mapping(page)->host;
  290. struct nfs_server *nfss = NFS_SERVER(inode);
  291. int ret = test_set_page_writeback(page);
  292. WARN_ON_ONCE(ret != 0);
  293. if (atomic_long_inc_return(&nfss->writeback) >
  294. NFS_CONGESTION_ON_THRESH)
  295. set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
  296. }
  297. static void nfs_end_page_writeback(struct nfs_page *req)
  298. {
  299. struct inode *inode = page_file_mapping(req->wb_page)->host;
  300. struct nfs_server *nfss = NFS_SERVER(inode);
  301. bool is_done;
  302. is_done = nfs_page_group_sync_on_bit(req, PG_WB_END);
  303. nfs_unlock_request(req);
  304. if (!is_done)
  305. return;
  306. end_page_writeback(req->wb_page);
  307. if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
  308. clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
  309. }
  310. /*
  311. * nfs_unroll_locks_and_wait - unlock all newly locked reqs and wait on @req
  312. *
  313. * this is a helper function for nfs_lock_and_join_requests
  314. *
  315. * @inode - inode associated with request page group, must be holding inode lock
  316. * @head - head request of page group, must be holding head lock
  317. * @req - request that couldn't lock and needs to wait on the req bit lock
  318. *
  319. * NOTE: this must be called holding page_group bit lock
  320. * which will be released before returning.
  321. *
  322. * returns 0 on success, < 0 on error.
  323. */
  324. static void
  325. nfs_unroll_locks(struct inode *inode, struct nfs_page *head,
  326. struct nfs_page *req)
  327. {
  328. struct nfs_page *tmp;
  329. /* relinquish all the locks successfully grabbed this run */
  330. for (tmp = head->wb_this_page ; tmp != req; tmp = tmp->wb_this_page) {
  331. if (!kref_read(&tmp->wb_kref))
  332. continue;
  333. nfs_unlock_and_release_request(tmp);
  334. }
  335. }
  336. /*
  337. * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests
  338. *
  339. * @destroy_list - request list (using wb_this_page) terminated by @old_head
  340. * @old_head - the old head of the list
  341. *
  342. * All subrequests must be locked and removed from all lists, so at this point
  343. * they are only "active" in this function, and possibly in nfs_wait_on_request
  344. * with a reference held by some other context.
  345. */
  346. static void
  347. nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list,
  348. struct nfs_page *old_head,
  349. struct inode *inode)
  350. {
  351. while (destroy_list) {
  352. struct nfs_page *subreq = destroy_list;
  353. destroy_list = (subreq->wb_this_page == old_head) ?
  354. NULL : subreq->wb_this_page;
  355. WARN_ON_ONCE(old_head != subreq->wb_head);
  356. /* make sure old group is not used */
  357. subreq->wb_this_page = subreq;
  358. clear_bit(PG_REMOVE, &subreq->wb_flags);
  359. /* Note: races with nfs_page_group_destroy() */
  360. if (!kref_read(&subreq->wb_kref)) {
  361. /* Check if we raced with nfs_page_group_destroy() */
  362. if (test_and_clear_bit(PG_TEARDOWN, &subreq->wb_flags))
  363. nfs_free_request(subreq);
  364. continue;
  365. }
  366. subreq->wb_head = subreq;
  367. if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags)) {
  368. nfs_release_request(subreq);
  369. atomic_long_dec(&NFS_I(inode)->nrequests);
  370. }
  371. /* subreq is now totally disconnected from page group or any
  372. * write / commit lists. last chance to wake any waiters */
  373. nfs_unlock_and_release_request(subreq);
  374. }
  375. }
  376. /*
  377. * nfs_lock_and_join_requests - join all subreqs to the head req and return
  378. * a locked reference, cancelling any pending
  379. * operations for this page.
  380. *
  381. * @page - the page used to lookup the "page group" of nfs_page structures
  382. *
  383. * This function joins all sub requests to the head request by first
  384. * locking all requests in the group, cancelling any pending operations
  385. * and finally updating the head request to cover the whole range covered by
  386. * the (former) group. All subrequests are removed from any write or commit
  387. * lists, unlinked from the group and destroyed.
  388. *
  389. * Returns a locked, referenced pointer to the head request - which after
  390. * this call is guaranteed to be the only request associated with the page.
  391. * Returns NULL if no requests are found for @page, or a ERR_PTR if an
  392. * error was encountered.
  393. */
  394. static struct nfs_page *
  395. nfs_lock_and_join_requests(struct page *page)
  396. {
  397. struct inode *inode = page_file_mapping(page)->host;
  398. struct nfs_page *head, *subreq;
  399. struct nfs_page *destroy_list = NULL;
  400. unsigned int total_bytes;
  401. int ret;
  402. try_again:
  403. /*
  404. * A reference is taken only on the head request which acts as a
  405. * reference to the whole page group - the group will not be destroyed
  406. * until the head reference is released.
  407. */
  408. head = nfs_page_find_head_request(page);
  409. if (!head)
  410. return NULL;
  411. /* lock the page head first in order to avoid an ABBA inefficiency */
  412. if (!nfs_lock_request(head)) {
  413. ret = nfs_wait_on_request(head);
  414. nfs_release_request(head);
  415. if (ret < 0)
  416. return ERR_PTR(ret);
  417. goto try_again;
  418. }
  419. /* Ensure that nobody removed the request before we locked it */
  420. if (head != nfs_page_private_request(page) && !PageSwapCache(page)) {
  421. nfs_unlock_and_release_request(head);
  422. goto try_again;
  423. }
  424. ret = nfs_page_group_lock(head);
  425. if (ret < 0)
  426. goto release_request;
  427. /* lock each request in the page group */
  428. total_bytes = head->wb_bytes;
  429. for (subreq = head->wb_this_page; subreq != head;
  430. subreq = subreq->wb_this_page) {
  431. if (!kref_get_unless_zero(&subreq->wb_kref)) {
  432. if (subreq->wb_offset == head->wb_offset + total_bytes)
  433. total_bytes += subreq->wb_bytes;
  434. continue;
  435. }
  436. while (!nfs_lock_request(subreq)) {
  437. /*
  438. * Unlock page to allow nfs_page_group_sync_on_bit()
  439. * to succeed
  440. */
  441. nfs_page_group_unlock(head);
  442. ret = nfs_wait_on_request(subreq);
  443. if (!ret)
  444. ret = nfs_page_group_lock(head);
  445. if (ret < 0) {
  446. nfs_unroll_locks(inode, head, subreq);
  447. nfs_release_request(subreq);
  448. goto release_request;
  449. }
  450. }
  451. /*
  452. * Subrequests are always contiguous, non overlapping
  453. * and in order - but may be repeated (mirrored writes).
  454. */
  455. if (subreq->wb_offset == (head->wb_offset + total_bytes)) {
  456. /* keep track of how many bytes this group covers */
  457. total_bytes += subreq->wb_bytes;
  458. } else if (WARN_ON_ONCE(subreq->wb_offset < head->wb_offset ||
  459. ((subreq->wb_offset + subreq->wb_bytes) >
  460. (head->wb_offset + total_bytes)))) {
  461. nfs_page_group_unlock(head);
  462. nfs_unroll_locks(inode, head, subreq);
  463. nfs_unlock_and_release_request(subreq);
  464. ret = -EIO;
  465. goto release_request;
  466. }
  467. }
  468. /* Now that all requests are locked, make sure they aren't on any list.
  469. * Commit list removal accounting is done after locks are dropped */
  470. subreq = head;
  471. do {
  472. nfs_clear_request_commit(subreq);
  473. subreq = subreq->wb_this_page;
  474. } while (subreq != head);
  475. /* unlink subrequests from head, destroy them later */
  476. if (head->wb_this_page != head) {
  477. /* destroy list will be terminated by head */
  478. destroy_list = head->wb_this_page;
  479. head->wb_this_page = head;
  480. /* change head request to cover whole range that
  481. * the former page group covered */
  482. head->wb_bytes = total_bytes;
  483. }
  484. /* Postpone destruction of this request */
  485. if (test_and_clear_bit(PG_REMOVE, &head->wb_flags)) {
  486. set_bit(PG_INODE_REF, &head->wb_flags);
  487. kref_get(&head->wb_kref);
  488. atomic_long_inc(&NFS_I(inode)->nrequests);
  489. }
  490. nfs_page_group_unlock(head);
  491. nfs_destroy_unlinked_subrequests(destroy_list, head, inode);
  492. /* Did we lose a race with nfs_inode_remove_request()? */
  493. if (!(PagePrivate(page) || PageSwapCache(page))) {
  494. nfs_unlock_and_release_request(head);
  495. return NULL;
  496. }
  497. /* still holds ref on head from nfs_page_find_head_request
  498. * and still has lock on head from lock loop */
  499. return head;
  500. release_request:
  501. nfs_unlock_and_release_request(head);
  502. return ERR_PTR(ret);
  503. }
  504. static void nfs_write_error_remove_page(struct nfs_page *req)
  505. {
  506. nfs_end_page_writeback(req);
  507. generic_error_remove_page(page_file_mapping(req->wb_page),
  508. req->wb_page);
  509. nfs_release_request(req);
  510. }
  511. static bool
  512. nfs_error_is_fatal_on_server(int err)
  513. {
  514. switch (err) {
  515. case 0:
  516. case -ERESTARTSYS:
  517. case -EINTR:
  518. return false;
  519. }
  520. return nfs_error_is_fatal(err);
  521. }
  522. /*
  523. * Find an associated nfs write request, and prepare to flush it out
  524. * May return an error if the user signalled nfs_wait_on_request().
  525. */
  526. static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
  527. struct page *page)
  528. {
  529. struct nfs_page *req;
  530. int ret = 0;
  531. req = nfs_lock_and_join_requests(page);
  532. if (!req)
  533. goto out;
  534. ret = PTR_ERR(req);
  535. if (IS_ERR(req))
  536. goto out;
  537. nfs_set_page_writeback(page);
  538. WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
  539. ret = 0;
  540. /* If there is a fatal error that covers this write, just exit */
  541. if (nfs_error_is_fatal_on_server(req->wb_context->error))
  542. goto out_launder;
  543. if (!nfs_pageio_add_request(pgio, req)) {
  544. ret = pgio->pg_error;
  545. /*
  546. * Remove the problematic req upon fatal errors on the server
  547. */
  548. if (nfs_error_is_fatal(ret)) {
  549. nfs_context_set_write_error(req->wb_context, ret);
  550. if (nfs_error_is_fatal_on_server(ret))
  551. goto out_launder;
  552. }
  553. nfs_redirty_request(req);
  554. ret = -EAGAIN;
  555. } else
  556. nfs_add_stats(page_file_mapping(page)->host,
  557. NFSIOS_WRITEPAGES, 1);
  558. out:
  559. return ret;
  560. out_launder:
  561. nfs_write_error_remove_page(req);
  562. return ret;
  563. }
  564. static int nfs_do_writepage(struct page *page, struct writeback_control *wbc,
  565. struct nfs_pageio_descriptor *pgio)
  566. {
  567. int ret;
  568. nfs_pageio_cond_complete(pgio, page_index(page));
  569. ret = nfs_page_async_flush(pgio, page);
  570. if (ret == -EAGAIN) {
  571. redirty_page_for_writepage(wbc, page);
  572. ret = 0;
  573. }
  574. return ret;
  575. }
  576. /*
  577. * Write an mmapped page to the server.
  578. */
  579. static int nfs_writepage_locked(struct page *page,
  580. struct writeback_control *wbc)
  581. {
  582. struct nfs_pageio_descriptor pgio;
  583. struct inode *inode = page_file_mapping(page)->host;
  584. int err;
  585. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
  586. nfs_pageio_init_write(&pgio, inode, 0,
  587. false, &nfs_async_write_completion_ops);
  588. err = nfs_do_writepage(page, wbc, &pgio);
  589. nfs_pageio_complete(&pgio);
  590. if (err < 0)
  591. return err;
  592. if (pgio.pg_error < 0)
  593. return pgio.pg_error;
  594. return 0;
  595. }
  596. int nfs_writepage(struct page *page, struct writeback_control *wbc)
  597. {
  598. int ret;
  599. ret = nfs_writepage_locked(page, wbc);
  600. unlock_page(page);
  601. return ret;
  602. }
  603. static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
  604. {
  605. int ret;
  606. ret = nfs_do_writepage(page, wbc, data);
  607. unlock_page(page);
  608. return ret;
  609. }
  610. static void nfs_io_completion_commit(void *inode)
  611. {
  612. nfs_commit_inode(inode, 0);
  613. }
  614. int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  615. {
  616. struct inode *inode = mapping->host;
  617. struct nfs_pageio_descriptor pgio;
  618. struct nfs_io_completion *ioc = nfs_io_completion_alloc(GFP_NOFS);
  619. int err;
  620. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
  621. if (ioc)
  622. nfs_io_completion_init(ioc, nfs_io_completion_commit, inode);
  623. nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
  624. &nfs_async_write_completion_ops);
  625. pgio.pg_io_completion = ioc;
  626. err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
  627. nfs_pageio_complete(&pgio);
  628. nfs_io_completion_put(ioc);
  629. if (err < 0)
  630. goto out_err;
  631. err = pgio.pg_error;
  632. if (err < 0)
  633. goto out_err;
  634. return 0;
  635. out_err:
  636. return err;
  637. }
  638. /*
  639. * Insert a write request into an inode
  640. */
  641. static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
  642. {
  643. struct address_space *mapping = page_file_mapping(req->wb_page);
  644. struct nfs_inode *nfsi = NFS_I(inode);
  645. WARN_ON_ONCE(req->wb_this_page != req);
  646. /* Lock the request! */
  647. nfs_lock_request(req);
  648. /*
  649. * Swap-space should not get truncated. Hence no need to plug the race
  650. * with invalidate/truncate.
  651. */
  652. spin_lock(&mapping->private_lock);
  653. if (!nfs_have_writebacks(inode) &&
  654. NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
  655. inode_inc_iversion_raw(inode);
  656. if (likely(!PageSwapCache(req->wb_page))) {
  657. set_bit(PG_MAPPED, &req->wb_flags);
  658. SetPagePrivate(req->wb_page);
  659. set_page_private(req->wb_page, (unsigned long)req);
  660. }
  661. spin_unlock(&mapping->private_lock);
  662. atomic_long_inc(&nfsi->nrequests);
  663. /* this a head request for a page group - mark it as having an
  664. * extra reference so sub groups can follow suit.
  665. * This flag also informs pgio layer when to bump nrequests when
  666. * adding subrequests. */
  667. WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
  668. kref_get(&req->wb_kref);
  669. }
  670. /*
  671. * Remove a write request from an inode
  672. */
  673. static void nfs_inode_remove_request(struct nfs_page *req)
  674. {
  675. struct address_space *mapping = page_file_mapping(req->wb_page);
  676. struct inode *inode = mapping->host;
  677. struct nfs_inode *nfsi = NFS_I(inode);
  678. struct nfs_page *head;
  679. atomic_long_dec(&nfsi->nrequests);
  680. if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
  681. head = req->wb_head;
  682. spin_lock(&mapping->private_lock);
  683. if (likely(head->wb_page && !PageSwapCache(head->wb_page))) {
  684. set_page_private(head->wb_page, 0);
  685. ClearPagePrivate(head->wb_page);
  686. clear_bit(PG_MAPPED, &head->wb_flags);
  687. }
  688. spin_unlock(&mapping->private_lock);
  689. }
  690. if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags))
  691. nfs_release_request(req);
  692. }
  693. static void
  694. nfs_mark_request_dirty(struct nfs_page *req)
  695. {
  696. if (req->wb_page)
  697. __set_page_dirty_nobuffers(req->wb_page);
  698. }
  699. /*
  700. * nfs_page_search_commits_for_head_request_locked
  701. *
  702. * Search through commit lists on @inode for the head request for @page.
  703. * Must be called while holding the inode (which is cinfo) lock.
  704. *
  705. * Returns the head request if found, or NULL if not found.
  706. */
  707. static struct nfs_page *
  708. nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
  709. struct page *page)
  710. {
  711. struct nfs_page *freq, *t;
  712. struct nfs_commit_info cinfo;
  713. struct inode *inode = &nfsi->vfs_inode;
  714. nfs_init_cinfo_from_inode(&cinfo, inode);
  715. /* search through pnfs commit lists */
  716. freq = pnfs_search_commit_reqs(inode, &cinfo, page);
  717. if (freq)
  718. return freq->wb_head;
  719. /* Linearly search the commit list for the correct request */
  720. list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) {
  721. if (freq->wb_page == page)
  722. return freq->wb_head;
  723. }
  724. return NULL;
  725. }
  726. /**
  727. * nfs_request_add_commit_list_locked - add request to a commit list
  728. * @req: pointer to a struct nfs_page
  729. * @dst: commit list head
  730. * @cinfo: holds list lock and accounting info
  731. *
  732. * This sets the PG_CLEAN bit, updates the cinfo count of
  733. * number of outstanding requests requiring a commit as well as
  734. * the MM page stats.
  735. *
  736. * The caller must hold NFS_I(cinfo->inode)->commit_mutex, and the
  737. * nfs_page lock.
  738. */
  739. void
  740. nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst,
  741. struct nfs_commit_info *cinfo)
  742. {
  743. set_bit(PG_CLEAN, &req->wb_flags);
  744. nfs_list_add_request(req, dst);
  745. atomic_long_inc(&cinfo->mds->ncommit);
  746. }
  747. EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked);
  748. /**
  749. * nfs_request_add_commit_list - add request to a commit list
  750. * @req: pointer to a struct nfs_page
  751. * @dst: commit list head
  752. * @cinfo: holds list lock and accounting info
  753. *
  754. * This sets the PG_CLEAN bit, updates the cinfo count of
  755. * number of outstanding requests requiring a commit as well as
  756. * the MM page stats.
  757. *
  758. * The caller must _not_ hold the cinfo->lock, but must be
  759. * holding the nfs_page lock.
  760. */
  761. void
  762. nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo)
  763. {
  764. mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
  765. nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo);
  766. mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
  767. if (req->wb_page)
  768. nfs_mark_page_unstable(req->wb_page, cinfo);
  769. }
  770. EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
  771. /**
  772. * nfs_request_remove_commit_list - Remove request from a commit list
  773. * @req: pointer to a nfs_page
  774. * @cinfo: holds list lock and accounting info
  775. *
  776. * This clears the PG_CLEAN bit, and updates the cinfo's count of
  777. * number of outstanding requests requiring a commit
  778. * It does not update the MM page stats.
  779. *
  780. * The caller _must_ hold the cinfo->lock and the nfs_page lock.
  781. */
  782. void
  783. nfs_request_remove_commit_list(struct nfs_page *req,
  784. struct nfs_commit_info *cinfo)
  785. {
  786. if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
  787. return;
  788. nfs_list_remove_request(req);
  789. atomic_long_dec(&cinfo->mds->ncommit);
  790. }
  791. EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
  792. static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
  793. struct inode *inode)
  794. {
  795. cinfo->inode = inode;
  796. cinfo->mds = &NFS_I(inode)->commit_info;
  797. cinfo->ds = pnfs_get_ds_info(inode);
  798. cinfo->dreq = NULL;
  799. cinfo->completion_ops = &nfs_commit_completion_ops;
  800. }
  801. void nfs_init_cinfo(struct nfs_commit_info *cinfo,
  802. struct inode *inode,
  803. struct nfs_direct_req *dreq)
  804. {
  805. if (dreq)
  806. nfs_init_cinfo_from_dreq(cinfo, dreq);
  807. else
  808. nfs_init_cinfo_from_inode(cinfo, inode);
  809. }
  810. EXPORT_SYMBOL_GPL(nfs_init_cinfo);
  811. /*
  812. * Add a request to the inode's commit list.
  813. */
  814. void
  815. nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
  816. struct nfs_commit_info *cinfo, u32 ds_commit_idx)
  817. {
  818. if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx))
  819. return;
  820. nfs_request_add_commit_list(req, cinfo);
  821. }
  822. static void
  823. nfs_clear_page_commit(struct page *page)
  824. {
  825. dec_node_page_state(page, NR_UNSTABLE_NFS);
  826. dec_wb_stat(&inode_to_bdi(page_file_mapping(page)->host)->wb,
  827. WB_RECLAIMABLE);
  828. }
  829. /* Called holding the request lock on @req */
  830. static void
  831. nfs_clear_request_commit(struct nfs_page *req)
  832. {
  833. if (test_bit(PG_CLEAN, &req->wb_flags)) {
  834. struct inode *inode = d_inode(req->wb_context->dentry);
  835. struct nfs_commit_info cinfo;
  836. nfs_init_cinfo_from_inode(&cinfo, inode);
  837. mutex_lock(&NFS_I(inode)->commit_mutex);
  838. if (!pnfs_clear_request_commit(req, &cinfo)) {
  839. nfs_request_remove_commit_list(req, &cinfo);
  840. }
  841. mutex_unlock(&NFS_I(inode)->commit_mutex);
  842. nfs_clear_page_commit(req->wb_page);
  843. }
  844. }
  845. int nfs_write_need_commit(struct nfs_pgio_header *hdr)
  846. {
  847. if (hdr->verf.committed == NFS_DATA_SYNC)
  848. return hdr->lseg == NULL;
  849. return hdr->verf.committed != NFS_FILE_SYNC;
  850. }
  851. static void nfs_async_write_init(struct nfs_pgio_header *hdr)
  852. {
  853. nfs_io_completion_get(hdr->io_completion);
  854. }
  855. static void nfs_write_completion(struct nfs_pgio_header *hdr)
  856. {
  857. struct nfs_commit_info cinfo;
  858. unsigned long bytes = 0;
  859. if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
  860. goto out;
  861. nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
  862. while (!list_empty(&hdr->pages)) {
  863. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  864. bytes += req->wb_bytes;
  865. nfs_list_remove_request(req);
  866. if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
  867. (hdr->good_bytes < bytes)) {
  868. nfs_set_pageerror(req->wb_page);
  869. nfs_context_set_write_error(req->wb_context, hdr->error);
  870. goto remove_req;
  871. }
  872. if (nfs_write_need_commit(hdr)) {
  873. memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
  874. nfs_mark_request_commit(req, hdr->lseg, &cinfo,
  875. hdr->pgio_mirror_idx);
  876. goto next;
  877. }
  878. remove_req:
  879. nfs_inode_remove_request(req);
  880. next:
  881. nfs_end_page_writeback(req);
  882. nfs_release_request(req);
  883. }
  884. out:
  885. nfs_io_completion_put(hdr->io_completion);
  886. hdr->release(hdr);
  887. }
  888. unsigned long
  889. nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
  890. {
  891. return atomic_long_read(&cinfo->mds->ncommit);
  892. }
  893. /* NFS_I(cinfo->inode)->commit_mutex held by caller */
  894. int
  895. nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
  896. struct nfs_commit_info *cinfo, int max)
  897. {
  898. struct nfs_page *req, *tmp;
  899. int ret = 0;
  900. restart:
  901. list_for_each_entry_safe(req, tmp, src, wb_list) {
  902. kref_get(&req->wb_kref);
  903. if (!nfs_lock_request(req)) {
  904. int status;
  905. /* Prevent deadlock with nfs_lock_and_join_requests */
  906. if (!list_empty(dst)) {
  907. nfs_release_request(req);
  908. continue;
  909. }
  910. /* Ensure we make progress to prevent livelock */
  911. mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
  912. status = nfs_wait_on_request(req);
  913. nfs_release_request(req);
  914. mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
  915. if (status < 0)
  916. break;
  917. goto restart;
  918. }
  919. nfs_request_remove_commit_list(req, cinfo);
  920. clear_bit(PG_COMMIT_TO_DS, &req->wb_flags);
  921. nfs_list_add_request(req, dst);
  922. ret++;
  923. if ((ret == max) && !cinfo->dreq)
  924. break;
  925. cond_resched();
  926. }
  927. return ret;
  928. }
  929. EXPORT_SYMBOL_GPL(nfs_scan_commit_list);
  930. /*
  931. * nfs_scan_commit - Scan an inode for commit requests
  932. * @inode: NFS inode to scan
  933. * @dst: mds destination list
  934. * @cinfo: mds and ds lists of reqs ready to commit
  935. *
  936. * Moves requests from the inode's 'commit' request list.
  937. * The requests are *not* checked to ensure that they form a contiguous set.
  938. */
  939. int
  940. nfs_scan_commit(struct inode *inode, struct list_head *dst,
  941. struct nfs_commit_info *cinfo)
  942. {
  943. int ret = 0;
  944. if (!atomic_long_read(&cinfo->mds->ncommit))
  945. return 0;
  946. mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
  947. if (atomic_long_read(&cinfo->mds->ncommit) > 0) {
  948. const int max = INT_MAX;
  949. ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
  950. cinfo, max);
  951. ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
  952. }
  953. mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
  954. return ret;
  955. }
  956. /*
  957. * Search for an existing write request, and attempt to update
  958. * it to reflect a new dirty region on a given page.
  959. *
  960. * If the attempt fails, then the existing request is flushed out
  961. * to disk.
  962. */
  963. static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
  964. struct page *page,
  965. unsigned int offset,
  966. unsigned int bytes)
  967. {
  968. struct nfs_page *req;
  969. unsigned int rqend;
  970. unsigned int end;
  971. int error;
  972. end = offset + bytes;
  973. req = nfs_lock_and_join_requests(page);
  974. if (IS_ERR_OR_NULL(req))
  975. return req;
  976. rqend = req->wb_offset + req->wb_bytes;
  977. /*
  978. * Tell the caller to flush out the request if
  979. * the offsets are non-contiguous.
  980. * Note: nfs_flush_incompatible() will already
  981. * have flushed out requests having wrong owners.
  982. */
  983. if (offset > rqend || end < req->wb_offset)
  984. goto out_flushme;
  985. /* Okay, the request matches. Update the region */
  986. if (offset < req->wb_offset) {
  987. req->wb_offset = offset;
  988. req->wb_pgbase = offset;
  989. }
  990. if (end > rqend)
  991. req->wb_bytes = end - req->wb_offset;
  992. else
  993. req->wb_bytes = rqend - req->wb_offset;
  994. return req;
  995. out_flushme:
  996. /*
  997. * Note: we mark the request dirty here because
  998. * nfs_lock_and_join_requests() cannot preserve
  999. * commit flags, so we have to replay the write.
  1000. */
  1001. nfs_mark_request_dirty(req);
  1002. nfs_unlock_and_release_request(req);
  1003. error = nfs_wb_page(inode, page);
  1004. return (error < 0) ? ERR_PTR(error) : NULL;
  1005. }
  1006. /*
  1007. * Try to update an existing write request, or create one if there is none.
  1008. *
  1009. * Note: Should always be called with the Page Lock held to prevent races
  1010. * if we have to add a new request. Also assumes that the caller has
  1011. * already called nfs_flush_incompatible() if necessary.
  1012. */
  1013. static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
  1014. struct page *page, unsigned int offset, unsigned int bytes)
  1015. {
  1016. struct inode *inode = page_file_mapping(page)->host;
  1017. struct nfs_page *req;
  1018. req = nfs_try_to_update_request(inode, page, offset, bytes);
  1019. if (req != NULL)
  1020. goto out;
  1021. req = nfs_create_request(ctx, page, NULL, offset, bytes);
  1022. if (IS_ERR(req))
  1023. goto out;
  1024. nfs_inode_add_request(inode, req);
  1025. out:
  1026. return req;
  1027. }
  1028. static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
  1029. unsigned int offset, unsigned int count)
  1030. {
  1031. struct nfs_page *req;
  1032. req = nfs_setup_write_request(ctx, page, offset, count);
  1033. if (IS_ERR(req))
  1034. return PTR_ERR(req);
  1035. /* Update file length */
  1036. nfs_grow_file(page, offset, count);
  1037. nfs_mark_uptodate(req);
  1038. nfs_mark_request_dirty(req);
  1039. nfs_unlock_and_release_request(req);
  1040. return 0;
  1041. }
  1042. int nfs_flush_incompatible(struct file *file, struct page *page)
  1043. {
  1044. struct nfs_open_context *ctx = nfs_file_open_context(file);
  1045. struct nfs_lock_context *l_ctx;
  1046. struct file_lock_context *flctx = file_inode(file)->i_flctx;
  1047. struct nfs_page *req;
  1048. int do_flush, status;
  1049. /*
  1050. * Look for a request corresponding to this page. If there
  1051. * is one, and it belongs to another file, we flush it out
  1052. * before we try to copy anything into the page. Do this
  1053. * due to the lack of an ACCESS-type call in NFSv2.
  1054. * Also do the same if we find a request from an existing
  1055. * dropped page.
  1056. */
  1057. do {
  1058. req = nfs_page_find_head_request(page);
  1059. if (req == NULL)
  1060. return 0;
  1061. l_ctx = req->wb_lock_context;
  1062. do_flush = req->wb_page != page ||
  1063. !nfs_match_open_context(req->wb_context, ctx);
  1064. if (l_ctx && flctx &&
  1065. !(list_empty_careful(&flctx->flc_posix) &&
  1066. list_empty_careful(&flctx->flc_flock))) {
  1067. do_flush |= l_ctx->lockowner != current->files;
  1068. }
  1069. nfs_release_request(req);
  1070. if (!do_flush)
  1071. return 0;
  1072. status = nfs_wb_page(page_file_mapping(page)->host, page);
  1073. } while (status == 0);
  1074. return status;
  1075. }
  1076. /*
  1077. * Avoid buffered writes when a open context credential's key would
  1078. * expire soon.
  1079. *
  1080. * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
  1081. *
  1082. * Return 0 and set a credential flag which triggers the inode to flush
  1083. * and performs NFS_FILE_SYNC writes if the key will expired within
  1084. * RPC_KEY_EXPIRE_TIMEO.
  1085. */
  1086. int
  1087. nfs_key_timeout_notify(struct file *filp, struct inode *inode)
  1088. {
  1089. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  1090. struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
  1091. return rpcauth_key_timeout_notify(auth, ctx->cred);
  1092. }
  1093. /*
  1094. * Test if the open context credential key is marked to expire soon.
  1095. */
  1096. bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode)
  1097. {
  1098. struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
  1099. return rpcauth_cred_key_to_expire(auth, ctx->cred);
  1100. }
  1101. /*
  1102. * If the page cache is marked as unsafe or invalid, then we can't rely on
  1103. * the PageUptodate() flag. In this case, we will need to turn off
  1104. * write optimisations that depend on the page contents being correct.
  1105. */
  1106. static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
  1107. {
  1108. struct nfs_inode *nfsi = NFS_I(inode);
  1109. if (nfs_have_delegated_attributes(inode))
  1110. goto out;
  1111. if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  1112. return false;
  1113. smp_rmb();
  1114. if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
  1115. return false;
  1116. out:
  1117. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  1118. return false;
  1119. return PageUptodate(page) != 0;
  1120. }
  1121. static bool
  1122. is_whole_file_wrlock(struct file_lock *fl)
  1123. {
  1124. return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX &&
  1125. fl->fl_type == F_WRLCK;
  1126. }
  1127. /* If we know the page is up to date, and we're not using byte range locks (or
  1128. * if we have the whole file locked for writing), it may be more efficient to
  1129. * extend the write to cover the entire page in order to avoid fragmentation
  1130. * inefficiencies.
  1131. *
  1132. * If the file is opened for synchronous writes then we can just skip the rest
  1133. * of the checks.
  1134. */
  1135. static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
  1136. {
  1137. int ret;
  1138. struct file_lock_context *flctx = inode->i_flctx;
  1139. struct file_lock *fl;
  1140. if (file->f_flags & O_DSYNC)
  1141. return 0;
  1142. if (!nfs_write_pageuptodate(page, inode))
  1143. return 0;
  1144. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
  1145. return 1;
  1146. if (!flctx || (list_empty_careful(&flctx->flc_flock) &&
  1147. list_empty_careful(&flctx->flc_posix)))
  1148. return 1;
  1149. /* Check to see if there are whole file write locks */
  1150. ret = 0;
  1151. spin_lock(&flctx->flc_lock);
  1152. if (!list_empty(&flctx->flc_posix)) {
  1153. fl = list_first_entry(&flctx->flc_posix, struct file_lock,
  1154. fl_list);
  1155. if (is_whole_file_wrlock(fl))
  1156. ret = 1;
  1157. } else if (!list_empty(&flctx->flc_flock)) {
  1158. fl = list_first_entry(&flctx->flc_flock, struct file_lock,
  1159. fl_list);
  1160. if (fl->fl_type == F_WRLCK)
  1161. ret = 1;
  1162. }
  1163. spin_unlock(&flctx->flc_lock);
  1164. return ret;
  1165. }
  1166. /*
  1167. * Update and possibly write a cached page of an NFS file.
  1168. *
  1169. * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
  1170. * things with a page scheduled for an RPC call (e.g. invalidate it).
  1171. */
  1172. int nfs_updatepage(struct file *file, struct page *page,
  1173. unsigned int offset, unsigned int count)
  1174. {
  1175. struct nfs_open_context *ctx = nfs_file_open_context(file);
  1176. struct inode *inode = page_file_mapping(page)->host;
  1177. int status = 0;
  1178. nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
  1179. dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n",
  1180. file, count, (long long)(page_file_offset(page) + offset));
  1181. if (!count)
  1182. goto out;
  1183. if (nfs_can_extend_write(file, page, inode)) {
  1184. count = max(count + offset, nfs_page_length(page));
  1185. offset = 0;
  1186. }
  1187. status = nfs_writepage_setup(ctx, page, offset, count);
  1188. if (status < 0)
  1189. nfs_set_pageerror(page);
  1190. else
  1191. __set_page_dirty_nobuffers(page);
  1192. out:
  1193. dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
  1194. status, (long long)i_size_read(inode));
  1195. return status;
  1196. }
  1197. static int flush_task_priority(int how)
  1198. {
  1199. switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
  1200. case FLUSH_HIGHPRI:
  1201. return RPC_PRIORITY_HIGH;
  1202. case FLUSH_LOWPRI:
  1203. return RPC_PRIORITY_LOW;
  1204. }
  1205. return RPC_PRIORITY_NORMAL;
  1206. }
  1207. static void nfs_initiate_write(struct nfs_pgio_header *hdr,
  1208. struct rpc_message *msg,
  1209. const struct nfs_rpc_ops *rpc_ops,
  1210. struct rpc_task_setup *task_setup_data, int how)
  1211. {
  1212. int priority = flush_task_priority(how);
  1213. task_setup_data->priority = priority;
  1214. rpc_ops->write_setup(hdr, msg, &task_setup_data->rpc_client);
  1215. trace_nfs_initiate_write(hdr->inode, hdr->io_start, hdr->good_bytes,
  1216. hdr->args.stable);
  1217. }
  1218. /* If a nfs_flush_* function fails, it should remove reqs from @head and
  1219. * call this on each, which will prepare them to be retried on next
  1220. * writeback using standard nfs.
  1221. */
  1222. static void nfs_redirty_request(struct nfs_page *req)
  1223. {
  1224. nfs_mark_request_dirty(req);
  1225. set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
  1226. nfs_end_page_writeback(req);
  1227. nfs_release_request(req);
  1228. }
  1229. static void nfs_async_write_error(struct list_head *head)
  1230. {
  1231. struct nfs_page *req;
  1232. while (!list_empty(head)) {
  1233. req = nfs_list_entry(head->next);
  1234. nfs_list_remove_request(req);
  1235. nfs_redirty_request(req);
  1236. }
  1237. }
  1238. static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
  1239. {
  1240. nfs_async_write_error(&hdr->pages);
  1241. }
  1242. static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
  1243. .init_hdr = nfs_async_write_init,
  1244. .error_cleanup = nfs_async_write_error,
  1245. .completion = nfs_write_completion,
  1246. .reschedule_io = nfs_async_write_reschedule_io,
  1247. };
  1248. void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
  1249. struct inode *inode, int ioflags, bool force_mds,
  1250. const struct nfs_pgio_completion_ops *compl_ops)
  1251. {
  1252. struct nfs_server *server = NFS_SERVER(inode);
  1253. const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
  1254. #ifdef CONFIG_NFS_V4_1
  1255. if (server->pnfs_curr_ld && !force_mds)
  1256. pg_ops = server->pnfs_curr_ld->pg_write_ops;
  1257. #endif
  1258. nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
  1259. server->wsize, ioflags);
  1260. }
  1261. EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
  1262. void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
  1263. {
  1264. struct nfs_pgio_mirror *mirror;
  1265. if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
  1266. pgio->pg_ops->pg_cleanup(pgio);
  1267. pgio->pg_ops = &nfs_pgio_rw_ops;
  1268. nfs_pageio_stop_mirroring(pgio);
  1269. mirror = &pgio->pg_mirrors[0];
  1270. mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
  1271. }
  1272. EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
  1273. void nfs_commit_prepare(struct rpc_task *task, void *calldata)
  1274. {
  1275. struct nfs_commit_data *data = calldata;
  1276. NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
  1277. }
  1278. /*
  1279. * Special version of should_remove_suid() that ignores capabilities.
  1280. */
  1281. static int nfs_should_remove_suid(const struct inode *inode)
  1282. {
  1283. umode_t mode = inode->i_mode;
  1284. int kill = 0;
  1285. /* suid always must be killed */
  1286. if (unlikely(mode & S_ISUID))
  1287. kill = ATTR_KILL_SUID;
  1288. /*
  1289. * sgid without any exec bits is just a mandatory locking mark; leave
  1290. * it alone. If some exec bits are set, it's a real sgid; kill it.
  1291. */
  1292. if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
  1293. kill |= ATTR_KILL_SGID;
  1294. if (unlikely(kill && S_ISREG(mode)))
  1295. return kill;
  1296. return 0;
  1297. }
  1298. static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr,
  1299. struct nfs_fattr *fattr)
  1300. {
  1301. struct nfs_pgio_args *argp = &hdr->args;
  1302. struct nfs_pgio_res *resp = &hdr->res;
  1303. u64 size = argp->offset + resp->count;
  1304. if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
  1305. fattr->size = size;
  1306. if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) {
  1307. fattr->valid &= ~NFS_ATTR_FATTR_SIZE;
  1308. return;
  1309. }
  1310. if (size != fattr->size)
  1311. return;
  1312. /* Set attribute barrier */
  1313. nfs_fattr_set_barrier(fattr);
  1314. /* ...and update size */
  1315. fattr->valid |= NFS_ATTR_FATTR_SIZE;
  1316. }
  1317. void nfs_writeback_update_inode(struct nfs_pgio_header *hdr)
  1318. {
  1319. struct nfs_fattr *fattr = &hdr->fattr;
  1320. struct inode *inode = hdr->inode;
  1321. spin_lock(&inode->i_lock);
  1322. nfs_writeback_check_extend(hdr, fattr);
  1323. nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
  1324. spin_unlock(&inode->i_lock);
  1325. }
  1326. EXPORT_SYMBOL_GPL(nfs_writeback_update_inode);
  1327. /*
  1328. * This function is called when the WRITE call is complete.
  1329. */
  1330. static int nfs_writeback_done(struct rpc_task *task,
  1331. struct nfs_pgio_header *hdr,
  1332. struct inode *inode)
  1333. {
  1334. int status;
  1335. /*
  1336. * ->write_done will attempt to use post-op attributes to detect
  1337. * conflicting writes by other clients. A strict interpretation
  1338. * of close-to-open would allow us to continue caching even if
  1339. * another writer had changed the file, but some applications
  1340. * depend on tighter cache coherency when writing.
  1341. */
  1342. status = NFS_PROTO(inode)->write_done(task, hdr);
  1343. if (status != 0)
  1344. return status;
  1345. nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
  1346. trace_nfs_writeback_done(inode, task->tk_status,
  1347. hdr->args.offset, hdr->res.verf);
  1348. if (hdr->res.verf->committed < hdr->args.stable &&
  1349. task->tk_status >= 0) {
  1350. /* We tried a write call, but the server did not
  1351. * commit data to stable storage even though we
  1352. * requested it.
  1353. * Note: There is a known bug in Tru64 < 5.0 in which
  1354. * the server reports NFS_DATA_SYNC, but performs
  1355. * NFS_FILE_SYNC. We therefore implement this checking
  1356. * as a dprintk() in order to avoid filling syslog.
  1357. */
  1358. static unsigned long complain;
  1359. /* Note this will print the MDS for a DS write */
  1360. if (time_before(complain, jiffies)) {
  1361. dprintk("NFS: faulty NFS server %s:"
  1362. " (committed = %d) != (stable = %d)\n",
  1363. NFS_SERVER(inode)->nfs_client->cl_hostname,
  1364. hdr->res.verf->committed, hdr->args.stable);
  1365. complain = jiffies + 300 * HZ;
  1366. }
  1367. }
  1368. /* Deal with the suid/sgid bit corner case */
  1369. if (nfs_should_remove_suid(inode)) {
  1370. spin_lock(&inode->i_lock);
  1371. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
  1372. spin_unlock(&inode->i_lock);
  1373. }
  1374. return 0;
  1375. }
  1376. /*
  1377. * This function is called when the WRITE call is complete.
  1378. */
  1379. static void nfs_writeback_result(struct rpc_task *task,
  1380. struct nfs_pgio_header *hdr)
  1381. {
  1382. struct nfs_pgio_args *argp = &hdr->args;
  1383. struct nfs_pgio_res *resp = &hdr->res;
  1384. if (resp->count < argp->count) {
  1385. static unsigned long complain;
  1386. /* This a short write! */
  1387. nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
  1388. /* Has the server at least made some progress? */
  1389. if (resp->count == 0) {
  1390. if (time_before(complain, jiffies)) {
  1391. printk(KERN_WARNING
  1392. "NFS: Server wrote zero bytes, expected %u.\n",
  1393. argp->count);
  1394. complain = jiffies + 300 * HZ;
  1395. }
  1396. nfs_set_pgio_error(hdr, -EIO, argp->offset);
  1397. task->tk_status = -EIO;
  1398. return;
  1399. }
  1400. /* For non rpc-based layout drivers, retry-through-MDS */
  1401. if (!task->tk_ops) {
  1402. hdr->pnfs_error = -EAGAIN;
  1403. return;
  1404. }
  1405. /* Was this an NFSv2 write or an NFSv3 stable write? */
  1406. if (resp->verf->committed != NFS_UNSTABLE) {
  1407. /* Resend from where the server left off */
  1408. hdr->mds_offset += resp->count;
  1409. argp->offset += resp->count;
  1410. argp->pgbase += resp->count;
  1411. argp->count -= resp->count;
  1412. } else {
  1413. /* Resend as a stable write in order to avoid
  1414. * headaches in the case of a server crash.
  1415. */
  1416. argp->stable = NFS_FILE_SYNC;
  1417. }
  1418. rpc_restart_call_prepare(task);
  1419. }
  1420. }
  1421. static int wait_on_commit(struct nfs_mds_commit_info *cinfo)
  1422. {
  1423. return wait_var_event_killable(&cinfo->rpcs_out,
  1424. !atomic_read(&cinfo->rpcs_out));
  1425. }
  1426. static void nfs_commit_begin(struct nfs_mds_commit_info *cinfo)
  1427. {
  1428. atomic_inc(&cinfo->rpcs_out);
  1429. }
  1430. static void nfs_commit_end(struct nfs_mds_commit_info *cinfo)
  1431. {
  1432. if (atomic_dec_and_test(&cinfo->rpcs_out))
  1433. wake_up_var(&cinfo->rpcs_out);
  1434. }
  1435. void nfs_commitdata_release(struct nfs_commit_data *data)
  1436. {
  1437. put_nfs_open_context(data->context);
  1438. nfs_commit_free(data);
  1439. }
  1440. EXPORT_SYMBOL_GPL(nfs_commitdata_release);
  1441. int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
  1442. const struct nfs_rpc_ops *nfs_ops,
  1443. const struct rpc_call_ops *call_ops,
  1444. int how, int flags)
  1445. {
  1446. struct rpc_task *task;
  1447. int priority = flush_task_priority(how);
  1448. struct rpc_message msg = {
  1449. .rpc_argp = &data->args,
  1450. .rpc_resp = &data->res,
  1451. .rpc_cred = data->cred,
  1452. };
  1453. struct rpc_task_setup task_setup_data = {
  1454. .task = &data->task,
  1455. .rpc_client = clnt,
  1456. .rpc_message = &msg,
  1457. .callback_ops = call_ops,
  1458. .callback_data = data,
  1459. .workqueue = nfsiod_workqueue,
  1460. .flags = RPC_TASK_ASYNC | flags,
  1461. .priority = priority,
  1462. };
  1463. /* Set up the initial task struct. */
  1464. nfs_ops->commit_setup(data, &msg, &task_setup_data.rpc_client);
  1465. trace_nfs_initiate_commit(data);
  1466. dprintk("NFS: initiated commit call\n");
  1467. task = rpc_run_task(&task_setup_data);
  1468. if (IS_ERR(task))
  1469. return PTR_ERR(task);
  1470. if (how & FLUSH_SYNC)
  1471. rpc_wait_for_completion_task(task);
  1472. rpc_put_task(task);
  1473. return 0;
  1474. }
  1475. EXPORT_SYMBOL_GPL(nfs_initiate_commit);
  1476. static loff_t nfs_get_lwb(struct list_head *head)
  1477. {
  1478. loff_t lwb = 0;
  1479. struct nfs_page *req;
  1480. list_for_each_entry(req, head, wb_list)
  1481. if (lwb < (req_offset(req) + req->wb_bytes))
  1482. lwb = req_offset(req) + req->wb_bytes;
  1483. return lwb;
  1484. }
  1485. /*
  1486. * Set up the argument/result storage required for the RPC call.
  1487. */
  1488. void nfs_init_commit(struct nfs_commit_data *data,
  1489. struct list_head *head,
  1490. struct pnfs_layout_segment *lseg,
  1491. struct nfs_commit_info *cinfo)
  1492. {
  1493. struct nfs_page *first = nfs_list_entry(head->next);
  1494. struct inode *inode = d_inode(first->wb_context->dentry);
  1495. /* Set up the RPC argument and reply structs
  1496. * NB: take care not to mess about with data->commit et al. */
  1497. list_splice_init(head, &data->pages);
  1498. data->inode = inode;
  1499. data->cred = first->wb_context->cred;
  1500. data->lseg = lseg; /* reference transferred */
  1501. /* only set lwb for pnfs commit */
  1502. if (lseg)
  1503. data->lwb = nfs_get_lwb(&data->pages);
  1504. data->mds_ops = &nfs_commit_ops;
  1505. data->completion_ops = cinfo->completion_ops;
  1506. data->dreq = cinfo->dreq;
  1507. data->args.fh = NFS_FH(data->inode);
  1508. /* Note: we always request a commit of the entire inode */
  1509. data->args.offset = 0;
  1510. data->args.count = 0;
  1511. data->context = get_nfs_open_context(first->wb_context);
  1512. data->res.fattr = &data->fattr;
  1513. data->res.verf = &data->verf;
  1514. nfs_fattr_init(&data->fattr);
  1515. }
  1516. EXPORT_SYMBOL_GPL(nfs_init_commit);
  1517. void nfs_retry_commit(struct list_head *page_list,
  1518. struct pnfs_layout_segment *lseg,
  1519. struct nfs_commit_info *cinfo,
  1520. u32 ds_commit_idx)
  1521. {
  1522. struct nfs_page *req;
  1523. while (!list_empty(page_list)) {
  1524. req = nfs_list_entry(page_list->next);
  1525. nfs_list_remove_request(req);
  1526. nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx);
  1527. if (!cinfo->dreq)
  1528. nfs_clear_page_commit(req->wb_page);
  1529. nfs_unlock_and_release_request(req);
  1530. }
  1531. }
  1532. EXPORT_SYMBOL_GPL(nfs_retry_commit);
  1533. static void
  1534. nfs_commit_resched_write(struct nfs_commit_info *cinfo,
  1535. struct nfs_page *req)
  1536. {
  1537. __set_page_dirty_nobuffers(req->wb_page);
  1538. }
  1539. /*
  1540. * Commit dirty pages
  1541. */
  1542. static int
  1543. nfs_commit_list(struct inode *inode, struct list_head *head, int how,
  1544. struct nfs_commit_info *cinfo)
  1545. {
  1546. struct nfs_commit_data *data;
  1547. /* another commit raced with us */
  1548. if (list_empty(head))
  1549. return 0;
  1550. data = nfs_commitdata_alloc(true);
  1551. /* Set up the argument struct */
  1552. nfs_init_commit(data, head, NULL, cinfo);
  1553. atomic_inc(&cinfo->mds->rpcs_out);
  1554. return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
  1555. data->mds_ops, how, 0);
  1556. }
  1557. /*
  1558. * COMMIT call returned
  1559. */
  1560. static void nfs_commit_done(struct rpc_task *task, void *calldata)
  1561. {
  1562. struct nfs_commit_data *data = calldata;
  1563. dprintk("NFS: %5u nfs_commit_done (status %d)\n",
  1564. task->tk_pid, task->tk_status);
  1565. /* Call the NFS version-specific code */
  1566. NFS_PROTO(data->inode)->commit_done(task, data);
  1567. trace_nfs_commit_done(data);
  1568. }
  1569. static void nfs_commit_release_pages(struct nfs_commit_data *data)
  1570. {
  1571. struct nfs_page *req;
  1572. int status = data->task.tk_status;
  1573. struct nfs_commit_info cinfo;
  1574. struct nfs_server *nfss;
  1575. while (!list_empty(&data->pages)) {
  1576. req = nfs_list_entry(data->pages.next);
  1577. nfs_list_remove_request(req);
  1578. if (req->wb_page)
  1579. nfs_clear_page_commit(req->wb_page);
  1580. dprintk("NFS: commit (%s/%llu %d@%lld)",
  1581. req->wb_context->dentry->d_sb->s_id,
  1582. (unsigned long long)NFS_FILEID(d_inode(req->wb_context->dentry)),
  1583. req->wb_bytes,
  1584. (long long)req_offset(req));
  1585. if (status < 0) {
  1586. nfs_context_set_write_error(req->wb_context, status);
  1587. if (req->wb_page)
  1588. nfs_inode_remove_request(req);
  1589. dprintk_cont(", error = %d\n", status);
  1590. goto next;
  1591. }
  1592. /* Okay, COMMIT succeeded, apparently. Check the verifier
  1593. * returned by the server against all stored verfs. */
  1594. if (!nfs_write_verifier_cmp(&req->wb_verf, &data->verf.verifier)) {
  1595. /* We have a match */
  1596. if (req->wb_page)
  1597. nfs_inode_remove_request(req);
  1598. dprintk_cont(" OK\n");
  1599. goto next;
  1600. }
  1601. /* We have a mismatch. Write the page again */
  1602. dprintk_cont(" mismatch\n");
  1603. nfs_mark_request_dirty(req);
  1604. set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
  1605. next:
  1606. nfs_unlock_and_release_request(req);
  1607. /* Latency breaker */
  1608. cond_resched();
  1609. }
  1610. nfss = NFS_SERVER(data->inode);
  1611. if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
  1612. clear_bdi_congested(inode_to_bdi(data->inode), BLK_RW_ASYNC);
  1613. nfs_init_cinfo(&cinfo, data->inode, data->dreq);
  1614. nfs_commit_end(cinfo.mds);
  1615. }
  1616. static void nfs_commit_release(void *calldata)
  1617. {
  1618. struct nfs_commit_data *data = calldata;
  1619. data->completion_ops->completion(data);
  1620. nfs_commitdata_release(calldata);
  1621. }
  1622. static const struct rpc_call_ops nfs_commit_ops = {
  1623. .rpc_call_prepare = nfs_commit_prepare,
  1624. .rpc_call_done = nfs_commit_done,
  1625. .rpc_release = nfs_commit_release,
  1626. };
  1627. static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
  1628. .completion = nfs_commit_release_pages,
  1629. .resched_write = nfs_commit_resched_write,
  1630. };
  1631. int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
  1632. int how, struct nfs_commit_info *cinfo)
  1633. {
  1634. int status;
  1635. status = pnfs_commit_list(inode, head, how, cinfo);
  1636. if (status == PNFS_NOT_ATTEMPTED)
  1637. status = nfs_commit_list(inode, head, how, cinfo);
  1638. return status;
  1639. }
  1640. static int __nfs_commit_inode(struct inode *inode, int how,
  1641. struct writeback_control *wbc)
  1642. {
  1643. LIST_HEAD(head);
  1644. struct nfs_commit_info cinfo;
  1645. int may_wait = how & FLUSH_SYNC;
  1646. int ret, nscan;
  1647. nfs_init_cinfo_from_inode(&cinfo, inode);
  1648. nfs_commit_begin(cinfo.mds);
  1649. for (;;) {
  1650. ret = nscan = nfs_scan_commit(inode, &head, &cinfo);
  1651. if (ret <= 0)
  1652. break;
  1653. ret = nfs_generic_commit_list(inode, &head, how, &cinfo);
  1654. if (ret < 0)
  1655. break;
  1656. ret = 0;
  1657. if (wbc && wbc->sync_mode == WB_SYNC_NONE) {
  1658. if (nscan < wbc->nr_to_write)
  1659. wbc->nr_to_write -= nscan;
  1660. else
  1661. wbc->nr_to_write = 0;
  1662. }
  1663. if (nscan < INT_MAX)
  1664. break;
  1665. cond_resched();
  1666. }
  1667. nfs_commit_end(cinfo.mds);
  1668. if (ret || !may_wait)
  1669. return ret;
  1670. return wait_on_commit(cinfo.mds);
  1671. }
  1672. int nfs_commit_inode(struct inode *inode, int how)
  1673. {
  1674. return __nfs_commit_inode(inode, how, NULL);
  1675. }
  1676. EXPORT_SYMBOL_GPL(nfs_commit_inode);
  1677. int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  1678. {
  1679. struct nfs_inode *nfsi = NFS_I(inode);
  1680. int flags = FLUSH_SYNC;
  1681. int ret = 0;
  1682. if (wbc->sync_mode == WB_SYNC_NONE) {
  1683. /* no commits means nothing needs to be done */
  1684. if (!atomic_long_read(&nfsi->commit_info.ncommit))
  1685. goto check_requests_outstanding;
  1686. /* Don't commit yet if this is a non-blocking flush and there
  1687. * are a lot of outstanding writes for this mapping.
  1688. */
  1689. if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
  1690. goto out_mark_dirty;
  1691. /* don't wait for the COMMIT response */
  1692. flags = 0;
  1693. }
  1694. ret = __nfs_commit_inode(inode, flags, wbc);
  1695. if (!ret) {
  1696. if (flags & FLUSH_SYNC)
  1697. return 0;
  1698. } else if (atomic_long_read(&nfsi->commit_info.ncommit))
  1699. goto out_mark_dirty;
  1700. check_requests_outstanding:
  1701. if (!atomic_read(&nfsi->commit_info.rpcs_out))
  1702. return ret;
  1703. out_mark_dirty:
  1704. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  1705. return ret;
  1706. }
  1707. EXPORT_SYMBOL_GPL(nfs_write_inode);
  1708. /*
  1709. * Wrapper for filemap_write_and_wait_range()
  1710. *
  1711. * Needed for pNFS in order to ensure data becomes visible to the
  1712. * client.
  1713. */
  1714. int nfs_filemap_write_and_wait_range(struct address_space *mapping,
  1715. loff_t lstart, loff_t lend)
  1716. {
  1717. int ret;
  1718. ret = filemap_write_and_wait_range(mapping, lstart, lend);
  1719. if (ret == 0)
  1720. ret = pnfs_sync_inode(mapping->host, true);
  1721. return ret;
  1722. }
  1723. EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range);
  1724. /*
  1725. * flush the inode to disk.
  1726. */
  1727. int nfs_wb_all(struct inode *inode)
  1728. {
  1729. int ret;
  1730. trace_nfs_writeback_inode_enter(inode);
  1731. ret = filemap_write_and_wait(inode->i_mapping);
  1732. if (ret)
  1733. goto out;
  1734. ret = nfs_commit_inode(inode, FLUSH_SYNC);
  1735. if (ret < 0)
  1736. goto out;
  1737. pnfs_sync_inode(inode, true);
  1738. ret = 0;
  1739. out:
  1740. trace_nfs_writeback_inode_exit(inode, ret);
  1741. return ret;
  1742. }
  1743. EXPORT_SYMBOL_GPL(nfs_wb_all);
  1744. int nfs_wb_page_cancel(struct inode *inode, struct page *page)
  1745. {
  1746. struct nfs_page *req;
  1747. int ret = 0;
  1748. wait_on_page_writeback(page);
  1749. /* blocking call to cancel all requests and join to a single (head)
  1750. * request */
  1751. req = nfs_lock_and_join_requests(page);
  1752. if (IS_ERR(req)) {
  1753. ret = PTR_ERR(req);
  1754. } else if (req) {
  1755. /* all requests from this page have been cancelled by
  1756. * nfs_lock_and_join_requests, so just remove the head
  1757. * request from the inode / page_private pointer and
  1758. * release it */
  1759. nfs_inode_remove_request(req);
  1760. nfs_unlock_and_release_request(req);
  1761. }
  1762. return ret;
  1763. }
  1764. /*
  1765. * Write back all requests on one page - we do this before reading it.
  1766. */
  1767. int nfs_wb_page(struct inode *inode, struct page *page)
  1768. {
  1769. loff_t range_start = page_file_offset(page);
  1770. loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
  1771. struct writeback_control wbc = {
  1772. .sync_mode = WB_SYNC_ALL,
  1773. .nr_to_write = 0,
  1774. .range_start = range_start,
  1775. .range_end = range_end,
  1776. };
  1777. int ret;
  1778. trace_nfs_writeback_page_enter(inode);
  1779. for (;;) {
  1780. wait_on_page_writeback(page);
  1781. if (clear_page_dirty_for_io(page)) {
  1782. ret = nfs_writepage_locked(page, &wbc);
  1783. if (ret < 0)
  1784. goto out_error;
  1785. continue;
  1786. }
  1787. ret = 0;
  1788. if (!PagePrivate(page))
  1789. break;
  1790. ret = nfs_commit_inode(inode, FLUSH_SYNC);
  1791. if (ret < 0)
  1792. goto out_error;
  1793. }
  1794. out_error:
  1795. trace_nfs_writeback_page_exit(inode, ret);
  1796. return ret;
  1797. }
  1798. #ifdef CONFIG_MIGRATION
  1799. int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
  1800. struct page *page, enum migrate_mode mode)
  1801. {
  1802. /*
  1803. * If PagePrivate is set, then the page is currently associated with
  1804. * an in-progress read or write request. Don't try to migrate it.
  1805. *
  1806. * FIXME: we could do this in principle, but we'll need a way to ensure
  1807. * that we can safely release the inode reference while holding
  1808. * the page lock.
  1809. */
  1810. if (PagePrivate(page))
  1811. return -EBUSY;
  1812. if (!nfs_fscache_release_page(page, GFP_KERNEL))
  1813. return -EBUSY;
  1814. return migrate_page(mapping, newpage, page, mode);
  1815. }
  1816. #endif
  1817. int __init nfs_init_writepagecache(void)
  1818. {
  1819. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1820. sizeof(struct nfs_pgio_header),
  1821. 0, SLAB_HWCACHE_ALIGN,
  1822. NULL);
  1823. if (nfs_wdata_cachep == NULL)
  1824. return -ENOMEM;
  1825. nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
  1826. nfs_wdata_cachep);
  1827. if (nfs_wdata_mempool == NULL)
  1828. goto out_destroy_write_cache;
  1829. nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
  1830. sizeof(struct nfs_commit_data),
  1831. 0, SLAB_HWCACHE_ALIGN,
  1832. NULL);
  1833. if (nfs_cdata_cachep == NULL)
  1834. goto out_destroy_write_mempool;
  1835. nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
  1836. nfs_cdata_cachep);
  1837. if (nfs_commit_mempool == NULL)
  1838. goto out_destroy_commit_cache;
  1839. /*
  1840. * NFS congestion size, scale with available memory.
  1841. *
  1842. * 64MB: 8192k
  1843. * 128MB: 11585k
  1844. * 256MB: 16384k
  1845. * 512MB: 23170k
  1846. * 1GB: 32768k
  1847. * 2GB: 46340k
  1848. * 4GB: 65536k
  1849. * 8GB: 92681k
  1850. * 16GB: 131072k
  1851. *
  1852. * This allows larger machines to have larger/more transfers.
  1853. * Limit the default to 256M
  1854. */
  1855. nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
  1856. if (nfs_congestion_kb > 256*1024)
  1857. nfs_congestion_kb = 256*1024;
  1858. return 0;
  1859. out_destroy_commit_cache:
  1860. kmem_cache_destroy(nfs_cdata_cachep);
  1861. out_destroy_write_mempool:
  1862. mempool_destroy(nfs_wdata_mempool);
  1863. out_destroy_write_cache:
  1864. kmem_cache_destroy(nfs_wdata_cachep);
  1865. return -ENOMEM;
  1866. }
  1867. void nfs_destroy_writepagecache(void)
  1868. {
  1869. mempool_destroy(nfs_commit_mempool);
  1870. kmem_cache_destroy(nfs_cdata_cachep);
  1871. mempool_destroy(nfs_wdata_mempool);
  1872. kmem_cache_destroy(nfs_wdata_cachep);
  1873. }
  1874. static const struct nfs_rw_ops nfs_rw_write_ops = {
  1875. .rw_alloc_header = nfs_writehdr_alloc,
  1876. .rw_free_header = nfs_writehdr_free,
  1877. .rw_done = nfs_writeback_done,
  1878. .rw_result = nfs_writeback_result,
  1879. .rw_initiate = nfs_initiate_write,
  1880. };