write.c 54 KB

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