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