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_defer.h"
28 #include "xfs_da_format.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_inode.h"
31 #include "xfs_alloc.h"
32 #include "xfs_trans.h"
33 #include "xfs_inode_item.h"
35 #include "xfs_bmap_util.h"
37 #include "xfs_attr_leaf.h"
38 #include "xfs_attr_remote.h"
39 #include "xfs_trans_space.h"
40 #include "xfs_trace.h"
41 #include "xfs_cksum.h"
42 #include "xfs_buf_item.h"
43 #include "xfs_error.h"
45 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
48 * Each contiguous block has a header, so it is not just a simple attribute
49 * length to FSB conversion.
56 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
57 int buflen
= XFS_ATTR3_RMT_BUF_SPACE(mp
, mp
->m_sb
.sb_blocksize
);
58 return (attrlen
+ buflen
- 1) / buflen
;
60 return XFS_B_TO_FSB(mp
, attrlen
);
64 * Checking of the remote attribute header is split into two parts. The verifier
65 * does CRC, location and bounds checking, the unpacking function checks the
66 * attribute parameters and owner.
76 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
78 if (bno
!= be64_to_cpu(rmt
->rm_blkno
))
79 return __this_address
;
80 if (offset
!= be32_to_cpu(rmt
->rm_offset
))
81 return __this_address
;
82 if (size
!= be32_to_cpu(rmt
->rm_bytes
))
83 return __this_address
;
84 if (ino
!= be64_to_cpu(rmt
->rm_owner
))
85 return __this_address
;
98 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
100 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
101 return __this_address
;
102 if (rmt
->rm_magic
!= cpu_to_be32(XFS_ATTR3_RMT_MAGIC
))
103 return __this_address
;
104 if (!uuid_equal(&rmt
->rm_uuid
, &mp
->m_sb
.sb_meta_uuid
))
105 return __this_address
;
106 if (be64_to_cpu(rmt
->rm_blkno
) != bno
)
107 return __this_address
;
108 if (be32_to_cpu(rmt
->rm_bytes
) > fsbsize
- sizeof(*rmt
))
109 return __this_address
;
110 if (be32_to_cpu(rmt
->rm_offset
) +
111 be32_to_cpu(rmt
->rm_bytes
) > XFS_XATTR_SIZE_MAX
)
112 return __this_address
;
113 if (rmt
->rm_owner
== 0)
114 return __this_address
;
120 __xfs_attr3_rmt_read_verify(
123 xfs_failaddr_t
*failaddr
)
125 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
129 int blksize
= mp
->m_attr_geo
->blksize
;
131 /* no verification of non-crc buffers */
132 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
137 len
= BBTOB(bp
->b_length
);
138 ASSERT(len
>= blksize
);
142 !xfs_verify_cksum(ptr
, blksize
, XFS_ATTR3_RMT_CRC_OFF
)) {
143 *failaddr
= __this_address
;
146 *failaddr
= xfs_attr3_rmt_verify(mp
, ptr
, blksize
, bno
);
148 return -EFSCORRUPTED
;
151 bno
+= BTOBB(blksize
);
155 *failaddr
= __this_address
;
156 return -EFSCORRUPTED
;
163 xfs_attr3_rmt_read_verify(
169 error
= __xfs_attr3_rmt_read_verify(bp
, true, &fa
);
171 xfs_verifier_error(bp
, error
, fa
);
174 static xfs_failaddr_t
175 xfs_attr3_rmt_verify_struct(
181 error
= __xfs_attr3_rmt_read_verify(bp
, false, &fa
);
182 return error
? fa
: NULL
;
186 xfs_attr3_rmt_write_verify(
189 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
191 int blksize
= mp
->m_attr_geo
->blksize
;
196 /* no verification of non-crc buffers */
197 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
202 len
= BBTOB(bp
->b_length
);
203 ASSERT(len
>= blksize
);
206 struct xfs_attr3_rmt_hdr
*rmt
= (struct xfs_attr3_rmt_hdr
*)ptr
;
208 fa
= xfs_attr3_rmt_verify(mp
, ptr
, blksize
, bno
);
210 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
215 * Ensure we aren't writing bogus LSNs to disk. See
216 * xfs_attr3_rmt_hdr_set() for the explanation.
218 if (rmt
->rm_lsn
!= cpu_to_be64(NULLCOMMITLSN
)) {
219 xfs_verifier_error(bp
, -EFSCORRUPTED
, __this_address
);
222 xfs_update_cksum(ptr
, blksize
, XFS_ATTR3_RMT_CRC_OFF
);
226 bno
+= BTOBB(blksize
);
230 xfs_verifier_error(bp
, -EFSCORRUPTED
, __this_address
);
233 const struct xfs_buf_ops xfs_attr3_rmt_buf_ops
= {
234 .name
= "xfs_attr3_rmt",
235 .verify_read
= xfs_attr3_rmt_read_verify
,
236 .verify_write
= xfs_attr3_rmt_write_verify
,
237 .verify_struct
= xfs_attr3_rmt_verify_struct
,
241 xfs_attr3_rmt_hdr_set(
242 struct xfs_mount
*mp
,
249 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
251 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
254 rmt
->rm_magic
= cpu_to_be32(XFS_ATTR3_RMT_MAGIC
);
255 rmt
->rm_offset
= cpu_to_be32(offset
);
256 rmt
->rm_bytes
= cpu_to_be32(size
);
257 uuid_copy(&rmt
->rm_uuid
, &mp
->m_sb
.sb_meta_uuid
);
258 rmt
->rm_owner
= cpu_to_be64(ino
);
259 rmt
->rm_blkno
= cpu_to_be64(bno
);
262 * Remote attribute blocks are written synchronously, so we don't
263 * have an LSN that we can stamp in them that makes any sense to log
264 * recovery. To ensure that log recovery handles overwrites of these
265 * blocks sanely (i.e. once they've been freed and reallocated as some
266 * other type of metadata) we need to ensure that the LSN has a value
267 * that tells log recovery to ignore the LSN and overwrite the buffer
268 * with whatever is in it's log. To do this, we use the magic
269 * NULLCOMMITLSN to indicate that the LSN is invalid.
271 rmt
->rm_lsn
= cpu_to_be64(NULLCOMMITLSN
);
273 return sizeof(struct xfs_attr3_rmt_hdr
);
277 * Helper functions to copy attribute data in and out of the one disk extents
280 xfs_attr_rmtval_copyout(
281 struct xfs_mount
*mp
,
288 char *src
= bp
->b_addr
;
289 xfs_daddr_t bno
= bp
->b_bn
;
290 int len
= BBTOB(bp
->b_length
);
291 int blksize
= mp
->m_attr_geo
->blksize
;
293 ASSERT(len
>= blksize
);
295 while (len
> 0 && *valuelen
> 0) {
297 int byte_cnt
= XFS_ATTR3_RMT_BUF_SPACE(mp
, blksize
);
299 byte_cnt
= min(*valuelen
, byte_cnt
);
301 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
302 if (xfs_attr3_rmt_hdr_ok(src
, ino
, *offset
,
305 "remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)",
306 bno
, *offset
, byte_cnt
, ino
);
307 return -EFSCORRUPTED
;
309 hdr_size
= sizeof(struct xfs_attr3_rmt_hdr
);
312 memcpy(*dst
, src
+ hdr_size
, byte_cnt
);
314 /* roll buffer forwards */
317 bno
+= BTOBB(blksize
);
319 /* roll attribute data forwards */
320 *valuelen
-= byte_cnt
;
328 xfs_attr_rmtval_copyin(
329 struct xfs_mount
*mp
,
336 char *dst
= bp
->b_addr
;
337 xfs_daddr_t bno
= bp
->b_bn
;
338 int len
= BBTOB(bp
->b_length
);
339 int blksize
= mp
->m_attr_geo
->blksize
;
341 ASSERT(len
>= blksize
);
343 while (len
> 0 && *valuelen
> 0) {
345 int byte_cnt
= XFS_ATTR3_RMT_BUF_SPACE(mp
, blksize
);
347 byte_cnt
= min(*valuelen
, byte_cnt
);
348 hdr_size
= xfs_attr3_rmt_hdr_set(mp
, dst
, ino
, *offset
,
351 memcpy(dst
+ hdr_size
, *src
, byte_cnt
);
354 * If this is the last block, zero the remainder of it.
355 * Check that we are actually the last block, too.
357 if (byte_cnt
+ hdr_size
< blksize
) {
358 ASSERT(*valuelen
- byte_cnt
== 0);
359 ASSERT(len
== blksize
);
360 memset(dst
+ hdr_size
+ byte_cnt
, 0,
361 blksize
- hdr_size
- byte_cnt
);
364 /* roll buffer forwards */
367 bno
+= BTOBB(blksize
);
369 /* roll attribute data forwards */
370 *valuelen
-= byte_cnt
;
377 * Read the value associated with an attribute from the out-of-line buffer
378 * that we stored it in.
382 struct xfs_da_args
*args
)
384 struct xfs_bmbt_irec map
[ATTR_RMTVALUE_MAPSIZE
];
385 struct xfs_mount
*mp
= args
->dp
->i_mount
;
387 xfs_dablk_t lblkno
= args
->rmtblkno
;
388 uint8_t *dst
= args
->value
;
392 int blkcnt
= args
->rmtblkcnt
;
396 trace_xfs_attr_rmtval_get(args
);
398 ASSERT(!(args
->flags
& ATTR_KERNOVAL
));
399 ASSERT(args
->rmtvaluelen
== args
->valuelen
);
401 valuelen
= args
->rmtvaluelen
;
402 while (valuelen
> 0) {
403 nmap
= ATTR_RMTVALUE_MAPSIZE
;
404 error
= xfs_bmapi_read(args
->dp
, (xfs_fileoff_t
)lblkno
,
411 for (i
= 0; (i
< nmap
) && (valuelen
> 0); i
++) {
415 ASSERT((map
[i
].br_startblock
!= DELAYSTARTBLOCK
) &&
416 (map
[i
].br_startblock
!= HOLESTARTBLOCK
));
417 dblkno
= XFS_FSB_TO_DADDR(mp
, map
[i
].br_startblock
);
418 dblkcnt
= XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
419 error
= xfs_trans_read_buf(mp
, args
->trans
,
421 dblkno
, dblkcnt
, 0, &bp
,
422 &xfs_attr3_rmt_buf_ops
);
426 error
= xfs_attr_rmtval_copyout(mp
, bp
, args
->dp
->i_ino
,
429 xfs_trans_brelse(args
->trans
, bp
);
433 /* roll attribute extent map forwards */
434 lblkno
+= map
[i
].br_blockcount
;
435 blkcnt
-= map
[i
].br_blockcount
;
438 ASSERT(valuelen
== 0);
443 * Write the value associated with an attribute into the out-of-line buffer
444 * that we have defined for it.
448 struct xfs_da_args
*args
)
450 struct xfs_inode
*dp
= args
->dp
;
451 struct xfs_mount
*mp
= dp
->i_mount
;
452 struct xfs_bmbt_irec map
;
454 xfs_fileoff_t lfileoff
= 0;
455 uint8_t *src
= args
->value
;
462 trace_xfs_attr_rmtval_set(args
);
465 * Find a "hole" in the attribute address space large enough for
466 * us to drop the new attribute's value into. Because CRC enable
467 * attributes have headers, we can't just do a straight byte to FSB
468 * conversion and have to take the header space into account.
470 blkcnt
= xfs_attr3_rmt_blocks(mp
, args
->rmtvaluelen
);
471 error
= xfs_bmap_first_unused(args
->trans
, args
->dp
, blkcnt
, &lfileoff
,
476 args
->rmtblkno
= lblkno
= (xfs_dablk_t
)lfileoff
;
477 args
->rmtblkcnt
= blkcnt
;
480 * Roll through the "value", allocating blocks on disk as required.
484 * Allocate a single extent, up to the size of the value.
486 * Note that we have to consider this a data allocation as we
487 * write the remote attribute without logging the contents.
488 * Hence we must ensure that we aren't using blocks that are on
489 * the busy list so that we don't overwrite blocks which have
490 * recently been freed but their transactions are not yet
491 * committed to disk. If we overwrite the contents of a busy
492 * extent and then crash then the block may not contain the
493 * correct metadata after log recovery occurs.
495 xfs_defer_init(args
->dfops
, args
->firstblock
);
497 error
= xfs_bmapi_write(args
->trans
, dp
, (xfs_fileoff_t
)lblkno
,
498 blkcnt
, XFS_BMAPI_ATTRFORK
, args
->firstblock
,
499 args
->total
, &map
, &nmap
, args
->dfops
);
501 goto out_defer_cancel
;
502 xfs_defer_ijoin(args
->dfops
, dp
);
503 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
505 goto out_defer_cancel
;
508 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
509 (map
.br_startblock
!= HOLESTARTBLOCK
));
510 lblkno
+= map
.br_blockcount
;
511 blkcnt
-= map
.br_blockcount
;
514 * Start the next trans in the chain.
516 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
522 * Roll through the "value", copying the attribute value to the
523 * already-allocated blocks. Blocks are written synchronously
524 * so that we can know they are all on disk before we turn off
525 * the INCOMPLETE flag.
527 lblkno
= args
->rmtblkno
;
528 blkcnt
= args
->rmtblkcnt
;
529 valuelen
= args
->rmtvaluelen
;
530 while (valuelen
> 0) {
537 xfs_defer_init(args
->dfops
, args
->firstblock
);
539 error
= xfs_bmapi_read(dp
, (xfs_fileoff_t
)lblkno
,
545 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
546 (map
.br_startblock
!= HOLESTARTBLOCK
));
548 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
549 dblkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
551 bp
= xfs_buf_get(mp
->m_ddev_targp
, dblkno
, dblkcnt
, 0);
554 bp
->b_ops
= &xfs_attr3_rmt_buf_ops
;
556 xfs_attr_rmtval_copyin(mp
, bp
, args
->dp
->i_ino
, &offset
,
559 error
= xfs_bwrite(bp
); /* GROT: NOTE: synchronous write */
565 /* roll attribute extent map forwards */
566 lblkno
+= map
.br_blockcount
;
567 blkcnt
-= map
.br_blockcount
;
569 ASSERT(valuelen
== 0);
572 xfs_defer_cancel(args
->dfops
);
578 * Remove the value associated with an attribute by deleting the
579 * out-of-line buffer that it is stored on.
582 xfs_attr_rmtval_remove(
583 struct xfs_da_args
*args
)
585 struct xfs_mount
*mp
= args
->dp
->i_mount
;
591 trace_xfs_attr_rmtval_remove(args
);
594 * Roll through the "value", invalidating the attribute value's blocks.
596 lblkno
= args
->rmtblkno
;
597 blkcnt
= args
->rmtblkcnt
;
599 struct xfs_bmbt_irec map
;
606 * Try to remember where we decided to put the value.
609 error
= xfs_bmapi_read(args
->dp
, (xfs_fileoff_t
)lblkno
,
610 blkcnt
, &map
, &nmap
, XFS_BMAPI_ATTRFORK
);
614 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
615 (map
.br_startblock
!= HOLESTARTBLOCK
));
617 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
618 dblkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
621 * If the "remote" value is in the cache, remove it.
623 bp
= xfs_incore(mp
->m_ddev_targp
, dblkno
, dblkcnt
, XBF_TRYLOCK
);
630 lblkno
+= map
.br_blockcount
;
631 blkcnt
-= map
.br_blockcount
;
635 * Keep de-allocating extents until the remote-value region is gone.
637 lblkno
= args
->rmtblkno
;
638 blkcnt
= args
->rmtblkcnt
;
641 xfs_defer_init(args
->dfops
, args
->firstblock
);
642 error
= xfs_bunmapi(args
->trans
, args
->dp
, lblkno
, blkcnt
,
643 XFS_BMAPI_ATTRFORK
, 1, args
->firstblock
,
646 goto out_defer_cancel
;
647 xfs_defer_ijoin(args
->dfops
, args
->dp
);
648 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
650 goto out_defer_cancel
;
653 * Close out trans and start the next one in the chain.
655 error
= xfs_trans_roll_inode(&args
->trans
, args
->dp
);
661 xfs_defer_cancel(args
->dfops
);