blob: ce558efa2ea02a8ba9fd1f8b2bb2bad121a64071 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
28#include "xfs_dmapi.h"
29#include "xfs_mount.h"
30#include "xfs_error.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_ialloc_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_dir2_sf.h"
36#include "xfs_attr_sf.h"
37#include "xfs_dinode.h"
38#include "xfs_inode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_btree.h"
40#include "xfs_ialloc.h"
41#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_bmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_quota.h"
Nathan Scotta844f452005-11-02 14:38:42 +110044#include "xfs_trans_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_trans_space.h"
Niv Sardi322ff6b2008-08-13 16:05:49 +100046#include "xfs_inode_item.h"
Dave Chinnered3b4d62010-05-21 12:07:08 +100047#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Nathan Scott8f794052006-03-14 13:32:41 +110049kmem_zone_t *xfs_trans_zone;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/*
Nathan Scott8f794052006-03-14 13:32:41 +110052 * Reservation functions here avoid a huge stack in xfs_trans_init
53 * due to register overflow from temporaries in the calculations.
54 */
Nathan Scott8f794052006-03-14 13:32:41 +110055STATIC uint
56xfs_calc_write_reservation(xfs_mount_t *mp)
57{
58 return XFS_CALC_WRITE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
59}
60
61STATIC uint
62xfs_calc_itruncate_reservation(xfs_mount_t *mp)
63{
64 return XFS_CALC_ITRUNCATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
65}
66
67STATIC uint
68xfs_calc_rename_reservation(xfs_mount_t *mp)
69{
70 return XFS_CALC_RENAME_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
71}
72
73STATIC uint
74xfs_calc_link_reservation(xfs_mount_t *mp)
75{
Nathan Scott2ddd5922006-03-17 17:25:46 +110076 return XFS_CALC_LINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
Nathan Scott8f794052006-03-14 13:32:41 +110077}
78
79STATIC uint
80xfs_calc_remove_reservation(xfs_mount_t *mp)
81{
82 return XFS_CALC_REMOVE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
83}
84
85STATIC uint
86xfs_calc_symlink_reservation(xfs_mount_t *mp)
87{
88 return XFS_CALC_SYMLINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
89}
90
91STATIC uint
92xfs_calc_create_reservation(xfs_mount_t *mp)
93{
94 return XFS_CALC_CREATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
95}
96
97STATIC uint
98xfs_calc_mkdir_reservation(xfs_mount_t *mp)
99{
100 return XFS_CALC_MKDIR_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
101}
102
103STATIC uint
104xfs_calc_ifree_reservation(xfs_mount_t *mp)
105{
106 return XFS_CALC_IFREE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
107}
108
109STATIC uint
110xfs_calc_ichange_reservation(xfs_mount_t *mp)
111{
112 return XFS_CALC_ICHANGE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
113}
114
115STATIC uint
116xfs_calc_growdata_reservation(xfs_mount_t *mp)
117{
118 return XFS_CALC_GROWDATA_LOG_RES(mp);
119}
120
121STATIC uint
122xfs_calc_growrtalloc_reservation(xfs_mount_t *mp)
123{
124 return XFS_CALC_GROWRTALLOC_LOG_RES(mp);
125}
126
127STATIC uint
128xfs_calc_growrtzero_reservation(xfs_mount_t *mp)
129{
130 return XFS_CALC_GROWRTZERO_LOG_RES(mp);
131}
132
133STATIC uint
134xfs_calc_growrtfree_reservation(xfs_mount_t *mp)
135{
136 return XFS_CALC_GROWRTFREE_LOG_RES(mp);
137}
138
139STATIC uint
140xfs_calc_swrite_reservation(xfs_mount_t *mp)
141{
142 return XFS_CALC_SWRITE_LOG_RES(mp);
143}
144
145STATIC uint
146xfs_calc_writeid_reservation(xfs_mount_t *mp)
147{
148 return XFS_CALC_WRITEID_LOG_RES(mp);
149}
150
151STATIC uint
152xfs_calc_addafork_reservation(xfs_mount_t *mp)
153{
154 return XFS_CALC_ADDAFORK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
155}
156
157STATIC uint
158xfs_calc_attrinval_reservation(xfs_mount_t *mp)
159{
160 return XFS_CALC_ATTRINVAL_LOG_RES(mp);
161}
162
163STATIC uint
164xfs_calc_attrset_reservation(xfs_mount_t *mp)
165{
166 return XFS_CALC_ATTRSET_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
167}
168
169STATIC uint
170xfs_calc_attrrm_reservation(xfs_mount_t *mp)
171{
172 return XFS_CALC_ATTRRM_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp);
173}
174
175STATIC uint
176xfs_calc_clear_agi_bucket_reservation(xfs_mount_t *mp)
177{
178 return XFS_CALC_CLEAR_AGI_BUCKET_LOG_RES(mp);
179}
180
181/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * Initialize the precomputed transaction reservation values
183 * in the mount structure.
184 */
185void
186xfs_trans_init(
187 xfs_mount_t *mp)
188{
189 xfs_trans_reservations_t *resp;
190
191 resp = &(mp->m_reservations);
Nathan Scott8f794052006-03-14 13:32:41 +1100192 resp->tr_write = xfs_calc_write_reservation(mp);
193 resp->tr_itruncate = xfs_calc_itruncate_reservation(mp);
194 resp->tr_rename = xfs_calc_rename_reservation(mp);
195 resp->tr_link = xfs_calc_link_reservation(mp);
196 resp->tr_remove = xfs_calc_remove_reservation(mp);
197 resp->tr_symlink = xfs_calc_symlink_reservation(mp);
198 resp->tr_create = xfs_calc_create_reservation(mp);
199 resp->tr_mkdir = xfs_calc_mkdir_reservation(mp);
200 resp->tr_ifree = xfs_calc_ifree_reservation(mp);
201 resp->tr_ichange = xfs_calc_ichange_reservation(mp);
202 resp->tr_growdata = xfs_calc_growdata_reservation(mp);
203 resp->tr_swrite = xfs_calc_swrite_reservation(mp);
204 resp->tr_writeid = xfs_calc_writeid_reservation(mp);
205 resp->tr_addafork = xfs_calc_addafork_reservation(mp);
206 resp->tr_attrinval = xfs_calc_attrinval_reservation(mp);
207 resp->tr_attrset = xfs_calc_attrset_reservation(mp);
208 resp->tr_attrrm = xfs_calc_attrrm_reservation(mp);
209 resp->tr_clearagi = xfs_calc_clear_agi_bucket_reservation(mp);
210 resp->tr_growrtalloc = xfs_calc_growrtalloc_reservation(mp);
211 resp->tr_growrtzero = xfs_calc_growrtzero_reservation(mp);
212 resp->tr_growrtfree = xfs_calc_growrtfree_reservation(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
215/*
216 * This routine is called to allocate a transaction structure.
217 * The type parameter indicates the type of the transaction. These
218 * are enumerated in xfs_trans.h.
219 *
220 * Dynamically allocate the transaction structure from the transaction
221 * zone, initialize it, and return it to the caller.
222 */
223xfs_trans_t *
224xfs_trans_alloc(
225 xfs_mount_t *mp,
226 uint type)
227{
Christoph Hellwigb267ce92007-08-30 17:21:30 +1000228 xfs_wait_for_freeze(mp, SB_FREEZE_TRANS);
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000229 return _xfs_trans_alloc(mp, type, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232xfs_trans_t *
233_xfs_trans_alloc(
234 xfs_mount_t *mp,
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000235 uint type,
236 uint memflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 xfs_trans_t *tp;
239
Nathan Scott34327e12006-06-09 17:11:55 +1000240 atomic_inc(&mp->m_active_trans);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Christoph Hellwig80641dc2009-10-19 04:00:03 +0000242 tp = kmem_zone_zalloc(xfs_trans_zone, memflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 tp->t_magic = XFS_TRANS_MAGIC;
244 tp->t_type = type;
245 tp->t_mountp = mp;
246 tp->t_items_free = XFS_LIC_NUM_SLOTS;
Eric Sandeen39dab9d2008-08-13 16:10:52 +1000247 xfs_lic_init(&(tp->t_items));
Dave Chinnered3b4d62010-05-21 12:07:08 +1000248 INIT_LIST_HEAD(&tp->t_busy);
Nathan Scott34327e12006-06-09 17:11:55 +1000249 return tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252/*
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100253 * Free the transaction structure. If there is more clean up
254 * to do when the structure is freed, add it here.
255 */
256STATIC void
257xfs_trans_free(
Dave Chinnered3b4d62010-05-21 12:07:08 +1000258 struct xfs_trans *tp)
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100259{
Dave Chinnered3b4d62010-05-21 12:07:08 +1000260 struct xfs_busy_extent *busyp, *n;
261
262 list_for_each_entry_safe(busyp, n, &tp->t_busy, list)
263 xfs_alloc_busy_clear(tp->t_mountp, busyp);
264
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100265 atomic_dec(&tp->t_mountp->m_active_trans);
266 xfs_trans_free_dqinfo(tp);
267 kmem_zone_free(xfs_trans_zone, tp);
268}
269
270/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 * 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;
Eric Sandeen39dab9d2008-08-13 16:10:52 +1000293 xfs_lic_init(&(ntp->t_items));
Dave Chinnered3b4d62010-05-21 12:07:08 +1000294 INIT_LIST_HEAD(&ntp->t_busy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 ASSERT(tp->t_ticket != NULL);
Nathan Scottcfcbbbd2005-11-02 15:12:04 +1100298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE);
Dave Chinnercc09c0d2008-11-17 17:37:10 +1100300 ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used;
302 tp->t_blk_res = tp->t_blk_res_used;
303 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used;
304 tp->t_rtx_res = tp->t_rtx_res_used;
Nathan Scott59c1b082006-06-09 14:59:13 +1000305 ntp->t_pflags = tp->t_pflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Christoph Hellwig7d095252009-06-08 15:33:32 +0200307 xfs_trans_dup_dqinfo(tp, ntp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 atomic_inc(&tp->t_mountp->m_active_trans);
310 return ntp;
311}
312
313/*
314 * This is called to reserve free disk blocks and log space for the
315 * given transaction. This must be done before allocating any resources
316 * within the transaction.
317 *
318 * This will return ENOSPC if there are not enough blocks available.
319 * It will sleep waiting for available log space.
320 * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which
321 * is used by long running transactions. If any one of the reservations
322 * fails then they will all be backed out.
323 *
324 * This does not do quota reservations. That typically is done by the
325 * caller afterwards.
326 */
327int
328xfs_trans_reserve(
329 xfs_trans_t *tp,
330 uint blocks,
331 uint logspace,
332 uint rtextents,
333 uint flags,
334 uint logcount)
335{
336 int log_flags;
Nathan Scott59c1b082006-06-09 14:59:13 +1000337 int error = 0;
338 int rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /* Mark this thread as being in a transaction */
Nathan Scott59c1b082006-06-09 14:59:13 +1000341 current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /*
344 * Attempt to reserve the needed disk blocks by decrementing
345 * the number needed from the number available. This will
346 * fail if the count would go below zero.
347 */
348 if (blocks > 0) {
349 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100350 -((int64_t)blocks), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (error != 0) {
Nathan Scott59c1b082006-06-09 14:59:13 +1000352 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return (XFS_ERROR(ENOSPC));
354 }
355 tp->t_blk_res += blocks;
356 }
357
358 /*
359 * Reserve the log space needed for this transaction.
360 */
361 if (logspace > 0) {
362 ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace));
363 ASSERT((tp->t_log_count == 0) ||
364 (tp->t_log_count == logcount));
365 if (flags & XFS_TRANS_PERM_LOG_RES) {
366 log_flags = XFS_LOG_PERM_RESERV;
367 tp->t_flags |= XFS_TRANS_PERM_LOG_RES;
368 } else {
369 ASSERT(tp->t_ticket == NULL);
370 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES));
371 log_flags = 0;
372 }
373
374 error = xfs_log_reserve(tp->t_mountp, logspace, logcount,
375 &tp->t_ticket,
Tim Shimmin7e9c6392005-09-02 16:42:05 +1000376 XFS_TRANSACTION, log_flags, tp->t_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (error) {
378 goto undo_blocks;
379 }
380 tp->t_log_res = logspace;
381 tp->t_log_count = logcount;
382 }
383
384 /*
385 * Attempt to reserve the needed realtime extents by decrementing
386 * the number needed from the number available. This will
387 * fail if the count would go below zero.
388 */
389 if (rtextents > 0) {
390 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100391 -((int64_t)rtextents), rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (error) {
393 error = XFS_ERROR(ENOSPC);
394 goto undo_log;
395 }
396 tp->t_rtx_res += rtextents;
397 }
398
399 return 0;
400
401 /*
402 * Error cases jump to one of these labels to undo any
403 * reservations which have already been performed.
404 */
405undo_log:
406 if (logspace > 0) {
407 if (flags & XFS_TRANS_PERM_LOG_RES) {
408 log_flags = XFS_LOG_REL_PERM_RESERV;
409 } else {
410 log_flags = 0;
411 }
412 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags);
413 tp->t_ticket = NULL;
414 tp->t_log_res = 0;
415 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES;
416 }
417
418undo_blocks:
419 if (blocks > 0) {
420 (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS,
David Chinner20f4ebf2007-02-10 18:36:10 +1100421 (int64_t)blocks, rsvd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 tp->t_blk_res = 0;
423 }
424
Nathan Scott59c1b082006-06-09 14:59:13 +1000425 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Nathan Scott59c1b082006-06-09 14:59:13 +1000427 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 * Record the indicated change to the given field for application
432 * to the file system's superblock when the transaction commits.
433 * For now, just store the change in the transaction structure.
434 *
435 * Mark the transaction structure to indicate that the superblock
436 * needs to be updated before committing.
David Chinner92821e22007-05-24 15:26:31 +1000437 *
438 * Because we may not be keeping track of allocated/free inodes and
439 * used filesystem blocks in the superblock, we do not mark the
440 * superblock dirty in this transaction if we modify these fields.
441 * We still need to update the transaction deltas so that they get
442 * applied to the incore superblock, but we don't want them to
443 * cause the superblock to get locked and logged if these are the
444 * only fields in the superblock that the transaction modifies.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 */
446void
447xfs_trans_mod_sb(
448 xfs_trans_t *tp,
449 uint field,
David Chinner20f4ebf2007-02-10 18:36:10 +1100450 int64_t delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
David Chinner92821e22007-05-24 15:26:31 +1000452 uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY);
453 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 switch (field) {
456 case XFS_TRANS_SB_ICOUNT:
457 tp->t_icount_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000458 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
459 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break;
461 case XFS_TRANS_SB_IFREE:
462 tp->t_ifree_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000463 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
464 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 break;
466 case XFS_TRANS_SB_FDBLOCKS:
467 /*
468 * Track the number of blocks allocated in the
469 * transaction. Make sure it does not exceed the
470 * number reserved.
471 */
472 if (delta < 0) {
473 tp->t_blk_res_used += (uint)-delta;
474 ASSERT(tp->t_blk_res_used <= tp->t_blk_res);
475 }
476 tp->t_fdblocks_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000477 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
478 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480 case XFS_TRANS_SB_RES_FDBLOCKS:
481 /*
482 * The allocation has already been applied to the
483 * in-core superblock's counter. This should only
484 * be applied to the on-disk superblock.
485 */
486 ASSERT(delta < 0);
487 tp->t_res_fdblocks_delta += delta;
David Chinner92821e22007-05-24 15:26:31 +1000488 if (xfs_sb_version_haslazysbcount(&mp->m_sb))
489 flags &= ~XFS_TRANS_SB_DIRTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 break;
491 case XFS_TRANS_SB_FREXTENTS:
492 /*
493 * Track the number of blocks allocated in the
494 * transaction. Make sure it does not exceed the
495 * number reserved.
496 */
497 if (delta < 0) {
498 tp->t_rtx_res_used += (uint)-delta;
499 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res);
500 }
501 tp->t_frextents_delta += delta;
502 break;
503 case XFS_TRANS_SB_RES_FREXTENTS:
504 /*
505 * The allocation has already been applied to the
Nathan Scottc41564b2006-03-29 08:55:14 +1000506 * in-core superblock's counter. This should only
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * be applied to the on-disk superblock.
508 */
509 ASSERT(delta < 0);
510 tp->t_res_frextents_delta += delta;
511 break;
512 case XFS_TRANS_SB_DBLOCKS:
513 ASSERT(delta > 0);
514 tp->t_dblocks_delta += delta;
515 break;
516 case XFS_TRANS_SB_AGCOUNT:
517 ASSERT(delta > 0);
518 tp->t_agcount_delta += delta;
519 break;
520 case XFS_TRANS_SB_IMAXPCT:
521 tp->t_imaxpct_delta += delta;
522 break;
523 case XFS_TRANS_SB_REXTSIZE:
524 tp->t_rextsize_delta += delta;
525 break;
526 case XFS_TRANS_SB_RBMBLOCKS:
527 tp->t_rbmblocks_delta += delta;
528 break;
529 case XFS_TRANS_SB_RBLOCKS:
530 tp->t_rblocks_delta += delta;
531 break;
532 case XFS_TRANS_SB_REXTENTS:
533 tp->t_rextents_delta += delta;
534 break;
535 case XFS_TRANS_SB_REXTSLOG:
536 tp->t_rextslog_delta += delta;
537 break;
538 default:
539 ASSERT(0);
540 return;
541 }
542
David Chinner210c6f12007-05-24 15:26:51 +1000543 tp->t_flags |= flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
546/*
547 * xfs_trans_apply_sb_deltas() is called from the commit code
548 * to bring the superblock buffer into the current transaction
549 * and modify it as requested by earlier calls to xfs_trans_mod_sb().
550 *
551 * For now we just look at each field allowed to change and change
552 * it if necessary.
553 */
554STATIC void
555xfs_trans_apply_sb_deltas(
556 xfs_trans_t *tp)
557{
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000558 xfs_dsb_t *sbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 xfs_buf_t *bp;
560 int whole = 0;
561
562 bp = xfs_trans_getsb(tp, tp->t_mountp, 0);
563 sbp = XFS_BUF_TO_SBP(bp);
564
565 /*
566 * Check that superblock mods match the mods made to AGF counters.
567 */
568 ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) ==
569 (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta +
570 tp->t_ag_btree_delta));
571
David Chinner92821e22007-05-24 15:26:31 +1000572 /*
573 * Only update the superblock counters if we are logging them
574 */
575 if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) {
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000576 if (tp->t_icount_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800577 be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000578 if (tp->t_ifree_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800579 be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000580 if (tp->t_fdblocks_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800581 be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000582 if (tp->t_res_fdblocks_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800583 be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000586 if (tp->t_frextents_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800587 be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000588 if (tp->t_res_frextents_delta)
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800589 be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta);
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000590
591 if (tp->t_dblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800592 be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 whole = 1;
594 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000595 if (tp->t_agcount_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800596 be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 whole = 1;
598 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000599 if (tp->t_imaxpct_delta) {
600 sbp->sb_imax_pct += tp->t_imaxpct_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 whole = 1;
602 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000603 if (tp->t_rextsize_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800604 be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 whole = 1;
606 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000607 if (tp->t_rbmblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800608 be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 whole = 1;
610 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000611 if (tp->t_rblocks_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800612 be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 whole = 1;
614 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000615 if (tp->t_rextents_delta) {
Marcin Slusarz413d57c2008-02-13 15:03:29 -0800616 be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 whole = 1;
618 }
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000619 if (tp->t_rextslog_delta) {
620 sbp->sb_rextslog += tp->t_rextslog_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 whole = 1;
622 }
623
624 if (whole)
625 /*
Nathan Scottc41564b2006-03-29 08:55:14 +1000626 * Log the whole thing, the fields are noncontiguous.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000628 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 else
630 /*
631 * Since all the modifiable fields are contiguous, we
632 * can get away with this.
633 */
Christoph Hellwig2bdf7cd2007-08-28 13:58:06 +1000634 xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount),
635 offsetof(xfs_dsb_t, sb_frextents) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 sizeof(sbp->sb_frextents) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639/*
David Chinner45c34142007-06-18 16:49:44 +1000640 * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations
641 * and apply superblock counter changes to the in-core superblock. The
642 * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT
643 * applied to the in-core superblock. The idea is that that has already been
644 * done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 *
646 * This is done efficiently with a single call to xfs_mod_incore_sb_batch().
David Chinner45c34142007-06-18 16:49:44 +1000647 * However, we have to ensure that we only modify each superblock field only
648 * once because the application of the delta values may not be atomic. That can
649 * lead to ENOSPC races occurring if we have two separate modifcations of the
650 * free space counter to put back the entire reservation and then take away
651 * what we used.
652 *
653 * If we are not logging superblock counters, then the inode allocated/free and
654 * used block counts are not updated in the on disk superblock. In this case,
655 * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we
656 * still need to update the incore superblock with the changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 */
Dave Chinner71e330b2010-05-21 14:37:18 +1000658void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659xfs_trans_unreserve_and_mod_sb(
660 xfs_trans_t *tp)
661{
662 xfs_mod_sb_t msb[14]; /* If you add cases, add entries */
663 xfs_mod_sb_t *msbp;
David Chinner92821e22007-05-24 15:26:31 +1000664 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* REFERENCED */
666 int error;
667 int rsvd;
David Chinner45c34142007-06-18 16:49:44 +1000668 int64_t blkdelta = 0;
669 int64_t rtxdelta = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 msbp = msb;
672 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0;
673
David Chinner45c34142007-06-18 16:49:44 +1000674 /* calculate free blocks delta */
675 if (tp->t_blk_res > 0)
676 blkdelta = tp->t_blk_res;
677
678 if ((tp->t_fdblocks_delta != 0) &&
679 (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
680 (tp->t_flags & XFS_TRANS_SB_DIRTY)))
681 blkdelta += tp->t_fdblocks_delta;
682
683 if (blkdelta != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 msbp->msb_field = XFS_SBS_FDBLOCKS;
David Chinner45c34142007-06-18 16:49:44 +1000685 msbp->msb_delta = blkdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 msbp++;
687 }
688
David Chinner45c34142007-06-18 16:49:44 +1000689 /* calculate free realtime extents delta */
690 if (tp->t_rtx_res > 0)
691 rtxdelta = tp->t_rtx_res;
692
693 if ((tp->t_frextents_delta != 0) &&
694 (tp->t_flags & XFS_TRANS_SB_DIRTY))
695 rtxdelta += tp->t_frextents_delta;
696
697 if (rtxdelta != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 msbp->msb_field = XFS_SBS_FREXTENTS;
David Chinner45c34142007-06-18 16:49:44 +1000699 msbp->msb_delta = rtxdelta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 msbp++;
701 }
702
David Chinner45c34142007-06-18 16:49:44 +1000703 /* apply remaining deltas */
704
David Chinner92821e22007-05-24 15:26:31 +1000705 if (xfs_sb_version_haslazysbcount(&mp->m_sb) ||
706 (tp->t_flags & XFS_TRANS_SB_DIRTY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (tp->t_icount_delta != 0) {
708 msbp->msb_field = XFS_SBS_ICOUNT;
David Chinner20f4ebf2007-02-10 18:36:10 +1100709 msbp->msb_delta = tp->t_icount_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 msbp++;
711 }
712 if (tp->t_ifree_delta != 0) {
713 msbp->msb_field = XFS_SBS_IFREE;
David Chinner20f4ebf2007-02-10 18:36:10 +1100714 msbp->msb_delta = tp->t_ifree_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 msbp++;
716 }
David Chinner92821e22007-05-24 15:26:31 +1000717 }
718
719 if (tp->t_flags & XFS_TRANS_SB_DIRTY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (tp->t_dblocks_delta != 0) {
721 msbp->msb_field = XFS_SBS_DBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100722 msbp->msb_delta = tp->t_dblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 msbp++;
724 }
725 if (tp->t_agcount_delta != 0) {
726 msbp->msb_field = XFS_SBS_AGCOUNT;
David Chinner20f4ebf2007-02-10 18:36:10 +1100727 msbp->msb_delta = tp->t_agcount_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 msbp++;
729 }
730 if (tp->t_imaxpct_delta != 0) {
731 msbp->msb_field = XFS_SBS_IMAX_PCT;
David Chinner20f4ebf2007-02-10 18:36:10 +1100732 msbp->msb_delta = tp->t_imaxpct_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 msbp++;
734 }
735 if (tp->t_rextsize_delta != 0) {
736 msbp->msb_field = XFS_SBS_REXTSIZE;
David Chinner20f4ebf2007-02-10 18:36:10 +1100737 msbp->msb_delta = tp->t_rextsize_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 msbp++;
739 }
740 if (tp->t_rbmblocks_delta != 0) {
741 msbp->msb_field = XFS_SBS_RBMBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100742 msbp->msb_delta = tp->t_rbmblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 msbp++;
744 }
745 if (tp->t_rblocks_delta != 0) {
746 msbp->msb_field = XFS_SBS_RBLOCKS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100747 msbp->msb_delta = tp->t_rblocks_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 msbp++;
749 }
750 if (tp->t_rextents_delta != 0) {
751 msbp->msb_field = XFS_SBS_REXTENTS;
David Chinner20f4ebf2007-02-10 18:36:10 +1100752 msbp->msb_delta = tp->t_rextents_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 msbp++;
754 }
755 if (tp->t_rextslog_delta != 0) {
756 msbp->msb_field = XFS_SBS_REXTSLOG;
David Chinner20f4ebf2007-02-10 18:36:10 +1100757 msbp->msb_delta = tp->t_rextslog_delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 msbp++;
759 }
760 }
761
762 /*
763 * If we need to change anything, do it.
764 */
765 if (msbp > msb) {
766 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb,
767 (uint)(msbp - msb), rsvd);
768 ASSERT(error == 0);
769 }
770}
771
Dave Chinner09243782010-03-08 11:28:28 +1100772/*
773 * Total up the number of log iovecs needed to commit this
774 * transaction. The transaction itself needs one for the
775 * transaction header. Ask each dirty item in turn how many
776 * it needs to get the total.
777 */
778static uint
779xfs_trans_count_vecs(
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100780 struct xfs_trans *tp)
Dave Chinner09243782010-03-08 11:28:28 +1100781{
782 int nvecs;
783 xfs_log_item_desc_t *lidp;
784
785 nvecs = 1;
786 lidp = xfs_trans_first_item(tp);
787 ASSERT(lidp != NULL);
788
789 /* In the non-debug case we need to start bailing out if we
790 * didn't find a log_item here, return zero and let trans_commit
791 * deal with it.
792 */
793 if (lidp == NULL)
794 return 0;
795
796 while (lidp != NULL) {
797 /*
798 * Skip items which aren't dirty in this transaction.
799 */
800 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
801 lidp = xfs_trans_next_item(tp, lidp);
802 continue;
803 }
804 lidp->lid_size = IOP_SIZE(lidp->lid_item);
805 nvecs += lidp->lid_size;
806 lidp = xfs_trans_next_item(tp, lidp);
807 }
808
809 return nvecs;
810}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812/*
Dave Chinner09243782010-03-08 11:28:28 +1100813 * Fill in the vector with pointers to data to be logged
814 * by this transaction. The transaction header takes
815 * the first vector, and then each dirty item takes the
816 * number of vectors it indicated it needed in xfs_trans_count_vecs().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 *
Dave Chinner09243782010-03-08 11:28:28 +1100818 * As each item fills in the entries it needs, also pin the item
819 * so that it cannot be flushed out until the log write completes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
Dave Chinner09243782010-03-08 11:28:28 +1100821static void
822xfs_trans_fill_vecs(
823 struct xfs_trans *tp,
824 struct xfs_log_iovec *log_vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Dave Chinner09243782010-03-08 11:28:28 +1100826 xfs_log_item_desc_t *lidp;
827 struct xfs_log_iovec *vecp;
828 uint nitems;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 /*
Dave Chinner09243782010-03-08 11:28:28 +1100831 * Skip over the entry for the transaction header, we'll
832 * fill that in at the end.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 */
Dave Chinner09243782010-03-08 11:28:28 +1100834 vecp = log_vector + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Dave Chinner09243782010-03-08 11:28:28 +1100836 nitems = 0;
837 lidp = xfs_trans_first_item(tp);
838 ASSERT(lidp);
839 while (lidp) {
840 /* Skip items which aren't dirty in this transaction. */
841 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
842 lidp = xfs_trans_next_item(tp, lidp);
843 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Dave Chinner09243782010-03-08 11:28:28 +1100845
846 /*
847 * The item may be marked dirty but not log anything. This can
848 * be used to get called when a transaction is committed.
849 */
850 if (lidp->lid_size)
851 nitems++;
852 IOP_FORMAT(lidp->lid_item, vecp);
853 vecp += lidp->lid_size;
854 IOP_PIN(lidp->lid_item);
855 lidp = xfs_trans_next_item(tp, lidp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 /*
Dave Chinner09243782010-03-08 11:28:28 +1100859 * Now that we've counted the number of items in this transaction, fill
860 * in the transaction header. Note that the transaction header does not
861 * have a log item.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 */
Dave Chinner09243782010-03-08 11:28:28 +1100863 tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC;
864 tp->t_header.th_type = tp->t_type;
865 tp->t_header.th_num_items = nitems;
866 log_vector->i_addr = (xfs_caddr_t)&tp->t_header;
867 log_vector->i_len = sizeof(xfs_trans_header_t);
868 log_vector->i_type = XLOG_REG_TYPE_TRANSHDR;
869}
870
871/*
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100872 * The committed item processing consists of calling the committed routine of
873 * each logged item, updating the item's position in the AIL if necessary, and
874 * unpinning each item. If the committed routine returns -1, then do nothing
875 * further with the item because it may have been freed.
876 *
877 * Since items are unlocked when they are copied to the incore log, it is
878 * possible for two transactions to be completing and manipulating the same
879 * item simultaneously. The AIL lock will protect the lsn field of each item.
880 * The value of this field can never go backwards.
881 *
882 * We unpin the items after repositioning them in the AIL, because otherwise
883 * they could be immediately flushed and we'd have to race with the flusher
884 * trying to pull the item from the AIL as we add it.
885 */
Dave Chinner71e330b2010-05-21 14:37:18 +1000886void
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100887xfs_trans_item_committed(
888 struct xfs_log_item *lip,
889 xfs_lsn_t commit_lsn,
890 int aborted)
891{
892 xfs_lsn_t item_lsn;
893 struct xfs_ail *ailp;
894
895 if (aborted)
896 lip->li_flags |= XFS_LI_ABORTED;
897 item_lsn = IOP_COMMITTED(lip, commit_lsn);
898
899 /* If the committed routine returns -1, item has been freed. */
900 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0)
901 return;
902
903 /*
904 * If the returned lsn is greater than what it contained before, update
905 * the location of the item in the AIL. If it is not, then do nothing.
906 * Items can never move backwards in the AIL.
907 *
908 * While the new lsn should usually be greater, it is possible that a
909 * later transaction completing simultaneously with an earlier one
910 * using the same item could complete first with a higher lsn. This
911 * would cause the earlier transaction to fail the test below.
912 */
913 ailp = lip->li_ailp;
914 spin_lock(&ailp->xa_lock);
915 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) {
916 /*
917 * This will set the item's lsn to item_lsn and update the
918 * position of the item in the AIL.
919 *
920 * xfs_trans_ail_update() drops the AIL lock.
921 */
922 xfs_trans_ail_update(ailp, lip, item_lsn);
923 } else {
924 spin_unlock(&ailp->xa_lock);
925 }
926
927 /*
928 * Now that we've repositioned the item in the AIL, unpin it so it can
929 * be flushed. Pass information about buffer stale state down from the
930 * log item flags, if anyone else stales the buffer we do not want to
931 * pay any attention to it.
932 */
933 IOP_UNPIN(lip);
934}
935
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100936/*
937 * This is typically called by the LM when a transaction has been fully
938 * committed to disk. It needs to unpin the items which have
939 * been logged by the transaction and update their positions
940 * in the AIL if necessary.
941 *
942 * This also gets called when the transactions didn't get written out
943 * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then.
944 */
945STATIC void
946xfs_trans_committed(
947 struct xfs_trans *tp,
948 int abortflag)
949{
950 xfs_log_item_desc_t *lidp;
951 xfs_log_item_chunk_t *licp;
952 xfs_log_item_chunk_t *next_licp;
953
954 /* Call the transaction's completion callback if there is one. */
955 if (tp->t_callback != NULL)
956 tp->t_callback(tp, tp->t_callarg);
957
958 for (lidp = xfs_trans_first_item(tp);
959 lidp != NULL;
960 lidp = xfs_trans_next_item(tp, lidp)) {
961 xfs_trans_item_committed(lidp->lid_item, tp->t_lsn, abortflag);
962 }
963
964 /* free the item chunks, ignoring the embedded chunk */
965 for (licp = tp->t_items.lic_next; licp != NULL; licp = next_licp) {
966 next_licp = licp->lic_next;
967 kmem_free(licp);
968 }
969
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100970 xfs_trans_free(tp);
971}
972
973/*
974 * Called from the trans_commit code when we notice that
975 * the filesystem is in the middle of a forced shutdown.
976 */
977STATIC void
978xfs_trans_uncommit(
979 struct xfs_trans *tp,
980 uint flags)
981{
982 xfs_log_item_desc_t *lidp;
983
984 for (lidp = xfs_trans_first_item(tp);
985 lidp != NULL;
986 lidp = xfs_trans_next_item(tp, lidp)) {
987 /*
988 * Unpin all but those that aren't dirty.
989 */
990 if (lidp->lid_flags & XFS_LID_DIRTY)
991 IOP_UNPIN_REMOVE(lidp->lid_item, tp);
992 }
993
994 xfs_trans_unreserve_and_mod_sb(tp);
995 xfs_trans_unreserve_and_mod_dquots(tp);
996
Dave Chinner71e330b2010-05-21 14:37:18 +1000997 xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
Dave Chinnerb1c1b5b2010-03-23 10:11:05 +1100998 xfs_trans_free(tp);
999}
1000
1001/*
Dave Chinner09243782010-03-08 11:28:28 +11001002 * Format the transaction direct to the iclog. This isolates the physical
1003 * transaction commit operation from the logical operation and hence allows
1004 * other methods to be introduced without affecting the existing commit path.
1005 */
1006static int
1007xfs_trans_commit_iclog(
1008 struct xfs_mount *mp,
1009 struct xfs_trans *tp,
1010 xfs_lsn_t *commit_lsn,
1011 int flags)
1012{
1013 int shutdown;
1014 int error;
1015 int log_flags = 0;
1016 struct xlog_in_core *commit_iclog;
1017#define XFS_TRANS_LOGVEC_COUNT 16
1018 struct xfs_log_iovec log_vector_fast[XFS_TRANS_LOGVEC_COUNT];
1019 struct xfs_log_iovec *log_vector;
1020 uint nvec;
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 /*
1024 * Ask each log item how many log_vector entries it will
1025 * need so we can figure out how many to allocate.
1026 * Try to avoid the kmem_alloc() call in the common case
1027 * by using a vector from the stack when it fits.
1028 */
1029 nvec = xfs_trans_count_vecs(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (nvec == 0) {
Dave Chinner09243782010-03-08 11:28:28 +11001031 return ENOMEM; /* triggers a shutdown! */
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001032 } else if (nvec <= XFS_TRANS_LOGVEC_COUNT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 log_vector = log_vector_fast;
1034 } else {
1035 log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec *
1036 sizeof(xfs_log_iovec_t),
1037 KM_SLEEP);
1038 }
1039
1040 /*
1041 * Fill in the log_vector and pin the logged items, and
1042 * then write the transaction to the log.
1043 */
1044 xfs_trans_fill_vecs(tp, log_vector);
1045
Dave Chinner09243782010-03-08 11:28:28 +11001046 if (flags & XFS_TRANS_RELEASE_LOG_RES)
1047 log_flags = XFS_LOG_REL_PERM_RESERV;
1048
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001049 error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, &(tp->t_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /*
Nathan Scottcfcbbbd2005-11-02 15:12:04 +11001052 * The transaction is committed incore here, and can go out to disk
1053 * at any time after this call. However, all the items associated
1054 * with the transaction are still locked and pinned in memory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 */
Dave Chinner09243782010-03-08 11:28:28 +11001056 *commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Dave Chinner09243782010-03-08 11:28:28 +11001058 tp->t_commit_lsn = *commit_lsn;
Dave Chinnered3b4d62010-05-21 12:07:08 +10001059 trace_xfs_trans_commit_lsn(tp);
1060
Dave Chinner09243782010-03-08 11:28:28 +11001061 if (nvec > XFS_TRANS_LOGVEC_COUNT)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10001062 kmem_free(log_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 /*
1065 * If we got a log write error. Unpin the logitems that we
1066 * had pinned, clean up, free trans structure, and return error.
1067 */
Dave Chinner09243782010-03-08 11:28:28 +11001068 if (error || *commit_lsn == -1) {
Nathan Scott59c1b082006-06-09 14:59:13 +10001069 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT);
1071 return XFS_ERROR(EIO);
1072 }
1073
1074 /*
1075 * Once the transaction has committed, unused
1076 * reservations need to be released and changes to
1077 * the superblock need to be reflected in the in-core
1078 * version. Do that now.
1079 */
1080 xfs_trans_unreserve_and_mod_sb(tp);
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /*
1083 * Tell the LM to call the transaction completion routine
1084 * when the log write with LSN commit_lsn completes (e.g.
1085 * when the transaction commit really hits the on-disk log).
1086 * After this call we cannot reference tp, because the call
1087 * can happen at any time and the call will free the transaction
1088 * structure pointed to by tp. The only case where we call
1089 * the completion routine (xfs_trans_committed) directly is
1090 * if the log is turned off on a debug kernel or we're
1091 * running in simulation mode (the log is explicitly turned
1092 * off).
1093 */
1094 tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed;
1095 tp->t_logcb.cb_arg = tp;
1096
1097 /*
1098 * We need to pass the iclog buffer which was used for the
1099 * transaction commit record into this function, and attach
1100 * the callback to it. The callback must be attached before
1101 * the items are unlocked to avoid racing with other threads
1102 * waiting for an item to unlock.
1103 */
1104 shutdown = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb));
1105
1106 /*
1107 * Mark this thread as no longer being in a transaction
1108 */
Nathan Scott59c1b082006-06-09 14:59:13 +10001109 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 /*
1112 * Once all the items of the transaction have been copied
1113 * to the in core log and the callback is attached, the
1114 * items can be unlocked.
1115 *
1116 * This will free descriptors pointing to items which were
1117 * not logged since there is nothing more to do with them.
1118 * For items which were logged, we will keep pointers to them
1119 * so they can be unpinned after the transaction commits to disk.
1120 * This will also stamp each modified meta-data item with
1121 * the commit lsn of this transaction for dependency tracking
1122 * purposes.
1123 */
Dave Chinner09243782010-03-08 11:28:28 +11001124 xfs_trans_unlock_items(tp, *commit_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 /*
1127 * If we detected a log error earlier, finish committing
1128 * the transaction now (unpin log items, etc).
1129 *
1130 * Order is critical here, to avoid using the transaction
1131 * pointer after its been freed (by xfs_trans_committed
1132 * either here now, or as a callback). We cannot do this
1133 * step inside xfs_log_notify as was done earlier because
1134 * of this issue.
1135 */
1136 if (shutdown)
1137 xfs_trans_committed(tp, XFS_LI_ABORTED);
1138
1139 /*
1140 * Now that the xfs_trans_committed callback has been attached,
1141 * and the items are released we can finally allow the iclog to
1142 * go to disk.
1143 */
Dave Chinner09243782010-03-08 11:28:28 +11001144 return xfs_log_release_iclog(mp, commit_iclog);
1145}
1146
Dave Chinner71e330b2010-05-21 14:37:18 +10001147/*
1148 * Walk the log items and allocate log vector structures for
1149 * each item large enough to fit all the vectors they require.
1150 * Note that this format differs from the old log vector format in
1151 * that there is no transaction header in these log vectors.
1152 */
1153STATIC struct xfs_log_vec *
1154xfs_trans_alloc_log_vecs(
1155 xfs_trans_t *tp)
1156{
1157 xfs_log_item_desc_t *lidp;
1158 struct xfs_log_vec *lv = NULL;
1159 struct xfs_log_vec *ret_lv = NULL;
1160
1161 lidp = xfs_trans_first_item(tp);
1162
1163 /* Bail out if we didn't find a log item. */
1164 if (!lidp) {
1165 ASSERT(0);
1166 return NULL;
1167 }
1168
1169 while (lidp != NULL) {
1170 struct xfs_log_vec *new_lv;
1171
1172 /* Skip items which aren't dirty in this transaction. */
1173 if (!(lidp->lid_flags & XFS_LID_DIRTY)) {
1174 lidp = xfs_trans_next_item(tp, lidp);
1175 continue;
1176 }
1177
1178 /* Skip items that do not have any vectors for writing */
1179 lidp->lid_size = IOP_SIZE(lidp->lid_item);
1180 if (!lidp->lid_size) {
1181 lidp = xfs_trans_next_item(tp, lidp);
1182 continue;
1183 }
1184
1185 new_lv = kmem_zalloc(sizeof(*new_lv) +
1186 lidp->lid_size * sizeof(struct xfs_log_iovec),
1187 KM_SLEEP);
1188
1189 /* The allocated iovec region lies beyond the log vector. */
1190 new_lv->lv_iovecp = (struct xfs_log_iovec *)&new_lv[1];
1191 new_lv->lv_niovecs = lidp->lid_size;
1192 new_lv->lv_item = lidp->lid_item;
1193 if (!ret_lv)
1194 ret_lv = new_lv;
1195 else
1196 lv->lv_next = new_lv;
1197 lv = new_lv;
1198 lidp = xfs_trans_next_item(tp, lidp);
1199 }
1200
1201 return ret_lv;
1202}
1203
1204static int
1205xfs_trans_commit_cil(
1206 struct xfs_mount *mp,
1207 struct xfs_trans *tp,
1208 xfs_lsn_t *commit_lsn,
1209 int flags)
1210{
1211 struct xfs_log_vec *log_vector;
1212 int error;
1213
1214 /*
1215 * Get each log item to allocate a vector structure for
1216 * the log item to to pass to the log write code. The
1217 * CIL commit code will format the vector and save it away.
1218 */
1219 log_vector = xfs_trans_alloc_log_vecs(tp);
1220 if (!log_vector)
1221 return ENOMEM;
1222
1223 error = xfs_log_commit_cil(mp, tp, log_vector, commit_lsn, flags);
1224 if (error)
1225 return error;
1226
1227 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
1228
1229 /* xfs_trans_free_items() unlocks them first */
1230 xfs_trans_free_items(tp, *commit_lsn, 0);
1231 xfs_trans_free(tp);
1232 return 0;
1233}
Dave Chinner09243782010-03-08 11:28:28 +11001234
1235/*
1236 * xfs_trans_commit
1237 *
1238 * Commit the given transaction to the log a/synchronously.
1239 *
1240 * XFS disk error handling mechanism is not based on a typical
1241 * transaction abort mechanism. Logically after the filesystem
1242 * gets marked 'SHUTDOWN', we can't let any new transactions
1243 * be durable - ie. committed to disk - because some metadata might
1244 * be inconsistent. In such cases, this returns an error, and the
1245 * caller may assume that all locked objects joined to the transaction
1246 * have already been unlocked as if the commit had succeeded.
1247 * Do not reference the transaction structure after this call.
1248 */
Dave Chinner09243782010-03-08 11:28:28 +11001249int
1250_xfs_trans_commit(
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001251 struct xfs_trans *tp,
1252 uint flags,
1253 int *log_flushed)
Dave Chinner09243782010-03-08 11:28:28 +11001254{
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001255 struct xfs_mount *mp = tp->t_mountp;
Dave Chinner09243782010-03-08 11:28:28 +11001256 xfs_lsn_t commit_lsn = -1;
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001257 int error = 0;
Dave Chinner09243782010-03-08 11:28:28 +11001258 int log_flags = 0;
1259 int sync = tp->t_flags & XFS_TRANS_SYNC;
Dave Chinner09243782010-03-08 11:28:28 +11001260
1261 /*
1262 * Determine whether this commit is releasing a permanent
1263 * log reservation or not.
1264 */
1265 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1266 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1267 log_flags = XFS_LOG_REL_PERM_RESERV;
1268 }
1269
1270 /*
1271 * If there is nothing to be logged by the transaction,
1272 * then unlock all of the items associated with the
1273 * transaction and free the transaction structure.
1274 * Also make sure to return any reserved blocks to
1275 * the free pool.
1276 */
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001277 if (!(tp->t_flags & XFS_TRANS_DIRTY))
1278 goto out_unreserve;
1279
1280 if (XFS_FORCED_SHUTDOWN(mp)) {
1281 error = XFS_ERROR(EIO);
1282 goto out_unreserve;
Dave Chinner09243782010-03-08 11:28:28 +11001283 }
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001284
Dave Chinner09243782010-03-08 11:28:28 +11001285 ASSERT(tp->t_ticket != NULL);
1286
1287 /*
1288 * If we need to update the superblock, then do it now.
1289 */
1290 if (tp->t_flags & XFS_TRANS_SB_DIRTY)
1291 xfs_trans_apply_sb_deltas(tp);
1292 xfs_trans_apply_dquot_deltas(tp);
1293
Dave Chinner71e330b2010-05-21 14:37:18 +10001294 if (mp->m_flags & XFS_MOUNT_DELAYLOG)
1295 error = xfs_trans_commit_cil(mp, tp, &commit_lsn, flags);
1296 else
1297 error = xfs_trans_commit_iclog(mp, tp, &commit_lsn, flags);
1298
Dave Chinner09243782010-03-08 11:28:28 +11001299 if (error == ENOMEM) {
1300 xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001301 error = XFS_ERROR(EIO);
1302 goto out_unreserve;
Dave Chinner09243782010-03-08 11:28:28 +11001303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 /*
1306 * If the transaction needs to be synchronous, then force the
1307 * log out now and wait for it.
1308 */
1309 if (sync) {
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001310 if (!error) {
Christoph Hellwiga14a3482010-01-19 09:56:46 +00001311 error = _xfs_log_force_lsn(mp, commit_lsn,
1312 XFS_LOG_SYNC, log_flushed);
Christoph Hellwigf538d4d2005-11-02 10:26:59 +11001313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 XFS_STATS_INC(xs_trans_sync);
1315 } else {
1316 XFS_STATS_INC(xs_trans_async);
1317 }
1318
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001319 return error;
1320
1321out_unreserve:
1322 xfs_trans_unreserve_and_mod_sb(tp);
1323
1324 /*
1325 * It is indeed possible for the transaction to be not dirty but
1326 * the dqinfo portion to be. All that means is that we have some
1327 * (non-persistent) quota reservations that need to be unreserved.
1328 */
1329 xfs_trans_unreserve_and_mod_dquots(tp);
1330 if (tp->t_ticket) {
1331 commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
1332 if (commit_lsn == -1 && !error)
1333 error = XFS_ERROR(EIO);
1334 }
1335 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Dave Chinner71e330b2010-05-21 14:37:18 +10001336 xfs_trans_free_items(tp, NULLCOMMITLSN, error ? XFS_TRANS_ABORT : 0);
Christoph Hellwiga3ccd2c2010-03-15 12:52:49 +11001337 xfs_trans_free(tp);
1338
1339 XFS_STATS_INC(xs_trans_empty);
1340 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 * Unlock all of the transaction's items and free the transaction.
1345 * The transaction must not have modified any of its items, because
1346 * there is no way to restore them to their previous state.
1347 *
1348 * If the transaction has made a log reservation, make sure to release
1349 * it as well.
1350 */
1351void
1352xfs_trans_cancel(
1353 xfs_trans_t *tp,
1354 int flags)
1355{
1356 int log_flags;
1357#ifdef DEBUG
1358 xfs_log_item_chunk_t *licp;
1359 xfs_log_item_desc_t *lidp;
1360 xfs_log_item_t *lip;
1361 int i;
1362#endif
Ryan Hankins0733af22006-01-11 15:36:44 +11001363 xfs_mount_t *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 /*
1366 * See if the caller is being too lazy to figure out if
1367 * the transaction really needs an abort.
1368 */
1369 if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY))
1370 flags &= ~XFS_TRANS_ABORT;
1371 /*
1372 * See if the caller is relying on us to shut down the
1373 * filesystem. This happens in paths where we detect
1374 * corruption and decide to give up.
1375 */
Nathan Scott60a204f2006-01-11 15:37:00 +11001376 if ((tp->t_flags & XFS_TRANS_DIRTY) && !XFS_FORCED_SHUTDOWN(mp)) {
Ryan Hankins0733af22006-01-11 15:36:44 +11001377 XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp);
Nathan Scott7d04a332006-06-09 14:58:38 +10001378 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
Nathan Scott60a204f2006-01-11 15:37:00 +11001379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380#ifdef DEBUG
1381 if (!(flags & XFS_TRANS_ABORT)) {
1382 licp = &(tp->t_items);
1383 while (licp != NULL) {
1384 lidp = licp->lic_descs;
1385 for (i = 0; i < licp->lic_unused; i++, lidp++) {
Eric Sandeen39dab9d2008-08-13 16:10:52 +10001386 if (xfs_lic_isfree(licp, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 continue;
1388 }
1389
1390 lip = lidp->lid_item;
Ryan Hankins0733af22006-01-11 15:36:44 +11001391 if (!XFS_FORCED_SHUTDOWN(mp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 ASSERT(!(lip->li_type == XFS_LI_EFD));
1393 }
1394 licp = licp->lic_next;
1395 }
1396 }
1397#endif
1398 xfs_trans_unreserve_and_mod_sb(tp);
Christoph Hellwig7d095252009-06-08 15:33:32 +02001399 xfs_trans_unreserve_and_mod_dquots(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 if (tp->t_ticket) {
1402 if (flags & XFS_TRANS_RELEASE_LOG_RES) {
1403 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
1404 log_flags = XFS_LOG_REL_PERM_RESERV;
1405 } else {
1406 log_flags = 0;
1407 }
Ryan Hankins0733af22006-01-11 15:36:44 +11001408 xfs_log_done(mp, tp->t_ticket, NULL, log_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410
1411 /* mark this thread as no longer being in a transaction */
Nathan Scott59c1b082006-06-09 14:59:13 +10001412 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Dave Chinner71e330b2010-05-21 14:37:18 +10001414 xfs_trans_free_items(tp, NULLCOMMITLSN, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 xfs_trans_free(tp);
1416}
1417
Niv Sardi322ff6b2008-08-13 16:05:49 +10001418/*
1419 * Roll from one trans in the sequence of PERMANENT transactions to
1420 * the next: permanent transactions are only flushed out when
1421 * committed with XFS_TRANS_RELEASE_LOG_RES, but we still want as soon
1422 * as possible to let chunks of it go to the log. So we commit the
1423 * chunk we've been working on and get a new transaction to continue.
1424 */
1425int
1426xfs_trans_roll(
1427 struct xfs_trans **tpp,
1428 struct xfs_inode *dp)
1429{
1430 struct xfs_trans *trans;
1431 unsigned int logres, count;
1432 int error;
1433
1434 /*
1435 * Ensure that the inode is always logged.
1436 */
1437 trans = *tpp;
1438 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
1439
1440 /*
1441 * Copy the critical parameters from one trans to the next.
1442 */
1443 logres = trans->t_log_res;
1444 count = trans->t_log_count;
1445 *tpp = xfs_trans_dup(trans);
1446
1447 /*
1448 * Commit the current transaction.
1449 * If this commit failed, then it'd just unlock those items that
1450 * are not marked ihold. That also means that a filesystem shutdown
1451 * is in progress. The caller takes the responsibility to cancel
1452 * the duplicate transaction that gets returned.
1453 */
1454 error = xfs_trans_commit(trans, 0);
1455 if (error)
1456 return (error);
1457
1458 trans = *tpp;
1459
1460 /*
Dave Chinnercc09c0d2008-11-17 17:37:10 +11001461 * transaction commit worked ok so we can drop the extra ticket
1462 * reference that we gained in xfs_trans_dup()
1463 */
1464 xfs_log_ticket_put(trans->t_ticket);
1465
1466
1467 /*
Niv Sardi322ff6b2008-08-13 16:05:49 +10001468 * Reserve space in the log for th next transaction.
1469 * This also pushes items in the "AIL", the list of logged items,
1470 * out to disk if they are taking up space at the tail of the log
1471 * that we want to use. This requires that either nothing be locked
1472 * across this call, or that anything that is locked be logged in
1473 * the prior and the next transactions.
1474 */
1475 error = xfs_trans_reserve(trans, 0, logres, 0,
1476 XFS_TRANS_PERM_LOG_RES, count);
1477 /*
1478 * Ensure that the inode is in the new transaction and locked.
1479 */
1480 if (error)
1481 return error;
1482
1483 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
1484 xfs_trans_ihold(trans, dp);
1485 return 0;
1486}