Blame view

fs/hpfs/alloc.c 14.4 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
  /*
   *  linux/fs/hpfs/alloc.c
   *
   *  Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
   *
   *  HPFS bitmap operations
   */
  
  #include "hpfs_fn.h"
2cbe5c76f   Mikulas Patocka   hpfs: remember fr...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  static void hpfs_claim_alloc(struct super_block *s, secno sec)
  {
  	struct hpfs_sb_info *sbi = hpfs_sb(s);
  	if (sbi->sb_n_free != (unsigned)-1) {
  		if (unlikely(!sbi->sb_n_free)) {
  			hpfs_error(s, "free count underflow, allocating sector %08x", sec);
  			sbi->sb_n_free = -1;
  			return;
  		}
  		sbi->sb_n_free--;
  	}
  }
  
  static void hpfs_claim_free(struct super_block *s, secno sec)
  {
  	struct hpfs_sb_info *sbi = hpfs_sb(s);
  	if (sbi->sb_n_free != (unsigned)-1) {
  		if (unlikely(sbi->sb_n_free >= sbi->sb_fs_size)) {
  			hpfs_error(s, "free count overflow, freeing sector %08x", sec);
  			sbi->sb_n_free = -1;
  			return;
  		}
  		sbi->sb_n_free++;
  	}
  }
  
  static void hpfs_claim_dirband_alloc(struct super_block *s, secno sec)
  {
  	struct hpfs_sb_info *sbi = hpfs_sb(s);
  	if (sbi->sb_n_free_dnodes != (unsigned)-1) {
  		if (unlikely(!sbi->sb_n_free_dnodes)) {
  			hpfs_error(s, "dirband free count underflow, allocating sector %08x", sec);
  			sbi->sb_n_free_dnodes = -1;
  			return;
  		}
  		sbi->sb_n_free_dnodes--;
  	}
  }
  
  static void hpfs_claim_dirband_free(struct super_block *s, secno sec)
  {
  	struct hpfs_sb_info *sbi = hpfs_sb(s);
  	if (sbi->sb_n_free_dnodes != (unsigned)-1) {
  		if (unlikely(sbi->sb_n_free_dnodes >= sbi->sb_dirband_size / 4)) {
  			hpfs_error(s, "dirband free count overflow, freeing sector %08x", sec);
  			sbi->sb_n_free_dnodes = -1;
  			return;
  		}
  		sbi->sb_n_free_dnodes++;
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
64
65
66
67
68
69
  /*
   * Check if a sector is allocated in bitmap
   * This is really slow. Turned on only if chk==2
   */
  
  static int chk_if_allocated(struct super_block *s, secno sec, char *msg)
  {
  	struct quad_buffer_head qbh;
52576da35   Al Viro   hpfs: bitmaps are...
70
  	__le32 *bmp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
  	if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "chk"))) goto fail;
52576da35   Al Viro   hpfs: bitmaps are...
72
  	if ((le32_to_cpu(bmp[(sec & 0x3fff) >> 5]) >> (sec & 0x1f)) & 1) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73
74
75
76
77
78
79
  		hpfs_error(s, "sector '%s' - %08x not allocated in bitmap", msg, sec);
  		goto fail1;
  	}
  	hpfs_brelse4(&qbh);
  	if (sec >= hpfs_sb(s)->sb_dirband_start && sec < hpfs_sb(s)->sb_dirband_start + hpfs_sb(s)->sb_dirband_size) {
  		unsigned ssec = (sec - hpfs_sb(s)->sb_dirband_start) / 4;
  		if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) goto fail;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
80
  		if ((le32_to_cpu(bmp[ssec >> 5]) >> (ssec & 0x1f)) & 1) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  			hpfs_error(s, "sector '%s' - %08x not allocated in directory bitmap", msg, sec);
  			goto fail1;
  		}
  		hpfs_brelse4(&qbh);
  	}
  	return 0;
  	fail1:
  	hpfs_brelse4(&qbh);
  	fail:
  	return 1;
  }
  
  /*
   * Check if sector(s) have proper number and additionally check if they're
   * allocated in bitmap.
   */
  	
  int hpfs_chk_sectors(struct super_block *s, secno start, int len, char *msg)
  {
  	if (start + len < start || start < 0x12 ||
  	    start + len > hpfs_sb(s)->sb_fs_size) {
  	    	hpfs_error(s, "sector(s) '%s' badly placed at %08x", msg, start);
  		return 1;
  	}
  	if (hpfs_sb(s)->sb_chk>=2) {
  		int i;
  		for (i = 0; i < len; i++)
  			if (chk_if_allocated(s, start + i, msg)) return 1;
  	}
  	return 0;
  }
  
  static secno alloc_in_bmp(struct super_block *s, secno near, unsigned n, unsigned forward)
  {
  	struct quad_buffer_head qbh;
52576da35   Al Viro   hpfs: bitmaps are...
116
  	__le32 *bmp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117
118
119
120
121
122
123
124
125
126
  	unsigned bs = near & ~0x3fff;
  	unsigned nr = (near & 0x3fff) & ~(n - 1);
  	/*unsigned mnr;*/
  	unsigned i, q;
  	int a, b;
  	secno ret = 0;
  	if (n != 1 && n != 4) {
  		hpfs_error(s, "Bad allocation size: %d", n);
  		return 0;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
127
128
129
130
131
132
133
134
135
  	if (bs != ~0x3fff) {
  		if (!(bmp = hpfs_map_bitmap(s, near >> 14, &qbh, "aib"))) goto uls;
  	} else {
  		if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) goto uls;
  	}
  	if (!tstbits(bmp, nr, n + forward)) {
  		ret = bs + nr;
  		goto rt;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  	q = nr + n; b = 0;
  	while ((a = tstbits(bmp, q, n + forward)) != 0) {
  		q += a;
  		if (n != 1) q = ((q-1)&~(n-1))+n;
  		if (!b) {
  			if (q>>5 != nr>>5) {
  				b = 1;
  				q = nr & 0x1f;
  			}
  		} else if (q > nr) break;
  	}
  	if (!a) {
  		ret = bs + q;
  		goto rt;
  	}
  	nr >>= 5;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
152
  	/*for (i = nr + 1; i != nr; i++, i &= 0x1ff) */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153
154
  	i = nr;
  	do {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
155
156
  		if (!le32_to_cpu(bmp[i])) goto cont;
  		if (n + forward >= 0x3f && le32_to_cpu(bmp[i]) != 0xffffffff) goto cont;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
157
158
  		q = i<<5;
  		if (i > 0) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
159
  			unsigned k = le32_to_cpu(bmp[i-1]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  			while (k & 0x80000000) {
  				q--; k <<= 1;
  			}
  		}
  		if (n != 1) q = ((q-1)&~(n-1))+n;
  		while ((a = tstbits(bmp, q, n + forward)) != 0) {
  			q += a;
  			if (n != 1) q = ((q-1)&~(n-1))+n;
  			if (q>>5 > i) break;
  		}
  		if (!a) {
  			ret = bs + q;
  			goto rt;
  		}
  		cont:
  		i++, i &= 0x1ff;
  	} while (i != nr);
  	rt:
  	if (ret) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
179
  		if (hpfs_sb(s)->sb_chk && ((ret >> 14) != (bs >> 14) || (le32_to_cpu(bmp[(ret & 0x3fff) >> 5]) | ~(((1 << n) - 1) << (ret & 0x1f))) != 0xffffffff)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
180
181
182
183
  			hpfs_error(s, "Allocation doesn't work! Wanted %d, allocated at %08x", n, ret);
  			ret = 0;
  			goto b;
  		}
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
184
  		bmp[(ret & 0x3fff) >> 5] &= cpu_to_le32(~(((1 << n) - 1) << (ret & 0x1f)));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185
186
187
188
189
  		hpfs_mark_4buffers_dirty(&qbh);
  	}
  	b:
  	hpfs_brelse4(&qbh);
  	uls:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
190
191
192
193
194
195
196
197
198
199
  	return ret;
  }
  
  /*
   * Allocation strategy:	1) search place near the sector specified
   *			2) search bitmap where free sectors last found
   *			3) search all bitmaps
   *			4) search all bitmaps ignoring number of pre-allocated
   *				sectors
   */
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
200
  secno hpfs_alloc_sector(struct super_block *s, secno near, unsigned n, int forward)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
201
202
203
204
205
206
207
208
209
210
211
  {
  	secno sec;
  	int i;
  	unsigned n_bmps;
  	struct hpfs_sb_info *sbi = hpfs_sb(s);
  	int f_p = 0;
  	int near_bmp;
  	if (forward < 0) {
  		forward = -forward;
  		f_p = 1;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  	n_bmps = (sbi->sb_fs_size + 0x4000 - 1) >> 14;
  	if (near && near < sbi->sb_fs_size) {
  		if ((sec = alloc_in_bmp(s, near, n, f_p ? forward : forward/4))) goto ret;
  		near_bmp = near >> 14;
  	} else near_bmp = n_bmps / 2;
  	/*
  	if (b != -1) {
  		if ((sec = alloc_in_bmp(s, b<<14, n, f_p ? forward : forward/2))) {
  			b &= 0x0fffffff;
  			goto ret;
  		}
  		if (b > 0x10000000) if ((sec = alloc_in_bmp(s, (b&0xfffffff)<<14, n, f_p ? forward : 0))) goto ret;
  	*/
  	if (!f_p) if (forward > sbi->sb_max_fwd_alloc) forward = sbi->sb_max_fwd_alloc;
  	less_fwd:
  	for (i = 0; i < n_bmps; i++) {
  		if (near_bmp+i < n_bmps && ((sec = alloc_in_bmp(s, (near_bmp+i) << 14, n, forward)))) {
  			sbi->sb_c_bitmap = near_bmp+i;
  			goto ret;
  		}	
  		if (!forward) {
  			if (near_bmp-i-1 >= 0 && ((sec = alloc_in_bmp(s, (near_bmp-i-1) << 14, n, forward)))) {
  				sbi->sb_c_bitmap = near_bmp-i-1;
  				goto ret;
  			}
  		} else {
  			if (near_bmp+i >= n_bmps && ((sec = alloc_in_bmp(s, (near_bmp+i-n_bmps) << 14, n, forward)))) {
  				sbi->sb_c_bitmap = near_bmp+i-n_bmps;
  				goto ret;
  			}
  		}
  		if (i == 1 && sbi->sb_c_bitmap != -1 && ((sec = alloc_in_bmp(s, (sbi->sb_c_bitmap) << 14, n, forward)))) {
  			goto ret;
  		}
  	}
  	if (!f_p) {
  		if (forward) {
  			sbi->sb_max_fwd_alloc = forward * 3 / 4;
  			forward /= 2;
  			goto less_fwd;
  		}
  	}
  	sec = 0;
  	ret:
2cbe5c76f   Mikulas Patocka   hpfs: remember fr...
256
257
258
259
260
261
  	if (sec) {
  		i = 0;
  		do
  			hpfs_claim_alloc(s, sec + i);
  		while (unlikely(++i < n));
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
262
263
  	if (sec && f_p) {
  		for (i = 0; i < forward; i++) {
2cbe5c76f   Mikulas Patocka   hpfs: remember fr...
264
  			if (!hpfs_alloc_if_possible(s, sec + n + i)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
265
266
267
268
269
270
  				hpfs_error(s, "Prealloc doesn't work! Wanted %d, allocated at %08x, can't allocate %d", forward, sec, i);
  				sec = 0;
  				break;
  			}
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
271
272
  	return sec;
  }
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
273
  static secno alloc_in_dirband(struct super_block *s, secno near)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
274
275
276
277
278
279
280
281
282
283
  {
  	unsigned nr = near;
  	secno sec;
  	struct hpfs_sb_info *sbi = hpfs_sb(s);
  	if (nr < sbi->sb_dirband_start)
  		nr = sbi->sb_dirband_start;
  	if (nr >= sbi->sb_dirband_start + sbi->sb_dirband_size)
  		nr = sbi->sb_dirband_start + sbi->sb_dirband_size - 4;
  	nr -= sbi->sb_dirband_start;
  	nr >>= 2;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
284
  	sec = alloc_in_bmp(s, (~0x3fff) | nr, 1, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
285
  	if (!sec) return 0;
2cbe5c76f   Mikulas Patocka   hpfs: remember fr...
286
  	hpfs_claim_dirband_alloc(s, sec);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
288
289
290
  	return ((sec & 0x3fff) << 2) + sbi->sb_dirband_start;
  }
  
  /* Alloc sector if it's free */
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
291
  int hpfs_alloc_if_possible(struct super_block *s, secno sec)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
292
293
  {
  	struct quad_buffer_head qbh;
52576da35   Al Viro   hpfs: bitmaps are...
294
  	__le32 *bmp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
295
  	if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "aip"))) goto end;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
296
297
  	if (le32_to_cpu(bmp[(sec & 0x3fff) >> 5]) & (1 << (sec & 0x1f))) {
  		bmp[(sec & 0x3fff) >> 5] &= cpu_to_le32(~(1 << (sec & 0x1f)));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
298
299
  		hpfs_mark_4buffers_dirty(&qbh);
  		hpfs_brelse4(&qbh);
2cbe5c76f   Mikulas Patocka   hpfs: remember fr...
300
  		hpfs_claim_alloc(s, sec);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
301
302
303
304
  		return 1;
  	}
  	hpfs_brelse4(&qbh);
  	end:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
305
306
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
309
310
311
  /* Free sectors in bitmaps */
  
  void hpfs_free_sectors(struct super_block *s, secno sec, unsigned n)
  {
  	struct quad_buffer_head qbh;
52576da35   Al Viro   hpfs: bitmaps are...
312
  	__le32 *bmp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
313
  	struct hpfs_sb_info *sbi = hpfs_sb(s);
b7cb1ce22   Fabian Frederick   fs/hpfs: convert ...
314
  	/*pr_info("2 - ");*/
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
315
316
317
318
319
  	if (!n) return;
  	if (sec < 0x12) {
  		hpfs_error(s, "Trying to free reserved sector %08x", sec);
  		return;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
320
321
322
323
  	sbi->sb_max_fwd_alloc += n > 0xffff ? 0xffff : n;
  	if (sbi->sb_max_fwd_alloc > 0xffffff) sbi->sb_max_fwd_alloc = 0xffffff;
  	new_map:
  	if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "free"))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
324
325
326
  		return;
  	}	
  	new_tst:
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
327
  	if ((le32_to_cpu(bmp[(sec & 0x3fff) >> 5]) >> (sec & 0x1f) & 1)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
329
  		hpfs_error(s, "sector %08x not allocated", sec);
  		hpfs_brelse4(&qbh);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
331
  		return;
  	}
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
332
  	bmp[(sec & 0x3fff) >> 5] |= cpu_to_le32(1 << (sec & 0x1f));
2cbe5c76f   Mikulas Patocka   hpfs: remember fr...
333
  	hpfs_claim_free(s, sec);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
334
335
336
  	if (!--n) {
  		hpfs_mark_4buffers_dirty(&qbh);
  		hpfs_brelse4(&qbh);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
  		return;
  	}	
  	if (!(++sec & 0x3fff)) {
  		hpfs_mark_4buffers_dirty(&qbh);
  		hpfs_brelse4(&qbh);
  		goto new_map;
  	}
  	goto new_tst;
  }
  
  /*
   * Check if there are at least n free dnodes on the filesystem.
   * Called before adding to dnode. If we run out of space while
   * splitting dnodes, it would corrupt dnode tree.
   */
  
  int hpfs_check_free_dnodes(struct super_block *s, int n)
  {
  	int n_bmps = (hpfs_sb(s)->sb_fs_size + 0x4000 - 1) >> 14;
  	int b = hpfs_sb(s)->sb_c_bitmap & 0x0fffffff;
  	int i, j;
52576da35   Al Viro   hpfs: bitmaps are...
358
  	__le32 *bmp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
359
360
361
362
  	struct quad_buffer_head qbh;
  	if ((bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
  		for (j = 0; j < 512; j++) {
  			unsigned k;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
363
364
  			if (!le32_to_cpu(bmp[j])) continue;
  			for (k = le32_to_cpu(bmp[j]); k; k >>= 1) if (k & 1) if (!--n) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
  				hpfs_brelse4(&qbh);
  				return 0;
  			}
  		}
  	}
  	hpfs_brelse4(&qbh);
  	i = 0;
  	if (hpfs_sb(s)->sb_c_bitmap != -1) {
  		bmp = hpfs_map_bitmap(s, b, &qbh, "chkdn1");
  		goto chk_bmp;
  	}
  	chk_next:
  	if (i == b) i++;
  	if (i >= n_bmps) return 1;
  	bmp = hpfs_map_bitmap(s, i, &qbh, "chkdn2");
  	chk_bmp:
  	if (bmp) {
  		for (j = 0; j < 512; j++) {
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
383
384
  			u32 k;
  			if (!le32_to_cpu(bmp[j])) continue;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
385
  			for (k = 0xf; k; k <<= 4)
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
386
  				if ((le32_to_cpu(bmp[j]) & k) == k) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
  					if (!--n) {
  						hpfs_brelse4(&qbh);
  						return 0;
  					}
  				}
  		}
  		hpfs_brelse4(&qbh);
  	}
  	i++;
  	goto chk_next;
  }
  
  void hpfs_free_dnode(struct super_block *s, dnode_secno dno)
  {
  	if (hpfs_sb(s)->sb_chk) if (dno & 3) {
  		hpfs_error(s, "hpfs_free_dnode: dnode %08x not aligned", dno);
  		return;
  	}
  	if (dno < hpfs_sb(s)->sb_dirband_start ||
  	    dno >= hpfs_sb(s)->sb_dirband_start + hpfs_sb(s)->sb_dirband_size) {
  		hpfs_free_sectors(s, dno, 4);
  	} else {
  		struct quad_buffer_head qbh;
52576da35   Al Viro   hpfs: bitmaps are...
410
  		__le32 *bmp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
411
  		unsigned ssec = (dno - hpfs_sb(s)->sb_dirband_start) / 4;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
412
  		if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413
414
  			return;
  		}
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
415
  		bmp[ssec >> 5] |= cpu_to_le32(1 << (ssec & 0x1f));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
416
417
  		hpfs_mark_4buffers_dirty(&qbh);
  		hpfs_brelse4(&qbh);
2cbe5c76f   Mikulas Patocka   hpfs: remember fr...
418
  		hpfs_claim_dirband_free(s, dno);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
419
420
421
422
  	}
  }
  
  struct dnode *hpfs_alloc_dnode(struct super_block *s, secno near,
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
423
  			 dnode_secno *dno, struct quad_buffer_head *qbh)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
424
425
  {
  	struct dnode *d;
2cbe5c76f   Mikulas Patocka   hpfs: remember fr...
426
  	if (hpfs_get_free_dnodes(s) > FREE_DNODES_ADD) {
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
427
428
  		if (!(*dno = alloc_in_dirband(s, near)))
  			if (!(*dno = hpfs_alloc_sector(s, near, 4, 0))) return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
429
  	} else {
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
430
431
  		if (!(*dno = hpfs_alloc_sector(s, near, 4, 0)))
  			if (!(*dno = alloc_in_dirband(s, near))) return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
432
433
434
435
436
437
  	}
  	if (!(d = hpfs_get_4sectors(s, *dno, qbh))) {
  		hpfs_free_dnode(s, *dno);
  		return NULL;
  	}
  	memset(d, 0, 2048);
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
438
439
  	d->magic = cpu_to_le32(DNODE_MAGIC);
  	d->first_free = cpu_to_le32(52);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
440
441
442
443
  	d->dirent[0] = 32;
  	d->dirent[2] = 8;
  	d->dirent[30] = 1;
  	d->dirent[31] = 255;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
444
  	d->self = cpu_to_le32(*dno);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
445
446
447
448
449
450
451
  	return d;
  }
  
  struct fnode *hpfs_alloc_fnode(struct super_block *s, secno near, fnode_secno *fno,
  			  struct buffer_head **bh)
  {
  	struct fnode *f;
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
452
  	if (!(*fno = hpfs_alloc_sector(s, near, 1, FNODE_ALLOC_FWD))) return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
453
454
455
456
457
  	if (!(f = hpfs_get_sector(s, *fno, bh))) {
  		hpfs_free_sectors(s, *fno, 1);
  		return NULL;
  	}	
  	memset(f, 0, 512);
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
458
459
  	f->magic = cpu_to_le32(FNODE_MAGIC);
  	f->ea_offs = cpu_to_le16(0xc4);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
460
  	f->btree.n_free_nodes = 8;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
461
  	f->btree.first_free = cpu_to_le16(8);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
462
463
464
465
466
467
468
  	return f;
  }
  
  struct anode *hpfs_alloc_anode(struct super_block *s, secno near, anode_secno *ano,
  			  struct buffer_head **bh)
  {
  	struct anode *a;
7d23ce36e   Mikulas Patocka   HPFS: Remove rema...
469
  	if (!(*ano = hpfs_alloc_sector(s, near, 1, ANODE_ALLOC_FWD))) return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470
471
472
473
474
  	if (!(a = hpfs_get_sector(s, *ano, bh))) {
  		hpfs_free_sectors(s, *ano, 1);
  		return NULL;
  	}
  	memset(a, 0, 512);
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
475
476
  	a->magic = cpu_to_le32(ANODE_MAGIC);
  	a->self = cpu_to_le32(*ano);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
477
478
  	a->btree.n_free_nodes = 40;
  	a->btree.n_used_nodes = 0;
0b69760be   Mikulas Patocka   HPFS: Fix endiani...
479
  	a->btree.first_free = cpu_to_le16(8);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
480
481
  	return a;
  }
a27b5b97d   Mikulas Patocka   hpfs: add fstrim ...
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
  
  static unsigned find_run(__le32 *bmp, unsigned *idx)
  {
  	unsigned len;
  	while (tstbits(bmp, *idx, 1)) {
  		(*idx)++;
  		if (unlikely(*idx >= 0x4000))
  			return 0;
  	}
  	len = 1;
  	while (!tstbits(bmp, *idx + len, 1))
  		len++;
  	return len;
  }
  
  static int do_trim(struct super_block *s, secno start, unsigned len, secno limit_start, secno limit_end, unsigned minlen, unsigned *result)
  {
  	int err;
  	secno end;
  	if (fatal_signal_pending(current))
  		return -EINTR;
  	end = start + len;
  	if (start < limit_start)
  		start = limit_start;
  	if (end > limit_end)
  		end = limit_end;
  	if (start >= end)
  		return 0;
  	if (end - start < minlen)
  		return 0;
  	err = sb_issue_discard(s, start, end - start, GFP_NOFS, 0);
  	if (err)
  		return err;
  	*result += end - start;
  	return 0;
  }
  
  int hpfs_trim_fs(struct super_block *s, u64 start, u64 end, u64 minlen, unsigned *result)
  {
  	int err = 0;
  	struct hpfs_sb_info *sbi = hpfs_sb(s);
  	unsigned idx, len, start_bmp, end_bmp;
  	__le32 *bmp;
  	struct quad_buffer_head qbh;
  
  	*result = 0;
  	if (!end || end > sbi->sb_fs_size)
  		end = sbi->sb_fs_size;
  	if (start >= sbi->sb_fs_size)
  		return 0;
  	if (minlen > 0x4000)
  		return 0;
  	if (start < sbi->sb_dirband_start + sbi->sb_dirband_size && end > sbi->sb_dirband_start) {
  		hpfs_lock(s);
bc98a42c1   David Howells   VFS: Convert sb->...
536
  		if (sb_rdonly(s)) {
a27b5b97d   Mikulas Patocka   hpfs: add fstrim ...
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
  			err = -EROFS;
  			goto unlock_1;
  		}
  		if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) {
  			err = -EIO;
  			goto unlock_1;
  		}
  		idx = 0;
  		while ((len = find_run(bmp, &idx)) && !err) {
  			err = do_trim(s, sbi->sb_dirband_start + idx * 4, len * 4, start, end, minlen, result);
  			idx += len;
  		}
  		hpfs_brelse4(&qbh);
  unlock_1:
  		hpfs_unlock(s);
  	}
  	start_bmp = start >> 14;
  	end_bmp = (end + 0x3fff) >> 14;
  	while (start_bmp < end_bmp && !err) {
  		hpfs_lock(s);
bc98a42c1   David Howells   VFS: Convert sb->...
557
  		if (sb_rdonly(s)) {
a27b5b97d   Mikulas Patocka   hpfs: add fstrim ...
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
  			err = -EROFS;
  			goto unlock_2;
  		}
  		if (!(bmp = hpfs_map_bitmap(s, start_bmp, &qbh, "trim"))) {
  			err = -EIO;
  			goto unlock_2;
  		}
  		idx = 0;
  		while ((len = find_run(bmp, &idx)) && !err) {
  			err = do_trim(s, (start_bmp << 14) + idx, len, start, end, minlen, result);
  			idx += len;
  		}
  		hpfs_brelse4(&qbh);
  unlock_2:
  		hpfs_unlock(s);
  		start_bmp++;
  	}
  	return err;
  }