alloc.c 14 KB

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