xfs: remote attribute tail zeroing does too much
[deliverable/linux.git] / fs / xfs / xfs_attr_remote.c
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
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"
41 #include "xfs_cksum.h"
42 #include "xfs_buf_item.h"
43
44 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
45
46 /*
47 * Each contiguous block has a header, so it is not just a simple attribute
48 * length to FSB conversion.
49 */
50 static int
51 xfs_attr3_rmt_blocks(
52 struct xfs_mount *mp,
53 int attrlen)
54 {
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);
60 }
61
62 static bool
63 xfs_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) +
78 be32_to_cpu(rmt->rm_bytes) >= XATTR_SIZE_MAX)
79 return false;
80 if (rmt->rm_owner == 0)
81 return false;
82
83 return true;
84 }
85
86 static void
87 xfs_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
104 static void
105 xfs_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
129 const 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
134 static int
135 xfs_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 */
163 static bool
164 xfs_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;
182 }
183
184 /*
185 * Read the value associated with an attribute from the out-of-line buffer
186 * that we stored it in.
187 */
188 int
189 xfs_attr_rmtval_get(
190 struct xfs_da_args *args)
191 {
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;
204
205 trace_xfs_attr_rmtval_get(args);
206
207 ASSERT(!(args->flags & ATTR_KERNOVAL));
208
209 while (valuelen > 0) {
210 nmap = ATTR_RMTVALUE_MAPSIZE;
211 blkcnt = xfs_attr3_rmt_blocks(mp, valuelen);
212 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
213 blkcnt, map, &nmap,
214 XFS_BMAPI_ATTRFORK);
215 if (error)
216 return error;
217 ASSERT(nmap >= 1);
218
219 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
220 int byte_cnt;
221 char *src;
222
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,
228 dblkno, blkcnt, 0, &bp,
229 &xfs_attr3_rmt_buf_ops);
230 if (error)
231 return error;
232
233 byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, BBTOB(bp->b_length));
234 byte_cnt = min_t(int, valuelen, byte_cnt);
235
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);
252 xfs_buf_relse(bp);
253
254 offset += byte_cnt;
255 dst += byte_cnt;
256 valuelen -= byte_cnt;
257
258 lblkno += map[i].br_blockcount;
259 }
260 }
261 ASSERT(valuelen == 0);
262 return 0;
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 */
269 int
270 xfs_attr_rmtval_set(
271 struct xfs_da_args *args)
272 {
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;
288
289 trace_xfs_attr_rmtval_set(args);
290
291 /*
292 * Find a "hole" in the attribute address space large enough for
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.
298 */
299 blkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
300
301 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
302 XFS_ATTR_FORK);
303 if (error)
304 return error;
305
306 /* Start with the attribute data. We'll allocate the rest afterwards. */
307 if (crcs)
308 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
309
310 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
311 args->rmtblkcnt = blkcnt;
312
313 /*
314 * Roll through the "value", allocating blocks on disk as required.
315 */
316 while (blkcnt > 0) {
317 int committed;
318
319 /*
320 * Allocate a single extent, up to the size of the value.
321 */
322 xfs_bmap_init(args->flist, args->firstblock);
323 nmap = 1;
324 error = xfs_bmapi_write(args->trans, dp, (xfs_fileoff_t)lblkno,
325 blkcnt,
326 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
327 args->firstblock, args->total, &map, &nmap,
328 args->flist);
329 if (!error) {
330 error = xfs_bmap_finish(&args->trans, args->flist,
331 &committed);
332 }
333 if (error) {
334 ASSERT(committed);
335 args->trans = NULL;
336 xfs_bmap_cancel(args->flist);
337 return(error);
338 }
339
340 /*
341 * bmap_finish() may have committed the last trans and started
342 * a new one. We need the inode to be in all transactions.
343 */
344 if (committed)
345 xfs_trans_ijoin(args->trans, dp, 0);
346
347 ASSERT(nmap == 1);
348 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
349 (map.br_startblock != HOLESTARTBLOCK));
350 lblkno += map.br_blockcount;
351 blkcnt -= map.br_blockcount;
352 hdrcnt++;
353
354 /*
355 * If we have enough blocks for the attribute data, calculate
356 * how many extra blocks we need for headers. We might run
357 * through this multiple times in the case that the additional
358 * headers in the blocks needed for the data fragments spills
359 * into requiring more blocks. e.g. for 512 byte blocks, we'll
360 * spill for another block every 9 headers we require in this
361 * loop.
362 *
363 * Note that this can result in contiguous allocation of blocks,
364 * so we don't use all the space we allocate for headers as we
365 * have one less header for each contiguous allocation that
366 * occurs in the map/write loop below.
367 */
368 if (crcs && blkcnt == 0) {
369 int total_len;
370
371 total_len = args->valuelen +
372 hdrcnt * sizeof(struct xfs_attr3_rmt_hdr);
373 blkcnt = XFS_B_TO_FSB(mp, total_len);
374 blkcnt -= args->rmtblkcnt;
375 args->rmtblkcnt += blkcnt;
376 }
377
378 /*
379 * Start the next trans in the chain.
380 */
381 error = xfs_trans_roll(&args->trans, dp);
382 if (error)
383 return (error);
384 }
385
386 /*
387 * Roll through the "value", copying the attribute value to the
388 * already-allocated blocks. Blocks are written synchronously
389 * so that we can know they are all on disk before we turn off
390 * the INCOMPLETE flag.
391 */
392 lblkno = args->rmtblkno;
393 valuelen = args->valuelen;
394 blkcnt = args->rmtblkcnt;
395 while (valuelen > 0) {
396 int byte_cnt;
397 int hdr_size;
398 int dblkcnt;
399 char *buf;
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 blkcnt, &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 dblkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
417
418 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, dblkcnt, 0);
419 if (!bp)
420 return ENOMEM;
421 bp->b_ops = &xfs_attr3_rmt_buf_ops;
422 buf = bp->b_addr;
423
424 byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, BBTOB(bp->b_length));
425 byte_cnt = min_t(int, valuelen, byte_cnt);
426 hdr_size = xfs_attr3_rmt_hdr_set(mp, dp->i_ino, offset,
427 byte_cnt, bp);
428 ASSERT(hdr_size + byte_cnt <= BBTOB(bp->b_length));
429
430 memcpy(buf + hdr_size, src, byte_cnt);
431
432 if (byte_cnt + hdr_size < BBTOB(bp->b_length))
433 xfs_buf_zero(bp, byte_cnt + hdr_size,
434 BBTOB(bp->b_length) - byte_cnt - hdr_size);
435
436 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
437 xfs_buf_relse(bp);
438 if (error)
439 return error;
440
441 src += byte_cnt;
442 valuelen -= byte_cnt;
443 offset += byte_cnt;
444
445 lblkno += map.br_blockcount;
446 blkcnt -= map.br_blockcount;
447 }
448 ASSERT(valuelen == 0);
449 return 0;
450 }
451
452 /*
453 * Remove the value associated with an attribute by deleting the
454 * out-of-line buffer that it is stored on.
455 */
456 int
457 xfs_attr_rmtval_remove(xfs_da_args_t *args)
458 {
459 xfs_mount_t *mp;
460 xfs_bmbt_irec_t map;
461 xfs_buf_t *bp;
462 xfs_daddr_t dblkno;
463 xfs_dablk_t lblkno;
464 int valuelen, blkcnt, nmap, error, done, committed;
465
466 trace_xfs_attr_rmtval_remove(args);
467
468 mp = args->dp->i_mount;
469
470 /*
471 * Roll through the "value", invalidating the attribute value's
472 * blocks.
473 */
474 lblkno = args->rmtblkno;
475 valuelen = args->rmtblkcnt;
476 while (valuelen > 0) {
477 /*
478 * Try to remember where we decided to put the value.
479 */
480 nmap = 1;
481 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
482 args->rmtblkcnt, &map, &nmap,
483 XFS_BMAPI_ATTRFORK);
484 if (error)
485 return(error);
486 ASSERT(nmap == 1);
487 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
488 (map.br_startblock != HOLESTARTBLOCK));
489
490 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
491 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
492
493 /*
494 * If the "remote" value is in the cache, remove it.
495 */
496 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
497 if (bp) {
498 xfs_buf_stale(bp);
499 xfs_buf_relse(bp);
500 bp = NULL;
501 }
502
503 valuelen -= map.br_blockcount;
504
505 lblkno += map.br_blockcount;
506 }
507
508 /*
509 * Keep de-allocating extents until the remote-value region is gone.
510 */
511 lblkno = args->rmtblkno;
512 blkcnt = args->rmtblkcnt;
513 done = 0;
514 while (!done) {
515 xfs_bmap_init(args->flist, args->firstblock);
516 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
517 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
518 1, args->firstblock, args->flist,
519 &done);
520 if (!error) {
521 error = xfs_bmap_finish(&args->trans, args->flist,
522 &committed);
523 }
524 if (error) {
525 ASSERT(committed);
526 args->trans = NULL;
527 xfs_bmap_cancel(args->flist);
528 return error;
529 }
530
531 /*
532 * bmap_finish() may have committed the last trans and started
533 * a new one. We need the inode to be in all transactions.
534 */
535 if (committed)
536 xfs_trans_ijoin(args->trans, args->dp, 0);
537
538 /*
539 * Close out trans and start the next one in the chain.
540 */
541 error = xfs_trans_roll(&args->trans, args->dp);
542 if (error)
543 return (error);
544 }
545 return(0);
546 }
547
This page took 0.059338 seconds and 5 git commands to generate.