Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 : * Copyright (c) 2013 Red Hat, Inc.
5 : * All Rights Reserved.
6 : */
7 : #include "xfs.h"
8 : #include "xfs_fs.h"
9 : #include "xfs_shared.h"
10 : #include "xfs_format.h"
11 : #include "xfs_log_format.h"
12 : #include "xfs_trans_resv.h"
13 : #include "xfs_sb.h"
14 : #include "xfs_mount.h"
15 : #include "xfs_da_format.h"
16 : #include "xfs_da_btree.h"
17 : #include "xfs_inode.h"
18 : #include "xfs_trans.h"
19 : #include "xfs_bmap_btree.h"
20 : #include "xfs_bmap.h"
21 : #include "xfs_attr_sf.h"
22 : #include "xfs_attr.h"
23 : #include "xfs_attr_remote.h"
24 : #include "xfs_attr_leaf.h"
25 : #include "xfs_error.h"
26 : #include "xfs_trace.h"
27 : #include "xfs_buf_item.h"
28 : #include "xfs_dir2.h"
29 : #include "xfs_log.h"
30 : #include "xfs_ag.h"
31 : #include "xfs_errortag.h"
32 : #include "xfs_health.h"
33 :
34 :
35 : /*
36 : * xfs_attr_leaf.c
37 : *
38 : * Routines to implement leaf blocks of attributes as Btrees of hashed names.
39 : */
40 :
41 : /*========================================================================
42 : * Function prototypes for the kernel.
43 : *========================================================================*/
44 :
45 : /*
46 : * Routines used for growing the Btree.
47 : */
48 : STATIC int xfs_attr3_leaf_create(struct xfs_da_args *args,
49 : xfs_dablk_t which_block, struct xfs_buf **bpp);
50 : STATIC int xfs_attr3_leaf_add_work(struct xfs_buf *leaf_buffer,
51 : struct xfs_attr3_icleaf_hdr *ichdr,
52 : struct xfs_da_args *args, int freemap_index);
53 : STATIC void xfs_attr3_leaf_compact(struct xfs_da_args *args,
54 : struct xfs_attr3_icleaf_hdr *ichdr,
55 : struct xfs_buf *leaf_buffer);
56 : STATIC void xfs_attr3_leaf_rebalance(xfs_da_state_t *state,
57 : xfs_da_state_blk_t *blk1,
58 : xfs_da_state_blk_t *blk2);
59 : STATIC int xfs_attr3_leaf_figure_balance(xfs_da_state_t *state,
60 : xfs_da_state_blk_t *leaf_blk_1,
61 : struct xfs_attr3_icleaf_hdr *ichdr1,
62 : xfs_da_state_blk_t *leaf_blk_2,
63 : struct xfs_attr3_icleaf_hdr *ichdr2,
64 : int *number_entries_in_blk1,
65 : int *number_usedbytes_in_blk1);
66 :
67 : /*
68 : * Utility routines.
69 : */
70 : STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args,
71 : struct xfs_attr_leafblock *src_leaf,
72 : struct xfs_attr3_icleaf_hdr *src_ichdr, int src_start,
73 : struct xfs_attr_leafblock *dst_leaf,
74 : struct xfs_attr3_icleaf_hdr *dst_ichdr, int dst_start,
75 : int move_count);
76 : STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
77 :
78 : /*
79 : * attr3 block 'firstused' conversion helpers.
80 : *
81 : * firstused refers to the offset of the first used byte of the nameval region
82 : * of an attr leaf block. The region starts at the tail of the block and expands
83 : * backwards towards the middle. As such, firstused is initialized to the block
84 : * size for an empty leaf block and is reduced from there.
85 : *
86 : * The attr3 block size is pegged to the fsb size and the maximum fsb is 64k.
87 : * The in-core firstused field is 32-bit and thus supports the maximum fsb size.
88 : * The on-disk field is only 16-bit, however, and overflows at 64k. Since this
89 : * only occurs at exactly 64k, we use zero as a magic on-disk value to represent
90 : * the attr block size. The following helpers manage the conversion between the
91 : * in-core and on-disk formats.
92 : */
93 :
94 : static void
95 2655771582 : xfs_attr3_leaf_firstused_from_disk(
96 : struct xfs_da_geometry *geo,
97 : struct xfs_attr3_icleaf_hdr *to,
98 : struct xfs_attr_leafblock *from)
99 : {
100 2655771582 : struct xfs_attr3_leaf_hdr *hdr3;
101 :
102 2655771582 : if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
103 2655771582 : hdr3 = (struct xfs_attr3_leaf_hdr *) from;
104 2655771582 : to->firstused = be16_to_cpu(hdr3->firstused);
105 : } else {
106 0 : to->firstused = be16_to_cpu(from->hdr.firstused);
107 : }
108 :
109 : /*
110 : * Convert from the magic fsb size value to actual blocksize. This
111 : * should only occur for empty blocks when the block size overflows
112 : * 16-bits.
113 : */
114 2655771582 : if (to->firstused == XFS_ATTR3_LEAF_NULLOFF) {
115 0 : ASSERT(!to->count && !to->usedbytes);
116 0 : ASSERT(geo->blksize > USHRT_MAX);
117 0 : to->firstused = geo->blksize;
118 : }
119 2655771582 : }
120 :
121 : static void
122 157759803 : xfs_attr3_leaf_firstused_to_disk(
123 : struct xfs_da_geometry *geo,
124 : struct xfs_attr_leafblock *to,
125 : struct xfs_attr3_icleaf_hdr *from)
126 : {
127 157759803 : struct xfs_attr3_leaf_hdr *hdr3;
128 157759803 : uint32_t firstused;
129 :
130 : /* magic value should only be seen on disk */
131 157759803 : ASSERT(from->firstused != XFS_ATTR3_LEAF_NULLOFF);
132 :
133 : /*
134 : * Scale down the 32-bit in-core firstused value to the 16-bit on-disk
135 : * value. This only overflows at the max supported value of 64k. Use the
136 : * magic on-disk value to represent block size in this case.
137 : */
138 157759803 : firstused = from->firstused;
139 157759803 : if (firstused > USHRT_MAX) {
140 0 : ASSERT(from->firstused == geo->blksize);
141 : firstused = XFS_ATTR3_LEAF_NULLOFF;
142 : }
143 :
144 157759803 : if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
145 157759803 : hdr3 = (struct xfs_attr3_leaf_hdr *) to;
146 157759803 : hdr3->firstused = cpu_to_be16(firstused);
147 : } else {
148 0 : to->hdr.firstused = cpu_to_be16(firstused);
149 : }
150 157759803 : }
151 :
152 : void
153 2655759687 : xfs_attr3_leaf_hdr_from_disk(
154 : struct xfs_da_geometry *geo,
155 : struct xfs_attr3_icleaf_hdr *to,
156 : struct xfs_attr_leafblock *from)
157 : {
158 2655759687 : int i;
159 :
160 2655759687 : ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
161 : from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
162 :
163 2655759687 : if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
164 2655759687 : struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)from;
165 :
166 2655759687 : to->forw = be32_to_cpu(hdr3->info.hdr.forw);
167 2655759687 : to->back = be32_to_cpu(hdr3->info.hdr.back);
168 2655759687 : to->magic = be16_to_cpu(hdr3->info.hdr.magic);
169 2655759687 : to->count = be16_to_cpu(hdr3->count);
170 2655759687 : to->usedbytes = be16_to_cpu(hdr3->usedbytes);
171 2655759687 : xfs_attr3_leaf_firstused_from_disk(geo, to, from);
172 2655832884 : to->holes = hdr3->holes;
173 :
174 10621086566 : for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
175 7965695639 : to->freemap[i].base = be16_to_cpu(hdr3->freemap[i].base);
176 7965262895 : to->freemap[i].size = be16_to_cpu(hdr3->freemap[i].size);
177 : }
178 : return;
179 : }
180 0 : to->forw = be32_to_cpu(from->hdr.info.forw);
181 0 : to->back = be32_to_cpu(from->hdr.info.back);
182 0 : to->magic = be16_to_cpu(from->hdr.info.magic);
183 0 : to->count = be16_to_cpu(from->hdr.count);
184 0 : to->usedbytes = be16_to_cpu(from->hdr.usedbytes);
185 0 : xfs_attr3_leaf_firstused_from_disk(geo, to, from);
186 0 : to->holes = from->hdr.holes;
187 :
188 0 : for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
189 0 : to->freemap[i].base = be16_to_cpu(from->hdr.freemap[i].base);
190 0 : to->freemap[i].size = be16_to_cpu(from->hdr.freemap[i].size);
191 : }
192 : }
193 :
194 : void
195 157797616 : xfs_attr3_leaf_hdr_to_disk(
196 : struct xfs_da_geometry *geo,
197 : struct xfs_attr_leafblock *to,
198 : struct xfs_attr3_icleaf_hdr *from)
199 : {
200 157797616 : int i;
201 :
202 157797616 : ASSERT(from->magic == XFS_ATTR_LEAF_MAGIC ||
203 : from->magic == XFS_ATTR3_LEAF_MAGIC);
204 :
205 157797616 : if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
206 157797616 : struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)to;
207 :
208 157797616 : hdr3->info.hdr.forw = cpu_to_be32(from->forw);
209 157797616 : hdr3->info.hdr.back = cpu_to_be32(from->back);
210 157797616 : hdr3->info.hdr.magic = cpu_to_be16(from->magic);
211 157797616 : hdr3->count = cpu_to_be16(from->count);
212 157797616 : hdr3->usedbytes = cpu_to_be16(from->usedbytes);
213 157797616 : xfs_attr3_leaf_firstused_to_disk(geo, to, from);
214 157786361 : hdr3->holes = from->holes;
215 157786361 : hdr3->pad1 = 0;
216 :
217 630446592 : for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
218 472889090 : hdr3->freemap[i].base = cpu_to_be16(from->freemap[i].base);
219 472795966 : hdr3->freemap[i].size = cpu_to_be16(from->freemap[i].size);
220 : }
221 : return;
222 : }
223 0 : to->hdr.info.forw = cpu_to_be32(from->forw);
224 0 : to->hdr.info.back = cpu_to_be32(from->back);
225 0 : to->hdr.info.magic = cpu_to_be16(from->magic);
226 0 : to->hdr.count = cpu_to_be16(from->count);
227 0 : to->hdr.usedbytes = cpu_to_be16(from->usedbytes);
228 0 : xfs_attr3_leaf_firstused_to_disk(geo, to, from);
229 0 : to->hdr.holes = from->holes;
230 0 : to->hdr.pad1 = 0;
231 :
232 0 : for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
233 0 : to->hdr.freemap[i].base = cpu_to_be16(from->freemap[i].base);
234 0 : to->hdr.freemap[i].size = cpu_to_be16(from->freemap[i].size);
235 : }
236 : }
237 :
238 : static xfs_failaddr_t
239 431228445 : xfs_attr3_leaf_verify_entry(
240 : struct xfs_mount *mp,
241 : char *buf_end,
242 : struct xfs_attr_leafblock *leaf,
243 : struct xfs_attr3_icleaf_hdr *leafhdr,
244 : struct xfs_attr_leaf_entry *ent,
245 : int idx,
246 : __u32 *last_hashval)
247 : {
248 431228445 : struct xfs_attr_leaf_name_local *lentry;
249 431228445 : struct xfs_attr_leaf_name_remote *rentry;
250 431228445 : char *name_end;
251 431228445 : unsigned int nameidx;
252 431228445 : unsigned int namesize;
253 431228445 : __u32 hashval;
254 :
255 : /* hash order check */
256 431228445 : hashval = be32_to_cpu(ent->hashval);
257 431228445 : if (hashval < *last_hashval)
258 0 : return __this_address;
259 431228445 : *last_hashval = hashval;
260 :
261 431228445 : nameidx = be16_to_cpu(ent->nameidx);
262 431228445 : if (nameidx < leafhdr->firstused || nameidx >= mp->m_attr_geo->blksize)
263 0 : return __this_address;
264 :
265 : /*
266 : * Check the name information. The namelen fields are u8 so we can't
267 : * possibly exceed the maximum name length of 255 bytes.
268 : */
269 431228445 : if (ent->flags & XFS_ATTR_LOCAL) {
270 431224401 : lentry = xfs_attr3_leaf_name_local(leaf, idx);
271 431224401 : namesize = xfs_attr_leaf_entsize_local(lentry->namelen,
272 431224401 : be16_to_cpu(lentry->valuelen));
273 431224401 : name_end = (char *)lentry + namesize;
274 431224401 : if (lentry->namelen == 0)
275 0 : return __this_address;
276 : } else {
277 4044 : rentry = xfs_attr3_leaf_name_remote(leaf, idx);
278 4044 : namesize = xfs_attr_leaf_entsize_remote(rentry->namelen);
279 4044 : name_end = (char *)rentry + namesize;
280 4044 : if (rentry->namelen == 0)
281 0 : return __this_address;
282 4044 : if (!(ent->flags & XFS_ATTR_INCOMPLETE) &&
283 4039 : rentry->valueblk == 0)
284 0 : return __this_address;
285 : }
286 :
287 431228445 : if (name_end > buf_end)
288 0 : return __this_address;
289 :
290 : return NULL;
291 : }
292 :
293 : /*
294 : * Validate an attribute leaf block.
295 : *
296 : * Empty leaf blocks can occur under the following circumstances:
297 : *
298 : * 1. setxattr adds a new extended attribute to a file;
299 : * 2. The file has zero existing attributes;
300 : * 3. The attribute is too large to fit in the attribute fork;
301 : * 4. The attribute is small enough to fit in a leaf block;
302 : * 5. A log flush occurs after committing the transaction that creates
303 : * the (empty) leaf block; and
304 : * 6. The filesystem goes down after the log flush but before the new
305 : * attribute can be committed to the leaf block.
306 : *
307 : * Hence we need to ensure that we don't fail the validation purely
308 : * because the leaf is empty.
309 : */
310 : static xfs_failaddr_t
311 23324918 : xfs_attr3_leaf_verify(
312 : struct xfs_buf *bp)
313 : {
314 23324918 : struct xfs_attr3_icleaf_hdr ichdr;
315 23324918 : struct xfs_mount *mp = bp->b_mount;
316 23324918 : struct xfs_attr_leafblock *leaf = bp->b_addr;
317 23324918 : struct xfs_attr_leaf_entry *entries;
318 23324918 : struct xfs_attr_leaf_entry *ent;
319 23324918 : char *buf_end;
320 23324918 : uint32_t end; /* must be 32bit - see below */
321 23324918 : __u32 last_hashval = 0;
322 23324918 : int i;
323 23324918 : xfs_failaddr_t fa;
324 :
325 23324918 : xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
326 :
327 23330507 : fa = xfs_da3_blkinfo_verify(bp, bp->b_addr);
328 23322580 : if (fa)
329 : return fa;
330 :
331 : /*
332 : * firstused is the block offset of the first name info structure.
333 : * Make sure it doesn't go off the block or crash into the header.
334 : */
335 23319618 : if (ichdr.firstused > mp->m_attr_geo->blksize)
336 0 : return __this_address;
337 46640198 : if (ichdr.firstused < xfs_attr3_leaf_hdr_size(leaf))
338 0 : return __this_address;
339 :
340 : /* Make sure the entries array doesn't crash into the name info. */
341 23319618 : entries = xfs_attr3_leaf_entryp(bp->b_addr);
342 23319618 : if ((char *)&entries[ichdr.count] >
343 23319618 : (char *)bp->b_addr + ichdr.firstused)
344 0 : return __this_address;
345 :
346 : /*
347 : * NOTE: This verifier historically failed empty leaf buffers because
348 : * we expect the fork to be in another format. Empty attr fork format
349 : * conversions are possible during xattr set, however, and format
350 : * conversion is not atomic with the xattr set that triggers it. We
351 : * cannot assume leaf blocks are non-empty until that is addressed.
352 : */
353 23319618 : buf_end = (char *)bp->b_addr + mp->m_attr_geo->blksize;
354 454540892 : for (i = 0, ent = entries; i < ichdr.count; ent++, i++) {
355 431206178 : fa = xfs_attr3_leaf_verify_entry(mp, buf_end, leaf, &ichdr,
356 : ent, i, &last_hashval);
357 431221274 : if (fa)
358 0 : return fa;
359 : }
360 :
361 : /*
362 : * Quickly check the freemap information. Attribute data has to be
363 : * aligned to 4-byte boundaries, and likewise for the free space.
364 : *
365 : * Note that for 64k block size filesystems, the freemap entries cannot
366 : * overflow as they are only be16 fields. However, when checking end
367 : * pointer of the freemap, we have to be careful to detect overflows and
368 : * so use uint32_t for those checks.
369 : */
370 93337324 : for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
371 70002503 : if (ichdr.freemap[i].base > mp->m_attr_geo->blksize)
372 0 : return __this_address;
373 70002709 : if (ichdr.freemap[i].base & 0x3)
374 0 : return __this_address;
375 70002644 : if (ichdr.freemap[i].size > mp->m_attr_geo->blksize)
376 0 : return __this_address;
377 70002680 : if (ichdr.freemap[i].size & 0x3)
378 0 : return __this_address;
379 :
380 : /* be care of 16 bit overflows here */
381 70002669 : end = (uint32_t)ichdr.freemap[i].base + ichdr.freemap[i].size;
382 70002721 : if (end < ichdr.freemap[i].base)
383 0 : return __this_address;
384 70002610 : if (end > mp->m_attr_geo->blksize)
385 0 : return __this_address;
386 : }
387 :
388 : return NULL;
389 : }
390 :
391 : xfs_failaddr_t
392 1424565815 : xfs_attr3_leaf_header_check(
393 : struct xfs_buf *bp,
394 : xfs_ino_t owner)
395 : {
396 1424565815 : struct xfs_mount *mp = bp->b_mount;
397 :
398 1424565815 : if (xfs_has_crc(mp)) {
399 1424565815 : struct xfs_attr3_leafblock *hdr3 = bp->b_addr;
400 :
401 1424565815 : ASSERT(hdr3->hdr.info.hdr.magic ==
402 : cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
403 :
404 1424565815 : if (be64_to_cpu(hdr3->hdr.info.owner) != owner)
405 0 : return __this_address;
406 : }
407 :
408 : return NULL;
409 : }
410 :
411 : static void
412 21170536 : xfs_attr3_leaf_write_verify(
413 : struct xfs_buf *bp)
414 : {
415 21170536 : struct xfs_mount *mp = bp->b_mount;
416 21170536 : struct xfs_buf_log_item *bip = bp->b_log_item;
417 21170536 : struct xfs_attr3_leaf_hdr *hdr3 = bp->b_addr;
418 21170536 : xfs_failaddr_t fa;
419 :
420 21170536 : fa = xfs_attr3_leaf_verify(bp);
421 21170536 : if (fa) {
422 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
423 0 : return;
424 : }
425 :
426 21170536 : if (!xfs_has_crc(mp))
427 : return;
428 :
429 21170536 : if (bip)
430 21170536 : hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
431 :
432 21170536 : xfs_buf_update_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF);
433 : }
434 :
435 : /*
436 : * leaf/node format detection on trees is sketchy, so a node read can be done on
437 : * leaf level blocks when detection identifies the tree as a node format tree
438 : * incorrectly. In this case, we need to swap the verifier to match the correct
439 : * format of the block being read.
440 : */
441 : static void
442 112219 : xfs_attr3_leaf_read_verify(
443 : struct xfs_buf *bp)
444 : {
445 112219 : struct xfs_mount *mp = bp->b_mount;
446 112219 : xfs_failaddr_t fa;
447 :
448 224438 : if (xfs_has_crc(mp) &&
449 : !xfs_buf_verify_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF))
450 22 : xfs_verifier_error(bp, -EFSBADCRC, __this_address);
451 : else {
452 112197 : fa = xfs_attr3_leaf_verify(bp);
453 112197 : if (fa)
454 0 : xfs_verifier_error(bp, -EFSCORRUPTED, fa);
455 : }
456 112219 : }
457 :
458 : const struct xfs_buf_ops xfs_attr3_leaf_buf_ops = {
459 : .name = "xfs_attr3_leaf",
460 : .magic16 = { cpu_to_be16(XFS_ATTR_LEAF_MAGIC),
461 : cpu_to_be16(XFS_ATTR3_LEAF_MAGIC) },
462 : .verify_read = xfs_attr3_leaf_read_verify,
463 : .verify_write = xfs_attr3_leaf_write_verify,
464 : .verify_struct = xfs_attr3_leaf_verify,
465 : };
466 :
467 : int
468 940206030 : xfs_attr3_leaf_read(
469 : struct xfs_trans *tp,
470 : struct xfs_inode *dp,
471 : xfs_ino_t owner,
472 : xfs_dablk_t bno,
473 : struct xfs_buf **bpp)
474 : {
475 940206030 : xfs_failaddr_t fa;
476 940206030 : int err;
477 :
478 940206030 : err = xfs_da_read_buf(tp, dp, bno, 0, bpp, XFS_ATTR_FORK,
479 : &xfs_attr3_leaf_buf_ops);
480 940459687 : if (err || !(*bpp))
481 : return err;
482 :
483 940459588 : fa = xfs_attr3_leaf_header_check(*bpp, owner);
484 940429914 : if (fa) {
485 0 : __xfs_buf_mark_corrupt(*bpp, fa);
486 0 : xfs_trans_brelse(tp, *bpp);
487 0 : *bpp = NULL;
488 0 : xfs_dirattr_mark_sick(dp, XFS_ATTR_FORK);
489 0 : return -EFSCORRUPTED;
490 : }
491 :
492 940429914 : if (tp)
493 396961786 : xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_ATTR_LEAF_BUF);
494 : return 0;
495 : }
496 :
497 : /*========================================================================
498 : * Namespace helper routines
499 : *========================================================================*/
500 :
501 : /*
502 : * If we are in log recovery, then we want the lookup to ignore the INCOMPLETE
503 : * flag on disk - if there's an incomplete attr then recovery needs to tear it
504 : * down. If there's no incomplete attr, then recovery needs to tear that attr
505 : * down to replace it with the attr that has been logged. In this case, the
506 : * INCOMPLETE flag will not be set in attr->attr_filter, but rather
507 : * XFS_DA_OP_RECOVERY will be set in args->op_flags.
508 : */
509 : static bool
510 4853872952 : xfs_attr_match(
511 : const struct xfs_da_args *args,
512 : uint8_t namelen,
513 : const unsigned char *name,
514 : unsigned int valuelen,
515 : const void *value,
516 : int flags)
517 : {
518 :
519 4853872952 : if (args->namelen != namelen)
520 : return false;
521 9196161218 : if (memcmp(args->name, name, namelen) != 0)
522 : return false;
523 :
524 495960212 : if (args->op_flags & XFS_DA_OP_NVLOOKUP) {
525 352010985 : if (args->valuelen != valuelen)
526 : return false;
527 351772664 : if (args->valuelen && !value) {
528 : /* not implemented for remote values */
529 0 : ASSERT(0);
530 0 : return false;
531 : }
532 351772664 : if (valuelen && !args->value) {
533 : /* caller gave us valuelen > 0 but no value?? */
534 0 : ASSERT(0);
535 0 : return false;
536 : }
537 703545328 : if (valuelen > 0 && memcmp(args->value, value, valuelen) != 0)
538 : return false;
539 : }
540 :
541 : /* Recovery ignores the INCOMPLETE flag. */
542 463201135 : if ((args->op_flags & XFS_DA_OP_RECOVERY) &&
543 281 : args->attr_filter == (flags & XFS_ATTR_NSP_ONDISK_MASK))
544 : return true;
545 :
546 : /* All remaining matches need to be filtered by INCOMPLETE state. */
547 463200931 : if (args->attr_filter !=
548 463200931 : (flags & (XFS_ATTR_NSP_ONDISK_MASK | XFS_ATTR_INCOMPLETE)))
549 364817 : return false;
550 : return true;
551 : }
552 :
553 : static int
554 213858925 : xfs_attr_copy_value(
555 : struct xfs_da_args *args,
556 : unsigned char *value,
557 : int valuelen)
558 : {
559 : /* vlookups already supplied the attr value; don't copy anything */
560 213858925 : if (args->op_flags & XFS_DA_OP_NVLOOKUP)
561 : return 0;
562 :
563 : /*
564 : * No copy if all we have to do is get the length
565 : */
566 13202487 : if (!args->valuelen) {
567 6632934 : args->valuelen = valuelen;
568 6632934 : return 0;
569 : }
570 :
571 : /*
572 : * No copy if the length of the existing buffer is too small
573 : */
574 6569553 : if (args->valuelen < valuelen) {
575 0 : args->valuelen = valuelen;
576 0 : return -ERANGE;
577 : }
578 :
579 6569553 : if (!args->value) {
580 1099 : args->value = kvmalloc(valuelen, GFP_KERNEL | __GFP_NOLOCKDEP);
581 1099 : if (!args->value)
582 : return -ENOMEM;
583 : }
584 6569553 : args->valuelen = valuelen;
585 :
586 : /* remote block xattr requires IO for copy-in */
587 6569553 : if (args->rmtblkno)
588 1158 : return xfs_attr_rmtval_get(args);
589 :
590 : /*
591 : * This is to prevent a GCC warning because the remote xattr case
592 : * doesn't have a value to pass in. In that case, we never reach here,
593 : * but GCC can't work that out and so throws a "passing NULL to
594 : * memcpy" warning.
595 : */
596 6568395 : if (!value)
597 : return -EINVAL;
598 13136790 : memcpy(args->value, value, valuelen);
599 6568395 : return 0;
600 : }
601 :
602 : /*========================================================================
603 : * External routines when attribute fork size < XFS_LITINO(mp).
604 : *========================================================================*/
605 :
606 : /*
607 : * Query whether the total requested number of attr fork bytes of extended
608 : * attribute space will be able to fit inline.
609 : *
610 : * Returns zero if not, else the i_forkoff fork offset to be used in the
611 : * literal area for attribute data once the new bytes have been added.
612 : *
613 : * i_forkoff must be 8 byte aligned, hence is stored as a >>3 value;
614 : * special case for dev/uuid inodes, they have fixed size data forks.
615 : */
616 : int
617 386708339 : xfs_attr_shortform_bytesfit(
618 : struct xfs_inode *dp,
619 : int bytes)
620 : {
621 386708339 : struct xfs_mount *mp = dp->i_mount;
622 386708339 : int64_t dsize;
623 386708339 : int minforkoff;
624 386708339 : int maxforkoff;
625 386708339 : int offset;
626 :
627 : /*
628 : * Check if the new size could fit at all first:
629 : */
630 386708394 : if (bytes > XFS_LITINO(mp))
631 : return 0;
632 :
633 : /* rounded down */
634 343377491 : offset = (XFS_LITINO(mp) - bytes) >> 3;
635 :
636 343377491 : if (dp->i_df.if_format == XFS_DINODE_FMT_DEV) {
637 42890761 : minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
638 42890761 : return (offset >= minforkoff) ? minforkoff : 0;
639 : }
640 :
641 : /*
642 : * If the requested numbers of bytes is smaller or equal to the
643 : * current attribute fork size we can always proceed.
644 : *
645 : * Note that if_bytes in the data fork might actually be larger than
646 : * the current data fork size is due to delalloc extents. In that
647 : * case either the extent count will go down when they are converted
648 : * to real extents, or the delalloc conversion will take care of the
649 : * literal area rebalancing.
650 : */
651 300486730 : if (bytes <= xfs_inode_attr_fork_size(dp))
652 277176879 : return dp->i_forkoff;
653 :
654 : /*
655 : * For attr2 we can try to move the forkoff if there is space in the
656 : * literal area, but for the old format we are done if there is no
657 : * space in the fixed attribute fork.
658 : */
659 23309851 : if (!xfs_has_attr2(mp))
660 : return 0;
661 :
662 23309851 : dsize = dp->i_df.if_bytes;
663 :
664 23309851 : switch (dp->i_df.if_format) {
665 22410112 : case XFS_DINODE_FMT_EXTENTS:
666 : /*
667 : * If there is no attr fork and the data fork is extents,
668 : * determine if creating the default attr fork will result
669 : * in the extents form migrating to btree. If so, the
670 : * minimum offset only needs to be the space required for
671 : * the btree root.
672 : */
673 22410112 : if (!dp->i_forkoff && dp->i_df.if_bytes >
674 4436018 : xfs_default_attroffset(dp))
675 3980 : dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
676 : break;
677 193956 : case XFS_DINODE_FMT_BTREE:
678 : /*
679 : * If we have a data btree then keep forkoff if we have one,
680 : * otherwise we are adding a new attr, so then we set
681 : * minforkoff to where the btree root can finish so we have
682 : * plenty of room for attrs
683 : */
684 193956 : if (dp->i_forkoff) {
685 192048 : if (offset < dp->i_forkoff)
686 : return 0;
687 0 : return dp->i_forkoff;
688 : }
689 1908 : dsize = XFS_BMAP_BROOT_SPACE(mp, dp->i_df.if_broot);
690 1908 : break;
691 : }
692 :
693 : /*
694 : * A data fork btree root must have space for at least
695 : * MINDBTPTRS key/ptr pairs if the data fork is small or empty.
696 : */
697 23117450 : minforkoff = max_t(int64_t, dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
698 23117450 : minforkoff = roundup(minforkoff, 8) >> 3;
699 :
700 : /* attr fork btree root can have at least this many key/ptr pairs */
701 23117450 : maxforkoff = XFS_LITINO(mp) - XFS_BMDR_SPACE_CALC(MINABTPTRS);
702 23117450 : maxforkoff = maxforkoff >> 3; /* rounded down */
703 :
704 23117450 : if (offset >= maxforkoff)
705 : return maxforkoff;
706 18972567 : if (offset >= minforkoff)
707 12335145 : return offset;
708 : return 0;
709 : }
710 :
711 : /*
712 : * Switch on the ATTR2 superblock bit (implies also FEATURES2) unless:
713 : * - noattr2 mount option is set,
714 : * - on-disk version bit says it is already set, or
715 : * - the attr2 mount option is not set to enable automatic upgrade from attr1.
716 : */
717 : STATIC void
718 339251061 : xfs_sbversion_add_attr2(
719 : struct xfs_mount *mp,
720 : struct xfs_trans *tp)
721 : {
722 339251061 : if (xfs_has_noattr2(mp))
723 : return;
724 339251061 : if (mp->m_sb.sb_features2 & XFS_SB_VERSION2_ATTR2BIT)
725 : return;
726 0 : if (!xfs_has_attr2(mp))
727 : return;
728 :
729 0 : spin_lock(&mp->m_sb_lock);
730 0 : xfs_add_attr2(mp);
731 0 : spin_unlock(&mp->m_sb_lock);
732 0 : xfs_log_sb(tp);
733 : }
734 :
735 : /*
736 : * Create the initial contents of a shortform attribute list.
737 : */
738 : void
739 101173740 : xfs_attr_shortform_create(
740 : struct xfs_da_args *args)
741 : {
742 101173740 : struct xfs_inode *dp = args->dp;
743 101173740 : struct xfs_ifork *ifp = &dp->i_af;
744 101173740 : struct xfs_attr_sf_hdr *hdr;
745 :
746 101173740 : trace_xfs_attr_sf_create(args);
747 :
748 101040707 : ASSERT(ifp->if_bytes == 0);
749 101040707 : if (ifp->if_format == XFS_DINODE_FMT_EXTENTS)
750 101044460 : ifp->if_format = XFS_DINODE_FMT_LOCAL;
751 101040707 : xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
752 101088037 : hdr = (struct xfs_attr_sf_hdr *)ifp->if_u1.if_data;
753 101088037 : memset(hdr, 0, sizeof(*hdr));
754 101088037 : hdr->totsize = cpu_to_be16(sizeof(*hdr));
755 101088037 : xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
756 101324032 : }
757 :
758 : /*
759 : * Return -EEXIST if attr is found, or -ENOATTR if not
760 : * args: args containing attribute name and namelen
761 : * sfep: If not null, pointer will be set to the last attr entry found on
762 : -EEXIST. On -ENOATTR pointer is left at the last entry in the list
763 : * basep: If not null, pointer is set to the byte offset of the entry in the
764 : * list on -EEXIST. On -ENOATTR, pointer is left at the byte offset of
765 : * the last entry in the list
766 : */
767 : int
768 397891465 : xfs_attr_sf_findname(
769 : struct xfs_da_args *args,
770 : struct xfs_attr_sf_entry **sfep,
771 : unsigned int *basep)
772 : {
773 397891465 : struct xfs_attr_shortform *sf;
774 397891465 : struct xfs_attr_sf_entry *sfe;
775 397891465 : unsigned int base = sizeof(struct xfs_attr_sf_hdr);
776 397891465 : int size = 0;
777 397891465 : int end;
778 397891465 : int i;
779 :
780 397891465 : sf = (struct xfs_attr_shortform *)args->dp->i_af.if_u1.if_data;
781 397891465 : sfe = &sf->list[0];
782 397891465 : end = sf->hdr.count;
783 759399651 : for (i = 0; i < end; sfe = xfs_attr_sf_nextentry(sfe),
784 361508186 : base += size, i++) {
785 506607229 : size = xfs_attr_sf_entsize(sfe);
786 506550859 : if (!xfs_attr_match(args, sfe->namelen, sfe->nameval,
787 506607229 : sfe->valuelen, &sfe->nameval[sfe->namelen],
788 506607229 : sfe->flags))
789 361508186 : continue;
790 : break;
791 : }
792 :
793 397835095 : if (sfep != NULL)
794 338948476 : *sfep = sfe;
795 :
796 397835095 : if (basep != NULL)
797 130381917 : *basep = base;
798 :
799 397835095 : if (i == end)
800 252750247 : return -ENOATTR;
801 : return -EEXIST;
802 : }
803 :
804 : /*
805 : * Add a name/value pair to the shortform attribute list.
806 : * Overflow from the inode has already been checked for.
807 : */
808 : void
809 208609014 : xfs_attr_shortform_add(
810 : struct xfs_da_args *args,
811 : int forkoff)
812 : {
813 208609014 : struct xfs_attr_shortform *sf;
814 208609014 : struct xfs_attr_sf_entry *sfe;
815 208609014 : int offset, size;
816 208609014 : struct xfs_mount *mp;
817 208609014 : struct xfs_inode *dp;
818 208609014 : struct xfs_ifork *ifp;
819 :
820 208609014 : trace_xfs_attr_sf_add(args);
821 :
822 208529356 : dp = args->dp;
823 208529356 : mp = dp->i_mount;
824 208529356 : dp->i_forkoff = forkoff;
825 :
826 208529356 : ifp = &dp->i_af;
827 208529356 : ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
828 208529356 : sf = (struct xfs_attr_shortform *)ifp->if_u1.if_data;
829 208529356 : if (xfs_attr_sf_findname(args, &sfe, NULL) == -EEXIST)
830 0 : ASSERT(0);
831 :
832 208585342 : offset = (char *)sfe - (char *)sf;
833 208585342 : size = xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
834 208585342 : xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
835 208768265 : sf = (struct xfs_attr_shortform *)ifp->if_u1.if_data;
836 208768265 : sfe = (struct xfs_attr_sf_entry *)((char *)sf + offset);
837 :
838 208768265 : sfe->namelen = args->namelen;
839 208768265 : sfe->valuelen = args->valuelen;
840 208768265 : sfe->flags = args->attr_filter;
841 417536530 : memcpy(sfe->nameval, args->name, args->namelen);
842 417536530 : memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
843 208768265 : sf->hdr.count++;
844 208768265 : be16_add_cpu(&sf->hdr.totsize, size);
845 208768265 : xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
846 :
847 208882141 : xfs_sbversion_add_attr2(mp, args->trans);
848 208790092 : }
849 :
850 : /*
851 : * After the last attribute is removed revert to original inode format,
852 : * making all literal area available to the data fork once more.
853 : */
854 : void
855 66034903 : xfs_attr_fork_remove(
856 : struct xfs_inode *ip,
857 : struct xfs_trans *tp)
858 : {
859 66034903 : ASSERT(ip->i_af.if_nextents == 0);
860 :
861 66034903 : xfs_ifork_zap_attr(ip);
862 66052033 : ip->i_forkoff = 0;
863 66052033 : xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
864 66126647 : }
865 :
866 : /*
867 : * Remove an attribute from the shortform attribute list structure.
868 : */
869 : int
870 130399328 : xfs_attr_sf_removename(
871 : struct xfs_da_args *args)
872 : {
873 130399328 : struct xfs_attr_shortform *sf;
874 130399328 : struct xfs_attr_sf_entry *sfe;
875 130399328 : int size = 0, end, totsize;
876 130399328 : unsigned int base;
877 130399328 : struct xfs_mount *mp;
878 130399328 : struct xfs_inode *dp;
879 130399328 : int error;
880 :
881 130399328 : trace_xfs_attr_sf_remove(args);
882 :
883 130376394 : dp = args->dp;
884 130376394 : mp = dp->i_mount;
885 130376394 : sf = (struct xfs_attr_shortform *)dp->i_af.if_u1.if_data;
886 :
887 130376394 : error = xfs_attr_sf_findname(args, &sfe, &base);
888 :
889 : /*
890 : * If we are recovering an operation, finding nothing to
891 : * remove is not an error - it just means there was nothing
892 : * to clean up.
893 : */
894 130392513 : if (error == -ENOATTR && (args->op_flags & XFS_DA_OP_RECOVERY))
895 : return 0;
896 130392467 : if (error != -EEXIST)
897 : return error;
898 130392467 : size = xfs_attr_sf_entsize(sfe);
899 :
900 : /*
901 : * Fix up the attribute fork data, covering the hole
902 : */
903 130392467 : end = base + size;
904 130392467 : totsize = be16_to_cpu(sf->hdr.totsize);
905 130392467 : if (end != totsize)
906 46968594 : memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
907 130392467 : sf->hdr.count--;
908 130392467 : be16_add_cpu(&sf->hdr.totsize, -size);
909 :
910 : /*
911 : * Fix up the start offset of the attribute fork
912 : */
913 130392467 : totsize -= size;
914 130392467 : if (totsize == sizeof(xfs_attr_sf_hdr_t) && xfs_has_attr2(mp) &&
915 91881729 : (dp->i_df.if_format != XFS_DINODE_FMT_BTREE) &&
916 91535862 : !(args->op_flags & (XFS_DA_OP_ADDNAME | XFS_DA_OP_REPLACE)) &&
917 : !xfs_has_parent(mp)) {
918 26485 : xfs_attr_fork_remove(dp, args->trans);
919 : } else {
920 130365982 : xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
921 130379750 : dp->i_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
922 130358926 : ASSERT(dp->i_forkoff);
923 130358926 : ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
924 : (args->op_flags & XFS_DA_OP_ADDNAME) ||
925 : !xfs_has_attr2(mp) ||
926 : dp->i_df.if_format == XFS_DINODE_FMT_BTREE ||
927 : xfs_has_parent(mp));
928 130358926 : xfs_trans_log_inode(args->trans, dp,
929 : XFS_ILOG_CORE | XFS_ILOG_ADATA);
930 : }
931 :
932 130425366 : xfs_sbversion_add_attr2(mp, args->trans);
933 :
934 130425366 : return 0;
935 : }
936 :
937 : /*
938 : * Look up a name in a shortform attribute list structure.
939 : */
940 : /*ARGSUSED*/
941 : int
942 199791600 : xfs_attr_shortform_lookup(xfs_da_args_t *args)
943 : {
944 199791600 : struct xfs_attr_shortform *sf;
945 199791600 : struct xfs_attr_sf_entry *sfe;
946 199791600 : int i;
947 199791600 : struct xfs_ifork *ifp;
948 :
949 199791600 : trace_xfs_attr_sf_lookup(args);
950 :
951 199740587 : ifp = &args->dp->i_af;
952 199740587 : ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
953 199740587 : sf = (struct xfs_attr_shortform *)ifp->if_u1.if_data;
954 199740587 : sfe = &sf->list[0];
955 352407940 : for (i = 0; i < sf->hdr.count;
956 152667353 : sfe = xfs_attr_sf_nextentry(sfe), i++) {
957 159966772 : if (xfs_attr_match(args, sfe->namelen, sfe->nameval,
958 159928645 : sfe->valuelen, &sfe->nameval[sfe->namelen],
959 159928645 : sfe->flags))
960 : return -EEXIST;
961 : }
962 : return -ENOATTR;
963 : }
964 :
965 : /*
966 : * Retrieve the attribute value and length.
967 : *
968 : * If args->valuelen is zero, only the length needs to be returned. Unlike a
969 : * lookup, we only return an error if the attribute does not exist or we can't
970 : * retrieve the value.
971 : */
972 : int
973 251406272 : xfs_attr_shortform_getvalue(
974 : struct xfs_da_args *args)
975 : {
976 251406272 : struct xfs_attr_shortform *sf;
977 251406272 : struct xfs_attr_sf_entry *sfe;
978 251406272 : int i;
979 :
980 251406272 : ASSERT(args->dp->i_af.if_format == XFS_DINODE_FMT_LOCAL);
981 251406272 : sf = (struct xfs_attr_shortform *)args->dp->i_af.if_u1.if_data;
982 251406272 : sfe = &sf->list[0];
983 337215917 : for (i = 0; i < sf->hdr.count;
984 85809645 : sfe = xfs_attr_sf_nextentry(sfe), i++) {
985 288220462 : if (xfs_attr_match(args, sfe->namelen, sfe->nameval,
986 288469422 : sfe->valuelen, &sfe->nameval[sfe->namelen],
987 288469422 : sfe->flags))
988 202410817 : return xfs_attr_copy_value(args,
989 202410817 : &sfe->nameval[args->namelen], sfe->valuelen);
990 : }
991 : return -ENOATTR;
992 : }
993 :
994 : /* Convert from using the shortform to the leaf format. */
995 : int
996 4682769 : xfs_attr_shortform_to_leaf(
997 : struct xfs_da_args *args)
998 : {
999 4682769 : struct xfs_inode *dp;
1000 4682769 : struct xfs_attr_shortform *sf;
1001 4682769 : struct xfs_attr_sf_entry *sfe;
1002 4682769 : struct xfs_da_args nargs;
1003 4682769 : char *tmpbuffer;
1004 4682769 : int error, i, size;
1005 4682769 : xfs_dablk_t blkno;
1006 4682769 : struct xfs_buf *bp;
1007 4682769 : struct xfs_ifork *ifp;
1008 :
1009 4682769 : trace_xfs_attr_sf_to_leaf(args);
1010 :
1011 4682534 : dp = args->dp;
1012 4682534 : ifp = &dp->i_af;
1013 4682534 : sf = (struct xfs_attr_shortform *)ifp->if_u1.if_data;
1014 4682534 : size = be16_to_cpu(sf->hdr.totsize);
1015 4682534 : tmpbuffer = kmem_alloc(size, 0);
1016 4682978 : ASSERT(tmpbuffer != NULL);
1017 9365956 : memcpy(tmpbuffer, ifp->if_u1.if_data, size);
1018 4682978 : sf = (struct xfs_attr_shortform *)tmpbuffer;
1019 :
1020 4682978 : xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
1021 4683000 : xfs_bmap_local_to_extents_empty(args->trans, dp, XFS_ATTR_FORK);
1022 :
1023 4683043 : bp = NULL;
1024 4683043 : error = xfs_da_grow_inode(args, &blkno);
1025 4681258 : if (error)
1026 7 : goto out;
1027 :
1028 4681251 : ASSERT(blkno == 0);
1029 4681251 : error = xfs_attr3_leaf_create(args, blkno, &bp);
1030 4679064 : if (error)
1031 0 : goto out;
1032 :
1033 4679064 : memset((char *)&nargs, 0, sizeof(nargs));
1034 4679064 : nargs.dp = dp;
1035 4679064 : nargs.geo = args->geo;
1036 4679064 : nargs.total = args->total;
1037 4679064 : nargs.whichfork = XFS_ATTR_FORK;
1038 4679064 : nargs.trans = args->trans;
1039 4679064 : nargs.op_flags = XFS_DA_OP_OKNOENT | XFS_DA_OP_NVLOOKUP;
1040 4679064 : nargs.owner = args->owner;
1041 :
1042 4679064 : sfe = &sf->list[0];
1043 28979108 : for (i = 0; i < sf->hdr.count; i++) {
1044 24296016 : nargs.name = sfe->nameval;
1045 24296016 : nargs.namelen = sfe->namelen;
1046 24296016 : nargs.value = &sfe->nameval[nargs.namelen];
1047 24296016 : nargs.valuelen = sfe->valuelen;
1048 48589962 : nargs.hashval = xfs_da_hashname(sfe->nameval,
1049 24296016 : sfe->namelen);
1050 24293946 : nargs.attr_filter = sfe->flags & XFS_ATTR_NSP_ONDISK_MASK;
1051 24293946 : error = xfs_attr3_leaf_lookup_int(bp, &nargs); /* set a->index */
1052 24287533 : ASSERT(error == -ENOATTR);
1053 24287533 : error = xfs_attr3_leaf_add(bp, &nargs);
1054 24300044 : ASSERT(error != -ENOSPC);
1055 24300044 : if (error)
1056 0 : goto out;
1057 24300044 : sfe = xfs_attr_sf_nextentry(sfe);
1058 : }
1059 : error = 0;
1060 4683099 : out:
1061 4683099 : kmem_free(tmpbuffer);
1062 4682711 : return error;
1063 : }
1064 :
1065 : /*
1066 : * Check a leaf attribute block to see if all the entries would fit into
1067 : * a shortform attribute list.
1068 : */
1069 : int
1070 47954179 : xfs_attr_shortform_allfit(
1071 : struct xfs_buf *bp,
1072 : struct xfs_inode *dp)
1073 : {
1074 47954179 : struct xfs_attr_leafblock *leaf;
1075 47954179 : struct xfs_attr_leaf_entry *entry;
1076 47954179 : xfs_attr_leaf_name_local_t *name_loc;
1077 47954179 : struct xfs_attr3_icleaf_hdr leafhdr;
1078 47954179 : int bytes;
1079 47954179 : int i;
1080 47954179 : struct xfs_mount *mp = bp->b_mount;
1081 :
1082 47954179 : leaf = bp->b_addr;
1083 47954179 : xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
1084 47974428 : entry = xfs_attr3_leaf_entryp(leaf);
1085 :
1086 47974428 : bytes = sizeof(struct xfs_attr_sf_hdr);
1087 906599328 : for (i = 0; i < leafhdr.count; entry++, i++) {
1088 858630964 : if (entry->flags & XFS_ATTR_INCOMPLETE)
1089 0 : continue; /* don't copy partial entries */
1090 858630964 : if (!(entry->flags & XFS_ATTR_LOCAL))
1091 : return 0;
1092 858630964 : name_loc = xfs_attr3_leaf_name_local(leaf, i);
1093 858630964 : if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
1094 : return 0;
1095 858630964 : if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
1096 : return 0;
1097 858624900 : bytes += xfs_attr_sf_entsize_byname(name_loc->namelen,
1098 : be16_to_cpu(name_loc->valuelen));
1099 : }
1100 47968364 : if (xfs_has_attr2(dp->i_mount) &&
1101 47968364 : (dp->i_df.if_format != XFS_DINODE_FMT_BTREE) &&
1102 : (bytes == sizeof(struct xfs_attr_sf_hdr)))
1103 : return -1;
1104 47968247 : return xfs_attr_shortform_bytesfit(dp, bytes);
1105 : }
1106 :
1107 : /* Verify the consistency of a raw inline attribute fork. */
1108 : xfs_failaddr_t
1109 609831646 : xfs_attr_shortform_verify_struct(
1110 : struct xfs_attr_shortform *sfp,
1111 : size_t size)
1112 : {
1113 609831646 : struct xfs_attr_sf_entry *sfep;
1114 609831646 : struct xfs_attr_sf_entry *next_sfep;
1115 609831646 : char *endp;
1116 609831646 : int i;
1117 :
1118 : /*
1119 : * Give up if the attribute is way too short.
1120 : */
1121 609831646 : if (size < sizeof(struct xfs_attr_sf_hdr))
1122 0 : return __this_address;
1123 :
1124 609831646 : endp = (char *)sfp + size;
1125 :
1126 : /* Check all reported entries */
1127 609831646 : sfep = &sfp->list[0];
1128 1382228694 : for (i = 0; i < sfp->hdr.count; i++) {
1129 : /*
1130 : * struct xfs_attr_sf_entry has a variable length.
1131 : * Check the fixed-offset parts of the structure are
1132 : * within the data buffer.
1133 : * xfs_attr_sf_entry is defined with a 1-byte variable
1134 : * array at the end, so we must subtract that off.
1135 : */
1136 772397048 : if (((char *)sfep + sizeof(*sfep)) >= endp)
1137 0 : return __this_address;
1138 :
1139 : /* Don't allow names with known bad length. */
1140 772397048 : if (sfep->namelen == 0)
1141 0 : return __this_address;
1142 :
1143 : /*
1144 : * Check that the variable-length part of the structure is
1145 : * within the data buffer. The next entry starts after the
1146 : * name component, so nextentry is an acceptable test.
1147 : */
1148 772397048 : next_sfep = xfs_attr_sf_nextentry(sfep);
1149 772397048 : if ((char *)next_sfep > endp)
1150 0 : return __this_address;
1151 :
1152 : /*
1153 : * Check for unknown flags. Short form doesn't support
1154 : * the incomplete or local bits, so we can use the namespace
1155 : * mask here.
1156 : */
1157 772397048 : if (sfep->flags & ~XFS_ATTR_NSP_ONDISK_MASK)
1158 0 : return __this_address;
1159 :
1160 : /*
1161 : * Check for invalid namespace combinations. We only allow
1162 : * one namespace flag per xattr, so we can just count the
1163 : * bits (i.e. hweight) here.
1164 : */
1165 772397048 : if (hweight8(sfep->flags & XFS_ATTR_NSP_ONDISK_MASK) > 1)
1166 0 : return __this_address;
1167 :
1168 772397048 : sfep = next_sfep;
1169 : }
1170 609831646 : if ((void *)sfep != (void *)endp)
1171 0 : return __this_address;
1172 :
1173 : return NULL;
1174 : }
1175 :
1176 : /* Verify the consistency of an inline attribute fork. */
1177 : xfs_failaddr_t
1178 609835796 : xfs_attr_shortform_verify(
1179 : struct xfs_inode *ip)
1180 : {
1181 609835796 : struct xfs_attr_shortform *sfp;
1182 609835796 : struct xfs_ifork *ifp;
1183 609835796 : int64_t size;
1184 :
1185 609835796 : ASSERT(ip->i_af.if_format == XFS_DINODE_FMT_LOCAL);
1186 609835796 : ifp = xfs_ifork_ptr(ip, XFS_ATTR_FORK);
1187 609835796 : sfp = (struct xfs_attr_shortform *)ifp->if_u1.if_data;
1188 609835796 : size = ifp->if_bytes;
1189 :
1190 609835796 : return xfs_attr_shortform_verify_struct(sfp, size);
1191 : }
1192 :
1193 : /*
1194 : * Convert a leaf attribute list to shortform attribute list
1195 : */
1196 : int
1197 2470446 : xfs_attr3_leaf_to_shortform(
1198 : struct xfs_buf *bp,
1199 : struct xfs_da_args *args,
1200 : int forkoff)
1201 : {
1202 2470446 : struct xfs_attr_leafblock *leaf;
1203 2470446 : struct xfs_attr3_icleaf_hdr ichdr;
1204 2470446 : struct xfs_attr_leaf_entry *entry;
1205 2470446 : struct xfs_attr_leaf_name_local *name_loc;
1206 2470446 : struct xfs_da_args nargs;
1207 2470446 : struct xfs_inode *dp = args->dp;
1208 2470446 : char *tmpbuffer;
1209 2470446 : int error;
1210 2470446 : int i;
1211 :
1212 2470446 : trace_xfs_attr_leaf_to_sf(args);
1213 :
1214 2470321 : tmpbuffer = kmem_alloc(args->geo->blksize, 0);
1215 2470584 : if (!tmpbuffer)
1216 : return -ENOMEM;
1217 :
1218 4941168 : memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
1219 :
1220 2470584 : leaf = (xfs_attr_leafblock_t *)tmpbuffer;
1221 2470584 : xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
1222 2470584 : entry = xfs_attr3_leaf_entryp(leaf);
1223 :
1224 : /* XXX (dgc): buffer is about to be marked stale - why zero it? */
1225 2470584 : memset(bp->b_addr, 0, args->geo->blksize);
1226 :
1227 : /*
1228 : * Clean out the prior contents of the attribute list.
1229 : */
1230 2470584 : error = xfs_da_shrink_inode(args, 0, bp);
1231 2470537 : if (error)
1232 0 : goto out;
1233 :
1234 2470537 : if (forkoff == -1) {
1235 : /*
1236 : * Don't remove the attr fork if this operation is the first
1237 : * part of a attr replace operations. We're going to add a new
1238 : * attr immediately, so we need to keep the attr fork around in
1239 : * this case.
1240 : */
1241 117 : if (!(args->op_flags & XFS_DA_OP_REPLACE)) {
1242 85 : ASSERT(xfs_has_attr2(dp->i_mount));
1243 85 : ASSERT(dp->i_df.if_format != XFS_DINODE_FMT_BTREE);
1244 85 : xfs_attr_fork_remove(dp, args->trans);
1245 : }
1246 117 : goto out;
1247 : }
1248 :
1249 2470420 : xfs_attr_shortform_create(args);
1250 :
1251 : /*
1252 : * Copy the attributes
1253 : */
1254 2470394 : memset((char *)&nargs, 0, sizeof(nargs));
1255 2470394 : nargs.geo = args->geo;
1256 2470394 : nargs.dp = dp;
1257 2470394 : nargs.total = args->total;
1258 2470394 : nargs.whichfork = XFS_ATTR_FORK;
1259 2470394 : nargs.trans = args->trans;
1260 2470394 : nargs.op_flags = XFS_DA_OP_OKNOENT | XFS_DA_OP_NVLOOKUP;
1261 2470394 : nargs.owner = args->owner;
1262 :
1263 16136045 : for (i = 0; i < ichdr.count; entry++, i++) {
1264 13665517 : if (entry->flags & XFS_ATTR_INCOMPLETE)
1265 0 : continue; /* don't copy partial entries */
1266 13665517 : if (!entry->nameidx)
1267 0 : continue;
1268 13665517 : ASSERT(entry->flags & XFS_ATTR_LOCAL);
1269 13665517 : name_loc = xfs_attr3_leaf_name_local(leaf, i);
1270 13665517 : nargs.name = name_loc->nameval;
1271 13665517 : nargs.namelen = name_loc->namelen;
1272 13665517 : nargs.value = &name_loc->nameval[nargs.namelen];
1273 13665517 : nargs.valuelen = be16_to_cpu(name_loc->valuelen);
1274 13665517 : nargs.hashval = be32_to_cpu(entry->hashval);
1275 13665517 : nargs.attr_filter = entry->flags & XFS_ATTR_NSP_ONDISK_MASK;
1276 13665517 : xfs_attr_shortform_add(&nargs, forkoff);
1277 : }
1278 : error = 0;
1279 :
1280 2470645 : out:
1281 2470645 : kmem_free(tmpbuffer);
1282 2470645 : return error;
1283 : }
1284 :
1285 : /*
1286 : * Convert from using a single leaf to a root node and a leaf.
1287 : */
1288 : int
1289 41036 : xfs_attr3_leaf_to_node(
1290 : struct xfs_da_args *args)
1291 : {
1292 41036 : struct xfs_attr_leafblock *leaf;
1293 41036 : struct xfs_attr3_icleaf_hdr icleafhdr;
1294 41036 : struct xfs_attr_leaf_entry *entries;
1295 41036 : struct xfs_da3_icnode_hdr icnodehdr;
1296 41036 : struct xfs_da_intnode *node;
1297 41036 : struct xfs_inode *dp = args->dp;
1298 41036 : struct xfs_mount *mp = dp->i_mount;
1299 41036 : struct xfs_buf *bp1 = NULL;
1300 41036 : struct xfs_buf *bp2 = NULL;
1301 41036 : xfs_dablk_t blkno;
1302 41036 : int error;
1303 :
1304 41036 : trace_xfs_attr_leaf_to_node(args);
1305 :
1306 41036 : if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_ATTR_LEAF_TO_NODE)) {
1307 11 : error = -EIO;
1308 11 : goto out;
1309 : }
1310 :
1311 41025 : error = xfs_da_grow_inode(args, &blkno);
1312 41025 : if (error)
1313 0 : goto out;
1314 41025 : error = xfs_attr3_leaf_read(args->trans, dp, args->owner, 0, &bp1);
1315 41024 : if (error)
1316 0 : goto out;
1317 :
1318 41024 : error = xfs_da_get_buf(args->trans, dp, blkno, &bp2, XFS_ATTR_FORK);
1319 41024 : if (error)
1320 0 : goto out;
1321 :
1322 : /* copy leaf to new buffer, update identifiers */
1323 41024 : xfs_trans_buf_set_type(args->trans, bp2, XFS_BLFT_ATTR_LEAF_BUF);
1324 41024 : bp2->b_ops = bp1->b_ops;
1325 82048 : memcpy(bp2->b_addr, bp1->b_addr, args->geo->blksize);
1326 41024 : if (xfs_has_crc(mp)) {
1327 41025 : struct xfs_da3_blkinfo *hdr3 = bp2->b_addr;
1328 41025 : hdr3->blkno = cpu_to_be64(xfs_buf_daddr(bp2));
1329 : }
1330 41024 : xfs_trans_log_buf(args->trans, bp2, 0, args->geo->blksize - 1);
1331 :
1332 : /*
1333 : * Set up the new root node.
1334 : */
1335 41025 : error = xfs_da3_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
1336 41024 : if (error)
1337 0 : goto out;
1338 41024 : node = bp1->b_addr;
1339 41024 : xfs_da3_node_hdr_from_disk(mp, &icnodehdr, node);
1340 :
1341 41024 : leaf = bp2->b_addr;
1342 41024 : xfs_attr3_leaf_hdr_from_disk(args->geo, &icleafhdr, leaf);
1343 41024 : entries = xfs_attr3_leaf_entryp(leaf);
1344 :
1345 : /* both on-disk, don't endian-flip twice */
1346 41024 : icnodehdr.btree[0].hashval = entries[icleafhdr.count - 1].hashval;
1347 41024 : icnodehdr.btree[0].before = cpu_to_be32(blkno);
1348 41024 : icnodehdr.count = 1;
1349 41024 : xfs_da3_node_hdr_to_disk(dp->i_mount, node, &icnodehdr);
1350 41025 : xfs_trans_log_buf(args->trans, bp1, 0, args->geo->blksize - 1);
1351 41025 : error = 0;
1352 41036 : out:
1353 41036 : return error;
1354 : }
1355 :
1356 : /*========================================================================
1357 : * Routines used for growing the Btree.
1358 : *========================================================================*/
1359 :
1360 : /*
1361 : * Create the initial contents of a leaf attribute list
1362 : * or a leaf in a node attribute list.
1363 : */
1364 : STATIC int
1365 4823052 : xfs_attr3_leaf_create(
1366 : struct xfs_da_args *args,
1367 : xfs_dablk_t blkno,
1368 : struct xfs_buf **bpp)
1369 : {
1370 4823052 : struct xfs_attr_leafblock *leaf;
1371 4823052 : struct xfs_attr3_icleaf_hdr ichdr;
1372 4823052 : struct xfs_inode *dp = args->dp;
1373 4823052 : struct xfs_mount *mp = dp->i_mount;
1374 4823052 : struct xfs_buf *bp;
1375 4823052 : int error;
1376 :
1377 4823052 : trace_xfs_attr_leaf_create(args);
1378 :
1379 4820280 : error = xfs_da_get_buf(args->trans, args->dp, blkno, &bp,
1380 : XFS_ATTR_FORK);
1381 4823626 : if (error)
1382 : return error;
1383 4822459 : bp->b_ops = &xfs_attr3_leaf_buf_ops;
1384 4822459 : xfs_trans_buf_set_type(args->trans, bp, XFS_BLFT_ATTR_LEAF_BUF);
1385 4823586 : leaf = bp->b_addr;
1386 4823586 : memset(leaf, 0, args->geo->blksize);
1387 :
1388 4823586 : memset(&ichdr, 0, sizeof(ichdr));
1389 4823586 : ichdr.firstused = args->geo->blksize;
1390 :
1391 4823586 : if (xfs_has_crc(mp)) {
1392 4823586 : struct xfs_da3_blkinfo *hdr3 = bp->b_addr;
1393 :
1394 4823586 : ichdr.magic = XFS_ATTR3_LEAF_MAGIC;
1395 :
1396 4823586 : hdr3->blkno = cpu_to_be64(xfs_buf_daddr(bp));
1397 4823586 : hdr3->owner = cpu_to_be64(args->owner);
1398 4823586 : uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
1399 :
1400 4820295 : ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr);
1401 : } else {
1402 0 : ichdr.magic = XFS_ATTR_LEAF_MAGIC;
1403 0 : ichdr.freemap[0].base = sizeof(struct xfs_attr_leaf_hdr);
1404 : }
1405 4820295 : ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base;
1406 :
1407 4820295 : xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
1408 4818683 : xfs_trans_log_buf(args->trans, bp, 0, args->geo->blksize - 1);
1409 :
1410 4822445 : *bpp = bp;
1411 4822445 : return 0;
1412 : }
1413 :
1414 : /*
1415 : * Split the leaf node, rebalance, then add the new entry.
1416 : */
1417 : int
1418 143317 : xfs_attr3_leaf_split(
1419 : struct xfs_da_state *state,
1420 : struct xfs_da_state_blk *oldblk,
1421 : struct xfs_da_state_blk *newblk)
1422 : {
1423 143317 : xfs_dablk_t blkno;
1424 143317 : int error;
1425 :
1426 143317 : trace_xfs_attr_leaf_split(state->args);
1427 :
1428 : /*
1429 : * Allocate space for a new leaf node.
1430 : */
1431 143316 : ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
1432 143316 : error = xfs_da_grow_inode(state->args, &blkno);
1433 143318 : if (error)
1434 : return error;
1435 143319 : error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp);
1436 143319 : if (error)
1437 : return error;
1438 143319 : newblk->blkno = blkno;
1439 143319 : newblk->magic = XFS_ATTR_LEAF_MAGIC;
1440 :
1441 : /*
1442 : * Rebalance the entries across the two leaves.
1443 : * NOTE: rebalance() currently depends on the 2nd block being empty.
1444 : */
1445 143319 : xfs_attr3_leaf_rebalance(state, oldblk, newblk);
1446 143319 : error = xfs_da3_blk_link(state, oldblk, newblk);
1447 143319 : if (error)
1448 : return error;
1449 :
1450 : /*
1451 : * Save info on "old" attribute for "atomic rename" ops, leaf_add()
1452 : * modifies the index/blkno/rmtblk/rmtblkcnt fields to show the
1453 : * "new" attrs info. Will need the "old" info to remove it later.
1454 : *
1455 : * Insert the "new" entry in the correct block.
1456 : */
1457 143319 : if (state->inleaf) {
1458 22935 : trace_xfs_attr_leaf_add_old(state->args);
1459 22935 : error = xfs_attr3_leaf_add(oldblk->bp, state->args);
1460 : } else {
1461 120384 : trace_xfs_attr_leaf_add_new(state->args);
1462 120383 : error = xfs_attr3_leaf_add(newblk->bp, state->args);
1463 : }
1464 :
1465 : /*
1466 : * Update last hashval in each block since we added the name.
1467 : */
1468 143319 : oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1469 143319 : newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1470 143319 : return error;
1471 : }
1472 :
1473 : /*
1474 : * Add a name to the leaf attribute list structure.
1475 : */
1476 : int
1477 103645527 : xfs_attr3_leaf_add(
1478 : struct xfs_buf *bp,
1479 : struct xfs_da_args *args)
1480 : {
1481 103645527 : struct xfs_attr_leafblock *leaf;
1482 103645527 : struct xfs_attr3_icleaf_hdr ichdr;
1483 103645527 : int tablesize;
1484 103645527 : int entsize;
1485 103645527 : int sum;
1486 103645527 : int tmp;
1487 103645527 : int i;
1488 :
1489 103645527 : trace_xfs_attr_leaf_add(args);
1490 :
1491 103603380 : leaf = bp->b_addr;
1492 103603380 : xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
1493 103611532 : ASSERT(args->index >= 0 && args->index <= ichdr.count);
1494 103611532 : entsize = xfs_attr_leaf_newentsize(args, NULL);
1495 :
1496 : /*
1497 : * Search through freemap for first-fit on new name length.
1498 : * (may need to figure in size of entry struct too)
1499 : */
1500 103609144 : tablesize = (ichdr.count + 1) * sizeof(xfs_attr_leaf_entry_t)
1501 103609144 : + xfs_attr3_leaf_hdr_size(leaf);
1502 254175993 : for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE - 1; i >= 0; i--) {
1503 253696732 : if (tablesize > ichdr.firstused) {
1504 92196 : sum += ichdr.freemap[i].size;
1505 92196 : continue;
1506 : }
1507 253604536 : if (!ichdr.freemap[i].size)
1508 87478361 : continue; /* no space in this map */
1509 166069497 : tmp = entsize;
1510 166069497 : if (ichdr.freemap[i].base < ichdr.firstused)
1511 67865160 : tmp += sizeof(xfs_attr_leaf_entry_t);
1512 166057929 : if (ichdr.freemap[i].size >= tmp) {
1513 103134678 : tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, i);
1514 103167895 : goto out_log_hdr;
1515 : }
1516 62994817 : sum += ichdr.freemap[i].size;
1517 : }
1518 :
1519 : /*
1520 : * If there are no holes in the address space of the block,
1521 : * and we don't have enough freespace, then compaction will do us
1522 : * no good and we should just give up.
1523 : */
1524 479261 : if (!ichdr.holes && sum < entsize)
1525 : return -ENOSPC;
1526 :
1527 : /*
1528 : * Compact the entries to coalesce free space.
1529 : * This may change the hdr->count via dropping INCOMPLETE entries.
1530 : */
1531 323802 : xfs_attr3_leaf_compact(args, &ichdr, bp);
1532 :
1533 : /*
1534 : * After compaction, the block is guaranteed to have only one
1535 : * free region, in freemap[0]. If it is not big enough, give up.
1536 : */
1537 323801 : if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) {
1538 28906 : tmp = -ENOSPC;
1539 28906 : goto out_log_hdr;
1540 : }
1541 :
1542 294895 : tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, 0);
1543 :
1544 103491697 : out_log_hdr:
1545 103491697 : xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
1546 103315216 : xfs_trans_log_buf(args->trans, bp,
1547 103315216 : XFS_DA_LOGRANGE(leaf, &leaf->hdr,
1548 : xfs_attr3_leaf_hdr_size(leaf)));
1549 103315216 : return tmp;
1550 : }
1551 :
1552 : /*
1553 : * Add a name to a leaf attribute list structure.
1554 : */
1555 : STATIC int
1556 103425976 : xfs_attr3_leaf_add_work(
1557 : struct xfs_buf *bp,
1558 : struct xfs_attr3_icleaf_hdr *ichdr,
1559 : struct xfs_da_args *args,
1560 : int mapindex)
1561 : {
1562 103425976 : struct xfs_attr_leafblock *leaf;
1563 103425976 : struct xfs_attr_leaf_entry *entry;
1564 103425976 : struct xfs_attr_leaf_name_local *name_loc;
1565 103425976 : struct xfs_attr_leaf_name_remote *name_rmt;
1566 103425976 : struct xfs_mount *mp;
1567 103425976 : int tmp;
1568 103425976 : int i;
1569 :
1570 103425976 : trace_xfs_attr_leaf_add_work(args);
1571 :
1572 103389203 : leaf = bp->b_addr;
1573 103389203 : ASSERT(mapindex >= 0 && mapindex < XFS_ATTR_LEAF_MAPSIZE);
1574 103389203 : ASSERT(args->index >= 0 && args->index <= ichdr->count);
1575 :
1576 : /*
1577 : * Force open some space in the entry array and fill it in.
1578 : */
1579 103389203 : entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
1580 103389203 : if (args->index < ichdr->count) {
1581 87866981 : tmp = ichdr->count - args->index;
1582 87866981 : tmp *= sizeof(xfs_attr_leaf_entry_t);
1583 175733962 : memmove(entry + 1, entry, tmp);
1584 87866981 : xfs_trans_log_buf(args->trans, bp,
1585 87866981 : XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1586 : }
1587 103432339 : ichdr->count++;
1588 :
1589 : /*
1590 : * Allocate space for the new string (at the end of the run).
1591 : */
1592 103432339 : mp = args->trans->t_mountp;
1593 103432339 : ASSERT(ichdr->freemap[mapindex].base < args->geo->blksize);
1594 103438516 : ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0);
1595 103437493 : ASSERT(ichdr->freemap[mapindex].size >=
1596 : xfs_attr_leaf_newentsize(args, NULL));
1597 103425631 : ASSERT(ichdr->freemap[mapindex].size < args->geo->blksize);
1598 103426200 : ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0);
1599 :
1600 103433316 : ichdr->freemap[mapindex].size -= xfs_attr_leaf_newentsize(args, &tmp);
1601 :
1602 103434314 : entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base +
1603 : ichdr->freemap[mapindex].size);
1604 103431562 : entry->hashval = cpu_to_be32(args->hashval);
1605 103431562 : entry->flags = args->attr_filter;
1606 103431562 : if (tmp)
1607 103423088 : entry->flags |= XFS_ATTR_LOCAL;
1608 103431562 : if (args->op_flags & XFS_DA_OP_REPLACE) {
1609 17909953 : if (!(args->op_flags & XFS_DA_OP_LOGGED))
1610 17909919 : entry->flags |= XFS_ATTR_INCOMPLETE;
1611 17909953 : if ((args->blkno2 == args->blkno) &&
1612 17909530 : (args->index2 <= args->index)) {
1613 17909508 : args->index2++;
1614 : }
1615 : }
1616 103431562 : xfs_trans_log_buf(args->trans, bp,
1617 103431562 : XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
1618 103427560 : ASSERT((args->index == 0) ||
1619 : (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
1620 103427560 : ASSERT((args->index == ichdr->count - 1) ||
1621 : (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
1622 :
1623 : /*
1624 : * For "remote" attribute values, simply note that we need to
1625 : * allocate space for the "remote" value. We can't actually
1626 : * allocate the extents in this transaction, and we can't decide
1627 : * which blocks they should be as we might allocate more blocks
1628 : * as part of this transaction (a split operation for example).
1629 : */
1630 103427560 : if (entry->flags & XFS_ATTR_LOCAL) {
1631 103424597 : name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
1632 103424597 : name_loc->namelen = args->namelen;
1633 103424597 : name_loc->valuelen = cpu_to_be16(args->valuelen);
1634 206849194 : memcpy((char *)name_loc->nameval, args->name, args->namelen);
1635 206849194 : memcpy((char *)&name_loc->nameval[args->namelen], args->value,
1636 : be16_to_cpu(name_loc->valuelen));
1637 : } else {
1638 2963 : name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
1639 2963 : name_rmt->namelen = args->namelen;
1640 5926 : memcpy((char *)name_rmt->name, args->name, args->namelen);
1641 2963 : entry->flags |= XFS_ATTR_INCOMPLETE;
1642 : /* just in case */
1643 2963 : name_rmt->valuelen = 0;
1644 2963 : name_rmt->valueblk = 0;
1645 2963 : args->rmtblkno = 1;
1646 2963 : args->rmtblkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
1647 2963 : args->rmtvaluelen = args->valuelen;
1648 : }
1649 206855120 : xfs_trans_log_buf(args->trans, bp,
1650 206855120 : XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
1651 : xfs_attr_leaf_entsize(leaf, args->index)));
1652 :
1653 : /*
1654 : * Update the control info for this leaf node
1655 : */
1656 103493469 : if (be16_to_cpu(entry->nameidx) < ichdr->firstused)
1657 67728010 : ichdr->firstused = be16_to_cpu(entry->nameidx);
1658 :
1659 206985732 : ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t)
1660 : + xfs_attr3_leaf_hdr_size(leaf));
1661 206986938 : tmp = (ichdr->count - 1) * sizeof(xfs_attr_leaf_entry_t)
1662 103493469 : + xfs_attr3_leaf_hdr_size(leaf);
1663 :
1664 413610705 : for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
1665 310165593 : if (ichdr->freemap[i].base == tmp) {
1666 103438159 : ichdr->freemap[i].base += sizeof(xfs_attr_leaf_entry_t);
1667 103412935 : ichdr->freemap[i].size -=
1668 103430647 : min_t(uint16_t, ichdr->freemap[i].size,
1669 : sizeof(xfs_attr_leaf_entry_t));
1670 : }
1671 : }
1672 103445112 : ichdr->usedbytes += xfs_attr_leaf_entsize(leaf, args->index);
1673 103445112 : return 0;
1674 : }
1675 :
1676 : /*
1677 : * Garbage collect a leaf attribute list block by copying it to a new buffer.
1678 : */
1679 : STATIC void
1680 323803 : xfs_attr3_leaf_compact(
1681 : struct xfs_da_args *args,
1682 : struct xfs_attr3_icleaf_hdr *ichdr_dst,
1683 : struct xfs_buf *bp)
1684 : {
1685 323803 : struct xfs_attr_leafblock *leaf_src;
1686 323803 : struct xfs_attr_leafblock *leaf_dst;
1687 323803 : struct xfs_attr3_icleaf_hdr ichdr_src;
1688 323803 : struct xfs_trans *trans = args->trans;
1689 323803 : char *tmpbuffer;
1690 :
1691 323803 : trace_xfs_attr_leaf_compact(args);
1692 :
1693 323801 : tmpbuffer = kmem_alloc(args->geo->blksize, 0);
1694 647604 : memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
1695 323802 : memset(bp->b_addr, 0, args->geo->blksize);
1696 323802 : leaf_src = (xfs_attr_leafblock_t *)tmpbuffer;
1697 323802 : leaf_dst = bp->b_addr;
1698 :
1699 : /*
1700 : * Copy the on-disk header back into the destination buffer to ensure
1701 : * all the information in the header that is not part of the incore
1702 : * header structure is preserved.
1703 : */
1704 971406 : memcpy(bp->b_addr, tmpbuffer, xfs_attr3_leaf_hdr_size(leaf_src));
1705 :
1706 : /* Initialise the incore headers */
1707 323802 : ichdr_src = *ichdr_dst; /* struct copy */
1708 323802 : ichdr_dst->firstused = args->geo->blksize;
1709 323802 : ichdr_dst->usedbytes = 0;
1710 323802 : ichdr_dst->count = 0;
1711 323802 : ichdr_dst->holes = 0;
1712 323802 : ichdr_dst->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_src);
1713 323802 : ichdr_dst->freemap[0].size = ichdr_dst->firstused -
1714 : ichdr_dst->freemap[0].base;
1715 :
1716 : /* write the header back to initialise the underlying buffer */
1717 323802 : xfs_attr3_leaf_hdr_to_disk(args->geo, leaf_dst, ichdr_dst);
1718 :
1719 : /*
1720 : * Copy all entry's in the same (sorted) order,
1721 : * but allocate name/value pairs packed and in sequence.
1722 : */
1723 323802 : xfs_attr3_leaf_moveents(args, leaf_src, &ichdr_src, 0,
1724 323802 : leaf_dst, ichdr_dst, 0, ichdr_src.count);
1725 : /*
1726 : * this logs the entire buffer, but the caller must write the header
1727 : * back to the buffer when it is finished modifying it.
1728 : */
1729 323803 : xfs_trans_log_buf(trans, bp, 0, args->geo->blksize - 1);
1730 :
1731 323801 : kmem_free(tmpbuffer);
1732 323802 : }
1733 :
1734 : /*
1735 : * Compare two leaf blocks "order".
1736 : * Return 0 unless leaf2 should go before leaf1.
1737 : */
1738 : static int
1739 288338 : xfs_attr3_leaf_order(
1740 : struct xfs_buf *leaf1_bp,
1741 : struct xfs_attr3_icleaf_hdr *leaf1hdr,
1742 : struct xfs_buf *leaf2_bp,
1743 : struct xfs_attr3_icleaf_hdr *leaf2hdr)
1744 : {
1745 288338 : struct xfs_attr_leaf_entry *entries1;
1746 288338 : struct xfs_attr_leaf_entry *entries2;
1747 :
1748 288338 : entries1 = xfs_attr3_leaf_entryp(leaf1_bp->b_addr);
1749 288338 : entries2 = xfs_attr3_leaf_entryp(leaf2_bp->b_addr);
1750 288338 : if (leaf1hdr->count > 0 && leaf2hdr->count > 0 &&
1751 145019 : ((be32_to_cpu(entries2[0].hashval) <
1752 145019 : be32_to_cpu(entries1[0].hashval)) ||
1753 144176 : (be32_to_cpu(entries2[leaf2hdr->count - 1].hashval) <
1754 144176 : be32_to_cpu(entries1[leaf1hdr->count - 1].hashval)))) {
1755 843 : return 1;
1756 : }
1757 : return 0;
1758 : }
1759 :
1760 : int
1761 143318 : xfs_attr_leaf_order(
1762 : struct xfs_buf *leaf1_bp,
1763 : struct xfs_buf *leaf2_bp)
1764 : {
1765 143318 : struct xfs_attr3_icleaf_hdr ichdr1;
1766 143318 : struct xfs_attr3_icleaf_hdr ichdr2;
1767 143318 : struct xfs_mount *mp = leaf1_bp->b_mount;
1768 :
1769 143318 : xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr1, leaf1_bp->b_addr);
1770 143318 : xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr2, leaf2_bp->b_addr);
1771 143319 : return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2);
1772 : }
1773 :
1774 : /*
1775 : * Redistribute the attribute list entries between two leaf nodes,
1776 : * taking into account the size of the new entry.
1777 : *
1778 : * NOTE: if new block is empty, then it will get the upper half of the
1779 : * old block. At present, all (one) callers pass in an empty second block.
1780 : *
1781 : * This code adjusts the args->index/blkno and args->index2/blkno2 fields
1782 : * to match what it is doing in splitting the attribute leaf block. Those
1783 : * values are used in "atomic rename" operations on attributes. Note that
1784 : * the "new" and "old" values can end up in different blocks.
1785 : */
1786 : STATIC void
1787 143318 : xfs_attr3_leaf_rebalance(
1788 : struct xfs_da_state *state,
1789 : struct xfs_da_state_blk *blk1,
1790 : struct xfs_da_state_blk *blk2)
1791 : {
1792 143318 : struct xfs_da_args *args;
1793 143318 : struct xfs_attr_leafblock *leaf1;
1794 143318 : struct xfs_attr_leafblock *leaf2;
1795 143318 : struct xfs_attr3_icleaf_hdr ichdr1;
1796 143318 : struct xfs_attr3_icleaf_hdr ichdr2;
1797 143318 : struct xfs_attr_leaf_entry *entries1;
1798 143318 : struct xfs_attr_leaf_entry *entries2;
1799 143318 : int count;
1800 143318 : int totallen;
1801 143318 : int max;
1802 143318 : int space;
1803 143318 : int swap;
1804 :
1805 : /*
1806 : * Set up environment.
1807 : */
1808 143318 : ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1809 143318 : ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1810 143318 : leaf1 = blk1->bp->b_addr;
1811 143318 : leaf2 = blk2->bp->b_addr;
1812 143318 : xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr1, leaf1);
1813 143318 : xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, leaf2);
1814 143318 : ASSERT(ichdr2.count == 0);
1815 143318 : args = state->args;
1816 :
1817 143318 : trace_xfs_attr_leaf_rebalance(args);
1818 :
1819 : /*
1820 : * Check ordering of blocks, reverse if it makes things simpler.
1821 : *
1822 : * NOTE: Given that all (current) callers pass in an empty
1823 : * second block, this code should never set "swap".
1824 : */
1825 143318 : swap = 0;
1826 143318 : if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) {
1827 0 : swap(blk1, blk2);
1828 :
1829 : /* swap structures rather than reconverting them */
1830 0 : swap(ichdr1, ichdr2);
1831 :
1832 0 : leaf1 = blk1->bp->b_addr;
1833 0 : leaf2 = blk2->bp->b_addr;
1834 0 : swap = 1;
1835 : }
1836 :
1837 : /*
1838 : * Examine entries until we reduce the absolute difference in
1839 : * byte usage between the two blocks to a minimum. Then get
1840 : * the direction to copy and the number of elements to move.
1841 : *
1842 : * "inleaf" is true if the new entry should be inserted into blk1.
1843 : * If "swap" is also true, then reverse the sense of "inleaf".
1844 : */
1845 143318 : state->inleaf = xfs_attr3_leaf_figure_balance(state, blk1, &ichdr1,
1846 : blk2, &ichdr2,
1847 : &count, &totallen);
1848 143319 : if (swap)
1849 0 : state->inleaf = !state->inleaf;
1850 :
1851 : /*
1852 : * Move any entries required from leaf to leaf:
1853 : */
1854 143319 : if (count < ichdr1.count) {
1855 : /*
1856 : * Figure the total bytes to be added to the destination leaf.
1857 : */
1858 : /* number entries being moved */
1859 143319 : count = ichdr1.count - count;
1860 143319 : space = ichdr1.usedbytes - totallen;
1861 143319 : space += count * sizeof(xfs_attr_leaf_entry_t);
1862 :
1863 : /*
1864 : * leaf2 is the destination, compact it if it looks tight.
1865 : */
1866 143319 : max = ichdr2.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1867 143319 : max -= ichdr2.count * sizeof(xfs_attr_leaf_entry_t);
1868 143319 : if (space > max)
1869 0 : xfs_attr3_leaf_compact(args, &ichdr2, blk2->bp);
1870 :
1871 : /*
1872 : * Move high entries from leaf1 to low end of leaf2.
1873 : */
1874 143319 : xfs_attr3_leaf_moveents(args, leaf1, &ichdr1,
1875 143319 : ichdr1.count - count, leaf2, &ichdr2, 0, count);
1876 :
1877 0 : } else if (count > ichdr1.count) {
1878 : /*
1879 : * I assert that since all callers pass in an empty
1880 : * second buffer, this code should never execute.
1881 : */
1882 0 : ASSERT(0);
1883 :
1884 : /*
1885 : * Figure the total bytes to be added to the destination leaf.
1886 : */
1887 : /* number entries being moved */
1888 0 : count -= ichdr1.count;
1889 0 : space = totallen - ichdr1.usedbytes;
1890 0 : space += count * sizeof(xfs_attr_leaf_entry_t);
1891 :
1892 : /*
1893 : * leaf1 is the destination, compact it if it looks tight.
1894 : */
1895 0 : max = ichdr1.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1896 0 : max -= ichdr1.count * sizeof(xfs_attr_leaf_entry_t);
1897 0 : if (space > max)
1898 0 : xfs_attr3_leaf_compact(args, &ichdr1, blk1->bp);
1899 :
1900 : /*
1901 : * Move low entries from leaf2 to high end of leaf1.
1902 : */
1903 0 : xfs_attr3_leaf_moveents(args, leaf2, &ichdr2, 0, leaf1, &ichdr1,
1904 0 : ichdr1.count, count);
1905 : }
1906 :
1907 143319 : xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf1, &ichdr1);
1908 143319 : xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf2, &ichdr2);
1909 143319 : xfs_trans_log_buf(args->trans, blk1->bp, 0, args->geo->blksize - 1);
1910 143319 : xfs_trans_log_buf(args->trans, blk2->bp, 0, args->geo->blksize - 1);
1911 :
1912 : /*
1913 : * Copy out last hashval in each block for B-tree code.
1914 : */
1915 143319 : entries1 = xfs_attr3_leaf_entryp(leaf1);
1916 143319 : entries2 = xfs_attr3_leaf_entryp(leaf2);
1917 143319 : blk1->hashval = be32_to_cpu(entries1[ichdr1.count - 1].hashval);
1918 143319 : blk2->hashval = be32_to_cpu(entries2[ichdr2.count - 1].hashval);
1919 :
1920 : /*
1921 : * Adjust the expected index for insertion.
1922 : * NOTE: this code depends on the (current) situation that the
1923 : * second block was originally empty.
1924 : *
1925 : * If the insertion point moved to the 2nd block, we must adjust
1926 : * the index. We must also track the entry just following the
1927 : * new entry for use in an "atomic rename" operation, that entry
1928 : * is always the "old" entry and the "new" entry is what we are
1929 : * inserting. The index/blkno fields refer to the "old" entry,
1930 : * while the index2/blkno2 fields refer to the "new" entry.
1931 : */
1932 143319 : if (blk1->index > ichdr1.count) {
1933 109448 : ASSERT(state->inleaf == 0);
1934 109448 : blk2->index = blk1->index - ichdr1.count;
1935 109448 : args->index = args->index2 = blk2->index;
1936 109448 : args->blkno = args->blkno2 = blk2->blkno;
1937 33871 : } else if (blk1->index == ichdr1.count) {
1938 14880 : if (state->inleaf) {
1939 3944 : args->index = blk1->index;
1940 3944 : args->blkno = blk1->blkno;
1941 3944 : args->index2 = 0;
1942 3944 : args->blkno2 = blk2->blkno;
1943 : } else {
1944 : /*
1945 : * On a double leaf split, the original attr location
1946 : * is already stored in blkno2/index2, so don't
1947 : * overwrite it overwise we corrupt the tree.
1948 : */
1949 10936 : blk2->index = blk1->index - ichdr1.count;
1950 10936 : args->index = blk2->index;
1951 10936 : args->blkno = blk2->blkno;
1952 10936 : if (!state->extravalid) {
1953 : /*
1954 : * set the new attr location to match the old
1955 : * one and let the higher level split code
1956 : * decide where in the leaf to place it.
1957 : */
1958 10936 : args->index2 = blk2->index;
1959 10936 : args->blkno2 = blk2->blkno;
1960 : }
1961 : }
1962 : } else {
1963 18991 : ASSERT(state->inleaf == 1);
1964 18991 : args->index = args->index2 = blk1->index;
1965 18991 : args->blkno = args->blkno2 = blk1->blkno;
1966 : }
1967 143319 : }
1968 :
1969 : /*
1970 : * Examine entries until we reduce the absolute difference in
1971 : * byte usage between the two blocks to a minimum.
1972 : * GROT: Is this really necessary? With other than a 512 byte blocksize,
1973 : * GROT: there will always be enough room in either block for a new entry.
1974 : * GROT: Do a double-split for this case?
1975 : */
1976 : STATIC int
1977 143318 : xfs_attr3_leaf_figure_balance(
1978 : struct xfs_da_state *state,
1979 : struct xfs_da_state_blk *blk1,
1980 : struct xfs_attr3_icleaf_hdr *ichdr1,
1981 : struct xfs_da_state_blk *blk2,
1982 : struct xfs_attr3_icleaf_hdr *ichdr2,
1983 : int *countarg,
1984 : int *usedbytesarg)
1985 : {
1986 143318 : struct xfs_attr_leafblock *leaf1 = blk1->bp->b_addr;
1987 143318 : struct xfs_attr_leafblock *leaf2 = blk2->bp->b_addr;
1988 143318 : struct xfs_attr_leaf_entry *entry;
1989 143318 : int count;
1990 143318 : int max;
1991 143318 : int index;
1992 143318 : int totallen = 0;
1993 143318 : int half;
1994 143318 : int lastdelta;
1995 143318 : int foundit = 0;
1996 143318 : int tmp;
1997 :
1998 : /*
1999 : * Examine entries until we reduce the absolute difference in
2000 : * byte usage between the two blocks to a minimum.
2001 : */
2002 143318 : max = ichdr1->count + ichdr2->count;
2003 143318 : half = (max + 1) * sizeof(*entry);
2004 286635 : half += ichdr1->usedbytes + ichdr2->usedbytes +
2005 143318 : xfs_attr_leaf_newentsize(state->args, NULL);
2006 143317 : half /= 2;
2007 143317 : lastdelta = state->args->geo->blksize;
2008 143317 : entry = xfs_attr3_leaf_entryp(leaf1);
2009 2212025 : for (count = index = 0; count < max; entry++, index++, count++) {
2010 :
2011 : #define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
2012 : /*
2013 : * The new entry is in the first block, account for it.
2014 : */
2015 2212025 : if (count == blk1->index) {
2016 67744 : tmp = totallen + sizeof(*entry) +
2017 33871 : xfs_attr_leaf_newentsize(state->args, NULL);
2018 33873 : if (XFS_ATTR_ABS(half - tmp) > lastdelta)
2019 : break;
2020 : lastdelta = XFS_ATTR_ABS(half - tmp);
2021 : totallen = tmp;
2022 : foundit = 1;
2023 : }
2024 :
2025 : /*
2026 : * Wrap around into the second block if necessary.
2027 : */
2028 2201091 : if (count == ichdr1->count) {
2029 0 : leaf1 = leaf2;
2030 0 : entry = xfs_attr3_leaf_entryp(leaf1);
2031 : index = 0;
2032 : }
2033 :
2034 : /*
2035 : * Figure out if next leaf entry would be too much.
2036 : */
2037 2201091 : tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
2038 : index);
2039 2201091 : if (XFS_ATTR_ABS(half - tmp) > lastdelta)
2040 : break;
2041 2068708 : lastdelta = XFS_ATTR_ABS(half - tmp);
2042 2068708 : totallen = tmp;
2043 : #undef XFS_ATTR_ABS
2044 : }
2045 :
2046 : /*
2047 : * Calculate the number of usedbytes that will end up in lower block.
2048 : * If new entry not in lower block, fix up the count.
2049 : */
2050 143319 : totallen -= count * sizeof(*entry);
2051 143319 : if (foundit) {
2052 45870 : totallen -= sizeof(*entry) +
2053 22935 : xfs_attr_leaf_newentsize(state->args, NULL);
2054 : }
2055 :
2056 143319 : *countarg = count;
2057 143319 : *usedbytesarg = totallen;
2058 143319 : return foundit;
2059 : }
2060 :
2061 : /*========================================================================
2062 : * Routines used for shrinking the Btree.
2063 : *========================================================================*/
2064 :
2065 : /*
2066 : * Check a leaf block and its neighbors to see if the block should be
2067 : * collapsed into one or the other neighbor. Always keep the block
2068 : * with the smaller block number.
2069 : * If the current block is over 50% full, don't try to join it, return 0.
2070 : * If the block is empty, fill in the state structure and return 2.
2071 : * If it can be collapsed, fill in the state structure and return 1.
2072 : * If nothing can be done, return 0.
2073 : *
2074 : * GROT: allow for INCOMPLETE entries in calculation.
2075 : */
2076 : int
2077 1044650 : xfs_attr3_leaf_toosmall(
2078 : struct xfs_da_state *state,
2079 : int *action)
2080 : {
2081 1044650 : struct xfs_attr_leafblock *leaf;
2082 1044650 : struct xfs_da_state_blk *blk;
2083 1044650 : struct xfs_attr3_icleaf_hdr ichdr;
2084 1044650 : struct xfs_buf *bp;
2085 1044650 : xfs_dablk_t blkno;
2086 1044650 : int bytes;
2087 1044650 : int forward;
2088 1044650 : int error;
2089 1044650 : int retval;
2090 1044650 : int i;
2091 :
2092 1044650 : trace_xfs_attr_leaf_toosmall(state->args);
2093 :
2094 : /*
2095 : * Check for the degenerate case of the block being over 50% full.
2096 : * If so, it's not worth even looking to see if we might be able
2097 : * to coalesce with a sibling.
2098 : */
2099 1044632 : blk = &state->path.blk[ state->path.active-1 ];
2100 1044632 : leaf = blk->bp->b_addr;
2101 1044632 : xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr, leaf);
2102 1044672 : bytes = xfs_attr3_leaf_hdr_size(leaf) +
2103 1044672 : ichdr.count * sizeof(xfs_attr_leaf_entry_t) +
2104 1044672 : ichdr.usedbytes;
2105 1044672 : if (bytes > (state->args->geo->blksize >> 1)) {
2106 695409 : *action = 0; /* blk over 50%, don't try to join */
2107 695409 : return 0;
2108 : }
2109 :
2110 : /*
2111 : * Check for the degenerate case of the block being empty.
2112 : * If the block is empty, we'll simply delete it, no need to
2113 : * coalesce it with a sibling block. We choose (arbitrarily)
2114 : * to merge with the forward block unless it is NULL.
2115 : */
2116 349263 : if (ichdr.count == 0) {
2117 : /*
2118 : * Make altpath point to the block we want to keep and
2119 : * path point to the block we want to drop (this one).
2120 : */
2121 0 : forward = (ichdr.forw != 0);
2122 0 : memcpy(&state->altpath, &state->path, sizeof(state->path));
2123 0 : error = xfs_da3_path_shift(state, &state->altpath, forward,
2124 : 0, &retval);
2125 0 : if (error)
2126 : return error;
2127 0 : if (retval) {
2128 0 : *action = 0;
2129 : } else {
2130 0 : *action = 2;
2131 : }
2132 0 : return 0;
2133 : }
2134 :
2135 : /*
2136 : * Examine each sibling block to see if we can coalesce with
2137 : * at least 25% free space to spare. We need to figure out
2138 : * whether to merge with the forward or the backward block.
2139 : * We prefer coalescing with the lower numbered sibling so as
2140 : * to shrink an attribute list over time.
2141 : */
2142 : /* start with smaller blk num */
2143 349263 : forward = ichdr.forw < ichdr.back;
2144 1045113 : for (i = 0; i < 2; forward = !forward, i++) {
2145 697556 : struct xfs_attr3_icleaf_hdr ichdr2;
2146 697556 : if (forward)
2147 : blkno = ichdr.forw;
2148 : else
2149 348906 : blkno = ichdr.back;
2150 697556 : if (blkno == 0)
2151 300378 : continue;
2152 397178 : error = xfs_attr3_leaf_read(state->args->trans, state->args->dp,
2153 397178 : state->args->owner, blkno, &bp);
2154 397183 : if (error)
2155 10 : return error;
2156 :
2157 397173 : xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, bp->b_addr);
2158 :
2159 794346 : bytes = state->args->geo->blksize -
2160 397173 : (state->args->geo->blksize >> 2) -
2161 397173 : ichdr.usedbytes - ichdr2.usedbytes -
2162 397173 : ((ichdr.count + ichdr2.count) *
2163 397173 : sizeof(xfs_attr_leaf_entry_t)) -
2164 397173 : xfs_attr3_leaf_hdr_size(leaf);
2165 :
2166 397173 : xfs_trans_brelse(state->args->trans, bp);
2167 397173 : if (bytes >= 0)
2168 : break; /* fits with at least 25% to spare */
2169 : }
2170 349258 : if (i >= 2) {
2171 347557 : *action = 0;
2172 347557 : return 0;
2173 : }
2174 :
2175 : /*
2176 : * Make altpath point to the block we want to keep (the lower
2177 : * numbered block) and path point to the block we want to drop.
2178 : */
2179 3402 : memcpy(&state->altpath, &state->path, sizeof(state->path));
2180 1701 : if (blkno < blk->blkno) {
2181 743 : error = xfs_da3_path_shift(state, &state->altpath, forward,
2182 : 0, &retval);
2183 : } else {
2184 958 : error = xfs_da3_path_shift(state, &state->path, forward,
2185 : 0, &retval);
2186 : }
2187 1701 : if (error)
2188 : return error;
2189 1701 : if (retval) {
2190 0 : *action = 0;
2191 : } else {
2192 1701 : *action = 1;
2193 : }
2194 : return 0;
2195 : }
2196 :
2197 : /*
2198 : * Remove a name from the leaf attribute list structure.
2199 : *
2200 : * Return 1 if leaf is less than 37% full, 0 if >= 37% full.
2201 : * If two leaves are 37% full, when combined they will leave 25% free.
2202 : */
2203 : int
2204 48964140 : xfs_attr3_leaf_remove(
2205 : struct xfs_buf *bp,
2206 : struct xfs_da_args *args)
2207 : {
2208 48964140 : struct xfs_attr_leafblock *leaf;
2209 48964140 : struct xfs_attr3_icleaf_hdr ichdr;
2210 48964140 : struct xfs_attr_leaf_entry *entry;
2211 48964140 : int before;
2212 48964140 : int after;
2213 48964140 : int smallest;
2214 48964140 : int entsize;
2215 48964140 : int tablesize;
2216 48964140 : int tmp;
2217 48964140 : int i;
2218 :
2219 48964140 : trace_xfs_attr_leaf_remove(args);
2220 :
2221 48948556 : leaf = bp->b_addr;
2222 48948556 : xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2223 :
2224 48950321 : ASSERT(ichdr.count > 0 && ichdr.count < args->geo->blksize / 8);
2225 48950321 : ASSERT(args->index >= 0 && args->index < ichdr.count);
2226 97900569 : ASSERT(ichdr.firstused >= ichdr.count * sizeof(*entry) +
2227 : xfs_attr3_leaf_hdr_size(leaf));
2228 :
2229 48950321 : entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2230 :
2231 48950321 : ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
2232 48950321 : ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
2233 :
2234 : /*
2235 : * Scan through free region table:
2236 : * check for adjacency of free'd entry with an existing one,
2237 : * find smallest free region in case we need to replace it,
2238 : * adjust any map that borders the entry table,
2239 : */
2240 48950321 : tablesize = ichdr.count * sizeof(xfs_attr_leaf_entry_t)
2241 48950321 : + xfs_attr3_leaf_hdr_size(leaf);
2242 48950321 : tmp = ichdr.freemap[0].size;
2243 48950321 : before = after = -1;
2244 48950321 : smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
2245 48950321 : entsize = xfs_attr_leaf_entsize(leaf, args->index);
2246 195765605 : for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
2247 146802762 : ASSERT(ichdr.freemap[i].base < args->geo->blksize);
2248 146791485 : ASSERT(ichdr.freemap[i].size < args->geo->blksize);
2249 146798539 : if (ichdr.freemap[i].base == tablesize) {
2250 48868127 : ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t);
2251 48867274 : ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t);
2252 : }
2253 :
2254 146794328 : if (ichdr.freemap[i].base + ichdr.freemap[i].size ==
2255 146804893 : be16_to_cpu(entry->nameidx)) {
2256 : before = i;
2257 139077759 : } else if (ichdr.freemap[i].base ==
2258 139088950 : (be16_to_cpu(entry->nameidx) + entsize)) {
2259 : after = i;
2260 134342568 : } else if (ichdr.freemap[i].size < tmp) {
2261 69188694 : tmp = ichdr.freemap[i].size;
2262 69188694 : smallest = i;
2263 : }
2264 : }
2265 :
2266 : /*
2267 : * Coalesce adjacent freemap regions,
2268 : * or replace the smallest region.
2269 : */
2270 48962843 : if ((before >= 0) || (after >= 0)) {
2271 11854437 : if ((before >= 0) && (after >= 0)) {
2272 618398 : ichdr.freemap[before].size += entsize;
2273 618398 : ichdr.freemap[before].size += ichdr.freemap[after].size;
2274 618398 : ichdr.freemap[after].base = 0;
2275 618396 : ichdr.freemap[after].size = 0;
2276 11236039 : } else if (before >= 0) {
2277 7108090 : ichdr.freemap[before].size += entsize;
2278 : } else {
2279 4127949 : ichdr.freemap[after].base = be16_to_cpu(entry->nameidx);
2280 4127933 : ichdr.freemap[after].size += entsize;
2281 : }
2282 : } else {
2283 : /*
2284 : * Replace smallest region (if it is smaller than free'd entry)
2285 : */
2286 37108406 : if (ichdr.freemap[smallest].size < entsize) {
2287 30673760 : ichdr.freemap[smallest].base = be16_to_cpu(entry->nameidx);
2288 30673177 : ichdr.freemap[smallest].size = entsize;
2289 : }
2290 : }
2291 :
2292 : /*
2293 : * Did we remove the first entry?
2294 : */
2295 48962931 : if (be16_to_cpu(entry->nameidx) == ichdr.firstused)
2296 : smallest = 1;
2297 : else
2298 45673460 : smallest = 0;
2299 :
2300 : /*
2301 : * Compress the remaining entries and zero out the removed stuff.
2302 : */
2303 48962931 : memset(xfs_attr3_leaf_name(leaf, args->index), 0, entsize);
2304 48962931 : ichdr.usedbytes -= entsize;
2305 97925862 : xfs_trans_log_buf(args->trans, bp,
2306 97925862 : XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
2307 : entsize));
2308 :
2309 48958791 : tmp = (ichdr.count - args->index) * sizeof(xfs_attr_leaf_entry_t);
2310 97917582 : memmove(entry, entry + 1, tmp);
2311 48958791 : ichdr.count--;
2312 48958791 : xfs_trans_log_buf(args->trans, bp,
2313 48958791 : XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(xfs_attr_leaf_entry_t)));
2314 :
2315 48970403 : entry = &xfs_attr3_leaf_entryp(leaf)[ichdr.count];
2316 48970403 : memset(entry, 0, sizeof(xfs_attr_leaf_entry_t));
2317 :
2318 : /*
2319 : * If we removed the first entry, re-find the first used byte
2320 : * in the name area. Note that if the entry was the "firstused",
2321 : * then we don't have a "hole" in our block resulting from
2322 : * removing the name.
2323 : */
2324 48970403 : if (smallest) {
2325 3282163 : tmp = args->geo->blksize;
2326 3282163 : entry = xfs_attr3_leaf_entryp(leaf);
2327 43926167 : for (i = ichdr.count - 1; i >= 0; entry++, i--) {
2328 40644017 : ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
2329 40644017 : ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
2330 :
2331 40644004 : if (be16_to_cpu(entry->nameidx) < tmp)
2332 : tmp = be16_to_cpu(entry->nameidx);
2333 : }
2334 3282150 : ichdr.firstused = tmp;
2335 3282150 : ASSERT(ichdr.firstused != 0);
2336 : } else {
2337 45688240 : ichdr.holes = 1; /* mark as needing compaction */
2338 : }
2339 48970390 : xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
2340 48928766 : xfs_trans_log_buf(args->trans, bp,
2341 48928766 : XFS_DA_LOGRANGE(leaf, &leaf->hdr,
2342 : xfs_attr3_leaf_hdr_size(leaf)));
2343 :
2344 : /*
2345 : * Check if leaf is less than 50% full, caller may want to
2346 : * "join" the leaf with a sibling if so.
2347 : */
2348 48966612 : tmp = ichdr.usedbytes + xfs_attr3_leaf_hdr_size(leaf) +
2349 48966612 : ichdr.count * sizeof(xfs_attr_leaf_entry_t);
2350 :
2351 48966612 : return tmp < args->geo->magicpct; /* leaf is < 37% full */
2352 : }
2353 :
2354 : /*
2355 : * Move all the attribute list entries from drop_leaf into save_leaf.
2356 : */
2357 : void
2358 1701 : xfs_attr3_leaf_unbalance(
2359 : struct xfs_da_state *state,
2360 : struct xfs_da_state_blk *drop_blk,
2361 : struct xfs_da_state_blk *save_blk)
2362 : {
2363 1701 : struct xfs_attr_leafblock *drop_leaf = drop_blk->bp->b_addr;
2364 1701 : struct xfs_attr_leafblock *save_leaf = save_blk->bp->b_addr;
2365 1701 : struct xfs_attr3_icleaf_hdr drophdr;
2366 1701 : struct xfs_attr3_icleaf_hdr savehdr;
2367 1701 : struct xfs_attr_leaf_entry *entry;
2368 :
2369 1701 : trace_xfs_attr_leaf_unbalance(state->args);
2370 :
2371 1701 : xfs_attr3_leaf_hdr_from_disk(state->args->geo, &drophdr, drop_leaf);
2372 1701 : xfs_attr3_leaf_hdr_from_disk(state->args->geo, &savehdr, save_leaf);
2373 1701 : entry = xfs_attr3_leaf_entryp(drop_leaf);
2374 :
2375 : /*
2376 : * Save last hashval from dying block for later Btree fixup.
2377 : */
2378 1701 : drop_blk->hashval = be32_to_cpu(entry[drophdr.count - 1].hashval);
2379 :
2380 : /*
2381 : * Check if we need a temp buffer, or can we do it in place.
2382 : * Note that we don't check "leaf" for holes because we will
2383 : * always be dropping it, toosmall() decided that for us already.
2384 : */
2385 1701 : if (savehdr.holes == 0) {
2386 : /*
2387 : * dest leaf has no holes, so we add there. May need
2388 : * to make some room in the entry array.
2389 : */
2390 0 : if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2391 : drop_blk->bp, &drophdr)) {
2392 0 : xfs_attr3_leaf_moveents(state->args,
2393 : drop_leaf, &drophdr, 0,
2394 : save_leaf, &savehdr, 0,
2395 : drophdr.count);
2396 : } else {
2397 0 : xfs_attr3_leaf_moveents(state->args,
2398 : drop_leaf, &drophdr, 0,
2399 : save_leaf, &savehdr,
2400 0 : savehdr.count, drophdr.count);
2401 : }
2402 : } else {
2403 : /*
2404 : * Destination has holes, so we make a temporary copy
2405 : * of the leaf and add them both to that.
2406 : */
2407 1701 : struct xfs_attr_leafblock *tmp_leaf;
2408 1701 : struct xfs_attr3_icleaf_hdr tmphdr;
2409 :
2410 1701 : tmp_leaf = kmem_zalloc(state->args->geo->blksize, 0);
2411 :
2412 : /*
2413 : * Copy the header into the temp leaf so that all the stuff
2414 : * not in the incore header is present and gets copied back in
2415 : * once we've moved all the entries.
2416 : */
2417 5103 : memcpy(tmp_leaf, save_leaf, xfs_attr3_leaf_hdr_size(save_leaf));
2418 :
2419 1701 : memset(&tmphdr, 0, sizeof(tmphdr));
2420 1701 : tmphdr.magic = savehdr.magic;
2421 1701 : tmphdr.forw = savehdr.forw;
2422 1701 : tmphdr.back = savehdr.back;
2423 1701 : tmphdr.firstused = state->args->geo->blksize;
2424 :
2425 : /* write the header to the temp buffer to initialise it */
2426 1701 : xfs_attr3_leaf_hdr_to_disk(state->args->geo, tmp_leaf, &tmphdr);
2427 :
2428 1701 : if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2429 : drop_blk->bp, &drophdr)) {
2430 843 : xfs_attr3_leaf_moveents(state->args,
2431 : drop_leaf, &drophdr, 0,
2432 : tmp_leaf, &tmphdr, 0,
2433 843 : drophdr.count);
2434 843 : xfs_attr3_leaf_moveents(state->args,
2435 : save_leaf, &savehdr, 0,
2436 843 : tmp_leaf, &tmphdr, tmphdr.count,
2437 843 : savehdr.count);
2438 : } else {
2439 858 : xfs_attr3_leaf_moveents(state->args,
2440 : save_leaf, &savehdr, 0,
2441 : tmp_leaf, &tmphdr, 0,
2442 858 : savehdr.count);
2443 858 : xfs_attr3_leaf_moveents(state->args,
2444 : drop_leaf, &drophdr, 0,
2445 858 : tmp_leaf, &tmphdr, tmphdr.count,
2446 858 : drophdr.count);
2447 : }
2448 3402 : memcpy(save_leaf, tmp_leaf, state->args->geo->blksize);
2449 1701 : savehdr = tmphdr; /* struct copy */
2450 1701 : kmem_free(tmp_leaf);
2451 : }
2452 :
2453 1701 : xfs_attr3_leaf_hdr_to_disk(state->args->geo, save_leaf, &savehdr);
2454 1701 : xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
2455 1701 : state->args->geo->blksize - 1);
2456 :
2457 : /*
2458 : * Copy out last hashval in each block for B-tree code.
2459 : */
2460 1701 : entry = xfs_attr3_leaf_entryp(save_leaf);
2461 1701 : save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval);
2462 1701 : }
2463 :
2464 : /*========================================================================
2465 : * Routines used for finding things in the Btree.
2466 : *========================================================================*/
2467 :
2468 : /*
2469 : * Look up a name in a leaf attribute list structure.
2470 : * This is the internal routine, it uses the caller's buffer.
2471 : *
2472 : * Note that duplicate keys are allowed, but only check within the
2473 : * current leaf node. The Btree code must check in adjacent leaf nodes.
2474 : *
2475 : * Return in args->index the index into the entry[] array of either
2476 : * the found entry, or where the entry should have been (insert before
2477 : * that entry).
2478 : *
2479 : * Don't change the args->value unless we find the attribute.
2480 : */
2481 : int
2482 751873814 : xfs_attr3_leaf_lookup_int(
2483 : struct xfs_buf *bp,
2484 : struct xfs_da_args *args)
2485 : {
2486 751873814 : struct xfs_attr_leafblock *leaf;
2487 751873814 : struct xfs_attr3_icleaf_hdr ichdr;
2488 751873814 : struct xfs_attr_leaf_entry *entry;
2489 751873814 : struct xfs_attr_leaf_entry *entries;
2490 751873814 : struct xfs_attr_leaf_name_local *name_loc;
2491 751873814 : struct xfs_attr_leaf_name_remote *name_rmt;
2492 751873814 : xfs_dahash_t hashval;
2493 751873814 : int probe;
2494 751873814 : int span;
2495 :
2496 751873814 : trace_xfs_attr_leaf_lookup(args);
2497 :
2498 751870440 : leaf = bp->b_addr;
2499 751870440 : xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2500 752050773 : entries = xfs_attr3_leaf_entryp(leaf);
2501 752050773 : if (ichdr.count >= args->geo->blksize / 8) {
2502 0 : xfs_buf_mark_corrupt(bp);
2503 0 : xfs_da_mark_sick(args);
2504 0 : return -EFSCORRUPTED;
2505 : }
2506 :
2507 : /*
2508 : * Binary search. (note: small blocks will skip this loop)
2509 : */
2510 752050773 : hashval = args->hashval;
2511 752050773 : probe = span = ichdr.count / 2;
2512 1045018215 : for (entry = &entries[probe]; span > 4; entry = &entries[probe]) {
2513 301888668 : span /= 2;
2514 301888668 : if (be32_to_cpu(entry->hashval) < hashval)
2515 183143504 : probe += span;
2516 118745164 : else if (be32_to_cpu(entry->hashval) > hashval)
2517 109823938 : probe -= span;
2518 : else
2519 : break;
2520 : }
2521 752050773 : if (!(probe >= 0 && (!ichdr.count || probe < ichdr.count))) {
2522 0 : xfs_buf_mark_corrupt(bp);
2523 0 : xfs_da_mark_sick(args);
2524 0 : return -EFSCORRUPTED;
2525 : }
2526 752050773 : if (!(span <= 4 || be32_to_cpu(entry->hashval) == hashval)) {
2527 0 : xfs_buf_mark_corrupt(bp);
2528 0 : xfs_da_mark_sick(args);
2529 0 : return -EFSCORRUPTED;
2530 : }
2531 :
2532 : /*
2533 : * Since we may have duplicate hashval's, find the first matching
2534 : * hashval in the leaf.
2535 : */
2536 2894052331 : while (probe > 0 && be32_to_cpu(entry->hashval) >= hashval) {
2537 2142001558 : entry--;
2538 2142001558 : probe--;
2539 : }
2540 1261270577 : while (probe < ichdr.count &&
2541 1244358912 : be32_to_cpu(entry->hashval) < hashval) {
2542 509219804 : entry++;
2543 509219804 : probe++;
2544 : }
2545 752050773 : if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) {
2546 170008217 : args->index = probe;
2547 170008217 : return -ENOATTR;
2548 : }
2549 :
2550 : /*
2551 : * Duplicate keys may be present, so search all of them for a match.
2552 : */
2553 4372907509 : for (; probe < ichdr.count && (be32_to_cpu(entry->hashval) == hashval);
2554 3790864953 : entry++, probe++) {
2555 : /*
2556 : * GROT: Add code to remove incomplete entries.
2557 : */
2558 3899089297 : if (entry->flags & XFS_ATTR_LOCAL) {
2559 3899087835 : name_loc = xfs_attr3_leaf_name_local(leaf, probe);
2560 3899035529 : if (!xfs_attr_match(args, name_loc->namelen,
2561 3899087835 : name_loc->nameval,
2562 3899087835 : be16_to_cpu(name_loc->valuelen),
2563 3899087835 : &name_loc->nameval[name_loc->namelen],
2564 : entry->flags))
2565 3790864943 : continue;
2566 108170586 : args->index = probe;
2567 108170586 : return -EEXIST;
2568 : } else {
2569 1462 : name_rmt = xfs_attr3_leaf_name_remote(leaf, probe);
2570 1462 : if (!xfs_attr_match(args, name_rmt->namelen,
2571 1462 : name_rmt->name, 0, NULL, entry->flags))
2572 10 : continue;
2573 1452 : args->index = probe;
2574 1452 : args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
2575 1452 : args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2576 2904 : args->rmtblkcnt = xfs_attr3_rmt_blocks(
2577 1452 : args->dp->i_mount,
2578 : args->rmtvaluelen);
2579 1452 : return -EEXIST;
2580 : }
2581 : }
2582 473818212 : args->index = probe;
2583 473818212 : return -ENOATTR;
2584 : }
2585 :
2586 : /*
2587 : * Get the value associated with an attribute name from a leaf attribute
2588 : * list structure.
2589 : *
2590 : * If args->valuelen is zero, only the length needs to be returned. Unlike a
2591 : * lookup, we only return an error if the attribute does not exist or we can't
2592 : * retrieve the value.
2593 : */
2594 : int
2595 11648776 : xfs_attr3_leaf_getvalue(
2596 : struct xfs_buf *bp,
2597 : struct xfs_da_args *args)
2598 : {
2599 11648776 : struct xfs_attr_leafblock *leaf;
2600 11648776 : struct xfs_attr3_icleaf_hdr ichdr;
2601 11648776 : struct xfs_attr_leaf_entry *entry;
2602 11648776 : struct xfs_attr_leaf_name_local *name_loc;
2603 11648776 : struct xfs_attr_leaf_name_remote *name_rmt;
2604 :
2605 11648776 : leaf = bp->b_addr;
2606 11648776 : xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2607 11646397 : ASSERT(ichdr.count < args->geo->blksize / 8);
2608 11646397 : ASSERT(args->index < ichdr.count);
2609 :
2610 11646397 : entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2611 11646397 : if (entry->flags & XFS_ATTR_LOCAL) {
2612 11645085 : name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
2613 11645085 : ASSERT(name_loc->namelen == args->namelen);
2614 23290170 : ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
2615 11645085 : return xfs_attr_copy_value(args,
2616 11645085 : &name_loc->nameval[args->namelen],
2617 11645085 : be16_to_cpu(name_loc->valuelen));
2618 : }
2619 :
2620 1312 : name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2621 1312 : ASSERT(name_rmt->namelen == args->namelen);
2622 2624 : ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
2623 1312 : args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
2624 1312 : args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2625 1312 : args->rmtblkcnt = xfs_attr3_rmt_blocks(args->dp->i_mount,
2626 : args->rmtvaluelen);
2627 1312 : return xfs_attr_copy_value(args, NULL, args->rmtvaluelen);
2628 : }
2629 :
2630 : /*========================================================================
2631 : * Utility routines.
2632 : *========================================================================*/
2633 :
2634 : /*
2635 : * Move the indicated entries from one leaf to another.
2636 : * NOTE: this routine modifies both source and destination leaves.
2637 : */
2638 : /*ARGSUSED*/
2639 : STATIC void
2640 470518 : xfs_attr3_leaf_moveents(
2641 : struct xfs_da_args *args,
2642 : struct xfs_attr_leafblock *leaf_s,
2643 : struct xfs_attr3_icleaf_hdr *ichdr_s,
2644 : int start_s,
2645 : struct xfs_attr_leafblock *leaf_d,
2646 : struct xfs_attr3_icleaf_hdr *ichdr_d,
2647 : int start_d,
2648 : int count)
2649 : {
2650 470518 : struct xfs_attr_leaf_entry *entry_s;
2651 470518 : struct xfs_attr_leaf_entry *entry_d;
2652 470518 : int desti;
2653 470518 : int tmp;
2654 470518 : int i;
2655 :
2656 : /*
2657 : * Check for nothing to do.
2658 : */
2659 470518 : if (count == 0)
2660 : return;
2661 :
2662 : /*
2663 : * Set up environment.
2664 : */
2665 470518 : ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC ||
2666 : ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC);
2667 470518 : ASSERT(ichdr_s->magic == ichdr_d->magic);
2668 470518 : ASSERT(ichdr_s->count > 0 && ichdr_s->count < args->geo->blksize / 8);
2669 941037 : ASSERT(ichdr_s->firstused >= (ichdr_s->count * sizeof(*entry_s))
2670 : + xfs_attr3_leaf_hdr_size(leaf_s));
2671 470518 : ASSERT(ichdr_d->count < args->geo->blksize / 8);
2672 941036 : ASSERT(ichdr_d->firstused >= (ichdr_d->count * sizeof(*entry_d))
2673 : + xfs_attr3_leaf_hdr_size(leaf_d));
2674 :
2675 470518 : ASSERT(start_s < ichdr_s->count);
2676 470518 : ASSERT(start_d <= ichdr_d->count);
2677 470518 : ASSERT(count <= ichdr_s->count);
2678 :
2679 :
2680 : /*
2681 : * Move the entries in the destination leaf up to make a hole?
2682 : */
2683 470518 : if (start_d < ichdr_d->count) {
2684 0 : tmp = ichdr_d->count - start_d;
2685 0 : tmp *= sizeof(xfs_attr_leaf_entry_t);
2686 0 : entry_s = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2687 0 : entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d + count];
2688 0 : memmove(entry_d, entry_s, tmp);
2689 : }
2690 :
2691 : /*
2692 : * Copy all entry's in the same (sorted) order,
2693 : * but allocate attribute info packed and in sequence.
2694 : */
2695 470518 : entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2696 470518 : entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2697 470518 : desti = start_d;
2698 17692918 : for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
2699 17222395 : ASSERT(be16_to_cpu(entry_s->nameidx) >= ichdr_s->firstused);
2700 17222395 : tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2701 : #ifdef GROT
2702 : /*
2703 : * Code to drop INCOMPLETE entries. Difficult to use as we
2704 : * may also need to change the insertion index. Code turned
2705 : * off for 6.2, should be revisited later.
2706 : */
2707 : if (entry_s->flags & XFS_ATTR_INCOMPLETE) { /* skip partials? */
2708 : memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2709 : ichdr_s->usedbytes -= tmp;
2710 : ichdr_s->count -= 1;
2711 : entry_d--; /* to compensate for ++ in loop hdr */
2712 : desti--;
2713 : if ((start_s + i) < offset)
2714 : result++; /* insertion index adjustment */
2715 : } else {
2716 : #endif /* GROT */
2717 17222395 : ichdr_d->firstused -= tmp;
2718 : /* both on-disk, don't endian flip twice */
2719 17222395 : entry_d->hashval = entry_s->hashval;
2720 17222395 : entry_d->nameidx = cpu_to_be16(ichdr_d->firstused);
2721 17222395 : entry_d->flags = entry_s->flags;
2722 17222395 : ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
2723 : <= args->geo->blksize);
2724 17222395 : memmove(xfs_attr3_leaf_name(leaf_d, desti),
2725 : xfs_attr3_leaf_name(leaf_s, start_s + i), tmp);
2726 17222395 : ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
2727 : <= args->geo->blksize);
2728 17222395 : memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2729 17222395 : ichdr_s->usedbytes -= tmp;
2730 17222395 : ichdr_d->usedbytes += tmp;
2731 17222395 : ichdr_s->count -= 1;
2732 17222395 : ichdr_d->count += 1;
2733 17222395 : tmp = ichdr_d->count * sizeof(xfs_attr_leaf_entry_t)
2734 17222395 : + xfs_attr3_leaf_hdr_size(leaf_d);
2735 17222395 : ASSERT(ichdr_d->firstused >= tmp);
2736 : #ifdef GROT
2737 : }
2738 : #endif /* GROT */
2739 : }
2740 :
2741 : /*
2742 : * Zero out the entries we just copied.
2743 : */
2744 470523 : if (start_s == ichdr_s->count) {
2745 470523 : tmp = count * sizeof(xfs_attr_leaf_entry_t);
2746 470523 : entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2747 470523 : ASSERT(((char *)entry_s + tmp) <=
2748 : ((char *)leaf_s + args->geo->blksize));
2749 941046 : memset(entry_s, 0, tmp);
2750 : } else {
2751 : /*
2752 : * Move the remaining entries down to fill the hole,
2753 : * then zero the entries at the top.
2754 : */
2755 0 : tmp = (ichdr_s->count - count) * sizeof(xfs_attr_leaf_entry_t);
2756 0 : entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s + count];
2757 0 : entry_d = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2758 0 : memmove(entry_d, entry_s, tmp);
2759 :
2760 0 : tmp = count * sizeof(xfs_attr_leaf_entry_t);
2761 0 : entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count];
2762 0 : ASSERT(((char *)entry_s + tmp) <=
2763 : ((char *)leaf_s + args->geo->blksize));
2764 0 : memset(entry_s, 0, tmp);
2765 : }
2766 :
2767 : /*
2768 : * Fill in the freemap information
2769 : */
2770 470523 : ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_d);
2771 470523 : ichdr_d->freemap[0].base += ichdr_d->count * sizeof(xfs_attr_leaf_entry_t);
2772 470523 : ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base;
2773 470523 : ichdr_d->freemap[1].base = 0;
2774 470523 : ichdr_d->freemap[2].base = 0;
2775 470523 : ichdr_d->freemap[1].size = 0;
2776 470523 : ichdr_d->freemap[2].size = 0;
2777 470523 : ichdr_s->holes = 1; /* leaf may not be compact */
2778 : }
2779 :
2780 : /*
2781 : * Pick up the last hashvalue from a leaf block.
2782 : */
2783 : xfs_dahash_t
2784 486378108 : xfs_attr_leaf_lasthash(
2785 : struct xfs_buf *bp,
2786 : int *count)
2787 : {
2788 486378108 : struct xfs_attr3_icleaf_hdr ichdr;
2789 486378108 : struct xfs_attr_leaf_entry *entries;
2790 486378108 : struct xfs_mount *mp = bp->b_mount;
2791 :
2792 486378108 : xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, bp->b_addr);
2793 486384128 : entries = xfs_attr3_leaf_entryp(bp->b_addr);
2794 486384128 : if (count)
2795 4845013 : *count = ichdr.count;
2796 486384128 : if (!ichdr.count)
2797 : return 0;
2798 486384095 : return be32_to_cpu(entries[ichdr.count - 1].hashval);
2799 : }
2800 :
2801 : /*
2802 : * Calculate the number of bytes used to store the indicated attribute
2803 : * (whether local or remote only calculate bytes in this block).
2804 : */
2805 : STATIC int
2806 275117884 : xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2807 : {
2808 275117884 : struct xfs_attr_leaf_entry *entries;
2809 275117884 : xfs_attr_leaf_name_local_t *name_loc;
2810 275117884 : xfs_attr_leaf_name_remote_t *name_rmt;
2811 275117884 : int size;
2812 :
2813 275117884 : entries = xfs_attr3_leaf_entryp(leaf);
2814 275117884 : if (entries[index].flags & XFS_ATTR_LOCAL) {
2815 275111870 : name_loc = xfs_attr3_leaf_name_local(leaf, index);
2816 275111870 : size = xfs_attr_leaf_entsize_local(name_loc->namelen,
2817 275111870 : be16_to_cpu(name_loc->valuelen));
2818 : } else {
2819 6014 : name_rmt = xfs_attr3_leaf_name_remote(leaf, index);
2820 6014 : size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
2821 : }
2822 275117884 : return size;
2823 : }
2824 :
2825 : /*
2826 : * Calculate the number of bytes that would be required to store the new
2827 : * attribute (whether local or remote only calculate bytes in this block).
2828 : * This routine decides as a side effect whether the attribute will be
2829 : * a "local" or a "remote" attribute.
2830 : */
2831 : int
2832 437340763 : xfs_attr_leaf_newentsize(
2833 : struct xfs_da_args *args,
2834 : int *local)
2835 : {
2836 437340763 : int size;
2837 :
2838 437340763 : size = xfs_attr_leaf_entsize_local(args->namelen, args->valuelen);
2839 437340763 : if (size < xfs_attr_leaf_entsize_local_max(args->geo->blksize)) {
2840 437328561 : if (local)
2841 230107876 : *local = 1;
2842 437328561 : return size;
2843 : }
2844 12202 : if (local)
2845 6276 : *local = 0;
2846 12202 : return xfs_attr_leaf_entsize_remote(args->namelen);
2847 : }
2848 :
2849 :
2850 : /*========================================================================
2851 : * Manage the INCOMPLETE flag in a leaf entry
2852 : *========================================================================*/
2853 :
2854 : /*
2855 : * Clear the INCOMPLETE flag on an entry in a leaf block.
2856 : */
2857 : int
2858 2953 : xfs_attr3_leaf_clearflag(
2859 : struct xfs_da_args *args)
2860 : {
2861 2953 : struct xfs_attr_leafblock *leaf;
2862 2953 : struct xfs_attr_leaf_entry *entry;
2863 2953 : struct xfs_attr_leaf_name_remote *name_rmt;
2864 2953 : struct xfs_buf *bp;
2865 2953 : int error;
2866 : #ifdef DEBUG
2867 2953 : struct xfs_attr3_icleaf_hdr ichdr;
2868 2953 : xfs_attr_leaf_name_local_t *name_loc;
2869 2953 : int namelen;
2870 2953 : char *name;
2871 : #endif /* DEBUG */
2872 :
2873 2953 : trace_xfs_attr_leaf_clearflag(args);
2874 : /*
2875 : * Set up the operation.
2876 : */
2877 2953 : error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner,
2878 : args->blkno, &bp);
2879 2953 : if (error)
2880 : return error;
2881 :
2882 2953 : leaf = bp->b_addr;
2883 2953 : entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2884 2953 : ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2885 :
2886 : #ifdef DEBUG
2887 2953 : xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2888 2953 : ASSERT(args->index < ichdr.count);
2889 2953 : ASSERT(args->index >= 0);
2890 :
2891 2953 : if (entry->flags & XFS_ATTR_LOCAL) {
2892 0 : name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
2893 0 : namelen = name_loc->namelen;
2894 0 : name = (char *)name_loc->nameval;
2895 : } else {
2896 2953 : name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2897 2953 : namelen = name_rmt->namelen;
2898 2953 : name = (char *)name_rmt->name;
2899 : }
2900 2953 : ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
2901 2953 : ASSERT(namelen == args->namelen);
2902 5906 : ASSERT(memcmp(name, args->name, namelen) == 0);
2903 : #endif /* DEBUG */
2904 :
2905 2953 : entry->flags &= ~XFS_ATTR_INCOMPLETE;
2906 2953 : xfs_trans_log_buf(args->trans, bp,
2907 2953 : XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2908 :
2909 2953 : if (args->rmtblkno) {
2910 2953 : ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2911 2953 : name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2912 2953 : name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2913 2953 : name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
2914 2953 : xfs_trans_log_buf(args->trans, bp,
2915 : XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2916 : }
2917 :
2918 : return 0;
2919 : }
2920 :
2921 : /*
2922 : * Set the INCOMPLETE flag on an entry in a leaf block.
2923 : */
2924 : int
2925 688930 : xfs_attr3_leaf_setflag(
2926 : struct xfs_da_args *args)
2927 : {
2928 688930 : struct xfs_attr_leafblock *leaf;
2929 688930 : struct xfs_attr_leaf_entry *entry;
2930 688930 : struct xfs_attr_leaf_name_remote *name_rmt;
2931 688930 : struct xfs_buf *bp;
2932 688930 : int error;
2933 : #ifdef DEBUG
2934 688930 : struct xfs_attr3_icleaf_hdr ichdr;
2935 : #endif
2936 :
2937 688930 : trace_xfs_attr_leaf_setflag(args);
2938 :
2939 : /*
2940 : * Set up the operation.
2941 : */
2942 688919 : error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner,
2943 : args->blkno, &bp);
2944 688951 : if (error)
2945 : return error;
2946 :
2947 688953 : leaf = bp->b_addr;
2948 : #ifdef DEBUG
2949 688953 : xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2950 688937 : ASSERT(args->index < ichdr.count);
2951 688937 : ASSERT(args->index >= 0);
2952 : #endif
2953 688937 : entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2954 :
2955 688937 : ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2956 688937 : entry->flags |= XFS_ATTR_INCOMPLETE;
2957 688937 : xfs_trans_log_buf(args->trans, bp,
2958 688937 : XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2959 688947 : if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
2960 68 : name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2961 68 : name_rmt->valueblk = 0;
2962 68 : name_rmt->valuelen = 0;
2963 68 : xfs_trans_log_buf(args->trans, bp,
2964 : XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2965 : }
2966 :
2967 : return 0;
2968 : }
2969 :
2970 : /*
2971 : * In a single transaction, clear the INCOMPLETE flag on the leaf entry
2972 : * given by args->blkno/index and set the INCOMPLETE flag on the leaf
2973 : * entry given by args->blkno2/index2.
2974 : *
2975 : * Note that they could be in different blocks, or in the same block.
2976 : */
2977 : int
2978 17912362 : xfs_attr3_leaf_flipflags(
2979 : struct xfs_da_args *args)
2980 : {
2981 17912362 : struct xfs_attr_leafblock *leaf1;
2982 17912362 : struct xfs_attr_leafblock *leaf2;
2983 17912362 : struct xfs_attr_leaf_entry *entry1;
2984 17912362 : struct xfs_attr_leaf_entry *entry2;
2985 17912362 : struct xfs_attr_leaf_name_remote *name_rmt;
2986 17912362 : struct xfs_buf *bp1;
2987 17912362 : struct xfs_buf *bp2;
2988 17912362 : int error;
2989 : #ifdef DEBUG
2990 17912362 : struct xfs_attr3_icleaf_hdr ichdr1;
2991 17912362 : struct xfs_attr3_icleaf_hdr ichdr2;
2992 17912362 : xfs_attr_leaf_name_local_t *name_loc;
2993 17912362 : int namelen1, namelen2;
2994 17912362 : char *name1, *name2;
2995 : #endif /* DEBUG */
2996 :
2997 17912362 : trace_xfs_attr_leaf_flipflags(args);
2998 :
2999 : /*
3000 : * Read the block containing the "old" attr
3001 : */
3002 17909253 : error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner,
3003 : args->blkno, &bp1);
3004 17913689 : if (error)
3005 : return error;
3006 :
3007 : /*
3008 : * Read the block containing the "new" attr, if it is different
3009 : */
3010 17913689 : if (args->blkno2 != args->blkno) {
3011 205 : error = xfs_attr3_leaf_read(args->trans, args->dp, args->owner,
3012 : args->blkno2, &bp2);
3013 205 : if (error)
3014 : return error;
3015 : } else {
3016 17913484 : bp2 = bp1;
3017 : }
3018 :
3019 17913689 : leaf1 = bp1->b_addr;
3020 17913689 : entry1 = &xfs_attr3_leaf_entryp(leaf1)[args->index];
3021 :
3022 17913689 : leaf2 = bp2->b_addr;
3023 17913689 : entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2];
3024 :
3025 : #ifdef DEBUG
3026 17913689 : xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr1, leaf1);
3027 17911051 : ASSERT(args->index < ichdr1.count);
3028 17911051 : ASSERT(args->index >= 0);
3029 :
3030 17911051 : xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr2, leaf2);
3031 17911089 : ASSERT(args->index2 < ichdr2.count);
3032 17911089 : ASSERT(args->index2 >= 0);
3033 :
3034 17911089 : if (entry1->flags & XFS_ATTR_LOCAL) {
3035 17911079 : name_loc = xfs_attr3_leaf_name_local(leaf1, args->index);
3036 17911079 : namelen1 = name_loc->namelen;
3037 17911079 : name1 = (char *)name_loc->nameval;
3038 : } else {
3039 10 : name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
3040 10 : namelen1 = name_rmt->namelen;
3041 10 : name1 = (char *)name_rmt->name;
3042 : }
3043 17911089 : if (entry2->flags & XFS_ATTR_LOCAL) {
3044 17910984 : name_loc = xfs_attr3_leaf_name_local(leaf2, args->index2);
3045 17910984 : namelen2 = name_loc->namelen;
3046 17910984 : name2 = (char *)name_loc->nameval;
3047 : } else {
3048 110 : name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
3049 110 : namelen2 = name_rmt->namelen;
3050 110 : name2 = (char *)name_rmt->name;
3051 : }
3052 17911089 : ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
3053 17911089 : ASSERT(namelen1 == namelen2);
3054 35822178 : ASSERT(memcmp(name1, name2, namelen1) == 0);
3055 : #endif /* DEBUG */
3056 :
3057 17911089 : ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
3058 17911089 : ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
3059 :
3060 17911089 : entry1->flags &= ~XFS_ATTR_INCOMPLETE;
3061 17911089 : xfs_trans_log_buf(args->trans, bp1,
3062 17911089 : XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
3063 17914050 : if (args->rmtblkno) {
3064 10 : ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
3065 10 : name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
3066 10 : name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
3067 10 : name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
3068 10 : xfs_trans_log_buf(args->trans, bp1,
3069 : XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
3070 : }
3071 :
3072 17914050 : entry2->flags |= XFS_ATTR_INCOMPLETE;
3073 17914050 : xfs_trans_log_buf(args->trans, bp2,
3074 17914050 : XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
3075 17914054 : if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
3076 20 : name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
3077 20 : name_rmt->valueblk = 0;
3078 20 : name_rmt->valuelen = 0;
3079 20 : xfs_trans_log_buf(args->trans, bp2,
3080 : XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
3081 : }
3082 :
3083 : return 0;
3084 : }
|