xref: /openbmc/linux/fs/ocfs2/slot_map.c (revision 8cb471e8f82506937fe5e2e9fb0bf90f6b1f1170)
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 #define MLOG_MASK_PREFIX ML_SUPER
31ccd979bdSMark Fasheh #include <cluster/masklog.h>
32ccd979bdSMark Fasheh 
33ccd979bdSMark Fasheh #include "ocfs2.h"
34ccd979bdSMark Fasheh 
35ccd979bdSMark Fasheh #include "dlmglue.h"
36ccd979bdSMark Fasheh #include "extent_map.h"
37ccd979bdSMark Fasheh #include "heartbeat.h"
38ccd979bdSMark Fasheh #include "inode.h"
39ccd979bdSMark Fasheh #include "slot_map.h"
40ccd979bdSMark Fasheh #include "super.h"
41ccd979bdSMark Fasheh #include "sysfile.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;
58fc881fa0SJoel Becker 	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 
1451c8d9a6aSJoel Becker 	mlog(0, "Refreshing slot map, reading %u block(s)\n",
1461c8d9a6aSJoel Becker 	     si->si_blocks);
1471c8d9a6aSJoel Becker 
1481c8d9a6aSJoel Becker 	/*
1491c8d9a6aSJoel Becker 	 * We pass -1 as blocknr because we expect all of si->si_bh to
1501c8d9a6aSJoel Becker 	 * be !NULL.  Thus, ocfs2_read_blocks() will ignore blocknr.  If
1511c8d9a6aSJoel Becker 	 * this is not true, the read of -1 (UINT64_MAX) will fail.
1521c8d9a6aSJoel Becker 	 */
153*8cb471e8SJoel Becker 	ret = ocfs2_read_blocks(INODE_CACHE(si->si_inode), -1, si->si_blocks,
154*8cb471e8SJoel Becker 				si->si_bh, OCFS2_BH_IGNORE_CACHE, NULL);
155d85b20e4SJoel Becker 	if (ret == 0) {
156d85b20e4SJoel Becker 		spin_lock(&osb->osb_lock);
1578e8a4603SMark Fasheh 		ocfs2_update_slot_info(si);
158d85b20e4SJoel Becker 		spin_unlock(&osb->osb_lock);
159d85b20e4SJoel Becker 	}
1608e8a4603SMark Fasheh 
1618e8a4603SMark Fasheh 	return ret;
1628e8a4603SMark Fasheh }
1638e8a4603SMark Fasheh 
164ccd979bdSMark Fasheh /* post the our slot info stuff into it's destination bh and write it
165ccd979bdSMark Fasheh  * out. */
166386a2ef8SJoel Becker static void ocfs2_update_disk_slot_extended(struct ocfs2_slot_info *si,
167386a2ef8SJoel Becker 					    int slot_num,
168386a2ef8SJoel Becker 					    struct buffer_head **bh)
169ccd979bdSMark Fasheh {
170386a2ef8SJoel Becker 	int blkind = slot_num / si->si_slots_per_block;
171386a2ef8SJoel Becker 	int slotno = slot_num % si->si_slots_per_block;
172386a2ef8SJoel Becker 	struct ocfs2_slot_map_extended *se;
173386a2ef8SJoel Becker 
174386a2ef8SJoel Becker 	BUG_ON(blkind >= si->si_blocks);
175386a2ef8SJoel Becker 
176386a2ef8SJoel Becker 	se = (struct ocfs2_slot_map_extended *)si->si_bh[blkind]->b_data;
177386a2ef8SJoel Becker 	se->se_slots[slotno].es_valid = si->si_slots[slot_num].sl_valid;
178386a2ef8SJoel Becker 	if (si->si_slots[slot_num].sl_valid)
179386a2ef8SJoel Becker 		se->se_slots[slotno].es_node_num =
180386a2ef8SJoel Becker 			cpu_to_le32(si->si_slots[slot_num].sl_node_num);
181386a2ef8SJoel Becker 	*bh = si->si_bh[blkind];
182386a2ef8SJoel Becker }
183386a2ef8SJoel Becker 
184386a2ef8SJoel Becker static void ocfs2_update_disk_slot_old(struct ocfs2_slot_info *si,
185386a2ef8SJoel Becker 				       int slot_num,
186386a2ef8SJoel Becker 				       struct buffer_head **bh)
187386a2ef8SJoel Becker {
188386a2ef8SJoel Becker 	int i;
189fb86b1f0SJoel Becker 	struct ocfs2_slot_map *sm;
190ccd979bdSMark Fasheh 
191fb86b1f0SJoel Becker 	sm = (struct ocfs2_slot_map *)si->si_bh[0]->b_data;
192fc881fa0SJoel Becker 	for (i = 0; i < si->si_num_slots; i++) {
193fc881fa0SJoel Becker 		if (si->si_slots[i].sl_valid)
194fb86b1f0SJoel Becker 			sm->sm_slots[i] =
195fc881fa0SJoel Becker 				cpu_to_le16(si->si_slots[i].sl_node_num);
196fc881fa0SJoel Becker 		else
197fb86b1f0SJoel Becker 			sm->sm_slots[i] = cpu_to_le16(OCFS2_INVALID_SLOT);
198fc881fa0SJoel Becker 	}
199386a2ef8SJoel Becker 	*bh = si->si_bh[0];
200386a2ef8SJoel Becker }
201386a2ef8SJoel Becker 
202386a2ef8SJoel Becker static int ocfs2_update_disk_slot(struct ocfs2_super *osb,
203386a2ef8SJoel Becker 				  struct ocfs2_slot_info *si,
204386a2ef8SJoel Becker 				  int slot_num)
205386a2ef8SJoel Becker {
206386a2ef8SJoel Becker 	int status;
207386a2ef8SJoel Becker 	struct buffer_head *bh;
208386a2ef8SJoel Becker 
209386a2ef8SJoel Becker 	spin_lock(&osb->osb_lock);
210386a2ef8SJoel Becker 	if (si->si_extended)
211386a2ef8SJoel Becker 		ocfs2_update_disk_slot_extended(si, slot_num, &bh);
212386a2ef8SJoel Becker 	else
213386a2ef8SJoel Becker 		ocfs2_update_disk_slot_old(si, slot_num, &bh);
214d85b20e4SJoel Becker 	spin_unlock(&osb->osb_lock);
215ccd979bdSMark Fasheh 
216*8cb471e8SJoel Becker 	status = ocfs2_write_block(osb, bh, INODE_CACHE(si->si_inode));
217ccd979bdSMark Fasheh 	if (status < 0)
218ccd979bdSMark Fasheh 		mlog_errno(status);
219ccd979bdSMark Fasheh 
220ccd979bdSMark Fasheh 	return status;
221ccd979bdSMark Fasheh }
222ccd979bdSMark Fasheh 
2231c8d9a6aSJoel Becker /*
2241c8d9a6aSJoel Becker  * Calculate how many bytes are needed by the slot map.  Returns
2251c8d9a6aSJoel Becker  * an error if the slot map file is too small.
2261c8d9a6aSJoel Becker  */
2271c8d9a6aSJoel Becker static int ocfs2_slot_map_physical_size(struct ocfs2_super *osb,
2281c8d9a6aSJoel Becker 					struct inode *inode,
2291c8d9a6aSJoel Becker 					unsigned long long *bytes)
2301c8d9a6aSJoel Becker {
2311c8d9a6aSJoel Becker 	unsigned long long bytes_needed;
2321c8d9a6aSJoel Becker 
233386a2ef8SJoel Becker 	if (ocfs2_uses_extended_slot_map(osb)) {
234386a2ef8SJoel Becker 		bytes_needed = osb->max_slots *
235386a2ef8SJoel Becker 			sizeof(struct ocfs2_extended_slot);
236386a2ef8SJoel Becker 	} else {
2371c8d9a6aSJoel Becker 		bytes_needed = osb->max_slots * sizeof(__le16);
238386a2ef8SJoel Becker 	}
2391c8d9a6aSJoel Becker 	if (bytes_needed > i_size_read(inode)) {
2401c8d9a6aSJoel Becker 		mlog(ML_ERROR,
2411c8d9a6aSJoel Becker 		     "Slot map file is too small!  (size %llu, needed %llu)\n",
2421c8d9a6aSJoel Becker 		     i_size_read(inode), bytes_needed);
2431c8d9a6aSJoel Becker 		return -ENOSPC;
2441c8d9a6aSJoel Becker 	}
2451c8d9a6aSJoel Becker 
2461c8d9a6aSJoel Becker 	*bytes = bytes_needed;
2471c8d9a6aSJoel Becker 	return 0;
2481c8d9a6aSJoel Becker }
2491c8d9a6aSJoel Becker 
250fc881fa0SJoel Becker /* try to find global node in the slot info. Returns -ENOENT
251fc881fa0SJoel Becker  * if nothing is found. */
252fc881fa0SJoel Becker static int __ocfs2_node_num_to_slot(struct ocfs2_slot_info *si,
253fc881fa0SJoel Becker 				    unsigned int node_num)
254ccd979bdSMark Fasheh {
255fc881fa0SJoel Becker 	int i, ret = -ENOENT;
256ccd979bdSMark Fasheh 
257ccd979bdSMark Fasheh 	for(i = 0; i < si->si_num_slots; i++) {
258fc881fa0SJoel Becker 		if (si->si_slots[i].sl_valid &&
259fc881fa0SJoel Becker 		    (node_num == si->si_slots[i].sl_node_num)) {
260fc881fa0SJoel Becker 			ret = i;
261ccd979bdSMark Fasheh 			break;
262ccd979bdSMark Fasheh 		}
263ccd979bdSMark Fasheh 	}
264fc881fa0SJoel Becker 
265ccd979bdSMark Fasheh 	return ret;
266ccd979bdSMark Fasheh }
267ccd979bdSMark Fasheh 
268fc881fa0SJoel Becker static int __ocfs2_find_empty_slot(struct ocfs2_slot_info *si,
269fc881fa0SJoel Becker 				   int preferred)
270ccd979bdSMark Fasheh {
271fc881fa0SJoel Becker 	int i, ret = -ENOSPC;
272ccd979bdSMark Fasheh 
273fc881fa0SJoel Becker 	if ((preferred >= 0) && (preferred < si->si_num_slots)) {
274fc881fa0SJoel Becker 		if (!si->si_slots[preferred].sl_valid) {
275baf4661aSSunil Mushran 			ret = preferred;
276baf4661aSSunil Mushran 			goto out;
277baf4661aSSunil Mushran 		}
278baf4661aSSunil Mushran 	}
279baf4661aSSunil Mushran 
280ccd979bdSMark Fasheh 	for(i = 0; i < si->si_num_slots; i++) {
281fc881fa0SJoel Becker 		if (!si->si_slots[i].sl_valid) {
282fc881fa0SJoel Becker 			ret = i;
283ccd979bdSMark Fasheh 			break;
284ccd979bdSMark Fasheh 		}
285ccd979bdSMark Fasheh 	}
286baf4661aSSunil Mushran out:
287ccd979bdSMark Fasheh 	return ret;
288ccd979bdSMark Fasheh }
289ccd979bdSMark Fasheh 
290d85b20e4SJoel Becker int ocfs2_node_num_to_slot(struct ocfs2_super *osb, unsigned int node_num)
291ccd979bdSMark Fasheh {
292fc881fa0SJoel Becker 	int slot;
293d85b20e4SJoel Becker 	struct ocfs2_slot_info *si = osb->slot_info;
294ccd979bdSMark Fasheh 
295d85b20e4SJoel Becker 	spin_lock(&osb->osb_lock);
296d85b20e4SJoel Becker 	slot = __ocfs2_node_num_to_slot(si, node_num);
297d85b20e4SJoel Becker 	spin_unlock(&osb->osb_lock);
298d85b20e4SJoel Becker 
299d85b20e4SJoel Becker 	return slot;
300d85b20e4SJoel Becker }
301d85b20e4SJoel Becker 
302d85b20e4SJoel Becker int ocfs2_slot_to_node_num_locked(struct ocfs2_super *osb, int slot_num,
303d85b20e4SJoel Becker 				  unsigned int *node_num)
304d85b20e4SJoel Becker {
305d85b20e4SJoel Becker 	struct ocfs2_slot_info *si = osb->slot_info;
306d85b20e4SJoel Becker 
307d85b20e4SJoel Becker 	assert_spin_locked(&osb->osb_lock);
308d85b20e4SJoel Becker 
309d85b20e4SJoel Becker 	BUG_ON(slot_num < 0);
310d85b20e4SJoel Becker 	BUG_ON(slot_num > osb->max_slots);
311d85b20e4SJoel Becker 
312fc881fa0SJoel Becker 	if (!si->si_slots[slot_num].sl_valid)
313d85b20e4SJoel Becker 		return -ENOENT;
314d85b20e4SJoel Becker 
315fc881fa0SJoel Becker 	*node_num = si->si_slots[slot_num].sl_node_num;
316d85b20e4SJoel Becker 	return 0;
317ccd979bdSMark Fasheh }
318ccd979bdSMark Fasheh 
3198e8a4603SMark Fasheh static void __ocfs2_free_slot_info(struct ocfs2_slot_info *si)
3208e8a4603SMark Fasheh {
3211c8d9a6aSJoel Becker 	unsigned int i;
3221c8d9a6aSJoel Becker 
3238e8a4603SMark Fasheh 	if (si == NULL)
3248e8a4603SMark Fasheh 		return;
3258e8a4603SMark Fasheh 
3268e8a4603SMark Fasheh 	if (si->si_inode)
3278e8a4603SMark Fasheh 		iput(si->si_inode);
3281c8d9a6aSJoel Becker 	if (si->si_bh) {
3291c8d9a6aSJoel Becker 		for (i = 0; i < si->si_blocks; i++) {
3301c8d9a6aSJoel Becker 			if (si->si_bh[i]) {
3311c8d9a6aSJoel Becker 				brelse(si->si_bh[i]);
3321c8d9a6aSJoel Becker 				si->si_bh[i] = NULL;
3331c8d9a6aSJoel Becker 			}
3341c8d9a6aSJoel Becker 		}
3351c8d9a6aSJoel Becker 		kfree(si->si_bh);
3361c8d9a6aSJoel Becker 	}
3378e8a4603SMark Fasheh 
3388e8a4603SMark Fasheh 	kfree(si);
3398e8a4603SMark Fasheh }
3408e8a4603SMark Fasheh 
341fc881fa0SJoel Becker int ocfs2_clear_slot(struct ocfs2_super *osb, int slot_num)
342ccd979bdSMark Fasheh {
3438e8a4603SMark Fasheh 	struct ocfs2_slot_info *si = osb->slot_info;
3448e8a4603SMark Fasheh 
3458e8a4603SMark Fasheh 	if (si == NULL)
3468e8a4603SMark Fasheh 		return 0;
3478e8a4603SMark Fasheh 
348d85b20e4SJoel Becker 	spin_lock(&osb->osb_lock);
349fc881fa0SJoel Becker 	ocfs2_invalidate_slot(si, slot_num);
350d85b20e4SJoel Becker 	spin_unlock(&osb->osb_lock);
3518e8a4603SMark Fasheh 
352386a2ef8SJoel Becker 	return ocfs2_update_disk_slot(osb, osb->slot_info, slot_num);
353ccd979bdSMark Fasheh }
354ccd979bdSMark Fasheh 
3551c8d9a6aSJoel Becker static int ocfs2_map_slot_buffers(struct ocfs2_super *osb,
3561c8d9a6aSJoel Becker 				  struct ocfs2_slot_info *si)
3571c8d9a6aSJoel Becker {
3581c8d9a6aSJoel Becker 	int status = 0;
3591c8d9a6aSJoel Becker 	u64 blkno;
3601c8d9a6aSJoel Becker 	unsigned long long blocks, bytes;
3611c8d9a6aSJoel Becker 	unsigned int i;
3621c8d9a6aSJoel Becker 	struct buffer_head *bh;
3631c8d9a6aSJoel Becker 
3641c8d9a6aSJoel Becker 	status = ocfs2_slot_map_physical_size(osb, si->si_inode, &bytes);
3651c8d9a6aSJoel Becker 	if (status)
3661c8d9a6aSJoel Becker 		goto bail;
3671c8d9a6aSJoel Becker 
3681c8d9a6aSJoel Becker 	blocks = ocfs2_blocks_for_bytes(si->si_inode->i_sb, bytes);
3691c8d9a6aSJoel Becker 	BUG_ON(blocks > UINT_MAX);
3701c8d9a6aSJoel Becker 	si->si_blocks = blocks;
3711c8d9a6aSJoel Becker 	if (!si->si_blocks)
3721c8d9a6aSJoel Becker 		goto bail;
3731c8d9a6aSJoel Becker 
374386a2ef8SJoel Becker 	if (si->si_extended)
375386a2ef8SJoel Becker 		si->si_slots_per_block =
376386a2ef8SJoel Becker 			(osb->sb->s_blocksize /
377386a2ef8SJoel Becker 			 sizeof(struct ocfs2_extended_slot));
378386a2ef8SJoel Becker 	else
379386a2ef8SJoel Becker 		si->si_slots_per_block = osb->sb->s_blocksize / sizeof(__le16);
380386a2ef8SJoel Becker 
381386a2ef8SJoel Becker 	/* The size checks above should ensure this */
382386a2ef8SJoel Becker 	BUG_ON((osb->max_slots / si->si_slots_per_block) > blocks);
383386a2ef8SJoel Becker 
3841c8d9a6aSJoel Becker 	mlog(0, "Slot map needs %u buffers for %llu bytes\n",
3851c8d9a6aSJoel Becker 	     si->si_blocks, bytes);
3861c8d9a6aSJoel Becker 
3871c8d9a6aSJoel Becker 	si->si_bh = kzalloc(sizeof(struct buffer_head *) * si->si_blocks,
3881c8d9a6aSJoel Becker 			    GFP_KERNEL);
3891c8d9a6aSJoel Becker 	if (!si->si_bh) {
3901c8d9a6aSJoel Becker 		status = -ENOMEM;
3911c8d9a6aSJoel Becker 		mlog_errno(status);
3921c8d9a6aSJoel Becker 		goto bail;
3931c8d9a6aSJoel Becker 	}
3941c8d9a6aSJoel Becker 
3951c8d9a6aSJoel Becker 	for (i = 0; i < si->si_blocks; i++) {
3961c8d9a6aSJoel Becker 		status = ocfs2_extent_map_get_blocks(si->si_inode, i,
3971c8d9a6aSJoel Becker 						     &blkno, NULL, NULL);
3981c8d9a6aSJoel Becker 		if (status < 0) {
3991c8d9a6aSJoel Becker 			mlog_errno(status);
4001c8d9a6aSJoel Becker 			goto bail;
4011c8d9a6aSJoel Becker 		}
4021c8d9a6aSJoel Becker 
4031c8d9a6aSJoel Becker 		mlog(0, "Reading slot map block %u at %llu\n", i,
4041c8d9a6aSJoel Becker 		     (unsigned long long)blkno);
4051c8d9a6aSJoel Becker 
4061c8d9a6aSJoel Becker 		bh = NULL;  /* Acquire a fresh bh */
407*8cb471e8SJoel Becker 		status = ocfs2_read_blocks(INODE_CACHE(si->si_inode), blkno,
408*8cb471e8SJoel Becker 					   1, &bh, OCFS2_BH_IGNORE_CACHE, NULL);
4091c8d9a6aSJoel Becker 		if (status < 0) {
4101c8d9a6aSJoel Becker 			mlog_errno(status);
4111c8d9a6aSJoel Becker 			goto bail;
4121c8d9a6aSJoel Becker 		}
4131c8d9a6aSJoel Becker 
4141c8d9a6aSJoel Becker 		si->si_bh[i] = bh;
4151c8d9a6aSJoel Becker 	}
4161c8d9a6aSJoel Becker 
4171c8d9a6aSJoel Becker bail:
4181c8d9a6aSJoel Becker 	return status;
4191c8d9a6aSJoel Becker }
4201c8d9a6aSJoel Becker 
421ccd979bdSMark Fasheh int ocfs2_init_slot_info(struct ocfs2_super *osb)
422ccd979bdSMark Fasheh {
423fc881fa0SJoel Becker 	int status;
424ccd979bdSMark Fasheh 	struct inode *inode = NULL;
425ccd979bdSMark Fasheh 	struct ocfs2_slot_info *si;
426ccd979bdSMark Fasheh 
427fc881fa0SJoel Becker 	si = kzalloc(sizeof(struct ocfs2_slot_info) +
428fc881fa0SJoel Becker 		     (sizeof(struct ocfs2_slot) * osb->max_slots),
429fc881fa0SJoel Becker 		     GFP_KERNEL);
430ccd979bdSMark Fasheh 	if (!si) {
431ccd979bdSMark Fasheh 		status = -ENOMEM;
432ccd979bdSMark Fasheh 		mlog_errno(status);
433ccd979bdSMark Fasheh 		goto bail;
434ccd979bdSMark Fasheh 	}
435ccd979bdSMark Fasheh 
436386a2ef8SJoel Becker 	si->si_extended = ocfs2_uses_extended_slot_map(osb);
437ccd979bdSMark Fasheh 	si->si_num_slots = osb->max_slots;
438fc881fa0SJoel Becker 	si->si_slots = (struct ocfs2_slot *)((char *)si +
439fc881fa0SJoel Becker 					     sizeof(struct ocfs2_slot_info));
440ccd979bdSMark Fasheh 
441ccd979bdSMark Fasheh 	inode = ocfs2_get_system_file_inode(osb, SLOT_MAP_SYSTEM_INODE,
442ccd979bdSMark Fasheh 					    OCFS2_INVALID_SLOT);
443ccd979bdSMark Fasheh 	if (!inode) {
444ccd979bdSMark Fasheh 		status = -EINVAL;
445ccd979bdSMark Fasheh 		mlog_errno(status);
446ccd979bdSMark Fasheh 		goto bail;
447ccd979bdSMark Fasheh 	}
448ccd979bdSMark Fasheh 
449ccd979bdSMark Fasheh 	si->si_inode = inode;
4501c8d9a6aSJoel Becker 	status = ocfs2_map_slot_buffers(osb, si);
4511c8d9a6aSJoel Becker 	if (status < 0) {
4521c8d9a6aSJoel Becker 		mlog_errno(status);
4531c8d9a6aSJoel Becker 		goto bail;
4541c8d9a6aSJoel Becker 	}
4551c8d9a6aSJoel Becker 
456d85b20e4SJoel Becker 	osb->slot_info = (struct ocfs2_slot_info *)si;
457ccd979bdSMark Fasheh bail:
458ccd979bdSMark Fasheh 	if (status < 0 && si)
4598e8a4603SMark Fasheh 		__ocfs2_free_slot_info(si);
460ccd979bdSMark Fasheh 
461ccd979bdSMark Fasheh 	return status;
462ccd979bdSMark Fasheh }
463ccd979bdSMark Fasheh 
4648e8a4603SMark Fasheh void ocfs2_free_slot_info(struct ocfs2_super *osb)
465ccd979bdSMark Fasheh {
4668e8a4603SMark Fasheh 	struct ocfs2_slot_info *si = osb->slot_info;
4678e8a4603SMark Fasheh 
4688e8a4603SMark Fasheh 	osb->slot_info = NULL;
4698e8a4603SMark Fasheh 	__ocfs2_free_slot_info(si);
470ccd979bdSMark Fasheh }
471ccd979bdSMark Fasheh 
472ccd979bdSMark Fasheh int ocfs2_find_slot(struct ocfs2_super *osb)
473ccd979bdSMark Fasheh {
474ccd979bdSMark Fasheh 	int status;
475fc881fa0SJoel Becker 	int slot;
476ccd979bdSMark Fasheh 	struct ocfs2_slot_info *si;
477ccd979bdSMark Fasheh 
478ccd979bdSMark Fasheh 	mlog_entry_void();
479ccd979bdSMark Fasheh 
480ccd979bdSMark Fasheh 	si = osb->slot_info;
481ccd979bdSMark Fasheh 
482d85b20e4SJoel Becker 	spin_lock(&osb->osb_lock);
483ccd979bdSMark Fasheh 	ocfs2_update_slot_info(si);
484ccd979bdSMark Fasheh 
485ccd979bdSMark Fasheh 	/* search for ourselves first and take the slot if it already
486ccd979bdSMark Fasheh 	 * exists. Perhaps we need to mark this in a variable for our
487ccd979bdSMark Fasheh 	 * own journal recovery? Possibly not, though we certainly
488ccd979bdSMark Fasheh 	 * need to warn to the user */
489ccd979bdSMark Fasheh 	slot = __ocfs2_node_num_to_slot(si, osb->node_num);
490fc881fa0SJoel Becker 	if (slot < 0) {
491ccd979bdSMark Fasheh 		/* if no slot yet, then just take 1st available
492ccd979bdSMark Fasheh 		 * one. */
493baf4661aSSunil Mushran 		slot = __ocfs2_find_empty_slot(si, osb->preferred_slot);
494fc881fa0SJoel Becker 		if (slot < 0) {
495d85b20e4SJoel Becker 			spin_unlock(&osb->osb_lock);
496ccd979bdSMark Fasheh 			mlog(ML_ERROR, "no free slots available!\n");
497ccd979bdSMark Fasheh 			status = -EINVAL;
498ccd979bdSMark Fasheh 			goto bail;
499ccd979bdSMark Fasheh 		}
500ccd979bdSMark Fasheh 	} else
501ccd979bdSMark Fasheh 		mlog(ML_NOTICE, "slot %d is already allocated to this node!\n",
502ccd979bdSMark Fasheh 		     slot);
503ccd979bdSMark Fasheh 
504fc881fa0SJoel Becker 	ocfs2_set_slot(si, slot, osb->node_num);
505ccd979bdSMark Fasheh 	osb->slot_num = slot;
506d85b20e4SJoel Becker 	spin_unlock(&osb->osb_lock);
507ccd979bdSMark Fasheh 
508e7607ab3SMark Fasheh 	mlog(0, "taking node slot %d\n", osb->slot_num);
509ccd979bdSMark Fasheh 
510386a2ef8SJoel Becker 	status = ocfs2_update_disk_slot(osb, si, osb->slot_num);
511ccd979bdSMark Fasheh 	if (status < 0)
512ccd979bdSMark Fasheh 		mlog_errno(status);
513ccd979bdSMark Fasheh 
514ccd979bdSMark Fasheh bail:
515ccd979bdSMark Fasheh 	mlog_exit(status);
516ccd979bdSMark Fasheh 	return status;
517ccd979bdSMark Fasheh }
518ccd979bdSMark Fasheh 
519ccd979bdSMark Fasheh void ocfs2_put_slot(struct ocfs2_super *osb)
520ccd979bdSMark Fasheh {
521386a2ef8SJoel Becker 	int status, slot_num;
522ccd979bdSMark Fasheh 	struct ocfs2_slot_info *si = osb->slot_info;
523ccd979bdSMark Fasheh 
524ccd979bdSMark Fasheh 	if (!si)
525ccd979bdSMark Fasheh 		return;
526ccd979bdSMark Fasheh 
527d85b20e4SJoel Becker 	spin_lock(&osb->osb_lock);
528ccd979bdSMark Fasheh 	ocfs2_update_slot_info(si);
529ccd979bdSMark Fasheh 
530386a2ef8SJoel Becker 	slot_num = osb->slot_num;
531fc881fa0SJoel Becker 	ocfs2_invalidate_slot(si, osb->slot_num);
532ccd979bdSMark Fasheh 	osb->slot_num = OCFS2_INVALID_SLOT;
533d85b20e4SJoel Becker 	spin_unlock(&osb->osb_lock);
534ccd979bdSMark Fasheh 
535386a2ef8SJoel Becker 	status = ocfs2_update_disk_slot(osb, si, slot_num);
536ccd979bdSMark Fasheh 	if (status < 0) {
537ccd979bdSMark Fasheh 		mlog_errno(status);
538ccd979bdSMark Fasheh 		goto bail;
539ccd979bdSMark Fasheh 	}
540ccd979bdSMark Fasheh 
541ccd979bdSMark Fasheh bail:
5428e8a4603SMark Fasheh 	ocfs2_free_slot_info(osb);
543ccd979bdSMark Fasheh }
544ccd979bdSMark Fasheh 
545