devfs - Fix broken assertion
[dragonfly.git] / sys / vfs / devfs / devfs_vnops.c
CommitLineData
21864bc5 1/*
9f889dc4
MD
2 * (MPSAFE)
3 *
21864bc5
MD
4 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
5 *
6 * This code is derived from software contributed to The DragonFly Project
7 * by Alex Hornung <ahornung@gmail.com>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
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
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/time.h>
39#include <sys/kernel.h>
40#include <sys/lock.h>
41#include <sys/fcntl.h>
42#include <sys/proc.h>
43#include <sys/priv.h>
44#include <sys/signalvar.h>
45#include <sys/vnode.h>
46#include <sys/uio.h>
47#include <sys/mount.h>
48#include <sys/file.h>
49#include <sys/fcntl.h>
50#include <sys/namei.h>
51#include <sys/dirent.h>
52#include <sys/malloc.h>
53#include <sys/stat.h>
54#include <sys/reg.h>
21864bc5
MD
55#include <vm/vm_pager.h>
56#include <vm/vm_zone.h>
57#include <vm/vm_object.h>
58#include <sys/filio.h>
59#include <sys/ttycom.h>
21864bc5 60#include <sys/tty.h>
2d076755 61#include <sys/diskslice.h>
3a1032a6 62#include <sys/sysctl.h>
2c1e28dd 63#include <sys/devfs.h>
21864bc5 64#include <sys/pioctl.h>
c705e298 65#include <vfs/fifofs/fifo.h>
21864bc5
MD
66
67#include <machine/limits.h>
684a93c4 68
1a54183b
MD
69#include <sys/buf2.h>
70#include <sys/sysref2.h>
684a93c4
MD
71#include <sys/mplock2.h>
72#include <vm/vm_page2.h>
21864bc5
MD
73
74MALLOC_DECLARE(M_DEVFS);
9f889dc4
MD
75#define DEVFS_BADOP (void *)devfs_vop_badop
76
77static int devfs_vop_badop(struct vop_generic_args *);
78static int devfs_vop_access(struct vop_access_args *);
79static int devfs_vop_inactive(struct vop_inactive_args *);
80static int devfs_vop_reclaim(struct vop_reclaim_args *);
81static int devfs_vop_readdir(struct vop_readdir_args *);
82static int devfs_vop_getattr(struct vop_getattr_args *);
83static int devfs_vop_setattr(struct vop_setattr_args *);
84static int devfs_vop_readlink(struct vop_readlink_args *);
85static int devfs_vop_print(struct vop_print_args *);
86
87static int devfs_vop_nresolve(struct vop_nresolve_args *);
88static int devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
89static int devfs_vop_nmkdir(struct vop_nmkdir_args *);
90static int devfs_vop_nsymlink(struct vop_nsymlink_args *);
91static int devfs_vop_nrmdir(struct vop_nrmdir_args *);
92static int devfs_vop_nremove(struct vop_nremove_args *);
21864bc5
MD
93
94static int devfs_spec_open(struct vop_open_args *);
95static int devfs_spec_close(struct vop_close_args *);
96static int devfs_spec_fsync(struct vop_fsync_args *);
97
98static int devfs_spec_read(struct vop_read_args *);
99static int devfs_spec_write(struct vop_write_args *);
100static int devfs_spec_ioctl(struct vop_ioctl_args *);
21864bc5
MD
101static int devfs_spec_kqfilter(struct vop_kqfilter_args *);
102static int devfs_spec_strategy(struct vop_strategy_args *);
103static void devfs_spec_strategy_done(struct bio *);
104static int devfs_spec_freeblks(struct vop_freeblks_args *);
105static int devfs_spec_bmap(struct vop_bmap_args *);
106static int devfs_spec_advlock(struct vop_advlock_args *);
107static void devfs_spec_getpages_iodone(struct bio *);
108static int devfs_spec_getpages(struct vop_getpages_args *);
109
9f889dc4
MD
110static int devfs_fo_close(struct file *);
111static int devfs_fo_read(struct file *, struct uio *, struct ucred *, int);
112static int devfs_fo_write(struct file *, struct uio *, struct ucred *, int);
113static int devfs_fo_stat(struct file *, struct stat *, struct ucred *);
114static int devfs_fo_kqfilter(struct file *, struct knote *);
115static int devfs_fo_ioctl(struct file *, u_long, caddr_t,
87baaf0c 116 struct ucred *, struct sysmsg *);
21864bc5 117static __inline int sequential_heuristic(struct uio *, struct file *);
87baaf0c
MD
118
119extern struct lock devfs_lock;
21864bc5
MD
120
121/*
9f889dc4 122 * devfs vnode operations for regular files. All vnode ops are MPSAFE.
21864bc5
MD
123 */
124struct vop_ops devfs_vnode_norm_vops = {
125 .vop_default = vop_defaultop,
9f889dc4 126 .vop_access = devfs_vop_access,
21864bc5 127 .vop_advlock = DEVFS_BADOP,
9f889dc4 128 .vop_bmap = DEVFS_BADOP,
21864bc5 129 .vop_close = vop_stdclose,
9f889dc4
MD
130 .vop_getattr = devfs_vop_getattr,
131 .vop_inactive = devfs_vop_inactive,
21864bc5 132 .vop_ncreate = DEVFS_BADOP,
9f889dc4
MD
133 .vop_nresolve = devfs_vop_nresolve,
134 .vop_nlookupdotdot = devfs_vop_nlookupdotdot,
21864bc5 135 .vop_nlink = DEVFS_BADOP,
9f889dc4 136 .vop_nmkdir = devfs_vop_nmkdir,
21864bc5 137 .vop_nmknod = DEVFS_BADOP,
9f889dc4 138 .vop_nremove = devfs_vop_nremove,
21864bc5 139 .vop_nrename = DEVFS_BADOP,
9f889dc4
MD
140 .vop_nrmdir = devfs_vop_nrmdir,
141 .vop_nsymlink = devfs_vop_nsymlink,
142 .vop_open = vop_stdopen,
21864bc5 143 .vop_pathconf = vop_stdpathconf,
9f889dc4
MD
144 .vop_print = devfs_vop_print,
145 .vop_read = DEVFS_BADOP,
146 .vop_readdir = devfs_vop_readdir,
147 .vop_readlink = devfs_vop_readlink,
148 .vop_reclaim = devfs_vop_reclaim,
149 .vop_setattr = devfs_vop_setattr,
21864bc5
MD
150 .vop_write = DEVFS_BADOP,
151 .vop_ioctl = DEVFS_BADOP
152};
153
154/*
9f889dc4 155 * devfs vnode operations for character devices. All vnode ops are MPSAFE.
21864bc5
MD
156 */
157struct vop_ops devfs_vnode_dev_vops = {
158 .vop_default = vop_defaultop,
9f889dc4 159 .vop_access = devfs_vop_access,
21864bc5 160 .vop_advlock = devfs_spec_advlock,
9f889dc4 161 .vop_bmap = devfs_spec_bmap,
21864bc5
MD
162 .vop_close = devfs_spec_close,
163 .vop_freeblks = devfs_spec_freeblks,
164 .vop_fsync = devfs_spec_fsync,
9f889dc4 165 .vop_getattr = devfs_vop_getattr,
21864bc5 166 .vop_getpages = devfs_spec_getpages,
9f889dc4
MD
167 .vop_inactive = devfs_vop_inactive,
168 .vop_open = devfs_spec_open,
21864bc5 169 .vop_pathconf = vop_stdpathconf,
9f889dc4 170 .vop_print = devfs_vop_print,
21864bc5 171 .vop_kqfilter = devfs_spec_kqfilter,
9f889dc4 172 .vop_read = devfs_spec_read,
21864bc5
MD
173 .vop_readdir = DEVFS_BADOP,
174 .vop_readlink = DEVFS_BADOP,
9f889dc4
MD
175 .vop_reclaim = devfs_vop_reclaim,
176 .vop_setattr = devfs_vop_setattr,
21864bc5
MD
177 .vop_strategy = devfs_spec_strategy,
178 .vop_write = devfs_spec_write,
179 .vop_ioctl = devfs_spec_ioctl
180};
181
9f889dc4
MD
182/*
183 * devfs file pointer operations. All fileops are MPSAFE.
184 */
21864bc5
MD
185struct vop_ops *devfs_vnode_dev_vops_p = &devfs_vnode_dev_vops;
186
187struct fileops devfs_dev_fileops = {
9f889dc4
MD
188 .fo_read = devfs_fo_read,
189 .fo_write = devfs_fo_write,
190 .fo_ioctl = devfs_fo_ioctl,
191 .fo_kqfilter = devfs_fo_kqfilter,
192 .fo_stat = devfs_fo_stat,
193 .fo_close = devfs_fo_close,
194 .fo_shutdown = nofo_shutdown
21864bc5
MD
195};
196
4062d050 197/*
9f889dc4
MD
198 * These two functions are possibly temporary hacks for devices (aka
199 * the pty code) which want to control the node attributes themselves.
4062d050
MD
200 *
201 * XXX we may ultimately desire to simply remove the uid/gid/mode
202 * from the node entirely.
9f889dc4
MD
203 *
204 * MPSAFE - sorta. Theoretically the overwrite can compete since they
205 * are loading from the same fields.
4062d050
MD
206 */
207static __inline void
208node_sync_dev_get(struct devfs_node *node)
209{
210 cdev_t dev;
211
212 if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) {
213 node->uid = dev->si_uid;
214 node->gid = dev->si_gid;
215 node->mode = dev->si_perms;
216 }
217}
218
219static __inline void
220node_sync_dev_set(struct devfs_node *node)
221{
222 cdev_t dev;
223
224 if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) {
225 dev->si_uid = node->uid;
226 dev->si_gid = node->gid;
227 dev->si_perms = node->mode;
228 }
229}
21864bc5
MD
230
231/*
232 * generic entry point for unsupported operations
233 */
234static int
9f889dc4 235devfs_vop_badop(struct vop_generic_args *ap)
21864bc5 236{
21864bc5
MD
237 return (EIO);
238}
239
240
241static int
9f889dc4 242devfs_vop_access(struct vop_access_args *ap)
21864bc5
MD
243{
244 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
898c91ee 245 int error;
21864bc5 246
894bbb25
AH
247 if (!devfs_node_is_accessible(node))
248 return ENOENT;
4062d050 249 node_sync_dev_get(node);
21864bc5 250 error = vop_helper_access(ap, node->uid, node->gid,
4062d050 251 node->mode, node->flags);
21864bc5 252
21864bc5 253 return error;
21864bc5
MD
254}
255
256
257static int
9f889dc4 258devfs_vop_inactive(struct vop_inactive_args *ap)
21864bc5 259{
ca8d7677 260 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
21864bc5 261
ca8d7677
MD
262 if (node == NULL || (node->flags & DEVFS_NODE_LINKED) == 0)
263 vrecycle(ap->a_vp);
21864bc5
MD
264 return 0;
265}
266
267
268static int
9f889dc4 269devfs_vop_reclaim(struct vop_reclaim_args *ap)
21864bc5 270{
be6f2e86
MD
271 struct devfs_node *node;
272 struct vnode *vp;
273 int locked;
274
be6f2e86
MD
275 /*
276 * Check if it is locked already. if not, we acquire the devfs lock
277 */
21864bc5
MD
278 if (!(lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE) {
279 lockmgr(&devfs_lock, LK_EXCLUSIVE);
280 locked = 1;
be6f2e86
MD
281 } else {
282 locked = 0;
21864bc5
MD
283 }
284
be6f2e86
MD
285 /*
286 * Get rid of the devfs_node if it is no longer linked into the
287 * topology.
288 */
289 vp = ap->a_vp;
290 if ((node = DEVFS_NODE(vp)) != NULL) {
4062d050
MD
291 node->v_node = NULL;
292 if ((node->flags & DEVFS_NODE_LINKED) == 0)
be6f2e86 293 devfs_freep(node);
21864bc5
MD
294 }
295
21864bc5
MD
296 if (locked)
297 lockmgr(&devfs_lock, LK_RELEASE);
298
be6f2e86 299 /*
9b823501
AH
300 * v_rdev needs to be properly released using v_release_rdev
301 * Make sure v_data is NULL as well.
be6f2e86
MD
302 */
303 vp->v_data = NULL;
9b823501 304 v_release_rdev(vp);
21864bc5
MD
305 return 0;
306}
307
308
309static int
9f889dc4 310devfs_vop_readdir(struct vop_readdir_args *ap)
21864bc5 311{
898c91ee 312 struct devfs_node *dnode = DEVFS_NODE(ap->a_vp);
21864bc5 313 struct devfs_node *node;
21864bc5
MD
314 int cookie_index;
315 int ncookies;
898c91ee
MD
316 int error2;
317 int error;
318 int r;
21864bc5
MD
319 off_t *cookies;
320 off_t saveoff;
321
322 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_readdir() called!\n");
323
324 if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX)
325 return (EINVAL);
326 if ((error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
327 return (error);
328
c512ab96
MD
329 if (!devfs_node_is_accessible(dnode)) {
330 vn_unlock(ap->a_vp);
ca8d7677 331 return ENOENT;
c512ab96 332 }
ca8d7677
MD
333
334 lockmgr(&devfs_lock, LK_EXCLUSIVE);
335
21864bc5
MD
336 saveoff = ap->a_uio->uio_offset;
337
338 if (ap->a_ncookies) {
339 ncookies = ap->a_uio->uio_resid / 16 + 1; /* Why / 16 ?? */
340 if (ncookies > 256)
341 ncookies = 256;
342 cookies = kmalloc(256 * sizeof(off_t), M_TEMP, M_WAITOK);
343 cookie_index = 0;
344 } else {
345 ncookies = -1;
346 cookies = NULL;
347 cookie_index = 0;
348 }
349
898c91ee 350 nanotime(&dnode->atime);
21864bc5
MD
351
352 if (saveoff == 0) {
898c91ee
MD
353 r = vop_write_dirent(&error, ap->a_uio, dnode->d_dir.d_ino,
354 DT_DIR, 1, ".");
21864bc5
MD
355 if (r)
356 goto done;
357 if (cookies)
358 cookies[cookie_index] = saveoff;
359 saveoff++;
360 cookie_index++;
361 if (cookie_index == ncookies)
362 goto done;
363 }
364
365 if (saveoff == 1) {
898c91ee 366 if (dnode->parent) {
21864bc5 367 r = vop_write_dirent(&error, ap->a_uio,
898c91ee 368 dnode->parent->d_dir.d_ino,
21864bc5
MD
369 DT_DIR, 2, "..");
370 } else {
371 r = vop_write_dirent(&error, ap->a_uio,
898c91ee
MD
372 dnode->d_dir.d_ino,
373 DT_DIR, 2, "..");
21864bc5
MD
374 }
375 if (r)
376 goto done;
377 if (cookies)
378 cookies[cookie_index] = saveoff;
379 saveoff++;
380 cookie_index++;
381 if (cookie_index == ncookies)
382 goto done;
383 }
384
898c91ee
MD
385 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
386 if ((node->flags & DEVFS_HIDDEN) ||
387 (node->flags & DEVFS_INVISIBLE)) {
21864bc5 388 continue;
898c91ee 389 }
21864bc5 390
f7e8960c 391 /*
9f889dc4
MD
392 * If the node type is a valid devfs alias, then we make
393 * sure that the target isn't hidden. If it is, we don't
394 * show the link in the directory listing.
f7e8960c
AH
395 */
396 if ((node->node_type == Plink) && (node->link_target != NULL) &&
397 (node->link_target->flags & DEVFS_HIDDEN))
21864bc5 398 continue;
f7e8960c
AH
399
400 if (node->cookie < saveoff)
21864bc5 401 continue;
f7e8960c 402
21864bc5
MD
403 saveoff = node->cookie;
404
898c91ee
MD
405 error2 = vop_write_dirent(&error, ap->a_uio, node->d_dir.d_ino,
406 node->d_dir.d_type,
407 node->d_dir.d_namlen,
408 node->d_dir.d_name);
21864bc5 409
898c91ee 410 if (error2)
21864bc5
MD
411 break;
412
413 saveoff++;
414
415 if (cookies)
416 cookies[cookie_index] = node->cookie;
417 ++cookie_index;
418 if (cookie_index == ncookies)
419 break;
21864bc5
MD
420 }
421
422done:
ca8d7677 423 lockmgr(&devfs_lock, LK_RELEASE);
21864bc5
MD
424 vn_unlock(ap->a_vp);
425
426 ap->a_uio->uio_offset = saveoff;
427 if (error && cookie_index == 0) {
428 if (cookies) {
429 kfree(cookies, M_TEMP);
430 *ap->a_ncookies = 0;
431 *ap->a_cookies = NULL;
432 }
433 } else {
434 if (cookies) {
435 *ap->a_ncookies = cookie_index;
436 *ap->a_cookies = cookies;
437 }
438 }
439 return (error);
440}
441
442
443static int
9f889dc4 444devfs_vop_nresolve(struct vop_nresolve_args *ap)
21864bc5 445{
898c91ee 446 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
21864bc5
MD
447 struct devfs_node *node, *found = NULL;
448 struct namecache *ncp;
449 struct vnode *vp = NULL;
21864bc5
MD
450 int error = 0;
451 int len;
260e4e8b 452 int depth;
21864bc5 453
21864bc5
MD
454 ncp = ap->a_nch->ncp;
455 len = ncp->nc_nlen;
456
898c91ee 457 if (!devfs_node_is_accessible(dnode))
ca8d7677
MD
458 return ENOENT;
459
21864bc5
MD
460 lockmgr(&devfs_lock, LK_EXCLUSIVE);
461
898c91ee 462 if ((dnode->node_type != Proot) && (dnode->node_type != Pdir)) {
e23485a5 463 error = ENOENT;
21864bc5
MD
464 cache_setvp(ap->a_nch, NULL);
465 goto out;
466 }
467
898c91ee 468 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
21864bc5
MD
469 if (len == node->d_dir.d_namlen) {
470 if (!memcmp(ncp->nc_name, node->d_dir.d_name, len)) {
21864bc5
MD
471 found = node;
472 break;
473 }
474 }
475 }
476
477 if (found) {
260e4e8b
AH
478 depth = 0;
479 while ((found->node_type == Plink) && (found->link_target)) {
480 if (depth >= 8) {
481 devfs_debug(DEVFS_DEBUG_SHOW, "Recursive link or depth >= 8");
482 break;
483 }
484
21864bc5 485 found = found->link_target;
260e4e8b
AH
486 ++depth;
487 }
21864bc5
MD
488
489 if (!(found->flags & DEVFS_HIDDEN))
490 devfs_allocv(/*ap->a_dvp->v_mount, */ &vp, found);
21864bc5
MD
491 }
492
21864bc5 493 if (vp == NULL) {
21864bc5
MD
494 error = ENOENT;
495 cache_setvp(ap->a_nch, NULL);
21864bc5
MD
496 goto out;
497
498 }
21864bc5
MD
499 KKASSERT(vp);
500 vn_unlock(vp);
501 cache_setvp(ap->a_nch, vp);
502 vrele(vp);
21864bc5 503out:
21864bc5 504 lockmgr(&devfs_lock, LK_RELEASE);
898c91ee 505
21864bc5
MD
506 return error;
507}
508
509
510static int
9f889dc4 511devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
21864bc5 512{
898c91ee 513 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
21864bc5 514
898c91ee
MD
515 *ap->a_vpp = NULL;
516 if (!devfs_node_is_accessible(dnode))
894bbb25
AH
517 return ENOENT;
518
21864bc5 519 lockmgr(&devfs_lock, LK_EXCLUSIVE);
898c91ee
MD
520 if (dnode->parent != NULL) {
521 devfs_allocv(ap->a_vpp, dnode->parent);
21864bc5
MD
522 vn_unlock(*ap->a_vpp);
523 }
524 lockmgr(&devfs_lock, LK_RELEASE);
525
526 return ((*ap->a_vpp == NULL) ? ENOENT : 0);
527}
528
529
530static int
9f889dc4 531devfs_vop_getattr(struct vop_getattr_args *ap)
21864bc5 532{
21864bc5 533 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
898c91ee 534 struct vattr *vap = ap->a_vap;
2d076755 535 struct partinfo pinfo;
21864bc5
MD
536 int error = 0;
537
952f0188 538#if 0
894bbb25 539 if (!devfs_node_is_accessible(node))
ca8d7677 540 return ENOENT;
952f0188 541#endif
4062d050 542 node_sync_dev_get(node);
ca8d7677 543
ca8d7677 544 lockmgr(&devfs_lock, LK_EXCLUSIVE);
21864bc5
MD
545
546 /* start by zeroing out the attributes */
547 VATTR_NULL(vap);
548
549 /* next do all the common fields */
550 vap->va_type = ap->a_vp->v_type;
551 vap->va_mode = node->mode;
552 vap->va_fileid = DEVFS_NODE(ap->a_vp)->d_dir.d_ino ;
a3c7ebb9 553 vap->va_flags = 0;
21864bc5 554 vap->va_blocksize = DEV_BSIZE;
a3c7ebb9 555 vap->va_bytes = vap->va_size = 0;
21864bc5 556
21864bc5
MD
557 vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
558
21864bc5
MD
559 vap->va_atime = node->atime;
560 vap->va_mtime = node->mtime;
561 vap->va_ctime = node->ctime;
562
563 vap->va_nlink = 1; /* number of references to file */
564
565 vap->va_uid = node->uid;
566 vap->va_gid = node->gid;
567
568 vap->va_rmajor = 0;
569 vap->va_rminor = 0;
570
898c91ee
MD
571 if ((node->node_type == Pdev) && node->d_dev) {
572 reference_dev(node->d_dev);
573 vap->va_rminor = node->d_dev->si_uminor;
574 release_dev(node->d_dev);
21864bc5
MD
575 }
576
577 /* For a softlink the va_size is the length of the softlink */
898c91ee 578 if (node->symlink_name != 0) {
2d076755 579 vap->va_bytes = vap->va_size = node->symlink_namelen;
21864bc5 580 }
2d076755
AH
581
582 /*
583 * For a disk-type device, va_size is the size of the underlying
584 * device, so that lseek() works properly.
585 */
586 if ((node->d_dev) && (dev_dflags(node->d_dev) & D_DISK)) {
587 bzero(&pinfo, sizeof(pinfo));
588 error = dev_dioctl(node->d_dev, DIOCGPART, (void *)&pinfo,
589 0, proc0.p_ucred, NULL);
590 if ((error == 0) && (pinfo.media_blksize != 0)) {
591 vap->va_size = pinfo.media_size;
592 } else {
593 vap->va_size = 0;
594 error = 0;
595 }
596 }
597
ca8d7677 598 lockmgr(&devfs_lock, LK_RELEASE);
898c91ee 599
894bbb25 600 return (error);
21864bc5
MD
601}
602
603
604static int
9f889dc4 605devfs_vop_setattr(struct vop_setattr_args *ap)
21864bc5 606{
898c91ee 607 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
21864bc5
MD
608 struct vattr *vap;
609 int error = 0;
610
894bbb25 611 if (!devfs_node_is_accessible(node))
ca8d7677 612 return ENOENT;
4062d050 613 node_sync_dev_get(node);
21864bc5 614
21864bc5
MD
615 lockmgr(&devfs_lock, LK_EXCLUSIVE);
616
617 vap = ap->a_vap;
21864bc5
MD
618
619 if (vap->va_uid != (uid_t)VNOVAL) {
620 if ((ap->a_cred->cr_uid != node->uid) &&
898c91ee 621 (!groupmember(node->gid, ap->a_cred))) {
21864bc5 622 error = priv_check(curthread, PRIV_VFS_CHOWN);
898c91ee 623 if (error)
21864bc5 624 goto out;
21864bc5
MD
625 }
626 node->uid = vap->va_uid;
627 }
628
629 if (vap->va_gid != (uid_t)VNOVAL) {
630 if ((ap->a_cred->cr_uid != node->uid) &&
898c91ee 631 (!groupmember(node->gid, ap->a_cred))) {
21864bc5 632 error = priv_check(curthread, PRIV_VFS_CHOWN);
898c91ee 633 if (error)
21864bc5 634 goto out;
21864bc5
MD
635 }
636 node->gid = vap->va_gid;
637 }
638
639 if (vap->va_mode != (mode_t)VNOVAL) {
640 if (ap->a_cred->cr_uid != node->uid) {
641 error = priv_check(curthread, PRIV_VFS_ADMIN);
898c91ee 642 if (error)
21864bc5 643 goto out;
21864bc5
MD
644 }
645 node->mode = vap->va_mode;
646 }
647
648out:
4062d050 649 node_sync_dev_set(node);
07dfa375 650 nanotime(&node->ctime);
21864bc5 651 lockmgr(&devfs_lock, LK_RELEASE);
898c91ee 652
21864bc5
MD
653 return error;
654}
655
656
657static int
9f889dc4 658devfs_vop_readlink(struct vop_readlink_args *ap)
21864bc5
MD
659{
660 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
ca8d7677
MD
661 int ret;
662
894bbb25 663 if (!devfs_node_is_accessible(node))
ca8d7677 664 return ENOENT;
21864bc5 665
ca8d7677
MD
666 lockmgr(&devfs_lock, LK_EXCLUSIVE);
667 ret = uiomove(node->symlink_name, node->symlink_namelen, ap->a_uio);
668 lockmgr(&devfs_lock, LK_RELEASE);
669
670 return ret;
21864bc5
MD
671}
672
673
674static int
9f889dc4 675devfs_vop_print(struct vop_print_args *ap)
21864bc5 676{
21864bc5
MD
677 return (0);
678}
679
39a08947 680static int
9f889dc4 681devfs_vop_nmkdir(struct vop_nmkdir_args *ap)
39a08947
AH
682{
683 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
684 struct devfs_node *node;
685
686 if (!devfs_node_is_accessible(dnode))
687 return ENOENT;
688
689 if ((dnode->node_type != Proot) && (dnode->node_type != Pdir))
690 goto out;
691
692 lockmgr(&devfs_lock, LK_EXCLUSIVE);
693 devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Pdir,
694 ap->a_nch->ncp->nc_name, dnode, NULL);
695
696 if (*ap->a_vpp) {
697 node = DEVFS_NODE(*ap->a_vpp);
698 node->flags |= DEVFS_USER_CREATED;
699 cache_setunresolved(ap->a_nch);
700 cache_setvp(ap->a_nch, *ap->a_vpp);
701 }
702 lockmgr(&devfs_lock, LK_RELEASE);
703out:
704 return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
705}
21864bc5
MD
706
707static int
9f889dc4 708devfs_vop_nsymlink(struct vop_nsymlink_args *ap)
21864bc5 709{
898c91ee
MD
710 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
711 struct devfs_node *node;
712 size_t targetlen;
21864bc5 713
898c91ee 714 if (!devfs_node_is_accessible(dnode))
ca8d7677
MD
715 return ENOENT;
716
894bbb25
AH
717 ap->a_vap->va_type = VLNK;
718
898c91ee 719 if ((dnode->node_type != Proot) && (dnode->node_type != Pdir))
21864bc5 720 goto out;
898c91ee 721
21864bc5
MD
722 lockmgr(&devfs_lock, LK_EXCLUSIVE);
723 devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Plink,
898c91ee 724 ap->a_nch->ncp->nc_name, dnode, NULL);
21864bc5 725
898c91ee 726 targetlen = strlen(ap->a_target);
21864bc5 727 if (*ap->a_vpp) {
898c91ee
MD
728 node = DEVFS_NODE(*ap->a_vpp);
729 node->flags |= DEVFS_USER_CREATED;
730 node->symlink_namelen = targetlen;
731 node->symlink_name = kmalloc(targetlen + 1, M_DEVFS, M_WAITOK);
732 memcpy(node->symlink_name, ap->a_target, targetlen);
733 node->symlink_name[targetlen] = '\0';
21864bc5 734 cache_setunresolved(ap->a_nch);
21864bc5
MD
735 cache_setvp(ap->a_nch, *ap->a_vpp);
736 }
737 lockmgr(&devfs_lock, LK_RELEASE);
738out:
739 return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
21864bc5
MD
740}
741
39a08947 742static int
9f889dc4 743devfs_vop_nrmdir(struct vop_nrmdir_args *ap)
39a08947
AH
744{
745 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
746 struct devfs_node *node;
747 struct namecache *ncp;
748 int error = ENOENT;
749
750 ncp = ap->a_nch->ncp;
751
752 if (!devfs_node_is_accessible(dnode))
753 return ENOENT;
754
755 lockmgr(&devfs_lock, LK_EXCLUSIVE);
756
757 if ((dnode->node_type != Proot) && (dnode->node_type != Pdir))
758 goto out;
759
760 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
761 if (ncp->nc_nlen != node->d_dir.d_namlen)
762 continue;
763 if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
764 continue;
765
766 /*
767 * only allow removal of user created dirs
768 */
769 if ((node->flags & DEVFS_USER_CREATED) == 0) {
770 error = EPERM;
771 goto out;
772 } else if (node->node_type != Pdir) {
773 error = ENOTDIR;
774 goto out;
775 } else if (node->nchildren > 2) {
776 error = ENOTEMPTY;
777 goto out;
778 } else {
779 if (node->v_node)
780 cache_inval_vp(node->v_node, CINV_DESTROY);
781 devfs_unlinkp(node);
782 error = 0;
783 break;
784 }
785 }
786
787 cache_setunresolved(ap->a_nch);
788 cache_setvp(ap->a_nch, NULL);
789
790out:
791 lockmgr(&devfs_lock, LK_RELEASE);
792 return error;
793}
21864bc5
MD
794
795static int
9f889dc4 796devfs_vop_nremove(struct vop_nremove_args *ap)
21864bc5 797{
898c91ee 798 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
21864bc5
MD
799 struct devfs_node *node;
800 struct namecache *ncp;
21864bc5
MD
801 int error = ENOENT;
802
21864bc5
MD
803 ncp = ap->a_nch->ncp;
804
898c91ee 805 if (!devfs_node_is_accessible(dnode))
ca8d7677
MD
806 return ENOENT;
807
21864bc5
MD
808 lockmgr(&devfs_lock, LK_EXCLUSIVE);
809
898c91ee 810 if ((dnode->node_type != Proot) && (dnode->node_type != Pdir))
21864bc5 811 goto out;
21864bc5 812
898c91ee
MD
813 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
814 if (ncp->nc_nlen != node->d_dir.d_namlen)
815 continue;
816 if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
817 continue;
21864bc5 818
898c91ee
MD
819 /*
820 * only allow removal of user created stuff (e.g. symlinks)
821 */
822 if ((node->flags & DEVFS_USER_CREATED) == 0) {
823 error = EPERM;
824 goto out;
39a08947
AH
825 } else if (node->node_type == Pdir) {
826 error = EISDIR;
827 goto out;
898c91ee
MD
828 } else {
829 if (node->v_node)
830 cache_inval_vp(node->v_node, CINV_DESTROY);
831 devfs_unlinkp(node);
832 error = 0;
833 break;
21864bc5
MD
834 }
835 }
836
837 cache_setunresolved(ap->a_nch);
838 cache_setvp(ap->a_nch, NULL);
21864bc5
MD
839
840out:
841 lockmgr(&devfs_lock, LK_RELEASE);
21864bc5
MD
842 return error;
843}
844
845
846static int
847devfs_spec_open(struct vop_open_args *ap)
848{
849 struct vnode *vp = ap->a_vp;
ca8d7677 850 struct vnode *orig_vp = NULL;
898c91ee
MD
851 struct devfs_node *node = DEVFS_NODE(vp);
852 struct devfs_node *newnode;
21864bc5 853 cdev_t dev, ndev = NULL;
21864bc5 854 int error = 0;
21864bc5 855
898c91ee
MD
856 if (node) {
857 if (node->d_dev == NULL)
21864bc5 858 return ENXIO;
898c91ee 859 if (!devfs_node_is_accessible(node))
894bbb25 860 return ENOENT;
21864bc5
MD
861 }
862
21864bc5
MD
863 if ((dev = vp->v_rdev) == NULL)
864 return ENXIO;
865
898c91ee 866 if (node && ap->a_fp) {
21864bc5
MD
867 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -1.1-\n");
868 lockmgr(&devfs_lock, LK_EXCLUSIVE);
07dfa375
AH
869
870 ndev = devfs_clone(dev, node->d_dir.d_name, node->d_dir.d_namlen,
871 ap->a_mode, ap->a_cred);
872 if (ndev != NULL) {
898c91ee
MD
873 newnode = devfs_create_device_node(
874 DEVFS_MNTDATA(vp->v_mount)->root_node,
07dfa375
AH
875 ndev, NULL, NULL);
876 /* XXX: possibly destroy device if this happens */
898c91ee 877
07dfa375
AH
878 if (newnode != NULL) {
879 dev = ndev;
880 devfs_link_dev(dev);
21864bc5 881
07dfa375
AH
882 devfs_debug(DEVFS_DEBUG_DEBUG,
883 "parent here is: %s, node is: |%s|\n",
884 ((node->parent->node_type == Proot) ?
885 "ROOT!" : node->parent->d_dir.d_name),
886 newnode->d_dir.d_name);
887 devfs_debug(DEVFS_DEBUG_DEBUG,
888 "test: %s\n",
889 ((struct devfs_node *)(TAILQ_LAST(DEVFS_DENODE_HEAD(node->parent), devfs_node_head)))->d_dir.d_name);
890
891 /*
892 * orig_vp is set to the original vp if we cloned.
893 */
894 /* node->flags |= DEVFS_CLONED; */
895 devfs_allocv(&vp, newnode);
896 orig_vp = ap->a_vp;
897 ap->a_vp = vp;
898 }
21864bc5
MD
899 }
900 lockmgr(&devfs_lock, LK_RELEASE);
901 }
902
898c91ee
MD
903 devfs_debug(DEVFS_DEBUG_DEBUG,
904 "devfs_spec_open() called on %s! \n",
905 dev->si_name);
906
21864bc5
MD
907 /*
908 * Make this field valid before any I/O in ->d_open
909 */
910 if (!dev->si_iosize_max)
911 dev->si_iosize_max = DFLTPHYS;
912
913 if (dev_dflags(dev) & D_TTY)
2247fe02 914 vsetflags(vp, VISTTY);
21864bc5 915
9f889dc4
MD
916 /*
917 * Open underlying device
918 */
21864bc5
MD
919 vn_unlock(vp);
920 error = dev_dopen(dev, ap->a_mode, S_IFCHR, ap->a_cred);
921 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
922
ca8d7677
MD
923 /*
924 * Clean up any cloned vp if we error out.
925 */
21864bc5 926 if (error) {
ca8d7677 927 if (orig_vp) {
21864bc5 928 vput(vp);
ca8d7677
MD
929 ap->a_vp = orig_vp;
930 /* orig_vp = NULL; */
931 }
21864bc5
MD
932 return error;
933 }
934
d894b0eb 935 /*
ddd7de82
AH
936 * This checks if the disk device is going to be opened for writing.
937 * It will be only allowed in the cases where securelevel permits it
938 * and it's not mounted R/W.
d894b0eb 939 */
ddd7de82
AH
940 if ((dev_dflags(dev) & D_DISK) && (ap->a_mode & FWRITE) &&
941 (ap->a_cred != FSCRED)) {
942
943 /* Very secure mode. No open for writing allowed */
944 if (securelevel >= 2)
945 return EPERM;
946
947 /*
948 * If it is mounted R/W, do not allow to open for writing.
949 * In the case it's mounted read-only but securelevel
950 * is >= 1, then do not allow opening for writing either.
951 */
952 if (vfs_mountedon(vp)) {
953 if (!(dev->si_mountpoint->mnt_flag & MNT_RDONLY))
954 return EBUSY;
955 else if (securelevel >= 1)
d894b0eb
AH
956 return EPERM;
957 }
958 }
21864bc5
MD
959
960 if (dev_dflags(dev) & D_TTY) {
961 if (dev->si_tty) {
962 struct tty *tp;
963 tp = dev->si_tty;
964 if (!tp->t_stop) {
898c91ee
MD
965 devfs_debug(DEVFS_DEBUG_DEBUG,
966 "devfs: no t_stop\n");
21864bc5
MD
967 tp->t_stop = nottystop;
968 }
969 }
970 }
971
972
973 if (vn_isdisk(vp, NULL)) {
974 if (!dev->si_bsize_phys)
975 dev->si_bsize_phys = DEV_BSIZE;
b0d18f7d 976 vinitvmio(vp, IDX_TO_OFF(INT_MAX), PAGE_SIZE, -1);
21864bc5
MD
977 }
978
979 vop_stdopen(ap);
07dfa375 980#if 0
898c91ee
MD
981 if (node)
982 nanotime(&node->atime);
07dfa375 983#endif
21864bc5 984
b80b4c32
MD
985 /*
986 * If we replaced the vp the vop_stdopen() call will have loaded
987 * it into fp->f_data and vref()d the vp, giving us two refs. So
988 * instead of just unlocking it here we have to vput() it.
989 */
ca8d7677 990 if (orig_vp)
b80b4c32 991 vput(vp);
21864bc5
MD
992
993 /* Ugly pty magic, to make pty devices appear once they are opened */
898c91ee
MD
994 if (node && (node->flags & DEVFS_PTY) == DEVFS_PTY)
995 node->flags &= ~DEVFS_INVISIBLE;
21864bc5
MD
996
997 if (ap->a_fp) {
b80b4c32 998 KKASSERT(ap->a_fp->f_type == DTYPE_VNODE);
fb12e9a5 999 KKASSERT((ap->a_fp->f_flag & FMASK) == (ap->a_mode & FMASK));
21864bc5 1000 ap->a_fp->f_ops = &devfs_dev_fileops;
b80b4c32 1001 KKASSERT(ap->a_fp->f_data == (void *)vp);
21864bc5
MD
1002 }
1003
21864bc5
MD
1004 return 0;
1005}
1006
1007
1008static int
1009devfs_spec_close(struct vop_close_args *ap)
1010{
92fb0c6a 1011 struct devfs_node *node;
21864bc5
MD
1012 struct proc *p = curproc;
1013 struct vnode *vp = ap->a_vp;
1014 cdev_t dev = vp->v_rdev;
1015 int error = 0;
1016 int needrelock;
1017
52f98df9
SG
1018 if (dev)
1019 devfs_debug(DEVFS_DEBUG_DEBUG,
1020 "devfs_spec_close() called on %s! \n",
1021 dev->si_name);
1022 else
1023 devfs_debug(DEVFS_DEBUG_DEBUG,
1024 "devfs_spec_close() called, null vode!\n");
21864bc5
MD
1025
1026 /*
1027 * A couple of hacks for devices and tty devices. The
1028 * vnode ref count cannot be used to figure out the
1029 * last close, but we can use v_opencount now that
1030 * revoke works properly.
1031 *
1032 * Detect the last close on a controlling terminal and clear
1033 * the session (half-close).
1034 */
1035 if (dev)
1036 reference_dev(dev);
1037
1038 if (p && vp->v_opencount <= 1 && vp == p->p_session->s_ttyvp) {
1039 p->p_session->s_ttyvp = NULL;
1040 vrele(vp);
1041 }
1042
1043 /*
1044 * Vnodes can be opened and closed multiple times. Do not really
1045 * close the device unless (1) it is being closed forcibly,
1046 * (2) the device wants to track closes, or (3) this is the last
1047 * vnode doing its last close on the device.
1048 *
1049 * XXX the VXLOCK (force close) case can leave vnodes referencing
1050 * a closed device. This might not occur now that our revoke is
1051 * fixed.
1052 */
1053 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -1- \n");
1054 if (dev && ((vp->v_flag & VRECLAIMED) ||
1055 (dev_dflags(dev) & D_TRACKCLOSE) ||
1056 (vp->v_opencount == 1))) {
898c91ee 1057 /*
92fb0c6a
MD
1058 * Ugly pty magic, to make pty devices disappear again once
1059 * they are closed.
1060 */
1061 node = DEVFS_NODE(ap->a_vp);
1062 if (node && (node->flags & DEVFS_PTY))
1063 node->flags |= DEVFS_INVISIBLE;
1064
1065 /*
898c91ee
MD
1066 * Unlock around dev_dclose()
1067 */
21864bc5
MD
1068 needrelock = 0;
1069 if (vn_islocked(vp)) {
1070 needrelock = 1;
1071 vn_unlock(vp);
1072 }
898c91ee
MD
1073
1074 /*
92fb0c6a
MD
1075 * WARNING! If the device destroys itself the devfs node
1076 * can disappear here.
898c91ee 1077 */
92fb0c6a
MD
1078 error = dev_dclose(dev, ap->a_fflag, S_IFCHR);
1079 /* node is now stale */
21864bc5
MD
1080
1081 if (needrelock)
1082 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1083 } else {
1084 error = 0;
1085 }
1086 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -2- \n");
898c91ee 1087
21864bc5
MD
1088 /*
1089 * Track the actual opens and closes on the vnode. The last close
898c91ee
MD
1090 * disassociates the rdev. If the rdev is already disassociated or
1091 * the opencount is already 0, the vnode might have been revoked
1092 * and no further opencount tracking occurs.
21864bc5 1093 */
898c91ee 1094 if (dev)
21864bc5 1095 release_dev(dev);
898c91ee 1096 if (vp->v_opencount > 0)
21864bc5 1097 vop_stdclose(ap);
21864bc5
MD
1098 return(error);
1099
1100}
1101
1102
1103static int
9f889dc4 1104devfs_fo_close(struct file *fp)
21864bc5 1105{
21864bc5 1106 struct vnode *vp = (struct vnode *)fp->f_data;
898c91ee 1107 int error;
21864bc5 1108
21864bc5 1109 fp->f_ops = &badfileops;
21864bc5 1110 error = vn_close(vp, fp->f_flag);
21864bc5
MD
1111
1112 return (error);
1113}
1114
1115
1116/*
1117 * Device-optimized file table vnode read routine.
1118 *
1119 * This bypasses the VOP table and talks directly to the device. Most
1120 * filesystems just route to specfs and can make this optimization.
1121 *
1122 * MPALMOSTSAFE - acquires mplock
1123 */
1124static int
9f889dc4 1125devfs_fo_read(struct file *fp, struct uio *uio,
898c91ee 1126 struct ucred *cred, int flags)
21864bc5 1127{
898c91ee 1128 struct devfs_node *node;
21864bc5
MD
1129 struct vnode *vp;
1130 int ioflag;
1131 int error;
1132 cdev_t dev;
1133
21864bc5
MD
1134 KASSERT(uio->uio_td == curthread,
1135 ("uio_td %p is not td %p", uio->uio_td, curthread));
1136
3a1032a6
AH
1137 if (uio->uio_resid == 0)
1138 return 0;
1139
21864bc5 1140 vp = (struct vnode *)fp->f_data;
3a1032a6
AH
1141 if (vp == NULL || vp->v_type == VBAD)
1142 return EBADF;
1143
898c91ee 1144 node = DEVFS_NODE(vp);
21864bc5 1145
3a1032a6
AH
1146 if ((dev = vp->v_rdev) == NULL)
1147 return EBADF;
1148
21864bc5
MD
1149 reference_dev(dev);
1150
21864bc5
MD
1151 if ((flags & O_FOFFSET) == 0)
1152 uio->uio_offset = fp->f_offset;
1153
1154 ioflag = 0;
1155 if (flags & O_FBLOCKING) {
1156 /* ioflag &= ~IO_NDELAY; */
1157 } else if (flags & O_FNONBLOCKING) {
1158 ioflag |= IO_NDELAY;
1159 } else if (fp->f_flag & FNONBLOCK) {
1160 ioflag |= IO_NDELAY;
1161 }
1162 if (flags & O_FBUFFERED) {
1163 /* ioflag &= ~IO_DIRECT; */
1164 } else if (flags & O_FUNBUFFERED) {
1165 ioflag |= IO_DIRECT;
1166 } else if (fp->f_flag & O_DIRECT) {
1167 ioflag |= IO_DIRECT;
1168 }
1169 ioflag |= sequential_heuristic(uio, fp);
1170
1171 error = dev_dread(dev, uio, ioflag);
1172
1173 release_dev(dev);
898c91ee
MD
1174 if (node)
1175 nanotime(&node->atime);
21864bc5
MD
1176 if ((flags & O_FOFFSET) == 0)
1177 fp->f_offset = uio->uio_offset;
1178 fp->f_nextoff = uio->uio_offset;
3a1032a6 1179
21864bc5
MD
1180 return (error);
1181}
1182
1183
1184static int
9f889dc4 1185devfs_fo_write(struct file *fp, struct uio *uio,
898c91ee 1186 struct ucred *cred, int flags)
21864bc5 1187{
898c91ee 1188 struct devfs_node *node;
21864bc5
MD
1189 struct vnode *vp;
1190 int ioflag;
1191 int error;
1192 cdev_t dev;
1193
21864bc5
MD
1194 KASSERT(uio->uio_td == curthread,
1195 ("uio_td %p is not p %p", uio->uio_td, curthread));
1196
1197 vp = (struct vnode *)fp->f_data;
3a1032a6
AH
1198 if (vp == NULL || vp->v_type == VBAD)
1199 return EBADF;
1200
898c91ee 1201 node = DEVFS_NODE(vp);
3a1032a6 1202
21864bc5
MD
1203 if (vp->v_type == VREG)
1204 bwillwrite(uio->uio_resid);
3a1032a6 1205
21864bc5
MD
1206 vp = (struct vnode *)fp->f_data;
1207
3a1032a6
AH
1208 if ((dev = vp->v_rdev) == NULL)
1209 return EBADF;
1210
21864bc5
MD
1211 reference_dev(dev);
1212
1213 if ((flags & O_FOFFSET) == 0)
1214 uio->uio_offset = fp->f_offset;
1215
1216 ioflag = IO_UNIT;
1217 if (vp->v_type == VREG &&
1218 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
1219 ioflag |= IO_APPEND;
1220 }
1221
1222 if (flags & O_FBLOCKING) {
1223 /* ioflag &= ~IO_NDELAY; */
1224 } else if (flags & O_FNONBLOCKING) {
1225 ioflag |= IO_NDELAY;
1226 } else if (fp->f_flag & FNONBLOCK) {
1227 ioflag |= IO_NDELAY;
1228 }
1229 if (flags & O_FBUFFERED) {
1230 /* ioflag &= ~IO_DIRECT; */
1231 } else if (flags & O_FUNBUFFERED) {
1232 ioflag |= IO_DIRECT;
1233 } else if (fp->f_flag & O_DIRECT) {
1234 ioflag |= IO_DIRECT;
1235 }
1236 if (flags & O_FASYNCWRITE) {
1237 /* ioflag &= ~IO_SYNC; */
1238 } else if (flags & O_FSYNCWRITE) {
1239 ioflag |= IO_SYNC;
1240 } else if (fp->f_flag & O_FSYNC) {
1241 ioflag |= IO_SYNC;
1242 }
1243
1244 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
1245 ioflag |= IO_SYNC;
1246 ioflag |= sequential_heuristic(uio, fp);
1247
1248 error = dev_dwrite(dev, uio, ioflag);
1249
1250 release_dev(dev);
07dfa375
AH
1251 if (node) {
1252 nanotime(&node->atime);
898c91ee 1253 nanotime(&node->mtime);
07dfa375 1254 }
21864bc5
MD
1255
1256 if ((flags & O_FOFFSET) == 0)
1257 fp->f_offset = uio->uio_offset;
1258 fp->f_nextoff = uio->uio_offset;
3a1032a6 1259
21864bc5
MD
1260 return (error);
1261}
1262
1263
1264static int
9f889dc4 1265devfs_fo_stat(struct file *fp, struct stat *sb, struct ucred *cred)
21864bc5
MD
1266{
1267 struct vnode *vp;
3a1032a6
AH
1268 struct vattr vattr;
1269 struct vattr *vap;
1270 u_short mode;
1271 cdev_t dev;
21864bc5
MD
1272 int error;
1273
21864bc5 1274 vp = (struct vnode *)fp->f_data;
3a1032a6
AH
1275 if (vp == NULL || vp->v_type == VBAD)
1276 return EBADF;
1277
21864bc5 1278 error = vn_stat(vp, sb, cred);
3a1032a6 1279 if (error)
21864bc5 1280 return (error);
21864bc5
MD
1281
1282 vap = &vattr;
1283 error = VOP_GETATTR(vp, vap);
3a1032a6 1284 if (error)
21864bc5 1285 return (error);
21864bc5
MD
1286
1287 /*
1288 * Zero the spare stat fields
1289 */
1290 sb->st_lspare = 0;
d98152a8
MD
1291 sb->st_qspare1 = 0;
1292 sb->st_qspare2 = 0;
21864bc5
MD
1293
1294 /*
1295 * Copy from vattr table ... or not in case it's a cloned device
1296 */
1297 if (vap->va_fsid != VNOVAL)
1298 sb->st_dev = vap->va_fsid;
1299 else
1300 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
1301
1302 sb->st_ino = vap->va_fileid;
1303
1304 mode = vap->va_mode;
1305 mode |= S_IFCHR;
1306 sb->st_mode = mode;
1307
1308 if (vap->va_nlink > (nlink_t)-1)
1309 sb->st_nlink = (nlink_t)-1;
1310 else
1311 sb->st_nlink = vap->va_nlink;
3a1032a6 1312
21864bc5
MD
1313 sb->st_uid = vap->va_uid;
1314 sb->st_gid = vap->va_gid;
ca8d7677 1315 sb->st_rdev = dev2udev(DEVFS_NODE(vp)->d_dev);
2d076755 1316 sb->st_size = vap->va_bytes;
21864bc5
MD
1317 sb->st_atimespec = vap->va_atime;
1318 sb->st_mtimespec = vap->va_mtime;
1319 sb->st_ctimespec = vap->va_ctime;
1320
1321 /*
1322 * A VCHR and VBLK device may track the last access and last modified
1323 * time independantly of the filesystem. This is particularly true
1324 * because device read and write calls may bypass the filesystem.
1325 */
1326 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1327 dev = vp->v_rdev;
1328 if (dev != NULL) {
1329 if (dev->si_lastread) {
1330 sb->st_atimespec.tv_sec = dev->si_lastread;
1331 sb->st_atimespec.tv_nsec = 0;
1332 }
1333 if (dev->si_lastwrite) {
1334 sb->st_atimespec.tv_sec = dev->si_lastwrite;
1335 sb->st_atimespec.tv_nsec = 0;
1336 }
1337 }
1338 }
1339
1340 /*
1341 * According to www.opengroup.org, the meaning of st_blksize is
1342 * "a filesystem-specific preferred I/O block size for this
1343 * object. In some filesystem types, this may vary from file
1344 * to file"
1345 * Default to PAGE_SIZE after much discussion.
1346 */
1347
1348 sb->st_blksize = PAGE_SIZE;
1349
1350 sb->st_flags = vap->va_flags;
1351
1352 error = priv_check_cred(cred, PRIV_VFS_GENERATION, 0);
1353 if (error)
1354 sb->st_gen = 0;
1355 else
1356 sb->st_gen = (u_int32_t)vap->va_gen;
1357
1358 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
21864bc5 1359
21864bc5
MD
1360 return (0);
1361}
1362
1363
1364static int
9f889dc4 1365devfs_fo_kqfilter(struct file *fp, struct knote *kn)
21864bc5
MD
1366{
1367 struct vnode *vp;
21864bc5
MD
1368 int error;
1369 cdev_t dev;
1370
21864bc5
MD
1371 vp = (struct vnode *)fp->f_data;
1372 if (vp == NULL || vp->v_type == VBAD) {
1373 error = EBADF;
1374 goto done;
1375 }
21864bc5
MD
1376 if ((dev = vp->v_rdev) == NULL) {
1377 error = EBADF;
1378 goto done;
1379 }
1380 reference_dev(dev);
1381
1382 error = dev_dkqfilter(dev, kn);
1383
1384 release_dev(dev);
1385
21864bc5 1386done:
b287d649 1387 return (error);
21864bc5
MD
1388}
1389
21864bc5
MD
1390/*
1391 * MPALMOSTSAFE - acquires mplock
1392 */
1393static int
9f889dc4 1394devfs_fo_ioctl(struct file *fp, u_long com, caddr_t data,
87baaf0c 1395 struct ucred *ucred, struct sysmsg *msg)
21864bc5 1396{
898c91ee
MD
1397 struct devfs_node *node;
1398 struct vnode *vp;
21864bc5 1399 struct vnode *ovp;
21864bc5
MD
1400 cdev_t dev;
1401 int error;
1402 struct fiodname_args *name_args;
1403 size_t namlen;
1404 const char *name;
1405
898c91ee 1406 vp = ((struct vnode *)fp->f_data);
3a1032a6
AH
1407
1408 if ((dev = vp->v_rdev) == NULL)
1409 return EBADF; /* device was revoked */
1410
1411 reference_dev(dev);
21864bc5 1412
898c91ee
MD
1413 node = DEVFS_NODE(vp);
1414
1415 devfs_debug(DEVFS_DEBUG_DEBUG,
9f889dc4 1416 "devfs_fo_ioctl() called! for dev %s\n",
898c91ee 1417 dev->si_name);
21864bc5
MD
1418
1419 if (com == FIODTYPE) {
1420 *(int *)data = dev_dflags(dev) & D_TYPEMASK;
1421 error = 0;
1422 goto out;
1423 } else if (com == FIODNAME) {
1424 name_args = (struct fiodname_args *)data;
1425 name = dev->si_name;
1426 namlen = strlen(name) + 1;
1427
898c91ee
MD
1428 devfs_debug(DEVFS_DEBUG_DEBUG,
1429 "ioctl, got: FIODNAME for %s\n", name);
21864bc5
MD
1430
1431 if (namlen <= name_args->len)
1432 error = copyout(dev->si_name, name_args->name, namlen);
1433 else
1434 error = EINVAL;
1435
898c91ee
MD
1436 devfs_debug(DEVFS_DEBUG_DEBUG,
1437 "ioctl stuff: error: %d\n", error);
21864bc5
MD
1438 goto out;
1439 }
3a1032a6 1440
87baaf0c 1441 error = dev_dioctl(dev, com, data, fp->f_flag, ucred, msg);
3a1032a6 1442
07dfa375 1443#if 0
898c91ee
MD
1444 if (node) {
1445 nanotime(&node->atime);
1446 nanotime(&node->mtime);
21864bc5 1447 }
07dfa375 1448#endif
898c91ee
MD
1449 if (com == TIOCSCTTY) {
1450 devfs_debug(DEVFS_DEBUG_DEBUG,
9f889dc4 1451 "devfs_fo_ioctl: got TIOCSCTTY on %s\n",
898c91ee
MD
1452 dev->si_name);
1453 }
21864bc5 1454 if (error == 0 && com == TIOCSCTTY) {
21864bc5
MD
1455 struct proc *p = curthread->td_proc;
1456 struct session *sess;
898c91ee
MD
1457
1458 devfs_debug(DEVFS_DEBUG_DEBUG,
9f889dc4 1459 "devfs_fo_ioctl: dealing with TIOCSCTTY on %s\n",
898c91ee
MD
1460 dev->si_name);
1461 if (p == NULL) {
21864bc5
MD
1462 error = ENOTTY;
1463 goto out;
1464 }
1465 sess = p->p_session;
898c91ee
MD
1466
1467 /*
1468 * Do nothing if reassigning same control tty
1469 */
21864bc5
MD
1470 if (sess->s_ttyvp == vp) {
1471 error = 0;
1472 goto out;
1473 }
898c91ee
MD
1474
1475 /*
1476 * Get rid of reference to old control tty
1477 */
21864bc5
MD
1478 ovp = sess->s_ttyvp;
1479 vref(vp);
1480 sess->s_ttyvp = vp;
1481 if (ovp)
1482 vrele(ovp);
1483 }
1484
1485out:
3a1032a6 1486 release_dev(dev);
9f889dc4 1487 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_fo_ioctl() finished! \n");
21864bc5
MD
1488 return (error);
1489}
1490
1491
1492static int
1493devfs_spec_fsync(struct vop_fsync_args *ap)
1494{
1495 struct vnode *vp = ap->a_vp;
1496 int error;
1497
1498 if (!vn_isdisk(vp, NULL))
1499 return (0);
1500
1501 /*
1502 * Flush all dirty buffers associated with a block device.
1503 */
1504 error = vfsync(vp, ap->a_waitfor, 10000, NULL, NULL);
1505 return (error);
1506}
1507
21864bc5
MD
1508static int
1509devfs_spec_read(struct vop_read_args *ap)
1510{
898c91ee 1511 struct devfs_node *node;
21864bc5
MD
1512 struct vnode *vp;
1513 struct uio *uio;
1514 cdev_t dev;
1515 int error;
1516
1517 vp = ap->a_vp;
1518 dev = vp->v_rdev;
1519 uio = ap->a_uio;
898c91ee 1520 node = DEVFS_NODE(vp);
21864bc5
MD
1521
1522 if (dev == NULL) /* device was revoked */
1523 return (EBADF);
1524 if (uio->uio_resid == 0)
1525 return (0);
1526
1527 vn_unlock(vp);
1528 error = dev_dread(dev, uio, ap->a_ioflag);
1529 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1530
898c91ee
MD
1531 if (node)
1532 nanotime(&node->atime);
21864bc5
MD
1533
1534 return (error);
1535}
1536
1537/*
1538 * Vnode op for write
1539 *
1540 * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1541 * struct ucred *a_cred)
1542 */
21864bc5
MD
1543static int
1544devfs_spec_write(struct vop_write_args *ap)
1545{
898c91ee 1546 struct devfs_node *node;
21864bc5
MD
1547 struct vnode *vp;
1548 struct uio *uio;
1549 cdev_t dev;
1550 int error;
1551
1552 vp = ap->a_vp;
1553 dev = vp->v_rdev;
1554 uio = ap->a_uio;
898c91ee 1555 node = DEVFS_NODE(vp);
21864bc5
MD
1556
1557 KKASSERT(uio->uio_segflg != UIO_NOCOPY);
1558
1559 if (dev == NULL) /* device was revoked */
1560 return (EBADF);
1561
1562 vn_unlock(vp);
1563 error = dev_dwrite(dev, uio, ap->a_ioflag);
1564 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1565
07dfa375
AH
1566 if (node) {
1567 nanotime(&node->atime);
898c91ee 1568 nanotime(&node->mtime);
07dfa375 1569 }
21864bc5
MD
1570
1571 return (error);
1572}
1573
1574/*
1575 * Device ioctl operation.
1576 *
1577 * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
87baaf0c 1578 * int a_fflag, struct ucred *a_cred, struct sysmsg *msg)
21864bc5 1579 */
21864bc5
MD
1580static int
1581devfs_spec_ioctl(struct vop_ioctl_args *ap)
1582{
21864bc5 1583 struct vnode *vp = ap->a_vp;
898c91ee
MD
1584 struct devfs_node *node;
1585 cdev_t dev;
21864bc5
MD
1586
1587 if ((dev = vp->v_rdev) == NULL)
1588 return (EBADF); /* device was revoked */
898c91ee 1589 node = DEVFS_NODE(vp);
21864bc5 1590
07dfa375 1591#if 0
898c91ee
MD
1592 if (node) {
1593 nanotime(&node->atime);
1594 nanotime(&node->mtime);
21864bc5 1595 }
07dfa375 1596#endif
21864bc5 1597
87baaf0c
MD
1598 return (dev_dioctl(dev, ap->a_command, ap->a_data, ap->a_fflag,
1599 ap->a_cred, ap->a_sysmsg));
21864bc5
MD
1600}
1601
1602/*
21864bc5
MD
1603 * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn)
1604 */
1605/* ARGSUSED */
1606static int
1607devfs_spec_kqfilter(struct vop_kqfilter_args *ap)
1608{
21864bc5 1609 struct vnode *vp = ap->a_vp;
898c91ee
MD
1610 struct devfs_node *node;
1611 cdev_t dev;
21864bc5
MD
1612
1613 if ((dev = vp->v_rdev) == NULL)
b287d649 1614 return (EBADF); /* device was revoked (EBADF) */
898c91ee 1615 node = DEVFS_NODE(vp);
21864bc5 1616
07dfa375 1617#if 0
898c91ee
MD
1618 if (node)
1619 nanotime(&node->atime);
07dfa375 1620#endif
21864bc5 1621
b287d649 1622 return (dev_dkqfilter(dev, ap->a_kn));
21864bc5
MD
1623}
1624
21864bc5
MD
1625/*
1626 * Convert a vnode strategy call into a device strategy call. Vnode strategy
1627 * calls are not limited to device DMA limits so we have to deal with the
1628 * case.
1629 *
1630 * spec_strategy(struct vnode *a_vp, struct bio *a_bio)
1631 */
1632static int
1633devfs_spec_strategy(struct vop_strategy_args *ap)
1634{
1635 struct bio *bio = ap->a_bio;
1636 struct buf *bp = bio->bio_buf;
1637 struct buf *nbp;
1638 struct vnode *vp;
1639 struct mount *mp;
1640 int chunksize;
1641 int maxiosize;
1642
1643 if (bp->b_cmd != BUF_CMD_READ && LIST_FIRST(&bp->b_dep) != NULL)
1644 buf_start(bp);
1645
1646 /*
1647 * Collect statistics on synchronous and asynchronous read
1648 * and write counts for disks that have associated filesystems.
1649 */
1650 vp = ap->a_vp;
1651 KKASSERT(vp->v_rdev != NULL); /* XXX */
1652 if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) {
1653 if (bp->b_cmd == BUF_CMD_READ) {
21864bc5
MD
1654 if (bp->b_flags & BIO_SYNC)
1655 mp->mnt_stat.f_syncreads++;
1656 else
1657 mp->mnt_stat.f_asyncreads++;
1658 } else {
1659 if (bp->b_flags & BIO_SYNC)
1660 mp->mnt_stat.f_syncwrites++;
1661 else
1662 mp->mnt_stat.f_asyncwrites++;
1663 }
1664 }
1665
1666 /*
1667 * Device iosize limitations only apply to read and write. Shortcut
1668 * the I/O if it fits.
1669 */
1670 if ((maxiosize = vp->v_rdev->si_iosize_max) == 0) {
898c91ee
MD
1671 devfs_debug(DEVFS_DEBUG_DEBUG,
1672 "%s: si_iosize_max not set!\n",
1673 dev_dname(vp->v_rdev));
21864bc5
MD
1674 maxiosize = MAXPHYS;
1675 }
1676#if SPEC_CHAIN_DEBUG & 2
1677 maxiosize = 4096;
1678#endif
1679 if (bp->b_bcount <= maxiosize ||
1680 (bp->b_cmd != BUF_CMD_READ && bp->b_cmd != BUF_CMD_WRITE)) {
1681 dev_dstrategy_chain(vp->v_rdev, bio);
1682 return (0);
1683 }
1684
1685 /*
1686 * Clone the buffer and set up an I/O chain to chunk up the I/O.
1687 */
1688 nbp = kmalloc(sizeof(*bp), M_DEVBUF, M_INTWAIT|M_ZERO);
1689 initbufbio(nbp);
1690 buf_dep_init(nbp);
21864bc5
MD
1691 BUF_LOCK(nbp, LK_EXCLUSIVE);
1692 BUF_KERNPROC(nbp);
1693 nbp->b_vp = vp;
1694 nbp->b_flags = B_PAGING | (bp->b_flags & B_BNOCLIP);
1695 nbp->b_data = bp->b_data;
1696 nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1697 nbp->b_bio1.bio_offset = bio->bio_offset;
1698 nbp->b_bio1.bio_caller_info1.ptr = bio;
1699
1700 /*
1701 * Start the first transfer
1702 */
1703 if (vn_isdisk(vp, NULL))
1704 chunksize = vp->v_rdev->si_bsize_phys;
1705 else
1706 chunksize = DEV_BSIZE;
1707 chunksize = maxiosize / chunksize * chunksize;
1708#if SPEC_CHAIN_DEBUG & 1
898c91ee
MD
1709 devfs_debug(DEVFS_DEBUG_DEBUG,
1710 "spec_strategy chained I/O chunksize=%d\n",
1711 chunksize);
21864bc5
MD
1712#endif
1713 nbp->b_cmd = bp->b_cmd;
1714 nbp->b_bcount = chunksize;
1715 nbp->b_bufsize = chunksize; /* used to detect a short I/O */
1716 nbp->b_bio1.bio_caller_info2.index = chunksize;
1717
1718#if SPEC_CHAIN_DEBUG & 1
898c91ee
MD
1719 devfs_debug(DEVFS_DEBUG_DEBUG,
1720 "spec_strategy: chain %p offset %d/%d bcount %d\n",
1721 bp, 0, bp->b_bcount, nbp->b_bcount);
21864bc5
MD
1722#endif
1723
1724 dev_dstrategy(vp->v_rdev, &nbp->b_bio1);
1725
1726 if (DEVFS_NODE(vp)) {
1727 nanotime(&DEVFS_NODE(vp)->atime);
1728 nanotime(&DEVFS_NODE(vp)->mtime);
1729 }
1730
1731 return (0);
1732}
1733
1734/*
1735 * Chunked up transfer completion routine - chain transfers until done
77912481
MD
1736 *
1737 * NOTE: MPSAFE callback.
21864bc5
MD
1738 */
1739static
1740void
1741devfs_spec_strategy_done(struct bio *nbio)
1742{
1743 struct buf *nbp = nbio->bio_buf;
1744 struct bio *bio = nbio->bio_caller_info1.ptr; /* original bio */
1745 struct buf *bp = bio->bio_buf; /* original bp */
1746 int chunksize = nbio->bio_caller_info2.index; /* chunking */
1747 int boffset = nbp->b_data - bp->b_data;
1748
1749 if (nbp->b_flags & B_ERROR) {
1750 /*
1751 * An error terminates the chain, propogate the error back
1752 * to the original bp
1753 */
1754 bp->b_flags |= B_ERROR;
1755 bp->b_error = nbp->b_error;
1756 bp->b_resid = bp->b_bcount - boffset +
1757 (nbp->b_bcount - nbp->b_resid);
1758#if SPEC_CHAIN_DEBUG & 1
898c91ee
MD
1759 devfs_debug(DEVFS_DEBUG_DEBUG,
1760 "spec_strategy: chain %p error %d bcount %d/%d\n",
1761 bp, bp->b_error, bp->b_bcount,
1762 bp->b_bcount - bp->b_resid);
21864bc5 1763#endif
21864bc5
MD
1764 } else if (nbp->b_resid) {
1765 /*
1766 * A short read or write terminates the chain
1767 */
1768 bp->b_error = nbp->b_error;
1769 bp->b_resid = bp->b_bcount - boffset +
1770 (nbp->b_bcount - nbp->b_resid);
1771#if SPEC_CHAIN_DEBUG & 1
898c91ee
MD
1772 devfs_debug(DEVFS_DEBUG_DEBUG,
1773 "spec_strategy: chain %p short read(1) "
1774 "bcount %d/%d\n",
1775 bp, bp->b_bcount - bp->b_resid, bp->b_bcount);
21864bc5 1776#endif
21864bc5
MD
1777 } else if (nbp->b_bcount != nbp->b_bufsize) {
1778 /*
1779 * A short read or write can also occur by truncating b_bcount
1780 */
1781#if SPEC_CHAIN_DEBUG & 1
898c91ee
MD
1782 devfs_debug(DEVFS_DEBUG_DEBUG,
1783 "spec_strategy: chain %p short read(2) "
1784 "bcount %d/%d\n",
1785 bp, nbp->b_bcount + boffset, bp->b_bcount);
21864bc5
MD
1786#endif
1787 bp->b_error = 0;
1788 bp->b_bcount = nbp->b_bcount + boffset;
1789 bp->b_resid = nbp->b_resid;
21864bc5
MD
1790 } else if (nbp->b_bcount + boffset == bp->b_bcount) {
1791 /*
1792 * No more data terminates the chain
1793 */
1794#if SPEC_CHAIN_DEBUG & 1
898c91ee
MD
1795 devfs_debug(DEVFS_DEBUG_DEBUG,
1796 "spec_strategy: chain %p finished bcount %d\n",
1797 bp, bp->b_bcount);
21864bc5
MD
1798#endif
1799 bp->b_error = 0;
1800 bp->b_resid = 0;
21864bc5
MD
1801 } else {
1802 /*
1803 * Continue the chain
1804 */
1805 boffset += nbp->b_bcount;
1806 nbp->b_data = bp->b_data + boffset;
1807 nbp->b_bcount = bp->b_bcount - boffset;
1808 if (nbp->b_bcount > chunksize)
1809 nbp->b_bcount = chunksize;
1810 nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1811 nbp->b_bio1.bio_offset = bio->bio_offset + boffset;
1812
1813#if SPEC_CHAIN_DEBUG & 1
898c91ee
MD
1814 devfs_debug(DEVFS_DEBUG_DEBUG,
1815 "spec_strategy: chain %p offset %d/%d bcount %d\n",
1816 bp, boffset, bp->b_bcount, nbp->b_bcount);
21864bc5
MD
1817#endif
1818
1819 dev_dstrategy(nbp->b_vp->v_rdev, &nbp->b_bio1);
b5d7061d 1820 return;
21864bc5 1821 }
b5d7061d
MD
1822
1823 /*
1824 * Fall through to here on termination. biodone(bp) and
1825 * clean up and free nbp.
1826 */
1827 biodone(bio);
1828 BUF_UNLOCK(nbp);
1829 uninitbufbio(nbp);
1830 kfree(nbp, M_DEVBUF);
21864bc5
MD
1831}
1832
1833/*
1834 * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
1835 */
1836static int
1837devfs_spec_freeblks(struct vop_freeblks_args *ap)
1838{
1839 struct buf *bp;
1840
1841 /*
1842 * XXX: This assumes that strategy does the deed right away.
1843 * XXX: this may not be TRTTD.
1844 */
1845 KKASSERT(ap->a_vp->v_rdev != NULL);
1846 if ((dev_dflags(ap->a_vp->v_rdev) & D_CANFREE) == 0)
1847 return (0);
1848 bp = geteblk(ap->a_length);
1849 bp->b_cmd = BUF_CMD_FREEBLKS;
1850 bp->b_bio1.bio_offset = ap->a_offset;
1851 bp->b_bcount = ap->a_length;
1852 dev_dstrategy(ap->a_vp->v_rdev, &bp->b_bio1);
1853 return (0);
1854}
1855
1856/*
1857 * Implement degenerate case where the block requested is the block
1858 * returned, and assume that the entire device is contiguous in regards
1859 * to the contiguous block range (runp and runb).
1860 *
1861 * spec_bmap(struct vnode *a_vp, off_t a_loffset,
1862 * off_t *a_doffsetp, int *a_runp, int *a_runb)
1863 */
1864static int
1865devfs_spec_bmap(struct vop_bmap_args *ap)
1866{
1867 if (ap->a_doffsetp != NULL)
1868 *ap->a_doffsetp = ap->a_loffset;
1869 if (ap->a_runp != NULL)
1870 *ap->a_runp = MAXBSIZE;
1871 if (ap->a_runb != NULL) {
1872 if (ap->a_loffset < MAXBSIZE)
1873 *ap->a_runb = (int)ap->a_loffset;
1874 else
1875 *ap->a_runb = MAXBSIZE;
1876 }
1877 return (0);
1878}
1879
1880
1881/*
1882 * Special device advisory byte-level locks.
1883 *
1884 * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
1885 * struct flock *a_fl, int a_flags)
1886 */
1887/* ARGSUSED */
1888static int
1889devfs_spec_advlock(struct vop_advlock_args *ap)
1890{
1891 return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
1892}
1893
77912481
MD
1894/*
1895 * NOTE: MPSAFE callback.
1896 */
21864bc5
MD
1897static void
1898devfs_spec_getpages_iodone(struct bio *bio)
1899{
1900 bio->bio_buf->b_cmd = BUF_CMD_DONE;
1901 wakeup(bio->bio_buf);
1902}
1903
1904/*
1905 * spec_getpages() - get pages associated with device vnode.
1906 *
1907 * Note that spec_read and spec_write do not use the buffer cache, so we
1908 * must fully implement getpages here.
1909 */
1910static int
1911devfs_spec_getpages(struct vop_getpages_args *ap)
1912{
1913 vm_offset_t kva;
1914 int error;
1915 int i, pcount, size;
1916 struct buf *bp;
1917 vm_page_t m;
1918 vm_ooffset_t offset;
1919 int toff, nextoff, nread;
1920 struct vnode *vp = ap->a_vp;
1921 int blksiz;
1922 int gotreqpage;
1923
1924 error = 0;
1925 pcount = round_page(ap->a_count) / PAGE_SIZE;
1926
1927 /*
1928 * Calculate the offset of the transfer and do sanity check.
1929 */
1930 offset = IDX_TO_OFF(ap->a_m[0]->pindex) + ap->a_offset;
1931
1932 /*
1933 * Round up physical size for real devices. We cannot round using
1934 * v_mount's block size data because v_mount has nothing to do with
1935 * the device. i.e. it's usually '/dev'. We need the physical block
1936 * size for the device itself.
1937 *
1938 * We can't use v_rdev->si_mountpoint because it only exists when the
1939 * block device is mounted. However, we can use v_rdev.
1940 */
21864bc5
MD
1941 if (vn_isdisk(vp, NULL))
1942 blksiz = vp->v_rdev->si_bsize_phys;
1943 else
1944 blksiz = DEV_BSIZE;
1945
1946 size = (ap->a_count + blksiz - 1) & ~(blksiz - 1);
1947
ad8b1a17 1948 bp = getpbuf_kva(NULL);
21864bc5
MD
1949 kva = (vm_offset_t)bp->b_data;
1950
1951 /*
1952 * Map the pages to be read into the kva.
1953 */
1954 pmap_qenter(kva, ap->a_m, pcount);
1955
1956 /* Build a minimal buffer header. */
1957 bp->b_cmd = BUF_CMD_READ;
1958 bp->b_bcount = size;
1959 bp->b_resid = 0;
77912481 1960 bsetrunningbufspace(bp, size);
21864bc5
MD
1961
1962 bp->b_bio1.bio_offset = offset;
1963 bp->b_bio1.bio_done = devfs_spec_getpages_iodone;
1964
1965 mycpu->gd_cnt.v_vnodein++;
1966 mycpu->gd_cnt.v_vnodepgsin += pcount;
1967
1968 /* Do the input. */
1969 vn_strategy(ap->a_vp, &bp->b_bio1);
1970
1971 crit_enter();
1972
1973 /* We definitely need to be at splbio here. */
1974 while (bp->b_cmd != BUF_CMD_DONE)
1975 tsleep(bp, 0, "spread", 0);
1976
1977 crit_exit();
1978
1979 if (bp->b_flags & B_ERROR) {
1980 if (bp->b_error)
1981 error = bp->b_error;
1982 else
1983 error = EIO;
1984 }
1985
1986 /*
1987 * If EOF is encountered we must zero-extend the result in order
1988 * to ensure that the page does not contain garabge. When no
1989 * error occurs, an early EOF is indicated if b_bcount got truncated.
1990 * b_resid is relative to b_bcount and should be 0, but some devices
1991 * might indicate an EOF with b_resid instead of truncating b_bcount.
1992 */
1993 nread = bp->b_bcount - bp->b_resid;
1994 if (nread < ap->a_count)
1995 bzero((caddr_t)kva + nread, ap->a_count - nread);
1996 pmap_qremove(kva, pcount);
1997
1998 gotreqpage = 0;
1999 for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) {
2000 nextoff = toff + PAGE_SIZE;
2001 m = ap->a_m[i];
2002
2003 m->flags &= ~PG_ZERO;
2004
cb1cf930
MD
2005 /*
2006 * NOTE: vm_page_undirty/clear_dirty etc do not clear the
2007 * pmap modified bit. pmap modified bit should have
2008 * already been cleared.
2009 */
21864bc5
MD
2010 if (nextoff <= nread) {
2011 m->valid = VM_PAGE_BITS_ALL;
2012 vm_page_undirty(m);
2013 } else if (toff < nread) {
2014 /*
2015 * Since this is a VM request, we have to supply the
cb1cf930 2016 * unaligned offset to allow vm_page_set_valid()
21864bc5
MD
2017 * to zero sub-DEV_BSIZE'd portions of the page.
2018 */
1a54183b
MD
2019 vm_page_set_valid(m, 0, nread - toff);
2020 vm_page_clear_dirty_end_nonincl(m, 0, nread - toff);
21864bc5
MD
2021 } else {
2022 m->valid = 0;
2023 vm_page_undirty(m);
2024 }
2025
2026 if (i != ap->a_reqpage) {
2027 /*
2028 * Just in case someone was asking for this page we
2029 * now tell them that it is ok to use.
2030 */
2031 if (!error || (m->valid == VM_PAGE_BITS_ALL)) {
2032 if (m->valid) {
2033 if (m->flags & PG_WANTED) {
2034 vm_page_activate(m);
2035 } else {
2036 vm_page_deactivate(m);
2037 }
2038 vm_page_wakeup(m);
2039 } else {
2040 vm_page_free(m);
2041 }
2042 } else {
2043 vm_page_free(m);
2044 }
2045 } else if (m->valid) {
2046 gotreqpage = 1;
2047 /*
2048 * Since this is a VM request, we need to make the
2049 * entire page presentable by zeroing invalid sections.
2050 */
2051 if (m->valid != VM_PAGE_BITS_ALL)
2052 vm_page_zero_invalid(m, FALSE);
2053 }
2054 }
2055 if (!gotreqpage) {
2056 m = ap->a_m[ap->a_reqpage];
2057 devfs_debug(DEVFS_DEBUG_WARNING,
2058 "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n",
2059 devtoname(vp->v_rdev), error, bp, bp->b_vp);
2060 devfs_debug(DEVFS_DEBUG_WARNING,
2061 " size: %d, resid: %d, a_count: %d, valid: 0x%x\n",
2062 size, bp->b_resid, ap->a_count, m->valid);
2063 devfs_debug(DEVFS_DEBUG_WARNING,
2064 " nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n",
2065 nread, ap->a_reqpage, (u_long)m->pindex, pcount);
2066 /*
2067 * Free the buffer header back to the swap buffer pool.
2068 */
2069 relpbuf(bp, NULL);
2070 return VM_PAGER_ERROR;
2071 }
2072 /*
2073 * Free the buffer header back to the swap buffer pool.
2074 */
2075 relpbuf(bp, NULL);
07dfa375
AH
2076 if (DEVFS_NODE(ap->a_vp))
2077 nanotime(&DEVFS_NODE(ap->a_vp)->mtime);
21864bc5
MD
2078 return VM_PAGER_OK;
2079}
2080
21864bc5
MD
2081static __inline
2082int
2083sequential_heuristic(struct uio *uio, struct file *fp)
2084{
2085 /*
2086 * Sequential heuristic - detect sequential operation
2087 */
2088 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
2089 uio->uio_offset == fp->f_nextoff) {
21864bc5
MD
2090 /*
2091 * XXX we assume that the filesystem block size is
2092 * the default. Not true, but still gives us a pretty
2093 * good indicator of how sequential the read operations
2094 * are.
2095 */
898c91ee
MD
2096 int tmpseq = fp->f_seqcount;
2097
21864bc5
MD
2098 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
2099 if (tmpseq > IO_SEQMAX)
2100 tmpseq = IO_SEQMAX;
2101 fp->f_seqcount = tmpseq;
2102 return(fp->f_seqcount << IO_SEQSHIFT);
2103 }
2104
2105 /*
2106 * Not sequential, quick draw-down of seqcount
2107 */
2108 if (fp->f_seqcount > 1)
2109 fp->f_seqcount = 1;
2110 else
2111 fp->f_seqcount = 0;
2112 return(0);
2113}