blob: f5727b8cc91395d1d9569b3b5505e883f96bf086 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- 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>
Mark Fashehccd979b2005-12-15 14:31:24 -080029
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
45static s16 __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
46 s16 global);
47static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
48 s16 slot_num,
49 s16 node_num);
50
Mark Fashehccd979b2005-12-15 14:31:24 -080051/* post the slot information on disk into our slot_info struct. */
Mark Fasheh8e8a4602008-02-01 11:59:09 -080052static void ocfs2_update_slot_info(struct ocfs2_slot_info *si)
Mark Fashehccd979b2005-12-15 14:31:24 -080053{
54 int i;
55 __le16 *disk_info;
56
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;
61
62 for (i = 0; i < si->si_size; i++)
63 si->si_global_node_nums[i] = le16_to_cpu(disk_info[i]);
64
65 spin_unlock(&si->si_lock);
66}
67
Mark Fasheh8e8a4602008-02-01 11:59:09 -080068int ocfs2_refresh_slot_info(struct ocfs2_super *osb)
69{
70 int ret;
71 struct ocfs2_slot_info *si = osb->slot_info;
72 struct buffer_head *bh;
73
74 if (si == NULL)
75 return 0;
76
77 bh = si->si_bh;
78 ret = ocfs2_read_block(osb, bh->b_blocknr, &bh, 0, si->si_inode);
79 if (ret == 0)
80 ocfs2_update_slot_info(si);
81
82 return ret;
83}
84
Mark Fashehccd979b2005-12-15 14:31:24 -080085/* post the our slot info stuff into it's destination bh and write it
86 * out. */
Mark Fasheh8e8a4602008-02-01 11:59:09 -080087static int ocfs2_update_disk_slots(struct ocfs2_super *osb,
88 struct ocfs2_slot_info *si)
Mark Fashehccd979b2005-12-15 14:31:24 -080089{
90 int status, i;
91 __le16 *disk_info = (__le16 *) si->si_bh->b_data;
92
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);
97
98 status = ocfs2_write_block(osb, si->si_bh, si->si_inode);
99 if (status < 0)
100 mlog_errno(status);
101
102 return status;
103}
104
105/* try to find global node in the slot info. Returns
106 * OCFS2_INVALID_SLOT if nothing is found. */
107static s16 __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
108 s16 global)
109{
110 int i;
111 s16 ret = OCFS2_INVALID_SLOT;
112
113 for(i = 0; i < si->si_num_slots; i++) {
114 if (global == si->si_global_node_nums[i]) {
115 ret = (s16) i;
116 break;
117 }
118 }
119 return ret;
120}
121
Sunil Mushranbaf46612007-06-18 17:00:24 -0700122static s16 __ocfs2_find_empty_slot(struct ocfs2_slot_info *si, s16 preferred)
Mark Fashehccd979b2005-12-15 14:31:24 -0800123{
124 int i;
125 s16 ret = OCFS2_INVALID_SLOT;
126
Sunil Mushranbaf46612007-06-18 17:00:24 -0700127 if (preferred >= 0 && preferred < si->si_num_slots) {
128 if (OCFS2_INVALID_SLOT == si->si_global_node_nums[preferred]) {
129 ret = preferred;
130 goto out;
131 }
132 }
133
Mark Fashehccd979b2005-12-15 14:31:24 -0800134 for(i = 0; i < si->si_num_slots; i++) {
135 if (OCFS2_INVALID_SLOT == si->si_global_node_nums[i]) {
136 ret = (s16) i;
137 break;
138 }
139 }
Sunil Mushranbaf46612007-06-18 17:00:24 -0700140out:
Mark Fashehccd979b2005-12-15 14:31:24 -0800141 return ret;
142}
143
144s16 ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
145 s16 global)
146{
147 s16 ret;
148
149 spin_lock(&si->si_lock);
150 ret = __ocfs2_node_num_to_slot(si, global);
151 spin_unlock(&si->si_lock);
152 return ret;
153}
154
Mark Fasheh8e8a4602008-02-01 11:59:09 -0800155static void __ocfs2_free_slot_info(struct ocfs2_slot_info *si)
156{
157 if (si == NULL)
158 return;
159
160 if (si->si_inode)
161 iput(si->si_inode);
162 if (si->si_bh)
163 brelse(si->si_bh);
164
165 kfree(si);
166}
167
Mark Fashehccd979b2005-12-15 14:31:24 -0800168static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
169 s16 slot_num,
170 s16 node_num)
171{
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));
176
177 si->si_global_node_nums[slot_num] = node_num;
178}
179
Mark Fasheh8e8a4602008-02-01 11:59:09 -0800180int ocfs2_clear_slot(struct ocfs2_super *osb, s16 slot_num)
Mark Fashehccd979b2005-12-15 14:31:24 -0800181{
Mark Fasheh8e8a4602008-02-01 11:59:09 -0800182 struct ocfs2_slot_info *si = osb->slot_info;
183
184 if (si == NULL)
185 return 0;
186
Mark Fashehccd979b2005-12-15 14:31:24 -0800187 spin_lock(&si->si_lock);
188 __ocfs2_fill_slot(si, slot_num, OCFS2_INVALID_SLOT);
189 spin_unlock(&si->si_lock);
Mark Fasheh8e8a4602008-02-01 11:59:09 -0800190
191 return ocfs2_update_disk_slots(osb, osb->slot_info);
Mark Fashehccd979b2005-12-15 14:31:24 -0800192}
193
194int ocfs2_init_slot_info(struct ocfs2_super *osb)
195{
196 int status, i;
197 u64 blkno;
198 struct inode *inode = NULL;
199 struct buffer_head *bh = NULL;
200 struct ocfs2_slot_info *si;
201
Robert P. J. Daycd861282006-12-13 00:34:52 -0800202 si = kzalloc(sizeof(struct ocfs2_slot_info), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800203 if (!si) {
204 status = -ENOMEM;
205 mlog_errno(status);
206 goto bail;
207 }
208
209 spin_lock_init(&si->si_lock);
210 si->si_num_slots = osb->max_slots;
211 si->si_size = OCFS2_MAX_SLOTS;
212
213 for(i = 0; i < si->si_num_slots; i++)
214 si->si_global_node_nums[i] = OCFS2_INVALID_SLOT;
215
216 inode = ocfs2_get_system_file_inode(osb, SLOT_MAP_SYSTEM_INODE,
217 OCFS2_INVALID_SLOT);
218 if (!inode) {
219 status = -EINVAL;
220 mlog_errno(status);
221 goto bail;
222 }
223
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800224 status = ocfs2_extent_map_get_blocks(inode, 0ULL, &blkno, NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800225 if (status < 0) {
226 mlog_errno(status);
227 goto bail;
228 }
229
230 status = ocfs2_read_block(osb, blkno, &bh, 0, inode);
231 if (status < 0) {
232 mlog_errno(status);
233 goto bail;
234 }
235
236 si->si_inode = inode;
237 si->si_bh = bh;
238 osb->slot_info = si;
239bail:
240 if (status < 0 && si)
Mark Fasheh8e8a4602008-02-01 11:59:09 -0800241 __ocfs2_free_slot_info(si);
Mark Fashehccd979b2005-12-15 14:31:24 -0800242
243 return status;
244}
245
Mark Fasheh8e8a4602008-02-01 11:59:09 -0800246void ocfs2_free_slot_info(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -0800247{
Mark Fasheh8e8a4602008-02-01 11:59:09 -0800248 struct ocfs2_slot_info *si = osb->slot_info;
249
250 osb->slot_info = NULL;
251 __ocfs2_free_slot_info(si);
Mark Fashehccd979b2005-12-15 14:31:24 -0800252}
253
254int ocfs2_find_slot(struct ocfs2_super *osb)
255{
256 int status;
257 s16 slot;
258 struct ocfs2_slot_info *si;
259
260 mlog_entry_void();
261
262 si = osb->slot_info;
263
264 ocfs2_update_slot_info(si);
265
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
274 * one. */
Sunil Mushranbaf46612007-06-18 17:00:24 -0700275 slot = __ocfs2_find_empty_slot(si, osb->preferred_slot);
Mark Fashehccd979b2005-12-15 14:31:24 -0800276 if (slot == OCFS2_INVALID_SLOT) {
277 spin_unlock(&si->si_lock);
278 mlog(ML_ERROR, "no free slots available!\n");
279 status = -EINVAL;
280 goto bail;
281 }
282 } else
283 mlog(ML_NOTICE, "slot %d is already allocated to this node!\n",
284 slot);
285
286 __ocfs2_fill_slot(si, slot, osb->node_num);
287 osb->slot_num = slot;
288 spin_unlock(&si->si_lock);
289
Mark Fashehe7607ab2006-04-27 17:53:22 -0700290 mlog(0, "taking node slot %d\n", osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800291
292 status = ocfs2_update_disk_slots(osb, si);
293 if (status < 0)
294 mlog_errno(status);
295
296bail:
297 mlog_exit(status);
298 return status;
299}
300
301void ocfs2_put_slot(struct ocfs2_super *osb)
302{
303 int status;
304 struct ocfs2_slot_info *si = osb->slot_info;
305
306 if (!si)
307 return;
308
309 ocfs2_update_slot_info(si);
310
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);
315
316 status = ocfs2_update_disk_slots(osb, si);
317 if (status < 0) {
318 mlog_errno(status);
319 goto bail;
320 }
321
322bail:
Mark Fasheh8e8a4602008-02-01 11:59:09 -0800323 ocfs2_free_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800324}
325