write.c 39 KB

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