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