xfs: merge xfs_ag.h into xfs_format.h
[firefly-linux-kernel-4.4.55.git] / fs / xfs / xfs_trans_buf.c
1 /*
2  * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
3  * All Rights Reserved.
4  *
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
7  * published by the Free Software Foundation.
8  *
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.
13  *
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
17  */
18 #include "xfs.h"
19 #include "xfs_fs.h"
20 #include "xfs_shared.h"
21 #include "xfs_format.h"
22 #include "xfs_log_format.h"
23 #include "xfs_trans_resv.h"
24 #include "xfs_sb.h"
25 #include "xfs_mount.h"
26 #include "xfs_inode.h"
27 #include "xfs_trans.h"
28 #include "xfs_buf_item.h"
29 #include "xfs_trans_priv.h"
30 #include "xfs_error.h"
31 #include "xfs_trace.h"
32
33 /*
34  * Check to see if a buffer matching the given parameters is already
35  * a part of the given transaction.
36  */
37 STATIC struct xfs_buf *
38 xfs_trans_buf_item_match(
39         struct xfs_trans        *tp,
40         struct xfs_buftarg      *target,
41         struct xfs_buf_map      *map,
42         int                     nmaps)
43 {
44         struct xfs_log_item_desc *lidp;
45         struct xfs_buf_log_item *blip;
46         int                     len = 0;
47         int                     i;
48
49         for (i = 0; i < nmaps; i++)
50                 len += map[i].bm_len;
51
52         list_for_each_entry(lidp, &tp->t_items, lid_trans) {
53                 blip = (struct xfs_buf_log_item *)lidp->lid_item;
54                 if (blip->bli_item.li_type == XFS_LI_BUF &&
55                     blip->bli_buf->b_target == target &&
56                     XFS_BUF_ADDR(blip->bli_buf) == map[0].bm_bn &&
57                     blip->bli_buf->b_length == len) {
58                         ASSERT(blip->bli_buf->b_map_count == nmaps);
59                         return blip->bli_buf;
60                 }
61         }
62
63         return NULL;
64 }
65
66 /*
67  * Add the locked buffer to the transaction.
68  *
69  * The buffer must be locked, and it cannot be associated with any
70  * transaction.
71  *
72  * If the buffer does not yet have a buf log item associated with it,
73  * then allocate one for it.  Then add the buf item to the transaction.
74  */
75 STATIC void
76 _xfs_trans_bjoin(
77         struct xfs_trans        *tp,
78         struct xfs_buf          *bp,
79         int                     reset_recur)
80 {
81         struct xfs_buf_log_item *bip;
82
83         ASSERT(bp->b_transp == NULL);
84
85         /*
86          * The xfs_buf_log_item pointer is stored in b_fsprivate.  If
87          * it doesn't have one yet, then allocate one and initialize it.
88          * The checks to see if one is there are in xfs_buf_item_init().
89          */
90         xfs_buf_item_init(bp, tp->t_mountp);
91         bip = bp->b_fspriv;
92         ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
93         ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
94         ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
95         if (reset_recur)
96                 bip->bli_recur = 0;
97
98         /*
99          * Take a reference for this transaction on the buf item.
100          */
101         atomic_inc(&bip->bli_refcount);
102
103         /*
104          * Get a log_item_desc to point at the new item.
105          */
106         xfs_trans_add_item(tp, &bip->bli_item);
107
108         /*
109          * Initialize b_fsprivate2 so we can find it with incore_match()
110          * in xfs_trans_get_buf() and friends above.
111          */
112         bp->b_transp = tp;
113
114 }
115
116 void
117 xfs_trans_bjoin(
118         struct xfs_trans        *tp,
119         struct xfs_buf          *bp)
120 {
121         _xfs_trans_bjoin(tp, bp, 0);
122         trace_xfs_trans_bjoin(bp->b_fspriv);
123 }
124
125 /*
126  * Get and lock the buffer for the caller if it is not already
127  * locked within the given transaction.  If it is already locked
128  * within the transaction, just increment its lock recursion count
129  * and return a pointer to it.
130  *
131  * If the transaction pointer is NULL, make this just a normal
132  * get_buf() call.
133  */
134 struct xfs_buf *
135 xfs_trans_get_buf_map(
136         struct xfs_trans        *tp,
137         struct xfs_buftarg      *target,
138         struct xfs_buf_map      *map,
139         int                     nmaps,
140         xfs_buf_flags_t         flags)
141 {
142         xfs_buf_t               *bp;
143         xfs_buf_log_item_t      *bip;
144
145         if (!tp)
146                 return xfs_buf_get_map(target, map, nmaps, flags);
147
148         /*
149          * If we find the buffer in the cache with this transaction
150          * pointer in its b_fsprivate2 field, then we know we already
151          * have it locked.  In this case we just increment the lock
152          * recursion count and return the buffer to the caller.
153          */
154         bp = xfs_trans_buf_item_match(tp, target, map, nmaps);
155         if (bp != NULL) {
156                 ASSERT(xfs_buf_islocked(bp));
157                 if (XFS_FORCED_SHUTDOWN(tp->t_mountp)) {
158                         xfs_buf_stale(bp);
159                         XFS_BUF_DONE(bp);
160                 }
161
162                 ASSERT(bp->b_transp == tp);
163                 bip = bp->b_fspriv;
164                 ASSERT(bip != NULL);
165                 ASSERT(atomic_read(&bip->bli_refcount) > 0);
166                 bip->bli_recur++;
167                 trace_xfs_trans_get_buf_recur(bip);
168                 return bp;
169         }
170
171         bp = xfs_buf_get_map(target, map, nmaps, flags);
172         if (bp == NULL) {
173                 return NULL;
174         }
175
176         ASSERT(!bp->b_error);
177
178         _xfs_trans_bjoin(tp, bp, 1);
179         trace_xfs_trans_get_buf(bp->b_fspriv);
180         return bp;
181 }
182
183 /*
184  * Get and lock the superblock buffer of this file system for the
185  * given transaction.
186  *
187  * We don't need to use incore_match() here, because the superblock
188  * buffer is a private buffer which we keep a pointer to in the
189  * mount structure.
190  */
191 xfs_buf_t *
192 xfs_trans_getsb(xfs_trans_t     *tp,
193                 struct xfs_mount *mp,
194                 int             flags)
195 {
196         xfs_buf_t               *bp;
197         xfs_buf_log_item_t      *bip;
198
199         /*
200          * Default to just trying to lock the superblock buffer
201          * if tp is NULL.
202          */
203         if (tp == NULL)
204                 return xfs_getsb(mp, flags);
205
206         /*
207          * If the superblock buffer already has this transaction
208          * pointer in its b_fsprivate2 field, then we know we already
209          * have it locked.  In this case we just increment the lock
210          * recursion count and return the buffer to the caller.
211          */
212         bp = mp->m_sb_bp;
213         if (bp->b_transp == tp) {
214                 bip = bp->b_fspriv;
215                 ASSERT(bip != NULL);
216                 ASSERT(atomic_read(&bip->bli_refcount) > 0);
217                 bip->bli_recur++;
218                 trace_xfs_trans_getsb_recur(bip);
219                 return bp;
220         }
221
222         bp = xfs_getsb(mp, flags);
223         if (bp == NULL)
224                 return NULL;
225
226         _xfs_trans_bjoin(tp, bp, 1);
227         trace_xfs_trans_getsb(bp->b_fspriv);
228         return bp;
229 }
230
231 #ifdef DEBUG
232 xfs_buftarg_t *xfs_error_target;
233 int     xfs_do_error;
234 int     xfs_req_num;
235 int     xfs_error_mod = 33;
236 #endif
237
238 /*
239  * Get and lock the buffer for the caller if it is not already
240  * locked within the given transaction.  If it has not yet been
241  * read in, read it from disk. If it is already locked
242  * within the transaction and already read in, just increment its
243  * lock recursion count and return a pointer to it.
244  *
245  * If the transaction pointer is NULL, make this just a normal
246  * read_buf() call.
247  */
248 int
249 xfs_trans_read_buf_map(
250         struct xfs_mount        *mp,
251         struct xfs_trans        *tp,
252         struct xfs_buftarg      *target,
253         struct xfs_buf_map      *map,
254         int                     nmaps,
255         xfs_buf_flags_t         flags,
256         struct xfs_buf          **bpp,
257         const struct xfs_buf_ops *ops)
258 {
259         xfs_buf_t               *bp;
260         xfs_buf_log_item_t      *bip;
261         int                     error;
262
263         *bpp = NULL;
264         if (!tp) {
265                 bp = xfs_buf_read_map(target, map, nmaps, flags, ops);
266                 if (!bp)
267                         return (flags & XBF_TRYLOCK) ?
268                                         -EAGAIN : -ENOMEM;
269
270                 if (bp->b_error) {
271                         error = bp->b_error;
272                         xfs_buf_ioerror_alert(bp, __func__);
273                         XFS_BUF_UNDONE(bp);
274                         xfs_buf_stale(bp);
275                         xfs_buf_relse(bp);
276
277                         /* bad CRC means corrupted metadata */
278                         if (error == -EFSBADCRC)
279                                 error = -EFSCORRUPTED;
280                         return error;
281                 }
282 #ifdef DEBUG
283                 if (xfs_do_error) {
284                         if (xfs_error_target == target) {
285                                 if (((xfs_req_num++) % xfs_error_mod) == 0) {
286                                         xfs_buf_relse(bp);
287                                         xfs_debug(mp, "Returning error!");
288                                         return -EIO;
289                                 }
290                         }
291                 }
292 #endif
293                 if (XFS_FORCED_SHUTDOWN(mp))
294                         goto shutdown_abort;
295                 *bpp = bp;
296                 return 0;
297         }
298
299         /*
300          * If we find the buffer in the cache with this transaction
301          * pointer in its b_fsprivate2 field, then we know we already
302          * have it locked.  If it is already read in we just increment
303          * the lock recursion count and return the buffer to the caller.
304          * If the buffer is not yet read in, then we read it in, increment
305          * the lock recursion count, and return it to the caller.
306          */
307         bp = xfs_trans_buf_item_match(tp, target, map, nmaps);
308         if (bp != NULL) {
309                 ASSERT(xfs_buf_islocked(bp));
310                 ASSERT(bp->b_transp == tp);
311                 ASSERT(bp->b_fspriv != NULL);
312                 ASSERT(!bp->b_error);
313                 if (!(XFS_BUF_ISDONE(bp))) {
314                         trace_xfs_trans_read_buf_io(bp, _RET_IP_);
315                         ASSERT(!XFS_BUF_ISASYNC(bp));
316                         ASSERT(bp->b_iodone == NULL);
317                         XFS_BUF_READ(bp);
318                         bp->b_ops = ops;
319
320                         error = xfs_buf_submit_wait(bp);
321                         if (error) {
322                                 if (!XFS_FORCED_SHUTDOWN(mp))
323                                         xfs_buf_ioerror_alert(bp, __func__);
324                                 xfs_buf_relse(bp);
325                                 /*
326                                  * We can gracefully recover from most read
327                                  * errors. Ones we can't are those that happen
328                                  * after the transaction's already dirty.
329                                  */
330                                 if (tp->t_flags & XFS_TRANS_DIRTY)
331                                         xfs_force_shutdown(tp->t_mountp,
332                                                         SHUTDOWN_META_IO_ERROR);
333                                 /* bad CRC means corrupted metadata */
334                                 if (error == -EFSBADCRC)
335                                         error = -EFSCORRUPTED;
336                                 return error;
337                         }
338                 }
339                 /*
340                  * We never locked this buf ourselves, so we shouldn't
341                  * brelse it either. Just get out.
342                  */
343                 if (XFS_FORCED_SHUTDOWN(mp)) {
344                         trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
345                         *bpp = NULL;
346                         return -EIO;
347                 }
348
349
350                 bip = bp->b_fspriv;
351                 bip->bli_recur++;
352
353                 ASSERT(atomic_read(&bip->bli_refcount) > 0);
354                 trace_xfs_trans_read_buf_recur(bip);
355                 *bpp = bp;
356                 return 0;
357         }
358
359         bp = xfs_buf_read_map(target, map, nmaps, flags, ops);
360         if (bp == NULL) {
361                 *bpp = NULL;
362                 return (flags & XBF_TRYLOCK) ?
363                                         0 : -ENOMEM;
364         }
365         if (bp->b_error) {
366                 error = bp->b_error;
367                 xfs_buf_stale(bp);
368                 XFS_BUF_DONE(bp);
369                 xfs_buf_ioerror_alert(bp, __func__);
370                 if (tp->t_flags & XFS_TRANS_DIRTY)
371                         xfs_force_shutdown(tp->t_mountp, SHUTDOWN_META_IO_ERROR);
372                 xfs_buf_relse(bp);
373
374                 /* bad CRC means corrupted metadata */
375                 if (error == -EFSBADCRC)
376                         error = -EFSCORRUPTED;
377                 return error;
378         }
379 #ifdef DEBUG
380         if (xfs_do_error && !(tp->t_flags & XFS_TRANS_DIRTY)) {
381                 if (xfs_error_target == target) {
382                         if (((xfs_req_num++) % xfs_error_mod) == 0) {
383                                 xfs_force_shutdown(tp->t_mountp,
384                                                    SHUTDOWN_META_IO_ERROR);
385                                 xfs_buf_relse(bp);
386                                 xfs_debug(mp, "Returning trans error!");
387                                 return -EIO;
388                         }
389                 }
390         }
391 #endif
392         if (XFS_FORCED_SHUTDOWN(mp))
393                 goto shutdown_abort;
394
395         _xfs_trans_bjoin(tp, bp, 1);
396         trace_xfs_trans_read_buf(bp->b_fspriv);
397
398         *bpp = bp;
399         return 0;
400
401 shutdown_abort:
402         trace_xfs_trans_read_buf_shut(bp, _RET_IP_);
403         xfs_buf_relse(bp);
404         *bpp = NULL;
405         return -EIO;
406 }
407
408 /*
409  * Release the buffer bp which was previously acquired with one of the
410  * xfs_trans_... buffer allocation routines if the buffer has not
411  * been modified within this transaction.  If the buffer is modified
412  * within this transaction, do decrement the recursion count but do
413  * not release the buffer even if the count goes to 0.  If the buffer is not
414  * modified within the transaction, decrement the recursion count and
415  * release the buffer if the recursion count goes to 0.
416  *
417  * If the buffer is to be released and it was not modified before
418  * this transaction began, then free the buf_log_item associated with it.
419  *
420  * If the transaction pointer is NULL, make this just a normal
421  * brelse() call.
422  */
423 void
424 xfs_trans_brelse(xfs_trans_t    *tp,
425                  xfs_buf_t      *bp)
426 {
427         xfs_buf_log_item_t      *bip;
428
429         /*
430          * Default to a normal brelse() call if the tp is NULL.
431          */
432         if (tp == NULL) {
433                 ASSERT(bp->b_transp == NULL);
434                 xfs_buf_relse(bp);
435                 return;
436         }
437
438         ASSERT(bp->b_transp == tp);
439         bip = bp->b_fspriv;
440         ASSERT(bip->bli_item.li_type == XFS_LI_BUF);
441         ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
442         ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
443         ASSERT(atomic_read(&bip->bli_refcount) > 0);
444
445         trace_xfs_trans_brelse(bip);
446
447         /*
448          * If the release is just for a recursive lock,
449          * then decrement the count and return.
450          */
451         if (bip->bli_recur > 0) {
452                 bip->bli_recur--;
453                 return;
454         }
455
456         /*
457          * If the buffer is dirty within this transaction, we can't
458          * release it until we commit.
459          */
460         if (bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY)
461                 return;
462
463         /*
464          * If the buffer has been invalidated, then we can't release
465          * it until the transaction commits to disk unless it is re-dirtied
466          * as part of this transaction.  This prevents us from pulling
467          * the item from the AIL before we should.
468          */
469         if (bip->bli_flags & XFS_BLI_STALE)
470                 return;
471
472         ASSERT(!(bip->bli_flags & XFS_BLI_LOGGED));
473
474         /*
475          * Free up the log item descriptor tracking the released item.
476          */
477         xfs_trans_del_item(&bip->bli_item);
478
479         /*
480          * Clear the hold flag in the buf log item if it is set.
481          * We wouldn't want the next user of the buffer to
482          * get confused.
483          */
484         if (bip->bli_flags & XFS_BLI_HOLD) {
485                 bip->bli_flags &= ~XFS_BLI_HOLD;
486         }
487
488         /*
489          * Drop our reference to the buf log item.
490          */
491         atomic_dec(&bip->bli_refcount);
492
493         /*
494          * If the buf item is not tracking data in the log, then
495          * we must free it before releasing the buffer back to the
496          * free pool.  Before releasing the buffer to the free pool,
497          * clear the transaction pointer in b_fsprivate2 to dissolve
498          * its relation to this transaction.
499          */
500         if (!xfs_buf_item_dirty(bip)) {
501 /***
502                 ASSERT(bp->b_pincount == 0);
503 ***/
504                 ASSERT(atomic_read(&bip->bli_refcount) == 0);
505                 ASSERT(!(bip->bli_item.li_flags & XFS_LI_IN_AIL));
506                 ASSERT(!(bip->bli_flags & XFS_BLI_INODE_ALLOC_BUF));
507                 xfs_buf_item_relse(bp);
508         }
509
510         bp->b_transp = NULL;
511         xfs_buf_relse(bp);
512 }
513
514 /*
515  * Mark the buffer as not needing to be unlocked when the buf item's
516  * iop_unlock() routine is called.  The buffer must already be locked
517  * and associated with the given transaction.
518  */
519 /* ARGSUSED */
520 void
521 xfs_trans_bhold(xfs_trans_t     *tp,
522                 xfs_buf_t       *bp)
523 {
524         xfs_buf_log_item_t      *bip = bp->b_fspriv;
525
526         ASSERT(bp->b_transp == tp);
527         ASSERT(bip != NULL);
528         ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
529         ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
530         ASSERT(atomic_read(&bip->bli_refcount) > 0);
531
532         bip->bli_flags |= XFS_BLI_HOLD;
533         trace_xfs_trans_bhold(bip);
534 }
535
536 /*
537  * Cancel the previous buffer hold request made on this buffer
538  * for this transaction.
539  */
540 void
541 xfs_trans_bhold_release(xfs_trans_t     *tp,
542                         xfs_buf_t       *bp)
543 {
544         xfs_buf_log_item_t      *bip = bp->b_fspriv;
545
546         ASSERT(bp->b_transp == tp);
547         ASSERT(bip != NULL);
548         ASSERT(!(bip->bli_flags & XFS_BLI_STALE));
549         ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_CANCEL));
550         ASSERT(atomic_read(&bip->bli_refcount) > 0);
551         ASSERT(bip->bli_flags & XFS_BLI_HOLD);
552
553         bip->bli_flags &= ~XFS_BLI_HOLD;
554         trace_xfs_trans_bhold_release(bip);
555 }
556
557 /*
558  * This is called to mark bytes first through last inclusive of the given
559  * buffer as needing to be logged when the transaction is committed.
560  * The buffer must already be associated with the given transaction.
561  *
562  * First and last are numbers relative to the beginning of this buffer,
563  * so the first byte in the buffer is numbered 0 regardless of the
564  * value of b_blkno.
565  */
566 void
567 xfs_trans_log_buf(xfs_trans_t   *tp,
568                   xfs_buf_t     *bp,
569                   uint          first,
570                   uint          last)
571 {
572         xfs_buf_log_item_t      *bip = bp->b_fspriv;
573
574         ASSERT(bp->b_transp == tp);
575         ASSERT(bip != NULL);
576         ASSERT(first <= last && last < BBTOB(bp->b_length));
577         ASSERT(bp->b_iodone == NULL ||
578                bp->b_iodone == xfs_buf_iodone_callbacks);
579
580         /*
581          * Mark the buffer as needing to be written out eventually,
582          * and set its iodone function to remove the buffer's buf log
583          * item from the AIL and free it when the buffer is flushed
584          * to disk.  See xfs_buf_attach_iodone() for more details
585          * on li_cb and xfs_buf_iodone_callbacks().
586          * If we end up aborting this transaction, we trap this buffer
587          * inside the b_bdstrat callback so that this won't get written to
588          * disk.
589          */
590         XFS_BUF_DONE(bp);
591
592         ASSERT(atomic_read(&bip->bli_refcount) > 0);
593         bp->b_iodone = xfs_buf_iodone_callbacks;
594         bip->bli_item.li_cb = xfs_buf_iodone;
595
596         trace_xfs_trans_log_buf(bip);
597
598         /*
599          * If we invalidated the buffer within this transaction, then
600          * cancel the invalidation now that we're dirtying the buffer
601          * again.  There are no races with the code in xfs_buf_item_unpin(),
602          * because we have a reference to the buffer this entire time.
603          */
604         if (bip->bli_flags & XFS_BLI_STALE) {
605                 bip->bli_flags &= ~XFS_BLI_STALE;
606                 ASSERT(XFS_BUF_ISSTALE(bp));
607                 XFS_BUF_UNSTALE(bp);
608                 bip->__bli_format.blf_flags &= ~XFS_BLF_CANCEL;
609         }
610
611         tp->t_flags |= XFS_TRANS_DIRTY;
612         bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
613
614         /*
615          * If we have an ordered buffer we are not logging any dirty range but
616          * it still needs to be marked dirty and that it has been logged.
617          */
618         bip->bli_flags |= XFS_BLI_DIRTY | XFS_BLI_LOGGED;
619         if (!(bip->bli_flags & XFS_BLI_ORDERED))
620                 xfs_buf_item_log(bip, first, last);
621 }
622
623
624 /*
625  * Invalidate a buffer that is being used within a transaction.
626  *
627  * Typically this is because the blocks in the buffer are being freed, so we
628  * need to prevent it from being written out when we're done.  Allowing it
629  * to be written again might overwrite data in the free blocks if they are
630  * reallocated to a file.
631  *
632  * We prevent the buffer from being written out by marking it stale.  We can't
633  * get rid of the buf log item at this point because the buffer may still be
634  * pinned by another transaction.  If that is the case, then we'll wait until
635  * the buffer is committed to disk for the last time (we can tell by the ref
636  * count) and free it in xfs_buf_item_unpin().  Until that happens we will
637  * keep the buffer locked so that the buffer and buf log item are not reused.
638  *
639  * We also set the XFS_BLF_CANCEL flag in the buf log format structure and log
640  * the buf item.  This will be used at recovery time to determine that copies
641  * of the buffer in the log before this should not be replayed.
642  *
643  * We mark the item descriptor and the transaction dirty so that we'll hold
644  * the buffer until after the commit.
645  *
646  * Since we're invalidating the buffer, we also clear the state about which
647  * parts of the buffer have been logged.  We also clear the flag indicating
648  * that this is an inode buffer since the data in the buffer will no longer
649  * be valid.
650  *
651  * We set the stale bit in the buffer as well since we're getting rid of it.
652  */
653 void
654 xfs_trans_binval(
655         xfs_trans_t     *tp,
656         xfs_buf_t       *bp)
657 {
658         xfs_buf_log_item_t      *bip = bp->b_fspriv;
659         int                     i;
660
661         ASSERT(bp->b_transp == tp);
662         ASSERT(bip != NULL);
663         ASSERT(atomic_read(&bip->bli_refcount) > 0);
664
665         trace_xfs_trans_binval(bip);
666
667         if (bip->bli_flags & XFS_BLI_STALE) {
668                 /*
669                  * If the buffer is already invalidated, then
670                  * just return.
671                  */
672                 ASSERT(XFS_BUF_ISSTALE(bp));
673                 ASSERT(!(bip->bli_flags & (XFS_BLI_LOGGED | XFS_BLI_DIRTY)));
674                 ASSERT(!(bip->__bli_format.blf_flags & XFS_BLF_INODE_BUF));
675                 ASSERT(!(bip->__bli_format.blf_flags & XFS_BLFT_MASK));
676                 ASSERT(bip->__bli_format.blf_flags & XFS_BLF_CANCEL);
677                 ASSERT(bip->bli_item.li_desc->lid_flags & XFS_LID_DIRTY);
678                 ASSERT(tp->t_flags & XFS_TRANS_DIRTY);
679                 return;
680         }
681
682         xfs_buf_stale(bp);
683
684         bip->bli_flags |= XFS_BLI_STALE;
685         bip->bli_flags &= ~(XFS_BLI_INODE_BUF | XFS_BLI_LOGGED | XFS_BLI_DIRTY);
686         bip->__bli_format.blf_flags &= ~XFS_BLF_INODE_BUF;
687         bip->__bli_format.blf_flags |= XFS_BLF_CANCEL;
688         bip->__bli_format.blf_flags &= ~XFS_BLFT_MASK;
689         for (i = 0; i < bip->bli_format_count; i++) {
690                 memset(bip->bli_formats[i].blf_data_map, 0,
691                        (bip->bli_formats[i].blf_map_size * sizeof(uint)));
692         }
693         bip->bli_item.li_desc->lid_flags |= XFS_LID_DIRTY;
694         tp->t_flags |= XFS_TRANS_DIRTY;
695 }
696
697 /*
698  * This call is used to indicate that the buffer contains on-disk inodes which
699  * must be handled specially during recovery.  They require special handling
700  * because only the di_next_unlinked from the inodes in the buffer should be
701  * recovered.  The rest of the data in the buffer is logged via the inodes
702  * themselves.
703  *
704  * All we do is set the XFS_BLI_INODE_BUF flag in the items flags so it can be
705  * transferred to the buffer's log format structure so that we'll know what to
706  * do at recovery time.
707  */
708 void
709 xfs_trans_inode_buf(
710         xfs_trans_t     *tp,
711         xfs_buf_t       *bp)
712 {
713         xfs_buf_log_item_t      *bip = bp->b_fspriv;
714
715         ASSERT(bp->b_transp == tp);
716         ASSERT(bip != NULL);
717         ASSERT(atomic_read(&bip->bli_refcount) > 0);
718
719         bip->bli_flags |= XFS_BLI_INODE_BUF;
720         xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
721 }
722
723 /*
724  * This call is used to indicate that the buffer is going to
725  * be staled and was an inode buffer. This means it gets
726  * special processing during unpin - where any inodes
727  * associated with the buffer should be removed from ail.
728  * There is also special processing during recovery,
729  * any replay of the inodes in the buffer needs to be
730  * prevented as the buffer may have been reused.
731  */
732 void
733 xfs_trans_stale_inode_buf(
734         xfs_trans_t     *tp,
735         xfs_buf_t       *bp)
736 {
737         xfs_buf_log_item_t      *bip = bp->b_fspriv;
738
739         ASSERT(bp->b_transp == tp);
740         ASSERT(bip != NULL);
741         ASSERT(atomic_read(&bip->bli_refcount) > 0);
742
743         bip->bli_flags |= XFS_BLI_STALE_INODE;
744         bip->bli_item.li_cb = xfs_buf_iodone;
745         xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
746 }
747
748 /*
749  * Mark the buffer as being one which contains newly allocated
750  * inodes.  We need to make sure that even if this buffer is
751  * relogged as an 'inode buf' we still recover all of the inode
752  * images in the face of a crash.  This works in coordination with
753  * xfs_buf_item_committed() to ensure that the buffer remains in the
754  * AIL at its original location even after it has been relogged.
755  */
756 /* ARGSUSED */
757 void
758 xfs_trans_inode_alloc_buf(
759         xfs_trans_t     *tp,
760         xfs_buf_t       *bp)
761 {
762         xfs_buf_log_item_t      *bip = bp->b_fspriv;
763
764         ASSERT(bp->b_transp == tp);
765         ASSERT(bip != NULL);
766         ASSERT(atomic_read(&bip->bli_refcount) > 0);
767
768         bip->bli_flags |= XFS_BLI_INODE_ALLOC_BUF;
769         xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DINO_BUF);
770 }
771
772 /*
773  * Mark the buffer as ordered for this transaction. This means
774  * that the contents of the buffer are not recorded in the transaction
775  * but it is tracked in the AIL as though it was. This allows us
776  * to record logical changes in transactions rather than the physical
777  * changes we make to the buffer without changing writeback ordering
778  * constraints of metadata buffers.
779  */
780 void
781 xfs_trans_ordered_buf(
782         struct xfs_trans        *tp,
783         struct xfs_buf          *bp)
784 {
785         struct xfs_buf_log_item *bip = bp->b_fspriv;
786
787         ASSERT(bp->b_transp == tp);
788         ASSERT(bip != NULL);
789         ASSERT(atomic_read(&bip->bli_refcount) > 0);
790
791         bip->bli_flags |= XFS_BLI_ORDERED;
792         trace_xfs_buf_item_ordered(bip);
793 }
794
795 /*
796  * Set the type of the buffer for log recovery so that it can correctly identify
797  * and hence attach the correct buffer ops to the buffer after replay.
798  */
799 void
800 xfs_trans_buf_set_type(
801         struct xfs_trans        *tp,
802         struct xfs_buf          *bp,
803         enum xfs_blft           type)
804 {
805         struct xfs_buf_log_item *bip = bp->b_fspriv;
806
807         if (!tp)
808                 return;
809
810         ASSERT(bp->b_transp == tp);
811         ASSERT(bip != NULL);
812         ASSERT(atomic_read(&bip->bli_refcount) > 0);
813
814         xfs_blft_to_flags(&bip->__bli_format, type);
815 }
816
817 void
818 xfs_trans_buf_copy_type(
819         struct xfs_buf          *dst_bp,
820         struct xfs_buf          *src_bp)
821 {
822         struct xfs_buf_log_item *sbip = src_bp->b_fspriv;
823         struct xfs_buf_log_item *dbip = dst_bp->b_fspriv;
824         enum xfs_blft           type;
825
826         type = xfs_blft_from_flags(&sbip->__bli_format);
827         xfs_blft_to_flags(&dbip->__bli_format, type);
828 }
829
830 /*
831  * Similar to xfs_trans_inode_buf(), this marks the buffer as a cluster of
832  * dquots. However, unlike in inode buffer recovery, dquot buffers get
833  * recovered in their entirety. (Hence, no XFS_BLI_DQUOT_ALLOC_BUF flag).
834  * The only thing that makes dquot buffers different from regular
835  * buffers is that we must not replay dquot bufs when recovering
836  * if a _corresponding_ quotaoff has happened. We also have to distinguish
837  * between usr dquot bufs and grp dquot bufs, because usr and grp quotas
838  * can be turned off independently.
839  */
840 /* ARGSUSED */
841 void
842 xfs_trans_dquot_buf(
843         xfs_trans_t     *tp,
844         xfs_buf_t       *bp,
845         uint            type)
846 {
847         struct xfs_buf_log_item *bip = bp->b_fspriv;
848
849         ASSERT(type == XFS_BLF_UDQUOT_BUF ||
850                type == XFS_BLF_PDQUOT_BUF ||
851                type == XFS_BLF_GDQUOT_BUF);
852
853         bip->__bli_format.blf_flags |= type;
854
855         switch (type) {
856         case XFS_BLF_UDQUOT_BUF:
857                 type = XFS_BLFT_UDQUOT_BUF;
858                 break;
859         case XFS_BLF_PDQUOT_BUF:
860                 type = XFS_BLFT_PDQUOT_BUF;
861                 break;
862         case XFS_BLF_GDQUOT_BUF:
863                 type = XFS_BLFT_GDQUOT_BUF;
864                 break;
865         default:
866                 type = XFS_BLFT_UNKNOWN_BUF;
867                 break;
868         }
869
870         xfs_trans_buf_set_type(tp, bp, type);
871 }