blob: 41f206c82aacdab6ec367b62e4b38a37644e92e1 [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"
27#include "xfs_ag.h"
28#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110029#include "xfs_da_format.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110030#include "xfs_da_btree.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110031#include "xfs_inode.h"
32#include "xfs_alloc.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110034#include "xfs_inode_item.h"
35#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100036#include "xfs_bmap_util.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110037#include "xfs_attr.h"
38#include "xfs_attr_leaf.h"
39#include "xfs_attr_remote.h"
40#include "xfs_trans_space.h"
41#include "xfs_trace.h"
Dave Chinnerd2e448d2013-04-03 16:11:28 +110042#include "xfs_cksum.h"
43#include "xfs_buf_item.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110044#include "xfs_error.h"
Dave Chinner95920cd2013-04-03 16:11:27 +110045
46#define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
47
48/*
Dave Chinnerd2e448d2013-04-03 16:11:28 +110049 * Each contiguous block has a header, so it is not just a simple attribute
50 * length to FSB conversion.
51 */
Dave Chinner7bc0dc22013-05-21 18:02:08 +100052int
Dave Chinnerd2e448d2013-04-03 16:11:28 +110053xfs_attr3_rmt_blocks(
54 struct xfs_mount *mp,
55 int attrlen)
56{
Dave Chinner551b3822013-05-21 18:02:02 +100057 if (xfs_sb_version_hascrc(&mp->m_sb)) {
58 int buflen = XFS_ATTR3_RMT_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
59 return (attrlen + buflen - 1) / buflen;
60 }
61 return XFS_B_TO_FSB(mp, attrlen);
Dave Chinnerd2e448d2013-04-03 16:11:28 +110062}
63
Dave Chinner7bc0dc22013-05-21 18:02:08 +100064/*
65 * Checking of the remote attribute header is split into two parts. The verifier
66 * does CRC, location and bounds checking, the unpacking function checks the
67 * attribute parameters and owner.
68 */
69static bool
70xfs_attr3_rmt_hdr_ok(
Dave Chinner7bc0dc22013-05-21 18:02:08 +100071 void *ptr,
72 xfs_ino_t ino,
73 uint32_t offset,
74 uint32_t size,
75 xfs_daddr_t bno)
76{
77 struct xfs_attr3_rmt_hdr *rmt = ptr;
78
79 if (bno != be64_to_cpu(rmt->rm_blkno))
80 return false;
81 if (offset != be32_to_cpu(rmt->rm_offset))
82 return false;
83 if (size != be32_to_cpu(rmt->rm_bytes))
84 return false;
85 if (ino != be64_to_cpu(rmt->rm_owner))
86 return false;
87
88 /* ok */
89 return true;
90}
91
Dave Chinnerd2e448d2013-04-03 16:11:28 +110092static bool
93xfs_attr3_rmt_verify(
Dave Chinner7bc0dc22013-05-21 18:02:08 +100094 struct xfs_mount *mp,
95 void *ptr,
96 int fsbsize,
97 xfs_daddr_t bno)
Dave Chinnerd2e448d2013-04-03 16:11:28 +110098{
Dave Chinner7bc0dc22013-05-21 18:02:08 +100099 struct xfs_attr3_rmt_hdr *rmt = ptr;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100100
101 if (!xfs_sb_version_hascrc(&mp->m_sb))
102 return false;
103 if (rmt->rm_magic != cpu_to_be32(XFS_ATTR3_RMT_MAGIC))
104 return false;
105 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_uuid))
106 return false;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000107 if (be64_to_cpu(rmt->rm_blkno) != bno)
108 return false;
109 if (be32_to_cpu(rmt->rm_bytes) > fsbsize - sizeof(*rmt))
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100110 return false;
111 if (be32_to_cpu(rmt->rm_offset) +
Jie Liubba719b2014-01-01 19:28:03 +0800112 be32_to_cpu(rmt->rm_bytes) > XATTR_SIZE_MAX)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100113 return false;
114 if (rmt->rm_owner == 0)
115 return false;
116
117 return true;
118}
119
120static void
121xfs_attr3_rmt_read_verify(
122 struct xfs_buf *bp)
123{
124 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000125 char *ptr;
126 int len;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000127 xfs_daddr_t bno;
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);
136 ASSERT(len >= XFS_LBSIZE(mp));
137
138 while (len > 0) {
139 if (!xfs_verify_cksum(ptr, XFS_LBSIZE(mp),
140 XFS_ATTR3_RMT_CRC_OFF)) {
Eric Sandeence5028c2014-02-27 15:23:10 +1100141 xfs_buf_ioerror(bp, EFSBADCRC);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000142 break;
143 }
144 if (!xfs_attr3_rmt_verify(mp, ptr, XFS_LBSIZE(mp), bno)) {
Eric Sandeence5028c2014-02-27 15:23:10 +1100145 xfs_buf_ioerror(bp, EFSCORRUPTED);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000146 break;
147 }
148 len -= XFS_LBSIZE(mp);
149 ptr += XFS_LBSIZE(mp);
150 bno += mp->m_bsize;
151 }
152
Eric Sandeence5028c2014-02-27 15:23:10 +1100153 if (bp->b_error)
154 xfs_verifier_error(bp);
155 else
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000156 ASSERT(len == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100157}
158
159static void
160xfs_attr3_rmt_write_verify(
161 struct xfs_buf *bp)
162{
163 struct xfs_mount *mp = bp->b_target->bt_mount;
164 struct xfs_buf_log_item *bip = bp->b_fspriv;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000165 char *ptr;
166 int len;
167 xfs_daddr_t bno;
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);
176 ASSERT(len >= XFS_LBSIZE(mp));
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100177
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000178 while (len > 0) {
179 if (!xfs_attr3_rmt_verify(mp, ptr, XFS_LBSIZE(mp), bno)) {
Dave Chinner7bc0dc22013-05-21 18:02: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 }
190 xfs_update_cksum(ptr, XFS_LBSIZE(mp), XFS_ATTR3_RMT_CRC_OFF);
191
192 len -= XFS_LBSIZE(mp);
193 ptr += XFS_LBSIZE(mp);
194 bno += mp->m_bsize;
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 Chinnerd2e448d2013-04-03 16:11:28 +1100243
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000244 ASSERT(len >= XFS_LBSIZE(mp));
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100245
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000246 while (len > 0 && *valuelen > 0) {
247 int hdr_size = 0;
248 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, XFS_LBSIZE(mp));
249
Dave Chinnerc5c249b2013-08-12 20:49:43 +1000250 byte_cnt = min(*valuelen, byte_cnt);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000251
252 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Eric Sandeen6d0081a2014-04-14 18:58:29 +1000253 if (!xfs_attr3_rmt_hdr_ok(src, ino, *offset,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000254 byte_cnt, bno)) {
255 xfs_alert(mp,
256"remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)",
257 bno, *offset, byte_cnt, ino);
258 return EFSCORRUPTED;
259 }
260 hdr_size = sizeof(struct xfs_attr3_rmt_hdr);
261 }
262
263 memcpy(*dst, src + hdr_size, byte_cnt);
264
265 /* roll buffer forwards */
266 len -= XFS_LBSIZE(mp);
267 src += XFS_LBSIZE(mp);
268 bno += mp->m_bsize;
269
270 /* roll attribute data forwards */
271 *valuelen -= byte_cnt;
272 *dst += byte_cnt;
273 *offset += byte_cnt;
274 }
275 return 0;
276}
277
278STATIC void
279xfs_attr_rmtval_copyin(
280 struct xfs_mount *mp,
281 struct xfs_buf *bp,
282 xfs_ino_t ino,
283 int *offset,
284 int *valuelen,
Dave Chinner836a94a2013-08-12 20:49:44 +1000285 __uint8_t **src)
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000286{
287 char *dst = bp->b_addr;
288 xfs_daddr_t bno = bp->b_bn;
289 int len = BBTOB(bp->b_length);
290
291 ASSERT(len >= XFS_LBSIZE(mp));
292
293 while (len > 0 && *valuelen > 0) {
294 int hdr_size;
295 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, XFS_LBSIZE(mp));
296
297 byte_cnt = min(*valuelen, byte_cnt);
298 hdr_size = xfs_attr3_rmt_hdr_set(mp, dst, ino, *offset,
299 byte_cnt, bno);
300
301 memcpy(dst + hdr_size, *src, byte_cnt);
302
303 /*
304 * If this is the last block, zero the remainder of it.
305 * Check that we are actually the last block, too.
306 */
307 if (byte_cnt + hdr_size < XFS_LBSIZE(mp)) {
308 ASSERT(*valuelen - byte_cnt == 0);
309 ASSERT(len == XFS_LBSIZE(mp));
310 memset(dst + hdr_size + byte_cnt, 0,
311 XFS_LBSIZE(mp) - hdr_size - byte_cnt);
312 }
313
314 /* roll buffer forwards */
315 len -= XFS_LBSIZE(mp);
316 dst += XFS_LBSIZE(mp);
317 bno += mp->m_bsize;
318
319 /* roll attribute data forwards */
320 *valuelen -= byte_cnt;
321 *src += byte_cnt;
322 *offset += byte_cnt;
323 }
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100324}
325
326/*
Dave Chinner95920cd2013-04-03 16:11:27 +1100327 * Read the value associated with an attribute from the out-of-line buffer
328 * that we stored it in.
329 */
330int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100331xfs_attr_rmtval_get(
332 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100333{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100334 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
335 struct xfs_mount *mp = args->dp->i_mount;
336 struct xfs_buf *bp;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100337 xfs_dablk_t lblkno = args->rmtblkno;
Dave Chinner836a94a2013-08-12 20:49:44 +1000338 __uint8_t *dst = args->value;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100339 int valuelen = args->valuelen;
340 int nmap;
341 int error;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000342 int blkcnt = args->rmtblkcnt;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100343 int i;
344 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100345
346 trace_xfs_attr_rmtval_get(args);
347
348 ASSERT(!(args->flags & ATTR_KERNOVAL));
349
Dave Chinner95920cd2013-04-03 16:11:27 +1100350 while (valuelen > 0) {
351 nmap = ATTR_RMTVALUE_MAPSIZE;
352 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
Dave Chinner551b3822013-05-21 18:02:02 +1000353 blkcnt, map, &nmap,
Dave Chinner95920cd2013-04-03 16:11:27 +1100354 XFS_BMAPI_ATTRFORK);
355 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100356 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100357 ASSERT(nmap >= 1);
358
359 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000360 xfs_daddr_t dblkno;
361 int dblkcnt;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100362
Dave Chinner95920cd2013-04-03 16:11:27 +1100363 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
364 (map[i].br_startblock != HOLESTARTBLOCK));
365 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000366 dblkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100367 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000368 dblkno, dblkcnt, 0, &bp,
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100369 &xfs_attr3_rmt_buf_ops);
Dave Chinner95920cd2013-04-03 16:11:27 +1100370 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100371 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100372
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000373 error = xfs_attr_rmtval_copyout(mp, bp, args->dp->i_ino,
374 &offset, &valuelen,
375 &dst);
Dave Chinner95920cd2013-04-03 16:11:27 +1100376 xfs_buf_relse(bp);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000377 if (error)
378 return error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100379
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000380 /* roll attribute extent map forwards */
Dave Chinner95920cd2013-04-03 16:11:27 +1100381 lblkno += map[i].br_blockcount;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000382 blkcnt -= map[i].br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100383 }
384 }
385 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100386 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100387}
388
389/*
390 * Write the value associated with an attribute into the out-of-line buffer
391 * that we have defined for it.
392 */
393int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100394xfs_attr_rmtval_set(
395 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100396{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100397 struct xfs_inode *dp = args->dp;
398 struct xfs_mount *mp = dp->i_mount;
399 struct xfs_bmbt_irec map;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100400 xfs_dablk_t lblkno;
401 xfs_fileoff_t lfileoff = 0;
Dave Chinner836a94a2013-08-12 20:49:44 +1000402 __uint8_t *src = args->value;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100403 int blkcnt;
404 int valuelen;
405 int nmap;
406 int error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100407 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100408
409 trace_xfs_attr_rmtval_set(args);
410
Dave Chinner95920cd2013-04-03 16:11:27 +1100411 /*
412 * Find a "hole" in the attribute address space large enough for
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100413 * us to drop the new attribute's value into. Because CRC enable
414 * attributes have headers, we can't just do a straight byte to FSB
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000415 * conversion and have to take the header space into account.
Dave Chinner95920cd2013-04-03 16:11:27 +1100416 */
Dave Chinner26f71442013-05-21 18:02:03 +1000417 blkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
Dave Chinner95920cd2013-04-03 16:11:27 +1100418 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
419 XFS_ATTR_FORK);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100420 if (error)
421 return error;
422
Dave Chinner95920cd2013-04-03 16:11:27 +1100423 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
424 args->rmtblkcnt = blkcnt;
425
426 /*
427 * Roll through the "value", allocating blocks on disk as required.
428 */
429 while (blkcnt > 0) {
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100430 int committed;
431
Dave Chinner95920cd2013-04-03 16:11:27 +1100432 /*
433 * Allocate a single extent, up to the size of the value.
434 */
435 xfs_bmap_init(args->flist, args->firstblock);
436 nmap = 1;
437 error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
438 blkcnt,
439 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
440 args->firstblock, args->total, &map, &nmap,
441 args->flist);
442 if (!error) {
443 error = xfs_bmap_finish(&args->trans, args->flist,
444 &committed);
445 }
446 if (error) {
447 ASSERT(committed);
448 args->trans = NULL;
449 xfs_bmap_cancel(args->flist);
450 return(error);
451 }
452
453 /*
454 * bmap_finish() may have committed the last trans and started
455 * a new one. We need the inode to be in all transactions.
456 */
457 if (committed)
458 xfs_trans_ijoin(args->trans, dp, 0);
459
460 ASSERT(nmap == 1);
461 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
462 (map.br_startblock != HOLESTARTBLOCK));
463 lblkno += map.br_blockcount;
464 blkcnt -= map.br_blockcount;
465
466 /*
467 * Start the next trans in the chain.
468 */
469 error = xfs_trans_roll(&args->trans, dp);
470 if (error)
471 return (error);
472 }
473
474 /*
475 * Roll through the "value", copying the attribute value to the
476 * already-allocated blocks. Blocks are written synchronously
477 * so that we can know they are all on disk before we turn off
478 * the INCOMPLETE flag.
479 */
480 lblkno = args->rmtblkno;
Dave Chinner26f71442013-05-21 18:02:03 +1000481 blkcnt = args->rmtblkcnt;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000482 valuelen = args->valuelen;
Dave Chinner95920cd2013-04-03 16:11:27 +1100483 while (valuelen > 0) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000484 struct xfs_buf *bp;
485 xfs_daddr_t dblkno;
486 int dblkcnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100487
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000488 ASSERT(blkcnt > 0);
489
Dave Chinner95920cd2013-04-03 16:11:27 +1100490 xfs_bmap_init(args->flist, args->firstblock);
491 nmap = 1;
492 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
Dave Chinner26f71442013-05-21 18:02:03 +1000493 blkcnt, &map, &nmap,
Dave Chinner95920cd2013-04-03 16:11:27 +1100494 XFS_BMAPI_ATTRFORK);
495 if (error)
496 return(error);
497 ASSERT(nmap == 1);
498 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
499 (map.br_startblock != HOLESTARTBLOCK));
500
501 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
Dave Chinner26f71442013-05-21 18:02:03 +1000502 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100503
Dave Chinner26f71442013-05-21 18:02:03 +1000504 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0);
Dave Chinner95920cd2013-04-03 16:11:27 +1100505 if (!bp)
506 return ENOMEM;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100507 bp->b_ops = &xfs_attr3_rmt_buf_ops;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100508
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000509 xfs_attr_rmtval_copyin(mp, bp, args->dp->i_ino, &offset,
510 &valuelen, &src);
Dave Chinner95920cd2013-04-03 16:11:27 +1100511
512 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
513 xfs_buf_relse(bp);
514 if (error)
515 return error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100516
Dave Chinner95920cd2013-04-03 16:11:27 +1100517
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000518 /* roll attribute extent map forwards */
Dave Chinner95920cd2013-04-03 16:11:27 +1100519 lblkno += map.br_blockcount;
Dave Chinner26f71442013-05-21 18:02:03 +1000520 blkcnt -= map.br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100521 }
522 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100523 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100524}
525
526/*
527 * Remove the value associated with an attribute by deleting the
528 * out-of-line buffer that it is stored on.
529 */
530int
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000531xfs_attr_rmtval_remove(
532 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100533{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000534 struct xfs_mount *mp = args->dp->i_mount;
535 xfs_dablk_t lblkno;
536 int blkcnt;
537 int error;
538 int done;
Dave Chinner95920cd2013-04-03 16:11:27 +1100539
540 trace_xfs_attr_rmtval_remove(args);
541
Dave Chinner95920cd2013-04-03 16:11:27 +1100542 /*
Dave Chinner58a72282013-05-21 18:02:04 +1000543 * Roll through the "value", invalidating the attribute value's blocks.
Dave Chinner95920cd2013-04-03 16:11:27 +1100544 */
545 lblkno = args->rmtblkno;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000546 blkcnt = args->rmtblkcnt;
547 while (blkcnt > 0) {
548 struct xfs_bmbt_irec map;
549 struct xfs_buf *bp;
550 xfs_daddr_t dblkno;
551 int dblkcnt;
552 int nmap;
Dave Chinner58a72282013-05-21 18:02:04 +1000553
Dave Chinner95920cd2013-04-03 16:11:27 +1100554 /*
555 * Try to remember where we decided to put the value.
556 */
557 nmap = 1;
558 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
Dave Chinner58a72282013-05-21 18:02:04 +1000559 blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
Dave Chinner95920cd2013-04-03 16:11:27 +1100560 if (error)
561 return(error);
562 ASSERT(nmap == 1);
563 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
564 (map.br_startblock != HOLESTARTBLOCK));
565
566 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
Dave Chinner58a72282013-05-21 18:02:04 +1000567 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100568
569 /*
570 * If the "remote" value is in the cache, remove it.
571 */
Dave Chinner58a72282013-05-21 18:02:04 +1000572 bp = xfs_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK);
Dave Chinner95920cd2013-04-03 16:11:27 +1100573 if (bp) {
574 xfs_buf_stale(bp);
575 xfs_buf_relse(bp);
576 bp = NULL;
577 }
578
Dave Chinner95920cd2013-04-03 16:11:27 +1100579 lblkno += map.br_blockcount;
Dave Chinner58a72282013-05-21 18:02:04 +1000580 blkcnt -= map.br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100581 }
582
583 /*
584 * Keep de-allocating extents until the remote-value region is gone.
585 */
586 lblkno = args->rmtblkno;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000587 blkcnt = args->rmtblkcnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100588 done = 0;
589 while (!done) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000590 int committed;
591
Dave Chinner95920cd2013-04-03 16:11:27 +1100592 xfs_bmap_init(args->flist, args->firstblock);
593 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
594 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
595 1, args->firstblock, args->flist,
596 &done);
597 if (!error) {
598 error = xfs_bmap_finish(&args->trans, args->flist,
599 &committed);
600 }
601 if (error) {
602 ASSERT(committed);
603 args->trans = NULL;
604 xfs_bmap_cancel(args->flist);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100605 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100606 }
607
608 /*
609 * bmap_finish() may have committed the last trans and started
610 * a new one. We need the inode to be in all transactions.
611 */
612 if (committed)
613 xfs_trans_ijoin(args->trans, args->dp, 0);
614
615 /*
616 * Close out trans and start the next one in the chain.
617 */
618 error = xfs_trans_roll(&args->trans, args->dp);
619 if (error)
620 return (error);
621 }
622 return(0);
623}