source: mainline/uspace/srv/fs/minixfs/mfs_dentry.c@ 4bf0052a

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

Remove the inode_grow() function and fix support to sparse files

  • Property mode set to 100644
File size: 5.5 KB
RevLine 
[41202a9]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
[488f7ed]35int
[18eb736]36read_dentry(struct mfs_node *mnode,
[e80c2ff]37 struct mfs_dentry_info *d_info, unsigned index)
[41202a9]38{
39 const struct mfs_instance *inst = mnode->instance;
40 const struct mfs_sb_info *sbi = inst->sbi;
41 const bool longnames = sbi->long_names;
42 uint32_t block;
43 block_t *b;
44
[ac28650]45 int r = read_map(&block, mnode, index * sbi->dirsize);
[e80c2ff]46 on_error(r, goto out_err);
[41202a9]47
[488f7ed]48 if (block == 0) {
[9e2d6dc]49 /*End of the dentries list*/
50 r = EOK;
[488f7ed]51 goto out_err;
52 }
53
[41202a9]54 r = block_get(&b, inst->handle, block, BLOCK_FLAGS_NONE);
[e80c2ff]55 on_error(r, goto out_err);
[41202a9]56
[7a57269f]57 unsigned dentries_per_zone = sbi->block_size / sbi->dirsize;
[1494e52]58 unsigned dentry_off = index % dentries_per_zone;
[7a57269f]59
[41202a9]60 if (sbi->fs_version == MFS_VERSION_V3) {
61 struct mfs3_dentry *d3;
62
[e666ddc]63 d3 = b->data + (dentry_off * MFS3_DIRSIZE);
[d0f3692]64
[e80c2ff]65 d_info->d_inum = conv32(sbi->native, d3->d_inum);
66 memcpy(d_info->d_name, d3->d_name, MFS3_MAX_NAME_LEN);
[41202a9]67 } else {
68 const int namelen = longnames ? MFS_L_MAX_NAME_LEN :
[44c6091f]69 MFS_MAX_NAME_LEN;
[41202a9]70
71 struct mfs_dentry *d;
72
[e666ddc]73 d = b->data + dentry_off * (longnames ? MFSL_DIRSIZE :
[44c6091f]74 MFS_DIRSIZE);
[e80c2ff]75 d_info->d_inum = conv16(sbi->native, d->d_inum);
76 memcpy(d_info->d_name, d->d_name, namelen);
[41202a9]77 }
78
79 block_put(b);
80
[e80c2ff]81 d_info->index = index;
82 d_info->node = mnode;
[41202a9]83
84out_err:
[b89281b]85 return r;
[41202a9]86}
87
[87d4422]88int
89write_dentry(struct mfs_dentry_info *d_info)
90{
91 struct mfs_node *mnode = d_info->node;
92 struct mfs_sb_info *sbi = mnode->instance->sbi;
93 const unsigned d_off_bytes = d_info->index * sbi->dirsize;
94 const unsigned dirs_per_block = sbi->block_size / sbi->dirsize;
95 block_t *b;
96 uint32_t block;
97 int r;
98
99 r = read_map(&block, mnode, d_off_bytes);
[e80c2ff]100 on_error(r, goto out);
[87d4422]101
102 r = block_get(&b, mnode->instance->handle, block, BLOCK_FLAGS_NONE);
[e80c2ff]103 on_error(r, goto out);
[87d4422]104
[07dcec5]105 const size_t name_len = sbi->max_name_len;
[87d4422]106 uint8_t *ptr = b->data;
107 ptr += (d_info->index % dirs_per_block) * sbi->dirsize;
108
109 if (sbi->fs_version == MFS_VERSION_V3) {
110 struct mfs3_dentry *dentry;
111 dentry = (struct mfs3_dentry *) ptr;
112
113 dentry->d_inum = conv32(sbi->native, d_info->d_inum);
114 memcpy(dentry->d_name, d_info->d_name, name_len);
115 } else {
116 struct mfs_dentry *dentry;
117 dentry = (struct mfs_dentry *) ptr;
118
119 dentry->d_inum = conv16(sbi->native, d_info->d_inum);
120 memcpy(dentry->d_name, d_info->d_name, name_len);
121 }
122
123 b->dirty = true;
124 block_put(b);
125
126out:
127 return r;
128}
129
[c955be91]130int
131remove_dentry(struct mfs_node *mnode, const char *d_name)
132{
133 struct mfs_sb_info *sbi = mnode->instance->sbi;
[e80c2ff]134 struct mfs_dentry_info d_info;
135 int r;
[c955be91]136
137 const size_t name_len = str_size(d_name);
138
139 if (name_len > sbi->max_name_len)
140 return ENAMETOOLONG;
141
142 /*Search the directory entry to be removed*/
[e80c2ff]143 unsigned i;
144 for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize ; ++i) {
[18eb736]145 r = read_dentry(mnode, &d_info, i);
[c955be91]146 on_error(r, return r);
147
[e80c2ff]148 if (!bcmp(d_info.d_name, d_name, name_len)) {
149 d_info.d_inum = 0;
150 r = write_dentry(&d_info);
[c955be91]151 return r;
152 }
153 }
154
155 return ENOENT;
156}
157
[07dcec5]158int
[13ab195]159insert_dentry(struct mfs_node *mnode, const char *d_name, fs_index_t d_inum)
[07dcec5]160{
[e80c2ff]161 int r;
[07dcec5]162 struct mfs_sb_info *sbi = mnode->instance->sbi;
[e80c2ff]163 struct mfs_dentry_info d_info;
[07dcec5]164 bool empty_dentry_found = false;
165
166 const size_t name_len = str_size(d_name);
167
[c955be91]168 if (name_len > sbi->max_name_len)
169 return ENAMETOOLONG;
[07dcec5]170
171 /*Search for an empty dentry*/
[e80c2ff]172 unsigned i;
173 for (i = 0; i < mnode->ino_i->i_size / sbi->dirsize; ++i) {
[18eb736]174 r = read_dentry(mnode, &d_info, i);
[e80c2ff]175 on_error(r, return r);
[07dcec5]176
[e80c2ff]177 if (d_info.d_inum == 0) {
[07dcec5]178 /*This entry is not used*/
179 empty_dentry_found = true;
180 break;
181 }
182 }
183
184 if (!empty_dentry_found) {
[b2a18234]185 uint32_t b, pos;
186 pos = mnode->ino_i->i_size;
187 r = read_map(&b, mnode, pos);
[2527b1d5]188 on_error(r, goto out);
[07dcec5]189
[b2a18234]190 if (b == 0) {
191 /*Increase the inode size*/
192
193 uint32_t dummy;
194 r = mfs_alloc_zone(mnode->instance, &b);
195 on_error(r, goto out);
196 r = write_map(mnode, pos, b, &dummy);
197 on_error(r, goto out);
198 }
199
200 mnode->ino_i->i_size += sbi->dirsize;
201 mnode->ino_i->dirty = true;
202
[18eb736]203 r = read_dentry(mnode, &d_info, i);
[2527b1d5]204 on_error(r, goto out);
[07dcec5]205 }
206
[e80c2ff]207 d_info.d_inum = d_inum;
208 memcpy(d_info.d_name, d_name, name_len);
209 d_info.d_name[name_len] = 0;
[07dcec5]210
[e80c2ff]211 r = write_dentry(&d_info);
[2527b1d5]212out:
[9e2d6dc]213 return r;
[07dcec5]214}
215
216
[41202a9]217/**
218 * @}
[44c6091f]219 */
[41202a9]220
Note: See TracBrowser for help on using the repository browser.