1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
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>
36 #include "extent_map.h"
37 #include "heartbeat.h"
43 #include "buffer_head_io.h"
45 static s16 __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
47 static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
51 /* post the slot information on disk into our slot_info struct. */
52 static void ocfs2_update_slot_info(struct ocfs2_slot_info *si)
57 /* we don't read the slot block here as ocfs2_super_lock
58 * should've made sure we have the most recent copy. */
59 spin_lock(&si->si_lock);
60 disk_info = (__le16 *) si->si_bh->b_data;
62 for (i = 0; i < si->si_size; i++)
63 si->si_global_node_nums[i] = le16_to_cpu(disk_info[i]);
65 spin_unlock(&si->si_lock);
68 int ocfs2_refresh_slot_info(struct ocfs2_super *osb)
71 struct ocfs2_slot_info *si = osb->slot_info;
72 struct buffer_head *bh;
78 ret = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0, si->si_inode);
80 ocfs2_update_slot_info(si);
85 /* post the our slot info stuff into it's destination bh and write it
87 static int ocfs2_update_disk_slots(struct ocfs2_super *osb,
88 struct ocfs2_slot_info *si)
91 __le16 *disk_info = (__le16 *) si->si_bh->b_data;
93 spin_lock(&si->si_lock);
94 for (i = 0; i < si->si_size; i++)
95 disk_info[i] = cpu_to_le16(si->si_global_node_nums[i]);
96 spin_unlock(&si->si_lock);
98 status = ocfs2_write_block(osb, si->si_bh, si->si_inode);
105 /* try to find global node in the slot info. Returns
106 * OCFS2_INVALID_SLOT if nothing is found. */
107 static s16 __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
111 s16 ret = OCFS2_INVALID_SLOT;
113 for(i = 0; i < si->si_num_slots; i++) {
114 if (global == si->si_global_node_nums[i]) {
122 static s16 __ocfs2_find_empty_slot(struct ocfs2_slot_info *si, s16 preferred)
125 s16 ret = OCFS2_INVALID_SLOT;
127 if (preferred >= 0 && preferred < si->si_num_slots) {
128 if (OCFS2_INVALID_SLOT == si->si_global_node_nums[preferred]) {
134 for(i = 0; i < si->si_num_slots; i++) {
135 if (OCFS2_INVALID_SLOT == si->si_global_node_nums[i]) {
144 s16 ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
149 spin_lock(&si->si_lock);
150 ret = __ocfs2_node_num_to_slot(si, global);
151 spin_unlock(&si->si_lock);
155 static void __ocfs2_free_slot_info(struct ocfs2_slot_info *si)
168 static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
172 BUG_ON(slot_num == OCFS2_INVALID_SLOT);
173 BUG_ON(slot_num >= si->si_num_slots);
174 BUG_ON((node_num != O2NM_INVALID_NODE_NUM) &&
175 (node_num >= O2NM_MAX_NODES));
177 si->si_global_node_nums[slot_num] = node_num;
180 int ocfs2_clear_slot(struct ocfs2_super *osb, s16 slot_num)
182 struct ocfs2_slot_info *si = osb->slot_info;
187 spin_lock(&si->si_lock);
188 __ocfs2_fill_slot(si, slot_num, OCFS2_INVALID_SLOT);
189 spin_unlock(&si->si_lock);
191 return ocfs2_update_disk_slots(osb, osb->slot_info);
194 int ocfs2_init_slot_info(struct ocfs2_super *osb)
198 struct inode *inode = NULL;
199 struct buffer_head *bh = NULL;
200 struct ocfs2_slot_info *si;
202 si = kzalloc(sizeof(struct ocfs2_slot_info), GFP_KERNEL);
209 spin_lock_init(&si->si_lock);
210 si->si_num_slots = osb->max_slots;
211 si->si_size = OCFS2_MAX_SLOTS;
213 for(i = 0; i < si->si_num_slots; i++)
214 si->si_global_node_nums[i] = OCFS2_INVALID_SLOT;
216 inode = ocfs2_get_system_file_inode(osb, SLOT_MAP_SYSTEM_INODE,
224 status = ocfs2_extent_map_get_blocks(inode, 0ULL, &blkno, NULL, NULL);
230 status = ocfs2_read_block(osb, blkno, &bh, 0, inode);
236 si->si_inode = inode;
240 if (status < 0 && si)
241 __ocfs2_free_slot_info(si);
246 void ocfs2_free_slot_info(struct ocfs2_super *osb)
248 struct ocfs2_slot_info *si = osb->slot_info;
250 osb->slot_info = NULL;
251 __ocfs2_free_slot_info(si);
254 int ocfs2_find_slot(struct ocfs2_super *osb)
258 struct ocfs2_slot_info *si;
264 ocfs2_update_slot_info(si);
266 spin_lock(&si->si_lock);
267 /* search for ourselves first and take the slot if it already
268 * exists. Perhaps we need to mark this in a variable for our
269 * own journal recovery? Possibly not, though we certainly
270 * need to warn to the user */
271 slot = __ocfs2_node_num_to_slot(si, osb->node_num);
272 if (slot == OCFS2_INVALID_SLOT) {
273 /* if no slot yet, then just take 1st available
275 slot = __ocfs2_find_empty_slot(si, osb->preferred_slot);
276 if (slot == OCFS2_INVALID_SLOT) {
277 spin_unlock(&si->si_lock);
278 mlog(ML_ERROR, "no free slots available!\n");
283 mlog(ML_NOTICE, "slot %d is already allocated to this node!\n",
286 __ocfs2_fill_slot(si, slot, osb->node_num);
287 osb->slot_num = slot;
288 spin_unlock(&si->si_lock);
290 mlog(0, "taking node slot %d\n", osb->slot_num);
292 status = ocfs2_update_disk_slots(osb, si);
301 void ocfs2_put_slot(struct ocfs2_super *osb)
304 struct ocfs2_slot_info *si = osb->slot_info;
309 ocfs2_update_slot_info(si);
311 spin_lock(&si->si_lock);
312 __ocfs2_fill_slot(si, osb->slot_num, OCFS2_INVALID_SLOT);
313 osb->slot_num = OCFS2_INVALID_SLOT;
314 spin_unlock(&si->si_lock);
316 status = ocfs2_update_disk_slots(osb, si);
323 ocfs2_free_slot_info(osb);