write.c 37 KB

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