]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_attr.c
xfs: allocate xattr buffer on demand
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_attr.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6 #include "libxfs_priv.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_trans_space.h"
25 #include "xfs_trace.h"
26
27 /*
28 * xfs_attr.c
29 *
30 * Provide the external interfaces to manage attribute lists.
31 */
32
33 /*========================================================================
34 * Function prototypes for the kernel.
35 *========================================================================*/
36
37 /*
38 * Internal routines when attribute list fits inside the inode.
39 */
40 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
41
42 /*
43 * Internal routines when attribute list is one block.
44 */
45 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
46 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
47 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
48
49 /*
50 * Internal routines when attribute list is more than one block.
51 */
52 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
53 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
54 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
55 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
56 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
57
58
59 STATIC int
60 xfs_attr_args_init(
61 struct xfs_da_args *args,
62 struct xfs_inode *dp,
63 const unsigned char *name,
64 int flags)
65 {
66
67 if (!name)
68 return -EINVAL;
69
70 memset(args, 0, sizeof(*args));
71 args->geo = dp->i_mount->m_attr_geo;
72 args->whichfork = XFS_ATTR_FORK;
73 args->dp = dp;
74 args->flags = flags;
75 args->name = name;
76 args->namelen = strlen((const char *)name);
77 if (args->namelen >= MAXNAMELEN)
78 return -EFAULT; /* match IRIX behaviour */
79
80 args->hashval = xfs_da_hashname(args->name, args->namelen);
81 return 0;
82 }
83
84 int
85 xfs_inode_hasattr(
86 struct xfs_inode *ip)
87 {
88 if (!XFS_IFORK_Q(ip) ||
89 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
90 ip->i_d.di_anextents == 0))
91 return 0;
92 return 1;
93 }
94
95 /*========================================================================
96 * Overall external interface routines.
97 *========================================================================*/
98
99 /*
100 * Retrieve an extended attribute and its value. Must have ilock.
101 * Returns 0 on successful retrieval, otherwise an error.
102 */
103 int
104 xfs_attr_get_ilocked(
105 struct xfs_inode *ip,
106 struct xfs_da_args *args)
107 {
108 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL));
109
110 if (!xfs_inode_hasattr(ip))
111 return -ENOATTR;
112 else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL)
113 return xfs_attr_shortform_getvalue(args);
114 else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK))
115 return xfs_attr_leaf_get(args);
116 else
117 return xfs_attr_node_get(args);
118 }
119
120 /*
121 * Retrieve an extended attribute by name, and its value if requested.
122 *
123 * If ATTR_KERNOVAL is set in @flags, then the caller does not want the value,
124 * just an indication whether the attribute exists and the size of the value if
125 * it exists. The size is returned in @valuelenp,
126 *
127 * If the attribute is found, but exceeds the size limit set by the caller in
128 * @valuelenp, return -ERANGE with the size of the attribute that was found in
129 * @valuelenp.
130 *
131 * If ATTR_ALLOC is set in @flags, allocate the buffer for the value after
132 * existence of the attribute has been determined. On success, return that
133 * buffer to the caller and leave them to free it. On failure, free any
134 * allocated buffer and ensure the buffer pointer returned to the caller is
135 * null.
136 */
137 int
138 xfs_attr_get(
139 struct xfs_inode *ip,
140 const unsigned char *name,
141 unsigned char **value,
142 int *valuelenp,
143 int flags)
144 {
145 struct xfs_da_args args;
146 uint lock_mode;
147 int error;
148
149 ASSERT((flags & (ATTR_ALLOC | ATTR_KERNOVAL)) || *value);
150
151 XFS_STATS_INC(ip->i_mount, xs_attr_get);
152
153 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
154 return -EIO;
155
156 error = xfs_attr_args_init(&args, ip, name, flags);
157 if (error)
158 return error;
159
160 /* Entirely possible to look up a name which doesn't exist */
161 args.op_flags = XFS_DA_OP_OKNOENT;
162 if (flags & ATTR_ALLOC)
163 args.op_flags |= XFS_DA_OP_ALLOCVAL;
164 else
165 args.value = *value;
166 args.valuelen = *valuelenp;
167
168 lock_mode = xfs_ilock_attr_map_shared(ip);
169 error = xfs_attr_get_ilocked(ip, &args);
170 xfs_iunlock(ip, lock_mode);
171 *valuelenp = args.valuelen;
172
173 /* on error, we have to clean up allocated value buffers */
174 if (error) {
175 if (flags & ATTR_ALLOC) {
176 kmem_free(args.value);
177 *value = NULL;
178 }
179 return error;
180 }
181 *value = args.value;
182 return 0;
183 }
184
185 /*
186 * Calculate how many blocks we need for the new attribute,
187 */
188 STATIC int
189 xfs_attr_calc_size(
190 struct xfs_da_args *args,
191 int *local)
192 {
193 struct xfs_mount *mp = args->dp->i_mount;
194 int size;
195 int nblks;
196
197 /*
198 * Determine space new attribute will use, and if it would be
199 * "local" or "remote" (note: local != inline).
200 */
201 size = xfs_attr_leaf_newentsize(args, local);
202 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
203 if (*local) {
204 if (size > (args->geo->blksize / 2)) {
205 /* Double split possible */
206 nblks *= 2;
207 }
208 } else {
209 /*
210 * Out of line attribute, cannot double split, but
211 * make room for the attribute value itself.
212 */
213 uint dblocks = xfs_attr3_rmt_blocks(mp, args->valuelen);
214 nblks += dblocks;
215 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
216 }
217
218 return nblks;
219 }
220
221 STATIC int
222 xfs_attr_try_sf_addname(
223 struct xfs_inode *dp,
224 struct xfs_da_args *args)
225 {
226
227 struct xfs_mount *mp = dp->i_mount;
228 int error, error2;
229
230 error = xfs_attr_shortform_addname(args);
231 if (error == -ENOSPC)
232 return error;
233
234 /*
235 * Commit the shortform mods, and we're done.
236 * NOTE: this is also the error path (EEXIST, etc).
237 */
238 if (!error && (args->flags & ATTR_KERNOTIME) == 0)
239 xfs_trans_ichgtime(args->trans, dp, XFS_ICHGTIME_CHG);
240
241 if (mp->m_flags & XFS_MOUNT_WSYNC)
242 xfs_trans_set_sync(args->trans);
243
244 error2 = xfs_trans_commit(args->trans);
245 args->trans = NULL;
246 return error ? error : error2;
247 }
248
249 /*
250 * Set the attribute specified in @args.
251 */
252 int
253 xfs_attr_set_args(
254 struct xfs_da_args *args)
255 {
256 struct xfs_inode *dp = args->dp;
257 struct xfs_buf *leaf_bp = NULL;
258 int error;
259
260 /*
261 * If the attribute list is non-existent or a shortform list,
262 * upgrade it to a single-leaf-block attribute list.
263 */
264 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL ||
265 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
266 dp->i_d.di_anextents == 0)) {
267
268 /*
269 * Build initial attribute list (if required).
270 */
271 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
272 xfs_attr_shortform_create(args);
273
274 /*
275 * Try to add the attr to the attribute list in the inode.
276 */
277 error = xfs_attr_try_sf_addname(dp, args);
278 if (error != -ENOSPC)
279 return error;
280
281 /*
282 * It won't fit in the shortform, transform to a leaf block.
283 * GROT: another possible req'mt for a double-split btree op.
284 */
285 error = xfs_attr_shortform_to_leaf(args, &leaf_bp);
286 if (error)
287 return error;
288
289 /*
290 * Prevent the leaf buffer from being unlocked so that a
291 * concurrent AIL push cannot grab the half-baked leaf
292 * buffer and run into problems with the write verifier.
293 * Once we're done rolling the transaction we can release
294 * the hold and add the attr to the leaf.
295 */
296 xfs_trans_bhold(args->trans, leaf_bp);
297 error = xfs_defer_finish(&args->trans);
298 xfs_trans_bhold_release(args->trans, leaf_bp);
299 if (error) {
300 xfs_trans_brelse(args->trans, leaf_bp);
301 return error;
302 }
303 }
304
305 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK))
306 error = xfs_attr_leaf_addname(args);
307 else
308 error = xfs_attr_node_addname(args);
309 return error;
310 }
311
312 /*
313 * Remove the attribute specified in @args.
314 */
315 int
316 xfs_attr_remove_args(
317 struct xfs_da_args *args)
318 {
319 struct xfs_inode *dp = args->dp;
320 int error;
321
322 if (!xfs_inode_hasattr(dp)) {
323 error = -ENOATTR;
324 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
325 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
326 error = xfs_attr_shortform_remove(args);
327 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
328 error = xfs_attr_leaf_removename(args);
329 } else {
330 error = xfs_attr_node_removename(args);
331 }
332
333 return error;
334 }
335
336 int
337 xfs_attr_set(
338 struct xfs_inode *dp,
339 const unsigned char *name,
340 unsigned char *value,
341 int valuelen,
342 int flags)
343 {
344 struct xfs_mount *mp = dp->i_mount;
345 struct xfs_da_args args;
346 struct xfs_trans_res tres;
347 int rsvd = (flags & ATTR_ROOT) != 0;
348 int error, local;
349
350 XFS_STATS_INC(mp, xs_attr_set);
351
352 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
353 return -EIO;
354
355 error = xfs_attr_args_init(&args, dp, name, flags);
356 if (error)
357 return error;
358
359 args.value = value;
360 args.valuelen = valuelen;
361 args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
362 args.total = xfs_attr_calc_size(&args, &local);
363
364 error = xfs_qm_dqattach(dp);
365 if (error)
366 return error;
367
368 /*
369 * If the inode doesn't have an attribute fork, add one.
370 * (inode must not be locked when we call this routine)
371 */
372 if (XFS_IFORK_Q(dp) == 0) {
373 int sf_size = sizeof(xfs_attr_sf_hdr_t) +
374 XFS_ATTR_SF_ENTSIZE_BYNAME(args.namelen, valuelen);
375
376 error = xfs_bmap_add_attrfork(dp, sf_size, rsvd);
377 if (error)
378 return error;
379 }
380
381 tres.tr_logres = M_RES(mp)->tr_attrsetm.tr_logres +
382 M_RES(mp)->tr_attrsetrt.tr_logres * args.total;
383 tres.tr_logcount = XFS_ATTRSET_LOG_COUNT;
384 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
385
386 /*
387 * Root fork attributes can use reserved data blocks for this
388 * operation if necessary
389 */
390 error = xfs_trans_alloc(mp, &tres, args.total, 0,
391 rsvd ? XFS_TRANS_RESERVE : 0, &args.trans);
392 if (error)
393 return error;
394
395 xfs_ilock(dp, XFS_ILOCK_EXCL);
396 error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
397 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
398 XFS_QMOPT_RES_REGBLKS);
399 if (error)
400 goto out_trans_cancel;
401
402 xfs_trans_ijoin(args.trans, dp, 0);
403 error = xfs_attr_set_args(&args);
404 if (error)
405 goto out_trans_cancel;
406 if (!args.trans) {
407 /* shortform attribute has already been committed */
408 goto out_unlock;
409 }
410
411 /*
412 * If this is a synchronous mount, make sure that the
413 * transaction goes to disk before returning to the user.
414 */
415 if (mp->m_flags & XFS_MOUNT_WSYNC)
416 xfs_trans_set_sync(args.trans);
417
418 if ((flags & ATTR_KERNOTIME) == 0)
419 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
420
421 /*
422 * Commit the last in the sequence of transactions.
423 */
424 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
425 error = xfs_trans_commit(args.trans);
426 out_unlock:
427 xfs_iunlock(dp, XFS_ILOCK_EXCL);
428 return error;
429
430 out_trans_cancel:
431 if (args.trans)
432 xfs_trans_cancel(args.trans);
433 goto out_unlock;
434 }
435
436 /*
437 * Generic handler routine to remove a name from an attribute list.
438 * Transitions attribute list from Btree to shortform as necessary.
439 */
440 int
441 xfs_attr_remove(
442 struct xfs_inode *dp,
443 const unsigned char *name,
444 int flags)
445 {
446 struct xfs_mount *mp = dp->i_mount;
447 struct xfs_da_args args;
448 int error;
449
450 XFS_STATS_INC(mp, xs_attr_remove);
451
452 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
453 return -EIO;
454
455 error = xfs_attr_args_init(&args, dp, name, flags);
456 if (error)
457 return error;
458
459 /*
460 * we have no control over the attribute names that userspace passes us
461 * to remove, so we have to allow the name lookup prior to attribute
462 * removal to fail.
463 */
464 args.op_flags = XFS_DA_OP_OKNOENT;
465
466 error = xfs_qm_dqattach(dp);
467 if (error)
468 return error;
469
470 /*
471 * Root fork attributes can use reserved data blocks for this
472 * operation if necessary
473 */
474 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_attrrm,
475 XFS_ATTRRM_SPACE_RES(mp), 0,
476 (flags & ATTR_ROOT) ? XFS_TRANS_RESERVE : 0,
477 &args.trans);
478 if (error)
479 return error;
480
481 xfs_ilock(dp, XFS_ILOCK_EXCL);
482 /*
483 * No need to make quota reservations here. We expect to release some
484 * blocks not allocate in the common case.
485 */
486 xfs_trans_ijoin(args.trans, dp, 0);
487
488 error = xfs_attr_remove_args(&args);
489 if (error)
490 goto out;
491
492 /*
493 * If this is a synchronous mount, make sure that the
494 * transaction goes to disk before returning to the user.
495 */
496 if (mp->m_flags & XFS_MOUNT_WSYNC)
497 xfs_trans_set_sync(args.trans);
498
499 if ((flags & ATTR_KERNOTIME) == 0)
500 xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
501
502 /*
503 * Commit the last in the sequence of transactions.
504 */
505 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
506 error = xfs_trans_commit(args.trans);
507 xfs_iunlock(dp, XFS_ILOCK_EXCL);
508
509 return error;
510
511 out:
512 if (args.trans)
513 xfs_trans_cancel(args.trans);
514 xfs_iunlock(dp, XFS_ILOCK_EXCL);
515 return error;
516 }
517
518 /*========================================================================
519 * External routines when attribute list is inside the inode
520 *========================================================================*/
521
522 /*
523 * Add a name to the shortform attribute list structure
524 * This is the external routine.
525 */
526 STATIC int
527 xfs_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 if (retval)
541 return retval;
542 /*
543 * Since we have removed the old attr, clear ATTR_REPLACE so
544 * that the leaf format add routine won't trip over the attr
545 * not being around.
546 */
547 args->flags &= ~ATTR_REPLACE;
548 }
549
550 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
551 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
552 return -ENOSPC;
553
554 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
555 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
556
557 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
558 if (!forkoff)
559 return -ENOSPC;
560
561 xfs_attr_shortform_add(args, forkoff);
562 return 0;
563 }
564
565
566 /*========================================================================
567 * External routines when attribute list is one block
568 *========================================================================*/
569
570 /*
571 * Add a name to the leaf attribute list structure
572 *
573 * This leaf block cannot have a "remote" value, we only call this routine
574 * if bmap_one_block() says there is only one block (ie: no remote blks).
575 */
576 STATIC int
577 xfs_attr_leaf_addname(
578 struct xfs_da_args *args)
579 {
580 struct xfs_inode *dp;
581 struct xfs_buf *bp;
582 int retval, error, forkoff;
583
584 trace_xfs_attr_leaf_addname(args);
585
586 /*
587 * Read the (only) block in the attribute list in.
588 */
589 dp = args->dp;
590 args->blkno = 0;
591 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
592 if (error)
593 return error;
594
595 /*
596 * Look up the given attribute in the leaf block. Figure out if
597 * the given flags produce an error or call for an atomic rename.
598 */
599 retval = xfs_attr3_leaf_lookup_int(bp, args);
600 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
601 xfs_trans_brelse(args->trans, bp);
602 return retval;
603 } else if (retval == -EEXIST) {
604 if (args->flags & ATTR_CREATE) { /* pure create op */
605 xfs_trans_brelse(args->trans, bp);
606 return retval;
607 }
608
609 trace_xfs_attr_leaf_replace(args);
610
611 /* save the attribute state for later removal*/
612 args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
613 args->blkno2 = args->blkno; /* set 2nd entry info*/
614 args->index2 = args->index;
615 args->rmtblkno2 = args->rmtblkno;
616 args->rmtblkcnt2 = args->rmtblkcnt;
617 args->rmtvaluelen2 = args->rmtvaluelen;
618
619 /*
620 * clear the remote attr state now that it is saved so that the
621 * values reflect the state of the attribute we are about to
622 * add, not the attribute we just found and will remove later.
623 */
624 args->rmtblkno = 0;
625 args->rmtblkcnt = 0;
626 args->rmtvaluelen = 0;
627 }
628
629 /*
630 * Add the attribute to the leaf block, transitioning to a Btree
631 * if required.
632 */
633 retval = xfs_attr3_leaf_add(bp, args);
634 if (retval == -ENOSPC) {
635 /*
636 * Promote the attribute list to the Btree format, then
637 * Commit that transaction so that the node_addname() call
638 * can manage its own transactions.
639 */
640 error = xfs_attr3_leaf_to_node(args);
641 if (error)
642 return error;
643 error = xfs_defer_finish(&args->trans);
644 if (error)
645 return error;
646
647 /*
648 * Commit the current trans (including the inode) and start
649 * a new one.
650 */
651 error = xfs_trans_roll_inode(&args->trans, dp);
652 if (error)
653 return error;
654
655 /*
656 * Fob the whole rest of the problem off on the Btree code.
657 */
658 error = xfs_attr_node_addname(args);
659 return error;
660 }
661
662 /*
663 * Commit the transaction that added the attr name so that
664 * later routines can manage their own transactions.
665 */
666 error = xfs_trans_roll_inode(&args->trans, dp);
667 if (error)
668 return error;
669
670 /*
671 * If there was an out-of-line value, allocate the blocks we
672 * identified for its storage and copy the value. This is done
673 * after we create the attribute so that we don't overflow the
674 * maximum size of a transaction and/or hit a deadlock.
675 */
676 if (args->rmtblkno > 0) {
677 error = xfs_attr_rmtval_set(args);
678 if (error)
679 return error;
680 }
681
682 /*
683 * If this is an atomic rename operation, we must "flip" the
684 * incomplete flags on the "new" and "old" attribute/value pairs
685 * so that one disappears and one appears atomically. Then we
686 * must remove the "old" attribute/value pair.
687 */
688 if (args->op_flags & XFS_DA_OP_RENAME) {
689 /*
690 * In a separate transaction, set the incomplete flag on the
691 * "old" attr and clear the incomplete flag on the "new" attr.
692 */
693 error = xfs_attr3_leaf_flipflags(args);
694 if (error)
695 return error;
696
697 /*
698 * Dismantle the "old" attribute/value pair by removing
699 * a "remote" value (if it exists).
700 */
701 args->index = args->index2;
702 args->blkno = args->blkno2;
703 args->rmtblkno = args->rmtblkno2;
704 args->rmtblkcnt = args->rmtblkcnt2;
705 args->rmtvaluelen = args->rmtvaluelen2;
706 if (args->rmtblkno) {
707 error = xfs_attr_rmtval_remove(args);
708 if (error)
709 return error;
710 }
711
712 /*
713 * Read in the block containing the "old" attr, then
714 * remove the "old" attr from that block (neat, huh!)
715 */
716 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
717 -1, &bp);
718 if (error)
719 return error;
720
721 xfs_attr3_leaf_remove(bp, args);
722
723 /*
724 * If the result is small enough, shrink it all into the inode.
725 */
726 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
727 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
728 /* bp is gone due to xfs_da_shrink_inode */
729 if (error)
730 return error;
731 error = xfs_defer_finish(&args->trans);
732 if (error)
733 return error;
734 }
735
736 /*
737 * Commit the remove and start the next trans in series.
738 */
739 error = xfs_trans_roll_inode(&args->trans, dp);
740
741 } else if (args->rmtblkno > 0) {
742 /*
743 * Added a "remote" value, just clear the incomplete flag.
744 */
745 error = xfs_attr3_leaf_clearflag(args);
746 }
747 return error;
748 }
749
750 /*
751 * Remove a name from the leaf attribute list structure
752 *
753 * This leaf block cannot have a "remote" value, we only call this routine
754 * if bmap_one_block() says there is only one block (ie: no remote blks).
755 */
756 STATIC int
757 xfs_attr_leaf_removename(
758 struct xfs_da_args *args)
759 {
760 struct xfs_inode *dp;
761 struct xfs_buf *bp;
762 int error, forkoff;
763
764 trace_xfs_attr_leaf_removename(args);
765
766 /*
767 * Remove the attribute.
768 */
769 dp = args->dp;
770 args->blkno = 0;
771 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
772 if (error)
773 return error;
774
775 error = xfs_attr3_leaf_lookup_int(bp, args);
776 if (error == -ENOATTR) {
777 xfs_trans_brelse(args->trans, bp);
778 return error;
779 }
780
781 xfs_attr3_leaf_remove(bp, args);
782
783 /*
784 * If the result is small enough, shrink it all into the inode.
785 */
786 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
787 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
788 /* bp is gone due to xfs_da_shrink_inode */
789 if (error)
790 return error;
791 error = xfs_defer_finish(&args->trans);
792 if (error)
793 return error;
794 }
795 return 0;
796 }
797
798 /*
799 * Look up a name in a leaf attribute list structure.
800 *
801 * This leaf block cannot have a "remote" value, we only call this routine
802 * if bmap_one_block() says there is only one block (ie: no remote blks).
803 *
804 * Returns 0 on successful retrieval, otherwise an error.
805 */
806 STATIC int
807 xfs_attr_leaf_get(xfs_da_args_t *args)
808 {
809 struct xfs_buf *bp;
810 int error;
811
812 trace_xfs_attr_leaf_get(args);
813
814 args->blkno = 0;
815 error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
816 if (error)
817 return error;
818
819 error = xfs_attr3_leaf_lookup_int(bp, args);
820 if (error != -EEXIST) {
821 xfs_trans_brelse(args->trans, bp);
822 return error;
823 }
824 error = xfs_attr3_leaf_getvalue(bp, args);
825 xfs_trans_brelse(args->trans, bp);
826 return error;
827 }
828
829 /*========================================================================
830 * External routines when attribute list size > geo->blksize
831 *========================================================================*/
832
833 /*
834 * Add a name to a Btree-format attribute list.
835 *
836 * This will involve walking down the Btree, and may involve splitting
837 * leaf nodes and even splitting intermediate nodes up to and including
838 * the root node (a special case of an intermediate node).
839 *
840 * "Remote" attribute values confuse the issue and atomic rename operations
841 * add a whole extra layer of confusion on top of that.
842 */
843 STATIC int
844 xfs_attr_node_addname(
845 struct xfs_da_args *args)
846 {
847 struct xfs_da_state *state;
848 struct xfs_da_state_blk *blk;
849 struct xfs_inode *dp;
850 struct xfs_mount *mp;
851 int retval, error;
852
853 trace_xfs_attr_node_addname(args);
854
855 /*
856 * Fill in bucket of arguments/results/context to carry around.
857 */
858 dp = args->dp;
859 mp = dp->i_mount;
860 restart:
861 state = xfs_da_state_alloc();
862 state->args = args;
863 state->mp = mp;
864
865 /*
866 * Search to see if name already exists, and get back a pointer
867 * to where it should go.
868 */
869 error = xfs_da3_node_lookup_int(state, &retval);
870 if (error)
871 goto out;
872 blk = &state->path.blk[ state->path.active-1 ];
873 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
874 if ((args->flags & ATTR_REPLACE) && (retval == -ENOATTR)) {
875 goto out;
876 } else if (retval == -EEXIST) {
877 if (args->flags & ATTR_CREATE)
878 goto out;
879
880 trace_xfs_attr_node_replace(args);
881
882 /* save the attribute state for later removal*/
883 args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
884 args->blkno2 = args->blkno; /* set 2nd entry info*/
885 args->index2 = args->index;
886 args->rmtblkno2 = args->rmtblkno;
887 args->rmtblkcnt2 = args->rmtblkcnt;
888 args->rmtvaluelen2 = args->rmtvaluelen;
889
890 /*
891 * clear the remote attr state now that it is saved so that the
892 * values reflect the state of the attribute we are about to
893 * add, not the attribute we just found and will remove later.
894 */
895 args->rmtblkno = 0;
896 args->rmtblkcnt = 0;
897 args->rmtvaluelen = 0;
898 }
899
900 retval = xfs_attr3_leaf_add(blk->bp, state->args);
901 if (retval == -ENOSPC) {
902 if (state->path.active == 1) {
903 /*
904 * Its really a single leaf node, but it had
905 * out-of-line values so it looked like it *might*
906 * have been a b-tree.
907 */
908 xfs_da_state_free(state);
909 state = NULL;
910 error = xfs_attr3_leaf_to_node(args);
911 if (error)
912 goto out;
913 error = xfs_defer_finish(&args->trans);
914 if (error)
915 goto out;
916
917 /*
918 * Commit the node conversion and start the next
919 * trans in the chain.
920 */
921 error = xfs_trans_roll_inode(&args->trans, dp);
922 if (error)
923 goto out;
924
925 goto restart;
926 }
927
928 /*
929 * Split as many Btree elements as required.
930 * This code tracks the new and old attr's location
931 * in the index/blkno/rmtblkno/rmtblkcnt fields and
932 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
933 */
934 error = xfs_da3_split(state);
935 if (error)
936 goto out;
937 error = xfs_defer_finish(&args->trans);
938 if (error)
939 goto out;
940 } else {
941 /*
942 * Addition succeeded, update Btree hashvals.
943 */
944 xfs_da3_fixhashpath(state, &state->path);
945 }
946
947 /*
948 * Kill the state structure, we're done with it and need to
949 * allow the buffers to come back later.
950 */
951 xfs_da_state_free(state);
952 state = NULL;
953
954 /*
955 * Commit the leaf addition or btree split and start the next
956 * trans in the chain.
957 */
958 error = xfs_trans_roll_inode(&args->trans, dp);
959 if (error)
960 goto out;
961
962 /*
963 * If there was an out-of-line value, allocate the blocks we
964 * identified for its storage and copy the value. This is done
965 * after we create the attribute so that we don't overflow the
966 * maximum size of a transaction and/or hit a deadlock.
967 */
968 if (args->rmtblkno > 0) {
969 error = xfs_attr_rmtval_set(args);
970 if (error)
971 return error;
972 }
973
974 /*
975 * If this is an atomic rename operation, we must "flip" the
976 * incomplete flags on the "new" and "old" attribute/value pairs
977 * so that one disappears and one appears atomically. Then we
978 * must remove the "old" attribute/value pair.
979 */
980 if (args->op_flags & XFS_DA_OP_RENAME) {
981 /*
982 * In a separate transaction, set the incomplete flag on the
983 * "old" attr and clear the incomplete flag on the "new" attr.
984 */
985 error = xfs_attr3_leaf_flipflags(args);
986 if (error)
987 goto out;
988
989 /*
990 * Dismantle the "old" attribute/value pair by removing
991 * a "remote" value (if it exists).
992 */
993 args->index = args->index2;
994 args->blkno = args->blkno2;
995 args->rmtblkno = args->rmtblkno2;
996 args->rmtblkcnt = args->rmtblkcnt2;
997 args->rmtvaluelen = args->rmtvaluelen2;
998 if (args->rmtblkno) {
999 error = xfs_attr_rmtval_remove(args);
1000 if (error)
1001 return error;
1002 }
1003
1004 /*
1005 * Re-find the "old" attribute entry after any split ops.
1006 * The INCOMPLETE flag means that we will find the "old"
1007 * attr, not the "new" one.
1008 */
1009 args->flags |= XFS_ATTR_INCOMPLETE;
1010 state = xfs_da_state_alloc();
1011 state->args = args;
1012 state->mp = mp;
1013 state->inleaf = 0;
1014 error = xfs_da3_node_lookup_int(state, &retval);
1015 if (error)
1016 goto out;
1017
1018 /*
1019 * Remove the name and update the hashvals in the tree.
1020 */
1021 blk = &state->path.blk[ state->path.active-1 ];
1022 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1023 error = xfs_attr3_leaf_remove(blk->bp, args);
1024 xfs_da3_fixhashpath(state, &state->path);
1025
1026 /*
1027 * Check to see if the tree needs to be collapsed.
1028 */
1029 if (retval && (state->path.active > 1)) {
1030 error = xfs_da3_join(state);
1031 if (error)
1032 goto out;
1033 error = xfs_defer_finish(&args->trans);
1034 if (error)
1035 goto out;
1036 }
1037
1038 /*
1039 * Commit and start the next trans in the chain.
1040 */
1041 error = xfs_trans_roll_inode(&args->trans, dp);
1042 if (error)
1043 goto out;
1044
1045 } else if (args->rmtblkno > 0) {
1046 /*
1047 * Added a "remote" value, just clear the incomplete flag.
1048 */
1049 error = xfs_attr3_leaf_clearflag(args);
1050 if (error)
1051 goto out;
1052 }
1053 retval = error = 0;
1054
1055 out:
1056 if (state)
1057 xfs_da_state_free(state);
1058 if (error)
1059 return error;
1060 return retval;
1061 }
1062
1063 /*
1064 * Remove a name from a B-tree attribute list.
1065 *
1066 * This will involve walking down the Btree, and may involve joining
1067 * leaf nodes and even joining intermediate nodes up to and including
1068 * the root node (a special case of an intermediate node).
1069 */
1070 STATIC int
1071 xfs_attr_node_removename(
1072 struct xfs_da_args *args)
1073 {
1074 struct xfs_da_state *state;
1075 struct xfs_da_state_blk *blk;
1076 struct xfs_inode *dp;
1077 struct xfs_buf *bp;
1078 int retval, error, forkoff;
1079
1080 trace_xfs_attr_node_removename(args);
1081
1082 /*
1083 * Tie a string around our finger to remind us where we are.
1084 */
1085 dp = args->dp;
1086 state = xfs_da_state_alloc();
1087 state->args = args;
1088 state->mp = dp->i_mount;
1089
1090 /*
1091 * Search to see if name exists, and get back a pointer to it.
1092 */
1093 error = xfs_da3_node_lookup_int(state, &retval);
1094 if (error || (retval != -EEXIST)) {
1095 if (error == 0)
1096 error = retval;
1097 goto out;
1098 }
1099
1100 /*
1101 * If there is an out-of-line value, de-allocate the blocks.
1102 * This is done before we remove the attribute so that we don't
1103 * overflow the maximum size of a transaction and/or hit a deadlock.
1104 */
1105 blk = &state->path.blk[ state->path.active-1 ];
1106 ASSERT(blk->bp != NULL);
1107 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1108 if (args->rmtblkno > 0) {
1109 /*
1110 * Fill in disk block numbers in the state structure
1111 * so that we can get the buffers back after we commit
1112 * several transactions in the following calls.
1113 */
1114 error = xfs_attr_fillstate(state);
1115 if (error)
1116 goto out;
1117
1118 /*
1119 * Mark the attribute as INCOMPLETE, then bunmapi() the
1120 * remote value.
1121 */
1122 error = xfs_attr3_leaf_setflag(args);
1123 if (error)
1124 goto out;
1125 error = xfs_attr_rmtval_remove(args);
1126 if (error)
1127 goto out;
1128
1129 /*
1130 * Refill the state structure with buffers, the prior calls
1131 * released our buffers.
1132 */
1133 error = xfs_attr_refillstate(state);
1134 if (error)
1135 goto out;
1136 }
1137
1138 /*
1139 * Remove the name and update the hashvals in the tree.
1140 */
1141 blk = &state->path.blk[ state->path.active-1 ];
1142 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1143 retval = xfs_attr3_leaf_remove(blk->bp, args);
1144 xfs_da3_fixhashpath(state, &state->path);
1145
1146 /*
1147 * Check to see if the tree needs to be collapsed.
1148 */
1149 if (retval && (state->path.active > 1)) {
1150 error = xfs_da3_join(state);
1151 if (error)
1152 goto out;
1153 error = xfs_defer_finish(&args->trans);
1154 if (error)
1155 goto out;
1156 /*
1157 * Commit the Btree join operation and start a new trans.
1158 */
1159 error = xfs_trans_roll_inode(&args->trans, dp);
1160 if (error)
1161 goto out;
1162 }
1163
1164 /*
1165 * If the result is small enough, push it all into the inode.
1166 */
1167 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1168 /*
1169 * Have to get rid of the copy of this dabuf in the state.
1170 */
1171 ASSERT(state->path.active == 1);
1172 ASSERT(state->path.blk[0].bp);
1173 state->path.blk[0].bp = NULL;
1174
1175 error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
1176 if (error)
1177 goto out;
1178
1179 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1180 error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
1181 /* bp is gone due to xfs_da_shrink_inode */
1182 if (error)
1183 goto out;
1184 error = xfs_defer_finish(&args->trans);
1185 if (error)
1186 goto out;
1187 } else
1188 xfs_trans_brelse(args->trans, bp);
1189 }
1190 error = 0;
1191
1192 out:
1193 xfs_da_state_free(state);
1194 return error;
1195 }
1196
1197 /*
1198 * Fill in the disk block numbers in the state structure for the buffers
1199 * that are attached to the state structure.
1200 * This is done so that we can quickly reattach ourselves to those buffers
1201 * after some set of transaction commits have released these buffers.
1202 */
1203 STATIC int
1204 xfs_attr_fillstate(xfs_da_state_t *state)
1205 {
1206 xfs_da_state_path_t *path;
1207 xfs_da_state_blk_t *blk;
1208 int level;
1209
1210 trace_xfs_attr_fillstate(state->args);
1211
1212 /*
1213 * Roll down the "path" in the state structure, storing the on-disk
1214 * block number for those buffers in the "path".
1215 */
1216 path = &state->path;
1217 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1218 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1219 if (blk->bp) {
1220 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1221 blk->bp = NULL;
1222 } else {
1223 blk->disk_blkno = 0;
1224 }
1225 }
1226
1227 /*
1228 * Roll down the "altpath" in the state structure, storing the on-disk
1229 * block number for those buffers in the "altpath".
1230 */
1231 path = &state->altpath;
1232 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1233 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1234 if (blk->bp) {
1235 blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
1236 blk->bp = NULL;
1237 } else {
1238 blk->disk_blkno = 0;
1239 }
1240 }
1241
1242 return 0;
1243 }
1244
1245 /*
1246 * Reattach the buffers to the state structure based on the disk block
1247 * numbers stored in the state structure.
1248 * This is done after some set of transaction commits have released those
1249 * buffers from our grip.
1250 */
1251 STATIC int
1252 xfs_attr_refillstate(xfs_da_state_t *state)
1253 {
1254 xfs_da_state_path_t *path;
1255 xfs_da_state_blk_t *blk;
1256 int level, error;
1257
1258 trace_xfs_attr_refillstate(state->args);
1259
1260 /*
1261 * Roll down the "path" in the state structure, storing the on-disk
1262 * block number for those buffers in the "path".
1263 */
1264 path = &state->path;
1265 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1266 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1267 if (blk->disk_blkno) {
1268 error = xfs_da3_node_read(state->args->trans,
1269 state->args->dp,
1270 blk->blkno, blk->disk_blkno,
1271 &blk->bp, XFS_ATTR_FORK);
1272 if (error)
1273 return error;
1274 } else {
1275 blk->bp = NULL;
1276 }
1277 }
1278
1279 /*
1280 * Roll down the "altpath" in the state structure, storing the on-disk
1281 * block number for those buffers in the "altpath".
1282 */
1283 path = &state->altpath;
1284 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1285 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1286 if (blk->disk_blkno) {
1287 error = xfs_da3_node_read(state->args->trans,
1288 state->args->dp,
1289 blk->blkno, blk->disk_blkno,
1290 &blk->bp, XFS_ATTR_FORK);
1291 if (error)
1292 return error;
1293 } else {
1294 blk->bp = NULL;
1295 }
1296 }
1297
1298 return 0;
1299 }
1300
1301 /*
1302 * Retrieve the attribute data from a node attribute list.
1303 *
1304 * This routine gets called for any attribute fork that has more than one
1305 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1306 * "remote" values taking up more blocks.
1307 *
1308 * Returns 0 on successful retrieval, otherwise an error.
1309 */
1310 STATIC int
1311 xfs_attr_node_get(xfs_da_args_t *args)
1312 {
1313 xfs_da_state_t *state;
1314 xfs_da_state_blk_t *blk;
1315 int error, retval;
1316 int i;
1317
1318 trace_xfs_attr_node_get(args);
1319
1320 state = xfs_da_state_alloc();
1321 state->args = args;
1322 state->mp = args->dp->i_mount;
1323
1324 /*
1325 * Search to see if name exists, and get back a pointer to it.
1326 */
1327 error = xfs_da3_node_lookup_int(state, &retval);
1328 if (error) {
1329 retval = error;
1330 goto out_release;
1331 }
1332 if (retval != -EEXIST)
1333 goto out_release;
1334
1335 /*
1336 * Get the value, local or "remote"
1337 */
1338 blk = &state->path.blk[state->path.active - 1];
1339 retval = xfs_attr3_leaf_getvalue(blk->bp, args);
1340
1341 /*
1342 * If not in a transaction, we have to release all the buffers.
1343 */
1344 out_release:
1345 for (i = 0; i < state->path.active; i++) {
1346 xfs_trans_brelse(args->trans, state->path.blk[i].bp);
1347 state->path.blk[i].bp = NULL;
1348 }
1349
1350 xfs_da_state_free(state);
1351 return retval;
1352 }
1353
1354 /* Returns true if the attribute entry name is valid. */
1355 bool
1356 xfs_attr_namecheck(
1357 const void *name,
1358 size_t length)
1359 {
1360 /*
1361 * MAXNAMELEN includes the trailing null, but (name/length) leave it
1362 * out, so use >= for the length check.
1363 */
1364 if (length >= MAXNAMELEN)
1365 return false;
1366
1367 /* There shouldn't be any nulls here */
1368 return !memchr(name, 0, length);
1369 }