write.c 39 KB

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