]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/xfs/xfs_attr_remote.c
xfs: remote attribute read too short
[mirror_ubuntu-bionic-kernel.git] / fs / xfs / xfs_attr_remote.c
CommitLineData
95920cd6
DC
1/*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
d2e448d5 3 * Copyright (c) 2013 Red Hat, Inc.
95920cd6
DC
4 * 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"
21#include "xfs_types.h"
22#include "xfs_bit.h"
23#include "xfs_log.h"
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
27#include "xfs_mount.h"
28#include "xfs_error.h"
29#include "xfs_da_btree.h"
30#include "xfs_bmap_btree.h"
31#include "xfs_dinode.h"
32#include "xfs_inode.h"
33#include "xfs_alloc.h"
34#include "xfs_inode_item.h"
35#include "xfs_bmap.h"
36#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"
d2e448d5
DC
41#include "xfs_cksum.h"
42#include "xfs_buf_item.h"
95920cd6
DC
43
44#define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
45
d2e448d5
DC
46/*
47 * Each contiguous block has a header, so it is not just a simple attribute
48 * length to FSB conversion.
49 */
50static int
51xfs_attr3_rmt_blocks(
52 struct xfs_mount *mp,
53 int attrlen)
54{
551b382f
DC
55 if (xfs_sb_version_hascrc(&mp->m_sb)) {
56 int buflen = XFS_ATTR3_RMT_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
57 return (attrlen + buflen - 1) / buflen;
58 }
59 return XFS_B_TO_FSB(mp, attrlen);
d2e448d5
DC
60}
61
62static bool
63xfs_attr3_rmt_verify(
64 struct xfs_buf *bp)
65{
66 struct xfs_mount *mp = bp->b_target->bt_mount;
67 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
68
69 if (!xfs_sb_version_hascrc(&mp->m_sb))
70 return false;
71 if (rmt->rm_magic != cpu_to_be32(XFS_ATTR3_RMT_MAGIC))
72 return false;
73 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_uuid))
74 return false;
75 if (bp->b_bn != be64_to_cpu(rmt->rm_blkno))
76 return false;
77 if (be32_to_cpu(rmt->rm_offset) +
946217ba 78 be32_to_cpu(rmt->rm_bytes) >= XATTR_SIZE_MAX)
d2e448d5
DC
79 return false;
80 if (rmt->rm_owner == 0)
81 return false;
82
83 return true;
84}
85
86static void
87xfs_attr3_rmt_read_verify(
88 struct xfs_buf *bp)
89{
90 struct xfs_mount *mp = bp->b_target->bt_mount;
91
92 /* no verification of non-crc buffers */
93 if (!xfs_sb_version_hascrc(&mp->m_sb))
94 return;
95
96 if (!xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
97 XFS_ATTR3_RMT_CRC_OFF) ||
98 !xfs_attr3_rmt_verify(bp)) {
99 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
100 xfs_buf_ioerror(bp, EFSCORRUPTED);
101 }
102}
103
104static void
105xfs_attr3_rmt_write_verify(
106 struct xfs_buf *bp)
107{
108 struct xfs_mount *mp = bp->b_target->bt_mount;
109 struct xfs_buf_log_item *bip = bp->b_fspriv;
110
111 /* no verification of non-crc buffers */
112 if (!xfs_sb_version_hascrc(&mp->m_sb))
113 return;
114
115 if (!xfs_attr3_rmt_verify(bp)) {
116 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
117 xfs_buf_ioerror(bp, EFSCORRUPTED);
118 return;
119 }
120
121 if (bip) {
122 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
123 rmt->rm_lsn = cpu_to_be64(bip->bli_item.li_lsn);
124 }
125 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
126 XFS_ATTR3_RMT_CRC_OFF);
127}
128
129const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
130 .verify_read = xfs_attr3_rmt_read_verify,
131 .verify_write = xfs_attr3_rmt_write_verify,
132};
133
134static int
135xfs_attr3_rmt_hdr_set(
136 struct xfs_mount *mp,
137 xfs_ino_t ino,
138 uint32_t offset,
139 uint32_t size,
140 struct xfs_buf *bp)
141{
142 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
143
144 if (!xfs_sb_version_hascrc(&mp->m_sb))
145 return 0;
146
147 rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC);
148 rmt->rm_offset = cpu_to_be32(offset);
149 rmt->rm_bytes = cpu_to_be32(size);
150 uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_uuid);
151 rmt->rm_owner = cpu_to_be64(ino);
152 rmt->rm_blkno = cpu_to_be64(bp->b_bn);
153 bp->b_ops = &xfs_attr3_rmt_buf_ops;
154
155 return sizeof(struct xfs_attr3_rmt_hdr);
156}
157
158/*
159 * Checking of the remote attribute header is split into two parts. the verifier
160 * does CRC, location and bounds checking, the unpacking function checks the
161 * attribute parameters and owner.
162 */
163static bool
164xfs_attr3_rmt_hdr_ok(
165 struct xfs_mount *mp,
166 xfs_ino_t ino,
167 uint32_t offset,
168 uint32_t size,
169 struct xfs_buf *bp)
170{
171 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
172
173 if (offset != be32_to_cpu(rmt->rm_offset))
174 return false;
175 if (size != be32_to_cpu(rmt->rm_bytes))
176 return false;
177 if (ino != be64_to_cpu(rmt->rm_owner))
178 return false;
179
180 /* ok */
181 return true;
d2e448d5
DC
182}
183
95920cd6
DC
184/*
185 * Read the value associated with an attribute from the out-of-line buffer
186 * that we stored it in.
187 */
188int
d2e448d5
DC
189xfs_attr_rmtval_get(
190 struct xfs_da_args *args)
95920cd6 191{
d2e448d5
DC
192 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
193 struct xfs_mount *mp = args->dp->i_mount;
194 struct xfs_buf *bp;
195 xfs_daddr_t dblkno;
196 xfs_dablk_t lblkno = args->rmtblkno;
197 void *dst = args->value;
198 int valuelen = args->valuelen;
199 int nmap;
200 int error;
201 int blkcnt;
202 int i;
203 int offset = 0;
95920cd6
DC
204
205 trace_xfs_attr_rmtval_get(args);
206
207 ASSERT(!(args->flags & ATTR_KERNOVAL));
208
95920cd6
DC
209 while (valuelen > 0) {
210 nmap = ATTR_RMTVALUE_MAPSIZE;
551b382f 211 blkcnt = xfs_attr3_rmt_blocks(mp, valuelen);
95920cd6 212 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
551b382f 213 blkcnt, map, &nmap,
95920cd6
DC
214 XFS_BMAPI_ATTRFORK);
215 if (error)
d2e448d5 216 return error;
95920cd6
DC
217 ASSERT(nmap >= 1);
218
219 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
d2e448d5
DC
220 int byte_cnt;
221 char *src;
222
95920cd6
DC
223 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
224 (map[i].br_startblock != HOLESTARTBLOCK));
225 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
226 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
227 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
d2e448d5
DC
228 dblkno, blkcnt, 0, &bp,
229 &xfs_attr3_rmt_buf_ops);
95920cd6 230 if (error)
d2e448d5
DC
231 return error;
232
551b382f
DC
233 byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, BBTOB(bp->b_length));
234 byte_cnt = min_t(int, valuelen, byte_cnt);
95920cd6 235
d2e448d5
DC
236 src = bp->b_addr;
237 if (xfs_sb_version_hascrc(&mp->m_sb)) {
238 if (!xfs_attr3_rmt_hdr_ok(mp, args->dp->i_ino,
239 offset, byte_cnt, bp)) {
240 xfs_alert(mp,
241"remote attribute header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
242 offset, byte_cnt, args->dp->i_ino);
243 xfs_buf_relse(bp);
244 return EFSCORRUPTED;
245
246 }
247
248 src += sizeof(struct xfs_attr3_rmt_hdr);
249 }
250
251 memcpy(dst, src, byte_cnt);
95920cd6 252 xfs_buf_relse(bp);
d2e448d5
DC
253
254 offset += byte_cnt;
255 dst += byte_cnt;
256 valuelen -= byte_cnt;
95920cd6
DC
257
258 lblkno += map[i].br_blockcount;
259 }
260 }
261 ASSERT(valuelen == 0);
d2e448d5 262 return 0;
95920cd6
DC
263}
264
265/*
266 * Write the value associated with an attribute into the out-of-line buffer
267 * that we have defined for it.
268 */
269int
d2e448d5
DC
270xfs_attr_rmtval_set(
271 struct xfs_da_args *args)
95920cd6 272{
d2e448d5
DC
273 struct xfs_inode *dp = args->dp;
274 struct xfs_mount *mp = dp->i_mount;
275 struct xfs_bmbt_irec map;
276 struct xfs_buf *bp;
277 xfs_daddr_t dblkno;
278 xfs_dablk_t lblkno;
279 xfs_fileoff_t lfileoff = 0;
280 void *src = args->value;
281 int blkcnt;
282 int valuelen;
283 int nmap;
284 int error;
285 int hdrcnt = 0;
286 bool crcs = xfs_sb_version_hascrc(&mp->m_sb);
287 int offset = 0;
95920cd6
DC
288
289 trace_xfs_attr_rmtval_set(args);
290
95920cd6
DC
291 /*
292 * Find a "hole" in the attribute address space large enough for
d2e448d5
DC
293 * us to drop the new attribute's value into. Because CRC enable
294 * attributes have headers, we can't just do a straight byte to FSB
295 * conversion. We calculate the worst case block count in this case
296 * and we may not need that many, so we have to handle this when
297 * allocating the blocks below.
95920cd6 298 */
d2e448d5
DC
299 if (!crcs)
300 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
301 else
302 blkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
303
95920cd6
DC
304 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
305 XFS_ATTR_FORK);
d2e448d5
DC
306 if (error)
307 return error;
308
309 /* Start with the attribute data. We'll allocate the rest afterwards. */
310 if (crcs)
311 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
312
95920cd6
DC
313 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
314 args->rmtblkcnt = blkcnt;
315
316 /*
317 * Roll through the "value", allocating blocks on disk as required.
318 */
319 while (blkcnt > 0) {
d2e448d5
DC
320 int committed;
321
95920cd6
DC
322 /*
323 * Allocate a single extent, up to the size of the value.
324 */
325 xfs_bmap_init(args->flist, args->firstblock);
326 nmap = 1;
327 error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
328 blkcnt,
329 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
330 args->firstblock, args->total, &map, &nmap,
331 args->flist);
332 if (!error) {
333 error = xfs_bmap_finish(&args->trans, args->flist,
334 &committed);
335 }
336 if (error) {
337 ASSERT(committed);
338 args->trans = NULL;
339 xfs_bmap_cancel(args->flist);
340 return(error);
341 }
342
343 /*
344 * bmap_finish() may have committed the last trans and started
345 * a new one. We need the inode to be in all transactions.
346 */
347 if (committed)
348 xfs_trans_ijoin(args->trans, dp, 0);
349
350 ASSERT(nmap == 1);
351 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
352 (map.br_startblock != HOLESTARTBLOCK));
353 lblkno += map.br_blockcount;
354 blkcnt -= map.br_blockcount;
d2e448d5
DC
355 hdrcnt++;
356
357 /*
358 * If we have enough blocks for the attribute data, calculate
359 * how many extra blocks we need for headers. We might run
360 * through this multiple times in the case that the additional
361 * headers in the blocks needed for the data fragments spills
362 * into requiring more blocks. e.g. for 512 byte blocks, we'll
363 * spill for another block every 9 headers we require in this
364 * loop.
9531e2de
DC
365 *
366 * Note that this can result in contiguous allocation of blocks,
367 * so we don't use all the space we allocate for headers as we
368 * have one less header for each contiguous allocation that
369 * occurs in the map/write loop below.
d2e448d5 370 */
d2e448d5
DC
371 if (crcs && blkcnt == 0) {
372 int total_len;
373
374 total_len = args->valuelen +
375 hdrcnt * sizeof(struct xfs_attr3_rmt_hdr);
376 blkcnt = XFS_B_TO_FSB(mp, total_len);
377 blkcnt -= args->rmtblkcnt;
378 args->rmtblkcnt += blkcnt;
379 }
95920cd6
DC
380
381 /*
382 * Start the next trans in the chain.
383 */
384 error = xfs_trans_roll(&args->trans, dp);
385 if (error)
386 return (error);
387 }
388
389 /*
390 * Roll through the "value", copying the attribute value to the
391 * already-allocated blocks. Blocks are written synchronously
392 * so that we can know they are all on disk before we turn off
393 * the INCOMPLETE flag.
394 */
395 lblkno = args->rmtblkno;
396 valuelen = args->valuelen;
397 while (valuelen > 0) {
d2e448d5
DC
398 int byte_cnt;
399 char *buf;
95920cd6
DC
400
401 /*
402 * Try to remember where we decided to put the value.
403 */
404 xfs_bmap_init(args->flist, args->firstblock);
405 nmap = 1;
406 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
407 args->rmtblkcnt, &map, &nmap,
408 XFS_BMAPI_ATTRFORK);
409 if (error)
410 return(error);
411 ASSERT(nmap == 1);
412 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
413 (map.br_startblock != HOLESTARTBLOCK));
414
415 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
416 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
417
418 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, blkcnt, 0);
419 if (!bp)
420 return ENOMEM;
d2e448d5
DC
421 bp->b_ops = &xfs_attr3_rmt_buf_ops;
422
423 byte_cnt = BBTOB(bp->b_length);
424 byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, byte_cnt);
946217ba 425 if (valuelen < byte_cnt)
d2e448d5 426 byte_cnt = valuelen;
d2e448d5
DC
427
428 buf = bp->b_addr;
429 buf += xfs_attr3_rmt_hdr_set(mp, dp->i_ino, offset,
430 byte_cnt, bp);
431 memcpy(buf, src, byte_cnt);
95920cd6 432
d2e448d5
DC
433 if (byte_cnt < BBTOB(bp->b_length))
434 xfs_buf_zero(bp, byte_cnt,
435 BBTOB(bp->b_length) - byte_cnt);
95920cd6
DC
436
437 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
438 xfs_buf_relse(bp);
439 if (error)
440 return error;
d2e448d5
DC
441
442 src += byte_cnt;
443 valuelen -= byte_cnt;
444 offset += byte_cnt;
445 hdrcnt--;
95920cd6
DC
446
447 lblkno += map.br_blockcount;
448 }
449 ASSERT(valuelen == 0);
d2e448d5 450 return 0;
95920cd6
DC
451}
452
453/*
454 * Remove the value associated with an attribute by deleting the
455 * out-of-line buffer that it is stored on.
456 */
457int
458xfs_attr_rmtval_remove(xfs_da_args_t *args)
459{
460 xfs_mount_t *mp;
461 xfs_bmbt_irec_t map;
462 xfs_buf_t *bp;
463 xfs_daddr_t dblkno;
464 xfs_dablk_t lblkno;
465 int valuelen, blkcnt, nmap, error, done, committed;
466
467 trace_xfs_attr_rmtval_remove(args);
468
469 mp = args->dp->i_mount;
470
471 /*
472 * Roll through the "value", invalidating the attribute value's
473 * blocks.
474 */
475 lblkno = args->rmtblkno;
476 valuelen = args->rmtblkcnt;
477 while (valuelen > 0) {
478 /*
479 * Try to remember where we decided to put the value.
480 */
481 nmap = 1;
482 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
483 args->rmtblkcnt, &map, &nmap,
484 XFS_BMAPI_ATTRFORK);
485 if (error)
486 return(error);
487 ASSERT(nmap == 1);
488 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
489 (map.br_startblock != HOLESTARTBLOCK));
490
491 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
492 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
493
494 /*
495 * If the "remote" value is in the cache, remove it.
496 */
497 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
498 if (bp) {
499 xfs_buf_stale(bp);
500 xfs_buf_relse(bp);
501 bp = NULL;
502 }
503
504 valuelen -= map.br_blockcount;
505
506 lblkno += map.br_blockcount;
507 }
508
509 /*
510 * Keep de-allocating extents until the remote-value region is gone.
511 */
512 lblkno = args->rmtblkno;
513 blkcnt = args->rmtblkcnt;
514 done = 0;
515 while (!done) {
516 xfs_bmap_init(args->flist, args->firstblock);
517 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
518 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
519 1, args->firstblock, args->flist,
520 &done);
521 if (!error) {
522 error = xfs_bmap_finish(&args->trans, args->flist,
523 &committed);
524 }
525 if (error) {
526 ASSERT(committed);
527 args->trans = NULL;
528 xfs_bmap_cancel(args->flist);
d2e448d5 529 return error;
95920cd6
DC
530 }
531
532 /*
533 * bmap_finish() may have committed the last trans and started
534 * a new one. We need the inode to be in all transactions.
535 */
536 if (committed)
537 xfs_trans_ijoin(args->trans, args->dp, 0);
538
539 /*
540 * Close out trans and start the next one in the chain.
541 */
542 error = xfs_trans_roll(&args->trans, args->dp);
543 if (error)
544 return (error);
545 }
546 return(0);
547}
548