1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/* $NetBSD: puffs_subr.c,v 1.67 2014/11/10 18:46:33 maxv Exp $ */
/*
* Copyright (c) 2006, 2007 Antti Kantee. All Rights Reserved.
*
* Development of this software was supported by the
* Ulla Tuominen Foundation and the Finnish Cultural Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: puffs_subr.c,v 1.67 2014/11/10 18:46:33 maxv Exp $");
#include <sys/param.h>
#include <sys/buf.h>
#include <sys/mount.h>
#include <sys/namei.h>
#include <sys/poll.h>
#include <sys/proc.h>
#include <fs/puffs/puffs_msgif.h>
#include <fs/puffs/puffs_sys.h>
#ifdef PUFFSDEBUG
int puffsdebug;
#endif
void
puffs_makecn(struct puffs_kcn *pkcn, struct puffs_kcred *pkcr,
const struct componentname *cn, int full)
{
pkcn->pkcn_nameiop = cn->cn_nameiop;
pkcn->pkcn_flags = cn->cn_flags;
if (full) {
(void)strcpy(pkcn->pkcn_name, cn->cn_nameptr);
} else {
(void)memcpy(pkcn->pkcn_name, cn->cn_nameptr, cn->cn_namelen);
pkcn->pkcn_name[cn->cn_namelen] = '\0';
}
pkcn->pkcn_namelen = cn->cn_namelen;
pkcn->pkcn_consume = 0;
puffs_credcvt(pkcr, cn->cn_cred);
}
/*
* Convert given credentials to struct puffs_kcred for userspace.
*/
void
puffs_credcvt(struct puffs_kcred *pkcr, const kauth_cred_t cred)
{
memset(pkcr, 0, sizeof(struct puffs_kcred));
if (cred == NOCRED || cred == FSCRED) {
pkcr->pkcr_type = PUFFCRED_TYPE_INTERNAL;
if (cred == NOCRED)
pkcr->pkcr_internal = PUFFCRED_CRED_NOCRED;
if (cred == FSCRED)
pkcr->pkcr_internal = PUFFCRED_CRED_FSCRED;
} else {
pkcr->pkcr_type = PUFFCRED_TYPE_UUC;
kauth_cred_to_uucred(&pkcr->pkcr_uuc, cred);
}
}
void
puffs_parkdone_asyncbioread(struct puffs_mount *pmp,
struct puffs_req *preq, void *arg)
{
struct puffs_vnmsg_read *read_msg = (void *)preq;
struct buf *bp = arg;
size_t moved;
DPRINTF(("%s\n", __func__));
bp->b_error = checkerr(pmp, preq->preq_rv, __func__);
if (bp->b_error == 0) {
if (read_msg->pvnr_resid > bp->b_bcount) {
puffs_senderr(pmp, PUFFS_ERR_READ, E2BIG,
"resid grew", preq->preq_cookie);
bp->b_error = E2BIG;
} else {
moved = bp->b_bcount - read_msg->pvnr_resid;
bp->b_resid = read_msg->pvnr_resid;
memcpy(bp->b_data, read_msg->pvnr_data, moved);
}
}
biodone(bp);
}
void
puffs_parkdone_asyncbiowrite(struct puffs_mount *pmp,
struct puffs_req *preq, void *arg)
{
struct puffs_vnmsg_write *write_msg = (void *)preq;
struct buf *bp = arg;
DPRINTF(("%s\n", __func__));
bp->b_error = checkerr(pmp, preq->preq_rv, __func__);
if (bp->b_error == 0) {
if (write_msg->pvnr_resid > bp->b_bcount) {
puffs_senderr(pmp, PUFFS_ERR_WRITE, E2BIG,
"resid grew", preq->preq_cookie);
bp->b_error = E2BIG;
} else {
bp->b_resid = write_msg->pvnr_resid;
}
}
biodone(bp);
}
/* XXX: userspace can leak kernel resources */
void
puffs_parkdone_poll(struct puffs_mount *pmp, struct puffs_req *preq, void *arg)
{
struct puffs_vnmsg_poll *poll_msg = (void *)preq;
struct puffs_node *pn = arg;
int revents, error;
error = checkerr(pmp, preq->preq_rv, __func__);
if (error)
revents = poll_msg->pvnr_events;
else
revents = POLLERR;
mutex_enter(&pn->pn_mtx);
pn->pn_revents |= revents;
mutex_exit(&pn->pn_mtx);
selnotify(&pn->pn_sel, revents, 0);
puffs_releasenode(pn);
}
void
puffs_mp_reference(struct puffs_mount *pmp)
{
KASSERT(mutex_owned(&pmp->pmp_lock));
pmp->pmp_refcount++;
}
void
puffs_mp_release(struct puffs_mount *pmp)
{
KASSERT(mutex_owned(&pmp->pmp_lock));
if (--pmp->pmp_refcount == 0)
cv_broadcast(&pmp->pmp_refcount_cv);
}
void
puffs_gop_size(struct vnode *vp, off_t size, off_t *eobp,
int flags)
{
*eobp = size;
}
void
puffs_gop_markupdate(struct vnode *vp, int flags)
{
int uflags = 0;
if (flags & GOP_UPDATE_ACCESSED)
uflags |= PUFFS_UPDATEATIME;
if (flags & GOP_UPDATE_MODIFIED)
uflags |= PUFFS_UPDATEMTIME;
puffs_updatenode(VPTOPP(vp), uflags, 0);
}
void
puffs_senderr(struct puffs_mount *pmp, int type, int error,
const char *str, puffs_cookie_t ck)
{
struct puffs_msgpark *park;
struct puffs_error *perr;
puffs_msgmem_alloc(sizeof(struct puffs_error), &park, (void *)&perr, 1);
puffs_msg_setfaf(park);
puffs_msg_setinfo(park, PUFFSOP_ERROR, type, ck);
perr->perr_error = error;
strlcpy(perr->perr_str, str, sizeof(perr->perr_str));
puffs_msg_enqueue(pmp, park);
puffs_msgmem_release(park);
}
|