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