RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / fs / ocfs2 / slot_map.c
blobd8b79067dc14778a5d67556c20c62fec9a5e4de7
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 static s16 __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
46 s16 global);
47 static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
48 s16 slot_num,
49 s16 node_num);
51 /* Use the slot information we've collected to create a map of mounted
52 * nodes. Should be holding an EX on super block. assumes slot info is
53 * up to date. Note that we call this *after* we find a slot, so our
54 * own node should be set in the map too... */
55 void ocfs2_populate_mounted_map(struct ocfs2_super *osb)
57 int i;
58 struct ocfs2_slot_info *si = osb->slot_info;
60 spin_lock(&si->si_lock);
62 for (i = 0; i < si->si_size; i++)
63 if (si->si_global_node_nums[i] != OCFS2_INVALID_SLOT)
64 ocfs2_node_map_set_bit(osb, &osb->mounted_map,
65 si->si_global_node_nums[i]);
67 spin_unlock(&si->si_lock);
70 /* post the slot information on disk into our slot_info struct. */
71 void ocfs2_update_slot_info(struct ocfs2_slot_info *si)
73 int i;
74 __le16 *disk_info;
76 /* we don't read the slot block here as ocfs2_super_lock
77 * should've made sure we have the most recent copy. */
78 spin_lock(&si->si_lock);
79 disk_info = (__le16 *) si->si_bh->b_data;
81 for (i = 0; i < si->si_size; i++)
82 si->si_global_node_nums[i] = le16_to_cpu(disk_info[i]);
84 spin_unlock(&si->si_lock);
87 /* post the our slot info stuff into it's destination bh and write it
88 * out. */
89 int ocfs2_update_disk_slots(struct ocfs2_super *osb,
90 struct ocfs2_slot_info *si)
92 int status, i;
93 __le16 *disk_info = (__le16 *) si->si_bh->b_data;
95 spin_lock(&si->si_lock);
96 for (i = 0; i < si->si_size; i++)
97 disk_info[i] = cpu_to_le16(si->si_global_node_nums[i]);
98 spin_unlock(&si->si_lock);
100 status = ocfs2_write_block(osb, si->si_bh, si->si_inode);
101 if (status < 0)
102 mlog_errno(status);
104 return status;
107 /* try to find global node in the slot info. Returns
108 * OCFS2_INVALID_SLOT if nothing is found. */
109 static s16 __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
110 s16 global)
112 int i;
113 s16 ret = OCFS2_INVALID_SLOT;
115 for(i = 0; i < si->si_num_slots; i++) {
116 if (global == si->si_global_node_nums[i]) {
117 ret = (s16) i;
118 break;
121 return ret;
124 static s16 __ocfs2_find_empty_slot(struct ocfs2_slot_info *si)
126 int i;
127 s16 ret = OCFS2_INVALID_SLOT;
129 for(i = 0; i < si->si_num_slots; i++) {
130 if (OCFS2_INVALID_SLOT == si->si_global_node_nums[i]) {
131 ret = (s16) i;
132 break;
135 return ret;
138 s16 ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
139 s16 global)
141 s16 ret;
143 spin_lock(&si->si_lock);
144 ret = __ocfs2_node_num_to_slot(si, global);
145 spin_unlock(&si->si_lock);
146 return ret;
149 static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
150 s16 slot_num,
151 s16 node_num)
153 BUG_ON(slot_num == OCFS2_INVALID_SLOT);
154 BUG_ON(slot_num >= si->si_num_slots);
155 BUG_ON((node_num != O2NM_INVALID_NODE_NUM) &&
156 (node_num >= O2NM_MAX_NODES));
158 si->si_global_node_nums[slot_num] = node_num;
161 void ocfs2_clear_slot(struct ocfs2_slot_info *si,
162 s16 slot_num)
164 spin_lock(&si->si_lock);
165 __ocfs2_fill_slot(si, slot_num, OCFS2_INVALID_SLOT);
166 spin_unlock(&si->si_lock);
169 int ocfs2_init_slot_info(struct ocfs2_super *osb)
171 int status, i;
172 u64 blkno;
173 struct inode *inode = NULL;
174 struct buffer_head *bh = NULL;
175 struct ocfs2_slot_info *si;
177 si = kzalloc(sizeof(struct ocfs2_slot_info), GFP_KERNEL);
178 if (!si) {
179 status = -ENOMEM;
180 mlog_errno(status);
181 goto bail;
184 spin_lock_init(&si->si_lock);
185 si->si_num_slots = osb->max_slots;
186 si->si_size = OCFS2_MAX_SLOTS;
188 for(i = 0; i < si->si_num_slots; i++)
189 si->si_global_node_nums[i] = OCFS2_INVALID_SLOT;
191 inode = ocfs2_get_system_file_inode(osb, SLOT_MAP_SYSTEM_INODE,
192 OCFS2_INVALID_SLOT);
193 if (!inode) {
194 status = -EINVAL;
195 mlog_errno(status);
196 goto bail;
199 status = ocfs2_extent_map_get_blocks(inode, 0ULL, &blkno, NULL, NULL);
200 if (status < 0) {
201 mlog_errno(status);
202 goto bail;
205 status = ocfs2_read_block(osb, blkno, &bh, 0, inode);
206 if (status < 0) {
207 mlog_errno(status);
208 goto bail;
211 si->si_inode = inode;
212 si->si_bh = bh;
213 osb->slot_info = si;
214 bail:
215 if (status < 0 && si)
216 ocfs2_free_slot_info(si);
218 return status;
221 void ocfs2_free_slot_info(struct ocfs2_slot_info *si)
223 if (si->si_inode)
224 iput(si->si_inode);
225 if (si->si_bh)
226 brelse(si->si_bh);
227 kfree(si);
230 int ocfs2_find_slot(struct ocfs2_super *osb)
232 int status;
233 s16 slot;
234 struct ocfs2_slot_info *si;
236 mlog_entry_void();
238 si = osb->slot_info;
240 ocfs2_update_slot_info(si);
242 spin_lock(&si->si_lock);
243 /* search for ourselves first and take the slot if it already
244 * exists. Perhaps we need to mark this in a variable for our
245 * own journal recovery? Possibly not, though we certainly
246 * need to warn to the user */
247 slot = __ocfs2_node_num_to_slot(si, osb->node_num);
248 if (slot == OCFS2_INVALID_SLOT) {
249 /* if no slot yet, then just take 1st available
250 * one. */
251 slot = __ocfs2_find_empty_slot(si);
252 if (slot == OCFS2_INVALID_SLOT) {
253 spin_unlock(&si->si_lock);
254 mlog(ML_ERROR, "no free slots available!\n");
255 status = -EINVAL;
256 goto bail;
258 } else
259 mlog(ML_NOTICE, "slot %d is already allocated to this node!\n",
260 slot);
262 __ocfs2_fill_slot(si, slot, osb->node_num);
263 osb->slot_num = slot;
264 spin_unlock(&si->si_lock);
266 mlog(0, "taking node slot %d\n", osb->slot_num);
268 status = ocfs2_update_disk_slots(osb, si);
269 if (status < 0)
270 mlog_errno(status);
272 bail:
273 mlog_exit(status);
274 return status;
277 void ocfs2_put_slot(struct ocfs2_super *osb)
279 int status;
280 struct ocfs2_slot_info *si = osb->slot_info;
282 if (!si)
283 return;
285 ocfs2_update_slot_info(si);
287 spin_lock(&si->si_lock);
288 __ocfs2_fill_slot(si, osb->slot_num, OCFS2_INVALID_SLOT);
289 osb->slot_num = OCFS2_INVALID_SLOT;
290 spin_unlock(&si->si_lock);
292 status = ocfs2_update_disk_slots(osb, si);
293 if (status < 0) {
294 mlog_errno(status);
295 goto bail;
298 bail:
299 osb->slot_info = NULL;
300 ocfs2_free_slot_info(si);