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