log.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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/gfs2_ondisk.h>
  15. #include <linux/crc32.h>
  16. #include <linux/crc32c.h>
  17. #include <linux/delay.h>
  18. #include <linux/kthread.h>
  19. #include <linux/freezer.h>
  20. #include <linux/bio.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/writeback.h>
  23. #include <linux/list_sort.h>
  24. #include "gfs2.h"
  25. #include "incore.h"
  26. #include "bmap.h"
  27. #include "glock.h"
  28. #include "log.h"
  29. #include "lops.h"
  30. #include "meta_io.h"
  31. #include "util.h"
  32. #include "dir.h"
  33. #include "trace_gfs2.h"
  34. /**
  35. * gfs2_struct2blk - compute stuff
  36. * @sdp: the filesystem
  37. * @nstruct: the number of structures
  38. * @ssize: the size of the structures
  39. *
  40. * Compute the number of log descriptor blocks needed to hold a certain number
  41. * of structures of a certain size.
  42. *
  43. * Returns: the number of blocks needed (minimum is always 1)
  44. */
  45. unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
  46. unsigned int ssize)
  47. {
  48. unsigned int blks;
  49. unsigned int first, second;
  50. blks = 1;
  51. first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
  52. if (nstruct > first) {
  53. second = (sdp->sd_sb.sb_bsize -
  54. sizeof(struct gfs2_meta_header)) / ssize;
  55. blks += DIV_ROUND_UP(nstruct - first, second);
  56. }
  57. return blks;
  58. }
  59. /**
  60. * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
  61. * @mapping: The associated mapping (maybe NULL)
  62. * @bd: The gfs2_bufdata to remove
  63. *
  64. * The ail lock _must_ be held when calling this function
  65. *
  66. */
  67. static void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
  68. {
  69. bd->bd_tr = NULL;
  70. list_del_init(&bd->bd_ail_st_list);
  71. list_del_init(&bd->bd_ail_gl_list);
  72. atomic_dec(&bd->bd_gl->gl_ail_count);
  73. brelse(bd->bd_bh);
  74. }
  75. /**
  76. * gfs2_ail1_start_one - Start I/O on a part of the AIL
  77. * @sdp: the filesystem
  78. * @wbc: The writeback control structure
  79. * @ai: The ail structure
  80. *
  81. */
  82. static int gfs2_ail1_start_one(struct gfs2_sbd *sdp,
  83. struct writeback_control *wbc,
  84. struct gfs2_trans *tr,
  85. bool *withdraw)
  86. __releases(&sdp->sd_ail_lock)
  87. __acquires(&sdp->sd_ail_lock)
  88. {
  89. struct gfs2_glock *gl = NULL;
  90. struct address_space *mapping;
  91. struct gfs2_bufdata *bd, *s;
  92. struct buffer_head *bh;
  93. list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list, bd_ail_st_list) {
  94. bh = bd->bd_bh;
  95. gfs2_assert(sdp, bd->bd_tr == tr);
  96. if (!buffer_busy(bh)) {
  97. if (!buffer_uptodate(bh) &&
  98. !test_and_set_bit(SDF_AIL1_IO_ERROR,
  99. &sdp->sd_flags)) {
  100. gfs2_io_error_bh(sdp, bh);
  101. *withdraw = true;
  102. }
  103. list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
  104. continue;
  105. }
  106. if (!buffer_dirty(bh))
  107. continue;
  108. if (gl == bd->bd_gl)
  109. continue;
  110. gl = bd->bd_gl;
  111. list_move(&bd->bd_ail_st_list, &tr->tr_ail1_list);
  112. mapping = bh->b_page->mapping;
  113. if (!mapping)
  114. continue;
  115. spin_unlock(&sdp->sd_ail_lock);
  116. generic_writepages(mapping, wbc);
  117. spin_lock(&sdp->sd_ail_lock);
  118. if (wbc->nr_to_write <= 0)
  119. break;
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. /**
  125. * gfs2_ail1_flush - start writeback of some ail1 entries
  126. * @sdp: The super block
  127. * @wbc: The writeback control structure
  128. *
  129. * Writes back some ail1 entries, according to the limits in the
  130. * writeback control structure
  131. */
  132. void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
  133. {
  134. struct list_head *head = &sdp->sd_ail1_list;
  135. struct gfs2_trans *tr;
  136. struct blk_plug plug;
  137. bool withdraw = false;
  138. trace_gfs2_ail_flush(sdp, wbc, 1);
  139. blk_start_plug(&plug);
  140. spin_lock(&sdp->sd_ail_lock);
  141. restart:
  142. list_for_each_entry_reverse(tr, head, tr_list) {
  143. if (wbc->nr_to_write <= 0)
  144. break;
  145. if (gfs2_ail1_start_one(sdp, wbc, tr, &withdraw))
  146. goto restart;
  147. }
  148. spin_unlock(&sdp->sd_ail_lock);
  149. blk_finish_plug(&plug);
  150. if (withdraw)
  151. gfs2_lm_withdraw(sdp, NULL);
  152. trace_gfs2_ail_flush(sdp, wbc, 0);
  153. }
  154. /**
  155. * gfs2_ail1_start - start writeback of all ail1 entries
  156. * @sdp: The superblock
  157. */
  158. static void gfs2_ail1_start(struct gfs2_sbd *sdp)
  159. {
  160. struct writeback_control wbc = {
  161. .sync_mode = WB_SYNC_NONE,
  162. .nr_to_write = LONG_MAX,
  163. .range_start = 0,
  164. .range_end = LLONG_MAX,
  165. };
  166. return gfs2_ail1_flush(sdp, &wbc);
  167. }
  168. /**
  169. * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
  170. * @sdp: the filesystem
  171. * @ai: the AIL entry
  172. *
  173. */
  174. static void gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
  175. bool *withdraw)
  176. {
  177. struct gfs2_bufdata *bd, *s;
  178. struct buffer_head *bh;
  179. list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list,
  180. bd_ail_st_list) {
  181. bh = bd->bd_bh;
  182. gfs2_assert(sdp, bd->bd_tr == tr);
  183. if (buffer_busy(bh))
  184. continue;
  185. if (!buffer_uptodate(bh) &&
  186. !test_and_set_bit(SDF_AIL1_IO_ERROR, &sdp->sd_flags)) {
  187. gfs2_io_error_bh(sdp, bh);
  188. *withdraw = true;
  189. }
  190. list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
  191. }
  192. }
  193. /**
  194. * gfs2_ail1_empty - Try to empty the ail1 lists
  195. * @sdp: The superblock
  196. *
  197. * Tries to empty the ail1 lists, starting with the oldest first
  198. */
  199. static int gfs2_ail1_empty(struct gfs2_sbd *sdp)
  200. {
  201. struct gfs2_trans *tr, *s;
  202. int oldest_tr = 1;
  203. int ret;
  204. bool withdraw = false;
  205. spin_lock(&sdp->sd_ail_lock);
  206. list_for_each_entry_safe_reverse(tr, s, &sdp->sd_ail1_list, tr_list) {
  207. gfs2_ail1_empty_one(sdp, tr, &withdraw);
  208. if (list_empty(&tr->tr_ail1_list) && oldest_tr)
  209. list_move(&tr->tr_list, &sdp->sd_ail2_list);
  210. else
  211. oldest_tr = 0;
  212. }
  213. ret = list_empty(&sdp->sd_ail1_list);
  214. spin_unlock(&sdp->sd_ail_lock);
  215. if (withdraw)
  216. gfs2_lm_withdraw(sdp, "fatal: I/O error(s)\n");
  217. return ret;
  218. }
  219. static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
  220. {
  221. struct gfs2_trans *tr;
  222. struct gfs2_bufdata *bd;
  223. struct buffer_head *bh;
  224. spin_lock(&sdp->sd_ail_lock);
  225. list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
  226. list_for_each_entry(bd, &tr->tr_ail1_list, bd_ail_st_list) {
  227. bh = bd->bd_bh;
  228. if (!buffer_locked(bh))
  229. continue;
  230. get_bh(bh);
  231. spin_unlock(&sdp->sd_ail_lock);
  232. wait_on_buffer(bh);
  233. brelse(bh);
  234. return;
  235. }
  236. }
  237. spin_unlock(&sdp->sd_ail_lock);
  238. }
  239. /**
  240. * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
  241. * @sdp: the filesystem
  242. * @ai: the AIL entry
  243. *
  244. */
  245. static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  246. {
  247. struct list_head *head = &tr->tr_ail2_list;
  248. struct gfs2_bufdata *bd;
  249. while (!list_empty(head)) {
  250. bd = list_entry(head->prev, struct gfs2_bufdata,
  251. bd_ail_st_list);
  252. gfs2_assert(sdp, bd->bd_tr == tr);
  253. gfs2_remove_from_ail(bd);
  254. }
  255. }
  256. static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
  257. {
  258. struct gfs2_trans *tr, *safe;
  259. unsigned int old_tail = sdp->sd_log_tail;
  260. int wrap = (new_tail < old_tail);
  261. int a, b, rm;
  262. spin_lock(&sdp->sd_ail_lock);
  263. list_for_each_entry_safe(tr, safe, &sdp->sd_ail2_list, tr_list) {
  264. a = (old_tail <= tr->tr_first);
  265. b = (tr->tr_first < new_tail);
  266. rm = (wrap) ? (a || b) : (a && b);
  267. if (!rm)
  268. continue;
  269. gfs2_ail2_empty_one(sdp, tr);
  270. list_del(&tr->tr_list);
  271. gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
  272. gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
  273. kfree(tr);
  274. }
  275. spin_unlock(&sdp->sd_ail_lock);
  276. }
  277. /**
  278. * gfs2_log_release - Release a given number of log blocks
  279. * @sdp: The GFS2 superblock
  280. * @blks: The number of blocks
  281. *
  282. */
  283. void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
  284. {
  285. atomic_add(blks, &sdp->sd_log_blks_free);
  286. trace_gfs2_log_blocks(sdp, blks);
  287. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  288. sdp->sd_jdesc->jd_blocks);
  289. up_read(&sdp->sd_log_flush_lock);
  290. }
  291. /**
  292. * gfs2_log_reserve - Make a log reservation
  293. * @sdp: The GFS2 superblock
  294. * @blks: The number of blocks to reserve
  295. *
  296. * Note that we never give out the last few blocks of the journal. Thats
  297. * due to the fact that there is a small number of header blocks
  298. * associated with each log flush. The exact number can't be known until
  299. * flush time, so we ensure that we have just enough free blocks at all
  300. * times to avoid running out during a log flush.
  301. *
  302. * We no longer flush the log here, instead we wake up logd to do that
  303. * for us. To avoid the thundering herd and to ensure that we deal fairly
  304. * with queued waiters, we use an exclusive wait. This means that when we
  305. * get woken with enough journal space to get our reservation, we need to
  306. * wake the next waiter on the list.
  307. *
  308. * Returns: errno
  309. */
  310. int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
  311. {
  312. int ret = 0;
  313. unsigned reserved_blks = 7 * (4096 / sdp->sd_vfs->s_blocksize);
  314. unsigned wanted = blks + reserved_blks;
  315. DEFINE_WAIT(wait);
  316. int did_wait = 0;
  317. unsigned int free_blocks;
  318. if (gfs2_assert_warn(sdp, blks) ||
  319. gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
  320. return -EINVAL;
  321. atomic_add(blks, &sdp->sd_log_blks_needed);
  322. retry:
  323. free_blocks = atomic_read(&sdp->sd_log_blks_free);
  324. if (unlikely(free_blocks <= wanted)) {
  325. do {
  326. prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
  327. TASK_UNINTERRUPTIBLE);
  328. wake_up(&sdp->sd_logd_waitq);
  329. did_wait = 1;
  330. if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
  331. io_schedule();
  332. free_blocks = atomic_read(&sdp->sd_log_blks_free);
  333. } while(free_blocks <= wanted);
  334. finish_wait(&sdp->sd_log_waitq, &wait);
  335. }
  336. atomic_inc(&sdp->sd_reserving_log);
  337. if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
  338. free_blocks - blks) != free_blocks) {
  339. if (atomic_dec_and_test(&sdp->sd_reserving_log))
  340. wake_up(&sdp->sd_reserving_log_wait);
  341. goto retry;
  342. }
  343. atomic_sub(blks, &sdp->sd_log_blks_needed);
  344. trace_gfs2_log_blocks(sdp, -blks);
  345. /*
  346. * If we waited, then so might others, wake them up _after_ we get
  347. * our share of the log.
  348. */
  349. if (unlikely(did_wait))
  350. wake_up(&sdp->sd_log_waitq);
  351. down_read(&sdp->sd_log_flush_lock);
  352. if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) {
  353. gfs2_log_release(sdp, blks);
  354. ret = -EROFS;
  355. }
  356. if (atomic_dec_and_test(&sdp->sd_reserving_log))
  357. wake_up(&sdp->sd_reserving_log_wait);
  358. return ret;
  359. }
  360. /**
  361. * log_distance - Compute distance between two journal blocks
  362. * @sdp: The GFS2 superblock
  363. * @newer: The most recent journal block of the pair
  364. * @older: The older journal block of the pair
  365. *
  366. * Compute the distance (in the journal direction) between two
  367. * blocks in the journal
  368. *
  369. * Returns: the distance in blocks
  370. */
  371. static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
  372. unsigned int older)
  373. {
  374. int dist;
  375. dist = newer - older;
  376. if (dist < 0)
  377. dist += sdp->sd_jdesc->jd_blocks;
  378. return dist;
  379. }
  380. /**
  381. * calc_reserved - Calculate the number of blocks to reserve when
  382. * refunding a transaction's unused buffers.
  383. * @sdp: The GFS2 superblock
  384. *
  385. * This is complex. We need to reserve room for all our currently used
  386. * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
  387. * all our journaled data buffers for journaled files (e.g. files in the
  388. * meta_fs like rindex, or files for which chattr +j was done.)
  389. * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
  390. * will count it as free space (sd_log_blks_free) and corruption will follow.
  391. *
  392. * We can have metadata bufs and jdata bufs in the same journal. So each
  393. * type gets its own log header, for which we need to reserve a block.
  394. * In fact, each type has the potential for needing more than one header
  395. * in cases where we have more buffers than will fit on a journal page.
  396. * Metadata journal entries take up half the space of journaled buffer entries.
  397. * Thus, metadata entries have buf_limit (502) and journaled buffers have
  398. * databuf_limit (251) before they cause a wrap around.
  399. *
  400. * Also, we need to reserve blocks for revoke journal entries and one for an
  401. * overall header for the lot.
  402. *
  403. * Returns: the number of blocks reserved
  404. */
  405. static unsigned int calc_reserved(struct gfs2_sbd *sdp)
  406. {
  407. unsigned int reserved = 0;
  408. unsigned int mbuf;
  409. unsigned int dbuf;
  410. struct gfs2_trans *tr = sdp->sd_log_tr;
  411. if (tr) {
  412. mbuf = tr->tr_num_buf_new - tr->tr_num_buf_rm;
  413. dbuf = tr->tr_num_databuf_new - tr->tr_num_databuf_rm;
  414. reserved = mbuf + dbuf;
  415. /* Account for header blocks */
  416. reserved += DIV_ROUND_UP(mbuf, buf_limit(sdp));
  417. reserved += DIV_ROUND_UP(dbuf, databuf_limit(sdp));
  418. }
  419. if (sdp->sd_log_commited_revoke > 0)
  420. reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
  421. sizeof(u64));
  422. /* One for the overall header */
  423. if (reserved)
  424. reserved++;
  425. return reserved;
  426. }
  427. static unsigned int current_tail(struct gfs2_sbd *sdp)
  428. {
  429. struct gfs2_trans *tr;
  430. unsigned int tail;
  431. spin_lock(&sdp->sd_ail_lock);
  432. if (list_empty(&sdp->sd_ail1_list)) {
  433. tail = sdp->sd_log_head;
  434. } else {
  435. tr = list_entry(sdp->sd_ail1_list.prev, struct gfs2_trans,
  436. tr_list);
  437. tail = tr->tr_first;
  438. }
  439. spin_unlock(&sdp->sd_ail_lock);
  440. return tail;
  441. }
  442. static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
  443. {
  444. unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
  445. ail2_empty(sdp, new_tail);
  446. atomic_add(dist, &sdp->sd_log_blks_free);
  447. trace_gfs2_log_blocks(sdp, dist);
  448. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  449. sdp->sd_jdesc->jd_blocks);
  450. sdp->sd_log_tail = new_tail;
  451. }
  452. static void log_flush_wait(struct gfs2_sbd *sdp)
  453. {
  454. DEFINE_WAIT(wait);
  455. if (atomic_read(&sdp->sd_log_in_flight)) {
  456. do {
  457. prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
  458. TASK_UNINTERRUPTIBLE);
  459. if (atomic_read(&sdp->sd_log_in_flight))
  460. io_schedule();
  461. } while(atomic_read(&sdp->sd_log_in_flight));
  462. finish_wait(&sdp->sd_log_flush_wait, &wait);
  463. }
  464. }
  465. static int ip_cmp(void *priv, struct list_head *a, struct list_head *b)
  466. {
  467. struct gfs2_inode *ipa, *ipb;
  468. ipa = list_entry(a, struct gfs2_inode, i_ordered);
  469. ipb = list_entry(b, struct gfs2_inode, i_ordered);
  470. if (ipa->i_no_addr < ipb->i_no_addr)
  471. return -1;
  472. if (ipa->i_no_addr > ipb->i_no_addr)
  473. return 1;
  474. return 0;
  475. }
  476. static void gfs2_ordered_write(struct gfs2_sbd *sdp)
  477. {
  478. struct gfs2_inode *ip;
  479. LIST_HEAD(written);
  480. spin_lock(&sdp->sd_ordered_lock);
  481. list_sort(NULL, &sdp->sd_log_le_ordered, &ip_cmp);
  482. while (!list_empty(&sdp->sd_log_le_ordered)) {
  483. ip = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_inode, i_ordered);
  484. if (ip->i_inode.i_mapping->nrpages == 0) {
  485. test_and_clear_bit(GIF_ORDERED, &ip->i_flags);
  486. list_del(&ip->i_ordered);
  487. continue;
  488. }
  489. list_move(&ip->i_ordered, &written);
  490. spin_unlock(&sdp->sd_ordered_lock);
  491. filemap_fdatawrite(ip->i_inode.i_mapping);
  492. spin_lock(&sdp->sd_ordered_lock);
  493. }
  494. list_splice(&written, &sdp->sd_log_le_ordered);
  495. spin_unlock(&sdp->sd_ordered_lock);
  496. }
  497. static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
  498. {
  499. struct gfs2_inode *ip;
  500. spin_lock(&sdp->sd_ordered_lock);
  501. while (!list_empty(&sdp->sd_log_le_ordered)) {
  502. ip = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_inode, i_ordered);
  503. list_del(&ip->i_ordered);
  504. WARN_ON(!test_and_clear_bit(GIF_ORDERED, &ip->i_flags));
  505. if (ip->i_inode.i_mapping->nrpages == 0)
  506. continue;
  507. spin_unlock(&sdp->sd_ordered_lock);
  508. filemap_fdatawait(ip->i_inode.i_mapping);
  509. spin_lock(&sdp->sd_ordered_lock);
  510. }
  511. spin_unlock(&sdp->sd_ordered_lock);
  512. }
  513. void gfs2_ordered_del_inode(struct gfs2_inode *ip)
  514. {
  515. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  516. spin_lock(&sdp->sd_ordered_lock);
  517. if (test_and_clear_bit(GIF_ORDERED, &ip->i_flags))
  518. list_del(&ip->i_ordered);
  519. spin_unlock(&sdp->sd_ordered_lock);
  520. }
  521. void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
  522. {
  523. struct buffer_head *bh = bd->bd_bh;
  524. struct gfs2_glock *gl = bd->bd_gl;
  525. bh->b_private = NULL;
  526. bd->bd_blkno = bh->b_blocknr;
  527. gfs2_remove_from_ail(bd); /* drops ref on bh */
  528. bd->bd_bh = NULL;
  529. bd->bd_ops = &gfs2_revoke_lops;
  530. sdp->sd_log_num_revoke++;
  531. if (atomic_inc_return(&gl->gl_revokes) == 1)
  532. gfs2_glock_hold(gl);
  533. set_bit(GLF_LFLUSH, &gl->gl_flags);
  534. list_add(&bd->bd_list, &sdp->sd_log_le_revoke);
  535. }
  536. void gfs2_glock_remove_revoke(struct gfs2_glock *gl)
  537. {
  538. if (atomic_dec_return(&gl->gl_revokes) == 0) {
  539. clear_bit(GLF_LFLUSH, &gl->gl_flags);
  540. gfs2_glock_queue_put(gl);
  541. }
  542. }
  543. void gfs2_write_revokes(struct gfs2_sbd *sdp)
  544. {
  545. struct gfs2_trans *tr;
  546. struct gfs2_bufdata *bd, *tmp;
  547. int have_revokes = 0;
  548. int max_revokes = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / sizeof(u64);
  549. gfs2_ail1_empty(sdp);
  550. spin_lock(&sdp->sd_ail_lock);
  551. list_for_each_entry(tr, &sdp->sd_ail1_list, tr_list) {
  552. list_for_each_entry(bd, &tr->tr_ail2_list, bd_ail_st_list) {
  553. if (list_empty(&bd->bd_list)) {
  554. have_revokes = 1;
  555. goto done;
  556. }
  557. }
  558. }
  559. done:
  560. spin_unlock(&sdp->sd_ail_lock);
  561. if (have_revokes == 0)
  562. return;
  563. while (sdp->sd_log_num_revoke > max_revokes)
  564. max_revokes += (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / sizeof(u64);
  565. max_revokes -= sdp->sd_log_num_revoke;
  566. if (!sdp->sd_log_num_revoke) {
  567. atomic_dec(&sdp->sd_log_blks_free);
  568. /* If no blocks have been reserved, we need to also
  569. * reserve a block for the header */
  570. if (!sdp->sd_log_blks_reserved)
  571. atomic_dec(&sdp->sd_log_blks_free);
  572. }
  573. gfs2_log_lock(sdp);
  574. spin_lock(&sdp->sd_ail_lock);
  575. list_for_each_entry(tr, &sdp->sd_ail1_list, tr_list) {
  576. list_for_each_entry_safe(bd, tmp, &tr->tr_ail2_list, bd_ail_st_list) {
  577. if (max_revokes == 0)
  578. goto out_of_blocks;
  579. if (!list_empty(&bd->bd_list))
  580. continue;
  581. gfs2_add_revoke(sdp, bd);
  582. max_revokes--;
  583. }
  584. }
  585. out_of_blocks:
  586. spin_unlock(&sdp->sd_ail_lock);
  587. gfs2_log_unlock(sdp);
  588. if (!sdp->sd_log_num_revoke) {
  589. atomic_inc(&sdp->sd_log_blks_free);
  590. if (!sdp->sd_log_blks_reserved)
  591. atomic_inc(&sdp->sd_log_blks_free);
  592. }
  593. }
  594. /**
  595. * write_log_header - Write a journal log header buffer at sd_log_flush_head
  596. * @sdp: The GFS2 superblock
  597. * @jd: journal descriptor of the journal to which we are writing
  598. * @seq: sequence number
  599. * @tail: tail of the log
  600. * @flags: log header flags GFS2_LOG_HEAD_*
  601. * @op_flags: flags to pass to the bio
  602. *
  603. * Returns: the initialized log buffer descriptor
  604. */
  605. void gfs2_write_log_header(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd,
  606. u64 seq, u32 tail, u32 flags, int op_flags)
  607. {
  608. struct gfs2_log_header *lh;
  609. u32 hash, crc;
  610. struct page *page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
  611. struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
  612. struct timespec64 tv;
  613. struct super_block *sb = sdp->sd_vfs;
  614. u64 addr;
  615. lh = page_address(page);
  616. clear_page(lh);
  617. lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
  618. lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
  619. lh->lh_header.__pad0 = cpu_to_be64(0);
  620. lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
  621. lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
  622. lh->lh_sequence = cpu_to_be64(seq);
  623. lh->lh_flags = cpu_to_be32(flags);
  624. lh->lh_tail = cpu_to_be32(tail);
  625. lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
  626. hash = ~crc32(~0, lh, LH_V1_SIZE);
  627. lh->lh_hash = cpu_to_be32(hash);
  628. ktime_get_coarse_real_ts64(&tv);
  629. lh->lh_nsec = cpu_to_be32(tv.tv_nsec);
  630. lh->lh_sec = cpu_to_be64(tv.tv_sec);
  631. addr = gfs2_log_bmap(sdp);
  632. lh->lh_addr = cpu_to_be64(addr);
  633. lh->lh_jinode = cpu_to_be64(GFS2_I(jd->jd_inode)->i_no_addr);
  634. /* We may only write local statfs, quota, etc., when writing to our
  635. own journal. The values are left 0 when recovering a journal
  636. different from our own. */
  637. if (!(flags & GFS2_LOG_HEAD_RECOVERY)) {
  638. lh->lh_statfs_addr =
  639. cpu_to_be64(GFS2_I(sdp->sd_sc_inode)->i_no_addr);
  640. lh->lh_quota_addr =
  641. cpu_to_be64(GFS2_I(sdp->sd_qc_inode)->i_no_addr);
  642. spin_lock(&sdp->sd_statfs_spin);
  643. lh->lh_local_total = cpu_to_be64(l_sc->sc_total);
  644. lh->lh_local_free = cpu_to_be64(l_sc->sc_free);
  645. lh->lh_local_dinodes = cpu_to_be64(l_sc->sc_dinodes);
  646. spin_unlock(&sdp->sd_statfs_spin);
  647. }
  648. BUILD_BUG_ON(offsetof(struct gfs2_log_header, lh_crc) != LH_V1_SIZE);
  649. crc = crc32c(~0, (void *)lh + LH_V1_SIZE + 4,
  650. sb->s_blocksize - LH_V1_SIZE - 4);
  651. lh->lh_crc = cpu_to_be32(crc);
  652. gfs2_log_write(sdp, page, sb->s_blocksize, 0, addr);
  653. gfs2_log_flush_bio(sdp, REQ_OP_WRITE, op_flags);
  654. log_flush_wait(sdp);
  655. }
  656. /**
  657. * log_write_header - Get and initialize a journal header buffer
  658. * @sdp: The GFS2 superblock
  659. * @flags: The log header flags, including log header origin
  660. *
  661. * Returns: the initialized log buffer descriptor
  662. */
  663. static void log_write_header(struct gfs2_sbd *sdp, u32 flags)
  664. {
  665. unsigned int tail;
  666. int op_flags = REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC;
  667. enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
  668. gfs2_assert_withdraw(sdp, (state != SFS_FROZEN));
  669. tail = current_tail(sdp);
  670. if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) {
  671. gfs2_ordered_wait(sdp);
  672. log_flush_wait(sdp);
  673. op_flags = REQ_SYNC | REQ_META | REQ_PRIO;
  674. }
  675. sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
  676. gfs2_write_log_header(sdp, sdp->sd_jdesc, sdp->sd_log_sequence++, tail,
  677. flags, op_flags);
  678. if (sdp->sd_log_tail != tail)
  679. log_pull_tail(sdp, tail);
  680. }
  681. /**
  682. * gfs2_log_flush - flush incore transaction(s)
  683. * @sdp: the filesystem
  684. * @gl: The glock structure to flush. If NULL, flush the whole incore log
  685. * @flags: The log header flags: GFS2_LOG_HEAD_FLUSH_* and debug flags
  686. *
  687. */
  688. void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
  689. {
  690. struct gfs2_trans *tr;
  691. enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
  692. down_write(&sdp->sd_log_flush_lock);
  693. /* Log might have been flushed while we waited for the flush lock */
  694. if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
  695. up_write(&sdp->sd_log_flush_lock);
  696. return;
  697. }
  698. trace_gfs2_log_flush(sdp, 1, flags);
  699. if (flags & GFS2_LOG_HEAD_FLUSH_SHUTDOWN)
  700. clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
  701. sdp->sd_log_flush_head = sdp->sd_log_head;
  702. tr = sdp->sd_log_tr;
  703. if (tr) {
  704. sdp->sd_log_tr = NULL;
  705. INIT_LIST_HEAD(&tr->tr_ail1_list);
  706. INIT_LIST_HEAD(&tr->tr_ail2_list);
  707. tr->tr_first = sdp->sd_log_flush_head;
  708. if (unlikely (state == SFS_FROZEN))
  709. gfs2_assert_withdraw(sdp, !tr->tr_num_buf_new && !tr->tr_num_databuf_new);
  710. }
  711. if (unlikely(state == SFS_FROZEN))
  712. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  713. gfs2_assert_withdraw(sdp,
  714. sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
  715. gfs2_ordered_write(sdp);
  716. lops_before_commit(sdp, tr);
  717. gfs2_log_flush_bio(sdp, REQ_OP_WRITE, 0);
  718. if (sdp->sd_log_head != sdp->sd_log_flush_head) {
  719. log_flush_wait(sdp);
  720. log_write_header(sdp, flags);
  721. } else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
  722. atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
  723. trace_gfs2_log_blocks(sdp, -1);
  724. log_write_header(sdp, flags);
  725. }
  726. lops_after_commit(sdp, tr);
  727. gfs2_log_lock(sdp);
  728. sdp->sd_log_head = sdp->sd_log_flush_head;
  729. sdp->sd_log_blks_reserved = 0;
  730. sdp->sd_log_commited_revoke = 0;
  731. spin_lock(&sdp->sd_ail_lock);
  732. if (tr && !list_empty(&tr->tr_ail1_list)) {
  733. list_add(&tr->tr_list, &sdp->sd_ail1_list);
  734. tr = NULL;
  735. }
  736. spin_unlock(&sdp->sd_ail_lock);
  737. gfs2_log_unlock(sdp);
  738. if (!(flags & GFS2_LOG_HEAD_FLUSH_NORMAL)) {
  739. if (!sdp->sd_log_idle) {
  740. for (;;) {
  741. gfs2_ail1_start(sdp);
  742. gfs2_ail1_wait(sdp);
  743. if (gfs2_ail1_empty(sdp))
  744. break;
  745. }
  746. atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
  747. trace_gfs2_log_blocks(sdp, -1);
  748. log_write_header(sdp, flags);
  749. sdp->sd_log_head = sdp->sd_log_flush_head;
  750. }
  751. if (flags & (GFS2_LOG_HEAD_FLUSH_SHUTDOWN |
  752. GFS2_LOG_HEAD_FLUSH_FREEZE))
  753. gfs2_log_shutdown(sdp);
  754. if (flags & GFS2_LOG_HEAD_FLUSH_FREEZE)
  755. atomic_set(&sdp->sd_freeze_state, SFS_FROZEN);
  756. }
  757. trace_gfs2_log_flush(sdp, 0, flags);
  758. up_write(&sdp->sd_log_flush_lock);
  759. kfree(tr);
  760. }
  761. /**
  762. * gfs2_merge_trans - Merge a new transaction into a cached transaction
  763. * @old: Original transaction to be expanded
  764. * @new: New transaction to be merged
  765. */
  766. static void gfs2_merge_trans(struct gfs2_trans *old, struct gfs2_trans *new)
  767. {
  768. WARN_ON_ONCE(!test_bit(TR_ATTACHED, &old->tr_flags));
  769. old->tr_num_buf_new += new->tr_num_buf_new;
  770. old->tr_num_databuf_new += new->tr_num_databuf_new;
  771. old->tr_num_buf_rm += new->tr_num_buf_rm;
  772. old->tr_num_databuf_rm += new->tr_num_databuf_rm;
  773. old->tr_num_revoke += new->tr_num_revoke;
  774. old->tr_num_revoke_rm += new->tr_num_revoke_rm;
  775. list_splice_tail_init(&new->tr_databuf, &old->tr_databuf);
  776. list_splice_tail_init(&new->tr_buf, &old->tr_buf);
  777. }
  778. static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  779. {
  780. unsigned int reserved;
  781. unsigned int unused;
  782. unsigned int maxres;
  783. gfs2_log_lock(sdp);
  784. if (sdp->sd_log_tr) {
  785. gfs2_merge_trans(sdp->sd_log_tr, tr);
  786. } else if (tr->tr_num_buf_new || tr->tr_num_databuf_new) {
  787. gfs2_assert_withdraw(sdp, test_bit(TR_ALLOCED, &tr->tr_flags));
  788. sdp->sd_log_tr = tr;
  789. set_bit(TR_ATTACHED, &tr->tr_flags);
  790. }
  791. sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
  792. reserved = calc_reserved(sdp);
  793. maxres = sdp->sd_log_blks_reserved + tr->tr_reserved;
  794. gfs2_assert_withdraw(sdp, maxres >= reserved);
  795. unused = maxres - reserved;
  796. atomic_add(unused, &sdp->sd_log_blks_free);
  797. trace_gfs2_log_blocks(sdp, unused);
  798. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  799. sdp->sd_jdesc->jd_blocks);
  800. sdp->sd_log_blks_reserved = reserved;
  801. gfs2_log_unlock(sdp);
  802. }
  803. /**
  804. * gfs2_log_commit - Commit a transaction to the log
  805. * @sdp: the filesystem
  806. * @tr: the transaction
  807. *
  808. * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
  809. * or the total number of used blocks (pinned blocks plus AIL blocks)
  810. * is greater than thresh2.
  811. *
  812. * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
  813. * journal size.
  814. *
  815. * Returns: errno
  816. */
  817. void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  818. {
  819. log_refund(sdp, tr);
  820. if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
  821. ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
  822. atomic_read(&sdp->sd_log_thresh2)))
  823. wake_up(&sdp->sd_logd_waitq);
  824. }
  825. /**
  826. * gfs2_log_shutdown - write a shutdown header into a journal
  827. * @sdp: the filesystem
  828. *
  829. */
  830. void gfs2_log_shutdown(struct gfs2_sbd *sdp)
  831. {
  832. gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
  833. gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
  834. gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
  835. sdp->sd_log_flush_head = sdp->sd_log_head;
  836. log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT | GFS2_LFC_SHUTDOWN);
  837. gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
  838. gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
  839. sdp->sd_log_head = sdp->sd_log_flush_head;
  840. sdp->sd_log_tail = sdp->sd_log_head;
  841. }
  842. static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
  843. {
  844. return (atomic_read(&sdp->sd_log_pinned) +
  845. atomic_read(&sdp->sd_log_blks_needed) >=
  846. atomic_read(&sdp->sd_log_thresh1));
  847. }
  848. static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
  849. {
  850. unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
  851. if (test_and_clear_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags))
  852. return 1;
  853. return used_blocks + atomic_read(&sdp->sd_log_blks_needed) >=
  854. atomic_read(&sdp->sd_log_thresh2);
  855. }
  856. /**
  857. * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
  858. * @sdp: Pointer to GFS2 superblock
  859. *
  860. * Also, periodically check to make sure that we're using the most recent
  861. * journal index.
  862. */
  863. int gfs2_logd(void *data)
  864. {
  865. struct gfs2_sbd *sdp = data;
  866. unsigned long t = 1;
  867. DEFINE_WAIT(wait);
  868. bool did_flush;
  869. while (!kthread_should_stop()) {
  870. /* Check for errors writing to the journal */
  871. if (sdp->sd_log_error) {
  872. gfs2_lm_withdraw(sdp,
  873. "GFS2: fsid=%s: error %d: "
  874. "withdrawing the file system to "
  875. "prevent further damage.\n",
  876. sdp->sd_fsname, sdp->sd_log_error);
  877. }
  878. did_flush = false;
  879. if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
  880. gfs2_ail1_empty(sdp);
  881. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
  882. GFS2_LFC_LOGD_JFLUSH_REQD);
  883. did_flush = true;
  884. }
  885. if (gfs2_ail_flush_reqd(sdp)) {
  886. gfs2_ail1_start(sdp);
  887. gfs2_ail1_wait(sdp);
  888. gfs2_ail1_empty(sdp);
  889. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
  890. GFS2_LFC_LOGD_AIL_FLUSH_REQD);
  891. did_flush = true;
  892. }
  893. if (!gfs2_ail_flush_reqd(sdp) || did_flush)
  894. wake_up(&sdp->sd_log_waitq);
  895. t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
  896. try_to_freeze();
  897. do {
  898. prepare_to_wait(&sdp->sd_logd_waitq, &wait,
  899. TASK_INTERRUPTIBLE);
  900. if (!gfs2_ail_flush_reqd(sdp) &&
  901. !gfs2_jrnl_flush_reqd(sdp) &&
  902. !kthread_should_stop())
  903. t = schedule_timeout(t);
  904. } while(t && !gfs2_ail_flush_reqd(sdp) &&
  905. !gfs2_jrnl_flush_reqd(sdp) &&
  906. !kthread_should_stop());
  907. finish_wait(&sdp->sd_logd_waitq, &wait);
  908. }
  909. return 0;
  910. }