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> |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 29 | |
| 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 | |
| 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); |
| 50 | |
| 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) |
| 56 | { |
| 57 | int i; |
| 58 | struct ocfs2_slot_info *si = osb->slot_info; |
| 59 | |
| 60 | spin_lock(&si->si_lock); |
| 61 | |
| 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]); |
| 66 | |
| 67 | spin_unlock(&si->si_lock); |
| 68 | } |
| 69 | |
| 70 | /* post the slot information on disk into our slot_info struct. */ |
| 71 | void ocfs2_update_slot_info(struct ocfs2_slot_info *si) |
| 72 | { |
| 73 | int i; |
| 74 | __le16 *disk_info; |
| 75 | |
| 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; |
| 80 | |
| 81 | for (i = 0; i < si->si_size; i++) |
| 82 | si->si_global_node_nums[i] = le16_to_cpu(disk_info[i]); |
| 83 | |
| 84 | spin_unlock(&si->si_lock); |
| 85 | } |
| 86 | |
| 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) |
| 91 | { |
| 92 | int status, i; |
| 93 | __le16 *disk_info = (__le16 *) si->si_bh->b_data; |
| 94 | |
| 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); |
| 99 | |
| 100 | status = ocfs2_write_block(osb, si->si_bh, si->si_inode); |
| 101 | if (status < 0) |
| 102 | mlog_errno(status); |
| 103 | |
| 104 | return status; |
| 105 | } |
| 106 | |
| 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) |
| 111 | { |
| 112 | int i; |
| 113 | s16 ret = OCFS2_INVALID_SLOT; |
| 114 | |
| 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; |
| 119 | } |
| 120 | } |
| 121 | return ret; |
| 122 | } |
| 123 | |
Sunil Mushran | baf4661 | 2007-06-18 17:00:24 -0700 | [diff] [blame] | 124 | static s16 __ocfs2_find_empty_slot(struct ocfs2_slot_info *si, s16 preferred) |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 125 | { |
| 126 | int i; |
| 127 | s16 ret = OCFS2_INVALID_SLOT; |
| 128 | |
Sunil Mushran | baf4661 | 2007-06-18 17:00:24 -0700 | [diff] [blame] | 129 | if (preferred >= 0 && preferred < si->si_num_slots) { |
| 130 | if (OCFS2_INVALID_SLOT == si->si_global_node_nums[preferred]) { |
| 131 | ret = preferred; |
| 132 | goto out; |
| 133 | } |
| 134 | } |
| 135 | |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 136 | for(i = 0; i < si->si_num_slots; i++) { |
| 137 | if (OCFS2_INVALID_SLOT == si->si_global_node_nums[i]) { |
| 138 | ret = (s16) i; |
| 139 | break; |
| 140 | } |
| 141 | } |
Sunil Mushran | baf4661 | 2007-06-18 17:00:24 -0700 | [diff] [blame] | 142 | out: |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | s16 ocfs2_node_num_to_slot(struct ocfs2_slot_info *si, |
| 147 | s16 global) |
| 148 | { |
| 149 | s16 ret; |
| 150 | |
| 151 | spin_lock(&si->si_lock); |
| 152 | ret = __ocfs2_node_num_to_slot(si, global); |
| 153 | spin_unlock(&si->si_lock); |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | static void __ocfs2_fill_slot(struct ocfs2_slot_info *si, |
| 158 | s16 slot_num, |
| 159 | s16 node_num) |
| 160 | { |
| 161 | BUG_ON(slot_num == OCFS2_INVALID_SLOT); |
| 162 | BUG_ON(slot_num >= si->si_num_slots); |
| 163 | BUG_ON((node_num != O2NM_INVALID_NODE_NUM) && |
| 164 | (node_num >= O2NM_MAX_NODES)); |
| 165 | |
| 166 | si->si_global_node_nums[slot_num] = node_num; |
| 167 | } |
| 168 | |
| 169 | void ocfs2_clear_slot(struct ocfs2_slot_info *si, |
| 170 | s16 slot_num) |
| 171 | { |
| 172 | spin_lock(&si->si_lock); |
| 173 | __ocfs2_fill_slot(si, slot_num, OCFS2_INVALID_SLOT); |
| 174 | spin_unlock(&si->si_lock); |
| 175 | } |
| 176 | |
| 177 | int ocfs2_init_slot_info(struct ocfs2_super *osb) |
| 178 | { |
| 179 | int status, i; |
| 180 | u64 blkno; |
| 181 | struct inode *inode = NULL; |
| 182 | struct buffer_head *bh = NULL; |
| 183 | struct ocfs2_slot_info *si; |
| 184 | |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 185 | si = kzalloc(sizeof(struct ocfs2_slot_info), GFP_KERNEL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 186 | if (!si) { |
| 187 | status = -ENOMEM; |
| 188 | mlog_errno(status); |
| 189 | goto bail; |
| 190 | } |
| 191 | |
| 192 | spin_lock_init(&si->si_lock); |
| 193 | si->si_num_slots = osb->max_slots; |
| 194 | si->si_size = OCFS2_MAX_SLOTS; |
| 195 | |
| 196 | for(i = 0; i < si->si_num_slots; i++) |
| 197 | si->si_global_node_nums[i] = OCFS2_INVALID_SLOT; |
| 198 | |
| 199 | inode = ocfs2_get_system_file_inode(osb, SLOT_MAP_SYSTEM_INODE, |
| 200 | OCFS2_INVALID_SLOT); |
| 201 | if (!inode) { |
| 202 | status = -EINVAL; |
| 203 | mlog_errno(status); |
| 204 | goto bail; |
| 205 | } |
| 206 | |
Mark Fasheh | 49cb8d2 | 2007-03-09 16:21:46 -0800 | [diff] [blame] | 207 | status = ocfs2_extent_map_get_blocks(inode, 0ULL, &blkno, NULL, NULL); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 208 | if (status < 0) { |
| 209 | mlog_errno(status); |
| 210 | goto bail; |
| 211 | } |
| 212 | |
| 213 | status = ocfs2_read_block(osb, blkno, &bh, 0, inode); |
| 214 | if (status < 0) { |
| 215 | mlog_errno(status); |
| 216 | goto bail; |
| 217 | } |
| 218 | |
| 219 | si->si_inode = inode; |
| 220 | si->si_bh = bh; |
| 221 | osb->slot_info = si; |
| 222 | bail: |
| 223 | if (status < 0 && si) |
| 224 | ocfs2_free_slot_info(si); |
| 225 | |
| 226 | return status; |
| 227 | } |
| 228 | |
| 229 | void ocfs2_free_slot_info(struct ocfs2_slot_info *si) |
| 230 | { |
| 231 | if (si->si_inode) |
| 232 | iput(si->si_inode); |
| 233 | if (si->si_bh) |
| 234 | brelse(si->si_bh); |
| 235 | kfree(si); |
| 236 | } |
| 237 | |
| 238 | int ocfs2_find_slot(struct ocfs2_super *osb) |
| 239 | { |
| 240 | int status; |
| 241 | s16 slot; |
| 242 | struct ocfs2_slot_info *si; |
| 243 | |
| 244 | mlog_entry_void(); |
| 245 | |
| 246 | si = osb->slot_info; |
| 247 | |
| 248 | ocfs2_update_slot_info(si); |
| 249 | |
| 250 | spin_lock(&si->si_lock); |
| 251 | /* search for ourselves first and take the slot if it already |
| 252 | * exists. Perhaps we need to mark this in a variable for our |
| 253 | * own journal recovery? Possibly not, though we certainly |
| 254 | * need to warn to the user */ |
| 255 | slot = __ocfs2_node_num_to_slot(si, osb->node_num); |
| 256 | if (slot == OCFS2_INVALID_SLOT) { |
| 257 | /* if no slot yet, then just take 1st available |
| 258 | * one. */ |
Sunil Mushran | baf4661 | 2007-06-18 17:00:24 -0700 | [diff] [blame] | 259 | slot = __ocfs2_find_empty_slot(si, osb->preferred_slot); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 260 | if (slot == OCFS2_INVALID_SLOT) { |
| 261 | spin_unlock(&si->si_lock); |
| 262 | mlog(ML_ERROR, "no free slots available!\n"); |
| 263 | status = -EINVAL; |
| 264 | goto bail; |
| 265 | } |
| 266 | } else |
| 267 | mlog(ML_NOTICE, "slot %d is already allocated to this node!\n", |
| 268 | slot); |
| 269 | |
| 270 | __ocfs2_fill_slot(si, slot, osb->node_num); |
| 271 | osb->slot_num = slot; |
| 272 | spin_unlock(&si->si_lock); |
| 273 | |
Mark Fasheh | e7607ab | 2006-04-27 17:53:22 -0700 | [diff] [blame] | 274 | mlog(0, "taking node slot %d\n", osb->slot_num); |
Mark Fasheh | ccd979b | 2005-12-15 14:31:24 -0800 | [diff] [blame] | 275 | |
| 276 | status = ocfs2_update_disk_slots(osb, si); |
| 277 | if (status < 0) |
| 278 | mlog_errno(status); |
| 279 | |
| 280 | bail: |
| 281 | mlog_exit(status); |
| 282 | return status; |
| 283 | } |
| 284 | |
| 285 | void ocfs2_put_slot(struct ocfs2_super *osb) |
| 286 | { |
| 287 | int status; |
| 288 | struct ocfs2_slot_info *si = osb->slot_info; |
| 289 | |
| 290 | if (!si) |
| 291 | return; |
| 292 | |
| 293 | ocfs2_update_slot_info(si); |
| 294 | |
| 295 | spin_lock(&si->si_lock); |
| 296 | __ocfs2_fill_slot(si, osb->slot_num, OCFS2_INVALID_SLOT); |
| 297 | osb->slot_num = OCFS2_INVALID_SLOT; |
| 298 | spin_unlock(&si->si_lock); |
| 299 | |
| 300 | status = ocfs2_update_disk_slots(osb, si); |
| 301 | if (status < 0) { |
| 302 | mlog_errno(status); |
| 303 | goto bail; |
| 304 | } |
| 305 | |
| 306 | bail: |
| 307 | osb->slot_info = NULL; |
| 308 | ocfs2_free_slot_info(si); |
| 309 | } |
| 310 | |