write.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577
  1. /*
  2. * linux/fs/nfs/write.c
  3. *
  4. * Writing file data over NFS.
  5. *
  6. * We do it like this: When a (user) process wishes to write data to an
  7. * NFS file, a write request is allocated that contains the RPC task data
  8. * plus some info on the page to be written, and added to the inode's
  9. * write chain. If the process writes past the end of the page, an async
  10. * RPC call to write the page is scheduled immediately; otherwise, the call
  11. * is delayed for a few seconds.
  12. *
  13. * Just like readahead, no async I/O is performed if wsize < PAGE_SIZE.
  14. *
  15. * Write requests are kept on the inode's writeback list. Each entry in
  16. * that list references the page (portion) to be written. When the
  17. * cache timeout has expired, the RPC task is woken up, and tries to
  18. * lock the page. As soon as it manages to do so, the request is moved
  19. * from the writeback list to the writelock list.
  20. *
  21. * Note: we must make sure never to confuse the inode passed in the
  22. * write_page request with the one in page->inode. As far as I understand
  23. * it, these are different when doing a swap-out.
  24. *
  25. * To understand everything that goes on here and in the NFS read code,
  26. * one should be aware that a page is locked in exactly one of the following
  27. * cases:
  28. *
  29. * - A write request is in progress.
  30. * - A user process is in generic_file_write/nfs_update_page
  31. * - A user process is in generic_file_read
  32. *
  33. * Also note that because of the way pages are invalidated in
  34. * nfs_revalidate_inode, the following assertions hold:
  35. *
  36. * - If a page is dirty, there will be no read requests (a page will
  37. * not be re-read unless invalidated by nfs_revalidate_inode).
  38. * - If the page is not uptodate, there will be no pending write
  39. * requests, and no process will be in nfs_update_page.
  40. *
  41. * FIXME: Interaction with the vmscan routines is not optimal yet.
  42. * Either vmscan must be made nfs-savvy, or we need a different page
  43. * reclaim concept that supports something like FS-independent
  44. * buffer_heads with a b_ops-> field.
  45. *
  46. * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de>
  47. */
  48. #include <linux/types.h>
  49. #include <linux/slab.h>
  50. #include <linux/mm.h>
  51. #include <linux/pagemap.h>
  52. #include <linux/file.h>
  53. #include <linux/writeback.h>
  54. #include <linux/sunrpc/clnt.h>
  55. #include <linux/nfs_fs.h>
  56. #include <linux/nfs_mount.h>
  57. #include <linux/nfs_page.h>
  58. #include <linux/backing-dev.h>
  59. #include <asm/uaccess.h>
  60. #include <linux/smp_lock.h>
  61. #include "delegation.h"
  62. #include "internal.h"
  63. #include "iostat.h"
  64. #define NFSDBG_FACILITY NFSDBG_PAGECACHE
  65. #define MIN_POOL_WRITE (32)
  66. #define MIN_POOL_COMMIT (4)
  67. /*
  68. * Local function declarations
  69. */
  70. static struct nfs_page * nfs_update_request(struct nfs_open_context*,
  71. struct page *,
  72. unsigned int, unsigned int);
  73. static void nfs_mark_request_dirty(struct nfs_page *req);
  74. static int nfs_wait_on_write_congestion(struct address_space *, int);
  75. static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int);
  76. static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how);
  77. static const struct rpc_call_ops nfs_write_partial_ops;
  78. static const struct rpc_call_ops nfs_write_full_ops;
  79. static const struct rpc_call_ops nfs_commit_ops;
  80. static struct kmem_cache *nfs_wdata_cachep;
  81. static mempool_t *nfs_wdata_mempool;
  82. static mempool_t *nfs_commit_mempool;
  83. static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion);
  84. struct nfs_write_data *nfs_commit_alloc(void)
  85. {
  86. struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS);
  87. if (p) {
  88. memset(p, 0, sizeof(*p));
  89. INIT_LIST_HEAD(&p->pages);
  90. }
  91. return p;
  92. }
  93. void nfs_commit_rcu_free(struct rcu_head *head)
  94. {
  95. struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
  96. if (p && (p->pagevec != &p->page_array[0]))
  97. kfree(p->pagevec);
  98. mempool_free(p, nfs_commit_mempool);
  99. }
  100. void nfs_commit_free(struct nfs_write_data *wdata)
  101. {
  102. call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free);
  103. }
  104. struct nfs_write_data *nfs_writedata_alloc(size_t len)
  105. {
  106. unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  107. struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS);
  108. if (p) {
  109. memset(p, 0, sizeof(*p));
  110. INIT_LIST_HEAD(&p->pages);
  111. p->npages = pagecount;
  112. if (pagecount <= ARRAY_SIZE(p->page_array))
  113. p->pagevec = p->page_array;
  114. else {
  115. p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
  116. if (!p->pagevec) {
  117. mempool_free(p, nfs_wdata_mempool);
  118. p = NULL;
  119. }
  120. }
  121. }
  122. return p;
  123. }
  124. static void nfs_writedata_rcu_free(struct rcu_head *head)
  125. {
  126. struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu);
  127. if (p && (p->pagevec != &p->page_array[0]))
  128. kfree(p->pagevec);
  129. mempool_free(p, nfs_wdata_mempool);
  130. }
  131. static void nfs_writedata_free(struct nfs_write_data *wdata)
  132. {
  133. call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free);
  134. }
  135. void nfs_writedata_release(void *wdata)
  136. {
  137. nfs_writedata_free(wdata);
  138. }
  139. static struct nfs_page *nfs_page_find_request_locked(struct page *page)
  140. {
  141. struct nfs_page *req = NULL;
  142. if (PagePrivate(page)) {
  143. req = (struct nfs_page *)page_private(page);
  144. if (req != NULL)
  145. atomic_inc(&req->wb_count);
  146. }
  147. return req;
  148. }
  149. static struct nfs_page *nfs_page_find_request(struct page *page)
  150. {
  151. struct nfs_page *req = NULL;
  152. spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
  153. spin_lock(req_lock);
  154. req = nfs_page_find_request_locked(page);
  155. spin_unlock(req_lock);
  156. return req;
  157. }
  158. /* Adjust the file length if we're writing beyond the end */
  159. static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count)
  160. {
  161. struct inode *inode = page->mapping->host;
  162. loff_t end, i_size = i_size_read(inode);
  163. unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
  164. if (i_size > 0 && page->index < end_index)
  165. return;
  166. end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count);
  167. if (i_size >= end)
  168. return;
  169. nfs_inc_stats(inode, NFSIOS_EXTENDWRITE);
  170. i_size_write(inode, end);
  171. }
  172. /* We can set the PG_uptodate flag if we see that a write request
  173. * covers the full page.
  174. */
  175. static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count)
  176. {
  177. if (PageUptodate(page))
  178. return;
  179. if (base != 0)
  180. return;
  181. if (count != nfs_page_length(page))
  182. return;
  183. if (count != PAGE_CACHE_SIZE)
  184. memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count);
  185. SetPageUptodate(page);
  186. }
  187. static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page,
  188. unsigned int offset, unsigned int count)
  189. {
  190. struct nfs_page *req;
  191. int ret;
  192. for (;;) {
  193. req = nfs_update_request(ctx, page, offset, count);
  194. if (!IS_ERR(req))
  195. break;
  196. ret = PTR_ERR(req);
  197. if (ret != -EBUSY)
  198. return ret;
  199. ret = nfs_wb_page(page->mapping->host, page);
  200. if (ret != 0)
  201. return ret;
  202. }
  203. /* Update file length */
  204. nfs_grow_file(page, offset, count);
  205. /* Set the PG_uptodate flag? */
  206. nfs_mark_uptodate(page, offset, count);
  207. nfs_unlock_request(req);
  208. return 0;
  209. }
  210. static int wb_priority(struct writeback_control *wbc)
  211. {
  212. if (wbc->for_reclaim)
  213. return FLUSH_HIGHPRI;
  214. if (wbc->for_kupdate)
  215. return FLUSH_LOWPRI;
  216. return 0;
  217. }
  218. /*
  219. * Find an associated nfs write request, and prepare to flush it out
  220. * Returns 1 if there was no write request, or if the request was
  221. * already tagged by nfs_set_page_dirty.Returns 0 if the request
  222. * was not tagged.
  223. * May also return an error if the user signalled nfs_wait_on_request().
  224. */
  225. static int nfs_page_mark_flush(struct page *page)
  226. {
  227. struct nfs_page *req;
  228. spinlock_t *req_lock = &NFS_I(page->mapping->host)->req_lock;
  229. int ret;
  230. spin_lock(req_lock);
  231. for(;;) {
  232. req = nfs_page_find_request_locked(page);
  233. if (req == NULL) {
  234. spin_unlock(req_lock);
  235. return 1;
  236. }
  237. if (nfs_lock_request_dontget(req))
  238. break;
  239. /* Note: If we hold the page lock, as is the case in nfs_writepage,
  240. * then the call to nfs_lock_request_dontget() will always
  241. * succeed provided that someone hasn't already marked the
  242. * request as dirty (in which case we don't care).
  243. */
  244. spin_unlock(req_lock);
  245. ret = nfs_wait_on_request(req);
  246. nfs_release_request(req);
  247. if (ret != 0)
  248. return ret;
  249. spin_lock(req_lock);
  250. }
  251. spin_unlock(req_lock);
  252. if (test_and_set_bit(PG_FLUSHING, &req->wb_flags) == 0) {
  253. nfs_mark_request_dirty(req);
  254. set_page_writeback(page);
  255. }
  256. ret = test_bit(PG_NEED_FLUSH, &req->wb_flags);
  257. nfs_unlock_request(req);
  258. return ret;
  259. }
  260. /*
  261. * Write an mmapped page to the server.
  262. */
  263. static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc)
  264. {
  265. struct nfs_open_context *ctx;
  266. struct inode *inode = page->mapping->host;
  267. unsigned offset;
  268. int err;
  269. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE);
  270. nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1);
  271. err = nfs_page_mark_flush(page);
  272. if (err <= 0)
  273. goto out;
  274. err = 0;
  275. offset = nfs_page_length(page);
  276. if (!offset)
  277. goto out;
  278. ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE);
  279. if (ctx == NULL) {
  280. err = -EBADF;
  281. goto out;
  282. }
  283. err = nfs_writepage_setup(ctx, page, 0, offset);
  284. put_nfs_open_context(ctx);
  285. if (err != 0)
  286. goto out;
  287. err = nfs_page_mark_flush(page);
  288. if (err > 0)
  289. err = 0;
  290. out:
  291. if (!wbc->for_writepages)
  292. nfs_flush_mapping(page->mapping, wbc, FLUSH_STABLE|wb_priority(wbc));
  293. return err;
  294. }
  295. int nfs_writepage(struct page *page, struct writeback_control *wbc)
  296. {
  297. int err;
  298. err = nfs_writepage_locked(page, wbc);
  299. unlock_page(page);
  300. return err;
  301. }
  302. /*
  303. * Note: causes nfs_update_request() to block on the assumption
  304. * that the writeback is generated due to memory pressure.
  305. */
  306. int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc)
  307. {
  308. struct backing_dev_info *bdi = mapping->backing_dev_info;
  309. struct inode *inode = mapping->host;
  310. int err;
  311. nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES);
  312. err = generic_writepages(mapping, wbc);
  313. if (err)
  314. return err;
  315. while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) {
  316. if (wbc->nonblocking)
  317. return 0;
  318. nfs_wait_on_write_congestion(mapping, 0);
  319. }
  320. err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc));
  321. if (err < 0)
  322. goto out;
  323. nfs_add_stats(inode, NFSIOS_WRITEPAGES, err);
  324. if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) {
  325. err = nfs_wait_on_requests(inode, 0, 0);
  326. if (err < 0)
  327. goto out;
  328. }
  329. err = nfs_commit_inode(inode, wb_priority(wbc));
  330. if (err > 0)
  331. err = 0;
  332. out:
  333. clear_bit(BDI_write_congested, &bdi->state);
  334. wake_up_all(&nfs_write_congestion);
  335. congestion_end(WRITE);
  336. return err;
  337. }
  338. /*
  339. * Insert a write request into an inode
  340. */
  341. static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req)
  342. {
  343. struct nfs_inode *nfsi = NFS_I(inode);
  344. int error;
  345. error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req);
  346. BUG_ON(error == -EEXIST);
  347. if (error)
  348. return error;
  349. if (!nfsi->npages) {
  350. igrab(inode);
  351. nfs_begin_data_update(inode);
  352. if (nfs_have_delegation(inode, FMODE_WRITE))
  353. nfsi->change_attr++;
  354. }
  355. SetPagePrivate(req->wb_page);
  356. set_page_private(req->wb_page, (unsigned long)req);
  357. nfsi->npages++;
  358. atomic_inc(&req->wb_count);
  359. return 0;
  360. }
  361. /*
  362. * Insert a write request into an inode
  363. */
  364. static void nfs_inode_remove_request(struct nfs_page *req)
  365. {
  366. struct inode *inode = req->wb_context->dentry->d_inode;
  367. struct nfs_inode *nfsi = NFS_I(inode);
  368. BUG_ON (!NFS_WBACK_BUSY(req));
  369. spin_lock(&nfsi->req_lock);
  370. set_page_private(req->wb_page, 0);
  371. ClearPagePrivate(req->wb_page);
  372. radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index);
  373. nfsi->npages--;
  374. if (!nfsi->npages) {
  375. spin_unlock(&nfsi->req_lock);
  376. nfs_end_data_update(inode);
  377. iput(inode);
  378. } else
  379. spin_unlock(&nfsi->req_lock);
  380. nfs_clear_request(req);
  381. nfs_release_request(req);
  382. }
  383. /*
  384. * Add a request to the inode's dirty list.
  385. */
  386. static void
  387. nfs_mark_request_dirty(struct nfs_page *req)
  388. {
  389. struct inode *inode = req->wb_context->dentry->d_inode;
  390. struct nfs_inode *nfsi = NFS_I(inode);
  391. spin_lock(&nfsi->req_lock);
  392. radix_tree_tag_set(&nfsi->nfs_page_tree,
  393. req->wb_index, NFS_PAGE_TAG_DIRTY);
  394. nfs_list_add_request(req, &nfsi->dirty);
  395. nfsi->ndirty++;
  396. spin_unlock(&nfsi->req_lock);
  397. __mark_inode_dirty(inode, I_DIRTY_PAGES);
  398. }
  399. static void
  400. nfs_redirty_request(struct nfs_page *req)
  401. {
  402. clear_bit(PG_FLUSHING, &req->wb_flags);
  403. __set_page_dirty_nobuffers(req->wb_page);
  404. }
  405. /*
  406. * Check if a request is dirty
  407. */
  408. static inline int
  409. nfs_dirty_request(struct nfs_page *req)
  410. {
  411. return test_bit(PG_FLUSHING, &req->wb_flags) == 0;
  412. }
  413. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  414. /*
  415. * Add a request to the inode's commit list.
  416. */
  417. static void
  418. nfs_mark_request_commit(struct nfs_page *req)
  419. {
  420. struct inode *inode = req->wb_context->dentry->d_inode;
  421. struct nfs_inode *nfsi = NFS_I(inode);
  422. spin_lock(&nfsi->req_lock);
  423. nfs_list_add_request(req, &nfsi->commit);
  424. nfsi->ncommit++;
  425. spin_unlock(&nfsi->req_lock);
  426. inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  427. __mark_inode_dirty(inode, I_DIRTY_DATASYNC);
  428. }
  429. #endif
  430. /*
  431. * Wait for a request to complete.
  432. *
  433. * Interruptible by signals only if mounted with intr flag.
  434. */
  435. static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages)
  436. {
  437. struct nfs_inode *nfsi = NFS_I(inode);
  438. struct nfs_page *req;
  439. unsigned long idx_end, next;
  440. unsigned int res = 0;
  441. int error;
  442. if (npages == 0)
  443. idx_end = ~0;
  444. else
  445. idx_end = idx_start + npages - 1;
  446. next = idx_start;
  447. while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) {
  448. if (req->wb_index > idx_end)
  449. break;
  450. next = req->wb_index + 1;
  451. BUG_ON(!NFS_WBACK_BUSY(req));
  452. atomic_inc(&req->wb_count);
  453. spin_unlock(&nfsi->req_lock);
  454. error = nfs_wait_on_request(req);
  455. nfs_release_request(req);
  456. spin_lock(&nfsi->req_lock);
  457. if (error < 0)
  458. return error;
  459. res++;
  460. }
  461. return res;
  462. }
  463. static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages)
  464. {
  465. struct nfs_inode *nfsi = NFS_I(inode);
  466. int ret;
  467. spin_lock(&nfsi->req_lock);
  468. ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
  469. spin_unlock(&nfsi->req_lock);
  470. return ret;
  471. }
  472. static void nfs_cancel_dirty_list(struct list_head *head)
  473. {
  474. struct nfs_page *req;
  475. while(!list_empty(head)) {
  476. req = nfs_list_entry(head->next);
  477. nfs_list_remove_request(req);
  478. nfs_inode_remove_request(req);
  479. nfs_clear_page_writeback(req);
  480. }
  481. }
  482. static void nfs_cancel_commit_list(struct list_head *head)
  483. {
  484. struct nfs_page *req;
  485. while(!list_empty(head)) {
  486. req = nfs_list_entry(head->next);
  487. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  488. nfs_list_remove_request(req);
  489. nfs_inode_remove_request(req);
  490. nfs_unlock_request(req);
  491. }
  492. }
  493. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  494. /*
  495. * nfs_scan_commit - Scan an inode for commit requests
  496. * @inode: NFS inode to scan
  497. * @dst: destination list
  498. * @idx_start: lower bound of page->index to scan.
  499. * @npages: idx_start + npages sets the upper bound to scan.
  500. *
  501. * Moves requests from the inode's 'commit' request list.
  502. * The requests are *not* checked to ensure that they form a contiguous set.
  503. */
  504. static int
  505. nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
  506. {
  507. struct nfs_inode *nfsi = NFS_I(inode);
  508. int res = 0;
  509. if (nfsi->ncommit != 0) {
  510. res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages);
  511. nfsi->ncommit -= res;
  512. if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit))
  513. printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n");
  514. }
  515. return res;
  516. }
  517. #else
  518. static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages)
  519. {
  520. return 0;
  521. }
  522. #endif
  523. static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr)
  524. {
  525. struct backing_dev_info *bdi = mapping->backing_dev_info;
  526. DEFINE_WAIT(wait);
  527. int ret = 0;
  528. might_sleep();
  529. if (!bdi_write_congested(bdi))
  530. return 0;
  531. nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT);
  532. if (intr) {
  533. struct rpc_clnt *clnt = NFS_CLIENT(mapping->host);
  534. sigset_t oldset;
  535. rpc_clnt_sigmask(clnt, &oldset);
  536. prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE);
  537. if (bdi_write_congested(bdi)) {
  538. if (signalled())
  539. ret = -ERESTARTSYS;
  540. else
  541. schedule();
  542. }
  543. rpc_clnt_sigunmask(clnt, &oldset);
  544. } else {
  545. prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE);
  546. if (bdi_write_congested(bdi))
  547. schedule();
  548. }
  549. finish_wait(&nfs_write_congestion, &wait);
  550. return ret;
  551. }
  552. /*
  553. * Try to update any existing write request, or create one if there is none.
  554. * In order to match, the request's credentials must match those of
  555. * the calling process.
  556. *
  557. * Note: Should always be called with the Page Lock held!
  558. */
  559. static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx,
  560. struct page *page, unsigned int offset, unsigned int bytes)
  561. {
  562. struct inode *inode = page->mapping->host;
  563. struct nfs_inode *nfsi = NFS_I(inode);
  564. struct nfs_page *req, *new = NULL;
  565. unsigned long rqend, end;
  566. end = offset + bytes;
  567. if (nfs_wait_on_write_congestion(page->mapping, NFS_SERVER(inode)->flags & NFS_MOUNT_INTR))
  568. return ERR_PTR(-ERESTARTSYS);
  569. for (;;) {
  570. /* Loop over all inode entries and see if we find
  571. * A request for the page we wish to update
  572. */
  573. spin_lock(&nfsi->req_lock);
  574. req = nfs_page_find_request_locked(page);
  575. if (req) {
  576. if (!nfs_lock_request_dontget(req)) {
  577. int error;
  578. spin_unlock(&nfsi->req_lock);
  579. error = nfs_wait_on_request(req);
  580. nfs_release_request(req);
  581. if (error < 0) {
  582. if (new)
  583. nfs_release_request(new);
  584. return ERR_PTR(error);
  585. }
  586. continue;
  587. }
  588. spin_unlock(&nfsi->req_lock);
  589. if (new)
  590. nfs_release_request(new);
  591. break;
  592. }
  593. if (new) {
  594. int error;
  595. nfs_lock_request_dontget(new);
  596. error = nfs_inode_add_request(inode, new);
  597. if (error) {
  598. spin_unlock(&nfsi->req_lock);
  599. nfs_unlock_request(new);
  600. return ERR_PTR(error);
  601. }
  602. spin_unlock(&nfsi->req_lock);
  603. return new;
  604. }
  605. spin_unlock(&nfsi->req_lock);
  606. new = nfs_create_request(ctx, inode, page, offset, bytes);
  607. if (IS_ERR(new))
  608. return new;
  609. }
  610. /* We have a request for our page.
  611. * If the creds don't match, or the
  612. * page addresses don't match,
  613. * tell the caller to wait on the conflicting
  614. * request.
  615. */
  616. rqend = req->wb_offset + req->wb_bytes;
  617. if (req->wb_context != ctx
  618. || req->wb_page != page
  619. || !nfs_dirty_request(req)
  620. || offset > rqend || end < req->wb_offset) {
  621. nfs_unlock_request(req);
  622. return ERR_PTR(-EBUSY);
  623. }
  624. /* Okay, the request matches. Update the region */
  625. if (offset < req->wb_offset) {
  626. req->wb_offset = offset;
  627. req->wb_pgbase = offset;
  628. req->wb_bytes = rqend - req->wb_offset;
  629. }
  630. if (end > rqend)
  631. req->wb_bytes = end - req->wb_offset;
  632. return req;
  633. }
  634. int nfs_flush_incompatible(struct file *file, struct page *page)
  635. {
  636. struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
  637. struct nfs_page *req;
  638. int do_flush, status;
  639. /*
  640. * Look for a request corresponding to this page. If there
  641. * is one, and it belongs to another file, we flush it out
  642. * before we try to copy anything into the page. Do this
  643. * due to the lack of an ACCESS-type call in NFSv2.
  644. * Also do the same if we find a request from an existing
  645. * dropped page.
  646. */
  647. do {
  648. req = nfs_page_find_request(page);
  649. if (req == NULL)
  650. return 0;
  651. do_flush = req->wb_page != page || req->wb_context != ctx
  652. || !nfs_dirty_request(req);
  653. nfs_release_request(req);
  654. if (!do_flush)
  655. return 0;
  656. status = nfs_wb_page(page->mapping->host, page);
  657. } while (status == 0);
  658. return status;
  659. }
  660. /*
  661. * Update and possibly write a cached page of an NFS file.
  662. *
  663. * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad
  664. * things with a page scheduled for an RPC call (e.g. invalidate it).
  665. */
  666. int nfs_updatepage(struct file *file, struct page *page,
  667. unsigned int offset, unsigned int count)
  668. {
  669. struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data;
  670. struct inode *inode = page->mapping->host;
  671. int status = 0;
  672. nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE);
  673. dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n",
  674. file->f_path.dentry->d_parent->d_name.name,
  675. file->f_path.dentry->d_name.name, count,
  676. (long long)(page_offset(page) +offset));
  677. /* If we're not using byte range locks, and we know the page
  678. * is entirely in cache, it may be more efficient to avoid
  679. * fragmenting write requests.
  680. */
  681. if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) {
  682. count = max(count + offset, nfs_page_length(page));
  683. offset = 0;
  684. }
  685. status = nfs_writepage_setup(ctx, page, offset, count);
  686. __set_page_dirty_nobuffers(page);
  687. dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n",
  688. status, (long long)i_size_read(inode));
  689. if (status < 0)
  690. ClearPageUptodate(page);
  691. return status;
  692. }
  693. static void nfs_writepage_release(struct nfs_page *req)
  694. {
  695. end_page_writeback(req->wb_page);
  696. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  697. if (!PageError(req->wb_page)) {
  698. if (NFS_NEED_RESCHED(req)) {
  699. nfs_redirty_request(req);
  700. goto out;
  701. } else if (NFS_NEED_COMMIT(req)) {
  702. nfs_mark_request_commit(req);
  703. goto out;
  704. }
  705. }
  706. nfs_inode_remove_request(req);
  707. out:
  708. nfs_clear_commit(req);
  709. nfs_clear_reschedule(req);
  710. #else
  711. nfs_inode_remove_request(req);
  712. #endif
  713. nfs_clear_page_writeback(req);
  714. }
  715. static inline int flush_task_priority(int how)
  716. {
  717. switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) {
  718. case FLUSH_HIGHPRI:
  719. return RPC_PRIORITY_HIGH;
  720. case FLUSH_LOWPRI:
  721. return RPC_PRIORITY_LOW;
  722. }
  723. return RPC_PRIORITY_NORMAL;
  724. }
  725. /*
  726. * Set up the argument/result storage required for the RPC call.
  727. */
  728. static void nfs_write_rpcsetup(struct nfs_page *req,
  729. struct nfs_write_data *data,
  730. const struct rpc_call_ops *call_ops,
  731. unsigned int count, unsigned int offset,
  732. int how)
  733. {
  734. struct inode *inode;
  735. int flags;
  736. /* Set up the RPC argument and reply structs
  737. * NB: take care not to mess about with data->commit et al. */
  738. data->req = req;
  739. data->inode = inode = req->wb_context->dentry->d_inode;
  740. data->cred = req->wb_context->cred;
  741. data->args.fh = NFS_FH(inode);
  742. data->args.offset = req_offset(req) + offset;
  743. data->args.pgbase = req->wb_pgbase + offset;
  744. data->args.pages = data->pagevec;
  745. data->args.count = count;
  746. data->args.context = req->wb_context;
  747. data->res.fattr = &data->fattr;
  748. data->res.count = count;
  749. data->res.verf = &data->verf;
  750. nfs_fattr_init(&data->fattr);
  751. /* Set up the initial task struct. */
  752. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  753. rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
  754. NFS_PROTO(inode)->write_setup(data, how);
  755. data->task.tk_priority = flush_task_priority(how);
  756. data->task.tk_cookie = (unsigned long)inode;
  757. dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n",
  758. data->task.tk_pid,
  759. inode->i_sb->s_id,
  760. (long long)NFS_FILEID(inode),
  761. count,
  762. (unsigned long long)data->args.offset);
  763. }
  764. static void nfs_execute_write(struct nfs_write_data *data)
  765. {
  766. struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
  767. sigset_t oldset;
  768. rpc_clnt_sigmask(clnt, &oldset);
  769. rpc_execute(&data->task);
  770. rpc_clnt_sigunmask(clnt, &oldset);
  771. }
  772. /*
  773. * Generate multiple small requests to write out a single
  774. * contiguous dirty area on one page.
  775. */
  776. static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how)
  777. {
  778. struct nfs_page *req = nfs_list_entry(head->next);
  779. struct page *page = req->wb_page;
  780. struct nfs_write_data *data;
  781. size_t wsize = NFS_SERVER(inode)->wsize, nbytes;
  782. unsigned int offset;
  783. int requests = 0;
  784. LIST_HEAD(list);
  785. nfs_list_remove_request(req);
  786. nbytes = req->wb_bytes;
  787. do {
  788. size_t len = min(nbytes, wsize);
  789. data = nfs_writedata_alloc(len);
  790. if (!data)
  791. goto out_bad;
  792. list_add(&data->pages, &list);
  793. requests++;
  794. nbytes -= len;
  795. } while (nbytes != 0);
  796. atomic_set(&req->wb_complete, requests);
  797. ClearPageError(page);
  798. offset = 0;
  799. nbytes = req->wb_bytes;
  800. do {
  801. data = list_entry(list.next, struct nfs_write_data, pages);
  802. list_del_init(&data->pages);
  803. data->pagevec[0] = page;
  804. if (nbytes > wsize) {
  805. nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
  806. wsize, offset, how);
  807. offset += wsize;
  808. nbytes -= wsize;
  809. } else {
  810. nfs_write_rpcsetup(req, data, &nfs_write_partial_ops,
  811. nbytes, offset, how);
  812. nbytes = 0;
  813. }
  814. nfs_execute_write(data);
  815. } while (nbytes != 0);
  816. return 0;
  817. out_bad:
  818. while (!list_empty(&list)) {
  819. data = list_entry(list.next, struct nfs_write_data, pages);
  820. list_del(&data->pages);
  821. nfs_writedata_release(data);
  822. }
  823. nfs_redirty_request(req);
  824. nfs_clear_page_writeback(req);
  825. return -ENOMEM;
  826. }
  827. /*
  828. * Create an RPC task for the given write request and kick it.
  829. * The page must have been locked by the caller.
  830. *
  831. * It may happen that the page we're passed is not marked dirty.
  832. * This is the case if nfs_updatepage detects a conflicting request
  833. * that has been written but not committed.
  834. */
  835. static int nfs_flush_one(struct inode *inode, struct list_head *head, int how)
  836. {
  837. struct nfs_page *req;
  838. struct page **pages;
  839. struct nfs_write_data *data;
  840. unsigned int count;
  841. data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize);
  842. if (!data)
  843. goto out_bad;
  844. pages = data->pagevec;
  845. count = 0;
  846. while (!list_empty(head)) {
  847. req = nfs_list_entry(head->next);
  848. nfs_list_remove_request(req);
  849. nfs_list_add_request(req, &data->pages);
  850. ClearPageError(req->wb_page);
  851. *pages++ = req->wb_page;
  852. count += req->wb_bytes;
  853. }
  854. req = nfs_list_entry(data->pages.next);
  855. /* Set up the argument struct */
  856. nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how);
  857. nfs_execute_write(data);
  858. return 0;
  859. out_bad:
  860. while (!list_empty(head)) {
  861. struct nfs_page *req = nfs_list_entry(head->next);
  862. nfs_list_remove_request(req);
  863. nfs_redirty_request(req);
  864. nfs_clear_page_writeback(req);
  865. }
  866. return -ENOMEM;
  867. }
  868. static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how)
  869. {
  870. LIST_HEAD(one_request);
  871. int (*flush_one)(struct inode *, struct list_head *, int);
  872. struct nfs_page *req;
  873. int wpages = NFS_SERVER(inode)->wpages;
  874. int wsize = NFS_SERVER(inode)->wsize;
  875. int error;
  876. flush_one = nfs_flush_one;
  877. if (wsize < PAGE_CACHE_SIZE)
  878. flush_one = nfs_flush_multi;
  879. /* For single writes, FLUSH_STABLE is more efficient */
  880. if (npages <= wpages && npages == NFS_I(inode)->npages
  881. && nfs_list_entry(head->next)->wb_bytes <= wsize)
  882. how |= FLUSH_STABLE;
  883. do {
  884. nfs_coalesce_requests(head, &one_request, wpages);
  885. req = nfs_list_entry(one_request.next);
  886. error = flush_one(inode, &one_request, how);
  887. if (error < 0)
  888. goto out_err;
  889. } while (!list_empty(head));
  890. return 0;
  891. out_err:
  892. while (!list_empty(head)) {
  893. req = nfs_list_entry(head->next);
  894. nfs_list_remove_request(req);
  895. nfs_redirty_request(req);
  896. nfs_clear_page_writeback(req);
  897. }
  898. return error;
  899. }
  900. /*
  901. * Handle a write reply that flushed part of a page.
  902. */
  903. static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata)
  904. {
  905. struct nfs_write_data *data = calldata;
  906. struct nfs_page *req = data->req;
  907. struct page *page = req->wb_page;
  908. dprintk("NFS: write (%s/%Ld %d@%Ld)",
  909. req->wb_context->dentry->d_inode->i_sb->s_id,
  910. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  911. req->wb_bytes,
  912. (long long)req_offset(req));
  913. if (nfs_writeback_done(task, data) != 0)
  914. return;
  915. if (task->tk_status < 0) {
  916. ClearPageUptodate(page);
  917. SetPageError(page);
  918. req->wb_context->error = task->tk_status;
  919. dprintk(", error = %d\n", task->tk_status);
  920. } else {
  921. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  922. if (data->verf.committed < NFS_FILE_SYNC) {
  923. if (!NFS_NEED_COMMIT(req)) {
  924. nfs_defer_commit(req);
  925. memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
  926. dprintk(" defer commit\n");
  927. } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) {
  928. nfs_defer_reschedule(req);
  929. dprintk(" server reboot detected\n");
  930. }
  931. } else
  932. #endif
  933. dprintk(" OK\n");
  934. }
  935. if (atomic_dec_and_test(&req->wb_complete))
  936. nfs_writepage_release(req);
  937. }
  938. static const struct rpc_call_ops nfs_write_partial_ops = {
  939. .rpc_call_done = nfs_writeback_done_partial,
  940. .rpc_release = nfs_writedata_release,
  941. };
  942. /*
  943. * Handle a write reply that flushes a whole page.
  944. *
  945. * FIXME: There is an inherent race with invalidate_inode_pages and
  946. * writebacks since the page->count is kept > 1 for as long
  947. * as the page has a write request pending.
  948. */
  949. static void nfs_writeback_done_full(struct rpc_task *task, void *calldata)
  950. {
  951. struct nfs_write_data *data = calldata;
  952. struct nfs_page *req;
  953. struct page *page;
  954. if (nfs_writeback_done(task, data) != 0)
  955. return;
  956. /* Update attributes as result of writeback. */
  957. while (!list_empty(&data->pages)) {
  958. req = nfs_list_entry(data->pages.next);
  959. nfs_list_remove_request(req);
  960. page = req->wb_page;
  961. dprintk("NFS: write (%s/%Ld %d@%Ld)",
  962. req->wb_context->dentry->d_inode->i_sb->s_id,
  963. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  964. req->wb_bytes,
  965. (long long)req_offset(req));
  966. if (task->tk_status < 0) {
  967. ClearPageUptodate(page);
  968. SetPageError(page);
  969. req->wb_context->error = task->tk_status;
  970. end_page_writeback(page);
  971. nfs_inode_remove_request(req);
  972. dprintk(", error = %d\n", task->tk_status);
  973. goto next;
  974. }
  975. end_page_writeback(page);
  976. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  977. if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) {
  978. nfs_inode_remove_request(req);
  979. dprintk(" OK\n");
  980. goto next;
  981. }
  982. memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf));
  983. nfs_mark_request_commit(req);
  984. dprintk(" marked for commit\n");
  985. #else
  986. nfs_inode_remove_request(req);
  987. #endif
  988. next:
  989. nfs_clear_page_writeback(req);
  990. }
  991. }
  992. static const struct rpc_call_ops nfs_write_full_ops = {
  993. .rpc_call_done = nfs_writeback_done_full,
  994. .rpc_release = nfs_writedata_release,
  995. };
  996. /*
  997. * This function is called when the WRITE call is complete.
  998. */
  999. int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data)
  1000. {
  1001. struct nfs_writeargs *argp = &data->args;
  1002. struct nfs_writeres *resp = &data->res;
  1003. int status;
  1004. dprintk("NFS: %4d nfs_writeback_done (status %d)\n",
  1005. task->tk_pid, task->tk_status);
  1006. /*
  1007. * ->write_done will attempt to use post-op attributes to detect
  1008. * conflicting writes by other clients. A strict interpretation
  1009. * of close-to-open would allow us to continue caching even if
  1010. * another writer had changed the file, but some applications
  1011. * depend on tighter cache coherency when writing.
  1012. */
  1013. status = NFS_PROTO(data->inode)->write_done(task, data);
  1014. if (status != 0)
  1015. return status;
  1016. nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count);
  1017. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1018. if (resp->verf->committed < argp->stable && task->tk_status >= 0) {
  1019. /* We tried a write call, but the server did not
  1020. * commit data to stable storage even though we
  1021. * requested it.
  1022. * Note: There is a known bug in Tru64 < 5.0 in which
  1023. * the server reports NFS_DATA_SYNC, but performs
  1024. * NFS_FILE_SYNC. We therefore implement this checking
  1025. * as a dprintk() in order to avoid filling syslog.
  1026. */
  1027. static unsigned long complain;
  1028. if (time_before(complain, jiffies)) {
  1029. dprintk("NFS: faulty NFS server %s:"
  1030. " (committed = %d) != (stable = %d)\n",
  1031. NFS_SERVER(data->inode)->nfs_client->cl_hostname,
  1032. resp->verf->committed, argp->stable);
  1033. complain = jiffies + 300 * HZ;
  1034. }
  1035. }
  1036. #endif
  1037. /* Is this a short write? */
  1038. if (task->tk_status >= 0 && resp->count < argp->count) {
  1039. static unsigned long complain;
  1040. nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE);
  1041. /* Has the server at least made some progress? */
  1042. if (resp->count != 0) {
  1043. /* Was this an NFSv2 write or an NFSv3 stable write? */
  1044. if (resp->verf->committed != NFS_UNSTABLE) {
  1045. /* Resend from where the server left off */
  1046. argp->offset += resp->count;
  1047. argp->pgbase += resp->count;
  1048. argp->count -= resp->count;
  1049. } else {
  1050. /* Resend as a stable write in order to avoid
  1051. * headaches in the case of a server crash.
  1052. */
  1053. argp->stable = NFS_FILE_SYNC;
  1054. }
  1055. rpc_restart_call(task);
  1056. return -EAGAIN;
  1057. }
  1058. if (time_before(complain, jiffies)) {
  1059. printk(KERN_WARNING
  1060. "NFS: Server wrote zero bytes, expected %u.\n",
  1061. argp->count);
  1062. complain = jiffies + 300 * HZ;
  1063. }
  1064. /* Can't do anything about it except throw an error. */
  1065. task->tk_status = -EIO;
  1066. }
  1067. return 0;
  1068. }
  1069. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1070. void nfs_commit_release(void *wdata)
  1071. {
  1072. nfs_commit_free(wdata);
  1073. }
  1074. /*
  1075. * Set up the argument/result storage required for the RPC call.
  1076. */
  1077. static void nfs_commit_rpcsetup(struct list_head *head,
  1078. struct nfs_write_data *data,
  1079. int how)
  1080. {
  1081. struct nfs_page *first;
  1082. struct inode *inode;
  1083. int flags;
  1084. /* Set up the RPC argument and reply structs
  1085. * NB: take care not to mess about with data->commit et al. */
  1086. list_splice_init(head, &data->pages);
  1087. first = nfs_list_entry(data->pages.next);
  1088. inode = first->wb_context->dentry->d_inode;
  1089. data->inode = inode;
  1090. data->cred = first->wb_context->cred;
  1091. data->args.fh = NFS_FH(data->inode);
  1092. /* Note: we always request a commit of the entire inode */
  1093. data->args.offset = 0;
  1094. data->args.count = 0;
  1095. data->res.count = 0;
  1096. data->res.fattr = &data->fattr;
  1097. data->res.verf = &data->verf;
  1098. nfs_fattr_init(&data->fattr);
  1099. /* Set up the initial task struct. */
  1100. flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC;
  1101. rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data);
  1102. NFS_PROTO(inode)->commit_setup(data, how);
  1103. data->task.tk_priority = flush_task_priority(how);
  1104. data->task.tk_cookie = (unsigned long)inode;
  1105. dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid);
  1106. }
  1107. /*
  1108. * Commit dirty pages
  1109. */
  1110. static int
  1111. nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1112. {
  1113. struct nfs_write_data *data;
  1114. struct nfs_page *req;
  1115. data = nfs_commit_alloc();
  1116. if (!data)
  1117. goto out_bad;
  1118. /* Set up the argument struct */
  1119. nfs_commit_rpcsetup(head, data, how);
  1120. nfs_execute_write(data);
  1121. return 0;
  1122. out_bad:
  1123. while (!list_empty(head)) {
  1124. req = nfs_list_entry(head->next);
  1125. nfs_list_remove_request(req);
  1126. nfs_mark_request_commit(req);
  1127. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1128. nfs_clear_page_writeback(req);
  1129. }
  1130. return -ENOMEM;
  1131. }
  1132. /*
  1133. * COMMIT call returned
  1134. */
  1135. static void nfs_commit_done(struct rpc_task *task, void *calldata)
  1136. {
  1137. struct nfs_write_data *data = calldata;
  1138. struct nfs_page *req;
  1139. dprintk("NFS: %4d nfs_commit_done (status %d)\n",
  1140. task->tk_pid, task->tk_status);
  1141. /* Call the NFS version-specific code */
  1142. if (NFS_PROTO(data->inode)->commit_done(task, data) != 0)
  1143. return;
  1144. while (!list_empty(&data->pages)) {
  1145. req = nfs_list_entry(data->pages.next);
  1146. nfs_list_remove_request(req);
  1147. dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS);
  1148. dprintk("NFS: commit (%s/%Ld %d@%Ld)",
  1149. req->wb_context->dentry->d_inode->i_sb->s_id,
  1150. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  1151. req->wb_bytes,
  1152. (long long)req_offset(req));
  1153. if (task->tk_status < 0) {
  1154. req->wb_context->error = task->tk_status;
  1155. nfs_inode_remove_request(req);
  1156. dprintk(", error = %d\n", task->tk_status);
  1157. goto next;
  1158. }
  1159. /* Okay, COMMIT succeeded, apparently. Check the verifier
  1160. * returned by the server against all stored verfs. */
  1161. if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) {
  1162. /* We have a match */
  1163. nfs_inode_remove_request(req);
  1164. dprintk(" OK\n");
  1165. goto next;
  1166. }
  1167. /* We have a mismatch. Write the page again */
  1168. dprintk(" mismatch\n");
  1169. nfs_redirty_request(req);
  1170. next:
  1171. nfs_clear_page_writeback(req);
  1172. }
  1173. }
  1174. static const struct rpc_call_ops nfs_commit_ops = {
  1175. .rpc_call_done = nfs_commit_done,
  1176. .rpc_release = nfs_commit_release,
  1177. };
  1178. #else
  1179. static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how)
  1180. {
  1181. return 0;
  1182. }
  1183. #endif
  1184. static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how)
  1185. {
  1186. struct nfs_inode *nfsi = NFS_I(mapping->host);
  1187. LIST_HEAD(head);
  1188. long res;
  1189. spin_lock(&nfsi->req_lock);
  1190. res = nfs_scan_dirty(mapping, wbc, &head);
  1191. spin_unlock(&nfsi->req_lock);
  1192. if (res) {
  1193. int error = nfs_flush_list(mapping->host, &head, res, how);
  1194. if (error < 0)
  1195. return error;
  1196. }
  1197. return res;
  1198. }
  1199. #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4)
  1200. int nfs_commit_inode(struct inode *inode, int how)
  1201. {
  1202. struct nfs_inode *nfsi = NFS_I(inode);
  1203. LIST_HEAD(head);
  1204. int res;
  1205. spin_lock(&nfsi->req_lock);
  1206. res = nfs_scan_commit(inode, &head, 0, 0);
  1207. spin_unlock(&nfsi->req_lock);
  1208. if (res) {
  1209. int error = nfs_commit_list(inode, &head, how);
  1210. if (error < 0)
  1211. return error;
  1212. }
  1213. return res;
  1214. }
  1215. #endif
  1216. long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how)
  1217. {
  1218. struct inode *inode = mapping->host;
  1219. struct nfs_inode *nfsi = NFS_I(inode);
  1220. unsigned long idx_start, idx_end;
  1221. unsigned int npages = 0;
  1222. LIST_HEAD(head);
  1223. int nocommit = how & FLUSH_NOCOMMIT;
  1224. long pages, ret;
  1225. /* FIXME */
  1226. if (wbc->range_cyclic)
  1227. idx_start = 0;
  1228. else {
  1229. idx_start = wbc->range_start >> PAGE_CACHE_SHIFT;
  1230. idx_end = wbc->range_end >> PAGE_CACHE_SHIFT;
  1231. if (idx_end > idx_start) {
  1232. unsigned long l_npages = 1 + idx_end - idx_start;
  1233. npages = l_npages;
  1234. if (sizeof(npages) != sizeof(l_npages) &&
  1235. (unsigned long)npages != l_npages)
  1236. npages = 0;
  1237. }
  1238. }
  1239. how &= ~FLUSH_NOCOMMIT;
  1240. spin_lock(&nfsi->req_lock);
  1241. do {
  1242. wbc->pages_skipped = 0;
  1243. ret = nfs_wait_on_requests_locked(inode, idx_start, npages);
  1244. if (ret != 0)
  1245. continue;
  1246. pages = nfs_scan_dirty(mapping, wbc, &head);
  1247. if (pages != 0) {
  1248. spin_unlock(&nfsi->req_lock);
  1249. if (how & FLUSH_INVALIDATE) {
  1250. nfs_cancel_dirty_list(&head);
  1251. ret = pages;
  1252. } else
  1253. ret = nfs_flush_list(inode, &head, pages, how);
  1254. spin_lock(&nfsi->req_lock);
  1255. continue;
  1256. }
  1257. if (wbc->pages_skipped != 0)
  1258. continue;
  1259. if (nocommit)
  1260. break;
  1261. pages = nfs_scan_commit(inode, &head, idx_start, npages);
  1262. if (pages == 0) {
  1263. if (wbc->pages_skipped != 0)
  1264. continue;
  1265. break;
  1266. }
  1267. if (how & FLUSH_INVALIDATE) {
  1268. spin_unlock(&nfsi->req_lock);
  1269. nfs_cancel_commit_list(&head);
  1270. ret = pages;
  1271. spin_lock(&nfsi->req_lock);
  1272. continue;
  1273. }
  1274. pages += nfs_scan_commit(inode, &head, 0, 0);
  1275. spin_unlock(&nfsi->req_lock);
  1276. ret = nfs_commit_list(inode, &head, how);
  1277. spin_lock(&nfsi->req_lock);
  1278. } while (ret >= 0);
  1279. spin_unlock(&nfsi->req_lock);
  1280. return ret;
  1281. }
  1282. /*
  1283. * flush the inode to disk.
  1284. */
  1285. int nfs_wb_all(struct inode *inode)
  1286. {
  1287. struct address_space *mapping = inode->i_mapping;
  1288. struct writeback_control wbc = {
  1289. .bdi = mapping->backing_dev_info,
  1290. .sync_mode = WB_SYNC_ALL,
  1291. .nr_to_write = LONG_MAX,
  1292. .for_writepages = 1,
  1293. .range_cyclic = 1,
  1294. };
  1295. int ret;
  1296. ret = generic_writepages(mapping, &wbc);
  1297. if (ret < 0)
  1298. goto out;
  1299. ret = nfs_sync_mapping_wait(mapping, &wbc, 0);
  1300. if (ret >= 0)
  1301. return 0;
  1302. out:
  1303. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  1304. return ret;
  1305. }
  1306. int nfs_sync_mapping_range(struct address_space *mapping, loff_t range_start, loff_t range_end, int how)
  1307. {
  1308. struct writeback_control wbc = {
  1309. .bdi = mapping->backing_dev_info,
  1310. .sync_mode = WB_SYNC_ALL,
  1311. .nr_to_write = LONG_MAX,
  1312. .range_start = range_start,
  1313. .range_end = range_end,
  1314. .for_writepages = 1,
  1315. };
  1316. int ret;
  1317. if (!(how & FLUSH_NOWRITEPAGE)) {
  1318. ret = generic_writepages(mapping, &wbc);
  1319. if (ret < 0)
  1320. goto out;
  1321. }
  1322. ret = nfs_sync_mapping_wait(mapping, &wbc, how);
  1323. if (ret >= 0)
  1324. return 0;
  1325. out:
  1326. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  1327. return ret;
  1328. }
  1329. int nfs_wb_page_priority(struct inode *inode, struct page *page, int how)
  1330. {
  1331. loff_t range_start = page_offset(page);
  1332. loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1);
  1333. struct writeback_control wbc = {
  1334. .bdi = page->mapping->backing_dev_info,
  1335. .sync_mode = WB_SYNC_ALL,
  1336. .nr_to_write = LONG_MAX,
  1337. .range_start = range_start,
  1338. .range_end = range_end,
  1339. };
  1340. int ret;
  1341. BUG_ON(!PageLocked(page));
  1342. if (!(how & FLUSH_NOWRITEPAGE) && clear_page_dirty_for_io(page)) {
  1343. ret = nfs_writepage_locked(page, &wbc);
  1344. if (ret < 0)
  1345. goto out;
  1346. }
  1347. ret = nfs_sync_mapping_wait(page->mapping, &wbc, how);
  1348. if (ret >= 0)
  1349. return 0;
  1350. out:
  1351. __mark_inode_dirty(inode, I_DIRTY_PAGES);
  1352. return ret;
  1353. }
  1354. /*
  1355. * Write back all requests on one page - we do this before reading it.
  1356. */
  1357. int nfs_wb_page(struct inode *inode, struct page* page)
  1358. {
  1359. return nfs_wb_page_priority(inode, page, FLUSH_STABLE);
  1360. }
  1361. int nfs_set_page_dirty(struct page *page)
  1362. {
  1363. struct nfs_page *req;
  1364. req = nfs_page_find_request(page);
  1365. if (req != NULL) {
  1366. /* Mark any existing write requests for flushing */
  1367. set_bit(PG_NEED_FLUSH, &req->wb_flags);
  1368. nfs_release_request(req);
  1369. }
  1370. return __set_page_dirty_nobuffers(page);
  1371. }
  1372. int __init nfs_init_writepagecache(void)
  1373. {
  1374. nfs_wdata_cachep = kmem_cache_create("nfs_write_data",
  1375. sizeof(struct nfs_write_data),
  1376. 0, SLAB_HWCACHE_ALIGN,
  1377. NULL, NULL);
  1378. if (nfs_wdata_cachep == NULL)
  1379. return -ENOMEM;
  1380. nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE,
  1381. nfs_wdata_cachep);
  1382. if (nfs_wdata_mempool == NULL)
  1383. return -ENOMEM;
  1384. nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT,
  1385. nfs_wdata_cachep);
  1386. if (nfs_commit_mempool == NULL)
  1387. return -ENOMEM;
  1388. return 0;
  1389. }
  1390. void nfs_destroy_writepagecache(void)
  1391. {
  1392. mempool_destroy(nfs_commit_mempool);
  1393. mempool_destroy(nfs_wdata_mempool);
  1394. kmem_cache_destroy(nfs_wdata_cachep);
  1395. }