]>
Commit | Line | Data |
---|---|---|
71e330b5 DC |
1 | /* |
2 | * Copyright (c) 2010 Red Hat, Inc. All Rights Reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it would be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program; if not, write the Free Software Foundation, | |
15 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | */ | |
17 | ||
18 | #include "xfs.h" | |
19 | #include "xfs_fs.h" | |
20 | #include "xfs_types.h" | |
21 | #include "xfs_bit.h" | |
22 | #include "xfs_log.h" | |
23 | #include "xfs_inum.h" | |
24 | #include "xfs_trans.h" | |
25 | #include "xfs_trans_priv.h" | |
26 | #include "xfs_log_priv.h" | |
27 | #include "xfs_sb.h" | |
28 | #include "xfs_ag.h" | |
71e330b5 DC |
29 | #include "xfs_mount.h" |
30 | #include "xfs_error.h" | |
31 | #include "xfs_alloc.h" | |
32 | ||
33 | /* | |
34 | * Perform initial CIL structure initialisation. If the CIL is not | |
35 | * enabled in this filesystem, ensure the log->l_cilp is null so | |
36 | * we can check this conditional to determine if we are doing delayed | |
37 | * logging or not. | |
38 | */ | |
39 | int | |
40 | xlog_cil_init( | |
41 | struct log *log) | |
42 | { | |
43 | struct xfs_cil *cil; | |
44 | struct xfs_cil_ctx *ctx; | |
45 | ||
46 | log->l_cilp = NULL; | |
47 | if (!(log->l_mp->m_flags & XFS_MOUNT_DELAYLOG)) | |
48 | return 0; | |
49 | ||
50 | cil = kmem_zalloc(sizeof(*cil), KM_SLEEP|KM_MAYFAIL); | |
51 | if (!cil) | |
52 | return ENOMEM; | |
53 | ||
54 | ctx = kmem_zalloc(sizeof(*ctx), KM_SLEEP|KM_MAYFAIL); | |
55 | if (!ctx) { | |
56 | kmem_free(cil); | |
57 | return ENOMEM; | |
58 | } | |
59 | ||
60 | INIT_LIST_HEAD(&cil->xc_cil); | |
61 | INIT_LIST_HEAD(&cil->xc_committing); | |
62 | spin_lock_init(&cil->xc_cil_lock); | |
63 | init_rwsem(&cil->xc_ctx_lock); | |
64 | sv_init(&cil->xc_commit_wait, SV_DEFAULT, "cilwait"); | |
65 | ||
66 | INIT_LIST_HEAD(&ctx->committing); | |
67 | INIT_LIST_HEAD(&ctx->busy_extents); | |
68 | ctx->sequence = 1; | |
69 | ctx->cil = cil; | |
70 | cil->xc_ctx = ctx; | |
71 | ||
72 | cil->xc_log = log; | |
73 | log->l_cilp = cil; | |
74 | return 0; | |
75 | } | |
76 | ||
77 | void | |
78 | xlog_cil_destroy( | |
79 | struct log *log) | |
80 | { | |
81 | if (!log->l_cilp) | |
82 | return; | |
83 | ||
84 | if (log->l_cilp->xc_ctx) { | |
85 | if (log->l_cilp->xc_ctx->ticket) | |
86 | xfs_log_ticket_put(log->l_cilp->xc_ctx->ticket); | |
87 | kmem_free(log->l_cilp->xc_ctx); | |
88 | } | |
89 | ||
90 | ASSERT(list_empty(&log->l_cilp->xc_cil)); | |
91 | kmem_free(log->l_cilp); | |
92 | } | |
93 | ||
94 | /* | |
95 | * Allocate a new ticket. Failing to get a new ticket makes it really hard to | |
96 | * recover, so we don't allow failure here. Also, we allocate in a context that | |
97 | * we don't want to be issuing transactions from, so we need to tell the | |
98 | * allocation code this as well. | |
99 | * | |
100 | * We don't reserve any space for the ticket - we are going to steal whatever | |
101 | * space we require from transactions as they commit. To ensure we reserve all | |
102 | * the space required, we need to set the current reservation of the ticket to | |
103 | * zero so that we know to steal the initial transaction overhead from the | |
104 | * first transaction commit. | |
105 | */ | |
106 | static struct xlog_ticket * | |
107 | xlog_cil_ticket_alloc( | |
108 | struct log *log) | |
109 | { | |
110 | struct xlog_ticket *tic; | |
111 | ||
112 | tic = xlog_ticket_alloc(log, 0, 1, XFS_TRANSACTION, 0, | |
113 | KM_SLEEP|KM_NOFS); | |
114 | tic->t_trans_type = XFS_TRANS_CHECKPOINT; | |
115 | ||
116 | /* | |
117 | * set the current reservation to zero so we know to steal the basic | |
118 | * transaction overhead reservation from the first transaction commit. | |
119 | */ | |
120 | tic->t_curr_res = 0; | |
121 | return tic; | |
122 | } | |
123 | ||
124 | /* | |
125 | * After the first stage of log recovery is done, we know where the head and | |
126 | * tail of the log are. We need this log initialisation done before we can | |
127 | * initialise the first CIL checkpoint context. | |
128 | * | |
129 | * Here we allocate a log ticket to track space usage during a CIL push. This | |
130 | * ticket is passed to xlog_write() directly so that we don't slowly leak log | |
131 | * space by failing to account for space used by log headers and additional | |
132 | * region headers for split regions. | |
133 | */ | |
134 | void | |
135 | xlog_cil_init_post_recovery( | |
136 | struct log *log) | |
137 | { | |
138 | if (!log->l_cilp) | |
139 | return; | |
140 | ||
141 | log->l_cilp->xc_ctx->ticket = xlog_cil_ticket_alloc(log); | |
142 | log->l_cilp->xc_ctx->sequence = 1; | |
143 | log->l_cilp->xc_ctx->commit_lsn = xlog_assign_lsn(log->l_curr_cycle, | |
144 | log->l_curr_block); | |
145 | } | |
146 | ||
147 | /* | |
148 | * Insert the log item into the CIL and calculate the difference in space | |
149 | * consumed by the item. Add the space to the checkpoint ticket and calculate | |
150 | * if the change requires additional log metadata. If it does, take that space | |
151 | * as well. Remove the amount of space we addded to the checkpoint ticket from | |
152 | * the current transaction ticket so that the accounting works out correctly. | |
153 | * | |
154 | * If this is the first time the item is being placed into the CIL in this | |
155 | * context, pin it so it can't be written to disk until the CIL is flushed to | |
156 | * the iclog and the iclog written to disk. | |
157 | */ | |
158 | static void | |
159 | xlog_cil_insert( | |
160 | struct log *log, | |
161 | struct xlog_ticket *ticket, | |
162 | struct xfs_log_item *item, | |
163 | struct xfs_log_vec *lv) | |
164 | { | |
165 | struct xfs_cil *cil = log->l_cilp; | |
166 | struct xfs_log_vec *old = lv->lv_item->li_lv; | |
167 | struct xfs_cil_ctx *ctx = cil->xc_ctx; | |
168 | int len; | |
169 | int diff_iovecs; | |
170 | int iclog_space; | |
171 | ||
172 | if (old) { | |
173 | /* existing lv on log item, space used is a delta */ | |
174 | ASSERT(!list_empty(&item->li_cil)); | |
175 | ASSERT(old->lv_buf && old->lv_buf_len && old->lv_niovecs); | |
176 | ||
177 | len = lv->lv_buf_len - old->lv_buf_len; | |
178 | diff_iovecs = lv->lv_niovecs - old->lv_niovecs; | |
179 | kmem_free(old->lv_buf); | |
180 | kmem_free(old); | |
181 | } else { | |
182 | /* new lv, must pin the log item */ | |
183 | ASSERT(!lv->lv_item->li_lv); | |
184 | ASSERT(list_empty(&item->li_cil)); | |
185 | ||
186 | len = lv->lv_buf_len; | |
187 | diff_iovecs = lv->lv_niovecs; | |
188 | IOP_PIN(lv->lv_item); | |
189 | ||
190 | } | |
191 | len += diff_iovecs * sizeof(xlog_op_header_t); | |
192 | ||
193 | /* attach new log vector to log item */ | |
194 | lv->lv_item->li_lv = lv; | |
195 | ||
196 | spin_lock(&cil->xc_cil_lock); | |
197 | list_move_tail(&item->li_cil, &cil->xc_cil); | |
198 | ctx->nvecs += diff_iovecs; | |
199 | ||
ccf7c23f DC |
200 | /* |
201 | * If this is the first time the item is being committed to the CIL, | |
202 | * store the sequence number on the log item so we can tell | |
203 | * in future commits whether this is the first checkpoint the item is | |
204 | * being committed into. | |
205 | */ | |
206 | if (!item->li_seq) | |
207 | item->li_seq = ctx->sequence; | |
208 | ||
71e330b5 DC |
209 | /* |
210 | * Now transfer enough transaction reservation to the context ticket | |
211 | * for the checkpoint. The context ticket is special - the unit | |
212 | * reservation has to grow as well as the current reservation as we | |
213 | * steal from tickets so we can correctly determine the space used | |
214 | * during the transaction commit. | |
215 | */ | |
216 | if (ctx->ticket->t_curr_res == 0) { | |
217 | /* first commit in checkpoint, steal the header reservation */ | |
218 | ASSERT(ticket->t_curr_res >= ctx->ticket->t_unit_res + len); | |
219 | ctx->ticket->t_curr_res = ctx->ticket->t_unit_res; | |
220 | ticket->t_curr_res -= ctx->ticket->t_unit_res; | |
221 | } | |
222 | ||
223 | /* do we need space for more log record headers? */ | |
224 | iclog_space = log->l_iclog_size - log->l_iclog_hsize; | |
225 | if (len > 0 && (ctx->space_used / iclog_space != | |
226 | (ctx->space_used + len) / iclog_space)) { | |
227 | int hdrs; | |
228 | ||
229 | hdrs = (len + iclog_space - 1) / iclog_space; | |
230 | /* need to take into account split region headers, too */ | |
231 | hdrs *= log->l_iclog_hsize + sizeof(struct xlog_op_header); | |
232 | ctx->ticket->t_unit_res += hdrs; | |
233 | ctx->ticket->t_curr_res += hdrs; | |
234 | ticket->t_curr_res -= hdrs; | |
235 | ASSERT(ticket->t_curr_res >= len); | |
236 | } | |
237 | ticket->t_curr_res -= len; | |
238 | ctx->space_used += len; | |
239 | ||
240 | spin_unlock(&cil->xc_cil_lock); | |
241 | } | |
242 | ||
243 | /* | |
244 | * Format log item into a flat buffers | |
245 | * | |
246 | * For delayed logging, we need to hold a formatted buffer containing all the | |
247 | * changes on the log item. This enables us to relog the item in memory and | |
248 | * write it out asynchronously without needing to relock the object that was | |
249 | * modified at the time it gets written into the iclog. | |
250 | * | |
251 | * This function builds a vector for the changes in each log item in the | |
252 | * transaction. It then works out the length of the buffer needed for each log | |
253 | * item, allocates them and formats the vector for the item into the buffer. | |
254 | * The buffer is then attached to the log item are then inserted into the | |
255 | * Committed Item List for tracking until the next checkpoint is written out. | |
256 | * | |
257 | * We don't set up region headers during this process; we simply copy the | |
258 | * regions into the flat buffer. We can do this because we still have to do a | |
259 | * formatting step to write the regions into the iclog buffer. Writing the | |
260 | * ophdrs during the iclog write means that we can support splitting large | |
261 | * regions across iclog boundares without needing a change in the format of the | |
262 | * item/region encapsulation. | |
263 | * | |
264 | * Hence what we need to do now is change the rewrite the vector array to point | |
265 | * to the copied region inside the buffer we just allocated. This allows us to | |
266 | * format the regions into the iclog as though they are being formatted | |
267 | * directly out of the objects themselves. | |
268 | */ | |
269 | static void | |
270 | xlog_cil_format_items( | |
271 | struct log *log, | |
272 | struct xfs_log_vec *log_vector, | |
273 | struct xlog_ticket *ticket, | |
274 | xfs_lsn_t *start_lsn) | |
275 | { | |
276 | struct xfs_log_vec *lv; | |
277 | ||
278 | if (start_lsn) | |
279 | *start_lsn = log->l_cilp->xc_ctx->sequence; | |
280 | ||
281 | ASSERT(log_vector); | |
282 | for (lv = log_vector; lv; lv = lv->lv_next) { | |
283 | void *ptr; | |
284 | int index; | |
285 | int len = 0; | |
286 | ||
287 | /* build the vector array and calculate it's length */ | |
288 | IOP_FORMAT(lv->lv_item, lv->lv_iovecp); | |
289 | for (index = 0; index < lv->lv_niovecs; index++) | |
290 | len += lv->lv_iovecp[index].i_len; | |
291 | ||
292 | lv->lv_buf_len = len; | |
293 | lv->lv_buf = kmem_zalloc(lv->lv_buf_len, KM_SLEEP|KM_NOFS); | |
294 | ptr = lv->lv_buf; | |
295 | ||
296 | for (index = 0; index < lv->lv_niovecs; index++) { | |
297 | struct xfs_log_iovec *vec = &lv->lv_iovecp[index]; | |
298 | ||
299 | memcpy(ptr, vec->i_addr, vec->i_len); | |
300 | vec->i_addr = ptr; | |
301 | ptr += vec->i_len; | |
302 | } | |
303 | ASSERT(ptr == lv->lv_buf + lv->lv_buf_len); | |
304 | ||
305 | xlog_cil_insert(log, ticket, lv->lv_item, lv); | |
306 | } | |
307 | } | |
308 | ||
309 | static void | |
310 | xlog_cil_free_logvec( | |
311 | struct xfs_log_vec *log_vector) | |
312 | { | |
313 | struct xfs_log_vec *lv; | |
314 | ||
315 | for (lv = log_vector; lv; ) { | |
316 | struct xfs_log_vec *next = lv->lv_next; | |
317 | kmem_free(lv->lv_buf); | |
318 | kmem_free(lv); | |
319 | lv = next; | |
320 | } | |
321 | } | |
322 | ||
323 | /* | |
324 | * Commit a transaction with the given vector to the Committed Item List. | |
325 | * | |
326 | * To do this, we need to format the item, pin it in memory if required and | |
327 | * account for the space used by the transaction. Once we have done that we | |
328 | * need to release the unused reservation for the transaction, attach the | |
329 | * transaction to the checkpoint context so we carry the busy extents through | |
330 | * to checkpoint completion, and then unlock all the items in the transaction. | |
331 | * | |
332 | * For more specific information about the order of operations in | |
333 | * xfs_log_commit_cil() please refer to the comments in | |
334 | * xfs_trans_commit_iclog(). | |
ccf7c23f DC |
335 | * |
336 | * Called with the context lock already held in read mode to lock out | |
337 | * background commit, returns without it held once background commits are | |
338 | * allowed again. | |
71e330b5 DC |
339 | */ |
340 | int | |
341 | xfs_log_commit_cil( | |
342 | struct xfs_mount *mp, | |
343 | struct xfs_trans *tp, | |
344 | struct xfs_log_vec *log_vector, | |
345 | xfs_lsn_t *commit_lsn, | |
346 | int flags) | |
347 | { | |
348 | struct log *log = mp->m_log; | |
349 | int log_flags = 0; | |
df806158 | 350 | int push = 0; |
71e330b5 DC |
351 | |
352 | if (flags & XFS_TRANS_RELEASE_LOG_RES) | |
353 | log_flags = XFS_LOG_REL_PERM_RESERV; | |
354 | ||
355 | if (XLOG_FORCED_SHUTDOWN(log)) { | |
356 | xlog_cil_free_logvec(log_vector); | |
357 | return XFS_ERROR(EIO); | |
358 | } | |
359 | ||
360 | /* lock out background commit */ | |
361 | down_read(&log->l_cilp->xc_ctx_lock); | |
362 | xlog_cil_format_items(log, log_vector, tp->t_ticket, commit_lsn); | |
363 | ||
364 | /* check we didn't blow the reservation */ | |
365 | if (tp->t_ticket->t_curr_res < 0) | |
366 | xlog_print_tic_res(log->l_mp, tp->t_ticket); | |
367 | ||
368 | /* attach the transaction to the CIL if it has any busy extents */ | |
369 | if (!list_empty(&tp->t_busy)) { | |
370 | spin_lock(&log->l_cilp->xc_cil_lock); | |
371 | list_splice_init(&tp->t_busy, | |
372 | &log->l_cilp->xc_ctx->busy_extents); | |
373 | spin_unlock(&log->l_cilp->xc_cil_lock); | |
374 | } | |
375 | ||
376 | tp->t_commit_lsn = *commit_lsn; | |
377 | xfs_log_done(mp, tp->t_ticket, NULL, log_flags); | |
378 | xfs_trans_unreserve_and_mod_sb(tp); | |
379 | ||
d17c701c DC |
380 | /* |
381 | * Once all the items of the transaction have been copied to the CIL, | |
382 | * the items can be unlocked and freed. | |
383 | * | |
384 | * This needs to be done before we drop the CIL context lock because we | |
385 | * have to update state in the log items and unlock them before they go | |
386 | * to disk. If we don't, then the CIL checkpoint can race with us and | |
387 | * we can run checkpoint completion before we've updated and unlocked | |
388 | * the log items. This affects (at least) processing of stale buffers, | |
389 | * inodes and EFIs. | |
390 | */ | |
391 | xfs_trans_free_items(tp, *commit_lsn, 0); | |
392 | ||
df806158 DC |
393 | /* check for background commit before unlock */ |
394 | if (log->l_cilp->xc_ctx->space_used > XLOG_CIL_SPACE_LIMIT(log)) | |
395 | push = 1; | |
d17c701c | 396 | |
71e330b5 | 397 | up_read(&log->l_cilp->xc_ctx_lock); |
df806158 DC |
398 | |
399 | /* | |
400 | * We need to push CIL every so often so we don't cache more than we | |
401 | * can fit in the log. The limit really is that a checkpoint can't be | |
402 | * more than half the log (the current checkpoint is not allowed to | |
403 | * overwrite the previous checkpoint), but commit latency and memory | |
404 | * usage limit this to a smaller size in most cases. | |
405 | */ | |
406 | if (push) | |
407 | xlog_cil_push(log, 0); | |
71e330b5 DC |
408 | return 0; |
409 | } | |
410 | ||
411 | /* | |
412 | * Mark all items committed and clear busy extents. We free the log vector | |
413 | * chains in a separate pass so that we unpin the log items as quickly as | |
414 | * possible. | |
415 | */ | |
416 | static void | |
417 | xlog_cil_committed( | |
418 | void *args, | |
419 | int abort) | |
420 | { | |
421 | struct xfs_cil_ctx *ctx = args; | |
422 | struct xfs_log_vec *lv; | |
423 | int abortflag = abort ? XFS_LI_ABORTED : 0; | |
424 | struct xfs_busy_extent *busyp, *n; | |
425 | ||
426 | /* unpin all the log items */ | |
427 | for (lv = ctx->lv_chain; lv; lv = lv->lv_next ) { | |
428 | xfs_trans_item_committed(lv->lv_item, ctx->start_lsn, | |
429 | abortflag); | |
430 | } | |
431 | ||
432 | list_for_each_entry_safe(busyp, n, &ctx->busy_extents, list) | |
433 | xfs_alloc_busy_clear(ctx->cil->xc_log->l_mp, busyp); | |
434 | ||
435 | spin_lock(&ctx->cil->xc_cil_lock); | |
436 | list_del(&ctx->committing); | |
437 | spin_unlock(&ctx->cil->xc_cil_lock); | |
438 | ||
439 | xlog_cil_free_logvec(ctx->lv_chain); | |
440 | kmem_free(ctx); | |
441 | } | |
442 | ||
443 | /* | |
444 | * Push the Committed Item List to the log. If the push_now flag is not set, | |
445 | * then it is a background flush and so we can chose to ignore it. | |
446 | */ | |
447 | int | |
448 | xlog_cil_push( | |
449 | struct log *log, | |
450 | int push_now) | |
451 | { | |
452 | struct xfs_cil *cil = log->l_cilp; | |
453 | struct xfs_log_vec *lv; | |
454 | struct xfs_cil_ctx *ctx; | |
455 | struct xfs_cil_ctx *new_ctx; | |
456 | struct xlog_in_core *commit_iclog; | |
457 | struct xlog_ticket *tic; | |
458 | int num_lv; | |
459 | int num_iovecs; | |
460 | int len; | |
461 | int error = 0; | |
462 | struct xfs_trans_header thdr; | |
463 | struct xfs_log_iovec lhdr; | |
464 | struct xfs_log_vec lvhdr = { NULL }; | |
465 | xfs_lsn_t commit_lsn; | |
466 | ||
467 | if (!cil) | |
468 | return 0; | |
469 | ||
71e330b5 DC |
470 | new_ctx = kmem_zalloc(sizeof(*new_ctx), KM_SLEEP|KM_NOFS); |
471 | new_ctx->ticket = xlog_cil_ticket_alloc(log); | |
472 | ||
df806158 DC |
473 | /* lock out transaction commit, but don't block on background push */ |
474 | if (!down_write_trylock(&cil->xc_ctx_lock)) { | |
475 | if (!push_now) | |
476 | goto out_free_ticket; | |
477 | down_write(&cil->xc_ctx_lock); | |
478 | } | |
71e330b5 DC |
479 | ctx = cil->xc_ctx; |
480 | ||
481 | /* check if we've anything to push */ | |
482 | if (list_empty(&cil->xc_cil)) | |
483 | goto out_skip; | |
484 | ||
df806158 DC |
485 | /* check for spurious background flush */ |
486 | if (!push_now && cil->xc_ctx->space_used < XLOG_CIL_SPACE_LIMIT(log)) | |
487 | goto out_skip; | |
488 | ||
71e330b5 DC |
489 | /* |
490 | * pull all the log vectors off the items in the CIL, and | |
491 | * remove the items from the CIL. We don't need the CIL lock | |
492 | * here because it's only needed on the transaction commit | |
493 | * side which is currently locked out by the flush lock. | |
494 | */ | |
495 | lv = NULL; | |
496 | num_lv = 0; | |
497 | num_iovecs = 0; | |
498 | len = 0; | |
499 | while (!list_empty(&cil->xc_cil)) { | |
500 | struct xfs_log_item *item; | |
501 | int i; | |
502 | ||
503 | item = list_first_entry(&cil->xc_cil, | |
504 | struct xfs_log_item, li_cil); | |
505 | list_del_init(&item->li_cil); | |
506 | if (!ctx->lv_chain) | |
507 | ctx->lv_chain = item->li_lv; | |
508 | else | |
509 | lv->lv_next = item->li_lv; | |
510 | lv = item->li_lv; | |
511 | item->li_lv = NULL; | |
512 | ||
513 | num_lv++; | |
514 | num_iovecs += lv->lv_niovecs; | |
515 | for (i = 0; i < lv->lv_niovecs; i++) | |
516 | len += lv->lv_iovecp[i].i_len; | |
517 | } | |
518 | ||
519 | /* | |
520 | * initialise the new context and attach it to the CIL. Then attach | |
521 | * the current context to the CIL committing lsit so it can be found | |
522 | * during log forces to extract the commit lsn of the sequence that | |
523 | * needs to be forced. | |
524 | */ | |
525 | INIT_LIST_HEAD(&new_ctx->committing); | |
526 | INIT_LIST_HEAD(&new_ctx->busy_extents); | |
527 | new_ctx->sequence = ctx->sequence + 1; | |
528 | new_ctx->cil = cil; | |
529 | cil->xc_ctx = new_ctx; | |
530 | ||
531 | /* | |
532 | * The switch is now done, so we can drop the context lock and move out | |
533 | * of a shared context. We can't just go straight to the commit record, | |
534 | * though - we need to synchronise with previous and future commits so | |
535 | * that the commit records are correctly ordered in the log to ensure | |
536 | * that we process items during log IO completion in the correct order. | |
537 | * | |
538 | * For example, if we get an EFI in one checkpoint and the EFD in the | |
539 | * next (e.g. due to log forces), we do not want the checkpoint with | |
540 | * the EFD to be committed before the checkpoint with the EFI. Hence | |
541 | * we must strictly order the commit records of the checkpoints so | |
542 | * that: a) the checkpoint callbacks are attached to the iclogs in the | |
543 | * correct order; and b) the checkpoints are replayed in correct order | |
544 | * in log recovery. | |
545 | * | |
546 | * Hence we need to add this context to the committing context list so | |
547 | * that higher sequences will wait for us to write out a commit record | |
548 | * before they do. | |
549 | */ | |
550 | spin_lock(&cil->xc_cil_lock); | |
551 | list_add(&ctx->committing, &cil->xc_committing); | |
552 | spin_unlock(&cil->xc_cil_lock); | |
553 | up_write(&cil->xc_ctx_lock); | |
554 | ||
555 | /* | |
556 | * Build a checkpoint transaction header and write it to the log to | |
557 | * begin the transaction. We need to account for the space used by the | |
558 | * transaction header here as it is not accounted for in xlog_write(). | |
559 | * | |
560 | * The LSN we need to pass to the log items on transaction commit is | |
561 | * the LSN reported by the first log vector write. If we use the commit | |
562 | * record lsn then we can move the tail beyond the grant write head. | |
563 | */ | |
564 | tic = ctx->ticket; | |
565 | thdr.th_magic = XFS_TRANS_HEADER_MAGIC; | |
566 | thdr.th_type = XFS_TRANS_CHECKPOINT; | |
567 | thdr.th_tid = tic->t_tid; | |
568 | thdr.th_num_items = num_iovecs; | |
4e0d5f92 | 569 | lhdr.i_addr = &thdr; |
71e330b5 DC |
570 | lhdr.i_len = sizeof(xfs_trans_header_t); |
571 | lhdr.i_type = XLOG_REG_TYPE_TRANSHDR; | |
572 | tic->t_curr_res -= lhdr.i_len + sizeof(xlog_op_header_t); | |
573 | ||
574 | lvhdr.lv_niovecs = 1; | |
575 | lvhdr.lv_iovecp = &lhdr; | |
576 | lvhdr.lv_next = ctx->lv_chain; | |
577 | ||
578 | error = xlog_write(log, &lvhdr, tic, &ctx->start_lsn, NULL, 0); | |
579 | if (error) | |
580 | goto out_abort; | |
581 | ||
582 | /* | |
583 | * now that we've written the checkpoint into the log, strictly | |
584 | * order the commit records so replay will get them in the right order. | |
585 | */ | |
586 | restart: | |
587 | spin_lock(&cil->xc_cil_lock); | |
588 | list_for_each_entry(new_ctx, &cil->xc_committing, committing) { | |
589 | /* | |
590 | * Higher sequences will wait for this one so skip them. | |
591 | * Don't wait for own own sequence, either. | |
592 | */ | |
593 | if (new_ctx->sequence >= ctx->sequence) | |
594 | continue; | |
595 | if (!new_ctx->commit_lsn) { | |
596 | /* | |
597 | * It is still being pushed! Wait for the push to | |
598 | * complete, then start again from the beginning. | |
599 | */ | |
600 | sv_wait(&cil->xc_commit_wait, 0, &cil->xc_cil_lock, 0); | |
601 | goto restart; | |
602 | } | |
603 | } | |
604 | spin_unlock(&cil->xc_cil_lock); | |
605 | ||
606 | commit_lsn = xfs_log_done(log->l_mp, tic, &commit_iclog, 0); | |
607 | if (error || commit_lsn == -1) | |
608 | goto out_abort; | |
609 | ||
610 | /* attach all the transactions w/ busy extents to iclog */ | |
611 | ctx->log_cb.cb_func = xlog_cil_committed; | |
612 | ctx->log_cb.cb_arg = ctx; | |
613 | error = xfs_log_notify(log->l_mp, commit_iclog, &ctx->log_cb); | |
614 | if (error) | |
615 | goto out_abort; | |
616 | ||
617 | /* | |
618 | * now the checkpoint commit is complete and we've attached the | |
619 | * callbacks to the iclog we can assign the commit LSN to the context | |
620 | * and wake up anyone who is waiting for the commit to complete. | |
621 | */ | |
622 | spin_lock(&cil->xc_cil_lock); | |
623 | ctx->commit_lsn = commit_lsn; | |
624 | sv_broadcast(&cil->xc_commit_wait); | |
625 | spin_unlock(&cil->xc_cil_lock); | |
626 | ||
627 | /* release the hounds! */ | |
628 | return xfs_log_release_iclog(log->l_mp, commit_iclog); | |
629 | ||
630 | out_skip: | |
631 | up_write(&cil->xc_ctx_lock); | |
df806158 | 632 | out_free_ticket: |
71e330b5 DC |
633 | xfs_log_ticket_put(new_ctx->ticket); |
634 | kmem_free(new_ctx); | |
635 | return 0; | |
636 | ||
637 | out_abort: | |
638 | xlog_cil_committed(ctx, XFS_LI_ABORTED); | |
639 | return XFS_ERROR(EIO); | |
640 | } | |
641 | ||
642 | /* | |
643 | * Conditionally push the CIL based on the sequence passed in. | |
644 | * | |
645 | * We only need to push if we haven't already pushed the sequence | |
646 | * number given. Hence the only time we will trigger a push here is | |
647 | * if the push sequence is the same as the current context. | |
648 | * | |
649 | * We return the current commit lsn to allow the callers to determine if a | |
650 | * iclog flush is necessary following this call. | |
651 | * | |
652 | * XXX: Initially, just push the CIL unconditionally and return whatever | |
653 | * commit lsn is there. It'll be empty, so this is broken for now. | |
654 | */ | |
655 | xfs_lsn_t | |
656 | xlog_cil_push_lsn( | |
657 | struct log *log, | |
658 | xfs_lsn_t push_seq) | |
659 | { | |
660 | struct xfs_cil *cil = log->l_cilp; | |
661 | struct xfs_cil_ctx *ctx; | |
662 | xfs_lsn_t commit_lsn = NULLCOMMITLSN; | |
663 | ||
664 | restart: | |
665 | down_write(&cil->xc_ctx_lock); | |
666 | ASSERT(push_seq <= cil->xc_ctx->sequence); | |
667 | ||
668 | /* check to see if we need to force out the current context */ | |
669 | if (push_seq == cil->xc_ctx->sequence) { | |
670 | up_write(&cil->xc_ctx_lock); | |
671 | xlog_cil_push(log, 1); | |
672 | goto restart; | |
673 | } | |
674 | ||
675 | /* | |
676 | * See if we can find a previous sequence still committing. | |
677 | * We can drop the flush lock as soon as we have the cil lock | |
678 | * because we are now only comparing contexts protected by | |
679 | * the cil lock. | |
680 | * | |
681 | * We need to wait for all previous sequence commits to complete | |
682 | * before allowing the force of push_seq to go ahead. Hence block | |
683 | * on commits for those as well. | |
684 | */ | |
685 | spin_lock(&cil->xc_cil_lock); | |
686 | up_write(&cil->xc_ctx_lock); | |
687 | list_for_each_entry(ctx, &cil->xc_committing, committing) { | |
688 | if (ctx->sequence > push_seq) | |
689 | continue; | |
690 | if (!ctx->commit_lsn) { | |
691 | /* | |
692 | * It is still being pushed! Wait for the push to | |
693 | * complete, then start again from the beginning. | |
694 | */ | |
695 | sv_wait(&cil->xc_commit_wait, 0, &cil->xc_cil_lock, 0); | |
696 | goto restart; | |
697 | } | |
698 | if (ctx->sequence != push_seq) | |
699 | continue; | |
700 | /* found it! */ | |
701 | commit_lsn = ctx->commit_lsn; | |
702 | } | |
703 | spin_unlock(&cil->xc_cil_lock); | |
704 | return commit_lsn; | |
705 | } | |
ccf7c23f DC |
706 | |
707 | /* | |
708 | * Check if the current log item was first committed in this sequence. | |
709 | * We can't rely on just the log item being in the CIL, we have to check | |
710 | * the recorded commit sequence number. | |
711 | * | |
712 | * Note: for this to be used in a non-racy manner, it has to be called with | |
713 | * CIL flushing locked out. As a result, it should only be used during the | |
714 | * transaction commit process when deciding what to format into the item. | |
715 | */ | |
716 | bool | |
717 | xfs_log_item_in_current_chkpt( | |
718 | struct xfs_log_item *lip) | |
719 | { | |
720 | struct xfs_cil_ctx *ctx; | |
721 | ||
722 | if (!(lip->li_mountp->m_flags & XFS_MOUNT_DELAYLOG)) | |
723 | return false; | |
724 | if (list_empty(&lip->li_cil)) | |
725 | return false; | |
726 | ||
727 | ctx = lip->li_mountp->m_log->l_cilp->xc_ctx; | |
728 | ||
729 | /* | |
730 | * li_seq is written on the first commit of a log item to record the | |
731 | * first checkpoint it is written to. Hence if it is different to the | |
732 | * current sequence, we're in a new checkpoint. | |
733 | */ | |
734 | if (XFS_LSN_CMP(lip->li_seq, ctx->sequence) != 0) | |
735 | return false; | |
736 | return true; | |
737 | } |