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_types.h"
24 #include "xfs_trans.h"
25 #include "xfs_trans_priv.h"
28 #include "xfs_mount.h"
29 #include "xfs_error.h"
30 #include "xfs_da_btree.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_dinode.h"
33 #include "xfs_inode.h"
34 #include "xfs_alloc.h"
35 #include "xfs_inode_item.h"
37 #include "xfs_bmap_util.h"
39 #include "xfs_attr_leaf.h"
40 #include "xfs_attr_remote.h"
41 #include "xfs_trans_space.h"
42 #include "xfs_trace.h"
43 #include "xfs_cksum.h"
44 #include "xfs_buf_item.h"
46 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
49 * Each contiguous block has a header, so it is not just a simple attribute
50 * length to FSB conversion.
57 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
58 int buflen
= XFS_ATTR3_RMT_BUF_SPACE(mp
, mp
->m_sb
.sb_blocksize
);
59 return (attrlen
+ buflen
- 1) / buflen
;
61 return XFS_B_TO_FSB(mp
, attrlen
);
65 * Checking of the remote attribute header is split into two parts. The verifier
66 * does CRC, location and bounds checking, the unpacking function checks the
67 * attribute parameters and owner.
78 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
80 if (bno
!= be64_to_cpu(rmt
->rm_blkno
))
82 if (offset
!= be32_to_cpu(rmt
->rm_offset
))
84 if (size
!= be32_to_cpu(rmt
->rm_bytes
))
86 if (ino
!= be64_to_cpu(rmt
->rm_owner
))
100 struct xfs_attr3_rmt_hdr
*rmt
= ptr
;
102 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
104 if (rmt
->rm_magic
!= cpu_to_be32(XFS_ATTR3_RMT_MAGIC
))
106 if (!uuid_equal(&rmt
->rm_uuid
, &mp
->m_sb
.sb_uuid
))
108 if (be64_to_cpu(rmt
->rm_blkno
) != bno
)
110 if (be32_to_cpu(rmt
->rm_bytes
) > fsbsize
- sizeof(*rmt
))
112 if (be32_to_cpu(rmt
->rm_offset
) +
113 be32_to_cpu(rmt
->rm_bytes
) >= XATTR_SIZE_MAX
)
115 if (rmt
->rm_owner
== 0)
122 xfs_attr3_rmt_read_verify(
125 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
128 bool corrupt
= false;
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
>= XFS_LBSIZE(mp
));
141 if (!xfs_verify_cksum(ptr
, XFS_LBSIZE(mp
),
142 XFS_ATTR3_RMT_CRC_OFF
)) {
146 if (!xfs_attr3_rmt_verify(mp
, ptr
, XFS_LBSIZE(mp
), bno
)) {
150 len
-= XFS_LBSIZE(mp
);
151 ptr
+= XFS_LBSIZE(mp
);
156 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
157 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
163 xfs_attr3_rmt_write_verify(
166 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
167 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
172 /* no verification of non-crc buffers */
173 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
178 len
= BBTOB(bp
->b_length
);
179 ASSERT(len
>= XFS_LBSIZE(mp
));
182 if (!xfs_attr3_rmt_verify(mp
, ptr
, XFS_LBSIZE(mp
), bno
)) {
183 XFS_CORRUPTION_ERROR(__func__
,
184 XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
185 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
189 struct xfs_attr3_rmt_hdr
*rmt
;
191 rmt
= (struct xfs_attr3_rmt_hdr
*)ptr
;
192 rmt
->rm_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
194 xfs_update_cksum(ptr
, XFS_LBSIZE(mp
), XFS_ATTR3_RMT_CRC_OFF
);
196 len
-= XFS_LBSIZE(mp
);
197 ptr
+= XFS_LBSIZE(mp
);
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_uuid
);
226 rmt
->rm_owner
= cpu_to_be64(ino
);
227 rmt
->rm_blkno
= cpu_to_be64(bno
);
229 return sizeof(struct xfs_attr3_rmt_hdr
);
233 * Helper functions to copy attribute data in and out of the one disk extents
236 xfs_attr_rmtval_copyout(
237 struct xfs_mount
*mp
,
244 char *src
= bp
->b_addr
;
245 xfs_daddr_t bno
= bp
->b_bn
;
246 int len
= BBTOB(bp
->b_length
);
248 ASSERT(len
>= XFS_LBSIZE(mp
));
250 while (len
> 0 && *valuelen
> 0) {
252 int byte_cnt
= XFS_ATTR3_RMT_BUF_SPACE(mp
, XFS_LBSIZE(mp
));
254 byte_cnt
= min(*valuelen
, byte_cnt
);
256 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
257 if (!xfs_attr3_rmt_hdr_ok(mp
, src
, ino
, *offset
,
260 "remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)",
261 bno
, *offset
, byte_cnt
, ino
);
264 hdr_size
= sizeof(struct xfs_attr3_rmt_hdr
);
267 memcpy(*dst
, src
+ hdr_size
, byte_cnt
);
269 /* roll buffer forwards */
270 len
-= XFS_LBSIZE(mp
);
271 src
+= XFS_LBSIZE(mp
);
274 /* roll attribute data forwards */
275 *valuelen
-= byte_cnt
;
283 xfs_attr_rmtval_copyin(
284 struct xfs_mount
*mp
,
291 char *dst
= bp
->b_addr
;
292 xfs_daddr_t bno
= bp
->b_bn
;
293 int len
= BBTOB(bp
->b_length
);
295 ASSERT(len
>= XFS_LBSIZE(mp
));
297 while (len
> 0 && *valuelen
> 0) {
299 int byte_cnt
= XFS_ATTR3_RMT_BUF_SPACE(mp
, XFS_LBSIZE(mp
));
301 byte_cnt
= min(*valuelen
, byte_cnt
);
302 hdr_size
= xfs_attr3_rmt_hdr_set(mp
, dst
, ino
, *offset
,
305 memcpy(dst
+ hdr_size
, *src
, byte_cnt
);
308 * If this is the last block, zero the remainder of it.
309 * Check that we are actually the last block, too.
311 if (byte_cnt
+ hdr_size
< XFS_LBSIZE(mp
)) {
312 ASSERT(*valuelen
- byte_cnt
== 0);
313 ASSERT(len
== XFS_LBSIZE(mp
));
314 memset(dst
+ hdr_size
+ byte_cnt
, 0,
315 XFS_LBSIZE(mp
) - hdr_size
- byte_cnt
);
318 /* roll buffer forwards */
319 len
-= XFS_LBSIZE(mp
);
320 dst
+= XFS_LBSIZE(mp
);
323 /* roll attribute data forwards */
324 *valuelen
-= byte_cnt
;
331 * Read the value associated with an attribute from the out-of-line buffer
332 * that we stored it in.
336 struct xfs_da_args
*args
)
338 struct xfs_bmbt_irec map
[ATTR_RMTVALUE_MAPSIZE
];
339 struct xfs_mount
*mp
= args
->dp
->i_mount
;
341 xfs_dablk_t lblkno
= args
->rmtblkno
;
342 __uint8_t
*dst
= args
->value
;
343 int valuelen
= args
->valuelen
;
346 int blkcnt
= args
->rmtblkcnt
;
350 trace_xfs_attr_rmtval_get(args
);
352 ASSERT(!(args
->flags
& ATTR_KERNOVAL
));
354 while (valuelen
> 0) {
355 nmap
= ATTR_RMTVALUE_MAPSIZE
;
356 error
= xfs_bmapi_read(args
->dp
, (xfs_fileoff_t
)lblkno
,
363 for (i
= 0; (i
< nmap
) && (valuelen
> 0); i
++) {
367 ASSERT((map
[i
].br_startblock
!= DELAYSTARTBLOCK
) &&
368 (map
[i
].br_startblock
!= HOLESTARTBLOCK
));
369 dblkno
= XFS_FSB_TO_DADDR(mp
, map
[i
].br_startblock
);
370 dblkcnt
= XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
371 error
= xfs_trans_read_buf(mp
, NULL
, mp
->m_ddev_targp
,
372 dblkno
, dblkcnt
, 0, &bp
,
373 &xfs_attr3_rmt_buf_ops
);
377 error
= xfs_attr_rmtval_copyout(mp
, bp
, args
->dp
->i_ino
,
384 /* roll attribute extent map forwards */
385 lblkno
+= map
[i
].br_blockcount
;
386 blkcnt
-= map
[i
].br_blockcount
;
389 ASSERT(valuelen
== 0);
394 * Write the value associated with an attribute into the out-of-line buffer
395 * that we have defined for it.
399 struct xfs_da_args
*args
)
401 struct xfs_inode
*dp
= args
->dp
;
402 struct xfs_mount
*mp
= dp
->i_mount
;
403 struct xfs_bmbt_irec map
;
405 xfs_fileoff_t lfileoff
= 0;
406 __uint8_t
*src
= args
->value
;
413 trace_xfs_attr_rmtval_set(args
);
416 * Find a "hole" in the attribute address space large enough for
417 * us to drop the new attribute's value into. Because CRC enable
418 * attributes have headers, we can't just do a straight byte to FSB
419 * conversion and have to take the header space into account.
421 blkcnt
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
422 error
= xfs_bmap_first_unused(args
->trans
, args
->dp
, blkcnt
, &lfileoff
,
427 args
->rmtblkno
= lblkno
= (xfs_dablk_t
)lfileoff
;
428 args
->rmtblkcnt
= blkcnt
;
431 * Roll through the "value", allocating blocks on disk as required.
437 * Allocate a single extent, up to the size of the value.
439 xfs_bmap_init(args
->flist
, args
->firstblock
);
441 error
= xfs_bmapi_write(args
->trans
, dp
, (xfs_fileoff_t
)lblkno
,
443 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
444 args
->firstblock
, args
->total
, &map
, &nmap
,
447 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
453 xfs_bmap_cancel(args
->flist
);
458 * bmap_finish() may have committed the last trans and started
459 * a new one. We need the inode to be in all transactions.
462 xfs_trans_ijoin(args
->trans
, dp
, 0);
465 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
466 (map
.br_startblock
!= HOLESTARTBLOCK
));
467 lblkno
+= map
.br_blockcount
;
468 blkcnt
-= map
.br_blockcount
;
471 * Start the next trans in the chain.
473 error
= xfs_trans_roll(&args
->trans
, dp
);
479 * Roll through the "value", copying the attribute value to the
480 * already-allocated blocks. Blocks are written synchronously
481 * so that we can know they are all on disk before we turn off
482 * the INCOMPLETE flag.
484 lblkno
= args
->rmtblkno
;
485 blkcnt
= args
->rmtblkcnt
;
486 valuelen
= args
->valuelen
;
487 while (valuelen
> 0) {
494 xfs_bmap_init(args
->flist
, args
->firstblock
);
496 error
= xfs_bmapi_read(dp
, (xfs_fileoff_t
)lblkno
,
502 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
503 (map
.br_startblock
!= HOLESTARTBLOCK
));
505 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
506 dblkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
508 bp
= xfs_buf_get(mp
->m_ddev_targp
, dblkno
, dblkcnt
, 0);
511 bp
->b_ops
= &xfs_attr3_rmt_buf_ops
;
513 xfs_attr_rmtval_copyin(mp
, bp
, args
->dp
->i_ino
, &offset
,
516 error
= xfs_bwrite(bp
); /* GROT: NOTE: synchronous write */
522 /* roll attribute extent map forwards */
523 lblkno
+= map
.br_blockcount
;
524 blkcnt
-= map
.br_blockcount
;
526 ASSERT(valuelen
== 0);
531 * Remove the value associated with an attribute by deleting the
532 * out-of-line buffer that it is stored on.
535 xfs_attr_rmtval_remove(
536 struct xfs_da_args
*args
)
538 struct xfs_mount
*mp
= args
->dp
->i_mount
;
544 trace_xfs_attr_rmtval_remove(args
);
547 * Roll through the "value", invalidating the attribute value's blocks.
549 lblkno
= args
->rmtblkno
;
550 blkcnt
= args
->rmtblkcnt
;
552 struct xfs_bmbt_irec map
;
559 * Try to remember where we decided to put the value.
562 error
= xfs_bmapi_read(args
->dp
, (xfs_fileoff_t
)lblkno
,
563 blkcnt
, &map
, &nmap
, XFS_BMAPI_ATTRFORK
);
567 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
568 (map
.br_startblock
!= HOLESTARTBLOCK
));
570 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
571 dblkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
574 * If the "remote" value is in the cache, remove it.
576 bp
= xfs_incore(mp
->m_ddev_targp
, dblkno
, dblkcnt
, XBF_TRYLOCK
);
583 lblkno
+= map
.br_blockcount
;
584 blkcnt
-= map
.br_blockcount
;
588 * Keep de-allocating extents until the remote-value region is gone.
590 lblkno
= args
->rmtblkno
;
591 blkcnt
= args
->rmtblkcnt
;
596 xfs_bmap_init(args
->flist
, args
->firstblock
);
597 error
= xfs_bunmapi(args
->trans
, args
->dp
, lblkno
, blkcnt
,
598 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
599 1, args
->firstblock
, args
->flist
,
602 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
608 xfs_bmap_cancel(args
->flist
);
613 * bmap_finish() may have committed the last trans and started
614 * a new one. We need the inode to be in all transactions.
617 xfs_trans_ijoin(args
->trans
, args
->dp
, 0);
620 * Close out trans and start the next one in the chain.
622 error
= xfs_trans_roll(&args
->trans
, args
->dp
);