tracing: Have preempt(irqs)off trace preempt disabled functions
[linux/fpc-iii.git] / fs / xfs / xfs_attr_remote.c
bloba46b19e2b4ceb5d0c2f3e6b742dd46f6a4eee947
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
4 * All Rights Reserved.
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.
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.
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
19 #include "xfs.h"
20 #include "xfs_fs.h"
21 #include "xfs_shared.h"
22 #include "xfs_format.h"
23 #include "xfs_log_format.h"
24 #include "xfs_trans_resv.h"
25 #include "xfs_bit.h"
26 #include "xfs_sb.h"
27 #include "xfs_ag.h"
28 #include "xfs_mount.h"
29 #include "xfs_da_format.h"
30 #include "xfs_da_btree.h"
31 #include "xfs_inode.h"
32 #include "xfs_alloc.h"
33 #include "xfs_trans.h"
34 #include "xfs_inode_item.h"
35 #include "xfs_bmap.h"
36 #include "xfs_bmap_util.h"
37 #include "xfs_attr.h"
38 #include "xfs_attr_leaf.h"
39 #include "xfs_attr_remote.h"
40 #include "xfs_trans_space.h"
41 #include "xfs_trace.h"
42 #include "xfs_cksum.h"
43 #include "xfs_buf_item.h"
44 #include "xfs_error.h"
46 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
49 * Each contiguous block has a header, so it is not just a simple attribute
50 * length to FSB conversion.
52 int
53 xfs_attr3_rmt_blocks(
54 struct xfs_mount *mp,
55 int attrlen)
57 if (xfs_sb_version_hascrc(&mp->m_sb)) {
58 int buflen = XFS_ATTR3_RMT_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
59 return (attrlen + buflen - 1) / buflen;
61 return XFS_B_TO_FSB(mp, attrlen);
65 * Checking of the remote attribute header is split into two parts. The verifier
66 * does CRC, location and bounds checking, the unpacking function checks the
67 * attribute parameters and owner.
69 static bool
70 xfs_attr3_rmt_hdr_ok(
71 void *ptr,
72 xfs_ino_t ino,
73 uint32_t offset,
74 uint32_t size,
75 xfs_daddr_t bno)
77 struct xfs_attr3_rmt_hdr *rmt = ptr;
79 if (bno != be64_to_cpu(rmt->rm_blkno))
80 return false;
81 if (offset != be32_to_cpu(rmt->rm_offset))
82 return false;
83 if (size != be32_to_cpu(rmt->rm_bytes))
84 return false;
85 if (ino != be64_to_cpu(rmt->rm_owner))
86 return false;
88 /* ok */
89 return true;
92 static bool
93 xfs_attr3_rmt_verify(
94 struct xfs_mount *mp,
95 void *ptr,
96 int fsbsize,
97 xfs_daddr_t bno)
99 struct xfs_attr3_rmt_hdr *rmt = ptr;
101 if (!xfs_sb_version_hascrc(&mp->m_sb))
102 return false;
103 if (rmt->rm_magic != cpu_to_be32(XFS_ATTR3_RMT_MAGIC))
104 return false;
105 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_uuid))
106 return false;
107 if (be64_to_cpu(rmt->rm_blkno) != bno)
108 return false;
109 if (be32_to_cpu(rmt->rm_bytes) > fsbsize - sizeof(*rmt))
110 return false;
111 if (be32_to_cpu(rmt->rm_offset) +
112 be32_to_cpu(rmt->rm_bytes) > XATTR_SIZE_MAX)
113 return false;
114 if (rmt->rm_owner == 0)
115 return false;
117 return true;
120 static void
121 xfs_attr3_rmt_read_verify(
122 struct xfs_buf *bp)
124 struct xfs_mount *mp = bp->b_target->bt_mount;
125 char *ptr;
126 int len;
127 xfs_daddr_t bno;
128 int blksize = mp->m_attr_geo->blksize;
130 /* no verification of non-crc buffers */
131 if (!xfs_sb_version_hascrc(&mp->m_sb))
132 return;
134 ptr = bp->b_addr;
135 bno = bp->b_bn;
136 len = BBTOB(bp->b_length);
137 ASSERT(len >= blksize);
139 while (len > 0) {
140 if (!xfs_verify_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF)) {
141 xfs_buf_ioerror(bp, EFSBADCRC);
142 break;
144 if (!xfs_attr3_rmt_verify(mp, ptr, blksize, bno)) {
145 xfs_buf_ioerror(bp, EFSCORRUPTED);
146 break;
148 len -= blksize;
149 ptr += blksize;
150 bno += BTOBB(blksize);
153 if (bp->b_error)
154 xfs_verifier_error(bp);
155 else
156 ASSERT(len == 0);
159 static void
160 xfs_attr3_rmt_write_verify(
161 struct xfs_buf *bp)
163 struct xfs_mount *mp = bp->b_target->bt_mount;
164 int blksize = mp->m_attr_geo->blksize;
165 char *ptr;
166 int len;
167 xfs_daddr_t bno;
169 /* no verification of non-crc buffers */
170 if (!xfs_sb_version_hascrc(&mp->m_sb))
171 return;
173 ptr = bp->b_addr;
174 bno = bp->b_bn;
175 len = BBTOB(bp->b_length);
176 ASSERT(len >= blksize);
178 while (len > 0) {
179 struct xfs_attr3_rmt_hdr *rmt = (struct xfs_attr3_rmt_hdr *)ptr;
181 if (!xfs_attr3_rmt_verify(mp, ptr, blksize, bno)) {
182 xfs_buf_ioerror(bp, EFSCORRUPTED);
183 xfs_verifier_error(bp);
184 return;
188 * Ensure we aren't writing bogus LSNs to disk. See
189 * xfs_attr3_rmt_hdr_set() for the explanation.
191 if (rmt->rm_lsn != cpu_to_be64(NULLCOMMITLSN)) {
192 xfs_buf_ioerror(bp, EFSCORRUPTED);
193 xfs_verifier_error(bp);
194 return;
196 xfs_update_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF);
198 len -= blksize;
199 ptr += blksize;
200 bno += BTOBB(blksize);
202 ASSERT(len == 0);
205 const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
206 .verify_read = xfs_attr3_rmt_read_verify,
207 .verify_write = xfs_attr3_rmt_write_verify,
210 STATIC int
211 xfs_attr3_rmt_hdr_set(
212 struct xfs_mount *mp,
213 void *ptr,
214 xfs_ino_t ino,
215 uint32_t offset,
216 uint32_t size,
217 xfs_daddr_t bno)
219 struct xfs_attr3_rmt_hdr *rmt = ptr;
221 if (!xfs_sb_version_hascrc(&mp->m_sb))
222 return 0;
224 rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC);
225 rmt->rm_offset = cpu_to_be32(offset);
226 rmt->rm_bytes = cpu_to_be32(size);
227 uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_uuid);
228 rmt->rm_owner = cpu_to_be64(ino);
229 rmt->rm_blkno = cpu_to_be64(bno);
232 * Remote attribute blocks are written synchronously, so we don't
233 * have an LSN that we can stamp in them that makes any sense to log
234 * recovery. To ensure that log recovery handles overwrites of these
235 * blocks sanely (i.e. once they've been freed and reallocated as some
236 * other type of metadata) we need to ensure that the LSN has a value
237 * that tells log recovery to ignore the LSN and overwrite the buffer
238 * with whatever is in it's log. To do this, we use the magic
239 * NULLCOMMITLSN to indicate that the LSN is invalid.
241 rmt->rm_lsn = cpu_to_be64(NULLCOMMITLSN);
243 return sizeof(struct xfs_attr3_rmt_hdr);
247 * Helper functions to copy attribute data in and out of the one disk extents
249 STATIC int
250 xfs_attr_rmtval_copyout(
251 struct xfs_mount *mp,
252 struct xfs_buf *bp,
253 xfs_ino_t ino,
254 int *offset,
255 int *valuelen,
256 __uint8_t **dst)
258 char *src = bp->b_addr;
259 xfs_daddr_t bno = bp->b_bn;
260 int len = BBTOB(bp->b_length);
261 int blksize = mp->m_attr_geo->blksize;
263 ASSERT(len >= blksize);
265 while (len > 0 && *valuelen > 0) {
266 int hdr_size = 0;
267 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize);
269 byte_cnt = min(*valuelen, byte_cnt);
271 if (xfs_sb_version_hascrc(&mp->m_sb)) {
272 if (!xfs_attr3_rmt_hdr_ok(src, ino, *offset,
273 byte_cnt, bno)) {
274 xfs_alert(mp,
275 "remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)",
276 bno, *offset, byte_cnt, ino);
277 return EFSCORRUPTED;
279 hdr_size = sizeof(struct xfs_attr3_rmt_hdr);
282 memcpy(*dst, src + hdr_size, byte_cnt);
284 /* roll buffer forwards */
285 len -= blksize;
286 src += blksize;
287 bno += BTOBB(blksize);
289 /* roll attribute data forwards */
290 *valuelen -= byte_cnt;
291 *dst += byte_cnt;
292 *offset += byte_cnt;
294 return 0;
297 STATIC void
298 xfs_attr_rmtval_copyin(
299 struct xfs_mount *mp,
300 struct xfs_buf *bp,
301 xfs_ino_t ino,
302 int *offset,
303 int *valuelen,
304 __uint8_t **src)
306 char *dst = bp->b_addr;
307 xfs_daddr_t bno = bp->b_bn;
308 int len = BBTOB(bp->b_length);
309 int blksize = mp->m_attr_geo->blksize;
311 ASSERT(len >= blksize);
313 while (len > 0 && *valuelen > 0) {
314 int hdr_size;
315 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize);
317 byte_cnt = min(*valuelen, byte_cnt);
318 hdr_size = xfs_attr3_rmt_hdr_set(mp, dst, ino, *offset,
319 byte_cnt, bno);
321 memcpy(dst + hdr_size, *src, byte_cnt);
324 * If this is the last block, zero the remainder of it.
325 * Check that we are actually the last block, too.
327 if (byte_cnt + hdr_size < blksize) {
328 ASSERT(*valuelen - byte_cnt == 0);
329 ASSERT(len == blksize);
330 memset(dst + hdr_size + byte_cnt, 0,
331 blksize - hdr_size - byte_cnt);
334 /* roll buffer forwards */
335 len -= blksize;
336 dst += blksize;
337 bno += BTOBB(blksize);
339 /* roll attribute data forwards */
340 *valuelen -= byte_cnt;
341 *src += byte_cnt;
342 *offset += byte_cnt;
347 * Read the value associated with an attribute from the out-of-line buffer
348 * that we stored it in.
351 xfs_attr_rmtval_get(
352 struct xfs_da_args *args)
354 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
355 struct xfs_mount *mp = args->dp->i_mount;
356 struct xfs_buf *bp;
357 xfs_dablk_t lblkno = args->rmtblkno;
358 __uint8_t *dst = args->value;
359 int valuelen;
360 int nmap;
361 int error;
362 int blkcnt = args->rmtblkcnt;
363 int i;
364 int offset = 0;
366 trace_xfs_attr_rmtval_get(args);
368 ASSERT(!(args->flags & ATTR_KERNOVAL));
369 ASSERT(args->rmtvaluelen == args->valuelen);
371 valuelen = args->rmtvaluelen;
372 while (valuelen > 0) {
373 nmap = ATTR_RMTVALUE_MAPSIZE;
374 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
375 blkcnt, map, &nmap,
376 XFS_BMAPI_ATTRFORK);
377 if (error)
378 return error;
379 ASSERT(nmap >= 1);
381 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
382 xfs_daddr_t dblkno;
383 int dblkcnt;
385 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
386 (map[i].br_startblock != HOLESTARTBLOCK));
387 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
388 dblkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
389 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
390 dblkno, dblkcnt, 0, &bp,
391 &xfs_attr3_rmt_buf_ops);
392 if (error)
393 return error;
395 error = xfs_attr_rmtval_copyout(mp, bp, args->dp->i_ino,
396 &offset, &valuelen,
397 &dst);
398 xfs_buf_relse(bp);
399 if (error)
400 return error;
402 /* roll attribute extent map forwards */
403 lblkno += map[i].br_blockcount;
404 blkcnt -= map[i].br_blockcount;
407 ASSERT(valuelen == 0);
408 return 0;
412 * Write the value associated with an attribute into the out-of-line buffer
413 * that we have defined for it.
416 xfs_attr_rmtval_set(
417 struct xfs_da_args *args)
419 struct xfs_inode *dp = args->dp;
420 struct xfs_mount *mp = dp->i_mount;
421 struct xfs_bmbt_irec map;
422 xfs_dablk_t lblkno;
423 xfs_fileoff_t lfileoff = 0;
424 __uint8_t *src = args->value;
425 int blkcnt;
426 int valuelen;
427 int nmap;
428 int error;
429 int offset = 0;
431 trace_xfs_attr_rmtval_set(args);
434 * Find a "hole" in the attribute address space large enough for
435 * us to drop the new attribute's value into. Because CRC enable
436 * attributes have headers, we can't just do a straight byte to FSB
437 * conversion and have to take the header space into account.
439 blkcnt = xfs_attr3_rmt_blocks(mp, args->rmtvaluelen);
440 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
441 XFS_ATTR_FORK);
442 if (error)
443 return error;
445 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
446 args->rmtblkcnt = blkcnt;
449 * Roll through the "value", allocating blocks on disk as required.
451 while (blkcnt > 0) {
452 int committed;
455 * Allocate a single extent, up to the size of the value.
457 * Note that we have to consider this a data allocation as we
458 * write the remote attribute without logging the contents.
459 * Hence we must ensure that we aren't using blocks that are on
460 * the busy list so that we don't overwrite blocks which have
461 * recently been freed but their transactions are not yet
462 * committed to disk. If we overwrite the contents of a busy
463 * extent and then crash then the block may not contain the
464 * correct metadata after log recovery occurs.
466 xfs_bmap_init(args->flist, args->firstblock);
467 nmap = 1;
468 error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
469 blkcnt, XFS_BMAPI_ATTRFORK, args->firstblock,
470 args->total, &map, &nmap, args->flist);
471 if (!error) {
472 error = xfs_bmap_finish(&args->trans, args->flist,
473 &committed);
475 if (error) {
476 ASSERT(committed);
477 args->trans = NULL;
478 xfs_bmap_cancel(args->flist);
479 return(error);
483 * bmap_finish() may have committed the last trans and started
484 * a new one. We need the inode to be in all transactions.
486 if (committed)
487 xfs_trans_ijoin(args->trans, dp, 0);
489 ASSERT(nmap == 1);
490 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
491 (map.br_startblock != HOLESTARTBLOCK));
492 lblkno += map.br_blockcount;
493 blkcnt -= map.br_blockcount;
496 * Start the next trans in the chain.
498 error = xfs_trans_roll(&args->trans, dp);
499 if (error)
500 return (error);
504 * Roll through the "value", copying the attribute value to the
505 * already-allocated blocks. Blocks are written synchronously
506 * so that we can know they are all on disk before we turn off
507 * the INCOMPLETE flag.
509 lblkno = args->rmtblkno;
510 blkcnt = args->rmtblkcnt;
511 valuelen = args->rmtvaluelen;
512 while (valuelen > 0) {
513 struct xfs_buf *bp;
514 xfs_daddr_t dblkno;
515 int dblkcnt;
517 ASSERT(blkcnt > 0);
519 xfs_bmap_init(args->flist, args->firstblock);
520 nmap = 1;
521 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
522 blkcnt, &map, &nmap,
523 XFS_BMAPI_ATTRFORK);
524 if (error)
525 return(error);
526 ASSERT(nmap == 1);
527 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
528 (map.br_startblock != HOLESTARTBLOCK));
530 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
531 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
533 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0);
534 if (!bp)
535 return ENOMEM;
536 bp->b_ops = &xfs_attr3_rmt_buf_ops;
538 xfs_attr_rmtval_copyin(mp, bp, args->dp->i_ino, &offset,
539 &valuelen, &src);
541 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
542 xfs_buf_relse(bp);
543 if (error)
544 return error;
547 /* roll attribute extent map forwards */
548 lblkno += map.br_blockcount;
549 blkcnt -= map.br_blockcount;
551 ASSERT(valuelen == 0);
552 return 0;
556 * Remove the value associated with an attribute by deleting the
557 * out-of-line buffer that it is stored on.
560 xfs_attr_rmtval_remove(
561 struct xfs_da_args *args)
563 struct xfs_mount *mp = args->dp->i_mount;
564 xfs_dablk_t lblkno;
565 int blkcnt;
566 int error;
567 int done;
569 trace_xfs_attr_rmtval_remove(args);
572 * Roll through the "value", invalidating the attribute value's blocks.
574 lblkno = args->rmtblkno;
575 blkcnt = args->rmtblkcnt;
576 while (blkcnt > 0) {
577 struct xfs_bmbt_irec map;
578 struct xfs_buf *bp;
579 xfs_daddr_t dblkno;
580 int dblkcnt;
581 int nmap;
584 * Try to remember where we decided to put the value.
586 nmap = 1;
587 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
588 blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
589 if (error)
590 return(error);
591 ASSERT(nmap == 1);
592 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
593 (map.br_startblock != HOLESTARTBLOCK));
595 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
596 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
599 * If the "remote" value is in the cache, remove it.
601 bp = xfs_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK);
602 if (bp) {
603 xfs_buf_stale(bp);
604 xfs_buf_relse(bp);
605 bp = NULL;
608 lblkno += map.br_blockcount;
609 blkcnt -= map.br_blockcount;
613 * Keep de-allocating extents until the remote-value region is gone.
615 lblkno = args->rmtblkno;
616 blkcnt = args->rmtblkcnt;
617 done = 0;
618 while (!done) {
619 int committed;
621 xfs_bmap_init(args->flist, args->firstblock);
622 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
623 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
624 1, args->firstblock, args->flist,
625 &done);
626 if (!error) {
627 error = xfs_bmap_finish(&args->trans, args->flist,
628 &committed);
630 if (error) {
631 ASSERT(committed);
632 args->trans = NULL;
633 xfs_bmap_cancel(args->flist);
634 return error;
638 * bmap_finish() may have committed the last trans and started
639 * a new one. We need the inode to be in all transactions.
641 if (committed)
642 xfs_trans_ijoin(args->trans, args->dp, 0);
645 * Close out trans and start the next one in the chain.
647 error = xfs_trans_roll(&args->trans, args->dp);
648 if (error)
649 return (error);
651 return(0);