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"
28 #include "xfs_mount.h"
29 #include "xfs_da_format.h"
30 #include "xfs_da_btree.h"
31 #include "xfs_inode.h"
32 #include "xfs_alloc.h"
33 #include "xfs_trans.h"
34 #include "xfs_inode_item.h"
36 #include "xfs_bmap_util.h"
38 #include "xfs_attr_leaf.h"
39 #include "xfs_attr_remote.h"
40 #include "xfs_trans_space.h"
41 #include "xfs_trace.h"
42 #include "xfs_cksum.h"
43 #include "xfs_buf_item.h"
44 #include "xfs_error.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;
130 /* no verification of non-crc buffers */
131 if (!xfs_sb_version_hascrc(&mp->m_sb))
136 len = BBTOB(bp->b_length);
137 ASSERT(len >= XFS_LBSIZE(mp));
140 if (!xfs_verify_cksum(ptr, XFS_LBSIZE(mp),
141 XFS_ATTR3_RMT_CRC_OFF)) {
142 xfs_buf_ioerror(bp, EFSBADCRC);
145 if (!xfs_attr3_rmt_verify(mp, ptr, XFS_LBSIZE(mp), bno)) {
146 xfs_buf_ioerror(bp, EFSCORRUPTED);
149 len -= XFS_LBSIZE(mp);
150 ptr += XFS_LBSIZE(mp);
155 xfs_verifier_error(bp);
161 xfs_attr3_rmt_write_verify(
164 struct xfs_mount *mp = bp->b_target->bt_mount;
165 struct xfs_buf_log_item *bip = bp->b_fspriv;
170 /* no verification of non-crc buffers */
171 if (!xfs_sb_version_hascrc(&mp->m_sb))
176 len = BBTOB(bp->b_length);
177 ASSERT(len >= XFS_LBSIZE(mp));
180 if (!xfs_attr3_rmt_verify(mp, ptr, XFS_LBSIZE(mp), bno)) {
181 xfs_buf_ioerror(bp, EFSCORRUPTED);
182 xfs_verifier_error(bp);
186 struct xfs_attr3_rmt_hdr *rmt;
188 rmt = (struct xfs_attr3_rmt_hdr *)ptr;
189 rmt->rm_lsn = cpu_to_be64(bip->bli_item.li_lsn);
191 xfs_update_cksum(ptr, XFS_LBSIZE(mp), XFS_ATTR3_RMT_CRC_OFF);
193 len -= XFS_LBSIZE(mp);
194 ptr += XFS_LBSIZE(mp);
200 const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
201 .verify_read = xfs_attr3_rmt_read_verify,
202 .verify_write = xfs_attr3_rmt_write_verify,
206 xfs_attr3_rmt_hdr_set(
207 struct xfs_mount *mp,
214 struct xfs_attr3_rmt_hdr *rmt = ptr;
216 if (!xfs_sb_version_hascrc(&mp->m_sb))
219 rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC);
220 rmt->rm_offset = cpu_to_be32(offset);
221 rmt->rm_bytes = cpu_to_be32(size);
222 uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_uuid);
223 rmt->rm_owner = cpu_to_be64(ino);
224 rmt->rm_blkno = cpu_to_be64(bno);
226 return sizeof(struct xfs_attr3_rmt_hdr);
230 * Helper functions to copy attribute data in and out of the one disk extents
233 xfs_attr_rmtval_copyout(
234 struct xfs_mount *mp,
241 char *src = bp->b_addr;
242 xfs_daddr_t bno = bp->b_bn;
243 int len = BBTOB(bp->b_length);
245 ASSERT(len >= XFS_LBSIZE(mp));
247 while (len > 0 && *valuelen > 0) {
249 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, XFS_LBSIZE(mp));
251 byte_cnt = min(*valuelen, byte_cnt);
253 if (xfs_sb_version_hascrc(&mp->m_sb)) {
254 if (!xfs_attr3_rmt_hdr_ok(mp, src, ino, *offset,
257 "remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)",
258 bno, *offset, byte_cnt, ino);
261 hdr_size = sizeof(struct xfs_attr3_rmt_hdr);
264 memcpy(*dst, src + hdr_size, byte_cnt);
266 /* roll buffer forwards */
267 len -= XFS_LBSIZE(mp);
268 src += XFS_LBSIZE(mp);
271 /* roll attribute data forwards */
272 *valuelen -= byte_cnt;
280 xfs_attr_rmtval_copyin(
281 struct xfs_mount *mp,
288 char *dst = bp->b_addr;
289 xfs_daddr_t bno = bp->b_bn;
290 int len = BBTOB(bp->b_length);
292 ASSERT(len >= XFS_LBSIZE(mp));
294 while (len > 0 && *valuelen > 0) {
296 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, XFS_LBSIZE(mp));
298 byte_cnt = min(*valuelen, byte_cnt);
299 hdr_size = xfs_attr3_rmt_hdr_set(mp, dst, ino, *offset,
302 memcpy(dst + hdr_size, *src, byte_cnt);
305 * If this is the last block, zero the remainder of it.
306 * Check that we are actually the last block, too.
308 if (byte_cnt + hdr_size < XFS_LBSIZE(mp)) {
309 ASSERT(*valuelen - byte_cnt == 0);
310 ASSERT(len == XFS_LBSIZE(mp));
311 memset(dst + hdr_size + byte_cnt, 0,
312 XFS_LBSIZE(mp) - hdr_size - byte_cnt);
315 /* roll buffer forwards */
316 len -= XFS_LBSIZE(mp);
317 dst += XFS_LBSIZE(mp);
320 /* roll attribute data forwards */
321 *valuelen -= byte_cnt;
328 * Read the value associated with an attribute from the out-of-line buffer
329 * that we stored it in.
333 struct xfs_da_args *args)
335 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
336 struct xfs_mount *mp = args->dp->i_mount;
338 xfs_dablk_t lblkno = args->rmtblkno;
339 __uint8_t *dst = args->value;
343 int blkcnt = args->rmtblkcnt;
347 trace_xfs_attr_rmtval_get(args);
349 ASSERT(!(args->flags & ATTR_KERNOVAL));
350 ASSERT(args->rmtvaluelen == args->valuelen);
352 valuelen = args->rmtvaluelen;
353 while (valuelen > 0) {
354 nmap = ATTR_RMTVALUE_MAPSIZE;
355 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
362 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
366 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
367 (map[i].br_startblock != HOLESTARTBLOCK));
368 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
369 dblkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
370 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
371 dblkno, dblkcnt, 0, &bp,
372 &xfs_attr3_rmt_buf_ops);
376 error = xfs_attr_rmtval_copyout(mp, bp, args->dp->i_ino,
383 /* roll attribute extent map forwards */
384 lblkno += map[i].br_blockcount;
385 blkcnt -= map[i].br_blockcount;
388 ASSERT(valuelen == 0);
393 * Write the value associated with an attribute into the out-of-line buffer
394 * that we have defined for it.
398 struct xfs_da_args *args)
400 struct xfs_inode *dp = args->dp;
401 struct xfs_mount *mp = dp->i_mount;
402 struct xfs_bmbt_irec map;
404 xfs_fileoff_t lfileoff = 0;
405 __uint8_t *src = args->value;
412 trace_xfs_attr_rmtval_set(args);
415 * Find a "hole" in the attribute address space large enough for
416 * us to drop the new attribute's value into. Because CRC enable
417 * attributes have headers, we can't just do a straight byte to FSB
418 * conversion and have to take the header space into account.
420 blkcnt = xfs_attr3_rmt_blocks(mp, args->rmtvaluelen);
421 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
426 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
427 args->rmtblkcnt = blkcnt;
430 * Roll through the "value", allocating blocks on disk as required.
436 * Allocate a single extent, up to the size of the value.
438 xfs_bmap_init(args->flist, args->firstblock);
440 error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
442 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
443 args->firstblock, args->total, &map, &nmap,
446 error = xfs_bmap_finish(&args->trans, args->flist,
452 xfs_bmap_cancel(args->flist);
457 * bmap_finish() may have committed the last trans and started
458 * a new one. We need the inode to be in all transactions.
461 xfs_trans_ijoin(args->trans, dp, 0);
464 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
465 (map.br_startblock != HOLESTARTBLOCK));
466 lblkno += map.br_blockcount;
467 blkcnt -= map.br_blockcount;
470 * Start the next trans in the chain.
472 error = xfs_trans_roll(&args->trans, dp);
478 * Roll through the "value", copying the attribute value to the
479 * already-allocated blocks. Blocks are written synchronously
480 * so that we can know they are all on disk before we turn off
481 * the INCOMPLETE flag.
483 lblkno = args->rmtblkno;
484 blkcnt = args->rmtblkcnt;
485 valuelen = args->rmtvaluelen;
486 while (valuelen > 0) {
493 xfs_bmap_init(args->flist, args->firstblock);
495 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
501 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
502 (map.br_startblock != HOLESTARTBLOCK));
504 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
505 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
507 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0);
510 bp->b_ops = &xfs_attr3_rmt_buf_ops;
512 xfs_attr_rmtval_copyin(mp, bp, args->dp->i_ino, &offset,
515 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
521 /* roll attribute extent map forwards */
522 lblkno += map.br_blockcount;
523 blkcnt -= map.br_blockcount;
525 ASSERT(valuelen == 0);
530 * Remove the value associated with an attribute by deleting the
531 * out-of-line buffer that it is stored on.
534 xfs_attr_rmtval_remove(
535 struct xfs_da_args *args)
537 struct xfs_mount *mp = args->dp->i_mount;
543 trace_xfs_attr_rmtval_remove(args);
546 * Roll through the "value", invalidating the attribute value's blocks.
548 lblkno = args->rmtblkno;
549 blkcnt = args->rmtblkcnt;
551 struct xfs_bmbt_irec map;
558 * Try to remember where we decided to put the value.
561 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
562 blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
566 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
567 (map.br_startblock != HOLESTARTBLOCK));
569 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
570 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
573 * If the "remote" value is in the cache, remove it.
575 bp = xfs_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK);
582 lblkno += map.br_blockcount;
583 blkcnt -= map.br_blockcount;
587 * Keep de-allocating extents until the remote-value region is gone.
589 lblkno = args->rmtblkno;
590 blkcnt = args->rmtblkcnt;
595 xfs_bmap_init(args->flist, args->firstblock);
596 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
597 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
598 1, args->firstblock, args->flist,
601 error = xfs_bmap_finish(&args->trans, args->flist,
607 xfs_bmap_cancel(args->flist);
612 * bmap_finish() may have committed the last trans and started
613 * a new one. We need the inode to be in all transactions.
616 xfs_trans_ijoin(args->trans, args->dp, 0);
619 * Close out trans and start the next one in the chain.
621 error = xfs_trans_roll(&args->trans, args->dp);