budget.c 23 KB

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