This repository has been archived by the owner on Jun 29, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdir.c
186 lines (163 loc) · 4.67 KB
/
dir.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// SPDX-License-Identifier: GPL-2.0
/*
* linux/fs/apfs/dir.c
*
* Copyright (C) 2018 Ernesto A. Fernández <[email protected]>
*/
#include <linux/slab.h>
#include <linux/buffer_head.h>
#include "apfs.h"
#include "btree.h"
#include "dir.h"
#include "key.h"
#include "message.h"
#include "node.h"
#include "super.h"
/**
* apfs_drec_from_query - Read the directory record found by a successful query
* @query: the query that found the record
* @drec: Return parameter. The directory record found.
*
* Reads the directory record into @drec and performs some basic sanity checks
* as a protection against crafted filesystems. Returns 0 on success or
* -EFSCORRUPTED otherwise.
*
* The caller must not free @query while @drec is in use, because @drec->name
* points to data on disk.
*/
int apfs_drec_from_query(struct apfs_query *query, struct apfs_drec *drec)
{
char *raw = query->node->object.bh->b_data;
struct apfs_drec_hashed_key *de_key;
struct apfs_drec_val *de;
int namelen = query->key_len - sizeof(*de_key);
if (namelen < 1)
return -EFSCORRUPTED;
if (query->len < sizeof(*de))
return -EFSCORRUPTED;
de = (struct apfs_drec_val *)(raw + query->off);
de_key = (struct apfs_drec_hashed_key *)(raw + query->key_off);
if (namelen != (le32_to_cpu(de_key->name_len_and_hash) &
APFS_DREC_LEN_MASK))
return -EFSCORRUPTED;
/* Filename must be NULL-terminated */
if (de_key->name[namelen - 1] != 0)
return -EFSCORRUPTED;
drec->name = de_key->name;
drec->name_len = namelen - 1; /* Don't count the NULL termination */
drec->ino = le64_to_cpu(de->file_id);
drec->type = le16_to_cpu(de->flags) & APFS_DREC_TYPE_MASK;
if (drec->type != DT_FIFO && drec->type & 1) /* Invalid file type */
drec->type = DT_UNKNOWN;
return 0;
}
/**
* apfs_inode_by_name - Find the cnid for a given filename
* @dir: parent directory
* @child: filename
* @ino: on return, the inode number found
*
* Returns 0 and the inode number (which is the cnid of the file
* record); otherwise, return the appropriate error code.
*/
int apfs_inode_by_name(struct inode *dir, const struct qstr *child, u64 *ino)
{
struct super_block *sb = dir->i_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_key key;
struct apfs_query *query;
struct apfs_drec drec;
u64 cnid = dir->i_ino;
int err;
apfs_init_drec_hashed_key(sb, cnid, child->name, &key);
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
query->key = &key;
/*
* Distinct filenames in the same directory may (rarely) share the same
* hash. The query code cannot handle that because their order in the
* b-tree would depend on their unnormalized original names. Just get
* all the candidates and check them one by one.
*/
query->flags |= APFS_QUERY_CAT | APFS_QUERY_ANY_NAME | APFS_QUERY_EXACT;
do {
err = apfs_btree_query(sb, &query);
if (err)
goto out;
err = apfs_drec_from_query(query, &drec);
if (err)
goto out;
} while (unlikely(apfs_filename_cmp(sb, child->name, drec.name)));
*ino = drec.ino;
out:
apfs_free_query(sb, query);
return err;
}
static int apfs_readdir(struct file *file, struct dir_context *ctx)
{
struct inode *inode = file_inode(file);
struct super_block *sb = inode->i_sb;
struct apfs_sb_info *sbi = APFS_SB(sb);
struct apfs_key key;
struct apfs_query *query;
u64 cnid = inode->i_ino;
loff_t pos;
int err = 0;
if (ctx->pos == 0) {
if (!dir_emit_dot(file, ctx))
return 0;
ctx->pos++;
}
if (ctx->pos == 1) {
if (!dir_emit_dotdot(file, ctx))
return 0;
ctx->pos++;
}
query = apfs_alloc_query(sbi->s_cat_root, NULL /* parent */);
if (!query)
return -ENOMEM;
/* We want all the children for the cnid, regardless of the name */
apfs_init_drec_hashed_key(sb, cnid, NULL /* name */, &key);
query->key = &key;
query->flags = APFS_QUERY_CAT | APFS_QUERY_MULTIPLE | APFS_QUERY_EXACT;
pos = ctx->pos - 2;
while (1) {
struct apfs_drec drec;
/*
* We query for the matching records, one by one. After we
* pass ctx->pos we begin to emit them.
*
* TODO: Faster approach for large directories?
*/
err = apfs_btree_query(sb, &query);
if (err == -ENODATA) { /* Got all the records */
err = 0;
break;
}
if (err)
break;
err = apfs_drec_from_query(query, &drec);
if (err) {
apfs_alert(sb, "bad dentry record in directory 0x%llx",
cnid);
break;
}
err = 0;
if (pos <= 0) {
if (!dir_emit(ctx, drec.name, drec.name_len,
drec.ino, drec.type))
break;
}
pos--;
}
if (pos < 0)
ctx->pos -= pos;
apfs_free_query(sb, query);
return err;
}
const struct file_operations apfs_dir_operations = {
.llseek = generic_file_llseek,
.read = generic_read_dir,
.iterate_shared = apfs_readdir,
};