xfs: remote attribute allocation may be contiguous
[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 int buflen = XFS_ATTR3_RMT_BUF_SPACE(mp,
56 mp->m_sb.sb_blocksize);
57 return (attrlen + buflen - 1) / buflen;
58 }
59
60 static bool
61 xfs_attr3_rmt_verify(
62 struct xfs_buf *bp)
63 {
64 struct xfs_mount *mp = bp->b_target->bt_mount;
65 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
66
67 if (!xfs_sb_version_hascrc(&mp->m_sb))
68 return false;
69 if (rmt->rm_magic != cpu_to_be32(XFS_ATTR3_RMT_MAGIC))
70 return false;
71 if (!uuid_equal(&rmt->rm_uuid, &mp->m_sb.sb_uuid))
72 return false;
73 if (bp->b_bn != be64_to_cpu(rmt->rm_blkno))
74 return false;
75 if (be32_to_cpu(rmt->rm_offset) +
76 be32_to_cpu(rmt->rm_bytes) >= XATTR_SIZE_MAX)
77 return false;
78 if (rmt->rm_owner == 0)
79 return false;
80
81 return true;
82 }
83
84 static void
85 xfs_attr3_rmt_read_verify(
86 struct xfs_buf *bp)
87 {
88 struct xfs_mount *mp = bp->b_target->bt_mount;
89
90 /* no verification of non-crc buffers */
91 if (!xfs_sb_version_hascrc(&mp->m_sb))
92 return;
93
94 if (!xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
95 XFS_ATTR3_RMT_CRC_OFF) ||
96 !xfs_attr3_rmt_verify(bp)) {
97 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
98 xfs_buf_ioerror(bp, EFSCORRUPTED);
99 }
100 }
101
102 static void
103 xfs_attr3_rmt_write_verify(
104 struct xfs_buf *bp)
105 {
106 struct xfs_mount *mp = bp->b_target->bt_mount;
107 struct xfs_buf_log_item *bip = bp->b_fspriv;
108
109 /* no verification of non-crc buffers */
110 if (!xfs_sb_version_hascrc(&mp->m_sb))
111 return;
112
113 if (!xfs_attr3_rmt_verify(bp)) {
114 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
115 xfs_buf_ioerror(bp, EFSCORRUPTED);
116 return;
117 }
118
119 if (bip) {
120 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
121 rmt->rm_lsn = cpu_to_be64(bip->bli_item.li_lsn);
122 }
123 xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
124 XFS_ATTR3_RMT_CRC_OFF);
125 }
126
127 const struct xfs_buf_ops xfs_attr3_rmt_buf_ops = {
128 .verify_read = xfs_attr3_rmt_read_verify,
129 .verify_write = xfs_attr3_rmt_write_verify,
130 };
131
132 static int
133 xfs_attr3_rmt_hdr_set(
134 struct xfs_mount *mp,
135 xfs_ino_t ino,
136 uint32_t offset,
137 uint32_t size,
138 struct xfs_buf *bp)
139 {
140 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
141
142 if (!xfs_sb_version_hascrc(&mp->m_sb))
143 return 0;
144
145 rmt->rm_magic = cpu_to_be32(XFS_ATTR3_RMT_MAGIC);
146 rmt->rm_offset = cpu_to_be32(offset);
147 rmt->rm_bytes = cpu_to_be32(size);
148 uuid_copy(&rmt->rm_uuid, &mp->m_sb.sb_uuid);
149 rmt->rm_owner = cpu_to_be64(ino);
150 rmt->rm_blkno = cpu_to_be64(bp->b_bn);
151 bp->b_ops = &xfs_attr3_rmt_buf_ops;
152
153 return sizeof(struct xfs_attr3_rmt_hdr);
154 }
155
156 /*
157 * Checking of the remote attribute header is split into two parts. the verifier
158 * does CRC, location and bounds checking, the unpacking function checks the
159 * attribute parameters and owner.
160 */
161 static bool
162 xfs_attr3_rmt_hdr_ok(
163 struct xfs_mount *mp,
164 xfs_ino_t ino,
165 uint32_t offset,
166 uint32_t size,
167 struct xfs_buf *bp)
168 {
169 struct xfs_attr3_rmt_hdr *rmt = bp->b_addr;
170
171 if (offset != be32_to_cpu(rmt->rm_offset))
172 return false;
173 if (size != be32_to_cpu(rmt->rm_bytes))
174 return false;
175 if (ino != be64_to_cpu(rmt->rm_owner))
176 return false;
177
178 /* ok */
179 return true;
180 }
181
182 /*
183 * Read the value associated with an attribute from the out-of-line buffer
184 * that we stored it in.
185 */
186 int
187 xfs_attr_rmtval_get(
188 struct xfs_da_args *args)
189 {
190 struct xfs_bmbt_irec map[ATTR_RMTVALUE_MAPSIZE];
191 struct xfs_mount *mp = args->dp->i_mount;
192 struct xfs_buf *bp;
193 xfs_daddr_t dblkno;
194 xfs_dablk_t lblkno = args->rmtblkno;
195 void *dst = args->value;
196 int valuelen = args->valuelen;
197 int nmap;
198 int error;
199 int blkcnt;
200 int i;
201 int offset = 0;
202
203 trace_xfs_attr_rmtval_get(args);
204
205 ASSERT(!(args->flags & ATTR_KERNOVAL));
206
207 while (valuelen > 0) {
208 nmap = ATTR_RMTVALUE_MAPSIZE;
209 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
210 args->rmtblkcnt, map, &nmap,
211 XFS_BMAPI_ATTRFORK);
212 if (error)
213 return error;
214 ASSERT(nmap >= 1);
215
216 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
217 int byte_cnt;
218 char *src;
219
220 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
221 (map[i].br_startblock != HOLESTARTBLOCK));
222 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
223 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
224 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp,
225 dblkno, blkcnt, 0, &bp,
226 &xfs_attr3_rmt_buf_ops);
227 if (error)
228 return error;
229
230 byte_cnt = min_t(int, valuelen, BBTOB(bp->b_length));
231 byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, byte_cnt);
232
233 src = bp->b_addr;
234 if (xfs_sb_version_hascrc(&mp->m_sb)) {
235 if (!xfs_attr3_rmt_hdr_ok(mp, args->dp->i_ino,
236 offset, byte_cnt, bp)) {
237 xfs_alert(mp,
238 "remote attribute header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
239 offset, byte_cnt, args->dp->i_ino);
240 xfs_buf_relse(bp);
241 return EFSCORRUPTED;
242
243 }
244
245 src += sizeof(struct xfs_attr3_rmt_hdr);
246 }
247
248 memcpy(dst, src, byte_cnt);
249 xfs_buf_relse(bp);
250
251 offset += byte_cnt;
252 dst += byte_cnt;
253 valuelen -= byte_cnt;
254
255 lblkno += map[i].br_blockcount;
256 }
257 }
258 ASSERT(valuelen == 0);
259 return 0;
260 }
261
262 /*
263 * Write the value associated with an attribute into the out-of-line buffer
264 * that we have defined for it.
265 */
266 int
267 xfs_attr_rmtval_set(
268 struct xfs_da_args *args)
269 {
270 struct xfs_inode *dp = args->dp;
271 struct xfs_mount *mp = dp->i_mount;
272 struct xfs_bmbt_irec map;
273 struct xfs_buf *bp;
274 xfs_daddr_t dblkno;
275 xfs_dablk_t lblkno;
276 xfs_fileoff_t lfileoff = 0;
277 void *src = args->value;
278 int blkcnt;
279 int valuelen;
280 int nmap;
281 int error;
282 int hdrcnt = 0;
283 bool crcs = xfs_sb_version_hascrc(&mp->m_sb);
284 int offset = 0;
285
286 trace_xfs_attr_rmtval_set(args);
287
288 /*
289 * Find a "hole" in the attribute address space large enough for
290 * us to drop the new attribute's value into. Because CRC enable
291 * attributes have headers, we can't just do a straight byte to FSB
292 * conversion. We calculate the worst case block count in this case
293 * and we may not need that many, so we have to handle this when
294 * allocating the blocks below.
295 */
296 if (!crcs)
297 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
298 else
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 while (valuelen > 0) {
395 int byte_cnt;
396 char *buf;
397
398 /*
399 * Try to remember where we decided to put the value.
400 */
401 xfs_bmap_init(args->flist, args->firstblock);
402 nmap = 1;
403 error = xfs_bmapi_read(dp, (xfs_fileoff_t)lblkno,
404 args->rmtblkcnt, &map, &nmap,
405 XFS_BMAPI_ATTRFORK);
406 if (error)
407 return(error);
408 ASSERT(nmap == 1);
409 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
410 (map.br_startblock != HOLESTARTBLOCK));
411
412 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
413 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
414
415 bp = xfs_buf_get(mp->m_ddev_targp, dblkno, blkcnt, 0);
416 if (!bp)
417 return ENOMEM;
418 bp->b_ops = &xfs_attr3_rmt_buf_ops;
419
420 byte_cnt = BBTOB(bp->b_length);
421 byte_cnt = XFS_ATTR3_RMT_BUF_SPACE(mp, byte_cnt);
422 if (valuelen < byte_cnt)
423 byte_cnt = valuelen;
424
425 buf = bp->b_addr;
426 buf += xfs_attr3_rmt_hdr_set(mp, dp->i_ino, offset,
427 byte_cnt, bp);
428 memcpy(buf, src, byte_cnt);
429
430 if (byte_cnt < BBTOB(bp->b_length))
431 xfs_buf_zero(bp, byte_cnt,
432 BBTOB(bp->b_length) - byte_cnt);
433
434 error = xfs_bwrite(bp); /* GROT: NOTE: synchronous write */
435 xfs_buf_relse(bp);
436 if (error)
437 return error;
438
439 src += byte_cnt;
440 valuelen -= byte_cnt;
441 offset += byte_cnt;
442 hdrcnt--;
443
444 lblkno += map.br_blockcount;
445 }
446 ASSERT(valuelen == 0);
447 return 0;
448 }
449
450 /*
451 * Remove the value associated with an attribute by deleting the
452 * out-of-line buffer that it is stored on.
453 */
454 int
455 xfs_attr_rmtval_remove(xfs_da_args_t *args)
456 {
457 xfs_mount_t *mp;
458 xfs_bmbt_irec_t map;
459 xfs_buf_t *bp;
460 xfs_daddr_t dblkno;
461 xfs_dablk_t lblkno;
462 int valuelen, blkcnt, nmap, error, done, committed;
463
464 trace_xfs_attr_rmtval_remove(args);
465
466 mp = args->dp->i_mount;
467
468 /*
469 * Roll through the "value", invalidating the attribute value's
470 * blocks.
471 */
472 lblkno = args->rmtblkno;
473 valuelen = args->rmtblkcnt;
474 while (valuelen > 0) {
475 /*
476 * Try to remember where we decided to put the value.
477 */
478 nmap = 1;
479 error = xfs_bmapi_read(args->dp, (xfs_fileoff_t)lblkno,
480 args->rmtblkcnt, &map, &nmap,
481 XFS_BMAPI_ATTRFORK);
482 if (error)
483 return(error);
484 ASSERT(nmap == 1);
485 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
486 (map.br_startblock != HOLESTARTBLOCK));
487
488 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
489 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
490
491 /*
492 * If the "remote" value is in the cache, remove it.
493 */
494 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt, XBF_TRYLOCK);
495 if (bp) {
496 xfs_buf_stale(bp);
497 xfs_buf_relse(bp);
498 bp = NULL;
499 }
500
501 valuelen -= map.br_blockcount;
502
503 lblkno += map.br_blockcount;
504 }
505
506 /*
507 * Keep de-allocating extents until the remote-value region is gone.
508 */
509 lblkno = args->rmtblkno;
510 blkcnt = args->rmtblkcnt;
511 done = 0;
512 while (!done) {
513 xfs_bmap_init(args->flist, args->firstblock);
514 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
515 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
516 1, args->firstblock, args->flist,
517 &done);
518 if (!error) {
519 error = xfs_bmap_finish(&args->trans, args->flist,
520 &committed);
521 }
522 if (error) {
523 ASSERT(committed);
524 args->trans = NULL;
525 xfs_bmap_cancel(args->flist);
526 return error;
527 }
528
529 /*
530 * bmap_finish() may have committed the last trans and started
531 * a new one. We need the inode to be in all transactions.
532 */
533 if (committed)
534 xfs_trans_ijoin(args->trans, args->dp, 0);
535
536 /*
537 * Close out trans and start the next one in the chain.
538 */
539 error = xfs_trans_roll(&args->trans, args->dp);
540 if (error)
541 return (error);
542 }
543 return(0);
544 }
545
This page took 0.073441 seconds and 5 git commands to generate.