write.c 57 KB

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