aops.c 30 KB

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