write.c 37 KB

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