Merge tag 'xfs-6.9-merge-8' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[linux-block.git] / fs / xfs / libxfs / xfs_attr_remote.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0
95920cd6
DC
2/*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
d2e448d5 4 * Copyright (c) 2013 Red Hat, Inc.
95920cd6 5 * All Rights Reserved.
95920cd6
DC
6 */
7#include "xfs.h"
8#include "xfs_fs.h"
632b89e8 9#include "xfs_shared.h"
a4fbe6ab 10#include "xfs_format.h"
239880ef
DC
11#include "xfs_log_format.h"
12#include "xfs_trans_resv.h"
95920cd6 13#include "xfs_bit.h"
95920cd6 14#include "xfs_mount.h"
3ab78df2 15#include "xfs_defer.h"
57062787 16#include "xfs_da_format.h"
95920cd6 17#include "xfs_da_btree.h"
95920cd6 18#include "xfs_inode.h"
239880ef 19#include "xfs_trans.h"
95920cd6
DC
20#include "xfs_bmap.h"
21#include "xfs_attr.h"
5f213ddb 22#include "xfs_attr_remote.h"
95920cd6 23#include "xfs_trace.h"
a4fbe6ab 24#include "xfs_error.h"
ca14c096 25#include "xfs_health.h"
95920cd6
DC
26
27#define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
28
e8db2aaf
DW
29/*
30 * Remote Attribute Values
31 * =======================
32 *
33 * Remote extended attribute values are conceptually simple -- they're written
34 * to data blocks mapped by an inode's attribute fork, and they have an upper
35 * size limit of 64k. Setting a value does not involve the XFS log.
36 *
37 * However, on a v5 filesystem, maximally sized remote attr values require one
38 * block more than 64k worth of space to hold both the remote attribute value
39 * header (64 bytes). On a 4k block filesystem this results in a 68k buffer;
40 * on a 64k block filesystem, this would be a 128k buffer. Note that the log
41 * format can only handle a dirty buffer of XFS_MAX_BLOCKSIZE length (64k).
42 * Therefore, we /must/ ensure that remote attribute value buffers never touch
43 * the logging system and therefore never have a log item.
44 */
45
d2e448d5
DC
46/*
47 * Each contiguous block has a header, so it is not just a simple attribute
48 * length to FSB conversion.
49 */
7bc0dc27 50int
d2e448d5
DC
51xfs_attr3_rmt_blocks(
52 struct xfs_mount *mp,
53 int attrlen)
54{
38c26bfd 55 if (xfs_has_crc(mp)) {
551b382f
DC
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);
d2e448d5
DC
60}
61
7bc0dc27
DC
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 */
a6a781a5 67static xfs_failaddr_t
7bc0dc27 68xfs_attr3_rmt_hdr_ok(
7bc0dc27
DC
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))
a6a781a5 78 return __this_address;
7bc0dc27 79 if (offset != be32_to_cpu(rmt->rm_offset))
a6a781a5 80 return __this_address;
7bc0dc27 81 if (size != be32_to_cpu(rmt->rm_bytes))
a6a781a5 82 return __this_address;
7bc0dc27 83 if (ino != be64_to_cpu(rmt->rm_owner))
a6a781a5 84 return __this_address;
7bc0dc27
DC
85
86 /* ok */
a6a781a5 87 return NULL;
7bc0dc27
DC
88}
89
a6a781a5 90static xfs_failaddr_t
d2e448d5 91xfs_attr3_rmt_verify(
7bc0dc27 92 struct xfs_mount *mp,
39708c20 93 struct xfs_buf *bp,
7bc0dc27
DC
94 void *ptr,
95 int fsbsize,
96 xfs_daddr_t bno)
d2e448d5 97{
7bc0dc27 98 struct xfs_attr3_rmt_hdr *rmt = ptr;
d2e448d5 99
39708c20 100 if (!xfs_verify_magic(bp, rmt->rm_magic))
a6a781a5 101 return __this_address;
ce748eaa 102 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid))
a6a781a5 103 return __this_address;
7bc0dc27 104 if (be64_to_cpu(rmt->rm_blkno) != bno)
a6a781a5 105 return __this_address;
7bc0dc27 106 if (be32_to_cpu(rmt->rm_bytes) > fsbsize - sizeof(*rmt))
a6a781a5 107 return __this_address;
d2e448d5 108 if (be32_to_cpu(rmt->rm_offset) +
51fcbfe7 109 be32_to_cpu(rmt->rm_bytes) > XFS_XATTR_SIZE_MAX)
a6a781a5 110 return __this_address;
d2e448d5 111 if (rmt->rm_owner == 0)
a6a781a5 112 return __this_address;
d2e448d5 113
a6a781a5 114 return NULL;
d2e448d5
DC
115}
116
b5572597
DW
117static int
118__xfs_attr3_rmt_read_verify(
119 struct xfs_buf *bp,
120 bool check_crc,
121 xfs_failaddr_t *failaddr)
d2e448d5 122{
dbd329f1 123 struct xfs_mount *mp = bp->b_mount;
7bc0dc27
DC
124 char *ptr;
125 int len;
7bc0dc27 126 xfs_daddr_t bno;
c2c4c477 127 int blksize = mp->m_attr_geo->blksize;
d2e448d5
DC
128
129 /* no verification of non-crc buffers */
38c26bfd 130 if (!xfs_has_crc(mp))
b5572597 131 return 0;
d2e448d5 132
7bc0dc27 133 ptr = bp->b_addr;
9343ee76 134 bno = xfs_buf_daddr(bp);
7bc0dc27 135 len = BBTOB(bp->b_length);
c2c4c477 136 ASSERT(len >= blksize);
7bc0dc27
DC
137
138 while (len > 0) {
b5572597
DW
139 if (check_crc &&
140 !xfs_verify_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF)) {
141 *failaddr = __this_address;
142 return -EFSBADCRC;
7bc0dc27 143 }
39708c20 144 *failaddr = xfs_attr3_rmt_verify(mp, bp, ptr, blksize, bno);
b5572597
DW
145 if (*failaddr)
146 return -EFSCORRUPTED;
c2c4c477
DC
147 len -= blksize;
148 ptr += blksize;
149 bno += BTOBB(blksize);
7bc0dc27
DC
150 }
151
b5572597
DW
152 if (len != 0) {
153 *failaddr = __this_address;
154 return -EFSCORRUPTED;
155 }
156
157 return 0;
158}
159
160static void
161xfs_attr3_rmt_read_verify(
162 struct xfs_buf *bp)
163{
164 xfs_failaddr_t fa;
165 int error;
166
167 error = __xfs_attr3_rmt_read_verify(bp, true, &fa);
168 if (error)
169 xfs_verifier_error(bp, error, fa);
170}
171
172static xfs_failaddr_t
173xfs_attr3_rmt_verify_struct(
174 struct xfs_buf *bp)
175{
176 xfs_failaddr_t fa;
177 int error;
178
179 error = __xfs_attr3_rmt_read_verify(bp, false, &fa);
180 return error ? fa : NULL;
d2e448d5
DC
181}
182
183static void
184xfs_attr3_rmt_write_verify(
185 struct xfs_buf *bp)
186{
dbd329f1 187 struct xfs_mount *mp = bp->b_mount;
bc1a09b8 188 xfs_failaddr_t fa;
e3c32ee9 189 int blksize = mp->m_attr_geo->blksize;
7bc0dc27
DC
190 char *ptr;
191 int len;
192 xfs_daddr_t bno;
d2e448d5
DC
193
194 /* no verification of non-crc buffers */
38c26bfd 195 if (!xfs_has_crc(mp))
d2e448d5
DC
196 return;
197
7bc0dc27 198 ptr = bp->b_addr;
9343ee76 199 bno = xfs_buf_daddr(bp);
7bc0dc27 200 len = BBTOB(bp->b_length);
c2c4c477 201 ASSERT(len >= blksize);
7bc0dc27
DC
202
203 while (len > 0) {
e3c32ee9
DC
204 struct xfs_attr3_rmt_hdr *rmt = (struct xfs_attr3_rmt_hdr *)ptr;
205
39708c20 206 fa = xfs_attr3_rmt_verify(mp, bp, ptr, blksize, bno);
bc1a09b8
DW
207 if (fa) {
208 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
7bc0dc27
DC
209 return;
210 }
d2e448d5 211
e3c32ee9
DC
212 /*
213 * Ensure we aren't writing bogus LSNs to disk. See
214 * xfs_attr3_rmt_hdr_set() for the explanation.
215 */
216 if (rmt->rm_lsn != cpu_to_be64(NULLCOMMITLSN)) {
bc1a09b8 217 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
e3c32ee9 218 return;
7bc0dc27 219 }
c2c4c477 220 xfs_update_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF);
7bc0dc27 221
c2c4c477
DC
222 len -= blksize;
223 ptr += blksize;
224 bno += BTOBB(blksize);
d2e448d5 225 }
31ca03c9
DW
226
227 if (len != 0)
bc1a09b8 228 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
d2e448d5
DC
229}
230
231const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
233135b7 232 .name = "xfs_attr3_rmt",
39708c20 233 .magic = { 0, cpu_to_be32(XFS_ATTR3_RMT_MAGIC) },
d2e448d5
DC
234 .verify_read = xfs_attr3_rmt_read_verify,
235 .verify_write = xfs_attr3_rmt_write_verify,
b5572597 236 .verify_struct = xfs_attr3_rmt_verify_struct,
d2e448d5
DC
237};
238
7bc0dc27 239STATIC int
d2e448d5
DC
240xfs_attr3_rmt_hdr_set(
241 struct xfs_mount *mp,
7bc0dc27 242 void *ptr,
d2e448d5
DC
243 xfs_ino_t ino,
244 uint32_t offset,
245 uint32_t size,
7bc0dc27 246 xfs_daddr_t bno)
d2e448d5 247{
7bc0dc27 248 struct xfs_attr3_rmt_hdr *rmt = ptr;
d2e448d5 249
38c26bfd 250 if (!xfs_has_crc(mp))
d2e448d5
DC
251 return 0;
252
253 rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC);
254 rmt->rm_offset = cpu_to_be32(offset);
255 rmt->rm_bytes = cpu_to_be32(size);
ce748eaa 256 uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid);
d2e448d5 257 rmt->rm_owner = cpu_to_be64(ino);
7bc0dc27 258 rmt->rm_blkno = cpu_to_be64(bno);
d2e448d5 259
e3c32ee9
DC
260 /*
261 * Remote attribute blocks are written synchronously, so we don't
262 * have an LSN that we can stamp in them that makes any sense to log
263 * recovery. To ensure that log recovery handles overwrites of these
264 * blocks sanely (i.e. once they've been freed and reallocated as some
265 * other type of metadata) we need to ensure that the LSN has a value
266 * that tells log recovery to ignore the LSN and overwrite the buffer
267 * with whatever is in it's log. To do this, we use the magic
268 * NULLCOMMITLSN to indicate that the LSN is invalid.
269 */
270 rmt->rm_lsn = cpu_to_be64(NULLCOMMITLSN);
271
d2e448d5
DC
272 return sizeof(struct xfs_attr3_rmt_hdr);
273}
274
275/*
7bc0dc27 276 * Helper functions to copy attribute data in and out of the one disk extents
d2e448d5 277 */
7bc0dc27
DC
278STATIC int
279xfs_attr_rmtval_copyout(
ca14c096
DW
280 struct xfs_mount *mp,
281 struct xfs_buf *bp,
282 struct xfs_inode *dp,
283 int *offset,
284 int *valuelen,
285 uint8_t **dst)
d2e448d5 286{
ca14c096
DW
287 char *src = bp->b_addr;
288 xfs_ino_t ino = dp->i_ino;
289 xfs_daddr_t bno = xfs_buf_daddr(bp);
290 int len = BBTOB(bp->b_length);
291 int blksize = mp->m_attr_geo->blksize;
d2e448d5 292
c2c4c477 293 ASSERT(len >= blksize);
d2e448d5 294
7bc0dc27
DC
295 while (len > 0 && *valuelen > 0) {
296 int hdr_size = 0;
c2c4c477 297 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize);
7bc0dc27 298
c5c249b4 299 byte_cnt = min(*valuelen, byte_cnt);
7bc0dc27 300
38c26bfd 301 if (xfs_has_crc(mp)) {
a6a781a5 302 if (xfs_attr3_rmt_hdr_ok(src, ino, *offset,
7bc0dc27
DC
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);
ca14c096 307 xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
2451337d 308 return -EFSCORRUPTED;
7bc0dc27
DC
309 }
310 hdr_size = sizeof(struct xfs_attr3_rmt_hdr);
311 }
312
313 memcpy(*dst, src + hdr_size, byte_cnt);
314
315 /* roll buffer forwards */
c2c4c477
DC
316 len -= blksize;
317 src += blksize;
318 bno += BTOBB(blksize);
7bc0dc27
DC
319
320 /* roll attribute data forwards */
321 *valuelen -= byte_cnt;
322 *dst += byte_cnt;
323 *offset += byte_cnt;
324 }
325 return 0;
326}
327
328STATIC void
329xfs_attr_rmtval_copyin(
330 struct xfs_mount *mp,
331 struct xfs_buf *bp,
332 xfs_ino_t ino,
333 int *offset,
334 int *valuelen,
c8ce540d 335 uint8_t **src)
7bc0dc27
DC
336{
337 char *dst = bp->b_addr;
9343ee76 338 xfs_daddr_t bno = xfs_buf_daddr(bp);
7bc0dc27 339 int len = BBTOB(bp->b_length);
c2c4c477 340 int blksize = mp->m_attr_geo->blksize;
7bc0dc27 341
c2c4c477 342 ASSERT(len >= blksize);
7bc0dc27
DC
343
344 while (len > 0 && *valuelen > 0) {
345 int hdr_size;
c2c4c477 346 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize);
7bc0dc27
DC
347
348 byte_cnt = min(*valuelen, byte_cnt);
349 hdr_size = xfs_attr3_rmt_hdr_set(mp, dst, ino, *offset,
350 byte_cnt, bno);
351
352 memcpy(dst + hdr_size, *src, byte_cnt);
353
354 /*
355 * If this is the last block, zero the remainder of it.
356 * Check that we are actually the last block, too.
357 */
c2c4c477 358 if (byte_cnt + hdr_size < blksize) {
7bc0dc27 359 ASSERT(*valuelen - byte_cnt == 0);
c2c4c477 360 ASSERT(len == blksize);
7bc0dc27 361 memset(dst + hdr_size + byte_cnt, 0,
c2c4c477 362 blksize - hdr_size - byte_cnt);
7bc0dc27
DC
363 }
364
365 /* roll buffer forwards */
c2c4c477
DC
366 len -= blksize;
367 dst += blksize;
368 bno += BTOBB(blksize);
7bc0dc27
DC
369
370 /* roll attribute data forwards */
371 *valuelen -= byte_cnt;
372 *src += byte_cnt;
373 *offset += byte_cnt;
374 }
d2e448d5
DC
375}
376
95920cd6
DC
377/*
378 * Read the value associated with an attribute from the out-of-line buffer
379 * that we stored it in.
728bcaa3
DC
380 *
381 * Returns 0 on successful retrieval, otherwise an error.
95920cd6
DC
382 */
383int
d2e448d5
DC
384xfs_attr_rmtval_get(
385 struct xfs_da_args *args)
95920cd6 386{
d2e448d5
DC
387 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
388 struct xfs_mount *mp = args->dp->i_mount;
389 struct xfs_buf *bp;
d2e448d5 390 xfs_dablk_t lblkno = args->rmtblkno;
c8ce540d 391 uint8_t *dst = args->value;
8275cdd0 392 int valuelen;
d2e448d5
DC
393 int nmap;
394 int error;
7bc0dc27 395 int blkcnt = args->rmtblkcnt;
d2e448d5
DC
396 int i;
397 int offset = 0;
95920cd6
DC
398
399 trace_xfs_attr_rmtval_get(args);
400
e513e25c 401 ASSERT(args->valuelen != 0);
8275cdd0 402 ASSERT(args->rmtvaluelen == args->valuelen);
95920cd6 403
8275cdd0 404 valuelen = args->rmtvaluelen;
95920cd6
DC
405 while (valuelen > 0) {
406 nmap = ATTR_RMTVALUE_MAPSIZE;
407 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
551b382f 408 blkcnt, map, &nmap,
95920cd6
DC
409 XFS_BMAPI_ATTRFORK);
410 if (error)
d2e448d5 411 return error;
95920cd6
DC
412 ASSERT(nmap >= 1);
413
414 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
7bc0dc27
DC
415 xfs_daddr_t dblkno;
416 int dblkcnt;
d2e448d5 417
95920cd6
DC
418 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
419 (map[i].br_startblock != HOLESTARTBLOCK));
420 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
7bc0dc27 421 dblkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
0e3eccce
DW
422 error = xfs_buf_read(mp->m_ddev_targp, dblkno, dblkcnt,
423 0, &bp, &xfs_attr3_rmt_buf_ops);
ca14c096
DW
424 if (xfs_metadata_is_sick(error))
425 xfs_dirattr_mark_sick(args->dp, XFS_ATTR_FORK);
0e3eccce
DW
426 if (error)
427 return error;
d2e448d5 428
ca14c096 429 error = xfs_attr_rmtval_copyout(mp, bp, args->dp,
7bc0dc27
DC
430 &offset, &valuelen,
431 &dst);
e8db2aaf 432 xfs_buf_relse(bp);
7bc0dc27
DC
433 if (error)
434 return error;
d2e448d5 435
7bc0dc27 436 /* roll attribute extent map forwards */
95920cd6 437 lblkno += map[i].br_blockcount;
7bc0dc27 438 blkcnt -= map[i].br_blockcount;
95920cd6
DC
439 }
440 }
441 ASSERT(valuelen == 0);
d2e448d5 442 return 0;
95920cd6
DC
443}
444
445/*
1a485fc1 446 * Find a "hole" in the attribute address space large enough for us to drop the
8f502a40 447 * new attributes value into
95920cd6 448 */
8f502a40 449int
1a485fc1 450xfs_attr_rmt_find_hole(
d2e448d5 451 struct xfs_da_args *args)
95920cd6 452{
d2e448d5
DC
453 struct xfs_inode *dp = args->dp;
454 struct xfs_mount *mp = dp->i_mount;
d2e448d5 455 int error;
1a485fc1
AC
456 int blkcnt;
457 xfs_fileoff_t lfileoff = 0;
95920cd6 458
95920cd6 459 /*
1a485fc1
AC
460 * Because CRC enable attributes have headers, we can't just do a
461 * straight byte to FSB conversion and have to take the header space
462 * into account.
95920cd6 463 */
8275cdd0 464 blkcnt = xfs_attr3_rmt_blocks(mp, args->rmtvaluelen);
95920cd6
DC
465 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
466 XFS_ATTR_FORK);
d2e448d5
DC
467 if (error)
468 return error;
469
1a485fc1 470 args->rmtblkno = (xfs_dablk_t)lfileoff;
95920cd6
DC
471 args->rmtblkcnt = blkcnt;
472
1a485fc1
AC
473 return 0;
474}
95920cd6 475
8f502a40 476int
1a485fc1
AC
477xfs_attr_rmtval_set_value(
478 struct xfs_da_args *args)
479{
480 struct xfs_inode *dp = args->dp;
481 struct xfs_mount *mp = dp->i_mount;
482 struct xfs_bmbt_irec map;
483 xfs_dablk_t lblkno;
484 uint8_t *src = args->value;
485 int blkcnt;
486 int valuelen;
487 int nmap;
488 int error;
489 int offset = 0;
95920cd6
DC
490
491 /*
492 * Roll through the "value", copying the attribute value to the
493 * already-allocated blocks. Blocks are written synchronously
494 * so that we can know they are all on disk before we turn off
495 * the INCOMPLETE flag.
496 */
497 lblkno = args->rmtblkno;
26f71445 498 blkcnt = args->rmtblkcnt;
8275cdd0 499 valuelen = args->rmtvaluelen;
95920cd6 500 while (valuelen > 0) {
7bc0dc27
DC
501 struct xfs_buf *bp;
502 xfs_daddr_t dblkno;
503 int dblkcnt;
504
505 ASSERT(blkcnt > 0);
95920cd6 506
95920cd6
DC
507 nmap = 1;
508 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
26f71445 509 blkcnt, &map, &nmap,
95920cd6
DC
510 XFS_BMAPI_ATTRFORK);
511 if (error)
d99831ff 512 return error;
95920cd6
DC
513 ASSERT(nmap == 1);
514 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
515 (map.br_startblock != HOLESTARTBLOCK));
516
517 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
26f71445 518 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
95920cd6 519
841263e9
DW
520 error = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, &bp);
521 if (error)
522 return error;
d2e448d5 523 bp->b_ops = &xfs_attr3_rmt_buf_ops;
26f71445 524
7bc0dc27
DC
525 xfs_attr_rmtval_copyin(mp, bp, args->dp->i_ino, &offset,
526 &valuelen, &src);
95920cd6
DC
527
528 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
529 xfs_buf_relse(bp);
530 if (error)
531 return error;
d2e448d5 532
95920cd6 533
7bc0dc27 534 /* roll attribute extent map forwards */
95920cd6 535 lblkno += map.br_blockcount;
26f71445 536 blkcnt -= map.br_blockcount;
95920cd6
DC
537 }
538 ASSERT(valuelen == 0);
d2e448d5 539 return 0;
95920cd6
DC
540}
541
8edbb26b
DW
542/* Mark stale any incore buffers for the remote value. */
543int
544xfs_attr_rmtval_stale(
545 struct xfs_inode *ip,
546 struct xfs_bmbt_irec *map,
547 xfs_buf_flags_t incore_flags)
548{
549 struct xfs_mount *mp = ip->i_mount;
550 struct xfs_buf *bp;
85c73bf7 551 int error;
8edbb26b 552
3fed24ff 553 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
8edbb26b
DW
554
555 if (XFS_IS_CORRUPT(mp, map->br_startblock == DELAYSTARTBLOCK) ||
989d5ec3
DW
556 XFS_IS_CORRUPT(mp, map->br_startblock == HOLESTARTBLOCK)) {
557 xfs_bmap_mark_sick(ip, XFS_ATTR_FORK);
8edbb26b 558 return -EFSCORRUPTED;
989d5ec3 559 }
8edbb26b 560
85c73bf7 561 error = xfs_buf_incore(mp->m_ddev_targp,
8edbb26b 562 XFS_FSB_TO_DADDR(mp, map->br_startblock),
85c73bf7
DC
563 XFS_FSB_TO_BB(mp, map->br_blockcount),
564 incore_flags, &bp);
565 if (error) {
566 if (error == -ENOENT)
567 return 0;
568 return error;
8edbb26b
DW
569 }
570
85c73bf7
DC
571 xfs_buf_stale(bp);
572 xfs_buf_relse(bp);
8edbb26b
DW
573 return 0;
574}
575
8f502a40
AH
576/*
577 * Find a hole for the attr and store it in the delayed attr context. This
578 * initializes the context to roll through allocating an attr extent for a
579 * delayed attr operation
580 */
581int
582xfs_attr_rmtval_find_space(
e3c5de22 583 struct xfs_attr_intent *attr)
8f502a40 584{
d68c51e9
AH
585 struct xfs_da_args *args = attr->xattri_da_args;
586 struct xfs_bmbt_irec *map = &attr->xattri_map;
8f502a40
AH
587 int error;
588
d68c51e9
AH
589 attr->xattri_lblkno = 0;
590 attr->xattri_blkcnt = 0;
8f502a40
AH
591 args->rmtblkcnt = 0;
592 args->rmtblkno = 0;
593 memset(map, 0, sizeof(struct xfs_bmbt_irec));
594
595 error = xfs_attr_rmt_find_hole(args);
596 if (error)
597 return error;
598
d68c51e9
AH
599 attr->xattri_blkcnt = args->rmtblkcnt;
600 attr->xattri_lblkno = args->rmtblkno;
8f502a40
AH
601
602 return 0;
603}
604
605/*
606 * Write one block of the value associated with an attribute into the
607 * out-of-line buffer that we have defined for it. This is similar to a subset
608 * of xfs_attr_rmtval_set, but records the current block to the delayed attr
609 * context, and leaves transaction handling to the caller.
610 */
611int
612xfs_attr_rmtval_set_blk(
e3c5de22 613 struct xfs_attr_intent *attr)
8f502a40 614{
d68c51e9 615 struct xfs_da_args *args = attr->xattri_da_args;
8f502a40 616 struct xfs_inode *dp = args->dp;
d68c51e9 617 struct xfs_bmbt_irec *map = &attr->xattri_map;
8f502a40
AH
618 int nmap;
619 int error;
620
621 nmap = 1;
d68c51e9
AH
622 error = xfs_bmapi_write(args->trans, dp,
623 (xfs_fileoff_t)attr->xattri_lblkno,
624 attr->xattri_blkcnt, XFS_BMAPI_ATTRFORK, args->total,
8f502a40
AH
625 map, &nmap);
626 if (error)
627 return error;
628
629 ASSERT(nmap == 1);
630 ASSERT((map->br_startblock != DELAYSTARTBLOCK) &&
631 (map->br_startblock != HOLESTARTBLOCK));
632
633 /* roll attribute extent map forwards */
d68c51e9
AH
634 attr->xattri_lblkno += map->br_blockcount;
635 attr->xattri_blkcnt -= map->br_blockcount;
8f502a40
AH
636
637 return 0;
638}
639
95920cd6
DC
640/*
641 * Remove the value associated with an attribute by deleting the
642 * out-of-line buffer that it is stored on.
643 */
644int
79514109 645xfs_attr_rmtval_invalidate(
7bc0dc27 646 struct xfs_da_args *args)
95920cd6 647{
7bc0dc27
DC
648 xfs_dablk_t lblkno;
649 int blkcnt;
650 int error;
95920cd6 651
95920cd6 652 /*
58a72281 653 * Roll through the "value", invalidating the attribute value's blocks.
95920cd6
DC
654 */
655 lblkno = args->rmtblkno;
7bc0dc27
DC
656 blkcnt = args->rmtblkcnt;
657 while (blkcnt > 0) {
658 struct xfs_bmbt_irec map;
7bc0dc27 659 int nmap;
58a72281 660
95920cd6
DC
661 /*
662 * Try to remember where we decided to put the value.
663 */
664 nmap = 1;
665 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
58a72281 666 blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
95920cd6 667 if (error)
d99831ff 668 return error;
989d5ec3
DW
669 if (XFS_IS_CORRUPT(args->dp->i_mount, nmap != 1)) {
670 xfs_bmap_mark_sick(args->dp, XFS_ATTR_FORK);
8edbb26b 671 return -EFSCORRUPTED;
989d5ec3 672 }
8edbb26b
DW
673 error = xfs_attr_rmtval_stale(args->dp, &map, XBF_TRYLOCK);
674 if (error)
675 return error;
95920cd6 676
95920cd6 677 lblkno += map.br_blockcount;
58a72281 678 blkcnt -= map.br_blockcount;
95920cd6 679 }
79514109
AC
680 return 0;
681}
95920cd6 682
8b8e0cc0
AC
683/*
684 * Remove the value associated with an attribute by deleting the out-of-line
2b74b03c
AH
685 * buffer that it is stored on. Returns -EAGAIN for the caller to refresh the
686 * transaction and re-call the function. Callers should keep calling this
687 * routine until it returns something other than -EAGAIN.
8b8e0cc0
AC
688 */
689int
5e68b4c7 690xfs_attr_rmtval_remove(
e3c5de22 691 struct xfs_attr_intent *attr)
8b8e0cc0 692{
d68c51e9 693 struct xfs_da_args *args = attr->xattri_da_args;
2b74b03c 694 int error, done;
8b8e0cc0
AC
695
696 /*
697 * Unmap value blocks for this attr.
698 */
699 error = xfs_bunmapi(args->trans, args->dp, args->rmtblkno,
700 args->rmtblkcnt, XFS_BMAPI_ATTRFORK, 1, &done);
701 if (error)
702 return error;
703
2b74b03c
AH
704 /*
705 * We don't need an explicit state here to pick up where we left off. We
706 * can figure it out using the !done return code. The actual value of
707 * attr->xattri_dela_state may be some value reminiscent of the calling
708 * function, but it's value is irrelevant with in the context of this
709 * function. Once we are done here, the next state is set as needed by
710 * the parent
711 */
712 if (!done) {
d68c51e9
AH
713 trace_xfs_attr_rmtval_remove_return(attr->xattri_dela_state,
714 args->dp);
8b8e0cc0 715 return -EAGAIN;
2b74b03c 716 }
8b8e0cc0 717
2b74b03c
AH
718 args->rmtblkno = 0;
719 args->rmtblkcnt = 0;
720 return 0;
8b8e0cc0 721}