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