budget.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. * @c->lst.empty_lebs are available because they are empty.
  373. * @c->freeable_cnt are available because they contain only free and
  374. * dirty space, @c->idx_gc_cnt are available because they are index
  375. * LEBs that have been garbage collected and are awaiting the commit
  376. * before they can be used. And the in-the-gaps method will grab these
  377. * if it needs them. @c->lst.taken_empty_lebs are empty LEBs that have
  378. * already been allocated for some purpose.
  379. *
  380. * Note, @c->idx_gc_cnt is included to both @c->lst.empty_lebs (because
  381. * these LEBs are empty) and to @c->lst.taken_empty_lebs (because they
  382. * are taken until after the commit).
  383. *
  384. * Note, @c->lst.taken_empty_lebs may temporarily be higher by one
  385. * because of the way we serialize LEB allocations and budgeting. See a
  386. * comment in 'ubifs_find_free_space()'.
  387. */
  388. lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
  389. c->lst.taken_empty_lebs;
  390. if (unlikely(rsvd_idx_lebs > lebs)) {
  391. dbg_budg("out of indexing space: min_idx_lebs %d (old %d), "
  392. "rsvd_idx_lebs %d", min_idx_lebs, c->min_idx_lebs,
  393. rsvd_idx_lebs);
  394. return -ENOSPC;
  395. }
  396. available = ubifs_calc_available(c, min_idx_lebs);
  397. outstanding = c->budg_data_growth + c->budg_dd_growth;
  398. if (unlikely(available < outstanding)) {
  399. dbg_budg("out of data space: available %lld, outstanding %lld",
  400. available, outstanding);
  401. return -ENOSPC;
  402. }
  403. if (available - outstanding <= c->rp_size && !can_use_rp(c))
  404. return -ENOSPC;
  405. c->min_idx_lebs = min_idx_lebs;
  406. return 0;
  407. }
  408. /**
  409. * calc_idx_growth - calculate approximate index growth from budgeting request.
  410. * @c: UBIFS file-system description object
  411. * @req: budgeting request
  412. *
  413. * For now we assume each new node adds one znode. But this is rather poor
  414. * approximation, though.
  415. */
  416. static int calc_idx_growth(const struct ubifs_info *c,
  417. const struct ubifs_budget_req *req)
  418. {
  419. int znodes;
  420. znodes = req->new_ino + (req->new_page << UBIFS_BLOCKS_PER_PAGE_SHIFT) +
  421. req->new_dent;
  422. return znodes * c->max_idx_node_sz;
  423. }
  424. /**
  425. * calc_data_growth - calculate approximate amount of new data from budgeting
  426. * request.
  427. * @c: UBIFS file-system description object
  428. * @req: budgeting request
  429. */
  430. static int calc_data_growth(const struct ubifs_info *c,
  431. const struct ubifs_budget_req *req)
  432. {
  433. int data_growth;
  434. data_growth = req->new_ino ? c->inode_budget : 0;
  435. if (req->new_page)
  436. data_growth += c->page_budget;
  437. if (req->new_dent)
  438. data_growth += c->dent_budget;
  439. data_growth += req->new_ino_d;
  440. return data_growth;
  441. }
  442. /**
  443. * calc_dd_growth - calculate approximate amount of data which makes other data
  444. * dirty from budgeting request.
  445. * @c: UBIFS file-system description object
  446. * @req: budgeting request
  447. */
  448. static int calc_dd_growth(const struct ubifs_info *c,
  449. const struct ubifs_budget_req *req)
  450. {
  451. int dd_growth;
  452. dd_growth = req->dirtied_page ? c->page_budget : 0;
  453. if (req->dirtied_ino)
  454. dd_growth += c->inode_budget << (req->dirtied_ino - 1);
  455. if (req->mod_dent)
  456. dd_growth += c->dent_budget;
  457. dd_growth += req->dirtied_ino_d;
  458. return dd_growth;
  459. }
  460. /**
  461. * ubifs_budget_space - ensure there is enough space to complete an operation.
  462. * @c: UBIFS file-system description object
  463. * @req: budget request
  464. *
  465. * This function allocates budget for an operation. It uses pessimistic
  466. * approximation of how much flash space the operation needs. The goal of this
  467. * function is to make sure UBIFS always has flash space to flush all dirty
  468. * pages, dirty inodes, and dirty znodes (liability). This function may force
  469. * commit, garbage-collection or write-back. Returns zero in case of success,
  470. * %-ENOSPC if there is no free space and other negative error codes in case of
  471. * failures.
  472. */
  473. int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req)
  474. {
  475. int uninitialized_var(cmt_retries), uninitialized_var(wb_retries);
  476. int err, idx_growth, data_growth, dd_growth;
  477. struct retries_info ri;
  478. ubifs_assert(req->new_page <= 1);
  479. ubifs_assert(req->dirtied_page <= 1);
  480. ubifs_assert(req->new_dent <= 1);
  481. ubifs_assert(req->mod_dent <= 1);
  482. ubifs_assert(req->new_ino <= 1);
  483. ubifs_assert(req->new_ino_d <= UBIFS_MAX_INO_DATA);
  484. ubifs_assert(req->dirtied_ino <= 4);
  485. ubifs_assert(req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4);
  486. ubifs_assert(!(req->new_ino_d & 7));
  487. ubifs_assert(!(req->dirtied_ino_d & 7));
  488. data_growth = calc_data_growth(c, req);
  489. dd_growth = calc_dd_growth(c, req);
  490. if (!data_growth && !dd_growth)
  491. return 0;
  492. idx_growth = calc_idx_growth(c, req);
  493. memset(&ri, 0, sizeof(struct retries_info));
  494. again:
  495. spin_lock(&c->space_lock);
  496. ubifs_assert(c->budg_idx_growth >= 0);
  497. ubifs_assert(c->budg_data_growth >= 0);
  498. ubifs_assert(c->budg_dd_growth >= 0);
  499. if (unlikely(c->nospace) && (c->nospace_rp || !can_use_rp(c))) {
  500. dbg_budg("no space");
  501. spin_unlock(&c->space_lock);
  502. return -ENOSPC;
  503. }
  504. c->budg_idx_growth += idx_growth;
  505. c->budg_data_growth += data_growth;
  506. c->budg_dd_growth += dd_growth;
  507. err = do_budget_space(c);
  508. if (likely(!err)) {
  509. req->idx_growth = idx_growth;
  510. req->data_growth = data_growth;
  511. req->dd_growth = dd_growth;
  512. spin_unlock(&c->space_lock);
  513. return 0;
  514. }
  515. /* Restore the old values */
  516. c->budg_idx_growth -= idx_growth;
  517. c->budg_data_growth -= data_growth;
  518. c->budg_dd_growth -= dd_growth;
  519. spin_unlock(&c->space_lock);
  520. if (req->fast) {
  521. dbg_budg("no space for fast budgeting");
  522. return err;
  523. }
  524. err = make_free_space(c, &ri);
  525. if (err == -EAGAIN) {
  526. dbg_budg("try again");
  527. cond_resched();
  528. goto again;
  529. } else if (err == -ENOSPC) {
  530. dbg_budg("FS is full, -ENOSPC");
  531. c->nospace = 1;
  532. if (can_use_rp(c) || c->rp_size == 0)
  533. c->nospace_rp = 1;
  534. smp_wmb();
  535. } else
  536. ubifs_err("cannot budget space, error %d", err);
  537. return err;
  538. }
  539. /**
  540. * ubifs_release_budget - release budgeted free space.
  541. * @c: UBIFS file-system description object
  542. * @req: budget request
  543. *
  544. * This function releases the space budgeted by 'ubifs_budget_space()'. Note,
  545. * since the index changes (which were budgeted for in @req->idx_growth) will
  546. * only be written to the media on commit, this function moves the index budget
  547. * from @c->budg_idx_growth to @c->budg_uncommitted_idx. The latter will be
  548. * zeroed by the commit operation.
  549. */
  550. void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req)
  551. {
  552. ubifs_assert(req->new_page <= 1);
  553. ubifs_assert(req->dirtied_page <= 1);
  554. ubifs_assert(req->new_dent <= 1);
  555. ubifs_assert(req->mod_dent <= 1);
  556. ubifs_assert(req->new_ino <= 1);
  557. ubifs_assert(req->new_ino_d <= UBIFS_MAX_INO_DATA);
  558. ubifs_assert(req->dirtied_ino <= 4);
  559. ubifs_assert(req->dirtied_ino_d <= UBIFS_MAX_INO_DATA * 4);
  560. ubifs_assert(!(req->new_ino_d & 7));
  561. ubifs_assert(!(req->dirtied_ino_d & 7));
  562. if (!req->recalculate) {
  563. ubifs_assert(req->idx_growth >= 0);
  564. ubifs_assert(req->data_growth >= 0);
  565. ubifs_assert(req->dd_growth >= 0);
  566. }
  567. if (req->recalculate) {
  568. req->data_growth = calc_data_growth(c, req);
  569. req->dd_growth = calc_dd_growth(c, req);
  570. req->idx_growth = calc_idx_growth(c, req);
  571. }
  572. if (!req->data_growth && !req->dd_growth)
  573. return;
  574. c->nospace = c->nospace_rp = 0;
  575. smp_wmb();
  576. spin_lock(&c->space_lock);
  577. c->budg_idx_growth -= req->idx_growth;
  578. c->budg_uncommitted_idx += req->idx_growth;
  579. c->budg_data_growth -= req->data_growth;
  580. c->budg_dd_growth -= req->dd_growth;
  581. c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
  582. ubifs_assert(c->budg_idx_growth >= 0);
  583. ubifs_assert(c->budg_data_growth >= 0);
  584. ubifs_assert(c->budg_dd_growth >= 0);
  585. ubifs_assert(c->min_idx_lebs < c->main_lebs);
  586. ubifs_assert(!(c->budg_idx_growth & 7));
  587. ubifs_assert(!(c->budg_data_growth & 7));
  588. ubifs_assert(!(c->budg_dd_growth & 7));
  589. spin_unlock(&c->space_lock);
  590. }
  591. /**
  592. * ubifs_convert_page_budget - convert budget of a new page.
  593. * @c: UBIFS file-system description object
  594. *
  595. * This function converts budget which was allocated for a new page of data to
  596. * the budget of changing an existing page of data. The latter is smaller then
  597. * the former, so this function only does simple re-calculation and does not
  598. * involve any write-back.
  599. */
  600. void ubifs_convert_page_budget(struct ubifs_info *c)
  601. {
  602. spin_lock(&c->space_lock);
  603. /* Release the index growth reservation */
  604. c->budg_idx_growth -= c->max_idx_node_sz << UBIFS_BLOCKS_PER_PAGE_SHIFT;
  605. /* Release the data growth reservation */
  606. c->budg_data_growth -= c->page_budget;
  607. /* Increase the dirty data growth reservation instead */
  608. c->budg_dd_growth += c->page_budget;
  609. /* And re-calculate the indexing space reservation */
  610. c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
  611. spin_unlock(&c->space_lock);
  612. }
  613. /**
  614. * ubifs_release_dirty_inode_budget - release dirty inode budget.
  615. * @c: UBIFS file-system description object
  616. * @ui: UBIFS inode to release the budget for
  617. *
  618. * This function releases budget corresponding to a dirty inode. It is usually
  619. * called when after the inode has been written to the media and marked as
  620. * clean.
  621. */
  622. void ubifs_release_dirty_inode_budget(struct ubifs_info *c,
  623. struct ubifs_inode *ui)
  624. {
  625. struct ubifs_budget_req req;
  626. memset(&req, 0, sizeof(struct ubifs_budget_req));
  627. req.dd_growth = c->inode_budget + ALIGN(ui->data_len, 8);
  628. ubifs_release_budget(c, &req);
  629. }
  630. /**
  631. * ubifs_reported_space - calculate reported free space.
  632. * @c: the UBIFS file-system description object
  633. * @free: amount of free space
  634. *
  635. * This function calculates amount of free space which will be reported to
  636. * user-space. User-space application tend to expect that if the file-system
  637. * (e.g., via the 'statfs()' call) reports that it has N bytes available, they
  638. * are able to write a file of size N. UBIFS attaches node headers to each data
  639. * node and it has to write indexind nodes as well. This introduces additional
  640. * overhead, and UBIFS it has to report sligtly less free space to meet the
  641. * above expectetion.
  642. *
  643. * This function assumes free space is made up of uncompressed data nodes and
  644. * full index nodes (one per data node, tripled because we always allow enough
  645. * space to write the index thrice).
  646. *
  647. * Note, the calculation is pessimistic, which means that most of the time
  648. * UBIFS reports less space than it actually has.
  649. */
  650. long long ubifs_reported_space(const struct ubifs_info *c, uint64_t free)
  651. {
  652. int divisor, factor, f;
  653. /*
  654. * Reported space size is @free * X, where X is UBIFS block size
  655. * divided by UBIFS block size + all overhead one data block
  656. * introduces. The overhead is the node header + indexing overhead.
  657. *
  658. * Indexing overhead calculations are based on the following formula:
  659. * I = N/(f - 1) + 1, where I - number of indexing nodes, N - number
  660. * of data nodes, f - fanout. Because effective UBIFS fanout is twice
  661. * as less than maximum fanout, we assume that each data node
  662. * introduces 3 * @c->max_idx_node_sz / (@c->fanout/2 - 1) bytes.
  663. * Note, the multiplier 3 is because UBIFS reseves thrice as more space
  664. * for the index.
  665. */
  666. f = c->fanout > 3 ? c->fanout >> 1 : 2;
  667. factor = UBIFS_BLOCK_SIZE;
  668. divisor = UBIFS_MAX_DATA_NODE_SZ;
  669. divisor += (c->max_idx_node_sz * 3) / (f - 1);
  670. free *= factor;
  671. do_div(free, divisor);
  672. return free;
  673. }
  674. /**
  675. * ubifs_get_free_space - return amount of free space.
  676. * @c: UBIFS file-system description object
  677. *
  678. * This function calculates amount of free space to report to user-space.
  679. *
  680. * Because UBIFS may introduce substantial overhead (the index, node headers,
  681. * alighment, wastage at the end of eraseblocks, etc), it cannot report real
  682. * amount of free flash space it has (well, because not all dirty space is
  683. * reclamable, UBIFS does not actually know the real amount). If UBIFS did so,
  684. * it would bread user expectetion about what free space is. Users seem to
  685. * accustomed to assume that if the file-system reports N bytes of free space,
  686. * they would be able to fit a file of N bytes to the FS. This almost works for
  687. * traditional file-systems, because they have way less overhead than UBIFS.
  688. * So, to keep users happy, UBIFS tries to take the overhead into account.
  689. */
  690. long long ubifs_get_free_space(struct ubifs_info *c)
  691. {
  692. int min_idx_lebs, rsvd_idx_lebs, lebs;
  693. long long available, outstanding, free;
  694. spin_lock(&c->space_lock);
  695. min_idx_lebs = ubifs_calc_min_idx_lebs(c);
  696. outstanding = c->budg_data_growth + c->budg_dd_growth;
  697. /*
  698. * Force the amount available to the total size reported if the used
  699. * space is zero.
  700. */
  701. if (c->lst.total_used <= UBIFS_INO_NODE_SZ && !outstanding) {
  702. spin_unlock(&c->space_lock);
  703. return (long long)c->block_cnt << UBIFS_BLOCK_SHIFT;
  704. }
  705. available = ubifs_calc_available(c, min_idx_lebs);
  706. /*
  707. * When reporting free space to user-space, UBIFS guarantees that it is
  708. * possible to write a file of free space size. This means that for
  709. * empty LEBs we may use more precise calculations than
  710. * 'ubifs_calc_available()' is using. Namely, we know that in empty
  711. * LEBs we would waste only @c->leb_overhead bytes, not @c->dark_wm.
  712. * Thus, amend the available space.
  713. *
  714. * Note, the calculations below are similar to what we have in
  715. * 'do_budget_space()', so refer there for comments.
  716. */
  717. if (min_idx_lebs > c->lst.idx_lebs)
  718. rsvd_idx_lebs = min_idx_lebs - c->lst.idx_lebs;
  719. else
  720. rsvd_idx_lebs = 0;
  721. lebs = c->lst.empty_lebs + c->freeable_cnt + c->idx_gc_cnt -
  722. c->lst.taken_empty_lebs;
  723. lebs -= rsvd_idx_lebs;
  724. available += lebs * (c->dark_wm - c->leb_overhead);
  725. spin_unlock(&c->space_lock);
  726. if (available > outstanding)
  727. free = ubifs_reported_space(c, available - outstanding);
  728. else
  729. free = 0;
  730. return free;
  731. }