write.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126
  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. /*
  482. * Find an associated nfs write request, and prepare to flush it out
  483. * May return an error if the user signalled nfs_wait_on_request().
  484. */
  485. static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio,
  486. struct page *page, bool nonblock,
  487. bool launder)
  488. {
  489. struct nfs_page *req;
  490. int ret = 0;
  491. req = nfs_lock_and_join_requests(page, nonblock);
  492. if (!req)
  493. goto out;
  494. ret = PTR_ERR(req);
  495. if (IS_ERR(req))
  496. goto out;
  497. nfs_set_page_writeback(page);
  498. WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags));
  499. ret = 0;
  500. if (!nfs_pageio_add_request(pgio, req)) {
  501. ret = pgio->pg_error;
  502. /*
  503. * Remove the problematic req upon fatal errors
  504. * in launder case, while other dirty pages can
  505. * still be around until they get flushed.
  506. */
  507. if (nfs_error_is_fatal(ret)) {
  508. nfs_context_set_write_error(req->wb_context, ret);
  509. if (launder) {
  510. nfs_write_error_remove_page(req);
  511. goto out;
  512. }
  513. }
  514. nfs_redirty_request(req);
  515. ret = -EAGAIN;
  516. } else
  517. nfs_add_stats(page_file_mapping(page)->host,
  518. NFSIOS_WRITEPAGES, 1);
  519. out:
  520. return ret;
  521. }
  522. static int nfs_do_writepage(struct page *page, struct writeback_control *wbc,
  523. struct nfs_pageio_descriptor *pgio, bool launder)
  524. {
  525. int ret;
  526. nfs_pageio_cond_complete(pgio, page_index(page));
  527. ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE,
  528. launder);
  529. if (ret == -EAGAIN) {
  530. redirty_page_for_writepage(wbc, page);
  531. ret = 0;
  532. }
  533. return ret;
  534. }
  535. /*
  536. * Write an mmapped page to the server.
  537. */
  538. static int nfs_writepage_locked(struct page *page,
  539. struct writeback_control *wbc,
  540. bool launder)
  541. {
  542. struct nfs_pageio_descriptor pgio;
  543. struct inode *inode = page_file_mapping(page)->host;
  544. int err;
  545. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
  546. nfs_pageio_init_write(&pgio, inode, 0,
  547. false, &nfs_async_write_completion_ops);
  548. err = nfs_do_writepage(page, wbc, &pgio, launder);
  549. nfs_pageio_complete(&pgio);
  550. if (err < 0)
  551. return err;
  552. if (pgio.pg_error < 0)
  553. return pgio.pg_error;
  554. return 0;
  555. }
  556. int nfs_writepage(struct page *page, struct writeback_control *wbc)
  557. {
  558. int ret;
  559. ret = nfs_writepage_locked(page, wbc, false);
  560. unlock_page(page);
  561. return ret;
  562. }
  563. static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data)
  564. {
  565. int ret;
  566. ret = nfs_do_writepage(page, wbc, data, false);
  567. unlock_page(page);
  568. return ret;
  569. }
  570. int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  571. {
  572. struct inode *inode = mapping->host;
  573. struct nfs_pageio_descriptor pgio;
  574. int err;
  575. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
  576. nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false,
  577. &nfs_async_write_completion_ops);
  578. err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio);
  579. nfs_pageio_complete(&pgio);
  580. if (err < 0)
  581. goto out_err;
  582. err = pgio.pg_error;
  583. if (err < 0)
  584. goto out_err;
  585. return 0;
  586. out_err:
  587. return err;
  588. }
  589. /*
  590. * Insert a write request into an inode
  591. */
  592. static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
  593. {
  594. struct nfs_inode *nfsi = NFS_I(inode);
  595. WARN_ON_ONCE(req->wb_this_page != req);
  596. /* Lock the request! */
  597. nfs_lock_request(req);
  598. spin_lock(&inode->i_lock);
  599. if (!nfsi->nrequests &&
  600. NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
  601. inode->i_version++;
  602. /*
  603. * Swap-space should not get truncated. Hence no need to plug the race
  604. * with invalidate/truncate.
  605. */
  606. if (likely(!PageSwapCache(req->wb_page))) {
  607. set_bit(PG_MAPPED, &req->wb_flags);
  608. SetPagePrivate(req->wb_page);
  609. set_page_private(req->wb_page, (unsigned long)req);
  610. }
  611. nfsi->nrequests++;
  612. /* this a head request for a page group - mark it as having an
  613. * extra reference so sub groups can follow suit.
  614. * This flag also informs pgio layer when to bump nrequests when
  615. * adding subrequests. */
  616. WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags));
  617. kref_get(&req->wb_kref);
  618. spin_unlock(&inode->i_lock);
  619. }
  620. /*
  621. * Remove a write request from an inode
  622. */
  623. static void nfs_inode_remove_request(struct nfs_page *req)
  624. {
  625. struct inode *inode = d_inode(req->wb_context->dentry);
  626. struct nfs_inode *nfsi = NFS_I(inode);
  627. struct nfs_page *head;
  628. if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) {
  629. head = req->wb_head;
  630. spin_lock(&inode->i_lock);
  631. if (likely(head->wb_page && !PageSwapCache(head->wb_page))) {
  632. set_page_private(head->wb_page, 0);
  633. ClearPagePrivate(head->wb_page);
  634. clear_bit(PG_MAPPED, &head->wb_flags);
  635. }
  636. nfsi->nrequests--;
  637. spin_unlock(&inode->i_lock);
  638. } else {
  639. spin_lock(&inode->i_lock);
  640. nfsi->nrequests--;
  641. spin_unlock(&inode->i_lock);
  642. }
  643. if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags))
  644. nfs_release_request(req);
  645. }
  646. static void
  647. nfs_mark_request_dirty(struct nfs_page *req)
  648. {
  649. if (req->wb_page)
  650. __set_page_dirty_nobuffers(req->wb_page);
  651. }
  652. /*
  653. * nfs_page_search_commits_for_head_request_locked
  654. *
  655. * Search through commit lists on @inode for the head request for @page.
  656. * Must be called while holding the inode (which is cinfo) lock.
  657. *
  658. * Returns the head request if found, or NULL if not found.
  659. */
  660. static struct nfs_page *
  661. nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi,
  662. struct page *page)
  663. {
  664. struct nfs_page *freq, *t;
  665. struct nfs_commit_info cinfo;
  666. struct inode *inode = &nfsi->vfs_inode;
  667. nfs_init_cinfo_from_inode(&cinfo, inode);
  668. /* search through pnfs commit lists */
  669. freq = pnfs_search_commit_reqs(inode, &cinfo, page);
  670. if (freq)
  671. return freq->wb_head;
  672. /* Linearly search the commit list for the correct request */
  673. list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) {
  674. if (freq->wb_page == page)
  675. return freq->wb_head;
  676. }
  677. return NULL;
  678. }
  679. /**
  680. * nfs_request_add_commit_list_locked - add request to a commit list
  681. * @req: pointer to a struct nfs_page
  682. * @dst: commit list head
  683. * @cinfo: holds list lock and accounting info
  684. *
  685. * This sets the PG_CLEAN bit, updates the cinfo count of
  686. * number of outstanding requests requiring a commit as well as
  687. * the MM page stats.
  688. *
  689. * The caller must hold cinfo->inode->i_lock, and the nfs_page lock.
  690. */
  691. void
  692. nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst,
  693. struct nfs_commit_info *cinfo)
  694. {
  695. set_bit(PG_CLEAN, &req->wb_flags);
  696. nfs_list_add_request(req, dst);
  697. cinfo->mds->ncommit++;
  698. }
  699. EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked);
  700. /**
  701. * nfs_request_add_commit_list - add request to a commit list
  702. * @req: pointer to a struct nfs_page
  703. * @dst: commit list head
  704. * @cinfo: holds list lock and accounting info
  705. *
  706. * This sets the PG_CLEAN bit, updates the cinfo count of
  707. * number of outstanding requests requiring a commit as well as
  708. * the MM page stats.
  709. *
  710. * The caller must _not_ hold the cinfo->lock, but must be
  711. * holding the nfs_page lock.
  712. */
  713. void
  714. nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo)
  715. {
  716. spin_lock(&cinfo->inode->i_lock);
  717. nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo);
  718. spin_unlock(&cinfo->inode->i_lock);
  719. if (req->wb_page)
  720. nfs_mark_page_unstable(req->wb_page, cinfo);
  721. }
  722. EXPORT_SYMBOL_GPL(nfs_request_add_commit_list);
  723. /**
  724. * nfs_request_remove_commit_list - Remove request from a commit list
  725. * @req: pointer to a nfs_page
  726. * @cinfo: holds list lock and accounting info
  727. *
  728. * This clears the PG_CLEAN bit, and updates the cinfo's count of
  729. * number of outstanding requests requiring a commit
  730. * It does not update the MM page stats.
  731. *
  732. * The caller _must_ hold the cinfo->lock and the nfs_page lock.
  733. */
  734. void
  735. nfs_request_remove_commit_list(struct nfs_page *req,
  736. struct nfs_commit_info *cinfo)
  737. {
  738. if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags))
  739. return;
  740. nfs_list_remove_request(req);
  741. cinfo->mds->ncommit--;
  742. }
  743. EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list);
  744. static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo,
  745. struct inode *inode)
  746. {
  747. cinfo->inode = inode;
  748. cinfo->mds = &NFS_I(inode)->commit_info;
  749. cinfo->ds = pnfs_get_ds_info(inode);
  750. cinfo->dreq = NULL;
  751. cinfo->completion_ops = &nfs_commit_completion_ops;
  752. }
  753. void nfs_init_cinfo(struct nfs_commit_info *cinfo,
  754. struct inode *inode,
  755. struct nfs_direct_req *dreq)
  756. {
  757. if (dreq)
  758. nfs_init_cinfo_from_dreq(cinfo, dreq);
  759. else
  760. nfs_init_cinfo_from_inode(cinfo, inode);
  761. }
  762. EXPORT_SYMBOL_GPL(nfs_init_cinfo);
  763. /*
  764. * Add a request to the inode's commit list.
  765. */
  766. void
  767. nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg,
  768. struct nfs_commit_info *cinfo, u32 ds_commit_idx)
  769. {
  770. if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx))
  771. return;
  772. nfs_request_add_commit_list(req, cinfo);
  773. }
  774. static void
  775. nfs_clear_page_commit(struct page *page)
  776. {
  777. dec_node_page_state(page, NR_UNSTABLE_NFS);
  778. dec_wb_stat(&inode_to_bdi(page_file_mapping(page)->host)->wb,
  779. WB_RECLAIMABLE);
  780. }
  781. /* Called holding inode (/cinfo) lock */
  782. static void
  783. nfs_clear_request_commit(struct nfs_page *req)
  784. {
  785. if (test_bit(PG_CLEAN, &req->wb_flags)) {
  786. struct inode *inode = d_inode(req->wb_context->dentry);
  787. struct nfs_commit_info cinfo;
  788. nfs_init_cinfo_from_inode(&cinfo, inode);
  789. if (!pnfs_clear_request_commit(req, &cinfo)) {
  790. nfs_request_remove_commit_list(req, &cinfo);
  791. }
  792. nfs_clear_page_commit(req->wb_page);
  793. }
  794. }
  795. int nfs_write_need_commit(struct nfs_pgio_header *hdr)
  796. {
  797. if (hdr->verf.committed == NFS_DATA_SYNC)
  798. return hdr->lseg == NULL;
  799. return hdr->verf.committed != NFS_FILE_SYNC;
  800. }
  801. static void nfs_write_completion(struct nfs_pgio_header *hdr)
  802. {
  803. struct nfs_commit_info cinfo;
  804. unsigned long bytes = 0;
  805. if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
  806. goto out;
  807. nfs_init_cinfo_from_inode(&cinfo, hdr->inode);
  808. while (!list_empty(&hdr->pages)) {
  809. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  810. bytes += req->wb_bytes;
  811. nfs_list_remove_request(req);
  812. if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) &&
  813. (hdr->good_bytes < bytes)) {
  814. nfs_set_pageerror(req->wb_page);
  815. nfs_context_set_write_error(req->wb_context, hdr->error);
  816. goto remove_req;
  817. }
  818. if (nfs_write_need_commit(hdr)) {
  819. memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf));
  820. nfs_mark_request_commit(req, hdr->lseg, &cinfo,
  821. hdr->pgio_mirror_idx);
  822. goto next;
  823. }
  824. remove_req:
  825. nfs_inode_remove_request(req);
  826. next:
  827. nfs_unlock_request(req);
  828. nfs_end_page_writeback(req);
  829. nfs_release_request(req);
  830. }
  831. out:
  832. hdr->release(hdr);
  833. }
  834. unsigned long
  835. nfs_reqs_to_commit(struct nfs_commit_info *cinfo)
  836. {
  837. return cinfo->mds->ncommit;
  838. }
  839. /* cinfo->inode->i_lock held by caller */
  840. int
  841. nfs_scan_commit_list(struct list_head *src, struct list_head *dst,
  842. struct nfs_commit_info *cinfo, int max)
  843. {
  844. struct nfs_page *req, *tmp;
  845. int ret = 0;
  846. list_for_each_entry_safe(req, tmp, src, wb_list) {
  847. if (!nfs_lock_request(req))
  848. continue;
  849. kref_get(&req->wb_kref);
  850. if (cond_resched_lock(&cinfo->inode->i_lock))
  851. list_safe_reset_next(req, tmp, wb_list);
  852. nfs_request_remove_commit_list(req, cinfo);
  853. nfs_list_add_request(req, dst);
  854. ret++;
  855. if ((ret == max) && !cinfo->dreq)
  856. break;
  857. }
  858. return ret;
  859. }
  860. /*
  861. * nfs_scan_commit - Scan an inode for commit requests
  862. * @inode: NFS inode to scan
  863. * @dst: mds destination list
  864. * @cinfo: mds and ds lists of reqs ready to commit
  865. *
  866. * Moves requests from the inode's 'commit' request list.
  867. * The requests are *not* checked to ensure that they form a contiguous set.
  868. */
  869. int
  870. nfs_scan_commit(struct inode *inode, struct list_head *dst,
  871. struct nfs_commit_info *cinfo)
  872. {
  873. int ret = 0;
  874. spin_lock(&cinfo->inode->i_lock);
  875. if (cinfo->mds->ncommit > 0) {
  876. const int max = INT_MAX;
  877. ret = nfs_scan_commit_list(&cinfo->mds->list, dst,
  878. cinfo, max);
  879. ret += pnfs_scan_commit_lists(inode, cinfo, max - ret);
  880. }
  881. spin_unlock(&cinfo->inode->i_lock);
  882. return ret;
  883. }
  884. /*
  885. * Search for an existing write request, and attempt to update
  886. * it to reflect a new dirty region on a given page.
  887. *
  888. * If the attempt fails, then the existing request is flushed out
  889. * to disk.
  890. */
  891. static struct nfs_page *nfs_try_to_update_request(struct inode *inode,
  892. struct page *page,
  893. unsigned int offset,
  894. unsigned int bytes)
  895. {
  896. struct nfs_page *req;
  897. unsigned int rqend;
  898. unsigned int end;
  899. int error;
  900. if (!PagePrivate(page))
  901. return NULL;
  902. end = offset + bytes;
  903. spin_lock(&inode->i_lock);
  904. for (;;) {
  905. req = nfs_page_find_head_request_locked(NFS_I(inode), page);
  906. if (req == NULL)
  907. goto out_unlock;
  908. /* should be handled by nfs_flush_incompatible */
  909. WARN_ON_ONCE(req->wb_head != req);
  910. WARN_ON_ONCE(req->wb_this_page != req);
  911. rqend = req->wb_offset + req->wb_bytes;
  912. /*
  913. * Tell the caller to flush out the request if
  914. * the offsets are non-contiguous.
  915. * Note: nfs_flush_incompatible() will already
  916. * have flushed out requests having wrong owners.
  917. */
  918. if (offset > rqend
  919. || end < req->wb_offset)
  920. goto out_flushme;
  921. if (nfs_lock_request(req))
  922. break;
  923. /* The request is locked, so wait and then retry */
  924. spin_unlock(&inode->i_lock);
  925. error = nfs_wait_on_request(req);
  926. nfs_release_request(req);
  927. if (error != 0)
  928. goto out_err;
  929. spin_lock(&inode->i_lock);
  930. }
  931. /* Okay, the request matches. Update the region */
  932. if (offset < req->wb_offset) {
  933. req->wb_offset = offset;
  934. req->wb_pgbase = offset;
  935. }
  936. if (end > rqend)
  937. req->wb_bytes = end - req->wb_offset;
  938. else
  939. req->wb_bytes = rqend - req->wb_offset;
  940. out_unlock:
  941. if (req)
  942. nfs_clear_request_commit(req);
  943. spin_unlock(&inode->i_lock);
  944. return req;
  945. out_flushme:
  946. spin_unlock(&inode->i_lock);
  947. nfs_release_request(req);
  948. error = nfs_wb_page(inode, page);
  949. out_err:
  950. return ERR_PTR(error);
  951. }
  952. /*
  953. * Try to update an existing write request, or create one if there is none.
  954. *
  955. * Note: Should always be called with the Page Lock held to prevent races
  956. * if we have to add a new request. Also assumes that the caller has
  957. * already called nfs_flush_incompatible() if necessary.
  958. */
  959. static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx,
  960. struct page *page, unsigned int offset, unsigned int bytes)
  961. {
  962. struct inode *inode = page_file_mapping(page)->host;
  963. struct nfs_page *req;
  964. req = nfs_try_to_update_request(inode, page, offset, bytes);
  965. if (req != NULL)
  966. goto out;
  967. req = nfs_create_request(ctx, page, NULL, offset, bytes);
  968. if (IS_ERR(req))
  969. goto out;
  970. nfs_inode_add_request(inode, req);
  971. out:
  972. return req;
  973. }
  974. static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
  975. unsigned int offset, unsigned int count)
  976. {
  977. struct nfs_page *req;
  978. req = nfs_setup_write_request(ctx, page, offset, count);
  979. if (IS_ERR(req))
  980. return PTR_ERR(req);
  981. /* Update file length */
  982. nfs_grow_file(page, offset, count);
  983. nfs_mark_uptodate(req);
  984. nfs_mark_request_dirty(req);
  985. nfs_unlock_and_release_request(req);
  986. return 0;
  987. }
  988. int nfs_flush_incompatible(struct file *file, struct page *page)
  989. {
  990. struct nfs_open_context *ctx = nfs_file_open_context(file);
  991. struct nfs_lock_context *l_ctx;
  992. struct file_lock_context *flctx = file_inode(file)->i_flctx;
  993. struct nfs_page *req;
  994. int do_flush, status;
  995. /*
  996. * Look for a request corresponding to this page. If there
  997. * is one, and it belongs to another file, we flush it out
  998. * before we try to copy anything into the page. Do this
  999. * due to the lack of an ACCESS-type call in NFSv2.
  1000. * Also do the same if we find a request from an existing
  1001. * dropped page.
  1002. */
  1003. do {
  1004. req = nfs_page_find_head_request(page);
  1005. if (req == NULL)
  1006. return 0;
  1007. l_ctx = req->wb_lock_context;
  1008. do_flush = req->wb_page != page ||
  1009. !nfs_match_open_context(req->wb_context, ctx);
  1010. /* for now, flush if more than 1 request in page_group */
  1011. do_flush |= req->wb_this_page != req;
  1012. if (l_ctx && flctx &&
  1013. !(list_empty_careful(&flctx->flc_posix) &&
  1014. list_empty_careful(&flctx->flc_flock))) {
  1015. do_flush |= l_ctx->lockowner != current->files;
  1016. }
  1017. nfs_release_request(req);
  1018. if (!do_flush)
  1019. return 0;
  1020. status = nfs_wb_page(page_file_mapping(page)->host, page);
  1021. } while (status == 0);
  1022. return status;
  1023. }
  1024. /*
  1025. * Avoid buffered writes when a open context credential's key would
  1026. * expire soon.
  1027. *
  1028. * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL.
  1029. *
  1030. * Return 0 and set a credential flag which triggers the inode to flush
  1031. * and performs NFS_FILE_SYNC writes if the key will expired within
  1032. * RPC_KEY_EXPIRE_TIMEO.
  1033. */
  1034. int
  1035. nfs_key_timeout_notify(struct file *filp, struct inode *inode)
  1036. {
  1037. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  1038. struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
  1039. return rpcauth_key_timeout_notify(auth, ctx->cred);
  1040. }
  1041. /*
  1042. * Test if the open context credential key is marked to expire soon.
  1043. */
  1044. bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode)
  1045. {
  1046. struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth;
  1047. return rpcauth_cred_key_to_expire(auth, ctx->cred);
  1048. }
  1049. /*
  1050. * If the page cache is marked as unsafe or invalid, then we can't rely on
  1051. * the PageUptodate() flag. In this case, we will need to turn off
  1052. * write optimisations that depend on the page contents being correct.
  1053. */
  1054. static bool nfs_write_pageuptodate(struct page *page, struct inode *inode)
  1055. {
  1056. struct nfs_inode *nfsi = NFS_I(inode);
  1057. if (nfs_have_delegated_attributes(inode))
  1058. goto out;
  1059. if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE)
  1060. return false;
  1061. smp_rmb();
  1062. if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags))
  1063. return false;
  1064. out:
  1065. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  1066. return false;
  1067. return PageUptodate(page) != 0;
  1068. }
  1069. static bool
  1070. is_whole_file_wrlock(struct file_lock *fl)
  1071. {
  1072. return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX &&
  1073. fl->fl_type == F_WRLCK;
  1074. }
  1075. /* If we know the page is up to date, and we're not using byte range locks (or
  1076. * if we have the whole file locked for writing), it may be more efficient to
  1077. * extend the write to cover the entire page in order to avoid fragmentation
  1078. * inefficiencies.
  1079. *
  1080. * If the file is opened for synchronous writes then we can just skip the rest
  1081. * of the checks.
  1082. */
  1083. static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode)
  1084. {
  1085. int ret;
  1086. struct file_lock_context *flctx = inode->i_flctx;
  1087. struct file_lock *fl;
  1088. if (file->f_flags & O_DSYNC)
  1089. return 0;
  1090. if (!nfs_write_pageuptodate(page, inode))
  1091. return 0;
  1092. if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE))
  1093. return 1;
  1094. if (!flctx || (list_empty_careful(&flctx->flc_flock) &&
  1095. list_empty_careful(&flctx->flc_posix)))
  1096. return 1;
  1097. /* Check to see if there are whole file write locks */
  1098. ret = 0;
  1099. spin_lock(&flctx->flc_lock);
  1100. if (!list_empty(&flctx->flc_posix)) {
  1101. fl = list_first_entry(&flctx->flc_posix, struct file_lock,
  1102. fl_list);
  1103. if (is_whole_file_wrlock(fl))
  1104. ret = 1;
  1105. } else if (!list_empty(&flctx->flc_flock)) {
  1106. fl = list_first_entry(&flctx->flc_flock, struct file_lock,
  1107. fl_list);
  1108. if (fl->fl_type == F_WRLCK)
  1109. ret = 1;
  1110. }
  1111. spin_unlock(&flctx->flc_lock);
  1112. return ret;
  1113. }
  1114. /*
  1115. * Update and possibly write a cached page of an NFS file.
  1116. *
  1117. * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
  1118. * things with a page scheduled for an RPC call (e.g. invalidate it).
  1119. */
  1120. int nfs_updatepage(struct file *file, struct page *page,
  1121. unsigned int offset, unsigned int count)
  1122. {
  1123. struct nfs_open_context *ctx = nfs_file_open_context(file);
  1124. struct inode *inode = page_file_mapping(page)->host;
  1125. int status = 0;
  1126. nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
  1127. dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n",
  1128. file, count, (long long)(page_file_offset(page) + offset));
  1129. if (!count)
  1130. goto out;
  1131. if (nfs_can_extend_write(file, page, inode)) {
  1132. count = max(count + offset, nfs_page_length(page));
  1133. offset = 0;
  1134. }
  1135. status = nfs_writepage_setup(ctx, page, offset, count);
  1136. if (status < 0)
  1137. nfs_set_pageerror(page);
  1138. else
  1139. __set_page_dirty_nobuffers(page);
  1140. out:
  1141. dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n",
  1142. status, (long long)i_size_read(inode));
  1143. return status;
  1144. }
  1145. static int flush_task_priority(int how)
  1146. {
  1147. switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
  1148. case FLUSH_HIGHPRI:
  1149. return RPC_PRIORITY_HIGH;
  1150. case FLUSH_LOWPRI:
  1151. return RPC_PRIORITY_LOW;
  1152. }
  1153. return RPC_PRIORITY_NORMAL;
  1154. }
  1155. static void nfs_initiate_write(struct nfs_pgio_header *hdr,
  1156. struct rpc_message *msg,
  1157. const struct nfs_rpc_ops *rpc_ops,
  1158. struct rpc_task_setup *task_setup_data, int how)
  1159. {
  1160. int priority = flush_task_priority(how);
  1161. task_setup_data->priority = priority;
  1162. rpc_ops->write_setup(hdr, msg);
  1163. nfs4_state_protect_write(NFS_SERVER(hdr->inode)->nfs_client,
  1164. &task_setup_data->rpc_client, msg, hdr);
  1165. }
  1166. /* If a nfs_flush_* function fails, it should remove reqs from @head and
  1167. * call this on each, which will prepare them to be retried on next
  1168. * writeback using standard nfs.
  1169. */
  1170. static void nfs_redirty_request(struct nfs_page *req)
  1171. {
  1172. nfs_mark_request_dirty(req);
  1173. set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
  1174. nfs_unlock_request(req);
  1175. nfs_end_page_writeback(req);
  1176. nfs_release_request(req);
  1177. }
  1178. static void nfs_async_write_error(struct list_head *head)
  1179. {
  1180. struct nfs_page *req;
  1181. while (!list_empty(head)) {
  1182. req = nfs_list_entry(head->next);
  1183. nfs_list_remove_request(req);
  1184. nfs_redirty_request(req);
  1185. }
  1186. }
  1187. static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr)
  1188. {
  1189. nfs_async_write_error(&hdr->pages);
  1190. }
  1191. static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = {
  1192. .error_cleanup = nfs_async_write_error,
  1193. .completion = nfs_write_completion,
  1194. .reschedule_io = nfs_async_write_reschedule_io,
  1195. };
  1196. void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio,
  1197. struct inode *inode, int ioflags, bool force_mds,
  1198. const struct nfs_pgio_completion_ops *compl_ops)
  1199. {
  1200. struct nfs_server *server = NFS_SERVER(inode);
  1201. const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
  1202. #ifdef CONFIG_NFS_V4_1
  1203. if (server->pnfs_curr_ld && !force_mds)
  1204. pg_ops = server->pnfs_curr_ld->pg_write_ops;
  1205. #endif
  1206. nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops,
  1207. server->wsize, ioflags, GFP_NOIO);
  1208. }
  1209. EXPORT_SYMBOL_GPL(nfs_pageio_init_write);
  1210. void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio)
  1211. {
  1212. struct nfs_pgio_mirror *mirror;
  1213. if (pgio->pg_ops && pgio->pg_ops->pg_cleanup)
  1214. pgio->pg_ops->pg_cleanup(pgio);
  1215. pgio->pg_ops = &nfs_pgio_rw_ops;
  1216. nfs_pageio_stop_mirroring(pgio);
  1217. mirror = &pgio->pg_mirrors[0];
  1218. mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize;
  1219. }
  1220. EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds);
  1221. void nfs_commit_prepare(struct rpc_task *task, void *calldata)
  1222. {
  1223. struct nfs_commit_data *data = calldata;
  1224. NFS_PROTO(data->inode)->commit_rpc_prepare(task, data);
  1225. }
  1226. /*
  1227. * Special version of should_remove_suid() that ignores capabilities.
  1228. */
  1229. static int nfs_should_remove_suid(const struct inode *inode)
  1230. {
  1231. umode_t mode = inode->i_mode;
  1232. int kill = 0;
  1233. /* suid always must be killed */
  1234. if (unlikely(mode & S_ISUID))
  1235. kill = ATTR_KILL_SUID;
  1236. /*
  1237. * sgid without any exec bits is just a mandatory locking mark; leave
  1238. * it alone. If some exec bits are set, it's a real sgid; kill it.
  1239. */
  1240. if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
  1241. kill |= ATTR_KILL_SGID;
  1242. if (unlikely(kill && S_ISREG(mode)))
  1243. return kill;
  1244. return 0;
  1245. }
  1246. static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr,
  1247. struct nfs_fattr *fattr)
  1248. {
  1249. struct nfs_pgio_args *argp = &hdr->args;
  1250. struct nfs_pgio_res *resp = &hdr->res;
  1251. u64 size = argp->offset + resp->count;
  1252. if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
  1253. fattr->size = size;
  1254. if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) {
  1255. fattr->valid &= ~NFS_ATTR_FATTR_SIZE;
  1256. return;
  1257. }
  1258. if (size != fattr->size)
  1259. return;
  1260. /* Set attribute barrier */
  1261. nfs_fattr_set_barrier(fattr);
  1262. /* ...and update size */
  1263. fattr->valid |= NFS_ATTR_FATTR_SIZE;
  1264. }
  1265. void nfs_writeback_update_inode(struct nfs_pgio_header *hdr)
  1266. {
  1267. struct nfs_fattr *fattr = &hdr->fattr;
  1268. struct inode *inode = hdr->inode;
  1269. spin_lock(&inode->i_lock);
  1270. nfs_writeback_check_extend(hdr, fattr);
  1271. nfs_post_op_update_inode_force_wcc_locked(inode, fattr);
  1272. spin_unlock(&inode->i_lock);
  1273. }
  1274. EXPORT_SYMBOL_GPL(nfs_writeback_update_inode);
  1275. /*
  1276. * This function is called when the WRITE call is complete.
  1277. */
  1278. static int nfs_writeback_done(struct rpc_task *task,
  1279. struct nfs_pgio_header *hdr,
  1280. struct inode *inode)
  1281. {
  1282. int status;
  1283. /*
  1284. * ->write_done will attempt to use post-op attributes to detect
  1285. * conflicting writes by other clients. A strict interpretation
  1286. * of close-to-open would allow us to continue caching even if
  1287. * another writer had changed the file, but some applications
  1288. * depend on tighter cache coherency when writing.
  1289. */
  1290. status = NFS_PROTO(inode)->write_done(task, hdr);
  1291. if (status != 0)
  1292. return status;
  1293. nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count);
  1294. if (hdr->res.verf->committed < hdr->args.stable &&
  1295. task->tk_status >= 0) {
  1296. /* We tried a write call, but the server did not
  1297. * commit data to stable storage even though we
  1298. * requested it.
  1299. * Note: There is a known bug in Tru64 < 5.0 in which
  1300. * the server reports NFS_DATA_SYNC, but performs
  1301. * NFS_FILE_SYNC. We therefore implement this checking
  1302. * as a dprintk() in order to avoid filling syslog.
  1303. */
  1304. static unsigned long complain;
  1305. /* Note this will print the MDS for a DS write */
  1306. if (time_before(complain, jiffies)) {
  1307. dprintk("NFS: faulty NFS server %s:"
  1308. " (committed = %d) != (stable = %d)\n",
  1309. NFS_SERVER(inode)->nfs_client->cl_hostname,
  1310. hdr->res.verf->committed, hdr->args.stable);
  1311. complain = jiffies + 300 * HZ;
  1312. }
  1313. }
  1314. /* Deal with the suid/sgid bit corner case */
  1315. if (nfs_should_remove_suid(inode))
  1316. nfs_mark_for_revalidate(inode);
  1317. return 0;
  1318. }
  1319. /*
  1320. * This function is called when the WRITE call is complete.
  1321. */
  1322. static void nfs_writeback_result(struct rpc_task *task,
  1323. struct nfs_pgio_header *hdr)
  1324. {
  1325. struct nfs_pgio_args *argp = &hdr->args;
  1326. struct nfs_pgio_res *resp = &hdr->res;
  1327. if (resp->count < argp->count) {
  1328. static unsigned long complain;
  1329. /* This a short write! */
  1330. nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE);
  1331. /* Has the server at least made some progress? */
  1332. if (resp->count == 0) {
  1333. if (time_before(complain, jiffies)) {
  1334. printk(KERN_WARNING
  1335. "NFS: Server wrote zero bytes, expected %u.\n",
  1336. argp->count);
  1337. complain = jiffies + 300 * HZ;
  1338. }
  1339. nfs_set_pgio_error(hdr, -EIO, argp->offset);
  1340. task->tk_status = -EIO;
  1341. return;
  1342. }
  1343. /* For non rpc-based layout drivers, retry-through-MDS */
  1344. if (!task->tk_ops) {
  1345. hdr->pnfs_error = -EAGAIN;
  1346. return;
  1347. }
  1348. /* Was this an NFSv2 write or an NFSv3 stable write? */
  1349. if (resp->verf->committed != NFS_UNSTABLE) {
  1350. /* Resend from where the server left off */
  1351. hdr->mds_offset += resp->count;
  1352. argp->offset += resp->count;
  1353. argp->pgbase += resp->count;
  1354. argp->count -= resp->count;
  1355. } else {
  1356. /* Resend as a stable write in order to avoid
  1357. * headaches in the case of a server crash.
  1358. */
  1359. argp->stable = NFS_FILE_SYNC;
  1360. }
  1361. rpc_restart_call_prepare(task);
  1362. }
  1363. }
  1364. static int wait_on_commit(struct nfs_mds_commit_info *cinfo)
  1365. {
  1366. return wait_on_atomic_t(&cinfo->rpcs_out,
  1367. nfs_wait_atomic_killable, TASK_KILLABLE);
  1368. }
  1369. static void nfs_commit_begin(struct nfs_mds_commit_info *cinfo)
  1370. {
  1371. atomic_inc(&cinfo->rpcs_out);
  1372. }
  1373. static void nfs_commit_end(struct nfs_mds_commit_info *cinfo)
  1374. {
  1375. if (atomic_dec_and_test(&cinfo->rpcs_out))
  1376. wake_up_atomic_t(&cinfo->rpcs_out);
  1377. }
  1378. void nfs_commitdata_release(struct nfs_commit_data *data)
  1379. {
  1380. put_nfs_open_context(data->context);
  1381. nfs_commit_free(data);
  1382. }
  1383. EXPORT_SYMBOL_GPL(nfs_commitdata_release);
  1384. int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data,
  1385. const struct nfs_rpc_ops *nfs_ops,
  1386. const struct rpc_call_ops *call_ops,
  1387. int how, int flags)
  1388. {
  1389. struct rpc_task *task;
  1390. int priority = flush_task_priority(how);
  1391. struct rpc_message msg = {
  1392. .rpc_argp = &data->args,
  1393. .rpc_resp = &data->res,
  1394. .rpc_cred = data->cred,
  1395. };
  1396. struct rpc_task_setup task_setup_data = {
  1397. .task = &data->task,
  1398. .rpc_client = clnt,
  1399. .rpc_message = &msg,
  1400. .callback_ops = call_ops,
  1401. .callback_data = data,
  1402. .workqueue = nfsiod_workqueue,
  1403. .flags = RPC_TASK_ASYNC | flags,
  1404. .priority = priority,
  1405. };
  1406. /* Set up the initial task struct. */
  1407. nfs_ops->commit_setup(data, &msg);
  1408. dprintk("NFS: initiated commit call\n");
  1409. nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client,
  1410. NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg);
  1411. task = rpc_run_task(&task_setup_data);
  1412. if (IS_ERR(task))
  1413. return PTR_ERR(task);
  1414. if (how & FLUSH_SYNC)
  1415. rpc_wait_for_completion_task(task);
  1416. rpc_put_task(task);
  1417. return 0;
  1418. }
  1419. EXPORT_SYMBOL_GPL(nfs_initiate_commit);
  1420. static loff_t nfs_get_lwb(struct list_head *head)
  1421. {
  1422. loff_t lwb = 0;
  1423. struct nfs_page *req;
  1424. list_for_each_entry(req, head, wb_list)
  1425. if (lwb < (req_offset(req) + req->wb_bytes))
  1426. lwb = req_offset(req) + req->wb_bytes;
  1427. return lwb;
  1428. }
  1429. /*
  1430. * Set up the argument/result storage required for the RPC call.
  1431. */
  1432. void nfs_init_commit(struct nfs_commit_data *data,
  1433. struct list_head *head,
  1434. struct pnfs_layout_segment *lseg,
  1435. struct nfs_commit_info *cinfo)
  1436. {
  1437. struct nfs_page *first = nfs_list_entry(head->next);
  1438. struct inode *inode = d_inode(first->wb_context->dentry);
  1439. /* Set up the RPC argument and reply structs
  1440. * NB: take care not to mess about with data->commit et al. */
  1441. list_splice_init(head, &data->pages);
  1442. data->inode = inode;
  1443. data->cred = first->wb_context->cred;
  1444. data->lseg = lseg; /* reference transferred */
  1445. /* only set lwb for pnfs commit */
  1446. if (lseg)
  1447. data->lwb = nfs_get_lwb(&data->pages);
  1448. data->mds_ops = &nfs_commit_ops;
  1449. data->completion_ops = cinfo->completion_ops;
  1450. data->dreq = cinfo->dreq;
  1451. data->args.fh = NFS_FH(data->inode);
  1452. /* Note: we always request a commit of the entire inode */
  1453. data->args.offset = 0;
  1454. data->args.count = 0;
  1455. data->context = get_nfs_open_context(first->wb_context);
  1456. data->res.fattr = &data->fattr;
  1457. data->res.verf = &data->verf;
  1458. nfs_fattr_init(&data->fattr);
  1459. }
  1460. EXPORT_SYMBOL_GPL(nfs_init_commit);
  1461. void nfs_retry_commit(struct list_head *page_list,
  1462. struct pnfs_layout_segment *lseg,
  1463. struct nfs_commit_info *cinfo,
  1464. u32 ds_commit_idx)
  1465. {
  1466. struct nfs_page *req;
  1467. while (!list_empty(page_list)) {
  1468. req = nfs_list_entry(page_list->next);
  1469. nfs_list_remove_request(req);
  1470. nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx);
  1471. if (!cinfo->dreq)
  1472. nfs_clear_page_commit(req->wb_page);
  1473. nfs_unlock_and_release_request(req);
  1474. }
  1475. }
  1476. EXPORT_SYMBOL_GPL(nfs_retry_commit);
  1477. static void
  1478. nfs_commit_resched_write(struct nfs_commit_info *cinfo,
  1479. struct nfs_page *req)
  1480. {
  1481. __set_page_dirty_nobuffers(req->wb_page);
  1482. }
  1483. /*
  1484. * Commit dirty pages
  1485. */
  1486. static int
  1487. nfs_commit_list(struct inode *inode, struct list_head *head, int how,
  1488. struct nfs_commit_info *cinfo)
  1489. {
  1490. struct nfs_commit_data *data;
  1491. /* another commit raced with us */
  1492. if (list_empty(head))
  1493. return 0;
  1494. data = nfs_commitdata_alloc(true);
  1495. /* Set up the argument struct */
  1496. nfs_init_commit(data, head, NULL, cinfo);
  1497. atomic_inc(&cinfo->mds->rpcs_out);
  1498. return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode),
  1499. data->mds_ops, how, 0);
  1500. }
  1501. int nfs_commit_file(struct file *file, struct nfs_write_verifier *verf)
  1502. {
  1503. struct inode *inode = file_inode(file);
  1504. struct nfs_open_context *open;
  1505. struct nfs_commit_info cinfo;
  1506. struct nfs_page *req;
  1507. int ret;
  1508. open = get_nfs_open_context(nfs_file_open_context(file));
  1509. req = nfs_create_request(open, NULL, NULL, 0, i_size_read(inode));
  1510. if (IS_ERR(req)) {
  1511. ret = PTR_ERR(req);
  1512. goto out_put;
  1513. }
  1514. nfs_init_cinfo_from_inode(&cinfo, inode);
  1515. memcpy(&req->wb_verf, verf, sizeof(struct nfs_write_verifier));
  1516. nfs_request_add_commit_list(req, &cinfo);
  1517. ret = nfs_commit_inode(inode, FLUSH_SYNC);
  1518. if (ret > 0)
  1519. ret = 0;
  1520. nfs_free_request(req);
  1521. out_put:
  1522. put_nfs_open_context(open);
  1523. return ret;
  1524. }
  1525. EXPORT_SYMBOL_GPL(nfs_commit_file);
  1526. /*
  1527. * COMMIT call returned
  1528. */
  1529. static void nfs_commit_done(struct rpc_task *task, void *calldata)
  1530. {
  1531. struct nfs_commit_data *data = calldata;
  1532. dprintk("NFS: %5u nfs_commit_done (status %d)\n",
  1533. task->tk_pid, task->tk_status);
  1534. /* Call the NFS version-specific code */
  1535. NFS_PROTO(data->inode)->commit_done(task, data);
  1536. }
  1537. static void nfs_commit_release_pages(struct nfs_commit_data *data)
  1538. {
  1539. struct nfs_page *req;
  1540. int status = data->task.tk_status;
  1541. struct nfs_commit_info cinfo;
  1542. struct nfs_server *nfss;
  1543. while (!list_empty(&data->pages)) {
  1544. req = nfs_list_entry(data->pages.next);
  1545. nfs_list_remove_request(req);
  1546. if (req->wb_page)
  1547. nfs_clear_page_commit(req->wb_page);
  1548. dprintk("NFS: commit (%s/%llu %d@%lld)",
  1549. req->wb_context->dentry->d_sb->s_id,
  1550. (unsigned long long)NFS_FILEID(d_inode(req->wb_context->dentry)),
  1551. req->wb_bytes,
  1552. (long long)req_offset(req));
  1553. if (status < 0) {
  1554. nfs_context_set_write_error(req->wb_context, status);
  1555. if (req->wb_page)
  1556. nfs_inode_remove_request(req);
  1557. dprintk_cont(", error = %d\n", status);
  1558. goto next;
  1559. }
  1560. /* Okay, COMMIT succeeded, apparently. Check the verifier
  1561. * returned by the server against all stored verfs. */
  1562. if (!nfs_write_verifier_cmp(&req->wb_verf, &data->verf.verifier)) {
  1563. /* We have a match */
  1564. if (req->wb_page)
  1565. nfs_inode_remove_request(req);
  1566. dprintk_cont(" OK\n");
  1567. goto next;
  1568. }
  1569. /* We have a mismatch. Write the page again */
  1570. dprintk_cont(" mismatch\n");
  1571. nfs_mark_request_dirty(req);
  1572. set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags);
  1573. next:
  1574. nfs_unlock_and_release_request(req);
  1575. }
  1576. nfss = NFS_SERVER(data->inode);
  1577. if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH)
  1578. clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC);
  1579. nfs_init_cinfo(&cinfo, data->inode, data->dreq);
  1580. nfs_commit_end(cinfo.mds);
  1581. }
  1582. static void nfs_commit_release(void *calldata)
  1583. {
  1584. struct nfs_commit_data *data = calldata;
  1585. data->completion_ops->completion(data);
  1586. nfs_commitdata_release(calldata);
  1587. }
  1588. static const struct rpc_call_ops nfs_commit_ops = {
  1589. .rpc_call_prepare = nfs_commit_prepare,
  1590. .rpc_call_done = nfs_commit_done,
  1591. .rpc_release = nfs_commit_release,
  1592. };
  1593. static const struct nfs_commit_completion_ops nfs_commit_completion_ops = {
  1594. .completion = nfs_commit_release_pages,
  1595. .resched_write = nfs_commit_resched_write,
  1596. };
  1597. int nfs_generic_commit_list(struct inode *inode, struct list_head *head,
  1598. int how, struct nfs_commit_info *cinfo)
  1599. {
  1600. int status;
  1601. status = pnfs_commit_list(inode, head, how, cinfo);
  1602. if (status == PNFS_NOT_ATTEMPTED)
  1603. status = nfs_commit_list(inode, head, how, cinfo);
  1604. return status;
  1605. }
  1606. int nfs_commit_inode(struct inode *inode, int how)
  1607. {
  1608. LIST_HEAD(head);
  1609. struct nfs_commit_info cinfo;
  1610. int may_wait = how & FLUSH_SYNC;
  1611. int error = 0;
  1612. int res;
  1613. nfs_init_cinfo_from_inode(&cinfo, inode);
  1614. nfs_commit_begin(cinfo.mds);
  1615. res = nfs_scan_commit(inode, &head, &cinfo);
  1616. if (res)
  1617. error = nfs_generic_commit_list(inode, &head, how, &cinfo);
  1618. nfs_commit_end(cinfo.mds);
  1619. if (error < 0)
  1620. goto out_error;
  1621. if (!may_wait)
  1622. goto out_mark_dirty;
  1623. error = wait_on_commit(cinfo.mds);
  1624. if (error < 0)
  1625. return error;
  1626. return res;
  1627. out_error:
  1628. res = error;
  1629. /* Note: If we exit without ensuring that the commit is complete,
  1630. * we must mark the inode as dirty. Otherwise, future calls to
  1631. * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure
  1632. * that the data is on the disk.
  1633. */
  1634. out_mark_dirty:
  1635. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  1636. return res;
  1637. }
  1638. EXPORT_SYMBOL_GPL(nfs_commit_inode);
  1639. int nfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  1640. {
  1641. struct nfs_inode *nfsi = NFS_I(inode);
  1642. int flags = FLUSH_SYNC;
  1643. int ret = 0;
  1644. /* no commits means nothing needs to be done */
  1645. if (!nfsi->commit_info.ncommit)
  1646. return ret;
  1647. if (wbc->sync_mode == WB_SYNC_NONE) {
  1648. /* Don't commit yet if this is a non-blocking flush and there
  1649. * are a lot of outstanding writes for this mapping.
  1650. */
  1651. if (nfsi->commit_info.ncommit <= (nfsi->nrequests >> 1))
  1652. goto out_mark_dirty;
  1653. /* don't wait for the COMMIT response */
  1654. flags = 0;
  1655. }
  1656. ret = nfs_commit_inode(inode, flags);
  1657. if (ret >= 0) {
  1658. if (wbc->sync_mode == WB_SYNC_NONE) {
  1659. if (ret < wbc->nr_to_write)
  1660. wbc->nr_to_write -= ret;
  1661. else
  1662. wbc->nr_to_write = 0;
  1663. }
  1664. return 0;
  1665. }
  1666. out_mark_dirty:
  1667. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  1668. return ret;
  1669. }
  1670. EXPORT_SYMBOL_GPL(nfs_write_inode);
  1671. /*
  1672. * Wrapper for filemap_write_and_wait_range()
  1673. *
  1674. * Needed for pNFS in order to ensure data becomes visible to the
  1675. * client.
  1676. */
  1677. int nfs_filemap_write_and_wait_range(struct address_space *mapping,
  1678. loff_t lstart, loff_t lend)
  1679. {
  1680. int ret;
  1681. ret = filemap_write_and_wait_range(mapping, lstart, lend);
  1682. if (ret == 0)
  1683. ret = pnfs_sync_inode(mapping->host, true);
  1684. return ret;
  1685. }
  1686. EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range);
  1687. /*
  1688. * flush the inode to disk.
  1689. */
  1690. int nfs_wb_all(struct inode *inode)
  1691. {
  1692. int ret;
  1693. trace_nfs_writeback_inode_enter(inode);
  1694. ret = filemap_write_and_wait(inode->i_mapping);
  1695. if (ret)
  1696. goto out;
  1697. ret = nfs_commit_inode(inode, FLUSH_SYNC);
  1698. if (ret < 0)
  1699. goto out;
  1700. pnfs_sync_inode(inode, true);
  1701. ret = 0;
  1702. out:
  1703. trace_nfs_writeback_inode_exit(inode, ret);
  1704. return ret;
  1705. }
  1706. EXPORT_SYMBOL_GPL(nfs_wb_all);
  1707. int nfs_wb_page_cancel(struct inode *inode, struct page *page)
  1708. {
  1709. struct nfs_page *req;
  1710. int ret = 0;
  1711. wait_on_page_writeback(page);
  1712. /* blocking call to cancel all requests and join to a single (head)
  1713. * request */
  1714. req = nfs_lock_and_join_requests(page, false);
  1715. if (IS_ERR(req)) {
  1716. ret = PTR_ERR(req);
  1717. } else if (req) {
  1718. /* all requests from this page have been cancelled by
  1719. * nfs_lock_and_join_requests, so just remove the head
  1720. * request from the inode / page_private pointer and
  1721. * release it */
  1722. nfs_inode_remove_request(req);
  1723. nfs_unlock_and_release_request(req);
  1724. }
  1725. return ret;
  1726. }
  1727. /*
  1728. * Write back all requests on one page - we do this before reading it.
  1729. */
  1730. int nfs_wb_single_page(struct inode *inode, struct page *page, bool launder)
  1731. {
  1732. loff_t range_start = page_file_offset(page);
  1733. loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1);
  1734. struct writeback_control wbc = {
  1735. .sync_mode = WB_SYNC_ALL,
  1736. .nr_to_write = 0,
  1737. .range_start = range_start,
  1738. .range_end = range_end,
  1739. };
  1740. int ret;
  1741. trace_nfs_writeback_page_enter(inode);
  1742. for (;;) {
  1743. wait_on_page_writeback(page);
  1744. if (clear_page_dirty_for_io(page)) {
  1745. ret = nfs_writepage_locked(page, &wbc, launder);
  1746. if (ret < 0)
  1747. goto out_error;
  1748. continue;
  1749. }
  1750. ret = 0;
  1751. if (!PagePrivate(page))
  1752. break;
  1753. ret = nfs_commit_inode(inode, FLUSH_SYNC);
  1754. if (ret < 0)
  1755. goto out_error;
  1756. }
  1757. out_error:
  1758. trace_nfs_writeback_page_exit(inode, ret);
  1759. return ret;
  1760. }
  1761. #ifdef CONFIG_MIGRATION
  1762. int nfs_migrate_page(struct address_space *mapping, struct page *newpage,
  1763. struct page *page, enum migrate_mode mode)
  1764. {
  1765. /*
  1766. * If PagePrivate is set, then the page is currently associated with
  1767. * an in-progress read or write request. Don't try to migrate it.
  1768. *
  1769. * FIXME: we could do this in principle, but we'll need a way to ensure
  1770. * that we can safely release the inode reference while holding
  1771. * the page lock.
  1772. */
  1773. if (PagePrivate(page))
  1774. return -EBUSY;
  1775. if (!nfs_fscache_release_page(page, GFP_KERNEL))
  1776. return -EBUSY;
  1777. return migrate_page(mapping, newpage, page, mode);
  1778. }
  1779. #endif
  1780. int __init nfs_init_writepagecache(void)
  1781. {
  1782. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1783. sizeof(struct nfs_pgio_header),
  1784. 0, SLAB_HWCACHE_ALIGN,
  1785. NULL);
  1786. if (nfs_wdata_cachep == NULL)
  1787. return -ENOMEM;
  1788. nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
  1789. nfs_wdata_cachep);
  1790. if (nfs_wdata_mempool == NULL)
  1791. goto out_destroy_write_cache;
  1792. nfs_cdata_cachep = kmem_cache_create("nfs_commit_data",
  1793. sizeof(struct nfs_commit_data),
  1794. 0, SLAB_HWCACHE_ALIGN,
  1795. NULL);
  1796. if (nfs_cdata_cachep == NULL)
  1797. goto out_destroy_write_mempool;
  1798. nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
  1799. nfs_cdata_cachep);
  1800. if (nfs_commit_mempool == NULL)
  1801. goto out_destroy_commit_cache;
  1802. /*
  1803. * NFS congestion size, scale with available memory.
  1804. *
  1805. * 64MB: 8192k
  1806. * 128MB: 11585k
  1807. * 256MB: 16384k
  1808. * 512MB: 23170k
  1809. * 1GB: 32768k
  1810. * 2GB: 46340k
  1811. * 4GB: 65536k
  1812. * 8GB: 92681k
  1813. * 16GB: 131072k
  1814. *
  1815. * This allows larger machines to have larger/more transfers.
  1816. * Limit the default to 256M
  1817. */
  1818. nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
  1819. if (nfs_congestion_kb > 256*1024)
  1820. nfs_congestion_kb = 256*1024;
  1821. return 0;
  1822. out_destroy_commit_cache:
  1823. kmem_cache_destroy(nfs_cdata_cachep);
  1824. out_destroy_write_mempool:
  1825. mempool_destroy(nfs_wdata_mempool);
  1826. out_destroy_write_cache:
  1827. kmem_cache_destroy(nfs_wdata_cachep);
  1828. return -ENOMEM;
  1829. }
  1830. void nfs_destroy_writepagecache(void)
  1831. {
  1832. mempool_destroy(nfs_commit_mempool);
  1833. kmem_cache_destroy(nfs_cdata_cachep);
  1834. mempool_destroy(nfs_wdata_mempool);
  1835. kmem_cache_destroy(nfs_wdata_cachep);
  1836. }
  1837. static const struct nfs_rw_ops nfs_rw_write_ops = {
  1838. .rw_alloc_header = nfs_writehdr_alloc,
  1839. .rw_free_header = nfs_writehdr_free,
  1840. .rw_done = nfs_writeback_done,
  1841. .rw_result = nfs_writeback_result,
  1842. .rw_initiate = nfs_initiate_write,
  1843. };