1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Data structure and helper functions for tracking pending FSF
8 * Copyright IBM Corp. 2009, 2016
11 #ifndef ZFCP_REQLIST_H
12 #define ZFCP_REQLIST_H
14 /* number of hash buckets */
15 #define ZFCP_REQ_LIST_BUCKETS 128
18 * struct zfcp_reqlist - Container for request list (reqlist)
19 * @lock: Spinlock for protecting the hash list
20 * @list: Array of hashbuckets, each is a list of requests in this bucket
24 struct list_head buckets
[ZFCP_REQ_LIST_BUCKETS
];
27 static inline int zfcp_reqlist_hash(unsigned long req_id
)
29 return req_id
% ZFCP_REQ_LIST_BUCKETS
;
33 * zfcp_reqlist_alloc - Allocate and initialize reqlist
35 * Returns pointer to allocated reqlist on success, or NULL on
38 static inline struct zfcp_reqlist
*zfcp_reqlist_alloc(void)
41 struct zfcp_reqlist
*rl
;
43 rl
= kzalloc(sizeof(struct zfcp_reqlist
), GFP_KERNEL
);
47 spin_lock_init(&rl
->lock
);
49 for (i
= 0; i
< ZFCP_REQ_LIST_BUCKETS
; i
++)
50 INIT_LIST_HEAD(&rl
->buckets
[i
]);
56 * zfcp_reqlist_isempty - Check whether the request list empty
57 * @rl: pointer to reqlist
59 * Returns: 1 if list is empty, 0 if not
61 static inline int zfcp_reqlist_isempty(struct zfcp_reqlist
*rl
)
65 for (i
= 0; i
< ZFCP_REQ_LIST_BUCKETS
; i
++)
66 if (!list_empty(&rl
->buckets
[i
]))
72 * zfcp_reqlist_free - Free allocated memory for reqlist
73 * @rl: The reqlist where to free memory
75 static inline void zfcp_reqlist_free(struct zfcp_reqlist
*rl
)
78 BUG_ON(!zfcp_reqlist_isempty(rl
));
83 static inline struct zfcp_fsf_req
*
84 _zfcp_reqlist_find(struct zfcp_reqlist
*rl
, unsigned long req_id
)
86 struct zfcp_fsf_req
*req
;
89 i
= zfcp_reqlist_hash(req_id
);
90 list_for_each_entry(req
, &rl
->buckets
[i
], list
)
91 if (req
->req_id
== req_id
)
97 * zfcp_reqlist_find - Lookup FSF request by its request id
98 * @rl: The reqlist where to lookup the FSF request
99 * @req_id: The request id to look for
101 * Returns a pointer to the FSF request with the specified request id
102 * or NULL if there is no known FSF request with this id.
104 static inline struct zfcp_fsf_req
*
105 zfcp_reqlist_find(struct zfcp_reqlist
*rl
, unsigned long req_id
)
108 struct zfcp_fsf_req
*req
;
110 spin_lock_irqsave(&rl
->lock
, flags
);
111 req
= _zfcp_reqlist_find(rl
, req_id
);
112 spin_unlock_irqrestore(&rl
->lock
, flags
);
118 * zfcp_reqlist_find_rm - Lookup request by id and remove it from reqlist
119 * @rl: reqlist where to search and remove entry
120 * @req_id: The request id of the request to look for
122 * This functions tries to find the FSF request with the specified
123 * id and then removes it from the reqlist. The reqlist lock is held
124 * during both steps of the operation.
126 * Returns: Pointer to the FSF request if the request has been found,
127 * NULL if it has not been found.
129 static inline struct zfcp_fsf_req
*
130 zfcp_reqlist_find_rm(struct zfcp_reqlist
*rl
, unsigned long req_id
)
133 struct zfcp_fsf_req
*req
;
135 spin_lock_irqsave(&rl
->lock
, flags
);
136 req
= _zfcp_reqlist_find(rl
, req_id
);
138 list_del(&req
->list
);
139 spin_unlock_irqrestore(&rl
->lock
, flags
);
145 * zfcp_reqlist_add - Add entry to reqlist
146 * @rl: reqlist where to add the entry
147 * @req: The entry to add
149 * The request id always increases. As an optimization new requests
150 * are added here with list_add_tail at the end of the bucket lists
151 * while old requests are looked up starting at the beginning of the
154 static inline void zfcp_reqlist_add(struct zfcp_reqlist
*rl
,
155 struct zfcp_fsf_req
*req
)
160 i
= zfcp_reqlist_hash(req
->req_id
);
162 spin_lock_irqsave(&rl
->lock
, flags
);
163 list_add_tail(&req
->list
, &rl
->buckets
[i
]);
164 spin_unlock_irqrestore(&rl
->lock
, flags
);
168 * zfcp_reqlist_move - Move all entries from reqlist to simple list
169 * @rl: The zfcp_reqlist where to remove all entries
170 * @list: The list where to move all entries
172 static inline void zfcp_reqlist_move(struct zfcp_reqlist
*rl
,
173 struct list_head
*list
)
178 spin_lock_irqsave(&rl
->lock
, flags
);
179 for (i
= 0; i
< ZFCP_REQ_LIST_BUCKETS
; i
++)
180 list_splice_init(&rl
->buckets
[i
], list
);
181 spin_unlock_irqrestore(&rl
->lock
, flags
);
185 * zfcp_reqlist_apply_for_all() - apply a function to every request.
186 * @rl: the requestlist that contains the target requests.
187 * @f: the function to apply to each request; the first parameter of the
188 * function will be the target-request; the second parameter is the same
189 * pointer as given with the argument @data.
190 * @data: freely chosen argument; passed through to @f as second parameter.
192 * Uses :c:macro:`list_for_each_entry` to iterate over the lists in the hash-
193 * table (not a 'safe' variant, so don't modify the list).
195 * Holds @rl->lock over the entire request-iteration.
198 zfcp_reqlist_apply_for_all(struct zfcp_reqlist
*rl
,
199 void (*f
)(struct zfcp_fsf_req
*, void *), void *data
)
201 struct zfcp_fsf_req
*req
;
205 spin_lock_irqsave(&rl
->lock
, flags
);
206 for (i
= 0; i
< ZFCP_REQ_LIST_BUCKETS
; i
++)
207 list_for_each_entry(req
, &rl
->buckets
[i
], list
)
209 spin_unlock_irqrestore(&rl
->lock
, flags
);
212 #endif /* ZFCP_REQLIST_H */