| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. |
| 4 |
* Copyright (C) 2010 Red Hat, Inc. |
| 5 |
* All Rights Reserved. |
| 6 |
*/ |
| 7 |
#include "xfs_platform.h" |
| 8 |
#include "xfs_fs.h" |
| 9 |
#include "xfs_shared.h" |
| 10 |
#include "xfs_format.h" |
| 11 |
#include "xfs_log_format.h" |
| 12 |
#include "xfs_trans_resv.h" |
| 13 |
#include "xfs_mount.h" |
| 14 |
#include "xfs_extent_busy.h" |
| 15 |
#include "xfs_quota.h" |
| 16 |
#include "xfs_trans.h" |
| 17 |
#include "xfs_trans_priv.h" |
| 18 |
#include "xfs_log.h" |
| 19 |
#include "xfs_log_priv.h" |
| 20 |
#include "xfs_trace.h" |
| 21 |
#include "xfs_error.h" |
| 22 |
#include "xfs_defer.h" |
| 23 |
#include "xfs_inode.h" |
| 24 |
#include "xfs_dquot_item.h" |
| 25 |
#include "xfs_dquot.h" |
| 26 |
#include "xfs_icache.h" |
| 27 |
#include "xfs_rtbitmap.h" |
| 28 |
#include "xfs_rtgroup.h" |
| 29 |
#include "xfs_sb.h" |
| 30 |
|
| 31 |
struct kmem_cache *xfs_trans_cache; |
| 32 |
|
| 33 |
#if defined(CONFIG_TRACEPOINTS) |
| 34 |
static void |
| 35 |
xfs_trans_trace_reservations( |
| 36 |
struct xfs_mount *mp) |
| 37 |
{ |
| 38 |
struct xfs_trans_res *res; |
| 39 |
struct xfs_trans_res *end_res; |
| 40 |
int i; |
| 41 |
|
| 42 |
res = (struct xfs_trans_res *)M_RES(mp); |
| 43 |
end_res = (struct xfs_trans_res *)(M_RES(mp) + 1); |
| 44 |
for (i = 0; res < end_res; i++, res++) |
| 45 |
trace_xfs_trans_resv_calc(mp, i, res); |
| 46 |
} |
| 47 |
#else |
| 48 |
# define xfs_trans_trace_reservations(mp) |
| 49 |
#endif |
| 50 |
|
| 51 |
/* |
| 52 |
* Initialize the precomputed transaction reservation values |
| 53 |
* in the mount structure. |
| 54 |
*/ |
| 55 |
void |
| 56 |
xfs_trans_init( |
| 57 |
struct xfs_mount *mp) |
| 58 |
{ |
| 59 |
xfs_trans_resv_calc(mp, M_RES(mp)); |
| 60 |
xfs_trans_trace_reservations(mp); |
| 61 |
} |
| 62 |
|
| 63 |
/* |
| 64 |
* Free the transaction structure. If there is more clean up |
| 65 |
* to do when the structure is freed, add it here. |
| 66 |
*/ |
| 67 |
STATIC void |
| 68 |
xfs_trans_free( |
| 69 |
struct xfs_trans *tp) |
| 70 |
{ |
| 71 |
xfs_extent_busy_sort(&tp->t_busy); |
| 72 |
xfs_extent_busy_clear(&tp->t_busy, false); |
| 73 |
|
| 74 |
trace_xfs_trans_free(tp, _RET_IP_); |
| 75 |
xfs_trans_clear_context(tp); |
| 76 |
if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT)) |
| 77 |
sb_end_intwrite(tp->t_mountp->m_super); |
| 78 |
xfs_trans_free_dqinfo(tp); |
| 79 |
kmem_cache_free(xfs_trans_cache, tp); |
| 80 |
} |
| 81 |
|
| 82 |
/* |
| 83 |
* This is called to create a new transaction which will share the |
| 84 |
* permanent log reservation of the given transaction. The remaining |
| 85 |
* unused block and rt extent reservations are also inherited. This |
| 86 |
* implies that the original transaction is no longer allowed to allocate |
| 87 |
* blocks. Locks and log items, however, are no inherited. They must |
| 88 |
* be added to the new transaction explicitly. |
| 89 |
*/ |
| 90 |
STATIC struct xfs_trans * |
| 91 |
xfs_trans_dup( |
| 92 |
struct xfs_trans *tp) |
| 93 |
{ |
| 94 |
struct xfs_trans *ntp; |
| 95 |
|
| 96 |
trace_xfs_trans_dup(tp, _RET_IP_); |
| 97 |
|
| 98 |
ntp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL); |
| 99 |
|
| 100 |
/* |
| 101 |
* Initialize the new transaction structure. |
| 102 |
*/ |
| 103 |
ntp->t_mountp = tp->t_mountp; |
| 104 |
INIT_LIST_HEAD(&ntp->t_items); |
| 105 |
INIT_LIST_HEAD(&ntp->t_busy); |
| 106 |
INIT_LIST_HEAD(&ntp->t_dfops); |
| 107 |
ntp->t_highest_agno = NULLAGNUMBER; |
| 108 |
|
| 109 |
ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); |
| 110 |
ASSERT(tp->t_ticket != NULL); |
| 111 |
|
| 112 |
ntp->t_flags = XFS_TRANS_PERM_LOG_RES | |
| 113 |
(tp->t_flags & XFS_TRANS_RESERVE) | |
| 114 |
(tp->t_flags & XFS_TRANS_NO_WRITECOUNT) | |
| 115 |
(tp->t_flags & XFS_TRANS_RES_FDBLKS); |
| 116 |
/* We gave our writer reference to the new transaction */ |
| 117 |
tp->t_flags |= XFS_TRANS_NO_WRITECOUNT; |
| 118 |
ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket); |
| 119 |
|
| 120 |
ASSERT(tp->t_blk_res >= tp->t_blk_res_used); |
| 121 |
ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used; |
| 122 |
tp->t_blk_res = tp->t_blk_res_used; |
| 123 |
|
| 124 |
ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used; |
| 125 |
tp->t_rtx_res = tp->t_rtx_res_used; |
| 126 |
|
| 127 |
/* move deferred ops over to the new tp */ |
| 128 |
xfs_defer_move(ntp, tp); |
| 129 |
|
| 130 |
xfs_trans_dup_dqinfo(tp, ntp); |
| 131 |
return ntp; |
| 132 |
} |
| 133 |
|
| 134 |
/* |
| 135 |
* This is called to reserve free disk blocks and log space for the given |
| 136 |
* transaction before allocating any resources within the transaction. |
| 137 |
* |
| 138 |
* This will return ENOSPC if there are not enough blocks available. |
| 139 |
* It will sleep waiting for available log space. |
| 140 |
* |
| 141 |
* This does not do quota reservations. That typically is done by the caller |
| 142 |
* afterwards. |
| 143 |
*/ |
| 144 |
static int |
| 145 |
xfs_trans_reserve( |
| 146 |
struct xfs_trans *tp, |
| 147 |
struct xfs_trans_res *resp, |
| 148 |
uint blocks, |
| 149 |
uint rtextents) |
| 150 |
{ |
| 151 |
struct xfs_mount *mp = tp->t_mountp; |
| 152 |
int error = 0; |
| 153 |
bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0; |
| 154 |
|
| 155 |
ASSERT(resp->tr_logres > 0); |
| 156 |
|
| 157 |
/* |
| 158 |
* Attempt to reserve the needed disk blocks by decrementing the number |
| 159 |
* needed from the number available. This will fail if the count would |
| 160 |
* go below zero. |
| 161 |
*/ |
| 162 |
if (blocks > 0) { |
| 163 |
error = xfs_dec_fdblocks(mp, blocks, rsvd); |
| 164 |
if (error != 0) |
| 165 |
return -ENOSPC; |
| 166 |
tp->t_blk_res += blocks; |
| 167 |
} |
| 168 |
|
| 169 |
/* |
| 170 |
* Reserve the log space needed for this transaction. |
| 171 |
*/ |
| 172 |
if (resp->tr_logflags & XFS_TRANS_PERM_LOG_RES) |
| 173 |
tp->t_flags |= XFS_TRANS_PERM_LOG_RES; |
| 174 |
error = xfs_log_reserve(mp, resp->tr_logres, resp->tr_logcount, |
| 175 |
&tp->t_ticket, (tp->t_flags & XFS_TRANS_PERM_LOG_RES)); |
| 176 |
if (error) |
| 177 |
goto undo_blocks; |
| 178 |
|
| 179 |
tp->t_log_res = resp->tr_logres; |
| 180 |
tp->t_log_count = resp->tr_logcount; |
| 181 |
|
| 182 |
/* |
| 183 |
* Attempt to reserve the needed realtime extents by decrementing the |
| 184 |
* number needed from the number available. This will fail if the |
| 185 |
* count would go below zero. |
| 186 |
*/ |
| 187 |
if (rtextents > 0) { |
| 188 |
error = xfs_dec_frextents(mp, rtextents); |
| 189 |
if (error) { |
| 190 |
error = -ENOSPC; |
| 191 |
goto undo_log; |
| 192 |
} |
| 193 |
tp->t_rtx_res += rtextents; |
| 194 |
} |
| 195 |
|
| 196 |
return 0; |
| 197 |
|
| 198 |
undo_log: |
| 199 |
xfs_log_ticket_ungrant(mp->m_log, tp->t_ticket); |
| 200 |
tp->t_ticket = NULL; |
| 201 |
tp->t_log_res = 0; |
| 202 |
tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES; |
| 203 |
undo_blocks: |
| 204 |
if (blocks > 0) { |
| 205 |
xfs_add_fdblocks(mp, blocks); |
| 206 |
tp->t_blk_res = 0; |
| 207 |
} |
| 208 |
return error; |
| 209 |
} |
| 210 |
|
| 211 |
static struct xfs_trans * |
| 212 |
__xfs_trans_alloc( |
| 213 |
struct xfs_mount *mp, |
| 214 |
uint flags) |
| 215 |
{ |
| 216 |
struct xfs_trans *tp; |
| 217 |
|
| 218 |
ASSERT(!(flags & XFS_TRANS_RES_FDBLKS) || xfs_has_lazysbcount(mp)); |
| 219 |
|
| 220 |
tp = kmem_cache_zalloc(xfs_trans_cache, GFP_KERNEL | __GFP_NOFAIL); |
| 221 |
if (!(flags & XFS_TRANS_NO_WRITECOUNT)) |
| 222 |
sb_start_intwrite(mp->m_super); |
| 223 |
xfs_trans_set_context(tp); |
| 224 |
tp->t_flags = flags; |
| 225 |
tp->t_mountp = mp; |
| 226 |
INIT_LIST_HEAD(&tp->t_items); |
| 227 |
INIT_LIST_HEAD(&tp->t_busy); |
| 228 |
INIT_LIST_HEAD(&tp->t_dfops); |
| 229 |
tp->t_highest_agno = NULLAGNUMBER; |
| 230 |
return tp; |
| 231 |
} |
| 232 |
|
| 233 |
int |
| 234 |
xfs_trans_alloc( |
| 235 |
struct xfs_mount *mp, |
| 236 |
struct xfs_trans_res *resp, |
| 237 |
uint blocks, |
| 238 |
uint rtextents, |
| 239 |
uint flags, |
| 240 |
struct xfs_trans **tpp) |
| 241 |
{ |
| 242 |
struct xfs_trans *tp; |
| 243 |
bool want_retry = true; |
| 244 |
int error; |
| 245 |
|
| 246 |
ASSERT(resp->tr_logres > 0); |
| 247 |
|
| 248 |
/* |
| 249 |
* Allocate the handle before we do our freeze accounting and setting up |
| 250 |
* GFP_NOFS allocation context so that we avoid lockdep false positives |
| 251 |
* by doing GFP_KERNEL allocations inside sb_start_intwrite(). |
| 252 |
*/ |
| 253 |
retry: |
| 254 |
tp = __xfs_trans_alloc(mp, flags); |
| 255 |
WARN_ON(mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE); |
| 256 |
error = xfs_trans_reserve(tp, resp, blocks, rtextents); |
| 257 |
if (error == -ENOSPC && want_retry) { |
| 258 |
xfs_trans_cancel(tp); |
| 259 |
|
| 260 |
/* |
| 261 |
* We weren't able to reserve enough space for the transaction. |
| 262 |
* Flush the other speculative space allocations to free space. |
| 263 |
* Do not perform a synchronous scan because callers can hold |
| 264 |
* other locks. |
| 265 |
*/ |
| 266 |
error = xfs_blockgc_flush_all(mp); |
| 267 |
if (error) |
| 268 |
return error; |
| 269 |
want_retry = false; |
| 270 |
goto retry; |
| 271 |
} |
| 272 |
if (error) { |
| 273 |
xfs_trans_cancel(tp); |
| 274 |
return error; |
| 275 |
} |
| 276 |
|
| 277 |
trace_xfs_trans_alloc(tp, _RET_IP_); |
| 278 |
|
| 279 |
*tpp = tp; |
| 280 |
return 0; |
| 281 |
} |
| 282 |
|
| 283 |
/* |
| 284 |
* Create an empty transaction with no reservation. This is a defensive |
| 285 |
* mechanism for routines that query metadata without actually modifying them -- |
| 286 |
* if the metadata being queried is somehow cross-linked (think a btree block |
| 287 |
* pointer that points higher in the tree), we risk deadlock. However, blocks |
| 288 |
* grabbed as part of a transaction can be re-grabbed. The verifiers will |
| 289 |
* notice the corrupt block and the operation will fail back to userspace |
| 290 |
* without deadlocking. |
| 291 |
* |
| 292 |
* Note the zero-length reservation; this transaction MUST be cancelled without |
| 293 |
* any dirty data. |
| 294 |
* |
| 295 |
* Callers should obtain freeze protection to avoid a conflict with fs freezing |
| 296 |
* where we can be grabbing buffers at the same time that freeze is trying to |
| 297 |
* drain the buffer LRU list. |
| 298 |
*/ |
| 299 |
struct xfs_trans * |
| 300 |
xfs_trans_alloc_empty( |
| 301 |
struct xfs_mount *mp) |
| 302 |
{ |
| 303 |
return __xfs_trans_alloc(mp, XFS_TRANS_NO_WRITECOUNT); |
| 304 |
} |
| 305 |
|
| 306 |
/* |
| 307 |
* Record the indicated change to the given field for application |
| 308 |
* to the file system's superblock when the transaction commits. |
| 309 |
* For now, just store the change in the transaction structure. |
| 310 |
* |
| 311 |
* Mark the transaction structure to indicate that the superblock |
| 312 |
* needs to be updated before committing. |
| 313 |
* |
| 314 |
* Because we may not be keeping track of allocated/free inodes and |
| 315 |
* used filesystem blocks in the superblock, we do not mark the |
| 316 |
* superblock dirty in this transaction if we modify these fields. |
| 317 |
* We still need to update the transaction deltas so that they get |
| 318 |
* applied to the incore superblock, but we don't want them to |
| 319 |
* cause the superblock to get locked and logged if these are the |
| 320 |
* only fields in the superblock that the transaction modifies. |
| 321 |
*/ |
| 322 |
void |
| 323 |
xfs_trans_mod_sb( |
| 324 |
xfs_trans_t *tp, |
| 325 |
uint field, |
| 326 |
int64_t delta) |
| 327 |
{ |
| 328 |
uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY); |
| 329 |
xfs_mount_t *mp = tp->t_mountp; |
| 330 |
|
| 331 |
switch (field) { |
| 332 |
case XFS_TRANS_SB_ICOUNT: |
| 333 |
tp->t_icount_delta += delta; |
| 334 |
if (xfs_has_lazysbcount(mp)) |
| 335 |
flags &= ~XFS_TRANS_SB_DIRTY; |
| 336 |
break; |
| 337 |
case XFS_TRANS_SB_IFREE: |
| 338 |
tp->t_ifree_delta += delta; |
| 339 |
if (xfs_has_lazysbcount(mp)) |
| 340 |
flags &= ~XFS_TRANS_SB_DIRTY; |
| 341 |
break; |
| 342 |
case XFS_TRANS_SB_FDBLOCKS: |
| 343 |
/* |
| 344 |
* Track the number of blocks allocated in the transaction. |
| 345 |
* Make sure it does not exceed the number reserved. If so, |
| 346 |
* shutdown as this can lead to accounting inconsistency. |
| 347 |
*/ |
| 348 |
if (delta < 0) { |
| 349 |
tp->t_blk_res_used += (uint)-delta; |
| 350 |
if (tp->t_blk_res_used > tp->t_blk_res) |
| 351 |
xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
| 352 |
} else if (delta > 0 && (tp->t_flags & XFS_TRANS_RES_FDBLKS)) { |
| 353 |
int64_t blkres_delta; |
| 354 |
|
| 355 |
/* |
| 356 |
* Return freed blocks directly to the reservation |
| 357 |
* instead of the global pool, being careful not to |
| 358 |
* overflow the trans counter. This is used to preserve |
| 359 |
* reservation across chains of transaction rolls that |
| 360 |
* repeatedly free and allocate blocks. |
| 361 |
*/ |
| 362 |
blkres_delta = min_t(int64_t, delta, |
| 363 |
UINT_MAX - tp->t_blk_res); |
| 364 |
tp->t_blk_res += blkres_delta; |
| 365 |
delta -= blkres_delta; |
| 366 |
} |
| 367 |
tp->t_fdblocks_delta += delta; |
| 368 |
if (xfs_has_lazysbcount(mp)) |
| 369 |
flags &= ~XFS_TRANS_SB_DIRTY; |
| 370 |
break; |
| 371 |
case XFS_TRANS_SB_RES_FDBLOCKS: |
| 372 |
/* |
| 373 |
* The allocation has already been applied to the |
| 374 |
* in-core superblock's counter. This should only |
| 375 |
* be applied to the on-disk superblock. |
| 376 |
*/ |
| 377 |
tp->t_res_fdblocks_delta += delta; |
| 378 |
if (xfs_has_lazysbcount(mp)) |
| 379 |
flags &= ~XFS_TRANS_SB_DIRTY; |
| 380 |
break; |
| 381 |
case XFS_TRANS_SB_FREXTENTS: |
| 382 |
/* |
| 383 |
* Track the number of blocks allocated in the |
| 384 |
* transaction. Make sure it does not exceed the |
| 385 |
* number reserved. |
| 386 |
*/ |
| 387 |
if (delta < 0) { |
| 388 |
tp->t_rtx_res_used += (uint)-delta; |
| 389 |
ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res); |
| 390 |
} |
| 391 |
tp->t_frextents_delta += delta; |
| 392 |
if (xfs_has_rtgroups(mp)) |
| 393 |
flags &= ~XFS_TRANS_SB_DIRTY; |
| 394 |
break; |
| 395 |
case XFS_TRANS_SB_RES_FREXTENTS: |
| 396 |
/* |
| 397 |
* The allocation has already been applied to the |
| 398 |
* in-core superblock's counter. This should only |
| 399 |
* be applied to the on-disk superblock. |
| 400 |
*/ |
| 401 |
ASSERT(delta < 0); |
| 402 |
tp->t_res_frextents_delta += delta; |
| 403 |
if (xfs_has_rtgroups(mp)) |
| 404 |
flags &= ~XFS_TRANS_SB_DIRTY; |
| 405 |
break; |
| 406 |
case XFS_TRANS_SB_DBLOCKS: |
| 407 |
tp->t_dblocks_delta += delta; |
| 408 |
break; |
| 409 |
case XFS_TRANS_SB_AGCOUNT: |
| 410 |
ASSERT(delta > 0); |
| 411 |
tp->t_agcount_delta += delta; |
| 412 |
break; |
| 413 |
case XFS_TRANS_SB_IMAXPCT: |
| 414 |
tp->t_imaxpct_delta += delta; |
| 415 |
break; |
| 416 |
case XFS_TRANS_SB_REXTSIZE: |
| 417 |
tp->t_rextsize_delta += delta; |
| 418 |
break; |
| 419 |
case XFS_TRANS_SB_RBMBLOCKS: |
| 420 |
tp->t_rbmblocks_delta += delta; |
| 421 |
break; |
| 422 |
case XFS_TRANS_SB_RBLOCKS: |
| 423 |
tp->t_rblocks_delta += delta; |
| 424 |
break; |
| 425 |
case XFS_TRANS_SB_REXTENTS: |
| 426 |
tp->t_rextents_delta += delta; |
| 427 |
break; |
| 428 |
case XFS_TRANS_SB_REXTSLOG: |
| 429 |
tp->t_rextslog_delta += delta; |
| 430 |
break; |
| 431 |
case XFS_TRANS_SB_RGCOUNT: |
| 432 |
ASSERT(delta > 0); |
| 433 |
tp->t_rgcount_delta += delta; |
| 434 |
break; |
| 435 |
default: |
| 436 |
ASSERT(0); |
| 437 |
return; |
| 438 |
} |
| 439 |
|
| 440 |
tp->t_flags |= flags; |
| 441 |
} |
| 442 |
|
| 443 |
/* |
| 444 |
* xfs_trans_apply_sb_deltas() is called from the commit code |
| 445 |
* to bring the superblock buffer into the current transaction |
| 446 |
* and modify it as requested by earlier calls to xfs_trans_mod_sb(). |
| 447 |
* |
| 448 |
* For now we just look at each field allowed to change and change |
| 449 |
* it if necessary. |
| 450 |
*/ |
| 451 |
STATIC void |
| 452 |
xfs_trans_apply_sb_deltas( |
| 453 |
struct xfs_trans *tp) |
| 454 |
{ |
| 455 |
struct xfs_mount *mp = tp->t_mountp; |
| 456 |
struct xfs_buf *bp = xfs_trans_getsb(tp); |
| 457 |
struct xfs_dsb *sbp = bp->b_addr; |
| 458 |
int whole = 0; |
| 459 |
|
| 460 |
/* |
| 461 |
* Only update the superblock counters if we are logging them |
| 462 |
*/ |
| 463 |
if (!xfs_has_lazysbcount(mp)) { |
| 464 |
if (tp->t_icount_delta) |
| 465 |
be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta); |
| 466 |
if (tp->t_ifree_delta) |
| 467 |
be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta); |
| 468 |
if (tp->t_fdblocks_delta) |
| 469 |
be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta); |
| 470 |
if (tp->t_res_fdblocks_delta) |
| 471 |
be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta); |
| 472 |
} |
| 473 |
|
| 474 |
/* |
| 475 |
* sb_frextents was added to the lazy sb counters when the rt groups |
| 476 |
* feature was introduced. This is possible because we know that all |
| 477 |
* kernels supporting rtgroups will also recompute frextents from the |
| 478 |
* realtime bitmap. |
| 479 |
* |
| 480 |
* For older file systems, updating frextents requires careful handling |
| 481 |
* because we cannot rely on log recovery in older kernels to recompute |
| 482 |
* the value from the rtbitmap. This means that the ondisk frextents |
| 483 |
* must be consistent with the rtbitmap. |
| 484 |
* |
| 485 |
* Therefore, log the frextents change to the ondisk superblock and |
| 486 |
* update the incore superblock so that future calls to xfs_log_sb |
| 487 |
* write the correct value ondisk. |
| 488 |
*/ |
| 489 |
if ((tp->t_frextents_delta || tp->t_res_frextents_delta) && |
| 490 |
!xfs_has_rtgroups(mp)) { |
| 491 |
int64_t rtxdelta; |
| 492 |
|
| 493 |
rtxdelta = tp->t_frextents_delta + tp->t_res_frextents_delta; |
| 494 |
|
| 495 |
spin_lock(&mp->m_sb_lock); |
| 496 |
be64_add_cpu(&sbp->sb_frextents, rtxdelta); |
| 497 |
mp->m_sb.sb_frextents += rtxdelta; |
| 498 |
spin_unlock(&mp->m_sb_lock); |
| 499 |
} |
| 500 |
|
| 501 |
if (tp->t_dblocks_delta) { |
| 502 |
be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta); |
| 503 |
mp->m_ddev_targp->bt_nr_sectors += |
| 504 |
XFS_FSB_TO_BB(mp, tp->t_dblocks_delta); |
| 505 |
whole = 1; |
| 506 |
} |
| 507 |
if (tp->t_agcount_delta) { |
| 508 |
be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta); |
| 509 |
whole = 1; |
| 510 |
} |
| 511 |
if (tp->t_imaxpct_delta) { |
| 512 |
sbp->sb_imax_pct += tp->t_imaxpct_delta; |
| 513 |
whole = 1; |
| 514 |
} |
| 515 |
if (tp->t_rextsize_delta) { |
| 516 |
be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta); |
| 517 |
|
| 518 |
/* |
| 519 |
* Because the ondisk sb records rtgroup size in units of rt |
| 520 |
* extents, any time we update the rt extent size we have to |
| 521 |
* recompute the ondisk rtgroup block log. The incore values |
| 522 |
* will be recomputed in xfs_trans_unreserve_and_mod_sb. |
| 523 |
*/ |
| 524 |
if (xfs_has_rtgroups(mp)) { |
| 525 |
sbp->sb_rgblklog = xfs_compute_rgblklog( |
| 526 |
be32_to_cpu(sbp->sb_rgextents), |
| 527 |
be32_to_cpu(sbp->sb_rextsize)); |
| 528 |
} |
| 529 |
whole = 1; |
| 530 |
} |
| 531 |
if (tp->t_rbmblocks_delta) { |
| 532 |
be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta); |
| 533 |
whole = 1; |
| 534 |
} |
| 535 |
if (tp->t_rblocks_delta) { |
| 536 |
be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta); |
| 537 |
mp->m_rtdev_targp->bt_nr_sectors += |
| 538 |
XFS_FSB_TO_BB(mp, tp->t_rblocks_delta); |
| 539 |
whole = 1; |
| 540 |
} |
| 541 |
if (tp->t_rextents_delta) { |
| 542 |
be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta); |
| 543 |
whole = 1; |
| 544 |
} |
| 545 |
if (tp->t_rextslog_delta) { |
| 546 |
sbp->sb_rextslog += tp->t_rextslog_delta; |
| 547 |
whole = 1; |
| 548 |
} |
| 549 |
if (tp->t_rgcount_delta) { |
| 550 |
be32_add_cpu(&sbp->sb_rgcount, tp->t_rgcount_delta); |
| 551 |
whole = 1; |
| 552 |
} |
| 553 |
|
| 554 |
xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF); |
| 555 |
if (whole) |
| 556 |
/* |
| 557 |
* Log the whole thing, the fields are noncontiguous. |
| 558 |
*/ |
| 559 |
xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1); |
| 560 |
else |
| 561 |
/* |
| 562 |
* Since all the modifiable fields are contiguous, we |
| 563 |
* can get away with this. |
| 564 |
*/ |
| 565 |
xfs_trans_log_buf(tp, bp, offsetof(struct xfs_dsb, sb_icount), |
| 566 |
offsetof(struct xfs_dsb, sb_frextents) + |
| 567 |
sizeof(sbp->sb_frextents) - 1); |
| 568 |
} |
| 569 |
|
| 570 |
/* |
| 571 |
* xfs_trans_unreserve_and_mod_sb() is called to release unused reservations and |
| 572 |
* apply superblock counter changes to the in-core superblock. The |
| 573 |
* t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT |
| 574 |
* applied to the in-core superblock. The idea is that that has already been |
| 575 |
* done. |
| 576 |
* |
| 577 |
* If we are not logging superblock counters, then the inode allocated/free and |
| 578 |
* used block counts are not updated in the on disk superblock. In this case, |
| 579 |
* XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we |
| 580 |
* still need to update the incore superblock with the changes. |
| 581 |
* |
| 582 |
* Deltas for the inode count are +/-64, hence we use a large batch size of 128 |
| 583 |
* so we don't need to take the counter lock on every update. |
| 584 |
*/ |
| 585 |
#define XFS_ICOUNT_BATCH 128 |
| 586 |
|
| 587 |
void |
| 588 |
xfs_trans_unreserve_and_mod_sb( |
| 589 |
struct xfs_trans *tp) |
| 590 |
{ |
| 591 |
struct xfs_mount *mp = tp->t_mountp; |
| 592 |
int64_t blkdelta = tp->t_blk_res; |
| 593 |
int64_t rtxdelta = tp->t_rtx_res; |
| 594 |
int64_t idelta = 0; |
| 595 |
int64_t ifreedelta = 0; |
| 596 |
|
| 597 |
/* |
| 598 |
* Calculate the deltas. |
| 599 |
* |
| 600 |
* t_fdblocks_delta and t_frextents_delta can be positive or negative: |
| 601 |
* |
| 602 |
* - positive values indicate blocks freed in the transaction. |
| 603 |
* - negative values indicate blocks allocated in the transaction |
| 604 |
* |
| 605 |
* Negative values can only happen if the transaction has a block |
| 606 |
* reservation that covers the allocated block. The end result is |
| 607 |
* that the calculated delta values must always be positive and we |
| 608 |
* can only put back previous allocated or reserved blocks here. |
| 609 |
*/ |
| 610 |
ASSERT(tp->t_blk_res || tp->t_fdblocks_delta >= 0); |
| 611 |
if (xfs_has_lazysbcount(mp) || (tp->t_flags & XFS_TRANS_SB_DIRTY)) { |
| 612 |
blkdelta += tp->t_fdblocks_delta; |
| 613 |
ASSERT(blkdelta >= 0); |
| 614 |
} |
| 615 |
|
| 616 |
ASSERT(tp->t_rtx_res || tp->t_frextents_delta >= 0); |
| 617 |
if (xfs_has_rtgroups(mp) || (tp->t_flags & XFS_TRANS_SB_DIRTY)) { |
| 618 |
rtxdelta += tp->t_frextents_delta; |
| 619 |
ASSERT(rtxdelta >= 0); |
| 620 |
} |
| 621 |
|
| 622 |
if (xfs_has_lazysbcount(mp) || (tp->t_flags & XFS_TRANS_SB_DIRTY)) { |
| 623 |
idelta = tp->t_icount_delta; |
| 624 |
ifreedelta = tp->t_ifree_delta; |
| 625 |
} |
| 626 |
|
| 627 |
/* apply the per-cpu counters */ |
| 628 |
if (blkdelta) |
| 629 |
xfs_add_fdblocks(mp, blkdelta); |
| 630 |
|
| 631 |
if (idelta) |
| 632 |
percpu_counter_add_batch(&mp->m_icount, idelta, |
| 633 |
XFS_ICOUNT_BATCH); |
| 634 |
|
| 635 |
if (ifreedelta) |
| 636 |
percpu_counter_add(&mp->m_ifree, ifreedelta); |
| 637 |
|
| 638 |
if (rtxdelta) |
| 639 |
xfs_add_frextents(mp, rtxdelta); |
| 640 |
|
| 641 |
if (!(tp->t_flags & XFS_TRANS_SB_DIRTY)) |
| 642 |
return; |
| 643 |
|
| 644 |
/* apply remaining deltas */ |
| 645 |
spin_lock(&mp->m_sb_lock); |
| 646 |
mp->m_sb.sb_fdblocks += tp->t_fdblocks_delta + tp->t_res_fdblocks_delta; |
| 647 |
mp->m_sb.sb_icount += idelta; |
| 648 |
mp->m_sb.sb_ifree += ifreedelta; |
| 649 |
/* |
| 650 |
* Do not touch sb_frextents here because it is handled in |
| 651 |
* xfs_trans_apply_sb_deltas for file systems where it isn't a lazy |
| 652 |
* counter anyway. |
| 653 |
*/ |
| 654 |
mp->m_sb.sb_dblocks += tp->t_dblocks_delta; |
| 655 |
mp->m_sb.sb_agcount += tp->t_agcount_delta; |
| 656 |
mp->m_sb.sb_imax_pct += tp->t_imaxpct_delta; |
| 657 |
if (tp->t_rextsize_delta) |
| 658 |
xfs_mount_sb_set_rextsize(mp, &mp->m_sb, |
| 659 |
mp->m_sb.sb_rextsize + tp->t_rextsize_delta); |
| 660 |
mp->m_sb.sb_rbmblocks += tp->t_rbmblocks_delta; |
| 661 |
mp->m_sb.sb_rblocks += tp->t_rblocks_delta; |
| 662 |
mp->m_sb.sb_rextents += tp->t_rextents_delta; |
| 663 |
mp->m_sb.sb_rextslog += tp->t_rextslog_delta; |
| 664 |
mp->m_sb.sb_rgcount += tp->t_rgcount_delta; |
| 665 |
spin_unlock(&mp->m_sb_lock); |
| 666 |
|
| 667 |
/* |
| 668 |
* Debug checks outside of the spinlock so they don't lock up the |
| 669 |
* machine if they fail. |
| 670 |
*/ |
| 671 |
ASSERT(mp->m_sb.sb_imax_pct >= 0); |
| 672 |
ASSERT(mp->m_sb.sb_rextslog >= 0); |
| 673 |
} |
| 674 |
|
| 675 |
/* Add the given log item to the transaction's list of log items. */ |
| 676 |
void |
| 677 |
xfs_trans_add_item( |
| 678 |
struct xfs_trans *tp, |
| 679 |
struct xfs_log_item *lip) |
| 680 |
{ |
| 681 |
ASSERT(lip->li_log == tp->t_mountp->m_log); |
| 682 |
ASSERT(lip->li_ailp == tp->t_mountp->m_ail); |
| 683 |
ASSERT(list_empty(&lip->li_trans)); |
| 684 |
ASSERT(!test_bit(XFS_LI_DIRTY, &lip->li_flags)); |
| 685 |
|
| 686 |
list_add_tail(&lip->li_trans, &tp->t_items); |
| 687 |
trace_xfs_trans_add_item(tp, _RET_IP_); |
| 688 |
} |
| 689 |
|
| 690 |
/* |
| 691 |
* Unlink the log item from the transaction. the log item is no longer |
| 692 |
* considered dirty in this transaction, as the linked transaction has |
| 693 |
* finished, either by abort or commit completion. |
| 694 |
*/ |
| 695 |
void |
| 696 |
xfs_trans_del_item( |
| 697 |
struct xfs_log_item *lip) |
| 698 |
{ |
| 699 |
clear_bit(XFS_LI_DIRTY, &lip->li_flags); |
| 700 |
list_del_init(&lip->li_trans); |
| 701 |
} |
| 702 |
|
| 703 |
/* Detach and unlock all of the items in a transaction */ |
| 704 |
static void |
| 705 |
xfs_trans_free_items( |
| 706 |
struct xfs_trans *tp, |
| 707 |
bool abort) |
| 708 |
{ |
| 709 |
struct xfs_log_item *lip, *next; |
| 710 |
|
| 711 |
trace_xfs_trans_free_items(tp, _RET_IP_); |
| 712 |
|
| 713 |
list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) { |
| 714 |
xfs_trans_del_item(lip); |
| 715 |
if (abort) { |
| 716 |
trace_xfs_trans_free_abort(lip); |
| 717 |
set_bit(XFS_LI_ABORTED, &lip->li_flags); |
| 718 |
} |
| 719 |
if (lip->li_ops->iop_release) |
| 720 |
lip->li_ops->iop_release(lip); |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
/* |
| 725 |
* Sort transaction items prior to running precommit operations. This will |
| 726 |
* attempt to order the items such that they will always be locked in the same |
| 727 |
* order. Items that have no sort function are moved to the end of the list |
| 728 |
* and so are locked last. |
| 729 |
* |
| 730 |
* This may need refinement as different types of objects add sort functions. |
| 731 |
* |
| 732 |
* Function is more complex than it needs to be because we are comparing 64 bit |
| 733 |
* values and the function only returns 32 bit values. |
| 734 |
*/ |
| 735 |
static int |
| 736 |
xfs_trans_precommit_sort( |
| 737 |
void *unused_arg, |
| 738 |
const struct list_head *a, |
| 739 |
const struct list_head *b) |
| 740 |
{ |
| 741 |
struct xfs_log_item *lia = container_of(a, |
| 742 |
struct xfs_log_item, li_trans); |
| 743 |
struct xfs_log_item *lib = container_of(b, |
| 744 |
struct xfs_log_item, li_trans); |
| 745 |
int64_t diff; |
| 746 |
|
| 747 |
/* |
| 748 |
* If both items are non-sortable, leave them alone. If only one is |
| 749 |
* sortable, move the non-sortable item towards the end of the list. |
| 750 |
*/ |
| 751 |
if (!lia->li_ops->iop_sort && !lib->li_ops->iop_sort) |
| 752 |
return 0; |
| 753 |
if (!lia->li_ops->iop_sort) |
| 754 |
return 1; |
| 755 |
if (!lib->li_ops->iop_sort) |
| 756 |
return -1; |
| 757 |
|
| 758 |
diff = lia->li_ops->iop_sort(lia) - lib->li_ops->iop_sort(lib); |
| 759 |
if (diff < 0) |
| 760 |
return -1; |
| 761 |
if (diff > 0) |
| 762 |
return 1; |
| 763 |
return 0; |
| 764 |
} |
| 765 |
|
| 766 |
/* |
| 767 |
* Run transaction precommit functions. |
| 768 |
* |
| 769 |
* If there is an error in any of the callouts, then stop immediately and |
| 770 |
* trigger a shutdown to abort the transaction. There is no recovery possible |
| 771 |
* from errors at this point as the transaction is dirty.... |
| 772 |
*/ |
| 773 |
static int |
| 774 |
xfs_trans_run_precommits( |
| 775 |
struct xfs_trans *tp) |
| 776 |
{ |
| 777 |
struct xfs_mount *mp = tp->t_mountp; |
| 778 |
struct xfs_log_item *lip, *n; |
| 779 |
int error = 0; |
| 780 |
|
| 781 |
/* |
| 782 |
* Sort the item list to avoid ABBA deadlocks with other transactions |
| 783 |
* running precommit operations that lock multiple shared items such as |
| 784 |
* inode cluster buffers. |
| 785 |
*/ |
| 786 |
list_sort(NULL, &tp->t_items, xfs_trans_precommit_sort); |
| 787 |
|
| 788 |
/* |
| 789 |
* Precommit operations can remove the log item from the transaction |
| 790 |
* if the log item exists purely to delay modifications until they |
| 791 |
* can be ordered against other operations. Hence we have to use |
| 792 |
* list_for_each_entry_safe() here. |
| 793 |
*/ |
| 794 |
list_for_each_entry_safe(lip, n, &tp->t_items, li_trans) { |
| 795 |
if (!test_bit(XFS_LI_DIRTY, &lip->li_flags)) |
| 796 |
continue; |
| 797 |
if (lip->li_ops->iop_precommit) { |
| 798 |
error = lip->li_ops->iop_precommit(tp, lip); |
| 799 |
if (error) |
| 800 |
break; |
| 801 |
} |
| 802 |
} |
| 803 |
if (error) |
| 804 |
xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
| 805 |
return error; |
| 806 |
} |
| 807 |
|
| 808 |
/* |
| 809 |
* Commit the given transaction to the log. |
| 810 |
* |
| 811 |
* XFS disk error handling mechanism is not based on a typical |
| 812 |
* transaction abort mechanism. Logically after the filesystem |
| 813 |
* gets marked 'SHUTDOWN', we can't let any new transactions |
| 814 |
* be durable - ie. committed to disk - because some metadata might |
| 815 |
* be inconsistent. In such cases, this returns an error, and the |
| 816 |
* caller may assume that all locked objects joined to the transaction |
| 817 |
* have already been unlocked as if the commit had succeeded. |
| 818 |
* Do not reference the transaction structure after this call. |
| 819 |
*/ |
| 820 |
static int |
| 821 |
__xfs_trans_commit( |
| 822 |
struct xfs_trans *tp, |
| 823 |
bool regrant) |
| 824 |
{ |
| 825 |
struct xfs_mount *mp = tp->t_mountp; |
| 826 |
struct xlog *log = mp->m_log; |
| 827 |
xfs_csn_t commit_seq = 0; |
| 828 |
int error = 0; |
| 829 |
int sync = tp->t_flags & XFS_TRANS_SYNC; |
| 830 |
|
| 831 |
trace_xfs_trans_commit(tp, _RET_IP_); |
| 832 |
|
| 833 |
/* |
| 834 |
* Commit per-transaction changes that are not already tracked through |
| 835 |
* log items. This can add dirty log items to the transaction. |
| 836 |
*/ |
| 837 |
if (tp->t_flags & XFS_TRANS_SB_DIRTY) |
| 838 |
xfs_trans_apply_sb_deltas(tp); |
| 839 |
xfs_trans_apply_dquot_deltas(tp); |
| 840 |
|
| 841 |
error = xfs_trans_run_precommits(tp); |
| 842 |
if (error) |
| 843 |
goto out_unreserve; |
| 844 |
|
| 845 |
/* |
| 846 |
* If there is nothing to be logged by the transaction, |
| 847 |
* then unlock all of the items associated with the |
| 848 |
* transaction and free the transaction structure. |
| 849 |
* Also make sure to return any reserved blocks to |
| 850 |
* the free pool. |
| 851 |
*/ |
| 852 |
if (!(tp->t_flags & XFS_TRANS_DIRTY)) |
| 853 |
goto out_unreserve; |
| 854 |
|
| 855 |
/* |
| 856 |
* We must check against log shutdown here because we cannot abort log |
| 857 |
* items and leave them dirty, inconsistent and unpinned in memory while |
| 858 |
* the log is active. This leaves them open to being written back to |
| 859 |
* disk, and that will lead to on-disk corruption. |
| 860 |
*/ |
| 861 |
if (xlog_is_shutdown(log)) { |
| 862 |
error = -EIO; |
| 863 |
goto out_unreserve; |
| 864 |
} |
| 865 |
|
| 866 |
ASSERT(tp->t_ticket != NULL); |
| 867 |
|
| 868 |
xlog_cil_commit(log, tp, &commit_seq, regrant); |
| 869 |
|
| 870 |
xfs_trans_free(tp); |
| 871 |
|
| 872 |
/* |
| 873 |
* If the transaction needs to be synchronous, then force the |
| 874 |
* log out now and wait for it. |
| 875 |
*/ |
| 876 |
if (sync) { |
| 877 |
error = xfs_log_force_seq(mp, commit_seq, XFS_LOG_SYNC, NULL); |
| 878 |
XFS_STATS_INC(mp, xs_trans_sync); |
| 879 |
} else { |
| 880 |
XFS_STATS_INC(mp, xs_trans_async); |
| 881 |
} |
| 882 |
|
| 883 |
return error; |
| 884 |
|
| 885 |
out_unreserve: |
| 886 |
xfs_trans_unreserve_and_mod_sb(tp); |
| 887 |
|
| 888 |
/* |
| 889 |
* It is indeed possible for the transaction to be not dirty but |
| 890 |
* the dqinfo portion to be. All that means is that we have some |
| 891 |
* (non-persistent) quota reservations that need to be unreserved. |
| 892 |
*/ |
| 893 |
xfs_trans_unreserve_and_mod_dquots(tp, true); |
| 894 |
if (tp->t_ticket) { |
| 895 |
if (regrant && !xlog_is_shutdown(log)) |
| 896 |
xfs_log_ticket_regrant(log, tp->t_ticket); |
| 897 |
else |
| 898 |
xfs_log_ticket_ungrant(log, tp->t_ticket); |
| 899 |
tp->t_ticket = NULL; |
| 900 |
} |
| 901 |
xfs_trans_free_items(tp, !!error); |
| 902 |
xfs_trans_free(tp); |
| 903 |
|
| 904 |
XFS_STATS_INC(mp, xs_trans_empty); |
| 905 |
return error; |
| 906 |
} |
| 907 |
|
| 908 |
int |
| 909 |
xfs_trans_commit( |
| 910 |
struct xfs_trans *tp) |
| 911 |
{ |
| 912 |
/* |
| 913 |
* Finish deferred items on final commit. Only permanent transactions |
| 914 |
* should ever have deferred ops. |
| 915 |
*/ |
| 916 |
WARN_ON_ONCE(!list_empty(&tp->t_dfops) && |
| 917 |
!(tp->t_flags & XFS_TRANS_PERM_LOG_RES)); |
| 918 |
if (tp->t_flags & XFS_TRANS_PERM_LOG_RES) { |
| 919 |
int error = xfs_defer_finish_noroll(&tp); |
| 920 |
if (error) { |
| 921 |
xfs_trans_cancel(tp); |
| 922 |
return error; |
| 923 |
} |
| 924 |
} |
| 925 |
|
| 926 |
return __xfs_trans_commit(tp, false); |
| 927 |
} |
| 928 |
|
| 929 |
/* |
| 930 |
* Unlock all of the transaction's items and free the transaction. If the |
| 931 |
* transaction is dirty, we must shut down the filesystem because there is no |
| 932 |
* way to restore them to their previous state. |
| 933 |
* |
| 934 |
* If the transaction has made a log reservation, make sure to release it as |
| 935 |
* well. |
| 936 |
* |
| 937 |
* This is a high level function (equivalent to xfs_trans_commit()) and so can |
| 938 |
* be called after the transaction has effectively been aborted due to the mount |
| 939 |
* being shut down. However, if the mount has not been shut down and the |
| 940 |
* transaction is dirty we will shut the mount down and, in doing so, that |
| 941 |
* guarantees that the log is shut down, too. Hence we don't need to be as |
| 942 |
* careful with shutdown state and dirty items here as we need to be in |
| 943 |
* xfs_trans_commit(). |
| 944 |
*/ |
| 945 |
void |
| 946 |
xfs_trans_cancel( |
| 947 |
struct xfs_trans *tp) |
| 948 |
{ |
| 949 |
struct xfs_mount *mp = tp->t_mountp; |
| 950 |
struct xlog *log = mp->m_log; |
| 951 |
bool dirty = (tp->t_flags & XFS_TRANS_DIRTY); |
| 952 |
|
| 953 |
trace_xfs_trans_cancel(tp, _RET_IP_); |
| 954 |
|
| 955 |
/* |
| 956 |
* It's never valid to cancel a transaction with deferred ops attached, |
| 957 |
* because the transaction is effectively dirty. Complain about this |
| 958 |
* loudly before freeing the in-memory defer items and shutting down the |
| 959 |
* filesystem. |
| 960 |
*/ |
| 961 |
if (!list_empty(&tp->t_dfops)) { |
| 962 |
ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); |
| 963 |
dirty = true; |
| 964 |
xfs_defer_cancel(tp); |
| 965 |
} |
| 966 |
|
| 967 |
/* |
| 968 |
* See if the caller is relying on us to shut down the filesystem. We |
| 969 |
* only want an error report if there isn't already a shutdown in |
| 970 |
* progress, so we only need to check against the mount shutdown state |
| 971 |
* here. |
| 972 |
*/ |
| 973 |
if (dirty && !xfs_is_shutdown(mp)) { |
| 974 |
XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp); |
| 975 |
xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
| 976 |
} |
| 977 |
#ifdef DEBUG |
| 978 |
/* Log items need to be consistent until the log is shut down. */ |
| 979 |
if (!dirty && !xlog_is_shutdown(log)) { |
| 980 |
struct xfs_log_item *lip; |
| 981 |
|
| 982 |
list_for_each_entry(lip, &tp->t_items, li_trans) |
| 983 |
ASSERT(!xlog_item_is_intent_done(lip)); |
| 984 |
} |
| 985 |
#endif |
| 986 |
xfs_trans_unreserve_and_mod_sb(tp); |
| 987 |
xfs_trans_unreserve_and_mod_dquots(tp, false); |
| 988 |
|
| 989 |
if (tp->t_ticket) { |
| 990 |
xfs_log_ticket_ungrant(log, tp->t_ticket); |
| 991 |
tp->t_ticket = NULL; |
| 992 |
} |
| 993 |
|
| 994 |
xfs_trans_free_items(tp, dirty); |
| 995 |
xfs_trans_free(tp); |
| 996 |
} |
| 997 |
|
| 998 |
/* |
| 999 |
* Roll from one trans in the sequence of PERMANENT transactions to the next: |
| 1000 |
* permanent transactions are only flushed out when committed with |
| 1001 |
* xfs_trans_commit(), but we still want as soon as possible to let chunks of it |
| 1002 |
* go to the log. So we commit the chunk we've been working on and get a new |
| 1003 |
* transaction to continue. |
| 1004 |
*/ |
| 1005 |
int |
| 1006 |
xfs_trans_roll( |
| 1007 |
struct xfs_trans **tpp) |
| 1008 |
{ |
| 1009 |
struct xfs_trans *tp = *tpp; |
| 1010 |
unsigned int log_res = tp->t_log_res; |
| 1011 |
unsigned int log_count = tp->t_log_count; |
| 1012 |
int error; |
| 1013 |
|
| 1014 |
trace_xfs_trans_roll(tp, _RET_IP_); |
| 1015 |
|
| 1016 |
ASSERT(log_res > 0); |
| 1017 |
|
| 1018 |
/* |
| 1019 |
* Copy the critical parameters from one trans to the next. |
| 1020 |
*/ |
| 1021 |
*tpp = xfs_trans_dup(tp); |
| 1022 |
|
| 1023 |
/* |
| 1024 |
* Commit the current transaction. |
| 1025 |
* |
| 1026 |
* If this commit failed, then it'd just unlock those items that are not |
| 1027 |
* marked ihold. That also means that a filesystem shutdown is in |
| 1028 |
* progress. The caller takes the responsibility to cancel the |
| 1029 |
* duplicate transaction that gets returned. |
| 1030 |
*/ |
| 1031 |
error = __xfs_trans_commit(tp, true); |
| 1032 |
if (error) |
| 1033 |
return error; |
| 1034 |
|
| 1035 |
/* |
| 1036 |
* Reserve space in the log for the next transaction. |
| 1037 |
* |
| 1038 |
* This also pushes items in the AIL out to disk if they are taking up |
| 1039 |
* space at the tail of the log that we want to use. This requires that |
| 1040 |
* either nothing be locked across this call, or that anything that is |
| 1041 |
* locked be logged in the prior and the next transactions. |
| 1042 |
*/ |
| 1043 |
tp = *tpp; |
| 1044 |
/* |
| 1045 |
* __xfs_trans_commit cleared the NOFS flag by calling into |
| 1046 |
* xfs_trans_free. Set it again here before doing memory |
| 1047 |
* allocations. |
| 1048 |
*/ |
| 1049 |
xfs_trans_set_context(tp); |
| 1050 |
error = xfs_log_regrant(tp->t_mountp, tp->t_ticket); |
| 1051 |
if (error) |
| 1052 |
return error; |
| 1053 |
tp->t_log_res = log_res; |
| 1054 |
tp->t_log_count = log_count; |
| 1055 |
return 0; |
| 1056 |
} |
| 1057 |
|
| 1058 |
/* |
| 1059 |
* Allocate an transaction, lock and join the inode to it, and reserve quota. |
| 1060 |
* |
| 1061 |
* The caller must ensure that the on-disk dquots attached to this inode have |
| 1062 |
* already been allocated and initialized. The caller is responsible for |
| 1063 |
* releasing ILOCK_EXCL if a new transaction is returned. |
| 1064 |
*/ |
| 1065 |
int |
| 1066 |
xfs_trans_alloc_inode( |
| 1067 |
struct xfs_inode *ip, |
| 1068 |
struct xfs_trans_res *resv, |
| 1069 |
unsigned int dblocks, |
| 1070 |
unsigned int rblocks, |
| 1071 |
bool force, |
| 1072 |
struct xfs_trans **tpp) |
| 1073 |
{ |
| 1074 |
struct xfs_trans *tp; |
| 1075 |
struct xfs_mount *mp = ip->i_mount; |
| 1076 |
bool retried = false; |
| 1077 |
int error; |
| 1078 |
|
| 1079 |
retry: |
| 1080 |
error = xfs_trans_alloc(mp, resv, dblocks, |
| 1081 |
xfs_extlen_to_rtxlen(mp, rblocks), |
| 1082 |
force ? XFS_TRANS_RESERVE : 0, &tp); |
| 1083 |
if (error) |
| 1084 |
return error; |
| 1085 |
|
| 1086 |
xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1087 |
xfs_trans_ijoin(tp, ip, 0); |
| 1088 |
|
| 1089 |
error = xfs_qm_dqattach_locked(ip, false); |
| 1090 |
if (error) { |
| 1091 |
/* Caller should have allocated the dquots! */ |
| 1092 |
ASSERT(error != -ENOENT); |
| 1093 |
goto out_cancel; |
| 1094 |
} |
| 1095 |
|
| 1096 |
error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, rblocks, force); |
| 1097 |
if ((error == -EDQUOT || error == -ENOSPC) && !retried) { |
| 1098 |
xfs_trans_cancel(tp); |
| 1099 |
xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1100 |
xfs_blockgc_free_quota(ip, 0); |
| 1101 |
retried = true; |
| 1102 |
goto retry; |
| 1103 |
} |
| 1104 |
if (error) |
| 1105 |
goto out_cancel; |
| 1106 |
|
| 1107 |
*tpp = tp; |
| 1108 |
return 0; |
| 1109 |
|
| 1110 |
out_cancel: |
| 1111 |
xfs_trans_cancel(tp); |
| 1112 |
xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1113 |
return error; |
| 1114 |
} |
| 1115 |
|
| 1116 |
/* |
| 1117 |
* Try to reserve more blocks for a transaction. |
| 1118 |
* |
| 1119 |
* This is for callers that need to attach resources to a transaction, scan |
| 1120 |
* those resources to determine the space reservation requirements, and then |
| 1121 |
* modify the attached resources. In other words, online repair. This can |
| 1122 |
* fail due to ENOSPC, so the caller must be able to cancel the transaction |
| 1123 |
* without shutting down the fs. |
| 1124 |
*/ |
| 1125 |
int |
| 1126 |
xfs_trans_reserve_more( |
| 1127 |
struct xfs_trans *tp, |
| 1128 |
unsigned int blocks, |
| 1129 |
unsigned int rtextents) |
| 1130 |
{ |
| 1131 |
bool rsvd = tp->t_flags & XFS_TRANS_RESERVE; |
| 1132 |
|
| 1133 |
if (blocks && xfs_dec_fdblocks(tp->t_mountp, blocks, rsvd)) |
| 1134 |
return -ENOSPC; |
| 1135 |
if (rtextents && xfs_dec_frextents(tp->t_mountp, rtextents)) { |
| 1136 |
if (blocks) |
| 1137 |
xfs_add_fdblocks(tp->t_mountp, blocks); |
| 1138 |
return -ENOSPC; |
| 1139 |
} |
| 1140 |
tp->t_blk_res += blocks; |
| 1141 |
tp->t_rtx_res += rtextents; |
| 1142 |
return 0; |
| 1143 |
} |
| 1144 |
|
| 1145 |
/* |
| 1146 |
* Try to reserve more blocks and file quota for a transaction. Same |
| 1147 |
* conditions of usage as xfs_trans_reserve_more. |
| 1148 |
*/ |
| 1149 |
int |
| 1150 |
xfs_trans_reserve_more_inode( |
| 1151 |
struct xfs_trans *tp, |
| 1152 |
struct xfs_inode *ip, |
| 1153 |
unsigned int dblocks, |
| 1154 |
unsigned int rblocks, |
| 1155 |
bool force_quota) |
| 1156 |
{ |
| 1157 |
struct xfs_mount *mp = ip->i_mount; |
| 1158 |
unsigned int rtx = xfs_extlen_to_rtxlen(mp, rblocks); |
| 1159 |
int error; |
| 1160 |
|
| 1161 |
xfs_assert_ilocked(ip, XFS_ILOCK_EXCL); |
| 1162 |
|
| 1163 |
error = xfs_trans_reserve_more(tp, dblocks, rtx); |
| 1164 |
if (error) |
| 1165 |
return error; |
| 1166 |
|
| 1167 |
if (!XFS_IS_QUOTA_ON(mp) || xfs_is_quota_inode(&mp->m_sb, ip->i_ino)) |
| 1168 |
return 0; |
| 1169 |
|
| 1170 |
if (tp->t_flags & XFS_TRANS_RESERVE) |
| 1171 |
force_quota = true; |
| 1172 |
|
| 1173 |
error = xfs_trans_reserve_quota_nblks(tp, ip, dblocks, rblocks, |
| 1174 |
force_quota); |
| 1175 |
if (!error) |
| 1176 |
return 0; |
| 1177 |
|
| 1178 |
/* Quota failed, give back the new reservation. */ |
| 1179 |
xfs_add_fdblocks(mp, dblocks); |
| 1180 |
tp->t_blk_res -= dblocks; |
| 1181 |
xfs_add_frextents(mp, rtx); |
| 1182 |
tp->t_rtx_res -= rtx; |
| 1183 |
return error; |
| 1184 |
} |
| 1185 |
|
| 1186 |
/* |
| 1187 |
* Allocate an transaction in preparation for inode creation by reserving quota |
| 1188 |
* against the given dquots. Callers are not required to hold any inode locks. |
| 1189 |
*/ |
| 1190 |
int |
| 1191 |
xfs_trans_alloc_icreate( |
| 1192 |
struct xfs_mount *mp, |
| 1193 |
struct xfs_trans_res *resv, |
| 1194 |
struct xfs_dquot *udqp, |
| 1195 |
struct xfs_dquot *gdqp, |
| 1196 |
struct xfs_dquot *pdqp, |
| 1197 |
unsigned int dblocks, |
| 1198 |
struct xfs_trans **tpp) |
| 1199 |
{ |
| 1200 |
struct xfs_trans *tp; |
| 1201 |
bool retried = false; |
| 1202 |
int error; |
| 1203 |
|
| 1204 |
retry: |
| 1205 |
error = xfs_trans_alloc(mp, resv, dblocks, 0, 0, &tp); |
| 1206 |
if (error) |
| 1207 |
return error; |
| 1208 |
|
| 1209 |
error = xfs_trans_reserve_quota_icreate(tp, udqp, gdqp, pdqp, dblocks); |
| 1210 |
if ((error == -EDQUOT || error == -ENOSPC) && !retried) { |
| 1211 |
xfs_trans_cancel(tp); |
| 1212 |
xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0); |
| 1213 |
retried = true; |
| 1214 |
goto retry; |
| 1215 |
} |
| 1216 |
if (error) { |
| 1217 |
xfs_trans_cancel(tp); |
| 1218 |
return error; |
| 1219 |
} |
| 1220 |
|
| 1221 |
*tpp = tp; |
| 1222 |
return 0; |
| 1223 |
} |
| 1224 |
|
| 1225 |
/* |
| 1226 |
* Allocate an transaction, lock and join the inode to it, and reserve quota |
| 1227 |
* in preparation for inode attribute changes that include uid, gid, or prid |
| 1228 |
* changes. |
| 1229 |
* |
| 1230 |
* The caller must ensure that the on-disk dquots attached to this inode have |
| 1231 |
* already been allocated and initialized. The ILOCK will be dropped when the |
| 1232 |
* transaction is committed or cancelled. |
| 1233 |
*/ |
| 1234 |
int |
| 1235 |
xfs_trans_alloc_ichange( |
| 1236 |
struct xfs_inode *ip, |
| 1237 |
struct xfs_dquot *new_udqp, |
| 1238 |
struct xfs_dquot *new_gdqp, |
| 1239 |
struct xfs_dquot *new_pdqp, |
| 1240 |
bool force, |
| 1241 |
struct xfs_trans **tpp) |
| 1242 |
{ |
| 1243 |
struct xfs_trans *tp; |
| 1244 |
struct xfs_mount *mp = ip->i_mount; |
| 1245 |
struct xfs_dquot *udqp; |
| 1246 |
struct xfs_dquot *gdqp; |
| 1247 |
struct xfs_dquot *pdqp; |
| 1248 |
bool retried = false; |
| 1249 |
int error; |
| 1250 |
|
| 1251 |
retry: |
| 1252 |
error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp); |
| 1253 |
if (error) |
| 1254 |
return error; |
| 1255 |
|
| 1256 |
xfs_ilock(ip, XFS_ILOCK_EXCL); |
| 1257 |
xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); |
| 1258 |
|
| 1259 |
if (xfs_is_metadir_inode(ip)) |
| 1260 |
goto out; |
| 1261 |
|
| 1262 |
error = xfs_qm_dqattach_locked(ip, false); |
| 1263 |
if (error) { |
| 1264 |
/* Caller should have allocated the dquots! */ |
| 1265 |
ASSERT(error != -ENOENT); |
| 1266 |
goto out_cancel; |
| 1267 |
} |
| 1268 |
|
| 1269 |
/* |
| 1270 |
* For each quota type, skip quota reservations if the inode's dquots |
| 1271 |
* now match the ones that came from the caller, or the caller didn't |
| 1272 |
* pass one in. The inode's dquots can change if we drop the ILOCK to |
| 1273 |
* perform a blockgc scan, so we must preserve the caller's arguments. |
| 1274 |
*/ |
| 1275 |
udqp = (new_udqp != ip->i_udquot) ? new_udqp : NULL; |
| 1276 |
gdqp = (new_gdqp != ip->i_gdquot) ? new_gdqp : NULL; |
| 1277 |
pdqp = (new_pdqp != ip->i_pdquot) ? new_pdqp : NULL; |
| 1278 |
if (udqp || gdqp || pdqp) { |
| 1279 |
xfs_filblks_t dblocks, rblocks; |
| 1280 |
unsigned int qflags = XFS_QMOPT_RES_REGBLKS; |
| 1281 |
bool isrt = XFS_IS_REALTIME_INODE(ip); |
| 1282 |
|
| 1283 |
if (force) |
| 1284 |
qflags |= XFS_QMOPT_FORCE_RES; |
| 1285 |
|
| 1286 |
if (isrt) { |
| 1287 |
error = xfs_iread_extents(tp, ip, XFS_DATA_FORK); |
| 1288 |
if (error) |
| 1289 |
goto out_cancel; |
| 1290 |
} |
| 1291 |
|
| 1292 |
xfs_inode_count_blocks(tp, ip, &dblocks, &rblocks); |
| 1293 |
|
| 1294 |
if (isrt) |
| 1295 |
rblocks += ip->i_delayed_blks; |
| 1296 |
else |
| 1297 |
dblocks += ip->i_delayed_blks; |
| 1298 |
|
| 1299 |
/* |
| 1300 |
* Reserve enough quota to handle blocks on disk and reserved |
| 1301 |
* for a delayed allocation. We'll actually transfer the |
| 1302 |
* delalloc reservation between dquots at chown time, even |
| 1303 |
* though that part is only semi-transactional. |
| 1304 |
*/ |
| 1305 |
error = xfs_trans_reserve_quota_bydquots(tp, mp, udqp, gdqp, |
| 1306 |
pdqp, dblocks, 1, qflags); |
| 1307 |
if ((error == -EDQUOT || error == -ENOSPC) && !retried) { |
| 1308 |
xfs_trans_cancel(tp); |
| 1309 |
xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0); |
| 1310 |
retried = true; |
| 1311 |
goto retry; |
| 1312 |
} |
| 1313 |
if (error) |
| 1314 |
goto out_cancel; |
| 1315 |
|
| 1316 |
/* Do the same for realtime. */ |
| 1317 |
qflags = XFS_QMOPT_RES_RTBLKS | (qflags & XFS_QMOPT_FORCE_RES); |
| 1318 |
error = xfs_trans_reserve_quota_bydquots(tp, mp, udqp, gdqp, |
| 1319 |
pdqp, rblocks, 0, qflags); |
| 1320 |
if ((error == -EDQUOT || error == -ENOSPC) && !retried) { |
| 1321 |
xfs_trans_cancel(tp); |
| 1322 |
xfs_blockgc_free_dquots(mp, udqp, gdqp, pdqp, 0); |
| 1323 |
retried = true; |
| 1324 |
goto retry; |
| 1325 |
} |
| 1326 |
if (error) |
| 1327 |
goto out_cancel; |
| 1328 |
} |
| 1329 |
|
| 1330 |
out: |
| 1331 |
*tpp = tp; |
| 1332 |
return 0; |
| 1333 |
|
| 1334 |
out_cancel: |
| 1335 |
xfs_trans_cancel(tp); |
| 1336 |
return error; |
| 1337 |
} |
| 1338 |
|
| 1339 |
/* |
| 1340 |
* Allocate an transaction, lock and join the directory and child inodes to it, |
| 1341 |
* and reserve quota for a directory update. If there isn't sufficient space, |
| 1342 |
* @dblocks will be set to zero for a reservationless directory update and |
| 1343 |
* @nospace_error will be set to a negative errno describing the space |
| 1344 |
* constraint we hit. |
| 1345 |
* |
| 1346 |
* The caller must ensure that the on-disk dquots attached to this inode have |
| 1347 |
* already been allocated and initialized. The ILOCKs will be dropped when the |
| 1348 |
* transaction is committed or cancelled. |
| 1349 |
* |
| 1350 |
* Caller is responsible for unlocking the inodes manually upon return |
| 1351 |
*/ |
| 1352 |
int |
| 1353 |
xfs_trans_alloc_dir( |
| 1354 |
struct xfs_inode *dp, |
| 1355 |
struct xfs_trans_res *resv, |
| 1356 |
struct xfs_inode *ip, |
| 1357 |
unsigned int *dblocks, |
| 1358 |
struct xfs_trans **tpp, |
| 1359 |
int *nospace_error) |
| 1360 |
{ |
| 1361 |
struct xfs_trans *tp; |
| 1362 |
struct xfs_mount *mp = ip->i_mount; |
| 1363 |
unsigned int resblks; |
| 1364 |
bool retried = false; |
| 1365 |
int error; |
| 1366 |
|
| 1367 |
retry: |
| 1368 |
*nospace_error = 0; |
| 1369 |
resblks = *dblocks; |
| 1370 |
error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp); |
| 1371 |
if (error == -ENOSPC) { |
| 1372 |
*nospace_error = error; |
| 1373 |
resblks = 0; |
| 1374 |
error = xfs_trans_alloc(mp, resv, resblks, 0, 0, &tp); |
| 1375 |
} |
| 1376 |
if (error) |
| 1377 |
return error; |
| 1378 |
|
| 1379 |
xfs_lock_two_inodes(dp, XFS_ILOCK_EXCL, ip, XFS_ILOCK_EXCL); |
| 1380 |
|
| 1381 |
xfs_trans_ijoin(tp, dp, 0); |
| 1382 |
xfs_trans_ijoin(tp, ip, 0); |
| 1383 |
|
| 1384 |
error = xfs_qm_dqattach_locked(dp, false); |
| 1385 |
if (error) { |
| 1386 |
/* Caller should have allocated the dquots! */ |
| 1387 |
ASSERT(error != -ENOENT); |
| 1388 |
goto out_cancel; |
| 1389 |
} |
| 1390 |
|
| 1391 |
error = xfs_qm_dqattach_locked(ip, false); |
| 1392 |
if (error) { |
| 1393 |
/* Caller should have allocated the dquots! */ |
| 1394 |
ASSERT(error != -ENOENT); |
| 1395 |
goto out_cancel; |
| 1396 |
} |
| 1397 |
|
| 1398 |
if (resblks == 0) |
| 1399 |
goto done; |
| 1400 |
|
| 1401 |
error = xfs_trans_reserve_quota_nblks(tp, dp, resblks, 0, false); |
| 1402 |
if (error == -EDQUOT || error == -ENOSPC) { |
| 1403 |
if (!retried) { |
| 1404 |
xfs_trans_cancel(tp); |
| 1405 |
xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 1406 |
if (dp != ip) |
| 1407 |
xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1408 |
xfs_blockgc_free_quota(dp, 0); |
| 1409 |
retried = true; |
| 1410 |
goto retry; |
| 1411 |
} |
| 1412 |
|
| 1413 |
*nospace_error = error; |
| 1414 |
resblks = 0; |
| 1415 |
error = 0; |
| 1416 |
} |
| 1417 |
if (error) |
| 1418 |
goto out_cancel; |
| 1419 |
|
| 1420 |
done: |
| 1421 |
*tpp = tp; |
| 1422 |
*dblocks = resblks; |
| 1423 |
return 0; |
| 1424 |
|
| 1425 |
out_cancel: |
| 1426 |
xfs_trans_cancel(tp); |
| 1427 |
xfs_iunlock(dp, XFS_ILOCK_EXCL); |
| 1428 |
if (dp != ip) |
| 1429 |
xfs_iunlock(ip, XFS_ILOCK_EXCL); |
| 1430 |
return error; |
| 1431 |
} |