Darrick J. Wong | bdf2863 | 2016-10-03 09:11:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 Oracle. All Rights Reserved. |
| 3 | * |
| 4 | * Author: Darrick J. Wong <darrick.wong@oracle.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it would be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write the Free Software Foundation, |
| 18 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | */ |
| 20 | #include "xfs.h" |
| 21 | #include "xfs_fs.h" |
| 22 | #include "xfs_shared.h" |
| 23 | #include "xfs_format.h" |
| 24 | #include "xfs_log_format.h" |
| 25 | #include "xfs_trans_resv.h" |
| 26 | #include "xfs_sb.h" |
| 27 | #include "xfs_mount.h" |
| 28 | #include "xfs_defer.h" |
| 29 | #include "xfs_btree.h" |
| 30 | #include "xfs_bmap.h" |
| 31 | #include "xfs_refcount_btree.h" |
| 32 | #include "xfs_alloc.h" |
| 33 | #include "xfs_error.h" |
| 34 | #include "xfs_trace.h" |
| 35 | #include "xfs_cksum.h" |
| 36 | #include "xfs_trans.h" |
| 37 | #include "xfs_bit.h" |
| 38 | #include "xfs_refcount.h" |
| 39 | |
| 40 | /* |
| 41 | * Look up the first record less than or equal to [bno, len] in the btree |
| 42 | * given by cur. |
| 43 | */ |
| 44 | int |
| 45 | xfs_refcount_lookup_le( |
| 46 | struct xfs_btree_cur *cur, |
| 47 | xfs_agblock_t bno, |
| 48 | int *stat) |
| 49 | { |
| 50 | trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_private.a.agno, bno, |
| 51 | XFS_LOOKUP_LE); |
| 52 | cur->bc_rec.rc.rc_startblock = bno; |
| 53 | cur->bc_rec.rc.rc_blockcount = 0; |
| 54 | return xfs_btree_lookup(cur, XFS_LOOKUP_LE, stat); |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Look up the first record greater than or equal to [bno, len] in the btree |
| 59 | * given by cur. |
| 60 | */ |
| 61 | int |
| 62 | xfs_refcount_lookup_ge( |
| 63 | struct xfs_btree_cur *cur, |
| 64 | xfs_agblock_t bno, |
| 65 | int *stat) |
| 66 | { |
| 67 | trace_xfs_refcount_lookup(cur->bc_mp, cur->bc_private.a.agno, bno, |
| 68 | XFS_LOOKUP_GE); |
| 69 | cur->bc_rec.rc.rc_startblock = bno; |
| 70 | cur->bc_rec.rc.rc_blockcount = 0; |
| 71 | return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat); |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Get the data from the pointed-to record. |
| 76 | */ |
| 77 | int |
| 78 | xfs_refcount_get_rec( |
| 79 | struct xfs_btree_cur *cur, |
| 80 | struct xfs_refcount_irec *irec, |
| 81 | int *stat) |
| 82 | { |
| 83 | union xfs_btree_rec *rec; |
| 84 | int error; |
| 85 | |
| 86 | error = xfs_btree_get_rec(cur, &rec, stat); |
| 87 | if (!error && *stat == 1) { |
| 88 | irec->rc_startblock = be32_to_cpu(rec->refc.rc_startblock); |
| 89 | irec->rc_blockcount = be32_to_cpu(rec->refc.rc_blockcount); |
| 90 | irec->rc_refcount = be32_to_cpu(rec->refc.rc_refcount); |
| 91 | trace_xfs_refcount_get(cur->bc_mp, cur->bc_private.a.agno, |
| 92 | irec); |
| 93 | } |
| 94 | return error; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Update the record referred to by cur to the value given |
| 99 | * by [bno, len, refcount]. |
| 100 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 101 | */ |
| 102 | STATIC int |
| 103 | xfs_refcount_update( |
| 104 | struct xfs_btree_cur *cur, |
| 105 | struct xfs_refcount_irec *irec) |
| 106 | { |
| 107 | union xfs_btree_rec rec; |
| 108 | int error; |
| 109 | |
| 110 | trace_xfs_refcount_update(cur->bc_mp, cur->bc_private.a.agno, irec); |
| 111 | rec.refc.rc_startblock = cpu_to_be32(irec->rc_startblock); |
| 112 | rec.refc.rc_blockcount = cpu_to_be32(irec->rc_blockcount); |
| 113 | rec.refc.rc_refcount = cpu_to_be32(irec->rc_refcount); |
| 114 | error = xfs_btree_update(cur, &rec); |
| 115 | if (error) |
| 116 | trace_xfs_refcount_update_error(cur->bc_mp, |
| 117 | cur->bc_private.a.agno, error, _RET_IP_); |
| 118 | return error; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Insert the record referred to by cur to the value given |
| 123 | * by [bno, len, refcount]. |
| 124 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 125 | */ |
| 126 | STATIC int |
| 127 | xfs_refcount_insert( |
| 128 | struct xfs_btree_cur *cur, |
| 129 | struct xfs_refcount_irec *irec, |
| 130 | int *i) |
| 131 | { |
| 132 | int error; |
| 133 | |
| 134 | trace_xfs_refcount_insert(cur->bc_mp, cur->bc_private.a.agno, irec); |
| 135 | cur->bc_rec.rc.rc_startblock = irec->rc_startblock; |
| 136 | cur->bc_rec.rc.rc_blockcount = irec->rc_blockcount; |
| 137 | cur->bc_rec.rc.rc_refcount = irec->rc_refcount; |
| 138 | error = xfs_btree_insert(cur, i); |
| 139 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, *i == 1, out_error); |
| 140 | out_error: |
| 141 | if (error) |
| 142 | trace_xfs_refcount_insert_error(cur->bc_mp, |
| 143 | cur->bc_private.a.agno, error, _RET_IP_); |
| 144 | return error; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Remove the record referred to by cur, then set the pointer to the spot |
| 149 | * where the record could be re-inserted, in case we want to increment or |
| 150 | * decrement the cursor. |
| 151 | * This either works (return 0) or gets an EFSCORRUPTED error. |
| 152 | */ |
| 153 | STATIC int |
| 154 | xfs_refcount_delete( |
| 155 | struct xfs_btree_cur *cur, |
| 156 | int *i) |
| 157 | { |
| 158 | struct xfs_refcount_irec irec; |
| 159 | int found_rec; |
| 160 | int error; |
| 161 | |
| 162 | error = xfs_refcount_get_rec(cur, &irec, &found_rec); |
| 163 | if (error) |
| 164 | goto out_error; |
| 165 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, found_rec == 1, out_error); |
| 166 | trace_xfs_refcount_delete(cur->bc_mp, cur->bc_private.a.agno, &irec); |
| 167 | error = xfs_btree_delete(cur, i); |
| 168 | XFS_WANT_CORRUPTED_GOTO(cur->bc_mp, *i == 1, out_error); |
| 169 | if (error) |
| 170 | goto out_error; |
| 171 | error = xfs_refcount_lookup_ge(cur, irec.rc_startblock, &found_rec); |
| 172 | out_error: |
| 173 | if (error) |
| 174 | trace_xfs_refcount_delete_error(cur->bc_mp, |
| 175 | cur->bc_private.a.agno, error, _RET_IP_); |
| 176 | return error; |
| 177 | } |