Blame view

fs/hfsplus/bitmap.c 5.02 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  /*
   *  linux/fs/hfsplus/bitmap.c
   *
   * Copyright (C) 2001
   * Brad Boyer (flar@allandria.com)
   * (C) 2003 Ardis Technologies <roman@ardistech.com>
   *
   * Handling of allocation file
   */
  
  #include <linux/pagemap.h>
  
  #include "hfsplus_fs.h"
  #include "hfsplus_raw.h"
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
16
  #define PAGE_CACHE_BITS	(PAGE_SIZE * 8)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17

2753cc281   Anton Salikhmetov   hfsplus: over 80 ...
18
19
  int hfsplus_block_allocate(struct super_block *sb, u32 size,
  		u32 offset, u32 *max)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
  {
dd73a01a3   Christoph Hellwig   hfsplus: fix HFSP...
21
  	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
23
24
25
26
27
28
29
30
31
  	struct page *page;
  	struct address_space *mapping;
  	__be32 *pptr, *curr, *end;
  	u32 mask, start, len, n;
  	__be32 val;
  	int i;
  
  	len = *max;
  	if (!len)
  		return size;
c2b3e1f76   Joe Perches   hfs/hfsplus: conv...
32
33
  	hfs_dbg(BITMAP, "block_allocate: %u,%u,%u
  ", size, offset, len);
dd73a01a3   Christoph Hellwig   hfsplus: fix HFSP...
34
35
  	mutex_lock(&sbi->alloc_mutex);
  	mapping = sbi->alloc_file->i_mapping;
090d2b185   Pekka Enberg   [PATCH] read_mapp...
36
  	page = read_mapping_page(mapping, offset / PAGE_CACHE_BITS, NULL);
649f1ee6c   Eric Sesterhenn   hfsplus: check re...
37
38
39
40
  	if (IS_ERR(page)) {
  		start = size;
  		goto out;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  	pptr = kmap(page);
  	curr = pptr + (offset & (PAGE_CACHE_BITS - 1)) / 32;
  	i = offset % 32;
  	offset &= ~(PAGE_CACHE_BITS - 1);
  	if ((size ^ offset) / PAGE_CACHE_BITS)
  		end = pptr + PAGE_CACHE_BITS / 32;
  	else
  		end = pptr + ((size + 31) & (PAGE_CACHE_BITS - 1)) / 32;
  
  	/* scan the first partial u32 for zero bits */
  	val = *curr;
  	if (~val) {
  		n = be32_to_cpu(val);
  		mask = (1U << 31) >> i;
  		for (; i < 32; mask >>= 1, i++) {
  			if (!(n & mask))
  				goto found;
  		}
  	}
  	curr++;
  
  	/* scan complete u32s for the first zero bit */
  	while (1) {
  		while (curr < end) {
  			val = *curr;
  			if (~val) {
  				n = be32_to_cpu(val);
  				mask = 1 << 31;
  				for (i = 0; i < 32; mask >>= 1, i++) {
  					if (!(n & mask))
  						goto found;
  				}
  			}
  			curr++;
  		}
  		kunmap(page);
  		offset += PAGE_CACHE_BITS;
  		if (offset >= size)
  			break;
090d2b185   Pekka Enberg   [PATCH] read_mapp...
80
81
  		page = read_mapping_page(mapping, offset / PAGE_CACHE_BITS,
  					 NULL);
649f1ee6c   Eric Sesterhenn   hfsplus: check re...
82
83
84
85
  		if (IS_ERR(page)) {
  			start = size;
  			goto out;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
88
89
90
91
  		curr = pptr = kmap(page);
  		if ((size ^ offset) / PAGE_CACHE_BITS)
  			end = pptr + PAGE_CACHE_BITS / 32;
  		else
  			end = pptr + ((size + 31) & (PAGE_CACHE_BITS - 1)) / 32;
  	}
c2b3e1f76   Joe Perches   hfs/hfsplus: conv...
92
93
  	hfs_dbg(BITMAP, "bitmap full
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
95
96
97
98
99
  	start = size;
  	goto out;
  
  found:
  	start = offset + (curr - pptr) * 32 + i;
  	if (start >= size) {
c2b3e1f76   Joe Perches   hfs/hfsplus: conv...
100
101
  		hfs_dbg(BITMAP, "bitmap full
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  		goto out;
  	}
  	/* do any partial u32 at the start */
  	len = min(size - start, len);
  	while (1) {
  		n |= mask;
  		if (++i >= 32)
  			break;
  		mask >>= 1;
  		if (!--len || n & mask)
  			goto done;
  	}
  	if (!--len)
  		goto done;
  	*curr++ = cpu_to_be32(n);
  	/* do full u32s */
  	while (1) {
  		while (curr < end) {
  			n = be32_to_cpu(*curr);
  			if (len < 32)
  				goto last;
  			if (n) {
  				len = 32;
  				goto last;
  			}
  			*curr++ = cpu_to_be32(0xffffffff);
  			len -= 32;
  		}
  		set_page_dirty(page);
  		kunmap(page);
  		offset += PAGE_CACHE_BITS;
090d2b185   Pekka Enberg   [PATCH] read_mapp...
133
134
  		page = read_mapping_page(mapping, offset / PAGE_CACHE_BITS,
  					 NULL);
649f1ee6c   Eric Sesterhenn   hfsplus: check re...
135
136
137
138
  		if (IS_ERR(page)) {
  			start = size;
  			goto out;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  		pptr = kmap(page);
  		curr = pptr;
  		end = pptr + PAGE_CACHE_BITS / 32;
  	}
  last:
  	/* do any partial u32 at end */
  	mask = 1U << 31;
  	for (i = 0; i < len; i++) {
  		if (n & mask)
  			break;
  		n |= mask;
  		mask >>= 1;
  	}
  done:
  	*curr = cpu_to_be32(n);
  	set_page_dirty(page);
  	kunmap(page);
  	*max = offset + (curr - pptr) * 32 + i - start;
dd73a01a3   Christoph Hellwig   hfsplus: fix HFSP...
157
  	sbi->free_blocks -= *max;
9e6c5829b   Artem Bityutskiy   hfsplus: get rid ...
158
  	hfsplus_mark_mdb_dirty(sb);
c2b3e1f76   Joe Perches   hfs/hfsplus: conv...
159
160
  	hfs_dbg(BITMAP, "-> %u,%u
  ", start, *max);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161
  out:
dd73a01a3   Christoph Hellwig   hfsplus: fix HFSP...
162
  	mutex_unlock(&sbi->alloc_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
164
165
166
167
  	return start;
  }
  
  int hfsplus_block_free(struct super_block *sb, u32 offset, u32 count)
  {
dd73a01a3   Christoph Hellwig   hfsplus: fix HFSP...
168
  	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
169
170
171
172
173
174
175
176
177
  	struct page *page;
  	struct address_space *mapping;
  	__be32 *pptr, *curr, *end;
  	u32 mask, len, pnr;
  	int i;
  
  	/* is there any actual work to be done? */
  	if (!count)
  		return 0;
c2b3e1f76   Joe Perches   hfs/hfsplus: conv...
178
179
  	hfs_dbg(BITMAP, "block_free: %u,%u
  ", offset, count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
  	/* are all of the bits in range? */
dd73a01a3   Christoph Hellwig   hfsplus: fix HFSP...
181
  	if ((offset + count) > sbi->total_blocks)
5daa669c8   Alan Cox   hfsplus: avoid cr...
182
  		return -ENOENT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183

dd73a01a3   Christoph Hellwig   hfsplus: fix HFSP...
184
185
  	mutex_lock(&sbi->alloc_mutex);
  	mapping = sbi->alloc_file->i_mapping;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
186
  	pnr = offset / PAGE_CACHE_BITS;
090d2b185   Pekka Enberg   [PATCH] read_mapp...
187
  	page = read_mapping_page(mapping, pnr, NULL);
5daa669c8   Alan Cox   hfsplus: avoid cr...
188
189
  	if (IS_ERR(page))
  		goto kaboom;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
  	pptr = kmap(page);
  	curr = pptr + (offset & (PAGE_CACHE_BITS - 1)) / 32;
  	end = pptr + PAGE_CACHE_BITS / 32;
  	len = count;
  
  	/* do any partial u32 at the start */
  	i = offset % 32;
  	if (i) {
  		int j = 32 - i;
  		mask = 0xffffffffU << j;
  		if (j > count) {
  			mask |= 0xffffffffU >> (i + count);
  			*curr++ &= cpu_to_be32(mask);
  			goto out;
  		}
  		*curr++ &= cpu_to_be32(mask);
  		count -= j;
  	}
  
  	/* do full u32s */
  	while (1) {
  		while (curr < end) {
  			if (count < 32)
  				goto done;
  			*curr++ = 0;
  			count -= 32;
  		}
  		if (!count)
  			break;
  		set_page_dirty(page);
  		kunmap(page);
090d2b185   Pekka Enberg   [PATCH] read_mapp...
221
  		page = read_mapping_page(mapping, ++pnr, NULL);
5daa669c8   Alan Cox   hfsplus: avoid cr...
222
223
  		if (IS_ERR(page))
  			goto kaboom;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
224
225
226
227
228
229
230
231
232
233
234
235
236
  		pptr = kmap(page);
  		curr = pptr;
  		end = pptr + PAGE_CACHE_BITS / 32;
  	}
  done:
  	/* do any partial u32 at end */
  	if (count) {
  		mask = 0xffffffffU >> count;
  		*curr &= cpu_to_be32(mask);
  	}
  out:
  	set_page_dirty(page);
  	kunmap(page);
dd73a01a3   Christoph Hellwig   hfsplus: fix HFSP...
237
  	sbi->free_blocks += len;
9e6c5829b   Artem Bityutskiy   hfsplus: get rid ...
238
  	hfsplus_mark_mdb_dirty(sb);
dd73a01a3   Christoph Hellwig   hfsplus: fix HFSP...
239
  	mutex_unlock(&sbi->alloc_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
241
  
  	return 0;
5daa669c8   Alan Cox   hfsplus: avoid cr...
242
243
  
  kaboom:
865f38a3a   Vyacheslav Dubeyko   hfsplus: remove d...
244
245
  	pr_crit("unable to mark blocks free: error %ld
  ", PTR_ERR(page));
5daa669c8   Alan Cox   hfsplus: avoid cr...
246
247
248
  	mutex_unlock(&sbi->alloc_mutex);
  
  	return -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
249
  }