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