xfs: fully initialise temp leaf in xfs_attr3_leaf_compact
[deliverable/linux.git] / fs / xfs / xfs_attr_remote.c
CommitLineData
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"
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"
d2e448d5
DC
41#include "xfs_cksum.h"
42#include "xfs_buf_item.h"
95920cd6
DC
43
44#define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
45
d2e448d5
DC
46/*
47 * 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{
551b382f
DC
55 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);
d2e448d5
DC
60}
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) +
946217ba 78 be32_to_cpu(rmt->rm_bytes) >= XATTR_SIZE_MAX)
d2e448d5
DC
79 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;
d2e448d5
DC
182}
183
95920cd6
DC
184/*
185 * Read the value associated with an attribute from the out-of-line buffer
186 * that we stored it in.
187 */
188int
d2e448d5
DC
189xfs_attr_rmtval_get(
190 struct xfs_da_args *args)
95920cd6 191{
d2e448d5
DC
192 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;
95920cd6
DC
204
205 trace_xfs_attr_rmtval_get(args);
206
207 ASSERT(!(args->flags & ATTR_KERNOVAL));
208
95920cd6
DC
209 while (valuelen > 0) {
210 nmap = ATTR_RMTVALUE_MAPSIZE;
551b382f 211 blkcnt = xfs_attr3_rmt_blocks(mp, valuelen);
95920cd6 212 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
551b382f 213 blkcnt, map, &nmap,
95920cd6
DC
214 XFS_BMAPI_ATTRFORK);
215 if (error)
d2e448d5 216 return error;
95920cd6
DC
217 ASSERT(nmap >= 1);
218
219 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
d2e448d5
DC
220 int byte_cnt;
221 char *src;
222
95920cd6
DC
223 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,
d2e448d5
DC
228 dblkno, blkcnt, 0, &bp,
229 &xfs_attr3_rmt_buf_ops);
95920cd6 230 if (error)
d2e448d5
DC
231 return error;
232
551b382f
DC
233 byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, BBTOB(bp->b_length));
234 byte_cnt = min_t(int, valuelen, byte_cnt);
95920cd6 235
d2e448d5
DC
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);
95920cd6 252 xfs_buf_relse(bp);
d2e448d5
DC
253
254 offset += byte_cnt;
255 dst += byte_cnt;
256 valuelen -= byte_cnt;
95920cd6
DC
257
258 lblkno += map[i].br_blockcount;
259 }
260 }
261 ASSERT(valuelen == 0);
d2e448d5 262 return 0;
95920cd6
DC
263}
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
d2e448d5
DC
270xfs_attr_rmtval_set(
271 struct xfs_da_args *args)
95920cd6 272{
d2e448d5
DC
273 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;
95920cd6
DC
288
289 trace_xfs_attr_rmtval_set(args);
290
95920cd6
DC
291 /*
292 * Find a "hole" in the attribute address space large enough for
d2e448d5
DC
293 * 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.
95920cd6 298 */
26f71445 299 blkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
d2e448d5 300
95920cd6
DC
301 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
302 XFS_ATTR_FORK);
d2e448d5
DC
303 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
95920cd6
DC
310 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) {
d2e448d5
DC
317 int committed;
318
95920cd6
DC
319 /*
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;
d2e448d5
DC
352 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.
9531e2de
DC
362 *
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.
d2e448d5 367 */
d2e448d5
DC
368 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 }
95920cd6
DC
377
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;
26f71445 394 blkcnt = args->rmtblkcnt;
95920cd6 395 while (valuelen > 0) {
d2e448d5 396 int byte_cnt;
26f71445
DC
397 int hdr_size;
398 int dblkcnt;
d2e448d5 399 char *buf;
95920cd6
DC
400
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,
26f71445 407 blkcnt, &map, &nmap,
95920cd6
DC
408 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),
26f71445 416 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
95920cd6 417
26f71445 418 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0);
95920cd6
DC
419 if (!bp)
420 return ENOMEM;
d2e448d5 421 bp->b_ops = &xfs_attr3_rmt_buf_ops;
d2e448d5 422 buf = bp->b_addr;
26f71445
DC
423
424 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,
d2e448d5 427 byte_cnt, bp);
26f71445
DC
428 ASSERT(hdr_size + byte_cnt <= BBTOB(bp->b_length));
429
430 memcpy(buf + hdr_size, src, byte_cnt);
95920cd6 431
26f71445
DC
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);
95920cd6
DC
435
436 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
437 xfs_buf_relse(bp);
438 if (error)
439 return error;
d2e448d5
DC
440
441 src += byte_cnt;
442 valuelen -= byte_cnt;
443 offset += byte_cnt;
95920cd6
DC
444
445 lblkno += map.br_blockcount;
26f71445 446 blkcnt -= map.br_blockcount;
95920cd6
DC
447 }
448 ASSERT(valuelen == 0);
d2e448d5 449 return 0;
95920cd6
DC
450}
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 /*
58a72281
DC
471 * 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.
95920cd6
DC
477 */
478 lblkno = args->rmtblkno;
58a72281
DC
479 valuelen = args->valuelen;
480 blkcnt = xfs_attr3_rmt_blocks(mp, valuelen);
95920cd6 481 while (valuelen > 0) {
58a72281
DC
482 int dblkcnt;
483
95920cd6
DC
484 /*
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,
58a72281 489 blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
95920cd6
DC
490 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),
58a72281 497 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
95920cd6
DC
498
499 /*
500 * If the "remote" value is in the cache, remove it.
501 */
58a72281 502 bp = xfs_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK);
95920cd6
DC
503 if (bp) {
504 xfs_buf_stale(bp);
505 xfs_buf_relse(bp);
506 bp = NULL;
507 }
508
58a72281
DC
509 valuelen -= XFS_ATTR3_RMT_BUF_SPACE(mp,
510 XFS_FSB_TO_B(mp, map.br_blockcount));
95920cd6
DC
511
512 lblkno += map.br_blockcount;
58a72281
DC
513 blkcnt -= map.br_blockcount;
514 blkcnt = max(blkcnt, xfs_attr3_rmt_blocks(mp, valuelen));
95920cd6
DC
515 }
516
517 /*
518 * Keep de-allocating extents until the remote-value region is gone.
519 */
58a72281 520 blkcnt = lblkno - args->rmtblkno;
95920cd6 521 lblkno = args->rmtblkno;
95920cd6
DC
522 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);
d2e448d5 537 return error;
95920cd6
DC
538 }
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
This page took 0.218859 seconds and 5 git commands to generate.