Skip to content

Commit 8698fc4

Browse files
josefbacikkdave
authored andcommitted
btrfs: add btrfs_reserve_data_bytes and use it
Create a new function btrfs_reserve_data_bytes() in order to handle data reservations. This uses the new flush types and flush states to handle making data reservations. This patch specifically does not change any functionality, and is purposefully not cleaned up in order to make bisection easier for the future patches. The new helper is identical to the old helper in how it handles data reservations. We first try to force a chunk allocation, and then we run through the flush states all at once and in the same order that they were done with the old helper. Subsequent patches will clean this up and change the behavior of the flushing, and it is important to keep those changes separate so we can easily bisect down to the patch that caused the regression, rather than the patch that made us start using the new infrastructure. Reviewed-by: Nikolay Borisov <nborisov@suse.com> Tested-by: Nikolay Borisov <nborisov@suse.com> Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com> Signed-off-by: Josef Bacik <josef@toxicpanda.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent a1ed0a8 commit 8698fc4

3 files changed

Lines changed: 98 additions & 115 deletions

File tree

‎fs/btrfs/delalloc-space.c‎

Lines changed: 4 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -115,126 +115,15 @@ int btrfs_alloc_data_chunk_ondemand(struct btrfs_inode *inode, u64 bytes)
115115
{
116116
struct btrfs_root *root = inode->root;
117117
struct btrfs_fs_info *fs_info = root->fs_info;
118-
struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
119-
u64 used;
120-
int ret = 0;
121-
int need_commit = 2;
122-
int have_pinned_space;
118+
enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_DATA;
123119

124120
/* Make sure bytes are sectorsize aligned */
125121
bytes = ALIGN(bytes, fs_info->sectorsize);
126122

127-
if (btrfs_is_free_space_inode(inode)) {
128-
need_commit = 0;
129-
ASSERT(current->journal_info);
130-
}
131-
132-
again:
133-
/* Make sure we have enough space to handle the data first */
134-
spin_lock(&data_sinfo->lock);
135-
used = btrfs_space_info_used(data_sinfo, true);
136-
137-
if (used + bytes > data_sinfo->total_bytes) {
138-
struct btrfs_trans_handle *trans;
139-
140-
/*
141-
* If we don't have enough free bytes in this space then we need
142-
* to alloc a new chunk.
143-
*/
144-
if (!data_sinfo->full) {
145-
u64 alloc_target;
146-
147-
data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
148-
spin_unlock(&data_sinfo->lock);
149-
150-
alloc_target = btrfs_data_alloc_profile(fs_info);
151-
/*
152-
* It is ugly that we don't call nolock join
153-
* transaction for the free space inode case here.
154-
* But it is safe because we only do the data space
155-
* reservation for the free space cache in the
156-
* transaction context, the common join transaction
157-
* just increase the counter of the current transaction
158-
* handler, doesn't try to acquire the trans_lock of
159-
* the fs.
160-
*/
161-
trans = btrfs_join_transaction(root);
162-
if (IS_ERR(trans))
163-
return PTR_ERR(trans);
164-
165-
ret = btrfs_chunk_alloc(trans, alloc_target,
166-
CHUNK_ALLOC_NO_FORCE);
167-
btrfs_end_transaction(trans);
168-
if (ret < 0) {
169-
if (ret != -ENOSPC)
170-
return ret;
171-
else {
172-
have_pinned_space = 1;
173-
goto commit_trans;
174-
}
175-
}
176-
177-
goto again;
178-
}
179-
180-
/*
181-
* If we don't have enough pinned space to deal with this
182-
* allocation, and no removed chunk in current transaction,
183-
* don't bother committing the transaction.
184-
*/
185-
have_pinned_space = __percpu_counter_compare(
186-
&data_sinfo->total_bytes_pinned,
187-
used + bytes - data_sinfo->total_bytes,
188-
BTRFS_TOTAL_BYTES_PINNED_BATCH);
189-
spin_unlock(&data_sinfo->lock);
190-
191-
/* Commit the current transaction and try again */
192-
commit_trans:
193-
if (need_commit) {
194-
need_commit--;
195-
196-
if (need_commit > 0) {
197-
btrfs_start_delalloc_roots(fs_info, -1);
198-
btrfs_wait_ordered_roots(fs_info, U64_MAX, 0,
199-
(u64)-1);
200-
}
201-
202-
trans = btrfs_join_transaction(root);
203-
if (IS_ERR(trans))
204-
return PTR_ERR(trans);
205-
if (have_pinned_space >= 0 ||
206-
test_bit(BTRFS_TRANS_HAVE_FREE_BGS,
207-
&trans->transaction->flags) ||
208-
need_commit > 0) {
209-
ret = btrfs_commit_transaction(trans);
210-
if (ret)
211-
return ret;
212-
/*
213-
* The cleaner kthread might still be doing iput
214-
* operations. Wait for it to finish so that
215-
* more space is released. We don't need to
216-
* explicitly run the delayed iputs here because
217-
* the commit_transaction would have woken up
218-
* the cleaner.
219-
*/
220-
ret = btrfs_wait_on_delayed_iputs(fs_info);
221-
if (ret)
222-
return ret;
223-
goto again;
224-
} else {
225-
btrfs_end_transaction(trans);
226-
}
227-
}
228-
229-
trace_btrfs_space_reservation(fs_info,
230-
"space_info:enospc",
231-
data_sinfo->flags, bytes, 1);
232-
return -ENOSPC;
233-
}
234-
btrfs_space_info_update_bytes_may_use(fs_info, data_sinfo, bytes);
235-
spin_unlock(&data_sinfo->lock);
123+
if (btrfs_is_free_space_inode(inode))
124+
flush = BTRFS_RESERVE_FLUSH_FREE_SPACE_INODE;
236125

237-
return 0;
126+
return btrfs_reserve_data_bytes(fs_info, bytes, flush);
238127
}
239128

240129
int btrfs_check_data_free_space(struct btrfs_inode *inode,

‎fs/btrfs/space-info.c‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,3 +1329,95 @@ int btrfs_reserve_metadata_bytes(struct btrfs_root *root,
13291329
}
13301330
return ret;
13311331
}
1332+
1333+
/**
1334+
* btrfs_reserve_data_bytes - try to reserve data bytes for an allocation
1335+
* @fs_info - the filesystem
1336+
* @bytes - the number of bytes we need
1337+
* @flush - how we are allowed to flush
1338+
*
1339+
* This will reserve bytes from the data space info. If there is not enough
1340+
* space then we will attempt to flush space as specified by flush.
1341+
*/
1342+
int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,
1343+
enum btrfs_reserve_flush_enum flush)
1344+
{
1345+
struct btrfs_space_info *data_sinfo = fs_info->data_sinfo;
1346+
const enum btrfs_flush_state *states = NULL;
1347+
u64 used;
1348+
int states_nr = 0;
1349+
int commit_cycles = 2;
1350+
int ret = -ENOSPC;
1351+
1352+
ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_DATA);
1353+
1354+
if (flush == BTRFS_RESERVE_FLUSH_DATA) {
1355+
states = data_flush_states;
1356+
states_nr = ARRAY_SIZE(data_flush_states);
1357+
}
1358+
1359+
spin_lock(&data_sinfo->lock);
1360+
again:
1361+
used = btrfs_space_info_used(data_sinfo, true);
1362+
1363+
if (used + bytes > data_sinfo->total_bytes) {
1364+
u64 prev_total_bytes = data_sinfo->total_bytes;
1365+
int flush_state = 0;
1366+
1367+
spin_unlock(&data_sinfo->lock);
1368+
1369+
/*
1370+
* Everybody can force chunk allocation, so try this first to
1371+
* see if we can just bail here and make our reservation.
1372+
*/
1373+
flush_space(fs_info, data_sinfo, bytes, ALLOC_CHUNK_FORCE);
1374+
spin_lock(&data_sinfo->lock);
1375+
if (prev_total_bytes < data_sinfo->total_bytes)
1376+
goto again;
1377+
spin_unlock(&data_sinfo->lock);
1378+
1379+
/*
1380+
* Cycle through the rest of the flushing options for our flush
1381+
* type, then try again.
1382+
*/
1383+
while (flush_state < states_nr) {
1384+
u64 flush_bytes = U64_MAX;
1385+
1386+
/*
1387+
* Previously we unconditionally committed the
1388+
* transaction twice before finally checking against
1389+
* pinned space before committing the final time. We
1390+
* also skipped flushing delalloc the final pass
1391+
* through.
1392+
*/
1393+
if (!commit_cycles) {
1394+
if (states[flush_state] == FLUSH_DELALLOC_WAIT) {
1395+
flush_state++;
1396+
continue;
1397+
}
1398+
if (states[flush_state] == COMMIT_TRANS)
1399+
flush_bytes = bytes;
1400+
}
1401+
1402+
flush_space(fs_info, data_sinfo, flush_bytes,
1403+
states[flush_state]);
1404+
flush_state++;
1405+
}
1406+
1407+
if (!commit_cycles)
1408+
goto out;
1409+
1410+
commit_cycles--;
1411+
spin_lock(&data_sinfo->lock);
1412+
goto again;
1413+
}
1414+
btrfs_space_info_update_bytes_may_use(fs_info, data_sinfo, bytes);
1415+
ret = 0;
1416+
spin_unlock(&data_sinfo->lock);
1417+
out:
1418+
if (ret)
1419+
trace_btrfs_space_reservation(fs_info,
1420+
"space_info:enospc",
1421+
data_sinfo->flags, bytes, 1);
1422+
return ret;
1423+
}

‎fs/btrfs/space-info.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,5 +149,7 @@ static inline void btrfs_space_info_free_bytes_may_use(
149149
btrfs_try_granting_tickets(fs_info, space_info);
150150
spin_unlock(&space_info->lock);
151151
}
152+
int btrfs_reserve_data_bytes(struct btrfs_fs_info *fs_info, u64 bytes,
153+
enum btrfs_reserve_flush_enum flush);
152154

153155
#endif /* BTRFS_SPACE_INFO_H */

0 commit comments

Comments
 (0)