budget.c 24 KB

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