budget.c 24 KB

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