Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 2 | * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc. |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 3 | * Copyright (C) 2010 Red Hat, Inc. |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * published by the Free Software Foundation. |
| 9 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 10 | * This program is distributed in the hope that it would be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 20 | #include "xfs_fs.h" |
Dave Chinner | 70a9883 | 2013-10-23 10:36:05 +1100 | [diff] [blame] | 21 | #include "xfs_shared.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 22 | #include "xfs_format.h" |
| 23 | #include "xfs_log_format.h" |
| 24 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "xfs_mount.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 26 | #include "xfs_inode.h" |
Dave Chinner | efc27b5 | 2012-04-29 10:39:43 +0000 | [diff] [blame] | 27 | #include "xfs_extent_busy.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "xfs_quota.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 29 | #include "xfs_trans.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 30 | #include "xfs_trans_priv.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 31 | #include "xfs_log.h" |
Dave Chinner | ed3b4d6 | 2010-05-21 12:07:08 +1000 | [diff] [blame] | 32 | #include "xfs_trace.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 33 | #include "xfs_error.h" |
Brian Foster | f8f2835 | 2018-05-07 17:38:47 -0700 | [diff] [blame] | 34 | #include "xfs_defer.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Nathan Scott | 8f79405 | 2006-03-14 13:32:41 +1100 | [diff] [blame] | 36 | kmem_zone_t *xfs_trans_zone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Darrick J. Wong | b872af2 | 2018-01-08 10:51:26 -0800 | [diff] [blame] | 38 | #if defined(CONFIG_TRACEPOINTS) |
| 39 | static void |
| 40 | xfs_trans_trace_reservations( |
| 41 | struct xfs_mount *mp) |
| 42 | { |
| 43 | struct xfs_trans_res resv; |
| 44 | struct xfs_trans_res *res; |
| 45 | struct xfs_trans_res *end_res; |
| 46 | int i; |
| 47 | |
| 48 | res = (struct xfs_trans_res *)M_RES(mp); |
| 49 | end_res = (struct xfs_trans_res *)(M_RES(mp) + 1); |
| 50 | for (i = 0; res < end_res; i++, res++) |
| 51 | trace_xfs_trans_resv_calc(mp, i, res); |
| 52 | xfs_log_get_max_trans_res(mp, &resv); |
| 53 | trace_xfs_trans_resv_calc(mp, -1, &resv); |
| 54 | } |
| 55 | #else |
| 56 | # define xfs_trans_trace_reservations(mp) |
| 57 | #endif |
| 58 | |
Jeff Liu | 4f3b578 | 2013-01-28 21:25:35 +0800 | [diff] [blame] | 59 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | * Initialize the precomputed transaction reservation values |
| 61 | * in the mount structure. |
| 62 | */ |
| 63 | void |
| 64 | xfs_trans_init( |
Christoph Hellwig | 025101d | 2010-05-04 13:53:48 +0000 | [diff] [blame] | 65 | struct xfs_mount *mp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 67 | xfs_trans_resv_calc(mp, M_RES(mp)); |
Darrick J. Wong | b872af2 | 2018-01-08 10:51:26 -0800 | [diff] [blame] | 68 | xfs_trans_trace_reservations(mp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* |
Dave Chinner | b1c1b5b | 2010-03-23 10:11:05 +1100 | [diff] [blame] | 72 | * Free the transaction structure. If there is more clean up |
| 73 | * to do when the structure is freed, add it here. |
| 74 | */ |
| 75 | STATIC void |
| 76 | xfs_trans_free( |
Dave Chinner | ed3b4d6 | 2010-05-21 12:07:08 +1000 | [diff] [blame] | 77 | struct xfs_trans *tp) |
Dave Chinner | b1c1b5b | 2010-03-23 10:11:05 +1100 | [diff] [blame] | 78 | { |
Dave Chinner | 4ecbfe6 | 2012-04-29 10:41:10 +0000 | [diff] [blame] | 79 | xfs_extent_busy_sort(&tp->t_busy); |
| 80 | xfs_extent_busy_clear(tp->t_mountp, &tp->t_busy, false); |
Dave Chinner | ed3b4d6 | 2010-05-21 12:07:08 +1000 | [diff] [blame] | 81 | |
Dave Chinner | ba18781 | 2018-05-09 07:47:57 -0700 | [diff] [blame] | 82 | trace_xfs_trans_free(tp, _RET_IP_); |
Dave Chinner | b1c1b5b | 2010-03-23 10:11:05 +1100 | [diff] [blame] | 83 | atomic_dec(&tp->t_mountp->m_active_trans); |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 84 | if (!(tp->t_flags & XFS_TRANS_NO_WRITECOUNT)) |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 85 | sb_end_intwrite(tp->t_mountp->m_super); |
Dave Chinner | b1c1b5b | 2010-03-23 10:11:05 +1100 | [diff] [blame] | 86 | xfs_trans_free_dqinfo(tp); |
| 87 | kmem_zone_free(xfs_trans_zone, tp); |
| 88 | } |
| 89 | |
| 90 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | * This is called to create a new transaction which will share the |
| 92 | * permanent log reservation of the given transaction. The remaining |
| 93 | * unused block and rt extent reservations are also inherited. This |
| 94 | * implies that the original transaction is no longer allowed to allocate |
| 95 | * blocks. Locks and log items, however, are no inherited. They must |
| 96 | * be added to the new transaction explicitly. |
| 97 | */ |
Brian Foster | f8f2835 | 2018-05-07 17:38:47 -0700 | [diff] [blame] | 98 | STATIC struct xfs_trans * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | xfs_trans_dup( |
Brian Foster | f8f2835 | 2018-05-07 17:38:47 -0700 | [diff] [blame] | 100 | struct xfs_trans *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
Brian Foster | f8f2835 | 2018-05-07 17:38:47 -0700 | [diff] [blame] | 102 | struct xfs_trans *ntp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Dave Chinner | ba18781 | 2018-05-09 07:47:57 -0700 | [diff] [blame] | 104 | trace_xfs_trans_dup(tp, _RET_IP_); |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | ntp = kmem_zone_zalloc(xfs_trans_zone, KM_SLEEP); |
| 107 | |
| 108 | /* |
| 109 | * Initialize the new transaction structure. |
| 110 | */ |
Dave Chinner | 2a3c0ac | 2013-08-12 20:49:28 +1000 | [diff] [blame] | 111 | ntp->t_magic = XFS_TRANS_HEADER_MAGIC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | ntp->t_mountp = tp->t_mountp; |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 113 | INIT_LIST_HEAD(&ntp->t_items); |
Dave Chinner | ed3b4d6 | 2010-05-21 12:07:08 +1000 | [diff] [blame] | 114 | INIT_LIST_HEAD(&ntp->t_busy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | ASSERT(tp->t_ticket != NULL); |
Nathan Scott | cfcbbbd | 2005-11-02 15:12:04 +1100 | [diff] [blame] | 118 | |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 119 | ntp->t_flags = XFS_TRANS_PERM_LOG_RES | |
| 120 | (tp->t_flags & XFS_TRANS_RESERVE) | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 121 | (tp->t_flags & XFS_TRANS_NO_WRITECOUNT); |
Jan Kara | d9457dc | 2012-06-12 16:20:39 +0200 | [diff] [blame] | 122 | /* We gave our writer reference to the new transaction */ |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 123 | tp->t_flags |= XFS_TRANS_NO_WRITECOUNT; |
Dave Chinner | cc09c0d | 2008-11-17 17:37:10 +1100 | [diff] [blame] | 124 | ntp->t_ticket = xfs_log_ticket_get(tp->t_ticket); |
Brian Foster | 3e78b9a | 2018-03-09 14:01:58 -0800 | [diff] [blame] | 125 | |
| 126 | ASSERT(tp->t_blk_res >= tp->t_blk_res_used); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | ntp->t_blk_res = tp->t_blk_res - tp->t_blk_res_used; |
| 128 | tp->t_blk_res = tp->t_blk_res_used; |
Brian Foster | 3e78b9a | 2018-03-09 14:01:58 -0800 | [diff] [blame] | 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | ntp->t_rtx_res = tp->t_rtx_res - tp->t_rtx_res_used; |
| 131 | tp->t_rtx_res = tp->t_rtx_res_used; |
Nathan Scott | 59c1b08 | 2006-06-09 14:59:13 +1000 | [diff] [blame] | 132 | ntp->t_pflags = tp->t_pflags; |
Brian Foster | f8f2835 | 2018-05-07 17:38:47 -0700 | [diff] [blame] | 133 | ntp->t_agfl_dfops = tp->t_agfl_dfops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 135 | xfs_trans_dup_dqinfo(tp, ntp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | atomic_inc(&tp->t_mountp->m_active_trans); |
| 138 | return ntp; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * This is called to reserve free disk blocks and log space for the |
| 143 | * given transaction. This must be done before allocating any resources |
| 144 | * within the transaction. |
| 145 | * |
| 146 | * This will return ENOSPC if there are not enough blocks available. |
| 147 | * It will sleep waiting for available log space. |
| 148 | * The only valid value for the flags parameter is XFS_RES_LOG_PERM, which |
| 149 | * is used by long running transactions. If any one of the reservations |
| 150 | * fails then they will all be backed out. |
| 151 | * |
| 152 | * This does not do quota reservations. That typically is done by the |
| 153 | * caller afterwards. |
| 154 | */ |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 155 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | xfs_trans_reserve( |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 157 | struct xfs_trans *tp, |
| 158 | struct xfs_trans_res *resp, |
| 159 | uint blocks, |
| 160 | uint rtextents) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
Nathan Scott | 59c1b08 | 2006-06-09 14:59:13 +1000 | [diff] [blame] | 162 | int error = 0; |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 163 | bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | /* Mark this thread as being in a transaction */ |
Michal Hocko | 9070733 | 2017-05-03 14:53:12 -0700 | [diff] [blame] | 166 | current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | /* |
| 169 | * Attempt to reserve the needed disk blocks by decrementing |
| 170 | * the number needed from the number available. This will |
| 171 | * fail if the count would go below zero. |
| 172 | */ |
| 173 | if (blocks > 0) { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 174 | error = xfs_mod_fdblocks(tp->t_mountp, -((int64_t)blocks), rsvd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | if (error != 0) { |
Michal Hocko | 9070733 | 2017-05-03 14:53:12 -0700 | [diff] [blame] | 176 | current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 177 | return -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | tp->t_blk_res += blocks; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Reserve the log space needed for this transaction. |
| 184 | */ |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 185 | if (resp->tr_logres > 0) { |
Christoph Hellwig | 9006fb9 | 2012-02-20 02:31:31 +0000 | [diff] [blame] | 186 | bool permanent = false; |
| 187 | |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 188 | ASSERT(tp->t_log_res == 0 || |
| 189 | tp->t_log_res == resp->tr_logres); |
| 190 | ASSERT(tp->t_log_count == 0 || |
| 191 | tp->t_log_count == resp->tr_logcount); |
Christoph Hellwig | 9006fb9 | 2012-02-20 02:31:31 +0000 | [diff] [blame] | 192 | |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 193 | if (resp->tr_logflags & XFS_TRANS_PERM_LOG_RES) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | tp->t_flags |= XFS_TRANS_PERM_LOG_RES; |
Christoph Hellwig | 9006fb9 | 2012-02-20 02:31:31 +0000 | [diff] [blame] | 195 | permanent = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } else { |
| 197 | ASSERT(tp->t_ticket == NULL); |
| 198 | ASSERT(!(tp->t_flags & XFS_TRANS_PERM_LOG_RES)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Christoph Hellwig | 9006fb9 | 2012-02-20 02:31:31 +0000 | [diff] [blame] | 201 | if (tp->t_ticket != NULL) { |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 202 | ASSERT(resp->tr_logflags & XFS_TRANS_PERM_LOG_RES); |
Christoph Hellwig | 9006fb9 | 2012-02-20 02:31:31 +0000 | [diff] [blame] | 203 | error = xfs_log_regrant(tp->t_mountp, tp->t_ticket); |
| 204 | } else { |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 205 | error = xfs_log_reserve(tp->t_mountp, |
| 206 | resp->tr_logres, |
| 207 | resp->tr_logcount, |
| 208 | &tp->t_ticket, XFS_TRANSACTION, |
Christoph Hellwig | 710b1e2 | 2016-04-06 09:20:36 +1000 | [diff] [blame] | 209 | permanent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
Christoph Hellwig | 9006fb9 | 2012-02-20 02:31:31 +0000 | [diff] [blame] | 211 | |
| 212 | if (error) |
| 213 | goto undo_blocks; |
| 214 | |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 215 | tp->t_log_res = resp->tr_logres; |
| 216 | tp->t_log_count = resp->tr_logcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* |
| 220 | * Attempt to reserve the needed realtime extents by decrementing |
| 221 | * the number needed from the number available. This will |
| 222 | * fail if the count would go below zero. |
| 223 | */ |
| 224 | if (rtextents > 0) { |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 225 | error = xfs_mod_frextents(tp->t_mountp, -((int64_t)rtextents)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | if (error) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 227 | error = -ENOSPC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | goto undo_log; |
| 229 | } |
| 230 | tp->t_rtx_res += rtextents; |
| 231 | } |
| 232 | |
| 233 | return 0; |
| 234 | |
| 235 | /* |
| 236 | * Error cases jump to one of these labels to undo any |
| 237 | * reservations which have already been performed. |
| 238 | */ |
| 239 | undo_log: |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 240 | if (resp->tr_logres > 0) { |
Christoph Hellwig | f78c390 | 2015-06-04 13:48:20 +1000 | [diff] [blame] | 241 | xfs_log_done(tp->t_mountp, tp->t_ticket, NULL, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | tp->t_ticket = NULL; |
| 243 | tp->t_log_res = 0; |
| 244 | tp->t_flags &= ~XFS_TRANS_PERM_LOG_RES; |
| 245 | } |
| 246 | |
| 247 | undo_blocks: |
| 248 | if (blocks > 0) { |
Eryu Guan | a27f6ef | 2016-09-14 07:39:07 +1000 | [diff] [blame] | 249 | xfs_mod_fdblocks(tp->t_mountp, (int64_t)blocks, rsvd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | tp->t_blk_res = 0; |
| 251 | } |
| 252 | |
Michal Hocko | 9070733 | 2017-05-03 14:53:12 -0700 | [diff] [blame] | 253 | current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
Nathan Scott | 59c1b08 | 2006-06-09 14:59:13 +1000 | [diff] [blame] | 255 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 258 | int |
| 259 | xfs_trans_alloc( |
| 260 | struct xfs_mount *mp, |
| 261 | struct xfs_trans_res *resp, |
| 262 | uint blocks, |
| 263 | uint rtextents, |
| 264 | uint flags, |
| 265 | struct xfs_trans **tpp) |
| 266 | { |
| 267 | struct xfs_trans *tp; |
| 268 | int error; |
| 269 | |
| 270 | if (!(flags & XFS_TRANS_NO_WRITECOUNT)) |
| 271 | sb_start_intwrite(mp->m_super); |
| 272 | |
| 273 | WARN_ON(mp->m_super->s_writers.frozen == SB_FREEZE_COMPLETE); |
| 274 | atomic_inc(&mp->m_active_trans); |
| 275 | |
| 276 | tp = kmem_zone_zalloc(xfs_trans_zone, |
| 277 | (flags & XFS_TRANS_NOFS) ? KM_NOFS : KM_SLEEP); |
| 278 | tp->t_magic = XFS_TRANS_HEADER_MAGIC; |
| 279 | tp->t_flags = flags; |
| 280 | tp->t_mountp = mp; |
| 281 | INIT_LIST_HEAD(&tp->t_items); |
| 282 | INIT_LIST_HEAD(&tp->t_busy); |
| 283 | |
| 284 | error = xfs_trans_reserve(tp, resp, blocks, rtextents); |
| 285 | if (error) { |
| 286 | xfs_trans_cancel(tp); |
| 287 | return error; |
| 288 | } |
| 289 | |
Dave Chinner | ba18781 | 2018-05-09 07:47:57 -0700 | [diff] [blame] | 290 | trace_xfs_trans_alloc(tp, _RET_IP_); |
| 291 | |
Christoph Hellwig | 253f491 | 2016-04-06 09:19:55 +1000 | [diff] [blame] | 292 | *tpp = tp; |
| 293 | return 0; |
| 294 | } |
| 295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | /* |
Darrick J. Wong | e89c041 | 2017-03-28 14:56:37 -0700 | [diff] [blame] | 297 | * Create an empty transaction with no reservation. This is a defensive |
| 298 | * mechanism for routines that query metadata without actually modifying |
| 299 | * them -- if the metadata being queried is somehow cross-linked (think a |
| 300 | * btree block pointer that points higher in the tree), we risk deadlock. |
| 301 | * However, blocks grabbed as part of a transaction can be re-grabbed. |
| 302 | * The verifiers will notice the corrupt block and the operation will fail |
| 303 | * back to userspace without deadlocking. |
| 304 | * |
| 305 | * Note the zero-length reservation; this transaction MUST be cancelled |
| 306 | * without any dirty data. |
| 307 | */ |
| 308 | int |
| 309 | xfs_trans_alloc_empty( |
| 310 | struct xfs_mount *mp, |
| 311 | struct xfs_trans **tpp) |
| 312 | { |
| 313 | struct xfs_trans_res resv = {0}; |
| 314 | |
| 315 | return xfs_trans_alloc(mp, &resv, 0, 0, XFS_TRANS_NO_WRITECOUNT, tpp); |
| 316 | } |
| 317 | |
| 318 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | * Record the indicated change to the given field for application |
| 320 | * to the file system's superblock when the transaction commits. |
| 321 | * For now, just store the change in the transaction structure. |
| 322 | * |
| 323 | * Mark the transaction structure to indicate that the superblock |
| 324 | * needs to be updated before committing. |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 325 | * |
| 326 | * Because we may not be keeping track of allocated/free inodes and |
| 327 | * used filesystem blocks in the superblock, we do not mark the |
| 328 | * superblock dirty in this transaction if we modify these fields. |
| 329 | * We still need to update the transaction deltas so that they get |
| 330 | * applied to the incore superblock, but we don't want them to |
| 331 | * cause the superblock to get locked and logged if these are the |
| 332 | * only fields in the superblock that the transaction modifies. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | */ |
| 334 | void |
| 335 | xfs_trans_mod_sb( |
| 336 | xfs_trans_t *tp, |
| 337 | uint field, |
David Chinner | 20f4ebf | 2007-02-10 18:36:10 +1100 | [diff] [blame] | 338 | int64_t delta) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | { |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 340 | uint32_t flags = (XFS_TRANS_DIRTY|XFS_TRANS_SB_DIRTY); |
| 341 | xfs_mount_t *mp = tp->t_mountp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
| 343 | switch (field) { |
| 344 | case XFS_TRANS_SB_ICOUNT: |
| 345 | tp->t_icount_delta += delta; |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 346 | if (xfs_sb_version_haslazysbcount(&mp->m_sb)) |
| 347 | flags &= ~XFS_TRANS_SB_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | break; |
| 349 | case XFS_TRANS_SB_IFREE: |
| 350 | tp->t_ifree_delta += delta; |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 351 | if (xfs_sb_version_haslazysbcount(&mp->m_sb)) |
| 352 | flags &= ~XFS_TRANS_SB_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | break; |
| 354 | case XFS_TRANS_SB_FDBLOCKS: |
| 355 | /* |
Brian Foster | 3e78b9a | 2018-03-09 14:01:58 -0800 | [diff] [blame] | 356 | * Track the number of blocks allocated in the transaction. |
| 357 | * Make sure it does not exceed the number reserved. If so, |
| 358 | * shutdown as this can lead to accounting inconsistency. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | */ |
| 360 | if (delta < 0) { |
| 361 | tp->t_blk_res_used += (uint)-delta; |
Brian Foster | 3e78b9a | 2018-03-09 14:01:58 -0800 | [diff] [blame] | 362 | if (tp->t_blk_res_used > tp->t_blk_res) |
| 363 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } |
| 365 | tp->t_fdblocks_delta += delta; |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 366 | if (xfs_sb_version_haslazysbcount(&mp->m_sb)) |
| 367 | flags &= ~XFS_TRANS_SB_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | break; |
| 369 | case XFS_TRANS_SB_RES_FDBLOCKS: |
| 370 | /* |
| 371 | * The allocation has already been applied to the |
| 372 | * in-core superblock's counter. This should only |
| 373 | * be applied to the on-disk superblock. |
| 374 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | tp->t_res_fdblocks_delta += delta; |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 376 | if (xfs_sb_version_haslazysbcount(&mp->m_sb)) |
| 377 | flags &= ~XFS_TRANS_SB_DIRTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | break; |
| 379 | case XFS_TRANS_SB_FREXTENTS: |
| 380 | /* |
| 381 | * Track the number of blocks allocated in the |
| 382 | * transaction. Make sure it does not exceed the |
| 383 | * number reserved. |
| 384 | */ |
| 385 | if (delta < 0) { |
| 386 | tp->t_rtx_res_used += (uint)-delta; |
| 387 | ASSERT(tp->t_rtx_res_used <= tp->t_rtx_res); |
| 388 | } |
| 389 | tp->t_frextents_delta += delta; |
| 390 | break; |
| 391 | case XFS_TRANS_SB_RES_FREXTENTS: |
| 392 | /* |
| 393 | * The allocation has already been applied to the |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 394 | * in-core superblock's counter. This should only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | * be applied to the on-disk superblock. |
| 396 | */ |
| 397 | ASSERT(delta < 0); |
| 398 | tp->t_res_frextents_delta += delta; |
| 399 | break; |
| 400 | case XFS_TRANS_SB_DBLOCKS: |
| 401 | ASSERT(delta > 0); |
| 402 | tp->t_dblocks_delta += delta; |
| 403 | break; |
| 404 | case XFS_TRANS_SB_AGCOUNT: |
| 405 | ASSERT(delta > 0); |
| 406 | tp->t_agcount_delta += delta; |
| 407 | break; |
| 408 | case XFS_TRANS_SB_IMAXPCT: |
| 409 | tp->t_imaxpct_delta += delta; |
| 410 | break; |
| 411 | case XFS_TRANS_SB_REXTSIZE: |
| 412 | tp->t_rextsize_delta += delta; |
| 413 | break; |
| 414 | case XFS_TRANS_SB_RBMBLOCKS: |
| 415 | tp->t_rbmblocks_delta += delta; |
| 416 | break; |
| 417 | case XFS_TRANS_SB_RBLOCKS: |
| 418 | tp->t_rblocks_delta += delta; |
| 419 | break; |
| 420 | case XFS_TRANS_SB_REXTENTS: |
| 421 | tp->t_rextents_delta += delta; |
| 422 | break; |
| 423 | case XFS_TRANS_SB_REXTSLOG: |
| 424 | tp->t_rextslog_delta += delta; |
| 425 | break; |
| 426 | default: |
| 427 | ASSERT(0); |
| 428 | return; |
| 429 | } |
| 430 | |
David Chinner | 210c6f1 | 2007-05-24 15:26:51 +1000 | [diff] [blame] | 431 | tp->t_flags |= flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* |
| 435 | * xfs_trans_apply_sb_deltas() is called from the commit code |
| 436 | * to bring the superblock buffer into the current transaction |
| 437 | * and modify it as requested by earlier calls to xfs_trans_mod_sb(). |
| 438 | * |
| 439 | * For now we just look at each field allowed to change and change |
| 440 | * it if necessary. |
| 441 | */ |
| 442 | STATIC void |
| 443 | xfs_trans_apply_sb_deltas( |
| 444 | xfs_trans_t *tp) |
| 445 | { |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 446 | xfs_dsb_t *sbp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | xfs_buf_t *bp; |
| 448 | int whole = 0; |
| 449 | |
| 450 | bp = xfs_trans_getsb(tp, tp->t_mountp, 0); |
| 451 | sbp = XFS_BUF_TO_SBP(bp); |
| 452 | |
| 453 | /* |
| 454 | * Check that superblock mods match the mods made to AGF counters. |
| 455 | */ |
| 456 | ASSERT((tp->t_fdblocks_delta + tp->t_res_fdblocks_delta) == |
| 457 | (tp->t_ag_freeblks_delta + tp->t_ag_flist_delta + |
| 458 | tp->t_ag_btree_delta)); |
| 459 | |
David Chinner | 92821e2 | 2007-05-24 15:26:31 +1000 | [diff] [blame] | 460 | /* |
| 461 | * Only update the superblock counters if we are logging them |
| 462 | */ |
| 463 | if (!xfs_sb_version_haslazysbcount(&(tp->t_mountp->m_sb))) { |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 464 | if (tp->t_icount_delta) |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 465 | be64_add_cpu(&sbp->sb_icount, tp->t_icount_delta); |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 466 | if (tp->t_ifree_delta) |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 467 | be64_add_cpu(&sbp->sb_ifree, tp->t_ifree_delta); |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 468 | if (tp->t_fdblocks_delta) |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 469 | be64_add_cpu(&sbp->sb_fdblocks, tp->t_fdblocks_delta); |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 470 | if (tp->t_res_fdblocks_delta) |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 471 | be64_add_cpu(&sbp->sb_fdblocks, tp->t_res_fdblocks_delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 474 | if (tp->t_frextents_delta) |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 475 | be64_add_cpu(&sbp->sb_frextents, tp->t_frextents_delta); |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 476 | if (tp->t_res_frextents_delta) |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 477 | be64_add_cpu(&sbp->sb_frextents, tp->t_res_frextents_delta); |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 478 | |
| 479 | if (tp->t_dblocks_delta) { |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 480 | be64_add_cpu(&sbp->sb_dblocks, tp->t_dblocks_delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | whole = 1; |
| 482 | } |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 483 | if (tp->t_agcount_delta) { |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 484 | be32_add_cpu(&sbp->sb_agcount, tp->t_agcount_delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | whole = 1; |
| 486 | } |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 487 | if (tp->t_imaxpct_delta) { |
| 488 | sbp->sb_imax_pct += tp->t_imaxpct_delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | whole = 1; |
| 490 | } |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 491 | if (tp->t_rextsize_delta) { |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 492 | be32_add_cpu(&sbp->sb_rextsize, tp->t_rextsize_delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | whole = 1; |
| 494 | } |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 495 | if (tp->t_rbmblocks_delta) { |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 496 | be32_add_cpu(&sbp->sb_rbmblocks, tp->t_rbmblocks_delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | whole = 1; |
| 498 | } |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 499 | if (tp->t_rblocks_delta) { |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 500 | be64_add_cpu(&sbp->sb_rblocks, tp->t_rblocks_delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | whole = 1; |
| 502 | } |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 503 | if (tp->t_rextents_delta) { |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 504 | be64_add_cpu(&sbp->sb_rextents, tp->t_rextents_delta); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | whole = 1; |
| 506 | } |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 507 | if (tp->t_rextslog_delta) { |
| 508 | sbp->sb_rextslog += tp->t_rextslog_delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | whole = 1; |
| 510 | } |
| 511 | |
Dave Chinner | 3443a3b | 2015-01-22 09:30:23 +1100 | [diff] [blame] | 512 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | if (whole) |
| 514 | /* |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 515 | * Log the whole thing, the fields are noncontiguous. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | */ |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 517 | xfs_trans_log_buf(tp, bp, 0, sizeof(xfs_dsb_t) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | else |
| 519 | /* |
| 520 | * Since all the modifiable fields are contiguous, we |
| 521 | * can get away with this. |
| 522 | */ |
Christoph Hellwig | 2bdf7cd | 2007-08-28 13:58:06 +1000 | [diff] [blame] | 523 | xfs_trans_log_buf(tp, bp, offsetof(xfs_dsb_t, sb_icount), |
| 524 | offsetof(xfs_dsb_t, sb_frextents) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | sizeof(sbp->sb_frextents) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Dave Chinner | 0bd5dded | 2015-02-23 21:24:11 +1100 | [diff] [blame] | 528 | STATIC int |
| 529 | xfs_sb_mod8( |
| 530 | uint8_t *field, |
| 531 | int8_t delta) |
| 532 | { |
| 533 | int8_t counter = *field; |
| 534 | |
| 535 | counter += delta; |
| 536 | if (counter < 0) { |
| 537 | ASSERT(0); |
| 538 | return -EINVAL; |
| 539 | } |
| 540 | *field = counter; |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | STATIC int |
| 545 | xfs_sb_mod32( |
| 546 | uint32_t *field, |
| 547 | int32_t delta) |
| 548 | { |
| 549 | int32_t counter = *field; |
| 550 | |
| 551 | counter += delta; |
| 552 | if (counter < 0) { |
| 553 | ASSERT(0); |
| 554 | return -EINVAL; |
| 555 | } |
| 556 | *field = counter; |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | STATIC int |
| 561 | xfs_sb_mod64( |
| 562 | uint64_t *field, |
| 563 | int64_t delta) |
| 564 | { |
| 565 | int64_t counter = *field; |
| 566 | |
| 567 | counter += delta; |
| 568 | if (counter < 0) { |
| 569 | ASSERT(0); |
| 570 | return -EINVAL; |
| 571 | } |
| 572 | *field = counter; |
| 573 | return 0; |
| 574 | } |
| 575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | /* |
David Chinner | 45c3414 | 2007-06-18 16:49:44 +1000 | [diff] [blame] | 577 | * xfs_trans_unreserve_and_mod_sb() is called to release unused reservations |
| 578 | * and apply superblock counter changes to the in-core superblock. The |
| 579 | * t_res_fdblocks_delta and t_res_frextents_delta fields are explicitly NOT |
| 580 | * applied to the in-core superblock. The idea is that that has already been |
| 581 | * done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | * |
David Chinner | 45c3414 | 2007-06-18 16:49:44 +1000 | [diff] [blame] | 583 | * If we are not logging superblock counters, then the inode allocated/free and |
| 584 | * used block counts are not updated in the on disk superblock. In this case, |
| 585 | * XFS_TRANS_SB_DIRTY will not be set when the transaction is updated but we |
| 586 | * still need to update the incore superblock with the changes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | */ |
Dave Chinner | 71e330b | 2010-05-21 14:37:18 +1000 | [diff] [blame] | 588 | void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | xfs_trans_unreserve_and_mod_sb( |
Dave Chinner | 0bd5dded | 2015-02-23 21:24:11 +1100 | [diff] [blame] | 590 | struct xfs_trans *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | { |
Dave Chinner | 0bd5dded | 2015-02-23 21:24:11 +1100 | [diff] [blame] | 592 | struct xfs_mount *mp = tp->t_mountp; |
| 593 | bool rsvd = (tp->t_flags & XFS_TRANS_RESERVE) != 0; |
| 594 | int64_t blkdelta = 0; |
| 595 | int64_t rtxdelta = 0; |
| 596 | int64_t idelta = 0; |
| 597 | int64_t ifreedelta = 0; |
| 598 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
Christoph Hellwig | 1b04071 | 2010-09-30 02:25:56 +0000 | [diff] [blame] | 600 | /* calculate deltas */ |
David Chinner | 45c3414 | 2007-06-18 16:49:44 +1000 | [diff] [blame] | 601 | if (tp->t_blk_res > 0) |
| 602 | blkdelta = tp->t_blk_res; |
David Chinner | 45c3414 | 2007-06-18 16:49:44 +1000 | [diff] [blame] | 603 | if ((tp->t_fdblocks_delta != 0) && |
| 604 | (xfs_sb_version_haslazysbcount(&mp->m_sb) || |
| 605 | (tp->t_flags & XFS_TRANS_SB_DIRTY))) |
| 606 | blkdelta += tp->t_fdblocks_delta; |
| 607 | |
David Chinner | 45c3414 | 2007-06-18 16:49:44 +1000 | [diff] [blame] | 608 | if (tp->t_rtx_res > 0) |
| 609 | rtxdelta = tp->t_rtx_res; |
David Chinner | 45c3414 | 2007-06-18 16:49:44 +1000 | [diff] [blame] | 610 | if ((tp->t_frextents_delta != 0) && |
| 611 | (tp->t_flags & XFS_TRANS_SB_DIRTY)) |
| 612 | rtxdelta += tp->t_frextents_delta; |
| 613 | |
Christoph Hellwig | 1b04071 | 2010-09-30 02:25:56 +0000 | [diff] [blame] | 614 | if (xfs_sb_version_haslazysbcount(&mp->m_sb) || |
| 615 | (tp->t_flags & XFS_TRANS_SB_DIRTY)) { |
| 616 | idelta = tp->t_icount_delta; |
| 617 | ifreedelta = tp->t_ifree_delta; |
| 618 | } |
| 619 | |
| 620 | /* apply the per-cpu counters */ |
| 621 | if (blkdelta) { |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 622 | error = xfs_mod_fdblocks(mp, blkdelta, rsvd); |
Christoph Hellwig | 1b04071 | 2010-09-30 02:25:56 +0000 | [diff] [blame] | 623 | if (error) |
| 624 | goto out; |
| 625 | } |
| 626 | |
| 627 | if (idelta) { |
Dave Chinner | 501ab32 | 2015-02-23 21:19:28 +1100 | [diff] [blame] | 628 | error = xfs_mod_icount(mp, idelta); |
Christoph Hellwig | 1b04071 | 2010-09-30 02:25:56 +0000 | [diff] [blame] | 629 | if (error) |
| 630 | goto out_undo_fdblocks; |
| 631 | } |
| 632 | |
| 633 | if (ifreedelta) { |
Dave Chinner | e88b64e | 2015-02-23 21:19:53 +1100 | [diff] [blame] | 634 | error = xfs_mod_ifree(mp, ifreedelta); |
Christoph Hellwig | 1b04071 | 2010-09-30 02:25:56 +0000 | [diff] [blame] | 635 | if (error) |
| 636 | goto out_undo_icount; |
| 637 | } |
| 638 | |
Dave Chinner | 0bd5dded | 2015-02-23 21:24:11 +1100 | [diff] [blame] | 639 | if (rtxdelta == 0 && !(tp->t_flags & XFS_TRANS_SB_DIRTY)) |
| 640 | return; |
| 641 | |
Christoph Hellwig | 1b04071 | 2010-09-30 02:25:56 +0000 | [diff] [blame] | 642 | /* apply remaining deltas */ |
Dave Chinner | 0bd5dded | 2015-02-23 21:24:11 +1100 | [diff] [blame] | 643 | spin_lock(&mp->m_sb_lock); |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 644 | if (rtxdelta) { |
Dave Chinner | 0bd5dded | 2015-02-23 21:24:11 +1100 | [diff] [blame] | 645 | error = xfs_sb_mod64(&mp->m_sb.sb_frextents, rtxdelta); |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 646 | if (error) |
| 647 | goto out_undo_ifree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Dave Chinner | 0bd5dded | 2015-02-23 21:24:11 +1100 | [diff] [blame] | 650 | if (tp->t_dblocks_delta != 0) { |
| 651 | error = xfs_sb_mod64(&mp->m_sb.sb_dblocks, tp->t_dblocks_delta); |
Christoph Hellwig | 1b04071 | 2010-09-30 02:25:56 +0000 | [diff] [blame] | 652 | if (error) |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 653 | goto out_undo_frextents; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | } |
Dave Chinner | 0bd5dded | 2015-02-23 21:24:11 +1100 | [diff] [blame] | 655 | if (tp->t_agcount_delta != 0) { |
| 656 | error = xfs_sb_mod32(&mp->m_sb.sb_agcount, tp->t_agcount_delta); |
| 657 | if (error) |
| 658 | goto out_undo_dblocks; |
| 659 | } |
| 660 | if (tp->t_imaxpct_delta != 0) { |
| 661 | error = xfs_sb_mod8(&mp->m_sb.sb_imax_pct, tp->t_imaxpct_delta); |
| 662 | if (error) |
| 663 | goto out_undo_agcount; |
| 664 | } |
| 665 | if (tp->t_rextsize_delta != 0) { |
| 666 | error = xfs_sb_mod32(&mp->m_sb.sb_rextsize, |
| 667 | tp->t_rextsize_delta); |
| 668 | if (error) |
| 669 | goto out_undo_imaxpct; |
| 670 | } |
| 671 | if (tp->t_rbmblocks_delta != 0) { |
| 672 | error = xfs_sb_mod32(&mp->m_sb.sb_rbmblocks, |
| 673 | tp->t_rbmblocks_delta); |
| 674 | if (error) |
| 675 | goto out_undo_rextsize; |
| 676 | } |
| 677 | if (tp->t_rblocks_delta != 0) { |
| 678 | error = xfs_sb_mod64(&mp->m_sb.sb_rblocks, tp->t_rblocks_delta); |
| 679 | if (error) |
| 680 | goto out_undo_rbmblocks; |
| 681 | } |
| 682 | if (tp->t_rextents_delta != 0) { |
| 683 | error = xfs_sb_mod64(&mp->m_sb.sb_rextents, |
| 684 | tp->t_rextents_delta); |
| 685 | if (error) |
| 686 | goto out_undo_rblocks; |
| 687 | } |
| 688 | if (tp->t_rextslog_delta != 0) { |
| 689 | error = xfs_sb_mod8(&mp->m_sb.sb_rextslog, |
| 690 | tp->t_rextslog_delta); |
| 691 | if (error) |
| 692 | goto out_undo_rextents; |
| 693 | } |
| 694 | spin_unlock(&mp->m_sb_lock); |
Christoph Hellwig | 1b04071 | 2010-09-30 02:25:56 +0000 | [diff] [blame] | 695 | return; |
| 696 | |
Dave Chinner | 0bd5dded | 2015-02-23 21:24:11 +1100 | [diff] [blame] | 697 | out_undo_rextents: |
| 698 | if (tp->t_rextents_delta) |
| 699 | xfs_sb_mod64(&mp->m_sb.sb_rextents, -tp->t_rextents_delta); |
| 700 | out_undo_rblocks: |
| 701 | if (tp->t_rblocks_delta) |
| 702 | xfs_sb_mod64(&mp->m_sb.sb_rblocks, -tp->t_rblocks_delta); |
| 703 | out_undo_rbmblocks: |
| 704 | if (tp->t_rbmblocks_delta) |
| 705 | xfs_sb_mod32(&mp->m_sb.sb_rbmblocks, -tp->t_rbmblocks_delta); |
| 706 | out_undo_rextsize: |
| 707 | if (tp->t_rextsize_delta) |
| 708 | xfs_sb_mod32(&mp->m_sb.sb_rextsize, -tp->t_rextsize_delta); |
| 709 | out_undo_imaxpct: |
| 710 | if (tp->t_rextsize_delta) |
| 711 | xfs_sb_mod8(&mp->m_sb.sb_imax_pct, -tp->t_imaxpct_delta); |
| 712 | out_undo_agcount: |
| 713 | if (tp->t_agcount_delta) |
| 714 | xfs_sb_mod32(&mp->m_sb.sb_agcount, -tp->t_agcount_delta); |
| 715 | out_undo_dblocks: |
| 716 | if (tp->t_dblocks_delta) |
| 717 | xfs_sb_mod64(&mp->m_sb.sb_dblocks, -tp->t_dblocks_delta); |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 718 | out_undo_frextents: |
| 719 | if (rtxdelta) |
Dave Chinner | 0bd5dded | 2015-02-23 21:24:11 +1100 | [diff] [blame] | 720 | xfs_sb_mod64(&mp->m_sb.sb_frextents, -rtxdelta); |
Dave Chinner | bab98bb | 2015-02-23 21:22:54 +1100 | [diff] [blame] | 721 | out_undo_ifree: |
Dave Chinner | 0bd5dded | 2015-02-23 21:24:11 +1100 | [diff] [blame] | 722 | spin_unlock(&mp->m_sb_lock); |
Christoph Hellwig | 1b04071 | 2010-09-30 02:25:56 +0000 | [diff] [blame] | 723 | if (ifreedelta) |
Dave Chinner | e88b64e | 2015-02-23 21:19:53 +1100 | [diff] [blame] | 724 | xfs_mod_ifree(mp, -ifreedelta); |
Christoph Hellwig | 1b04071 | 2010-09-30 02:25:56 +0000 | [diff] [blame] | 725 | out_undo_icount: |
| 726 | if (idelta) |
Dave Chinner | 501ab32 | 2015-02-23 21:19:28 +1100 | [diff] [blame] | 727 | xfs_mod_icount(mp, -idelta); |
Christoph Hellwig | 1b04071 | 2010-09-30 02:25:56 +0000 | [diff] [blame] | 728 | out_undo_fdblocks: |
| 729 | if (blkdelta) |
Dave Chinner | 0d485ad | 2015-02-23 21:22:03 +1100 | [diff] [blame] | 730 | xfs_mod_fdblocks(mp, -blkdelta, rsvd); |
Christoph Hellwig | 1b04071 | 2010-09-30 02:25:56 +0000 | [diff] [blame] | 731 | out: |
Jesper Juhl | 1884bd8 | 2010-12-25 20:14:53 +0000 | [diff] [blame] | 732 | ASSERT(error == 0); |
Christoph Hellwig | 1b04071 | 2010-09-30 02:25:56 +0000 | [diff] [blame] | 733 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame^] | 736 | /* Add the given log item to the transaction's list of log items. */ |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 737 | void |
| 738 | xfs_trans_add_item( |
| 739 | struct xfs_trans *tp, |
| 740 | struct xfs_log_item *lip) |
| 741 | { |
Jesper Juhl | f65020a | 2012-02-13 20:51:05 +0000 | [diff] [blame] | 742 | ASSERT(lip->li_mountp == tp->t_mountp); |
| 743 | ASSERT(lip->li_ailp == tp->t_mountp->m_ail); |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame^] | 744 | ASSERT(list_empty(&lip->li_trans)); |
| 745 | ASSERT(!test_bit(XFS_LI_DIRTY, &lip->li_flags)); |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 746 | |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame^] | 747 | list_add_tail(&lip->li_trans, &tp->t_items); |
Dave Chinner | ba18781 | 2018-05-09 07:47:57 -0700 | [diff] [blame] | 748 | trace_xfs_trans_add_item(tp, _RET_IP_); |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 749 | } |
| 750 | |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 751 | /* |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame^] | 752 | * Unlink the log item from the transaction. the log item is no longer |
| 753 | * considered dirty in this transaction, as the linked transaction has |
| 754 | * finished, either by abort or commit completion. |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 755 | */ |
| 756 | void |
| 757 | xfs_trans_del_item( |
| 758 | struct xfs_log_item *lip) |
| 759 | { |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame^] | 760 | clear_bit(XFS_LI_DIRTY, &lip->li_flags); |
| 761 | list_del_init(&lip->li_trans); |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 762 | } |
| 763 | |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame^] | 764 | /* Detach and unlock all of the items in a transaction */ |
Dave Chinner | d17c701 | 2010-08-24 11:42:52 +1000 | [diff] [blame] | 765 | void |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 766 | xfs_trans_free_items( |
| 767 | struct xfs_trans *tp, |
| 768 | xfs_lsn_t commit_lsn, |
Christoph Hellwig | eacb24e | 2015-06-04 13:47:43 +1000 | [diff] [blame] | 769 | bool abort) |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 770 | { |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame^] | 771 | struct xfs_log_item *lip, *next; |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 772 | |
Dave Chinner | ba18781 | 2018-05-09 07:47:57 -0700 | [diff] [blame] | 773 | trace_xfs_trans_free_items(tp, _RET_IP_); |
| 774 | |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame^] | 775 | list_for_each_entry_safe(lip, next, &tp->t_items, li_trans) { |
| 776 | xfs_trans_del_item(lip); |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 777 | if (commit_lsn != NULLCOMMITLSN) |
Dave Chinner | 904c17e | 2013-08-28 21:12:03 +1000 | [diff] [blame] | 778 | lip->li_ops->iop_committing(lip, commit_lsn); |
Christoph Hellwig | eacb24e | 2015-06-04 13:47:43 +1000 | [diff] [blame] | 779 | if (abort) |
Dave Chinner | 22525c1 | 2018-05-09 07:47:34 -0700 | [diff] [blame] | 780 | set_bit(XFS_LI_ABORTED, &lip->li_flags); |
Dave Chinner | 904c17e | 2013-08-28 21:12:03 +1000 | [diff] [blame] | 781 | lip->li_ops->iop_unlock(lip); |
Christoph Hellwig | e98c414 | 2010-06-23 18:11:15 +1000 | [diff] [blame] | 782 | } |
| 783 | } |
| 784 | |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 785 | static inline void |
| 786 | xfs_log_item_batch_insert( |
| 787 | struct xfs_ail *ailp, |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 788 | struct xfs_ail_cursor *cur, |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 789 | struct xfs_log_item **log_items, |
| 790 | int nr_items, |
| 791 | xfs_lsn_t commit_lsn) |
| 792 | { |
| 793 | int i; |
| 794 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 795 | spin_lock(&ailp->ail_lock); |
| 796 | /* xfs_trans_ail_update_bulk drops ailp->ail_lock */ |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 797 | xfs_trans_ail_update_bulk(ailp, cur, log_items, nr_items, commit_lsn); |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 798 | |
Dave Chinner | 904c17e | 2013-08-28 21:12:03 +1000 | [diff] [blame] | 799 | for (i = 0; i < nr_items; i++) { |
| 800 | struct xfs_log_item *lip = log_items[i]; |
| 801 | |
| 802 | lip->li_ops->iop_unpin(lip, 0); |
| 803 | } |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | /* |
| 807 | * Bulk operation version of xfs_trans_committed that takes a log vector of |
| 808 | * items to insert into the AIL. This uses bulk AIL insertion techniques to |
| 809 | * minimise lock traffic. |
Dave Chinner | e34a314 | 2011-01-27 12:13:35 +1100 | [diff] [blame] | 810 | * |
| 811 | * If we are called with the aborted flag set, it is because a log write during |
| 812 | * a CIL checkpoint commit has failed. In this case, all the items in the |
Dave Chinner | 904c17e | 2013-08-28 21:12:03 +1000 | [diff] [blame] | 813 | * checkpoint have already gone through iop_commited and iop_unlock, which |
Dave Chinner | e34a314 | 2011-01-27 12:13:35 +1100 | [diff] [blame] | 814 | * means that checkpoint commit abort handling is treated exactly the same |
| 815 | * as an iclog write error even though we haven't started any IO yet. Hence in |
Dave Chinner | 904c17e | 2013-08-28 21:12:03 +1000 | [diff] [blame] | 816 | * this case all we need to do is iop_committed processing, followed by an |
| 817 | * iop_unpin(aborted) call. |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 818 | * |
| 819 | * The AIL cursor is used to optimise the insert process. If commit_lsn is not |
| 820 | * at the end of the AIL, the insert cursor avoids the need to walk |
| 821 | * the AIL to find the insertion point on every xfs_log_item_batch_insert() |
| 822 | * call. This saves a lot of needless list walking and is a net win, even |
| 823 | * though it slightly increases that amount of AIL lock traffic to set it up |
| 824 | * and tear it down. |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 825 | */ |
| 826 | void |
| 827 | xfs_trans_committed_bulk( |
| 828 | struct xfs_ail *ailp, |
| 829 | struct xfs_log_vec *log_vector, |
| 830 | xfs_lsn_t commit_lsn, |
| 831 | int aborted) |
| 832 | { |
| 833 | #define LOG_ITEM_BATCH_SIZE 32 |
| 834 | struct xfs_log_item *log_items[LOG_ITEM_BATCH_SIZE]; |
| 835 | struct xfs_log_vec *lv; |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 836 | struct xfs_ail_cursor cur; |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 837 | int i = 0; |
| 838 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 839 | spin_lock(&ailp->ail_lock); |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 840 | xfs_trans_ail_cursor_last(ailp, &cur, commit_lsn); |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 841 | spin_unlock(&ailp->ail_lock); |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 842 | |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 843 | /* unpin all the log items */ |
| 844 | for (lv = log_vector; lv; lv = lv->lv_next ) { |
| 845 | struct xfs_log_item *lip = lv->lv_item; |
| 846 | xfs_lsn_t item_lsn; |
| 847 | |
| 848 | if (aborted) |
Dave Chinner | 22525c1 | 2018-05-09 07:47:34 -0700 | [diff] [blame] | 849 | set_bit(XFS_LI_ABORTED, &lip->li_flags); |
Dave Chinner | 904c17e | 2013-08-28 21:12:03 +1000 | [diff] [blame] | 850 | item_lsn = lip->li_ops->iop_committed(lip, commit_lsn); |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 851 | |
Dave Chinner | 1316d4d | 2011-07-04 05:27:36 +0000 | [diff] [blame] | 852 | /* item_lsn of -1 means the item needs no further processing */ |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 853 | if (XFS_LSN_CMP(item_lsn, (xfs_lsn_t)-1) == 0) |
| 854 | continue; |
| 855 | |
Dave Chinner | e34a314 | 2011-01-27 12:13:35 +1100 | [diff] [blame] | 856 | /* |
| 857 | * if we are aborting the operation, no point in inserting the |
| 858 | * object into the AIL as we are in a shutdown situation. |
| 859 | */ |
| 860 | if (aborted) { |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 861 | ASSERT(XFS_FORCED_SHUTDOWN(ailp->ail_mount)); |
Dave Chinner | 904c17e | 2013-08-28 21:12:03 +1000 | [diff] [blame] | 862 | lip->li_ops->iop_unpin(lip, 1); |
Dave Chinner | e34a314 | 2011-01-27 12:13:35 +1100 | [diff] [blame] | 863 | continue; |
| 864 | } |
| 865 | |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 866 | if (item_lsn != commit_lsn) { |
| 867 | |
| 868 | /* |
| 869 | * Not a bulk update option due to unusual item_lsn. |
| 870 | * Push into AIL immediately, rechecking the lsn once |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 871 | * we have the ail lock. Then unpin the item. This does |
| 872 | * not affect the AIL cursor the bulk insert path is |
| 873 | * using. |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 874 | */ |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 875 | spin_lock(&ailp->ail_lock); |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 876 | if (XFS_LSN_CMP(item_lsn, lip->li_lsn) > 0) |
| 877 | xfs_trans_ail_update(ailp, lip, item_lsn); |
| 878 | else |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 879 | spin_unlock(&ailp->ail_lock); |
Dave Chinner | 904c17e | 2013-08-28 21:12:03 +1000 | [diff] [blame] | 880 | lip->li_ops->iop_unpin(lip, 0); |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 881 | continue; |
| 882 | } |
| 883 | |
| 884 | /* Item is a candidate for bulk AIL insert. */ |
| 885 | log_items[i++] = lv->lv_item; |
| 886 | if (i >= LOG_ITEM_BATCH_SIZE) { |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 887 | xfs_log_item_batch_insert(ailp, &cur, log_items, |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 888 | LOG_ITEM_BATCH_SIZE, commit_lsn); |
| 889 | i = 0; |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | /* make sure we insert the remainder! */ |
| 894 | if (i) |
Dave Chinner | 1d8c95a | 2011-07-18 03:40:16 +0000 | [diff] [blame] | 895 | xfs_log_item_batch_insert(ailp, &cur, log_items, i, commit_lsn); |
| 896 | |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 897 | spin_lock(&ailp->ail_lock); |
Eric Sandeen | e4a1e29 | 2014-04-14 19:06:05 +1000 | [diff] [blame] | 898 | xfs_trans_ail_cursor_done(&cur); |
Matthew Wilcox | 57e8095 | 2018-03-07 14:59:39 -0800 | [diff] [blame] | 899 | spin_unlock(&ailp->ail_lock); |
Dave Chinner | 0e57f6a | 2010-12-20 12:02:19 +1100 | [diff] [blame] | 900 | } |
| 901 | |
Dave Chinner | b1c1b5b | 2010-03-23 10:11:05 +1100 | [diff] [blame] | 902 | /* |
Christoph Hellwig | b103705 | 2011-09-19 14:55:51 +0000 | [diff] [blame] | 903 | * Commit the given transaction to the log. |
Dave Chinner | 0924378 | 2010-03-08 11:28:28 +1100 | [diff] [blame] | 904 | * |
| 905 | * XFS disk error handling mechanism is not based on a typical |
| 906 | * transaction abort mechanism. Logically after the filesystem |
| 907 | * gets marked 'SHUTDOWN', we can't let any new transactions |
| 908 | * be durable - ie. committed to disk - because some metadata might |
| 909 | * be inconsistent. In such cases, this returns an error, and the |
| 910 | * caller may assume that all locked objects joined to the transaction |
| 911 | * have already been unlocked as if the commit had succeeded. |
| 912 | * Do not reference the transaction structure after this call. |
| 913 | */ |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 914 | static int |
| 915 | __xfs_trans_commit( |
Christoph Hellwig | a3ccd2c | 2010-03-15 12:52:49 +1100 | [diff] [blame] | 916 | struct xfs_trans *tp, |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 917 | bool regrant) |
Dave Chinner | 0924378 | 2010-03-08 11:28:28 +1100 | [diff] [blame] | 918 | { |
Christoph Hellwig | a3ccd2c | 2010-03-15 12:52:49 +1100 | [diff] [blame] | 919 | struct xfs_mount *mp = tp->t_mountp; |
Dave Chinner | 0924378 | 2010-03-08 11:28:28 +1100 | [diff] [blame] | 920 | xfs_lsn_t commit_lsn = -1; |
Christoph Hellwig | a3ccd2c | 2010-03-15 12:52:49 +1100 | [diff] [blame] | 921 | int error = 0; |
Dave Chinner | 0924378 | 2010-03-08 11:28:28 +1100 | [diff] [blame] | 922 | int sync = tp->t_flags & XFS_TRANS_SYNC; |
Dave Chinner | 0924378 | 2010-03-08 11:28:28 +1100 | [diff] [blame] | 923 | |
Brian Foster | f8f2835 | 2018-05-07 17:38:47 -0700 | [diff] [blame] | 924 | ASSERT(!tp->t_agfl_dfops || |
| 925 | !xfs_defer_has_unfinished_work(tp->t_agfl_dfops) || regrant); |
| 926 | |
Dave Chinner | ba18781 | 2018-05-09 07:47:57 -0700 | [diff] [blame] | 927 | trace_xfs_trans_commit(tp, _RET_IP_); |
| 928 | |
Dave Chinner | 0924378 | 2010-03-08 11:28:28 +1100 | [diff] [blame] | 929 | /* |
Dave Chinner | 0924378 | 2010-03-08 11:28:28 +1100 | [diff] [blame] | 930 | * If there is nothing to be logged by the transaction, |
| 931 | * then unlock all of the items associated with the |
| 932 | * transaction and free the transaction structure. |
| 933 | * Also make sure to return any reserved blocks to |
| 934 | * the free pool. |
| 935 | */ |
Christoph Hellwig | a3ccd2c | 2010-03-15 12:52:49 +1100 | [diff] [blame] | 936 | if (!(tp->t_flags & XFS_TRANS_DIRTY)) |
| 937 | goto out_unreserve; |
| 938 | |
| 939 | if (XFS_FORCED_SHUTDOWN(mp)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 940 | error = -EIO; |
Christoph Hellwig | a3ccd2c | 2010-03-15 12:52:49 +1100 | [diff] [blame] | 941 | goto out_unreserve; |
Dave Chinner | 0924378 | 2010-03-08 11:28:28 +1100 | [diff] [blame] | 942 | } |
Christoph Hellwig | a3ccd2c | 2010-03-15 12:52:49 +1100 | [diff] [blame] | 943 | |
Dave Chinner | 0924378 | 2010-03-08 11:28:28 +1100 | [diff] [blame] | 944 | ASSERT(tp->t_ticket != NULL); |
| 945 | |
| 946 | /* |
| 947 | * If we need to update the superblock, then do it now. |
| 948 | */ |
| 949 | if (tp->t_flags & XFS_TRANS_SB_DIRTY) |
| 950 | xfs_trans_apply_sb_deltas(tp); |
| 951 | xfs_trans_apply_dquot_deltas(tp); |
| 952 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 953 | xfs_log_commit_cil(mp, tp, &commit_lsn, regrant); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | |
Michal Hocko | 9070733 | 2017-05-03 14:53:12 -0700 | [diff] [blame] | 955 | current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); |
Christoph Hellwig | 0244b96 | 2011-12-06 21:58:08 +0000 | [diff] [blame] | 956 | xfs_trans_free(tp); |
| 957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | /* |
| 959 | * If the transaction needs to be synchronous, then force the |
| 960 | * log out now and wait for it. |
| 961 | */ |
| 962 | if (sync) { |
Christoph Hellwig | 656de4f | 2018-03-13 23:15:28 -0700 | [diff] [blame] | 963 | error = xfs_log_force_lsn(mp, commit_lsn, XFS_LOG_SYNC, NULL); |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 964 | XFS_STATS_INC(mp, xs_trans_sync); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | } else { |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 966 | XFS_STATS_INC(mp, xs_trans_async); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | } |
| 968 | |
Christoph Hellwig | a3ccd2c | 2010-03-15 12:52:49 +1100 | [diff] [blame] | 969 | return error; |
| 970 | |
| 971 | out_unreserve: |
| 972 | xfs_trans_unreserve_and_mod_sb(tp); |
| 973 | |
| 974 | /* |
| 975 | * It is indeed possible for the transaction to be not dirty but |
| 976 | * the dqinfo portion to be. All that means is that we have some |
| 977 | * (non-persistent) quota reservations that need to be unreserved. |
| 978 | */ |
| 979 | xfs_trans_unreserve_and_mod_dquots(tp); |
| 980 | if (tp->t_ticket) { |
Christoph Hellwig | f78c390 | 2015-06-04 13:48:20 +1000 | [diff] [blame] | 981 | commit_lsn = xfs_log_done(mp, tp->t_ticket, NULL, regrant); |
Christoph Hellwig | a3ccd2c | 2010-03-15 12:52:49 +1100 | [diff] [blame] | 982 | if (commit_lsn == -1 && !error) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 983 | error = -EIO; |
Dave Chinner | ba18781 | 2018-05-09 07:47:57 -0700 | [diff] [blame] | 984 | tp->t_ticket = NULL; |
Christoph Hellwig | a3ccd2c | 2010-03-15 12:52:49 +1100 | [diff] [blame] | 985 | } |
Michal Hocko | 9070733 | 2017-05-03 14:53:12 -0700 | [diff] [blame] | 986 | current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); |
Christoph Hellwig | eacb24e | 2015-06-04 13:47:43 +1000 | [diff] [blame] | 987 | xfs_trans_free_items(tp, NULLCOMMITLSN, !!error); |
Christoph Hellwig | a3ccd2c | 2010-03-15 12:52:49 +1100 | [diff] [blame] | 988 | xfs_trans_free(tp); |
| 989 | |
Bill O'Donnell | ff6d6af | 2015-10-12 18:21:22 +1100 | [diff] [blame] | 990 | XFS_STATS_INC(mp, xs_trans_empty); |
Christoph Hellwig | a3ccd2c | 2010-03-15 12:52:49 +1100 | [diff] [blame] | 991 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | } |
| 993 | |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 994 | int |
| 995 | xfs_trans_commit( |
| 996 | struct xfs_trans *tp) |
| 997 | { |
| 998 | return __xfs_trans_commit(tp, false); |
| 999 | } |
| 1000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | * Unlock all of the transaction's items and free the transaction. |
| 1003 | * The transaction must not have modified any of its items, because |
| 1004 | * there is no way to restore them to their previous state. |
| 1005 | * |
| 1006 | * If the transaction has made a log reservation, make sure to release |
| 1007 | * it as well. |
| 1008 | */ |
| 1009 | void |
| 1010 | xfs_trans_cancel( |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1011 | struct xfs_trans *tp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | { |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1013 | struct xfs_mount *mp = tp->t_mountp; |
| 1014 | bool dirty = (tp->t_flags & XFS_TRANS_DIRTY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
Dave Chinner | ba18781 | 2018-05-09 07:47:57 -0700 | [diff] [blame] | 1016 | trace_xfs_trans_cancel(tp, _RET_IP_); |
| 1017 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | * See if the caller is relying on us to shut down the |
| 1020 | * filesystem. This happens in paths where we detect |
| 1021 | * corruption and decide to give up. |
| 1022 | */ |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1023 | if (dirty && !XFS_FORCED_SHUTDOWN(mp)) { |
Ryan Hankins | 0733af2 | 2006-01-11 15:36:44 +1100 | [diff] [blame] | 1024 | XFS_ERROR_REPORT("xfs_trans_cancel", XFS_ERRLEVEL_LOW, mp); |
Nathan Scott | 7d04a33 | 2006-06-09 14:58:38 +1000 | [diff] [blame] | 1025 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
Nathan Scott | 60a204f | 2006-01-11 15:37:00 +1100 | [diff] [blame] | 1026 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | #ifdef DEBUG |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1028 | if (!dirty && !XFS_FORCED_SHUTDOWN(mp)) { |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame^] | 1029 | struct xfs_log_item *lip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | |
Dave Chinner | e6631f8 | 2018-05-09 07:49:37 -0700 | [diff] [blame^] | 1031 | list_for_each_entry(lip, &tp->t_items, li_trans) |
| 1032 | ASSERT(!(lip->li_type == XFS_LI_EFD)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | } |
| 1034 | #endif |
| 1035 | xfs_trans_unreserve_and_mod_sb(tp); |
Christoph Hellwig | 7d09525 | 2009-06-08 15:33:32 +0200 | [diff] [blame] | 1036 | xfs_trans_unreserve_and_mod_dquots(tp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | |
Dave Chinner | ba18781 | 2018-05-09 07:47:57 -0700 | [diff] [blame] | 1038 | if (tp->t_ticket) { |
Christoph Hellwig | f78c390 | 2015-06-04 13:48:20 +1000 | [diff] [blame] | 1039 | xfs_log_done(mp, tp->t_ticket, NULL, false); |
Dave Chinner | ba18781 | 2018-05-09 07:47:57 -0700 | [diff] [blame] | 1040 | tp->t_ticket = NULL; |
| 1041 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | |
| 1043 | /* mark this thread as no longer being in a transaction */ |
Michal Hocko | 9070733 | 2017-05-03 14:53:12 -0700 | [diff] [blame] | 1044 | current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | |
Christoph Hellwig | 4906e21 | 2015-06-04 13:47:56 +1000 | [diff] [blame] | 1046 | xfs_trans_free_items(tp, NULLCOMMITLSN, dirty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | xfs_trans_free(tp); |
| 1048 | } |
| 1049 | |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1050 | /* |
| 1051 | * Roll from one trans in the sequence of PERMANENT transactions to |
| 1052 | * the next: permanent transactions are only flushed out when |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1053 | * committed with xfs_trans_commit(), but we still want as soon |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1054 | * as possible to let chunks of it go to the log. So we commit the |
| 1055 | * chunk we've been working on and get a new transaction to continue. |
| 1056 | */ |
| 1057 | int |
Christoph Hellwig | 254133f | 2017-04-06 16:00:11 -0700 | [diff] [blame] | 1058 | xfs_trans_roll( |
Christoph Hellwig | 411350d | 2017-08-28 10:21:03 -0700 | [diff] [blame] | 1059 | struct xfs_trans **tpp) |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1060 | { |
Christoph Hellwig | 411350d | 2017-08-28 10:21:03 -0700 | [diff] [blame] | 1061 | struct xfs_trans *trans = *tpp; |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 1062 | struct xfs_trans_res tres; |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1063 | int error; |
| 1064 | |
Dave Chinner | ba18781 | 2018-05-09 07:47:57 -0700 | [diff] [blame] | 1065 | trace_xfs_trans_roll(trans, _RET_IP_); |
| 1066 | |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1067 | /* |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1068 | * Copy the critical parameters from one trans to the next. |
| 1069 | */ |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 1070 | tres.tr_logres = trans->t_log_res; |
| 1071 | tres.tr_logcount = trans->t_log_count; |
Christoph Hellwig | 411350d | 2017-08-28 10:21:03 -0700 | [diff] [blame] | 1072 | |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1073 | *tpp = xfs_trans_dup(trans); |
| 1074 | |
| 1075 | /* |
| 1076 | * Commit the current transaction. |
| 1077 | * If this commit failed, then it'd just unlock those items that |
| 1078 | * are not marked ihold. That also means that a filesystem shutdown |
| 1079 | * is in progress. The caller takes the responsibility to cancel |
| 1080 | * the duplicate transaction that gets returned. |
| 1081 | */ |
Christoph Hellwig | 7039331 | 2015-06-04 13:48:08 +1000 | [diff] [blame] | 1082 | error = __xfs_trans_commit(trans, true); |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1083 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 1084 | return error; |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1085 | |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1086 | /* |
Christoph Hellwig | 411350d | 2017-08-28 10:21:03 -0700 | [diff] [blame] | 1087 | * Reserve space in the log for the next transaction. |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1088 | * This also pushes items in the "AIL", the list of logged items, |
| 1089 | * out to disk if they are taking up space at the tail of the log |
| 1090 | * that we want to use. This requires that either nothing be locked |
| 1091 | * across this call, or that anything that is locked be logged in |
| 1092 | * the prior and the next transactions. |
| 1093 | */ |
Jie Liu | 3d3c8b5 | 2013-08-12 20:49:59 +1000 | [diff] [blame] | 1094 | tres.tr_logflags = XFS_TRANS_PERM_LOG_RES; |
Christoph Hellwig | 411350d | 2017-08-28 10:21:03 -0700 | [diff] [blame] | 1095 | return xfs_trans_reserve(*tpp, &tres, 0, 0); |
Niv Sardi | 322ff6b | 2008-08-13 16:05:49 +1000 | [diff] [blame] | 1096 | } |