write.c 42 KB

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