1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
7 #include "libxfs_priv.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_defer.h"
16 #include "xfs_da_format.h"
17 #include "xfs_da_btree.h"
18 #include "xfs_inode.h"
19 #include "xfs_alloc.h"
20 #include "xfs_trans.h"
22 #include "xfs_attr_leaf.h"
23 #include "xfs_attr_remote.h"
24 #include "xfs_trans_space.h"
25 #include "xfs_trace.h"
26 #include "xfs_cksum.h"
28 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
31 * Each contiguous block has a header, so it is not just a simple attribute
32 * length to FSB conversion.
39 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
40 int buflen
= XFS_ATTR3_RMT_BUF_SPACE(mp
, mp
->m_sb
.sb_blocksize
);
41 return (attrlen
+ buflen
- 1) / buflen
;
43 return XFS_B_TO_FSB(mp
, attrlen
);
47 * Checking of the remote attribute header is split into two parts. The verifier
48 * does CRC, location and bounds checking, the unpacking function checks the
49 * attribute parameters and owner.
59 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
61 if (bno
!= be64_to_cpu(rmt
->rm_blkno
))
62 return __this_address
;
63 if (offset
!= be32_to_cpu(rmt
->rm_offset
))
64 return __this_address
;
65 if (size
!= be32_to_cpu(rmt
->rm_bytes
))
66 return __this_address
;
67 if (ino
!= be64_to_cpu(rmt
->rm_owner
))
68 return __this_address
;
81 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
83 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
84 return __this_address
;
85 if (rmt
->rm_magic
!= cpu_to_be32(XFS_ATTR3_RMT_MAGIC
))
86 return __this_address
;
87 if (!uuid_equal(&rmt
->rm_uuid
, &mp
->m_sb
.sb_meta_uuid
))
88 return __this_address
;
89 if (be64_to_cpu(rmt
->rm_blkno
) != bno
)
90 return __this_address
;
91 if (be32_to_cpu(rmt
->rm_bytes
) > fsbsize
- sizeof(*rmt
))
92 return __this_address
;
93 if (be32_to_cpu(rmt
->rm_offset
) +
94 be32_to_cpu(rmt
->rm_bytes
) > XFS_XATTR_SIZE_MAX
)
95 return __this_address
;
96 if (rmt
->rm_owner
== 0)
97 return __this_address
;
103 __xfs_attr3_rmt_read_verify(
106 xfs_failaddr_t
*failaddr
)
108 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
112 int blksize
= mp
->m_attr_geo
->blksize
;
114 /* no verification of non-crc buffers */
115 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
120 len
= BBTOB(bp
->b_length
);
121 ASSERT(len
>= blksize
);
125 !xfs_verify_cksum(ptr
, blksize
, XFS_ATTR3_RMT_CRC_OFF
)) {
126 *failaddr
= __this_address
;
129 *failaddr
= xfs_attr3_rmt_verify(mp
, ptr
, blksize
, bno
);
131 return -EFSCORRUPTED
;
134 bno
+= BTOBB(blksize
);
138 *failaddr
= __this_address
;
139 return -EFSCORRUPTED
;
146 xfs_attr3_rmt_read_verify(
152 error
= __xfs_attr3_rmt_read_verify(bp
, true, &fa
);
154 xfs_verifier_error(bp
, error
, fa
);
157 static xfs_failaddr_t
158 xfs_attr3_rmt_verify_struct(
164 error
= __xfs_attr3_rmt_read_verify(bp
, false, &fa
);
165 return error
? fa
: NULL
;
169 xfs_attr3_rmt_write_verify(
172 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
174 int blksize
= mp
->m_attr_geo
->blksize
;
179 /* no verification of non-crc buffers */
180 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
185 len
= BBTOB(bp
->b_length
);
186 ASSERT(len
>= blksize
);
189 struct xfs_attr3_rmt_hdr
*rmt
= (struct xfs_attr3_rmt_hdr
*)ptr
;
191 fa
= xfs_attr3_rmt_verify(mp
, ptr
, blksize
, bno
);
193 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
198 * Ensure we aren't writing bogus LSNs to disk. See
199 * xfs_attr3_rmt_hdr_set() for the explanation.
201 if (rmt
->rm_lsn
!= cpu_to_be64(NULLCOMMITLSN
)) {
202 xfs_verifier_error(bp
, -EFSCORRUPTED
, __this_address
);
205 xfs_update_cksum(ptr
, blksize
, XFS_ATTR3_RMT_CRC_OFF
);
209 bno
+= BTOBB(blksize
);
213 xfs_verifier_error(bp
, -EFSCORRUPTED
, __this_address
);
216 const struct xfs_buf_ops xfs_attr3_rmt_buf_ops
= {
217 .name
= "xfs_attr3_rmt",
218 .verify_read
= xfs_attr3_rmt_read_verify
,
219 .verify_write
= xfs_attr3_rmt_write_verify
,
220 .verify_struct
= xfs_attr3_rmt_verify_struct
,
224 xfs_attr3_rmt_hdr_set(
225 struct xfs_mount
*mp
,
232 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
234 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
237 rmt
->rm_magic
= cpu_to_be32(XFS_ATTR3_RMT_MAGIC
);
238 rmt
->rm_offset
= cpu_to_be32(offset
);
239 rmt
->rm_bytes
= cpu_to_be32(size
);
240 uuid_copy(&rmt
->rm_uuid
, &mp
->m_sb
.sb_meta_uuid
);
241 rmt
->rm_owner
= cpu_to_be64(ino
);
242 rmt
->rm_blkno
= cpu_to_be64(bno
);
245 * Remote attribute blocks are written synchronously, so we don't
246 * have an LSN that we can stamp in them that makes any sense to log
247 * recovery. To ensure that log recovery handles overwrites of these
248 * blocks sanely (i.e. once they've been freed and reallocated as some
249 * other type of metadata) we need to ensure that the LSN has a value
250 * that tells log recovery to ignore the LSN and overwrite the buffer
251 * with whatever is in it's log. To do this, we use the magic
252 * NULLCOMMITLSN to indicate that the LSN is invalid.
254 rmt
->rm_lsn
= cpu_to_be64(NULLCOMMITLSN
);
256 return sizeof(struct xfs_attr3_rmt_hdr
);
260 * Helper functions to copy attribute data in and out of the one disk extents
263 xfs_attr_rmtval_copyout(
264 struct xfs_mount
*mp
,
271 char *src
= bp
->b_addr
;
272 xfs_daddr_t bno
= bp
->b_bn
;
273 int len
= BBTOB(bp
->b_length
);
274 int blksize
= mp
->m_attr_geo
->blksize
;
276 ASSERT(len
>= blksize
);
278 while (len
> 0 && *valuelen
> 0) {
280 int byte_cnt
= XFS_ATTR3_RMT_BUF_SPACE(mp
, blksize
);
282 byte_cnt
= min(*valuelen
, byte_cnt
);
284 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
285 if (xfs_attr3_rmt_hdr_ok(src
, ino
, *offset
,
288 "remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)",
289 bno
, *offset
, byte_cnt
, ino
);
290 return -EFSCORRUPTED
;
292 hdr_size
= sizeof(struct xfs_attr3_rmt_hdr
);
295 memcpy(*dst
, src
+ hdr_size
, byte_cnt
);
297 /* roll buffer forwards */
300 bno
+= BTOBB(blksize
);
302 /* roll attribute data forwards */
303 *valuelen
-= byte_cnt
;
311 xfs_attr_rmtval_copyin(
312 struct xfs_mount
*mp
,
319 char *dst
= bp
->b_addr
;
320 xfs_daddr_t bno
= bp
->b_bn
;
321 int len
= BBTOB(bp
->b_length
);
322 int blksize
= mp
->m_attr_geo
->blksize
;
324 ASSERT(len
>= blksize
);
326 while (len
> 0 && *valuelen
> 0) {
328 int byte_cnt
= XFS_ATTR3_RMT_BUF_SPACE(mp
, blksize
);
330 byte_cnt
= min(*valuelen
, byte_cnt
);
331 hdr_size
= xfs_attr3_rmt_hdr_set(mp
, dst
, ino
, *offset
,
334 memcpy(dst
+ hdr_size
, *src
, byte_cnt
);
337 * If this is the last block, zero the remainder of it.
338 * Check that we are actually the last block, too.
340 if (byte_cnt
+ hdr_size
< blksize
) {
341 ASSERT(*valuelen
- byte_cnt
== 0);
342 ASSERT(len
== blksize
);
343 memset(dst
+ hdr_size
+ byte_cnt
, 0,
344 blksize
- hdr_size
- byte_cnt
);
347 /* roll buffer forwards */
350 bno
+= BTOBB(blksize
);
352 /* roll attribute data forwards */
353 *valuelen
-= byte_cnt
;
360 * Read the value associated with an attribute from the out-of-line buffer
361 * that we stored it in.
365 struct xfs_da_args
*args
)
367 struct xfs_bmbt_irec map
[ATTR_RMTVALUE_MAPSIZE
];
368 struct xfs_mount
*mp
= args
->dp
->i_mount
;
370 xfs_dablk_t lblkno
= args
->rmtblkno
;
371 uint8_t *dst
= args
->value
;
375 int blkcnt
= args
->rmtblkcnt
;
379 trace_xfs_attr_rmtval_get(args
);
381 ASSERT(!(args
->flags
& ATTR_KERNOVAL
));
382 ASSERT(args
->rmtvaluelen
== args
->valuelen
);
384 valuelen
= args
->rmtvaluelen
;
385 while (valuelen
> 0) {
386 nmap
= ATTR_RMTVALUE_MAPSIZE
;
387 error
= xfs_bmapi_read(args
->dp
, (xfs_fileoff_t
)lblkno
,
394 for (i
= 0; (i
< nmap
) && (valuelen
> 0); i
++) {
398 ASSERT((map
[i
].br_startblock
!= DELAYSTARTBLOCK
) &&
399 (map
[i
].br_startblock
!= HOLESTARTBLOCK
));
400 dblkno
= XFS_FSB_TO_DADDR(mp
, map
[i
].br_startblock
);
401 dblkcnt
= XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
402 error
= xfs_trans_read_buf(mp
, args
->trans
,
404 dblkno
, dblkcnt
, 0, &bp
,
405 &xfs_attr3_rmt_buf_ops
);
409 error
= xfs_attr_rmtval_copyout(mp
, bp
, args
->dp
->i_ino
,
412 xfs_trans_brelse(args
->trans
, bp
);
416 /* roll attribute extent map forwards */
417 lblkno
+= map
[i
].br_blockcount
;
418 blkcnt
-= map
[i
].br_blockcount
;
421 ASSERT(valuelen
== 0);
426 * Write the value associated with an attribute into the out-of-line buffer
427 * that we have defined for it.
431 struct xfs_da_args
*args
)
433 struct xfs_inode
*dp
= args
->dp
;
434 struct xfs_mount
*mp
= dp
->i_mount
;
435 struct xfs_bmbt_irec map
;
437 xfs_fileoff_t lfileoff
= 0;
438 uint8_t *src
= args
->value
;
445 trace_xfs_attr_rmtval_set(args
);
448 * Find a "hole" in the attribute address space large enough for
449 * us to drop the new attribute's value into. Because CRC enable
450 * attributes have headers, we can't just do a straight byte to FSB
451 * conversion and have to take the header space into account.
453 blkcnt
= xfs_attr3_rmt_blocks(mp
, args
->rmtvaluelen
);
454 error
= xfs_bmap_first_unused(args
->trans
, args
->dp
, blkcnt
, &lfileoff
,
459 args
->rmtblkno
= lblkno
= (xfs_dablk_t
)lfileoff
;
460 args
->rmtblkcnt
= blkcnt
;
463 * Roll through the "value", allocating blocks on disk as required.
467 * Allocate a single extent, up to the size of the value.
469 * Note that we have to consider this a data allocation as we
470 * write the remote attribute without logging the contents.
471 * Hence we must ensure that we aren't using blocks that are on
472 * the busy list so that we don't overwrite blocks which have
473 * recently been freed but their transactions are not yet
474 * committed to disk. If we overwrite the contents of a busy
475 * extent and then crash then the block may not contain the
476 * correct metadata after log recovery occurs.
479 error
= xfs_bmapi_write(args
->trans
, dp
, (xfs_fileoff_t
)lblkno
,
480 blkcnt
, XFS_BMAPI_ATTRFORK
, args
->total
, &map
,
484 error
= xfs_defer_finish(&args
->trans
);
489 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
490 (map
.br_startblock
!= HOLESTARTBLOCK
));
491 lblkno
+= map
.br_blockcount
;
492 blkcnt
-= map
.br_blockcount
;
495 * Start the next trans in the chain.
497 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
503 * Roll through the "value", copying the attribute value to the
504 * already-allocated blocks. Blocks are written synchronously
505 * so that we can know they are all on disk before we turn off
506 * the INCOMPLETE flag.
508 lblkno
= args
->rmtblkno
;
509 blkcnt
= args
->rmtblkcnt
;
510 valuelen
= args
->rmtvaluelen
;
511 while (valuelen
> 0) {
519 error
= xfs_bmapi_read(dp
, (xfs_fileoff_t
)lblkno
,
525 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
526 (map
.br_startblock
!= HOLESTARTBLOCK
));
528 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
529 dblkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
531 bp
= xfs_buf_get(mp
->m_ddev_targp
, dblkno
, dblkcnt
, 0);
534 bp
->b_ops
= &xfs_attr3_rmt_buf_ops
;
536 xfs_attr_rmtval_copyin(mp
, bp
, args
->dp
->i_ino
, &offset
,
539 error
= xfs_bwrite(bp
); /* GROT: NOTE: synchronous write */
545 /* roll attribute extent map forwards */
546 lblkno
+= map
.br_blockcount
;
547 blkcnt
-= map
.br_blockcount
;
549 ASSERT(valuelen
== 0);
554 * Remove the value associated with an attribute by deleting the
555 * out-of-line buffer that it is stored on.
558 xfs_attr_rmtval_remove(
559 struct xfs_da_args
*args
)
561 struct xfs_mount
*mp
= args
->dp
->i_mount
;
567 trace_xfs_attr_rmtval_remove(args
);
570 * Roll through the "value", invalidating the attribute value's blocks.
572 lblkno
= args
->rmtblkno
;
573 blkcnt
= args
->rmtblkcnt
;
575 struct xfs_bmbt_irec map
;
582 * Try to remember where we decided to put the value.
585 error
= xfs_bmapi_read(args
->dp
, (xfs_fileoff_t
)lblkno
,
586 blkcnt
, &map
, &nmap
, XFS_BMAPI_ATTRFORK
);
590 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
591 (map
.br_startblock
!= HOLESTARTBLOCK
));
593 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
594 dblkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
597 * If the "remote" value is in the cache, remove it.
599 bp
= xfs_buf_incore(mp
->m_ddev_targp
, dblkno
, dblkcnt
, XBF_TRYLOCK
);
606 lblkno
+= map
.br_blockcount
;
607 blkcnt
-= map
.br_blockcount
;
611 * Keep de-allocating extents until the remote-value region is gone.
613 lblkno
= args
->rmtblkno
;
614 blkcnt
= args
->rmtblkcnt
;
617 error
= xfs_bunmapi(args
->trans
, args
->dp
, lblkno
, blkcnt
,
618 XFS_BMAPI_ATTRFORK
, 1, &done
);
621 error
= xfs_defer_finish(&args
->trans
);
626 * Close out trans and start the next one in the chain.
628 error
= xfs_trans_roll_inode(&args
->trans
, args
->dp
);