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
19 #include "libxfs_priv.h"
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"
34 #include "xfs_attr_leaf.h"
35 #include "xfs_attr_remote.h"
36 #include "xfs_trans_space.h"
37 #include "xfs_trace.h"
38 #include "xfs_cksum.h"
40 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
43 * Each contiguous block has a header, so it is not just a simple attribute
44 * length to FSB conversion.
51 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
52 int buflen
= XFS_ATTR3_RMT_BUF_SPACE(mp
, mp
->m_sb
.sb_blocksize
);
53 return (attrlen
+ buflen
- 1) / buflen
;
55 return XFS_B_TO_FSB(mp
, attrlen
);
59 * Checking of the remote attribute header is split into two parts. The verifier
60 * does CRC, location and bounds checking, the unpacking function checks the
61 * attribute parameters and owner.
71 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
73 if (bno
!= be64_to_cpu(rmt
->rm_blkno
))
74 return __this_address
;
75 if (offset
!= be32_to_cpu(rmt
->rm_offset
))
76 return __this_address
;
77 if (size
!= be32_to_cpu(rmt
->rm_bytes
))
78 return __this_address
;
79 if (ino
!= be64_to_cpu(rmt
->rm_owner
))
80 return __this_address
;
93 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
95 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
96 return __this_address
;
97 if (rmt
->rm_magic
!= cpu_to_be32(XFS_ATTR3_RMT_MAGIC
))
98 return __this_address
;
99 if (!uuid_equal(&rmt
->rm_uuid
, &mp
->m_sb
.sb_meta_uuid
))
100 return __this_address
;
101 if (be64_to_cpu(rmt
->rm_blkno
) != bno
)
102 return __this_address
;
103 if (be32_to_cpu(rmt
->rm_bytes
) > fsbsize
- sizeof(*rmt
))
104 return __this_address
;
105 if (be32_to_cpu(rmt
->rm_offset
) +
106 be32_to_cpu(rmt
->rm_bytes
) > XFS_XATTR_SIZE_MAX
)
107 return __this_address
;
108 if (rmt
->rm_owner
== 0)
109 return __this_address
;
115 __xfs_attr3_rmt_read_verify(
118 xfs_failaddr_t
*failaddr
)
120 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
124 int blksize
= mp
->m_attr_geo
->blksize
;
126 /* no verification of non-crc buffers */
127 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
132 len
= BBTOB(bp
->b_length
);
133 ASSERT(len
>= blksize
);
137 !xfs_verify_cksum(ptr
, blksize
, XFS_ATTR3_RMT_CRC_OFF
)) {
138 *failaddr
= __this_address
;
141 *failaddr
= xfs_attr3_rmt_verify(mp
, ptr
, blksize
, bno
);
143 return -EFSCORRUPTED
;
146 bno
+= BTOBB(blksize
);
150 *failaddr
= __this_address
;
151 return -EFSCORRUPTED
;
158 xfs_attr3_rmt_read_verify(
164 error
= __xfs_attr3_rmt_read_verify(bp
, true, &fa
);
166 xfs_verifier_error(bp
, error
, fa
);
169 static xfs_failaddr_t
170 xfs_attr3_rmt_verify_struct(
176 error
= __xfs_attr3_rmt_read_verify(bp
, false, &fa
);
177 return error
? fa
: NULL
;
181 xfs_attr3_rmt_write_verify(
184 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
186 int blksize
= mp
->m_attr_geo
->blksize
;
191 /* no verification of non-crc buffers */
192 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
197 len
= BBTOB(bp
->b_length
);
198 ASSERT(len
>= blksize
);
201 struct xfs_attr3_rmt_hdr
*rmt
= (struct xfs_attr3_rmt_hdr
*)ptr
;
203 fa
= xfs_attr3_rmt_verify(mp
, ptr
, blksize
, bno
);
205 xfs_verifier_error(bp
, -EFSCORRUPTED
, fa
);
210 * Ensure we aren't writing bogus LSNs to disk. See
211 * xfs_attr3_rmt_hdr_set() for the explanation.
213 if (rmt
->rm_lsn
!= cpu_to_be64(NULLCOMMITLSN
)) {
214 xfs_verifier_error(bp
, -EFSCORRUPTED
, __this_address
);
217 xfs_update_cksum(ptr
, blksize
, XFS_ATTR3_RMT_CRC_OFF
);
221 bno
+= BTOBB(blksize
);
225 xfs_verifier_error(bp
, -EFSCORRUPTED
, __this_address
);
228 const struct xfs_buf_ops xfs_attr3_rmt_buf_ops
= {
229 .name
= "xfs_attr3_rmt",
230 .verify_read
= xfs_attr3_rmt_read_verify
,
231 .verify_write
= xfs_attr3_rmt_write_verify
,
232 .verify_struct
= xfs_attr3_rmt_verify_struct
,
236 xfs_attr3_rmt_hdr_set(
237 struct xfs_mount
*mp
,
244 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
246 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
249 rmt
->rm_magic
= cpu_to_be32(XFS_ATTR3_RMT_MAGIC
);
250 rmt
->rm_offset
= cpu_to_be32(offset
);
251 rmt
->rm_bytes
= cpu_to_be32(size
);
252 uuid_copy(&rmt
->rm_uuid
, &mp
->m_sb
.sb_meta_uuid
);
253 rmt
->rm_owner
= cpu_to_be64(ino
);
254 rmt
->rm_blkno
= cpu_to_be64(bno
);
257 * Remote attribute blocks are written synchronously, so we don't
258 * have an LSN that we can stamp in them that makes any sense to log
259 * recovery. To ensure that log recovery handles overwrites of these
260 * blocks sanely (i.e. once they've been freed and reallocated as some
261 * other type of metadata) we need to ensure that the LSN has a value
262 * that tells log recovery to ignore the LSN and overwrite the buffer
263 * with whatever is in it's log. To do this, we use the magic
264 * NULLCOMMITLSN to indicate that the LSN is invalid.
266 rmt
->rm_lsn
= cpu_to_be64(NULLCOMMITLSN
);
268 return sizeof(struct xfs_attr3_rmt_hdr
);
272 * Helper functions to copy attribute data in and out of the one disk extents
275 xfs_attr_rmtval_copyout(
276 struct xfs_mount
*mp
,
283 char *src
= bp
->b_addr
;
284 xfs_daddr_t bno
= bp
->b_bn
;
285 int len
= BBTOB(bp
->b_length
);
286 int blksize
= mp
->m_attr_geo
->blksize
;
288 ASSERT(len
>= blksize
);
290 while (len
> 0 && *valuelen
> 0) {
292 int byte_cnt
= XFS_ATTR3_RMT_BUF_SPACE(mp
, blksize
);
294 byte_cnt
= min(*valuelen
, byte_cnt
);
296 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
297 if (xfs_attr3_rmt_hdr_ok(src
, ino
, *offset
,
300 "remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)",
301 bno
, *offset
, byte_cnt
, ino
);
302 return -EFSCORRUPTED
;
304 hdr_size
= sizeof(struct xfs_attr3_rmt_hdr
);
307 memcpy(*dst
, src
+ hdr_size
, byte_cnt
);
309 /* roll buffer forwards */
312 bno
+= BTOBB(blksize
);
314 /* roll attribute data forwards */
315 *valuelen
-= byte_cnt
;
323 xfs_attr_rmtval_copyin(
324 struct xfs_mount
*mp
,
331 char *dst
= bp
->b_addr
;
332 xfs_daddr_t bno
= bp
->b_bn
;
333 int len
= BBTOB(bp
->b_length
);
334 int blksize
= mp
->m_attr_geo
->blksize
;
336 ASSERT(len
>= blksize
);
338 while (len
> 0 && *valuelen
> 0) {
340 int byte_cnt
= XFS_ATTR3_RMT_BUF_SPACE(mp
, blksize
);
342 byte_cnt
= min(*valuelen
, byte_cnt
);
343 hdr_size
= xfs_attr3_rmt_hdr_set(mp
, dst
, ino
, *offset
,
346 memcpy(dst
+ hdr_size
, *src
, byte_cnt
);
349 * If this is the last block, zero the remainder of it.
350 * Check that we are actually the last block, too.
352 if (byte_cnt
+ hdr_size
< blksize
) {
353 ASSERT(*valuelen
- byte_cnt
== 0);
354 ASSERT(len
== blksize
);
355 memset(dst
+ hdr_size
+ byte_cnt
, 0,
356 blksize
- hdr_size
- byte_cnt
);
359 /* roll buffer forwards */
362 bno
+= BTOBB(blksize
);
364 /* roll attribute data forwards */
365 *valuelen
-= byte_cnt
;
372 * Read the value associated with an attribute from the out-of-line buffer
373 * that we stored it in.
377 struct xfs_da_args
*args
)
379 struct xfs_bmbt_irec map
[ATTR_RMTVALUE_MAPSIZE
];
380 struct xfs_mount
*mp
= args
->dp
->i_mount
;
382 xfs_dablk_t lblkno
= args
->rmtblkno
;
383 uint8_t *dst
= args
->value
;
387 int blkcnt
= args
->rmtblkcnt
;
391 trace_xfs_attr_rmtval_get(args
);
393 ASSERT(!(args
->flags
& ATTR_KERNOVAL
));
394 ASSERT(args
->rmtvaluelen
== args
->valuelen
);
396 valuelen
= args
->rmtvaluelen
;
397 while (valuelen
> 0) {
398 nmap
= ATTR_RMTVALUE_MAPSIZE
;
399 error
= xfs_bmapi_read(args
->dp
, (xfs_fileoff_t
)lblkno
,
406 for (i
= 0; (i
< nmap
) && (valuelen
> 0); i
++) {
410 ASSERT((map
[i
].br_startblock
!= DELAYSTARTBLOCK
) &&
411 (map
[i
].br_startblock
!= HOLESTARTBLOCK
));
412 dblkno
= XFS_FSB_TO_DADDR(mp
, map
[i
].br_startblock
);
413 dblkcnt
= XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
414 error
= xfs_trans_read_buf(mp
, args
->trans
,
416 dblkno
, dblkcnt
, 0, &bp
,
417 &xfs_attr3_rmt_buf_ops
);
421 error
= xfs_attr_rmtval_copyout(mp
, bp
, args
->dp
->i_ino
,
424 xfs_trans_brelse(args
->trans
, bp
);
428 /* roll attribute extent map forwards */
429 lblkno
+= map
[i
].br_blockcount
;
430 blkcnt
-= map
[i
].br_blockcount
;
433 ASSERT(valuelen
== 0);
438 * Write the value associated with an attribute into the out-of-line buffer
439 * that we have defined for it.
443 struct xfs_da_args
*args
)
445 struct xfs_inode
*dp
= args
->dp
;
446 struct xfs_mount
*mp
= dp
->i_mount
;
447 struct xfs_bmbt_irec map
;
449 xfs_fileoff_t lfileoff
= 0;
450 uint8_t *src
= args
->value
;
457 trace_xfs_attr_rmtval_set(args
);
460 * Find a "hole" in the attribute address space large enough for
461 * us to drop the new attribute's value into. Because CRC enable
462 * attributes have headers, we can't just do a straight byte to FSB
463 * conversion and have to take the header space into account.
465 blkcnt
= xfs_attr3_rmt_blocks(mp
, args
->rmtvaluelen
);
466 error
= xfs_bmap_first_unused(args
->trans
, args
->dp
, blkcnt
, &lfileoff
,
471 args
->rmtblkno
= lblkno
= (xfs_dablk_t
)lfileoff
;
472 args
->rmtblkcnt
= blkcnt
;
475 * Roll through the "value", allocating blocks on disk as required.
479 * Allocate a single extent, up to the size of the value.
481 * Note that we have to consider this a data allocation as we
482 * write the remote attribute without logging the contents.
483 * Hence we must ensure that we aren't using blocks that are on
484 * the busy list so that we don't overwrite blocks which have
485 * recently been freed but their transactions are not yet
486 * committed to disk. If we overwrite the contents of a busy
487 * extent and then crash then the block may not contain the
488 * correct metadata after log recovery occurs.
490 xfs_defer_init(args
->dfops
, args
->firstblock
);
492 error
= xfs_bmapi_write(args
->trans
, dp
, (xfs_fileoff_t
)lblkno
,
493 blkcnt
, XFS_BMAPI_ATTRFORK
, args
->firstblock
,
494 args
->total
, &map
, &nmap
, args
->dfops
);
496 goto out_defer_cancel
;
497 xfs_defer_ijoin(args
->dfops
, dp
);
498 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
500 goto out_defer_cancel
;
503 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
504 (map
.br_startblock
!= HOLESTARTBLOCK
));
505 lblkno
+= map
.br_blockcount
;
506 blkcnt
-= map
.br_blockcount
;
509 * Start the next trans in the chain.
511 error
= xfs_trans_roll_inode(&args
->trans
, dp
);
517 * Roll through the "value", copying the attribute value to the
518 * already-allocated blocks. Blocks are written synchronously
519 * so that we can know they are all on disk before we turn off
520 * the INCOMPLETE flag.
522 lblkno
= args
->rmtblkno
;
523 blkcnt
= args
->rmtblkcnt
;
524 valuelen
= args
->rmtvaluelen
;
525 while (valuelen
> 0) {
532 xfs_defer_init(args
->dfops
, args
->firstblock
);
534 error
= xfs_bmapi_read(dp
, (xfs_fileoff_t
)lblkno
,
540 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
541 (map
.br_startblock
!= HOLESTARTBLOCK
));
543 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
544 dblkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
546 bp
= xfs_buf_get(mp
->m_ddev_targp
, dblkno
, dblkcnt
, 0);
549 bp
->b_ops
= &xfs_attr3_rmt_buf_ops
;
551 xfs_attr_rmtval_copyin(mp
, bp
, args
->dp
->i_ino
, &offset
,
554 error
= xfs_bwrite(bp
); /* GROT: NOTE: synchronous write */
560 /* roll attribute extent map forwards */
561 lblkno
+= map
.br_blockcount
;
562 blkcnt
-= map
.br_blockcount
;
564 ASSERT(valuelen
== 0);
567 xfs_defer_cancel(args
->dfops
);
573 * Remove the value associated with an attribute by deleting the
574 * out-of-line buffer that it is stored on.
577 xfs_attr_rmtval_remove(
578 struct xfs_da_args
*args
)
580 struct xfs_mount
*mp
= args
->dp
->i_mount
;
586 trace_xfs_attr_rmtval_remove(args
);
589 * Roll through the "value", invalidating the attribute value's blocks.
591 lblkno
= args
->rmtblkno
;
592 blkcnt
= args
->rmtblkcnt
;
594 struct xfs_bmbt_irec map
;
601 * Try to remember where we decided to put the value.
604 error
= xfs_bmapi_read(args
->dp
, (xfs_fileoff_t
)lblkno
,
605 blkcnt
, &map
, &nmap
, XFS_BMAPI_ATTRFORK
);
609 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
610 (map
.br_startblock
!= HOLESTARTBLOCK
));
612 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
613 dblkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
616 * If the "remote" value is in the cache, remove it.
618 bp
= xfs_incore(mp
->m_ddev_targp
, dblkno
, dblkcnt
, XBF_TRYLOCK
);
625 lblkno
+= map
.br_blockcount
;
626 blkcnt
-= map
.br_blockcount
;
630 * Keep de-allocating extents until the remote-value region is gone.
632 lblkno
= args
->rmtblkno
;
633 blkcnt
= args
->rmtblkcnt
;
636 xfs_defer_init(args
->dfops
, args
->firstblock
);
637 error
= xfs_bunmapi(args
->trans
, args
->dp
, lblkno
, blkcnt
,
638 XFS_BMAPI_ATTRFORK
, 1, args
->firstblock
,
641 goto out_defer_cancel
;
642 xfs_defer_ijoin(args
->dfops
, args
->dp
);
643 error
= xfs_defer_finish(&args
->trans
, args
->dfops
);
645 goto out_defer_cancel
;
648 * Close out trans and start the next one in the chain.
650 error
= xfs_trans_roll_inode(&args
->trans
, args
->dp
);
656 xfs_defer_cancel(args
->dfops
);