[PATCH] powerpc: Add flattened device tree documentation
[linux-2.6/zen-sources.git] / fs / xfs / xfs_attr.c
blobe5e91e9c7e898d9f65299813115b8373c22e4c26
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <linux/capability.h>
21 #include "xfs.h"
22 #include "xfs_fs.h"
23 #include "xfs_types.h"
24 #include "xfs_bit.h"
25 #include "xfs_log.h"
26 #include "xfs_inum.h"
27 #include "xfs_trans.h"
28 #include "xfs_sb.h"
29 #include "xfs_ag.h"
30 #include "xfs_dir.h"
31 #include "xfs_dir2.h"
32 #include "xfs_dmapi.h"
33 #include "xfs_mount.h"
34 #include "xfs_da_btree.h"
35 #include "xfs_bmap_btree.h"
36 #include "xfs_alloc_btree.h"
37 #include "xfs_ialloc_btree.h"
38 #include "xfs_dir_sf.h"
39 #include "xfs_dir2_sf.h"
40 #include "xfs_attr_sf.h"
41 #include "xfs_dinode.h"
42 #include "xfs_inode.h"
43 #include "xfs_alloc.h"
44 #include "xfs_btree.h"
45 #include "xfs_inode_item.h"
46 #include "xfs_bmap.h"
47 #include "xfs_attr.h"
48 #include "xfs_attr_leaf.h"
49 #include "xfs_error.h"
50 #include "xfs_quota.h"
51 #include "xfs_trans_space.h"
52 #include "xfs_acl.h"
53 #include "xfs_rw.h"
56 * xfs_attr.c
58 * Provide the external interfaces to manage attribute lists.
61 #define ATTR_SYSCOUNT 2
62 STATIC struct attrnames posix_acl_access;
63 STATIC struct attrnames posix_acl_default;
64 STATIC struct attrnames *attr_system_names[ATTR_SYSCOUNT];
66 /*========================================================================
67 * Function prototypes for the kernel.
68 *========================================================================*/
71 * Internal routines when attribute list fits inside the inode.
73 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
76 * Internal routines when attribute list is one block.
78 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
79 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
80 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
81 STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
84 * Internal routines when attribute list is more than one block.
86 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
87 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
88 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
89 STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
90 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
91 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
94 * Routines to manipulate out-of-line attribute values.
96 STATIC int xfs_attr_rmtval_get(xfs_da_args_t *args);
97 STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
98 STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
100 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
102 #if defined(XFS_ATTR_TRACE)
103 ktrace_t *xfs_attr_trace_buf;
104 #endif
107 /*========================================================================
108 * Overall external interface routines.
109 *========================================================================*/
112 xfs_attr_fetch(xfs_inode_t *ip, const char *name, int namelen,
113 char *value, int *valuelenp, int flags, struct cred *cred)
115 xfs_da_args_t args;
116 int error;
118 if ((XFS_IFORK_Q(ip) == 0) ||
119 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
120 ip->i_d.di_anextents == 0))
121 return(ENOATTR);
124 * Fill in the arg structure for this request.
126 memset((char *)&args, 0, sizeof(args));
127 args.name = name;
128 args.namelen = namelen;
129 args.value = value;
130 args.valuelen = *valuelenp;
131 args.flags = flags;
132 args.hashval = xfs_da_hashname(args.name, args.namelen);
133 args.dp = ip;
134 args.whichfork = XFS_ATTR_FORK;
137 * Decide on what work routines to call based on the inode size.
139 if (XFS_IFORK_Q(ip) == 0 ||
140 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
141 ip->i_d.di_anextents == 0)) {
142 error = XFS_ERROR(ENOATTR);
143 } else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
144 error = xfs_attr_shortform_getvalue(&args);
145 } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
146 error = xfs_attr_leaf_get(&args);
147 } else {
148 error = xfs_attr_node_get(&args);
152 * Return the number of bytes in the value to the caller.
154 *valuelenp = args.valuelen;
156 if (error == EEXIST)
157 error = 0;
158 return(error);
162 xfs_attr_get(bhv_desc_t *bdp, const char *name, char *value, int *valuelenp,
163 int flags, struct cred *cred)
165 xfs_inode_t *ip = XFS_BHVTOI(bdp);
166 int error, namelen;
168 XFS_STATS_INC(xs_attr_get);
170 if (!name)
171 return(EINVAL);
172 namelen = strlen(name);
173 if (namelen >= MAXNAMELEN)
174 return(EFAULT); /* match IRIX behaviour */
176 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
177 return(EIO);
179 xfs_ilock(ip, XFS_ILOCK_SHARED);
180 error = xfs_attr_fetch(ip, name, namelen, value, valuelenp, flags, cred);
181 xfs_iunlock(ip, XFS_ILOCK_SHARED);
182 return(error);
185 STATIC int
186 xfs_attr_set_int(xfs_inode_t *dp, const char *name, int namelen,
187 char *value, int valuelen, int flags)
189 xfs_da_args_t args;
190 xfs_fsblock_t firstblock;
191 xfs_bmap_free_t flist;
192 int error, err2, committed;
193 int local, size;
194 uint nblks;
195 xfs_mount_t *mp = dp->i_mount;
196 int rsvd = (flags & ATTR_ROOT) != 0;
199 * Attach the dquots to the inode.
201 if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
202 return (error);
205 * Determine space new attribute will use, and if it would be
206 * "local" or "remote" (note: local != inline).
208 size = xfs_attr_leaf_newentsize(namelen, valuelen,
209 mp->m_sb.sb_blocksize, &local);
212 * If the inode doesn't have an attribute fork, add one.
213 * (inode must not be locked when we call this routine)
215 if (XFS_IFORK_Q(dp) == 0) {
216 if ((error = xfs_bmap_add_attrfork(dp, size, rsvd)))
217 return(error);
221 * Fill in the arg structure for this request.
223 memset((char *)&args, 0, sizeof(args));
224 args.name = name;
225 args.namelen = namelen;
226 args.value = value;
227 args.valuelen = valuelen;
228 args.flags = flags;
229 args.hashval = xfs_da_hashname(args.name, args.namelen);
230 args.dp = dp;
231 args.firstblock = &firstblock;
232 args.flist = &flist;
233 args.whichfork = XFS_ATTR_FORK;
234 args.addname = 1;
235 args.oknoent = 1;
237 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
238 if (local) {
239 if (size > (mp->m_sb.sb_blocksize >> 1)) {
240 /* Double split possible */
241 nblks <<= 1;
243 } else {
244 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
245 /* Out of line attribute, cannot double split, but make
246 * room for the attribute value itself.
248 nblks += dblocks;
249 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
252 /* Size is now blocks for attribute data */
253 args.total = nblks;
256 * Start our first transaction of the day.
258 * All future transactions during this code must be "chained" off
259 * this one via the trans_dup() call. All transactions will contain
260 * the inode, and the inode will always be marked with trans_ihold().
261 * Since the inode will be locked in all transactions, we must log
262 * the inode in every transaction to let it float upward through
263 * the log.
265 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
268 * Root fork attributes can use reserved data blocks for this
269 * operation if necessary
272 if (rsvd)
273 args.trans->t_flags |= XFS_TRANS_RESERVE;
275 if ((error = xfs_trans_reserve(args.trans, (uint) nblks,
276 XFS_ATTRSET_LOG_RES(mp, nblks),
277 0, XFS_TRANS_PERM_LOG_RES,
278 XFS_ATTRSET_LOG_COUNT))) {
279 xfs_trans_cancel(args.trans, 0);
280 return(error);
282 xfs_ilock(dp, XFS_ILOCK_EXCL);
284 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, args.trans, dp, nblks, 0,
285 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
286 XFS_QMOPT_RES_REGBLKS);
287 if (error) {
288 xfs_iunlock(dp, XFS_ILOCK_EXCL);
289 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
290 return (error);
293 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
294 xfs_trans_ihold(args.trans, dp);
297 * If the attribute list is non-existant or a shortform list,
298 * upgrade it to a single-leaf-block attribute list.
300 if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
301 ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
302 (dp->i_d.di_anextents == 0))) {
305 * Build initial attribute list (if required).
307 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
308 xfs_attr_shortform_create(&args);
311 * Try to add the attr to the attribute list in
312 * the inode.
314 error = xfs_attr_shortform_addname(&args);
315 if (error != ENOSPC) {
317 * Commit the shortform mods, and we're done.
318 * NOTE: this is also the error path (EEXIST, etc).
320 ASSERT(args.trans != NULL);
323 * If this is a synchronous mount, make sure that
324 * the transaction goes to disk before returning
325 * to the user.
327 if (mp->m_flags & XFS_MOUNT_WSYNC) {
328 xfs_trans_set_sync(args.trans);
330 err2 = xfs_trans_commit(args.trans,
331 XFS_TRANS_RELEASE_LOG_RES,
332 NULL);
333 xfs_iunlock(dp, XFS_ILOCK_EXCL);
336 * Hit the inode change time.
338 if (!error && (flags & ATTR_KERNOTIME) == 0) {
339 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
341 return(error == 0 ? err2 : error);
345 * It won't fit in the shortform, transform to a leaf block.
346 * GROT: another possible req'mt for a double-split btree op.
348 XFS_BMAP_INIT(args.flist, args.firstblock);
349 error = xfs_attr_shortform_to_leaf(&args);
350 if (!error) {
351 error = xfs_bmap_finish(&args.trans, args.flist,
352 *args.firstblock, &committed);
354 if (error) {
355 ASSERT(committed);
356 args.trans = NULL;
357 xfs_bmap_cancel(&flist);
358 goto out;
362 * bmap_finish() may have committed the last trans and started
363 * a new one. We need the inode to be in all transactions.
365 if (committed) {
366 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
367 xfs_trans_ihold(args.trans, dp);
371 * Commit the leaf transformation. We'll need another (linked)
372 * transaction to add the new attribute to the leaf.
374 if ((error = xfs_attr_rolltrans(&args.trans, dp)))
375 goto out;
379 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
380 error = xfs_attr_leaf_addname(&args);
381 } else {
382 error = xfs_attr_node_addname(&args);
384 if (error) {
385 goto out;
389 * If this is a synchronous mount, make sure that the
390 * transaction goes to disk before returning to the user.
392 if (mp->m_flags & XFS_MOUNT_WSYNC) {
393 xfs_trans_set_sync(args.trans);
397 * Commit the last in the sequence of transactions.
399 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
400 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES,
401 NULL);
402 xfs_iunlock(dp, XFS_ILOCK_EXCL);
405 * Hit the inode change time.
407 if (!error && (flags & ATTR_KERNOTIME) == 0) {
408 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
411 return(error);
413 out:
414 if (args.trans)
415 xfs_trans_cancel(args.trans,
416 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
417 xfs_iunlock(dp, XFS_ILOCK_EXCL);
418 return(error);
422 xfs_attr_set(bhv_desc_t *bdp, const char *name, char *value, int valuelen, int flags,
423 struct cred *cred)
425 xfs_inode_t *dp;
426 int namelen;
428 namelen = strlen(name);
429 if (namelen >= MAXNAMELEN)
430 return EFAULT; /* match IRIX behaviour */
432 XFS_STATS_INC(xs_attr_set);
434 dp = XFS_BHVTOI(bdp);
435 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
436 return (EIO);
438 return xfs_attr_set_int(dp, name, namelen, value, valuelen, flags);
442 * Generic handler routine to remove a name from an attribute list.
443 * Transitions attribute list from Btree to shortform as necessary.
445 STATIC int
446 xfs_attr_remove_int(xfs_inode_t *dp, const char *name, int namelen, int flags)
448 xfs_da_args_t args;
449 xfs_fsblock_t firstblock;
450 xfs_bmap_free_t flist;
451 int error;
452 xfs_mount_t *mp = dp->i_mount;
455 * Fill in the arg structure for this request.
457 memset((char *)&args, 0, sizeof(args));
458 args.name = name;
459 args.namelen = namelen;
460 args.flags = flags;
461 args.hashval = xfs_da_hashname(args.name, args.namelen);
462 args.dp = dp;
463 args.firstblock = &firstblock;
464 args.flist = &flist;
465 args.total = 0;
466 args.whichfork = XFS_ATTR_FORK;
469 * Attach the dquots to the inode.
471 if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
472 return (error);
475 * Start our first transaction of the day.
477 * All future transactions during this code must be "chained" off
478 * this one via the trans_dup() call. All transactions will contain
479 * the inode, and the inode will always be marked with trans_ihold().
480 * Since the inode will be locked in all transactions, we must log
481 * the inode in every transaction to let it float upward through
482 * the log.
484 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
487 * Root fork attributes can use reserved data blocks for this
488 * operation if necessary
491 if (flags & ATTR_ROOT)
492 args.trans->t_flags |= XFS_TRANS_RESERVE;
494 if ((error = xfs_trans_reserve(args.trans,
495 XFS_ATTRRM_SPACE_RES(mp),
496 XFS_ATTRRM_LOG_RES(mp),
497 0, XFS_TRANS_PERM_LOG_RES,
498 XFS_ATTRRM_LOG_COUNT))) {
499 xfs_trans_cancel(args.trans, 0);
500 return(error);
503 xfs_ilock(dp, XFS_ILOCK_EXCL);
505 * No need to make quota reservations here. We expect to release some
506 * blocks not allocate in the common case.
508 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
509 xfs_trans_ihold(args.trans, dp);
512 * Decide on what work routines to call based on the inode size.
514 if (XFS_IFORK_Q(dp) == 0 ||
515 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
516 dp->i_d.di_anextents == 0)) {
517 error = XFS_ERROR(ENOATTR);
518 goto out;
520 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
521 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
522 error = xfs_attr_shortform_remove(&args);
523 if (error) {
524 goto out;
526 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
527 error = xfs_attr_leaf_removename(&args);
528 } else {
529 error = xfs_attr_node_removename(&args);
531 if (error) {
532 goto out;
536 * If this is a synchronous mount, make sure that the
537 * transaction goes to disk before returning to the user.
539 if (mp->m_flags & XFS_MOUNT_WSYNC) {
540 xfs_trans_set_sync(args.trans);
544 * Commit the last in the sequence of transactions.
546 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
547 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES,
548 NULL);
549 xfs_iunlock(dp, XFS_ILOCK_EXCL);
552 * Hit the inode change time.
554 if (!error && (flags & ATTR_KERNOTIME) == 0) {
555 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
558 return(error);
560 out:
561 if (args.trans)
562 xfs_trans_cancel(args.trans,
563 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
564 xfs_iunlock(dp, XFS_ILOCK_EXCL);
565 return(error);
569 xfs_attr_remove(bhv_desc_t *bdp, const char *name, int flags, struct cred *cred)
571 xfs_inode_t *dp;
572 int namelen;
574 namelen = strlen(name);
575 if (namelen >= MAXNAMELEN)
576 return EFAULT; /* match IRIX behaviour */
578 XFS_STATS_INC(xs_attr_remove);
580 dp = XFS_BHVTOI(bdp);
581 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
582 return (EIO);
584 xfs_ilock(dp, XFS_ILOCK_SHARED);
585 if (XFS_IFORK_Q(dp) == 0 ||
586 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
587 dp->i_d.di_anextents == 0)) {
588 xfs_iunlock(dp, XFS_ILOCK_SHARED);
589 return(XFS_ERROR(ENOATTR));
591 xfs_iunlock(dp, XFS_ILOCK_SHARED);
593 return xfs_attr_remove_int(dp, name, namelen, flags);
597 * Generate a list of extended attribute names and optionally
598 * also value lengths. Positive return value follows the XFS
599 * convention of being an error, zero or negative return code
600 * is the length of the buffer returned (negated), indicating
601 * success.
604 xfs_attr_list(bhv_desc_t *bdp, char *buffer, int bufsize, int flags,
605 attrlist_cursor_kern_t *cursor, struct cred *cred)
607 xfs_attr_list_context_t context;
608 xfs_inode_t *dp;
609 int error;
611 XFS_STATS_INC(xs_attr_list);
614 * Validate the cursor.
616 if (cursor->pad1 || cursor->pad2)
617 return(XFS_ERROR(EINVAL));
618 if ((cursor->initted == 0) &&
619 (cursor->hashval || cursor->blkno || cursor->offset))
620 return(XFS_ERROR(EINVAL));
623 * Check for a properly aligned buffer.
625 if (((long)buffer) & (sizeof(int)-1))
626 return(XFS_ERROR(EFAULT));
627 if (flags & ATTR_KERNOVAL)
628 bufsize = 0;
631 * Initialize the output buffer.
633 context.dp = dp = XFS_BHVTOI(bdp);
634 context.cursor = cursor;
635 context.count = 0;
636 context.dupcnt = 0;
637 context.resynch = 1;
638 context.flags = flags;
639 if (!(flags & ATTR_KERNAMELS)) {
640 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
641 context.firstu = context.bufsize;
642 context.alist = (attrlist_t *)buffer;
643 context.alist->al_count = 0;
644 context.alist->al_more = 0;
645 context.alist->al_offset[0] = context.bufsize;
647 else {
648 context.bufsize = bufsize;
649 context.firstu = context.bufsize;
650 context.alist = (attrlist_t *)buffer;
653 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
654 return (EIO);
656 xfs_ilock(dp, XFS_ILOCK_SHARED);
658 * Decide on what work routines to call based on the inode size.
660 xfs_attr_trace_l_c("syscall start", &context);
661 if (XFS_IFORK_Q(dp) == 0 ||
662 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
663 dp->i_d.di_anextents == 0)) {
664 error = 0;
665 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
666 error = xfs_attr_shortform_list(&context);
667 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
668 error = xfs_attr_leaf_list(&context);
669 } else {
670 error = xfs_attr_node_list(&context);
672 xfs_iunlock(dp, XFS_ILOCK_SHARED);
673 xfs_attr_trace_l_c("syscall end", &context);
675 if (!(context.flags & (ATTR_KERNOVAL|ATTR_KERNAMELS))) {
676 ASSERT(error >= 0);
678 else { /* must return negated buffer size or the error */
679 if (context.count < 0)
680 error = XFS_ERROR(ERANGE);
681 else
682 error = -context.count;
685 return(error);
688 int /* error */
689 xfs_attr_inactive(xfs_inode_t *dp)
691 xfs_trans_t *trans;
692 xfs_mount_t *mp;
693 int error;
695 mp = dp->i_mount;
696 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
698 xfs_ilock(dp, XFS_ILOCK_SHARED);
699 if ((XFS_IFORK_Q(dp) == 0) ||
700 (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
701 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
702 dp->i_d.di_anextents == 0)) {
703 xfs_iunlock(dp, XFS_ILOCK_SHARED);
704 return(0);
706 xfs_iunlock(dp, XFS_ILOCK_SHARED);
709 * Start our first transaction of the day.
711 * All future transactions during this code must be "chained" off
712 * this one via the trans_dup() call. All transactions will contain
713 * the inode, and the inode will always be marked with trans_ihold().
714 * Since the inode will be locked in all transactions, we must log
715 * the inode in every transaction to let it float upward through
716 * the log.
718 trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
719 if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
720 XFS_TRANS_PERM_LOG_RES,
721 XFS_ATTRINVAL_LOG_COUNT))) {
722 xfs_trans_cancel(trans, 0);
723 return(error);
725 xfs_ilock(dp, XFS_ILOCK_EXCL);
728 * No need to make quota reservations here. We expect to release some
729 * blocks, not allocate, in the common case.
731 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
732 xfs_trans_ihold(trans, dp);
735 * Decide on what work routines to call based on the inode size.
737 if ((XFS_IFORK_Q(dp) == 0) ||
738 (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
739 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
740 dp->i_d.di_anextents == 0)) {
741 error = 0;
742 goto out;
744 error = xfs_attr_root_inactive(&trans, dp);
745 if (error)
746 goto out;
748 * signal synchronous inactive transactions unless this
749 * is a synchronous mount filesystem in which case we
750 * know that we're here because we've been called out of
751 * xfs_inactive which means that the last reference is gone
752 * and the unlink transaction has already hit the disk so
753 * async inactive transactions are safe.
755 if ((error = xfs_itruncate_finish(&trans, dp, 0LL, XFS_ATTR_FORK,
756 (!(mp->m_flags & XFS_MOUNT_WSYNC)
757 ? 1 : 0))))
758 goto out;
761 * Commit the last in the sequence of transactions.
763 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
764 error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES,
765 NULL);
766 xfs_iunlock(dp, XFS_ILOCK_EXCL);
768 return(error);
770 out:
771 xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
772 xfs_iunlock(dp, XFS_ILOCK_EXCL);
773 return(error);
778 /*========================================================================
779 * External routines when attribute list is inside the inode
780 *========================================================================*/
783 * Add a name to the shortform attribute list structure
784 * This is the external routine.
786 STATIC int
787 xfs_attr_shortform_addname(xfs_da_args_t *args)
789 int newsize, forkoff, retval;
791 retval = xfs_attr_shortform_lookup(args);
792 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
793 return(retval);
794 } else if (retval == EEXIST) {
795 if (args->flags & ATTR_CREATE)
796 return(retval);
797 retval = xfs_attr_shortform_remove(args);
798 ASSERT(retval == 0);
801 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
802 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
803 return(XFS_ERROR(ENOSPC));
805 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
806 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
808 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
809 if (!forkoff)
810 return(XFS_ERROR(ENOSPC));
812 xfs_attr_shortform_add(args, forkoff);
813 return(0);
817 /*========================================================================
818 * External routines when attribute list is one block
819 *========================================================================*/
822 * Add a name to the leaf attribute list structure
824 * This leaf block cannot have a "remote" value, we only call this routine
825 * if bmap_one_block() says there is only one block (ie: no remote blks).
828 xfs_attr_leaf_addname(xfs_da_args_t *args)
830 xfs_inode_t *dp;
831 xfs_dabuf_t *bp;
832 int retval, error, committed, forkoff;
835 * Read the (only) block in the attribute list in.
837 dp = args->dp;
838 args->blkno = 0;
839 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
840 XFS_ATTR_FORK);
841 if (error)
842 return(error);
843 ASSERT(bp != NULL);
846 * Look up the given attribute in the leaf block. Figure out if
847 * the given flags produce an error or call for an atomic rename.
849 retval = xfs_attr_leaf_lookup_int(bp, args);
850 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
851 xfs_da_brelse(args->trans, bp);
852 return(retval);
853 } else if (retval == EEXIST) {
854 if (args->flags & ATTR_CREATE) { /* pure create op */
855 xfs_da_brelse(args->trans, bp);
856 return(retval);
858 args->rename = 1; /* an atomic rename */
859 args->blkno2 = args->blkno; /* set 2nd entry info*/
860 args->index2 = args->index;
861 args->rmtblkno2 = args->rmtblkno;
862 args->rmtblkcnt2 = args->rmtblkcnt;
866 * Add the attribute to the leaf block, transitioning to a Btree
867 * if required.
869 retval = xfs_attr_leaf_add(bp, args);
870 xfs_da_buf_done(bp);
871 if (retval == ENOSPC) {
873 * Promote the attribute list to the Btree format, then
874 * Commit that transaction so that the node_addname() call
875 * can manage its own transactions.
877 XFS_BMAP_INIT(args->flist, args->firstblock);
878 error = xfs_attr_leaf_to_node(args);
879 if (!error) {
880 error = xfs_bmap_finish(&args->trans, args->flist,
881 *args->firstblock, &committed);
883 if (error) {
884 ASSERT(committed);
885 args->trans = NULL;
886 xfs_bmap_cancel(args->flist);
887 return(error);
891 * bmap_finish() may have committed the last trans and started
892 * a new one. We need the inode to be in all transactions.
894 if (committed) {
895 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
896 xfs_trans_ihold(args->trans, dp);
900 * Commit the current trans (including the inode) and start
901 * a new one.
903 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
904 return (error);
907 * Fob the whole rest of the problem off on the Btree code.
909 error = xfs_attr_node_addname(args);
910 return(error);
914 * Commit the transaction that added the attr name so that
915 * later routines can manage their own transactions.
917 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
918 return (error);
921 * If there was an out-of-line value, allocate the blocks we
922 * identified for its storage and copy the value. This is done
923 * after we create the attribute so that we don't overflow the
924 * maximum size of a transaction and/or hit a deadlock.
926 if (args->rmtblkno > 0) {
927 error = xfs_attr_rmtval_set(args);
928 if (error)
929 return(error);
933 * If this is an atomic rename operation, we must "flip" the
934 * incomplete flags on the "new" and "old" attribute/value pairs
935 * so that one disappears and one appears atomically. Then we
936 * must remove the "old" attribute/value pair.
938 if (args->rename) {
940 * In a separate transaction, set the incomplete flag on the
941 * "old" attr and clear the incomplete flag on the "new" attr.
943 error = xfs_attr_leaf_flipflags(args);
944 if (error)
945 return(error);
948 * Dismantle the "old" attribute/value pair by removing
949 * a "remote" value (if it exists).
951 args->index = args->index2;
952 args->blkno = args->blkno2;
953 args->rmtblkno = args->rmtblkno2;
954 args->rmtblkcnt = args->rmtblkcnt2;
955 if (args->rmtblkno) {
956 error = xfs_attr_rmtval_remove(args);
957 if (error)
958 return(error);
962 * Read in the block containing the "old" attr, then
963 * remove the "old" attr from that block (neat, huh!)
965 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
966 &bp, XFS_ATTR_FORK);
967 if (error)
968 return(error);
969 ASSERT(bp != NULL);
970 (void)xfs_attr_leaf_remove(bp, args);
973 * If the result is small enough, shrink it all into the inode.
975 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
976 XFS_BMAP_INIT(args->flist, args->firstblock);
977 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
978 /* bp is gone due to xfs_da_shrink_inode */
979 if (!error) {
980 error = xfs_bmap_finish(&args->trans,
981 args->flist,
982 *args->firstblock,
983 &committed);
985 if (error) {
986 ASSERT(committed);
987 args->trans = NULL;
988 xfs_bmap_cancel(args->flist);
989 return(error);
993 * bmap_finish() may have committed the last trans
994 * and started a new one. We need the inode to be
995 * in all transactions.
997 if (committed) {
998 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
999 xfs_trans_ihold(args->trans, dp);
1001 } else
1002 xfs_da_buf_done(bp);
1005 * Commit the remove and start the next trans in series.
1007 error = xfs_attr_rolltrans(&args->trans, dp);
1009 } else if (args->rmtblkno > 0) {
1011 * Added a "remote" value, just clear the incomplete flag.
1013 error = xfs_attr_leaf_clearflag(args);
1015 return(error);
1019 * Remove a name from the leaf attribute list structure
1021 * This leaf block cannot have a "remote" value, we only call this routine
1022 * if bmap_one_block() says there is only one block (ie: no remote blks).
1024 STATIC int
1025 xfs_attr_leaf_removename(xfs_da_args_t *args)
1027 xfs_inode_t *dp;
1028 xfs_dabuf_t *bp;
1029 int error, committed, forkoff;
1032 * Remove the attribute.
1034 dp = args->dp;
1035 args->blkno = 0;
1036 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1037 XFS_ATTR_FORK);
1038 if (error) {
1039 return(error);
1042 ASSERT(bp != NULL);
1043 error = xfs_attr_leaf_lookup_int(bp, args);
1044 if (error == ENOATTR) {
1045 xfs_da_brelse(args->trans, bp);
1046 return(error);
1049 (void)xfs_attr_leaf_remove(bp, args);
1052 * If the result is small enough, shrink it all into the inode.
1054 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1055 XFS_BMAP_INIT(args->flist, args->firstblock);
1056 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1057 /* bp is gone due to xfs_da_shrink_inode */
1058 if (!error) {
1059 error = xfs_bmap_finish(&args->trans, args->flist,
1060 *args->firstblock, &committed);
1062 if (error) {
1063 ASSERT(committed);
1064 args->trans = NULL;
1065 xfs_bmap_cancel(args->flist);
1066 return(error);
1070 * bmap_finish() may have committed the last trans and started
1071 * a new one. We need the inode to be in all transactions.
1073 if (committed) {
1074 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1075 xfs_trans_ihold(args->trans, dp);
1077 } else
1078 xfs_da_buf_done(bp);
1079 return(0);
1083 * Look up a name in a leaf attribute list structure.
1085 * This leaf block cannot have a "remote" value, we only call this routine
1086 * if bmap_one_block() says there is only one block (ie: no remote blks).
1088 STATIC int
1089 xfs_attr_leaf_get(xfs_da_args_t *args)
1091 xfs_dabuf_t *bp;
1092 int error;
1094 args->blkno = 0;
1095 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1096 XFS_ATTR_FORK);
1097 if (error)
1098 return(error);
1099 ASSERT(bp != NULL);
1101 error = xfs_attr_leaf_lookup_int(bp, args);
1102 if (error != EEXIST) {
1103 xfs_da_brelse(args->trans, bp);
1104 return(error);
1106 error = xfs_attr_leaf_getvalue(bp, args);
1107 xfs_da_brelse(args->trans, bp);
1108 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
1109 error = xfs_attr_rmtval_get(args);
1111 return(error);
1115 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1117 STATIC int
1118 xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1120 xfs_attr_leafblock_t *leaf;
1121 int error;
1122 xfs_dabuf_t *bp;
1124 context->cursor->blkno = 0;
1125 error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
1126 if (error)
1127 return(error);
1128 ASSERT(bp != NULL);
1129 leaf = bp->data;
1130 if (unlikely(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1131 != XFS_ATTR_LEAF_MAGIC)) {
1132 XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
1133 context->dp->i_mount, leaf);
1134 xfs_da_brelse(NULL, bp);
1135 return(XFS_ERROR(EFSCORRUPTED));
1138 (void)xfs_attr_leaf_list_int(bp, context);
1139 xfs_da_brelse(NULL, bp);
1140 return(0);
1144 /*========================================================================
1145 * External routines when attribute list size > XFS_LBSIZE(mp).
1146 *========================================================================*/
1149 * Add a name to a Btree-format attribute list.
1151 * This will involve walking down the Btree, and may involve splitting
1152 * leaf nodes and even splitting intermediate nodes up to and including
1153 * the root node (a special case of an intermediate node).
1155 * "Remote" attribute values confuse the issue and atomic rename operations
1156 * add a whole extra layer of confusion on top of that.
1158 STATIC int
1159 xfs_attr_node_addname(xfs_da_args_t *args)
1161 xfs_da_state_t *state;
1162 xfs_da_state_blk_t *blk;
1163 xfs_inode_t *dp;
1164 xfs_mount_t *mp;
1165 int committed, retval, error;
1168 * Fill in bucket of arguments/results/context to carry around.
1170 dp = args->dp;
1171 mp = dp->i_mount;
1172 restart:
1173 state = xfs_da_state_alloc();
1174 state->args = args;
1175 state->mp = mp;
1176 state->blocksize = state->mp->m_sb.sb_blocksize;
1177 state->node_ents = state->mp->m_attr_node_ents;
1180 * Search to see if name already exists, and get back a pointer
1181 * to where it should go.
1183 error = xfs_da_node_lookup_int(state, &retval);
1184 if (error)
1185 goto out;
1186 blk = &state->path.blk[ state->path.active-1 ];
1187 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1188 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
1189 goto out;
1190 } else if (retval == EEXIST) {
1191 if (args->flags & ATTR_CREATE)
1192 goto out;
1193 args->rename = 1; /* atomic rename op */
1194 args->blkno2 = args->blkno; /* set 2nd entry info*/
1195 args->index2 = args->index;
1196 args->rmtblkno2 = args->rmtblkno;
1197 args->rmtblkcnt2 = args->rmtblkcnt;
1198 args->rmtblkno = 0;
1199 args->rmtblkcnt = 0;
1202 retval = xfs_attr_leaf_add(blk->bp, state->args);
1203 if (retval == ENOSPC) {
1204 if (state->path.active == 1) {
1206 * Its really a single leaf node, but it had
1207 * out-of-line values so it looked like it *might*
1208 * have been a b-tree.
1210 xfs_da_state_free(state);
1211 XFS_BMAP_INIT(args->flist, args->firstblock);
1212 error = xfs_attr_leaf_to_node(args);
1213 if (!error) {
1214 error = xfs_bmap_finish(&args->trans,
1215 args->flist,
1216 *args->firstblock,
1217 &committed);
1219 if (error) {
1220 ASSERT(committed);
1221 args->trans = NULL;
1222 xfs_bmap_cancel(args->flist);
1223 goto out;
1227 * bmap_finish() may have committed the last trans
1228 * and started a new one. We need the inode to be
1229 * in all transactions.
1231 if (committed) {
1232 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1233 xfs_trans_ihold(args->trans, dp);
1237 * Commit the node conversion and start the next
1238 * trans in the chain.
1240 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1241 goto out;
1243 goto restart;
1247 * Split as many Btree elements as required.
1248 * This code tracks the new and old attr's location
1249 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1250 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1252 XFS_BMAP_INIT(args->flist, args->firstblock);
1253 error = xfs_da_split(state);
1254 if (!error) {
1255 error = xfs_bmap_finish(&args->trans, args->flist,
1256 *args->firstblock, &committed);
1258 if (error) {
1259 ASSERT(committed);
1260 args->trans = NULL;
1261 xfs_bmap_cancel(args->flist);
1262 goto out;
1266 * bmap_finish() may have committed the last trans and started
1267 * a new one. We need the inode to be in all transactions.
1269 if (committed) {
1270 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1271 xfs_trans_ihold(args->trans, dp);
1273 } else {
1275 * Addition succeeded, update Btree hashvals.
1277 xfs_da_fixhashpath(state, &state->path);
1281 * Kill the state structure, we're done with it and need to
1282 * allow the buffers to come back later.
1284 xfs_da_state_free(state);
1285 state = NULL;
1288 * Commit the leaf addition or btree split and start the next
1289 * trans in the chain.
1291 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1292 goto out;
1295 * If there was an out-of-line value, allocate the blocks we
1296 * identified for its storage and copy the value. This is done
1297 * after we create the attribute so that we don't overflow the
1298 * maximum size of a transaction and/or hit a deadlock.
1300 if (args->rmtblkno > 0) {
1301 error = xfs_attr_rmtval_set(args);
1302 if (error)
1303 return(error);
1307 * If this is an atomic rename operation, we must "flip" the
1308 * incomplete flags on the "new" and "old" attribute/value pairs
1309 * so that one disappears and one appears atomically. Then we
1310 * must remove the "old" attribute/value pair.
1312 if (args->rename) {
1314 * In a separate transaction, set the incomplete flag on the
1315 * "old" attr and clear the incomplete flag on the "new" attr.
1317 error = xfs_attr_leaf_flipflags(args);
1318 if (error)
1319 goto out;
1322 * Dismantle the "old" attribute/value pair by removing
1323 * a "remote" value (if it exists).
1325 args->index = args->index2;
1326 args->blkno = args->blkno2;
1327 args->rmtblkno = args->rmtblkno2;
1328 args->rmtblkcnt = args->rmtblkcnt2;
1329 if (args->rmtblkno) {
1330 error = xfs_attr_rmtval_remove(args);
1331 if (error)
1332 return(error);
1336 * Re-find the "old" attribute entry after any split ops.
1337 * The INCOMPLETE flag means that we will find the "old"
1338 * attr, not the "new" one.
1340 args->flags |= XFS_ATTR_INCOMPLETE;
1341 state = xfs_da_state_alloc();
1342 state->args = args;
1343 state->mp = mp;
1344 state->blocksize = state->mp->m_sb.sb_blocksize;
1345 state->node_ents = state->mp->m_attr_node_ents;
1346 state->inleaf = 0;
1347 error = xfs_da_node_lookup_int(state, &retval);
1348 if (error)
1349 goto out;
1352 * Remove the name and update the hashvals in the tree.
1354 blk = &state->path.blk[ state->path.active-1 ];
1355 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1356 error = xfs_attr_leaf_remove(blk->bp, args);
1357 xfs_da_fixhashpath(state, &state->path);
1360 * Check to see if the tree needs to be collapsed.
1362 if (retval && (state->path.active > 1)) {
1363 XFS_BMAP_INIT(args->flist, args->firstblock);
1364 error = xfs_da_join(state);
1365 if (!error) {
1366 error = xfs_bmap_finish(&args->trans,
1367 args->flist,
1368 *args->firstblock,
1369 &committed);
1371 if (error) {
1372 ASSERT(committed);
1373 args->trans = NULL;
1374 xfs_bmap_cancel(args->flist);
1375 goto out;
1379 * bmap_finish() may have committed the last trans
1380 * and started a new one. We need the inode to be
1381 * in all transactions.
1383 if (committed) {
1384 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1385 xfs_trans_ihold(args->trans, dp);
1390 * Commit and start the next trans in the chain.
1392 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1393 goto out;
1395 } else if (args->rmtblkno > 0) {
1397 * Added a "remote" value, just clear the incomplete flag.
1399 error = xfs_attr_leaf_clearflag(args);
1400 if (error)
1401 goto out;
1403 retval = error = 0;
1405 out:
1406 if (state)
1407 xfs_da_state_free(state);
1408 if (error)
1409 return(error);
1410 return(retval);
1414 * Remove a name from a B-tree attribute list.
1416 * This will involve walking down the Btree, and may involve joining
1417 * leaf nodes and even joining intermediate nodes up to and including
1418 * the root node (a special case of an intermediate node).
1420 STATIC int
1421 xfs_attr_node_removename(xfs_da_args_t *args)
1423 xfs_da_state_t *state;
1424 xfs_da_state_blk_t *blk;
1425 xfs_inode_t *dp;
1426 xfs_dabuf_t *bp;
1427 int retval, error, committed, forkoff;
1430 * Tie a string around our finger to remind us where we are.
1432 dp = args->dp;
1433 state = xfs_da_state_alloc();
1434 state->args = args;
1435 state->mp = dp->i_mount;
1436 state->blocksize = state->mp->m_sb.sb_blocksize;
1437 state->node_ents = state->mp->m_attr_node_ents;
1440 * Search to see if name exists, and get back a pointer to it.
1442 error = xfs_da_node_lookup_int(state, &retval);
1443 if (error || (retval != EEXIST)) {
1444 if (error == 0)
1445 error = retval;
1446 goto out;
1450 * If there is an out-of-line value, de-allocate the blocks.
1451 * This is done before we remove the attribute so that we don't
1452 * overflow the maximum size of a transaction and/or hit a deadlock.
1454 blk = &state->path.blk[ state->path.active-1 ];
1455 ASSERT(blk->bp != NULL);
1456 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1457 if (args->rmtblkno > 0) {
1459 * Fill in disk block numbers in the state structure
1460 * so that we can get the buffers back after we commit
1461 * several transactions in the following calls.
1463 error = xfs_attr_fillstate(state);
1464 if (error)
1465 goto out;
1468 * Mark the attribute as INCOMPLETE, then bunmapi() the
1469 * remote value.
1471 error = xfs_attr_leaf_setflag(args);
1472 if (error)
1473 goto out;
1474 error = xfs_attr_rmtval_remove(args);
1475 if (error)
1476 goto out;
1479 * Refill the state structure with buffers, the prior calls
1480 * released our buffers.
1482 error = xfs_attr_refillstate(state);
1483 if (error)
1484 goto out;
1488 * Remove the name and update the hashvals in the tree.
1490 blk = &state->path.blk[ state->path.active-1 ];
1491 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1492 retval = xfs_attr_leaf_remove(blk->bp, args);
1493 xfs_da_fixhashpath(state, &state->path);
1496 * Check to see if the tree needs to be collapsed.
1498 if (retval && (state->path.active > 1)) {
1499 XFS_BMAP_INIT(args->flist, args->firstblock);
1500 error = xfs_da_join(state);
1501 if (!error) {
1502 error = xfs_bmap_finish(&args->trans, args->flist,
1503 *args->firstblock, &committed);
1505 if (error) {
1506 ASSERT(committed);
1507 args->trans = NULL;
1508 xfs_bmap_cancel(args->flist);
1509 goto out;
1513 * bmap_finish() may have committed the last trans and started
1514 * a new one. We need the inode to be in all transactions.
1516 if (committed) {
1517 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1518 xfs_trans_ihold(args->trans, dp);
1522 * Commit the Btree join operation and start a new trans.
1524 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1525 goto out;
1529 * If the result is small enough, push it all into the inode.
1531 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1533 * Have to get rid of the copy of this dabuf in the state.
1535 ASSERT(state->path.active == 1);
1536 ASSERT(state->path.blk[0].bp);
1537 xfs_da_buf_done(state->path.blk[0].bp);
1538 state->path.blk[0].bp = NULL;
1540 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
1541 XFS_ATTR_FORK);
1542 if (error)
1543 goto out;
1544 ASSERT(INT_GET(((xfs_attr_leafblock_t *)
1545 bp->data)->hdr.info.magic, ARCH_CONVERT)
1546 == XFS_ATTR_LEAF_MAGIC);
1548 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1549 XFS_BMAP_INIT(args->flist, args->firstblock);
1550 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1551 /* bp is gone due to xfs_da_shrink_inode */
1552 if (!error) {
1553 error = xfs_bmap_finish(&args->trans,
1554 args->flist,
1555 *args->firstblock,
1556 &committed);
1558 if (error) {
1559 ASSERT(committed);
1560 args->trans = NULL;
1561 xfs_bmap_cancel(args->flist);
1562 goto out;
1566 * bmap_finish() may have committed the last trans
1567 * and started a new one. We need the inode to be
1568 * in all transactions.
1570 if (committed) {
1571 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1572 xfs_trans_ihold(args->trans, dp);
1574 } else
1575 xfs_da_brelse(args->trans, bp);
1577 error = 0;
1579 out:
1580 xfs_da_state_free(state);
1581 return(error);
1585 * Fill in the disk block numbers in the state structure for the buffers
1586 * that are attached to the state structure.
1587 * This is done so that we can quickly reattach ourselves to those buffers
1588 * after some set of transaction commit's has released these buffers.
1590 STATIC int
1591 xfs_attr_fillstate(xfs_da_state_t *state)
1593 xfs_da_state_path_t *path;
1594 xfs_da_state_blk_t *blk;
1595 int level;
1598 * Roll down the "path" in the state structure, storing the on-disk
1599 * block number for those buffers in the "path".
1601 path = &state->path;
1602 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1603 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1604 if (blk->bp) {
1605 blk->disk_blkno = xfs_da_blkno(blk->bp);
1606 xfs_da_buf_done(blk->bp);
1607 blk->bp = NULL;
1608 } else {
1609 blk->disk_blkno = 0;
1614 * Roll down the "altpath" in the state structure, storing the on-disk
1615 * block number for those buffers in the "altpath".
1617 path = &state->altpath;
1618 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1619 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1620 if (blk->bp) {
1621 blk->disk_blkno = xfs_da_blkno(blk->bp);
1622 xfs_da_buf_done(blk->bp);
1623 blk->bp = NULL;
1624 } else {
1625 blk->disk_blkno = 0;
1629 return(0);
1633 * Reattach the buffers to the state structure based on the disk block
1634 * numbers stored in the state structure.
1635 * This is done after some set of transaction commit's has released those
1636 * buffers from our grip.
1638 STATIC int
1639 xfs_attr_refillstate(xfs_da_state_t *state)
1641 xfs_da_state_path_t *path;
1642 xfs_da_state_blk_t *blk;
1643 int level, error;
1646 * Roll down the "path" in the state structure, storing the on-disk
1647 * block number for those buffers in the "path".
1649 path = &state->path;
1650 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1651 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1652 if (blk->disk_blkno) {
1653 error = xfs_da_read_buf(state->args->trans,
1654 state->args->dp,
1655 blk->blkno, blk->disk_blkno,
1656 &blk->bp, XFS_ATTR_FORK);
1657 if (error)
1658 return(error);
1659 } else {
1660 blk->bp = NULL;
1665 * Roll down the "altpath" in the state structure, storing the on-disk
1666 * block number for those buffers in the "altpath".
1668 path = &state->altpath;
1669 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1670 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1671 if (blk->disk_blkno) {
1672 error = xfs_da_read_buf(state->args->trans,
1673 state->args->dp,
1674 blk->blkno, blk->disk_blkno,
1675 &blk->bp, XFS_ATTR_FORK);
1676 if (error)
1677 return(error);
1678 } else {
1679 blk->bp = NULL;
1683 return(0);
1687 * Look up a filename in a node attribute list.
1689 * This routine gets called for any attribute fork that has more than one
1690 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1691 * "remote" values taking up more blocks.
1693 STATIC int
1694 xfs_attr_node_get(xfs_da_args_t *args)
1696 xfs_da_state_t *state;
1697 xfs_da_state_blk_t *blk;
1698 int error, retval;
1699 int i;
1701 state = xfs_da_state_alloc();
1702 state->args = args;
1703 state->mp = args->dp->i_mount;
1704 state->blocksize = state->mp->m_sb.sb_blocksize;
1705 state->node_ents = state->mp->m_attr_node_ents;
1708 * Search to see if name exists, and get back a pointer to it.
1710 error = xfs_da_node_lookup_int(state, &retval);
1711 if (error) {
1712 retval = error;
1713 } else if (retval == EEXIST) {
1714 blk = &state->path.blk[ state->path.active-1 ];
1715 ASSERT(blk->bp != NULL);
1716 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1719 * Get the value, local or "remote"
1721 retval = xfs_attr_leaf_getvalue(blk->bp, args);
1722 if (!retval && (args->rmtblkno > 0)
1723 && !(args->flags & ATTR_KERNOVAL)) {
1724 retval = xfs_attr_rmtval_get(args);
1729 * If not in a transaction, we have to release all the buffers.
1731 for (i = 0; i < state->path.active; i++) {
1732 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1733 state->path.blk[i].bp = NULL;
1736 xfs_da_state_free(state);
1737 return(retval);
1740 STATIC int /* error */
1741 xfs_attr_node_list(xfs_attr_list_context_t *context)
1743 attrlist_cursor_kern_t *cursor;
1744 xfs_attr_leafblock_t *leaf;
1745 xfs_da_intnode_t *node;
1746 xfs_da_node_entry_t *btree;
1747 int error, i;
1748 xfs_dabuf_t *bp;
1750 cursor = context->cursor;
1751 cursor->initted = 1;
1754 * Do all sorts of validation on the passed-in cursor structure.
1755 * If anything is amiss, ignore the cursor and look up the hashval
1756 * starting from the btree root.
1758 bp = NULL;
1759 if (cursor->blkno > 0) {
1760 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1761 &bp, XFS_ATTR_FORK);
1762 if ((error != 0) && (error != EFSCORRUPTED))
1763 return(error);
1764 if (bp) {
1765 node = bp->data;
1766 switch (INT_GET(node->hdr.info.magic, ARCH_CONVERT)) {
1767 case XFS_DA_NODE_MAGIC:
1768 xfs_attr_trace_l_cn("wrong blk", context, node);
1769 xfs_da_brelse(NULL, bp);
1770 bp = NULL;
1771 break;
1772 case XFS_ATTR_LEAF_MAGIC:
1773 leaf = bp->data;
1774 if (cursor->hashval >
1775 INT_GET(leaf->entries[
1776 INT_GET(leaf->hdr.count,
1777 ARCH_CONVERT)-1].hashval,
1778 ARCH_CONVERT)) {
1779 xfs_attr_trace_l_cl("wrong blk",
1780 context, leaf);
1781 xfs_da_brelse(NULL, bp);
1782 bp = NULL;
1783 } else if (cursor->hashval <=
1784 INT_GET(leaf->entries[0].hashval,
1785 ARCH_CONVERT)) {
1786 xfs_attr_trace_l_cl("maybe wrong blk",
1787 context, leaf);
1788 xfs_da_brelse(NULL, bp);
1789 bp = NULL;
1791 break;
1792 default:
1793 xfs_attr_trace_l_c("wrong blk - ??", context);
1794 xfs_da_brelse(NULL, bp);
1795 bp = NULL;
1801 * We did not find what we expected given the cursor's contents,
1802 * so we start from the top and work down based on the hash value.
1803 * Note that start of node block is same as start of leaf block.
1805 if (bp == NULL) {
1806 cursor->blkno = 0;
1807 for (;;) {
1808 error = xfs_da_read_buf(NULL, context->dp,
1809 cursor->blkno, -1, &bp,
1810 XFS_ATTR_FORK);
1811 if (error)
1812 return(error);
1813 if (unlikely(bp == NULL)) {
1814 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1815 XFS_ERRLEVEL_LOW,
1816 context->dp->i_mount);
1817 return(XFS_ERROR(EFSCORRUPTED));
1819 node = bp->data;
1820 if (INT_GET(node->hdr.info.magic, ARCH_CONVERT)
1821 == XFS_ATTR_LEAF_MAGIC)
1822 break;
1823 if (unlikely(INT_GET(node->hdr.info.magic, ARCH_CONVERT)
1824 != XFS_DA_NODE_MAGIC)) {
1825 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1826 XFS_ERRLEVEL_LOW,
1827 context->dp->i_mount,
1828 node);
1829 xfs_da_brelse(NULL, bp);
1830 return(XFS_ERROR(EFSCORRUPTED));
1832 btree = node->btree;
1833 for (i = 0;
1834 i < INT_GET(node->hdr.count, ARCH_CONVERT);
1835 btree++, i++) {
1836 if (cursor->hashval
1837 <= INT_GET(btree->hashval,
1838 ARCH_CONVERT)) {
1839 cursor->blkno = INT_GET(btree->before, ARCH_CONVERT);
1840 xfs_attr_trace_l_cb("descending",
1841 context, btree);
1842 break;
1845 if (i == INT_GET(node->hdr.count, ARCH_CONVERT)) {
1846 xfs_da_brelse(NULL, bp);
1847 return(0);
1849 xfs_da_brelse(NULL, bp);
1852 ASSERT(bp != NULL);
1855 * Roll upward through the blocks, processing each leaf block in
1856 * order. As long as there is space in the result buffer, keep
1857 * adding the information.
1859 for (;;) {
1860 leaf = bp->data;
1861 if (unlikely(INT_GET(leaf->hdr.info.magic, ARCH_CONVERT)
1862 != XFS_ATTR_LEAF_MAGIC)) {
1863 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1864 XFS_ERRLEVEL_LOW,
1865 context->dp->i_mount, leaf);
1866 xfs_da_brelse(NULL, bp);
1867 return(XFS_ERROR(EFSCORRUPTED));
1869 error = xfs_attr_leaf_list_int(bp, context);
1870 if (error || !leaf->hdr.info.forw)
1871 break; /* not really an error, buffer full or EOF */
1872 cursor->blkno = INT_GET(leaf->hdr.info.forw, ARCH_CONVERT);
1873 xfs_da_brelse(NULL, bp);
1874 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1875 &bp, XFS_ATTR_FORK);
1876 if (error)
1877 return(error);
1878 if (unlikely((bp == NULL))) {
1879 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1880 XFS_ERRLEVEL_LOW,
1881 context->dp->i_mount);
1882 return(XFS_ERROR(EFSCORRUPTED));
1885 xfs_da_brelse(NULL, bp);
1886 return(0);
1890 /*========================================================================
1891 * External routines for manipulating out-of-line attribute values.
1892 *========================================================================*/
1895 * Read the value associated with an attribute from the out-of-line buffer
1896 * that we stored it in.
1898 STATIC int
1899 xfs_attr_rmtval_get(xfs_da_args_t *args)
1901 xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
1902 xfs_mount_t *mp;
1903 xfs_daddr_t dblkno;
1904 xfs_caddr_t dst;
1905 xfs_buf_t *bp;
1906 int nmap, error, tmp, valuelen, blkcnt, i;
1907 xfs_dablk_t lblkno;
1909 ASSERT(!(args->flags & ATTR_KERNOVAL));
1911 mp = args->dp->i_mount;
1912 dst = args->value;
1913 valuelen = args->valuelen;
1914 lblkno = args->rmtblkno;
1915 while (valuelen > 0) {
1916 nmap = ATTR_RMTVALUE_MAPSIZE;
1917 error = xfs_bmapi(args->trans, args->dp, (xfs_fileoff_t)lblkno,
1918 args->rmtblkcnt,
1919 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
1920 NULL, 0, map, &nmap, NULL);
1921 if (error)
1922 return(error);
1923 ASSERT(nmap >= 1);
1925 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
1926 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
1927 (map[i].br_startblock != HOLESTARTBLOCK));
1928 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
1929 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
1930 error = xfs_read_buf(mp, mp->m_ddev_targp, dblkno,
1931 blkcnt, XFS_BUF_LOCK, &bp);
1932 if (error)
1933 return(error);
1935 tmp = (valuelen < XFS_BUF_SIZE(bp))
1936 ? valuelen : XFS_BUF_SIZE(bp);
1937 xfs_biomove(bp, 0, tmp, dst, XFS_B_READ);
1938 xfs_buf_relse(bp);
1939 dst += tmp;
1940 valuelen -= tmp;
1942 lblkno += map[i].br_blockcount;
1945 ASSERT(valuelen == 0);
1946 return(0);
1950 * Write the value associated with an attribute into the out-of-line buffer
1951 * that we have defined for it.
1953 STATIC int
1954 xfs_attr_rmtval_set(xfs_da_args_t *args)
1956 xfs_mount_t *mp;
1957 xfs_fileoff_t lfileoff;
1958 xfs_inode_t *dp;
1959 xfs_bmbt_irec_t map;
1960 xfs_daddr_t dblkno;
1961 xfs_caddr_t src;
1962 xfs_buf_t *bp;
1963 xfs_dablk_t lblkno;
1964 int blkcnt, valuelen, nmap, error, tmp, committed;
1966 dp = args->dp;
1967 mp = dp->i_mount;
1968 src = args->value;
1971 * Find a "hole" in the attribute address space large enough for
1972 * us to drop the new attribute's value into.
1974 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
1975 lfileoff = 0;
1976 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
1977 XFS_ATTR_FORK);
1978 if (error) {
1979 return(error);
1981 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
1982 args->rmtblkcnt = blkcnt;
1985 * Roll through the "value", allocating blocks on disk as required.
1987 while (blkcnt > 0) {
1989 * Allocate a single extent, up to the size of the value.
1991 XFS_BMAP_INIT(args->flist, args->firstblock);
1992 nmap = 1;
1993 error = xfs_bmapi(args->trans, dp, (xfs_fileoff_t)lblkno,
1994 blkcnt,
1995 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA |
1996 XFS_BMAPI_WRITE,
1997 args->firstblock, args->total, &map, &nmap,
1998 args->flist);
1999 if (!error) {
2000 error = xfs_bmap_finish(&args->trans, args->flist,
2001 *args->firstblock, &committed);
2003 if (error) {
2004 ASSERT(committed);
2005 args->trans = NULL;
2006 xfs_bmap_cancel(args->flist);
2007 return(error);
2011 * bmap_finish() may have committed the last trans and started
2012 * a new one. We need the inode to be in all transactions.
2014 if (committed) {
2015 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
2016 xfs_trans_ihold(args->trans, dp);
2019 ASSERT(nmap == 1);
2020 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2021 (map.br_startblock != HOLESTARTBLOCK));
2022 lblkno += map.br_blockcount;
2023 blkcnt -= map.br_blockcount;
2026 * Start the next trans in the chain.
2028 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
2029 return (error);
2033 * Roll through the "value", copying the attribute value to the
2034 * already-allocated blocks. Blocks are written synchronously
2035 * so that we can know they are all on disk before we turn off
2036 * the INCOMPLETE flag.
2038 lblkno = args->rmtblkno;
2039 valuelen = args->valuelen;
2040 while (valuelen > 0) {
2042 * Try to remember where we decided to put the value.
2044 XFS_BMAP_INIT(args->flist, args->firstblock);
2045 nmap = 1;
2046 error = xfs_bmapi(NULL, dp, (xfs_fileoff_t)lblkno,
2047 args->rmtblkcnt,
2048 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2049 args->firstblock, 0, &map, &nmap, NULL);
2050 if (error) {
2051 return(error);
2053 ASSERT(nmap == 1);
2054 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2055 (map.br_startblock != HOLESTARTBLOCK));
2057 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2058 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2060 bp = xfs_buf_get_flags(mp->m_ddev_targp, dblkno,
2061 blkcnt, XFS_BUF_LOCK);
2062 ASSERT(bp);
2063 ASSERT(!XFS_BUF_GETERROR(bp));
2065 tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
2066 XFS_BUF_SIZE(bp);
2067 xfs_biomove(bp, 0, tmp, src, XFS_B_WRITE);
2068 if (tmp < XFS_BUF_SIZE(bp))
2069 xfs_biozero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
2070 if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
2071 return (error);
2073 src += tmp;
2074 valuelen -= tmp;
2076 lblkno += map.br_blockcount;
2078 ASSERT(valuelen == 0);
2079 return(0);
2083 * Remove the value associated with an attribute by deleting the
2084 * out-of-line buffer that it is stored on.
2086 STATIC int
2087 xfs_attr_rmtval_remove(xfs_da_args_t *args)
2089 xfs_mount_t *mp;
2090 xfs_bmbt_irec_t map;
2091 xfs_buf_t *bp;
2092 xfs_daddr_t dblkno;
2093 xfs_dablk_t lblkno;
2094 int valuelen, blkcnt, nmap, error, done, committed;
2096 mp = args->dp->i_mount;
2099 * Roll through the "value", invalidating the attribute value's
2100 * blocks.
2102 lblkno = args->rmtblkno;
2103 valuelen = args->rmtblkcnt;
2104 while (valuelen > 0) {
2106 * Try to remember where we decided to put the value.
2108 XFS_BMAP_INIT(args->flist, args->firstblock);
2109 nmap = 1;
2110 error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
2111 args->rmtblkcnt,
2112 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2113 args->firstblock, 0, &map, &nmap,
2114 args->flist);
2115 if (error) {
2116 return(error);
2118 ASSERT(nmap == 1);
2119 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2120 (map.br_startblock != HOLESTARTBLOCK));
2122 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2123 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2126 * If the "remote" value is in the cache, remove it.
2128 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt,
2129 XFS_INCORE_TRYLOCK);
2130 if (bp) {
2131 XFS_BUF_STALE(bp);
2132 XFS_BUF_UNDELAYWRITE(bp);
2133 xfs_buf_relse(bp);
2134 bp = NULL;
2137 valuelen -= map.br_blockcount;
2139 lblkno += map.br_blockcount;
2143 * Keep de-allocating extents until the remote-value region is gone.
2145 lblkno = args->rmtblkno;
2146 blkcnt = args->rmtblkcnt;
2147 done = 0;
2148 while (!done) {
2149 XFS_BMAP_INIT(args->flist, args->firstblock);
2150 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
2151 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2152 1, args->firstblock, args->flist, &done);
2153 if (!error) {
2154 error = xfs_bmap_finish(&args->trans, args->flist,
2155 *args->firstblock, &committed);
2157 if (error) {
2158 ASSERT(committed);
2159 args->trans = NULL;
2160 xfs_bmap_cancel(args->flist);
2161 return(error);
2165 * bmap_finish() may have committed the last trans and started
2166 * a new one. We need the inode to be in all transactions.
2168 if (committed) {
2169 xfs_trans_ijoin(args->trans, args->dp, XFS_ILOCK_EXCL);
2170 xfs_trans_ihold(args->trans, args->dp);
2174 * Close out trans and start the next one in the chain.
2176 if ((error = xfs_attr_rolltrans(&args->trans, args->dp)))
2177 return (error);
2179 return(0);
2182 #if defined(XFS_ATTR_TRACE)
2184 * Add a trace buffer entry for an attr_list context structure.
2186 void
2187 xfs_attr_trace_l_c(char *where, struct xfs_attr_list_context *context)
2189 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_C, where,
2190 (__psunsigned_t)context->dp,
2191 (__psunsigned_t)context->cursor->hashval,
2192 (__psunsigned_t)context->cursor->blkno,
2193 (__psunsigned_t)context->cursor->offset,
2194 (__psunsigned_t)context->alist,
2195 (__psunsigned_t)context->bufsize,
2196 (__psunsigned_t)context->count,
2197 (__psunsigned_t)context->firstu,
2198 (__psunsigned_t)
2199 ((context->count > 0) &&
2200 !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
2201 ? (ATTR_ENTRY(context->alist,
2202 context->count-1)->a_valuelen)
2203 : 0,
2204 (__psunsigned_t)context->dupcnt,
2205 (__psunsigned_t)context->flags,
2206 (__psunsigned_t)NULL,
2207 (__psunsigned_t)NULL,
2208 (__psunsigned_t)NULL);
2212 * Add a trace buffer entry for a context structure and a Btree node.
2214 void
2215 xfs_attr_trace_l_cn(char *where, struct xfs_attr_list_context *context,
2216 struct xfs_da_intnode *node)
2218 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CN, where,
2219 (__psunsigned_t)context->dp,
2220 (__psunsigned_t)context->cursor->hashval,
2221 (__psunsigned_t)context->cursor->blkno,
2222 (__psunsigned_t)context->cursor->offset,
2223 (__psunsigned_t)context->alist,
2224 (__psunsigned_t)context->bufsize,
2225 (__psunsigned_t)context->count,
2226 (__psunsigned_t)context->firstu,
2227 (__psunsigned_t)
2228 ((context->count > 0) &&
2229 !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
2230 ? (ATTR_ENTRY(context->alist,
2231 context->count-1)->a_valuelen)
2232 : 0,
2233 (__psunsigned_t)context->dupcnt,
2234 (__psunsigned_t)context->flags,
2235 (__psunsigned_t)INT_GET(node->hdr.count, ARCH_CONVERT),
2236 (__psunsigned_t)INT_GET(node->btree[0].hashval, ARCH_CONVERT),
2237 (__psunsigned_t)INT_GET(node->btree[INT_GET(node->hdr.count, ARCH_CONVERT)-1].hashval, ARCH_CONVERT));
2241 * Add a trace buffer entry for a context structure and a Btree element.
2243 void
2244 xfs_attr_trace_l_cb(char *where, struct xfs_attr_list_context *context,
2245 struct xfs_da_node_entry *btree)
2247 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CB, where,
2248 (__psunsigned_t)context->dp,
2249 (__psunsigned_t)context->cursor->hashval,
2250 (__psunsigned_t)context->cursor->blkno,
2251 (__psunsigned_t)context->cursor->offset,
2252 (__psunsigned_t)context->alist,
2253 (__psunsigned_t)context->bufsize,
2254 (__psunsigned_t)context->count,
2255 (__psunsigned_t)context->firstu,
2256 (__psunsigned_t)
2257 ((context->count > 0) &&
2258 !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
2259 ? (ATTR_ENTRY(context->alist,
2260 context->count-1)->a_valuelen)
2261 : 0,
2262 (__psunsigned_t)context->dupcnt,
2263 (__psunsigned_t)context->flags,
2264 (__psunsigned_t)INT_GET(btree->hashval, ARCH_CONVERT),
2265 (__psunsigned_t)INT_GET(btree->before, ARCH_CONVERT),
2266 (__psunsigned_t)NULL);
2270 * Add a trace buffer entry for a context structure and a leaf block.
2272 void
2273 xfs_attr_trace_l_cl(char *where, struct xfs_attr_list_context *context,
2274 struct xfs_attr_leafblock *leaf)
2276 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CL, where,
2277 (__psunsigned_t)context->dp,
2278 (__psunsigned_t)context->cursor->hashval,
2279 (__psunsigned_t)context->cursor->blkno,
2280 (__psunsigned_t)context->cursor->offset,
2281 (__psunsigned_t)context->alist,
2282 (__psunsigned_t)context->bufsize,
2283 (__psunsigned_t)context->count,
2284 (__psunsigned_t)context->firstu,
2285 (__psunsigned_t)
2286 ((context->count > 0) &&
2287 !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
2288 ? (ATTR_ENTRY(context->alist,
2289 context->count-1)->a_valuelen)
2290 : 0,
2291 (__psunsigned_t)context->dupcnt,
2292 (__psunsigned_t)context->flags,
2293 (__psunsigned_t)INT_GET(leaf->hdr.count, ARCH_CONVERT),
2294 (__psunsigned_t)INT_GET(leaf->entries[0].hashval, ARCH_CONVERT),
2295 (__psunsigned_t)INT_GET(leaf->entries[INT_GET(leaf->hdr.count, ARCH_CONVERT)-1].hashval, ARCH_CONVERT));
2299 * Add a trace buffer entry for the arguments given to the routine,
2300 * generic form.
2302 void
2303 xfs_attr_trace_enter(int type, char *where,
2304 __psunsigned_t a2, __psunsigned_t a3,
2305 __psunsigned_t a4, __psunsigned_t a5,
2306 __psunsigned_t a6, __psunsigned_t a7,
2307 __psunsigned_t a8, __psunsigned_t a9,
2308 __psunsigned_t a10, __psunsigned_t a11,
2309 __psunsigned_t a12, __psunsigned_t a13,
2310 __psunsigned_t a14, __psunsigned_t a15)
2312 ASSERT(xfs_attr_trace_buf);
2313 ktrace_enter(xfs_attr_trace_buf, (void *)((__psunsigned_t)type),
2314 (void *)where,
2315 (void *)a2, (void *)a3, (void *)a4,
2316 (void *)a5, (void *)a6, (void *)a7,
2317 (void *)a8, (void *)a9, (void *)a10,
2318 (void *)a11, (void *)a12, (void *)a13,
2319 (void *)a14, (void *)a15);
2321 #endif /* XFS_ATTR_TRACE */
2324 /*========================================================================
2325 * System (pseudo) namespace attribute interface routines.
2326 *========================================================================*/
2328 STATIC int
2329 posix_acl_access_set(
2330 vnode_t *vp, char *name, void *data, size_t size, int xflags)
2332 return xfs_acl_vset(vp, data, size, _ACL_TYPE_ACCESS);
2335 STATIC int
2336 posix_acl_access_remove(
2337 struct vnode *vp, char *name, int xflags)
2339 return xfs_acl_vremove(vp, _ACL_TYPE_ACCESS);
2342 STATIC int
2343 posix_acl_access_get(
2344 vnode_t *vp, char *name, void *data, size_t size, int xflags)
2346 return xfs_acl_vget(vp, data, size, _ACL_TYPE_ACCESS);
2349 STATIC int
2350 posix_acl_access_exists(
2351 vnode_t *vp)
2353 return xfs_acl_vhasacl_access(vp);
2356 STATIC int
2357 posix_acl_default_set(
2358 vnode_t *vp, char *name, void *data, size_t size, int xflags)
2360 return xfs_acl_vset(vp, data, size, _ACL_TYPE_DEFAULT);
2363 STATIC int
2364 posix_acl_default_get(
2365 vnode_t *vp, char *name, void *data, size_t size, int xflags)
2367 return xfs_acl_vget(vp, data, size, _ACL_TYPE_DEFAULT);
2370 STATIC int
2371 posix_acl_default_remove(
2372 struct vnode *vp, char *name, int xflags)
2374 return xfs_acl_vremove(vp, _ACL_TYPE_DEFAULT);
2377 STATIC int
2378 posix_acl_default_exists(
2379 vnode_t *vp)
2381 return xfs_acl_vhasacl_default(vp);
2384 STATIC struct attrnames posix_acl_access = {
2385 .attr_name = "posix_acl_access",
2386 .attr_namelen = sizeof("posix_acl_access") - 1,
2387 .attr_get = posix_acl_access_get,
2388 .attr_set = posix_acl_access_set,
2389 .attr_remove = posix_acl_access_remove,
2390 .attr_exists = posix_acl_access_exists,
2393 STATIC struct attrnames posix_acl_default = {
2394 .attr_name = "posix_acl_default",
2395 .attr_namelen = sizeof("posix_acl_default") - 1,
2396 .attr_get = posix_acl_default_get,
2397 .attr_set = posix_acl_default_set,
2398 .attr_remove = posix_acl_default_remove,
2399 .attr_exists = posix_acl_default_exists,
2402 STATIC struct attrnames *attr_system_names[] =
2403 { &posix_acl_access, &posix_acl_default };
2406 /*========================================================================
2407 * Namespace-prefix-style attribute name interface routines.
2408 *========================================================================*/
2410 STATIC int
2411 attr_generic_set(
2412 struct vnode *vp, char *name, void *data, size_t size, int xflags)
2414 int error;
2416 VOP_ATTR_SET(vp, name, data, size, xflags, NULL, error);
2417 return -error;
2420 STATIC int
2421 attr_generic_get(
2422 struct vnode *vp, char *name, void *data, size_t size, int xflags)
2424 int error, asize = size;
2426 VOP_ATTR_GET(vp, name, data, &asize, xflags, NULL, error);
2427 if (!error)
2428 return asize;
2429 return -error;
2432 STATIC int
2433 attr_generic_remove(
2434 struct vnode *vp, char *name, int xflags)
2436 int error;
2438 VOP_ATTR_REMOVE(vp, name, xflags, NULL, error);
2439 return -error;
2442 STATIC int
2443 attr_generic_listadd(
2444 attrnames_t *prefix,
2445 attrnames_t *namesp,
2446 void *data,
2447 size_t size,
2448 ssize_t *result)
2450 char *p = data + *result;
2452 *result += prefix->attr_namelen;
2453 *result += namesp->attr_namelen + 1;
2454 if (!size)
2455 return 0;
2456 if (*result > size)
2457 return -ERANGE;
2458 strcpy(p, prefix->attr_name);
2459 p += prefix->attr_namelen;
2460 strcpy(p, namesp->attr_name);
2461 p += namesp->attr_namelen + 1;
2462 return 0;
2465 STATIC int
2466 attr_system_list(
2467 struct vnode *vp,
2468 void *data,
2469 size_t size,
2470 ssize_t *result)
2472 attrnames_t *namesp;
2473 int i, error = 0;
2475 for (i = 0; i < ATTR_SYSCOUNT; i++) {
2476 namesp = attr_system_names[i];
2477 if (!namesp->attr_exists || !namesp->attr_exists(vp))
2478 continue;
2479 error = attr_generic_listadd(&attr_system, namesp,
2480 data, size, result);
2481 if (error)
2482 break;
2484 return error;
2488 attr_generic_list(
2489 struct vnode *vp, void *data, size_t size, int xflags, ssize_t *result)
2491 attrlist_cursor_kern_t cursor = { 0 };
2492 int error;
2494 VOP_ATTR_LIST(vp, data, size, xflags, &cursor, NULL, error);
2495 if (error > 0)
2496 return -error;
2497 *result = -error;
2498 return attr_system_list(vp, data, size, result);
2501 attrnames_t *
2502 attr_lookup_namespace(
2503 char *name,
2504 struct attrnames **names,
2505 int nnames)
2507 int i;
2509 for (i = 0; i < nnames; i++)
2510 if (!strncmp(name, names[i]->attr_name, names[i]->attr_namelen))
2511 return names[i];
2512 return NULL;
2516 * Some checks to prevent people abusing EAs to get over quota:
2517 * - Don't allow modifying user EAs on devices/symlinks;
2518 * - Don't allow modifying user EAs if sticky bit set;
2520 STATIC int
2521 attr_user_capable(
2522 struct vnode *vp,
2523 cred_t *cred)
2525 struct inode *inode = LINVFS_GET_IP(vp);
2527 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
2528 return -EPERM;
2529 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode) &&
2530 !capable(CAP_SYS_ADMIN))
2531 return -EPERM;
2532 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
2533 (current_fsuid(cred) != inode->i_uid) && !capable(CAP_FOWNER))
2534 return -EPERM;
2535 return 0;
2538 STATIC int
2539 attr_trusted_capable(
2540 struct vnode *vp,
2541 cred_t *cred)
2543 struct inode *inode = LINVFS_GET_IP(vp);
2545 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
2546 return -EPERM;
2547 if (!capable(CAP_SYS_ADMIN))
2548 return -EPERM;
2549 return 0;
2552 STATIC int
2553 attr_secure_capable(
2554 struct vnode *vp,
2555 cred_t *cred)
2557 return -ENOSECURITY;
2560 STATIC int
2561 attr_system_set(
2562 struct vnode *vp, char *name, void *data, size_t size, int xflags)
2564 attrnames_t *namesp;
2565 int error;
2567 if (xflags & ATTR_CREATE)
2568 return -EINVAL;
2570 namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
2571 if (!namesp)
2572 return -EOPNOTSUPP;
2573 error = namesp->attr_set(vp, name, data, size, xflags);
2574 if (!error)
2575 error = vn_revalidate(vp);
2576 return error;
2579 STATIC int
2580 attr_system_get(
2581 struct vnode *vp, char *name, void *data, size_t size, int xflags)
2583 attrnames_t *namesp;
2585 namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
2586 if (!namesp)
2587 return -EOPNOTSUPP;
2588 return namesp->attr_get(vp, name, data, size, xflags);
2591 STATIC int
2592 attr_system_remove(
2593 struct vnode *vp, char *name, int xflags)
2595 attrnames_t *namesp;
2597 namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
2598 if (!namesp)
2599 return -EOPNOTSUPP;
2600 return namesp->attr_remove(vp, name, xflags);
2603 struct attrnames attr_system = {
2604 .attr_name = "system.",
2605 .attr_namelen = sizeof("system.") - 1,
2606 .attr_flag = ATTR_SYSTEM,
2607 .attr_get = attr_system_get,
2608 .attr_set = attr_system_set,
2609 .attr_remove = attr_system_remove,
2610 .attr_capable = (attrcapable_t)fs_noerr,
2613 struct attrnames attr_trusted = {
2614 .attr_name = "trusted.",
2615 .attr_namelen = sizeof("trusted.") - 1,
2616 .attr_flag = ATTR_ROOT,
2617 .attr_get = attr_generic_get,
2618 .attr_set = attr_generic_set,
2619 .attr_remove = attr_generic_remove,
2620 .attr_capable = attr_trusted_capable,
2623 struct attrnames attr_secure = {
2624 .attr_name = "security.",
2625 .attr_namelen = sizeof("security.") - 1,
2626 .attr_flag = ATTR_SECURE,
2627 .attr_get = attr_generic_get,
2628 .attr_set = attr_generic_set,
2629 .attr_remove = attr_generic_remove,
2630 .attr_capable = attr_secure_capable,
2633 struct attrnames attr_user = {
2634 .attr_name = "user.",
2635 .attr_namelen = sizeof("user.") - 1,
2636 .attr_get = attr_generic_get,
2637 .attr_set = attr_generic_set,
2638 .attr_remove = attr_generic_remove,
2639 .attr_capable = attr_user_capable,
2642 struct attrnames *attr_namespaces[] =
2643 { &attr_system, &attr_trusted, &attr_secure, &attr_user };