Commit | Line | Data |
---|---|---|
f5166768 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
470decc6 | 2 | /* |
58862699 | 3 | * linux/fs/jbd2/transaction.c |
470decc6 DK |
4 | * |
5 | * Written by Stephen C. Tweedie <sct@redhat.com>, 1998 | |
6 | * | |
7 | * Copyright 1998 Red Hat corp --- All Rights Reserved | |
8 | * | |
470decc6 DK |
9 | * Generic filesystem transaction handling code; part of the ext2fs |
10 | * journaling system. | |
11 | * | |
12 | * This file manages transactions (compound commits managed by the | |
13 | * journaling code) and handles (individual atomic operations by the | |
14 | * filesystem). | |
15 | */ | |
16 | ||
17 | #include <linux/time.h> | |
18 | #include <linux/fs.h> | |
f7f4bccb | 19 | #include <linux/jbd2.h> |
470decc6 DK |
20 | #include <linux/errno.h> |
21 | #include <linux/slab.h> | |
22 | #include <linux/timer.h> | |
470decc6 DK |
23 | #include <linux/mm.h> |
24 | #include <linux/highmem.h> | |
e07f7183 | 25 | #include <linux/hrtimer.h> |
47def826 | 26 | #include <linux/backing-dev.h> |
44705754 | 27 | #include <linux/bug.h> |
47def826 | 28 | #include <linux/module.h> |
81378da6 | 29 | #include <linux/sched/mm.h> |
470decc6 | 30 | |
343d9c28 TT |
31 | #include <trace/events/jbd2.h> |
32 | ||
7ddae860 | 33 | static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh); |
de1b7941 | 34 | static void __jbd2_journal_unfile_buffer(struct journal_head *jh); |
7ddae860 | 35 | |
0c2022ec YY |
36 | static struct kmem_cache *transaction_cache; |
37 | int __init jbd2_journal_init_transaction_cache(void) | |
38 | { | |
39 | J_ASSERT(!transaction_cache); | |
40 | transaction_cache = kmem_cache_create("jbd2_transaction_s", | |
41 | sizeof(transaction_t), | |
42 | 0, | |
43 | SLAB_HWCACHE_ALIGN|SLAB_TEMPORARY, | |
44 | NULL); | |
45 | if (transaction_cache) | |
46 | return 0; | |
47 | return -ENOMEM; | |
48 | } | |
49 | ||
50 | void jbd2_journal_destroy_transaction_cache(void) | |
51 | { | |
52 | if (transaction_cache) { | |
53 | kmem_cache_destroy(transaction_cache); | |
54 | transaction_cache = NULL; | |
55 | } | |
56 | } | |
57 | ||
58 | void jbd2_journal_free_transaction(transaction_t *transaction) | |
59 | { | |
60 | if (unlikely(ZERO_OR_NULL_PTR(transaction))) | |
61 | return; | |
62 | kmem_cache_free(transaction_cache, transaction); | |
63 | } | |
64 | ||
470decc6 | 65 | /* |
f7f4bccb | 66 | * jbd2_get_transaction: obtain a new transaction_t object. |
470decc6 DK |
67 | * |
68 | * Simply allocate and initialise a new transaction. Create it in | |
69 | * RUNNING state and add it to the current journal (which should not | |
70 | * have an existing running transaction: we only make a new transaction | |
71 | * once we have started to commit the old one). | |
72 | * | |
73 | * Preconditions: | |
74 | * The journal MUST be locked. We don't perform atomic mallocs on the | |
75 | * new transaction and we can't block without protecting against other | |
76 | * processes trying to touch the journal while it is in transition. | |
77 | * | |
470decc6 DK |
78 | */ |
79 | ||
80 | static transaction_t * | |
f7f4bccb | 81 | jbd2_get_transaction(journal_t *journal, transaction_t *transaction) |
470decc6 DK |
82 | { |
83 | transaction->t_journal = journal; | |
84 | transaction->t_state = T_RUNNING; | |
e07f7183 | 85 | transaction->t_start_time = ktime_get(); |
470decc6 DK |
86 | transaction->t_tid = journal->j_transaction_sequence++; |
87 | transaction->t_expires = jiffies + journal->j_commit_interval; | |
88 | spin_lock_init(&transaction->t_handle_lock); | |
a51dca9c | 89 | atomic_set(&transaction->t_updates, 0); |
8f7d89f3 JK |
90 | atomic_set(&transaction->t_outstanding_credits, |
91 | atomic_read(&journal->j_reserved_credits)); | |
8dd42046 | 92 | atomic_set(&transaction->t_handle_count, 0); |
c851ed54 | 93 | INIT_LIST_HEAD(&transaction->t_inode_list); |
3e624fc7 | 94 | INIT_LIST_HEAD(&transaction->t_private_list); |
470decc6 DK |
95 | |
96 | /* Set up the commit timer for the new transaction. */ | |
b1f485f2 | 97 | journal->j_commit_timer.expires = round_jiffies_up(transaction->t_expires); |
470decc6 DK |
98 | add_timer(&journal->j_commit_timer); |
99 | ||
100 | J_ASSERT(journal->j_running_transaction == NULL); | |
101 | journal->j_running_transaction = transaction; | |
8e85fb3f JL |
102 | transaction->t_max_wait = 0; |
103 | transaction->t_start = jiffies; | |
9fff24aa | 104 | transaction->t_requested = 0; |
470decc6 DK |
105 | |
106 | return transaction; | |
107 | } | |
108 | ||
109 | /* | |
110 | * Handle management. | |
111 | * | |
112 | * A handle_t is an object which represents a single atomic update to a | |
113 | * filesystem, and which tracks all of the modifications which form part | |
114 | * of that one update. | |
115 | */ | |
116 | ||
6d0bf005 | 117 | /* |
28e35e42 | 118 | * Update transaction's maximum wait time, if debugging is enabled. |
6d0bf005 TT |
119 | * |
120 | * In order for t_max_wait to be reliable, it must be protected by a | |
121 | * lock. But doing so will mean that start_this_handle() can not be | |
122 | * run in parallel on SMP systems, which limits our scalability. So | |
123 | * unless debugging is enabled, we no longer update t_max_wait, which | |
124 | * means that maximum wait time reported by the jbd2_run_stats | |
125 | * tracepoint will always be zero. | |
126 | */ | |
28e35e42 TM |
127 | static inline void update_t_max_wait(transaction_t *transaction, |
128 | unsigned long ts) | |
6d0bf005 TT |
129 | { |
130 | #ifdef CONFIG_JBD2_DEBUG | |
6d0bf005 TT |
131 | if (jbd2_journal_enable_debug && |
132 | time_after(transaction->t_start, ts)) { | |
133 | ts = jbd2_time_diff(ts, transaction->t_start); | |
134 | spin_lock(&transaction->t_handle_lock); | |
135 | if (ts > transaction->t_max_wait) | |
136 | transaction->t_max_wait = ts; | |
137 | spin_unlock(&transaction->t_handle_lock); | |
138 | } | |
139 | #endif | |
140 | } | |
141 | ||
8f7d89f3 JK |
142 | /* |
143 | * Wait until running transaction passes T_LOCKED state. Also starts the commit | |
144 | * if needed. The function expects running transaction to exist and releases | |
145 | * j_state_lock. | |
146 | */ | |
147 | static void wait_transaction_locked(journal_t *journal) | |
148 | __releases(journal->j_state_lock) | |
149 | { | |
150 | DEFINE_WAIT(wait); | |
151 | int need_to_start; | |
152 | tid_t tid = journal->j_running_transaction->t_tid; | |
153 | ||
154 | prepare_to_wait(&journal->j_wait_transaction_locked, &wait, | |
155 | TASK_UNINTERRUPTIBLE); | |
156 | need_to_start = !tid_geq(journal->j_commit_request, tid); | |
157 | read_unlock(&journal->j_state_lock); | |
158 | if (need_to_start) | |
159 | jbd2_log_start_commit(journal, tid); | |
e03a9976 | 160 | jbd2_might_wait_for_commit(journal); |
8f7d89f3 JK |
161 | schedule(); |
162 | finish_wait(&journal->j_wait_transaction_locked, &wait); | |
163 | } | |
164 | ||
165 | static void sub_reserved_credits(journal_t *journal, int blocks) | |
166 | { | |
167 | atomic_sub(blocks, &journal->j_reserved_credits); | |
168 | wake_up(&journal->j_wait_reserved); | |
169 | } | |
170 | ||
171 | /* | |
172 | * Wait until we can add credits for handle to the running transaction. Called | |
173 | * with j_state_lock held for reading. Returns 0 if handle joined the running | |
174 | * transaction. Returns 1 if we had to wait, j_state_lock is dropped, and | |
175 | * caller must retry. | |
176 | */ | |
177 | static int add_transaction_credits(journal_t *journal, int blocks, | |
178 | int rsv_blocks) | |
179 | { | |
180 | transaction_t *t = journal->j_running_transaction; | |
181 | int needed; | |
182 | int total = blocks + rsv_blocks; | |
183 | ||
184 | /* | |
185 | * If the current transaction is locked down for commit, wait | |
186 | * for the lock to be released. | |
187 | */ | |
188 | if (t->t_state == T_LOCKED) { | |
189 | wait_transaction_locked(journal); | |
190 | return 1; | |
191 | } | |
192 | ||
193 | /* | |
194 | * If there is not enough space left in the log to write all | |
195 | * potential buffers requested by this operation, we need to | |
196 | * stall pending a log checkpoint to free some more log space. | |
197 | */ | |
198 | needed = atomic_add_return(total, &t->t_outstanding_credits); | |
199 | if (needed > journal->j_max_transaction_buffers) { | |
200 | /* | |
201 | * If the current transaction is already too large, | |
202 | * then start to commit it: we can then go back and | |
203 | * attach this handle to a new transaction. | |
204 | */ | |
205 | atomic_sub(total, &t->t_outstanding_credits); | |
6d3ec14d LC |
206 | |
207 | /* | |
208 | * Is the number of reserved credits in the current transaction too | |
209 | * big to fit this handle? Wait until reserved credits are freed. | |
210 | */ | |
211 | if (atomic_read(&journal->j_reserved_credits) + total > | |
212 | journal->j_max_transaction_buffers) { | |
213 | read_unlock(&journal->j_state_lock); | |
e03a9976 | 214 | jbd2_might_wait_for_commit(journal); |
6d3ec14d LC |
215 | wait_event(journal->j_wait_reserved, |
216 | atomic_read(&journal->j_reserved_credits) + total <= | |
217 | journal->j_max_transaction_buffers); | |
218 | return 1; | |
219 | } | |
220 | ||
8f7d89f3 JK |
221 | wait_transaction_locked(journal); |
222 | return 1; | |
223 | } | |
224 | ||
225 | /* | |
226 | * The commit code assumes that it can get enough log space | |
227 | * without forcing a checkpoint. This is *critical* for | |
228 | * correctness: a checkpoint of a buffer which is also | |
229 | * associated with a committing transaction creates a deadlock, | |
230 | * so commit simply cannot force through checkpoints. | |
231 | * | |
232 | * We must therefore ensure the necessary space in the journal | |
233 | * *before* starting to dirty potentially checkpointed buffers | |
234 | * in the new transaction. | |
235 | */ | |
236 | if (jbd2_log_space_left(journal) < jbd2_space_needed(journal)) { | |
237 | atomic_sub(total, &t->t_outstanding_credits); | |
238 | read_unlock(&journal->j_state_lock); | |
e03a9976 | 239 | jbd2_might_wait_for_commit(journal); |
8f7d89f3 JK |
240 | write_lock(&journal->j_state_lock); |
241 | if (jbd2_log_space_left(journal) < jbd2_space_needed(journal)) | |
242 | __jbd2_log_wait_for_space(journal); | |
243 | write_unlock(&journal->j_state_lock); | |
244 | return 1; | |
245 | } | |
246 | ||
247 | /* No reservation? We are done... */ | |
248 | if (!rsv_blocks) | |
249 | return 0; | |
250 | ||
251 | needed = atomic_add_return(rsv_blocks, &journal->j_reserved_credits); | |
252 | /* We allow at most half of a transaction to be reserved */ | |
253 | if (needed > journal->j_max_transaction_buffers / 2) { | |
254 | sub_reserved_credits(journal, rsv_blocks); | |
255 | atomic_sub(total, &t->t_outstanding_credits); | |
256 | read_unlock(&journal->j_state_lock); | |
e03a9976 | 257 | jbd2_might_wait_for_commit(journal); |
8f7d89f3 JK |
258 | wait_event(journal->j_wait_reserved, |
259 | atomic_read(&journal->j_reserved_credits) + rsv_blocks | |
260 | <= journal->j_max_transaction_buffers / 2); | |
261 | return 1; | |
262 | } | |
263 | return 0; | |
264 | } | |
265 | ||
470decc6 DK |
266 | /* |
267 | * start_this_handle: Given a handle, deal with any locking or stalling | |
268 | * needed to make sure that there is enough journal space for the handle | |
269 | * to begin. Attach the handle to a transaction and set up the | |
270 | * transaction's buffer credits. | |
271 | */ | |
272 | ||
47def826 | 273 | static int start_this_handle(journal_t *journal, handle_t *handle, |
d2159fb7 | 274 | gfp_t gfp_mask) |
470decc6 | 275 | { |
e4471831 | 276 | transaction_t *transaction, *new_transaction = NULL; |
8f7d89f3 JK |
277 | int blocks = handle->h_buffer_credits; |
278 | int rsv_blocks = 0; | |
28e35e42 | 279 | unsigned long ts = jiffies; |
470decc6 | 280 | |
6d3ec14d LC |
281 | if (handle->h_rsv_handle) |
282 | rsv_blocks = handle->h_rsv_handle->h_buffer_credits; | |
283 | ||
8f7d89f3 | 284 | /* |
6d3ec14d LC |
285 | * Limit the number of reserved credits to 1/2 of maximum transaction |
286 | * size and limit the number of total credits to not exceed maximum | |
287 | * transaction size per operation. | |
8f7d89f3 | 288 | */ |
6d3ec14d LC |
289 | if ((rsv_blocks > journal->j_max_transaction_buffers / 2) || |
290 | (rsv_blocks + blocks > journal->j_max_transaction_buffers)) { | |
291 | printk(KERN_ERR "JBD2: %s wants too many credits " | |
292 | "credits:%d rsv_credits:%d max:%d\n", | |
293 | current->comm, blocks, rsv_blocks, | |
294 | journal->j_max_transaction_buffers); | |
295 | WARN_ON(1); | |
47def826 | 296 | return -ENOSPC; |
470decc6 DK |
297 | } |
298 | ||
299 | alloc_transaction: | |
300 | if (!journal->j_running_transaction) { | |
6ccaf3e2 MH |
301 | /* |
302 | * If __GFP_FS is not present, then we may be being called from | |
303 | * inside the fs writeback layer, so we MUST NOT fail. | |
304 | */ | |
305 | if ((gfp_mask & __GFP_FS) == 0) | |
306 | gfp_mask |= __GFP_NOFAIL; | |
b2f4edb3 WG |
307 | new_transaction = kmem_cache_zalloc(transaction_cache, |
308 | gfp_mask); | |
6ccaf3e2 | 309 | if (!new_transaction) |
47def826 | 310 | return -ENOMEM; |
470decc6 DK |
311 | } |
312 | ||
313 | jbd_debug(3, "New handle %p going live.\n", handle); | |
314 | ||
470decc6 DK |
315 | /* |
316 | * We need to hold j_state_lock until t_updates has been incremented, | |
317 | * for proper journal barrier handling | |
318 | */ | |
a931da6a TT |
319 | repeat: |
320 | read_lock(&journal->j_state_lock); | |
5c2178e7 | 321 | BUG_ON(journal->j_flags & JBD2_UNMOUNT); |
470decc6 | 322 | if (is_journal_aborted(journal) || |
f7f4bccb | 323 | (journal->j_errno != 0 && !(journal->j_flags & JBD2_ACK_ERR))) { |
a931da6a | 324 | read_unlock(&journal->j_state_lock); |
0c2022ec | 325 | jbd2_journal_free_transaction(new_transaction); |
47def826 | 326 | return -EROFS; |
470decc6 DK |
327 | } |
328 | ||
8f7d89f3 JK |
329 | /* |
330 | * Wait on the journal's transaction barrier if necessary. Specifically | |
331 | * we allow reserved handles to proceed because otherwise commit could | |
332 | * deadlock on page writeback not being able to complete. | |
333 | */ | |
334 | if (!handle->h_reserved && journal->j_barrier_count) { | |
a931da6a | 335 | read_unlock(&journal->j_state_lock); |
470decc6 DK |
336 | wait_event(journal->j_wait_transaction_locked, |
337 | journal->j_barrier_count == 0); | |
338 | goto repeat; | |
339 | } | |
340 | ||
341 | if (!journal->j_running_transaction) { | |
a931da6a TT |
342 | read_unlock(&journal->j_state_lock); |
343 | if (!new_transaction) | |
470decc6 | 344 | goto alloc_transaction; |
a931da6a | 345 | write_lock(&journal->j_state_lock); |
d7961c7f | 346 | if (!journal->j_running_transaction && |
8f7d89f3 | 347 | (handle->h_reserved || !journal->j_barrier_count)) { |
a931da6a TT |
348 | jbd2_get_transaction(journal, new_transaction); |
349 | new_transaction = NULL; | |
470decc6 | 350 | } |
a931da6a TT |
351 | write_unlock(&journal->j_state_lock); |
352 | goto repeat; | |
470decc6 DK |
353 | } |
354 | ||
355 | transaction = journal->j_running_transaction; | |
356 | ||
8f7d89f3 JK |
357 | if (!handle->h_reserved) { |
358 | /* We may have dropped j_state_lock - restart in that case */ | |
359 | if (add_transaction_credits(journal, blocks, rsv_blocks)) | |
360 | goto repeat; | |
361 | } else { | |
470decc6 | 362 | /* |
8f7d89f3 JK |
363 | * We have handle reserved so we are allowed to join T_LOCKED |
364 | * transaction and we don't have to check for transaction size | |
365 | * and journal space. | |
470decc6 | 366 | */ |
8f7d89f3 JK |
367 | sub_reserved_credits(journal, blocks); |
368 | handle->h_reserved = 0; | |
470decc6 DK |
369 | } |
370 | ||
371 | /* OK, account for the buffers that this operation expects to | |
8dd42046 | 372 | * use and add the handle to the running transaction. |
8dd42046 | 373 | */ |
28e35e42 | 374 | update_t_max_wait(transaction, ts); |
470decc6 | 375 | handle->h_transaction = transaction; |
8f7d89f3 | 376 | handle->h_requested_credits = blocks; |
343d9c28 | 377 | handle->h_start_jiffies = jiffies; |
a51dca9c | 378 | atomic_inc(&transaction->t_updates); |
8dd42046 | 379 | atomic_inc(&transaction->t_handle_count); |
8f7d89f3 JK |
380 | jbd_debug(4, "Handle %p given %d credits (total %d, free %lu)\n", |
381 | handle, blocks, | |
a51dca9c | 382 | atomic_read(&transaction->t_outstanding_credits), |
76c39904 | 383 | jbd2_log_space_left(journal)); |
a931da6a | 384 | read_unlock(&journal->j_state_lock); |
41a5b913 | 385 | current->journal_info = handle; |
9599b0e5 | 386 | |
ab714aff | 387 | rwsem_acquire_read(&journal->j_trans_commit_map, 0, 0, _THIS_IP_); |
0c2022ec | 388 | jbd2_journal_free_transaction(new_transaction); |
81378da6 MH |
389 | /* |
390 | * Ensure that no allocations done while the transaction is open are | |
391 | * going to recurse back to the fs layer. | |
392 | */ | |
393 | handle->saved_alloc_context = memalloc_nofs_save(); | |
47def826 | 394 | return 0; |
470decc6 DK |
395 | } |
396 | ||
397 | /* Allocate a new handle. This should probably be in a slab... */ | |
398 | static handle_t *new_handle(int nblocks) | |
399 | { | |
af1e76d6 | 400 | handle_t *handle = jbd2_alloc_handle(GFP_NOFS); |
470decc6 DK |
401 | if (!handle) |
402 | return NULL; | |
470decc6 DK |
403 | handle->h_buffer_credits = nblocks; |
404 | handle->h_ref = 1; | |
405 | ||
406 | return handle; | |
407 | } | |
408 | ||
8f7d89f3 JK |
409 | handle_t *jbd2__journal_start(journal_t *journal, int nblocks, int rsv_blocks, |
410 | gfp_t gfp_mask, unsigned int type, | |
411 | unsigned int line_no) | |
470decc6 DK |
412 | { |
413 | handle_t *handle = journal_current_handle(); | |
414 | int err; | |
415 | ||
416 | if (!journal) | |
417 | return ERR_PTR(-EROFS); | |
418 | ||
419 | if (handle) { | |
420 | J_ASSERT(handle->h_transaction->t_journal == journal); | |
421 | handle->h_ref++; | |
422 | return handle; | |
423 | } | |
424 | ||
425 | handle = new_handle(nblocks); | |
426 | if (!handle) | |
427 | return ERR_PTR(-ENOMEM); | |
8f7d89f3 JK |
428 | if (rsv_blocks) { |
429 | handle_t *rsv_handle; | |
430 | ||
431 | rsv_handle = new_handle(rsv_blocks); | |
432 | if (!rsv_handle) { | |
433 | jbd2_free_handle(handle); | |
434 | return ERR_PTR(-ENOMEM); | |
435 | } | |
436 | rsv_handle->h_reserved = 1; | |
437 | rsv_handle->h_journal = journal; | |
438 | handle->h_rsv_handle = rsv_handle; | |
439 | } | |
470decc6 | 440 | |
47def826 | 441 | err = start_this_handle(journal, handle, gfp_mask); |
470decc6 | 442 | if (err < 0) { |
8f7d89f3 JK |
443 | if (handle->h_rsv_handle) |
444 | jbd2_free_handle(handle->h_rsv_handle); | |
af1e76d6 | 445 | jbd2_free_handle(handle); |
df05c1b8 | 446 | return ERR_PTR(err); |
470decc6 | 447 | } |
343d9c28 TT |
448 | handle->h_type = type; |
449 | handle->h_line_no = line_no; | |
450 | trace_jbd2_handle_start(journal->j_fs_dev->bd_dev, | |
451 | handle->h_transaction->t_tid, type, | |
452 | line_no, nblocks); | |
81378da6 | 453 | |
470decc6 DK |
454 | return handle; |
455 | } | |
47def826 TT |
456 | EXPORT_SYMBOL(jbd2__journal_start); |
457 | ||
458 | ||
91e4775d MCC |
459 | /** |
460 | * handle_t *jbd2_journal_start() - Obtain a new handle. | |
461 | * @journal: Journal to start transaction on. | |
462 | * @nblocks: number of block buffer we might modify | |
463 | * | |
464 | * We make sure that the transaction can guarantee at least nblocks of | |
465 | * modified buffers in the log. We block until the log can guarantee | |
466 | * that much space. Additionally, if rsv_blocks > 0, we also create another | |
467 | * handle with rsv_blocks reserved blocks in the journal. This handle is | |
468 | * is stored in h_rsv_handle. It is not attached to any particular transaction | |
469 | * and thus doesn't block transaction commit. If the caller uses this reserved | |
470 | * handle, it has to set h_rsv_handle to NULL as otherwise jbd2_journal_stop() | |
471 | * on the parent handle will dispose the reserved one. Reserved handle has to | |
472 | * be converted to a normal handle using jbd2_journal_start_reserved() before | |
473 | * it can be used. | |
474 | * | |
475 | * Return a pointer to a newly allocated handle, or an ERR_PTR() value | |
476 | * on failure. | |
477 | */ | |
47def826 TT |
478 | handle_t *jbd2_journal_start(journal_t *journal, int nblocks) |
479 | { | |
8f7d89f3 | 480 | return jbd2__journal_start(journal, nblocks, 0, GFP_NOFS, 0, 0); |
47def826 TT |
481 | } |
482 | EXPORT_SYMBOL(jbd2_journal_start); | |
483 | ||
8f7d89f3 JK |
484 | void jbd2_journal_free_reserved(handle_t *handle) |
485 | { | |
486 | journal_t *journal = handle->h_journal; | |
487 | ||
488 | WARN_ON(!handle->h_reserved); | |
489 | sub_reserved_credits(journal, handle->h_buffer_credits); | |
490 | jbd2_free_handle(handle); | |
491 | } | |
492 | EXPORT_SYMBOL(jbd2_journal_free_reserved); | |
493 | ||
494 | /** | |
495 | * int jbd2_journal_start_reserved(handle_t *handle) - start reserved handle | |
496 | * @handle: handle to start | |
497 | * | |
498 | * Start handle that has been previously reserved with jbd2_journal_reserve(). | |
499 | * This attaches @handle to the running transaction (or creates one if there's | |
500 | * not transaction running). Unlike jbd2_journal_start() this function cannot | |
501 | * block on journal commit, checkpointing, or similar stuff. It can block on | |
502 | * memory allocation or frozen journal though. | |
503 | * | |
504 | * Return 0 on success, non-zero on error - handle is freed in that case. | |
505 | */ | |
506 | int jbd2_journal_start_reserved(handle_t *handle, unsigned int type, | |
507 | unsigned int line_no) | |
508 | { | |
509 | journal_t *journal = handle->h_journal; | |
510 | int ret = -EIO; | |
511 | ||
512 | if (WARN_ON(!handle->h_reserved)) { | |
513 | /* Someone passed in normal handle? Just stop it. */ | |
514 | jbd2_journal_stop(handle); | |
515 | return ret; | |
516 | } | |
517 | /* | |
518 | * Usefulness of mixing of reserved and unreserved handles is | |
519 | * questionable. So far nobody seems to need it so just error out. | |
520 | */ | |
521 | if (WARN_ON(current->journal_info)) { | |
522 | jbd2_journal_free_reserved(handle); | |
523 | return ret; | |
524 | } | |
525 | ||
526 | handle->h_journal = NULL; | |
8f7d89f3 JK |
527 | /* |
528 | * GFP_NOFS is here because callers are likely from writeback or | |
529 | * similarly constrained call sites | |
530 | */ | |
531 | ret = start_this_handle(journal, handle, GFP_NOFS); | |
92e3b405 | 532 | if (ret < 0) { |
8f7d89f3 | 533 | jbd2_journal_free_reserved(handle); |
92e3b405 DC |
534 | return ret; |
535 | } | |
8f7d89f3 JK |
536 | handle->h_type = type; |
537 | handle->h_line_no = line_no; | |
92e3b405 | 538 | return 0; |
8f7d89f3 JK |
539 | } |
540 | EXPORT_SYMBOL(jbd2_journal_start_reserved); | |
470decc6 DK |
541 | |
542 | /** | |
f7f4bccb | 543 | * int jbd2_journal_extend() - extend buffer credits. |
470decc6 DK |
544 | * @handle: handle to 'extend' |
545 | * @nblocks: nr blocks to try to extend by. | |
546 | * | |
547 | * Some transactions, such as large extends and truncates, can be done | |
548 | * atomically all at once or in several stages. The operation requests | |
bd7ced98 | 549 | * a credit for a number of buffer modifications in advance, but can |
470decc6 DK |
550 | * extend its credit if it needs more. |
551 | * | |
f7f4bccb | 552 | * jbd2_journal_extend tries to give the running handle more buffer credits. |
470decc6 DK |
553 | * It does not guarantee that allocation - this is a best-effort only. |
554 | * The calling process MUST be able to deal cleanly with a failure to | |
555 | * extend here. | |
556 | * | |
557 | * Return 0 on success, non-zero on failure. | |
558 | * | |
559 | * return code < 0 implies an error | |
560 | * return code > 0 implies normal transaction-full status. | |
561 | */ | |
f7f4bccb | 562 | int jbd2_journal_extend(handle_t *handle, int nblocks) |
470decc6 DK |
563 | { |
564 | transaction_t *transaction = handle->h_transaction; | |
41a5b913 | 565 | journal_t *journal; |
470decc6 DK |
566 | int result; |
567 | int wanted; | |
568 | ||
470decc6 | 569 | if (is_handle_aborted(handle)) |
41a5b913 TT |
570 | return -EROFS; |
571 | journal = transaction->t_journal; | |
470decc6 DK |
572 | |
573 | result = 1; | |
574 | ||
a931da6a | 575 | read_lock(&journal->j_state_lock); |
470decc6 DK |
576 | |
577 | /* Don't extend a locked-down transaction! */ | |
41a5b913 | 578 | if (transaction->t_state != T_RUNNING) { |
470decc6 DK |
579 | jbd_debug(3, "denied handle %p %d blocks: " |
580 | "transaction not running\n", handle, nblocks); | |
581 | goto error_out; | |
582 | } | |
583 | ||
584 | spin_lock(&transaction->t_handle_lock); | |
fe1e8db5 JK |
585 | wanted = atomic_add_return(nblocks, |
586 | &transaction->t_outstanding_credits); | |
470decc6 DK |
587 | |
588 | if (wanted > journal->j_max_transaction_buffers) { | |
589 | jbd_debug(3, "denied handle %p %d blocks: " | |
590 | "transaction too large\n", handle, nblocks); | |
fe1e8db5 | 591 | atomic_sub(nblocks, &transaction->t_outstanding_credits); |
470decc6 DK |
592 | goto unlock; |
593 | } | |
594 | ||
76c39904 JK |
595 | if (wanted + (wanted >> JBD2_CONTROL_BLOCKS_SHIFT) > |
596 | jbd2_log_space_left(journal)) { | |
470decc6 DK |
597 | jbd_debug(3, "denied handle %p %d blocks: " |
598 | "insufficient log space\n", handle, nblocks); | |
fe1e8db5 | 599 | atomic_sub(nblocks, &transaction->t_outstanding_credits); |
470decc6 DK |
600 | goto unlock; |
601 | } | |
602 | ||
343d9c28 | 603 | trace_jbd2_handle_extend(journal->j_fs_dev->bd_dev, |
41a5b913 | 604 | transaction->t_tid, |
343d9c28 TT |
605 | handle->h_type, handle->h_line_no, |
606 | handle->h_buffer_credits, | |
607 | nblocks); | |
608 | ||
470decc6 | 609 | handle->h_buffer_credits += nblocks; |
343d9c28 | 610 | handle->h_requested_credits += nblocks; |
470decc6 DK |
611 | result = 0; |
612 | ||
613 | jbd_debug(3, "extended handle %p by %d\n", handle, nblocks); | |
614 | unlock: | |
615 | spin_unlock(&transaction->t_handle_lock); | |
616 | error_out: | |
a931da6a | 617 | read_unlock(&journal->j_state_lock); |
470decc6 DK |
618 | return result; |
619 | } | |
620 | ||
621 | ||
622 | /** | |
f7f4bccb | 623 | * int jbd2_journal_restart() - restart a handle . |
470decc6 DK |
624 | * @handle: handle to restart |
625 | * @nblocks: nr credits requested | |
626 | * | |
627 | * Restart a handle for a multi-transaction filesystem | |
628 | * operation. | |
629 | * | |
f7f4bccb MC |
630 | * If the jbd2_journal_extend() call above fails to grant new buffer credits |
631 | * to a running handle, a call to jbd2_journal_restart will commit the | |
470decc6 | 632 | * handle's transaction so far and reattach the handle to a new |
bd7ced98 | 633 | * transaction capable of guaranteeing the requested number of |
8f7d89f3 JK |
634 | * credits. We preserve reserved handle if there's any attached to the |
635 | * passed in handle. | |
470decc6 | 636 | */ |
d2159fb7 | 637 | int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask) |
470decc6 DK |
638 | { |
639 | transaction_t *transaction = handle->h_transaction; | |
41a5b913 | 640 | journal_t *journal; |
e4471831 TT |
641 | tid_t tid; |
642 | int need_to_start, ret; | |
470decc6 DK |
643 | |
644 | /* If we've had an abort of any type, don't even think about | |
645 | * actually doing the restart! */ | |
646 | if (is_handle_aborted(handle)) | |
647 | return 0; | |
41a5b913 | 648 | journal = transaction->t_journal; |
470decc6 DK |
649 | |
650 | /* | |
651 | * First unlink the handle from its current transaction, and start the | |
652 | * commit on that. | |
653 | */ | |
a51dca9c | 654 | J_ASSERT(atomic_read(&transaction->t_updates) > 0); |
470decc6 DK |
655 | J_ASSERT(journal_current_handle() == handle); |
656 | ||
a931da6a | 657 | read_lock(&journal->j_state_lock); |
470decc6 | 658 | spin_lock(&transaction->t_handle_lock); |
a51dca9c TT |
659 | atomic_sub(handle->h_buffer_credits, |
660 | &transaction->t_outstanding_credits); | |
8f7d89f3 JK |
661 | if (handle->h_rsv_handle) { |
662 | sub_reserved_credits(journal, | |
663 | handle->h_rsv_handle->h_buffer_credits); | |
664 | } | |
a51dca9c | 665 | if (atomic_dec_and_test(&transaction->t_updates)) |
470decc6 | 666 | wake_up(&journal->j_wait_updates); |
39c04153 | 667 | tid = transaction->t_tid; |
470decc6 | 668 | spin_unlock(&transaction->t_handle_lock); |
41a5b913 TT |
669 | handle->h_transaction = NULL; |
670 | current->journal_info = NULL; | |
470decc6 DK |
671 | |
672 | jbd_debug(2, "restarting handle %p\n", handle); | |
e4471831 | 673 | need_to_start = !tid_geq(journal->j_commit_request, tid); |
a931da6a | 674 | read_unlock(&journal->j_state_lock); |
e4471831 TT |
675 | if (need_to_start) |
676 | jbd2_log_start_commit(journal, tid); | |
470decc6 | 677 | |
ab714aff | 678 | rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_); |
470decc6 | 679 | handle->h_buffer_credits = nblocks; |
b4709067 TE |
680 | /* |
681 | * Restore the original nofs context because the journal restart | |
682 | * is basically the same thing as journal stop and start. | |
683 | * start_this_handle will start a new nofs context. | |
684 | */ | |
685 | memalloc_nofs_restore(handle->saved_alloc_context); | |
47def826 | 686 | ret = start_this_handle(journal, handle, gfp_mask); |
470decc6 DK |
687 | return ret; |
688 | } | |
47def826 | 689 | EXPORT_SYMBOL(jbd2__journal_restart); |
470decc6 DK |
690 | |
691 | ||
47def826 TT |
692 | int jbd2_journal_restart(handle_t *handle, int nblocks) |
693 | { | |
694 | return jbd2__journal_restart(handle, nblocks, GFP_NOFS); | |
695 | } | |
696 | EXPORT_SYMBOL(jbd2_journal_restart); | |
697 | ||
470decc6 | 698 | /** |
f7f4bccb | 699 | * void jbd2_journal_lock_updates () - establish a transaction barrier. |
470decc6 DK |
700 | * @journal: Journal to establish a barrier on. |
701 | * | |
702 | * This locks out any further updates from being started, and blocks | |
703 | * until all existing updates have completed, returning only once the | |
704 | * journal is in a quiescent state with no updates running. | |
705 | * | |
706 | * The journal lock should not be held on entry. | |
707 | */ | |
f7f4bccb | 708 | void jbd2_journal_lock_updates(journal_t *journal) |
470decc6 DK |
709 | { |
710 | DEFINE_WAIT(wait); | |
711 | ||
1eaa566d JK |
712 | jbd2_might_wait_for_commit(journal); |
713 | ||
a931da6a | 714 | write_lock(&journal->j_state_lock); |
470decc6 DK |
715 | ++journal->j_barrier_count; |
716 | ||
8f7d89f3 JK |
717 | /* Wait until there are no reserved handles */ |
718 | if (atomic_read(&journal->j_reserved_credits)) { | |
719 | write_unlock(&journal->j_state_lock); | |
720 | wait_event(journal->j_wait_reserved, | |
721 | atomic_read(&journal->j_reserved_credits) == 0); | |
722 | write_lock(&journal->j_state_lock); | |
723 | } | |
724 | ||
470decc6 DK |
725 | /* Wait until there are no running updates */ |
726 | while (1) { | |
727 | transaction_t *transaction = journal->j_running_transaction; | |
728 | ||
729 | if (!transaction) | |
730 | break; | |
731 | ||
732 | spin_lock(&transaction->t_handle_lock); | |
9837d8e9 JK |
733 | prepare_to_wait(&journal->j_wait_updates, &wait, |
734 | TASK_UNINTERRUPTIBLE); | |
a51dca9c | 735 | if (!atomic_read(&transaction->t_updates)) { |
470decc6 | 736 | spin_unlock(&transaction->t_handle_lock); |
9837d8e9 | 737 | finish_wait(&journal->j_wait_updates, &wait); |
470decc6 DK |
738 | break; |
739 | } | |
470decc6 | 740 | spin_unlock(&transaction->t_handle_lock); |
a931da6a | 741 | write_unlock(&journal->j_state_lock); |
470decc6 DK |
742 | schedule(); |
743 | finish_wait(&journal->j_wait_updates, &wait); | |
a931da6a | 744 | write_lock(&journal->j_state_lock); |
470decc6 | 745 | } |
a931da6a | 746 | write_unlock(&journal->j_state_lock); |
470decc6 DK |
747 | |
748 | /* | |
749 | * We have now established a barrier against other normal updates, but | |
f7f4bccb | 750 | * we also need to barrier against other jbd2_journal_lock_updates() calls |
470decc6 DK |
751 | * to make sure that we serialise special journal-locked operations |
752 | * too. | |
753 | */ | |
754 | mutex_lock(&journal->j_barrier); | |
755 | } | |
756 | ||
757 | /** | |
f7f4bccb | 758 | * void jbd2_journal_unlock_updates (journal_t* journal) - release barrier |
470decc6 DK |
759 | * @journal: Journal to release the barrier on. |
760 | * | |
f7f4bccb | 761 | * Release a transaction barrier obtained with jbd2_journal_lock_updates(). |
470decc6 DK |
762 | * |
763 | * Should be called without the journal lock held. | |
764 | */ | |
f7f4bccb | 765 | void jbd2_journal_unlock_updates (journal_t *journal) |
470decc6 DK |
766 | { |
767 | J_ASSERT(journal->j_barrier_count != 0); | |
768 | ||
769 | mutex_unlock(&journal->j_barrier); | |
a931da6a | 770 | write_lock(&journal->j_state_lock); |
470decc6 | 771 | --journal->j_barrier_count; |
a931da6a | 772 | write_unlock(&journal->j_state_lock); |
470decc6 DK |
773 | wake_up(&journal->j_wait_transaction_locked); |
774 | } | |
775 | ||
f91d1d04 | 776 | static void warn_dirty_buffer(struct buffer_head *bh) |
470decc6 | 777 | { |
f91d1d04 | 778 | printk(KERN_WARNING |
a1c6f057 | 779 | "JBD2: Spotted dirty metadata buffer (dev = %pg, blocknr = %llu). " |
f91d1d04 JK |
780 | "There's a risk of filesystem corruption in case of system " |
781 | "crash.\n", | |
a1c6f057 | 782 | bh->b_bdev, (unsigned long long)bh->b_blocknr); |
470decc6 DK |
783 | } |
784 | ||
ee57aba1 JK |
785 | /* Call t_frozen trigger and copy buffer data into jh->b_frozen_data. */ |
786 | static void jbd2_freeze_jh_data(struct journal_head *jh) | |
787 | { | |
788 | struct page *page; | |
789 | int offset; | |
790 | char *source; | |
791 | struct buffer_head *bh = jh2bh(jh); | |
792 | ||
793 | J_EXPECT_JH(jh, buffer_uptodate(bh), "Possible IO failure.\n"); | |
794 | page = bh->b_page; | |
795 | offset = offset_in_page(bh->b_data); | |
796 | source = kmap_atomic(page); | |
797 | /* Fire data frozen trigger just before we copy the data */ | |
798 | jbd2_buffer_frozen_trigger(jh, source + offset, jh->b_triggers); | |
799 | memcpy(jh->b_frozen_data, source + offset, bh->b_size); | |
800 | kunmap_atomic(source); | |
801 | ||
802 | /* | |
803 | * Now that the frozen data is saved off, we need to store any matching | |
804 | * triggers. | |
805 | */ | |
806 | jh->b_frozen_triggers = jh->b_triggers; | |
807 | } | |
808 | ||
470decc6 DK |
809 | /* |
810 | * If the buffer is already part of the current transaction, then there | |
811 | * is nothing we need to do. If it is already part of a prior | |
812 | * transaction which we are still committing to disk, then we need to | |
813 | * make sure that we do not overwrite the old copy: we do copy-out to | |
814 | * preserve the copy going to disk. We also account the buffer against | |
815 | * the handle's metadata buffer credits (unless the buffer is already | |
816 | * part of the transaction, that is). | |
817 | * | |
818 | */ | |
819 | static int | |
820 | do_get_write_access(handle_t *handle, struct journal_head *jh, | |
821 | int force_copy) | |
822 | { | |
823 | struct buffer_head *bh; | |
41a5b913 | 824 | transaction_t *transaction = handle->h_transaction; |
470decc6 DK |
825 | journal_t *journal; |
826 | int error; | |
827 | char *frozen_buffer = NULL; | |
f783f091 | 828 | unsigned long start_lock, time_lock; |
470decc6 DK |
829 | |
830 | if (is_handle_aborted(handle)) | |
831 | return -EROFS; | |
470decc6 DK |
832 | journal = transaction->t_journal; |
833 | ||
cfef2c6a | 834 | jbd_debug(5, "journal_head %p, force_copy %d\n", jh, force_copy); |
470decc6 DK |
835 | |
836 | JBUFFER_TRACE(jh, "entry"); | |
837 | repeat: | |
838 | bh = jh2bh(jh); | |
839 | ||
840 | /* @@@ Need to check for errors here at some point. */ | |
841 | ||
f783f091 | 842 | start_lock = jiffies; |
470decc6 DK |
843 | lock_buffer(bh); |
844 | jbd_lock_bh_state(bh); | |
845 | ||
f783f091 TT |
846 | /* If it takes too long to lock the buffer, trace it */ |
847 | time_lock = jbd2_time_diff(start_lock, jiffies); | |
848 | if (time_lock > HZ/10) | |
849 | trace_jbd2_lock_buffer_stall(bh->b_bdev->bd_dev, | |
850 | jiffies_to_msecs(time_lock)); | |
851 | ||
470decc6 DK |
852 | /* We now hold the buffer lock so it is safe to query the buffer |
853 | * state. Is the buffer dirty? | |
854 | * | |
855 | * If so, there are two possibilities. The buffer may be | |
856 | * non-journaled, and undergoing a quite legitimate writeback. | |
857 | * Otherwise, it is journaled, and we don't expect dirty buffers | |
858 | * in that state (the buffers should be marked JBD_Dirty | |
859 | * instead.) So either the IO is being done under our own | |
860 | * control and this is a bug, or it's a third party IO such as | |
861 | * dump(8) (which may leave the buffer scheduled for read --- | |
862 | * ie. locked but not dirty) or tune2fs (which may actually have | |
863 | * the buffer dirtied, ugh.) */ | |
864 | ||
865 | if (buffer_dirty(bh)) { | |
866 | /* | |
867 | * First question: is this buffer already part of the current | |
868 | * transaction or the existing committing transaction? | |
869 | */ | |
870 | if (jh->b_transaction) { | |
871 | J_ASSERT_JH(jh, | |
872 | jh->b_transaction == transaction || | |
873 | jh->b_transaction == | |
874 | journal->j_committing_transaction); | |
875 | if (jh->b_next_transaction) | |
876 | J_ASSERT_JH(jh, jh->b_next_transaction == | |
877 | transaction); | |
f91d1d04 | 878 | warn_dirty_buffer(bh); |
470decc6 DK |
879 | } |
880 | /* | |
881 | * In any case we need to clean the dirty flag and we must | |
882 | * do it under the buffer lock to be sure we don't race | |
883 | * with running write-out. | |
884 | */ | |
f91d1d04 JK |
885 | JBUFFER_TRACE(jh, "Journalling dirty buffer"); |
886 | clear_buffer_dirty(bh); | |
887 | set_buffer_jbddirty(bh); | |
470decc6 DK |
888 | } |
889 | ||
890 | unlock_buffer(bh); | |
891 | ||
892 | error = -EROFS; | |
893 | if (is_handle_aborted(handle)) { | |
894 | jbd_unlock_bh_state(bh); | |
895 | goto out; | |
896 | } | |
897 | error = 0; | |
898 | ||
899 | /* | |
900 | * The buffer is already part of this transaction if b_transaction or | |
901 | * b_next_transaction points to it | |
902 | */ | |
903 | if (jh->b_transaction == transaction || | |
904 | jh->b_next_transaction == transaction) | |
905 | goto done; | |
906 | ||
9fc7c63a JB |
907 | /* |
908 | * this is the first time this transaction is touching this buffer, | |
909 | * reset the modified flag | |
910 | */ | |
911 | jh->b_modified = 0; | |
912 | ||
8b00f400 JK |
913 | /* |
914 | * If the buffer is not journaled right now, we need to make sure it | |
915 | * doesn't get written to disk before the caller actually commits the | |
916 | * new data | |
917 | */ | |
918 | if (!jh->b_transaction) { | |
919 | JBUFFER_TRACE(jh, "no transaction"); | |
920 | J_ASSERT_JH(jh, !jh->b_next_transaction); | |
921 | JBUFFER_TRACE(jh, "file as BJ_Reserved"); | |
de92c8ca JK |
922 | /* |
923 | * Make sure all stores to jh (b_modified, b_frozen_data) are | |
924 | * visible before attaching it to the running transaction. | |
925 | * Paired with barrier in jbd2_write_access_granted() | |
926 | */ | |
927 | smp_wmb(); | |
8b00f400 JK |
928 | spin_lock(&journal->j_list_lock); |
929 | __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved); | |
930 | spin_unlock(&journal->j_list_lock); | |
931 | goto done; | |
932 | } | |
470decc6 DK |
933 | /* |
934 | * If there is already a copy-out version of this buffer, then we don't | |
935 | * need to make another one | |
936 | */ | |
937 | if (jh->b_frozen_data) { | |
938 | JBUFFER_TRACE(jh, "has frozen data"); | |
939 | J_ASSERT_JH(jh, jh->b_next_transaction == NULL); | |
de92c8ca | 940 | goto attach_next; |
470decc6 DK |
941 | } |
942 | ||
8b00f400 JK |
943 | JBUFFER_TRACE(jh, "owned by older transaction"); |
944 | J_ASSERT_JH(jh, jh->b_next_transaction == NULL); | |
945 | J_ASSERT_JH(jh, jh->b_transaction == journal->j_committing_transaction); | |
470decc6 | 946 | |
8b00f400 JK |
947 | /* |
948 | * There is one case we have to be very careful about. If the | |
949 | * committing transaction is currently writing this buffer out to disk | |
950 | * and has NOT made a copy-out, then we cannot modify the buffer | |
951 | * contents at all right now. The essence of copy-out is that it is | |
952 | * the extra copy, not the primary copy, which gets journaled. If the | |
953 | * primary copy is already going to disk then we cannot do copy-out | |
954 | * here. | |
955 | */ | |
956 | if (buffer_shadow(bh)) { | |
957 | JBUFFER_TRACE(jh, "on shadow: sleep"); | |
958 | jbd_unlock_bh_state(bh); | |
959 | wait_on_bit_io(&bh->b_state, BH_Shadow, TASK_UNINTERRUPTIBLE); | |
960 | goto repeat; | |
961 | } | |
470decc6 | 962 | |
8b00f400 JK |
963 | /* |
964 | * Only do the copy if the currently-owning transaction still needs it. | |
965 | * If buffer isn't on BJ_Metadata list, the committing transaction is | |
966 | * past that stage (here we use the fact that BH_Shadow is set under | |
967 | * bh_state lock together with refiling to BJ_Shadow list and at this | |
968 | * point we know the buffer doesn't have BH_Shadow set). | |
969 | * | |
970 | * Subtle point, though: if this is a get_undo_access, then we will be | |
971 | * relying on the frozen_data to contain the new value of the | |
972 | * committed_data record after the transaction, so we HAVE to force the | |
973 | * frozen_data copy in that case. | |
974 | */ | |
975 | if (jh->b_jlist == BJ_Metadata || force_copy) { | |
976 | JBUFFER_TRACE(jh, "generate frozen data"); | |
977 | if (!frozen_buffer) { | |
978 | JBUFFER_TRACE(jh, "allocate memory for buffer"); | |
470decc6 | 979 | jbd_unlock_bh_state(bh); |
490c1b44 MH |
980 | frozen_buffer = jbd2_alloc(jh2bh(jh)->b_size, |
981 | GFP_NOFS | __GFP_NOFAIL); | |
8b00f400 | 982 | goto repeat; |
470decc6 | 983 | } |
8b00f400 JK |
984 | jh->b_frozen_data = frozen_buffer; |
985 | frozen_buffer = NULL; | |
986 | jbd2_freeze_jh_data(jh); | |
470decc6 | 987 | } |
de92c8ca JK |
988 | attach_next: |
989 | /* | |
990 | * Make sure all stores to jh (b_modified, b_frozen_data) are visible | |
991 | * before attaching it to the running transaction. Paired with barrier | |
992 | * in jbd2_write_access_granted() | |
993 | */ | |
994 | smp_wmb(); | |
8b00f400 | 995 | jh->b_next_transaction = transaction; |
470decc6 DK |
996 | |
997 | done: | |
470decc6 DK |
998 | jbd_unlock_bh_state(bh); |
999 | ||
1000 | /* | |
1001 | * If we are about to journal a buffer, then any revoke pending on it is | |
1002 | * no longer valid | |
1003 | */ | |
f7f4bccb | 1004 | jbd2_journal_cancel_revoke(handle, jh); |
470decc6 DK |
1005 | |
1006 | out: | |
1007 | if (unlikely(frozen_buffer)) /* It's usually NULL */ | |
af1e76d6 | 1008 | jbd2_free(frozen_buffer, bh->b_size); |
470decc6 DK |
1009 | |
1010 | JBUFFER_TRACE(jh, "exit"); | |
1011 | return error; | |
1012 | } | |
1013 | ||
de92c8ca | 1014 | /* Fast check whether buffer is already attached to the required transaction */ |
087ffd4e JB |
1015 | static bool jbd2_write_access_granted(handle_t *handle, struct buffer_head *bh, |
1016 | bool undo) | |
de92c8ca JK |
1017 | { |
1018 | struct journal_head *jh; | |
1019 | bool ret = false; | |
1020 | ||
1021 | /* Dirty buffers require special handling... */ | |
1022 | if (buffer_dirty(bh)) | |
1023 | return false; | |
1024 | ||
1025 | /* | |
1026 | * RCU protects us from dereferencing freed pages. So the checks we do | |
1027 | * are guaranteed not to oops. However the jh slab object can get freed | |
1028 | * & reallocated while we work with it. So we have to be careful. When | |
1029 | * we see jh attached to the running transaction, we know it must stay | |
1030 | * so until the transaction is committed. Thus jh won't be freed and | |
1031 | * will be attached to the same bh while we run. However it can | |
1032 | * happen jh gets freed, reallocated, and attached to the transaction | |
1033 | * just after we get pointer to it from bh. So we have to be careful | |
1034 | * and recheck jh still belongs to our bh before we return success. | |
1035 | */ | |
1036 | rcu_read_lock(); | |
1037 | if (!buffer_jbd(bh)) | |
1038 | goto out; | |
1039 | /* This should be bh2jh() but that doesn't work with inline functions */ | |
1040 | jh = READ_ONCE(bh->b_private); | |
1041 | if (!jh) | |
1042 | goto out; | |
087ffd4e JB |
1043 | /* For undo access buffer must have data copied */ |
1044 | if (undo && !jh->b_committed_data) | |
1045 | goto out; | |
de92c8ca JK |
1046 | if (jh->b_transaction != handle->h_transaction && |
1047 | jh->b_next_transaction != handle->h_transaction) | |
1048 | goto out; | |
1049 | /* | |
1050 | * There are two reasons for the barrier here: | |
1051 | * 1) Make sure to fetch b_bh after we did previous checks so that we | |
1052 | * detect when jh went through free, realloc, attach to transaction | |
1053 | * while we were checking. Paired with implicit barrier in that path. | |
1054 | * 2) So that access to bh done after jbd2_write_access_granted() | |
1055 | * doesn't get reordered and see inconsistent state of concurrent | |
1056 | * do_get_write_access(). | |
1057 | */ | |
1058 | smp_mb(); | |
1059 | if (unlikely(jh->b_bh != bh)) | |
1060 | goto out; | |
1061 | ret = true; | |
1062 | out: | |
1063 | rcu_read_unlock(); | |
1064 | return ret; | |
1065 | } | |
1066 | ||
470decc6 | 1067 | /** |
f7f4bccb | 1068 | * int jbd2_journal_get_write_access() - notify intent to modify a buffer for metadata (not data) update. |
470decc6 DK |
1069 | * @handle: transaction to add buffer modifications to |
1070 | * @bh: bh to be used for metadata writes | |
470decc6 | 1071 | * |
df1b560a | 1072 | * Returns: error code or 0 on success. |
470decc6 DK |
1073 | * |
1074 | * In full data journalling mode the buffer may be of type BJ_AsyncData, | |
df1b560a | 1075 | * because we're ``write()ing`` a buffer which is also part of a shared mapping. |
470decc6 DK |
1076 | */ |
1077 | ||
f7f4bccb | 1078 | int jbd2_journal_get_write_access(handle_t *handle, struct buffer_head *bh) |
470decc6 | 1079 | { |
de92c8ca | 1080 | struct journal_head *jh; |
470decc6 DK |
1081 | int rc; |
1082 | ||
087ffd4e | 1083 | if (jbd2_write_access_granted(handle, bh, false)) |
de92c8ca JK |
1084 | return 0; |
1085 | ||
1086 | jh = jbd2_journal_add_journal_head(bh); | |
470decc6 DK |
1087 | /* We do not want to get caught playing with fields which the |
1088 | * log thread also manipulates. Make sure that the buffer | |
1089 | * completes any outstanding IO before proceeding. */ | |
1090 | rc = do_get_write_access(handle, jh, 0); | |
f7f4bccb | 1091 | jbd2_journal_put_journal_head(jh); |
470decc6 DK |
1092 | return rc; |
1093 | } | |
1094 | ||
1095 | ||
1096 | /* | |
1097 | * When the user wants to journal a newly created buffer_head | |
1098 | * (ie. getblk() returned a new buffer and we are going to populate it | |
1099 | * manually rather than reading off disk), then we need to keep the | |
1100 | * buffer_head locked until it has been completely filled with new | |
1101 | * data. In this case, we should be able to make the assertion that | |
1102 | * the bh is not already part of an existing transaction. | |
1103 | * | |
1104 | * The buffer should already be locked by the caller by this point. | |
1105 | * There is no lock ranking violation: it was a newly created, | |
1106 | * unlocked buffer beforehand. */ | |
1107 | ||
1108 | /** | |
f7f4bccb | 1109 | * int jbd2_journal_get_create_access () - notify intent to use newly created bh |
470decc6 DK |
1110 | * @handle: transaction to new buffer to |
1111 | * @bh: new buffer. | |
1112 | * | |
1113 | * Call this if you create a new bh. | |
1114 | */ | |
f7f4bccb | 1115 | int jbd2_journal_get_create_access(handle_t *handle, struct buffer_head *bh) |
470decc6 DK |
1116 | { |
1117 | transaction_t *transaction = handle->h_transaction; | |
41a5b913 | 1118 | journal_t *journal; |
f7f4bccb | 1119 | struct journal_head *jh = jbd2_journal_add_journal_head(bh); |
470decc6 DK |
1120 | int err; |
1121 | ||
1122 | jbd_debug(5, "journal_head %p\n", jh); | |
1123 | err = -EROFS; | |
1124 | if (is_handle_aborted(handle)) | |
1125 | goto out; | |
41a5b913 | 1126 | journal = transaction->t_journal; |
470decc6 DK |
1127 | err = 0; |
1128 | ||
1129 | JBUFFER_TRACE(jh, "entry"); | |
1130 | /* | |
1131 | * The buffer may already belong to this transaction due to pre-zeroing | |
1132 | * in the filesystem's new_block code. It may also be on the previous, | |
1133 | * committing transaction's lists, but it HAS to be in Forget state in | |
1134 | * that case: the transaction must have deleted the buffer for it to be | |
1135 | * reused here. | |
1136 | */ | |
1137 | jbd_lock_bh_state(bh); | |
470decc6 DK |
1138 | J_ASSERT_JH(jh, (jh->b_transaction == transaction || |
1139 | jh->b_transaction == NULL || | |
1140 | (jh->b_transaction == journal->j_committing_transaction && | |
1141 | jh->b_jlist == BJ_Forget))); | |
1142 | ||
1143 | J_ASSERT_JH(jh, jh->b_next_transaction == NULL); | |
1144 | J_ASSERT_JH(jh, buffer_locked(jh2bh(jh))); | |
1145 | ||
1146 | if (jh->b_transaction == NULL) { | |
f91d1d04 JK |
1147 | /* |
1148 | * Previous jbd2_journal_forget() could have left the buffer | |
1149 | * with jbddirty bit set because it was being committed. When | |
1150 | * the commit finished, we've filed the buffer for | |
1151 | * checkpointing and marked it dirty. Now we are reallocating | |
1152 | * the buffer so the transaction freeing it must have | |
1153 | * committed and so it's safe to clear the dirty bit. | |
1154 | */ | |
1155 | clear_buffer_dirty(jh2bh(jh)); | |
9fc7c63a JB |
1156 | /* first access by this transaction */ |
1157 | jh->b_modified = 0; | |
1158 | ||
470decc6 | 1159 | JBUFFER_TRACE(jh, "file as BJ_Reserved"); |
6e4862a5 | 1160 | spin_lock(&journal->j_list_lock); |
f7f4bccb | 1161 | __jbd2_journal_file_buffer(jh, transaction, BJ_Reserved); |
559cce69 | 1162 | spin_unlock(&journal->j_list_lock); |
470decc6 | 1163 | } else if (jh->b_transaction == journal->j_committing_transaction) { |
9fc7c63a JB |
1164 | /* first access by this transaction */ |
1165 | jh->b_modified = 0; | |
1166 | ||
470decc6 | 1167 | JBUFFER_TRACE(jh, "set next transaction"); |
6e4862a5 | 1168 | spin_lock(&journal->j_list_lock); |
470decc6 | 1169 | jh->b_next_transaction = transaction; |
559cce69 | 1170 | spin_unlock(&journal->j_list_lock); |
470decc6 | 1171 | } |
470decc6 DK |
1172 | jbd_unlock_bh_state(bh); |
1173 | ||
1174 | /* | |
1175 | * akpm: I added this. ext3_alloc_branch can pick up new indirect | |
1176 | * blocks which contain freed but then revoked metadata. We need | |
1177 | * to cancel the revoke in case we end up freeing it yet again | |
1178 | * and the reallocating as data - this would cause a second revoke, | |
1179 | * which hits an assertion error. | |
1180 | */ | |
1181 | JBUFFER_TRACE(jh, "cancelling revoke"); | |
f7f4bccb | 1182 | jbd2_journal_cancel_revoke(handle, jh); |
470decc6 | 1183 | out: |
3991b400 | 1184 | jbd2_journal_put_journal_head(jh); |
470decc6 DK |
1185 | return err; |
1186 | } | |
1187 | ||
1188 | /** | |
f7f4bccb | 1189 | * int jbd2_journal_get_undo_access() - Notify intent to modify metadata with |
470decc6 DK |
1190 | * non-rewindable consequences |
1191 | * @handle: transaction | |
1192 | * @bh: buffer to undo | |
470decc6 DK |
1193 | * |
1194 | * Sometimes there is a need to distinguish between metadata which has | |
1195 | * been committed to disk and that which has not. The ext3fs code uses | |
1196 | * this for freeing and allocating space, we have to make sure that we | |
1197 | * do not reuse freed space until the deallocation has been committed, | |
1198 | * since if we overwrote that space we would make the delete | |
1199 | * un-rewindable in case of a crash. | |
1200 | * | |
f7f4bccb | 1201 | * To deal with that, jbd2_journal_get_undo_access requests write access to a |
470decc6 DK |
1202 | * buffer for parts of non-rewindable operations such as delete |
1203 | * operations on the bitmaps. The journaling code must keep a copy of | |
1204 | * the buffer's contents prior to the undo_access call until such time | |
1205 | * as we know that the buffer has definitely been committed to disk. | |
1206 | * | |
1207 | * We never need to know which transaction the committed data is part | |
1208 | * of, buffers touched here are guaranteed to be dirtied later and so | |
1209 | * will be committed to a new transaction in due course, at which point | |
1210 | * we can discard the old committed data pointer. | |
1211 | * | |
1212 | * Returns error number or 0 on success. | |
1213 | */ | |
f7f4bccb | 1214 | int jbd2_journal_get_undo_access(handle_t *handle, struct buffer_head *bh) |
470decc6 DK |
1215 | { |
1216 | int err; | |
de92c8ca | 1217 | struct journal_head *jh; |
470decc6 DK |
1218 | char *committed_data = NULL; |
1219 | ||
1220 | JBUFFER_TRACE(jh, "entry"); | |
087ffd4e | 1221 | if (jbd2_write_access_granted(handle, bh, true)) |
de92c8ca | 1222 | return 0; |
470decc6 | 1223 | |
de92c8ca | 1224 | jh = jbd2_journal_add_journal_head(bh); |
470decc6 DK |
1225 | /* |
1226 | * Do this first --- it can drop the journal lock, so we want to | |
1227 | * make sure that obtaining the committed_data is done | |
1228 | * atomically wrt. completion of any outstanding commits. | |
1229 | */ | |
1230 | err = do_get_write_access(handle, jh, 1); | |
1231 | if (err) | |
1232 | goto out; | |
1233 | ||
1234 | repeat: | |
490c1b44 MH |
1235 | if (!jh->b_committed_data) |
1236 | committed_data = jbd2_alloc(jh2bh(jh)->b_size, | |
1237 | GFP_NOFS|__GFP_NOFAIL); | |
470decc6 DK |
1238 | |
1239 | jbd_lock_bh_state(bh); | |
1240 | if (!jh->b_committed_data) { | |
1241 | /* Copy out the current buffer contents into the | |
1242 | * preserved, committed copy. */ | |
1243 | JBUFFER_TRACE(jh, "generate b_committed data"); | |
1244 | if (!committed_data) { | |
1245 | jbd_unlock_bh_state(bh); | |
1246 | goto repeat; | |
1247 | } | |
1248 | ||
1249 | jh->b_committed_data = committed_data; | |
1250 | committed_data = NULL; | |
1251 | memcpy(jh->b_committed_data, bh->b_data, bh->b_size); | |
1252 | } | |
1253 | jbd_unlock_bh_state(bh); | |
1254 | out: | |
f7f4bccb | 1255 | jbd2_journal_put_journal_head(jh); |
470decc6 | 1256 | if (unlikely(committed_data)) |
af1e76d6 | 1257 | jbd2_free(committed_data, bh->b_size); |
470decc6 DK |
1258 | return err; |
1259 | } | |
1260 | ||
e06c8227 JB |
1261 | /** |
1262 | * void jbd2_journal_set_triggers() - Add triggers for commit writeout | |
1263 | * @bh: buffer to trigger on | |
1264 | * @type: struct jbd2_buffer_trigger_type containing the trigger(s). | |
1265 | * | |
1266 | * Set any triggers on this journal_head. This is always safe, because | |
1267 | * triggers for a committing buffer will be saved off, and triggers for | |
1268 | * a running transaction will match the buffer in that transaction. | |
1269 | * | |
1270 | * Call with NULL to clear the triggers. | |
1271 | */ | |
1272 | void jbd2_journal_set_triggers(struct buffer_head *bh, | |
1273 | struct jbd2_buffer_trigger_type *type) | |
1274 | { | |
ad56edad | 1275 | struct journal_head *jh = jbd2_journal_grab_journal_head(bh); |
e06c8227 | 1276 | |
ad56edad JK |
1277 | if (WARN_ON(!jh)) |
1278 | return; | |
e06c8227 | 1279 | jh->b_triggers = type; |
ad56edad | 1280 | jbd2_journal_put_journal_head(jh); |
e06c8227 JB |
1281 | } |
1282 | ||
13ceef09 | 1283 | void jbd2_buffer_frozen_trigger(struct journal_head *jh, void *mapped_data, |
e06c8227 JB |
1284 | struct jbd2_buffer_trigger_type *triggers) |
1285 | { | |
1286 | struct buffer_head *bh = jh2bh(jh); | |
1287 | ||
13ceef09 | 1288 | if (!triggers || !triggers->t_frozen) |
e06c8227 JB |
1289 | return; |
1290 | ||
13ceef09 | 1291 | triggers->t_frozen(triggers, bh, mapped_data, bh->b_size); |
e06c8227 JB |
1292 | } |
1293 | ||
1294 | void jbd2_buffer_abort_trigger(struct journal_head *jh, | |
1295 | struct jbd2_buffer_trigger_type *triggers) | |
1296 | { | |
1297 | if (!triggers || !triggers->t_abort) | |
1298 | return; | |
1299 | ||
1300 | triggers->t_abort(triggers, jh2bh(jh)); | |
1301 | } | |
1302 | ||
470decc6 | 1303 | /** |
f7f4bccb | 1304 | * int jbd2_journal_dirty_metadata() - mark a buffer as containing dirty metadata |
470decc6 DK |
1305 | * @handle: transaction to add buffer to. |
1306 | * @bh: buffer to mark | |
1307 | * | |
1308 | * mark dirty metadata which needs to be journaled as part of the current | |
1309 | * transaction. | |
1310 | * | |
9ea7a0df TT |
1311 | * The buffer must have previously had jbd2_journal_get_write_access() |
1312 | * called so that it has a valid journal_head attached to the buffer | |
1313 | * head. | |
1314 | * | |
470decc6 DK |
1315 | * The buffer is placed on the transaction's metadata list and is marked |
1316 | * as belonging to the transaction. | |
1317 | * | |
1318 | * Returns error number or 0 on success. | |
1319 | * | |
1320 | * Special care needs to be taken if the buffer already belongs to the | |
1321 | * current committing transaction (in which case we should have frozen | |
1322 | * data present for that commit). In that case, we don't relink the | |
1323 | * buffer: that only gets done when the old transaction finally | |
1324 | * completes its commit. | |
1325 | */ | |
f7f4bccb | 1326 | int jbd2_journal_dirty_metadata(handle_t *handle, struct buffer_head *bh) |
470decc6 DK |
1327 | { |
1328 | transaction_t *transaction = handle->h_transaction; | |
41a5b913 | 1329 | journal_t *journal; |
ad56edad | 1330 | struct journal_head *jh; |
9ea7a0df | 1331 | int ret = 0; |
470decc6 | 1332 | |
470decc6 | 1333 | if (is_handle_aborted(handle)) |
41a5b913 | 1334 | return -EROFS; |
6e06ae88 | 1335 | if (!buffer_jbd(bh)) { |
9ea7a0df TT |
1336 | ret = -EUCLEAN; |
1337 | goto out; | |
1338 | } | |
6e06ae88 JK |
1339 | /* |
1340 | * We don't grab jh reference here since the buffer must be part | |
1341 | * of the running transaction. | |
1342 | */ | |
1343 | jh = bh2jh(bh); | |
1344 | /* | |
1345 | * This and the following assertions are unreliable since we may see jh | |
1346 | * in inconsistent state unless we grab bh_state lock. But this is | |
1347 | * crucial to catch bugs so let's do a reliable check until the | |
1348 | * lockless handling is fully proven. | |
1349 | */ | |
1350 | if (jh->b_transaction != transaction && | |
1351 | jh->b_next_transaction != transaction) { | |
1352 | jbd_lock_bh_state(bh); | |
1353 | J_ASSERT_JH(jh, jh->b_transaction == transaction || | |
1354 | jh->b_next_transaction == transaction); | |
1355 | jbd_unlock_bh_state(bh); | |
1356 | } | |
1357 | if (jh->b_modified == 1) { | |
1358 | /* If it's in our transaction it must be in BJ_Metadata list. */ | |
1359 | if (jh->b_transaction == transaction && | |
1360 | jh->b_jlist != BJ_Metadata) { | |
1361 | jbd_lock_bh_state(bh); | |
1362 | J_ASSERT_JH(jh, jh->b_transaction != transaction || | |
1363 | jh->b_jlist == BJ_Metadata); | |
1364 | jbd_unlock_bh_state(bh); | |
1365 | } | |
1366 | goto out; | |
1367 | } | |
1368 | ||
1369 | journal = transaction->t_journal; | |
ad56edad JK |
1370 | jbd_debug(5, "journal_head %p\n", jh); |
1371 | JBUFFER_TRACE(jh, "entry"); | |
470decc6 DK |
1372 | |
1373 | jbd_lock_bh_state(bh); | |
1374 | ||
1375 | if (jh->b_modified == 0) { | |
1376 | /* | |
1377 | * This buffer's got modified and becoming part | |
1378 | * of the transaction. This needs to be done | |
1379 | * once a transaction -bzzz | |
1380 | */ | |
1381 | jh->b_modified = 1; | |
f6c07cad TT |
1382 | if (handle->h_buffer_credits <= 0) { |
1383 | ret = -ENOSPC; | |
1384 | goto out_unlock_bh; | |
1385 | } | |
470decc6 DK |
1386 | handle->h_buffer_credits--; |
1387 | } | |
1388 | ||
1389 | /* | |
1390 | * fastpath, to avoid expensive locking. If this buffer is already | |
1391 | * on the running transaction's metadata list there is nothing to do. | |
1392 | * Nobody can take it off again because there is a handle open. | |
1393 | * I _think_ we're OK here with SMP barriers - a mistaken decision will | |
1394 | * result in this test being false, so we go in and take the locks. | |
1395 | */ | |
1396 | if (jh->b_transaction == transaction && jh->b_jlist == BJ_Metadata) { | |
1397 | JBUFFER_TRACE(jh, "fastpath"); | |
9ea7a0df TT |
1398 | if (unlikely(jh->b_transaction != |
1399 | journal->j_running_transaction)) { | |
a67c848a | 1400 | printk(KERN_ERR "JBD2: %s: " |
9ea7a0df | 1401 | "jh->b_transaction (%llu, %p, %u) != " |
66a4cb18 | 1402 | "journal->j_running_transaction (%p, %u)\n", |
9ea7a0df TT |
1403 | journal->j_devname, |
1404 | (unsigned long long) bh->b_blocknr, | |
1405 | jh->b_transaction, | |
1406 | jh->b_transaction ? jh->b_transaction->t_tid : 0, | |
1407 | journal->j_running_transaction, | |
1408 | journal->j_running_transaction ? | |
1409 | journal->j_running_transaction->t_tid : 0); | |
1410 | ret = -EINVAL; | |
1411 | } | |
470decc6 DK |
1412 | goto out_unlock_bh; |
1413 | } | |
1414 | ||
1415 | set_buffer_jbddirty(bh); | |
1416 | ||
1417 | /* | |
1418 | * Metadata already on the current transaction list doesn't | |
1419 | * need to be filed. Metadata on another transaction's list must | |
1420 | * be committing, and will be refiled once the commit completes: | |
1421 | * leave it alone for now. | |
1422 | */ | |
1423 | if (jh->b_transaction != transaction) { | |
1424 | JBUFFER_TRACE(jh, "already on other transaction"); | |
66a4cb18 TT |
1425 | if (unlikely(((jh->b_transaction != |
1426 | journal->j_committing_transaction)) || | |
1427 | (jh->b_next_transaction != transaction))) { | |
1428 | printk(KERN_ERR "jbd2_journal_dirty_metadata: %s: " | |
1429 | "bad jh for block %llu: " | |
1430 | "transaction (%p, %u), " | |
1431 | "jh->b_transaction (%p, %u), " | |
1432 | "jh->b_next_transaction (%p, %u), jlist %u\n", | |
9ea7a0df TT |
1433 | journal->j_devname, |
1434 | (unsigned long long) bh->b_blocknr, | |
66a4cb18 | 1435 | transaction, transaction->t_tid, |
9ea7a0df | 1436 | jh->b_transaction, |
66a4cb18 TT |
1437 | jh->b_transaction ? |
1438 | jh->b_transaction->t_tid : 0, | |
9ea7a0df TT |
1439 | jh->b_next_transaction, |
1440 | jh->b_next_transaction ? | |
1441 | jh->b_next_transaction->t_tid : 0, | |
66a4cb18 TT |
1442 | jh->b_jlist); |
1443 | WARN_ON(1); | |
9ea7a0df TT |
1444 | ret = -EINVAL; |
1445 | } | |
470decc6 DK |
1446 | /* And this case is illegal: we can't reuse another |
1447 | * transaction's data buffer, ever. */ | |
1448 | goto out_unlock_bh; | |
1449 | } | |
1450 | ||
1451 | /* That test should have eliminated the following case: */ | |
4019191b | 1452 | J_ASSERT_JH(jh, jh->b_frozen_data == NULL); |
470decc6 DK |
1453 | |
1454 | JBUFFER_TRACE(jh, "file as BJ_Metadata"); | |
1455 | spin_lock(&journal->j_list_lock); | |
41a5b913 | 1456 | __jbd2_journal_file_buffer(jh, transaction, BJ_Metadata); |
470decc6 DK |
1457 | spin_unlock(&journal->j_list_lock); |
1458 | out_unlock_bh: | |
1459 | jbd_unlock_bh_state(bh); | |
1460 | out: | |
1461 | JBUFFER_TRACE(jh, "exit"); | |
9ea7a0df | 1462 | return ret; |
470decc6 DK |
1463 | } |
1464 | ||
470decc6 | 1465 | /** |
f7f4bccb | 1466 | * void jbd2_journal_forget() - bforget() for potentially-journaled buffers. |
470decc6 DK |
1467 | * @handle: transaction handle |
1468 | * @bh: bh to 'forget' | |
1469 | * | |
1470 | * We can only do the bforget if there are no commits pending against the | |
1471 | * buffer. If the buffer is dirty in the current running transaction we | |
1472 | * can safely unlink it. | |
1473 | * | |
1474 | * bh may not be a journalled buffer at all - it may be a non-JBD | |
1475 | * buffer which came off the hashtable. Check for this. | |
1476 | * | |
1477 | * Decrements bh->b_count by one. | |
1478 | * | |
1479 | * Allow this call even if the handle has aborted --- it may be part of | |
1480 | * the caller's cleanup after an abort. | |
1481 | */ | |
f7f4bccb | 1482 | int jbd2_journal_forget (handle_t *handle, struct buffer_head *bh) |
470decc6 DK |
1483 | { |
1484 | transaction_t *transaction = handle->h_transaction; | |
41a5b913 | 1485 | journal_t *journal; |
470decc6 DK |
1486 | struct journal_head *jh; |
1487 | int drop_reserve = 0; | |
1488 | int err = 0; | |
1dfc3220 | 1489 | int was_modified = 0; |
470decc6 | 1490 | |
41a5b913 TT |
1491 | if (is_handle_aborted(handle)) |
1492 | return -EROFS; | |
1493 | journal = transaction->t_journal; | |
1494 | ||
470decc6 DK |
1495 | BUFFER_TRACE(bh, "entry"); |
1496 | ||
1497 | jbd_lock_bh_state(bh); | |
470decc6 DK |
1498 | |
1499 | if (!buffer_jbd(bh)) | |
1500 | goto not_jbd; | |
1501 | jh = bh2jh(bh); | |
1502 | ||
1503 | /* Critical error: attempting to delete a bitmap buffer, maybe? | |
1504 | * Don't do any jbd operations, and return an error. */ | |
1505 | if (!J_EXPECT_JH(jh, !jh->b_committed_data, | |
1506 | "inconsistent data on disk")) { | |
1507 | err = -EIO; | |
1508 | goto not_jbd; | |
1509 | } | |
1510 | ||
48fc7f7e | 1511 | /* keep track of whether or not this transaction modified us */ |
1dfc3220 JB |
1512 | was_modified = jh->b_modified; |
1513 | ||
470decc6 DK |
1514 | /* |
1515 | * The buffer's going from the transaction, we must drop | |
1516 | * all references -bzzz | |
1517 | */ | |
1518 | jh->b_modified = 0; | |
1519 | ||
41a5b913 | 1520 | if (jh->b_transaction == transaction) { |
470decc6 DK |
1521 | J_ASSERT_JH(jh, !jh->b_frozen_data); |
1522 | ||
1523 | /* If we are forgetting a buffer which is already part | |
1524 | * of this transaction, then we can just drop it from | |
1525 | * the transaction immediately. */ | |
1526 | clear_buffer_dirty(bh); | |
1527 | clear_buffer_jbddirty(bh); | |
1528 | ||
1529 | JBUFFER_TRACE(jh, "belongs to current transaction: unfile"); | |
1530 | ||
1dfc3220 JB |
1531 | /* |
1532 | * we only want to drop a reference if this transaction | |
1533 | * modified the buffer | |
1534 | */ | |
1535 | if (was_modified) | |
1536 | drop_reserve = 1; | |
470decc6 DK |
1537 | |
1538 | /* | |
1539 | * We are no longer going to journal this buffer. | |
1540 | * However, the commit of this transaction is still | |
1541 | * important to the buffer: the delete that we are now | |
1542 | * processing might obsolete an old log entry, so by | |
1543 | * committing, we can satisfy the buffer's checkpoint. | |
1544 | * | |
1545 | * So, if we have a checkpoint on the buffer, we should | |
1546 | * now refile the buffer on our BJ_Forget list so that | |
1547 | * we know to remove the checkpoint after we commit. | |
1548 | */ | |
1549 | ||
0bfea811 | 1550 | spin_lock(&journal->j_list_lock); |
470decc6 | 1551 | if (jh->b_cp_transaction) { |
f7f4bccb MC |
1552 | __jbd2_journal_temp_unlink_buffer(jh); |
1553 | __jbd2_journal_file_buffer(jh, transaction, BJ_Forget); | |
470decc6 | 1554 | } else { |
f7f4bccb | 1555 | __jbd2_journal_unfile_buffer(jh); |
470decc6 DK |
1556 | if (!buffer_jbd(bh)) { |
1557 | spin_unlock(&journal->j_list_lock); | |
1558 | jbd_unlock_bh_state(bh); | |
1559 | __bforget(bh); | |
1560 | goto drop; | |
1561 | } | |
1562 | } | |
0bfea811 | 1563 | spin_unlock(&journal->j_list_lock); |
470decc6 DK |
1564 | } else if (jh->b_transaction) { |
1565 | J_ASSERT_JH(jh, (jh->b_transaction == | |
1566 | journal->j_committing_transaction)); | |
1567 | /* However, if the buffer is still owned by a prior | |
1568 | * (committing) transaction, we can't drop it yet... */ | |
1569 | JBUFFER_TRACE(jh, "belongs to older transaction"); | |
1570 | /* ... but we CAN drop it from the new transaction if we | |
1571 | * have also modified it since the original commit. */ | |
1572 | ||
1573 | if (jh->b_next_transaction) { | |
1574 | J_ASSERT(jh->b_next_transaction == transaction); | |
0bfea811 | 1575 | spin_lock(&journal->j_list_lock); |
470decc6 | 1576 | jh->b_next_transaction = NULL; |
0bfea811 | 1577 | spin_unlock(&journal->j_list_lock); |
1dfc3220 JB |
1578 | |
1579 | /* | |
1580 | * only drop a reference if this transaction modified | |
1581 | * the buffer | |
1582 | */ | |
1583 | if (was_modified) | |
1584 | drop_reserve = 1; | |
470decc6 DK |
1585 | } |
1586 | } | |
1587 | ||
1588 | not_jbd: | |
470decc6 DK |
1589 | jbd_unlock_bh_state(bh); |
1590 | __brelse(bh); | |
1591 | drop: | |
1592 | if (drop_reserve) { | |
1593 | /* no need to reserve log space for this block -bzzz */ | |
1594 | handle->h_buffer_credits++; | |
1595 | } | |
1596 | return err; | |
1597 | } | |
1598 | ||
1599 | /** | |
f7f4bccb | 1600 | * int jbd2_journal_stop() - complete a transaction |
bd7ced98 | 1601 | * @handle: transaction to complete. |
470decc6 DK |
1602 | * |
1603 | * All done for a particular handle. | |
1604 | * | |
1605 | * There is not much action needed here. We just return any remaining | |
1606 | * buffer credits to the transaction and remove the handle. The only | |
1607 | * complication is that we need to start a commit operation if the | |
1608 | * filesystem is marked for synchronous update. | |
1609 | * | |
f7f4bccb | 1610 | * jbd2_journal_stop itself will not usually return an error, but it may |
470decc6 | 1611 | * do so in unusual circumstances. In particular, expect it to |
f7f4bccb | 1612 | * return -EIO if a jbd2_journal_abort has been executed since the |
470decc6 DK |
1613 | * transaction began. |
1614 | */ | |
f7f4bccb | 1615 | int jbd2_journal_stop(handle_t *handle) |
470decc6 DK |
1616 | { |
1617 | transaction_t *transaction = handle->h_transaction; | |
41a5b913 TT |
1618 | journal_t *journal; |
1619 | int err = 0, wait_for_commit = 0; | |
a51dca9c | 1620 | tid_t tid; |
470decc6 DK |
1621 | pid_t pid; |
1622 | ||
9d506594 LC |
1623 | if (!transaction) { |
1624 | /* | |
1625 | * Handle is already detached from the transaction so | |
1626 | * there is nothing to do other than decrease a refcount, | |
1627 | * or free the handle if refcount drops to zero | |
1628 | */ | |
1629 | if (--handle->h_ref > 0) { | |
1630 | jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1, | |
1631 | handle->h_ref); | |
1632 | return err; | |
1633 | } else { | |
1634 | if (handle->h_rsv_handle) | |
1635 | jbd2_free_handle(handle->h_rsv_handle); | |
1636 | goto free_and_exit; | |
1637 | } | |
1638 | } | |
41a5b913 TT |
1639 | journal = transaction->t_journal; |
1640 | ||
470decc6 DK |
1641 | J_ASSERT(journal_current_handle() == handle); |
1642 | ||
1643 | if (is_handle_aborted(handle)) | |
1644 | err = -EIO; | |
41a5b913 | 1645 | else |
a51dca9c | 1646 | J_ASSERT(atomic_read(&transaction->t_updates) > 0); |
470decc6 DK |
1647 | |
1648 | if (--handle->h_ref > 0) { | |
1649 | jbd_debug(4, "h_ref %d -> %d\n", handle->h_ref + 1, | |
1650 | handle->h_ref); | |
1651 | return err; | |
1652 | } | |
1653 | ||
1654 | jbd_debug(4, "Handle %p going down\n", handle); | |
343d9c28 | 1655 | trace_jbd2_handle_stats(journal->j_fs_dev->bd_dev, |
41a5b913 | 1656 | transaction->t_tid, |
343d9c28 TT |
1657 | handle->h_type, handle->h_line_no, |
1658 | jiffies - handle->h_start_jiffies, | |
1659 | handle->h_sync, handle->h_requested_credits, | |
1660 | (handle->h_requested_credits - | |
1661 | handle->h_buffer_credits)); | |
470decc6 DK |
1662 | |
1663 | /* | |
1664 | * Implement synchronous transaction batching. If the handle | |
1665 | * was synchronous, don't force a commit immediately. Let's | |
e07f7183 JB |
1666 | * yield and let another thread piggyback onto this |
1667 | * transaction. Keep doing that while new threads continue to | |
1668 | * arrive. It doesn't cost much - we're about to run a commit | |
1669 | * and sleep on IO anyway. Speeds up many-threaded, many-dir | |
1670 | * operations by 30x or more... | |
1671 | * | |
1672 | * We try and optimize the sleep time against what the | |
1673 | * underlying disk can do, instead of having a static sleep | |
1674 | * time. This is useful for the case where our storage is so | |
1675 | * fast that it is more optimal to go ahead and force a flush | |
1676 | * and wait for the transaction to be committed than it is to | |
1677 | * wait for an arbitrary amount of time for new writers to | |
1678 | * join the transaction. We achieve this by measuring how | |
1679 | * long it takes to commit a transaction, and compare it with | |
1680 | * how long this transaction has been running, and if run time | |
1681 | * < commit time then we sleep for the delta and commit. This | |
1682 | * greatly helps super fast disks that would see slowdowns as | |
1683 | * more threads started doing fsyncs. | |
470decc6 | 1684 | * |
e07f7183 JB |
1685 | * But don't do this if this process was the most recent one |
1686 | * to perform a synchronous write. We do this to detect the | |
1687 | * case where a single process is doing a stream of sync | |
1688 | * writes. No point in waiting for joiners in that case. | |
5dd21424 ES |
1689 | * |
1690 | * Setting max_batch_time to 0 disables this completely. | |
470decc6 DK |
1691 | */ |
1692 | pid = current->pid; | |
5dd21424 ES |
1693 | if (handle->h_sync && journal->j_last_sync_writer != pid && |
1694 | journal->j_max_batch_time) { | |
e07f7183 JB |
1695 | u64 commit_time, trans_time; |
1696 | ||
470decc6 | 1697 | journal->j_last_sync_writer = pid; |
e07f7183 | 1698 | |
a931da6a | 1699 | read_lock(&journal->j_state_lock); |
e07f7183 | 1700 | commit_time = journal->j_average_commit_time; |
a931da6a | 1701 | read_unlock(&journal->j_state_lock); |
e07f7183 JB |
1702 | |
1703 | trans_time = ktime_to_ns(ktime_sub(ktime_get(), | |
1704 | transaction->t_start_time)); | |
1705 | ||
30773840 TT |
1706 | commit_time = max_t(u64, commit_time, |
1707 | 1000*journal->j_min_batch_time); | |
e07f7183 | 1708 | commit_time = min_t(u64, commit_time, |
30773840 | 1709 | 1000*journal->j_max_batch_time); |
e07f7183 JB |
1710 | |
1711 | if (trans_time < commit_time) { | |
1712 | ktime_t expires = ktime_add_ns(ktime_get(), | |
1713 | commit_time); | |
1714 | set_current_state(TASK_UNINTERRUPTIBLE); | |
1715 | schedule_hrtimeout(&expires, HRTIMER_MODE_ABS); | |
1716 | } | |
470decc6 DK |
1717 | } |
1718 | ||
7058548c TT |
1719 | if (handle->h_sync) |
1720 | transaction->t_synchronous_commit = 1; | |
470decc6 | 1721 | current->journal_info = NULL; |
a51dca9c TT |
1722 | atomic_sub(handle->h_buffer_credits, |
1723 | &transaction->t_outstanding_credits); | |
470decc6 DK |
1724 | |
1725 | /* | |
1726 | * If the handle is marked SYNC, we need to set another commit | |
1727 | * going! We also want to force a commit if the current | |
1728 | * transaction is occupying too much of the log, or if the | |
1729 | * transaction is too old now. | |
1730 | */ | |
1731 | if (handle->h_sync || | |
a51dca9c TT |
1732 | (atomic_read(&transaction->t_outstanding_credits) > |
1733 | journal->j_max_transaction_buffers) || | |
1734 | time_after_eq(jiffies, transaction->t_expires)) { | |
470decc6 DK |
1735 | /* Do this even for aborted journals: an abort still |
1736 | * completes the commit thread, it just doesn't write | |
1737 | * anything to disk. */ | |
470decc6 | 1738 | |
470decc6 DK |
1739 | jbd_debug(2, "transaction too old, requesting commit for " |
1740 | "handle %p\n", handle); | |
1741 | /* This is non-blocking */ | |
c35a56a0 | 1742 | jbd2_log_start_commit(journal, transaction->t_tid); |
470decc6 DK |
1743 | |
1744 | /* | |
f7f4bccb | 1745 | * Special case: JBD2_SYNC synchronous updates require us |
470decc6 DK |
1746 | * to wait for the commit to complete. |
1747 | */ | |
1748 | if (handle->h_sync && !(current->flags & PF_MEMALLOC)) | |
a51dca9c | 1749 | wait_for_commit = 1; |
470decc6 DK |
1750 | } |
1751 | ||
a51dca9c TT |
1752 | /* |
1753 | * Once we drop t_updates, if it goes to zero the transaction | |
25985edc | 1754 | * could start committing on us and eventually disappear. So |
a51dca9c TT |
1755 | * once we do this, we must not dereference transaction |
1756 | * pointer again. | |
1757 | */ | |
1758 | tid = transaction->t_tid; | |
1759 | if (atomic_dec_and_test(&transaction->t_updates)) { | |
1760 | wake_up(&journal->j_wait_updates); | |
1761 | if (journal->j_barrier_count) | |
1762 | wake_up(&journal->j_wait_transaction_locked); | |
1763 | } | |
1764 | ||
ab714aff | 1765 | rwsem_release(&journal->j_trans_commit_map, 1, _THIS_IP_); |
7a4b188f | 1766 | |
a51dca9c TT |
1767 | if (wait_for_commit) |
1768 | err = jbd2_log_wait_commit(journal, tid); | |
1769 | ||
8f7d89f3 JK |
1770 | if (handle->h_rsv_handle) |
1771 | jbd2_journal_free_reserved(handle->h_rsv_handle); | |
41a5b913 | 1772 | free_and_exit: |
81378da6 MH |
1773 | /* |
1774 | * Scope of the GFP_NOFS context is over here and so we can restore the | |
1775 | * original alloc context. | |
1776 | */ | |
1777 | memalloc_nofs_restore(handle->saved_alloc_context); | |
af1e76d6 | 1778 | jbd2_free_handle(handle); |
470decc6 DK |
1779 | return err; |
1780 | } | |
1781 | ||
470decc6 DK |
1782 | /* |
1783 | * | |
1784 | * List management code snippets: various functions for manipulating the | |
1785 | * transaction buffer lists. | |
1786 | * | |
1787 | */ | |
1788 | ||
1789 | /* | |
1790 | * Append a buffer to a transaction list, given the transaction's list head | |
1791 | * pointer. | |
1792 | * | |
1793 | * j_list_lock is held. | |
1794 | * | |
1795 | * jbd_lock_bh_state(jh2bh(jh)) is held. | |
1796 | */ | |
1797 | ||
1798 | static inline void | |
1799 | __blist_add_buffer(struct journal_head **list, struct journal_head *jh) | |
1800 | { | |
1801 | if (!*list) { | |
1802 | jh->b_tnext = jh->b_tprev = jh; | |
1803 | *list = jh; | |
1804 | } else { | |
1805 | /* Insert at the tail of the list to preserve order */ | |
1806 | struct journal_head *first = *list, *last = first->b_tprev; | |
1807 | jh->b_tprev = last; | |
1808 | jh->b_tnext = first; | |
1809 | last->b_tnext = first->b_tprev = jh; | |
1810 | } | |
1811 | } | |
1812 | ||
1813 | /* | |
1814 | * Remove a buffer from a transaction list, given the transaction's list | |
1815 | * head pointer. | |
1816 | * | |
1817 | * Called with j_list_lock held, and the journal may not be locked. | |
1818 | * | |
1819 | * jbd_lock_bh_state(jh2bh(jh)) is held. | |
1820 | */ | |
1821 | ||
1822 | static inline void | |
1823 | __blist_del_buffer(struct journal_head **list, struct journal_head *jh) | |
1824 | { | |
1825 | if (*list == jh) { | |
1826 | *list = jh->b_tnext; | |
1827 | if (*list == jh) | |
1828 | *list = NULL; | |
1829 | } | |
1830 | jh->b_tprev->b_tnext = jh->b_tnext; | |
1831 | jh->b_tnext->b_tprev = jh->b_tprev; | |
1832 | } | |
1833 | ||
1834 | /* | |
1835 | * Remove a buffer from the appropriate transaction list. | |
1836 | * | |
1837 | * Note that this function can *change* the value of | |
f5113eff JK |
1838 | * bh->b_transaction->t_buffers, t_forget, t_shadow_list, t_log_list or |
1839 | * t_reserved_list. If the caller is holding onto a copy of one of these | |
1840 | * pointers, it could go bad. Generally the caller needs to re-read the | |
1841 | * pointer from the transaction_t. | |
470decc6 | 1842 | * |
5bebccf9 | 1843 | * Called under j_list_lock. |
470decc6 | 1844 | */ |
5bebccf9 | 1845 | static void __jbd2_journal_temp_unlink_buffer(struct journal_head *jh) |
470decc6 DK |
1846 | { |
1847 | struct journal_head **list = NULL; | |
1848 | transaction_t *transaction; | |
1849 | struct buffer_head *bh = jh2bh(jh); | |
1850 | ||
1851 | J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh)); | |
1852 | transaction = jh->b_transaction; | |
1853 | if (transaction) | |
1854 | assert_spin_locked(&transaction->t_journal->j_list_lock); | |
1855 | ||
1856 | J_ASSERT_JH(jh, jh->b_jlist < BJ_Types); | |
1857 | if (jh->b_jlist != BJ_None) | |
4019191b | 1858 | J_ASSERT_JH(jh, transaction != NULL); |
470decc6 DK |
1859 | |
1860 | switch (jh->b_jlist) { | |
1861 | case BJ_None: | |
1862 | return; | |
470decc6 DK |
1863 | case BJ_Metadata: |
1864 | transaction->t_nr_buffers--; | |
1865 | J_ASSERT_JH(jh, transaction->t_nr_buffers >= 0); | |
1866 | list = &transaction->t_buffers; | |
1867 | break; | |
1868 | case BJ_Forget: | |
1869 | list = &transaction->t_forget; | |
1870 | break; | |
470decc6 DK |
1871 | case BJ_Shadow: |
1872 | list = &transaction->t_shadow_list; | |
1873 | break; | |
470decc6 DK |
1874 | case BJ_Reserved: |
1875 | list = &transaction->t_reserved_list; | |
1876 | break; | |
470decc6 DK |
1877 | } |
1878 | ||
1879 | __blist_del_buffer(list, jh); | |
1880 | jh->b_jlist = BJ_None; | |
e112666b TT |
1881 | if (transaction && is_journal_aborted(transaction->t_journal)) |
1882 | clear_buffer_jbddirty(bh); | |
1883 | else if (test_clear_buffer_jbddirty(bh)) | |
470decc6 DK |
1884 | mark_buffer_dirty(bh); /* Expose it to the VM */ |
1885 | } | |
1886 | ||
de1b7941 JK |
1887 | /* |
1888 | * Remove buffer from all transactions. | |
1889 | * | |
1890 | * Called with bh_state lock and j_list_lock | |
1891 | * | |
1892 | * jh and bh may be already freed when this function returns. | |
1893 | */ | |
1894 | static void __jbd2_journal_unfile_buffer(struct journal_head *jh) | |
470decc6 | 1895 | { |
f7f4bccb | 1896 | __jbd2_journal_temp_unlink_buffer(jh); |
470decc6 | 1897 | jh->b_transaction = NULL; |
de1b7941 | 1898 | jbd2_journal_put_journal_head(jh); |
470decc6 DK |
1899 | } |
1900 | ||
f7f4bccb | 1901 | void jbd2_journal_unfile_buffer(journal_t *journal, struct journal_head *jh) |
470decc6 | 1902 | { |
de1b7941 JK |
1903 | struct buffer_head *bh = jh2bh(jh); |
1904 | ||
1905 | /* Get reference so that buffer cannot be freed before we unlock it */ | |
1906 | get_bh(bh); | |
1907 | jbd_lock_bh_state(bh); | |
470decc6 | 1908 | spin_lock(&journal->j_list_lock); |
f7f4bccb | 1909 | __jbd2_journal_unfile_buffer(jh); |
470decc6 | 1910 | spin_unlock(&journal->j_list_lock); |
de1b7941 JK |
1911 | jbd_unlock_bh_state(bh); |
1912 | __brelse(bh); | |
470decc6 DK |
1913 | } |
1914 | ||
1915 | /* | |
f7f4bccb | 1916 | * Called from jbd2_journal_try_to_free_buffers(). |
470decc6 DK |
1917 | * |
1918 | * Called under jbd_lock_bh_state(bh) | |
1919 | */ | |
1920 | static void | |
1921 | __journal_try_to_free_buffer(journal_t *journal, struct buffer_head *bh) | |
1922 | { | |
1923 | struct journal_head *jh; | |
1924 | ||
1925 | jh = bh2jh(bh); | |
1926 | ||
1927 | if (buffer_locked(bh) || buffer_dirty(bh)) | |
1928 | goto out; | |
1929 | ||
d2eb0b99 | 1930 | if (jh->b_next_transaction != NULL || jh->b_transaction != NULL) |
470decc6 DK |
1931 | goto out; |
1932 | ||
1933 | spin_lock(&journal->j_list_lock); | |
d2eb0b99 | 1934 | if (jh->b_cp_transaction != NULL) { |
470decc6 | 1935 | /* written-back checkpointed metadata buffer */ |
c254c9ec JK |
1936 | JBUFFER_TRACE(jh, "remove from checkpoint list"); |
1937 | __jbd2_journal_remove_checkpoint(jh); | |
470decc6 DK |
1938 | } |
1939 | spin_unlock(&journal->j_list_lock); | |
1940 | out: | |
1941 | return; | |
1942 | } | |
1943 | ||
470decc6 | 1944 | /** |
f7f4bccb | 1945 | * int jbd2_journal_try_to_free_buffers() - try to free page buffers. |
470decc6 DK |
1946 | * @journal: journal for operation |
1947 | * @page: to try and free | |
530576bb | 1948 | * @gfp_mask: we use the mask to detect how hard should we try to release |
d0164adc MG |
1949 | * buffers. If __GFP_DIRECT_RECLAIM and __GFP_FS is set, we wait for commit |
1950 | * code to release the buffers. | |
470decc6 DK |
1951 | * |
1952 | * | |
1953 | * For all the buffers on this page, | |
1954 | * if they are fully written out ordered data, move them onto BUF_CLEAN | |
1955 | * so try_to_free_buffers() can reap them. | |
1956 | * | |
1957 | * This function returns non-zero if we wish try_to_free_buffers() | |
1958 | * to be called. We do this if the page is releasable by try_to_free_buffers(). | |
1959 | * We also do it if the page has locked or dirty buffers and the caller wants | |
1960 | * us to perform sync or async writeout. | |
1961 | * | |
1962 | * This complicates JBD locking somewhat. We aren't protected by the | |
1963 | * BKL here. We wish to remove the buffer from its committing or | |
f7f4bccb | 1964 | * running transaction's ->t_datalist via __jbd2_journal_unfile_buffer. |
470decc6 DK |
1965 | * |
1966 | * This may *change* the value of transaction_t->t_datalist, so anyone | |
1967 | * who looks at t_datalist needs to lock against this function. | |
1968 | * | |
f7f4bccb MC |
1969 | * Even worse, someone may be doing a jbd2_journal_dirty_data on this |
1970 | * buffer. So we need to lock against that. jbd2_journal_dirty_data() | |
470decc6 DK |
1971 | * will come out of the lock with the buffer dirty, which makes it |
1972 | * ineligible for release here. | |
1973 | * | |
1974 | * Who else is affected by this? hmm... Really the only contender | |
1975 | * is do_get_write_access() - it could be looking at the buffer while | |
1976 | * journal_try_to_free_buffer() is changing its state. But that | |
1977 | * cannot happen because we never reallocate freed data as metadata | |
1978 | * while the data is part of a transaction. Yes? | |
530576bb MC |
1979 | * |
1980 | * Return 0 on failure, 1 on success | |
470decc6 | 1981 | */ |
f7f4bccb | 1982 | int jbd2_journal_try_to_free_buffers(journal_t *journal, |
530576bb | 1983 | struct page *page, gfp_t gfp_mask) |
470decc6 DK |
1984 | { |
1985 | struct buffer_head *head; | |
1986 | struct buffer_head *bh; | |
1987 | int ret = 0; | |
1988 | ||
1989 | J_ASSERT(PageLocked(page)); | |
1990 | ||
1991 | head = page_buffers(page); | |
1992 | bh = head; | |
1993 | do { | |
1994 | struct journal_head *jh; | |
1995 | ||
1996 | /* | |
1997 | * We take our own ref against the journal_head here to avoid | |
1998 | * having to add tons of locking around each instance of | |
530576bb | 1999 | * jbd2_journal_put_journal_head(). |
470decc6 | 2000 | */ |
f7f4bccb | 2001 | jh = jbd2_journal_grab_journal_head(bh); |
470decc6 DK |
2002 | if (!jh) |
2003 | continue; | |
2004 | ||
2005 | jbd_lock_bh_state(bh); | |
2006 | __journal_try_to_free_buffer(journal, bh); | |
f7f4bccb | 2007 | jbd2_journal_put_journal_head(jh); |
470decc6 DK |
2008 | jbd_unlock_bh_state(bh); |
2009 | if (buffer_jbd(bh)) | |
2010 | goto busy; | |
2011 | } while ((bh = bh->b_this_page) != head); | |
530576bb | 2012 | |
470decc6 | 2013 | ret = try_to_free_buffers(page); |
530576bb | 2014 | |
470decc6 DK |
2015 | busy: |
2016 | return ret; | |
2017 | } | |
2018 | ||
2019 | /* | |
2020 | * This buffer is no longer needed. If it is on an older transaction's | |
2021 | * checkpoint list we need to record it on this transaction's forget list | |
2022 | * to pin this buffer (and hence its checkpointing transaction) down until | |
2023 | * this transaction commits. If the buffer isn't on a checkpoint list, we | |
2024 | * release it. | |
2025 | * Returns non-zero if JBD no longer has an interest in the buffer. | |
2026 | * | |
2027 | * Called under j_list_lock. | |
2028 | * | |
2029 | * Called under jbd_lock_bh_state(bh). | |
2030 | */ | |
2031 | static int __dispose_buffer(struct journal_head *jh, transaction_t *transaction) | |
2032 | { | |
2033 | int may_free = 1; | |
2034 | struct buffer_head *bh = jh2bh(jh); | |
2035 | ||
470decc6 DK |
2036 | if (jh->b_cp_transaction) { |
2037 | JBUFFER_TRACE(jh, "on running+cp transaction"); | |
de1b7941 | 2038 | __jbd2_journal_temp_unlink_buffer(jh); |
f91d1d04 JK |
2039 | /* |
2040 | * We don't want to write the buffer anymore, clear the | |
2041 | * bit so that we don't confuse checks in | |
2042 | * __journal_file_buffer | |
2043 | */ | |
2044 | clear_buffer_dirty(bh); | |
f7f4bccb | 2045 | __jbd2_journal_file_buffer(jh, transaction, BJ_Forget); |
470decc6 DK |
2046 | may_free = 0; |
2047 | } else { | |
2048 | JBUFFER_TRACE(jh, "on running transaction"); | |
de1b7941 | 2049 | __jbd2_journal_unfile_buffer(jh); |
470decc6 DK |
2050 | } |
2051 | return may_free; | |
2052 | } | |
2053 | ||
2054 | /* | |
f7f4bccb | 2055 | * jbd2_journal_invalidatepage |
470decc6 DK |
2056 | * |
2057 | * This code is tricky. It has a number of cases to deal with. | |
2058 | * | |
2059 | * There are two invariants which this code relies on: | |
2060 | * | |
2061 | * i_size must be updated on disk before we start calling invalidatepage on the | |
2062 | * data. | |
2063 | * | |
2064 | * This is done in ext3 by defining an ext3_setattr method which | |
2065 | * updates i_size before truncate gets going. By maintaining this | |
2066 | * invariant, we can be sure that it is safe to throw away any buffers | |
2067 | * attached to the current transaction: once the transaction commits, | |
2068 | * we know that the data will not be needed. | |
2069 | * | |
2070 | * Note however that we can *not* throw away data belonging to the | |
2071 | * previous, committing transaction! | |
2072 | * | |
2073 | * Any disk blocks which *are* part of the previous, committing | |
2074 | * transaction (and which therefore cannot be discarded immediately) are | |
2075 | * not going to be reused in the new running transaction | |
2076 | * | |
2077 | * The bitmap committed_data images guarantee this: any block which is | |
2078 | * allocated in one transaction and removed in the next will be marked | |
2079 | * as in-use in the committed_data bitmap, so cannot be reused until | |
2080 | * the next transaction to delete the block commits. This means that | |
2081 | * leaving committing buffers dirty is quite safe: the disk blocks | |
2082 | * cannot be reallocated to a different file and so buffer aliasing is | |
2083 | * not possible. | |
2084 | * | |
2085 | * | |
2086 | * The above applies mainly to ordered data mode. In writeback mode we | |
2087 | * don't make guarantees about the order in which data hits disk --- in | |
2088 | * particular we don't guarantee that new dirty data is flushed before | |
2089 | * transaction commit --- so it is always safe just to discard data | |
2090 | * immediately in that mode. --sct | |
2091 | */ | |
2092 | ||
2093 | /* | |
2094 | * The journal_unmap_buffer helper function returns zero if the buffer | |
2095 | * concerned remains pinned as an anonymous buffer belonging to an older | |
2096 | * transaction. | |
2097 | * | |
2098 | * We're outside-transaction here. Either or both of j_running_transaction | |
2099 | * and j_committing_transaction may be NULL. | |
2100 | */ | |
b794e7a6 JK |
2101 | static int journal_unmap_buffer(journal_t *journal, struct buffer_head *bh, |
2102 | int partial_page) | |
470decc6 DK |
2103 | { |
2104 | transaction_t *transaction; | |
2105 | struct journal_head *jh; | |
2106 | int may_free = 1; | |
470decc6 DK |
2107 | |
2108 | BUFFER_TRACE(bh, "entry"); | |
2109 | ||
2110 | /* | |
2111 | * It is safe to proceed here without the j_list_lock because the | |
2112 | * buffers cannot be stolen by try_to_free_buffers as long as we are | |
2113 | * holding the page lock. --sct | |
2114 | */ | |
2115 | ||
2116 | if (!buffer_jbd(bh)) | |
2117 | goto zap_buffer_unlocked; | |
2118 | ||
87c89c23 | 2119 | /* OK, we have data buffer in journaled mode */ |
a931da6a | 2120 | write_lock(&journal->j_state_lock); |
470decc6 DK |
2121 | jbd_lock_bh_state(bh); |
2122 | spin_lock(&journal->j_list_lock); | |
2123 | ||
f7f4bccb | 2124 | jh = jbd2_journal_grab_journal_head(bh); |
470decc6 DK |
2125 | if (!jh) |
2126 | goto zap_buffer_no_jh; | |
2127 | ||
ba869023 | 2128 | /* |
2129 | * We cannot remove the buffer from checkpoint lists until the | |
2130 | * transaction adding inode to orphan list (let's call it T) | |
2131 | * is committed. Otherwise if the transaction changing the | |
2132 | * buffer would be cleaned from the journal before T is | |
2133 | * committed, a crash will cause that the correct contents of | |
2134 | * the buffer will be lost. On the other hand we have to | |
2135 | * clear the buffer dirty bit at latest at the moment when the | |
2136 | * transaction marking the buffer as freed in the filesystem | |
2137 | * structures is committed because from that moment on the | |
b794e7a6 | 2138 | * block can be reallocated and used by a different page. |
ba869023 | 2139 | * Since the block hasn't been freed yet but the inode has |
2140 | * already been added to orphan list, it is safe for us to add | |
2141 | * the buffer to BJ_Forget list of the newest transaction. | |
b794e7a6 JK |
2142 | * |
2143 | * Also we have to clear buffer_mapped flag of a truncated buffer | |
2144 | * because the buffer_head may be attached to the page straddling | |
2145 | * i_size (can happen only when blocksize < pagesize) and thus the | |
2146 | * buffer_head can be reused when the file is extended again. So we end | |
2147 | * up keeping around invalidated buffers attached to transactions' | |
2148 | * BJ_Forget list just to stop checkpointing code from cleaning up | |
2149 | * the transaction this buffer was modified in. | |
ba869023 | 2150 | */ |
470decc6 DK |
2151 | transaction = jh->b_transaction; |
2152 | if (transaction == NULL) { | |
2153 | /* First case: not on any transaction. If it | |
2154 | * has no checkpoint link, then we can zap it: | |
2155 | * it's a writeback-mode buffer so we don't care | |
2156 | * if it hits disk safely. */ | |
2157 | if (!jh->b_cp_transaction) { | |
2158 | JBUFFER_TRACE(jh, "not on any transaction: zap"); | |
2159 | goto zap_buffer; | |
2160 | } | |
2161 | ||
2162 | if (!buffer_dirty(bh)) { | |
2163 | /* bdflush has written it. We can drop it now */ | |
bc23f0c8 | 2164 | __jbd2_journal_remove_checkpoint(jh); |
470decc6 DK |
2165 | goto zap_buffer; |
2166 | } | |
2167 | ||
2168 | /* OK, it must be in the journal but still not | |
2169 | * written fully to disk: it's metadata or | |
2170 | * journaled data... */ | |
2171 | ||
2172 | if (journal->j_running_transaction) { | |
2173 | /* ... and once the current transaction has | |
2174 | * committed, the buffer won't be needed any | |
2175 | * longer. */ | |
2176 | JBUFFER_TRACE(jh, "checkpointed: add to BJ_Forget"); | |
b794e7a6 | 2177 | may_free = __dispose_buffer(jh, |
470decc6 | 2178 | journal->j_running_transaction); |
b794e7a6 | 2179 | goto zap_buffer; |
470decc6 DK |
2180 | } else { |
2181 | /* There is no currently-running transaction. So the | |
2182 | * orphan record which we wrote for this file must have | |
2183 | * passed into commit. We must attach this buffer to | |
2184 | * the committing transaction, if it exists. */ | |
2185 | if (journal->j_committing_transaction) { | |
2186 | JBUFFER_TRACE(jh, "give to committing trans"); | |
b794e7a6 | 2187 | may_free = __dispose_buffer(jh, |
470decc6 | 2188 | journal->j_committing_transaction); |
b794e7a6 | 2189 | goto zap_buffer; |
470decc6 DK |
2190 | } else { |
2191 | /* The orphan record's transaction has | |
2192 | * committed. We can cleanse this buffer */ | |
2193 | clear_buffer_jbddirty(bh); | |
bc23f0c8 | 2194 | __jbd2_journal_remove_checkpoint(jh); |
470decc6 DK |
2195 | goto zap_buffer; |
2196 | } | |
2197 | } | |
2198 | } else if (transaction == journal->j_committing_transaction) { | |
9b57988d | 2199 | JBUFFER_TRACE(jh, "on committing transaction"); |
470decc6 | 2200 | /* |
ba869023 | 2201 | * The buffer is committing, we simply cannot touch |
b794e7a6 JK |
2202 | * it. If the page is straddling i_size we have to wait |
2203 | * for commit and try again. | |
2204 | */ | |
2205 | if (partial_page) { | |
b794e7a6 JK |
2206 | jbd2_journal_put_journal_head(jh); |
2207 | spin_unlock(&journal->j_list_lock); | |
2208 | jbd_unlock_bh_state(bh); | |
2209 | write_unlock(&journal->j_state_lock); | |
53e87268 | 2210 | return -EBUSY; |
b794e7a6 JK |
2211 | } |
2212 | /* | |
2213 | * OK, buffer won't be reachable after truncate. We just set | |
2214 | * j_next_transaction to the running transaction (if there is | |
2215 | * one) and mark buffer as freed so that commit code knows it | |
2216 | * should clear dirty bits when it is done with the buffer. | |
ba869023 | 2217 | */ |
470decc6 | 2218 | set_buffer_freed(bh); |
ba869023 | 2219 | if (journal->j_running_transaction && buffer_jbddirty(bh)) |
2220 | jh->b_next_transaction = journal->j_running_transaction; | |
f7f4bccb | 2221 | jbd2_journal_put_journal_head(jh); |
470decc6 DK |
2222 | spin_unlock(&journal->j_list_lock); |
2223 | jbd_unlock_bh_state(bh); | |
a931da6a | 2224 | write_unlock(&journal->j_state_lock); |
470decc6 DK |
2225 | return 0; |
2226 | } else { | |
2227 | /* Good, the buffer belongs to the running transaction. | |
2228 | * We are writing our own transaction's data, not any | |
2229 | * previous one's, so it is safe to throw it away | |
2230 | * (remember that we expect the filesystem to have set | |
2231 | * i_size already for this truncate so recovery will not | |
2232 | * expose the disk blocks we are discarding here.) */ | |
2233 | J_ASSERT_JH(jh, transaction == journal->j_running_transaction); | |
9b57988d | 2234 | JBUFFER_TRACE(jh, "on running transaction"); |
470decc6 DK |
2235 | may_free = __dispose_buffer(jh, transaction); |
2236 | } | |
2237 | ||
2238 | zap_buffer: | |
b794e7a6 JK |
2239 | /* |
2240 | * This is tricky. Although the buffer is truncated, it may be reused | |
2241 | * if blocksize < pagesize and it is attached to the page straddling | |
2242 | * EOF. Since the buffer might have been added to BJ_Forget list of the | |
2243 | * running transaction, journal_get_write_access() won't clear | |
2244 | * b_modified and credit accounting gets confused. So clear b_modified | |
2245 | * here. | |
2246 | */ | |
2247 | jh->b_modified = 0; | |
f7f4bccb | 2248 | jbd2_journal_put_journal_head(jh); |
470decc6 DK |
2249 | zap_buffer_no_jh: |
2250 | spin_unlock(&journal->j_list_lock); | |
2251 | jbd_unlock_bh_state(bh); | |
a931da6a | 2252 | write_unlock(&journal->j_state_lock); |
470decc6 DK |
2253 | zap_buffer_unlocked: |
2254 | clear_buffer_dirty(bh); | |
2255 | J_ASSERT_BH(bh, !buffer_jbddirty(bh)); | |
2256 | clear_buffer_mapped(bh); | |
2257 | clear_buffer_req(bh); | |
2258 | clear_buffer_new(bh); | |
15291164 ES |
2259 | clear_buffer_delay(bh); |
2260 | clear_buffer_unwritten(bh); | |
470decc6 DK |
2261 | bh->b_bdev = NULL; |
2262 | return may_free; | |
2263 | } | |
2264 | ||
2265 | /** | |
f7f4bccb | 2266 | * void jbd2_journal_invalidatepage() |
470decc6 DK |
2267 | * @journal: journal to use for flush... |
2268 | * @page: page to flush | |
259709b0 LC |
2269 | * @offset: start of the range to invalidate |
2270 | * @length: length of the range to invalidate | |
470decc6 | 2271 | * |
259709b0 LC |
2272 | * Reap page buffers containing data after in the specified range in page. |
2273 | * Can return -EBUSY if buffers are part of the committing transaction and | |
2274 | * the page is straddling i_size. Caller then has to wait for current commit | |
2275 | * and try again. | |
470decc6 | 2276 | */ |
53e87268 JK |
2277 | int jbd2_journal_invalidatepage(journal_t *journal, |
2278 | struct page *page, | |
259709b0 LC |
2279 | unsigned int offset, |
2280 | unsigned int length) | |
470decc6 DK |
2281 | { |
2282 | struct buffer_head *head, *bh, *next; | |
259709b0 | 2283 | unsigned int stop = offset + length; |
470decc6 | 2284 | unsigned int curr_off = 0; |
09cbfeaf | 2285 | int partial_page = (offset || length < PAGE_SIZE); |
470decc6 | 2286 | int may_free = 1; |
53e87268 | 2287 | int ret = 0; |
470decc6 DK |
2288 | |
2289 | if (!PageLocked(page)) | |
2290 | BUG(); | |
2291 | if (!page_has_buffers(page)) | |
53e87268 | 2292 | return 0; |
470decc6 | 2293 | |
09cbfeaf | 2294 | BUG_ON(stop > PAGE_SIZE || stop < length); |
259709b0 | 2295 | |
470decc6 DK |
2296 | /* We will potentially be playing with lists other than just the |
2297 | * data lists (especially for journaled data mode), so be | |
2298 | * cautious in our locking. */ | |
2299 | ||
2300 | head = bh = page_buffers(page); | |
2301 | do { | |
2302 | unsigned int next_off = curr_off + bh->b_size; | |
2303 | next = bh->b_this_page; | |
2304 | ||
259709b0 LC |
2305 | if (next_off > stop) |
2306 | return 0; | |
2307 | ||
470decc6 DK |
2308 | if (offset <= curr_off) { |
2309 | /* This block is wholly outside the truncation point */ | |
2310 | lock_buffer(bh); | |
259709b0 | 2311 | ret = journal_unmap_buffer(journal, bh, partial_page); |
470decc6 | 2312 | unlock_buffer(bh); |
53e87268 JK |
2313 | if (ret < 0) |
2314 | return ret; | |
2315 | may_free &= ret; | |
470decc6 DK |
2316 | } |
2317 | curr_off = next_off; | |
2318 | bh = next; | |
2319 | ||
2320 | } while (bh != head); | |
2321 | ||
259709b0 | 2322 | if (!partial_page) { |
470decc6 DK |
2323 | if (may_free && try_to_free_buffers(page)) |
2324 | J_ASSERT(!page_has_buffers(page)); | |
2325 | } | |
53e87268 | 2326 | return 0; |
470decc6 DK |
2327 | } |
2328 | ||
2329 | /* | |
2330 | * File a buffer on the given transaction list. | |
2331 | */ | |
f7f4bccb | 2332 | void __jbd2_journal_file_buffer(struct journal_head *jh, |
470decc6 DK |
2333 | transaction_t *transaction, int jlist) |
2334 | { | |
2335 | struct journal_head **list = NULL; | |
2336 | int was_dirty = 0; | |
2337 | struct buffer_head *bh = jh2bh(jh); | |
2338 | ||
2339 | J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh)); | |
2340 | assert_spin_locked(&transaction->t_journal->j_list_lock); | |
2341 | ||
2342 | J_ASSERT_JH(jh, jh->b_jlist < BJ_Types); | |
2343 | J_ASSERT_JH(jh, jh->b_transaction == transaction || | |
4019191b | 2344 | jh->b_transaction == NULL); |
470decc6 DK |
2345 | |
2346 | if (jh->b_transaction && jh->b_jlist == jlist) | |
2347 | return; | |
2348 | ||
470decc6 DK |
2349 | if (jlist == BJ_Metadata || jlist == BJ_Reserved || |
2350 | jlist == BJ_Shadow || jlist == BJ_Forget) { | |
f91d1d04 JK |
2351 | /* |
2352 | * For metadata buffers, we track dirty bit in buffer_jbddirty | |
2353 | * instead of buffer_dirty. We should not see a dirty bit set | |
2354 | * here because we clear it in do_get_write_access but e.g. | |
2355 | * tune2fs can modify the sb and set the dirty bit at any time | |
2356 | * so we try to gracefully handle that. | |
2357 | */ | |
2358 | if (buffer_dirty(bh)) | |
2359 | warn_dirty_buffer(bh); | |
470decc6 DK |
2360 | if (test_clear_buffer_dirty(bh) || |
2361 | test_clear_buffer_jbddirty(bh)) | |
2362 | was_dirty = 1; | |
2363 | } | |
2364 | ||
2365 | if (jh->b_transaction) | |
f7f4bccb | 2366 | __jbd2_journal_temp_unlink_buffer(jh); |
de1b7941 JK |
2367 | else |
2368 | jbd2_journal_grab_journal_head(bh); | |
470decc6 DK |
2369 | jh->b_transaction = transaction; |
2370 | ||
2371 | switch (jlist) { | |
2372 | case BJ_None: | |
2373 | J_ASSERT_JH(jh, !jh->b_committed_data); | |
2374 | J_ASSERT_JH(jh, !jh->b_frozen_data); | |
2375 | return; | |
470decc6 DK |
2376 | case BJ_Metadata: |
2377 | transaction->t_nr_buffers++; | |
2378 | list = &transaction->t_buffers; | |
2379 | break; | |
2380 | case BJ_Forget: | |
2381 | list = &transaction->t_forget; | |
2382 | break; | |
470decc6 DK |
2383 | case BJ_Shadow: |
2384 | list = &transaction->t_shadow_list; | |
2385 | break; | |
470decc6 DK |
2386 | case BJ_Reserved: |
2387 | list = &transaction->t_reserved_list; | |
2388 | break; | |
470decc6 DK |
2389 | } |
2390 | ||
2391 | __blist_add_buffer(list, jh); | |
2392 | jh->b_jlist = jlist; | |
2393 | ||
2394 | if (was_dirty) | |
2395 | set_buffer_jbddirty(bh); | |
2396 | } | |
2397 | ||
f7f4bccb | 2398 | void jbd2_journal_file_buffer(struct journal_head *jh, |
470decc6 DK |
2399 | transaction_t *transaction, int jlist) |
2400 | { | |
2401 | jbd_lock_bh_state(jh2bh(jh)); | |
2402 | spin_lock(&transaction->t_journal->j_list_lock); | |
f7f4bccb | 2403 | __jbd2_journal_file_buffer(jh, transaction, jlist); |
470decc6 DK |
2404 | spin_unlock(&transaction->t_journal->j_list_lock); |
2405 | jbd_unlock_bh_state(jh2bh(jh)); | |
2406 | } | |
2407 | ||
2408 | /* | |
2409 | * Remove a buffer from its current buffer list in preparation for | |
2410 | * dropping it from its current transaction entirely. If the buffer has | |
2411 | * already started to be used by a subsequent transaction, refile the | |
2412 | * buffer on that transaction's metadata list. | |
2413 | * | |
de1b7941 | 2414 | * Called under j_list_lock |
470decc6 | 2415 | * Called under jbd_lock_bh_state(jh2bh(jh)) |
de1b7941 JK |
2416 | * |
2417 | * jh and bh may be already free when this function returns | |
470decc6 | 2418 | */ |
f7f4bccb | 2419 | void __jbd2_journal_refile_buffer(struct journal_head *jh) |
470decc6 | 2420 | { |
ba869023 | 2421 | int was_dirty, jlist; |
470decc6 DK |
2422 | struct buffer_head *bh = jh2bh(jh); |
2423 | ||
2424 | J_ASSERT_JH(jh, jbd_is_locked_bh_state(bh)); | |
2425 | if (jh->b_transaction) | |
2426 | assert_spin_locked(&jh->b_transaction->t_journal->j_list_lock); | |
2427 | ||
2428 | /* If the buffer is now unused, just drop it. */ | |
2429 | if (jh->b_next_transaction == NULL) { | |
f7f4bccb | 2430 | __jbd2_journal_unfile_buffer(jh); |
470decc6 DK |
2431 | return; |
2432 | } | |
2433 | ||
2434 | /* | |
2435 | * It has been modified by a later transaction: add it to the new | |
2436 | * transaction's metadata list. | |
2437 | */ | |
2438 | ||
2439 | was_dirty = test_clear_buffer_jbddirty(bh); | |
f7f4bccb | 2440 | __jbd2_journal_temp_unlink_buffer(jh); |
de1b7941 JK |
2441 | /* |
2442 | * We set b_transaction here because b_next_transaction will inherit | |
2443 | * our jh reference and thus __jbd2_journal_file_buffer() must not | |
2444 | * take a new one. | |
2445 | */ | |
470decc6 DK |
2446 | jh->b_transaction = jh->b_next_transaction; |
2447 | jh->b_next_transaction = NULL; | |
ba869023 | 2448 | if (buffer_freed(bh)) |
2449 | jlist = BJ_Forget; | |
2450 | else if (jh->b_modified) | |
2451 | jlist = BJ_Metadata; | |
2452 | else | |
2453 | jlist = BJ_Reserved; | |
2454 | __jbd2_journal_file_buffer(jh, jh->b_transaction, jlist); | |
470decc6 DK |
2455 | J_ASSERT_JH(jh, jh->b_transaction->t_state == T_RUNNING); |
2456 | ||
2457 | if (was_dirty) | |
2458 | set_buffer_jbddirty(bh); | |
2459 | } | |
2460 | ||
2461 | /* | |
de1b7941 JK |
2462 | * __jbd2_journal_refile_buffer() with necessary locking added. We take our |
2463 | * bh reference so that we can safely unlock bh. | |
2464 | * | |
2465 | * The jh and bh may be freed by this call. | |
470decc6 | 2466 | */ |
f7f4bccb | 2467 | void jbd2_journal_refile_buffer(journal_t *journal, struct journal_head *jh) |
470decc6 DK |
2468 | { |
2469 | struct buffer_head *bh = jh2bh(jh); | |
2470 | ||
de1b7941 JK |
2471 | /* Get reference so that buffer cannot be freed before we unlock it */ |
2472 | get_bh(bh); | |
470decc6 DK |
2473 | jbd_lock_bh_state(bh); |
2474 | spin_lock(&journal->j_list_lock); | |
f7f4bccb | 2475 | __jbd2_journal_refile_buffer(jh); |
470decc6 | 2476 | jbd_unlock_bh_state(bh); |
470decc6 DK |
2477 | spin_unlock(&journal->j_list_lock); |
2478 | __brelse(bh); | |
2479 | } | |
c851ed54 JK |
2480 | |
2481 | /* | |
2482 | * File inode in the inode list of the handle's transaction | |
2483 | */ | |
41617e1a JK |
2484 | static int jbd2_journal_file_inode(handle_t *handle, struct jbd2_inode *jinode, |
2485 | unsigned long flags) | |
c851ed54 JK |
2486 | { |
2487 | transaction_t *transaction = handle->h_transaction; | |
41a5b913 | 2488 | journal_t *journal; |
c851ed54 JK |
2489 | |
2490 | if (is_handle_aborted(handle)) | |
41a5b913 TT |
2491 | return -EROFS; |
2492 | journal = transaction->t_journal; | |
c851ed54 JK |
2493 | |
2494 | jbd_debug(4, "Adding inode %lu, tid:%d\n", jinode->i_vfs_inode->i_ino, | |
2495 | transaction->t_tid); | |
2496 | ||
2497 | /* | |
2498 | * First check whether inode isn't already on the transaction's | |
2499 | * lists without taking the lock. Note that this check is safe | |
2500 | * without the lock as we cannot race with somebody removing inode | |
2501 | * from the transaction. The reason is that we remove inode from the | |
2502 | * transaction only in journal_release_jbd_inode() and when we commit | |
2503 | * the transaction. We are guarded from the first case by holding | |
2504 | * a reference to the inode. We are safe against the second case | |
2505 | * because if jinode->i_transaction == transaction, commit code | |
2506 | * cannot touch the transaction because we hold reference to it, | |
2507 | * and if jinode->i_next_transaction == transaction, commit code | |
2508 | * will only file the inode where we want it. | |
2509 | */ | |
41617e1a JK |
2510 | if ((jinode->i_transaction == transaction || |
2511 | jinode->i_next_transaction == transaction) && | |
2512 | (jinode->i_flags & flags) == flags) | |
c851ed54 JK |
2513 | return 0; |
2514 | ||
2515 | spin_lock(&journal->j_list_lock); | |
41617e1a JK |
2516 | jinode->i_flags |= flags; |
2517 | /* Is inode already attached where we need it? */ | |
c851ed54 JK |
2518 | if (jinode->i_transaction == transaction || |
2519 | jinode->i_next_transaction == transaction) | |
2520 | goto done; | |
2521 | ||
81be12c8 JK |
2522 | /* |
2523 | * We only ever set this variable to 1 so the test is safe. Since | |
2524 | * t_need_data_flush is likely to be set, we do the test to save some | |
2525 | * cacheline bouncing | |
2526 | */ | |
2527 | if (!transaction->t_need_data_flush) | |
2528 | transaction->t_need_data_flush = 1; | |
c851ed54 JK |
2529 | /* On some different transaction's list - should be |
2530 | * the committing one */ | |
2531 | if (jinode->i_transaction) { | |
2532 | J_ASSERT(jinode->i_next_transaction == NULL); | |
2533 | J_ASSERT(jinode->i_transaction == | |
2534 | journal->j_committing_transaction); | |
2535 | jinode->i_next_transaction = transaction; | |
2536 | goto done; | |
2537 | } | |
2538 | /* Not on any transaction list... */ | |
2539 | J_ASSERT(!jinode->i_next_transaction); | |
2540 | jinode->i_transaction = transaction; | |
2541 | list_add(&jinode->i_list, &transaction->t_inode_list); | |
2542 | done: | |
2543 | spin_unlock(&journal->j_list_lock); | |
2544 | ||
2545 | return 0; | |
2546 | } | |
2547 | ||
41617e1a JK |
2548 | int jbd2_journal_inode_add_write(handle_t *handle, struct jbd2_inode *jinode) |
2549 | { | |
2550 | return jbd2_journal_file_inode(handle, jinode, | |
2551 | JI_WRITE_DATA | JI_WAIT_DATA); | |
2552 | } | |
2553 | ||
2554 | int jbd2_journal_inode_add_wait(handle_t *handle, struct jbd2_inode *jinode) | |
2555 | { | |
2556 | return jbd2_journal_file_inode(handle, jinode, JI_WAIT_DATA); | |
2557 | } | |
2558 | ||
c851ed54 | 2559 | /* |
7f5aa215 JK |
2560 | * File truncate and transaction commit interact with each other in a |
2561 | * non-trivial way. If a transaction writing data block A is | |
2562 | * committing, we cannot discard the data by truncate until we have | |
2563 | * written them. Otherwise if we crashed after the transaction with | |
2564 | * write has committed but before the transaction with truncate has | |
2565 | * committed, we could see stale data in block A. This function is a | |
2566 | * helper to solve this problem. It starts writeout of the truncated | |
2567 | * part in case it is in the committing transaction. | |
2568 | * | |
2569 | * Filesystem code must call this function when inode is journaled in | |
2570 | * ordered mode before truncation happens and after the inode has been | |
2571 | * placed on orphan list with the new inode size. The second condition | |
2572 | * avoids the race that someone writes new data and we start | |
2573 | * committing the transaction after this function has been called but | |
2574 | * before a transaction for truncate is started (and furthermore it | |
2575 | * allows us to optimize the case where the addition to orphan list | |
2576 | * happens in the same transaction as write --- we don't have to write | |
2577 | * any data in such case). | |
c851ed54 | 2578 | */ |
7f5aa215 JK |
2579 | int jbd2_journal_begin_ordered_truncate(journal_t *journal, |
2580 | struct jbd2_inode *jinode, | |
c851ed54 JK |
2581 | loff_t new_size) |
2582 | { | |
7f5aa215 | 2583 | transaction_t *inode_trans, *commit_trans; |
c851ed54 JK |
2584 | int ret = 0; |
2585 | ||
7f5aa215 JK |
2586 | /* This is a quick check to avoid locking if not necessary */ |
2587 | if (!jinode->i_transaction) | |
c851ed54 | 2588 | goto out; |
7f5aa215 JK |
2589 | /* Locks are here just to force reading of recent values, it is |
2590 | * enough that the transaction was not committing before we started | |
2591 | * a transaction adding the inode to orphan list */ | |
a931da6a | 2592 | read_lock(&journal->j_state_lock); |
c851ed54 | 2593 | commit_trans = journal->j_committing_transaction; |
a931da6a | 2594 | read_unlock(&journal->j_state_lock); |
7f5aa215 JK |
2595 | spin_lock(&journal->j_list_lock); |
2596 | inode_trans = jinode->i_transaction; | |
2597 | spin_unlock(&journal->j_list_lock); | |
2598 | if (inode_trans == commit_trans) { | |
2599 | ret = filemap_fdatawrite_range(jinode->i_vfs_inode->i_mapping, | |
c851ed54 JK |
2600 | new_size, LLONG_MAX); |
2601 | if (ret) | |
2602 | jbd2_journal_abort(journal, ret); | |
2603 | } | |
2604 | out: | |
2605 | return ret; | |
2606 | } |