write.c 54 KB

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