1ccd979bdSMark Fasheh /* -*- mode: c; c-basic-offset: 8; -*- 2ccd979bdSMark Fasheh * vim: noexpandtab sw=8 ts=8 sts=0: 3ccd979bdSMark Fasheh * 4ccd979bdSMark Fasheh * slot_map.c 5ccd979bdSMark Fasheh * 6ccd979bdSMark Fasheh * 7ccd979bdSMark Fasheh * 8ccd979bdSMark Fasheh * Copyright (C) 2002, 2004 Oracle. All rights reserved. 9ccd979bdSMark Fasheh * 10ccd979bdSMark Fasheh * This program is free software; you can redistribute it and/or 11ccd979bdSMark Fasheh * modify it under the terms of the GNU General Public 12ccd979bdSMark Fasheh * License as published by the Free Software Foundation; either 13ccd979bdSMark Fasheh * version 2 of the License, or (at your option) any later version. 14ccd979bdSMark Fasheh * 15ccd979bdSMark Fasheh * This program is distributed in the hope that it will be useful, 16ccd979bdSMark Fasheh * but WITHOUT ANY WARRANTY; without even the implied warranty of 17ccd979bdSMark Fasheh * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18ccd979bdSMark Fasheh * General Public License for more details. 19ccd979bdSMark Fasheh * 20ccd979bdSMark Fasheh * You should have received a copy of the GNU General Public 21ccd979bdSMark Fasheh * License along with this program; if not, write to the 22ccd979bdSMark Fasheh * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 23ccd979bdSMark Fasheh * Boston, MA 021110-1307, USA. 24ccd979bdSMark Fasheh */ 25ccd979bdSMark Fasheh 26ccd979bdSMark Fasheh #include <linux/types.h> 27ccd979bdSMark Fasheh #include <linux/slab.h> 28ccd979bdSMark Fasheh #include <linux/highmem.h> 29ccd979bdSMark Fasheh 30ccd979bdSMark Fasheh #include <cluster/masklog.h> 31ccd979bdSMark Fasheh 32ccd979bdSMark Fasheh #include "ocfs2.h" 33ccd979bdSMark Fasheh 34ccd979bdSMark Fasheh #include "dlmglue.h" 35ccd979bdSMark Fasheh #include "extent_map.h" 36ccd979bdSMark Fasheh #include "heartbeat.h" 37ccd979bdSMark Fasheh #include "inode.h" 38ccd979bdSMark Fasheh #include "slot_map.h" 39ccd979bdSMark Fasheh #include "super.h" 40ccd979bdSMark Fasheh #include "sysfile.h" 41a8731086STao Ma #include "ocfs2_trace.h" 42ccd979bdSMark Fasheh 43ccd979bdSMark Fasheh #include "buffer_head_io.h" 44ccd979bdSMark Fasheh 45fc881fa0SJoel Becker 46fc881fa0SJoel Becker struct ocfs2_slot { 47fc881fa0SJoel Becker int sl_valid; 48fc881fa0SJoel Becker unsigned int sl_node_num; 49fc881fa0SJoel Becker }; 50fc881fa0SJoel Becker 51d85b20e4SJoel Becker struct ocfs2_slot_info { 52386a2ef8SJoel Becker int si_extended; 53386a2ef8SJoel Becker int si_slots_per_block; 54d85b20e4SJoel Becker struct inode *si_inode; 551c8d9a6aSJoel Becker unsigned int si_blocks; 561c8d9a6aSJoel Becker struct buffer_head **si_bh; 57d85b20e4SJoel Becker unsigned int si_num_slots; 58*f402cf03SGustavo A. R. Silva struct ocfs2_slot si_slots[]; 59d85b20e4SJoel Becker }; 60d85b20e4SJoel Becker 61d85b20e4SJoel Becker 62fc881fa0SJoel Becker static int __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si, 63fc881fa0SJoel Becker unsigned int node_num); 64fc881fa0SJoel Becker 65fc881fa0SJoel Becker static void ocfs2_invalidate_slot(struct ocfs2_slot_info *si, 66fc881fa0SJoel Becker int slot_num) 67fc881fa0SJoel Becker { 68fc881fa0SJoel Becker BUG_ON((slot_num < 0) || (slot_num >= si->si_num_slots)); 69fc881fa0SJoel Becker si->si_slots[slot_num].sl_valid = 0; 70fc881fa0SJoel Becker } 71fc881fa0SJoel Becker 72fc881fa0SJoel Becker static void ocfs2_set_slot(struct ocfs2_slot_info *si, 73fc881fa0SJoel Becker int slot_num, unsigned int node_num) 74fc881fa0SJoel Becker { 75fc881fa0SJoel Becker BUG_ON((slot_num < 0) || (slot_num >= si->si_num_slots)); 76fc881fa0SJoel Becker 77fc881fa0SJoel Becker si->si_slots[slot_num].sl_valid = 1; 78fc881fa0SJoel Becker si->si_slots[slot_num].sl_node_num = node_num; 79fc881fa0SJoel Becker } 80ccd979bdSMark Fasheh 81386a2ef8SJoel Becker /* This version is for the extended slot map */ 82386a2ef8SJoel Becker static void ocfs2_update_slot_info_extended(struct ocfs2_slot_info *si) 83386a2ef8SJoel Becker { 84386a2ef8SJoel Becker int b, i, slotno; 85386a2ef8SJoel Becker struct ocfs2_slot_map_extended *se; 86386a2ef8SJoel Becker 87386a2ef8SJoel Becker slotno = 0; 88386a2ef8SJoel Becker for (b = 0; b < si->si_blocks; b++) { 89386a2ef8SJoel Becker se = (struct ocfs2_slot_map_extended *)si->si_bh[b]->b_data; 90386a2ef8SJoel Becker for (i = 0; 91386a2ef8SJoel Becker (i < si->si_slots_per_block) && 92386a2ef8SJoel Becker (slotno < si->si_num_slots); 93386a2ef8SJoel Becker i++, slotno++) { 94386a2ef8SJoel Becker if (se->se_slots[i].es_valid) 95386a2ef8SJoel Becker ocfs2_set_slot(si, slotno, 96386a2ef8SJoel Becker le32_to_cpu(se->se_slots[i].es_node_num)); 97386a2ef8SJoel Becker else 98386a2ef8SJoel Becker ocfs2_invalidate_slot(si, slotno); 99386a2ef8SJoel Becker } 100386a2ef8SJoel Becker } 101386a2ef8SJoel Becker } 102386a2ef8SJoel Becker 103d85b20e4SJoel Becker /* 104d85b20e4SJoel Becker * Post the slot information on disk into our slot_info struct. 105d85b20e4SJoel Becker * Must be protected by osb_lock. 106d85b20e4SJoel Becker */ 107386a2ef8SJoel Becker static void ocfs2_update_slot_info_old(struct ocfs2_slot_info *si) 108ccd979bdSMark Fasheh { 109ccd979bdSMark Fasheh int i; 110fb86b1f0SJoel Becker struct ocfs2_slot_map *sm; 111ccd979bdSMark Fasheh 112fb86b1f0SJoel Becker sm = (struct ocfs2_slot_map *)si->si_bh[0]->b_data; 113ccd979bdSMark Fasheh 114fc881fa0SJoel Becker for (i = 0; i < si->si_num_slots; i++) { 115fb86b1f0SJoel Becker if (le16_to_cpu(sm->sm_slots[i]) == (u16)OCFS2_INVALID_SLOT) 116fc881fa0SJoel Becker ocfs2_invalidate_slot(si, i); 117fc881fa0SJoel Becker else 118fb86b1f0SJoel Becker ocfs2_set_slot(si, i, le16_to_cpu(sm->sm_slots[i])); 119fc881fa0SJoel Becker } 120ccd979bdSMark Fasheh } 121ccd979bdSMark Fasheh 122386a2ef8SJoel Becker static void ocfs2_update_slot_info(struct ocfs2_slot_info *si) 123386a2ef8SJoel Becker { 124386a2ef8SJoel Becker /* 125386a2ef8SJoel Becker * The slot data will have been refreshed when ocfs2_super_lock 126386a2ef8SJoel Becker * was taken. 127386a2ef8SJoel Becker */ 128386a2ef8SJoel Becker if (si->si_extended) 129386a2ef8SJoel Becker ocfs2_update_slot_info_extended(si); 130386a2ef8SJoel Becker else 131386a2ef8SJoel Becker ocfs2_update_slot_info_old(si); 132386a2ef8SJoel Becker } 133386a2ef8SJoel Becker 1348e8a4603SMark Fasheh int ocfs2_refresh_slot_info(struct ocfs2_super *osb) 1358e8a4603SMark Fasheh { 1368e8a4603SMark Fasheh int ret; 1378e8a4603SMark Fasheh struct ocfs2_slot_info *si = osb->slot_info; 1388e8a4603SMark Fasheh 1398e8a4603SMark Fasheh if (si == NULL) 1408e8a4603SMark Fasheh return 0; 1418e8a4603SMark Fasheh 1421c8d9a6aSJoel Becker BUG_ON(si->si_blocks == 0); 1431c8d9a6aSJoel Becker BUG_ON(si->si_bh == NULL); 1441c8d9a6aSJoel Becker 145a8731086STao Ma trace_ocfs2_refresh_slot_info(si->si_blocks); 1461c8d9a6aSJoel Becker 1471c8d9a6aSJoel Becker /* 1481c8d9a6aSJoel Becker * We pass -1 as blocknr because we expect all of si->si_bh to 1491c8d9a6aSJoel Becker * be !NULL. Thus, ocfs2_read_blocks() will ignore blocknr. If 1501c8d9a6aSJoel Becker * this is not true, the read of -1 (UINT64_MAX) will fail. 1511c8d9a6aSJoel Becker */ 1528cb471e8SJoel Becker ret = ocfs2_read_blocks(INODE_CACHE(si->si_inode), -1, si->si_blocks, 1538cb471e8SJoel Becker si->si_bh, OCFS2_BH_IGNORE_CACHE, NULL); 154d85b20e4SJoel Becker if (ret == 0) { 155d85b20e4SJoel Becker spin_lock(&osb->osb_lock); 1568e8a4603SMark Fasheh ocfs2_update_slot_info(si); 157d85b20e4SJoel Becker spin_unlock(&osb->osb_lock); 158d85b20e4SJoel Becker } 1598e8a4603SMark Fasheh 1608e8a4603SMark Fasheh return ret; 1618e8a4603SMark Fasheh } 1628e8a4603SMark Fasheh 163ccd979bdSMark Fasheh /* post the our slot info stuff into it's destination bh and write it 164ccd979bdSMark Fasheh * out. */ 165386a2ef8SJoel Becker static void ocfs2_update_disk_slot_extended(struct ocfs2_slot_info *si, 166386a2ef8SJoel Becker int slot_num, 167386a2ef8SJoel Becker struct buffer_head **bh) 168ccd979bdSMark Fasheh { 169386a2ef8SJoel Becker int blkind = slot_num / si->si_slots_per_block; 170386a2ef8SJoel Becker int slotno = slot_num % si->si_slots_per_block; 171386a2ef8SJoel Becker struct ocfs2_slot_map_extended *se; 172386a2ef8SJoel Becker 173386a2ef8SJoel Becker BUG_ON(blkind >= si->si_blocks); 174386a2ef8SJoel Becker 175386a2ef8SJoel Becker se = (struct ocfs2_slot_map_extended *)si->si_bh[blkind]->b_data; 176386a2ef8SJoel Becker se->se_slots[slotno].es_valid = si->si_slots[slot_num].sl_valid; 177386a2ef8SJoel Becker if (si->si_slots[slot_num].sl_valid) 178386a2ef8SJoel Becker se->se_slots[slotno].es_node_num = 179386a2ef8SJoel Becker cpu_to_le32(si->si_slots[slot_num].sl_node_num); 180386a2ef8SJoel Becker *bh = si->si_bh[blkind]; 181386a2ef8SJoel Becker } 182386a2ef8SJoel Becker 183386a2ef8SJoel Becker static void ocfs2_update_disk_slot_old(struct ocfs2_slot_info *si, 184386a2ef8SJoel Becker int slot_num, 185386a2ef8SJoel Becker struct buffer_head **bh) 186386a2ef8SJoel Becker { 187386a2ef8SJoel Becker int i; 188fb86b1f0SJoel Becker struct ocfs2_slot_map *sm; 189ccd979bdSMark Fasheh 190fb86b1f0SJoel Becker sm = (struct ocfs2_slot_map *)si->si_bh[0]->b_data; 191fc881fa0SJoel Becker for (i = 0; i < si->si_num_slots; i++) { 192fc881fa0SJoel Becker if (si->si_slots[i].sl_valid) 193fb86b1f0SJoel Becker sm->sm_slots[i] = 194fc881fa0SJoel Becker cpu_to_le16(si->si_slots[i].sl_node_num); 195fc881fa0SJoel Becker else 196fb86b1f0SJoel Becker sm->sm_slots[i] = cpu_to_le16(OCFS2_INVALID_SLOT); 197fc881fa0SJoel Becker } 198386a2ef8SJoel Becker *bh = si->si_bh[0]; 199386a2ef8SJoel Becker } 200386a2ef8SJoel Becker 201386a2ef8SJoel Becker static int ocfs2_update_disk_slot(struct ocfs2_super *osb, 202386a2ef8SJoel Becker struct ocfs2_slot_info *si, 203386a2ef8SJoel Becker int slot_num) 204386a2ef8SJoel Becker { 205386a2ef8SJoel Becker int status; 206386a2ef8SJoel Becker struct buffer_head *bh; 207386a2ef8SJoel Becker 208386a2ef8SJoel Becker spin_lock(&osb->osb_lock); 209386a2ef8SJoel Becker if (si->si_extended) 210386a2ef8SJoel Becker ocfs2_update_disk_slot_extended(si, slot_num, &bh); 211386a2ef8SJoel Becker else 212386a2ef8SJoel Becker ocfs2_update_disk_slot_old(si, slot_num, &bh); 213d85b20e4SJoel Becker spin_unlock(&osb->osb_lock); 214ccd979bdSMark Fasheh 2158cb471e8SJoel Becker status = ocfs2_write_block(osb, bh, INODE_CACHE(si->si_inode)); 216ccd979bdSMark Fasheh if (status < 0) 217ccd979bdSMark Fasheh mlog_errno(status); 218ccd979bdSMark Fasheh 219ccd979bdSMark Fasheh return status; 220ccd979bdSMark Fasheh } 221ccd979bdSMark Fasheh 2221c8d9a6aSJoel Becker /* 2231c8d9a6aSJoel Becker * Calculate how many bytes are needed by the slot map. Returns 2241c8d9a6aSJoel Becker * an error if the slot map file is too small. 2251c8d9a6aSJoel Becker */ 2261c8d9a6aSJoel Becker static int ocfs2_slot_map_physical_size(struct ocfs2_super *osb, 2271c8d9a6aSJoel Becker struct inode *inode, 2281c8d9a6aSJoel Becker unsigned long long *bytes) 2291c8d9a6aSJoel Becker { 2301c8d9a6aSJoel Becker unsigned long long bytes_needed; 2311c8d9a6aSJoel Becker 232386a2ef8SJoel Becker if (ocfs2_uses_extended_slot_map(osb)) { 233386a2ef8SJoel Becker bytes_needed = osb->max_slots * 234386a2ef8SJoel Becker sizeof(struct ocfs2_extended_slot); 235386a2ef8SJoel Becker } else { 2361c8d9a6aSJoel Becker bytes_needed = osb->max_slots * sizeof(__le16); 237386a2ef8SJoel Becker } 2381c8d9a6aSJoel Becker if (bytes_needed > i_size_read(inode)) { 2391c8d9a6aSJoel Becker mlog(ML_ERROR, 2401c8d9a6aSJoel Becker "Slot map file is too small! (size %llu, needed %llu)\n", 2411c8d9a6aSJoel Becker i_size_read(inode), bytes_needed); 2421c8d9a6aSJoel Becker return -ENOSPC; 2431c8d9a6aSJoel Becker } 2441c8d9a6aSJoel Becker 2451c8d9a6aSJoel Becker *bytes = bytes_needed; 2461c8d9a6aSJoel Becker return 0; 2471c8d9a6aSJoel Becker } 2481c8d9a6aSJoel Becker 249fc881fa0SJoel Becker /* try to find global node in the slot info. Returns -ENOENT 250fc881fa0SJoel Becker * if nothing is found. */ 251fc881fa0SJoel Becker static int __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si, 252fc881fa0SJoel Becker unsigned int node_num) 253ccd979bdSMark Fasheh { 254fc881fa0SJoel Becker int i, ret = -ENOENT; 255ccd979bdSMark Fasheh 256ccd979bdSMark Fasheh for(i = 0; i < si->si_num_slots; i++) { 257fc881fa0SJoel Becker if (si->si_slots[i].sl_valid && 258fc881fa0SJoel Becker (node_num == si->si_slots[i].sl_node_num)) { 259fc881fa0SJoel Becker ret = i; 260ccd979bdSMark Fasheh break; 261ccd979bdSMark Fasheh } 262ccd979bdSMark Fasheh } 263fc881fa0SJoel Becker 264ccd979bdSMark Fasheh return ret; 265ccd979bdSMark Fasheh } 266ccd979bdSMark Fasheh 267fc881fa0SJoel Becker static int __ocfs2_find_empty_slot(struct ocfs2_slot_info *si, 268fc881fa0SJoel Becker int preferred) 269ccd979bdSMark Fasheh { 270fc881fa0SJoel Becker int i, ret = -ENOSPC; 271ccd979bdSMark Fasheh 272fc881fa0SJoel Becker if ((preferred >= 0) && (preferred < si->si_num_slots)) { 273fc881fa0SJoel Becker if (!si->si_slots[preferred].sl_valid) { 274baf4661aSSunil Mushran ret = preferred; 275baf4661aSSunil Mushran goto out; 276baf4661aSSunil Mushran } 277baf4661aSSunil Mushran } 278baf4661aSSunil Mushran 279ccd979bdSMark Fasheh for(i = 0; i < si->si_num_slots; i++) { 280fc881fa0SJoel Becker if (!si->si_slots[i].sl_valid) { 281fc881fa0SJoel Becker ret = i; 282ccd979bdSMark Fasheh break; 283ccd979bdSMark Fasheh } 284ccd979bdSMark Fasheh } 285baf4661aSSunil Mushran out: 286ccd979bdSMark Fasheh return ret; 287ccd979bdSMark Fasheh } 288ccd979bdSMark Fasheh 289d85b20e4SJoel Becker int ocfs2_node_num_to_slot(struct ocfs2_super *osb, unsigned int node_num) 290ccd979bdSMark Fasheh { 291fc881fa0SJoel Becker int slot; 292d85b20e4SJoel Becker struct ocfs2_slot_info *si = osb->slot_info; 293ccd979bdSMark Fasheh 294d85b20e4SJoel Becker spin_lock(&osb->osb_lock); 295d85b20e4SJoel Becker slot = __ocfs2_node_num_to_slot(si, node_num); 296d85b20e4SJoel Becker spin_unlock(&osb->osb_lock); 297d85b20e4SJoel Becker 298d85b20e4SJoel Becker return slot; 299d85b20e4SJoel Becker } 300d85b20e4SJoel Becker 301d85b20e4SJoel Becker int ocfs2_slot_to_node_num_locked(struct ocfs2_super *osb, int slot_num, 302d85b20e4SJoel Becker unsigned int *node_num) 303d85b20e4SJoel Becker { 304d85b20e4SJoel Becker struct ocfs2_slot_info *si = osb->slot_info; 305d85b20e4SJoel Becker 306d85b20e4SJoel Becker assert_spin_locked(&osb->osb_lock); 307d85b20e4SJoel Becker 308d85b20e4SJoel Becker BUG_ON(slot_num < 0); 309519a2861SDan Carpenter BUG_ON(slot_num >= osb->max_slots); 310d85b20e4SJoel Becker 311fc881fa0SJoel Becker if (!si->si_slots[slot_num].sl_valid) 312d85b20e4SJoel Becker return -ENOENT; 313d85b20e4SJoel Becker 314fc881fa0SJoel Becker *node_num = si->si_slots[slot_num].sl_node_num; 315d85b20e4SJoel Becker return 0; 316ccd979bdSMark Fasheh } 317ccd979bdSMark Fasheh 3188e8a4603SMark Fasheh static void __ocfs2_free_slot_info(struct ocfs2_slot_info *si) 3198e8a4603SMark Fasheh { 3201c8d9a6aSJoel Becker unsigned int i; 3211c8d9a6aSJoel Becker 3228e8a4603SMark Fasheh if (si == NULL) 3238e8a4603SMark Fasheh return; 3248e8a4603SMark Fasheh 3258e8a4603SMark Fasheh iput(si->si_inode); 3261c8d9a6aSJoel Becker if (si->si_bh) { 3271c8d9a6aSJoel Becker for (i = 0; i < si->si_blocks; i++) { 3281c8d9a6aSJoel Becker if (si->si_bh[i]) { 3291c8d9a6aSJoel Becker brelse(si->si_bh[i]); 3301c8d9a6aSJoel Becker si->si_bh[i] = NULL; 3311c8d9a6aSJoel Becker } 3321c8d9a6aSJoel Becker } 3331c8d9a6aSJoel Becker kfree(si->si_bh); 3341c8d9a6aSJoel Becker } 3358e8a4603SMark Fasheh 3368e8a4603SMark Fasheh kfree(si); 3378e8a4603SMark Fasheh } 3388e8a4603SMark Fasheh 339fc881fa0SJoel Becker int ocfs2_clear_slot(struct ocfs2_super *osb, int slot_num) 340ccd979bdSMark Fasheh { 3418e8a4603SMark Fasheh struct ocfs2_slot_info *si = osb->slot_info; 3428e8a4603SMark Fasheh 3438e8a4603SMark Fasheh if (si == NULL) 3448e8a4603SMark Fasheh return 0; 3458e8a4603SMark Fasheh 346d85b20e4SJoel Becker spin_lock(&osb->osb_lock); 347fc881fa0SJoel Becker ocfs2_invalidate_slot(si, slot_num); 348d85b20e4SJoel Becker spin_unlock(&osb->osb_lock); 3498e8a4603SMark Fasheh 350386a2ef8SJoel Becker return ocfs2_update_disk_slot(osb, osb->slot_info, slot_num); 351ccd979bdSMark Fasheh } 352ccd979bdSMark Fasheh 3531c8d9a6aSJoel Becker static int ocfs2_map_slot_buffers(struct ocfs2_super *osb, 3541c8d9a6aSJoel Becker struct ocfs2_slot_info *si) 3551c8d9a6aSJoel Becker { 3561c8d9a6aSJoel Becker int status = 0; 3571c8d9a6aSJoel Becker u64 blkno; 358f30d44f3SPoyo VL unsigned long long blocks, bytes = 0; 3591c8d9a6aSJoel Becker unsigned int i; 3601c8d9a6aSJoel Becker struct buffer_head *bh; 3611c8d9a6aSJoel Becker 3621c8d9a6aSJoel Becker status = ocfs2_slot_map_physical_size(osb, si->si_inode, &bytes); 3631c8d9a6aSJoel Becker if (status) 3641c8d9a6aSJoel Becker goto bail; 3651c8d9a6aSJoel Becker 3661c8d9a6aSJoel Becker blocks = ocfs2_blocks_for_bytes(si->si_inode->i_sb, bytes); 3671c8d9a6aSJoel Becker BUG_ON(blocks > UINT_MAX); 3681c8d9a6aSJoel Becker si->si_blocks = blocks; 3691c8d9a6aSJoel Becker if (!si->si_blocks) 3701c8d9a6aSJoel Becker goto bail; 3711c8d9a6aSJoel Becker 372386a2ef8SJoel Becker if (si->si_extended) 373386a2ef8SJoel Becker si->si_slots_per_block = 374386a2ef8SJoel Becker (osb->sb->s_blocksize / 375386a2ef8SJoel Becker sizeof(struct ocfs2_extended_slot)); 376386a2ef8SJoel Becker else 377386a2ef8SJoel Becker si->si_slots_per_block = osb->sb->s_blocksize / sizeof(__le16); 378386a2ef8SJoel Becker 379386a2ef8SJoel Becker /* The size checks above should ensure this */ 380386a2ef8SJoel Becker BUG_ON((osb->max_slots / si->si_slots_per_block) > blocks); 381386a2ef8SJoel Becker 382a8731086STao Ma trace_ocfs2_map_slot_buffers(bytes, si->si_blocks); 3831c8d9a6aSJoel Becker 3841b7f8ba6SFabian Frederick si->si_bh = kcalloc(si->si_blocks, sizeof(struct buffer_head *), 3851c8d9a6aSJoel Becker GFP_KERNEL); 3861c8d9a6aSJoel Becker if (!si->si_bh) { 3871c8d9a6aSJoel Becker status = -ENOMEM; 3881c8d9a6aSJoel Becker mlog_errno(status); 3891c8d9a6aSJoel Becker goto bail; 3901c8d9a6aSJoel Becker } 3911c8d9a6aSJoel Becker 3921c8d9a6aSJoel Becker for (i = 0; i < si->si_blocks; i++) { 3931c8d9a6aSJoel Becker status = ocfs2_extent_map_get_blocks(si->si_inode, i, 3941c8d9a6aSJoel Becker &blkno, NULL, NULL); 3951c8d9a6aSJoel Becker if (status < 0) { 3961c8d9a6aSJoel Becker mlog_errno(status); 3971c8d9a6aSJoel Becker goto bail; 3981c8d9a6aSJoel Becker } 3991c8d9a6aSJoel Becker 400a8731086STao Ma trace_ocfs2_map_slot_buffers_block((unsigned long long)blkno, i); 4011c8d9a6aSJoel Becker 4021c8d9a6aSJoel Becker bh = NULL; /* Acquire a fresh bh */ 4038cb471e8SJoel Becker status = ocfs2_read_blocks(INODE_CACHE(si->si_inode), blkno, 4048cb471e8SJoel Becker 1, &bh, OCFS2_BH_IGNORE_CACHE, NULL); 4051c8d9a6aSJoel Becker if (status < 0) { 4061c8d9a6aSJoel Becker mlog_errno(status); 4071c8d9a6aSJoel Becker goto bail; 4081c8d9a6aSJoel Becker } 4091c8d9a6aSJoel Becker 4101c8d9a6aSJoel Becker si->si_bh[i] = bh; 4111c8d9a6aSJoel Becker } 4121c8d9a6aSJoel Becker 4131c8d9a6aSJoel Becker bail: 4141c8d9a6aSJoel Becker return status; 4151c8d9a6aSJoel Becker } 4161c8d9a6aSJoel Becker 417ccd979bdSMark Fasheh int ocfs2_init_slot_info(struct ocfs2_super *osb) 418ccd979bdSMark Fasheh { 419fc881fa0SJoel Becker int status; 420ccd979bdSMark Fasheh struct inode *inode = NULL; 421ccd979bdSMark Fasheh struct ocfs2_slot_info *si; 422ccd979bdSMark Fasheh 423*f402cf03SGustavo A. R. Silva si = kzalloc(struct_size(si, si_slots, osb->max_slots), GFP_KERNEL); 424ccd979bdSMark Fasheh if (!si) { 425ccd979bdSMark Fasheh status = -ENOMEM; 426ccd979bdSMark Fasheh mlog_errno(status); 427bb34ed21SMarkus Elfring return status; 428ccd979bdSMark Fasheh } 429ccd979bdSMark Fasheh 430386a2ef8SJoel Becker si->si_extended = ocfs2_uses_extended_slot_map(osb); 431ccd979bdSMark Fasheh si->si_num_slots = osb->max_slots; 432ccd979bdSMark Fasheh 433ccd979bdSMark Fasheh inode = ocfs2_get_system_file_inode(osb, SLOT_MAP_SYSTEM_INODE, 434ccd979bdSMark Fasheh OCFS2_INVALID_SLOT); 435ccd979bdSMark Fasheh if (!inode) { 436ccd979bdSMark Fasheh status = -EINVAL; 437ccd979bdSMark Fasheh mlog_errno(status); 438ccd979bdSMark Fasheh goto bail; 439ccd979bdSMark Fasheh } 440ccd979bdSMark Fasheh 441ccd979bdSMark Fasheh si->si_inode = inode; 4421c8d9a6aSJoel Becker status = ocfs2_map_slot_buffers(osb, si); 4431c8d9a6aSJoel Becker if (status < 0) { 4441c8d9a6aSJoel Becker mlog_errno(status); 4451c8d9a6aSJoel Becker goto bail; 4461c8d9a6aSJoel Becker } 4471c8d9a6aSJoel Becker 448d85b20e4SJoel Becker osb->slot_info = (struct ocfs2_slot_info *)si; 449ccd979bdSMark Fasheh bail: 450fd90d4dfSMarkus Elfring if (status < 0) 4518e8a4603SMark Fasheh __ocfs2_free_slot_info(si); 452ccd979bdSMark Fasheh 453ccd979bdSMark Fasheh return status; 454ccd979bdSMark Fasheh } 455ccd979bdSMark Fasheh 4568e8a4603SMark Fasheh void ocfs2_free_slot_info(struct ocfs2_super *osb) 457ccd979bdSMark Fasheh { 4588e8a4603SMark Fasheh struct ocfs2_slot_info *si = osb->slot_info; 4598e8a4603SMark Fasheh 4608e8a4603SMark Fasheh osb->slot_info = NULL; 4618e8a4603SMark Fasheh __ocfs2_free_slot_info(si); 462ccd979bdSMark Fasheh } 463ccd979bdSMark Fasheh 464ccd979bdSMark Fasheh int ocfs2_find_slot(struct ocfs2_super *osb) 465ccd979bdSMark Fasheh { 466ccd979bdSMark Fasheh int status; 467fc881fa0SJoel Becker int slot; 468ccd979bdSMark Fasheh struct ocfs2_slot_info *si; 469ccd979bdSMark Fasheh 470ccd979bdSMark Fasheh si = osb->slot_info; 471ccd979bdSMark Fasheh 472d85b20e4SJoel Becker spin_lock(&osb->osb_lock); 473ccd979bdSMark Fasheh ocfs2_update_slot_info(si); 474ccd979bdSMark Fasheh 475ccd979bdSMark Fasheh /* search for ourselves first and take the slot if it already 476ccd979bdSMark Fasheh * exists. Perhaps we need to mark this in a variable for our 477ccd979bdSMark Fasheh * own journal recovery? Possibly not, though we certainly 478ccd979bdSMark Fasheh * need to warn to the user */ 479ccd979bdSMark Fasheh slot = __ocfs2_node_num_to_slot(si, osb->node_num); 480fc881fa0SJoel Becker if (slot < 0) { 481ccd979bdSMark Fasheh /* if no slot yet, then just take 1st available 482ccd979bdSMark Fasheh * one. */ 483baf4661aSSunil Mushran slot = __ocfs2_find_empty_slot(si, osb->preferred_slot); 484fc881fa0SJoel Becker if (slot < 0) { 485d85b20e4SJoel Becker spin_unlock(&osb->osb_lock); 486ccd979bdSMark Fasheh mlog(ML_ERROR, "no free slots available!\n"); 487ccd979bdSMark Fasheh status = -EINVAL; 488ccd979bdSMark Fasheh goto bail; 489ccd979bdSMark Fasheh } 490ccd979bdSMark Fasheh } else 491619c200dSSunil Mushran printk(KERN_INFO "ocfs2: Slot %d on device (%s) was already " 492619c200dSSunil Mushran "allocated to this node!\n", slot, osb->dev_str); 493ccd979bdSMark Fasheh 494fc881fa0SJoel Becker ocfs2_set_slot(si, slot, osb->node_num); 495ccd979bdSMark Fasheh osb->slot_num = slot; 496d85b20e4SJoel Becker spin_unlock(&osb->osb_lock); 497ccd979bdSMark Fasheh 498a8731086STao Ma trace_ocfs2_find_slot(osb->slot_num); 499ccd979bdSMark Fasheh 500386a2ef8SJoel Becker status = ocfs2_update_disk_slot(osb, si, osb->slot_num); 5011247017fSjiangyiwen if (status < 0) { 502ccd979bdSMark Fasheh mlog_errno(status); 5031247017fSjiangyiwen /* 5041247017fSjiangyiwen * if write block failed, invalidate slot to avoid overwrite 5051247017fSjiangyiwen * slot during dismount in case another node rightly has mounted 5061247017fSjiangyiwen */ 5071247017fSjiangyiwen spin_lock(&osb->osb_lock); 5081247017fSjiangyiwen ocfs2_invalidate_slot(si, osb->slot_num); 5091247017fSjiangyiwen osb->slot_num = OCFS2_INVALID_SLOT; 5101247017fSjiangyiwen spin_unlock(&osb->osb_lock); 5111247017fSjiangyiwen } 512ccd979bdSMark Fasheh 513ccd979bdSMark Fasheh bail: 514ccd979bdSMark Fasheh return status; 515ccd979bdSMark Fasheh } 516ccd979bdSMark Fasheh 517ccd979bdSMark Fasheh void ocfs2_put_slot(struct ocfs2_super *osb) 518ccd979bdSMark Fasheh { 519386a2ef8SJoel Becker int status, slot_num; 520ccd979bdSMark Fasheh struct ocfs2_slot_info *si = osb->slot_info; 521ccd979bdSMark Fasheh 522ccd979bdSMark Fasheh if (!si) 523ccd979bdSMark Fasheh return; 524ccd979bdSMark Fasheh 525d85b20e4SJoel Becker spin_lock(&osb->osb_lock); 526ccd979bdSMark Fasheh ocfs2_update_slot_info(si); 527ccd979bdSMark Fasheh 528386a2ef8SJoel Becker slot_num = osb->slot_num; 529fc881fa0SJoel Becker ocfs2_invalidate_slot(si, osb->slot_num); 530ccd979bdSMark Fasheh osb->slot_num = OCFS2_INVALID_SLOT; 531d85b20e4SJoel Becker spin_unlock(&osb->osb_lock); 532ccd979bdSMark Fasheh 533386a2ef8SJoel Becker status = ocfs2_update_disk_slot(osb, si, slot_num); 5348f9b1802SGuozhonghua if (status < 0) 535ccd979bdSMark Fasheh mlog_errno(status); 536ccd979bdSMark Fasheh 5378e8a4603SMark Fasheh ocfs2_free_slot_info(osb); 538ccd979bdSMark Fasheh } 539