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.
60 len
-= XFS_ATTR3_RMT_BUF_SPACE(mp
, mp
->m_sb
.sb_blocksize
);
70 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
71 struct xfs_attr3_rmt_hdr
*rmt
= bp
->b_addr
;
73 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
75 if (rmt
->rm_magic
!= cpu_to_be32(XFS_ATTR3_RMT_MAGIC
))
77 if (!uuid_equal(&rmt
->rm_uuid
, &mp
->m_sb
.sb_uuid
))
79 if (bp
->b_bn
!= be64_to_cpu(rmt
->rm_blkno
))
81 if (be32_to_cpu(rmt
->rm_offset
) +
82 be32_to_cpu(rmt
->rm_bytes
) >= MAXPATHLEN
)
84 if (rmt
->rm_owner
== 0)
91 xfs_attr3_rmt_read_verify(
94 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
96 /* no verification of non-crc buffers */
97 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
100 if (!xfs_verify_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
101 XFS_ATTR3_RMT_CRC_OFF
) ||
102 !xfs_attr3_rmt_verify(bp
)) {
103 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
104 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
109 xfs_attr3_rmt_write_verify(
112 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
113 struct xfs_buf_log_item
*bip
= bp
->b_fspriv
;
115 /* no verification of non-crc buffers */
116 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
119 if (!xfs_attr3_rmt_verify(bp
)) {
120 XFS_CORRUPTION_ERROR(__func__
, XFS_ERRLEVEL_LOW
, mp
, bp
->b_addr
);
121 xfs_buf_ioerror(bp
, EFSCORRUPTED
);
126 struct xfs_attr3_rmt_hdr
*rmt
= bp
->b_addr
;
127 rmt
->rm_lsn
= cpu_to_be64(bip
->bli_item
.li_lsn
);
129 xfs_update_cksum(bp
->b_addr
, BBTOB(bp
->b_length
),
130 XFS_ATTR3_RMT_CRC_OFF
);
133 const struct xfs_buf_ops xfs_attr3_rmt_buf_ops
= {
134 .verify_read
= xfs_attr3_rmt_read_verify
,
135 .verify_write
= xfs_attr3_rmt_write_verify
,
139 xfs_attr3_rmt_hdr_set(
140 struct xfs_mount
*mp
,
146 struct xfs_attr3_rmt_hdr
*rmt
= bp
->b_addr
;
148 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
151 rmt
->rm_magic
= cpu_to_be32(XFS_ATTR3_RMT_MAGIC
);
152 rmt
->rm_offset
= cpu_to_be32(offset
);
153 rmt
->rm_bytes
= cpu_to_be32(size
);
154 uuid_copy(&rmt
->rm_uuid
, &mp
->m_sb
.sb_uuid
);
155 rmt
->rm_owner
= cpu_to_be64(ino
);
156 rmt
->rm_blkno
= cpu_to_be64(bp
->b_bn
);
157 bp
->b_ops
= &xfs_attr3_rmt_buf_ops
;
159 return sizeof(struct xfs_attr3_rmt_hdr
);
163 * Checking of the remote attribute header is split into two parts. the verifier
164 * does CRC, location and bounds checking, the unpacking function checks the
165 * attribute parameters and owner.
168 xfs_attr3_rmt_hdr_ok(
169 struct xfs_mount
*mp
,
175 struct xfs_attr3_rmt_hdr
*rmt
= bp
->b_addr
;
177 if (offset
!= be32_to_cpu(rmt
->rm_offset
))
179 if (size
!= be32_to_cpu(rmt
->rm_bytes
))
181 if (ino
!= be64_to_cpu(rmt
->rm_owner
))
190 * Read the value associated with an attribute from the out-of-line buffer
191 * that we stored it in.
195 struct xfs_da_args
*args
)
197 struct xfs_bmbt_irec map
[ATTR_RMTVALUE_MAPSIZE
];
198 struct xfs_mount
*mp
= args
->dp
->i_mount
;
201 xfs_dablk_t lblkno
= args
->rmtblkno
;
202 void *dst
= args
->value
;
203 int valuelen
= args
->valuelen
;
210 trace_xfs_attr_rmtval_get(args
);
212 ASSERT(!(args
->flags
& ATTR_KERNOVAL
));
214 while (valuelen
> 0) {
215 nmap
= ATTR_RMTVALUE_MAPSIZE
;
216 error
= xfs_bmapi_read(args
->dp
, (xfs_fileoff_t
)lblkno
,
217 args
->rmtblkcnt
, map
, &nmap
,
223 for (i
= 0; (i
< nmap
) && (valuelen
> 0); i
++) {
227 ASSERT((map
[i
].br_startblock
!= DELAYSTARTBLOCK
) &&
228 (map
[i
].br_startblock
!= HOLESTARTBLOCK
));
229 dblkno
= XFS_FSB_TO_DADDR(mp
, map
[i
].br_startblock
);
230 blkcnt
= XFS_FSB_TO_BB(mp
, map
[i
].br_blockcount
);
231 error
= xfs_trans_read_buf(mp
, NULL
, mp
->m_ddev_targp
,
232 dblkno
, blkcnt
, 0, &bp
,
233 &xfs_attr3_rmt_buf_ops
);
237 byte_cnt
= min_t(int, valuelen
, BBTOB(bp
->b_length
));
238 byte_cnt
= XFS_ATTR3_RMT_BUF_SPACE(mp
, byte_cnt
);
241 if (xfs_sb_version_hascrc(&mp
->m_sb
)) {
242 if (!xfs_attr3_rmt_hdr_ok(mp
, args
->dp
->i_ino
,
243 offset
, byte_cnt
, bp
)) {
245 "remote attribute header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
246 offset
, byte_cnt
, args
->dp
->i_ino
);
252 src
+= sizeof(struct xfs_attr3_rmt_hdr
);
255 memcpy(dst
, src
, byte_cnt
);
260 valuelen
-= byte_cnt
;
262 lblkno
+= map
[i
].br_blockcount
;
265 ASSERT(valuelen
== 0);
270 * Write the value associated with an attribute into the out-of-line buffer
271 * that we have defined for it.
275 struct xfs_da_args
*args
)
277 struct xfs_inode
*dp
= args
->dp
;
278 struct xfs_mount
*mp
= dp
->i_mount
;
279 struct xfs_bmbt_irec map
;
283 xfs_fileoff_t lfileoff
= 0;
284 void *src
= args
->value
;
290 bool crcs
= xfs_sb_version_hascrc(&mp
->m_sb
);
293 trace_xfs_attr_rmtval_set(args
);
296 * Find a "hole" in the attribute address space large enough for
297 * us to drop the new attribute's value into. Because CRC enable
298 * attributes have headers, we can't just do a straight byte to FSB
299 * conversion. We calculate the worst case block count in this case
300 * and we may not need that many, so we have to handle this when
301 * allocating the blocks below.
304 blkcnt
= XFS_B_TO_FSB(mp
, args
->valuelen
);
306 blkcnt
= xfs_attr3_rmt_blocks(mp
, args
->valuelen
);
308 error
= xfs_bmap_first_unused(args
->trans
, args
->dp
, blkcnt
, &lfileoff
,
313 /* Start with the attribute data. We'll allocate the rest afterwards. */
315 blkcnt
= XFS_B_TO_FSB(mp
, args
->valuelen
);
317 args
->rmtblkno
= lblkno
= (xfs_dablk_t
)lfileoff
;
318 args
->rmtblkcnt
= blkcnt
;
321 * Roll through the "value", allocating blocks on disk as required.
327 * Allocate a single extent, up to the size of the value.
329 xfs_bmap_init(args
->flist
, args
->firstblock
);
331 error
= xfs_bmapi_write(args
->trans
, dp
, (xfs_fileoff_t
)lblkno
,
333 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
334 args
->firstblock
, args
->total
, &map
, &nmap
,
337 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
343 xfs_bmap_cancel(args
->flist
);
348 * bmap_finish() may have committed the last trans and started
349 * a new one. We need the inode to be in all transactions.
352 xfs_trans_ijoin(args
->trans
, dp
, 0);
355 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
356 (map
.br_startblock
!= HOLESTARTBLOCK
));
357 lblkno
+= map
.br_blockcount
;
358 blkcnt
-= map
.br_blockcount
;
362 * If we have enough blocks for the attribute data, calculate
363 * how many extra blocks we need for headers. We might run
364 * through this multiple times in the case that the additional
365 * headers in the blocks needed for the data fragments spills
366 * into requiring more blocks. e.g. for 512 byte blocks, we'll
367 * spill for another block every 9 headers we require in this
371 if (crcs
&& blkcnt
== 0) {
374 total_len
= args
->valuelen
+
375 hdrcnt
* sizeof(struct xfs_attr3_rmt_hdr
);
376 blkcnt
= XFS_B_TO_FSB(mp
, total_len
);
377 blkcnt
-= args
->rmtblkcnt
;
378 args
->rmtblkcnt
+= blkcnt
;
382 * Start the next trans in the chain.
384 error
= xfs_trans_roll(&args
->trans
, dp
);
390 * Roll through the "value", copying the attribute value to the
391 * already-allocated blocks. Blocks are written synchronously
392 * so that we can know they are all on disk before we turn off
393 * the INCOMPLETE flag.
395 lblkno
= args
->rmtblkno
;
396 valuelen
= args
->valuelen
;
397 while (valuelen
> 0) {
402 * Try to remember where we decided to put the value.
404 xfs_bmap_init(args
->flist
, args
->firstblock
);
406 error
= xfs_bmapi_read(dp
, (xfs_fileoff_t
)lblkno
,
407 args
->rmtblkcnt
, &map
, &nmap
,
412 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
413 (map
.br_startblock
!= HOLESTARTBLOCK
));
415 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
416 blkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
418 bp
= xfs_buf_get(mp
->m_ddev_targp
, dblkno
, blkcnt
, 0);
421 bp
->b_ops
= &xfs_attr3_rmt_buf_ops
;
423 byte_cnt
= BBTOB(bp
->b_length
);
424 byte_cnt
= XFS_ATTR3_RMT_BUF_SPACE(mp
, byte_cnt
);
425 if (valuelen
< byte_cnt
) {
430 buf
+= xfs_attr3_rmt_hdr_set(mp
, dp
->i_ino
, offset
,
432 memcpy(buf
, src
, byte_cnt
);
434 if (byte_cnt
< BBTOB(bp
->b_length
))
435 xfs_buf_zero(bp
, byte_cnt
,
436 BBTOB(bp
->b_length
) - byte_cnt
);
438 error
= xfs_bwrite(bp
); /* GROT: NOTE: synchronous write */
444 valuelen
-= byte_cnt
;
448 lblkno
+= map
.br_blockcount
;
450 ASSERT(valuelen
== 0);
456 * Remove the value associated with an attribute by deleting the
457 * out-of-line buffer that it is stored on.
460 xfs_attr_rmtval_remove(xfs_da_args_t
*args
)
467 int valuelen
, blkcnt
, nmap
, error
, done
, committed
;
469 trace_xfs_attr_rmtval_remove(args
);
471 mp
= args
->dp
->i_mount
;
474 * Roll through the "value", invalidating the attribute value's
477 lblkno
= args
->rmtblkno
;
478 valuelen
= args
->rmtblkcnt
;
479 while (valuelen
> 0) {
481 * Try to remember where we decided to put the value.
484 error
= xfs_bmapi_read(args
->dp
, (xfs_fileoff_t
)lblkno
,
485 args
->rmtblkcnt
, &map
, &nmap
,
490 ASSERT((map
.br_startblock
!= DELAYSTARTBLOCK
) &&
491 (map
.br_startblock
!= HOLESTARTBLOCK
));
493 dblkno
= XFS_FSB_TO_DADDR(mp
, map
.br_startblock
),
494 blkcnt
= XFS_FSB_TO_BB(mp
, map
.br_blockcount
);
497 * If the "remote" value is in the cache, remove it.
499 bp
= xfs_incore(mp
->m_ddev_targp
, dblkno
, blkcnt
, XBF_TRYLOCK
);
506 valuelen
-= map
.br_blockcount
;
508 lblkno
+= map
.br_blockcount
;
512 * Keep de-allocating extents until the remote-value region is gone.
514 lblkno
= args
->rmtblkno
;
515 blkcnt
= args
->rmtblkcnt
;
518 xfs_bmap_init(args
->flist
, args
->firstblock
);
519 error
= xfs_bunmapi(args
->trans
, args
->dp
, lblkno
, blkcnt
,
520 XFS_BMAPI_ATTRFORK
| XFS_BMAPI_METADATA
,
521 1, args
->firstblock
, args
->flist
,
524 error
= xfs_bmap_finish(&args
->trans
, args
->flist
,
530 xfs_bmap_cancel(args
->flist
);
535 * bmap_finish() may have committed the last trans and started
536 * a new one. We need the inode to be in all transactions.
539 xfs_trans_ijoin(args
->trans
, args
->dp
, 0);
542 * Close out trans and start the next one in the chain.
544 error
= xfs_trans_roll(&args
->trans
, args
->dp
);