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