write.c 55 KB

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