write.c 40 KB

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