tmpfs_vnops.c revision 1.51.6.5 1 1.51.6.5 snj /* $NetBSD: tmpfs_vnops.c,v 1.51.6.5 2009/04/19 15:27:32 snj Exp $ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.45 ad * Copyright (c) 2005, 2006, 2007 The NetBSD Foundation, Inc.
5 1.1 jmmv * All rights reserved.
6 1.1 jmmv *
7 1.1 jmmv * This code is derived from software contributed to The NetBSD Foundation
8 1.12 jmmv * by Julio M. Merino Vidal, developed as part of Google's Summer of Code
9 1.12 jmmv * 2005 program.
10 1.1 jmmv *
11 1.1 jmmv * Redistribution and use in source and binary forms, with or without
12 1.1 jmmv * modification, are permitted provided that the following conditions
13 1.1 jmmv * are met:
14 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
15 1.1 jmmv * notice, this list of conditions and the following disclaimer.
16 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 jmmv * notice, this list of conditions and the following disclaimer in the
18 1.1 jmmv * documentation and/or other materials provided with the distribution.
19 1.1 jmmv *
20 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 jmmv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 jmmv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 jmmv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 jmmv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 jmmv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 jmmv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 jmmv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 jmmv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 jmmv * POSSIBILITY OF SUCH DAMAGE.
31 1.1 jmmv */
32 1.1 jmmv
33 1.1 jmmv /*
34 1.1 jmmv * tmpfs vnode interface.
35 1.1 jmmv */
36 1.1 jmmv
37 1.1 jmmv #include <sys/cdefs.h>
38 1.51.6.5 snj __KERNEL_RCSID(0, "$NetBSD: tmpfs_vnops.c,v 1.51.6.5 2009/04/19 15:27:32 snj Exp $");
39 1.1 jmmv
40 1.1 jmmv #include <sys/param.h>
41 1.1 jmmv #include <sys/dirent.h>
42 1.1 jmmv #include <sys/fcntl.h>
43 1.1 jmmv #include <sys/event.h>
44 1.1 jmmv #include <sys/malloc.h>
45 1.1 jmmv #include <sys/namei.h>
46 1.1 jmmv #include <sys/proc.h>
47 1.1 jmmv #include <sys/stat.h>
48 1.1 jmmv #include <sys/uio.h>
49 1.1 jmmv #include <sys/unistd.h>
50 1.1 jmmv #include <sys/vnode.h>
51 1.15 jmmv #include <sys/lockf.h>
52 1.24 christos #include <sys/kauth.h>
53 1.1 jmmv
54 1.1 jmmv #include <uvm/uvm.h>
55 1.1 jmmv
56 1.1 jmmv #include <miscfs/fifofs/fifo.h>
57 1.1 jmmv #include <fs/tmpfs/tmpfs_vnops.h>
58 1.1 jmmv #include <fs/tmpfs/tmpfs.h>
59 1.1 jmmv
60 1.1 jmmv /* --------------------------------------------------------------------- */
61 1.1 jmmv
62 1.1 jmmv /*
63 1.2 jmmv * vnode operations vector used for files stored in a tmpfs file system.
64 1.1 jmmv */
65 1.1 jmmv int (**tmpfs_vnodeop_p)(void *);
66 1.1 jmmv const struct vnodeopv_entry_desc tmpfs_vnodeop_entries[] = {
67 1.1 jmmv { &vop_default_desc, vn_default_error },
68 1.1 jmmv { &vop_lookup_desc, tmpfs_lookup },
69 1.1 jmmv { &vop_create_desc, tmpfs_create },
70 1.1 jmmv { &vop_mknod_desc, tmpfs_mknod },
71 1.1 jmmv { &vop_open_desc, tmpfs_open },
72 1.1 jmmv { &vop_close_desc, tmpfs_close },
73 1.1 jmmv { &vop_access_desc, tmpfs_access },
74 1.1 jmmv { &vop_getattr_desc, tmpfs_getattr },
75 1.1 jmmv { &vop_setattr_desc, tmpfs_setattr },
76 1.1 jmmv { &vop_read_desc, tmpfs_read },
77 1.1 jmmv { &vop_write_desc, tmpfs_write },
78 1.1 jmmv { &vop_ioctl_desc, tmpfs_ioctl },
79 1.1 jmmv { &vop_fcntl_desc, tmpfs_fcntl },
80 1.1 jmmv { &vop_poll_desc, tmpfs_poll },
81 1.1 jmmv { &vop_kqfilter_desc, tmpfs_kqfilter },
82 1.1 jmmv { &vop_revoke_desc, tmpfs_revoke },
83 1.1 jmmv { &vop_mmap_desc, tmpfs_mmap },
84 1.1 jmmv { &vop_fsync_desc, tmpfs_fsync },
85 1.1 jmmv { &vop_seek_desc, tmpfs_seek },
86 1.1 jmmv { &vop_remove_desc, tmpfs_remove },
87 1.1 jmmv { &vop_link_desc, tmpfs_link },
88 1.1 jmmv { &vop_rename_desc, tmpfs_rename },
89 1.1 jmmv { &vop_mkdir_desc, tmpfs_mkdir },
90 1.1 jmmv { &vop_rmdir_desc, tmpfs_rmdir },
91 1.1 jmmv { &vop_symlink_desc, tmpfs_symlink },
92 1.1 jmmv { &vop_readdir_desc, tmpfs_readdir },
93 1.1 jmmv { &vop_readlink_desc, tmpfs_readlink },
94 1.1 jmmv { &vop_abortop_desc, tmpfs_abortop },
95 1.1 jmmv { &vop_inactive_desc, tmpfs_inactive },
96 1.1 jmmv { &vop_reclaim_desc, tmpfs_reclaim },
97 1.1 jmmv { &vop_lock_desc, tmpfs_lock },
98 1.1 jmmv { &vop_unlock_desc, tmpfs_unlock },
99 1.1 jmmv { &vop_bmap_desc, tmpfs_bmap },
100 1.1 jmmv { &vop_strategy_desc, tmpfs_strategy },
101 1.1 jmmv { &vop_print_desc, tmpfs_print },
102 1.1 jmmv { &vop_pathconf_desc, tmpfs_pathconf },
103 1.1 jmmv { &vop_islocked_desc, tmpfs_islocked },
104 1.1 jmmv { &vop_advlock_desc, tmpfs_advlock },
105 1.1 jmmv { &vop_bwrite_desc, tmpfs_bwrite },
106 1.1 jmmv { &vop_getpages_desc, tmpfs_getpages },
107 1.1 jmmv { &vop_putpages_desc, tmpfs_putpages },
108 1.1 jmmv { NULL, NULL }
109 1.1 jmmv };
110 1.1 jmmv const struct vnodeopv_desc tmpfs_vnodeop_opv_desc =
111 1.1 jmmv { &tmpfs_vnodeop_p, tmpfs_vnodeop_entries };
112 1.1 jmmv
113 1.1 jmmv /* --------------------------------------------------------------------- */
114 1.1 jmmv
115 1.1 jmmv int
116 1.1 jmmv tmpfs_lookup(void *v)
117 1.1 jmmv {
118 1.1 jmmv struct vnode *dvp = ((struct vop_lookup_args *)v)->a_dvp;
119 1.1 jmmv struct vnode **vpp = ((struct vop_lookup_args *)v)->a_vpp;
120 1.1 jmmv struct componentname *cnp = ((struct vop_lookup_args *)v)->a_cnp;
121 1.1 jmmv
122 1.1 jmmv int error;
123 1.1 jmmv struct tmpfs_dirent *de;
124 1.1 jmmv struct tmpfs_node *dnode;
125 1.1 jmmv
126 1.1 jmmv KASSERT(VOP_ISLOCKED(dvp));
127 1.1 jmmv
128 1.1 jmmv dnode = VP_TO_TMPFS_DIR(dvp);
129 1.1 jmmv *vpp = NULL;
130 1.1 jmmv
131 1.1 jmmv /* Check accessibility of requested node as a first step. */
132 1.44 pooka error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
133 1.1 jmmv if (error != 0)
134 1.1 jmmv goto out;
135 1.1 jmmv
136 1.2 jmmv /* If requesting the last path component on a read-only file system
137 1.1 jmmv * with a write operation, deny it. */
138 1.1 jmmv if ((cnp->cn_flags & ISLASTCN) &&
139 1.1 jmmv (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
140 1.1 jmmv (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
141 1.1 jmmv error = EROFS;
142 1.1 jmmv goto out;
143 1.1 jmmv }
144 1.1 jmmv
145 1.1 jmmv /* Avoid doing a linear scan of the directory if the requested
146 1.1 jmmv * directory/name couple is already in the cache. */
147 1.1 jmmv error = cache_lookup(dvp, vpp, cnp);
148 1.1 jmmv if (error >= 0)
149 1.1 jmmv goto out;
150 1.1 jmmv
151 1.1 jmmv /* We cannot be requesting the parent directory of the root node. */
152 1.1 jmmv KASSERT(IMPLIES(dnode->tn_type == VDIR &&
153 1.21 jmmv dnode->tn_spec.tn_dir.tn_parent == dnode,
154 1.21 jmmv !(cnp->cn_flags & ISDOTDOT)));
155 1.1 jmmv
156 1.1 jmmv if (cnp->cn_flags & ISDOTDOT) {
157 1.1 jmmv VOP_UNLOCK(dvp, 0);
158 1.1 jmmv
159 1.1 jmmv /* Allocate a new vnode on the matching entry. */
160 1.21 jmmv error = tmpfs_alloc_vp(dvp->v_mount,
161 1.21 jmmv dnode->tn_spec.tn_dir.tn_parent, vpp);
162 1.1 jmmv
163 1.33 chs vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
164 1.1 jmmv } else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
165 1.1 jmmv VREF(dvp);
166 1.1 jmmv *vpp = dvp;
167 1.1 jmmv error = 0;
168 1.1 jmmv } else {
169 1.1 jmmv de = tmpfs_dir_lookup(dnode, cnp);
170 1.1 jmmv if (de == NULL) {
171 1.1 jmmv /* The entry was not found in the directory.
172 1.1 jmmv * This is OK iff we are creating or renaming an
173 1.1 jmmv * entry and are working on the last component of
174 1.1 jmmv * the path name. */
175 1.1 jmmv if ((cnp->cn_flags & ISLASTCN) &&
176 1.1 jmmv (cnp->cn_nameiop == CREATE || \
177 1.1 jmmv cnp->cn_nameiop == RENAME)) {
178 1.44 pooka error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
179 1.1 jmmv if (error != 0)
180 1.1 jmmv goto out;
181 1.1 jmmv
182 1.1 jmmv /* Keep the component name in the buffer for
183 1.1 jmmv * future uses. */
184 1.1 jmmv cnp->cn_flags |= SAVENAME;
185 1.1 jmmv
186 1.1 jmmv error = EJUSTRETURN;
187 1.1 jmmv } else
188 1.1 jmmv error = ENOENT;
189 1.1 jmmv } else {
190 1.1 jmmv struct tmpfs_node *tnode;
191 1.1 jmmv
192 1.1 jmmv /* The entry was found, so get its associated
193 1.1 jmmv * tmpfs_node. */
194 1.1 jmmv tnode = de->td_node;
195 1.1 jmmv
196 1.1 jmmv /* If we are not at the last path component and
197 1.16 jmmv * found a non-directory or non-link entry (which
198 1.16 jmmv * may itself be pointing to a directory), raise
199 1.16 jmmv * an error. */
200 1.16 jmmv if ((tnode->tn_type != VDIR &&
201 1.16 jmmv tnode->tn_type != VLNK) &&
202 1.1 jmmv !(cnp->cn_flags & ISLASTCN)) {
203 1.1 jmmv error = ENOTDIR;
204 1.1 jmmv goto out;
205 1.1 jmmv }
206 1.1 jmmv
207 1.1 jmmv /* If we are deleting or renaming the entry, keep
208 1.1 jmmv * track of its tmpfs_dirent so that it can be
209 1.1 jmmv * easily deleted later. */
210 1.1 jmmv if ((cnp->cn_flags & ISLASTCN) &&
211 1.1 jmmv (cnp->cn_nameiop == DELETE ||
212 1.1 jmmv cnp->cn_nameiop == RENAME)) {
213 1.22 christos if ((dnode->tn_mode & S_ISTXT) != 0 &&
214 1.35 elad kauth_authorize_generic(cnp->cn_cred,
215 1.35 elad KAUTH_GENERIC_ISSUSER, NULL) != 0 &&
216 1.23 elad kauth_cred_geteuid(cnp->cn_cred) != dnode->tn_uid &&
217 1.23 elad kauth_cred_geteuid(cnp->cn_cred) != tnode->tn_uid)
218 1.22 christos return EPERM;
219 1.44 pooka error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
220 1.1 jmmv if (error != 0)
221 1.1 jmmv goto out;
222 1.51.6.4 snj cnp->cn_flags |= SAVENAME;
223 1.45 ad } else
224 1.45 ad de = NULL;
225 1.1 jmmv
226 1.1 jmmv /* Allocate a new vnode on the matching entry. */
227 1.1 jmmv error = tmpfs_alloc_vp(dvp->v_mount, tnode, vpp);
228 1.1 jmmv }
229 1.1 jmmv }
230 1.1 jmmv
231 1.1 jmmv /* Store the result of this lookup in the cache. Avoid this if the
232 1.1 jmmv * request was for creation, as it does not improve timings on
233 1.1 jmmv * emprical tests. */
234 1.45 ad if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE &&
235 1.45 ad (cnp->cn_flags & ISDOTDOT) == 0)
236 1.1 jmmv cache_enter(dvp, *vpp, cnp);
237 1.1 jmmv
238 1.1 jmmv out:
239 1.1 jmmv /* If there were no errors, *vpp cannot be null and it must be
240 1.1 jmmv * locked. */
241 1.1 jmmv KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
242 1.1 jmmv
243 1.33 chs /* dvp must always be locked. */
244 1.33 chs KASSERT(VOP_ISLOCKED(dvp));
245 1.1 jmmv
246 1.1 jmmv return error;
247 1.1 jmmv }
248 1.1 jmmv
249 1.1 jmmv /* --------------------------------------------------------------------- */
250 1.1 jmmv
251 1.1 jmmv int
252 1.1 jmmv tmpfs_create(void *v)
253 1.1 jmmv {
254 1.1 jmmv struct vnode *dvp = ((struct vop_create_args *)v)->a_dvp;
255 1.1 jmmv struct vnode **vpp = ((struct vop_create_args *)v)->a_vpp;
256 1.1 jmmv struct componentname *cnp = ((struct vop_create_args *)v)->a_cnp;
257 1.1 jmmv struct vattr *vap = ((struct vop_create_args *)v)->a_vap;
258 1.1 jmmv
259 1.1 jmmv KASSERT(vap->va_type == VREG || vap->va_type == VSOCK);
260 1.1 jmmv
261 1.1 jmmv return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
262 1.1 jmmv }
263 1.1 jmmv /* --------------------------------------------------------------------- */
264 1.1 jmmv
265 1.1 jmmv int
266 1.1 jmmv tmpfs_mknod(void *v)
267 1.1 jmmv {
268 1.1 jmmv struct vnode *dvp = ((struct vop_mknod_args *)v)->a_dvp;
269 1.1 jmmv struct vnode **vpp = ((struct vop_mknod_args *)v)->a_vpp;
270 1.1 jmmv struct componentname *cnp = ((struct vop_mknod_args *)v)->a_cnp;
271 1.1 jmmv struct vattr *vap = ((struct vop_mknod_args *)v)->a_vap;
272 1.1 jmmv
273 1.1 jmmv if (vap->va_type != VBLK && vap->va_type != VCHR &&
274 1.51.6.2 snj vap->va_type != VFIFO) {
275 1.51.6.2 snj vput(dvp);
276 1.1 jmmv return EINVAL;
277 1.51.6.2 snj }
278 1.1 jmmv
279 1.1 jmmv return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
280 1.1 jmmv }
281 1.1 jmmv
282 1.1 jmmv /* --------------------------------------------------------------------- */
283 1.1 jmmv
284 1.1 jmmv int
285 1.1 jmmv tmpfs_open(void *v)
286 1.1 jmmv {
287 1.1 jmmv struct vnode *vp = ((struct vop_open_args *)v)->a_vp;
288 1.1 jmmv int mode = ((struct vop_open_args *)v)->a_mode;
289 1.1 jmmv
290 1.1 jmmv int error;
291 1.1 jmmv struct tmpfs_node *node;
292 1.1 jmmv
293 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
294 1.1 jmmv
295 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
296 1.1 jmmv
297 1.32 jmmv /* The file is still active but all its names have been removed
298 1.32 jmmv * (e.g. by a "rmdir $(pwd)"). It cannot be opened any more as
299 1.32 jmmv * it is about to die. */
300 1.32 jmmv if (node->tn_links < 1) {
301 1.32 jmmv error = ENOENT;
302 1.32 jmmv goto out;
303 1.32 jmmv }
304 1.32 jmmv
305 1.1 jmmv /* If the file is marked append-only, deny write requests. */
306 1.1 jmmv if (node->tn_flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
307 1.1 jmmv error = EPERM;
308 1.1 jmmv else
309 1.1 jmmv error = 0;
310 1.1 jmmv
311 1.32 jmmv out:
312 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
313 1.1 jmmv
314 1.1 jmmv return error;
315 1.1 jmmv }
316 1.1 jmmv
317 1.1 jmmv /* --------------------------------------------------------------------- */
318 1.1 jmmv
319 1.1 jmmv int
320 1.1 jmmv tmpfs_close(void *v)
321 1.1 jmmv {
322 1.1 jmmv struct vnode *vp = ((struct vop_close_args *)v)->a_vp;
323 1.1 jmmv
324 1.1 jmmv struct tmpfs_node *node;
325 1.1 jmmv
326 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
327 1.1 jmmv
328 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
329 1.1 jmmv
330 1.1 jmmv if (node->tn_links > 0) {
331 1.1 jmmv /* Update node times. No need to do it if the node has
332 1.1 jmmv * been deleted, because it will vanish after we return. */
333 1.51 christos tmpfs_update(vp, NULL, NULL, NULL, UPDATE_CLOSE);
334 1.17 yamt }
335 1.1 jmmv
336 1.17 yamt return 0;
337 1.1 jmmv }
338 1.1 jmmv
339 1.1 jmmv /* --------------------------------------------------------------------- */
340 1.1 jmmv
341 1.1 jmmv int
342 1.1 jmmv tmpfs_access(void *v)
343 1.1 jmmv {
344 1.1 jmmv struct vnode *vp = ((struct vop_access_args *)v)->a_vp;
345 1.1 jmmv int mode = ((struct vop_access_args *)v)->a_mode;
346 1.23 elad kauth_cred_t cred = ((struct vop_access_args *)v)->a_cred;
347 1.1 jmmv
348 1.1 jmmv int error;
349 1.1 jmmv struct tmpfs_node *node;
350 1.1 jmmv
351 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
352 1.1 jmmv
353 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
354 1.1 jmmv
355 1.1 jmmv switch (vp->v_type) {
356 1.1 jmmv case VDIR:
357 1.1 jmmv /* FALLTHROUGH */
358 1.1 jmmv case VLNK:
359 1.1 jmmv /* FALLTHROUGH */
360 1.1 jmmv case VREG:
361 1.1 jmmv if (mode & VWRITE && vp->v_mount->mnt_flag & MNT_RDONLY) {
362 1.1 jmmv error = EROFS;
363 1.1 jmmv goto out;
364 1.1 jmmv }
365 1.1 jmmv break;
366 1.1 jmmv
367 1.1 jmmv case VBLK:
368 1.1 jmmv /* FALLTHROUGH */
369 1.1 jmmv case VCHR:
370 1.1 jmmv /* FALLTHROUGH */
371 1.1 jmmv case VSOCK:
372 1.1 jmmv /* FALLTHROUGH */
373 1.1 jmmv case VFIFO:
374 1.1 jmmv break;
375 1.1 jmmv
376 1.1 jmmv default:
377 1.1 jmmv error = EINVAL;
378 1.1 jmmv goto out;
379 1.1 jmmv }
380 1.1 jmmv
381 1.1 jmmv if (mode & VWRITE && node->tn_flags & IMMUTABLE) {
382 1.1 jmmv error = EPERM;
383 1.1 jmmv goto out;
384 1.1 jmmv }
385 1.1 jmmv
386 1.1 jmmv error = vaccess(vp->v_type, node->tn_mode, node->tn_uid,
387 1.1 jmmv node->tn_gid, mode, cred);
388 1.1 jmmv
389 1.1 jmmv out:
390 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
391 1.1 jmmv
392 1.1 jmmv return error;
393 1.1 jmmv }
394 1.1 jmmv
395 1.1 jmmv /* --------------------------------------------------------------------- */
396 1.1 jmmv
397 1.1 jmmv int
398 1.1 jmmv tmpfs_getattr(void *v)
399 1.1 jmmv {
400 1.1 jmmv struct vnode *vp = ((struct vop_getattr_args *)v)->a_vp;
401 1.1 jmmv struct vattr *vap = ((struct vop_getattr_args *)v)->a_vap;
402 1.1 jmmv
403 1.1 jmmv struct tmpfs_node *node;
404 1.1 jmmv
405 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
406 1.1 jmmv
407 1.1 jmmv VATTR_NULL(vap);
408 1.1 jmmv
409 1.51 christos tmpfs_itimes(vp, NULL, NULL, NULL);
410 1.14 yamt
411 1.1 jmmv vap->va_type = vp->v_type;
412 1.1 jmmv vap->va_mode = node->tn_mode;
413 1.1 jmmv vap->va_nlink = node->tn_links;
414 1.1 jmmv vap->va_uid = node->tn_uid;
415 1.1 jmmv vap->va_gid = node->tn_gid;
416 1.1 jmmv vap->va_fsid = vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
417 1.1 jmmv vap->va_fileid = node->tn_id;
418 1.1 jmmv vap->va_size = node->tn_size;
419 1.1 jmmv vap->va_blocksize = PAGE_SIZE;
420 1.1 jmmv vap->va_atime = node->tn_atime;
421 1.1 jmmv vap->va_mtime = node->tn_mtime;
422 1.1 jmmv vap->va_ctime = node->tn_ctime;
423 1.1 jmmv vap->va_birthtime = node->tn_birthtime;
424 1.1 jmmv vap->va_gen = node->tn_gen;
425 1.1 jmmv vap->va_flags = node->tn_flags;
426 1.1 jmmv vap->va_rdev = (vp->v_type == VBLK || vp->v_type == VCHR) ?
427 1.21 jmmv node->tn_spec.tn_dev.tn_rdev : VNOVAL;
428 1.1 jmmv vap->va_bytes = round_page(node->tn_size);
429 1.1 jmmv vap->va_filerev = VNOVAL;
430 1.1 jmmv vap->va_vaflags = 0;
431 1.1 jmmv vap->va_spare = VNOVAL; /* XXX */
432 1.1 jmmv
433 1.1 jmmv return 0;
434 1.1 jmmv }
435 1.1 jmmv
436 1.1 jmmv /* --------------------------------------------------------------------- */
437 1.1 jmmv
438 1.51 christos #define GOODTIME(tv) ((tv)->tv_sec != VNOVAL || (tv)->tv_nsec != VNOVAL)
439 1.1 jmmv /* XXX Should this operation be atomic? I think it should, but code in
440 1.1 jmmv * XXX other places (e.g., ufs) doesn't seem to be... */
441 1.1 jmmv int
442 1.1 jmmv tmpfs_setattr(void *v)
443 1.1 jmmv {
444 1.1 jmmv struct vnode *vp = ((struct vop_setattr_args *)v)->a_vp;
445 1.1 jmmv struct vattr *vap = ((struct vop_setattr_args *)v)->a_vap;
446 1.23 elad kauth_cred_t cred = ((struct vop_setattr_args *)v)->a_cred;
447 1.44 pooka struct lwp *l = curlwp;
448 1.1 jmmv
449 1.17 yamt int error;
450 1.1 jmmv
451 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
452 1.1 jmmv
453 1.1 jmmv error = 0;
454 1.1 jmmv
455 1.1 jmmv /* Abort if any unsettable attribute is given. */
456 1.1 jmmv if (vap->va_type != VNON ||
457 1.1 jmmv vap->va_nlink != VNOVAL ||
458 1.1 jmmv vap->va_fsid != VNOVAL ||
459 1.1 jmmv vap->va_fileid != VNOVAL ||
460 1.1 jmmv vap->va_blocksize != VNOVAL ||
461 1.51 christos GOODTIME(&vap->va_ctime) ||
462 1.1 jmmv vap->va_gen != VNOVAL ||
463 1.1 jmmv vap->va_rdev != VNOVAL ||
464 1.1 jmmv vap->va_bytes != VNOVAL)
465 1.1 jmmv error = EINVAL;
466 1.1 jmmv
467 1.1 jmmv if (error == 0 && (vap->va_flags != VNOVAL))
468 1.25 ad error = tmpfs_chflags(vp, vap->va_flags, cred, l);
469 1.1 jmmv
470 1.1 jmmv if (error == 0 && (vap->va_size != VNOVAL))
471 1.25 ad error = tmpfs_chsize(vp, vap->va_size, cred, l);
472 1.1 jmmv
473 1.1 jmmv if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
474 1.25 ad error = tmpfs_chown(vp, vap->va_uid, vap->va_gid, cred, l);
475 1.1 jmmv
476 1.1 jmmv if (error == 0 && (vap->va_mode != VNOVAL))
477 1.25 ad error = tmpfs_chmod(vp, vap->va_mode, cred, l);
478 1.1 jmmv
479 1.51 christos if (error == 0 && (GOODTIME(&vap->va_atime) || GOODTIME(&vap->va_mtime)
480 1.51 christos || GOODTIME(&vap->va_birthtime)))
481 1.51 christos if ((error = tmpfs_chtimes(vp, &vap->va_atime, &vap->va_mtime,
482 1.51 christos &vap->va_birthtime, vap->va_vaflags, cred, l)) == 0)
483 1.51 christos return 0;
484 1.1 jmmv
485 1.1 jmmv /* Update the node times. We give preference to the error codes
486 1.1 jmmv * generated by this function rather than the ones that may arise
487 1.1 jmmv * from tmpfs_update. */
488 1.51 christos tmpfs_update(vp, NULL, NULL, NULL, 0);
489 1.1 jmmv
490 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
491 1.1 jmmv
492 1.1 jmmv return error;
493 1.1 jmmv }
494 1.1 jmmv
495 1.1 jmmv /* --------------------------------------------------------------------- */
496 1.1 jmmv
497 1.1 jmmv int
498 1.1 jmmv tmpfs_read(void *v)
499 1.1 jmmv {
500 1.1 jmmv struct vnode *vp = ((struct vop_read_args *)v)->a_vp;
501 1.1 jmmv struct uio *uio = ((struct vop_read_args *)v)->a_uio;
502 1.1 jmmv
503 1.7 jmmv int error;
504 1.7 jmmv int flags;
505 1.6 yamt struct tmpfs_node *node;
506 1.6 yamt struct uvm_object *uobj;
507 1.1 jmmv
508 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
509 1.1 jmmv
510 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
511 1.1 jmmv
512 1.5 yamt if (vp->v_type != VREG) {
513 1.5 yamt error = EISDIR;
514 1.5 yamt goto out;
515 1.5 yamt }
516 1.5 yamt
517 1.5 yamt if (uio->uio_offset < 0) {
518 1.1 jmmv error = EINVAL;
519 1.1 jmmv goto out;
520 1.1 jmmv }
521 1.1 jmmv
522 1.1 jmmv node->tn_status |= TMPFS_NODE_ACCESSED;
523 1.1 jmmv
524 1.21 jmmv uobj = node->tn_spec.tn_reg.tn_aobj;
525 1.6 yamt flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
526 1.6 yamt error = 0;
527 1.7 jmmv while (error == 0 && uio->uio_resid > 0) {
528 1.6 yamt vsize_t len;
529 1.6 yamt void *win;
530 1.6 yamt
531 1.8 yamt if (node->tn_size <= uio->uio_offset)
532 1.8 yamt break;
533 1.8 yamt
534 1.6 yamt len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
535 1.7 jmmv if (len == 0)
536 1.6 yamt break;
537 1.7 jmmv
538 1.18 yamt win = ubc_alloc(uobj, uio->uio_offset, &len, UVM_ADV_NORMAL,
539 1.18 yamt UBC_READ);
540 1.6 yamt error = uiomove(win, len, uio);
541 1.6 yamt ubc_release(win, flags);
542 1.1 jmmv }
543 1.1 jmmv
544 1.1 jmmv out:
545 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
546 1.1 jmmv
547 1.1 jmmv return error;
548 1.1 jmmv }
549 1.1 jmmv
550 1.1 jmmv /* --------------------------------------------------------------------- */
551 1.1 jmmv
552 1.1 jmmv int
553 1.1 jmmv tmpfs_write(void *v)
554 1.1 jmmv {
555 1.1 jmmv struct vnode *vp = ((struct vop_write_args *)v)->a_vp;
556 1.1 jmmv struct uio *uio = ((struct vop_write_args *)v)->a_uio;
557 1.1 jmmv int ioflag = ((struct vop_write_args *)v)->a_ioflag;
558 1.1 jmmv
559 1.36 thorpej bool extended;
560 1.1 jmmv int error;
561 1.7 jmmv int flags;
562 1.1 jmmv off_t oldsize;
563 1.1 jmmv struct tmpfs_node *node;
564 1.6 yamt struct uvm_object *uobj;
565 1.1 jmmv
566 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
567 1.1 jmmv
568 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
569 1.1 jmmv oldsize = node->tn_size;
570 1.1 jmmv
571 1.1 jmmv if (uio->uio_offset < 0 || vp->v_type != VREG) {
572 1.1 jmmv error = EINVAL;
573 1.1 jmmv goto out;
574 1.1 jmmv }
575 1.1 jmmv
576 1.1 jmmv if (uio->uio_resid == 0) {
577 1.1 jmmv error = 0;
578 1.1 jmmv goto out;
579 1.1 jmmv }
580 1.1 jmmv
581 1.1 jmmv if (ioflag & IO_APPEND)
582 1.1 jmmv uio->uio_offset = node->tn_size;
583 1.1 jmmv
584 1.1 jmmv extended = uio->uio_offset + uio->uio_resid > node->tn_size;
585 1.1 jmmv if (extended) {
586 1.1 jmmv error = tmpfs_reg_resize(vp, uio->uio_offset + uio->uio_resid);
587 1.1 jmmv if (error != 0)
588 1.1 jmmv goto out;
589 1.1 jmmv }
590 1.1 jmmv
591 1.21 jmmv uobj = node->tn_spec.tn_reg.tn_aobj;
592 1.6 yamt flags = UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
593 1.6 yamt error = 0;
594 1.7 jmmv while (error == 0 && uio->uio_resid > 0) {
595 1.6 yamt vsize_t len;
596 1.6 yamt void *win;
597 1.6 yamt
598 1.6 yamt len = MIN(node->tn_size - uio->uio_offset, uio->uio_resid);
599 1.7 jmmv if (len == 0)
600 1.6 yamt break;
601 1.7 jmmv
602 1.18 yamt win = ubc_alloc(uobj, uio->uio_offset, &len, UVM_ADV_NORMAL,
603 1.18 yamt UBC_WRITE);
604 1.6 yamt error = uiomove(win, len, uio);
605 1.6 yamt ubc_release(win, flags);
606 1.1 jmmv }
607 1.6 yamt
608 1.1 jmmv node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_MODIFIED |
609 1.1 jmmv (extended ? TMPFS_NODE_CHANGED : 0);
610 1.1 jmmv
611 1.7 jmmv if (error != 0)
612 1.6 yamt (void)tmpfs_reg_resize(vp, oldsize);
613 1.6 yamt
614 1.31 jmmv VN_KNOTE(vp, NOTE_WRITE);
615 1.31 jmmv
616 1.1 jmmv out:
617 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
618 1.1 jmmv KASSERT(IMPLIES(error == 0, uio->uio_resid == 0));
619 1.1 jmmv KASSERT(IMPLIES(error != 0, oldsize == node->tn_size));
620 1.1 jmmv
621 1.1 jmmv return error;
622 1.1 jmmv }
623 1.1 jmmv
624 1.1 jmmv /* --------------------------------------------------------------------- */
625 1.1 jmmv
626 1.1 jmmv int
627 1.1 jmmv tmpfs_fsync(void *v)
628 1.1 jmmv {
629 1.1 jmmv struct vnode *vp = ((struct vop_fsync_args *)v)->a_vp;
630 1.1 jmmv
631 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
632 1.1 jmmv
633 1.51 christos tmpfs_update(vp, NULL, NULL, NULL, 0);
634 1.17 yamt
635 1.17 yamt return 0;
636 1.1 jmmv }
637 1.1 jmmv
638 1.1 jmmv /* --------------------------------------------------------------------- */
639 1.1 jmmv
640 1.1 jmmv int
641 1.1 jmmv tmpfs_remove(void *v)
642 1.1 jmmv {
643 1.1 jmmv struct vnode *dvp = ((struct vop_remove_args *)v)->a_dvp;
644 1.1 jmmv struct vnode *vp = ((struct vop_remove_args *)v)->a_vp;
645 1.45 ad struct componentname *cnp = (((struct vop_remove_args *)v)->a_cnp);
646 1.1 jmmv
647 1.1 jmmv int error;
648 1.1 jmmv struct tmpfs_dirent *de;
649 1.1 jmmv struct tmpfs_mount *tmp;
650 1.1 jmmv struct tmpfs_node *dnode;
651 1.1 jmmv struct tmpfs_node *node;
652 1.1 jmmv
653 1.1 jmmv KASSERT(VOP_ISLOCKED(dvp));
654 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
655 1.1 jmmv
656 1.34 pooka if (vp->v_type == VDIR) {
657 1.34 pooka error = EPERM;
658 1.34 pooka goto out;
659 1.34 pooka }
660 1.34 pooka
661 1.1 jmmv dnode = VP_TO_TMPFS_DIR(dvp);
662 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
663 1.1 jmmv tmp = VFS_TO_TMPFS(vp->v_mount);
664 1.45 ad de = tmpfs_dir_lookup(dnode, cnp);
665 1.45 ad if (de == NULL) {
666 1.45 ad error = ENOENT;
667 1.45 ad goto out;
668 1.45 ad }
669 1.45 ad KASSERT(de->td_node == node);
670 1.1 jmmv
671 1.1 jmmv /* Files marked as immutable or append-only cannot be deleted. */
672 1.1 jmmv if (node->tn_flags & (IMMUTABLE | APPEND)) {
673 1.1 jmmv error = EPERM;
674 1.1 jmmv goto out;
675 1.1 jmmv }
676 1.1 jmmv
677 1.1 jmmv /* Remove the entry from the directory; as it is a file, we do not
678 1.1 jmmv * have to change the number of hard links of the directory. */
679 1.1 jmmv tmpfs_dir_detach(dvp, de);
680 1.1 jmmv
681 1.1 jmmv /* Free the directory entry we just deleted. Note that the node
682 1.1 jmmv * referred by it will not be removed until the vnode is really
683 1.1 jmmv * reclaimed. */
684 1.37 thorpej tmpfs_free_dirent(tmp, de, true);
685 1.1 jmmv
686 1.1 jmmv error = 0;
687 1.1 jmmv
688 1.1 jmmv out:
689 1.1 jmmv vput(vp);
690 1.34 pooka if (dvp == vp)
691 1.34 pooka vrele(dvp);
692 1.34 pooka else
693 1.34 pooka vput(dvp);
694 1.51.6.5 snj PNBUF_PUT(cnp->cn_pnbuf);
695 1.1 jmmv
696 1.1 jmmv return error;
697 1.1 jmmv }
698 1.1 jmmv
699 1.1 jmmv /* --------------------------------------------------------------------- */
700 1.1 jmmv
701 1.1 jmmv int
702 1.1 jmmv tmpfs_link(void *v)
703 1.1 jmmv {
704 1.1 jmmv struct vnode *dvp = ((struct vop_link_args *)v)->a_dvp;
705 1.1 jmmv struct vnode *vp = ((struct vop_link_args *)v)->a_vp;
706 1.1 jmmv struct componentname *cnp = ((struct vop_link_args *)v)->a_cnp;
707 1.1 jmmv
708 1.1 jmmv int error;
709 1.1 jmmv struct tmpfs_dirent *de;
710 1.1 jmmv struct tmpfs_node *dnode;
711 1.1 jmmv struct tmpfs_node *node;
712 1.1 jmmv
713 1.1 jmmv KASSERT(VOP_ISLOCKED(dvp));
714 1.1 jmmv KASSERT(cnp->cn_flags & HASBUF);
715 1.1 jmmv KASSERT(dvp != vp); /* XXX When can this be false? */
716 1.1 jmmv
717 1.1 jmmv dnode = VP_TO_TMPFS_DIR(dvp);
718 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
719 1.1 jmmv
720 1.17 yamt /* Lock vp because we will need to run tmpfs_update over it, which
721 1.1 jmmv * needs the vnode to be locked. */
722 1.1 jmmv error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
723 1.1 jmmv if (error != 0)
724 1.45 ad goto out1;
725 1.1 jmmv
726 1.1 jmmv /* XXX: Why aren't the following two tests done by the caller? */
727 1.1 jmmv
728 1.1 jmmv /* Hard links of directories are forbidden. */
729 1.1 jmmv if (vp->v_type == VDIR) {
730 1.1 jmmv error = EPERM;
731 1.1 jmmv goto out;
732 1.1 jmmv }
733 1.1 jmmv
734 1.1 jmmv /* Cannot create cross-device links. */
735 1.1 jmmv if (dvp->v_mount != vp->v_mount) {
736 1.1 jmmv error = EXDEV;
737 1.1 jmmv goto out;
738 1.1 jmmv }
739 1.1 jmmv
740 1.1 jmmv /* Ensure that we do not overflow the maximum number of links imposed
741 1.1 jmmv * by the system. */
742 1.1 jmmv KASSERT(node->tn_links <= LINK_MAX);
743 1.1 jmmv if (node->tn_links == LINK_MAX) {
744 1.1 jmmv error = EMLINK;
745 1.1 jmmv goto out;
746 1.1 jmmv }
747 1.1 jmmv
748 1.1 jmmv /* We cannot create links of files marked immutable or append-only. */
749 1.1 jmmv if (node->tn_flags & (IMMUTABLE | APPEND)) {
750 1.1 jmmv error = EPERM;
751 1.1 jmmv goto out;
752 1.1 jmmv }
753 1.1 jmmv
754 1.1 jmmv /* Allocate a new directory entry to represent the node. */
755 1.1 jmmv error = tmpfs_alloc_dirent(VFS_TO_TMPFS(vp->v_mount), node,
756 1.1 jmmv cnp->cn_nameptr, cnp->cn_namelen, &de);
757 1.1 jmmv if (error != 0)
758 1.1 jmmv goto out;
759 1.1 jmmv
760 1.1 jmmv /* Insert the new directory entry into the appropriate directory. */
761 1.1 jmmv tmpfs_dir_attach(dvp, de);
762 1.1 jmmv
763 1.1 jmmv /* vp link count has changed, so update node times. */
764 1.1 jmmv node->tn_status |= TMPFS_NODE_CHANGED;
765 1.51 christos tmpfs_update(vp, NULL, NULL, NULL, 0);
766 1.1 jmmv
767 1.1 jmmv error = 0;
768 1.1 jmmv
769 1.1 jmmv out:
770 1.45 ad VOP_UNLOCK(vp, 0);
771 1.45 ad out1:
772 1.4 yamt PNBUF_PUT(cnp->cn_pnbuf);
773 1.1 jmmv
774 1.1 jmmv vput(dvp);
775 1.1 jmmv
776 1.1 jmmv return error;
777 1.1 jmmv }
778 1.1 jmmv
779 1.1 jmmv /* --------------------------------------------------------------------- */
780 1.1 jmmv
781 1.1 jmmv int
782 1.1 jmmv tmpfs_rename(void *v)
783 1.1 jmmv {
784 1.1 jmmv struct vnode *fdvp = ((struct vop_rename_args *)v)->a_fdvp;
785 1.1 jmmv struct vnode *fvp = ((struct vop_rename_args *)v)->a_fvp;
786 1.1 jmmv struct componentname *fcnp = ((struct vop_rename_args *)v)->a_fcnp;
787 1.1 jmmv struct vnode *tdvp = ((struct vop_rename_args *)v)->a_tdvp;
788 1.1 jmmv struct vnode *tvp = ((struct vop_rename_args *)v)->a_tvp;
789 1.1 jmmv struct componentname *tcnp = ((struct vop_rename_args *)v)->a_tcnp;
790 1.1 jmmv
791 1.1 jmmv char *newname;
792 1.1 jmmv int error;
793 1.45 ad struct tmpfs_dirent *de, *de2;
794 1.1 jmmv struct tmpfs_mount *tmp;
795 1.1 jmmv struct tmpfs_node *fdnode;
796 1.1 jmmv struct tmpfs_node *fnode;
797 1.39 jmmv struct tmpfs_node *tnode;
798 1.1 jmmv struct tmpfs_node *tdnode;
799 1.45 ad size_t namelen;
800 1.1 jmmv
801 1.1 jmmv KASSERT(VOP_ISLOCKED(tdvp));
802 1.45 ad KASSERT(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
803 1.1 jmmv KASSERT(fcnp->cn_flags & HASBUF);
804 1.1 jmmv KASSERT(tcnp->cn_flags & HASBUF);
805 1.1 jmmv
806 1.45 ad newname = NULL;
807 1.45 ad namelen = 0;
808 1.45 ad tmp = NULL;
809 1.1 jmmv
810 1.45 ad /* Disallow cross-device renames. */
811 1.1 jmmv if (fvp->v_mount != tdvp->v_mount ||
812 1.1 jmmv (tvp != NULL && fvp->v_mount != tvp->v_mount)) {
813 1.1 jmmv error = EXDEV;
814 1.45 ad goto out_unlocked;
815 1.1 jmmv }
816 1.1 jmmv
817 1.45 ad fnode = VP_TO_TMPFS_NODE(fvp);
818 1.45 ad fdnode = VP_TO_TMPFS_DIR(fdvp);
819 1.45 ad tnode = (tvp == NULL) ? NULL : VP_TO_TMPFS_NODE(tvp);
820 1.45 ad tdnode = VP_TO_TMPFS_DIR(tdvp);
821 1.1 jmmv tmp = VFS_TO_TMPFS(tdvp->v_mount);
822 1.45 ad
823 1.51.6.1 snj if (fdvp == tvp) {
824 1.51.6.1 snj error = 0;
825 1.51.6.1 snj goto out_unlocked;
826 1.51.6.1 snj }
827 1.51.6.1 snj
828 1.45 ad /* If we need to move the directory between entries, lock the
829 1.45 ad * source so that we can safely operate on it. */
830 1.45 ad
831 1.45 ad /* XXX: this is a potential locking order violation! */
832 1.45 ad if (fdnode != tdnode) {
833 1.45 ad error = vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
834 1.45 ad if (error != 0)
835 1.45 ad goto out_unlocked;
836 1.45 ad }
837 1.45 ad
838 1.51.6.3 snj /*
839 1.51.6.3 snj * If the node we were renaming has scarpered, just give up.
840 1.51.6.3 snj */
841 1.45 ad de = tmpfs_dir_lookup(fdnode, fcnp);
842 1.51.6.3 snj if (de == NULL || de->td_node != fnode) {
843 1.45 ad error = ENOENT;
844 1.45 ad goto out;
845 1.45 ad }
846 1.1 jmmv
847 1.1 jmmv /* If source and target are the same file, there is nothing to do. */
848 1.1 jmmv if (fvp == tvp) {
849 1.1 jmmv error = 0;
850 1.1 jmmv goto out;
851 1.1 jmmv }
852 1.1 jmmv
853 1.39 jmmv /* If replacing an existing entry, ensure we can do the operation. */
854 1.39 jmmv if (tvp != NULL) {
855 1.39 jmmv KASSERT(tnode != NULL);
856 1.39 jmmv if (fnode->tn_type == VDIR && tnode->tn_type == VDIR) {
857 1.39 jmmv if (tnode->tn_size > 0) {
858 1.39 jmmv error = ENOTEMPTY;
859 1.39 jmmv goto out;
860 1.39 jmmv }
861 1.39 jmmv } else if (fnode->tn_type == VDIR && tnode->tn_type != VDIR) {
862 1.39 jmmv error = ENOTDIR;
863 1.39 jmmv goto out;
864 1.39 jmmv } else if (fnode->tn_type != VDIR && tnode->tn_type == VDIR) {
865 1.39 jmmv error = EISDIR;
866 1.39 jmmv goto out;
867 1.39 jmmv } else {
868 1.39 jmmv KASSERT(fnode->tn_type != VDIR &&
869 1.39 jmmv tnode->tn_type != VDIR);
870 1.39 jmmv }
871 1.39 jmmv }
872 1.39 jmmv
873 1.1 jmmv /* Ensure that we have enough memory to hold the new name, if it
874 1.1 jmmv * has to be changed. */
875 1.45 ad namelen = tcnp->cn_namelen;
876 1.1 jmmv if (fcnp->cn_namelen != tcnp->cn_namelen ||
877 1.1 jmmv memcmp(fcnp->cn_nameptr, tcnp->cn_nameptr, fcnp->cn_namelen) != 0) {
878 1.45 ad newname = tmpfs_str_pool_get(&tmp->tm_str_pool, namelen, 0);
879 1.1 jmmv if (newname == NULL) {
880 1.1 jmmv error = ENOSPC;
881 1.45 ad goto out;
882 1.1 jmmv }
883 1.45 ad }
884 1.1 jmmv
885 1.1 jmmv /* If the node is being moved to another directory, we have to do
886 1.1 jmmv * the move. */
887 1.1 jmmv if (fdnode != tdnode) {
888 1.1 jmmv /* In case we are moving a directory, we have to adjust its
889 1.1 jmmv * parent to point to the new parent. */
890 1.1 jmmv if (de->td_node->tn_type == VDIR) {
891 1.1 jmmv struct tmpfs_node *n;
892 1.1 jmmv
893 1.1 jmmv /* Ensure the target directory is not a child of the
894 1.1 jmmv * directory being moved. Otherwise, we'd end up
895 1.1 jmmv * with stale nodes. */
896 1.1 jmmv n = tdnode;
897 1.21 jmmv while (n != n->tn_spec.tn_dir.tn_parent) {
898 1.1 jmmv if (n == fnode) {
899 1.1 jmmv error = EINVAL;
900 1.45 ad goto out;
901 1.1 jmmv }
902 1.21 jmmv n = n->tn_spec.tn_dir.tn_parent;
903 1.1 jmmv }
904 1.1 jmmv
905 1.1 jmmv /* Adjust the parent pointer. */
906 1.1 jmmv TMPFS_VALIDATE_DIR(fnode);
907 1.21 jmmv de->td_node->tn_spec.tn_dir.tn_parent = tdnode;
908 1.1 jmmv
909 1.1 jmmv /* As a result of changing the target of the '..'
910 1.1 jmmv * entry, the link count of the source and target
911 1.1 jmmv * directories has to be adjusted. */
912 1.1 jmmv fdnode->tn_links--;
913 1.1 jmmv tdnode->tn_links++;
914 1.1 jmmv }
915 1.1 jmmv
916 1.1 jmmv /* Do the move: just remove the entry from the source directory
917 1.1 jmmv * and insert it into the target one. */
918 1.1 jmmv tmpfs_dir_detach(fdvp, de);
919 1.1 jmmv tmpfs_dir_attach(tdvp, de);
920 1.1 jmmv
921 1.1 jmmv /* Notify listeners of fdvp about the change in the directory.
922 1.1 jmmv * We can do it at this point because we aren't touching fdvp
923 1.1 jmmv * any more below. */
924 1.1 jmmv VN_KNOTE(fdvp, NOTE_WRITE);
925 1.1 jmmv }
926 1.1 jmmv
927 1.45 ad /* If we are overwriting an entry, we have to remove the old one
928 1.45 ad * from the target directory. */
929 1.45 ad if (tvp != NULL) {
930 1.45 ad KASSERT(tnode != NULL);
931 1.45 ad
932 1.45 ad /* Remove the old entry from the target directory.
933 1.45 ad * Note! This relies on tmpfs_dir_attach() putting the new
934 1.45 ad * node on the end of the target's node list. */
935 1.45 ad de2 = tmpfs_dir_lookup(tdnode, tcnp);
936 1.45 ad KASSERT(de2 != NULL);
937 1.45 ad KASSERT(de2->td_node == tnode);
938 1.45 ad tmpfs_dir_detach(tdvp, de2);
939 1.45 ad
940 1.45 ad /* Free the directory entry we just deleted. Note that the
941 1.45 ad * node referred by it will not be removed until the vnode is
942 1.45 ad * really reclaimed. */
943 1.45 ad tmpfs_free_dirent(VFS_TO_TMPFS(tvp->v_mount), de2, true);
944 1.45 ad }
945 1.45 ad
946 1.1 jmmv /* If the name has changed, we need to make it effective by changing
947 1.1 jmmv * it in the directory entry. */
948 1.1 jmmv if (newname != NULL) {
949 1.1 jmmv KASSERT(tcnp->cn_namelen < MAXNAMLEN);
950 1.1 jmmv KASSERT(tcnp->cn_namelen < 0xffff);
951 1.1 jmmv
952 1.1 jmmv tmpfs_str_pool_put(&tmp->tm_str_pool, de->td_name,
953 1.1 jmmv de->td_namelen);
954 1.45 ad de->td_namelen = (uint16_t)namelen;
955 1.45 ad memcpy(newname, tcnp->cn_nameptr, namelen);
956 1.1 jmmv de->td_name = newname;
957 1.45 ad newname = NULL;
958 1.1 jmmv
959 1.27 jmmv fnode->tn_status |= TMPFS_NODE_CHANGED;
960 1.26 jmmv tdnode->tn_status |= TMPFS_NODE_MODIFIED;
961 1.1 jmmv }
962 1.1 jmmv
963 1.1 jmmv /* Notify listeners of tdvp about the change in the directory (either
964 1.31 jmmv * because a new entry was added or because one was removed) and
965 1.31 jmmv * listeners of fvp about the rename. */
966 1.1 jmmv VN_KNOTE(tdvp, NOTE_WRITE);
967 1.31 jmmv VN_KNOTE(fvp, NOTE_RENAME);
968 1.1 jmmv
969 1.1 jmmv error = 0;
970 1.1 jmmv
971 1.45 ad out:
972 1.11 jmmv if (fdnode != tdnode)
973 1.11 jmmv VOP_UNLOCK(fdvp, 0);
974 1.11 jmmv
975 1.45 ad out_unlocked:
976 1.1 jmmv /* Release target nodes. */
977 1.1 jmmv if (tdvp == tvp)
978 1.1 jmmv vrele(tdvp);
979 1.1 jmmv else
980 1.1 jmmv vput(tdvp);
981 1.1 jmmv if (tvp != NULL)
982 1.1 jmmv vput(tvp);
983 1.1 jmmv
984 1.1 jmmv /* Release source nodes. */
985 1.1 jmmv vrele(fdvp);
986 1.1 jmmv vrele(fvp);
987 1.1 jmmv
988 1.45 ad if (newname != NULL)
989 1.45 ad tmpfs_str_pool_put(&tmp->tm_str_pool, newname, namelen);
990 1.45 ad
991 1.1 jmmv return error;
992 1.1 jmmv }
993 1.1 jmmv
994 1.1 jmmv /* --------------------------------------------------------------------- */
995 1.1 jmmv
996 1.1 jmmv int
997 1.1 jmmv tmpfs_mkdir(void *v)
998 1.1 jmmv {
999 1.1 jmmv struct vnode *dvp = ((struct vop_mkdir_args *)v)->a_dvp;
1000 1.1 jmmv struct vnode **vpp = ((struct vop_mkdir_args *)v)->a_vpp;
1001 1.1 jmmv struct componentname *cnp = ((struct vop_mkdir_args *)v)->a_cnp;
1002 1.1 jmmv struct vattr *vap = ((struct vop_mkdir_args *)v)->a_vap;
1003 1.1 jmmv
1004 1.1 jmmv KASSERT(vap->va_type == VDIR);
1005 1.1 jmmv
1006 1.1 jmmv return tmpfs_alloc_file(dvp, vpp, vap, cnp, NULL);
1007 1.1 jmmv }
1008 1.1 jmmv
1009 1.1 jmmv /* --------------------------------------------------------------------- */
1010 1.1 jmmv
1011 1.1 jmmv int
1012 1.1 jmmv tmpfs_rmdir(void *v)
1013 1.1 jmmv {
1014 1.1 jmmv struct vnode *dvp = ((struct vop_rmdir_args *)v)->a_dvp;
1015 1.1 jmmv struct vnode *vp = ((struct vop_rmdir_args *)v)->a_vp;
1016 1.45 ad struct componentname *cnp = ((struct vop_rmdir_args *)v)->a_cnp;
1017 1.1 jmmv
1018 1.1 jmmv int error;
1019 1.1 jmmv struct tmpfs_dirent *de;
1020 1.1 jmmv struct tmpfs_mount *tmp;
1021 1.1 jmmv struct tmpfs_node *dnode;
1022 1.1 jmmv struct tmpfs_node *node;
1023 1.1 jmmv
1024 1.1 jmmv KASSERT(VOP_ISLOCKED(dvp));
1025 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
1026 1.1 jmmv
1027 1.1 jmmv tmp = VFS_TO_TMPFS(dvp->v_mount);
1028 1.1 jmmv dnode = VP_TO_TMPFS_DIR(dvp);
1029 1.1 jmmv node = VP_TO_TMPFS_DIR(vp);
1030 1.40 dyoung error = 0;
1031 1.34 pooka
1032 1.34 pooka /* Directories with more than two entries ('.' and '..') cannot be
1033 1.34 pooka * removed. */
1034 1.34 pooka if (node->tn_size > 0) {
1035 1.34 pooka error = ENOTEMPTY;
1036 1.34 pooka goto out;
1037 1.34 pooka }
1038 1.34 pooka
1039 1.34 pooka /* This invariant holds only if we are not trying to remove "..".
1040 1.34 pooka * We checked for that above so this is safe now. */
1041 1.21 jmmv KASSERT(node->tn_spec.tn_dir.tn_parent == dnode);
1042 1.1 jmmv
1043 1.45 ad /* Get the directory entry associated with node (vp). */
1044 1.45 ad de = tmpfs_dir_lookup(dnode, cnp);
1045 1.45 ad if (de == NULL) {
1046 1.45 ad error = ENOENT;
1047 1.45 ad goto out;
1048 1.45 ad }
1049 1.45 ad KASSERT(de->td_node == node);
1050 1.1 jmmv
1051 1.1 jmmv /* Check flags to see if we are allowed to remove the directory. */
1052 1.1 jmmv if (dnode->tn_flags & APPEND || node->tn_flags & (IMMUTABLE | APPEND)) {
1053 1.1 jmmv error = EPERM;
1054 1.1 jmmv goto out;
1055 1.1 jmmv }
1056 1.1 jmmv
1057 1.1 jmmv /* Detach the directory entry from the directory (dnode). */
1058 1.1 jmmv tmpfs_dir_detach(dvp, de);
1059 1.1 jmmv
1060 1.1 jmmv node->tn_links--;
1061 1.1 jmmv node->tn_status |= TMPFS_NODE_ACCESSED | TMPFS_NODE_CHANGED | \
1062 1.1 jmmv TMPFS_NODE_MODIFIED;
1063 1.21 jmmv node->tn_spec.tn_dir.tn_parent->tn_links--;
1064 1.21 jmmv node->tn_spec.tn_dir.tn_parent->tn_status |= TMPFS_NODE_ACCESSED | \
1065 1.1 jmmv TMPFS_NODE_CHANGED | TMPFS_NODE_MODIFIED;
1066 1.1 jmmv
1067 1.31 jmmv /* Release the parent. */
1068 1.1 jmmv cache_purge(dvp); /* XXX Is this needed? */
1069 1.1 jmmv
1070 1.1 jmmv /* Free the directory entry we just deleted. Note that the node
1071 1.1 jmmv * referred by it will not be removed until the vnode is really
1072 1.1 jmmv * reclaimed. */
1073 1.37 thorpej tmpfs_free_dirent(tmp, de, true);
1074 1.1 jmmv
1075 1.45 ad KASSERT(node->tn_links == 0);
1076 1.40 dyoung out:
1077 1.40 dyoung /* Release the nodes. */
1078 1.40 dyoung vput(dvp);
1079 1.1 jmmv vput(vp);
1080 1.51.6.5 snj PNBUF_PUT(cnp->cn_pnbuf);
1081 1.1 jmmv
1082 1.1 jmmv return error;
1083 1.1 jmmv }
1084 1.1 jmmv
1085 1.1 jmmv /* --------------------------------------------------------------------- */
1086 1.1 jmmv
1087 1.1 jmmv int
1088 1.1 jmmv tmpfs_symlink(void *v)
1089 1.1 jmmv {
1090 1.1 jmmv struct vnode *dvp = ((struct vop_symlink_args *)v)->a_dvp;
1091 1.1 jmmv struct vnode **vpp = ((struct vop_symlink_args *)v)->a_vpp;
1092 1.1 jmmv struct componentname *cnp = ((struct vop_symlink_args *)v)->a_cnp;
1093 1.1 jmmv struct vattr *vap = ((struct vop_symlink_args *)v)->a_vap;
1094 1.1 jmmv char *target = ((struct vop_symlink_args *)v)->a_target;
1095 1.1 jmmv
1096 1.1 jmmv KASSERT(vap->va_type == VLNK);
1097 1.1 jmmv
1098 1.1 jmmv return tmpfs_alloc_file(dvp, vpp, vap, cnp, target);
1099 1.1 jmmv }
1100 1.1 jmmv
1101 1.1 jmmv /* --------------------------------------------------------------------- */
1102 1.1 jmmv
1103 1.1 jmmv int
1104 1.1 jmmv tmpfs_readdir(void *v)
1105 1.1 jmmv {
1106 1.1 jmmv struct vnode *vp = ((struct vop_readdir_args *)v)->a_vp;
1107 1.1 jmmv struct uio *uio = ((struct vop_readdir_args *)v)->a_uio;
1108 1.1 jmmv int *eofflag = ((struct vop_readdir_args *)v)->a_eofflag;
1109 1.1 jmmv off_t **cookies = ((struct vop_readdir_args *)v)->a_cookies;
1110 1.1 jmmv int *ncookies = ((struct vop_readdir_args *)v)->a_ncookies;
1111 1.1 jmmv
1112 1.1 jmmv int error;
1113 1.10 yamt off_t startoff;
1114 1.10 yamt off_t cnt;
1115 1.1 jmmv struct tmpfs_node *node;
1116 1.1 jmmv
1117 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
1118 1.1 jmmv
1119 1.1 jmmv /* This operation only makes sense on directory nodes. */
1120 1.1 jmmv if (vp->v_type != VDIR) {
1121 1.1 jmmv error = ENOTDIR;
1122 1.1 jmmv goto out;
1123 1.1 jmmv }
1124 1.1 jmmv
1125 1.1 jmmv node = VP_TO_TMPFS_DIR(vp);
1126 1.1 jmmv
1127 1.1 jmmv startoff = uio->uio_offset;
1128 1.1 jmmv
1129 1.10 yamt cnt = 0;
1130 1.10 yamt if (uio->uio_offset == TMPFS_DIRCOOKIE_DOT) {
1131 1.1 jmmv error = tmpfs_dir_getdotdent(node, uio);
1132 1.1 jmmv if (error == -1) {
1133 1.1 jmmv error = 0;
1134 1.1 jmmv goto outok;
1135 1.1 jmmv } else if (error != 0)
1136 1.1 jmmv goto outok;
1137 1.10 yamt cnt++;
1138 1.1 jmmv }
1139 1.1 jmmv
1140 1.10 yamt if (uio->uio_offset == TMPFS_DIRCOOKIE_DOTDOT) {
1141 1.1 jmmv error = tmpfs_dir_getdotdotdent(node, uio);
1142 1.1 jmmv if (error == -1) {
1143 1.1 jmmv error = 0;
1144 1.1 jmmv goto outok;
1145 1.1 jmmv } else if (error != 0)
1146 1.1 jmmv goto outok;
1147 1.10 yamt cnt++;
1148 1.1 jmmv }
1149 1.1 jmmv
1150 1.10 yamt error = tmpfs_dir_getdents(node, uio, &cnt);
1151 1.1 jmmv if (error == -1)
1152 1.1 jmmv error = 0;
1153 1.1 jmmv KASSERT(error >= 0);
1154 1.1 jmmv
1155 1.1 jmmv outok:
1156 1.10 yamt /* This label assumes that startoff has been
1157 1.1 jmmv * initialized. If the compiler didn't spit out warnings, we'd
1158 1.1 jmmv * simply make this one be 'out' and drop 'outok'. */
1159 1.1 jmmv
1160 1.1 jmmv if (eofflag != NULL)
1161 1.1 jmmv *eofflag =
1162 1.10 yamt (error == 0 && uio->uio_offset == TMPFS_DIRCOOKIE_EOF);
1163 1.1 jmmv
1164 1.1 jmmv /* Update NFS-related variables. */
1165 1.1 jmmv if (error == 0 && cookies != NULL && ncookies != NULL) {
1166 1.1 jmmv off_t i;
1167 1.10 yamt off_t off = startoff;
1168 1.10 yamt struct tmpfs_dirent *de = NULL;
1169 1.1 jmmv
1170 1.10 yamt *ncookies = cnt;
1171 1.10 yamt *cookies = malloc(cnt * sizeof(off_t), M_TEMP, M_WAITOK);
1172 1.10 yamt
1173 1.10 yamt for (i = 0; i < cnt; i++) {
1174 1.10 yamt KASSERT(off != TMPFS_DIRCOOKIE_EOF);
1175 1.10 yamt if (off == TMPFS_DIRCOOKIE_DOT) {
1176 1.10 yamt off = TMPFS_DIRCOOKIE_DOTDOT;
1177 1.10 yamt } else {
1178 1.10 yamt if (off == TMPFS_DIRCOOKIE_DOTDOT) {
1179 1.21 jmmv de = TAILQ_FIRST(&node->tn_spec.
1180 1.21 jmmv tn_dir.tn_dir);
1181 1.10 yamt } else if (de != NULL) {
1182 1.10 yamt de = TAILQ_NEXT(de, td_entries);
1183 1.10 yamt } else {
1184 1.10 yamt de = tmpfs_dir_lookupbycookie(node,
1185 1.10 yamt off);
1186 1.10 yamt KASSERT(de != NULL);
1187 1.10 yamt de = TAILQ_NEXT(de, td_entries);
1188 1.10 yamt }
1189 1.10 yamt if (de == NULL) {
1190 1.10 yamt off = TMPFS_DIRCOOKIE_EOF;
1191 1.10 yamt } else {
1192 1.29 jmmv off = tmpfs_dircookie(de);
1193 1.10 yamt }
1194 1.10 yamt }
1195 1.10 yamt
1196 1.10 yamt (*cookies)[i] = off;
1197 1.10 yamt }
1198 1.10 yamt KASSERT(uio->uio_offset == off);
1199 1.1 jmmv }
1200 1.1 jmmv
1201 1.1 jmmv out:
1202 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
1203 1.1 jmmv
1204 1.1 jmmv return error;
1205 1.1 jmmv }
1206 1.1 jmmv
1207 1.1 jmmv /* --------------------------------------------------------------------- */
1208 1.1 jmmv
1209 1.1 jmmv int
1210 1.1 jmmv tmpfs_readlink(void *v)
1211 1.1 jmmv {
1212 1.1 jmmv struct vnode *vp = ((struct vop_readlink_args *)v)->a_vp;
1213 1.1 jmmv struct uio *uio = ((struct vop_readlink_args *)v)->a_uio;
1214 1.1 jmmv
1215 1.1 jmmv int error;
1216 1.1 jmmv struct tmpfs_node *node;
1217 1.1 jmmv
1218 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
1219 1.1 jmmv KASSERT(uio->uio_offset == 0);
1220 1.1 jmmv KASSERT(vp->v_type == VLNK);
1221 1.1 jmmv
1222 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
1223 1.1 jmmv
1224 1.21 jmmv error = uiomove(node->tn_spec.tn_lnk.tn_link,
1225 1.21 jmmv MIN(node->tn_size, uio->uio_resid), uio);
1226 1.1 jmmv node->tn_status |= TMPFS_NODE_ACCESSED;
1227 1.1 jmmv
1228 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
1229 1.1 jmmv
1230 1.1 jmmv return error;
1231 1.1 jmmv }
1232 1.1 jmmv
1233 1.1 jmmv /* --------------------------------------------------------------------- */
1234 1.1 jmmv
1235 1.1 jmmv int
1236 1.1 jmmv tmpfs_inactive(void *v)
1237 1.1 jmmv {
1238 1.1 jmmv struct vnode *vp = ((struct vop_inactive_args *)v)->a_vp;
1239 1.1 jmmv
1240 1.1 jmmv struct tmpfs_node *node;
1241 1.1 jmmv
1242 1.1 jmmv KASSERT(VOP_ISLOCKED(vp));
1243 1.1 jmmv
1244 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
1245 1.45 ad *((struct vop_inactive_args *)v)->a_recycle = (node->tn_links == 0);
1246 1.1 jmmv VOP_UNLOCK(vp, 0);
1247 1.1 jmmv
1248 1.1 jmmv return 0;
1249 1.1 jmmv }
1250 1.1 jmmv
1251 1.1 jmmv /* --------------------------------------------------------------------- */
1252 1.1 jmmv
1253 1.1 jmmv int
1254 1.1 jmmv tmpfs_reclaim(void *v)
1255 1.1 jmmv {
1256 1.1 jmmv struct vnode *vp = ((struct vop_reclaim_args *)v)->a_vp;
1257 1.1 jmmv
1258 1.1 jmmv struct tmpfs_mount *tmp;
1259 1.1 jmmv struct tmpfs_node *node;
1260 1.1 jmmv
1261 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
1262 1.1 jmmv tmp = VFS_TO_TMPFS(vp->v_mount);
1263 1.1 jmmv
1264 1.1 jmmv cache_purge(vp);
1265 1.1 jmmv tmpfs_free_vp(vp);
1266 1.1 jmmv
1267 1.1 jmmv /* If the node referenced by this vnode was deleted by the user,
1268 1.1 jmmv * we must free its associated data structures (now that the vnode
1269 1.1 jmmv * is being reclaimed). */
1270 1.1 jmmv if (node->tn_links == 0)
1271 1.1 jmmv tmpfs_free_node(tmp, node);
1272 1.1 jmmv
1273 1.1 jmmv KASSERT(vp->v_data == NULL);
1274 1.1 jmmv
1275 1.1 jmmv return 0;
1276 1.1 jmmv }
1277 1.1 jmmv
1278 1.1 jmmv /* --------------------------------------------------------------------- */
1279 1.1 jmmv
1280 1.1 jmmv int
1281 1.1 jmmv tmpfs_print(void *v)
1282 1.1 jmmv {
1283 1.1 jmmv struct vnode *vp = ((struct vop_print_args *)v)->a_vp;
1284 1.1 jmmv
1285 1.1 jmmv struct tmpfs_node *node;
1286 1.1 jmmv
1287 1.1 jmmv node = VP_TO_TMPFS_NODE(vp);
1288 1.1 jmmv
1289 1.1 jmmv printf("tag VT_TMPFS, tmpfs_node %p, flags 0x%x, links %d\n",
1290 1.1 jmmv node, node->tn_flags, node->tn_links);
1291 1.1 jmmv printf("\tmode 0%o, owner %d, group %d, size %" PRIdMAX
1292 1.1 jmmv ", status 0x%x\n",
1293 1.1 jmmv node->tn_mode, node->tn_uid, node->tn_gid,
1294 1.1 jmmv (uintmax_t)node->tn_size, node->tn_status);
1295 1.1 jmmv if (vp->v_type == VFIFO)
1296 1.1 jmmv fifo_printinfo(vp);
1297 1.1 jmmv printf("\n");
1298 1.1 jmmv
1299 1.1 jmmv return 0;
1300 1.1 jmmv }
1301 1.1 jmmv
1302 1.1 jmmv /* --------------------------------------------------------------------- */
1303 1.1 jmmv
1304 1.1 jmmv int
1305 1.1 jmmv tmpfs_pathconf(void *v)
1306 1.1 jmmv {
1307 1.1 jmmv int name = ((struct vop_pathconf_args *)v)->a_name;
1308 1.1 jmmv register_t *retval = ((struct vop_pathconf_args *)v)->a_retval;
1309 1.1 jmmv
1310 1.1 jmmv int error;
1311 1.1 jmmv
1312 1.1 jmmv error = 0;
1313 1.1 jmmv
1314 1.1 jmmv switch (name) {
1315 1.1 jmmv case _PC_LINK_MAX:
1316 1.1 jmmv *retval = LINK_MAX;
1317 1.1 jmmv break;
1318 1.1 jmmv
1319 1.1 jmmv case _PC_NAME_MAX:
1320 1.1 jmmv *retval = NAME_MAX;
1321 1.1 jmmv break;
1322 1.1 jmmv
1323 1.1 jmmv case _PC_PATH_MAX:
1324 1.1 jmmv *retval = PATH_MAX;
1325 1.1 jmmv break;
1326 1.1 jmmv
1327 1.1 jmmv case _PC_PIPE_BUF:
1328 1.1 jmmv *retval = PIPE_BUF;
1329 1.1 jmmv break;
1330 1.1 jmmv
1331 1.1 jmmv case _PC_CHOWN_RESTRICTED:
1332 1.1 jmmv *retval = 1;
1333 1.1 jmmv break;
1334 1.1 jmmv
1335 1.1 jmmv case _PC_NO_TRUNC:
1336 1.1 jmmv *retval = 1;
1337 1.1 jmmv break;
1338 1.1 jmmv
1339 1.1 jmmv case _PC_SYNC_IO:
1340 1.1 jmmv *retval = 1;
1341 1.1 jmmv break;
1342 1.1 jmmv
1343 1.1 jmmv case _PC_FILESIZEBITS:
1344 1.1 jmmv *retval = 0; /* XXX Don't know which value should I return. */
1345 1.1 jmmv break;
1346 1.1 jmmv
1347 1.1 jmmv default:
1348 1.1 jmmv error = EINVAL;
1349 1.1 jmmv }
1350 1.1 jmmv
1351 1.1 jmmv return error;
1352 1.1 jmmv }
1353 1.1 jmmv
1354 1.1 jmmv /* --------------------------------------------------------------------- */
1355 1.1 jmmv
1356 1.1 jmmv int
1357 1.15 jmmv tmpfs_advlock(void *v)
1358 1.15 jmmv {
1359 1.15 jmmv struct vnode *vp = ((struct vop_advlock_args *)v)->a_vp;
1360 1.15 jmmv
1361 1.15 jmmv struct tmpfs_node *node;
1362 1.15 jmmv
1363 1.15 jmmv node = VP_TO_TMPFS_NODE(vp);
1364 1.15 jmmv
1365 1.15 jmmv return lf_advlock(v, &node->tn_lockf, node->tn_size);
1366 1.15 jmmv }
1367 1.15 jmmv
1368 1.15 jmmv /* --------------------------------------------------------------------- */
1369 1.15 jmmv
1370 1.15 jmmv int
1371 1.1 jmmv tmpfs_getpages(void *v)
1372 1.1 jmmv {
1373 1.7 jmmv struct vnode *vp = ((struct vop_getpages_args *)v)->a_vp;
1374 1.7 jmmv voff_t offset = ((struct vop_getpages_args *)v)->a_offset;
1375 1.7 jmmv struct vm_page **m = ((struct vop_getpages_args *)v)->a_m;
1376 1.7 jmmv int *count = ((struct vop_getpages_args *)v)->a_count;
1377 1.7 jmmv int centeridx = ((struct vop_getpages_args *)v)->a_centeridx;
1378 1.7 jmmv vm_prot_t access_type = ((struct vop_getpages_args *)v)->a_access_type;
1379 1.7 jmmv int advice = ((struct vop_getpages_args *)v)->a_advice;
1380 1.7 jmmv int flags = ((struct vop_getpages_args *)v)->a_flags;
1381 1.7 jmmv
1382 1.7 jmmv int error;
1383 1.28 jmmv int i;
1384 1.7 jmmv struct tmpfs_node *node;
1385 1.6 yamt struct uvm_object *uobj;
1386 1.9 yamt int npages = *count;
1387 1.1 jmmv
1388 1.6 yamt KASSERT(vp->v_type == VREG);
1389 1.45 ad KASSERT(mutex_owned(&vp->v_interlock));
1390 1.1 jmmv
1391 1.7 jmmv node = VP_TO_TMPFS_NODE(vp);
1392 1.21 jmmv uobj = node->tn_spec.tn_reg.tn_aobj;
1393 1.1 jmmv
1394 1.9 yamt /* We currently don't rely on PGO_PASTEOF. */
1395 1.9 yamt
1396 1.9 yamt if (vp->v_size <= offset + (centeridx << PAGE_SHIFT)) {
1397 1.9 yamt if ((flags & PGO_LOCKED) == 0)
1398 1.45 ad mutex_exit(&vp->v_interlock);
1399 1.9 yamt return EINVAL;
1400 1.9 yamt }
1401 1.9 yamt
1402 1.9 yamt if (vp->v_size < offset + (npages << PAGE_SHIFT)) {
1403 1.9 yamt npages = (round_page(vp->v_size) - offset) >> PAGE_SHIFT;
1404 1.9 yamt }
1405 1.9 yamt
1406 1.7 jmmv if ((flags & PGO_LOCKED) != 0)
1407 1.6 yamt return EBUSY;
1408 1.1 jmmv
1409 1.6 yamt if ((flags & PGO_NOTIMESTAMP) == 0) {
1410 1.7 jmmv if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
1411 1.6 yamt node->tn_status |= TMPFS_NODE_ACCESSED;
1412 1.7 jmmv
1413 1.7 jmmv if ((access_type & VM_PROT_WRITE) != 0)
1414 1.6 yamt node->tn_status |= TMPFS_NODE_MODIFIED;
1415 1.1 jmmv }
1416 1.1 jmmv
1417 1.45 ad mutex_exit(&vp->v_interlock);
1418 1.6 yamt
1419 1.28 jmmv /*
1420 1.28 jmmv * Make sure that the array on which we will store the
1421 1.28 jmmv * gotten pages is clean. Otherwise uao_get (pointed to by
1422 1.28 jmmv * the pgo_get below) gets confused and does not return the
1423 1.28 jmmv * appropriate pages.
1424 1.49 jmmv *
1425 1.28 jmmv * XXX This shall be revisited when kern/32166 is addressed
1426 1.28 jmmv * because the loop to clean m[i] will most likely be redundant
1427 1.28 jmmv * as well as the PGO_ALLPAGES flag.
1428 1.28 jmmv */
1429 1.28 jmmv if (m != NULL)
1430 1.28 jmmv for (i = 0; i < npages; i++)
1431 1.28 jmmv m[i] = NULL;
1432 1.45 ad mutex_enter(&uobj->vmobjlock);
1433 1.9 yamt error = (*uobj->pgops->pgo_get)(uobj, offset, m, &npages, centeridx,
1434 1.28 jmmv access_type, advice, flags | PGO_ALLPAGES);
1435 1.28 jmmv #if defined(DEBUG)
1436 1.28 jmmv {
1437 1.28 jmmv /* Make sure that all the pages we return are valid. */
1438 1.28 jmmv int dbgi;
1439 1.28 jmmv if (error == 0 && m != NULL)
1440 1.28 jmmv for (dbgi = 0; dbgi < npages; dbgi++)
1441 1.28 jmmv KASSERT(m[dbgi] != NULL);
1442 1.28 jmmv }
1443 1.28 jmmv #endif
1444 1.1 jmmv
1445 1.6 yamt return error;
1446 1.6 yamt }
1447 1.6 yamt
1448 1.6 yamt /* --------------------------------------------------------------------- */
1449 1.6 yamt
1450 1.6 yamt int
1451 1.6 yamt tmpfs_putpages(void *v)
1452 1.6 yamt {
1453 1.7 jmmv struct vnode *vp = ((struct vop_putpages_args *)v)->a_vp;
1454 1.7 jmmv voff_t offlo = ((struct vop_putpages_args *)v)->a_offlo;
1455 1.7 jmmv voff_t offhi = ((struct vop_putpages_args *)v)->a_offhi;
1456 1.7 jmmv int flags = ((struct vop_putpages_args *)v)->a_flags;
1457 1.7 jmmv
1458 1.7 jmmv int error;
1459 1.7 jmmv struct tmpfs_node *node;
1460 1.6 yamt struct uvm_object *uobj;
1461 1.6 yamt
1462 1.45 ad KASSERT(mutex_owned(&vp->v_interlock));
1463 1.6 yamt
1464 1.7 jmmv node = VP_TO_TMPFS_NODE(vp);
1465 1.7 jmmv
1466 1.6 yamt if (vp->v_type != VREG) {
1467 1.45 ad mutex_exit(&vp->v_interlock);
1468 1.6 yamt return 0;
1469 1.1 jmmv }
1470 1.1 jmmv
1471 1.21 jmmv uobj = node->tn_spec.tn_reg.tn_aobj;
1472 1.45 ad mutex_exit(&vp->v_interlock);
1473 1.6 yamt
1474 1.45 ad mutex_enter(&uobj->vmobjlock);
1475 1.7 jmmv error = (*uobj->pgops->pgo_put)(uobj, offlo, offhi, flags);
1476 1.6 yamt
1477 1.6 yamt /* XXX mtime */
1478 1.1 jmmv
1479 1.1 jmmv return error;
1480 1.1 jmmv }
1481