aops.c 31 KB

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