1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include "xfs.h"
20#include "xfs_fs.h"
21#include "xfs_shared.h"
22#include "xfs_format.h"
23#include "xfs_log_format.h"
24#include "xfs_trans_resv.h"
25#include "xfs_bit.h"
26#include "xfs_sb.h"
27#include "xfs_mount.h"
28#include "xfs_da_format.h"
29#include "xfs_da_btree.h"
30#include "xfs_inode.h"
31#include "xfs_trans.h"
32#include "xfs_inode_item.h"
33#include "xfs_bmap_btree.h"
34#include "xfs_bmap.h"
35#include "xfs_attr_sf.h"
36#include "xfs_attr_remote.h"
37#include "xfs_attr.h"
38#include "xfs_attr_leaf.h"
39#include "xfs_error.h"
40#include "xfs_trace.h"
41#include "xfs_buf_item.h"
42#include "xfs_cksum.h"
43#include "xfs_dir2.h"
44#include "xfs_log.h"
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60STATIC int xfs_attr3_leaf_create(struct xfs_da_args *args,
61 xfs_dablk_t which_block, struct xfs_buf **bpp);
62STATIC int xfs_attr3_leaf_add_work(struct xfs_buf *leaf_buffer,
63 struct xfs_attr3_icleaf_hdr *ichdr,
64 struct xfs_da_args *args, int freemap_index);
65STATIC void xfs_attr3_leaf_compact(struct xfs_da_args *args,
66 struct xfs_attr3_icleaf_hdr *ichdr,
67 struct xfs_buf *leaf_buffer);
68STATIC void xfs_attr3_leaf_rebalance(xfs_da_state_t *state,
69 xfs_da_state_blk_t *blk1,
70 xfs_da_state_blk_t *blk2);
71STATIC int xfs_attr3_leaf_figure_balance(xfs_da_state_t *state,
72 xfs_da_state_blk_t *leaf_blk_1,
73 struct xfs_attr3_icleaf_hdr *ichdr1,
74 xfs_da_state_blk_t *leaf_blk_2,
75 struct xfs_attr3_icleaf_hdr *ichdr2,
76 int *number_entries_in_blk1,
77 int *number_usedbytes_in_blk1);
78
79
80
81
82STATIC void xfs_attr3_leaf_moveents(struct xfs_da_args *args,
83 struct xfs_attr_leafblock *src_leaf,
84 struct xfs_attr3_icleaf_hdr *src_ichdr, int src_start,
85 struct xfs_attr_leafblock *dst_leaf,
86 struct xfs_attr3_icleaf_hdr *dst_ichdr, int dst_start,
87 int move_count);
88STATIC int xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index);
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106static void
107xfs_attr3_leaf_firstused_from_disk(
108 struct xfs_da_geometry *geo,
109 struct xfs_attr3_icleaf_hdr *to,
110 struct xfs_attr_leafblock *from)
111{
112 struct xfs_attr3_leaf_hdr *hdr3;
113
114 if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
115 hdr3 = (struct xfs_attr3_leaf_hdr *) from;
116 to->firstused = be16_to_cpu(hdr3->firstused);
117 } else {
118 to->firstused = be16_to_cpu(from->hdr.firstused);
119 }
120
121
122
123
124
125
126 if (to->firstused == XFS_ATTR3_LEAF_NULLOFF) {
127 ASSERT(!to->count && !to->usedbytes);
128 ASSERT(geo->blksize > USHRT_MAX);
129 to->firstused = geo->blksize;
130 }
131}
132
133static void
134xfs_attr3_leaf_firstused_to_disk(
135 struct xfs_da_geometry *geo,
136 struct xfs_attr_leafblock *to,
137 struct xfs_attr3_icleaf_hdr *from)
138{
139 struct xfs_attr3_leaf_hdr *hdr3;
140 uint32_t firstused;
141
142
143 ASSERT(from->firstused != XFS_ATTR3_LEAF_NULLOFF);
144
145
146
147
148
149
150 firstused = from->firstused;
151 if (firstused > USHRT_MAX) {
152 ASSERT(from->firstused == geo->blksize);
153 firstused = XFS_ATTR3_LEAF_NULLOFF;
154 }
155
156 if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
157 hdr3 = (struct xfs_attr3_leaf_hdr *) to;
158 hdr3->firstused = cpu_to_be16(firstused);
159 } else {
160 to->hdr.firstused = cpu_to_be16(firstused);
161 }
162}
163
164void
165xfs_attr3_leaf_hdr_from_disk(
166 struct xfs_da_geometry *geo,
167 struct xfs_attr3_icleaf_hdr *to,
168 struct xfs_attr_leafblock *from)
169{
170 int i;
171
172 ASSERT(from->hdr.info.magic == cpu_to_be16(XFS_ATTR_LEAF_MAGIC) ||
173 from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC));
174
175 if (from->hdr.info.magic == cpu_to_be16(XFS_ATTR3_LEAF_MAGIC)) {
176 struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)from;
177
178 to->forw = be32_to_cpu(hdr3->info.hdr.forw);
179 to->back = be32_to_cpu(hdr3->info.hdr.back);
180 to->magic = be16_to_cpu(hdr3->info.hdr.magic);
181 to->count = be16_to_cpu(hdr3->count);
182 to->usedbytes = be16_to_cpu(hdr3->usedbytes);
183 xfs_attr3_leaf_firstused_from_disk(geo, to, from);
184 to->holes = hdr3->holes;
185
186 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
187 to->freemap[i].base = be16_to_cpu(hdr3->freemap[i].base);
188 to->freemap[i].size = be16_to_cpu(hdr3->freemap[i].size);
189 }
190 return;
191 }
192 to->forw = be32_to_cpu(from->hdr.info.forw);
193 to->back = be32_to_cpu(from->hdr.info.back);
194 to->magic = be16_to_cpu(from->hdr.info.magic);
195 to->count = be16_to_cpu(from->hdr.count);
196 to->usedbytes = be16_to_cpu(from->hdr.usedbytes);
197 xfs_attr3_leaf_firstused_from_disk(geo, to, from);
198 to->holes = from->hdr.holes;
199
200 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
201 to->freemap[i].base = be16_to_cpu(from->hdr.freemap[i].base);
202 to->freemap[i].size = be16_to_cpu(from->hdr.freemap[i].size);
203 }
204}
205
206void
207xfs_attr3_leaf_hdr_to_disk(
208 struct xfs_da_geometry *geo,
209 struct xfs_attr_leafblock *to,
210 struct xfs_attr3_icleaf_hdr *from)
211{
212 int i;
213
214 ASSERT(from->magic == XFS_ATTR_LEAF_MAGIC ||
215 from->magic == XFS_ATTR3_LEAF_MAGIC);
216
217 if (from->magic == XFS_ATTR3_LEAF_MAGIC) {
218 struct xfs_attr3_leaf_hdr *hdr3 = (struct xfs_attr3_leaf_hdr *)to;
219
220 hdr3->info.hdr.forw = cpu_to_be32(from->forw);
221 hdr3->info.hdr.back = cpu_to_be32(from->back);
222 hdr3->info.hdr.magic = cpu_to_be16(from->magic);
223 hdr3->count = cpu_to_be16(from->count);
224 hdr3->usedbytes = cpu_to_be16(from->usedbytes);
225 xfs_attr3_leaf_firstused_to_disk(geo, to, from);
226 hdr3->holes = from->holes;
227 hdr3->pad1 = 0;
228
229 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
230 hdr3->freemap[i].base = cpu_to_be16(from->freemap[i].base);
231 hdr3->freemap[i].size = cpu_to_be16(from->freemap[i].size);
232 }
233 return;
234 }
235 to->hdr.info.forw = cpu_to_be32(from->forw);
236 to->hdr.info.back = cpu_to_be32(from->back);
237 to->hdr.info.magic = cpu_to_be16(from->magic);
238 to->hdr.count = cpu_to_be16(from->count);
239 to->hdr.usedbytes = cpu_to_be16(from->usedbytes);
240 xfs_attr3_leaf_firstused_to_disk(geo, to, from);
241 to->hdr.holes = from->holes;
242 to->hdr.pad1 = 0;
243
244 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
245 to->hdr.freemap[i].base = cpu_to_be16(from->freemap[i].base);
246 to->hdr.freemap[i].size = cpu_to_be16(from->freemap[i].size);
247 }
248}
249
250static xfs_failaddr_t
251xfs_attr3_leaf_verify(
252 struct xfs_buf *bp)
253{
254 struct xfs_attr3_icleaf_hdr ichdr;
255 struct xfs_mount *mp = bp->b_target->bt_mount;
256 struct xfs_attr_leafblock *leaf = bp->b_addr;
257 struct xfs_perag *pag = bp->b_pag;
258 struct xfs_attr_leaf_entry *entries;
259
260 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
261
262 if (xfs_sb_version_hascrc(&mp->m_sb)) {
263 struct xfs_da3_node_hdr *hdr3 = bp->b_addr;
264
265 if (ichdr.magic != XFS_ATTR3_LEAF_MAGIC)
266 return __this_address;
267
268 if (!uuid_equal(&hdr3->info.uuid, &mp->m_sb.sb_meta_uuid))
269 return __this_address;
270 if (be64_to_cpu(hdr3->info.blkno) != bp->b_bn)
271 return __this_address;
272 if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->info.lsn)))
273 return __this_address;
274 } else {
275 if (ichdr.magic != XFS_ATTR_LEAF_MAGIC)
276 return __this_address;
277 }
278
279
280
281
282
283 if (pag && pag->pagf_init && ichdr.count == 0)
284 return __this_address;
285
286
287
288
289
290 if (ichdr.firstused > mp->m_attr_geo->blksize)
291 return __this_address;
292 if (ichdr.firstused < xfs_attr3_leaf_hdr_size(leaf))
293 return __this_address;
294
295
296 entries = xfs_attr3_leaf_entryp(bp->b_addr);
297 if ((char *)&entries[ichdr.count] >
298 (char *)bp->b_addr + ichdr.firstused)
299 return __this_address;
300
301
302
303
304 return NULL;
305}
306
307static void
308xfs_attr3_leaf_write_verify(
309 struct xfs_buf *bp)
310{
311 struct xfs_mount *mp = bp->b_target->bt_mount;
312 struct xfs_buf_log_item *bip = bp->b_log_item;
313 struct xfs_attr3_leaf_hdr *hdr3 = bp->b_addr;
314 xfs_failaddr_t fa;
315
316 fa = xfs_attr3_leaf_verify(bp);
317 if (fa) {
318 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
319 return;
320 }
321
322 if (!xfs_sb_version_hascrc(&mp->m_sb))
323 return;
324
325 if (bip)
326 hdr3->info.lsn = cpu_to_be64(bip->bli_item.li_lsn);
327
328 xfs_buf_update_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF);
329}
330
331
332
333
334
335
336
337static void
338xfs_attr3_leaf_read_verify(
339 struct xfs_buf *bp)
340{
341 struct xfs_mount *mp = bp->b_target->bt_mount;
342 xfs_failaddr_t fa;
343
344 if (xfs_sb_version_hascrc(&mp->m_sb) &&
345 !xfs_buf_verify_cksum(bp, XFS_ATTR3_LEAF_CRC_OFF))
346 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
347 else {
348 fa = xfs_attr3_leaf_verify(bp);
349 if (fa)
350 xfs_verifier_error(bp, -EFSCORRUPTED, fa);
351 }
352}
353
354const struct xfs_buf_ops xfs_attr3_leaf_buf_ops = {
355 .name = "xfs_attr3_leaf",
356 .verify_read = xfs_attr3_leaf_read_verify,
357 .verify_write = xfs_attr3_leaf_write_verify,
358 .verify_struct = xfs_attr3_leaf_verify,
359};
360
361int
362xfs_attr3_leaf_read(
363 struct xfs_trans *tp,
364 struct xfs_inode *dp,
365 xfs_dablk_t bno,
366 xfs_daddr_t mappedbno,
367 struct xfs_buf **bpp)
368{
369 int err;
370
371 err = xfs_da_read_buf(tp, dp, bno, mappedbno, bpp,
372 XFS_ATTR_FORK, &xfs_attr3_leaf_buf_ops);
373 if (!err && tp && *bpp)
374 xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_ATTR_LEAF_BUF);
375 return err;
376}
377
378
379
380
381
382
383
384
385
386STATIC int
387xfs_attr_namesp_match(int arg_flags, int ondisk_flags)
388{
389 return XFS_ATTR_NSP_ONDISK(ondisk_flags) == XFS_ATTR_NSP_ARGS_TO_ONDISK(arg_flags);
390}
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407int
408xfs_attr_shortform_bytesfit(xfs_inode_t *dp, int bytes)
409{
410 int offset;
411 int minforkoff;
412 int maxforkoff;
413 int dsize;
414 xfs_mount_t *mp = dp->i_mount;
415
416
417 offset = (XFS_LITINO(mp, dp->i_d.di_version) - bytes) >> 3;
418
419 if (dp->i_d.di_format == XFS_DINODE_FMT_DEV) {
420 minforkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
421 return (offset >= minforkoff) ? minforkoff : 0;
422 }
423
424
425
426
427
428
429
430
431
432
433
434 if (bytes <= XFS_IFORK_ASIZE(dp))
435 return dp->i_d.di_forkoff;
436
437
438
439
440
441
442 if (!(mp->m_flags & XFS_MOUNT_ATTR2))
443 return 0;
444
445 dsize = dp->i_df.if_bytes;
446
447 switch (dp->i_d.di_format) {
448 case XFS_DINODE_FMT_EXTENTS:
449
450
451
452
453
454
455
456 if (!dp->i_d.di_forkoff && dp->i_df.if_bytes >
457 xfs_default_attroffset(dp))
458 dsize = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
459 break;
460 case XFS_DINODE_FMT_BTREE:
461
462
463
464
465
466
467 if (dp->i_d.di_forkoff) {
468 if (offset < dp->i_d.di_forkoff)
469 return 0;
470 return dp->i_d.di_forkoff;
471 }
472 dsize = XFS_BMAP_BROOT_SPACE(mp, dp->i_df.if_broot);
473 break;
474 }
475
476
477
478
479
480 minforkoff = MAX(dsize, XFS_BMDR_SPACE_CALC(MINDBTPTRS));
481 minforkoff = roundup(minforkoff, 8) >> 3;
482
483
484 maxforkoff = XFS_LITINO(mp, dp->i_d.di_version) -
485 XFS_BMDR_SPACE_CALC(MINABTPTRS);
486 maxforkoff = maxforkoff >> 3;
487
488 if (offset >= maxforkoff)
489 return maxforkoff;
490 if (offset >= minforkoff)
491 return offset;
492 return 0;
493}
494
495
496
497
498STATIC void
499xfs_sbversion_add_attr2(xfs_mount_t *mp, xfs_trans_t *tp)
500{
501 if ((mp->m_flags & XFS_MOUNT_ATTR2) &&
502 !(xfs_sb_version_hasattr2(&mp->m_sb))) {
503 spin_lock(&mp->m_sb_lock);
504 if (!xfs_sb_version_hasattr2(&mp->m_sb)) {
505 xfs_sb_version_addattr2(&mp->m_sb);
506 spin_unlock(&mp->m_sb_lock);
507 xfs_log_sb(tp);
508 } else
509 spin_unlock(&mp->m_sb_lock);
510 }
511}
512
513
514
515
516void
517xfs_attr_shortform_create(xfs_da_args_t *args)
518{
519 xfs_attr_sf_hdr_t *hdr;
520 xfs_inode_t *dp;
521 xfs_ifork_t *ifp;
522
523 trace_xfs_attr_sf_create(args);
524
525 dp = args->dp;
526 ASSERT(dp != NULL);
527 ifp = dp->i_afp;
528 ASSERT(ifp != NULL);
529 ASSERT(ifp->if_bytes == 0);
530 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) {
531 ifp->if_flags &= ~XFS_IFEXTENTS;
532 dp->i_d.di_aformat = XFS_DINODE_FMT_LOCAL;
533 ifp->if_flags |= XFS_IFINLINE;
534 } else {
535 ASSERT(ifp->if_flags & XFS_IFINLINE);
536 }
537 xfs_idata_realloc(dp, sizeof(*hdr), XFS_ATTR_FORK);
538 hdr = (xfs_attr_sf_hdr_t *)ifp->if_u1.if_data;
539 hdr->count = 0;
540 hdr->totsize = cpu_to_be16(sizeof(*hdr));
541 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
542}
543
544
545
546
547
548void
549xfs_attr_shortform_add(xfs_da_args_t *args, int forkoff)
550{
551 xfs_attr_shortform_t *sf;
552 xfs_attr_sf_entry_t *sfe;
553 int i, offset, size;
554 xfs_mount_t *mp;
555 xfs_inode_t *dp;
556 xfs_ifork_t *ifp;
557
558 trace_xfs_attr_sf_add(args);
559
560 dp = args->dp;
561 mp = dp->i_mount;
562 dp->i_d.di_forkoff = forkoff;
563
564 ifp = dp->i_afp;
565 ASSERT(ifp->if_flags & XFS_IFINLINE);
566 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
567 sfe = &sf->list[0];
568 for (i = 0; i < sf->hdr.count; sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
569#ifdef DEBUG
570 if (sfe->namelen != args->namelen)
571 continue;
572 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
573 continue;
574 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
575 continue;
576 ASSERT(0);
577#endif
578 }
579
580 offset = (char *)sfe - (char *)sf;
581 size = XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
582 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
583 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
584 sfe = (xfs_attr_sf_entry_t *)((char *)sf + offset);
585
586 sfe->namelen = args->namelen;
587 sfe->valuelen = args->valuelen;
588 sfe->flags = XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
589 memcpy(sfe->nameval, args->name, args->namelen);
590 memcpy(&sfe->nameval[args->namelen], args->value, args->valuelen);
591 sf->hdr.count++;
592 be16_add_cpu(&sf->hdr.totsize, size);
593 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE | XFS_ILOG_ADATA);
594
595 xfs_sbversion_add_attr2(mp, args->trans);
596}
597
598
599
600
601
602void
603xfs_attr_fork_remove(
604 struct xfs_inode *ip,
605 struct xfs_trans *tp)
606{
607 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
608 ip->i_d.di_forkoff = 0;
609 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
610
611 ASSERT(ip->i_d.di_anextents == 0);
612 ASSERT(ip->i_afp == NULL);
613
614 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
615}
616
617
618
619
620int
621xfs_attr_shortform_remove(xfs_da_args_t *args)
622{
623 xfs_attr_shortform_t *sf;
624 xfs_attr_sf_entry_t *sfe;
625 int base, size=0, end, totsize, i;
626 xfs_mount_t *mp;
627 xfs_inode_t *dp;
628
629 trace_xfs_attr_sf_remove(args);
630
631 dp = args->dp;
632 mp = dp->i_mount;
633 base = sizeof(xfs_attr_sf_hdr_t);
634 sf = (xfs_attr_shortform_t *)dp->i_afp->if_u1.if_data;
635 sfe = &sf->list[0];
636 end = sf->hdr.count;
637 for (i = 0; i < end; sfe = XFS_ATTR_SF_NEXTENTRY(sfe),
638 base += size, i++) {
639 size = XFS_ATTR_SF_ENTSIZE(sfe);
640 if (sfe->namelen != args->namelen)
641 continue;
642 if (memcmp(sfe->nameval, args->name, args->namelen) != 0)
643 continue;
644 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
645 continue;
646 break;
647 }
648 if (i == end)
649 return -ENOATTR;
650
651
652
653
654 end = base + size;
655 totsize = be16_to_cpu(sf->hdr.totsize);
656 if (end != totsize)
657 memmove(&((char *)sf)[base], &((char *)sf)[end], totsize - end);
658 sf->hdr.count--;
659 be16_add_cpu(&sf->hdr.totsize, -size);
660
661
662
663
664 totsize -= size;
665 if (totsize == sizeof(xfs_attr_sf_hdr_t) &&
666 (mp->m_flags & XFS_MOUNT_ATTR2) &&
667 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
668 !(args->op_flags & XFS_DA_OP_ADDNAME)) {
669 xfs_attr_fork_remove(dp, args->trans);
670 } else {
671 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
672 dp->i_d.di_forkoff = xfs_attr_shortform_bytesfit(dp, totsize);
673 ASSERT(dp->i_d.di_forkoff);
674 ASSERT(totsize > sizeof(xfs_attr_sf_hdr_t) ||
675 (args->op_flags & XFS_DA_OP_ADDNAME) ||
676 !(mp->m_flags & XFS_MOUNT_ATTR2) ||
677 dp->i_d.di_format == XFS_DINODE_FMT_BTREE);
678 xfs_trans_log_inode(args->trans, dp,
679 XFS_ILOG_CORE | XFS_ILOG_ADATA);
680 }
681
682 xfs_sbversion_add_attr2(mp, args->trans);
683
684 return 0;
685}
686
687
688
689
690
691int
692xfs_attr_shortform_lookup(xfs_da_args_t *args)
693{
694 xfs_attr_shortform_t *sf;
695 xfs_attr_sf_entry_t *sfe;
696 int i;
697 xfs_ifork_t *ifp;
698
699 trace_xfs_attr_sf_lookup(args);
700
701 ifp = args->dp->i_afp;
702 ASSERT(ifp->if_flags & XFS_IFINLINE);
703 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
704 sfe = &sf->list[0];
705 for (i = 0; i < sf->hdr.count;
706 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
707 if (sfe->namelen != args->namelen)
708 continue;
709 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
710 continue;
711 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
712 continue;
713 return -EEXIST;
714 }
715 return -ENOATTR;
716}
717
718
719
720
721
722int
723xfs_attr_shortform_getvalue(xfs_da_args_t *args)
724{
725 xfs_attr_shortform_t *sf;
726 xfs_attr_sf_entry_t *sfe;
727 int i;
728
729 ASSERT(args->dp->i_afp->if_flags == XFS_IFINLINE);
730 sf = (xfs_attr_shortform_t *)args->dp->i_afp->if_u1.if_data;
731 sfe = &sf->list[0];
732 for (i = 0; i < sf->hdr.count;
733 sfe = XFS_ATTR_SF_NEXTENTRY(sfe), i++) {
734 if (sfe->namelen != args->namelen)
735 continue;
736 if (memcmp(args->name, sfe->nameval, args->namelen) != 0)
737 continue;
738 if (!xfs_attr_namesp_match(args->flags, sfe->flags))
739 continue;
740 if (args->flags & ATTR_KERNOVAL) {
741 args->valuelen = sfe->valuelen;
742 return -EEXIST;
743 }
744 if (args->valuelen < sfe->valuelen) {
745 args->valuelen = sfe->valuelen;
746 return -ERANGE;
747 }
748 args->valuelen = sfe->valuelen;
749 memcpy(args->value, &sfe->nameval[args->namelen],
750 args->valuelen);
751 return -EEXIST;
752 }
753 return -ENOATTR;
754}
755
756
757
758
759
760int
761xfs_attr_shortform_to_leaf(
762 struct xfs_da_args *args,
763 struct xfs_buf **leaf_bp)
764{
765 xfs_inode_t *dp;
766 xfs_attr_shortform_t *sf;
767 xfs_attr_sf_entry_t *sfe;
768 xfs_da_args_t nargs;
769 char *tmpbuffer;
770 int error, i, size;
771 xfs_dablk_t blkno;
772 struct xfs_buf *bp;
773 xfs_ifork_t *ifp;
774
775 trace_xfs_attr_sf_to_leaf(args);
776
777 dp = args->dp;
778 ifp = dp->i_afp;
779 sf = (xfs_attr_shortform_t *)ifp->if_u1.if_data;
780 size = be16_to_cpu(sf->hdr.totsize);
781 tmpbuffer = kmem_alloc(size, KM_SLEEP);
782 ASSERT(tmpbuffer != NULL);
783 memcpy(tmpbuffer, ifp->if_u1.if_data, size);
784 sf = (xfs_attr_shortform_t *)tmpbuffer;
785
786 xfs_idata_realloc(dp, -size, XFS_ATTR_FORK);
787 xfs_bmap_local_to_extents_empty(dp, XFS_ATTR_FORK);
788
789 bp = NULL;
790 error = xfs_da_grow_inode(args, &blkno);
791 if (error) {
792
793
794
795
796 if (error == -EIO)
797 goto out;
798 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
799 memcpy(ifp->if_u1.if_data, tmpbuffer, size);
800 goto out;
801 }
802
803 ASSERT(blkno == 0);
804 error = xfs_attr3_leaf_create(args, blkno, &bp);
805 if (error) {
806 error = xfs_da_shrink_inode(args, 0, bp);
807 bp = NULL;
808 if (error)
809 goto out;
810 xfs_idata_realloc(dp, size, XFS_ATTR_FORK);
811 memcpy(ifp->if_u1.if_data, tmpbuffer, size);
812 goto out;
813 }
814
815 memset((char *)&nargs, 0, sizeof(nargs));
816 nargs.dp = dp;
817 nargs.geo = args->geo;
818 nargs.firstblock = args->firstblock;
819 nargs.dfops = args->dfops;
820 nargs.total = args->total;
821 nargs.whichfork = XFS_ATTR_FORK;
822 nargs.trans = args->trans;
823 nargs.op_flags = XFS_DA_OP_OKNOENT;
824
825 sfe = &sf->list[0];
826 for (i = 0; i < sf->hdr.count; i++) {
827 nargs.name = sfe->nameval;
828 nargs.namelen = sfe->namelen;
829 nargs.value = &sfe->nameval[nargs.namelen];
830 nargs.valuelen = sfe->valuelen;
831 nargs.hashval = xfs_da_hashname(sfe->nameval,
832 sfe->namelen);
833 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(sfe->flags);
834 error = xfs_attr3_leaf_lookup_int(bp, &nargs);
835 ASSERT(error == -ENOATTR);
836 error = xfs_attr3_leaf_add(bp, &nargs);
837 ASSERT(error != -ENOSPC);
838 if (error)
839 goto out;
840 sfe = XFS_ATTR_SF_NEXTENTRY(sfe);
841 }
842 error = 0;
843 *leaf_bp = bp;
844out:
845 kmem_free(tmpbuffer);
846 return error;
847}
848
849
850
851
852
853int
854xfs_attr_shortform_allfit(
855 struct xfs_buf *bp,
856 struct xfs_inode *dp)
857{
858 struct xfs_attr_leafblock *leaf;
859 struct xfs_attr_leaf_entry *entry;
860 xfs_attr_leaf_name_local_t *name_loc;
861 struct xfs_attr3_icleaf_hdr leafhdr;
862 int bytes;
863 int i;
864 struct xfs_mount *mp = bp->b_target->bt_mount;
865
866 leaf = bp->b_addr;
867 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
868 entry = xfs_attr3_leaf_entryp(leaf);
869
870 bytes = sizeof(struct xfs_attr_sf_hdr);
871 for (i = 0; i < leafhdr.count; entry++, i++) {
872 if (entry->flags & XFS_ATTR_INCOMPLETE)
873 continue;
874 if (!(entry->flags & XFS_ATTR_LOCAL))
875 return 0;
876 name_loc = xfs_attr3_leaf_name_local(leaf, i);
877 if (name_loc->namelen >= XFS_ATTR_SF_ENTSIZE_MAX)
878 return 0;
879 if (be16_to_cpu(name_loc->valuelen) >= XFS_ATTR_SF_ENTSIZE_MAX)
880 return 0;
881 bytes += sizeof(struct xfs_attr_sf_entry) - 1
882 + name_loc->namelen
883 + be16_to_cpu(name_loc->valuelen);
884 }
885 if ((dp->i_mount->m_flags & XFS_MOUNT_ATTR2) &&
886 (dp->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
887 (bytes == sizeof(struct xfs_attr_sf_hdr)))
888 return -1;
889 return xfs_attr_shortform_bytesfit(dp, bytes);
890}
891
892
893xfs_failaddr_t
894xfs_attr_shortform_verify(
895 struct xfs_inode *ip)
896{
897 struct xfs_attr_shortform *sfp;
898 struct xfs_attr_sf_entry *sfep;
899 struct xfs_attr_sf_entry *next_sfep;
900 char *endp;
901 struct xfs_ifork *ifp;
902 int i;
903 int size;
904
905 ASSERT(ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL);
906 ifp = XFS_IFORK_PTR(ip, XFS_ATTR_FORK);
907 sfp = (struct xfs_attr_shortform *)ifp->if_u1.if_data;
908 size = ifp->if_bytes;
909
910
911
912
913 if (size < sizeof(struct xfs_attr_sf_hdr))
914 return __this_address;
915
916 endp = (char *)sfp + size;
917
918
919 sfep = &sfp->list[0];
920 for (i = 0; i < sfp->hdr.count; i++) {
921
922
923
924
925
926 if (((char *)sfep + sizeof(*sfep)) >= endp)
927 return __this_address;
928
929
930 if (sfep->namelen == 0)
931 return __this_address;
932
933
934
935
936
937
938 next_sfep = XFS_ATTR_SF_NEXTENTRY(sfep);
939 if ((char *)next_sfep > endp)
940 return __this_address;
941
942
943
944
945
946
947 if (sfep->flags & ~XFS_ATTR_NSP_ONDISK_MASK)
948 return __this_address;
949
950
951
952
953
954
955 if (hweight8(sfep->flags & XFS_ATTR_NSP_ONDISK_MASK) > 1)
956 return __this_address;
957
958 sfep = next_sfep;
959 }
960 if ((void *)sfep != (void *)endp)
961 return __this_address;
962
963 return NULL;
964}
965
966
967
968
969int
970xfs_attr3_leaf_to_shortform(
971 struct xfs_buf *bp,
972 struct xfs_da_args *args,
973 int forkoff)
974{
975 struct xfs_attr_leafblock *leaf;
976 struct xfs_attr3_icleaf_hdr ichdr;
977 struct xfs_attr_leaf_entry *entry;
978 struct xfs_attr_leaf_name_local *name_loc;
979 struct xfs_da_args nargs;
980 struct xfs_inode *dp = args->dp;
981 char *tmpbuffer;
982 int error;
983 int i;
984
985 trace_xfs_attr_leaf_to_sf(args);
986
987 tmpbuffer = kmem_alloc(args->geo->blksize, KM_SLEEP);
988 if (!tmpbuffer)
989 return -ENOMEM;
990
991 memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
992
993 leaf = (xfs_attr_leafblock_t *)tmpbuffer;
994 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
995 entry = xfs_attr3_leaf_entryp(leaf);
996
997
998 memset(bp->b_addr, 0, args->geo->blksize);
999
1000
1001
1002
1003 error = xfs_da_shrink_inode(args, 0, bp);
1004 if (error)
1005 goto out;
1006
1007 if (forkoff == -1) {
1008 ASSERT(dp->i_mount->m_flags & XFS_MOUNT_ATTR2);
1009 ASSERT(dp->i_d.di_format != XFS_DINODE_FMT_BTREE);
1010 xfs_attr_fork_remove(dp, args->trans);
1011 goto out;
1012 }
1013
1014 xfs_attr_shortform_create(args);
1015
1016
1017
1018
1019 memset((char *)&nargs, 0, sizeof(nargs));
1020 nargs.geo = args->geo;
1021 nargs.dp = dp;
1022 nargs.firstblock = args->firstblock;
1023 nargs.dfops = args->dfops;
1024 nargs.total = args->total;
1025 nargs.whichfork = XFS_ATTR_FORK;
1026 nargs.trans = args->trans;
1027 nargs.op_flags = XFS_DA_OP_OKNOENT;
1028
1029 for (i = 0; i < ichdr.count; entry++, i++) {
1030 if (entry->flags & XFS_ATTR_INCOMPLETE)
1031 continue;
1032 if (!entry->nameidx)
1033 continue;
1034 ASSERT(entry->flags & XFS_ATTR_LOCAL);
1035 name_loc = xfs_attr3_leaf_name_local(leaf, i);
1036 nargs.name = name_loc->nameval;
1037 nargs.namelen = name_loc->namelen;
1038 nargs.value = &name_loc->nameval[nargs.namelen];
1039 nargs.valuelen = be16_to_cpu(name_loc->valuelen);
1040 nargs.hashval = be32_to_cpu(entry->hashval);
1041 nargs.flags = XFS_ATTR_NSP_ONDISK_TO_ARGS(entry->flags);
1042 xfs_attr_shortform_add(&nargs, forkoff);
1043 }
1044 error = 0;
1045
1046out:
1047 kmem_free(tmpbuffer);
1048 return error;
1049}
1050
1051
1052
1053
1054int
1055xfs_attr3_leaf_to_node(
1056 struct xfs_da_args *args)
1057{
1058 struct xfs_attr_leafblock *leaf;
1059 struct xfs_attr3_icleaf_hdr icleafhdr;
1060 struct xfs_attr_leaf_entry *entries;
1061 struct xfs_da_node_entry *btree;
1062 struct xfs_da3_icnode_hdr icnodehdr;
1063 struct xfs_da_intnode *node;
1064 struct xfs_inode *dp = args->dp;
1065 struct xfs_mount *mp = dp->i_mount;
1066 struct xfs_buf *bp1 = NULL;
1067 struct xfs_buf *bp2 = NULL;
1068 xfs_dablk_t blkno;
1069 int error;
1070
1071 trace_xfs_attr_leaf_to_node(args);
1072
1073 error = xfs_da_grow_inode(args, &blkno);
1074 if (error)
1075 goto out;
1076 error = xfs_attr3_leaf_read(args->trans, dp, 0, -1, &bp1);
1077 if (error)
1078 goto out;
1079
1080 error = xfs_da_get_buf(args->trans, dp, blkno, -1, &bp2, XFS_ATTR_FORK);
1081 if (error)
1082 goto out;
1083
1084
1085 xfs_trans_buf_set_type(args->trans, bp2, XFS_BLFT_ATTR_LEAF_BUF);
1086 bp2->b_ops = bp1->b_ops;
1087 memcpy(bp2->b_addr, bp1->b_addr, args->geo->blksize);
1088 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1089 struct xfs_da3_blkinfo *hdr3 = bp2->b_addr;
1090 hdr3->blkno = cpu_to_be64(bp2->b_bn);
1091 }
1092 xfs_trans_log_buf(args->trans, bp2, 0, args->geo->blksize - 1);
1093
1094
1095
1096
1097 error = xfs_da3_node_create(args, 0, 1, &bp1, XFS_ATTR_FORK);
1098 if (error)
1099 goto out;
1100 node = bp1->b_addr;
1101 dp->d_ops->node_hdr_from_disk(&icnodehdr, node);
1102 btree = dp->d_ops->node_tree_p(node);
1103
1104 leaf = bp2->b_addr;
1105 xfs_attr3_leaf_hdr_from_disk(args->geo, &icleafhdr, leaf);
1106 entries = xfs_attr3_leaf_entryp(leaf);
1107
1108
1109 btree[0].hashval = entries[icleafhdr.count - 1].hashval;
1110 btree[0].before = cpu_to_be32(blkno);
1111 icnodehdr.count = 1;
1112 dp->d_ops->node_hdr_to_disk(node, &icnodehdr);
1113 xfs_trans_log_buf(args->trans, bp1, 0, args->geo->blksize - 1);
1114 error = 0;
1115out:
1116 return error;
1117}
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127STATIC int
1128xfs_attr3_leaf_create(
1129 struct xfs_da_args *args,
1130 xfs_dablk_t blkno,
1131 struct xfs_buf **bpp)
1132{
1133 struct xfs_attr_leafblock *leaf;
1134 struct xfs_attr3_icleaf_hdr ichdr;
1135 struct xfs_inode *dp = args->dp;
1136 struct xfs_mount *mp = dp->i_mount;
1137 struct xfs_buf *bp;
1138 int error;
1139
1140 trace_xfs_attr_leaf_create(args);
1141
1142 error = xfs_da_get_buf(args->trans, args->dp, blkno, -1, &bp,
1143 XFS_ATTR_FORK);
1144 if (error)
1145 return error;
1146 bp->b_ops = &xfs_attr3_leaf_buf_ops;
1147 xfs_trans_buf_set_type(args->trans, bp, XFS_BLFT_ATTR_LEAF_BUF);
1148 leaf = bp->b_addr;
1149 memset(leaf, 0, args->geo->blksize);
1150
1151 memset(&ichdr, 0, sizeof(ichdr));
1152 ichdr.firstused = args->geo->blksize;
1153
1154 if (xfs_sb_version_hascrc(&mp->m_sb)) {
1155 struct xfs_da3_blkinfo *hdr3 = bp->b_addr;
1156
1157 ichdr.magic = XFS_ATTR3_LEAF_MAGIC;
1158
1159 hdr3->blkno = cpu_to_be64(bp->b_bn);
1160 hdr3->owner = cpu_to_be64(dp->i_ino);
1161 uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid);
1162
1163 ichdr.freemap[0].base = sizeof(struct xfs_attr3_leaf_hdr);
1164 } else {
1165 ichdr.magic = XFS_ATTR_LEAF_MAGIC;
1166 ichdr.freemap[0].base = sizeof(struct xfs_attr_leaf_hdr);
1167 }
1168 ichdr.freemap[0].size = ichdr.firstused - ichdr.freemap[0].base;
1169
1170 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
1171 xfs_trans_log_buf(args->trans, bp, 0, args->geo->blksize - 1);
1172
1173 *bpp = bp;
1174 return 0;
1175}
1176
1177
1178
1179
1180int
1181xfs_attr3_leaf_split(
1182 struct xfs_da_state *state,
1183 struct xfs_da_state_blk *oldblk,
1184 struct xfs_da_state_blk *newblk)
1185{
1186 xfs_dablk_t blkno;
1187 int error;
1188
1189 trace_xfs_attr_leaf_split(state->args);
1190
1191
1192
1193
1194 ASSERT(oldblk->magic == XFS_ATTR_LEAF_MAGIC);
1195 error = xfs_da_grow_inode(state->args, &blkno);
1196 if (error)
1197 return error;
1198 error = xfs_attr3_leaf_create(state->args, blkno, &newblk->bp);
1199 if (error)
1200 return error;
1201 newblk->blkno = blkno;
1202 newblk->magic = XFS_ATTR_LEAF_MAGIC;
1203
1204
1205
1206
1207
1208 xfs_attr3_leaf_rebalance(state, oldblk, newblk);
1209 error = xfs_da3_blk_link(state, oldblk, newblk);
1210 if (error)
1211 return error;
1212
1213
1214
1215
1216
1217
1218
1219
1220 if (state->inleaf) {
1221 trace_xfs_attr_leaf_add_old(state->args);
1222 error = xfs_attr3_leaf_add(oldblk->bp, state->args);
1223 } else {
1224 trace_xfs_attr_leaf_add_new(state->args);
1225 error = xfs_attr3_leaf_add(newblk->bp, state->args);
1226 }
1227
1228
1229
1230
1231 oldblk->hashval = xfs_attr_leaf_lasthash(oldblk->bp, NULL);
1232 newblk->hashval = xfs_attr_leaf_lasthash(newblk->bp, NULL);
1233 return error;
1234}
1235
1236
1237
1238
1239int
1240xfs_attr3_leaf_add(
1241 struct xfs_buf *bp,
1242 struct xfs_da_args *args)
1243{
1244 struct xfs_attr_leafblock *leaf;
1245 struct xfs_attr3_icleaf_hdr ichdr;
1246 int tablesize;
1247 int entsize;
1248 int sum;
1249 int tmp;
1250 int i;
1251
1252 trace_xfs_attr_leaf_add(args);
1253
1254 leaf = bp->b_addr;
1255 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
1256 ASSERT(args->index >= 0 && args->index <= ichdr.count);
1257 entsize = xfs_attr_leaf_newentsize(args, NULL);
1258
1259
1260
1261
1262
1263 tablesize = (ichdr.count + 1) * sizeof(xfs_attr_leaf_entry_t)
1264 + xfs_attr3_leaf_hdr_size(leaf);
1265 for (sum = 0, i = XFS_ATTR_LEAF_MAPSIZE - 1; i >= 0; i--) {
1266 if (tablesize > ichdr.firstused) {
1267 sum += ichdr.freemap[i].size;
1268 continue;
1269 }
1270 if (!ichdr.freemap[i].size)
1271 continue;
1272 tmp = entsize;
1273 if (ichdr.freemap[i].base < ichdr.firstused)
1274 tmp += sizeof(xfs_attr_leaf_entry_t);
1275 if (ichdr.freemap[i].size >= tmp) {
1276 tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, i);
1277 goto out_log_hdr;
1278 }
1279 sum += ichdr.freemap[i].size;
1280 }
1281
1282
1283
1284
1285
1286
1287 if (!ichdr.holes && sum < entsize)
1288 return -ENOSPC;
1289
1290
1291
1292
1293
1294 xfs_attr3_leaf_compact(args, &ichdr, bp);
1295
1296
1297
1298
1299
1300 if (ichdr.freemap[0].size < (entsize + sizeof(xfs_attr_leaf_entry_t))) {
1301 tmp = -ENOSPC;
1302 goto out_log_hdr;
1303 }
1304
1305 tmp = xfs_attr3_leaf_add_work(bp, &ichdr, args, 0);
1306
1307out_log_hdr:
1308 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
1309 xfs_trans_log_buf(args->trans, bp,
1310 XFS_DA_LOGRANGE(leaf, &leaf->hdr,
1311 xfs_attr3_leaf_hdr_size(leaf)));
1312 return tmp;
1313}
1314
1315
1316
1317
1318STATIC int
1319xfs_attr3_leaf_add_work(
1320 struct xfs_buf *bp,
1321 struct xfs_attr3_icleaf_hdr *ichdr,
1322 struct xfs_da_args *args,
1323 int mapindex)
1324{
1325 struct xfs_attr_leafblock *leaf;
1326 struct xfs_attr_leaf_entry *entry;
1327 struct xfs_attr_leaf_name_local *name_loc;
1328 struct xfs_attr_leaf_name_remote *name_rmt;
1329 struct xfs_mount *mp;
1330 int tmp;
1331 int i;
1332
1333 trace_xfs_attr_leaf_add_work(args);
1334
1335 leaf = bp->b_addr;
1336 ASSERT(mapindex >= 0 && mapindex < XFS_ATTR_LEAF_MAPSIZE);
1337 ASSERT(args->index >= 0 && args->index <= ichdr->count);
1338
1339
1340
1341
1342 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
1343 if (args->index < ichdr->count) {
1344 tmp = ichdr->count - args->index;
1345 tmp *= sizeof(xfs_attr_leaf_entry_t);
1346 memmove(entry + 1, entry, tmp);
1347 xfs_trans_log_buf(args->trans, bp,
1348 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(*entry)));
1349 }
1350 ichdr->count++;
1351
1352
1353
1354
1355 mp = args->trans->t_mountp;
1356 ASSERT(ichdr->freemap[mapindex].base < args->geo->blksize);
1357 ASSERT((ichdr->freemap[mapindex].base & 0x3) == 0);
1358 ASSERT(ichdr->freemap[mapindex].size >=
1359 xfs_attr_leaf_newentsize(args, NULL));
1360 ASSERT(ichdr->freemap[mapindex].size < args->geo->blksize);
1361 ASSERT((ichdr->freemap[mapindex].size & 0x3) == 0);
1362
1363 ichdr->freemap[mapindex].size -= xfs_attr_leaf_newentsize(args, &tmp);
1364
1365 entry->nameidx = cpu_to_be16(ichdr->freemap[mapindex].base +
1366 ichdr->freemap[mapindex].size);
1367 entry->hashval = cpu_to_be32(args->hashval);
1368 entry->flags = tmp ? XFS_ATTR_LOCAL : 0;
1369 entry->flags |= XFS_ATTR_NSP_ARGS_TO_ONDISK(args->flags);
1370 if (args->op_flags & XFS_DA_OP_RENAME) {
1371 entry->flags |= XFS_ATTR_INCOMPLETE;
1372 if ((args->blkno2 == args->blkno) &&
1373 (args->index2 <= args->index)) {
1374 args->index2++;
1375 }
1376 }
1377 xfs_trans_log_buf(args->trans, bp,
1378 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
1379 ASSERT((args->index == 0) ||
1380 (be32_to_cpu(entry->hashval) >= be32_to_cpu((entry-1)->hashval)));
1381 ASSERT((args->index == ichdr->count - 1) ||
1382 (be32_to_cpu(entry->hashval) <= be32_to_cpu((entry+1)->hashval)));
1383
1384
1385
1386
1387
1388
1389
1390
1391 if (entry->flags & XFS_ATTR_LOCAL) {
1392 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
1393 name_loc->namelen = args->namelen;
1394 name_loc->valuelen = cpu_to_be16(args->valuelen);
1395 memcpy((char *)name_loc->nameval, args->name, args->namelen);
1396 memcpy((char *)&name_loc->nameval[args->namelen], args->value,
1397 be16_to_cpu(name_loc->valuelen));
1398 } else {
1399 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
1400 name_rmt->namelen = args->namelen;
1401 memcpy((char *)name_rmt->name, args->name, args->namelen);
1402 entry->flags |= XFS_ATTR_INCOMPLETE;
1403
1404 name_rmt->valuelen = 0;
1405 name_rmt->valueblk = 0;
1406 args->rmtblkno = 1;
1407 args->rmtblkcnt = xfs_attr3_rmt_blocks(mp, args->valuelen);
1408 args->rmtvaluelen = args->valuelen;
1409 }
1410 xfs_trans_log_buf(args->trans, bp,
1411 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
1412 xfs_attr_leaf_entsize(leaf, args->index)));
1413
1414
1415
1416
1417 if (be16_to_cpu(entry->nameidx) < ichdr->firstused)
1418 ichdr->firstused = be16_to_cpu(entry->nameidx);
1419
1420 ASSERT(ichdr->firstused >= ichdr->count * sizeof(xfs_attr_leaf_entry_t)
1421 + xfs_attr3_leaf_hdr_size(leaf));
1422 tmp = (ichdr->count - 1) * sizeof(xfs_attr_leaf_entry_t)
1423 + xfs_attr3_leaf_hdr_size(leaf);
1424
1425 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
1426 if (ichdr->freemap[i].base == tmp) {
1427 ichdr->freemap[i].base += sizeof(xfs_attr_leaf_entry_t);
1428 ichdr->freemap[i].size -= sizeof(xfs_attr_leaf_entry_t);
1429 }
1430 }
1431 ichdr->usedbytes += xfs_attr_leaf_entsize(leaf, args->index);
1432 return 0;
1433}
1434
1435
1436
1437
1438STATIC void
1439xfs_attr3_leaf_compact(
1440 struct xfs_da_args *args,
1441 struct xfs_attr3_icleaf_hdr *ichdr_dst,
1442 struct xfs_buf *bp)
1443{
1444 struct xfs_attr_leafblock *leaf_src;
1445 struct xfs_attr_leafblock *leaf_dst;
1446 struct xfs_attr3_icleaf_hdr ichdr_src;
1447 struct xfs_trans *trans = args->trans;
1448 char *tmpbuffer;
1449
1450 trace_xfs_attr_leaf_compact(args);
1451
1452 tmpbuffer = kmem_alloc(args->geo->blksize, KM_SLEEP);
1453 memcpy(tmpbuffer, bp->b_addr, args->geo->blksize);
1454 memset(bp->b_addr, 0, args->geo->blksize);
1455 leaf_src = (xfs_attr_leafblock_t *)tmpbuffer;
1456 leaf_dst = bp->b_addr;
1457
1458
1459
1460
1461
1462
1463 memcpy(bp->b_addr, tmpbuffer, xfs_attr3_leaf_hdr_size(leaf_src));
1464
1465
1466 ichdr_src = *ichdr_dst;
1467 ichdr_dst->firstused = args->geo->blksize;
1468 ichdr_dst->usedbytes = 0;
1469 ichdr_dst->count = 0;
1470 ichdr_dst->holes = 0;
1471 ichdr_dst->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_src);
1472 ichdr_dst->freemap[0].size = ichdr_dst->firstused -
1473 ichdr_dst->freemap[0].base;
1474
1475
1476 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf_dst, ichdr_dst);
1477
1478
1479
1480
1481
1482 xfs_attr3_leaf_moveents(args, leaf_src, &ichdr_src, 0,
1483 leaf_dst, ichdr_dst, 0, ichdr_src.count);
1484
1485
1486
1487
1488 xfs_trans_log_buf(trans, bp, 0, args->geo->blksize - 1);
1489
1490 kmem_free(tmpbuffer);
1491}
1492
1493
1494
1495
1496
1497static int
1498xfs_attr3_leaf_order(
1499 struct xfs_buf *leaf1_bp,
1500 struct xfs_attr3_icleaf_hdr *leaf1hdr,
1501 struct xfs_buf *leaf2_bp,
1502 struct xfs_attr3_icleaf_hdr *leaf2hdr)
1503{
1504 struct xfs_attr_leaf_entry *entries1;
1505 struct xfs_attr_leaf_entry *entries2;
1506
1507 entries1 = xfs_attr3_leaf_entryp(leaf1_bp->b_addr);
1508 entries2 = xfs_attr3_leaf_entryp(leaf2_bp->b_addr);
1509 if (leaf1hdr->count > 0 && leaf2hdr->count > 0 &&
1510 ((be32_to_cpu(entries2[0].hashval) <
1511 be32_to_cpu(entries1[0].hashval)) ||
1512 (be32_to_cpu(entries2[leaf2hdr->count - 1].hashval) <
1513 be32_to_cpu(entries1[leaf1hdr->count - 1].hashval)))) {
1514 return 1;
1515 }
1516 return 0;
1517}
1518
1519int
1520xfs_attr_leaf_order(
1521 struct xfs_buf *leaf1_bp,
1522 struct xfs_buf *leaf2_bp)
1523{
1524 struct xfs_attr3_icleaf_hdr ichdr1;
1525 struct xfs_attr3_icleaf_hdr ichdr2;
1526 struct xfs_mount *mp = leaf1_bp->b_target->bt_mount;
1527
1528 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr1, leaf1_bp->b_addr);
1529 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr2, leaf2_bp->b_addr);
1530 return xfs_attr3_leaf_order(leaf1_bp, &ichdr1, leaf2_bp, &ichdr2);
1531}
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545STATIC void
1546xfs_attr3_leaf_rebalance(
1547 struct xfs_da_state *state,
1548 struct xfs_da_state_blk *blk1,
1549 struct xfs_da_state_blk *blk2)
1550{
1551 struct xfs_da_args *args;
1552 struct xfs_attr_leafblock *leaf1;
1553 struct xfs_attr_leafblock *leaf2;
1554 struct xfs_attr3_icleaf_hdr ichdr1;
1555 struct xfs_attr3_icleaf_hdr ichdr2;
1556 struct xfs_attr_leaf_entry *entries1;
1557 struct xfs_attr_leaf_entry *entries2;
1558 int count;
1559 int totallen;
1560 int max;
1561 int space;
1562 int swap;
1563
1564
1565
1566
1567 ASSERT(blk1->magic == XFS_ATTR_LEAF_MAGIC);
1568 ASSERT(blk2->magic == XFS_ATTR_LEAF_MAGIC);
1569 leaf1 = blk1->bp->b_addr;
1570 leaf2 = blk2->bp->b_addr;
1571 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr1, leaf1);
1572 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, leaf2);
1573 ASSERT(ichdr2.count == 0);
1574 args = state->args;
1575
1576 trace_xfs_attr_leaf_rebalance(args);
1577
1578
1579
1580
1581
1582
1583
1584 swap = 0;
1585 if (xfs_attr3_leaf_order(blk1->bp, &ichdr1, blk2->bp, &ichdr2)) {
1586 struct xfs_da_state_blk *tmp_blk;
1587 struct xfs_attr3_icleaf_hdr tmp_ichdr;
1588
1589 tmp_blk = blk1;
1590 blk1 = blk2;
1591 blk2 = tmp_blk;
1592
1593
1594 tmp_ichdr = ichdr1;
1595 ichdr1 = ichdr2;
1596 ichdr2 = tmp_ichdr;
1597
1598 leaf1 = blk1->bp->b_addr;
1599 leaf2 = blk2->bp->b_addr;
1600 swap = 1;
1601 }
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611 state->inleaf = xfs_attr3_leaf_figure_balance(state, blk1, &ichdr1,
1612 blk2, &ichdr2,
1613 &count, &totallen);
1614 if (swap)
1615 state->inleaf = !state->inleaf;
1616
1617
1618
1619
1620 if (count < ichdr1.count) {
1621
1622
1623
1624
1625 count = ichdr1.count - count;
1626 space = ichdr1.usedbytes - totallen;
1627 space += count * sizeof(xfs_attr_leaf_entry_t);
1628
1629
1630
1631
1632 max = ichdr2.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1633 max -= ichdr2.count * sizeof(xfs_attr_leaf_entry_t);
1634 if (space > max)
1635 xfs_attr3_leaf_compact(args, &ichdr2, blk2->bp);
1636
1637
1638
1639
1640 xfs_attr3_leaf_moveents(args, leaf1, &ichdr1,
1641 ichdr1.count - count, leaf2, &ichdr2, 0, count);
1642
1643 } else if (count > ichdr1.count) {
1644
1645
1646
1647
1648 ASSERT(0);
1649
1650
1651
1652
1653
1654 count -= ichdr1.count;
1655 space = totallen - ichdr1.usedbytes;
1656 space += count * sizeof(xfs_attr_leaf_entry_t);
1657
1658
1659
1660
1661 max = ichdr1.firstused - xfs_attr3_leaf_hdr_size(leaf1);
1662 max -= ichdr1.count * sizeof(xfs_attr_leaf_entry_t);
1663 if (space > max)
1664 xfs_attr3_leaf_compact(args, &ichdr1, blk1->bp);
1665
1666
1667
1668
1669 xfs_attr3_leaf_moveents(args, leaf2, &ichdr2, 0, leaf1, &ichdr1,
1670 ichdr1.count, count);
1671 }
1672
1673 xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf1, &ichdr1);
1674 xfs_attr3_leaf_hdr_to_disk(state->args->geo, leaf2, &ichdr2);
1675 xfs_trans_log_buf(args->trans, blk1->bp, 0, args->geo->blksize - 1);
1676 xfs_trans_log_buf(args->trans, blk2->bp, 0, args->geo->blksize - 1);
1677
1678
1679
1680
1681 entries1 = xfs_attr3_leaf_entryp(leaf1);
1682 entries2 = xfs_attr3_leaf_entryp(leaf2);
1683 blk1->hashval = be32_to_cpu(entries1[ichdr1.count - 1].hashval);
1684 blk2->hashval = be32_to_cpu(entries2[ichdr2.count - 1].hashval);
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698 if (blk1->index > ichdr1.count) {
1699 ASSERT(state->inleaf == 0);
1700 blk2->index = blk1->index - ichdr1.count;
1701 args->index = args->index2 = blk2->index;
1702 args->blkno = args->blkno2 = blk2->blkno;
1703 } else if (blk1->index == ichdr1.count) {
1704 if (state->inleaf) {
1705 args->index = blk1->index;
1706 args->blkno = blk1->blkno;
1707 args->index2 = 0;
1708 args->blkno2 = blk2->blkno;
1709 } else {
1710
1711
1712
1713
1714
1715 blk2->index = blk1->index - ichdr1.count;
1716 args->index = blk2->index;
1717 args->blkno = blk2->blkno;
1718 if (!state->extravalid) {
1719
1720
1721
1722
1723
1724 args->index2 = blk2->index;
1725 args->blkno2 = blk2->blkno;
1726 }
1727 }
1728 } else {
1729 ASSERT(state->inleaf == 1);
1730 args->index = args->index2 = blk1->index;
1731 args->blkno = args->blkno2 = blk1->blkno;
1732 }
1733}
1734
1735
1736
1737
1738
1739
1740
1741
1742STATIC int
1743xfs_attr3_leaf_figure_balance(
1744 struct xfs_da_state *state,
1745 struct xfs_da_state_blk *blk1,
1746 struct xfs_attr3_icleaf_hdr *ichdr1,
1747 struct xfs_da_state_blk *blk2,
1748 struct xfs_attr3_icleaf_hdr *ichdr2,
1749 int *countarg,
1750 int *usedbytesarg)
1751{
1752 struct xfs_attr_leafblock *leaf1 = blk1->bp->b_addr;
1753 struct xfs_attr_leafblock *leaf2 = blk2->bp->b_addr;
1754 struct xfs_attr_leaf_entry *entry;
1755 int count;
1756 int max;
1757 int index;
1758 int totallen = 0;
1759 int half;
1760 int lastdelta;
1761 int foundit = 0;
1762 int tmp;
1763
1764
1765
1766
1767
1768 max = ichdr1->count + ichdr2->count;
1769 half = (max + 1) * sizeof(*entry);
1770 half += ichdr1->usedbytes + ichdr2->usedbytes +
1771 xfs_attr_leaf_newentsize(state->args, NULL);
1772 half /= 2;
1773 lastdelta = state->args->geo->blksize;
1774 entry = xfs_attr3_leaf_entryp(leaf1);
1775 for (count = index = 0; count < max; entry++, index++, count++) {
1776
1777#define XFS_ATTR_ABS(A) (((A) < 0) ? -(A) : (A))
1778
1779
1780
1781 if (count == blk1->index) {
1782 tmp = totallen + sizeof(*entry) +
1783 xfs_attr_leaf_newentsize(state->args, NULL);
1784 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1785 break;
1786 lastdelta = XFS_ATTR_ABS(half - tmp);
1787 totallen = tmp;
1788 foundit = 1;
1789 }
1790
1791
1792
1793
1794 if (count == ichdr1->count) {
1795 leaf1 = leaf2;
1796 entry = xfs_attr3_leaf_entryp(leaf1);
1797 index = 0;
1798 }
1799
1800
1801
1802
1803 tmp = totallen + sizeof(*entry) + xfs_attr_leaf_entsize(leaf1,
1804 index);
1805 if (XFS_ATTR_ABS(half - tmp) > lastdelta)
1806 break;
1807 lastdelta = XFS_ATTR_ABS(half - tmp);
1808 totallen = tmp;
1809#undef XFS_ATTR_ABS
1810 }
1811
1812
1813
1814
1815
1816 totallen -= count * sizeof(*entry);
1817 if (foundit) {
1818 totallen -= sizeof(*entry) +
1819 xfs_attr_leaf_newentsize(state->args, NULL);
1820 }
1821
1822 *countarg = count;
1823 *usedbytesarg = totallen;
1824 return foundit;
1825}
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842int
1843xfs_attr3_leaf_toosmall(
1844 struct xfs_da_state *state,
1845 int *action)
1846{
1847 struct xfs_attr_leafblock *leaf;
1848 struct xfs_da_state_blk *blk;
1849 struct xfs_attr3_icleaf_hdr ichdr;
1850 struct xfs_buf *bp;
1851 xfs_dablk_t blkno;
1852 int bytes;
1853 int forward;
1854 int error;
1855 int retval;
1856 int i;
1857
1858 trace_xfs_attr_leaf_toosmall(state->args);
1859
1860
1861
1862
1863
1864
1865 blk = &state->path.blk[ state->path.active-1 ];
1866 leaf = blk->bp->b_addr;
1867 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr, leaf);
1868 bytes = xfs_attr3_leaf_hdr_size(leaf) +
1869 ichdr.count * sizeof(xfs_attr_leaf_entry_t) +
1870 ichdr.usedbytes;
1871 if (bytes > (state->args->geo->blksize >> 1)) {
1872 *action = 0;
1873 return 0;
1874 }
1875
1876
1877
1878
1879
1880
1881
1882 if (ichdr.count == 0) {
1883
1884
1885
1886
1887 forward = (ichdr.forw != 0);
1888 memcpy(&state->altpath, &state->path, sizeof(state->path));
1889 error = xfs_da3_path_shift(state, &state->altpath, forward,
1890 0, &retval);
1891 if (error)
1892 return error;
1893 if (retval) {
1894 *action = 0;
1895 } else {
1896 *action = 2;
1897 }
1898 return 0;
1899 }
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909 forward = ichdr.forw < ichdr.back;
1910 for (i = 0; i < 2; forward = !forward, i++) {
1911 struct xfs_attr3_icleaf_hdr ichdr2;
1912 if (forward)
1913 blkno = ichdr.forw;
1914 else
1915 blkno = ichdr.back;
1916 if (blkno == 0)
1917 continue;
1918 error = xfs_attr3_leaf_read(state->args->trans, state->args->dp,
1919 blkno, -1, &bp);
1920 if (error)
1921 return error;
1922
1923 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &ichdr2, bp->b_addr);
1924
1925 bytes = state->args->geo->blksize -
1926 (state->args->geo->blksize >> 2) -
1927 ichdr.usedbytes - ichdr2.usedbytes -
1928 ((ichdr.count + ichdr2.count) *
1929 sizeof(xfs_attr_leaf_entry_t)) -
1930 xfs_attr3_leaf_hdr_size(leaf);
1931
1932 xfs_trans_brelse(state->args->trans, bp);
1933 if (bytes >= 0)
1934 break;
1935 }
1936 if (i >= 2) {
1937 *action = 0;
1938 return 0;
1939 }
1940
1941
1942
1943
1944
1945 memcpy(&state->altpath, &state->path, sizeof(state->path));
1946 if (blkno < blk->blkno) {
1947 error = xfs_da3_path_shift(state, &state->altpath, forward,
1948 0, &retval);
1949 } else {
1950 error = xfs_da3_path_shift(state, &state->path, forward,
1951 0, &retval);
1952 }
1953 if (error)
1954 return error;
1955 if (retval) {
1956 *action = 0;
1957 } else {
1958 *action = 1;
1959 }
1960 return 0;
1961}
1962
1963
1964
1965
1966
1967
1968
1969int
1970xfs_attr3_leaf_remove(
1971 struct xfs_buf *bp,
1972 struct xfs_da_args *args)
1973{
1974 struct xfs_attr_leafblock *leaf;
1975 struct xfs_attr3_icleaf_hdr ichdr;
1976 struct xfs_attr_leaf_entry *entry;
1977 int before;
1978 int after;
1979 int smallest;
1980 int entsize;
1981 int tablesize;
1982 int tmp;
1983 int i;
1984
1985 trace_xfs_attr_leaf_remove(args);
1986
1987 leaf = bp->b_addr;
1988 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
1989
1990 ASSERT(ichdr.count > 0 && ichdr.count < args->geo->blksize / 8);
1991 ASSERT(args->index >= 0 && args->index < ichdr.count);
1992 ASSERT(ichdr.firstused >= ichdr.count * sizeof(*entry) +
1993 xfs_attr3_leaf_hdr_size(leaf));
1994
1995 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
1996
1997 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
1998 ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
1999
2000
2001
2002
2003
2004
2005
2006 tablesize = ichdr.count * sizeof(xfs_attr_leaf_entry_t)
2007 + xfs_attr3_leaf_hdr_size(leaf);
2008 tmp = ichdr.freemap[0].size;
2009 before = after = -1;
2010 smallest = XFS_ATTR_LEAF_MAPSIZE - 1;
2011 entsize = xfs_attr_leaf_entsize(leaf, args->index);
2012 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
2013 ASSERT(ichdr.freemap[i].base < args->geo->blksize);
2014 ASSERT(ichdr.freemap[i].size < args->geo->blksize);
2015 if (ichdr.freemap[i].base == tablesize) {
2016 ichdr.freemap[i].base -= sizeof(xfs_attr_leaf_entry_t);
2017 ichdr.freemap[i].size += sizeof(xfs_attr_leaf_entry_t);
2018 }
2019
2020 if (ichdr.freemap[i].base + ichdr.freemap[i].size ==
2021 be16_to_cpu(entry->nameidx)) {
2022 before = i;
2023 } else if (ichdr.freemap[i].base ==
2024 (be16_to_cpu(entry->nameidx) + entsize)) {
2025 after = i;
2026 } else if (ichdr.freemap[i].size < tmp) {
2027 tmp = ichdr.freemap[i].size;
2028 smallest = i;
2029 }
2030 }
2031
2032
2033
2034
2035
2036 if ((before >= 0) || (after >= 0)) {
2037 if ((before >= 0) && (after >= 0)) {
2038 ichdr.freemap[before].size += entsize;
2039 ichdr.freemap[before].size += ichdr.freemap[after].size;
2040 ichdr.freemap[after].base = 0;
2041 ichdr.freemap[after].size = 0;
2042 } else if (before >= 0) {
2043 ichdr.freemap[before].size += entsize;
2044 } else {
2045 ichdr.freemap[after].base = be16_to_cpu(entry->nameidx);
2046 ichdr.freemap[after].size += entsize;
2047 }
2048 } else {
2049
2050
2051
2052 if (ichdr.freemap[smallest].size < entsize) {
2053 ichdr.freemap[smallest].base = be16_to_cpu(entry->nameidx);
2054 ichdr.freemap[smallest].size = entsize;
2055 }
2056 }
2057
2058
2059
2060
2061 if (be16_to_cpu(entry->nameidx) == ichdr.firstused)
2062 smallest = 1;
2063 else
2064 smallest = 0;
2065
2066
2067
2068
2069 memset(xfs_attr3_leaf_name(leaf, args->index), 0, entsize);
2070 ichdr.usedbytes -= entsize;
2071 xfs_trans_log_buf(args->trans, bp,
2072 XFS_DA_LOGRANGE(leaf, xfs_attr3_leaf_name(leaf, args->index),
2073 entsize));
2074
2075 tmp = (ichdr.count - args->index) * sizeof(xfs_attr_leaf_entry_t);
2076 memmove(entry, entry + 1, tmp);
2077 ichdr.count--;
2078 xfs_trans_log_buf(args->trans, bp,
2079 XFS_DA_LOGRANGE(leaf, entry, tmp + sizeof(xfs_attr_leaf_entry_t)));
2080
2081 entry = &xfs_attr3_leaf_entryp(leaf)[ichdr.count];
2082 memset(entry, 0, sizeof(xfs_attr_leaf_entry_t));
2083
2084
2085
2086
2087
2088
2089
2090 if (smallest) {
2091 tmp = args->geo->blksize;
2092 entry = xfs_attr3_leaf_entryp(leaf);
2093 for (i = ichdr.count - 1; i >= 0; entry++, i--) {
2094 ASSERT(be16_to_cpu(entry->nameidx) >= ichdr.firstused);
2095 ASSERT(be16_to_cpu(entry->nameidx) < args->geo->blksize);
2096
2097 if (be16_to_cpu(entry->nameidx) < tmp)
2098 tmp = be16_to_cpu(entry->nameidx);
2099 }
2100 ichdr.firstused = tmp;
2101 ASSERT(ichdr.firstused != 0);
2102 } else {
2103 ichdr.holes = 1;
2104 }
2105 xfs_attr3_leaf_hdr_to_disk(args->geo, leaf, &ichdr);
2106 xfs_trans_log_buf(args->trans, bp,
2107 XFS_DA_LOGRANGE(leaf, &leaf->hdr,
2108 xfs_attr3_leaf_hdr_size(leaf)));
2109
2110
2111
2112
2113
2114 tmp = ichdr.usedbytes + xfs_attr3_leaf_hdr_size(leaf) +
2115 ichdr.count * sizeof(xfs_attr_leaf_entry_t);
2116
2117 return tmp < args->geo->magicpct;
2118}
2119
2120
2121
2122
2123void
2124xfs_attr3_leaf_unbalance(
2125 struct xfs_da_state *state,
2126 struct xfs_da_state_blk *drop_blk,
2127 struct xfs_da_state_blk *save_blk)
2128{
2129 struct xfs_attr_leafblock *drop_leaf = drop_blk->bp->b_addr;
2130 struct xfs_attr_leafblock *save_leaf = save_blk->bp->b_addr;
2131 struct xfs_attr3_icleaf_hdr drophdr;
2132 struct xfs_attr3_icleaf_hdr savehdr;
2133 struct xfs_attr_leaf_entry *entry;
2134
2135 trace_xfs_attr_leaf_unbalance(state->args);
2136
2137 drop_leaf = drop_blk->bp->b_addr;
2138 save_leaf = save_blk->bp->b_addr;
2139 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &drophdr, drop_leaf);
2140 xfs_attr3_leaf_hdr_from_disk(state->args->geo, &savehdr, save_leaf);
2141 entry = xfs_attr3_leaf_entryp(drop_leaf);
2142
2143
2144
2145
2146 drop_blk->hashval = be32_to_cpu(entry[drophdr.count - 1].hashval);
2147
2148
2149
2150
2151
2152
2153 if (savehdr.holes == 0) {
2154
2155
2156
2157
2158 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2159 drop_blk->bp, &drophdr)) {
2160 xfs_attr3_leaf_moveents(state->args,
2161 drop_leaf, &drophdr, 0,
2162 save_leaf, &savehdr, 0,
2163 drophdr.count);
2164 } else {
2165 xfs_attr3_leaf_moveents(state->args,
2166 drop_leaf, &drophdr, 0,
2167 save_leaf, &savehdr,
2168 savehdr.count, drophdr.count);
2169 }
2170 } else {
2171
2172
2173
2174
2175 struct xfs_attr_leafblock *tmp_leaf;
2176 struct xfs_attr3_icleaf_hdr tmphdr;
2177
2178 tmp_leaf = kmem_zalloc(state->args->geo->blksize, KM_SLEEP);
2179
2180
2181
2182
2183
2184
2185 memcpy(tmp_leaf, save_leaf, xfs_attr3_leaf_hdr_size(save_leaf));
2186
2187 memset(&tmphdr, 0, sizeof(tmphdr));
2188 tmphdr.magic = savehdr.magic;
2189 tmphdr.forw = savehdr.forw;
2190 tmphdr.back = savehdr.back;
2191 tmphdr.firstused = state->args->geo->blksize;
2192
2193
2194 xfs_attr3_leaf_hdr_to_disk(state->args->geo, tmp_leaf, &tmphdr);
2195
2196 if (xfs_attr3_leaf_order(save_blk->bp, &savehdr,
2197 drop_blk->bp, &drophdr)) {
2198 xfs_attr3_leaf_moveents(state->args,
2199 drop_leaf, &drophdr, 0,
2200 tmp_leaf, &tmphdr, 0,
2201 drophdr.count);
2202 xfs_attr3_leaf_moveents(state->args,
2203 save_leaf, &savehdr, 0,
2204 tmp_leaf, &tmphdr, tmphdr.count,
2205 savehdr.count);
2206 } else {
2207 xfs_attr3_leaf_moveents(state->args,
2208 save_leaf, &savehdr, 0,
2209 tmp_leaf, &tmphdr, 0,
2210 savehdr.count);
2211 xfs_attr3_leaf_moveents(state->args,
2212 drop_leaf, &drophdr, 0,
2213 tmp_leaf, &tmphdr, tmphdr.count,
2214 drophdr.count);
2215 }
2216 memcpy(save_leaf, tmp_leaf, state->args->geo->blksize);
2217 savehdr = tmphdr;
2218 kmem_free(tmp_leaf);
2219 }
2220
2221 xfs_attr3_leaf_hdr_to_disk(state->args->geo, save_leaf, &savehdr);
2222 xfs_trans_log_buf(state->args->trans, save_blk->bp, 0,
2223 state->args->geo->blksize - 1);
2224
2225
2226
2227
2228 entry = xfs_attr3_leaf_entryp(save_leaf);
2229 save_blk->hashval = be32_to_cpu(entry[savehdr.count - 1].hashval);
2230}
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249int
2250xfs_attr3_leaf_lookup_int(
2251 struct xfs_buf *bp,
2252 struct xfs_da_args *args)
2253{
2254 struct xfs_attr_leafblock *leaf;
2255 struct xfs_attr3_icleaf_hdr ichdr;
2256 struct xfs_attr_leaf_entry *entry;
2257 struct xfs_attr_leaf_entry *entries;
2258 struct xfs_attr_leaf_name_local *name_loc;
2259 struct xfs_attr_leaf_name_remote *name_rmt;
2260 xfs_dahash_t hashval;
2261 int probe;
2262 int span;
2263
2264 trace_xfs_attr_leaf_lookup(args);
2265
2266 leaf = bp->b_addr;
2267 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2268 entries = xfs_attr3_leaf_entryp(leaf);
2269 if (ichdr.count >= args->geo->blksize / 8)
2270 return -EFSCORRUPTED;
2271
2272
2273
2274
2275 hashval = args->hashval;
2276 probe = span = ichdr.count / 2;
2277 for (entry = &entries[probe]; span > 4; entry = &entries[probe]) {
2278 span /= 2;
2279 if (be32_to_cpu(entry->hashval) < hashval)
2280 probe += span;
2281 else if (be32_to_cpu(entry->hashval) > hashval)
2282 probe -= span;
2283 else
2284 break;
2285 }
2286 if (!(probe >= 0 && (!ichdr.count || probe < ichdr.count)))
2287 return -EFSCORRUPTED;
2288 if (!(span <= 4 || be32_to_cpu(entry->hashval) == hashval))
2289 return -EFSCORRUPTED;
2290
2291
2292
2293
2294
2295 while (probe > 0 && be32_to_cpu(entry->hashval) >= hashval) {
2296 entry--;
2297 probe--;
2298 }
2299 while (probe < ichdr.count &&
2300 be32_to_cpu(entry->hashval) < hashval) {
2301 entry++;
2302 probe++;
2303 }
2304 if (probe == ichdr.count || be32_to_cpu(entry->hashval) != hashval) {
2305 args->index = probe;
2306 return -ENOATTR;
2307 }
2308
2309
2310
2311
2312 for (; probe < ichdr.count && (be32_to_cpu(entry->hashval) == hashval);
2313 entry++, probe++) {
2314
2315
2316
2317
2318
2319
2320
2321 if ((args->flags & XFS_ATTR_INCOMPLETE) !=
2322 (entry->flags & XFS_ATTR_INCOMPLETE)) {
2323 continue;
2324 }
2325 if (entry->flags & XFS_ATTR_LOCAL) {
2326 name_loc = xfs_attr3_leaf_name_local(leaf, probe);
2327 if (name_loc->namelen != args->namelen)
2328 continue;
2329 if (memcmp(args->name, name_loc->nameval,
2330 args->namelen) != 0)
2331 continue;
2332 if (!xfs_attr_namesp_match(args->flags, entry->flags))
2333 continue;
2334 args->index = probe;
2335 return -EEXIST;
2336 } else {
2337 name_rmt = xfs_attr3_leaf_name_remote(leaf, probe);
2338 if (name_rmt->namelen != args->namelen)
2339 continue;
2340 if (memcmp(args->name, name_rmt->name,
2341 args->namelen) != 0)
2342 continue;
2343 if (!xfs_attr_namesp_match(args->flags, entry->flags))
2344 continue;
2345 args->index = probe;
2346 args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
2347 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2348 args->rmtblkcnt = xfs_attr3_rmt_blocks(
2349 args->dp->i_mount,
2350 args->rmtvaluelen);
2351 return -EEXIST;
2352 }
2353 }
2354 args->index = probe;
2355 return -ENOATTR;
2356}
2357
2358
2359
2360
2361
2362int
2363xfs_attr3_leaf_getvalue(
2364 struct xfs_buf *bp,
2365 struct xfs_da_args *args)
2366{
2367 struct xfs_attr_leafblock *leaf;
2368 struct xfs_attr3_icleaf_hdr ichdr;
2369 struct xfs_attr_leaf_entry *entry;
2370 struct xfs_attr_leaf_name_local *name_loc;
2371 struct xfs_attr_leaf_name_remote *name_rmt;
2372 int valuelen;
2373
2374 leaf = bp->b_addr;
2375 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2376 ASSERT(ichdr.count < args->geo->blksize / 8);
2377 ASSERT(args->index < ichdr.count);
2378
2379 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2380 if (entry->flags & XFS_ATTR_LOCAL) {
2381 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
2382 ASSERT(name_loc->namelen == args->namelen);
2383 ASSERT(memcmp(args->name, name_loc->nameval, args->namelen) == 0);
2384 valuelen = be16_to_cpu(name_loc->valuelen);
2385 if (args->flags & ATTR_KERNOVAL) {
2386 args->valuelen = valuelen;
2387 return 0;
2388 }
2389 if (args->valuelen < valuelen) {
2390 args->valuelen = valuelen;
2391 return -ERANGE;
2392 }
2393 args->valuelen = valuelen;
2394 memcpy(args->value, &name_loc->nameval[args->namelen], valuelen);
2395 } else {
2396 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2397 ASSERT(name_rmt->namelen == args->namelen);
2398 ASSERT(memcmp(args->name, name_rmt->name, args->namelen) == 0);
2399 args->rmtvaluelen = be32_to_cpu(name_rmt->valuelen);
2400 args->rmtblkno = be32_to_cpu(name_rmt->valueblk);
2401 args->rmtblkcnt = xfs_attr3_rmt_blocks(args->dp->i_mount,
2402 args->rmtvaluelen);
2403 if (args->flags & ATTR_KERNOVAL) {
2404 args->valuelen = args->rmtvaluelen;
2405 return 0;
2406 }
2407 if (args->valuelen < args->rmtvaluelen) {
2408 args->valuelen = args->rmtvaluelen;
2409 return -ERANGE;
2410 }
2411 args->valuelen = args->rmtvaluelen;
2412 }
2413 return 0;
2414}
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425STATIC void
2426xfs_attr3_leaf_moveents(
2427 struct xfs_da_args *args,
2428 struct xfs_attr_leafblock *leaf_s,
2429 struct xfs_attr3_icleaf_hdr *ichdr_s,
2430 int start_s,
2431 struct xfs_attr_leafblock *leaf_d,
2432 struct xfs_attr3_icleaf_hdr *ichdr_d,
2433 int start_d,
2434 int count)
2435{
2436 struct xfs_attr_leaf_entry *entry_s;
2437 struct xfs_attr_leaf_entry *entry_d;
2438 int desti;
2439 int tmp;
2440 int i;
2441
2442
2443
2444
2445 if (count == 0)
2446 return;
2447
2448
2449
2450
2451 ASSERT(ichdr_s->magic == XFS_ATTR_LEAF_MAGIC ||
2452 ichdr_s->magic == XFS_ATTR3_LEAF_MAGIC);
2453 ASSERT(ichdr_s->magic == ichdr_d->magic);
2454 ASSERT(ichdr_s->count > 0 && ichdr_s->count < args->geo->blksize / 8);
2455 ASSERT(ichdr_s->firstused >= (ichdr_s->count * sizeof(*entry_s))
2456 + xfs_attr3_leaf_hdr_size(leaf_s));
2457 ASSERT(ichdr_d->count < args->geo->blksize / 8);
2458 ASSERT(ichdr_d->firstused >= (ichdr_d->count * sizeof(*entry_d))
2459 + xfs_attr3_leaf_hdr_size(leaf_d));
2460
2461 ASSERT(start_s < ichdr_s->count);
2462 ASSERT(start_d <= ichdr_d->count);
2463 ASSERT(count <= ichdr_s->count);
2464
2465
2466
2467
2468
2469 if (start_d < ichdr_d->count) {
2470 tmp = ichdr_d->count - start_d;
2471 tmp *= sizeof(xfs_attr_leaf_entry_t);
2472 entry_s = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2473 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d + count];
2474 memmove(entry_d, entry_s, tmp);
2475 }
2476
2477
2478
2479
2480
2481 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2482 entry_d = &xfs_attr3_leaf_entryp(leaf_d)[start_d];
2483 desti = start_d;
2484 for (i = 0; i < count; entry_s++, entry_d++, desti++, i++) {
2485 ASSERT(be16_to_cpu(entry_s->nameidx) >= ichdr_s->firstused);
2486 tmp = xfs_attr_leaf_entsize(leaf_s, start_s + i);
2487#ifdef GROT
2488
2489
2490
2491
2492
2493 if (entry_s->flags & XFS_ATTR_INCOMPLETE) {
2494 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2495 ichdr_s->usedbytes -= tmp;
2496 ichdr_s->count -= 1;
2497 entry_d--;
2498 desti--;
2499 if ((start_s + i) < offset)
2500 result++;
2501 } else {
2502#endif
2503 ichdr_d->firstused -= tmp;
2504
2505 entry_d->hashval = entry_s->hashval;
2506 entry_d->nameidx = cpu_to_be16(ichdr_d->firstused);
2507 entry_d->flags = entry_s->flags;
2508 ASSERT(be16_to_cpu(entry_d->nameidx) + tmp
2509 <= args->geo->blksize);
2510 memmove(xfs_attr3_leaf_name(leaf_d, desti),
2511 xfs_attr3_leaf_name(leaf_s, start_s + i), tmp);
2512 ASSERT(be16_to_cpu(entry_s->nameidx) + tmp
2513 <= args->geo->blksize);
2514 memset(xfs_attr3_leaf_name(leaf_s, start_s + i), 0, tmp);
2515 ichdr_s->usedbytes -= tmp;
2516 ichdr_d->usedbytes += tmp;
2517 ichdr_s->count -= 1;
2518 ichdr_d->count += 1;
2519 tmp = ichdr_d->count * sizeof(xfs_attr_leaf_entry_t)
2520 + xfs_attr3_leaf_hdr_size(leaf_d);
2521 ASSERT(ichdr_d->firstused >= tmp);
2522#ifdef GROT
2523 }
2524#endif
2525 }
2526
2527
2528
2529
2530 if (start_s == ichdr_s->count) {
2531 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2532 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2533 ASSERT(((char *)entry_s + tmp) <=
2534 ((char *)leaf_s + args->geo->blksize));
2535 memset(entry_s, 0, tmp);
2536 } else {
2537
2538
2539
2540
2541 tmp = (ichdr_s->count - count) * sizeof(xfs_attr_leaf_entry_t);
2542 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[start_s + count];
2543 entry_d = &xfs_attr3_leaf_entryp(leaf_s)[start_s];
2544 memmove(entry_d, entry_s, tmp);
2545
2546 tmp = count * sizeof(xfs_attr_leaf_entry_t);
2547 entry_s = &xfs_attr3_leaf_entryp(leaf_s)[ichdr_s->count];
2548 ASSERT(((char *)entry_s + tmp) <=
2549 ((char *)leaf_s + args->geo->blksize));
2550 memset(entry_s, 0, tmp);
2551 }
2552
2553
2554
2555
2556 ichdr_d->freemap[0].base = xfs_attr3_leaf_hdr_size(leaf_d);
2557 ichdr_d->freemap[0].base += ichdr_d->count * sizeof(xfs_attr_leaf_entry_t);
2558 ichdr_d->freemap[0].size = ichdr_d->firstused - ichdr_d->freemap[0].base;
2559 ichdr_d->freemap[1].base = 0;
2560 ichdr_d->freemap[2].base = 0;
2561 ichdr_d->freemap[1].size = 0;
2562 ichdr_d->freemap[2].size = 0;
2563 ichdr_s->holes = 1;
2564}
2565
2566
2567
2568
2569xfs_dahash_t
2570xfs_attr_leaf_lasthash(
2571 struct xfs_buf *bp,
2572 int *count)
2573{
2574 struct xfs_attr3_icleaf_hdr ichdr;
2575 struct xfs_attr_leaf_entry *entries;
2576 struct xfs_mount *mp = bp->b_target->bt_mount;
2577
2578 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, bp->b_addr);
2579 entries = xfs_attr3_leaf_entryp(bp->b_addr);
2580 if (count)
2581 *count = ichdr.count;
2582 if (!ichdr.count)
2583 return 0;
2584 return be32_to_cpu(entries[ichdr.count - 1].hashval);
2585}
2586
2587
2588
2589
2590
2591STATIC int
2592xfs_attr_leaf_entsize(xfs_attr_leafblock_t *leaf, int index)
2593{
2594 struct xfs_attr_leaf_entry *entries;
2595 xfs_attr_leaf_name_local_t *name_loc;
2596 xfs_attr_leaf_name_remote_t *name_rmt;
2597 int size;
2598
2599 entries = xfs_attr3_leaf_entryp(leaf);
2600 if (entries[index].flags & XFS_ATTR_LOCAL) {
2601 name_loc = xfs_attr3_leaf_name_local(leaf, index);
2602 size = xfs_attr_leaf_entsize_local(name_loc->namelen,
2603 be16_to_cpu(name_loc->valuelen));
2604 } else {
2605 name_rmt = xfs_attr3_leaf_name_remote(leaf, index);
2606 size = xfs_attr_leaf_entsize_remote(name_rmt->namelen);
2607 }
2608 return size;
2609}
2610
2611
2612
2613
2614
2615
2616
2617int
2618xfs_attr_leaf_newentsize(
2619 struct xfs_da_args *args,
2620 int *local)
2621{
2622 int size;
2623
2624 size = xfs_attr_leaf_entsize_local(args->namelen, args->valuelen);
2625 if (size < xfs_attr_leaf_entsize_local_max(args->geo->blksize)) {
2626 if (local)
2627 *local = 1;
2628 return size;
2629 }
2630 if (local)
2631 *local = 0;
2632 return xfs_attr_leaf_entsize_remote(args->namelen);
2633}
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643int
2644xfs_attr3_leaf_clearflag(
2645 struct xfs_da_args *args)
2646{
2647 struct xfs_attr_leafblock *leaf;
2648 struct xfs_attr_leaf_entry *entry;
2649 struct xfs_attr_leaf_name_remote *name_rmt;
2650 struct xfs_buf *bp;
2651 int error;
2652#ifdef DEBUG
2653 struct xfs_attr3_icleaf_hdr ichdr;
2654 xfs_attr_leaf_name_local_t *name_loc;
2655 int namelen;
2656 char *name;
2657#endif
2658
2659 trace_xfs_attr_leaf_clearflag(args);
2660
2661
2662
2663 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
2664 if (error)
2665 return error;
2666
2667 leaf = bp->b_addr;
2668 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2669 ASSERT(entry->flags & XFS_ATTR_INCOMPLETE);
2670
2671#ifdef DEBUG
2672 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2673 ASSERT(args->index < ichdr.count);
2674 ASSERT(args->index >= 0);
2675
2676 if (entry->flags & XFS_ATTR_LOCAL) {
2677 name_loc = xfs_attr3_leaf_name_local(leaf, args->index);
2678 namelen = name_loc->namelen;
2679 name = (char *)name_loc->nameval;
2680 } else {
2681 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2682 namelen = name_rmt->namelen;
2683 name = (char *)name_rmt->name;
2684 }
2685 ASSERT(be32_to_cpu(entry->hashval) == args->hashval);
2686 ASSERT(namelen == args->namelen);
2687 ASSERT(memcmp(name, args->name, namelen) == 0);
2688#endif
2689
2690 entry->flags &= ~XFS_ATTR_INCOMPLETE;
2691 xfs_trans_log_buf(args->trans, bp,
2692 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2693
2694 if (args->rmtblkno) {
2695 ASSERT((entry->flags & XFS_ATTR_LOCAL) == 0);
2696 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2697 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2698 name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
2699 xfs_trans_log_buf(args->trans, bp,
2700 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2701 }
2702
2703
2704
2705
2706 return xfs_trans_roll_inode(&args->trans, args->dp);
2707}
2708
2709
2710
2711
2712int
2713xfs_attr3_leaf_setflag(
2714 struct xfs_da_args *args)
2715{
2716 struct xfs_attr_leafblock *leaf;
2717 struct xfs_attr_leaf_entry *entry;
2718 struct xfs_attr_leaf_name_remote *name_rmt;
2719 struct xfs_buf *bp;
2720 int error;
2721#ifdef DEBUG
2722 struct xfs_attr3_icleaf_hdr ichdr;
2723#endif
2724
2725 trace_xfs_attr_leaf_setflag(args);
2726
2727
2728
2729
2730 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
2731 if (error)
2732 return error;
2733
2734 leaf = bp->b_addr;
2735#ifdef DEBUG
2736 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr, leaf);
2737 ASSERT(args->index < ichdr.count);
2738 ASSERT(args->index >= 0);
2739#endif
2740 entry = &xfs_attr3_leaf_entryp(leaf)[args->index];
2741
2742 ASSERT((entry->flags & XFS_ATTR_INCOMPLETE) == 0);
2743 entry->flags |= XFS_ATTR_INCOMPLETE;
2744 xfs_trans_log_buf(args->trans, bp,
2745 XFS_DA_LOGRANGE(leaf, entry, sizeof(*entry)));
2746 if ((entry->flags & XFS_ATTR_LOCAL) == 0) {
2747 name_rmt = xfs_attr3_leaf_name_remote(leaf, args->index);
2748 name_rmt->valueblk = 0;
2749 name_rmt->valuelen = 0;
2750 xfs_trans_log_buf(args->trans, bp,
2751 XFS_DA_LOGRANGE(leaf, name_rmt, sizeof(*name_rmt)));
2752 }
2753
2754
2755
2756
2757 return xfs_trans_roll_inode(&args->trans, args->dp);
2758}
2759
2760
2761
2762
2763
2764
2765
2766
2767int
2768xfs_attr3_leaf_flipflags(
2769 struct xfs_da_args *args)
2770{
2771 struct xfs_attr_leafblock *leaf1;
2772 struct xfs_attr_leafblock *leaf2;
2773 struct xfs_attr_leaf_entry *entry1;
2774 struct xfs_attr_leaf_entry *entry2;
2775 struct xfs_attr_leaf_name_remote *name_rmt;
2776 struct xfs_buf *bp1;
2777 struct xfs_buf *bp2;
2778 int error;
2779#ifdef DEBUG
2780 struct xfs_attr3_icleaf_hdr ichdr1;
2781 struct xfs_attr3_icleaf_hdr ichdr2;
2782 xfs_attr_leaf_name_local_t *name_loc;
2783 int namelen1, namelen2;
2784 char *name1, *name2;
2785#endif
2786
2787 trace_xfs_attr_leaf_flipflags(args);
2788
2789
2790
2791
2792 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp1);
2793 if (error)
2794 return error;
2795
2796
2797
2798
2799 if (args->blkno2 != args->blkno) {
2800 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno2,
2801 -1, &bp2);
2802 if (error)
2803 return error;
2804 } else {
2805 bp2 = bp1;
2806 }
2807
2808 leaf1 = bp1->b_addr;
2809 entry1 = &xfs_attr3_leaf_entryp(leaf1)[args->index];
2810
2811 leaf2 = bp2->b_addr;
2812 entry2 = &xfs_attr3_leaf_entryp(leaf2)[args->index2];
2813
2814#ifdef DEBUG
2815 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr1, leaf1);
2816 ASSERT(args->index < ichdr1.count);
2817 ASSERT(args->index >= 0);
2818
2819 xfs_attr3_leaf_hdr_from_disk(args->geo, &ichdr2, leaf2);
2820 ASSERT(args->index2 < ichdr2.count);
2821 ASSERT(args->index2 >= 0);
2822
2823 if (entry1->flags & XFS_ATTR_LOCAL) {
2824 name_loc = xfs_attr3_leaf_name_local(leaf1, args->index);
2825 namelen1 = name_loc->namelen;
2826 name1 = (char *)name_loc->nameval;
2827 } else {
2828 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
2829 namelen1 = name_rmt->namelen;
2830 name1 = (char *)name_rmt->name;
2831 }
2832 if (entry2->flags & XFS_ATTR_LOCAL) {
2833 name_loc = xfs_attr3_leaf_name_local(leaf2, args->index2);
2834 namelen2 = name_loc->namelen;
2835 name2 = (char *)name_loc->nameval;
2836 } else {
2837 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
2838 namelen2 = name_rmt->namelen;
2839 name2 = (char *)name_rmt->name;
2840 }
2841 ASSERT(be32_to_cpu(entry1->hashval) == be32_to_cpu(entry2->hashval));
2842 ASSERT(namelen1 == namelen2);
2843 ASSERT(memcmp(name1, name2, namelen1) == 0);
2844#endif
2845
2846 ASSERT(entry1->flags & XFS_ATTR_INCOMPLETE);
2847 ASSERT((entry2->flags & XFS_ATTR_INCOMPLETE) == 0);
2848
2849 entry1->flags &= ~XFS_ATTR_INCOMPLETE;
2850 xfs_trans_log_buf(args->trans, bp1,
2851 XFS_DA_LOGRANGE(leaf1, entry1, sizeof(*entry1)));
2852 if (args->rmtblkno) {
2853 ASSERT((entry1->flags & XFS_ATTR_LOCAL) == 0);
2854 name_rmt = xfs_attr3_leaf_name_remote(leaf1, args->index);
2855 name_rmt->valueblk = cpu_to_be32(args->rmtblkno);
2856 name_rmt->valuelen = cpu_to_be32(args->rmtvaluelen);
2857 xfs_trans_log_buf(args->trans, bp1,
2858 XFS_DA_LOGRANGE(leaf1, name_rmt, sizeof(*name_rmt)));
2859 }
2860
2861 entry2->flags |= XFS_ATTR_INCOMPLETE;
2862 xfs_trans_log_buf(args->trans, bp2,
2863 XFS_DA_LOGRANGE(leaf2, entry2, sizeof(*entry2)));
2864 if ((entry2->flags & XFS_ATTR_LOCAL) == 0) {
2865 name_rmt = xfs_attr3_leaf_name_remote(leaf2, args->index2);
2866 name_rmt->valueblk = 0;
2867 name_rmt->valuelen = 0;
2868 xfs_trans_log_buf(args->trans, bp2,
2869 XFS_DA_LOGRANGE(leaf2, name_rmt, sizeof(*name_rmt)));
2870 }
2871
2872
2873
2874
2875 error = xfs_trans_roll_inode(&args->trans, args->dp);
2876
2877 return error;
2878}
2879