]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/gnu/fs/xfs/xfs_trans.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / gnu / fs / xfs / xfs_trans.c
1 /*
2  * Copyright (c) 2000-2003,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_dir.h"
28 #include "xfs_dir2.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_error.h"
32 #include "xfs_da_btree.h"
33 #include "xfs_bmap_btree.h"
34 #include "xfs_alloc_btree.h"
35 #include "xfs_ialloc_btree.h"
36 #include "xfs_dir_sf.h"
37 #include "xfs_dir2_sf.h"
38 #include "xfs_attr_sf.h"
39 #include "xfs_dinode.h"
40 #include "xfs_inode.h"
41 #include "xfs_btree.h"
42 #include "xfs_ialloc.h"
43 #include "xfs_alloc.h"
44 #include "xfs_bmap.h"
45 #include "xfs_quota.h"
46 #include "xfs_trans_priv.h"
47 #include "xfs_trans_space.h"
48
49
50 STATIC void     xfs_trans_apply_sb_deltas(xfs_trans_t *);
51 STATIC uint     xfs_trans_count_vecs(xfs_trans_t *);
52 STATIC void     xfs_trans_fill_vecs(xfs_trans_t *, xfs_log_iovec_t *);
53 STATIC void     xfs_trans_uncommit(xfs_trans_t *, uint);
54 STATIC void     xfs_trans_committed(xfs_trans_t *, int);
55 STATIC void     xfs_trans_chunk_committed(xfs_log_item_chunk_t *, xfs_lsn_t, int);
56 STATIC void     xfs_trans_free(xfs_trans_t *);
57
58 kmem_zone_t     *xfs_trans_zone;
59
60
61 /*
62  * Reservation functions here avoid a huge stack in xfs_trans_init
63  * due to register overflow from temporaries in the calculations.
64  */
65
66 STATIC uint
67 xfs_calc_write_reservation(xfs_mount_t *mp)
68 {
69         return XFS_CALC_WRITE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
70 }
71
72 STATIC uint
73 xfs_calc_itruncate_reservation(xfs_mount_t *mp)
74 {
75         return XFS_CALC_ITRUNCATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
76 }
77
78 STATIC uint
79 xfs_calc_rename_reservation(xfs_mount_t *mp)
80 {
81         return XFS_CALC_RENAME_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
82 }
83
84 STATIC uint
85 xfs_calc_link_reservation(xfs_mount_t *mp)
86 {
87         return XFS_CALC_LINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
88 }
89
90 STATIC uint
91 xfs_calc_remove_reservation(xfs_mount_t *mp)
92 {
93         return XFS_CALC_REMOVE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
94 }
95
96 STATIC uint
97 xfs_calc_symlink_reservation(xfs_mount_t *mp)
98 {
99         return XFS_CALC_SYMLINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
100 }
101
102 STATIC uint
103 xfs_calc_create_reservation(xfs_mount_t *mp)
104 {
105         return XFS_CALC_CREATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
106 }
107
108 STATIC uint
109 xfs_calc_mkdir_reservation(xfs_mount_t *mp)
110 {
111         return XFS_CALC_MKDIR_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
112 }
113
114 STATIC uint
115 xfs_calc_ifree_reservation(xfs_mount_t *mp)
116 {
117         return XFS_CALC_IFREE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
118 }
119
120 STATIC uint
121 xfs_calc_ichange_reservation(xfs_mount_t *mp)
122 {
123         return XFS_CALC_ICHANGE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
124 }
125
126 STATIC uint
127 xfs_calc_growdata_reservation(xfs_mount_t *mp)
128 {
129         return XFS_CALC_GROWDATA_LOG_RES(mp);
130 }
131
132 STATIC uint
133 xfs_calc_growrtalloc_reservation(xfs_mount_t *mp)
134 {
135         return XFS_CALC_GROWRTALLOC_LOG_RES(mp);
136 }
137
138 STATIC uint
139 xfs_calc_growrtzero_reservation(xfs_mount_t *mp)
140 {
141         return XFS_CALC_GROWRTZERO_LOG_RES(mp);
142 }
143
144 STATIC uint
145 xfs_calc_growrtfree_reservation(xfs_mount_t *mp)
146 {
147         return XFS_CALC_GROWRTFREE_LOG_RES(mp);
148 }
149
150 STATIC uint
151 xfs_calc_swrite_reservation(xfs_mount_t *mp)
152 {
153         return XFS_CALC_SWRITE_LOG_RES(mp);
154 }
155
156 STATIC uint
157 xfs_calc_writeid_reservation(xfs_mount_t *mp)
158 {
159         return XFS_CALC_WRITEID_LOG_RES(mp);
160 }
161
162 STATIC uint
163 xfs_calc_addafork_reservation(xfs_mount_t *mp)
164 {
165         return XFS_CALC_ADDAFORK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
166 }
167
168 STATIC uint
169 xfs_calc_attrinval_reservation(xfs_mount_t *mp)
170 {
171         return XFS_CALC_ATTRINVAL_LOG_RES(mp);
172 }
173
174 STATIC uint
175 xfs_calc_attrset_reservation(xfs_mount_t *mp)
176 {
177         return XFS_CALC_ATTRSET_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
178 }
179
180 STATIC uint
181 xfs_calc_attrrm_reservation(xfs_mount_t *mp)
182 {
183         return XFS_CALC_ATTRRM_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
184 }
185
186 STATIC uint
187 xfs_calc_clear_agi_bucket_reservation(xfs_mount_t *mp)
188 {
189         return XFS_CALC_CLEAR_AGI_BUCKET_LOG_RES(mp);
190 }
191
192 /*
193  * Initialize the precomputed transaction reservation values
194  * in the mount structure.
195  */
196 void
197 xfs_trans_init(
198         xfs_mount_t     *mp)
199 {
200         xfs_trans_reservations_t        *resp;
201
202         resp = &(mp->m_reservations);
203         resp->tr_write = xfs_calc_write_reservation(mp);
204         resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
205         resp->tr_rename = xfs_calc_rename_reservation(mp);
206         resp->tr_link = xfs_calc_link_reservation(mp);
207         resp->tr_remove = xfs_calc_remove_reservation(mp);
208         resp->tr_symlink = xfs_calc_symlink_reservation(mp);
209         resp->tr_create = xfs_calc_create_reservation(mp);
210         resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
211         resp->tr_ifree = xfs_calc_ifree_reservation(mp);
212         resp->tr_ichange = xfs_calc_ichange_reservation(mp);
213         resp->tr_growdata = xfs_calc_growdata_reservation(mp);
214         resp->tr_swrite = xfs_calc_swrite_reservation(mp);
215         resp->tr_writeid = xfs_calc_writeid_reservation(mp);
216         resp->tr_addafork = xfs_calc_addafork_reservation(mp);
217         resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
218         resp->tr_attrset = xfs_calc_attrset_reservation(mp);
219         resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
220         resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
221         resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
222         resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
223         resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
224 }
225
226 /*
227  * This routine is called to allocate a transaction structure.
228  * The type parameter indicates the type of the transaction.  These
229  * are enumerated in xfs_trans.h.
230  *
231  * Dynamically allocate the transaction structure from the transaction
232  * zone, initialize it, and return it to the caller.
233  */
234 xfs_trans_t *
235 xfs_trans_alloc(
236         xfs_mount_t     *mp,
237         uint            type)
238 {
239 #ifdef RMC
240         fs_check_frozen(XFS_MTOVFS(mp), SB_FREEZE_TRANS);
241 #endif
242         atomic_inc(&mp->m_active_trans);
243
244         return (_xfs_trans_alloc(mp, type));
245
246 }
247
248 xfs_trans_t *
249 _xfs_trans_alloc(
250         xfs_mount_t     *mp,
251         uint            type)
252 {
253         xfs_trans_t     *tp;
254
255         ASSERT(xfs_trans_zone != NULL);
256         tp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
257
258         /*
259          * Initialize the transaction structure.
260          */
261         tp->t_magic = XFS_TRANS_MAGIC;
262         tp->t_type = type;
263         tp->t_mountp = mp;
264         tp->t_items_free = XFS_LIC_NUM_SLOTS;
265         tp->t_busy_free = XFS_LBC_NUM_SLOTS;
266         XFS_LIC_INIT(&(tp->t_items));
267         XFS_LBC_INIT(&(tp->t_busy));
268
269         return (tp);
270 }
271
272 /*
273  * This is called to create a new transaction which will share the
274  * permanent log reservation of the given transaction.  The remaining
275  * unused block and rt extent reservations are also inherited.  This
276  * implies that the original transaction is no longer allowed to allocate
277  * blocks.  Locks and log items, however, are no inherited.  They must
278  * be added to the new transaction explicitly.
279  */
280 xfs_trans_t *
281 xfs_trans_dup(
282         xfs_trans_t     *tp)
283 {
284         xfs_trans_t     *ntp;
285
286         ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
287
288         /*
289          * Initialize the new transaction structure.
290          */
291         ntp->t_magic = XFS_TRANS_MAGIC;
292         ntp->t_type = tp->t_type;
293         ntp->t_mountp = tp->t_mountp;
294         ntp->t_items_free = XFS_LIC_NUM_SLOTS;
295         ntp->t_busy_free = XFS_LBC_NUM_SLOTS;
296         XFS_LIC_INIT(&(ntp->t_items));
297         XFS_LBC_INIT(&(ntp->t_busy));
298
299         ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
300         ASSERT(tp->t_ticket != NULL);
301
302         ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
303         ntp->t_ticket = tp->t_ticket;
304         ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
305         tp->t_blk_res = tp->t_blk_res_used;
306         ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
307         tp->t_rtx_res = tp->t_rtx_res_used;
308         PFLAGS_DUP(&tp->t_pflags, &ntp->t_pflags);
309
310         XFS_TRANS_DUP_DQINFO(tp->t_mountp, tp, ntp);
311
312         atomic_inc(&tp->t_mountp->m_active_trans);
313         return ntp;
314 }
315
316 /*
317  * This is called to reserve free disk blocks and log space for the
318  * given transaction.  This must be done before allocating any resources
319  * within the transaction.
320  *
321  * This will return ENOSPC if there are not enough blocks available.
322  * It will sleep waiting for available log space.
323  * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
324  * is used by long running transactions.  If any one of the reservations
325  * fails then they will all be backed out.
326  *
327  * This does not do quota reservations. That typically is done by the
328  * caller afterwards.
329  */
330 int
331 xfs_trans_reserve(
332         xfs_trans_t     *tp,
333         uint            blocks,
334         uint            logspace,
335         uint            rtextents,
336         uint            flags,
337         uint            logcount)
338 {
339         int             log_flags;
340         int             error;
341         int     rsvd;
342
343         error = 0;
344         rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
345
346         /* Mark this thread as being in a transaction */
347         PFLAGS_SET_FSTRANS(&tp->t_pflags);
348
349
350         /*
351          * Attempt to reserve the needed disk blocks by decrementing
352          * the number needed from the number available.  This will
353          * fail if the count would go below zero.
354          */
355         if (blocks > 0) {
356                 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
357                                           -blocks, rsvd);
358                 if (error != 0) {
359                         PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
360                         return (XFS_ERROR(ENOSPC));
361                 }
362                 tp->t_blk_res += blocks;
363         }
364
365         /*
366          * Reserve the log space needed for this transaction.
367          */
368         if (logspace > 0) {
369                 ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace));
370                 ASSERT((tp->t_log_count == 0) ||
371                         (tp->t_log_count == logcount));
372                 if (flags & XFS_TRANS_PERM_LOG_RES) {
373                         log_flags = XFS_LOG_PERM_RESERV;
374                         tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
375                 } else {
376                         ASSERT(tp->t_ticket == NULL);
377                         ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
378                         log_flags = 0;
379                 }
380
381                 error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
382                                         &tp->t_ticket,
383                                         XFS_TRANSACTION, log_flags, tp->t_type);
384                 if (error) {
385                         goto undo_blocks;
386                 }
387                 tp->t_log_res = logspace;
388                 tp->t_log_count = logcount;
389         }
390
391         /*
392          * Attempt to reserve the needed realtime extents by decrementing
393          * the number needed from the number available.  This will
394          * fail if the count would go below zero.
395          */
396         if (rtextents > 0) {
397                 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
398                                           -rtextents, rsvd);
399                 if (error) {
400                         error = XFS_ERROR(ENOSPC);
401                         goto undo_log;
402                 }
403                 tp->t_rtx_res += rtextents;
404         }
405
406         return 0;
407
408         /*
409          * Error cases jump to one of these labels to undo any
410          * reservations which have already been performed.
411          */
412 undo_log:
413         if (logspace > 0) {
414                 if (flags & XFS_TRANS_PERM_LOG_RES) {
415                         log_flags = XFS_LOG_REL_PERM_RESERV;
416                 } else {
417                         log_flags = 0;
418                 }
419                 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
420                 tp->t_ticket = NULL;
421                 tp->t_log_res = 0;
422                 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
423         }
424
425 undo_blocks:
426         if (blocks > 0) {
427                 (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
428                                          blocks, rsvd);
429                 tp->t_blk_res = 0;
430         }
431
432         PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
433
434         return (error);
435 }
436
437
438 /*
439  * Record the indicated change to the given field for application
440  * to the file system's superblock when the transaction commits.
441  * For now, just store the change in the transaction structure.
442  *
443  * Mark the transaction structure to indicate that the superblock
444  * needs to be updated before committing.
445  */
446 void
447 xfs_trans_mod_sb(
448         xfs_trans_t     *tp,
449         uint            field,
450         long            delta)
451 {
452
453         switch (field) {
454         case XFS_TRANS_SB_ICOUNT:
455                 tp->t_icount_delta += delta;
456                 break;
457         case XFS_TRANS_SB_IFREE:
458                 tp->t_ifree_delta += delta;
459                 break;
460         case XFS_TRANS_SB_FDBLOCKS:
461                 /*
462                  * Track the number of blocks allocated in the
463                  * transaction.  Make sure it does not exceed the
464                  * number reserved.
465                  */
466                 if (delta < 0) {
467                         tp->t_blk_res_used += (uint)-delta;
468                         ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
469                 }
470                 tp->t_fdblocks_delta += delta;
471                 break;
472         case XFS_TRANS_SB_RES_FDBLOCKS:
473                 /*
474                  * The allocation has already been applied to the
475                  * in-core superblock's counter.  This should only
476                  * be applied to the on-disk superblock.
477                  */
478                 ASSERT(delta < 0);
479                 tp->t_res_fdblocks_delta += delta;
480                 break;
481         case XFS_TRANS_SB_FREXTENTS:
482                 /*
483                  * Track the number of blocks allocated in the
484                  * transaction.  Make sure it does not exceed the
485                  * number reserved.
486                  */
487                 if (delta < 0) {
488                         tp->t_rtx_res_used += (uint)-delta;
489                         ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
490                 }
491                 tp->t_frextents_delta += delta;
492                 break;
493         case XFS_TRANS_SB_RES_FREXTENTS:
494                 /*
495                  * The allocation has already been applied to the
496                  * in-core superblock's counter.  This should only
497                  * be applied to the on-disk superblock.
498                  */
499                 ASSERT(delta < 0);
500                 tp->t_res_frextents_delta += delta;
501                 break;
502         case XFS_TRANS_SB_DBLOCKS:
503                 ASSERT(delta > 0);
504                 tp->t_dblocks_delta += delta;
505                 break;
506         case XFS_TRANS_SB_AGCOUNT:
507                 ASSERT(delta > 0);
508                 tp->t_agcount_delta += delta;
509                 break;
510         case XFS_TRANS_SB_IMAXPCT:
511                 tp->t_imaxpct_delta += delta;
512                 break;
513         case XFS_TRANS_SB_REXTSIZE:
514                 tp->t_rextsize_delta += delta;
515                 break;
516         case XFS_TRANS_SB_RBMBLOCKS:
517                 tp->t_rbmblocks_delta += delta;
518                 break;
519         case XFS_TRANS_SB_RBLOCKS:
520                 tp->t_rblocks_delta += delta;
521                 break;
522         case XFS_TRANS_SB_REXTENTS:
523                 tp->t_rextents_delta += delta;
524                 break;
525         case XFS_TRANS_SB_REXTSLOG:
526                 tp->t_rextslog_delta += delta;
527                 break;
528         default:
529                 ASSERT(0);
530                 return;
531         }
532
533         tp->t_flags |= (XFS_TRANS_SB_DIRTY | XFS_TRANS_DIRTY);
534 }
535
536 /*
537  * xfs_trans_apply_sb_deltas() is called from the commit code
538  * to bring the superblock buffer into the current transaction
539  * and modify it as requested by earlier calls to xfs_trans_mod_sb().
540  *
541  * For now we just look at each field allowed to change and change
542  * it if necessary.
543  */
544 STATIC void
545 xfs_trans_apply_sb_deltas(
546         xfs_trans_t     *tp)
547 {
548         xfs_sb_t        *sbp;
549         xfs_buf_t       *bp;
550         int             whole = 0;
551
552         bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
553         sbp = XFS_BUF_TO_SBP(bp);
554
555         /*
556          * Check that superblock mods match the mods made to AGF counters.
557          */
558         ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
559                (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
560                 tp->t_ag_btree_delta));
561
562         if (tp->t_icount_delta != 0) {
563                 INT_MOD(sbp->sb_icount, ARCH_CONVERT, tp->t_icount_delta);
564         }
565         if (tp->t_ifree_delta != 0) {
566                 INT_MOD(sbp->sb_ifree, ARCH_CONVERT, tp->t_ifree_delta);
567         }
568
569         if (tp->t_fdblocks_delta != 0) {
570                 INT_MOD(sbp->sb_fdblocks, ARCH_CONVERT, tp->t_fdblocks_delta);
571         }
572         if (tp->t_res_fdblocks_delta != 0) {
573                 INT_MOD(sbp->sb_fdblocks, ARCH_CONVERT, tp->t_res_fdblocks_delta);
574         }
575
576         if (tp->t_frextents_delta != 0) {
577                 INT_MOD(sbp->sb_frextents, ARCH_CONVERT, tp->t_frextents_delta);
578         }
579         if (tp->t_res_frextents_delta != 0) {
580                 INT_MOD(sbp->sb_frextents, ARCH_CONVERT, tp->t_res_frextents_delta);
581         }
582         if (tp->t_dblocks_delta != 0) {
583                 INT_MOD(sbp->sb_dblocks, ARCH_CONVERT, tp->t_dblocks_delta);
584                 whole = 1;
585         }
586         if (tp->t_agcount_delta != 0) {
587                 INT_MOD(sbp->sb_agcount, ARCH_CONVERT, tp->t_agcount_delta);
588                 whole = 1;
589         }
590         if (tp->t_imaxpct_delta != 0) {
591                 INT_MOD(sbp->sb_imax_pct, ARCH_CONVERT, tp->t_imaxpct_delta);
592                 whole = 1;
593         }
594         if (tp->t_rextsize_delta != 0) {
595                 INT_MOD(sbp->sb_rextsize, ARCH_CONVERT, tp->t_rextsize_delta);
596                 whole = 1;
597         }
598         if (tp->t_rbmblocks_delta != 0) {
599                 INT_MOD(sbp->sb_rbmblocks, ARCH_CONVERT, tp->t_rbmblocks_delta);
600                 whole = 1;
601         }
602         if (tp->t_rblocks_delta != 0) {
603                 INT_MOD(sbp->sb_rblocks, ARCH_CONVERT, tp->t_rblocks_delta);
604                 whole = 1;
605         }
606         if (tp->t_rextents_delta != 0) {
607                 INT_MOD(sbp->sb_rextents, ARCH_CONVERT, tp->t_rextents_delta);
608                 whole = 1;
609         }
610         if (tp->t_rextslog_delta != 0) {
611                 INT_MOD(sbp->sb_rextslog, ARCH_CONVERT, tp->t_rextslog_delta);
612                 whole = 1;
613         }
614
615         if (whole)
616                 /*
617                  * Log the whole thing, the fields are noncontiguous.
618                  */
619                 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_sb_t) - 1);
620         else
621                 /*
622                  * Since all the modifiable fields are contiguous, we
623                  * can get away with this.
624                  */
625                 xfs_trans_log_buf(tp, bp, offsetof(xfs_sb_t, sb_icount),
626                                   offsetof(xfs_sb_t, sb_frextents) +
627                                   sizeof(sbp->sb_frextents) - 1);
628
629 #ifdef XXXKAN
630         XFS_MTOVFS(tp->t_mountp)->vfs_super->s_dirt = 1;
631 #endif
632 }
633
634 /*
635  * xfs_trans_unreserve_and_mod_sb() is called to release unused
636  * reservations and apply superblock counter changes to the in-core
637  * superblock.
638  *
639  * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
640  */
641 STATIC void
642 xfs_trans_unreserve_and_mod_sb(
643         xfs_trans_t     *tp)
644 {
645         xfs_mod_sb_t    msb[14];        /* If you add cases, add entries */
646         xfs_mod_sb_t    *msbp;
647         /* REFERENCED */
648         int             error;
649         int             rsvd;
650
651         msbp = msb;
652         rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
653
654         /*
655          * Release any reserved blocks.  Any that were allocated
656          * will be taken back again by fdblocks_delta below.
657          */
658         if (tp->t_blk_res > 0) {
659                 msbp->msb_field = XFS_SBS_FDBLOCKS;
660                 msbp->msb_delta = tp->t_blk_res;
661                 msbp++;
662         }
663
664         /*
665          * Release any reserved real time extents .  Any that were
666          * allocated will be taken back again by frextents_delta below.
667          */
668         if (tp->t_rtx_res > 0) {
669                 msbp->msb_field = XFS_SBS_FREXTENTS;
670                 msbp->msb_delta = tp->t_rtx_res;
671                 msbp++;
672         }
673
674         /*
675          * Apply any superblock modifications to the in-core version.
676          * The t_res_fdblocks_delta and t_res_frextents_delta fields are
677          * explicitly NOT applied to the in-core superblock.
678          * The idea is that that has already been done.
679          */
680         if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
681                 if (tp->t_icount_delta != 0) {
682                         msbp->msb_field = XFS_SBS_ICOUNT;
683                         msbp->msb_delta = (int)tp->t_icount_delta;
684                         msbp++;
685                 }
686                 if (tp->t_ifree_delta != 0) {
687                         msbp->msb_field = XFS_SBS_IFREE;
688                         msbp->msb_delta = (int)tp->t_ifree_delta;
689                         msbp++;
690                 }
691                 if (tp->t_fdblocks_delta != 0) {
692                         msbp->msb_field = XFS_SBS_FDBLOCKS;
693                         msbp->msb_delta = (int)tp->t_fdblocks_delta;
694                         msbp++;
695                 }
696                 if (tp->t_frextents_delta != 0) {
697                         msbp->msb_field = XFS_SBS_FREXTENTS;
698                         msbp->msb_delta = (int)tp->t_frextents_delta;
699                         msbp++;
700                 }
701                 if (tp->t_dblocks_delta != 0) {
702                         msbp->msb_field = XFS_SBS_DBLOCKS;
703                         msbp->msb_delta = (int)tp->t_dblocks_delta;
704                         msbp++;
705                 }
706                 if (tp->t_agcount_delta != 0) {
707                         msbp->msb_field = XFS_SBS_AGCOUNT;
708                         msbp->msb_delta = (int)tp->t_agcount_delta;
709                         msbp++;
710                 }
711                 if (tp->t_imaxpct_delta != 0) {
712                         msbp->msb_field = XFS_SBS_IMAX_PCT;
713                         msbp->msb_delta = (int)tp->t_imaxpct_delta;
714                         msbp++;
715                 }
716                 if (tp->t_rextsize_delta != 0) {
717                         msbp->msb_field = XFS_SBS_REXTSIZE;
718                         msbp->msb_delta = (int)tp->t_rextsize_delta;
719                         msbp++;
720                 }
721                 if (tp->t_rbmblocks_delta != 0) {
722                         msbp->msb_field = XFS_SBS_RBMBLOCKS;
723                         msbp->msb_delta = (int)tp->t_rbmblocks_delta;
724                         msbp++;
725                 }
726                 if (tp->t_rblocks_delta != 0) {
727                         msbp->msb_field = XFS_SBS_RBLOCKS;
728                         msbp->msb_delta = (int)tp->t_rblocks_delta;
729                         msbp++;
730                 }
731                 if (tp->t_rextents_delta != 0) {
732                         msbp->msb_field = XFS_SBS_REXTENTS;
733                         msbp->msb_delta = (int)tp->t_rextents_delta;
734                         msbp++;
735                 }
736                 if (tp->t_rextslog_delta != 0) {
737                         msbp->msb_field = XFS_SBS_REXTSLOG;
738                         msbp->msb_delta = (int)tp->t_rextslog_delta;
739                         msbp++;
740                 }
741         }
742
743         /*
744          * If we need to change anything, do it.
745          */
746         if (msbp > msb) {
747                 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
748                         (uint)(msbp - msb), rsvd);
749                 ASSERT(error == 0);
750         }
751 }
752
753
754 /*
755  * xfs_trans_commit
756  *
757  * Commit the given transaction to the log a/synchronously.
758  *
759  * XFS disk error handling mechanism is not based on a typical
760  * transaction abort mechanism. Logically after the filesystem
761  * gets marked 'SHUTDOWN', we can't let any new transactions
762  * be durable - ie. committed to disk - because some metadata might
763  * be inconsistent. In such cases, this returns an error, and the
764  * caller may assume that all locked objects joined to the transaction
765  * have already been unlocked as if the commit had succeeded.
766  * Do not reference the transaction structure after this call.
767  */
768  /*ARGSUSED*/
769 int
770 _xfs_trans_commit(
771         xfs_trans_t     *tp,
772         uint            flags,
773         xfs_lsn_t       *commit_lsn_p,
774         int             *log_flushed)
775 {
776         xfs_log_iovec_t         *log_vector;
777         int                     nvec;
778         xfs_mount_t             *mp;
779         xfs_lsn_t               commit_lsn;
780         /* REFERENCED */
781         int                     error;
782         int                     log_flags;
783         int                     sync;
784 #define XFS_TRANS_LOGVEC_COUNT  16
785         xfs_log_iovec_t         log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
786         void                    *commit_iclog;
787         int                     shutdown;
788
789         commit_lsn = -1;
790
791         /*
792          * Determine whether this commit is releasing a permanent
793          * log reservation or not.
794          */
795         if (flags & XFS_TRANS_RELEASE_LOG_RES) {
796                 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
797                 log_flags = XFS_LOG_REL_PERM_RESERV;
798         } else {
799                 log_flags = 0;
800         }
801         mp = tp->t_mountp;
802
803         /*
804          * If there is nothing to be logged by the transaction,
805          * then unlock all of the items associated with the
806          * transaction and free the transaction structure.
807          * Also make sure to return any reserved blocks to
808          * the free pool.
809          */
810 shut_us_down:
811         shutdown = XFS_FORCED_SHUTDOWN(mp) ? EIO : 0;
812         if (!(tp->t_flags & XFS_TRANS_DIRTY) || shutdown) {
813                 xfs_trans_unreserve_and_mod_sb(tp);
814                 /*
815                  * It is indeed possible for the transaction to be
816                  * not dirty but the dqinfo portion to be. All that
817                  * means is that we have some (non-persistent) quota
818                  * reservations that need to be unreserved.
819                  */
820                 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(mp, tp);
821                 if (tp->t_ticket) {
822                         commit_lsn = xfs_log_done(mp, tp->t_ticket,
823                                                         NULL, log_flags);
824                         if (commit_lsn == -1 && !shutdown)
825                                 shutdown = XFS_ERROR(EIO);
826                 }
827                 PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
828                 xfs_trans_free_items(tp, shutdown? XFS_TRANS_ABORT : 0);
829                 xfs_trans_free_busy(tp);
830                 xfs_trans_free(tp);
831                 XFS_STATS_INC(xs_trans_empty);
832                 if (commit_lsn_p)
833                         *commit_lsn_p = commit_lsn;
834                 return (shutdown);
835         }
836         ASSERT(tp->t_ticket != NULL);
837
838         /*
839          * If we need to update the superblock, then do it now.
840          */
841         if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
842                 xfs_trans_apply_sb_deltas(tp);
843         }
844         XFS_TRANS_APPLY_DQUOT_DELTAS(mp, tp);
845
846         /*
847          * Ask each log item how many log_vector entries it will
848          * need so we can figure out how many to allocate.
849          * Try to avoid the kmem_alloc() call in the common case
850          * by using a vector from the stack when it fits.
851          */
852         nvec = xfs_trans_count_vecs(tp);
853         if (nvec == 0) {
854                 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR);
855                 goto shut_us_down;
856         } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
857                 log_vector = log_vector_fast;
858         } else {
859                 log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
860                                                    sizeof(xfs_log_iovec_t),
861                                                    KM_SLEEP);
862         }
863
864         /*
865          * Fill in the log_vector and pin the logged items, and
866          * then write the transaction to the log.
867          */
868         xfs_trans_fill_vecs(tp, log_vector);
869
870         error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn));
871
872         /*
873          * The transaction is committed incore here, and can go out to disk
874          * at any time after this call.  However, all the items associated
875          * with the transaction are still locked and pinned in memory.
876          */
877         commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
878
879         tp->t_commit_lsn = commit_lsn;
880         if (nvec > XFS_TRANS_LOGVEC_COUNT) {
881                 kmem_free(log_vector, nvec * sizeof(xfs_log_iovec_t));
882         }
883
884         if (commit_lsn_p)
885                 *commit_lsn_p = commit_lsn;
886
887         /*
888          * If we got a log write error. Unpin the logitems that we
889          * had pinned, clean up, free trans structure, and return error.
890          */
891         if (error || commit_lsn == -1) {
892                 PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
893                 xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
894                 return XFS_ERROR(EIO);
895         }
896
897         /*
898          * Once the transaction has committed, unused
899          * reservations need to be released and changes to
900          * the superblock need to be reflected in the in-core
901          * version.  Do that now.
902          */
903         xfs_trans_unreserve_and_mod_sb(tp);
904
905         sync = tp->t_flags & XFS_TRANS_SYNC;
906
907         /*
908          * Tell the LM to call the transaction completion routine
909          * when the log write with LSN commit_lsn completes (e.g.
910          * when the transaction commit really hits the on-disk log).
911          * After this call we cannot reference tp, because the call
912          * can happen at any time and the call will free the transaction
913          * structure pointed to by tp.  The only case where we call
914          * the completion routine (xfs_trans_committed) directly is
915          * if the log is turned off on a debug kernel or we're
916          * running in simulation mode (the log is explicitly turned
917          * off).
918          */
919         tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed;
920         tp->t_logcb.cb_arg = tp;
921
922         /*
923          * We need to pass the iclog buffer which was used for the
924          * transaction commit record into this function, and attach
925          * the callback to it. The callback must be attached before
926          * the items are unlocked to avoid racing with other threads
927          * waiting for an item to unlock.
928          */
929         shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
930
931         /*
932          * Mark this thread as no longer being in a transaction
933          */
934         PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
935
936         /*
937          * Once all the items of the transaction have been copied
938          * to the in core log and the callback is attached, the
939          * items can be unlocked.
940          *
941          * This will free descriptors pointing to items which were
942          * not logged since there is nothing more to do with them.
943          * For items which were logged, we will keep pointers to them
944          * so they can be unpinned after the transaction commits to disk.
945          * This will also stamp each modified meta-data item with
946          * the commit lsn of this transaction for dependency tracking
947          * purposes.
948          */
949         xfs_trans_unlock_items(tp, commit_lsn);
950
951         /*
952          * If we detected a log error earlier, finish committing
953          * the transaction now (unpin log items, etc).
954          *
955          * Order is critical here, to avoid using the transaction
956          * pointer after its been freed (by xfs_trans_committed
957          * either here now, or as a callback).  We cannot do this
958          * step inside xfs_log_notify as was done earlier because
959          * of this issue.
960          */
961         if (shutdown)
962                 xfs_trans_committed(tp, XFS_LI_ABORTED);
963
964         /*
965          * Now that the xfs_trans_committed callback has been attached,
966          * and the items are released we can finally allow the iclog to
967          * go to disk.
968          */
969         error = xfs_log_release_iclog(mp, commit_iclog);
970
971         /*
972          * If the transaction needs to be synchronous, then force the
973          * log out now and wait for it.
974          */
975         if (sync) {
976                 if (!error) {
977                         error = _xfs_log_force(mp, commit_lsn,
978                                       XFS_LOG_FORCE | XFS_LOG_SYNC,
979                                       log_flushed);
980                 }
981                 XFS_STATS_INC(xs_trans_sync);
982         } else {
983                 XFS_STATS_INC(xs_trans_async);
984         }
985
986         return (error);
987 }
988
989
990 /*
991  * Total up the number of log iovecs needed to commit this
992  * transaction.  The transaction itself needs one for the
993  * transaction header.  Ask each dirty item in turn how many
994  * it needs to get the total.
995  */
996 STATIC uint
997 xfs_trans_count_vecs(
998         xfs_trans_t     *tp)
999 {
1000         int                     nvecs;
1001         xfs_log_item_desc_t     *lidp;
1002
1003         nvecs = 1;
1004         lidp = xfs_trans_first_item(tp);
1005         ASSERT(lidp != NULL);
1006
1007         /* In the non-debug case we need to start bailing out if we
1008          * didn't find a log_item here, return zero and let trans_commit
1009          * deal with it.
1010          */
1011         if (lidp == NULL)
1012                 return 0;
1013
1014         while (lidp != NULL) {
1015                 /*
1016                  * Skip items which aren't dirty in this transaction.
1017                  */
1018                 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
1019                         lidp = xfs_trans_next_item(tp, lidp);
1020                         continue;
1021                 }
1022                 lidp->lid_size = IOP_SIZE(lidp->lid_item);
1023                 nvecs += lidp->lid_size;
1024                 lidp = xfs_trans_next_item(tp, lidp);
1025         }
1026
1027         return nvecs;
1028 }
1029
1030 /*
1031  * Called from the trans_commit code when we notice that
1032  * the filesystem is in the middle of a forced shutdown.
1033  */
1034 STATIC void
1035 xfs_trans_uncommit(
1036         xfs_trans_t     *tp,
1037         uint            flags)
1038 {
1039         xfs_log_item_desc_t     *lidp;
1040
1041         for (lidp = xfs_trans_first_item(tp);
1042              lidp != NULL;
1043              lidp = xfs_trans_next_item(tp, lidp)) {
1044                 /*
1045                  * Unpin all but those that aren't dirty.
1046                  */
1047                 if (lidp->lid_flags & XFS_LID_DIRTY)
1048                         IOP_UNPIN_REMOVE(lidp->lid_item, tp);
1049         }
1050
1051         xfs_trans_unreserve_and_mod_sb(tp);
1052         XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(tp->t_mountp, tp);
1053
1054         xfs_trans_free_items(tp, flags);
1055         xfs_trans_free_busy(tp);
1056         xfs_trans_free(tp);
1057 }
1058
1059 /*
1060  * Fill in the vector with pointers to data to be logged
1061  * by this transaction.  The transaction header takes
1062  * the first vector, and then each dirty item takes the
1063  * number of vectors it indicated it needed in xfs_trans_count_vecs().
1064  *
1065  * As each item fills in the entries it needs, also pin the item
1066  * so that it cannot be flushed out until the log write completes.
1067  */
1068 STATIC void
1069 xfs_trans_fill_vecs(
1070         xfs_trans_t             *tp,
1071         xfs_log_iovec_t         *log_vector)
1072 {
1073         xfs_log_item_desc_t     *lidp;
1074         xfs_log_iovec_t         *vecp;
1075         uint                    nitems;
1076
1077         /*
1078          * Skip over the entry for the transaction header, we'll
1079          * fill that in at the end.
1080          */
1081         vecp = log_vector + 1;          /* pointer arithmetic */
1082
1083         nitems = 0;
1084         lidp = xfs_trans_first_item(tp);
1085         ASSERT(lidp != NULL);
1086         while (lidp != NULL) {
1087                 /*
1088                  * Skip items which aren't dirty in this transaction.
1089                  */
1090                 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
1091                         lidp = xfs_trans_next_item(tp, lidp);
1092                         continue;
1093                 }
1094                 /*
1095                  * The item may be marked dirty but not log anything.
1096                  * This can be used to get called when a transaction
1097                  * is committed.
1098                  */
1099                 if (lidp->lid_size) {
1100                         nitems++;
1101                 }
1102                 IOP_FORMAT(lidp->lid_item, vecp);
1103                 vecp += lidp->lid_size;         /* pointer arithmetic */
1104                 IOP_PIN(lidp->lid_item);
1105                 lidp = xfs_trans_next_item(tp, lidp);
1106         }
1107
1108         /*
1109          * Now that we've counted the number of items in this
1110          * transaction, fill in the transaction header.
1111          */
1112         tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
1113         tp->t_header.th_type = tp->t_type;
1114         tp->t_header.th_num_items = nitems;
1115         log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
1116         log_vector->i_len = sizeof(xfs_trans_header_t);
1117         XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_TRANSHDR);
1118 }
1119
1120
1121 /*
1122  * Unlock all of the transaction's items and free the transaction.
1123  * The transaction must not have modified any of its items, because
1124  * there is no way to restore them to their previous state.
1125  *
1126  * If the transaction has made a log reservation, make sure to release
1127  * it as well.
1128  */
1129 void
1130 xfs_trans_cancel(
1131         xfs_trans_t             *tp,
1132         int                     flags)
1133 {
1134         int                     log_flags;
1135 #ifdef DEBUG
1136         xfs_log_item_chunk_t    *licp;
1137         xfs_log_item_desc_t     *lidp;
1138         xfs_log_item_t          *lip;
1139         int                     i;
1140 #endif
1141         xfs_mount_t             *mp = tp->t_mountp;
1142
1143         /*
1144          * See if the caller is being too lazy to figure out if
1145          * the transaction really needs an abort.
1146          */
1147         if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
1148                 flags &= ~XFS_TRANS_ABORT;
1149         /*
1150          * See if the caller is relying on us to shut down the
1151          * filesystem.  This happens in paths where we detect
1152          * corruption and decide to give up.
1153          */
1154         if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
1155                 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
1156                 xfs_force_shutdown(mp, XFS_CORRUPT_INCORE);
1157         }
1158 #ifdef DEBUG
1159         if (!(flags & XFS_TRANS_ABORT)) {
1160                 licp = &(tp->t_items);
1161                 while (licp != NULL) {
1162                         lidp = licp->lic_descs;
1163                         for (i = 0; i < licp->lic_unused; i++, lidp++) {
1164                                 if (XFS_LIC_ISFREE(licp, i)) {
1165                                         continue;
1166                                 }
1167
1168                                 lip = lidp->lid_item;
1169                                 if (!XFS_FORCED_SHUTDOWN(mp))
1170                                         ASSERT(!(lip->li_type == XFS_LI_EFD));
1171                         }
1172                         licp = licp->lic_next;
1173                 }
1174         }
1175 #endif
1176         xfs_trans_unreserve_and_mod_sb(tp);
1177         XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(mp, tp);
1178
1179         if (tp->t_ticket) {
1180                 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1181                         ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1182                         log_flags = XFS_LOG_REL_PERM_RESERV;
1183                 } else {
1184                         log_flags = 0;
1185                 }
1186                 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1187         }
1188
1189         /* mark this thread as no longer being in a transaction */
1190         PFLAGS_RESTORE_FSTRANS(&tp->t_pflags);
1191
1192         xfs_trans_free_items(tp, flags);
1193         xfs_trans_free_busy(tp);
1194         xfs_trans_free(tp);
1195 }
1196
1197
1198 /*
1199  * Free the transaction structure.  If there is more clean up
1200  * to do when the structure is freed, add it here.
1201  */
1202 STATIC void
1203 xfs_trans_free(
1204         xfs_trans_t     *tp)
1205 {
1206         atomic_dec(&tp->t_mountp->m_active_trans);
1207         XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp);
1208         kmem_zone_free(xfs_trans_zone, tp);
1209 }
1210
1211
1212 /*
1213  * THIS SHOULD BE REWRITTEN TO USE xfs_trans_next_item().
1214  *
1215  * This is typically called by the LM when a transaction has been fully
1216  * committed to disk.  It needs to unpin the items which have
1217  * been logged by the transaction and update their positions
1218  * in the AIL if necessary.
1219  * This also gets called when the transactions didn't get written out
1220  * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
1221  *
1222  * Call xfs_trans_chunk_committed() to process the items in
1223  * each chunk.
1224  */
1225 STATIC void
1226 xfs_trans_committed(
1227         xfs_trans_t     *tp,
1228         int             abortflag)
1229 {
1230         xfs_log_item_chunk_t    *licp;
1231         xfs_log_item_chunk_t    *next_licp;
1232         xfs_log_busy_chunk_t    *lbcp;
1233         xfs_log_busy_slot_t     *lbsp;
1234         int                     i;
1235
1236         /*
1237          * Call the transaction's completion callback if there
1238          * is one.
1239          */
1240         if (tp->t_callback != NULL) {
1241                 tp->t_callback(tp, tp->t_callarg);
1242         }
1243
1244         /*
1245          * Special case the chunk embedded in the transaction.
1246          */
1247         licp = &(tp->t_items);
1248         if (!(XFS_LIC_ARE_ALL_FREE(licp))) {
1249                 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
1250         }
1251
1252         /*
1253          * Process the items in each chunk in turn.
1254          */
1255         licp = licp->lic_next;
1256         while (licp != NULL) {
1257                 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp));
1258                 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
1259                 next_licp = licp->lic_next;
1260                 kmem_free(licp, sizeof(xfs_log_item_chunk_t));
1261                 licp = next_licp;
1262         }
1263
1264         /*
1265          * Clear all the per-AG busy list items listed in this transaction
1266          */
1267         lbcp = &tp->t_busy;
1268         while (lbcp != NULL) {
1269                 for (i = 0, lbsp = lbcp->lbc_busy; i < lbcp->lbc_unused; i++, lbsp++) {
1270                         if (!XFS_LBC_ISFREE(lbcp, i)) {
1271                                 xfs_alloc_clear_busy(tp, lbsp->lbc_ag,
1272                                                      lbsp->lbc_idx);
1273                         }
1274                 }
1275                 lbcp = lbcp->lbc_next;
1276         }
1277         xfs_trans_free_busy(tp);
1278
1279         /*
1280          * That's it for the transaction structure.  Free it.
1281          */
1282         xfs_trans_free(tp);
1283 }
1284
1285 /*
1286  * This is called to perform the commit processing for each
1287  * item described by the given chunk.
1288  *
1289  * The commit processing consists of unlocking items which were
1290  * held locked with the SYNC_UNLOCK attribute, calling the committed
1291  * routine of each logged item, updating the item's position in the AIL
1292  * if necessary, and unpinning each item.  If the committed routine
1293  * returns -1, then do nothing further with the item because it
1294  * may have been freed.
1295  *
1296  * Since items are unlocked when they are copied to the incore
1297  * log, it is possible for two transactions to be completing
1298  * and manipulating the same item simultaneously.  The AIL lock
1299  * will protect the lsn field of each item.  The value of this
1300  * field can never go backwards.
1301  *
1302  * We unpin the items after repositioning them in the AIL, because
1303  * otherwise they could be immediately flushed and we'd have to race
1304  * with the flusher trying to pull the item from the AIL as we add it.
1305  */
1306 STATIC void
1307 xfs_trans_chunk_committed(
1308         xfs_log_item_chunk_t    *licp,
1309         xfs_lsn_t               lsn,
1310         int                     aborted)
1311 {
1312         xfs_log_item_desc_t     *lidp;
1313         xfs_log_item_t          *lip;
1314         xfs_lsn_t               item_lsn;
1315         struct xfs_mount        *mp;
1316         int                     i;
1317         SPLDECL(s);
1318
1319         lidp = licp->lic_descs;
1320         for (i = 0; i < licp->lic_unused; i++, lidp++) {
1321                 if (XFS_LIC_ISFREE(licp, i)) {
1322                         continue;
1323                 }
1324
1325                 lip = lidp->lid_item;
1326                 if (aborted)
1327                         lip->li_flags |= XFS_LI_ABORTED;
1328
1329                 /*
1330                  * Send in the ABORTED flag to the COMMITTED routine
1331                  * so that it knows whether the transaction was aborted
1332                  * or not.
1333                  */
1334                 item_lsn = IOP_COMMITTED(lip, lsn);
1335
1336                 /*
1337                  * If the committed routine returns -1, make
1338                  * no more references to the item.
1339                  */
1340                 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) {
1341                         continue;
1342                 }
1343
1344                 /*
1345                  * If the returned lsn is greater than what it
1346                  * contained before, update the location of the
1347                  * item in the AIL.  If it is not, then do nothing.
1348                  * Items can never move backwards in the AIL.
1349                  *
1350                  * While the new lsn should usually be greater, it
1351                  * is possible that a later transaction completing
1352                  * simultaneously with an earlier one using the
1353                  * same item could complete first with a higher lsn.
1354                  * This would cause the earlier transaction to fail
1355                  * the test below.
1356                  */
1357                 mp = lip->li_mountp;
1358                 AIL_LOCK(mp,s);
1359                 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
1360                         /*
1361                          * This will set the item's lsn to item_lsn
1362                          * and update the position of the item in
1363                          * the AIL.
1364                          *
1365                          * xfs_trans_update_ail() drops the AIL lock.
1366                          */
1367                         xfs_trans_update_ail(mp, lip, item_lsn, s);
1368                 } else {
1369                         AIL_UNLOCK(mp, s);
1370                 }
1371
1372                 /*
1373                  * Now that we've repositioned the item in the AIL,
1374                  * unpin it so it can be flushed. Pass information
1375                  * about buffer stale state down from the log item
1376                  * flags, if anyone else stales the buffer we do not
1377                  * want to pay any attention to it.
1378                  */
1379                 IOP_UNPIN(lip, lidp->lid_flags & XFS_LID_BUF_STALE);
1380         }
1381 }
1382