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