write.c 39 KB

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