budget.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc., 51
  17. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * Authors: Adrian Hunter
  20. * Artem Bityutskiy (Битюцкий Артём)
  21. */
  22. /*
  23. * This file implements the budgeting sub-system which is responsible for UBIFS
  24. * space management.
  25. *
  26. * Factors such as compression, wasted space at the ends of LEBs, space in other
  27. * journal heads, the effect of updates on the index, and so on, make it
  28. * impossible to accurately predict the amount of space needed. Consequently
  29. * approximations are used.
  30. */
  31. #include "ubifs.h"
  32. #include <linux/writeback.h>
  33. #include <asm/div64.h>
  34. /*
  35. * When pessimistic budget calculations say that there is no enough space,
  36. * UBIFS starts writing back dirty inodes and pages, doing garbage collection,
  37. * or committing. The below constants define maximum number of times UBIFS
  38. * repeats the operations.
  39. */
  40. #define MAX_SHRINK_RETRIES 8
  41. #define MAX_GC_RETRIES 4
  42. #define MAX_CMT_RETRIES 2
  43. #define MAX_NOSPC_RETRIES 1
  44. /*
  45. * The below constant defines amount of dirty pages which should be written
  46. * back at when trying to shrink the liability.
  47. */
  48. #define NR_TO_WRITE 16
  49. /**
  50. * struct retries_info - information about re-tries while making free space.
  51. * @prev_liability: previous liability
  52. * @shrink_cnt: how many times the liability was shrinked
  53. * @shrink_retries: count of liability shrink re-tries (increased when
  54. * liability does not shrink)
  55. * @try_gc: GC should be tried first
  56. * @gc_retries: how many times GC was run
  57. * @cmt_retries: how many times commit has been done
  58. * @nospc_retries: how many times GC returned %-ENOSPC
  59. *
  60. * Since we consider budgeting to be the fast-path, and this structure has to
  61. * be allocated on stack and zeroed out, we make it smaller using bit-fields.
  62. */
  63. struct retries_info {
  64. long long prev_liability;
  65. unsigned int shrink_cnt;
  66. unsigned int shrink_retries:5;
  67. unsigned int try_gc:1;
  68. unsigned int gc_retries:4;
  69. unsigned int cmt_retries:3;
  70. unsigned int nospc_retries:1;
  71. };
  72. /**
  73. * shrink_liability - write-back some dirty pages/inodes.
  74. * @c: UBIFS file-system description object
  75. * @nr_to_write: how many dirty pages to write-back
  76. *
  77. * This function shrinks UBIFS liability by means of writing back some amount
  78. * of dirty inodes and their pages. Returns the amount of pages which were
  79. * written back. The returned value does not include dirty inodes which were
  80. * synchronized.
  81. *
  82. * Note, this function synchronizes even VFS inodes which are locked
  83. * (@i_mutex) by the caller of the budgeting function, because write-back does
  84. * not touch @i_mutex.
  85. */
  86. static int shrink_liability(struct ubifs_info *c, int nr_to_write)
  87. {
  88. int nr_written;
  89. struct writeback_control wbc = {
  90. .sync_mode = WB_SYNC_NONE,
  91. .range_end = LLONG_MAX,
  92. .nr_to_write = nr_to_write,
  93. };
  94. generic_sync_sb_inodes(c->vfs_sb, &wbc);
  95. nr_written = nr_to_write - wbc.nr_to_write;
  96. if (!nr_written) {
  97. /*
  98. * Re-try again but wait on pages/inodes which are being
  99. * written-back concurrently (e.g., by pdflush).
  100. */
  101. memset(&wbc, 0, sizeof(struct writeback_control));
  102. wbc.sync_mode = WB_SYNC_ALL;
  103. wbc.range_end = LLONG_MAX;
  104. wbc.nr_to_write = nr_to_write;
  105. generic_sync_sb_inodes(c->vfs_sb, &wbc);
  106. nr_written = nr_to_write - wbc.nr_to_write;
  107. }
  108. dbg_budg("%d pages were written back", nr_written);
  109. return nr_written;
  110. }
  111. /**
  112. * run_gc - run garbage collector.
  113. * @c: UBIFS file-system description object
  114. *
  115. * This function runs garbage collector to make some more free space. Returns
  116. * zero if a free LEB has been produced, %-EAGAIN if commit is required, and a
  117. * negative error code in case of failure.
  118. */
  119. static int run_gc(struct ubifs_info *c)
  120. {
  121. int err, lnum;
  122. /* Make some free space by garbage-collecting dirty space */
  123. down_read(&c->commit_sem);
  124. lnum = ubifs_garbage_collect(c, 1);
  125. up_read(&c->commit_sem);
  126. if (lnum < 0)
  127. return lnum;
  128. /* GC freed one LEB, return it to lprops */
  129. dbg_budg("GC freed LEB %d", lnum);
  130. err = ubifs_return_leb(c, lnum);
  131. if (err)
  132. return err;
  133. return 0;
  134. }
  135. /**
  136. * make_free_space - make more free space on the file-system.
  137. * @c: UBIFS file-system description object
  138. * @ri: information about previous invocations of this function
  139. *
  140. * This function is called when an operation cannot be budgeted because there
  141. * is supposedly no free space. But in most cases there is some free space:
  142. * o budgeting is pessimistic, so it always budgets more then it is actually
  143. * needed, so shrinking the liability is one way to make free space - the
  144. * cached data will take less space then it was budgeted for;
  145. * o GC may turn some dark space into free space (budgeting treats dark space
  146. * as not available);
  147. * o commit may free some LEB, i.e., turn freeable LEBs into free LEBs.
  148. *
  149. * So this function tries to do the above. Returns %-EAGAIN if some free space
  150. * was presumably made and the caller has to re-try budgeting the operation.
  151. * Returns %-ENOSPC if it couldn't do more free space, and other negative error
  152. * codes on failures.
  153. */
  154. static int make_free_space(struct ubifs_info *c, struct retries_info *ri)
  155. {
  156. int err;
  157. /*
  158. * If we have some dirty pages and inodes (liability), try to write
  159. * them back unless this was tried too many times without effect
  160. * already.
  161. */
  162. if (ri->shrink_retries < MAX_SHRINK_RETRIES && !ri->try_gc) {
  163. long long liability;
  164. spin_lock(&c->space_lock);
  165. liability = c->budg_idx_growth + c->budg_data_growth +
  166. c->budg_dd_growth;
  167. spin_unlock(&c->space_lock);
  168. if (ri->prev_liability >= liability) {
  169. /* Liability does not shrink, next time try GC then */
  170. ri->shrink_retries += 1;
  171. if (ri->gc_retries < MAX_GC_RETRIES)
  172. ri->try_gc = 1;
  173. dbg_budg("liability did not shrink: retries %d of %d",
  174. ri->shrink_retries, MAX_SHRINK_RETRIES);
  175. }
  176. dbg_budg("force write-back (count %d)", ri->shrink_cnt);
  177. shrink_liability(c, NR_TO_WRITE + ri->shrink_cnt);
  178. ri->prev_liability = liability;
  179. ri->shrink_cnt += 1;
  180. return -EAGAIN;
  181. }
  182. /*
  183. * Try to run garbage collector unless it was already tried too many
  184. * times.
  185. */
  186. if (ri->gc_retries < MAX_GC_RETRIES) {
  187. ri->gc_retries += 1;
  188. dbg_budg("run GC, retries %d of %d",
  189. ri->gc_retries, MAX_GC_RETRIES);
  190. ri->try_gc = 0;
  191. err = run_gc(c);
  192. if (!err)
  193. return -EAGAIN;
  194. if (err == -EAGAIN) {
  195. dbg_budg("GC asked to commit");
  196. err = ubifs_run_commit(c);
  197. if (err)
  198. return err;
  199. return -EAGAIN;
  200. }
  201. if (err != -ENOSPC)
  202. return err;
  203. /*
  204. * GC could not make any progress. If this is the first time,
  205. * then it makes sense to try to commit, because it might make
  206. * some dirty space.
  207. */
  208. dbg_budg("GC returned -ENOSPC, retries %d",
  209. ri->nospc_retries);
  210. if (ri->nospc_retries >= MAX_NOSPC_RETRIES)
  211. return err;
  212. ri->nospc_retries += 1;
  213. }
  214. /* Neither GC nor write-back helped, try to commit */
  215. if (ri->cmt_retries < MAX_CMT_RETRIES) {
  216. ri->cmt_retries += 1;
  217. dbg_budg("run commit, retries %d of %d",
  218. ri->cmt_retries, MAX_CMT_RETRIES);
  219. err = ubifs_run_commit(c);
  220. if (err)
  221. return err;
  222. return -EAGAIN;
  223. }
  224. return -ENOSPC;
  225. }
  226. /**
  227. * ubifs_calc_min_idx_lebs - calculate amount of eraseblocks for the index.
  228. * @c: UBIFS file-system description object
  229. *
  230. * This function calculates and returns the number of eraseblocks which should
  231. * be kept for index usage.
  232. */
  233. int ubifs_calc_min_idx_lebs(struct ubifs_info *c)
  234. {
  235. int ret;
  236. uint64_t idx_size;
  237. idx_size = c->old_idx_sz + c->budg_idx_growth + c->budg_uncommitted_idx;
  238. /* And make sure we have twice the index size of space reserved */
  239. idx_size <<= 1;
  240. /*
  241. * We do not maintain 'old_idx_size' as 'old_idx_lebs'/'old_idx_bytes'
  242. * pair, nor similarly the two variables for the new index size, so we
  243. * have to do this costly 64-bit division on fast-path.
  244. */
  245. if (do_div(idx_size, c->leb_size - c->max_idx_node_sz))
  246. ret = idx_size + 1;
  247. else
  248. ret = idx_size;
  249. /*
  250. * The index head is not available for the in-the-gaps method, so add an
  251. * extra LEB to compensate.
  252. */
  253. ret += 1;
  254. /*
  255. * At present the index needs at least 2 LEBs: one for the index head
  256. * and one for in-the-gaps method (which currently does not cater for
  257. * the index head and so excludes it from consideration).
  258. */
  259. if (ret < 2)
  260. ret = 2;
  261. return ret;
  262. }
  263. /**
  264. * ubifs_calc_available - calculate available FS space.
  265. * @c: UBIFS file-system description object
  266. * @min_idx_lebs: minimum number of LEBs reserved for the index
  267. *
  268. * This function calculates and returns amount of FS space available for use.
  269. */
  270. long long ubifs_calc_available(const struct ubifs_info *c, int min_idx_lebs)
  271. {
  272. int subtract_lebs;
  273. long long available;
  274. /*
  275. * Force the amount available to the total size reported if the used
  276. * space is zero.
  277. */
  278. if (c->lst.total_used <= UBIFS_INO_NODE_SZ &&
  279. c->budg_data_growth + c->budg_dd_growth == 0) {
  280. /* Do the same calculation as for c->block_cnt */
  281. available = c->main_lebs - 2;
  282. available *= c->leb_size - c->dark_wm;
  283. return available;
  284. }
  285. available = c->main_bytes - c->lst.total_used;
  286. /*
  287. * Now 'available' contains theoretically available flash space
  288. * assuming there is no index, so we have to subtract the space which
  289. * is reserved for the index.
  290. */
  291. subtract_lebs = min_idx_lebs;
  292. /* Take into account that GC reserves one LEB for its own needs */
  293. subtract_lebs += 1;
  294. /*
  295. * The GC journal head LEB is not really accessible. And since
  296. * different write types go to different heads, we may count only on
  297. * one head's space.
  298. */
  299. subtract_lebs += c->jhead_cnt - 1;
  300. /* We also reserve one LEB for deletions, which bypass budgeting */
  301. subtract_lebs += 1;
  302. available -= (long long)subtract_lebs * c->leb_size;
  303. /* Subtract the dead space which is not available for use */
  304. available -= c->lst.total_dead;
  305. /*
  306. * Subtract dark space, which might or might not be usable - it depends
  307. * on the data which we have on the media and which will be written. If
  308. * this is a lot of uncompressed or not-compressible data, the dark
  309. * space cannot be used.
  310. */
  311. available -= c->lst.total_dark;
  312. /*
  313. * However, there is more dark space. The index may be bigger than
  314. * @min_idx_lebs. Those extra LEBs are assumed to be available, but
  315. * their dark space is not included in total_dark, so it is subtracted
  316. * here.
  317. */
  318. if (c->lst.idx_lebs > min_idx_lebs) {
  319. subtract_lebs = c->lst.idx_lebs - min_idx_lebs;
  320. available -= subtract_lebs * c->dark_wm;
  321. }
  322. /* The calculations are rough and may end up with a negative number */
  323. return available > 0 ? available : 0;
  324. }
  325. /**
  326. * can_use_rp - check whether the user is allowed to use reserved pool.
  327. * @c: UBIFS file-system description object
  328. *
  329. * UBIFS has so-called "reserved pool" which is flash space reserved
  330. * for the superuser and for uses whose UID/GID is recorded in UBIFS superblock.
  331. * This function checks whether current user is allowed to use reserved pool.
  332. * Returns %1 current user is allowed to use reserved pool and %0 otherwise.
  333. */
  334. static int can_use_rp(struct ubifs_info *c)
  335. {
  336. if (current->fsuid == c->rp_uid || capable(CAP_SYS_RESOURCE) ||
  337. (c->rp_gid != 0 && in_group_p(c->rp_gid)))
  338. return 1;
  339. return 0;
  340. }
  341. /**
  342. * do_budget_space - reserve flash space for index and data growth.
  343. * @c: UBIFS file-system description object
  344. *
  345. * This function makes sure UBIFS has enough free eraseblocks for index growth
  346. * and data.
  347. *
  348. * When budgeting index space, UBIFS reserves twice as more LEBs as the index
  349. * would take if it was consolidated and written to the flash. This guarantees
  350. * that the "in-the-gaps" commit method always succeeds and UBIFS will always
  351. * be able to commit dirty index. So this function basically adds amount of
  352. * budgeted index space to the size of the current index, multiplies this by 2,
  353. * and makes sure this does not exceed the amount of free eraseblocks.
  354. *
  355. * Notes about @c->min_idx_lebs and @c->lst.idx_lebs variables:
  356. * o @c->lst.idx_lebs is the number of LEBs the index currently uses. It might
  357. * be large, because UBIFS does not do any index consolidation as long as
  358. * there is free space. IOW, the index may take a lot of LEBs, but the LEBs
  359. * will contain a lot of dirt.
  360. * o @c->min_idx_lebs is the the index presumably takes. IOW, the index may be
  361. * consolidated to take up to @c->min_idx_lebs LEBs.
  362. *
  363. * This function returns zero in case of success, and %-ENOSPC in case of
  364. * failure.
  365. */
  366. static int do_budget_space(struct ubifs_info *c)
  367. {
  368. long long outstanding, available;
  369. int lebs, rsvd_idx_lebs, min_idx_lebs;
  370. /* First budget index space */
  371. min_idx_lebs = ubifs_calc_min_idx_lebs(c);
  372. /* Now 'min_idx_lebs' contains number of LEBs to reserve */
  373. if (min_idx_lebs > c->lst.idx_lebs)
  374. rsvd_idx_lebs = min_idx_lebs - c->lst.idx_lebs;
  375. else
  376. rsvd_idx_lebs = 0;
  377. /*
  378. * The number of LEBs that are available to be used by the index is:
  379. *
  380. * @c->lst.empty_lebs + @c->freeable_cnt + @c->idx_gc_cnt -
  381. * @c->lst.taken_empty_lebs
  382. *
  383. * @empty_lebs are available because they are empty. @freeable_cnt are
  384. * available because they contain only free and dirty space and the
  385. * index allocation always occurs after wbufs are synch'ed.
  386. * @idx_gc_cnt are available because they are index LEBs that have been
  387. * garbage collected (including trivial GC) and are awaiting the commit
  388. * before they can be unmapped - note that the in-the-gaps method will
  389. * grab these if it needs them. @taken_empty_lebs are empty_lebs that
  390. * have already been allocated for some purpose (also includes those
  391. * LEBs on the @idx_gc list).
  392. *
  393. * Note, @taken_empty_lebs may temporarily be higher by one because of
  394. * the way we serialize LEB allocations and budgeting. See a comment in
  395. * 'ubifs_find_free_space()'.
  396. */
  397. lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
  398. c->lst.taken_empty_lebs;
  399. if (unlikely(rsvd_idx_lebs > lebs)) {
  400. dbg_budg("out of indexing space: min_idx_lebs %d (old %d), "
  401. "rsvd_idx_lebs %d", min_idx_lebs, c->min_idx_lebs,
  402. rsvd_idx_lebs);
  403. return -ENOSPC;
  404. }
  405. available = ubifs_calc_available(c, min_idx_lebs);
  406. outstanding = c->budg_data_growth + c->budg_dd_growth;
  407. if (unlikely(available < outstanding)) {
  408. dbg_budg("out of data space: available %lld, outstanding %lld",
  409. available, outstanding);
  410. return -ENOSPC;
  411. }
  412. if (available - outstanding <= c->rp_size && !can_use_rp(c))
  413. return -ENOSPC;
  414. c->min_idx_lebs = min_idx_lebs;
  415. return 0;
  416. }
  417. /**
  418. * calc_idx_growth - calculate approximate index growth from budgeting request.
  419. * @c: UBIFS file-system description object
  420. * @req: budgeting request
  421. *
  422. * For now we assume each new node adds one znode. But this is rather poor
  423. * approximation, though.
  424. */
  425. static int calc_idx_growth(const struct ubifs_info *c,
  426. const struct ubifs_budget_req *req)
  427. {
  428. int znodes;
  429. znodes = req->new_ino + (req->new_page << UBIFS_BLOCKS_PER_PAGE_SHIFT) +
  430. req->new_dent;
  431. return znodes * c->max_idx_node_sz;
  432. }
  433. /**
  434. * calc_data_growth - calculate approximate amount of new data from budgeting
  435. * request.
  436. * @c: UBIFS file-system description object
  437. * @req: budgeting request
  438. */
  439. static int calc_data_growth(const struct ubifs_info *c,
  440. const struct ubifs_budget_req *req)
  441. {
  442. int data_growth;
  443. data_growth = req->new_ino ? c->inode_budget : 0;
  444. if (req->new_page)
  445. data_growth += c->page_budget;
  446. if (req->new_dent)
  447. data_growth += c->dent_budget;
  448. data_growth += req->new_ino_d;
  449. return data_growth;
  450. }
  451. /**
  452. * calc_dd_growth - calculate approximate amount of data which makes other data
  453. * dirty from budgeting request.
  454. * @c: UBIFS file-system description object
  455. * @req: budgeting request
  456. */
  457. static int calc_dd_growth(const struct ubifs_info *c,
  458. const struct ubifs_budget_req *req)
  459. {
  460. int dd_growth;
  461. dd_growth = req->dirtied_page ? c->page_budget : 0;
  462. if (req->dirtied_ino)
  463. dd_growth += c->inode_budget << (req->dirtied_ino - 1);
  464. if (req->mod_dent)
  465. dd_growth += c->dent_budget;
  466. dd_growth += req->dirtied_ino_d;
  467. return dd_growth;
  468. }
  469. /**
  470. * ubifs_budget_space - ensure there is enough space to complete an operation.
  471. * @c: UBIFS file-system description object
  472. * @req: budget request
  473. *
  474. * This function allocates budget for an operation. It uses pessimistic
  475. * approximation of how much flash space the operation needs. The goal of this
  476. * function is to make sure UBIFS always has flash space to flush all dirty
  477. * pages, dirty inodes, and dirty znodes (liability). This function may force
  478. * commit, garbage-collection or write-back. Returns zero in case of success,
  479. * %-ENOSPC if there is no free space and other negative error codes in case of
  480. * failures.
  481. */
  482. int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req)
  483. {
  484. int uninitialized_var(cmt_retries), uninitialized_var(wb_retries);
  485. int err, idx_growth, data_growth, dd_growth;
  486. struct retries_info ri;
  487. ubifs_assert(req->dirtied_ino <= 4);
  488. ubifs_assert(req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4);
  489. data_growth = calc_data_growth(c, req);
  490. dd_growth = calc_dd_growth(c, req);
  491. if (!data_growth && !dd_growth)
  492. return 0;
  493. idx_growth = calc_idx_growth(c, req);
  494. memset(&ri, 0, sizeof(struct retries_info));
  495. again:
  496. spin_lock(&c->space_lock);
  497. ubifs_assert(c->budg_idx_growth >= 0);
  498. ubifs_assert(c->budg_data_growth >= 0);
  499. ubifs_assert(c->budg_dd_growth >= 0);
  500. if (unlikely(c->nospace) && (c->nospace_rp || !can_use_rp(c))) {
  501. dbg_budg("no space");
  502. spin_unlock(&c->space_lock);
  503. return -ENOSPC;
  504. }
  505. c->budg_idx_growth += idx_growth;
  506. c->budg_data_growth += data_growth;
  507. c->budg_dd_growth += dd_growth;
  508. err = do_budget_space(c);
  509. if (likely(!err)) {
  510. req->idx_growth = idx_growth;
  511. req->data_growth = data_growth;
  512. req->dd_growth = dd_growth;
  513. spin_unlock(&c->space_lock);
  514. return 0;
  515. }
  516. /* Restore the old values */
  517. c->budg_idx_growth -= idx_growth;
  518. c->budg_data_growth -= data_growth;
  519. c->budg_dd_growth -= dd_growth;
  520. spin_unlock(&c->space_lock);
  521. if (req->fast) {
  522. dbg_budg("no space for fast budgeting");
  523. return err;
  524. }
  525. err = make_free_space(c, &ri);
  526. if (err == -EAGAIN) {
  527. dbg_budg("try again");
  528. cond_resched();
  529. goto again;
  530. } else if (err == -ENOSPC) {
  531. dbg_budg("FS is full, -ENOSPC");
  532. c->nospace = 1;
  533. if (can_use_rp(c) || c->rp_size == 0)
  534. c->nospace_rp = 1;
  535. smp_wmb();
  536. } else
  537. ubifs_err("cannot budget space, error %d", err);
  538. return err;
  539. }
  540. /**
  541. * ubifs_release_budget - release budgeted free space.
  542. * @c: UBIFS file-system description object
  543. * @req: budget request
  544. *
  545. * This function releases the space budgeted by 'ubifs_budget_space()'. Note,
  546. * since the index changes (which were budgeted for in @req->idx_growth) will
  547. * only be written to the media on commit, this function moves the index budget
  548. * from @c->budg_idx_growth to @c->budg_uncommitted_idx. The latter will be
  549. * zeroed by the commit operation.
  550. */
  551. void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req)
  552. {
  553. ubifs_assert(req->dirtied_ino <= 4);
  554. ubifs_assert(req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4);
  555. if (!req->recalculate) {
  556. ubifs_assert(req->idx_growth >= 0);
  557. ubifs_assert(req->data_growth >= 0);
  558. ubifs_assert(req->dd_growth >= 0);
  559. }
  560. if (req->recalculate) {
  561. req->data_growth = calc_data_growth(c, req);
  562. req->dd_growth = calc_dd_growth(c, req);
  563. req->idx_growth = calc_idx_growth(c, req);
  564. }
  565. if (!req->data_growth && !req->dd_growth)
  566. return;
  567. c->nospace = c->nospace_rp = 0;
  568. smp_wmb();
  569. spin_lock(&c->space_lock);
  570. c->budg_idx_growth -= req->idx_growth;
  571. c->budg_uncommitted_idx += req->idx_growth;
  572. c->budg_data_growth -= req->data_growth;
  573. c->budg_dd_growth -= req->dd_growth;
  574. c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
  575. ubifs_assert(c->budg_idx_growth >= 0);
  576. ubifs_assert(c->budg_data_growth >= 0);
  577. ubifs_assert(c->min_idx_lebs < c->main_lebs);
  578. spin_unlock(&c->space_lock);
  579. }
  580. /**
  581. * ubifs_convert_page_budget - convert budget of a new page.
  582. * @c: UBIFS file-system description object
  583. *
  584. * This function converts budget which was allocated for a new page of data to
  585. * the budget of changing an existing page of data. The latter is smaller then
  586. * the former, so this function only does simple re-calculation and does not
  587. * involve any write-back.
  588. */
  589. void ubifs_convert_page_budget(struct ubifs_info *c)
  590. {
  591. spin_lock(&c->space_lock);
  592. /* Release the index growth reservation */
  593. c->budg_idx_growth -= c->max_idx_node_sz << UBIFS_BLOCKS_PER_PAGE_SHIFT;
  594. /* Release the data growth reservation */
  595. c->budg_data_growth -= c->page_budget;
  596. /* Increase the dirty data growth reservation instead */
  597. c->budg_dd_growth += c->page_budget;
  598. /* And re-calculate the indexing space reservation */
  599. c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
  600. spin_unlock(&c->space_lock);
  601. }
  602. /**
  603. * ubifs_release_dirty_inode_budget - release dirty inode budget.
  604. * @c: UBIFS file-system description object
  605. * @ui: UBIFS inode to release the budget for
  606. *
  607. * This function releases budget corresponding to a dirty inode. It is usually
  608. * called when after the inode has been written to the media and marked as
  609. * clean.
  610. */
  611. void ubifs_release_dirty_inode_budget(struct ubifs_info *c,
  612. struct ubifs_inode *ui)
  613. {
  614. struct ubifs_budget_req req = {.dd_growth = c->inode_budget,
  615. .dirtied_ino_d = ui->data_len};
  616. ubifs_release_budget(c, &req);
  617. }
  618. /**
  619. * ubifs_budg_get_free_space - return amount of free space.
  620. * @c: UBIFS file-system description object
  621. *
  622. * This function returns amount of free space on the file-system.
  623. */
  624. long long ubifs_budg_get_free_space(struct ubifs_info *c)
  625. {
  626. int min_idx_lebs, rsvd_idx_lebs;
  627. long long available, outstanding, free;
  628. /* Do exactly the same calculations as in 'do_budget_space()' */
  629. spin_lock(&c->space_lock);
  630. min_idx_lebs = ubifs_calc_min_idx_lebs(c);
  631. if (min_idx_lebs > c->lst.idx_lebs)
  632. rsvd_idx_lebs = min_idx_lebs - c->lst.idx_lebs;
  633. else
  634. rsvd_idx_lebs = 0;
  635. if (rsvd_idx_lebs > c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt
  636. - c->lst.taken_empty_lebs) {
  637. spin_unlock(&c->space_lock);
  638. return 0;
  639. }
  640. available = ubifs_calc_available(c, min_idx_lebs);
  641. outstanding = c->budg_data_growth + c->budg_dd_growth;
  642. c->min_idx_lebs = min_idx_lebs;
  643. spin_unlock(&c->space_lock);
  644. if (available > outstanding)
  645. free = ubifs_reported_space(c, available - outstanding);
  646. else
  647. free = 0;
  648. return free;
  649. }