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