blob: dee84466dcc934add7391861d6c83631a63bca62 [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 Chinner946217b2013-04-30 21:39:35 +100055 int buflen = XFS_ATTR3_RMT_BUF_SPACE(mp,
56 mp->m_sb.sb_blocksize);
57 return (attrlen + buflen - 1) / buflen;
Dave Chinnerd2e448d2013-04-03 16:11:28 +110058}
59
60static bool
61xfs_attr3_rmt_verify(
62 struct xfs_buf *bp)
63{
64 struct xfs_mount *mp = bp->b_target->bt_mount;
65 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
66
67 if (!xfs_sb_version_hascrc(&mp->m_sb))
68 return false;
69 if (rmt->rm_magic != cpu_to_be32(XFS_ATTR3_RMT_MAGIC))
70 return false;
71 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_uuid))
72 return false;
73 if (bp->b_bn != be64_to_cpu(rmt->rm_blkno))
74 return false;
75 if (be32_to_cpu(rmt->rm_offset) +
Dave Chinner946217b2013-04-30 21:39:35 +100076 be32_to_cpu(rmt->rm_bytes) >= XATTR_SIZE_MAX)
Dave Chinnerd2e448d2013-04-03 16:11:28 +110077 return false;
78 if (rmt->rm_owner == 0)
79 return false;
80
81 return true;
82}
83
84static void
85xfs_attr3_rmt_read_verify(
86 struct xfs_buf *bp)
87{
88 struct xfs_mount *mp = bp->b_target->bt_mount;
89
90 /* no verification of non-crc buffers */
91 if (!xfs_sb_version_hascrc(&mp->m_sb))
92 return;
93
94 if (!xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
95 XFS_ATTR3_RMT_CRC_OFF) ||
96 !xfs_attr3_rmt_verify(bp)) {
97 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
98 xfs_buf_ioerror(bp, EFSCORRUPTED);
99 }
100}
101
102static void
103xfs_attr3_rmt_write_verify(
104 struct xfs_buf *bp)
105{
106 struct xfs_mount *mp = bp->b_target->bt_mount;
107 struct xfs_buf_log_item *bip = bp->b_fspriv;
108
109 /* no verification of non-crc buffers */
110 if (!xfs_sb_version_hascrc(&mp->m_sb))
111 return;
112
113 if (!xfs_attr3_rmt_verify(bp)) {
114 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
115 xfs_buf_ioerror(bp, EFSCORRUPTED);
116 return;
117 }
118
119 if (bip) {
120 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
121 rmt->rm_lsn = cpu_to_be64(bip->bli_item.li_lsn);
122 }
123 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
124 XFS_ATTR3_RMT_CRC_OFF);
125}
126
127const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
128 .verify_read = xfs_attr3_rmt_read_verify,
129 .verify_write = xfs_attr3_rmt_write_verify,
130};
131
132static int
133xfs_attr3_rmt_hdr_set(
134 struct xfs_mount *mp,
135 xfs_ino_t ino,
136 uint32_t offset,
137 uint32_t size,
138 struct xfs_buf *bp)
139{
140 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
141
142 if (!xfs_sb_version_hascrc(&mp->m_sb))
143 return 0;
144
145 rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC);
146 rmt->rm_offset = cpu_to_be32(offset);
147 rmt->rm_bytes = cpu_to_be32(size);
148 uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_uuid);
149 rmt->rm_owner = cpu_to_be64(ino);
150 rmt->rm_blkno = cpu_to_be64(bp->b_bn);
151 bp->b_ops = &xfs_attr3_rmt_buf_ops;
152
153 return sizeof(struct xfs_attr3_rmt_hdr);
154}
155
156/*
157 * Checking of the remote attribute header is split into two parts. the verifier
158 * does CRC, location and bounds checking, the unpacking function checks the
159 * attribute parameters and owner.
160 */
161static bool
162xfs_attr3_rmt_hdr_ok(
163 struct xfs_mount *mp,
164 xfs_ino_t ino,
165 uint32_t offset,
166 uint32_t size,
167 struct xfs_buf *bp)
168{
169 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
170
171 if (offset != be32_to_cpu(rmt->rm_offset))
172 return false;
173 if (size != be32_to_cpu(rmt->rm_bytes))
174 return false;
175 if (ino != be64_to_cpu(rmt->rm_owner))
176 return false;
177
178 /* ok */
179 return true;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100180}
181
182/*
Dave Chinner95920cd2013-04-03 16:11:27 +1100183 * Read the value associated with an attribute from the out-of-line buffer
184 * that we stored it in.
185 */
186int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100187xfs_attr_rmtval_get(
188 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100189{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100190 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
191 struct xfs_mount *mp = args->dp->i_mount;
192 struct xfs_buf *bp;
193 xfs_daddr_t dblkno;
194 xfs_dablk_t lblkno = args->rmtblkno;
195 void *dst = args->value;
196 int valuelen = args->valuelen;
197 int nmap;
198 int error;
199 int blkcnt;
200 int i;
201 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100202
203 trace_xfs_attr_rmtval_get(args);
204
205 ASSERT(!(args->flags & ATTR_KERNOVAL));
206
Dave Chinner95920cd2013-04-03 16:11:27 +1100207 while (valuelen > 0) {
208 nmap = ATTR_RMTVALUE_MAPSIZE;
209 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
210 args->rmtblkcnt, map, &nmap,
211 XFS_BMAPI_ATTRFORK);
212 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100213 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100214 ASSERT(nmap >= 1);
215
216 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100217 int byte_cnt;
218 char *src;
219
Dave Chinner95920cd2013-04-03 16:11:27 +1100220 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
221 (map[i].br_startblock != HOLESTARTBLOCK));
222 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
223 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
224 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100225 dblkno, blkcnt, 0, &bp,
226 &xfs_attr3_rmt_buf_ops);
Dave Chinner95920cd2013-04-03 16:11:27 +1100227 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100228 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100229
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100230 byte_cnt = min_t(int, valuelen, BBTOB(bp->b_length));
231 byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, byte_cnt);
232
233 src = bp->b_addr;
234 if (xfs_sb_version_hascrc(&mp->m_sb)) {
235 if (!xfs_attr3_rmt_hdr_ok(mp, args->dp->i_ino,
236 offset, byte_cnt, bp)) {
237 xfs_alert(mp,
238"remote attribute header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
239 offset, byte_cnt, args->dp->i_ino);
240 xfs_buf_relse(bp);
241 return EFSCORRUPTED;
242
243 }
244
245 src += sizeof(struct xfs_attr3_rmt_hdr);
246 }
247
248 memcpy(dst, src, byte_cnt);
Dave Chinner95920cd2013-04-03 16:11:27 +1100249 xfs_buf_relse(bp);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100250
251 offset += byte_cnt;
252 dst += byte_cnt;
253 valuelen -= byte_cnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100254
255 lblkno += map[i].br_blockcount;
256 }
257 }
258 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100259 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100260}
261
262/*
263 * Write the value associated with an attribute into the out-of-line buffer
264 * that we have defined for it.
265 */
266int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100267xfs_attr_rmtval_set(
268 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100269{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100270 struct xfs_inode *dp = args->dp;
271 struct xfs_mount *mp = dp->i_mount;
272 struct xfs_bmbt_irec map;
273 struct xfs_buf *bp;
274 xfs_daddr_t dblkno;
275 xfs_dablk_t lblkno;
276 xfs_fileoff_t lfileoff = 0;
277 void *src = args->value;
278 int blkcnt;
279 int valuelen;
280 int nmap;
281 int error;
282 int hdrcnt = 0;
283 bool crcs = xfs_sb_version_hascrc(&mp->m_sb);
284 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100285
286 trace_xfs_attr_rmtval_set(args);
287
Dave Chinner95920cd2013-04-03 16:11:27 +1100288 /*
289 * Find a "hole" in the attribute address space large enough for
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100290 * us to drop the new attribute's value into. Because CRC enable
291 * attributes have headers, we can't just do a straight byte to FSB
292 * conversion. We calculate the worst case block count in this case
293 * and we may not need that many, so we have to handle this when
294 * allocating the blocks below.
Dave Chinner95920cd2013-04-03 16:11:27 +1100295 */
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100296 if (!crcs)
297 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
298 else
299 blkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
300
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.
362 */
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100363 if (crcs && blkcnt == 0) {
364 int total_len;
365
366 total_len = args->valuelen +
367 hdrcnt * sizeof(struct xfs_attr3_rmt_hdr);
368 blkcnt = XFS_B_TO_FSB(mp, total_len);
369 blkcnt -= args->rmtblkcnt;
370 args->rmtblkcnt += blkcnt;
371 }
Dave Chinner95920cd2013-04-03 16:11:27 +1100372
373 /*
374 * Start the next trans in the chain.
375 */
376 error = xfs_trans_roll(&args->trans, dp);
377 if (error)
378 return (error);
379 }
380
381 /*
382 * Roll through the "value", copying the attribute value to the
383 * already-allocated blocks. Blocks are written synchronously
384 * so that we can know they are all on disk before we turn off
385 * the INCOMPLETE flag.
386 */
387 lblkno = args->rmtblkno;
388 valuelen = args->valuelen;
389 while (valuelen > 0) {
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100390 int byte_cnt;
391 char *buf;
Dave Chinner95920cd2013-04-03 16:11:27 +1100392
393 /*
394 * Try to remember where we decided to put the value.
395 */
396 xfs_bmap_init(args->flist, args->firstblock);
397 nmap = 1;
398 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
399 args->rmtblkcnt, &map, &nmap,
400 XFS_BMAPI_ATTRFORK);
401 if (error)
402 return(error);
403 ASSERT(nmap == 1);
404 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
405 (map.br_startblock != HOLESTARTBLOCK));
406
407 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
408 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
409
410 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, blkcnt, 0);
411 if (!bp)
412 return ENOMEM;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100413 bp->b_ops = &xfs_attr3_rmt_buf_ops;
Dave Chinner95920cd2013-04-03 16:11:27 +1100414
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100415 byte_cnt = BBTOB(bp->b_length);
416 byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, byte_cnt);
Dave Chinner946217b2013-04-30 21:39:35 +1000417 if (valuelen < byte_cnt)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100418 byte_cnt = valuelen;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100419
420 buf = bp->b_addr;
421 buf += xfs_attr3_rmt_hdr_set(mp, dp->i_ino, offset,
422 byte_cnt, bp);
423 memcpy(buf, src, byte_cnt);
424
425 if (byte_cnt < BBTOB(bp->b_length))
426 xfs_buf_zero(bp, byte_cnt,
427 BBTOB(bp->b_length) - byte_cnt);
Dave Chinner95920cd2013-04-03 16:11:27 +1100428
429 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
430 xfs_buf_relse(bp);
431 if (error)
432 return error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100433
434 src += byte_cnt;
435 valuelen -= byte_cnt;
436 offset += byte_cnt;
437 hdrcnt--;
Dave Chinner95920cd2013-04-03 16:11:27 +1100438
439 lblkno += map.br_blockcount;
440 }
441 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100442 ASSERT(hdrcnt == 0);
443 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100444}
445
446/*
447 * Remove the value associated with an attribute by deleting the
448 * out-of-line buffer that it is stored on.
449 */
450int
451xfs_attr_rmtval_remove(xfs_da_args_t *args)
452{
453 xfs_mount_t *mp;
454 xfs_bmbt_irec_t map;
455 xfs_buf_t *bp;
456 xfs_daddr_t dblkno;
457 xfs_dablk_t lblkno;
458 int valuelen, blkcnt, nmap, error, done, committed;
459
460 trace_xfs_attr_rmtval_remove(args);
461
462 mp = args->dp->i_mount;
463
464 /*
465 * Roll through the "value", invalidating the attribute value's
466 * blocks.
467 */
468 lblkno = args->rmtblkno;
469 valuelen = args->rmtblkcnt;
470 while (valuelen > 0) {
471 /*
472 * Try to remember where we decided to put the value.
473 */
474 nmap = 1;
475 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
476 args->rmtblkcnt, &map, &nmap,
477 XFS_BMAPI_ATTRFORK);
478 if (error)
479 return(error);
480 ASSERT(nmap == 1);
481 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
482 (map.br_startblock != HOLESTARTBLOCK));
483
484 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
485 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
486
487 /*
488 * If the "remote" value is in the cache, remove it.
489 */
490 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
491 if (bp) {
492 xfs_buf_stale(bp);
493 xfs_buf_relse(bp);
494 bp = NULL;
495 }
496
497 valuelen -= map.br_blockcount;
498
499 lblkno += map.br_blockcount;
500 }
501
502 /*
503 * Keep de-allocating extents until the remote-value region is gone.
504 */
505 lblkno = args->rmtblkno;
506 blkcnt = args->rmtblkcnt;
507 done = 0;
508 while (!done) {
509 xfs_bmap_init(args->flist, args->firstblock);
510 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
511 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
512 1, args->firstblock, args->flist,
513 &done);
514 if (!error) {
515 error = xfs_bmap_finish(&args->trans, args->flist,
516 &committed);
517 }
518 if (error) {
519 ASSERT(committed);
520 args->trans = NULL;
521 xfs_bmap_cancel(args->flist);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100522 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100523 }
524
525 /*
526 * bmap_finish() may have committed the last trans and started
527 * a new one. We need the inode to be in all transactions.
528 */
529 if (committed)
530 xfs_trans_ijoin(args->trans, args->dp, 0);
531
532 /*
533 * Close out trans and start the next one in the chain.
534 */
535 error = xfs_trans_roll(&args->trans, args->dp);
536 if (error)
537 return (error);
538 }
539 return(0);
540}
541