subr_kobj_vfs.c revision 1.7
1/* $NetBSD: subr_kobj_vfs.c,v 1.7 2013/09/15 14:08:25 martin Exp $ */ 2 3/*- 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software developed for The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32/*- 33 * Copyright (c) 1998-2000 Doug Rabson 34 * Copyright (c) 2004 Peter Wemm 35 * All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 49 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 56 * SUCH DAMAGE. 57 */ 58 59/* 60 * Kernel loader vfs routines. 61 */ 62 63#include <sys/kobj_impl.h> 64#include "opt_modular.h" 65 66#ifdef MODULAR 67 68#include <sys/param.h> 69#include <sys/fcntl.h> 70#include <sys/module.h> 71#include <sys/namei.h> 72#include <sys/vnode.h> 73 74#include <sys/cdefs.h> 75__KERNEL_RCSID(0, "$NetBSD: subr_kobj_vfs.c,v 1.7 2013/09/15 14:08:25 martin Exp $"); 76 77static void 78kobj_close_vfs(kobj_t ko) 79{ 80 81 VOP_UNLOCK(ko->ko_source); 82 vn_close(ko->ko_source, FREAD, kauth_cred_get()); 83} 84 85/* 86 * kobj_read: 87 * 88 * Utility function: read from the object. 89 */ 90static int 91kobj_read_vfs(kobj_t ko, void **basep, size_t size, off_t off, 92 bool allocate) 93{ 94 size_t resid; 95 void *base; 96 int error; 97 98 KASSERT(ko->ko_source != NULL); 99 100 if (allocate) { 101 base = kmem_alloc(size, KM_SLEEP); 102 } else { 103 base = *basep; 104 KASSERT((uintptr_t)base >= (uintptr_t)ko->ko_address); 105 KASSERT((uintptr_t)base + size <= 106 (uintptr_t)ko->ko_address + ko->ko_size); 107 } 108 109 error = vn_rdwr(UIO_READ, ko->ko_source, base, size, off, 110 UIO_SYSSPACE, IO_NODELOCKED, curlwp->l_cred, &resid, 111 curlwp); 112 113 if (error == 0 && resid != 0) { 114 error = EINVAL; 115 } 116 117 if (allocate && error != 0) { 118 kmem_free(base, size); 119 base = NULL; 120 } 121 122 if (allocate) 123 *basep = base; 124 125 return error; 126} 127 128/* 129 * kobj_load_vfs: 130 * 131 * Load an object located in the file system. 132 */ 133int 134kobj_load_vfs(kobj_t *kop, const char *path, const bool nochroot) 135{ 136 struct nameidata nd; 137 struct pathbuf *pb; 138 int error; 139 kobj_t ko; 140 141 KASSERT(path != NULL); 142 if (strchr(path, '/') == NULL) 143 return ENOENT; 144 145 ko = kmem_zalloc(sizeof(*ko), KM_SLEEP); 146 if (ko == NULL) { 147 return ENOMEM; 148 } 149 150 pb = pathbuf_create(path); 151 if (pb == NULL) { 152 kmem_free(ko, sizeof(*ko)); 153 return ENOMEM; 154 } 155 156 NDINIT(&nd, LOOKUP, FOLLOW | (nochroot ? NOCHROOT : 0), pb); 157 error = vn_open(&nd, FREAD, 0); 158 159 if (error != 0) { 160 pathbuf_destroy(pb); 161 kmem_free(ko, sizeof(*ko)); 162 return error; 163 } 164 165 ko->ko_type = KT_VNODE; 166 kobj_setname(ko, path); 167 ko->ko_source = nd.ni_vp; 168 ko->ko_read = kobj_read_vfs; 169 ko->ko_close = kobj_close_vfs; 170 pathbuf_destroy(pb); 171 172 *kop = ko; 173 return kobj_load(ko); 174} 175 176#else /* MODULAR */ 177 178int 179kobj_load_vfs(kobj_t *kop, const char *path, const bool nochroot) 180{ 181 182 return ENOSYS; 183} 184 185#endif 186