1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #include "xfs_platform.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_bit.h"
13 #include "xfs_mount.h"
14 #include "xfs_trans.h"
15 #include "xfs_trans_priv.h"
16 #include "xfs_buf_item.h"
17 #include "xfs_inode.h"
18 #include "xfs_inode_item.h"
19 #include "xfs_quota.h"
20 #include "xfs_dquot_item.h"
21 #include "xfs_dquot.h"
22 #include "xfs_trace.h"
23 #include "xfs_log.h"
24 #include "xfs_log_priv.h"
25 #include "xfs_error.h"
26
27
28 struct kmem_cache *xfs_buf_item_cache;
29
BUF_ITEM(struct xfs_log_item * lip)30 static inline struct xfs_buf_log_item *BUF_ITEM(struct xfs_log_item *lip)
31 {
32 return container_of(lip, struct xfs_buf_log_item, bli_item);
33 }
34
35 static void
xfs_buf_item_get_format(struct xfs_buf_log_item * bip,int count)36 xfs_buf_item_get_format(
37 struct xfs_buf_log_item *bip,
38 int count)
39 {
40 ASSERT(bip->bli_formats == NULL);
41 bip->bli_format_count = count;
42
43 if (count == 1) {
44 bip->bli_formats = &bip->__bli_format;
45 return;
46 }
47
48 bip->bli_formats = kzalloc(count * sizeof(struct xfs_buf_log_format),
49 GFP_KERNEL | __GFP_NOFAIL);
50 }
51
52 static void
xfs_buf_item_free_format(struct xfs_buf_log_item * bip)53 xfs_buf_item_free_format(
54 struct xfs_buf_log_item *bip)
55 {
56 if (bip->bli_formats != &bip->__bli_format) {
57 kfree(bip->bli_formats);
58 bip->bli_formats = NULL;
59 }
60 }
61
62 static void
xfs_buf_item_free(struct xfs_buf_log_item * bip)63 xfs_buf_item_free(
64 struct xfs_buf_log_item *bip)
65 {
66 xfs_buf_item_free_format(bip);
67 kvfree(bip->bli_item.li_lv_shadow);
68 kmem_cache_free(xfs_buf_item_cache, bip);
69 }
70
71 /*
72 * xfs_buf_item_relse() is called when the buf log item is no longer needed.
73 */
74 static void
xfs_buf_item_relse(struct xfs_buf_log_item * bip)75 xfs_buf_item_relse(
76 struct xfs_buf_log_item *bip)
77 {
78 struct xfs_buf *bp = bip->bli_buf;
79
80 trace_xfs_buf_item_relse(bp, _RET_IP_);
81
82 ASSERT(!test_bit(XFS_LI_IN_AIL, &bip->bli_item.li_flags));
83 ASSERT(atomic_read(&bip->bli_refcount) == 0);
84
85 bp->b_log_item = NULL;
86 xfs_buf_rele(bp);
87 xfs_buf_item_free(bip);
88 }
89
90 /* Is this log iovec plausibly large enough to contain the buffer log format? */
91 bool
xfs_buf_log_check_iovec(struct kvec * iovec)92 xfs_buf_log_check_iovec(
93 struct kvec *iovec)
94 {
95 struct xfs_buf_log_format *blfp = iovec->iov_base;
96 char *bmp_end;
97 char *item_end;
98
99 if (offsetof(struct xfs_buf_log_format, blf_data_map) > iovec->iov_len)
100 return false;
101
102 item_end = (char *)iovec->iov_base + iovec->iov_len;
103 bmp_end = (char *)&blfp->blf_data_map[blfp->blf_map_size];
104 return bmp_end <= item_end;
105 }
106
107 static inline int
xfs_buf_log_format_size(struct xfs_buf_log_format * blfp)108 xfs_buf_log_format_size(
109 struct xfs_buf_log_format *blfp)
110 {
111 return offsetof(struct xfs_buf_log_format, blf_data_map) +
112 (blfp->blf_map_size * sizeof(blfp->blf_data_map[0]));
113 }
114
115 /*
116 * Return the number of log iovecs and space needed to log the given buf log
117 * item segment.
118 *
119 * It calculates this as 1 iovec for the buf log format structure and 1 for each
120 * stretch of non-contiguous chunks to be logged. Contiguous chunks are logged
121 * in a single iovec.
122 */
123 STATIC void
xfs_buf_item_size_segment(struct xfs_buf_log_item * bip,struct xfs_buf_log_format * blfp,uint offset,int * nvecs,int * nbytes)124 xfs_buf_item_size_segment(
125 struct xfs_buf_log_item *bip,
126 struct xfs_buf_log_format *blfp,
127 uint offset,
128 int *nvecs,
129 int *nbytes)
130 {
131 int first_bit;
132 int nbits;
133
134 first_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size, 0);
135 if (first_bit == -1)
136 return;
137
138 (*nvecs)++;
139 *nbytes += xfs_buf_log_format_size(blfp);
140
141 do {
142 nbits = xfs_contig_bits(blfp->blf_data_map,
143 blfp->blf_map_size, first_bit);
144 ASSERT(nbits > 0);
145 (*nvecs)++;
146 *nbytes += nbits * XFS_BLF_CHUNK;
147
148 /*
149 * This takes the bit number to start looking from and
150 * returns the next set bit from there. It returns -1
151 * if there are no more bits set or the start bit is
152 * beyond the end of the bitmap.
153 */
154 first_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size,
155 (uint)first_bit + nbits + 1);
156 } while (first_bit != -1);
157
158 return;
159 }
160
161 /*
162 * Compute the worst case log item overhead for an invalidated buffer with the
163 * given map count and block size.
164 */
165 unsigned int
xfs_buf_inval_log_space(unsigned int map_count,unsigned int blocksize)166 xfs_buf_inval_log_space(
167 unsigned int map_count,
168 unsigned int blocksize)
169 {
170 unsigned int chunks = DIV_ROUND_UP(blocksize, XFS_BLF_CHUNK);
171 unsigned int bitmap_size = DIV_ROUND_UP(chunks, NBWORD);
172 unsigned int ret =
173 offsetof(struct xfs_buf_log_format, blf_data_map) +
174 (bitmap_size * sizeof_field(struct xfs_buf_log_format,
175 blf_data_map[0]));
176
177 return ret * map_count;
178 }
179
180 /*
181 * Return the number of log iovecs and space needed to log the given buf log
182 * item.
183 *
184 * Discontiguous buffers need a format structure per region that is being
185 * logged. This makes the changes in the buffer appear to log recovery as though
186 * they came from separate buffers, just like would occur if multiple buffers
187 * were used instead of a single discontiguous buffer. This enables
188 * discontiguous buffers to be in-memory constructs, completely transparent to
189 * what ends up on disk.
190 *
191 * If the XFS_BLI_STALE flag has been set, then log nothing but the buf log
192 * format structures. If the item has previously been logged and has dirty
193 * regions, we do not relog them in stale buffers. This has the effect of
194 * reducing the size of the relogged item by the amount of dirty data tracked
195 * by the log item. This can result in the committing transaction reducing the
196 * amount of space being consumed by the CIL.
197 */
198 STATIC void
xfs_buf_item_size(struct xfs_log_item * lip,int * nvecs,int * nbytes)199 xfs_buf_item_size(
200 struct xfs_log_item *lip,
201 int *nvecs,
202 int *nbytes)
203 {
204 struct xfs_buf_log_item *bip = BUF_ITEM(lip);
205 struct xfs_buf *bp = bip->bli_buf;
206 int i;
207 int bytes;
208 uint offset = 0;
209
210 ASSERT(atomic_read(&bip->bli_refcount) > 0);
211 if (bip->bli_flags & XFS_BLI_STALE) {
212 /*
213 * The buffer is stale, so all we need to log is the buf log
214 * format structure with the cancel flag in it as we are never
215 * going to replay the changes tracked in the log item.
216 */
217 trace_xfs_buf_item_size_stale(bip);
218 ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
219 *nvecs += bip->bli_format_count;
220 for (i = 0; i < bip->bli_format_count; i++) {
221 *nbytes += xfs_buf_log_format_size(&bip->bli_formats[i]);
222 }
223 return;
224 }
225
226 ASSERT(bip->bli_flags & XFS_BLI_LOGGED);
227
228 if (bip->bli_flags & XFS_BLI_ORDERED) {
229 /*
230 * The buffer has been logged just to order it. It is not being
231 * included in the transaction commit, so no vectors are used at
232 * all.
233 */
234 trace_xfs_buf_item_size_ordered(bip);
235 *nvecs = XFS_LOG_VEC_ORDERED;
236 return;
237 }
238
239 /*
240 * The vector count is based on the number of buffer vectors we have
241 * dirty bits in. This will only be greater than one when we have a
242 * compound buffer with more than one segment dirty. Hence for compound
243 * buffers we need to track which segment the dirty bits correspond to,
244 * and when we move from one segment to the next increment the vector
245 * count for the extra buf log format structure that will need to be
246 * written.
247 */
248 bytes = 0;
249 for (i = 0; i < bip->bli_format_count; i++) {
250 xfs_buf_item_size_segment(bip, &bip->bli_formats[i], offset,
251 nvecs, &bytes);
252 offset += BBTOB(bp->b_maps[i].bm_len);
253 }
254
255 /*
256 * Round up the buffer size required to minimise the number of memory
257 * allocations that need to be done as this item grows when relogged by
258 * repeated modifications.
259 */
260 *nbytes = round_up(bytes, 512);
261 trace_xfs_buf_item_size(bip);
262 }
263
264 static inline void
xfs_buf_item_copy_iovec(struct xlog_format_buf * lfb,struct xfs_buf * bp,uint offset,int first_bit,uint nbits)265 xfs_buf_item_copy_iovec(
266 struct xlog_format_buf *lfb,
267 struct xfs_buf *bp,
268 uint offset,
269 int first_bit,
270 uint nbits)
271 {
272 offset += first_bit * XFS_BLF_CHUNK;
273 xlog_format_copy(lfb, XLOG_REG_TYPE_BCHUNK, xfs_buf_offset(bp, offset),
274 nbits * XFS_BLF_CHUNK);
275 }
276
277 static void
xfs_buf_item_format_segment(struct xfs_buf_log_item * bip,struct xlog_format_buf * lfb,uint offset,struct xfs_buf_log_format * blfp)278 xfs_buf_item_format_segment(
279 struct xfs_buf_log_item *bip,
280 struct xlog_format_buf *lfb,
281 uint offset,
282 struct xfs_buf_log_format *blfp)
283 {
284 struct xfs_buf *bp = bip->bli_buf;
285 uint base_size;
286 int first_bit;
287 uint nbits;
288
289 /* copy the flags across from the base format item */
290 blfp->blf_flags = bip->__bli_format.blf_flags;
291
292 /*
293 * Base size is the actual size of the ondisk structure - it reflects
294 * the actual size of the dirty bitmap rather than the size of the in
295 * memory structure.
296 */
297 base_size = xfs_buf_log_format_size(blfp);
298
299 first_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size, 0);
300 if (!(bip->bli_flags & XFS_BLI_STALE) && first_bit == -1) {
301 /*
302 * If the map is not be dirty in the transaction, mark
303 * the size as zero and do not advance the vector pointer.
304 */
305 return;
306 }
307
308 blfp = xlog_format_copy(lfb, XLOG_REG_TYPE_BFORMAT, blfp, base_size);
309 blfp->blf_size = 1;
310
311 if (bip->bli_flags & XFS_BLI_STALE) {
312 /*
313 * The buffer is stale, so all we need to log
314 * is the buf log format structure with the
315 * cancel flag in it.
316 */
317 trace_xfs_buf_item_format_stale(bip);
318 ASSERT(blfp->blf_flags & XFS_BLF_CANCEL);
319 return;
320 }
321
322
323 /*
324 * Fill in an iovec for each set of contiguous chunks.
325 */
326 do {
327 ASSERT(first_bit >= 0);
328 nbits = xfs_contig_bits(blfp->blf_data_map,
329 blfp->blf_map_size, first_bit);
330 ASSERT(nbits > 0);
331 xfs_buf_item_copy_iovec(lfb, bp, offset, first_bit, nbits);
332 blfp->blf_size++;
333
334 /*
335 * This takes the bit number to start looking from and
336 * returns the next set bit from there. It returns -1
337 * if there are no more bits set or the start bit is
338 * beyond the end of the bitmap.
339 */
340 first_bit = xfs_next_bit(blfp->blf_data_map, blfp->blf_map_size,
341 (uint)first_bit + nbits + 1);
342 } while (first_bit != -1);
343
344 return;
345 }
346
347 /*
348 * This is called to fill in the vector of log iovecs for the
349 * given log buf item. It fills the first entry with a buf log
350 * format structure, and the rest point to contiguous chunks
351 * within the buffer.
352 */
353 STATIC void
xfs_buf_item_format(struct xfs_log_item * lip,struct xlog_format_buf * lfb)354 xfs_buf_item_format(
355 struct xfs_log_item *lip,
356 struct xlog_format_buf *lfb)
357 {
358 struct xfs_buf_log_item *bip = BUF_ITEM(lip);
359 struct xfs_buf *bp = bip->bli_buf;
360 uint offset = 0;
361 int i;
362
363 ASSERT(atomic_read(&bip->bli_refcount) > 0);
364 ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
365 (bip->bli_flags & XFS_BLI_STALE));
366 ASSERT((bip->bli_flags & XFS_BLI_STALE) ||
367 (xfs_blft_from_flags(&bip->__bli_format) > XFS_BLFT_UNKNOWN_BUF
368 && xfs_blft_from_flags(&bip->__bli_format) < XFS_BLFT_MAX_BUF));
369 ASSERT(!(bip->bli_flags & XFS_BLI_ORDERED) ||
370 (bip->bli_flags & XFS_BLI_STALE));
371
372
373 /*
374 * If it is an inode buffer, transfer the in-memory state to the
375 * format flags and clear the in-memory state.
376 *
377 * For buffer based inode allocation, we do not transfer
378 * this state if the inode buffer allocation has not yet been committed
379 * to the log as setting the XFS_BLI_INODE_BUF flag will prevent
380 * correct replay of the inode allocation.
381 *
382 * For icreate item based inode allocation, the buffers aren't written
383 * to the journal during allocation, and hence we should always tag the
384 * buffer as an inode buffer so that the correct unlinked list replay
385 * occurs during recovery.
386 */
387 if (bip->bli_flags & XFS_BLI_INODE_BUF) {
388 if (xfs_has_v3inodes(lip->li_log->l_mp) ||
389 !((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) &&
390 xfs_log_item_in_current_chkpt(lip)))
391 bip->__bli_format.blf_flags |= XFS_BLF_INODE_BUF;
392 bip->bli_flags &= ~XFS_BLI_INODE_BUF;
393 }
394
395 for (i = 0; i < bip->bli_format_count; i++) {
396 xfs_buf_item_format_segment(bip, lfb, offset,
397 &bip->bli_formats[i]);
398 offset += BBTOB(bp->b_maps[i].bm_len);
399 }
400
401 /*
402 * Check to make sure everything is consistent.
403 */
404 trace_xfs_buf_item_format(bip);
405 }
406
407 /*
408 * This is called to pin the buffer associated with the buf log item in memory
409 * so it cannot be written out.
410 *
411 * We take a reference to the buffer log item here so that the BLI life cycle
412 * extends at least until the buffer is unpinned via xfs_buf_item_unpin() and
413 * inserted into the AIL.
414 *
415 * We also need to take a reference to the buffer itself as the BLI unpin
416 * processing requires accessing the buffer after the BLI has dropped the final
417 * BLI reference. See xfs_buf_item_unpin() for an explanation.
418 * If unpins race to drop the final BLI reference and only the
419 * BLI owns a reference to the buffer, then the loser of the race can have the
420 * buffer fgreed from under it (e.g. on shutdown). Taking a buffer reference per
421 * pin count ensures the life cycle of the buffer extends for as
422 * long as we hold the buffer pin reference in xfs_buf_item_unpin().
423 */
424 STATIC void
xfs_buf_item_pin(struct xfs_log_item * lip)425 xfs_buf_item_pin(
426 struct xfs_log_item *lip)
427 {
428 struct xfs_buf_log_item *bip = BUF_ITEM(lip);
429
430 ASSERT(atomic_read(&bip->bli_refcount) > 0);
431 ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
432 (bip->bli_flags & XFS_BLI_ORDERED) ||
433 (bip->bli_flags & XFS_BLI_STALE));
434
435 trace_xfs_buf_item_pin(bip);
436
437 xfs_buf_hold(bip->bli_buf);
438 atomic_inc(&bip->bli_refcount);
439 atomic_inc(&bip->bli_buf->b_pin_count);
440 }
441
442 /*
443 * For a stale BLI, process all the necessary completions that must be
444 * performed when the final BLI reference goes away. The buffer will be
445 * referenced and locked here - we return to the caller with the buffer still
446 * referenced and locked for them to finalise processing of the buffer.
447 */
448 static void
xfs_buf_item_finish_stale(struct xfs_buf_log_item * bip)449 xfs_buf_item_finish_stale(
450 struct xfs_buf_log_item *bip)
451 {
452 struct xfs_buf *bp = bip->bli_buf;
453 struct xfs_log_item *lip = &bip->bli_item;
454
455 ASSERT(bip->bli_flags & XFS_BLI_STALE);
456 ASSERT(xfs_buf_islocked(bp));
457 ASSERT(bp->b_flags & XBF_STALE);
458 ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
459 ASSERT(list_empty(&lip->li_trans));
460 ASSERT(!bp->b_transp);
461
462 if (bip->bli_flags & XFS_BLI_STALE_INODE) {
463 xfs_buf_item_done(bp);
464 xfs_buf_inode_iodone(bp);
465 ASSERT(list_empty(&bp->b_li_list));
466 return;
467 }
468
469 /*
470 * We may or may not be on the AIL here, xfs_trans_ail_delete() will do
471 * the right thing regardless of the situation in which we are called.
472 */
473 xfs_trans_ail_delete(lip, SHUTDOWN_LOG_IO_ERROR);
474 xfs_buf_item_relse(bip);
475 ASSERT(bp->b_log_item == NULL);
476 }
477
478 /*
479 * This is called to unpin the buffer associated with the buf log item which was
480 * previously pinned with a call to xfs_buf_item_pin(). We enter this function
481 * with a buffer pin count, a buffer reference and a BLI reference.
482 *
483 * We must drop the BLI reference before we unpin the buffer because the AIL
484 * doesn't acquire a BLI reference whenever it accesses it. Therefore if the
485 * refcount drops to zero, the bli could still be AIL resident and the buffer
486 * submitted for I/O at any point before we return. This can result in IO
487 * completion freeing the buffer while we are still trying to access it here.
488 * This race condition can also occur in shutdown situations where we abort and
489 * unpin buffers from contexts other that journal IO completion.
490 *
491 * Hence we have to hold a buffer reference per pin count to ensure that the
492 * buffer cannot be freed until we have finished processing the unpin operation.
493 * The reference is taken in xfs_buf_item_pin(), and we must hold it until we
494 * are done processing the buffer state. In the case of an abort (remove =
495 * true) then we re-use the current pin reference as the IO reference we hand
496 * off to IO failure handling.
497 */
498 STATIC void
xfs_buf_item_unpin(struct xfs_log_item * lip,int remove)499 xfs_buf_item_unpin(
500 struct xfs_log_item *lip,
501 int remove)
502 {
503 struct xfs_buf_log_item *bip = BUF_ITEM(lip);
504 struct xfs_buf *bp = bip->bli_buf;
505 int stale = bip->bli_flags & XFS_BLI_STALE;
506 int freed;
507
508 ASSERT(bp->b_log_item == bip);
509 ASSERT(atomic_read(&bip->bli_refcount) > 0);
510
511 trace_xfs_buf_item_unpin(bip);
512
513 freed = atomic_dec_and_test(&bip->bli_refcount);
514 if (atomic_dec_and_test(&bp->b_pin_count))
515 wake_up_all(&bp->b_waiters);
516
517 /*
518 * Nothing to do but drop the buffer pin reference if the BLI is
519 * still active.
520 */
521 if (!freed) {
522 xfs_buf_rele(bp);
523 return;
524 }
525
526 if (stale) {
527 trace_xfs_buf_item_unpin_stale(bip);
528
529 /*
530 * The buffer has been locked and referenced since it was marked
531 * stale so we own both lock and reference exclusively here. We
532 * do not need the pin reference any more, so drop it now so
533 * that we only have one reference to drop once item completion
534 * processing is complete.
535 */
536 xfs_buf_rele(bp);
537 xfs_buf_item_finish_stale(bip);
538 xfs_buf_relse(bp);
539 return;
540 }
541
542 if (remove) {
543 /*
544 * We need to simulate an async IO failures here to ensure that
545 * the correct error completion is run on this buffer. This
546 * requires a reference to the buffer and for the buffer to be
547 * locked. We can safely pass ownership of the pin reference to
548 * the IO to ensure that nothing can free the buffer while we
549 * wait for the lock and then run the IO failure completion.
550 */
551 xfs_buf_lock(bp);
552 xfs_buf_fail(bp);
553 return;
554 }
555
556 /*
557 * BLI has no more active references - it will be moved to the AIL to
558 * manage the remaining BLI/buffer life cycle. There is nothing left for
559 * us to do here so drop the pin reference to the buffer.
560 */
561 xfs_buf_rele(bp);
562 }
563
564 STATIC uint
xfs_buf_item_push(struct xfs_log_item * lip,struct list_head * buffer_list)565 xfs_buf_item_push(
566 struct xfs_log_item *lip,
567 struct list_head *buffer_list)
568 {
569 struct xfs_buf_log_item *bip = BUF_ITEM(lip);
570 struct xfs_buf *bp = bip->bli_buf;
571 uint rval = XFS_ITEM_SUCCESS;
572
573 if (xfs_buf_ispinned(bp))
574 return XFS_ITEM_PINNED;
575 if (!xfs_buf_trylock(bp)) {
576 /*
577 * If we have just raced with a buffer being pinned and it has
578 * been marked stale, we could end up stalling until someone else
579 * issues a log force to unpin the stale buffer. Check for the
580 * race condition here so xfsaild recognizes the buffer is pinned
581 * and queues a log force to move it along.
582 */
583 if (xfs_buf_ispinned(bp))
584 return XFS_ITEM_PINNED;
585 return XFS_ITEM_LOCKED;
586 }
587
588 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
589
590 trace_xfs_buf_item_push(bip);
591
592 /* has a previous flush failed due to IO errors? */
593 if (bp->b_flags & XBF_WRITE_FAIL) {
594 xfs_buf_alert_ratelimited(bp, "XFS: Failing async write",
595 "Failing async write on buffer block 0x%llx. Retrying async write.",
596 (long long)xfs_buf_daddr(bp));
597 }
598
599 if (!xfs_buf_delwri_queue(bp, buffer_list))
600 rval = XFS_ITEM_FLUSHING;
601 xfs_buf_unlock(bp);
602 return rval;
603 }
604
605 /*
606 * Drop the buffer log item refcount and take appropriate action. This helper
607 * determines whether the bli must be freed or not, since a decrement to zero
608 * does not necessarily mean the bli is unused.
609 */
610 void
xfs_buf_item_put(struct xfs_buf_log_item * bip)611 xfs_buf_item_put(
612 struct xfs_buf_log_item *bip)
613 {
614
615 ASSERT(xfs_buf_islocked(bip->bli_buf));
616
617 /* drop the bli ref and return if it wasn't the last one */
618 if (!atomic_dec_and_test(&bip->bli_refcount))
619 return;
620
621 /* If the BLI is in the AIL, then it is still dirty and in use */
622 if (test_bit(XFS_LI_IN_AIL, &bip->bli_item.li_flags)) {
623 ASSERT(bip->bli_flags & XFS_BLI_DIRTY);
624 return;
625 }
626
627 /*
628 * In shutdown conditions, we can be asked to free a dirty BLI that
629 * isn't in the AIL. This can occur due to a checkpoint aborting a BLI
630 * instead of inserting it into the AIL at checkpoint IO completion. If
631 * there's another bli reference (e.g. a btree cursor holds a clean
632 * reference) and it is released via xfs_trans_brelse(), we can get here
633 * with that aborted, dirty BLI. In this case, it is safe to free the
634 * dirty BLI immediately, as it is not in the AIL and there are no
635 * other references to it.
636 *
637 * We should never get here with a stale BLI via that path as
638 * xfs_trans_brelse() specifically holds onto stale buffers rather than
639 * releasing them.
640 */
641 ASSERT(!(bip->bli_flags & XFS_BLI_DIRTY) ||
642 test_bit(XFS_LI_ABORTED, &bip->bli_item.li_flags));
643 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
644 xfs_buf_item_relse(bip);
645 }
646
647 /*
648 * Release the buffer associated with the buf log item. If there is no dirty
649 * logged data associated with the buffer recorded in the buf log item, then
650 * free the buf log item and remove the reference to it in the buffer.
651 *
652 * This call ignores the recursion count. It is only called when the buffer
653 * should REALLY be unlocked, regardless of the recursion count.
654 *
655 * We unconditionally drop the transaction's reference to the log item. If the
656 * item was logged, then another reference was taken when it was pinned, so we
657 * can safely drop the transaction reference now. This also allows us to avoid
658 * potential races with the unpin code freeing the bli by not referencing the
659 * bli after we've dropped the reference count.
660 *
661 * If the XFS_BLI_HOLD flag is set in the buf log item, then free the log item
662 * if necessary but do not unlock the buffer. This is for support of
663 * xfs_trans_bhold(). Make sure the XFS_BLI_HOLD field is cleared if we don't
664 * free the item.
665 *
666 * If the XFS_BLI_STALE flag is set, the last reference to the BLI *must*
667 * perform a completion abort of any objects attached to the buffer for IO
668 * tracking purposes. This generally only happens in shutdown situations,
669 * normally xfs_buf_item_unpin() will drop the last BLI reference and perform
670 * completion processing. However, because transaction completion can race with
671 * checkpoint completion during a shutdown, this release context may end up
672 * being the last active reference to the BLI and so needs to perform this
673 * cleanup.
674 */
675 STATIC void
xfs_buf_item_release(struct xfs_log_item * lip)676 xfs_buf_item_release(
677 struct xfs_log_item *lip)
678 {
679 struct xfs_buf_log_item *bip = BUF_ITEM(lip);
680 struct xfs_buf *bp = bip->bli_buf;
681 bool hold = bip->bli_flags & XFS_BLI_HOLD;
682 bool stale = bip->bli_flags & XFS_BLI_STALE;
683 bool aborted = test_bit(XFS_LI_ABORTED,
684 &lip->li_flags);
685 bool dirty = bip->bli_flags & XFS_BLI_DIRTY;
686 #if defined(DEBUG) || defined(XFS_WARN)
687 bool ordered = bip->bli_flags & XFS_BLI_ORDERED;
688 #endif
689
690 trace_xfs_buf_item_release(bip);
691
692 ASSERT(xfs_buf_islocked(bp));
693
694 /*
695 * The bli dirty state should match whether the blf has logged segments
696 * except for ordered buffers, where only the bli should be dirty.
697 */
698 ASSERT((!ordered && dirty == xfs_buf_item_dirty_format(bip)) ||
699 (ordered && dirty && !xfs_buf_item_dirty_format(bip)));
700 ASSERT(!stale || (bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
701
702 /*
703 * Clear the buffer's association with this transaction and
704 * per-transaction state from the bli, which has been copied above.
705 */
706 bp->b_transp = NULL;
707 bip->bli_flags &= ~(XFS_BLI_LOGGED | XFS_BLI_HOLD | XFS_BLI_ORDERED);
708
709 /* If there are other references, then we have nothing to do. */
710 if (!atomic_dec_and_test(&bip->bli_refcount))
711 goto out_release;
712
713 /*
714 * Stale buffer completion frees the BLI, unlocks and releases the
715 * buffer. Neither the BLI or buffer are safe to reference after this
716 * call, so there's nothing more we need to do here.
717 *
718 * If we get here with a stale buffer and references to the BLI remain,
719 * we must not unlock the buffer as the last BLI reference owns lock
720 * context, not us.
721 */
722 if (stale) {
723 xfs_buf_item_finish_stale(bip);
724 xfs_buf_relse(bp);
725 ASSERT(!hold);
726 return;
727 }
728
729 /*
730 * Dirty or clean, aborted items are done and need to be removed from
731 * the AIL and released. This frees the BLI, but leaves the buffer
732 * locked and referenced.
733 */
734 if (aborted || xlog_is_shutdown(lip->li_log)) {
735 ASSERT(list_empty(&bip->bli_buf->b_li_list));
736 xfs_buf_item_done(bp);
737 goto out_release;
738 }
739
740 /*
741 * Clean, unreferenced BLIs can be immediately freed, leaving the buffer
742 * locked and referenced.
743 *
744 * Dirty, unreferenced BLIs *must* be in the AIL awaiting writeback.
745 */
746 if (!dirty)
747 xfs_buf_item_relse(bip);
748 else
749 ASSERT(test_bit(XFS_LI_IN_AIL, &lip->li_flags));
750
751 /* Not safe to reference the BLI from here */
752 out_release:
753 /*
754 * If we get here with a stale buffer, we must not unlock the
755 * buffer as the last BLI reference owns lock context, not us.
756 */
757 if (stale || hold)
758 return;
759 xfs_buf_relse(bp);
760 }
761
762 STATIC void
xfs_buf_item_committing(struct xfs_log_item * lip,xfs_csn_t seq)763 xfs_buf_item_committing(
764 struct xfs_log_item *lip,
765 xfs_csn_t seq)
766 {
767 return xfs_buf_item_release(lip);
768 }
769
770 /*
771 * This is called to find out where the oldest active copy of the
772 * buf log item in the on disk log resides now that the last log
773 * write of it completed at the given lsn.
774 * We always re-log all the dirty data in a buffer, so usually the
775 * latest copy in the on disk log is the only one that matters. For
776 * those cases we simply return the given lsn.
777 *
778 * The one exception to this is for buffers full of newly allocated
779 * inodes. These buffers are only relogged with the XFS_BLI_INODE_BUF
780 * flag set, indicating that only the di_next_unlinked fields from the
781 * inodes in the buffers will be replayed during recovery. If the
782 * original newly allocated inode images have not yet been flushed
783 * when the buffer is so relogged, then we need to make sure that we
784 * keep the old images in the 'active' portion of the log. We do this
785 * by returning the original lsn of that transaction here rather than
786 * the current one.
787 */
788 STATIC xfs_lsn_t
xfs_buf_item_committed(struct xfs_log_item * lip,xfs_lsn_t lsn)789 xfs_buf_item_committed(
790 struct xfs_log_item *lip,
791 xfs_lsn_t lsn)
792 {
793 struct xfs_buf_log_item *bip = BUF_ITEM(lip);
794
795 trace_xfs_buf_item_committed(bip);
796
797 if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) && lip->li_lsn != 0)
798 return lip->li_lsn;
799 return lsn;
800 }
801
802 #ifdef DEBUG_EXPENSIVE
803 static int
xfs_buf_item_precommit(struct xfs_trans * tp,struct xfs_log_item * lip)804 xfs_buf_item_precommit(
805 struct xfs_trans *tp,
806 struct xfs_log_item *lip)
807 {
808 struct xfs_buf_log_item *bip = BUF_ITEM(lip);
809 struct xfs_buf *bp = bip->bli_buf;
810 struct xfs_mount *mp = bp->b_mount;
811 xfs_failaddr_t fa;
812
813 if (!bp->b_ops || !bp->b_ops->verify_struct)
814 return 0;
815 if (bip->bli_flags & XFS_BLI_STALE)
816 return 0;
817
818 fa = bp->b_ops->verify_struct(bp);
819 if (fa) {
820 xfs_buf_verifier_error(bp, -EFSCORRUPTED, bp->b_ops->name,
821 bp->b_addr, BBTOB(bp->b_length), fa);
822 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
823 ASSERT(fa == NULL);
824 }
825
826 return 0;
827 }
828 #else
829 # define xfs_buf_item_precommit NULL
830 #endif
831
832 static const struct xfs_item_ops xfs_buf_item_ops = {
833 .iop_size = xfs_buf_item_size,
834 .iop_precommit = xfs_buf_item_precommit,
835 .iop_format = xfs_buf_item_format,
836 .iop_pin = xfs_buf_item_pin,
837 .iop_unpin = xfs_buf_item_unpin,
838 .iop_release = xfs_buf_item_release,
839 .iop_committing = xfs_buf_item_committing,
840 .iop_committed = xfs_buf_item_committed,
841 .iop_push = xfs_buf_item_push,
842 };
843
844 /*
845 * Allocate a new buf log item to go with the given buffer.
846 * Set the buffer's b_log_item field to point to the new
847 * buf log item.
848 */
849 int
xfs_buf_item_init(struct xfs_buf * bp,struct xfs_mount * mp)850 xfs_buf_item_init(
851 struct xfs_buf *bp,
852 struct xfs_mount *mp)
853 {
854 struct xfs_buf_log_item *bip = bp->b_log_item;
855 int chunks;
856 int map_size;
857 int i;
858
859 /*
860 * Check to see if there is already a buf log item for
861 * this buffer. If we do already have one, there is
862 * nothing to do here so return.
863 */
864 ASSERT(bp->b_mount == mp);
865 if (bip) {
866 ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
867 ASSERT(!bp->b_transp);
868 ASSERT(bip->bli_buf == bp);
869 return 0;
870 }
871
872 bip = kmem_cache_zalloc(xfs_buf_item_cache, GFP_KERNEL | __GFP_NOFAIL);
873 xfs_log_item_init(mp, &bip->bli_item, XFS_LI_BUF, &xfs_buf_item_ops);
874 bip->bli_buf = bp;
875
876 /*
877 * chunks is the number of XFS_BLF_CHUNK size pieces the buffer
878 * can be divided into. Make sure not to truncate any pieces.
879 * map_size is the size of the bitmap needed to describe the
880 * chunks of the buffer.
881 *
882 * Discontiguous buffer support follows the layout of the underlying
883 * buffer. This makes the implementation as simple as possible.
884 */
885 xfs_buf_item_get_format(bip, bp->b_map_count);
886
887 for (i = 0; i < bip->bli_format_count; i++) {
888 chunks = DIV_ROUND_UP(BBTOB(bp->b_maps[i].bm_len),
889 XFS_BLF_CHUNK);
890 map_size = DIV_ROUND_UP(chunks, NBWORD);
891
892 if (map_size > XFS_BLF_DATAMAP_SIZE) {
893 xfs_buf_item_free_format(bip);
894 kmem_cache_free(xfs_buf_item_cache, bip);
895 xfs_err(mp,
896 "buffer item dirty bitmap (%u uints) too small to reflect %u bytes!",
897 map_size,
898 BBTOB(bp->b_maps[i].bm_len));
899 return -EFSCORRUPTED;
900 }
901
902 bip->bli_formats[i].blf_type = XFS_LI_BUF;
903 bip->bli_formats[i].blf_blkno = bp->b_maps[i].bm_bn;
904 bip->bli_formats[i].blf_len = bp->b_maps[i].bm_len;
905 bip->bli_formats[i].blf_map_size = map_size;
906 }
907
908 bp->b_log_item = bip;
909 xfs_buf_hold(bp);
910 return 0;
911 }
912
913
914 /*
915 * Mark bytes first through last inclusive as dirty in the buf
916 * item's bitmap.
917 */
918 static void
xfs_buf_item_log_segment(uint first,uint last,uint * map)919 xfs_buf_item_log_segment(
920 uint first,
921 uint last,
922 uint *map)
923 {
924 uint first_bit;
925 uint last_bit;
926 uint bits_to_set;
927 uint bits_set;
928 uint word_num;
929 uint *wordp;
930 uint bit;
931 uint end_bit;
932 uint mask;
933
934 ASSERT(first < XFS_BLF_DATAMAP_SIZE * XFS_BLF_CHUNK * NBWORD);
935 ASSERT(last < XFS_BLF_DATAMAP_SIZE * XFS_BLF_CHUNK * NBWORD);
936
937 /*
938 * Convert byte offsets to bit numbers.
939 */
940 first_bit = first >> XFS_BLF_SHIFT;
941 last_bit = last >> XFS_BLF_SHIFT;
942
943 /*
944 * Calculate the total number of bits to be set.
945 */
946 bits_to_set = last_bit - first_bit + 1;
947
948 /*
949 * Get a pointer to the first word in the bitmap
950 * to set a bit in.
951 */
952 word_num = first_bit >> BIT_TO_WORD_SHIFT;
953 wordp = &map[word_num];
954
955 /*
956 * Calculate the starting bit in the first word.
957 */
958 bit = first_bit & (uint)(NBWORD - 1);
959
960 /*
961 * First set any bits in the first word of our range.
962 * If it starts at bit 0 of the word, it will be
963 * set below rather than here. That is what the variable
964 * bit tells us. The variable bits_set tracks the number
965 * of bits that have been set so far. End_bit is the number
966 * of the last bit to be set in this word plus one.
967 */
968 if (bit) {
969 end_bit = min(bit + bits_to_set, (uint)NBWORD);
970 mask = ((1U << (end_bit - bit)) - 1) << bit;
971 *wordp |= mask;
972 wordp++;
973 bits_set = end_bit - bit;
974 } else {
975 bits_set = 0;
976 }
977
978 /*
979 * Now set bits a whole word at a time that are between
980 * first_bit and last_bit.
981 */
982 while ((bits_to_set - bits_set) >= NBWORD) {
983 *wordp = 0xffffffff;
984 bits_set += NBWORD;
985 wordp++;
986 }
987
988 /*
989 * Finally, set any bits left to be set in one last partial word.
990 */
991 end_bit = bits_to_set - bits_set;
992 if (end_bit) {
993 mask = (1U << end_bit) - 1;
994 *wordp |= mask;
995 }
996 }
997
998 /*
999 * Mark bytes first through last inclusive as dirty in the buf
1000 * item's bitmap.
1001 */
1002 void
xfs_buf_item_log(struct xfs_buf_log_item * bip,uint first,uint last)1003 xfs_buf_item_log(
1004 struct xfs_buf_log_item *bip,
1005 uint first,
1006 uint last)
1007 {
1008 int i;
1009 uint start;
1010 uint end;
1011 struct xfs_buf *bp = bip->bli_buf;
1012
1013 /*
1014 * walk each buffer segment and mark them dirty appropriately.
1015 */
1016 start = 0;
1017 for (i = 0; i < bip->bli_format_count; i++) {
1018 if (start > last)
1019 break;
1020 end = start + BBTOB(bp->b_maps[i].bm_len) - 1;
1021
1022 /* skip to the map that includes the first byte to log */
1023 if (first > end) {
1024 start += BBTOB(bp->b_maps[i].bm_len);
1025 continue;
1026 }
1027
1028 /*
1029 * Trim the range to this segment and mark it in the bitmap.
1030 * Note that we must convert buffer offsets to segment relative
1031 * offsets (e.g., the first byte of each segment is byte 0 of
1032 * that segment).
1033 */
1034 if (first < start)
1035 first = start;
1036 if (end > last)
1037 end = last;
1038 xfs_buf_item_log_segment(first - start, end - start,
1039 &bip->bli_formats[i].blf_data_map[0]);
1040
1041 start += BBTOB(bp->b_maps[i].bm_len);
1042 }
1043 }
1044
1045
1046 /*
1047 * Return true if the buffer has any ranges logged/dirtied by a transaction,
1048 * false otherwise.
1049 */
1050 bool
xfs_buf_item_dirty_format(struct xfs_buf_log_item * bip)1051 xfs_buf_item_dirty_format(
1052 struct xfs_buf_log_item *bip)
1053 {
1054 int i;
1055
1056 for (i = 0; i < bip->bli_format_count; i++) {
1057 if (!xfs_bitmap_empty(bip->bli_formats[i].blf_data_map,
1058 bip->bli_formats[i].blf_map_size))
1059 return true;
1060 }
1061
1062 return false;
1063 }
1064
1065 void
xfs_buf_item_done(struct xfs_buf * bp)1066 xfs_buf_item_done(
1067 struct xfs_buf *bp)
1068 {
1069 struct xfs_buf_log_item *bip = bp->b_log_item;
1070
1071 /*
1072 * If we are forcibly shutting down, this may well be off the AIL
1073 * already. That's because we simulate the log-committed callbacks to
1074 * unpin these buffers. Or we may never have put this item on AIL
1075 * because of the transaction was aborted forcibly.
1076 * xfs_trans_ail_delete() takes care of these.
1077 *
1078 * Either way, AIL is useless if we're forcing a shutdown.
1079 *
1080 * Note that log recovery writes might have buffer items that are not on
1081 * the AIL even when the file system is not shut down.
1082 */
1083 xfs_trans_ail_delete(&bip->bli_item,
1084 xlog_in_recovery(bip->bli_item.li_log) ?
1085 0 : SHUTDOWN_CORRUPT_INCORE);
1086 xfs_buf_item_relse(bip);
1087 }
1088