budget.c 23 KB

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