write.c 51 KB

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