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