uipc_shm.c revision 185533
1175164Sjhb/*-
2175164Sjhb * Copyright (c) 2006 Robert N. M. Watson
3175164Sjhb * All rights reserved.
4175164Sjhb *
5175164Sjhb * Redistribution and use in source and binary forms, with or without
6175164Sjhb * modification, are permitted provided that the following conditions
7175164Sjhb * are met:
8175164Sjhb * 1. Redistributions of source code must retain the above copyright
9175164Sjhb *    notice, this list of conditions and the following disclaimer.
10175164Sjhb * 2. Redistributions in binary form must reproduce the above copyright
11175164Sjhb *    notice, this list of conditions and the following disclaimer in the
12175164Sjhb *    documentation and/or other materials provided with the distribution.
13175164Sjhb *
14175164Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15175164Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16175164Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17175164Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18175164Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19175164Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20175164Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21175164Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22175164Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23175164Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24175164Sjhb * SUCH DAMAGE.
25175164Sjhb */
26175164Sjhb
27175164Sjhb/*
28175164Sjhb * Support for shared swap-backed anonymous memory objects via
29175164Sjhb * shm_open(2) and shm_unlink(2).  While most of the implementation is
30175164Sjhb * here, vm_mmap.c contains mapping logic changes.
31175164Sjhb *
32175164Sjhb * TODO:
33175164Sjhb *
34175164Sjhb * (2) Need to export data to a userland tool via a sysctl.  Should ipcs(1)
35175164Sjhb *     and ipcrm(1) be expanded or should new tools to manage both POSIX
36175164Sjhb *     kernel semaphores and POSIX shared memory be written?
37175164Sjhb *
38175164Sjhb * (3) Add support for this file type to fstat(1).
39175164Sjhb *
40175164Sjhb * (4) Resource limits?  Does this need its own resource limits or are the
41175164Sjhb *     existing limits in mmap(2) sufficient?
42175164Sjhb *
43175164Sjhb * (5) Partial page truncation.  vnode_pager_setsize() will zero any parts
44175164Sjhb *     of a partially mapped page as a result of ftruncate(2)/truncate(2).
45175164Sjhb *     We can do the same (with the same pmap evil), but do we need to
46175164Sjhb *     worry about the bits on disk if the page is swapped out or will the
47175164Sjhb *     swapper zero the parts of a page that are invalid if the page is
48175164Sjhb *     swapped back in for us?
49180059Sjhb *
50180059Sjhb * (6) Add MAC support in mac_biba(4) and mac_mls(4).
51180059Sjhb *
52180059Sjhb * (7) Add a MAC check_create() hook for creating new named objects.
53175164Sjhb */
54175164Sjhb
55175164Sjhb#include <sys/cdefs.h>
56175164Sjhb__FBSDID("$FreeBSD: head/sys/kern/uipc_shm.c 185533 2008-12-01 22:33:50Z kan $");
57175164Sjhb
58175164Sjhb#include "opt_mac.h"
59175164Sjhb
60175164Sjhb#include <sys/param.h>
61175164Sjhb#include <sys/fcntl.h>
62175164Sjhb#include <sys/file.h>
63175164Sjhb#include <sys/filedesc.h>
64175164Sjhb#include <sys/fnv_hash.h>
65175164Sjhb#include <sys/kernel.h>
66175164Sjhb#include <sys/lock.h>
67175164Sjhb#include <sys/malloc.h>
68175164Sjhb#include <sys/mman.h>
69175164Sjhb#include <sys/mutex.h>
70175164Sjhb#include <sys/proc.h>
71175164Sjhb#include <sys/refcount.h>
72175164Sjhb#include <sys/resourcevar.h>
73175164Sjhb#include <sys/stat.h>
74175164Sjhb#include <sys/sysctl.h>
75175164Sjhb#include <sys/sysproto.h>
76175164Sjhb#include <sys/systm.h>
77175164Sjhb#include <sys/sx.h>
78175164Sjhb#include <sys/time.h>
79175164Sjhb#include <sys/vnode.h>
80175164Sjhb
81175164Sjhb#include <security/mac/mac_framework.h>
82175164Sjhb
83175164Sjhb#include <vm/vm.h>
84175164Sjhb#include <vm/vm_param.h>
85175164Sjhb#include <vm/pmap.h>
86175164Sjhb#include <vm/vm_map.h>
87175164Sjhb#include <vm/vm_object.h>
88175164Sjhb#include <vm/vm_page.h>
89175164Sjhb#include <vm/vm_pager.h>
90175164Sjhb#include <vm/swap_pager.h>
91175164Sjhb
92175164Sjhbstruct shm_mapping {
93175164Sjhb	char		*sm_path;
94175164Sjhb	Fnv32_t		sm_fnv;
95175164Sjhb	struct shmfd	*sm_shmfd;
96175164Sjhb	LIST_ENTRY(shm_mapping) sm_link;
97175164Sjhb};
98175164Sjhb
99175164Sjhbstatic MALLOC_DEFINE(M_SHMFD, "shmfd", "shared memory file descriptor");
100175164Sjhbstatic LIST_HEAD(, shm_mapping) *shm_dictionary;
101175164Sjhbstatic struct sx shm_dict_lock;
102175164Sjhbstatic struct mtx shm_timestamp_lock;
103175164Sjhbstatic u_long shm_hash;
104175164Sjhb
105175164Sjhb#define	SHM_HASH(fnv)	(&shm_dictionary[(fnv) & shm_hash])
106175164Sjhb
107175164Sjhbstatic int	shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags);
108175164Sjhbstatic struct shmfd *shm_alloc(struct ucred *ucred, mode_t mode);
109175164Sjhbstatic void	shm_dict_init(void *arg);
110175164Sjhbstatic void	shm_drop(struct shmfd *shmfd);
111175164Sjhbstatic struct shmfd *shm_hold(struct shmfd *shmfd);
112175164Sjhbstatic void	shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd);
113175164Sjhbstatic struct shmfd *shm_lookup(char *path, Fnv32_t fnv);
114175164Sjhbstatic int	shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
115175164Sjhbstatic void	shm_dotruncate(struct shmfd *shmfd, off_t length);
116175164Sjhb
117175164Sjhbstatic fo_rdwr_t	shm_read;
118175164Sjhbstatic fo_rdwr_t	shm_write;
119175164Sjhbstatic fo_truncate_t	shm_truncate;
120175164Sjhbstatic fo_ioctl_t	shm_ioctl;
121175164Sjhbstatic fo_poll_t	shm_poll;
122175164Sjhbstatic fo_kqfilter_t	shm_kqfilter;
123175164Sjhbstatic fo_stat_t	shm_stat;
124175164Sjhbstatic fo_close_t	shm_close;
125175164Sjhb
126175164Sjhb/* File descriptor operations. */
127175164Sjhbstatic struct fileops shm_ops = {
128175164Sjhb	.fo_read = shm_read,
129175164Sjhb	.fo_write = shm_write,
130175164Sjhb	.fo_truncate = shm_truncate,
131175164Sjhb	.fo_ioctl = shm_ioctl,
132175164Sjhb	.fo_poll = shm_poll,
133175164Sjhb	.fo_kqfilter = shm_kqfilter,
134175164Sjhb	.fo_stat = shm_stat,
135175164Sjhb	.fo_close = shm_close,
136175164Sjhb	.fo_flags = DFLAG_PASSABLE
137175164Sjhb};
138175164Sjhb
139175164SjhbFEATURE(posix_shm, "POSIX shared memory");
140175164Sjhb
141175164Sjhbstatic int
142175164Sjhbshm_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
143175164Sjhb    int flags, struct thread *td)
144175164Sjhb{
145175164Sjhb
146175164Sjhb	return (EOPNOTSUPP);
147175164Sjhb}
148175164Sjhb
149175164Sjhbstatic int
150175164Sjhbshm_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
151175164Sjhb    int flags, struct thread *td)
152175164Sjhb{
153175164Sjhb
154175164Sjhb	return (EOPNOTSUPP);
155175164Sjhb}
156175164Sjhb
157175164Sjhbstatic int
158175164Sjhbshm_truncate(struct file *fp, off_t length, struct ucred *active_cred,
159175164Sjhb    struct thread *td)
160175164Sjhb{
161175164Sjhb	struct shmfd *shmfd;
162175164Sjhb#ifdef MAC
163175164Sjhb	int error;
164175164Sjhb#endif
165175164Sjhb
166175164Sjhb	shmfd = fp->f_data;
167175164Sjhb#ifdef MAC
168175164Sjhb	error = mac_posixshm_check_truncate(active_cred, fp->f_cred, shmfd);
169175164Sjhb	if (error)
170175164Sjhb		return (error);
171175164Sjhb#endif
172175164Sjhb	shm_dotruncate(shmfd, length);
173175164Sjhb	return (0);
174175164Sjhb}
175175164Sjhb
176175164Sjhbstatic int
177175164Sjhbshm_ioctl(struct file *fp, u_long com, void *data,
178175164Sjhb    struct ucred *active_cred, struct thread *td)
179175164Sjhb{
180175164Sjhb
181175164Sjhb	return (EOPNOTSUPP);
182175164Sjhb}
183175164Sjhb
184175164Sjhbstatic int
185175164Sjhbshm_poll(struct file *fp, int events, struct ucred *active_cred,
186175164Sjhb    struct thread *td)
187175164Sjhb{
188175164Sjhb
189175164Sjhb	return (EOPNOTSUPP);
190175164Sjhb}
191175164Sjhb
192175164Sjhbstatic int
193175164Sjhbshm_kqfilter(struct file *fp, struct knote *kn)
194175164Sjhb{
195175164Sjhb
196175164Sjhb	return (EOPNOTSUPP);
197175164Sjhb}
198175164Sjhb
199175164Sjhbstatic int
200175164Sjhbshm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
201175164Sjhb    struct thread *td)
202175164Sjhb{
203175164Sjhb	struct shmfd *shmfd;
204175164Sjhb#ifdef MAC
205175164Sjhb	int error;
206175164Sjhb#endif
207175164Sjhb
208175164Sjhb	shmfd = fp->f_data;
209175164Sjhb
210175164Sjhb#ifdef MAC
211175164Sjhb	error = mac_posixshm_check_stat(active_cred, fp->f_cred, shmfd);
212175164Sjhb	if (error)
213175164Sjhb		return (error);
214175164Sjhb#endif
215175164Sjhb
216175164Sjhb	/*
217175164Sjhb	 * Attempt to return sanish values for fstat() on a memory file
218175164Sjhb	 * descriptor.
219175164Sjhb	 */
220175164Sjhb	bzero(sb, sizeof(*sb));
221175164Sjhb	sb->st_mode = S_IFREG | shmfd->shm_mode;		/* XXX */
222175164Sjhb	sb->st_blksize = PAGE_SIZE;
223175164Sjhb	sb->st_size = shmfd->shm_size;
224175164Sjhb	sb->st_blocks = (sb->st_size + sb->st_blksize - 1) / sb->st_blksize;
225175164Sjhb	sb->st_atimespec = shmfd->shm_atime;
226175164Sjhb	sb->st_ctimespec = shmfd->shm_ctime;
227175164Sjhb	sb->st_mtimespec = shmfd->shm_mtime;
228175164Sjhb	sb->st_birthtimespec = shmfd->shm_birthtime;
229175164Sjhb	sb->st_uid = shmfd->shm_uid;
230175164Sjhb	sb->st_gid = shmfd->shm_gid;
231175164Sjhb
232175164Sjhb	return (0);
233175164Sjhb}
234175164Sjhb
235175164Sjhbstatic int
236175164Sjhbshm_close(struct file *fp, struct thread *td)
237175164Sjhb{
238175164Sjhb	struct shmfd *shmfd;
239175164Sjhb
240175164Sjhb	shmfd = fp->f_data;
241175164Sjhb	fp->f_data = NULL;
242175164Sjhb	shm_drop(shmfd);
243175164Sjhb
244175164Sjhb	return (0);
245175164Sjhb}
246175164Sjhb
247175164Sjhbstatic void
248175164Sjhbshm_dotruncate(struct shmfd *shmfd, off_t length)
249175164Sjhb{
250175164Sjhb	vm_object_t object;
251175164Sjhb	vm_page_t m;
252175164Sjhb	vm_pindex_t nobjsize;
253175164Sjhb
254175164Sjhb	object = shmfd->shm_object;
255175164Sjhb	VM_OBJECT_LOCK(object);
256175164Sjhb	if (length == shmfd->shm_size) {
257175164Sjhb		VM_OBJECT_UNLOCK(object);
258175164Sjhb		return;
259175164Sjhb	}
260175164Sjhb	nobjsize = OFF_TO_IDX(length + PAGE_MASK);
261175164Sjhb
262175164Sjhb	/* Are we shrinking?  If so, trim the end. */
263175164Sjhb	if (length < shmfd->shm_size) {
264175164Sjhb		/* Toss in memory pages. */
265175164Sjhb		if (nobjsize < object->size)
266175164Sjhb			vm_object_page_remove(object, nobjsize, object->size,
267175164Sjhb			    FALSE);
268175164Sjhb
269175164Sjhb		/* Toss pages from swap. */
270175164Sjhb		if (object->type == OBJT_SWAP)
271175164Sjhb			swap_pager_freespace(object, nobjsize,
272175164Sjhb			    object->size - nobjsize);
273175164Sjhb
274175164Sjhb		/*
275175164Sjhb		 * If the last page is partially mapped, then zero out
276175164Sjhb		 * the garbage at the end of the page.  See comments
277175164Sjhb		 * in vnode_page_setsize() for more details.
278175164Sjhb		 *
279175164Sjhb		 * XXXJHB: This handles in memory pages, but what about
280175164Sjhb		 * a page swapped out to disk?
281175164Sjhb		 */
282175164Sjhb		if ((length & PAGE_MASK) &&
283175164Sjhb		    (m = vm_page_lookup(object, OFF_TO_IDX(length))) != NULL &&
284175164Sjhb		    m->valid != 0) {
285175164Sjhb			int base = (int)length & PAGE_MASK;
286175164Sjhb			int size = PAGE_SIZE - base;
287175164Sjhb
288175164Sjhb			pmap_zero_page_area(m, base, size);
289175164Sjhb			vm_page_lock_queues();
290175164Sjhb			vm_page_set_validclean(m, base, size);
291175164Sjhb			if (m->dirty != 0)
292175164Sjhb				m->dirty = VM_PAGE_BITS_ALL;
293175164Sjhb			vm_page_unlock_queues();
294176075Salc		} else if ((length & PAGE_MASK) &&
295176075Salc		    __predict_false(object->cache != NULL)) {
296176075Salc			vm_page_cache_free(object, OFF_TO_IDX(length),
297176075Salc			    nobjsize);
298175164Sjhb		}
299175164Sjhb	}
300175164Sjhb	shmfd->shm_size = length;
301175164Sjhb	mtx_lock(&shm_timestamp_lock);
302175164Sjhb	vfs_timestamp(&shmfd->shm_ctime);
303175164Sjhb	shmfd->shm_mtime = shmfd->shm_ctime;
304175164Sjhb	mtx_unlock(&shm_timestamp_lock);
305175164Sjhb	object->size = nobjsize;
306175164Sjhb	VM_OBJECT_UNLOCK(object);
307175164Sjhb}
308175164Sjhb
309175164Sjhb/*
310175164Sjhb * shmfd object management including creation and reference counting
311175164Sjhb * routines.
312175164Sjhb */
313175164Sjhbstatic struct shmfd *
314175164Sjhbshm_alloc(struct ucred *ucred, mode_t mode)
315175164Sjhb{
316175164Sjhb	struct shmfd *shmfd;
317175164Sjhb
318175164Sjhb	shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO);
319175164Sjhb	shmfd->shm_size = 0;
320175164Sjhb	shmfd->shm_uid = ucred->cr_uid;
321175164Sjhb	shmfd->shm_gid = ucred->cr_gid;
322175164Sjhb	shmfd->shm_mode = mode;
323175164Sjhb	shmfd->shm_object = vm_pager_allocate(OBJT_DEFAULT, NULL,
324175164Sjhb	    shmfd->shm_size, VM_PROT_DEFAULT, 0);
325175164Sjhb	KASSERT(shmfd->shm_object != NULL, ("shm_create: vm_pager_allocate"));
326178181Salc	VM_OBJECT_LOCK(shmfd->shm_object);
327178181Salc	vm_object_clear_flag(shmfd->shm_object, OBJ_ONEMAPPING);
328178181Salc	vm_object_set_flag(shmfd->shm_object, OBJ_NOSPLIT);
329178181Salc	VM_OBJECT_UNLOCK(shmfd->shm_object);
330175164Sjhb	vfs_timestamp(&shmfd->shm_birthtime);
331175164Sjhb	shmfd->shm_atime = shmfd->shm_mtime = shmfd->shm_ctime =
332175164Sjhb	    shmfd->shm_birthtime;
333175164Sjhb	refcount_init(&shmfd->shm_refs, 1);
334175164Sjhb#ifdef MAC
335175164Sjhb	mac_posixshm_init(shmfd);
336175164Sjhb	mac_posixshm_create(ucred, shmfd);
337175164Sjhb#endif
338175164Sjhb
339175164Sjhb	return (shmfd);
340175164Sjhb}
341175164Sjhb
342175164Sjhbstatic struct shmfd *
343175164Sjhbshm_hold(struct shmfd *shmfd)
344175164Sjhb{
345175164Sjhb
346175164Sjhb	refcount_acquire(&shmfd->shm_refs);
347175164Sjhb	return (shmfd);
348175164Sjhb}
349175164Sjhb
350175164Sjhbstatic void
351175164Sjhbshm_drop(struct shmfd *shmfd)
352175164Sjhb{
353175164Sjhb
354175164Sjhb	if (refcount_release(&shmfd->shm_refs)) {
355175164Sjhb#ifdef MAC
356175164Sjhb		mac_posixshm_destroy(shmfd);
357175164Sjhb#endif
358175164Sjhb		vm_object_deallocate(shmfd->shm_object);
359175164Sjhb		free(shmfd, M_SHMFD);
360175164Sjhb	}
361175164Sjhb}
362175164Sjhb
363175164Sjhb/*
364175164Sjhb * Determine if the credentials have sufficient permissions for a
365175164Sjhb * specified combination of FREAD and FWRITE.
366175164Sjhb */
367175164Sjhbstatic int
368175164Sjhbshm_access(struct shmfd *shmfd, struct ucred *ucred, int flags)
369175164Sjhb{
370184413Strasz	accmode_t accmode;
371175164Sjhb
372184413Strasz	accmode = 0;
373175164Sjhb	if (flags & FREAD)
374184413Strasz		accmode |= VREAD;
375175164Sjhb	if (flags & FWRITE)
376184413Strasz		accmode |= VWRITE;
377175164Sjhb	return (vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid,
378184413Strasz	    accmode, ucred, NULL));
379175164Sjhb}
380175164Sjhb
381175164Sjhb/*
382175164Sjhb * Dictionary management.  We maintain an in-kernel dictionary to map
383175164Sjhb * paths to shmfd objects.  We use the FNV hash on the path to store
384175164Sjhb * the mappings in a hash table.
385175164Sjhb */
386175164Sjhbstatic void
387175164Sjhbshm_dict_init(void *arg)
388175164Sjhb{
389175164Sjhb
390175164Sjhb	mtx_init(&shm_timestamp_lock, "shm timestamps", NULL, MTX_DEF);
391175164Sjhb	sx_init(&shm_dict_lock, "shm dictionary");
392175164Sjhb	shm_dictionary = hashinit(1024, M_SHMFD, &shm_hash);
393175164Sjhb}
394175164SjhbSYSINIT(shm_dict_init, SI_SUB_SYSV_SHM, SI_ORDER_ANY, shm_dict_init, NULL);
395175164Sjhb
396175164Sjhbstatic struct shmfd *
397175164Sjhbshm_lookup(char *path, Fnv32_t fnv)
398175164Sjhb{
399175164Sjhb	struct shm_mapping *map;
400175164Sjhb
401175164Sjhb	LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
402175164Sjhb		if (map->sm_fnv != fnv)
403175164Sjhb			continue;
404175164Sjhb		if (strcmp(map->sm_path, path) == 0)
405175164Sjhb			return (map->sm_shmfd);
406175164Sjhb	}
407175164Sjhb
408175164Sjhb	return (NULL);
409175164Sjhb}
410175164Sjhb
411175164Sjhbstatic void
412175164Sjhbshm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd)
413175164Sjhb{
414175164Sjhb	struct shm_mapping *map;
415175164Sjhb
416175164Sjhb	map = malloc(sizeof(struct shm_mapping), M_SHMFD, M_WAITOK);
417175164Sjhb	map->sm_path = path;
418175164Sjhb	map->sm_fnv = fnv;
419175164Sjhb	map->sm_shmfd = shm_hold(shmfd);
420175164Sjhb	LIST_INSERT_HEAD(SHM_HASH(fnv), map, sm_link);
421175164Sjhb}
422175164Sjhb
423175164Sjhbstatic int
424175164Sjhbshm_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
425175164Sjhb{
426175164Sjhb	struct shm_mapping *map;
427175164Sjhb	int error;
428175164Sjhb
429175164Sjhb	LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
430175164Sjhb		if (map->sm_fnv != fnv)
431175164Sjhb			continue;
432175164Sjhb		if (strcmp(map->sm_path, path) == 0) {
433175164Sjhb#ifdef MAC
434175164Sjhb			error = mac_posixshm_check_unlink(ucred, map->sm_shmfd);
435175164Sjhb			if (error)
436175164Sjhb				return (error);
437175164Sjhb#endif
438175164Sjhb			error = shm_access(map->sm_shmfd, ucred,
439175164Sjhb			    FREAD | FWRITE);
440175164Sjhb			if (error)
441175164Sjhb				return (error);
442175164Sjhb			LIST_REMOVE(map, sm_link);
443175164Sjhb			shm_drop(map->sm_shmfd);
444175164Sjhb			free(map->sm_path, M_SHMFD);
445175164Sjhb			free(map, M_SHMFD);
446175164Sjhb			return (0);
447175164Sjhb		}
448175164Sjhb	}
449175164Sjhb
450175164Sjhb	return (ENOENT);
451175164Sjhb}
452175164Sjhb
453175164Sjhb/* System calls. */
454175164Sjhbint
455175164Sjhbshm_open(struct thread *td, struct shm_open_args *uap)
456175164Sjhb{
457175164Sjhb	struct filedesc *fdp;
458175164Sjhb	struct shmfd *shmfd;
459175164Sjhb	struct file *fp;
460175164Sjhb	char *path;
461175164Sjhb	Fnv32_t fnv;
462175164Sjhb	mode_t cmode;
463175164Sjhb	int fd, error;
464175164Sjhb
465175164Sjhb	if ((uap->flags & O_ACCMODE) != O_RDONLY &&
466175164Sjhb	    (uap->flags & O_ACCMODE) != O_RDWR)
467175164Sjhb		return (EINVAL);
468175164Sjhb
469175164Sjhb	if ((uap->flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) != 0)
470175164Sjhb		return (EINVAL);
471175164Sjhb
472175164Sjhb	fdp = td->td_proc->p_fd;
473175164Sjhb	cmode = (uap->mode & ~fdp->fd_cmask) & ACCESSPERMS;
474175164Sjhb
475175164Sjhb	error = falloc(td, &fp, &fd);
476175164Sjhb	if (error)
477175164Sjhb		return (error);
478175164Sjhb
479175164Sjhb	/* A SHM_ANON path pointer creates an anonymous object. */
480175164Sjhb	if (uap->path == SHM_ANON) {
481175164Sjhb		/* A read-only anonymous object is pointless. */
482175164Sjhb		if ((uap->flags & O_ACCMODE) == O_RDONLY) {
483175164Sjhb			fdclose(fdp, fp, fd, td);
484175164Sjhb			fdrop(fp, td);
485175164Sjhb			return (EINVAL);
486175164Sjhb		}
487175164Sjhb		shmfd = shm_alloc(td->td_ucred, cmode);
488175164Sjhb	} else {
489175164Sjhb		path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK);
490175164Sjhb		error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
491175164Sjhb
492175164Sjhb		/* Require paths to start with a '/' character. */
493175164Sjhb		if (error == 0 && path[0] != '/')
494175164Sjhb			error = EINVAL;
495175164Sjhb		if (error) {
496175164Sjhb			fdclose(fdp, fp, fd, td);
497175164Sjhb			fdrop(fp, td);
498175164Sjhb			free(path, M_SHMFD);
499175164Sjhb			return (error);
500175164Sjhb		}
501175164Sjhb
502175164Sjhb		fnv = fnv_32_str(path, FNV1_32_INIT);
503175164Sjhb		sx_xlock(&shm_dict_lock);
504175164Sjhb		shmfd = shm_lookup(path, fnv);
505175164Sjhb		if (shmfd == NULL) {
506175164Sjhb			/* Object does not yet exist, create it if requested. */
507175164Sjhb			if (uap->flags & O_CREAT) {
508175164Sjhb				shmfd = shm_alloc(td->td_ucred, cmode);
509175164Sjhb				shm_insert(path, fnv, shmfd);
510175164Sjhb			} else {
511175164Sjhb				free(path, M_SHMFD);
512175164Sjhb				error = ENOENT;
513175164Sjhb			}
514175164Sjhb		} else {
515175164Sjhb			/*
516175164Sjhb			 * Object already exists, obtain a new
517175164Sjhb			 * reference if requested and permitted.
518175164Sjhb			 */
519175164Sjhb			free(path, M_SHMFD);
520175164Sjhb			if ((uap->flags & (O_CREAT | O_EXCL)) ==
521175164Sjhb			    (O_CREAT | O_EXCL))
522175164Sjhb				error = EEXIST;
523175164Sjhb			else {
524175164Sjhb#ifdef MAC
525175164Sjhb				error = mac_posixshm_check_open(td->td_ucred,
526175164Sjhb				    shmfd);
527175164Sjhb				if (error == 0)
528175164Sjhb#endif
529175164Sjhb				error = shm_access(shmfd, td->td_ucred,
530175164Sjhb				    FFLAGS(uap->flags & O_ACCMODE));
531175164Sjhb			}
532175164Sjhb
533175164Sjhb			/*
534175164Sjhb			 * Truncate the file back to zero length if
535175164Sjhb			 * O_TRUNC was specified and the object was
536175164Sjhb			 * opened with read/write.
537175164Sjhb			 */
538175164Sjhb			if (error == 0 &&
539175164Sjhb			    (uap->flags & (O_ACCMODE | O_TRUNC)) ==
540175164Sjhb			    (O_RDWR | O_TRUNC)) {
541175164Sjhb#ifdef MAC
542175164Sjhb				error = mac_posixshm_check_truncate(
543175164Sjhb					td->td_ucred, fp->f_cred, shmfd);
544175164Sjhb				if (error == 0)
545175164Sjhb#endif
546175164Sjhb					shm_dotruncate(shmfd, 0);
547175164Sjhb			}
548175164Sjhb			if (error == 0)
549175164Sjhb				shm_hold(shmfd);
550175164Sjhb		}
551175164Sjhb		sx_xunlock(&shm_dict_lock);
552175164Sjhb
553175164Sjhb		if (error) {
554175164Sjhb			fdclose(fdp, fp, fd, td);
555175164Sjhb			fdrop(fp, td);
556175164Sjhb			return (error);
557175164Sjhb		}
558175164Sjhb	}
559175164Sjhb
560175164Sjhb	finit(fp, FFLAGS(uap->flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops);
561175164Sjhb
562175164Sjhb	FILEDESC_XLOCK(fdp);
563175164Sjhb	if (fdp->fd_ofiles[fd] == fp)
564175164Sjhb		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
565175164Sjhb	FILEDESC_XUNLOCK(fdp);
566175164Sjhb	td->td_retval[0] = fd;
567175164Sjhb	fdrop(fp, td);
568175164Sjhb
569175164Sjhb	return (0);
570175164Sjhb}
571175164Sjhb
572175164Sjhbint
573175164Sjhbshm_unlink(struct thread *td, struct shm_unlink_args *uap)
574175164Sjhb{
575175164Sjhb	char *path;
576175164Sjhb	Fnv32_t fnv;
577175164Sjhb	int error;
578175164Sjhb
579175164Sjhb	path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
580175164Sjhb	error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
581175164Sjhb	if (error) {
582175164Sjhb		free(path, M_TEMP);
583175164Sjhb		return (error);
584175164Sjhb	}
585175164Sjhb
586175164Sjhb	fnv = fnv_32_str(path, FNV1_32_INIT);
587175164Sjhb	sx_xlock(&shm_dict_lock);
588175164Sjhb	error = shm_remove(path, fnv, td->td_ucred);
589175164Sjhb	sx_xunlock(&shm_dict_lock);
590175164Sjhb	free(path, M_TEMP);
591175164Sjhb
592175164Sjhb	return (error);
593175164Sjhb}
594175164Sjhb
595175164Sjhb/*
596175164Sjhb * mmap() helper to validate mmap() requests against shm object state
597175164Sjhb * and give mmap() the vm_object to use for the mapping.
598175164Sjhb */
599175164Sjhbint
600175164Sjhbshm_mmap(struct shmfd *shmfd, vm_size_t objsize, vm_ooffset_t foff,
601175164Sjhb    vm_object_t *obj)
602175164Sjhb{
603175164Sjhb
604175164Sjhb	/*
605175164Sjhb	 * XXXRW: This validation is probably insufficient, and subject to
606175164Sjhb	 * sign errors.  It should be fixed.
607175164Sjhb	 */
608185533Skan	if (foff >= shmfd->shm_size ||
609185533Skan	    foff + objsize > round_page(shmfd->shm_size))
610175164Sjhb		return (EINVAL);
611175164Sjhb
612175164Sjhb	mtx_lock(&shm_timestamp_lock);
613175164Sjhb	vfs_timestamp(&shmfd->shm_atime);
614175164Sjhb	mtx_unlock(&shm_timestamp_lock);
615175164Sjhb	vm_object_reference(shmfd->shm_object);
616175164Sjhb	*obj = shmfd->shm_object;
617175164Sjhb	return (0);
618175164Sjhb}
619