blob: f3ed9bf0b0655e2fd746da915aa095ffd1280523 [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_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110027#include "xfs_da_format.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110028#include "xfs_da_btree.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110029#include "xfs_inode.h"
30#include "xfs_alloc.h"
Dave Chinner239880e2013-10-23 10:50:10 +110031#include "xfs_trans.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110032#include "xfs_inode_item.h"
33#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100034#include "xfs_bmap_util.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110035#include "xfs_attr.h"
36#include "xfs_attr_leaf.h"
37#include "xfs_attr_remote.h"
38#include "xfs_trans_space.h"
39#include "xfs_trace.h"
Dave Chinnerd2e448d2013-04-03 16:11:28 +110040#include "xfs_cksum.h"
41#include "xfs_buf_item.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110042#include "xfs_error.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 */
Dave Chinner7bc0dc22013-05-21 18:02:08 +100050int
Dave Chinnerd2e448d2013-04-03 16:11:28 +110051xfs_attr3_rmt_blocks(
52 struct xfs_mount *mp,
53 int attrlen)
54{
Dave Chinner551b3822013-05-21 18:02:02 +100055 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);
Dave Chinnerd2e448d2013-04-03 16:11:28 +110060}
61
Dave Chinner7bc0dc22013-05-21 18:02:08 +100062/*
63 * Checking of the remote attribute header is split into two parts. The verifier
64 * does CRC, location and bounds checking, the unpacking function checks the
65 * attribute parameters and owner.
66 */
67static bool
68xfs_attr3_rmt_hdr_ok(
Dave Chinner7bc0dc22013-05-21 18:02:08 +100069 void *ptr,
70 xfs_ino_t ino,
71 uint32_t offset,
72 uint32_t size,
73 xfs_daddr_t bno)
74{
75 struct xfs_attr3_rmt_hdr *rmt = ptr;
76
77 if (bno != be64_to_cpu(rmt->rm_blkno))
78 return false;
79 if (offset != be32_to_cpu(rmt->rm_offset))
80 return false;
81 if (size != be32_to_cpu(rmt->rm_bytes))
82 return false;
83 if (ino != be64_to_cpu(rmt->rm_owner))
84 return false;
85
86 /* ok */
87 return true;
88}
89
Dave Chinnerd2e448d2013-04-03 16:11:28 +110090static bool
91xfs_attr3_rmt_verify(
Dave Chinner7bc0dc22013-05-21 18:02:08 +100092 struct xfs_mount *mp,
93 void *ptr,
94 int fsbsize,
95 xfs_daddr_t bno)
Dave Chinnerd2e448d2013-04-03 16:11:28 +110096{
Dave Chinner7bc0dc22013-05-21 18:02:08 +100097 struct xfs_attr3_rmt_hdr *rmt = ptr;
Dave Chinnerd2e448d2013-04-03 16:11:28 +110098
99 if (!xfs_sb_version_hascrc(&mp->m_sb))
100 return false;
101 if (rmt->rm_magic != cpu_to_be32(XFS_ATTR3_RMT_MAGIC))
102 return false;
Eric Sandeence748ea2015-07-29 11:53:31 +1000103 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100104 return false;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000105 if (be64_to_cpu(rmt->rm_blkno) != bno)
106 return false;
107 if (be32_to_cpu(rmt->rm_bytes) > fsbsize - sizeof(*rmt))
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100108 return false;
109 if (be32_to_cpu(rmt->rm_offset) +
Jan Tulak51fcbfe2015-10-12 16:03:59 +1100110 be32_to_cpu(rmt->rm_bytes) > XFS_XATTR_SIZE_MAX)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100111 return false;
112 if (rmt->rm_owner == 0)
113 return false;
114
115 return true;
116}
117
118static void
119xfs_attr3_rmt_read_verify(
120 struct xfs_buf *bp)
121{
122 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000123 char *ptr;
124 int len;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000125 xfs_daddr_t bno;
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000126 int blksize = mp->m_attr_geo->blksize;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100127
128 /* no verification of non-crc buffers */
129 if (!xfs_sb_version_hascrc(&mp->m_sb))
130 return;
131
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000132 ptr = bp->b_addr;
133 bno = bp->b_bn;
134 len = BBTOB(bp->b_length);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000135 ASSERT(len >= blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000136
137 while (len > 0) {
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000138 if (!xfs_verify_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF)) {
Dave Chinner24513372014-06-25 14:58:08 +1000139 xfs_buf_ioerror(bp, -EFSBADCRC);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000140 break;
141 }
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000142 if (!xfs_attr3_rmt_verify(mp, ptr, blksize, bno)) {
Dave Chinner24513372014-06-25 14:58:08 +1000143 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000144 break;
145 }
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000146 len -= blksize;
147 ptr += blksize;
148 bno += BTOBB(blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000149 }
150
Eric Sandeence5028c2014-02-27 15:23:10 +1100151 if (bp->b_error)
152 xfs_verifier_error(bp);
153 else
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000154 ASSERT(len == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100155}
156
157static void
158xfs_attr3_rmt_write_verify(
159 struct xfs_buf *bp)
160{
161 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinnere3c32ee2015-07-29 11:48:01 +1000162 int blksize = mp->m_attr_geo->blksize;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000163 char *ptr;
164 int len;
165 xfs_daddr_t bno;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100166
167 /* no verification of non-crc buffers */
168 if (!xfs_sb_version_hascrc(&mp->m_sb))
169 return;
170
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000171 ptr = bp->b_addr;
172 bno = bp->b_bn;
173 len = BBTOB(bp->b_length);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000174 ASSERT(len >= blksize);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100175
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000176 while (len > 0) {
Dave Chinnere3c32ee2015-07-29 11:48:01 +1000177 struct xfs_attr3_rmt_hdr *rmt = (struct xfs_attr3_rmt_hdr *)ptr;
178
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 }
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000184
Dave Chinnere3c32ee2015-07-29 11:48:01 +1000185 /*
186 * Ensure we aren't writing bogus LSNs to disk. See
187 * xfs_attr3_rmt_hdr_set() for the explanation.
188 */
189 if (rmt->rm_lsn != cpu_to_be64(NULLCOMMITLSN)) {
190 xfs_buf_ioerror(bp, -EFSCORRUPTED);
191 xfs_verifier_error(bp);
192 return;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000193 }
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000194 xfs_update_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000195
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000196 len -= blksize;
197 ptr += blksize;
198 bno += BTOBB(blksize);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100199 }
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000200 ASSERT(len == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100201}
202
203const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100204 .name = "xfs_attr3_rmt",
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100205 .verify_read = xfs_attr3_rmt_read_verify,
206 .verify_write = xfs_attr3_rmt_write_verify,
207};
208
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000209STATIC int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100210xfs_attr3_rmt_hdr_set(
211 struct xfs_mount *mp,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000212 void *ptr,
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100213 xfs_ino_t ino,
214 uint32_t offset,
215 uint32_t size,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000216 xfs_daddr_t bno)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100217{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000218 struct xfs_attr3_rmt_hdr *rmt = ptr;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100219
220 if (!xfs_sb_version_hascrc(&mp->m_sb))
221 return 0;
222
223 rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC);
224 rmt->rm_offset = cpu_to_be32(offset);
225 rmt->rm_bytes = cpu_to_be32(size);
Eric Sandeence748ea2015-07-29 11:53:31 +1000226 uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100227 rmt->rm_owner = cpu_to_be64(ino);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000228 rmt->rm_blkno = cpu_to_be64(bno);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100229
Dave Chinnere3c32ee2015-07-29 11:48:01 +1000230 /*
231 * Remote attribute blocks are written synchronously, so we don't
232 * have an LSN that we can stamp in them that makes any sense to log
233 * recovery. To ensure that log recovery handles overwrites of these
234 * blocks sanely (i.e. once they've been freed and reallocated as some
235 * other type of metadata) we need to ensure that the LSN has a value
236 * that tells log recovery to ignore the LSN and overwrite the buffer
237 * with whatever is in it's log. To do this, we use the magic
238 * NULLCOMMITLSN to indicate that the LSN is invalid.
239 */
240 rmt->rm_lsn = cpu_to_be64(NULLCOMMITLSN);
241
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100242 return sizeof(struct xfs_attr3_rmt_hdr);
243}
244
245/*
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000246 * Helper functions to copy attribute data in and out of the one disk extents
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100247 */
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000248STATIC int
249xfs_attr_rmtval_copyout(
250 struct xfs_mount *mp,
251 struct xfs_buf *bp,
252 xfs_ino_t ino,
253 int *offset,
254 int *valuelen,
Dave Chinner836a94a2013-08-12 20:49:44 +1000255 __uint8_t **dst)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100256{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000257 char *src = bp->b_addr;
258 xfs_daddr_t bno = bp->b_bn;
259 int len = BBTOB(bp->b_length);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000260 int blksize = mp->m_attr_geo->blksize;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100261
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000262 ASSERT(len >= blksize);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100263
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000264 while (len > 0 && *valuelen > 0) {
265 int hdr_size = 0;
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000266 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000267
Dave Chinnerc5c249b2013-08-12 20:49:43 +1000268 byte_cnt = min(*valuelen, byte_cnt);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000269
270 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Eric Sandeen6d0081a2014-04-14 18:58:29 +1000271 if (!xfs_attr3_rmt_hdr_ok(src, ino, *offset,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000272 byte_cnt, bno)) {
273 xfs_alert(mp,
274"remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)",
275 bno, *offset, byte_cnt, ino);
Dave Chinner24513372014-06-25 14:58:08 +1000276 return -EFSCORRUPTED;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000277 }
278 hdr_size = sizeof(struct xfs_attr3_rmt_hdr);
279 }
280
281 memcpy(*dst, src + hdr_size, byte_cnt);
282
283 /* roll buffer forwards */
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000284 len -= blksize;
285 src += blksize;
286 bno += BTOBB(blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000287
288 /* roll attribute data forwards */
289 *valuelen -= byte_cnt;
290 *dst += byte_cnt;
291 *offset += byte_cnt;
292 }
293 return 0;
294}
295
296STATIC void
297xfs_attr_rmtval_copyin(
298 struct xfs_mount *mp,
299 struct xfs_buf *bp,
300 xfs_ino_t ino,
301 int *offset,
302 int *valuelen,
Dave Chinner836a94a2013-08-12 20:49:44 +1000303 __uint8_t **src)
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000304{
305 char *dst = bp->b_addr;
306 xfs_daddr_t bno = bp->b_bn;
307 int len = BBTOB(bp->b_length);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000308 int blksize = mp->m_attr_geo->blksize;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000309
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000310 ASSERT(len >= blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000311
312 while (len > 0 && *valuelen > 0) {
313 int hdr_size;
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000314 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000315
316 byte_cnt = min(*valuelen, byte_cnt);
317 hdr_size = xfs_attr3_rmt_hdr_set(mp, dst, ino, *offset,
318 byte_cnt, bno);
319
320 memcpy(dst + hdr_size, *src, byte_cnt);
321
322 /*
323 * If this is the last block, zero the remainder of it.
324 * Check that we are actually the last block, too.
325 */
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000326 if (byte_cnt + hdr_size < blksize) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000327 ASSERT(*valuelen - byte_cnt == 0);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000328 ASSERT(len == blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000329 memset(dst + hdr_size + byte_cnt, 0,
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000330 blksize - hdr_size - byte_cnt);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000331 }
332
333 /* roll buffer forwards */
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000334 len -= blksize;
335 dst += blksize;
336 bno += BTOBB(blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000337
338 /* roll attribute data forwards */
339 *valuelen -= byte_cnt;
340 *src += byte_cnt;
341 *offset += byte_cnt;
342 }
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100343}
344
345/*
Dave Chinner95920cd2013-04-03 16:11:27 +1100346 * Read the value associated with an attribute from the out-of-line buffer
347 * that we stored it in.
348 */
349int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100350xfs_attr_rmtval_get(
351 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100352{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100353 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
354 struct xfs_mount *mp = args->dp->i_mount;
355 struct xfs_buf *bp;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100356 xfs_dablk_t lblkno = args->rmtblkno;
Dave Chinner836a94a2013-08-12 20:49:44 +1000357 __uint8_t *dst = args->value;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000358 int valuelen;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100359 int nmap;
360 int error;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000361 int blkcnt = args->rmtblkcnt;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100362 int i;
363 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100364
365 trace_xfs_attr_rmtval_get(args);
366
367 ASSERT(!(args->flags & ATTR_KERNOVAL));
Dave Chinner8275cdd2014-05-06 07:37:31 +1000368 ASSERT(args->rmtvaluelen == args->valuelen);
Dave Chinner95920cd2013-04-03 16:11:27 +1100369
Dave Chinner8275cdd2014-05-06 07:37:31 +1000370 valuelen = args->rmtvaluelen;
Dave Chinner95920cd2013-04-03 16:11:27 +1100371 while (valuelen > 0) {
372 nmap = ATTR_RMTVALUE_MAPSIZE;
373 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
Dave Chinner551b3822013-05-21 18:02:02 +1000374 blkcnt, map, &nmap,
Dave Chinner95920cd2013-04-03 16:11:27 +1100375 XFS_BMAPI_ATTRFORK);
376 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100377 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100378 ASSERT(nmap >= 1);
379
380 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000381 xfs_daddr_t dblkno;
382 int dblkcnt;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100383
Dave Chinner95920cd2013-04-03 16:11:27 +1100384 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
385 (map[i].br_startblock != HOLESTARTBLOCK));
386 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000387 dblkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100388 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000389 dblkno, dblkcnt, 0, &bp,
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100390 &xfs_attr3_rmt_buf_ops);
Dave Chinner95920cd2013-04-03 16:11:27 +1100391 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100392 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100393
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000394 error = xfs_attr_rmtval_copyout(mp, bp, args->dp->i_ino,
395 &offset, &valuelen,
396 &dst);
Dave Chinner95920cd2013-04-03 16:11:27 +1100397 xfs_buf_relse(bp);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000398 if (error)
399 return error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100400
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000401 /* roll attribute extent map forwards */
Dave Chinner95920cd2013-04-03 16:11:27 +1100402 lblkno += map[i].br_blockcount;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000403 blkcnt -= map[i].br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100404 }
405 }
406 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100407 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100408}
409
410/*
411 * Write the value associated with an attribute into the out-of-line buffer
412 * that we have defined for it.
413 */
414int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100415xfs_attr_rmtval_set(
416 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100417{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100418 struct xfs_inode *dp = args->dp;
419 struct xfs_mount *mp = dp->i_mount;
420 struct xfs_bmbt_irec map;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100421 xfs_dablk_t lblkno;
422 xfs_fileoff_t lfileoff = 0;
Dave Chinner836a94a2013-08-12 20:49:44 +1000423 __uint8_t *src = args->value;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100424 int blkcnt;
425 int valuelen;
426 int nmap;
427 int error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100428 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100429
430 trace_xfs_attr_rmtval_set(args);
431
Dave Chinner95920cd2013-04-03 16:11:27 +1100432 /*
433 * Find a "hole" in the attribute address space large enough for
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100434 * us to drop the new attribute's value into. Because CRC enable
435 * attributes have headers, we can't just do a straight byte to FSB
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000436 * conversion and have to take the header space into account.
Dave Chinner95920cd2013-04-03 16:11:27 +1100437 */
Dave Chinner8275cdd2014-05-06 07:37:31 +1000438 blkcnt = xfs_attr3_rmt_blocks(mp, args->rmtvaluelen);
Dave Chinner95920cd2013-04-03 16:11:27 +1100439 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
440 XFS_ATTR_FORK);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100441 if (error)
442 return error;
443
Dave Chinner95920cd2013-04-03 16:11:27 +1100444 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
445 args->rmtblkcnt = blkcnt;
446
447 /*
448 * Roll through the "value", allocating blocks on disk as required.
449 */
450 while (blkcnt > 0) {
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100451 int committed;
452
Dave Chinner95920cd2013-04-03 16:11:27 +1100453 /*
454 * Allocate a single extent, up to the size of the value.
Dave Chinnerdf150ed12015-07-29 11:48:02 +1000455 *
456 * Note that we have to consider this a data allocation as we
457 * write the remote attribute without logging the contents.
458 * Hence we must ensure that we aren't using blocks that are on
459 * the busy list so that we don't overwrite blocks which have
460 * recently been freed but their transactions are not yet
461 * committed to disk. If we overwrite the contents of a busy
462 * extent and then crash then the block may not contain the
463 * correct metadata after log recovery occurs.
Dave Chinner95920cd2013-04-03 16:11:27 +1100464 */
465 xfs_bmap_init(args->flist, args->firstblock);
466 nmap = 1;
467 error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
Dave Chinnerdf150ed12015-07-29 11:48:02 +1000468 blkcnt, XFS_BMAPI_ATTRFORK, args->firstblock,
469 args->total, &map, &nmap, args->flist);
Dave Chinner95920cd2013-04-03 16:11:27 +1100470 if (!error) {
471 error = xfs_bmap_finish(&args->trans, args->flist,
472 &committed);
473 }
474 if (error) {
475 ASSERT(committed);
476 args->trans = NULL;
477 xfs_bmap_cancel(args->flist);
Eric Sandeend99831f2014-06-22 15:03:54 +1000478 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100479 }
480
481 /*
482 * bmap_finish() may have committed the last trans and started
483 * a new one. We need the inode to be in all transactions.
484 */
485 if (committed)
486 xfs_trans_ijoin(args->trans, dp, 0);
487
488 ASSERT(nmap == 1);
489 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
490 (map.br_startblock != HOLESTARTBLOCK));
491 lblkno += map.br_blockcount;
492 blkcnt -= map.br_blockcount;
493
494 /*
495 * Start the next trans in the chain.
496 */
497 error = xfs_trans_roll(&args->trans, dp);
498 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000499 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100500 }
501
502 /*
503 * Roll through the "value", copying the attribute value to the
504 * already-allocated blocks. Blocks are written synchronously
505 * so that we can know they are all on disk before we turn off
506 * the INCOMPLETE flag.
507 */
508 lblkno = args->rmtblkno;
Dave Chinner26f71442013-05-21 18:02:03 +1000509 blkcnt = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000510 valuelen = args->rmtvaluelen;
Dave Chinner95920cd2013-04-03 16:11:27 +1100511 while (valuelen > 0) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000512 struct xfs_buf *bp;
513 xfs_daddr_t dblkno;
514 int dblkcnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100515
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000516 ASSERT(blkcnt > 0);
517
Dave Chinner95920cd2013-04-03 16:11:27 +1100518 xfs_bmap_init(args->flist, args->firstblock);
519 nmap = 1;
520 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
Dave Chinner26f71442013-05-21 18:02:03 +1000521 blkcnt, &map, &nmap,
Dave Chinner95920cd2013-04-03 16:11:27 +1100522 XFS_BMAPI_ATTRFORK);
523 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000524 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100525 ASSERT(nmap == 1);
526 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
527 (map.br_startblock != HOLESTARTBLOCK));
528
529 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
Dave Chinner26f71442013-05-21 18:02:03 +1000530 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100531
Dave Chinner26f71442013-05-21 18:02:03 +1000532 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0);
Dave Chinner95920cd2013-04-03 16:11:27 +1100533 if (!bp)
Dave Chinner24513372014-06-25 14:58:08 +1000534 return -ENOMEM;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100535 bp->b_ops = &xfs_attr3_rmt_buf_ops;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100536
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000537 xfs_attr_rmtval_copyin(mp, bp, args->dp->i_ino, &offset,
538 &valuelen, &src);
Dave Chinner95920cd2013-04-03 16:11:27 +1100539
540 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
541 xfs_buf_relse(bp);
542 if (error)
543 return error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100544
Dave Chinner95920cd2013-04-03 16:11:27 +1100545
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000546 /* roll attribute extent map forwards */
Dave Chinner95920cd2013-04-03 16:11:27 +1100547 lblkno += map.br_blockcount;
Dave Chinner26f71442013-05-21 18:02:03 +1000548 blkcnt -= map.br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100549 }
550 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100551 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100552}
553
554/*
555 * Remove the value associated with an attribute by deleting the
556 * out-of-line buffer that it is stored on.
557 */
558int
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000559xfs_attr_rmtval_remove(
560 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100561{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000562 struct xfs_mount *mp = args->dp->i_mount;
563 xfs_dablk_t lblkno;
564 int blkcnt;
565 int error;
566 int done;
Dave Chinner95920cd2013-04-03 16:11:27 +1100567
568 trace_xfs_attr_rmtval_remove(args);
569
Dave Chinner95920cd2013-04-03 16:11:27 +1100570 /*
Dave Chinner58a72282013-05-21 18:02:04 +1000571 * Roll through the "value", invalidating the attribute value's blocks.
Dave Chinner95920cd2013-04-03 16:11:27 +1100572 */
573 lblkno = args->rmtblkno;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000574 blkcnt = args->rmtblkcnt;
575 while (blkcnt > 0) {
576 struct xfs_bmbt_irec map;
577 struct xfs_buf *bp;
578 xfs_daddr_t dblkno;
579 int dblkcnt;
580 int nmap;
Dave Chinner58a72282013-05-21 18:02:04 +1000581
Dave Chinner95920cd2013-04-03 16:11:27 +1100582 /*
583 * Try to remember where we decided to put the value.
584 */
585 nmap = 1;
586 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
Dave Chinner58a72282013-05-21 18:02:04 +1000587 blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
Dave Chinner95920cd2013-04-03 16:11:27 +1100588 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000589 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100590 ASSERT(nmap == 1);
591 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
592 (map.br_startblock != HOLESTARTBLOCK));
593
594 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
Dave Chinner58a72282013-05-21 18:02:04 +1000595 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100596
597 /*
598 * If the "remote" value is in the cache, remove it.
599 */
Dave Chinner58a72282013-05-21 18:02:04 +1000600 bp = xfs_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK);
Dave Chinner95920cd2013-04-03 16:11:27 +1100601 if (bp) {
602 xfs_buf_stale(bp);
603 xfs_buf_relse(bp);
604 bp = NULL;
605 }
606
Dave Chinner95920cd2013-04-03 16:11:27 +1100607 lblkno += map.br_blockcount;
Dave Chinner58a72282013-05-21 18:02:04 +1000608 blkcnt -= map.br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100609 }
610
611 /*
612 * Keep de-allocating extents until the remote-value region is gone.
613 */
614 lblkno = args->rmtblkno;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000615 blkcnt = args->rmtblkcnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100616 done = 0;
617 while (!done) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000618 int committed;
619
Dave Chinner95920cd2013-04-03 16:11:27 +1100620 xfs_bmap_init(args->flist, args->firstblock);
621 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
Dave Chinnerab7bb612015-07-29 11:51:01 +1000622 XFS_BMAPI_ATTRFORK, 1, args->firstblock,
623 args->flist, &done);
Dave Chinner95920cd2013-04-03 16:11:27 +1100624 if (!error) {
625 error = xfs_bmap_finish(&args->trans, args->flist,
626 &committed);
627 }
628 if (error) {
629 ASSERT(committed);
630 args->trans = NULL;
631 xfs_bmap_cancel(args->flist);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100632 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100633 }
634
635 /*
636 * bmap_finish() may have committed the last trans and started
637 * a new one. We need the inode to be in all transactions.
638 */
639 if (committed)
640 xfs_trans_ijoin(args->trans, args->dp, 0);
641
642 /*
643 * Close out trans and start the next one in the chain.
644 */
645 error = xfs_trans_roll(&args->trans, args->dp);
646 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000647 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100648 }
Eric Sandeend99831f2014-06-22 15:03:54 +1000649 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100650}