fs: rename AT_* to VATTR_*
[unleashed.git] / kernel / fs / tmpfs / tmp_vfsops.c
blobe304522904be870c7c1f2292156162c1b41657ba
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright 2015 Joyent, Inc.
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/sysmacros.h>
29 #include <sys/kmem.h>
30 #include <sys/time.h>
31 #include <sys/pathname.h>
32 #include <sys/vfs.h>
33 #include <sys/vnode.h>
34 #include <sys/stat.h>
35 #include <sys/uio.h>
36 #include <sys/stat.h>
37 #include <sys/errno.h>
38 #include <sys/cmn_err.h>
39 #include <sys/cred.h>
40 #include <sys/statvfs.h>
41 #include <sys/mount.h>
42 #include <sys/debug.h>
43 #include <sys/systm.h>
44 #include <sys/mntent.h>
45 #include <sys/fs_subr.h>
46 #include <vm/page.h>
47 #include <vm/anon.h>
48 #include <sys/model.h>
49 #include <sys/policy.h>
51 #include <sys/fs/swapnode.h>
52 #include <sys/fs/tmp.h>
53 #include <sys/fs/tmpnode.h>
55 static int tmpfsfstype;
58 * tmpfs vfs operations.
60 static int tmpfsinit(int, char *);
61 static int tmp_mount(struct vfs *, struct vnode *,
62 struct mounta *, struct cred *);
63 static int tmp_unmount(struct vfs *, int, struct cred *);
64 static int tmp_root(struct vfs *, struct vnode **);
65 static int tmp_statvfs(struct vfs *, struct statvfs64 *);
66 static int tmp_vget(struct vfs *, struct vnode **, struct fid *);
69 * Loadable module wrapper
71 #include <sys/modctl.h>
73 static mntopts_t tmpfs_proto_opttbl;
75 static vfsdef_t vfw = {
76 VFSDEF_VERSION,
77 "tmpfs",
78 tmpfsinit,
79 VSW_HASPROTO|VSW_CANREMOUNT|VSW_STATS|VSW_ZMOUNT,
80 &tmpfs_proto_opttbl
84 * in-kernel mnttab options
86 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
87 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
89 static mntopt_t tmpfs_options[] = {
90 /* Option name Cancel Opt Arg Flags Data */
91 { MNTOPT_XATTR, xattr_cancel, NULL, MO_DEFAULT, NULL},
92 { MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL},
93 { "size", NULL, "0", MO_HASVALUE, NULL},
94 { "mode", NULL, NULL, MO_HASVALUE, NULL},
98 static mntopts_t tmpfs_proto_opttbl = {
99 sizeof (tmpfs_options) / sizeof (mntopt_t),
100 tmpfs_options
104 * Module linkage information
106 static struct modlfs modlfs = {
107 &mod_fsops, "filesystem for tmpfs", &vfw
110 static struct modlinkage modlinkage = {
111 MODREV_1, &modlfs, NULL
115 _init()
117 return (mod_install(&modlinkage));
121 _fini()
123 int error;
125 error = mod_remove(&modlinkage);
126 if (error)
127 return (error);
129 * Tear down the operations vectors
131 (void) vfs_freevfsops_by_type(tmpfsfstype);
132 return (0);
136 _info(struct modinfo *modinfop)
138 return (mod_info(&modlinkage, modinfop));
142 * The following are patchable variables limiting the amount of system
143 * resources tmpfs can use.
145 * tmpfs_maxkmem limits the amount of kernel kmem_alloc memory
146 * tmpfs can use for it's data structures (e.g. tmpnodes, directory entries)
147 * It is not determined by setting a hard limit but rather as a percentage of
148 * physical memory which is determined when tmpfs is first used in the system.
150 * tmpfs_minfree is the minimum amount of swap space that tmpfs leaves for
151 * the rest of the system. In other words, if the amount of free swap space
152 * in the system (i.e. anoninfo.ani_free) drops below tmpfs_minfree, tmpfs
153 * anon allocations will fail.
155 * There is also a per mount limit on the amount of swap space
156 * (tmount.tm_anonmax) settable via a mount option.
158 size_t tmpfs_maxkmem = 0;
159 size_t tmpfs_minfree = 0;
160 size_t tmp_kmemspace; /* bytes of kernel heap used by all tmpfs */
162 static major_t tmpfs_major;
163 static minor_t tmpfs_minor;
164 static kmutex_t tmpfs_minor_lock;
166 static const struct vfsops tmp_vfsops = {
167 .vfs_mount = tmp_mount,
168 .vfs_unmount = tmp_unmount,
169 .vfs_root = tmp_root,
170 .vfs_statvfs = tmp_statvfs,
171 .vfs_vget = tmp_vget,
175 * initialize global tmpfs locks and such
176 * called when loading tmpfs module
178 static int
179 tmpfsinit(int fstype, char *name)
181 int error;
182 extern void tmpfs_hash_init();
184 tmpfs_hash_init();
185 tmpfsfstype = fstype;
186 ASSERT(tmpfsfstype != 0);
188 error = vfs_setfsops(fstype, &tmp_vfsops);
189 if (error != 0) {
190 cmn_err(CE_WARN, "tmpfsinit: bad fstype");
191 return (error);
195 * tmpfs_minfree doesn't need to be some function of configured
196 * swap space since it really is an absolute limit of swap space
197 * which still allows other processes to execute.
199 if (tmpfs_minfree == 0) {
201 * Set if not patched
203 tmpfs_minfree = btopr(TMPMINFREE);
207 * The maximum amount of space tmpfs can allocate is
208 * TMPMAXPROCKMEM percent of kernel memory
210 if (tmpfs_maxkmem == 0)
211 tmpfs_maxkmem = MAX(PAGESIZE, kmem_maxavail() / TMPMAXFRACKMEM);
213 if ((tmpfs_major = getudev()) == (major_t)-1) {
214 cmn_err(CE_WARN, "tmpfsinit: Can't get unique device number.");
215 tmpfs_major = 0;
217 mutex_init(&tmpfs_minor_lock, NULL, MUTEX_DEFAULT, NULL);
218 return (0);
221 static int
222 tmp_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
224 struct tmount *tm = NULL;
225 struct tmpnode *tp;
226 struct pathname dpn;
227 int error;
228 pgcnt_t anonmax;
229 struct vattr rattr;
230 int got_attrs;
231 boolean_t mode_arg = B_FALSE;
232 mode_t root_mode = 0777;
233 char *argstr;
235 if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
236 return (error);
238 if (mvp->v_type != VDIR)
239 return (ENOTDIR);
241 mutex_enter(&mvp->v_lock);
242 if ((uap->flags & MS_REMOUNT) == 0 && (uap->flags & MS_OVERLAY) == 0 &&
243 (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
244 mutex_exit(&mvp->v_lock);
245 return (EBUSY);
247 mutex_exit(&mvp->v_lock);
250 * Having the resource be anything but "swap" doesn't make sense.
252 vfs_setresource(vfsp, "swap", 0);
255 * now look for options we understand...
258 /* tmpfs doesn't support read-only mounts */
259 if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
260 error = EINVAL;
261 goto out;
265 * tm_anonmax is set according to the mount arguments
266 * if any. Otherwise, it is set to a maximum value.
268 if (vfs_optionisset(vfsp, "size", &argstr)) {
269 if ((error = tmp_convnum(argstr, &anonmax)) != 0)
270 goto out;
271 } else {
272 anonmax = ULONG_MAX;
276 * The "mode" mount argument allows the operator to override the
277 * permissions of the root of the tmpfs mount.
279 if (vfs_optionisset(vfsp, "mode", &argstr)) {
280 if ((error = tmp_convmode(argstr, &root_mode)) != 0) {
281 goto out;
283 mode_arg = B_TRUE;
286 if (error = pn_get(uap->dir,
287 (uap->flags & MS_SYSSPACE) ? UIO_SYSSPACE : UIO_USERSPACE, &dpn))
288 goto out;
290 if (uap->flags & MS_REMOUNT) {
291 tm = (struct tmount *)VFSTOTM(vfsp);
294 * If we change the size so its less than what is currently
295 * being used, we allow that. The file system will simply be
296 * full until enough files have been removed to get below the
297 * new max.
299 mutex_enter(&tm->tm_contents);
300 tm->tm_anonmax = anonmax;
301 mutex_exit(&tm->tm_contents);
302 goto out;
305 if ((tm = tmp_memalloc(sizeof (struct tmount), 0)) == NULL) {
306 pn_free(&dpn);
307 error = ENOMEM;
308 goto out;
312 * find an available minor device number for this mount
314 mutex_enter(&tmpfs_minor_lock);
315 do {
316 tmpfs_minor = (tmpfs_minor + 1) & L_MAXMIN32;
317 tm->tm_dev = makedevice(tmpfs_major, tmpfs_minor);
318 } while (vfs_devismounted(tm->tm_dev));
319 mutex_exit(&tmpfs_minor_lock);
322 * Set but don't bother entering the mutex
323 * (tmount not on mount list yet)
325 mutex_init(&tm->tm_contents, NULL, MUTEX_DEFAULT, NULL);
326 mutex_init(&tm->tm_renamelck, NULL, MUTEX_DEFAULT, NULL);
328 tm->tm_vfsp = vfsp;
329 tm->tm_anonmax = anonmax;
331 vfsp->vfs_data = (caddr_t)tm;
332 vfsp->vfs_fstype = tmpfsfstype;
333 vfsp->vfs_dev = tm->tm_dev;
334 vfsp->vfs_bsize = PAGESIZE;
335 vfsp->vfs_flag |= VFS_NOTRUNC;
336 vfs_make_fsid(&vfsp->vfs_fsid, tm->tm_dev, tmpfsfstype);
337 tm->tm_mntpath = tmp_memalloc(dpn.pn_pathlen + 1, TMP_MUSTHAVE);
338 (void) strcpy(tm->tm_mntpath, dpn.pn_path);
341 * allocate and initialize root tmpnode structure
343 bzero(&rattr, sizeof (struct vattr));
344 rattr.va_mode = (mode_t)(S_IFDIR | root_mode);
345 rattr.va_type = VDIR;
346 rattr.va_rdev = 0;
347 tp = tmp_memalloc(sizeof (struct tmpnode), TMP_MUSTHAVE);
348 tmpnode_init(tm, tp, &rattr, cr);
351 * Get the mode, uid, and gid from the underlying mount point.
353 rattr.va_mask = VATTR_MODE|VATTR_UID|VATTR_GID; /* Hint to getattr */
354 got_attrs = fop_getattr(mvp, &rattr, 0, cr, NULL);
356 rw_enter(&tp->tn_rwlock, RW_WRITER);
357 TNTOV(tp)->v_flag |= VROOT;
360 * If the getattr succeeded, use its results. Otherwise allow
361 * the previously set hardwired defaults to prevail.
363 if (got_attrs == 0) {
364 if (!mode_arg) {
366 * Only use the underlying mount point for the
367 * mode if the "mode" mount argument was not
368 * provided.
370 tp->tn_mode = rattr.va_mode;
372 tp->tn_uid = rattr.va_uid;
373 tp->tn_gid = rattr.va_gid;
377 * initialize linked list of tmpnodes so that the back pointer of
378 * the root tmpnode always points to the last one on the list
379 * and the forward pointer of the last node is null
381 tp->tn_back = tp;
382 tp->tn_forw = NULL;
383 tp->tn_nlink = 0;
384 tm->tm_rootnode = tp;
386 tdirinit(tp, tp);
388 rw_exit(&tp->tn_rwlock);
390 pn_free(&dpn);
391 error = 0;
393 out:
394 if (error == 0)
395 vfs_set_feature(vfsp, VFSFT_SYSATTR_VIEWS);
397 return (error);
400 static int
401 tmp_unmount(struct vfs *vfsp, int flag, struct cred *cr)
403 struct tmount *tm = (struct tmount *)VFSTOTM(vfsp);
404 struct tmpnode *tnp, *cancel;
405 struct vnode *vp;
406 int error;
408 if ((error = secpolicy_fs_unmount(cr, vfsp)) != 0)
409 return (error);
412 * forced unmount is not supported by this file system
413 * and thus, ENOTSUP, is being returned.
415 if (flag & MS_FORCE)
416 return (ENOTSUP);
418 mutex_enter(&tm->tm_contents);
421 * If there are no open files, only the root node should have
422 * a reference count.
423 * With tm_contents held, nothing can be added or removed.
424 * There may be some dirty pages. To prevent fsflush from
425 * disrupting the unmount, put a hold on each node while scanning.
426 * If we find a previously referenced node, undo the holds we have
427 * placed and fail EBUSY.
429 tnp = tm->tm_rootnode;
430 if (TNTOV(tnp)->v_count > 1) {
431 mutex_exit(&tm->tm_contents);
432 return (EBUSY);
435 for (tnp = tnp->tn_forw; tnp; tnp = tnp->tn_forw) {
436 if ((vp = TNTOV(tnp))->v_count > 0) {
437 cancel = tm->tm_rootnode->tn_forw;
438 while (cancel != tnp) {
439 vp = TNTOV(cancel);
440 ASSERT(vp->v_count > 0);
441 VN_RELE(vp);
442 cancel = cancel->tn_forw;
444 mutex_exit(&tm->tm_contents);
445 return (EBUSY);
447 VN_HOLD(vp);
451 * We can drop the mutex now because no one can find this mount
453 mutex_exit(&tm->tm_contents);
456 * Free all kmemalloc'd and anonalloc'd memory associated with
457 * this filesystem. To do this, we go through the file list twice,
458 * once to remove all the directory entries, and then to remove
459 * all the files. We do this because there is useful code in
460 * tmpnode_free which assumes that the directory entry has been
461 * removed before the file.
464 * Remove all directory entries
466 for (tnp = tm->tm_rootnode; tnp; tnp = tnp->tn_forw) {
467 rw_enter(&tnp->tn_rwlock, RW_WRITER);
468 if (tnp->tn_type == VDIR)
469 tdirtrunc(tnp);
470 if (tnp->tn_vnode->v_flag & V_XATTRDIR) {
472 * Account for implicit attrdir reference.
474 ASSERT(tnp->tn_nlink > 0);
475 DECR_COUNT(&tnp->tn_nlink, &tnp->tn_tlock);
477 rw_exit(&tnp->tn_rwlock);
480 ASSERT(tm->tm_rootnode);
483 * All links are gone, v_count is keeping nodes in place.
484 * VN_RELE should make the node disappear, unless somebody
485 * is holding pages against it. Nap and retry until it disappears.
487 * We re-acquire the lock to prevent others who have a HOLD on
488 * a tmpnode via its pages or anon slots from blowing it away
489 * (in tmp_inactive) while we're trying to get to it here. Once
490 * we have a HOLD on it we know it'll stick around.
493 mutex_enter(&tm->tm_contents);
495 * Remove all the files (except the rootnode) backwards.
497 while ((tnp = tm->tm_rootnode->tn_back) != tm->tm_rootnode) {
498 mutex_exit(&tm->tm_contents);
500 * Inhibit tmp_inactive from touching attribute directory
501 * as all nodes will be released here.
502 * Note we handled the link count in pass 2 above.
504 rw_enter(&tnp->tn_rwlock, RW_WRITER);
505 tnp->tn_xattrdp = NULL;
506 rw_exit(&tnp->tn_rwlock);
507 vp = TNTOV(tnp);
508 VN_RELE(vp);
509 mutex_enter(&tm->tm_contents);
511 * It's still there after the RELE. Someone else like pageout
512 * has a hold on it so wait a bit and then try again - we know
513 * they'll give it up soon.
515 if (tnp == tm->tm_rootnode->tn_back) {
516 VN_HOLD(vp);
517 mutex_exit(&tm->tm_contents);
518 ddi_msleep(250);
519 mutex_enter(&tm->tm_contents);
522 mutex_exit(&tm->tm_contents);
524 tm->tm_rootnode->tn_xattrdp = NULL;
525 VN_RELE(TNTOV(tm->tm_rootnode));
527 ASSERT(tm->tm_mntpath);
529 tmp_memfree(tm->tm_mntpath, strlen(tm->tm_mntpath) + 1);
531 ASSERT(tm->tm_anonmem == 0);
533 mutex_destroy(&tm->tm_contents);
534 mutex_destroy(&tm->tm_renamelck);
535 tmp_memfree(tm, sizeof (struct tmount));
537 return (0);
541 * return root tmpnode for given vnode
543 static int
544 tmp_root(struct vfs *vfsp, struct vnode **vpp)
546 struct tmount *tm = (struct tmount *)VFSTOTM(vfsp);
547 struct tmpnode *tp = tm->tm_rootnode;
548 struct vnode *vp;
550 ASSERT(tp);
552 vp = TNTOV(tp);
553 VN_HOLD(vp);
554 *vpp = vp;
555 return (0);
558 static int
559 tmp_statvfs(struct vfs *vfsp, struct statvfs64 *sbp)
561 struct tmount *tm = (struct tmount *)VFSTOTM(vfsp);
562 ulong_t blocks;
563 dev32_t d32;
564 zoneid_t eff_zid;
565 struct zone *zp;
568 * The file system may have been mounted by the global zone on
569 * behalf of the non-global zone. In that case, the tmount zone_id
570 * will be the global zone. We still want to show the swap cap inside
571 * the zone in this case, even though the file system was mounted by
572 * the global zone.
574 if (curproc->p_zone->zone_id != GLOBAL_ZONEUNIQID)
575 zp = curproc->p_zone;
576 else
577 zp = tm->tm_vfsp->vfs_zone;
579 if (zp == NULL)
580 eff_zid = GLOBAL_ZONEUNIQID;
581 else
582 eff_zid = zp->zone_id;
584 sbp->f_bsize = PAGESIZE;
585 sbp->f_frsize = PAGESIZE;
588 * Find the amount of available physical and memory swap
590 mutex_enter(&anoninfo_lock);
591 ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
592 blocks = (ulong_t)CURRENT_TOTAL_AVAILABLE_SWAP;
593 mutex_exit(&anoninfo_lock);
596 * If tm_anonmax for this mount is less than the available swap space
597 * (minus the amount tmpfs can't use), use that instead
599 if (blocks > tmpfs_minfree)
600 sbp->f_bfree = MIN(blocks - tmpfs_minfree,
601 tm->tm_anonmax - tm->tm_anonmem);
602 else
603 sbp->f_bfree = 0;
605 sbp->f_bavail = sbp->f_bfree;
608 * Total number of blocks is what's available plus what's been used
610 sbp->f_blocks = (fsblkcnt64_t)(sbp->f_bfree + tm->tm_anonmem);
612 if (eff_zid != GLOBAL_ZONEUNIQID &&
613 zp->zone_max_swap_ctl != UINT64_MAX) {
615 * If the fs is used by a non-global zone with a swap cap,
616 * then report the capped size.
618 rctl_qty_t cap, used;
619 pgcnt_t pgcap, pgused;
621 mutex_enter(&zp->zone_mem_lock);
622 cap = zp->zone_max_swap_ctl;
623 used = zp->zone_max_swap;
624 mutex_exit(&zp->zone_mem_lock);
626 pgcap = btop(cap);
627 pgused = btop(used);
629 sbp->f_bfree = MIN(pgcap - pgused, sbp->f_bfree);
630 sbp->f_bavail = sbp->f_bfree;
631 sbp->f_blocks = MIN(pgcap, sbp->f_blocks);
635 * The maximum number of files available is approximately the number
636 * of tmpnodes we can allocate from the remaining kernel memory
637 * available to tmpfs. This is fairly inaccurate since it doesn't
638 * take into account the names stored in the directory entries.
640 if (tmpfs_maxkmem > tmp_kmemspace)
641 sbp->f_ffree = (tmpfs_maxkmem - tmp_kmemspace) /
642 (sizeof (struct tmpnode) + sizeof (struct tdirent));
643 else
644 sbp->f_ffree = 0;
646 sbp->f_files = tmpfs_maxkmem /
647 (sizeof (struct tmpnode) + sizeof (struct tdirent));
648 sbp->f_favail = (fsfilcnt64_t)(sbp->f_ffree);
649 (void) cmpldev(&d32, vfsp->vfs_dev);
650 sbp->f_fsid = d32;
651 (void) strcpy(sbp->f_basetype, vfssw[tmpfsfstype].vsw_name);
652 (void) strncpy(sbp->f_fstr, tm->tm_mntpath, sizeof (sbp->f_fstr));
654 * ensure null termination
656 sbp->f_fstr[sizeof (sbp->f_fstr) - 1] = '\0';
657 sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
658 sbp->f_namemax = MAXNAMELEN - 1;
659 return (0);
662 static int
663 tmp_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
665 struct tfid *tfid;
666 struct tmount *tm = (struct tmount *)VFSTOTM(vfsp);
667 struct tmpnode *tp = NULL;
669 tfid = (struct tfid *)fidp;
670 *vpp = NULL;
672 mutex_enter(&tm->tm_contents);
673 for (tp = tm->tm_rootnode; tp; tp = tp->tn_forw) {
674 mutex_enter(&tp->tn_tlock);
675 if (tp->tn_nodeid == tfid->tfid_ino) {
677 * If the gen numbers don't match we know the
678 * file won't be found since only one tmpnode
679 * can have this number at a time.
681 if (tp->tn_gen != tfid->tfid_gen || tp->tn_nlink == 0) {
682 mutex_exit(&tp->tn_tlock);
683 mutex_exit(&tm->tm_contents);
684 return (0);
686 *vpp = (struct vnode *)TNTOV(tp);
688 VN_HOLD(*vpp);
690 if ((tp->tn_mode & S_ISVTX) &&
691 !(tp->tn_mode & (S_IXUSR | S_IFDIR))) {
692 mutex_enter(&(*vpp)->v_lock);
693 (*vpp)->v_flag |= VISSWAP;
694 mutex_exit(&(*vpp)->v_lock);
696 mutex_exit(&tp->tn_tlock);
697 mutex_exit(&tm->tm_contents);
698 return (0);
700 mutex_exit(&tp->tn_tlock);
702 mutex_exit(&tm->tm_contents);
703 return (0);