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"
27 #include "xfs_mount.h"
28 #include "xfs_error.h"
29 #include "xfs_da_btree.h"
30 #include "xfs_bmap_btree.h"
31 #include "xfs_dinode.h"
32 #include "xfs_inode.h"
33 #include "xfs_alloc.h"
34 #include "xfs_inode_item.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"
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 int buflen
= XFS_ATTR3_RMT_BUF_SPACE(mp
,
56 mp
->m_sb
.sb_blocksize
);
57 return (attrlen
+ buflen
- 1) / buflen
;
64 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
65 struct xfs_attr3_rmt_hdr
*rmt
= bp
->b_addr
;
67 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
69 if (rmt
->rm_magic
!= cpu_to_be32(XFS_ATTR3_RMT_MAGIC
))
71 if (!uuid_equal(&rmt
->rm_uuid
, &mp
->m_sb
.sb_uuid
))
73 if (bp
->b_bn
!= be64_to_cpu(rmt
->rm_blkno
))
75 if (be32_to_cpu(rmt
->rm_offset
) +
76 be32_to_cpu(rmt
->rm_bytes
) >= XATTR_SIZE_MAX
)
78 if (rmt
->rm_owner
== 0)
85 xfs_attr3_rmt_read_verify(
88 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
90 /* no verification of non-crc buffers */
91 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
94 if (!xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
95 XFS_ATTR3_RMT_CRC_OFF
) ||
96 !xfs_attr3_rmt_verify(bp
)) {
97 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
98 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
103 xfs_attr3_rmt_write_verify(
106 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
107 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
109 /* no verification of non-crc buffers */
110 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
113 if (!xfs_attr3_rmt_verify(bp
)) {
114 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
115 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
120 struct xfs_attr3_rmt_hdr
*rmt
= bp
->b_addr
;
121 rmt
->rm_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
123 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
124 XFS_ATTR3_RMT_CRC_OFF
);
127 const struct xfs_buf_ops xfs_attr3_rmt_buf_ops
= {
128 .verify_read
= xfs_attr3_rmt_read_verify
,
129 .verify_write
= xfs_attr3_rmt_write_verify
,
133 xfs_attr3_rmt_hdr_set(
134 struct xfs_mount
*mp
,
140 struct xfs_attr3_rmt_hdr
*rmt
= bp
->b_addr
;
142 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
145 rmt
->rm_magic
= cpu_to_be32(XFS_ATTR3_RMT_MAGIC
);
146 rmt
->rm_offset
= cpu_to_be32(offset
);
147 rmt
->rm_bytes
= cpu_to_be32(size
);
148 uuid_copy(&rmt
->rm_uuid
, &mp
->m_sb
.sb_uuid
);
149 rmt
->rm_owner
= cpu_to_be64(ino
);
150 rmt
->rm_blkno
= cpu_to_be64(bp
->b_bn
);
151 bp
->b_ops
= &xfs_attr3_rmt_buf_ops
;
153 return sizeof(struct xfs_attr3_rmt_hdr
);
157 * Checking of the remote attribute header is split into two parts. the verifier
158 * does CRC, location and bounds checking, the unpacking function checks the
159 * attribute parameters and owner.
162 xfs_attr3_rmt_hdr_ok(
163 struct xfs_mount
*mp
,
169 struct xfs_attr3_rmt_hdr
*rmt
= bp
->b_addr
;
171 if (offset
!= be32_to_cpu(rmt
->rm_offset
))
173 if (size
!= be32_to_cpu(rmt
->rm_bytes
))
175 if (ino
!= be64_to_cpu(rmt
->rm_owner
))
183 * Read the value associated with an attribute from the out-of-line buffer
184 * that we stored it in.
188 struct xfs_da_args
*args
)
190 struct xfs_bmbt_irec map
[ATTR_RMTVALUE_MAPSIZE
];
191 struct xfs_mount
*mp
= args
->dp
->i_mount
;
194 xfs_dablk_t lblkno
= args
->rmtblkno
;
195 void *dst
= args
->value
;
196 int valuelen
= args
->valuelen
;
203 trace_xfs_attr_rmtval_get(args
);
205 ASSERT(!(args
->flags
& ATTR_KERNOVAL
));
207 while (valuelen
> 0) {
208 nmap
= ATTR_RMTVALUE_MAPSIZE
;
209 error
= xfs_bmapi_read(args
->dp
, (xfs_fileoff_t
)lblkno
,
210 args
->rmtblkcnt
, map
, &nmap
,
216 for (i
= 0; (i
< nmap
) && (valuelen
> 0); i
++) {
220 ASSERT((map
[i
].br_startblock
!= DELAYSTARTBLOCK
) &&
221 (map
[i
].br_startblock
!= HOLESTARTBLOCK
));
222 dblkno
= XFS_FSB_TO_DADDR(mp
, map
[i
].br_startblock
);
223 blkcnt
= XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
224 error
= xfs_trans_read_buf(mp
, NULL
, mp
->m_ddev_targp
,
225 dblkno
, blkcnt
, 0, &bp
,
226 &xfs_attr3_rmt_buf_ops
);
230 byte_cnt
= min_t(int, valuelen
, BBTOB(bp
->b_length
));
231 byte_cnt
= XFS_ATTR3_RMT_BUF_SPACE(mp
, byte_cnt
);
234 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
235 if (!xfs_attr3_rmt_hdr_ok(mp
, args
->dp
->i_ino
,
236 offset
, byte_cnt
, bp
)) {
238 "remote attribute header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
239 offset
, byte_cnt
, args
->dp
->i_ino
);
245 src
+= sizeof(struct xfs_attr3_rmt_hdr
);
248 memcpy(dst
, src
, byte_cnt
);
253 valuelen
-= byte_cnt
;
255 lblkno
+= map
[i
].br_blockcount
;
258 ASSERT(valuelen
== 0);
263 * Write the value associated with an attribute into the out-of-line buffer
264 * that we have defined for it.
268 struct xfs_da_args
*args
)
270 struct xfs_inode
*dp
= args
->dp
;
271 struct xfs_mount
*mp
= dp
->i_mount
;
272 struct xfs_bmbt_irec map
;
276 xfs_fileoff_t lfileoff
= 0;
277 void *src
= args
->value
;
283 bool crcs
= xfs_sb_version_hascrc(&mp
->m_sb
);
286 trace_xfs_attr_rmtval_set(args
);
289 * Find a "hole" in the attribute address space large enough for
290 * us to drop the new attribute's value into. Because CRC enable
291 * attributes have headers, we can't just do a straight byte to FSB
292 * conversion. We calculate the worst case block count in this case
293 * and we may not need that many, so we have to handle this when
294 * allocating the blocks below.
297 blkcnt
= XFS_B_TO_FSB(mp
, args
->valuelen
);
299 blkcnt
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
301 error
= xfs_bmap_first_unused(args
->trans
, args
->dp
, blkcnt
, &lfileoff
,
306 /* Start with the attribute data. We'll allocate the rest afterwards. */
308 blkcnt
= XFS_B_TO_FSB(mp
, args
->valuelen
);
310 args
->rmtblkno
= lblkno
= (xfs_dablk_t
)lfileoff
;
311 args
->rmtblkcnt
= blkcnt
;
314 * Roll through the "value", allocating blocks on disk as required.
320 * Allocate a single extent, up to the size of the value.
322 xfs_bmap_init(args
->flist
, args
->firstblock
);
324 error
= xfs_bmapi_write(args
->trans
, dp
, (xfs_fileoff_t
)lblkno
,
326 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
327 args
->firstblock
, args
->total
, &map
, &nmap
,
330 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
336 xfs_bmap_cancel(args
->flist
);
341 * bmap_finish() may have committed the last trans and started
342 * a new one. We need the inode to be in all transactions.
345 xfs_trans_ijoin(args
->trans
, dp
, 0);
348 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
349 (map
.br_startblock
!= HOLESTARTBLOCK
));
350 lblkno
+= map
.br_blockcount
;
351 blkcnt
-= map
.br_blockcount
;
355 * If we have enough blocks for the attribute data, calculate
356 * how many extra blocks we need for headers. We might run
357 * through this multiple times in the case that the additional
358 * headers in the blocks needed for the data fragments spills
359 * into requiring more blocks. e.g. for 512 byte blocks, we'll
360 * spill for another block every 9 headers we require in this
363 if (crcs
&& blkcnt
== 0) {
366 total_len
= args
->valuelen
+
367 hdrcnt
* sizeof(struct xfs_attr3_rmt_hdr
);
368 blkcnt
= XFS_B_TO_FSB(mp
, total_len
);
369 blkcnt
-= args
->rmtblkcnt
;
370 args
->rmtblkcnt
+= blkcnt
;
374 * Start the next trans in the chain.
376 error
= xfs_trans_roll(&args
->trans
, dp
);
382 * Roll through the "value", copying the attribute value to the
383 * already-allocated blocks. Blocks are written synchronously
384 * so that we can know they are all on disk before we turn off
385 * the INCOMPLETE flag.
387 lblkno
= args
->rmtblkno
;
388 valuelen
= args
->valuelen
;
389 while (valuelen
> 0) {
394 * Try to remember where we decided to put the value.
396 xfs_bmap_init(args
->flist
, args
->firstblock
);
398 error
= xfs_bmapi_read(dp
, (xfs_fileoff_t
)lblkno
,
399 args
->rmtblkcnt
, &map
, &nmap
,
404 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
405 (map
.br_startblock
!= HOLESTARTBLOCK
));
407 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
408 blkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
410 bp
= xfs_buf_get(mp
->m_ddev_targp
, dblkno
, blkcnt
, 0);
413 bp
->b_ops
= &xfs_attr3_rmt_buf_ops
;
415 byte_cnt
= BBTOB(bp
->b_length
);
416 byte_cnt
= XFS_ATTR3_RMT_BUF_SPACE(mp
, byte_cnt
);
417 if (valuelen
< byte_cnt
)
421 buf
+= xfs_attr3_rmt_hdr_set(mp
, dp
->i_ino
, offset
,
423 memcpy(buf
, src
, byte_cnt
);
425 if (byte_cnt
< BBTOB(bp
->b_length
))
426 xfs_buf_zero(bp
, byte_cnt
,
427 BBTOB(bp
->b_length
) - byte_cnt
);
429 error
= xfs_bwrite(bp
); /* GROT: NOTE: synchronous write */
435 valuelen
-= byte_cnt
;
439 lblkno
+= map
.br_blockcount
;
441 ASSERT(valuelen
== 0);
447 * Remove the value associated with an attribute by deleting the
448 * out-of-line buffer that it is stored on.
451 xfs_attr_rmtval_remove(xfs_da_args_t
*args
)
458 int valuelen
, blkcnt
, nmap
, error
, done
, committed
;
460 trace_xfs_attr_rmtval_remove(args
);
462 mp
= args
->dp
->i_mount
;
465 * Roll through the "value", invalidating the attribute value's
468 lblkno
= args
->rmtblkno
;
469 valuelen
= args
->rmtblkcnt
;
470 while (valuelen
> 0) {
472 * Try to remember where we decided to put the value.
475 error
= xfs_bmapi_read(args
->dp
, (xfs_fileoff_t
)lblkno
,
476 args
->rmtblkcnt
, &map
, &nmap
,
481 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
482 (map
.br_startblock
!= HOLESTARTBLOCK
));
484 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
485 blkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
488 * If the "remote" value is in the cache, remove it.
490 bp
= xfs_incore(mp
->m_ddev_targp
, dblkno
, blkcnt
, XBF_TRYLOCK
);
497 valuelen
-= map
.br_blockcount
;
499 lblkno
+= map
.br_blockcount
;
503 * Keep de-allocating extents until the remote-value region is gone.
505 lblkno
= args
->rmtblkno
;
506 blkcnt
= args
->rmtblkcnt
;
509 xfs_bmap_init(args
->flist
, args
->firstblock
);
510 error
= xfs_bunmapi(args
->trans
, args
->dp
, lblkno
, blkcnt
,
511 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
512 1, args
->firstblock
, args
->flist
,
515 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
521 xfs_bmap_cancel(args
->flist
);
526 * bmap_finish() may have committed the last trans and started
527 * a new one. We need the inode to be in all transactions.
530 xfs_trans_ijoin(args
->trans
, args
->dp
, 0);
533 * Close out trans and start the next one in the chain.
535 error
= xfs_trans_roll(&args
->trans
, args
->dp
);