write.c 56 KB

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