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