49
51
* either allocate a new transaction or hop into the existing one
51
static noinline int join_transaction(struct btrfs_root *root)
53
static noinline int join_transaction(struct btrfs_root *root, int nofail)
53
55
struct btrfs_transaction *cur_trans;
57
spin_lock(&root->fs_info->trans_lock);
58
if (root->fs_info->trans_no_join) {
60
spin_unlock(&root->fs_info->trans_lock);
54
65
cur_trans = root->fs_info->running_transaction;
56
cur_trans = kmem_cache_alloc(btrfs_transaction_cachep,
60
root->fs_info->generation++;
61
atomic_set(&cur_trans->num_writers, 1);
62
cur_trans->num_joined = 0;
63
cur_trans->transid = root->fs_info->generation;
64
init_waitqueue_head(&cur_trans->writer_wait);
65
init_waitqueue_head(&cur_trans->commit_wait);
66
cur_trans->in_commit = 0;
67
cur_trans->blocked = 0;
68
atomic_set(&cur_trans->use_count, 1);
69
cur_trans->commit_done = 0;
70
cur_trans->start_time = get_seconds();
72
cur_trans->delayed_refs.root = RB_ROOT;
73
cur_trans->delayed_refs.num_entries = 0;
74
cur_trans->delayed_refs.num_heads_ready = 0;
75
cur_trans->delayed_refs.num_heads = 0;
76
cur_trans->delayed_refs.flushing = 0;
77
cur_trans->delayed_refs.run_delayed_start = 0;
78
spin_lock_init(&cur_trans->delayed_refs.lock);
80
INIT_LIST_HEAD(&cur_trans->pending_snapshots);
81
list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
82
extent_io_tree_init(&cur_trans->dirty_pages,
83
root->fs_info->btree_inode->i_mapping,
85
spin_lock(&root->fs_info->new_trans_lock);
86
root->fs_info->running_transaction = cur_trans;
87
spin_unlock(&root->fs_info->new_trans_lock);
89
atomic_inc(&cur_trans->num_writers);
90
cur_trans->num_joined++;
67
atomic_inc(&cur_trans->use_count);
68
atomic_inc(&cur_trans->num_writers);
69
cur_trans->num_joined++;
70
spin_unlock(&root->fs_info->trans_lock);
73
spin_unlock(&root->fs_info->trans_lock);
75
cur_trans = kmem_cache_alloc(btrfs_transaction_cachep, GFP_NOFS);
78
spin_lock(&root->fs_info->trans_lock);
79
if (root->fs_info->running_transaction) {
80
kmem_cache_free(btrfs_transaction_cachep, cur_trans);
81
cur_trans = root->fs_info->running_transaction;
82
atomic_inc(&cur_trans->use_count);
83
atomic_inc(&cur_trans->num_writers);
84
cur_trans->num_joined++;
85
spin_unlock(&root->fs_info->trans_lock);
88
atomic_set(&cur_trans->num_writers, 1);
89
cur_trans->num_joined = 0;
90
init_waitqueue_head(&cur_trans->writer_wait);
91
init_waitqueue_head(&cur_trans->commit_wait);
92
cur_trans->in_commit = 0;
93
cur_trans->blocked = 0;
95
* One for this trans handle, one so it will live on until we
96
* commit the transaction.
98
atomic_set(&cur_trans->use_count, 2);
99
cur_trans->commit_done = 0;
100
cur_trans->start_time = get_seconds();
102
cur_trans->delayed_refs.root = RB_ROOT;
103
cur_trans->delayed_refs.num_entries = 0;
104
cur_trans->delayed_refs.num_heads_ready = 0;
105
cur_trans->delayed_refs.num_heads = 0;
106
cur_trans->delayed_refs.flushing = 0;
107
cur_trans->delayed_refs.run_delayed_start = 0;
108
spin_lock_init(&cur_trans->commit_lock);
109
spin_lock_init(&cur_trans->delayed_refs.lock);
111
INIT_LIST_HEAD(&cur_trans->pending_snapshots);
112
list_add_tail(&cur_trans->list, &root->fs_info->trans_list);
113
extent_io_tree_init(&cur_trans->dirty_pages,
114
root->fs_info->btree_inode->i_mapping);
115
root->fs_info->generation++;
116
cur_trans->transid = root->fs_info->generation;
117
root->fs_info->running_transaction = cur_trans;
118
spin_unlock(&root->fs_info->trans_lock);
99
126
* to make sure the old root from before we joined the transaction is deleted
100
127
* when the transaction commits
102
static noinline int record_root_in_trans(struct btrfs_trans_handle *trans,
103
struct btrfs_root *root)
129
static int record_root_in_trans(struct btrfs_trans_handle *trans,
130
struct btrfs_root *root)
105
132
if (root->ref_cows && root->last_trans < trans->transid) {
106
133
WARN_ON(root == root->fs_info->extent_root);
107
134
WARN_ON(root->commit_root != root->node);
137
* see below for in_trans_setup usage rules
138
* we have the reloc mutex held now, so there
139
* is only one writer in this function
141
root->in_trans_setup = 1;
143
/* make sure readers find in_trans_setup before
144
* they find our root->last_trans update
148
spin_lock(&root->fs_info->fs_roots_radix_lock);
149
if (root->last_trans == trans->transid) {
150
spin_unlock(&root->fs_info->fs_roots_radix_lock);
109
153
radix_tree_tag_set(&root->fs_info->fs_roots_radix,
110
154
(unsigned long)root->root_key.objectid,
111
155
BTRFS_ROOT_TRANS_TAG);
156
spin_unlock(&root->fs_info->fs_roots_radix_lock);
112
157
root->last_trans = trans->transid;
159
/* this is pretty tricky. We don't want to
160
* take the relocation lock in btrfs_record_root_in_trans
161
* unless we're really doing the first setup for this root in
164
* Normally we'd use root->last_trans as a flag to decide
165
* if we want to take the expensive mutex.
167
* But, we have to set root->last_trans before we
168
* init the relocation root, otherwise, we trip over warnings
169
* in ctree.c. The solution used here is to flag ourselves
170
* with root->in_trans_setup. When this is 1, we're still
171
* fixing up the reloc trees and everyone must wait.
173
* When this is zero, they can trust root->last_trans and fly
174
* through btrfs_record_root_in_trans without having to take the
175
* lock. smp_wmb() makes sure that all the writes above are
176
* done before we pop in the zero below
113
178
btrfs_init_reloc_root(trans, root);
180
root->in_trans_setup = 0;
118
186
int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
119
187
struct btrfs_root *root)
121
189
if (!root->ref_cows)
124
mutex_lock(&root->fs_info->trans_mutex);
125
if (root->last_trans == trans->transid) {
126
mutex_unlock(&root->fs_info->trans_mutex);
193
* see record_root_in_trans for comments about in_trans_setup usage
197
if (root->last_trans == trans->transid &&
198
!root->in_trans_setup)
201
mutex_lock(&root->fs_info->reloc_mutex);
130
202
record_root_in_trans(trans, root);
131
mutex_unlock(&root->fs_info->trans_mutex);
203
mutex_unlock(&root->fs_info->reloc_mutex);
141
214
struct btrfs_transaction *cur_trans;
216
spin_lock(&root->fs_info->trans_lock);
143
217
cur_trans = root->fs_info->running_transaction;
144
218
if (cur_trans && cur_trans->blocked) {
145
219
DEFINE_WAIT(wait);
146
220
atomic_inc(&cur_trans->use_count);
221
spin_unlock(&root->fs_info->trans_lock);
148
223
prepare_to_wait(&root->fs_info->transaction_wait, &wait,
149
224
TASK_UNINTERRUPTIBLE);
150
225
if (!cur_trans->blocked)
152
mutex_unlock(&root->fs_info->trans_mutex);
154
mutex_lock(&root->fs_info->trans_mutex);
156
229
finish_wait(&root->fs_info->transaction_wait, &wait);
157
230
put_transaction(cur_trans);
232
spin_unlock(&root->fs_info->trans_lock);
185
266
if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR)
186
267
return ERR_PTR(-EROFS);
269
if (current->journal_info) {
270
WARN_ON(type != TRANS_JOIN && type != TRANS_JOIN_NOLOCK);
271
h = current->journal_info;
273
h->orig_rsv = h->block_rsv;
188
278
h = kmem_cache_alloc(btrfs_trans_handle_cachep, GFP_NOFS);
190
280
return ERR_PTR(-ENOMEM);
192
if (type != TRANS_JOIN_NOLOCK)
193
mutex_lock(&root->fs_info->trans_mutex);
194
282
if (may_wait_transaction(root, type))
195
283
wait_current_trans(root);
197
ret = join_transaction(root);
286
ret = join_transaction(root, type == TRANS_JOIN_NOLOCK);
288
wait_current_trans(root);
289
} while (ret == -EBUSY);
199
292
kmem_cache_free(btrfs_trans_handle_cachep, h);
200
if (type != TRANS_JOIN_NOLOCK)
201
mutex_unlock(&root->fs_info->trans_mutex);
202
293
return ERR_PTR(ret);
205
296
cur_trans = root->fs_info->running_transaction;
206
atomic_inc(&cur_trans->use_count);
207
if (type != TRANS_JOIN_NOLOCK)
208
mutex_unlock(&root->fs_info->trans_mutex);
210
298
h->transid = cur_trans->transid;
211
299
h->transaction = cur_trans;
212
300
h->blocks_used = 0;
214
301
h->bytes_reserved = 0;
215
302
h->delayed_ref_updates = 0;
216
304
h->block_rsv = NULL;
219
308
if (cur_trans->blocked && may_wait_transaction(root, type)) {
258
344
return start_transaction(root, num_items, TRANS_START);
260
struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root,
346
struct btrfs_trans_handle *btrfs_join_transaction(struct btrfs_root *root)
263
348
return start_transaction(root, 0, TRANS_JOIN);
266
struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root,
351
struct btrfs_trans_handle *btrfs_join_transaction_nolock(struct btrfs_root *root)
269
353
return start_transaction(root, 0, TRANS_JOIN_NOLOCK);
272
struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *r,
356
struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root)
275
return start_transaction(r, 0, TRANS_USERSPACE);
358
return start_transaction(root, 0, TRANS_USERSPACE);
278
361
/* wait for a transaction commit to be fully complete */
300
379
struct btrfs_transaction *cur_trans = NULL, *t;
303
mutex_lock(&root->fs_info->trans_mutex);
307
384
if (transid <= root->fs_info->last_trans_committed)
310
387
/* find specified transaction */
388
spin_lock(&root->fs_info->trans_lock);
311
389
list_for_each_entry(t, &root->fs_info->trans_list, list) {
312
390
if (t->transid == transid) {
392
atomic_inc(&cur_trans->use_count);
316
395
if (t->transid > transid)
398
spin_unlock(&root->fs_info->trans_lock);
321
goto out_unlock; /* bad transid */
401
goto out; /* bad transid */
323
403
/* find newest transaction that is committing | committed */
404
spin_lock(&root->fs_info->trans_lock);
324
405
list_for_each_entry_reverse(t, &root->fs_info->trans_list,
326
407
if (t->in_commit) {
327
408
if (t->commit_done)
411
atomic_inc(&cur_trans->use_count);
415
spin_unlock(&root->fs_info->trans_lock);
334
goto out_unlock; /* nothing committing|committed */
417
goto out; /* nothing committing|committed */
337
atomic_inc(&cur_trans->use_count);
338
mutex_unlock(&root->fs_info->trans_mutex);
340
420
wait_for_commit(root, cur_trans);
342
mutex_lock(&root->fs_info->trans_mutex);
343
422
put_transaction(cur_trans);
346
mutex_unlock(&root->fs_info->trans_mutex);
352
* rate limit against the drop_snapshot code. This helps to slow down new
353
* operations if the drop_snapshot code isn't able to keep up.
355
static void throttle_on_drops(struct btrfs_root *root)
357
struct btrfs_fs_info *info = root->fs_info;
358
int harder_count = 0;
361
if (atomic_read(&info->throttles)) {
364
thr = atomic_read(&info->throttle_gen);
367
prepare_to_wait(&info->transaction_throttle,
368
&wait, TASK_UNINTERRUPTIBLE);
369
if (!atomic_read(&info->throttles)) {
370
finish_wait(&info->transaction_throttle, &wait);
374
finish_wait(&info->transaction_throttle, &wait);
375
} while (thr == atomic_read(&info->throttle_gen));
378
if (root->fs_info->total_ref_cache_size > 1 * 1024 * 1024 &&
382
if (root->fs_info->total_ref_cache_size > 5 * 1024 * 1024 &&
386
if (root->fs_info->total_ref_cache_size > 10 * 1024 * 1024 &&
393
428
void btrfs_throttle(struct btrfs_root *root)
395
mutex_lock(&root->fs_info->trans_mutex);
396
if (!root->fs_info->open_ioctl_trans)
430
if (!atomic_read(&root->fs_info->open_ioctl_trans))
397
431
wait_current_trans(root);
398
mutex_unlock(&root->fs_info->trans_mutex);
401
434
static int should_end_transaction(struct btrfs_trans_handle *trans,
487
528
int btrfs_end_transaction(struct btrfs_trans_handle *trans,
488
529
struct btrfs_root *root)
490
return __btrfs_end_transaction(trans, root, 0, 1);
533
ret = __btrfs_end_transaction(trans, root, 0, 1);
493
539
int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
494
540
struct btrfs_root *root)
544
ret = __btrfs_end_transaction(trans, root, 1, 1);
550
int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
551
struct btrfs_root *root)
555
ret = __btrfs_end_transaction(trans, root, 0, 0);
561
int btrfs_end_transaction_dmeta(struct btrfs_trans_handle *trans,
562
struct btrfs_root *root)
496
564
return __btrfs_end_transaction(trans, root, 1, 1);
499
int btrfs_end_transaction_nolock(struct btrfs_trans_handle *trans,
500
struct btrfs_root *root)
502
return __btrfs_end_transaction(trans, root, 0, 0);
506
568
* when btree blocks are allocated, they have some corresponding bits set for
507
569
* them in one of two extent_io trees. This is used to make sure all of
755
818
radix_tree_tag_clear(&fs_info->fs_roots_radix,
756
819
(unsigned long)root->root_key.objectid,
757
820
BTRFS_ROOT_TRANS_TAG);
821
spin_unlock(&fs_info->fs_roots_radix_lock);
759
823
btrfs_free_log(trans, root);
760
824
btrfs_update_reloc_root(trans, root);
761
825
btrfs_orphan_commit_root(trans, root);
827
btrfs_save_ino_cache(root, trans);
763
829
if (root->commit_root != root->node) {
830
mutex_lock(&root->fs_commit_mutex);
764
831
switch_commit_root(root);
832
btrfs_unpin_free_ino(root);
833
mutex_unlock(&root->fs_commit_mutex);
765
835
btrfs_set_root_node(&root->root_item,
802
874
btrfs_btree_balance_dirty(info->tree_root, nr);
805
if (root->fs_info->closing || ret != -EAGAIN)
877
if (btrfs_fs_closing(root->fs_info) || ret != -EAGAIN)
808
880
root->defrag_running = 0;
814
* when dropping snapshots, we generate a ton of delayed refs, and it makes
815
* sense not to join the transaction while it is trying to flush the current
816
* queue of delayed refs out.
818
* This is used by the drop snapshot code only
820
static noinline int wait_transaction_pre_flush(struct btrfs_fs_info *info)
824
mutex_lock(&info->trans_mutex);
825
while (info->running_transaction &&
826
info->running_transaction->delayed_refs.flushing) {
827
prepare_to_wait(&info->transaction_wait, &wait,
828
TASK_UNINTERRUPTIBLE);
829
mutex_unlock(&info->trans_mutex);
833
mutex_lock(&info->trans_mutex);
834
finish_wait(&info->transaction_wait, &wait);
836
mutex_unlock(&info->trans_mutex);
841
* Given a list of roots that need to be deleted, call btrfs_drop_snapshot on
844
int btrfs_drop_dead_root(struct btrfs_root *root)
846
struct btrfs_trans_handle *trans;
847
struct btrfs_root *tree_root = root->fs_info->tree_root;
853
* we don't want to jump in and create a bunch of
854
* delayed refs if the transaction is starting to close
856
wait_transaction_pre_flush(tree_root->fs_info);
857
trans = btrfs_start_transaction(tree_root, 1);
860
* we've joined a transaction, make sure it isn't
863
if (trans->transaction->delayed_refs.flushing) {
864
btrfs_end_transaction(trans, tree_root);
868
ret = btrfs_drop_snapshot(trans, root);
872
ret = btrfs_update_root(trans, tree_root,
878
nr = trans->blocks_used;
879
ret = btrfs_end_transaction(trans, tree_root);
882
btrfs_btree_balance_dirty(tree_root, nr);
887
ret = btrfs_del_root(trans, tree_root, &root->root_key);
890
nr = trans->blocks_used;
891
ret = btrfs_end_transaction(trans, tree_root);
894
free_extent_buffer(root->node);
895
free_extent_buffer(root->commit_root);
898
btrfs_btree_balance_dirty(tree_root, nr);
904
885
* new snapshots need to be created at a very specific time in the
905
886
* transaction commit. This does the actual creation
1066
1056
int btrfs_transaction_in_commit(struct btrfs_fs_info *info)
1069
spin_lock(&info->new_trans_lock);
1059
spin_lock(&info->trans_lock);
1070
1060
if (info->running_transaction)
1071
1061
ret = info->running_transaction->in_commit;
1072
spin_unlock(&info->new_trans_lock);
1062
spin_unlock(&info->trans_lock);
1076
1066
int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1079
spin_lock(&info->new_trans_lock);
1069
spin_lock(&info->trans_lock);
1080
1070
if (info->running_transaction)
1081
1071
ret = info->running_transaction->blocked;
1082
spin_unlock(&info->new_trans_lock);
1072
spin_unlock(&info->trans_lock);
1181
1167
/* take transaction reference */
1182
mutex_lock(&root->fs_info->trans_mutex);
1183
1168
cur_trans = trans->transaction;
1184
1169
atomic_inc(&cur_trans->use_count);
1185
mutex_unlock(&root->fs_info->trans_mutex);
1187
1171
btrfs_end_transaction(trans, root);
1188
1172
schedule_delayed_work(&ac->work, 0);
1190
1174
/* wait for transaction to start and unblock */
1191
mutex_lock(&root->fs_info->trans_mutex);
1192
1175
if (wait_for_unblock)
1193
1176
wait_current_trans_commit_start_and_unblock(root, cur_trans);
1195
1178
wait_current_trans_commit_start(root, cur_trans);
1180
if (current->journal_info == trans)
1181
current->journal_info = NULL;
1196
1183
put_transaction(cur_trans);
1197
mutex_unlock(&root->fs_info->trans_mutex);
1238
1223
ret = btrfs_run_delayed_refs(trans, root, 0);
1241
mutex_lock(&root->fs_info->trans_mutex);
1226
spin_lock(&cur_trans->commit_lock);
1242
1227
if (cur_trans->in_commit) {
1228
spin_unlock(&cur_trans->commit_lock);
1243
1229
atomic_inc(&cur_trans->use_count);
1244
mutex_unlock(&root->fs_info->trans_mutex);
1245
1230
btrfs_end_transaction(trans, root);
1247
1232
ret = wait_for_commit(root, cur_trans);
1250
mutex_lock(&root->fs_info->trans_mutex);
1251
1235
put_transaction(cur_trans);
1252
mutex_unlock(&root->fs_info->trans_mutex);
1257
1240
trans->transaction->in_commit = 1;
1258
1241
trans->transaction->blocked = 1;
1242
spin_unlock(&cur_trans->commit_lock);
1259
1243
wake_up(&root->fs_info->transaction_blocked_wait);
1245
spin_lock(&root->fs_info->trans_lock);
1261
1246
if (cur_trans->list.prev != &root->fs_info->trans_list) {
1262
1247
prev_trans = list_entry(cur_trans->list.prev,
1263
1248
struct btrfs_transaction, list);
1264
1249
if (!prev_trans->commit_done) {
1265
1250
atomic_inc(&prev_trans->use_count);
1266
mutex_unlock(&root->fs_info->trans_mutex);
1251
spin_unlock(&root->fs_info->trans_lock);
1268
1253
wait_for_commit(root, prev_trans);
1270
mutex_lock(&root->fs_info->trans_mutex);
1271
1255
put_transaction(prev_trans);
1257
spin_unlock(&root->fs_info->trans_lock);
1260
spin_unlock(&root->fs_info->trans_lock);
1275
1263
if (now < cur_trans->start_time || now - cur_trans->start_time < 1)
1302
1293
prepare_to_wait(&cur_trans->writer_wait, &wait,
1303
1294
TASK_UNINTERRUPTIBLE);
1306
1296
if (atomic_read(&cur_trans->num_writers) > 1)
1307
1297
schedule_timeout(MAX_SCHEDULE_TIMEOUT);
1308
1298
else if (should_grow)
1309
1299
schedule_timeout(1);
1311
mutex_lock(&root->fs_info->trans_mutex);
1312
1301
finish_wait(&cur_trans->writer_wait, &wait);
1313
1302
} while (atomic_read(&cur_trans->num_writers) > 1 ||
1314
1303
(should_grow && cur_trans->num_joined != joined));
1306
* Ok now we need to make sure to block out any other joins while we
1307
* commit the transaction. We could have started a join before setting
1308
* no_join so make sure to wait for num_writers to == 1 again.
1310
spin_lock(&root->fs_info->trans_lock);
1311
root->fs_info->trans_no_join = 1;
1312
spin_unlock(&root->fs_info->trans_lock);
1313
wait_event(cur_trans->writer_wait,
1314
atomic_read(&cur_trans->num_writers) == 1);
1317
* the reloc mutex makes sure that we stop
1318
* the balancing code from coming in and moving
1319
* extents around in the middle of the commit
1321
mutex_lock(&root->fs_info->reloc_mutex);
1323
ret = btrfs_run_delayed_items(trans, root);
1316
1326
ret = create_pending_snapshots(trans, root->fs_info);
1319
1329
ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
1333
* make sure none of the code above managed to slip in a
1336
btrfs_assert_delayed_root_empty(root);
1322
1338
WARN_ON(cur_trans != trans->transaction);
1340
btrfs_scrub_pause(root);
1324
1341
/* btrfs_commit_tree_roots is responsible for getting the
1325
1342
* various roots consistent with each other. Every pointer
1326
1343
* in the tree of tree roots has to point to the most up to date
1390
1408
btrfs_finish_extent_commit(trans, root);
1392
mutex_lock(&root->fs_info->trans_mutex);
1394
1410
cur_trans->commit_done = 1;
1396
1412
root->fs_info->last_trans_committed = cur_trans->transid;
1398
1414
wake_up(&cur_trans->commit_wait);
1416
spin_lock(&root->fs_info->trans_lock);
1400
1417
list_del_init(&cur_trans->list);
1418
spin_unlock(&root->fs_info->trans_lock);
1401
1420
put_transaction(cur_trans);
1402
1421
put_transaction(cur_trans);
1404
1423
trace_btrfs_transaction_commit(root);
1406
mutex_unlock(&root->fs_info->trans_mutex);
1425
btrfs_scrub_continue(root);
1408
1427
if (current->journal_info == trans)
1409
1428
current->journal_info = NULL;
1424
1443
LIST_HEAD(list);
1425
1444
struct btrfs_fs_info *fs_info = root->fs_info;
1427
mutex_lock(&fs_info->trans_mutex);
1446
spin_lock(&fs_info->trans_lock);
1428
1447
list_splice_init(&fs_info->dead_roots, &list);
1429
mutex_unlock(&fs_info->trans_mutex);
1448
spin_unlock(&fs_info->trans_lock);
1431
1450
while (!list_empty(&list)) {
1432
1451
root = list_entry(list.next, struct btrfs_root, root_list);
1433
1452
list_del(&root->root_list);
1454
btrfs_kill_all_delayed_nodes(root);
1435
1456
if (btrfs_header_backref_rev(root->node) <
1436
1457
BTRFS_MIXED_BACKREF_REV)
1437
1458
btrfs_drop_snapshot(root, NULL, 0);