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