write.c 37 KB

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