Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
zfcp_reqlist.h
Go to the documentation of this file.
1 /*
2  * zfcp device driver
3  *
4  * Data structure and helper functions for tracking pending FSF
5  * requests.
6  *
7  * Copyright IBM Corp. 2009
8  */
9 
10 #ifndef ZFCP_REQLIST_H
11 #define ZFCP_REQLIST_H
12 
13 /* number of hash buckets */
14 #define ZFCP_REQ_LIST_BUCKETS 128
15 
21 struct zfcp_reqlist {
24 };
25 
26 static inline int zfcp_reqlist_hash(unsigned long req_id)
27 {
28  return req_id % ZFCP_REQ_LIST_BUCKETS;
29 }
30 
37 static inline struct zfcp_reqlist *zfcp_reqlist_alloc(void)
38 {
39  unsigned int i;
40  struct zfcp_reqlist *rl;
41 
42  rl = kzalloc(sizeof(struct zfcp_reqlist), GFP_KERNEL);
43  if (!rl)
44  return NULL;
45 
46  spin_lock_init(&rl->lock);
47 
48  for (i = 0; i < ZFCP_REQ_LIST_BUCKETS; i++)
49  INIT_LIST_HEAD(&rl->buckets[i]);
50 
51  return rl;
52 }
53 
60 static inline int zfcp_reqlist_isempty(struct zfcp_reqlist *rl)
61 {
62  unsigned int i;
63 
64  for (i = 0; i < ZFCP_REQ_LIST_BUCKETS; i++)
65  if (!list_empty(&rl->buckets[i]))
66  return 0;
67  return 1;
68 }
69 
74 static inline void zfcp_reqlist_free(struct zfcp_reqlist *rl)
75 {
76  /* sanity check */
77  BUG_ON(!zfcp_reqlist_isempty(rl));
78 
79  kfree(rl);
80 }
81 
82 static inline struct zfcp_fsf_req *
83 _zfcp_reqlist_find(struct zfcp_reqlist *rl, unsigned long req_id)
84 {
85  struct zfcp_fsf_req *req;
86  unsigned int i;
87 
88  i = zfcp_reqlist_hash(req_id);
89  list_for_each_entry(req, &rl->buckets[i], list)
90  if (req->req_id == req_id)
91  return req;
92  return NULL;
93 }
94 
103 static inline struct zfcp_fsf_req *
104 zfcp_reqlist_find(struct zfcp_reqlist *rl, unsigned long req_id)
105 {
106  unsigned long flags;
107  struct zfcp_fsf_req *req;
108 
109  spin_lock_irqsave(&rl->lock, flags);
110  req = _zfcp_reqlist_find(rl, req_id);
111  spin_unlock_irqrestore(&rl->lock, flags);
112 
113  return req;
114 }
115 
128 static inline struct zfcp_fsf_req *
129 zfcp_reqlist_find_rm(struct zfcp_reqlist *rl, unsigned long req_id)
130 {
131  unsigned long flags;
132  struct zfcp_fsf_req *req;
133 
134  spin_lock_irqsave(&rl->lock, flags);
135  req = _zfcp_reqlist_find(rl, req_id);
136  if (req)
137  list_del(&req->list);
138  spin_unlock_irqrestore(&rl->lock, flags);
139 
140  return req;
141 }
142 
153 static inline void zfcp_reqlist_add(struct zfcp_reqlist *rl,
154  struct zfcp_fsf_req *req)
155 {
156  unsigned int i;
157  unsigned long flags;
158 
159  i = zfcp_reqlist_hash(req->req_id);
160 
161  spin_lock_irqsave(&rl->lock, flags);
162  list_add_tail(&req->list, &rl->buckets[i]);
163  spin_unlock_irqrestore(&rl->lock, flags);
164 }
165 
171 static inline void zfcp_reqlist_move(struct zfcp_reqlist *rl,
172  struct list_head *list)
173 {
174  unsigned int i;
175  unsigned long flags;
176 
177  spin_lock_irqsave(&rl->lock, flags);
178  for (i = 0; i < ZFCP_REQ_LIST_BUCKETS; i++)
179  list_splice_init(&rl->buckets[i], list);
180  spin_unlock_irqrestore(&rl->lock, flags);
181 }
182 
183 #endif /* ZFCP_REQLIST_H */