1 // SPDX-License-Identifier: GPL-2.0 << 2 /* 1 /* 3 * Copyright (c) 2000-2003,2005 Silicon Graphi !! 2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved. 4 * Copyright (C) 2010 Red Hat, Inc. !! 3 * 5 * All Rights Reserved. !! 4 * This program is free software; you can redistribute it and/or modify it >> 5 * under the terms of version 2 of the GNU General Public License as >> 6 * published by the Free Software Foundation. >> 7 * >> 8 * This program is distributed in the hope that it would be useful, but >> 9 * WITHOUT ANY WARRANTY; without even the implied warranty of >> 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. >> 11 * >> 12 * Further, this software is distributed without any warranty that it is >> 13 * free of the rightful claim of any third person regarding infringement >> 14 * or the like. Any license provided herein, whether implied or >> 15 * otherwise, applies only to this software file. Patent licenses, if >> 16 * any, provided herein do not apply to combinations of this program with >> 17 * other software, or any other product whatsoever. >> 18 * >> 19 * You should have received a copy of the GNU General Public License along >> 20 * with this program; if not, write the Free Software Foundation, Inc., 59 >> 21 * Temple Place - Suite 330, Boston MA 02111-1307, USA. >> 22 * >> 23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, >> 24 * Mountain View, CA 94043, or: >> 25 * >> 26 * http://www.sgi.com >> 27 * >> 28 * For further information regarding this notice, see: >> 29 * >> 30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/ 6 */ 31 */ >> 32 7 #include "xfs.h" 33 #include "xfs.h" 8 #include "xfs_fs.h" !! 34 #include "xfs_macros.h" 9 #include "xfs_shared.h" !! 35 #include "xfs_types.h" 10 #include "xfs_format.h" !! 36 #include "xfs_inum.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" 37 #include "xfs_log.h" 19 #include "xfs_log_priv.h" !! 38 #include "xfs_trans.h" 20 #include "xfs_trace.h" !! 39 #include "xfs_sb.h" >> 40 #include "xfs_ag.h" >> 41 #include "xfs_dir.h" >> 42 #include "xfs_dir2.h" >> 43 #include "xfs_dmapi.h" >> 44 #include "xfs_mount.h" 21 #include "xfs_error.h" 45 #include "xfs_error.h" 22 #include "xfs_defer.h" !! 46 #include "xfs_trans_priv.h" >> 47 #include "xfs_alloc_btree.h" >> 48 #include "xfs_bmap_btree.h" >> 49 #include "xfs_ialloc_btree.h" >> 50 #include "xfs_btree.h" >> 51 #include "xfs_ialloc.h" >> 52 #include "xfs_alloc.h" >> 53 #include "xfs_attr_sf.h" >> 54 #include "xfs_dir_sf.h" >> 55 #include "xfs_dir2_sf.h" >> 56 #include "xfs_dinode.h" 23 #include "xfs_inode.h" 57 #include "xfs_inode.h" 24 #include "xfs_dquot_item.h" !! 58 #include "xfs_bmap.h" 25 #include "xfs_dquot.h" !! 59 #include "xfs_da_btree.h" 26 #include "xfs_icache.h" !! 60 #include "xfs_quota.h" 27 #include "xfs_rtbitmap.h" !! 61 #include "xfs_trans_space.h" 28 !! 62 29 struct kmem_cache *xfs_trans_cache; !! 63 30 !! 64 STATIC void xfs_trans_apply_sb_deltas(xfs_trans_t *); 31 #if defined(CONFIG_TRACEPOINTS) !! 65 STATIC uint xfs_trans_count_vecs(xfs_trans_t *); 32 static void !! 66 STATIC void xfs_trans_fill_vecs(xfs_trans_t *, xfs_log_iovec_t *); 33 xfs_trans_trace_reservations( !! 67 STATIC void xfs_trans_uncommit(xfs_trans_t *, uint); 34 struct xfs_mount *mp) !! 68 STATIC void xfs_trans_committed(xfs_trans_t *, int); 35 { !! 69 STATIC void xfs_trans_chunk_committed(xfs_log_item_chunk_t *, xfs_lsn_t, int); 36 struct xfs_trans_res *res; !! 70 STATIC void xfs_trans_free(xfs_trans_t *); 37 struct xfs_trans_res *end_res; !! 71 38 int i; !! 72 kmem_zone_t *xfs_trans_zone; 39 73 40 res = (struct xfs_trans_res *)M_RES(mp << 41 end_res = (struct xfs_trans_res *)(M_R << 42 for (i = 0; res < end_res; i++, res++) << 43 trace_xfs_trans_resv_calc(mp, << 44 } << 45 #else << 46 # define xfs_trans_trace_reservations(mp) << 47 #endif << 48 74 49 /* 75 /* 50 * Initialize the precomputed transaction rese 76 * Initialize the precomputed transaction reservation values 51 * in the mount structure. 77 * in the mount structure. 52 */ 78 */ 53 void 79 void 54 xfs_trans_init( 80 xfs_trans_init( 55 struct xfs_mount *mp) !! 81 xfs_mount_t *mp) 56 { 82 { 57 xfs_trans_resv_calc(mp, M_RES(mp)); !! 83 xfs_trans_reservations_t *resp; 58 xfs_trans_trace_reservations(mp); !! 84 >> 85 resp = &(mp->m_reservations); >> 86 resp->tr_write = >> 87 (uint)(XFS_CALC_WRITE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); >> 88 resp->tr_itruncate = >> 89 (uint)(XFS_CALC_ITRUNCATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); >> 90 resp->tr_rename = >> 91 (uint)(XFS_CALC_RENAME_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); >> 92 resp->tr_link = (uint)XFS_CALC_LINK_LOG_RES(mp); >> 93 resp->tr_remove = >> 94 (uint)(XFS_CALC_REMOVE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); >> 95 resp->tr_symlink = >> 96 (uint)(XFS_CALC_SYMLINK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); >> 97 resp->tr_create = >> 98 (uint)(XFS_CALC_CREATE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); >> 99 resp->tr_mkdir = >> 100 (uint)(XFS_CALC_MKDIR_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); >> 101 resp->tr_ifree = >> 102 (uint)(XFS_CALC_IFREE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); >> 103 resp->tr_ichange = >> 104 (uint)(XFS_CALC_ICHANGE_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); >> 105 resp->tr_growdata = (uint)XFS_CALC_GROWDATA_LOG_RES(mp); >> 106 resp->tr_swrite = (uint)XFS_CALC_SWRITE_LOG_RES(mp); >> 107 resp->tr_writeid = (uint)XFS_CALC_WRITEID_LOG_RES(mp); >> 108 resp->tr_addafork = >> 109 (uint)(XFS_CALC_ADDAFORK_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); >> 110 resp->tr_attrinval = (uint)XFS_CALC_ATTRINVAL_LOG_RES(mp); >> 111 resp->tr_attrset = >> 112 (uint)(XFS_CALC_ATTRSET_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); >> 113 resp->tr_attrrm = >> 114 (uint)(XFS_CALC_ATTRRM_LOG_RES(mp) + XFS_DQUOT_LOGRES(mp)); >> 115 resp->tr_clearagi = (uint)XFS_CALC_CLEAR_AGI_BUCKET_LOG_RES(mp); >> 116 resp->tr_growrtalloc = (uint)XFS_CALC_GROWRTALLOC_LOG_RES(mp); >> 117 resp->tr_growrtzero = (uint)XFS_CALC_GROWRTZERO_LOG_RES(mp); >> 118 resp->tr_growrtfree = (uint)XFS_CALC_GROWRTFREE_LOG_RES(mp); 59 } 119 } 60 120 61 /* 121 /* 62 * Free the transaction structure. If there i !! 122 * This routine is called to allocate a transaction structure. 63 * to do when the structure is freed, add it h !! 123 * The type parameter indicates the type of the transaction. These >> 124 * are enumerated in xfs_trans.h. >> 125 * >> 126 * Dynamically allocate the transaction structure from the transaction >> 127 * zone, initialize it, and return it to the caller. 64 */ 128 */ 65 STATIC void !! 129 xfs_trans_t * 66 xfs_trans_free( !! 130 xfs_trans_alloc( 67 struct xfs_trans *tp) !! 131 xfs_mount_t *mp, >> 132 uint type) >> 133 { >> 134 xfs_check_frozen(mp, NULL, XFS_FREEZE_TRANS); >> 135 return (_xfs_trans_alloc(mp, type)); >> 136 >> 137 } >> 138 >> 139 xfs_trans_t * >> 140 _xfs_trans_alloc( >> 141 xfs_mount_t *mp, >> 142 uint type) 68 { 143 { 69 xfs_extent_busy_sort(&tp->t_busy); !! 144 xfs_trans_t *tp; 70 xfs_extent_busy_clear(tp->t_mountp, &t !! 145 ASSERT(xfs_trans_zone != NULL); >> 146 tp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP); >> 147 tp->t_dqinfo = NULL; >> 148 >> 149 /* >> 150 * Initialize the transaction structure. >> 151 */ >> 152 tp->t_magic = XFS_TRANS_MAGIC; >> 153 tp->t_type = type; >> 154 tp->t_mountp = mp; >> 155 tp->t_items_free = XFS_LIC_NUM_SLOTS; >> 156 tp->t_busy_free = XFS_LBC_NUM_SLOTS; >> 157 XFS_LIC_INIT(&(tp->t_items)); >> 158 XFS_LBC_INIT(&(tp->t_busy)); 71 159 72 trace_xfs_trans_free(tp, _RET_IP_); !! 160 return (tp); 73 xfs_trans_clear_context(tp); << 74 if (!(tp->t_flags & XFS_TRANS_NO_WRITE << 75 sb_end_intwrite(tp->t_mountp-> << 76 xfs_trans_free_dqinfo(tp); << 77 kmem_cache_free(xfs_trans_cache, tp); << 78 } 161 } 79 162 80 /* 163 /* 81 * This is called to create a new transaction 164 * This is called to create a new transaction which will share the 82 * permanent log reservation of the given tran 165 * permanent log reservation of the given transaction. The remaining 83 * unused block and rt extent reservations are 166 * unused block and rt extent reservations are also inherited. This 84 * implies that the original transaction is no 167 * implies that the original transaction is no longer allowed to allocate 85 * blocks. Locks and log items, however, are 168 * blocks. Locks and log items, however, are no inherited. They must 86 * be added to the new transaction explicitly. 169 * be added to the new transaction explicitly. 87 */ 170 */ 88 STATIC struct xfs_trans * !! 171 xfs_trans_t * 89 xfs_trans_dup( 172 xfs_trans_dup( 90 struct xfs_trans *tp) !! 173 xfs_trans_t *tp) 91 { 174 { 92 struct xfs_trans *ntp; !! 175 xfs_trans_t *ntp; 93 << 94 trace_xfs_trans_dup(tp, _RET_IP_); << 95 176 96 ntp = kmem_cache_zalloc(xfs_trans_cach !! 177 ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP); 97 178 98 /* 179 /* 99 * Initialize the new transaction stru 180 * Initialize the new transaction structure. 100 */ 181 */ 101 ntp->t_magic = XFS_TRANS_HEADER_MAGIC; !! 182 ntp->t_magic = XFS_TRANS_MAGIC; >> 183 ntp->t_type = tp->t_type; 102 ntp->t_mountp = tp->t_mountp; 184 ntp->t_mountp = tp->t_mountp; 103 INIT_LIST_HEAD(&ntp->t_items); !! 185 ntp->t_items_free = XFS_LIC_NUM_SLOTS; 104 INIT_LIST_HEAD(&ntp->t_busy); !! 186 ntp->t_busy_free = XFS_LBC_NUM_SLOTS; 105 INIT_LIST_HEAD(&ntp->t_dfops); !! 187 XFS_LIC_INIT(&(ntp->t_items)); 106 ntp->t_highest_agno = NULLAGNUMBER; !! 188 XFS_LBC_INIT(&(ntp->t_busy)); 107 189 108 ASSERT(tp->t_flags & XFS_TRANS_PERM_LO 190 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 109 ASSERT(tp->t_ticket != NULL); << 110 191 111 ntp->t_flags = XFS_TRANS_PERM_LOG_RES !! 192 #if defined(XLOG_NOLOG) || defined(DEBUG) 112 (tp->t_flags & XFS_TRAN !! 193 ASSERT(!xlog_debug || tp->t_ticket != NULL); 113 (tp->t_flags & XFS_TRAN !! 194 #else 114 (tp->t_flags & XFS_TRAN !! 195 ASSERT(tp->t_ticket != NULL); 115 /* We gave our writer reference to the !! 196 #endif 116 tp->t_flags |= XFS_TRANS_NO_WRITECOUNT !! 197 ntp->t_flags = XFS_TRANS_PERM_LOG_RES | (tp->t_flags & XFS_TRANS_RESERVE); 117 ntp->t_ticket = xfs_log_ticket_get(tp- !! 198 ntp->t_ticket = tp->t_ticket; 118 << 119 ASSERT(tp->t_blk_res >= tp->t_blk_res_ << 120 ntp->t_blk_res = tp->t_blk_res - tp->t 199 ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used; 121 tp->t_blk_res = tp->t_blk_res_used; 200 tp->t_blk_res = tp->t_blk_res_used; 122 << 123 ntp->t_rtx_res = tp->t_rtx_res - tp->t 201 ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used; 124 tp->t_rtx_res = tp->t_rtx_res_used; 202 tp->t_rtx_res = tp->t_rtx_res_used; >> 203 PFLAGS_DUP(&tp->t_pflags, &ntp->t_pflags); 125 204 126 xfs_trans_switch_context(tp, ntp); !! 205 XFS_TRANS_DUP_DQINFO(tp->t_mountp, tp, ntp); 127 << 128 /* move deferred ops over to the new t << 129 xfs_defer_move(ntp, tp); << 130 206 131 xfs_trans_dup_dqinfo(tp, ntp); !! 207 atomic_inc(&tp->t_mountp->m_active_trans); 132 return ntp; 208 return ntp; 133 } 209 } 134 210 135 /* 211 /* 136 * This is called to reserve free disk blocks 212 * This is called to reserve free disk blocks and log space for the 137 * given transaction. This must be done befor 213 * given transaction. This must be done before allocating any resources 138 * within the transaction. 214 * within the transaction. 139 * 215 * 140 * This will return ENOSPC if there are not en 216 * This will return ENOSPC if there are not enough blocks available. 141 * It will sleep waiting for available log spa 217 * It will sleep waiting for available log space. 142 * The only valid value for the flags paramete 218 * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which 143 * is used by long running transactions. If a 219 * is used by long running transactions. If any one of the reservations 144 * fails then they will all be backed out. 220 * fails then they will all be backed out. 145 * 221 * 146 * This does not do quota reservations. That t 222 * This does not do quota reservations. That typically is done by the 147 * caller afterwards. 223 * caller afterwards. 148 */ 224 */ 149 static int !! 225 int 150 xfs_trans_reserve( 226 xfs_trans_reserve( 151 struct xfs_trans *tp, !! 227 xfs_trans_t *tp, 152 struct xfs_trans_res *resp, !! 228 uint blocks, 153 uint blocks, !! 229 uint logspace, 154 uint rtextents) !! 230 uint rtextents, 155 { !! 231 uint flags, 156 struct xfs_mount *mp = tp->t_mo !! 232 uint logcount) 157 int error = 0; !! 233 { 158 bool rsvd = (tp->t_ !! 234 int log_flags; >> 235 int error; >> 236 int rsvd; >> 237 >> 238 error = 0; >> 239 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0; >> 240 >> 241 /* Mark this thread as being in a transaction */ >> 242 PFLAGS_SET_FSTRANS(&tp->t_pflags); 159 243 160 /* 244 /* 161 * Attempt to reserve the needed disk 245 * Attempt to reserve the needed disk blocks by decrementing 162 * the number needed from the number a 246 * the number needed from the number available. This will 163 * fail if the count would go below ze 247 * fail if the count would go below zero. 164 */ 248 */ 165 if (blocks > 0) { 249 if (blocks > 0) { 166 error = xfs_dec_fdblocks(mp, b !! 250 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS, 167 if (error != 0) !! 251 -blocks, rsvd); 168 return -ENOSPC; !! 252 if (error != 0) { >> 253 PFLAGS_RESTORE(&tp->t_pflags); >> 254 return (XFS_ERROR(ENOSPC)); >> 255 } 169 tp->t_blk_res += blocks; 256 tp->t_blk_res += blocks; 170 } 257 } 171 258 172 /* 259 /* 173 * Reserve the log space needed for th 260 * Reserve the log space needed for this transaction. 174 */ 261 */ 175 if (resp->tr_logres > 0) { !! 262 if (logspace > 0) { 176 bool permanent = false; !! 263 ASSERT((tp->t_log_res == 0) || (tp->t_log_res == logspace)); 177 !! 264 ASSERT((tp->t_log_count == 0) || 178 ASSERT(tp->t_log_res == 0 || !! 265 (tp->t_log_count == logcount)); 179 tp->t_log_res == resp-> !! 266 if (flags & XFS_TRANS_PERM_LOG_RES) { 180 ASSERT(tp->t_log_count == 0 || !! 267 log_flags = XFS_LOG_PERM_RESERV; 181 tp->t_log_count == resp << 182 << 183 if (resp->tr_logflags & XFS_TR << 184 tp->t_flags |= XFS_TRA 268 tp->t_flags |= XFS_TRANS_PERM_LOG_RES; 185 permanent = true; << 186 } else { 269 } else { 187 ASSERT(tp->t_ticket == 270 ASSERT(tp->t_ticket == NULL); 188 ASSERT(!(tp->t_flags & 271 ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES)); >> 272 log_flags = 0; 189 } 273 } 190 274 191 if (tp->t_ticket != NULL) { !! 275 error = xfs_log_reserve(tp->t_mountp, logspace, logcount, 192 ASSERT(resp->tr_logfla !! 276 &tp->t_ticket, 193 error = xfs_log_regran !! 277 XFS_TRANSACTION, log_flags); 194 } else { !! 278 if (error) { 195 error = xfs_log_reserv << 196 << 197 << 198 } << 199 << 200 if (error) << 201 goto undo_blocks; 279 goto undo_blocks; 202 !! 280 } 203 tp->t_log_res = resp->tr_logre !! 281 tp->t_log_res = logspace; 204 tp->t_log_count = resp->tr_log !! 282 tp->t_log_count = logcount; 205 } 283 } 206 284 207 /* 285 /* 208 * Attempt to reserve the needed realt 286 * Attempt to reserve the needed realtime extents by decrementing 209 * the number needed from the number a 287 * the number needed from the number available. This will 210 * fail if the count would go below ze 288 * fail if the count would go below zero. 211 */ 289 */ 212 if (rtextents > 0) { 290 if (rtextents > 0) { 213 error = xfs_dec_frextents(mp, !! 291 error = xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FREXTENTS, >> 292 -rtextents, rsvd); 214 if (error) { 293 if (error) { 215 error = -ENOSPC; !! 294 error = XFS_ERROR(ENOSPC); 216 goto undo_log; 295 goto undo_log; 217 } 296 } 218 tp->t_rtx_res += rtextents; 297 tp->t_rtx_res += rtextents; 219 } 298 } 220 299 221 return 0; 300 return 0; 222 301 223 /* 302 /* 224 * Error cases jump to one of these la 303 * Error cases jump to one of these labels to undo any 225 * reservations which have already bee 304 * reservations which have already been performed. 226 */ 305 */ 227 undo_log: 306 undo_log: 228 if (resp->tr_logres > 0) { !! 307 if (logspace > 0) { 229 xfs_log_ticket_ungrant(mp->m_l !! 308 if (flags & XFS_TRANS_PERM_LOG_RES) { >> 309 log_flags = XFS_LOG_REL_PERM_RESERV; >> 310 } else { >> 311 log_flags = 0; >> 312 } >> 313 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags); 230 tp->t_ticket = NULL; 314 tp->t_ticket = NULL; 231 tp->t_log_res = 0; 315 tp->t_log_res = 0; 232 tp->t_flags &= ~XFS_TRANS_PERM 316 tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES; 233 } 317 } 234 318 235 undo_blocks: 319 undo_blocks: 236 if (blocks > 0) { 320 if (blocks > 0) { 237 xfs_add_fdblocks(mp, blocks); !! 321 (void) xfs_mod_incore_sb(tp->t_mountp, XFS_SBS_FDBLOCKS, >> 322 blocks, rsvd); 238 tp->t_blk_res = 0; 323 tp->t_blk_res = 0; 239 } 324 } 240 return error; << 241 } << 242 << 243 int << 244 xfs_trans_alloc( << 245 struct xfs_mount *mp, << 246 struct xfs_trans_res *resp, << 247 uint blocks, << 248 uint rtextents, << 249 uint flags, << 250 struct xfs_trans **tpp) << 251 { << 252 struct xfs_trans *tp; << 253 bool want_retry = t << 254 int error; << 255 << 256 /* << 257 * Allocate the handle before we do ou << 258 * GFP_NOFS allocation context so that << 259 * by doing GFP_KERNEL allocations ins << 260 */ << 261 retry: << 262 tp = kmem_cache_zalloc(xfs_trans_cache << 263 if (!(flags & XFS_TRANS_NO_WRITECOUNT) << 264 sb_start_intwrite(mp->m_super) << 265 xfs_trans_set_context(tp); << 266 << 267 /* << 268 * Zero-reservation ("empty") transact << 269 * they're allowed to run while we're << 270 */ << 271 WARN_ON(resp->tr_logres > 0 && << 272 mp->m_super->s_writers.frozen << 273 ASSERT(!(flags & XFS_TRANS_RES_FDBLKS) << 274 xfs_has_lazysbcount(mp)); << 275 << 276 tp->t_magic = XFS_TRANS_HEADER_MAGIC; << 277 tp->t_flags = flags; << 278 tp->t_mountp = mp; << 279 INIT_LIST_HEAD(&tp->t_items); << 280 INIT_LIST_HEAD(&tp->t_busy); << 281 INIT_LIST_HEAD(&tp->t_dfops); << 282 tp->t_highest_agno = NULLAGNUMBER; << 283 << 284 error = xfs_trans_reserve(tp, resp, bl << 285 if (error == -ENOSPC && want_retry) { << 286 xfs_trans_cancel(tp); << 287 << 288 /* << 289 * We weren't able to reserve << 290 * Flush the other speculative << 291 * Do not perform a synchronou << 292 * other locks. << 293 */ << 294 error = xfs_blockgc_flush_all( << 295 if (error) << 296 return error; << 297 want_retry = false; << 298 goto retry; << 299 } << 300 if (error) { << 301 xfs_trans_cancel(tp); << 302 return error; << 303 } << 304 325 305 trace_xfs_trans_alloc(tp, _RET_IP_); !! 326 PFLAGS_RESTORE(&tp->t_pflags); 306 327 307 *tpp = tp; !! 328 return (error); 308 return 0; << 309 } 329 } 310 330 >> 331 311 /* 332 /* 312 * Create an empty transaction with no reserva !! 333 * This is called to set the a callback to be called when the given 313 * mechanism for routines that query metadata !! 334 * transaction is committed to disk. The transaction pointer and the 314 * if the metadata being queried is somehow cr !! 335 * argument pointer will be passed to the callback routine. 315 * pointer that points higher in the tree), we !! 336 * 316 * grabbed as part of a transaction can be re- !! 337 * Only one callback can be associated with any single transaction. 317 * notice the corrupt block and the operation << 318 * without deadlocking. << 319 * << 320 * Note the zero-length reservation; this tran << 321 * any dirty data. << 322 * << 323 * Callers should obtain freeze protection to << 324 * where we can be grabbing buffers at the sam << 325 * drain the buffer LRU list. << 326 */ 338 */ 327 int !! 339 void 328 xfs_trans_alloc_empty( !! 340 xfs_trans_callback( 329 struct xfs_mount *mp, !! 341 xfs_trans_t *tp, 330 struct xfs_trans **tpp) !! 342 xfs_trans_callback_t callback, 331 { !! 343 void *arg) 332 struct xfs_trans_res resv = !! 344 { 333 !! 345 ASSERT(tp->t_callback == NULL); 334 return xfs_trans_alloc(mp, &resv, 0, 0 !! 346 tp->t_callback = callback; >> 347 tp->t_callarg = arg; 335 } 348 } 336 349 >> 350 337 /* 351 /* 338 * Record the indicated change to the given fi 352 * Record the indicated change to the given field for application 339 * to the file system's superblock when the tr 353 * to the file system's superblock when the transaction commits. 340 * For now, just store the change in the trans 354 * For now, just store the change in the transaction structure. 341 * 355 * 342 * Mark the transaction structure to indicate 356 * Mark the transaction structure to indicate that the superblock 343 * needs to be updated before committing. 357 * needs to be updated before committing. 344 * << 345 * Because we may not be keeping track of allo << 346 * used filesystem blocks in the superblock, w << 347 * superblock dirty in this transaction if we << 348 * We still need to update the transaction del << 349 * applied to the incore superblock, but we do << 350 * cause the superblock to get locked and logg << 351 * only fields in the superblock that the tran << 352 */ 358 */ 353 void 359 void 354 xfs_trans_mod_sb( 360 xfs_trans_mod_sb( 355 xfs_trans_t *tp, 361 xfs_trans_t *tp, 356 uint field, 362 uint field, 357 int64_t delta) !! 363 long delta) 358 { 364 { 359 uint32_t flags = (XFS_TRANS_DIR << 360 xfs_mount_t *mp = tp->t_mountp; << 361 365 362 switch (field) { 366 switch (field) { 363 case XFS_TRANS_SB_ICOUNT: 367 case XFS_TRANS_SB_ICOUNT: 364 tp->t_icount_delta += delta; 368 tp->t_icount_delta += delta; 365 if (xfs_has_lazysbcount(mp)) << 366 flags &= ~XFS_TRANS_SB << 367 break; 369 break; 368 case XFS_TRANS_SB_IFREE: 370 case XFS_TRANS_SB_IFREE: 369 tp->t_ifree_delta += delta; 371 tp->t_ifree_delta += delta; 370 if (xfs_has_lazysbcount(mp)) << 371 flags &= ~XFS_TRANS_SB << 372 break; 372 break; 373 case XFS_TRANS_SB_FDBLOCKS: 373 case XFS_TRANS_SB_FDBLOCKS: 374 /* 374 /* 375 * Track the number of blocks !! 375 * Track the number of blocks allocated in the 376 * Make sure it does not excee !! 376 * transaction. Make sure it does not exceed the 377 * shutdown as this can lead t !! 377 * number reserved. 378 */ 378 */ 379 if (delta < 0) { 379 if (delta < 0) { 380 tp->t_blk_res_used += 380 tp->t_blk_res_used += (uint)-delta; 381 if (tp->t_blk_res_used !! 381 ASSERT(tp->t_blk_res_used <= tp->t_blk_res); 382 xfs_force_shut << 383 } else if (delta > 0 && (tp->t << 384 int64_t blkres_delta; << 385 << 386 /* << 387 * Return freed blocks << 388 * instead of the glob << 389 * overflow the trans << 390 * reservation across << 391 * repeatedly free and << 392 */ << 393 blkres_delta = min_t(i << 394 U << 395 tp->t_blk_res += blkre << 396 delta -= blkres_delta; << 397 } 382 } 398 tp->t_fdblocks_delta += delta; 383 tp->t_fdblocks_delta += delta; 399 if (xfs_has_lazysbcount(mp)) << 400 flags &= ~XFS_TRANS_SB << 401 break; 384 break; 402 case XFS_TRANS_SB_RES_FDBLOCKS: 385 case XFS_TRANS_SB_RES_FDBLOCKS: 403 /* 386 /* 404 * The allocation has already 387 * The allocation has already been applied to the 405 * in-core superblock's counte 388 * in-core superblock's counter. This should only 406 * be applied to the on-disk s 389 * be applied to the on-disk superblock. 407 */ 390 */ >> 391 ASSERT(delta < 0); 408 tp->t_res_fdblocks_delta += de 392 tp->t_res_fdblocks_delta += delta; 409 if (xfs_has_lazysbcount(mp)) << 410 flags &= ~XFS_TRANS_SB << 411 break; 393 break; 412 case XFS_TRANS_SB_FREXTENTS: 394 case XFS_TRANS_SB_FREXTENTS: 413 /* 395 /* 414 * Track the number of blocks 396 * Track the number of blocks allocated in the 415 * transaction. Make sure it 397 * transaction. Make sure it does not exceed the 416 * number reserved. 398 * number reserved. 417 */ 399 */ 418 if (delta < 0) { 400 if (delta < 0) { 419 tp->t_rtx_res_used += 401 tp->t_rtx_res_used += (uint)-delta; 420 ASSERT(tp->t_rtx_res_u 402 ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res); 421 } 403 } 422 tp->t_frextents_delta += delta 404 tp->t_frextents_delta += delta; 423 break; 405 break; 424 case XFS_TRANS_SB_RES_FREXTENTS: 406 case XFS_TRANS_SB_RES_FREXTENTS: 425 /* 407 /* 426 * The allocation has already 408 * The allocation has already been applied to the 427 * in-core superblock's counte !! 409 * in-core superblocks's counter. This should only 428 * be applied to the on-disk s 410 * be applied to the on-disk superblock. 429 */ 411 */ 430 ASSERT(delta < 0); 412 ASSERT(delta < 0); 431 tp->t_res_frextents_delta += d 413 tp->t_res_frextents_delta += delta; 432 break; 414 break; 433 case XFS_TRANS_SB_DBLOCKS: 415 case XFS_TRANS_SB_DBLOCKS: >> 416 ASSERT(delta > 0); 434 tp->t_dblocks_delta += delta; 417 tp->t_dblocks_delta += delta; 435 break; 418 break; 436 case XFS_TRANS_SB_AGCOUNT: 419 case XFS_TRANS_SB_AGCOUNT: 437 ASSERT(delta > 0); 420 ASSERT(delta > 0); 438 tp->t_agcount_delta += delta; 421 tp->t_agcount_delta += delta; 439 break; 422 break; 440 case XFS_TRANS_SB_IMAXPCT: 423 case XFS_TRANS_SB_IMAXPCT: 441 tp->t_imaxpct_delta += delta; 424 tp->t_imaxpct_delta += delta; 442 break; 425 break; 443 case XFS_TRANS_SB_REXTSIZE: 426 case XFS_TRANS_SB_REXTSIZE: 444 tp->t_rextsize_delta += delta; 427 tp->t_rextsize_delta += delta; 445 break; 428 break; 446 case XFS_TRANS_SB_RBMBLOCKS: 429 case XFS_TRANS_SB_RBMBLOCKS: 447 tp->t_rbmblocks_delta += delta 430 tp->t_rbmblocks_delta += delta; 448 break; 431 break; 449 case XFS_TRANS_SB_RBLOCKS: 432 case XFS_TRANS_SB_RBLOCKS: 450 tp->t_rblocks_delta += delta; 433 tp->t_rblocks_delta += delta; 451 break; 434 break; 452 case XFS_TRANS_SB_REXTENTS: 435 case XFS_TRANS_SB_REXTENTS: 453 tp->t_rextents_delta += delta; 436 tp->t_rextents_delta += delta; 454 break; 437 break; 455 case XFS_TRANS_SB_REXTSLOG: 438 case XFS_TRANS_SB_REXTSLOG: 456 tp->t_rextslog_delta += delta; 439 tp->t_rextslog_delta += delta; 457 break; 440 break; 458 default: 441 default: 459 ASSERT(0); 442 ASSERT(0); 460 return; 443 return; 461 } 444 } 462 445 463 tp->t_flags |= flags; !! 446 tp->t_flags |= (XFS_TRANS_SB_DIRTY | XFS_TRANS_DIRTY); 464 } 447 } 465 448 466 /* 449 /* 467 * xfs_trans_apply_sb_deltas() is called from 450 * xfs_trans_apply_sb_deltas() is called from the commit code 468 * to bring the superblock buffer into the cur 451 * to bring the superblock buffer into the current transaction 469 * and modify it as requested by earlier calls 452 * and modify it as requested by earlier calls to xfs_trans_mod_sb(). 470 * 453 * 471 * For now we just look at each field allowed 454 * For now we just look at each field allowed to change and change 472 * it if necessary. 455 * it if necessary. 473 */ 456 */ 474 STATIC void 457 STATIC void 475 xfs_trans_apply_sb_deltas( 458 xfs_trans_apply_sb_deltas( 476 xfs_trans_t *tp) 459 xfs_trans_t *tp) 477 { 460 { 478 struct xfs_dsb *sbp; !! 461 xfs_sb_t *sbp; 479 struct xfs_buf *bp; !! 462 xfs_buf_t *bp; 480 int whole = 0; 463 int whole = 0; 481 464 482 bp = xfs_trans_getsb(tp); !! 465 bp = xfs_trans_getsb(tp, tp->t_mountp, 0); 483 sbp = bp->b_addr; !! 466 sbp = XFS_BUF_TO_SBP(bp); 484 467 485 /* 468 /* 486 * Only update the superblock counters !! 469 * Check that superblock mods match the mods made to AGF counters. 487 */ << 488 if (!xfs_has_lazysbcount((tp->t_mountp << 489 if (tp->t_icount_delta) << 490 be64_add_cpu(&sbp->sb_ << 491 if (tp->t_ifree_delta) << 492 be64_add_cpu(&sbp->sb_ << 493 if (tp->t_fdblocks_delta) << 494 be64_add_cpu(&sbp->sb_ << 495 if (tp->t_res_fdblocks_delta) << 496 be64_add_cpu(&sbp->sb_ << 497 } << 498 << 499 /* << 500 * Updating frextents requires careful << 501 * behave like the lazysb counters bec << 502 * recovery in older kenels to recompu << 503 * This means that the ondisk frextent << 504 * rtbitmap. << 505 * << 506 * Therefore, log the frextents change << 507 * update the incore superblock so tha << 508 * write the correct value ondisk. << 509 * << 510 * Don't touch m_frextents because it << 511 * and those are handled by the unrese << 512 */ 470 */ 513 if (tp->t_frextents_delta || tp->t_res !! 471 ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) == 514 struct xfs_mount *mp = !! 472 (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta + 515 int64_t rtxdel !! 473 tp->t_ag_btree_delta)); 516 474 517 rtxdelta = tp->t_frextents_del !! 475 if (tp->t_icount_delta != 0) { >> 476 INT_MOD(sbp->sb_icount, ARCH_CONVERT, tp->t_icount_delta); >> 477 } >> 478 if (tp->t_ifree_delta != 0) { >> 479 INT_MOD(sbp->sb_ifree, ARCH_CONVERT, tp->t_ifree_delta); >> 480 } 518 481 519 spin_lock(&mp->m_sb_lock); !! 482 if (tp->t_fdblocks_delta != 0) { 520 be64_add_cpu(&sbp->sb_frextent !! 483 INT_MOD(sbp->sb_fdblocks, ARCH_CONVERT, tp->t_fdblocks_delta); 521 mp->m_sb.sb_frextents += rtxde !! 484 } 522 spin_unlock(&mp->m_sb_lock); !! 485 if (tp->t_res_fdblocks_delta != 0) { >> 486 INT_MOD(sbp->sb_fdblocks, ARCH_CONVERT, tp->t_res_fdblocks_delta); 523 } 487 } 524 488 525 if (tp->t_dblocks_delta) { !! 489 if (tp->t_frextents_delta != 0) { 526 be64_add_cpu(&sbp->sb_dblocks, !! 490 INT_MOD(sbp->sb_frextents, ARCH_CONVERT, tp->t_frextents_delta); >> 491 } >> 492 if (tp->t_dblocks_delta != 0) { >> 493 INT_MOD(sbp->sb_dblocks, ARCH_CONVERT, tp->t_dblocks_delta); 527 whole = 1; 494 whole = 1; 528 } 495 } 529 if (tp->t_agcount_delta) { !! 496 if (tp->t_agcount_delta != 0) { 530 be32_add_cpu(&sbp->sb_agcount, !! 497 INT_MOD(sbp->sb_agcount, ARCH_CONVERT, tp->t_agcount_delta); 531 whole = 1; 498 whole = 1; 532 } 499 } 533 if (tp->t_imaxpct_delta) { !! 500 if (tp->t_imaxpct_delta != 0) { 534 sbp->sb_imax_pct += tp->t_imax !! 501 INT_MOD(sbp->sb_imax_pct, ARCH_CONVERT, tp->t_imaxpct_delta); 535 whole = 1; 502 whole = 1; 536 } 503 } 537 if (tp->t_rextsize_delta) { !! 504 if (tp->t_rextsize_delta != 0) { 538 be32_add_cpu(&sbp->sb_rextsize !! 505 INT_MOD(sbp->sb_rextsize, ARCH_CONVERT, tp->t_rextsize_delta); 539 whole = 1; 506 whole = 1; 540 } 507 } 541 if (tp->t_rbmblocks_delta) { !! 508 if (tp->t_rbmblocks_delta != 0) { 542 be32_add_cpu(&sbp->sb_rbmblock !! 509 INT_MOD(sbp->sb_rbmblocks, ARCH_CONVERT, tp->t_rbmblocks_delta); 543 whole = 1; 510 whole = 1; 544 } 511 } 545 if (tp->t_rblocks_delta) { !! 512 if (tp->t_rblocks_delta != 0) { 546 be64_add_cpu(&sbp->sb_rblocks, !! 513 INT_MOD(sbp->sb_rblocks, ARCH_CONVERT, tp->t_rblocks_delta); 547 whole = 1; 514 whole = 1; 548 } 515 } 549 if (tp->t_rextents_delta) { !! 516 if (tp->t_rextents_delta != 0) { 550 be64_add_cpu(&sbp->sb_rextents !! 517 INT_MOD(sbp->sb_rextents, ARCH_CONVERT, tp->t_rextents_delta); 551 whole = 1; 518 whole = 1; 552 } 519 } 553 if (tp->t_rextslog_delta) { !! 520 if (tp->t_rextslog_delta != 0) { 554 sbp->sb_rextslog += tp->t_rext !! 521 INT_MOD(sbp->sb_rextslog, ARCH_CONVERT, tp->t_rextslog_delta); 555 whole = 1; 522 whole = 1; 556 } 523 } 557 524 558 xfs_trans_buf_set_type(tp, bp, XFS_BLF << 559 if (whole) 525 if (whole) 560 /* 526 /* 561 * Log the whole thing, the fi !! 527 * Log the whole thing, the fields are discontiguous. 562 */ 528 */ 563 xfs_trans_log_buf(tp, bp, 0, s !! 529 xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_sb_t) - 1); 564 else 530 else 565 /* 531 /* 566 * Since all the modifiable fi 532 * Since all the modifiable fields are contiguous, we 567 * can get away with this. 533 * can get away with this. 568 */ 534 */ 569 xfs_trans_log_buf(tp, bp, offs !! 535 xfs_trans_log_buf(tp, bp, offsetof(xfs_sb_t, sb_icount), 570 offsetof(str !! 536 offsetof(xfs_sb_t, sb_frextents) + 571 sizeof(sbp-> 537 sizeof(sbp->sb_frextents) - 1); >> 538 >> 539 XFS_MTOVFS(tp->t_mountp)->vfs_super->s_dirt = 1; 572 } 540 } 573 541 574 /* 542 /* 575 * xfs_trans_unreserve_and_mod_sb() is called !! 543 * xfs_trans_unreserve_and_mod_sb() is called to release unused 576 * apply superblock counter changes to the in- !! 544 * reservations and apply superblock counter changes to the in-core 577 * t_res_fdblocks_delta and t_res_frextents_de !! 545 * superblock. 578 * applied to the in-core superblock. The ide << 579 * done. << 580 * << 581 * If we are not logging superblock counters, << 582 * used block counts are not updated in the on << 583 * XFS_TRANS_SB_DIRTY will not be set when the << 584 * still need to update the incore superblock << 585 * 546 * 586 * Deltas for the inode count are +/-64, hence !! 547 * This is done efficiently with a single call to xfs_mod_incore_sb_batch(). 587 * so we don't need to take the counter lock o << 588 */ 548 */ 589 #define XFS_ICOUNT_BATCH 128 << 590 << 591 void 549 void 592 xfs_trans_unreserve_and_mod_sb( 550 xfs_trans_unreserve_and_mod_sb( 593 struct xfs_trans *tp) !! 551 xfs_trans_t *tp) 594 { 552 { 595 struct xfs_mount *mp = tp->t_mo !! 553 xfs_mod_sb_t msb[14]; /* If you add cases, add entries */ 596 int64_t blkdelta = tp- !! 554 xfs_mod_sb_t *msbp; 597 int64_t rtxdelta = tp- !! 555 /* REFERENCED */ 598 int64_t idelta = 0; !! 556 int error; 599 int64_t ifreedelta = 0 !! 557 int rsvd; 600 << 601 /* << 602 * Calculate the deltas. << 603 * << 604 * t_fdblocks_delta and t_frextents_de << 605 * << 606 * - positive values indicate blocks << 607 * - negative values indicate blocks << 608 * << 609 * Negative values can only happen if << 610 * reservation that covers the allocat << 611 * that the calculated delta values mu << 612 * can only put back previous allocate << 613 */ << 614 ASSERT(tp->t_blk_res || tp->t_fdblocks << 615 if (xfs_has_lazysbcount(mp) || (tp->t_ << 616 blkdelta += tp->t_fdblocks_del << 617 ASSERT(blkdelta >= 0); << 618 } << 619 << 620 ASSERT(tp->t_rtx_res || tp->t_frextent << 621 if (tp->t_flags & XFS_TRANS_SB_DIRTY) << 622 rtxdelta += tp->t_frextents_de << 623 ASSERT(rtxdelta >= 0); << 624 } << 625 << 626 if (xfs_has_lazysbcount(mp) || (tp->t_ << 627 idelta = tp->t_icount_delta; << 628 ifreedelta = tp->t_ifree_delta << 629 } << 630 558 631 /* apply the per-cpu counters */ !! 559 msbp = msb; 632 if (blkdelta) !! 560 rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0; 633 xfs_add_fdblocks(mp, blkdelta) << 634 << 635 if (idelta) << 636 percpu_counter_add_batch(&mp-> << 637 XFS_I << 638 << 639 if (ifreedelta) << 640 percpu_counter_add(&mp->m_ifre << 641 << 642 if (rtxdelta) << 643 xfs_add_frextents(mp, rtxdelta << 644 << 645 if (!(tp->t_flags & XFS_TRANS_SB_DIRTY << 646 return; << 647 << 648 /* apply remaining deltas */ << 649 spin_lock(&mp->m_sb_lock); << 650 mp->m_sb.sb_fdblocks += tp->t_fdblocks << 651 mp->m_sb.sb_icount += idelta; << 652 mp->m_sb.sb_ifree += ifreedelta; << 653 /* << 654 * Do not touch sb_frextents here beca << 655 * reservation. sb_frextents is not p << 656 * must be consistent with the ondisk << 657 * incore reservations. << 658 */ << 659 mp->m_sb.sb_dblocks += tp->t_dblocks_d << 660 mp->m_sb.sb_agcount += tp->t_agcount_d << 661 mp->m_sb.sb_imax_pct += tp->t_imaxpct_ << 662 mp->m_sb.sb_rextsize += tp->t_rextsize << 663 if (tp->t_rextsize_delta) { << 664 mp->m_rtxblklog = log2_if_powe << 665 mp->m_rtxblkmask = mask64_if_p << 666 } << 667 mp->m_sb.sb_rbmblocks += tp->t_rbmbloc << 668 mp->m_sb.sb_rblocks += tp->t_rblocks_d << 669 mp->m_sb.sb_rextents += tp->t_rextents << 670 mp->m_sb.sb_rextslog += tp->t_rextslog << 671 spin_unlock(&mp->m_sb_lock); << 672 561 673 /* 562 /* 674 * Debug checks outside of the spinloc !! 563 * Release any reserved blocks. Any that were allocated 675 * machine if they fail. !! 564 * will be taken back again by fdblocks_delta below. 676 */ 565 */ 677 ASSERT(mp->m_sb.sb_imax_pct >= 0); !! 566 if (tp->t_blk_res > 0) { 678 ASSERT(mp->m_sb.sb_rextslog >= 0); !! 567 msbp->msb_field = XFS_SBS_FDBLOCKS; 679 } !! 568 msbp->msb_delta = tp->t_blk_res; 680 !! 569 msbp++; 681 /* Add the given log item to the transaction's << 682 void << 683 xfs_trans_add_item( << 684 struct xfs_trans *tp, << 685 struct xfs_log_item *lip) << 686 { << 687 ASSERT(lip->li_log == tp->t_mountp->m_ << 688 ASSERT(lip->li_ailp == tp->t_mountp->m << 689 ASSERT(list_empty(&lip->li_trans)); << 690 ASSERT(!test_bit(XFS_LI_DIRTY, &lip->l << 691 << 692 list_add_tail(&lip->li_trans, &tp->t_i << 693 trace_xfs_trans_add_item(tp, _RET_IP_) << 694 } << 695 << 696 /* << 697 * Unlink the log item from the transaction. t << 698 * considered dirty in this transaction, as th << 699 * finished, either by abort or commit complet << 700 */ << 701 void << 702 xfs_trans_del_item( << 703 struct xfs_log_item *lip) << 704 { << 705 clear_bit(XFS_LI_DIRTY, &lip->li_flags << 706 list_del_init(&lip->li_trans); << 707 } << 708 << 709 /* Detach and unlock all of the items in a tra << 710 static void << 711 xfs_trans_free_items( << 712 struct xfs_trans *tp, << 713 bool abort) << 714 { << 715 struct xfs_log_item *lip, *next; << 716 << 717 trace_xfs_trans_free_items(tp, _RET_IP << 718 << 719 list_for_each_entry_safe(lip, next, &t << 720 xfs_trans_del_item(lip); << 721 if (abort) << 722 set_bit(XFS_LI_ABORTED << 723 if (lip->li_ops->iop_release) << 724 lip->li_ops->iop_relea << 725 } 570 } 726 } << 727 << 728 /* << 729 * Sort transaction items prior to running pre << 730 * attempt to order the items such that they w << 731 * order. Items that have no sort function are << 732 * and so are locked last. << 733 * << 734 * This may need refinement as different types << 735 * << 736 * Function is more complex than it needs to b << 737 * values and the function only returns 32 bit << 738 */ << 739 static int << 740 xfs_trans_precommit_sort( << 741 void *unused_arg, << 742 const struct list_head *a, << 743 const struct list_head *b) << 744 { << 745 struct xfs_log_item *lia = contain << 746 struct << 747 struct xfs_log_item *lib = contain << 748 struct << 749 int64_t diff; << 750 571 751 /* 572 /* 752 * If both items are non-sortable, lea !! 573 * Release any reserved real time extents . Any that were 753 * sortable, move the non-sortable ite !! 574 * allocated will be taken back again by frextents_delta below. 754 */ 575 */ 755 if (!lia->li_ops->iop_sort && !lib->li !! 576 if (tp->t_rtx_res > 0) { 756 return 0; !! 577 msbp->msb_field = XFS_SBS_FREXTENTS; 757 if (!lia->li_ops->iop_sort) !! 578 msbp->msb_delta = tp->t_rtx_res; 758 return 1; !! 579 msbp++; 759 if (!lib->li_ops->iop_sort) !! 580 } 760 return -1; << 761 << 762 diff = lia->li_ops->iop_sort(lia) - li << 763 if (diff < 0) << 764 return -1; << 765 if (diff > 0) << 766 return 1; << 767 return 0; << 768 } << 769 << 770 /* << 771 * Run transaction precommit functions. << 772 * << 773 * If there is an error in any of the callouts << 774 * trigger a shutdown to abort the transaction << 775 * from errors at this point as the transactio << 776 */ << 777 static int << 778 xfs_trans_run_precommits( << 779 struct xfs_trans *tp) << 780 { << 781 struct xfs_mount *mp = tp->t_mo << 782 struct xfs_log_item *lip, *n; << 783 int error = 0; << 784 581 785 /* 582 /* 786 * Sort the item list to avoid ABBA de !! 583 * Apply any superblock modifications to the in-core version. 787 * running precommit operations that l !! 584 * The t_res_fdblocks_delta and t_res_frextents_delta fields are 788 * inode cluster buffers. !! 585 * explicity NOT applied to the in-core superblock. >> 586 * The idea is that that has already been done. 789 */ 587 */ 790 list_sort(NULL, &tp->t_items, xfs_tran !! 588 if (tp->t_flags & XFS_TRANS_SB_DIRTY) { >> 589 if (tp->t_icount_delta != 0) { >> 590 msbp->msb_field = XFS_SBS_ICOUNT; >> 591 msbp->msb_delta = (int)tp->t_icount_delta; >> 592 msbp++; >> 593 } >> 594 if (tp->t_ifree_delta != 0) { >> 595 msbp->msb_field = XFS_SBS_IFREE; >> 596 msbp->msb_delta = (int)tp->t_ifree_delta; >> 597 msbp++; >> 598 } >> 599 if (tp->t_fdblocks_delta != 0) { >> 600 msbp->msb_field = XFS_SBS_FDBLOCKS; >> 601 msbp->msb_delta = (int)tp->t_fdblocks_delta; >> 602 msbp++; >> 603 } >> 604 if (tp->t_frextents_delta != 0) { >> 605 msbp->msb_field = XFS_SBS_FREXTENTS; >> 606 msbp->msb_delta = (int)tp->t_frextents_delta; >> 607 msbp++; >> 608 } >> 609 if (tp->t_dblocks_delta != 0) { >> 610 msbp->msb_field = XFS_SBS_DBLOCKS; >> 611 msbp->msb_delta = (int)tp->t_dblocks_delta; >> 612 msbp++; >> 613 } >> 614 if (tp->t_agcount_delta != 0) { >> 615 msbp->msb_field = XFS_SBS_AGCOUNT; >> 616 msbp->msb_delta = (int)tp->t_agcount_delta; >> 617 msbp++; >> 618 } >> 619 if (tp->t_imaxpct_delta != 0) { >> 620 msbp->msb_field = XFS_SBS_IMAX_PCT; >> 621 msbp->msb_delta = (int)tp->t_imaxpct_delta; >> 622 msbp++; >> 623 } >> 624 if (tp->t_rextsize_delta != 0) { >> 625 msbp->msb_field = XFS_SBS_REXTSIZE; >> 626 msbp->msb_delta = (int)tp->t_rextsize_delta; >> 627 msbp++; >> 628 } >> 629 if (tp->t_rbmblocks_delta != 0) { >> 630 msbp->msb_field = XFS_SBS_RBMBLOCKS; >> 631 msbp->msb_delta = (int)tp->t_rbmblocks_delta; >> 632 msbp++; >> 633 } >> 634 if (tp->t_rblocks_delta != 0) { >> 635 msbp->msb_field = XFS_SBS_RBLOCKS; >> 636 msbp->msb_delta = (int)tp->t_rblocks_delta; >> 637 msbp++; >> 638 } >> 639 if (tp->t_rextents_delta != 0) { >> 640 msbp->msb_field = XFS_SBS_REXTENTS; >> 641 msbp->msb_delta = (int)tp->t_rextents_delta; >> 642 msbp++; >> 643 } >> 644 if (tp->t_rextslog_delta != 0) { >> 645 msbp->msb_field = XFS_SBS_REXTSLOG; >> 646 msbp->msb_delta = (int)tp->t_rextslog_delta; >> 647 msbp++; >> 648 } >> 649 } 791 650 792 /* 651 /* 793 * Precommit operations can remove the !! 652 * If we need to change anything, do it. 794 * if the log item exists purely to de << 795 * can be ordered against other operat << 796 * list_for_each_entry_safe() here. << 797 */ 653 */ 798 list_for_each_entry_safe(lip, n, &tp-> !! 654 if (msbp > msb) { 799 if (!test_bit(XFS_LI_DIRTY, &l !! 655 error = xfs_mod_incore_sb_batch(tp->t_mountp, msb, 800 continue; !! 656 (uint)(msbp - msb), rsvd); 801 if (lip->li_ops->iop_precommit !! 657 ASSERT(error == 0); 802 error = lip->li_ops->i << 803 if (error) << 804 break; << 805 } << 806 } 658 } 807 if (error) << 808 xfs_force_shutdown(mp, SHUTDOW << 809 return error; << 810 } 659 } 811 660 >> 661 812 /* 662 /* 813 * Commit the given transaction to the log. !! 663 * xfs_trans_commit >> 664 * >> 665 * Commit the given transaction to the log a/synchronously. 814 * 666 * 815 * XFS disk error handling mechanism is not ba 667 * XFS disk error handling mechanism is not based on a typical 816 * transaction abort mechanism. Logically afte 668 * transaction abort mechanism. Logically after the filesystem 817 * gets marked 'SHUTDOWN', we can't let any ne 669 * gets marked 'SHUTDOWN', we can't let any new transactions 818 * be durable - ie. committed to disk - becaus 670 * be durable - ie. committed to disk - because some metadata might 819 * be inconsistent. In such cases, this return 671 * be inconsistent. In such cases, this returns an error, and the 820 * caller may assume that all locked objects j 672 * caller may assume that all locked objects joined to the transaction 821 * have already been unlocked as if the commit 673 * have already been unlocked as if the commit had succeeded. 822 * Do not reference the transaction structure 674 * Do not reference the transaction structure after this call. 823 */ 675 */ 824 static int !! 676 /*ARGSUSED*/ 825 __xfs_trans_commit( !! 677 int 826 struct xfs_trans *tp, !! 678 xfs_trans_commit( 827 bool regrant) !! 679 xfs_trans_t *tp, >> 680 uint flags, >> 681 xfs_lsn_t *commit_lsn_p) 828 { 682 { 829 struct xfs_mount *mp = tp->t_mo !! 683 xfs_log_iovec_t *log_vector; 830 struct xlog *log = mp->m_l !! 684 int nvec; 831 xfs_csn_t commit_seq = 0 !! 685 xfs_mount_t *mp; 832 int error = 0; !! 686 xfs_lsn_t commit_lsn; 833 int sync = tp->t_f !! 687 /* REFERENCED */ 834 !! 688 int error; 835 trace_xfs_trans_commit(tp, _RET_IP_); !! 689 int log_flags; 836 !! 690 int sync; 837 error = xfs_trans_run_precommits(tp); !! 691 #define XFS_TRANS_LOGVEC_COUNT 16 838 if (error) { !! 692 xfs_log_iovec_t log_vector_fast[XFS_TRANS_LOGVEC_COUNT]; 839 if (tp->t_flags & XFS_TRANS_PE !! 693 #if defined(XLOG_NOLOG) || defined(DEBUG) 840 xfs_defer_cancel(tp); !! 694 static xfs_lsn_t trans_lsn = 1; 841 goto out_unreserve; !! 695 #endif 842 } !! 696 void *commit_iclog; >> 697 int shutdown; >> 698 >> 699 commit_lsn = -1; 843 700 844 /* 701 /* 845 * Finish deferred items on final comm !! 702 * Determine whether this commit is releasing a permanent 846 * should ever have deferred ops. !! 703 * log reservation or not. 847 */ !! 704 */ 848 WARN_ON_ONCE(!list_empty(&tp->t_dfops) !! 705 if (flags & XFS_TRANS_RELEASE_LOG_RES) { 849 !(tp->t_flags & XFS_TRANS !! 706 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); 850 if (!regrant && (tp->t_flags & XFS_TRA !! 707 log_flags = XFS_LOG_REL_PERM_RESERV; 851 error = xfs_defer_finish_norol !! 708 } else { 852 if (error) !! 709 log_flags = 0; 853 goto out_unreserve; << 854 << 855 /* Run precommits from final t << 856 error = xfs_trans_run_precommi << 857 if (error) << 858 goto out_unreserve; << 859 } 710 } >> 711 mp = tp->t_mountp; 860 712 861 /* 713 /* 862 * If there is nothing to be logged by 714 * If there is nothing to be logged by the transaction, 863 * then unlock all of the items associ 715 * then unlock all of the items associated with the 864 * transaction and free the transactio 716 * transaction and free the transaction structure. 865 * Also make sure to return any reserv 717 * Also make sure to return any reserved blocks to 866 * the free pool. 718 * the free pool. 867 */ 719 */ 868 if (!(tp->t_flags & XFS_TRANS_DIRTY)) !! 720 shut_us_down: 869 goto out_unreserve; !! 721 shutdown = XFS_FORCED_SHUTDOWN(mp) ? EIO : 0; 870 !! 722 if (!(tp->t_flags & XFS_TRANS_DIRTY) || shutdown) { 871 /* !! 723 xfs_trans_unreserve_and_mod_sb(tp); 872 * We must check against log shutdown !! 724 /* 873 * items and leave them dirty, inconsi !! 725 * It is indeed possible for the transaction to be 874 * the log is active. This leaves them !! 726 * not dirty but the dqinfo portion to be. All that 875 * disk, and that will lead to on-disk !! 727 * means is that we have some (non-persistent) quota 876 */ !! 728 * reservations that need to be unreserved. 877 if (xlog_is_shutdown(log)) { !! 729 */ 878 error = -EIO; !! 730 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(mp, tp); 879 goto out_unreserve; !! 731 if (tp->t_ticket) { >> 732 commit_lsn = xfs_log_done(mp, tp->t_ticket, >> 733 NULL, log_flags); >> 734 if (commit_lsn == -1 && !shutdown) >> 735 shutdown = XFS_ERROR(EIO); >> 736 } >> 737 PFLAGS_RESTORE(&tp->t_pflags); >> 738 xfs_trans_free_items(tp, shutdown? XFS_TRANS_ABORT : 0); >> 739 xfs_trans_free_busy(tp); >> 740 xfs_trans_free(tp); >> 741 XFS_STATS_INC(xs_trans_empty); >> 742 if (commit_lsn_p) >> 743 *commit_lsn_p = commit_lsn; >> 744 return (shutdown); 880 } 745 } 881 !! 746 #if defined(XLOG_NOLOG) || defined(DEBUG) >> 747 ASSERT(!xlog_debug || tp->t_ticket != NULL); >> 748 #else 882 ASSERT(tp->t_ticket != NULL); 749 ASSERT(tp->t_ticket != NULL); >> 750 #endif 883 751 884 /* 752 /* 885 * If we need to update the superblock 753 * If we need to update the superblock, then do it now. 886 */ 754 */ 887 if (tp->t_flags & XFS_TRANS_SB_DIRTY) !! 755 if (tp->t_flags & XFS_TRANS_SB_DIRTY) { 888 xfs_trans_apply_sb_deltas(tp); 756 xfs_trans_apply_sb_deltas(tp); 889 xfs_trans_apply_dquot_deltas(tp); !! 757 } 890 !! 758 XFS_TRANS_APPLY_DQUOT_DELTAS(mp, tp); 891 xlog_cil_commit(log, tp, &commit_seq, << 892 << 893 xfs_trans_free(tp); << 894 759 895 /* 760 /* 896 * If the transaction needs to be sync !! 761 * Ask each log item how many log_vector entries it will 897 * log out now and wait for it. !! 762 * need so we can figure out how many to allocate. >> 763 * Try to avoid the kmem_alloc() call in the common case >> 764 * by using a vector from the stack when it fits. 898 */ 765 */ 899 if (sync) { !! 766 nvec = xfs_trans_count_vecs(tp); 900 error = xfs_log_force_seq(mp, !! 767 901 XFS_STATS_INC(mp, xs_trans_syn !! 768 if (nvec == 0) { 902 } else { !! 769 xfs_force_shutdown(mp, XFS_LOG_IO_ERROR); 903 XFS_STATS_INC(mp, xs_trans_asy !! 770 goto shut_us_down; 904 } 771 } 905 772 906 return error; << 907 773 908 out_unreserve: !! 774 if (nvec <= XFS_TRANS_LOGVEC_COUNT) { 909 xfs_trans_unreserve_and_mod_sb(tp); !! 775 log_vector = log_vector_fast; >> 776 } else { >> 777 log_vector = (xfs_log_iovec_t *)kmem_alloc(nvec * >> 778 sizeof(xfs_log_iovec_t), >> 779 KM_SLEEP); >> 780 } 910 781 911 /* 782 /* 912 * It is indeed possible for the trans !! 783 * Fill in the log_vector and pin the logged items, and 913 * the dqinfo portion to be. All that !! 784 * then write the transaction to the log. 914 * (non-persistent) quota reservations << 915 */ 785 */ 916 xfs_trans_unreserve_and_mod_dquots(tp) !! 786 xfs_trans_fill_vecs(tp, log_vector); 917 if (tp->t_ticket) { << 918 if (regrant && !xlog_is_shutdo << 919 xfs_log_ticket_regrant << 920 else << 921 xfs_log_ticket_ungrant << 922 tp->t_ticket = NULL; << 923 } << 924 xfs_trans_free_items(tp, !!error); << 925 xfs_trans_free(tp); << 926 787 927 XFS_STATS_INC(mp, xs_trans_empty); !! 788 /* 928 return error; !! 789 * Ignore errors here. xfs_log_done would do the right thing. 929 } !! 790 * We need to put the ticket, etc. away. >> 791 */ >> 792 error = xfs_log_write(mp, log_vector, nvec, tp->t_ticket, >> 793 &(tp->t_lsn)); 930 794 931 int !! 795 #if defined(XLOG_NOLOG) || defined(DEBUG) 932 xfs_trans_commit( !! 796 if (xlog_debug) { 933 struct xfs_trans *tp) !! 797 commit_lsn = xfs_log_done(mp, tp->t_ticket, 934 { !! 798 &commit_iclog, log_flags); 935 return __xfs_trans_commit(tp, false); !! 799 } else { 936 } !! 800 commit_lsn = 0; >> 801 tp->t_lsn = trans_lsn++; >> 802 } >> 803 #else >> 804 /* >> 805 * This is the regular case. At this point (after the call finishes), >> 806 * the transaction is committed incore and could go out to disk at >> 807 * any time. However, all the items associated with the transaction >> 808 * are still locked and pinned in memory. >> 809 */ >> 810 commit_lsn = xfs_log_done(mp, tp->t_ticket, &commit_iclog, log_flags); >> 811 #endif 937 812 938 /* !! 813 tp->t_commit_lsn = commit_lsn; 939 * Unlock all of the transaction's items and f !! 814 if (nvec > XFS_TRANS_LOGVEC_COUNT) { 940 * transaction is dirty, we must shut down the !! 815 kmem_free(log_vector, nvec * sizeof(xfs_log_iovec_t)); 941 * way to restore them to their previous state !! 816 } 942 * << 943 * If the transaction has made a log reservati << 944 * well. << 945 * << 946 * This is a high level function (equivalent t << 947 * be called after the transaction has effecti << 948 * being shut down. However, if the mount has << 949 * transaction is dirty we will shut the mount << 950 * guarantees that the log is shut down, too. << 951 * careful with shutdown state and dirty items << 952 * xfs_trans_commit(). << 953 */ << 954 void << 955 xfs_trans_cancel( << 956 struct xfs_trans *tp) << 957 { << 958 struct xfs_mount *mp = tp->t_mo << 959 struct xlog *log = mp->m_l << 960 bool dirty = (tp->t << 961 817 962 trace_xfs_trans_cancel(tp, _RET_IP_); !! 818 if (commit_lsn_p) >> 819 *commit_lsn_p = commit_lsn; 963 820 964 /* 821 /* 965 * It's never valid to cancel a transa !! 822 * If we got a log write error. Unpin the logitems that we 966 * because the transaction is effectiv !! 823 * had pinned, clean up, free trans structure, and return error. 967 * loudly before freeing the in-memory << 968 * filesystem. << 969 */ 824 */ 970 if (!list_empty(&tp->t_dfops)) { !! 825 if (error || commit_lsn == -1) { 971 ASSERT(tp->t_flags & XFS_TRANS !! 826 PFLAGS_RESTORE(&tp->t_pflags); 972 dirty = true; !! 827 xfs_trans_uncommit(tp, flags|XFS_TRANS_ABORT); 973 xfs_defer_cancel(tp); !! 828 return XFS_ERROR(EIO); 974 } 829 } 975 830 976 /* 831 /* 977 * See if the caller is relying on us !! 832 * Once the transaction has committed, unused 978 * only want an error report if there !! 833 * reservations need to be released and changes to 979 * progress, so we only need to check !! 834 * the superblock need to be reflected in the in-core 980 * here. !! 835 * version. Do that now. 981 */ 836 */ 982 if (dirty && !xfs_is_shutdown(mp)) { << 983 XFS_ERROR_REPORT("xfs_trans_ca << 984 xfs_force_shutdown(mp, SHUTDOW << 985 } << 986 #ifdef DEBUG << 987 /* Log items need to be consistent unt << 988 if (!dirty && !xlog_is_shutdown(log)) << 989 struct xfs_log_item *lip; << 990 << 991 list_for_each_entry(lip, &tp-> << 992 ASSERT(!xlog_item_is_i << 993 } << 994 #endif << 995 xfs_trans_unreserve_and_mod_sb(tp); 837 xfs_trans_unreserve_and_mod_sb(tp); 996 xfs_trans_unreserve_and_mod_dquots(tp) << 997 838 998 if (tp->t_ticket) { !! 839 sync = tp->t_flags & XFS_TRANS_SYNC; 999 xfs_log_ticket_ungrant(log, tp !! 840 1000 tp->t_ticket = NULL; !! 841 /* >> 842 * Tell the LM to call the transaction completion routine >> 843 * when the log write with LSN commit_lsn completes (e.g. >> 844 * when the transaction commit really hits the on-disk log). >> 845 * After this call we cannot reference tp, because the call >> 846 * can happen at any time and the call will free the transaction >> 847 * structure pointed to by tp. The only case where we call >> 848 * the completion routine (xfs_trans_committed) directly is >> 849 * if the log is turned off on a debug kernel or we're >> 850 * running in simulation mode (the log is explicitly turned >> 851 * off). >> 852 */ >> 853 tp->t_logcb.cb_func = (void(*)(void*, int))xfs_trans_committed; >> 854 tp->t_logcb.cb_arg = tp; >> 855 >> 856 /* We need to pass the iclog buffer which was used for the >> 857 * transaction commit record into this function, and attach >> 858 * the callback to it. The callback must be attached before >> 859 * the items are unlocked to avoid racing with other threads >> 860 * waiting for an item to unlock. >> 861 */ >> 862 error = xfs_log_notify(mp, commit_iclog, &(tp->t_logcb)); >> 863 >> 864 /* mark this thread as no longer being in a transaction */ >> 865 PFLAGS_RESTORE(&tp->t_pflags); >> 866 >> 867 /* >> 868 * Once all the items of the transaction have been copied >> 869 * to the in core log and the callback is attached, the >> 870 * items can be unlocked. >> 871 * >> 872 * This will free descriptors pointing to items which were >> 873 * not logged since there is nothing more to do with them. >> 874 * For items which were logged, we will keep pointers to them >> 875 * so they can be unpinned after the transaction commits to disk. >> 876 * This will also stamp each modified meta-data item with >> 877 * the commit lsn of this transaction for dependency tracking >> 878 * purposes. >> 879 */ >> 880 xfs_trans_unlock_items(tp, commit_lsn); >> 881 >> 882 /* >> 883 * Now that the xfs_trans_committed callback has been attached, >> 884 * and the items are released we can finally allow the iclog to >> 885 * go to disk. >> 886 */ >> 887 error = xfs_log_release_iclog(mp, commit_iclog); >> 888 >> 889 /* >> 890 * If the transaction needs to be synchronous, then force the >> 891 * log out now and wait for it. >> 892 */ >> 893 if (sync) { >> 894 if (!error) >> 895 error = xfs_log_force(mp, commit_lsn, >> 896 XFS_LOG_FORCE | XFS_LOG_SYNC); >> 897 XFS_STATS_INC(xs_trans_sync); >> 898 } else { >> 899 XFS_STATS_INC(xs_trans_async); 1001 } 900 } 1002 901 1003 xfs_trans_free_items(tp, dirty); !! 902 return (error); 1004 xfs_trans_free(tp); << 1005 } 903 } 1006 904 >> 905 1007 /* 906 /* 1008 * Roll from one trans in the sequence of PER !! 907 * Total up the number of log iovecs needed to commit this 1009 * the next: permanent transactions are only !! 908 * transaction. The transaction itself needs one for the 1010 * committed with xfs_trans_commit(), but we !! 909 * transaction header. Ask each dirty item in turn how many 1011 * as possible to let chunks of it go to the !! 910 * it needs to get the total. 1012 * chunk we've been working on and get a new << 1013 */ 911 */ 1014 int !! 912 STATIC uint 1015 xfs_trans_roll( !! 913 xfs_trans_count_vecs( 1016 struct xfs_trans **tpp) !! 914 xfs_trans_t *tp) 1017 { 915 { 1018 struct xfs_trans *trans = *tpp !! 916 int nvecs; 1019 struct xfs_trans_res tres; !! 917 xfs_log_item_desc_t *lidp; 1020 int error; << 1021 << 1022 trace_xfs_trans_roll(trans, _RET_IP_) << 1023 918 1024 /* !! 919 nvecs = 1; 1025 * Copy the critical parameters from !! 920 lidp = xfs_trans_first_item(tp); >> 921 ASSERT(lidp != NULL); >> 922 >> 923 /* In the non-debug case we need to start bailing out if we >> 924 * didn't find a log_item here, return zero and let trans_commit >> 925 * deal with it. 1026 */ 926 */ 1027 tres.tr_logres = trans->t_log_res; !! 927 if (lidp == NULL) 1028 tres.tr_logcount = trans->t_log_count !! 928 return 0; 1029 << 1030 *tpp = xfs_trans_dup(trans); << 1031 929 1032 /* !! 930 while (lidp != NULL) { 1033 * Commit the current transaction. !! 931 /* 1034 * If this commit failed, then it'd j !! 932 * Skip items which aren't dirty in this transaction. 1035 * are not marked ihold. That also me !! 933 */ 1036 * is in progress. The caller takes t !! 934 if (!(lidp->lid_flags & XFS_LID_DIRTY)) { 1037 * the duplicate transaction that get !! 935 lidp = xfs_trans_next_item(tp, lidp); 1038 */ !! 936 continue; 1039 error = __xfs_trans_commit(trans, tru !! 937 } 1040 if (error) !! 938 lidp->lid_size = IOP_SIZE(lidp->lid_item); 1041 return error; !! 939 nvecs += lidp->lid_size; >> 940 lidp = xfs_trans_next_item(tp, lidp); >> 941 } 1042 942 1043 /* !! 943 return nvecs; 1044 * Reserve space in the log for the n << 1045 * This also pushes items in the "AIL << 1046 * out to disk if they are taking up << 1047 * that we want to use. This require << 1048 * across this call, or that anything << 1049 * the prior and the next transaction << 1050 */ << 1051 tres.tr_logflags = XFS_TRANS_PERM_LOG << 1052 return xfs_trans_reserve(*tpp, &tres, << 1053 } 944 } 1054 945 1055 /* 946 /* 1056 * Allocate an transaction, lock and join the !! 947 * Called from the trans_commit code when we notice that 1057 * !! 948 * the filesystem is in the middle of a forced shutdown. 1058 * The caller must ensure that the on-disk dq << 1059 * already been allocated and initialized. T << 1060 * releasing ILOCK_EXCL if a new transaction << 1061 */ 949 */ 1062 int !! 950 STATIC void 1063 xfs_trans_alloc_inode( !! 951 xfs_trans_uncommit( 1064 struct xfs_inode *ip, !! 952 xfs_trans_t *tp, 1065 struct xfs_trans_res *resv, !! 953 uint flags) 1066 unsigned int dblocks, << 1067 unsigned int rblocks, << 1068 bool force, << 1069 struct xfs_trans **tpp) << 1070 { 954 { 1071 struct xfs_trans *tp; !! 955 xfs_log_item_desc_t *lidp; 1072 struct xfs_mount *mp = ip->i_m << 1073 bool retried = fal << 1074 int error; << 1075 956 1076 retry: !! 957 for (lidp = xfs_trans_first_item(tp); 1077 error = xfs_trans_alloc(mp, resv, dbl !! 958 lidp != NULL; 1078 xfs_extlen_to_rtxlen( !! 959 lidp = xfs_trans_next_item(tp, lidp)) { 1079 force ? XFS_TRANS_RES !! 960 /* 1080 if (error) !! 961 * Unpin all but those that aren't dirty. 1081 return error; !! 962 */ 1082 !! 963 if (lidp->lid_flags & XFS_LID_DIRTY) 1083 xfs_ilock(ip, XFS_ILOCK_EXCL); !! 964 IOP_UNPIN_REMOVE(lidp->lid_item, tp); 1084 xfs_trans_ijoin(tp, ip, 0); << 1085 << 1086 error = xfs_qm_dqattach_locked(ip, fa << 1087 if (error) { << 1088 /* Caller should have allocat << 1089 ASSERT(error != -ENOENT); << 1090 goto out_cancel; << 1091 } << 1092 << 1093 error = xfs_trans_reserve_quota_nblks << 1094 if ((error == -EDQUOT || error == -EN << 1095 xfs_trans_cancel(tp); << 1096 xfs_iunlock(ip, XFS_ILOCK_EXC << 1097 xfs_blockgc_free_quota(ip, 0) << 1098 retried = true; << 1099 goto retry; << 1100 } 965 } 1101 if (error) << 1102 goto out_cancel; << 1103 966 1104 *tpp = tp; !! 967 xfs_trans_unreserve_and_mod_sb(tp); 1105 return 0; !! 968 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(tp->t_mountp, tp); 1106 969 1107 out_cancel: !! 970 xfs_trans_free_items(tp, flags); 1108 xfs_trans_cancel(tp); !! 971 xfs_trans_free_busy(tp); 1109 xfs_iunlock(ip, XFS_ILOCK_EXCL); !! 972 xfs_trans_free(tp); 1110 return error; << 1111 } 973 } 1112 974 1113 /* 975 /* 1114 * Try to reserve more blocks for a transacti !! 976 * Fill in the vector with pointers to data to be logged >> 977 * by this transaction. The transaction header takes >> 978 * the first vector, and then each dirty item takes the >> 979 * number of vectors it indicated it needed in xfs_trans_count_vecs(). 1115 * 980 * 1116 * This is for callers that need to attach re !! 981 * As each item fills in the entries it needs, also pin the item 1117 * those resources to determine the space res !! 982 * so that it cannot be flushed out until the log write completes. 1118 * modify the attached resources. In other w << 1119 * fail due to ENOSPC, so the caller must be << 1120 * without shutting down the fs. << 1121 */ 983 */ 1122 int !! 984 STATIC void 1123 xfs_trans_reserve_more( !! 985 xfs_trans_fill_vecs( 1124 struct xfs_trans *tp, !! 986 xfs_trans_t *tp, 1125 unsigned int blocks, !! 987 xfs_log_iovec_t *log_vector) 1126 unsigned int rtextents) << 1127 { 988 { 1128 struct xfs_trans_res resv = { }; !! 989 xfs_log_item_desc_t *lidp; >> 990 xfs_log_iovec_t *vecp; >> 991 uint nitems; >> 992 >> 993 /* >> 994 * Skip over the entry for the transaction header, we'll >> 995 * fill that in at the end. >> 996 */ >> 997 vecp = log_vector + 1; /* pointer arithmetic */ >> 998 >> 999 nitems = 0; >> 1000 lidp = xfs_trans_first_item(tp); >> 1001 ASSERT(lidp != NULL); >> 1002 while (lidp != NULL) { >> 1003 /* >> 1004 * Skip items which aren't dirty in this transaction. >> 1005 */ >> 1006 if (!(lidp->lid_flags & XFS_LID_DIRTY)) { >> 1007 lidp = xfs_trans_next_item(tp, lidp); >> 1008 continue; >> 1009 } >> 1010 /* >> 1011 * The item may be marked dirty but not log anything. >> 1012 * This can be used to get called when a transaction >> 1013 * is committed. >> 1014 */ >> 1015 if (lidp->lid_size) { >> 1016 nitems++; >> 1017 } >> 1018 IOP_FORMAT(lidp->lid_item, vecp); >> 1019 vecp += lidp->lid_size; /* pointer arithmetic */ >> 1020 IOP_PIN(lidp->lid_item); >> 1021 lidp = xfs_trans_next_item(tp, lidp); >> 1022 } 1129 1023 1130 return xfs_trans_reserve(tp, &resv, b !! 1024 /* >> 1025 * Now that we've counted the number of items in this >> 1026 * transaction, fill in the transaction header. >> 1027 */ >> 1028 tp->t_header.th_magic = XFS_TRANS_HEADER_MAGIC; >> 1029 tp->t_header.th_type = tp->t_type; >> 1030 tp->t_header.th_num_items = nitems; >> 1031 log_vector->i_addr = (xfs_caddr_t)&tp->t_header; >> 1032 log_vector->i_len = sizeof(xfs_trans_header_t); 1131 } 1033 } 1132 1034 >> 1035 1133 /* 1036 /* 1134 * Try to reserve more blocks and file quota !! 1037 * Unlock all of the transaction's items and free the transaction. 1135 * conditions of usage as xfs_trans_reserve_m !! 1038 * The transaction must not have modified any of its items, because >> 1039 * there is no way to restore them to their previous state. >> 1040 * >> 1041 * If the transaction has made a log reservation, make sure to release >> 1042 * it as well. 1136 */ 1043 */ 1137 int !! 1044 void 1138 xfs_trans_reserve_more_inode( !! 1045 xfs_trans_cancel( 1139 struct xfs_trans *tp, !! 1046 xfs_trans_t *tp, 1140 struct xfs_inode *ip, !! 1047 int flags) 1141 unsigned int dblocks, << 1142 unsigned int rblocks, << 1143 bool force_quota) << 1144 { 1048 { 1145 struct xfs_trans_res resv = { }; !! 1049 int log_flags; 1146 struct xfs_mount *mp = ip->i_m !! 1050 #ifdef DEBUG 1147 unsigned int rtx = xfs_ext !! 1051 xfs_log_item_chunk_t *licp; 1148 int error; !! 1052 xfs_log_item_desc_t *lidp; 1149 !! 1053 xfs_log_item_t *lip; 1150 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL !! 1054 int i; 1151 !! 1055 #endif 1152 error = xfs_trans_reserve(tp, &resv, << 1153 if (error) << 1154 return error; << 1155 1056 1156 if (!XFS_IS_QUOTA_ON(mp) || xfs_is_qu !! 1057 /* 1157 return 0; !! 1058 * See if the caller is being too lazy to figure out if >> 1059 * the transaction really needs an abort. >> 1060 */ >> 1061 if ((flags & XFS_TRANS_ABORT) && !(tp->t_flags & XFS_TRANS_DIRTY)) >> 1062 flags &= ~XFS_TRANS_ABORT; >> 1063 /* >> 1064 * See if the caller is relying on us to shut down the >> 1065 * filesystem. This happens in paths where we detect >> 1066 * corruption and decide to give up. >> 1067 */ >> 1068 if ((tp->t_flags & XFS_TRANS_DIRTY) && >> 1069 !XFS_FORCED_SHUTDOWN(tp->t_mountp)) >> 1070 xfs_force_shutdown(tp->t_mountp, XFS_CORRUPT_INCORE); >> 1071 #ifdef DEBUG >> 1072 if (!(flags & XFS_TRANS_ABORT)) { >> 1073 licp = &(tp->t_items); >> 1074 while (licp != NULL) { >> 1075 lidp = licp->lic_descs; >> 1076 for (i = 0; i < licp->lic_unused; i++, lidp++) { >> 1077 if (XFS_LIC_ISFREE(licp, i)) { >> 1078 continue; >> 1079 } >> 1080 >> 1081 lip = lidp->lid_item; >> 1082 if (!XFS_FORCED_SHUTDOWN(tp->t_mountp)) >> 1083 ASSERT(!(lip->li_type == XFS_LI_EFD)); >> 1084 } >> 1085 licp = licp->lic_next; >> 1086 } >> 1087 } >> 1088 #endif >> 1089 xfs_trans_unreserve_and_mod_sb(tp); >> 1090 XFS_TRANS_UNRESERVE_AND_MOD_DQUOTS(tp->t_mountp, tp); 1158 1091 1159 if (tp->t_flags & XFS_TRANS_RESERVE) !! 1092 if (tp->t_ticket) { 1160 force_quota = true; !! 1093 if (flags & XFS_TRANS_RELEASE_LOG_RES) { >> 1094 ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); >> 1095 log_flags = XFS_LOG_REL_PERM_RESERV; >> 1096 } else { >> 1097 log_flags = 0; >> 1098 } >> 1099 xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, log_flags); >> 1100 } 1161 1101 1162 error = xfs_trans_reserve_quota_nblks !! 1102 /* mark this thread as no longer being in a transaction */ 1163 force_quota); !! 1103 PFLAGS_RESTORE(&tp->t_pflags); 1164 if (!error) << 1165 return 0; << 1166 1104 1167 /* Quota failed, give back the new re !! 1105 xfs_trans_free_items(tp, flags); 1168 xfs_add_fdblocks(mp, dblocks); !! 1106 xfs_trans_free_busy(tp); 1169 tp->t_blk_res -= dblocks; !! 1107 xfs_trans_free(tp); 1170 xfs_add_frextents(mp, rtx); << 1171 tp->t_rtx_res -= rtx; << 1172 return error; << 1173 } 1108 } 1174 1109 >> 1110 1175 /* 1111 /* 1176 * Allocate an transaction in preparation for !! 1112 * Free the transaction structure. If there is more clean up 1177 * against the given dquots. Callers are not !! 1113 * to do when the structure is freed, add it here. 1178 */ 1114 */ 1179 int !! 1115 STATIC void 1180 xfs_trans_alloc_icreate( !! 1116 xfs_trans_free( 1181 struct xfs_mount *mp, !! 1117 xfs_trans_t *tp) 1182 struct xfs_trans_res *resv, << 1183 struct xfs_dquot *udqp, << 1184 struct xfs_dquot *gdqp, << 1185 struct xfs_dquot *pdqp, << 1186 unsigned int dblocks, << 1187 struct xfs_trans **tpp) << 1188 { 1118 { 1189 struct xfs_trans *tp; !! 1119 atomic_dec(&tp->t_mountp->m_active_trans); 1190 bool retried = fal !! 1120 XFS_TRANS_FREE_DQINFO(tp->t_mountp, tp); 1191 int error; !! 1121 kmem_zone_free(xfs_trans_zone, tp); 1192 << 1193 retry: << 1194 error = xfs_trans_alloc(mp, resv, dbl << 1195 if (error) << 1196 return error; << 1197 << 1198 error = xfs_trans_reserve_quota_icrea << 1199 if ((error == -EDQUOT || error == -EN << 1200 xfs_trans_cancel(tp); << 1201 xfs_blockgc_free_dquots(mp, u << 1202 retried = true; << 1203 goto retry; << 1204 } << 1205 if (error) { << 1206 xfs_trans_cancel(tp); << 1207 return error; << 1208 } << 1209 << 1210 *tpp = tp; << 1211 return 0; << 1212 } 1122 } 1213 1123 >> 1124 1214 /* 1125 /* 1215 * Allocate an transaction, lock and join the !! 1126 * THIS SHOULD BE REWRITTEN TO USE xfs_trans_next_item(). 1216 * in preparation for inode attribute changes !! 1127 * 1217 * changes. !! 1128 * This is typically called by the LM when a transaction has been fully 1218 * !! 1129 * committed to disk. It needs to unpin the items which have 1219 * The caller must ensure that the on-disk dq !! 1130 * been logged by the transaction and update their positions 1220 * already been allocated and initialized. T !! 1131 * in the AIL if necessary. 1221 * transaction is committed or cancelled. !! 1132 * This also gets called when the transactions didn't get written out >> 1133 * because of an I/O error. Abortflag & XFS_LI_ABORTED is set then. >> 1134 * >> 1135 * Call xfs_trans_chunk_committed() to process the items in >> 1136 * each chunk. 1222 */ 1137 */ 1223 int !! 1138 STATIC void 1224 xfs_trans_alloc_ichange( !! 1139 xfs_trans_committed( 1225 struct xfs_inode *ip, !! 1140 xfs_trans_t *tp, 1226 struct xfs_dquot *new_udqp, !! 1141 int abortflag) 1227 struct xfs_dquot *new_gdqp, << 1228 struct xfs_dquot *new_pdqp, << 1229 bool force, << 1230 struct xfs_trans **tpp) << 1231 { 1142 { 1232 struct xfs_trans *tp; !! 1143 xfs_log_item_chunk_t *licp; 1233 struct xfs_mount *mp = ip->i_m !! 1144 xfs_log_item_chunk_t *next_licp; 1234 struct xfs_dquot *udqp; !! 1145 xfs_log_busy_chunk_t *lbcp; 1235 struct xfs_dquot *gdqp; !! 1146 xfs_log_busy_slot_t *lbsp; 1236 struct xfs_dquot *pdqp; !! 1147 int i; 1237 bool retried = fal << 1238 int error; << 1239 1148 1240 retry: !! 1149 /* 1241 error = xfs_trans_alloc(mp, &M_RES(mp !! 1150 * Call the transaction's completion callback if there 1242 if (error) !! 1151 * is one. 1243 return error; !! 1152 */ 1244 !! 1153 if (tp->t_callback != NULL) { 1245 xfs_ilock(ip, XFS_ILOCK_EXCL); !! 1154 tp->t_callback(tp, tp->t_callarg); 1246 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXC << 1247 << 1248 error = xfs_qm_dqattach_locked(ip, fa << 1249 if (error) { << 1250 /* Caller should have allocat << 1251 ASSERT(error != -ENOENT); << 1252 goto out_cancel; << 1253 } 1155 } 1254 1156 1255 /* 1157 /* 1256 * For each quota type, skip quota re !! 1158 * Special case the chunk embedded in the transaction. 1257 * now match the ones that came from !! 1159 */ 1258 * pass one in. The inode's dquots c !! 1160 licp = &(tp->t_items); 1259 * perform a blockgc scan, so we must !! 1161 if (!(XFS_LIC_ARE_ALL_FREE(licp))) { 1260 */ !! 1162 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag); 1261 udqp = (new_udqp != ip->i_udquot) ? n !! 1163 } 1262 gdqp = (new_gdqp != ip->i_gdquot) ? n << 1263 pdqp = (new_pdqp != ip->i_pdquot) ? n << 1264 if (udqp || gdqp || pdqp) { << 1265 unsigned int qflags = XFS_ << 1266 1164 1267 if (force) !! 1165 /* 1268 qflags |= XFS_QMOPT_F !! 1166 * Process the items in each chunk in turn. >> 1167 */ >> 1168 licp = licp->lic_next; >> 1169 while (licp != NULL) { >> 1170 ASSERT(!XFS_LIC_ARE_ALL_FREE(licp)); >> 1171 xfs_trans_chunk_committed(licp, tp->t_lsn, abortflag); >> 1172 next_licp = licp->lic_next; >> 1173 kmem_free(licp, sizeof(xfs_log_item_chunk_t)); >> 1174 licp = next_licp; >> 1175 } 1269 1176 1270 /* !! 1177 /* 1271 * Reserve enough quota to ha !! 1178 * Clear all the per-AG busy list items listed in this transaction 1272 * for a delayed allocation. !! 1179 */ 1273 * delalloc reservation betwe !! 1180 lbcp = &tp->t_busy; 1274 * though that part is only s !! 1181 while (lbcp != NULL) { 1275 */ !! 1182 for (i = 0, lbsp = lbcp->lbc_busy; i < lbcp->lbc_unused; i++, lbsp++) { 1276 error = xfs_trans_reserve_quo !! 1183 if (!XFS_LBC_ISFREE(lbcp, i)) { 1277 pdqp, ip->i_n !! 1184 xfs_alloc_clear_busy(tp, lbsp->lbc_ag, 1278 1, qflags); !! 1185 lbsp->lbc_idx); 1279 if ((error == -EDQUOT || erro !! 1186 } 1280 xfs_trans_cancel(tp); << 1281 xfs_blockgc_free_dquo << 1282 retried = true; << 1283 goto retry; << 1284 } 1187 } 1285 if (error) !! 1188 lbcp = lbcp->lbc_next; 1286 goto out_cancel; << 1287 } 1189 } >> 1190 xfs_trans_free_busy(tp); 1288 1191 1289 *tpp = tp; !! 1192 /* 1290 return 0; !! 1193 * That's it for the transaction structure. Free it. 1291 !! 1194 */ 1292 out_cancel: !! 1195 xfs_trans_free(tp); 1293 xfs_trans_cancel(tp); << 1294 return error; << 1295 } 1196 } 1296 1197 1297 /* 1198 /* 1298 * Allocate an transaction, lock and join the !! 1199 * This is called to perform the commit processing for each 1299 * and reserve quota for a directory update. !! 1200 * item described by the given chunk. 1300 * @dblocks will be set to zero for a reserva !! 1201 * 1301 * @nospace_error will be set to a negative e !! 1202 * The commit processing consists of unlocking items which were 1302 * constraint we hit. !! 1203 * held locked with the SYNC_UNLOCK attribute, calling the committed 1303 * !! 1204 * routine of each logged item, updating the item's position in the AIL 1304 * The caller must ensure that the on-disk dq !! 1205 * if necessary, and unpinning each item. If the committed routine 1305 * already been allocated and initialized. T !! 1206 * returns -1, then do nothing further with the item because it 1306 * transaction is committed or cancelled. !! 1207 * may have been freed. 1307 * 1208 * 1308 * Caller is responsible for unlocking the in !! 1209 * Since items are unlocked when they are copied to the incore >> 1210 * log, it is possible for two transactions to be completing >> 1211 * and manipulating the same item simultaneously. The AIL lock >> 1212 * will protect the lsn field of each item. The value of this >> 1213 * field can never go backwards. >> 1214 * >> 1215 * We unpin the items after repositioning them in the AIL, because >> 1216 * otherwise they could be immediately flushed and we'd have to race >> 1217 * with the flusher trying to pull the item from the AIL as we add it. 1309 */ 1218 */ 1310 int !! 1219 STATIC void 1311 xfs_trans_alloc_dir( !! 1220 xfs_trans_chunk_committed( 1312 struct xfs_inode *dp, !! 1221 xfs_log_item_chunk_t *licp, 1313 struct xfs_trans_res *resv, !! 1222 xfs_lsn_t lsn, 1314 struct xfs_inode *ip, !! 1223 int aborted) 1315 unsigned int *dblocks, !! 1224 { 1316 struct xfs_trans **tpp, !! 1225 xfs_log_item_desc_t *lidp; 1317 int *nospace_erro !! 1226 xfs_log_item_t *lip; 1318 { !! 1227 xfs_lsn_t item_lsn; 1319 struct xfs_trans *tp; !! 1228 struct xfs_mount *mp; 1320 struct xfs_mount *mp = ip->i_m !! 1229 int i; 1321 unsigned int resblks; !! 1230 SPLDECL(s); 1322 bool retried = fal << 1323 int error; << 1324 1231 1325 retry: !! 1232 lidp = licp->lic_descs; 1326 *nospace_error = 0; !! 1233 for (i = 0; i < licp->lic_unused; i++, lidp++) { 1327 resblks = *dblocks; !! 1234 if (XFS_LIC_ISFREE(licp, i)) { 1328 error = xfs_trans_alloc(mp, resv, res !! 1235 continue; 1329 if (error == -ENOSPC) { !! 1236 } 1330 *nospace_error = error; !! 1237 1331 resblks = 0; !! 1238 lip = lidp->lid_item; 1332 error = xfs_trans_alloc(mp, r !! 1239 if (aborted) 1333 } !! 1240 lip->li_flags |= XFS_LI_ABORTED; 1334 if (error) !! 1241 1335 return error; !! 1242 if (lidp->lid_flags & XFS_LID_SYNC_UNLOCK) { 1336 !! 1243 IOP_UNLOCK(lip); 1337 xfs_lock_two_inodes(dp, XFS_ILOCK_EXC !! 1244 } 1338 !! 1245 1339 xfs_trans_ijoin(tp, dp, 0); !! 1246 /* 1340 xfs_trans_ijoin(tp, ip, 0); !! 1247 * Send in the ABORTED flag to the COMMITTED routine 1341 !! 1248 * so that it knows whether the transaction was aborted 1342 error = xfs_qm_dqattach_locked(dp, fa !! 1249 * or not. 1343 if (error) { !! 1250 */ 1344 /* Caller should have allocat !! 1251 item_lsn = IOP_COMMITTED(lip, lsn); 1345 ASSERT(error != -ENOENT); !! 1252 1346 goto out_cancel; !! 1253 /* 1347 } !! 1254 * If the committed routine returns -1, make 1348 !! 1255 * no more references to the item. 1349 error = xfs_qm_dqattach_locked(ip, fa !! 1256 */ 1350 if (error) { !! 1257 if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) { 1351 /* Caller should have allocat !! 1258 continue; 1352 ASSERT(error != -ENOENT); !! 1259 } 1353 goto out_cancel; !! 1260 1354 } !! 1261 /* 1355 !! 1262 * If the returned lsn is greater than what it 1356 if (resblks == 0) !! 1263 * contained before, update the location of the 1357 goto done; !! 1264 * item in the AIL. If it is not, then do nothing. 1358 !! 1265 * Items can never move backwards in the AIL. 1359 error = xfs_trans_reserve_quota_nblks !! 1266 * 1360 if (error == -EDQUOT || error == -ENO !! 1267 * While the new lsn should usually be greater, it 1361 if (!retried) { !! 1268 * is possible that a later transaction completing 1362 xfs_trans_cancel(tp); !! 1269 * simultaneously with an earlier one using the 1363 xfs_iunlock(dp, XFS_I !! 1270 * same item could complete first with a higher lsn. 1364 if (dp != ip) !! 1271 * This would cause the earlier transaction to fail 1365 xfs_iunlock(i !! 1272 * the test below. 1366 xfs_blockgc_free_quot !! 1273 */ 1367 retried = true; !! 1274 mp = lip->li_mountp; 1368 goto retry; !! 1275 AIL_LOCK(mp,s); 1369 } !! 1276 if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) { 1370 !! 1277 /* 1371 *nospace_error = error; !! 1278 * This will set the item's lsn to item_lsn 1372 resblks = 0; !! 1279 * and update the position of the item in 1373 error = 0; !! 1280 * the AIL. 1374 } !! 1281 * 1375 if (error) !! 1282 * xfs_trans_update_ail() drops the AIL lock. 1376 goto out_cancel; !! 1283 */ 1377 !! 1284 xfs_trans_update_ail(mp, lip, item_lsn, s); 1378 done: !! 1285 } else { 1379 *tpp = tp; !! 1286 AIL_UNLOCK(mp, s); 1380 *dblocks = resblks; !! 1287 } 1381 return 0; << 1382 1288 1383 out_cancel: !! 1289 /* 1384 xfs_trans_cancel(tp); !! 1290 * Now that we've repositioned the item in the AIL, 1385 return error; !! 1291 * unpin it so it can be flushed. Pass information >> 1292 * about buffer stale state down from the log item >> 1293 * flags, if anyone else stales the buffer we do not >> 1294 * want to pay any attention to it. >> 1295 */ >> 1296 IOP_UNPIN(lip, lidp->lid_flags & XFS_LID_BUF_STALE); >> 1297 } 1386 } 1298 } 1387 1299
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.