bitmap.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. /*
  2. * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
  3. */
  4. /* Reiserfs block (de)allocator, bitmap-based. */
  5. #include <linux/time.h>
  6. #include "reiserfs.h"
  7. #include <linux/errno.h>
  8. #include <linux/buffer_head.h>
  9. #include <linux/kernel.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/quotaops.h>
  13. #include <linux/seq_file.h>
  14. #define PREALLOCATION_SIZE 9
  15. /* different reiserfs block allocator options */
  16. #define SB_ALLOC_OPTS(s) (REISERFS_SB(s)->s_alloc_options.bits)
  17. #define _ALLOC_concentrating_formatted_nodes 0
  18. #define _ALLOC_displacing_large_files 1
  19. #define _ALLOC_displacing_new_packing_localities 2
  20. #define _ALLOC_old_hashed_relocation 3
  21. #define _ALLOC_new_hashed_relocation 4
  22. #define _ALLOC_skip_busy 5
  23. #define _ALLOC_displace_based_on_dirid 6
  24. #define _ALLOC_hashed_formatted_nodes 7
  25. #define _ALLOC_old_way 8
  26. #define _ALLOC_hundredth_slices 9
  27. #define _ALLOC_dirid_groups 10
  28. #define _ALLOC_oid_groups 11
  29. #define _ALLOC_packing_groups 12
  30. #define concentrating_formatted_nodes(s) test_bit(_ALLOC_concentrating_formatted_nodes, &SB_ALLOC_OPTS(s))
  31. #define displacing_large_files(s) test_bit(_ALLOC_displacing_large_files, &SB_ALLOC_OPTS(s))
  32. #define displacing_new_packing_localities(s) test_bit(_ALLOC_displacing_new_packing_localities, &SB_ALLOC_OPTS(s))
  33. #define SET_OPTION(optname) \
  34. do { \
  35. reiserfs_info(s, "block allocator option \"%s\" is set", #optname); \
  36. set_bit(_ALLOC_ ## optname , &SB_ALLOC_OPTS(s)); \
  37. } while(0)
  38. #define TEST_OPTION(optname, s) \
  39. test_bit(_ALLOC_ ## optname , &SB_ALLOC_OPTS(s))
  40. static inline void get_bit_address(struct super_block *s,
  41. b_blocknr_t block,
  42. unsigned int *bmap_nr,
  43. unsigned int *offset)
  44. {
  45. /*
  46. * It is in the bitmap block number equal to the block
  47. * number divided by the number of bits in a block.
  48. */
  49. *bmap_nr = block >> (s->s_blocksize_bits + 3);
  50. /* Within that bitmap block it is located at bit offset *offset. */
  51. *offset = block & ((s->s_blocksize << 3) - 1);
  52. }
  53. int is_reusable(struct super_block *s, b_blocknr_t block, int bit_value)
  54. {
  55. unsigned int bmap, offset;
  56. unsigned int bmap_count = reiserfs_bmap_count(s);
  57. if (block == 0 || block >= SB_BLOCK_COUNT(s)) {
  58. reiserfs_error(s, "vs-4010",
  59. "block number is out of range %lu (%u)",
  60. block, SB_BLOCK_COUNT(s));
  61. return 0;
  62. }
  63. get_bit_address(s, block, &bmap, &offset);
  64. /*
  65. * Old format filesystem? Unlikely, but the bitmaps are all
  66. * up front so we need to account for it.
  67. */
  68. if (unlikely(test_bit(REISERFS_OLD_FORMAT,
  69. &REISERFS_SB(s)->s_properties))) {
  70. b_blocknr_t bmap1 = REISERFS_SB(s)->s_sbh->b_blocknr + 1;
  71. if (block >= bmap1 &&
  72. block <= bmap1 + bmap_count) {
  73. reiserfs_error(s, "vs-4019", "bitmap block %lu(%u) "
  74. "can't be freed or reused",
  75. block, bmap_count);
  76. return 0;
  77. }
  78. } else {
  79. if (offset == 0) {
  80. reiserfs_error(s, "vs-4020", "bitmap block %lu(%u) "
  81. "can't be freed or reused",
  82. block, bmap_count);
  83. return 0;
  84. }
  85. }
  86. if (bmap >= bmap_count) {
  87. reiserfs_error(s, "vs-4030", "bitmap for requested block "
  88. "is out of range: block=%lu, bitmap_nr=%u",
  89. block, bmap);
  90. return 0;
  91. }
  92. if (bit_value == 0 && block == SB_ROOT_BLOCK(s)) {
  93. reiserfs_error(s, "vs-4050", "this is root block (%u), "
  94. "it must be busy", SB_ROOT_BLOCK(s));
  95. return 0;
  96. }
  97. return 1;
  98. }
  99. /*
  100. * Searches in journal structures for a given block number (bmap, off).
  101. * If block is found in reiserfs journal it suggests next free block
  102. * candidate to test.
  103. */
  104. static inline int is_block_in_journal(struct super_block *s, unsigned int bmap,
  105. int off, int *next)
  106. {
  107. b_blocknr_t tmp;
  108. if (reiserfs_in_journal(s, bmap, off, 1, &tmp)) {
  109. if (tmp) { /* hint supplied */
  110. *next = tmp;
  111. PROC_INFO_INC(s, scan_bitmap.in_journal_hint);
  112. } else {
  113. (*next) = off + 1; /* inc offset to avoid looping. */
  114. PROC_INFO_INC(s, scan_bitmap.in_journal_nohint);
  115. }
  116. PROC_INFO_INC(s, scan_bitmap.retry);
  117. return 1;
  118. }
  119. return 0;
  120. }
  121. /*
  122. * Searches for a window of zero bits with given minimum and maximum
  123. * lengths in one bitmap block
  124. */
  125. static int scan_bitmap_block(struct reiserfs_transaction_handle *th,
  126. unsigned int bmap_n, int *beg, int boundary,
  127. int min, int max, int unfm)
  128. {
  129. struct super_block *s = th->t_super;
  130. struct reiserfs_bitmap_info *bi = &SB_AP_BITMAP(s)[bmap_n];
  131. struct buffer_head *bh;
  132. int end, next;
  133. int org = *beg;
  134. BUG_ON(!th->t_trans_id);
  135. RFALSE(bmap_n >= reiserfs_bmap_count(s), "Bitmap %u is out of "
  136. "range (0..%u)", bmap_n, reiserfs_bmap_count(s) - 1);
  137. PROC_INFO_INC(s, scan_bitmap.bmap);
  138. if (!bi) {
  139. reiserfs_error(s, "jdm-4055", "NULL bitmap info pointer "
  140. "for bitmap %d", bmap_n);
  141. return 0;
  142. }
  143. bh = reiserfs_read_bitmap_block(s, bmap_n);
  144. if (bh == NULL)
  145. return 0;
  146. while (1) {
  147. cont:
  148. if (bi->free_count < min) {
  149. brelse(bh);
  150. return 0; /* No free blocks in this bitmap */
  151. }
  152. /* search for a first zero bit -- beginning of a window */
  153. *beg = reiserfs_find_next_zero_le_bit
  154. ((unsigned long *)(bh->b_data), boundary, *beg);
  155. /*
  156. * search for a zero bit fails or the rest of bitmap block
  157. * cannot contain a zero window of minimum size
  158. */
  159. if (*beg + min > boundary) {
  160. brelse(bh);
  161. return 0;
  162. }
  163. if (unfm && is_block_in_journal(s, bmap_n, *beg, beg))
  164. continue;
  165. /* first zero bit found; we check next bits */
  166. for (end = *beg + 1;; end++) {
  167. if (end >= *beg + max || end >= boundary
  168. || reiserfs_test_le_bit(end, bh->b_data)) {
  169. next = end;
  170. break;
  171. }
  172. /*
  173. * finding the other end of zero bit window requires
  174. * looking into journal structures (in case of
  175. * searching for free blocks for unformatted nodes)
  176. */
  177. if (unfm && is_block_in_journal(s, bmap_n, end, &next))
  178. break;
  179. }
  180. /*
  181. * now (*beg) points to beginning of zero bits window,
  182. * (end) points to one bit after the window end
  183. */
  184. /* found window of proper size */
  185. if (end - *beg >= min) {
  186. int i;
  187. reiserfs_prepare_for_journal(s, bh, 1);
  188. /*
  189. * try to set all blocks used checking are
  190. * they still free
  191. */
  192. for (i = *beg; i < end; i++) {
  193. /* Don't check in journal again. */
  194. if (reiserfs_test_and_set_le_bit
  195. (i, bh->b_data)) {
  196. /*
  197. * bit was set by another process while
  198. * we slept in prepare_for_journal()
  199. */
  200. PROC_INFO_INC(s, scan_bitmap.stolen);
  201. /*
  202. * we can continue with smaller set
  203. * of allocated blocks, if length of
  204. * this set is more or equal to `min'
  205. */
  206. if (i >= *beg + min) {
  207. end = i;
  208. break;
  209. }
  210. /*
  211. * otherwise we clear all bit
  212. * were set ...
  213. */
  214. while (--i >= *beg)
  215. reiserfs_clear_le_bit
  216. (i, bh->b_data);
  217. reiserfs_restore_prepared_buffer(s, bh);
  218. *beg = org;
  219. /*
  220. * Search again in current block
  221. * from beginning
  222. */
  223. goto cont;
  224. }
  225. }
  226. bi->free_count -= (end - *beg);
  227. journal_mark_dirty(th, bh);
  228. brelse(bh);
  229. /* free block count calculation */
  230. reiserfs_prepare_for_journal(s, SB_BUFFER_WITH_SB(s),
  231. 1);
  232. PUT_SB_FREE_BLOCKS(s, SB_FREE_BLOCKS(s) - (end - *beg));
  233. journal_mark_dirty(th, SB_BUFFER_WITH_SB(s));
  234. return end - (*beg);
  235. } else {
  236. *beg = next;
  237. }
  238. }
  239. }
  240. static int bmap_hash_id(struct super_block *s, u32 id)
  241. {
  242. char *hash_in = NULL;
  243. unsigned long hash;
  244. unsigned bm;
  245. if (id <= 2) {
  246. bm = 1;
  247. } else {
  248. hash_in = (char *)(&id);
  249. hash = keyed_hash(hash_in, 4);
  250. bm = hash % reiserfs_bmap_count(s);
  251. if (!bm)
  252. bm = 1;
  253. }
  254. /* this can only be true when SB_BMAP_NR = 1 */
  255. if (bm >= reiserfs_bmap_count(s))
  256. bm = 0;
  257. return bm;
  258. }
  259. /*
  260. * hashes the id and then returns > 0 if the block group for the
  261. * corresponding hash is full
  262. */
  263. static inline int block_group_used(struct super_block *s, u32 id)
  264. {
  265. int bm = bmap_hash_id(s, id);
  266. struct reiserfs_bitmap_info *info = &SB_AP_BITMAP(s)[bm];
  267. /*
  268. * If we don't have cached information on this bitmap block, we're
  269. * going to have to load it later anyway. Loading it here allows us
  270. * to make a better decision. This favors long-term performance gain
  271. * with a better on-disk layout vs. a short term gain of skipping the
  272. * read and potentially having a bad placement.
  273. */
  274. if (info->free_count == UINT_MAX) {
  275. struct buffer_head *bh = reiserfs_read_bitmap_block(s, bm);
  276. brelse(bh);
  277. }
  278. if (info->free_count > ((s->s_blocksize << 3) * 60 / 100)) {
  279. return 0;
  280. }
  281. return 1;
  282. }
  283. /*
  284. * the packing is returned in disk byte order
  285. */
  286. __le32 reiserfs_choose_packing(struct inode * dir)
  287. {
  288. __le32 packing;
  289. if (TEST_OPTION(packing_groups, dir->i_sb)) {
  290. u32 parent_dir = le32_to_cpu(INODE_PKEY(dir)->k_dir_id);
  291. /*
  292. * some versions of reiserfsck expect packing locality 1 to be
  293. * special
  294. */
  295. if (parent_dir == 1 || block_group_used(dir->i_sb, parent_dir))
  296. packing = INODE_PKEY(dir)->k_objectid;
  297. else
  298. packing = INODE_PKEY(dir)->k_dir_id;
  299. } else
  300. packing = INODE_PKEY(dir)->k_objectid;
  301. return packing;
  302. }
  303. /*
  304. * Tries to find contiguous zero bit window (given size) in given region of
  305. * bitmap and place new blocks there. Returns number of allocated blocks.
  306. */
  307. static int scan_bitmap(struct reiserfs_transaction_handle *th,
  308. b_blocknr_t * start, b_blocknr_t finish,
  309. int min, int max, int unfm, sector_t file_block)
  310. {
  311. int nr_allocated = 0;
  312. struct super_block *s = th->t_super;
  313. unsigned int bm, off;
  314. unsigned int end_bm, end_off;
  315. unsigned int off_max = s->s_blocksize << 3;
  316. BUG_ON(!th->t_trans_id);
  317. PROC_INFO_INC(s, scan_bitmap.call);
  318. /* No point in looking for more free blocks */
  319. if (SB_FREE_BLOCKS(s) <= 0)
  320. return 0;
  321. get_bit_address(s, *start, &bm, &off);
  322. get_bit_address(s, finish, &end_bm, &end_off);
  323. if (bm > reiserfs_bmap_count(s))
  324. return 0;
  325. if (end_bm > reiserfs_bmap_count(s))
  326. end_bm = reiserfs_bmap_count(s);
  327. /*
  328. * When the bitmap is more than 10% free, anyone can allocate.
  329. * When it's less than 10% free, only files that already use the
  330. * bitmap are allowed. Once we pass 80% full, this restriction
  331. * is lifted.
  332. *
  333. * We do this so that files that grow later still have space close to
  334. * their original allocation. This improves locality, and presumably
  335. * performance as a result.
  336. *
  337. * This is only an allocation policy and does not make up for getting a
  338. * bad hint. Decent hinting must be implemented for this to work well.
  339. */
  340. if (TEST_OPTION(skip_busy, s)
  341. && SB_FREE_BLOCKS(s) > SB_BLOCK_COUNT(s) / 20) {
  342. for (; bm < end_bm; bm++, off = 0) {
  343. if ((off && (!unfm || (file_block != 0)))
  344. || SB_AP_BITMAP(s)[bm].free_count >
  345. (s->s_blocksize << 3) / 10)
  346. nr_allocated =
  347. scan_bitmap_block(th, bm, &off, off_max,
  348. min, max, unfm);
  349. if (nr_allocated)
  350. goto ret;
  351. }
  352. /* we know from above that start is a reasonable number */
  353. get_bit_address(s, *start, &bm, &off);
  354. }
  355. for (; bm < end_bm; bm++, off = 0) {
  356. nr_allocated =
  357. scan_bitmap_block(th, bm, &off, off_max, min, max, unfm);
  358. if (nr_allocated)
  359. goto ret;
  360. }
  361. nr_allocated =
  362. scan_bitmap_block(th, bm, &off, end_off + 1, min, max, unfm);
  363. ret:
  364. *start = bm * off_max + off;
  365. return nr_allocated;
  366. }
  367. static void _reiserfs_free_block(struct reiserfs_transaction_handle *th,
  368. struct inode *inode, b_blocknr_t block,
  369. int for_unformatted)
  370. {
  371. struct super_block *s = th->t_super;
  372. struct reiserfs_super_block *rs;
  373. struct buffer_head *sbh, *bmbh;
  374. struct reiserfs_bitmap_info *apbi;
  375. unsigned int nr, offset;
  376. BUG_ON(!th->t_trans_id);
  377. PROC_INFO_INC(s, free_block);
  378. rs = SB_DISK_SUPER_BLOCK(s);
  379. sbh = SB_BUFFER_WITH_SB(s);
  380. apbi = SB_AP_BITMAP(s);
  381. get_bit_address(s, block, &nr, &offset);
  382. if (nr >= reiserfs_bmap_count(s)) {
  383. reiserfs_error(s, "vs-4075", "block %lu is out of range",
  384. block);
  385. return;
  386. }
  387. bmbh = reiserfs_read_bitmap_block(s, nr);
  388. if (!bmbh)
  389. return;
  390. reiserfs_prepare_for_journal(s, bmbh, 1);
  391. /* clear bit for the given block in bit map */
  392. if (!reiserfs_test_and_clear_le_bit(offset, bmbh->b_data)) {
  393. reiserfs_error(s, "vs-4080",
  394. "block %lu: bit already cleared", block);
  395. }
  396. apbi[nr].free_count++;
  397. journal_mark_dirty(th, bmbh);
  398. brelse(bmbh);
  399. reiserfs_prepare_for_journal(s, sbh, 1);
  400. /* update super block */
  401. set_sb_free_blocks(rs, sb_free_blocks(rs) + 1);
  402. journal_mark_dirty(th, sbh);
  403. if (for_unformatted) {
  404. int depth = reiserfs_write_unlock_nested(s);
  405. dquot_free_block_nodirty(inode, 1);
  406. reiserfs_write_lock_nested(s, depth);
  407. }
  408. }
  409. void reiserfs_free_block(struct reiserfs_transaction_handle *th,
  410. struct inode *inode, b_blocknr_t block,
  411. int for_unformatted)
  412. {
  413. struct super_block *s = th->t_super;
  414. BUG_ON(!th->t_trans_id);
  415. RFALSE(!s, "vs-4061: trying to free block on nonexistent device");
  416. if (!is_reusable(s, block, 1))
  417. return;
  418. if (block > sb_block_count(REISERFS_SB(s)->s_rs)) {
  419. reiserfs_error(th->t_super, "bitmap-4072",
  420. "Trying to free block outside file system "
  421. "boundaries (%lu > %lu)",
  422. block, sb_block_count(REISERFS_SB(s)->s_rs));
  423. return;
  424. }
  425. /* mark it before we clear it, just in case */
  426. journal_mark_freed(th, s, block);
  427. _reiserfs_free_block(th, inode, block, for_unformatted);
  428. }
  429. /* preallocated blocks don't need to be run through journal_mark_freed */
  430. static void reiserfs_free_prealloc_block(struct reiserfs_transaction_handle *th,
  431. struct inode *inode, b_blocknr_t block)
  432. {
  433. BUG_ON(!th->t_trans_id);
  434. RFALSE(!th->t_super,
  435. "vs-4060: trying to free block on nonexistent device");
  436. if (!is_reusable(th->t_super, block, 1))
  437. return;
  438. _reiserfs_free_block(th, inode, block, 1);
  439. }
  440. static void __discard_prealloc(struct reiserfs_transaction_handle *th,
  441. struct reiserfs_inode_info *ei)
  442. {
  443. unsigned long save = ei->i_prealloc_block;
  444. int dirty = 0;
  445. struct inode *inode = &ei->vfs_inode;
  446. BUG_ON(!th->t_trans_id);
  447. #ifdef CONFIG_REISERFS_CHECK
  448. if (ei->i_prealloc_count < 0)
  449. reiserfs_error(th->t_super, "zam-4001",
  450. "inode has negative prealloc blocks count.");
  451. #endif
  452. while (ei->i_prealloc_count > 0) {
  453. reiserfs_free_prealloc_block(th, inode, ei->i_prealloc_block);
  454. ei->i_prealloc_block++;
  455. ei->i_prealloc_count--;
  456. dirty = 1;
  457. }
  458. if (dirty)
  459. reiserfs_update_sd(th, inode);
  460. ei->i_prealloc_block = save;
  461. list_del_init(&ei->i_prealloc_list);
  462. }
  463. /* FIXME: It should be inline function */
  464. void reiserfs_discard_prealloc(struct reiserfs_transaction_handle *th,
  465. struct inode *inode)
  466. {
  467. struct reiserfs_inode_info *ei = REISERFS_I(inode);
  468. BUG_ON(!th->t_trans_id);
  469. if (ei->i_prealloc_count)
  470. __discard_prealloc(th, ei);
  471. }
  472. void reiserfs_discard_all_prealloc(struct reiserfs_transaction_handle *th)
  473. {
  474. struct list_head *plist = &SB_JOURNAL(th->t_super)->j_prealloc_list;
  475. BUG_ON(!th->t_trans_id);
  476. while (!list_empty(plist)) {
  477. struct reiserfs_inode_info *ei;
  478. ei = list_entry(plist->next, struct reiserfs_inode_info,
  479. i_prealloc_list);
  480. #ifdef CONFIG_REISERFS_CHECK
  481. if (!ei->i_prealloc_count) {
  482. reiserfs_error(th->t_super, "zam-4001",
  483. "inode is in prealloc list but has "
  484. "no preallocated blocks.");
  485. }
  486. #endif
  487. __discard_prealloc(th, ei);
  488. }
  489. }
  490. void reiserfs_init_alloc_options(struct super_block *s)
  491. {
  492. set_bit(_ALLOC_skip_busy, &SB_ALLOC_OPTS(s));
  493. set_bit(_ALLOC_dirid_groups, &SB_ALLOC_OPTS(s));
  494. set_bit(_ALLOC_packing_groups, &SB_ALLOC_OPTS(s));
  495. }
  496. /* block allocator related options are parsed here */
  497. int reiserfs_parse_alloc_options(struct super_block *s, char *options)
  498. {
  499. char *this_char, *value;
  500. /* clear default settings */
  501. REISERFS_SB(s)->s_alloc_options.bits = 0;
  502. while ((this_char = strsep(&options, ":")) != NULL) {
  503. if ((value = strchr(this_char, '=')) != NULL)
  504. *value++ = 0;
  505. if (!strcmp(this_char, "concentrating_formatted_nodes")) {
  506. int temp;
  507. SET_OPTION(concentrating_formatted_nodes);
  508. temp = (value
  509. && *value) ? simple_strtoul(value, &value,
  510. 0) : 10;
  511. if (temp <= 0 || temp > 100) {
  512. REISERFS_SB(s)->s_alloc_options.border = 10;
  513. } else {
  514. REISERFS_SB(s)->s_alloc_options.border =
  515. 100 / temp;
  516. }
  517. continue;
  518. }
  519. if (!strcmp(this_char, "displacing_large_files")) {
  520. SET_OPTION(displacing_large_files);
  521. REISERFS_SB(s)->s_alloc_options.large_file_size =
  522. (value
  523. && *value) ? simple_strtoul(value, &value, 0) : 16;
  524. continue;
  525. }
  526. if (!strcmp(this_char, "displacing_new_packing_localities")) {
  527. SET_OPTION(displacing_new_packing_localities);
  528. continue;
  529. }
  530. if (!strcmp(this_char, "old_hashed_relocation")) {
  531. SET_OPTION(old_hashed_relocation);
  532. continue;
  533. }
  534. if (!strcmp(this_char, "new_hashed_relocation")) {
  535. SET_OPTION(new_hashed_relocation);
  536. continue;
  537. }
  538. if (!strcmp(this_char, "dirid_groups")) {
  539. SET_OPTION(dirid_groups);
  540. continue;
  541. }
  542. if (!strcmp(this_char, "oid_groups")) {
  543. SET_OPTION(oid_groups);
  544. continue;
  545. }
  546. if (!strcmp(this_char, "packing_groups")) {
  547. SET_OPTION(packing_groups);
  548. continue;
  549. }
  550. if (!strcmp(this_char, "hashed_formatted_nodes")) {
  551. SET_OPTION(hashed_formatted_nodes);
  552. continue;
  553. }
  554. if (!strcmp(this_char, "skip_busy")) {
  555. SET_OPTION(skip_busy);
  556. continue;
  557. }
  558. if (!strcmp(this_char, "hundredth_slices")) {
  559. SET_OPTION(hundredth_slices);
  560. continue;
  561. }
  562. if (!strcmp(this_char, "old_way")) {
  563. SET_OPTION(old_way);
  564. continue;
  565. }
  566. if (!strcmp(this_char, "displace_based_on_dirid")) {
  567. SET_OPTION(displace_based_on_dirid);
  568. continue;
  569. }
  570. if (!strcmp(this_char, "preallocmin")) {
  571. REISERFS_SB(s)->s_alloc_options.preallocmin =
  572. (value
  573. && *value) ? simple_strtoul(value, &value, 0) : 4;
  574. continue;
  575. }
  576. if (!strcmp(this_char, "preallocsize")) {
  577. REISERFS_SB(s)->s_alloc_options.preallocsize =
  578. (value
  579. && *value) ? simple_strtoul(value, &value,
  580. 0) :
  581. PREALLOCATION_SIZE;
  582. continue;
  583. }
  584. reiserfs_warning(s, "zam-4001", "unknown option - %s",
  585. this_char);
  586. return 1;
  587. }
  588. reiserfs_info(s, "allocator options = [%08x]\n", SB_ALLOC_OPTS(s));
  589. return 0;
  590. }
  591. static void print_sep(struct seq_file *seq, int *first)
  592. {
  593. if (!*first)
  594. seq_puts(seq, ":");
  595. else
  596. *first = 0;
  597. }
  598. void show_alloc_options(struct seq_file *seq, struct super_block *s)
  599. {
  600. int first = 1;
  601. if (SB_ALLOC_OPTS(s) == ((1 << _ALLOC_skip_busy) |
  602. (1 << _ALLOC_dirid_groups) | (1 << _ALLOC_packing_groups)))
  603. return;
  604. seq_puts(seq, ",alloc=");
  605. if (TEST_OPTION(concentrating_formatted_nodes, s)) {
  606. print_sep(seq, &first);
  607. if (REISERFS_SB(s)->s_alloc_options.border != 10) {
  608. seq_printf(seq, "concentrating_formatted_nodes=%d",
  609. 100 / REISERFS_SB(s)->s_alloc_options.border);
  610. } else
  611. seq_puts(seq, "concentrating_formatted_nodes");
  612. }
  613. if (TEST_OPTION(displacing_large_files, s)) {
  614. print_sep(seq, &first);
  615. if (REISERFS_SB(s)->s_alloc_options.large_file_size != 16) {
  616. seq_printf(seq, "displacing_large_files=%lu",
  617. REISERFS_SB(s)->s_alloc_options.large_file_size);
  618. } else
  619. seq_puts(seq, "displacing_large_files");
  620. }
  621. if (TEST_OPTION(displacing_new_packing_localities, s)) {
  622. print_sep(seq, &first);
  623. seq_puts(seq, "displacing_new_packing_localities");
  624. }
  625. if (TEST_OPTION(old_hashed_relocation, s)) {
  626. print_sep(seq, &first);
  627. seq_puts(seq, "old_hashed_relocation");
  628. }
  629. if (TEST_OPTION(new_hashed_relocation, s)) {
  630. print_sep(seq, &first);
  631. seq_puts(seq, "new_hashed_relocation");
  632. }
  633. if (TEST_OPTION(dirid_groups, s)) {
  634. print_sep(seq, &first);
  635. seq_puts(seq, "dirid_groups");
  636. }
  637. if (TEST_OPTION(oid_groups, s)) {
  638. print_sep(seq, &first);
  639. seq_puts(seq, "oid_groups");
  640. }
  641. if (TEST_OPTION(packing_groups, s)) {
  642. print_sep(seq, &first);
  643. seq_puts(seq, "packing_groups");
  644. }
  645. if (TEST_OPTION(hashed_formatted_nodes, s)) {
  646. print_sep(seq, &first);
  647. seq_puts(seq, "hashed_formatted_nodes");
  648. }
  649. if (TEST_OPTION(skip_busy, s)) {
  650. print_sep(seq, &first);
  651. seq_puts(seq, "skip_busy");
  652. }
  653. if (TEST_OPTION(hundredth_slices, s)) {
  654. print_sep(seq, &first);
  655. seq_puts(seq, "hundredth_slices");
  656. }
  657. if (TEST_OPTION(old_way, s)) {
  658. print_sep(seq, &first);
  659. seq_puts(seq, "old_way");
  660. }
  661. if (TEST_OPTION(displace_based_on_dirid, s)) {
  662. print_sep(seq, &first);
  663. seq_puts(seq, "displace_based_on_dirid");
  664. }
  665. if (REISERFS_SB(s)->s_alloc_options.preallocmin != 0) {
  666. print_sep(seq, &first);
  667. seq_printf(seq, "preallocmin=%d",
  668. REISERFS_SB(s)->s_alloc_options.preallocmin);
  669. }
  670. if (REISERFS_SB(s)->s_alloc_options.preallocsize != 17) {
  671. print_sep(seq, &first);
  672. seq_printf(seq, "preallocsize=%d",
  673. REISERFS_SB(s)->s_alloc_options.preallocsize);
  674. }
  675. }
  676. static inline void new_hashed_relocation(reiserfs_blocknr_hint_t * hint)
  677. {
  678. char *hash_in;
  679. if (hint->formatted_node) {
  680. hash_in = (char *)&hint->key.k_dir_id;
  681. } else {
  682. if (!hint->inode) {
  683. /*hint->search_start = hint->beg;*/
  684. hash_in = (char *)&hint->key.k_dir_id;
  685. } else
  686. if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
  687. hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
  688. else
  689. hash_in =
  690. (char *)(&INODE_PKEY(hint->inode)->k_objectid);
  691. }
  692. hint->search_start =
  693. hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
  694. }
  695. /*
  696. * Relocation based on dirid, hashing them into a given bitmap block
  697. * files. Formatted nodes are unaffected, a separate policy covers them
  698. */
  699. static void dirid_groups(reiserfs_blocknr_hint_t * hint)
  700. {
  701. unsigned long hash;
  702. __u32 dirid = 0;
  703. int bm = 0;
  704. struct super_block *sb = hint->th->t_super;
  705. if (hint->inode)
  706. dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
  707. else if (hint->formatted_node)
  708. dirid = hint->key.k_dir_id;
  709. if (dirid) {
  710. bm = bmap_hash_id(sb, dirid);
  711. hash = bm * (sb->s_blocksize << 3);
  712. /* give a portion of the block group to metadata */
  713. if (hint->inode)
  714. hash += sb->s_blocksize / 2;
  715. hint->search_start = hash;
  716. }
  717. }
  718. /*
  719. * Relocation based on oid, hashing them into a given bitmap block
  720. * files. Formatted nodes are unaffected, a separate policy covers them
  721. */
  722. static void oid_groups(reiserfs_blocknr_hint_t * hint)
  723. {
  724. if (hint->inode) {
  725. unsigned long hash;
  726. __u32 oid;
  727. __u32 dirid;
  728. int bm;
  729. dirid = le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id);
  730. /*
  731. * keep the root dir and it's first set of subdirs close to
  732. * the start of the disk
  733. */
  734. if (dirid <= 2)
  735. hash = (hint->inode->i_sb->s_blocksize << 3);
  736. else {
  737. oid = le32_to_cpu(INODE_PKEY(hint->inode)->k_objectid);
  738. bm = bmap_hash_id(hint->inode->i_sb, oid);
  739. hash = bm * (hint->inode->i_sb->s_blocksize << 3);
  740. }
  741. hint->search_start = hash;
  742. }
  743. }
  744. /*
  745. * returns 1 if it finds an indirect item and gets valid hint info
  746. * from it, otherwise 0
  747. */
  748. static int get_left_neighbor(reiserfs_blocknr_hint_t * hint)
  749. {
  750. struct treepath *path;
  751. struct buffer_head *bh;
  752. struct item_head *ih;
  753. int pos_in_item;
  754. __le32 *item;
  755. int ret = 0;
  756. /*
  757. * reiserfs code can call this function w/o pointer to path
  758. * structure supplied; then we rely on supplied search_start
  759. */
  760. if (!hint->path)
  761. return 0;
  762. path = hint->path;
  763. bh = get_last_bh(path);
  764. RFALSE(!bh, "green-4002: Illegal path specified to get_left_neighbor");
  765. ih = tp_item_head(path);
  766. pos_in_item = path->pos_in_item;
  767. item = tp_item_body(path);
  768. hint->search_start = bh->b_blocknr;
  769. /*
  770. * for indirect item: go to left and look for the first non-hole entry
  771. * in the indirect item
  772. */
  773. if (!hint->formatted_node && is_indirect_le_ih(ih)) {
  774. if (pos_in_item == I_UNFM_NUM(ih))
  775. pos_in_item--;
  776. while (pos_in_item >= 0) {
  777. int t = get_block_num(item, pos_in_item);
  778. if (t) {
  779. hint->search_start = t;
  780. ret = 1;
  781. break;
  782. }
  783. pos_in_item--;
  784. }
  785. }
  786. /* does result value fit into specified region? */
  787. return ret;
  788. }
  789. /*
  790. * should be, if formatted node, then try to put on first part of the device
  791. * specified as number of percent with mount option device, else try to put
  792. * on last of device. This is not to say it is good code to do so,
  793. * but the effect should be measured.
  794. */
  795. static inline void set_border_in_hint(struct super_block *s,
  796. reiserfs_blocknr_hint_t * hint)
  797. {
  798. b_blocknr_t border =
  799. SB_BLOCK_COUNT(s) / REISERFS_SB(s)->s_alloc_options.border;
  800. if (hint->formatted_node)
  801. hint->end = border - 1;
  802. else
  803. hint->beg = border;
  804. }
  805. static inline void displace_large_file(reiserfs_blocknr_hint_t * hint)
  806. {
  807. if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
  808. hint->search_start =
  809. hint->beg +
  810. keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_dir_id),
  811. 4) % (hint->end - hint->beg);
  812. else
  813. hint->search_start =
  814. hint->beg +
  815. keyed_hash((char *)(&INODE_PKEY(hint->inode)->k_objectid),
  816. 4) % (hint->end - hint->beg);
  817. }
  818. static inline void hash_formatted_node(reiserfs_blocknr_hint_t * hint)
  819. {
  820. char *hash_in;
  821. if (!hint->inode)
  822. hash_in = (char *)&hint->key.k_dir_id;
  823. else if (TEST_OPTION(displace_based_on_dirid, hint->th->t_super))
  824. hash_in = (char *)(&INODE_PKEY(hint->inode)->k_dir_id);
  825. else
  826. hash_in = (char *)(&INODE_PKEY(hint->inode)->k_objectid);
  827. hint->search_start =
  828. hint->beg + keyed_hash(hash_in, 4) % (hint->end - hint->beg);
  829. }
  830. static inline int
  831. this_blocknr_allocation_would_make_it_a_large_file(reiserfs_blocknr_hint_t *
  832. hint)
  833. {
  834. return hint->block ==
  835. REISERFS_SB(hint->th->t_super)->s_alloc_options.large_file_size;
  836. }
  837. #ifdef DISPLACE_NEW_PACKING_LOCALITIES
  838. static inline void displace_new_packing_locality(reiserfs_blocknr_hint_t * hint)
  839. {
  840. struct in_core_key *key = &hint->key;
  841. hint->th->displace_new_blocks = 0;
  842. hint->search_start =
  843. hint->beg + keyed_hash((char *)(&key->k_objectid),
  844. 4) % (hint->end - hint->beg);
  845. }
  846. #endif
  847. static inline int old_hashed_relocation(reiserfs_blocknr_hint_t * hint)
  848. {
  849. b_blocknr_t border;
  850. u32 hash_in;
  851. if (hint->formatted_node || hint->inode == NULL) {
  852. return 0;
  853. }
  854. hash_in = le32_to_cpu((INODE_PKEY(hint->inode))->k_dir_id);
  855. border =
  856. hint->beg + (u32) keyed_hash(((char *)(&hash_in)),
  857. 4) % (hint->end - hint->beg - 1);
  858. if (border > hint->search_start)
  859. hint->search_start = border;
  860. return 1;
  861. }
  862. static inline int old_way(reiserfs_blocknr_hint_t * hint)
  863. {
  864. b_blocknr_t border;
  865. if (hint->formatted_node || hint->inode == NULL) {
  866. return 0;
  867. }
  868. border =
  869. hint->beg +
  870. le32_to_cpu(INODE_PKEY(hint->inode)->k_dir_id) % (hint->end -
  871. hint->beg);
  872. if (border > hint->search_start)
  873. hint->search_start = border;
  874. return 1;
  875. }
  876. static inline void hundredth_slices(reiserfs_blocknr_hint_t * hint)
  877. {
  878. struct in_core_key *key = &hint->key;
  879. b_blocknr_t slice_start;
  880. slice_start =
  881. (keyed_hash((char *)(&key->k_dir_id), 4) % 100) * (hint->end / 100);
  882. if (slice_start > hint->search_start
  883. || slice_start + (hint->end / 100) <= hint->search_start) {
  884. hint->search_start = slice_start;
  885. }
  886. }
  887. static void determine_search_start(reiserfs_blocknr_hint_t * hint,
  888. int amount_needed)
  889. {
  890. struct super_block *s = hint->th->t_super;
  891. int unfm_hint;
  892. hint->beg = 0;
  893. hint->end = SB_BLOCK_COUNT(s) - 1;
  894. /* This is former border algorithm. Now with tunable border offset */
  895. if (concentrating_formatted_nodes(s))
  896. set_border_in_hint(s, hint);
  897. #ifdef DISPLACE_NEW_PACKING_LOCALITIES
  898. /*
  899. * whenever we create a new directory, we displace it. At first
  900. * we will hash for location, later we might look for a moderately
  901. * empty place for it
  902. */
  903. if (displacing_new_packing_localities(s)
  904. && hint->th->displace_new_blocks) {
  905. displace_new_packing_locality(hint);
  906. /*
  907. * we do not continue determine_search_start,
  908. * if new packing locality is being displaced
  909. */
  910. return;
  911. }
  912. #endif
  913. /*
  914. * all persons should feel encouraged to add more special cases
  915. * here and test them
  916. */
  917. if (displacing_large_files(s) && !hint->formatted_node
  918. && this_blocknr_allocation_would_make_it_a_large_file(hint)) {
  919. displace_large_file(hint);
  920. return;
  921. }
  922. /*
  923. * if none of our special cases is relevant, use the left
  924. * neighbor in the tree order of the new node we are allocating for
  925. */
  926. if (hint->formatted_node && TEST_OPTION(hashed_formatted_nodes, s)) {
  927. hash_formatted_node(hint);
  928. return;
  929. }
  930. unfm_hint = get_left_neighbor(hint);
  931. /*
  932. * Mimic old block allocator behaviour, that is if VFS allowed for
  933. * preallocation, new blocks are displaced based on directory ID.
  934. * Also, if suggested search_start is less than last preallocated
  935. * block, we start searching from it, assuming that HDD dataflow
  936. * is faster in forward direction
  937. */
  938. if (TEST_OPTION(old_way, s)) {
  939. if (!hint->formatted_node) {
  940. if (!reiserfs_hashed_relocation(s))
  941. old_way(hint);
  942. else if (!reiserfs_no_unhashed_relocation(s))
  943. old_hashed_relocation(hint);
  944. if (hint->inode
  945. && hint->search_start <
  946. REISERFS_I(hint->inode)->i_prealloc_block)
  947. hint->search_start =
  948. REISERFS_I(hint->inode)->i_prealloc_block;
  949. }
  950. return;
  951. }
  952. /* This is an approach proposed by Hans */
  953. if (TEST_OPTION(hundredth_slices, s)
  954. && !(displacing_large_files(s) && !hint->formatted_node)) {
  955. hundredth_slices(hint);
  956. return;
  957. }
  958. /* old_hashed_relocation only works on unformatted */
  959. if (!unfm_hint && !hint->formatted_node &&
  960. TEST_OPTION(old_hashed_relocation, s)) {
  961. old_hashed_relocation(hint);
  962. }
  963. /* new_hashed_relocation works with both formatted/unformatted nodes */
  964. if ((!unfm_hint || hint->formatted_node) &&
  965. TEST_OPTION(new_hashed_relocation, s)) {
  966. new_hashed_relocation(hint);
  967. }
  968. /* dirid grouping works only on unformatted nodes */
  969. if (!unfm_hint && !hint->formatted_node && TEST_OPTION(dirid_groups, s)) {
  970. dirid_groups(hint);
  971. }
  972. #ifdef DISPLACE_NEW_PACKING_LOCALITIES
  973. if (hint->formatted_node && TEST_OPTION(dirid_groups, s)) {
  974. dirid_groups(hint);
  975. }
  976. #endif
  977. /* oid grouping works only on unformatted nodes */
  978. if (!unfm_hint && !hint->formatted_node && TEST_OPTION(oid_groups, s)) {
  979. oid_groups(hint);
  980. }
  981. return;
  982. }
  983. static int determine_prealloc_size(reiserfs_blocknr_hint_t * hint)
  984. {
  985. /* make minimum size a mount option and benchmark both ways */
  986. /* we preallocate blocks only for regular files, specific size */
  987. /* benchmark preallocating always and see what happens */
  988. hint->prealloc_size = 0;
  989. if (!hint->formatted_node && hint->preallocate) {
  990. if (S_ISREG(hint->inode->i_mode)
  991. && hint->inode->i_size >=
  992. REISERFS_SB(hint->th->t_super)->s_alloc_options.
  993. preallocmin * hint->inode->i_sb->s_blocksize)
  994. hint->prealloc_size =
  995. REISERFS_SB(hint->th->t_super)->s_alloc_options.
  996. preallocsize - 1;
  997. }
  998. return CARRY_ON;
  999. }
  1000. static inline int allocate_without_wrapping_disk(reiserfs_blocknr_hint_t * hint,
  1001. b_blocknr_t * new_blocknrs,
  1002. b_blocknr_t start,
  1003. b_blocknr_t finish, int min,
  1004. int amount_needed,
  1005. int prealloc_size)
  1006. {
  1007. int rest = amount_needed;
  1008. int nr_allocated;
  1009. while (rest > 0 && start <= finish) {
  1010. nr_allocated = scan_bitmap(hint->th, &start, finish, min,
  1011. rest + prealloc_size,
  1012. !hint->formatted_node, hint->block);
  1013. if (nr_allocated == 0) /* no new blocks allocated, return */
  1014. break;
  1015. /* fill free_blocknrs array first */
  1016. while (rest > 0 && nr_allocated > 0) {
  1017. *new_blocknrs++ = start++;
  1018. rest--;
  1019. nr_allocated--;
  1020. }
  1021. /* do we have something to fill prealloc. array also ? */
  1022. if (nr_allocated > 0) {
  1023. /*
  1024. * it means prealloc_size was greater that 0 and
  1025. * we do preallocation
  1026. */
  1027. list_add(&REISERFS_I(hint->inode)->i_prealloc_list,
  1028. &SB_JOURNAL(hint->th->t_super)->
  1029. j_prealloc_list);
  1030. REISERFS_I(hint->inode)->i_prealloc_block = start;
  1031. REISERFS_I(hint->inode)->i_prealloc_count =
  1032. nr_allocated;
  1033. break;
  1034. }
  1035. }
  1036. return (amount_needed - rest);
  1037. }
  1038. static inline int blocknrs_and_prealloc_arrays_from_search_start
  1039. (reiserfs_blocknr_hint_t * hint, b_blocknr_t * new_blocknrs,
  1040. int amount_needed) {
  1041. struct super_block *s = hint->th->t_super;
  1042. b_blocknr_t start = hint->search_start;
  1043. b_blocknr_t finish = SB_BLOCK_COUNT(s) - 1;
  1044. int passno = 0;
  1045. int nr_allocated = 0;
  1046. int depth;
  1047. determine_prealloc_size(hint);
  1048. if (!hint->formatted_node) {
  1049. int quota_ret;
  1050. #ifdef REISERQUOTA_DEBUG
  1051. reiserfs_debug(s, REISERFS_DEBUG_CODE,
  1052. "reiserquota: allocating %d blocks id=%u",
  1053. amount_needed, hint->inode->i_uid);
  1054. #endif
  1055. depth = reiserfs_write_unlock_nested(s);
  1056. quota_ret =
  1057. dquot_alloc_block_nodirty(hint->inode, amount_needed);
  1058. if (quota_ret) { /* Quota exceeded? */
  1059. reiserfs_write_lock_nested(s, depth);
  1060. return QUOTA_EXCEEDED;
  1061. }
  1062. if (hint->preallocate && hint->prealloc_size) {
  1063. #ifdef REISERQUOTA_DEBUG
  1064. reiserfs_debug(s, REISERFS_DEBUG_CODE,
  1065. "reiserquota: allocating (prealloc) %d blocks id=%u",
  1066. hint->prealloc_size, hint->inode->i_uid);
  1067. #endif
  1068. quota_ret = dquot_prealloc_block_nodirty(hint->inode,
  1069. hint->prealloc_size);
  1070. if (quota_ret)
  1071. hint->preallocate = hint->prealloc_size = 0;
  1072. }
  1073. /* for unformatted nodes, force large allocations */
  1074. reiserfs_write_lock_nested(s, depth);
  1075. }
  1076. do {
  1077. switch (passno++) {
  1078. case 0: /* Search from hint->search_start to end of disk */
  1079. start = hint->search_start;
  1080. finish = SB_BLOCK_COUNT(s) - 1;
  1081. break;
  1082. case 1: /* Search from hint->beg to hint->search_start */
  1083. start = hint->beg;
  1084. finish = hint->search_start;
  1085. break;
  1086. case 2: /* Last chance: Search from 0 to hint->beg */
  1087. start = 0;
  1088. finish = hint->beg;
  1089. break;
  1090. default:
  1091. /* We've tried searching everywhere, not enough space */
  1092. /* Free the blocks */
  1093. if (!hint->formatted_node) {
  1094. #ifdef REISERQUOTA_DEBUG
  1095. reiserfs_debug(s, REISERFS_DEBUG_CODE,
  1096. "reiserquota: freeing (nospace) %d blocks id=%u",
  1097. amount_needed +
  1098. hint->prealloc_size -
  1099. nr_allocated,
  1100. hint->inode->i_uid);
  1101. #endif
  1102. /* Free not allocated blocks */
  1103. depth = reiserfs_write_unlock_nested(s);
  1104. dquot_free_block_nodirty(hint->inode,
  1105. amount_needed + hint->prealloc_size -
  1106. nr_allocated);
  1107. reiserfs_write_lock_nested(s, depth);
  1108. }
  1109. while (nr_allocated--)
  1110. reiserfs_free_block(hint->th, hint->inode,
  1111. new_blocknrs[nr_allocated],
  1112. !hint->formatted_node);
  1113. return NO_DISK_SPACE;
  1114. }
  1115. } while ((nr_allocated += allocate_without_wrapping_disk(hint,
  1116. new_blocknrs +
  1117. nr_allocated,
  1118. start, finish,
  1119. 1,
  1120. amount_needed -
  1121. nr_allocated,
  1122. hint->
  1123. prealloc_size))
  1124. < amount_needed);
  1125. if (!hint->formatted_node &&
  1126. amount_needed + hint->prealloc_size >
  1127. nr_allocated + REISERFS_I(hint->inode)->i_prealloc_count) {
  1128. /* Some of preallocation blocks were not allocated */
  1129. #ifdef REISERQUOTA_DEBUG
  1130. reiserfs_debug(s, REISERFS_DEBUG_CODE,
  1131. "reiserquota: freeing (failed prealloc) %d blocks id=%u",
  1132. amount_needed + hint->prealloc_size -
  1133. nr_allocated -
  1134. REISERFS_I(hint->inode)->i_prealloc_count,
  1135. hint->inode->i_uid);
  1136. #endif
  1137. depth = reiserfs_write_unlock_nested(s);
  1138. dquot_free_block_nodirty(hint->inode, amount_needed +
  1139. hint->prealloc_size - nr_allocated -
  1140. REISERFS_I(hint->inode)->
  1141. i_prealloc_count);
  1142. reiserfs_write_lock_nested(s, depth);
  1143. }
  1144. return CARRY_ON;
  1145. }
  1146. /* grab new blocknrs from preallocated list */
  1147. /* return amount still needed after using them */
  1148. static int use_preallocated_list_if_available(reiserfs_blocknr_hint_t * hint,
  1149. b_blocknr_t * new_blocknrs,
  1150. int amount_needed)
  1151. {
  1152. struct inode *inode = hint->inode;
  1153. if (REISERFS_I(inode)->i_prealloc_count > 0) {
  1154. while (amount_needed) {
  1155. *new_blocknrs++ = REISERFS_I(inode)->i_prealloc_block++;
  1156. REISERFS_I(inode)->i_prealloc_count--;
  1157. amount_needed--;
  1158. if (REISERFS_I(inode)->i_prealloc_count <= 0) {
  1159. list_del(&REISERFS_I(inode)->i_prealloc_list);
  1160. break;
  1161. }
  1162. }
  1163. }
  1164. /* return amount still needed after using preallocated blocks */
  1165. return amount_needed;
  1166. }
  1167. int reiserfs_allocate_blocknrs(reiserfs_blocknr_hint_t *hint,
  1168. b_blocknr_t *new_blocknrs,
  1169. int amount_needed,
  1170. /* Amount of blocks we have already reserved */
  1171. int reserved_by_us)
  1172. {
  1173. int initial_amount_needed = amount_needed;
  1174. int ret;
  1175. struct super_block *s = hint->th->t_super;
  1176. /* Check if there is enough space, taking into account reserved space */
  1177. if (SB_FREE_BLOCKS(s) - REISERFS_SB(s)->reserved_blocks <
  1178. amount_needed - reserved_by_us)
  1179. return NO_DISK_SPACE;
  1180. /* should this be if !hint->inode && hint->preallocate? */
  1181. /* do you mean hint->formatted_node can be removed ? - Zam */
  1182. /*
  1183. * hint->formatted_node cannot be removed because we try to access
  1184. * inode information here, and there is often no inode associated with
  1185. * metadata allocations - green
  1186. */
  1187. if (!hint->formatted_node && hint->preallocate) {
  1188. amount_needed = use_preallocated_list_if_available
  1189. (hint, new_blocknrs, amount_needed);
  1190. /*
  1191. * We have all the block numbers we need from the
  1192. * prealloc list
  1193. */
  1194. if (amount_needed == 0)
  1195. return CARRY_ON;
  1196. new_blocknrs += (initial_amount_needed - amount_needed);
  1197. }
  1198. /* find search start and save it in hint structure */
  1199. determine_search_start(hint, amount_needed);
  1200. if (hint->search_start >= SB_BLOCK_COUNT(s))
  1201. hint->search_start = SB_BLOCK_COUNT(s) - 1;
  1202. /* allocation itself; fill new_blocknrs and preallocation arrays */
  1203. ret = blocknrs_and_prealloc_arrays_from_search_start
  1204. (hint, new_blocknrs, amount_needed);
  1205. /*
  1206. * We used prealloc. list to fill (partially) new_blocknrs array.
  1207. * If final allocation fails we need to return blocks back to
  1208. * prealloc. list or just free them. -- Zam (I chose second
  1209. * variant)
  1210. */
  1211. if (ret != CARRY_ON) {
  1212. while (amount_needed++ < initial_amount_needed) {
  1213. reiserfs_free_block(hint->th, hint->inode,
  1214. *(--new_blocknrs), 1);
  1215. }
  1216. }
  1217. return ret;
  1218. }
  1219. void reiserfs_cache_bitmap_metadata(struct super_block *sb,
  1220. struct buffer_head *bh,
  1221. struct reiserfs_bitmap_info *info)
  1222. {
  1223. unsigned long *cur = (unsigned long *)(bh->b_data + bh->b_size);
  1224. /* The first bit must ALWAYS be 1 */
  1225. if (!reiserfs_test_le_bit(0, (unsigned long *)bh->b_data))
  1226. reiserfs_error(sb, "reiserfs-2025", "bitmap block %lu is "
  1227. "corrupted: first bit must be 1", bh->b_blocknr);
  1228. info->free_count = 0;
  1229. while (--cur >= (unsigned long *)bh->b_data) {
  1230. /* 0 and ~0 are special, we can optimize for them */
  1231. if (*cur == 0)
  1232. info->free_count += BITS_PER_LONG;
  1233. else if (*cur != ~0L) /* A mix, investigate */
  1234. info->free_count += BITS_PER_LONG - hweight_long(*cur);
  1235. }
  1236. }
  1237. struct buffer_head *reiserfs_read_bitmap_block(struct super_block *sb,
  1238. unsigned int bitmap)
  1239. {
  1240. b_blocknr_t block = (sb->s_blocksize << 3) * bitmap;
  1241. struct reiserfs_bitmap_info *info = SB_AP_BITMAP(sb) + bitmap;
  1242. struct buffer_head *bh;
  1243. /*
  1244. * Way old format filesystems had the bitmaps packed up front.
  1245. * I doubt there are any of these left, but just in case...
  1246. */
  1247. if (unlikely(test_bit(REISERFS_OLD_FORMAT,
  1248. &REISERFS_SB(sb)->s_properties)))
  1249. block = REISERFS_SB(sb)->s_sbh->b_blocknr + 1 + bitmap;
  1250. else if (bitmap == 0)
  1251. block = (REISERFS_DISK_OFFSET_IN_BYTES >> sb->s_blocksize_bits) + 1;
  1252. bh = sb_bread(sb, block);
  1253. if (bh == NULL)
  1254. reiserfs_warning(sb, "sh-2029: %s: bitmap block (#%u) "
  1255. "reading failed", __func__, block);
  1256. else {
  1257. if (buffer_locked(bh)) {
  1258. int depth;
  1259. PROC_INFO_INC(sb, scan_bitmap.wait);
  1260. depth = reiserfs_write_unlock_nested(sb);
  1261. __wait_on_buffer(bh);
  1262. reiserfs_write_lock_nested(sb, depth);
  1263. }
  1264. BUG_ON(!buffer_uptodate(bh));
  1265. BUG_ON(atomic_read(&bh->b_count) == 0);
  1266. if (info->free_count == UINT_MAX)
  1267. reiserfs_cache_bitmap_metadata(sb, bh, info);
  1268. }
  1269. return bh;
  1270. }
  1271. int reiserfs_init_bitmap_cache(struct super_block *sb)
  1272. {
  1273. struct reiserfs_bitmap_info *bitmap;
  1274. unsigned int bmap_nr = reiserfs_bmap_count(sb);
  1275. bitmap = vmalloc(sizeof(*bitmap) * bmap_nr);
  1276. if (bitmap == NULL)
  1277. return -ENOMEM;
  1278. memset(bitmap, 0xff, sizeof(*bitmap) * bmap_nr);
  1279. SB_AP_BITMAP(sb) = bitmap;
  1280. return 0;
  1281. }
  1282. void reiserfs_free_bitmap_cache(struct super_block *sb)
  1283. {
  1284. if (SB_AP_BITMAP(sb)) {
  1285. vfree(SB_AP_BITMAP(sb));
  1286. SB_AP_BITMAP(sb) = NULL;
  1287. }
  1288. }