Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 1 | /* -*- 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> |
| 29 | #include <linux/smp_lock.h> |
| 30 | |
| 31 | #define MLOG_MASK_PREFIX ML_SUPER |
| 32 | #include <cluster/masklog.h> |
| 33 | |
| 34 | #include "ocfs2.h" |
| 35 | |
| 36 | #include "dlmglue.h" |
| 37 | #include "extent_map.h" |
| 38 | #include "heartbeat.h" |
| 39 | #include "inode.h" |
| 40 | #include "slot_map.h" |
| 41 | #include "super.h" |
| 42 | #include "sysfile.h" |
| 43 | |
| 44 | #include "buffer_head_io.h" |
| 45 | |
| 46 | static s16 __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si, |
| 47 | s16 global); |
| 48 | static void __ocfs2_fill_slot(struct ocfs2_slot_info *si, |
| 49 | s16 slot_num, |
| 50 | s16 node_num); |
| 51 | |
| 52 | /* Use the slot information we've collected to create a map of mounted |
| 53 | * nodes. Should be holding an EX on super block. assumes slot info is |
| 54 | * up to date. Note that we call this *after* we find a slot, so our |
| 55 | * own node should be set in the map too... */ |
| 56 | void ocfs2_populate_mounted_map(struct ocfs2_super *osb) |
| 57 | { |
| 58 | int i; |
| 59 | struct ocfs2_slot_info *si = osb->slot_info; |
| 60 | |
| 61 | spin_lock(&si->si_lock); |
| 62 | |
| 63 | for (i = 0; i < si->si_size; i++) |
| 64 | if (si->si_global_node_nums[i] != OCFS2_INVALID_SLOT) |
| 65 | ocfs2_node_map_set_bit(osb, &osb->mounted_map, |
| 66 | si->si_global_node_nums[i]); |
| 67 | |
| 68 | spin_unlock(&si->si_lock); |
| 69 | } |
| 70 | |
| 71 | /* post the slot information on disk into our slot_info struct. */ |
| 72 | void ocfs2_update_slot_info(struct ocfs2_slot_info *si) |
| 73 | { |
| 74 | int i; |
| 75 | __le16 *disk_info; |
| 76 | |
| 77 | /* we don't read the slot block here as ocfs2_super_lock |
| 78 | * should've made sure we have the most recent copy. */ |
| 79 | spin_lock(&si->si_lock); |
| 80 | disk_info = (__le16 *) si->si_bh->b_data; |
| 81 | |
| 82 | for (i = 0; i < si->si_size; i++) |
| 83 | si->si_global_node_nums[i] = le16_to_cpu(disk_info[i]); |
| 84 | |
| 85 | spin_unlock(&si->si_lock); |
| 86 | } |
| 87 | |
| 88 | /* post the our slot info stuff into it's destination bh and write it |
| 89 | * out. */ |
| 90 | int ocfs2_update_disk_slots(struct ocfs2_super *osb, |
| 91 | struct ocfs2_slot_info *si) |
| 92 | { |
| 93 | int status, i; |
| 94 | __le16 *disk_info = (__le16 *) si->si_bh->b_data; |
| 95 | |
| 96 | spin_lock(&si->si_lock); |
| 97 | for (i = 0; i < si->si_size; i++) |
| 98 | disk_info[i] = cpu_to_le16(si->si_global_node_nums[i]); |
| 99 | spin_unlock(&si->si_lock); |
| 100 | |
| 101 | status = ocfs2_write_block(osb, si->si_bh, si->si_inode); |
| 102 | if (status < 0) |
| 103 | mlog_errno(status); |
| 104 | |
| 105 | return status; |
| 106 | } |
| 107 | |
| 108 | /* try to find global node in the slot info. Returns |
| 109 | * OCFS2_INVALID_SLOT if nothing is found. */ |
| 110 | static s16 __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si, |
| 111 | s16 global) |
| 112 | { |
| 113 | int i; |
| 114 | s16 ret = OCFS2_INVALID_SLOT; |
| 115 | |
| 116 | for(i = 0; i < si->si_num_slots; i++) { |
| 117 | if (global == si->si_global_node_nums[i]) { |
| 118 | ret = (s16) i; |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | static s16 __ocfs2_find_empty_slot(struct ocfs2_slot_info *si) |
| 126 | { |
| 127 | int i; |
| 128 | s16 ret = OCFS2_INVALID_SLOT; |
| 129 | |
| 130 | for(i = 0; i < si->si_num_slots; i++) { |
| 131 | if (OCFS2_INVALID_SLOT == si->si_global_node_nums[i]) { |
| 132 | ret = (s16) i; |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | s16 ocfs2_node_num_to_slot(struct ocfs2_slot_info *si, |
| 140 | s16 global) |
| 141 | { |
| 142 | s16 ret; |
| 143 | |
| 144 | spin_lock(&si->si_lock); |
| 145 | ret = __ocfs2_node_num_to_slot(si, global); |
| 146 | spin_unlock(&si->si_lock); |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | static void __ocfs2_fill_slot(struct ocfs2_slot_info *si, |
| 151 | s16 slot_num, |
| 152 | s16 node_num) |
| 153 | { |
| 154 | BUG_ON(slot_num == OCFS2_INVALID_SLOT); |
| 155 | BUG_ON(slot_num >= si->si_num_slots); |
| 156 | BUG_ON((node_num != O2NM_INVALID_NODE_NUM) && |
| 157 | (node_num >= O2NM_MAX_NODES)); |
| 158 | |
| 159 | si->si_global_node_nums[slot_num] = node_num; |
| 160 | } |
| 161 | |
| 162 | void ocfs2_clear_slot(struct ocfs2_slot_info *si, |
| 163 | s16 slot_num) |
| 164 | { |
| 165 | spin_lock(&si->si_lock); |
| 166 | __ocfs2_fill_slot(si, slot_num, OCFS2_INVALID_SLOT); |
| 167 | spin_unlock(&si->si_lock); |
| 168 | } |
| 169 | |
| 170 | int ocfs2_init_slot_info(struct ocfs2_super *osb) |
| 171 | { |
| 172 | int status, i; |
| 173 | u64 blkno; |
| 174 | struct inode *inode = NULL; |
| 175 | struct buffer_head *bh = NULL; |
| 176 | struct ocfs2_slot_info *si; |
| 177 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 178 | si = kzalloc(sizeof(struct ocfs2_slot_info), GFP_KERNEL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 179 | if (!si) { |
| 180 | status = -ENOMEM; |
| 181 | mlog_errno(status); |
| 182 | goto bail; |
| 183 | } |
| 184 | |
| 185 | spin_lock_init(&si->si_lock); |
| 186 | si->si_num_slots = osb->max_slots; |
| 187 | si->si_size = OCFS2_MAX_SLOTS; |
| 188 | |
| 189 | for(i = 0; i < si->si_num_slots; i++) |
| 190 | si->si_global_node_nums[i] = OCFS2_INVALID_SLOT; |
| 191 | |
| 192 | inode = ocfs2_get_system_file_inode(osb, SLOT_MAP_SYSTEM_INODE, |
| 193 | OCFS2_INVALID_SLOT); |
| 194 | if (!inode) { |
| 195 | status = -EINVAL; |
| 196 | mlog_errno(status); |
| 197 | goto bail; |
| 198 | } |
| 199 | |
| 200 | status = ocfs2_extent_map_get_blocks(inode, 0ULL, 1, &blkno, NULL); |
| 201 | if (status < 0) { |
| 202 | mlog_errno(status); |
| 203 | goto bail; |
| 204 | } |
| 205 | |
| 206 | status = ocfs2_read_block(osb, blkno, &bh, 0, inode); |
| 207 | if (status < 0) { |
| 208 | mlog_errno(status); |
| 209 | goto bail; |
| 210 | } |
| 211 | |
| 212 | si->si_inode = inode; |
| 213 | si->si_bh = bh; |
| 214 | osb->slot_info = si; |
| 215 | bail: |
| 216 | if (status < 0 && si) |
| 217 | ocfs2_free_slot_info(si); |
| 218 | |
| 219 | return status; |
| 220 | } |
| 221 | |
| 222 | void ocfs2_free_slot_info(struct ocfs2_slot_info *si) |
| 223 | { |
| 224 | if (si->si_inode) |
| 225 | iput(si->si_inode); |
| 226 | if (si->si_bh) |
| 227 | brelse(si->si_bh); |
| 228 | kfree(si); |
| 229 | } |
| 230 | |
| 231 | int ocfs2_find_slot(struct ocfs2_super *osb) |
| 232 | { |
| 233 | int status; |
| 234 | s16 slot; |
| 235 | struct ocfs2_slot_info *si; |
| 236 | |
| 237 | mlog_entry_void(); |
| 238 | |
| 239 | si = osb->slot_info; |
| 240 | |
| 241 | ocfs2_update_slot_info(si); |
| 242 | |
| 243 | spin_lock(&si->si_lock); |
| 244 | /* search for ourselves first and take the slot if it already |
| 245 | * exists. Perhaps we need to mark this in a variable for our |
| 246 | * own journal recovery? Possibly not, though we certainly |
| 247 | * need to warn to the user */ |
| 248 | slot = __ocfs2_node_num_to_slot(si, osb->node_num); |
| 249 | if (slot == OCFS2_INVALID_SLOT) { |
| 250 | /* if no slot yet, then just take 1st available |
| 251 | * one. */ |
| 252 | slot = __ocfs2_find_empty_slot(si); |
| 253 | if (slot == OCFS2_INVALID_SLOT) { |
| 254 | spin_unlock(&si->si_lock); |
| 255 | mlog(ML_ERROR, "no free slots available!\n"); |
| 256 | status = -EINVAL; |
| 257 | goto bail; |
| 258 | } |
| 259 | } else |
| 260 | mlog(ML_NOTICE, "slot %d is already allocated to this node!\n", |
| 261 | slot); |
| 262 | |
| 263 | __ocfs2_fill_slot(si, slot, osb->node_num); |
| 264 | osb->slot_num = slot; |
| 265 | spin_unlock(&si->si_lock); |
| 266 | |
Mark Fasheh | e7607ab | 2006-04-27 17:53:22 -0700 | [diff] [blame] | 267 | mlog(0, "taking node slot %d\n", osb->slot_num); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 268 | |
| 269 | status = ocfs2_update_disk_slots(osb, si); |
| 270 | if (status < 0) |
| 271 | mlog_errno(status); |
| 272 | |
| 273 | bail: |
| 274 | mlog_exit(status); |
| 275 | return status; |
| 276 | } |
| 277 | |
| 278 | void ocfs2_put_slot(struct ocfs2_super *osb) |
| 279 | { |
| 280 | int status; |
| 281 | struct ocfs2_slot_info *si = osb->slot_info; |
| 282 | |
| 283 | if (!si) |
| 284 | return; |
| 285 | |
| 286 | ocfs2_update_slot_info(si); |
| 287 | |
| 288 | spin_lock(&si->si_lock); |
| 289 | __ocfs2_fill_slot(si, osb->slot_num, OCFS2_INVALID_SLOT); |
| 290 | osb->slot_num = OCFS2_INVALID_SLOT; |
| 291 | spin_unlock(&si->si_lock); |
| 292 | |
| 293 | status = ocfs2_update_disk_slots(osb, si); |
| 294 | if (status < 0) { |
| 295 | mlog_errno(status); |
| 296 | goto bail; |
| 297 | } |
| 298 | |
| 299 | bail: |
| 300 | osb->slot_info = NULL; |
| 301 | ocfs2_free_slot_info(si); |
| 302 | } |
| 303 | |