ocfs2: Make ocfs2_slot_info private.
[linux-2.6/verdex.git] / fs / ocfs2 / slot_map.c
blob762360d95e9315d7003e5ea79e406fb48ac5be94
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * slot_map.c
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
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.
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.
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.
26 #include <linux/types.h>
27 #include <linux/slab.h>
28 #include <linux/highmem.h>
30 #define MLOG_MASK_PREFIX ML_SUPER
31 #include <cluster/masklog.h>
33 #include "ocfs2.h"
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"
43 #include "buffer_head_io.h"
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];
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);
61 * Post the slot information on disk into our slot_info struct.
62 * Must be protected by osb_lock.
64 static void ocfs2_update_slot_info(struct ocfs2_slot_info *si)
66 int i;
67 __le16 *disk_info;
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;
73 for (i = 0; i < si->si_size; i++)
74 si->si_global_node_nums[i] = le16_to_cpu(disk_info[i]);
77 int ocfs2_refresh_slot_info(struct ocfs2_super *osb)
79 int ret;
80 struct ocfs2_slot_info *si = osb->slot_info;
81 struct buffer_head *bh;
83 if (si == NULL)
84 return 0;
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);
94 return ret;
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)
102 int status, i;
103 __le16 *disk_info = (__le16 *) si->si_bh->b_data;
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);
110 status = ocfs2_write_block(osb, si->si_bh, si->si_inode);
111 if (status < 0)
112 mlog_errno(status);
114 return status;
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)
122 int i;
123 s16 ret = OCFS2_INVALID_SLOT;
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;
131 return ret;
134 static s16 __ocfs2_find_empty_slot(struct ocfs2_slot_info *si,
135 s16 preferred)
137 int i;
138 s16 ret = OCFS2_INVALID_SLOT;
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;
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;
153 out:
154 return ret;
157 int ocfs2_node_num_to_slot(struct ocfs2_super *osb, unsigned int node_num)
159 s16 slot;
160 struct ocfs2_slot_info *si = osb->slot_info;
162 spin_lock(&osb->osb_lock);
163 slot = __ocfs2_node_num_to_slot(si, node_num);
164 spin_unlock(&osb->osb_lock);
166 if (slot == OCFS2_INVALID_SLOT)
167 return -ENOENT;
169 return slot;
172 int ocfs2_slot_to_node_num_locked(struct ocfs2_super *osb, int slot_num,
173 unsigned int *node_num)
175 struct ocfs2_slot_info *si = osb->slot_info;
177 assert_spin_locked(&osb->osb_lock);
179 BUG_ON(slot_num < 0);
180 BUG_ON(slot_num > osb->max_slots);
182 if (si->si_global_node_nums[slot_num] == OCFS2_INVALID_SLOT)
183 return -ENOENT;
185 *node_num = si->si_global_node_nums[slot_num];
186 return 0;
189 static void __ocfs2_free_slot_info(struct ocfs2_slot_info *si)
191 if (si == NULL)
192 return;
194 if (si->si_inode)
195 iput(si->si_inode);
196 if (si->si_bh)
197 brelse(si->si_bh);
199 kfree(si);
202 static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
203 s16 slot_num,
204 s16 node_num)
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));
211 si->si_global_node_nums[slot_num] = node_num;
214 int ocfs2_clear_slot(struct ocfs2_super *osb, s16 slot_num)
216 struct ocfs2_slot_info *si = osb->slot_info;
218 if (si == NULL)
219 return 0;
221 spin_lock(&osb->osb_lock);
222 __ocfs2_fill_slot(si, slot_num, OCFS2_INVALID_SLOT);
223 spin_unlock(&osb->osb_lock);
225 return ocfs2_update_disk_slots(osb, osb->slot_info);
228 int ocfs2_init_slot_info(struct ocfs2_super *osb)
230 int status, i;
231 u64 blkno;
232 struct inode *inode = NULL;
233 struct buffer_head *bh = NULL;
234 struct ocfs2_slot_info *si;
236 si = kzalloc(sizeof(struct ocfs2_slot_info), GFP_KERNEL);
237 if (!si) {
238 status = -ENOMEM;
239 mlog_errno(status);
240 goto bail;
243 si->si_num_slots = osb->max_slots;
244 si->si_size = OCFS2_MAX_SLOTS;
246 for(i = 0; i < si->si_num_slots; i++)
247 si->si_global_node_nums[i] = OCFS2_INVALID_SLOT;
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;
257 status = ocfs2_extent_map_get_blocks(inode, 0ULL, &blkno, NULL, NULL);
258 if (status < 0) {
259 mlog_errno(status);
260 goto bail;
263 status = ocfs2_read_block(osb, blkno, &bh, 0, inode);
264 if (status < 0) {
265 mlog_errno(status);
266 goto bail;
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);
276 return status;
279 void ocfs2_free_slot_info(struct ocfs2_super *osb)
281 struct ocfs2_slot_info *si = osb->slot_info;
283 osb->slot_info = NULL;
284 __ocfs2_free_slot_info(si);
287 int ocfs2_find_slot(struct ocfs2_super *osb)
289 int status;
290 s16 slot;
291 struct ocfs2_slot_info *si;
293 mlog_entry_void();
295 si = osb->slot_info;
297 spin_lock(&osb->osb_lock);
298 ocfs2_update_slot_info(si);
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;
315 } else
316 mlog(ML_NOTICE, "slot %d is already allocated to this node!\n",
317 slot);
319 __ocfs2_fill_slot(si, slot, osb->node_num);
320 osb->slot_num = slot;
321 spin_unlock(&osb->osb_lock);
323 mlog(0, "taking node slot %d\n", osb->slot_num);
325 status = ocfs2_update_disk_slots(osb, si);
326 if (status < 0)
327 mlog_errno(status);
329 bail:
330 mlog_exit(status);
331 return status;
334 void ocfs2_put_slot(struct ocfs2_super *osb)
336 int status;
337 struct ocfs2_slot_info *si = osb->slot_info;
339 if (!si)
340 return;
342 spin_lock(&osb->osb_lock);
343 ocfs2_update_slot_info(si);
345 __ocfs2_fill_slot(si, osb->slot_num, OCFS2_INVALID_SLOT);
346 osb->slot_num = OCFS2_INVALID_SLOT;
347 spin_unlock(&osb->osb_lock);
349 status = ocfs2_update_disk_slots(osb, si);
350 if (status < 0) {
351 mlog_errno(status);
352 goto bail;
355 bail:
356 ocfs2_free_slot_info(osb);