GNU Linux-libre 4.9.308-gnu1
[releases.git] / fs / ext4 / balloc.c
1 /*
2  *  linux/fs/ext4/balloc.c
3  *
4  * Copyright (C) 1992, 1993, 1994, 1995
5  * Remy Card (card@masi.ibp.fr)
6  * Laboratoire MASI - Institut Blaise Pascal
7  * Universite Pierre et Marie Curie (Paris VI)
8  *
9  *  Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993
10  *  Big-endian to little-endian byte-swapping/bitmaps by
11  *        David S. Miller (davem@caip.rutgers.edu), 1995
12  */
13
14 #include <linux/time.h>
15 #include <linux/capability.h>
16 #include <linux/fs.h>
17 #include <linux/quotaops.h>
18 #include <linux/buffer_head.h>
19 #include "ext4.h"
20 #include "ext4_jbd2.h"
21 #include "mballoc.h"
22
23 #include <trace/events/ext4.h>
24
25 static unsigned ext4_num_base_meta_clusters(struct super_block *sb,
26                                             ext4_group_t block_group);
27 /*
28  * balloc.c contains the blocks allocation and deallocation routines
29  */
30
31 /*
32  * Calculate block group number for a given block number
33  */
34 ext4_group_t ext4_get_group_number(struct super_block *sb,
35                                    ext4_fsblk_t block)
36 {
37         ext4_group_t group;
38
39         if (test_opt2(sb, STD_GROUP_SIZE))
40                 group = (block -
41                          le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) >>
42                         (EXT4_BLOCK_SIZE_BITS(sb) + EXT4_CLUSTER_BITS(sb) + 3);
43         else
44                 ext4_get_group_no_and_offset(sb, block, &group, NULL);
45         return group;
46 }
47
48 /*
49  * Calculate the block group number and offset into the block/cluster
50  * allocation bitmap, given a block number
51  */
52 void ext4_get_group_no_and_offset(struct super_block *sb, ext4_fsblk_t blocknr,
53                 ext4_group_t *blockgrpp, ext4_grpblk_t *offsetp)
54 {
55         struct ext4_super_block *es = EXT4_SB(sb)->s_es;
56         ext4_grpblk_t offset;
57
58         blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
59         offset = do_div(blocknr, EXT4_BLOCKS_PER_GROUP(sb)) >>
60                 EXT4_SB(sb)->s_cluster_bits;
61         if (offsetp)
62                 *offsetp = offset;
63         if (blockgrpp)
64                 *blockgrpp = blocknr;
65
66 }
67
68 /*
69  * Check whether the 'block' lives within the 'block_group'. Returns 1 if so
70  * and 0 otherwise.
71  */
72 static inline int ext4_block_in_group(struct super_block *sb,
73                                       ext4_fsblk_t block,
74                                       ext4_group_t block_group)
75 {
76         ext4_group_t actual_group;
77
78         actual_group = ext4_get_group_number(sb, block);
79         return (actual_group == block_group) ? 1 : 0;
80 }
81
82 /* Return the number of clusters used for file system metadata; this
83  * represents the overhead needed by the file system.
84  */
85 static unsigned ext4_num_overhead_clusters(struct super_block *sb,
86                                            ext4_group_t block_group,
87                                            struct ext4_group_desc *gdp)
88 {
89         unsigned num_clusters;
90         int block_cluster = -1, inode_cluster = -1, itbl_cluster = -1, i, c;
91         ext4_fsblk_t start = ext4_group_first_block_no(sb, block_group);
92         ext4_fsblk_t itbl_blk;
93         struct ext4_sb_info *sbi = EXT4_SB(sb);
94
95         /* This is the number of clusters used by the superblock,
96          * block group descriptors, and reserved block group
97          * descriptor blocks */
98         num_clusters = ext4_num_base_meta_clusters(sb, block_group);
99
100         /*
101          * For the allocation bitmaps and inode table, we first need
102          * to check to see if the block is in the block group.  If it
103          * is, then check to see if the cluster is already accounted
104          * for in the clusters used for the base metadata cluster, or
105          * if we can increment the base metadata cluster to include
106          * that block.  Otherwise, we will have to track the cluster
107          * used for the allocation bitmap or inode table explicitly.
108          * Normally all of these blocks are contiguous, so the special
109          * case handling shouldn't be necessary except for *very*
110          * unusual file system layouts.
111          */
112         if (ext4_block_in_group(sb, ext4_block_bitmap(sb, gdp), block_group)) {
113                 block_cluster = EXT4_B2C(sbi,
114                                          ext4_block_bitmap(sb, gdp) - start);
115                 if (block_cluster < num_clusters)
116                         block_cluster = -1;
117                 else if (block_cluster == num_clusters) {
118                         num_clusters++;
119                         block_cluster = -1;
120                 }
121         }
122
123         if (ext4_block_in_group(sb, ext4_inode_bitmap(sb, gdp), block_group)) {
124                 inode_cluster = EXT4_B2C(sbi,
125                                          ext4_inode_bitmap(sb, gdp) - start);
126                 if (inode_cluster < num_clusters)
127                         inode_cluster = -1;
128                 else if (inode_cluster == num_clusters) {
129                         num_clusters++;
130                         inode_cluster = -1;
131                 }
132         }
133
134         itbl_blk = ext4_inode_table(sb, gdp);
135         for (i = 0; i < sbi->s_itb_per_group; i++) {
136                 if (ext4_block_in_group(sb, itbl_blk + i, block_group)) {
137                         c = EXT4_B2C(sbi, itbl_blk + i - start);
138                         if ((c < num_clusters) || (c == inode_cluster) ||
139                             (c == block_cluster) || (c == itbl_cluster))
140                                 continue;
141                         if (c == num_clusters) {
142                                 num_clusters++;
143                                 continue;
144                         }
145                         num_clusters++;
146                         itbl_cluster = c;
147                 }
148         }
149
150         if (block_cluster != -1)
151                 num_clusters++;
152         if (inode_cluster != -1)
153                 num_clusters++;
154
155         return num_clusters;
156 }
157
158 static unsigned int num_clusters_in_group(struct super_block *sb,
159                                           ext4_group_t block_group)
160 {
161         unsigned int blocks;
162
163         if (block_group == ext4_get_groups_count(sb) - 1) {
164                 /*
165                  * Even though mke2fs always initializes the first and
166                  * last group, just in case some other tool was used,
167                  * we need to make sure we calculate the right free
168                  * blocks.
169                  */
170                 blocks = ext4_blocks_count(EXT4_SB(sb)->s_es) -
171                         ext4_group_first_block_no(sb, block_group);
172         } else
173                 blocks = EXT4_BLOCKS_PER_GROUP(sb);
174         return EXT4_NUM_B2C(EXT4_SB(sb), blocks);
175 }
176
177 /* Initializes an uninitialized block bitmap */
178 static int ext4_init_block_bitmap(struct super_block *sb,
179                                    struct buffer_head *bh,
180                                    ext4_group_t block_group,
181                                    struct ext4_group_desc *gdp)
182 {
183         unsigned int bit, bit_max;
184         struct ext4_sb_info *sbi = EXT4_SB(sb);
185         ext4_fsblk_t start, tmp;
186         struct ext4_group_info *grp;
187
188         J_ASSERT_BH(bh, buffer_locked(bh));
189
190         /* If checksum is bad mark all blocks used to prevent allocation
191          * essentially implementing a per-group read-only flag. */
192         if (!ext4_group_desc_csum_verify(sb, block_group, gdp)) {
193                 grp = ext4_get_group_info(sb, block_group);
194                 if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
195                         percpu_counter_sub(&sbi->s_freeclusters_counter,
196                                            grp->bb_free);
197                 set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state);
198                 if (!EXT4_MB_GRP_IBITMAP_CORRUPT(grp)) {
199                         int count;
200                         count = ext4_free_inodes_count(sb, gdp);
201                         percpu_counter_sub(&sbi->s_freeinodes_counter,
202                                            count);
203                 }
204                 set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &grp->bb_state);
205                 return -EFSBADCRC;
206         }
207         memset(bh->b_data, 0, sb->s_blocksize);
208
209         bit_max = ext4_num_base_meta_clusters(sb, block_group);
210         if ((bit_max >> 3) >= bh->b_size)
211                 return -EFSCORRUPTED;
212
213         for (bit = 0; bit < bit_max; bit++)
214                 ext4_set_bit(bit, bh->b_data);
215
216         start = ext4_group_first_block_no(sb, block_group);
217
218         /* Set bits for block and inode bitmaps, and inode table */
219         tmp = ext4_block_bitmap(sb, gdp);
220         if (ext4_block_in_group(sb, tmp, block_group))
221                 ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
222
223         tmp = ext4_inode_bitmap(sb, gdp);
224         if (ext4_block_in_group(sb, tmp, block_group))
225                 ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
226
227         tmp = ext4_inode_table(sb, gdp);
228         for (; tmp < ext4_inode_table(sb, gdp) +
229                      sbi->s_itb_per_group; tmp++) {
230                 if (ext4_block_in_group(sb, tmp, block_group))
231                         ext4_set_bit(EXT4_B2C(sbi, tmp - start), bh->b_data);
232         }
233
234         /*
235          * Also if the number of blocks within the group is less than
236          * the blocksize * 8 ( which is the size of bitmap ), set rest
237          * of the block bitmap to 1
238          */
239         ext4_mark_bitmap_end(num_clusters_in_group(sb, block_group),
240                              sb->s_blocksize * 8, bh->b_data);
241         return 0;
242 }
243
244 /* Return the number of free blocks in a block group.  It is used when
245  * the block bitmap is uninitialized, so we can't just count the bits
246  * in the bitmap. */
247 unsigned ext4_free_clusters_after_init(struct super_block *sb,
248                                        ext4_group_t block_group,
249                                        struct ext4_group_desc *gdp)
250 {
251         return num_clusters_in_group(sb, block_group) - 
252                 ext4_num_overhead_clusters(sb, block_group, gdp);
253 }
254
255 /*
256  * The free blocks are managed by bitmaps.  A file system contains several
257  * blocks groups.  Each group contains 1 bitmap block for blocks, 1 bitmap
258  * block for inodes, N blocks for the inode table and data blocks.
259  *
260  * The file system contains group descriptors which are located after the
261  * super block.  Each descriptor contains the number of the bitmap block and
262  * the free blocks count in the block.  The descriptors are loaded in memory
263  * when a file system is mounted (see ext4_fill_super).
264  */
265
266 /**
267  * ext4_get_group_desc() -- load group descriptor from disk
268  * @sb:                 super block
269  * @block_group:        given block group
270  * @bh:                 pointer to the buffer head to store the block
271  *                      group descriptor
272  */
273 struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb,
274                                              ext4_group_t block_group,
275                                              struct buffer_head **bh)
276 {
277         unsigned int group_desc;
278         unsigned int offset;
279         ext4_group_t ngroups = ext4_get_groups_count(sb);
280         struct ext4_group_desc *desc;
281         struct ext4_sb_info *sbi = EXT4_SB(sb);
282         struct buffer_head *bh_p;
283
284         if (block_group >= ngroups) {
285                 ext4_error(sb, "block_group >= groups_count - block_group = %u,"
286                            " groups_count = %u", block_group, ngroups);
287
288                 return NULL;
289         }
290
291         group_desc = block_group >> EXT4_DESC_PER_BLOCK_BITS(sb);
292         offset = block_group & (EXT4_DESC_PER_BLOCK(sb) - 1);
293         bh_p = sbi_array_rcu_deref(sbi, s_group_desc, group_desc);
294         /*
295          * sbi_array_rcu_deref returns with rcu unlocked, this is ok since
296          * the pointer being dereferenced won't be dereferenced again. By
297          * looking at the usage in add_new_gdb() the value isn't modified,
298          * just the pointer, and so it remains valid.
299          */
300         if (!bh_p) {
301                 ext4_error(sb, "Group descriptor not loaded - "
302                            "block_group = %u, group_desc = %u, desc = %u",
303                            block_group, group_desc, offset);
304                 return NULL;
305         }
306
307         desc = (struct ext4_group_desc *)(
308                 (__u8 *)bh_p->b_data +
309                 offset * EXT4_DESC_SIZE(sb));
310         if (bh)
311                 *bh = bh_p;
312         return desc;
313 }
314
315 /*
316  * Return the block number which was discovered to be invalid, or 0 if
317  * the block bitmap is valid.
318  */
319 static ext4_fsblk_t ext4_valid_block_bitmap(struct super_block *sb,
320                                             struct ext4_group_desc *desc,
321                                             ext4_group_t block_group,
322                                             struct buffer_head *bh)
323 {
324         struct ext4_sb_info *sbi = EXT4_SB(sb);
325         ext4_grpblk_t offset;
326         ext4_grpblk_t next_zero_bit;
327         ext4_grpblk_t max_bit = EXT4_CLUSTERS_PER_GROUP(sb);
328         ext4_fsblk_t blk;
329         ext4_fsblk_t group_first_block;
330
331         if (ext4_has_feature_flex_bg(sb)) {
332                 /* with FLEX_BG, the inode/block bitmaps and itable
333                  * blocks may not be in the group at all
334                  * so the bitmap validation will be skipped for those groups
335                  * or it has to also read the block group where the bitmaps
336                  * are located to verify they are set.
337                  */
338                 return 0;
339         }
340         group_first_block = ext4_group_first_block_no(sb, block_group);
341
342         /* check whether block bitmap block number is set */
343         blk = ext4_block_bitmap(sb, desc);
344         offset = blk - group_first_block;
345         if (offset < 0 || EXT4_B2C(sbi, offset) >= max_bit ||
346             !ext4_test_bit(EXT4_B2C(sbi, offset), bh->b_data))
347                 /* bad block bitmap */
348                 return blk;
349
350         /* check whether the inode bitmap block number is set */
351         blk = ext4_inode_bitmap(sb, desc);
352         offset = blk - group_first_block;
353         if (offset < 0 || EXT4_B2C(sbi, offset) >= max_bit ||
354             !ext4_test_bit(EXT4_B2C(sbi, offset), bh->b_data))
355                 /* bad block bitmap */
356                 return blk;
357
358         /* check whether the inode table block number is set */
359         blk = ext4_inode_table(sb, desc);
360         offset = blk - group_first_block;
361         if (offset < 0 || EXT4_B2C(sbi, offset) >= max_bit ||
362             EXT4_B2C(sbi, offset + sbi->s_itb_per_group) >= max_bit)
363                 return blk;
364         next_zero_bit = ext4_find_next_zero_bit(bh->b_data,
365                         EXT4_B2C(sbi, offset + EXT4_SB(sb)->s_itb_per_group),
366                         EXT4_B2C(sbi, offset));
367         if (next_zero_bit <
368             EXT4_B2C(sbi, offset + EXT4_SB(sb)->s_itb_per_group))
369                 /* bad bitmap for inode tables */
370                 return blk;
371         return 0;
372 }
373
374 static int ext4_validate_block_bitmap(struct super_block *sb,
375                                       struct ext4_group_desc *desc,
376                                       ext4_group_t block_group,
377                                       struct buffer_head *bh)
378 {
379         ext4_fsblk_t    blk;
380         struct ext4_group_info *grp = ext4_get_group_info(sb, block_group);
381         struct ext4_sb_info *sbi = EXT4_SB(sb);
382
383         if (buffer_verified(bh))
384                 return 0;
385         if (EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
386                 return -EFSCORRUPTED;
387
388         ext4_lock_group(sb, block_group);
389         if (buffer_verified(bh))
390                 goto verified;
391         if (unlikely(!ext4_block_bitmap_csum_verify(sb, block_group,
392                         desc, bh))) {
393                 ext4_unlock_group(sb, block_group);
394                 ext4_error(sb, "bg %u: bad block bitmap checksum", block_group);
395                 if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
396                         percpu_counter_sub(&sbi->s_freeclusters_counter,
397                                            grp->bb_free);
398                 set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state);
399                 return -EFSBADCRC;
400         }
401         blk = ext4_valid_block_bitmap(sb, desc, block_group, bh);
402         if (unlikely(blk != 0)) {
403                 ext4_unlock_group(sb, block_group);
404                 ext4_error(sb, "bg %u: block %llu: invalid block bitmap",
405                            block_group, blk);
406                 if (!EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
407                         percpu_counter_sub(&sbi->s_freeclusters_counter,
408                                            grp->bb_free);
409                 set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &grp->bb_state);
410                 return -EFSCORRUPTED;
411         }
412         set_buffer_verified(bh);
413 verified:
414         ext4_unlock_group(sb, block_group);
415         return 0;
416 }
417
418 /**
419  * ext4_read_block_bitmap_nowait()
420  * @sb:                 super block
421  * @block_group:        given block group
422  *
423  * Read the bitmap for a given block_group,and validate the
424  * bits for block/inode/inode tables are set in the bitmaps
425  *
426  * Return buffer_head on success or NULL in case of failure.
427  */
428 struct buffer_head *
429 ext4_read_block_bitmap_nowait(struct super_block *sb, ext4_group_t block_group)
430 {
431         struct ext4_group_desc *desc;
432         struct ext4_sb_info *sbi = EXT4_SB(sb);
433         struct buffer_head *bh;
434         ext4_fsblk_t bitmap_blk;
435         int err;
436
437         desc = ext4_get_group_desc(sb, block_group, NULL);
438         if (!desc)
439                 return ERR_PTR(-EFSCORRUPTED);
440         bitmap_blk = ext4_block_bitmap(sb, desc);
441         if ((bitmap_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
442             (bitmap_blk >= ext4_blocks_count(sbi->s_es))) {
443                 ext4_error(sb, "Invalid block bitmap block %llu in "
444                            "block_group %u", bitmap_blk, block_group);
445                 return ERR_PTR(-EFSCORRUPTED);
446         }
447         bh = sb_getblk(sb, bitmap_blk);
448         if (unlikely(!bh)) {
449                 ext4_error(sb, "Cannot get buffer for block bitmap - "
450                            "block_group = %u, block_bitmap = %llu",
451                            block_group, bitmap_blk);
452                 return ERR_PTR(-ENOMEM);
453         }
454
455         if (bitmap_uptodate(bh))
456                 goto verify;
457
458         lock_buffer(bh);
459         if (bitmap_uptodate(bh)) {
460                 unlock_buffer(bh);
461                 goto verify;
462         }
463         ext4_lock_group(sb, block_group);
464         if (ext4_has_group_desc_csum(sb) &&
465             (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))) {
466                 if (block_group == 0) {
467                         ext4_unlock_group(sb, block_group);
468                         unlock_buffer(bh);
469                         ext4_error(sb, "Block bitmap for bg 0 marked "
470                                    "uninitialized");
471                         err = -EFSCORRUPTED;
472                         goto out;
473                 }
474                 err = ext4_init_block_bitmap(sb, bh, block_group, desc);
475                 set_bitmap_uptodate(bh);
476                 set_buffer_uptodate(bh);
477                 set_buffer_verified(bh);
478                 ext4_unlock_group(sb, block_group);
479                 unlock_buffer(bh);
480                 if (err) {
481                         ext4_error(sb, "Failed to init block bitmap for group "
482                                    "%u: %d", block_group, err);
483                         goto out;
484                 }
485                 goto verify;
486         }
487         ext4_unlock_group(sb, block_group);
488         if (buffer_uptodate(bh)) {
489                 /*
490                  * if not uninit if bh is uptodate,
491                  * bitmap is also uptodate
492                  */
493                 set_bitmap_uptodate(bh);
494                 unlock_buffer(bh);
495                 goto verify;
496         }
497         /*
498          * submit the buffer_head for reading
499          */
500         set_buffer_new(bh);
501         trace_ext4_read_block_bitmap_load(sb, block_group);
502         bh->b_end_io = ext4_end_bitmap_read;
503         get_bh(bh);
504         submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
505         return bh;
506 verify:
507         err = ext4_validate_block_bitmap(sb, desc, block_group, bh);
508         if (err)
509                 goto out;
510         return bh;
511 out:
512         put_bh(bh);
513         return ERR_PTR(err);
514 }
515
516 /* Returns 0 on success, 1 on error */
517 int ext4_wait_block_bitmap(struct super_block *sb, ext4_group_t block_group,
518                            struct buffer_head *bh)
519 {
520         struct ext4_group_desc *desc;
521
522         if (!buffer_new(bh))
523                 return 0;
524         desc = ext4_get_group_desc(sb, block_group, NULL);
525         if (!desc)
526                 return -EFSCORRUPTED;
527         wait_on_buffer(bh);
528         if (!buffer_uptodate(bh)) {
529                 ext4_error(sb, "Cannot read block bitmap - "
530                            "block_group = %u, block_bitmap = %llu",
531                            block_group, (unsigned long long) bh->b_blocknr);
532                 return -EIO;
533         }
534         clear_buffer_new(bh);
535         /* Panic or remount fs read-only if block bitmap is invalid */
536         return ext4_validate_block_bitmap(sb, desc, block_group, bh);
537 }
538
539 struct buffer_head *
540 ext4_read_block_bitmap(struct super_block *sb, ext4_group_t block_group)
541 {
542         struct buffer_head *bh;
543         int err;
544
545         bh = ext4_read_block_bitmap_nowait(sb, block_group);
546         if (IS_ERR(bh))
547                 return bh;
548         err = ext4_wait_block_bitmap(sb, block_group, bh);
549         if (err) {
550                 put_bh(bh);
551                 return ERR_PTR(err);
552         }
553         return bh;
554 }
555
556 /**
557  * ext4_has_free_clusters()
558  * @sbi:        in-core super block structure.
559  * @nclusters:  number of needed blocks
560  * @flags:      flags from ext4_mb_new_blocks()
561  *
562  * Check if filesystem has nclusters free & available for allocation.
563  * On success return 1, return 0 on failure.
564  */
565 static int ext4_has_free_clusters(struct ext4_sb_info *sbi,
566                                   s64 nclusters, unsigned int flags)
567 {
568         s64 free_clusters, dirty_clusters, rsv, resv_clusters;
569         struct percpu_counter *fcc = &sbi->s_freeclusters_counter;
570         struct percpu_counter *dcc = &sbi->s_dirtyclusters_counter;
571
572         free_clusters  = percpu_counter_read_positive(fcc);
573         dirty_clusters = percpu_counter_read_positive(dcc);
574         resv_clusters = atomic64_read(&sbi->s_resv_clusters);
575
576         /*
577          * r_blocks_count should always be multiple of the cluster ratio so
578          * we are safe to do a plane bit shift only.
579          */
580         rsv = (ext4_r_blocks_count(sbi->s_es) >> sbi->s_cluster_bits) +
581               resv_clusters;
582
583         if (free_clusters - (nclusters + rsv + dirty_clusters) <
584                                         EXT4_FREECLUSTERS_WATERMARK) {
585                 free_clusters  = percpu_counter_sum_positive(fcc);
586                 dirty_clusters = percpu_counter_sum_positive(dcc);
587         }
588         /* Check whether we have space after accounting for current
589          * dirty clusters & root reserved clusters.
590          */
591         if (free_clusters >= (rsv + nclusters + dirty_clusters))
592                 return 1;
593
594         /* Hm, nope.  Are (enough) root reserved clusters available? */
595         if (uid_eq(sbi->s_resuid, current_fsuid()) ||
596             (!gid_eq(sbi->s_resgid, GLOBAL_ROOT_GID) && in_group_p(sbi->s_resgid)) ||
597             capable(CAP_SYS_RESOURCE) ||
598             (flags & EXT4_MB_USE_ROOT_BLOCKS)) {
599
600                 if (free_clusters >= (nclusters + dirty_clusters +
601                                       resv_clusters))
602                         return 1;
603         }
604         /* No free blocks. Let's see if we can dip into reserved pool */
605         if (flags & EXT4_MB_USE_RESERVED) {
606                 if (free_clusters >= (nclusters + dirty_clusters))
607                         return 1;
608         }
609
610         return 0;
611 }
612
613 int ext4_claim_free_clusters(struct ext4_sb_info *sbi,
614                              s64 nclusters, unsigned int flags)
615 {
616         if (ext4_has_free_clusters(sbi, nclusters, flags)) {
617                 percpu_counter_add(&sbi->s_dirtyclusters_counter, nclusters);
618                 return 0;
619         } else
620                 return -ENOSPC;
621 }
622
623 /**
624  * ext4_should_retry_alloc()
625  * @sb:                 super block
626  * @retries             number of attemps has been made
627  *
628  * ext4_should_retry_alloc() is called when ENOSPC is returned, and if
629  * it is profitable to retry the operation, this function will wait
630  * for the current or committing transaction to complete, and then
631  * return TRUE.
632  *
633  * if the total number of retries exceed three times, return FALSE.
634  */
635 int ext4_should_retry_alloc(struct super_block *sb, int *retries)
636 {
637         if (!ext4_has_free_clusters(EXT4_SB(sb), 1, 0) ||
638             (*retries)++ > 3 ||
639             !EXT4_SB(sb)->s_journal)
640                 return 0;
641
642         jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
643
644         smp_mb();
645         if (EXT4_SB(sb)->s_mb_free_pending)
646                 jbd2_journal_force_commit_nested(EXT4_SB(sb)->s_journal);
647         return 1;
648 }
649
650 /*
651  * ext4_new_meta_blocks() -- allocate block for meta data (indexing) blocks
652  *
653  * @handle:             handle to this transaction
654  * @inode:              file inode
655  * @goal:               given target block(filesystem wide)
656  * @count:              pointer to total number of clusters needed
657  * @errp:               error code
658  *
659  * Return 1st allocated block number on success, *count stores total account
660  * error stores in errp pointer
661  */
662 ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
663                                   ext4_fsblk_t goal, unsigned int flags,
664                                   unsigned long *count, int *errp)
665 {
666         struct ext4_allocation_request ar;
667         ext4_fsblk_t ret;
668
669         memset(&ar, 0, sizeof(ar));
670         /* Fill with neighbour allocated blocks */
671         ar.inode = inode;
672         ar.goal = goal;
673         ar.len = count ? *count : 1;
674         ar.flags = flags;
675
676         ret = ext4_mb_new_blocks(handle, &ar, errp);
677         if (count)
678                 *count = ar.len;
679         /*
680          * Account for the allocated meta blocks.  We will never
681          * fail EDQUOT for metdata, but we do account for it.
682          */
683         if (!(*errp) && (flags & EXT4_MB_DELALLOC_RESERVED)) {
684                 dquot_alloc_block_nofail(inode,
685                                 EXT4_C2B(EXT4_SB(inode->i_sb), ar.len));
686         }
687         return ret;
688 }
689
690 /**
691  * ext4_count_free_clusters() -- count filesystem free clusters
692  * @sb:         superblock
693  *
694  * Adds up the number of free clusters from each block group.
695  */
696 ext4_fsblk_t ext4_count_free_clusters(struct super_block *sb)
697 {
698         ext4_fsblk_t desc_count;
699         struct ext4_group_desc *gdp;
700         ext4_group_t i;
701         ext4_group_t ngroups = ext4_get_groups_count(sb);
702         struct ext4_group_info *grp;
703 #ifdef EXT4FS_DEBUG
704         struct ext4_super_block *es;
705         ext4_fsblk_t bitmap_count;
706         unsigned int x;
707         struct buffer_head *bitmap_bh = NULL;
708
709         es = EXT4_SB(sb)->s_es;
710         desc_count = 0;
711         bitmap_count = 0;
712         gdp = NULL;
713
714         for (i = 0; i < ngroups; i++) {
715                 gdp = ext4_get_group_desc(sb, i, NULL);
716                 if (!gdp)
717                         continue;
718                 grp = NULL;
719                 if (EXT4_SB(sb)->s_group_info)
720                         grp = ext4_get_group_info(sb, i);
721                 if (!grp || !EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
722                         desc_count += ext4_free_group_clusters(sb, gdp);
723                 brelse(bitmap_bh);
724                 bitmap_bh = ext4_read_block_bitmap(sb, i);
725                 if (IS_ERR(bitmap_bh)) {
726                         bitmap_bh = NULL;
727                         continue;
728                 }
729
730                 x = ext4_count_free(bitmap_bh->b_data,
731                                     EXT4_CLUSTERS_PER_GROUP(sb) / 8);
732                 printk(KERN_DEBUG "group %u: stored = %d, counted = %u\n",
733                         i, ext4_free_group_clusters(sb, gdp), x);
734                 bitmap_count += x;
735         }
736         brelse(bitmap_bh);
737         printk(KERN_DEBUG "ext4_count_free_clusters: stored = %llu"
738                ", computed = %llu, %llu\n",
739                EXT4_NUM_B2C(EXT4_SB(sb), ext4_free_blocks_count(es)),
740                desc_count, bitmap_count);
741         return bitmap_count;
742 #else
743         desc_count = 0;
744         for (i = 0; i < ngroups; i++) {
745                 gdp = ext4_get_group_desc(sb, i, NULL);
746                 if (!gdp)
747                         continue;
748                 grp = NULL;
749                 if (EXT4_SB(sb)->s_group_info)
750                         grp = ext4_get_group_info(sb, i);
751                 if (!grp || !EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
752                         desc_count += ext4_free_group_clusters(sb, gdp);
753         }
754
755         return desc_count;
756 #endif
757 }
758
759 static inline int test_root(ext4_group_t a, int b)
760 {
761         while (1) {
762                 if (a < b)
763                         return 0;
764                 if (a == b)
765                         return 1;
766                 if ((a % b) != 0)
767                         return 0;
768                 a = a / b;
769         }
770 }
771
772 /**
773  *      ext4_bg_has_super - number of blocks used by the superblock in group
774  *      @sb: superblock for filesystem
775  *      @group: group number to check
776  *
777  *      Return the number of blocks used by the superblock (primary or backup)
778  *      in this group.  Currently this will be only 0 or 1.
779  */
780 int ext4_bg_has_super(struct super_block *sb, ext4_group_t group)
781 {
782         struct ext4_super_block *es = EXT4_SB(sb)->s_es;
783
784         if (group == 0)
785                 return 1;
786         if (ext4_has_feature_sparse_super2(sb)) {
787                 if (group == le32_to_cpu(es->s_backup_bgs[0]) ||
788                     group == le32_to_cpu(es->s_backup_bgs[1]))
789                         return 1;
790                 return 0;
791         }
792         if ((group <= 1) || !ext4_has_feature_sparse_super(sb))
793                 return 1;
794         if (!(group & 1))
795                 return 0;
796         if (test_root(group, 3) || (test_root(group, 5)) ||
797             test_root(group, 7))
798                 return 1;
799
800         return 0;
801 }
802
803 static unsigned long ext4_bg_num_gdb_meta(struct super_block *sb,
804                                         ext4_group_t group)
805 {
806         unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
807         ext4_group_t first = metagroup * EXT4_DESC_PER_BLOCK(sb);
808         ext4_group_t last = first + EXT4_DESC_PER_BLOCK(sb) - 1;
809
810         if (group == first || group == first + 1 || group == last)
811                 return 1;
812         return 0;
813 }
814
815 static unsigned long ext4_bg_num_gdb_nometa(struct super_block *sb,
816                                         ext4_group_t group)
817 {
818         if (!ext4_bg_has_super(sb, group))
819                 return 0;
820
821         if (ext4_has_feature_meta_bg(sb))
822                 return le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
823         else
824                 return EXT4_SB(sb)->s_gdb_count;
825 }
826
827 /**
828  *      ext4_bg_num_gdb - number of blocks used by the group table in group
829  *      @sb: superblock for filesystem
830  *      @group: group number to check
831  *
832  *      Return the number of blocks used by the group descriptor table
833  *      (primary or backup) in this group.  In the future there may be a
834  *      different number of descriptor blocks in each group.
835  */
836 unsigned long ext4_bg_num_gdb(struct super_block *sb, ext4_group_t group)
837 {
838         unsigned long first_meta_bg =
839                         le32_to_cpu(EXT4_SB(sb)->s_es->s_first_meta_bg);
840         unsigned long metagroup = group / EXT4_DESC_PER_BLOCK(sb);
841
842         if (!ext4_has_feature_meta_bg(sb) || metagroup < first_meta_bg)
843                 return ext4_bg_num_gdb_nometa(sb, group);
844
845         return ext4_bg_num_gdb_meta(sb,group);
846
847 }
848
849 /*
850  * This function returns the number of file system metadata clusters at
851  * the beginning of a block group, including the reserved gdt blocks.
852  */
853 static unsigned ext4_num_base_meta_clusters(struct super_block *sb,
854                                      ext4_group_t block_group)
855 {
856         struct ext4_sb_info *sbi = EXT4_SB(sb);
857         unsigned num;
858
859         /* Check for superblock and gdt backups in this group */
860         num = ext4_bg_has_super(sb, block_group);
861
862         if (!ext4_has_feature_meta_bg(sb) ||
863             block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) *
864                           sbi->s_desc_per_block) {
865                 if (num) {
866                         num += ext4_bg_num_gdb(sb, block_group);
867                         num += le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
868                 }
869         } else { /* For META_BG_BLOCK_GROUPS */
870                 num += ext4_bg_num_gdb(sb, block_group);
871         }
872         return EXT4_NUM_B2C(sbi, num);
873 }
874 /**
875  *      ext4_inode_to_goal_block - return a hint for block allocation
876  *      @inode: inode for block allocation
877  *
878  *      Return the ideal location to start allocating blocks for a
879  *      newly created inode.
880  */
881 ext4_fsblk_t ext4_inode_to_goal_block(struct inode *inode)
882 {
883         struct ext4_inode_info *ei = EXT4_I(inode);
884         ext4_group_t block_group;
885         ext4_grpblk_t colour;
886         int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
887         ext4_fsblk_t bg_start;
888         ext4_fsblk_t last_block;
889
890         block_group = ei->i_block_group;
891         if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
892                 /*
893                  * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
894                  * block groups per flexgroup, reserve the first block
895                  * group for directories and special files.  Regular
896                  * files will start at the second block group.  This
897                  * tends to speed up directory access and improves
898                  * fsck times.
899                  */
900                 block_group &= ~(flex_size-1);
901                 if (S_ISREG(inode->i_mode))
902                         block_group++;
903         }
904         bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
905         last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
906
907         /*
908          * If we are doing delayed allocation, we don't need take
909          * colour into account.
910          */
911         if (test_opt(inode->i_sb, DELALLOC))
912                 return bg_start;
913
914         if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
915                 colour = (current->pid % 16) *
916                         (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
917         else
918                 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
919         return bg_start + colour;
920 }
921