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