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_trace.h"
23 #include "xfs_error.h"
25 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
28 * Each contiguous block has a header, so it is not just a simple attribute
29 * length to FSB conversion.
36 if (xfs_sb_version_hascrc(&mp->m_sb)) {
37 int buflen = XFS_ATTR3_RMT_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
38 return (attrlen + buflen - 1) / buflen;
40 return XFS_B_TO_FSB(mp, attrlen);
44 * Checking of the remote attribute header is split into two parts. The verifier
45 * does CRC, location and bounds checking, the unpacking function checks the
46 * attribute parameters and owner.
56 struct xfs_attr3_rmt_hdr *rmt = ptr;
58 if (bno != be64_to_cpu(rmt->rm_blkno))
59 return __this_address;
60 if (offset != be32_to_cpu(rmt->rm_offset))
61 return __this_address;
62 if (size != be32_to_cpu(rmt->rm_bytes))
63 return __this_address;
64 if (ino != be64_to_cpu(rmt->rm_owner))
65 return __this_address;
79 struct xfs_attr3_rmt_hdr *rmt = ptr;
81 if (!xfs_sb_version_hascrc(&mp->m_sb))
82 return __this_address;
83 if (!xfs_verify_magic(bp, rmt->rm_magic))
84 return __this_address;
85 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid))
86 return __this_address;
87 if (be64_to_cpu(rmt->rm_blkno) != bno)
88 return __this_address;
89 if (be32_to_cpu(rmt->rm_bytes) > fsbsize - sizeof(*rmt))
90 return __this_address;
91 if (be32_to_cpu(rmt->rm_offset) +
92 be32_to_cpu(rmt->rm_bytes) > XFS_XATTR_SIZE_MAX)
93 return __this_address;
94 if (rmt->rm_owner == 0)
95 return __this_address;
101 __xfs_attr3_rmt_read_verify(
104 xfs_failaddr_t *failaddr)
106 struct xfs_mount *mp = bp->b_mount;
110 int blksize = mp->m_attr_geo->blksize;
112 /* no verification of non-crc buffers */
113 if (!xfs_sb_version_hascrc(&mp->m_sb))
118 len = BBTOB(bp->b_length);
119 ASSERT(len >= blksize);
123 !xfs_verify_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF)) {
124 *failaddr = __this_address;
127 *failaddr = xfs_attr3_rmt_verify(mp, bp, ptr, blksize, bno);
129 return -EFSCORRUPTED;
132 bno += BTOBB(blksize);
136 *failaddr = __this_address;
137 return -EFSCORRUPTED;
144 xfs_attr3_rmt_read_verify(
150 error = __xfs_attr3_rmt_read_verify(bp, true, &fa);
152 xfs_verifier_error(bp, error, fa);
155 static xfs_failaddr_t
156 xfs_attr3_rmt_verify_struct(
162 error = __xfs_attr3_rmt_read_verify(bp, false, &fa);
163 return error ? fa : NULL;
167 xfs_attr3_rmt_write_verify(
170 struct xfs_mount *mp = bp->b_mount;
172 int blksize = mp->m_attr_geo->blksize;
177 /* no verification of non-crc buffers */
178 if (!xfs_sb_version_hascrc(&mp->m_sb))
183 len = BBTOB(bp->b_length);
184 ASSERT(len >= blksize);
187 struct xfs_attr3_rmt_hdr *rmt = (struct xfs_attr3_rmt_hdr *)ptr;
189 fa = xfs_attr3_rmt_verify(mp, bp, ptr, blksize, bno);
191 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
196 * Ensure we aren't writing bogus LSNs to disk. See
197 * xfs_attr3_rmt_hdr_set() for the explanation.
199 if (rmt->rm_lsn != cpu_to_be64(NULLCOMMITLSN)) {
200 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
203 xfs_update_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF);
207 bno += BTOBB(blksize);
211 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
214 const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
215 .name = "xfs_attr3_rmt",
216 .magic = { 0, cpu_to_be32(XFS_ATTR3_RMT_MAGIC) },
217 .verify_read = xfs_attr3_rmt_read_verify,
218 .verify_write = xfs_attr3_rmt_write_verify,
219 .verify_struct = xfs_attr3_rmt_verify_struct,
223 xfs_attr3_rmt_hdr_set(
224 struct xfs_mount *mp,
231 struct xfs_attr3_rmt_hdr *rmt = ptr;
233 if (!xfs_sb_version_hascrc(&mp->m_sb))
236 rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC);
237 rmt->rm_offset = cpu_to_be32(offset);
238 rmt->rm_bytes = cpu_to_be32(size);
239 uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid);
240 rmt->rm_owner = cpu_to_be64(ino);
241 rmt->rm_blkno = cpu_to_be64(bno);
244 * Remote attribute blocks are written synchronously, so we don't
245 * have an LSN that we can stamp in them that makes any sense to log
246 * recovery. To ensure that log recovery handles overwrites of these
247 * blocks sanely (i.e. once they've been freed and reallocated as some
248 * other type of metadata) we need to ensure that the LSN has a value
249 * that tells log recovery to ignore the LSN and overwrite the buffer
250 * with whatever is in it's log. To do this, we use the magic
251 * NULLCOMMITLSN to indicate that the LSN is invalid.
253 rmt->rm_lsn = cpu_to_be64(NULLCOMMITLSN);
255 return sizeof(struct xfs_attr3_rmt_hdr);
259 * Helper functions to copy attribute data in and out of the one disk extents
262 xfs_attr_rmtval_copyout(
263 struct xfs_mount *mp,
270 char *src = bp->b_addr;
271 xfs_daddr_t bno = bp->b_bn;
272 int len = BBTOB(bp->b_length);
273 int blksize = mp->m_attr_geo->blksize;
275 ASSERT(len >= blksize);
277 while (len > 0 && *valuelen > 0) {
279 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize);
281 byte_cnt = min(*valuelen, byte_cnt);
283 if (xfs_sb_version_hascrc(&mp->m_sb)) {
284 if (xfs_attr3_rmt_hdr_ok(src, ino, *offset,
287 "remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)",
288 bno, *offset, byte_cnt, ino);
289 return -EFSCORRUPTED;
291 hdr_size = sizeof(struct xfs_attr3_rmt_hdr);
294 memcpy(*dst, src + hdr_size, byte_cnt);
296 /* roll buffer forwards */
299 bno += BTOBB(blksize);
301 /* roll attribute data forwards */
302 *valuelen -= byte_cnt;
310 xfs_attr_rmtval_copyin(
311 struct xfs_mount *mp,
318 char *dst = bp->b_addr;
319 xfs_daddr_t bno = bp->b_bn;
320 int len = BBTOB(bp->b_length);
321 int blksize = mp->m_attr_geo->blksize;
323 ASSERT(len >= blksize);
325 while (len > 0 && *valuelen > 0) {
327 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize);
329 byte_cnt = min(*valuelen, byte_cnt);
330 hdr_size = xfs_attr3_rmt_hdr_set(mp, dst, ino, *offset,
333 memcpy(dst + hdr_size, *src, byte_cnt);
336 * If this is the last block, zero the remainder of it.
337 * Check that we are actually the last block, too.
339 if (byte_cnt + hdr_size < blksize) {
340 ASSERT(*valuelen - byte_cnt == 0);
341 ASSERT(len == blksize);
342 memset(dst + hdr_size + byte_cnt, 0,
343 blksize - hdr_size - byte_cnt);
346 /* roll buffer forwards */
349 bno += BTOBB(blksize);
351 /* roll attribute data forwards */
352 *valuelen -= byte_cnt;
359 * Read the value associated with an attribute from the out-of-line buffer
360 * that we stored it in.
364 struct xfs_da_args *args)
366 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
367 struct xfs_mount *mp = args->dp->i_mount;
369 xfs_dablk_t lblkno = args->rmtblkno;
370 uint8_t *dst = args->value;
374 int blkcnt = args->rmtblkcnt;
378 trace_xfs_attr_rmtval_get(args);
380 ASSERT(!(args->flags & ATTR_KERNOVAL));
381 ASSERT(args->rmtvaluelen == args->valuelen);
383 valuelen = args->rmtvaluelen;
384 while (valuelen > 0) {
385 nmap = ATTR_RMTVALUE_MAPSIZE;
386 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
393 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
397 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
398 (map[i].br_startblock != HOLESTARTBLOCK));
399 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
400 dblkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
401 error = xfs_trans_read_buf(mp, args->trans,
403 dblkno, dblkcnt, 0, &bp,
404 &xfs_attr3_rmt_buf_ops);
408 error = xfs_attr_rmtval_copyout(mp, bp, args->dp->i_ino,
411 xfs_trans_brelse(args->trans, bp);
415 /* roll attribute extent map forwards */
416 lblkno += map[i].br_blockcount;
417 blkcnt -= map[i].br_blockcount;
420 ASSERT(valuelen == 0);
425 * Write the value associated with an attribute into the out-of-line buffer
426 * that we have defined for it.
430 struct xfs_da_args *args)
432 struct xfs_inode *dp = args->dp;
433 struct xfs_mount *mp = dp->i_mount;
434 struct xfs_bmbt_irec map;
436 xfs_fileoff_t lfileoff = 0;
437 uint8_t *src = args->value;
444 trace_xfs_attr_rmtval_set(args);
447 * Find a "hole" in the attribute address space large enough for
448 * us to drop the new attribute's value into. Because CRC enable
449 * attributes have headers, we can't just do a straight byte to FSB
450 * conversion and have to take the header space into account.
452 blkcnt = xfs_attr3_rmt_blocks(mp, args->rmtvaluelen);
453 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
458 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
459 args->rmtblkcnt = blkcnt;
462 * Roll through the "value", allocating blocks on disk as required.
466 * Allocate a single extent, up to the size of the value.
468 * Note that we have to consider this a data allocation as we
469 * write the remote attribute without logging the contents.
470 * Hence we must ensure that we aren't using blocks that are on
471 * the busy list so that we don't overwrite blocks which have
472 * recently been freed but their transactions are not yet
473 * committed to disk. If we overwrite the contents of a busy
474 * extent and then crash then the block may not contain the
475 * correct metadata after log recovery occurs.
478 error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
479 blkcnt, XFS_BMAPI_ATTRFORK, args->total, &map,
483 error = xfs_defer_finish(&args->trans);
488 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
489 (map.br_startblock != HOLESTARTBLOCK));
490 lblkno += map.br_blockcount;
491 blkcnt -= map.br_blockcount;
494 * Start the next trans in the chain.
496 error = xfs_trans_roll_inode(&args->trans, dp);
502 * Roll through the "value", copying the attribute value to the
503 * already-allocated blocks. Blocks are written synchronously
504 * so that we can know they are all on disk before we turn off
505 * the INCOMPLETE flag.
507 lblkno = args->rmtblkno;
508 blkcnt = args->rmtblkcnt;
509 valuelen = args->rmtvaluelen;
510 while (valuelen > 0) {
518 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
524 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
525 (map.br_startblock != HOLESTARTBLOCK));
527 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
528 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
530 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt);
533 bp->b_ops = &xfs_attr3_rmt_buf_ops;
535 xfs_attr_rmtval_copyin(mp, bp, args->dp->i_ino, &offset,
538 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
544 /* roll attribute extent map forwards */
545 lblkno += map.br_blockcount;
546 blkcnt -= map.br_blockcount;
548 ASSERT(valuelen == 0);
553 * Remove the value associated with an attribute by deleting the
554 * out-of-line buffer that it is stored on.
557 xfs_attr_rmtval_remove(
558 struct xfs_da_args *args)
560 struct xfs_mount *mp = args->dp->i_mount;
566 trace_xfs_attr_rmtval_remove(args);
569 * Roll through the "value", invalidating the attribute value's blocks.
571 lblkno = args->rmtblkno;
572 blkcnt = args->rmtblkcnt;
574 struct xfs_bmbt_irec map;
581 * Try to remember where we decided to put the value.
584 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
585 blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
589 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
590 (map.br_startblock != HOLESTARTBLOCK));
592 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
593 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
596 * If the "remote" value is in the cache, remove it.
598 bp = xfs_buf_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK);
605 lblkno += map.br_blockcount;
606 blkcnt -= map.br_blockcount;
610 * Keep de-allocating extents until the remote-value region is gone.
612 lblkno = args->rmtblkno;
613 blkcnt = args->rmtblkcnt;
616 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
617 XFS_BMAPI_ATTRFORK, 1, &done);
620 error = xfs_defer_finish(&args->trans);
625 * Close out trans and start the next one in the chain.
627 error = xfs_trans_roll_inode(&args->trans, args->dp);