blob: 39f0b1ed3224d3e0b9ffef3916ebda713b192778 [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{
Nathan Scott34327e12006-06-09 17:11:55 +1000239 vfs_wait_for_freeze(XFS_MTOVFS(mp), SB_FREEZE_TRANS);
240 return _xfs_trans_alloc(mp, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243xfs_trans_t *
244_xfs_trans_alloc(
245 xfs_mount_t *mp,
246 uint type)
247{
248 xfs_trans_t *tp;
249
Nathan Scott34327e12006-06-09 17:11:55 +1000250 atomic_inc(&mp->m_active_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Nathan Scott34327e12006-06-09 17:11:55 +1000252 tp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 tp->t_magic = XFS_TRANS_MAGIC;
254 tp->t_type = type;
255 tp->t_mountp = mp;
256 tp->t_items_free = XFS_LIC_NUM_SLOTS;
257 tp->t_busy_free = XFS_LBC_NUM_SLOTS;
258 XFS_LIC_INIT(&(tp->t_items));
259 XFS_LBC_INIT(&(tp->t_busy));
Nathan Scott34327e12006-06-09 17:11:55 +1000260 return tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263/*
264 * This is called to create a new transaction which will share the
265 * permanent log reservation of the given transaction. The remaining
266 * unused block and rt extent reservations are also inherited. This
267 * implies that the original transaction is no longer allowed to allocate
268 * blocks. Locks and log items, however, are no inherited. They must
269 * be added to the new transaction explicitly.
270 */
271xfs_trans_t *
272xfs_trans_dup(
273 xfs_trans_t *tp)
274{
275 xfs_trans_t *ntp;
276
277 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP);
278
279 /*
280 * Initialize the new transaction structure.
281 */
282 ntp->t_magic = XFS_TRANS_MAGIC;
283 ntp->t_type = tp->t_type;
284 ntp->t_mountp = tp->t_mountp;
285 ntp->t_items_free = XFS_LIC_NUM_SLOTS;
286 ntp->t_busy_free = XFS_LBC_NUM_SLOTS;
287 XFS_LIC_INIT(&(ntp->t_items));
288 XFS_LBC_INIT(&(ntp->t_busy));
289
290 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 ASSERT(tp->t_ticket != NULL);
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
294 ntp->t_ticket = tp->t_ticket;
295 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
296 tp->t_blk_res = tp->t_blk_res_used;
297 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
298 tp->t_rtx_res = tp->t_rtx_res_used;
Nathan Scott59c1b082006-06-09 14:59:13 +1000299 ntp->t_pflags = tp->t_pflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 XFS_TRANS_DUP_DQINFO(tp->t_mountp, tp, ntp);
302
303 atomic_inc(&tp->t_mountp->m_active_trans);
304 return ntp;
305}
306
307/*
308 * This is called to reserve free disk blocks and log space for the
309 * given transaction. This must be done before allocating any resources
310 * within the transaction.
311 *
312 * This will return ENOSPC if there are not enough blocks available.
313 * It will sleep waiting for available log space.
314 * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
315 * is used by long running transactions. If any one of the reservations
316 * fails then they will all be backed out.
317 *
318 * This does not do quota reservations. That typically is done by the
319 * caller afterwards.
320 */
321int
322xfs_trans_reserve(
323 xfs_trans_t *tp,
324 uint blocks,
325 uint logspace,
326 uint rtextents,
327 uint flags,
328 uint logcount)
329{
330 int log_flags;
Nathan Scott59c1b082006-06-09 14:59:13 +1000331 int error = 0;
332 int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* Mark this thread as being in a transaction */
Nathan Scott59c1b082006-06-09 14:59:13 +1000335 current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /*
338 * Attempt to reserve the needed disk blocks by decrementing
339 * the number needed from the number available. This will
340 * fail if the count would go below zero.
341 */
342 if (blocks > 0) {
343 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
344 -blocks, rsvd);
345 if (error != 0) {
Nathan Scott59c1b082006-06-09 14:59:13 +1000346 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return (XFS_ERROR(ENOSPC));
348 }
349 tp->t_blk_res += blocks;
350 }
351
352 /*
353 * Reserve the log space needed for this transaction.
354 */
355 if (logspace > 0) {
356 ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace));
357 ASSERT((tp->t_log_count == 0) ||
358 (tp->t_log_count == logcount));
359 if (flags & XFS_TRANS_PERM_LOG_RES) {
360 log_flags = XFS_LOG_PERM_RESERV;
361 tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
362 } else {
363 ASSERT(tp->t_ticket == NULL);
364 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
365 log_flags = 0;
366 }
367
368 error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
369 &tp->t_ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000370 XFS_TRANSACTION, log_flags, tp->t_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (error) {
372 goto undo_blocks;
373 }
374 tp->t_log_res = logspace;
375 tp->t_log_count = logcount;
376 }
377
378 /*
379 * Attempt to reserve the needed realtime extents by decrementing
380 * the number needed from the number available. This will
381 * fail if the count would go below zero.
382 */
383 if (rtextents > 0) {
384 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
385 -rtextents, rsvd);
386 if (error) {
387 error = XFS_ERROR(ENOSPC);
388 goto undo_log;
389 }
390 tp->t_rtx_res += rtextents;
391 }
392
393 return 0;
394
395 /*
396 * Error cases jump to one of these labels to undo any
397 * reservations which have already been performed.
398 */
399undo_log:
400 if (logspace > 0) {
401 if (flags & XFS_TRANS_PERM_LOG_RES) {
402 log_flags = XFS_LOG_REL_PERM_RESERV;
403 } else {
404 log_flags = 0;
405 }
406 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
407 tp->t_ticket = NULL;
408 tp->t_log_res = 0;
409 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
410 }
411
412undo_blocks:
413 if (blocks > 0) {
414 (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
415 blocks, rsvd);
416 tp->t_blk_res = 0;
417 }
418
Nathan Scott59c1b082006-06-09 14:59:13 +1000419 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Nathan Scott59c1b082006-06-09 14:59:13 +1000421 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424
425/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * Record the indicated change to the given field for application
427 * to the file system's superblock when the transaction commits.
428 * For now, just store the change in the transaction structure.
429 *
430 * Mark the transaction structure to indicate that the superblock
431 * needs to be updated before committing.
432 */
433void
434xfs_trans_mod_sb(
435 xfs_trans_t *tp,
436 uint field,
437 long delta)
438{
439
440 switch (field) {
441 case XFS_TRANS_SB_ICOUNT:
442 tp->t_icount_delta += delta;
443 break;
444 case XFS_TRANS_SB_IFREE:
445 tp->t_ifree_delta += delta;
446 break;
447 case XFS_TRANS_SB_FDBLOCKS:
448 /*
449 * Track the number of blocks allocated in the
450 * transaction. Make sure it does not exceed the
451 * number reserved.
452 */
453 if (delta < 0) {
454 tp->t_blk_res_used += (uint)-delta;
455 ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
456 }
457 tp->t_fdblocks_delta += delta;
458 break;
459 case XFS_TRANS_SB_RES_FDBLOCKS:
460 /*
461 * The allocation has already been applied to the
462 * in-core superblock's counter. This should only
463 * be applied to the on-disk superblock.
464 */
465 ASSERT(delta < 0);
466 tp->t_res_fdblocks_delta += delta;
467 break;
468 case XFS_TRANS_SB_FREXTENTS:
469 /*
470 * Track the number of blocks allocated in the
471 * transaction. Make sure it does not exceed the
472 * number reserved.
473 */
474 if (delta < 0) {
475 tp->t_rtx_res_used += (uint)-delta;
476 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
477 }
478 tp->t_frextents_delta += delta;
479 break;
480 case XFS_TRANS_SB_RES_FREXTENTS:
481 /*
482 * The allocation has already been applied to the
Nathan Scottc41564b2006-03-29 08:55:14 +1000483 * in-core superblock's counter. This should only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * be applied to the on-disk superblock.
485 */
486 ASSERT(delta < 0);
487 tp->t_res_frextents_delta += delta;
488 break;
489 case XFS_TRANS_SB_DBLOCKS:
490 ASSERT(delta > 0);
491 tp->t_dblocks_delta += delta;
492 break;
493 case XFS_TRANS_SB_AGCOUNT:
494 ASSERT(delta > 0);
495 tp->t_agcount_delta += delta;
496 break;
497 case XFS_TRANS_SB_IMAXPCT:
498 tp->t_imaxpct_delta += delta;
499 break;
500 case XFS_TRANS_SB_REXTSIZE:
501 tp->t_rextsize_delta += delta;
502 break;
503 case XFS_TRANS_SB_RBMBLOCKS:
504 tp->t_rbmblocks_delta += delta;
505 break;
506 case XFS_TRANS_SB_RBLOCKS:
507 tp->t_rblocks_delta += delta;
508 break;
509 case XFS_TRANS_SB_REXTENTS:
510 tp->t_rextents_delta += delta;
511 break;
512 case XFS_TRANS_SB_REXTSLOG:
513 tp->t_rextslog_delta += delta;
514 break;
515 default:
516 ASSERT(0);
517 return;
518 }
519
520 tp->t_flags |= (XFS_TRANS_SB_DIRTY | XFS_TRANS_DIRTY);
521}
522
523/*
524 * xfs_trans_apply_sb_deltas() is called from the commit code
525 * to bring the superblock buffer into the current transaction
526 * and modify it as requested by earlier calls to xfs_trans_mod_sb().
527 *
528 * For now we just look at each field allowed to change and change
529 * it if necessary.
530 */
531STATIC void
532xfs_trans_apply_sb_deltas(
533 xfs_trans_t *tp)
534{
535 xfs_sb_t *sbp;
536 xfs_buf_t *bp;
537 int whole = 0;
538
539 bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
540 sbp = XFS_BUF_TO_SBP(bp);
541
542 /*
543 * Check that superblock mods match the mods made to AGF counters.
544 */
545 ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
546 (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
547 tp->t_ag_btree_delta));
548
549 if (tp->t_icount_delta != 0) {
550 INT_MOD(sbp->sb_icount, ARCH_CONVERT, tp->t_icount_delta);
551 }
552 if (tp->t_ifree_delta != 0) {
553 INT_MOD(sbp->sb_ifree, ARCH_CONVERT, tp->t_ifree_delta);
554 }
555
556 if (tp->t_fdblocks_delta != 0) {
557 INT_MOD(sbp->sb_fdblocks, ARCH_CONVERT, tp->t_fdblocks_delta);
558 }
559 if (tp->t_res_fdblocks_delta != 0) {
560 INT_MOD(sbp->sb_fdblocks, ARCH_CONVERT, tp->t_res_fdblocks_delta);
561 }
562
563 if (tp->t_frextents_delta != 0) {
564 INT_MOD(sbp->sb_frextents, ARCH_CONVERT, tp->t_frextents_delta);
565 }
566 if (tp->t_res_frextents_delta != 0) {
567 INT_MOD(sbp->sb_frextents, ARCH_CONVERT, tp->t_res_frextents_delta);
568 }
569 if (tp->t_dblocks_delta != 0) {
570 INT_MOD(sbp->sb_dblocks, ARCH_CONVERT, tp->t_dblocks_delta);
571 whole = 1;
572 }
573 if (tp->t_agcount_delta != 0) {
574 INT_MOD(sbp->sb_agcount, ARCH_CONVERT, tp->t_agcount_delta);
575 whole = 1;
576 }
577 if (tp->t_imaxpct_delta != 0) {
578 INT_MOD(sbp->sb_imax_pct, ARCH_CONVERT, tp->t_imaxpct_delta);
579 whole = 1;
580 }
581 if (tp->t_rextsize_delta != 0) {
582 INT_MOD(sbp->sb_rextsize, ARCH_CONVERT, tp->t_rextsize_delta);
583 whole = 1;
584 }
585 if (tp->t_rbmblocks_delta != 0) {
586 INT_MOD(sbp->sb_rbmblocks, ARCH_CONVERT, tp->t_rbmblocks_delta);
587 whole = 1;
588 }
589 if (tp->t_rblocks_delta != 0) {
590 INT_MOD(sbp->sb_rblocks, ARCH_CONVERT, tp->t_rblocks_delta);
591 whole = 1;
592 }
593 if (tp->t_rextents_delta != 0) {
594 INT_MOD(sbp->sb_rextents, ARCH_CONVERT, tp->t_rextents_delta);
595 whole = 1;
596 }
597 if (tp->t_rextslog_delta != 0) {
598 INT_MOD(sbp->sb_rextslog, ARCH_CONVERT, tp->t_rextslog_delta);
599 whole = 1;
600 }
601
602 if (whole)
603 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000604 * Log the whole thing, the fields are noncontiguous.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 */
606 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_sb_t) - 1);
607 else
608 /*
609 * Since all the modifiable fields are contiguous, we
610 * can get away with this.
611 */
612 xfs_trans_log_buf(tp, bp, offsetof(xfs_sb_t, sb_icount),
613 offsetof(xfs_sb_t, sb_frextents) +
614 sizeof(sbp->sb_frextents) - 1);
615
616 XFS_MTOVFS(tp->t_mountp)->vfs_super->s_dirt = 1;
617}
618
619/*
620 * xfs_trans_unreserve_and_mod_sb() is called to release unused
621 * reservations and apply superblock counter changes to the in-core
622 * superblock.
623 *
624 * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
625 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000626STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627xfs_trans_unreserve_and_mod_sb(
628 xfs_trans_t *tp)
629{
630 xfs_mod_sb_t msb[14]; /* If you add cases, add entries */
631 xfs_mod_sb_t *msbp;
632 /* REFERENCED */
633 int error;
634 int rsvd;
635
636 msbp = msb;
637 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
638
639 /*
640 * Release any reserved blocks. Any that were allocated
641 * will be taken back again by fdblocks_delta below.
642 */
643 if (tp->t_blk_res > 0) {
644 msbp->msb_field = XFS_SBS_FDBLOCKS;
645 msbp->msb_delta = tp->t_blk_res;
646 msbp++;
647 }
648
649 /*
650 * Release any reserved real time extents . Any that were
651 * allocated will be taken back again by frextents_delta below.
652 */
653 if (tp->t_rtx_res > 0) {
654 msbp->msb_field = XFS_SBS_FREXTENTS;
655 msbp->msb_delta = tp->t_rtx_res;
656 msbp++;
657 }
658
659 /*
660 * Apply any superblock modifications to the in-core version.
661 * The t_res_fdblocks_delta and t_res_frextents_delta fields are
Nathan Scottc41564b2006-03-29 08:55:14 +1000662 * explicitly NOT applied to the in-core superblock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 * The idea is that that has already been done.
664 */
665 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
666 if (tp->t_icount_delta != 0) {
667 msbp->msb_field = XFS_SBS_ICOUNT;
668 msbp->msb_delta = (int)tp->t_icount_delta;
669 msbp++;
670 }
671 if (tp->t_ifree_delta != 0) {
672 msbp->msb_field = XFS_SBS_IFREE;
673 msbp->msb_delta = (int)tp->t_ifree_delta;
674 msbp++;
675 }
676 if (tp->t_fdblocks_delta != 0) {
677 msbp->msb_field = XFS_SBS_FDBLOCKS;
678 msbp->msb_delta = (int)tp->t_fdblocks_delta;
679 msbp++;
680 }
681 if (tp->t_frextents_delta != 0) {
682 msbp->msb_field = XFS_SBS_FREXTENTS;
683 msbp->msb_delta = (int)tp->t_frextents_delta;
684 msbp++;
685 }
686 if (tp->t_dblocks_delta != 0) {
687 msbp->msb_field = XFS_SBS_DBLOCKS;
688 msbp->msb_delta = (int)tp->t_dblocks_delta;
689 msbp++;
690 }
691 if (tp->t_agcount_delta != 0) {
692 msbp->msb_field = XFS_SBS_AGCOUNT;
693 msbp->msb_delta = (int)tp->t_agcount_delta;
694 msbp++;
695 }
696 if (tp->t_imaxpct_delta != 0) {
697 msbp->msb_field = XFS_SBS_IMAX_PCT;
698 msbp->msb_delta = (int)tp->t_imaxpct_delta;
699 msbp++;
700 }
701 if (tp->t_rextsize_delta != 0) {
702 msbp->msb_field = XFS_SBS_REXTSIZE;
703 msbp->msb_delta = (int)tp->t_rextsize_delta;
704 msbp++;
705 }
706 if (tp->t_rbmblocks_delta != 0) {
707 msbp->msb_field = XFS_SBS_RBMBLOCKS;
708 msbp->msb_delta = (int)tp->t_rbmblocks_delta;
709 msbp++;
710 }
711 if (tp->t_rblocks_delta != 0) {
712 msbp->msb_field = XFS_SBS_RBLOCKS;
713 msbp->msb_delta = (int)tp->t_rblocks_delta;
714 msbp++;
715 }
716 if (tp->t_rextents_delta != 0) {
717 msbp->msb_field = XFS_SBS_REXTENTS;
718 msbp->msb_delta = (int)tp->t_rextents_delta;
719 msbp++;
720 }
721 if (tp->t_rextslog_delta != 0) {
722 msbp->msb_field = XFS_SBS_REXTSLOG;
723 msbp->msb_delta = (int)tp->t_rextslog_delta;
724 msbp++;
725 }
726 }
727
728 /*
729 * If we need to change anything, do it.
730 */
731 if (msbp > msb) {
732 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
733 (uint)(msbp - msb), rsvd);
734 ASSERT(error == 0);
735 }
736}
737
738
739/*
740 * xfs_trans_commit
741 *
742 * Commit the given transaction to the log a/synchronously.
743 *
744 * XFS disk error handling mechanism is not based on a typical
745 * transaction abort mechanism. Logically after the filesystem
746 * gets marked 'SHUTDOWN', we can't let any new transactions
747 * be durable - ie. committed to disk - because some metadata might
748 * be inconsistent. In such cases, this returns an error, and the
749 * caller may assume that all locked objects joined to the transaction
750 * have already been unlocked as if the commit had succeeded.
751 * Do not reference the transaction structure after this call.
752 */
753 /*ARGSUSED*/
754int
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100755_xfs_trans_commit(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 xfs_trans_t *tp,
757 uint flags,
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100758 xfs_lsn_t *commit_lsn_p,
759 int *log_flushed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 xfs_log_iovec_t *log_vector;
762 int nvec;
763 xfs_mount_t *mp;
764 xfs_lsn_t commit_lsn;
765 /* REFERENCED */
766 int error;
767 int log_flags;
768 int sync;
769#define XFS_TRANS_LOGVEC_COUNT 16
770 xfs_log_iovec_t log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 void *commit_iclog;
772 int shutdown;
773
774 commit_lsn = -1;
775
776 /*
777 * Determine whether this commit is releasing a permanent
778 * log reservation or not.
779 */
780 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
781 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
782 log_flags = XFS_LOG_REL_PERM_RESERV;
783 } else {
784 log_flags = 0;
785 }
786 mp = tp->t_mountp;
787
788 /*
789 * If there is nothing to be logged by the transaction,
790 * then unlock all of the items associated with the
791 * transaction and free the transaction structure.
792 * Also make sure to return any reserved blocks to
793 * the free pool.
794 */
795shut_us_down:
796 shutdown = XFS_FORCED_SHUTDOWN(mp) ? EIO : 0;
797 if (!(tp->t_flags & XFS_TRANS_DIRTY) || shutdown) {
798 xfs_trans_unreserve_and_mod_sb(tp);
799 /*
800 * It is indeed possible for the transaction to be
801 * not dirty but the dqinfo portion to be. All that
802 * means is that we have some (non-persistent) quota
803 * reservations that need to be unreserved.
804 */
805 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(mp, tp);
806 if (tp->t_ticket) {
807 commit_lsn = xfs_log_done(mp, tp->t_ticket,
808 NULL, log_flags);
809 if (commit_lsn == -1 && !shutdown)
810 shutdown = XFS_ERROR(EIO);
811 }
Nathan Scott59c1b082006-06-09 14:59:13 +1000812 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 xfs_trans_free_items(tp, shutdown? XFS_TRANS_ABORT : 0);
814 xfs_trans_free_busy(tp);
815 xfs_trans_free(tp);
816 XFS_STATS_INC(xs_trans_empty);
817 if (commit_lsn_p)
818 *commit_lsn_p = commit_lsn;
819 return (shutdown);
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 ASSERT(tp->t_ticket != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /*
824 * If we need to update the superblock, then do it now.
825 */
826 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
827 xfs_trans_apply_sb_deltas(tp);
828 }
829 XFS_TRANS_APPLY_DQUOT_DELTAS(mp, tp);
830
831 /*
832 * Ask each log item how many log_vector entries it will
833 * need so we can figure out how many to allocate.
834 * Try to avoid the kmem_alloc() call in the common case
835 * by using a vector from the stack when it fits.
836 */
837 nvec = xfs_trans_count_vecs(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (nvec == 0) {
Nathan Scott7d04a332006-06-09 14:58:38 +1000839 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 goto shut_us_down;
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100841 } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 log_vector = log_vector_fast;
843 } else {
844 log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
845 sizeof(xfs_log_iovec_t),
846 KM_SLEEP);
847 }
848
849 /*
850 * Fill in the log_vector and pin the logged items, and
851 * then write the transaction to the log.
852 */
853 xfs_trans_fill_vecs(tp, log_vector);
854
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100855 error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /*
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100858 * The transaction is committed incore here, and can go out to disk
859 * at any time after this call. However, all the items associated
860 * with the transaction are still locked and pinned in memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 */
862 commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 tp->t_commit_lsn = commit_lsn;
865 if (nvec > XFS_TRANS_LOGVEC_COUNT) {
866 kmem_free(log_vector, nvec * sizeof(xfs_log_iovec_t));
867 }
868
869 if (commit_lsn_p)
870 *commit_lsn_p = commit_lsn;
871
872 /*
873 * If we got a log write error. Unpin the logitems that we
874 * had pinned, clean up, free trans structure, and return error.
875 */
876 if (error || commit_lsn == -1) {
Nathan Scott59c1b082006-06-09 14:59:13 +1000877 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
879 return XFS_ERROR(EIO);
880 }
881
882 /*
883 * Once the transaction has committed, unused
884 * reservations need to be released and changes to
885 * the superblock need to be reflected in the in-core
886 * version. Do that now.
887 */
888 xfs_trans_unreserve_and_mod_sb(tp);
889
890 sync = tp->t_flags & XFS_TRANS_SYNC;
891
892 /*
893 * Tell the LM to call the transaction completion routine
894 * when the log write with LSN commit_lsn completes (e.g.
895 * when the transaction commit really hits the on-disk log).
896 * After this call we cannot reference tp, because the call
897 * can happen at any time and the call will free the transaction
898 * structure pointed to by tp. The only case where we call
899 * the completion routine (xfs_trans_committed) directly is
900 * if the log is turned off on a debug kernel or we're
901 * running in simulation mode (the log is explicitly turned
902 * off).
903 */
904 tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed;
905 tp->t_logcb.cb_arg = tp;
906
907 /*
908 * We need to pass the iclog buffer which was used for the
909 * transaction commit record into this function, and attach
910 * the callback to it. The callback must be attached before
911 * the items are unlocked to avoid racing with other threads
912 * waiting for an item to unlock.
913 */
914 shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
915
916 /*
917 * Mark this thread as no longer being in a transaction
918 */
Nathan Scott59c1b082006-06-09 14:59:13 +1000919 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 /*
922 * Once all the items of the transaction have been copied
923 * to the in core log and the callback is attached, the
924 * items can be unlocked.
925 *
926 * This will free descriptors pointing to items which were
927 * not logged since there is nothing more to do with them.
928 * For items which were logged, we will keep pointers to them
929 * so they can be unpinned after the transaction commits to disk.
930 * This will also stamp each modified meta-data item with
931 * the commit lsn of this transaction for dependency tracking
932 * purposes.
933 */
934 xfs_trans_unlock_items(tp, commit_lsn);
935
936 /*
937 * If we detected a log error earlier, finish committing
938 * the transaction now (unpin log items, etc).
939 *
940 * Order is critical here, to avoid using the transaction
941 * pointer after its been freed (by xfs_trans_committed
942 * either here now, or as a callback). We cannot do this
943 * step inside xfs_log_notify as was done earlier because
944 * of this issue.
945 */
946 if (shutdown)
947 xfs_trans_committed(tp, XFS_LI_ABORTED);
948
949 /*
950 * Now that the xfs_trans_committed callback has been attached,
951 * and the items are released we can finally allow the iclog to
952 * go to disk.
953 */
954 error = xfs_log_release_iclog(mp, commit_iclog);
955
956 /*
957 * If the transaction needs to be synchronous, then force the
958 * log out now and wait for it.
959 */
960 if (sync) {
Christoph Hellwigf538d4d2005-11-02 10:26:59 +1100961 if (!error) {
962 error = _xfs_log_force(mp, commit_lsn,
963 XFS_LOG_FORCE | XFS_LOG_SYNC,
964 log_flushed);
965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 XFS_STATS_INC(xs_trans_sync);
967 } else {
968 XFS_STATS_INC(xs_trans_async);
969 }
970
971 return (error);
972}
973
974
975/*
976 * Total up the number of log iovecs needed to commit this
977 * transaction. The transaction itself needs one for the
978 * transaction header. Ask each dirty item in turn how many
979 * it needs to get the total.
980 */
981STATIC uint
982xfs_trans_count_vecs(
983 xfs_trans_t *tp)
984{
985 int nvecs;
986 xfs_log_item_desc_t *lidp;
987
988 nvecs = 1;
989 lidp = xfs_trans_first_item(tp);
990 ASSERT(lidp != NULL);
991
992 /* In the non-debug case we need to start bailing out if we
993 * didn't find a log_item here, return zero and let trans_commit
994 * deal with it.
995 */
996 if (lidp == NULL)
997 return 0;
998
999 while (lidp != NULL) {
1000 /*
1001 * Skip items which aren't dirty in this transaction.
1002 */
1003 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
1004 lidp = xfs_trans_next_item(tp, lidp);
1005 continue;
1006 }
1007 lidp->lid_size = IOP_SIZE(lidp->lid_item);
1008 nvecs += lidp->lid_size;
1009 lidp = xfs_trans_next_item(tp, lidp);
1010 }
1011
1012 return nvecs;
1013}
1014
1015/*
1016 * Called from the trans_commit code when we notice that
1017 * the filesystem is in the middle of a forced shutdown.
1018 */
1019STATIC void
1020xfs_trans_uncommit(
1021 xfs_trans_t *tp,
1022 uint flags)
1023{
1024 xfs_log_item_desc_t *lidp;
1025
1026 for (lidp = xfs_trans_first_item(tp);
1027 lidp != NULL;
1028 lidp = xfs_trans_next_item(tp, lidp)) {
1029 /*
1030 * Unpin all but those that aren't dirty.
1031 */
1032 if (lidp->lid_flags & XFS_LID_DIRTY)
1033 IOP_UNPIN_REMOVE(lidp->lid_item, tp);
1034 }
1035
1036 xfs_trans_unreserve_and_mod_sb(tp);
1037 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(tp->t_mountp, tp);
1038
1039 xfs_trans_free_items(tp, flags);
1040 xfs_trans_free_busy(tp);
1041 xfs_trans_free(tp);
1042}
1043
1044/*
1045 * Fill in the vector with pointers to data to be logged
1046 * by this transaction. The transaction header takes
1047 * the first vector, and then each dirty item takes the
1048 * number of vectors it indicated it needed in xfs_trans_count_vecs().
1049 *
1050 * As each item fills in the entries it needs, also pin the item
1051 * so that it cannot be flushed out until the log write completes.
1052 */
1053STATIC void
1054xfs_trans_fill_vecs(
1055 xfs_trans_t *tp,
1056 xfs_log_iovec_t *log_vector)
1057{
1058 xfs_log_item_desc_t *lidp;
1059 xfs_log_iovec_t *vecp;
1060 uint nitems;
1061
1062 /*
1063 * Skip over the entry for the transaction header, we'll
1064 * fill that in at the end.
1065 */
1066 vecp = log_vector + 1; /* pointer arithmetic */
1067
1068 nitems = 0;
1069 lidp = xfs_trans_first_item(tp);
1070 ASSERT(lidp != NULL);
1071 while (lidp != NULL) {
1072 /*
1073 * Skip items which aren't dirty in this transaction.
1074 */
1075 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
1076 lidp = xfs_trans_next_item(tp, lidp);
1077 continue;
1078 }
1079 /*
1080 * The item may be marked dirty but not log anything.
1081 * This can be used to get called when a transaction
1082 * is committed.
1083 */
1084 if (lidp->lid_size) {
1085 nitems++;
1086 }
1087 IOP_FORMAT(lidp->lid_item, vecp);
1088 vecp += lidp->lid_size; /* pointer arithmetic */
1089 IOP_PIN(lidp->lid_item);
1090 lidp = xfs_trans_next_item(tp, lidp);
1091 }
1092
1093 /*
1094 * Now that we've counted the number of items in this
1095 * transaction, fill in the transaction header.
1096 */
1097 tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
1098 tp->t_header.th_type = tp->t_type;
1099 tp->t_header.th_num_items = nitems;
1100 log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
1101 log_vector->i_len = sizeof(xfs_trans_header_t);
Tim Shimmin7e9c6392005-09-02 16:42:05 +10001102 XLOG_VEC_SET_TYPE(log_vector, XLOG_REG_TYPE_TRANSHDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
1105
1106/*
1107 * Unlock all of the transaction's items and free the transaction.
1108 * The transaction must not have modified any of its items, because
1109 * there is no way to restore them to their previous state.
1110 *
1111 * If the transaction has made a log reservation, make sure to release
1112 * it as well.
1113 */
1114void
1115xfs_trans_cancel(
1116 xfs_trans_t *tp,
1117 int flags)
1118{
1119 int log_flags;
1120#ifdef DEBUG
1121 xfs_log_item_chunk_t *licp;
1122 xfs_log_item_desc_t *lidp;
1123 xfs_log_item_t *lip;
1124 int i;
1125#endif
Ryan Hankins0733af22006-01-11 15:36:44 +11001126 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /*
1129 * See if the caller is being too lazy to figure out if
1130 * the transaction really needs an abort.
1131 */
1132 if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
1133 flags &= ~XFS_TRANS_ABORT;
1134 /*
1135 * See if the caller is relying on us to shut down the
1136 * filesystem. This happens in paths where we detect
1137 * corruption and decide to give up.
1138 */
Nathan Scott60a204f2006-01-11 15:37:00 +11001139 if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
Ryan Hankins0733af22006-01-11 15:36:44 +11001140 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001141 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Nathan Scott60a204f2006-01-11 15:37:00 +11001142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143#ifdef DEBUG
1144 if (!(flags & XFS_TRANS_ABORT)) {
1145 licp = &(tp->t_items);
1146 while (licp != NULL) {
1147 lidp = licp->lic_descs;
1148 for (i = 0; i < licp->lic_unused; i++, lidp++) {
1149 if (XFS_LIC_ISFREE(licp, i)) {
1150 continue;
1151 }
1152
1153 lip = lidp->lid_item;
Ryan Hankins0733af22006-01-11 15:36:44 +11001154 if (!XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 ASSERT(!(lip->li_type == XFS_LI_EFD));
1156 }
1157 licp = licp->lic_next;
1158 }
1159 }
1160#endif
1161 xfs_trans_unreserve_and_mod_sb(tp);
Ryan Hankins0733af22006-01-11 15:36:44 +11001162 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(mp, tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 if (tp->t_ticket) {
1165 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1166 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1167 log_flags = XFS_LOG_REL_PERM_RESERV;
1168 } else {
1169 log_flags = 0;
1170 }
Ryan Hankins0733af22006-01-11 15:36:44 +11001171 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173
1174 /* mark this thread as no longer being in a transaction */
Nathan Scott59c1b082006-06-09 14:59:13 +10001175 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 xfs_trans_free_items(tp, flags);
1178 xfs_trans_free_busy(tp);
1179 xfs_trans_free(tp);
1180}
1181
1182
1183/*
1184 * Free the transaction structure. If there is more clean up
1185 * to do when the structure is freed, add it here.
1186 */
1187STATIC void
1188xfs_trans_free(
1189 xfs_trans_t *tp)
1190{
1191 atomic_dec(&tp->t_mountp->m_active_trans);
1192 XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp);
1193 kmem_zone_free(xfs_trans_zone, tp);
1194}
1195
1196
1197/*
1198 * THIS SHOULD BE REWRITTEN TO USE xfs_trans_next_item().
1199 *
1200 * This is typically called by the LM when a transaction has been fully
1201 * committed to disk. It needs to unpin the items which have
1202 * been logged by the transaction and update their positions
1203 * in the AIL if necessary.
1204 * This also gets called when the transactions didn't get written out
1205 * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
1206 *
1207 * Call xfs_trans_chunk_committed() to process the items in
1208 * each chunk.
1209 */
1210STATIC void
1211xfs_trans_committed(
1212 xfs_trans_t *tp,
1213 int abortflag)
1214{
1215 xfs_log_item_chunk_t *licp;
1216 xfs_log_item_chunk_t *next_licp;
1217 xfs_log_busy_chunk_t *lbcp;
1218 xfs_log_busy_slot_t *lbsp;
1219 int i;
1220
1221 /*
1222 * Call the transaction's completion callback if there
1223 * is one.
1224 */
1225 if (tp->t_callback != NULL) {
1226 tp->t_callback(tp, tp->t_callarg);
1227 }
1228
1229 /*
1230 * Special case the chunk embedded in the transaction.
1231 */
1232 licp = &(tp->t_items);
1233 if (!(XFS_LIC_ARE_ALL_FREE(licp))) {
1234 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
1235 }
1236
1237 /*
1238 * Process the items in each chunk in turn.
1239 */
1240 licp = licp->lic_next;
1241 while (licp != NULL) {
1242 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp));
1243 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag);
1244 next_licp = licp->lic_next;
1245 kmem_free(licp, sizeof(xfs_log_item_chunk_t));
1246 licp = next_licp;
1247 }
1248
1249 /*
1250 * Clear all the per-AG busy list items listed in this transaction
1251 */
1252 lbcp = &tp->t_busy;
1253 while (lbcp != NULL) {
1254 for (i = 0, lbsp = lbcp->lbc_busy; i < lbcp->lbc_unused; i++, lbsp++) {
1255 if (!XFS_LBC_ISFREE(lbcp, i)) {
1256 xfs_alloc_clear_busy(tp, lbsp->lbc_ag,
1257 lbsp->lbc_idx);
1258 }
1259 }
1260 lbcp = lbcp->lbc_next;
1261 }
1262 xfs_trans_free_busy(tp);
1263
1264 /*
1265 * That's it for the transaction structure. Free it.
1266 */
1267 xfs_trans_free(tp);
1268}
1269
1270/*
1271 * This is called to perform the commit processing for each
1272 * item described by the given chunk.
1273 *
1274 * The commit processing consists of unlocking items which were
1275 * held locked with the SYNC_UNLOCK attribute, calling the committed
1276 * routine of each logged item, updating the item's position in the AIL
1277 * if necessary, and unpinning each item. If the committed routine
1278 * returns -1, then do nothing further with the item because it
1279 * may have been freed.
1280 *
1281 * Since items are unlocked when they are copied to the incore
1282 * log, it is possible for two transactions to be completing
1283 * and manipulating the same item simultaneously. The AIL lock
1284 * will protect the lsn field of each item. The value of this
1285 * field can never go backwards.
1286 *
1287 * We unpin the items after repositioning them in the AIL, because
1288 * otherwise they could be immediately flushed and we'd have to race
1289 * with the flusher trying to pull the item from the AIL as we add it.
1290 */
1291STATIC void
1292xfs_trans_chunk_committed(
1293 xfs_log_item_chunk_t *licp,
1294 xfs_lsn_t lsn,
1295 int aborted)
1296{
1297 xfs_log_item_desc_t *lidp;
1298 xfs_log_item_t *lip;
1299 xfs_lsn_t item_lsn;
1300 struct xfs_mount *mp;
1301 int i;
1302 SPLDECL(s);
1303
1304 lidp = licp->lic_descs;
1305 for (i = 0; i < licp->lic_unused; i++, lidp++) {
1306 if (XFS_LIC_ISFREE(licp, i)) {
1307 continue;
1308 }
1309
1310 lip = lidp->lid_item;
1311 if (aborted)
1312 lip->li_flags |= XFS_LI_ABORTED;
1313
1314 /*
1315 * Send in the ABORTED flag to the COMMITTED routine
1316 * so that it knows whether the transaction was aborted
1317 * or not.
1318 */
1319 item_lsn = IOP_COMMITTED(lip, lsn);
1320
1321 /*
1322 * If the committed routine returns -1, make
1323 * no more references to the item.
1324 */
1325 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) {
1326 continue;
1327 }
1328
1329 /*
1330 * If the returned lsn is greater than what it
1331 * contained before, update the location of the
1332 * item in the AIL. If it is not, then do nothing.
1333 * Items can never move backwards in the AIL.
1334 *
1335 * While the new lsn should usually be greater, it
1336 * is possible that a later transaction completing
1337 * simultaneously with an earlier one using the
1338 * same item could complete first with a higher lsn.
1339 * This would cause the earlier transaction to fail
1340 * the test below.
1341 */
1342 mp = lip->li_mountp;
1343 AIL_LOCK(mp,s);
1344 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
1345 /*
1346 * This will set the item's lsn to item_lsn
1347 * and update the position of the item in
1348 * the AIL.
1349 *
1350 * xfs_trans_update_ail() drops the AIL lock.
1351 */
1352 xfs_trans_update_ail(mp, lip, item_lsn, s);
1353 } else {
1354 AIL_UNLOCK(mp, s);
1355 }
1356
1357 /*
1358 * Now that we've repositioned the item in the AIL,
1359 * unpin it so it can be flushed. Pass information
1360 * about buffer stale state down from the log item
1361 * flags, if anyone else stales the buffer we do not
1362 * want to pay any attention to it.
1363 */
1364 IOP_UNPIN(lip, lidp->lid_flags & XFS_LID_BUF_STALE);
1365 }
1366}