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