ops_address.c 29 KB

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