source: mainline/uspace/srv/fs/minixfs/mfs_dentry.c@ 13ecdac9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 13ecdac9 was 13ecdac9, checked in by Maurizio Lombardi <m.lombardi85@…>, 15 years ago

Add support to directories (mkdir now works)

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/*
2 * Copyright (c) 2011 Maurizio Lombardi
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup fs
30 * @{
31 */
32
33#include "mfs.h"
34#include "mfs_utils.h"
35
36int
37read_directory_entry(struct mfs_node *mnode,
38 struct mfs_dentry_info **d_info, unsigned index)
39{
40 const struct mfs_instance *inst = mnode->instance;
41 const struct mfs_sb_info *sbi = inst->sbi;
42 const bool longnames = sbi->long_names;
43 uint32_t block;
44 block_t *b;
45
46 *d_info = malloc(sizeof(**d_info));
47 if (!*d_info)
48 return ENOMEM;
49
50 int r = read_map(&block, mnode, index * sbi->dirsize);
51 if (r != EOK)
52 goto out_err;
53
54 if (block == 0) {
55 /*End of the dentries list*/
56 r = EOK;
57 goto out_err;
58 }
59
60 r = block_get(&b, inst->handle, block, BLOCK_FLAGS_NONE);
61 if (r != EOK)
62 goto out_err;
63
64 unsigned dentries_per_zone = sbi->block_size / sbi->dirsize;
65 unsigned dentry_off = index % dentries_per_zone;
66
67 if (sbi->fs_version == MFS_VERSION_V3) {
68 struct mfs3_dentry *d3;
69
70 d3 = b->data + (dentry_off * MFS3_DIRSIZE);
71
72 (*d_info)->d_inum = conv32(sbi->native, d3->d_inum);
73 memcpy((*d_info)->d_name, d3->d_name, MFS3_MAX_NAME_LEN);
74 } else {
75 const int namelen = longnames ? MFS_L_MAX_NAME_LEN :
76 MFS_MAX_NAME_LEN;
77
78 struct mfs_dentry *d;
79
80 d = b->data + dentry_off * (longnames ? MFSL_DIRSIZE :
81 MFS_DIRSIZE);
82 (*d_info)->d_inum = conv16(sbi->native, d->d_inum);
83 memcpy((*d_info)->d_name, d->d_name, namelen);
84 }
85
86 block_put(b);
87
88 (*d_info)->index = index;
89 (*d_info)->node = mnode;
90 return EOK;
91
92out_err:
93 free(*d_info);
94 *d_info = NULL;
95 return r;
96}
97
98int
99write_dentry(struct mfs_dentry_info *d_info)
100{
101 struct mfs_node *mnode = d_info->node;
102 struct mfs_sb_info *sbi = mnode->instance->sbi;
103 const unsigned d_off_bytes = d_info->index * sbi->dirsize;
104 const unsigned dirs_per_block = sbi->block_size / sbi->dirsize;
105 block_t *b;
106 uint32_t block;
107 int r;
108
109 r = read_map(&block, mnode, d_off_bytes);
110 if (r != EOK)
111 goto out;
112
113 r = block_get(&b, mnode->instance->handle, block, BLOCK_FLAGS_NONE);
114 if (r != EOK)
115 goto out;
116
117 const size_t name_len = sbi->max_name_len;
118 uint8_t *ptr = b->data;
119 ptr += (d_info->index % dirs_per_block) * sbi->dirsize;
120
121 if (sbi->fs_version == MFS_VERSION_V3) {
122 struct mfs3_dentry *dentry;
123 dentry = (struct mfs3_dentry *) ptr;
124
125 dentry->d_inum = conv32(sbi->native, d_info->d_inum);
126 memcpy(dentry->d_name, d_info->d_name, name_len);
127 } else {
128 struct mfs_dentry *dentry;
129 dentry = (struct mfs_dentry *) ptr;
130
131 dentry->d_inum = conv16(sbi->native, d_info->d_inum);
132 memcpy(dentry->d_name, d_info->d_name, name_len);
133 }
134
135 b->dirty = true;
136 block_put(b);
137
138out:
139 return r;
140}
141
142int
143insert_dentry(struct mfs_node *mnode, const char *d_name, fs_index_t d_inum)
144{
145 int i, r;
146 struct mfs_sb_info *sbi = mnode->instance->sbi;
147 struct mfs_dentry_info *d_info;
148 bool empty_dentry_found = false;
149
150 const size_t name_len = str_size(d_name);
151
152 assert(name_len <= sbi->max_name_len);
153
154 /*Search for an empty dentry*/
155
156 for (i = 0; ; ++i) {
157 r = read_directory_entry(mnode, &d_info, i);
158 if (r != EOK)
159 return r;
160
161 if (!d_info) {
162 /*Reached the end of the dentries list*/
163 break;
164 }
165
166 if (d_info->d_inum == 0) {
167 /*This entry is not used*/
168 empty_dentry_found = true;
169 break;
170 }
171 free(d_info);
172 }
173
174 if (!empty_dentry_found) {
175 mfsdebug("inode grow\n");
176 r = inode_grow(mnode, sbi->dirsize);
177 if (r != EOK)
178 return r;
179
180 mfsdebug("read dentry %d\n", i);
181 r = read_directory_entry(mnode, &d_info, i);
182 if (r != EOK)
183 return r;
184
185 assert(d_info != NULL);
186 }
187
188 d_info->d_inum = d_inum;
189 memcpy(d_info->d_name, d_name, name_len);
190 d_info->d_name[name_len] = 0;
191
192 r = write_dentry(d_info);
193 mfsdebug("write inode\n");
194 free(d_info);
195
196 return r;
197}
198
199
200/**
201 * @}
202 */
203
Note: See TracBrowser for help on using the repository browser.