This source file includes following definitions.
- btrfs_alloc_data_chunk_ondemand
- btrfs_check_data_free_space
- btrfs_free_reserved_data_space_noquota
- btrfs_free_reserved_data_space
- btrfs_inode_rsv_release
- btrfs_calculate_inode_block_rsv_size
- calc_inode_reservations
- btrfs_delalloc_reserve_metadata
- btrfs_delalloc_release_metadata
- btrfs_delalloc_release_extents
- btrfs_delalloc_reserve_space
- btrfs_delalloc_release_space
1
2
3 #include "ctree.h"
4 #include "delalloc-space.h"
5 #include "block-rsv.h"
6 #include "btrfs_inode.h"
7 #include "space-info.h"
8 #include "transaction.h"
9 #include "qgroup.h"
10 #include "block-group.h"
11
12 int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
13 {
14 struct btrfs_root *root = inode->root;
15 struct btrfs_fs_info *fs_info = root->fs_info;
16 struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
17 u64 used;
18 int ret = 0;
19 int need_commit = 2;
20 int have_pinned_space;
21
22
23 bytes = ALIGN(bytes, fs_info->sectorsize);
24
25 if (btrfs_is_free_space_inode(inode)) {
26 need_commit = 0;
27 ASSERT(current->journal_info);
28 }
29
30 again:
31
32 spin_lock(&data_sinfo->lock);
33 used = btrfs_space_info_used(data_sinfo, true);
34
35 if (used + bytes > data_sinfo->total_bytes) {
36 struct btrfs_trans_handle *trans;
37
38
39
40
41
42 if (!data_sinfo->full) {
43 u64 alloc_target;
44
45 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
46 spin_unlock(&data_sinfo->lock);
47
48 alloc_target = btrfs_data_alloc_profile(fs_info);
49
50
51
52
53
54
55
56
57
58
59 trans = btrfs_join_transaction(root);
60 if (IS_ERR(trans))
61 return PTR_ERR(trans);
62
63 ret = btrfs_chunk_alloc(trans, alloc_target,
64 CHUNK_ALLOC_NO_FORCE);
65 btrfs_end_transaction(trans);
66 if (ret < 0) {
67 if (ret != -ENOSPC)
68 return ret;
69 else {
70 have_pinned_space = 1;
71 goto commit_trans;
72 }
73 }
74
75 goto again;
76 }
77
78
79
80
81
82
83 have_pinned_space = __percpu_counter_compare(
84 &data_sinfo->total_bytes_pinned,
85 used + bytes - data_sinfo->total_bytes,
86 BTRFS_TOTAL_BYTES_PINNED_BATCH);
87 spin_unlock(&data_sinfo->lock);
88
89
90 commit_trans:
91 if (need_commit) {
92 need_commit--;
93
94 if (need_commit > 0) {
95 btrfs_start_delalloc_roots(fs_info, -1);
96 btrfs_wait_ordered_roots(fs_info, U64_MAX, 0,
97 (u64)-1);
98 }
99
100 trans = btrfs_join_transaction(root);
101 if (IS_ERR(trans))
102 return PTR_ERR(trans);
103 if (have_pinned_space >= 0 ||
104 test_bit(BTRFS_TRANS_HAVE_FREE_BGS,
105 &trans->transaction->flags) ||
106 need_commit > 0) {
107 ret = btrfs_commit_transaction(trans);
108 if (ret)
109 return ret;
110
111
112
113
114
115
116
117
118 ret = btrfs_wait_on_delayed_iputs(fs_info);
119 if (ret)
120 return ret;
121 goto again;
122 } else {
123 btrfs_end_transaction(trans);
124 }
125 }
126
127 trace_btrfs_space_reservation(fs_info,
128 "space_info:enospc",
129 data_sinfo->flags, bytes, 1);
130 return -ENOSPC;
131 }
132 btrfs_space_info_update_bytes_may_use(fs_info, data_sinfo, bytes);
133 spin_unlock(&data_sinfo->lock);
134
135 return 0;
136 }
137
138 int btrfs_check_data_free_space(struct inode *inode,
139 struct extent_changeset **reserved, u64 start, u64 len)
140 {
141 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
142 int ret;
143
144
145 len = round_up(start + len, fs_info->sectorsize) -
146 round_down(start, fs_info->sectorsize);
147 start = round_down(start, fs_info->sectorsize);
148
149 ret = btrfs_alloc_data_chunk_ondemand(BTRFS_I(inode), len);
150 if (ret < 0)
151 return ret;
152
153
154 ret = btrfs_qgroup_reserve_data(inode, reserved, start, len);
155 if (ret < 0)
156 btrfs_free_reserved_data_space_noquota(inode, start, len);
157 else
158 ret = 0;
159 return ret;
160 }
161
162
163
164
165
166
167
168
169
170 void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
171 u64 len)
172 {
173 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
174 struct btrfs_space_info *data_sinfo;
175
176
177 len = round_up(start + len, fs_info->sectorsize) -
178 round_down(start, fs_info->sectorsize);
179 start = round_down(start, fs_info->sectorsize);
180
181 data_sinfo = fs_info->data_sinfo;
182 spin_lock(&data_sinfo->lock);
183 btrfs_space_info_update_bytes_may_use(fs_info, data_sinfo, -len);
184 spin_unlock(&data_sinfo->lock);
185 }
186
187
188
189
190
191
192
193
194 void btrfs_free_reserved_data_space(struct inode *inode,
195 struct extent_changeset *reserved, u64 start, u64 len)
196 {
197 struct btrfs_root *root = BTRFS_I(inode)->root;
198
199
200 len = round_up(start + len, root->fs_info->sectorsize) -
201 round_down(start, root->fs_info->sectorsize);
202 start = round_down(start, root->fs_info->sectorsize);
203
204 btrfs_free_reserved_data_space_noquota(inode, start, len);
205 btrfs_qgroup_free_data(inode, reserved, start, len);
206 }
207
208
209
210
211
212
213
214
215
216
217
218
219 static void btrfs_inode_rsv_release(struct btrfs_inode *inode, bool qgroup_free)
220 {
221 struct btrfs_fs_info *fs_info = inode->root->fs_info;
222 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
223 u64 released = 0;
224 u64 qgroup_to_release = 0;
225
226
227
228
229
230
231 released = __btrfs_block_rsv_release(fs_info, block_rsv, 0,
232 &qgroup_to_release);
233 if (released > 0)
234 trace_btrfs_space_reservation(fs_info, "delalloc",
235 btrfs_ino(inode), released, 0);
236 if (qgroup_free)
237 btrfs_qgroup_free_meta_prealloc(inode->root, qgroup_to_release);
238 else
239 btrfs_qgroup_convert_reserved_meta(inode->root,
240 qgroup_to_release);
241 }
242
243 static void btrfs_calculate_inode_block_rsv_size(struct btrfs_fs_info *fs_info,
244 struct btrfs_inode *inode)
245 {
246 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
247 u64 reserve_size = 0;
248 u64 qgroup_rsv_size = 0;
249 u64 csum_leaves;
250 unsigned outstanding_extents;
251
252 lockdep_assert_held(&inode->lock);
253 outstanding_extents = inode->outstanding_extents;
254
255
256
257
258
259 if (outstanding_extents) {
260 reserve_size = btrfs_calc_insert_metadata_size(fs_info,
261 outstanding_extents);
262 reserve_size += btrfs_calc_metadata_size(fs_info, 1);
263 }
264 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info,
265 inode->csum_bytes);
266 reserve_size += btrfs_calc_insert_metadata_size(fs_info,
267 csum_leaves);
268
269
270
271
272
273
274 qgroup_rsv_size = (u64)outstanding_extents * fs_info->nodesize;
275
276 spin_lock(&block_rsv->lock);
277 block_rsv->size = reserve_size;
278 block_rsv->qgroup_rsv_size = qgroup_rsv_size;
279 spin_unlock(&block_rsv->lock);
280 }
281
282 static void calc_inode_reservations(struct btrfs_fs_info *fs_info,
283 u64 num_bytes, u64 *meta_reserve,
284 u64 *qgroup_reserve)
285 {
286 u64 nr_extents = count_max_extents(num_bytes);
287 u64 csum_leaves = btrfs_csum_bytes_to_leaves(fs_info, num_bytes);
288 u64 inode_update = btrfs_calc_metadata_size(fs_info, 1);
289
290 *meta_reserve = btrfs_calc_insert_metadata_size(fs_info,
291 nr_extents + csum_leaves);
292
293
294
295
296
297 *meta_reserve += inode_update;
298 *qgroup_reserve = nr_extents * fs_info->nodesize;
299 }
300
301 int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes)
302 {
303 struct btrfs_root *root = inode->root;
304 struct btrfs_fs_info *fs_info = root->fs_info;
305 struct btrfs_block_rsv *block_rsv = &inode->block_rsv;
306 u64 meta_reserve, qgroup_reserve;
307 unsigned nr_extents;
308 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
309 int ret = 0;
310 bool delalloc_lock = true;
311
312
313
314
315
316
317
318
319
320
321 if (btrfs_is_free_space_inode(inode)) {
322 flush = BTRFS_RESERVE_NO_FLUSH;
323 delalloc_lock = false;
324 } else {
325 if (current->journal_info)
326 flush = BTRFS_RESERVE_FLUSH_LIMIT;
327
328 if (btrfs_transaction_in_commit(fs_info))
329 schedule_timeout(1);
330 }
331
332 if (delalloc_lock)
333 mutex_lock(&inode->delalloc_mutex);
334
335 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
336
337
338
339
340
341
342
343
344
345
346
347 calc_inode_reservations(fs_info, num_bytes, &meta_reserve,
348 &qgroup_reserve);
349 ret = btrfs_qgroup_reserve_meta_prealloc(root, qgroup_reserve, true);
350 if (ret)
351 goto out_fail;
352 ret = btrfs_reserve_metadata_bytes(root, block_rsv, meta_reserve, flush);
353 if (ret)
354 goto out_qgroup;
355
356
357
358
359
360
361
362 spin_lock(&inode->lock);
363 nr_extents = count_max_extents(num_bytes);
364 btrfs_mod_outstanding_extents(inode, nr_extents);
365 inode->csum_bytes += num_bytes;
366 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
367 spin_unlock(&inode->lock);
368
369
370 btrfs_block_rsv_add_bytes(block_rsv, meta_reserve, false);
371 trace_btrfs_space_reservation(root->fs_info, "delalloc",
372 btrfs_ino(inode), meta_reserve, 1);
373
374 spin_lock(&block_rsv->lock);
375 block_rsv->qgroup_rsv_reserved += qgroup_reserve;
376 spin_unlock(&block_rsv->lock);
377
378 if (delalloc_lock)
379 mutex_unlock(&inode->delalloc_mutex);
380 return 0;
381 out_qgroup:
382 btrfs_qgroup_free_meta_prealloc(root, qgroup_reserve);
383 out_fail:
384 if (delalloc_lock)
385 mutex_unlock(&inode->delalloc_mutex);
386 return ret;
387 }
388
389
390
391
392
393
394
395
396
397
398
399 void btrfs_delalloc_release_metadata(struct btrfs_inode *inode, u64 num_bytes,
400 bool qgroup_free)
401 {
402 struct btrfs_fs_info *fs_info = inode->root->fs_info;
403
404 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
405 spin_lock(&inode->lock);
406 inode->csum_bytes -= num_bytes;
407 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
408 spin_unlock(&inode->lock);
409
410 if (btrfs_is_testing(fs_info))
411 return;
412
413 btrfs_inode_rsv_release(inode, qgroup_free);
414 }
415
416
417
418
419
420
421
422
423
424
425
426
427 void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes)
428 {
429 struct btrfs_fs_info *fs_info = inode->root->fs_info;
430 unsigned num_extents;
431
432 spin_lock(&inode->lock);
433 num_extents = count_max_extents(num_bytes);
434 btrfs_mod_outstanding_extents(inode, -num_extents);
435 btrfs_calculate_inode_block_rsv_size(fs_info, inode);
436 spin_unlock(&inode->lock);
437
438 if (btrfs_is_testing(fs_info))
439 return;
440
441 btrfs_inode_rsv_release(inode, true);
442 }
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469 int btrfs_delalloc_reserve_space(struct inode *inode,
470 struct extent_changeset **reserved, u64 start, u64 len)
471 {
472 int ret;
473
474 ret = btrfs_check_data_free_space(inode, reserved, start, len);
475 if (ret < 0)
476 return ret;
477 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), len);
478 if (ret < 0)
479 btrfs_free_reserved_data_space(inode, *reserved, start, len);
480 return ret;
481 }
482
483
484
485
486
487
488
489
490
491
492
493
494
495 void btrfs_delalloc_release_space(struct inode *inode,
496 struct extent_changeset *reserved,
497 u64 start, u64 len, bool qgroup_free)
498 {
499 btrfs_delalloc_release_metadata(BTRFS_I(inode), len, qgroup_free);
500 btrfs_free_reserved_data_space(inode, reserved, start, len);
501 }