blob: 4edd4a84abe9c30ea5ea7f9fa226068b4cb47acf [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"
Dave Chinner632b89e2013-10-29 22:11:58 +110021#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110022#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110023#include "xfs_log_format.h"
24#include "xfs_trans_resv.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110025#include "xfs_bit.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110026#include "xfs_sb.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110027#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110028#include "xfs_da_format.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110029#include "xfs_da_btree.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110030#include "xfs_inode.h"
31#include "xfs_alloc.h"
Dave Chinner239880e2013-10-23 10:50:10 +110032#include "xfs_trans.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110033#include "xfs_inode_item.h"
34#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100035#include "xfs_bmap_util.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110036#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 Chinnera4fbe6a2013-10-23 10:51:50 +110043#include "xfs_error.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110044
45#define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
46
47/*
Dave Chinnerd2e448d2013-04-03 16:11:28 +110048 * Each contiguous block has a header, so it is not just a simple attribute
49 * length to FSB conversion.
50 */
Dave Chinner7bc0dc22013-05-21 18:02:08 +100051int
Dave Chinnerd2e448d2013-04-03 16:11:28 +110052xfs_attr3_rmt_blocks(
53 struct xfs_mount *mp,
54 int attrlen)
55{
Dave Chinner551b3822013-05-21 18:02:02 +100056 if (xfs_sb_version_hascrc(&mp->m_sb)) {
57 int buflen = XFS_ATTR3_RMT_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
58 return (attrlen + buflen - 1) / buflen;
59 }
60 return XFS_B_TO_FSB(mp, attrlen);
Dave Chinnerd2e448d2013-04-03 16:11:28 +110061}
62
Dave Chinner7bc0dc22013-05-21 18:02:08 +100063/*
64 * Checking of the remote attribute header is split into two parts. The verifier
65 * does CRC, location and bounds checking, the unpacking function checks the
66 * attribute parameters and owner.
67 */
68static bool
69xfs_attr3_rmt_hdr_ok(
Dave Chinner7bc0dc22013-05-21 18:02:08 +100070 void *ptr,
71 xfs_ino_t ino,
72 uint32_t offset,
73 uint32_t size,
74 xfs_daddr_t bno)
75{
76 struct xfs_attr3_rmt_hdr *rmt = ptr;
77
78 if (bno != be64_to_cpu(rmt->rm_blkno))
79 return false;
80 if (offset != be32_to_cpu(rmt->rm_offset))
81 return false;
82 if (size != be32_to_cpu(rmt->rm_bytes))
83 return false;
84 if (ino != be64_to_cpu(rmt->rm_owner))
85 return false;
86
87 /* ok */
88 return true;
89}
90
Dave Chinnerd2e448d2013-04-03 16:11:28 +110091static bool
92xfs_attr3_rmt_verify(
Dave Chinner7bc0dc22013-05-21 18:02:08 +100093 struct xfs_mount *mp,
94 void *ptr,
95 int fsbsize,
96 xfs_daddr_t bno)
Dave Chinnerd2e448d2013-04-03 16:11:28 +110097{
Dave Chinner7bc0dc22013-05-21 18:02:08 +100098 struct xfs_attr3_rmt_hdr *rmt = ptr;
Dave Chinnerd2e448d2013-04-03 16:11:28 +110099
100 if (!xfs_sb_version_hascrc(&mp->m_sb))
101 return false;
102 if (rmt->rm_magic != cpu_to_be32(XFS_ATTR3_RMT_MAGIC))
103 return false;
104 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_uuid))
105 return false;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000106 if (be64_to_cpu(rmt->rm_blkno) != bno)
107 return false;
108 if (be32_to_cpu(rmt->rm_bytes) > fsbsize - sizeof(*rmt))
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100109 return false;
110 if (be32_to_cpu(rmt->rm_offset) +
Jie Liubba719b2014-01-01 19:28:03 +0800111 be32_to_cpu(rmt->rm_bytes) > XATTR_SIZE_MAX)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100112 return false;
113 if (rmt->rm_owner == 0)
114 return false;
115
116 return true;
117}
118
119static void
120xfs_attr3_rmt_read_verify(
121 struct xfs_buf *bp)
122{
123 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000124 char *ptr;
125 int len;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000126 xfs_daddr_t bno;
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000127 int blksize = mp->m_attr_geo->blksize;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100128
129 /* no verification of non-crc buffers */
130 if (!xfs_sb_version_hascrc(&mp->m_sb))
131 return;
132
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000133 ptr = bp->b_addr;
134 bno = bp->b_bn;
135 len = BBTOB(bp->b_length);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000136 ASSERT(len >= blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000137
138 while (len > 0) {
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000139 if (!xfs_verify_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF)) {
Dave Chinner24513372014-06-25 14:58:08 +1000140 xfs_buf_ioerror(bp, -EFSBADCRC);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000141 break;
142 }
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000143 if (!xfs_attr3_rmt_verify(mp, ptr, blksize, bno)) {
Dave Chinner24513372014-06-25 14:58:08 +1000144 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000145 break;
146 }
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000147 len -= blksize;
148 ptr += blksize;
149 bno += BTOBB(blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000150 }
151
Eric Sandeence5028c2014-02-27 15:23:10 +1100152 if (bp->b_error)
153 xfs_verifier_error(bp);
154 else
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000155 ASSERT(len == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100156}
157
158static void
159xfs_attr3_rmt_write_verify(
160 struct xfs_buf *bp)
161{
162 struct xfs_mount *mp = bp->b_target->bt_mount;
163 struct xfs_buf_log_item *bip = bp->b_fspriv;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000164 char *ptr;
165 int len;
166 xfs_daddr_t bno;
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000167 int blksize = mp->m_attr_geo->blksize;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100168
169 /* no verification of non-crc buffers */
170 if (!xfs_sb_version_hascrc(&mp->m_sb))
171 return;
172
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000173 ptr = bp->b_addr;
174 bno = bp->b_bn;
175 len = BBTOB(bp->b_length);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000176 ASSERT(len >= blksize);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100177
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000178 while (len > 0) {
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000179 if (!xfs_attr3_rmt_verify(mp, ptr, blksize, bno)) {
Dave Chinner24513372014-06-25 14:58:08 +1000180 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100181 xfs_verifier_error(bp);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000182 return;
183 }
184 if (bip) {
185 struct xfs_attr3_rmt_hdr *rmt;
186
187 rmt = (struct xfs_attr3_rmt_hdr *)ptr;
188 rmt->rm_lsn = cpu_to_be64(bip->bli_item.li_lsn);
189 }
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000190 xfs_update_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000191
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000192 len -= blksize;
193 ptr += blksize;
194 bno += BTOBB(blksize);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100195 }
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000196 ASSERT(len == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100197}
198
199const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
200 .verify_read = xfs_attr3_rmt_read_verify,
201 .verify_write = xfs_attr3_rmt_write_verify,
202};
203
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000204STATIC int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100205xfs_attr3_rmt_hdr_set(
206 struct xfs_mount *mp,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000207 void *ptr,
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100208 xfs_ino_t ino,
209 uint32_t offset,
210 uint32_t size,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000211 xfs_daddr_t bno)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100212{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000213 struct xfs_attr3_rmt_hdr *rmt = ptr;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100214
215 if (!xfs_sb_version_hascrc(&mp->m_sb))
216 return 0;
217
218 rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC);
219 rmt->rm_offset = cpu_to_be32(offset);
220 rmt->rm_bytes = cpu_to_be32(size);
221 uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_uuid);
222 rmt->rm_owner = cpu_to_be64(ino);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000223 rmt->rm_blkno = cpu_to_be64(bno);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100224
225 return sizeof(struct xfs_attr3_rmt_hdr);
226}
227
228/*
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000229 * Helper functions to copy attribute data in and out of the one disk extents
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100230 */
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000231STATIC int
232xfs_attr_rmtval_copyout(
233 struct xfs_mount *mp,
234 struct xfs_buf *bp,
235 xfs_ino_t ino,
236 int *offset,
237 int *valuelen,
Dave Chinner836a94a2013-08-12 20:49:44 +1000238 __uint8_t **dst)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100239{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000240 char *src = bp->b_addr;
241 xfs_daddr_t bno = bp->b_bn;
242 int len = BBTOB(bp->b_length);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000243 int blksize = mp->m_attr_geo->blksize;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100244
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000245 ASSERT(len >= blksize);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100246
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000247 while (len > 0 && *valuelen > 0) {
248 int hdr_size = 0;
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000249 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000250
Dave Chinnerc5c249b2013-08-12 20:49:43 +1000251 byte_cnt = min(*valuelen, byte_cnt);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000252
253 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Eric Sandeen6d0081a2014-04-14 18:58:29 +1000254 if (!xfs_attr3_rmt_hdr_ok(src, ino, *offset,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000255 byte_cnt, bno)) {
256 xfs_alert(mp,
257"remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)",
258 bno, *offset, byte_cnt, ino);
Dave Chinner24513372014-06-25 14:58:08 +1000259 return -EFSCORRUPTED;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000260 }
261 hdr_size = sizeof(struct xfs_attr3_rmt_hdr);
262 }
263
264 memcpy(*dst, src + hdr_size, byte_cnt);
265
266 /* roll buffer forwards */
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000267 len -= blksize;
268 src += blksize;
269 bno += BTOBB(blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000270
271 /* roll attribute data forwards */
272 *valuelen -= byte_cnt;
273 *dst += byte_cnt;
274 *offset += byte_cnt;
275 }
276 return 0;
277}
278
279STATIC void
280xfs_attr_rmtval_copyin(
281 struct xfs_mount *mp,
282 struct xfs_buf *bp,
283 xfs_ino_t ino,
284 int *offset,
285 int *valuelen,
Dave Chinner836a94a2013-08-12 20:49:44 +1000286 __uint8_t **src)
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000287{
288 char *dst = bp->b_addr;
289 xfs_daddr_t bno = bp->b_bn;
290 int len = BBTOB(bp->b_length);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000291 int blksize = mp->m_attr_geo->blksize;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000292
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000293 ASSERT(len >= blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000294
295 while (len > 0 && *valuelen > 0) {
296 int hdr_size;
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000297 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000298
299 byte_cnt = min(*valuelen, byte_cnt);
300 hdr_size = xfs_attr3_rmt_hdr_set(mp, dst, ino, *offset,
301 byte_cnt, bno);
302
303 memcpy(dst + hdr_size, *src, byte_cnt);
304
305 /*
306 * If this is the last block, zero the remainder of it.
307 * Check that we are actually the last block, too.
308 */
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000309 if (byte_cnt + hdr_size < blksize) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000310 ASSERT(*valuelen - byte_cnt == 0);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000311 ASSERT(len == blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000312 memset(dst + hdr_size + byte_cnt, 0,
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000313 blksize - hdr_size - byte_cnt);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000314 }
315
316 /* roll buffer forwards */
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000317 len -= blksize;
318 dst += blksize;
319 bno += BTOBB(blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000320
321 /* roll attribute data forwards */
322 *valuelen -= byte_cnt;
323 *src += byte_cnt;
324 *offset += byte_cnt;
325 }
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100326}
327
328/*
Dave Chinner95920cd2013-04-03 16:11:27 +1100329 * Read the value associated with an attribute from the out-of-line buffer
330 * that we stored it in.
331 */
332int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100333xfs_attr_rmtval_get(
334 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100335{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100336 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
337 struct xfs_mount *mp = args->dp->i_mount;
338 struct xfs_buf *bp;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100339 xfs_dablk_t lblkno = args->rmtblkno;
Dave Chinner836a94a2013-08-12 20:49:44 +1000340 __uint8_t *dst = args->value;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000341 int valuelen;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100342 int nmap;
343 int error;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000344 int blkcnt = args->rmtblkcnt;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100345 int i;
346 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100347
348 trace_xfs_attr_rmtval_get(args);
349
350 ASSERT(!(args->flags & ATTR_KERNOVAL));
Dave Chinner8275cdd2014-05-06 07:37:31 +1000351 ASSERT(args->rmtvaluelen == args->valuelen);
Dave Chinner95920cd2013-04-03 16:11:27 +1100352
Dave Chinner8275cdd2014-05-06 07:37:31 +1000353 valuelen = args->rmtvaluelen;
Dave Chinner95920cd2013-04-03 16:11:27 +1100354 while (valuelen > 0) {
355 nmap = ATTR_RMTVALUE_MAPSIZE;
356 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
Dave Chinner551b3822013-05-21 18:02:02 +1000357 blkcnt, map, &nmap,
Dave Chinner95920cd2013-04-03 16:11:27 +1100358 XFS_BMAPI_ATTRFORK);
359 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100360 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100361 ASSERT(nmap >= 1);
362
363 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000364 xfs_daddr_t dblkno;
365 int dblkcnt;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100366
Dave Chinner95920cd2013-04-03 16:11:27 +1100367 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
368 (map[i].br_startblock != HOLESTARTBLOCK));
369 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000370 dblkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100371 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000372 dblkno, dblkcnt, 0, &bp,
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100373 &xfs_attr3_rmt_buf_ops);
Dave Chinner95920cd2013-04-03 16:11:27 +1100374 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100375 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100376
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000377 error = xfs_attr_rmtval_copyout(mp, bp, args->dp->i_ino,
378 &offset, &valuelen,
379 &dst);
Dave Chinner95920cd2013-04-03 16:11:27 +1100380 xfs_buf_relse(bp);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000381 if (error)
382 return error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100383
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000384 /* roll attribute extent map forwards */
Dave Chinner95920cd2013-04-03 16:11:27 +1100385 lblkno += map[i].br_blockcount;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000386 blkcnt -= map[i].br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100387 }
388 }
389 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100390 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100391}
392
393/*
394 * Write the value associated with an attribute into the out-of-line buffer
395 * that we have defined for it.
396 */
397int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100398xfs_attr_rmtval_set(
399 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100400{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100401 struct xfs_inode *dp = args->dp;
402 struct xfs_mount *mp = dp->i_mount;
403 struct xfs_bmbt_irec map;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100404 xfs_dablk_t lblkno;
405 xfs_fileoff_t lfileoff = 0;
Dave Chinner836a94a2013-08-12 20:49:44 +1000406 __uint8_t *src = args->value;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100407 int blkcnt;
408 int valuelen;
409 int nmap;
410 int error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100411 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100412
413 trace_xfs_attr_rmtval_set(args);
414
Dave Chinner95920cd2013-04-03 16:11:27 +1100415 /*
416 * Find a "hole" in the attribute address space large enough for
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100417 * us to drop the new attribute's value into. Because CRC enable
418 * attributes have headers, we can't just do a straight byte to FSB
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000419 * conversion and have to take the header space into account.
Dave Chinner95920cd2013-04-03 16:11:27 +1100420 */
Dave Chinner8275cdd2014-05-06 07:37:31 +1000421 blkcnt = xfs_attr3_rmt_blocks(mp, args->rmtvaluelen);
Dave Chinner95920cd2013-04-03 16:11:27 +1100422 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
423 XFS_ATTR_FORK);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100424 if (error)
425 return error;
426
Dave Chinner95920cd2013-04-03 16:11:27 +1100427 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
428 args->rmtblkcnt = blkcnt;
429
430 /*
431 * Roll through the "value", allocating blocks on disk as required.
432 */
433 while (blkcnt > 0) {
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100434 int committed;
435
Dave Chinner95920cd2013-04-03 16:11:27 +1100436 /*
437 * Allocate a single extent, up to the size of the value.
438 */
439 xfs_bmap_init(args->flist, args->firstblock);
440 nmap = 1;
441 error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
442 blkcnt,
443 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
444 args->firstblock, args->total, &map, &nmap,
445 args->flist);
446 if (!error) {
447 error = xfs_bmap_finish(&args->trans, args->flist,
448 &committed);
449 }
450 if (error) {
451 ASSERT(committed);
452 args->trans = NULL;
453 xfs_bmap_cancel(args->flist);
Eric Sandeend99831f2014-06-22 15:03:54 +1000454 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100455 }
456
457 /*
458 * bmap_finish() may have committed the last trans and started
459 * a new one. We need the inode to be in all transactions.
460 */
461 if (committed)
462 xfs_trans_ijoin(args->trans, dp, 0);
463
464 ASSERT(nmap == 1);
465 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
466 (map.br_startblock != HOLESTARTBLOCK));
467 lblkno += map.br_blockcount;
468 blkcnt -= map.br_blockcount;
469
470 /*
471 * Start the next trans in the chain.
472 */
473 error = xfs_trans_roll(&args->trans, dp);
474 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000475 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100476 }
477
478 /*
479 * Roll through the "value", copying the attribute value to the
480 * already-allocated blocks. Blocks are written synchronously
481 * so that we can know they are all on disk before we turn off
482 * the INCOMPLETE flag.
483 */
484 lblkno = args->rmtblkno;
Dave Chinner26f71442013-05-21 18:02:03 +1000485 blkcnt = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000486 valuelen = args->rmtvaluelen;
Dave Chinner95920cd2013-04-03 16:11:27 +1100487 while (valuelen > 0) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000488 struct xfs_buf *bp;
489 xfs_daddr_t dblkno;
490 int dblkcnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100491
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000492 ASSERT(blkcnt > 0);
493
Dave Chinner95920cd2013-04-03 16:11:27 +1100494 xfs_bmap_init(args->flist, args->firstblock);
495 nmap = 1;
496 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
Dave Chinner26f71442013-05-21 18:02:03 +1000497 blkcnt, &map, &nmap,
Dave Chinner95920cd2013-04-03 16:11:27 +1100498 XFS_BMAPI_ATTRFORK);
499 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000500 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100501 ASSERT(nmap == 1);
502 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
503 (map.br_startblock != HOLESTARTBLOCK));
504
505 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
Dave Chinner26f71442013-05-21 18:02:03 +1000506 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100507
Dave Chinner26f71442013-05-21 18:02:03 +1000508 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0);
Dave Chinner95920cd2013-04-03 16:11:27 +1100509 if (!bp)
Dave Chinner24513372014-06-25 14:58:08 +1000510 return -ENOMEM;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100511 bp->b_ops = &xfs_attr3_rmt_buf_ops;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100512
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000513 xfs_attr_rmtval_copyin(mp, bp, args->dp->i_ino, &offset,
514 &valuelen, &src);
Dave Chinner95920cd2013-04-03 16:11:27 +1100515
516 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
517 xfs_buf_relse(bp);
518 if (error)
519 return error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100520
Dave Chinner95920cd2013-04-03 16:11:27 +1100521
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000522 /* roll attribute extent map forwards */
Dave Chinner95920cd2013-04-03 16:11:27 +1100523 lblkno += map.br_blockcount;
Dave Chinner26f71442013-05-21 18:02:03 +1000524 blkcnt -= map.br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100525 }
526 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100527 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100528}
529
530/*
531 * Remove the value associated with an attribute by deleting the
532 * out-of-line buffer that it is stored on.
533 */
534int
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000535xfs_attr_rmtval_remove(
536 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100537{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000538 struct xfs_mount *mp = args->dp->i_mount;
539 xfs_dablk_t lblkno;
540 int blkcnt;
541 int error;
542 int done;
Dave Chinner95920cd2013-04-03 16:11:27 +1100543
544 trace_xfs_attr_rmtval_remove(args);
545
Dave Chinner95920cd2013-04-03 16:11:27 +1100546 /*
Dave Chinner58a72282013-05-21 18:02:04 +1000547 * Roll through the "value", invalidating the attribute value's blocks.
Dave Chinner95920cd2013-04-03 16:11:27 +1100548 */
549 lblkno = args->rmtblkno;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000550 blkcnt = args->rmtblkcnt;
551 while (blkcnt > 0) {
552 struct xfs_bmbt_irec map;
553 struct xfs_buf *bp;
554 xfs_daddr_t dblkno;
555 int dblkcnt;
556 int nmap;
Dave Chinner58a72282013-05-21 18:02:04 +1000557
Dave Chinner95920cd2013-04-03 16:11:27 +1100558 /*
559 * Try to remember where we decided to put the value.
560 */
561 nmap = 1;
562 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
Dave Chinner58a72282013-05-21 18:02:04 +1000563 blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
Dave Chinner95920cd2013-04-03 16:11:27 +1100564 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000565 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100566 ASSERT(nmap == 1);
567 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
568 (map.br_startblock != HOLESTARTBLOCK));
569
570 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
Dave Chinner58a72282013-05-21 18:02:04 +1000571 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100572
573 /*
574 * If the "remote" value is in the cache, remove it.
575 */
Dave Chinner58a72282013-05-21 18:02:04 +1000576 bp = xfs_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK);
Dave Chinner95920cd2013-04-03 16:11:27 +1100577 if (bp) {
578 xfs_buf_stale(bp);
579 xfs_buf_relse(bp);
580 bp = NULL;
581 }
582
Dave Chinner95920cd2013-04-03 16:11:27 +1100583 lblkno += map.br_blockcount;
Dave Chinner58a72282013-05-21 18:02:04 +1000584 blkcnt -= map.br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100585 }
586
587 /*
588 * Keep de-allocating extents until the remote-value region is gone.
589 */
590 lblkno = args->rmtblkno;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000591 blkcnt = args->rmtblkcnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100592 done = 0;
593 while (!done) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000594 int committed;
595
Dave Chinner95920cd2013-04-03 16:11:27 +1100596 xfs_bmap_init(args->flist, args->firstblock);
597 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
598 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
599 1, args->firstblock, args->flist,
600 &done);
601 if (!error) {
602 error = xfs_bmap_finish(&args->trans, args->flist,
603 &committed);
604 }
605 if (error) {
606 ASSERT(committed);
607 args->trans = NULL;
608 xfs_bmap_cancel(args->flist);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100609 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100610 }
611
612 /*
613 * bmap_finish() may have committed the last trans and started
614 * a new one. We need the inode to be in all transactions.
615 */
616 if (committed)
617 xfs_trans_ijoin(args->trans, args->dp, 0);
618
619 /*
620 * Close out trans and start the next one in the chain.
621 */
622 error = xfs_trans_roll(&args->trans, args->dp);
623 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000624 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100625 }
Eric Sandeend99831f2014-06-22 15:03:54 +1000626 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100627}