alloc.c 11 KB

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