]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/tmpfs/tmpfs_vfsops.c
Make TMPFS_PAGES_MINRESERVED a kernel option
[FreeBSD/FreeBSD.git] / sys / fs / tmpfs / tmpfs_vfsops.c
1 /*      $NetBSD: tmpfs_vfsops.c,v 1.10 2005/12/11 12:24:29 christos Exp $       */
2
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 2005 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
11  * 2005 program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 /*
36  * Efficient memory file system.
37  *
38  * tmpfs is a file system that uses FreeBSD's virtual memory
39  * sub-system to store file data and metadata in an efficient way.
40  * This means that it does not follow the structure of an on-disk file
41  * system because it simply does not need to.  Instead, it uses
42  * memory-specific data structures and algorithms to automatically
43  * allocate and release resources.
44  */
45
46 #include "opt_tmpfs.h"
47
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/dirent.h>
54 #include <sys/limits.h>
55 #include <sys/lock.h>
56 #include <sys/mount.h>
57 #include <sys/mutex.h>
58 #include <sys/proc.h>
59 #include <sys/jail.h>
60 #include <sys/kernel.h>
61 #include <sys/rwlock.h>
62 #include <sys/stat.h>
63 #include <sys/sysctl.h>
64 #include <sys/vnode.h>
65
66 #include <vm/vm.h>
67 #include <vm/vm_object.h>
68 #include <vm/vm_param.h>
69
70 #include <fs/tmpfs/tmpfs.h>
71
72 /*
73  * Default permission for root node
74  */
75 #define TMPFS_DEFAULT_ROOT_MODE (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
76
77 MALLOC_DEFINE(M_TMPFSMNT, "tmpfs mount", "tmpfs mount structures");
78 MALLOC_DEFINE(M_TMPFSNAME, "tmpfs name", "tmpfs file names");
79
80 static int      tmpfs_mount(struct mount *);
81 static int      tmpfs_unmount(struct mount *, int);
82 static int      tmpfs_root(struct mount *, int flags, struct vnode **);
83 static int      tmpfs_fhtovp(struct mount *, struct fid *, int,
84                     struct vnode **);
85 static int      tmpfs_statfs(struct mount *, struct statfs *);
86 static void     tmpfs_susp_clean(struct mount *);
87
88 static const char *tmpfs_opts[] = {
89         "from", "size", "maxfilesize", "inodes", "uid", "gid", "mode", "export",
90         "union", "nonc", NULL
91 };
92
93 static const char *tmpfs_updateopts[] = {
94         "from", "export", "size", NULL
95 };
96
97 static int
98 tmpfs_node_ctor(void *mem, int size, void *arg, int flags)
99 {
100         struct tmpfs_node *node = (struct tmpfs_node *)mem;
101
102         node->tn_gen++;
103         node->tn_size = 0;
104         node->tn_status = 0;
105         node->tn_flags = 0;
106         node->tn_links = 0;
107         node->tn_vnode = NULL;
108         node->tn_vpstate = 0;
109
110         return (0);
111 }
112
113 static void
114 tmpfs_node_dtor(void *mem, int size, void *arg)
115 {
116         struct tmpfs_node *node = (struct tmpfs_node *)mem;
117         node->tn_type = VNON;
118 }
119
120 static int
121 tmpfs_node_init(void *mem, int size, int flags)
122 {
123         struct tmpfs_node *node = (struct tmpfs_node *)mem;
124         node->tn_id = 0;
125
126         mtx_init(&node->tn_interlock, "tmpfs node interlock", NULL, MTX_DEF);
127         node->tn_gen = arc4random();
128
129         return (0);
130 }
131
132 static void
133 tmpfs_node_fini(void *mem, int size)
134 {
135         struct tmpfs_node *node = (struct tmpfs_node *)mem;
136
137         mtx_destroy(&node->tn_interlock);
138 }
139
140 #define TMPFS_SC(mp) ((struct tmpfs_mount *)(mp)->mnt_data)
141
142 static int
143 tmpfs_mount(struct mount *mp)
144 {
145         const size_t nodes_per_page = howmany(PAGE_SIZE,
146             sizeof(struct tmpfs_dirent) + sizeof(struct tmpfs_node));
147         struct tmpfs_mount *tmp;
148         struct tmpfs_node *root;
149         int error, flags;
150         bool nonc;
151         /* Size counters. */
152         u_quad_t pages;
153         off_t nodes_max, size_max, maxfilesize;
154
155         /* Root node attributes. */
156         uid_t root_uid;
157         gid_t root_gid;
158         mode_t root_mode;
159
160         struct vattr va;
161
162         if (vfs_filteropt(mp->mnt_optnew, tmpfs_opts))
163                 return (EINVAL);
164
165         if (mp->mnt_flag & MNT_UPDATE) {
166                 /* Only support update mounts for certain options. */
167                 if (vfs_filteropt(mp->mnt_optnew, tmpfs_updateopts) != 0)
168                         return (EOPNOTSUPP);
169                 if (vfs_getopt_size(mp->mnt_optnew, "size", &size_max) == 0) {
170                         /*
171                          * On-the-fly resizing is not supported (yet). We still
172                          * need to have "size" listed as "supported", otherwise
173                          * trying to update fs that is listed in fstab with size
174                          * parameter, say trying to change rw to ro or vice
175                          * versa, would cause vfs_filteropt() to bail.
176                          */
177                         if (size_max != TMPFS_SC(mp)->tm_size_max)
178                                 return (EOPNOTSUPP);
179                 }
180                 if (vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) &&
181                     !(TMPFS_SC(mp)->tm_ronly)) {
182                         /* RW -> RO */
183                         error = VFS_SYNC(mp, MNT_WAIT);
184                         if (error)
185                                 return (error);
186                         flags = WRITECLOSE;
187                         if (mp->mnt_flag & MNT_FORCE)
188                                 flags |= FORCECLOSE;
189                         error = vflush(mp, 0, flags, curthread);
190                         if (error)
191                                 return (error);
192                         TMPFS_SC(mp)->tm_ronly = 1;
193                         MNT_ILOCK(mp);
194                         mp->mnt_flag |= MNT_RDONLY;
195                         MNT_IUNLOCK(mp);
196                 } else if (!vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0) &&
197                     TMPFS_SC(mp)->tm_ronly) {
198                         /* RO -> RW */
199                         TMPFS_SC(mp)->tm_ronly = 0;
200                         MNT_ILOCK(mp);
201                         mp->mnt_flag &= ~MNT_RDONLY;
202                         MNT_IUNLOCK(mp);
203                 }
204                 return (0);
205         }
206
207         vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY);
208         error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred);
209         VOP_UNLOCK(mp->mnt_vnodecovered, 0);
210         if (error)
211                 return (error);
212
213         if (mp->mnt_cred->cr_ruid != 0 ||
214             vfs_scanopt(mp->mnt_optnew, "gid", "%d", &root_gid) != 1)
215                 root_gid = va.va_gid;
216         if (mp->mnt_cred->cr_ruid != 0 ||
217             vfs_scanopt(mp->mnt_optnew, "uid", "%d", &root_uid) != 1)
218                 root_uid = va.va_uid;
219         if (mp->mnt_cred->cr_ruid != 0 ||
220             vfs_scanopt(mp->mnt_optnew, "mode", "%ho", &root_mode) != 1)
221                 root_mode = va.va_mode;
222         if (vfs_getopt_size(mp->mnt_optnew, "inodes", &nodes_max) != 0)
223                 nodes_max = 0;
224         if (vfs_getopt_size(mp->mnt_optnew, "size", &size_max) != 0)
225                 size_max = 0;
226         if (vfs_getopt_size(mp->mnt_optnew, "maxfilesize", &maxfilesize) != 0)
227                 maxfilesize = 0;
228         nonc = vfs_getopt(mp->mnt_optnew, "nonc", NULL, NULL) == 0;
229
230         /* Do not allow mounts if we do not have enough memory to preserve
231          * the minimum reserved pages. */
232         if (tmpfs_mem_avail() < TMPFS_PAGES_MINRESERVED)
233                 return (ENOSPC);
234
235         /* Get the maximum number of memory pages this file system is
236          * allowed to use, based on the maximum size the user passed in
237          * the mount structure.  A value of zero is treated as if the
238          * maximum available space was requested. */
239         if (size_max == 0 || size_max > OFF_MAX - PAGE_SIZE ||
240             (SIZE_MAX < OFF_MAX && size_max / PAGE_SIZE >= SIZE_MAX))
241                 pages = SIZE_MAX;
242         else {
243                 size_max = roundup(size_max, PAGE_SIZE);
244                 pages = howmany(size_max, PAGE_SIZE);
245         }
246         MPASS(pages > 0);
247
248         if (nodes_max <= 3) {
249                 if (pages < INT_MAX / nodes_per_page)
250                         nodes_max = pages * nodes_per_page;
251                 else
252                         nodes_max = INT_MAX;
253         }
254         if (nodes_max > INT_MAX)
255                 nodes_max = INT_MAX;
256         MPASS(nodes_max >= 3);
257
258         /* Allocate the tmpfs mount structure and fill it. */
259         tmp = (struct tmpfs_mount *)malloc(sizeof(struct tmpfs_mount),
260             M_TMPFSMNT, M_WAITOK | M_ZERO);
261
262         mtx_init(&tmp->tm_allnode_lock, "tmpfs allnode lock", NULL, MTX_DEF);
263         tmp->tm_nodes_max = nodes_max;
264         tmp->tm_nodes_inuse = 0;
265         tmp->tm_refcount = 1;
266         tmp->tm_maxfilesize = maxfilesize > 0 ? maxfilesize : OFF_MAX;
267         LIST_INIT(&tmp->tm_nodes_used);
268
269         tmp->tm_size_max = size_max;
270         tmp->tm_pages_max = pages;
271         tmp->tm_pages_used = 0;
272         new_unrhdr64(&tmp->tm_ino_unr, 2);
273         tmp->tm_dirent_pool = uma_zcreate("TMPFS dirent",
274             sizeof(struct tmpfs_dirent), NULL, NULL, NULL, NULL,
275             UMA_ALIGN_PTR, 0);
276         tmp->tm_node_pool = uma_zcreate("TMPFS node",
277             sizeof(struct tmpfs_node), tmpfs_node_ctor, tmpfs_node_dtor,
278             tmpfs_node_init, tmpfs_node_fini, UMA_ALIGN_PTR, 0);
279         tmp->tm_ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
280         tmp->tm_nonc = nonc;
281
282         /* Allocate the root node. */
283         error = tmpfs_alloc_node(mp, tmp, VDIR, root_uid, root_gid,
284             root_mode & ALLPERMS, NULL, NULL, VNOVAL, &root);
285
286         if (error != 0 || root == NULL) {
287                 uma_zdestroy(tmp->tm_node_pool);
288                 uma_zdestroy(tmp->tm_dirent_pool);
289                 free(tmp, M_TMPFSMNT);
290                 return (error);
291         }
292         KASSERT(root->tn_id == 2,
293             ("tmpfs root with invalid ino: %ju", (uintmax_t)root->tn_id));
294         tmp->tm_root = root;
295
296         MNT_ILOCK(mp);
297         mp->mnt_flag |= MNT_LOCAL;
298         mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
299         MNT_IUNLOCK(mp);
300
301         mp->mnt_data = tmp;
302         mp->mnt_stat.f_namemax = MAXNAMLEN;
303         vfs_getnewfsid(mp);
304         vfs_mountedfrom(mp, "tmpfs");
305
306         return 0;
307 }
308
309 /* ARGSUSED2 */
310 static int
311 tmpfs_unmount(struct mount *mp, int mntflags)
312 {
313         struct tmpfs_mount *tmp;
314         struct tmpfs_node *node;
315         int error, flags;
316
317         flags = (mntflags & MNT_FORCE) != 0 ? FORCECLOSE : 0;
318         tmp = VFS_TO_TMPFS(mp);
319
320         /* Stop writers */
321         error = vfs_write_suspend_umnt(mp);
322         if (error != 0)
323                 return (error);
324         /*
325          * At this point, nodes cannot be destroyed by any other
326          * thread because write suspension is started.
327          */
328
329         for (;;) {
330                 error = vflush(mp, 0, flags, curthread);
331                 if (error != 0) {
332                         vfs_write_resume(mp, VR_START_WRITE);
333                         return (error);
334                 }
335                 MNT_ILOCK(mp);
336                 if (mp->mnt_nvnodelistsize == 0) {
337                         MNT_IUNLOCK(mp);
338                         break;
339                 }
340                 MNT_IUNLOCK(mp);
341                 if ((mntflags & MNT_FORCE) == 0) {
342                         vfs_write_resume(mp, VR_START_WRITE);
343                         return (EBUSY);
344                 }
345         }
346
347         TMPFS_LOCK(tmp);
348         while ((node = LIST_FIRST(&tmp->tm_nodes_used)) != NULL) {
349                 TMPFS_NODE_LOCK(node);
350                 if (node->tn_type == VDIR)
351                         tmpfs_dir_destroy(tmp, node);
352                 if (tmpfs_free_node_locked(tmp, node, true))
353                         TMPFS_LOCK(tmp);
354                 else
355                         TMPFS_NODE_UNLOCK(node);
356         }
357
358         mp->mnt_data = NULL;
359         tmpfs_free_tmp(tmp);
360         vfs_write_resume(mp, VR_START_WRITE);
361
362         MNT_ILOCK(mp);
363         mp->mnt_flag &= ~MNT_LOCAL;
364         MNT_IUNLOCK(mp);
365
366         return (0);
367 }
368
369 void
370 tmpfs_free_tmp(struct tmpfs_mount *tmp)
371 {
372
373         MPASS(tmp->tm_refcount > 0);
374         tmp->tm_refcount--;
375         if (tmp->tm_refcount > 0) {
376                 TMPFS_UNLOCK(tmp);
377                 return;
378         }
379         TMPFS_UNLOCK(tmp);
380
381         uma_zdestroy(tmp->tm_dirent_pool);
382         uma_zdestroy(tmp->tm_node_pool);
383
384         mtx_destroy(&tmp->tm_allnode_lock);
385         MPASS(tmp->tm_pages_used == 0);
386         MPASS(tmp->tm_nodes_inuse == 0);
387
388         free(tmp, M_TMPFSMNT);
389 }
390
391 static int
392 tmpfs_root(struct mount *mp, int flags, struct vnode **vpp)
393 {
394         int error;
395
396         error = tmpfs_alloc_vp(mp, VFS_TO_TMPFS(mp)->tm_root, flags, vpp);
397         if (error == 0)
398                 (*vpp)->v_vflag |= VV_ROOT;
399         return (error);
400 }
401
402 static int
403 tmpfs_fhtovp(struct mount *mp, struct fid *fhp, int flags,
404     struct vnode **vpp)
405 {
406         struct tmpfs_fid *tfhp;
407         struct tmpfs_mount *tmp;
408         struct tmpfs_node *node;
409         int error;
410
411         tmp = VFS_TO_TMPFS(mp);
412
413         tfhp = (struct tmpfs_fid *)fhp;
414         if (tfhp->tf_len != sizeof(struct tmpfs_fid))
415                 return (EINVAL);
416
417         if (tfhp->tf_id >= tmp->tm_nodes_max)
418                 return (EINVAL);
419
420         TMPFS_LOCK(tmp);
421         LIST_FOREACH(node, &tmp->tm_nodes_used, tn_entries) {
422                 if (node->tn_id == tfhp->tf_id &&
423                     node->tn_gen == tfhp->tf_gen) {
424                         tmpfs_ref_node(node);
425                         break;
426                 }
427         }
428         TMPFS_UNLOCK(tmp);
429
430         if (node != NULL) {
431                 error = tmpfs_alloc_vp(mp, node, LK_EXCLUSIVE, vpp);
432                 tmpfs_free_node(tmp, node);
433         } else
434                 error = EINVAL;
435         return (error);
436 }
437
438 /* ARGSUSED2 */
439 static int
440 tmpfs_statfs(struct mount *mp, struct statfs *sbp)
441 {
442         struct tmpfs_mount *tmp;
443         size_t used;
444
445         tmp = VFS_TO_TMPFS(mp);
446
447         sbp->f_iosize = PAGE_SIZE;
448         sbp->f_bsize = PAGE_SIZE;
449
450         used = tmpfs_pages_used(tmp);
451         if (tmp->tm_pages_max != ULONG_MAX)
452                  sbp->f_blocks = tmp->tm_pages_max;
453         else
454                  sbp->f_blocks = used + tmpfs_mem_avail();
455         if (sbp->f_blocks <= used)
456                 sbp->f_bavail = 0;
457         else
458                 sbp->f_bavail = sbp->f_blocks - used;
459         sbp->f_bfree = sbp->f_bavail;
460         used = tmp->tm_nodes_inuse;
461         sbp->f_files = tmp->tm_nodes_max;
462         if (sbp->f_files <= used)
463                 sbp->f_ffree = 0;
464         else
465                 sbp->f_ffree = sbp->f_files - used;
466         /* sbp->f_owner = tmp->tn_uid; */
467
468         return 0;
469 }
470
471 static int
472 tmpfs_sync(struct mount *mp, int waitfor)
473 {
474         struct vnode *vp, *mvp;
475         struct vm_object *obj;
476
477         if (waitfor == MNT_SUSPEND) {
478                 MNT_ILOCK(mp);
479                 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
480                 MNT_IUNLOCK(mp);
481         } else if (waitfor == MNT_LAZY) {
482                 /*
483                  * Handle lazy updates of mtime from writes to mmaped
484                  * regions.  Use MNT_VNODE_FOREACH_ALL instead of
485                  * MNT_VNODE_FOREACH_ACTIVE, since unmap of the
486                  * tmpfs-backed vnode does not call vinactive(), due
487                  * to vm object type is OBJT_SWAP.
488                  */
489                 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
490                         if (vp->v_type != VREG) {
491                                 VI_UNLOCK(vp);
492                                 continue;
493                         }
494                         obj = vp->v_object;
495                         KASSERT((obj->flags & (OBJ_TMPFS_NODE | OBJ_TMPFS)) ==
496                             (OBJ_TMPFS_NODE | OBJ_TMPFS), ("non-tmpfs obj"));
497
498                         /*
499                          * Unlocked read, avoid taking vnode lock if
500                          * not needed.  Lost update will be handled on
501                          * the next call.
502                          */
503                         if ((obj->flags & OBJ_TMPFS_DIRTY) == 0) {
504                                 VI_UNLOCK(vp);
505                                 continue;
506                         }
507                         if (vget(vp, LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
508                             curthread) != 0)
509                                 continue;
510                         tmpfs_check_mtime(vp);
511                         vput(vp);
512                 }
513         }
514         return (0);
515 }
516
517 /*
518  * The presence of a susp_clean method tells the VFS to track writes.
519  */
520 static void
521 tmpfs_susp_clean(struct mount *mp __unused)
522 {
523 }
524
525 /*
526  * tmpfs vfs operations.
527  */
528
529 struct vfsops tmpfs_vfsops = {
530         .vfs_mount =                    tmpfs_mount,
531         .vfs_unmount =                  tmpfs_unmount,
532         .vfs_root =                     tmpfs_root,
533         .vfs_statfs =                   tmpfs_statfs,
534         .vfs_fhtovp =                   tmpfs_fhtovp,
535         .vfs_sync =                     tmpfs_sync,
536         .vfs_susp_clean =               tmpfs_susp_clean,
537 };
538 VFS_SET(tmpfs_vfsops, tmpfs, VFCF_JAIL);