write.c 55 KB

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