tmpfs_vfsops.c revision 1.7 1 /* $NetBSD: tmpfs_vfsops.c,v 1.7 2005/09/25 16:28:43 jmmv Exp $ */
2
3 /*
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 * 2005 program.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Efficient memory file system.
42 *
43 * tmpfs is a file system that uses NetBSD's virtual memory sub-system
44 * (the well-known UVM) to store file data and metadata in an efficient
45 * way. This means that it does not follow the structure of an on-disk
46 * file system because it simply does not need to. Instead, it uses
47 * memory-specific data structures and algorithms to automatically
48 * allocate and release resources.
49 */
50
51 #include <sys/cdefs.h>
52 __KERNEL_RCSID(0, "$NetBSD: tmpfs_vfsops.c,v 1.7 2005/09/25 16:28:43 jmmv Exp $");
53
54 #include <sys/param.h>
55 #include <sys/types.h>
56 #include <sys/malloc.h>
57 #include <sys/mount.h>
58 #include <sys/systm.h>
59 #include <sys/vnode.h>
60
61 #include <fs/tmpfs/tmpfs.h>
62
63 MALLOC_DEFINE(M_TMPFSMNT, "tmpfs mount", "tmpfs mount structures");
64
65 /* --------------------------------------------------------------------- */
66
67 static int tmpfs_mount(struct mount *, const char *, void *,
68 struct nameidata *, struct proc *);
69 static int tmpfs_start(struct mount *, int, struct proc *);
70 static int tmpfs_unmount(struct mount *, int, struct proc *);
71 static int tmpfs_root(struct mount *, struct vnode **);
72 static int tmpfs_quotactl(struct mount *, int, uid_t, void *,
73 struct proc *);
74 static int tmpfs_vget(struct mount *, ino_t, struct vnode **);
75 static int tmpfs_fhtovp(struct mount *, struct fid *, struct vnode **);
76 static int tmpfs_vptofh(struct vnode *, struct fid *);
77 static int tmpfs_statvfs(struct mount *, struct statvfs *, struct proc *);
78 static int tmpfs_sync(struct mount *, int, struct ucred *, struct proc *);
79 static void tmpfs_init(void);
80 static void tmpfs_done(void);
81 static int tmpfs_snapshot(struct mount *, struct vnode *,
82 struct timespec *);
83
84 /* --------------------------------------------------------------------- */
85
86 static int
87 tmpfs_mount(struct mount *mp, const char *path, void *data,
88 struct nameidata *ndp, struct proc *p)
89 {
90 int error;
91 ino_t nodes;
92 size_t pages;
93 struct tmpfs_mount *tmp;
94 struct tmpfs_node *root;
95 struct tmpfs_args args;
96
97 /* Handle retrieval of mount point arguments. */
98 if (mp->mnt_flag & MNT_GETARGS) {
99 if (mp->mnt_data == NULL)
100 return EIO;
101 tmp = VFS_TO_TMPFS(mp);
102
103 args.ta_version = TMPFS_ARGS_VERSION;
104 args.ta_nodes_max = tmp->tm_nodes_max;
105 args.ta_size_max = tmp->tm_pages_max * PAGE_SIZE;
106
107 root = tmp->tm_root;
108 args.ta_root_uid = root->tn_uid;
109 args.ta_root_gid = root->tn_gid;
110 args.ta_root_mode = root->tn_mode;
111
112 return copyout(&args, data, sizeof(args));
113 }
114
115 /* Verify that we have parameters, as they are required. */
116 if (data == NULL)
117 return EINVAL;
118
119 /* Get the mount parameters. */
120 error = copyin(data, &args, sizeof(args));
121 if (error != 0)
122 return error;
123
124 if (mp->mnt_flag & MNT_UPDATE) {
125 /* XXX: There is no support yet to update file system
126 * settings. Should be added. */
127
128 return EOPNOTSUPP;
129 }
130
131 if (args.ta_version != TMPFS_ARGS_VERSION)
132 return EINVAL;
133
134 /* Do not allow mounts if we do not have enough memory to preserve
135 * the minimum reserved pages. */
136 if (tmpfs_mem_info(TRUE) < TMPFS_PAGES_RESERVED)
137 return EINVAL;
138
139 /* Get the maximum number of memory pages this file system is
140 * allowed to use, based on the maximum size the user passed in
141 * the mount structure. A value of zero is treated as if the
142 * maximum available space was requested. */
143 if (args.ta_size_max < PAGE_SIZE)
144 pages = SIZE_MAX;
145 else
146 pages = args.ta_size_max / PAGE_SIZE +
147 (args.ta_size_max % PAGE_SIZE == 0 ? 0 : 1);
148 KASSERT(pages > 0);
149
150 if (args.ta_nodes_max <= 3)
151 nodes = 3 + pages * PAGE_SIZE / 1024;
152 else
153 nodes = args.ta_nodes_max;
154 KASSERT(nodes >= 3);
155
156 /* Allocate the tmpfs mount structure and fill it. */
157 tmp = (struct tmpfs_mount *)malloc(sizeof(struct tmpfs_mount),
158 M_TMPFSMNT, M_WAITOK);
159 KASSERT(tmp != NULL);
160
161 tmp->tm_nodes_max = nodes;
162 tmp->tm_nodes_last = 2;
163 LIST_INIT(&tmp->tm_nodes_used);
164 LIST_INIT(&tmp->tm_nodes_avail);
165
166 tmp->tm_pages_max = pages;
167 tmp->tm_pages_used = 0;
168 tmpfs_pool_init(&tmp->tm_dirent_pool, sizeof(struct tmpfs_dirent),
169 "dirent", tmp);
170 tmpfs_pool_init(&tmp->tm_node_pool, sizeof(struct tmpfs_node),
171 "node", tmp);
172 tmpfs_str_pool_init(&tmp->tm_str_pool, tmp);
173
174 /* Allocate the root node. */
175 error = tmpfs_alloc_node(tmp, VDIR, args.ta_root_uid,
176 args.ta_root_gid, args.ta_root_mode, NULL, NULL, VNOVAL, p,
177 &root);
178 KASSERT(error == 0 && root != NULL);
179 tmp->tm_root = root;
180
181 mp->mnt_data = tmp;
182 mp->mnt_flag |= MNT_LOCAL;
183 mp->mnt_stat.f_namemax = MAXNAMLEN;
184 vfs_getnewfsid(mp);
185
186 return set_statvfs_info(path, UIO_USERSPACE, "tmpfs", UIO_SYSSPACE,
187 mp, p);
188 }
189
190 /* --------------------------------------------------------------------- */
191
192 static int
193 tmpfs_start(struct mount *mp, int flags, struct proc *p)
194 {
195
196 return 0;
197 }
198
199 /* --------------------------------------------------------------------- */
200
201 /* ARGSUSED2 */
202 static int
203 tmpfs_unmount(struct mount *mp, int mntflags, struct proc *p)
204 {
205 int error;
206 int flags = 0;
207 struct tmpfs_mount *tmp;
208 struct tmpfs_node *node;
209
210 /* Handle forced unmounts. */
211 if (mntflags & MNT_FORCE)
212 flags |= FORCECLOSE;
213
214 /* Finalize all pending I/O. */
215 error = vflush(mp, NULL, flags);
216 if (error != 0)
217 return error;
218
219 tmp = VFS_TO_TMPFS(mp);
220
221 /* Free all associated data. The loop iterates over the linked list
222 * we have containing all used nodes. For each of them that is
223 * a directory, we free all its directory entries. Note that after
224 * freeing a node, it will automatically go to the available list,
225 * so we will later have to iterate over it to release its items. */
226 node = LIST_FIRST(&tmp->tm_nodes_used);
227 while (node != NULL) {
228 struct tmpfs_node *next;
229
230 if (node->tn_type == VDIR) {
231 struct tmpfs_dirent *de;
232
233 de = TAILQ_FIRST(&node->tn_dir);
234 while (de != NULL) {
235 struct tmpfs_dirent *nde;
236
237 nde = TAILQ_NEXT(de, td_entries);
238 tmpfs_free_dirent(tmp, de, FALSE);
239 de = nde;
240 node->tn_size -= sizeof(struct tmpfs_dirent);
241 }
242 }
243
244 next = LIST_NEXT(node, tn_entries);
245 tmpfs_free_node(tmp, node);
246 node = next;
247 }
248 node = LIST_FIRST(&tmp->tm_nodes_avail);
249 while (node != NULL) {
250 struct tmpfs_node *next;
251
252 next = LIST_NEXT(node, tn_entries);
253 LIST_REMOVE(node, tn_entries);
254 TMPFS_POOL_PUT(&tmp->tm_node_pool, node);
255 node = next;
256 }
257
258 tmpfs_pool_destroy(&tmp->tm_dirent_pool);
259 tmpfs_pool_destroy(&tmp->tm_node_pool);
260 tmpfs_str_pool_destroy(&tmp->tm_str_pool);
261
262 KASSERT(tmp->tm_pages_used == 0);
263
264 /* Throw away the tmpfs_mount structure. */
265 free(mp->mnt_data, M_TMPFSMNT);
266 mp->mnt_data = NULL;
267
268 return 0;
269 }
270
271 /* --------------------------------------------------------------------- */
272
273 static int
274 tmpfs_root(struct mount *mp, struct vnode **vpp)
275 {
276
277 return tmpfs_alloc_vp(mp, VFS_TO_TMPFS(mp)->tm_root, vpp);
278 }
279
280 /* --------------------------------------------------------------------- */
281
282 static int
283 tmpfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg,
284 struct proc *p)
285 {
286
287 printf("tmpfs_quotactl called; need for it unknown yet\n");
288 return EOPNOTSUPP;
289 }
290
291 /* --------------------------------------------------------------------- */
292
293 static int
294 tmpfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
295 {
296
297 printf("tmpfs_vget called; need for it unknown yet\n");
298 return EOPNOTSUPP;
299 }
300
301 /* --------------------------------------------------------------------- */
302
303 static int
304 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
305 {
306 boolean_t found;
307 struct tmpfs_fid *tfhp;
308 struct tmpfs_mount *tmp;
309 struct tmpfs_node *node;
310
311 tmp = VFS_TO_TMPFS(mp);
312
313 tfhp = (struct tmpfs_fid *)fhp;
314 if (tfhp->tf_len != sizeof(struct tmpfs_fid))
315 return EINVAL;
316
317 if (tfhp->tf_id >= tmp->tm_nodes_max)
318 return EINVAL;
319
320 found = FALSE;
321 LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) {
322 if (node->tn_id == tfhp->tf_id &&
323 node->tn_gen == tfhp->tf_gen) {
324 found = TRUE;
325 break;
326 }
327 }
328
329 return found ? tmpfs_alloc_vp(mp, node, vpp) : EINVAL;
330 }
331
332 /* --------------------------------------------------------------------- */
333
334 static int
335 tmpfs_vptofh(struct vnode *vp, struct fid *fhp)
336 {
337 struct tmpfs_fid *tfhp;
338 struct tmpfs_node *node;
339
340 tfhp = (struct tmpfs_fid *)fhp;
341 node = VP_TO_TMPFS_NODE(vp);
342
343 tfhp->tf_len = sizeof(struct tmpfs_fid);
344 tfhp->tf_id = node->tn_id;
345 tfhp->tf_gen = node->tn_gen;
346
347 return 0;
348 }
349
350 /* --------------------------------------------------------------------- */
351
352 /* ARGSUSED2 */
353 static int
354 tmpfs_statvfs(struct mount *mp, struct statvfs *sbp, struct proc *p)
355 {
356 fsfilcnt_t freenodes, usednodes;
357 struct tmpfs_mount *tmp;
358 struct tmpfs_node *dummy;
359
360 tmp = VFS_TO_TMPFS(mp);
361
362 sbp->f_iosize = sbp->f_frsize = sbp->f_bsize = PAGE_SIZE;
363
364 sbp->f_blocks = TMPFS_PAGES_MAX(tmp);
365 sbp->f_bavail = sbp->f_bfree = TMPFS_PAGES_AVAIL(tmp);
366 sbp->f_bresvd = 0;
367
368 freenodes = MIN(tmp->tm_nodes_max - tmp->tm_nodes_last,
369 TMPFS_PAGES_AVAIL(tmp) * PAGE_SIZE / sizeof(struct tmpfs_node));
370 LIST_FOREACH(dummy, &tmp->tm_nodes_avail, tn_entries)
371 freenodes++;
372
373 usednodes = 0;
374 LIST_FOREACH(dummy, &tmp->tm_nodes_used, tn_entries)
375 usednodes++;
376
377 sbp->f_files = freenodes + usednodes;
378 sbp->f_favail = sbp->f_ffree = freenodes;
379 sbp->f_fresvd = 0;
380
381 copy_statvfs_info(sbp, mp);
382
383 return 0;
384 }
385
386 /* --------------------------------------------------------------------- */
387
388 /* ARGSUSED0 */
389 static int
390 tmpfs_sync(struct mount *mp, int waitfor, struct ucred *uc, struct proc *p)
391 {
392
393 return 0;
394 }
395
396 /* --------------------------------------------------------------------- */
397
398 static void
399 tmpfs_init(void)
400 {
401
402 #ifdef _LKM
403 malloc_type_attach(M_TMPFSMNT);
404 #endif
405 }
406
407 /* --------------------------------------------------------------------- */
408
409 static void
410 tmpfs_done(void)
411 {
412
413 #ifdef _LKM
414 malloc_type_detach(M_TMPFSMNT);
415 #endif
416 }
417
418 /* --------------------------------------------------------------------- */
419
420 static int
421 tmpfs_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ctime)
422 {
423
424 return EOPNOTSUPP;
425 }
426
427 /* --------------------------------------------------------------------- */
428
429 /*
430 * tmpfs vfs operations.
431 */
432
433 extern const struct vnodeopv_desc tmpfs_fifoop_opv_desc;
434 extern const struct vnodeopv_desc tmpfs_specop_opv_desc;
435 extern const struct vnodeopv_desc tmpfs_vnodeop_opv_desc;
436
437 const struct vnodeopv_desc * const tmpfs_vnodeopv_descs[] = {
438 &tmpfs_fifoop_opv_desc,
439 &tmpfs_specop_opv_desc,
440 &tmpfs_vnodeop_opv_desc,
441 NULL,
442 };
443
444 struct vfsops tmpfs_vfsops = {
445 MOUNT_TMPFS, /* vfs_name */
446 tmpfs_mount, /* vfs_mount */
447 tmpfs_start, /* vfs_start */
448 tmpfs_unmount, /* vfs_unmount */
449 tmpfs_root, /* vfs_root */
450 tmpfs_quotactl, /* vfs_quotactl */
451 tmpfs_statvfs, /* vfs_statvfs */
452 tmpfs_sync, /* vfs_sync */
453 tmpfs_vget, /* vfs_vget */
454 tmpfs_fhtovp, /* vfs_fhtovp */
455 tmpfs_vptofh, /* vfs_vptofh */
456 tmpfs_init, /* vfs_init */
457 NULL, /* vfs_reinit */
458 tmpfs_done, /* vfs_done */
459 NULL, /* vfs_mountroot */
460 tmpfs_snapshot, /* vfs_snapshot */
461 vfs_stdextattrctl, /* vfs_extattrctl */
462 tmpfs_vnodeopv_descs,
463 };
464 VFS_ATTACH(tmpfs_vfsops);
465