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 struct ocfs2_slot_info
{
46 struct inode
*si_inode
;
47 struct buffer_head
*si_bh
;
48 unsigned int si_num_slots
;
50 s16 si_global_node_nums
[OCFS2_MAX_SLOTS
];
54 static s16
__ocfs2_node_num_to_slot(struct ocfs2_slot_info
*si
,
56 static void __ocfs2_fill_slot(struct ocfs2_slot_info
*si
,
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
)
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
)
80 struct ocfs2_slot_info
*si
= osb
->slot_info
;
81 struct buffer_head
*bh
;
87 ret
= ocfs2_read_block(osb
, bh
->b_blocknr
, &bh
, 0, si
->si_inode
);
89 spin_lock(&osb
->osb_lock
);
90 ocfs2_update_slot_info(si
);
91 spin_unlock(&osb
->osb_lock
);
97 /* post the our slot info stuff into it's destination bh and write it
99 static int ocfs2_update_disk_slots(struct ocfs2_super
*osb
,
100 struct ocfs2_slot_info
*si
)
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
);
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
,
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
]) {
134 static s16
__ocfs2_find_empty_slot(struct ocfs2_slot_info
*si
,
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
]) {
147 for(i
= 0; i
< si
->si_num_slots
; i
++) {
148 if (OCFS2_INVALID_SLOT
== si
->si_global_node_nums
[i
]) {
157 int ocfs2_node_num_to_slot(struct ocfs2_super
*osb
, unsigned int node_num
)
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
)
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
)
185 *node_num
= si
->si_global_node_nums
[slot_num
];
189 static void __ocfs2_free_slot_info(struct ocfs2_slot_info
*si
)
202 static void __ocfs2_fill_slot(struct ocfs2_slot_info
*si
,
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
;
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
)
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
);
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
,
257 status
= ocfs2_extent_map_get_blocks(inode
, 0ULL, &blkno
, NULL
, NULL
);
263 status
= ocfs2_read_block(osb
, blkno
, &bh
, 0, inode
);
269 si
->si_inode
= inode
;
271 osb
->slot_info
= (struct ocfs2_slot_info
*)si
;
273 if (status
< 0 && si
)
274 __ocfs2_free_slot_info(si
);
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
)
291 struct ocfs2_slot_info
*si
;
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
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");
316 mlog(ML_NOTICE
, "slot %d is already allocated to this node!\n",
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
);
334 void ocfs2_put_slot(struct ocfs2_super
*osb
)
337 struct ocfs2_slot_info
*si
= osb
->slot_info
;
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
);
356 ocfs2_free_slot_info(osb
);