Debian Codegraph @

tmpfs/dir.c

raw
1 /* Directories for tmpfs.
2 Copyright (C) 2000,01,02 Free Software Foundation, Inc.
3
4 This file is part of the GNU Hurd.
5
6 The GNU Hurd is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 The GNU Hurd is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with the GNU Hurd; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include <stddef.h>
21 #include <unistd.h>
22 #include <sys/mman.h>
23
24 #include <libdiskfs/diskfs.h>
25 #include "tmpfs.h"
26 #include <stdlib.h>
27
28 error_t
29 diskfs_init_dir (struct node *dp, struct node *pdp, struct protid *cred)
30 {
31 dp->dn->u.dir.dotdot = pdp->dn;
32 dp->dn->u.dir.entries = 0;
33
34 /* Increase hardlink count for parent directory */
35 pdp->dn_stat.st_nlink++;
36 /* Take '.' directory into account */
37 dp->dn_stat.st_nlink++;
38
39 return 0;
40 }
41
42 error_t
43 diskfs_clear_directory (struct node *dp, struct node *pdp,
44 struct protid *cred)
45 {
46 if (dp->dn->u.dir.entries != 0)
47 return ENOTEMPTY;
48 assert_backtrace (dp->dn_stat.st_size == 0);
49 assert_backtrace (dp->dn->u.dir.dotdot == pdp->dn);
50
51 /* Decrease hardlink count for parent directory */
52 pdp->dn_stat.st_nlink--;
53 /* Take '.' directory into account */
54 dp->dn_stat.st_nlink--;
55
56 return 0;
57 }
58
59 int
60 diskfs_dirempty (struct node *dp, struct protid *cred)
61 {
62 return dp->dn->u.dir.entries == 0;
63 }
64
65 error_t
66 diskfs_get_directs (struct node *dp, int entry, int n,
67 char **data, mach_msg_type_number_t *datacnt,
68 vm_size_t bufsiz, int *amt)
69 {
70 struct tmpfs_dirent *d;
71 struct dirent *entp;
72 int i;
73
74 if (bufsiz == 0)
75 bufsiz = dp->dn_stat.st_size
76 + 2 * ((offsetof (struct dirent, d_name[3]) + 7) & ~7);
77 if (bufsiz > *datacnt)
78 {
79 *data = mmap (0, bufsiz, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
80 if (*data == MAP_FAILED)
81 return ENOMEM;
82 }
83
84 /* We always synthesize the first two entries (. and ..) on the fly. */
85 entp = (struct dirent *) *data;
86 i = 0;
87 if (i++ >= entry)
88 {
89 entp->d_fileno = dp->dn_stat.st_ino;
90 entp->d_type = DT_DIR;
91 entp->d_namlen = 1;
92 entp->d_name[0] = '.';
93 entp->d_name[1] = '\0';
94 entp->d_reclen = (&entp->d_name[2] - (char *) entp + 7) & ~7;
95 entp = (void *) entp + entp->d_reclen;
96 }
97 if (i++ >= entry)
98 {
99 if (dp->dn->u.dir.dotdot == 0)
100 {
101 assert_backtrace (dp == diskfs_root_node);
102 /* Use something not zero and not an st_ino value for any node in
103 this filesystem. Since we use pointer values, 2 will never
104 be a valid number. */
105 entp->d_fileno = 2;
106 }
107 else
108 entp->d_fileno = (ino_t) (uintptr_t) dp->dn->u.dir.dotdot;
109 entp->d_type = DT_DIR;
110 entp->d_namlen = 2;
111 entp->d_name[0] = '.';
112 entp->d_name[1] = '.';
113 entp->d_name[2] = '\0';
114 entp->d_reclen = (&entp->d_name[3] - (char *) entp + 7) & ~7;
115 entp = (void *) entp + entp->d_reclen;
116 }
117
118 /* Skip ahead to the desired entry. */
119 for (d = dp->dn->u.dir.entries; i < entry && d != 0; d = d->next)
120 ++i;
121
122 if (i < entry)
123 {
124 assert_backtrace (d == 0);
125 *datacnt = 0;
126 *amt = 0;
127 return 0;
128 }
129
130 /* Now fill in the buffer with real entries. */
131 for (; d != 0; d = d->next, i++)
132 {
133 size_t rlen = (offsetof (struct dirent, d_name[1]) + d->namelen + 7) & ~7;
134 if (rlen + (char *) entp - *data > bufsiz || (n >= 0 && i > n))
135 break;
136 entp->d_fileno = (ino_t) (uintptr_t) d->dn;
137 entp->d_type = DT_UNKNOWN;
138 entp->d_namlen = d->namelen;
139 memcpy (entp->d_name, d->name, d->namelen + 1);
140 entp->d_reclen = rlen;
141 entp = (void *) entp + rlen;
142 }
143
144 *datacnt = (char *) entp - *data;
145 *amt = i - entry;
146
147 return 0;
148 }
149
150
151 struct dirstat
152 {
153 struct tmpfs_dirent **prevp;
154 int dotdot;
155 };
156 const size_t diskfs_dirstat_size = sizeof (struct dirstat);
157
158 void
159 diskfs_null_dirstat (struct dirstat *ds)
160 {
161 ds->prevp = 0;
162 }
163
164 error_t
165 diskfs_drop_dirstat (struct node *dp, struct dirstat *ds)
166 {
167 /* No need to clear the pointers. */
168 return 0;
169 }
170
171 error_t
172 diskfs_lookup_hard (struct node *dp,
173 const char *name, lookup_flags_t l_flags,
174 struct node **np, struct dirstat *ds,
175 struct protid *cred)
176 {
177 const size_t namelen = strlen (name);
178 struct tmpfs_dirent *d, **prevp;
179
180 if (l_flags == REMOVE || l_flags == RENAME)
181 assert_backtrace (np);
182
183 if (ds)
184 ds->dotdot = l_flags & SPEC_DOTDOT;
185
186 if (namelen == 1 && name[0] == '.')
187 {
188 if (np != 0)
189 {
190 *np = dp;
191 diskfs_nref (dp);
192 }
193 return 0;
194 }
195 if (namelen == 2 && name[0] == '.' && name[1] == '.')
196 {
197 struct disknode *dddn = dp->dn->u.dir.dotdot;
198 error_t err;
199
200 assert_backtrace (np != 0);
201 if (dddn == 0) /* root directory */
202 return EAGAIN;
203
204 if (l_flags == (REMOVE|SPEC_DOTDOT) || l_flags == (RENAME|SPEC_DOTDOT))
205 {
206 *np = *dddn->hprevp;
207 assert_backtrace (*np);
208 assert_backtrace ((*np)->dn == dddn);
209 assert_backtrace (*dddn->hprevp == *np);
210 return 0;
211 }
212 else
213 {
214 pthread_mutex_unlock (&dp->lock);
215 err = diskfs_cached_lookup ((ino_t) (intptr_t) dddn, np);
216
217 if (l_flags == (LOOKUP|SPEC_DOTDOT))
218 diskfs_nrele (dp);
219 else
220 pthread_mutex_lock (&dp->lock);
221
222 if (err)
223 *np = 0;
224
225 return err;
226 }
227 }
228
229 for (d = *(prevp = &dp->dn->u.dir.entries); d != 0;
230 d = *(prevp = &d->next))
231 if (d->namelen == namelen && !memcmp (d->name, name, namelen))
232 {
233 if (ds)
234 ds->prevp = prevp;
235
236 if (np)
237 return diskfs_cached_lookup ((ino_t) (uintptr_t) d->dn, np);
238 else
239 return 0;
240 }
241
242 if (ds)
243 ds->prevp = prevp;
244 if (np)
245 *np = 0;
246 return ENOENT;
247 }
248
249
250 error_t
251 diskfs_direnter_hard (struct node *dp, const char *name,
252 struct node *np, struct dirstat *ds,
253 struct protid *cred)
254 {
255 const size_t namelen = strlen (name);
256 const size_t entsize
257 = (offsetof (struct dirent, d_name[1]) + namelen + 7) & ~7;
258 struct tmpfs_dirent *new;
259
260 if (round_page (tmpfs_space_used + entsize) / vm_page_size
261 > tmpfs_page_limit)
262 return ENOSPC;
263
264 new = malloc (offsetof (struct tmpfs_dirent, name) + namelen + 1);
265 if (new == 0)
266 return ENOSPC;
267
268 new->next = 0;
269 new->dn = np->dn;
270 new->namelen = namelen;
271 memcpy (new->name, name, namelen + 1);
272 *ds->prevp = new;
273
274 dp->dn_stat.st_size += entsize;
275 adjust_used (entsize);
276
277 dp->dn_stat.st_blocks = ((sizeof *dp->dn + dp->dn->translen
278 + dp->dn_stat.st_size + 511)
279 / 512);
280 return 0;
281 }
282
283 error_t
284 diskfs_dirrewrite_hard (struct node *dp, struct node *np,
285 struct dirstat *ds)
286 {
287 if (ds->dotdot)
288 dp->dn->u.dir.dotdot = np->dn;
289 else
290 (*ds->prevp)->dn = np->dn;
291
292 return 0;
293 }
294
295 error_t
296 diskfs_dirremove_hard (struct node *dp, struct dirstat *ds)
297 {
298 struct tmpfs_dirent *d = *ds->prevp;
299 const size_t entsize
300 = (offsetof (struct dirent, d_name[1]) + d->namelen + 7) & ~7;
301
302 *ds->prevp = d->next;
303
304 if (dp->dirmod_reqs != 0)
305 diskfs_notice_dirchange (dp, DIR_CHANGED_UNLINK, d->name);
306
307 free (d);
308
309 adjust_used (-entsize);
310 dp->dn_stat.st_size -= entsize;
311 dp->dn_stat.st_blocks = ((sizeof *dp->dn + dp->dn->translen
312 + dp->dn_stat.st_size + 511)
313 / 512);
314
315 return 0;
316 }