1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_shared.h"
21#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
24#include "xfs_bit.h"
25#include "xfs_mount.h"
26#include "xfs_da_format.h"
27#include "xfs_da_btree.h"
28#include "xfs_attr_sf.h"
29#include "xfs_inode.h"
30#include "xfs_alloc.h"
31#include "xfs_trans.h"
32#include "xfs_inode_item.h"
33#include "xfs_bmap.h"
34#include "xfs_bmap_util.h"
35#include "xfs_bmap_btree.h"
36#include "xfs_attr.h"
37#include "xfs_attr_leaf.h"
38#include "xfs_attr_remote.h"
39#include "xfs_error.h"
40#include "xfs_quota.h"
41#include "xfs_trans_space.h"
42#include "xfs_trace.h"
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
58
59
60
61
62STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
63STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
64STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
65
66
67
68
69STATIC int xfs_attr_node_get(xfs_da_args_t *args);
70STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
71STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
72STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
73STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
74
75
76STATIC int
77xfs_attr_args_init(
78 struct xfs_da_args *args,
79 struct xfs_inode *dp,
80 const unsigned char *name,
81 int flags)
82{
83
84 if (!name)
85 return -EINVAL;
86
87 memset(args, 0, sizeof(*args));
88 args->geo = dp->i_mount->m_attr_geo;
89 args->whichfork = XFS_ATTR_FORK;
90 args->dp = dp;
91 args->flags = flags;
92 args->name = name;
93 args->namelen = strlen((const char *)name);
94 if (args->namelen >= MAXNAMELEN)
95 return -EFAULT;
96
97 args->hashval = xfs_da_hashname(args->name, args->namelen);
98 return 0;
99}
100
101int
102xfs_inode_hasattr(
103 struct xfs_inode *ip)
104{
105 if (!XFS_IFORK_Q(ip) ||
106 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
107 ip->i_d.di_anextents == 0))
108 return 0;
109 return 1;
110}
111
112
113
114
115
116int
117xfs_attr_get(
118 struct xfs_inode *ip,
119 const unsigned char *name,
120 unsigned char *value,
121 int *valuelenp,
122 int flags)
123{
124 struct xfs_da_args args;
125 uint lock_mode;
126 int error;
127
128 XFS_STATS_INC(xs_attr_get);
129
130 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
131 return -EIO;
132
133 if (!xfs_inode_hasattr(ip))
134 return -ENOATTR;
135
136 error = xfs_attr_args_init(&args, ip, name, flags);
137 if (error)
138 return error;
139
140 args.value = value;
141 args.valuelen = *valuelenp;
142
143 lock_mode = xfs_ilock_attr_map_shared(ip);
144 if (!xfs_inode_hasattr(ip))
145 error = -ENOATTR;
146 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
147 error = xfs_attr_shortform_getvalue(&args);
148 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
149 error = xfs_attr_leaf_get(&args);
150 else
151 error = xfs_attr_node_get(&args);
152 xfs_iunlock(ip, lock_mode);
153
154 *valuelenp = args.valuelen;
155 return error == -EEXIST ? 0 : error;
156}
157
158
159
160
161STATIC int
162xfs_attr_calc_size(
163 struct xfs_da_args *args,
164 int *local)
165{
166 struct xfs_mount *mp = args->dp->i_mount;
167 int size;
168 int nblks;
169
170
171
172
173
174 size = xfs_attr_leaf_newentsize(args, local);
175 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
176 if (*local) {
177 if (size > (args->geo->blksize / 2)) {
178
179 nblks *= 2;
180 }
181 } else {
182
183
184
185
186 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
187 nblks += dblocks;
188 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
189 }
190
191 return nblks;
192}
193
194int
195xfs_attr_set(
196 struct xfs_inode *dp,
197 const unsigned char *name,
198 unsigned char *value,
199 int valuelen,
200 int flags)
201{
202 struct xfs_mount *mp = dp->i_mount;
203 struct xfs_da_args args;
204 struct xfs_bmap_free flist;
205 struct xfs_trans_res tres;
206 xfs_fsblock_t firstblock;
207 int rsvd = (flags & ATTR_ROOT) != 0;
208 int error, err2, committed, local;
209
210 XFS_STATS_INC(xs_attr_set);
211
212 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
213 return -EIO;
214
215 error = xfs_attr_args_init(&args, dp, name, flags);
216 if (error)
217 return error;
218
219 args.value = value;
220 args.valuelen = valuelen;
221 args.firstblock = &firstblock;
222 args.flist = &flist;
223 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
224 args.total = xfs_attr_calc_size(&args, &local);
225
226 error = xfs_qm_dqattach(dp, 0);
227 if (error)
228 return error;
229
230
231
232
233
234 if (XFS_IFORK_Q(dp) == 0) {
235 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
236 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
237
238 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
239 if (error)
240 return error;
241 }
242
243
244
245
246
247
248
249
250
251
252
253 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
254
255
256
257
258
259
260 if (rsvd)
261 args.trans->t_flags |= XFS_TRANS_RESERVE;
262
263 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
264 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
265 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
266 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
267 error = xfs_trans_reserve(args.trans, &tres, args.total, 0);
268 if (error) {
269 xfs_trans_cancel(args.trans, 0);
270 return error;
271 }
272 xfs_ilock(dp, XFS_ILOCK_EXCL);
273
274 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
275 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
276 XFS_QMOPT_RES_REGBLKS);
277 if (error) {
278 xfs_iunlock(dp, XFS_ILOCK_EXCL);
279 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
280 return error;
281 }
282
283 xfs_trans_ijoin(args.trans, dp, 0);
284
285
286
287
288
289 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
290 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
291 dp->i_d.di_anextents == 0)) {
292
293
294
295
296 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
297 xfs_attr_shortform_create(&args);
298
299
300
301
302
303 error = xfs_attr_shortform_addname(&args);
304 if (error != -ENOSPC) {
305
306
307
308
309 ASSERT(args.trans != NULL);
310
311
312
313
314
315
316 if (mp->m_flags & XFS_MOUNT_WSYNC)
317 xfs_trans_set_sync(args.trans);
318
319 if (!error && (flags & ATTR_KERNOTIME) == 0) {
320 xfs_trans_ichgtime(args.trans, dp,
321 XFS_ICHGTIME_CHG);
322 }
323 err2 = xfs_trans_commit(args.trans,
324 XFS_TRANS_RELEASE_LOG_RES);
325 xfs_iunlock(dp, XFS_ILOCK_EXCL);
326
327 return error ? error : err2;
328 }
329
330
331
332
333
334 xfs_bmap_init(args.flist, args.firstblock);
335 error = xfs_attr_shortform_to_leaf(&args);
336 if (!error) {
337 error = xfs_bmap_finish(&args.trans, args.flist,
338 &committed);
339 }
340 if (error) {
341 ASSERT(committed);
342 args.trans = NULL;
343 xfs_bmap_cancel(&flist);
344 goto out;
345 }
346
347
348
349
350
351 if (committed)
352 xfs_trans_ijoin(args.trans, dp, 0);
353
354
355
356
357
358
359 error = xfs_trans_roll(&args.trans, dp);
360 if (error)
361 goto out;
362
363 }
364
365 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
366 error = xfs_attr_leaf_addname(&args);
367 else
368 error = xfs_attr_node_addname(&args);
369 if (error)
370 goto out;
371
372
373
374
375
376 if (mp->m_flags & XFS_MOUNT_WSYNC)
377 xfs_trans_set_sync(args.trans);
378
379 if ((flags & ATTR_KERNOTIME) == 0)
380 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
381
382
383
384
385 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
386 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
387 xfs_iunlock(dp, XFS_ILOCK_EXCL);
388
389 return error;
390
391out:
392 if (args.trans) {
393 xfs_trans_cancel(args.trans,
394 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
395 }
396 xfs_iunlock(dp, XFS_ILOCK_EXCL);
397 return error;
398}
399
400
401
402
403
404int
405xfs_attr_remove(
406 struct xfs_inode *dp,
407 const unsigned char *name,
408 int flags)
409{
410 struct xfs_mount *mp = dp->i_mount;
411 struct xfs_da_args args;
412 struct xfs_bmap_free flist;
413 xfs_fsblock_t firstblock;
414 int error;
415
416 XFS_STATS_INC(xs_attr_remove);
417
418 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
419 return -EIO;
420
421 if (!xfs_inode_hasattr(dp))
422 return -ENOATTR;
423
424 error = xfs_attr_args_init(&args, dp, name, flags);
425 if (error)
426 return error;
427
428 args.firstblock = &firstblock;
429 args.flist = &flist;
430
431
432
433
434
435
436 args.op_flags = XFS_DA_OP_OKNOENT;
437
438 error = xfs_qm_dqattach(dp, 0);
439 if (error)
440 return error;
441
442
443
444
445
446
447
448
449
450
451
452 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
453
454
455
456
457
458
459 if (flags & ATTR_ROOT)
460 args.trans->t_flags |= XFS_TRANS_RESERVE;
461
462 error = xfs_trans_reserve(args.trans, &M_RES(mp)->tr_attrrm,
463 XFS_ATTRRM_SPACE_RES(mp), 0);
464 if (error) {
465 xfs_trans_cancel(args.trans, 0);
466 return error;
467 }
468
469 xfs_ilock(dp, XFS_ILOCK_EXCL);
470
471
472
473
474 xfs_trans_ijoin(args.trans, dp, 0);
475
476 if (!xfs_inode_hasattr(dp)) {
477 error = -ENOATTR;
478 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
479 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
480 error = xfs_attr_shortform_remove(&args);
481 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
482 error = xfs_attr_leaf_removename(&args);
483 } else {
484 error = xfs_attr_node_removename(&args);
485 }
486
487 if (error)
488 goto out;
489
490
491
492
493
494 if (mp->m_flags & XFS_MOUNT_WSYNC)
495 xfs_trans_set_sync(args.trans);
496
497 if ((flags & ATTR_KERNOTIME) == 0)
498 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
499
500
501
502
503 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
504 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
505 xfs_iunlock(dp, XFS_ILOCK_EXCL);
506
507 return error;
508
509out:
510 if (args.trans) {
511 xfs_trans_cancel(args.trans,
512 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
513 }
514 xfs_iunlock(dp, XFS_ILOCK_EXCL);
515 return error;
516}
517
518
519
520
521
522
523
524
525
526STATIC int
527xfs_attr_shortform_addname(xfs_da_args_t *args)
528{
529 int newsize, forkoff, retval;
530
531 trace_xfs_attr_sf_addname(args);
532
533 retval = xfs_attr_shortform_lookup(args);
534 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
535 return retval;
536 } else if (retval == -EEXIST) {
537 if (args->flags & ATTR_CREATE)
538 return retval;
539 retval = xfs_attr_shortform_remove(args);
540 ASSERT(retval == 0);
541 }
542
543 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
544 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
545 return -ENOSPC;
546
547 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
548 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
549
550 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
551 if (!forkoff)
552 return -ENOSPC;
553
554 xfs_attr_shortform_add(args, forkoff);
555 return 0;
556}
557
558
559
560
561
562
563
564
565
566
567
568
569STATIC int
570xfs_attr_leaf_addname(xfs_da_args_t *args)
571{
572 xfs_inode_t *dp;
573 struct xfs_buf *bp;
574 int retval, error, committed, forkoff;
575
576 trace_xfs_attr_leaf_addname(args);
577
578
579
580
581 dp = args->dp;
582 args->blkno = 0;
583 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
584 if (error)
585 return error;
586
587
588
589
590
591 retval = xfs_attr3_leaf_lookup_int(bp, args);
592 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
593 xfs_trans_brelse(args->trans, bp);
594 return retval;
595 } else if (retval == -EEXIST) {
596 if (args->flags & ATTR_CREATE) {
597 xfs_trans_brelse(args->trans, bp);
598 return retval;
599 }
600
601 trace_xfs_attr_leaf_replace(args);
602
603
604 args->op_flags |= XFS_DA_OP_RENAME;
605 args->blkno2 = args->blkno;
606 args->index2 = args->index;
607 args->rmtblkno2 = args->rmtblkno;
608 args->rmtblkcnt2 = args->rmtblkcnt;
609 args->rmtvaluelen2 = args->rmtvaluelen;
610
611
612
613
614
615
616 args->rmtblkno = 0;
617 args->rmtblkcnt = 0;
618 args->rmtvaluelen = 0;
619 }
620
621
622
623
624
625 retval = xfs_attr3_leaf_add(bp, args);
626 if (retval == -ENOSPC) {
627
628
629
630
631
632 xfs_bmap_init(args->flist, args->firstblock);
633 error = xfs_attr3_leaf_to_node(args);
634 if (!error) {
635 error = xfs_bmap_finish(&args->trans, args->flist,
636 &committed);
637 }
638 if (error) {
639 ASSERT(committed);
640 args->trans = NULL;
641 xfs_bmap_cancel(args->flist);
642 return error;
643 }
644
645
646
647
648
649 if (committed)
650 xfs_trans_ijoin(args->trans, dp, 0);
651
652
653
654
655
656 error = xfs_trans_roll(&args->trans, dp);
657 if (error)
658 return error;
659
660
661
662
663 error = xfs_attr_node_addname(args);
664 return error;
665 }
666
667
668
669
670
671 error = xfs_trans_roll(&args->trans, dp);
672 if (error)
673 return error;
674
675
676
677
678
679
680
681 if (args->rmtblkno > 0) {
682 error = xfs_attr_rmtval_set(args);
683 if (error)
684 return error;
685 }
686
687
688
689
690
691
692
693 if (args->op_flags & XFS_DA_OP_RENAME) {
694
695
696
697
698 error = xfs_attr3_leaf_flipflags(args);
699 if (error)
700 return error;
701
702
703
704
705
706 args->index = args->index2;
707 args->blkno = args->blkno2;
708 args->rmtblkno = args->rmtblkno2;
709 args->rmtblkcnt = args->rmtblkcnt2;
710 args->rmtvaluelen = args->rmtvaluelen2;
711 if (args->rmtblkno) {
712 error = xfs_attr_rmtval_remove(args);
713 if (error)
714 return error;
715 }
716
717
718
719
720
721 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
722 -1, &bp);
723 if (error)
724 return error;
725
726 xfs_attr3_leaf_remove(bp, args);
727
728
729
730
731 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
732 xfs_bmap_init(args->flist, args->firstblock);
733 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
734
735 if (!error) {
736 error = xfs_bmap_finish(&args->trans,
737 args->flist,
738 &committed);
739 }
740 if (error) {
741 ASSERT(committed);
742 args->trans = NULL;
743 xfs_bmap_cancel(args->flist);
744 return error;
745 }
746
747
748
749
750
751
752 if (committed)
753 xfs_trans_ijoin(args->trans, dp, 0);
754 }
755
756
757
758
759 error = xfs_trans_roll(&args->trans, dp);
760
761 } else if (args->rmtblkno > 0) {
762
763
764
765 error = xfs_attr3_leaf_clearflag(args);
766 }
767 return error;
768}
769
770
771
772
773
774
775
776STATIC int
777xfs_attr_leaf_removename(xfs_da_args_t *args)
778{
779 xfs_inode_t *dp;
780 struct xfs_buf *bp;
781 int error, committed, forkoff;
782
783 trace_xfs_attr_leaf_removename(args);
784
785
786
787
788 dp = args->dp;
789 args->blkno = 0;
790 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
791 if (error)
792 return error;
793
794 error = xfs_attr3_leaf_lookup_int(bp, args);
795 if (error == -ENOATTR) {
796 xfs_trans_brelse(args->trans, bp);
797 return error;
798 }
799
800 xfs_attr3_leaf_remove(bp, args);
801
802
803
804
805 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
806 xfs_bmap_init(args->flist, args->firstblock);
807 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
808
809 if (!error) {
810 error = xfs_bmap_finish(&args->trans, args->flist,
811 &committed);
812 }
813 if (error) {
814 ASSERT(committed);
815 args->trans = NULL;
816 xfs_bmap_cancel(args->flist);
817 return error;
818 }
819
820
821
822
823
824 if (committed)
825 xfs_trans_ijoin(args->trans, dp, 0);
826 }
827 return 0;
828}
829
830
831
832
833
834
835
836STATIC int
837xfs_attr_leaf_get(xfs_da_args_t *args)
838{
839 struct xfs_buf *bp;
840 int error;
841
842 trace_xfs_attr_leaf_get(args);
843
844 args->blkno = 0;
845 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
846 if (error)
847 return error;
848
849 error = xfs_attr3_leaf_lookup_int(bp, args);
850 if (error != -EEXIST) {
851 xfs_trans_brelse(args->trans, bp);
852 return error;
853 }
854 error = xfs_attr3_leaf_getvalue(bp, args);
855 xfs_trans_brelse(args->trans, bp);
856 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
857 error = xfs_attr_rmtval_get(args);
858 }
859 return error;
860}
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876STATIC int
877xfs_attr_node_addname(xfs_da_args_t *args)
878{
879 xfs_da_state_t *state;
880 xfs_da_state_blk_t *blk;
881 xfs_inode_t *dp;
882 xfs_mount_t *mp;
883 int committed, retval, error;
884
885 trace_xfs_attr_node_addname(args);
886
887
888
889
890 dp = args->dp;
891 mp = dp->i_mount;
892restart:
893 state = xfs_da_state_alloc();
894 state->args = args;
895 state->mp = mp;
896
897
898
899
900
901 error = xfs_da3_node_lookup_int(state, &retval);
902 if (error)
903 goto out;
904 blk = &state->path.blk[ state->path.active-1 ];
905 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
906 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
907 goto out;
908 } else if (retval == -EEXIST) {
909 if (args->flags & ATTR_CREATE)
910 goto out;
911
912 trace_xfs_attr_node_replace(args);
913
914
915 args->op_flags |= XFS_DA_OP_RENAME;
916 args->blkno2 = args->blkno;
917 args->index2 = args->index;
918 args->rmtblkno2 = args->rmtblkno;
919 args->rmtblkcnt2 = args->rmtblkcnt;
920 args->rmtvaluelen2 = args->rmtvaluelen;
921
922
923
924
925
926
927 args->rmtblkno = 0;
928 args->rmtblkcnt = 0;
929 args->rmtvaluelen = 0;
930 }
931
932 retval = xfs_attr3_leaf_add(blk->bp, state->args);
933 if (retval == -ENOSPC) {
934 if (state->path.active == 1) {
935
936
937
938
939
940 xfs_da_state_free(state);
941 state = NULL;
942 xfs_bmap_init(args->flist, args->firstblock);
943 error = xfs_attr3_leaf_to_node(args);
944 if (!error) {
945 error = xfs_bmap_finish(&args->trans,
946 args->flist,
947 &committed);
948 }
949 if (error) {
950 ASSERT(committed);
951 args->trans = NULL;
952 xfs_bmap_cancel(args->flist);
953 goto out;
954 }
955
956
957
958
959
960
961 if (committed)
962 xfs_trans_ijoin(args->trans, dp, 0);
963
964
965
966
967
968 error = xfs_trans_roll(&args->trans, dp);
969 if (error)
970 goto out;
971
972 goto restart;
973 }
974
975
976
977
978
979
980
981 xfs_bmap_init(args->flist, args->firstblock);
982 error = xfs_da3_split(state);
983 if (!error) {
984 error = xfs_bmap_finish(&args->trans, args->flist,
985 &committed);
986 }
987 if (error) {
988 ASSERT(committed);
989 args->trans = NULL;
990 xfs_bmap_cancel(args->flist);
991 goto out;
992 }
993
994
995
996
997
998 if (committed)
999 xfs_trans_ijoin(args->trans, dp, 0);
1000 } else {
1001
1002
1003
1004 xfs_da3_fixhashpath(state, &state->path);
1005 }
1006
1007
1008
1009
1010
1011 xfs_da_state_free(state);
1012 state = NULL;
1013
1014
1015
1016
1017
1018 error = xfs_trans_roll(&args->trans, dp);
1019 if (error)
1020 goto out;
1021
1022
1023
1024
1025
1026
1027
1028 if (args->rmtblkno > 0) {
1029 error = xfs_attr_rmtval_set(args);
1030 if (error)
1031 return error;
1032 }
1033
1034
1035
1036
1037
1038
1039
1040 if (args->op_flags & XFS_DA_OP_RENAME) {
1041
1042
1043
1044
1045 error = xfs_attr3_leaf_flipflags(args);
1046 if (error)
1047 goto out;
1048
1049
1050
1051
1052
1053 args->index = args->index2;
1054 args->blkno = args->blkno2;
1055 args->rmtblkno = args->rmtblkno2;
1056 args->rmtblkcnt = args->rmtblkcnt2;
1057 args->rmtvaluelen = args->rmtvaluelen2;
1058 if (args->rmtblkno) {
1059 error = xfs_attr_rmtval_remove(args);
1060 if (error)
1061 return error;
1062 }
1063
1064
1065
1066
1067
1068
1069 args->flags |= XFS_ATTR_INCOMPLETE;
1070 state = xfs_da_state_alloc();
1071 state->args = args;
1072 state->mp = mp;
1073 state->inleaf = 0;
1074 error = xfs_da3_node_lookup_int(state, &retval);
1075 if (error)
1076 goto out;
1077
1078
1079
1080
1081 blk = &state->path.blk[ state->path.active-1 ];
1082 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1083 error = xfs_attr3_leaf_remove(blk->bp, args);
1084 xfs_da3_fixhashpath(state, &state->path);
1085
1086
1087
1088
1089 if (retval && (state->path.active > 1)) {
1090 xfs_bmap_init(args->flist, args->firstblock);
1091 error = xfs_da3_join(state);
1092 if (!error) {
1093 error = xfs_bmap_finish(&args->trans,
1094 args->flist,
1095 &committed);
1096 }
1097 if (error) {
1098 ASSERT(committed);
1099 args->trans = NULL;
1100 xfs_bmap_cancel(args->flist);
1101 goto out;
1102 }
1103
1104
1105
1106
1107
1108
1109 if (committed)
1110 xfs_trans_ijoin(args->trans, dp, 0);
1111 }
1112
1113
1114
1115
1116 error = xfs_trans_roll(&args->trans, dp);
1117 if (error)
1118 goto out;
1119
1120 } else if (args->rmtblkno > 0) {
1121
1122
1123
1124 error = xfs_attr3_leaf_clearflag(args);
1125 if (error)
1126 goto out;
1127 }
1128 retval = error = 0;
1129
1130out:
1131 if (state)
1132 xfs_da_state_free(state);
1133 if (error)
1134 return error;
1135 return retval;
1136}
1137
1138
1139
1140
1141
1142
1143
1144
1145STATIC int
1146xfs_attr_node_removename(xfs_da_args_t *args)
1147{
1148 xfs_da_state_t *state;
1149 xfs_da_state_blk_t *blk;
1150 xfs_inode_t *dp;
1151 struct xfs_buf *bp;
1152 int retval, error, committed, forkoff;
1153
1154 trace_xfs_attr_node_removename(args);
1155
1156
1157
1158
1159 dp = args->dp;
1160 state = xfs_da_state_alloc();
1161 state->args = args;
1162 state->mp = dp->i_mount;
1163
1164
1165
1166
1167 error = xfs_da3_node_lookup_int(state, &retval);
1168 if (error || (retval != -EEXIST)) {
1169 if (error == 0)
1170 error = retval;
1171 goto out;
1172 }
1173
1174
1175
1176
1177
1178
1179 blk = &state->path.blk[ state->path.active-1 ];
1180 ASSERT(blk->bp != NULL);
1181 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1182 if (args->rmtblkno > 0) {
1183
1184
1185
1186
1187
1188 error = xfs_attr_fillstate(state);
1189 if (error)
1190 goto out;
1191
1192
1193
1194
1195
1196 error = xfs_attr3_leaf_setflag(args);
1197 if (error)
1198 goto out;
1199 error = xfs_attr_rmtval_remove(args);
1200 if (error)
1201 goto out;
1202
1203
1204
1205
1206
1207 error = xfs_attr_refillstate(state);
1208 if (error)
1209 goto out;
1210 }
1211
1212
1213
1214
1215 blk = &state->path.blk[ state->path.active-1 ];
1216 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1217 retval = xfs_attr3_leaf_remove(blk->bp, args);
1218 xfs_da3_fixhashpath(state, &state->path);
1219
1220
1221
1222
1223 if (retval && (state->path.active > 1)) {
1224 xfs_bmap_init(args->flist, args->firstblock);
1225 error = xfs_da3_join(state);
1226 if (!error) {
1227 error = xfs_bmap_finish(&args->trans, args->flist,
1228 &committed);
1229 }
1230 if (error) {
1231 ASSERT(committed);
1232 args->trans = NULL;
1233 xfs_bmap_cancel(args->flist);
1234 goto out;
1235 }
1236
1237
1238
1239
1240
1241 if (committed)
1242 xfs_trans_ijoin(args->trans, dp, 0);
1243
1244
1245
1246
1247 error = xfs_trans_roll(&args->trans, dp);
1248 if (error)
1249 goto out;
1250 }
1251
1252
1253
1254
1255 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1256
1257
1258
1259 ASSERT(state->path.active == 1);
1260 ASSERT(state->path.blk[0].bp);
1261 state->path.blk[0].bp = NULL;
1262
1263 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
1264 if (error)
1265 goto out;
1266
1267 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1268 xfs_bmap_init(args->flist, args->firstblock);
1269 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1270
1271 if (!error) {
1272 error = xfs_bmap_finish(&args->trans,
1273 args->flist,
1274 &committed);
1275 }
1276 if (error) {
1277 ASSERT(committed);
1278 args->trans = NULL;
1279 xfs_bmap_cancel(args->flist);
1280 goto out;
1281 }
1282
1283
1284
1285
1286
1287
1288 if (committed)
1289 xfs_trans_ijoin(args->trans, dp, 0);
1290 } else
1291 xfs_trans_brelse(args->trans, bp);
1292 }
1293 error = 0;
1294
1295out:
1296 xfs_da_state_free(state);
1297 return error;
1298}
1299
1300
1301
1302
1303
1304
1305
1306STATIC int
1307xfs_attr_fillstate(xfs_da_state_t *state)
1308{
1309 xfs_da_state_path_t *path;
1310 xfs_da_state_blk_t *blk;
1311 int level;
1312
1313 trace_xfs_attr_fillstate(state->args);
1314
1315
1316
1317
1318
1319 path = &state->path;
1320 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1321 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1322 if (blk->bp) {
1323 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1324 blk->bp = NULL;
1325 } else {
1326 blk->disk_blkno = 0;
1327 }
1328 }
1329
1330
1331
1332
1333
1334 path = &state->altpath;
1335 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1336 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1337 if (blk->bp) {
1338 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1339 blk->bp = NULL;
1340 } else {
1341 blk->disk_blkno = 0;
1342 }
1343 }
1344
1345 return 0;
1346}
1347
1348
1349
1350
1351
1352
1353
1354STATIC int
1355xfs_attr_refillstate(xfs_da_state_t *state)
1356{
1357 xfs_da_state_path_t *path;
1358 xfs_da_state_blk_t *blk;
1359 int level, error;
1360
1361 trace_xfs_attr_refillstate(state->args);
1362
1363
1364
1365
1366
1367 path = &state->path;
1368 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1369 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1370 if (blk->disk_blkno) {
1371 error = xfs_da3_node_read(state->args->trans,
1372 state->args->dp,
1373 blk->blkno, blk->disk_blkno,
1374 &blk->bp, XFS_ATTR_FORK);
1375 if (error)
1376 return error;
1377 } else {
1378 blk->bp = NULL;
1379 }
1380 }
1381
1382
1383
1384
1385
1386 path = &state->altpath;
1387 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1388 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1389 if (blk->disk_blkno) {
1390 error = xfs_da3_node_read(state->args->trans,
1391 state->args->dp,
1392 blk->blkno, blk->disk_blkno,
1393 &blk->bp, XFS_ATTR_FORK);
1394 if (error)
1395 return error;
1396 } else {
1397 blk->bp = NULL;
1398 }
1399 }
1400
1401 return 0;
1402}
1403
1404
1405
1406
1407
1408
1409
1410
1411STATIC int
1412xfs_attr_node_get(xfs_da_args_t *args)
1413{
1414 xfs_da_state_t *state;
1415 xfs_da_state_blk_t *blk;
1416 int error, retval;
1417 int i;
1418
1419 trace_xfs_attr_node_get(args);
1420
1421 state = xfs_da_state_alloc();
1422 state->args = args;
1423 state->mp = args->dp->i_mount;
1424
1425
1426
1427
1428 error = xfs_da3_node_lookup_int(state, &retval);
1429 if (error) {
1430 retval = error;
1431 } else if (retval == -EEXIST) {
1432 blk = &state->path.blk[ state->path.active-1 ];
1433 ASSERT(blk->bp != NULL);
1434 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1435
1436
1437
1438
1439 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1440 if (!retval && (args->rmtblkno > 0)
1441 && !(args->flags & ATTR_KERNOVAL)) {
1442 retval = xfs_attr_rmtval_get(args);
1443 }
1444 }
1445
1446
1447
1448
1449 for (i = 0; i < state->path.active; i++) {
1450 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1451 state->path.blk[i].bp = NULL;
1452 }
1453
1454 xfs_da_state_free(state);
1455 return retval;
1456}
1457