blob: d52f525f5b2dffe74be16ccf1bae2494a02787d7 [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,
Dave Chinner836a94a2013-08-12 20:49:44 +1000256 __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,
Dave Chinner836a94a2013-08-12 20:49:44 +1000304 __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;
Dave Chinner836a94a2013-08-12 20:49:44 +1000358 __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);
Dave Chinner95920cd2013-04-03 16:11:27 +1100389 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000390 dblkno, dblkcnt, 0, &bp,
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100391 &xfs_attr3_rmt_buf_ops);
Dave Chinner95920cd2013-04-03 16:11:27 +1100392 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100393 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100394
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000395 error = xfs_attr_rmtval_copyout(mp, bp, args->dp->i_ino,
396 &offset, &valuelen,
397 &dst);
Dave Chinner95920cd2013-04-03 16:11:27 +1100398 xfs_buf_relse(bp);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000399 if (error)
400 return error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100401
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000402 /* roll attribute extent map forwards */
Dave Chinner95920cd2013-04-03 16:11:27 +1100403 lblkno += map[i].br_blockcount;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000404 blkcnt -= map[i].br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100405 }
406 }
407 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100408 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100409}
410
411/*
412 * Write the value associated with an attribute into the out-of-line buffer
413 * that we have defined for it.
414 */
415int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100416xfs_attr_rmtval_set(
417 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100418{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100419 struct xfs_inode *dp = args->dp;
420 struct xfs_mount *mp = dp->i_mount;
421 struct xfs_bmbt_irec map;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100422 xfs_dablk_t lblkno;
423 xfs_fileoff_t lfileoff = 0;
Dave Chinner836a94a2013-08-12 20:49:44 +1000424 __uint8_t *src = args->value;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100425 int blkcnt;
426 int valuelen;
427 int nmap;
428 int error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100429 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100430
431 trace_xfs_attr_rmtval_set(args);
432
Dave Chinner95920cd2013-04-03 16:11:27 +1100433 /*
434 * Find a "hole" in the attribute address space large enough for
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100435 * us to drop the new attribute's value into. Because CRC enable
436 * attributes have headers, we can't just do a straight byte to FSB
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000437 * conversion and have to take the header space into account.
Dave Chinner95920cd2013-04-03 16:11:27 +1100438 */
Dave Chinner8275cdd2014-05-06 07:37:31 +1000439 blkcnt = xfs_attr3_rmt_blocks(mp, args->rmtvaluelen);
Dave Chinner95920cd2013-04-03 16:11:27 +1100440 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
441 XFS_ATTR_FORK);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100442 if (error)
443 return error;
444
Dave Chinner95920cd2013-04-03 16:11:27 +1100445 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
446 args->rmtblkcnt = blkcnt;
447
448 /*
449 * Roll through the "value", allocating blocks on disk as required.
450 */
451 while (blkcnt > 0) {
452 /*
453 * Allocate a single extent, up to the size of the value.
Dave Chinnerdf150ed12015-07-29 11:48:02 +1000454 *
455 * Note that we have to consider this a data allocation as we
456 * write the remote attribute without logging the contents.
457 * Hence we must ensure that we aren't using blocks that are on
458 * the busy list so that we don't overwrite blocks which have
459 * recently been freed but their transactions are not yet
460 * committed to disk. If we overwrite the contents of a busy
461 * extent and then crash then the block may not contain the
462 * correct metadata after log recovery occurs.
Dave Chinner95920cd2013-04-03 16:11:27 +1100463 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000464 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner95920cd2013-04-03 16:11:27 +1100465 nmap = 1;
466 error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
Dave Chinnerdf150ed12015-07-29 11:48:02 +1000467 blkcnt, XFS_BMAPI_ATTRFORK, args->firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000468 args->total, &map, &nmap, args->dfops);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100469 if (!error)
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000470 error = xfs_defer_finish(&args->trans, args->dfops, dp);
Dave Chinner95920cd2013-04-03 16:11:27 +1100471 if (error) {
Dave Chinner95920cd2013-04-03 16:11:27 +1100472 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000473 xfs_defer_cancel(args->dfops);
Eric Sandeend99831f2014-06-22 15:03:54 +1000474 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100475 }
476
Dave Chinner95920cd2013-04-03 16:11:27 +1100477 ASSERT(nmap == 1);
478 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
479 (map.br_startblock != HOLESTARTBLOCK));
480 lblkno += map.br_blockcount;
481 blkcnt -= map.br_blockcount;
482
483 /*
484 * Start the next trans in the chain.
485 */
486 error = xfs_trans_roll(&args->trans, dp);
487 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000488 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100489 }
490
491 /*
492 * Roll through the "value", copying the attribute value to the
493 * already-allocated blocks. Blocks are written synchronously
494 * so that we can know they are all on disk before we turn off
495 * the INCOMPLETE flag.
496 */
497 lblkno = args->rmtblkno;
Dave Chinner26f71442013-05-21 18:02:03 +1000498 blkcnt = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000499 valuelen = args->rmtvaluelen;
Dave Chinner95920cd2013-04-03 16:11:27 +1100500 while (valuelen > 0) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000501 struct xfs_buf *bp;
502 xfs_daddr_t dblkno;
503 int dblkcnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100504
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000505 ASSERT(blkcnt > 0);
506
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000507 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner95920cd2013-04-03 16:11:27 +1100508 nmap = 1;
509 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
Dave Chinner26f71442013-05-21 18:02:03 +1000510 blkcnt, &map, &nmap,
Dave Chinner95920cd2013-04-03 16:11:27 +1100511 XFS_BMAPI_ATTRFORK);
512 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000513 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100514 ASSERT(nmap == 1);
515 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
516 (map.br_startblock != HOLESTARTBLOCK));
517
518 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
Dave Chinner26f71442013-05-21 18:02:03 +1000519 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100520
Dave Chinner26f71442013-05-21 18:02:03 +1000521 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0);
Dave Chinner95920cd2013-04-03 16:11:27 +1100522 if (!bp)
Dave Chinner24513372014-06-25 14:58:08 +1000523 return -ENOMEM;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100524 bp->b_ops = &xfs_attr3_rmt_buf_ops;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100525
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000526 xfs_attr_rmtval_copyin(mp, bp, args->dp->i_ino, &offset,
527 &valuelen, &src);
Dave Chinner95920cd2013-04-03 16:11:27 +1100528
529 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
530 xfs_buf_relse(bp);
531 if (error)
532 return error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100533
Dave Chinner95920cd2013-04-03 16:11:27 +1100534
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000535 /* roll attribute extent map forwards */
Dave Chinner95920cd2013-04-03 16:11:27 +1100536 lblkno += map.br_blockcount;
Dave Chinner26f71442013-05-21 18:02:03 +1000537 blkcnt -= map.br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100538 }
539 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100540 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100541}
542
543/*
544 * Remove the value associated with an attribute by deleting the
545 * out-of-line buffer that it is stored on.
546 */
547int
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000548xfs_attr_rmtval_remove(
549 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100550{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000551 struct xfs_mount *mp = args->dp->i_mount;
552 xfs_dablk_t lblkno;
553 int blkcnt;
554 int error;
555 int done;
Dave Chinner95920cd2013-04-03 16:11:27 +1100556
557 trace_xfs_attr_rmtval_remove(args);
558
Dave Chinner95920cd2013-04-03 16:11:27 +1100559 /*
Dave Chinner58a72282013-05-21 18:02:04 +1000560 * Roll through the "value", invalidating the attribute value's blocks.
Dave Chinner95920cd2013-04-03 16:11:27 +1100561 */
562 lblkno = args->rmtblkno;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000563 blkcnt = args->rmtblkcnt;
564 while (blkcnt > 0) {
565 struct xfs_bmbt_irec map;
566 struct xfs_buf *bp;
567 xfs_daddr_t dblkno;
568 int dblkcnt;
569 int nmap;
Dave Chinner58a72282013-05-21 18:02:04 +1000570
Dave Chinner95920cd2013-04-03 16:11:27 +1100571 /*
572 * Try to remember where we decided to put the value.
573 */
574 nmap = 1;
575 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
Dave Chinner58a72282013-05-21 18:02:04 +1000576 blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
Dave Chinner95920cd2013-04-03 16:11:27 +1100577 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000578 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100579 ASSERT(nmap == 1);
580 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
581 (map.br_startblock != HOLESTARTBLOCK));
582
583 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
Dave Chinner58a72282013-05-21 18:02:04 +1000584 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100585
586 /*
587 * If the "remote" value is in the cache, remove it.
588 */
Dave Chinner58a72282013-05-21 18:02:04 +1000589 bp = xfs_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK);
Dave Chinner95920cd2013-04-03 16:11:27 +1100590 if (bp) {
591 xfs_buf_stale(bp);
592 xfs_buf_relse(bp);
593 bp = NULL;
594 }
595
Dave Chinner95920cd2013-04-03 16:11:27 +1100596 lblkno += map.br_blockcount;
Dave Chinner58a72282013-05-21 18:02:04 +1000597 blkcnt -= map.br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100598 }
599
600 /*
601 * Keep de-allocating extents until the remote-value region is gone.
602 */
603 lblkno = args->rmtblkno;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000604 blkcnt = args->rmtblkcnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100605 done = 0;
606 while (!done) {
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000607 xfs_defer_init(args->dfops, args->firstblock);
Dave Chinner95920cd2013-04-03 16:11:27 +1100608 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
Dave Chinnerab7bb612015-07-29 11:51:01 +1000609 XFS_BMAPI_ATTRFORK, 1, args->firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000610 args->dfops, &done);
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100611 if (!error)
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000612 error = xfs_defer_finish(&args->trans, args->dfops,
Eric Sandeenf6106ef2016-01-11 11:34:01 +1100613 args->dp);
Dave Chinner95920cd2013-04-03 16:11:27 +1100614 if (error) {
Dave Chinner95920cd2013-04-03 16:11:27 +1100615 args->trans = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000616 xfs_defer_cancel(args->dfops);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100617 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100618 }
619
620 /*
Dave Chinner95920cd2013-04-03 16:11:27 +1100621 * Close out trans and start the next one in the chain.
622 */
623 error = xfs_trans_roll(&args->trans, args->dp);
624 if (error)
Eric Sandeend99831f2014-06-22 15:03:54 +1000625 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100626 }
Eric Sandeend99831f2014-06-22 15:03:54 +1000627 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100628}