2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
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
21 #include "xfs_types.h"
24 #include "xfs_trans.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"
37 #include "xfs_attr_leaf.h"
38 #include "xfs_attr_remote.h"
39 #include "xfs_trans_space.h"
40 #include "xfs_trace.h"
41 #include "xfs_cksum.h"
42 #include "xfs_buf_item.h"
44 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
47 * Each contiguous block has a header, so it is not just a simple attribute
48 * length to FSB conversion.
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;
59 return XFS_B_TO_FSB(mp, attrlen);
66 struct xfs_mount *mp = bp->b_target->bt_mount;
67 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
69 if (!xfs_sb_version_hascrc(&mp->m_sb))
71 if (rmt->rm_magic != cpu_to_be32(XFS_ATTR3_RMT_MAGIC))
73 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_uuid))
75 if (bp->b_bn != be64_to_cpu(rmt->rm_blkno))
77 if (be32_to_cpu(rmt->rm_offset) +
78 be32_to_cpu(rmt->rm_bytes) >= XATTR_SIZE_MAX)
80 if (rmt->rm_owner == 0)
87 xfs_attr3_rmt_read_verify(
90 struct xfs_mount *mp = bp->b_target->bt_mount;
92 /* no verification of non-crc buffers */
93 if (!xfs_sb_version_hascrc(&mp->m_sb))
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);
105 xfs_attr3_rmt_write_verify(
108 struct xfs_mount *mp = bp->b_target->bt_mount;
109 struct xfs_buf_log_item *bip = bp->b_fspriv;
111 /* no verification of non-crc buffers */
112 if (!xfs_sb_version_hascrc(&mp->m_sb))
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);
122 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
123 rmt->rm_lsn = cpu_to_be64(bip->bli_item.li_lsn);
125 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
126 XFS_ATTR3_RMT_CRC_OFF);
129 const 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,
135 xfs_attr3_rmt_hdr_set(
136 struct xfs_mount *mp,
142 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
144 if (!xfs_sb_version_hascrc(&mp->m_sb))
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;
155 return sizeof(struct xfs_attr3_rmt_hdr);
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.
164 xfs_attr3_rmt_hdr_ok(
165 struct xfs_mount *mp,
171 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
173 if (offset != be32_to_cpu(rmt->rm_offset))
175 if (size != be32_to_cpu(rmt->rm_bytes))
177 if (ino != be64_to_cpu(rmt->rm_owner))
185 * Read the value associated with an attribute from the out-of-line buffer
186 * that we stored it in.
190 struct xfs_da_args *args)
192 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
193 struct xfs_mount *mp = args->dp->i_mount;
196 xfs_dablk_t lblkno = args->rmtblkno;
197 void *dst = args->value;
198 int valuelen = args->valuelen;
205 trace_xfs_attr_rmtval_get(args);
207 ASSERT(!(args->flags & ATTR_KERNOVAL));
209 while (valuelen > 0) {
210 nmap = ATTR_RMTVALUE_MAPSIZE;
211 blkcnt = xfs_attr3_rmt_blocks(mp, valuelen);
212 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
219 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
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,
228 dblkno, blkcnt, 0, &bp,
229 &xfs_attr3_rmt_buf_ops);
233 byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, BBTOB(bp->b_length));
234 byte_cnt = min_t(int, valuelen, byte_cnt);
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)) {
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);
248 src += sizeof(struct xfs_attr3_rmt_hdr);
251 memcpy(dst, src, byte_cnt);
256 valuelen -= byte_cnt;
258 lblkno += map[i].br_blockcount;
261 ASSERT(valuelen == 0);
266 * Write the value associated with an attribute into the out-of-line buffer
267 * that we have defined for it.
271 struct xfs_da_args *args)
273 struct xfs_inode *dp = args->dp;
274 struct xfs_mount *mp = dp->i_mount;
275 struct xfs_bmbt_irec map;
279 xfs_fileoff_t lfileoff = 0;
280 void *src = args->value;
286 bool crcs = xfs_sb_version_hascrc(&mp->m_sb);
289 trace_xfs_attr_rmtval_set(args);
292 * Find a "hole" in the attribute address space large enough for
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.
299 blkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
301 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
306 /* Start with the attribute data. We'll allocate the rest afterwards. */
308 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
310 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
311 args->rmtblkcnt = blkcnt;
314 * Roll through the "value", allocating blocks on disk as required.
320 * Allocate a single extent, up to the size of the value.
322 xfs_bmap_init(args->flist, args->firstblock);
324 error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
326 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
327 args->firstblock, args->total, &map, &nmap,
330 error = xfs_bmap_finish(&args->trans, args->flist,
336 xfs_bmap_cancel(args->flist);
341 * bmap_finish() may have committed the last trans and started
342 * a new one. We need the inode to be in all transactions.
345 xfs_trans_ijoin(args->trans, dp, 0);
348 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
349 (map.br_startblock != HOLESTARTBLOCK));
350 lblkno += map.br_blockcount;
351 blkcnt -= map.br_blockcount;
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
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.
368 if (crcs && blkcnt == 0) {
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;
379 * Start the next trans in the chain.
381 error = xfs_trans_roll(&args->trans, dp);
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.
392 lblkno = args->rmtblkno;
393 valuelen = args->valuelen;
394 blkcnt = args->rmtblkcnt;
395 while (valuelen > 0) {
402 * Try to remember where we decided to put the value.
404 xfs_bmap_init(args->flist, args->firstblock);
406 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
412 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
413 (map.br_startblock != HOLESTARTBLOCK));
415 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
416 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
418 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0);
421 bp->b_ops = &xfs_attr3_rmt_buf_ops;
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,
428 ASSERT(hdr_size + byte_cnt <= BBTOB(bp->b_length));
430 memcpy(buf + hdr_size, src, byte_cnt);
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);
436 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
442 valuelen -= byte_cnt;
445 lblkno += map.br_blockcount;
446 blkcnt -= map.br_blockcount;
448 ASSERT(valuelen == 0);
453 * Remove the value associated with an attribute by deleting the
454 * out-of-line buffer that it is stored on.
457 xfs_attr_rmtval_remove(xfs_da_args_t *args)
464 int valuelen, blkcnt, nmap, error, done, committed;
466 trace_xfs_attr_rmtval_remove(args);
468 mp = args->dp->i_mount;
471 * Roll through the "value", invalidating the attribute value's
474 lblkno = args->rmtblkno;
475 valuelen = args->rmtblkcnt;
476 while (valuelen > 0) {
478 * Try to remember where we decided to put the value.
481 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
482 args->rmtblkcnt, &map, &nmap,
487 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
488 (map.br_startblock != HOLESTARTBLOCK));
490 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
491 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
494 * If the "remote" value is in the cache, remove it.
496 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
503 valuelen -= map.br_blockcount;
505 lblkno += map.br_blockcount;
509 * Keep de-allocating extents until the remote-value region is gone.
511 lblkno = args->rmtblkno;
512 blkcnt = args->rmtblkcnt;
515 xfs_bmap_init(args->flist, args->firstblock);
516 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
517 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
518 1, args->firstblock, args->flist,
521 error = xfs_bmap_finish(&args->trans, args->flist,
527 xfs_bmap_cancel(args->flist);
532 * bmap_finish() may have committed the last trans and started
533 * a new one. We need the inode to be in all transactions.
536 xfs_trans_ijoin(args->trans, args->dp, 0);
539 * Close out trans and start the next one in the chain.
541 error = xfs_trans_roll(&args->trans, args->dp);