write.c 56 KB

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