1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * linux/fs/ext2/balloc.c
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 1992, 1993, 1994, 1995
6*4882a593Smuzhiyun * Remy Card (card@masi.ibp.fr)
7*4882a593Smuzhiyun * Laboratoire MASI - Institut Blaise Pascal
8*4882a593Smuzhiyun * Universite Pierre et Marie Curie (Paris VI)
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993
11*4882a593Smuzhiyun * Big-endian to little-endian byte-swapping/bitmaps by
12*4882a593Smuzhiyun * David S. Miller (davem@caip.rutgers.edu), 1995
13*4882a593Smuzhiyun */
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #include "ext2.h"
16*4882a593Smuzhiyun #include <linux/quotaops.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/sched.h>
19*4882a593Smuzhiyun #include <linux/cred.h>
20*4882a593Smuzhiyun #include <linux/buffer_head.h>
21*4882a593Smuzhiyun #include <linux/capability.h>
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun * balloc.c contains the blocks allocation and deallocation routines
25*4882a593Smuzhiyun */
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun /*
28*4882a593Smuzhiyun * The free blocks are managed by bitmaps. A file system contains several
29*4882a593Smuzhiyun * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
30*4882a593Smuzhiyun * block for inodes, N blocks for the inode table and data blocks.
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * The file system contains group descriptors which are located after the
33*4882a593Smuzhiyun * super block. Each descriptor contains the number of the bitmap block and
34*4882a593Smuzhiyun * the free blocks count in the block. The descriptors are loaded in memory
35*4882a593Smuzhiyun * when a file system is mounted (see ext2_fill_super).
36*4882a593Smuzhiyun */
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
40*4882a593Smuzhiyun
ext2_get_group_desc(struct super_block * sb,unsigned int block_group,struct buffer_head ** bh)41*4882a593Smuzhiyun struct ext2_group_desc * ext2_get_group_desc(struct super_block * sb,
42*4882a593Smuzhiyun unsigned int block_group,
43*4882a593Smuzhiyun struct buffer_head ** bh)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun unsigned long group_desc;
46*4882a593Smuzhiyun unsigned long offset;
47*4882a593Smuzhiyun struct ext2_group_desc * desc;
48*4882a593Smuzhiyun struct ext2_sb_info *sbi = EXT2_SB(sb);
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun if (block_group >= sbi->s_groups_count) {
51*4882a593Smuzhiyun WARN(1, "block_group >= groups_count - "
52*4882a593Smuzhiyun "block_group = %d, groups_count = %lu",
53*4882a593Smuzhiyun block_group, sbi->s_groups_count);
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun return NULL;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun group_desc = block_group >> EXT2_DESC_PER_BLOCK_BITS(sb);
59*4882a593Smuzhiyun offset = block_group & (EXT2_DESC_PER_BLOCK(sb) - 1);
60*4882a593Smuzhiyun if (!sbi->s_group_desc[group_desc]) {
61*4882a593Smuzhiyun WARN(1, "Group descriptor not loaded - "
62*4882a593Smuzhiyun "block_group = %d, group_desc = %lu, desc = %lu",
63*4882a593Smuzhiyun block_group, group_desc, offset);
64*4882a593Smuzhiyun return NULL;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun desc = (struct ext2_group_desc *) sbi->s_group_desc[group_desc]->b_data;
68*4882a593Smuzhiyun if (bh)
69*4882a593Smuzhiyun *bh = sbi->s_group_desc[group_desc];
70*4882a593Smuzhiyun return desc + offset;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
ext2_valid_block_bitmap(struct super_block * sb,struct ext2_group_desc * desc,unsigned int block_group,struct buffer_head * bh)73*4882a593Smuzhiyun static int ext2_valid_block_bitmap(struct super_block *sb,
74*4882a593Smuzhiyun struct ext2_group_desc *desc,
75*4882a593Smuzhiyun unsigned int block_group,
76*4882a593Smuzhiyun struct buffer_head *bh)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun ext2_grpblk_t offset;
79*4882a593Smuzhiyun ext2_grpblk_t next_zero_bit;
80*4882a593Smuzhiyun ext2_fsblk_t bitmap_blk;
81*4882a593Smuzhiyun ext2_fsblk_t group_first_block;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun group_first_block = ext2_group_first_block_no(sb, block_group);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /* check whether block bitmap block number is set */
86*4882a593Smuzhiyun bitmap_blk = le32_to_cpu(desc->bg_block_bitmap);
87*4882a593Smuzhiyun offset = bitmap_blk - group_first_block;
88*4882a593Smuzhiyun if (!ext2_test_bit(offset, bh->b_data))
89*4882a593Smuzhiyun /* bad block bitmap */
90*4882a593Smuzhiyun goto err_out;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /* check whether the inode bitmap block number is set */
93*4882a593Smuzhiyun bitmap_blk = le32_to_cpu(desc->bg_inode_bitmap);
94*4882a593Smuzhiyun offset = bitmap_blk - group_first_block;
95*4882a593Smuzhiyun if (!ext2_test_bit(offset, bh->b_data))
96*4882a593Smuzhiyun /* bad block bitmap */
97*4882a593Smuzhiyun goto err_out;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /* check whether the inode table block number is set */
100*4882a593Smuzhiyun bitmap_blk = le32_to_cpu(desc->bg_inode_table);
101*4882a593Smuzhiyun offset = bitmap_blk - group_first_block;
102*4882a593Smuzhiyun next_zero_bit = ext2_find_next_zero_bit(bh->b_data,
103*4882a593Smuzhiyun offset + EXT2_SB(sb)->s_itb_per_group,
104*4882a593Smuzhiyun offset);
105*4882a593Smuzhiyun if (next_zero_bit >= offset + EXT2_SB(sb)->s_itb_per_group)
106*4882a593Smuzhiyun /* good bitmap for inode tables */
107*4882a593Smuzhiyun return 1;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun err_out:
110*4882a593Smuzhiyun ext2_error(sb, __func__,
111*4882a593Smuzhiyun "Invalid block bitmap - "
112*4882a593Smuzhiyun "block_group = %d, block = %lu",
113*4882a593Smuzhiyun block_group, bitmap_blk);
114*4882a593Smuzhiyun return 0;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /*
118*4882a593Smuzhiyun * Read the bitmap for a given block_group,and validate the
119*4882a593Smuzhiyun * bits for block/inode/inode tables are set in the bitmaps
120*4882a593Smuzhiyun *
121*4882a593Smuzhiyun * Return buffer_head on success or NULL in case of failure.
122*4882a593Smuzhiyun */
123*4882a593Smuzhiyun static struct buffer_head *
read_block_bitmap(struct super_block * sb,unsigned int block_group)124*4882a593Smuzhiyun read_block_bitmap(struct super_block *sb, unsigned int block_group)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun struct ext2_group_desc * desc;
127*4882a593Smuzhiyun struct buffer_head * bh = NULL;
128*4882a593Smuzhiyun ext2_fsblk_t bitmap_blk;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun desc = ext2_get_group_desc(sb, block_group, NULL);
131*4882a593Smuzhiyun if (!desc)
132*4882a593Smuzhiyun return NULL;
133*4882a593Smuzhiyun bitmap_blk = le32_to_cpu(desc->bg_block_bitmap);
134*4882a593Smuzhiyun bh = sb_getblk(sb, bitmap_blk);
135*4882a593Smuzhiyun if (unlikely(!bh)) {
136*4882a593Smuzhiyun ext2_error(sb, __func__,
137*4882a593Smuzhiyun "Cannot read block bitmap - "
138*4882a593Smuzhiyun "block_group = %d, block_bitmap = %u",
139*4882a593Smuzhiyun block_group, le32_to_cpu(desc->bg_block_bitmap));
140*4882a593Smuzhiyun return NULL;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun if (likely(bh_uptodate_or_lock(bh)))
143*4882a593Smuzhiyun return bh;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun if (bh_submit_read(bh) < 0) {
146*4882a593Smuzhiyun brelse(bh);
147*4882a593Smuzhiyun ext2_error(sb, __func__,
148*4882a593Smuzhiyun "Cannot read block bitmap - "
149*4882a593Smuzhiyun "block_group = %d, block_bitmap = %u",
150*4882a593Smuzhiyun block_group, le32_to_cpu(desc->bg_block_bitmap));
151*4882a593Smuzhiyun return NULL;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun ext2_valid_block_bitmap(sb, desc, block_group, bh);
155*4882a593Smuzhiyun /*
156*4882a593Smuzhiyun * file system mounted not to panic on error, continue with corrupt
157*4882a593Smuzhiyun * bitmap
158*4882a593Smuzhiyun */
159*4882a593Smuzhiyun return bh;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
group_adjust_blocks(struct super_block * sb,int group_no,struct ext2_group_desc * desc,struct buffer_head * bh,int count)162*4882a593Smuzhiyun static void group_adjust_blocks(struct super_block *sb, int group_no,
163*4882a593Smuzhiyun struct ext2_group_desc *desc, struct buffer_head *bh, int count)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun if (count) {
166*4882a593Smuzhiyun struct ext2_sb_info *sbi = EXT2_SB(sb);
167*4882a593Smuzhiyun unsigned free_blocks;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun spin_lock(sb_bgl_lock(sbi, group_no));
170*4882a593Smuzhiyun free_blocks = le16_to_cpu(desc->bg_free_blocks_count);
171*4882a593Smuzhiyun desc->bg_free_blocks_count = cpu_to_le16(free_blocks + count);
172*4882a593Smuzhiyun spin_unlock(sb_bgl_lock(sbi, group_no));
173*4882a593Smuzhiyun mark_buffer_dirty(bh);
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun /*
178*4882a593Smuzhiyun * The reservation window structure operations
179*4882a593Smuzhiyun * --------------------------------------------
180*4882a593Smuzhiyun * Operations include:
181*4882a593Smuzhiyun * dump, find, add, remove, is_empty, find_next_reservable_window, etc.
182*4882a593Smuzhiyun *
183*4882a593Smuzhiyun * We use a red-black tree to represent per-filesystem reservation
184*4882a593Smuzhiyun * windows.
185*4882a593Smuzhiyun *
186*4882a593Smuzhiyun */
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /**
189*4882a593Smuzhiyun * __rsv_window_dump() -- Dump the filesystem block allocation reservation map
190*4882a593Smuzhiyun * @root: root of per-filesystem reservation rb tree
191*4882a593Smuzhiyun * @verbose: verbose mode
192*4882a593Smuzhiyun * @fn: function which wishes to dump the reservation map
193*4882a593Smuzhiyun *
194*4882a593Smuzhiyun * If verbose is turned on, it will print the whole block reservation
195*4882a593Smuzhiyun * windows(start, end). Otherwise, it will only print out the "bad" windows,
196*4882a593Smuzhiyun * those windows that overlap with their immediate neighbors.
197*4882a593Smuzhiyun */
198*4882a593Smuzhiyun #if 1
__rsv_window_dump(struct rb_root * root,int verbose,const char * fn)199*4882a593Smuzhiyun static void __rsv_window_dump(struct rb_root *root, int verbose,
200*4882a593Smuzhiyun const char *fn)
201*4882a593Smuzhiyun {
202*4882a593Smuzhiyun struct rb_node *n;
203*4882a593Smuzhiyun struct ext2_reserve_window_node *rsv, *prev;
204*4882a593Smuzhiyun int bad;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun restart:
207*4882a593Smuzhiyun n = rb_first(root);
208*4882a593Smuzhiyun bad = 0;
209*4882a593Smuzhiyun prev = NULL;
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun printk("Block Allocation Reservation Windows Map (%s):\n", fn);
212*4882a593Smuzhiyun while (n) {
213*4882a593Smuzhiyun rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
214*4882a593Smuzhiyun if (verbose)
215*4882a593Smuzhiyun printk("reservation window 0x%p "
216*4882a593Smuzhiyun "start: %lu, end: %lu\n",
217*4882a593Smuzhiyun rsv, rsv->rsv_start, rsv->rsv_end);
218*4882a593Smuzhiyun if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) {
219*4882a593Smuzhiyun printk("Bad reservation %p (start >= end)\n",
220*4882a593Smuzhiyun rsv);
221*4882a593Smuzhiyun bad = 1;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun if (prev && prev->rsv_end >= rsv->rsv_start) {
224*4882a593Smuzhiyun printk("Bad reservation %p (prev->end >= start)\n",
225*4882a593Smuzhiyun rsv);
226*4882a593Smuzhiyun bad = 1;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun if (bad) {
229*4882a593Smuzhiyun if (!verbose) {
230*4882a593Smuzhiyun printk("Restarting reservation walk in verbose mode\n");
231*4882a593Smuzhiyun verbose = 1;
232*4882a593Smuzhiyun goto restart;
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun n = rb_next(n);
236*4882a593Smuzhiyun prev = rsv;
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun printk("Window map complete.\n");
239*4882a593Smuzhiyun BUG_ON(bad);
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun #define rsv_window_dump(root, verbose) \
242*4882a593Smuzhiyun __rsv_window_dump((root), (verbose), __func__)
243*4882a593Smuzhiyun #else
244*4882a593Smuzhiyun #define rsv_window_dump(root, verbose) do {} while (0)
245*4882a593Smuzhiyun #endif
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun /**
248*4882a593Smuzhiyun * goal_in_my_reservation()
249*4882a593Smuzhiyun * @rsv: inode's reservation window
250*4882a593Smuzhiyun * @grp_goal: given goal block relative to the allocation block group
251*4882a593Smuzhiyun * @group: the current allocation block group
252*4882a593Smuzhiyun * @sb: filesystem super block
253*4882a593Smuzhiyun *
254*4882a593Smuzhiyun * Test if the given goal block (group relative) is within the file's
255*4882a593Smuzhiyun * own block reservation window range.
256*4882a593Smuzhiyun *
257*4882a593Smuzhiyun * If the reservation window is outside the goal allocation group, return 0;
258*4882a593Smuzhiyun * grp_goal (given goal block) could be -1, which means no specific
259*4882a593Smuzhiyun * goal block. In this case, always return 1.
260*4882a593Smuzhiyun * If the goal block is within the reservation window, return 1;
261*4882a593Smuzhiyun * otherwise, return 0;
262*4882a593Smuzhiyun */
263*4882a593Smuzhiyun static int
goal_in_my_reservation(struct ext2_reserve_window * rsv,ext2_grpblk_t grp_goal,unsigned int group,struct super_block * sb)264*4882a593Smuzhiyun goal_in_my_reservation(struct ext2_reserve_window *rsv, ext2_grpblk_t grp_goal,
265*4882a593Smuzhiyun unsigned int group, struct super_block * sb)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun ext2_fsblk_t group_first_block, group_last_block;
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun group_first_block = ext2_group_first_block_no(sb, group);
270*4882a593Smuzhiyun group_last_block = ext2_group_last_block_no(sb, group);
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun if ((rsv->_rsv_start > group_last_block) ||
273*4882a593Smuzhiyun (rsv->_rsv_end < group_first_block))
274*4882a593Smuzhiyun return 0;
275*4882a593Smuzhiyun if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start)
276*4882a593Smuzhiyun || (grp_goal + group_first_block > rsv->_rsv_end)))
277*4882a593Smuzhiyun return 0;
278*4882a593Smuzhiyun return 1;
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /**
282*4882a593Smuzhiyun * search_reserve_window()
283*4882a593Smuzhiyun * @root: root of reservation tree
284*4882a593Smuzhiyun * @goal: target allocation block
285*4882a593Smuzhiyun *
286*4882a593Smuzhiyun * Find the reserved window which includes the goal, or the previous one
287*4882a593Smuzhiyun * if the goal is not in any window.
288*4882a593Smuzhiyun * Returns NULL if there are no windows or if all windows start after the goal.
289*4882a593Smuzhiyun */
290*4882a593Smuzhiyun static struct ext2_reserve_window_node *
search_reserve_window(struct rb_root * root,ext2_fsblk_t goal)291*4882a593Smuzhiyun search_reserve_window(struct rb_root *root, ext2_fsblk_t goal)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun struct rb_node *n = root->rb_node;
294*4882a593Smuzhiyun struct ext2_reserve_window_node *rsv;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun if (!n)
297*4882a593Smuzhiyun return NULL;
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun do {
300*4882a593Smuzhiyun rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun if (goal < rsv->rsv_start)
303*4882a593Smuzhiyun n = n->rb_left;
304*4882a593Smuzhiyun else if (goal > rsv->rsv_end)
305*4882a593Smuzhiyun n = n->rb_right;
306*4882a593Smuzhiyun else
307*4882a593Smuzhiyun return rsv;
308*4882a593Smuzhiyun } while (n);
309*4882a593Smuzhiyun /*
310*4882a593Smuzhiyun * We've fallen off the end of the tree: the goal wasn't inside
311*4882a593Smuzhiyun * any particular node. OK, the previous node must be to one
312*4882a593Smuzhiyun * side of the interval containing the goal. If it's the RHS,
313*4882a593Smuzhiyun * we need to back up one.
314*4882a593Smuzhiyun */
315*4882a593Smuzhiyun if (rsv->rsv_start > goal) {
316*4882a593Smuzhiyun n = rb_prev(&rsv->rsv_node);
317*4882a593Smuzhiyun rsv = rb_entry(n, struct ext2_reserve_window_node, rsv_node);
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun return rsv;
320*4882a593Smuzhiyun }
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun /*
323*4882a593Smuzhiyun * ext2_rsv_window_add() -- Insert a window to the block reservation rb tree.
324*4882a593Smuzhiyun * @sb: super block
325*4882a593Smuzhiyun * @rsv: reservation window to add
326*4882a593Smuzhiyun *
327*4882a593Smuzhiyun * Must be called with rsv_lock held.
328*4882a593Smuzhiyun */
ext2_rsv_window_add(struct super_block * sb,struct ext2_reserve_window_node * rsv)329*4882a593Smuzhiyun void ext2_rsv_window_add(struct super_block *sb,
330*4882a593Smuzhiyun struct ext2_reserve_window_node *rsv)
331*4882a593Smuzhiyun {
332*4882a593Smuzhiyun struct rb_root *root = &EXT2_SB(sb)->s_rsv_window_root;
333*4882a593Smuzhiyun struct rb_node *node = &rsv->rsv_node;
334*4882a593Smuzhiyun ext2_fsblk_t start = rsv->rsv_start;
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun struct rb_node ** p = &root->rb_node;
337*4882a593Smuzhiyun struct rb_node * parent = NULL;
338*4882a593Smuzhiyun struct ext2_reserve_window_node *this;
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun while (*p)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun parent = *p;
343*4882a593Smuzhiyun this = rb_entry(parent, struct ext2_reserve_window_node, rsv_node);
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun if (start < this->rsv_start)
346*4882a593Smuzhiyun p = &(*p)->rb_left;
347*4882a593Smuzhiyun else if (start > this->rsv_end)
348*4882a593Smuzhiyun p = &(*p)->rb_right;
349*4882a593Smuzhiyun else {
350*4882a593Smuzhiyun rsv_window_dump(root, 1);
351*4882a593Smuzhiyun BUG();
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun }
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun rb_link_node(node, parent, p);
356*4882a593Smuzhiyun rb_insert_color(node, root);
357*4882a593Smuzhiyun }
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun /**
360*4882a593Smuzhiyun * rsv_window_remove() -- unlink a window from the reservation rb tree
361*4882a593Smuzhiyun * @sb: super block
362*4882a593Smuzhiyun * @rsv: reservation window to remove
363*4882a593Smuzhiyun *
364*4882a593Smuzhiyun * Mark the block reservation window as not allocated, and unlink it
365*4882a593Smuzhiyun * from the filesystem reservation window rb tree. Must be called with
366*4882a593Smuzhiyun * rsv_lock held.
367*4882a593Smuzhiyun */
rsv_window_remove(struct super_block * sb,struct ext2_reserve_window_node * rsv)368*4882a593Smuzhiyun static void rsv_window_remove(struct super_block *sb,
369*4882a593Smuzhiyun struct ext2_reserve_window_node *rsv)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
372*4882a593Smuzhiyun rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
373*4882a593Smuzhiyun rsv->rsv_alloc_hit = 0;
374*4882a593Smuzhiyun rb_erase(&rsv->rsv_node, &EXT2_SB(sb)->s_rsv_window_root);
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun /*
378*4882a593Smuzhiyun * rsv_is_empty() -- Check if the reservation window is allocated.
379*4882a593Smuzhiyun * @rsv: given reservation window to check
380*4882a593Smuzhiyun *
381*4882a593Smuzhiyun * returns 1 if the end block is EXT2_RESERVE_WINDOW_NOT_ALLOCATED.
382*4882a593Smuzhiyun */
rsv_is_empty(struct ext2_reserve_window * rsv)383*4882a593Smuzhiyun static inline int rsv_is_empty(struct ext2_reserve_window *rsv)
384*4882a593Smuzhiyun {
385*4882a593Smuzhiyun /* a valid reservation end block could not be 0 */
386*4882a593Smuzhiyun return (rsv->_rsv_end == EXT2_RESERVE_WINDOW_NOT_ALLOCATED);
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun /**
390*4882a593Smuzhiyun * ext2_init_block_alloc_info()
391*4882a593Smuzhiyun * @inode: file inode structure
392*4882a593Smuzhiyun *
393*4882a593Smuzhiyun * Allocate and initialize the reservation window structure, and
394*4882a593Smuzhiyun * link the window to the ext2 inode structure at last
395*4882a593Smuzhiyun *
396*4882a593Smuzhiyun * The reservation window structure is only dynamically allocated
397*4882a593Smuzhiyun * and linked to ext2 inode the first time the open file
398*4882a593Smuzhiyun * needs a new block. So, before every ext2_new_block(s) call, for
399*4882a593Smuzhiyun * regular files, we should check whether the reservation window
400*4882a593Smuzhiyun * structure exists or not. In the latter case, this function is called.
401*4882a593Smuzhiyun * Fail to do so will result in block reservation being turned off for that
402*4882a593Smuzhiyun * open file.
403*4882a593Smuzhiyun *
404*4882a593Smuzhiyun * This function is called from ext2_get_blocks_handle(), also called
405*4882a593Smuzhiyun * when setting the reservation window size through ioctl before the file
406*4882a593Smuzhiyun * is open for write (needs block allocation).
407*4882a593Smuzhiyun *
408*4882a593Smuzhiyun * Needs truncate_mutex protection prior to calling this function.
409*4882a593Smuzhiyun */
ext2_init_block_alloc_info(struct inode * inode)410*4882a593Smuzhiyun void ext2_init_block_alloc_info(struct inode *inode)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun struct ext2_inode_info *ei = EXT2_I(inode);
413*4882a593Smuzhiyun struct ext2_block_alloc_info *block_i;
414*4882a593Smuzhiyun struct super_block *sb = inode->i_sb;
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun block_i = kmalloc(sizeof(*block_i), GFP_NOFS);
417*4882a593Smuzhiyun if (block_i) {
418*4882a593Smuzhiyun struct ext2_reserve_window_node *rsv = &block_i->rsv_window_node;
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun rsv->rsv_start = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
421*4882a593Smuzhiyun rsv->rsv_end = EXT2_RESERVE_WINDOW_NOT_ALLOCATED;
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun /*
424*4882a593Smuzhiyun * if filesystem is mounted with NORESERVATION, the goal
425*4882a593Smuzhiyun * reservation window size is set to zero to indicate
426*4882a593Smuzhiyun * block reservation is off
427*4882a593Smuzhiyun */
428*4882a593Smuzhiyun if (!test_opt(sb, RESERVATION))
429*4882a593Smuzhiyun rsv->rsv_goal_size = 0;
430*4882a593Smuzhiyun else
431*4882a593Smuzhiyun rsv->rsv_goal_size = EXT2_DEFAULT_RESERVE_BLOCKS;
432*4882a593Smuzhiyun rsv->rsv_alloc_hit = 0;
433*4882a593Smuzhiyun block_i->last_alloc_logical_block = 0;
434*4882a593Smuzhiyun block_i->last_alloc_physical_block = 0;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun ei->i_block_alloc_info = block_i;
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun
439*4882a593Smuzhiyun /**
440*4882a593Smuzhiyun * ext2_discard_reservation()
441*4882a593Smuzhiyun * @inode: inode
442*4882a593Smuzhiyun *
443*4882a593Smuzhiyun * Discard(free) block reservation window on last file close, or truncate
444*4882a593Smuzhiyun * or at last iput().
445*4882a593Smuzhiyun *
446*4882a593Smuzhiyun * It is being called in three cases:
447*4882a593Smuzhiyun * ext2_release_file(): last writer closes the file
448*4882a593Smuzhiyun * ext2_clear_inode(): last iput(), when nobody links to this file.
449*4882a593Smuzhiyun * ext2_truncate(): when the block indirect map is about to change.
450*4882a593Smuzhiyun */
ext2_discard_reservation(struct inode * inode)451*4882a593Smuzhiyun void ext2_discard_reservation(struct inode *inode)
452*4882a593Smuzhiyun {
453*4882a593Smuzhiyun struct ext2_inode_info *ei = EXT2_I(inode);
454*4882a593Smuzhiyun struct ext2_block_alloc_info *block_i = ei->i_block_alloc_info;
455*4882a593Smuzhiyun struct ext2_reserve_window_node *rsv;
456*4882a593Smuzhiyun spinlock_t *rsv_lock = &EXT2_SB(inode->i_sb)->s_rsv_window_lock;
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun if (!block_i)
459*4882a593Smuzhiyun return;
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun rsv = &block_i->rsv_window_node;
462*4882a593Smuzhiyun if (!rsv_is_empty(&rsv->rsv_window)) {
463*4882a593Smuzhiyun spin_lock(rsv_lock);
464*4882a593Smuzhiyun if (!rsv_is_empty(&rsv->rsv_window))
465*4882a593Smuzhiyun rsv_window_remove(inode->i_sb, rsv);
466*4882a593Smuzhiyun spin_unlock(rsv_lock);
467*4882a593Smuzhiyun }
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun /**
471*4882a593Smuzhiyun * ext2_free_blocks() -- Free given blocks and update quota and i_blocks
472*4882a593Smuzhiyun * @inode: inode
473*4882a593Smuzhiyun * @block: start physical block to free
474*4882a593Smuzhiyun * @count: number of blocks to free
475*4882a593Smuzhiyun */
ext2_free_blocks(struct inode * inode,unsigned long block,unsigned long count)476*4882a593Smuzhiyun void ext2_free_blocks (struct inode * inode, unsigned long block,
477*4882a593Smuzhiyun unsigned long count)
478*4882a593Smuzhiyun {
479*4882a593Smuzhiyun struct buffer_head *bitmap_bh = NULL;
480*4882a593Smuzhiyun struct buffer_head * bh2;
481*4882a593Smuzhiyun unsigned long block_group;
482*4882a593Smuzhiyun unsigned long bit;
483*4882a593Smuzhiyun unsigned long i;
484*4882a593Smuzhiyun unsigned long overflow;
485*4882a593Smuzhiyun struct super_block * sb = inode->i_sb;
486*4882a593Smuzhiyun struct ext2_sb_info * sbi = EXT2_SB(sb);
487*4882a593Smuzhiyun struct ext2_group_desc * desc;
488*4882a593Smuzhiyun struct ext2_super_block * es = sbi->s_es;
489*4882a593Smuzhiyun unsigned freed = 0, group_freed;
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun if (!ext2_data_block_valid(sbi, block, count)) {
492*4882a593Smuzhiyun ext2_error (sb, "ext2_free_blocks",
493*4882a593Smuzhiyun "Freeing blocks not in datazone - "
494*4882a593Smuzhiyun "block = %lu, count = %lu", block, count);
495*4882a593Smuzhiyun goto error_return;
496*4882a593Smuzhiyun }
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun ext2_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1);
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun do_more:
501*4882a593Smuzhiyun overflow = 0;
502*4882a593Smuzhiyun block_group = (block - le32_to_cpu(es->s_first_data_block)) /
503*4882a593Smuzhiyun EXT2_BLOCKS_PER_GROUP(sb);
504*4882a593Smuzhiyun bit = (block - le32_to_cpu(es->s_first_data_block)) %
505*4882a593Smuzhiyun EXT2_BLOCKS_PER_GROUP(sb);
506*4882a593Smuzhiyun /*
507*4882a593Smuzhiyun * Check to see if we are freeing blocks across a group
508*4882a593Smuzhiyun * boundary.
509*4882a593Smuzhiyun */
510*4882a593Smuzhiyun if (bit + count > EXT2_BLOCKS_PER_GROUP(sb)) {
511*4882a593Smuzhiyun overflow = bit + count - EXT2_BLOCKS_PER_GROUP(sb);
512*4882a593Smuzhiyun count -= overflow;
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun brelse(bitmap_bh);
515*4882a593Smuzhiyun bitmap_bh = read_block_bitmap(sb, block_group);
516*4882a593Smuzhiyun if (!bitmap_bh)
517*4882a593Smuzhiyun goto error_return;
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun desc = ext2_get_group_desc (sb, block_group, &bh2);
520*4882a593Smuzhiyun if (!desc)
521*4882a593Smuzhiyun goto error_return;
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) ||
524*4882a593Smuzhiyun in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) ||
525*4882a593Smuzhiyun in_range (block, le32_to_cpu(desc->bg_inode_table),
526*4882a593Smuzhiyun sbi->s_itb_per_group) ||
527*4882a593Smuzhiyun in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table),
528*4882a593Smuzhiyun sbi->s_itb_per_group)) {
529*4882a593Smuzhiyun ext2_error (sb, "ext2_free_blocks",
530*4882a593Smuzhiyun "Freeing blocks in system zones - "
531*4882a593Smuzhiyun "Block = %lu, count = %lu",
532*4882a593Smuzhiyun block, count);
533*4882a593Smuzhiyun goto error_return;
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun for (i = 0, group_freed = 0; i < count; i++) {
537*4882a593Smuzhiyun if (!ext2_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
538*4882a593Smuzhiyun bit + i, bitmap_bh->b_data)) {
539*4882a593Smuzhiyun ext2_error(sb, __func__,
540*4882a593Smuzhiyun "bit already cleared for block %lu", block + i);
541*4882a593Smuzhiyun } else {
542*4882a593Smuzhiyun group_freed++;
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun }
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun mark_buffer_dirty(bitmap_bh);
547*4882a593Smuzhiyun if (sb->s_flags & SB_SYNCHRONOUS)
548*4882a593Smuzhiyun sync_dirty_buffer(bitmap_bh);
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun group_adjust_blocks(sb, block_group, desc, bh2, group_freed);
551*4882a593Smuzhiyun freed += group_freed;
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun if (overflow) {
554*4882a593Smuzhiyun block += count;
555*4882a593Smuzhiyun count = overflow;
556*4882a593Smuzhiyun goto do_more;
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun error_return:
559*4882a593Smuzhiyun brelse(bitmap_bh);
560*4882a593Smuzhiyun if (freed) {
561*4882a593Smuzhiyun percpu_counter_add(&sbi->s_freeblocks_counter, freed);
562*4882a593Smuzhiyun dquot_free_block_nodirty(inode, freed);
563*4882a593Smuzhiyun mark_inode_dirty(inode);
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun /**
568*4882a593Smuzhiyun * bitmap_search_next_usable_block()
569*4882a593Smuzhiyun * @start: the starting block (group relative) of the search
570*4882a593Smuzhiyun * @bh: bufferhead contains the block group bitmap
571*4882a593Smuzhiyun * @maxblocks: the ending block (group relative) of the reservation
572*4882a593Smuzhiyun *
573*4882a593Smuzhiyun * The bitmap search --- search forward through the actual bitmap on disk until
574*4882a593Smuzhiyun * we find a bit free.
575*4882a593Smuzhiyun */
576*4882a593Smuzhiyun static ext2_grpblk_t
bitmap_search_next_usable_block(ext2_grpblk_t start,struct buffer_head * bh,ext2_grpblk_t maxblocks)577*4882a593Smuzhiyun bitmap_search_next_usable_block(ext2_grpblk_t start, struct buffer_head *bh,
578*4882a593Smuzhiyun ext2_grpblk_t maxblocks)
579*4882a593Smuzhiyun {
580*4882a593Smuzhiyun ext2_grpblk_t next;
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun next = ext2_find_next_zero_bit(bh->b_data, maxblocks, start);
583*4882a593Smuzhiyun if (next >= maxblocks)
584*4882a593Smuzhiyun return -1;
585*4882a593Smuzhiyun return next;
586*4882a593Smuzhiyun }
587*4882a593Smuzhiyun
588*4882a593Smuzhiyun /**
589*4882a593Smuzhiyun * find_next_usable_block()
590*4882a593Smuzhiyun * @start: the starting block (group relative) to find next
591*4882a593Smuzhiyun * allocatable block in bitmap.
592*4882a593Smuzhiyun * @bh: bufferhead contains the block group bitmap
593*4882a593Smuzhiyun * @maxblocks: the ending block (group relative) for the search
594*4882a593Smuzhiyun *
595*4882a593Smuzhiyun * Find an allocatable block in a bitmap. We perform the "most
596*4882a593Smuzhiyun * appropriate allocation" algorithm of looking for a free block near
597*4882a593Smuzhiyun * the initial goal; then for a free byte somewhere in the bitmap;
598*4882a593Smuzhiyun * then for any free bit in the bitmap.
599*4882a593Smuzhiyun */
600*4882a593Smuzhiyun static ext2_grpblk_t
find_next_usable_block(int start,struct buffer_head * bh,int maxblocks)601*4882a593Smuzhiyun find_next_usable_block(int start, struct buffer_head *bh, int maxblocks)
602*4882a593Smuzhiyun {
603*4882a593Smuzhiyun ext2_grpblk_t here, next;
604*4882a593Smuzhiyun char *p, *r;
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun if (start > 0) {
607*4882a593Smuzhiyun /*
608*4882a593Smuzhiyun * The goal was occupied; search forward for a free
609*4882a593Smuzhiyun * block within the next XX blocks.
610*4882a593Smuzhiyun *
611*4882a593Smuzhiyun * end_goal is more or less random, but it has to be
612*4882a593Smuzhiyun * less than EXT2_BLOCKS_PER_GROUP. Aligning up to the
613*4882a593Smuzhiyun * next 64-bit boundary is simple..
614*4882a593Smuzhiyun */
615*4882a593Smuzhiyun ext2_grpblk_t end_goal = (start + 63) & ~63;
616*4882a593Smuzhiyun if (end_goal > maxblocks)
617*4882a593Smuzhiyun end_goal = maxblocks;
618*4882a593Smuzhiyun here = ext2_find_next_zero_bit(bh->b_data, end_goal, start);
619*4882a593Smuzhiyun if (here < end_goal)
620*4882a593Smuzhiyun return here;
621*4882a593Smuzhiyun ext2_debug("Bit not found near goal\n");
622*4882a593Smuzhiyun }
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun here = start;
625*4882a593Smuzhiyun if (here < 0)
626*4882a593Smuzhiyun here = 0;
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun p = ((char *)bh->b_data) + (here >> 3);
629*4882a593Smuzhiyun r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3));
630*4882a593Smuzhiyun next = (r - ((char *)bh->b_data)) << 3;
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun if (next < maxblocks && next >= here)
633*4882a593Smuzhiyun return next;
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun here = bitmap_search_next_usable_block(here, bh, maxblocks);
636*4882a593Smuzhiyun return here;
637*4882a593Smuzhiyun }
638*4882a593Smuzhiyun
639*4882a593Smuzhiyun /**
640*4882a593Smuzhiyun * ext2_try_to_allocate()
641*4882a593Smuzhiyun * @sb: superblock
642*4882a593Smuzhiyun * @group: given allocation block group
643*4882a593Smuzhiyun * @bitmap_bh: bufferhead holds the block bitmap
644*4882a593Smuzhiyun * @grp_goal: given target block within the group
645*4882a593Smuzhiyun * @count: target number of blocks to allocate
646*4882a593Smuzhiyun * @my_rsv: reservation window
647*4882a593Smuzhiyun *
648*4882a593Smuzhiyun * Attempt to allocate blocks within a give range. Set the range of allocation
649*4882a593Smuzhiyun * first, then find the first free bit(s) from the bitmap (within the range),
650*4882a593Smuzhiyun * and at last, allocate the blocks by claiming the found free bit as allocated.
651*4882a593Smuzhiyun *
652*4882a593Smuzhiyun * To set the range of this allocation:
653*4882a593Smuzhiyun * if there is a reservation window, only try to allocate block(s)
654*4882a593Smuzhiyun * from the file's own reservation window;
655*4882a593Smuzhiyun * Otherwise, the allocation range starts from the give goal block,
656*4882a593Smuzhiyun * ends at the block group's last block.
657*4882a593Smuzhiyun *
658*4882a593Smuzhiyun * If we failed to allocate the desired block then we may end up crossing to a
659*4882a593Smuzhiyun * new bitmap.
660*4882a593Smuzhiyun */
661*4882a593Smuzhiyun static int
ext2_try_to_allocate(struct super_block * sb,int group,struct buffer_head * bitmap_bh,ext2_grpblk_t grp_goal,unsigned long * count,struct ext2_reserve_window * my_rsv)662*4882a593Smuzhiyun ext2_try_to_allocate(struct super_block *sb, int group,
663*4882a593Smuzhiyun struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal,
664*4882a593Smuzhiyun unsigned long *count,
665*4882a593Smuzhiyun struct ext2_reserve_window *my_rsv)
666*4882a593Smuzhiyun {
667*4882a593Smuzhiyun ext2_fsblk_t group_first_block = ext2_group_first_block_no(sb, group);
668*4882a593Smuzhiyun ext2_fsblk_t group_last_block = ext2_group_last_block_no(sb, group);
669*4882a593Smuzhiyun ext2_grpblk_t start, end;
670*4882a593Smuzhiyun unsigned long num = 0;
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun start = 0;
673*4882a593Smuzhiyun end = group_last_block - group_first_block + 1;
674*4882a593Smuzhiyun /* we do allocation within the reservation window if we have a window */
675*4882a593Smuzhiyun if (my_rsv) {
676*4882a593Smuzhiyun if (my_rsv->_rsv_start >= group_first_block)
677*4882a593Smuzhiyun start = my_rsv->_rsv_start - group_first_block;
678*4882a593Smuzhiyun if (my_rsv->_rsv_end < group_last_block)
679*4882a593Smuzhiyun end = my_rsv->_rsv_end - group_first_block + 1;
680*4882a593Smuzhiyun if (grp_goal < start || grp_goal >= end)
681*4882a593Smuzhiyun grp_goal = -1;
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun BUG_ON(start > EXT2_BLOCKS_PER_GROUP(sb));
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun if (grp_goal < 0) {
686*4882a593Smuzhiyun grp_goal = find_next_usable_block(start, bitmap_bh, end);
687*4882a593Smuzhiyun if (grp_goal < 0)
688*4882a593Smuzhiyun goto fail_access;
689*4882a593Smuzhiyun if (!my_rsv) {
690*4882a593Smuzhiyun int i;
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun for (i = 0; i < 7 && grp_goal > start &&
693*4882a593Smuzhiyun !ext2_test_bit(grp_goal - 1,
694*4882a593Smuzhiyun bitmap_bh->b_data);
695*4882a593Smuzhiyun i++, grp_goal--)
696*4882a593Smuzhiyun ;
697*4882a593Smuzhiyun }
698*4882a593Smuzhiyun }
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun for (; num < *count && grp_goal < end; grp_goal++) {
701*4882a593Smuzhiyun if (ext2_set_bit_atomic(sb_bgl_lock(EXT2_SB(sb), group),
702*4882a593Smuzhiyun grp_goal, bitmap_bh->b_data)) {
703*4882a593Smuzhiyun if (num == 0)
704*4882a593Smuzhiyun continue;
705*4882a593Smuzhiyun break;
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun num++;
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun if (num == 0)
711*4882a593Smuzhiyun goto fail_access;
712*4882a593Smuzhiyun
713*4882a593Smuzhiyun *count = num;
714*4882a593Smuzhiyun return grp_goal - num;
715*4882a593Smuzhiyun fail_access:
716*4882a593Smuzhiyun return -1;
717*4882a593Smuzhiyun }
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun /**
720*4882a593Smuzhiyun * find_next_reservable_window():
721*4882a593Smuzhiyun * find a reservable space within the given range.
722*4882a593Smuzhiyun * It does not allocate the reservation window for now:
723*4882a593Smuzhiyun * alloc_new_reservation() will do the work later.
724*4882a593Smuzhiyun *
725*4882a593Smuzhiyun * @search_head: the head of the searching list;
726*4882a593Smuzhiyun * This is not necessarily the list head of the whole filesystem
727*4882a593Smuzhiyun *
728*4882a593Smuzhiyun * We have both head and start_block to assist the search
729*4882a593Smuzhiyun * for the reservable space. The list starts from head,
730*4882a593Smuzhiyun * but we will shift to the place where start_block is,
731*4882a593Smuzhiyun * then start from there, when looking for a reservable space.
732*4882a593Smuzhiyun *
733*4882a593Smuzhiyun * @sb: the super block.
734*4882a593Smuzhiyun *
735*4882a593Smuzhiyun * @start_block: the first block we consider to start the real search from
736*4882a593Smuzhiyun *
737*4882a593Smuzhiyun * @last_block:
738*4882a593Smuzhiyun * the maximum block number that our goal reservable space
739*4882a593Smuzhiyun * could start from. This is normally the last block in this
740*4882a593Smuzhiyun * group. The search will end when we found the start of next
741*4882a593Smuzhiyun * possible reservable space is out of this boundary.
742*4882a593Smuzhiyun * This could handle the cross boundary reservation window
743*4882a593Smuzhiyun * request.
744*4882a593Smuzhiyun *
745*4882a593Smuzhiyun * basically we search from the given range, rather than the whole
746*4882a593Smuzhiyun * reservation double linked list, (start_block, last_block)
747*4882a593Smuzhiyun * to find a free region that is of my size and has not
748*4882a593Smuzhiyun * been reserved.
749*4882a593Smuzhiyun *
750*4882a593Smuzhiyun */
find_next_reservable_window(struct ext2_reserve_window_node * search_head,struct ext2_reserve_window_node * my_rsv,struct super_block * sb,ext2_fsblk_t start_block,ext2_fsblk_t last_block)751*4882a593Smuzhiyun static int find_next_reservable_window(
752*4882a593Smuzhiyun struct ext2_reserve_window_node *search_head,
753*4882a593Smuzhiyun struct ext2_reserve_window_node *my_rsv,
754*4882a593Smuzhiyun struct super_block * sb,
755*4882a593Smuzhiyun ext2_fsblk_t start_block,
756*4882a593Smuzhiyun ext2_fsblk_t last_block)
757*4882a593Smuzhiyun {
758*4882a593Smuzhiyun struct rb_node *next;
759*4882a593Smuzhiyun struct ext2_reserve_window_node *rsv, *prev;
760*4882a593Smuzhiyun ext2_fsblk_t cur;
761*4882a593Smuzhiyun int size = my_rsv->rsv_goal_size;
762*4882a593Smuzhiyun
763*4882a593Smuzhiyun /* TODO: make the start of the reservation window byte-aligned */
764*4882a593Smuzhiyun /* cur = *start_block & ~7;*/
765*4882a593Smuzhiyun cur = start_block;
766*4882a593Smuzhiyun rsv = search_head;
767*4882a593Smuzhiyun if (!rsv)
768*4882a593Smuzhiyun return -1;
769*4882a593Smuzhiyun
770*4882a593Smuzhiyun while (1) {
771*4882a593Smuzhiyun if (cur <= rsv->rsv_end)
772*4882a593Smuzhiyun cur = rsv->rsv_end + 1;
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun /* TODO?
775*4882a593Smuzhiyun * in the case we could not find a reservable space
776*4882a593Smuzhiyun * that is what is expected, during the re-search, we could
777*4882a593Smuzhiyun * remember what's the largest reservable space we could have
778*4882a593Smuzhiyun * and return that one.
779*4882a593Smuzhiyun *
780*4882a593Smuzhiyun * For now it will fail if we could not find the reservable
781*4882a593Smuzhiyun * space with expected-size (or more)...
782*4882a593Smuzhiyun */
783*4882a593Smuzhiyun if (cur > last_block)
784*4882a593Smuzhiyun return -1; /* fail */
785*4882a593Smuzhiyun
786*4882a593Smuzhiyun prev = rsv;
787*4882a593Smuzhiyun next = rb_next(&rsv->rsv_node);
788*4882a593Smuzhiyun rsv = rb_entry(next,struct ext2_reserve_window_node,rsv_node);
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun /*
791*4882a593Smuzhiyun * Reached the last reservation, we can just append to the
792*4882a593Smuzhiyun * previous one.
793*4882a593Smuzhiyun */
794*4882a593Smuzhiyun if (!next)
795*4882a593Smuzhiyun break;
796*4882a593Smuzhiyun
797*4882a593Smuzhiyun if (cur + size <= rsv->rsv_start) {
798*4882a593Smuzhiyun /*
799*4882a593Smuzhiyun * Found a reserveable space big enough. We could
800*4882a593Smuzhiyun * have a reservation across the group boundary here
801*4882a593Smuzhiyun */
802*4882a593Smuzhiyun break;
803*4882a593Smuzhiyun }
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun /*
806*4882a593Smuzhiyun * we come here either :
807*4882a593Smuzhiyun * when we reach the end of the whole list,
808*4882a593Smuzhiyun * and there is empty reservable space after last entry in the list.
809*4882a593Smuzhiyun * append it to the end of the list.
810*4882a593Smuzhiyun *
811*4882a593Smuzhiyun * or we found one reservable space in the middle of the list,
812*4882a593Smuzhiyun * return the reservation window that we could append to.
813*4882a593Smuzhiyun * succeed.
814*4882a593Smuzhiyun */
815*4882a593Smuzhiyun
816*4882a593Smuzhiyun if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window)))
817*4882a593Smuzhiyun rsv_window_remove(sb, my_rsv);
818*4882a593Smuzhiyun
819*4882a593Smuzhiyun /*
820*4882a593Smuzhiyun * Let's book the whole available window for now. We will check the
821*4882a593Smuzhiyun * disk bitmap later and then, if there are free blocks then we adjust
822*4882a593Smuzhiyun * the window size if it's larger than requested.
823*4882a593Smuzhiyun * Otherwise, we will remove this node from the tree next time
824*4882a593Smuzhiyun * call find_next_reservable_window.
825*4882a593Smuzhiyun */
826*4882a593Smuzhiyun my_rsv->rsv_start = cur;
827*4882a593Smuzhiyun my_rsv->rsv_end = cur + size - 1;
828*4882a593Smuzhiyun my_rsv->rsv_alloc_hit = 0;
829*4882a593Smuzhiyun
830*4882a593Smuzhiyun if (prev != my_rsv)
831*4882a593Smuzhiyun ext2_rsv_window_add(sb, my_rsv);
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun return 0;
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun
836*4882a593Smuzhiyun /**
837*4882a593Smuzhiyun * alloc_new_reservation()--allocate a new reservation window
838*4882a593Smuzhiyun *
839*4882a593Smuzhiyun * To make a new reservation, we search part of the filesystem
840*4882a593Smuzhiyun * reservation list (the list that inside the group). We try to
841*4882a593Smuzhiyun * allocate a new reservation window near the allocation goal,
842*4882a593Smuzhiyun * or the beginning of the group, if there is no goal.
843*4882a593Smuzhiyun *
844*4882a593Smuzhiyun * We first find a reservable space after the goal, then from
845*4882a593Smuzhiyun * there, we check the bitmap for the first free block after
846*4882a593Smuzhiyun * it. If there is no free block until the end of group, then the
847*4882a593Smuzhiyun * whole group is full, we failed. Otherwise, check if the free
848*4882a593Smuzhiyun * block is inside the expected reservable space, if so, we
849*4882a593Smuzhiyun * succeed.
850*4882a593Smuzhiyun * If the first free block is outside the reservable space, then
851*4882a593Smuzhiyun * start from the first free block, we search for next available
852*4882a593Smuzhiyun * space, and go on.
853*4882a593Smuzhiyun *
854*4882a593Smuzhiyun * on succeed, a new reservation will be found and inserted into the list
855*4882a593Smuzhiyun * It contains at least one free block, and it does not overlap with other
856*4882a593Smuzhiyun * reservation windows.
857*4882a593Smuzhiyun *
858*4882a593Smuzhiyun * failed: we failed to find a reservation window in this group
859*4882a593Smuzhiyun *
860*4882a593Smuzhiyun * @my_rsv: the reservation
861*4882a593Smuzhiyun *
862*4882a593Smuzhiyun * @grp_goal: The goal (group-relative). It is where the search for a
863*4882a593Smuzhiyun * free reservable space should start from.
864*4882a593Smuzhiyun * if we have a goal(goal >0 ), then start from there,
865*4882a593Smuzhiyun * no goal(goal = -1), we start from the first block
866*4882a593Smuzhiyun * of the group.
867*4882a593Smuzhiyun *
868*4882a593Smuzhiyun * @sb: the super block
869*4882a593Smuzhiyun * @group: the group we are trying to allocate in
870*4882a593Smuzhiyun * @bitmap_bh: the block group block bitmap
871*4882a593Smuzhiyun *
872*4882a593Smuzhiyun */
alloc_new_reservation(struct ext2_reserve_window_node * my_rsv,ext2_grpblk_t grp_goal,struct super_block * sb,unsigned int group,struct buffer_head * bitmap_bh)873*4882a593Smuzhiyun static int alloc_new_reservation(struct ext2_reserve_window_node *my_rsv,
874*4882a593Smuzhiyun ext2_grpblk_t grp_goal, struct super_block *sb,
875*4882a593Smuzhiyun unsigned int group, struct buffer_head *bitmap_bh)
876*4882a593Smuzhiyun {
877*4882a593Smuzhiyun struct ext2_reserve_window_node *search_head;
878*4882a593Smuzhiyun ext2_fsblk_t group_first_block, group_end_block, start_block;
879*4882a593Smuzhiyun ext2_grpblk_t first_free_block;
880*4882a593Smuzhiyun struct rb_root *fs_rsv_root = &EXT2_SB(sb)->s_rsv_window_root;
881*4882a593Smuzhiyun unsigned long size;
882*4882a593Smuzhiyun int ret;
883*4882a593Smuzhiyun spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock;
884*4882a593Smuzhiyun
885*4882a593Smuzhiyun group_first_block = ext2_group_first_block_no(sb, group);
886*4882a593Smuzhiyun group_end_block = ext2_group_last_block_no(sb, group);
887*4882a593Smuzhiyun
888*4882a593Smuzhiyun if (grp_goal < 0)
889*4882a593Smuzhiyun start_block = group_first_block;
890*4882a593Smuzhiyun else
891*4882a593Smuzhiyun start_block = grp_goal + group_first_block;
892*4882a593Smuzhiyun
893*4882a593Smuzhiyun size = my_rsv->rsv_goal_size;
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun if (!rsv_is_empty(&my_rsv->rsv_window)) {
896*4882a593Smuzhiyun /*
897*4882a593Smuzhiyun * if the old reservation is cross group boundary
898*4882a593Smuzhiyun * and if the goal is inside the old reservation window,
899*4882a593Smuzhiyun * we will come here when we just failed to allocate from
900*4882a593Smuzhiyun * the first part of the window. We still have another part
901*4882a593Smuzhiyun * that belongs to the next group. In this case, there is no
902*4882a593Smuzhiyun * point to discard our window and try to allocate a new one
903*4882a593Smuzhiyun * in this group(which will fail). we should
904*4882a593Smuzhiyun * keep the reservation window, just simply move on.
905*4882a593Smuzhiyun *
906*4882a593Smuzhiyun * Maybe we could shift the start block of the reservation
907*4882a593Smuzhiyun * window to the first block of next group.
908*4882a593Smuzhiyun */
909*4882a593Smuzhiyun
910*4882a593Smuzhiyun if ((my_rsv->rsv_start <= group_end_block) &&
911*4882a593Smuzhiyun (my_rsv->rsv_end > group_end_block) &&
912*4882a593Smuzhiyun (start_block >= my_rsv->rsv_start))
913*4882a593Smuzhiyun return -1;
914*4882a593Smuzhiyun
915*4882a593Smuzhiyun if ((my_rsv->rsv_alloc_hit >
916*4882a593Smuzhiyun (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) {
917*4882a593Smuzhiyun /*
918*4882a593Smuzhiyun * if the previously allocation hit ratio is
919*4882a593Smuzhiyun * greater than 1/2, then we double the size of
920*4882a593Smuzhiyun * the reservation window the next time,
921*4882a593Smuzhiyun * otherwise we keep the same size window
922*4882a593Smuzhiyun */
923*4882a593Smuzhiyun size = size * 2;
924*4882a593Smuzhiyun if (size > EXT2_MAX_RESERVE_BLOCKS)
925*4882a593Smuzhiyun size = EXT2_MAX_RESERVE_BLOCKS;
926*4882a593Smuzhiyun my_rsv->rsv_goal_size= size;
927*4882a593Smuzhiyun }
928*4882a593Smuzhiyun }
929*4882a593Smuzhiyun
930*4882a593Smuzhiyun spin_lock(rsv_lock);
931*4882a593Smuzhiyun /*
932*4882a593Smuzhiyun * shift the search start to the window near the goal block
933*4882a593Smuzhiyun */
934*4882a593Smuzhiyun search_head = search_reserve_window(fs_rsv_root, start_block);
935*4882a593Smuzhiyun
936*4882a593Smuzhiyun /*
937*4882a593Smuzhiyun * find_next_reservable_window() simply finds a reservable window
938*4882a593Smuzhiyun * inside the given range(start_block, group_end_block).
939*4882a593Smuzhiyun *
940*4882a593Smuzhiyun * To make sure the reservation window has a free bit inside it, we
941*4882a593Smuzhiyun * need to check the bitmap after we found a reservable window.
942*4882a593Smuzhiyun */
943*4882a593Smuzhiyun retry:
944*4882a593Smuzhiyun ret = find_next_reservable_window(search_head, my_rsv, sb,
945*4882a593Smuzhiyun start_block, group_end_block);
946*4882a593Smuzhiyun
947*4882a593Smuzhiyun if (ret == -1) {
948*4882a593Smuzhiyun if (!rsv_is_empty(&my_rsv->rsv_window))
949*4882a593Smuzhiyun rsv_window_remove(sb, my_rsv);
950*4882a593Smuzhiyun spin_unlock(rsv_lock);
951*4882a593Smuzhiyun return -1;
952*4882a593Smuzhiyun }
953*4882a593Smuzhiyun
954*4882a593Smuzhiyun /*
955*4882a593Smuzhiyun * On success, find_next_reservable_window() returns the
956*4882a593Smuzhiyun * reservation window where there is a reservable space after it.
957*4882a593Smuzhiyun * Before we reserve this reservable space, we need
958*4882a593Smuzhiyun * to make sure there is at least a free block inside this region.
959*4882a593Smuzhiyun *
960*4882a593Smuzhiyun * Search the first free bit on the block bitmap. Search starts from
961*4882a593Smuzhiyun * the start block of the reservable space we just found.
962*4882a593Smuzhiyun */
963*4882a593Smuzhiyun spin_unlock(rsv_lock);
964*4882a593Smuzhiyun first_free_block = bitmap_search_next_usable_block(
965*4882a593Smuzhiyun my_rsv->rsv_start - group_first_block,
966*4882a593Smuzhiyun bitmap_bh, group_end_block - group_first_block + 1);
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun if (first_free_block < 0) {
969*4882a593Smuzhiyun /*
970*4882a593Smuzhiyun * no free block left on the bitmap, no point
971*4882a593Smuzhiyun * to reserve the space. return failed.
972*4882a593Smuzhiyun */
973*4882a593Smuzhiyun spin_lock(rsv_lock);
974*4882a593Smuzhiyun if (!rsv_is_empty(&my_rsv->rsv_window))
975*4882a593Smuzhiyun rsv_window_remove(sb, my_rsv);
976*4882a593Smuzhiyun spin_unlock(rsv_lock);
977*4882a593Smuzhiyun return -1; /* failed */
978*4882a593Smuzhiyun }
979*4882a593Smuzhiyun
980*4882a593Smuzhiyun start_block = first_free_block + group_first_block;
981*4882a593Smuzhiyun /*
982*4882a593Smuzhiyun * check if the first free block is within the
983*4882a593Smuzhiyun * free space we just reserved
984*4882a593Smuzhiyun */
985*4882a593Smuzhiyun if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end)
986*4882a593Smuzhiyun return 0; /* success */
987*4882a593Smuzhiyun /*
988*4882a593Smuzhiyun * if the first free bit we found is out of the reservable space
989*4882a593Smuzhiyun * continue search for next reservable space,
990*4882a593Smuzhiyun * start from where the free block is,
991*4882a593Smuzhiyun * we also shift the list head to where we stopped last time
992*4882a593Smuzhiyun */
993*4882a593Smuzhiyun search_head = my_rsv;
994*4882a593Smuzhiyun spin_lock(rsv_lock);
995*4882a593Smuzhiyun goto retry;
996*4882a593Smuzhiyun }
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun /**
999*4882a593Smuzhiyun * try_to_extend_reservation()
1000*4882a593Smuzhiyun * @my_rsv: given reservation window
1001*4882a593Smuzhiyun * @sb: super block
1002*4882a593Smuzhiyun * @size: the delta to extend
1003*4882a593Smuzhiyun *
1004*4882a593Smuzhiyun * Attempt to expand the reservation window large enough to have
1005*4882a593Smuzhiyun * required number of free blocks
1006*4882a593Smuzhiyun *
1007*4882a593Smuzhiyun * Since ext2_try_to_allocate() will always allocate blocks within
1008*4882a593Smuzhiyun * the reservation window range, if the window size is too small,
1009*4882a593Smuzhiyun * multiple blocks allocation has to stop at the end of the reservation
1010*4882a593Smuzhiyun * window. To make this more efficient, given the total number of
1011*4882a593Smuzhiyun * blocks needed and the current size of the window, we try to
1012*4882a593Smuzhiyun * expand the reservation window size if necessary on a best-effort
1013*4882a593Smuzhiyun * basis before ext2_new_blocks() tries to allocate blocks.
1014*4882a593Smuzhiyun */
try_to_extend_reservation(struct ext2_reserve_window_node * my_rsv,struct super_block * sb,int size)1015*4882a593Smuzhiyun static void try_to_extend_reservation(struct ext2_reserve_window_node *my_rsv,
1016*4882a593Smuzhiyun struct super_block *sb, int size)
1017*4882a593Smuzhiyun {
1018*4882a593Smuzhiyun struct ext2_reserve_window_node *next_rsv;
1019*4882a593Smuzhiyun struct rb_node *next;
1020*4882a593Smuzhiyun spinlock_t *rsv_lock = &EXT2_SB(sb)->s_rsv_window_lock;
1021*4882a593Smuzhiyun
1022*4882a593Smuzhiyun if (!spin_trylock(rsv_lock))
1023*4882a593Smuzhiyun return;
1024*4882a593Smuzhiyun
1025*4882a593Smuzhiyun next = rb_next(&my_rsv->rsv_node);
1026*4882a593Smuzhiyun
1027*4882a593Smuzhiyun if (!next)
1028*4882a593Smuzhiyun my_rsv->rsv_end += size;
1029*4882a593Smuzhiyun else {
1030*4882a593Smuzhiyun next_rsv = rb_entry(next, struct ext2_reserve_window_node, rsv_node);
1031*4882a593Smuzhiyun
1032*4882a593Smuzhiyun if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size)
1033*4882a593Smuzhiyun my_rsv->rsv_end += size;
1034*4882a593Smuzhiyun else
1035*4882a593Smuzhiyun my_rsv->rsv_end = next_rsv->rsv_start - 1;
1036*4882a593Smuzhiyun }
1037*4882a593Smuzhiyun spin_unlock(rsv_lock);
1038*4882a593Smuzhiyun }
1039*4882a593Smuzhiyun
1040*4882a593Smuzhiyun /**
1041*4882a593Smuzhiyun * ext2_try_to_allocate_with_rsv()
1042*4882a593Smuzhiyun * @sb: superblock
1043*4882a593Smuzhiyun * @group: given allocation block group
1044*4882a593Smuzhiyun * @bitmap_bh: bufferhead holds the block bitmap
1045*4882a593Smuzhiyun * @grp_goal: given target block within the group
1046*4882a593Smuzhiyun * @count: target number of blocks to allocate
1047*4882a593Smuzhiyun * @my_rsv: reservation window
1048*4882a593Smuzhiyun *
1049*4882a593Smuzhiyun * This is the main function used to allocate a new block and its reservation
1050*4882a593Smuzhiyun * window.
1051*4882a593Smuzhiyun *
1052*4882a593Smuzhiyun * Each time when a new block allocation is need, first try to allocate from
1053*4882a593Smuzhiyun * its own reservation. If it does not have a reservation window, instead of
1054*4882a593Smuzhiyun * looking for a free bit on bitmap first, then look up the reservation list to
1055*4882a593Smuzhiyun * see if it is inside somebody else's reservation window, we try to allocate a
1056*4882a593Smuzhiyun * reservation window for it starting from the goal first. Then do the block
1057*4882a593Smuzhiyun * allocation within the reservation window.
1058*4882a593Smuzhiyun *
1059*4882a593Smuzhiyun * This will avoid keeping on searching the reservation list again and
1060*4882a593Smuzhiyun * again when somebody is looking for a free block (without
1061*4882a593Smuzhiyun * reservation), and there are lots of free blocks, but they are all
1062*4882a593Smuzhiyun * being reserved.
1063*4882a593Smuzhiyun *
1064*4882a593Smuzhiyun * We use a red-black tree for the per-filesystem reservation list.
1065*4882a593Smuzhiyun */
1066*4882a593Smuzhiyun static ext2_grpblk_t
ext2_try_to_allocate_with_rsv(struct super_block * sb,unsigned int group,struct buffer_head * bitmap_bh,ext2_grpblk_t grp_goal,struct ext2_reserve_window_node * my_rsv,unsigned long * count)1067*4882a593Smuzhiyun ext2_try_to_allocate_with_rsv(struct super_block *sb, unsigned int group,
1068*4882a593Smuzhiyun struct buffer_head *bitmap_bh, ext2_grpblk_t grp_goal,
1069*4882a593Smuzhiyun struct ext2_reserve_window_node * my_rsv,
1070*4882a593Smuzhiyun unsigned long *count)
1071*4882a593Smuzhiyun {
1072*4882a593Smuzhiyun ext2_fsblk_t group_first_block, group_last_block;
1073*4882a593Smuzhiyun ext2_grpblk_t ret = 0;
1074*4882a593Smuzhiyun unsigned long num = *count;
1075*4882a593Smuzhiyun
1076*4882a593Smuzhiyun /*
1077*4882a593Smuzhiyun * we don't deal with reservation when
1078*4882a593Smuzhiyun * filesystem is mounted without reservation
1079*4882a593Smuzhiyun * or the file is not a regular file
1080*4882a593Smuzhiyun * or last attempt to allocate a block with reservation turned on failed
1081*4882a593Smuzhiyun */
1082*4882a593Smuzhiyun if (my_rsv == NULL) {
1083*4882a593Smuzhiyun return ext2_try_to_allocate(sb, group, bitmap_bh,
1084*4882a593Smuzhiyun grp_goal, count, NULL);
1085*4882a593Smuzhiyun }
1086*4882a593Smuzhiyun /*
1087*4882a593Smuzhiyun * grp_goal is a group relative block number (if there is a goal)
1088*4882a593Smuzhiyun * 0 <= grp_goal < EXT2_BLOCKS_PER_GROUP(sb)
1089*4882a593Smuzhiyun * first block is a filesystem wide block number
1090*4882a593Smuzhiyun * first block is the block number of the first block in this group
1091*4882a593Smuzhiyun */
1092*4882a593Smuzhiyun group_first_block = ext2_group_first_block_no(sb, group);
1093*4882a593Smuzhiyun group_last_block = ext2_group_last_block_no(sb, group);
1094*4882a593Smuzhiyun
1095*4882a593Smuzhiyun /*
1096*4882a593Smuzhiyun * Basically we will allocate a new block from inode's reservation
1097*4882a593Smuzhiyun * window.
1098*4882a593Smuzhiyun *
1099*4882a593Smuzhiyun * We need to allocate a new reservation window, if:
1100*4882a593Smuzhiyun * a) inode does not have a reservation window; or
1101*4882a593Smuzhiyun * b) last attempt to allocate a block from existing reservation
1102*4882a593Smuzhiyun * failed; or
1103*4882a593Smuzhiyun * c) we come here with a goal and with a reservation window
1104*4882a593Smuzhiyun *
1105*4882a593Smuzhiyun * We do not need to allocate a new reservation window if we come here
1106*4882a593Smuzhiyun * at the beginning with a goal and the goal is inside the window, or
1107*4882a593Smuzhiyun * we don't have a goal but already have a reservation window.
1108*4882a593Smuzhiyun * then we could go to allocate from the reservation window directly.
1109*4882a593Smuzhiyun */
1110*4882a593Smuzhiyun while (1) {
1111*4882a593Smuzhiyun if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) ||
1112*4882a593Smuzhiyun !goal_in_my_reservation(&my_rsv->rsv_window,
1113*4882a593Smuzhiyun grp_goal, group, sb)) {
1114*4882a593Smuzhiyun if (my_rsv->rsv_goal_size < *count)
1115*4882a593Smuzhiyun my_rsv->rsv_goal_size = *count;
1116*4882a593Smuzhiyun ret = alloc_new_reservation(my_rsv, grp_goal, sb,
1117*4882a593Smuzhiyun group, bitmap_bh);
1118*4882a593Smuzhiyun if (ret < 0)
1119*4882a593Smuzhiyun break; /* failed */
1120*4882a593Smuzhiyun
1121*4882a593Smuzhiyun if (!goal_in_my_reservation(&my_rsv->rsv_window,
1122*4882a593Smuzhiyun grp_goal, group, sb))
1123*4882a593Smuzhiyun grp_goal = -1;
1124*4882a593Smuzhiyun } else if (grp_goal >= 0) {
1125*4882a593Smuzhiyun int curr = my_rsv->rsv_end -
1126*4882a593Smuzhiyun (grp_goal + group_first_block) + 1;
1127*4882a593Smuzhiyun
1128*4882a593Smuzhiyun if (curr < *count)
1129*4882a593Smuzhiyun try_to_extend_reservation(my_rsv, sb,
1130*4882a593Smuzhiyun *count - curr);
1131*4882a593Smuzhiyun }
1132*4882a593Smuzhiyun
1133*4882a593Smuzhiyun if ((my_rsv->rsv_start > group_last_block) ||
1134*4882a593Smuzhiyun (my_rsv->rsv_end < group_first_block)) {
1135*4882a593Smuzhiyun rsv_window_dump(&EXT2_SB(sb)->s_rsv_window_root, 1);
1136*4882a593Smuzhiyun BUG();
1137*4882a593Smuzhiyun }
1138*4882a593Smuzhiyun ret = ext2_try_to_allocate(sb, group, bitmap_bh, grp_goal,
1139*4882a593Smuzhiyun &num, &my_rsv->rsv_window);
1140*4882a593Smuzhiyun if (ret >= 0) {
1141*4882a593Smuzhiyun my_rsv->rsv_alloc_hit += num;
1142*4882a593Smuzhiyun *count = num;
1143*4882a593Smuzhiyun break; /* succeed */
1144*4882a593Smuzhiyun }
1145*4882a593Smuzhiyun num = *count;
1146*4882a593Smuzhiyun }
1147*4882a593Smuzhiyun return ret;
1148*4882a593Smuzhiyun }
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun /**
1151*4882a593Smuzhiyun * ext2_has_free_blocks()
1152*4882a593Smuzhiyun * @sbi: in-core super block structure.
1153*4882a593Smuzhiyun *
1154*4882a593Smuzhiyun * Check if filesystem has at least 1 free block available for allocation.
1155*4882a593Smuzhiyun */
ext2_has_free_blocks(struct ext2_sb_info * sbi)1156*4882a593Smuzhiyun static int ext2_has_free_blocks(struct ext2_sb_info *sbi)
1157*4882a593Smuzhiyun {
1158*4882a593Smuzhiyun ext2_fsblk_t free_blocks, root_blocks;
1159*4882a593Smuzhiyun
1160*4882a593Smuzhiyun free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
1161*4882a593Smuzhiyun root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count);
1162*4882a593Smuzhiyun if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) &&
1163*4882a593Smuzhiyun !uid_eq(sbi->s_resuid, current_fsuid()) &&
1164*4882a593Smuzhiyun (gid_eq(sbi->s_resgid, GLOBAL_ROOT_GID) ||
1165*4882a593Smuzhiyun !in_group_p (sbi->s_resgid))) {
1166*4882a593Smuzhiyun return 0;
1167*4882a593Smuzhiyun }
1168*4882a593Smuzhiyun return 1;
1169*4882a593Smuzhiyun }
1170*4882a593Smuzhiyun
1171*4882a593Smuzhiyun /*
1172*4882a593Smuzhiyun * Returns 1 if the passed-in block region is valid; 0 if some part overlaps
1173*4882a593Smuzhiyun * with filesystem metadata blocks.
1174*4882a593Smuzhiyun */
ext2_data_block_valid(struct ext2_sb_info * sbi,ext2_fsblk_t start_blk,unsigned int count)1175*4882a593Smuzhiyun int ext2_data_block_valid(struct ext2_sb_info *sbi, ext2_fsblk_t start_blk,
1176*4882a593Smuzhiyun unsigned int count)
1177*4882a593Smuzhiyun {
1178*4882a593Smuzhiyun if ((start_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
1179*4882a593Smuzhiyun (start_blk + count - 1 < start_blk) ||
1180*4882a593Smuzhiyun (start_blk + count - 1 >= le32_to_cpu(sbi->s_es->s_blocks_count)))
1181*4882a593Smuzhiyun return 0;
1182*4882a593Smuzhiyun
1183*4882a593Smuzhiyun /* Ensure we do not step over superblock */
1184*4882a593Smuzhiyun if ((start_blk <= sbi->s_sb_block) &&
1185*4882a593Smuzhiyun (start_blk + count - 1 >= sbi->s_sb_block))
1186*4882a593Smuzhiyun return 0;
1187*4882a593Smuzhiyun
1188*4882a593Smuzhiyun return 1;
1189*4882a593Smuzhiyun }
1190*4882a593Smuzhiyun
1191*4882a593Smuzhiyun /*
1192*4882a593Smuzhiyun * ext2_new_blocks() -- core block(s) allocation function
1193*4882a593Smuzhiyun * @inode: file inode
1194*4882a593Smuzhiyun * @goal: given target block(filesystem wide)
1195*4882a593Smuzhiyun * @count: target number of blocks to allocate
1196*4882a593Smuzhiyun * @errp: error code
1197*4882a593Smuzhiyun *
1198*4882a593Smuzhiyun * ext2_new_blocks uses a goal block to assist allocation. If the goal is
1199*4882a593Smuzhiyun * free, or there is a free block within 32 blocks of the goal, that block
1200*4882a593Smuzhiyun * is allocated. Otherwise a forward search is made for a free block; within
1201*4882a593Smuzhiyun * each block group the search first looks for an entire free byte in the block
1202*4882a593Smuzhiyun * bitmap, and then for any free bit if that fails.
1203*4882a593Smuzhiyun * This function also updates quota and i_blocks field.
1204*4882a593Smuzhiyun */
ext2_new_blocks(struct inode * inode,ext2_fsblk_t goal,unsigned long * count,int * errp)1205*4882a593Smuzhiyun ext2_fsblk_t ext2_new_blocks(struct inode *inode, ext2_fsblk_t goal,
1206*4882a593Smuzhiyun unsigned long *count, int *errp)
1207*4882a593Smuzhiyun {
1208*4882a593Smuzhiyun struct buffer_head *bitmap_bh = NULL;
1209*4882a593Smuzhiyun struct buffer_head *gdp_bh;
1210*4882a593Smuzhiyun int group_no;
1211*4882a593Smuzhiyun int goal_group;
1212*4882a593Smuzhiyun ext2_grpblk_t grp_target_blk; /* blockgroup relative goal block */
1213*4882a593Smuzhiyun ext2_grpblk_t grp_alloc_blk; /* blockgroup-relative allocated block*/
1214*4882a593Smuzhiyun ext2_fsblk_t ret_block; /* filesyetem-wide allocated block */
1215*4882a593Smuzhiyun int bgi; /* blockgroup iteration index */
1216*4882a593Smuzhiyun int performed_allocation = 0;
1217*4882a593Smuzhiyun ext2_grpblk_t free_blocks; /* number of free blocks in a group */
1218*4882a593Smuzhiyun struct super_block *sb;
1219*4882a593Smuzhiyun struct ext2_group_desc *gdp;
1220*4882a593Smuzhiyun struct ext2_super_block *es;
1221*4882a593Smuzhiyun struct ext2_sb_info *sbi;
1222*4882a593Smuzhiyun struct ext2_reserve_window_node *my_rsv = NULL;
1223*4882a593Smuzhiyun struct ext2_block_alloc_info *block_i;
1224*4882a593Smuzhiyun unsigned short windowsz = 0;
1225*4882a593Smuzhiyun unsigned long ngroups;
1226*4882a593Smuzhiyun unsigned long num = *count;
1227*4882a593Smuzhiyun int ret;
1228*4882a593Smuzhiyun
1229*4882a593Smuzhiyun *errp = -ENOSPC;
1230*4882a593Smuzhiyun sb = inode->i_sb;
1231*4882a593Smuzhiyun
1232*4882a593Smuzhiyun /*
1233*4882a593Smuzhiyun * Check quota for allocation of this block.
1234*4882a593Smuzhiyun */
1235*4882a593Smuzhiyun ret = dquot_alloc_block(inode, num);
1236*4882a593Smuzhiyun if (ret) {
1237*4882a593Smuzhiyun *errp = ret;
1238*4882a593Smuzhiyun return 0;
1239*4882a593Smuzhiyun }
1240*4882a593Smuzhiyun
1241*4882a593Smuzhiyun sbi = EXT2_SB(sb);
1242*4882a593Smuzhiyun es = EXT2_SB(sb)->s_es;
1243*4882a593Smuzhiyun ext2_debug("goal=%lu.\n", goal);
1244*4882a593Smuzhiyun /*
1245*4882a593Smuzhiyun * Allocate a block from reservation only when
1246*4882a593Smuzhiyun * filesystem is mounted with reservation(default,-o reservation), and
1247*4882a593Smuzhiyun * it's a regular file, and
1248*4882a593Smuzhiyun * the desired window size is greater than 0 (One could use ioctl
1249*4882a593Smuzhiyun * command EXT2_IOC_SETRSVSZ to set the window size to 0 to turn off
1250*4882a593Smuzhiyun * reservation on that particular file)
1251*4882a593Smuzhiyun */
1252*4882a593Smuzhiyun block_i = EXT2_I(inode)->i_block_alloc_info;
1253*4882a593Smuzhiyun if (block_i) {
1254*4882a593Smuzhiyun windowsz = block_i->rsv_window_node.rsv_goal_size;
1255*4882a593Smuzhiyun if (windowsz > 0)
1256*4882a593Smuzhiyun my_rsv = &block_i->rsv_window_node;
1257*4882a593Smuzhiyun }
1258*4882a593Smuzhiyun
1259*4882a593Smuzhiyun if (!ext2_has_free_blocks(sbi)) {
1260*4882a593Smuzhiyun *errp = -ENOSPC;
1261*4882a593Smuzhiyun goto out;
1262*4882a593Smuzhiyun }
1263*4882a593Smuzhiyun
1264*4882a593Smuzhiyun /*
1265*4882a593Smuzhiyun * First, test whether the goal block is free.
1266*4882a593Smuzhiyun */
1267*4882a593Smuzhiyun if (goal < le32_to_cpu(es->s_first_data_block) ||
1268*4882a593Smuzhiyun goal >= le32_to_cpu(es->s_blocks_count))
1269*4882a593Smuzhiyun goal = le32_to_cpu(es->s_first_data_block);
1270*4882a593Smuzhiyun group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
1271*4882a593Smuzhiyun EXT2_BLOCKS_PER_GROUP(sb);
1272*4882a593Smuzhiyun goal_group = group_no;
1273*4882a593Smuzhiyun retry_alloc:
1274*4882a593Smuzhiyun gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);
1275*4882a593Smuzhiyun if (!gdp)
1276*4882a593Smuzhiyun goto io_error;
1277*4882a593Smuzhiyun
1278*4882a593Smuzhiyun free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
1279*4882a593Smuzhiyun /*
1280*4882a593Smuzhiyun * if there is not enough free blocks to make a new resevation
1281*4882a593Smuzhiyun * turn off reservation for this allocation
1282*4882a593Smuzhiyun */
1283*4882a593Smuzhiyun if (my_rsv && (free_blocks < windowsz)
1284*4882a593Smuzhiyun && (free_blocks > 0)
1285*4882a593Smuzhiyun && (rsv_is_empty(&my_rsv->rsv_window)))
1286*4882a593Smuzhiyun my_rsv = NULL;
1287*4882a593Smuzhiyun
1288*4882a593Smuzhiyun if (free_blocks > 0) {
1289*4882a593Smuzhiyun grp_target_blk = ((goal - le32_to_cpu(es->s_first_data_block)) %
1290*4882a593Smuzhiyun EXT2_BLOCKS_PER_GROUP(sb));
1291*4882a593Smuzhiyun /*
1292*4882a593Smuzhiyun * In case we retry allocation (due to fs reservation not
1293*4882a593Smuzhiyun * working out or fs corruption), the bitmap_bh is non-null
1294*4882a593Smuzhiyun * pointer and we have to release it before calling
1295*4882a593Smuzhiyun * read_block_bitmap().
1296*4882a593Smuzhiyun */
1297*4882a593Smuzhiyun brelse(bitmap_bh);
1298*4882a593Smuzhiyun bitmap_bh = read_block_bitmap(sb, group_no);
1299*4882a593Smuzhiyun if (!bitmap_bh)
1300*4882a593Smuzhiyun goto io_error;
1301*4882a593Smuzhiyun grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no,
1302*4882a593Smuzhiyun bitmap_bh, grp_target_blk,
1303*4882a593Smuzhiyun my_rsv, &num);
1304*4882a593Smuzhiyun if (grp_alloc_blk >= 0)
1305*4882a593Smuzhiyun goto allocated;
1306*4882a593Smuzhiyun }
1307*4882a593Smuzhiyun
1308*4882a593Smuzhiyun ngroups = EXT2_SB(sb)->s_groups_count;
1309*4882a593Smuzhiyun smp_rmb();
1310*4882a593Smuzhiyun
1311*4882a593Smuzhiyun /*
1312*4882a593Smuzhiyun * Now search the rest of the groups. We assume that
1313*4882a593Smuzhiyun * group_no and gdp correctly point to the last group visited.
1314*4882a593Smuzhiyun */
1315*4882a593Smuzhiyun for (bgi = 0; bgi < ngroups; bgi++) {
1316*4882a593Smuzhiyun group_no++;
1317*4882a593Smuzhiyun if (group_no >= ngroups)
1318*4882a593Smuzhiyun group_no = 0;
1319*4882a593Smuzhiyun gdp = ext2_get_group_desc(sb, group_no, &gdp_bh);
1320*4882a593Smuzhiyun if (!gdp)
1321*4882a593Smuzhiyun goto io_error;
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
1324*4882a593Smuzhiyun /*
1325*4882a593Smuzhiyun * skip this group (and avoid loading bitmap) if there
1326*4882a593Smuzhiyun * are no free blocks
1327*4882a593Smuzhiyun */
1328*4882a593Smuzhiyun if (!free_blocks)
1329*4882a593Smuzhiyun continue;
1330*4882a593Smuzhiyun /*
1331*4882a593Smuzhiyun * skip this group if the number of
1332*4882a593Smuzhiyun * free blocks is less than half of the reservation
1333*4882a593Smuzhiyun * window size.
1334*4882a593Smuzhiyun */
1335*4882a593Smuzhiyun if (my_rsv && (free_blocks <= (windowsz/2)))
1336*4882a593Smuzhiyun continue;
1337*4882a593Smuzhiyun
1338*4882a593Smuzhiyun brelse(bitmap_bh);
1339*4882a593Smuzhiyun bitmap_bh = read_block_bitmap(sb, group_no);
1340*4882a593Smuzhiyun if (!bitmap_bh)
1341*4882a593Smuzhiyun goto io_error;
1342*4882a593Smuzhiyun /*
1343*4882a593Smuzhiyun * try to allocate block(s) from this group, without a goal(-1).
1344*4882a593Smuzhiyun */
1345*4882a593Smuzhiyun grp_alloc_blk = ext2_try_to_allocate_with_rsv(sb, group_no,
1346*4882a593Smuzhiyun bitmap_bh, -1, my_rsv, &num);
1347*4882a593Smuzhiyun if (grp_alloc_blk >= 0)
1348*4882a593Smuzhiyun goto allocated;
1349*4882a593Smuzhiyun }
1350*4882a593Smuzhiyun /*
1351*4882a593Smuzhiyun * We may end up a bogus earlier ENOSPC error due to
1352*4882a593Smuzhiyun * filesystem is "full" of reservations, but
1353*4882a593Smuzhiyun * there maybe indeed free blocks available on disk
1354*4882a593Smuzhiyun * In this case, we just forget about the reservations
1355*4882a593Smuzhiyun * just do block allocation as without reservations.
1356*4882a593Smuzhiyun */
1357*4882a593Smuzhiyun if (my_rsv) {
1358*4882a593Smuzhiyun my_rsv = NULL;
1359*4882a593Smuzhiyun windowsz = 0;
1360*4882a593Smuzhiyun group_no = goal_group;
1361*4882a593Smuzhiyun goto retry_alloc;
1362*4882a593Smuzhiyun }
1363*4882a593Smuzhiyun /* No space left on the device */
1364*4882a593Smuzhiyun *errp = -ENOSPC;
1365*4882a593Smuzhiyun goto out;
1366*4882a593Smuzhiyun
1367*4882a593Smuzhiyun allocated:
1368*4882a593Smuzhiyun
1369*4882a593Smuzhiyun ext2_debug("using block group %d(%d)\n",
1370*4882a593Smuzhiyun group_no, gdp->bg_free_blocks_count);
1371*4882a593Smuzhiyun
1372*4882a593Smuzhiyun ret_block = grp_alloc_blk + ext2_group_first_block_no(sb, group_no);
1373*4882a593Smuzhiyun
1374*4882a593Smuzhiyun if (in_range(le32_to_cpu(gdp->bg_block_bitmap), ret_block, num) ||
1375*4882a593Smuzhiyun in_range(le32_to_cpu(gdp->bg_inode_bitmap), ret_block, num) ||
1376*4882a593Smuzhiyun in_range(ret_block, le32_to_cpu(gdp->bg_inode_table),
1377*4882a593Smuzhiyun EXT2_SB(sb)->s_itb_per_group) ||
1378*4882a593Smuzhiyun in_range(ret_block + num - 1, le32_to_cpu(gdp->bg_inode_table),
1379*4882a593Smuzhiyun EXT2_SB(sb)->s_itb_per_group)) {
1380*4882a593Smuzhiyun ext2_error(sb, "ext2_new_blocks",
1381*4882a593Smuzhiyun "Allocating block in system zone - "
1382*4882a593Smuzhiyun "blocks from "E2FSBLK", length %lu",
1383*4882a593Smuzhiyun ret_block, num);
1384*4882a593Smuzhiyun /*
1385*4882a593Smuzhiyun * ext2_try_to_allocate marked the blocks we allocated as in
1386*4882a593Smuzhiyun * use. So we may want to selectively mark some of the blocks
1387*4882a593Smuzhiyun * as free
1388*4882a593Smuzhiyun */
1389*4882a593Smuzhiyun num = *count;
1390*4882a593Smuzhiyun goto retry_alloc;
1391*4882a593Smuzhiyun }
1392*4882a593Smuzhiyun
1393*4882a593Smuzhiyun performed_allocation = 1;
1394*4882a593Smuzhiyun
1395*4882a593Smuzhiyun if (ret_block + num - 1 >= le32_to_cpu(es->s_blocks_count)) {
1396*4882a593Smuzhiyun ext2_error(sb, "ext2_new_blocks",
1397*4882a593Smuzhiyun "block("E2FSBLK") >= blocks count(%d) - "
1398*4882a593Smuzhiyun "block_group = %d, es == %p ", ret_block,
1399*4882a593Smuzhiyun le32_to_cpu(es->s_blocks_count), group_no, es);
1400*4882a593Smuzhiyun goto out;
1401*4882a593Smuzhiyun }
1402*4882a593Smuzhiyun
1403*4882a593Smuzhiyun group_adjust_blocks(sb, group_no, gdp, gdp_bh, -num);
1404*4882a593Smuzhiyun percpu_counter_sub(&sbi->s_freeblocks_counter, num);
1405*4882a593Smuzhiyun
1406*4882a593Smuzhiyun mark_buffer_dirty(bitmap_bh);
1407*4882a593Smuzhiyun if (sb->s_flags & SB_SYNCHRONOUS)
1408*4882a593Smuzhiyun sync_dirty_buffer(bitmap_bh);
1409*4882a593Smuzhiyun
1410*4882a593Smuzhiyun *errp = 0;
1411*4882a593Smuzhiyun brelse(bitmap_bh);
1412*4882a593Smuzhiyun if (num < *count) {
1413*4882a593Smuzhiyun dquot_free_block_nodirty(inode, *count-num);
1414*4882a593Smuzhiyun mark_inode_dirty(inode);
1415*4882a593Smuzhiyun *count = num;
1416*4882a593Smuzhiyun }
1417*4882a593Smuzhiyun return ret_block;
1418*4882a593Smuzhiyun
1419*4882a593Smuzhiyun io_error:
1420*4882a593Smuzhiyun *errp = -EIO;
1421*4882a593Smuzhiyun out:
1422*4882a593Smuzhiyun /*
1423*4882a593Smuzhiyun * Undo the block allocation
1424*4882a593Smuzhiyun */
1425*4882a593Smuzhiyun if (!performed_allocation) {
1426*4882a593Smuzhiyun dquot_free_block_nodirty(inode, *count);
1427*4882a593Smuzhiyun mark_inode_dirty(inode);
1428*4882a593Smuzhiyun }
1429*4882a593Smuzhiyun brelse(bitmap_bh);
1430*4882a593Smuzhiyun return 0;
1431*4882a593Smuzhiyun }
1432*4882a593Smuzhiyun
ext2_new_block(struct inode * inode,unsigned long goal,int * errp)1433*4882a593Smuzhiyun ext2_fsblk_t ext2_new_block(struct inode *inode, unsigned long goal, int *errp)
1434*4882a593Smuzhiyun {
1435*4882a593Smuzhiyun unsigned long count = 1;
1436*4882a593Smuzhiyun
1437*4882a593Smuzhiyun return ext2_new_blocks(inode, goal, &count, errp);
1438*4882a593Smuzhiyun }
1439*4882a593Smuzhiyun
1440*4882a593Smuzhiyun #ifdef EXT2FS_DEBUG
1441*4882a593Smuzhiyun
ext2_count_free(struct buffer_head * map,unsigned int numchars)1442*4882a593Smuzhiyun unsigned long ext2_count_free(struct buffer_head *map, unsigned int numchars)
1443*4882a593Smuzhiyun {
1444*4882a593Smuzhiyun return numchars * BITS_PER_BYTE - memweight(map->b_data, numchars);
1445*4882a593Smuzhiyun }
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun #endif /* EXT2FS_DEBUG */
1448*4882a593Smuzhiyun
ext2_count_free_blocks(struct super_block * sb)1449*4882a593Smuzhiyun unsigned long ext2_count_free_blocks (struct super_block * sb)
1450*4882a593Smuzhiyun {
1451*4882a593Smuzhiyun struct ext2_group_desc * desc;
1452*4882a593Smuzhiyun unsigned long desc_count = 0;
1453*4882a593Smuzhiyun int i;
1454*4882a593Smuzhiyun #ifdef EXT2FS_DEBUG
1455*4882a593Smuzhiyun unsigned long bitmap_count, x;
1456*4882a593Smuzhiyun struct ext2_super_block *es;
1457*4882a593Smuzhiyun
1458*4882a593Smuzhiyun es = EXT2_SB(sb)->s_es;
1459*4882a593Smuzhiyun desc_count = 0;
1460*4882a593Smuzhiyun bitmap_count = 0;
1461*4882a593Smuzhiyun desc = NULL;
1462*4882a593Smuzhiyun for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
1463*4882a593Smuzhiyun struct buffer_head *bitmap_bh;
1464*4882a593Smuzhiyun desc = ext2_get_group_desc (sb, i, NULL);
1465*4882a593Smuzhiyun if (!desc)
1466*4882a593Smuzhiyun continue;
1467*4882a593Smuzhiyun desc_count += le16_to_cpu(desc->bg_free_blocks_count);
1468*4882a593Smuzhiyun bitmap_bh = read_block_bitmap(sb, i);
1469*4882a593Smuzhiyun if (!bitmap_bh)
1470*4882a593Smuzhiyun continue;
1471*4882a593Smuzhiyun
1472*4882a593Smuzhiyun x = ext2_count_free(bitmap_bh, sb->s_blocksize);
1473*4882a593Smuzhiyun printk ("group %d: stored = %d, counted = %lu\n",
1474*4882a593Smuzhiyun i, le16_to_cpu(desc->bg_free_blocks_count), x);
1475*4882a593Smuzhiyun bitmap_count += x;
1476*4882a593Smuzhiyun brelse(bitmap_bh);
1477*4882a593Smuzhiyun }
1478*4882a593Smuzhiyun printk("ext2_count_free_blocks: stored = %lu, computed = %lu, %lu\n",
1479*4882a593Smuzhiyun (long)le32_to_cpu(es->s_free_blocks_count),
1480*4882a593Smuzhiyun desc_count, bitmap_count);
1481*4882a593Smuzhiyun return bitmap_count;
1482*4882a593Smuzhiyun #else
1483*4882a593Smuzhiyun for (i = 0; i < EXT2_SB(sb)->s_groups_count; i++) {
1484*4882a593Smuzhiyun desc = ext2_get_group_desc (sb, i, NULL);
1485*4882a593Smuzhiyun if (!desc)
1486*4882a593Smuzhiyun continue;
1487*4882a593Smuzhiyun desc_count += le16_to_cpu(desc->bg_free_blocks_count);
1488*4882a593Smuzhiyun }
1489*4882a593Smuzhiyun return desc_count;
1490*4882a593Smuzhiyun #endif
1491*4882a593Smuzhiyun }
1492*4882a593Smuzhiyun
test_root(int a,int b)1493*4882a593Smuzhiyun static inline int test_root(int a, int b)
1494*4882a593Smuzhiyun {
1495*4882a593Smuzhiyun int num = b;
1496*4882a593Smuzhiyun
1497*4882a593Smuzhiyun while (a > num)
1498*4882a593Smuzhiyun num *= b;
1499*4882a593Smuzhiyun return num == a;
1500*4882a593Smuzhiyun }
1501*4882a593Smuzhiyun
ext2_group_sparse(int group)1502*4882a593Smuzhiyun static int ext2_group_sparse(int group)
1503*4882a593Smuzhiyun {
1504*4882a593Smuzhiyun if (group <= 1)
1505*4882a593Smuzhiyun return 1;
1506*4882a593Smuzhiyun return (test_root(group, 3) || test_root(group, 5) ||
1507*4882a593Smuzhiyun test_root(group, 7));
1508*4882a593Smuzhiyun }
1509*4882a593Smuzhiyun
1510*4882a593Smuzhiyun /**
1511*4882a593Smuzhiyun * ext2_bg_has_super - number of blocks used by the superblock in group
1512*4882a593Smuzhiyun * @sb: superblock for filesystem
1513*4882a593Smuzhiyun * @group: group number to check
1514*4882a593Smuzhiyun *
1515*4882a593Smuzhiyun * Return the number of blocks used by the superblock (primary or backup)
1516*4882a593Smuzhiyun * in this group. Currently this will be only 0 or 1.
1517*4882a593Smuzhiyun */
ext2_bg_has_super(struct super_block * sb,int group)1518*4882a593Smuzhiyun int ext2_bg_has_super(struct super_block *sb, int group)
1519*4882a593Smuzhiyun {
1520*4882a593Smuzhiyun if (EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER)&&
1521*4882a593Smuzhiyun !ext2_group_sparse(group))
1522*4882a593Smuzhiyun return 0;
1523*4882a593Smuzhiyun return 1;
1524*4882a593Smuzhiyun }
1525*4882a593Smuzhiyun
1526*4882a593Smuzhiyun /**
1527*4882a593Smuzhiyun * ext2_bg_num_gdb - number of blocks used by the group table in group
1528*4882a593Smuzhiyun * @sb: superblock for filesystem
1529*4882a593Smuzhiyun * @group: group number to check
1530*4882a593Smuzhiyun *
1531*4882a593Smuzhiyun * Return the number of blocks used by the group descriptor table
1532*4882a593Smuzhiyun * (primary or backup) in this group. In the future there may be a
1533*4882a593Smuzhiyun * different number of descriptor blocks in each group.
1534*4882a593Smuzhiyun */
ext2_bg_num_gdb(struct super_block * sb,int group)1535*4882a593Smuzhiyun unsigned long ext2_bg_num_gdb(struct super_block *sb, int group)
1536*4882a593Smuzhiyun {
1537*4882a593Smuzhiyun return ext2_bg_has_super(sb, group) ? EXT2_SB(sb)->s_gdb_count : 0;
1538*4882a593Smuzhiyun }
1539*4882a593Smuzhiyun
1540