blob: 762360d95e9315d7003e5ea79e406fb48ac5be94 [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
Joel Beckerd85b20e2008-02-01 12:01:05 -080045struct 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
Mark Fashehccd979b2005-12-15 14:31:24 -080054static s16 __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
55 s16 global);
56static void __ocfs2_fill_slot(struct ocfs2_slot_info *si,
57 s16 slot_num,
58 s16 node_num);
59
Joel Beckerd85b20e2008-02-01 12:01:05 -080060/*
61 * Post the slot information on disk into our slot_info struct.
62 * Must be protected by osb_lock.
63 */
Mark Fasheh8e8a4602008-02-01 11:59:09 -080064static void ocfs2_update_slot_info(struct ocfs2_slot_info *si)
Mark Fashehccd979b2005-12-15 14:31:24 -080065{
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. */
Mark Fashehccd979b2005-12-15 14:31:24 -080071 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]);
Mark Fashehccd979b2005-12-15 14:31:24 -080075}
76
Mark Fasheh8e8a4602008-02-01 11:59:09 -080077int 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);
Joel Beckerd85b20e2008-02-01 12:01:05 -080088 if (ret == 0) {
89 spin_lock(&osb->osb_lock);
Mark Fasheh8e8a4602008-02-01 11:59:09 -080090 ocfs2_update_slot_info(si);
Joel Beckerd85b20e2008-02-01 12:01:05 -080091 spin_unlock(&osb->osb_lock);
92 }
Mark Fasheh8e8a4602008-02-01 11:59:09 -080093
94 return ret;
95}
96
Mark Fashehccd979b2005-12-15 14:31:24 -080097/* post the our slot info stuff into it's destination bh and write it
98 * out. */
Mark Fasheh8e8a4602008-02-01 11:59:09 -080099static int ocfs2_update_disk_slots(struct ocfs2_super *osb,
100 struct ocfs2_slot_info *si)
Mark Fashehccd979b2005-12-15 14:31:24 -0800101{
102 int status, i;
103 __le16 *disk_info = (__le16 *) si->si_bh->b_data;
104
Joel Beckerd85b20e2008-02-01 12:01:05 -0800105 spin_lock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800106 for (i = 0; i < si->si_size; i++)
107 disk_info[i] = cpu_to_le16(si->si_global_node_nums[i]);
Joel Beckerd85b20e2008-02-01 12:01:05 -0800108 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800109
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. */
119static 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
Joel Beckerd85b20e2008-02-01 12:01:05 -0800134static s16 __ocfs2_find_empty_slot(struct ocfs2_slot_info *si,
135 s16 preferred)
Mark Fashehccd979b2005-12-15 14:31:24 -0800136{
137 int i;
138 s16 ret = OCFS2_INVALID_SLOT;
139
Sunil Mushranbaf46612007-06-18 17:00:24 -0700140 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
Mark Fashehccd979b2005-12-15 14:31:24 -0800147 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 }
Sunil Mushranbaf46612007-06-18 17:00:24 -0700153out:
Mark Fashehccd979b2005-12-15 14:31:24 -0800154 return ret;
155}
156
Joel Beckerd85b20e2008-02-01 12:01:05 -0800157int ocfs2_node_num_to_slot(struct ocfs2_super *osb, unsigned int node_num)
Mark Fashehccd979b2005-12-15 14:31:24 -0800158{
Joel Beckerd85b20e2008-02-01 12:01:05 -0800159 s16 slot;
160 struct ocfs2_slot_info *si = osb->slot_info;
Mark Fashehccd979b2005-12-15 14:31:24 -0800161
Joel Beckerd85b20e2008-02-01 12:01:05 -0800162 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
172int 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;
Mark Fashehccd979b2005-12-15 14:31:24 -0800187}
188
Mark Fasheh8e8a4602008-02-01 11:59:09 -0800189static 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
Mark Fashehccd979b2005-12-15 14:31:24 -0800202static 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
Mark Fasheh8e8a4602008-02-01 11:59:09 -0800214int ocfs2_clear_slot(struct ocfs2_super *osb, s16 slot_num)
Mark Fashehccd979b2005-12-15 14:31:24 -0800215{
Mark Fasheh8e8a4602008-02-01 11:59:09 -0800216 struct ocfs2_slot_info *si = osb->slot_info;
217
218 if (si == NULL)
219 return 0;
220
Joel Beckerd85b20e2008-02-01 12:01:05 -0800221 spin_lock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800222 __ocfs2_fill_slot(si, slot_num, OCFS2_INVALID_SLOT);
Joel Beckerd85b20e2008-02-01 12:01:05 -0800223 spin_unlock(&osb->osb_lock);
Mark Fasheh8e8a4602008-02-01 11:59:09 -0800224
225 return ocfs2_update_disk_slots(osb, osb->slot_info);
Mark Fashehccd979b2005-12-15 14:31:24 -0800226}
227
228int 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
Robert P. J. Daycd861282006-12-13 00:34:52 -0800236 si = kzalloc(sizeof(struct ocfs2_slot_info), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800237 if (!si) {
238 status = -ENOMEM;
239 mlog_errno(status);
240 goto bail;
241 }
242
Mark Fashehccd979b2005-12-15 14:31:24 -0800243 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
Mark Fasheh49cb8d22007-03-09 16:21:46 -0800257 status = ocfs2_extent_map_get_blocks(inode, 0ULL, &blkno, NULL, NULL);
Mark Fashehccd979b2005-12-15 14:31:24 -0800258 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;
Joel Beckerd85b20e2008-02-01 12:01:05 -0800271 osb->slot_info = (struct ocfs2_slot_info *)si;
Mark Fashehccd979b2005-12-15 14:31:24 -0800272bail:
273 if (status < 0 && si)
Mark Fasheh8e8a4602008-02-01 11:59:09 -0800274 __ocfs2_free_slot_info(si);
Mark Fashehccd979b2005-12-15 14:31:24 -0800275
276 return status;
277}
278
Mark Fasheh8e8a4602008-02-01 11:59:09 -0800279void ocfs2_free_slot_info(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -0800280{
Mark Fasheh8e8a4602008-02-01 11:59:09 -0800281 struct ocfs2_slot_info *si = osb->slot_info;
282
283 osb->slot_info = NULL;
284 __ocfs2_free_slot_info(si);
Mark Fashehccd979b2005-12-15 14:31:24 -0800285}
286
287int 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
Joel Beckerd85b20e2008-02-01 12:01:05 -0800297 spin_lock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800298 ocfs2_update_slot_info(si);
299
Mark Fashehccd979b2005-12-15 14:31:24 -0800300 /* 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. */
Sunil Mushranbaf46612007-06-18 17:00:24 -0700308 slot = __ocfs2_find_empty_slot(si, osb->preferred_slot);
Mark Fashehccd979b2005-12-15 14:31:24 -0800309 if (slot == OCFS2_INVALID_SLOT) {
Joel Beckerd85b20e2008-02-01 12:01:05 -0800310 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800311 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;
Joel Beckerd85b20e2008-02-01 12:01:05 -0800321 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800322
Mark Fashehe7607ab2006-04-27 17:53:22 -0700323 mlog(0, "taking node slot %d\n", osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800324
325 status = ocfs2_update_disk_slots(osb, si);
326 if (status < 0)
327 mlog_errno(status);
328
329bail:
330 mlog_exit(status);
331 return status;
332}
333
334void 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
Joel Beckerd85b20e2008-02-01 12:01:05 -0800342 spin_lock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800343 ocfs2_update_slot_info(si);
344
Mark Fashehccd979b2005-12-15 14:31:24 -0800345 __ocfs2_fill_slot(si, osb->slot_num, OCFS2_INVALID_SLOT);
346 osb->slot_num = OCFS2_INVALID_SLOT;
Joel Beckerd85b20e2008-02-01 12:01:05 -0800347 spin_unlock(&osb->osb_lock);
Mark Fashehccd979b2005-12-15 14:31:24 -0800348
349 status = ocfs2_update_disk_slots(osb, si);
350 if (status < 0) {
351 mlog_errno(status);
352 goto bail;
353 }
354
355bail:
Mark Fasheh8e8a4602008-02-01 11:59:09 -0800356 ocfs2_free_slot_info(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800357}
358