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