blob: 83a6d3c7f872db5e7cc18472937629d37a655927 [file] [log] [blame]
Dave Chinner95920cd2013-04-03 16:11:27 +11001/*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
Dave Chinnerd2e448d2013-04-03 16:11:28 +11003 * Copyright (c) 2013 Red Hat, Inc.
Dave Chinner95920cd2013-04-03 16:11:27 +11004 * 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 Chinner632b89e2013-10-29 22:11:58 +110021#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110022#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110023#include "xfs_log_format.h"
24#include "xfs_trans_resv.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110025#include "xfs_bit.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110026#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.h"
Dave Chinner57062782013-10-15 09:17:51 +110028#include "xfs_da_format.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110029#include "xfs_da_btree.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110030#include "xfs_inode.h"
31#include "xfs_alloc.h"
Dave Chinner239880e2013-10-23 10:50:10 +110032#include "xfs_trans.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110033#include "xfs_inode_item.h"
34#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100035#include "xfs_bmap_util.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110036#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 Chinnerd2e448d2013-04-03 16:11:28 +110041#include "xfs_cksum.h"
42#include "xfs_buf_item.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110043#include "xfs_error.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110044
45#define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
46
47/*
Dave Chinnerd2e448d2013-04-03 16:11:28 +110048 * Each contiguous block has a header, so it is not just a simple attribute
49 * length to FSB conversion.
50 */
Dave Chinner7bc0dc22013-05-21 18:02:08 +100051int
Dave Chinnerd2e448d2013-04-03 16:11:28 +110052xfs_attr3_rmt_blocks(
53 struct xfs_mount *mp,
54 int attrlen)
55{
Dave Chinner551b3822013-05-21 18:02:02 +100056 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 Chinnerd2e448d2013-04-03 16:11:28 +110061}
62
Dave Chinner7bc0dc22013-05-21 18:02:08 +100063/*
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. Wonga6a781a2018-01-08 10:51:03 -080068static xfs_failaddr_t
Dave Chinner7bc0dc22013-05-21 18:02:08 +100069xfs_attr3_rmt_hdr_ok(
Dave Chinner7bc0dc22013-05-21 18:02:08 +100070 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. Wonga6a781a2018-01-08 10:51:03 -080079 return __this_address;
Dave Chinner7bc0dc22013-05-21 18:02:08 +100080 if (offset != be32_to_cpu(rmt->rm_offset))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080081 return __this_address;
Dave Chinner7bc0dc22013-05-21 18:02:08 +100082 if (size != be32_to_cpu(rmt->rm_bytes))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080083 return __this_address;
Dave Chinner7bc0dc22013-05-21 18:02:08 +100084 if (ino != be64_to_cpu(rmt->rm_owner))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080085 return __this_address;
Dave Chinner7bc0dc22013-05-21 18:02:08 +100086
87 /* ok */
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080088 return NULL;
Dave Chinner7bc0dc22013-05-21 18:02:08 +100089}
90
Darrick J. Wonga6a781a2018-01-08 10:51:03 -080091static xfs_failaddr_t
Dave Chinnerd2e448d2013-04-03 16:11:28 +110092xfs_attr3_rmt_verify(
Dave Chinner7bc0dc22013-05-21 18:02:08 +100093 struct xfs_mount *mp,
94 void *ptr,
95 int fsbsize,
96 xfs_daddr_t bno)
Dave Chinnerd2e448d2013-04-03 16:11:28 +110097{
Dave Chinner7bc0dc22013-05-21 18:02:08 +100098 struct xfs_attr3_rmt_hdr *rmt = ptr;
Dave Chinnerd2e448d2013-04-03 16:11:28 +110099
100 if (!xfs_sb_version_hascrc(&mp->m_sb))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800101 return __this_address;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100102 if (rmt->rm_magic != cpu_to_be32(XFS_ATTR3_RMT_MAGIC))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800103 return __this_address;
Eric Sandeence748ea2015-07-29 11:53:31 +1000104 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800105 return __this_address;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000106 if (be64_to_cpu(rmt->rm_blkno) != bno)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800107 return __this_address;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000108 if (be32_to_cpu(rmt->rm_bytes) > fsbsize - sizeof(*rmt))
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800109 return __this_address;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100110 if (be32_to_cpu(rmt->rm_offset) +
Jan Tulak51fcbfe2015-10-12 16:03:59 +1100111 be32_to_cpu(rmt->rm_bytes) > XFS_XATTR_SIZE_MAX)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800112 return __this_address;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100113 if (rmt->rm_owner == 0)
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800114 return __this_address;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100115
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800116 return NULL;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100117}
118
Darrick J. Wongb5572592018-01-08 10:51:08 -0800119static int
120__xfs_attr3_rmt_read_verify(
121 struct xfs_buf *bp,
122 bool check_crc,
123 xfs_failaddr_t *failaddr)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100124{
125 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000126 char *ptr;
127 int len;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000128 xfs_daddr_t bno;
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000129 int blksize = mp->m_attr_geo->blksize;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100130
131 /* no verification of non-crc buffers */
132 if (!xfs_sb_version_hascrc(&mp->m_sb))
Darrick J. Wongb5572592018-01-08 10:51:08 -0800133 return 0;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100134
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000135 ptr = bp->b_addr;
136 bno = bp->b_bn;
137 len = BBTOB(bp->b_length);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000138 ASSERT(len >= blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000139
140 while (len > 0) {
Darrick J. Wongb5572592018-01-08 10:51:08 -0800141 if (check_crc &&
142 !xfs_verify_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF)) {
143 *failaddr = __this_address;
144 return -EFSBADCRC;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000145 }
Darrick J. Wongb5572592018-01-08 10:51:08 -0800146 *failaddr = xfs_attr3_rmt_verify(mp, ptr, blksize, bno);
147 if (*failaddr)
148 return -EFSCORRUPTED;
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000149 len -= blksize;
150 ptr += blksize;
151 bno += BTOBB(blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000152 }
153
Darrick J. Wongb5572592018-01-08 10:51:08 -0800154 if (len != 0) {
155 *failaddr = __this_address;
156 return -EFSCORRUPTED;
157 }
158
159 return 0;
160}
161
162static void
163xfs_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
174static xfs_failaddr_t
175xfs_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 Chinnerd2e448d2013-04-03 16:11:28 +1100183}
184
185static void
186xfs_attr3_rmt_write_verify(
187 struct xfs_buf *bp)
188{
189 struct xfs_mount *mp = bp->b_target->bt_mount;
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800190 xfs_failaddr_t fa;
Dave Chinnere3c32ee2015-07-29 11:48:01 +1000191 int blksize = mp->m_attr_geo->blksize;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000192 char *ptr;
193 int len;
194 xfs_daddr_t bno;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100195
196 /* no verification of non-crc buffers */
197 if (!xfs_sb_version_hascrc(&mp->m_sb))
198 return;
199
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000200 ptr = bp->b_addr;
201 bno = bp->b_bn;
202 len = BBTOB(bp->b_length);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000203 ASSERT(len >= blksize);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100204
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000205 while (len > 0) {
Dave Chinnere3c32ee2015-07-29 11:48:01 +1000206 struct xfs_attr3_rmt_hdr *rmt = (struct xfs_attr3_rmt_hdr *)ptr;
207
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800208 fa = xfs_attr3_rmt_verify(mp, ptr, blksize, bno);
209 if (fa) {
210 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000211 return;
212 }
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000213
Dave Chinnere3c32ee2015-07-29 11:48:01 +1000214 /*
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. Wongbc1a09b2018-01-08 10:51:03 -0800219 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
Dave Chinnere3c32ee2015-07-29 11:48:01 +1000220 return;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000221 }
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000222 xfs_update_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000223
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000224 len -= blksize;
225 ptr += blksize;
226 bno += BTOBB(blksize);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100227 }
Darrick J. Wong31ca03c2018-01-08 10:51:02 -0800228
229 if (len != 0)
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800230 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100231}
232
233const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100234 .name = "xfs_attr3_rmt",
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100235 .verify_read = xfs_attr3_rmt_read_verify,
236 .verify_write = xfs_attr3_rmt_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800237 .verify_struct = xfs_attr3_rmt_verify_struct,
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100238};
239
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000240STATIC int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100241xfs_attr3_rmt_hdr_set(
242 struct xfs_mount *mp,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000243 void *ptr,
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100244 xfs_ino_t ino,
245 uint32_t offset,
246 uint32_t size,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000247 xfs_daddr_t bno)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100248{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000249 struct xfs_attr3_rmt_hdr *rmt = ptr;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100250
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 Sandeence748ea2015-07-29 11:53:31 +1000257 uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100258 rmt->rm_owner = cpu_to_be64(ino);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000259 rmt->rm_blkno = cpu_to_be64(bno);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100260
Dave Chinnere3c32ee2015-07-29 11:48:01 +1000261 /*
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 Chinnerd2e448d2013-04-03 16:11:28 +1100273 return sizeof(struct xfs_attr3_rmt_hdr);
274}
275
276/*
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000277 * Helper functions to copy attribute data in and out of the one disk extents
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100278 */
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000279STATIC int
280xfs_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. Wongc8ce5402017-06-16 11:00:05 -0700286 uint8_t **dst)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100287{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000288 char *src = bp->b_addr;
289 xfs_daddr_t bno = bp->b_bn;
290 int len = BBTOB(bp->b_length);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000291 int blksize = mp->m_attr_geo->blksize;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100292
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000293 ASSERT(len >= blksize);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100294
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000295 while (len > 0 && *valuelen > 0) {
296 int hdr_size = 0;
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000297 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000298
Dave Chinnerc5c249b2013-08-12 20:49:43 +1000299 byte_cnt = min(*valuelen, byte_cnt);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000300
301 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Darrick J. Wonga6a781a2018-01-08 10:51:03 -0800302 if (xfs_attr3_rmt_hdr_ok(src, ino, *offset,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000303 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 Chinner24513372014-06-25 14:58:08 +1000307 return -EFSCORRUPTED;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000308 }
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 Chinnerc2c4c472014-06-06 15:21:45 +1000315 len -= blksize;
316 src += blksize;
317 bno += BTOBB(blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000318
319 /* roll attribute data forwards */
320 *valuelen -= byte_cnt;
321 *dst += byte_cnt;
322 *offset += byte_cnt;
323 }
324 return 0;
325}
326
327STATIC void
328xfs_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. Wongc8ce5402017-06-16 11:00:05 -0700334 uint8_t **src)
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000335{
336 char *dst = bp->b_addr;
337 xfs_daddr_t bno = bp->b_bn;
338 int len = BBTOB(bp->b_length);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000339 int blksize = mp->m_attr_geo->blksize;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000340
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000341 ASSERT(len >= blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000342
343 while (len > 0 && *valuelen > 0) {
344 int hdr_size;
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000345 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000346
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 Chinnerc2c4c472014-06-06 15:21:45 +1000357 if (byte_cnt + hdr_size < blksize) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000358 ASSERT(*valuelen - byte_cnt == 0);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000359 ASSERT(len == blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000360 memset(dst + hdr_size + byte_cnt, 0,
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000361 blksize - hdr_size - byte_cnt);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000362 }
363
364 /* roll buffer forwards */
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000365 len -= blksize;
366 dst += blksize;
367 bno += BTOBB(blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000368
369 /* roll attribute data forwards */
370 *valuelen -= byte_cnt;
371 *src += byte_cnt;
372 *offset += byte_cnt;
373 }
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100374}
375
376/*
Dave Chinner95920cd2013-04-03 16:11:27 +1100377 * Read the value associated with an attribute from the out-of-line buffer
378 * that we stored it in.
379 */
380int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100381xfs_attr_rmtval_get(
382 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100383{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100384 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
385 struct xfs_mount *mp = args->dp->i_mount;
386 struct xfs_buf *bp;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100387 xfs_dablk_t lblkno = args->rmtblkno;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700388 uint8_t *dst = args->value;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000389 int valuelen;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100390 int nmap;
391 int error;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000392 int blkcnt = args->rmtblkcnt;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100393 int i;
394 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100395
396 trace_xfs_attr_rmtval_get(args);
397
398 ASSERT(!(args->flags & ATTR_KERNOVAL));
Dave Chinner8275cdd2014-05-06 07:37:31 +1000399 ASSERT(args->rmtvaluelen == args->valuelen);
Dave Chinner95920cd2013-04-03 16:11:27 +1100400
Dave Chinner8275cdd2014-05-06 07:37:31 +1000401 valuelen = args->rmtvaluelen;
Dave Chinner95920cd2013-04-03 16:11:27 +1100402 while (valuelen > 0) {
403 nmap = ATTR_RMTVALUE_MAPSIZE;
404 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
Dave Chinner551b3822013-05-21 18:02:02 +1000405 blkcnt, map, &nmap,
Dave Chinner95920cd2013-04-03 16:11:27 +1100406 XFS_BMAPI_ATTRFORK);
407 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100408 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100409 ASSERT(nmap >= 1);
410
411 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000412 xfs_daddr_t dblkno;
413 int dblkcnt;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100414
Dave Chinner95920cd2013-04-03 16:11:27 +1100415 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
416 (map[i].br_startblock != HOLESTARTBLOCK));
417 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000418 dblkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
Darrick J. Wongad017f62017-06-16 11:00:14 -0700419 error = xfs_trans_read_buf(mp, args->trans,
420 mp->m_ddev_targp,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000421 dblkno, dblkcnt, 0, &bp,
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100422 &xfs_attr3_rmt_buf_ops);
Dave Chinner95920cd2013-04-03 16:11:27 +1100423 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100424 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100425
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000426 error = xfs_attr_rmtval_copyout(mp, bp, args->dp->i_ino,
427 &offset, &valuelen,
428 &dst);
Darrick J. Wongad017f62017-06-16 11:00:14 -0700429 xfs_trans_brelse(args->trans, bp);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000430 if (error)
431 return error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100432
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000433 /* roll attribute extent map forwards */
Dave Chinner95920cd2013-04-03 16:11:27 +1100434 lblkno += map[i].br_blockcount;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000435 blkcnt -= map[i].br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100436 }
437 }
438 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100439 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100440}
441
442/*
443 * Write the value associated with an attribute into the out-of-line buffer
444 * that we have defined for it.
445 */
446int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100447xfs_attr_rmtval_set(
448 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100449{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100450 struct xfs_inode *dp = args->dp;
451 struct xfs_mount *mp = dp->i_mount;
452 struct xfs_bmbt_irec map;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100453 xfs_dablk_t lblkno;
454 xfs_fileoff_t lfileoff = 0;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700455 uint8_t *src = args->value;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100456 int blkcnt;
457 int valuelen;
458 int nmap;
459 int error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100460 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100461
462 trace_xfs_attr_rmtval_set(args);
463
Dave Chinner95920cd2013-04-03 16:11:27 +1100464 /*
465 * Find a "hole" in the attribute address space large enough for
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100466 * 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 Chinner7bc0dc22013-05-21 18:02:08 +1000468 * conversion and have to take the header space into account.
Dave Chinner95920cd2013-04-03 16:11:27 +1100469 */
Dave Chinner8275cdd2014-05-06 07:37:31 +1000470 blkcnt = xfs_attr3_rmt_blocks(mp, args->rmtvaluelen);
Dave Chinner95920cd2013-04-03 16:11:27 +1100471 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
472 XFS_ATTR_FORK);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100473 if (error)
474 return error;
475
Dave Chinner95920cd2013-04-03 16:11:27 +1100476 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 Chinnerdf150ed12015-07-29 11:48:02 +1000485 *
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 Chinner95920cd2013-04-03 16:11:27 +1100494 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000495 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner95920cd2013-04-03 16:11:27 +1100496 nmap = 1;
497 error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
Dave Chinnerdf150ed12015-07-29 11:48:02 +1000498 blkcnt, XFS_BMAPI_ATTRFORK, args->firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000499 args->total, &map, &nmap, args->dfops);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700500 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 Chinner95920cd2013-04-03 16:11:27 +1100506
Dave Chinner95920cd2013-04-03 16:11:27 +1100507 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 Hellwig411350d2017-08-28 10:21:03 -0700516 error = xfs_trans_roll_inode(&args->trans, dp);
Dave Chinner95920cd2013-04-03 16:11:27 +1100517 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000518 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100519 }
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 Chinner26f71442013-05-21 18:02:03 +1000528 blkcnt = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000529 valuelen = args->rmtvaluelen;
Dave Chinner95920cd2013-04-03 16:11:27 +1100530 while (valuelen > 0) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000531 struct xfs_buf *bp;
532 xfs_daddr_t dblkno;
533 int dblkcnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100534
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000535 ASSERT(blkcnt > 0);
536
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000537 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner95920cd2013-04-03 16:11:27 +1100538 nmap = 1;
539 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
Dave Chinner26f71442013-05-21 18:02:03 +1000540 blkcnt, &map, &nmap,
Dave Chinner95920cd2013-04-03 16:11:27 +1100541 XFS_BMAPI_ATTRFORK);
542 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000543 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100544 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 Chinner26f71442013-05-21 18:02:03 +1000549 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100550
Dave Chinner26f71442013-05-21 18:02:03 +1000551 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0);
Dave Chinner95920cd2013-04-03 16:11:27 +1100552 if (!bp)
Dave Chinner24513372014-06-25 14:58:08 +1000553 return -ENOMEM;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100554 bp->b_ops = &xfs_attr3_rmt_buf_ops;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100555
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000556 xfs_attr_rmtval_copyin(mp, bp, args->dp->i_ino, &offset,
557 &valuelen, &src);
Dave Chinner95920cd2013-04-03 16:11:27 +1100558
559 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
560 xfs_buf_relse(bp);
561 if (error)
562 return error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100563
Dave Chinner95920cd2013-04-03 16:11:27 +1100564
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000565 /* roll attribute extent map forwards */
Dave Chinner95920cd2013-04-03 16:11:27 +1100566 lblkno += map.br_blockcount;
Dave Chinner26f71442013-05-21 18:02:03 +1000567 blkcnt -= map.br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100568 }
569 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100570 return 0;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700571out_defer_cancel:
572 xfs_defer_cancel(args->dfops);
573 args->trans = NULL;
574 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100575}
576
577/*
578 * Remove the value associated with an attribute by deleting the
579 * out-of-line buffer that it is stored on.
580 */
581int
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000582xfs_attr_rmtval_remove(
583 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100584{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000585 struct xfs_mount *mp = args->dp->i_mount;
586 xfs_dablk_t lblkno;
587 int blkcnt;
588 int error;
589 int done;
Dave Chinner95920cd2013-04-03 16:11:27 +1100590
591 trace_xfs_attr_rmtval_remove(args);
592
Dave Chinner95920cd2013-04-03 16:11:27 +1100593 /*
Dave Chinner58a72282013-05-21 18:02:04 +1000594 * Roll through the "value", invalidating the attribute value's blocks.
Dave Chinner95920cd2013-04-03 16:11:27 +1100595 */
596 lblkno = args->rmtblkno;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000597 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 Chinner58a72282013-05-21 18:02:04 +1000604
Dave Chinner95920cd2013-04-03 16:11:27 +1100605 /*
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 Chinner58a72282013-05-21 18:02:04 +1000610 blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
Dave Chinner95920cd2013-04-03 16:11:27 +1100611 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000612 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100613 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 Chinner58a72282013-05-21 18:02:04 +1000618 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100619
620 /*
621 * If the "remote" value is in the cache, remove it.
622 */
Dave Chinner8925a3d2018-04-18 08:25:20 -0700623 bp = xfs_buf_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK);
Dave Chinner95920cd2013-04-03 16:11:27 +1100624 if (bp) {
625 xfs_buf_stale(bp);
626 xfs_buf_relse(bp);
627 bp = NULL;
628 }
629
Dave Chinner95920cd2013-04-03 16:11:27 +1100630 lblkno += map.br_blockcount;
Dave Chinner58a72282013-05-21 18:02:04 +1000631 blkcnt -= map.br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100632 }
633
634 /*
635 * Keep de-allocating extents until the remote-value region is gone.
636 */
637 lblkno = args->rmtblkno;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000638 blkcnt = args->rmtblkcnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100639 done = 0;
640 while (!done) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000641 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner95920cd2013-04-03 16:11:27 +1100642 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
Dave Chinnerab7bb612015-07-29 11:51:01 +1000643 XFS_BMAPI_ATTRFORK, 1, args->firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000644 args->dfops, &done);
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700645 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 Chinner95920cd2013-04-03 16:11:27 +1100651
652 /*
Dave Chinner95920cd2013-04-03 16:11:27 +1100653 * Close out trans and start the next one in the chain.
654 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700655 error = xfs_trans_roll_inode(&args->trans, args->dp);
Dave Chinner95920cd2013-04-03 16:11:27 +1100656 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000657 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100658 }
Eric Sandeend99831f2014-06-22 15:03:54 +1000659 return 0;
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -0700660out_defer_cancel:
661 xfs_defer_cancel(args->dfops);
662 args->trans = NULL;
663 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100664}