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