2 * Copyright (c) 2000-2003,2005 Silicon Graphics, Inc.
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
21 * Source file used to associate/disassociate behaviors with virtualized
22 * objects. See xfs_behavior.h for more information about behaviors, etc.
24 * The implementation is split between functions in this file and macros
29 * Insert a new behavior descriptor into a behavior chain.
31 * The behavior chain is ordered based on the 'position' number which
32 * lives in the first field of the ops vector (higher numbers first).
34 * Attempts to insert duplicate ops result in an EINVAL return code.
35 * Otherwise, return 0 to indicate success.
38 bhv_insert(bhv_head_t
*bhp
, bhv_desc_t
*bdp
)
40 bhv_desc_t
*curdesc
, *prev
;
44 * Validate the position value of the new behavior.
46 position
= BHV_POSITION(bdp
);
47 ASSERT(position
>= BHV_POSITION_BASE
&& position
<= BHV_POSITION_TOP
);
50 * Find location to insert behavior. Check for duplicates.
53 for (curdesc
= bhp
->bh_first
;
55 curdesc
= curdesc
->bd_next
) {
57 /* Check for duplication. */
58 if (curdesc
->bd_ops
== bdp
->bd_ops
) {
63 /* Find correct position */
64 if (position
>= BHV_POSITION(curdesc
)) {
65 ASSERT(position
!= BHV_POSITION(curdesc
));
73 /* insert at front of chain */
74 bdp
->bd_next
= bhp
->bh_first
;
77 /* insert after prev */
78 bdp
->bd_next
= prev
->bd_next
;
86 * Remove a behavior descriptor from a position in a behavior chain;
87 * the position is guaranteed not to be the first position.
88 * Should only be called by the bhv_remove() macro.
91 bhv_remove_not_first(bhv_head_t
*bhp
, bhv_desc_t
*bdp
)
93 bhv_desc_t
*curdesc
, *prev
;
95 ASSERT(bhp
->bh_first
!= NULL
);
96 ASSERT(bhp
->bh_first
->bd_next
!= NULL
);
99 for (curdesc
= bhp
->bh_first
->bd_next
;
101 curdesc
= curdesc
->bd_next
) {
104 break; /* found it */
108 ASSERT(curdesc
== bdp
);
109 prev
->bd_next
= bdp
->bd_next
; /* remove from after prev */
113 * Looks for the first behavior within a specified range of positions.
114 * Return the associated behavior descriptor. Or NULL, if none found.
117 bhv_lookup_range(bhv_head_t
*bhp
, int low
, int high
)
121 for (curdesc
= bhp
->bh_first
;
123 curdesc
= curdesc
->bd_next
) {
125 int position
= BHV_POSITION(curdesc
);
127 if (position
<= high
) {
138 * Return the base behavior in the chain, or NULL if the chain
141 * The caller has not read locked the behavior chain, so acquire the
142 * lock before traversing the chain.
145 bhv_base(bhv_head_t
*bhp
)
149 for (curdesc
= bhp
->bh_first
;
151 curdesc
= curdesc
->bd_next
) {
153 if (curdesc
->bd_next
== NULL
) {
166 bhp
->bh_first
= NULL
;
174 ASSERT(bhp
->bh_first
== NULL
);
175 (bhp
)->bh_first
= bdp
;
182 ASSERT(bhp
->bh_first
== NULL
);