Spidernet add net_ratelimit to suppress long output
[linux-2.6/verdex.git] / fs / xfs / xfs_attr.c
blob9ada7bdbae5219a0803aac3b8c5f55a8d2f095c1
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_dir2.h"
31 #include "xfs_dmapi.h"
32 #include "xfs_mount.h"
33 #include "xfs_da_btree.h"
34 #include "xfs_bmap_btree.h"
35 #include "xfs_alloc_btree.h"
36 #include "xfs_ialloc_btree.h"
37 #include "xfs_dir2_sf.h"
38 #include "xfs_attr_sf.h"
39 #include "xfs_dinode.h"
40 #include "xfs_inode.h"
41 #include "xfs_alloc.h"
42 #include "xfs_btree.h"
43 #include "xfs_inode_item.h"
44 #include "xfs_bmap.h"
45 #include "xfs_attr.h"
46 #include "xfs_attr_leaf.h"
47 #include "xfs_error.h"
48 #include "xfs_quota.h"
49 #include "xfs_trans_space.h"
50 #include "xfs_acl.h"
51 #include "xfs_rw.h"
54 * xfs_attr.c
56 * Provide the external interfaces to manage attribute lists.
59 #define ATTR_SYSCOUNT 2
60 STATIC struct attrnames posix_acl_access;
61 STATIC struct attrnames posix_acl_default;
62 STATIC struct attrnames *attr_system_names[ATTR_SYSCOUNT];
64 /*========================================================================
65 * Function prototypes for the kernel.
66 *========================================================================*/
69 * Internal routines when attribute list fits inside the inode.
71 STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
74 * Internal routines when attribute list is one block.
76 STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
77 STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
78 STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
79 STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
82 * Internal routines when attribute list is more than one block.
84 STATIC int xfs_attr_node_get(xfs_da_args_t *args);
85 STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
86 STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
87 STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
88 STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
89 STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
92 * Routines to manipulate out-of-line attribute values.
94 STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
95 STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
97 #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
99 #if defined(XFS_ATTR_TRACE)
100 ktrace_t *xfs_attr_trace_buf;
101 #endif
104 /*========================================================================
105 * Overall external interface routines.
106 *========================================================================*/
109 xfs_attr_fetch(xfs_inode_t *ip, const char *name, int namelen,
110 char *value, int *valuelenp, int flags, struct cred *cred)
112 xfs_da_args_t args;
113 int error;
115 if ((XFS_IFORK_Q(ip) == 0) ||
116 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
117 ip->i_d.di_anextents == 0))
118 return(ENOATTR);
121 * Fill in the arg structure for this request.
123 memset((char *)&args, 0, sizeof(args));
124 args.name = name;
125 args.namelen = namelen;
126 args.value = value;
127 args.valuelen = *valuelenp;
128 args.flags = flags;
129 args.hashval = xfs_da_hashname(args.name, args.namelen);
130 args.dp = ip;
131 args.whichfork = XFS_ATTR_FORK;
134 * Decide on what work routines to call based on the inode size.
136 if (XFS_IFORK_Q(ip) == 0 ||
137 (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
138 ip->i_d.di_anextents == 0)) {
139 error = XFS_ERROR(ENOATTR);
140 } else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
141 error = xfs_attr_shortform_getvalue(&args);
142 } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
143 error = xfs_attr_leaf_get(&args);
144 } else {
145 error = xfs_attr_node_get(&args);
149 * Return the number of bytes in the value to the caller.
151 *valuelenp = args.valuelen;
153 if (error == EEXIST)
154 error = 0;
155 return(error);
159 xfs_attr_get(bhv_desc_t *bdp, const char *name, char *value, int *valuelenp,
160 int flags, struct cred *cred)
162 xfs_inode_t *ip = XFS_BHVTOI(bdp);
163 int error, namelen;
165 XFS_STATS_INC(xs_attr_get);
167 if (!name)
168 return(EINVAL);
169 namelen = strlen(name);
170 if (namelen >= MAXNAMELEN)
171 return(EFAULT); /* match IRIX behaviour */
173 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
174 return(EIO);
176 xfs_ilock(ip, XFS_ILOCK_SHARED);
177 error = xfs_attr_fetch(ip, name, namelen, value, valuelenp, flags, cred);
178 xfs_iunlock(ip, XFS_ILOCK_SHARED);
179 return(error);
183 xfs_attr_set_int(xfs_inode_t *dp, const char *name, int namelen,
184 char *value, int valuelen, int flags)
186 xfs_da_args_t args;
187 xfs_fsblock_t firstblock;
188 xfs_bmap_free_t flist;
189 int error, err2, committed;
190 int local, size;
191 uint nblks;
192 xfs_mount_t *mp = dp->i_mount;
193 int rsvd = (flags & ATTR_ROOT) != 0;
196 * Attach the dquots to the inode.
198 if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
199 return (error);
202 * Determine space new attribute will use, and if it would be
203 * "local" or "remote" (note: local != inline).
205 size = xfs_attr_leaf_newentsize(namelen, valuelen,
206 mp->m_sb.sb_blocksize, &local);
209 * If the inode doesn't have an attribute fork, add one.
210 * (inode must not be locked when we call this routine)
212 if (XFS_IFORK_Q(dp) == 0) {
213 if ((error = xfs_bmap_add_attrfork(dp, size, rsvd)))
214 return(error);
218 * Fill in the arg structure for this request.
220 memset((char *)&args, 0, sizeof(args));
221 args.name = name;
222 args.namelen = namelen;
223 args.value = value;
224 args.valuelen = valuelen;
225 args.flags = flags;
226 args.hashval = xfs_da_hashname(args.name, args.namelen);
227 args.dp = dp;
228 args.firstblock = &firstblock;
229 args.flist = &flist;
230 args.whichfork = XFS_ATTR_FORK;
231 args.addname = 1;
232 args.oknoent = 1;
234 nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
235 if (local) {
236 if (size > (mp->m_sb.sb_blocksize >> 1)) {
237 /* Double split possible */
238 nblks <<= 1;
240 } else {
241 uint dblocks = XFS_B_TO_FSB(mp, valuelen);
242 /* Out of line attribute, cannot double split, but make
243 * room for the attribute value itself.
245 nblks += dblocks;
246 nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
249 /* Size is now blocks for attribute data */
250 args.total = nblks;
253 * Start our first transaction of the day.
255 * All future transactions during this code must be "chained" off
256 * this one via the trans_dup() call. All transactions will contain
257 * the inode, and the inode will always be marked with trans_ihold().
258 * Since the inode will be locked in all transactions, we must log
259 * the inode in every transaction to let it float upward through
260 * the log.
262 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
265 * Root fork attributes can use reserved data blocks for this
266 * operation if necessary
269 if (rsvd)
270 args.trans->t_flags |= XFS_TRANS_RESERVE;
272 if ((error = xfs_trans_reserve(args.trans, (uint) nblks,
273 XFS_ATTRSET_LOG_RES(mp, nblks),
274 0, XFS_TRANS_PERM_LOG_RES,
275 XFS_ATTRSET_LOG_COUNT))) {
276 xfs_trans_cancel(args.trans, 0);
277 return(error);
279 xfs_ilock(dp, XFS_ILOCK_EXCL);
281 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, args.trans, dp, nblks, 0,
282 rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
283 XFS_QMOPT_RES_REGBLKS);
284 if (error) {
285 xfs_iunlock(dp, XFS_ILOCK_EXCL);
286 xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
287 return (error);
290 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
291 xfs_trans_ihold(args.trans, dp);
294 * If the attribute list is non-existent or a shortform list,
295 * upgrade it to a single-leaf-block attribute list.
297 if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
298 ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
299 (dp->i_d.di_anextents == 0))) {
302 * Build initial attribute list (if required).
304 if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
305 xfs_attr_shortform_create(&args);
308 * Try to add the attr to the attribute list in
309 * the inode.
311 error = xfs_attr_shortform_addname(&args);
312 if (error != ENOSPC) {
314 * Commit the shortform mods, and we're done.
315 * NOTE: this is also the error path (EEXIST, etc).
317 ASSERT(args.trans != NULL);
320 * If this is a synchronous mount, make sure that
321 * the transaction goes to disk before returning
322 * to the user.
324 if (mp->m_flags & XFS_MOUNT_WSYNC) {
325 xfs_trans_set_sync(args.trans);
327 err2 = xfs_trans_commit(args.trans,
328 XFS_TRANS_RELEASE_LOG_RES,
329 NULL);
330 xfs_iunlock(dp, XFS_ILOCK_EXCL);
333 * Hit the inode change time.
335 if (!error && (flags & ATTR_KERNOTIME) == 0) {
336 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
338 return(error == 0 ? err2 : error);
342 * It won't fit in the shortform, transform to a leaf block.
343 * GROT: another possible req'mt for a double-split btree op.
345 XFS_BMAP_INIT(args.flist, args.firstblock);
346 error = xfs_attr_shortform_to_leaf(&args);
347 if (!error) {
348 error = xfs_bmap_finish(&args.trans, args.flist,
349 *args.firstblock, &committed);
351 if (error) {
352 ASSERT(committed);
353 args.trans = NULL;
354 xfs_bmap_cancel(&flist);
355 goto out;
359 * bmap_finish() may have committed the last trans and started
360 * a new one. We need the inode to be in all transactions.
362 if (committed) {
363 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
364 xfs_trans_ihold(args.trans, dp);
368 * Commit the leaf transformation. We'll need another (linked)
369 * transaction to add the new attribute to the leaf.
371 if ((error = xfs_attr_rolltrans(&args.trans, dp)))
372 goto out;
376 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
377 error = xfs_attr_leaf_addname(&args);
378 } else {
379 error = xfs_attr_node_addname(&args);
381 if (error) {
382 goto out;
386 * If this is a synchronous mount, make sure that the
387 * transaction goes to disk before returning to the user.
389 if (mp->m_flags & XFS_MOUNT_WSYNC) {
390 xfs_trans_set_sync(args.trans);
394 * Commit the last in the sequence of transactions.
396 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
397 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES,
398 NULL);
399 xfs_iunlock(dp, XFS_ILOCK_EXCL);
402 * Hit the inode change time.
404 if (!error && (flags & ATTR_KERNOTIME) == 0) {
405 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
408 return(error);
410 out:
411 if (args.trans)
412 xfs_trans_cancel(args.trans,
413 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
414 xfs_iunlock(dp, XFS_ILOCK_EXCL);
415 return(error);
419 xfs_attr_set(bhv_desc_t *bdp, const char *name, char *value, int valuelen, int flags,
420 struct cred *cred)
422 xfs_inode_t *dp;
423 int namelen;
425 namelen = strlen(name);
426 if (namelen >= MAXNAMELEN)
427 return EFAULT; /* match IRIX behaviour */
429 XFS_STATS_INC(xs_attr_set);
431 dp = XFS_BHVTOI(bdp);
432 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
433 return (EIO);
435 return xfs_attr_set_int(dp, name, namelen, value, valuelen, flags);
439 * Generic handler routine to remove a name from an attribute list.
440 * Transitions attribute list from Btree to shortform as necessary.
443 xfs_attr_remove_int(xfs_inode_t *dp, const char *name, int namelen, int flags)
445 xfs_da_args_t args;
446 xfs_fsblock_t firstblock;
447 xfs_bmap_free_t flist;
448 int error;
449 xfs_mount_t *mp = dp->i_mount;
452 * Fill in the arg structure for this request.
454 memset((char *)&args, 0, sizeof(args));
455 args.name = name;
456 args.namelen = namelen;
457 args.flags = flags;
458 args.hashval = xfs_da_hashname(args.name, args.namelen);
459 args.dp = dp;
460 args.firstblock = &firstblock;
461 args.flist = &flist;
462 args.total = 0;
463 args.whichfork = XFS_ATTR_FORK;
466 * Attach the dquots to the inode.
468 if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
469 return (error);
472 * Start our first transaction of the day.
474 * All future transactions during this code must be "chained" off
475 * this one via the trans_dup() call. All transactions will contain
476 * the inode, and the inode will always be marked with trans_ihold().
477 * Since the inode will be locked in all transactions, we must log
478 * the inode in every transaction to let it float upward through
479 * the log.
481 args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
484 * Root fork attributes can use reserved data blocks for this
485 * operation if necessary
488 if (flags & ATTR_ROOT)
489 args.trans->t_flags |= XFS_TRANS_RESERVE;
491 if ((error = xfs_trans_reserve(args.trans,
492 XFS_ATTRRM_SPACE_RES(mp),
493 XFS_ATTRRM_LOG_RES(mp),
494 0, XFS_TRANS_PERM_LOG_RES,
495 XFS_ATTRRM_LOG_COUNT))) {
496 xfs_trans_cancel(args.trans, 0);
497 return(error);
500 xfs_ilock(dp, XFS_ILOCK_EXCL);
502 * No need to make quota reservations here. We expect to release some
503 * blocks not allocate in the common case.
505 xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
506 xfs_trans_ihold(args.trans, dp);
509 * Decide on what work routines to call based on the inode size.
511 if (XFS_IFORK_Q(dp) == 0 ||
512 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
513 dp->i_d.di_anextents == 0)) {
514 error = XFS_ERROR(ENOATTR);
515 goto out;
517 if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
518 ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
519 error = xfs_attr_shortform_remove(&args);
520 if (error) {
521 goto out;
523 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
524 error = xfs_attr_leaf_removename(&args);
525 } else {
526 error = xfs_attr_node_removename(&args);
528 if (error) {
529 goto out;
533 * If this is a synchronous mount, make sure that the
534 * transaction goes to disk before returning to the user.
536 if (mp->m_flags & XFS_MOUNT_WSYNC) {
537 xfs_trans_set_sync(args.trans);
541 * Commit the last in the sequence of transactions.
543 xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
544 error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES,
545 NULL);
546 xfs_iunlock(dp, XFS_ILOCK_EXCL);
549 * Hit the inode change time.
551 if (!error && (flags & ATTR_KERNOTIME) == 0) {
552 xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
555 return(error);
557 out:
558 if (args.trans)
559 xfs_trans_cancel(args.trans,
560 XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
561 xfs_iunlock(dp, XFS_ILOCK_EXCL);
562 return(error);
566 xfs_attr_remove(bhv_desc_t *bdp, const char *name, int flags, struct cred *cred)
568 xfs_inode_t *dp;
569 int namelen;
571 namelen = strlen(name);
572 if (namelen >= MAXNAMELEN)
573 return EFAULT; /* match IRIX behaviour */
575 XFS_STATS_INC(xs_attr_remove);
577 dp = XFS_BHVTOI(bdp);
578 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
579 return (EIO);
581 xfs_ilock(dp, XFS_ILOCK_SHARED);
582 if (XFS_IFORK_Q(dp) == 0 ||
583 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
584 dp->i_d.di_anextents == 0)) {
585 xfs_iunlock(dp, XFS_ILOCK_SHARED);
586 return(XFS_ERROR(ENOATTR));
588 xfs_iunlock(dp, XFS_ILOCK_SHARED);
590 return xfs_attr_remove_int(dp, name, namelen, flags);
593 int /* error */
594 xfs_attr_list_int(xfs_attr_list_context_t *context)
596 int error;
597 xfs_inode_t *dp = context->dp;
600 * Decide on what work routines to call based on the inode size.
602 if (XFS_IFORK_Q(dp) == 0 ||
603 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
604 dp->i_d.di_anextents == 0)) {
605 error = 0;
606 } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
607 error = xfs_attr_shortform_list(context);
608 } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
609 error = xfs_attr_leaf_list(context);
610 } else {
611 error = xfs_attr_node_list(context);
613 return error;
616 #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
617 (((struct attrlist_ent *) 0)->a_name - (char *) 0)
618 #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
619 ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
620 & ~(sizeof(u_int32_t)-1))
623 * Format an attribute and copy it out to the user's buffer.
624 * Take care to check values and protect against them changing later,
625 * we may be reading them directly out of a user buffer.
627 /*ARGSUSED*/
628 STATIC int
629 xfs_attr_put_listent(xfs_attr_list_context_t *context, attrnames_t *namesp,
630 char *name, int namelen,
631 int valuelen, char *value)
633 attrlist_ent_t *aep;
634 int arraytop;
636 ASSERT(!(context->flags & ATTR_KERNOVAL));
637 ASSERT(context->count >= 0);
638 ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
639 ASSERT(context->firstu >= sizeof(*context->alist));
640 ASSERT(context->firstu <= context->bufsize);
642 arraytop = sizeof(*context->alist) +
643 context->count * sizeof(context->alist->al_offset[0]);
644 context->firstu -= ATTR_ENTSIZE(namelen);
645 if (context->firstu < arraytop) {
646 xfs_attr_trace_l_c("buffer full", context);
647 context->alist->al_more = 1;
648 context->seen_enough = 1;
649 return 1;
652 aep = (attrlist_ent_t *)&(((char *)context->alist)[ context->firstu ]);
653 aep->a_valuelen = valuelen;
654 memcpy(aep->a_name, name, namelen);
655 aep->a_name[ namelen ] = 0;
656 context->alist->al_offset[ context->count++ ] = context->firstu;
657 context->alist->al_count = context->count;
658 xfs_attr_trace_l_c("add", context);
659 return 0;
662 STATIC int
663 xfs_attr_kern_list(xfs_attr_list_context_t *context, attrnames_t *namesp,
664 char *name, int namelen,
665 int valuelen, char *value)
667 char *offset;
668 int arraytop;
670 ASSERT(context->count >= 0);
672 arraytop = context->count + namesp->attr_namelen + namelen + 1;
673 if (arraytop > context->firstu) {
674 context->count = -1; /* insufficient space */
675 return 1;
677 offset = (char *)context->alist + context->count;
678 strncpy(offset, namesp->attr_name, namesp->attr_namelen);
679 offset += namesp->attr_namelen;
680 strncpy(offset, name, namelen); /* real name */
681 offset += namelen;
682 *offset = '\0';
683 context->count += namesp->attr_namelen + namelen + 1;
684 return 0;
687 /*ARGSUSED*/
688 STATIC int
689 xfs_attr_kern_list_sizes(xfs_attr_list_context_t *context, attrnames_t *namesp,
690 char *name, int namelen,
691 int valuelen, char *value)
693 context->count += namesp->attr_namelen + namelen + 1;
694 return 0;
698 * Generate a list of extended attribute names and optionally
699 * also value lengths. Positive return value follows the XFS
700 * convention of being an error, zero or negative return code
701 * is the length of the buffer returned (negated), indicating
702 * success.
705 xfs_attr_list(bhv_desc_t *bdp, char *buffer, int bufsize, int flags,
706 attrlist_cursor_kern_t *cursor, struct cred *cred)
708 xfs_attr_list_context_t context;
709 xfs_inode_t *dp;
710 int error;
712 XFS_STATS_INC(xs_attr_list);
715 * Validate the cursor.
717 if (cursor->pad1 || cursor->pad2)
718 return(XFS_ERROR(EINVAL));
719 if ((cursor->initted == 0) &&
720 (cursor->hashval || cursor->blkno || cursor->offset))
721 return XFS_ERROR(EINVAL);
724 * Check for a properly aligned buffer.
726 if (((long)buffer) & (sizeof(int)-1))
727 return XFS_ERROR(EFAULT);
728 if (flags & ATTR_KERNOVAL)
729 bufsize = 0;
732 * Initialize the output buffer.
734 context.dp = dp = XFS_BHVTOI(bdp);
735 context.cursor = cursor;
736 context.count = 0;
737 context.dupcnt = 0;
738 context.resynch = 1;
739 context.flags = flags;
740 context.seen_enough = 0;
741 context.alist = (attrlist_t *)buffer;
742 context.put_value = 0;
744 if (flags & ATTR_KERNAMELS) {
745 context.bufsize = bufsize;
746 context.firstu = context.bufsize;
747 if (flags & ATTR_KERNOVAL)
748 context.put_listent = xfs_attr_kern_list_sizes;
749 else
750 context.put_listent = xfs_attr_kern_list;
751 } else {
752 context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
753 context.firstu = context.bufsize;
754 context.alist->al_count = 0;
755 context.alist->al_more = 0;
756 context.alist->al_offset[0] = context.bufsize;
757 context.put_listent = xfs_attr_put_listent;
760 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
761 return EIO;
763 xfs_ilock(dp, XFS_ILOCK_SHARED);
764 xfs_attr_trace_l_c("syscall start", &context);
766 error = xfs_attr_list_int(&context);
768 xfs_iunlock(dp, XFS_ILOCK_SHARED);
769 xfs_attr_trace_l_c("syscall end", &context);
771 if (context.flags & (ATTR_KERNOVAL|ATTR_KERNAMELS)) {
772 /* must return negated buffer size or the error */
773 if (context.count < 0)
774 error = XFS_ERROR(ERANGE);
775 else
776 error = -context.count;
777 } else
778 ASSERT(error >= 0);
780 return error;
783 int /* error */
784 xfs_attr_inactive(xfs_inode_t *dp)
786 xfs_trans_t *trans;
787 xfs_mount_t *mp;
788 int error;
790 mp = dp->i_mount;
791 ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
793 xfs_ilock(dp, XFS_ILOCK_SHARED);
794 if ((XFS_IFORK_Q(dp) == 0) ||
795 (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
796 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
797 dp->i_d.di_anextents == 0)) {
798 xfs_iunlock(dp, XFS_ILOCK_SHARED);
799 return(0);
801 xfs_iunlock(dp, XFS_ILOCK_SHARED);
804 * Start our first transaction of the day.
806 * All future transactions during this code must be "chained" off
807 * this one via the trans_dup() call. All transactions will contain
808 * the inode, and the inode will always be marked with trans_ihold().
809 * Since the inode will be locked in all transactions, we must log
810 * the inode in every transaction to let it float upward through
811 * the log.
813 trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
814 if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
815 XFS_TRANS_PERM_LOG_RES,
816 XFS_ATTRINVAL_LOG_COUNT))) {
817 xfs_trans_cancel(trans, 0);
818 return(error);
820 xfs_ilock(dp, XFS_ILOCK_EXCL);
823 * No need to make quota reservations here. We expect to release some
824 * blocks, not allocate, in the common case.
826 xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
827 xfs_trans_ihold(trans, dp);
830 * Decide on what work routines to call based on the inode size.
832 if ((XFS_IFORK_Q(dp) == 0) ||
833 (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
834 (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
835 dp->i_d.di_anextents == 0)) {
836 error = 0;
837 goto out;
839 error = xfs_attr_root_inactive(&trans, dp);
840 if (error)
841 goto out;
843 * signal synchronous inactive transactions unless this
844 * is a synchronous mount filesystem in which case we
845 * know that we're here because we've been called out of
846 * xfs_inactive which means that the last reference is gone
847 * and the unlink transaction has already hit the disk so
848 * async inactive transactions are safe.
850 if ((error = xfs_itruncate_finish(&trans, dp, 0LL, XFS_ATTR_FORK,
851 (!(mp->m_flags & XFS_MOUNT_WSYNC)
852 ? 1 : 0))))
853 goto out;
856 * Commit the last in the sequence of transactions.
858 xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
859 error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES,
860 NULL);
861 xfs_iunlock(dp, XFS_ILOCK_EXCL);
863 return(error);
865 out:
866 xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
867 xfs_iunlock(dp, XFS_ILOCK_EXCL);
868 return(error);
873 /*========================================================================
874 * External routines when attribute list is inside the inode
875 *========================================================================*/
878 * Add a name to the shortform attribute list structure
879 * This is the external routine.
881 STATIC int
882 xfs_attr_shortform_addname(xfs_da_args_t *args)
884 int newsize, forkoff, retval;
886 retval = xfs_attr_shortform_lookup(args);
887 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
888 return(retval);
889 } else if (retval == EEXIST) {
890 if (args->flags & ATTR_CREATE)
891 return(retval);
892 retval = xfs_attr_shortform_remove(args);
893 ASSERT(retval == 0);
896 if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
897 args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
898 return(XFS_ERROR(ENOSPC));
900 newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
901 newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
903 forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
904 if (!forkoff)
905 return(XFS_ERROR(ENOSPC));
907 xfs_attr_shortform_add(args, forkoff);
908 return(0);
912 /*========================================================================
913 * External routines when attribute list is one block
914 *========================================================================*/
917 * Add a name to the leaf attribute list structure
919 * This leaf block cannot have a "remote" value, we only call this routine
920 * if bmap_one_block() says there is only one block (ie: no remote blks).
923 xfs_attr_leaf_addname(xfs_da_args_t *args)
925 xfs_inode_t *dp;
926 xfs_dabuf_t *bp;
927 int retval, error, committed, forkoff;
930 * Read the (only) block in the attribute list in.
932 dp = args->dp;
933 args->blkno = 0;
934 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
935 XFS_ATTR_FORK);
936 if (error)
937 return(error);
938 ASSERT(bp != NULL);
941 * Look up the given attribute in the leaf block. Figure out if
942 * the given flags produce an error or call for an atomic rename.
944 retval = xfs_attr_leaf_lookup_int(bp, args);
945 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
946 xfs_da_brelse(args->trans, bp);
947 return(retval);
948 } else if (retval == EEXIST) {
949 if (args->flags & ATTR_CREATE) { /* pure create op */
950 xfs_da_brelse(args->trans, bp);
951 return(retval);
953 args->rename = 1; /* an atomic rename */
954 args->blkno2 = args->blkno; /* set 2nd entry info*/
955 args->index2 = args->index;
956 args->rmtblkno2 = args->rmtblkno;
957 args->rmtblkcnt2 = args->rmtblkcnt;
961 * Add the attribute to the leaf block, transitioning to a Btree
962 * if required.
964 retval = xfs_attr_leaf_add(bp, args);
965 xfs_da_buf_done(bp);
966 if (retval == ENOSPC) {
968 * Promote the attribute list to the Btree format, then
969 * Commit that transaction so that the node_addname() call
970 * can manage its own transactions.
972 XFS_BMAP_INIT(args->flist, args->firstblock);
973 error = xfs_attr_leaf_to_node(args);
974 if (!error) {
975 error = xfs_bmap_finish(&args->trans, args->flist,
976 *args->firstblock, &committed);
978 if (error) {
979 ASSERT(committed);
980 args->trans = NULL;
981 xfs_bmap_cancel(args->flist);
982 return(error);
986 * bmap_finish() may have committed the last trans and started
987 * a new one. We need the inode to be in all transactions.
989 if (committed) {
990 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
991 xfs_trans_ihold(args->trans, dp);
995 * Commit the current trans (including the inode) and start
996 * a new one.
998 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
999 return (error);
1002 * Fob the whole rest of the problem off on the Btree code.
1004 error = xfs_attr_node_addname(args);
1005 return(error);
1009 * Commit the transaction that added the attr name so that
1010 * later routines can manage their own transactions.
1012 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1013 return (error);
1016 * If there was an out-of-line value, allocate the blocks we
1017 * identified for its storage and copy the value. This is done
1018 * after we create the attribute so that we don't overflow the
1019 * maximum size of a transaction and/or hit a deadlock.
1021 if (args->rmtblkno > 0) {
1022 error = xfs_attr_rmtval_set(args);
1023 if (error)
1024 return(error);
1028 * If this is an atomic rename operation, we must "flip" the
1029 * incomplete flags on the "new" and "old" attribute/value pairs
1030 * so that one disappears and one appears atomically. Then we
1031 * must remove the "old" attribute/value pair.
1033 if (args->rename) {
1035 * In a separate transaction, set the incomplete flag on the
1036 * "old" attr and clear the incomplete flag on the "new" attr.
1038 error = xfs_attr_leaf_flipflags(args);
1039 if (error)
1040 return(error);
1043 * Dismantle the "old" attribute/value pair by removing
1044 * a "remote" value (if it exists).
1046 args->index = args->index2;
1047 args->blkno = args->blkno2;
1048 args->rmtblkno = args->rmtblkno2;
1049 args->rmtblkcnt = args->rmtblkcnt2;
1050 if (args->rmtblkno) {
1051 error = xfs_attr_rmtval_remove(args);
1052 if (error)
1053 return(error);
1057 * Read in the block containing the "old" attr, then
1058 * remove the "old" attr from that block (neat, huh!)
1060 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
1061 &bp, XFS_ATTR_FORK);
1062 if (error)
1063 return(error);
1064 ASSERT(bp != NULL);
1065 (void)xfs_attr_leaf_remove(bp, args);
1068 * If the result is small enough, shrink it all into the inode.
1070 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1071 XFS_BMAP_INIT(args->flist, args->firstblock);
1072 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1073 /* bp is gone due to xfs_da_shrink_inode */
1074 if (!error) {
1075 error = xfs_bmap_finish(&args->trans,
1076 args->flist,
1077 *args->firstblock,
1078 &committed);
1080 if (error) {
1081 ASSERT(committed);
1082 args->trans = NULL;
1083 xfs_bmap_cancel(args->flist);
1084 return(error);
1088 * bmap_finish() may have committed the last trans
1089 * and started a new one. We need the inode to be
1090 * in all transactions.
1092 if (committed) {
1093 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1094 xfs_trans_ihold(args->trans, dp);
1096 } else
1097 xfs_da_buf_done(bp);
1100 * Commit the remove and start the next trans in series.
1102 error = xfs_attr_rolltrans(&args->trans, dp);
1104 } else if (args->rmtblkno > 0) {
1106 * Added a "remote" value, just clear the incomplete flag.
1108 error = xfs_attr_leaf_clearflag(args);
1110 return(error);
1114 * Remove a name from the leaf attribute list structure
1116 * This leaf block cannot have a "remote" value, we only call this routine
1117 * if bmap_one_block() says there is only one block (ie: no remote blks).
1119 STATIC int
1120 xfs_attr_leaf_removename(xfs_da_args_t *args)
1122 xfs_inode_t *dp;
1123 xfs_dabuf_t *bp;
1124 int error, committed, forkoff;
1127 * Remove the attribute.
1129 dp = args->dp;
1130 args->blkno = 0;
1131 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1132 XFS_ATTR_FORK);
1133 if (error) {
1134 return(error);
1137 ASSERT(bp != NULL);
1138 error = xfs_attr_leaf_lookup_int(bp, args);
1139 if (error == ENOATTR) {
1140 xfs_da_brelse(args->trans, bp);
1141 return(error);
1144 (void)xfs_attr_leaf_remove(bp, args);
1147 * If the result is small enough, shrink it all into the inode.
1149 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1150 XFS_BMAP_INIT(args->flist, args->firstblock);
1151 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1152 /* bp is gone due to xfs_da_shrink_inode */
1153 if (!error) {
1154 error = xfs_bmap_finish(&args->trans, args->flist,
1155 *args->firstblock, &committed);
1157 if (error) {
1158 ASSERT(committed);
1159 args->trans = NULL;
1160 xfs_bmap_cancel(args->flist);
1161 return(error);
1165 * bmap_finish() may have committed the last trans and started
1166 * a new one. We need the inode to be in all transactions.
1168 if (committed) {
1169 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1170 xfs_trans_ihold(args->trans, dp);
1172 } else
1173 xfs_da_buf_done(bp);
1174 return(0);
1178 * Look up a name in a leaf attribute list structure.
1180 * This leaf block cannot have a "remote" value, we only call this routine
1181 * if bmap_one_block() says there is only one block (ie: no remote blks).
1183 STATIC int
1184 xfs_attr_leaf_get(xfs_da_args_t *args)
1186 xfs_dabuf_t *bp;
1187 int error;
1189 args->blkno = 0;
1190 error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
1191 XFS_ATTR_FORK);
1192 if (error)
1193 return(error);
1194 ASSERT(bp != NULL);
1196 error = xfs_attr_leaf_lookup_int(bp, args);
1197 if (error != EEXIST) {
1198 xfs_da_brelse(args->trans, bp);
1199 return(error);
1201 error = xfs_attr_leaf_getvalue(bp, args);
1202 xfs_da_brelse(args->trans, bp);
1203 if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
1204 error = xfs_attr_rmtval_get(args);
1206 return(error);
1210 * Copy out attribute entries for attr_list(), for leaf attribute lists.
1212 STATIC int
1213 xfs_attr_leaf_list(xfs_attr_list_context_t *context)
1215 xfs_attr_leafblock_t *leaf;
1216 int error;
1217 xfs_dabuf_t *bp;
1219 context->cursor->blkno = 0;
1220 error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
1221 if (error)
1222 return XFS_ERROR(error);
1223 ASSERT(bp != NULL);
1224 leaf = bp->data;
1225 if (unlikely(be16_to_cpu(leaf->hdr.info.magic) != XFS_ATTR_LEAF_MAGIC)) {
1226 XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
1227 context->dp->i_mount, leaf);
1228 xfs_da_brelse(NULL, bp);
1229 return XFS_ERROR(EFSCORRUPTED);
1232 error = xfs_attr_leaf_list_int(bp, context);
1233 xfs_da_brelse(NULL, bp);
1234 return XFS_ERROR(error);
1238 /*========================================================================
1239 * External routines when attribute list size > XFS_LBSIZE(mp).
1240 *========================================================================*/
1243 * Add a name to a Btree-format attribute list.
1245 * This will involve walking down the Btree, and may involve splitting
1246 * leaf nodes and even splitting intermediate nodes up to and including
1247 * the root node (a special case of an intermediate node).
1249 * "Remote" attribute values confuse the issue and atomic rename operations
1250 * add a whole extra layer of confusion on top of that.
1252 STATIC int
1253 xfs_attr_node_addname(xfs_da_args_t *args)
1255 xfs_da_state_t *state;
1256 xfs_da_state_blk_t *blk;
1257 xfs_inode_t *dp;
1258 xfs_mount_t *mp;
1259 int committed, retval, error;
1262 * Fill in bucket of arguments/results/context to carry around.
1264 dp = args->dp;
1265 mp = dp->i_mount;
1266 restart:
1267 state = xfs_da_state_alloc();
1268 state->args = args;
1269 state->mp = mp;
1270 state->blocksize = state->mp->m_sb.sb_blocksize;
1271 state->node_ents = state->mp->m_attr_node_ents;
1274 * Search to see if name already exists, and get back a pointer
1275 * to where it should go.
1277 error = xfs_da_node_lookup_int(state, &retval);
1278 if (error)
1279 goto out;
1280 blk = &state->path.blk[ state->path.active-1 ];
1281 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1282 if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
1283 goto out;
1284 } else if (retval == EEXIST) {
1285 if (args->flags & ATTR_CREATE)
1286 goto out;
1287 args->rename = 1; /* atomic rename op */
1288 args->blkno2 = args->blkno; /* set 2nd entry info*/
1289 args->index2 = args->index;
1290 args->rmtblkno2 = args->rmtblkno;
1291 args->rmtblkcnt2 = args->rmtblkcnt;
1292 args->rmtblkno = 0;
1293 args->rmtblkcnt = 0;
1296 retval = xfs_attr_leaf_add(blk->bp, state->args);
1297 if (retval == ENOSPC) {
1298 if (state->path.active == 1) {
1300 * Its really a single leaf node, but it had
1301 * out-of-line values so it looked like it *might*
1302 * have been a b-tree.
1304 xfs_da_state_free(state);
1305 XFS_BMAP_INIT(args->flist, args->firstblock);
1306 error = xfs_attr_leaf_to_node(args);
1307 if (!error) {
1308 error = xfs_bmap_finish(&args->trans,
1309 args->flist,
1310 *args->firstblock,
1311 &committed);
1313 if (error) {
1314 ASSERT(committed);
1315 args->trans = NULL;
1316 xfs_bmap_cancel(args->flist);
1317 goto out;
1321 * bmap_finish() may have committed the last trans
1322 * and started a new one. We need the inode to be
1323 * in all transactions.
1325 if (committed) {
1326 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1327 xfs_trans_ihold(args->trans, dp);
1331 * Commit the node conversion and start the next
1332 * trans in the chain.
1334 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1335 goto out;
1337 goto restart;
1341 * Split as many Btree elements as required.
1342 * This code tracks the new and old attr's location
1343 * in the index/blkno/rmtblkno/rmtblkcnt fields and
1344 * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
1346 XFS_BMAP_INIT(args->flist, args->firstblock);
1347 error = xfs_da_split(state);
1348 if (!error) {
1349 error = xfs_bmap_finish(&args->trans, args->flist,
1350 *args->firstblock, &committed);
1352 if (error) {
1353 ASSERT(committed);
1354 args->trans = NULL;
1355 xfs_bmap_cancel(args->flist);
1356 goto out;
1360 * bmap_finish() may have committed the last trans and started
1361 * a new one. We need the inode to be in all transactions.
1363 if (committed) {
1364 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1365 xfs_trans_ihold(args->trans, dp);
1367 } else {
1369 * Addition succeeded, update Btree hashvals.
1371 xfs_da_fixhashpath(state, &state->path);
1375 * Kill the state structure, we're done with it and need to
1376 * allow the buffers to come back later.
1378 xfs_da_state_free(state);
1379 state = NULL;
1382 * Commit the leaf addition or btree split and start the next
1383 * trans in the chain.
1385 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1386 goto out;
1389 * If there was an out-of-line value, allocate the blocks we
1390 * identified for its storage and copy the value. This is done
1391 * after we create the attribute so that we don't overflow the
1392 * maximum size of a transaction and/or hit a deadlock.
1394 if (args->rmtblkno > 0) {
1395 error = xfs_attr_rmtval_set(args);
1396 if (error)
1397 return(error);
1401 * If this is an atomic rename operation, we must "flip" the
1402 * incomplete flags on the "new" and "old" attribute/value pairs
1403 * so that one disappears and one appears atomically. Then we
1404 * must remove the "old" attribute/value pair.
1406 if (args->rename) {
1408 * In a separate transaction, set the incomplete flag on the
1409 * "old" attr and clear the incomplete flag on the "new" attr.
1411 error = xfs_attr_leaf_flipflags(args);
1412 if (error)
1413 goto out;
1416 * Dismantle the "old" attribute/value pair by removing
1417 * a "remote" value (if it exists).
1419 args->index = args->index2;
1420 args->blkno = args->blkno2;
1421 args->rmtblkno = args->rmtblkno2;
1422 args->rmtblkcnt = args->rmtblkcnt2;
1423 if (args->rmtblkno) {
1424 error = xfs_attr_rmtval_remove(args);
1425 if (error)
1426 return(error);
1430 * Re-find the "old" attribute entry after any split ops.
1431 * The INCOMPLETE flag means that we will find the "old"
1432 * attr, not the "new" one.
1434 args->flags |= XFS_ATTR_INCOMPLETE;
1435 state = xfs_da_state_alloc();
1436 state->args = args;
1437 state->mp = mp;
1438 state->blocksize = state->mp->m_sb.sb_blocksize;
1439 state->node_ents = state->mp->m_attr_node_ents;
1440 state->inleaf = 0;
1441 error = xfs_da_node_lookup_int(state, &retval);
1442 if (error)
1443 goto out;
1446 * Remove the name and update the hashvals in the tree.
1448 blk = &state->path.blk[ state->path.active-1 ];
1449 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1450 error = xfs_attr_leaf_remove(blk->bp, args);
1451 xfs_da_fixhashpath(state, &state->path);
1454 * Check to see if the tree needs to be collapsed.
1456 if (retval && (state->path.active > 1)) {
1457 XFS_BMAP_INIT(args->flist, args->firstblock);
1458 error = xfs_da_join(state);
1459 if (!error) {
1460 error = xfs_bmap_finish(&args->trans,
1461 args->flist,
1462 *args->firstblock,
1463 &committed);
1465 if (error) {
1466 ASSERT(committed);
1467 args->trans = NULL;
1468 xfs_bmap_cancel(args->flist);
1469 goto out;
1473 * bmap_finish() may have committed the last trans
1474 * and started a new one. We need the inode to be
1475 * in all transactions.
1477 if (committed) {
1478 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1479 xfs_trans_ihold(args->trans, dp);
1484 * Commit and start the next trans in the chain.
1486 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1487 goto out;
1489 } else if (args->rmtblkno > 0) {
1491 * Added a "remote" value, just clear the incomplete flag.
1493 error = xfs_attr_leaf_clearflag(args);
1494 if (error)
1495 goto out;
1497 retval = error = 0;
1499 out:
1500 if (state)
1501 xfs_da_state_free(state);
1502 if (error)
1503 return(error);
1504 return(retval);
1508 * Remove a name from a B-tree attribute list.
1510 * This will involve walking down the Btree, and may involve joining
1511 * leaf nodes and even joining intermediate nodes up to and including
1512 * the root node (a special case of an intermediate node).
1514 STATIC int
1515 xfs_attr_node_removename(xfs_da_args_t *args)
1517 xfs_da_state_t *state;
1518 xfs_da_state_blk_t *blk;
1519 xfs_inode_t *dp;
1520 xfs_dabuf_t *bp;
1521 int retval, error, committed, forkoff;
1524 * Tie a string around our finger to remind us where we are.
1526 dp = args->dp;
1527 state = xfs_da_state_alloc();
1528 state->args = args;
1529 state->mp = dp->i_mount;
1530 state->blocksize = state->mp->m_sb.sb_blocksize;
1531 state->node_ents = state->mp->m_attr_node_ents;
1534 * Search to see if name exists, and get back a pointer to it.
1536 error = xfs_da_node_lookup_int(state, &retval);
1537 if (error || (retval != EEXIST)) {
1538 if (error == 0)
1539 error = retval;
1540 goto out;
1544 * If there is an out-of-line value, de-allocate the blocks.
1545 * This is done before we remove the attribute so that we don't
1546 * overflow the maximum size of a transaction and/or hit a deadlock.
1548 blk = &state->path.blk[ state->path.active-1 ];
1549 ASSERT(blk->bp != NULL);
1550 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1551 if (args->rmtblkno > 0) {
1553 * Fill in disk block numbers in the state structure
1554 * so that we can get the buffers back after we commit
1555 * several transactions in the following calls.
1557 error = xfs_attr_fillstate(state);
1558 if (error)
1559 goto out;
1562 * Mark the attribute as INCOMPLETE, then bunmapi() the
1563 * remote value.
1565 error = xfs_attr_leaf_setflag(args);
1566 if (error)
1567 goto out;
1568 error = xfs_attr_rmtval_remove(args);
1569 if (error)
1570 goto out;
1573 * Refill the state structure with buffers, the prior calls
1574 * released our buffers.
1576 error = xfs_attr_refillstate(state);
1577 if (error)
1578 goto out;
1582 * Remove the name and update the hashvals in the tree.
1584 blk = &state->path.blk[ state->path.active-1 ];
1585 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1586 retval = xfs_attr_leaf_remove(blk->bp, args);
1587 xfs_da_fixhashpath(state, &state->path);
1590 * Check to see if the tree needs to be collapsed.
1592 if (retval && (state->path.active > 1)) {
1593 XFS_BMAP_INIT(args->flist, args->firstblock);
1594 error = xfs_da_join(state);
1595 if (!error) {
1596 error = xfs_bmap_finish(&args->trans, args->flist,
1597 *args->firstblock, &committed);
1599 if (error) {
1600 ASSERT(committed);
1601 args->trans = NULL;
1602 xfs_bmap_cancel(args->flist);
1603 goto out;
1607 * bmap_finish() may have committed the last trans and started
1608 * a new one. We need the inode to be in all transactions.
1610 if (committed) {
1611 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1612 xfs_trans_ihold(args->trans, dp);
1616 * Commit the Btree join operation and start a new trans.
1618 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
1619 goto out;
1623 * If the result is small enough, push it all into the inode.
1625 if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
1627 * Have to get rid of the copy of this dabuf in the state.
1629 ASSERT(state->path.active == 1);
1630 ASSERT(state->path.blk[0].bp);
1631 xfs_da_buf_done(state->path.blk[0].bp);
1632 state->path.blk[0].bp = NULL;
1634 error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
1635 XFS_ATTR_FORK);
1636 if (error)
1637 goto out;
1638 ASSERT(be16_to_cpu(((xfs_attr_leafblock_t *)
1639 bp->data)->hdr.info.magic)
1640 == XFS_ATTR_LEAF_MAGIC);
1642 if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
1643 XFS_BMAP_INIT(args->flist, args->firstblock);
1644 error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
1645 /* bp is gone due to xfs_da_shrink_inode */
1646 if (!error) {
1647 error = xfs_bmap_finish(&args->trans,
1648 args->flist,
1649 *args->firstblock,
1650 &committed);
1652 if (error) {
1653 ASSERT(committed);
1654 args->trans = NULL;
1655 xfs_bmap_cancel(args->flist);
1656 goto out;
1660 * bmap_finish() may have committed the last trans
1661 * and started a new one. We need the inode to be
1662 * in all transactions.
1664 if (committed) {
1665 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
1666 xfs_trans_ihold(args->trans, dp);
1668 } else
1669 xfs_da_brelse(args->trans, bp);
1671 error = 0;
1673 out:
1674 xfs_da_state_free(state);
1675 return(error);
1679 * Fill in the disk block numbers in the state structure for the buffers
1680 * that are attached to the state structure.
1681 * This is done so that we can quickly reattach ourselves to those buffers
1682 * after some set of transaction commits have released these buffers.
1684 STATIC int
1685 xfs_attr_fillstate(xfs_da_state_t *state)
1687 xfs_da_state_path_t *path;
1688 xfs_da_state_blk_t *blk;
1689 int level;
1692 * Roll down the "path" in the state structure, storing the on-disk
1693 * block number for those buffers in the "path".
1695 path = &state->path;
1696 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1697 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1698 if (blk->bp) {
1699 blk->disk_blkno = xfs_da_blkno(blk->bp);
1700 xfs_da_buf_done(blk->bp);
1701 blk->bp = NULL;
1702 } else {
1703 blk->disk_blkno = 0;
1708 * Roll down the "altpath" in the state structure, storing the on-disk
1709 * block number for those buffers in the "altpath".
1711 path = &state->altpath;
1712 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1713 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1714 if (blk->bp) {
1715 blk->disk_blkno = xfs_da_blkno(blk->bp);
1716 xfs_da_buf_done(blk->bp);
1717 blk->bp = NULL;
1718 } else {
1719 blk->disk_blkno = 0;
1723 return(0);
1727 * Reattach the buffers to the state structure based on the disk block
1728 * numbers stored in the state structure.
1729 * This is done after some set of transaction commits have released those
1730 * buffers from our grip.
1732 STATIC int
1733 xfs_attr_refillstate(xfs_da_state_t *state)
1735 xfs_da_state_path_t *path;
1736 xfs_da_state_blk_t *blk;
1737 int level, error;
1740 * Roll down the "path" in the state structure, storing the on-disk
1741 * block number for those buffers in the "path".
1743 path = &state->path;
1744 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1745 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1746 if (blk->disk_blkno) {
1747 error = xfs_da_read_buf(state->args->trans,
1748 state->args->dp,
1749 blk->blkno, blk->disk_blkno,
1750 &blk->bp, XFS_ATTR_FORK);
1751 if (error)
1752 return(error);
1753 } else {
1754 blk->bp = NULL;
1759 * Roll down the "altpath" in the state structure, storing the on-disk
1760 * block number for those buffers in the "altpath".
1762 path = &state->altpath;
1763 ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
1764 for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
1765 if (blk->disk_blkno) {
1766 error = xfs_da_read_buf(state->args->trans,
1767 state->args->dp,
1768 blk->blkno, blk->disk_blkno,
1769 &blk->bp, XFS_ATTR_FORK);
1770 if (error)
1771 return(error);
1772 } else {
1773 blk->bp = NULL;
1777 return(0);
1781 * Look up a filename in a node attribute list.
1783 * This routine gets called for any attribute fork that has more than one
1784 * block, ie: both true Btree attr lists and for single-leaf-blocks with
1785 * "remote" values taking up more blocks.
1787 STATIC int
1788 xfs_attr_node_get(xfs_da_args_t *args)
1790 xfs_da_state_t *state;
1791 xfs_da_state_blk_t *blk;
1792 int error, retval;
1793 int i;
1795 state = xfs_da_state_alloc();
1796 state->args = args;
1797 state->mp = args->dp->i_mount;
1798 state->blocksize = state->mp->m_sb.sb_blocksize;
1799 state->node_ents = state->mp->m_attr_node_ents;
1802 * Search to see if name exists, and get back a pointer to it.
1804 error = xfs_da_node_lookup_int(state, &retval);
1805 if (error) {
1806 retval = error;
1807 } else if (retval == EEXIST) {
1808 blk = &state->path.blk[ state->path.active-1 ];
1809 ASSERT(blk->bp != NULL);
1810 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
1813 * Get the value, local or "remote"
1815 retval = xfs_attr_leaf_getvalue(blk->bp, args);
1816 if (!retval && (args->rmtblkno > 0)
1817 && !(args->flags & ATTR_KERNOVAL)) {
1818 retval = xfs_attr_rmtval_get(args);
1823 * If not in a transaction, we have to release all the buffers.
1825 for (i = 0; i < state->path.active; i++) {
1826 xfs_da_brelse(args->trans, state->path.blk[i].bp);
1827 state->path.blk[i].bp = NULL;
1830 xfs_da_state_free(state);
1831 return(retval);
1834 STATIC int /* error */
1835 xfs_attr_node_list(xfs_attr_list_context_t *context)
1837 attrlist_cursor_kern_t *cursor;
1838 xfs_attr_leafblock_t *leaf;
1839 xfs_da_intnode_t *node;
1840 xfs_da_node_entry_t *btree;
1841 int error, i;
1842 xfs_dabuf_t *bp;
1844 cursor = context->cursor;
1845 cursor->initted = 1;
1848 * Do all sorts of validation on the passed-in cursor structure.
1849 * If anything is amiss, ignore the cursor and look up the hashval
1850 * starting from the btree root.
1852 bp = NULL;
1853 if (cursor->blkno > 0) {
1854 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1855 &bp, XFS_ATTR_FORK);
1856 if ((error != 0) && (error != EFSCORRUPTED))
1857 return(error);
1858 if (bp) {
1859 node = bp->data;
1860 switch (be16_to_cpu(node->hdr.info.magic)) {
1861 case XFS_DA_NODE_MAGIC:
1862 xfs_attr_trace_l_cn("wrong blk", context, node);
1863 xfs_da_brelse(NULL, bp);
1864 bp = NULL;
1865 break;
1866 case XFS_ATTR_LEAF_MAGIC:
1867 leaf = bp->data;
1868 if (cursor->hashval > be32_to_cpu(leaf->entries[
1869 be16_to_cpu(leaf->hdr.count)-1].hashval)) {
1870 xfs_attr_trace_l_cl("wrong blk",
1871 context, leaf);
1872 xfs_da_brelse(NULL, bp);
1873 bp = NULL;
1874 } else if (cursor->hashval <=
1875 be32_to_cpu(leaf->entries[0].hashval)) {
1876 xfs_attr_trace_l_cl("maybe wrong blk",
1877 context, leaf);
1878 xfs_da_brelse(NULL, bp);
1879 bp = NULL;
1881 break;
1882 default:
1883 xfs_attr_trace_l_c("wrong blk - ??", context);
1884 xfs_da_brelse(NULL, bp);
1885 bp = NULL;
1891 * We did not find what we expected given the cursor's contents,
1892 * so we start from the top and work down based on the hash value.
1893 * Note that start of node block is same as start of leaf block.
1895 if (bp == NULL) {
1896 cursor->blkno = 0;
1897 for (;;) {
1898 error = xfs_da_read_buf(NULL, context->dp,
1899 cursor->blkno, -1, &bp,
1900 XFS_ATTR_FORK);
1901 if (error)
1902 return(error);
1903 if (unlikely(bp == NULL)) {
1904 XFS_ERROR_REPORT("xfs_attr_node_list(2)",
1905 XFS_ERRLEVEL_LOW,
1906 context->dp->i_mount);
1907 return(XFS_ERROR(EFSCORRUPTED));
1909 node = bp->data;
1910 if (be16_to_cpu(node->hdr.info.magic)
1911 == XFS_ATTR_LEAF_MAGIC)
1912 break;
1913 if (unlikely(be16_to_cpu(node->hdr.info.magic)
1914 != XFS_DA_NODE_MAGIC)) {
1915 XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
1916 XFS_ERRLEVEL_LOW,
1917 context->dp->i_mount,
1918 node);
1919 xfs_da_brelse(NULL, bp);
1920 return(XFS_ERROR(EFSCORRUPTED));
1922 btree = node->btree;
1923 for (i = 0; i < be16_to_cpu(node->hdr.count);
1924 btree++, i++) {
1925 if (cursor->hashval
1926 <= be32_to_cpu(btree->hashval)) {
1927 cursor->blkno = be32_to_cpu(btree->before);
1928 xfs_attr_trace_l_cb("descending",
1929 context, btree);
1930 break;
1933 if (i == be16_to_cpu(node->hdr.count)) {
1934 xfs_da_brelse(NULL, bp);
1935 return(0);
1937 xfs_da_brelse(NULL, bp);
1940 ASSERT(bp != NULL);
1943 * Roll upward through the blocks, processing each leaf block in
1944 * order. As long as there is space in the result buffer, keep
1945 * adding the information.
1947 for (;;) {
1948 leaf = bp->data;
1949 if (unlikely(be16_to_cpu(leaf->hdr.info.magic)
1950 != XFS_ATTR_LEAF_MAGIC)) {
1951 XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
1952 XFS_ERRLEVEL_LOW,
1953 context->dp->i_mount, leaf);
1954 xfs_da_brelse(NULL, bp);
1955 return(XFS_ERROR(EFSCORRUPTED));
1957 error = xfs_attr_leaf_list_int(bp, context);
1958 if (error) {
1959 xfs_da_brelse(NULL, bp);
1960 return error;
1962 if (context->seen_enough || leaf->hdr.info.forw == 0)
1963 break;
1964 cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
1965 xfs_da_brelse(NULL, bp);
1966 error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
1967 &bp, XFS_ATTR_FORK);
1968 if (error)
1969 return(error);
1970 if (unlikely((bp == NULL))) {
1971 XFS_ERROR_REPORT("xfs_attr_node_list(5)",
1972 XFS_ERRLEVEL_LOW,
1973 context->dp->i_mount);
1974 return(XFS_ERROR(EFSCORRUPTED));
1977 xfs_da_brelse(NULL, bp);
1978 return(0);
1982 /*========================================================================
1983 * External routines for manipulating out-of-line attribute values.
1984 *========================================================================*/
1987 * Read the value associated with an attribute from the out-of-line buffer
1988 * that we stored it in.
1991 xfs_attr_rmtval_get(xfs_da_args_t *args)
1993 xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
1994 xfs_mount_t *mp;
1995 xfs_daddr_t dblkno;
1996 xfs_caddr_t dst;
1997 xfs_buf_t *bp;
1998 int nmap, error, tmp, valuelen, blkcnt, i;
1999 xfs_dablk_t lblkno;
2001 ASSERT(!(args->flags & ATTR_KERNOVAL));
2003 mp = args->dp->i_mount;
2004 dst = args->value;
2005 valuelen = args->valuelen;
2006 lblkno = args->rmtblkno;
2007 while (valuelen > 0) {
2008 nmap = ATTR_RMTVALUE_MAPSIZE;
2009 error = xfs_bmapi(args->trans, args->dp, (xfs_fileoff_t)lblkno,
2010 args->rmtblkcnt,
2011 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2012 NULL, 0, map, &nmap, NULL, NULL);
2013 if (error)
2014 return(error);
2015 ASSERT(nmap >= 1);
2017 for (i = 0; (i < nmap) && (valuelen > 0); i++) {
2018 ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
2019 (map[i].br_startblock != HOLESTARTBLOCK));
2020 dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
2021 blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
2022 error = xfs_read_buf(mp, mp->m_ddev_targp, dblkno,
2023 blkcnt, XFS_BUF_LOCK, &bp);
2024 if (error)
2025 return(error);
2027 tmp = (valuelen < XFS_BUF_SIZE(bp))
2028 ? valuelen : XFS_BUF_SIZE(bp);
2029 xfs_biomove(bp, 0, tmp, dst, XFS_B_READ);
2030 xfs_buf_relse(bp);
2031 dst += tmp;
2032 valuelen -= tmp;
2034 lblkno += map[i].br_blockcount;
2037 ASSERT(valuelen == 0);
2038 return(0);
2042 * Write the value associated with an attribute into the out-of-line buffer
2043 * that we have defined for it.
2045 STATIC int
2046 xfs_attr_rmtval_set(xfs_da_args_t *args)
2048 xfs_mount_t *mp;
2049 xfs_fileoff_t lfileoff;
2050 xfs_inode_t *dp;
2051 xfs_bmbt_irec_t map;
2052 xfs_daddr_t dblkno;
2053 xfs_caddr_t src;
2054 xfs_buf_t *bp;
2055 xfs_dablk_t lblkno;
2056 int blkcnt, valuelen, nmap, error, tmp, committed;
2058 dp = args->dp;
2059 mp = dp->i_mount;
2060 src = args->value;
2063 * Find a "hole" in the attribute address space large enough for
2064 * us to drop the new attribute's value into.
2066 blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
2067 lfileoff = 0;
2068 error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
2069 XFS_ATTR_FORK);
2070 if (error) {
2071 return(error);
2073 args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
2074 args->rmtblkcnt = blkcnt;
2077 * Roll through the "value", allocating blocks on disk as required.
2079 while (blkcnt > 0) {
2081 * Allocate a single extent, up to the size of the value.
2083 XFS_BMAP_INIT(args->flist, args->firstblock);
2084 nmap = 1;
2085 error = xfs_bmapi(args->trans, dp, (xfs_fileoff_t)lblkno,
2086 blkcnt,
2087 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA |
2088 XFS_BMAPI_WRITE,
2089 args->firstblock, args->total, &map, &nmap,
2090 args->flist, NULL);
2091 if (!error) {
2092 error = xfs_bmap_finish(&args->trans, args->flist,
2093 *args->firstblock, &committed);
2095 if (error) {
2096 ASSERT(committed);
2097 args->trans = NULL;
2098 xfs_bmap_cancel(args->flist);
2099 return(error);
2103 * bmap_finish() may have committed the last trans and started
2104 * a new one. We need the inode to be in all transactions.
2106 if (committed) {
2107 xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
2108 xfs_trans_ihold(args->trans, dp);
2111 ASSERT(nmap == 1);
2112 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2113 (map.br_startblock != HOLESTARTBLOCK));
2114 lblkno += map.br_blockcount;
2115 blkcnt -= map.br_blockcount;
2118 * Start the next trans in the chain.
2120 if ((error = xfs_attr_rolltrans(&args->trans, dp)))
2121 return (error);
2125 * Roll through the "value", copying the attribute value to the
2126 * already-allocated blocks. Blocks are written synchronously
2127 * so that we can know they are all on disk before we turn off
2128 * the INCOMPLETE flag.
2130 lblkno = args->rmtblkno;
2131 valuelen = args->valuelen;
2132 while (valuelen > 0) {
2134 * Try to remember where we decided to put the value.
2136 XFS_BMAP_INIT(args->flist, args->firstblock);
2137 nmap = 1;
2138 error = xfs_bmapi(NULL, dp, (xfs_fileoff_t)lblkno,
2139 args->rmtblkcnt,
2140 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2141 args->firstblock, 0, &map, &nmap,
2142 NULL, NULL);
2143 if (error) {
2144 return(error);
2146 ASSERT(nmap == 1);
2147 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2148 (map.br_startblock != HOLESTARTBLOCK));
2150 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2151 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2153 bp = xfs_buf_get_flags(mp->m_ddev_targp, dblkno,
2154 blkcnt, XFS_BUF_LOCK);
2155 ASSERT(bp);
2156 ASSERT(!XFS_BUF_GETERROR(bp));
2158 tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
2159 XFS_BUF_SIZE(bp);
2160 xfs_biomove(bp, 0, tmp, src, XFS_B_WRITE);
2161 if (tmp < XFS_BUF_SIZE(bp))
2162 xfs_biozero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
2163 if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
2164 return (error);
2166 src += tmp;
2167 valuelen -= tmp;
2169 lblkno += map.br_blockcount;
2171 ASSERT(valuelen == 0);
2172 return(0);
2176 * Remove the value associated with an attribute by deleting the
2177 * out-of-line buffer that it is stored on.
2179 STATIC int
2180 xfs_attr_rmtval_remove(xfs_da_args_t *args)
2182 xfs_mount_t *mp;
2183 xfs_bmbt_irec_t map;
2184 xfs_buf_t *bp;
2185 xfs_daddr_t dblkno;
2186 xfs_dablk_t lblkno;
2187 int valuelen, blkcnt, nmap, error, done, committed;
2189 mp = args->dp->i_mount;
2192 * Roll through the "value", invalidating the attribute value's
2193 * blocks.
2195 lblkno = args->rmtblkno;
2196 valuelen = args->rmtblkcnt;
2197 while (valuelen > 0) {
2199 * Try to remember where we decided to put the value.
2201 XFS_BMAP_INIT(args->flist, args->firstblock);
2202 nmap = 1;
2203 error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
2204 args->rmtblkcnt,
2205 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2206 args->firstblock, 0, &map, &nmap,
2207 args->flist, NULL);
2208 if (error) {
2209 return(error);
2211 ASSERT(nmap == 1);
2212 ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
2213 (map.br_startblock != HOLESTARTBLOCK));
2215 dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
2216 blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
2219 * If the "remote" value is in the cache, remove it.
2221 bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt,
2222 XFS_INCORE_TRYLOCK);
2223 if (bp) {
2224 XFS_BUF_STALE(bp);
2225 XFS_BUF_UNDELAYWRITE(bp);
2226 xfs_buf_relse(bp);
2227 bp = NULL;
2230 valuelen -= map.br_blockcount;
2232 lblkno += map.br_blockcount;
2236 * Keep de-allocating extents until the remote-value region is gone.
2238 lblkno = args->rmtblkno;
2239 blkcnt = args->rmtblkcnt;
2240 done = 0;
2241 while (!done) {
2242 XFS_BMAP_INIT(args->flist, args->firstblock);
2243 error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
2244 XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
2245 1, args->firstblock, args->flist,
2246 NULL, &done);
2247 if (!error) {
2248 error = xfs_bmap_finish(&args->trans, args->flist,
2249 *args->firstblock, &committed);
2251 if (error) {
2252 ASSERT(committed);
2253 args->trans = NULL;
2254 xfs_bmap_cancel(args->flist);
2255 return(error);
2259 * bmap_finish() may have committed the last trans and started
2260 * a new one. We need the inode to be in all transactions.
2262 if (committed) {
2263 xfs_trans_ijoin(args->trans, args->dp, XFS_ILOCK_EXCL);
2264 xfs_trans_ihold(args->trans, args->dp);
2268 * Close out trans and start the next one in the chain.
2270 if ((error = xfs_attr_rolltrans(&args->trans, args->dp)))
2271 return (error);
2273 return(0);
2276 #if defined(XFS_ATTR_TRACE)
2278 * Add a trace buffer entry for an attr_list context structure.
2280 void
2281 xfs_attr_trace_l_c(char *where, struct xfs_attr_list_context *context)
2283 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_C, where,
2284 (__psunsigned_t)context->dp,
2285 (__psunsigned_t)context->cursor->hashval,
2286 (__psunsigned_t)context->cursor->blkno,
2287 (__psunsigned_t)context->cursor->offset,
2288 (__psunsigned_t)context->alist,
2289 (__psunsigned_t)context->bufsize,
2290 (__psunsigned_t)context->count,
2291 (__psunsigned_t)context->firstu,
2292 (__psunsigned_t)
2293 ((context->count > 0) &&
2294 !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
2295 ? (ATTR_ENTRY(context->alist,
2296 context->count-1)->a_valuelen)
2297 : 0,
2298 (__psunsigned_t)context->dupcnt,
2299 (__psunsigned_t)context->flags,
2300 (__psunsigned_t)NULL,
2301 (__psunsigned_t)NULL,
2302 (__psunsigned_t)NULL);
2306 * Add a trace buffer entry for a context structure and a Btree node.
2308 void
2309 xfs_attr_trace_l_cn(char *where, struct xfs_attr_list_context *context,
2310 struct xfs_da_intnode *node)
2312 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CN, where,
2313 (__psunsigned_t)context->dp,
2314 (__psunsigned_t)context->cursor->hashval,
2315 (__psunsigned_t)context->cursor->blkno,
2316 (__psunsigned_t)context->cursor->offset,
2317 (__psunsigned_t)context->alist,
2318 (__psunsigned_t)context->bufsize,
2319 (__psunsigned_t)context->count,
2320 (__psunsigned_t)context->firstu,
2321 (__psunsigned_t)
2322 ((context->count > 0) &&
2323 !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
2324 ? (ATTR_ENTRY(context->alist,
2325 context->count-1)->a_valuelen)
2326 : 0,
2327 (__psunsigned_t)context->dupcnt,
2328 (__psunsigned_t)context->flags,
2329 (__psunsigned_t)be16_to_cpu(node->hdr.count),
2330 (__psunsigned_t)be32_to_cpu(node->btree[0].hashval),
2331 (__psunsigned_t)be32_to_cpu(node->btree[
2332 be16_to_cpu(node->hdr.count)-1].hashval));
2336 * Add a trace buffer entry for a context structure and a Btree element.
2338 void
2339 xfs_attr_trace_l_cb(char *where, struct xfs_attr_list_context *context,
2340 struct xfs_da_node_entry *btree)
2342 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CB, where,
2343 (__psunsigned_t)context->dp,
2344 (__psunsigned_t)context->cursor->hashval,
2345 (__psunsigned_t)context->cursor->blkno,
2346 (__psunsigned_t)context->cursor->offset,
2347 (__psunsigned_t)context->alist,
2348 (__psunsigned_t)context->bufsize,
2349 (__psunsigned_t)context->count,
2350 (__psunsigned_t)context->firstu,
2351 (__psunsigned_t)
2352 ((context->count > 0) &&
2353 !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
2354 ? (ATTR_ENTRY(context->alist,
2355 context->count-1)->a_valuelen)
2356 : 0,
2357 (__psunsigned_t)context->dupcnt,
2358 (__psunsigned_t)context->flags,
2359 (__psunsigned_t)be32_to_cpu(btree->hashval),
2360 (__psunsigned_t)be32_to_cpu(btree->before),
2361 (__psunsigned_t)NULL);
2365 * Add a trace buffer entry for a context structure and a leaf block.
2367 void
2368 xfs_attr_trace_l_cl(char *where, struct xfs_attr_list_context *context,
2369 struct xfs_attr_leafblock *leaf)
2371 xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CL, where,
2372 (__psunsigned_t)context->dp,
2373 (__psunsigned_t)context->cursor->hashval,
2374 (__psunsigned_t)context->cursor->blkno,
2375 (__psunsigned_t)context->cursor->offset,
2376 (__psunsigned_t)context->alist,
2377 (__psunsigned_t)context->bufsize,
2378 (__psunsigned_t)context->count,
2379 (__psunsigned_t)context->firstu,
2380 (__psunsigned_t)
2381 ((context->count > 0) &&
2382 !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
2383 ? (ATTR_ENTRY(context->alist,
2384 context->count-1)->a_valuelen)
2385 : 0,
2386 (__psunsigned_t)context->dupcnt,
2387 (__psunsigned_t)context->flags,
2388 (__psunsigned_t)be16_to_cpu(leaf->hdr.count),
2389 (__psunsigned_t)be32_to_cpu(leaf->entries[0].hashval),
2390 (__psunsigned_t)be32_to_cpu(leaf->entries[
2391 be16_to_cpu(leaf->hdr.count)-1].hashval));
2395 * Add a trace buffer entry for the arguments given to the routine,
2396 * generic form.
2398 void
2399 xfs_attr_trace_enter(int type, char *where,
2400 __psunsigned_t a2, __psunsigned_t a3,
2401 __psunsigned_t a4, __psunsigned_t a5,
2402 __psunsigned_t a6, __psunsigned_t a7,
2403 __psunsigned_t a8, __psunsigned_t a9,
2404 __psunsigned_t a10, __psunsigned_t a11,
2405 __psunsigned_t a12, __psunsigned_t a13,
2406 __psunsigned_t a14, __psunsigned_t a15)
2408 ASSERT(xfs_attr_trace_buf);
2409 ktrace_enter(xfs_attr_trace_buf, (void *)((__psunsigned_t)type),
2410 (void *)where,
2411 (void *)a2, (void *)a3, (void *)a4,
2412 (void *)a5, (void *)a6, (void *)a7,
2413 (void *)a8, (void *)a9, (void *)a10,
2414 (void *)a11, (void *)a12, (void *)a13,
2415 (void *)a14, (void *)a15);
2417 #endif /* XFS_ATTR_TRACE */
2420 /*========================================================================
2421 * System (pseudo) namespace attribute interface routines.
2422 *========================================================================*/
2424 STATIC int
2425 posix_acl_access_set(
2426 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2428 return xfs_acl_vset(vp, data, size, _ACL_TYPE_ACCESS);
2431 STATIC int
2432 posix_acl_access_remove(
2433 bhv_vnode_t *vp, char *name, int xflags)
2435 return xfs_acl_vremove(vp, _ACL_TYPE_ACCESS);
2438 STATIC int
2439 posix_acl_access_get(
2440 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2442 return xfs_acl_vget(vp, data, size, _ACL_TYPE_ACCESS);
2445 STATIC int
2446 posix_acl_access_exists(
2447 bhv_vnode_t *vp)
2449 return xfs_acl_vhasacl_access(vp);
2452 STATIC int
2453 posix_acl_default_set(
2454 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2456 return xfs_acl_vset(vp, data, size, _ACL_TYPE_DEFAULT);
2459 STATIC int
2460 posix_acl_default_get(
2461 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2463 return xfs_acl_vget(vp, data, size, _ACL_TYPE_DEFAULT);
2466 STATIC int
2467 posix_acl_default_remove(
2468 bhv_vnode_t *vp, char *name, int xflags)
2470 return xfs_acl_vremove(vp, _ACL_TYPE_DEFAULT);
2473 STATIC int
2474 posix_acl_default_exists(
2475 bhv_vnode_t *vp)
2477 return xfs_acl_vhasacl_default(vp);
2480 STATIC struct attrnames posix_acl_access = {
2481 .attr_name = "posix_acl_access",
2482 .attr_namelen = sizeof("posix_acl_access") - 1,
2483 .attr_get = posix_acl_access_get,
2484 .attr_set = posix_acl_access_set,
2485 .attr_remove = posix_acl_access_remove,
2486 .attr_exists = posix_acl_access_exists,
2489 STATIC struct attrnames posix_acl_default = {
2490 .attr_name = "posix_acl_default",
2491 .attr_namelen = sizeof("posix_acl_default") - 1,
2492 .attr_get = posix_acl_default_get,
2493 .attr_set = posix_acl_default_set,
2494 .attr_remove = posix_acl_default_remove,
2495 .attr_exists = posix_acl_default_exists,
2498 STATIC struct attrnames *attr_system_names[] =
2499 { &posix_acl_access, &posix_acl_default };
2502 /*========================================================================
2503 * Namespace-prefix-style attribute name interface routines.
2504 *========================================================================*/
2506 STATIC int
2507 attr_generic_set(
2508 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2510 return -bhv_vop_attr_set(vp, name, data, size, xflags, NULL);
2513 STATIC int
2514 attr_generic_get(
2515 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2517 int error, asize = size;
2519 error = bhv_vop_attr_get(vp, name, data, &asize, xflags, NULL);
2520 if (!error)
2521 return asize;
2522 return -error;
2525 STATIC int
2526 attr_generic_remove(
2527 bhv_vnode_t *vp, char *name, int xflags)
2529 return -bhv_vop_attr_remove(vp, name, xflags, NULL);
2532 STATIC int
2533 attr_generic_listadd(
2534 attrnames_t *prefix,
2535 attrnames_t *namesp,
2536 void *data,
2537 size_t size,
2538 ssize_t *result)
2540 char *p = data + *result;
2542 *result += prefix->attr_namelen;
2543 *result += namesp->attr_namelen + 1;
2544 if (!size)
2545 return 0;
2546 if (*result > size)
2547 return -ERANGE;
2548 strcpy(p, prefix->attr_name);
2549 p += prefix->attr_namelen;
2550 strcpy(p, namesp->attr_name);
2551 p += namesp->attr_namelen + 1;
2552 return 0;
2555 STATIC int
2556 attr_system_list(
2557 bhv_vnode_t *vp,
2558 void *data,
2559 size_t size,
2560 ssize_t *result)
2562 attrnames_t *namesp;
2563 int i, error = 0;
2565 for (i = 0; i < ATTR_SYSCOUNT; i++) {
2566 namesp = attr_system_names[i];
2567 if (!namesp->attr_exists || !namesp->attr_exists(vp))
2568 continue;
2569 error = attr_generic_listadd(&attr_system, namesp,
2570 data, size, result);
2571 if (error)
2572 break;
2574 return error;
2578 attr_generic_list(
2579 bhv_vnode_t *vp, void *data, size_t size, int xflags, ssize_t *result)
2581 attrlist_cursor_kern_t cursor = { 0 };
2582 int error;
2584 error = bhv_vop_attr_list(vp, data, size, xflags, &cursor, NULL);
2585 if (error > 0)
2586 return -error;
2587 *result = -error;
2588 return attr_system_list(vp, data, size, result);
2591 attrnames_t *
2592 attr_lookup_namespace(
2593 char *name,
2594 struct attrnames **names,
2595 int nnames)
2597 int i;
2599 for (i = 0; i < nnames; i++)
2600 if (!strncmp(name, names[i]->attr_name, names[i]->attr_namelen))
2601 return names[i];
2602 return NULL;
2606 * Some checks to prevent people abusing EAs to get over quota:
2607 * - Don't allow modifying user EAs on devices/symlinks;
2608 * - Don't allow modifying user EAs if sticky bit set;
2610 STATIC int
2611 attr_user_capable(
2612 bhv_vnode_t *vp,
2613 cred_t *cred)
2615 struct inode *inode = vn_to_inode(vp);
2617 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
2618 return -EPERM;
2619 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode) &&
2620 !capable(CAP_SYS_ADMIN))
2621 return -EPERM;
2622 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
2623 (current_fsuid(cred) != inode->i_uid) && !capable(CAP_FOWNER))
2624 return -EPERM;
2625 return 0;
2628 STATIC int
2629 attr_trusted_capable(
2630 bhv_vnode_t *vp,
2631 cred_t *cred)
2633 struct inode *inode = vn_to_inode(vp);
2635 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
2636 return -EPERM;
2637 if (!capable(CAP_SYS_ADMIN))
2638 return -EPERM;
2639 return 0;
2642 STATIC int
2643 attr_secure_capable(
2644 bhv_vnode_t *vp,
2645 cred_t *cred)
2647 return -ENOSECURITY;
2650 STATIC int
2651 attr_system_set(
2652 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2654 attrnames_t *namesp;
2655 int error;
2657 if (xflags & ATTR_CREATE)
2658 return -EINVAL;
2660 namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
2661 if (!namesp)
2662 return -EOPNOTSUPP;
2663 error = namesp->attr_set(vp, name, data, size, xflags);
2664 if (!error)
2665 error = vn_revalidate(vp);
2666 return error;
2669 STATIC int
2670 attr_system_get(
2671 bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
2673 attrnames_t *namesp;
2675 namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
2676 if (!namesp)
2677 return -EOPNOTSUPP;
2678 return namesp->attr_get(vp, name, data, size, xflags);
2681 STATIC int
2682 attr_system_remove(
2683 bhv_vnode_t *vp, char *name, int xflags)
2685 attrnames_t *namesp;
2687 namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
2688 if (!namesp)
2689 return -EOPNOTSUPP;
2690 return namesp->attr_remove(vp, name, xflags);
2693 struct attrnames attr_system = {
2694 .attr_name = "system.",
2695 .attr_namelen = sizeof("system.") - 1,
2696 .attr_flag = ATTR_SYSTEM,
2697 .attr_get = attr_system_get,
2698 .attr_set = attr_system_set,
2699 .attr_remove = attr_system_remove,
2700 .attr_capable = (attrcapable_t)fs_noerr,
2703 struct attrnames attr_trusted = {
2704 .attr_name = "trusted.",
2705 .attr_namelen = sizeof("trusted.") - 1,
2706 .attr_flag = ATTR_ROOT,
2707 .attr_get = attr_generic_get,
2708 .attr_set = attr_generic_set,
2709 .attr_remove = attr_generic_remove,
2710 .attr_capable = attr_trusted_capable,
2713 struct attrnames attr_secure = {
2714 .attr_name = "security.",
2715 .attr_namelen = sizeof("security.") - 1,
2716 .attr_flag = ATTR_SECURE,
2717 .attr_get = attr_generic_get,
2718 .attr_set = attr_generic_set,
2719 .attr_remove = attr_generic_remove,
2720 .attr_capable = attr_secure_capable,
2723 struct attrnames attr_user = {
2724 .attr_name = "user.",
2725 .attr_namelen = sizeof("user.") - 1,
2726 .attr_get = attr_generic_get,
2727 .attr_set = attr_generic_set,
2728 .attr_remove = attr_generic_remove,
2729 .attr_capable = attr_user_capable,
2732 struct attrnames *attr_namespaces[] =
2733 { &attr_system, &attr_trusted, &attr_secure, &attr_user };