summaryrefslogtreecommitdiff
path: root/fs/dcache.cc
blob: 94ab287e0e0db845531a45fc25b2c4a8872e8824 (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright (C) 2026 Richard Qin */

/*
 * dcache.cc — Directory entry cache
 *
 * Provides dentry allocation, lookup, instantiation, and d_path.
 */

#include <lume/dcache.h>
#include <lume/fs.h>
#include <lume/file.h>
#include <lume/slab.h>
#include <lume/errno.h>
#include <lume/fs_types.h>

/* ================================================================== */
/*  Hash function                                                     */
/* ================================================================== */

uint32 full_name_hash(const char* name, uint32 len)
{
    uint32 hash = 0;
    for (uint32 i = 0; i < len; i++)
        hash = hash * 31 + static_cast<uint8>(name[i]);
    return hash;
}

/* ================================================================== */
/*  d_set_type — set DCACHE_*_TYPE bits from inode mode               */
/* ================================================================== */

void d_set_type(dentry* de, uint32 i_mode)
{
    de->d_flags &= ~DCACHE_ENTRY_TYPE;

    if (S_ISDIR(i_mode))
        de->d_flags |= DCACHE_DIRECTORY_TYPE;
    else if (S_ISLNK(i_mode))
        de->d_flags |= DCACHE_SYMLINK_TYPE;
    else if (S_ISREG(i_mode))
        de->d_flags |= DCACHE_REGULAR_TYPE;
    else
        de->d_flags |= DCACHE_SPECIAL_TYPE;
}

/* ================================================================== */
/*  d_alloc — allocate a dentry under a parent                        */
/* ================================================================== */

static void dentry_init(dentry* de, dentry* parent, const qstr* name)
{
    de->d_flags = DCACHE_MISS_TYPE;  /* negative until instantiated */
    de->d_parent = parent ? parent : de;
    de->d_inode = nullptr;
    de->d_op = nullptr;
    de->d_sb = parent ? parent->d_sb : nullptr;
    de->d_time = 0;
    de->d_fsdata = nullptr;
    de->d_lockref.store(1);

    list_init(&de->d_lru);
    list_init(&de->d_child);
    list_init(&de->d_children);
    list_init(&de->d_alias);

    /* Copy name */
    if (name && name->name && name->len > 0) {
        uint32 len = name->len;
        if (len < DNAME_INLINE_LEN) {
            for (uint32 i = 0; i < len; i++)
                de->d_iname[i] = name->name[i];
            de->d_iname[len] = '\0';
            de->d_name.name = reinterpret_cast<const char*>(de->d_iname);
        } else {
            /* Heap-allocate for long names */
            char* buf = new char[len + 1];
            for (uint32 i = 0; i < len; i++)
                buf[i] = name->name[i];
            buf[len] = '\0';
            de->d_name.name = buf;
        }
        de->d_name.len = len;
        de->d_name.hash = name->hash ? name->hash : full_name_hash(name->name, len);
    } else {
        de->d_iname[0] = '\0';
        de->d_name.name = reinterpret_cast<const char*>(de->d_iname);
        de->d_name.len = 0;
        de->d_name.hash = 0;
    }
}

dentry* d_alloc(dentry* parent, const qstr* name)
{
    auto* de = new dentry{};
    if (!de) return nullptr;

    dentry_init(de, parent, name);
    de->d_sb = parent->d_sb;

    /* Use sb default d_op if set */
    if (parent->d_sb->s_d_op)
        de->d_op = parent->d_sb->s_d_op;

    /* Link into parent's children list */
    if (parent) {
        LockGuard guard(parent->d_lock);
        list_add_tail(&de->d_child, &parent->d_children);
        dget(parent);  /* child holds ref on parent */
    }

    return de;
}

dentry* d_alloc_anon(super_block* sb)
{
    auto* de = new dentry{};
    if (!de) return nullptr;

    dentry_init(de, nullptr, nullptr);
    de->d_sb = sb;
    de->d_parent = de;  /* root: parent is self */

    return de;
}

/* ================================================================== */
/*  d_instantiate — bind an inode to a dentry                         */
/* ================================================================== */

void d_instantiate(dentry* de, inode* ip)
{
    if (!ip) return;

    LockGuard guard(de->d_lock);
    de->d_inode = ip;
    d_set_type(de, ip->i_mode);

    /* Add to inode's alias list */
    list_add_tail(&de->d_alias, &ip->i_dentry);
}

/* ================================================================== */
/*  d_make_root — create root dentry for a superblock                 */
/* ================================================================== */

dentry* d_make_root(inode* root_inode)
{
    if (!root_inode) return nullptr;

    dentry* de = d_alloc_anon(root_inode->i_sb);
    if (!de) {
        iput(root_inode);
        return nullptr;
    }

    d_instantiate(de, root_inode);
    return de;
}

/* ================================================================== */
/*  d_add — combined d_instantiate + d_rehash                         */
/* ================================================================== */

void d_add(dentry* de, inode* ip)
{
    d_instantiate(de, ip);
    /* d_rehash would add to dcache hash table; simplified for now */
}

/* ================================================================== */
/*  d_lookup — search children of parent for a name match             */
/* ================================================================== */

dentry* d_lookup(const dentry* parent, const qstr* name)
{
    LockGuard guard(const_cast<Spinlock&>(parent->d_lock));

    list_node* pos;
    list_for_each(pos, &parent->d_children) {
        dentry* child = list_entry(pos, dentry, d_child);

        if (child->d_name.len != name->len)
            continue;

        /* Compare names */
        const char* a = child->d_name.name;
        const char* b = name->name;
        uint32 len = name->len;
        bool match = true;
        for (uint32 i = 0; i < len; i++) {
            if (a[i] != b[i]) { match = false; break; }
        }
        if (match) {
            dget(child);
            return child;
        }
    }

    return nullptr;
}

/* ================================================================== */
/*  dget / dput — dentry reference counting                           */
/* ================================================================== */

dentry* dget(dentry* de)
{
    if (de)
        de->d_lockref.fetch_add(1);
    return de;
}

void dput(dentry* de)
{
    if (!de) return;

    int old = de->d_lockref.fetch_sub(1);
    if (old > 1) return;

    /* Last reference — could free, but for ramfs we keep dentries alive.
     * A real implementation would check d_op->d_delete and possibly
     * add to LRU for later reclaim. For now, just leak (Phase 1). */
}

/* ================================================================== */
/*  d_drop / d_delete                                                 */
/* ================================================================== */

void d_drop(dentry* de)
{
    /* Remove from parent's children list */
    LockGuard guard(de->d_lock);
    if (!list_empty(&de->d_child))
        list_del_init(&de->d_child);
}

void d_delete(dentry* de)
{
    d_drop(de);
}

/* ================================================================== */
/*  d_path — build absolute pathname from a path                      */
/* ================================================================== */

char* d_path(const path* p, char* buf, int buflen)
{
    if (!p || !p->de || buflen <= 0)
        return nullptr;

    /* Build path by walking up d_parent chain, writing backwards */
    char* end = buf + buflen - 1;
    *end = '\0';

    dentry* de = p->de;

    if (d_is_root(de)) {
        if (end <= buf) return nullptr;
        *(--end) = '/';
        return end;
    }

    while (!d_is_root(de)) {
        uint32 len = de->d_name.len;
        end -= len;
        if (end <= buf) return nullptr;

        const char* name = de->d_name.name;
        for (uint32 i = 0; i < len; i++)
            end[i] = name[i];

        *(--end) = '/';
        de = de->d_parent;
    }

    return end;
}

/* ================================================================== */
/*  dcache_init                                                       */
/* ================================================================== */

void dcache_init()
{
    /* Future: init dcache hash table */
}