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