blob: d8bcb2d742d18a59692efe09ab05c8593ec3c567 [file] [log] [blame]
Dave Chinner95920cd2013-04-03 16:11:27 +11001/*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
Dave Chinnerd2e448d2013-04-03 16:11:28 +11003 * Copyright (c) 2013 Red Hat, Inc.
Dave Chinner95920cd2013-04-03 16:11:27 +11004 * 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"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
27#include "xfs_mount.h"
28#include "xfs_error.h"
29#include "xfs_da_btree.h"
30#include "xfs_bmap_btree.h"
31#include "xfs_dinode.h"
32#include "xfs_inode.h"
33#include "xfs_alloc.h"
34#include "xfs_inode_item.h"
35#include "xfs_bmap.h"
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"
Dave Chinnerd2e448d2013-04-03 16:11:28 +110041#include "xfs_cksum.h"
42#include "xfs_buf_item.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110043
44#define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
45
46/*
Dave Chinnerd2e448d2013-04-03 16:11:28 +110047 * Each contiguous block has a header, so it is not just a simple attribute
48 * length to FSB conversion.
49 */
50static int
51xfs_attr3_rmt_blocks(
52 struct xfs_mount *mp,
53 int attrlen)
54{
Dave Chinner551b3822013-05-21 18:02:02 +100055 if (xfs_sb_version_hascrc(&mp->m_sb)) {
56 int buflen = XFS_ATTR3_RMT_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
57 return (attrlen + buflen - 1) / buflen;
58 }
59 return XFS_B_TO_FSB(mp, attrlen);
Dave Chinnerd2e448d2013-04-03 16:11:28 +110060}
61
62static bool
63xfs_attr3_rmt_verify(
64 struct xfs_buf *bp)
65{
66 struct xfs_mount *mp = bp->b_target->bt_mount;
67 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
68
69 if (!xfs_sb_version_hascrc(&mp->m_sb))
70 return false;
71 if (rmt->rm_magic != cpu_to_be32(XFS_ATTR3_RMT_MAGIC))
72 return false;
73 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_uuid))
74 return false;
75 if (bp->b_bn != be64_to_cpu(rmt->rm_blkno))
76 return false;
77 if (be32_to_cpu(rmt->rm_offset) +
Dave Chinner946217b2013-04-30 21:39:35 +100078 be32_to_cpu(rmt->rm_bytes) >= XATTR_SIZE_MAX)
Dave Chinnerd2e448d2013-04-03 16:11:28 +110079 return false;
80 if (rmt->rm_owner == 0)
81 return false;
82
83 return true;
84}
85
86static void
87xfs_attr3_rmt_read_verify(
88 struct xfs_buf *bp)
89{
90 struct xfs_mount *mp = bp->b_target->bt_mount;
91
92 /* no verification of non-crc buffers */
93 if (!xfs_sb_version_hascrc(&mp->m_sb))
94 return;
95
96 if (!xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
97 XFS_ATTR3_RMT_CRC_OFF) ||
98 !xfs_attr3_rmt_verify(bp)) {
99 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
100 xfs_buf_ioerror(bp, EFSCORRUPTED);
101 }
102}
103
104static void
105xfs_attr3_rmt_write_verify(
106 struct xfs_buf *bp)
107{
108 struct xfs_mount *mp = bp->b_target->bt_mount;
109 struct xfs_buf_log_item *bip = bp->b_fspriv;
110
111 /* no verification of non-crc buffers */
112 if (!xfs_sb_version_hascrc(&mp->m_sb))
113 return;
114
115 if (!xfs_attr3_rmt_verify(bp)) {
116 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
117 xfs_buf_ioerror(bp, EFSCORRUPTED);
118 return;
119 }
120
121 if (bip) {
122 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
123 rmt->rm_lsn = cpu_to_be64(bip->bli_item.li_lsn);
124 }
125 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
126 XFS_ATTR3_RMT_CRC_OFF);
127}
128
129const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
130 .verify_read = xfs_attr3_rmt_read_verify,
131 .verify_write = xfs_attr3_rmt_write_verify,
132};
133
134static int
135xfs_attr3_rmt_hdr_set(
136 struct xfs_mount *mp,
137 xfs_ino_t ino,
138 uint32_t offset,
139 uint32_t size,
140 struct xfs_buf *bp)
141{
142 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
143
144 if (!xfs_sb_version_hascrc(&mp->m_sb))
145 return 0;
146
147 rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC);
148 rmt->rm_offset = cpu_to_be32(offset);
149 rmt->rm_bytes = cpu_to_be32(size);
150 uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_uuid);
151 rmt->rm_owner = cpu_to_be64(ino);
152 rmt->rm_blkno = cpu_to_be64(bp->b_bn);
153 bp->b_ops = &xfs_attr3_rmt_buf_ops;
154
155 return sizeof(struct xfs_attr3_rmt_hdr);
156}
157
158/*
159 * Checking of the remote attribute header is split into two parts. the verifier
160 * does CRC, location and bounds checking, the unpacking function checks the
161 * attribute parameters and owner.
162 */
163static bool
164xfs_attr3_rmt_hdr_ok(
165 struct xfs_mount *mp,
166 xfs_ino_t ino,
167 uint32_t offset,
168 uint32_t size,
169 struct xfs_buf *bp)
170{
171 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
172
173 if (offset != be32_to_cpu(rmt->rm_offset))
174 return false;
175 if (size != be32_to_cpu(rmt->rm_bytes))
176 return false;
177 if (ino != be64_to_cpu(rmt->rm_owner))
178 return false;
179
180 /* ok */
181 return true;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100182}
183
184/*
Dave Chinner95920cd2013-04-03 16:11:27 +1100185 * Read the value associated with an attribute from the out-of-line buffer
186 * that we stored it in.
187 */
188int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100189xfs_attr_rmtval_get(
190 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100191{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100192 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
193 struct xfs_mount *mp = args->dp->i_mount;
194 struct xfs_buf *bp;
195 xfs_daddr_t dblkno;
196 xfs_dablk_t lblkno = args->rmtblkno;
197 void *dst = args->value;
198 int valuelen = args->valuelen;
199 int nmap;
200 int error;
201 int blkcnt;
202 int i;
203 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100204
205 trace_xfs_attr_rmtval_get(args);
206
207 ASSERT(!(args->flags & ATTR_KERNOVAL));
208
Dave Chinner95920cd2013-04-03 16:11:27 +1100209 while (valuelen > 0) {
210 nmap = ATTR_RMTVALUE_MAPSIZE;
Dave Chinner551b3822013-05-21 18:02:02 +1000211 blkcnt = xfs_attr3_rmt_blocks(mp, valuelen);
Dave Chinner95920cd2013-04-03 16:11:27 +1100212 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
Dave Chinner551b3822013-05-21 18:02:02 +1000213 blkcnt, map, &nmap,
Dave Chinner95920cd2013-04-03 16:11:27 +1100214 XFS_BMAPI_ATTRFORK);
215 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100216 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100217 ASSERT(nmap >= 1);
218
219 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100220 int byte_cnt;
221 char *src;
222
Dave Chinner95920cd2013-04-03 16:11:27 +1100223 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
224 (map[i].br_startblock != HOLESTARTBLOCK));
225 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
226 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
227 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100228 dblkno, blkcnt, 0, &bp,
229 &xfs_attr3_rmt_buf_ops);
Dave Chinner95920cd2013-04-03 16:11:27 +1100230 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100231 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100232
Dave Chinner551b3822013-05-21 18:02:02 +1000233 byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, BBTOB(bp->b_length));
234 byte_cnt = min_t(int, valuelen, byte_cnt);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100235
236 src = bp->b_addr;
237 if (xfs_sb_version_hascrc(&mp->m_sb)) {
238 if (!xfs_attr3_rmt_hdr_ok(mp, args->dp->i_ino,
239 offset, byte_cnt, bp)) {
240 xfs_alert(mp,
241"remote attribute header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
242 offset, byte_cnt, args->dp->i_ino);
243 xfs_buf_relse(bp);
244 return EFSCORRUPTED;
245
246 }
247
248 src += sizeof(struct xfs_attr3_rmt_hdr);
249 }
250
251 memcpy(dst, src, byte_cnt);
Dave Chinner95920cd2013-04-03 16:11:27 +1100252 xfs_buf_relse(bp);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100253
254 offset += byte_cnt;
255 dst += byte_cnt;
256 valuelen -= byte_cnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100257
258 lblkno += map[i].br_blockcount;
259 }
260 }
261 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100262 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100263}
264
265/*
266 * Write the value associated with an attribute into the out-of-line buffer
267 * that we have defined for it.
268 */
269int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100270xfs_attr_rmtval_set(
271 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100272{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100273 struct xfs_inode *dp = args->dp;
274 struct xfs_mount *mp = dp->i_mount;
275 struct xfs_bmbt_irec map;
276 struct xfs_buf *bp;
277 xfs_daddr_t dblkno;
278 xfs_dablk_t lblkno;
279 xfs_fileoff_t lfileoff = 0;
280 void *src = args->value;
281 int blkcnt;
282 int valuelen;
283 int nmap;
284 int error;
285 int hdrcnt = 0;
286 bool crcs = xfs_sb_version_hascrc(&mp->m_sb);
287 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100288
289 trace_xfs_attr_rmtval_set(args);
290
Dave Chinner95920cd2013-04-03 16:11:27 +1100291 /*
292 * Find a "hole" in the attribute address space large enough for
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100293 * us to drop the new attribute's value into. Because CRC enable
294 * attributes have headers, we can't just do a straight byte to FSB
295 * conversion. We calculate the worst case block count in this case
296 * and we may not need that many, so we have to handle this when
297 * allocating the blocks below.
Dave Chinner95920cd2013-04-03 16:11:27 +1100298 */
Dave Chinner26f71442013-05-21 18:02:03 +1000299 blkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100300
Dave Chinner95920cd2013-04-03 16:11:27 +1100301 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
302 XFS_ATTR_FORK);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100303 if (error)
304 return error;
305
306 /* Start with the attribute data. We'll allocate the rest afterwards. */
307 if (crcs)
308 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
309
Dave Chinner95920cd2013-04-03 16:11:27 +1100310 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
311 args->rmtblkcnt = blkcnt;
312
313 /*
314 * Roll through the "value", allocating blocks on disk as required.
315 */
316 while (blkcnt > 0) {
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100317 int committed;
318
Dave Chinner95920cd2013-04-03 16:11:27 +1100319 /*
320 * Allocate a single extent, up to the size of the value.
321 */
322 xfs_bmap_init(args->flist, args->firstblock);
323 nmap = 1;
324 error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
325 blkcnt,
326 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
327 args->firstblock, args->total, &map, &nmap,
328 args->flist);
329 if (!error) {
330 error = xfs_bmap_finish(&args->trans, args->flist,
331 &committed);
332 }
333 if (error) {
334 ASSERT(committed);
335 args->trans = NULL;
336 xfs_bmap_cancel(args->flist);
337 return(error);
338 }
339
340 /*
341 * bmap_finish() may have committed the last trans and started
342 * a new one. We need the inode to be in all transactions.
343 */
344 if (committed)
345 xfs_trans_ijoin(args->trans, dp, 0);
346
347 ASSERT(nmap == 1);
348 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
349 (map.br_startblock != HOLESTARTBLOCK));
350 lblkno += map.br_blockcount;
351 blkcnt -= map.br_blockcount;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100352 hdrcnt++;
353
354 /*
355 * If we have enough blocks for the attribute data, calculate
356 * how many extra blocks we need for headers. We might run
357 * through this multiple times in the case that the additional
358 * headers in the blocks needed for the data fragments spills
359 * into requiring more blocks. e.g. for 512 byte blocks, we'll
360 * spill for another block every 9 headers we require in this
361 * loop.
Dave Chinner9531e2d2013-05-21 18:02:01 +1000362 *
363 * Note that this can result in contiguous allocation of blocks,
364 * so we don't use all the space we allocate for headers as we
365 * have one less header for each contiguous allocation that
366 * occurs in the map/write loop below.
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100367 */
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100368 if (crcs && blkcnt == 0) {
369 int total_len;
370
371 total_len = args->valuelen +
372 hdrcnt * sizeof(struct xfs_attr3_rmt_hdr);
373 blkcnt = XFS_B_TO_FSB(mp, total_len);
374 blkcnt -= args->rmtblkcnt;
375 args->rmtblkcnt += blkcnt;
376 }
Dave Chinner95920cd2013-04-03 16:11:27 +1100377
378 /*
379 * Start the next trans in the chain.
380 */
381 error = xfs_trans_roll(&args->trans, dp);
382 if (error)
383 return (error);
384 }
385
386 /*
387 * Roll through the "value", copying the attribute value to the
388 * already-allocated blocks. Blocks are written synchronously
389 * so that we can know they are all on disk before we turn off
390 * the INCOMPLETE flag.
391 */
392 lblkno = args->rmtblkno;
393 valuelen = args->valuelen;
Dave Chinner26f71442013-05-21 18:02:03 +1000394 blkcnt = args->rmtblkcnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100395 while (valuelen > 0) {
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100396 int byte_cnt;
Dave Chinner26f71442013-05-21 18:02:03 +1000397 int hdr_size;
398 int dblkcnt;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100399 char *buf;
Dave Chinner95920cd2013-04-03 16:11:27 +1100400
401 /*
402 * Try to remember where we decided to put the value.
403 */
404 xfs_bmap_init(args->flist, args->firstblock);
405 nmap = 1;
406 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
Dave Chinner26f71442013-05-21 18:02:03 +1000407 blkcnt, &map, &nmap,
Dave Chinner95920cd2013-04-03 16:11:27 +1100408 XFS_BMAPI_ATTRFORK);
409 if (error)
410 return(error);
411 ASSERT(nmap == 1);
412 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
413 (map.br_startblock != HOLESTARTBLOCK));
414
415 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
Dave Chinner26f71442013-05-21 18:02:03 +1000416 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100417
Dave Chinner26f71442013-05-21 18:02:03 +1000418 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0);
Dave Chinner95920cd2013-04-03 16:11:27 +1100419 if (!bp)
420 return ENOMEM;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100421 bp->b_ops = &xfs_attr3_rmt_buf_ops;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100422 buf = bp->b_addr;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100423
Dave Chinner26f71442013-05-21 18:02:03 +1000424 byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, BBTOB(bp->b_length));
425 byte_cnt = min_t(int, valuelen, byte_cnt);
426 hdr_size = xfs_attr3_rmt_hdr_set(mp, dp->i_ino, offset,
427 byte_cnt, bp);
428 ASSERT(hdr_size + byte_cnt <= BBTOB(bp->b_length));
429
430 memcpy(buf + hdr_size, src, byte_cnt);
431
432 if (byte_cnt + hdr_size < BBTOB(bp->b_length))
433 xfs_buf_zero(bp, byte_cnt + hdr_size,
434 BBTOB(bp->b_length) - byte_cnt - hdr_size);
Dave Chinner95920cd2013-04-03 16:11:27 +1100435
436 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
437 xfs_buf_relse(bp);
438 if (error)
439 return error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100440
441 src += byte_cnt;
442 valuelen -= byte_cnt;
443 offset += byte_cnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100444
445 lblkno += map.br_blockcount;
Dave Chinner26f71442013-05-21 18:02:03 +1000446 blkcnt -= map.br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100447 }
448 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100449 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100450}
451
452/*
453 * Remove the value associated with an attribute by deleting the
454 * out-of-line buffer that it is stored on.
455 */
456int
457xfs_attr_rmtval_remove(xfs_da_args_t *args)
458{
459 xfs_mount_t *mp;
460 xfs_bmbt_irec_t map;
461 xfs_buf_t *bp;
462 xfs_daddr_t dblkno;
463 xfs_dablk_t lblkno;
464 int valuelen, blkcnt, nmap, error, done, committed;
465
466 trace_xfs_attr_rmtval_remove(args);
467
468 mp = args->dp->i_mount;
469
470 /*
Dave Chinner58a72282013-05-21 18:02:04 +1000471 * Roll through the "value", invalidating the attribute value's blocks.
472 * Note that args->rmtblkcnt is the minimum number of data blocks we'll
473 * see for a CRC enabled remote attribute. Each extent will have a
474 * header, and so we may have more blocks than we realise here. If we
475 * fail to map the blocks correctly, we'll have problems with the buffer
476 * lookups.
Dave Chinner95920cd2013-04-03 16:11:27 +1100477 */
478 lblkno = args->rmtblkno;
Dave Chinner58a72282013-05-21 18:02:04 +1000479 valuelen = args->valuelen;
480 blkcnt = xfs_attr3_rmt_blocks(mp, valuelen);
Dave Chinner95920cd2013-04-03 16:11:27 +1100481 while (valuelen > 0) {
Dave Chinner58a72282013-05-21 18:02:04 +1000482 int dblkcnt;
483
Dave Chinner95920cd2013-04-03 16:11:27 +1100484 /*
485 * Try to remember where we decided to put the value.
486 */
487 nmap = 1;
488 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
Dave Chinner58a72282013-05-21 18:02:04 +1000489 blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
Dave Chinner95920cd2013-04-03 16:11:27 +1100490 if (error)
491 return(error);
492 ASSERT(nmap == 1);
493 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
494 (map.br_startblock != HOLESTARTBLOCK));
495
496 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
Dave Chinner58a72282013-05-21 18:02:04 +1000497 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100498
499 /*
500 * If the "remote" value is in the cache, remove it.
501 */
Dave Chinner58a72282013-05-21 18:02:04 +1000502 bp = xfs_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK);
Dave Chinner95920cd2013-04-03 16:11:27 +1100503 if (bp) {
504 xfs_buf_stale(bp);
505 xfs_buf_relse(bp);
506 bp = NULL;
507 }
508
Dave Chinner58a72282013-05-21 18:02:04 +1000509 valuelen -= XFS_ATTR3_RMT_BUF_SPACE(mp,
510 XFS_FSB_TO_B(mp, map.br_blockcount));
Dave Chinner95920cd2013-04-03 16:11:27 +1100511
512 lblkno += map.br_blockcount;
Dave Chinner58a72282013-05-21 18:02:04 +1000513 blkcnt -= map.br_blockcount;
514 blkcnt = max(blkcnt, xfs_attr3_rmt_blocks(mp, valuelen));
Dave Chinner95920cd2013-04-03 16:11:27 +1100515 }
516
517 /*
518 * Keep de-allocating extents until the remote-value region is gone.
519 */
Dave Chinner58a72282013-05-21 18:02:04 +1000520 blkcnt = lblkno - args->rmtblkno;
Dave Chinner95920cd2013-04-03 16:11:27 +1100521 lblkno = args->rmtblkno;
Dave Chinner95920cd2013-04-03 16:11:27 +1100522 done = 0;
523 while (!done) {
524 xfs_bmap_init(args->flist, args->firstblock);
525 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
526 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
527 1, args->firstblock, args->flist,
528 &done);
529 if (!error) {
530 error = xfs_bmap_finish(&args->trans, args->flist,
531 &committed);
532 }
533 if (error) {
534 ASSERT(committed);
535 args->trans = NULL;
536 xfs_bmap_cancel(args->flist);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100537 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100538 }
539
540 /*
541 * bmap_finish() may have committed the last trans and started
542 * a new one. We need the inode to be in all transactions.
543 */
544 if (committed)
545 xfs_trans_ijoin(args->trans, args->dp, 0);
546
547 /*
548 * Close out trans and start the next one in the chain.
549 */
550 error = xfs_trans_roll(&args->trans, args->dp);
551 if (error)
552 return (error);
553 }
554 return(0);
555}
556