1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
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"
21 #include "xfs_inode_item.h"
23 #include "xfs_bmap_util.h"
25 #include "xfs_attr_leaf.h"
26 #include "xfs_attr_remote.h"
27 #include "xfs_trans_space.h"
28 #include "xfs_trace.h"
29 #include "xfs_cksum.h"
30 #include "xfs_buf_item.h"
31 #include "xfs_error.h"
33 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
36 * Each contiguous block has a header, so it is not just a simple attribute
37 * length to FSB conversion.
44 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
45 int buflen
= XFS_ATTR3_RMT_BUF_SPACE(mp
, mp
->m_sb
.sb_blocksize
);
46 return (attrlen
+ buflen
- 1) / buflen
;
48 return XFS_B_TO_FSB(mp
, attrlen
);
52 * Checking of the remote attribute header is split into two parts. The verifier
53 * does CRC, location and bounds checking, the unpacking function checks the
54 * attribute parameters and owner.
64 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
66 if (bno
!= be64_to_cpu(rmt
->rm_blkno
))
67 return __this_address
;
68 if (offset
!= be32_to_cpu(rmt
->rm_offset
))
69 return __this_address
;
70 if (size
!= be32_to_cpu(rmt
->rm_bytes
))
71 return __this_address
;
72 if (ino
!= be64_to_cpu(rmt
->rm_owner
))
73 return __this_address
;
86 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
88 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
89 return __this_address
;
90 if (rmt
->rm_magic
!= cpu_to_be32(XFS_ATTR3_RMT_MAGIC
))
91 return __this_address
;
92 if (!uuid_equal(&rmt
->rm_uuid
, &mp
->m_sb
.sb_meta_uuid
))
93 return __this_address
;
94 if (be64_to_cpu(rmt
->rm_blkno
) != bno
)
95 return __this_address
;
96 if (be32_to_cpu(rmt
->rm_bytes
) > fsbsize
- sizeof(*rmt
))
97 return __this_address
;
98 if (be32_to_cpu(rmt
->rm_offset
) +
99 be32_to_cpu(rmt
->rm_bytes
) > XFS_XATTR_SIZE_MAX
)
100 return __this_address
;
101 if (rmt
->rm_owner
== 0)
102 return __this_address
;
108 __xfs_attr3_rmt_read_verify(
111 xfs_failaddr_t
*failaddr
)
113 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
117 int blksize
= mp
->m_attr_geo
->blksize
;
119 /* no verification of non-crc buffers */
120 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
125 len
= BBTOB(bp
->b_length
);
126 ASSERT(len
>= blksize
);
130 !xfs_verify_cksum(ptr
, blksize
, XFS_ATTR3_RMT_CRC_OFF
)) {
131 *failaddr
= __this_address
;
134 *failaddr
= xfs_attr3_rmt_verify(mp
, ptr
, blksize
, bno
);
136 return -EFSCORRUPTED
;
139 bno
+= BTOBB(blksize
);
143 *failaddr
= __this_address
;
144 return -EFSCORRUPTED
;
151 xfs_attr3_rmt_read_verify(
157 error
= __xfs_attr3_rmt_read_verify(bp
, true, &fa
);
159 xfs_verifier_error(bp
, error
, fa
);
162 static xfs_failaddr_t
163 xfs_attr3_rmt_verify_struct(
169 error
= __xfs_attr3_rmt_read_verify(bp
, false, &fa
);
170 return error
? fa
: NULL
;
174 xfs_attr3_rmt_write_verify(
177 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
179 int blksize
= mp
->m_attr_geo
->blksize
;
184 /* no verification of non-crc buffers */
185 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
190 len
= BBTOB(bp
->b_length
);
191 ASSERT(len
>= blksize
);
194 struct xfs_attr3_rmt_hdr
*rmt
= (struct xfs_attr3_rmt_hdr
*)ptr
;
196 fa
= xfs_attr3_rmt_verify(mp
, ptr
, blksize
, bno
);
198 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
203 * Ensure we aren't writing bogus LSNs to disk. See
204 * xfs_attr3_rmt_hdr_set() for the explanation.
206 if (rmt
->rm_lsn
!= cpu_to_be64(NULLCOMMITLSN
)) {
207 xfs_verifier_error(bp
, -EFSCORRUPTED
, __this_address
);
210 xfs_update_cksum(ptr
, blksize
, XFS_ATTR3_RMT_CRC_OFF
);
214 bno
+= BTOBB(blksize
);
218 xfs_verifier_error(bp
, -EFSCORRUPTED
, __this_address
);
221 const struct xfs_buf_ops xfs_attr3_rmt_buf_ops
= {
222 .name
= "xfs_attr3_rmt",
223 .verify_read
= xfs_attr3_rmt_read_verify
,
224 .verify_write
= xfs_attr3_rmt_write_verify
,
225 .verify_struct
= xfs_attr3_rmt_verify_struct
,
229 xfs_attr3_rmt_hdr_set(
230 struct xfs_mount
*mp
,
237 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
239 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
242 rmt
->rm_magic
= cpu_to_be32(XFS_ATTR3_RMT_MAGIC
);
243 rmt
->rm_offset
= cpu_to_be32(offset
);
244 rmt
->rm_bytes
= cpu_to_be32(size
);
245 uuid_copy(&rmt
->rm_uuid
, &mp
->m_sb
.sb_meta_uuid
);
246 rmt
->rm_owner
= cpu_to_be64(ino
);
247 rmt
->rm_blkno
= cpu_to_be64(bno
);
250 * Remote attribute blocks are written synchronously, so we don't
251 * have an LSN that we can stamp in them that makes any sense to log
252 * recovery. To ensure that log recovery handles overwrites of these
253 * blocks sanely (i.e. once they've been freed and reallocated as some
254 * other type of metadata) we need to ensure that the LSN has a value
255 * that tells log recovery to ignore the LSN and overwrite the buffer
256 * with whatever is in it's log. To do this, we use the magic
257 * NULLCOMMITLSN to indicate that the LSN is invalid.
259 rmt
->rm_lsn
= cpu_to_be64(NULLCOMMITLSN
);
261 return sizeof(struct xfs_attr3_rmt_hdr
);
265 * Helper functions to copy attribute data in and out of the one disk extents
268 xfs_attr_rmtval_copyout(
269 struct xfs_mount
*mp
,
276 char *src
= bp
->b_addr
;
277 xfs_daddr_t bno
= bp
->b_bn
;
278 int len
= BBTOB(bp
->b_length
);
279 int blksize
= mp
->m_attr_geo
->blksize
;
281 ASSERT(len
>= blksize
);
283 while (len
> 0 && *valuelen
> 0) {
285 int byte_cnt
= XFS_ATTR3_RMT_BUF_SPACE(mp
, blksize
);
287 byte_cnt
= min(*valuelen
, byte_cnt
);
289 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
290 if (xfs_attr3_rmt_hdr_ok(src
, ino
, *offset
,
293 "remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)",
294 bno
, *offset
, byte_cnt
, ino
);
295 return -EFSCORRUPTED
;
297 hdr_size
= sizeof(struct xfs_attr3_rmt_hdr
);
300 memcpy(*dst
, src
+ hdr_size
, byte_cnt
);
302 /* roll buffer forwards */
305 bno
+= BTOBB(blksize
);
307 /* roll attribute data forwards */
308 *valuelen
-= byte_cnt
;
316 xfs_attr_rmtval_copyin(
317 struct xfs_mount
*mp
,
324 char *dst
= bp
->b_addr
;
325 xfs_daddr_t bno
= bp
->b_bn
;
326 int len
= BBTOB(bp
->b_length
);
327 int blksize
= mp
->m_attr_geo
->blksize
;
329 ASSERT(len
>= blksize
);
331 while (len
> 0 && *valuelen
> 0) {
333 int byte_cnt
= XFS_ATTR3_RMT_BUF_SPACE(mp
, blksize
);
335 byte_cnt
= min(*valuelen
, byte_cnt
);
336 hdr_size
= xfs_attr3_rmt_hdr_set(mp
, dst
, ino
, *offset
,
339 memcpy(dst
+ hdr_size
, *src
, byte_cnt
);
342 * If this is the last block, zero the remainder of it.
343 * Check that we are actually the last block, too.
345 if (byte_cnt
+ hdr_size
< blksize
) {
346 ASSERT(*valuelen
- byte_cnt
== 0);
347 ASSERT(len
== blksize
);
348 memset(dst
+ hdr_size
+ byte_cnt
, 0,
349 blksize
- hdr_size
- byte_cnt
);
352 /* roll buffer forwards */
355 bno
+= BTOBB(blksize
);
357 /* roll attribute data forwards */
358 *valuelen
-= byte_cnt
;
365 * Read the value associated with an attribute from the out-of-line buffer
366 * that we stored it in.
370 struct xfs_da_args
*args
)
372 struct xfs_bmbt_irec map
[ATTR_RMTVALUE_MAPSIZE
];
373 struct xfs_mount
*mp
= args
->dp
->i_mount
;
375 xfs_dablk_t lblkno
= args
->rmtblkno
;
376 uint8_t *dst
= args
->value
;
380 int blkcnt
= args
->rmtblkcnt
;
384 trace_xfs_attr_rmtval_get(args
);
386 ASSERT(!(args
->flags
& ATTR_KERNOVAL
));
387 ASSERT(args
->rmtvaluelen
== args
->valuelen
);
389 valuelen
= args
->rmtvaluelen
;
390 while (valuelen
> 0) {
391 nmap
= ATTR_RMTVALUE_MAPSIZE
;
392 error
= xfs_bmapi_read(args
->dp
, (xfs_fileoff_t
)lblkno
,
399 for (i
= 0; (i
< nmap
) && (valuelen
> 0); i
++) {
403 ASSERT((map
[i
].br_startblock
!= DELAYSTARTBLOCK
) &&
404 (map
[i
].br_startblock
!= HOLESTARTBLOCK
));
405 dblkno
= XFS_FSB_TO_DADDR(mp
, map
[i
].br_startblock
);
406 dblkcnt
= XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
407 error
= xfs_trans_read_buf(mp
, args
->trans
,
409 dblkno
, dblkcnt
, 0, &bp
,
410 &xfs_attr3_rmt_buf_ops
);
414 error
= xfs_attr_rmtval_copyout(mp
, bp
, args
->dp
->i_ino
,
417 xfs_trans_brelse(args
->trans
, bp
);
421 /* roll attribute extent map forwards */
422 lblkno
+= map
[i
].br_blockcount
;
423 blkcnt
-= map
[i
].br_blockcount
;
426 ASSERT(valuelen
== 0);
431 * Write the value associated with an attribute into the out-of-line buffer
432 * that we have defined for it.
436 struct xfs_da_args
*args
)
438 struct xfs_inode
*dp
= args
->dp
;
439 struct xfs_mount
*mp
= dp
->i_mount
;
440 struct xfs_bmbt_irec map
;
442 xfs_fileoff_t lfileoff
= 0;
443 uint8_t *src
= args
->value
;
450 trace_xfs_attr_rmtval_set(args
);
453 * Find a "hole" in the attribute address space large enough for
454 * us to drop the new attribute's value into. Because CRC enable
455 * attributes have headers, we can't just do a straight byte to FSB
456 * conversion and have to take the header space into account.
458 blkcnt
= xfs_attr3_rmt_blocks(mp
, args
->rmtvaluelen
);
459 error
= xfs_bmap_first_unused(args
->trans
, args
->dp
, blkcnt
, &lfileoff
,
464 args
->rmtblkno
= lblkno
= (xfs_dablk_t
)lfileoff
;
465 args
->rmtblkcnt
= blkcnt
;
468 * Roll through the "value", allocating blocks on disk as required.
472 * Allocate a single extent, up to the size of the value.
474 * Note that we have to consider this a data allocation as we
475 * write the remote attribute without logging the contents.
476 * Hence we must ensure that we aren't using blocks that are on
477 * the busy list so that we don't overwrite blocks which have
478 * recently been freed but their transactions are not yet
479 * committed to disk. If we overwrite the contents of a busy
480 * extent and then crash then the block may not contain the
481 * correct metadata after log recovery occurs.
484 error
= xfs_bmapi_write(args
->trans
, dp
, (xfs_fileoff_t
)lblkno
,
485 blkcnt
, XFS_BMAPI_ATTRFORK
, args
->total
, &map
,
489 error
= xfs_defer_finish(&args
->trans
);
494 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
495 (map
.br_startblock
!= HOLESTARTBLOCK
));
496 lblkno
+= map
.br_blockcount
;
497 blkcnt
-= map
.br_blockcount
;
500 * Start the next trans in the chain.
502 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
508 * Roll through the "value", copying the attribute value to the
509 * already-allocated blocks. Blocks are written synchronously
510 * so that we can know they are all on disk before we turn off
511 * the INCOMPLETE flag.
513 lblkno
= args
->rmtblkno
;
514 blkcnt
= args
->rmtblkcnt
;
515 valuelen
= args
->rmtvaluelen
;
516 while (valuelen
> 0) {
524 error
= xfs_bmapi_read(dp
, (xfs_fileoff_t
)lblkno
,
530 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
531 (map
.br_startblock
!= HOLESTARTBLOCK
));
533 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
534 dblkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
536 bp
= xfs_buf_get(mp
->m_ddev_targp
, dblkno
, dblkcnt
, 0);
539 bp
->b_ops
= &xfs_attr3_rmt_buf_ops
;
541 xfs_attr_rmtval_copyin(mp
, bp
, args
->dp
->i_ino
, &offset
,
544 error
= xfs_bwrite(bp
); /* GROT: NOTE: synchronous write */
550 /* roll attribute extent map forwards */
551 lblkno
+= map
.br_blockcount
;
552 blkcnt
-= map
.br_blockcount
;
554 ASSERT(valuelen
== 0);
559 * Remove the value associated with an attribute by deleting the
560 * out-of-line buffer that it is stored on.
563 xfs_attr_rmtval_remove(
564 struct xfs_da_args
*args
)
566 struct xfs_mount
*mp
= args
->dp
->i_mount
;
572 trace_xfs_attr_rmtval_remove(args
);
575 * Roll through the "value", invalidating the attribute value's blocks.
577 lblkno
= args
->rmtblkno
;
578 blkcnt
= args
->rmtblkcnt
;
580 struct xfs_bmbt_irec map
;
587 * Try to remember where we decided to put the value.
590 error
= xfs_bmapi_read(args
->dp
, (xfs_fileoff_t
)lblkno
,
591 blkcnt
, &map
, &nmap
, XFS_BMAPI_ATTRFORK
);
595 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
596 (map
.br_startblock
!= HOLESTARTBLOCK
));
598 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
599 dblkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
602 * If the "remote" value is in the cache, remove it.
604 bp
= xfs_buf_incore(mp
->m_ddev_targp
, dblkno
, dblkcnt
, XBF_TRYLOCK
);
611 lblkno
+= map
.br_blockcount
;
612 blkcnt
-= map
.br_blockcount
;
616 * Keep de-allocating extents until the remote-value region is gone.
618 lblkno
= args
->rmtblkno
;
619 blkcnt
= args
->rmtblkcnt
;
622 error
= xfs_bunmapi(args
->trans
, args
->dp
, lblkno
, blkcnt
,
623 XFS_BMAPI_ATTRFORK
, 1, &done
);
626 error
= xfs_defer_finish(&args
->trans
);
631 * Close out trans and start the next one in the chain.
633 error
= xfs_trans_roll_inode(&args
->trans
, args
->dp
);