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