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