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