ops_address.c 29 KB

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