ops_address.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/pagevec.h>
  16. #include <linux/mpage.h>
  17. #include <linux/fs.h>
  18. #include <linux/writeback.h>
  19. #include <linux/swap.h>
  20. #include <linux/gfs2_ondisk.h>
  21. #include <linux/lm_interface.h>
  22. #include <linux/backing-dev.h>
  23. #include <linux/pagevec.h>
  24. #include "gfs2.h"
  25. #include "incore.h"
  26. #include "bmap.h"
  27. #include "glock.h"
  28. #include "inode.h"
  29. #include "log.h"
  30. #include "meta_io.h"
  31. #include "ops_address.h"
  32. #include "quota.h"
  33. #include "trans.h"
  34. #include "rgrp.h"
  35. #include "super.h"
  36. #include "util.h"
  37. #include "glops.h"
  38. static void gfs2_page_add_databufs(struct gfs2_inode *ip, struct page *page,
  39. unsigned int from, unsigned int to)
  40. {
  41. struct buffer_head *head = page_buffers(page);
  42. unsigned int bsize = head->b_size;
  43. struct buffer_head *bh;
  44. unsigned int start, end;
  45. for (bh = head, start = 0; bh != head || !start;
  46. bh = bh->b_this_page, start = end) {
  47. end = start + bsize;
  48. if (end <= from || start >= to)
  49. continue;
  50. if (gfs2_is_jdata(ip))
  51. set_buffer_uptodate(bh);
  52. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  53. }
  54. }
  55. /**
  56. * gfs2_get_block_noalloc - Fills in a buffer head with details about a block
  57. * @inode: The inode
  58. * @lblock: The block number to look up
  59. * @bh_result: The buffer head to return the result in
  60. * @create: Non-zero if we may add block to the file
  61. *
  62. * Returns: errno
  63. */
  64. static int gfs2_get_block_noalloc(struct inode *inode, sector_t lblock,
  65. struct buffer_head *bh_result, int create)
  66. {
  67. int error;
  68. error = gfs2_block_map(inode, lblock, bh_result, 0);
  69. if (error)
  70. return error;
  71. if (!buffer_mapped(bh_result))
  72. return -EIO;
  73. return 0;
  74. }
  75. static int gfs2_get_block_direct(struct inode *inode, sector_t lblock,
  76. struct buffer_head *bh_result, int create)
  77. {
  78. return gfs2_block_map(inode, lblock, bh_result, 0);
  79. }
  80. /**
  81. * gfs2_writepage_common - Common bits of writepage
  82. * @page: The page to be written
  83. * @wbc: The writeback control
  84. *
  85. * Returns: 1 if writepage is ok, otherwise an error code or zero if no error.
  86. */
  87. static int gfs2_writepage_common(struct page *page,
  88. struct writeback_control *wbc)
  89. {
  90. struct inode *inode = page->mapping->host;
  91. struct gfs2_inode *ip = GFS2_I(inode);
  92. struct gfs2_sbd *sdp = GFS2_SB(inode);
  93. loff_t i_size = i_size_read(inode);
  94. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  95. unsigned offset;
  96. if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl)))
  97. goto out;
  98. if (current->journal_info)
  99. goto redirty;
  100. /* Is the page fully outside i_size? (truncate in progress) */
  101. offset = i_size & (PAGE_CACHE_SIZE-1);
  102. if (page->index > end_index || (page->index == end_index && !offset)) {
  103. page->mapping->a_ops->invalidatepage(page, 0);
  104. goto out;
  105. }
  106. return 1;
  107. redirty:
  108. redirty_page_for_writepage(wbc, page);
  109. out:
  110. unlock_page(page);
  111. return 0;
  112. }
  113. /**
  114. * gfs2_writeback_writepage - Write page for writeback mappings
  115. * @page: The page
  116. * @wbc: The writeback control
  117. *
  118. */
  119. static int gfs2_writeback_writepage(struct page *page,
  120. struct writeback_control *wbc)
  121. {
  122. int ret;
  123. ret = gfs2_writepage_common(page, wbc);
  124. if (ret <= 0)
  125. return ret;
  126. ret = mpage_writepage(page, gfs2_get_block_noalloc, wbc);
  127. if (ret == -EAGAIN)
  128. ret = block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  129. return ret;
  130. }
  131. /**
  132. * gfs2_ordered_writepage - Write page for ordered data files
  133. * @page: The page to write
  134. * @wbc: The writeback control
  135. *
  136. */
  137. static int gfs2_ordered_writepage(struct page *page,
  138. struct writeback_control *wbc)
  139. {
  140. struct inode *inode = page->mapping->host;
  141. struct gfs2_inode *ip = GFS2_I(inode);
  142. int ret;
  143. ret = gfs2_writepage_common(page, wbc);
  144. if (ret <= 0)
  145. return ret;
  146. if (!page_has_buffers(page)) {
  147. create_empty_buffers(page, inode->i_sb->s_blocksize,
  148. (1 << BH_Dirty)|(1 << BH_Uptodate));
  149. }
  150. gfs2_page_add_databufs(ip, page, 0, inode->i_sb->s_blocksize-1);
  151. return block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  152. }
  153. /**
  154. * __gfs2_jdata_writepage - The core of jdata writepage
  155. * @page: The page to write
  156. * @wbc: The writeback control
  157. *
  158. * This is shared between writepage and writepages and implements the
  159. * core of the writepage operation. If a transaction is required then
  160. * PageChecked will have been set and the transaction will have
  161. * already been started before this is called.
  162. */
  163. static int __gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
  164. {
  165. struct inode *inode = page->mapping->host;
  166. struct gfs2_inode *ip = GFS2_I(inode);
  167. struct gfs2_sbd *sdp = GFS2_SB(inode);
  168. if (PageChecked(page)) {
  169. ClearPageChecked(page);
  170. if (!page_has_buffers(page)) {
  171. create_empty_buffers(page, inode->i_sb->s_blocksize,
  172. (1 << BH_Dirty)|(1 << BH_Uptodate));
  173. }
  174. gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
  175. }
  176. return block_write_full_page(page, gfs2_get_block_noalloc, wbc);
  177. }
  178. /**
  179. * gfs2_jdata_writepage - Write complete page
  180. * @page: Page to write
  181. *
  182. * Returns: errno
  183. *
  184. */
  185. static int gfs2_jdata_writepage(struct page *page, struct writeback_control *wbc)
  186. {
  187. struct inode *inode = page->mapping->host;
  188. struct gfs2_sbd *sdp = GFS2_SB(inode);
  189. int error;
  190. int done_trans = 0;
  191. error = gfs2_writepage_common(page, wbc);
  192. if (error <= 0)
  193. return error;
  194. if (PageChecked(page)) {
  195. if (wbc->sync_mode != WB_SYNC_ALL)
  196. goto out_ignore;
  197. error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
  198. if (error)
  199. goto out_ignore;
  200. done_trans = 1;
  201. }
  202. error = __gfs2_jdata_writepage(page, wbc);
  203. if (done_trans)
  204. gfs2_trans_end(sdp);
  205. return error;
  206. out_ignore:
  207. redirty_page_for_writepage(wbc, page);
  208. unlock_page(page);
  209. return 0;
  210. }
  211. /**
  212. * gfs2_writeback_writepages - Write a bunch of dirty pages back to disk
  213. * @mapping: The mapping to write
  214. * @wbc: Write-back control
  215. *
  216. * For the data=writeback case we can already ignore buffer heads
  217. * and write whole extents at once. This is a big reduction in the
  218. * number of I/O requests we send and the bmap calls we make in this case.
  219. */
  220. static int gfs2_writeback_writepages(struct address_space *mapping,
  221. struct writeback_control *wbc)
  222. {
  223. return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
  224. }
  225. /**
  226. * gfs2_write_jdata_pagevec - Write back a pagevec's worth of pages
  227. * @mapping: The mapping
  228. * @wbc: The writeback control
  229. * @writepage: The writepage function to call for each page
  230. * @pvec: The vector of pages
  231. * @nr_pages: The number of pages to write
  232. *
  233. * Returns: non-zero if loop should terminate, zero otherwise
  234. */
  235. static int gfs2_write_jdata_pagevec(struct address_space *mapping,
  236. struct writeback_control *wbc,
  237. struct pagevec *pvec,
  238. int nr_pages, pgoff_t end)
  239. {
  240. struct inode *inode = mapping->host;
  241. struct gfs2_sbd *sdp = GFS2_SB(inode);
  242. loff_t i_size = i_size_read(inode);
  243. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  244. unsigned offset = i_size & (PAGE_CACHE_SIZE-1);
  245. unsigned nrblocks = nr_pages * (PAGE_CACHE_SIZE/inode->i_sb->s_blocksize);
  246. struct backing_dev_info *bdi = mapping->backing_dev_info;
  247. int i;
  248. int ret;
  249. ret = gfs2_trans_begin(sdp, nrblocks, 0);
  250. if (ret < 0)
  251. return ret;
  252. for(i = 0; i < nr_pages; i++) {
  253. struct page *page = pvec->pages[i];
  254. lock_page(page);
  255. if (unlikely(page->mapping != mapping)) {
  256. unlock_page(page);
  257. continue;
  258. }
  259. if (!wbc->range_cyclic && page->index > end) {
  260. ret = 1;
  261. unlock_page(page);
  262. continue;
  263. }
  264. if (wbc->sync_mode != WB_SYNC_NONE)
  265. wait_on_page_writeback(page);
  266. if (PageWriteback(page) ||
  267. !clear_page_dirty_for_io(page)) {
  268. unlock_page(page);
  269. continue;
  270. }
  271. /* Is the page fully outside i_size? (truncate in progress) */
  272. if (page->index > end_index || (page->index == end_index && !offset)) {
  273. page->mapping->a_ops->invalidatepage(page, 0);
  274. unlock_page(page);
  275. continue;
  276. }
  277. ret = __gfs2_jdata_writepage(page, wbc);
  278. if (ret || (--(wbc->nr_to_write) <= 0))
  279. ret = 1;
  280. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  281. wbc->encountered_congestion = 1;
  282. ret = 1;
  283. }
  284. }
  285. gfs2_trans_end(sdp);
  286. return ret;
  287. }
  288. /**
  289. * gfs2_write_cache_jdata - Like write_cache_pages but different
  290. * @mapping: The mapping to write
  291. * @wbc: The writeback control
  292. * @writepage: The writepage function to call
  293. * @data: The data to pass to writepage
  294. *
  295. * The reason that we use our own function here is that we need to
  296. * start transactions before we grab page locks. This allows us
  297. * to get the ordering right.
  298. */
  299. static int gfs2_write_cache_jdata(struct address_space *mapping,
  300. struct writeback_control *wbc)
  301. {
  302. struct backing_dev_info *bdi = mapping->backing_dev_info;
  303. int ret = 0;
  304. int done = 0;
  305. struct pagevec pvec;
  306. int nr_pages;
  307. pgoff_t index;
  308. pgoff_t end;
  309. int scanned = 0;
  310. int range_whole = 0;
  311. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  312. wbc->encountered_congestion = 1;
  313. return 0;
  314. }
  315. pagevec_init(&pvec, 0);
  316. if (wbc->range_cyclic) {
  317. index = mapping->writeback_index; /* Start from prev offset */
  318. end = -1;
  319. } else {
  320. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  321. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  322. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  323. range_whole = 1;
  324. scanned = 1;
  325. }
  326. retry:
  327. while (!done && (index <= end) &&
  328. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  329. PAGECACHE_TAG_DIRTY,
  330. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  331. scanned = 1;
  332. ret = gfs2_write_jdata_pagevec(mapping, wbc, &pvec, nr_pages, end);
  333. if (ret)
  334. done = 1;
  335. if (ret > 0)
  336. ret = 0;
  337. pagevec_release(&pvec);
  338. cond_resched();
  339. }
  340. if (!scanned && !done) {
  341. /*
  342. * We hit the last page and there is more work to be done: wrap
  343. * back to the start of the file
  344. */
  345. scanned = 1;
  346. index = 0;
  347. goto retry;
  348. }
  349. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  350. mapping->writeback_index = index;
  351. return ret;
  352. }
  353. /**
  354. * gfs2_jdata_writepages - Write a bunch of dirty pages back to disk
  355. * @mapping: The mapping to write
  356. * @wbc: The writeback control
  357. *
  358. */
  359. static int gfs2_jdata_writepages(struct address_space *mapping,
  360. struct writeback_control *wbc)
  361. {
  362. struct gfs2_inode *ip = GFS2_I(mapping->host);
  363. struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
  364. int ret;
  365. ret = gfs2_write_cache_jdata(mapping, wbc);
  366. if (ret == 0 && wbc->sync_mode == WB_SYNC_ALL) {
  367. gfs2_log_flush(sdp, ip->i_gl);
  368. ret = gfs2_write_cache_jdata(mapping, wbc);
  369. }
  370. return ret;
  371. }
  372. /**
  373. * stuffed_readpage - Fill in a Linux page with stuffed file data
  374. * @ip: the inode
  375. * @page: the page
  376. *
  377. * Returns: errno
  378. */
  379. static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
  380. {
  381. struct buffer_head *dibh;
  382. void *kaddr;
  383. int error;
  384. /*
  385. * Due to the order of unstuffing files and ->nopage(), we can be
  386. * asked for a zero page in the case of a stuffed file being extended,
  387. * so we need to supply one here. It doesn't happen often.
  388. */
  389. if (unlikely(page->index)) {
  390. zero_user(page, 0, PAGE_CACHE_SIZE);
  391. return 0;
  392. }
  393. error = gfs2_meta_inode_buffer(ip, &dibh);
  394. if (error)
  395. return error;
  396. kaddr = kmap_atomic(page, KM_USER0);
  397. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
  398. ip->i_di.di_size);
  399. memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
  400. kunmap_atomic(kaddr, KM_USER0);
  401. flush_dcache_page(page);
  402. brelse(dibh);
  403. SetPageUptodate(page);
  404. return 0;
  405. }
  406. /**
  407. * __gfs2_readpage - readpage
  408. * @file: The file to read a page for
  409. * @page: The page to read
  410. *
  411. * This is the core of gfs2's readpage. Its used by the internal file
  412. * reading code as in that case we already hold the glock. Also its
  413. * called by gfs2_readpage() once the required lock has been granted.
  414. *
  415. */
  416. static int __gfs2_readpage(void *file, struct page *page)
  417. {
  418. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  419. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  420. int error;
  421. if (gfs2_is_stuffed(ip)) {
  422. error = stuffed_readpage(ip, page);
  423. unlock_page(page);
  424. } else {
  425. error = mpage_readpage(page, gfs2_block_map);
  426. }
  427. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  428. return -EIO;
  429. return error;
  430. }
  431. /**
  432. * gfs2_readpage - read a page of a file
  433. * @file: The file to read
  434. * @page: The page of the file
  435. *
  436. * This deals with the locking required. We use a trylock in order to
  437. * avoid the page lock / glock ordering problems returning AOP_TRUNCATED_PAGE
  438. * in the event that we are unable to get the lock.
  439. */
  440. static int gfs2_readpage(struct file *file, struct page *page)
  441. {
  442. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  443. struct gfs2_holder *gh;
  444. int error;
  445. gh = gfs2_glock_is_locked_by_me(ip->i_gl);
  446. if (!gh) {
  447. gh = kmalloc(sizeof(struct gfs2_holder), GFP_NOFS);
  448. if (!gh)
  449. return -ENOBUFS;
  450. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, gh);
  451. unlock_page(page);
  452. error = gfs2_glock_nq_atime(gh);
  453. if (likely(error != 0))
  454. goto out;
  455. return AOP_TRUNCATED_PAGE;
  456. }
  457. error = __gfs2_readpage(file, page);
  458. gfs2_glock_dq(gh);
  459. out:
  460. gfs2_holder_uninit(gh);
  461. kfree(gh);
  462. return error;
  463. }
  464. /**
  465. * gfs2_internal_read - read an internal file
  466. * @ip: The gfs2 inode
  467. * @ra_state: The readahead state (or NULL for no readahead)
  468. * @buf: The buffer to fill
  469. * @pos: The file position
  470. * @size: The amount to read
  471. *
  472. */
  473. int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
  474. char *buf, loff_t *pos, unsigned size)
  475. {
  476. struct address_space *mapping = ip->i_inode.i_mapping;
  477. unsigned long index = *pos / PAGE_CACHE_SIZE;
  478. unsigned offset = *pos & (PAGE_CACHE_SIZE - 1);
  479. unsigned copied = 0;
  480. unsigned amt;
  481. struct page *page;
  482. void *p;
  483. do {
  484. amt = size - copied;
  485. if (offset + size > PAGE_CACHE_SIZE)
  486. amt = PAGE_CACHE_SIZE - offset;
  487. page = read_cache_page(mapping, index, __gfs2_readpage, NULL);
  488. if (IS_ERR(page))
  489. return PTR_ERR(page);
  490. p = kmap_atomic(page, KM_USER0);
  491. memcpy(buf + copied, p + offset, amt);
  492. kunmap_atomic(p, KM_USER0);
  493. mark_page_accessed(page);
  494. page_cache_release(page);
  495. copied += amt;
  496. index++;
  497. offset = 0;
  498. } while(copied < size);
  499. (*pos) += size;
  500. return size;
  501. }
  502. /**
  503. * gfs2_readpages - Read a bunch of pages at once
  504. *
  505. * Some notes:
  506. * 1. This is only for readahead, so we can simply ignore any things
  507. * which are slightly inconvenient (such as locking conflicts between
  508. * the page lock and the glock) and return having done no I/O. Its
  509. * obviously not something we'd want to do on too regular a basis.
  510. * Any I/O we ignore at this time will be done via readpage later.
  511. * 2. We don't handle stuffed files here we let readpage do the honours.
  512. * 3. mpage_readpages() does most of the heavy lifting in the common case.
  513. * 4. gfs2_block_map() is relied upon to set BH_Boundary in the right places.
  514. */
  515. static int gfs2_readpages(struct file *file, struct address_space *mapping,
  516. struct list_head *pages, unsigned nr_pages)
  517. {
  518. struct inode *inode = mapping->host;
  519. struct gfs2_inode *ip = GFS2_I(inode);
  520. struct gfs2_sbd *sdp = GFS2_SB(inode);
  521. struct gfs2_holder gh;
  522. int ret;
  523. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
  524. ret = gfs2_glock_nq_atime(&gh);
  525. if (unlikely(ret))
  526. goto out_uninit;
  527. if (!gfs2_is_stuffed(ip))
  528. ret = mpage_readpages(mapping, pages, nr_pages, gfs2_block_map);
  529. gfs2_glock_dq(&gh);
  530. out_uninit:
  531. gfs2_holder_uninit(&gh);
  532. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  533. ret = -EIO;
  534. return ret;
  535. }
  536. /**
  537. * gfs2_write_begin - Begin to write to a file
  538. * @file: The file to write to
  539. * @mapping: The mapping in which to write
  540. * @pos: The file offset at which to start writing
  541. * @len: Length of the write
  542. * @flags: Various flags
  543. * @pagep: Pointer to return the page
  544. * @fsdata: Pointer to return fs data (unused by GFS2)
  545. *
  546. * Returns: errno
  547. */
  548. static int gfs2_write_begin(struct file *file, struct address_space *mapping,
  549. loff_t pos, unsigned len, unsigned flags,
  550. struct page **pagep, void **fsdata)
  551. {
  552. struct gfs2_inode *ip = GFS2_I(mapping->host);
  553. struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
  554. unsigned int data_blocks, ind_blocks, rblocks;
  555. int alloc_required;
  556. int error = 0;
  557. struct gfs2_alloc *al;
  558. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  559. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  560. unsigned to = from + len;
  561. struct page *page;
  562. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME, &ip->i_gh);
  563. error = gfs2_glock_nq_atime(&ip->i_gh);
  564. if (unlikely(error))
  565. goto out_uninit;
  566. gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
  567. error = gfs2_write_alloc_required(ip, pos, len, &alloc_required);
  568. if (error)
  569. goto out_unlock;
  570. if (alloc_required) {
  571. al = gfs2_alloc_get(ip);
  572. if (!al) {
  573. error = -ENOMEM;
  574. goto out_unlock;
  575. }
  576. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  577. if (error)
  578. goto out_alloc_put;
  579. error = gfs2_quota_check(ip, ip->i_inode.i_uid, ip->i_inode.i_gid);
  580. if (error)
  581. goto out_qunlock;
  582. al->al_requested = data_blocks + ind_blocks;
  583. error = gfs2_inplace_reserve(ip);
  584. if (error)
  585. goto out_qunlock;
  586. }
  587. rblocks = RES_DINODE + ind_blocks;
  588. if (gfs2_is_jdata(ip))
  589. rblocks += data_blocks ? data_blocks : 1;
  590. if (ind_blocks || data_blocks)
  591. rblocks += RES_STATFS + RES_QUOTA;
  592. error = gfs2_trans_begin(sdp, rblocks,
  593. PAGE_CACHE_SIZE/sdp->sd_sb.sb_bsize);
  594. if (error)
  595. goto out_trans_fail;
  596. error = -ENOMEM;
  597. page = __grab_cache_page(mapping, index);
  598. *pagep = page;
  599. if (unlikely(!page))
  600. goto out_endtrans;
  601. if (gfs2_is_stuffed(ip)) {
  602. error = 0;
  603. if (pos + len > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
  604. error = gfs2_unstuff_dinode(ip, page);
  605. if (error == 0)
  606. goto prepare_write;
  607. } else if (!PageUptodate(page)) {
  608. error = stuffed_readpage(ip, page);
  609. }
  610. goto out;
  611. }
  612. prepare_write:
  613. error = block_prepare_write(page, from, to, gfs2_block_map);
  614. out:
  615. if (error == 0)
  616. return 0;
  617. page_cache_release(page);
  618. if (pos + len > ip->i_inode.i_size)
  619. vmtruncate(&ip->i_inode, ip->i_inode.i_size);
  620. out_endtrans:
  621. gfs2_trans_end(sdp);
  622. out_trans_fail:
  623. if (alloc_required) {
  624. gfs2_inplace_release(ip);
  625. out_qunlock:
  626. gfs2_quota_unlock(ip);
  627. out_alloc_put:
  628. gfs2_alloc_put(ip);
  629. }
  630. out_unlock:
  631. gfs2_glock_dq(&ip->i_gh);
  632. out_uninit:
  633. gfs2_holder_uninit(&ip->i_gh);
  634. return error;
  635. }
  636. /**
  637. * adjust_fs_space - Adjusts the free space available due to gfs2_grow
  638. * @inode: the rindex inode
  639. */
  640. static void adjust_fs_space(struct inode *inode)
  641. {
  642. struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
  643. struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
  644. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  645. u64 fs_total, new_free;
  646. /* Total up the file system space, according to the latest rindex. */
  647. fs_total = gfs2_ri_total(sdp);
  648. spin_lock(&sdp->sd_statfs_spin);
  649. if (fs_total > (m_sc->sc_total + l_sc->sc_total))
  650. new_free = fs_total - (m_sc->sc_total + l_sc->sc_total);
  651. else
  652. new_free = 0;
  653. spin_unlock(&sdp->sd_statfs_spin);
  654. fs_warn(sdp, "File system extended by %llu blocks.\n",
  655. (unsigned long long)new_free);
  656. gfs2_statfs_change(sdp, new_free, new_free, 0);
  657. }
  658. /**
  659. * gfs2_stuffed_write_end - Write end for stuffed files
  660. * @inode: The inode
  661. * @dibh: The buffer_head containing the on-disk inode
  662. * @pos: The file position
  663. * @len: The length of the write
  664. * @copied: How much was actually copied by the VFS
  665. * @page: The page
  666. *
  667. * This copies the data from the page into the inode block after
  668. * the inode data structure itself.
  669. *
  670. * Returns: errno
  671. */
  672. static int gfs2_stuffed_write_end(struct inode *inode, struct buffer_head *dibh,
  673. loff_t pos, unsigned len, unsigned copied,
  674. struct page *page)
  675. {
  676. struct gfs2_inode *ip = GFS2_I(inode);
  677. struct gfs2_sbd *sdp = GFS2_SB(inode);
  678. u64 to = pos + copied;
  679. void *kaddr;
  680. unsigned char *buf = dibh->b_data + sizeof(struct gfs2_dinode);
  681. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  682. BUG_ON((pos + len) > (dibh->b_size - sizeof(struct gfs2_dinode)));
  683. kaddr = kmap_atomic(page, KM_USER0);
  684. memcpy(buf + pos, kaddr + pos, copied);
  685. memset(kaddr + pos + copied, 0, len - copied);
  686. flush_dcache_page(page);
  687. kunmap_atomic(kaddr, KM_USER0);
  688. if (!PageUptodate(page))
  689. SetPageUptodate(page);
  690. unlock_page(page);
  691. page_cache_release(page);
  692. if (inode->i_size < to) {
  693. i_size_write(inode, to);
  694. ip->i_di.di_size = inode->i_size;
  695. di->di_size = cpu_to_be64(inode->i_size);
  696. mark_inode_dirty(inode);
  697. }
  698. if (inode == sdp->sd_rindex)
  699. adjust_fs_space(inode);
  700. brelse(dibh);
  701. gfs2_trans_end(sdp);
  702. gfs2_glock_dq(&ip->i_gh);
  703. gfs2_holder_uninit(&ip->i_gh);
  704. return copied;
  705. }
  706. /**
  707. * gfs2_write_end
  708. * @file: The file to write to
  709. * @mapping: The address space to write to
  710. * @pos: The file position
  711. * @len: The length of the data
  712. * @copied:
  713. * @page: The page that has been written
  714. * @fsdata: The fsdata (unused in GFS2)
  715. *
  716. * The main write_end function for GFS2. We have a separate one for
  717. * stuffed files as they are slightly different, otherwise we just
  718. * put our locking around the VFS provided functions.
  719. *
  720. * Returns: errno
  721. */
  722. static int gfs2_write_end(struct file *file, struct address_space *mapping,
  723. loff_t pos, unsigned len, unsigned copied,
  724. struct page *page, void *fsdata)
  725. {
  726. struct inode *inode = page->mapping->host;
  727. struct gfs2_inode *ip = GFS2_I(inode);
  728. struct gfs2_sbd *sdp = GFS2_SB(inode);
  729. struct buffer_head *dibh;
  730. struct gfs2_alloc *al = ip->i_alloc;
  731. struct gfs2_dinode *di;
  732. unsigned int from = pos & (PAGE_CACHE_SIZE - 1);
  733. unsigned int to = from + len;
  734. int ret;
  735. BUG_ON(gfs2_glock_is_locked_by_me(ip->i_gl) == NULL);
  736. ret = gfs2_meta_inode_buffer(ip, &dibh);
  737. if (unlikely(ret)) {
  738. unlock_page(page);
  739. page_cache_release(page);
  740. goto failed;
  741. }
  742. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  743. if (gfs2_is_stuffed(ip))
  744. return gfs2_stuffed_write_end(inode, dibh, pos, len, copied, page);
  745. if (!gfs2_is_writeback(ip))
  746. gfs2_page_add_databufs(ip, page, from, to);
  747. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  748. if (likely(ret >= 0) && (inode->i_size > ip->i_di.di_size)) {
  749. di = (struct gfs2_dinode *)dibh->b_data;
  750. ip->i_di.di_size = inode->i_size;
  751. di->di_size = cpu_to_be64(inode->i_size);
  752. mark_inode_dirty(inode);
  753. }
  754. if (inode == sdp->sd_rindex)
  755. adjust_fs_space(inode);
  756. brelse(dibh);
  757. gfs2_trans_end(sdp);
  758. failed:
  759. if (al) {
  760. gfs2_inplace_release(ip);
  761. gfs2_quota_unlock(ip);
  762. gfs2_alloc_put(ip);
  763. }
  764. gfs2_glock_dq(&ip->i_gh);
  765. gfs2_holder_uninit(&ip->i_gh);
  766. return ret;
  767. }
  768. /**
  769. * gfs2_set_page_dirty - Page dirtying function
  770. * @page: The page to dirty
  771. *
  772. * Returns: 1 if it dirtyed the page, or 0 otherwise
  773. */
  774. static int gfs2_set_page_dirty(struct page *page)
  775. {
  776. SetPageChecked(page);
  777. return __set_page_dirty_buffers(page);
  778. }
  779. /**
  780. * gfs2_bmap - Block map function
  781. * @mapping: Address space info
  782. * @lblock: The block to map
  783. *
  784. * Returns: The disk address for the block or 0 on hole or error
  785. */
  786. static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
  787. {
  788. struct gfs2_inode *ip = GFS2_I(mapping->host);
  789. struct gfs2_holder i_gh;
  790. sector_t dblock = 0;
  791. int error;
  792. error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  793. if (error)
  794. return 0;
  795. if (!gfs2_is_stuffed(ip))
  796. dblock = generic_block_bmap(mapping, lblock, gfs2_block_map);
  797. gfs2_glock_dq_uninit(&i_gh);
  798. return dblock;
  799. }
  800. static void gfs2_discard(struct gfs2_sbd *sdp, struct buffer_head *bh)
  801. {
  802. struct gfs2_bufdata *bd;
  803. lock_buffer(bh);
  804. gfs2_log_lock(sdp);
  805. clear_buffer_dirty(bh);
  806. bd = bh->b_private;
  807. if (bd) {
  808. if (!list_empty(&bd->bd_le.le_list) && !buffer_pinned(bh))
  809. list_del_init(&bd->bd_le.le_list);
  810. else
  811. gfs2_remove_from_journal(bh, current->journal_info, 0);
  812. }
  813. bh->b_bdev = NULL;
  814. clear_buffer_mapped(bh);
  815. clear_buffer_req(bh);
  816. clear_buffer_new(bh);
  817. gfs2_log_unlock(sdp);
  818. unlock_buffer(bh);
  819. }
  820. static void gfs2_invalidatepage(struct page *page, unsigned long offset)
  821. {
  822. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  823. struct buffer_head *bh, *head;
  824. unsigned long pos = 0;
  825. BUG_ON(!PageLocked(page));
  826. if (offset == 0)
  827. ClearPageChecked(page);
  828. if (!page_has_buffers(page))
  829. goto out;
  830. bh = head = page_buffers(page);
  831. do {
  832. if (offset <= pos)
  833. gfs2_discard(sdp, bh);
  834. pos += bh->b_size;
  835. bh = bh->b_this_page;
  836. } while (bh != head);
  837. out:
  838. if (offset == 0)
  839. try_to_release_page(page, 0);
  840. }
  841. /**
  842. * gfs2_ok_for_dio - check that dio is valid on this file
  843. * @ip: The inode
  844. * @rw: READ or WRITE
  845. * @offset: The offset at which we are reading or writing
  846. *
  847. * Returns: 0 (to ignore the i/o request and thus fall back to buffered i/o)
  848. * 1 (to accept the i/o request)
  849. */
  850. static int gfs2_ok_for_dio(struct gfs2_inode *ip, int rw, loff_t offset)
  851. {
  852. /*
  853. * Should we return an error here? I can't see that O_DIRECT for
  854. * a stuffed file makes any sense. For now we'll silently fall
  855. * back to buffered I/O
  856. */
  857. if (gfs2_is_stuffed(ip))
  858. return 0;
  859. if (offset > i_size_read(&ip->i_inode))
  860. return 0;
  861. return 1;
  862. }
  863. static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
  864. const struct iovec *iov, loff_t offset,
  865. unsigned long nr_segs)
  866. {
  867. struct file *file = iocb->ki_filp;
  868. struct inode *inode = file->f_mapping->host;
  869. struct gfs2_inode *ip = GFS2_I(inode);
  870. struct gfs2_holder gh;
  871. int rv;
  872. /*
  873. * Deferred lock, even if its a write, since we do no allocation
  874. * on this path. All we need change is atime, and this lock mode
  875. * ensures that other nodes have flushed their buffered read caches
  876. * (i.e. their page cache entries for this inode). We do not,
  877. * unfortunately have the option of only flushing a range like
  878. * the VFS does.
  879. */
  880. gfs2_holder_init(ip->i_gl, LM_ST_DEFERRED, GL_ATIME, &gh);
  881. rv = gfs2_glock_nq_atime(&gh);
  882. if (rv)
  883. return rv;
  884. rv = gfs2_ok_for_dio(ip, rw, offset);
  885. if (rv != 1)
  886. goto out; /* dio not valid, fall back to buffered i/o */
  887. rv = blockdev_direct_IO_no_locking(rw, iocb, inode, inode->i_sb->s_bdev,
  888. iov, offset, nr_segs,
  889. gfs2_get_block_direct, NULL);
  890. out:
  891. gfs2_glock_dq_m(1, &gh);
  892. gfs2_holder_uninit(&gh);
  893. return rv;
  894. }
  895. /**
  896. * gfs2_releasepage - free the metadata associated with a page
  897. * @page: the page that's being released
  898. * @gfp_mask: passed from Linux VFS, ignored by us
  899. *
  900. * Call try_to_free_buffers() if the buffers in this page can be
  901. * released.
  902. *
  903. * Returns: 0
  904. */
  905. int gfs2_releasepage(struct page *page, gfp_t gfp_mask)
  906. {
  907. struct inode *aspace = page->mapping->host;
  908. struct gfs2_sbd *sdp = aspace->i_sb->s_fs_info;
  909. struct buffer_head *bh, *head;
  910. struct gfs2_bufdata *bd;
  911. if (!page_has_buffers(page))
  912. return 0;
  913. gfs2_log_lock(sdp);
  914. head = bh = page_buffers(page);
  915. do {
  916. if (atomic_read(&bh->b_count))
  917. goto cannot_release;
  918. bd = bh->b_private;
  919. if (bd && bd->bd_ail)
  920. goto cannot_release;
  921. gfs2_assert_warn(sdp, !buffer_pinned(bh));
  922. gfs2_assert_warn(sdp, !buffer_dirty(bh));
  923. bh = bh->b_this_page;
  924. } while(bh != head);
  925. gfs2_log_unlock(sdp);
  926. head = bh = page_buffers(page);
  927. do {
  928. gfs2_log_lock(sdp);
  929. bd = bh->b_private;
  930. if (bd) {
  931. gfs2_assert_warn(sdp, bd->bd_bh == bh);
  932. gfs2_assert_warn(sdp, list_empty(&bd->bd_list_tr));
  933. if (!list_empty(&bd->bd_le.le_list)) {
  934. if (!buffer_pinned(bh))
  935. list_del_init(&bd->bd_le.le_list);
  936. else
  937. bd = NULL;
  938. }
  939. if (bd)
  940. bd->bd_bh = NULL;
  941. bh->b_private = NULL;
  942. }
  943. gfs2_log_unlock(sdp);
  944. if (bd)
  945. kmem_cache_free(gfs2_bufdata_cachep, bd);
  946. bh = bh->b_this_page;
  947. } while (bh != head);
  948. return try_to_free_buffers(page);
  949. cannot_release:
  950. gfs2_log_unlock(sdp);
  951. return 0;
  952. }
  953. static const struct address_space_operations gfs2_writeback_aops = {
  954. .writepage = gfs2_writeback_writepage,
  955. .writepages = gfs2_writeback_writepages,
  956. .readpage = gfs2_readpage,
  957. .readpages = gfs2_readpages,
  958. .sync_page = block_sync_page,
  959. .write_begin = gfs2_write_begin,
  960. .write_end = gfs2_write_end,
  961. .bmap = gfs2_bmap,
  962. .invalidatepage = gfs2_invalidatepage,
  963. .releasepage = gfs2_releasepage,
  964. .direct_IO = gfs2_direct_IO,
  965. .migratepage = buffer_migrate_page,
  966. };
  967. static const struct address_space_operations gfs2_ordered_aops = {
  968. .writepage = gfs2_ordered_writepage,
  969. .readpage = gfs2_readpage,
  970. .readpages = gfs2_readpages,
  971. .sync_page = block_sync_page,
  972. .write_begin = gfs2_write_begin,
  973. .write_end = gfs2_write_end,
  974. .set_page_dirty = gfs2_set_page_dirty,
  975. .bmap = gfs2_bmap,
  976. .invalidatepage = gfs2_invalidatepage,
  977. .releasepage = gfs2_releasepage,
  978. .direct_IO = gfs2_direct_IO,
  979. .migratepage = buffer_migrate_page,
  980. };
  981. static const struct address_space_operations gfs2_jdata_aops = {
  982. .writepage = gfs2_jdata_writepage,
  983. .writepages = gfs2_jdata_writepages,
  984. .readpage = gfs2_readpage,
  985. .readpages = gfs2_readpages,
  986. .sync_page = block_sync_page,
  987. .write_begin = gfs2_write_begin,
  988. .write_end = gfs2_write_end,
  989. .set_page_dirty = gfs2_set_page_dirty,
  990. .bmap = gfs2_bmap,
  991. .invalidatepage = gfs2_invalidatepage,
  992. .releasepage = gfs2_releasepage,
  993. };
  994. void gfs2_set_aops(struct inode *inode)
  995. {
  996. struct gfs2_inode *ip = GFS2_I(inode);
  997. if (gfs2_is_writeback(ip))
  998. inode->i_mapping->a_ops = &gfs2_writeback_aops;
  999. else if (gfs2_is_ordered(ip))
  1000. inode->i_mapping->a_ops = &gfs2_ordered_aops;
  1001. else if (gfs2_is_jdata(ip))
  1002. inode->i_mapping->a_ops = &gfs2_jdata_aops;
  1003. else
  1004. BUG();
  1005. }