lops.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/slab.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/completion.h>
  10. #include <linux/buffer_head.h>
  11. #include <linux/mempool.h>
  12. #include <linux/gfs2_ondisk.h>
  13. #include <linux/bio.h>
  14. #include <linux/fs.h>
  15. #include <linux/list_sort.h>
  16. #include <linux/blkdev.h>
  17. #include "bmap.h"
  18. #include "dir.h"
  19. #include "gfs2.h"
  20. #include "incore.h"
  21. #include "inode.h"
  22. #include "glock.h"
  23. #include "glops.h"
  24. #include "log.h"
  25. #include "lops.h"
  26. #include "meta_io.h"
  27. #include "recovery.h"
  28. #include "rgrp.h"
  29. #include "trans.h"
  30. #include "util.h"
  31. #include "trace_gfs2.h"
  32. /**
  33. * gfs2_pin - Pin a buffer in memory
  34. * @sdp: The superblock
  35. * @bh: The buffer to be pinned
  36. *
  37. * The log lock must be held when calling this function
  38. */
  39. void gfs2_pin(struct gfs2_sbd *sdp, struct buffer_head *bh)
  40. {
  41. struct gfs2_bufdata *bd;
  42. BUG_ON(!current->journal_info);
  43. clear_buffer_dirty(bh);
  44. if (test_set_buffer_pinned(bh))
  45. gfs2_assert_withdraw(sdp, 0);
  46. if (!buffer_uptodate(bh))
  47. gfs2_io_error_bh_wd(sdp, bh);
  48. bd = bh->b_private;
  49. /* If this buffer is in the AIL and it has already been written
  50. * to in-place disk block, remove it from the AIL.
  51. */
  52. spin_lock(&sdp->sd_ail_lock);
  53. if (bd->bd_tr)
  54. list_move(&bd->bd_ail_st_list, &bd->bd_tr->tr_ail2_list);
  55. spin_unlock(&sdp->sd_ail_lock);
  56. get_bh(bh);
  57. atomic_inc(&sdp->sd_log_pinned);
  58. trace_gfs2_pin(bd, 1);
  59. }
  60. static bool buffer_is_rgrp(const struct gfs2_bufdata *bd)
  61. {
  62. return bd->bd_gl->gl_name.ln_type == LM_TYPE_RGRP;
  63. }
  64. static void maybe_release_space(struct gfs2_bufdata *bd)
  65. {
  66. struct gfs2_glock *gl = bd->bd_gl;
  67. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  68. struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
  69. unsigned int index = bd->bd_bh->b_blocknr - gl->gl_name.ln_number;
  70. struct gfs2_bitmap *bi = rgd->rd_bits + index;
  71. rgrp_lock_local(rgd);
  72. if (bi->bi_clone == NULL)
  73. goto out;
  74. if (sdp->sd_args.ar_discard)
  75. gfs2_rgrp_send_discards(sdp, rgd->rd_data0, bd->bd_bh, bi, 1, NULL);
  76. memcpy(bi->bi_clone + bi->bi_offset,
  77. bd->bd_bh->b_data + bi->bi_offset, bi->bi_bytes);
  78. clear_bit(GBF_FULL, &bi->bi_flags);
  79. rgd->rd_free_clone = rgd->rd_free;
  80. BUG_ON(rgd->rd_free_clone < rgd->rd_reserved);
  81. rgd->rd_extfail_pt = rgd->rd_free;
  82. out:
  83. rgrp_unlock_local(rgd);
  84. }
  85. /**
  86. * gfs2_unpin - Unpin a buffer
  87. * @sdp: the filesystem the buffer belongs to
  88. * @bh: The buffer to unpin
  89. * @tr: The system transaction being flushed
  90. */
  91. static void gfs2_unpin(struct gfs2_sbd *sdp, struct buffer_head *bh,
  92. struct gfs2_trans *tr)
  93. {
  94. struct gfs2_bufdata *bd = bh->b_private;
  95. BUG_ON(!buffer_uptodate(bh));
  96. BUG_ON(!buffer_pinned(bh));
  97. lock_buffer(bh);
  98. mark_buffer_dirty(bh);
  99. clear_buffer_pinned(bh);
  100. if (buffer_is_rgrp(bd))
  101. maybe_release_space(bd);
  102. spin_lock(&sdp->sd_ail_lock);
  103. if (bd->bd_tr) {
  104. list_del(&bd->bd_ail_st_list);
  105. brelse(bh);
  106. } else {
  107. struct gfs2_glock *gl = bd->bd_gl;
  108. list_add(&bd->bd_ail_gl_list, &gl->gl_ail_list);
  109. atomic_inc(&gl->gl_ail_count);
  110. }
  111. bd->bd_tr = tr;
  112. list_add(&bd->bd_ail_st_list, &tr->tr_ail1_list);
  113. spin_unlock(&sdp->sd_ail_lock);
  114. clear_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
  115. trace_gfs2_pin(bd, 0);
  116. unlock_buffer(bh);
  117. atomic_dec(&sdp->sd_log_pinned);
  118. }
  119. void gfs2_log_incr_head(struct gfs2_sbd *sdp)
  120. {
  121. BUG_ON((sdp->sd_log_flush_head == sdp->sd_log_tail) &&
  122. (sdp->sd_log_flush_head != sdp->sd_log_head));
  123. if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks)
  124. sdp->sd_log_flush_head = 0;
  125. }
  126. u64 gfs2_log_bmap(struct gfs2_jdesc *jd, unsigned int lblock)
  127. {
  128. struct gfs2_journal_extent *je;
  129. list_for_each_entry(je, &jd->extent_list, list) {
  130. if (lblock >= je->lblock && lblock < je->lblock + je->blocks)
  131. return je->dblock + lblock - je->lblock;
  132. }
  133. return -1;
  134. }
  135. /**
  136. * gfs2_end_log_write_bh - end log write of pagecache data with buffers
  137. * @sdp: The superblock
  138. * @bvec: The bio_vec
  139. * @error: The i/o status
  140. *
  141. * This finds the relevant buffers and unlocks them and sets the
  142. * error flag according to the status of the i/o request. This is
  143. * used when the log is writing data which has an in-place version
  144. * that is pinned in the pagecache.
  145. */
  146. static void gfs2_end_log_write_bh(struct gfs2_sbd *sdp,
  147. struct bio_vec *bvec,
  148. blk_status_t error)
  149. {
  150. struct buffer_head *bh, *next;
  151. struct page *page = bvec->bv_page;
  152. unsigned size;
  153. bh = page_buffers(page);
  154. size = bvec->bv_len;
  155. while (bh_offset(bh) < bvec->bv_offset)
  156. bh = bh->b_this_page;
  157. do {
  158. if (error)
  159. mark_buffer_write_io_error(bh);
  160. unlock_buffer(bh);
  161. next = bh->b_this_page;
  162. size -= bh->b_size;
  163. brelse(bh);
  164. bh = next;
  165. } while(bh && size);
  166. }
  167. /**
  168. * gfs2_end_log_write - end of i/o to the log
  169. * @bio: The bio
  170. *
  171. * Each bio_vec contains either data from the pagecache or data
  172. * relating to the log itself. Here we iterate over the bio_vec
  173. * array, processing both kinds of data.
  174. *
  175. */
  176. static void gfs2_end_log_write(struct bio *bio)
  177. {
  178. struct gfs2_sbd *sdp = bio->bi_private;
  179. struct bio_vec *bvec;
  180. struct page *page;
  181. struct bvec_iter_all iter_all;
  182. if (bio->bi_status) {
  183. if (!cmpxchg(&sdp->sd_log_error, 0, (int)bio->bi_status))
  184. fs_err(sdp, "Error %d writing to journal, jid=%u\n",
  185. bio->bi_status, sdp->sd_jdesc->jd_jid);
  186. gfs2_withdraw_delayed(sdp);
  187. /* prevent more writes to the journal */
  188. clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  189. wake_up(&sdp->sd_logd_waitq);
  190. }
  191. bio_for_each_segment_all(bvec, bio, iter_all) {
  192. page = bvec->bv_page;
  193. if (page_has_buffers(page))
  194. gfs2_end_log_write_bh(sdp, bvec, bio->bi_status);
  195. else
  196. mempool_free(page, gfs2_page_pool);
  197. }
  198. bio_put(bio);
  199. if (atomic_dec_and_test(&sdp->sd_log_in_flight))
  200. wake_up(&sdp->sd_log_flush_wait);
  201. }
  202. /**
  203. * gfs2_log_submit_bio - Submit any pending log bio
  204. * @biop: Address of the bio pointer
  205. * @opf: REQ_OP | op_flags
  206. *
  207. * Submit any pending part-built or full bio to the block device. If
  208. * there is no pending bio, then this is a no-op.
  209. */
  210. void gfs2_log_submit_bio(struct bio **biop, blk_opf_t opf)
  211. {
  212. struct bio *bio = *biop;
  213. if (bio) {
  214. struct gfs2_sbd *sdp = bio->bi_private;
  215. atomic_inc(&sdp->sd_log_in_flight);
  216. bio->bi_opf = opf;
  217. submit_bio(bio);
  218. *biop = NULL;
  219. }
  220. }
  221. /**
  222. * gfs2_log_alloc_bio - Allocate a bio
  223. * @sdp: The super block
  224. * @blkno: The device block number we want to write to
  225. * @end_io: The bi_end_io callback
  226. *
  227. * Allocate a new bio, initialize it with the given parameters and return it.
  228. *
  229. * Returns: The newly allocated bio
  230. */
  231. static struct bio *gfs2_log_alloc_bio(struct gfs2_sbd *sdp, u64 blkno,
  232. bio_end_io_t *end_io)
  233. {
  234. struct super_block *sb = sdp->sd_vfs;
  235. struct bio *bio = bio_alloc(sb->s_bdev, BIO_MAX_VECS, 0, GFP_NOIO);
  236. bio->bi_iter.bi_sector = blkno << sdp->sd_fsb2bb_shift;
  237. bio->bi_end_io = end_io;
  238. bio->bi_private = sdp;
  239. return bio;
  240. }
  241. /**
  242. * gfs2_log_get_bio - Get cached log bio, or allocate a new one
  243. * @sdp: The super block
  244. * @blkno: The device block number we want to write to
  245. * @biop: The bio to get or allocate
  246. * @op: REQ_OP
  247. * @end_io: The bi_end_io callback
  248. * @flush: Always flush the current bio and allocate a new one?
  249. *
  250. * If there is a cached bio, then if the next block number is sequential
  251. * with the previous one, return it, otherwise flush the bio to the
  252. * device. If there is no cached bio, or we just flushed it, then
  253. * allocate a new one.
  254. *
  255. * Returns: The bio to use for log writes
  256. */
  257. static struct bio *gfs2_log_get_bio(struct gfs2_sbd *sdp, u64 blkno,
  258. struct bio **biop, enum req_op op,
  259. bio_end_io_t *end_io, bool flush)
  260. {
  261. struct bio *bio = *biop;
  262. if (bio) {
  263. u64 nblk;
  264. nblk = bio_end_sector(bio);
  265. nblk >>= sdp->sd_fsb2bb_shift;
  266. if (blkno == nblk && !flush)
  267. return bio;
  268. gfs2_log_submit_bio(biop, op);
  269. }
  270. *biop = gfs2_log_alloc_bio(sdp, blkno, end_io);
  271. return *biop;
  272. }
  273. /**
  274. * gfs2_log_write - write to log
  275. * @sdp: the filesystem
  276. * @jd: The journal descriptor
  277. * @page: the page to write
  278. * @size: the size of the data to write
  279. * @offset: the offset within the page
  280. * @blkno: block number of the log entry
  281. *
  282. * Try and add the page segment to the current bio. If that fails,
  283. * submit the current bio to the device and create a new one, and
  284. * then add the page segment to that.
  285. */
  286. void gfs2_log_write(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd,
  287. struct page *page, unsigned size, unsigned offset,
  288. u64 blkno)
  289. {
  290. struct bio *bio;
  291. int ret;
  292. bio = gfs2_log_get_bio(sdp, blkno, &jd->jd_log_bio, REQ_OP_WRITE,
  293. gfs2_end_log_write, false);
  294. ret = bio_add_page(bio, page, size, offset);
  295. if (ret == 0) {
  296. bio = gfs2_log_get_bio(sdp, blkno, &jd->jd_log_bio,
  297. REQ_OP_WRITE, gfs2_end_log_write, true);
  298. ret = bio_add_page(bio, page, size, offset);
  299. WARN_ON(ret == 0);
  300. }
  301. }
  302. /**
  303. * gfs2_log_write_bh - write a buffer's content to the log
  304. * @sdp: The super block
  305. * @bh: The buffer pointing to the in-place location
  306. *
  307. * This writes the content of the buffer to the next available location
  308. * in the log. The buffer will be unlocked once the i/o to the log has
  309. * completed.
  310. */
  311. static void gfs2_log_write_bh(struct gfs2_sbd *sdp, struct buffer_head *bh)
  312. {
  313. u64 dblock;
  314. dblock = gfs2_log_bmap(sdp->sd_jdesc, sdp->sd_log_flush_head);
  315. gfs2_log_incr_head(sdp);
  316. gfs2_log_write(sdp, sdp->sd_jdesc, bh->b_page, bh->b_size,
  317. bh_offset(bh), dblock);
  318. }
  319. /**
  320. * gfs2_log_write_page - write one block stored in a page, into the log
  321. * @sdp: The superblock
  322. * @page: The struct page
  323. *
  324. * This writes the first block-sized part of the page into the log. Note
  325. * that the page must have been allocated from the gfs2_page_pool mempool
  326. * and that after this has been called, ownership has been transferred and
  327. * the page may be freed at any time.
  328. */
  329. static void gfs2_log_write_page(struct gfs2_sbd *sdp, struct page *page)
  330. {
  331. struct super_block *sb = sdp->sd_vfs;
  332. u64 dblock;
  333. dblock = gfs2_log_bmap(sdp->sd_jdesc, sdp->sd_log_flush_head);
  334. gfs2_log_incr_head(sdp);
  335. gfs2_log_write(sdp, sdp->sd_jdesc, page, sb->s_blocksize, 0, dblock);
  336. }
  337. /**
  338. * gfs2_end_log_read - end I/O callback for reads from the log
  339. * @bio: The bio
  340. *
  341. * Simply unlock the pages in the bio. The main thread will wait on them and
  342. * process them in order as necessary.
  343. */
  344. static void gfs2_end_log_read(struct bio *bio)
  345. {
  346. int error = blk_status_to_errno(bio->bi_status);
  347. struct folio_iter fi;
  348. bio_for_each_folio_all(fi, bio) {
  349. /* We're abusing wb_err to get the error to gfs2_find_jhead */
  350. filemap_set_wb_err(fi.folio->mapping, error);
  351. folio_end_read(fi.folio, !error);
  352. }
  353. bio_put(bio);
  354. }
  355. /**
  356. * gfs2_jhead_pg_srch - Look for the journal head in a given page.
  357. * @jd: The journal descriptor
  358. * @head: The journal head to start from
  359. * @page: The page to look in
  360. *
  361. * Returns: 1 if found, 0 otherwise.
  362. */
  363. static bool gfs2_jhead_pg_srch(struct gfs2_jdesc *jd,
  364. struct gfs2_log_header_host *head,
  365. struct page *page)
  366. {
  367. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  368. struct gfs2_log_header_host lh;
  369. void *kaddr;
  370. unsigned int offset;
  371. bool ret = false;
  372. kaddr = kmap_local_page(page);
  373. for (offset = 0; offset < PAGE_SIZE; offset += sdp->sd_sb.sb_bsize) {
  374. if (!__get_log_header(sdp, kaddr + offset, 0, &lh)) {
  375. if (lh.lh_sequence >= head->lh_sequence)
  376. *head = lh;
  377. else {
  378. ret = true;
  379. break;
  380. }
  381. }
  382. }
  383. kunmap_local(kaddr);
  384. return ret;
  385. }
  386. /**
  387. * gfs2_jhead_process_page - Search/cleanup a page
  388. * @jd: The journal descriptor
  389. * @index: Index of the page to look into
  390. * @head: The journal head to start from
  391. * @done: If set, perform only cleanup, else search and set if found.
  392. *
  393. * Find the folio with 'index' in the journal's mapping. Search the folio for
  394. * the journal head if requested (cleanup == false). Release refs on the
  395. * folio so the page cache can reclaim it. We grabbed a
  396. * reference on this folio twice, first when we did a grab_cache_page()
  397. * to obtain the folio to add it to the bio and second when we do a
  398. * filemap_get_folio() here to get the folio to wait on while I/O on it is being
  399. * completed.
  400. * This function is also used to free up a folio we might've grabbed but not
  401. * used. Maybe we added it to a bio, but not submitted it for I/O. Or we
  402. * submitted the I/O, but we already found the jhead so we only need to drop
  403. * our references to the folio.
  404. */
  405. static void gfs2_jhead_process_page(struct gfs2_jdesc *jd, unsigned long index,
  406. struct gfs2_log_header_host *head,
  407. bool *done)
  408. {
  409. struct folio *folio;
  410. folio = filemap_get_folio(jd->jd_inode->i_mapping, index);
  411. folio_wait_locked(folio);
  412. if (!folio_test_uptodate(folio))
  413. *done = true;
  414. if (!*done)
  415. *done = gfs2_jhead_pg_srch(jd, head, &folio->page);
  416. /* filemap_get_folio() and the earlier grab_cache_page() */
  417. folio_put_refs(folio, 2);
  418. }
  419. static struct bio *gfs2_chain_bio(struct bio *prev, unsigned int nr_iovecs)
  420. {
  421. struct bio *new;
  422. new = bio_alloc(prev->bi_bdev, nr_iovecs, prev->bi_opf, GFP_NOIO);
  423. bio_clone_blkg_association(new, prev);
  424. new->bi_iter.bi_sector = bio_end_sector(prev);
  425. bio_chain(new, prev);
  426. submit_bio(prev);
  427. return new;
  428. }
  429. /**
  430. * gfs2_find_jhead - find the head of a log
  431. * @jd: The journal descriptor
  432. * @head: The log descriptor for the head of the log is returned here
  433. * @keep_cache: If set inode pages will not be truncated
  434. *
  435. * Do a search of a journal by reading it in large chunks using bios and find
  436. * the valid log entry with the highest sequence number. (i.e. the log head)
  437. *
  438. * Returns: 0 on success, errno otherwise
  439. */
  440. int gfs2_find_jhead(struct gfs2_jdesc *jd, struct gfs2_log_header_host *head,
  441. bool keep_cache)
  442. {
  443. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  444. struct address_space *mapping = jd->jd_inode->i_mapping;
  445. unsigned int block = 0, blocks_submitted = 0, blocks_read = 0;
  446. unsigned int bsize = sdp->sd_sb.sb_bsize, off;
  447. unsigned int bsize_shift = sdp->sd_sb.sb_bsize_shift;
  448. unsigned int shift = PAGE_SHIFT - bsize_shift;
  449. unsigned int max_blocks = 2 * 1024 * 1024 >> bsize_shift;
  450. struct gfs2_journal_extent *je;
  451. int sz, ret = 0;
  452. struct bio *bio = NULL;
  453. struct page *page = NULL;
  454. bool done = false;
  455. errseq_t since;
  456. memset(head, 0, sizeof(*head));
  457. if (list_empty(&jd->extent_list))
  458. gfs2_map_journal_extents(sdp, jd);
  459. since = filemap_sample_wb_err(mapping);
  460. list_for_each_entry(je, &jd->extent_list, list) {
  461. u64 dblock = je->dblock;
  462. for (; block < je->lblock + je->blocks; block++, dblock++) {
  463. if (!page) {
  464. page = grab_cache_page(mapping, block >> shift);
  465. if (!page) {
  466. ret = -ENOMEM;
  467. done = true;
  468. goto out;
  469. }
  470. off = 0;
  471. }
  472. if (bio && (off || block < blocks_submitted + max_blocks)) {
  473. sector_t sector = dblock << sdp->sd_fsb2bb_shift;
  474. if (bio_end_sector(bio) == sector) {
  475. sz = bio_add_page(bio, page, bsize, off);
  476. if (sz == bsize)
  477. goto block_added;
  478. }
  479. if (off) {
  480. unsigned int blocks =
  481. (PAGE_SIZE - off) >> bsize_shift;
  482. bio = gfs2_chain_bio(bio, blocks);
  483. goto add_block_to_new_bio;
  484. }
  485. }
  486. if (bio) {
  487. blocks_submitted = block;
  488. submit_bio(bio);
  489. }
  490. bio = gfs2_log_alloc_bio(sdp, dblock, gfs2_end_log_read);
  491. bio->bi_opf = REQ_OP_READ;
  492. add_block_to_new_bio:
  493. sz = bio_add_page(bio, page, bsize, off);
  494. BUG_ON(sz != bsize);
  495. block_added:
  496. off += bsize;
  497. if (off == PAGE_SIZE)
  498. page = NULL;
  499. if (blocks_submitted <= blocks_read + max_blocks) {
  500. /* Keep at least one bio in flight */
  501. continue;
  502. }
  503. gfs2_jhead_process_page(jd, blocks_read >> shift, head, &done);
  504. blocks_read += PAGE_SIZE >> bsize_shift;
  505. if (done)
  506. goto out; /* found */
  507. }
  508. }
  509. out:
  510. if (bio)
  511. submit_bio(bio);
  512. while (blocks_read < block) {
  513. gfs2_jhead_process_page(jd, blocks_read >> shift, head, &done);
  514. blocks_read += PAGE_SIZE >> bsize_shift;
  515. }
  516. if (!ret)
  517. ret = filemap_check_wb_err(mapping, since);
  518. if (!keep_cache)
  519. truncate_inode_pages(mapping, 0);
  520. return ret;
  521. }
  522. static struct page *gfs2_get_log_desc(struct gfs2_sbd *sdp, u32 ld_type,
  523. u32 ld_length, u32 ld_data1)
  524. {
  525. struct page *page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  526. struct gfs2_log_descriptor *ld = page_address(page);
  527. clear_page(ld);
  528. ld->ld_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  529. ld->ld_header.mh_type = cpu_to_be32(GFS2_METATYPE_LD);
  530. ld->ld_header.mh_format = cpu_to_be32(GFS2_FORMAT_LD);
  531. ld->ld_type = cpu_to_be32(ld_type);
  532. ld->ld_length = cpu_to_be32(ld_length);
  533. ld->ld_data1 = cpu_to_be32(ld_data1);
  534. ld->ld_data2 = 0;
  535. return page;
  536. }
  537. static void gfs2_check_magic(struct buffer_head *bh)
  538. {
  539. void *kaddr;
  540. __be32 *ptr;
  541. clear_buffer_escaped(bh);
  542. kaddr = kmap_local_page(bh->b_page);
  543. ptr = kaddr + bh_offset(bh);
  544. if (*ptr == cpu_to_be32(GFS2_MAGIC))
  545. set_buffer_escaped(bh);
  546. kunmap_local(kaddr);
  547. }
  548. static int blocknr_cmp(void *priv, const struct list_head *a,
  549. const struct list_head *b)
  550. {
  551. struct gfs2_bufdata *bda, *bdb;
  552. bda = list_entry(a, struct gfs2_bufdata, bd_list);
  553. bdb = list_entry(b, struct gfs2_bufdata, bd_list);
  554. if (bda->bd_bh->b_blocknr < bdb->bd_bh->b_blocknr)
  555. return -1;
  556. if (bda->bd_bh->b_blocknr > bdb->bd_bh->b_blocknr)
  557. return 1;
  558. return 0;
  559. }
  560. static void gfs2_before_commit(struct gfs2_sbd *sdp, unsigned int limit,
  561. unsigned int total, struct list_head *blist,
  562. bool is_databuf)
  563. {
  564. struct gfs2_log_descriptor *ld;
  565. struct gfs2_bufdata *bd1 = NULL, *bd2;
  566. struct page *page;
  567. unsigned int num;
  568. unsigned n;
  569. __be64 *ptr;
  570. gfs2_log_lock(sdp);
  571. list_sort(NULL, blist, blocknr_cmp);
  572. bd1 = bd2 = list_prepare_entry(bd1, blist, bd_list);
  573. while(total) {
  574. num = total;
  575. if (total > limit)
  576. num = limit;
  577. gfs2_log_unlock(sdp);
  578. page = gfs2_get_log_desc(sdp,
  579. is_databuf ? GFS2_LOG_DESC_JDATA :
  580. GFS2_LOG_DESC_METADATA, num + 1, num);
  581. ld = page_address(page);
  582. gfs2_log_lock(sdp);
  583. ptr = (__be64 *)(ld + 1);
  584. n = 0;
  585. list_for_each_entry_continue(bd1, blist, bd_list) {
  586. *ptr++ = cpu_to_be64(bd1->bd_bh->b_blocknr);
  587. if (is_databuf) {
  588. gfs2_check_magic(bd1->bd_bh);
  589. *ptr++ = cpu_to_be64(buffer_escaped(bd1->bd_bh) ? 1 : 0);
  590. }
  591. if (++n >= num)
  592. break;
  593. }
  594. gfs2_log_unlock(sdp);
  595. gfs2_log_write_page(sdp, page);
  596. gfs2_log_lock(sdp);
  597. n = 0;
  598. list_for_each_entry_continue(bd2, blist, bd_list) {
  599. get_bh(bd2->bd_bh);
  600. gfs2_log_unlock(sdp);
  601. lock_buffer(bd2->bd_bh);
  602. if (buffer_escaped(bd2->bd_bh)) {
  603. void *p;
  604. page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  605. p = page_address(page);
  606. memcpy_from_page(p, page, bh_offset(bd2->bd_bh), bd2->bd_bh->b_size);
  607. *(__be32 *)p = 0;
  608. clear_buffer_escaped(bd2->bd_bh);
  609. unlock_buffer(bd2->bd_bh);
  610. brelse(bd2->bd_bh);
  611. gfs2_log_write_page(sdp, page);
  612. } else {
  613. gfs2_log_write_bh(sdp, bd2->bd_bh);
  614. }
  615. gfs2_log_lock(sdp);
  616. if (++n >= num)
  617. break;
  618. }
  619. BUG_ON(total < num);
  620. total -= num;
  621. }
  622. gfs2_log_unlock(sdp);
  623. }
  624. static void buf_lo_before_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  625. {
  626. unsigned int limit = buf_limit(sdp); /* 503 for 4k blocks */
  627. unsigned int nbuf;
  628. if (tr == NULL)
  629. return;
  630. nbuf = tr->tr_num_buf_new - tr->tr_num_buf_rm;
  631. gfs2_before_commit(sdp, limit, nbuf, &tr->tr_buf, 0);
  632. }
  633. static void buf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  634. {
  635. struct list_head *head;
  636. struct gfs2_bufdata *bd;
  637. if (tr == NULL)
  638. return;
  639. head = &tr->tr_buf;
  640. while (!list_empty(head)) {
  641. bd = list_first_entry(head, struct gfs2_bufdata, bd_list);
  642. list_del_init(&bd->bd_list);
  643. gfs2_unpin(sdp, bd->bd_bh, tr);
  644. }
  645. }
  646. static void buf_lo_before_scan(struct gfs2_jdesc *jd,
  647. struct gfs2_log_header_host *head, int pass)
  648. {
  649. if (pass != 0)
  650. return;
  651. jd->jd_found_blocks = 0;
  652. jd->jd_replayed_blocks = 0;
  653. }
  654. #define obsolete_rgrp_replay \
  655. "Replaying 0x%llx from jid=%d/0x%llx but we already have a bh!\n"
  656. #define obsolete_rgrp_replay2 \
  657. "busy:%d, pinned:%d rg_gen:0x%llx, j_gen:0x%llx\n"
  658. static void obsolete_rgrp(struct gfs2_jdesc *jd, struct buffer_head *bh_log,
  659. u64 blkno)
  660. {
  661. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  662. struct gfs2_rgrpd *rgd;
  663. struct gfs2_rgrp *jrgd = (struct gfs2_rgrp *)bh_log->b_data;
  664. rgd = gfs2_blk2rgrpd(sdp, blkno, false);
  665. if (rgd && rgd->rd_addr == blkno &&
  666. rgd->rd_bits && rgd->rd_bits->bi_bh) {
  667. fs_info(sdp, obsolete_rgrp_replay, (unsigned long long)blkno,
  668. jd->jd_jid, bh_log->b_blocknr);
  669. fs_info(sdp, obsolete_rgrp_replay2,
  670. buffer_busy(rgd->rd_bits->bi_bh) ? 1 : 0,
  671. buffer_pinned(rgd->rd_bits->bi_bh),
  672. rgd->rd_igeneration,
  673. be64_to_cpu(jrgd->rg_igeneration));
  674. gfs2_dump_glock(NULL, rgd->rd_gl, true);
  675. }
  676. }
  677. static int buf_lo_scan_elements(struct gfs2_jdesc *jd, u32 start,
  678. struct gfs2_log_descriptor *ld, __be64 *ptr,
  679. int pass)
  680. {
  681. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  682. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  683. struct gfs2_glock *gl = ip->i_gl;
  684. unsigned int blks = be32_to_cpu(ld->ld_data1);
  685. struct buffer_head *bh_log, *bh_ip;
  686. u64 blkno;
  687. int error = 0;
  688. if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_METADATA)
  689. return 0;
  690. gfs2_replay_incr_blk(jd, &start);
  691. for (; blks; gfs2_replay_incr_blk(jd, &start), blks--) {
  692. blkno = be64_to_cpu(*ptr++);
  693. jd->jd_found_blocks++;
  694. if (gfs2_revoke_check(jd, blkno, start))
  695. continue;
  696. error = gfs2_replay_read_block(jd, start, &bh_log);
  697. if (error)
  698. return error;
  699. bh_ip = gfs2_meta_new(gl, blkno);
  700. memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
  701. if (gfs2_meta_check(sdp, bh_ip))
  702. error = -EIO;
  703. else {
  704. struct gfs2_meta_header *mh =
  705. (struct gfs2_meta_header *)bh_ip->b_data;
  706. if (mh->mh_type == cpu_to_be32(GFS2_METATYPE_RG))
  707. obsolete_rgrp(jd, bh_log, blkno);
  708. mark_buffer_dirty(bh_ip);
  709. }
  710. brelse(bh_log);
  711. brelse(bh_ip);
  712. if (error)
  713. break;
  714. jd->jd_replayed_blocks++;
  715. }
  716. return error;
  717. }
  718. static void buf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  719. {
  720. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  721. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  722. if (error) {
  723. gfs2_inode_metasync(ip->i_gl);
  724. return;
  725. }
  726. if (pass != 1)
  727. return;
  728. gfs2_inode_metasync(ip->i_gl);
  729. fs_info(sdp, "jid=%u: Replayed %u of %u blocks\n",
  730. jd->jd_jid, jd->jd_replayed_blocks, jd->jd_found_blocks);
  731. }
  732. static void revoke_lo_before_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  733. {
  734. struct gfs2_meta_header *mh;
  735. unsigned int offset;
  736. struct list_head *head = &sdp->sd_log_revokes;
  737. struct gfs2_bufdata *bd;
  738. struct page *page;
  739. unsigned int length;
  740. gfs2_flush_revokes(sdp);
  741. if (!sdp->sd_log_num_revoke)
  742. return;
  743. length = gfs2_struct2blk(sdp, sdp->sd_log_num_revoke);
  744. page = gfs2_get_log_desc(sdp, GFS2_LOG_DESC_REVOKE, length, sdp->sd_log_num_revoke);
  745. offset = sizeof(struct gfs2_log_descriptor);
  746. list_for_each_entry(bd, head, bd_list) {
  747. sdp->sd_log_num_revoke--;
  748. if (offset + sizeof(u64) > sdp->sd_sb.sb_bsize) {
  749. gfs2_log_write_page(sdp, page);
  750. page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  751. mh = page_address(page);
  752. clear_page(mh);
  753. mh->mh_magic = cpu_to_be32(GFS2_MAGIC);
  754. mh->mh_type = cpu_to_be32(GFS2_METATYPE_LB);
  755. mh->mh_format = cpu_to_be32(GFS2_FORMAT_LB);
  756. offset = sizeof(struct gfs2_meta_header);
  757. }
  758. *(__be64 *)(page_address(page) + offset) = cpu_to_be64(bd->bd_blkno);
  759. offset += sizeof(u64);
  760. }
  761. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  762. gfs2_log_write_page(sdp, page);
  763. }
  764. void gfs2_drain_revokes(struct gfs2_sbd *sdp)
  765. {
  766. struct list_head *head = &sdp->sd_log_revokes;
  767. struct gfs2_bufdata *bd;
  768. struct gfs2_glock *gl;
  769. while (!list_empty(head)) {
  770. bd = list_first_entry(head, struct gfs2_bufdata, bd_list);
  771. list_del_init(&bd->bd_list);
  772. gl = bd->bd_gl;
  773. gfs2_glock_remove_revoke(gl);
  774. kmem_cache_free(gfs2_bufdata_cachep, bd);
  775. }
  776. }
  777. static void revoke_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  778. {
  779. gfs2_drain_revokes(sdp);
  780. }
  781. static void revoke_lo_before_scan(struct gfs2_jdesc *jd,
  782. struct gfs2_log_header_host *head, int pass)
  783. {
  784. if (pass != 0)
  785. return;
  786. jd->jd_found_revokes = 0;
  787. jd->jd_replay_tail = head->lh_tail;
  788. }
  789. static int revoke_lo_scan_elements(struct gfs2_jdesc *jd, u32 start,
  790. struct gfs2_log_descriptor *ld, __be64 *ptr,
  791. int pass)
  792. {
  793. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  794. unsigned int blks = be32_to_cpu(ld->ld_length);
  795. unsigned int revokes = be32_to_cpu(ld->ld_data1);
  796. struct buffer_head *bh;
  797. unsigned int offset;
  798. u64 blkno;
  799. int first = 1;
  800. int error;
  801. if (pass != 0 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_REVOKE)
  802. return 0;
  803. offset = sizeof(struct gfs2_log_descriptor);
  804. for (; blks; gfs2_replay_incr_blk(jd, &start), blks--) {
  805. error = gfs2_replay_read_block(jd, start, &bh);
  806. if (error)
  807. return error;
  808. if (!first)
  809. gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LB);
  810. while (offset + sizeof(u64) <= sdp->sd_sb.sb_bsize) {
  811. blkno = be64_to_cpu(*(__be64 *)(bh->b_data + offset));
  812. error = gfs2_revoke_add(jd, blkno, start);
  813. if (error < 0) {
  814. brelse(bh);
  815. return error;
  816. }
  817. else if (error)
  818. jd->jd_found_revokes++;
  819. if (!--revokes)
  820. break;
  821. offset += sizeof(u64);
  822. }
  823. brelse(bh);
  824. offset = sizeof(struct gfs2_meta_header);
  825. first = 0;
  826. }
  827. return 0;
  828. }
  829. static void revoke_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  830. {
  831. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  832. if (error) {
  833. gfs2_revoke_clean(jd);
  834. return;
  835. }
  836. if (pass != 1)
  837. return;
  838. fs_info(sdp, "jid=%u: Found %u revoke tags\n",
  839. jd->jd_jid, jd->jd_found_revokes);
  840. gfs2_revoke_clean(jd);
  841. }
  842. /**
  843. * databuf_lo_before_commit - Scan the data buffers, writing as we go
  844. * @sdp: The filesystem
  845. * @tr: The system transaction being flushed
  846. */
  847. static void databuf_lo_before_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  848. {
  849. unsigned int limit = databuf_limit(sdp);
  850. unsigned int nbuf;
  851. if (tr == NULL)
  852. return;
  853. nbuf = tr->tr_num_databuf_new - tr->tr_num_databuf_rm;
  854. gfs2_before_commit(sdp, limit, nbuf, &tr->tr_databuf, 1);
  855. }
  856. static int databuf_lo_scan_elements(struct gfs2_jdesc *jd, u32 start,
  857. struct gfs2_log_descriptor *ld,
  858. __be64 *ptr, int pass)
  859. {
  860. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  861. struct gfs2_glock *gl = ip->i_gl;
  862. unsigned int blks = be32_to_cpu(ld->ld_data1);
  863. struct buffer_head *bh_log, *bh_ip;
  864. u64 blkno;
  865. u64 esc;
  866. int error = 0;
  867. if (pass != 1 || be32_to_cpu(ld->ld_type) != GFS2_LOG_DESC_JDATA)
  868. return 0;
  869. gfs2_replay_incr_blk(jd, &start);
  870. for (; blks; gfs2_replay_incr_blk(jd, &start), blks--) {
  871. blkno = be64_to_cpu(*ptr++);
  872. esc = be64_to_cpu(*ptr++);
  873. jd->jd_found_blocks++;
  874. if (gfs2_revoke_check(jd, blkno, start))
  875. continue;
  876. error = gfs2_replay_read_block(jd, start, &bh_log);
  877. if (error)
  878. return error;
  879. bh_ip = gfs2_meta_new(gl, blkno);
  880. memcpy(bh_ip->b_data, bh_log->b_data, bh_log->b_size);
  881. /* Unescape */
  882. if (esc) {
  883. __be32 *eptr = (__be32 *)bh_ip->b_data;
  884. *eptr = cpu_to_be32(GFS2_MAGIC);
  885. }
  886. mark_buffer_dirty(bh_ip);
  887. brelse(bh_log);
  888. brelse(bh_ip);
  889. jd->jd_replayed_blocks++;
  890. }
  891. return error;
  892. }
  893. /* FIXME: sort out accounting for log blocks etc. */
  894. static void databuf_lo_after_scan(struct gfs2_jdesc *jd, int error, int pass)
  895. {
  896. struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
  897. struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
  898. if (error) {
  899. gfs2_inode_metasync(ip->i_gl);
  900. return;
  901. }
  902. if (pass != 1)
  903. return;
  904. /* data sync? */
  905. gfs2_inode_metasync(ip->i_gl);
  906. fs_info(sdp, "jid=%u: Replayed %u of %u data blocks\n",
  907. jd->jd_jid, jd->jd_replayed_blocks, jd->jd_found_blocks);
  908. }
  909. static void databuf_lo_after_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  910. {
  911. struct list_head *head;
  912. struct gfs2_bufdata *bd;
  913. if (tr == NULL)
  914. return;
  915. head = &tr->tr_databuf;
  916. while (!list_empty(head)) {
  917. bd = list_first_entry(head, struct gfs2_bufdata, bd_list);
  918. list_del_init(&bd->bd_list);
  919. gfs2_unpin(sdp, bd->bd_bh, tr);
  920. }
  921. }
  922. static const struct gfs2_log_operations gfs2_buf_lops = {
  923. .lo_before_commit = buf_lo_before_commit,
  924. .lo_after_commit = buf_lo_after_commit,
  925. .lo_before_scan = buf_lo_before_scan,
  926. .lo_scan_elements = buf_lo_scan_elements,
  927. .lo_after_scan = buf_lo_after_scan,
  928. .lo_name = "buf",
  929. };
  930. static const struct gfs2_log_operations gfs2_revoke_lops = {
  931. .lo_before_commit = revoke_lo_before_commit,
  932. .lo_after_commit = revoke_lo_after_commit,
  933. .lo_before_scan = revoke_lo_before_scan,
  934. .lo_scan_elements = revoke_lo_scan_elements,
  935. .lo_after_scan = revoke_lo_after_scan,
  936. .lo_name = "revoke",
  937. };
  938. static const struct gfs2_log_operations gfs2_databuf_lops = {
  939. .lo_before_commit = databuf_lo_before_commit,
  940. .lo_after_commit = databuf_lo_after_commit,
  941. .lo_scan_elements = databuf_lo_scan_elements,
  942. .lo_after_scan = databuf_lo_after_scan,
  943. .lo_name = "databuf",
  944. };
  945. const struct gfs2_log_operations *gfs2_log_ops[] = {
  946. &gfs2_databuf_lops,
  947. &gfs2_buf_lops,
  948. &gfs2_revoke_lops,
  949. NULL,
  950. };