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