ops_address.c 29 KB

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