write.c 57 KB

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