uipc_shm.c revision 223677
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 223677 2011-06-29 16:40:41Z alc $");
57175164Sjhb
58175164Sjhb#include <sys/param.h>
59175164Sjhb#include <sys/fcntl.h>
60175164Sjhb#include <sys/file.h>
61175164Sjhb#include <sys/filedesc.h>
62175164Sjhb#include <sys/fnv_hash.h>
63175164Sjhb#include <sys/kernel.h>
64175164Sjhb#include <sys/lock.h>
65175164Sjhb#include <sys/malloc.h>
66175164Sjhb#include <sys/mman.h>
67175164Sjhb#include <sys/mutex.h>
68175164Sjhb#include <sys/proc.h>
69175164Sjhb#include <sys/refcount.h>
70175164Sjhb#include <sys/resourcevar.h>
71175164Sjhb#include <sys/stat.h>
72175164Sjhb#include <sys/sysctl.h>
73175164Sjhb#include <sys/sysproto.h>
74175164Sjhb#include <sys/systm.h>
75175164Sjhb#include <sys/sx.h>
76175164Sjhb#include <sys/time.h>
77175164Sjhb#include <sys/vnode.h>
78175164Sjhb
79175164Sjhb#include <security/mac/mac_framework.h>
80175164Sjhb
81175164Sjhb#include <vm/vm.h>
82175164Sjhb#include <vm/vm_param.h>
83175164Sjhb#include <vm/pmap.h>
84175164Sjhb#include <vm/vm_map.h>
85175164Sjhb#include <vm/vm_object.h>
86175164Sjhb#include <vm/vm_page.h>
87175164Sjhb#include <vm/vm_pager.h>
88175164Sjhb#include <vm/swap_pager.h>
89175164Sjhb
90175164Sjhbstruct shm_mapping {
91175164Sjhb	char		*sm_path;
92175164Sjhb	Fnv32_t		sm_fnv;
93175164Sjhb	struct shmfd	*sm_shmfd;
94175164Sjhb	LIST_ENTRY(shm_mapping) sm_link;
95175164Sjhb};
96175164Sjhb
97175164Sjhbstatic MALLOC_DEFINE(M_SHMFD, "shmfd", "shared memory file descriptor");
98175164Sjhbstatic LIST_HEAD(, shm_mapping) *shm_dictionary;
99175164Sjhbstatic struct sx shm_dict_lock;
100175164Sjhbstatic struct mtx shm_timestamp_lock;
101175164Sjhbstatic u_long shm_hash;
102175164Sjhb
103175164Sjhb#define	SHM_HASH(fnv)	(&shm_dictionary[(fnv) & shm_hash])
104175164Sjhb
105175164Sjhbstatic int	shm_access(struct shmfd *shmfd, struct ucred *ucred, int flags);
106175164Sjhbstatic struct shmfd *shm_alloc(struct ucred *ucred, mode_t mode);
107175164Sjhbstatic void	shm_dict_init(void *arg);
108175164Sjhbstatic void	shm_drop(struct shmfd *shmfd);
109175164Sjhbstatic struct shmfd *shm_hold(struct shmfd *shmfd);
110175164Sjhbstatic void	shm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd);
111175164Sjhbstatic struct shmfd *shm_lookup(char *path, Fnv32_t fnv);
112175164Sjhbstatic int	shm_remove(char *path, Fnv32_t fnv, struct ucred *ucred);
113194766Skibstatic int	shm_dotruncate(struct shmfd *shmfd, off_t length);
114175164Sjhb
115175164Sjhbstatic fo_rdwr_t	shm_read;
116175164Sjhbstatic fo_rdwr_t	shm_write;
117175164Sjhbstatic fo_truncate_t	shm_truncate;
118175164Sjhbstatic fo_ioctl_t	shm_ioctl;
119175164Sjhbstatic fo_poll_t	shm_poll;
120175164Sjhbstatic fo_kqfilter_t	shm_kqfilter;
121175164Sjhbstatic fo_stat_t	shm_stat;
122175164Sjhbstatic fo_close_t	shm_close;
123175164Sjhb
124175164Sjhb/* File descriptor operations. */
125175164Sjhbstatic struct fileops shm_ops = {
126175164Sjhb	.fo_read = shm_read,
127175164Sjhb	.fo_write = shm_write,
128175164Sjhb	.fo_truncate = shm_truncate,
129175164Sjhb	.fo_ioctl = shm_ioctl,
130175164Sjhb	.fo_poll = shm_poll,
131175164Sjhb	.fo_kqfilter = shm_kqfilter,
132175164Sjhb	.fo_stat = shm_stat,
133175164Sjhb	.fo_close = shm_close,
134175164Sjhb	.fo_flags = DFLAG_PASSABLE
135175164Sjhb};
136175164Sjhb
137175164SjhbFEATURE(posix_shm, "POSIX shared memory");
138175164Sjhb
139175164Sjhbstatic int
140175164Sjhbshm_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
141175164Sjhb    int flags, struct thread *td)
142175164Sjhb{
143175164Sjhb
144175164Sjhb	return (EOPNOTSUPP);
145175164Sjhb}
146175164Sjhb
147175164Sjhbstatic int
148175164Sjhbshm_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
149175164Sjhb    int flags, struct thread *td)
150175164Sjhb{
151175164Sjhb
152175164Sjhb	return (EOPNOTSUPP);
153175164Sjhb}
154175164Sjhb
155175164Sjhbstatic int
156175164Sjhbshm_truncate(struct file *fp, off_t length, struct ucred *active_cred,
157175164Sjhb    struct thread *td)
158175164Sjhb{
159175164Sjhb	struct shmfd *shmfd;
160175164Sjhb#ifdef MAC
161175164Sjhb	int error;
162175164Sjhb#endif
163175164Sjhb
164175164Sjhb	shmfd = fp->f_data;
165175164Sjhb#ifdef MAC
166175164Sjhb	error = mac_posixshm_check_truncate(active_cred, fp->f_cred, shmfd);
167175164Sjhb	if (error)
168175164Sjhb		return (error);
169175164Sjhb#endif
170194766Skib	return (shm_dotruncate(shmfd, length));
171175164Sjhb}
172175164Sjhb
173175164Sjhbstatic int
174175164Sjhbshm_ioctl(struct file *fp, u_long com, void *data,
175175164Sjhb    struct ucred *active_cred, struct thread *td)
176175164Sjhb{
177175164Sjhb
178175164Sjhb	return (EOPNOTSUPP);
179175164Sjhb}
180175164Sjhb
181175164Sjhbstatic int
182175164Sjhbshm_poll(struct file *fp, int events, struct ucred *active_cred,
183175164Sjhb    struct thread *td)
184175164Sjhb{
185175164Sjhb
186175164Sjhb	return (EOPNOTSUPP);
187175164Sjhb}
188175164Sjhb
189175164Sjhbstatic int
190175164Sjhbshm_kqfilter(struct file *fp, struct knote *kn)
191175164Sjhb{
192175164Sjhb
193175164Sjhb	return (EOPNOTSUPP);
194175164Sjhb}
195175164Sjhb
196175164Sjhbstatic int
197175164Sjhbshm_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
198175164Sjhb    struct thread *td)
199175164Sjhb{
200175164Sjhb	struct shmfd *shmfd;
201175164Sjhb#ifdef MAC
202175164Sjhb	int error;
203175164Sjhb#endif
204175164Sjhb
205175164Sjhb	shmfd = fp->f_data;
206175164Sjhb
207175164Sjhb#ifdef MAC
208175164Sjhb	error = mac_posixshm_check_stat(active_cred, fp->f_cred, shmfd);
209175164Sjhb	if (error)
210175164Sjhb		return (error);
211175164Sjhb#endif
212175164Sjhb
213175164Sjhb	/*
214175164Sjhb	 * Attempt to return sanish values for fstat() on a memory file
215175164Sjhb	 * descriptor.
216175164Sjhb	 */
217175164Sjhb	bzero(sb, sizeof(*sb));
218175164Sjhb	sb->st_mode = S_IFREG | shmfd->shm_mode;		/* XXX */
219175164Sjhb	sb->st_blksize = PAGE_SIZE;
220175164Sjhb	sb->st_size = shmfd->shm_size;
221175164Sjhb	sb->st_blocks = (sb->st_size + sb->st_blksize - 1) / sb->st_blksize;
222205792Sed	sb->st_atim = shmfd->shm_atime;
223205792Sed	sb->st_ctim = shmfd->shm_ctime;
224205792Sed	sb->st_mtim = shmfd->shm_mtime;
225205792Sed	sb->st_birthtim = shmfd->shm_birthtime;
226175164Sjhb	sb->st_uid = shmfd->shm_uid;
227175164Sjhb	sb->st_gid = shmfd->shm_gid;
228175164Sjhb
229175164Sjhb	return (0);
230175164Sjhb}
231175164Sjhb
232175164Sjhbstatic int
233175164Sjhbshm_close(struct file *fp, struct thread *td)
234175164Sjhb{
235175164Sjhb	struct shmfd *shmfd;
236175164Sjhb
237175164Sjhb	shmfd = fp->f_data;
238175164Sjhb	fp->f_data = NULL;
239175164Sjhb	shm_drop(shmfd);
240175164Sjhb
241175164Sjhb	return (0);
242175164Sjhb}
243175164Sjhb
244194766Skibstatic int
245175164Sjhbshm_dotruncate(struct shmfd *shmfd, off_t length)
246175164Sjhb{
247175164Sjhb	vm_object_t object;
248175164Sjhb	vm_page_t m;
249175164Sjhb	vm_pindex_t nobjsize;
250194766Skib	vm_ooffset_t delta;
251175164Sjhb
252175164Sjhb	object = shmfd->shm_object;
253175164Sjhb	VM_OBJECT_LOCK(object);
254175164Sjhb	if (length == shmfd->shm_size) {
255175164Sjhb		VM_OBJECT_UNLOCK(object);
256194766Skib		return (0);
257175164Sjhb	}
258175164Sjhb	nobjsize = OFF_TO_IDX(length + PAGE_MASK);
259175164Sjhb
260175164Sjhb	/* Are we shrinking?  If so, trim the end. */
261175164Sjhb	if (length < shmfd->shm_size) {
262194766Skib		delta = ptoa(object->size - nobjsize);
263194766Skib
264175164Sjhb		/* Toss in memory pages. */
265175164Sjhb		if (nobjsize < object->size)
266175164Sjhb			vm_object_page_remove(object, nobjsize, object->size,
267223677Salc			    0);
268175164Sjhb
269175164Sjhb		/* Toss pages from swap. */
270175164Sjhb		if (object->type == OBJT_SWAP)
271194766Skib			swap_pager_freespace(object, nobjsize, delta);
272175164Sjhb
273194766Skib		/* Free the swap accounted for shm */
274216128Strasz		swap_release_by_cred(delta, object->cred);
275194766Skib		object->charge -= delta;
276194766Skib
277175164Sjhb		/*
278175164Sjhb		 * If the last page is partially mapped, then zero out
279175164Sjhb		 * the garbage at the end of the page.  See comments
280193303Salc		 * in vnode_pager_setsize() for more details.
281175164Sjhb		 *
282175164Sjhb		 * XXXJHB: This handles in memory pages, but what about
283175164Sjhb		 * a page swapped out to disk?
284175164Sjhb		 */
285175164Sjhb		if ((length & PAGE_MASK) &&
286175164Sjhb		    (m = vm_page_lookup(object, OFF_TO_IDX(length))) != NULL &&
287175164Sjhb		    m->valid != 0) {
288175164Sjhb			int base = (int)length & PAGE_MASK;
289175164Sjhb			int size = PAGE_SIZE - base;
290175164Sjhb
291175164Sjhb			pmap_zero_page_area(m, base, size);
292193303Salc
293193303Salc			/*
294193303Salc			 * Update the valid bits to reflect the blocks that
295193303Salc			 * have been zeroed.  Some of these valid bits may
296193303Salc			 * have already been set.
297193303Salc			 */
298193303Salc			vm_page_set_valid(m, base, size);
299193303Salc
300193303Salc			/*
301193303Salc			 * Round "base" to the next block boundary so that the
302193303Salc			 * dirty bit for a partially zeroed block is not
303193303Salc			 * cleared.
304193303Salc			 */
305193303Salc			base = roundup2(base, DEV_BSIZE);
306193303Salc
307193303Salc			vm_page_clear_dirty(m, base, PAGE_SIZE - base);
308176075Salc		} else if ((length & PAGE_MASK) &&
309176075Salc		    __predict_false(object->cache != NULL)) {
310176075Salc			vm_page_cache_free(object, OFF_TO_IDX(length),
311176075Salc			    nobjsize);
312175164Sjhb		}
313194766Skib	} else {
314194766Skib
315194766Skib		/* Attempt to reserve the swap */
316194766Skib		delta = ptoa(nobjsize - object->size);
317216128Strasz		if (!swap_reserve_by_cred(delta, object->cred)) {
318194766Skib			VM_OBJECT_UNLOCK(object);
319194766Skib			return (ENOMEM);
320194766Skib		}
321194766Skib		object->charge += delta;
322175164Sjhb	}
323175164Sjhb	shmfd->shm_size = length;
324175164Sjhb	mtx_lock(&shm_timestamp_lock);
325175164Sjhb	vfs_timestamp(&shmfd->shm_ctime);
326175164Sjhb	shmfd->shm_mtime = shmfd->shm_ctime;
327175164Sjhb	mtx_unlock(&shm_timestamp_lock);
328175164Sjhb	object->size = nobjsize;
329175164Sjhb	VM_OBJECT_UNLOCK(object);
330194766Skib	return (0);
331175164Sjhb}
332175164Sjhb
333175164Sjhb/*
334175164Sjhb * shmfd object management including creation and reference counting
335175164Sjhb * routines.
336175164Sjhb */
337175164Sjhbstatic struct shmfd *
338175164Sjhbshm_alloc(struct ucred *ucred, mode_t mode)
339175164Sjhb{
340175164Sjhb	struct shmfd *shmfd;
341175164Sjhb
342175164Sjhb	shmfd = malloc(sizeof(*shmfd), M_SHMFD, M_WAITOK | M_ZERO);
343175164Sjhb	shmfd->shm_size = 0;
344175164Sjhb	shmfd->shm_uid = ucred->cr_uid;
345175164Sjhb	shmfd->shm_gid = ucred->cr_gid;
346175164Sjhb	shmfd->shm_mode = mode;
347175164Sjhb	shmfd->shm_object = vm_pager_allocate(OBJT_DEFAULT, NULL,
348194766Skib	    shmfd->shm_size, VM_PROT_DEFAULT, 0, ucred);
349175164Sjhb	KASSERT(shmfd->shm_object != NULL, ("shm_create: vm_pager_allocate"));
350178181Salc	VM_OBJECT_LOCK(shmfd->shm_object);
351178181Salc	vm_object_clear_flag(shmfd->shm_object, OBJ_ONEMAPPING);
352178181Salc	vm_object_set_flag(shmfd->shm_object, OBJ_NOSPLIT);
353178181Salc	VM_OBJECT_UNLOCK(shmfd->shm_object);
354175164Sjhb	vfs_timestamp(&shmfd->shm_birthtime);
355175164Sjhb	shmfd->shm_atime = shmfd->shm_mtime = shmfd->shm_ctime =
356175164Sjhb	    shmfd->shm_birthtime;
357175164Sjhb	refcount_init(&shmfd->shm_refs, 1);
358175164Sjhb#ifdef MAC
359175164Sjhb	mac_posixshm_init(shmfd);
360175164Sjhb	mac_posixshm_create(ucred, shmfd);
361175164Sjhb#endif
362175164Sjhb
363175164Sjhb	return (shmfd);
364175164Sjhb}
365175164Sjhb
366175164Sjhbstatic struct shmfd *
367175164Sjhbshm_hold(struct shmfd *shmfd)
368175164Sjhb{
369175164Sjhb
370175164Sjhb	refcount_acquire(&shmfd->shm_refs);
371175164Sjhb	return (shmfd);
372175164Sjhb}
373175164Sjhb
374175164Sjhbstatic void
375175164Sjhbshm_drop(struct shmfd *shmfd)
376175164Sjhb{
377175164Sjhb
378175164Sjhb	if (refcount_release(&shmfd->shm_refs)) {
379175164Sjhb#ifdef MAC
380175164Sjhb		mac_posixshm_destroy(shmfd);
381175164Sjhb#endif
382175164Sjhb		vm_object_deallocate(shmfd->shm_object);
383175164Sjhb		free(shmfd, M_SHMFD);
384175164Sjhb	}
385175164Sjhb}
386175164Sjhb
387175164Sjhb/*
388175164Sjhb * Determine if the credentials have sufficient permissions for a
389175164Sjhb * specified combination of FREAD and FWRITE.
390175164Sjhb */
391175164Sjhbstatic int
392175164Sjhbshm_access(struct shmfd *shmfd, struct ucred *ucred, int flags)
393175164Sjhb{
394184413Strasz	accmode_t accmode;
395175164Sjhb
396184413Strasz	accmode = 0;
397175164Sjhb	if (flags & FREAD)
398184413Strasz		accmode |= VREAD;
399175164Sjhb	if (flags & FWRITE)
400184413Strasz		accmode |= VWRITE;
401175164Sjhb	return (vaccess(VREG, shmfd->shm_mode, shmfd->shm_uid, shmfd->shm_gid,
402184413Strasz	    accmode, ucred, NULL));
403175164Sjhb}
404175164Sjhb
405175164Sjhb/*
406175164Sjhb * Dictionary management.  We maintain an in-kernel dictionary to map
407175164Sjhb * paths to shmfd objects.  We use the FNV hash on the path to store
408175164Sjhb * the mappings in a hash table.
409175164Sjhb */
410175164Sjhbstatic void
411175164Sjhbshm_dict_init(void *arg)
412175164Sjhb{
413175164Sjhb
414175164Sjhb	mtx_init(&shm_timestamp_lock, "shm timestamps", NULL, MTX_DEF);
415175164Sjhb	sx_init(&shm_dict_lock, "shm dictionary");
416175164Sjhb	shm_dictionary = hashinit(1024, M_SHMFD, &shm_hash);
417175164Sjhb}
418175164SjhbSYSINIT(shm_dict_init, SI_SUB_SYSV_SHM, SI_ORDER_ANY, shm_dict_init, NULL);
419175164Sjhb
420175164Sjhbstatic struct shmfd *
421175164Sjhbshm_lookup(char *path, Fnv32_t fnv)
422175164Sjhb{
423175164Sjhb	struct shm_mapping *map;
424175164Sjhb
425175164Sjhb	LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
426175164Sjhb		if (map->sm_fnv != fnv)
427175164Sjhb			continue;
428175164Sjhb		if (strcmp(map->sm_path, path) == 0)
429175164Sjhb			return (map->sm_shmfd);
430175164Sjhb	}
431175164Sjhb
432175164Sjhb	return (NULL);
433175164Sjhb}
434175164Sjhb
435175164Sjhbstatic void
436175164Sjhbshm_insert(char *path, Fnv32_t fnv, struct shmfd *shmfd)
437175164Sjhb{
438175164Sjhb	struct shm_mapping *map;
439175164Sjhb
440175164Sjhb	map = malloc(sizeof(struct shm_mapping), M_SHMFD, M_WAITOK);
441175164Sjhb	map->sm_path = path;
442175164Sjhb	map->sm_fnv = fnv;
443175164Sjhb	map->sm_shmfd = shm_hold(shmfd);
444175164Sjhb	LIST_INSERT_HEAD(SHM_HASH(fnv), map, sm_link);
445175164Sjhb}
446175164Sjhb
447175164Sjhbstatic int
448175164Sjhbshm_remove(char *path, Fnv32_t fnv, struct ucred *ucred)
449175164Sjhb{
450175164Sjhb	struct shm_mapping *map;
451175164Sjhb	int error;
452175164Sjhb
453175164Sjhb	LIST_FOREACH(map, SHM_HASH(fnv), sm_link) {
454175164Sjhb		if (map->sm_fnv != fnv)
455175164Sjhb			continue;
456175164Sjhb		if (strcmp(map->sm_path, path) == 0) {
457175164Sjhb#ifdef MAC
458175164Sjhb			error = mac_posixshm_check_unlink(ucred, map->sm_shmfd);
459175164Sjhb			if (error)
460175164Sjhb				return (error);
461175164Sjhb#endif
462175164Sjhb			error = shm_access(map->sm_shmfd, ucred,
463175164Sjhb			    FREAD | FWRITE);
464175164Sjhb			if (error)
465175164Sjhb				return (error);
466175164Sjhb			LIST_REMOVE(map, sm_link);
467175164Sjhb			shm_drop(map->sm_shmfd);
468175164Sjhb			free(map->sm_path, M_SHMFD);
469175164Sjhb			free(map, M_SHMFD);
470175164Sjhb			return (0);
471175164Sjhb		}
472175164Sjhb	}
473175164Sjhb
474175164Sjhb	return (ENOENT);
475175164Sjhb}
476175164Sjhb
477175164Sjhb/* System calls. */
478175164Sjhbint
479175164Sjhbshm_open(struct thread *td, struct shm_open_args *uap)
480175164Sjhb{
481175164Sjhb	struct filedesc *fdp;
482175164Sjhb	struct shmfd *shmfd;
483175164Sjhb	struct file *fp;
484175164Sjhb	char *path;
485175164Sjhb	Fnv32_t fnv;
486175164Sjhb	mode_t cmode;
487175164Sjhb	int fd, error;
488175164Sjhb
489175164Sjhb	if ((uap->flags & O_ACCMODE) != O_RDONLY &&
490175164Sjhb	    (uap->flags & O_ACCMODE) != O_RDWR)
491175164Sjhb		return (EINVAL);
492175164Sjhb
493175164Sjhb	if ((uap->flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) != 0)
494175164Sjhb		return (EINVAL);
495175164Sjhb
496175164Sjhb	fdp = td->td_proc->p_fd;
497175164Sjhb	cmode = (uap->mode & ~fdp->fd_cmask) & ACCESSPERMS;
498175164Sjhb
499220245Skib	error = falloc(td, &fp, &fd, 0);
500175164Sjhb	if (error)
501175164Sjhb		return (error);
502175164Sjhb
503175164Sjhb	/* A SHM_ANON path pointer creates an anonymous object. */
504175164Sjhb	if (uap->path == SHM_ANON) {
505175164Sjhb		/* A read-only anonymous object is pointless. */
506175164Sjhb		if ((uap->flags & O_ACCMODE) == O_RDONLY) {
507175164Sjhb			fdclose(fdp, fp, fd, td);
508175164Sjhb			fdrop(fp, td);
509175164Sjhb			return (EINVAL);
510175164Sjhb		}
511175164Sjhb		shmfd = shm_alloc(td->td_ucred, cmode);
512175164Sjhb	} else {
513175164Sjhb		path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK);
514175164Sjhb		error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
515175164Sjhb
516175164Sjhb		/* Require paths to start with a '/' character. */
517175164Sjhb		if (error == 0 && path[0] != '/')
518175164Sjhb			error = EINVAL;
519175164Sjhb		if (error) {
520175164Sjhb			fdclose(fdp, fp, fd, td);
521175164Sjhb			fdrop(fp, td);
522175164Sjhb			free(path, M_SHMFD);
523175164Sjhb			return (error);
524175164Sjhb		}
525175164Sjhb
526175164Sjhb		fnv = fnv_32_str(path, FNV1_32_INIT);
527175164Sjhb		sx_xlock(&shm_dict_lock);
528175164Sjhb		shmfd = shm_lookup(path, fnv);
529175164Sjhb		if (shmfd == NULL) {
530175164Sjhb			/* Object does not yet exist, create it if requested. */
531175164Sjhb			if (uap->flags & O_CREAT) {
532175164Sjhb				shmfd = shm_alloc(td->td_ucred, cmode);
533175164Sjhb				shm_insert(path, fnv, shmfd);
534175164Sjhb			} else {
535175164Sjhb				free(path, M_SHMFD);
536175164Sjhb				error = ENOENT;
537175164Sjhb			}
538175164Sjhb		} else {
539175164Sjhb			/*
540175164Sjhb			 * Object already exists, obtain a new
541175164Sjhb			 * reference if requested and permitted.
542175164Sjhb			 */
543175164Sjhb			free(path, M_SHMFD);
544175164Sjhb			if ((uap->flags & (O_CREAT | O_EXCL)) ==
545175164Sjhb			    (O_CREAT | O_EXCL))
546175164Sjhb				error = EEXIST;
547175164Sjhb			else {
548175164Sjhb#ifdef MAC
549175164Sjhb				error = mac_posixshm_check_open(td->td_ucred,
550175164Sjhb				    shmfd);
551175164Sjhb				if (error == 0)
552175164Sjhb#endif
553175164Sjhb				error = shm_access(shmfd, td->td_ucred,
554175164Sjhb				    FFLAGS(uap->flags & O_ACCMODE));
555175164Sjhb			}
556175164Sjhb
557175164Sjhb			/*
558175164Sjhb			 * Truncate the file back to zero length if
559175164Sjhb			 * O_TRUNC was specified and the object was
560175164Sjhb			 * opened with read/write.
561175164Sjhb			 */
562175164Sjhb			if (error == 0 &&
563175164Sjhb			    (uap->flags & (O_ACCMODE | O_TRUNC)) ==
564175164Sjhb			    (O_RDWR | O_TRUNC)) {
565175164Sjhb#ifdef MAC
566175164Sjhb				error = mac_posixshm_check_truncate(
567175164Sjhb					td->td_ucred, fp->f_cred, shmfd);
568175164Sjhb				if (error == 0)
569175164Sjhb#endif
570175164Sjhb					shm_dotruncate(shmfd, 0);
571175164Sjhb			}
572175164Sjhb			if (error == 0)
573175164Sjhb				shm_hold(shmfd);
574175164Sjhb		}
575175164Sjhb		sx_xunlock(&shm_dict_lock);
576175164Sjhb
577175164Sjhb		if (error) {
578175164Sjhb			fdclose(fdp, fp, fd, td);
579175164Sjhb			fdrop(fp, td);
580175164Sjhb			return (error);
581175164Sjhb		}
582175164Sjhb	}
583175164Sjhb
584175164Sjhb	finit(fp, FFLAGS(uap->flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops);
585175164Sjhb
586175164Sjhb	FILEDESC_XLOCK(fdp);
587175164Sjhb	if (fdp->fd_ofiles[fd] == fp)
588175164Sjhb		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
589175164Sjhb	FILEDESC_XUNLOCK(fdp);
590175164Sjhb	td->td_retval[0] = fd;
591175164Sjhb	fdrop(fp, td);
592175164Sjhb
593175164Sjhb	return (0);
594175164Sjhb}
595175164Sjhb
596175164Sjhbint
597175164Sjhbshm_unlink(struct thread *td, struct shm_unlink_args *uap)
598175164Sjhb{
599175164Sjhb	char *path;
600175164Sjhb	Fnv32_t fnv;
601175164Sjhb	int error;
602175164Sjhb
603175164Sjhb	path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
604175164Sjhb	error = copyinstr(uap->path, path, MAXPATHLEN, NULL);
605175164Sjhb	if (error) {
606175164Sjhb		free(path, M_TEMP);
607175164Sjhb		return (error);
608175164Sjhb	}
609175164Sjhb
610175164Sjhb	fnv = fnv_32_str(path, FNV1_32_INIT);
611175164Sjhb	sx_xlock(&shm_dict_lock);
612175164Sjhb	error = shm_remove(path, fnv, td->td_ucred);
613175164Sjhb	sx_xunlock(&shm_dict_lock);
614175164Sjhb	free(path, M_TEMP);
615175164Sjhb
616175164Sjhb	return (error);
617175164Sjhb}
618175164Sjhb
619175164Sjhb/*
620175164Sjhb * mmap() helper to validate mmap() requests against shm object state
621175164Sjhb * and give mmap() the vm_object to use for the mapping.
622175164Sjhb */
623175164Sjhbint
624175164Sjhbshm_mmap(struct shmfd *shmfd, vm_size_t objsize, vm_ooffset_t foff,
625175164Sjhb    vm_object_t *obj)
626175164Sjhb{
627175164Sjhb
628175164Sjhb	/*
629175164Sjhb	 * XXXRW: This validation is probably insufficient, and subject to
630175164Sjhb	 * sign errors.  It should be fixed.
631175164Sjhb	 */
632185533Skan	if (foff >= shmfd->shm_size ||
633185533Skan	    foff + objsize > round_page(shmfd->shm_size))
634175164Sjhb		return (EINVAL);
635175164Sjhb
636175164Sjhb	mtx_lock(&shm_timestamp_lock);
637175164Sjhb	vfs_timestamp(&shmfd->shm_atime);
638175164Sjhb	mtx_unlock(&shm_timestamp_lock);
639175164Sjhb	vm_object_reference(shmfd->shm_object);
640175164Sjhb	*obj = shmfd->shm_object;
641175164Sjhb	return (0);
642175164Sjhb}
643