diff options
-rw-r--r-- | fs/ubifs/budget.c | 151 |
1 files changed, 49 insertions, 102 deletions
diff --git a/fs/ubifs/budget.c b/fs/ubifs/budget.c index 3715d0114952..4d270f0a8564 100644 --- a/fs/ubifs/budget.c +++ b/fs/ubifs/budget.c | |||
@@ -37,13 +37,10 @@ | |||
37 | /* | 37 | /* |
38 | * When pessimistic budget calculations say that there is no enough space, | 38 | * When pessimistic budget calculations say that there is no enough space, |
39 | * UBIFS starts writing back dirty inodes and pages, doing garbage collection, | 39 | * UBIFS starts writing back dirty inodes and pages, doing garbage collection, |
40 | * or committing. The below constants define maximum number of times UBIFS | 40 | * or committing. The below constant defines maximum number of times UBIFS |
41 | * repeats the operations. | 41 | * repeats the operations. |
42 | */ | 42 | */ |
43 | #define MAX_SHRINK_RETRIES 8 | 43 | #define MAX_MKSPC_RETRIES 3 |
44 | #define MAX_GC_RETRIES 4 | ||
45 | #define MAX_CMT_RETRIES 2 | ||
46 | #define MAX_NOSPC_RETRIES 1 | ||
47 | 44 | ||
48 | /* | 45 | /* |
49 | * The below constant defines amount of dirty pages which should be written | 46 | * The below constant defines amount of dirty pages which should be written |
@@ -52,30 +49,6 @@ | |||
52 | #define NR_TO_WRITE 16 | 49 | #define NR_TO_WRITE 16 |
53 | 50 | ||
54 | /** | 51 | /** |
55 | * struct retries_info - information about re-tries while making free space. | ||
56 | * @prev_liability: previous liability | ||
57 | * @shrink_cnt: how many times the liability was shrinked | ||
58 | * @shrink_retries: count of liability shrink re-tries (increased when | ||
59 | * liability does not shrink) | ||
60 | * @try_gc: GC should be tried first | ||
61 | * @gc_retries: how many times GC was run | ||
62 | * @cmt_retries: how many times commit has been done | ||
63 | * @nospc_retries: how many times GC returned %-ENOSPC | ||
64 | * | ||
65 | * Since we consider budgeting to be the fast-path, and this structure has to | ||
66 | * be allocated on stack and zeroed out, we make it smaller using bit-fields. | ||
67 | */ | ||
68 | struct retries_info { | ||
69 | long long prev_liability; | ||
70 | unsigned int shrink_cnt; | ||
71 | unsigned int shrink_retries:5; | ||
72 | unsigned int try_gc:1; | ||
73 | unsigned int gc_retries:4; | ||
74 | unsigned int cmt_retries:3; | ||
75 | unsigned int nospc_retries:1; | ||
76 | }; | ||
77 | |||
78 | /** | ||
79 | * shrink_liability - write-back some dirty pages/inodes. | 52 | * shrink_liability - write-back some dirty pages/inodes. |
80 | * @c: UBIFS file-system description object | 53 | * @c: UBIFS file-system description object |
81 | * @nr_to_write: how many dirty pages to write-back | 54 | * @nr_to_write: how many dirty pages to write-back |
@@ -147,9 +120,25 @@ static int run_gc(struct ubifs_info *c) | |||
147 | } | 120 | } |
148 | 121 | ||
149 | /** | 122 | /** |
123 | * get_liability - calculate current liability. | ||
124 | * @c: UBIFS file-system description object | ||
125 | * | ||
126 | * This function calculates and returns current UBIFS liability, i.e. the | ||
127 | * amount of bytes UBIFS has "promised" to write to the media. | ||
128 | */ | ||
129 | static long long get_liability(struct ubifs_info *c) | ||
130 | { | ||
131 | long long liab; | ||
132 | |||
133 | spin_lock(&c->space_lock); | ||
134 | liab = c->budg_idx_growth + c->budg_data_growth + c->budg_dd_growth; | ||
135 | spin_unlock(&c->space_lock); | ||
136 | return liab; | ||
137 | } | ||
138 | |||
139 | /** | ||
150 | * make_free_space - make more free space on the file-system. | 140 | * make_free_space - make more free space on the file-system. |
151 | * @c: UBIFS file-system description object | 141 | * @c: UBIFS file-system description object |
152 | * @ri: information about previous invocations of this function | ||
153 | * | 142 | * |
154 | * This function is called when an operation cannot be budgeted because there | 143 | * This function is called when an operation cannot be budgeted because there |
155 | * is supposedly no free space. But in most cases there is some free space: | 144 | * is supposedly no free space. But in most cases there is some free space: |
@@ -165,87 +154,42 @@ static int run_gc(struct ubifs_info *c) | |||
165 | * Returns %-ENOSPC if it couldn't do more free space, and other negative error | 154 | * Returns %-ENOSPC if it couldn't do more free space, and other negative error |
166 | * codes on failures. | 155 | * codes on failures. |
167 | */ | 156 | */ |
168 | static int make_free_space(struct ubifs_info *c, struct retries_info *ri) | 157 | static int make_free_space(struct ubifs_info *c) |
169 | { | 158 | { |
170 | int err; | 159 | int err, retries = 0; |
171 | 160 | long long liab1, liab2; | |
172 | /* | ||
173 | * If we have some dirty pages and inodes (liability), try to write | ||
174 | * them back unless this was tried too many times without effect | ||
175 | * already. | ||
176 | */ | ||
177 | if (ri->shrink_retries < MAX_SHRINK_RETRIES && !ri->try_gc) { | ||
178 | long long liability; | ||
179 | |||
180 | spin_lock(&c->space_lock); | ||
181 | liability = c->budg_idx_growth + c->budg_data_growth + | ||
182 | c->budg_dd_growth; | ||
183 | spin_unlock(&c->space_lock); | ||
184 | 161 | ||
185 | if (ri->prev_liability >= liability) { | 162 | do { |
186 | /* Liability does not shrink, next time try GC then */ | 163 | liab1 = get_liability(c); |
187 | ri->shrink_retries += 1; | 164 | /* |
188 | if (ri->gc_retries < MAX_GC_RETRIES) | 165 | * We probably have some dirty pages or inodes (liability), try |
189 | ri->try_gc = 1; | 166 | * to write them back. |
190 | dbg_budg("liability did not shrink: retries %d of %d", | 167 | */ |
191 | ri->shrink_retries, MAX_SHRINK_RETRIES); | 168 | dbg_budg("liability %lld, run write-back", liab1); |
192 | } | 169 | shrink_liability(c, NR_TO_WRITE); |
193 | |||
194 | dbg_budg("force write-back (count %d)", ri->shrink_cnt); | ||
195 | shrink_liability(c, NR_TO_WRITE + ri->shrink_cnt); | ||
196 | 170 | ||
197 | ri->prev_liability = liability; | 171 | liab2 = get_liability(c); |
198 | ri->shrink_cnt += 1; | 172 | if (liab2 < liab1) |
199 | return -EAGAIN; | 173 | return -EAGAIN; |
200 | } | ||
201 | 174 | ||
202 | /* | 175 | dbg_budg("new liability %lld (not shrinked)", liab2); |
203 | * Try to run garbage collector unless it was already tried too many | ||
204 | * times. | ||
205 | */ | ||
206 | if (ri->gc_retries < MAX_GC_RETRIES) { | ||
207 | ri->gc_retries += 1; | ||
208 | dbg_budg("run GC, retries %d of %d", | ||
209 | ri->gc_retries, MAX_GC_RETRIES); | ||
210 | 176 | ||
211 | ri->try_gc = 0; | 177 | /* Liability did not shrink again, try GC */ |
178 | dbg_budg("Run GC"); | ||
212 | err = run_gc(c); | 179 | err = run_gc(c); |
213 | if (!err) | 180 | if (!err) |
214 | return -EAGAIN; | 181 | return -EAGAIN; |
215 | 182 | ||
216 | if (err == -EAGAIN) { | 183 | if (err != -EAGAIN && err != -ENOSPC) |
217 | dbg_budg("GC asked to commit"); | 184 | /* Some real error happened */ |
218 | err = ubifs_run_commit(c); | ||
219 | if (err) | ||
220 | return err; | ||
221 | return -EAGAIN; | ||
222 | } | ||
223 | |||
224 | if (err != -ENOSPC) | ||
225 | return err; | 185 | return err; |
226 | 186 | ||
227 | /* | 187 | dbg_budg("Run commit (retries %d)", retries); |
228 | * GC could not make any progress. If this is the first time, | ||
229 | * then it makes sense to try to commit, because it might make | ||
230 | * some dirty space. | ||
231 | */ | ||
232 | dbg_budg("GC returned -ENOSPC, retries %d", | ||
233 | ri->nospc_retries); | ||
234 | if (ri->nospc_retries >= MAX_NOSPC_RETRIES) | ||
235 | return err; | ||
236 | ri->nospc_retries += 1; | ||
237 | } | ||
238 | |||
239 | /* Neither GC nor write-back helped, try to commit */ | ||
240 | if (ri->cmt_retries < MAX_CMT_RETRIES) { | ||
241 | ri->cmt_retries += 1; | ||
242 | dbg_budg("run commit, retries %d of %d", | ||
243 | ri->cmt_retries, MAX_CMT_RETRIES); | ||
244 | err = ubifs_run_commit(c); | 188 | err = ubifs_run_commit(c); |
245 | if (err) | 189 | if (err) |
246 | return err; | 190 | return err; |
247 | return -EAGAIN; | 191 | } while (retries++ < MAX_MKSPC_RETRIES); |
248 | } | 192 | |
249 | return -ENOSPC; | 193 | return -ENOSPC; |
250 | } | 194 | } |
251 | 195 | ||
@@ -523,8 +467,7 @@ static int calc_dd_growth(const struct ubifs_info *c, | |||
523 | int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req) | 467 | int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req) |
524 | { | 468 | { |
525 | int uninitialized_var(cmt_retries), uninitialized_var(wb_retries); | 469 | int uninitialized_var(cmt_retries), uninitialized_var(wb_retries); |
526 | int err, idx_growth, data_growth, dd_growth; | 470 | int err, idx_growth, data_growth, dd_growth, retried = 0; |
527 | struct retries_info ri; | ||
528 | 471 | ||
529 | ubifs_assert(req->new_page <= 1); | 472 | ubifs_assert(req->new_page <= 1); |
530 | ubifs_assert(req->dirtied_page <= 1); | 473 | ubifs_assert(req->dirtied_page <= 1); |
@@ -542,7 +485,6 @@ int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req) | |||
542 | if (!data_growth && !dd_growth) | 485 | if (!data_growth && !dd_growth) |
543 | return 0; | 486 | return 0; |
544 | idx_growth = calc_idx_growth(c, req); | 487 | idx_growth = calc_idx_growth(c, req); |
545 | memset(&ri, 0, sizeof(struct retries_info)); | ||
546 | 488 | ||
547 | again: | 489 | again: |
548 | spin_lock(&c->space_lock); | 490 | spin_lock(&c->space_lock); |
@@ -580,12 +522,17 @@ again: | |||
580 | return err; | 522 | return err; |
581 | } | 523 | } |
582 | 524 | ||
583 | err = make_free_space(c, &ri); | 525 | err = make_free_space(c); |
526 | cond_resched(); | ||
584 | if (err == -EAGAIN) { | 527 | if (err == -EAGAIN) { |
585 | dbg_budg("try again"); | 528 | dbg_budg("try again"); |
586 | cond_resched(); | ||
587 | goto again; | 529 | goto again; |
588 | } else if (err == -ENOSPC) { | 530 | } else if (err == -ENOSPC) { |
531 | if (!retried) { | ||
532 | retried = 1; | ||
533 | dbg_budg("-ENOSPC, but anyway try once again"); | ||
534 | goto again; | ||
535 | } | ||
589 | dbg_budg("FS is full, -ENOSPC"); | 536 | dbg_budg("FS is full, -ENOSPC"); |
590 | c->nospace = 1; | 537 | c->nospace = 1; |
591 | if (can_use_rp(c) || c->rp_size == 0) | 538 | if (can_use_rp(c) || c->rp_size == 0) |