]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/ocfs2/slot_map.c
ocfs2: Make ocfs2_slot_info private.
[linux-2.6-omap-h63xx.git] / fs / ocfs2 / slot_map.c
1 /* -*- mode: c; c-basic-offset: 8; -*-
2  * vim: noexpandtab sw=8 ts=8 sts=0:
3  *
4  * slot_map.c
5  *
6  *
7  *
8  * Copyright (C) 2002, 2004 Oracle.  All rights reserved.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this program; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 021110-1307, USA.
24  */
25
26 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/highmem.h>
29
30 #define MLOG_MASK_PREFIX ML_SUPER
31 #include <cluster/masklog.h>
32
33 #include "ocfs2.h"
34
35 #include "dlmglue.h"
36 #include "extent_map.h"
37 #include "heartbeat.h"
38 #include "inode.h"
39 #include "slot_map.h"
40 #include "super.h"
41 #include "sysfile.h"
42
43 #include "buffer_head_io.h"
44
45 struct ocfs2_slot_info {
46         struct inode *si_inode;
47         struct buffer_head *si_bh;
48         unsigned int si_num_slots;
49         unsigned int si_size;
50         s16 si_global_node_nums[OCFS2_MAX_SLOTS];
51 };
52
53
54 static s16 __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
55                                     s16 global);
56 static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
57                               s16 slot_num,
58                               s16 node_num);
59
60 /*
61  * Post the slot information on disk into our slot_info struct.
62  * Must be protected by osb_lock.
63  */
64 static void ocfs2_update_slot_info(struct ocfs2_slot_info *si)
65 {
66         int i;
67         __le16 *disk_info;
68
69         /* we don't read the slot block here as ocfs2_super_lock
70          * should've made sure we have the most recent copy. */
71         disk_info = (__le16 *) si->si_bh->b_data;
72
73         for (i = 0; i < si->si_size; i++)
74                 si->si_global_node_nums[i] = le16_to_cpu(disk_info[i]);
75 }
76
77 int ocfs2_refresh_slot_info(struct ocfs2_super *osb)
78 {
79         int ret;
80         struct ocfs2_slot_info *si = osb->slot_info;
81         struct buffer_head *bh;
82
83         if (si == NULL)
84                 return 0;
85
86         bh = si->si_bh;
87         ret = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0, si->si_inode);
88         if (ret == 0) {
89                 spin_lock(&osb->osb_lock);
90                 ocfs2_update_slot_info(si);
91                 spin_unlock(&osb->osb_lock);
92         }
93
94         return ret;
95 }
96
97 /* post the our slot info stuff into it's destination bh and write it
98  * out. */
99 static int ocfs2_update_disk_slots(struct ocfs2_super *osb,
100                                    struct ocfs2_slot_info *si)
101 {
102         int status, i;
103         __le16 *disk_info = (__le16 *) si->si_bh->b_data;
104
105         spin_lock(&osb->osb_lock);
106         for (i = 0; i < si->si_size; i++)
107                 disk_info[i] = cpu_to_le16(si->si_global_node_nums[i]);
108         spin_unlock(&osb->osb_lock);
109
110         status = ocfs2_write_block(osb, si->si_bh, si->si_inode);
111         if (status < 0)
112                 mlog_errno(status);
113
114         return status;
115 }
116
117 /* try to find global node in the slot info. Returns
118  * OCFS2_INVALID_SLOT if nothing is found. */
119 static s16 __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
120                                     s16 global)
121 {
122         int i;
123         s16 ret = OCFS2_INVALID_SLOT;
124
125         for(i = 0; i < si->si_num_slots; i++) {
126                 if (global == si->si_global_node_nums[i]) {
127                         ret = (s16) i;
128                         break;
129                 }
130         }
131         return ret;
132 }
133
134 static s16 __ocfs2_find_empty_slot(struct ocfs2_slot_info *si,
135                                    s16 preferred)
136 {
137         int i;
138         s16 ret = OCFS2_INVALID_SLOT;
139
140         if (preferred >= 0 && preferred < si->si_num_slots) {
141                 if (OCFS2_INVALID_SLOT == si->si_global_node_nums[preferred]) {
142                         ret = preferred;
143                         goto out;
144                 }
145         }
146
147         for(i = 0; i < si->si_num_slots; i++) {
148                 if (OCFS2_INVALID_SLOT == si->si_global_node_nums[i]) {
149                         ret = (s16) i;
150                         break;
151                 }
152         }
153 out:
154         return ret;
155 }
156
157 int ocfs2_node_num_to_slot(struct ocfs2_super *osb, unsigned int node_num)
158 {
159         s16 slot;
160         struct ocfs2_slot_info *si = osb->slot_info;
161
162         spin_lock(&osb->osb_lock);
163         slot = __ocfs2_node_num_to_slot(si, node_num);
164         spin_unlock(&osb->osb_lock);
165
166         if (slot == OCFS2_INVALID_SLOT)
167                 return -ENOENT;
168
169         return slot;
170 }
171
172 int ocfs2_slot_to_node_num_locked(struct ocfs2_super *osb, int slot_num,
173                                   unsigned int *node_num)
174 {
175         struct ocfs2_slot_info *si = osb->slot_info;
176
177         assert_spin_locked(&osb->osb_lock);
178
179         BUG_ON(slot_num < 0);
180         BUG_ON(slot_num > osb->max_slots);
181
182         if (si->si_global_node_nums[slot_num] == OCFS2_INVALID_SLOT)
183                 return -ENOENT;
184
185         *node_num = si->si_global_node_nums[slot_num];
186         return 0;
187 }
188
189 static void __ocfs2_free_slot_info(struct ocfs2_slot_info *si)
190 {
191         if (si == NULL)
192                 return;
193
194         if (si->si_inode)
195                 iput(si->si_inode);
196         if (si->si_bh)
197                 brelse(si->si_bh);
198
199         kfree(si);
200 }
201
202 static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
203                               s16 slot_num,
204                               s16 node_num)
205 {
206         BUG_ON(slot_num == OCFS2_INVALID_SLOT);
207         BUG_ON(slot_num >= si->si_num_slots);
208         BUG_ON((node_num != O2NM_INVALID_NODE_NUM) &&
209                (node_num >= O2NM_MAX_NODES));
210
211         si->si_global_node_nums[slot_num] = node_num;
212 }
213
214 int ocfs2_clear_slot(struct ocfs2_super *osb, s16 slot_num)
215 {
216         struct ocfs2_slot_info *si = osb->slot_info;
217
218         if (si == NULL)
219                 return 0;
220
221         spin_lock(&osb->osb_lock);
222         __ocfs2_fill_slot(si, slot_num, OCFS2_INVALID_SLOT);
223         spin_unlock(&osb->osb_lock);
224
225         return ocfs2_update_disk_slots(osb, osb->slot_info);
226 }
227
228 int ocfs2_init_slot_info(struct ocfs2_super *osb)
229 {
230         int status, i;
231         u64 blkno;
232         struct inode *inode = NULL;
233         struct buffer_head *bh = NULL;
234         struct ocfs2_slot_info *si;
235
236         si = kzalloc(sizeof(struct ocfs2_slot_info), GFP_KERNEL);
237         if (!si) {
238                 status = -ENOMEM;
239                 mlog_errno(status);
240                 goto bail;
241         }
242
243         si->si_num_slots = osb->max_slots;
244         si->si_size = OCFS2_MAX_SLOTS;
245
246         for(i = 0; i < si->si_num_slots; i++)
247                 si->si_global_node_nums[i] = OCFS2_INVALID_SLOT;
248
249         inode = ocfs2_get_system_file_inode(osb, SLOT_MAP_SYSTEM_INODE,
250                                             OCFS2_INVALID_SLOT);
251         if (!inode) {
252                 status = -EINVAL;
253                 mlog_errno(status);
254                 goto bail;
255         }
256
257         status = ocfs2_extent_map_get_blocks(inode, 0ULL, &blkno, NULL, NULL);
258         if (status < 0) {
259                 mlog_errno(status);
260                 goto bail;
261         }
262
263         status = ocfs2_read_block(osb, blkno, &bh, 0, inode);
264         if (status < 0) {
265                 mlog_errno(status);
266                 goto bail;
267         }
268
269         si->si_inode = inode;
270         si->si_bh = bh;
271         osb->slot_info = (struct ocfs2_slot_info *)si;
272 bail:
273         if (status < 0 && si)
274                 __ocfs2_free_slot_info(si);
275
276         return status;
277 }
278
279 void ocfs2_free_slot_info(struct ocfs2_super *osb)
280 {
281         struct ocfs2_slot_info *si = osb->slot_info;
282
283         osb->slot_info = NULL;
284         __ocfs2_free_slot_info(si);
285 }
286
287 int ocfs2_find_slot(struct ocfs2_super *osb)
288 {
289         int status;
290         s16 slot;
291         struct ocfs2_slot_info *si;
292
293         mlog_entry_void();
294
295         si = osb->slot_info;
296
297         spin_lock(&osb->osb_lock);
298         ocfs2_update_slot_info(si);
299
300         /* search for ourselves first and take the slot if it already
301          * exists. Perhaps we need to mark this in a variable for our
302          * own journal recovery? Possibly not, though we certainly
303          * need to warn to the user */
304         slot = __ocfs2_node_num_to_slot(si, osb->node_num);
305         if (slot == OCFS2_INVALID_SLOT) {
306                 /* if no slot yet, then just take 1st available
307                  * one. */
308                 slot = __ocfs2_find_empty_slot(si, osb->preferred_slot);
309                 if (slot == OCFS2_INVALID_SLOT) {
310                         spin_unlock(&osb->osb_lock);
311                         mlog(ML_ERROR, "no free slots available!\n");
312                         status = -EINVAL;
313                         goto bail;
314                 }
315         } else
316                 mlog(ML_NOTICE, "slot %d is already allocated to this node!\n",
317                      slot);
318
319         __ocfs2_fill_slot(si, slot, osb->node_num);
320         osb->slot_num = slot;
321         spin_unlock(&osb->osb_lock);
322
323         mlog(0, "taking node slot %d\n", osb->slot_num);
324
325         status = ocfs2_update_disk_slots(osb, si);
326         if (status < 0)
327                 mlog_errno(status);
328
329 bail:
330         mlog_exit(status);
331         return status;
332 }
333
334 void ocfs2_put_slot(struct ocfs2_super *osb)
335 {
336         int status;
337         struct ocfs2_slot_info *si = osb->slot_info;
338
339         if (!si)
340                 return;
341
342         spin_lock(&osb->osb_lock);
343         ocfs2_update_slot_info(si);
344
345         __ocfs2_fill_slot(si, osb->slot_num, OCFS2_INVALID_SLOT);
346         osb->slot_num = OCFS2_INVALID_SLOT;
347         spin_unlock(&osb->osb_lock);
348
349         status = ocfs2_update_disk_slots(osb, si);
350         if (status < 0) {
351                 mlog_errno(status);
352                 goto bail;
353         }
354
355 bail:
356         ocfs2_free_slot_info(osb);
357 }
358