1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <stdbool.h>
20 
21 #include "block_mac.h"
22 #include "block_set.h"
23 #include "block_tree.h"
24 #include "fs.h"
25 
26 /**
27  * struct transaction - Transaction state
28  * @node:                   List node used to link transaction in fs.
29  * @fs:                     Pointer to file system state.
30  * @open_files:             List of open files.
31  * @failed:                 %true if transaction ran out of disk space, or
32  *                          collided with another transaction, %false if no
33  *                          error has occured since transaction_activate.
34  * @invalid_block_found:    %true if a block MAC mismatch or missing block was
35  *                          encountered while trying to operate on this
36  *                          transaction.
37  * @complete:               Transaction has been written to disk.
38  * @rebuild_free_set:       %true if this transaction requires the free set to
39  *                          be reuilt by a full file-system scan.
40  * @repaired:               Transaction includes a repair that should increment
41  *                          the file system repair count when committed.
42  * @min_free_block:         Used when completing a transaction to track how much
43  *                          of the free set has been updated.
44  * @last_free_block:        Similar to @last_tmp_free_block, used when
45  *                          completing a transaction.
46  * @last_tmp_free_block:    Used by allocator to prevent ping-pong between
47  *                          allocating and freeing the same block when
48  *                          re-entering the same set.
49  * @new_free_set:           New free set used while completing a transaction.
50  * @tmp_allocated:          Blocks used while transaction is pending.
51  * @allocated:              Blocks allocated by transaction.
52  * @freed:                  Blocks freed by transaction.
53  * @files_added:            Files added by transaction.
54  * @files_updated:          Files modified by transaction.
55  * @files_removed:          Files removed by transaction.
56  */
57 struct transaction {
58     struct list_node node;
59     struct fs* fs;
60     struct list_node open_files;
61     bool failed;
62     bool invalid_block_found;
63     bool complete;
64     bool rebuild_free_set;
65     bool repaired;
66     data_block_t min_free_block;
67     data_block_t last_free_block;
68     data_block_t last_tmp_free_block;
69     struct block_set* new_free_set;
70     struct block_set tmp_allocated;
71     struct block_set allocated;
72     struct block_set freed;
73 
74     struct block_tree files_added;
75     struct block_tree files_updated;
76     struct block_tree files_removed;
77 };
78 
79 void transaction_init(struct transaction* tr, struct fs* fs, bool activate);
80 void transaction_free(struct transaction* tr);
81 void transaction_activate(struct transaction* tr);
82 void transaction_fail(struct transaction* tr);
83 void transaction_complete_etc(struct transaction* tr, bool update_checkpoint);
84 void transaction_initial_super_block_complete(struct transaction* tr);
85 
transaction_is_active(struct transaction * tr)86 static inline bool transaction_is_active(struct transaction* tr) {
87     return list_in_list(&tr->allocated.node);
88 };
89 
90 bool transaction_block_need_copy(struct transaction* tr, data_block_t block);
91