budget.c 26 KB

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