Commit | Line | Data |
---|---|---|
95920cd6 DC |
1 | /* |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. | |
d2e448d5 | 3 | * Copyright (c) 2013 Red Hat, Inc. |
95920cd6 DC |
4 | * All Rights Reserved. |
5 | * | |
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. | |
9 | * | |
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. | |
14 | * | |
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 | |
18 | */ | |
19 | #include "xfs.h" | |
20 | #include "xfs_fs.h" | |
632b89e8 | 21 | #include "xfs_shared.h" |
a4fbe6ab | 22 | #include "xfs_format.h" |
239880ef DC |
23 | #include "xfs_log_format.h" |
24 | #include "xfs_trans_resv.h" | |
95920cd6 | 25 | #include "xfs_bit.h" |
95920cd6 | 26 | #include "xfs_mount.h" |
3ab78df2 | 27 | #include "xfs_defer.h" |
57062787 | 28 | #include "xfs_da_format.h" |
95920cd6 | 29 | #include "xfs_da_btree.h" |
95920cd6 DC |
30 | #include "xfs_inode.h" |
31 | #include "xfs_alloc.h" | |
239880ef | 32 | #include "xfs_trans.h" |
95920cd6 DC |
33 | #include "xfs_inode_item.h" |
34 | #include "xfs_bmap.h" | |
68988114 | 35 | #include "xfs_bmap_util.h" |
95920cd6 DC |
36 | #include "xfs_attr.h" |
37 | #include "xfs_attr_leaf.h" | |
38 | #include "xfs_attr_remote.h" | |
39 | #include "xfs_trans_space.h" | |
40 | #include "xfs_trace.h" | |
d2e448d5 DC |
41 | #include "xfs_cksum.h" |
42 | #include "xfs_buf_item.h" | |
a4fbe6ab | 43 | #include "xfs_error.h" |
95920cd6 DC |
44 | |
45 | #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */ | |
46 | ||
d2e448d5 DC |
47 | /* |
48 | * Each contiguous block has a header, so it is not just a simple attribute | |
49 | * length to FSB conversion. | |
50 | */ | |
7bc0dc27 | 51 | int |
d2e448d5 DC |
52 | xfs_attr3_rmt_blocks( |
53 | struct xfs_mount *mp, | |
54 | int attrlen) | |
55 | { | |
551b382f DC |
56 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
57 | int buflen = XFS_ATTR3_RMT_BUF_SPACE(mp, mp->m_sb.sb_blocksize); | |
58 | return (attrlen + buflen - 1) / buflen; | |
59 | } | |
60 | return XFS_B_TO_FSB(mp, attrlen); | |
d2e448d5 DC |
61 | } |
62 | ||
7bc0dc27 DC |
63 | /* |
64 | * Checking of the remote attribute header is split into two parts. The verifier | |
65 | * does CRC, location and bounds checking, the unpacking function checks the | |
66 | * attribute parameters and owner. | |
67 | */ | |
68 | static bool | |
69 | xfs_attr3_rmt_hdr_ok( | |
7bc0dc27 DC |
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; | |
89 | } | |
90 | ||
d2e448d5 DC |
91 | static bool |
92 | xfs_attr3_rmt_verify( | |
7bc0dc27 DC |
93 | struct xfs_mount *mp, |
94 | void *ptr, | |
95 | int fsbsize, | |
96 | xfs_daddr_t bno) | |
d2e448d5 | 97 | { |
7bc0dc27 | 98 | struct xfs_attr3_rmt_hdr *rmt = ptr; |
d2e448d5 DC |
99 | |
100 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
101 | return false; | |
102 | if (rmt->rm_magic != cpu_to_be32(XFS_ATTR3_RMT_MAGIC)) | |
103 | return false; | |
ce748eaa | 104 | if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid)) |
d2e448d5 | 105 | return false; |
7bc0dc27 DC |
106 | if (be64_to_cpu(rmt->rm_blkno) != bno) |
107 | return false; | |
108 | if (be32_to_cpu(rmt->rm_bytes) > fsbsize - sizeof(*rmt)) | |
d2e448d5 DC |
109 | return false; |
110 | if (be32_to_cpu(rmt->rm_offset) + | |
51fcbfe7 | 111 | be32_to_cpu(rmt->rm_bytes) > XFS_XATTR_SIZE_MAX) |
d2e448d5 DC |
112 | return false; |
113 | if (rmt->rm_owner == 0) | |
114 | return false; | |
115 | ||
116 | return true; | |
117 | } | |
118 | ||
119 | static void | |
120 | xfs_attr3_rmt_read_verify( | |
121 | struct xfs_buf *bp) | |
122 | { | |
123 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
7bc0dc27 DC |
124 | char *ptr; |
125 | int len; | |
7bc0dc27 | 126 | xfs_daddr_t bno; |
c2c4c477 | 127 | int blksize = mp->m_attr_geo->blksize; |
d2e448d5 DC |
128 | |
129 | /* no verification of non-crc buffers */ | |
130 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
131 | return; | |
132 | ||
7bc0dc27 DC |
133 | ptr = bp->b_addr; |
134 | bno = bp->b_bn; | |
135 | len = BBTOB(bp->b_length); | |
c2c4c477 | 136 | ASSERT(len >= blksize); |
7bc0dc27 DC |
137 | |
138 | while (len > 0) { | |
c2c4c477 | 139 | if (!xfs_verify_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF)) { |
2451337d | 140 | xfs_buf_ioerror(bp, -EFSBADCRC); |
7bc0dc27 DC |
141 | break; |
142 | } | |
c2c4c477 | 143 | if (!xfs_attr3_rmt_verify(mp, ptr, blksize, bno)) { |
2451337d | 144 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
7bc0dc27 DC |
145 | break; |
146 | } | |
c2c4c477 DC |
147 | len -= blksize; |
148 | ptr += blksize; | |
149 | bno += BTOBB(blksize); | |
7bc0dc27 DC |
150 | } |
151 | ||
ce5028cf ES |
152 | if (bp->b_error) |
153 | xfs_verifier_error(bp); | |
154 | else | |
7bc0dc27 | 155 | ASSERT(len == 0); |
d2e448d5 DC |
156 | } |
157 | ||
158 | static void | |
159 | xfs_attr3_rmt_write_verify( | |
160 | struct xfs_buf *bp) | |
161 | { | |
162 | struct xfs_mount *mp = bp->b_target->bt_mount; | |
e3c32ee9 | 163 | int blksize = mp->m_attr_geo->blksize; |
7bc0dc27 DC |
164 | char *ptr; |
165 | int len; | |
166 | xfs_daddr_t bno; | |
d2e448d5 DC |
167 | |
168 | /* no verification of non-crc buffers */ | |
169 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
170 | return; | |
171 | ||
7bc0dc27 DC |
172 | ptr = bp->b_addr; |
173 | bno = bp->b_bn; | |
174 | len = BBTOB(bp->b_length); | |
c2c4c477 | 175 | ASSERT(len >= blksize); |
7bc0dc27 DC |
176 | |
177 | while (len > 0) { | |
e3c32ee9 DC |
178 | struct xfs_attr3_rmt_hdr *rmt = (struct xfs_attr3_rmt_hdr *)ptr; |
179 | ||
c2c4c477 | 180 | if (!xfs_attr3_rmt_verify(mp, ptr, blksize, bno)) { |
2451337d | 181 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
ce5028cf | 182 | xfs_verifier_error(bp); |
7bc0dc27 DC |
183 | return; |
184 | } | |
d2e448d5 | 185 | |
e3c32ee9 DC |
186 | /* |
187 | * Ensure we aren't writing bogus LSNs to disk. See | |
188 | * xfs_attr3_rmt_hdr_set() for the explanation. | |
189 | */ | |
190 | if (rmt->rm_lsn != cpu_to_be64(NULLCOMMITLSN)) { | |
191 | xfs_buf_ioerror(bp, -EFSCORRUPTED); | |
192 | xfs_verifier_error(bp); | |
193 | return; | |
7bc0dc27 | 194 | } |
c2c4c477 | 195 | xfs_update_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF); |
7bc0dc27 | 196 | |
c2c4c477 DC |
197 | len -= blksize; |
198 | ptr += blksize; | |
199 | bno += BTOBB(blksize); | |
d2e448d5 | 200 | } |
7bc0dc27 | 201 | ASSERT(len == 0); |
d2e448d5 DC |
202 | } |
203 | ||
204 | const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = { | |
233135b7 | 205 | .name = "xfs_attr3_rmt", |
d2e448d5 DC |
206 | .verify_read = xfs_attr3_rmt_read_verify, |
207 | .verify_write = xfs_attr3_rmt_write_verify, | |
208 | }; | |
209 | ||
7bc0dc27 | 210 | STATIC int |
d2e448d5 DC |
211 | xfs_attr3_rmt_hdr_set( |
212 | struct xfs_mount *mp, | |
7bc0dc27 | 213 | void *ptr, |
d2e448d5 DC |
214 | xfs_ino_t ino, |
215 | uint32_t offset, | |
216 | uint32_t size, | |
7bc0dc27 | 217 | xfs_daddr_t bno) |
d2e448d5 | 218 | { |
7bc0dc27 | 219 | struct xfs_attr3_rmt_hdr *rmt = ptr; |
d2e448d5 DC |
220 | |
221 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
222 | return 0; | |
223 | ||
224 | rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC); | |
225 | rmt->rm_offset = cpu_to_be32(offset); | |
226 | rmt->rm_bytes = cpu_to_be32(size); | |
ce748eaa | 227 | uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid); |
d2e448d5 | 228 | rmt->rm_owner = cpu_to_be64(ino); |
7bc0dc27 | 229 | rmt->rm_blkno = cpu_to_be64(bno); |
d2e448d5 | 230 | |
e3c32ee9 DC |
231 | /* |
232 | * Remote attribute blocks are written synchronously, so we don't | |
233 | * have an LSN that we can stamp in them that makes any sense to log | |
234 | * recovery. To ensure that log recovery handles overwrites of these | |
235 | * blocks sanely (i.e. once they've been freed and reallocated as some | |
236 | * other type of metadata) we need to ensure that the LSN has a value | |
237 | * that tells log recovery to ignore the LSN and overwrite the buffer | |
238 | * with whatever is in it's log. To do this, we use the magic | |
239 | * NULLCOMMITLSN to indicate that the LSN is invalid. | |
240 | */ | |
241 | rmt->rm_lsn = cpu_to_be64(NULLCOMMITLSN); | |
242 | ||
d2e448d5 DC |
243 | return sizeof(struct xfs_attr3_rmt_hdr); |
244 | } | |
245 | ||
246 | /* | |
7bc0dc27 | 247 | * Helper functions to copy attribute data in and out of the one disk extents |
d2e448d5 | 248 | */ |
7bc0dc27 DC |
249 | STATIC int |
250 | xfs_attr_rmtval_copyout( | |
251 | struct xfs_mount *mp, | |
252 | struct xfs_buf *bp, | |
253 | xfs_ino_t ino, | |
254 | int *offset, | |
255 | int *valuelen, | |
836a94ad | 256 | __uint8_t **dst) |
d2e448d5 | 257 | { |
7bc0dc27 DC |
258 | char *src = bp->b_addr; |
259 | xfs_daddr_t bno = bp->b_bn; | |
260 | int len = BBTOB(bp->b_length); | |
c2c4c477 | 261 | int blksize = mp->m_attr_geo->blksize; |
d2e448d5 | 262 | |
c2c4c477 | 263 | ASSERT(len >= blksize); |
d2e448d5 | 264 | |
7bc0dc27 DC |
265 | while (len > 0 && *valuelen > 0) { |
266 | int hdr_size = 0; | |
c2c4c477 | 267 | int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize); |
7bc0dc27 | 268 | |
c5c249b4 | 269 | byte_cnt = min(*valuelen, byte_cnt); |
7bc0dc27 DC |
270 | |
271 | if (xfs_sb_version_hascrc(&mp->m_sb)) { | |
6d0081a3 | 272 | if (!xfs_attr3_rmt_hdr_ok(src, ino, *offset, |
7bc0dc27 DC |
273 | byte_cnt, bno)) { |
274 | xfs_alert(mp, | |
275 | "remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)", | |
276 | bno, *offset, byte_cnt, ino); | |
2451337d | 277 | return -EFSCORRUPTED; |
7bc0dc27 DC |
278 | } |
279 | hdr_size = sizeof(struct xfs_attr3_rmt_hdr); | |
280 | } | |
281 | ||
282 | memcpy(*dst, src + hdr_size, byte_cnt); | |
283 | ||
284 | /* roll buffer forwards */ | |
c2c4c477 DC |
285 | len -= blksize; |
286 | src += blksize; | |
287 | bno += BTOBB(blksize); | |
7bc0dc27 DC |
288 | |
289 | /* roll attribute data forwards */ | |
290 | *valuelen -= byte_cnt; | |
291 | *dst += byte_cnt; | |
292 | *offset += byte_cnt; | |
293 | } | |
294 | return 0; | |
295 | } | |
296 | ||
297 | STATIC void | |
298 | xfs_attr_rmtval_copyin( | |
299 | struct xfs_mount *mp, | |
300 | struct xfs_buf *bp, | |
301 | xfs_ino_t ino, | |
302 | int *offset, | |
303 | int *valuelen, | |
836a94ad | 304 | __uint8_t **src) |
7bc0dc27 DC |
305 | { |
306 | char *dst = bp->b_addr; | |
307 | xfs_daddr_t bno = bp->b_bn; | |
308 | int len = BBTOB(bp->b_length); | |
c2c4c477 | 309 | int blksize = mp->m_attr_geo->blksize; |
7bc0dc27 | 310 | |
c2c4c477 | 311 | ASSERT(len >= blksize); |
7bc0dc27 DC |
312 | |
313 | while (len > 0 && *valuelen > 0) { | |
314 | int hdr_size; | |
c2c4c477 | 315 | int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize); |
7bc0dc27 DC |
316 | |
317 | byte_cnt = min(*valuelen, byte_cnt); | |
318 | hdr_size = xfs_attr3_rmt_hdr_set(mp, dst, ino, *offset, | |
319 | byte_cnt, bno); | |
320 | ||
321 | memcpy(dst + hdr_size, *src, byte_cnt); | |
322 | ||
323 | /* | |
324 | * If this is the last block, zero the remainder of it. | |
325 | * Check that we are actually the last block, too. | |
326 | */ | |
c2c4c477 | 327 | if (byte_cnt + hdr_size < blksize) { |
7bc0dc27 | 328 | ASSERT(*valuelen - byte_cnt == 0); |
c2c4c477 | 329 | ASSERT(len == blksize); |
7bc0dc27 | 330 | memset(dst + hdr_size + byte_cnt, 0, |
c2c4c477 | 331 | blksize - hdr_size - byte_cnt); |
7bc0dc27 DC |
332 | } |
333 | ||
334 | /* roll buffer forwards */ | |
c2c4c477 DC |
335 | len -= blksize; |
336 | dst += blksize; | |
337 | bno += BTOBB(blksize); | |
7bc0dc27 DC |
338 | |
339 | /* roll attribute data forwards */ | |
340 | *valuelen -= byte_cnt; | |
341 | *src += byte_cnt; | |
342 | *offset += byte_cnt; | |
343 | } | |
d2e448d5 DC |
344 | } |
345 | ||
95920cd6 DC |
346 | /* |
347 | * Read the value associated with an attribute from the out-of-line buffer | |
348 | * that we stored it in. | |
349 | */ | |
350 | int | |
d2e448d5 DC |
351 | xfs_attr_rmtval_get( |
352 | struct xfs_da_args *args) | |
95920cd6 | 353 | { |
d2e448d5 DC |
354 | struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE]; |
355 | struct xfs_mount *mp = args->dp->i_mount; | |
356 | struct xfs_buf *bp; | |
d2e448d5 | 357 | xfs_dablk_t lblkno = args->rmtblkno; |
836a94ad | 358 | __uint8_t *dst = args->value; |
8275cdd0 | 359 | int valuelen; |
d2e448d5 DC |
360 | int nmap; |
361 | int error; | |
7bc0dc27 | 362 | int blkcnt = args->rmtblkcnt; |
d2e448d5 DC |
363 | int i; |
364 | int offset = 0; | |
95920cd6 DC |
365 | |
366 | trace_xfs_attr_rmtval_get(args); | |
367 | ||
368 | ASSERT(!(args->flags & ATTR_KERNOVAL)); | |
8275cdd0 | 369 | ASSERT(args->rmtvaluelen == args->valuelen); |
95920cd6 | 370 | |
8275cdd0 | 371 | valuelen = args->rmtvaluelen; |
95920cd6 DC |
372 | while (valuelen > 0) { |
373 | nmap = ATTR_RMTVALUE_MAPSIZE; | |
374 | error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno, | |
551b382f | 375 | blkcnt, map, &nmap, |
95920cd6 DC |
376 | XFS_BMAPI_ATTRFORK); |
377 | if (error) | |
d2e448d5 | 378 | return error; |
95920cd6 DC |
379 | ASSERT(nmap >= 1); |
380 | ||
381 | for (i = 0; (i < nmap) && (valuelen > 0); i++) { | |
7bc0dc27 DC |
382 | xfs_daddr_t dblkno; |
383 | int dblkcnt; | |
d2e448d5 | 384 | |
95920cd6 DC |
385 | ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) && |
386 | (map[i].br_startblock != HOLESTARTBLOCK)); | |
387 | dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock); | |
7bc0dc27 | 388 | dblkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount); |
95920cd6 | 389 | error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, |
7bc0dc27 | 390 | dblkno, dblkcnt, 0, &bp, |
d2e448d5 | 391 | &xfs_attr3_rmt_buf_ops); |
95920cd6 | 392 | if (error) |
d2e448d5 DC |
393 | return error; |
394 | ||
7bc0dc27 DC |
395 | error = xfs_attr_rmtval_copyout(mp, bp, args->dp->i_ino, |
396 | &offset, &valuelen, | |
397 | &dst); | |
95920cd6 | 398 | xfs_buf_relse(bp); |
7bc0dc27 DC |
399 | if (error) |
400 | return error; | |
d2e448d5 | 401 | |
7bc0dc27 | 402 | /* roll attribute extent map forwards */ |
95920cd6 | 403 | lblkno += map[i].br_blockcount; |
7bc0dc27 | 404 | blkcnt -= map[i].br_blockcount; |
95920cd6 DC |
405 | } |
406 | } | |
407 | ASSERT(valuelen == 0); | |
d2e448d5 | 408 | return 0; |
95920cd6 DC |
409 | } |
410 | ||
411 | /* | |
412 | * Write the value associated with an attribute into the out-of-line buffer | |
413 | * that we have defined for it. | |
414 | */ | |
415 | int | |
d2e448d5 DC |
416 | xfs_attr_rmtval_set( |
417 | struct xfs_da_args *args) | |
95920cd6 | 418 | { |
d2e448d5 DC |
419 | struct xfs_inode *dp = args->dp; |
420 | struct xfs_mount *mp = dp->i_mount; | |
421 | struct xfs_bmbt_irec map; | |
d2e448d5 DC |
422 | xfs_dablk_t lblkno; |
423 | xfs_fileoff_t lfileoff = 0; | |
836a94ad | 424 | __uint8_t *src = args->value; |
d2e448d5 DC |
425 | int blkcnt; |
426 | int valuelen; | |
427 | int nmap; | |
428 | int error; | |
d2e448d5 | 429 | int offset = 0; |
95920cd6 DC |
430 | |
431 | trace_xfs_attr_rmtval_set(args); | |
432 | ||
95920cd6 DC |
433 | /* |
434 | * Find a "hole" in the attribute address space large enough for | |
d2e448d5 DC |
435 | * us to drop the new attribute's value into. Because CRC enable |
436 | * attributes have headers, we can't just do a straight byte to FSB | |
7bc0dc27 | 437 | * conversion and have to take the header space into account. |
95920cd6 | 438 | */ |
8275cdd0 | 439 | blkcnt = xfs_attr3_rmt_blocks(mp, args->rmtvaluelen); |
95920cd6 DC |
440 | error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff, |
441 | XFS_ATTR_FORK); | |
d2e448d5 DC |
442 | if (error) |
443 | return error; | |
444 | ||
95920cd6 DC |
445 | args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff; |
446 | args->rmtblkcnt = blkcnt; | |
447 | ||
448 | /* | |
449 | * Roll through the "value", allocating blocks on disk as required. | |
450 | */ | |
451 | while (blkcnt > 0) { | |
452 | /* | |
453 | * Allocate a single extent, up to the size of the value. | |
df150ed1 DC |
454 | * |
455 | * Note that we have to consider this a data allocation as we | |
456 | * write the remote attribute without logging the contents. | |
457 | * Hence we must ensure that we aren't using blocks that are on | |
458 | * the busy list so that we don't overwrite blocks which have | |
459 | * recently been freed but their transactions are not yet | |
460 | * committed to disk. If we overwrite the contents of a busy | |
461 | * extent and then crash then the block may not contain the | |
462 | * correct metadata after log recovery occurs. | |
95920cd6 | 463 | */ |
310a75a3 | 464 | xfs_defer_init(args->flist, args->firstblock); |
95920cd6 DC |
465 | nmap = 1; |
466 | error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno, | |
df150ed1 DC |
467 | blkcnt, XFS_BMAPI_ATTRFORK, args->firstblock, |
468 | args->total, &map, &nmap, args->flist); | |
f6106efa | 469 | if (!error) |
310a75a3 | 470 | error = xfs_defer_finish(&args->trans, args->flist, dp); |
95920cd6 | 471 | if (error) { |
95920cd6 | 472 | args->trans = NULL; |
310a75a3 | 473 | xfs_defer_cancel(args->flist); |
d99831ff | 474 | return error; |
95920cd6 DC |
475 | } |
476 | ||
95920cd6 DC |
477 | ASSERT(nmap == 1); |
478 | ASSERT((map.br_startblock != DELAYSTARTBLOCK) && | |
479 | (map.br_startblock != HOLESTARTBLOCK)); | |
480 | lblkno += map.br_blockcount; | |
481 | blkcnt -= map.br_blockcount; | |
482 | ||
483 | /* | |
484 | * Start the next trans in the chain. | |
485 | */ | |
486 | error = xfs_trans_roll(&args->trans, dp); | |
487 | if (error) | |
d99831ff | 488 | return error; |
95920cd6 DC |
489 | } |
490 | ||
491 | /* | |
492 | * Roll through the "value", copying the attribute value to the | |
493 | * already-allocated blocks. Blocks are written synchronously | |
494 | * so that we can know they are all on disk before we turn off | |
495 | * the INCOMPLETE flag. | |
496 | */ | |
497 | lblkno = args->rmtblkno; | |
26f71445 | 498 | blkcnt = args->rmtblkcnt; |
8275cdd0 | 499 | valuelen = args->rmtvaluelen; |
95920cd6 | 500 | while (valuelen > 0) { |
7bc0dc27 DC |
501 | struct xfs_buf *bp; |
502 | xfs_daddr_t dblkno; | |
503 | int dblkcnt; | |
504 | ||
505 | ASSERT(blkcnt > 0); | |
95920cd6 | 506 | |
310a75a3 | 507 | xfs_defer_init(args->flist, args->firstblock); |
95920cd6 DC |
508 | nmap = 1; |
509 | error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno, | |
26f71445 | 510 | blkcnt, &map, &nmap, |
95920cd6 DC |
511 | XFS_BMAPI_ATTRFORK); |
512 | if (error) | |
d99831ff | 513 | return error; |
95920cd6 DC |
514 | ASSERT(nmap == 1); |
515 | ASSERT((map.br_startblock != DELAYSTARTBLOCK) && | |
516 | (map.br_startblock != HOLESTARTBLOCK)); | |
517 | ||
518 | dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock), | |
26f71445 | 519 | dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount); |
95920cd6 | 520 | |
26f71445 | 521 | bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0); |
95920cd6 | 522 | if (!bp) |
2451337d | 523 | return -ENOMEM; |
d2e448d5 | 524 | bp->b_ops = &xfs_attr3_rmt_buf_ops; |
26f71445 | 525 | |
7bc0dc27 DC |
526 | xfs_attr_rmtval_copyin(mp, bp, args->dp->i_ino, &offset, |
527 | &valuelen, &src); | |
95920cd6 DC |
528 | |
529 | error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */ | |
530 | xfs_buf_relse(bp); | |
531 | if (error) | |
532 | return error; | |
d2e448d5 | 533 | |
95920cd6 | 534 | |
7bc0dc27 | 535 | /* roll attribute extent map forwards */ |
95920cd6 | 536 | lblkno += map.br_blockcount; |
26f71445 | 537 | blkcnt -= map.br_blockcount; |
95920cd6 DC |
538 | } |
539 | ASSERT(valuelen == 0); | |
d2e448d5 | 540 | return 0; |
95920cd6 DC |
541 | } |
542 | ||
543 | /* | |
544 | * Remove the value associated with an attribute by deleting the | |
545 | * out-of-line buffer that it is stored on. | |
546 | */ | |
547 | int | |
7bc0dc27 DC |
548 | xfs_attr_rmtval_remove( |
549 | struct xfs_da_args *args) | |
95920cd6 | 550 | { |
7bc0dc27 DC |
551 | struct xfs_mount *mp = args->dp->i_mount; |
552 | xfs_dablk_t lblkno; | |
553 | int blkcnt; | |
554 | int error; | |
555 | int done; | |
95920cd6 DC |
556 | |
557 | trace_xfs_attr_rmtval_remove(args); | |
558 | ||
95920cd6 | 559 | /* |
58a72281 | 560 | * Roll through the "value", invalidating the attribute value's blocks. |
95920cd6 DC |
561 | */ |
562 | lblkno = args->rmtblkno; | |
7bc0dc27 DC |
563 | blkcnt = args->rmtblkcnt; |
564 | while (blkcnt > 0) { | |
565 | struct xfs_bmbt_irec map; | |
566 | struct xfs_buf *bp; | |
567 | xfs_daddr_t dblkno; | |
568 | int dblkcnt; | |
569 | int nmap; | |
58a72281 | 570 | |
95920cd6 DC |
571 | /* |
572 | * Try to remember where we decided to put the value. | |
573 | */ | |
574 | nmap = 1; | |
575 | error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno, | |
58a72281 | 576 | blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK); |
95920cd6 | 577 | if (error) |
d99831ff | 578 | return error; |
95920cd6 DC |
579 | ASSERT(nmap == 1); |
580 | ASSERT((map.br_startblock != DELAYSTARTBLOCK) && | |
581 | (map.br_startblock != HOLESTARTBLOCK)); | |
582 | ||
583 | dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock), | |
58a72281 | 584 | dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount); |
95920cd6 DC |
585 | |
586 | /* | |
587 | * If the "remote" value is in the cache, remove it. | |
588 | */ | |
58a72281 | 589 | bp = xfs_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK); |
95920cd6 DC |
590 | if (bp) { |
591 | xfs_buf_stale(bp); | |
592 | xfs_buf_relse(bp); | |
593 | bp = NULL; | |
594 | } | |
595 | ||
95920cd6 | 596 | lblkno += map.br_blockcount; |
58a72281 | 597 | blkcnt -= map.br_blockcount; |
95920cd6 DC |
598 | } |
599 | ||
600 | /* | |
601 | * Keep de-allocating extents until the remote-value region is gone. | |
602 | */ | |
603 | lblkno = args->rmtblkno; | |
7bc0dc27 | 604 | blkcnt = args->rmtblkcnt; |
95920cd6 DC |
605 | done = 0; |
606 | while (!done) { | |
310a75a3 | 607 | xfs_defer_init(args->flist, args->firstblock); |
95920cd6 | 608 | error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt, |
ab7bb610 DC |
609 | XFS_BMAPI_ATTRFORK, 1, args->firstblock, |
610 | args->flist, &done); | |
f6106efa | 611 | if (!error) |
310a75a3 | 612 | error = xfs_defer_finish(&args->trans, args->flist, |
f6106efa | 613 | args->dp); |
95920cd6 | 614 | if (error) { |
95920cd6 | 615 | args->trans = NULL; |
310a75a3 | 616 | xfs_defer_cancel(args->flist); |
d2e448d5 | 617 | return error; |
95920cd6 DC |
618 | } |
619 | ||
95920cd6 DC |
620 | /* |
621 | * Close out trans and start the next one in the chain. | |
622 | */ | |
623 | error = xfs_trans_roll(&args->trans, args->dp); | |
624 | if (error) | |
d99831ff | 625 | return error; |
95920cd6 | 626 | } |
d99831ff | 627 | return 0; |
95920cd6 | 628 | } |