blob: 433c36714e4081145087568d1dedd0a297d8a6f8 [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"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.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;
Eric Sandeence748ea2015-07-29 11:53:31 +1000104 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100105 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) +
Jan Tulak51fcbfe2015-10-12 16:03:59 +1100111 be32_to_cpu(rmt->rm_bytes) > XFS_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;
Dave Chinnere3c32ee2015-07-29 11:48:01 +1000163 int blksize = mp->m_attr_geo->blksize;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000164 char *ptr;
165 int len;
166 xfs_daddr_t bno;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100167
168 /* no verification of non-crc buffers */
169 if (!xfs_sb_version_hascrc(&mp->m_sb))
170 return;
171
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000172 ptr = bp->b_addr;
173 bno = bp->b_bn;
174 len = BBTOB(bp->b_length);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000175 ASSERT(len >= blksize);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100176
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000177 while (len > 0) {
Dave Chinnere3c32ee2015-07-29 11:48:01 +1000178 struct xfs_attr3_rmt_hdr *rmt = (struct xfs_attr3_rmt_hdr *)ptr;
179
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000180 if (!xfs_attr3_rmt_verify(mp, ptr, blksize, bno)) {
Dave Chinner24513372014-06-25 14:58:08 +1000181 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100182 xfs_verifier_error(bp);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000183 return;
184 }
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000185
Dave Chinnere3c32ee2015-07-29 11:48:01 +1000186 /*
187 * Ensure we aren't writing bogus LSNs to disk. See
188 * xfs_attr3_rmt_hdr_set() for the explanation.
189 */
190 if (rmt->rm_lsn != cpu_to_be64(NULLCOMMITLSN)) {
191 xfs_buf_ioerror(bp, -EFSCORRUPTED);
192 xfs_verifier_error(bp);
193 return;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000194 }
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000195 xfs_update_cksum(ptr, blksize, XFS_ATTR3_RMT_CRC_OFF);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000196
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000197 len -= blksize;
198 ptr += blksize;
199 bno += BTOBB(blksize);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100200 }
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000201 ASSERT(len == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100202}
203
204const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100205 .name = "xfs_attr3_rmt",
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100206 .verify_read = xfs_attr3_rmt_read_verify,
207 .verify_write = xfs_attr3_rmt_write_verify,
208};
209
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000210STATIC int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100211xfs_attr3_rmt_hdr_set(
212 struct xfs_mount *mp,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000213 void *ptr,
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100214 xfs_ino_t ino,
215 uint32_t offset,
216 uint32_t size,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000217 xfs_daddr_t bno)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100218{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000219 struct xfs_attr3_rmt_hdr *rmt = ptr;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100220
221 if (!xfs_sb_version_hascrc(&mp->m_sb))
222 return 0;
223
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);
Eric Sandeence748ea2015-07-29 11:53:31 +1000227 uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100228 rmt->rm_owner = cpu_to_be64(ino);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000229 rmt->rm_blkno = cpu_to_be64(bno);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100230
Dave Chinnere3c32ee2015-07-29 11:48:01 +1000231 /*
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.
240 */
241 rmt->rm_lsn = cpu_to_be64(NULLCOMMITLSN);
242
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100243 return sizeof(struct xfs_attr3_rmt_hdr);
244}
245
246/*
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000247 * Helper functions to copy attribute data in and out of the one disk extents
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100248 */
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000249STATIC int
250xfs_attr_rmtval_copyout(
251 struct xfs_mount *mp,
252 struct xfs_buf *bp,
253 xfs_ino_t ino,
254 int *offset,
255 int *valuelen,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700256 uint8_t **dst)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100257{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000258 char *src = bp->b_addr;
259 xfs_daddr_t bno = bp->b_bn;
260 int len = BBTOB(bp->b_length);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000261 int blksize = mp->m_attr_geo->blksize;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100262
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000263 ASSERT(len >= blksize);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100264
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000265 while (len > 0 && *valuelen > 0) {
266 int hdr_size = 0;
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000267 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000268
Dave Chinnerc5c249b2013-08-12 20:49:43 +1000269 byte_cnt = min(*valuelen, byte_cnt);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000270
271 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Eric Sandeen6d0081a2014-04-14 18:58:29 +1000272 if (!xfs_attr3_rmt_hdr_ok(src, ino, *offset,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000273 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);
Dave Chinner24513372014-06-25 14:58:08 +1000277 return -EFSCORRUPTED;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000278 }
279 hdr_size = sizeof(struct xfs_attr3_rmt_hdr);
280 }
281
282 memcpy(*dst, src + hdr_size, byte_cnt);
283
284 /* roll buffer forwards */
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000285 len -= blksize;
286 src += blksize;
287 bno += BTOBB(blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000288
289 /* roll attribute data forwards */
290 *valuelen -= byte_cnt;
291 *dst += byte_cnt;
292 *offset += byte_cnt;
293 }
294 return 0;
295}
296
297STATIC void
298xfs_attr_rmtval_copyin(
299 struct xfs_mount *mp,
300 struct xfs_buf *bp,
301 xfs_ino_t ino,
302 int *offset,
303 int *valuelen,
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700304 uint8_t **src)
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000305{
306 char *dst = bp->b_addr;
307 xfs_daddr_t bno = bp->b_bn;
308 int len = BBTOB(bp->b_length);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000309 int blksize = mp->m_attr_geo->blksize;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000310
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000311 ASSERT(len >= blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000312
313 while (len > 0 && *valuelen > 0) {
314 int hdr_size;
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000315 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000316
317 byte_cnt = min(*valuelen, byte_cnt);
318 hdr_size = xfs_attr3_rmt_hdr_set(mp, dst, ino, *offset,
319 byte_cnt, bno);
320
321 memcpy(dst + hdr_size, *src, byte_cnt);
322
323 /*
324 * If this is the last block, zero the remainder of it.
325 * Check that we are actually the last block, too.
326 */
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000327 if (byte_cnt + hdr_size < blksize) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000328 ASSERT(*valuelen - byte_cnt == 0);
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000329 ASSERT(len == blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000330 memset(dst + hdr_size + byte_cnt, 0,
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000331 blksize - hdr_size - byte_cnt);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000332 }
333
334 /* roll buffer forwards */
Dave Chinnerc2c4c472014-06-06 15:21:45 +1000335 len -= blksize;
336 dst += blksize;
337 bno += BTOBB(blksize);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000338
339 /* roll attribute data forwards */
340 *valuelen -= byte_cnt;
341 *src += byte_cnt;
342 *offset += byte_cnt;
343 }
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100344}
345
346/*
Dave Chinner95920cd2013-04-03 16:11:27 +1100347 * Read the value associated with an attribute from the out-of-line buffer
348 * that we stored it in.
349 */
350int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100351xfs_attr_rmtval_get(
352 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100353{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100354 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
355 struct xfs_mount *mp = args->dp->i_mount;
356 struct xfs_buf *bp;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100357 xfs_dablk_t lblkno = args->rmtblkno;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700358 uint8_t *dst = args->value;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000359 int valuelen;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100360 int nmap;
361 int error;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000362 int blkcnt = args->rmtblkcnt;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100363 int i;
364 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100365
366 trace_xfs_attr_rmtval_get(args);
367
368 ASSERT(!(args->flags & ATTR_KERNOVAL));
Dave Chinner8275cdd2014-05-06 07:37:31 +1000369 ASSERT(args->rmtvaluelen == args->valuelen);
Dave Chinner95920cd2013-04-03 16:11:27 +1100370
Dave Chinner8275cdd2014-05-06 07:37:31 +1000371 valuelen = args->rmtvaluelen;
Dave Chinner95920cd2013-04-03 16:11:27 +1100372 while (valuelen > 0) {
373 nmap = ATTR_RMTVALUE_MAPSIZE;
374 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
Dave Chinner551b3822013-05-21 18:02:02 +1000375 blkcnt, map, &nmap,
Dave Chinner95920cd2013-04-03 16:11:27 +1100376 XFS_BMAPI_ATTRFORK);
377 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100378 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100379 ASSERT(nmap >= 1);
380
381 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000382 xfs_daddr_t dblkno;
383 int dblkcnt;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100384
Dave Chinner95920cd2013-04-03 16:11:27 +1100385 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
386 (map[i].br_startblock != HOLESTARTBLOCK));
387 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000388 dblkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
Darrick J. Wongad017f62017-06-16 11:00:14 -0700389 error = xfs_trans_read_buf(mp, args->trans,
390 mp->m_ddev_targp,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000391 dblkno, dblkcnt, 0, &bp,
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100392 &xfs_attr3_rmt_buf_ops);
Dave Chinner95920cd2013-04-03 16:11:27 +1100393 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100394 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100395
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000396 error = xfs_attr_rmtval_copyout(mp, bp, args->dp->i_ino,
397 &offset, &valuelen,
398 &dst);
Darrick J. Wongad017f62017-06-16 11:00:14 -0700399 xfs_trans_brelse(args->trans, bp);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000400 if (error)
401 return error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100402
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000403 /* roll attribute extent map forwards */
Dave Chinner95920cd2013-04-03 16:11:27 +1100404 lblkno += map[i].br_blockcount;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000405 blkcnt -= map[i].br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100406 }
407 }
408 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100409 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100410}
411
412/*
413 * Write the value associated with an attribute into the out-of-line buffer
414 * that we have defined for it.
415 */
416int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100417xfs_attr_rmtval_set(
418 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100419{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100420 struct xfs_inode *dp = args->dp;
421 struct xfs_mount *mp = dp->i_mount;
422 struct xfs_bmbt_irec map;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100423 xfs_dablk_t lblkno;
424 xfs_fileoff_t lfileoff = 0;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -0700425 uint8_t *src = args->value;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100426 int blkcnt;
427 int valuelen;
428 int nmap;
429 int error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100430 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100431
432 trace_xfs_attr_rmtval_set(args);
433
Dave Chinner95920cd2013-04-03 16:11:27 +1100434 /*
435 * Find a "hole" in the attribute address space large enough for
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100436 * us to drop the new attribute's value into. Because CRC enable
437 * attributes have headers, we can't just do a straight byte to FSB
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000438 * conversion and have to take the header space into account.
Dave Chinner95920cd2013-04-03 16:11:27 +1100439 */
Dave Chinner8275cdd2014-05-06 07:37:31 +1000440 blkcnt = xfs_attr3_rmt_blocks(mp, args->rmtvaluelen);
Dave Chinner95920cd2013-04-03 16:11:27 +1100441 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
442 XFS_ATTR_FORK);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100443 if (error)
444 return error;
445
Dave Chinner95920cd2013-04-03 16:11:27 +1100446 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
447 args->rmtblkcnt = blkcnt;
448
449 /*
450 * Roll through the "value", allocating blocks on disk as required.
451 */
452 while (blkcnt > 0) {
453 /*
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 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000465 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner95920cd2013-04-03 16:11:27 +1100466 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,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000469 args->total, &map, &nmap, args->dfops);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100470 if (!error)
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000471 error = xfs_defer_finish(&args->trans, args->dfops, dp);
Dave Chinner95920cd2013-04-03 16:11:27 +1100472 if (error) {
Dave Chinner95920cd2013-04-03 16:11:27 +1100473 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000474 xfs_defer_cancel(args->dfops);
Eric Sandeend99831f2014-06-22 15:03:54 +1000475 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100476 }
477
Dave Chinner95920cd2013-04-03 16:11:27 +1100478 ASSERT(nmap == 1);
479 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
480 (map.br_startblock != HOLESTARTBLOCK));
481 lblkno += map.br_blockcount;
482 blkcnt -= map.br_blockcount;
483
484 /*
485 * Start the next trans in the chain.
486 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700487 error = xfs_trans_roll_inode(&args->trans, dp);
Dave Chinner95920cd2013-04-03 16:11:27 +1100488 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000489 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100490 }
491
492 /*
493 * Roll through the "value", copying the attribute value to the
494 * already-allocated blocks. Blocks are written synchronously
495 * so that we can know they are all on disk before we turn off
496 * the INCOMPLETE flag.
497 */
498 lblkno = args->rmtblkno;
Dave Chinner26f71442013-05-21 18:02:03 +1000499 blkcnt = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000500 valuelen = args->rmtvaluelen;
Dave Chinner95920cd2013-04-03 16:11:27 +1100501 while (valuelen > 0) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000502 struct xfs_buf *bp;
503 xfs_daddr_t dblkno;
504 int dblkcnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100505
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000506 ASSERT(blkcnt > 0);
507
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000508 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner95920cd2013-04-03 16:11:27 +1100509 nmap = 1;
510 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
Dave Chinner26f71442013-05-21 18:02:03 +1000511 blkcnt, &map, &nmap,
Dave Chinner95920cd2013-04-03 16:11:27 +1100512 XFS_BMAPI_ATTRFORK);
513 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000514 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100515 ASSERT(nmap == 1);
516 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
517 (map.br_startblock != HOLESTARTBLOCK));
518
519 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
Dave Chinner26f71442013-05-21 18:02:03 +1000520 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100521
Dave Chinner26f71442013-05-21 18:02:03 +1000522 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0);
Dave Chinner95920cd2013-04-03 16:11:27 +1100523 if (!bp)
Dave Chinner24513372014-06-25 14:58:08 +1000524 return -ENOMEM;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100525 bp->b_ops = &xfs_attr3_rmt_buf_ops;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100526
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000527 xfs_attr_rmtval_copyin(mp, bp, args->dp->i_ino, &offset,
528 &valuelen, &src);
Dave Chinner95920cd2013-04-03 16:11:27 +1100529
530 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
531 xfs_buf_relse(bp);
532 if (error)
533 return error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100534
Dave Chinner95920cd2013-04-03 16:11:27 +1100535
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000536 /* roll attribute extent map forwards */
Dave Chinner95920cd2013-04-03 16:11:27 +1100537 lblkno += map.br_blockcount;
Dave Chinner26f71442013-05-21 18:02:03 +1000538 blkcnt -= map.br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100539 }
540 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100541 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100542}
543
544/*
545 * Remove the value associated with an attribute by deleting the
546 * out-of-line buffer that it is stored on.
547 */
548int
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000549xfs_attr_rmtval_remove(
550 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100551{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000552 struct xfs_mount *mp = args->dp->i_mount;
553 xfs_dablk_t lblkno;
554 int blkcnt;
555 int error;
556 int done;
Dave Chinner95920cd2013-04-03 16:11:27 +1100557
558 trace_xfs_attr_rmtval_remove(args);
559
Dave Chinner95920cd2013-04-03 16:11:27 +1100560 /*
Dave Chinner58a72282013-05-21 18:02:04 +1000561 * Roll through the "value", invalidating the attribute value's blocks.
Dave Chinner95920cd2013-04-03 16:11:27 +1100562 */
563 lblkno = args->rmtblkno;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000564 blkcnt = args->rmtblkcnt;
565 while (blkcnt > 0) {
566 struct xfs_bmbt_irec map;
567 struct xfs_buf *bp;
568 xfs_daddr_t dblkno;
569 int dblkcnt;
570 int nmap;
Dave Chinner58a72282013-05-21 18:02:04 +1000571
Dave Chinner95920cd2013-04-03 16:11:27 +1100572 /*
573 * Try to remember where we decided to put the value.
574 */
575 nmap = 1;
576 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
Dave Chinner58a72282013-05-21 18:02:04 +1000577 blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
Dave Chinner95920cd2013-04-03 16:11:27 +1100578 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000579 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100580 ASSERT(nmap == 1);
581 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
582 (map.br_startblock != HOLESTARTBLOCK));
583
584 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
Dave Chinner58a72282013-05-21 18:02:04 +1000585 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100586
587 /*
588 * If the "remote" value is in the cache, remove it.
589 */
Dave Chinner58a72282013-05-21 18:02:04 +1000590 bp = xfs_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK);
Dave Chinner95920cd2013-04-03 16:11:27 +1100591 if (bp) {
592 xfs_buf_stale(bp);
593 xfs_buf_relse(bp);
594 bp = NULL;
595 }
596
Dave Chinner95920cd2013-04-03 16:11:27 +1100597 lblkno += map.br_blockcount;
Dave Chinner58a72282013-05-21 18:02:04 +1000598 blkcnt -= map.br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100599 }
600
601 /*
602 * Keep de-allocating extents until the remote-value region is gone.
603 */
604 lblkno = args->rmtblkno;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000605 blkcnt = args->rmtblkcnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100606 done = 0;
607 while (!done) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000608 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner95920cd2013-04-03 16:11:27 +1100609 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
Dave Chinnerab7bb612015-07-29 11:51:01 +1000610 XFS_BMAPI_ATTRFORK, 1, args->firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000611 args->dfops, &done);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100612 if (!error)
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000613 error = xfs_defer_finish(&args->trans, args->dfops,
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100614 args->dp);
Dave Chinner95920cd2013-04-03 16:11:27 +1100615 if (error) {
Dave Chinner95920cd2013-04-03 16:11:27 +1100616 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000617 xfs_defer_cancel(args->dfops);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100618 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100619 }
620
621 /*
Dave Chinner95920cd2013-04-03 16:11:27 +1100622 * Close out trans and start the next one in the chain.
623 */
Christoph Hellwig411350d2017-08-28 10:21:03 -0700624 error = xfs_trans_roll_inode(&args->trans, args->dp);
Dave Chinner95920cd2013-04-03 16:11:27 +1100625 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000626 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100627 }
Eric Sandeend99831f2014-06-22 15:03:54 +1000628 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100629}