write.c 41 KB

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