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_trans.h"
22 #include "xfs_attr_remote.h"
23 #include "xfs_trace.h"
24 #include "xfs_error.h"
25 #include "xfs_health.h"
27 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
30 * Remote Attribute Values
31 * =======================
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.
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.
46 /* How many bytes can be stored in a remote value buffer? */
48 xfs_attr3_rmt_buf_space(
51 unsigned int blocksize = mp->m_attr_geo->blksize;
54 return blocksize - sizeof(struct xfs_attr3_rmt_hdr);
59 /* Compute number of fsblocks needed to store a remote attr value */
66 * Each contiguous block has a header, so it is not just a simple
67 * attribute length to FSB conversion.
70 return howmany(attrlen, xfs_attr3_rmt_buf_space(mp));
72 return XFS_B_TO_FSB(mp, attrlen);
76 * Checking of the remote attribute header is split into two parts. The verifier
77 * does CRC, location and bounds checking, the unpacking function checks the
78 * attribute parameters and owner.
88 struct xfs_attr3_rmt_hdr *rmt = ptr;
90 if (bno != be64_to_cpu(rmt->rm_blkno))
91 return __this_address;
92 if (offset != be32_to_cpu(rmt->rm_offset))
93 return __this_address;
94 if (size != be32_to_cpu(rmt->rm_bytes))
95 return __this_address;
96 if (ino != be64_to_cpu(rmt->rm_owner))
97 return __this_address;
103 static xfs_failaddr_t
104 xfs_attr3_rmt_verify(
105 struct xfs_mount *mp,
110 struct xfs_attr3_rmt_hdr *rmt = ptr;
112 if (!xfs_verify_magic(bp, rmt->rm_magic))
113 return __this_address;
114 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid))
115 return __this_address;
116 if (be64_to_cpu(rmt->rm_blkno) != bno)
117 return __this_address;
118 if (be32_to_cpu(rmt->rm_bytes) > mp->m_attr_geo->blksize - sizeof(*rmt))
119 return __this_address;
120 if (be32_to_cpu(rmt->rm_offset) +
121 be32_to_cpu(rmt->rm_bytes) > XFS_XATTR_SIZE_MAX)
122 return __this_address;
123 if (rmt->rm_owner == 0)
124 return __this_address;
130 __xfs_attr3_rmt_read_verify(
133 xfs_failaddr_t *failaddr)
135 struct xfs_mount *mp = bp->b_mount;
139 unsigned int blksize = mp->m_attr_geo->blksize;
141 /* no verification of non-crc buffers */
142 if (!xfs_has_crc(mp))
146 bno = xfs_buf_daddr(bp);
147 len = BBTOB(bp->b_length);
148 ASSERT(len >= blksize);
152 !xfs_verify_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF)) {
153 *failaddr = __this_address;
156 *failaddr = xfs_attr3_rmt_verify(mp, bp, ptr, bno);
158 return -EFSCORRUPTED;
161 bno += BTOBB(blksize);
165 *failaddr = __this_address;
166 return -EFSCORRUPTED;
173 xfs_attr3_rmt_read_verify(
179 error = __xfs_attr3_rmt_read_verify(bp, true, &fa);
181 xfs_verifier_error(bp, error, fa);
184 static xfs_failaddr_t
185 xfs_attr3_rmt_verify_struct(
191 error = __xfs_attr3_rmt_read_verify(bp, false, &fa);
192 return error ? fa : NULL;
196 xfs_attr3_rmt_write_verify(
199 struct xfs_mount *mp = bp->b_mount;
201 unsigned int blksize = mp->m_attr_geo->blksize;
206 /* no verification of non-crc buffers */
207 if (!xfs_has_crc(mp))
211 bno = xfs_buf_daddr(bp);
212 len = BBTOB(bp->b_length);
213 ASSERT(len >= blksize);
216 struct xfs_attr3_rmt_hdr *rmt = (struct xfs_attr3_rmt_hdr *)ptr;
218 fa = xfs_attr3_rmt_verify(mp, bp, ptr, bno);
220 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
225 * Ensure we aren't writing bogus LSNs to disk. See
226 * xfs_attr3_rmt_hdr_set() for the explanation.
228 if (rmt->rm_lsn != cpu_to_be64(NULLCOMMITLSN)) {
229 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
232 xfs_update_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF);
236 bno += BTOBB(blksize);
240 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
243 const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
244 .name = "xfs_attr3_rmt",
245 .magic = { 0, cpu_to_be32(XFS_ATTR3_RMT_MAGIC) },
246 .verify_read = xfs_attr3_rmt_read_verify,
247 .verify_write = xfs_attr3_rmt_write_verify,
248 .verify_struct = xfs_attr3_rmt_verify_struct,
252 xfs_attr3_rmt_hdr_set(
253 struct xfs_mount *mp,
260 struct xfs_attr3_rmt_hdr *rmt = ptr;
262 if (!xfs_has_crc(mp))
265 rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC);
266 rmt->rm_offset = cpu_to_be32(offset);
267 rmt->rm_bytes = cpu_to_be32(size);
268 uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid);
269 rmt->rm_owner = cpu_to_be64(ino);
270 rmt->rm_blkno = cpu_to_be64(bno);
273 * Remote attribute blocks are written synchronously, so we don't
274 * have an LSN that we can stamp in them that makes any sense to log
275 * recovery. To ensure that log recovery handles overwrites of these
276 * blocks sanely (i.e. once they've been freed and reallocated as some
277 * other type of metadata) we need to ensure that the LSN has a value
278 * that tells log recovery to ignore the LSN and overwrite the buffer
279 * with whatever is in it's log. To do this, we use the magic
280 * NULLCOMMITLSN to indicate that the LSN is invalid.
282 rmt->rm_lsn = cpu_to_be64(NULLCOMMITLSN);
284 return sizeof(struct xfs_attr3_rmt_hdr);
288 * Helper functions to copy attribute data in and out of the one disk extents
291 xfs_attr_rmtval_copyout(
292 struct xfs_mount *mp,
294 struct xfs_inode *dp,
296 unsigned int *offset,
297 unsigned int *valuelen,
300 char *src = bp->b_addr;
301 xfs_daddr_t bno = xfs_buf_daddr(bp);
302 unsigned int len = BBTOB(bp->b_length);
303 unsigned int blksize = mp->m_attr_geo->blksize;
305 ASSERT(len >= blksize);
307 while (len > 0 && *valuelen > 0) {
308 unsigned int hdr_size = 0;
309 unsigned int byte_cnt = xfs_attr3_rmt_buf_space(mp);
311 byte_cnt = min(*valuelen, byte_cnt);
313 if (xfs_has_crc(mp)) {
314 if (xfs_attr3_rmt_hdr_ok(src, owner, *offset,
317 "remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)",
318 bno, *offset, byte_cnt, owner);
319 xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
320 return -EFSCORRUPTED;
322 hdr_size = sizeof(struct xfs_attr3_rmt_hdr);
325 memcpy(*dst, src + hdr_size, byte_cnt);
327 /* roll buffer forwards */
330 bno += BTOBB(blksize);
332 /* roll attribute data forwards */
333 *valuelen -= byte_cnt;
341 xfs_attr_rmtval_copyin(
342 struct xfs_mount *mp,
345 unsigned int *offset,
346 unsigned int *valuelen,
349 char *dst = bp->b_addr;
350 xfs_daddr_t bno = xfs_buf_daddr(bp);
351 unsigned int len = BBTOB(bp->b_length);
352 unsigned int blksize = mp->m_attr_geo->blksize;
354 ASSERT(len >= blksize);
356 while (len > 0 && *valuelen > 0) {
357 unsigned int hdr_size;
358 unsigned int byte_cnt = xfs_attr3_rmt_buf_space(mp);
360 byte_cnt = min(*valuelen, byte_cnt);
361 hdr_size = xfs_attr3_rmt_hdr_set(mp, dst, ino, *offset,
364 memcpy(dst + hdr_size, *src, byte_cnt);
367 * If this is the last block, zero the remainder of it.
368 * Check that we are actually the last block, too.
370 if (byte_cnt + hdr_size < blksize) {
371 ASSERT(*valuelen - byte_cnt == 0);
372 ASSERT(len == blksize);
373 memset(dst + hdr_size + byte_cnt, 0,
374 blksize - hdr_size - byte_cnt);
377 /* roll buffer forwards */
380 bno += BTOBB(blksize);
382 /* roll attribute data forwards */
383 *valuelen -= byte_cnt;
390 * Read the value associated with an attribute from the out-of-line buffer
391 * that we stored it in.
393 * Returns 0 on successful retrieval, otherwise an error.
397 struct xfs_da_args *args)
399 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
400 struct xfs_mount *mp = args->dp->i_mount;
402 xfs_dablk_t lblkno = args->rmtblkno;
403 uint8_t *dst = args->value;
404 unsigned int valuelen;
407 unsigned int blkcnt = args->rmtblkcnt;
409 unsigned int offset = 0;
411 trace_xfs_attr_rmtval_get(args);
413 ASSERT(args->valuelen != 0);
414 ASSERT(args->rmtvaluelen == args->valuelen);
416 valuelen = args->rmtvaluelen;
417 while (valuelen > 0) {
418 nmap = ATTR_RMTVALUE_MAPSIZE;
419 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
426 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
430 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
431 (map[i].br_startblock != HOLESTARTBLOCK));
432 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
433 dblkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
434 error = xfs_buf_read(mp->m_ddev_targp, dblkno, dblkcnt,
435 0, &bp, &xfs_attr3_rmt_buf_ops);
436 if (xfs_metadata_is_sick(error))
437 xfs_dirattr_mark_sick(args->dp, XFS_ATTR_FORK);
441 error = xfs_attr_rmtval_copyout(mp, bp, args->dp,
442 args->owner, &offset, &valuelen, &dst);
447 /* roll attribute extent map forwards */
448 lblkno += map[i].br_blockcount;
449 blkcnt -= map[i].br_blockcount;
452 ASSERT(valuelen == 0);
457 * Find a "hole" in the attribute address space large enough for us to drop the
458 * new attributes value into
461 xfs_attr_rmt_find_hole(
462 struct xfs_da_args *args)
464 struct xfs_inode *dp = args->dp;
465 struct xfs_mount *mp = dp->i_mount;
468 xfs_fileoff_t lfileoff = 0;
471 * Because CRC enable attributes have headers, we can't just do a
472 * straight byte to FSB conversion and have to take the header space
475 blkcnt = xfs_attr3_rmt_blocks(mp, args->rmtvaluelen);
476 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
481 args->rmtblkno = (xfs_dablk_t)lfileoff;
482 args->rmtblkcnt = blkcnt;
488 xfs_attr_rmtval_set_value(
489 struct xfs_da_args *args)
491 struct xfs_inode *dp = args->dp;
492 struct xfs_mount *mp = dp->i_mount;
493 struct xfs_bmbt_irec map;
495 uint8_t *src = args->value;
497 unsigned int valuelen;
500 unsigned int offset = 0;
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 error = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, &bp);
534 bp->b_ops = &xfs_attr3_rmt_buf_ops;
536 xfs_attr_rmtval_copyin(mp, bp, args->owner, &offset, &valuelen,
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);
553 /* Mark stale any incore buffers for the remote value. */
555 xfs_attr_rmtval_stale(
556 struct xfs_inode *ip,
557 struct xfs_bmbt_irec *map,
558 xfs_buf_flags_t incore_flags)
560 struct xfs_mount *mp = ip->i_mount;
564 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
566 if (XFS_IS_CORRUPT(mp, map->br_startblock == DELAYSTARTBLOCK) ||
567 XFS_IS_CORRUPT(mp, map->br_startblock == HOLESTARTBLOCK)) {
568 xfs_bmap_mark_sick(ip, XFS_ATTR_FORK);
569 return -EFSCORRUPTED;
572 error = xfs_buf_incore(mp->m_ddev_targp,
573 XFS_FSB_TO_DADDR(mp, map->br_startblock),
574 XFS_FSB_TO_BB(mp, map->br_blockcount),
577 if (error == -ENOENT)
588 * Find a hole for the attr and store it in the delayed attr context. This
589 * initializes the context to roll through allocating an attr extent for a
590 * delayed attr operation
593 xfs_attr_rmtval_find_space(
594 struct xfs_attr_intent *attr)
596 struct xfs_da_args *args = attr->xattri_da_args;
597 struct xfs_bmbt_irec *map = &attr->xattri_map;
600 attr->xattri_lblkno = 0;
601 attr->xattri_blkcnt = 0;
604 memset(map, 0, sizeof(struct xfs_bmbt_irec));
606 error = xfs_attr_rmt_find_hole(args);
610 attr->xattri_blkcnt = args->rmtblkcnt;
611 attr->xattri_lblkno = args->rmtblkno;
617 * Write one block of the value associated with an attribute into the
618 * out-of-line buffer that we have defined for it. This is similar to a subset
619 * of xfs_attr_rmtval_set, but records the current block to the delayed attr
620 * context, and leaves transaction handling to the caller.
623 xfs_attr_rmtval_set_blk(
624 struct xfs_attr_intent *attr)
626 struct xfs_da_args *args = attr->xattri_da_args;
627 struct xfs_inode *dp = args->dp;
628 struct xfs_bmbt_irec *map = &attr->xattri_map;
633 error = xfs_bmapi_write(args->trans, dp,
634 (xfs_fileoff_t)attr->xattri_lblkno,
635 attr->xattri_blkcnt, XFS_BMAPI_ATTRFORK, args->total,
640 ASSERT((map->br_startblock != DELAYSTARTBLOCK) &&
641 (map->br_startblock != HOLESTARTBLOCK));
643 /* roll attribute extent map forwards */
644 attr->xattri_lblkno += map->br_blockcount;
645 attr->xattri_blkcnt -= map->br_blockcount;
651 * Remove the value associated with an attribute by deleting the
652 * out-of-line buffer that it is stored on.
655 xfs_attr_rmtval_invalidate(
656 struct xfs_da_args *args)
663 * Roll through the "value", invalidating the attribute value's blocks.
665 lblkno = args->rmtblkno;
666 blkcnt = args->rmtblkcnt;
668 struct xfs_bmbt_irec map;
672 * Try to remember where we decided to put the value.
675 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
676 blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
679 if (XFS_IS_CORRUPT(args->dp->i_mount, nmap != 1)) {
680 xfs_bmap_mark_sick(args->dp, XFS_ATTR_FORK);
681 return -EFSCORRUPTED;
683 error = xfs_attr_rmtval_stale(args->dp, &map, XBF_TRYLOCK);
687 lblkno += map.br_blockcount;
688 blkcnt -= map.br_blockcount;
694 * Remove the value associated with an attribute by deleting the out-of-line
695 * buffer that it is stored on. Returns -EAGAIN for the caller to refresh the
696 * transaction and re-call the function. Callers should keep calling this
697 * routine until it returns something other than -EAGAIN.
700 xfs_attr_rmtval_remove(
701 struct xfs_attr_intent *attr)
703 struct xfs_da_args *args = attr->xattri_da_args;
707 * Unmap value blocks for this attr.
709 error = xfs_bunmapi(args->trans, args->dp, args->rmtblkno,
710 args->rmtblkcnt, XFS_BMAPI_ATTRFORK, 1, &done);
715 * We don't need an explicit state here to pick up where we left off. We
716 * can figure it out using the !done return code. The actual value of
717 * attr->xattri_dela_state may be some value reminiscent of the calling
718 * function, but it's value is irrelevant with in the context of this
719 * function. Once we are done here, the next state is set as needed by
723 trace_xfs_attr_rmtval_remove_return(attr->xattri_dela_state,