aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2013-05-31 17:56:21 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2013-05-31 17:56:21 -0400
commit7cfb9532581ed3d0e542712be6f9ca5bc1c3b021 (patch)
tree3fc02a775bb5a2e7332bde94591733497b31c562 /fs
parente8d256aca05b6de163605a04f86e5827ef77899f (diff)
parent7bc0dc271e494e12be3afd3c6431e5216347c624 (diff)
Merge tag 'for-linus-v3.10-rc4-crc-xattr-fixes' of git://oss.sgi.com/xfs/xfs
Pull xfs extended attribute fixes for CRCs from Ben Myers: "Here are several fixes that are relevant on CRC enabled XFS filesystems. They are followed by a rework of the remote attribute code so that each block of the attribute contains a header with a CRC. Previously there was a CRC header per extent in the remote attribute code, but this was untenable because it was not possible to know how many extents would be allocated for the attribute until after the allocation has completed, due to the fragmentation of free space. This became complicated because the size of the headers needs to be added to the length of the payload to get the overall length required for the allocation. With a header per block, things are less complicated at the cost of a little space. I would have preferred to defer this and the rest of the CRC queue to 3.11 to mitigate risk for existing non-crc users in 3.10. Doing so would require setting a feature bit for the on-disk changes, and so I have been pressured into sending this pull request by Eric Sandeen and David Chinner from Red Hat. I'll send another pull request or two with the rest of the CRC queue next week. - Remove assert on count of remote attribute CRC headers - Fix the number of blocks read in for remote attributes - Zero remote attribute tails properly - Fix mapping of remote attribute buffers to have correct length - initialize temp leaf properly in xfs_attr3_leaf_unbalance, and xfs_attr3_leaf_compact - Rework remote atttributes to have a header per block" * tag 'for-linus-v3.10-rc4-crc-xattr-fixes' of git://oss.sgi.com/xfs/xfs: xfs: rework remote attr CRCs xfs: fully initialise temp leaf in xfs_attr3_leaf_compact xfs: fully initialise temp leaf in xfs_attr3_leaf_unbalance xfs: correctly map remote attr buffers during removal xfs: remote attribute tail zeroing does too much xfs: remote attribute read too short xfs: remote attribute allocation may be contiguous
Diffstat (limited to 'fs')
-rw-r--r--fs/xfs/xfs_attr_leaf.c71
-rw-r--r--fs/xfs/xfs_attr_remote.c408
-rw-r--r--fs/xfs/xfs_attr_remote.h10
-rw-r--r--fs/xfs/xfs_buf.c1
4 files changed, 307 insertions, 183 deletions
diff --git a/fs/xfs/xfs_attr_leaf.c b/fs/xfs/xfs_attr_leaf.c
index 0bce1b348580..d788302e506a 100644
--- a/fs/xfs/xfs_attr_leaf.c
+++ b/fs/xfs/xfs_attr_leaf.c
@@ -1412,7 +1412,7 @@ xfs_attr3_leaf_add_work(
1412 name_rmt->valuelen = 0; 1412 name_rmt->valuelen = 0;
1413 name_rmt->valueblk = 0; 1413 name_rmt->valueblk = 0;
1414 args->rmtblkno = 1; 1414 args->rmtblkno = 1;
1415 args->rmtblkcnt = XFS_B_TO_FSB(mp, args->valuelen); 1415 args->rmtblkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
1416 } 1416 }
1417 xfs_trans_log_buf(args->trans, bp, 1417 xfs_trans_log_buf(args->trans, bp,
1418 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index), 1418 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
@@ -1445,11 +1445,12 @@ xfs_attr3_leaf_add_work(
1445STATIC void 1445STATIC void
1446xfs_attr3_leaf_compact( 1446xfs_attr3_leaf_compact(
1447 struct xfs_da_args *args, 1447 struct xfs_da_args *args,
1448 struct xfs_attr3_icleaf_hdr *ichdr_d, 1448 struct xfs_attr3_icleaf_hdr *ichdr_dst,
1449 struct xfs_buf *bp) 1449 struct xfs_buf *bp)
1450{ 1450{
1451 xfs_attr_leafblock_t *leaf_s, *leaf_d; 1451 struct xfs_attr_leafblock *leaf_src;
1452 struct xfs_attr3_icleaf_hdr ichdr_s; 1452 struct xfs_attr_leafblock *leaf_dst;
1453 struct xfs_attr3_icleaf_hdr ichdr_src;
1453 struct xfs_trans *trans = args->trans; 1454 struct xfs_trans *trans = args->trans;
1454 struct xfs_mount *mp = trans->t_mountp; 1455 struct xfs_mount *mp = trans->t_mountp;
1455 char *tmpbuffer; 1456 char *tmpbuffer;
@@ -1457,29 +1458,38 @@ xfs_attr3_leaf_compact(
1457 trace_xfs_attr_leaf_compact(args); 1458 trace_xfs_attr_leaf_compact(args);
1458 1459
1459 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP); 1460 tmpbuffer = kmem_alloc(XFS_LBSIZE(mp), KM_SLEEP);
1460 ASSERT(tmpbuffer != NULL);
1461 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(mp)); 1461 memcpy(tmpbuffer, bp->b_addr, XFS_LBSIZE(mp));
1462 memset(bp->b_addr, 0, XFS_LBSIZE(mp)); 1462 memset(bp->b_addr, 0, XFS_LBSIZE(mp));
1463 leaf_src = (xfs_attr_leafblock_t *)tmpbuffer;
1464 leaf_dst = bp->b_addr;
1463 1465
1464 /* 1466 /*
1465 * Copy basic information 1467 * Copy the on-disk header back into the destination buffer to ensure
1468 * all the information in the header that is not part of the incore
1469 * header structure is preserved.
1466 */ 1470 */
1467 leaf_s = (xfs_attr_leafblock_t *)tmpbuffer; 1471 memcpy(bp->b_addr, tmpbuffer, xfs_attr3_leaf_hdr_size(leaf_src));
1468 leaf_d = bp->b_addr; 1472
1469 ichdr_s = *ichdr_d; /* struct copy */ 1473 /* Initialise the incore headers */
1470 ichdr_d->firstused = XFS_LBSIZE(mp); 1474 ichdr_src = *ichdr_dst; /* struct copy */
1471 ichdr_d->usedbytes = 0; 1475 ichdr_dst->firstused = XFS_LBSIZE(mp);
1472 ichdr_d->count = 0; 1476 ichdr_dst->usedbytes = 0;
1473 ichdr_d->holes = 0; 1477 ichdr_dst->count = 0;
1474 ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_s); 1478 ichdr_dst->holes = 0;
1475 ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base; 1479 ichdr_dst->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_src);
1480 ichdr_dst->freemap[0].size = ichdr_dst->firstused -
1481 ichdr_dst->freemap[0].base;
1482
1483
1484 /* write the header back to initialise the underlying buffer */
1485 xfs_attr3_leaf_hdr_to_disk(leaf_dst, ichdr_dst);
1476 1486
1477 /* 1487 /*
1478 * Copy all entry's in the same (sorted) order, 1488 * Copy all entry's in the same (sorted) order,
1479 * but allocate name/value pairs packed and in sequence. 1489 * but allocate name/value pairs packed and in sequence.
1480 */ 1490 */
1481 xfs_attr3_leaf_moveents(leaf_s, &ichdr_s, 0, leaf_d, ichdr_d, 0, 1491 xfs_attr3_leaf_moveents(leaf_src, &ichdr_src, 0, leaf_dst, ichdr_dst, 0,
1482 ichdr_s.count, mp); 1492 ichdr_src.count, mp);
1483 /* 1493 /*
1484 * this logs the entire buffer, but the caller must write the header 1494 * this logs the entire buffer, but the caller must write the header
1485 * back to the buffer when it is finished modifying it. 1495 * back to the buffer when it is finished modifying it.
@@ -2181,14 +2191,24 @@ xfs_attr3_leaf_unbalance(
2181 struct xfs_attr_leafblock *tmp_leaf; 2191 struct xfs_attr_leafblock *tmp_leaf;
2182 struct xfs_attr3_icleaf_hdr tmphdr; 2192 struct xfs_attr3_icleaf_hdr tmphdr;
2183 2193
2184 tmp_leaf = kmem_alloc(state->blocksize, KM_SLEEP); 2194 tmp_leaf = kmem_zalloc(state->blocksize, KM_SLEEP);
2185 memset(tmp_leaf, 0, state->blocksize);
2186 memset(&tmphdr, 0, sizeof(tmphdr));
2187 2195
2196 /*
2197 * Copy the header into the temp leaf so that all the stuff
2198 * not in the incore header is present and gets copied back in
2199 * once we've moved all the entries.
2200 */
2201 memcpy(tmp_leaf, save_leaf, xfs_attr3_leaf_hdr_size(save_leaf));
2202
2203 memset(&tmphdr, 0, sizeof(tmphdr));
2188 tmphdr.magic = savehdr.magic; 2204 tmphdr.magic = savehdr.magic;
2189 tmphdr.forw = savehdr.forw; 2205 tmphdr.forw = savehdr.forw;
2190 tmphdr.back = savehdr.back; 2206 tmphdr.back = savehdr.back;
2191 tmphdr.firstused = state->blocksize; 2207 tmphdr.firstused = state->blocksize;
2208
2209 /* write the header to the temp buffer to initialise it */
2210 xfs_attr3_leaf_hdr_to_disk(tmp_leaf, &tmphdr);
2211
2192 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr, 2212 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2193 drop_blk->bp, &drophdr)) { 2213 drop_blk->bp, &drophdr)) {
2194 xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0, 2214 xfs_attr3_leaf_moveents(drop_leaf, &drophdr, 0,
@@ -2334,8 +2354,9 @@ xfs_attr3_leaf_lookup_int(
2334 args->index = probe; 2354 args->index = probe;
2335 args->valuelen = be32_to_cpu(name_rmt->valuelen); 2355 args->valuelen = be32_to_cpu(name_rmt->valuelen);
2336 args->rmtblkno = be32_to_cpu(name_rmt->valueblk); 2356 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2337 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, 2357 args->rmtblkcnt = xfs_attr3_rmt_blocks(
2338 args->valuelen); 2358 args->dp->i_mount,
2359 args->valuelen);
2339 return XFS_ERROR(EEXIST); 2360 return XFS_ERROR(EEXIST);
2340 } 2361 }
2341 } 2362 }
@@ -2386,7 +2407,8 @@ xfs_attr3_leaf_getvalue(
2386 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0); 2407 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
2387 valuelen = be32_to_cpu(name_rmt->valuelen); 2408 valuelen = be32_to_cpu(name_rmt->valuelen);
2388 args->rmtblkno = be32_to_cpu(name_rmt->valueblk); 2409 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2389 args->rmtblkcnt = XFS_B_TO_FSB(args->dp->i_mount, valuelen); 2410 args->rmtblkcnt = xfs_attr3_rmt_blocks(args->dp->i_mount,
2411 valuelen);
2390 if (args->flags & ATTR_KERNOVAL) { 2412 if (args->flags & ATTR_KERNOVAL) {
2391 args->valuelen = valuelen; 2413 args->valuelen = valuelen;
2392 return 0; 2414 return 0;
@@ -2712,7 +2734,8 @@ xfs_attr3_leaf_list_int(
2712 args.valuelen = valuelen; 2734 args.valuelen = valuelen;
2713 args.value = kmem_alloc(valuelen, KM_SLEEP | KM_NOFS); 2735 args.value = kmem_alloc(valuelen, KM_SLEEP | KM_NOFS);
2714 args.rmtblkno = be32_to_cpu(name_rmt->valueblk); 2736 args.rmtblkno = be32_to_cpu(name_rmt->valueblk);
2715 args.rmtblkcnt = XFS_B_TO_FSB(args.dp->i_mount, valuelen); 2737 args.rmtblkcnt = xfs_attr3_rmt_blocks(
2738 args.dp->i_mount, valuelen);
2716 retval = xfs_attr_rmtval_get(&args); 2739 retval = xfs_attr_rmtval_get(&args);
2717 if (retval) 2740 if (retval)
2718 return retval; 2741 return retval;
diff --git a/fs/xfs/xfs_attr_remote.c b/fs/xfs/xfs_attr_remote.c
index dee84466dcc9..ef6b0c124528 100644
--- a/fs/xfs/xfs_attr_remote.c
+++ b/fs/xfs/xfs_attr_remote.c
@@ -47,22 +47,55 @@
47 * Each contiguous block has a header, so it is not just a simple attribute 47 * Each contiguous block has a header, so it is not just a simple attribute
48 * length to FSB conversion. 48 * length to FSB conversion.
49 */ 49 */
50static int 50int
51xfs_attr3_rmt_blocks( 51xfs_attr3_rmt_blocks(
52 struct xfs_mount *mp, 52 struct xfs_mount *mp,
53 int attrlen) 53 int attrlen)
54{ 54{
55 int buflen = XFS_ATTR3_RMT_BUF_SPACE(mp, 55 if (xfs_sb_version_hascrc(&mp->m_sb)) {
56 mp->m_sb.sb_blocksize); 56 int buflen = XFS_ATTR3_RMT_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
57 return (attrlen + buflen - 1) / buflen; 57 return (attrlen + buflen - 1) / buflen;
58 }
59 return XFS_B_TO_FSB(mp, attrlen);
60}
61
62/*
63 * Checking of the remote attribute header is split into two parts. The verifier
64 * does CRC, location and bounds checking, the unpacking function checks the
65 * attribute parameters and owner.
66 */
67static bool
68xfs_attr3_rmt_hdr_ok(
69 struct xfs_mount *mp,
70 void *ptr,
71 xfs_ino_t ino,
72 uint32_t offset,
73 uint32_t size,
74 xfs_daddr_t bno)
75{
76 struct xfs_attr3_rmt_hdr *rmt = ptr;
77
78 if (bno != be64_to_cpu(rmt->rm_blkno))
79 return false;
80 if (offset != be32_to_cpu(rmt->rm_offset))
81 return false;
82 if (size != be32_to_cpu(rmt->rm_bytes))
83 return false;
84 if (ino != be64_to_cpu(rmt->rm_owner))
85 return false;
86
87 /* ok */
88 return true;
58} 89}
59 90
60static bool 91static bool
61xfs_attr3_rmt_verify( 92xfs_attr3_rmt_verify(
62 struct xfs_buf *bp) 93 struct xfs_mount *mp,
94 void *ptr,
95 int fsbsize,
96 xfs_daddr_t bno)
63{ 97{
64 struct xfs_mount *mp = bp->b_target->bt_mount; 98 struct xfs_attr3_rmt_hdr *rmt = ptr;
65 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
66 99
67 if (!xfs_sb_version_hascrc(&mp->m_sb)) 100 if (!xfs_sb_version_hascrc(&mp->m_sb))
68 return false; 101 return false;
@@ -70,7 +103,9 @@ xfs_attr3_rmt_verify(
70 return false; 103 return false;
71 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_uuid)) 104 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_uuid))
72 return false; 105 return false;
73 if (bp->b_bn != be64_to_cpu(rmt->rm_blkno)) 106 if (be64_to_cpu(rmt->rm_blkno) != bno)
107 return false;
108 if (be32_to_cpu(rmt->rm_bytes) > fsbsize - sizeof(*rmt))
74 return false; 109 return false;
75 if (be32_to_cpu(rmt->rm_offset) + 110 if (be32_to_cpu(rmt->rm_offset) +
76 be32_to_cpu(rmt->rm_bytes) >= XATTR_SIZE_MAX) 111 be32_to_cpu(rmt->rm_bytes) >= XATTR_SIZE_MAX)
@@ -86,17 +121,40 @@ xfs_attr3_rmt_read_verify(
86 struct xfs_buf *bp) 121 struct xfs_buf *bp)
87{ 122{
88 struct xfs_mount *mp = bp->b_target->bt_mount; 123 struct xfs_mount *mp = bp->b_target->bt_mount;
124 char *ptr;
125 int len;
126 bool corrupt = false;
127 xfs_daddr_t bno;
89 128
90 /* no verification of non-crc buffers */ 129 /* no verification of non-crc buffers */
91 if (!xfs_sb_version_hascrc(&mp->m_sb)) 130 if (!xfs_sb_version_hascrc(&mp->m_sb))
92 return; 131 return;
93 132
94 if (!xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length), 133 ptr = bp->b_addr;
95 XFS_ATTR3_RMT_CRC_OFF) || 134 bno = bp->b_bn;
96 !xfs_attr3_rmt_verify(bp)) { 135 len = BBTOB(bp->b_length);
136 ASSERT(len >= XFS_LBSIZE(mp));
137
138 while (len > 0) {
139 if (!xfs_verify_cksum(ptr, XFS_LBSIZE(mp),
140 XFS_ATTR3_RMT_CRC_OFF)) {
141 corrupt = true;
142 break;
143 }
144 if (!xfs_attr3_rmt_verify(mp, ptr, XFS_LBSIZE(mp), bno)) {
145 corrupt = true;
146 break;
147 }
148 len -= XFS_LBSIZE(mp);
149 ptr += XFS_LBSIZE(mp);
150 bno += mp->m_bsize;
151 }
152
153 if (corrupt) {
97 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr); 154 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
98 xfs_buf_ioerror(bp, EFSCORRUPTED); 155 xfs_buf_ioerror(bp, EFSCORRUPTED);
99 } 156 } else
157 ASSERT(len == 0);
100} 158}
101 159
102static void 160static void
@@ -105,23 +163,39 @@ xfs_attr3_rmt_write_verify(
105{ 163{
106 struct xfs_mount *mp = bp->b_target->bt_mount; 164 struct xfs_mount *mp = bp->b_target->bt_mount;
107 struct xfs_buf_log_item *bip = bp->b_fspriv; 165 struct xfs_buf_log_item *bip = bp->b_fspriv;
166 char *ptr;
167 int len;
168 xfs_daddr_t bno;
108 169
109 /* no verification of non-crc buffers */ 170 /* no verification of non-crc buffers */
110 if (!xfs_sb_version_hascrc(&mp->m_sb)) 171 if (!xfs_sb_version_hascrc(&mp->m_sb))
111 return; 172 return;
112 173
113 if (!xfs_attr3_rmt_verify(bp)) { 174 ptr = bp->b_addr;
114 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr); 175 bno = bp->b_bn;
115 xfs_buf_ioerror(bp, EFSCORRUPTED); 176 len = BBTOB(bp->b_length);
116 return; 177 ASSERT(len >= XFS_LBSIZE(mp));
117 } 178
179 while (len > 0) {
180 if (!xfs_attr3_rmt_verify(mp, ptr, XFS_LBSIZE(mp), bno)) {
181 XFS_CORRUPTION_ERROR(__func__,
182 XFS_ERRLEVEL_LOW, mp, bp->b_addr);
183 xfs_buf_ioerror(bp, EFSCORRUPTED);
184 return;
185 }
186 if (bip) {
187 struct xfs_attr3_rmt_hdr *rmt;
188
189 rmt = (struct xfs_attr3_rmt_hdr *)ptr;
190 rmt->rm_lsn = cpu_to_be64(bip->bli_item.li_lsn);
191 }
192 xfs_update_cksum(ptr, XFS_LBSIZE(mp), XFS_ATTR3_RMT_CRC_OFF);
118 193
119 if (bip) { 194 len -= XFS_LBSIZE(mp);
120 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr; 195 ptr += XFS_LBSIZE(mp);
121 rmt->rm_lsn = cpu_to_be64(bip->bli_item.li_lsn); 196 bno += mp->m_bsize;
122 } 197 }
123 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length), 198 ASSERT(len == 0);
124 XFS_ATTR3_RMT_CRC_OFF);
125} 199}
126 200
127const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = { 201const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
@@ -129,15 +203,16 @@ const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
129 .verify_write = xfs_attr3_rmt_write_verify, 203 .verify_write = xfs_attr3_rmt_write_verify,
130}; 204};
131 205
132static int 206STATIC int
133xfs_attr3_rmt_hdr_set( 207xfs_attr3_rmt_hdr_set(
134 struct xfs_mount *mp, 208 struct xfs_mount *mp,
209 void *ptr,
135 xfs_ino_t ino, 210 xfs_ino_t ino,
136 uint32_t offset, 211 uint32_t offset,
137 uint32_t size, 212 uint32_t size,
138 struct xfs_buf *bp) 213 xfs_daddr_t bno)
139{ 214{
140 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr; 215 struct xfs_attr3_rmt_hdr *rmt = ptr;
141 216
142 if (!xfs_sb_version_hascrc(&mp->m_sb)) 217 if (!xfs_sb_version_hascrc(&mp->m_sb))
143 return 0; 218 return 0;
@@ -147,36 +222,107 @@ xfs_attr3_rmt_hdr_set(
147 rmt->rm_bytes = cpu_to_be32(size); 222 rmt->rm_bytes = cpu_to_be32(size);
148 uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_uuid); 223 uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_uuid);
149 rmt->rm_owner = cpu_to_be64(ino); 224 rmt->rm_owner = cpu_to_be64(ino);
150 rmt->rm_blkno = cpu_to_be64(bp->b_bn); 225 rmt->rm_blkno = cpu_to_be64(bno);
151 bp->b_ops = &xfs_attr3_rmt_buf_ops;
152 226
153 return sizeof(struct xfs_attr3_rmt_hdr); 227 return sizeof(struct xfs_attr3_rmt_hdr);
154} 228}
155 229
156/* 230/*
157 * Checking of the remote attribute header is split into two parts. the verifier 231 * Helper functions to copy attribute data in and out of the one disk extents
158 * does CRC, location and bounds checking, the unpacking function checks the
159 * attribute parameters and owner.
160 */ 232 */
161static bool 233STATIC int
162xfs_attr3_rmt_hdr_ok( 234xfs_attr_rmtval_copyout(
163 struct xfs_mount *mp, 235 struct xfs_mount *mp,
164 xfs_ino_t ino, 236 struct xfs_buf *bp,
165 uint32_t offset, 237 xfs_ino_t ino,
166 uint32_t size, 238 int *offset,
167 struct xfs_buf *bp) 239 int *valuelen,
240 char **dst)
168{ 241{
169 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr; 242 char *src = bp->b_addr;
243 xfs_daddr_t bno = bp->b_bn;
244 int len = BBTOB(bp->b_length);
170 245
171 if (offset != be32_to_cpu(rmt->rm_offset)) 246 ASSERT(len >= XFS_LBSIZE(mp));
172 return false;
173 if (size != be32_to_cpu(rmt->rm_bytes))
174 return false;
175 if (ino != be64_to_cpu(rmt->rm_owner))
176 return false;
177 247
178 /* ok */ 248 while (len > 0 && *valuelen > 0) {
179 return true; 249 int hdr_size = 0;
250 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, XFS_LBSIZE(mp));
251
252 byte_cnt = min_t(int, *valuelen, byte_cnt);
253
254 if (xfs_sb_version_hascrc(&mp->m_sb)) {
255 if (!xfs_attr3_rmt_hdr_ok(mp, src, ino, *offset,
256 byte_cnt, bno)) {
257 xfs_alert(mp,
258"remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)",
259 bno, *offset, byte_cnt, ino);
260 return EFSCORRUPTED;
261 }
262 hdr_size = sizeof(struct xfs_attr3_rmt_hdr);
263 }
264
265 memcpy(*dst, src + hdr_size, byte_cnt);
266
267 /* roll buffer forwards */
268 len -= XFS_LBSIZE(mp);
269 src += XFS_LBSIZE(mp);
270 bno += mp->m_bsize;
271
272 /* roll attribute data forwards */
273 *valuelen -= byte_cnt;
274 *dst += byte_cnt;
275 *offset += byte_cnt;
276 }
277 return 0;
278}
279
280STATIC void
281xfs_attr_rmtval_copyin(
282 struct xfs_mount *mp,
283 struct xfs_buf *bp,
284 xfs_ino_t ino,
285 int *offset,
286 int *valuelen,
287 char **src)
288{
289 char *dst = bp->b_addr;
290 xfs_daddr_t bno = bp->b_bn;
291 int len = BBTOB(bp->b_length);
292
293 ASSERT(len >= XFS_LBSIZE(mp));
294
295 while (len > 0 && *valuelen > 0) {
296 int hdr_size;
297 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, XFS_LBSIZE(mp));
298
299 byte_cnt = min(*valuelen, byte_cnt);
300 hdr_size = xfs_attr3_rmt_hdr_set(mp, dst, ino, *offset,
301 byte_cnt, bno);
302
303 memcpy(dst + hdr_size, *src, byte_cnt);
304
305 /*
306 * If this is the last block, zero the remainder of it.
307 * Check that we are actually the last block, too.
308 */
309 if (byte_cnt + hdr_size < XFS_LBSIZE(mp)) {
310 ASSERT(*valuelen - byte_cnt == 0);
311 ASSERT(len == XFS_LBSIZE(mp));
312 memset(dst + hdr_size + byte_cnt, 0,
313 XFS_LBSIZE(mp) - hdr_size - byte_cnt);
314 }
315
316 /* roll buffer forwards */
317 len -= XFS_LBSIZE(mp);
318 dst += XFS_LBSIZE(mp);
319 bno += mp->m_bsize;
320
321 /* roll attribute data forwards */
322 *valuelen -= byte_cnt;
323 *src += byte_cnt;
324 *offset += byte_cnt;
325 }
180} 326}
181 327
182/* 328/*
@@ -190,13 +336,12 @@ xfs_attr_rmtval_get(
190 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE]; 336 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
191 struct xfs_mount *mp = args->dp->i_mount; 337 struct xfs_mount *mp = args->dp->i_mount;
192 struct xfs_buf *bp; 338 struct xfs_buf *bp;
193 xfs_daddr_t dblkno;
194 xfs_dablk_t lblkno = args->rmtblkno; 339 xfs_dablk_t lblkno = args->rmtblkno;
195 void *dst = args->value; 340 char *dst = args->value;
196 int valuelen = args->valuelen; 341 int valuelen = args->valuelen;
197 int nmap; 342 int nmap;
198 int error; 343 int error;
199 int blkcnt; 344 int blkcnt = args->rmtblkcnt;
200 int i; 345 int i;
201 int offset = 0; 346 int offset = 0;
202 347
@@ -207,52 +352,36 @@ xfs_attr_rmtval_get(
207 while (valuelen > 0) { 352 while (valuelen > 0) {
208 nmap = ATTR_RMTVALUE_MAPSIZE; 353 nmap = ATTR_RMTVALUE_MAPSIZE;
209 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno, 354 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
210 args->rmtblkcnt, map, &nmap, 355 blkcnt, map, &nmap,
211 XFS_BMAPI_ATTRFORK); 356 XFS_BMAPI_ATTRFORK);
212 if (error) 357 if (error)
213 return error; 358 return error;
214 ASSERT(nmap >= 1); 359 ASSERT(nmap >= 1);
215 360
216 for (i = 0; (i < nmap) && (valuelen > 0); i++) { 361 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
217 int byte_cnt; 362 xfs_daddr_t dblkno;
218 char *src; 363 int dblkcnt;
219 364
220 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) && 365 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
221 (map[i].br_startblock != HOLESTARTBLOCK)); 366 (map[i].br_startblock != HOLESTARTBLOCK));
222 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock); 367 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
223 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount); 368 dblkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
224 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, 369 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
225 dblkno, blkcnt, 0, &bp, 370 dblkno, dblkcnt, 0, &bp,
226 &xfs_attr3_rmt_buf_ops); 371 &xfs_attr3_rmt_buf_ops);
227 if (error) 372 if (error)
228 return error; 373 return error;
229 374
230 byte_cnt = min_t(int, valuelen, BBTOB(bp->b_length)); 375 error = xfs_attr_rmtval_copyout(mp, bp, args->dp->i_ino,
231 byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, byte_cnt); 376 &offset, &valuelen,
232 377 &dst);
233 src = bp->b_addr;
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)) {
237 xfs_alert(mp,
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);
240 xfs_buf_relse(bp);
241 return EFSCORRUPTED;
242
243 }
244
245 src += sizeof(struct xfs_attr3_rmt_hdr);
246 }
247
248 memcpy(dst, src, byte_cnt);
249 xfs_buf_relse(bp); 378 xfs_buf_relse(bp);
379 if (error)
380 return error;
250 381
251 offset += byte_cnt; 382 /* roll attribute extent map forwards */
252 dst += byte_cnt;
253 valuelen -= byte_cnt;
254
255 lblkno += map[i].br_blockcount; 383 lblkno += map[i].br_blockcount;
384 blkcnt -= map[i].br_blockcount;
256 } 385 }
257 } 386 }
258 ASSERT(valuelen == 0); 387 ASSERT(valuelen == 0);
@@ -270,17 +399,13 @@ xfs_attr_rmtval_set(
270 struct xfs_inode *dp = args->dp; 399 struct xfs_inode *dp = args->dp;
271 struct xfs_mount *mp = dp->i_mount; 400 struct xfs_mount *mp = dp->i_mount;
272 struct xfs_bmbt_irec map; 401 struct xfs_bmbt_irec map;
273 struct xfs_buf *bp;
274 xfs_daddr_t dblkno;
275 xfs_dablk_t lblkno; 402 xfs_dablk_t lblkno;
276 xfs_fileoff_t lfileoff = 0; 403 xfs_fileoff_t lfileoff = 0;
277 void *src = args->value; 404 char *src = args->value;
278 int blkcnt; 405 int blkcnt;
279 int valuelen; 406 int valuelen;
280 int nmap; 407 int nmap;
281 int error; 408 int error;
282 int hdrcnt = 0;
283 bool crcs = xfs_sb_version_hascrc(&mp->m_sb);
284 int offset = 0; 409 int offset = 0;
285 410
286 trace_xfs_attr_rmtval_set(args); 411 trace_xfs_attr_rmtval_set(args);
@@ -289,24 +414,14 @@ xfs_attr_rmtval_set(
289 * Find a "hole" in the attribute address space large enough for 414 * Find a "hole" in the attribute address space large enough for
290 * us to drop the new attribute's value into. Because CRC enable 415 * 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 416 * 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 417 * conversion and have to take the header space into account.
293 * and we may not need that many, so we have to handle this when
294 * allocating the blocks below.
295 */ 418 */
296 if (!crcs) 419 blkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
297 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
298 else
299 blkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
300
301 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff, 420 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
302 XFS_ATTR_FORK); 421 XFS_ATTR_FORK);
303 if (error) 422 if (error)
304 return error; 423 return error;
305 424
306 /* Start with the attribute data. We'll allocate the rest afterwards. */
307 if (crcs)
308 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
309
310 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff; 425 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
311 args->rmtblkcnt = blkcnt; 426 args->rmtblkcnt = blkcnt;
312 427
@@ -349,26 +464,6 @@ xfs_attr_rmtval_set(
349 (map.br_startblock != HOLESTARTBLOCK)); 464 (map.br_startblock != HOLESTARTBLOCK));
350 lblkno += map.br_blockcount; 465 lblkno += map.br_blockcount;
351 blkcnt -= map.br_blockcount; 466 blkcnt -= map.br_blockcount;
352 hdrcnt++;
353
354 /*
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
361 * loop.
362 */
363 if (crcs && blkcnt == 0) {
364 int total_len;
365
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;
371 }
372 467
373 /* 468 /*
374 * Start the next trans in the chain. 469 * Start the next trans in the chain.
@@ -385,18 +480,19 @@ xfs_attr_rmtval_set(
385 * the INCOMPLETE flag. 480 * the INCOMPLETE flag.
386 */ 481 */
387 lblkno = args->rmtblkno; 482 lblkno = args->rmtblkno;
483 blkcnt = args->rmtblkcnt;
388 valuelen = args->valuelen; 484 valuelen = args->valuelen;
389 while (valuelen > 0) { 485 while (valuelen > 0) {
390 int byte_cnt; 486 struct xfs_buf *bp;
391 char *buf; 487 xfs_daddr_t dblkno;
488 int dblkcnt;
489
490 ASSERT(blkcnt > 0);
392 491
393 /*
394 * Try to remember where we decided to put the value.
395 */
396 xfs_bmap_init(args->flist, args->firstblock); 492 xfs_bmap_init(args->flist, args->firstblock);
397 nmap = 1; 493 nmap = 1;
398 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno, 494 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
399 args->rmtblkcnt, &map, &nmap, 495 blkcnt, &map, &nmap,
400 XFS_BMAPI_ATTRFORK); 496 XFS_BMAPI_ATTRFORK);
401 if (error) 497 if (error)
402 return(error); 498 return(error);
@@ -405,41 +501,27 @@ xfs_attr_rmtval_set(
405 (map.br_startblock != HOLESTARTBLOCK)); 501 (map.br_startblock != HOLESTARTBLOCK));
406 502
407 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock), 503 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
408 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount); 504 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
409 505
410 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, blkcnt, 0); 506 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0);
411 if (!bp) 507 if (!bp)
412 return ENOMEM; 508 return ENOMEM;
413 bp->b_ops = &xfs_attr3_rmt_buf_ops; 509 bp->b_ops = &xfs_attr3_rmt_buf_ops;
414 510
415 byte_cnt = BBTOB(bp->b_length); 511 xfs_attr_rmtval_copyin(mp, bp, args->dp->i_ino, &offset,
416 byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, byte_cnt); 512 &valuelen, &src);
417 if (valuelen < byte_cnt)
418 byte_cnt = valuelen;
419
420 buf = bp->b_addr;
421 buf += xfs_attr3_rmt_hdr_set(mp, dp->i_ino, offset,
422 byte_cnt, bp);
423 memcpy(buf, src, byte_cnt);
424
425 if (byte_cnt < BBTOB(bp->b_length))
426 xfs_buf_zero(bp, byte_cnt,
427 BBTOB(bp->b_length) - byte_cnt);
428 513
429 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */ 514 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
430 xfs_buf_relse(bp); 515 xfs_buf_relse(bp);
431 if (error) 516 if (error)
432 return error; 517 return error;
433 518
434 src += byte_cnt;
435 valuelen -= byte_cnt;
436 offset += byte_cnt;
437 hdrcnt--;
438 519
520 /* roll attribute extent map forwards */
439 lblkno += map.br_blockcount; 521 lblkno += map.br_blockcount;
522 blkcnt -= map.br_blockcount;
440 } 523 }
441 ASSERT(valuelen == 0); 524 ASSERT(valuelen == 0);
442 ASSERT(hdrcnt == 0);
443 return 0; 525 return 0;
444} 526}
445 527
@@ -448,33 +530,40 @@ xfs_attr_rmtval_set(
448 * out-of-line buffer that it is stored on. 530 * out-of-line buffer that it is stored on.
449 */ 531 */
450int 532int
451xfs_attr_rmtval_remove(xfs_da_args_t *args) 533xfs_attr_rmtval_remove(
534 struct xfs_da_args *args)
452{ 535{
453 xfs_mount_t *mp; 536 struct xfs_mount *mp = args->dp->i_mount;
454 xfs_bmbt_irec_t map; 537 xfs_dablk_t lblkno;
455 xfs_buf_t *bp; 538 int blkcnt;
456 xfs_daddr_t dblkno; 539 int error;
457 xfs_dablk_t lblkno; 540 int done;
458 int valuelen, blkcnt, nmap, error, done, committed;
459 541
460 trace_xfs_attr_rmtval_remove(args); 542 trace_xfs_attr_rmtval_remove(args);
461 543
462 mp = args->dp->i_mount;
463
464 /* 544 /*
465 * Roll through the "value", invalidating the attribute value's 545 * Roll through the "value", invalidating the attribute value's blocks.
466 * blocks. 546 * Note that args->rmtblkcnt is the minimum number of data blocks we'll
547 * see for a CRC enabled remote attribute. Each extent will have a
548 * header, and so we may have more blocks than we realise here. If we
549 * fail to map the blocks correctly, we'll have problems with the buffer
550 * lookups.
467 */ 551 */
468 lblkno = args->rmtblkno; 552 lblkno = args->rmtblkno;
469 valuelen = args->rmtblkcnt; 553 blkcnt = args->rmtblkcnt;
470 while (valuelen > 0) { 554 while (blkcnt > 0) {
555 struct xfs_bmbt_irec map;
556 struct xfs_buf *bp;
557 xfs_daddr_t dblkno;
558 int dblkcnt;
559 int nmap;
560
471 /* 561 /*
472 * Try to remember where we decided to put the value. 562 * Try to remember where we decided to put the value.
473 */ 563 */
474 nmap = 1; 564 nmap = 1;
475 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno, 565 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
476 args->rmtblkcnt, &map, &nmap, 566 blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
477 XFS_BMAPI_ATTRFORK);
478 if (error) 567 if (error)
479 return(error); 568 return(error);
480 ASSERT(nmap == 1); 569 ASSERT(nmap == 1);
@@ -482,21 +571,20 @@ xfs_attr_rmtval_remove(xfs_da_args_t *args)
482 (map.br_startblock != HOLESTARTBLOCK)); 571 (map.br_startblock != HOLESTARTBLOCK));
483 572
484 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock), 573 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
485 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount); 574 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
486 575
487 /* 576 /*
488 * If the "remote" value is in the cache, remove it. 577 * If the "remote" value is in the cache, remove it.
489 */ 578 */
490 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK); 579 bp = xfs_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK);
491 if (bp) { 580 if (bp) {
492 xfs_buf_stale(bp); 581 xfs_buf_stale(bp);
493 xfs_buf_relse(bp); 582 xfs_buf_relse(bp);
494 bp = NULL; 583 bp = NULL;
495 } 584 }
496 585
497 valuelen -= map.br_blockcount;
498
499 lblkno += map.br_blockcount; 586 lblkno += map.br_blockcount;
587 blkcnt -= map.br_blockcount;
500 } 588 }
501 589
502 /* 590 /*
@@ -506,6 +594,8 @@ xfs_attr_rmtval_remove(xfs_da_args_t *args)
506 blkcnt = args->rmtblkcnt; 594 blkcnt = args->rmtblkcnt;
507 done = 0; 595 done = 0;
508 while (!done) { 596 while (!done) {
597 int committed;
598
509 xfs_bmap_init(args->flist, args->firstblock); 599 xfs_bmap_init(args->flist, args->firstblock);
510 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt, 600 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
511 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA, 601 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
diff --git a/fs/xfs/xfs_attr_remote.h b/fs/xfs/xfs_attr_remote.h
index c7cca60a062a..92a8fd7977cc 100644
--- a/fs/xfs/xfs_attr_remote.h
+++ b/fs/xfs/xfs_attr_remote.h
@@ -20,6 +20,14 @@
20 20
21#define XFS_ATTR3_RMT_MAGIC 0x5841524d /* XARM */ 21#define XFS_ATTR3_RMT_MAGIC 0x5841524d /* XARM */
22 22
23/*
24 * There is one of these headers per filesystem block in a remote attribute.
25 * This is done to ensure there is a 1:1 mapping between the attribute value
26 * length and the number of blocks needed to store the attribute. This makes the
27 * verification of a buffer a little more complex, but greatly simplifies the
28 * allocation, reading and writing of these attributes as we don't have to guess
29 * the number of blocks needed to store the attribute data.
30 */
23struct xfs_attr3_rmt_hdr { 31struct xfs_attr3_rmt_hdr {
24 __be32 rm_magic; 32 __be32 rm_magic;
25 __be32 rm_offset; 33 __be32 rm_offset;
@@ -39,6 +47,8 @@ struct xfs_attr3_rmt_hdr {
39 47
40extern const struct xfs_buf_ops xfs_attr3_rmt_buf_ops; 48extern const struct xfs_buf_ops xfs_attr3_rmt_buf_ops;
41 49
50int xfs_attr3_rmt_blocks(struct xfs_mount *mp, int attrlen);
51
42int xfs_attr_rmtval_get(struct xfs_da_args *args); 52int xfs_attr_rmtval_get(struct xfs_da_args *args);
43int xfs_attr_rmtval_set(struct xfs_da_args *args); 53int xfs_attr_rmtval_set(struct xfs_da_args *args);
44int xfs_attr_rmtval_remove(struct xfs_da_args *args); 54int xfs_attr_rmtval_remove(struct xfs_da_args *args);
diff --git a/fs/xfs/xfs_buf.c b/fs/xfs/xfs_buf.c
index 0d2554299688..1b2472a46e46 100644
--- a/fs/xfs/xfs_buf.c
+++ b/fs/xfs/xfs_buf.c
@@ -513,6 +513,7 @@ _xfs_buf_find(
513 xfs_alert(btp->bt_mount, 513 xfs_alert(btp->bt_mount,
514 "%s: Block out of range: block 0x%llx, EOFS 0x%llx ", 514 "%s: Block out of range: block 0x%llx, EOFS 0x%llx ",
515 __func__, blkno, eofs); 515 __func__, blkno, eofs);
516 WARN_ON(1);
516 return NULL; 517 return NULL;
517 } 518 }
518 519