~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/exfat/balloc.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/exfat/balloc.c (Version linux-6.12-rc7) and /fs/exfat/balloc.c (Version linux-6.2.16)


  1 // SPDX-License-Identifier: GPL-2.0-or-later        1 // SPDX-License-Identifier: GPL-2.0-or-later
  2 /*                                                  2 /*
  3  *  Copyright (C) 2012-2013 Samsung Electronic      3  *  Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  4  */                                                 4  */
  5                                                     5 
  6 #include <linux/blkdev.h>                           6 #include <linux/blkdev.h>
  7 #include <linux/slab.h>                             7 #include <linux/slab.h>
  8 #include <linux/bitmap.h>                      << 
  9 #include <linux/buffer_head.h>                      8 #include <linux/buffer_head.h>
 10                                                     9 
 11 #include "exfat_raw.h"                             10 #include "exfat_raw.h"
 12 #include "exfat_fs.h"                              11 #include "exfat_fs.h"
 13                                                    12 
 14 #if BITS_PER_LONG == 32                        !!  13 static const unsigned char free_bit[] = {
 15 #define __le_long __le32                       !!  14         0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/*  0 ~  19*/
 16 #define lel_to_cpu(A) le32_to_cpu(A)           !!  15         0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3,/* 20 ~  39*/
 17 #define cpu_to_lel(A) cpu_to_le32(A)           !!  16         0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/* 40 ~  59*/
 18 #elif BITS_PER_LONG == 64                      !!  17         0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/* 60 ~  79*/
 19 #define __le_long __le64                       !!  18         0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2,/* 80 ~  99*/
 20 #define lel_to_cpu(A) le64_to_cpu(A)           !!  19         0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3,/*100 ~ 119*/
 21 #define cpu_to_lel(A) cpu_to_le64(A)           !!  20         0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*120 ~ 139*/
 22 #else                                          !!  21         0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,/*140 ~ 159*/
 23 #error "BITS_PER_LONG not 32 or 64"            !!  22         0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/*160 ~ 179*/
 24 #endif                                         !!  23         0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3,/*180 ~ 199*/
                                                   >>  24         0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*200 ~ 219*/
                                                   >>  25         0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/*220 ~ 239*/
                                                   >>  26         0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0                /*240 ~ 254*/
                                                   >>  27 };
                                                   >>  28 
                                                   >>  29 static const unsigned char used_bit[] = {
                                                   >>  30         0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3,/*  0 ~  19*/
                                                   >>  31         2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4,/* 20 ~  39*/
                                                   >>  32         2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5,/* 40 ~  59*/
                                                   >>  33         4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,/* 60 ~  79*/
                                                   >>  34         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4,/* 80 ~  99*/
                                                   >>  35         3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,/*100 ~ 119*/
                                                   >>  36         4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4,/*120 ~ 139*/
                                                   >>  37         3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,/*140 ~ 159*/
                                                   >>  38         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5,/*160 ~ 179*/
                                                   >>  39         4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5,/*180 ~ 199*/
                                                   >>  40         3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6,/*200 ~ 219*/
                                                   >>  41         5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,/*220 ~ 239*/
                                                   >>  42         4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8             /*240 ~ 255*/
                                                   >>  43 };
 25                                                    44 
 26 /*                                                 45 /*
 27  *  Allocation Bitmap Management Functions         46  *  Allocation Bitmap Management Functions
 28  */                                                47  */
 29 static int exfat_allocate_bitmap(struct super_     48 static int exfat_allocate_bitmap(struct super_block *sb,
 30                 struct exfat_dentry *ep)           49                 struct exfat_dentry *ep)
 31 {                                                  50 {
 32         struct exfat_sb_info *sbi = EXFAT_SB(s     51         struct exfat_sb_info *sbi = EXFAT_SB(sb);
 33         long long map_size;                        52         long long map_size;
 34         unsigned int i, need_map_size;             53         unsigned int i, need_map_size;
 35         sector_t sector;                           54         sector_t sector;
 36                                                    55 
 37         sbi->map_clu = le32_to_cpu(ep->dentry.     56         sbi->map_clu = le32_to_cpu(ep->dentry.bitmap.start_clu);
 38         map_size = le64_to_cpu(ep->dentry.bitm     57         map_size = le64_to_cpu(ep->dentry.bitmap.size);
 39         need_map_size = ((EXFAT_DATA_CLUSTER_C     58         need_map_size = ((EXFAT_DATA_CLUSTER_COUNT(sbi) - 1) / BITS_PER_BYTE)
 40                 + 1;                               59                 + 1;
 41         if (need_map_size != map_size) {           60         if (need_map_size != map_size) {
 42                 exfat_err(sb, "bogus allocatio     61                 exfat_err(sb, "bogus allocation bitmap size(need : %u, cur : %lld)",
 43                           need_map_size, map_s     62                           need_map_size, map_size);
 44                 /*                                 63                 /*
 45                  * Only allowed when bogus all     64                  * Only allowed when bogus allocation
 46                  * bitmap size is large            65                  * bitmap size is large
 47                  */                                66                  */
 48                 if (need_map_size > map_size)      67                 if (need_map_size > map_size)
 49                         return -EIO;               68                         return -EIO;
 50         }                                          69         }
 51         sbi->map_sectors = ((need_map_size - 1     70         sbi->map_sectors = ((need_map_size - 1) >>
 52                         (sb->s_blocksize_bits)     71                         (sb->s_blocksize_bits)) + 1;
 53         sbi->vol_amap = kvmalloc_array(sbi->ma !!  72         sbi->vol_amap = kmalloc_array(sbi->map_sectors,
 54                                 sizeof(struct      73                                 sizeof(struct buffer_head *), GFP_KERNEL);
 55         if (!sbi->vol_amap)                        74         if (!sbi->vol_amap)
 56                 return -ENOMEM;                    75                 return -ENOMEM;
 57                                                    76 
 58         sector = exfat_cluster_to_sector(sbi,      77         sector = exfat_cluster_to_sector(sbi, sbi->map_clu);
 59         for (i = 0; i < sbi->map_sectors; i++)     78         for (i = 0; i < sbi->map_sectors; i++) {
 60                 sbi->vol_amap[i] = sb_bread(sb     79                 sbi->vol_amap[i] = sb_bread(sb, sector + i);
 61                 if (!sbi->vol_amap[i]) {           80                 if (!sbi->vol_amap[i]) {
 62                         /* release all buffers     81                         /* release all buffers and free vol_amap */
 63                         int j = 0;                 82                         int j = 0;
 64                                                    83 
 65                         while (j < i)              84                         while (j < i)
 66                                 brelse(sbi->vo     85                                 brelse(sbi->vol_amap[j++]);
 67                                                    86 
 68                         kvfree(sbi->vol_amap); !!  87                         kfree(sbi->vol_amap);
 69                         sbi->vol_amap = NULL;      88                         sbi->vol_amap = NULL;
 70                         return -EIO;               89                         return -EIO;
 71                 }                                  90                 }
 72         }                                          91         }
 73                                                    92 
 74         return 0;                                  93         return 0;
 75 }                                                  94 }
 76                                                    95 
 77 int exfat_load_bitmap(struct super_block *sb)      96 int exfat_load_bitmap(struct super_block *sb)
 78 {                                                  97 {
 79         unsigned int i, type;                      98         unsigned int i, type;
 80         struct exfat_chain clu;                    99         struct exfat_chain clu;
 81         struct exfat_sb_info *sbi = EXFAT_SB(s    100         struct exfat_sb_info *sbi = EXFAT_SB(sb);
 82                                                   101 
 83         exfat_chain_set(&clu, sbi->root_dir, 0    102         exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
 84         while (clu.dir != EXFAT_EOF_CLUSTER) {    103         while (clu.dir != EXFAT_EOF_CLUSTER) {
 85                 for (i = 0; i < sbi->dentries_    104                 for (i = 0; i < sbi->dentries_per_clu; i++) {
 86                         struct exfat_dentry *e    105                         struct exfat_dentry *ep;
 87                         struct buffer_head *bh    106                         struct buffer_head *bh;
 88                                                   107 
 89                         ep = exfat_get_dentry(    108                         ep = exfat_get_dentry(sb, &clu, i, &bh);
 90                         if (!ep)                  109                         if (!ep)
 91                                 return -EIO;      110                                 return -EIO;
 92                                                   111 
 93                         type = exfat_get_entry    112                         type = exfat_get_entry_type(ep);
 94                         if (type == TYPE_BITMA !! 113                         if (type == TYPE_UNUSED)
 95                             ep->dentry.bitmap. !! 114                                 break;
                                                   >> 115                         if (type != TYPE_BITMAP)
                                                   >> 116                                 continue;
                                                   >> 117                         if (ep->dentry.bitmap.flags == 0x0) {
 96                                 int err;          118                                 int err;
 97                                                   119 
 98                                 err = exfat_al    120                                 err = exfat_allocate_bitmap(sb, ep);
 99                                 brelse(bh);       121                                 brelse(bh);
100                                 return err;       122                                 return err;
101                         }                         123                         }
102                         brelse(bh);               124                         brelse(bh);
103                                                << 
104                         if (type == TYPE_UNUSE << 
105                                 return -EINVAL << 
106                 }                                 125                 }
107                                                   126 
108                 if (exfat_get_next_cluster(sb,    127                 if (exfat_get_next_cluster(sb, &clu.dir))
109                         return -EIO;              128                         return -EIO;
110         }                                         129         }
111                                                   130 
112         return -EINVAL;                           131         return -EINVAL;
113 }                                                 132 }
114                                                   133 
115 void exfat_free_bitmap(struct exfat_sb_info *s    134 void exfat_free_bitmap(struct exfat_sb_info *sbi)
116 {                                                 135 {
117         int i;                                    136         int i;
118                                                   137 
119         for (i = 0; i < sbi->map_sectors; i++)    138         for (i = 0; i < sbi->map_sectors; i++)
120                 __brelse(sbi->vol_amap[i]);       139                 __brelse(sbi->vol_amap[i]);
121                                                   140 
122         kvfree(sbi->vol_amap);                 !! 141         kfree(sbi->vol_amap);
123 }                                                 142 }
124                                                   143 
125 int exfat_set_bitmap(struct inode *inode, unsi    144 int exfat_set_bitmap(struct inode *inode, unsigned int clu, bool sync)
126 {                                                 145 {
127         int i, b;                                 146         int i, b;
128         unsigned int ent_idx;                     147         unsigned int ent_idx;
129         struct super_block *sb = inode->i_sb;     148         struct super_block *sb = inode->i_sb;
130         struct exfat_sb_info *sbi = EXFAT_SB(s    149         struct exfat_sb_info *sbi = EXFAT_SB(sb);
131                                                   150 
132         if (!is_valid_cluster(sbi, clu))          151         if (!is_valid_cluster(sbi, clu))
133                 return -EINVAL;                   152                 return -EINVAL;
134                                                   153 
135         ent_idx = CLUSTER_TO_BITMAP_ENT(clu);     154         ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
136         i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent    155         i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
137         b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, en    156         b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
138                                                   157 
139         set_bit_le(b, sbi->vol_amap[i]->b_data    158         set_bit_le(b, sbi->vol_amap[i]->b_data);
140         exfat_update_bh(sbi->vol_amap[i], sync    159         exfat_update_bh(sbi->vol_amap[i], sync);
141         return 0;                                 160         return 0;
142 }                                                 161 }
143                                                   162 
144 void exfat_clear_bitmap(struct inode *inode, u    163 void exfat_clear_bitmap(struct inode *inode, unsigned int clu, bool sync)
145 {                                                 164 {
146         int i, b;                                 165         int i, b;
147         unsigned int ent_idx;                     166         unsigned int ent_idx;
148         struct super_block *sb = inode->i_sb;     167         struct super_block *sb = inode->i_sb;
149         struct exfat_sb_info *sbi = EXFAT_SB(s    168         struct exfat_sb_info *sbi = EXFAT_SB(sb);
150         struct exfat_mount_options *opts = &sb    169         struct exfat_mount_options *opts = &sbi->options;
151                                                   170 
152         if (!is_valid_cluster(sbi, clu))          171         if (!is_valid_cluster(sbi, clu))
153                 return;                           172                 return;
154                                                   173 
155         ent_idx = CLUSTER_TO_BITMAP_ENT(clu);     174         ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
156         i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent    175         i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
157         b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, en    176         b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
158                                                   177 
159         clear_bit_le(b, sbi->vol_amap[i]->b_da    178         clear_bit_le(b, sbi->vol_amap[i]->b_data);
160         exfat_update_bh(sbi->vol_amap[i], sync    179         exfat_update_bh(sbi->vol_amap[i], sync);
161                                                   180 
162         if (opts->discard) {                      181         if (opts->discard) {
163                 int ret_discard;                  182                 int ret_discard;
164                                                   183 
165                 ret_discard = sb_issue_discard    184                 ret_discard = sb_issue_discard(sb,
166                         exfat_cluster_to_secto    185                         exfat_cluster_to_sector(sbi, clu),
167                         (1 << sbi->sect_per_cl    186                         (1 << sbi->sect_per_clus_bits), GFP_NOFS, 0);
168                                                   187 
169                 if (ret_discard == -EOPNOTSUPP    188                 if (ret_discard == -EOPNOTSUPP) {
170                         exfat_err(sb, "discard    189                         exfat_err(sb, "discard not supported by device, disabling");
171                         opts->discard = 0;        190                         opts->discard = 0;
172                 }                                 191                 }
173         }                                         192         }
174 }                                                 193 }
175                                                   194 
176 /*                                                195 /*
177  * If the value of "clu" is 0, it means cluste    196  * If the value of "clu" is 0, it means cluster 2 which is the first cluster of
178  * the cluster heap.                              197  * the cluster heap.
179  */                                               198  */
180 unsigned int exfat_find_free_bitmap(struct sup    199 unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu)
181 {                                                 200 {
182         unsigned int i, map_i, map_b, ent_idx;    201         unsigned int i, map_i, map_b, ent_idx;
183         unsigned int clu_base, clu_free;          202         unsigned int clu_base, clu_free;
184         unsigned long clu_bits, clu_mask;      !! 203         unsigned char k, clu_mask;
185         struct exfat_sb_info *sbi = EXFAT_SB(s    204         struct exfat_sb_info *sbi = EXFAT_SB(sb);
186         __le_long bitval;                      << 
187                                                   205 
188         WARN_ON(clu < EXFAT_FIRST_CLUSTER);       206         WARN_ON(clu < EXFAT_FIRST_CLUSTER);
189         ent_idx = ALIGN_DOWN(CLUSTER_TO_BITMAP !! 207         ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
190         clu_base = BITMAP_ENT_TO_CLUSTER(ent_i !! 208         clu_base = BITMAP_ENT_TO_CLUSTER(ent_idx & ~(BITS_PER_BYTE_MASK));
191         clu_mask = IGNORED_BITS_REMAINED(clu,     209         clu_mask = IGNORED_BITS_REMAINED(clu, clu_base);
192                                                   210 
193         map_i = BITMAP_OFFSET_SECTOR_INDEX(sb,    211         map_i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
194         map_b = BITMAP_OFFSET_BYTE_IN_SECTOR(s    212         map_b = BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent_idx);
195                                                   213 
196         for (i = EXFAT_FIRST_CLUSTER; i < sbi-    214         for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters;
197              i += BITS_PER_LONG) {             !! 215              i += BITS_PER_BYTE) {
198                 bitval = *(__le_long *)(sbi->v !! 216                 k = *(sbi->vol_amap[map_i]->b_data + map_b);
199                 if (clu_mask > 0) {               217                 if (clu_mask > 0) {
200                         bitval |= cpu_to_lel(c !! 218                         k |= clu_mask;
201                         clu_mask = 0;             219                         clu_mask = 0;
202                 }                                 220                 }
203                 if (lel_to_cpu(bitval) != ULON !! 221                 if (k < 0xFF) {
204                         clu_bits = lel_to_cpu( !! 222                         clu_free = clu_base + free_bit[k];
205                         clu_free = clu_base +  << 
206                         if (clu_free < sbi->nu    223                         if (clu_free < sbi->num_clusters)
207                                 return clu_fre    224                                 return clu_free;
208                 }                                 225                 }
209                 clu_base += BITS_PER_LONG;     !! 226                 clu_base += BITS_PER_BYTE;
210                 map_b += sizeof(long);         << 
211                                                   227 
212                 if (map_b >= sb->s_blocksize | !! 228                 if (++map_b >= sb->s_blocksize ||
213                     clu_base >= sbi->num_clust    229                     clu_base >= sbi->num_clusters) {
214                         if (++map_i >= sbi->ma    230                         if (++map_i >= sbi->map_sectors) {
215                                 clu_base = EXF    231                                 clu_base = EXFAT_FIRST_CLUSTER;
216                                 map_i = 0;        232                                 map_i = 0;
217                         }                         233                         }
218                         map_b = 0;                234                         map_b = 0;
219                 }                                 235                 }
220         }                                         236         }
221                                                   237 
222         return EXFAT_EOF_CLUSTER;                 238         return EXFAT_EOF_CLUSTER;
223 }                                                 239 }
224                                                   240 
225 int exfat_count_used_clusters(struct super_blo    241 int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count)
226 {                                                 242 {
227         struct exfat_sb_info *sbi = EXFAT_SB(s    243         struct exfat_sb_info *sbi = EXFAT_SB(sb);
228         unsigned int count = 0;                   244         unsigned int count = 0;
229         unsigned int i, map_i = 0, map_b = 0;     245         unsigned int i, map_i = 0, map_b = 0;
230         unsigned int total_clus = EXFAT_DATA_C    246         unsigned int total_clus = EXFAT_DATA_CLUSTER_COUNT(sbi);
231         unsigned int last_mask = total_clus &  !! 247         unsigned int last_mask = total_clus & BITS_PER_BYTE_MASK;
232         unsigned long *bitmap, clu_bits;       !! 248         unsigned char clu_bits;
                                                   >> 249         const unsigned char last_bit_mask[] = {0, 0b00000001, 0b00000011,
                                                   >> 250                 0b00000111, 0b00001111, 0b00011111, 0b00111111, 0b01111111};
233                                                   251 
234         total_clus &= ~last_mask;                 252         total_clus &= ~last_mask;
235         for (i = 0; i < total_clus; i += BITS_ !! 253         for (i = 0; i < total_clus; i += BITS_PER_BYTE) {
236                 bitmap = (void *)(sbi->vol_ama !! 254                 clu_bits = *(sbi->vol_amap[map_i]->b_data + map_b);
237                 count += hweight_long(*bitmap) !! 255                 count += used_bit[clu_bits];
238                 map_b += sizeof(long);         !! 256                 if (++map_b >= (unsigned int)sb->s_blocksize) {
239                 if (map_b >= (unsigned int)sb- << 
240                         map_i++;                  257                         map_i++;
241                         map_b = 0;                258                         map_b = 0;
242                 }                                 259                 }
243         }                                         260         }
244                                                   261 
245         if (last_mask) {                          262         if (last_mask) {
246                 bitmap = (void *)(sbi->vol_ama !! 263                 clu_bits = *(sbi->vol_amap[map_i]->b_data + map_b);
247                 clu_bits = lel_to_cpu(*(__le_l !! 264                 clu_bits &= last_bit_mask[last_mask];
248                 count += hweight_long(clu_bits !! 265                 count += used_bit[clu_bits];
249         }                                         266         }
250                                                   267 
251         *ret_count = count;                       268         *ret_count = count;
252         return 0;                                 269         return 0;
253 }                                                 270 }
254                                                   271 
255 int exfat_trim_fs(struct inode *inode, struct     272 int exfat_trim_fs(struct inode *inode, struct fstrim_range *range)
256 {                                                 273 {
257         unsigned int trim_begin, trim_end, cou    274         unsigned int trim_begin, trim_end, count, next_free_clu;
258         u64 clu_start, clu_end, trim_minlen, t    275         u64 clu_start, clu_end, trim_minlen, trimmed_total = 0;
259         struct super_block *sb = inode->i_sb;     276         struct super_block *sb = inode->i_sb;
260         struct exfat_sb_info *sbi = EXFAT_SB(s    277         struct exfat_sb_info *sbi = EXFAT_SB(sb);
261         int err = 0;                              278         int err = 0;
262                                                   279 
263         clu_start = max_t(u64, range->start >>    280         clu_start = max_t(u64, range->start >> sbi->cluster_size_bits,
264                                 EXFAT_FIRST_CL    281                                 EXFAT_FIRST_CLUSTER);
265         clu_end = clu_start + (range->len >> s    282         clu_end = clu_start + (range->len >> sbi->cluster_size_bits) - 1;
266         trim_minlen = range->minlen >> sbi->cl    283         trim_minlen = range->minlen >> sbi->cluster_size_bits;
267                                                   284 
268         if (clu_start >= sbi->num_clusters ||     285         if (clu_start >= sbi->num_clusters || range->len < sbi->cluster_size)
269                 return -EINVAL;                   286                 return -EINVAL;
270                                                   287 
271         if (clu_end >= sbi->num_clusters)         288         if (clu_end >= sbi->num_clusters)
272                 clu_end = sbi->num_clusters -     289                 clu_end = sbi->num_clusters - 1;
273                                                   290 
274         mutex_lock(&sbi->bitmap_lock);            291         mutex_lock(&sbi->bitmap_lock);
275                                                   292 
276         trim_begin = trim_end = exfat_find_fre    293         trim_begin = trim_end = exfat_find_free_bitmap(sb, clu_start);
277         if (trim_begin == EXFAT_EOF_CLUSTER)      294         if (trim_begin == EXFAT_EOF_CLUSTER)
278                 goto unlock;                      295                 goto unlock;
279                                                   296 
280         next_free_clu = exfat_find_free_bitmap    297         next_free_clu = exfat_find_free_bitmap(sb, trim_end + 1);
281         if (next_free_clu == EXFAT_EOF_CLUSTER    298         if (next_free_clu == EXFAT_EOF_CLUSTER)
282                 goto unlock;                      299                 goto unlock;
283                                                   300 
284         do {                                      301         do {
285                 if (next_free_clu == trim_end     302                 if (next_free_clu == trim_end + 1) {
286                         /* extend trim range f    303                         /* extend trim range for continuous free cluster */
287                         trim_end++;               304                         trim_end++;
288                 } else {                          305                 } else {
289                         /* trim current range     306                         /* trim current range if it's larger than trim_minlen */
290                         count = trim_end - tri    307                         count = trim_end - trim_begin + 1;
291                         if (count >= trim_minl    308                         if (count >= trim_minlen) {
292                                 err = sb_issue    309                                 err = sb_issue_discard(sb,
293                                         exfat_    310                                         exfat_cluster_to_sector(sbi, trim_begin),
294                                         count     311                                         count * sbi->sect_per_clus, GFP_NOFS, 0);
295                                 if (err)          312                                 if (err)
296                                         goto u    313                                         goto unlock;
297                                                   314 
298                                 trimmed_total     315                                 trimmed_total += count;
299                         }                         316                         }
300                                                   317 
301                         /* set next start poin    318                         /* set next start point of the free hole */
302                         trim_begin = trim_end     319                         trim_begin = trim_end = next_free_clu;
303                 }                                 320                 }
304                                                   321 
305                 if (next_free_clu >= clu_end)     322                 if (next_free_clu >= clu_end)
306                         break;                    323                         break;
307                                                   324 
308                 if (fatal_signal_pending(curre    325                 if (fatal_signal_pending(current)) {
309                         err = -ERESTARTSYS;       326                         err = -ERESTARTSYS;
310                         goto unlock;              327                         goto unlock;
311                 }                                 328                 }
312                                                   329 
313                 next_free_clu = exfat_find_fre    330                 next_free_clu = exfat_find_free_bitmap(sb, next_free_clu + 1);
314         } while (next_free_clu != EXFAT_EOF_CL    331         } while (next_free_clu != EXFAT_EOF_CLUSTER &&
315                         next_free_clu > trim_e    332                         next_free_clu > trim_end);
316                                                   333 
317         /* try to trim remainder */               334         /* try to trim remainder */
318         count = trim_end - trim_begin + 1;        335         count = trim_end - trim_begin + 1;
319         if (count >= trim_minlen) {               336         if (count >= trim_minlen) {
320                 err = sb_issue_discard(sb, exf    337                 err = sb_issue_discard(sb, exfat_cluster_to_sector(sbi, trim_begin),
321                         count * sbi->sect_per_    338                         count * sbi->sect_per_clus, GFP_NOFS, 0);
322                 if (err)                          339                 if (err)
323                         goto unlock;              340                         goto unlock;
324                                                   341 
325                 trimmed_total += count;           342                 trimmed_total += count;
326         }                                         343         }
327                                                   344 
328 unlock:                                           345 unlock:
329         mutex_unlock(&sbi->bitmap_lock);          346         mutex_unlock(&sbi->bitmap_lock);
330         range->len = trimmed_total << sbi->clu    347         range->len = trimmed_total << sbi->cluster_size_bits;
331                                                   348 
332         return err;                               349         return err;
333 }                                                 350 }
334                                                   351 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php