2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_shared.h"
22 #include "xfs_format.h"
23 #include "xfs_log_format.h"
24 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_da_format.h"
28 #include "xfs_da_btree.h"
29 #include "xfs_inode.h"
30 #include "xfs_alloc.h"
31 #include "xfs_trans.h"
32 #include "xfs_inode_item.h"
34 #include "xfs_bmap_util.h"
36 #include "xfs_attr_leaf.h"
37 #include "xfs_attr_remote.h"
38 #include "xfs_trans_space.h"
39 #include "xfs_trace.h"
40 #include "xfs_cksum.h"
41 #include "xfs_buf_item.h"
42 #include "xfs_error.h"
44 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
47 * Each contiguous block has a header, so it is not just a simple attribute
48 * length to FSB conversion.
55 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
56 int buflen
= XFS_ATTR3_RMT_BUF_SPACE(mp
, mp
->m_sb
.sb_blocksize
);
57 return (attrlen
+ buflen
- 1) / buflen
;
59 return XFS_B_TO_FSB(mp
, attrlen
);
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.
75 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
77 if (bno
!= be64_to_cpu(rmt
->rm_blkno
))
79 if (offset
!= be32_to_cpu(rmt
->rm_offset
))
81 if (size
!= be32_to_cpu(rmt
->rm_bytes
))
83 if (ino
!= be64_to_cpu(rmt
->rm_owner
))
97 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
99 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
101 if (rmt
->rm_magic
!= cpu_to_be32(XFS_ATTR3_RMT_MAGIC
))
103 if (!uuid_equal(&rmt
->rm_uuid
, &mp
->m_sb
.sb_meta_uuid
))
105 if (be64_to_cpu(rmt
->rm_blkno
) != bno
)
107 if (be32_to_cpu(rmt
->rm_bytes
) > fsbsize
- sizeof(*rmt
))
109 if (be32_to_cpu(rmt
->rm_offset
) +
110 be32_to_cpu(rmt
->rm_bytes
) > XFS_XATTR_SIZE_MAX
)
112 if (rmt
->rm_owner
== 0)
119 xfs_attr3_rmt_read_verify(
122 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
126 int blksize
= mp
->m_attr_geo
->blksize
;
128 /* no verification of non-crc buffers */
129 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
134 len
= BBTOB(bp
->b_length
);
135 ASSERT(len
>= blksize
);
138 if (!xfs_verify_cksum(ptr
, blksize
, XFS_ATTR3_RMT_CRC_OFF
)) {
139 xfs_buf_ioerror(bp
, -EFSBADCRC
);
142 if (!xfs_attr3_rmt_verify(mp
, ptr
, blksize
, bno
)) {
143 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
148 bno
+= BTOBB(blksize
);
152 xfs_verifier_error(bp
);
158 xfs_attr3_rmt_write_verify(
161 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
162 int blksize
= mp
->m_attr_geo
->blksize
;
167 /* no verification of non-crc buffers */
168 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
173 len
= BBTOB(bp
->b_length
);
174 ASSERT(len
>= blksize
);
177 struct xfs_attr3_rmt_hdr
*rmt
= (struct xfs_attr3_rmt_hdr
*)ptr
;
179 if (!xfs_attr3_rmt_verify(mp
, ptr
, blksize
, bno
)) {
180 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
181 xfs_verifier_error(bp
);
186 * Ensure we aren't writing bogus LSNs to disk. See
187 * xfs_attr3_rmt_hdr_set() for the explanation.
189 if (rmt
->rm_lsn
!= cpu_to_be64(NULLCOMMITLSN
)) {
190 xfs_buf_ioerror(bp
, -EFSCORRUPTED
);
191 xfs_verifier_error(bp
);
194 xfs_update_cksum(ptr
, blksize
, XFS_ATTR3_RMT_CRC_OFF
);
198 bno
+= BTOBB(blksize
);
203 const struct xfs_buf_ops xfs_attr3_rmt_buf_ops
= {
204 .verify_read
= xfs_attr3_rmt_read_verify
,
205 .verify_write
= xfs_attr3_rmt_write_verify
,
209 xfs_attr3_rmt_hdr_set(
210 struct xfs_mount
*mp
,
217 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
219 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
222 rmt
->rm_magic
= cpu_to_be32(XFS_ATTR3_RMT_MAGIC
);
223 rmt
->rm_offset
= cpu_to_be32(offset
);
224 rmt
->rm_bytes
= cpu_to_be32(size
);
225 uuid_copy(&rmt
->rm_uuid
, &mp
->m_sb
.sb_meta_uuid
);
226 rmt
->rm_owner
= cpu_to_be64(ino
);
227 rmt
->rm_blkno
= cpu_to_be64(bno
);
230 * Remote attribute blocks are written synchronously, so we don't
231 * have an LSN that we can stamp in them that makes any sense to log
232 * recovery. To ensure that log recovery handles overwrites of these
233 * blocks sanely (i.e. once they've been freed and reallocated as some
234 * other type of metadata) we need to ensure that the LSN has a value
235 * that tells log recovery to ignore the LSN and overwrite the buffer
236 * with whatever is in it's log. To do this, we use the magic
237 * NULLCOMMITLSN to indicate that the LSN is invalid.
239 rmt
->rm_lsn
= cpu_to_be64(NULLCOMMITLSN
);
241 return sizeof(struct xfs_attr3_rmt_hdr
);
245 * Helper functions to copy attribute data in and out of the one disk extents
248 xfs_attr_rmtval_copyout(
249 struct xfs_mount
*mp
,
256 char *src
= bp
->b_addr
;
257 xfs_daddr_t bno
= bp
->b_bn
;
258 int len
= BBTOB(bp
->b_length
);
259 int blksize
= mp
->m_attr_geo
->blksize
;
261 ASSERT(len
>= blksize
);
263 while (len
> 0 && *valuelen
> 0) {
265 int byte_cnt
= XFS_ATTR3_RMT_BUF_SPACE(mp
, blksize
);
267 byte_cnt
= min(*valuelen
, byte_cnt
);
269 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
270 if (!xfs_attr3_rmt_hdr_ok(src
, ino
, *offset
,
273 "remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)",
274 bno
, *offset
, byte_cnt
, ino
);
275 return -EFSCORRUPTED
;
277 hdr_size
= sizeof(struct xfs_attr3_rmt_hdr
);
280 memcpy(*dst
, src
+ hdr_size
, byte_cnt
);
282 /* roll buffer forwards */
285 bno
+= BTOBB(blksize
);
287 /* roll attribute data forwards */
288 *valuelen
-= byte_cnt
;
296 xfs_attr_rmtval_copyin(
297 struct xfs_mount
*mp
,
304 char *dst
= bp
->b_addr
;
305 xfs_daddr_t bno
= bp
->b_bn
;
306 int len
= BBTOB(bp
->b_length
);
307 int blksize
= mp
->m_attr_geo
->blksize
;
309 ASSERT(len
>= blksize
);
311 while (len
> 0 && *valuelen
> 0) {
313 int byte_cnt
= XFS_ATTR3_RMT_BUF_SPACE(mp
, blksize
);
315 byte_cnt
= min(*valuelen
, byte_cnt
);
316 hdr_size
= xfs_attr3_rmt_hdr_set(mp
, dst
, ino
, *offset
,
319 memcpy(dst
+ hdr_size
, *src
, byte_cnt
);
322 * If this is the last block, zero the remainder of it.
323 * Check that we are actually the last block, too.
325 if (byte_cnt
+ hdr_size
< blksize
) {
326 ASSERT(*valuelen
- byte_cnt
== 0);
327 ASSERT(len
== blksize
);
328 memset(dst
+ hdr_size
+ byte_cnt
, 0,
329 blksize
- hdr_size
- byte_cnt
);
332 /* roll buffer forwards */
335 bno
+= BTOBB(blksize
);
337 /* roll attribute data forwards */
338 *valuelen
-= byte_cnt
;
345 * Read the value associated with an attribute from the out-of-line buffer
346 * that we stored it in.
350 struct xfs_da_args
*args
)
352 struct xfs_bmbt_irec map
[ATTR_RMTVALUE_MAPSIZE
];
353 struct xfs_mount
*mp
= args
->dp
->i_mount
;
355 xfs_dablk_t lblkno
= args
->rmtblkno
;
356 __uint8_t
*dst
= args
->value
;
360 int blkcnt
= args
->rmtblkcnt
;
364 trace_xfs_attr_rmtval_get(args
);
366 ASSERT(!(args
->flags
& ATTR_KERNOVAL
));
367 ASSERT(args
->rmtvaluelen
== args
->valuelen
);
369 valuelen
= args
->rmtvaluelen
;
370 while (valuelen
> 0) {
371 nmap
= ATTR_RMTVALUE_MAPSIZE
;
372 error
= xfs_bmapi_read(args
->dp
, (xfs_fileoff_t
)lblkno
,
379 for (i
= 0; (i
< nmap
) && (valuelen
> 0); i
++) {
383 ASSERT((map
[i
].br_startblock
!= DELAYSTARTBLOCK
) &&
384 (map
[i
].br_startblock
!= HOLESTARTBLOCK
));
385 dblkno
= XFS_FSB_TO_DADDR(mp
, map
[i
].br_startblock
);
386 dblkcnt
= XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
387 error
= xfs_trans_read_buf(mp
, NULL
, mp
->m_ddev_targp
,
388 dblkno
, dblkcnt
, 0, &bp
,
389 &xfs_attr3_rmt_buf_ops
);
393 error
= xfs_attr_rmtval_copyout(mp
, bp
, args
->dp
->i_ino
,
400 /* roll attribute extent map forwards */
401 lblkno
+= map
[i
].br_blockcount
;
402 blkcnt
-= map
[i
].br_blockcount
;
405 ASSERT(valuelen
== 0);
410 * Write the value associated with an attribute into the out-of-line buffer
411 * that we have defined for it.
415 struct xfs_da_args
*args
)
417 struct xfs_inode
*dp
= args
->dp
;
418 struct xfs_mount
*mp
= dp
->i_mount
;
419 struct xfs_bmbt_irec map
;
421 xfs_fileoff_t lfileoff
= 0;
422 __uint8_t
*src
= args
->value
;
429 trace_xfs_attr_rmtval_set(args
);
432 * Find a "hole" in the attribute address space large enough for
433 * us to drop the new attribute's value into. Because CRC enable
434 * attributes have headers, we can't just do a straight byte to FSB
435 * conversion and have to take the header space into account.
437 blkcnt
= xfs_attr3_rmt_blocks(mp
, args
->rmtvaluelen
);
438 error
= xfs_bmap_first_unused(args
->trans
, args
->dp
, blkcnt
, &lfileoff
,
443 args
->rmtblkno
= lblkno
= (xfs_dablk_t
)lfileoff
;
444 args
->rmtblkcnt
= blkcnt
;
447 * Roll through the "value", allocating blocks on disk as required.
453 * Allocate a single extent, up to the size of the value.
455 * Note that we have to consider this a data allocation as we
456 * write the remote attribute without logging the contents.
457 * Hence we must ensure that we aren't using blocks that are on
458 * the busy list so that we don't overwrite blocks which have
459 * recently been freed but their transactions are not yet
460 * committed to disk. If we overwrite the contents of a busy
461 * extent and then crash then the block may not contain the
462 * correct metadata after log recovery occurs.
464 xfs_bmap_init(args
->flist
, args
->firstblock
);
466 error
= xfs_bmapi_write(args
->trans
, dp
, (xfs_fileoff_t
)lblkno
,
467 blkcnt
, XFS_BMAPI_ATTRFORK
, args
->firstblock
,
468 args
->total
, &map
, &nmap
, args
->flist
);
470 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
476 xfs_bmap_cancel(args
->flist
);
481 * bmap_finish() may have committed the last trans and started
482 * a new one. We need the inode to be in all transactions.
485 xfs_trans_ijoin(args
->trans
, dp
, 0);
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(&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) {
517 xfs_bmap_init(args
->flist
, args
->firstblock
);
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_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
;
619 xfs_bmap_init(args
->flist
, args
->firstblock
);
620 error
= xfs_bunmapi(args
->trans
, args
->dp
, lblkno
, blkcnt
,
621 XFS_BMAPI_ATTRFORK
, 1, args
->firstblock
,
624 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
630 xfs_bmap_cancel(args
->flist
);
635 * bmap_finish() may have committed the last trans and started
636 * a new one. We need the inode to be in all transactions.
639 xfs_trans_ijoin(args
->trans
, args
->dp
, 0);
642 * Close out trans and start the next one in the chain.
644 error
= xfs_trans_roll(&args
->trans
, args
->dp
);