write.c 56 KB

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