xref: /linux/fs/gfs2/log.c (revision 4da0dd95be3b0321bf9687fb1a3c2fed3319c032)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
4  * Copyright (C) 2004-2007 Red Hat, Inc.  All rights reserved.
5  */
6 
7 #include <linux/sched.h>
8 #include <linux/slab.h>
9 #include <linux/spinlock.h>
10 #include <linux/completion.h>
11 #include <linux/buffer_head.h>
12 #include <linux/gfs2_ondisk.h>
13 #include <linux/crc32.h>
14 #include <linux/crc32c.h>
15 #include <linux/delay.h>
16 #include <linux/kthread.h>
17 #include <linux/freezer.h>
18 #include <linux/bio.h>
19 #include <linux/blkdev.h>
20 #include <linux/writeback.h>
21 #include <linux/list_sort.h>
22 
23 #include "gfs2.h"
24 #include "incore.h"
25 #include "bmap.h"
26 #include "glock.h"
27 #include "log.h"
28 #include "lops.h"
29 #include "meta_io.h"
30 #include "util.h"
31 #include "dir.h"
32 #include "trace_gfs2.h"
33 #include "trans.h"
34 #include "aops.h"
35 
36 static void gfs2_log_shutdown(struct gfs2_sbd *sdp);
37 
38 /**
39  * gfs2_struct2blk - compute stuff
40  * @sdp: the filesystem
41  * @nstruct: the number of structures
42  *
43  * Compute the number of log descriptor blocks needed to hold a certain number
44  * of structures of a certain size.
45  *
46  * Returns: the number of blocks needed (minimum is always 1)
47  */
48 
gfs2_struct2blk(struct gfs2_sbd * sdp,unsigned int nstruct)49 unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct)
50 {
51 	unsigned int blks;
52 	unsigned int first, second;
53 
54 	/* The initial struct gfs2_log_descriptor block */
55 	blks = 1;
56 	first = sdp->sd_ldptrs;
57 
58 	if (nstruct > first) {
59 		/* Subsequent struct gfs2_meta_header blocks */
60 		second = sdp->sd_inptrs;
61 		blks += DIV_ROUND_UP(nstruct - first, second);
62 	}
63 
64 	return blks;
65 }
66 
67 /**
68  * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
69  * @bd: The gfs2_bufdata to remove
70  *
71  * The ail lock _must_ be held when calling this function
72  *
73  */
74 
gfs2_remove_from_ail(struct gfs2_bufdata * bd)75 static void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
76 {
77 	bd->bd_tr = NULL;
78 	list_del_init(&bd->bd_ail_st_list);
79 	list_del_init(&bd->bd_ail_gl_list);
80 	atomic_dec(&bd->bd_gl->gl_ail_count);
81 	brelse(bd->bd_bh);
82 }
83 
84 /**
85  * gfs2_ail1_start_one - Start I/O on a transaction
86  * @sdp: The superblock
87  * @wbc: The writeback control structure
88  * @tr: The transaction to start I/O on
89  * @plug: The block plug currently active
90  */
91 
gfs2_ail1_start_one(struct gfs2_sbd * sdp,struct writeback_control * wbc,struct gfs2_trans * tr,struct blk_plug * plug)92 static int gfs2_ail1_start_one(struct gfs2_sbd *sdp,
93 			       struct writeback_control *wbc,
94 			       struct gfs2_trans *tr, struct blk_plug *plug)
95 __releases(&sdp->sd_ail_lock)
96 __acquires(&sdp->sd_ail_lock)
97 {
98 	struct gfs2_glock *gl = NULL;
99 	struct address_space *mapping;
100 	struct gfs2_bufdata *bd, *s;
101 	struct buffer_head *bh;
102 	int ret = 0;
103 
104 	list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list, bd_ail_st_list) {
105 		bh = bd->bd_bh;
106 
107 		gfs2_assert(sdp, bd->bd_tr == tr);
108 
109 		if (!buffer_busy(bh)) {
110 			if (buffer_uptodate(bh)) {
111 				list_move(&bd->bd_ail_st_list,
112 					  &tr->tr_ail2_list);
113 				continue;
114 			}
115 			if (!cmpxchg(&sdp->sd_log_error, 0, -EIO))
116 				gfs2_io_error_bh(sdp, bh);
117 		}
118 
119 		if (gfs2_withdrawn(sdp)) {
120 			gfs2_remove_from_ail(bd);
121 			continue;
122 		}
123 		if (!buffer_dirty(bh))
124 			continue;
125 		if (gl == bd->bd_gl)
126 			continue;
127 		gl = bd->bd_gl;
128 		list_move(&bd->bd_ail_st_list, &tr->tr_ail1_list);
129 		mapping = bh->b_folio->mapping;
130 		if (!mapping)
131 			continue;
132 		spin_unlock(&sdp->sd_ail_lock);
133 		BUG_ON(GFS2_SB(mapping->host) != sdp);
134 		if (gfs2_is_jdata(GFS2_I(mapping->host)))
135 			ret = gfs2_jdata_writeback(mapping, wbc);
136 		else
137 			ret = mapping->a_ops->writepages(mapping, wbc);
138 		if (need_resched()) {
139 			blk_finish_plug(plug);
140 			cond_resched();
141 			blk_start_plug(plug);
142 		}
143 		spin_lock(&sdp->sd_ail_lock);
144 		if (ret == -ENODATA) /* if a jdata write into a new hole */
145 			ret = 0; /* ignore it */
146 		mapping_set_error(mapping, ret);
147 		if (ret || wbc->nr_to_write <= 0)
148 			break;
149 		return -EBUSY;
150 	}
151 
152 	return ret;
153 }
154 
dump_ail_list(struct gfs2_sbd * sdp)155 static void dump_ail_list(struct gfs2_sbd *sdp)
156 {
157 	struct gfs2_trans *tr;
158 	struct gfs2_bufdata *bd;
159 	struct buffer_head *bh;
160 
161 	list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
162 		list_for_each_entry_reverse(bd, &tr->tr_ail1_list,
163 					    bd_ail_st_list) {
164 			bh = bd->bd_bh;
165 			fs_err(sdp, "bd %p: blk:0x%llx bh=%p ", bd,
166 			       (unsigned long long)bd->bd_blkno, bh);
167 			if (!bh) {
168 				fs_err(sdp, "\n");
169 				continue;
170 			}
171 			fs_err(sdp, "0x%llx up2:%d dirt:%d lkd:%d req:%d "
172 			       "map:%d new:%d ar:%d aw:%d delay:%d "
173 			       "io err:%d unwritten:%d dfr:%d pin:%d esc:%d\n",
174 			       (unsigned long long)bh->b_blocknr,
175 			       buffer_uptodate(bh), buffer_dirty(bh),
176 			       buffer_locked(bh), buffer_req(bh),
177 			       buffer_mapped(bh), buffer_new(bh),
178 			       buffer_async_read(bh), buffer_async_write(bh),
179 			       buffer_delay(bh), buffer_write_io_error(bh),
180 			       buffer_unwritten(bh),
181 			       buffer_defer_completion(bh),
182 			       buffer_pinned(bh), buffer_escaped(bh));
183 		}
184 	}
185 }
186 
187 /**
188  * gfs2_ail1_flush - start writeback of some ail1 entries
189  * @sdp: The super block
190  * @wbc: The writeback control structure
191  *
192  * Writes back some ail1 entries, according to the limits in the
193  * writeback control structure
194  */
195 
gfs2_ail1_flush(struct gfs2_sbd * sdp,struct writeback_control * wbc)196 void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
197 {
198 	struct list_head *head = &sdp->sd_ail1_list;
199 	struct gfs2_trans *tr;
200 	struct blk_plug plug;
201 	int ret;
202 	unsigned long flush_start = jiffies;
203 
204 	trace_gfs2_ail_flush(sdp, wbc, 1);
205 	blk_start_plug(&plug);
206 	spin_lock(&sdp->sd_ail_lock);
207 restart:
208 	ret = 0;
209 	if (time_after(jiffies, flush_start + (HZ * 600))) {
210 		fs_err(sdp, "Error: In %s for ten minutes! t=%d\n",
211 		       __func__, current->journal_info ? 1 : 0);
212 		dump_ail_list(sdp);
213 		goto out;
214 	}
215 	list_for_each_entry_reverse(tr, head, tr_list) {
216 		if (wbc->nr_to_write <= 0)
217 			break;
218 		ret = gfs2_ail1_start_one(sdp, wbc, tr, &plug);
219 		if (ret) {
220 			if (ret == -EBUSY)
221 				goto restart;
222 			break;
223 		}
224 	}
225 out:
226 	spin_unlock(&sdp->sd_ail_lock);
227 	blk_finish_plug(&plug);
228 	if (ret) {
229 		gfs2_lm(sdp, "gfs2_ail1_start_one returned: %d\n", ret);
230 		gfs2_withdraw(sdp);
231 	}
232 	trace_gfs2_ail_flush(sdp, wbc, 0);
233 }
234 
235 /**
236  * gfs2_ail1_start - start writeback of all ail1 entries
237  * @sdp: The superblock
238  */
239 
gfs2_ail1_start(struct gfs2_sbd * sdp)240 static void gfs2_ail1_start(struct gfs2_sbd *sdp)
241 {
242 	struct writeback_control wbc = {
243 		.sync_mode = WB_SYNC_NONE,
244 		.nr_to_write = LONG_MAX,
245 		.range_start = 0,
246 		.range_end = LLONG_MAX,
247 	};
248 
249 	return gfs2_ail1_flush(sdp, &wbc);
250 }
251 
gfs2_log_update_flush_tail(struct gfs2_sbd * sdp)252 static void gfs2_log_update_flush_tail(struct gfs2_sbd *sdp)
253 {
254 	unsigned int new_flush_tail = sdp->sd_log_head;
255 	struct gfs2_trans *tr;
256 
257 	if (!list_empty(&sdp->sd_ail1_list)) {
258 		tr = list_last_entry(&sdp->sd_ail1_list,
259 				     struct gfs2_trans, tr_list);
260 		new_flush_tail = tr->tr_first;
261 	}
262 	sdp->sd_log_flush_tail = new_flush_tail;
263 }
264 
gfs2_log_update_head(struct gfs2_sbd * sdp)265 static void gfs2_log_update_head(struct gfs2_sbd *sdp)
266 {
267 	unsigned int new_head = sdp->sd_log_flush_head;
268 
269 	if (sdp->sd_log_flush_tail == sdp->sd_log_head)
270 		sdp->sd_log_flush_tail = new_head;
271 	sdp->sd_log_head = new_head;
272 }
273 
274 /*
275  * gfs2_ail_empty_tr - empty one of the ail lists of a transaction
276  */
277 
gfs2_ail_empty_tr(struct gfs2_sbd * sdp,struct gfs2_trans * tr,struct list_head * head)278 static void gfs2_ail_empty_tr(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
279 			      struct list_head *head)
280 {
281 	struct gfs2_bufdata *bd;
282 
283 	while (!list_empty(head)) {
284 		bd = list_first_entry(head, struct gfs2_bufdata,
285 				      bd_ail_st_list);
286 		gfs2_assert(sdp, bd->bd_tr == tr);
287 		gfs2_remove_from_ail(bd);
288 	}
289 }
290 
291 /**
292  * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
293  * @sdp: the filesystem
294  * @tr: the transaction
295  * @max_revokes: If nonzero, issue revokes for the bd items for written buffers
296  *
297  * returns: the transaction's count of remaining active items
298  */
299 
gfs2_ail1_empty_one(struct gfs2_sbd * sdp,struct gfs2_trans * tr,int * max_revokes)300 static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
301 				int *max_revokes)
302 {
303 	struct gfs2_bufdata *bd, *s;
304 	struct buffer_head *bh;
305 	int active_count = 0;
306 
307 	list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list,
308 					 bd_ail_st_list) {
309 		bh = bd->bd_bh;
310 		gfs2_assert(sdp, bd->bd_tr == tr);
311 		/*
312 		 * If another process flagged an io error, e.g. writing to the
313 		 * journal, error all other bhs and move them off the ail1 to
314 		 * prevent a tight loop when unmount tries to flush ail1,
315 		 * regardless of whether they're still busy. If no outside
316 		 * errors were found and the buffer is busy, move to the next.
317 		 * If the ail buffer is not busy and caught an error, flag it
318 		 * for others.
319 		 */
320 		if (!sdp->sd_log_error && buffer_busy(bh)) {
321 			active_count++;
322 			continue;
323 		}
324 		if (!buffer_uptodate(bh) &&
325 		    !cmpxchg(&sdp->sd_log_error, 0, -EIO))
326 			gfs2_io_error_bh(sdp, bh);
327 		/*
328 		 * If we have space for revokes and the bd is no longer on any
329 		 * buf list, we can just add a revoke for it immediately and
330 		 * avoid having to put it on the ail2 list, where it would need
331 		 * to be revoked later.
332 		 */
333 		if (*max_revokes && list_empty(&bd->bd_list)) {
334 			gfs2_add_revoke(sdp, bd);
335 			(*max_revokes)--;
336 			continue;
337 		}
338 		list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
339 	}
340 	return active_count;
341 }
342 
343 /**
344  * gfs2_ail1_empty - Try to empty the ail1 lists
345  * @sdp: The superblock
346  * @max_revokes: If non-zero, add revokes where appropriate
347  *
348  * Tries to empty the ail1 lists, starting with the oldest first.
349  * Returns %true if the ail1 list is now empty.
350  */
351 
gfs2_ail1_empty(struct gfs2_sbd * sdp,int max_revokes)352 static bool gfs2_ail1_empty(struct gfs2_sbd *sdp, int max_revokes)
353 {
354 	struct gfs2_trans *tr, *s;
355 	int oldest_tr = 1;
356 	bool empty;
357 
358 	spin_lock(&sdp->sd_ail_lock);
359 	list_for_each_entry_safe_reverse(tr, s, &sdp->sd_ail1_list, tr_list) {
360 		if (!gfs2_ail1_empty_one(sdp, tr, &max_revokes) && oldest_tr)
361 			list_move(&tr->tr_list, &sdp->sd_ail2_list);
362 		else
363 			oldest_tr = 0;
364 	}
365 	gfs2_log_update_flush_tail(sdp);
366 	empty = list_empty(&sdp->sd_ail1_list);
367 	spin_unlock(&sdp->sd_ail_lock);
368 
369 	return empty;
370 }
371 
gfs2_ail1_wait(struct gfs2_sbd * sdp)372 static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
373 {
374 	struct gfs2_trans *tr;
375 	struct gfs2_bufdata *bd;
376 	struct buffer_head *bh;
377 
378 	spin_lock(&sdp->sd_ail_lock);
379 	list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
380 		list_for_each_entry(bd, &tr->tr_ail1_list, bd_ail_st_list) {
381 			bh = bd->bd_bh;
382 			if (!buffer_locked(bh))
383 				continue;
384 			get_bh(bh);
385 			spin_unlock(&sdp->sd_ail_lock);
386 			wait_on_buffer(bh);
387 			brelse(bh);
388 			return;
389 		}
390 	}
391 	spin_unlock(&sdp->sd_ail_lock);
392 }
393 
__ail2_empty(struct gfs2_sbd * sdp,struct gfs2_trans * tr)394 static void __ail2_empty(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
395 {
396 	gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
397 	list_del(&tr->tr_list);
398 	gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
399 	gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
400 	gfs2_trans_free(sdp, tr);
401 }
402 
ail2_empty(struct gfs2_sbd * sdp,unsigned int new_tail)403 static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
404 {
405 	struct list_head *ail2_list = &sdp->sd_ail2_list;
406 	unsigned int old_tail = sdp->sd_log_tail;
407 	struct gfs2_trans *tr, *safe;
408 
409 	spin_lock(&sdp->sd_ail_lock);
410 	if (old_tail <= new_tail) {
411 		list_for_each_entry_safe(tr, safe, ail2_list, tr_list) {
412 			if (old_tail <= tr->tr_first && tr->tr_first < new_tail)
413 				__ail2_empty(sdp, tr);
414 		}
415 	} else {
416 		list_for_each_entry_safe(tr, safe, ail2_list, tr_list) {
417 			if (old_tail <= tr->tr_first || tr->tr_first < new_tail)
418 				__ail2_empty(sdp, tr);
419 		}
420 	}
421 	spin_unlock(&sdp->sd_ail_lock);
422 }
423 
424 /**
425  * gfs2_log_is_empty - Check if the log is empty
426  * @sdp: The GFS2 superblock
427  */
428 
gfs2_log_is_empty(struct gfs2_sbd * sdp)429 bool gfs2_log_is_empty(struct gfs2_sbd *sdp) {
430 	return atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks;
431 }
432 
__gfs2_log_try_reserve_revokes(struct gfs2_sbd * sdp,unsigned int revokes)433 static bool __gfs2_log_try_reserve_revokes(struct gfs2_sbd *sdp, unsigned int revokes)
434 {
435 	unsigned int available;
436 
437 	available = atomic_read(&sdp->sd_log_revokes_available);
438 	while (available >= revokes) {
439 		if (atomic_try_cmpxchg(&sdp->sd_log_revokes_available,
440 				       &available, available - revokes))
441 			return true;
442 	}
443 	return false;
444 }
445 
446 /**
447  * gfs2_log_release_revokes - Release a given number of revokes
448  * @sdp: The GFS2 superblock
449  * @revokes: The number of revokes to release
450  *
451  * sdp->sd_log_flush_lock must be held.
452  */
gfs2_log_release_revokes(struct gfs2_sbd * sdp,unsigned int revokes)453 void gfs2_log_release_revokes(struct gfs2_sbd *sdp, unsigned int revokes)
454 {
455 	if (revokes)
456 		atomic_add(revokes, &sdp->sd_log_revokes_available);
457 }
458 
459 /**
460  * gfs2_log_release - Release a given number of log blocks
461  * @sdp: The GFS2 superblock
462  * @blks: The number of blocks
463  *
464  */
465 
gfs2_log_release(struct gfs2_sbd * sdp,unsigned int blks)466 void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
467 {
468 	atomic_add(blks, &sdp->sd_log_blks_free);
469 	trace_gfs2_log_blocks(sdp, blks);
470 	gfs2_assert_withdraw(sdp, !sdp->sd_jdesc ||
471 			atomic_read(&sdp->sd_log_blks_free) <=
472 			sdp->sd_jdesc->jd_blocks);
473 	if (atomic_read(&sdp->sd_log_blks_needed))
474 		wake_up(&sdp->sd_log_waitq);
475 }
476 
477 /**
478  * __gfs2_log_try_reserve - Try to make a log reservation
479  * @sdp: The GFS2 superblock
480  * @blks: The number of blocks to reserve
481  * @taboo_blks: The number of blocks to leave free
482  *
483  * Try to do the same as __gfs2_log_reserve(), but fail if no more log
484  * space is immediately available.
485  */
__gfs2_log_try_reserve(struct gfs2_sbd * sdp,unsigned int blks,unsigned int taboo_blks)486 static bool __gfs2_log_try_reserve(struct gfs2_sbd *sdp, unsigned int blks,
487 				   unsigned int taboo_blks)
488 {
489 	unsigned wanted = blks + taboo_blks;
490 	unsigned int free_blocks;
491 
492 	free_blocks = atomic_read(&sdp->sd_log_blks_free);
493 	while (free_blocks >= wanted) {
494 		if (atomic_try_cmpxchg(&sdp->sd_log_blks_free, &free_blocks,
495 				       free_blocks - blks)) {
496 			trace_gfs2_log_blocks(sdp, -blks);
497 			return true;
498 		}
499 	}
500 	return false;
501 }
502 
503 /**
504  * __gfs2_log_reserve - Make a log reservation
505  * @sdp: The GFS2 superblock
506  * @blks: The number of blocks to reserve
507  * @taboo_blks: The number of blocks to leave free
508  *
509  * @taboo_blks is set to 0 for logd, and to GFS2_LOG_FLUSH_MIN_BLOCKS
510  * for all other processes.  This ensures that when the log is almost full,
511  * logd will still be able to call gfs2_log_flush one more time  without
512  * blocking, which will advance the tail and make some more log space
513  * available.
514  *
515  * We no longer flush the log here, instead we wake up logd to do that
516  * for us. To avoid the thundering herd and to ensure that we deal fairly
517  * with queued waiters, we use an exclusive wait. This means that when we
518  * get woken with enough journal space to get our reservation, we need to
519  * wake the next waiter on the list.
520  */
521 
__gfs2_log_reserve(struct gfs2_sbd * sdp,unsigned int blks,unsigned int taboo_blks)522 static void __gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks,
523 			       unsigned int taboo_blks)
524 {
525 	unsigned wanted = blks + taboo_blks;
526 	unsigned int free_blocks;
527 
528 	atomic_add(blks, &sdp->sd_log_blks_needed);
529 	for (;;) {
530 		if (current != sdp->sd_logd_process)
531 			wake_up(&sdp->sd_logd_waitq);
532 		io_wait_event(sdp->sd_log_waitq,
533 			(free_blocks = atomic_read(&sdp->sd_log_blks_free),
534 			 free_blocks >= wanted));
535 		do {
536 			if (atomic_try_cmpxchg(&sdp->sd_log_blks_free,
537 					       &free_blocks,
538 					       free_blocks - blks))
539 				goto reserved;
540 		} while (free_blocks >= wanted);
541 	}
542 
543 reserved:
544 	trace_gfs2_log_blocks(sdp, -blks);
545 	if (atomic_sub_return(blks, &sdp->sd_log_blks_needed))
546 		wake_up(&sdp->sd_log_waitq);
547 }
548 
549 /**
550  * gfs2_log_try_reserve - Try to make a log reservation
551  * @sdp: The GFS2 superblock
552  * @tr: The transaction
553  * @extra_revokes: The number of additional revokes reserved (output)
554  *
555  * This is similar to gfs2_log_reserve, but sdp->sd_log_flush_lock must be
556  * held for correct revoke accounting.
557  */
558 
gfs2_log_try_reserve(struct gfs2_sbd * sdp,struct gfs2_trans * tr,unsigned int * extra_revokes)559 bool gfs2_log_try_reserve(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
560 			  unsigned int *extra_revokes)
561 {
562 	unsigned int blks = tr->tr_reserved;
563 	unsigned int revokes = tr->tr_revokes;
564 	unsigned int revoke_blks = 0;
565 
566 	*extra_revokes = 0;
567 	if (revokes && !__gfs2_log_try_reserve_revokes(sdp, revokes)) {
568 		revoke_blks = DIV_ROUND_UP(revokes, sdp->sd_inptrs);
569 		*extra_revokes = revoke_blks * sdp->sd_inptrs - revokes;
570 		blks += revoke_blks;
571 	}
572 	if (!blks)
573 		return true;
574 	if (__gfs2_log_try_reserve(sdp, blks, GFS2_LOG_FLUSH_MIN_BLOCKS))
575 		return true;
576 	if (!revoke_blks)
577 		gfs2_log_release_revokes(sdp, revokes);
578 	return false;
579 }
580 
581 /**
582  * gfs2_log_reserve - Make a log reservation
583  * @sdp: The GFS2 superblock
584  * @tr: The transaction
585  * @extra_revokes: The number of additional revokes reserved (output)
586  *
587  * sdp->sd_log_flush_lock must not be held.
588  */
589 
gfs2_log_reserve(struct gfs2_sbd * sdp,struct gfs2_trans * tr,unsigned int * extra_revokes)590 void gfs2_log_reserve(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
591 		      unsigned int *extra_revokes)
592 {
593 	unsigned int blks = tr->tr_reserved;
594 	unsigned int revokes = tr->tr_revokes;
595 	unsigned int revoke_blks;
596 
597 	*extra_revokes = 0;
598 	if (revokes) {
599 		revoke_blks = DIV_ROUND_UP(revokes, sdp->sd_inptrs);
600 		*extra_revokes = revoke_blks * sdp->sd_inptrs - revokes;
601 		blks += revoke_blks;
602 	}
603 	__gfs2_log_reserve(sdp, blks, GFS2_LOG_FLUSH_MIN_BLOCKS);
604 }
605 
606 /**
607  * log_distance - Compute distance between two journal blocks
608  * @sdp: The GFS2 superblock
609  * @newer: The most recent journal block of the pair
610  * @older: The older journal block of the pair
611  *
612  *   Compute the distance (in the journal direction) between two
613  *   blocks in the journal
614  *
615  * Returns: the distance in blocks
616  */
617 
log_distance(struct gfs2_sbd * sdp,unsigned int newer,unsigned int older)618 static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
619 					unsigned int older)
620 {
621 	int dist;
622 
623 	dist = newer - older;
624 	if (dist < 0)
625 		dist += sdp->sd_jdesc->jd_blocks;
626 
627 	return dist;
628 }
629 
630 /**
631  * calc_reserved - Calculate the number of blocks to keep reserved
632  * @sdp: The GFS2 superblock
633  *
634  * This is complex.  We need to reserve room for all our currently used
635  * metadata blocks (e.g. normal file I/O rewriting file time stamps) and
636  * all our journaled data blocks for journaled files (e.g. files in the
637  * meta_fs like rindex, or files for which chattr +j was done.)
638  * If we don't reserve enough space, corruption will follow.
639  *
640  * We can have metadata blocks and jdata blocks in the same journal.  Each
641  * type gets its own log descriptor, for which we need to reserve a block.
642  * In fact, each type has the potential for needing more than one log descriptor
643  * in cases where we have more blocks than will fit in a log descriptor.
644  * Metadata journal entries take up half the space of journaled buffer entries.
645  *
646  * Also, we need to reserve blocks for revoke journal entries and one for an
647  * overall header for the lot.
648  *
649  * Returns: the number of blocks reserved
650  */
calc_reserved(struct gfs2_sbd * sdp)651 static unsigned int calc_reserved(struct gfs2_sbd *sdp)
652 {
653 	unsigned int reserved = GFS2_LOG_FLUSH_MIN_BLOCKS;
654 	unsigned int blocks;
655 	struct gfs2_trans *tr = sdp->sd_log_tr;
656 
657 	if (tr) {
658 		blocks = tr->tr_num_buf_new - tr->tr_num_buf_rm;
659 		reserved += blocks + DIV_ROUND_UP(blocks, buf_limit(sdp));
660 		blocks = tr->tr_num_databuf_new - tr->tr_num_databuf_rm;
661 		reserved += blocks + DIV_ROUND_UP(blocks, databuf_limit(sdp));
662 	}
663 	return reserved;
664 }
665 
log_pull_tail(struct gfs2_sbd * sdp)666 static void log_pull_tail(struct gfs2_sbd *sdp)
667 {
668 	unsigned int new_tail = sdp->sd_log_flush_tail;
669 	unsigned int dist;
670 
671 	if (new_tail == sdp->sd_log_tail)
672 		return;
673 	dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
674 	ail2_empty(sdp, new_tail);
675 	gfs2_log_release(sdp, dist);
676 	sdp->sd_log_tail = new_tail;
677 }
678 
679 
log_flush_wait(struct gfs2_sbd * sdp)680 void log_flush_wait(struct gfs2_sbd *sdp)
681 {
682 	DEFINE_WAIT(wait);
683 
684 	if (atomic_read(&sdp->sd_log_in_flight)) {
685 		do {
686 			prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
687 					TASK_UNINTERRUPTIBLE);
688 			if (atomic_read(&sdp->sd_log_in_flight))
689 				io_schedule();
690 		} while(atomic_read(&sdp->sd_log_in_flight));
691 		finish_wait(&sdp->sd_log_flush_wait, &wait);
692 	}
693 }
694 
ip_cmp(void * priv,const struct list_head * a,const struct list_head * b)695 static int ip_cmp(void *priv, const struct list_head *a, const struct list_head *b)
696 {
697 	struct gfs2_inode *ipa, *ipb;
698 
699 	ipa = list_entry(a, struct gfs2_inode, i_ordered);
700 	ipb = list_entry(b, struct gfs2_inode, i_ordered);
701 
702 	if (ipa->i_no_addr < ipb->i_no_addr)
703 		return -1;
704 	if (ipa->i_no_addr > ipb->i_no_addr)
705 		return 1;
706 	return 0;
707 }
708 
__ordered_del_inode(struct gfs2_inode * ip)709 static void __ordered_del_inode(struct gfs2_inode *ip)
710 {
711 	if (!list_empty(&ip->i_ordered))
712 		list_del_init(&ip->i_ordered);
713 }
714 
gfs2_ordered_write(struct gfs2_sbd * sdp)715 static void gfs2_ordered_write(struct gfs2_sbd *sdp)
716 {
717 	struct gfs2_inode *ip;
718 	LIST_HEAD(written);
719 
720 	spin_lock(&sdp->sd_ordered_lock);
721 	list_sort(NULL, &sdp->sd_log_ordered, &ip_cmp);
722 	while (!list_empty(&sdp->sd_log_ordered)) {
723 		ip = list_first_entry(&sdp->sd_log_ordered, struct gfs2_inode, i_ordered);
724 		if (ip->i_inode.i_mapping->nrpages == 0) {
725 			__ordered_del_inode(ip);
726 			continue;
727 		}
728 		list_move(&ip->i_ordered, &written);
729 		spin_unlock(&sdp->sd_ordered_lock);
730 		filemap_fdatawrite(ip->i_inode.i_mapping);
731 		spin_lock(&sdp->sd_ordered_lock);
732 	}
733 	list_splice(&written, &sdp->sd_log_ordered);
734 	spin_unlock(&sdp->sd_ordered_lock);
735 }
736 
gfs2_ordered_wait(struct gfs2_sbd * sdp)737 static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
738 {
739 	struct gfs2_inode *ip;
740 
741 	spin_lock(&sdp->sd_ordered_lock);
742 	while (!list_empty(&sdp->sd_log_ordered)) {
743 		ip = list_first_entry(&sdp->sd_log_ordered, struct gfs2_inode, i_ordered);
744 		__ordered_del_inode(ip);
745 		if (ip->i_inode.i_mapping->nrpages == 0)
746 			continue;
747 		spin_unlock(&sdp->sd_ordered_lock);
748 		filemap_fdatawait(ip->i_inode.i_mapping);
749 		spin_lock(&sdp->sd_ordered_lock);
750 	}
751 	spin_unlock(&sdp->sd_ordered_lock);
752 }
753 
gfs2_ordered_del_inode(struct gfs2_inode * ip)754 void gfs2_ordered_del_inode(struct gfs2_inode *ip)
755 {
756 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
757 
758 	spin_lock(&sdp->sd_ordered_lock);
759 	__ordered_del_inode(ip);
760 	spin_unlock(&sdp->sd_ordered_lock);
761 }
762 
gfs2_add_revoke(struct gfs2_sbd * sdp,struct gfs2_bufdata * bd)763 void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
764 {
765 	struct buffer_head *bh = bd->bd_bh;
766 	struct gfs2_glock *gl = bd->bd_gl;
767 
768 	sdp->sd_log_num_revoke++;
769 	if (atomic_inc_return(&gl->gl_revokes) == 1)
770 		gfs2_glock_hold(gl);
771 	bh->b_private = NULL;
772 	bd->bd_blkno = bh->b_blocknr;
773 	gfs2_remove_from_ail(bd); /* drops ref on bh */
774 	bd->bd_bh = NULL;
775 	set_bit(GLF_LFLUSH, &gl->gl_flags);
776 	list_add(&bd->bd_list, &sdp->sd_log_revokes);
777 }
778 
gfs2_glock_remove_revoke(struct gfs2_glock * gl)779 void gfs2_glock_remove_revoke(struct gfs2_glock *gl)
780 {
781 	if (atomic_dec_return(&gl->gl_revokes) == 0) {
782 		clear_bit(GLF_LFLUSH, &gl->gl_flags);
783 		gfs2_glock_put_async(gl);
784 	}
785 }
786 
787 /**
788  * gfs2_flush_revokes - Add as many revokes to the system transaction as we can
789  * @sdp: The GFS2 superblock
790  *
791  * Our usual strategy is to defer writing revokes as much as we can in the hope
792  * that we'll eventually overwrite the journal, which will make those revokes
793  * go away.  This changes when we flush the log: at that point, there will
794  * likely be some left-over space in the last revoke block of that transaction.
795  * We can fill that space with additional revokes for blocks that have already
796  * been written back.  This will basically come at no cost now, and will save
797  * us from having to keep track of those blocks on the AIL2 list later.
798  */
gfs2_flush_revokes(struct gfs2_sbd * sdp)799 void gfs2_flush_revokes(struct gfs2_sbd *sdp)
800 {
801 	/* number of revokes we still have room for */
802 	unsigned int max_revokes = atomic_read(&sdp->sd_log_revokes_available);
803 
804 	spin_lock(&sdp->sd_log_lock);
805 	gfs2_ail1_empty(sdp, max_revokes);
806 	spin_unlock(&sdp->sd_log_lock);
807 }
808 
809 /**
810  * gfs2_write_log_header - Write a journal log header buffer at lblock
811  * @sdp: The GFS2 superblock
812  * @jd: journal descriptor of the journal to which we are writing
813  * @seq: sequence number
814  * @tail: tail of the log
815  * @lblock: value for lh_blkno (block number relative to start of journal)
816  * @flags: log header flags GFS2_LOG_HEAD_*
817  * @op_flags: flags to pass to the bio
818  *
819  * Returns: the initialized log buffer descriptor
820  */
821 
gfs2_write_log_header(struct gfs2_sbd * sdp,struct gfs2_jdesc * jd,u64 seq,u32 tail,u32 lblock,u32 flags,blk_opf_t op_flags)822 void gfs2_write_log_header(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd,
823 			   u64 seq, u32 tail, u32 lblock, u32 flags,
824 			   blk_opf_t op_flags)
825 {
826 	struct gfs2_log_header *lh;
827 	u32 hash, crc;
828 	struct page *page;
829 	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
830 	struct timespec64 tv;
831 	struct super_block *sb = sdp->sd_vfs;
832 	u64 dblock;
833 
834 	if (gfs2_withdrawn(sdp))
835 		return;
836 
837 	page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
838 	lh = page_address(page);
839 	clear_page(lh);
840 
841 	lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
842 	lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
843 	lh->lh_header.__pad0 = cpu_to_be64(0);
844 	lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
845 	lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
846 	lh->lh_sequence = cpu_to_be64(seq);
847 	lh->lh_flags = cpu_to_be32(flags);
848 	lh->lh_tail = cpu_to_be32(tail);
849 	lh->lh_blkno = cpu_to_be32(lblock);
850 	hash = ~crc32(~0, lh, LH_V1_SIZE);
851 	lh->lh_hash = cpu_to_be32(hash);
852 
853 	ktime_get_coarse_real_ts64(&tv);
854 	lh->lh_nsec = cpu_to_be32(tv.tv_nsec);
855 	lh->lh_sec = cpu_to_be64(tv.tv_sec);
856 	if (!list_empty(&jd->extent_list))
857 		dblock = gfs2_log_bmap(jd, lblock);
858 	else {
859 		unsigned int extlen;
860 		int ret;
861 
862 		extlen = 1;
863 		ret = gfs2_get_extent(jd->jd_inode, lblock, &dblock, &extlen);
864 		if (gfs2_assert_withdraw(sdp, ret == 0))
865 			return;
866 	}
867 	lh->lh_addr = cpu_to_be64(dblock);
868 	lh->lh_jinode = cpu_to_be64(GFS2_I(jd->jd_inode)->i_no_addr);
869 
870 	/* We may only write local statfs, quota, etc., when writing to our
871 	   own journal. The values are left 0 when recovering a journal
872 	   different from our own. */
873 	if (!(flags & GFS2_LOG_HEAD_RECOVERY)) {
874 		lh->lh_statfs_addr =
875 			cpu_to_be64(GFS2_I(sdp->sd_sc_inode)->i_no_addr);
876 		lh->lh_quota_addr =
877 			cpu_to_be64(GFS2_I(sdp->sd_qc_inode)->i_no_addr);
878 
879 		spin_lock(&sdp->sd_statfs_spin);
880 		lh->lh_local_total = cpu_to_be64(l_sc->sc_total);
881 		lh->lh_local_free = cpu_to_be64(l_sc->sc_free);
882 		lh->lh_local_dinodes = cpu_to_be64(l_sc->sc_dinodes);
883 		spin_unlock(&sdp->sd_statfs_spin);
884 	}
885 
886 	BUILD_BUG_ON(offsetof(struct gfs2_log_header, lh_crc) != LH_V1_SIZE);
887 
888 	crc = crc32c(~0, (void *)lh + LH_V1_SIZE + 4,
889 		     sb->s_blocksize - LH_V1_SIZE - 4);
890 	lh->lh_crc = cpu_to_be32(crc);
891 
892 	gfs2_log_write(sdp, jd, page, sb->s_blocksize, 0, dblock,
893 		       REQ_OP_WRITE | op_flags);
894 	gfs2_log_submit_write(&jd->jd_log_bio);
895 }
896 
897 /**
898  * log_write_header - Get and initialize a journal header buffer
899  * @sdp: The GFS2 superblock
900  * @flags: The log header flags, including log header origin
901  *
902  * Returns: the initialized log buffer descriptor
903  */
904 
log_write_header(struct gfs2_sbd * sdp,u32 flags)905 static void log_write_header(struct gfs2_sbd *sdp, u32 flags)
906 {
907 	blk_opf_t op_flags = REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC;
908 	struct super_block *sb = sdp->sd_vfs;
909 
910 	gfs2_assert_withdraw(sdp, sb->s_writers.frozen != SB_FREEZE_COMPLETE);
911 
912 	if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) {
913 		gfs2_ordered_wait(sdp);
914 		log_flush_wait(sdp);
915 		op_flags = REQ_SYNC | REQ_META | REQ_PRIO;
916 	}
917 	sdp->sd_log_idle = (sdp->sd_log_flush_tail == sdp->sd_log_flush_head);
918 	gfs2_write_log_header(sdp, sdp->sd_jdesc, sdp->sd_log_sequence++,
919 			      sdp->sd_log_flush_tail, sdp->sd_log_flush_head,
920 			      flags, op_flags);
921 	gfs2_log_incr_head(sdp);
922 	log_flush_wait(sdp);
923 	log_pull_tail(sdp);
924 	gfs2_log_update_head(sdp);
925 }
926 
927 /**
928  * gfs2_ail_drain - drain the ail lists after a withdraw
929  * @sdp: Pointer to GFS2 superblock
930  */
gfs2_ail_drain(struct gfs2_sbd * sdp)931 void gfs2_ail_drain(struct gfs2_sbd *sdp)
932 {
933 	struct gfs2_trans *tr;
934 
935 	spin_lock(&sdp->sd_ail_lock);
936 	/*
937 	 * For transactions on the sd_ail1_list we need to drain both the
938 	 * ail1 and ail2 lists. That's because function gfs2_ail1_start_one
939 	 * (temporarily) moves items from its tr_ail1 list to tr_ail2 list
940 	 * before revokes are sent for that block. Items on the sd_ail2_list
941 	 * should have already gotten beyond that point, so no need.
942 	 */
943 	while (!list_empty(&sdp->sd_ail1_list)) {
944 		tr = list_first_entry(&sdp->sd_ail1_list, struct gfs2_trans,
945 				      tr_list);
946 		gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail1_list);
947 		gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
948 		list_del(&tr->tr_list);
949 		gfs2_trans_free(sdp, tr);
950 	}
951 	while (!list_empty(&sdp->sd_ail2_list)) {
952 		tr = list_first_entry(&sdp->sd_ail2_list, struct gfs2_trans,
953 				      tr_list);
954 		gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
955 		list_del(&tr->tr_list);
956 		gfs2_trans_free(sdp, tr);
957 	}
958 	gfs2_drain_revokes(sdp);
959 	spin_unlock(&sdp->sd_ail_lock);
960 }
961 
962 /**
963  * empty_ail1_list - try to start IO and empty the ail1 list
964  * @sdp: Pointer to GFS2 superblock
965  */
empty_ail1_list(struct gfs2_sbd * sdp)966 static void empty_ail1_list(struct gfs2_sbd *sdp)
967 {
968 	unsigned long start = jiffies;
969 	bool empty = false;
970 
971 	while (!empty) {
972 		if (time_after(jiffies, start + (HZ * 600))) {
973 			fs_err(sdp, "Error: In %s for 10 minutes! t=%d\n",
974 			       __func__, current->journal_info ? 1 : 0);
975 			dump_ail_list(sdp);
976 			return;
977 		}
978 		gfs2_ail1_start(sdp);
979 		gfs2_ail1_wait(sdp);
980 		empty = gfs2_ail1_empty(sdp, 0);
981 
982 		if (gfs2_withdrawn(sdp))
983 			break;
984 	}
985 }
986 
gfs2_trans_drain_list(struct gfs2_sbd * sdp,struct list_head * list)987 static void gfs2_trans_drain_list(struct gfs2_sbd *sdp, struct list_head *list)
988 {
989 	struct gfs2_bufdata *bd;
990 
991 	while (!list_empty(list)) {
992 		bd = list_first_entry(list, struct gfs2_bufdata, bd_list);
993 		struct buffer_head *bh = bd->bd_bh;
994 
995 		WARN_ON_ONCE(!buffer_pinned(bh));
996 		clear_buffer_pinned(bh);
997 		trace_gfs2_pin(bd, 0);
998 		atomic_dec(&sdp->sd_log_pinned);
999 		list_del_init(&bd->bd_list);
1000 		brelse(bh);
1001 	}
1002 }
1003 
1004 /**
1005  * gfs2_trans_drain - drain the buf and databuf queue for a failed transaction
1006  * @sdp: the filesystem
1007  * @tr: the transaction to drain
1008  *
1009  * When this is called, we're taking an error exit for a log write that failed
1010  * but since we bypassed the after_commit functions, we need to remove the
1011  * items from the buf and databuf queue.
1012  */
gfs2_trans_drain(struct gfs2_sbd * sdp,struct gfs2_trans * tr)1013 static void gfs2_trans_drain(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
1014 {
1015 	if (!tr)
1016 		return;
1017 	gfs2_trans_drain_list(sdp, &tr->tr_buf);
1018 	gfs2_trans_drain_list(sdp, &tr->tr_databuf);
1019 }
1020 
gfs2_remove_from_journal(struct buffer_head * bh,int meta)1021 void gfs2_remove_from_journal(struct buffer_head *bh, int meta)
1022 {
1023 	struct address_space *mapping = bh->b_folio->mapping;
1024 	struct gfs2_sbd *sdp = gfs2_mapping2sbd(mapping);
1025 	struct gfs2_bufdata *bd = bh->b_private;
1026 	struct gfs2_trans *tr = current->journal_info;
1027 	int was_pinned = 0;
1028 
1029 	if (test_clear_buffer_pinned(bh)) {
1030 		trace_gfs2_pin(bd, 0);
1031 		atomic_dec(&sdp->sd_log_pinned);
1032 		list_del_init(&bd->bd_list);
1033 		if (tr) {
1034 			if (meta == REMOVE_META)
1035 				tr->tr_num_buf_rm++;
1036 			else
1037 				tr->tr_num_databuf_rm++;
1038 			set_bit(TR_TOUCHED, &tr->tr_flags);
1039 		}
1040 		was_pinned = 1;
1041 		brelse(bh);
1042 	}
1043 	if (bd) {
1044 		if (bd->bd_tr) {
1045 			if (tr)
1046 				gfs2_trans_add_revoke(sdp, bd);
1047 			else
1048 				 gfs2_remove_from_ail(bd);
1049 		} else if (was_pinned) {
1050 			bh->b_private = NULL;
1051 			kmem_cache_free(gfs2_bufdata_cachep, bd);
1052 		} else if (!list_empty(&bd->bd_ail_st_list) &&
1053 			   !list_empty(&bd->bd_ail_gl_list)) {
1054 			gfs2_remove_from_ail(bd);
1055 		}
1056 	}
1057 	clear_buffer_dirty(bh);
1058 	clear_buffer_uptodate(bh);
1059 }
1060 
1061 /**
1062  * __gfs2_log_flush - flush incore transaction(s)
1063  * @sdp: The filesystem
1064  * @gl: The glock structure to flush.  If NULL, flush the whole incore log
1065  * @flags: The log header flags: GFS2_LOG_HEAD_FLUSH_* and debug flags
1066  *
1067  */
1068 
__gfs2_log_flush(struct gfs2_sbd * sdp,struct gfs2_glock * gl,u32 flags)1069 static void __gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl,
1070 			     u32 flags)
1071 {
1072 	struct gfs2_trans *tr = NULL;
1073 	unsigned int reserved_blocks = 0, used_blocks = 0;
1074 	bool frozen = test_bit(SDF_FROZEN, &sdp->sd_flags);
1075 	unsigned int first_log_head;
1076 	unsigned int reserved_revokes = 0;
1077 
1078 	trace_gfs2_log_flush(sdp, 1, flags);
1079 
1080 repeat:
1081 	/*
1082 	 * Do this check while holding the log_flush_lock to prevent new
1083 	 * buffers from being added to the ail via gfs2_pin()
1084 	 */
1085 	if (gfs2_withdrawn(sdp) ||
1086 	    !test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
1087 		goto out;
1088 
1089 	/* Log might have been flushed while we waited for the flush lock */
1090 	if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags))
1091 		goto out;
1092 
1093 	first_log_head = sdp->sd_log_head;
1094 	sdp->sd_log_flush_head = first_log_head;
1095 
1096 	tr = sdp->sd_log_tr;
1097 	if (tr || sdp->sd_log_num_revoke) {
1098 		if (reserved_blocks)
1099 			gfs2_log_release(sdp, reserved_blocks);
1100 		reserved_blocks = sdp->sd_log_blks_reserved;
1101 		reserved_revokes = sdp->sd_log_num_revoke;
1102 		if (tr) {
1103 			sdp->sd_log_tr = NULL;
1104 			tr->tr_first = first_log_head;
1105 			if (unlikely(frozen)) {
1106 				if (gfs2_assert_withdraw(sdp,
1107 				       !tr->tr_num_buf_new && !tr->tr_num_databuf_new))
1108 					goto out_withdraw;
1109 			}
1110 		}
1111 	} else if (!reserved_blocks) {
1112 		unsigned int taboo_blocks = GFS2_LOG_FLUSH_MIN_BLOCKS;
1113 
1114 		reserved_blocks = GFS2_LOG_FLUSH_MIN_BLOCKS;
1115 		if (current == sdp->sd_logd_process)
1116 			taboo_blocks = 0;
1117 
1118 		if (!__gfs2_log_try_reserve(sdp, reserved_blocks, taboo_blocks)) {
1119 			up_write(&sdp->sd_log_flush_lock);
1120 			__gfs2_log_reserve(sdp, reserved_blocks, taboo_blocks);
1121 			down_write(&sdp->sd_log_flush_lock);
1122 			goto repeat;
1123 		}
1124 		BUG_ON(sdp->sd_log_num_revoke);
1125 	}
1126 
1127 	if (flags & GFS2_LOG_HEAD_FLUSH_SHUTDOWN)
1128 		clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
1129 
1130 	if (unlikely(frozen))
1131 		if (gfs2_assert_withdraw(sdp, !reserved_revokes))
1132 			goto out_withdraw;
1133 
1134 	gfs2_ordered_write(sdp);
1135 	if (gfs2_withdrawn(sdp))
1136 		goto out_withdraw;
1137 	lops_before_commit(sdp, tr);
1138 	if (gfs2_withdrawn(sdp))
1139 		goto out_withdraw;
1140 	if (sdp->sd_jdesc)
1141 		gfs2_log_submit_write(&sdp->sd_jdesc->jd_log_bio);
1142 	if (gfs2_withdrawn(sdp))
1143 		goto out_withdraw;
1144 
1145 	if (sdp->sd_log_head != sdp->sd_log_flush_head) {
1146 		log_write_header(sdp, flags);
1147 	} else if (sdp->sd_log_tail != sdp->sd_log_flush_tail && !sdp->sd_log_idle) {
1148 		log_write_header(sdp, flags);
1149 	}
1150 	if (gfs2_withdrawn(sdp))
1151 		goto out_withdraw;
1152 	lops_after_commit(sdp, tr);
1153 
1154 	spin_lock(&sdp->sd_log_lock);
1155 	sdp->sd_log_blks_reserved = 0;
1156 
1157 	spin_lock(&sdp->sd_ail_lock);
1158 	if (tr && !list_empty(&tr->tr_ail1_list)) {
1159 		list_add(&tr->tr_list, &sdp->sd_ail1_list);
1160 		tr = NULL;
1161 	}
1162 	spin_unlock(&sdp->sd_ail_lock);
1163 	spin_unlock(&sdp->sd_log_lock);
1164 
1165 	if (!(flags & GFS2_LOG_HEAD_FLUSH_NORMAL)) {
1166 		if (!sdp->sd_log_idle) {
1167 			empty_ail1_list(sdp);
1168 			if (gfs2_withdrawn(sdp))
1169 				goto out_withdraw;
1170 			log_write_header(sdp, flags);
1171 		}
1172 		if (flags & (GFS2_LOG_HEAD_FLUSH_SHUTDOWN |
1173 			     GFS2_LOG_HEAD_FLUSH_FREEZE))
1174 			gfs2_log_shutdown(sdp);
1175 	}
1176 
1177 out_end:
1178 	used_blocks = log_distance(sdp, sdp->sd_log_flush_head, first_log_head);
1179 	reserved_revokes += atomic_read(&sdp->sd_log_revokes_available);
1180 	atomic_set(&sdp->sd_log_revokes_available, sdp->sd_ldptrs);
1181 	gfs2_assert_withdraw(sdp, reserved_revokes % sdp->sd_inptrs == sdp->sd_ldptrs);
1182 	if (reserved_revokes > sdp->sd_ldptrs)
1183 		reserved_blocks += (reserved_revokes - sdp->sd_ldptrs) / sdp->sd_inptrs;
1184 out:
1185 	if (used_blocks != reserved_blocks) {
1186 		gfs2_assert_withdraw(sdp, used_blocks < reserved_blocks);
1187 		gfs2_log_release(sdp, reserved_blocks - used_blocks);
1188 	}
1189 	gfs2_trans_free(sdp, tr);
1190 	trace_gfs2_log_flush(sdp, 0, flags);
1191 	return;
1192 
1193 out_withdraw:
1194 	if (sdp->sd_jdesc->jd_log_bio) {
1195 		bio_io_error(sdp->sd_jdesc->jd_log_bio);
1196 		sdp->sd_jdesc->jd_log_bio = NULL;
1197 	}
1198 	gfs2_trans_drain(sdp, tr);
1199 	/**
1200 	 * If the tr_list is empty, we're withdrawing during a log
1201 	 * flush that targets a transaction, but the transaction was
1202 	 * never queued onto any of the ail lists. Here we add it to
1203 	 * ail1 just so that ail_drain() will find and free it.
1204 	 */
1205 	spin_lock(&sdp->sd_ail_lock);
1206 	if (tr && list_empty(&tr->tr_list))
1207 		list_add(&tr->tr_list, &sdp->sd_ail1_list);
1208 	spin_unlock(&sdp->sd_ail_lock);
1209 	tr = NULL;
1210 	goto out_end;
1211 }
1212 
gfs2_log_flush(struct gfs2_sbd * sdp,struct gfs2_glock * gl,u32 flags)1213 void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
1214 {
1215 	down_write(&sdp->sd_log_flush_lock);
1216 	__gfs2_log_flush(sdp, gl, flags);
1217 	up_write(&sdp->sd_log_flush_lock);
1218 }
1219 
1220 /**
1221  * gfs2_merge_trans - Merge a new transaction into a cached transaction
1222  * @sdp: the filesystem
1223  * @new: New transaction to be merged
1224  */
1225 
gfs2_merge_trans(struct gfs2_sbd * sdp,struct gfs2_trans * new)1226 static void gfs2_merge_trans(struct gfs2_sbd *sdp, struct gfs2_trans *new)
1227 {
1228 	struct gfs2_trans *old = sdp->sd_log_tr;
1229 
1230 	WARN_ON_ONCE(!test_bit(TR_ATTACHED, &old->tr_flags));
1231 
1232 	old->tr_num_buf_new	+= new->tr_num_buf_new;
1233 	old->tr_num_databuf_new	+= new->tr_num_databuf_new;
1234 	old->tr_num_buf_rm	+= new->tr_num_buf_rm;
1235 	old->tr_num_databuf_rm	+= new->tr_num_databuf_rm;
1236 	old->tr_revokes		+= new->tr_revokes;
1237 	old->tr_num_revoke	+= new->tr_num_revoke;
1238 
1239 	list_splice_tail_init(&new->tr_databuf, &old->tr_databuf);
1240 	list_splice_tail_init(&new->tr_buf, &old->tr_buf);
1241 
1242 	spin_lock(&sdp->sd_ail_lock);
1243 	list_splice_tail_init(&new->tr_ail1_list, &old->tr_ail1_list);
1244 	list_splice_tail_init(&new->tr_ail2_list, &old->tr_ail2_list);
1245 	spin_unlock(&sdp->sd_ail_lock);
1246 }
1247 
log_refund(struct gfs2_sbd * sdp,struct gfs2_trans * tr)1248 static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
1249 {
1250 	unsigned int reserved;
1251 	unsigned int unused;
1252 	unsigned int maxres;
1253 
1254 	spin_lock(&sdp->sd_log_lock);
1255 
1256 	if (sdp->sd_log_tr) {
1257 		gfs2_merge_trans(sdp, tr);
1258 	} else if (tr->tr_num_buf_new || tr->tr_num_databuf_new) {
1259 		gfs2_assert_withdraw(sdp, !test_bit(TR_ONSTACK, &tr->tr_flags));
1260 		sdp->sd_log_tr = tr;
1261 		set_bit(TR_ATTACHED, &tr->tr_flags);
1262 	}
1263 
1264 	reserved = calc_reserved(sdp);
1265 	maxres = sdp->sd_log_blks_reserved + tr->tr_reserved;
1266 	gfs2_assert_withdraw(sdp, maxres >= reserved);
1267 	unused = maxres - reserved;
1268 	if (unused)
1269 		gfs2_log_release(sdp, unused);
1270 	sdp->sd_log_blks_reserved = reserved;
1271 
1272 	spin_unlock(&sdp->sd_log_lock);
1273 }
1274 
gfs2_jrnl_flush_reqd(struct gfs2_sbd * sdp)1275 static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
1276 {
1277 	return atomic_read(&sdp->sd_log_pinned) +
1278 	       atomic_read(&sdp->sd_log_blks_needed) >=
1279 	       atomic_read(&sdp->sd_log_thresh1);
1280 }
1281 
gfs2_ail_flush_reqd(struct gfs2_sbd * sdp)1282 static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
1283 {
1284 	return sdp->sd_jdesc->jd_blocks -
1285 	       atomic_read(&sdp->sd_log_blks_free) +
1286 	       atomic_read(&sdp->sd_log_blks_needed) >=
1287 	       atomic_read(&sdp->sd_log_thresh2);
1288 }
1289 
1290 /**
1291  * gfs2_log_commit - Commit a transaction to the log
1292  * @sdp: the filesystem
1293  * @tr: the transaction
1294  *
1295  * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
1296  * or the total number of used blocks (pinned blocks plus AIL blocks)
1297  * is greater than thresh2.
1298  *
1299  * At mount time thresh1 is 2/5ths of journal size, thresh2 is 4/5ths of
1300  * journal size.
1301  *
1302  * Returns: errno
1303  */
1304 
gfs2_log_commit(struct gfs2_sbd * sdp,struct gfs2_trans * tr)1305 void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
1306 {
1307 	log_refund(sdp, tr);
1308 
1309 	if (gfs2_ail_flush_reqd(sdp) || gfs2_jrnl_flush_reqd(sdp))
1310 		wake_up(&sdp->sd_logd_waitq);
1311 }
1312 
1313 /**
1314  * gfs2_log_shutdown - write a shutdown header into a journal
1315  * @sdp: the filesystem
1316  *
1317  */
1318 
gfs2_log_shutdown(struct gfs2_sbd * sdp)1319 static void gfs2_log_shutdown(struct gfs2_sbd *sdp)
1320 {
1321 	gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
1322 	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
1323 	gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
1324 
1325 	log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT | GFS2_LFC_SHUTDOWN);
1326 	log_pull_tail(sdp);
1327 
1328 	gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
1329 	gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
1330 }
1331 
1332 /**
1333  * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
1334  * @data: Pointer to GFS2 superblock
1335  *
1336  * Also, periodically check to make sure that we're using the most recent
1337  * journal index.
1338  */
1339 
gfs2_logd(void * data)1340 int gfs2_logd(void *data)
1341 {
1342 	struct gfs2_sbd *sdp = data;
1343 	unsigned long t = 1;
1344 
1345 	set_freezable();
1346 	while (!kthread_should_stop()) {
1347 		if (gfs2_withdrawn(sdp))
1348 			break;
1349 
1350 		if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
1351 			down_write(&sdp->sd_log_flush_lock);
1352 			gfs2_ail1_empty(sdp, 0);
1353 			__gfs2_log_flush(sdp, NULL,
1354 					 GFS2_LOG_HEAD_FLUSH_NORMAL |
1355 					 GFS2_LFC_LOGD_JFLUSH_REQD);
1356 			up_write(&sdp->sd_log_flush_lock);
1357 		}
1358 
1359 		if (test_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags) ||
1360 		    gfs2_ail_flush_reqd(sdp)) {
1361 			clear_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags);
1362 			down_write(&sdp->sd_log_flush_lock);
1363 			gfs2_ail1_start(sdp);
1364 			gfs2_ail1_wait(sdp);
1365 			gfs2_ail1_empty(sdp, 0);
1366 			__gfs2_log_flush(sdp, NULL,
1367 					 GFS2_LOG_HEAD_FLUSH_NORMAL |
1368 					 GFS2_LFC_LOGD_AIL_FLUSH_REQD);
1369 			up_write(&sdp->sd_log_flush_lock);
1370 		}
1371 
1372 		t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
1373 
1374 		t = wait_event_freezable_timeout(sdp->sd_logd_waitq,
1375 				test_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags) ||
1376 				gfs2_ail_flush_reqd(sdp) ||
1377 				gfs2_jrnl_flush_reqd(sdp) ||
1378 				gfs2_withdrawn(sdp) ||
1379 				kthread_should_stop(),
1380 				t);
1381 	}
1382 
1383 	return 0;
1384 }
1385 
1386