blob: d2e6e948cec7be3013b853033b6e1a64b365a682 [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(
71 struct xfs_mount *mp,
72 void *ptr,
73 xfs_ino_t ino,
74 uint32_t offset,
75 uint32_t size,
76 xfs_daddr_t bno)
77{
78 struct xfs_attr3_rmt_hdr *rmt = ptr;
79
80 if (bno != be64_to_cpu(rmt->rm_blkno))
81 return false;
82 if (offset != be32_to_cpu(rmt->rm_offset))
83 return false;
84 if (size != be32_to_cpu(rmt->rm_bytes))
85 return false;
86 if (ino != be64_to_cpu(rmt->rm_owner))
87 return false;
88
89 /* ok */
90 return true;
91}
92
Dave Chinnerd2e448d2013-04-03 16:11:28 +110093static bool
94xfs_attr3_rmt_verify(
Dave Chinner7bc0dc22013-05-21 18:02:08 +100095 struct xfs_mount *mp,
96 void *ptr,
97 int fsbsize,
98 xfs_daddr_t bno)
Dave Chinnerd2e448d2013-04-03 16:11:28 +110099{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000100 struct xfs_attr3_rmt_hdr *rmt = ptr;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100101
102 if (!xfs_sb_version_hascrc(&mp->m_sb))
103 return false;
104 if (rmt->rm_magic != cpu_to_be32(XFS_ATTR3_RMT_MAGIC))
105 return false;
106 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_uuid))
107 return false;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000108 if (be64_to_cpu(rmt->rm_blkno) != bno)
109 return false;
110 if (be32_to_cpu(rmt->rm_bytes) > fsbsize - sizeof(*rmt))
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100111 return false;
112 if (be32_to_cpu(rmt->rm_offset) +
Jie Liubba719b2014-01-01 19:28:03 +0800113 be32_to_cpu(rmt->rm_bytes) > XATTR_SIZE_MAX)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100114 return false;
115 if (rmt->rm_owner == 0)
116 return false;
117
118 return true;
119}
120
121static void
122xfs_attr3_rmt_read_verify(
123 struct xfs_buf *bp)
124{
125 struct xfs_mount *mp = bp->b_target->bt_mount;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000126 char *ptr;
127 int len;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000128 xfs_daddr_t bno;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100129
130 /* no verification of non-crc buffers */
131 if (!xfs_sb_version_hascrc(&mp->m_sb))
132 return;
133
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000134 ptr = bp->b_addr;
135 bno = bp->b_bn;
136 len = BBTOB(bp->b_length);
137 ASSERT(len >= XFS_LBSIZE(mp));
138
139 while (len > 0) {
140 if (!xfs_verify_cksum(ptr, XFS_LBSIZE(mp),
141 XFS_ATTR3_RMT_CRC_OFF)) {
Eric Sandeence5028c2014-02-27 15:23:10 +1100142 xfs_buf_ioerror(bp, EFSBADCRC);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000143 break;
144 }
145 if (!xfs_attr3_rmt_verify(mp, ptr, XFS_LBSIZE(mp), bno)) {
Eric Sandeence5028c2014-02-27 15:23:10 +1100146 xfs_buf_ioerror(bp, EFSCORRUPTED);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000147 break;
148 }
149 len -= XFS_LBSIZE(mp);
150 ptr += XFS_LBSIZE(mp);
151 bno += mp->m_bsize;
152 }
153
Eric Sandeence5028c2014-02-27 15:23:10 +1100154 if (bp->b_error)
155 xfs_verifier_error(bp);
156 else
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000157 ASSERT(len == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100158}
159
160static void
161xfs_attr3_rmt_write_verify(
162 struct xfs_buf *bp)
163{
164 struct xfs_mount *mp = bp->b_target->bt_mount;
165 struct xfs_buf_log_item *bip = bp->b_fspriv;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000166 char *ptr;
167 int len;
168 xfs_daddr_t bno;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100169
170 /* no verification of non-crc buffers */
171 if (!xfs_sb_version_hascrc(&mp->m_sb))
172 return;
173
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000174 ptr = bp->b_addr;
175 bno = bp->b_bn;
176 len = BBTOB(bp->b_length);
177 ASSERT(len >= XFS_LBSIZE(mp));
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100178
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000179 while (len > 0) {
180 if (!xfs_attr3_rmt_verify(mp, ptr, XFS_LBSIZE(mp), bno)) {
Dave Chinner7bc0dc22013-05-21 18:02: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 }
185 if (bip) {
186 struct xfs_attr3_rmt_hdr *rmt;
187
188 rmt = (struct xfs_attr3_rmt_hdr *)ptr;
189 rmt->rm_lsn = cpu_to_be64(bip->bli_item.li_lsn);
190 }
191 xfs_update_cksum(ptr, XFS_LBSIZE(mp), XFS_ATTR3_RMT_CRC_OFF);
192
193 len -= XFS_LBSIZE(mp);
194 ptr += XFS_LBSIZE(mp);
195 bno += mp->m_bsize;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100196 }
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000197 ASSERT(len == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100198}
199
200const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
201 .verify_read = xfs_attr3_rmt_read_verify,
202 .verify_write = xfs_attr3_rmt_write_verify,
203};
204
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000205STATIC int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100206xfs_attr3_rmt_hdr_set(
207 struct xfs_mount *mp,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000208 void *ptr,
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100209 xfs_ino_t ino,
210 uint32_t offset,
211 uint32_t size,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000212 xfs_daddr_t bno)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100213{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000214 struct xfs_attr3_rmt_hdr *rmt = ptr;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100215
216 if (!xfs_sb_version_hascrc(&mp->m_sb))
217 return 0;
218
219 rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC);
220 rmt->rm_offset = cpu_to_be32(offset);
221 rmt->rm_bytes = cpu_to_be32(size);
222 uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_uuid);
223 rmt->rm_owner = cpu_to_be64(ino);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000224 rmt->rm_blkno = cpu_to_be64(bno);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100225
226 return sizeof(struct xfs_attr3_rmt_hdr);
227}
228
229/*
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000230 * Helper functions to copy attribute data in and out of the one disk extents
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100231 */
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000232STATIC int
233xfs_attr_rmtval_copyout(
234 struct xfs_mount *mp,
235 struct xfs_buf *bp,
236 xfs_ino_t ino,
237 int *offset,
238 int *valuelen,
Dave Chinner836a94a2013-08-12 20:49:44 +1000239 __uint8_t **dst)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100240{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000241 char *src = bp->b_addr;
242 xfs_daddr_t bno = bp->b_bn;
243 int len = BBTOB(bp->b_length);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100244
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000245 ASSERT(len >= XFS_LBSIZE(mp));
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100246
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000247 while (len > 0 && *valuelen > 0) {
248 int hdr_size = 0;
249 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, XFS_LBSIZE(mp));
250
Dave Chinnerc5c249b2013-08-12 20:49:43 +1000251 byte_cnt = min(*valuelen, byte_cnt);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000252
253 if (xfs_sb_version_hascrc(&mp->m_sb)) {
254 if (!xfs_attr3_rmt_hdr_ok(mp, src, ino, *offset,
255 byte_cnt, bno)) {
256 xfs_alert(mp,
257"remote attribute header mismatch bno/off/len/owner (0x%llx/0x%x/Ox%x/0x%llx)",
258 bno, *offset, byte_cnt, ino);
259 return EFSCORRUPTED;
260 }
261 hdr_size = sizeof(struct xfs_attr3_rmt_hdr);
262 }
263
264 memcpy(*dst, src + hdr_size, byte_cnt);
265
266 /* roll buffer forwards */
267 len -= XFS_LBSIZE(mp);
268 src += XFS_LBSIZE(mp);
269 bno += mp->m_bsize;
270
271 /* roll attribute data forwards */
272 *valuelen -= byte_cnt;
273 *dst += byte_cnt;
274 *offset += byte_cnt;
275 }
276 return 0;
277}
278
279STATIC void
280xfs_attr_rmtval_copyin(
281 struct xfs_mount *mp,
282 struct xfs_buf *bp,
283 xfs_ino_t ino,
284 int *offset,
285 int *valuelen,
Dave Chinner836a94a2013-08-12 20:49:44 +1000286 __uint8_t **src)
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000287{
288 char *dst = bp->b_addr;
289 xfs_daddr_t bno = bp->b_bn;
290 int len = BBTOB(bp->b_length);
291
292 ASSERT(len >= XFS_LBSIZE(mp));
293
294 while (len > 0 && *valuelen > 0) {
295 int hdr_size;
296 int byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, XFS_LBSIZE(mp));
297
298 byte_cnt = min(*valuelen, byte_cnt);
299 hdr_size = xfs_attr3_rmt_hdr_set(mp, dst, ino, *offset,
300 byte_cnt, bno);
301
302 memcpy(dst + hdr_size, *src, byte_cnt);
303
304 /*
305 * If this is the last block, zero the remainder of it.
306 * Check that we are actually the last block, too.
307 */
308 if (byte_cnt + hdr_size < XFS_LBSIZE(mp)) {
309 ASSERT(*valuelen - byte_cnt == 0);
310 ASSERT(len == XFS_LBSIZE(mp));
311 memset(dst + hdr_size + byte_cnt, 0,
312 XFS_LBSIZE(mp) - hdr_size - byte_cnt);
313 }
314
315 /* roll buffer forwards */
316 len -= XFS_LBSIZE(mp);
317 dst += XFS_LBSIZE(mp);
318 bno += mp->m_bsize;
319
320 /* roll attribute data forwards */
321 *valuelen -= byte_cnt;
322 *src += byte_cnt;
323 *offset += byte_cnt;
324 }
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100325}
326
327/*
Dave Chinner95920cd2013-04-03 16:11:27 +1100328 * Read the value associated with an attribute from the out-of-line buffer
329 * that we stored it in.
330 */
331int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100332xfs_attr_rmtval_get(
333 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100334{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100335 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
336 struct xfs_mount *mp = args->dp->i_mount;
337 struct xfs_buf *bp;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100338 xfs_dablk_t lblkno = args->rmtblkno;
Dave Chinner836a94a2013-08-12 20:49:44 +1000339 __uint8_t *dst = args->value;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000340 int valuelen;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100341 int nmap;
342 int error;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000343 int blkcnt = args->rmtblkcnt;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100344 int i;
345 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100346
347 trace_xfs_attr_rmtval_get(args);
348
349 ASSERT(!(args->flags & ATTR_KERNOVAL));
Dave Chinner8275cdd2014-05-06 07:37:31 +1000350 ASSERT(args->rmtvaluelen == args->valuelen);
Dave Chinner95920cd2013-04-03 16:11:27 +1100351
Dave Chinner8275cdd2014-05-06 07:37:31 +1000352 valuelen = args->rmtvaluelen;
Dave Chinner95920cd2013-04-03 16:11:27 +1100353 while (valuelen > 0) {
354 nmap = ATTR_RMTVALUE_MAPSIZE;
355 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
Dave Chinner551b3822013-05-21 18:02:02 +1000356 blkcnt, map, &nmap,
Dave Chinner95920cd2013-04-03 16:11:27 +1100357 XFS_BMAPI_ATTRFORK);
358 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100359 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100360 ASSERT(nmap >= 1);
361
362 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000363 xfs_daddr_t dblkno;
364 int dblkcnt;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100365
Dave Chinner95920cd2013-04-03 16:11:27 +1100366 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
367 (map[i].br_startblock != HOLESTARTBLOCK));
368 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000369 dblkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100370 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000371 dblkno, dblkcnt, 0, &bp,
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100372 &xfs_attr3_rmt_buf_ops);
Dave Chinner95920cd2013-04-03 16:11:27 +1100373 if (error)
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100374 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100375
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000376 error = xfs_attr_rmtval_copyout(mp, bp, args->dp->i_ino,
377 &offset, &valuelen,
378 &dst);
Dave Chinner95920cd2013-04-03 16:11:27 +1100379 xfs_buf_relse(bp);
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000380 if (error)
381 return error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100382
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000383 /* roll attribute extent map forwards */
Dave Chinner95920cd2013-04-03 16:11:27 +1100384 lblkno += map[i].br_blockcount;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000385 blkcnt -= map[i].br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100386 }
387 }
388 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100389 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100390}
391
392/*
393 * Write the value associated with an attribute into the out-of-line buffer
394 * that we have defined for it.
395 */
396int
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100397xfs_attr_rmtval_set(
398 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100399{
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100400 struct xfs_inode *dp = args->dp;
401 struct xfs_mount *mp = dp->i_mount;
402 struct xfs_bmbt_irec map;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100403 xfs_dablk_t lblkno;
404 xfs_fileoff_t lfileoff = 0;
Dave Chinner836a94a2013-08-12 20:49:44 +1000405 __uint8_t *src = args->value;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100406 int blkcnt;
407 int valuelen;
408 int nmap;
409 int error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100410 int offset = 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100411
412 trace_xfs_attr_rmtval_set(args);
413
Dave Chinner95920cd2013-04-03 16:11:27 +1100414 /*
415 * Find a "hole" in the attribute address space large enough for
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100416 * us to drop the new attribute's value into. Because CRC enable
417 * attributes have headers, we can't just do a straight byte to FSB
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000418 * conversion and have to take the header space into account.
Dave Chinner95920cd2013-04-03 16:11:27 +1100419 */
Dave Chinner8275cdd2014-05-06 07:37:31 +1000420 blkcnt = xfs_attr3_rmt_blocks(mp, args->rmtvaluelen);
Dave Chinner95920cd2013-04-03 16:11:27 +1100421 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
422 XFS_ATTR_FORK);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100423 if (error)
424 return error;
425
Dave Chinner95920cd2013-04-03 16:11:27 +1100426 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
427 args->rmtblkcnt = blkcnt;
428
429 /*
430 * Roll through the "value", allocating blocks on disk as required.
431 */
432 while (blkcnt > 0) {
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100433 int committed;
434
Dave Chinner95920cd2013-04-03 16:11:27 +1100435 /*
436 * Allocate a single extent, up to the size of the value.
437 */
438 xfs_bmap_init(args->flist, args->firstblock);
439 nmap = 1;
440 error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
441 blkcnt,
442 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
443 args->firstblock, args->total, &map, &nmap,
444 args->flist);
445 if (!error) {
446 error = xfs_bmap_finish(&args->trans, args->flist,
447 &committed);
448 }
449 if (error) {
450 ASSERT(committed);
451 args->trans = NULL;
452 xfs_bmap_cancel(args->flist);
453 return(error);
454 }
455
456 /*
457 * bmap_finish() may have committed the last trans and started
458 * a new one. We need the inode to be in all transactions.
459 */
460 if (committed)
461 xfs_trans_ijoin(args->trans, dp, 0);
462
463 ASSERT(nmap == 1);
464 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
465 (map.br_startblock != HOLESTARTBLOCK));
466 lblkno += map.br_blockcount;
467 blkcnt -= map.br_blockcount;
468
469 /*
470 * Start the next trans in the chain.
471 */
472 error = xfs_trans_roll(&args->trans, dp);
473 if (error)
474 return (error);
475 }
476
477 /*
478 * Roll through the "value", copying the attribute value to the
479 * already-allocated blocks. Blocks are written synchronously
480 * so that we can know they are all on disk before we turn off
481 * the INCOMPLETE flag.
482 */
483 lblkno = args->rmtblkno;
Dave Chinner26f71442013-05-21 18:02:03 +1000484 blkcnt = args->rmtblkcnt;
Dave Chinner8275cdd2014-05-06 07:37:31 +1000485 valuelen = args->rmtvaluelen;
Dave Chinner95920cd2013-04-03 16:11:27 +1100486 while (valuelen > 0) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000487 struct xfs_buf *bp;
488 xfs_daddr_t dblkno;
489 int dblkcnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100490
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000491 ASSERT(blkcnt > 0);
492
Dave Chinner95920cd2013-04-03 16:11:27 +1100493 xfs_bmap_init(args->flist, args->firstblock);
494 nmap = 1;
495 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
Dave Chinner26f71442013-05-21 18:02:03 +1000496 blkcnt, &map, &nmap,
Dave Chinner95920cd2013-04-03 16:11:27 +1100497 XFS_BMAPI_ATTRFORK);
498 if (error)
499 return(error);
500 ASSERT(nmap == 1);
501 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
502 (map.br_startblock != HOLESTARTBLOCK));
503
504 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
Dave Chinner26f71442013-05-21 18:02:03 +1000505 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100506
Dave Chinner26f71442013-05-21 18:02:03 +1000507 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0);
Dave Chinner95920cd2013-04-03 16:11:27 +1100508 if (!bp)
509 return ENOMEM;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100510 bp->b_ops = &xfs_attr3_rmt_buf_ops;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100511
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000512 xfs_attr_rmtval_copyin(mp, bp, args->dp->i_ino, &offset,
513 &valuelen, &src);
Dave Chinner95920cd2013-04-03 16:11:27 +1100514
515 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
516 xfs_buf_relse(bp);
517 if (error)
518 return error;
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100519
Dave Chinner95920cd2013-04-03 16:11:27 +1100520
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000521 /* roll attribute extent map forwards */
Dave Chinner95920cd2013-04-03 16:11:27 +1100522 lblkno += map.br_blockcount;
Dave Chinner26f71442013-05-21 18:02:03 +1000523 blkcnt -= map.br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100524 }
525 ASSERT(valuelen == 0);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100526 return 0;
Dave Chinner95920cd2013-04-03 16:11:27 +1100527}
528
529/*
530 * Remove the value associated with an attribute by deleting the
531 * out-of-line buffer that it is stored on.
532 */
533int
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000534xfs_attr_rmtval_remove(
535 struct xfs_da_args *args)
Dave Chinner95920cd2013-04-03 16:11:27 +1100536{
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000537 struct xfs_mount *mp = args->dp->i_mount;
538 xfs_dablk_t lblkno;
539 int blkcnt;
540 int error;
541 int done;
Dave Chinner95920cd2013-04-03 16:11:27 +1100542
543 trace_xfs_attr_rmtval_remove(args);
544
Dave Chinner95920cd2013-04-03 16:11:27 +1100545 /*
Dave Chinner58a72282013-05-21 18:02:04 +1000546 * Roll through the "value", invalidating the attribute value's blocks.
Dave Chinner95920cd2013-04-03 16:11:27 +1100547 */
548 lblkno = args->rmtblkno;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000549 blkcnt = args->rmtblkcnt;
550 while (blkcnt > 0) {
551 struct xfs_bmbt_irec map;
552 struct xfs_buf *bp;
553 xfs_daddr_t dblkno;
554 int dblkcnt;
555 int nmap;
Dave Chinner58a72282013-05-21 18:02:04 +1000556
Dave Chinner95920cd2013-04-03 16:11:27 +1100557 /*
558 * Try to remember where we decided to put the value.
559 */
560 nmap = 1;
561 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
Dave Chinner58a72282013-05-21 18:02:04 +1000562 blkcnt, &map, &nmap, XFS_BMAPI_ATTRFORK);
Dave Chinner95920cd2013-04-03 16:11:27 +1100563 if (error)
564 return(error);
565 ASSERT(nmap == 1);
566 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
567 (map.br_startblock != HOLESTARTBLOCK));
568
569 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
Dave Chinner58a72282013-05-21 18:02:04 +1000570 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
Dave Chinner95920cd2013-04-03 16:11:27 +1100571
572 /*
573 * If the "remote" value is in the cache, remove it.
574 */
Dave Chinner58a72282013-05-21 18:02:04 +1000575 bp = xfs_incore(mp->m_ddev_targp, dblkno, dblkcnt, XBF_TRYLOCK);
Dave Chinner95920cd2013-04-03 16:11:27 +1100576 if (bp) {
577 xfs_buf_stale(bp);
578 xfs_buf_relse(bp);
579 bp = NULL;
580 }
581
Dave Chinner95920cd2013-04-03 16:11:27 +1100582 lblkno += map.br_blockcount;
Dave Chinner58a72282013-05-21 18:02:04 +1000583 blkcnt -= map.br_blockcount;
Dave Chinner95920cd2013-04-03 16:11:27 +1100584 }
585
586 /*
587 * Keep de-allocating extents until the remote-value region is gone.
588 */
589 lblkno = args->rmtblkno;
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000590 blkcnt = args->rmtblkcnt;
Dave Chinner95920cd2013-04-03 16:11:27 +1100591 done = 0;
592 while (!done) {
Dave Chinner7bc0dc22013-05-21 18:02:08 +1000593 int committed;
594
Dave Chinner95920cd2013-04-03 16:11:27 +1100595 xfs_bmap_init(args->flist, args->firstblock);
596 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
597 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
598 1, args->firstblock, args->flist,
599 &done);
600 if (!error) {
601 error = xfs_bmap_finish(&args->trans, args->flist,
602 &committed);
603 }
604 if (error) {
605 ASSERT(committed);
606 args->trans = NULL;
607 xfs_bmap_cancel(args->flist);
Dave Chinnerd2e448d2013-04-03 16:11:28 +1100608 return error;
Dave Chinner95920cd2013-04-03 16:11:27 +1100609 }
610
611 /*
612 * bmap_finish() may have committed the last trans and started
613 * a new one. We need the inode to be in all transactions.
614 */
615 if (committed)
616 xfs_trans_ijoin(args->trans, args->dp, 0);
617
618 /*
619 * Close out trans and start the next one in the chain.
620 */
621 error = xfs_trans_roll(&args->trans, args->dp);
622 if (error)
623 return (error);
624 }
625 return(0);
626}