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#include "xfs_attr_item.h"
28#include "xfs_xattr.h"
29
30struct kmem_cache *xfs_attr_intent_cache;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
46
47
48
49
50STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
51STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
52STATIC int xfs_attr_leaf_hasname(struct xfs_da_args *args, struct xfs_buf **bp);
53STATIC int xfs_attr_leaf_try_add(struct xfs_da_args *args);
54
55
56
57
58STATIC int xfs_attr_node_get(xfs_da_args_t *args);
59STATIC void xfs_attr_restore_rmt_blk(struct xfs_da_args *args);
60static int xfs_attr_node_try_addname(struct xfs_attr_intent *attr);
61STATIC int xfs_attr_node_addname_find_attr(struct xfs_attr_intent *attr);
62STATIC int xfs_attr_node_remove_attr(struct xfs_attr_intent *attr);
63STATIC int xfs_attr_node_lookup(struct xfs_da_args *args,
64 struct xfs_da_state *state);
65
66int
67xfs_inode_hasattr(
68 struct xfs_inode *ip)
69{
70 if (!XFS_IFORK_Q(ip))
71 return 0;
72 if (!ip->i_afp)
73 return 0;
74 if (ip->i_afp->if_format == XFS_DINODE_FMT_EXTENTS &&
75 ip->i_afp->if_nextents == 0)
76 return 0;
77 return 1;
78}
79
80
81
82
83
84bool
85xfs_attr_is_leaf(
86 struct xfs_inode *ip)
87{
88 struct xfs_ifork *ifp = ip->i_afp;
89 struct xfs_iext_cursor icur;
90 struct xfs_bmbt_irec imap;
91
92 if (ifp->if_nextents != 1 || ifp->if_format != XFS_DINODE_FMT_EXTENTS)
93 return false;
94
95 xfs_iext_first(ifp, &icur);
96 xfs_iext_get_extent(ifp, &icur, &imap);
97 return imap.br_startoff == 0 && imap.br_blockcount == 1;
98}
99
100
101
102
103
104
105
106
107
108
109
110
111#if 0
112
113
114
115
116
117
118static int
119xfs_attr_fillstate(xfs_da_state_t *state)
120{
121 xfs_da_state_path_t *path;
122 xfs_da_state_blk_t *blk;
123 int level;
124
125 trace_xfs_attr_fillstate(state->args);
126
127
128
129
130
131 path = &state->path;
132 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
133 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
134 if (blk->bp) {
135 blk->disk_blkno = xfs_buf_daddr(blk->bp);
136 blk->bp = NULL;
137 } else {
138 blk->disk_blkno = 0;
139 }
140 }
141
142
143
144
145
146 path = &state->altpath;
147 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
148 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
149 if (blk->bp) {
150 blk->disk_blkno = xfs_buf_daddr(blk->bp);
151 blk->bp = NULL;
152 } else {
153 blk->disk_blkno = 0;
154 }
155 }
156
157 return 0;
158}
159
160
161
162
163
164
165
166static int
167xfs_attr_refillstate(xfs_da_state_t *state)
168{
169 xfs_da_state_path_t *path;
170 xfs_da_state_blk_t *blk;
171 int level, error;
172
173 trace_xfs_attr_refillstate(state->args);
174
175
176
177
178
179 path = &state->path;
180 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
181 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
182 if (blk->disk_blkno) {
183 error = xfs_da3_node_read_mapped(state->args->trans,
184 state->args->dp, blk->disk_blkno,
185 &blk->bp, XFS_ATTR_FORK);
186 if (error)
187 return error;
188 } else {
189 blk->bp = NULL;
190 }
191 }
192
193
194
195
196
197 path = &state->altpath;
198 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
199 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
200 if (blk->disk_blkno) {
201 error = xfs_da3_node_read_mapped(state->args->trans,
202 state->args->dp, blk->disk_blkno,
203 &blk->bp, XFS_ATTR_FORK);
204 if (error)
205 return error;
206 } else {
207 blk->bp = NULL;
208 }
209 }
210
211 return 0;
212}
213#else
214static int xfs_attr_fillstate(xfs_da_state_t *state) { return 0; }
215#endif
216
217
218
219
220
221
222
223
224
225int
226xfs_attr_get_ilocked(
227 struct xfs_da_args *args)
228{
229 ASSERT(xfs_isilocked(args->dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
230
231 if (!xfs_inode_hasattr(args->dp))
232 return -ENOATTR;
233
234 if (args->dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
235 return xfs_attr_shortform_getvalue(args);
236 if (xfs_attr_is_leaf(args->dp))
237 return xfs_attr_leaf_get(args);
238 return xfs_attr_node_get(args);
239}
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257int
258xfs_attr_get(
259 struct xfs_da_args *args)
260{
261 uint lock_mode;
262 int error;
263
264 XFS_STATS_INC(args->dp->i_mount, xs_attr_get);
265
266 if (xfs_is_shutdown(args->dp->i_mount))
267 return -EIO;
268
269 args->geo = args->dp->i_mount->m_attr_geo;
270 args->whichfork = XFS_ATTR_FORK;
271 args->hashval = xfs_da_hashname(args->name, args->namelen);
272
273
274 args->op_flags = XFS_DA_OP_OKNOENT;
275
276 lock_mode = xfs_ilock_attr_map_shared(args->dp);
277 error = xfs_attr_get_ilocked(args);
278 xfs_iunlock(args->dp, lock_mode);
279
280 return error;
281}
282
283
284
285
286int
287xfs_attr_calc_size(
288 struct xfs_da_args *args,
289 int *local)
290{
291 struct xfs_mount *mp = args->dp->i_mount;
292 int size;
293 int nblks;
294
295
296
297
298
299 size = xfs_attr_leaf_newentsize(args, local);
300 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
301 if (*local) {
302 if (size > (args->geo->blksize / 2)) {
303
304 nblks *= 2;
305 }
306 } else {
307
308
309
310
311 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
312 nblks += dblocks;
313 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
314 }
315
316 return nblks;
317}
318
319
320void
321xfs_init_attr_trans(
322 struct xfs_da_args *args,
323 struct xfs_trans_res *tres,
324 unsigned int *total)
325{
326 struct xfs_mount *mp = args->dp->i_mount;
327
328 if (args->value) {
329 tres->tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
330 M_RES(mp)->tr_attrsetrt.tr_logres *
331 args->total;
332 tres->tr_logcount = XFS_ATTRSET_LOG_COUNT;
333 tres->tr_logflags = XFS_TRANS_PERM_LOG_RES;
334 *total = args->total;
335 } else {
336 *tres = M_RES(mp)->tr_attrrm;
337 *total = XFS_ATTRRM_SPACE_RES(mp);
338 }
339}
340
341
342
343
344
345
346STATIC int
347xfs_attr_try_sf_addname(
348 struct xfs_inode *dp,
349 struct xfs_da_args *args)
350{
351
352 int error;
353
354
355
356
357 if (dp->i_afp->if_format == XFS_DINODE_FMT_EXTENTS)
358 xfs_attr_shortform_create(args);
359
360 error = xfs_attr_shortform_addname(args);
361 if (error == -ENOSPC)
362 return error;
363
364
365
366
367
368 if (!error && !(args->op_flags & XFS_DA_OP_NOTIME))
369 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
370
371 if (xfs_has_wsync(dp->i_mount))
372 xfs_trans_set_sync(args->trans);
373
374 return error;
375}
376
377static int
378xfs_attr_sf_addname(
379 struct xfs_attr_intent *attr)
380{
381 struct xfs_da_args *args = attr->xattri_da_args;
382 struct xfs_inode *dp = args->dp;
383 int error = 0;
384
385 error = xfs_attr_try_sf_addname(dp, args);
386 if (error != -ENOSPC) {
387 ASSERT(!error || error == -EEXIST);
388 attr->xattri_dela_state = XFS_DAS_DONE;
389 goto out;
390 }
391
392
393
394
395
396 error = xfs_attr_shortform_to_leaf(args);
397 if (error)
398 return error;
399
400 attr->xattri_dela_state = XFS_DAS_LEAF_ADD;
401out:
402 trace_xfs_attr_sf_addname_return(attr->xattri_dela_state, args->dp);
403 return error;
404}
405
406
407
408
409
410
411
412
413
414
415
416
417static enum xfs_delattr_state
418xfs_attr_complete_op(
419 struct xfs_attr_intent *attr,
420 enum xfs_delattr_state replace_state)
421{
422 struct xfs_da_args *args = attr->xattri_da_args;
423 bool do_replace = args->op_flags & XFS_DA_OP_REPLACE;
424
425 args->op_flags &= ~XFS_DA_OP_REPLACE;
426 if (do_replace) {
427 args->attr_filter &= ~XFS_ATTR_INCOMPLETE;
428 return replace_state;
429 }
430 return XFS_DAS_DONE;
431}
432
433static int
434xfs_attr_leaf_addname(
435 struct xfs_attr_intent *attr)
436{
437 struct xfs_da_args *args = attr->xattri_da_args;
438 int error;
439
440 ASSERT(xfs_attr_is_leaf(args->dp));
441
442
443
444
445
446 error = xfs_attr_leaf_try_add(args);
447
448 if (error == -ENOSPC) {
449 error = xfs_attr3_leaf_to_node(args);
450 if (error)
451 return error;
452
453
454
455
456
457 attr->xattri_dela_state = XFS_DAS_NODE_ADD;
458 goto out;
459 }
460 if (error)
461 return error;
462
463
464
465
466
467
468 if (args->rmtblkno)
469 attr->xattri_dela_state = XFS_DAS_LEAF_SET_RMT;
470 else
471 attr->xattri_dela_state = xfs_attr_complete_op(attr,
472 XFS_DAS_LEAF_REPLACE);
473out:
474 trace_xfs_attr_leaf_addname_return(attr->xattri_dela_state, args->dp);
475 return error;
476}
477
478
479
480
481
482
483
484
485static int
486xfs_attr_node_addname(
487 struct xfs_attr_intent *attr)
488{
489 struct xfs_da_args *args = attr->xattri_da_args;
490 int error;
491
492 error = xfs_attr_node_addname_find_attr(attr);
493 if (error)
494 return error;
495
496 error = xfs_attr_node_try_addname(attr);
497 if (error == -ENOSPC) {
498 error = xfs_attr3_leaf_to_node(args);
499 if (error)
500 return error;
501
502
503
504
505 goto out;
506 }
507 if (error)
508 return error;
509
510 if (args->rmtblkno)
511 attr->xattri_dela_state = XFS_DAS_NODE_SET_RMT;
512 else
513 attr->xattri_dela_state = xfs_attr_complete_op(attr,
514 XFS_DAS_NODE_REPLACE);
515out:
516 trace_xfs_attr_node_addname_return(attr->xattri_dela_state, args->dp);
517 return error;
518}
519
520static int
521xfs_attr_rmtval_alloc(
522 struct xfs_attr_intent *attr)
523{
524 struct xfs_da_args *args = attr->xattri_da_args;
525 int error = 0;
526
527
528
529
530
531
532
533 if (attr->xattri_blkcnt > 0) {
534 error = xfs_attr_rmtval_set_blk(attr);
535 if (error)
536 return error;
537
538 if (attr->xattri_blkcnt > 0)
539 goto out;
540 }
541
542 error = xfs_attr_rmtval_set_value(args);
543 if (error)
544 return error;
545
546 attr->xattri_dela_state = xfs_attr_complete_op(attr,
547 ++attr->xattri_dela_state);
548
549
550
551
552
553 if (attr->xattri_dela_state == XFS_DAS_DONE)
554 error = xfs_attr3_leaf_clearflag(args);
555out:
556 trace_xfs_attr_rmtval_alloc(attr->xattri_dela_state, args->dp);
557 return error;
558}
559
560
561
562
563
564static int
565xfs_attr_leaf_mark_incomplete(
566 struct xfs_da_args *args,
567 struct xfs_da_state *state)
568{
569 int error;
570
571
572
573
574
575
576 error = xfs_attr_fillstate(state);
577 if (error)
578 return error;
579
580
581
582
583 return xfs_attr3_leaf_setflag(args);
584}
585
586
587static inline void
588xfs_attr_item_init_da_state(
589 struct xfs_attr_intent *attr)
590{
591 struct xfs_da_args *args = attr->xattri_da_args;
592
593 if (!attr->xattri_da_state)
594 attr->xattri_da_state = xfs_da_state_alloc(args);
595 else
596 xfs_da_state_reset(attr->xattri_da_state, args);
597}
598
599
600
601
602
603
604static
605int xfs_attr_node_removename_setup(
606 struct xfs_attr_intent *attr)
607{
608 struct xfs_da_args *args = attr->xattri_da_args;
609 struct xfs_da_state *state;
610 int error;
611
612 xfs_attr_item_init_da_state(attr);
613 error = xfs_attr_node_lookup(args, attr->xattri_da_state);
614 if (error != -EEXIST)
615 goto out;
616 error = 0;
617
618 state = attr->xattri_da_state;
619 ASSERT(state->path.blk[state->path.active - 1].bp != NULL);
620 ASSERT(state->path.blk[state->path.active - 1].magic ==
621 XFS_ATTR_LEAF_MAGIC);
622
623 error = xfs_attr_leaf_mark_incomplete(args, state);
624 if (error)
625 goto out;
626 if (args->rmtblkno > 0)
627 error = xfs_attr_rmtval_invalidate(args);
628out:
629 if (error) {
630 xfs_da_state_free(attr->xattri_da_state);
631 attr->xattri_da_state = NULL;
632 }
633
634 return error;
635}
636
637
638
639
640
641
642static int
643xfs_attr_leaf_remove_attr(
644 struct xfs_attr_intent *attr)
645{
646 struct xfs_da_args *args = attr->xattri_da_args;
647 struct xfs_inode *dp = args->dp;
648 struct xfs_buf *bp = NULL;
649 int forkoff;
650 int error;
651
652 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
653 &bp);
654 if (error)
655 return error;
656
657 xfs_attr3_leaf_remove(bp, args);
658
659 forkoff = xfs_attr_shortform_allfit(bp, dp);
660 if (forkoff)
661 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
662
663
664 return error;
665}
666
667
668
669
670
671
672static int
673xfs_attr_leaf_shrink(
674 struct xfs_da_args *args)
675{
676 struct xfs_inode *dp = args->dp;
677 struct xfs_buf *bp;
678 int forkoff;
679 int error;
680
681 if (!xfs_attr_is_leaf(dp))
682 return 0;
683
684 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
685 if (error)
686 return error;
687
688 forkoff = xfs_attr_shortform_allfit(bp, dp);
689 if (forkoff) {
690 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
691
692 } else {
693 xfs_trans_brelse(args->trans, bp);
694 }
695
696 return error;
697}
698
699
700
701
702
703
704
705
706
707int
708xfs_attr_set_iter(
709 struct xfs_attr_intent *attr)
710{
711 struct xfs_da_args *args = attr->xattri_da_args;
712 int error = 0;
713
714
715next_state:
716 switch (attr->xattri_dela_state) {
717 case XFS_DAS_UNINIT:
718 ASSERT(0);
719 return -EFSCORRUPTED;
720 case XFS_DAS_SF_ADD:
721 return xfs_attr_sf_addname(attr);
722 case XFS_DAS_LEAF_ADD:
723 return xfs_attr_leaf_addname(attr);
724 case XFS_DAS_NODE_ADD:
725 return xfs_attr_node_addname(attr);
726
727 case XFS_DAS_SF_REMOVE:
728 error = xfs_attr_sf_removename(args);
729 attr->xattri_dela_state = xfs_attr_complete_op(attr,
730 xfs_attr_init_add_state(args));
731 break;
732 case XFS_DAS_LEAF_REMOVE:
733 error = xfs_attr_leaf_removename(args);
734 attr->xattri_dela_state = xfs_attr_complete_op(attr,
735 xfs_attr_init_add_state(args));
736 break;
737 case XFS_DAS_NODE_REMOVE:
738 error = xfs_attr_node_removename_setup(attr);
739 if (error == -ENOATTR &&
740 (args->op_flags & XFS_DA_OP_RECOVERY)) {
741 attr->xattri_dela_state = xfs_attr_complete_op(attr,
742 xfs_attr_init_add_state(args));
743 error = 0;
744 break;
745 }
746 if (error)
747 return error;
748 attr->xattri_dela_state = XFS_DAS_NODE_REMOVE_RMT;
749 if (args->rmtblkno == 0)
750 attr->xattri_dela_state++;
751 break;
752
753 case XFS_DAS_LEAF_SET_RMT:
754 case XFS_DAS_NODE_SET_RMT:
755 error = xfs_attr_rmtval_find_space(attr);
756 if (error)
757 return error;
758 attr->xattri_dela_state++;
759 fallthrough;
760
761 case XFS_DAS_LEAF_ALLOC_RMT:
762 case XFS_DAS_NODE_ALLOC_RMT:
763 error = xfs_attr_rmtval_alloc(attr);
764 if (error)
765 return error;
766 if (attr->xattri_dela_state == XFS_DAS_DONE)
767 break;
768 goto next_state;
769
770 case XFS_DAS_LEAF_REPLACE:
771 case XFS_DAS_NODE_REPLACE:
772
773
774
775
776
777 error = xfs_attr3_leaf_flipflags(args);
778 if (error)
779 return error;
780
781
782
783
784 attr->xattri_dela_state++;
785 break;
786
787 case XFS_DAS_LEAF_REMOVE_OLD:
788 case XFS_DAS_NODE_REMOVE_OLD:
789
790
791
792
793
794
795
796 xfs_attr_restore_rmt_blk(args);
797 if (args->rmtblkno) {
798 error = xfs_attr_rmtval_invalidate(args);
799 if (error)
800 return error;
801 } else {
802 attr->xattri_dela_state++;
803 }
804
805 attr->xattri_dela_state++;
806 goto next_state;
807
808 case XFS_DAS_LEAF_REMOVE_RMT:
809 case XFS_DAS_NODE_REMOVE_RMT:
810 error = xfs_attr_rmtval_remove(attr);
811 if (error == -EAGAIN) {
812 error = 0;
813 break;
814 }
815 if (error)
816 return error;
817
818
819
820
821
822
823
824
825
826 attr->xattri_dela_state++;
827 break;
828
829 case XFS_DAS_LEAF_REMOVE_ATTR:
830 error = xfs_attr_leaf_remove_attr(attr);
831 attr->xattri_dela_state = xfs_attr_complete_op(attr,
832 xfs_attr_init_add_state(args));
833 break;
834
835 case XFS_DAS_NODE_REMOVE_ATTR:
836 error = xfs_attr_node_remove_attr(attr);
837 if (!error)
838 error = xfs_attr_leaf_shrink(args);
839 attr->xattri_dela_state = xfs_attr_complete_op(attr,
840 xfs_attr_init_add_state(args));
841 break;
842 default:
843 ASSERT(0);
844 break;
845 }
846
847 trace_xfs_attr_set_iter_return(attr->xattri_dela_state, args->dp);
848 return error;
849}
850
851
852
853
854
855static int
856xfs_attr_lookup(
857 struct xfs_da_args *args)
858{
859 struct xfs_inode *dp = args->dp;
860 struct xfs_buf *bp = NULL;
861 struct xfs_da_state *state;
862 int error;
863
864 if (!xfs_inode_hasattr(dp))
865 return -ENOATTR;
866
867 if (dp->i_afp->if_format == XFS_DINODE_FMT_LOCAL)
868 return xfs_attr_sf_findname(args, NULL, NULL);
869
870 if (xfs_attr_is_leaf(dp)) {
871 error = xfs_attr_leaf_hasname(args, &bp);
872
873 if (bp)
874 xfs_trans_brelse(args->trans, bp);
875
876 return error;
877 }
878
879 state = xfs_da_state_alloc(args);
880 error = xfs_attr_node_lookup(args, state);
881 xfs_da_state_free(state);
882 return error;
883}
884
885static int
886xfs_attr_intent_init(
887 struct xfs_da_args *args,
888 unsigned int op_flags,
889 struct xfs_attr_intent **attr)
890{
891
892 struct xfs_attr_intent *new;
893
894 new = kmem_cache_zalloc(xfs_attr_intent_cache, GFP_NOFS | __GFP_NOFAIL);
895 new->xattri_op_flags = op_flags;
896 new->xattri_da_args = args;
897
898 *attr = new;
899 return 0;
900}
901
902
903static int
904xfs_attr_defer_add(
905 struct xfs_da_args *args)
906{
907 struct xfs_attr_intent *new;
908 int error = 0;
909
910 error = xfs_attr_intent_init(args, XFS_ATTRI_OP_FLAGS_SET, &new);
911 if (error)
912 return error;
913
914 new->xattri_dela_state = xfs_attr_init_add_state(args);
915 xfs_defer_add(args->trans, XFS_DEFER_OPS_TYPE_ATTR, &new->xattri_list);
916 trace_xfs_attr_defer_add(new->xattri_dela_state, args->dp);
917
918 return 0;
919}
920
921
922static int
923xfs_attr_defer_replace(
924 struct xfs_da_args *args)
925{
926 struct xfs_attr_intent *new;
927 int error = 0;
928
929 error = xfs_attr_intent_init(args, XFS_ATTRI_OP_FLAGS_REPLACE, &new);
930 if (error)
931 return error;
932
933 new->xattri_dela_state = xfs_attr_init_replace_state(args);
934 xfs_defer_add(args->trans, XFS_DEFER_OPS_TYPE_ATTR, &new->xattri_list);
935 trace_xfs_attr_defer_replace(new->xattri_dela_state, args->dp);
936
937 return 0;
938}
939
940
941static int
942xfs_attr_defer_remove(
943 struct xfs_da_args *args)
944{
945
946 struct xfs_attr_intent *new;
947 int error;
948
949 error = xfs_attr_intent_init(args, XFS_ATTRI_OP_FLAGS_REMOVE, &new);
950 if (error)
951 return error;
952
953 new->xattri_dela_state = xfs_attr_init_remove_state(args);
954 xfs_defer_add(args->trans, XFS_DEFER_OPS_TYPE_ATTR, &new->xattri_list);
955 trace_xfs_attr_defer_remove(new->xattri_dela_state, args->dp);
956
957 return 0;
958}
959
960
961
962
963
964int
965xfs_attr_set(
966 struct xfs_da_args *args)
967{
968 struct xfs_inode *dp = args->dp;
969 struct xfs_mount *mp = dp->i_mount;
970 struct xfs_trans_res tres;
971 bool rsvd = (args->attr_filter & XFS_ATTR_ROOT);
972 int error, local;
973 int rmt_blks = 0;
974 unsigned int total;
975
976 if (xfs_is_shutdown(dp->i_mount))
977 return -EIO;
978
979 error = xfs_qm_dqattach(dp);
980 if (error)
981 return error;
982
983 args->geo = mp->m_attr_geo;
984 args->whichfork = XFS_ATTR_FORK;
985 args->hashval = xfs_da_hashname(args->name, args->namelen);
986
987
988
989
990
991
992
993 args->op_flags = XFS_DA_OP_OKNOENT |
994 (args->op_flags & XFS_DA_OP_LOGGED);
995
996 if (args->value) {
997 XFS_STATS_INC(mp, xs_attr_set);
998 args->total = xfs_attr_calc_size(args, &local);
999
1000
1001
1002
1003
1004 if (XFS_IFORK_Q(dp) == 0) {
1005 int sf_size = sizeof(struct xfs_attr_sf_hdr) +
1006 xfs_attr_sf_entsize_byname(args->namelen,
1007 args->valuelen);
1008
1009 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
1010 if (error)
1011 return error;
1012 }
1013
1014 if (!local)
1015 rmt_blks = xfs_attr3_rmt_blocks(mp, args->valuelen);
1016 } else {
1017 XFS_STATS_INC(mp, xs_attr_remove);
1018 rmt_blks = xfs_attr3_rmt_blocks(mp, XFS_XATTR_SIZE_MAX);
1019 }
1020
1021
1022
1023
1024
1025 xfs_init_attr_trans(args, &tres, &total);
1026 error = xfs_trans_alloc_inode(dp, &tres, total, 0, rsvd, &args->trans);
1027 if (error)
1028 return error;
1029
1030 if (args->value || xfs_inode_hasattr(dp)) {
1031 error = xfs_iext_count_may_overflow(dp, XFS_ATTR_FORK,
1032 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
1033 if (error == -EFBIG)
1034 error = xfs_iext_count_upgrade(args->trans, dp,
1035 XFS_IEXT_ATTR_MANIP_CNT(rmt_blks));
1036 if (error)
1037 goto out_trans_cancel;
1038 }
1039
1040 error = xfs_attr_lookup(args);
1041 switch (error) {
1042 case -EEXIST:
1043
1044 if (!args->value) {
1045 error = xfs_attr_defer_remove(args);
1046 break;
1047 }
1048
1049 if (args->attr_flags & XATTR_CREATE)
1050 goto out_trans_cancel;
1051
1052 error = xfs_attr_defer_replace(args);
1053 break;
1054 case -ENOATTR:
1055
1056 if (!args->value)
1057 goto out_trans_cancel;
1058
1059
1060 if (args->attr_flags & XATTR_REPLACE)
1061 goto out_trans_cancel;
1062
1063 error = xfs_attr_defer_add(args);
1064 break;
1065 default:
1066 goto out_trans_cancel;
1067 }
1068 if (error)
1069 goto out_trans_cancel;
1070
1071
1072
1073
1074
1075 if (xfs_has_wsync(mp))
1076 xfs_trans_set_sync(args->trans);
1077
1078 if (!(args->op_flags & XFS_DA_OP_NOTIME))
1079 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
1080
1081
1082
1083
1084 xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
1085 error = xfs_trans_commit(args->trans);
1086out_unlock:
1087 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1088 return error;
1089
1090out_trans_cancel:
1091 if (args->trans)
1092 xfs_trans_cancel(args->trans);
1093 goto out_unlock;
1094}
1095
1096
1097
1098
1099
1100static inline int xfs_attr_sf_totsize(struct xfs_inode *dp)
1101{
1102 struct xfs_attr_shortform *sf;
1103
1104 sf = (struct xfs_attr_shortform *)dp->i_afp->if_u1.if_data;
1105 return be16_to_cpu(sf->hdr.totsize);
1106}
1107
1108
1109
1110
1111
1112static int
1113xfs_attr_shortform_addname(
1114 struct xfs_da_args *args)
1115{
1116 int newsize, forkoff;
1117 int error;
1118
1119 trace_xfs_attr_sf_addname(args);
1120
1121 error = xfs_attr_shortform_lookup(args);
1122 switch (error) {
1123 case -ENOATTR:
1124 if (args->op_flags & XFS_DA_OP_REPLACE)
1125 return error;
1126 break;
1127 case -EEXIST:
1128 if (!(args->op_flags & XFS_DA_OP_REPLACE))
1129 return error;
1130
1131 error = xfs_attr_sf_removename(args);
1132 if (error)
1133 return error;
1134
1135
1136
1137
1138
1139
1140
1141 args->op_flags &= ~XFS_DA_OP_REPLACE;
1142 break;
1143 case 0:
1144 break;
1145 default:
1146 return error;
1147 }
1148
1149 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
1150 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
1151 return -ENOSPC;
1152
1153 newsize = xfs_attr_sf_totsize(args->dp);
1154 newsize += xfs_attr_sf_entsize_byname(args->namelen, args->valuelen);
1155
1156 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
1157 if (!forkoff)
1158 return -ENOSPC;
1159
1160 xfs_attr_shortform_add(args, forkoff);
1161 return 0;
1162}
1163
1164
1165
1166
1167
1168
1169
1170static void
1171xfs_attr_save_rmt_blk(
1172 struct xfs_da_args *args)
1173{
1174 args->blkno2 = args->blkno;
1175 args->index2 = args->index;
1176 args->rmtblkno2 = args->rmtblkno;
1177 args->rmtblkcnt2 = args->rmtblkcnt;
1178 args->rmtvaluelen2 = args->rmtvaluelen;
1179 args->rmtblkno = 0;
1180 args->rmtblkcnt = 0;
1181 args->rmtvaluelen = 0;
1182}
1183
1184
1185static void
1186xfs_attr_restore_rmt_blk(
1187 struct xfs_da_args *args)
1188{
1189 args->blkno = args->blkno2;
1190 args->index = args->index2;
1191 args->rmtblkno = args->rmtblkno2;
1192 args->rmtblkcnt = args->rmtblkcnt2;
1193 args->rmtvaluelen = args->rmtvaluelen2;
1194}
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206STATIC int
1207xfs_attr_leaf_try_add(
1208 struct xfs_da_args *args)
1209{
1210 struct xfs_buf *bp;
1211 int error;
1212
1213 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, &bp);
1214 if (error)
1215 return error;
1216
1217
1218
1219
1220 error = xfs_attr3_leaf_lookup_int(bp, args);
1221 switch (error) {
1222 case -ENOATTR:
1223 if (args->op_flags & XFS_DA_OP_REPLACE)
1224 goto out_brelse;
1225 break;
1226 case -EEXIST:
1227 if (!(args->op_flags & XFS_DA_OP_REPLACE))
1228 goto out_brelse;
1229
1230 trace_xfs_attr_leaf_replace(args);
1231
1232
1233
1234
1235
1236 xfs_attr_save_rmt_blk(args);
1237 break;
1238 case 0:
1239 break;
1240 default:
1241 goto out_brelse;
1242 }
1243
1244 return xfs_attr3_leaf_add(bp, args);
1245
1246out_brelse:
1247 xfs_trans_brelse(args->trans, bp);
1248 return error;
1249}
1250
1251
1252
1253
1254STATIC int
1255xfs_attr_leaf_hasname(
1256 struct xfs_da_args *args,
1257 struct xfs_buf **bp)
1258{
1259 int error = 0;
1260
1261 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, bp);
1262 if (error)
1263 return error;
1264
1265 error = xfs_attr3_leaf_lookup_int(*bp, args);
1266 if (error != -ENOATTR && error != -EEXIST)
1267 xfs_trans_brelse(args->trans, *bp);
1268
1269 return error;
1270}
1271
1272
1273
1274
1275
1276
1277
1278STATIC int
1279xfs_attr_leaf_removename(
1280 struct xfs_da_args *args)
1281{
1282 struct xfs_inode *dp;
1283 struct xfs_buf *bp;
1284 int error, forkoff;
1285
1286 trace_xfs_attr_leaf_removename(args);
1287
1288
1289
1290
1291 dp = args->dp;
1292
1293 error = xfs_attr_leaf_hasname(args, &bp);
1294 if (error == -ENOATTR) {
1295 xfs_trans_brelse(args->trans, bp);
1296 if (args->op_flags & XFS_DA_OP_RECOVERY)
1297 return 0;
1298 return error;
1299 } else if (error != -EEXIST)
1300 return error;
1301
1302 xfs_attr3_leaf_remove(bp, args);
1303
1304
1305
1306
1307 forkoff = xfs_attr_shortform_allfit(bp, dp);
1308 if (forkoff)
1309 return xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1310
1311
1312 return 0;
1313}
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323STATIC int
1324xfs_attr_leaf_get(xfs_da_args_t *args)
1325{
1326 struct xfs_buf *bp;
1327 int error;
1328
1329 trace_xfs_attr_leaf_get(args);
1330
1331 error = xfs_attr_leaf_hasname(args, &bp);
1332
1333 if (error == -ENOATTR) {
1334 xfs_trans_brelse(args->trans, bp);
1335 return error;
1336 } else if (error != -EEXIST)
1337 return error;
1338
1339
1340 error = xfs_attr3_leaf_getvalue(bp, args);
1341 xfs_trans_brelse(args->trans, bp);
1342 return error;
1343}
1344
1345
1346STATIC int
1347xfs_attr_node_lookup(
1348 struct xfs_da_args *args,
1349 struct xfs_da_state *state)
1350{
1351 int retval, error;
1352
1353
1354
1355
1356 error = xfs_da3_node_lookup_int(state, &retval);
1357 if (error)
1358 return error;
1359
1360 return retval;
1361}
1362
1363
1364
1365
1366
1367STATIC int
1368xfs_attr_node_addname_find_attr(
1369 struct xfs_attr_intent *attr)
1370{
1371 struct xfs_da_args *args = attr->xattri_da_args;
1372 int error;
1373
1374
1375
1376
1377
1378 xfs_attr_item_init_da_state(attr);
1379 error = xfs_attr_node_lookup(args, attr->xattri_da_state);
1380 switch (error) {
1381 case -ENOATTR:
1382 if (args->op_flags & XFS_DA_OP_REPLACE)
1383 goto error;
1384 break;
1385 case -EEXIST:
1386 if (!(args->op_flags & XFS_DA_OP_REPLACE))
1387 goto error;
1388
1389
1390 trace_xfs_attr_node_replace(args);
1391
1392
1393
1394
1395
1396 xfs_attr_save_rmt_blk(args);
1397 break;
1398 case 0:
1399 break;
1400 default:
1401 goto error;
1402 }
1403
1404 return 0;
1405error:
1406 if (attr->xattri_da_state) {
1407 xfs_da_state_free(attr->xattri_da_state);
1408 attr->xattri_da_state = NULL;
1409 }
1410 return error;
1411}
1412
1413
1414
1415
1416
1417
1418
1419
1420static int
1421xfs_attr_node_try_addname(
1422 struct xfs_attr_intent *attr)
1423{
1424 struct xfs_da_state *state = attr->xattri_da_state;
1425 struct xfs_da_state_blk *blk;
1426 int error;
1427
1428 trace_xfs_attr_node_addname(state->args);
1429
1430 blk = &state->path.blk[state->path.active-1];
1431 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1432
1433 error = xfs_attr3_leaf_add(blk->bp, state->args);
1434 if (error == -ENOSPC) {
1435 if (state->path.active == 1) {
1436
1437
1438
1439
1440
1441 goto out;
1442 }
1443
1444
1445
1446
1447
1448
1449
1450 error = xfs_da3_split(state);
1451 if (error)
1452 goto out;
1453 } else {
1454
1455
1456
1457 xfs_da3_fixhashpath(state, &state->path);
1458 }
1459
1460out:
1461 xfs_da_state_free(state);
1462 attr->xattri_da_state = NULL;
1463 return error;
1464}
1465
1466static int
1467xfs_attr_node_removename(
1468 struct xfs_da_args *args,
1469 struct xfs_da_state *state)
1470{
1471 struct xfs_da_state_blk *blk;
1472 int retval;
1473
1474
1475
1476
1477 blk = &state->path.blk[state->path.active-1];
1478 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1479 retval = xfs_attr3_leaf_remove(blk->bp, args);
1480 xfs_da3_fixhashpath(state, &state->path);
1481
1482 return retval;
1483}
1484
1485static int
1486xfs_attr_node_remove_attr(
1487 struct xfs_attr_intent *attr)
1488{
1489 struct xfs_da_args *args = attr->xattri_da_args;
1490 struct xfs_da_state *state = xfs_da_state_alloc(args);
1491 int retval = 0;
1492 int error = 0;
1493
1494
1495
1496
1497
1498
1499 args->attr_filter |= XFS_ATTR_INCOMPLETE;
1500 error = xfs_da3_node_lookup_int(state, &retval);
1501 if (error)
1502 goto out;
1503
1504 error = xfs_attr_node_removename(args, state);
1505
1506
1507
1508
1509 if (retval && (state->path.active > 1)) {
1510 error = xfs_da3_join(state);
1511 if (error)
1512 goto out;
1513 }
1514 retval = error = 0;
1515
1516out:
1517 xfs_da_state_free(state);
1518 if (error)
1519 return error;
1520 return retval;
1521}
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532STATIC int
1533xfs_attr_node_get(
1534 struct xfs_da_args *args)
1535{
1536 struct xfs_da_state *state;
1537 struct xfs_da_state_blk *blk;
1538 int i;
1539 int error;
1540
1541 trace_xfs_attr_node_get(args);
1542
1543
1544
1545
1546 state = xfs_da_state_alloc(args);
1547 error = xfs_attr_node_lookup(args, state);
1548 if (error != -EEXIST)
1549 goto out_release;
1550
1551
1552
1553
1554 blk = &state->path.blk[state->path.active - 1];
1555 error = xfs_attr3_leaf_getvalue(blk->bp, args);
1556
1557
1558
1559
1560out_release:
1561 for (i = 0; state != NULL && i < state->path.active; i++) {
1562 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1563 state->path.blk[i].bp = NULL;
1564 }
1565
1566 xfs_da_state_free(state);
1567 return error;
1568}
1569
1570
1571bool
1572xfs_attr_namecheck(
1573 const void *name,
1574 size_t length)
1575{
1576
1577
1578
1579
1580 if (length >= MAXNAMELEN)
1581 return false;
1582
1583
1584 return !memchr(name, 0, length);
1585}
1586
1587int __init
1588xfs_attr_intent_init_cache(void)
1589{
1590 xfs_attr_intent_cache = kmem_cache_create("xfs_attr_intent",
1591 sizeof(struct xfs_attr_intent),
1592 0, 0, NULL);
1593
1594 return xfs_attr_intent_cache != NULL ? 0 : -ENOMEM;
1595}
1596
1597void
1598xfs_attr_intent_destroy_cache(void)
1599{
1600 kmem_cache_destroy(xfs_attr_intent_cache);
1601 xfs_attr_intent_cache = NULL;
1602}
1603