write.c 37 KB

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