]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/xfs/xfs_trans_buf.c
xfs: implement quota warnings via netlink
[mirror_ubuntu-bionic-kernel.git] / fs / xfs / xfs_trans_buf.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
1da177e4 13 *
7b718769
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
1da177e4 20#include "xfs_types.h"
a844f451 21#include "xfs_bit.h"
1da177e4 22#include "xfs_log.h"
a844f451 23#include "xfs_inum.h"
1da177e4 24#include "xfs_trans.h"
1da177e4
LT
25#include "xfs_sb.h"
26#include "xfs_ag.h"
a844f451 27#include "xfs_dir2.h"
1da177e4
LT
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
a844f451
NS
30#include "xfs_bmap_btree.h"
31#include "xfs_alloc_btree.h"
32#include "xfs_ialloc_btree.h"
a844f451
NS
33#include "xfs_dir2_sf.h"
34#include "xfs_attr_sf.h"
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
37#include "xfs_buf_item.h"
1da177e4
LT
38#include "xfs_trans_priv.h"
39#include "xfs_error.h"
40#include "xfs_rw.h"
0b1b213f 41#include "xfs_trace.h"
1da177e4
LT
42
43
44STATIC xfs_buf_t *xfs_trans_buf_item_match(xfs_trans_t *, xfs_buftarg_t *,
45 xfs_daddr_t, int);
46STATIC xfs_buf_t *xfs_trans_buf_item_match_all(xfs_trans_t *, xfs_buftarg_t *,
47 xfs_daddr_t, int);
48
49
50/*
51 * Get and lock the buffer for the caller if it is not already
52 * locked within the given transaction. If it is already locked
53 * within the transaction, just increment its lock recursion count
54 * and return a pointer to it.
55 *
56 * Use the fast path function xfs_trans_buf_item_match() or the buffer
57 * cache routine incore_match() to find the buffer
58 * if it is already owned by this transaction.
59 *
60 * If we don't already own the buffer, use get_buf() to get it.
61 * If it doesn't yet have an associated xfs_buf_log_item structure,
62 * then allocate one and add the item to this transaction.
63 *
64 * If the transaction pointer is NULL, make this just a normal
65 * get_buf() call.
66 */
67xfs_buf_t *
68xfs_trans_get_buf(xfs_trans_t *tp,
69 xfs_buftarg_t *target_dev,
70 xfs_daddr_t blkno,
71 int len,
72 uint flags)
73{
74 xfs_buf_t *bp;
75 xfs_buf_log_item_t *bip;
76
77 if (flags == 0)
78 flags = XFS_BUF_LOCK | XFS_BUF_MAPPED;
79
80 /*
81 * Default to a normal get_buf() call if the tp is NULL.
82 */
6ad112bf
CH
83 if (tp == NULL)
84 return xfs_buf_get(target_dev, blkno, len, flags | BUF_BUSY);
1da177e4
LT
85
86 /*
87 * If we find the buffer in the cache with this transaction
88 * pointer in its b_fsprivate2 field, then we know we already
89 * have it locked. In this case we just increment the lock
90 * recursion count and return the buffer to the caller.
91 */
92 if (tp->t_items.lic_next == NULL) {
93 bp = xfs_trans_buf_item_match(tp, target_dev, blkno, len);
94 } else {
95 bp = xfs_trans_buf_item_match_all(tp, target_dev, blkno, len);
96 }
97 if (bp != NULL) {
98 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
0b1b213f 99 if (XFS_FORCED_SHUTDOWN(tp->t_mountp))
1da177e4 100 XFS_BUF_SUPER_STALE(bp);
0b1b213f 101
1da177e4
LT
102 /*
103 * If the buffer is stale then it was binval'ed
104 * since last read. This doesn't matter since the
105 * caller isn't allowed to use the data anyway.
106 */
0b1b213f 107 else if (XFS_BUF_ISSTALE(bp))
1da177e4 108 ASSERT(!XFS_BUF_ISDELAYWRITE(bp));
0b1b213f 109
1da177e4
LT
110 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
111 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
112 ASSERT(bip != NULL);
113 ASSERT(atomic_read(&bip->bli_refcount) > 0);
114 bip->bli_recur++;
0b1b213f 115 trace_xfs_trans_get_buf_recur(bip);
1da177e4
LT
116 return (bp);
117 }
118
119 /*
120 * We always specify the BUF_BUSY flag within a transaction so
121 * that get_buf does not try to push out a delayed write buffer
122 * which might cause another transaction to take place (if the
123 * buffer was delayed alloc). Such recursive transactions can
124 * easily deadlock with our current transaction as well as cause
125 * us to run out of stack space.
126 */
6ad112bf 127 bp = xfs_buf_get(target_dev, blkno, len, flags | BUF_BUSY);
1da177e4
LT
128 if (bp == NULL) {
129 return NULL;
130 }
131
132 ASSERT(!XFS_BUF_GETERROR(bp));
133
134 /*
135 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
136 * it doesn't have one yet, then allocate one and initialize it.
137 * The checks to see if one is there are in xfs_buf_item_init().
138 */
139 xfs_buf_item_init(bp, tp->t_mountp);
140
141 /*
142 * Set the recursion count for the buffer within this transaction
143 * to 0.
144 */
145 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
146 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
147 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
148 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
149 bip->bli_recur = 0;
150
151 /*
152 * Take a reference for this transaction on the buf item.
153 */
154 atomic_inc(&bip->bli_refcount);
155
156 /*
157 * Get a log_item_desc to point at the new item.
158 */
159 (void) xfs_trans_add_item(tp, (xfs_log_item_t*)bip);
160
161 /*
162 * Initialize b_fsprivate2 so we can find it with incore_match()
163 * above.
164 */
165 XFS_BUF_SET_FSPRIVATE2(bp, tp);
166
0b1b213f 167 trace_xfs_trans_get_buf(bip);
1da177e4
LT
168 return (bp);
169}
170
171/*
172 * Get and lock the superblock buffer of this file system for the
173 * given transaction.
174 *
175 * We don't need to use incore_match() here, because the superblock
176 * buffer is a private buffer which we keep a pointer to in the
177 * mount structure.
178 */
179xfs_buf_t *
180xfs_trans_getsb(xfs_trans_t *tp,
181 struct xfs_mount *mp,
182 int flags)
183{
184 xfs_buf_t *bp;
185 xfs_buf_log_item_t *bip;
186
187 /*
188 * Default to just trying to lock the superblock buffer
189 * if tp is NULL.
190 */
191 if (tp == NULL) {
192 return (xfs_getsb(mp, flags));
193 }
194
195 /*
196 * If the superblock buffer already has this transaction
197 * pointer in its b_fsprivate2 field, then we know we already
198 * have it locked. In this case we just increment the lock
199 * recursion count and return the buffer to the caller.
200 */
201 bp = mp->m_sb_bp;
202 if (XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp) {
203 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
204 ASSERT(bip != NULL);
205 ASSERT(atomic_read(&bip->bli_refcount) > 0);
206 bip->bli_recur++;
0b1b213f 207 trace_xfs_trans_getsb_recur(bip);
1da177e4
LT
208 return (bp);
209 }
210
211 bp = xfs_getsb(mp, flags);
212 if (bp == NULL) {
213 return NULL;
214 }
215
216 /*
217 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
218 * it doesn't have one yet, then allocate one and initialize it.
219 * The checks to see if one is there are in xfs_buf_item_init().
220 */
221 xfs_buf_item_init(bp, mp);
222
223 /*
224 * Set the recursion count for the buffer within this transaction
225 * to 0.
226 */
227 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
228 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
229 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
230 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
231 bip->bli_recur = 0;
232
233 /*
234 * Take a reference for this transaction on the buf item.
235 */
236 atomic_inc(&bip->bli_refcount);
237
238 /*
239 * Get a log_item_desc to point at the new item.
240 */
241 (void) xfs_trans_add_item(tp, (xfs_log_item_t*)bip);
242
243 /*
244 * Initialize b_fsprivate2 so we can find it with incore_match()
245 * above.
246 */
247 XFS_BUF_SET_FSPRIVATE2(bp, tp);
248
0b1b213f 249 trace_xfs_trans_getsb(bip);
1da177e4
LT
250 return (bp);
251}
252
253#ifdef DEBUG
254xfs_buftarg_t *xfs_error_target;
255int xfs_do_error;
256int xfs_req_num;
257int xfs_error_mod = 33;
258#endif
259
260/*
261 * Get and lock the buffer for the caller if it is not already
262 * locked within the given transaction. If it has not yet been
263 * read in, read it from disk. If it is already locked
264 * within the transaction and already read in, just increment its
265 * lock recursion count and return a pointer to it.
266 *
267 * Use the fast path function xfs_trans_buf_item_match() or the buffer
268 * cache routine incore_match() to find the buffer
269 * if it is already owned by this transaction.
270 *
271 * If we don't already own the buffer, use read_buf() to get it.
272 * If it doesn't yet have an associated xfs_buf_log_item structure,
273 * then allocate one and add the item to this transaction.
274 *
275 * If the transaction pointer is NULL, make this just a normal
276 * read_buf() call.
277 */
278int
279xfs_trans_read_buf(
280 xfs_mount_t *mp,
281 xfs_trans_t *tp,
282 xfs_buftarg_t *target,
283 xfs_daddr_t blkno,
284 int len,
285 uint flags,
286 xfs_buf_t **bpp)
287{
288 xfs_buf_t *bp;
289 xfs_buf_log_item_t *bip;
290 int error;
291
292 if (flags == 0)
293 flags = XFS_BUF_LOCK | XFS_BUF_MAPPED;
294
295 /*
296 * Default to a normal get_buf() call if the tp is NULL.
297 */
298 if (tp == NULL) {
6ad112bf 299 bp = xfs_buf_read(target, blkno, len, flags | BUF_BUSY);
1da177e4 300 if (!bp)
a3f74ffb
DC
301 return (flags & XFS_BUF_TRYLOCK) ?
302 EAGAIN : XFS_ERROR(ENOMEM);
1da177e4 303
a0f7bfd3 304 if (XFS_BUF_GETERROR(bp) != 0) {
1da177e4
LT
305 xfs_ioerror_alert("xfs_trans_read_buf", mp,
306 bp, blkno);
307 error = XFS_BUF_GETERROR(bp);
308 xfs_buf_relse(bp);
309 return error;
310 }
311#ifdef DEBUG
a0f7bfd3 312 if (xfs_do_error) {
1da177e4
LT
313 if (xfs_error_target == target) {
314 if (((xfs_req_num++) % xfs_error_mod) == 0) {
315 xfs_buf_relse(bp);
b6574520 316 cmn_err(CE_DEBUG, "Returning error!\n");
1da177e4
LT
317 return XFS_ERROR(EIO);
318 }
319 }
320 }
321#endif
322 if (XFS_FORCED_SHUTDOWN(mp))
323 goto shutdown_abort;
324 *bpp = bp;
325 return 0;
326 }
327
328 /*
329 * If we find the buffer in the cache with this transaction
330 * pointer in its b_fsprivate2 field, then we know we already
331 * have it locked. If it is already read in we just increment
332 * the lock recursion count and return the buffer to the caller.
333 * If the buffer is not yet read in, then we read it in, increment
334 * the lock recursion count, and return it to the caller.
335 */
336 if (tp->t_items.lic_next == NULL) {
337 bp = xfs_trans_buf_item_match(tp, target, blkno, len);
338 } else {
339 bp = xfs_trans_buf_item_match_all(tp, target, blkno, len);
340 }
341 if (bp != NULL) {
342 ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
343 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
344 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
345 ASSERT((XFS_BUF_ISERROR(bp)) == 0);
346 if (!(XFS_BUF_ISDONE(bp))) {
0b1b213f 347 trace_xfs_trans_read_buf_io(bp, _RET_IP_);
1da177e4
LT
348 ASSERT(!XFS_BUF_ISASYNC(bp));
349 XFS_BUF_READ(bp);
350 xfsbdstrat(tp->t_mountp, bp);
d64e31a2
DC
351 error = xfs_iowait(bp);
352 if (error) {
1da177e4
LT
353 xfs_ioerror_alert("xfs_trans_read_buf", mp,
354 bp, blkno);
1da177e4
LT
355 xfs_buf_relse(bp);
356 /*
d64e31a2
DC
357 * We can gracefully recover from most read
358 * errors. Ones we can't are those that happen
359 * after the transaction's already dirty.
1da177e4
LT
360 */
361 if (tp->t_flags & XFS_TRANS_DIRTY)
362 xfs_force_shutdown(tp->t_mountp,
7d04a335 363 SHUTDOWN_META_IO_ERROR);
1da177e4
LT
364 return error;
365 }
366 }
367 /*
368 * We never locked this buf ourselves, so we shouldn't
369 * brelse it either. Just get out.
370 */
371 if (XFS_FORCED_SHUTDOWN(mp)) {
0b1b213f 372 trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
1da177e4
LT
373 *bpp = NULL;
374 return XFS_ERROR(EIO);
375 }
376
377
378 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
379 bip->bli_recur++;
380
381 ASSERT(atomic_read(&bip->bli_refcount) > 0);
0b1b213f 382 trace_xfs_trans_read_buf_recur(bip);
1da177e4
LT
383 *bpp = bp;
384 return 0;
385 }
386
387 /*
388 * We always specify the BUF_BUSY flag within a transaction so
389 * that get_buf does not try to push out a delayed write buffer
390 * which might cause another transaction to take place (if the
391 * buffer was delayed alloc). Such recursive transactions can
392 * easily deadlock with our current transaction as well as cause
393 * us to run out of stack space.
394 */
6ad112bf 395 bp = xfs_buf_read(target, blkno, len, flags | BUF_BUSY);
1da177e4
LT
396 if (bp == NULL) {
397 *bpp = NULL;
398 return 0;
399 }
400 if (XFS_BUF_GETERROR(bp) != 0) {
401 XFS_BUF_SUPER_STALE(bp);
1da177e4
LT
402 error = XFS_BUF_GETERROR(bp);
403
404 xfs_ioerror_alert("xfs_trans_read_buf", mp,
405 bp, blkno);
406 if (tp->t_flags & XFS_TRANS_DIRTY)
7d04a335 407 xfs_force_shutdown(tp->t_mountp, SHUTDOWN_META_IO_ERROR);
1da177e4
LT
408 xfs_buf_relse(bp);
409 return error;
410 }
411#ifdef DEBUG
412 if (xfs_do_error && !(tp->t_flags & XFS_TRANS_DIRTY)) {
413 if (xfs_error_target == target) {
414 if (((xfs_req_num++) % xfs_error_mod) == 0) {
415 xfs_force_shutdown(tp->t_mountp,
7d04a335 416 SHUTDOWN_META_IO_ERROR);
1da177e4 417 xfs_buf_relse(bp);
b6574520 418 cmn_err(CE_DEBUG, "Returning trans error!\n");
1da177e4
LT
419 return XFS_ERROR(EIO);
420 }
421 }
422 }
423#endif
424 if (XFS_FORCED_SHUTDOWN(mp))
425 goto shutdown_abort;
426
427 /*
428 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
429 * it doesn't have one yet, then allocate one and initialize it.
430 * The checks to see if one is there are in xfs_buf_item_init().
431 */
432 xfs_buf_item_init(bp, tp->t_mountp);
433
434 /*
435 * Set the recursion count for the buffer within this transaction
436 * to 0.
437 */
438 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t*);
439 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
440 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
441 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
442 bip->bli_recur = 0;
443
444 /*
445 * Take a reference for this transaction on the buf item.
446 */
447 atomic_inc(&bip->bli_refcount);
448
449 /*
450 * Get a log_item_desc to point at the new item.
451 */
452 (void) xfs_trans_add_item(tp, (xfs_log_item_t*)bip);
453
454 /*
455 * Initialize b_fsprivate2 so we can find it with incore_match()
456 * above.
457 */
458 XFS_BUF_SET_FSPRIVATE2(bp, tp);
459
0b1b213f 460 trace_xfs_trans_read_buf(bip);
1da177e4
LT
461 *bpp = bp;
462 return 0;
463
464shutdown_abort:
465 /*
466 * the theory here is that buffer is good but we're
467 * bailing out because the filesystem is being forcibly
468 * shut down. So we should leave the b_flags alone since
469 * the buffer's not staled and just get out.
470 */
471#if defined(DEBUG)
472 if (XFS_BUF_ISSTALE(bp) && XFS_BUF_ISDELAYWRITE(bp))
473 cmn_err(CE_NOTE, "about to pop assert, bp == 0x%p", bp);
474#endif
475 ASSERT((XFS_BUF_BFLAGS(bp) & (XFS_B_STALE|XFS_B_DELWRI)) !=
476 (XFS_B_STALE|XFS_B_DELWRI));
477
0b1b213f 478 trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
1da177e4
LT
479 xfs_buf_relse(bp);
480 *bpp = NULL;
481 return XFS_ERROR(EIO);
482}
483
484
485/*
486 * Release the buffer bp which was previously acquired with one of the
487 * xfs_trans_... buffer allocation routines if the buffer has not
488 * been modified within this transaction. If the buffer is modified
489 * within this transaction, do decrement the recursion count but do
490 * not release the buffer even if the count goes to 0. If the buffer is not
491 * modified within the transaction, decrement the recursion count and
492 * release the buffer if the recursion count goes to 0.
493 *
494 * If the buffer is to be released and it was not modified before
495 * this transaction began, then free the buf_log_item associated with it.
496 *
497 * If the transaction pointer is NULL, make this just a normal
498 * brelse() call.
499 */
500void
501xfs_trans_brelse(xfs_trans_t *tp,
502 xfs_buf_t *bp)
503{
504 xfs_buf_log_item_t *bip;
505 xfs_log_item_t *lip;
506 xfs_log_item_desc_t *lidp;
507
508 /*
509 * Default to a normal brelse() call if the tp is NULL.
510 */
511 if (tp == NULL) {
512 ASSERT(XFS_BUF_FSPRIVATE2(bp, void *) == NULL);
513 /*
514 * If there's a buf log item attached to the buffer,
515 * then let the AIL know that the buffer is being
516 * unlocked.
517 */
518 if (XFS_BUF_FSPRIVATE(bp, void *) != NULL) {
519 lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
520 if (lip->li_type == XFS_LI_BUF) {
521 bip = XFS_BUF_FSPRIVATE(bp,xfs_buf_log_item_t*);
783a2f65
DC
522 xfs_trans_unlocked_item(bip->bli_item.li_ailp,
523 lip);
1da177e4
LT
524 }
525 }
526 xfs_buf_relse(bp);
527 return;
528 }
529
530 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
531 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
532 ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
533 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
534 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
535 ASSERT(atomic_read(&bip->bli_refcount) > 0);
536
537 /*
538 * Find the item descriptor pointing to this buffer's
539 * log item. It must be there.
540 */
541 lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
542 ASSERT(lidp != NULL);
543
0b1b213f
CH
544 trace_xfs_trans_brelse(bip);
545
1da177e4
LT
546 /*
547 * If the release is just for a recursive lock,
548 * then decrement the count and return.
549 */
550 if (bip->bli_recur > 0) {
551 bip->bli_recur--;
1da177e4
LT
552 return;
553 }
554
555 /*
556 * If the buffer is dirty within this transaction, we can't
557 * release it until we commit.
558 */
0b1b213f 559 if (lidp->lid_flags & XFS_LID_DIRTY)
1da177e4 560 return;
1da177e4
LT
561
562 /*
563 * If the buffer has been invalidated, then we can't release
564 * it until the transaction commits to disk unless it is re-dirtied
565 * as part of this transaction. This prevents us from pulling
566 * the item from the AIL before we should.
567 */
0b1b213f 568 if (bip->bli_flags & XFS_BLI_STALE)
1da177e4 569 return;
1da177e4
LT
570
571 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
1da177e4
LT
572
573 /*
574 * Free up the log item descriptor tracking the released item.
575 */
576 xfs_trans_free_item(tp, lidp);
577
578 /*
579 * Clear the hold flag in the buf log item if it is set.
580 * We wouldn't want the next user of the buffer to
581 * get confused.
582 */
583 if (bip->bli_flags & XFS_BLI_HOLD) {
584 bip->bli_flags &= ~XFS_BLI_HOLD;
585 }
586
587 /*
588 * Drop our reference to the buf log item.
589 */
590 atomic_dec(&bip->bli_refcount);
591
592 /*
593 * If the buf item is not tracking data in the log, then
594 * we must free it before releasing the buffer back to the
595 * free pool. Before releasing the buffer to the free pool,
596 * clear the transaction pointer in b_fsprivate2 to dissolve
597 * its relation to this transaction.
598 */
599 if (!xfs_buf_item_dirty(bip)) {
600/***
601 ASSERT(bp->b_pincount == 0);
602***/
603 ASSERT(atomic_read(&bip->bli_refcount) == 0);
604 ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
605 ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF));
606 xfs_buf_item_relse(bp);
607 bip = NULL;
608 }
609 XFS_BUF_SET_FSPRIVATE2(bp, NULL);
610
611 /*
612 * If we've still got a buf log item on the buffer, then
613 * tell the AIL that the buffer is being unlocked.
614 */
615 if (bip != NULL) {
783a2f65 616 xfs_trans_unlocked_item(bip->bli_item.li_ailp,
1da177e4
LT
617 (xfs_log_item_t*)bip);
618 }
619
620 xfs_buf_relse(bp);
621 return;
622}
623
624/*
625 * Add the locked buffer to the transaction.
626 * The buffer must be locked, and it cannot be associated with any
627 * transaction.
628 *
629 * If the buffer does not yet have a buf log item associated with it,
630 * then allocate one for it. Then add the buf item to the transaction.
631 */
632void
633xfs_trans_bjoin(xfs_trans_t *tp,
634 xfs_buf_t *bp)
635{
636 xfs_buf_log_item_t *bip;
637
638 ASSERT(XFS_BUF_ISBUSY(bp));
639 ASSERT(XFS_BUF_FSPRIVATE2(bp, void *) == NULL);
640
641 /*
642 * The xfs_buf_log_item pointer is stored in b_fsprivate. If
643 * it doesn't have one yet, then allocate one and initialize it.
644 * The checks to see if one is there are in xfs_buf_item_init().
645 */
646 xfs_buf_item_init(bp, tp->t_mountp);
647 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
648 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
649 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
650 ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
651
652 /*
653 * Take a reference for this transaction on the buf item.
654 */
655 atomic_inc(&bip->bli_refcount);
656
657 /*
658 * Get a log_item_desc to point at the new item.
659 */
660 (void) xfs_trans_add_item(tp, (xfs_log_item_t *)bip);
661
662 /*
663 * Initialize b_fsprivate2 so we can find it with incore_match()
664 * in xfs_trans_get_buf() and friends above.
665 */
666 XFS_BUF_SET_FSPRIVATE2(bp, tp);
667
0b1b213f 668 trace_xfs_trans_bjoin(bip);
1da177e4
LT
669}
670
671/*
672 * Mark the buffer as not needing to be unlocked when the buf item's
673 * IOP_UNLOCK() routine is called. The buffer must already be locked
674 * and associated with the given transaction.
675 */
676/* ARGSUSED */
677void
678xfs_trans_bhold(xfs_trans_t *tp,
679 xfs_buf_t *bp)
680{
681 xfs_buf_log_item_t *bip;
682
683 ASSERT(XFS_BUF_ISBUSY(bp));
684 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
685 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
686
687 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
688 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
689 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
690 ASSERT(atomic_read(&bip->bli_refcount) > 0);
691 bip->bli_flags |= XFS_BLI_HOLD;
0b1b213f 692 trace_xfs_trans_bhold(bip);
1da177e4
LT
693}
694
efa092f3
TS
695/*
696 * Cancel the previous buffer hold request made on this buffer
697 * for this transaction.
698 */
699void
700xfs_trans_bhold_release(xfs_trans_t *tp,
701 xfs_buf_t *bp)
702{
703 xfs_buf_log_item_t *bip;
704
705 ASSERT(XFS_BUF_ISBUSY(bp));
706 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
707 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
708
709 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
710 ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
711 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_CANCEL));
712 ASSERT(atomic_read(&bip->bli_refcount) > 0);
713 ASSERT(bip->bli_flags & XFS_BLI_HOLD);
714 bip->bli_flags &= ~XFS_BLI_HOLD;
0b1b213f
CH
715
716 trace_xfs_trans_bhold_release(bip);
efa092f3
TS
717}
718
1da177e4
LT
719/*
720 * This is called to mark bytes first through last inclusive of the given
721 * buffer as needing to be logged when the transaction is committed.
722 * The buffer must already be associated with the given transaction.
723 *
724 * First and last are numbers relative to the beginning of this buffer,
725 * so the first byte in the buffer is numbered 0 regardless of the
726 * value of b_blkno.
727 */
728void
729xfs_trans_log_buf(xfs_trans_t *tp,
730 xfs_buf_t *bp,
731 uint first,
732 uint last)
733{
734 xfs_buf_log_item_t *bip;
735 xfs_log_item_desc_t *lidp;
736
737 ASSERT(XFS_BUF_ISBUSY(bp));
738 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
739 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
740 ASSERT((first <= last) && (last < XFS_BUF_COUNT(bp)));
741 ASSERT((XFS_BUF_IODONE_FUNC(bp) == NULL) ||
742 (XFS_BUF_IODONE_FUNC(bp) == xfs_buf_iodone_callbacks));
743
744 /*
745 * Mark the buffer as needing to be written out eventually,
746 * and set its iodone function to remove the buffer's buf log
747 * item from the AIL and free it when the buffer is flushed
748 * to disk. See xfs_buf_attach_iodone() for more details
749 * on li_cb and xfs_buf_iodone_callbacks().
750 * If we end up aborting this transaction, we trap this buffer
751 * inside the b_bdstrat callback so that this won't get written to
752 * disk.
753 */
754 XFS_BUF_DELAYWRITE(bp);
755 XFS_BUF_DONE(bp);
756
757 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
758 ASSERT(atomic_read(&bip->bli_refcount) > 0);
759 XFS_BUF_SET_IODONE_FUNC(bp, xfs_buf_iodone_callbacks);
760 bip->bli_item.li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*))xfs_buf_iodone;
761
0b1b213f
CH
762 trace_xfs_trans_log_buf(bip);
763
1da177e4
LT
764 /*
765 * If we invalidated the buffer within this transaction, then
766 * cancel the invalidation now that we're dirtying the buffer
767 * again. There are no races with the code in xfs_buf_item_unpin(),
768 * because we have a reference to the buffer this entire time.
769 */
770 if (bip->bli_flags & XFS_BLI_STALE) {
1da177e4
LT
771 bip->bli_flags &= ~XFS_BLI_STALE;
772 ASSERT(XFS_BUF_ISSTALE(bp));
773 XFS_BUF_UNSTALE(bp);
774 bip->bli_format.blf_flags &= ~XFS_BLI_CANCEL;
775 }
776
777 lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
778 ASSERT(lidp != NULL);
779
780 tp->t_flags |= XFS_TRANS_DIRTY;
781 lidp->lid_flags |= XFS_LID_DIRTY;
782 lidp->lid_flags &= ~XFS_LID_BUF_STALE;
783 bip->bli_flags |= XFS_BLI_LOGGED;
784 xfs_buf_item_log(bip, first, last);
1da177e4
LT
785}
786
787
788/*
789 * This called to invalidate a buffer that is being used within
790 * a transaction. Typically this is because the blocks in the
791 * buffer are being freed, so we need to prevent it from being
792 * written out when we're done. Allowing it to be written again
793 * might overwrite data in the free blocks if they are reallocated
794 * to a file.
795 *
796 * We prevent the buffer from being written out by clearing the
797 * B_DELWRI flag. We can't always
798 * get rid of the buf log item at this point, though, because
799 * the buffer may still be pinned by another transaction. If that
800 * is the case, then we'll wait until the buffer is committed to
801 * disk for the last time (we can tell by the ref count) and
802 * free it in xfs_buf_item_unpin(). Until it is cleaned up we
803 * will keep the buffer locked so that the buffer and buf log item
804 * are not reused.
805 */
806void
807xfs_trans_binval(
808 xfs_trans_t *tp,
809 xfs_buf_t *bp)
810{
811 xfs_log_item_desc_t *lidp;
812 xfs_buf_log_item_t *bip;
813
814 ASSERT(XFS_BUF_ISBUSY(bp));
815 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
816 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
817
818 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
819 lidp = xfs_trans_find_item(tp, (xfs_log_item_t*)bip);
820 ASSERT(lidp != NULL);
821 ASSERT(atomic_read(&bip->bli_refcount) > 0);
822
0b1b213f
CH
823 trace_xfs_trans_binval(bip);
824
1da177e4
LT
825 if (bip->bli_flags & XFS_BLI_STALE) {
826 /*
827 * If the buffer is already invalidated, then
828 * just return.
829 */
830 ASSERT(!(XFS_BUF_ISDELAYWRITE(bp)));
831 ASSERT(XFS_BUF_ISSTALE(bp));
832 ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
833 ASSERT(!(bip->bli_format.blf_flags & XFS_BLI_INODE_BUF));
834 ASSERT(bip->bli_format.blf_flags & XFS_BLI_CANCEL);
835 ASSERT(lidp->lid_flags & XFS_LID_DIRTY);
836 ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
1da177e4
LT
837 return;
838 }
839
840 /*
841 * Clear the dirty bit in the buffer and set the STALE flag
842 * in the buf log item. The STALE flag will be used in
843 * xfs_buf_item_unpin() to determine if it should clean up
844 * when the last reference to the buf item is given up.
845 * We set the XFS_BLI_CANCEL flag in the buf log format structure
846 * and log the buf item. This will be used at recovery time
847 * to determine that copies of the buffer in the log before
848 * this should not be replayed.
849 * We mark the item descriptor and the transaction dirty so
850 * that we'll hold the buffer until after the commit.
851 *
852 * Since we're invalidating the buffer, we also clear the state
853 * about which parts of the buffer have been logged. We also
854 * clear the flag indicating that this is an inode buffer since
855 * the data in the buffer will no longer be valid.
856 *
857 * We set the stale bit in the buffer as well since we're getting
858 * rid of it.
859 */
860 XFS_BUF_UNDELAYWRITE(bp);
861 XFS_BUF_STALE(bp);
862 bip->bli_flags |= XFS_BLI_STALE;
863 bip->bli_flags &= ~(XFS_BLI_LOGGED | XFS_BLI_DIRTY);
864 bip->bli_format.blf_flags &= ~XFS_BLI_INODE_BUF;
865 bip->bli_format.blf_flags |= XFS_BLI_CANCEL;
866 memset((char *)(bip->bli_format.blf_data_map), 0,
867 (bip->bli_format.blf_map_size * sizeof(uint)));
868 lidp->lid_flags |= XFS_LID_DIRTY|XFS_LID_BUF_STALE;
869 tp->t_flags |= XFS_TRANS_DIRTY;
1da177e4
LT
870}
871
872/*
873 * This call is used to indicate that the buffer contains on-disk
874 * inodes which must be handled specially during recovery. They
875 * require special handling because only the di_next_unlinked from
876 * the inodes in the buffer should be recovered. The rest of the
877 * data in the buffer is logged via the inodes themselves.
878 *
879 * All we do is set the XFS_BLI_INODE_BUF flag in the buffer's log
880 * format structure so that we'll know what to do at recovery time.
881 */
882/* ARGSUSED */
883void
884xfs_trans_inode_buf(
885 xfs_trans_t *tp,
886 xfs_buf_t *bp)
887{
888 xfs_buf_log_item_t *bip;
889
890 ASSERT(XFS_BUF_ISBUSY(bp));
891 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
892 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
893
894 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
895 ASSERT(atomic_read(&bip->bli_refcount) > 0);
896
897 bip->bli_format.blf_flags |= XFS_BLI_INODE_BUF;
898}
899
900/*
901 * This call is used to indicate that the buffer is going to
902 * be staled and was an inode buffer. This means it gets
903 * special processing during unpin - where any inodes
904 * associated with the buffer should be removed from ail.
905 * There is also special processing during recovery,
906 * any replay of the inodes in the buffer needs to be
907 * prevented as the buffer may have been reused.
908 */
909void
910xfs_trans_stale_inode_buf(
911 xfs_trans_t *tp,
912 xfs_buf_t *bp)
913{
914 xfs_buf_log_item_t *bip;
915
916 ASSERT(XFS_BUF_ISBUSY(bp));
917 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
918 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
919
920 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
921 ASSERT(atomic_read(&bip->bli_refcount) > 0);
922
923 bip->bli_flags |= XFS_BLI_STALE_INODE;
924 bip->bli_item.li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*))
925 xfs_buf_iodone;
926}
927
928
929
930/*
931 * Mark the buffer as being one which contains newly allocated
932 * inodes. We need to make sure that even if this buffer is
933 * relogged as an 'inode buf' we still recover all of the inode
934 * images in the face of a crash. This works in coordination with
935 * xfs_buf_item_committed() to ensure that the buffer remains in the
936 * AIL at its original location even after it has been relogged.
937 */
938/* ARGSUSED */
939void
940xfs_trans_inode_alloc_buf(
941 xfs_trans_t *tp,
942 xfs_buf_t *bp)
943{
944 xfs_buf_log_item_t *bip;
945
946 ASSERT(XFS_BUF_ISBUSY(bp));
947 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
948 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
949
950 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
951 ASSERT(atomic_read(&bip->bli_refcount) > 0);
952
953 bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
954}
955
956
957/*
958 * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
959 * dquots. However, unlike in inode buffer recovery, dquot buffers get
960 * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
961 * The only thing that makes dquot buffers different from regular
962 * buffers is that we must not replay dquot bufs when recovering
963 * if a _corresponding_ quotaoff has happened. We also have to distinguish
964 * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
965 * can be turned off independently.
966 */
967/* ARGSUSED */
968void
969xfs_trans_dquot_buf(
970 xfs_trans_t *tp,
971 xfs_buf_t *bp,
972 uint type)
973{
974 xfs_buf_log_item_t *bip;
975
976 ASSERT(XFS_BUF_ISBUSY(bp));
977 ASSERT(XFS_BUF_FSPRIVATE2(bp, xfs_trans_t *) == tp);
978 ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
979 ASSERT(type == XFS_BLI_UDQUOT_BUF ||
c8ad20ff 980 type == XFS_BLI_PDQUOT_BUF ||
1da177e4
LT
981 type == XFS_BLI_GDQUOT_BUF);
982
983 bip = XFS_BUF_FSPRIVATE(bp, xfs_buf_log_item_t *);
984 ASSERT(atomic_read(&bip->bli_refcount) > 0);
985
986 bip->bli_format.blf_flags |= type;
987}
988
989/*
990 * Check to see if a buffer matching the given parameters is already
991 * a part of the given transaction. Only check the first, embedded
992 * chunk, since we don't want to spend all day scanning large transactions.
993 */
994STATIC xfs_buf_t *
995xfs_trans_buf_item_match(
996 xfs_trans_t *tp,
997 xfs_buftarg_t *target,
998 xfs_daddr_t blkno,
999 int len)
1000{
1001 xfs_log_item_chunk_t *licp;
1002 xfs_log_item_desc_t *lidp;
1003 xfs_buf_log_item_t *blip;
1004 xfs_buf_t *bp;
1005 int i;
1006
1007 bp = NULL;
1008 len = BBTOB(len);
1009 licp = &tp->t_items;
39dab9d7 1010 if (!xfs_lic_are_all_free(licp)) {
1da177e4
LT
1011 for (i = 0; i < licp->lic_unused; i++) {
1012 /*
1013 * Skip unoccupied slots.
1014 */
39dab9d7 1015 if (xfs_lic_isfree(licp, i)) {
1da177e4
LT
1016 continue;
1017 }
1018
39dab9d7 1019 lidp = xfs_lic_slot(licp, i);
1da177e4
LT
1020 blip = (xfs_buf_log_item_t *)lidp->lid_item;
1021 if (blip->bli_item.li_type != XFS_LI_BUF) {
1022 continue;
1023 }
1024
1025 bp = blip->bli_buf;
1026 if ((XFS_BUF_TARGET(bp) == target) &&
1027 (XFS_BUF_ADDR(bp) == blkno) &&
1028 (XFS_BUF_COUNT(bp) == len)) {
1029 /*
1030 * We found it. Break out and
1031 * return the pointer to the buffer.
1032 */
1033 break;
1034 } else {
1035 bp = NULL;
1036 }
1037 }
1038 }
1039 return bp;
1040}
1041
1042/*
1043 * Check to see if a buffer matching the given parameters is already
1044 * a part of the given transaction. Check all the chunks, we
1045 * want to be thorough.
1046 */
1047STATIC xfs_buf_t *
1048xfs_trans_buf_item_match_all(
1049 xfs_trans_t *tp,
1050 xfs_buftarg_t *target,
1051 xfs_daddr_t blkno,
1052 int len)
1053{
1054 xfs_log_item_chunk_t *licp;
1055 xfs_log_item_desc_t *lidp;
1056 xfs_buf_log_item_t *blip;
1057 xfs_buf_t *bp;
1058 int i;
1059
1060 bp = NULL;
1061 len = BBTOB(len);
1062 for (licp = &tp->t_items; licp != NULL; licp = licp->lic_next) {
39dab9d7 1063 if (xfs_lic_are_all_free(licp)) {
1da177e4
LT
1064 ASSERT(licp == &tp->t_items);
1065 ASSERT(licp->lic_next == NULL);
1066 return NULL;
1067 }
1068 for (i = 0; i < licp->lic_unused; i++) {
1069 /*
1070 * Skip unoccupied slots.
1071 */
39dab9d7 1072 if (xfs_lic_isfree(licp, i)) {
1da177e4
LT
1073 continue;
1074 }
1075
39dab9d7 1076 lidp = xfs_lic_slot(licp, i);
1da177e4
LT
1077 blip = (xfs_buf_log_item_t *)lidp->lid_item;
1078 if (blip->bli_item.li_type != XFS_LI_BUF) {
1079 continue;
1080 }
1081
1082 bp = blip->bli_buf;
1083 if ((XFS_BUF_TARGET(bp) == target) &&
1084 (XFS_BUF_ADDR(bp) == blkno) &&
1085 (XFS_BUF_COUNT(bp) == len)) {
1086 /*
1087 * We found it. Break out and
1088 * return the pointer to the buffer.
1089 */
1090 return bp;
1091 }
1092 }
1093 }
1094 return NULL;
1095}