[XFS] Remove xfs_macros.c, xfs_macros.h, rework headers a whole lot.
[deliverable/linux.git] / fs / xfs / xfs_buf_item.c
1 /*
2 * Copyright (c) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 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, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32 #include "xfs.h"
33 #include "xfs_fs.h"
34 #include "xfs_types.h"
35 #include "xfs_bit.h"
36 #include "xfs_log.h"
37 #include "xfs_inum.h"
38 #include "xfs_trans.h"
39 #include "xfs_sb.h"
40 #include "xfs_dir.h"
41 #include "xfs_dmapi.h"
42 #include "xfs_mount.h"
43 #include "xfs_buf_item.h"
44 #include "xfs_trans_priv.h"
45 #include "xfs_error.h"
46
47
48 kmem_zone_t *xfs_buf_item_zone;
49
50 #ifdef XFS_TRANS_DEBUG
51 /*
52 * This function uses an alternate strategy for tracking the bytes
53 * that the user requests to be logged. This can then be used
54 * in conjunction with the bli_orig array in the buf log item to
55 * catch bugs in our callers' code.
56 *
57 * We also double check the bits set in xfs_buf_item_log using a
58 * simple algorithm to check that every byte is accounted for.
59 */
60 STATIC void
61 xfs_buf_item_log_debug(
62 xfs_buf_log_item_t *bip,
63 uint first,
64 uint last)
65 {
66 uint x;
67 uint byte;
68 uint nbytes;
69 uint chunk_num;
70 uint word_num;
71 uint bit_num;
72 uint bit_set;
73 uint *wordp;
74
75 ASSERT(bip->bli_logged != NULL);
76 byte = first;
77 nbytes = last - first + 1;
78 bfset(bip->bli_logged, first, nbytes);
79 for (x = 0; x < nbytes; x++) {
80 chunk_num = byte >> XFS_BLI_SHIFT;
81 word_num = chunk_num >> BIT_TO_WORD_SHIFT;
82 bit_num = chunk_num & (NBWORD - 1);
83 wordp = &(bip->bli_format.blf_data_map[word_num]);
84 bit_set = *wordp & (1 << bit_num);
85 ASSERT(bit_set);
86 byte++;
87 }
88 }
89
90 /*
91 * This function is called when we flush something into a buffer without
92 * logging it. This happens for things like inodes which are logged
93 * separately from the buffer.
94 */
95 void
96 xfs_buf_item_flush_log_debug(
97 xfs_buf_t *bp,
98 uint first,
99 uint last)
100 {
101 xfs_buf_log_item_t *bip;
102 uint nbytes;
103
104 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
105 if ((bip == NULL) || (bip->bli_item.li_type != XFS_LI_BUF)) {
106 return;
107 }
108
109 ASSERT(bip->bli_logged != NULL);
110 nbytes = last - first + 1;
111 bfset(bip->bli_logged, first, nbytes);
112 }
113
114 /*
115 * This function is called to verify that our caller's have logged
116 * all the bytes that they changed.
117 *
118 * It does this by comparing the original copy of the buffer stored in
119 * the buf log item's bli_orig array to the current copy of the buffer
120 * and ensuring that all bytes which miscompare are set in the bli_logged
121 * array of the buf log item.
122 */
123 STATIC void
124 xfs_buf_item_log_check(
125 xfs_buf_log_item_t *bip)
126 {
127 char *orig;
128 char *buffer;
129 int x;
130 xfs_buf_t *bp;
131
132 ASSERT(bip->bli_orig != NULL);
133 ASSERT(bip->bli_logged != NULL);
134
135 bp = bip->bli_buf;
136 ASSERT(XFS_BUF_COUNT(bp) > 0);
137 ASSERT(XFS_BUF_PTR(bp) != NULL);
138 orig = bip->bli_orig;
139 buffer = XFS_BUF_PTR(bp);
140 for (x = 0; x < XFS_BUF_COUNT(bp); x++) {
141 if (orig[x] != buffer[x] && !btst(bip->bli_logged, x))
142 cmn_err(CE_PANIC,
143 "xfs_buf_item_log_check bip %x buffer %x orig %x index %d",
144 bip, bp, orig, x);
145 }
146 }
147 #else
148 #define xfs_buf_item_log_debug(x,y,z)
149 #define xfs_buf_item_log_check(x)
150 #endif
151
152 STATIC void xfs_buf_error_relse(xfs_buf_t *bp);
153 STATIC void xfs_buf_do_callbacks(xfs_buf_t *bp, xfs_log_item_t *lip);
154
155 /*
156 * This returns the number of log iovecs needed to log the
157 * given buf log item.
158 *
159 * It calculates this as 1 iovec for the buf log format structure
160 * and 1 for each stretch of non-contiguous chunks to be logged.
161 * Contiguous chunks are logged in a single iovec.
162 *
163 * If the XFS_BLI_STALE flag has been set, then log nothing.
164 */
165 STATIC uint
166 xfs_buf_item_size(
167 xfs_buf_log_item_t *bip)
168 {
169 uint nvecs;
170 int next_bit;
171 int last_bit;
172 xfs_buf_t *bp;
173
174 ASSERT(atomic_read(&bip->bli_refcount) > 0);
175 if (bip->bli_flags & XFS_BLI_STALE) {
176 /*
177 * The buffer is stale, so all we need to log
178 * is the buf log format structure with the
179 * cancel flag in it.
180 */
181 xfs_buf_item_trace("SIZE STALE", bip);
182 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
183 return 1;
184 }
185
186 bp = bip->bli_buf;
187 ASSERT(bip->bli_flags & XFS_BLI_LOGGED);
188 nvecs = 1;
189 last_bit = xfs_next_bit(bip->bli_format.blf_data_map,
190 bip->bli_format.blf_map_size, 0);
191 ASSERT(last_bit != -1);
192 nvecs++;
193 while (last_bit != -1) {
194 /*
195 * This takes the bit number to start looking from and
196 * returns the next set bit from there. It returns -1
197 * if there are no more bits set or the start bit is
198 * beyond the end of the bitmap.
199 */
200 next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
201 bip->bli_format.blf_map_size,
202 last_bit + 1);
203 /*
204 * If we run out of bits, leave the loop,
205 * else if we find a new set of bits bump the number of vecs,
206 * else keep scanning the current set of bits.
207 */
208 if (next_bit == -1) {
209 last_bit = -1;
210 } else if (next_bit != last_bit + 1) {
211 last_bit = next_bit;
212 nvecs++;
213 } else if (xfs_buf_offset(bp, next_bit * XFS_BLI_CHUNK) !=
214 (xfs_buf_offset(bp, last_bit * XFS_BLI_CHUNK) +
215 XFS_BLI_CHUNK)) {
216 last_bit = next_bit;
217 nvecs++;
218 } else {
219 last_bit++;
220 }
221 }
222
223 xfs_buf_item_trace("SIZE NORM", bip);
224 return nvecs;
225 }
226
227 /*
228 * This is called to fill in the vector of log iovecs for the
229 * given log buf item. It fills the first entry with a buf log
230 * format structure, and the rest point to contiguous chunks
231 * within the buffer.
232 */
233 STATIC void
234 xfs_buf_item_format(
235 xfs_buf_log_item_t *bip,
236 xfs_log_iovec_t *log_vector)
237 {
238 uint base_size;
239 uint nvecs;
240 xfs_log_iovec_t *vecp;
241 xfs_buf_t *bp;
242 int first_bit;
243 int last_bit;
244 int next_bit;
245 uint nbits;
246 uint buffer_offset;
247
248 ASSERT(atomic_read(&bip->bli_refcount) > 0);
249 ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
250 (bip->bli_flags & XFS_BLI_STALE));
251 bp = bip->bli_buf;
252 ASSERT(XFS_BUF_BP_ISMAPPED(bp));
253 vecp = log_vector;
254
255 /*
256 * The size of the base structure is the size of the
257 * declared structure plus the space for the extra words
258 * of the bitmap. We subtract one from the map size, because
259 * the first element of the bitmap is accounted for in the
260 * size of the base structure.
261 */
262 base_size =
263 (uint)(sizeof(xfs_buf_log_format_t) +
264 ((bip->bli_format.blf_map_size - 1) * sizeof(uint)));
265 vecp->i_addr = (xfs_caddr_t)&bip->bli_format;
266 vecp->i_len = base_size;
267 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_BFORMAT);
268 vecp++;
269 nvecs = 1;
270
271 if (bip->bli_flags & XFS_BLI_STALE) {
272 /*
273 * The buffer is stale, so all we need to log
274 * is the buf log format structure with the
275 * cancel flag in it.
276 */
277 xfs_buf_item_trace("FORMAT STALE", bip);
278 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
279 bip->bli_format.blf_size = nvecs;
280 return;
281 }
282
283 /*
284 * Fill in an iovec for each set of contiguous chunks.
285 */
286 first_bit = xfs_next_bit(bip->bli_format.blf_data_map,
287 bip->bli_format.blf_map_size, 0);
288 ASSERT(first_bit != -1);
289 last_bit = first_bit;
290 nbits = 1;
291 for (;;) {
292 /*
293 * This takes the bit number to start looking from and
294 * returns the next set bit from there. It returns -1
295 * if there are no more bits set or the start bit is
296 * beyond the end of the bitmap.
297 */
298 next_bit = xfs_next_bit(bip->bli_format.blf_data_map,
299 bip->bli_format.blf_map_size,
300 (uint)last_bit + 1);
301 /*
302 * If we run out of bits fill in the last iovec and get
303 * out of the loop.
304 * Else if we start a new set of bits then fill in the
305 * iovec for the series we were looking at and start
306 * counting the bits in the new one.
307 * Else we're still in the same set of bits so just
308 * keep counting and scanning.
309 */
310 if (next_bit == -1) {
311 buffer_offset = first_bit * XFS_BLI_CHUNK;
312 vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
313 vecp->i_len = nbits * XFS_BLI_CHUNK;
314 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_BCHUNK);
315 nvecs++;
316 break;
317 } else if (next_bit != last_bit + 1) {
318 buffer_offset = first_bit * XFS_BLI_CHUNK;
319 vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
320 vecp->i_len = nbits * XFS_BLI_CHUNK;
321 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_BCHUNK);
322 nvecs++;
323 vecp++;
324 first_bit = next_bit;
325 last_bit = next_bit;
326 nbits = 1;
327 } else if (xfs_buf_offset(bp, next_bit << XFS_BLI_SHIFT) !=
328 (xfs_buf_offset(bp, last_bit << XFS_BLI_SHIFT) +
329 XFS_BLI_CHUNK)) {
330 buffer_offset = first_bit * XFS_BLI_CHUNK;
331 vecp->i_addr = xfs_buf_offset(bp, buffer_offset);
332 vecp->i_len = nbits * XFS_BLI_CHUNK;
333 XLOG_VEC_SET_TYPE(vecp, XLOG_REG_TYPE_BCHUNK);
334 /* You would think we need to bump the nvecs here too, but we do not
335 * this number is used by recovery, and it gets confused by the boundary
336 * split here
337 * nvecs++;
338 */
339 vecp++;
340 first_bit = next_bit;
341 last_bit = next_bit;
342 nbits = 1;
343 } else {
344 last_bit++;
345 nbits++;
346 }
347 }
348 bip->bli_format.blf_size = nvecs;
349
350 /*
351 * Check to make sure everything is consistent.
352 */
353 xfs_buf_item_trace("FORMAT NORM", bip);
354 xfs_buf_item_log_check(bip);
355 }
356
357 /*
358 * This is called to pin the buffer associated with the buf log
359 * item in memory so it cannot be written out. Simply call bpin()
360 * on the buffer to do this.
361 */
362 STATIC void
363 xfs_buf_item_pin(
364 xfs_buf_log_item_t *bip)
365 {
366 xfs_buf_t *bp;
367
368 bp = bip->bli_buf;
369 ASSERT(XFS_BUF_ISBUSY(bp));
370 ASSERT(atomic_read(&bip->bli_refcount) > 0);
371 ASSERT((bip->bli_flags & XFS_BLI_LOGGED) ||
372 (bip->bli_flags & XFS_BLI_STALE));
373 xfs_buf_item_trace("PIN", bip);
374 xfs_buftrace("XFS_PIN", bp);
375 xfs_bpin(bp);
376 }
377
378
379 /*
380 * This is called to unpin the buffer associated with the buf log
381 * item which was previously pinned with a call to xfs_buf_item_pin().
382 * Just call bunpin() on the buffer to do this.
383 *
384 * Also drop the reference to the buf item for the current transaction.
385 * If the XFS_BLI_STALE flag is set and we are the last reference,
386 * then free up the buf log item and unlock the buffer.
387 */
388 STATIC void
389 xfs_buf_item_unpin(
390 xfs_buf_log_item_t *bip,
391 int stale)
392 {
393 xfs_mount_t *mp;
394 xfs_buf_t *bp;
395 int freed;
396 SPLDECL(s);
397
398 bp = bip->bli_buf;
399 ASSERT(bp != NULL);
400 ASSERT(XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *) == bip);
401 ASSERT(atomic_read(&bip->bli_refcount) > 0);
402 xfs_buf_item_trace("UNPIN", bip);
403 xfs_buftrace("XFS_UNPIN", bp);
404
405 freed = atomic_dec_and_test(&bip->bli_refcount);
406 mp = bip->bli_item.li_mountp;
407 xfs_bunpin(bp);
408 if (freed && stale) {
409 ASSERT(bip->bli_flags & XFS_BLI_STALE);
410 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
411 ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
412 ASSERT(XFS_BUF_ISSTALE(bp));
413 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
414 xfs_buf_item_trace("UNPIN STALE", bip);
415 xfs_buftrace("XFS_UNPIN STALE", bp);
416 /*
417 * If we get called here because of an IO error, we may
418 * or may not have the item on the AIL. xfs_trans_delete_ail()
419 * will take care of that situation.
420 * xfs_trans_delete_ail() drops the AIL lock.
421 */
422 if (bip->bli_flags & XFS_BLI_STALE_INODE) {
423 xfs_buf_do_callbacks(bp, (xfs_log_item_t *)bip);
424 XFS_BUF_SET_FSPRIVATE(bp, NULL);
425 XFS_BUF_CLR_IODONE_FUNC(bp);
426 } else {
427 AIL_LOCK(mp,s);
428 xfs_trans_delete_ail(mp, (xfs_log_item_t *)bip, s);
429 xfs_buf_item_relse(bp);
430 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) == NULL);
431 }
432 xfs_buf_relse(bp);
433 }
434 }
435
436 /*
437 * this is called from uncommit in the forced-shutdown path.
438 * we need to check to see if the reference count on the log item
439 * is going to drop to zero. If so, unpin will free the log item
440 * so we need to free the item's descriptor (that points to the item)
441 * in the transaction.
442 */
443 STATIC void
444 xfs_buf_item_unpin_remove(
445 xfs_buf_log_item_t *bip,
446 xfs_trans_t *tp)
447 {
448 xfs_buf_t *bp;
449 xfs_log_item_desc_t *lidp;
450 int stale = 0;
451
452 bp = bip->bli_buf;
453 /*
454 * will xfs_buf_item_unpin() call xfs_buf_item_relse()?
455 */
456 if ((atomic_read(&bip->bli_refcount) == 1) &&
457 (bip->bli_flags & XFS_BLI_STALE)) {
458 ASSERT(XFS_BUF_VALUSEMA(bip->bli_buf) <= 0);
459 xfs_buf_item_trace("UNPIN REMOVE", bip);
460 xfs_buftrace("XFS_UNPIN_REMOVE", bp);
461 /*
462 * yes -- clear the xaction descriptor in-use flag
463 * and free the chunk if required. We can safely
464 * do some work here and then call buf_item_unpin
465 * to do the rest because if the if is true, then
466 * we are holding the buffer locked so no one else
467 * will be able to bump up the refcount.
468 */
469 lidp = xfs_trans_find_item(tp, (xfs_log_item_t *) bip);
470 stale = lidp->lid_flags & XFS_LID_BUF_STALE;
471 xfs_trans_free_item(tp, lidp);
472 /*
473 * Since the transaction no longer refers to the buffer,
474 * the buffer should no longer refer to the transaction.
475 */
476 XFS_BUF_SET_FSPRIVATE2(bp, NULL);
477 }
478
479 xfs_buf_item_unpin(bip, stale);
480
481 return;
482 }
483
484 /*
485 * This is called to attempt to lock the buffer associated with this
486 * buf log item. Don't sleep on the buffer lock. If we can't get
487 * the lock right away, return 0. If we can get the lock, pull the
488 * buffer from the free list, mark it busy, and return 1.
489 */
490 STATIC uint
491 xfs_buf_item_trylock(
492 xfs_buf_log_item_t *bip)
493 {
494 xfs_buf_t *bp;
495
496 bp = bip->bli_buf;
497
498 if (XFS_BUF_ISPINNED(bp)) {
499 return XFS_ITEM_PINNED;
500 }
501
502 if (!XFS_BUF_CPSEMA(bp)) {
503 return XFS_ITEM_LOCKED;
504 }
505
506 /*
507 * Remove the buffer from the free list. Only do this
508 * if it's on the free list. Private buffers like the
509 * superblock buffer are not.
510 */
511 XFS_BUF_HOLD(bp);
512
513 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
514 xfs_buf_item_trace("TRYLOCK SUCCESS", bip);
515 return XFS_ITEM_SUCCESS;
516 }
517
518 /*
519 * Release the buffer associated with the buf log item.
520 * If there is no dirty logged data associated with the
521 * buffer recorded in the buf log item, then free the
522 * buf log item and remove the reference to it in the
523 * buffer.
524 *
525 * This call ignores the recursion count. It is only called
526 * when the buffer should REALLY be unlocked, regardless
527 * of the recursion count.
528 *
529 * If the XFS_BLI_HOLD flag is set in the buf log item, then
530 * free the log item if necessary but do not unlock the buffer.
531 * This is for support of xfs_trans_bhold(). Make sure the
532 * XFS_BLI_HOLD field is cleared if we don't free the item.
533 */
534 STATIC void
535 xfs_buf_item_unlock(
536 xfs_buf_log_item_t *bip)
537 {
538 int aborted;
539 xfs_buf_t *bp;
540 uint hold;
541
542 bp = bip->bli_buf;
543 xfs_buftrace("XFS_UNLOCK", bp);
544
545 /*
546 * Clear the buffer's association with this transaction.
547 */
548 XFS_BUF_SET_FSPRIVATE2(bp, NULL);
549
550 /*
551 * If this is a transaction abort, don't return early.
552 * Instead, allow the brelse to happen.
553 * Normally it would be done for stale (cancelled) buffers
554 * at unpin time, but we'll never go through the pin/unpin
555 * cycle if we abort inside commit.
556 */
557 aborted = (bip->bli_item.li_flags & XFS_LI_ABORTED) != 0;
558
559 /*
560 * If the buf item is marked stale, then don't do anything.
561 * We'll unlock the buffer and free the buf item when the
562 * buffer is unpinned for the last time.
563 */
564 if (bip->bli_flags & XFS_BLI_STALE) {
565 bip->bli_flags &= ~XFS_BLI_LOGGED;
566 xfs_buf_item_trace("UNLOCK STALE", bip);
567 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
568 if (!aborted)
569 return;
570 }
571
572 /*
573 * Drop the transaction's reference to the log item if
574 * it was not logged as part of the transaction. Otherwise
575 * we'll drop the reference in xfs_buf_item_unpin() when
576 * the transaction is really through with the buffer.
577 */
578 if (!(bip->bli_flags & XFS_BLI_LOGGED)) {
579 atomic_dec(&bip->bli_refcount);
580 } else {
581 /*
582 * Clear the logged flag since this is per
583 * transaction state.
584 */
585 bip->bli_flags &= ~XFS_BLI_LOGGED;
586 }
587
588 /*
589 * Before possibly freeing the buf item, determine if we should
590 * release the buffer at the end of this routine.
591 */
592 hold = bip->bli_flags & XFS_BLI_HOLD;
593 xfs_buf_item_trace("UNLOCK", bip);
594
595 /*
596 * If the buf item isn't tracking any data, free it.
597 * Otherwise, if XFS_BLI_HOLD is set clear it.
598 */
599 if (xfs_count_bits(bip->bli_format.blf_data_map,
600 bip->bli_format.blf_map_size, 0) == 0) {
601 xfs_buf_item_relse(bp);
602 } else if (hold) {
603 bip->bli_flags &= ~XFS_BLI_HOLD;
604 }
605
606 /*
607 * Release the buffer if XFS_BLI_HOLD was not set.
608 */
609 if (!hold) {
610 xfs_buf_relse(bp);
611 }
612 }
613
614 /*
615 * This is called to find out where the oldest active copy of the
616 * buf log item in the on disk log resides now that the last log
617 * write of it completed at the given lsn.
618 * We always re-log all the dirty data in a buffer, so usually the
619 * latest copy in the on disk log is the only one that matters. For
620 * those cases we simply return the given lsn.
621 *
622 * The one exception to this is for buffers full of newly allocated
623 * inodes. These buffers are only relogged with the XFS_BLI_INODE_BUF
624 * flag set, indicating that only the di_next_unlinked fields from the
625 * inodes in the buffers will be replayed during recovery. If the
626 * original newly allocated inode images have not yet been flushed
627 * when the buffer is so relogged, then we need to make sure that we
628 * keep the old images in the 'active' portion of the log. We do this
629 * by returning the original lsn of that transaction here rather than
630 * the current one.
631 */
632 STATIC xfs_lsn_t
633 xfs_buf_item_committed(
634 xfs_buf_log_item_t *bip,
635 xfs_lsn_t lsn)
636 {
637 xfs_buf_item_trace("COMMITTED", bip);
638 if ((bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF) &&
639 (bip->bli_item.li_lsn != 0)) {
640 return bip->bli_item.li_lsn;
641 }
642 return (lsn);
643 }
644
645 /*
646 * This is called when the transaction holding the buffer is aborted.
647 * Just behave as if the transaction had been cancelled. If we're shutting down
648 * and have aborted this transaction, we'll trap this buffer when it tries to
649 * get written out.
650 */
651 STATIC void
652 xfs_buf_item_abort(
653 xfs_buf_log_item_t *bip)
654 {
655 xfs_buf_t *bp;
656
657 bp = bip->bli_buf;
658 xfs_buftrace("XFS_ABORT", bp);
659 XFS_BUF_SUPER_STALE(bp);
660 xfs_buf_item_unlock(bip);
661 return;
662 }
663
664 /*
665 * This is called to asynchronously write the buffer associated with this
666 * buf log item out to disk. The buffer will already have been locked by
667 * a successful call to xfs_buf_item_trylock(). If the buffer still has
668 * B_DELWRI set, then get it going out to disk with a call to bawrite().
669 * If not, then just release the buffer.
670 */
671 STATIC void
672 xfs_buf_item_push(
673 xfs_buf_log_item_t *bip)
674 {
675 xfs_buf_t *bp;
676
677 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
678 xfs_buf_item_trace("PUSH", bip);
679
680 bp = bip->bli_buf;
681
682 if (XFS_BUF_ISDELAYWRITE(bp)) {
683 xfs_bawrite(bip->bli_item.li_mountp, bp);
684 } else {
685 xfs_buf_relse(bp);
686 }
687 }
688
689 /* ARGSUSED */
690 STATIC void
691 xfs_buf_item_committing(xfs_buf_log_item_t *bip, xfs_lsn_t commit_lsn)
692 {
693 }
694
695 /*
696 * This is the ops vector shared by all buf log items.
697 */
698 STATIC struct xfs_item_ops xfs_buf_item_ops = {
699 .iop_size = (uint(*)(xfs_log_item_t*))xfs_buf_item_size,
700 .iop_format = (void(*)(xfs_log_item_t*, xfs_log_iovec_t*))
701 xfs_buf_item_format,
702 .iop_pin = (void(*)(xfs_log_item_t*))xfs_buf_item_pin,
703 .iop_unpin = (void(*)(xfs_log_item_t*, int))xfs_buf_item_unpin,
704 .iop_unpin_remove = (void(*)(xfs_log_item_t*, xfs_trans_t *))
705 xfs_buf_item_unpin_remove,
706 .iop_trylock = (uint(*)(xfs_log_item_t*))xfs_buf_item_trylock,
707 .iop_unlock = (void(*)(xfs_log_item_t*))xfs_buf_item_unlock,
708 .iop_committed = (xfs_lsn_t(*)(xfs_log_item_t*, xfs_lsn_t))
709 xfs_buf_item_committed,
710 .iop_push = (void(*)(xfs_log_item_t*))xfs_buf_item_push,
711 .iop_abort = (void(*)(xfs_log_item_t*))xfs_buf_item_abort,
712 .iop_pushbuf = NULL,
713 .iop_committing = (void(*)(xfs_log_item_t*, xfs_lsn_t))
714 xfs_buf_item_committing
715 };
716
717
718 /*
719 * Allocate a new buf log item to go with the given buffer.
720 * Set the buffer's b_fsprivate field to point to the new
721 * buf log item. If there are other item's attached to the
722 * buffer (see xfs_buf_attach_iodone() below), then put the
723 * buf log item at the front.
724 */
725 void
726 xfs_buf_item_init(
727 xfs_buf_t *bp,
728 xfs_mount_t *mp)
729 {
730 xfs_log_item_t *lip;
731 xfs_buf_log_item_t *bip;
732 int chunks;
733 int map_size;
734
735 /*
736 * Check to see if there is already a buf log item for
737 * this buffer. If there is, it is guaranteed to be
738 * the first. If we do already have one, there is
739 * nothing to do here so return.
740 */
741 if (XFS_BUF_FSPRIVATE3(bp, xfs_mount_t *) != mp)
742 XFS_BUF_SET_FSPRIVATE3(bp, mp);
743 XFS_BUF_SET_BDSTRAT_FUNC(bp, xfs_bdstrat_cb);
744 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
745 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
746 if (lip->li_type == XFS_LI_BUF) {
747 return;
748 }
749 }
750
751 /*
752 * chunks is the number of XFS_BLI_CHUNK size pieces
753 * the buffer can be divided into. Make sure not to
754 * truncate any pieces. map_size is the size of the
755 * bitmap needed to describe the chunks of the buffer.
756 */
757 chunks = (int)((XFS_BUF_COUNT(bp) + (XFS_BLI_CHUNK - 1)) >> XFS_BLI_SHIFT);
758 map_size = (int)((chunks + NBWORD) >> BIT_TO_WORD_SHIFT);
759
760 bip = (xfs_buf_log_item_t*)kmem_zone_zalloc(xfs_buf_item_zone,
761 KM_SLEEP);
762 bip->bli_item.li_type = XFS_LI_BUF;
763 bip->bli_item.li_ops = &xfs_buf_item_ops;
764 bip->bli_item.li_mountp = mp;
765 bip->bli_buf = bp;
766 bip->bli_format.blf_type = XFS_LI_BUF;
767 bip->bli_format.blf_blkno = (__int64_t)XFS_BUF_ADDR(bp);
768 bip->bli_format.blf_len = (ushort)BTOBB(XFS_BUF_COUNT(bp));
769 bip->bli_format.blf_map_size = map_size;
770 #ifdef XFS_BLI_TRACE
771 bip->bli_trace = ktrace_alloc(XFS_BLI_TRACE_SIZE, KM_SLEEP);
772 #endif
773
774 #ifdef XFS_TRANS_DEBUG
775 /*
776 * Allocate the arrays for tracking what needs to be logged
777 * and what our callers request to be logged. bli_orig
778 * holds a copy of the original, clean buffer for comparison
779 * against, and bli_logged keeps a 1 bit flag per byte in
780 * the buffer to indicate which bytes the callers have asked
781 * to have logged.
782 */
783 bip->bli_orig = (char *)kmem_alloc(XFS_BUF_COUNT(bp), KM_SLEEP);
784 memcpy(bip->bli_orig, XFS_BUF_PTR(bp), XFS_BUF_COUNT(bp));
785 bip->bli_logged = (char *)kmem_zalloc(XFS_BUF_COUNT(bp) / NBBY, KM_SLEEP);
786 #endif
787
788 /*
789 * Put the buf item into the list of items attached to the
790 * buffer at the front.
791 */
792 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
793 bip->bli_item.li_bio_list =
794 XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
795 }
796 XFS_BUF_SET_FSPRIVATE(bp, bip);
797 }
798
799
800 /*
801 * Mark bytes first through last inclusive as dirty in the buf
802 * item's bitmap.
803 */
804 void
805 xfs_buf_item_log(
806 xfs_buf_log_item_t *bip,
807 uint first,
808 uint last)
809 {
810 uint first_bit;
811 uint last_bit;
812 uint bits_to_set;
813 uint bits_set;
814 uint word_num;
815 uint *wordp;
816 uint bit;
817 uint end_bit;
818 uint mask;
819
820 /*
821 * Mark the item as having some dirty data for
822 * quick reference in xfs_buf_item_dirty.
823 */
824 bip->bli_flags |= XFS_BLI_DIRTY;
825
826 /*
827 * Convert byte offsets to bit numbers.
828 */
829 first_bit = first >> XFS_BLI_SHIFT;
830 last_bit = last >> XFS_BLI_SHIFT;
831
832 /*
833 * Calculate the total number of bits to be set.
834 */
835 bits_to_set = last_bit - first_bit + 1;
836
837 /*
838 * Get a pointer to the first word in the bitmap
839 * to set a bit in.
840 */
841 word_num = first_bit >> BIT_TO_WORD_SHIFT;
842 wordp = &(bip->bli_format.blf_data_map[word_num]);
843
844 /*
845 * Calculate the starting bit in the first word.
846 */
847 bit = first_bit & (uint)(NBWORD - 1);
848
849 /*
850 * First set any bits in the first word of our range.
851 * If it starts at bit 0 of the word, it will be
852 * set below rather than here. That is what the variable
853 * bit tells us. The variable bits_set tracks the number
854 * of bits that have been set so far. End_bit is the number
855 * of the last bit to be set in this word plus one.
856 */
857 if (bit) {
858 end_bit = MIN(bit + bits_to_set, (uint)NBWORD);
859 mask = ((1 << (end_bit - bit)) - 1) << bit;
860 *wordp |= mask;
861 wordp++;
862 bits_set = end_bit - bit;
863 } else {
864 bits_set = 0;
865 }
866
867 /*
868 * Now set bits a whole word at a time that are between
869 * first_bit and last_bit.
870 */
871 while ((bits_to_set - bits_set) >= NBWORD) {
872 *wordp |= 0xffffffff;
873 bits_set += NBWORD;
874 wordp++;
875 }
876
877 /*
878 * Finally, set any bits left to be set in one last partial word.
879 */
880 end_bit = bits_to_set - bits_set;
881 if (end_bit) {
882 mask = (1 << end_bit) - 1;
883 *wordp |= mask;
884 }
885
886 xfs_buf_item_log_debug(bip, first, last);
887 }
888
889
890 /*
891 * Return 1 if the buffer has some data that has been logged (at any
892 * point, not just the current transaction) and 0 if not.
893 */
894 uint
895 xfs_buf_item_dirty(
896 xfs_buf_log_item_t *bip)
897 {
898 return (bip->bli_flags & XFS_BLI_DIRTY);
899 }
900
901 /*
902 * This is called when the buf log item is no longer needed. It should
903 * free the buf log item associated with the given buffer and clear
904 * the buffer's pointer to the buf log item. If there are no more
905 * items in the list, clear the b_iodone field of the buffer (see
906 * xfs_buf_attach_iodone() below).
907 */
908 void
909 xfs_buf_item_relse(
910 xfs_buf_t *bp)
911 {
912 xfs_buf_log_item_t *bip;
913
914 xfs_buftrace("XFS_RELSE", bp);
915 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
916 XFS_BUF_SET_FSPRIVATE(bp, bip->bli_item.li_bio_list);
917 if ((XFS_BUF_FSPRIVATE(bp, void *) == NULL) &&
918 (XFS_BUF_IODONE_FUNC(bp) != NULL)) {
919 ASSERT((XFS_BUF_ISUNINITIAL(bp)) == 0);
920 XFS_BUF_CLR_IODONE_FUNC(bp);
921 }
922
923 #ifdef XFS_TRANS_DEBUG
924 kmem_free(bip->bli_orig, XFS_BUF_COUNT(bp));
925 bip->bli_orig = NULL;
926 kmem_free(bip->bli_logged, XFS_BUF_COUNT(bp) / NBBY);
927 bip->bli_logged = NULL;
928 #endif /* XFS_TRANS_DEBUG */
929
930 #ifdef XFS_BLI_TRACE
931 ktrace_free(bip->bli_trace);
932 #endif
933 kmem_zone_free(xfs_buf_item_zone, bip);
934 }
935
936
937 /*
938 * Add the given log item with its callback to the list of callbacks
939 * to be called when the buffer's I/O completes. If it is not set
940 * already, set the buffer's b_iodone() routine to be
941 * xfs_buf_iodone_callbacks() and link the log item into the list of
942 * items rooted at b_fsprivate. Items are always added as the second
943 * entry in the list if there is a first, because the buf item code
944 * assumes that the buf log item is first.
945 */
946 void
947 xfs_buf_attach_iodone(
948 xfs_buf_t *bp,
949 void (*cb)(xfs_buf_t *, xfs_log_item_t *),
950 xfs_log_item_t *lip)
951 {
952 xfs_log_item_t *head_lip;
953
954 ASSERT(XFS_BUF_ISBUSY(bp));
955 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
956
957 lip->li_cb = cb;
958 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
959 head_lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
960 lip->li_bio_list = head_lip->li_bio_list;
961 head_lip->li_bio_list = lip;
962 } else {
963 XFS_BUF_SET_FSPRIVATE(bp, lip);
964 }
965
966 ASSERT((XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks) ||
967 (XFS_BUF_IODONE_FUNC(bp) == NULL));
968 XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks);
969 }
970
971 STATIC void
972 xfs_buf_do_callbacks(
973 xfs_buf_t *bp,
974 xfs_log_item_t *lip)
975 {
976 xfs_log_item_t *nlip;
977
978 while (lip != NULL) {
979 nlip = lip->li_bio_list;
980 ASSERT(lip->li_cb != NULL);
981 /*
982 * Clear the next pointer so we don't have any
983 * confusion if the item is added to another buf.
984 * Don't touch the log item after calling its
985 * callback, because it could have freed itself.
986 */
987 lip->li_bio_list = NULL;
988 lip->li_cb(bp, lip);
989 lip = nlip;
990 }
991 }
992
993 /*
994 * This is the iodone() function for buffers which have had callbacks
995 * attached to them by xfs_buf_attach_iodone(). It should remove each
996 * log item from the buffer's list and call the callback of each in turn.
997 * When done, the buffer's fsprivate field is set to NULL and the buffer
998 * is unlocked with a call to iodone().
999 */
1000 void
1001 xfs_buf_iodone_callbacks(
1002 xfs_buf_t *bp)
1003 {
1004 xfs_log_item_t *lip;
1005 static ulong lasttime;
1006 static xfs_buftarg_t *lasttarg;
1007 xfs_mount_t *mp;
1008
1009 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
1010 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
1011
1012 if (XFS_BUF_GETERROR(bp) != 0) {
1013 /*
1014 * If we've already decided to shutdown the filesystem
1015 * because of IO errors, there's no point in giving this
1016 * a retry.
1017 */
1018 mp = lip->li_mountp;
1019 if (XFS_FORCED_SHUTDOWN(mp)) {
1020 ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp);
1021 XFS_BUF_SUPER_STALE(bp);
1022 xfs_buftrace("BUF_IODONE_CB", bp);
1023 xfs_buf_do_callbacks(bp, lip);
1024 XFS_BUF_SET_FSPRIVATE(bp, NULL);
1025 XFS_BUF_CLR_IODONE_FUNC(bp);
1026
1027 /*
1028 * XFS_SHUT flag gets set when we go thru the
1029 * entire buffer cache and deliberately start
1030 * throwing away delayed write buffers.
1031 * Since there's no biowait done on those,
1032 * we should just brelse them.
1033 */
1034 if (XFS_BUF_ISSHUT(bp)) {
1035 XFS_BUF_UNSHUT(bp);
1036 xfs_buf_relse(bp);
1037 } else {
1038 xfs_biodone(bp);
1039 }
1040
1041 return;
1042 }
1043
1044 if ((XFS_BUF_TARGET(bp) != lasttarg) ||
1045 (time_after(jiffies, (lasttime + 5*HZ)))) {
1046 lasttime = jiffies;
1047 prdev("XFS write error in file system meta-data "
1048 "block 0x%llx in %s",
1049 XFS_BUF_TARGET(bp),
1050 (__uint64_t)XFS_BUF_ADDR(bp), mp->m_fsname);
1051 }
1052 lasttarg = XFS_BUF_TARGET(bp);
1053
1054 if (XFS_BUF_ISASYNC(bp)) {
1055 /*
1056 * If the write was asynchronous then noone will be
1057 * looking for the error. Clear the error state
1058 * and write the buffer out again delayed write.
1059 *
1060 * XXXsup This is OK, so long as we catch these
1061 * before we start the umount; we don't want these
1062 * DELWRI metadata bufs to be hanging around.
1063 */
1064 XFS_BUF_ERROR(bp,0); /* errno of 0 unsets the flag */
1065
1066 if (!(XFS_BUF_ISSTALE(bp))) {
1067 XFS_BUF_DELAYWRITE(bp);
1068 XFS_BUF_DONE(bp);
1069 XFS_BUF_SET_START(bp);
1070 }
1071 ASSERT(XFS_BUF_IODONE_FUNC(bp));
1072 xfs_buftrace("BUF_IODONE ASYNC", bp);
1073 xfs_buf_relse(bp);
1074 } else {
1075 /*
1076 * If the write of the buffer was not asynchronous,
1077 * then we want to make sure to return the error
1078 * to the caller of bwrite(). Because of this we
1079 * cannot clear the B_ERROR state at this point.
1080 * Instead we install a callback function that
1081 * will be called when the buffer is released, and
1082 * that routine will clear the error state and
1083 * set the buffer to be written out again after
1084 * some delay.
1085 */
1086 /* We actually overwrite the existing b-relse
1087 function at times, but we're gonna be shutting down
1088 anyway. */
1089 XFS_BUF_SET_BRELSE_FUNC(bp,xfs_buf_error_relse);
1090 XFS_BUF_DONE(bp);
1091 XFS_BUF_V_IODONESEMA(bp);
1092 }
1093 return;
1094 }
1095 #ifdef XFSERRORDEBUG
1096 xfs_buftrace("XFS BUFCB NOERR", bp);
1097 #endif
1098 xfs_buf_do_callbacks(bp, lip);
1099 XFS_BUF_SET_FSPRIVATE(bp, NULL);
1100 XFS_BUF_CLR_IODONE_FUNC(bp);
1101 xfs_biodone(bp);
1102 }
1103
1104 /*
1105 * This is a callback routine attached to a buffer which gets an error
1106 * when being written out synchronously.
1107 */
1108 STATIC void
1109 xfs_buf_error_relse(
1110 xfs_buf_t *bp)
1111 {
1112 xfs_log_item_t *lip;
1113 xfs_mount_t *mp;
1114
1115 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
1116 mp = (xfs_mount_t *)lip->li_mountp;
1117 ASSERT(XFS_BUF_TARGET(bp) == mp->m_ddev_targp);
1118
1119 XFS_BUF_STALE(bp);
1120 XFS_BUF_DONE(bp);
1121 XFS_BUF_UNDELAYWRITE(bp);
1122 XFS_BUF_ERROR(bp,0);
1123 xfs_buftrace("BUF_ERROR_RELSE", bp);
1124 if (! XFS_FORCED_SHUTDOWN(mp))
1125 xfs_force_shutdown(mp, XFS_METADATA_IO_ERROR);
1126 /*
1127 * We have to unpin the pinned buffers so do the
1128 * callbacks.
1129 */
1130 xfs_buf_do_callbacks(bp, lip);
1131 XFS_BUF_SET_FSPRIVATE(bp, NULL);
1132 XFS_BUF_CLR_IODONE_FUNC(bp);
1133 XFS_BUF_SET_BRELSE_FUNC(bp,NULL);
1134 xfs_buf_relse(bp);
1135 }
1136
1137
1138 /*
1139 * This is the iodone() function for buffers which have been
1140 * logged. It is called when they are eventually flushed out.
1141 * It should remove the buf item from the AIL, and free the buf item.
1142 * It is called by xfs_buf_iodone_callbacks() above which will take
1143 * care of cleaning up the buffer itself.
1144 */
1145 /* ARGSUSED */
1146 void
1147 xfs_buf_iodone(
1148 xfs_buf_t *bp,
1149 xfs_buf_log_item_t *bip)
1150 {
1151 struct xfs_mount *mp;
1152 SPLDECL(s);
1153
1154 ASSERT(bip->bli_buf == bp);
1155
1156 mp = bip->bli_item.li_mountp;
1157
1158 /*
1159 * If we are forcibly shutting down, this may well be
1160 * off the AIL already. That's because we simulate the
1161 * log-committed callbacks to unpin these buffers. Or we may never
1162 * have put this item on AIL because of the transaction was
1163 * aborted forcibly. xfs_trans_delete_ail() takes care of these.
1164 *
1165 * Either way, AIL is useless if we're forcing a shutdown.
1166 */
1167 AIL_LOCK(mp,s);
1168 /*
1169 * xfs_trans_delete_ail() drops the AIL lock.
1170 */
1171 xfs_trans_delete_ail(mp, (xfs_log_item_t *)bip, s);
1172
1173 #ifdef XFS_TRANS_DEBUG
1174 kmem_free(bip->bli_orig, XFS_BUF_COUNT(bp));
1175 bip->bli_orig = NULL;
1176 kmem_free(bip->bli_logged, XFS_BUF_COUNT(bp) / NBBY);
1177 bip->bli_logged = NULL;
1178 #endif /* XFS_TRANS_DEBUG */
1179
1180 #ifdef XFS_BLI_TRACE
1181 ktrace_free(bip->bli_trace);
1182 #endif
1183 kmem_zone_free(xfs_buf_item_zone, bip);
1184 }
1185
1186 #if defined(XFS_BLI_TRACE)
1187 void
1188 xfs_buf_item_trace(
1189 char *id,
1190 xfs_buf_log_item_t *bip)
1191 {
1192 xfs_buf_t *bp;
1193 ASSERT(bip->bli_trace != NULL);
1194
1195 bp = bip->bli_buf;
1196 ktrace_enter(bip->bli_trace,
1197 (void *)id,
1198 (void *)bip->bli_buf,
1199 (void *)((unsigned long)bip->bli_flags),
1200 (void *)((unsigned long)bip->bli_recur),
1201 (void *)((unsigned long)atomic_read(&bip->bli_refcount)),
1202 (void *)((unsigned long)
1203 (0xFFFFFFFF & XFS_BUF_ADDR(bp) >> 32)),
1204 (void *)((unsigned long)(0xFFFFFFFF & XFS_BUF_ADDR(bp))),
1205 (void *)((unsigned long)XFS_BUF_COUNT(bp)),
1206 (void *)((unsigned long)XFS_BUF_BFLAGS(bp)),
1207 XFS_BUF_FSPRIVATE(bp, void *),
1208 XFS_BUF_FSPRIVATE2(bp, void *),
1209 (void *)(unsigned long)XFS_BUF_ISPINNED(bp),
1210 (void *)XFS_BUF_IODONE_FUNC(bp),
1211 (void *)((unsigned long)(XFS_BUF_VALUSEMA(bp))),
1212 (void *)bip->bli_item.li_desc,
1213 (void *)((unsigned long)bip->bli_item.li_flags));
1214 }
1215 #endif /* XFS_BLI_TRACE */
This page took 0.056921 seconds and 5 git commands to generate.