write.c 37 KB

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