write.c 40 KB

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