xref: /openbmc/linux/fs/ocfs2/slot_map.c (revision a8731086eff053b430cddbf5783654dfd700ea06)
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"
41*a8731086STao 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;
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 
145*a8731086STao 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);
309d85b20e4SJoel Becker 	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 	if (si->si_inode)
3268e8a4603SMark Fasheh 		iput(si->si_inode);
3271c8d9a6aSJoel Becker 	if (si->si_bh) {
3281c8d9a6aSJoel Becker 		for (i = 0; i < si->si_blocks; i++) {
3291c8d9a6aSJoel Becker 			if (si->si_bh[i]) {
3301c8d9a6aSJoel Becker 				brelse(si->si_bh[i]);
3311c8d9a6aSJoel Becker 				si->si_bh[i] = NULL;
3321c8d9a6aSJoel Becker 			}
3331c8d9a6aSJoel Becker 		}
3341c8d9a6aSJoel Becker 		kfree(si->si_bh);
3351c8d9a6aSJoel Becker 	}
3368e8a4603SMark Fasheh 
3378e8a4603SMark Fasheh 	kfree(si);
3388e8a4603SMark Fasheh }
3398e8a4603SMark Fasheh 
340fc881fa0SJoel Becker int ocfs2_clear_slot(struct ocfs2_super *osb, int slot_num)
341ccd979bdSMark Fasheh {
3428e8a4603SMark Fasheh 	struct ocfs2_slot_info *si = osb->slot_info;
3438e8a4603SMark Fasheh 
3448e8a4603SMark Fasheh 	if (si == NULL)
3458e8a4603SMark Fasheh 		return 0;
3468e8a4603SMark Fasheh 
347d85b20e4SJoel Becker 	spin_lock(&osb->osb_lock);
348fc881fa0SJoel Becker 	ocfs2_invalidate_slot(si, slot_num);
349d85b20e4SJoel Becker 	spin_unlock(&osb->osb_lock);
3508e8a4603SMark Fasheh 
351386a2ef8SJoel Becker 	return ocfs2_update_disk_slot(osb, osb->slot_info, slot_num);
352ccd979bdSMark Fasheh }
353ccd979bdSMark Fasheh 
3541c8d9a6aSJoel Becker static int ocfs2_map_slot_buffers(struct ocfs2_super *osb,
3551c8d9a6aSJoel Becker 				  struct ocfs2_slot_info *si)
3561c8d9a6aSJoel Becker {
3571c8d9a6aSJoel Becker 	int status = 0;
3581c8d9a6aSJoel Becker 	u64 blkno;
359f30d44f3SPoyo VL 	unsigned long long blocks, bytes = 0;
3601c8d9a6aSJoel Becker 	unsigned int i;
3611c8d9a6aSJoel Becker 	struct buffer_head *bh;
3621c8d9a6aSJoel Becker 
3631c8d9a6aSJoel Becker 	status = ocfs2_slot_map_physical_size(osb, si->si_inode, &bytes);
3641c8d9a6aSJoel Becker 	if (status)
3651c8d9a6aSJoel Becker 		goto bail;
3661c8d9a6aSJoel Becker 
3671c8d9a6aSJoel Becker 	blocks = ocfs2_blocks_for_bytes(si->si_inode->i_sb, bytes);
3681c8d9a6aSJoel Becker 	BUG_ON(blocks > UINT_MAX);
3691c8d9a6aSJoel Becker 	si->si_blocks = blocks;
3701c8d9a6aSJoel Becker 	if (!si->si_blocks)
3711c8d9a6aSJoel Becker 		goto bail;
3721c8d9a6aSJoel Becker 
373386a2ef8SJoel Becker 	if (si->si_extended)
374386a2ef8SJoel Becker 		si->si_slots_per_block =
375386a2ef8SJoel Becker 			(osb->sb->s_blocksize /
376386a2ef8SJoel Becker 			 sizeof(struct ocfs2_extended_slot));
377386a2ef8SJoel Becker 	else
378386a2ef8SJoel Becker 		si->si_slots_per_block = osb->sb->s_blocksize / sizeof(__le16);
379386a2ef8SJoel Becker 
380386a2ef8SJoel Becker 	/* The size checks above should ensure this */
381386a2ef8SJoel Becker 	BUG_ON((osb->max_slots / si->si_slots_per_block) > blocks);
382386a2ef8SJoel Becker 
383*a8731086STao Ma 	trace_ocfs2_map_slot_buffers(bytes, si->si_blocks);
3841c8d9a6aSJoel Becker 
3851c8d9a6aSJoel Becker 	si->si_bh = kzalloc(sizeof(struct buffer_head *) * si->si_blocks,
3861c8d9a6aSJoel Becker 			    GFP_KERNEL);
3871c8d9a6aSJoel Becker 	if (!si->si_bh) {
3881c8d9a6aSJoel Becker 		status = -ENOMEM;
3891c8d9a6aSJoel Becker 		mlog_errno(status);
3901c8d9a6aSJoel Becker 		goto bail;
3911c8d9a6aSJoel Becker 	}
3921c8d9a6aSJoel Becker 
3931c8d9a6aSJoel Becker 	for (i = 0; i < si->si_blocks; i++) {
3941c8d9a6aSJoel Becker 		status = ocfs2_extent_map_get_blocks(si->si_inode, i,
3951c8d9a6aSJoel Becker 						     &blkno, NULL, NULL);
3961c8d9a6aSJoel Becker 		if (status < 0) {
3971c8d9a6aSJoel Becker 			mlog_errno(status);
3981c8d9a6aSJoel Becker 			goto bail;
3991c8d9a6aSJoel Becker 		}
4001c8d9a6aSJoel Becker 
401*a8731086STao Ma 		trace_ocfs2_map_slot_buffers_block((unsigned long long)blkno, i);
4021c8d9a6aSJoel Becker 
4031c8d9a6aSJoel Becker 		bh = NULL;  /* Acquire a fresh bh */
4048cb471e8SJoel Becker 		status = ocfs2_read_blocks(INODE_CACHE(si->si_inode), blkno,
4058cb471e8SJoel Becker 					   1, &bh, OCFS2_BH_IGNORE_CACHE, NULL);
4061c8d9a6aSJoel Becker 		if (status < 0) {
4071c8d9a6aSJoel Becker 			mlog_errno(status);
4081c8d9a6aSJoel Becker 			goto bail;
4091c8d9a6aSJoel Becker 		}
4101c8d9a6aSJoel Becker 
4111c8d9a6aSJoel Becker 		si->si_bh[i] = bh;
4121c8d9a6aSJoel Becker 	}
4131c8d9a6aSJoel Becker 
4141c8d9a6aSJoel Becker bail:
4151c8d9a6aSJoel Becker 	return status;
4161c8d9a6aSJoel Becker }
4171c8d9a6aSJoel Becker 
418ccd979bdSMark Fasheh int ocfs2_init_slot_info(struct ocfs2_super *osb)
419ccd979bdSMark Fasheh {
420fc881fa0SJoel Becker 	int status;
421ccd979bdSMark Fasheh 	struct inode *inode = NULL;
422ccd979bdSMark Fasheh 	struct ocfs2_slot_info *si;
423ccd979bdSMark Fasheh 
424fc881fa0SJoel Becker 	si = kzalloc(sizeof(struct ocfs2_slot_info) +
425fc881fa0SJoel Becker 		     (sizeof(struct ocfs2_slot) * osb->max_slots),
426fc881fa0SJoel Becker 		     GFP_KERNEL);
427ccd979bdSMark Fasheh 	if (!si) {
428ccd979bdSMark Fasheh 		status = -ENOMEM;
429ccd979bdSMark Fasheh 		mlog_errno(status);
430ccd979bdSMark Fasheh 		goto bail;
431ccd979bdSMark Fasheh 	}
432ccd979bdSMark Fasheh 
433386a2ef8SJoel Becker 	si->si_extended = ocfs2_uses_extended_slot_map(osb);
434ccd979bdSMark Fasheh 	si->si_num_slots = osb->max_slots;
435fc881fa0SJoel Becker 	si->si_slots = (struct ocfs2_slot *)((char *)si +
436fc881fa0SJoel Becker 					     sizeof(struct ocfs2_slot_info));
437ccd979bdSMark Fasheh 
438ccd979bdSMark Fasheh 	inode = ocfs2_get_system_file_inode(osb, SLOT_MAP_SYSTEM_INODE,
439ccd979bdSMark Fasheh 					    OCFS2_INVALID_SLOT);
440ccd979bdSMark Fasheh 	if (!inode) {
441ccd979bdSMark Fasheh 		status = -EINVAL;
442ccd979bdSMark Fasheh 		mlog_errno(status);
443ccd979bdSMark Fasheh 		goto bail;
444ccd979bdSMark Fasheh 	}
445ccd979bdSMark Fasheh 
446ccd979bdSMark Fasheh 	si->si_inode = inode;
4471c8d9a6aSJoel Becker 	status = ocfs2_map_slot_buffers(osb, si);
4481c8d9a6aSJoel Becker 	if (status < 0) {
4491c8d9a6aSJoel Becker 		mlog_errno(status);
4501c8d9a6aSJoel Becker 		goto bail;
4511c8d9a6aSJoel Becker 	}
4521c8d9a6aSJoel Becker 
453d85b20e4SJoel Becker 	osb->slot_info = (struct ocfs2_slot_info *)si;
454ccd979bdSMark Fasheh bail:
455ccd979bdSMark Fasheh 	if (status < 0 && si)
4568e8a4603SMark Fasheh 		__ocfs2_free_slot_info(si);
457ccd979bdSMark Fasheh 
458ccd979bdSMark Fasheh 	return status;
459ccd979bdSMark Fasheh }
460ccd979bdSMark Fasheh 
4618e8a4603SMark Fasheh void ocfs2_free_slot_info(struct ocfs2_super *osb)
462ccd979bdSMark Fasheh {
4638e8a4603SMark Fasheh 	struct ocfs2_slot_info *si = osb->slot_info;
4648e8a4603SMark Fasheh 
4658e8a4603SMark Fasheh 	osb->slot_info = NULL;
4668e8a4603SMark Fasheh 	__ocfs2_free_slot_info(si);
467ccd979bdSMark Fasheh }
468ccd979bdSMark Fasheh 
469ccd979bdSMark Fasheh int ocfs2_find_slot(struct ocfs2_super *osb)
470ccd979bdSMark Fasheh {
471ccd979bdSMark Fasheh 	int status;
472fc881fa0SJoel Becker 	int slot;
473ccd979bdSMark Fasheh 	struct ocfs2_slot_info *si;
474ccd979bdSMark Fasheh 
475ccd979bdSMark Fasheh 	si = osb->slot_info;
476ccd979bdSMark Fasheh 
477d85b20e4SJoel Becker 	spin_lock(&osb->osb_lock);
478ccd979bdSMark Fasheh 	ocfs2_update_slot_info(si);
479ccd979bdSMark Fasheh 
480ccd979bdSMark Fasheh 	/* search for ourselves first and take the slot if it already
481ccd979bdSMark Fasheh 	 * exists. Perhaps we need to mark this in a variable for our
482ccd979bdSMark Fasheh 	 * own journal recovery? Possibly not, though we certainly
483ccd979bdSMark Fasheh 	 * need to warn to the user */
484ccd979bdSMark Fasheh 	slot = __ocfs2_node_num_to_slot(si, osb->node_num);
485fc881fa0SJoel Becker 	if (slot < 0) {
486ccd979bdSMark Fasheh 		/* if no slot yet, then just take 1st available
487ccd979bdSMark Fasheh 		 * one. */
488baf4661aSSunil Mushran 		slot = __ocfs2_find_empty_slot(si, osb->preferred_slot);
489fc881fa0SJoel Becker 		if (slot < 0) {
490d85b20e4SJoel Becker 			spin_unlock(&osb->osb_lock);
491ccd979bdSMark Fasheh 			mlog(ML_ERROR, "no free slots available!\n");
492ccd979bdSMark Fasheh 			status = -EINVAL;
493ccd979bdSMark Fasheh 			goto bail;
494ccd979bdSMark Fasheh 		}
495ccd979bdSMark Fasheh 	} else
496ccd979bdSMark Fasheh 		mlog(ML_NOTICE, "slot %d is already allocated to this node!\n",
497ccd979bdSMark Fasheh 		     slot);
498ccd979bdSMark Fasheh 
499fc881fa0SJoel Becker 	ocfs2_set_slot(si, slot, osb->node_num);
500ccd979bdSMark Fasheh 	osb->slot_num = slot;
501d85b20e4SJoel Becker 	spin_unlock(&osb->osb_lock);
502ccd979bdSMark Fasheh 
503*a8731086STao Ma 	trace_ocfs2_find_slot(osb->slot_num);
504ccd979bdSMark Fasheh 
505386a2ef8SJoel Becker 	status = ocfs2_update_disk_slot(osb, si, osb->slot_num);
506ccd979bdSMark Fasheh 	if (status < 0)
507ccd979bdSMark Fasheh 		mlog_errno(status);
508ccd979bdSMark Fasheh 
509ccd979bdSMark Fasheh bail:
510ccd979bdSMark Fasheh 	return status;
511ccd979bdSMark Fasheh }
512ccd979bdSMark Fasheh 
513ccd979bdSMark Fasheh void ocfs2_put_slot(struct ocfs2_super *osb)
514ccd979bdSMark Fasheh {
515386a2ef8SJoel Becker 	int status, slot_num;
516ccd979bdSMark Fasheh 	struct ocfs2_slot_info *si = osb->slot_info;
517ccd979bdSMark Fasheh 
518ccd979bdSMark Fasheh 	if (!si)
519ccd979bdSMark Fasheh 		return;
520ccd979bdSMark Fasheh 
521d85b20e4SJoel Becker 	spin_lock(&osb->osb_lock);
522ccd979bdSMark Fasheh 	ocfs2_update_slot_info(si);
523ccd979bdSMark Fasheh 
524386a2ef8SJoel Becker 	slot_num = osb->slot_num;
525fc881fa0SJoel Becker 	ocfs2_invalidate_slot(si, osb->slot_num);
526ccd979bdSMark Fasheh 	osb->slot_num = OCFS2_INVALID_SLOT;
527d85b20e4SJoel Becker 	spin_unlock(&osb->osb_lock);
528ccd979bdSMark Fasheh 
529386a2ef8SJoel Becker 	status = ocfs2_update_disk_slot(osb, si, slot_num);
530ccd979bdSMark Fasheh 	if (status < 0) {
531ccd979bdSMark Fasheh 		mlog_errno(status);
532ccd979bdSMark Fasheh 		goto bail;
533ccd979bdSMark Fasheh 	}
534ccd979bdSMark Fasheh 
535ccd979bdSMark Fasheh bail:
5368e8a4603SMark Fasheh 	ocfs2_free_slot_info(osb);
537ccd979bdSMark Fasheh }
538ccd979bdSMark Fasheh 
539