@@ -170,13 +170,13 @@ static int __create_free_space_inode(struct btrfs_root *root,
170
170
key .objectid = BTRFS_FREE_SPACE_OBJECTID ;
171
171
key .offset = offset ;
172
172
key .type = 0 ;
173
-
174
173
ret = btrfs_insert_empty_item (trans , root , path , & key ,
175
174
sizeof (struct btrfs_free_space_header ));
176
175
if (ret < 0 ) {
177
176
btrfs_release_path (path );
178
177
return ret ;
179
178
}
179
+
180
180
leaf = path -> nodes [0 ];
181
181
header = btrfs_item_ptr (leaf , path -> slots [0 ],
182
182
struct btrfs_free_space_header );
@@ -296,13 +296,15 @@ static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
296
296
io_ctl -> num_pages = num_pages ;
297
297
io_ctl -> root = root ;
298
298
io_ctl -> check_crcs = check_crcs ;
299
+ io_ctl -> inode = inode ;
299
300
300
301
return 0 ;
301
302
}
302
303
303
304
static void io_ctl_free (struct btrfs_io_ctl * io_ctl )
304
305
{
305
306
kfree (io_ctl -> pages );
307
+ io_ctl -> pages = NULL ;
306
308
}
307
309
308
310
static void io_ctl_unmap_page (struct btrfs_io_ctl * io_ctl )
@@ -1092,6 +1094,61 @@ cleanup_write_cache_enospc(struct inode *inode,
1092
1094
GFP_NOFS );
1093
1095
}
1094
1096
1097
+ int btrfs_wait_cache_io (struct btrfs_root * root ,
1098
+ struct btrfs_trans_handle * trans ,
1099
+ struct btrfs_block_group_cache * block_group ,
1100
+ struct btrfs_io_ctl * io_ctl ,
1101
+ struct btrfs_path * path , u64 offset )
1102
+ {
1103
+ int ret ;
1104
+ struct inode * inode = io_ctl -> inode ;
1105
+
1106
+ root = root -> fs_info -> tree_root ;
1107
+
1108
+ /* Flush the dirty pages in the cache file. */
1109
+ ret = flush_dirty_cache (inode );
1110
+ if (ret )
1111
+ goto out ;
1112
+
1113
+ /* Update the cache item to tell everyone this cache file is valid. */
1114
+ ret = update_cache_item (trans , root , inode , path , offset ,
1115
+ io_ctl -> entries , io_ctl -> bitmaps );
1116
+ out :
1117
+ io_ctl_free (io_ctl );
1118
+ if (ret ) {
1119
+ invalidate_inode_pages2 (inode -> i_mapping );
1120
+ BTRFS_I (inode )-> generation = 0 ;
1121
+ if (block_group ) {
1122
+ #ifdef DEBUG
1123
+ btrfs_err (root -> fs_info ,
1124
+ "failed to write free space cache for block group %llu" ,
1125
+ block_group -> key .objectid );
1126
+ #endif
1127
+ }
1128
+ }
1129
+ btrfs_update_inode (trans , root , inode );
1130
+
1131
+ if (block_group ) {
1132
+ spin_lock (& block_group -> lock );
1133
+
1134
+ /*
1135
+ * only mark this as written if we didn't get put back on
1136
+ * the dirty list while waiting for IO.
1137
+ */
1138
+ if (!ret && list_empty (& block_group -> dirty_list ))
1139
+ block_group -> disk_cache_state = BTRFS_DC_WRITTEN ;
1140
+ else if (ret )
1141
+ block_group -> disk_cache_state = BTRFS_DC_ERROR ;
1142
+
1143
+ spin_unlock (& block_group -> lock );
1144
+ io_ctl -> inode = NULL ;
1145
+ iput (inode );
1146
+ }
1147
+
1148
+ return ret ;
1149
+
1150
+ }
1151
+
1095
1152
/**
1096
1153
* __btrfs_write_out_cache - write out cached info to an inode
1097
1154
* @root - the root the inode belongs to
@@ -1108,20 +1165,22 @@ cleanup_write_cache_enospc(struct inode *inode,
1108
1165
static int __btrfs_write_out_cache (struct btrfs_root * root , struct inode * inode ,
1109
1166
struct btrfs_free_space_ctl * ctl ,
1110
1167
struct btrfs_block_group_cache * block_group ,
1168
+ struct btrfs_io_ctl * io_ctl ,
1111
1169
struct btrfs_trans_handle * trans ,
1112
1170
struct btrfs_path * path , u64 offset )
1113
1171
{
1114
1172
struct extent_state * cached_state = NULL ;
1115
- struct btrfs_io_ctl io_ctl ;
1116
1173
LIST_HEAD (bitmap_list );
1117
1174
int entries = 0 ;
1118
1175
int bitmaps = 0 ;
1119
1176
int ret ;
1177
+ int must_iput = 0 ;
1120
1178
1121
1179
if (!i_size_read (inode ))
1122
1180
return -1 ;
1123
1181
1124
- ret = io_ctl_init (& io_ctl , inode , root , 1 );
1182
+ WARN_ON (io_ctl -> pages );
1183
+ ret = io_ctl_init (io_ctl , inode , root , 1 );
1125
1184
if (ret )
1126
1185
return -1 ;
1127
1186
@@ -1134,22 +1193,23 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1134
1193
up_write (& block_group -> data_rwsem );
1135
1194
BTRFS_I (inode )-> generation = 0 ;
1136
1195
ret = 0 ;
1196
+ must_iput = 1 ;
1137
1197
goto out ;
1138
1198
}
1139
1199
spin_unlock (& block_group -> lock );
1140
1200
}
1141
1201
1142
1202
/* Lock all pages first so we can lock the extent safely. */
1143
- io_ctl_prepare_pages (& io_ctl , inode , 0 );
1203
+ io_ctl_prepare_pages (io_ctl , inode , 0 );
1144
1204
1145
1205
lock_extent_bits (& BTRFS_I (inode )-> io_tree , 0 , i_size_read (inode ) - 1 ,
1146
1206
0 , & cached_state );
1147
1207
1148
- io_ctl_set_generation (& io_ctl , trans -> transid );
1208
+ io_ctl_set_generation (io_ctl , trans -> transid );
1149
1209
1150
1210
mutex_lock (& ctl -> cache_writeout_mutex );
1151
1211
/* Write out the extent entries in the free space cache */
1152
- ret = write_cache_extent_entries (& io_ctl , ctl ,
1212
+ ret = write_cache_extent_entries (io_ctl , ctl ,
1153
1213
block_group , & entries , & bitmaps ,
1154
1214
& bitmap_list );
1155
1215
if (ret ) {
@@ -1162,7 +1222,7 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1162
1222
* they will be added into free space cache after the transaction is
1163
1223
* committed, we shouldn't lose them.
1164
1224
*/
1165
- ret = write_pinned_extent_entries (root , block_group , & io_ctl , & entries );
1225
+ ret = write_pinned_extent_entries (root , block_group , io_ctl , & entries );
1166
1226
if (ret ) {
1167
1227
mutex_unlock (& ctl -> cache_writeout_mutex );
1168
1228
goto out_nospc ;
@@ -1173,16 +1233,16 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1173
1233
* locked while doing it because a concurrent trim can be manipulating
1174
1234
* or freeing the bitmap.
1175
1235
*/
1176
- ret = write_bitmap_entries (& io_ctl , & bitmap_list );
1236
+ ret = write_bitmap_entries (io_ctl , & bitmap_list );
1177
1237
mutex_unlock (& ctl -> cache_writeout_mutex );
1178
1238
if (ret )
1179
1239
goto out_nospc ;
1180
1240
1181
1241
/* Zero out the rest of the pages just to make sure */
1182
- io_ctl_zero_remaining_pages (& io_ctl );
1242
+ io_ctl_zero_remaining_pages (io_ctl );
1183
1243
1184
1244
/* Everything is written out, now we dirty the pages in the file. */
1185
- ret = btrfs_dirty_pages (root , inode , io_ctl . pages , io_ctl . num_pages ,
1245
+ ret = btrfs_dirty_pages (root , inode , io_ctl -> pages , io_ctl -> num_pages ,
1186
1246
0 , i_size_read (inode ), & cached_state );
1187
1247
if (ret )
1188
1248
goto out_nospc ;
@@ -1193,30 +1253,39 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1193
1253
* Release the pages and unlock the extent, we will flush
1194
1254
* them out later
1195
1255
*/
1196
- io_ctl_drop_pages (& io_ctl );
1256
+ io_ctl_drop_pages (io_ctl );
1197
1257
1198
1258
unlock_extent_cached (& BTRFS_I (inode )-> io_tree , 0 ,
1199
1259
i_size_read (inode ) - 1 , & cached_state , GFP_NOFS );
1200
1260
1201
- /* Flush the dirty pages in the cache file. */
1202
- ret = flush_dirty_cache (inode );
1261
+ /*
1262
+ * at this point the pages are under IO and we're happy,
1263
+ * The caller is responsible for waiting on them and updating the
1264
+ * the cache and the inode
1265
+ */
1266
+ io_ctl -> entries = entries ;
1267
+ io_ctl -> bitmaps = bitmaps ;
1268
+
1269
+ ret = btrfs_fdatawrite_range (inode , 0 , (u64 )- 1 );
1203
1270
if (ret )
1204
1271
goto out ;
1205
1272
1206
- /* Update the cache item to tell everyone this cache file is valid. */
1207
- ret = update_cache_item (trans , root , inode , path , offset ,
1208
- entries , bitmaps );
1273
+ return 0 ;
1274
+
1209
1275
out :
1210
- io_ctl_free (& io_ctl );
1276
+ io_ctl -> inode = NULL ;
1277
+ io_ctl_free (io_ctl );
1211
1278
if (ret ) {
1212
1279
invalidate_inode_pages2 (inode -> i_mapping );
1213
1280
BTRFS_I (inode )-> generation = 0 ;
1214
1281
}
1215
1282
btrfs_update_inode (trans , root , inode );
1283
+ if (must_iput )
1284
+ iput (inode );
1216
1285
return ret ;
1217
1286
1218
1287
out_nospc :
1219
- cleanup_write_cache_enospc (inode , & io_ctl , & cached_state , & bitmap_list );
1288
+ cleanup_write_cache_enospc (inode , io_ctl , & cached_state , & bitmap_list );
1220
1289
1221
1290
if (block_group && (block_group -> flags & BTRFS_BLOCK_GROUP_DATA ))
1222
1291
up_write (& block_group -> data_rwsem );
@@ -1232,7 +1301,6 @@ int btrfs_write_out_cache(struct btrfs_root *root,
1232
1301
struct btrfs_free_space_ctl * ctl = block_group -> free_space_ctl ;
1233
1302
struct inode * inode ;
1234
1303
int ret = 0 ;
1235
- enum btrfs_disk_cache_state dcs = BTRFS_DC_WRITTEN ;
1236
1304
1237
1305
root = root -> fs_info -> tree_root ;
1238
1306
@@ -1253,22 +1321,28 @@ int btrfs_write_out_cache(struct btrfs_root *root,
1253
1321
if (IS_ERR (inode ))
1254
1322
return 0 ;
1255
1323
1256
- ret = __btrfs_write_out_cache (root , inode , ctl , block_group , trans ,
1324
+ ret = __btrfs_write_out_cache (root , inode , ctl , block_group ,
1325
+ & block_group -> io_ctl , trans ,
1257
1326
path , block_group -> key .objectid );
1258
1327
if (ret ) {
1259
- dcs = BTRFS_DC_ERROR ;
1260
- ret = 0 ;
1261
1328
#ifdef DEBUG
1262
1329
btrfs_err (root -> fs_info ,
1263
1330
"failed to write free space cache for block group %llu" ,
1264
1331
block_group -> key .objectid );
1265
1332
#endif
1333
+ spin_lock (& block_group -> lock );
1334
+ block_group -> disk_cache_state = BTRFS_DC_ERROR ;
1335
+ spin_unlock (& block_group -> lock );
1336
+
1337
+ block_group -> io_ctl .inode = NULL ;
1338
+ iput (inode );
1266
1339
}
1267
1340
1268
- spin_lock (& block_group -> lock );
1269
- block_group -> disk_cache_state = dcs ;
1270
- spin_unlock (& block_group -> lock );
1271
- iput (inode );
1341
+ /*
1342
+ * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1343
+ * to wait for IO and put the inode
1344
+ */
1345
+
1272
1346
return ret ;
1273
1347
}
1274
1348
@@ -3331,11 +3405,14 @@ int btrfs_write_out_ino_cache(struct btrfs_root *root,
3331
3405
{
3332
3406
struct btrfs_free_space_ctl * ctl = root -> free_ino_ctl ;
3333
3407
int ret ;
3408
+ struct btrfs_io_ctl io_ctl ;
3334
3409
3335
3410
if (!btrfs_test_opt (root , INODE_MAP_CACHE ))
3336
3411
return 0 ;
3337
3412
3338
- ret = __btrfs_write_out_cache (root , inode , ctl , NULL , trans , path , 0 );
3413
+ ret = __btrfs_write_out_cache (root , inode , ctl , NULL , & io_ctl ,
3414
+ trans , path , 0 ) ||
3415
+ btrfs_wait_cache_io (root , trans , NULL , & io_ctl , path , 0 );
3339
3416
if (ret ) {
3340
3417
btrfs_delalloc_release_metadata (inode , inode -> i_size );
3341
3418
#ifdef DEBUG
0 commit comments