balloc.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157
  1. /*
  2. * linux/fs/ext3/balloc.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * Enhanced block allocation by Stephen Tweedie (sct@redhat.com), 1993
  10. * Big-endian to little-endian byte-swapping/bitmaps by
  11. * David S. Miller (davem@caip.rutgers.edu), 1995
  12. */
  13. #include <linux/time.h>
  14. #include <linux/capability.h>
  15. #include <linux/fs.h>
  16. #include <linux/slab.h>
  17. #include <linux/jbd.h>
  18. #include <linux/ext3_fs.h>
  19. #include <linux/ext3_jbd.h>
  20. #include <linux/quotaops.h>
  21. #include <linux/buffer_head.h>
  22. #include <linux/blkdev.h>
  23. /*
  24. * balloc.c contains the blocks allocation and deallocation routines
  25. */
  26. /*
  27. * The free blocks are managed by bitmaps. A file system contains several
  28. * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
  29. * block for inodes, N blocks for the inode table and data blocks.
  30. *
  31. * The file system contains group descriptors which are located after the
  32. * super block. Each descriptor contains the number of the bitmap block and
  33. * the free blocks count in the block. The descriptors are loaded in memory
  34. * when a file system is mounted (see ext3_fill_super).
  35. */
  36. #define in_range(b, first, len) ((b) >= (first) && (b) <= (first) + (len) - 1)
  37. /*
  38. * Calculate the block group number and offset, given a block number
  39. */
  40. static void ext3_get_group_no_and_offset(struct super_block *sb,
  41. ext3_fsblk_t blocknr, unsigned long *blockgrpp, ext3_grpblk_t *offsetp)
  42. {
  43. struct ext3_super_block *es = EXT3_SB(sb)->s_es;
  44. blocknr = blocknr - le32_to_cpu(es->s_first_data_block);
  45. if (offsetp)
  46. *offsetp = blocknr % EXT3_BLOCKS_PER_GROUP(sb);
  47. if (blockgrpp)
  48. *blockgrpp = blocknr / EXT3_BLOCKS_PER_GROUP(sb);
  49. }
  50. /**
  51. * ext3_get_group_desc() -- load group descriptor from disk
  52. * @sb: super block
  53. * @block_group: given block group
  54. * @bh: pointer to the buffer head to store the block
  55. * group descriptor
  56. */
  57. struct ext3_group_desc * ext3_get_group_desc(struct super_block * sb,
  58. unsigned int block_group,
  59. struct buffer_head ** bh)
  60. {
  61. unsigned long group_desc;
  62. unsigned long offset;
  63. struct ext3_group_desc * desc;
  64. struct ext3_sb_info *sbi = EXT3_SB(sb);
  65. if (block_group >= sbi->s_groups_count) {
  66. ext3_error (sb, "ext3_get_group_desc",
  67. "block_group >= groups_count - "
  68. "block_group = %d, groups_count = %lu",
  69. block_group, sbi->s_groups_count);
  70. return NULL;
  71. }
  72. smp_rmb();
  73. group_desc = block_group >> EXT3_DESC_PER_BLOCK_BITS(sb);
  74. offset = block_group & (EXT3_DESC_PER_BLOCK(sb) - 1);
  75. if (!sbi->s_group_desc[group_desc]) {
  76. ext3_error (sb, "ext3_get_group_desc",
  77. "Group descriptor not loaded - "
  78. "block_group = %d, group_desc = %lu, desc = %lu",
  79. block_group, group_desc, offset);
  80. return NULL;
  81. }
  82. desc = (struct ext3_group_desc *) sbi->s_group_desc[group_desc]->b_data;
  83. if (bh)
  84. *bh = sbi->s_group_desc[group_desc];
  85. return desc + offset;
  86. }
  87. static int ext3_valid_block_bitmap(struct super_block *sb,
  88. struct ext3_group_desc *desc,
  89. unsigned int block_group,
  90. struct buffer_head *bh)
  91. {
  92. ext3_grpblk_t offset;
  93. ext3_grpblk_t next_zero_bit;
  94. ext3_fsblk_t bitmap_blk;
  95. ext3_fsblk_t group_first_block;
  96. group_first_block = ext3_group_first_block_no(sb, block_group);
  97. /* check whether block bitmap block number is set */
  98. bitmap_blk = le32_to_cpu(desc->bg_block_bitmap);
  99. offset = bitmap_blk - group_first_block;
  100. if (!ext3_test_bit(offset, bh->b_data))
  101. /* bad block bitmap */
  102. goto err_out;
  103. /* check whether the inode bitmap block number is set */
  104. bitmap_blk = le32_to_cpu(desc->bg_inode_bitmap);
  105. offset = bitmap_blk - group_first_block;
  106. if (!ext3_test_bit(offset, bh->b_data))
  107. /* bad block bitmap */
  108. goto err_out;
  109. /* check whether the inode table block number is set */
  110. bitmap_blk = le32_to_cpu(desc->bg_inode_table);
  111. offset = bitmap_blk - group_first_block;
  112. next_zero_bit = ext3_find_next_zero_bit(bh->b_data,
  113. offset + EXT3_SB(sb)->s_itb_per_group,
  114. offset);
  115. if (next_zero_bit >= offset + EXT3_SB(sb)->s_itb_per_group)
  116. /* good bitmap for inode tables */
  117. return 1;
  118. err_out:
  119. ext3_error(sb, __func__,
  120. "Invalid block bitmap - "
  121. "block_group = %d, block = %lu",
  122. block_group, bitmap_blk);
  123. return 0;
  124. }
  125. /**
  126. * read_block_bitmap()
  127. * @sb: super block
  128. * @block_group: given block group
  129. *
  130. * Read the bitmap for a given block_group,and validate the
  131. * bits for block/inode/inode tables are set in the bitmaps
  132. *
  133. * Return buffer_head on success or NULL in case of failure.
  134. */
  135. static struct buffer_head *
  136. read_block_bitmap(struct super_block *sb, unsigned int block_group)
  137. {
  138. struct ext3_group_desc * desc;
  139. struct buffer_head * bh = NULL;
  140. ext3_fsblk_t bitmap_blk;
  141. desc = ext3_get_group_desc(sb, block_group, NULL);
  142. if (!desc)
  143. return NULL;
  144. bitmap_blk = le32_to_cpu(desc->bg_block_bitmap);
  145. bh = sb_getblk(sb, bitmap_blk);
  146. if (unlikely(!bh)) {
  147. ext3_error(sb, __func__,
  148. "Cannot read block bitmap - "
  149. "block_group = %d, block_bitmap = %u",
  150. block_group, le32_to_cpu(desc->bg_block_bitmap));
  151. return NULL;
  152. }
  153. if (likely(bh_uptodate_or_lock(bh)))
  154. return bh;
  155. if (bh_submit_read(bh) < 0) {
  156. brelse(bh);
  157. ext3_error(sb, __func__,
  158. "Cannot read block bitmap - "
  159. "block_group = %d, block_bitmap = %u",
  160. block_group, le32_to_cpu(desc->bg_block_bitmap));
  161. return NULL;
  162. }
  163. ext3_valid_block_bitmap(sb, desc, block_group, bh);
  164. /*
  165. * file system mounted not to panic on error, continue with corrupt
  166. * bitmap
  167. */
  168. return bh;
  169. }
  170. /*
  171. * The reservation window structure operations
  172. * --------------------------------------------
  173. * Operations include:
  174. * dump, find, add, remove, is_empty, find_next_reservable_window, etc.
  175. *
  176. * We use a red-black tree to represent per-filesystem reservation
  177. * windows.
  178. *
  179. */
  180. /**
  181. * __rsv_window_dump() -- Dump the filesystem block allocation reservation map
  182. * @rb_root: root of per-filesystem reservation rb tree
  183. * @verbose: verbose mode
  184. * @fn: function which wishes to dump the reservation map
  185. *
  186. * If verbose is turned on, it will print the whole block reservation
  187. * windows(start, end). Otherwise, it will only print out the "bad" windows,
  188. * those windows that overlap with their immediate neighbors.
  189. */
  190. #if 1
  191. static void __rsv_window_dump(struct rb_root *root, int verbose,
  192. const char *fn)
  193. {
  194. struct rb_node *n;
  195. struct ext3_reserve_window_node *rsv, *prev;
  196. int bad;
  197. restart:
  198. n = rb_first(root);
  199. bad = 0;
  200. prev = NULL;
  201. printk("Block Allocation Reservation Windows Map (%s):\n", fn);
  202. while (n) {
  203. rsv = rb_entry(n, struct ext3_reserve_window_node, rsv_node);
  204. if (verbose)
  205. printk("reservation window 0x%p "
  206. "start: %lu, end: %lu\n",
  207. rsv, rsv->rsv_start, rsv->rsv_end);
  208. if (rsv->rsv_start && rsv->rsv_start >= rsv->rsv_end) {
  209. printk("Bad reservation %p (start >= end)\n",
  210. rsv);
  211. bad = 1;
  212. }
  213. if (prev && prev->rsv_end >= rsv->rsv_start) {
  214. printk("Bad reservation %p (prev->end >= start)\n",
  215. rsv);
  216. bad = 1;
  217. }
  218. if (bad) {
  219. if (!verbose) {
  220. printk("Restarting reservation walk in verbose mode\n");
  221. verbose = 1;
  222. goto restart;
  223. }
  224. }
  225. n = rb_next(n);
  226. prev = rsv;
  227. }
  228. printk("Window map complete.\n");
  229. BUG_ON(bad);
  230. }
  231. #define rsv_window_dump(root, verbose) \
  232. __rsv_window_dump((root), (verbose), __func__)
  233. #else
  234. #define rsv_window_dump(root, verbose) do {} while (0)
  235. #endif
  236. /**
  237. * goal_in_my_reservation()
  238. * @rsv: inode's reservation window
  239. * @grp_goal: given goal block relative to the allocation block group
  240. * @group: the current allocation block group
  241. * @sb: filesystem super block
  242. *
  243. * Test if the given goal block (group relative) is within the file's
  244. * own block reservation window range.
  245. *
  246. * If the reservation window is outside the goal allocation group, return 0;
  247. * grp_goal (given goal block) could be -1, which means no specific
  248. * goal block. In this case, always return 1.
  249. * If the goal block is within the reservation window, return 1;
  250. * otherwise, return 0;
  251. */
  252. static int
  253. goal_in_my_reservation(struct ext3_reserve_window *rsv, ext3_grpblk_t grp_goal,
  254. unsigned int group, struct super_block * sb)
  255. {
  256. ext3_fsblk_t group_first_block, group_last_block;
  257. group_first_block = ext3_group_first_block_no(sb, group);
  258. group_last_block = group_first_block + (EXT3_BLOCKS_PER_GROUP(sb) - 1);
  259. if ((rsv->_rsv_start > group_last_block) ||
  260. (rsv->_rsv_end < group_first_block))
  261. return 0;
  262. if ((grp_goal >= 0) && ((grp_goal + group_first_block < rsv->_rsv_start)
  263. || (grp_goal + group_first_block > rsv->_rsv_end)))
  264. return 0;
  265. return 1;
  266. }
  267. /**
  268. * search_reserve_window()
  269. * @rb_root: root of reservation tree
  270. * @goal: target allocation block
  271. *
  272. * Find the reserved window which includes the goal, or the previous one
  273. * if the goal is not in any window.
  274. * Returns NULL if there are no windows or if all windows start after the goal.
  275. */
  276. static struct ext3_reserve_window_node *
  277. search_reserve_window(struct rb_root *root, ext3_fsblk_t goal)
  278. {
  279. struct rb_node *n = root->rb_node;
  280. struct ext3_reserve_window_node *rsv;
  281. if (!n)
  282. return NULL;
  283. do {
  284. rsv = rb_entry(n, struct ext3_reserve_window_node, rsv_node);
  285. if (goal < rsv->rsv_start)
  286. n = n->rb_left;
  287. else if (goal > rsv->rsv_end)
  288. n = n->rb_right;
  289. else
  290. return rsv;
  291. } while (n);
  292. /*
  293. * We've fallen off the end of the tree: the goal wasn't inside
  294. * any particular node. OK, the previous node must be to one
  295. * side of the interval containing the goal. If it's the RHS,
  296. * we need to back up one.
  297. */
  298. if (rsv->rsv_start > goal) {
  299. n = rb_prev(&rsv->rsv_node);
  300. rsv = rb_entry(n, struct ext3_reserve_window_node, rsv_node);
  301. }
  302. return rsv;
  303. }
  304. /**
  305. * ext3_rsv_window_add() -- Insert a window to the block reservation rb tree.
  306. * @sb: super block
  307. * @rsv: reservation window to add
  308. *
  309. * Must be called with rsv_lock hold.
  310. */
  311. void ext3_rsv_window_add(struct super_block *sb,
  312. struct ext3_reserve_window_node *rsv)
  313. {
  314. struct rb_root *root = &EXT3_SB(sb)->s_rsv_window_root;
  315. struct rb_node *node = &rsv->rsv_node;
  316. ext3_fsblk_t start = rsv->rsv_start;
  317. struct rb_node ** p = &root->rb_node;
  318. struct rb_node * parent = NULL;
  319. struct ext3_reserve_window_node *this;
  320. while (*p)
  321. {
  322. parent = *p;
  323. this = rb_entry(parent, struct ext3_reserve_window_node, rsv_node);
  324. if (start < this->rsv_start)
  325. p = &(*p)->rb_left;
  326. else if (start > this->rsv_end)
  327. p = &(*p)->rb_right;
  328. else {
  329. rsv_window_dump(root, 1);
  330. BUG();
  331. }
  332. }
  333. rb_link_node(node, parent, p);
  334. rb_insert_color(node, root);
  335. }
  336. /**
  337. * ext3_rsv_window_remove() -- unlink a window from the reservation rb tree
  338. * @sb: super block
  339. * @rsv: reservation window to remove
  340. *
  341. * Mark the block reservation window as not allocated, and unlink it
  342. * from the filesystem reservation window rb tree. Must be called with
  343. * rsv_lock hold.
  344. */
  345. static void rsv_window_remove(struct super_block *sb,
  346. struct ext3_reserve_window_node *rsv)
  347. {
  348. rsv->rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  349. rsv->rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  350. rsv->rsv_alloc_hit = 0;
  351. rb_erase(&rsv->rsv_node, &EXT3_SB(sb)->s_rsv_window_root);
  352. }
  353. /*
  354. * rsv_is_empty() -- Check if the reservation window is allocated.
  355. * @rsv: given reservation window to check
  356. *
  357. * returns 1 if the end block is EXT3_RESERVE_WINDOW_NOT_ALLOCATED.
  358. */
  359. static inline int rsv_is_empty(struct ext3_reserve_window *rsv)
  360. {
  361. /* a valid reservation end block could not be 0 */
  362. return rsv->_rsv_end == EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  363. }
  364. /**
  365. * ext3_init_block_alloc_info()
  366. * @inode: file inode structure
  367. *
  368. * Allocate and initialize the reservation window structure, and
  369. * link the window to the ext3 inode structure at last
  370. *
  371. * The reservation window structure is only dynamically allocated
  372. * and linked to ext3 inode the first time the open file
  373. * needs a new block. So, before every ext3_new_block(s) call, for
  374. * regular files, we should check whether the reservation window
  375. * structure exists or not. In the latter case, this function is called.
  376. * Fail to do so will result in block reservation being turned off for that
  377. * open file.
  378. *
  379. * This function is called from ext3_get_blocks_handle(), also called
  380. * when setting the reservation window size through ioctl before the file
  381. * is open for write (needs block allocation).
  382. *
  383. * Needs truncate_mutex protection prior to call this function.
  384. */
  385. void ext3_init_block_alloc_info(struct inode *inode)
  386. {
  387. struct ext3_inode_info *ei = EXT3_I(inode);
  388. struct ext3_block_alloc_info *block_i = ei->i_block_alloc_info;
  389. struct super_block *sb = inode->i_sb;
  390. block_i = kmalloc(sizeof(*block_i), GFP_NOFS);
  391. if (block_i) {
  392. struct ext3_reserve_window_node *rsv = &block_i->rsv_window_node;
  393. rsv->rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  394. rsv->rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  395. /*
  396. * if filesystem is mounted with NORESERVATION, the goal
  397. * reservation window size is set to zero to indicate
  398. * block reservation is off
  399. */
  400. if (!test_opt(sb, RESERVATION))
  401. rsv->rsv_goal_size = 0;
  402. else
  403. rsv->rsv_goal_size = EXT3_DEFAULT_RESERVE_BLOCKS;
  404. rsv->rsv_alloc_hit = 0;
  405. block_i->last_alloc_logical_block = 0;
  406. block_i->last_alloc_physical_block = 0;
  407. }
  408. ei->i_block_alloc_info = block_i;
  409. }
  410. /**
  411. * ext3_discard_reservation()
  412. * @inode: inode
  413. *
  414. * Discard(free) block reservation window on last file close, or truncate
  415. * or at last iput().
  416. *
  417. * It is being called in three cases:
  418. * ext3_release_file(): last writer close the file
  419. * ext3_clear_inode(): last iput(), when nobody link to this file.
  420. * ext3_truncate(): when the block indirect map is about to change.
  421. *
  422. */
  423. void ext3_discard_reservation(struct inode *inode)
  424. {
  425. struct ext3_inode_info *ei = EXT3_I(inode);
  426. struct ext3_block_alloc_info *block_i = ei->i_block_alloc_info;
  427. struct ext3_reserve_window_node *rsv;
  428. spinlock_t *rsv_lock = &EXT3_SB(inode->i_sb)->s_rsv_window_lock;
  429. if (!block_i)
  430. return;
  431. rsv = &block_i->rsv_window_node;
  432. if (!rsv_is_empty(&rsv->rsv_window)) {
  433. spin_lock(rsv_lock);
  434. if (!rsv_is_empty(&rsv->rsv_window))
  435. rsv_window_remove(inode->i_sb, rsv);
  436. spin_unlock(rsv_lock);
  437. }
  438. }
  439. /**
  440. * ext3_free_blocks_sb() -- Free given blocks and update quota
  441. * @handle: handle to this transaction
  442. * @sb: super block
  443. * @block: start physcial block to free
  444. * @count: number of blocks to free
  445. * @pdquot_freed_blocks: pointer to quota
  446. */
  447. void ext3_free_blocks_sb(handle_t *handle, struct super_block *sb,
  448. ext3_fsblk_t block, unsigned long count,
  449. unsigned long *pdquot_freed_blocks)
  450. {
  451. struct buffer_head *bitmap_bh = NULL;
  452. struct buffer_head *gd_bh;
  453. unsigned long block_group;
  454. ext3_grpblk_t bit;
  455. unsigned long i;
  456. unsigned long overflow;
  457. struct ext3_group_desc * desc;
  458. struct ext3_super_block * es;
  459. struct ext3_sb_info *sbi;
  460. int err = 0, ret;
  461. ext3_grpblk_t group_freed;
  462. *pdquot_freed_blocks = 0;
  463. sbi = EXT3_SB(sb);
  464. es = sbi->s_es;
  465. if (block < le32_to_cpu(es->s_first_data_block) ||
  466. block + count < block ||
  467. block + count > le32_to_cpu(es->s_blocks_count)) {
  468. ext3_error (sb, "ext3_free_blocks",
  469. "Freeing blocks not in datazone - "
  470. "block = "E3FSBLK", count = %lu", block, count);
  471. goto error_return;
  472. }
  473. ext3_debug ("freeing block(s) %lu-%lu\n", block, block + count - 1);
  474. do_more:
  475. overflow = 0;
  476. block_group = (block - le32_to_cpu(es->s_first_data_block)) /
  477. EXT3_BLOCKS_PER_GROUP(sb);
  478. bit = (block - le32_to_cpu(es->s_first_data_block)) %
  479. EXT3_BLOCKS_PER_GROUP(sb);
  480. /*
  481. * Check to see if we are freeing blocks across a group
  482. * boundary.
  483. */
  484. if (bit + count > EXT3_BLOCKS_PER_GROUP(sb)) {
  485. overflow = bit + count - EXT3_BLOCKS_PER_GROUP(sb);
  486. count -= overflow;
  487. }
  488. brelse(bitmap_bh);
  489. bitmap_bh = read_block_bitmap(sb, block_group);
  490. if (!bitmap_bh)
  491. goto error_return;
  492. desc = ext3_get_group_desc (sb, block_group, &gd_bh);
  493. if (!desc)
  494. goto error_return;
  495. if (in_range (le32_to_cpu(desc->bg_block_bitmap), block, count) ||
  496. in_range (le32_to_cpu(desc->bg_inode_bitmap), block, count) ||
  497. in_range (block, le32_to_cpu(desc->bg_inode_table),
  498. sbi->s_itb_per_group) ||
  499. in_range (block + count - 1, le32_to_cpu(desc->bg_inode_table),
  500. sbi->s_itb_per_group)) {
  501. ext3_error (sb, "ext3_free_blocks",
  502. "Freeing blocks in system zones - "
  503. "Block = "E3FSBLK", count = %lu",
  504. block, count);
  505. goto error_return;
  506. }
  507. /*
  508. * We are about to start releasing blocks in the bitmap,
  509. * so we need undo access.
  510. */
  511. /* @@@ check errors */
  512. BUFFER_TRACE(bitmap_bh, "getting undo access");
  513. err = ext3_journal_get_undo_access(handle, bitmap_bh);
  514. if (err)
  515. goto error_return;
  516. /*
  517. * We are about to modify some metadata. Call the journal APIs
  518. * to unshare ->b_data if a currently-committing transaction is
  519. * using it
  520. */
  521. BUFFER_TRACE(gd_bh, "get_write_access");
  522. err = ext3_journal_get_write_access(handle, gd_bh);
  523. if (err)
  524. goto error_return;
  525. jbd_lock_bh_state(bitmap_bh);
  526. for (i = 0, group_freed = 0; i < count; i++) {
  527. /*
  528. * An HJ special. This is expensive...
  529. */
  530. #ifdef CONFIG_JBD_DEBUG
  531. jbd_unlock_bh_state(bitmap_bh);
  532. {
  533. struct buffer_head *debug_bh;
  534. debug_bh = sb_find_get_block(sb, block + i);
  535. if (debug_bh) {
  536. BUFFER_TRACE(debug_bh, "Deleted!");
  537. if (!bh2jh(bitmap_bh)->b_committed_data)
  538. BUFFER_TRACE(debug_bh,
  539. "No committed data in bitmap");
  540. BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap");
  541. __brelse(debug_bh);
  542. }
  543. }
  544. jbd_lock_bh_state(bitmap_bh);
  545. #endif
  546. if (need_resched()) {
  547. jbd_unlock_bh_state(bitmap_bh);
  548. cond_resched();
  549. jbd_lock_bh_state(bitmap_bh);
  550. }
  551. /* @@@ This prevents newly-allocated data from being
  552. * freed and then reallocated within the same
  553. * transaction.
  554. *
  555. * Ideally we would want to allow that to happen, but to
  556. * do so requires making journal_forget() capable of
  557. * revoking the queued write of a data block, which
  558. * implies blocking on the journal lock. *forget()
  559. * cannot block due to truncate races.
  560. *
  561. * Eventually we can fix this by making journal_forget()
  562. * return a status indicating whether or not it was able
  563. * to revoke the buffer. On successful revoke, it is
  564. * safe not to set the allocation bit in the committed
  565. * bitmap, because we know that there is no outstanding
  566. * activity on the buffer any more and so it is safe to
  567. * reallocate it.
  568. */
  569. BUFFER_TRACE(bitmap_bh, "set in b_committed_data");
  570. J_ASSERT_BH(bitmap_bh,
  571. bh2jh(bitmap_bh)->b_committed_data != NULL);
  572. ext3_set_bit_atomic(sb_bgl_lock(sbi, block_group), bit + i,
  573. bh2jh(bitmap_bh)->b_committed_data);
  574. /*
  575. * We clear the bit in the bitmap after setting the committed
  576. * data bit, because this is the reverse order to that which
  577. * the allocator uses.
  578. */
  579. BUFFER_TRACE(bitmap_bh, "clear bit");
  580. if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, block_group),
  581. bit + i, bitmap_bh->b_data)) {
  582. jbd_unlock_bh_state(bitmap_bh);
  583. ext3_error(sb, __func__,
  584. "bit already cleared for block "E3FSBLK,
  585. block + i);
  586. jbd_lock_bh_state(bitmap_bh);
  587. BUFFER_TRACE(bitmap_bh, "bit already cleared");
  588. } else {
  589. group_freed++;
  590. }
  591. }
  592. jbd_unlock_bh_state(bitmap_bh);
  593. spin_lock(sb_bgl_lock(sbi, block_group));
  594. le16_add_cpu(&desc->bg_free_blocks_count, group_freed);
  595. spin_unlock(sb_bgl_lock(sbi, block_group));
  596. percpu_counter_add(&sbi->s_freeblocks_counter, count);
  597. /* We dirtied the bitmap block */
  598. BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
  599. err = ext3_journal_dirty_metadata(handle, bitmap_bh);
  600. /* And the group descriptor block */
  601. BUFFER_TRACE(gd_bh, "dirtied group descriptor block");
  602. ret = ext3_journal_dirty_metadata(handle, gd_bh);
  603. if (!err) err = ret;
  604. *pdquot_freed_blocks += group_freed;
  605. if (overflow && !err) {
  606. block += count;
  607. count = overflow;
  608. goto do_more;
  609. }
  610. error_return:
  611. brelse(bitmap_bh);
  612. ext3_std_error(sb, err);
  613. return;
  614. }
  615. /**
  616. * ext3_free_blocks() -- Free given blocks and update quota
  617. * @handle: handle for this transaction
  618. * @inode: inode
  619. * @block: start physical block to free
  620. * @count: number of blocks to count
  621. */
  622. void ext3_free_blocks(handle_t *handle, struct inode *inode,
  623. ext3_fsblk_t block, unsigned long count)
  624. {
  625. struct super_block * sb;
  626. unsigned long dquot_freed_blocks;
  627. sb = inode->i_sb;
  628. if (!sb) {
  629. printk ("ext3_free_blocks: nonexistent device");
  630. return;
  631. }
  632. ext3_free_blocks_sb(handle, sb, block, count, &dquot_freed_blocks);
  633. if (dquot_freed_blocks)
  634. dquot_free_block(inode, dquot_freed_blocks);
  635. return;
  636. }
  637. /**
  638. * ext3_test_allocatable()
  639. * @nr: given allocation block group
  640. * @bh: bufferhead contains the bitmap of the given block group
  641. *
  642. * For ext3 allocations, we must not reuse any blocks which are
  643. * allocated in the bitmap buffer's "last committed data" copy. This
  644. * prevents deletes from freeing up the page for reuse until we have
  645. * committed the delete transaction.
  646. *
  647. * If we didn't do this, then deleting something and reallocating it as
  648. * data would allow the old block to be overwritten before the
  649. * transaction committed (because we force data to disk before commit).
  650. * This would lead to corruption if we crashed between overwriting the
  651. * data and committing the delete.
  652. *
  653. * @@@ We may want to make this allocation behaviour conditional on
  654. * data-writes at some point, and disable it for metadata allocations or
  655. * sync-data inodes.
  656. */
  657. static int ext3_test_allocatable(ext3_grpblk_t nr, struct buffer_head *bh)
  658. {
  659. int ret;
  660. struct journal_head *jh = bh2jh(bh);
  661. if (ext3_test_bit(nr, bh->b_data))
  662. return 0;
  663. jbd_lock_bh_state(bh);
  664. if (!jh->b_committed_data)
  665. ret = 1;
  666. else
  667. ret = !ext3_test_bit(nr, jh->b_committed_data);
  668. jbd_unlock_bh_state(bh);
  669. return ret;
  670. }
  671. /**
  672. * bitmap_search_next_usable_block()
  673. * @start: the starting block (group relative) of the search
  674. * @bh: bufferhead contains the block group bitmap
  675. * @maxblocks: the ending block (group relative) of the reservation
  676. *
  677. * The bitmap search --- search forward alternately through the actual
  678. * bitmap on disk and the last-committed copy in journal, until we find a
  679. * bit free in both bitmaps.
  680. */
  681. static ext3_grpblk_t
  682. bitmap_search_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh,
  683. ext3_grpblk_t maxblocks)
  684. {
  685. ext3_grpblk_t next;
  686. struct journal_head *jh = bh2jh(bh);
  687. while (start < maxblocks) {
  688. next = ext3_find_next_zero_bit(bh->b_data, maxblocks, start);
  689. if (next >= maxblocks)
  690. return -1;
  691. if (ext3_test_allocatable(next, bh))
  692. return next;
  693. jbd_lock_bh_state(bh);
  694. if (jh->b_committed_data)
  695. start = ext3_find_next_zero_bit(jh->b_committed_data,
  696. maxblocks, next);
  697. jbd_unlock_bh_state(bh);
  698. }
  699. return -1;
  700. }
  701. /**
  702. * find_next_usable_block()
  703. * @start: the starting block (group relative) to find next
  704. * allocatable block in bitmap.
  705. * @bh: bufferhead contains the block group bitmap
  706. * @maxblocks: the ending block (group relative) for the search
  707. *
  708. * Find an allocatable block in a bitmap. We honor both the bitmap and
  709. * its last-committed copy (if that exists), and perform the "most
  710. * appropriate allocation" algorithm of looking for a free block near
  711. * the initial goal; then for a free byte somewhere in the bitmap; then
  712. * for any free bit in the bitmap.
  713. */
  714. static ext3_grpblk_t
  715. find_next_usable_block(ext3_grpblk_t start, struct buffer_head *bh,
  716. ext3_grpblk_t maxblocks)
  717. {
  718. ext3_grpblk_t here, next;
  719. char *p, *r;
  720. if (start > 0) {
  721. /*
  722. * The goal was occupied; search forward for a free
  723. * block within the next XX blocks.
  724. *
  725. * end_goal is more or less random, but it has to be
  726. * less than EXT3_BLOCKS_PER_GROUP. Aligning up to the
  727. * next 64-bit boundary is simple..
  728. */
  729. ext3_grpblk_t end_goal = (start + 63) & ~63;
  730. if (end_goal > maxblocks)
  731. end_goal = maxblocks;
  732. here = ext3_find_next_zero_bit(bh->b_data, end_goal, start);
  733. if (here < end_goal && ext3_test_allocatable(here, bh))
  734. return here;
  735. ext3_debug("Bit not found near goal\n");
  736. }
  737. here = start;
  738. if (here < 0)
  739. here = 0;
  740. p = bh->b_data + (here >> 3);
  741. r = memscan(p, 0, ((maxblocks + 7) >> 3) - (here >> 3));
  742. next = (r - bh->b_data) << 3;
  743. if (next < maxblocks && next >= start && ext3_test_allocatable(next, bh))
  744. return next;
  745. /*
  746. * The bitmap search --- search forward alternately through the actual
  747. * bitmap and the last-committed copy until we find a bit free in
  748. * both
  749. */
  750. here = bitmap_search_next_usable_block(here, bh, maxblocks);
  751. return here;
  752. }
  753. /**
  754. * claim_block()
  755. * @lock: the spin lock for this block group
  756. * @block: the free block (group relative) to allocate
  757. * @bh: the buffer_head contains the block group bitmap
  758. *
  759. * We think we can allocate this block in this bitmap. Try to set the bit.
  760. * If that succeeds then check that nobody has allocated and then freed the
  761. * block since we saw that is was not marked in b_committed_data. If it _was_
  762. * allocated and freed then clear the bit in the bitmap again and return
  763. * zero (failure).
  764. */
  765. static inline int
  766. claim_block(spinlock_t *lock, ext3_grpblk_t block, struct buffer_head *bh)
  767. {
  768. struct journal_head *jh = bh2jh(bh);
  769. int ret;
  770. if (ext3_set_bit_atomic(lock, block, bh->b_data))
  771. return 0;
  772. jbd_lock_bh_state(bh);
  773. if (jh->b_committed_data && ext3_test_bit(block,jh->b_committed_data)) {
  774. ext3_clear_bit_atomic(lock, block, bh->b_data);
  775. ret = 0;
  776. } else {
  777. ret = 1;
  778. }
  779. jbd_unlock_bh_state(bh);
  780. return ret;
  781. }
  782. /**
  783. * ext3_try_to_allocate()
  784. * @sb: superblock
  785. * @handle: handle to this transaction
  786. * @group: given allocation block group
  787. * @bitmap_bh: bufferhead holds the block bitmap
  788. * @grp_goal: given target block within the group
  789. * @count: target number of blocks to allocate
  790. * @my_rsv: reservation window
  791. *
  792. * Attempt to allocate blocks within a give range. Set the range of allocation
  793. * first, then find the first free bit(s) from the bitmap (within the range),
  794. * and at last, allocate the blocks by claiming the found free bit as allocated.
  795. *
  796. * To set the range of this allocation:
  797. * if there is a reservation window, only try to allocate block(s) from the
  798. * file's own reservation window;
  799. * Otherwise, the allocation range starts from the give goal block, ends at
  800. * the block group's last block.
  801. *
  802. * If we failed to allocate the desired block then we may end up crossing to a
  803. * new bitmap. In that case we must release write access to the old one via
  804. * ext3_journal_release_buffer(), else we'll run out of credits.
  805. */
  806. static ext3_grpblk_t
  807. ext3_try_to_allocate(struct super_block *sb, handle_t *handle, int group,
  808. struct buffer_head *bitmap_bh, ext3_grpblk_t grp_goal,
  809. unsigned long *count, struct ext3_reserve_window *my_rsv)
  810. {
  811. ext3_fsblk_t group_first_block;
  812. ext3_grpblk_t start, end;
  813. unsigned long num = 0;
  814. /* we do allocation within the reservation window if we have a window */
  815. if (my_rsv) {
  816. group_first_block = ext3_group_first_block_no(sb, group);
  817. if (my_rsv->_rsv_start >= group_first_block)
  818. start = my_rsv->_rsv_start - group_first_block;
  819. else
  820. /* reservation window cross group boundary */
  821. start = 0;
  822. end = my_rsv->_rsv_end - group_first_block + 1;
  823. if (end > EXT3_BLOCKS_PER_GROUP(sb))
  824. /* reservation window crosses group boundary */
  825. end = EXT3_BLOCKS_PER_GROUP(sb);
  826. if ((start <= grp_goal) && (grp_goal < end))
  827. start = grp_goal;
  828. else
  829. grp_goal = -1;
  830. } else {
  831. if (grp_goal > 0)
  832. start = grp_goal;
  833. else
  834. start = 0;
  835. end = EXT3_BLOCKS_PER_GROUP(sb);
  836. }
  837. BUG_ON(start > EXT3_BLOCKS_PER_GROUP(sb));
  838. repeat:
  839. if (grp_goal < 0 || !ext3_test_allocatable(grp_goal, bitmap_bh)) {
  840. grp_goal = find_next_usable_block(start, bitmap_bh, end);
  841. if (grp_goal < 0)
  842. goto fail_access;
  843. if (!my_rsv) {
  844. int i;
  845. for (i = 0; i < 7 && grp_goal > start &&
  846. ext3_test_allocatable(grp_goal - 1,
  847. bitmap_bh);
  848. i++, grp_goal--)
  849. ;
  850. }
  851. }
  852. start = grp_goal;
  853. if (!claim_block(sb_bgl_lock(EXT3_SB(sb), group),
  854. grp_goal, bitmap_bh)) {
  855. /*
  856. * The block was allocated by another thread, or it was
  857. * allocated and then freed by another thread
  858. */
  859. start++;
  860. grp_goal++;
  861. if (start >= end)
  862. goto fail_access;
  863. goto repeat;
  864. }
  865. num++;
  866. grp_goal++;
  867. while (num < *count && grp_goal < end
  868. && ext3_test_allocatable(grp_goal, bitmap_bh)
  869. && claim_block(sb_bgl_lock(EXT3_SB(sb), group),
  870. grp_goal, bitmap_bh)) {
  871. num++;
  872. grp_goal++;
  873. }
  874. *count = num;
  875. return grp_goal - num;
  876. fail_access:
  877. *count = num;
  878. return -1;
  879. }
  880. /**
  881. * find_next_reservable_window():
  882. * find a reservable space within the given range.
  883. * It does not allocate the reservation window for now:
  884. * alloc_new_reservation() will do the work later.
  885. *
  886. * @search_head: the head of the searching list;
  887. * This is not necessarily the list head of the whole filesystem
  888. *
  889. * We have both head and start_block to assist the search
  890. * for the reservable space. The list starts from head,
  891. * but we will shift to the place where start_block is,
  892. * then start from there, when looking for a reservable space.
  893. *
  894. * @my_rsv: the reservation window
  895. *
  896. * @sb: the super block
  897. *
  898. * @start_block: the first block we consider to start
  899. * the real search from
  900. *
  901. * @last_block:
  902. * the maximum block number that our goal reservable space
  903. * could start from. This is normally the last block in this
  904. * group. The search will end when we found the start of next
  905. * possible reservable space is out of this boundary.
  906. * This could handle the cross boundary reservation window
  907. * request.
  908. *
  909. * basically we search from the given range, rather than the whole
  910. * reservation double linked list, (start_block, last_block)
  911. * to find a free region that is of my size and has not
  912. * been reserved.
  913. *
  914. */
  915. static int find_next_reservable_window(
  916. struct ext3_reserve_window_node *search_head,
  917. struct ext3_reserve_window_node *my_rsv,
  918. struct super_block * sb,
  919. ext3_fsblk_t start_block,
  920. ext3_fsblk_t last_block)
  921. {
  922. struct rb_node *next;
  923. struct ext3_reserve_window_node *rsv, *prev;
  924. ext3_fsblk_t cur;
  925. int size = my_rsv->rsv_goal_size;
  926. /* TODO: make the start of the reservation window byte-aligned */
  927. /* cur = *start_block & ~7;*/
  928. cur = start_block;
  929. rsv = search_head;
  930. if (!rsv)
  931. return -1;
  932. while (1) {
  933. if (cur <= rsv->rsv_end)
  934. cur = rsv->rsv_end + 1;
  935. /* TODO?
  936. * in the case we could not find a reservable space
  937. * that is what is expected, during the re-search, we could
  938. * remember what's the largest reservable space we could have
  939. * and return that one.
  940. *
  941. * For now it will fail if we could not find the reservable
  942. * space with expected-size (or more)...
  943. */
  944. if (cur > last_block)
  945. return -1; /* fail */
  946. prev = rsv;
  947. next = rb_next(&rsv->rsv_node);
  948. rsv = rb_entry(next,struct ext3_reserve_window_node,rsv_node);
  949. /*
  950. * Reached the last reservation, we can just append to the
  951. * previous one.
  952. */
  953. if (!next)
  954. break;
  955. if (cur + size <= rsv->rsv_start) {
  956. /*
  957. * Found a reserveable space big enough. We could
  958. * have a reservation across the group boundary here
  959. */
  960. break;
  961. }
  962. }
  963. /*
  964. * we come here either :
  965. * when we reach the end of the whole list,
  966. * and there is empty reservable space after last entry in the list.
  967. * append it to the end of the list.
  968. *
  969. * or we found one reservable space in the middle of the list,
  970. * return the reservation window that we could append to.
  971. * succeed.
  972. */
  973. if ((prev != my_rsv) && (!rsv_is_empty(&my_rsv->rsv_window)))
  974. rsv_window_remove(sb, my_rsv);
  975. /*
  976. * Let's book the whole available window for now. We will check the
  977. * disk bitmap later and then, if there are free blocks then we adjust
  978. * the window size if it's larger than requested.
  979. * Otherwise, we will remove this node from the tree next time
  980. * call find_next_reservable_window.
  981. */
  982. my_rsv->rsv_start = cur;
  983. my_rsv->rsv_end = cur + size - 1;
  984. my_rsv->rsv_alloc_hit = 0;
  985. if (prev != my_rsv)
  986. ext3_rsv_window_add(sb, my_rsv);
  987. return 0;
  988. }
  989. /**
  990. * alloc_new_reservation()--allocate a new reservation window
  991. *
  992. * To make a new reservation, we search part of the filesystem
  993. * reservation list (the list that inside the group). We try to
  994. * allocate a new reservation window near the allocation goal,
  995. * or the beginning of the group, if there is no goal.
  996. *
  997. * We first find a reservable space after the goal, then from
  998. * there, we check the bitmap for the first free block after
  999. * it. If there is no free block until the end of group, then the
  1000. * whole group is full, we failed. Otherwise, check if the free
  1001. * block is inside the expected reservable space, if so, we
  1002. * succeed.
  1003. * If the first free block is outside the reservable space, then
  1004. * start from the first free block, we search for next available
  1005. * space, and go on.
  1006. *
  1007. * on succeed, a new reservation will be found and inserted into the list
  1008. * It contains at least one free block, and it does not overlap with other
  1009. * reservation windows.
  1010. *
  1011. * failed: we failed to find a reservation window in this group
  1012. *
  1013. * @my_rsv: the reservation window
  1014. *
  1015. * @grp_goal: The goal (group-relative). It is where the search for a
  1016. * free reservable space should start from.
  1017. * if we have a grp_goal(grp_goal >0 ), then start from there,
  1018. * no grp_goal(grp_goal = -1), we start from the first block
  1019. * of the group.
  1020. *
  1021. * @sb: the super block
  1022. * @group: the group we are trying to allocate in
  1023. * @bitmap_bh: the block group block bitmap
  1024. *
  1025. */
  1026. static int alloc_new_reservation(struct ext3_reserve_window_node *my_rsv,
  1027. ext3_grpblk_t grp_goal, struct super_block *sb,
  1028. unsigned int group, struct buffer_head *bitmap_bh)
  1029. {
  1030. struct ext3_reserve_window_node *search_head;
  1031. ext3_fsblk_t group_first_block, group_end_block, start_block;
  1032. ext3_grpblk_t first_free_block;
  1033. struct rb_root *fs_rsv_root = &EXT3_SB(sb)->s_rsv_window_root;
  1034. unsigned long size;
  1035. int ret;
  1036. spinlock_t *rsv_lock = &EXT3_SB(sb)->s_rsv_window_lock;
  1037. group_first_block = ext3_group_first_block_no(sb, group);
  1038. group_end_block = group_first_block + (EXT3_BLOCKS_PER_GROUP(sb) - 1);
  1039. if (grp_goal < 0)
  1040. start_block = group_first_block;
  1041. else
  1042. start_block = grp_goal + group_first_block;
  1043. size = my_rsv->rsv_goal_size;
  1044. if (!rsv_is_empty(&my_rsv->rsv_window)) {
  1045. /*
  1046. * if the old reservation is cross group boundary
  1047. * and if the goal is inside the old reservation window,
  1048. * we will come here when we just failed to allocate from
  1049. * the first part of the window. We still have another part
  1050. * that belongs to the next group. In this case, there is no
  1051. * point to discard our window and try to allocate a new one
  1052. * in this group(which will fail). we should
  1053. * keep the reservation window, just simply move on.
  1054. *
  1055. * Maybe we could shift the start block of the reservation
  1056. * window to the first block of next group.
  1057. */
  1058. if ((my_rsv->rsv_start <= group_end_block) &&
  1059. (my_rsv->rsv_end > group_end_block) &&
  1060. (start_block >= my_rsv->rsv_start))
  1061. return -1;
  1062. if ((my_rsv->rsv_alloc_hit >
  1063. (my_rsv->rsv_end - my_rsv->rsv_start + 1) / 2)) {
  1064. /*
  1065. * if the previously allocation hit ratio is
  1066. * greater than 1/2, then we double the size of
  1067. * the reservation window the next time,
  1068. * otherwise we keep the same size window
  1069. */
  1070. size = size * 2;
  1071. if (size > EXT3_MAX_RESERVE_BLOCKS)
  1072. size = EXT3_MAX_RESERVE_BLOCKS;
  1073. my_rsv->rsv_goal_size= size;
  1074. }
  1075. }
  1076. spin_lock(rsv_lock);
  1077. /*
  1078. * shift the search start to the window near the goal block
  1079. */
  1080. search_head = search_reserve_window(fs_rsv_root, start_block);
  1081. /*
  1082. * find_next_reservable_window() simply finds a reservable window
  1083. * inside the given range(start_block, group_end_block).
  1084. *
  1085. * To make sure the reservation window has a free bit inside it, we
  1086. * need to check the bitmap after we found a reservable window.
  1087. */
  1088. retry:
  1089. ret = find_next_reservable_window(search_head, my_rsv, sb,
  1090. start_block, group_end_block);
  1091. if (ret == -1) {
  1092. if (!rsv_is_empty(&my_rsv->rsv_window))
  1093. rsv_window_remove(sb, my_rsv);
  1094. spin_unlock(rsv_lock);
  1095. return -1;
  1096. }
  1097. /*
  1098. * On success, find_next_reservable_window() returns the
  1099. * reservation window where there is a reservable space after it.
  1100. * Before we reserve this reservable space, we need
  1101. * to make sure there is at least a free block inside this region.
  1102. *
  1103. * searching the first free bit on the block bitmap and copy of
  1104. * last committed bitmap alternatively, until we found a allocatable
  1105. * block. Search start from the start block of the reservable space
  1106. * we just found.
  1107. */
  1108. spin_unlock(rsv_lock);
  1109. first_free_block = bitmap_search_next_usable_block(
  1110. my_rsv->rsv_start - group_first_block,
  1111. bitmap_bh, group_end_block - group_first_block + 1);
  1112. if (first_free_block < 0) {
  1113. /*
  1114. * no free block left on the bitmap, no point
  1115. * to reserve the space. return failed.
  1116. */
  1117. spin_lock(rsv_lock);
  1118. if (!rsv_is_empty(&my_rsv->rsv_window))
  1119. rsv_window_remove(sb, my_rsv);
  1120. spin_unlock(rsv_lock);
  1121. return -1; /* failed */
  1122. }
  1123. start_block = first_free_block + group_first_block;
  1124. /*
  1125. * check if the first free block is within the
  1126. * free space we just reserved
  1127. */
  1128. if (start_block >= my_rsv->rsv_start && start_block <= my_rsv->rsv_end)
  1129. return 0; /* success */
  1130. /*
  1131. * if the first free bit we found is out of the reservable space
  1132. * continue search for next reservable space,
  1133. * start from where the free block is,
  1134. * we also shift the list head to where we stopped last time
  1135. */
  1136. search_head = my_rsv;
  1137. spin_lock(rsv_lock);
  1138. goto retry;
  1139. }
  1140. /**
  1141. * try_to_extend_reservation()
  1142. * @my_rsv: given reservation window
  1143. * @sb: super block
  1144. * @size: the delta to extend
  1145. *
  1146. * Attempt to expand the reservation window large enough to have
  1147. * required number of free blocks
  1148. *
  1149. * Since ext3_try_to_allocate() will always allocate blocks within
  1150. * the reservation window range, if the window size is too small,
  1151. * multiple blocks allocation has to stop at the end of the reservation
  1152. * window. To make this more efficient, given the total number of
  1153. * blocks needed and the current size of the window, we try to
  1154. * expand the reservation window size if necessary on a best-effort
  1155. * basis before ext3_new_blocks() tries to allocate blocks,
  1156. */
  1157. static void try_to_extend_reservation(struct ext3_reserve_window_node *my_rsv,
  1158. struct super_block *sb, int size)
  1159. {
  1160. struct ext3_reserve_window_node *next_rsv;
  1161. struct rb_node *next;
  1162. spinlock_t *rsv_lock = &EXT3_SB(sb)->s_rsv_window_lock;
  1163. if (!spin_trylock(rsv_lock))
  1164. return;
  1165. next = rb_next(&my_rsv->rsv_node);
  1166. if (!next)
  1167. my_rsv->rsv_end += size;
  1168. else {
  1169. next_rsv = rb_entry(next, struct ext3_reserve_window_node, rsv_node);
  1170. if ((next_rsv->rsv_start - my_rsv->rsv_end - 1) >= size)
  1171. my_rsv->rsv_end += size;
  1172. else
  1173. my_rsv->rsv_end = next_rsv->rsv_start - 1;
  1174. }
  1175. spin_unlock(rsv_lock);
  1176. }
  1177. /**
  1178. * ext3_try_to_allocate_with_rsv()
  1179. * @sb: superblock
  1180. * @handle: handle to this transaction
  1181. * @group: given allocation block group
  1182. * @bitmap_bh: bufferhead holds the block bitmap
  1183. * @grp_goal: given target block within the group
  1184. * @my_rsv: reservation window
  1185. * @count: target number of blocks to allocate
  1186. * @errp: pointer to store the error code
  1187. *
  1188. * This is the main function used to allocate a new block and its reservation
  1189. * window.
  1190. *
  1191. * Each time when a new block allocation is need, first try to allocate from
  1192. * its own reservation. If it does not have a reservation window, instead of
  1193. * looking for a free bit on bitmap first, then look up the reservation list to
  1194. * see if it is inside somebody else's reservation window, we try to allocate a
  1195. * reservation window for it starting from the goal first. Then do the block
  1196. * allocation within the reservation window.
  1197. *
  1198. * This will avoid keeping on searching the reservation list again and
  1199. * again when somebody is looking for a free block (without
  1200. * reservation), and there are lots of free blocks, but they are all
  1201. * being reserved.
  1202. *
  1203. * We use a red-black tree for the per-filesystem reservation list.
  1204. *
  1205. */
  1206. static ext3_grpblk_t
  1207. ext3_try_to_allocate_with_rsv(struct super_block *sb, handle_t *handle,
  1208. unsigned int group, struct buffer_head *bitmap_bh,
  1209. ext3_grpblk_t grp_goal,
  1210. struct ext3_reserve_window_node * my_rsv,
  1211. unsigned long *count, int *errp)
  1212. {
  1213. ext3_fsblk_t group_first_block, group_last_block;
  1214. ext3_grpblk_t ret = 0;
  1215. int fatal;
  1216. unsigned long num = *count;
  1217. *errp = 0;
  1218. /*
  1219. * Make sure we use undo access for the bitmap, because it is critical
  1220. * that we do the frozen_data COW on bitmap buffers in all cases even
  1221. * if the buffer is in BJ_Forget state in the committing transaction.
  1222. */
  1223. BUFFER_TRACE(bitmap_bh, "get undo access for new block");
  1224. fatal = ext3_journal_get_undo_access(handle, bitmap_bh);
  1225. if (fatal) {
  1226. *errp = fatal;
  1227. return -1;
  1228. }
  1229. /*
  1230. * we don't deal with reservation when
  1231. * filesystem is mounted without reservation
  1232. * or the file is not a regular file
  1233. * or last attempt to allocate a block with reservation turned on failed
  1234. */
  1235. if (my_rsv == NULL ) {
  1236. ret = ext3_try_to_allocate(sb, handle, group, bitmap_bh,
  1237. grp_goal, count, NULL);
  1238. goto out;
  1239. }
  1240. /*
  1241. * grp_goal is a group relative block number (if there is a goal)
  1242. * 0 <= grp_goal < EXT3_BLOCKS_PER_GROUP(sb)
  1243. * first block is a filesystem wide block number
  1244. * first block is the block number of the first block in this group
  1245. */
  1246. group_first_block = ext3_group_first_block_no(sb, group);
  1247. group_last_block = group_first_block + (EXT3_BLOCKS_PER_GROUP(sb) - 1);
  1248. /*
  1249. * Basically we will allocate a new block from inode's reservation
  1250. * window.
  1251. *
  1252. * We need to allocate a new reservation window, if:
  1253. * a) inode does not have a reservation window; or
  1254. * b) last attempt to allocate a block from existing reservation
  1255. * failed; or
  1256. * c) we come here with a goal and with a reservation window
  1257. *
  1258. * We do not need to allocate a new reservation window if we come here
  1259. * at the beginning with a goal and the goal is inside the window, or
  1260. * we don't have a goal but already have a reservation window.
  1261. * then we could go to allocate from the reservation window directly.
  1262. */
  1263. while (1) {
  1264. if (rsv_is_empty(&my_rsv->rsv_window) || (ret < 0) ||
  1265. !goal_in_my_reservation(&my_rsv->rsv_window,
  1266. grp_goal, group, sb)) {
  1267. if (my_rsv->rsv_goal_size < *count)
  1268. my_rsv->rsv_goal_size = *count;
  1269. ret = alloc_new_reservation(my_rsv, grp_goal, sb,
  1270. group, bitmap_bh);
  1271. if (ret < 0)
  1272. break; /* failed */
  1273. if (!goal_in_my_reservation(&my_rsv->rsv_window,
  1274. grp_goal, group, sb))
  1275. grp_goal = -1;
  1276. } else if (grp_goal >= 0) {
  1277. int curr = my_rsv->rsv_end -
  1278. (grp_goal + group_first_block) + 1;
  1279. if (curr < *count)
  1280. try_to_extend_reservation(my_rsv, sb,
  1281. *count - curr);
  1282. }
  1283. if ((my_rsv->rsv_start > group_last_block) ||
  1284. (my_rsv->rsv_end < group_first_block)) {
  1285. rsv_window_dump(&EXT3_SB(sb)->s_rsv_window_root, 1);
  1286. BUG();
  1287. }
  1288. ret = ext3_try_to_allocate(sb, handle, group, bitmap_bh,
  1289. grp_goal, &num, &my_rsv->rsv_window);
  1290. if (ret >= 0) {
  1291. my_rsv->rsv_alloc_hit += num;
  1292. *count = num;
  1293. break; /* succeed */
  1294. }
  1295. num = *count;
  1296. }
  1297. out:
  1298. if (ret >= 0) {
  1299. BUFFER_TRACE(bitmap_bh, "journal_dirty_metadata for "
  1300. "bitmap block");
  1301. fatal = ext3_journal_dirty_metadata(handle, bitmap_bh);
  1302. if (fatal) {
  1303. *errp = fatal;
  1304. return -1;
  1305. }
  1306. return ret;
  1307. }
  1308. BUFFER_TRACE(bitmap_bh, "journal_release_buffer");
  1309. ext3_journal_release_buffer(handle, bitmap_bh);
  1310. return ret;
  1311. }
  1312. /**
  1313. * ext3_has_free_blocks()
  1314. * @sbi: in-core super block structure.
  1315. *
  1316. * Check if filesystem has at least 1 free block available for allocation.
  1317. */
  1318. static int ext3_has_free_blocks(struct ext3_sb_info *sbi)
  1319. {
  1320. ext3_fsblk_t free_blocks, root_blocks;
  1321. free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
  1322. root_blocks = le32_to_cpu(sbi->s_es->s_r_blocks_count);
  1323. if (free_blocks < root_blocks + 1 && !capable(CAP_SYS_RESOURCE) &&
  1324. sbi->s_resuid != current_fsuid() &&
  1325. (sbi->s_resgid == 0 || !in_group_p (sbi->s_resgid))) {
  1326. return 0;
  1327. }
  1328. return 1;
  1329. }
  1330. /**
  1331. * ext3_should_retry_alloc()
  1332. * @sb: super block
  1333. * @retries number of attemps has been made
  1334. *
  1335. * ext3_should_retry_alloc() is called when ENOSPC is returned, and if
  1336. * it is profitable to retry the operation, this function will wait
  1337. * for the current or committing transaction to complete, and then
  1338. * return TRUE.
  1339. *
  1340. * if the total number of retries exceed three times, return FALSE.
  1341. */
  1342. int ext3_should_retry_alloc(struct super_block *sb, int *retries)
  1343. {
  1344. if (!ext3_has_free_blocks(EXT3_SB(sb)) || (*retries)++ > 3)
  1345. return 0;
  1346. jbd_debug(1, "%s: retrying operation after ENOSPC\n", sb->s_id);
  1347. return journal_force_commit_nested(EXT3_SB(sb)->s_journal);
  1348. }
  1349. /**
  1350. * ext3_new_blocks() -- core block(s) allocation function
  1351. * @handle: handle to this transaction
  1352. * @inode: file inode
  1353. * @goal: given target block(filesystem wide)
  1354. * @count: target number of blocks to allocate
  1355. * @errp: error code
  1356. *
  1357. * ext3_new_blocks uses a goal block to assist allocation. It tries to
  1358. * allocate block(s) from the block group contains the goal block first. If that
  1359. * fails, it will try to allocate block(s) from other block groups without
  1360. * any specific goal block.
  1361. *
  1362. */
  1363. ext3_fsblk_t ext3_new_blocks(handle_t *handle, struct inode *inode,
  1364. ext3_fsblk_t goal, unsigned long *count, int *errp)
  1365. {
  1366. struct buffer_head *bitmap_bh = NULL;
  1367. struct buffer_head *gdp_bh;
  1368. int group_no;
  1369. int goal_group;
  1370. ext3_grpblk_t grp_target_blk; /* blockgroup relative goal block */
  1371. ext3_grpblk_t grp_alloc_blk; /* blockgroup-relative allocated block*/
  1372. ext3_fsblk_t ret_block; /* filesyetem-wide allocated block */
  1373. int bgi; /* blockgroup iteration index */
  1374. int fatal = 0, err;
  1375. int performed_allocation = 0;
  1376. ext3_grpblk_t free_blocks; /* number of free blocks in a group */
  1377. struct super_block *sb;
  1378. struct ext3_group_desc *gdp;
  1379. struct ext3_super_block *es;
  1380. struct ext3_sb_info *sbi;
  1381. struct ext3_reserve_window_node *my_rsv = NULL;
  1382. struct ext3_block_alloc_info *block_i;
  1383. unsigned short windowsz = 0;
  1384. #ifdef EXT3FS_DEBUG
  1385. static int goal_hits, goal_attempts;
  1386. #endif
  1387. unsigned long ngroups;
  1388. unsigned long num = *count;
  1389. *errp = -ENOSPC;
  1390. sb = inode->i_sb;
  1391. if (!sb) {
  1392. printk("ext3_new_block: nonexistent device");
  1393. return 0;
  1394. }
  1395. /*
  1396. * Check quota for allocation of this block.
  1397. */
  1398. err = dquot_alloc_block(inode, num);
  1399. if (err) {
  1400. *errp = err;
  1401. return 0;
  1402. }
  1403. sbi = EXT3_SB(sb);
  1404. es = EXT3_SB(sb)->s_es;
  1405. ext3_debug("goal=%lu.\n", goal);
  1406. /*
  1407. * Allocate a block from reservation only when
  1408. * filesystem is mounted with reservation(default,-o reservation), and
  1409. * it's a regular file, and
  1410. * the desired window size is greater than 0 (One could use ioctl
  1411. * command EXT3_IOC_SETRSVSZ to set the window size to 0 to turn off
  1412. * reservation on that particular file)
  1413. */
  1414. block_i = EXT3_I(inode)->i_block_alloc_info;
  1415. if (block_i && ((windowsz = block_i->rsv_window_node.rsv_goal_size) > 0))
  1416. my_rsv = &block_i->rsv_window_node;
  1417. if (!ext3_has_free_blocks(sbi)) {
  1418. *errp = -ENOSPC;
  1419. goto out;
  1420. }
  1421. /*
  1422. * First, test whether the goal block is free.
  1423. */
  1424. if (goal < le32_to_cpu(es->s_first_data_block) ||
  1425. goal >= le32_to_cpu(es->s_blocks_count))
  1426. goal = le32_to_cpu(es->s_first_data_block);
  1427. group_no = (goal - le32_to_cpu(es->s_first_data_block)) /
  1428. EXT3_BLOCKS_PER_GROUP(sb);
  1429. goal_group = group_no;
  1430. retry_alloc:
  1431. gdp = ext3_get_group_desc(sb, group_no, &gdp_bh);
  1432. if (!gdp)
  1433. goto io_error;
  1434. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1435. /*
  1436. * if there is not enough free blocks to make a new resevation
  1437. * turn off reservation for this allocation
  1438. */
  1439. if (my_rsv && (free_blocks < windowsz)
  1440. && (free_blocks > 0)
  1441. && (rsv_is_empty(&my_rsv->rsv_window)))
  1442. my_rsv = NULL;
  1443. if (free_blocks > 0) {
  1444. grp_target_blk = ((goal - le32_to_cpu(es->s_first_data_block)) %
  1445. EXT3_BLOCKS_PER_GROUP(sb));
  1446. bitmap_bh = read_block_bitmap(sb, group_no);
  1447. if (!bitmap_bh)
  1448. goto io_error;
  1449. grp_alloc_blk = ext3_try_to_allocate_with_rsv(sb, handle,
  1450. group_no, bitmap_bh, grp_target_blk,
  1451. my_rsv, &num, &fatal);
  1452. if (fatal)
  1453. goto out;
  1454. if (grp_alloc_blk >= 0)
  1455. goto allocated;
  1456. }
  1457. ngroups = EXT3_SB(sb)->s_groups_count;
  1458. smp_rmb();
  1459. /*
  1460. * Now search the rest of the groups. We assume that
  1461. * group_no and gdp correctly point to the last group visited.
  1462. */
  1463. for (bgi = 0; bgi < ngroups; bgi++) {
  1464. group_no++;
  1465. if (group_no >= ngroups)
  1466. group_no = 0;
  1467. gdp = ext3_get_group_desc(sb, group_no, &gdp_bh);
  1468. if (!gdp)
  1469. goto io_error;
  1470. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1471. /*
  1472. * skip this group (and avoid loading bitmap) if there
  1473. * are no free blocks
  1474. */
  1475. if (!free_blocks)
  1476. continue;
  1477. /*
  1478. * skip this group if the number of
  1479. * free blocks is less than half of the reservation
  1480. * window size.
  1481. */
  1482. if (my_rsv && (free_blocks <= (windowsz/2)))
  1483. continue;
  1484. brelse(bitmap_bh);
  1485. bitmap_bh = read_block_bitmap(sb, group_no);
  1486. if (!bitmap_bh)
  1487. goto io_error;
  1488. /*
  1489. * try to allocate block(s) from this group, without a goal(-1).
  1490. */
  1491. grp_alloc_blk = ext3_try_to_allocate_with_rsv(sb, handle,
  1492. group_no, bitmap_bh, -1, my_rsv,
  1493. &num, &fatal);
  1494. if (fatal)
  1495. goto out;
  1496. if (grp_alloc_blk >= 0)
  1497. goto allocated;
  1498. }
  1499. /*
  1500. * We may end up a bogus earlier ENOSPC error due to
  1501. * filesystem is "full" of reservations, but
  1502. * there maybe indeed free blocks available on disk
  1503. * In this case, we just forget about the reservations
  1504. * just do block allocation as without reservations.
  1505. */
  1506. if (my_rsv) {
  1507. my_rsv = NULL;
  1508. windowsz = 0;
  1509. group_no = goal_group;
  1510. goto retry_alloc;
  1511. }
  1512. /* No space left on the device */
  1513. *errp = -ENOSPC;
  1514. goto out;
  1515. allocated:
  1516. ext3_debug("using block group %d(%d)\n",
  1517. group_no, gdp->bg_free_blocks_count);
  1518. BUFFER_TRACE(gdp_bh, "get_write_access");
  1519. fatal = ext3_journal_get_write_access(handle, gdp_bh);
  1520. if (fatal)
  1521. goto out;
  1522. ret_block = grp_alloc_blk + ext3_group_first_block_no(sb, group_no);
  1523. if (in_range(le32_to_cpu(gdp->bg_block_bitmap), ret_block, num) ||
  1524. in_range(le32_to_cpu(gdp->bg_inode_bitmap), ret_block, num) ||
  1525. in_range(ret_block, le32_to_cpu(gdp->bg_inode_table),
  1526. EXT3_SB(sb)->s_itb_per_group) ||
  1527. in_range(ret_block + num - 1, le32_to_cpu(gdp->bg_inode_table),
  1528. EXT3_SB(sb)->s_itb_per_group)) {
  1529. ext3_error(sb, "ext3_new_block",
  1530. "Allocating block in system zone - "
  1531. "blocks from "E3FSBLK", length %lu",
  1532. ret_block, num);
  1533. /*
  1534. * claim_block() marked the blocks we allocated as in use. So we
  1535. * may want to selectively mark some of the blocks as free.
  1536. */
  1537. goto retry_alloc;
  1538. }
  1539. performed_allocation = 1;
  1540. #ifdef CONFIG_JBD_DEBUG
  1541. {
  1542. struct buffer_head *debug_bh;
  1543. /* Record bitmap buffer state in the newly allocated block */
  1544. debug_bh = sb_find_get_block(sb, ret_block);
  1545. if (debug_bh) {
  1546. BUFFER_TRACE(debug_bh, "state when allocated");
  1547. BUFFER_TRACE2(debug_bh, bitmap_bh, "bitmap state");
  1548. brelse(debug_bh);
  1549. }
  1550. }
  1551. jbd_lock_bh_state(bitmap_bh);
  1552. spin_lock(sb_bgl_lock(sbi, group_no));
  1553. if (buffer_jbd(bitmap_bh) && bh2jh(bitmap_bh)->b_committed_data) {
  1554. int i;
  1555. for (i = 0; i < num; i++) {
  1556. if (ext3_test_bit(grp_alloc_blk+i,
  1557. bh2jh(bitmap_bh)->b_committed_data)) {
  1558. printk("%s: block was unexpectedly set in "
  1559. "b_committed_data\n", __func__);
  1560. }
  1561. }
  1562. }
  1563. ext3_debug("found bit %d\n", grp_alloc_blk);
  1564. spin_unlock(sb_bgl_lock(sbi, group_no));
  1565. jbd_unlock_bh_state(bitmap_bh);
  1566. #endif
  1567. if (ret_block + num - 1 >= le32_to_cpu(es->s_blocks_count)) {
  1568. ext3_error(sb, "ext3_new_block",
  1569. "block("E3FSBLK") >= blocks count(%d) - "
  1570. "block_group = %d, es == %p ", ret_block,
  1571. le32_to_cpu(es->s_blocks_count), group_no, es);
  1572. goto out;
  1573. }
  1574. /*
  1575. * It is up to the caller to add the new buffer to a journal
  1576. * list of some description. We don't know in advance whether
  1577. * the caller wants to use it as metadata or data.
  1578. */
  1579. ext3_debug("allocating block %lu. Goal hits %d of %d.\n",
  1580. ret_block, goal_hits, goal_attempts);
  1581. spin_lock(sb_bgl_lock(sbi, group_no));
  1582. le16_add_cpu(&gdp->bg_free_blocks_count, -num);
  1583. spin_unlock(sb_bgl_lock(sbi, group_no));
  1584. percpu_counter_sub(&sbi->s_freeblocks_counter, num);
  1585. BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor");
  1586. err = ext3_journal_dirty_metadata(handle, gdp_bh);
  1587. if (!fatal)
  1588. fatal = err;
  1589. if (fatal)
  1590. goto out;
  1591. *errp = 0;
  1592. brelse(bitmap_bh);
  1593. dquot_free_block(inode, *count-num);
  1594. *count = num;
  1595. return ret_block;
  1596. io_error:
  1597. *errp = -EIO;
  1598. out:
  1599. if (fatal) {
  1600. *errp = fatal;
  1601. ext3_std_error(sb, fatal);
  1602. }
  1603. /*
  1604. * Undo the block allocation
  1605. */
  1606. if (!performed_allocation)
  1607. dquot_free_block(inode, *count);
  1608. brelse(bitmap_bh);
  1609. return 0;
  1610. }
  1611. ext3_fsblk_t ext3_new_block(handle_t *handle, struct inode *inode,
  1612. ext3_fsblk_t goal, int *errp)
  1613. {
  1614. unsigned long count = 1;
  1615. return ext3_new_blocks(handle, inode, goal, &count, errp);
  1616. }
  1617. /**
  1618. * ext3_count_free_blocks() -- count filesystem free blocks
  1619. * @sb: superblock
  1620. *
  1621. * Adds up the number of free blocks from each block group.
  1622. */
  1623. ext3_fsblk_t ext3_count_free_blocks(struct super_block *sb)
  1624. {
  1625. ext3_fsblk_t desc_count;
  1626. struct ext3_group_desc *gdp;
  1627. int i;
  1628. unsigned long ngroups = EXT3_SB(sb)->s_groups_count;
  1629. #ifdef EXT3FS_DEBUG
  1630. struct ext3_super_block *es;
  1631. ext3_fsblk_t bitmap_count;
  1632. unsigned long x;
  1633. struct buffer_head *bitmap_bh = NULL;
  1634. es = EXT3_SB(sb)->s_es;
  1635. desc_count = 0;
  1636. bitmap_count = 0;
  1637. gdp = NULL;
  1638. smp_rmb();
  1639. for (i = 0; i < ngroups; i++) {
  1640. gdp = ext3_get_group_desc(sb, i, NULL);
  1641. if (!gdp)
  1642. continue;
  1643. desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
  1644. brelse(bitmap_bh);
  1645. bitmap_bh = read_block_bitmap(sb, i);
  1646. if (bitmap_bh == NULL)
  1647. continue;
  1648. x = ext3_count_free(bitmap_bh, sb->s_blocksize);
  1649. printk("group %d: stored = %d, counted = %lu\n",
  1650. i, le16_to_cpu(gdp->bg_free_blocks_count), x);
  1651. bitmap_count += x;
  1652. }
  1653. brelse(bitmap_bh);
  1654. printk("ext3_count_free_blocks: stored = "E3FSBLK
  1655. ", computed = "E3FSBLK", "E3FSBLK"\n",
  1656. le32_to_cpu(es->s_free_blocks_count),
  1657. desc_count, bitmap_count);
  1658. return bitmap_count;
  1659. #else
  1660. desc_count = 0;
  1661. smp_rmb();
  1662. for (i = 0; i < ngroups; i++) {
  1663. gdp = ext3_get_group_desc(sb, i, NULL);
  1664. if (!gdp)
  1665. continue;
  1666. desc_count += le16_to_cpu(gdp->bg_free_blocks_count);
  1667. }
  1668. return desc_count;
  1669. #endif
  1670. }
  1671. static inline int test_root(int a, int b)
  1672. {
  1673. int num = b;
  1674. while (a > num)
  1675. num *= b;
  1676. return num == a;
  1677. }
  1678. static int ext3_group_sparse(int group)
  1679. {
  1680. if (group <= 1)
  1681. return 1;
  1682. if (!(group & 1))
  1683. return 0;
  1684. return (test_root(group, 7) || test_root(group, 5) ||
  1685. test_root(group, 3));
  1686. }
  1687. /**
  1688. * ext3_bg_has_super - number of blocks used by the superblock in group
  1689. * @sb: superblock for filesystem
  1690. * @group: group number to check
  1691. *
  1692. * Return the number of blocks used by the superblock (primary or backup)
  1693. * in this group. Currently this will be only 0 or 1.
  1694. */
  1695. int ext3_bg_has_super(struct super_block *sb, int group)
  1696. {
  1697. if (EXT3_HAS_RO_COMPAT_FEATURE(sb,
  1698. EXT3_FEATURE_RO_COMPAT_SPARSE_SUPER) &&
  1699. !ext3_group_sparse(group))
  1700. return 0;
  1701. return 1;
  1702. }
  1703. static unsigned long ext3_bg_num_gdb_meta(struct super_block *sb, int group)
  1704. {
  1705. unsigned long metagroup = group / EXT3_DESC_PER_BLOCK(sb);
  1706. unsigned long first = metagroup * EXT3_DESC_PER_BLOCK(sb);
  1707. unsigned long last = first + EXT3_DESC_PER_BLOCK(sb) - 1;
  1708. if (group == first || group == first + 1 || group == last)
  1709. return 1;
  1710. return 0;
  1711. }
  1712. static unsigned long ext3_bg_num_gdb_nometa(struct super_block *sb, int group)
  1713. {
  1714. return ext3_bg_has_super(sb, group) ? EXT3_SB(sb)->s_gdb_count : 0;
  1715. }
  1716. /**
  1717. * ext3_bg_num_gdb - number of blocks used by the group table in group
  1718. * @sb: superblock for filesystem
  1719. * @group: group number to check
  1720. *
  1721. * Return the number of blocks used by the group descriptor table
  1722. * (primary or backup) in this group. In the future there may be a
  1723. * different number of descriptor blocks in each group.
  1724. */
  1725. unsigned long ext3_bg_num_gdb(struct super_block *sb, int group)
  1726. {
  1727. unsigned long first_meta_bg =
  1728. le32_to_cpu(EXT3_SB(sb)->s_es->s_first_meta_bg);
  1729. unsigned long metagroup = group / EXT3_DESC_PER_BLOCK(sb);
  1730. if (!EXT3_HAS_INCOMPAT_FEATURE(sb,EXT3_FEATURE_INCOMPAT_META_BG) ||
  1731. metagroup < first_meta_bg)
  1732. return ext3_bg_num_gdb_nometa(sb,group);
  1733. return ext3_bg_num_gdb_meta(sb,group);
  1734. }
  1735. /**
  1736. * ext3_trim_all_free -- function to trim all free space in alloc. group
  1737. * @sb: super block for file system
  1738. * @group: allocation group to trim
  1739. * @start: first group block to examine
  1740. * @max: last group block to examine
  1741. * @gdp: allocation group description structure
  1742. * @minblocks: minimum extent block count
  1743. *
  1744. * ext3_trim_all_free walks through group's block bitmap searching for free
  1745. * blocks. When the free block is found, it tries to allocate this block and
  1746. * consequent free block to get the biggest free extent possible, until it
  1747. * reaches any used block. Then issue a TRIM command on this extent and free
  1748. * the extent in the block bitmap. This is done until whole group is scanned.
  1749. */
  1750. ext3_grpblk_t ext3_trim_all_free(struct super_block *sb, unsigned int group,
  1751. ext3_grpblk_t start, ext3_grpblk_t max,
  1752. ext3_grpblk_t minblocks)
  1753. {
  1754. handle_t *handle;
  1755. ext3_grpblk_t next, free_blocks, bit, freed, count = 0;
  1756. ext3_fsblk_t discard_block;
  1757. struct ext3_sb_info *sbi;
  1758. struct buffer_head *gdp_bh, *bitmap_bh = NULL;
  1759. struct ext3_group_desc *gdp;
  1760. int err = 0, ret = 0;
  1761. /*
  1762. * We will update one block bitmap, and one group descriptor
  1763. */
  1764. handle = ext3_journal_start_sb(sb, 2);
  1765. if (IS_ERR(handle))
  1766. return PTR_ERR(handle);
  1767. bitmap_bh = read_block_bitmap(sb, group);
  1768. if (!bitmap_bh) {
  1769. err = -EIO;
  1770. goto err_out;
  1771. }
  1772. BUFFER_TRACE(bitmap_bh, "getting undo access");
  1773. err = ext3_journal_get_undo_access(handle, bitmap_bh);
  1774. if (err)
  1775. goto err_out;
  1776. gdp = ext3_get_group_desc(sb, group, &gdp_bh);
  1777. if (!gdp) {
  1778. err = -EIO;
  1779. goto err_out;
  1780. }
  1781. BUFFER_TRACE(gdp_bh, "get_write_access");
  1782. err = ext3_journal_get_write_access(handle, gdp_bh);
  1783. if (err)
  1784. goto err_out;
  1785. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1786. sbi = EXT3_SB(sb);
  1787. /* Walk through the whole group */
  1788. while (start < max) {
  1789. start = bitmap_search_next_usable_block(start, bitmap_bh, max);
  1790. if (start < 0)
  1791. break;
  1792. next = start;
  1793. /*
  1794. * Allocate contiguous free extents by setting bits in the
  1795. * block bitmap
  1796. */
  1797. while (next < max
  1798. && claim_block(sb_bgl_lock(sbi, group),
  1799. next, bitmap_bh)) {
  1800. next++;
  1801. }
  1802. /* We did not claim any blocks */
  1803. if (next == start)
  1804. continue;
  1805. discard_block = (ext3_fsblk_t)start +
  1806. ext3_group_first_block_no(sb, group);
  1807. /* Update counters */
  1808. spin_lock(sb_bgl_lock(sbi, group));
  1809. le16_add_cpu(&gdp->bg_free_blocks_count, start - next);
  1810. spin_unlock(sb_bgl_lock(sbi, group));
  1811. percpu_counter_sub(&sbi->s_freeblocks_counter, next - start);
  1812. free_blocks -= next - start;
  1813. /* Do not issue a TRIM on extents smaller than minblocks */
  1814. if ((next - start) < minblocks)
  1815. goto free_extent;
  1816. /* Send the TRIM command down to the device */
  1817. err = sb_issue_discard(sb, discard_block, next - start,
  1818. GFP_NOFS, 0);
  1819. count += (next - start);
  1820. free_extent:
  1821. freed = 0;
  1822. /*
  1823. * Clear bits in the bitmap
  1824. */
  1825. for (bit = start; bit < next; bit++) {
  1826. BUFFER_TRACE(bitmap_bh, "clear bit");
  1827. if (!ext3_clear_bit_atomic(sb_bgl_lock(sbi, group),
  1828. bit, bitmap_bh->b_data)) {
  1829. ext3_error(sb, __func__,
  1830. "bit already cleared for block "E3FSBLK,
  1831. (unsigned long)bit);
  1832. BUFFER_TRACE(bitmap_bh, "bit already cleared");
  1833. } else {
  1834. freed++;
  1835. }
  1836. }
  1837. /* Update couters */
  1838. spin_lock(sb_bgl_lock(sbi, group));
  1839. le16_add_cpu(&gdp->bg_free_blocks_count, freed);
  1840. spin_unlock(sb_bgl_lock(sbi, group));
  1841. percpu_counter_add(&sbi->s_freeblocks_counter, freed);
  1842. start = next;
  1843. if (err < 0) {
  1844. if (err != -EOPNOTSUPP)
  1845. ext3_warning(sb, __func__, "Discard command "
  1846. "returned error %d\n", err);
  1847. break;
  1848. }
  1849. if (fatal_signal_pending(current)) {
  1850. err = -ERESTARTSYS;
  1851. break;
  1852. }
  1853. cond_resched();
  1854. /* No more suitable extents */
  1855. if (free_blocks < minblocks)
  1856. break;
  1857. }
  1858. /* We dirtied the bitmap block */
  1859. BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
  1860. ret = ext3_journal_dirty_metadata(handle, bitmap_bh);
  1861. if (!err)
  1862. err = ret;
  1863. /* And the group descriptor block */
  1864. BUFFER_TRACE(gdp_bh, "dirtied group descriptor block");
  1865. ret = ext3_journal_dirty_metadata(handle, gdp_bh);
  1866. if (!err)
  1867. err = ret;
  1868. ext3_debug("trimmed %d blocks in the group %d\n",
  1869. count, group);
  1870. err_out:
  1871. if (err)
  1872. count = err;
  1873. ext3_journal_stop(handle);
  1874. brelse(bitmap_bh);
  1875. return count;
  1876. }
  1877. /**
  1878. * ext3_trim_fs() -- trim ioctl handle function
  1879. * @sb: superblock for filesystem
  1880. * @start: First Byte to trim
  1881. * @len: number of Bytes to trim from start
  1882. * @minlen: minimum extent length in Bytes
  1883. *
  1884. * ext3_trim_fs goes through all allocation groups containing Bytes from
  1885. * start to start+len. For each such a group ext3_trim_all_free function
  1886. * is invoked to trim all free space.
  1887. */
  1888. int ext3_trim_fs(struct super_block *sb, struct fstrim_range *range)
  1889. {
  1890. ext3_grpblk_t last_block, first_block, free_blocks;
  1891. unsigned long first_group, last_group;
  1892. unsigned long group, ngroups;
  1893. struct ext3_group_desc *gdp;
  1894. struct ext3_super_block *es = EXT3_SB(sb)->s_es;
  1895. uint64_t start, len, minlen, trimmed;
  1896. ext3_fsblk_t max_blks = le32_to_cpu(es->s_blocks_count);
  1897. int ret = 0;
  1898. start = (range->start >> sb->s_blocksize_bits) +
  1899. le32_to_cpu(es->s_first_data_block);
  1900. len = range->len >> sb->s_blocksize_bits;
  1901. minlen = range->minlen >> sb->s_blocksize_bits;
  1902. trimmed = 0;
  1903. if (unlikely(minlen > EXT3_BLOCKS_PER_GROUP(sb)))
  1904. return -EINVAL;
  1905. if (start >= max_blks)
  1906. goto out;
  1907. if (start + len > max_blks)
  1908. len = max_blks - start;
  1909. ngroups = EXT3_SB(sb)->s_groups_count;
  1910. smp_rmb();
  1911. /* Determine first and last group to examine based on start and len */
  1912. ext3_get_group_no_and_offset(sb, (ext3_fsblk_t) start,
  1913. &first_group, &first_block);
  1914. ext3_get_group_no_and_offset(sb, (ext3_fsblk_t) (start + len),
  1915. &last_group, &last_block);
  1916. last_group = (last_group > ngroups - 1) ? ngroups - 1 : last_group;
  1917. last_block = EXT3_BLOCKS_PER_GROUP(sb);
  1918. if (first_group > last_group)
  1919. return -EINVAL;
  1920. for (group = first_group; group <= last_group; group++) {
  1921. gdp = ext3_get_group_desc(sb, group, NULL);
  1922. if (!gdp)
  1923. break;
  1924. free_blocks = le16_to_cpu(gdp->bg_free_blocks_count);
  1925. if (free_blocks < minlen)
  1926. continue;
  1927. /*
  1928. * For all the groups except the last one, last block will
  1929. * always be EXT3_BLOCKS_PER_GROUP(sb), so we only need to
  1930. * change it for the last group in which case first_block +
  1931. * len < EXT3_BLOCKS_PER_GROUP(sb).
  1932. */
  1933. if (first_block + len < EXT3_BLOCKS_PER_GROUP(sb))
  1934. last_block = first_block + len;
  1935. len -= last_block - first_block;
  1936. ret = ext3_trim_all_free(sb, group, first_block,
  1937. last_block, minlen);
  1938. if (ret < 0)
  1939. break;
  1940. trimmed += ret;
  1941. first_block = 0;
  1942. }
  1943. if (ret >= 0)
  1944. ret = 0;
  1945. out:
  1946. range->len = trimmed * sb->s_blocksize;
  1947. return ret;
  1948. }