alloc.c 12 KB

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