write.c 37 KB

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