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