43 #define MAX_MKSPC_RETRIES 3
49 #define NR_TO_WRITE 16
63 static void shrink_liability(
struct ubifs_info *
c,
int nr_to_write)
91 err = ubifs_return_leb(c, lnum);
104 static long long get_liability(
struct ubifs_info *c)
109 liab = c->
bi.idx_growth + c->
bi.data_growth + c->
bi.dd_growth;
132 static int make_free_space(
struct ubifs_info *c)
135 long long liab1, liab2;
138 liab1 = get_liability(c);
143 dbg_budg(
"liability %lld, run write-back", liab1);
146 liab2 = get_liability(c);
150 dbg_budg(
"new liability %lld (not shrunk)", liab2);
162 dbg_budg(
"Run commit (retries %d)", retries);
183 idx_size = c->
bi.old_idx_sz + c->
bi.idx_growth + c->
bi.uncommitted_idx;
185 idx_size += idx_size << 1;
221 subtract_lebs = min_idx_lebs;
239 available -= c->
lst.total_dead;
247 available -= c->
lst.total_dark;
255 if (c->
lst.idx_lebs > min_idx_lebs) {
256 subtract_lebs = c->
lst.idx_lebs - min_idx_lebs;
257 available -= subtract_lebs * c->
dark_wm;
261 return available > 0 ? available : 0;
306 static int do_budget_space(
struct ubifs_info *c)
309 int lebs, rsvd_idx_lebs, min_idx_lebs;
315 if (min_idx_lebs > c->
lst.idx_lebs)
316 rsvd_idx_lebs = min_idx_lebs - c->
lst.idx_lebs;
343 c->
lst.taken_empty_lebs;
344 if (
unlikely(rsvd_idx_lebs > lebs)) {
345 dbg_budg(
"out of indexing space: min_idx_lebs %d (old %d), rsvd_idx_lebs %d",
346 min_idx_lebs, c->
bi.min_idx_lebs, rsvd_idx_lebs);
351 outstanding = c->
bi.data_growth + c->
bi.dd_growth;
353 if (
unlikely(available < outstanding)) {
354 dbg_budg(
"out of data space: available %lld, outstanding %lld",
355 available, outstanding);
359 if (available - outstanding <= c->rp_size && !can_use_rp(c))
362 c->
bi.min_idx_lebs = min_idx_lebs;
374 static int calc_idx_growth(
const struct ubifs_info *c,
390 static int calc_data_growth(
const struct ubifs_info *c,
395 data_growth = req->
new_ino ? c->
bi.inode_budget : 0;
397 data_growth += c->
bi.page_budget;
399 data_growth += c->
bi.dent_budget;
410 static int calc_dd_growth(
const struct ubifs_info *c,
420 dd_growth += c->
bi.dent_budget;
441 int err, idx_growth, data_growth, dd_growth, retried = 0;
454 data_growth = calc_data_growth(c, req);
455 dd_growth = calc_dd_growth(c, req);
456 if (!data_growth && !dd_growth)
458 idx_growth = calc_idx_growth(c, req);
466 if (
unlikely(c->
bi.nospace) && (c->
bi.nospace_rp || !can_use_rp(c))) {
472 c->
bi.idx_growth += idx_growth;
473 c->
bi.data_growth += data_growth;
474 c->
bi.dd_growth += dd_growth;
476 err = do_budget_space(c);
486 c->
bi.idx_growth -= idx_growth;
487 c->
bi.data_growth -= data_growth;
488 c->
bi.dd_growth -= dd_growth;
492 dbg_budg(
"no space for fast budgeting");
496 err = make_free_space(c);
501 }
else if (err == -
ENOSPC) {
504 dbg_budg(
"-ENOSPC, but anyway try once again");
509 if (can_use_rp(c) || c->
rp_size == 0)
510 c->
bi.nospace_rp = 1;
513 ubifs_err(
"cannot budget space, error %d", err);
555 c->
bi.nospace = c->
bi.nospace_rp = 0;
590 c->
bi.data_growth -= c->
bi.page_budget;
592 c->
bi.dd_growth += c->
bi.page_budget;
660 return div_u64(free, divisor);
681 int rsvd_idx_lebs, lebs;
685 outstanding = c->
bi.data_growth + c->
bi.dd_growth;
699 if (c->
bi.min_idx_lebs > c->
lst.idx_lebs)
700 rsvd_idx_lebs = c->
bi.min_idx_lebs - c->
lst.idx_lebs;
704 c->
lst.taken_empty_lebs;
705 lebs -= rsvd_idx_lebs;
708 if (available > outstanding)