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_sb.h" |
95920cd6 | 27 | #include "xfs_mount.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; | |
104 | if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_uuid)) | |
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) + | |
bba719b5 | 111 | be32_to_cpu(rmt->rm_bytes) > 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; | |
163 | struct xfs_buf_log_item *bip = bp->b_fspriv; | |
7bc0dc27 DC |
164 | char *ptr; |
165 | int len; | |
166 | xfs_daddr_t bno; | |
c2c4c477 | 167 | int blksize = mp->m_attr_geo->blksize; |
d2e448d5 DC |
168 | |
169 | /* no verification of non-crc buffers */ | |
170 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
171 | return; | |
172 | ||
7bc0dc27 DC |
173 | ptr = bp->b_addr; |
174 | bno = bp->b_bn; | |
175 | len = BBTOB(bp->b_length); | |
c2c4c477 | 176 | ASSERT(len >= blksize); |
7bc0dc27 DC |
177 | |
178 | while (len > 0) { | |
c2c4c477 | 179 | if (!xfs_attr3_rmt_verify(mp, ptr, blksize, bno)) { |
2451337d | 180 | xfs_buf_ioerror(bp, -EFSCORRUPTED); |
ce5028cf | 181 | xfs_verifier_error(bp); |
7bc0dc27 DC |
182 | return; |
183 | } | |
184 | if (bip) { | |
185 | struct xfs_attr3_rmt_hdr *rmt; | |
d2e448d5 | 186 | |
7bc0dc27 DC |
187 | rmt = (struct xfs_attr3_rmt_hdr *)ptr; |
188 | rmt->rm_lsn = cpu_to_be64(bip->bli_item.li_lsn); | |
189 | } | |
c2c4c477 | 190 | xfs_update_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF); |
7bc0dc27 | 191 | |
c2c4c477 DC |
192 | len -= blksize; |
193 | ptr += blksize; | |
194 | bno += BTOBB(blksize); | |
d2e448d5 | 195 | } |
7bc0dc27 | 196 | ASSERT(len == 0); |
d2e448d5 DC |
197 | } |
198 | ||
199 | const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = { | |
200 | .verify_read = xfs_attr3_rmt_read_verify, | |
201 | .verify_write = xfs_attr3_rmt_write_verify, | |
202 | }; | |
203 | ||
7bc0dc27 | 204 | STATIC int |
d2e448d5 DC |
205 | xfs_attr3_rmt_hdr_set( |
206 | struct xfs_mount *mp, | |
7bc0dc27 | 207 | void *ptr, |
d2e448d5 DC |
208 | xfs_ino_t ino, |
209 | uint32_t offset, | |
210 | uint32_t size, | |
7bc0dc27 | 211 | xfs_daddr_t bno) |
d2e448d5 | 212 | { |
7bc0dc27 | 213 | struct xfs_attr3_rmt_hdr *rmt = ptr; |
d2e448d5 DC |
214 | |
215 | if (!xfs_sb_version_hascrc(&mp->m_sb)) | |
216 | return 0; | |
217 | ||
218 | rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC); | |
219 | rmt->rm_offset = cpu_to_be32(offset); | |
220 | rmt->rm_bytes = cpu_to_be32(size); | |
221 | uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_uuid); | |
222 | rmt->rm_owner = cpu_to_be64(ino); | |
7bc0dc27 | 223 | rmt->rm_blkno = cpu_to_be64(bno); |
d2e448d5 DC |
224 | |
225 | return sizeof(struct xfs_attr3_rmt_hdr); | |
226 | } | |
227 | ||
228 | /* | |
7bc0dc27 | 229 | * Helper functions to copy attribute data in and out of the one disk extents |
d2e448d5 | 230 | */ |
7bc0dc27 DC |
231 | STATIC int |
232 | xfs_attr_rmtval_copyout( | |
233 | struct xfs_mount *mp, | |
234 | struct xfs_buf *bp, | |
235 | xfs_ino_t ino, | |
236 | int *offset, | |
237 | int *valuelen, | |
836a94ad | 238 | __uint8_t **dst) |
d2e448d5 | 239 | { |
7bc0dc27 DC |
240 | char *src = bp->b_addr; |
241 | xfs_daddr_t bno = bp->b_bn; | |
242 | int len = BBTOB(bp->b_length); | |
c2c4c477 | 243 | int blksize = mp->m_attr_geo->blksize; |
d2e448d5 | 244 | |
c2c4c477 | 245 | ASSERT(len >= blksize); |
d2e448d5 | 246 | |
7bc0dc27 DC |
247 | while (len > 0 && *valuelen > 0) { |
248 | int hdr_size = 0; | |
c2c4c477 | 249 | int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize); |
7bc0dc27 | 250 | |
c5c249b4 | 251 | byte_cnt = min(*valuelen, byte_cnt); |
7bc0dc27 DC |
252 | |
253 | if (xfs_sb_version_hascrc(&mp->m_sb)) { | |
6d0081a3 | 254 | if (!xfs_attr3_rmt_hdr_ok(src, ino, *offset, |
7bc0dc27 DC |
255 | byte_cnt, bno)) { |
256 | xfs_alert(mp, | |
257 | "remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)", | |
258 | bno, *offset, byte_cnt, ino); | |
2451337d | 259 | return -EFSCORRUPTED; |
7bc0dc27 DC |
260 | } |
261 | hdr_size = sizeof(struct xfs_attr3_rmt_hdr); | |
262 | } | |
263 | ||
264 | memcpy(*dst, src + hdr_size, byte_cnt); | |
265 | ||
266 | /* roll buffer forwards */ | |
c2c4c477 DC |
267 | len -= blksize; |
268 | src += blksize; | |
269 | bno += BTOBB(blksize); | |
7bc0dc27 DC |
270 | |
271 | /* roll attribute data forwards */ | |
272 | *valuelen -= byte_cnt; | |
273 | *dst += byte_cnt; | |
274 | *offset += byte_cnt; | |
275 | } | |
276 | return 0; | |
277 | } | |
278 | ||
279 | STATIC void | |
280 | xfs_attr_rmtval_copyin( | |
281 | struct xfs_mount *mp, | |
282 | struct xfs_buf *bp, | |
283 | xfs_ino_t ino, | |
284 | int *offset, | |
285 | int *valuelen, | |
836a94ad | 286 | __uint8_t **src) |
7bc0dc27 DC |
287 | { |
288 | char *dst = bp->b_addr; | |
289 | xfs_daddr_t bno = bp->b_bn; | |
290 | int len = BBTOB(bp->b_length); | |
c2c4c477 | 291 | int blksize = mp->m_attr_geo->blksize; |
7bc0dc27 | 292 | |
c2c4c477 | 293 | ASSERT(len >= blksize); |
7bc0dc27 DC |
294 | |
295 | while (len > 0 && *valuelen > 0) { | |
296 | int hdr_size; | |
c2c4c477 | 297 | int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize); |
7bc0dc27 DC |
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 | */ | |
c2c4c477 | 309 | if (byte_cnt + hdr_size < blksize) { |
7bc0dc27 | 310 | ASSERT(*valuelen - byte_cnt == 0); |
c2c4c477 | 311 | ASSERT(len == blksize); |
7bc0dc27 | 312 | memset(dst + hdr_size + byte_cnt, 0, |
c2c4c477 | 313 | blksize - hdr_size - byte_cnt); |
7bc0dc27 DC |
314 | } |
315 | ||
316 | /* roll buffer forwards */ | |
c2c4c477 DC |
317 | len -= blksize; |
318 | dst += blksize; | |
319 | bno += BTOBB(blksize); | |
7bc0dc27 DC |
320 | |
321 | /* roll attribute data forwards */ | |
322 | *valuelen -= byte_cnt; | |
323 | *src += byte_cnt; | |
324 | *offset += byte_cnt; | |
325 | } | |
d2e448d5 DC |
326 | } |
327 | ||
95920cd6 DC |
328 | /* |
329 | * Read the value associated with an attribute from the out-of-line buffer | |
330 | * that we stored it in. | |
331 | */ | |
332 | int | |
d2e448d5 DC |
333 | xfs_attr_rmtval_get( |
334 | struct xfs_da_args *args) | |
95920cd6 | 335 | { |
d2e448d5 DC |
336 | struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE]; |
337 | struct xfs_mount *mp = args->dp->i_mount; | |
338 | struct xfs_buf *bp; | |
d2e448d5 | 339 | xfs_dablk_t lblkno = args->rmtblkno; |
836a94ad | 340 | __uint8_t *dst = args->value; |
8275cdd0 | 341 | int valuelen; |
d2e448d5 DC |
342 | int nmap; |
343 | int error; | |
7bc0dc27 | 344 | int blkcnt = args->rmtblkcnt; |
d2e448d5 DC |
345 | int i; |
346 | int offset = 0; | |
95920cd6 DC |
347 | |
348 | trace_xfs_attr_rmtval_get(args); | |
349 | ||
350 | ASSERT(!(args->flags & ATTR_KERNOVAL)); | |
8275cdd0 | 351 | ASSERT(args->rmtvaluelen == args->valuelen); |
95920cd6 | 352 | |
8275cdd0 | 353 | valuelen = args->rmtvaluelen; |
95920cd6 DC |
354 | while (valuelen > 0) { |
355 | nmap = ATTR_RMTVALUE_MAPSIZE; | |
356 | error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno, | |
551b382f | 357 | blkcnt, map, &nmap, |
95920cd6 DC |
358 | XFS_BMAPI_ATTRFORK); |
359 | if (error) | |
d2e448d5 | 360 | return error; |
95920cd6 DC |
361 | ASSERT(nmap >= 1); |
362 | ||
363 | for (i = 0; (i < nmap) && (valuelen > 0); i++) { | |
7bc0dc27 DC |
364 | xfs_daddr_t dblkno; |
365 | int dblkcnt; | |
d2e448d5 | 366 | |
95920cd6 DC |
367 | ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) && |
368 | (map[i].br_startblock != HOLESTARTBLOCK)); | |
369 | dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock); | |
7bc0dc27 | 370 | dblkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount); |
95920cd6 | 371 | error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, |
7bc0dc27 | 372 | dblkno, dblkcnt, 0, &bp, |
d2e448d5 | 373 | &xfs_attr3_rmt_buf_ops); |
95920cd6 | 374 | if (error) |
d2e448d5 DC |
375 | return error; |
376 | ||
7bc0dc27 DC |
377 | error = xfs_attr_rmtval_copyout(mp, bp, args->dp->i_ino, |
378 | &offset, &valuelen, | |
379 | &dst); | |
95920cd6 | 380 | xfs_buf_relse(bp); |
7bc0dc27 DC |
381 | if (error) |
382 | return error; | |
d2e448d5 | 383 | |
7bc0dc27 | 384 | /* roll attribute extent map forwards */ |
95920cd6 | 385 | lblkno += map[i].br_blockcount; |
7bc0dc27 | 386 | blkcnt -= map[i].br_blockcount; |
95920cd6 DC |
387 | } |
388 | } | |
389 | ASSERT(valuelen == 0); | |
d2e448d5 | 390 | return 0; |
95920cd6 DC |
391 | } |
392 | ||
393 | /* | |
394 | * Write the value associated with an attribute into the out-of-line buffer | |
395 | * that we have defined for it. | |
396 | */ | |
397 | int | |
d2e448d5 DC |
398 | xfs_attr_rmtval_set( |
399 | struct xfs_da_args *args) | |
95920cd6 | 400 | { |
d2e448d5 DC |
401 | struct xfs_inode *dp = args->dp; |
402 | struct xfs_mount *mp = dp->i_mount; | |
403 | struct xfs_bmbt_irec map; | |
d2e448d5 DC |
404 | xfs_dablk_t lblkno; |
405 | xfs_fileoff_t lfileoff = 0; | |
836a94ad | 406 | __uint8_t *src = args->value; |
d2e448d5 DC |
407 | int blkcnt; |
408 | int valuelen; | |
409 | int nmap; | |
410 | int error; | |
d2e448d5 | 411 | int offset = 0; |
95920cd6 DC |
412 | |
413 | trace_xfs_attr_rmtval_set(args); | |
414 | ||
95920cd6 DC |
415 | /* |
416 | * Find a "hole" in the attribute address space large enough for | |
d2e448d5 DC |
417 | * us to drop the new attribute's value into. Because CRC enable |
418 | * attributes have headers, we can't just do a straight byte to FSB | |
7bc0dc27 | 419 | * conversion and have to take the header space into account. |
95920cd6 | 420 | */ |
8275cdd0 | 421 | blkcnt = xfs_attr3_rmt_blocks(mp, args->rmtvaluelen); |
95920cd6 DC |
422 | error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff, |
423 | XFS_ATTR_FORK); | |
d2e448d5 DC |
424 | if (error) |
425 | return error; | |
426 | ||
95920cd6 DC |
427 | args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff; |
428 | args->rmtblkcnt = blkcnt; | |
429 | ||
430 | /* | |
431 | * Roll through the "value", allocating blocks on disk as required. | |
432 | */ | |
433 | while (blkcnt > 0) { | |
d2e448d5 DC |
434 | int committed; |
435 | ||
95920cd6 DC |
436 | /* |
437 | * Allocate a single extent, up to the size of the value. | |
438 | */ | |
439 | xfs_bmap_init(args->flist, args->firstblock); | |
440 | nmap = 1; | |
441 | error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno, | |
442 | blkcnt, | |
443 | XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA, | |
444 | args->firstblock, args->total, &map, &nmap, | |
445 | args->flist); | |
446 | if (!error) { | |
447 | error = xfs_bmap_finish(&args->trans, args->flist, | |
448 | &committed); | |
449 | } | |
450 | if (error) { | |
451 | ASSERT(committed); | |
452 | args->trans = NULL; | |
453 | xfs_bmap_cancel(args->flist); | |
d99831ff | 454 | return error; |
95920cd6 DC |
455 | } |
456 | ||
457 | /* | |
458 | * bmap_finish() may have committed the last trans and started | |
459 | * a new one. We need the inode to be in all transactions. | |
460 | */ | |
461 | if (committed) | |
462 | xfs_trans_ijoin(args->trans, dp, 0); | |
463 | ||
464 | ASSERT(nmap == 1); | |
465 | ASSERT((map.br_startblock != DELAYSTARTBLOCK) && | |
466 | (map.br_startblock != HOLESTARTBLOCK)); | |
467 | lblkno += map.br_blockcount; | |
468 | blkcnt -= map.br_blockcount; | |
469 | ||
470 | /* | |
471 | * Start the next trans in the chain. | |
472 | */ | |
473 | error = xfs_trans_roll(&args->trans, dp); | |
474 | if (error) | |
d99831ff | 475 | return error; |
95920cd6 DC |
476 | } |
477 | ||
478 | /* | |
479 | * Roll through the "value", copying the attribute value to the | |
480 | * already-allocated blocks. Blocks are written synchronously | |
481 | * so that we can know they are all on disk before we turn off | |
482 | * the INCOMPLETE flag. | |
483 | */ | |
484 | lblkno = args->rmtblkno; | |
26f71445 | 485 | blkcnt = args->rmtblkcnt; |
8275cdd0 | 486 | valuelen = args->rmtvaluelen; |
95920cd6 | 487 | while (valuelen > 0) { |
7bc0dc27 DC |
488 | struct xfs_buf *bp; |
489 | xfs_daddr_t dblkno; | |
490 | int dblkcnt; | |
491 | ||
492 | ASSERT(blkcnt > 0); | |
95920cd6 | 493 | |
95920cd6 DC |
494 | xfs_bmap_init(args->flist, args->firstblock); |
495 | nmap = 1; | |
496 | error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno, | |
26f71445 | 497 | blkcnt, &map, &nmap, |
95920cd6 DC |
498 | XFS_BMAPI_ATTRFORK); |
499 | if (error) | |
d99831ff | 500 | return error; |
95920cd6 DC |
501 | ASSERT(nmap == 1); |
502 | ASSERT((map.br_startblock != DELAYSTARTBLOCK) && | |
503 | (map.br_startblock != HOLESTARTBLOCK)); | |
504 | ||
505 | dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock), | |
26f71445 | 506 | dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount); |
95920cd6 | 507 | |
26f71445 | 508 | bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0); |
95920cd6 | 509 | if (!bp) |
2451337d | 510 | return -ENOMEM; |
d2e448d5 | 511 | bp->b_ops = &xfs_attr3_rmt_buf_ops; |
26f71445 | 512 | |
7bc0dc27 DC |
513 | xfs_attr_rmtval_copyin(mp, bp, args->dp->i_ino, &offset, |
514 | &valuelen, &src); | |
95920cd6 DC |
515 | |
516 | error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */ | |
517 | xfs_buf_relse(bp); | |
518 | if (error) | |
519 | return error; | |
d2e448d5 | 520 | |
95920cd6 | 521 | |
7bc0dc27 | 522 | /* roll attribute extent map forwards */ |
95920cd6 | 523 | lblkno += map.br_blockcount; |
26f71445 | 524 | blkcnt -= map.br_blockcount; |
95920cd6 DC |
525 | } |
526 | ASSERT(valuelen == 0); | |
d2e448d5 | 527 | return 0; |
95920cd6 DC |
528 | } |
529 | ||
530 | /* | |
531 | * Remove the value associated with an attribute by deleting the | |
532 | * out-of-line buffer that it is stored on. | |
533 | */ | |
534 | int | |
7bc0dc27 DC |
535 | xfs_attr_rmtval_remove( |
536 | struct xfs_da_args *args) | |
95920cd6 | 537 | { |
7bc0dc27 DC |
538 | struct xfs_mount *mp = args->dp->i_mount; |
539 | xfs_dablk_t lblkno; | |
540 | int blkcnt; | |
541 | int error; | |
542 | int done; | |
95920cd6 DC |
543 | |
544 | trace_xfs_attr_rmtval_remove(args); | |
545 | ||
95920cd6 | 546 | /* |
58a72281 | 547 | * Roll through the "value", invalidating the attribute value's blocks. |
95920cd6 DC |
548 | */ |
549 | lblkno = args->rmtblkno; | |
7bc0dc27 DC |
550 | blkcnt = args->rmtblkcnt; |
551 | while (blkcnt > 0) { | |
552 | struct xfs_bmbt_irec map; | |
553 | struct xfs_buf *bp; | |
554 | xfs_daddr_t dblkno; | |
555 | int dblkcnt; | |
556 | int nmap; | |
58a72281 | 557 | |
95920cd6 DC |
558 | /* |
559 | * Try to remember where we decided to put the value. | |
560 | */ | |
561 | nmap = 1; | |
562 | error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno, | |
58a72281 | 563 | blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK); |
95920cd6 | 564 | if (error) |
d99831ff | 565 | return error; |
95920cd6 DC |
566 | ASSERT(nmap == 1); |
567 | ASSERT((map.br_startblock != DELAYSTARTBLOCK) && | |
568 | (map.br_startblock != HOLESTARTBLOCK)); | |
569 | ||
570 | dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock), | |
58a72281 | 571 | dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount); |
95920cd6 DC |
572 | |
573 | /* | |
574 | * If the "remote" value is in the cache, remove it. | |
575 | */ | |
58a72281 | 576 | bp = xfs_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK); |
95920cd6 DC |
577 | if (bp) { |
578 | xfs_buf_stale(bp); | |
579 | xfs_buf_relse(bp); | |
580 | bp = NULL; | |
581 | } | |
582 | ||
95920cd6 | 583 | lblkno += map.br_blockcount; |
58a72281 | 584 | blkcnt -= map.br_blockcount; |
95920cd6 DC |
585 | } |
586 | ||
587 | /* | |
588 | * Keep de-allocating extents until the remote-value region is gone. | |
589 | */ | |
590 | lblkno = args->rmtblkno; | |
7bc0dc27 | 591 | blkcnt = args->rmtblkcnt; |
95920cd6 DC |
592 | done = 0; |
593 | while (!done) { | |
7bc0dc27 DC |
594 | int committed; |
595 | ||
95920cd6 DC |
596 | xfs_bmap_init(args->flist, args->firstblock); |
597 | error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt, | |
598 | XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA, | |
599 | 1, args->firstblock, args->flist, | |
600 | &done); | |
601 | if (!error) { | |
602 | error = xfs_bmap_finish(&args->trans, args->flist, | |
603 | &committed); | |
604 | } | |
605 | if (error) { | |
606 | ASSERT(committed); | |
607 | args->trans = NULL; | |
608 | xfs_bmap_cancel(args->flist); | |
d2e448d5 | 609 | return error; |
95920cd6 DC |
610 | } |
611 | ||
612 | /* | |
613 | * bmap_finish() may have committed the last trans and started | |
614 | * a new one. We need the inode to be in all transactions. | |
615 | */ | |
616 | if (committed) | |
617 | xfs_trans_ijoin(args->trans, args->dp, 0); | |
618 | ||
619 | /* | |
620 | * Close out trans and start the next one in the chain. | |
621 | */ | |
622 | error = xfs_trans_roll(&args->trans, args->dp); | |
623 | if (error) | |
d99831ff | 624 | return error; |
95920cd6 | 625 | } |
d99831ff | 626 | return 0; |
95920cd6 | 627 | } |