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