Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 3 | * Copyright (c) 2013 Red Hat, Inc. |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 4 | * All Rights Reserved. |
| 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 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 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. |
| 14 | * |
| 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 |
| 18 | */ |
| 19 | #include "xfs.h" |
| 20 | #include "xfs_fs.h" |
Dave Chinner | 632b89e | 2013-10-29 22:11:58 +1100 | [diff] [blame] | 21 | #include "xfs_shared.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 22 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 23 | #include "xfs_log_format.h" |
| 24 | #include "xfs_trans_resv.h" |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 25 | #include "xfs_bit.h" |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 26 | #include "xfs_mount.h" |
Darrick J. Wong | 3ab78df | 2016-08-03 11:15:38 +1000 | [diff] [blame] | 27 | #include "xfs_defer.h" |
Dave Chinner | 5706278 | 2013-10-15 09:17:51 +1100 | [diff] [blame] | 28 | #include "xfs_da_format.h" |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 29 | #include "xfs_da_btree.h" |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 30 | #include "xfs_inode.h" |
| 31 | #include "xfs_alloc.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 32 | #include "xfs_trans.h" |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 33 | #include "xfs_inode_item.h" |
| 34 | #include "xfs_bmap.h" |
Dave Chinner | 6898811 | 2013-08-12 20:49:42 +1000 | [diff] [blame] | 35 | #include "xfs_bmap_util.h" |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 36 | #include "xfs_attr.h" |
| 37 | #include "xfs_attr_leaf.h" |
| 38 | #include "xfs_attr_remote.h" |
| 39 | #include "xfs_trans_space.h" |
| 40 | #include "xfs_trace.h" |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 41 | #include "xfs_cksum.h" |
| 42 | #include "xfs_buf_item.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 43 | #include "xfs_error.h" |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 44 | |
| 45 | #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */ |
| 46 | |
| 47 | /* |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 48 | * Each contiguous block has a header, so it is not just a simple attribute |
| 49 | * length to FSB conversion. |
| 50 | */ |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 51 | int |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 52 | xfs_attr3_rmt_blocks( |
| 53 | struct xfs_mount *mp, |
| 54 | int attrlen) |
| 55 | { |
Dave Chinner | 551b382 | 2013-05-21 18:02:02 +1000 | [diff] [blame] | 56 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 57 | int buflen = XFS_ATTR3_RMT_BUF_SPACE(mp, mp->m_sb.sb_blocksize); |
| 58 | return (attrlen + buflen - 1) / buflen; |
| 59 | } |
| 60 | return XFS_B_TO_FSB(mp, attrlen); |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 61 | } |
| 62 | |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 63 | /* |
| 64 | * Checking of the remote attribute header is split into two parts. The verifier |
| 65 | * does CRC, location and bounds checking, the unpacking function checks the |
| 66 | * attribute parameters and owner. |
| 67 | */ |
| 68 | static bool |
| 69 | xfs_attr3_rmt_hdr_ok( |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 70 | void *ptr, |
| 71 | xfs_ino_t ino, |
| 72 | uint32_t offset, |
| 73 | uint32_t size, |
| 74 | xfs_daddr_t bno) |
| 75 | { |
| 76 | struct xfs_attr3_rmt_hdr *rmt = ptr; |
| 77 | |
| 78 | if (bno != be64_to_cpu(rmt->rm_blkno)) |
| 79 | return false; |
| 80 | if (offset != be32_to_cpu(rmt->rm_offset)) |
| 81 | return false; |
| 82 | if (size != be32_to_cpu(rmt->rm_bytes)) |
| 83 | return false; |
| 84 | if (ino != be64_to_cpu(rmt->rm_owner)) |
| 85 | return false; |
| 86 | |
| 87 | /* ok */ |
| 88 | return true; |
| 89 | } |
| 90 | |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 91 | static bool |
| 92 | xfs_attr3_rmt_verify( |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 93 | struct xfs_mount *mp, |
| 94 | void *ptr, |
| 95 | int fsbsize, |
| 96 | xfs_daddr_t bno) |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 97 | { |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 98 | struct xfs_attr3_rmt_hdr *rmt = ptr; |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 99 | |
| 100 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 101 | return false; |
| 102 | if (rmt->rm_magic != cpu_to_be32(XFS_ATTR3_RMT_MAGIC)) |
| 103 | return false; |
Eric Sandeen | ce748ea | 2015-07-29 11:53:31 +1000 | [diff] [blame] | 104 | if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid)) |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 105 | return false; |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 106 | if (be64_to_cpu(rmt->rm_blkno) != bno) |
| 107 | return false; |
| 108 | if (be32_to_cpu(rmt->rm_bytes) > fsbsize - sizeof(*rmt)) |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 109 | return false; |
| 110 | if (be32_to_cpu(rmt->rm_offset) + |
Jan Tulak | 51fcbfe | 2015-10-12 16:03:59 +1100 | [diff] [blame] | 111 | be32_to_cpu(rmt->rm_bytes) > XFS_XATTR_SIZE_MAX) |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 112 | return false; |
| 113 | if (rmt->rm_owner == 0) |
| 114 | return false; |
| 115 | |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | static void |
| 120 | xfs_attr3_rmt_read_verify( |
| 121 | struct xfs_buf *bp) |
| 122 | { |
| 123 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 124 | char *ptr; |
| 125 | int len; |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 126 | xfs_daddr_t bno; |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 127 | int blksize = mp->m_attr_geo->blksize; |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 128 | |
| 129 | /* no verification of non-crc buffers */ |
| 130 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 131 | return; |
| 132 | |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 133 | ptr = bp->b_addr; |
| 134 | bno = bp->b_bn; |
| 135 | len = BBTOB(bp->b_length); |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 136 | ASSERT(len >= blksize); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 137 | |
| 138 | while (len > 0) { |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 139 | if (!xfs_verify_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 140 | xfs_buf_ioerror(bp, -EFSBADCRC); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 141 | break; |
| 142 | } |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 143 | if (!xfs_attr3_rmt_verify(mp, ptr, blksize, bno)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 144 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 145 | break; |
| 146 | } |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 147 | len -= blksize; |
| 148 | ptr += blksize; |
| 149 | bno += BTOBB(blksize); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 150 | } |
| 151 | |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 152 | if (bp->b_error) |
| 153 | xfs_verifier_error(bp); |
| 154 | else |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 155 | ASSERT(len == 0); |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void |
| 159 | xfs_attr3_rmt_write_verify( |
| 160 | struct xfs_buf *bp) |
| 161 | { |
| 162 | struct xfs_mount *mp = bp->b_target->bt_mount; |
Dave Chinner | e3c32ee | 2015-07-29 11:48:01 +1000 | [diff] [blame] | 163 | int blksize = mp->m_attr_geo->blksize; |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 164 | char *ptr; |
| 165 | int len; |
| 166 | xfs_daddr_t bno; |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 167 | |
| 168 | /* no verification of non-crc buffers */ |
| 169 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 170 | return; |
| 171 | |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 172 | ptr = bp->b_addr; |
| 173 | bno = bp->b_bn; |
| 174 | len = BBTOB(bp->b_length); |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 175 | ASSERT(len >= blksize); |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 176 | |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 177 | while (len > 0) { |
Dave Chinner | e3c32ee | 2015-07-29 11:48:01 +1000 | [diff] [blame] | 178 | struct xfs_attr3_rmt_hdr *rmt = (struct xfs_attr3_rmt_hdr *)ptr; |
| 179 | |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 180 | if (!xfs_attr3_rmt_verify(mp, ptr, blksize, bno)) { |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 181 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
Eric Sandeen | ce5028c | 2014-02-27 15:23:10 +1100 | [diff] [blame] | 182 | xfs_verifier_error(bp); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 183 | return; |
| 184 | } |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 185 | |
Dave Chinner | e3c32ee | 2015-07-29 11:48:01 +1000 | [diff] [blame] | 186 | /* |
| 187 | * Ensure we aren't writing bogus LSNs to disk. See |
| 188 | * xfs_attr3_rmt_hdr_set() for the explanation. |
| 189 | */ |
| 190 | if (rmt->rm_lsn != cpu_to_be64(NULLCOMMITLSN)) { |
| 191 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
| 192 | xfs_verifier_error(bp); |
| 193 | return; |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 194 | } |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 195 | xfs_update_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 196 | |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 197 | len -= blksize; |
| 198 | ptr += blksize; |
| 199 | bno += BTOBB(blksize); |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 200 | } |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 201 | ASSERT(len == 0); |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = { |
Eric Sandeen | 233135b | 2016-01-04 16:10:19 +1100 | [diff] [blame] | 205 | .name = "xfs_attr3_rmt", |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 206 | .verify_read = xfs_attr3_rmt_read_verify, |
| 207 | .verify_write = xfs_attr3_rmt_write_verify, |
| 208 | }; |
| 209 | |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 210 | STATIC int |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 211 | xfs_attr3_rmt_hdr_set( |
| 212 | struct xfs_mount *mp, |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 213 | void *ptr, |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 214 | xfs_ino_t ino, |
| 215 | uint32_t offset, |
| 216 | uint32_t size, |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 217 | xfs_daddr_t bno) |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 218 | { |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 219 | struct xfs_attr3_rmt_hdr *rmt = ptr; |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 220 | |
| 221 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 222 | return 0; |
| 223 | |
| 224 | rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC); |
| 225 | rmt->rm_offset = cpu_to_be32(offset); |
| 226 | rmt->rm_bytes = cpu_to_be32(size); |
Eric Sandeen | ce748ea | 2015-07-29 11:53:31 +1000 | [diff] [blame] | 227 | uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid); |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 228 | rmt->rm_owner = cpu_to_be64(ino); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 229 | rmt->rm_blkno = cpu_to_be64(bno); |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 230 | |
Dave Chinner | e3c32ee | 2015-07-29 11:48:01 +1000 | [diff] [blame] | 231 | /* |
| 232 | * Remote attribute blocks are written synchronously, so we don't |
| 233 | * have an LSN that we can stamp in them that makes any sense to log |
| 234 | * recovery. To ensure that log recovery handles overwrites of these |
| 235 | * blocks sanely (i.e. once they've been freed and reallocated as some |
| 236 | * other type of metadata) we need to ensure that the LSN has a value |
| 237 | * that tells log recovery to ignore the LSN and overwrite the buffer |
| 238 | * with whatever is in it's log. To do this, we use the magic |
| 239 | * NULLCOMMITLSN to indicate that the LSN is invalid. |
| 240 | */ |
| 241 | rmt->rm_lsn = cpu_to_be64(NULLCOMMITLSN); |
| 242 | |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 243 | return sizeof(struct xfs_attr3_rmt_hdr); |
| 244 | } |
| 245 | |
| 246 | /* |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 247 | * Helper functions to copy attribute data in and out of the one disk extents |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 248 | */ |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 249 | STATIC int |
| 250 | xfs_attr_rmtval_copyout( |
| 251 | struct xfs_mount *mp, |
| 252 | struct xfs_buf *bp, |
| 253 | xfs_ino_t ino, |
| 254 | int *offset, |
| 255 | int *valuelen, |
Dave Chinner | 836a94a | 2013-08-12 20:49:44 +1000 | [diff] [blame] | 256 | __uint8_t **dst) |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 257 | { |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 258 | char *src = bp->b_addr; |
| 259 | xfs_daddr_t bno = bp->b_bn; |
| 260 | int len = BBTOB(bp->b_length); |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 261 | int blksize = mp->m_attr_geo->blksize; |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 262 | |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 263 | ASSERT(len >= blksize); |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 264 | |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 265 | while (len > 0 && *valuelen > 0) { |
| 266 | int hdr_size = 0; |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 267 | int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 268 | |
Dave Chinner | c5c249b | 2013-08-12 20:49:43 +1000 | [diff] [blame] | 269 | byte_cnt = min(*valuelen, byte_cnt); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 270 | |
| 271 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
Eric Sandeen | 6d0081a | 2014-04-14 18:58:29 +1000 | [diff] [blame] | 272 | if (!xfs_attr3_rmt_hdr_ok(src, ino, *offset, |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 273 | byte_cnt, bno)) { |
| 274 | xfs_alert(mp, |
| 275 | "remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)", |
| 276 | bno, *offset, byte_cnt, ino); |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 277 | return -EFSCORRUPTED; |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 278 | } |
| 279 | hdr_size = sizeof(struct xfs_attr3_rmt_hdr); |
| 280 | } |
| 281 | |
| 282 | memcpy(*dst, src + hdr_size, byte_cnt); |
| 283 | |
| 284 | /* roll buffer forwards */ |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 285 | len -= blksize; |
| 286 | src += blksize; |
| 287 | bno += BTOBB(blksize); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 288 | |
| 289 | /* roll attribute data forwards */ |
| 290 | *valuelen -= byte_cnt; |
| 291 | *dst += byte_cnt; |
| 292 | *offset += byte_cnt; |
| 293 | } |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | STATIC void |
| 298 | xfs_attr_rmtval_copyin( |
| 299 | struct xfs_mount *mp, |
| 300 | struct xfs_buf *bp, |
| 301 | xfs_ino_t ino, |
| 302 | int *offset, |
| 303 | int *valuelen, |
Dave Chinner | 836a94a | 2013-08-12 20:49:44 +1000 | [diff] [blame] | 304 | __uint8_t **src) |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 305 | { |
| 306 | char *dst = bp->b_addr; |
| 307 | xfs_daddr_t bno = bp->b_bn; |
| 308 | int len = BBTOB(bp->b_length); |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 309 | int blksize = mp->m_attr_geo->blksize; |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 310 | |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 311 | ASSERT(len >= blksize); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 312 | |
| 313 | while (len > 0 && *valuelen > 0) { |
| 314 | int hdr_size; |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 315 | int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 316 | |
| 317 | byte_cnt = min(*valuelen, byte_cnt); |
| 318 | hdr_size = xfs_attr3_rmt_hdr_set(mp, dst, ino, *offset, |
| 319 | byte_cnt, bno); |
| 320 | |
| 321 | memcpy(dst + hdr_size, *src, byte_cnt); |
| 322 | |
| 323 | /* |
| 324 | * If this is the last block, zero the remainder of it. |
| 325 | * Check that we are actually the last block, too. |
| 326 | */ |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 327 | if (byte_cnt + hdr_size < blksize) { |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 328 | ASSERT(*valuelen - byte_cnt == 0); |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 329 | ASSERT(len == blksize); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 330 | memset(dst + hdr_size + byte_cnt, 0, |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 331 | blksize - hdr_size - byte_cnt); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /* roll buffer forwards */ |
Dave Chinner | c2c4c47 | 2014-06-06 15:21:45 +1000 | [diff] [blame] | 335 | len -= blksize; |
| 336 | dst += blksize; |
| 337 | bno += BTOBB(blksize); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 338 | |
| 339 | /* roll attribute data forwards */ |
| 340 | *valuelen -= byte_cnt; |
| 341 | *src += byte_cnt; |
| 342 | *offset += byte_cnt; |
| 343 | } |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /* |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 347 | * Read the value associated with an attribute from the out-of-line buffer |
| 348 | * that we stored it in. |
| 349 | */ |
| 350 | int |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 351 | xfs_attr_rmtval_get( |
| 352 | struct xfs_da_args *args) |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 353 | { |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 354 | struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE]; |
| 355 | struct xfs_mount *mp = args->dp->i_mount; |
| 356 | struct xfs_buf *bp; |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 357 | xfs_dablk_t lblkno = args->rmtblkno; |
Dave Chinner | 836a94a | 2013-08-12 20:49:44 +1000 | [diff] [blame] | 358 | __uint8_t *dst = args->value; |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 359 | int valuelen; |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 360 | int nmap; |
| 361 | int error; |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 362 | int blkcnt = args->rmtblkcnt; |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 363 | int i; |
| 364 | int offset = 0; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 365 | |
| 366 | trace_xfs_attr_rmtval_get(args); |
| 367 | |
| 368 | ASSERT(!(args->flags & ATTR_KERNOVAL)); |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 369 | ASSERT(args->rmtvaluelen == args->valuelen); |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 370 | |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 371 | valuelen = args->rmtvaluelen; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 372 | while (valuelen > 0) { |
| 373 | nmap = ATTR_RMTVALUE_MAPSIZE; |
| 374 | error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno, |
Dave Chinner | 551b382 | 2013-05-21 18:02:02 +1000 | [diff] [blame] | 375 | blkcnt, map, &nmap, |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 376 | XFS_BMAPI_ATTRFORK); |
| 377 | if (error) |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 378 | return error; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 379 | ASSERT(nmap >= 1); |
| 380 | |
| 381 | for (i = 0; (i < nmap) && (valuelen > 0); i++) { |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 382 | xfs_daddr_t dblkno; |
| 383 | int dblkcnt; |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 384 | |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 385 | ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) && |
| 386 | (map[i].br_startblock != HOLESTARTBLOCK)); |
| 387 | dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 388 | dblkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount); |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 389 | error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 390 | dblkno, dblkcnt, 0, &bp, |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 391 | &xfs_attr3_rmt_buf_ops); |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 392 | if (error) |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 393 | return error; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 394 | |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 395 | error = xfs_attr_rmtval_copyout(mp, bp, args->dp->i_ino, |
| 396 | &offset, &valuelen, |
| 397 | &dst); |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 398 | xfs_buf_relse(bp); |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 399 | if (error) |
| 400 | return error; |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 401 | |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 402 | /* roll attribute extent map forwards */ |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 403 | lblkno += map[i].br_blockcount; |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 404 | blkcnt -= map[i].br_blockcount; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | ASSERT(valuelen == 0); |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 408 | return 0; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Write the value associated with an attribute into the out-of-line buffer |
| 413 | * that we have defined for it. |
| 414 | */ |
| 415 | int |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 416 | xfs_attr_rmtval_set( |
| 417 | struct xfs_da_args *args) |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 418 | { |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 419 | struct xfs_inode *dp = args->dp; |
| 420 | struct xfs_mount *mp = dp->i_mount; |
| 421 | struct xfs_bmbt_irec map; |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 422 | xfs_dablk_t lblkno; |
| 423 | xfs_fileoff_t lfileoff = 0; |
Dave Chinner | 836a94a | 2013-08-12 20:49:44 +1000 | [diff] [blame] | 424 | __uint8_t *src = args->value; |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 425 | int blkcnt; |
| 426 | int valuelen; |
| 427 | int nmap; |
| 428 | int error; |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 429 | int offset = 0; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 430 | |
| 431 | trace_xfs_attr_rmtval_set(args); |
| 432 | |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 433 | /* |
| 434 | * Find a "hole" in the attribute address space large enough for |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 435 | * us to drop the new attribute's value into. Because CRC enable |
| 436 | * attributes have headers, we can't just do a straight byte to FSB |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 437 | * conversion and have to take the header space into account. |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 438 | */ |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 439 | blkcnt = xfs_attr3_rmt_blocks(mp, args->rmtvaluelen); |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 440 | error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff, |
| 441 | XFS_ATTR_FORK); |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 442 | if (error) |
| 443 | return error; |
| 444 | |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 445 | args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff; |
| 446 | args->rmtblkcnt = blkcnt; |
| 447 | |
| 448 | /* |
| 449 | * Roll through the "value", allocating blocks on disk as required. |
| 450 | */ |
| 451 | while (blkcnt > 0) { |
| 452 | /* |
| 453 | * Allocate a single extent, up to the size of the value. |
Dave Chinner | df150ed1 | 2015-07-29 11:48:02 +1000 | [diff] [blame] | 454 | * |
| 455 | * Note that we have to consider this a data allocation as we |
| 456 | * write the remote attribute without logging the contents. |
| 457 | * Hence we must ensure that we aren't using blocks that are on |
| 458 | * the busy list so that we don't overwrite blocks which have |
| 459 | * recently been freed but their transactions are not yet |
| 460 | * committed to disk. If we overwrite the contents of a busy |
| 461 | * extent and then crash then the block may not contain the |
| 462 | * correct metadata after log recovery occurs. |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 463 | */ |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 464 | xfs_defer_init(args->dfops, args->firstblock); |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 465 | nmap = 1; |
| 466 | error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno, |
Dave Chinner | df150ed1 | 2015-07-29 11:48:02 +1000 | [diff] [blame] | 467 | blkcnt, XFS_BMAPI_ATTRFORK, args->firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 468 | args->total, &map, &nmap, args->dfops); |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 469 | if (!error) |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 470 | error = xfs_defer_finish(&args->trans, args->dfops, dp); |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 471 | if (error) { |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 472 | args->trans = NULL; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 473 | xfs_defer_cancel(args->dfops); |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 474 | return error; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 475 | } |
| 476 | |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 477 | ASSERT(nmap == 1); |
| 478 | ASSERT((map.br_startblock != DELAYSTARTBLOCK) && |
| 479 | (map.br_startblock != HOLESTARTBLOCK)); |
| 480 | lblkno += map.br_blockcount; |
| 481 | blkcnt -= map.br_blockcount; |
| 482 | |
| 483 | /* |
| 484 | * Start the next trans in the chain. |
| 485 | */ |
| 486 | error = xfs_trans_roll(&args->trans, dp); |
| 487 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 488 | return error; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /* |
| 492 | * Roll through the "value", copying the attribute value to the |
| 493 | * already-allocated blocks. Blocks are written synchronously |
| 494 | * so that we can know they are all on disk before we turn off |
| 495 | * the INCOMPLETE flag. |
| 496 | */ |
| 497 | lblkno = args->rmtblkno; |
Dave Chinner | 26f7144 | 2013-05-21 18:02:03 +1000 | [diff] [blame] | 498 | blkcnt = args->rmtblkcnt; |
Dave Chinner | 8275cdd | 2014-05-06 07:37:31 +1000 | [diff] [blame] | 499 | valuelen = args->rmtvaluelen; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 500 | while (valuelen > 0) { |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 501 | struct xfs_buf *bp; |
| 502 | xfs_daddr_t dblkno; |
| 503 | int dblkcnt; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 504 | |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 505 | ASSERT(blkcnt > 0); |
| 506 | |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 507 | xfs_defer_init(args->dfops, args->firstblock); |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 508 | nmap = 1; |
| 509 | error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno, |
Dave Chinner | 26f7144 | 2013-05-21 18:02:03 +1000 | [diff] [blame] | 510 | blkcnt, &map, &nmap, |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 511 | XFS_BMAPI_ATTRFORK); |
| 512 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 513 | return error; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 514 | ASSERT(nmap == 1); |
| 515 | ASSERT((map.br_startblock != DELAYSTARTBLOCK) && |
| 516 | (map.br_startblock != HOLESTARTBLOCK)); |
| 517 | |
| 518 | dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock), |
Dave Chinner | 26f7144 | 2013-05-21 18:02:03 +1000 | [diff] [blame] | 519 | dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount); |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 520 | |
Dave Chinner | 26f7144 | 2013-05-21 18:02:03 +1000 | [diff] [blame] | 521 | bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0); |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 522 | if (!bp) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 523 | return -ENOMEM; |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 524 | bp->b_ops = &xfs_attr3_rmt_buf_ops; |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 525 | |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 526 | xfs_attr_rmtval_copyin(mp, bp, args->dp->i_ino, &offset, |
| 527 | &valuelen, &src); |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 528 | |
| 529 | error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */ |
| 530 | xfs_buf_relse(bp); |
| 531 | if (error) |
| 532 | return error; |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 533 | |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 534 | |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 535 | /* roll attribute extent map forwards */ |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 536 | lblkno += map.br_blockcount; |
Dave Chinner | 26f7144 | 2013-05-21 18:02:03 +1000 | [diff] [blame] | 537 | blkcnt -= map.br_blockcount; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 538 | } |
| 539 | ASSERT(valuelen == 0); |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 540 | return 0; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | /* |
| 544 | * Remove the value associated with an attribute by deleting the |
| 545 | * out-of-line buffer that it is stored on. |
| 546 | */ |
| 547 | int |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 548 | xfs_attr_rmtval_remove( |
| 549 | struct xfs_da_args *args) |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 550 | { |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 551 | struct xfs_mount *mp = args->dp->i_mount; |
| 552 | xfs_dablk_t lblkno; |
| 553 | int blkcnt; |
| 554 | int error; |
| 555 | int done; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 556 | |
| 557 | trace_xfs_attr_rmtval_remove(args); |
| 558 | |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 559 | /* |
Dave Chinner | 58a7228 | 2013-05-21 18:02:04 +1000 | [diff] [blame] | 560 | * Roll through the "value", invalidating the attribute value's blocks. |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 561 | */ |
| 562 | lblkno = args->rmtblkno; |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 563 | blkcnt = args->rmtblkcnt; |
| 564 | while (blkcnt > 0) { |
| 565 | struct xfs_bmbt_irec map; |
| 566 | struct xfs_buf *bp; |
| 567 | xfs_daddr_t dblkno; |
| 568 | int dblkcnt; |
| 569 | int nmap; |
Dave Chinner | 58a7228 | 2013-05-21 18:02:04 +1000 | [diff] [blame] | 570 | |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 571 | /* |
| 572 | * Try to remember where we decided to put the value. |
| 573 | */ |
| 574 | nmap = 1; |
| 575 | error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno, |
Dave Chinner | 58a7228 | 2013-05-21 18:02:04 +1000 | [diff] [blame] | 576 | blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK); |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 577 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 578 | return error; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 579 | ASSERT(nmap == 1); |
| 580 | ASSERT((map.br_startblock != DELAYSTARTBLOCK) && |
| 581 | (map.br_startblock != HOLESTARTBLOCK)); |
| 582 | |
| 583 | dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock), |
Dave Chinner | 58a7228 | 2013-05-21 18:02:04 +1000 | [diff] [blame] | 584 | dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount); |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 585 | |
| 586 | /* |
| 587 | * If the "remote" value is in the cache, remove it. |
| 588 | */ |
Dave Chinner | 58a7228 | 2013-05-21 18:02:04 +1000 | [diff] [blame] | 589 | bp = xfs_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK); |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 590 | if (bp) { |
| 591 | xfs_buf_stale(bp); |
| 592 | xfs_buf_relse(bp); |
| 593 | bp = NULL; |
| 594 | } |
| 595 | |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 596 | lblkno += map.br_blockcount; |
Dave Chinner | 58a7228 | 2013-05-21 18:02:04 +1000 | [diff] [blame] | 597 | blkcnt -= map.br_blockcount; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /* |
| 601 | * Keep de-allocating extents until the remote-value region is gone. |
| 602 | */ |
| 603 | lblkno = args->rmtblkno; |
Dave Chinner | 7bc0dc2 | 2013-05-21 18:02:08 +1000 | [diff] [blame] | 604 | blkcnt = args->rmtblkcnt; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 605 | done = 0; |
| 606 | while (!done) { |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 607 | xfs_defer_init(args->dfops, args->firstblock); |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 608 | error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt, |
Dave Chinner | ab7bb61 | 2015-07-29 11:51:01 +1000 | [diff] [blame] | 609 | XFS_BMAPI_ATTRFORK, 1, args->firstblock, |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 610 | args->dfops, &done); |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 611 | if (!error) |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 612 | error = xfs_defer_finish(&args->trans, args->dfops, |
Eric Sandeen | f6106ef | 2016-01-11 11:34:01 +1100 | [diff] [blame] | 613 | args->dp); |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 614 | if (error) { |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 615 | args->trans = NULL; |
Darrick J. Wong | 2c3234d | 2016-08-03 11:19:29 +1000 | [diff] [blame] | 616 | xfs_defer_cancel(args->dfops); |
Dave Chinner | d2e448d | 2013-04-03 16:11:28 +1100 | [diff] [blame] | 617 | return error; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 621 | * Close out trans and start the next one in the chain. |
| 622 | */ |
| 623 | error = xfs_trans_roll(&args->trans, args->dp); |
| 624 | if (error) |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 625 | return error; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 626 | } |
Eric Sandeen | d99831f | 2014-06-22 15:03:54 +1000 | [diff] [blame] | 627 | return 0; |
Dave Chinner | 95920cd | 2013-04-03 16:11:27 +1100 | [diff] [blame] | 628 | } |