2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
36 * Source file used to associate/disassociate behaviors with virtualized
37 * objects. See xfs_behavior.h for more information about behaviors, etc.
39 * The implementation is split between functions in this file and macros
44 * Insert a new behavior descriptor into a behavior chain.
46 * The behavior chain is ordered based on the 'position' number which
47 * lives in the first field of the ops vector (higher numbers first).
49 * Attemps to insert duplicate ops result in an EINVAL return code.
50 * Otherwise, return 0 to indicate success.
53 bhv_insert(bhv_head_t *bhp, bhv_desc_t *bdp)
55 bhv_desc_t *curdesc, *prev;
59 * Validate the position value of the new behavior.
61 position = BHV_POSITION(bdp);
62 ASSERT(position >= BHV_POSITION_BASE && position <= BHV_POSITION_TOP);
65 * Find location to insert behavior. Check for duplicates.
68 for (curdesc = bhp->bh_first;
70 curdesc = curdesc->bd_next) {
72 /* Check for duplication. */
73 if (curdesc->bd_ops == bdp->bd_ops) {
78 /* Find correct position */
79 if (position >= BHV_POSITION(curdesc)) {
80 ASSERT(position != BHV_POSITION(curdesc));
88 /* insert at front of chain */
89 bdp->bd_next = bhp->bh_first;
92 /* insert after prev */
93 bdp->bd_next = prev->bd_next;
101 * Remove a behavior descriptor from a position in a behavior chain;
102 * the postition is guaranteed not to be the first position.
103 * Should only be called by the bhv_remove() macro.
106 bhv_remove_not_first(bhv_head_t *bhp, bhv_desc_t *bdp)
108 bhv_desc_t *curdesc, *prev;
110 ASSERT(bhp->bh_first != NULL);
111 ASSERT(bhp->bh_first->bd_next != NULL);
113 prev = bhp->bh_first;
114 for (curdesc = bhp->bh_first->bd_next;
116 curdesc = curdesc->bd_next) {
119 break; /* found it */
123 ASSERT(curdesc == bdp);
124 prev->bd_next = bdp->bd_next; /* remove from after prev */
128 * Look for a specific ops vector on the specified behavior chain.
129 * Return the associated behavior descriptor. Or NULL, if not found.
132 bhv_lookup(bhv_head_t *bhp, void *ops)
136 for (curdesc = bhp->bh_first;
138 curdesc = curdesc->bd_next) {
140 if (curdesc->bd_ops == ops)
148 * Looks for the first behavior within a specified range of positions.
149 * Return the associated behavior descriptor. Or NULL, if none found.
152 bhv_lookup_range(bhv_head_t *bhp, int low, int high)
156 for (curdesc = bhp->bh_first;
158 curdesc = curdesc->bd_next) {
160 int position = BHV_POSITION(curdesc);
162 if (position <= high) {
173 * Return the base behavior in the chain, or NULL if the chain
176 * The caller has not read locked the behavior chain, so acquire the
177 * lock before traversing the chain.
180 bhv_base(bhv_head_t *bhp)
184 for (curdesc = bhp->bh_first;
186 curdesc = curdesc->bd_next) {
188 if (curdesc->bd_next == NULL) {
201 bhp->bh_first = NULL;
209 ASSERT(bhp->bh_first == NULL);
210 (bhp)->bh_first = bdp;
217 ASSERT(bhp->bh_first == NULL);