balloc.c 62 KB

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