-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.c
150 lines (130 loc) · 5.06 KB
/
file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* This file is part of the APFS-Module.
* Copyright (c) 2019 Jordi Barcons.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/fs.h>
#include <linux/buffer_head.h>
#include <linux/slab.h>
#include "apfs.h"
#include "apfs/volume.h"
/*
* This is a recursive function. The records for an object can be distributed
* in different nodes in the B-Tree. For this reason, the function starts in a
* node branch. There can be two possibilities:
* - It is a non-leaf node: for each record, the function reads the disk
* block and then calls itself.
* - It's a leaf node: for each record, the function checks that the record
* type is APFS_TYPE_FILE_EXTENT. Next, we verify if the EXTENT containts
* the data requested by the user.
*/
static size_t read_data (struct apfs_btree_node_phys_t* node,
struct super_block* sb, struct file* filp, char __user* buf,
size_t len, loff_t* ppos)
{
struct buffer_head* bh;
struct buffer_head* bh_data;
struct inode* inode;
struct apfs_glb_info* glb_info;
struct apfs_record_file_extent_key_t* ext_key;
struct apfs_record_file_extent_val_t* ext_val;
struct apfs_btree_node_phys_t* node_chl;
struct apfs_kvloc_t* kvloc;
size_t file_len, rst_data, bytes_to_read, ret;
u_int8_t* key_zone;
u_int8_t* val_zone;
u_int8_t* toc_zone;
int ckeys, ext_len, block_size;
glb_info = (struct apfs_glb_info*) sb->s_fs_info;
block_size = sb->s_blocksize;
inode = filp->f_path.dentry->d_inode;
file_len = inode->i_size;
rst_data = file_len-(*ppos);
bytes_to_read = min(rst_data, len);
toc_zone = get_toc_zone(node);
key_zone = get_key_zone(node);
val_zone = get_val_zone(sb, node);
kvloc = (struct apfs_kvloc_t*) toc_zone;
/*
* Iterate for over each record of the node
*/
for (ckeys = 0; ckeys<le32_to_cpu(node->btn_nkeys); ckeys++, kvloc++) {
/*
* If it is not a leaf node, we go to the child node of the tree.
*/
if (le16_to_cpu(node->btn_level) != 0) {
bh = get_fstree_child(sb, node, kvloc);
if (!bh)
continue;
node_chl = (struct apfs_btree_node_phys_t*) bh->b_data;
ret = read_data(node_chl, sb, filp, buf, len, ppos);
brelse(bh);
if (ret) {
return ret;
}
continue;
}
/*
* It's a leaf node, we look for the extent entries.
*/
ext_key = (struct apfs_record_file_extent_key_t*) (key_zone + le16_to_cpu(kvloc->k.off));
ext_val = (struct apfs_record_file_extent_val_t*) (val_zone - le16_to_cpu(kvloc->v.off));
if (get_fs_obj_id(&(ext_key->hdr)) == inode->i_ino
&& get_fs_obj_type(&(ext_key->hdr)) == APFS_TYPE_FILE_EXTENT) {
ext_len = ext_val->len_and_flags & APFS_RECORD_FILE_EXTENT_LEN_MASK;
if (*ppos >= ext_key->logical_addr
&& *ppos < ext_key->logical_addr + ext_len) {
bytes_to_read = min(bytes_to_read, (size_t)(block_size-(*ppos)%(block_size)));
bh_data = sb_bread(inode->i_sb, ext_val->phys_block_num+(*ppos)/(block_size));
if (!bh_data) {
printk(KERN_ERR "apfs: unable to read block number\n");
return 0;
}
if (copy_to_user(buf, (bh_data->b_data)+(*ppos)%(block_size), bytes_to_read)) {
brelse(bh_data);
printk(KERN_ERR
"apfs: error copying data to the userspace buffer\n");
return 0;
}
*ppos += bytes_to_read;
brelse(bh_data);
return bytes_to_read;
}
}
}
return 0;
}
ssize_t apfs_read(struct file* filp, char __user* buf, size_t len,
loff_t* ppos)
{
struct inode* inode;
struct buffer_head *bh;
struct apfs_btree_node_phys_t* node;
size_t read_b;
inode = filp->f_path.dentry->d_inode;
if (inode->i_size <= *ppos)
return 0;
bh = get_inode_branch(inode->i_sb, inode->i_ino);
if (!bh)
return 0;
node = (struct apfs_btree_node_phys_t*) bh->b_data;
read_b = read_data(node, inode->i_sb, filp, buf, len, ppos);
brelse(bh);
return read_b;
}
struct file_operations apfs_file_operations = {
.owner = THIS_MODULE,
.read = apfs_read,
.llseek = generic_file_llseek
};