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 error;
  189. int done_trans = 0;
  190. error = gfs2_writepage_common(page, wbc);
  191. if (error <= 0)
  192. return error;
  193. if (PageChecked(page)) {
  194. if (wbc->sync_mode != WB_SYNC_ALL)
  195. goto out_ignore;
  196. error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
  197. if (error)
  198. goto out_ignore;
  199. done_trans = 1;
  200. }
  201. error = __gfs2_jdata_writepage(page, wbc);
  202. if (done_trans)
  203. gfs2_trans_end(sdp);
  204. return error;
  205. out_ignore:
  206. redirty_page_for_writepage(wbc, page);
  207. unlock_page(page);
  208. return 0;
  209. }
  210. /**
  211. * gfs2_writeback_writepages - Write a bunch of dirty pages back to disk
  212. * @mapping: The mapping to write
  213. * @wbc: Write-back control
  214. *
  215. * For the data=writeback case we can already ignore buffer heads
  216. * and write whole extents at once. This is a big reduction in the
  217. * number of I/O requests we send and the bmap calls we make in this case.
  218. */
  219. static int gfs2_writeback_writepages(struct address_space *mapping,
  220. struct writeback_control *wbc)
  221. {
  222. return mpage_writepages(mapping, wbc, gfs2_get_block_noalloc);
  223. }
  224. /**
  225. * gfs2_write_jdata_pagevec - Write back a pagevec's worth of pages
  226. * @mapping: The mapping
  227. * @wbc: The writeback control
  228. * @writepage: The writepage function to call for each page
  229. * @pvec: The vector of pages
  230. * @nr_pages: The number of pages to write
  231. *
  232. * Returns: non-zero if loop should terminate, zero otherwise
  233. */
  234. static int gfs2_write_jdata_pagevec(struct address_space *mapping,
  235. struct writeback_control *wbc,
  236. struct pagevec *pvec,
  237. int nr_pages, pgoff_t end)
  238. {
  239. struct inode *inode = mapping->host;
  240. struct gfs2_sbd *sdp = GFS2_SB(inode);
  241. loff_t i_size = i_size_read(inode);
  242. pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
  243. unsigned offset = i_size & (PAGE_CACHE_SIZE-1);
  244. unsigned nrblocks = nr_pages * (PAGE_CACHE_SIZE/inode->i_sb->s_blocksize);
  245. struct backing_dev_info *bdi = mapping->backing_dev_info;
  246. int i;
  247. int ret;
  248. ret = gfs2_trans_begin(sdp, nrblocks, nrblocks);
  249. if (ret < 0)
  250. return ret;
  251. for(i = 0; i < nr_pages; i++) {
  252. struct page *page = pvec->pages[i];
  253. lock_page(page);
  254. if (unlikely(page->mapping != mapping)) {
  255. unlock_page(page);
  256. continue;
  257. }
  258. if (!wbc->range_cyclic && page->index > end) {
  259. ret = 1;
  260. unlock_page(page);
  261. continue;
  262. }
  263. if (wbc->sync_mode != WB_SYNC_NONE)
  264. wait_on_page_writeback(page);
  265. if (PageWriteback(page) ||
  266. !clear_page_dirty_for_io(page)) {
  267. unlock_page(page);
  268. continue;
  269. }
  270. /* Is the page fully outside i_size? (truncate in progress) */
  271. if (page->index > end_index || (page->index == end_index && !offset)) {
  272. page->mapping->a_ops->invalidatepage(page, 0);
  273. unlock_page(page);
  274. continue;
  275. }
  276. ret = __gfs2_jdata_writepage(page, wbc);
  277. if (ret || (--(wbc->nr_to_write) <= 0))
  278. ret = 1;
  279. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  280. wbc->encountered_congestion = 1;
  281. ret = 1;
  282. }
  283. }
  284. gfs2_trans_end(sdp);
  285. return ret;
  286. }
  287. /**
  288. * gfs2_write_cache_jdata - Like write_cache_pages but different
  289. * @mapping: The mapping to write
  290. * @wbc: The writeback control
  291. * @writepage: The writepage function to call
  292. * @data: The data to pass to writepage
  293. *
  294. * The reason that we use our own function here is that we need to
  295. * start transactions before we grab page locks. This allows us
  296. * to get the ordering right.
  297. */
  298. static int gfs2_write_cache_jdata(struct address_space *mapping,
  299. struct writeback_control *wbc)
  300. {
  301. struct backing_dev_info *bdi = mapping->backing_dev_info;
  302. int ret = 0;
  303. int done = 0;
  304. struct pagevec pvec;
  305. int nr_pages;
  306. pgoff_t index;
  307. pgoff_t end;
  308. int scanned = 0;
  309. int range_whole = 0;
  310. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  311. wbc->encountered_congestion = 1;
  312. return 0;
  313. }
  314. pagevec_init(&pvec, 0);
  315. if (wbc->range_cyclic) {
  316. index = mapping->writeback_index; /* Start from prev offset */
  317. end = -1;
  318. } else {
  319. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  320. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  321. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  322. range_whole = 1;
  323. scanned = 1;
  324. }
  325. retry:
  326. while (!done && (index <= end) &&
  327. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  328. PAGECACHE_TAG_DIRTY,
  329. min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
  330. scanned = 1;
  331. ret = gfs2_write_jdata_pagevec(mapping, wbc, &pvec, nr_pages, end);
  332. if (ret)
  333. done = 1;
  334. if (ret > 0)
  335. ret = 0;
  336. pagevec_release(&pvec);
  337. cond_resched();
  338. }
  339. if (!scanned && !done) {
  340. /*
  341. * We hit the last page and there is more work to be done: wrap
  342. * back to the start of the file
  343. */
  344. scanned = 1;
  345. index = 0;
  346. goto retry;
  347. }
  348. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  349. mapping->writeback_index = index;
  350. return ret;
  351. }
  352. /**
  353. * gfs2_jdata_writepages - Write a bunch of dirty pages back to disk
  354. * @mapping: The mapping to write
  355. * @wbc: The writeback control
  356. *
  357. */
  358. static int gfs2_jdata_writepages(struct address_space *mapping,
  359. struct writeback_control *wbc)
  360. {
  361. struct gfs2_inode *ip = GFS2_I(mapping->host);
  362. struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
  363. int ret;
  364. ret = gfs2_write_cache_jdata(mapping, wbc);
  365. if (ret == 0 && wbc->sync_mode == WB_SYNC_ALL) {
  366. gfs2_log_flush(sdp, ip->i_gl);
  367. ret = gfs2_write_cache_jdata(mapping, wbc);
  368. }
  369. return ret;
  370. }
  371. /**
  372. * stuffed_readpage - Fill in a Linux page with stuffed file data
  373. * @ip: the inode
  374. * @page: the page
  375. *
  376. * Returns: errno
  377. */
  378. static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
  379. {
  380. struct buffer_head *dibh;
  381. void *kaddr;
  382. int error;
  383. /*
  384. * Due to the order of unstuffing files and ->fault(), we can be
  385. * asked for a zero page in the case of a stuffed file being extended,
  386. * so we need to supply one here. It doesn't happen often.
  387. */
  388. if (unlikely(page->index)) {
  389. zero_user(page, 0, PAGE_CACHE_SIZE);
  390. return 0;
  391. }
  392. error = gfs2_meta_inode_buffer(ip, &dibh);
  393. if (error)
  394. return error;
  395. kaddr = kmap_atomic(page, KM_USER0);
  396. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode),
  397. ip->i_di.di_size);
  398. memset(kaddr + ip->i_di.di_size, 0, PAGE_CACHE_SIZE - ip->i_di.di_size);
  399. kunmap_atomic(kaddr, KM_USER0);
  400. flush_dcache_page(page);
  401. brelse(dibh);
  402. SetPageUptodate(page);
  403. return 0;
  404. }
  405. /**
  406. * __gfs2_readpage - readpage
  407. * @file: The file to read a page for
  408. * @page: The page to read
  409. *
  410. * This is the core of gfs2's readpage. Its used by the internal file
  411. * reading code as in that case we already hold the glock. Also its
  412. * called by gfs2_readpage() once the required lock has been granted.
  413. *
  414. */
  415. static int __gfs2_readpage(void *file, struct page *page)
  416. {
  417. struct gfs2_inode *ip = GFS2_I(page->mapping->host);
  418. struct gfs2_sbd *sdp = GFS2_SB(page->mapping->host);
  419. int error;
  420. if (gfs2_is_stuffed(ip)) {
  421. error = stuffed_readpage(ip, page);
  422. unlock_page(page);
  423. } else {
  424. error = mpage_readpage(page, gfs2_block_map);
  425. }
  426. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  427. return -EIO;
  428. return error;
  429. }
  430. /**
  431. * gfs2_readpage - read a page of a file
  432. * @file: The file to read
  433. * @page: The page of the file
  434. *
  435. * This deals with the locking required. We have to unlock and
  436. * relock the page in order to get the locking in the right
  437. * order.
  438. */
  439. static int gfs2_readpage(struct file *file, struct page *page)
  440. {
  441. struct address_space *mapping = page->mapping;
  442. struct gfs2_inode *ip = GFS2_I(mapping->host);
  443. struct gfs2_holder gh;
  444. int error;
  445. unlock_page(page);
  446. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  447. error = gfs2_glock_nq(&gh);
  448. if (unlikely(error))
  449. goto out;
  450. error = AOP_TRUNCATED_PAGE;
  451. lock_page(page);
  452. if (page->mapping == mapping && !PageUptodate(page))
  453. error = __gfs2_readpage(file, page);
  454. else
  455. unlock_page(page);
  456. gfs2_glock_dq(&gh);
  457. out:
  458. gfs2_holder_uninit(&gh);
  459. if (error && error != AOP_TRUNCATED_PAGE)
  460. lock_page(page);
  461. return error;
  462. }
  463. /**
  464. * gfs2_internal_read - read an internal file
  465. * @ip: The gfs2 inode
  466. * @ra_state: The readahead state (or NULL for no readahead)
  467. * @buf: The buffer to fill
  468. * @pos: The file position
  469. * @size: The amount to read
  470. *
  471. */
  472. int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
  473. char *buf, loff_t *pos, unsigned size)
  474. {
  475. struct address_space *mapping = ip->i_inode.i_mapping;
  476. unsigned long index = *pos / PAGE_CACHE_SIZE;
  477. unsigned offset = *pos & (PAGE_CACHE_SIZE - 1);
  478. unsigned copied = 0;
  479. unsigned amt;
  480. struct page *page;
  481. void *p;
  482. do {
  483. amt = size - copied;
  484. if (offset + size > PAGE_CACHE_SIZE)
  485. amt = PAGE_CACHE_SIZE - offset;
  486. page = read_cache_page(mapping, index, __gfs2_readpage, NULL);
  487. if (IS_ERR(page))
  488. return PTR_ERR(page);
  489. p = kmap_atomic(page, KM_USER0);
  490. memcpy(buf + copied, p + offset, amt);
  491. kunmap_atomic(p, KM_USER0);
  492. mark_page_accessed(page);
  493. page_cache_release(page);
  494. copied += amt;
  495. index++;
  496. offset = 0;
  497. } while(copied < size);
  498. (*pos) += size;
  499. return size;
  500. }
  501. /**
  502. * gfs2_readpages - Read a bunch of pages at once
  503. *
  504. * Some notes:
  505. * 1. This is only for readahead, so we can simply ignore any things
  506. * which are slightly inconvenient (such as locking conflicts between
  507. * the page lock and the glock) and return having done no I/O. Its
  508. * obviously not something we'd want to do on too regular a basis.
  509. * Any I/O we ignore at this time will be done via readpage later.
  510. * 2. We don't handle stuffed files here we let readpage do the honours.
  511. * 3. mpage_readpages() does most of the heavy lifting in the common case.
  512. * 4. gfs2_block_map() is relied upon to set BH_Boundary in the right places.
  513. */
  514. static int gfs2_readpages(struct file *file, struct address_space *mapping,
  515. struct list_head *pages, unsigned nr_pages)
  516. {
  517. struct inode *inode = mapping->host;
  518. struct gfs2_inode *ip = GFS2_I(inode);
  519. struct gfs2_sbd *sdp = GFS2_SB(inode);
  520. struct gfs2_holder gh;
  521. int ret;
  522. gfs2_holder_init(ip->i_gl, LM_ST_SHARED, 0, &gh);
  523. ret = gfs2_glock_nq(&gh);
  524. if (unlikely(ret))
  525. goto out_uninit;
  526. if (!gfs2_is_stuffed(ip))
  527. ret = mpage_readpages(mapping, pages, nr_pages, gfs2_block_map);
  528. gfs2_glock_dq(&gh);
  529. out_uninit:
  530. gfs2_holder_uninit(&gh);
  531. if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
  532. ret = -EIO;
  533. return ret;
  534. }
  535. /**
  536. * gfs2_write_begin - Begin to write to a file
  537. * @file: The file to write to
  538. * @mapping: The mapping in which to write
  539. * @pos: The file offset at which to start writing
  540. * @len: Length of the write
  541. * @flags: Various flags
  542. * @pagep: Pointer to return the page
  543. * @fsdata: Pointer to return fs data (unused by GFS2)
  544. *
  545. * Returns: errno
  546. */
  547. static int gfs2_write_begin(struct file *file, struct address_space *mapping,
  548. loff_t pos, unsigned len, unsigned flags,
  549. struct page **pagep, void **fsdata)
  550. {
  551. struct gfs2_inode *ip = GFS2_I(mapping->host);
  552. struct gfs2_sbd *sdp = GFS2_SB(mapping->host);
  553. unsigned int data_blocks, ind_blocks, rblocks;
  554. int alloc_required;
  555. int error = 0;
  556. struct gfs2_alloc *al;
  557. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  558. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  559. unsigned to = from + len;
  560. struct page *page;
  561. gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ip->i_gh);
  562. error = gfs2_glock_nq(&ip->i_gh);
  563. if (unlikely(error))
  564. goto out_uninit;
  565. gfs2_write_calc_reserv(ip, len, &data_blocks, &ind_blocks);
  566. error = gfs2_write_alloc_required(ip, pos, len, &alloc_required);
  567. if (error)
  568. goto out_unlock;
  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_di.di_size = 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_di.di_size)) {
  745. di = (struct gfs2_dinode *)dibh->b_data;
  746. ip->i_di.di_size = 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. }