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 #include "block_cache.h"
21 #include "block_mac.h"
22 
23 struct block_set;
24 struct fs;
25 struct transaction;
26 
27 #define FS_PATH_MAX (64 + 128)
28 
29 struct storage_file_handle {
30     struct list_node node;
31     struct block_mac to_commit_block_mac;
32     struct block_mac committed_block_mac;
33     struct block_mac block_mac;
34     data_block_t to_commit_size;
35     data_block_t size;
36     bool used_by_tr;
37 };
38 
39 /**
40  * struct file_info - On-disk file entry
41  * @size:       File size in bytes.
42  * @reserved:   Reserved for future use. Write 0, read ignore.
43  * @path:       File path and name.
44  */
45 struct file_info {
46     data_block_t size;
47     uint64_t reserved;
48     char path[FS_PATH_MAX];
49 };
50 
51 /**
52  * struct file_iterate_state - File iterator state
53  */
54 struct file_iterate_state {
55     /**
56      * file - Found file callback
57      * @iter:       Iterator object.
58      * @tr:         Transaction object.
59      * @block_mac:  File entry block_mac.
60      * @added:      %true if file was added in current transaction and has not
61      *              yet been committed
62      */
63     bool (*file)(struct file_iterate_state* iter,
64                  struct transaction* tr,
65                  const struct block_mac* block_mac,
66                  bool added,
67                  bool removed);
68 };
69 
70 size_t get_file_block_size(struct fs* fs);
71 const void* file_get_block(struct transaction* tr,
72                            struct storage_file_handle* file,
73                            data_block_t file_block,
74                            struct obj_ref* ref);
75 void* file_get_block_write(struct transaction* tr,
76                            struct storage_file_handle* file,
77                            data_block_t file_block,
78                            bool read,
79                            struct obj_ref* ref);
80 void file_block_put(const void* data, struct obj_ref* data_ref);
81 void file_block_put_dirty(struct transaction* tr,
82                           struct storage_file_handle* file,
83                           data_block_t file_block,
84                           void* data,
85                           struct obj_ref* data_ref);
86 
87 const struct file_info* file_get_info(struct transaction* tr,
88                                       const struct block_mac* block_mac,
89                                       struct obj_ref* ref);
90 void file_info_put(const struct file_info* data, struct obj_ref* data_ref);
91 
92 bool file_get_size(struct transaction* tr,
93                    struct storage_file_handle* file,
94                    data_block_t* size);
95 void file_set_size(struct transaction* tr,
96                    struct storage_file_handle* file,
97                    data_block_t size);
98 
99 void file_print(struct transaction* tr, const struct storage_file_handle* file);
100 void files_print(struct transaction* tr);
101 
102 bool file_check(struct transaction* tr, const struct storage_file_handle* file);
103 
104 void file_transaction_complete(struct transaction* tr,
105                                struct block_mac* new_files_block_mac);
106 void file_transaction_complete_failed(struct transaction* tr);
107 
108 void file_transaction_success(struct transaction* tr);
109 void file_transaction_failed(struct transaction* tr);
110 void files_rebuild_free_set(struct transaction* tr,
111                             struct block_set* new_free_set,
112                             struct block_mac* files_root);
113 
114 /* TODO: move to dir? */
115 enum file_create_mode {
116     FILE_OPEN_NO_CREATE,
117     FILE_OPEN_CREATE,
118     FILE_OPEN_CREATE_EXCLUSIVE,
119 };
120 
121 /**
122  * enum file_op_result - Result of attempting to operate on a file
123  * @FILE_OP_SUCCESS: File was opened successfully.
124  * @FILE_OP_ERR_FAILED: Transaction failed while attempting to open the file.
125  * @FILE_OP_ERR_EXIST: File was found but exclusive access was requested.
126  * @FILE_OP_ERR_ALREADY_OPEN: File is already open in the provided
127  *                            transaction.
128  * @FILE_OP_ERR_NOT_FOUND: File was not found.
129  * @FILE_OP_ERR_FS_REPAIRED: File system has been repaired which may have
130  *                           impacted the requested file. Pass @allow_repaired
131  *                           = true to accept the repaired state.
132  */
133 enum file_op_result {
134     FILE_OP_SUCCESS,
135     FILE_OP_ERR_FAILED,
136     FILE_OP_ERR_EXIST,
137     FILE_OP_ERR_ALREADY_OPEN,
138     FILE_OP_ERR_NOT_FOUND,
139     FILE_OP_ERR_FS_REPAIRED,
140 };
141 enum file_op_result file_open(struct transaction* tr,
142                               const char* path,
143                               struct storage_file_handle* file,
144                               enum file_create_mode create,
145                               bool allow_repaired);
146 void file_close(struct storage_file_handle* file);
147 
148 enum file_op_result file_delete(struct transaction* tr,
149                                 const char* path,
150                                 bool allow_repaired);
151 enum file_op_result file_move(struct transaction* tr,
152                               struct storage_file_handle* file,
153                               const char* dest_path,
154                               enum file_create_mode dest_create,
155                               bool allow_repaired);
156 enum file_op_result file_iterate(struct transaction* tr,
157                                  const char* start_path,
158                                  bool added,
159                                  struct file_iterate_state* state,
160                                  bool allow_repaired);
161