kernel - Adjust devfs, hammer, and puffs to use the new cache_unlink()
[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 608 struct vattr *vap;
dffaed1b
AH
609 uid_t cur_uid;
610 gid_t cur_gid;
611 mode_t cur_mode;
21864bc5
MD
612 int error = 0;
613
894bbb25 614 if (!devfs_node_is_accessible(node))
ca8d7677 615 return ENOENT;
4062d050 616 node_sync_dev_get(node);
21864bc5 617
21864bc5
MD
618 lockmgr(&devfs_lock, LK_EXCLUSIVE);
619
620 vap = ap->a_vap;
21864bc5 621
dffaed1b
AH
622 if ((vap->va_uid != (uid_t)VNOVAL) || (vap->va_gid != (gid_t)VNOVAL)) {
623 cur_uid = node->uid;
624 cur_gid = node->gid;
625 cur_mode = node->mode;
626 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
627 ap->a_cred, &cur_uid, &cur_gid, &cur_mode);
628 if (error)
629 goto out;
630
631 if (node->uid != cur_uid || node->gid != cur_gid) {
632 node->uid = cur_uid;
633 node->gid = cur_gid;
634 node->mode = cur_mode;
21864bc5 635 }
21864bc5
MD
636 }
637
638 if (vap->va_mode != (mode_t)VNOVAL) {
dffaed1b
AH
639 cur_mode = node->mode;
640 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
641 node->uid, node->gid, &cur_mode);
642 if (error == 0 && node->mode != cur_mode) {
643 node->mode = cur_mode;
21864bc5 644 }
21864bc5
MD
645 }
646
647out:
4062d050 648 node_sync_dev_set(node);
07dfa375 649 nanotime(&node->ctime);
21864bc5 650 lockmgr(&devfs_lock, LK_RELEASE);
898c91ee 651
21864bc5
MD
652 return error;
653}
654
655
656static int
9f889dc4 657devfs_vop_readlink(struct vop_readlink_args *ap)
21864bc5
MD
658{
659 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
ca8d7677
MD
660 int ret;
661
894bbb25 662 if (!devfs_node_is_accessible(node))
ca8d7677 663 return ENOENT;
21864bc5 664
ca8d7677
MD
665 lockmgr(&devfs_lock, LK_EXCLUSIVE);
666 ret = uiomove(node->symlink_name, node->symlink_namelen, ap->a_uio);
667 lockmgr(&devfs_lock, LK_RELEASE);
668
669 return ret;
21864bc5
MD
670}
671
672
673static int
9f889dc4 674devfs_vop_print(struct vop_print_args *ap)
21864bc5 675{
21864bc5
MD
676 return (0);
677}
678
39a08947 679static int
9f889dc4 680devfs_vop_nmkdir(struct vop_nmkdir_args *ap)
39a08947
AH
681{
682 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
683 struct devfs_node *node;
684
685 if (!devfs_node_is_accessible(dnode))
686 return ENOENT;
687
688 if ((dnode->node_type != Proot) && (dnode->node_type != Pdir))
689 goto out;
690
691 lockmgr(&devfs_lock, LK_EXCLUSIVE);
692 devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Pdir,
693 ap->a_nch->ncp->nc_name, dnode, NULL);
694
695 if (*ap->a_vpp) {
696 node = DEVFS_NODE(*ap->a_vpp);
697 node->flags |= DEVFS_USER_CREATED;
698 cache_setunresolved(ap->a_nch);
699 cache_setvp(ap->a_nch, *ap->a_vpp);
700 }
701 lockmgr(&devfs_lock, LK_RELEASE);
702out:
703 return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
704}
21864bc5
MD
705
706static int
9f889dc4 707devfs_vop_nsymlink(struct vop_nsymlink_args *ap)
21864bc5 708{
898c91ee
MD
709 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
710 struct devfs_node *node;
711 size_t targetlen;
21864bc5 712
898c91ee 713 if (!devfs_node_is_accessible(dnode))
ca8d7677
MD
714 return ENOENT;
715
894bbb25
AH
716 ap->a_vap->va_type = VLNK;
717
898c91ee 718 if ((dnode->node_type != Proot) && (dnode->node_type != Pdir))
21864bc5 719 goto out;
898c91ee 720
21864bc5
MD
721 lockmgr(&devfs_lock, LK_EXCLUSIVE);
722 devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Plink,
898c91ee 723 ap->a_nch->ncp->nc_name, dnode, NULL);
21864bc5 724
898c91ee 725 targetlen = strlen(ap->a_target);
21864bc5 726 if (*ap->a_vpp) {
898c91ee
MD
727 node = DEVFS_NODE(*ap->a_vpp);
728 node->flags |= DEVFS_USER_CREATED;
729 node->symlink_namelen = targetlen;
730 node->symlink_name = kmalloc(targetlen + 1, M_DEVFS, M_WAITOK);
731 memcpy(node->symlink_name, ap->a_target, targetlen);
732 node->symlink_name[targetlen] = '\0';
21864bc5 733 cache_setunresolved(ap->a_nch);
21864bc5
MD
734 cache_setvp(ap->a_nch, *ap->a_vpp);
735 }
736 lockmgr(&devfs_lock, LK_RELEASE);
737out:
738 return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
21864bc5
MD
739}
740
39a08947 741static int
9f889dc4 742devfs_vop_nrmdir(struct vop_nrmdir_args *ap)
39a08947
AH
743{
744 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
745 struct devfs_node *node;
746 struct namecache *ncp;
747 int error = ENOENT;
748
749 ncp = ap->a_nch->ncp;
750
751 if (!devfs_node_is_accessible(dnode))
752 return ENOENT;
753
754 lockmgr(&devfs_lock, LK_EXCLUSIVE);
755
756 if ((dnode->node_type != Proot) && (dnode->node_type != Pdir))
757 goto out;
758
759 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
760 if (ncp->nc_nlen != node->d_dir.d_namlen)
761 continue;
762 if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
763 continue;
764
765 /*
766 * only allow removal of user created dirs
767 */
768 if ((node->flags & DEVFS_USER_CREATED) == 0) {
769 error = EPERM;
770 goto out;
771 } else if (node->node_type != Pdir) {
772 error = ENOTDIR;
773 goto out;
774 } else if (node->nchildren > 2) {
775 error = ENOTEMPTY;
776 goto out;
777 } else {
778 if (node->v_node)
779 cache_inval_vp(node->v_node, CINV_DESTROY);
780 devfs_unlinkp(node);
781 error = 0;
782 break;
783 }
784 }
785
5323ed62 786 cache_unlink(ap->a_nch);
39a08947
AH
787out:
788 lockmgr(&devfs_lock, LK_RELEASE);
789 return error;
790}
21864bc5
MD
791
792static int
9f889dc4 793devfs_vop_nremove(struct vop_nremove_args *ap)
21864bc5 794{
898c91ee 795 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
21864bc5
MD
796 struct devfs_node *node;
797 struct namecache *ncp;
21864bc5
MD
798 int error = ENOENT;
799
21864bc5
MD
800 ncp = ap->a_nch->ncp;
801
898c91ee 802 if (!devfs_node_is_accessible(dnode))
ca8d7677
MD
803 return ENOENT;
804
21864bc5
MD
805 lockmgr(&devfs_lock, LK_EXCLUSIVE);
806
898c91ee 807 if ((dnode->node_type != Proot) && (dnode->node_type != Pdir))
21864bc5 808 goto out;
21864bc5 809
898c91ee
MD
810 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
811 if (ncp->nc_nlen != node->d_dir.d_namlen)
812 continue;
813 if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
814 continue;
21864bc5 815
898c91ee
MD
816 /*
817 * only allow removal of user created stuff (e.g. symlinks)
818 */
819 if ((node->flags & DEVFS_USER_CREATED) == 0) {
820 error = EPERM;
821 goto out;
39a08947
AH
822 } else if (node->node_type == Pdir) {
823 error = EISDIR;
824 goto out;
898c91ee
MD
825 } else {
826 if (node->v_node)
827 cache_inval_vp(node->v_node, CINV_DESTROY);
828 devfs_unlinkp(node);
829 error = 0;
830 break;
21864bc5
MD
831 }
832 }
833
5323ed62 834 cache_unlink(ap->a_nch);
21864bc5
MD
835out:
836 lockmgr(&devfs_lock, LK_RELEASE);
21864bc5
MD
837 return error;
838}
839
840
841static int
842devfs_spec_open(struct vop_open_args *ap)
843{
844 struct vnode *vp = ap->a_vp;
ca8d7677 845 struct vnode *orig_vp = NULL;
898c91ee
MD
846 struct devfs_node *node = DEVFS_NODE(vp);
847 struct devfs_node *newnode;
21864bc5 848 cdev_t dev, ndev = NULL;
21864bc5 849 int error = 0;
21864bc5 850
898c91ee
MD
851 if (node) {
852 if (node->d_dev == NULL)
21864bc5 853 return ENXIO;
898c91ee 854 if (!devfs_node_is_accessible(node))
894bbb25 855 return ENOENT;
21864bc5
MD
856 }
857
21864bc5
MD
858 if ((dev = vp->v_rdev) == NULL)
859 return ENXIO;
860
898c91ee 861 if (node && ap->a_fp) {
21864bc5
MD
862 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -1.1-\n");
863 lockmgr(&devfs_lock, LK_EXCLUSIVE);
07dfa375
AH
864
865 ndev = devfs_clone(dev, node->d_dir.d_name, node->d_dir.d_namlen,
866 ap->a_mode, ap->a_cred);
867 if (ndev != NULL) {
898c91ee
MD
868 newnode = devfs_create_device_node(
869 DEVFS_MNTDATA(vp->v_mount)->root_node,
07dfa375
AH
870 ndev, NULL, NULL);
871 /* XXX: possibly destroy device if this happens */
898c91ee 872
07dfa375
AH
873 if (newnode != NULL) {
874 dev = ndev;
875 devfs_link_dev(dev);
21864bc5 876
07dfa375
AH
877 devfs_debug(DEVFS_DEBUG_DEBUG,
878 "parent here is: %s, node is: |%s|\n",
879 ((node->parent->node_type == Proot) ?
880 "ROOT!" : node->parent->d_dir.d_name),
881 newnode->d_dir.d_name);
882 devfs_debug(DEVFS_DEBUG_DEBUG,
883 "test: %s\n",
884 ((struct devfs_node *)(TAILQ_LAST(DEVFS_DENODE_HEAD(node->parent), devfs_node_head)))->d_dir.d_name);
885
886 /*
887 * orig_vp is set to the original vp if we cloned.
888 */
889 /* node->flags |= DEVFS_CLONED; */
890 devfs_allocv(&vp, newnode);
891 orig_vp = ap->a_vp;
892 ap->a_vp = vp;
893 }
21864bc5
MD
894 }
895 lockmgr(&devfs_lock, LK_RELEASE);
896 }
897
898c91ee
MD
898 devfs_debug(DEVFS_DEBUG_DEBUG,
899 "devfs_spec_open() called on %s! \n",
900 dev->si_name);
901
21864bc5
MD
902 /*
903 * Make this field valid before any I/O in ->d_open
904 */
905 if (!dev->si_iosize_max)
95df18e4
FT
906 /* XXX: old DFLTPHYS == 64KB dependency */
907 dev->si_iosize_max = min(MAXPHYS,64*1024);
21864bc5
MD
908
909 if (dev_dflags(dev) & D_TTY)
2247fe02 910 vsetflags(vp, VISTTY);
21864bc5 911
9f889dc4
MD
912 /*
913 * Open underlying device
914 */
21864bc5
MD
915 vn_unlock(vp);
916 error = dev_dopen(dev, ap->a_mode, S_IFCHR, ap->a_cred);
917 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
918
ca8d7677
MD
919 /*
920 * Clean up any cloned vp if we error out.
921 */
21864bc5 922 if (error) {
ca8d7677 923 if (orig_vp) {
21864bc5 924 vput(vp);
ca8d7677
MD
925 ap->a_vp = orig_vp;
926 /* orig_vp = NULL; */
927 }
21864bc5
MD
928 return error;
929 }
930
d894b0eb 931 /*
ddd7de82
AH
932 * This checks if the disk device is going to be opened for writing.
933 * It will be only allowed in the cases where securelevel permits it
934 * and it's not mounted R/W.
d894b0eb 935 */
ddd7de82
AH
936 if ((dev_dflags(dev) & D_DISK) && (ap->a_mode & FWRITE) &&
937 (ap->a_cred != FSCRED)) {
938
939 /* Very secure mode. No open for writing allowed */
940 if (securelevel >= 2)
941 return EPERM;
942
943 /*
944 * If it is mounted R/W, do not allow to open for writing.
945 * In the case it's mounted read-only but securelevel
946 * is >= 1, then do not allow opening for writing either.
947 */
948 if (vfs_mountedon(vp)) {
949 if (!(dev->si_mountpoint->mnt_flag & MNT_RDONLY))
950 return EBUSY;
951 else if (securelevel >= 1)
d894b0eb
AH
952 return EPERM;
953 }
954 }
21864bc5
MD
955
956 if (dev_dflags(dev) & D_TTY) {
957 if (dev->si_tty) {
958 struct tty *tp;
959 tp = dev->si_tty;
960 if (!tp->t_stop) {
898c91ee
MD
961 devfs_debug(DEVFS_DEBUG_DEBUG,
962 "devfs: no t_stop\n");
21864bc5
MD
963 tp->t_stop = nottystop;
964 }
965 }
966 }
967
968
969 if (vn_isdisk(vp, NULL)) {
970 if (!dev->si_bsize_phys)
971 dev->si_bsize_phys = DEV_BSIZE;
b0d18f7d 972 vinitvmio(vp, IDX_TO_OFF(INT_MAX), PAGE_SIZE, -1);
21864bc5
MD
973 }
974
975 vop_stdopen(ap);
07dfa375 976#if 0
898c91ee
MD
977 if (node)
978 nanotime(&node->atime);
07dfa375 979#endif
21864bc5 980
b80b4c32
MD
981 /*
982 * If we replaced the vp the vop_stdopen() call will have loaded
983 * it into fp->f_data and vref()d the vp, giving us two refs. So
984 * instead of just unlocking it here we have to vput() it.
985 */
ca8d7677 986 if (orig_vp)
b80b4c32 987 vput(vp);
21864bc5
MD
988
989 /* Ugly pty magic, to make pty devices appear once they are opened */
898c91ee
MD
990 if (node && (node->flags & DEVFS_PTY) == DEVFS_PTY)
991 node->flags &= ~DEVFS_INVISIBLE;
21864bc5
MD
992
993 if (ap->a_fp) {
b80b4c32 994 KKASSERT(ap->a_fp->f_type == DTYPE_VNODE);
fb12e9a5 995 KKASSERT((ap->a_fp->f_flag & FMASK) == (ap->a_mode & FMASK));
21864bc5 996 ap->a_fp->f_ops = &devfs_dev_fileops;
b80b4c32 997 KKASSERT(ap->a_fp->f_data == (void *)vp);
21864bc5
MD
998 }
999
21864bc5
MD
1000 return 0;
1001}
1002
1003
1004static int
1005devfs_spec_close(struct vop_close_args *ap)
1006{
92fb0c6a 1007 struct devfs_node *node;
21864bc5
MD
1008 struct proc *p = curproc;
1009 struct vnode *vp = ap->a_vp;
1010 cdev_t dev = vp->v_rdev;
1011 int error = 0;
1012 int needrelock;
1013
52f98df9
SG
1014 if (dev)
1015 devfs_debug(DEVFS_DEBUG_DEBUG,
1016 "devfs_spec_close() called on %s! \n",
1017 dev->si_name);
1018 else
1019 devfs_debug(DEVFS_DEBUG_DEBUG,
1020 "devfs_spec_close() called, null vode!\n");
21864bc5
MD
1021
1022 /*
1023 * A couple of hacks for devices and tty devices. The
1024 * vnode ref count cannot be used to figure out the
1025 * last close, but we can use v_opencount now that
1026 * revoke works properly.
1027 *
1028 * Detect the last close on a controlling terminal and clear
1029 * the session (half-close).
1030 */
1031 if (dev)
1032 reference_dev(dev);
1033
1034 if (p && vp->v_opencount <= 1 && vp == p->p_session->s_ttyvp) {
1035 p->p_session->s_ttyvp = NULL;
1036 vrele(vp);
1037 }
1038
1039 /*
1040 * Vnodes can be opened and closed multiple times. Do not really
1041 * close the device unless (1) it is being closed forcibly,
1042 * (2) the device wants to track closes, or (3) this is the last
1043 * vnode doing its last close on the device.
1044 *
1045 * XXX the VXLOCK (force close) case can leave vnodes referencing
1046 * a closed device. This might not occur now that our revoke is
1047 * fixed.
1048 */
1049 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -1- \n");
1050 if (dev && ((vp->v_flag & VRECLAIMED) ||
1051 (dev_dflags(dev) & D_TRACKCLOSE) ||
1052 (vp->v_opencount == 1))) {
898c91ee 1053 /*
92fb0c6a
MD
1054 * Ugly pty magic, to make pty devices disappear again once
1055 * they are closed.
1056 */
1057 node = DEVFS_NODE(ap->a_vp);
1058 if (node && (node->flags & DEVFS_PTY))
1059 node->flags |= DEVFS_INVISIBLE;
1060
1061 /*
495d3a1e
MD
1062 * Unlock around dev_dclose(), unless the vnode is
1063 * undergoing a vgone/reclaim (during umount).
898c91ee 1064 */
21864bc5 1065 needrelock = 0;
495d3a1e 1066 if ((vp->v_flag & VRECLAIMED) == 0 && vn_islocked(vp)) {
21864bc5
MD
1067 needrelock = 1;
1068 vn_unlock(vp);
1069 }
898c91ee
MD
1070
1071 /*
92fb0c6a
MD
1072 * WARNING! If the device destroys itself the devfs node
1073 * can disappear here.
495d3a1e
MD
1074 *
1075 * WARNING! vn_lock() will fail if the vp is in a VRECLAIM,
1076 * which can occur during umount.
898c91ee 1077 */
92fb0c6a
MD
1078 error = dev_dclose(dev, ap->a_fflag, S_IFCHR);
1079 /* node is now stale */
21864bc5 1080
495d3a1e
MD
1081 if (needrelock) {
1082 if (vn_lock(vp, LK_EXCLUSIVE | LK_RETRY) != 0) {
1083 panic("devfs_spec_close: vnode %p "
1084 "unexpectedly could not be relocked",
1085 vp);
1086 }
1087 }
21864bc5
MD
1088 } else {
1089 error = 0;
1090 }
1091 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -2- \n");
898c91ee 1092
21864bc5
MD
1093 /*
1094 * Track the actual opens and closes on the vnode. The last close
898c91ee
MD
1095 * disassociates the rdev. If the rdev is already disassociated or
1096 * the opencount is already 0, the vnode might have been revoked
1097 * and no further opencount tracking occurs.
21864bc5 1098 */
898c91ee 1099 if (dev)
21864bc5 1100 release_dev(dev);
898c91ee 1101 if (vp->v_opencount > 0)
21864bc5 1102 vop_stdclose(ap);
21864bc5
MD
1103 return(error);
1104
1105}
1106
1107
1108static int
9f889dc4 1109devfs_fo_close(struct file *fp)
21864bc5 1110{
21864bc5 1111 struct vnode *vp = (struct vnode *)fp->f_data;
898c91ee 1112 int error;
21864bc5 1113
21864bc5 1114 fp->f_ops = &badfileops;
21864bc5 1115 error = vn_close(vp, fp->f_flag);
21864bc5
MD
1116
1117 return (error);
1118}
1119
1120
1121/*
1122 * Device-optimized file table vnode read routine.
1123 *
1124 * This bypasses the VOP table and talks directly to the device. Most
1125 * filesystems just route to specfs and can make this optimization.
1126 *
1127 * MPALMOSTSAFE - acquires mplock
1128 */
1129static int
9f889dc4 1130devfs_fo_read(struct file *fp, struct uio *uio,
898c91ee 1131 struct ucred *cred, int flags)
21864bc5 1132{
898c91ee 1133 struct devfs_node *node;
21864bc5
MD
1134 struct vnode *vp;
1135 int ioflag;
1136 int error;
1137 cdev_t dev;
1138
21864bc5
MD
1139 KASSERT(uio->uio_td == curthread,
1140 ("uio_td %p is not td %p", uio->uio_td, curthread));
1141
3a1032a6
AH
1142 if (uio->uio_resid == 0)
1143 return 0;
1144
21864bc5 1145 vp = (struct vnode *)fp->f_data;
3a1032a6
AH
1146 if (vp == NULL || vp->v_type == VBAD)
1147 return EBADF;
1148
898c91ee 1149 node = DEVFS_NODE(vp);
21864bc5 1150
3a1032a6
AH
1151 if ((dev = vp->v_rdev) == NULL)
1152 return EBADF;
1153
21864bc5
MD
1154 reference_dev(dev);
1155
21864bc5
MD
1156 if ((flags & O_FOFFSET) == 0)
1157 uio->uio_offset = fp->f_offset;
1158
1159 ioflag = 0;
1160 if (flags & O_FBLOCKING) {
1161 /* ioflag &= ~IO_NDELAY; */
1162 } else if (flags & O_FNONBLOCKING) {
1163 ioflag |= IO_NDELAY;
1164 } else if (fp->f_flag & FNONBLOCK) {
1165 ioflag |= IO_NDELAY;
1166 }
1167 if (flags & O_FBUFFERED) {
1168 /* ioflag &= ~IO_DIRECT; */
1169 } else if (flags & O_FUNBUFFERED) {
1170 ioflag |= IO_DIRECT;
1171 } else if (fp->f_flag & O_DIRECT) {
1172 ioflag |= IO_DIRECT;
1173 }
1174 ioflag |= sequential_heuristic(uio, fp);
1175
1176 error = dev_dread(dev, uio, ioflag);
1177
1178 release_dev(dev);
898c91ee
MD
1179 if (node)
1180 nanotime(&node->atime);
21864bc5
MD
1181 if ((flags & O_FOFFSET) == 0)
1182 fp->f_offset = uio->uio_offset;
1183 fp->f_nextoff = uio->uio_offset;
3a1032a6 1184
21864bc5
MD
1185 return (error);
1186}
1187
1188
1189static int
9f889dc4 1190devfs_fo_write(struct file *fp, struct uio *uio,
898c91ee 1191 struct ucred *cred, int flags)
21864bc5 1192{
898c91ee 1193 struct devfs_node *node;
21864bc5
MD
1194 struct vnode *vp;
1195 int ioflag;
1196 int error;
1197 cdev_t dev;
1198
21864bc5
MD
1199 KASSERT(uio->uio_td == curthread,
1200 ("uio_td %p is not p %p", uio->uio_td, curthread));
1201
1202 vp = (struct vnode *)fp->f_data;
3a1032a6
AH
1203 if (vp == NULL || vp->v_type == VBAD)
1204 return EBADF;
1205
898c91ee 1206 node = DEVFS_NODE(vp);
3a1032a6 1207
21864bc5
MD
1208 if (vp->v_type == VREG)
1209 bwillwrite(uio->uio_resid);
3a1032a6 1210
21864bc5
MD
1211 vp = (struct vnode *)fp->f_data;
1212
3a1032a6
AH
1213 if ((dev = vp->v_rdev) == NULL)
1214 return EBADF;
1215
21864bc5
MD
1216 reference_dev(dev);
1217
1218 if ((flags & O_FOFFSET) == 0)
1219 uio->uio_offset = fp->f_offset;
1220
1221 ioflag = IO_UNIT;
1222 if (vp->v_type == VREG &&
1223 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
1224 ioflag |= IO_APPEND;
1225 }
1226
1227 if (flags & O_FBLOCKING) {
1228 /* ioflag &= ~IO_NDELAY; */
1229 } else if (flags & O_FNONBLOCKING) {
1230 ioflag |= IO_NDELAY;
1231 } else if (fp->f_flag & FNONBLOCK) {
1232 ioflag |= IO_NDELAY;
1233 }
1234 if (flags & O_FBUFFERED) {
1235 /* ioflag &= ~IO_DIRECT; */
1236 } else if (flags & O_FUNBUFFERED) {
1237 ioflag |= IO_DIRECT;
1238 } else if (fp->f_flag & O_DIRECT) {
1239 ioflag |= IO_DIRECT;
1240 }
1241 if (flags & O_FASYNCWRITE) {
1242 /* ioflag &= ~IO_SYNC; */
1243 } else if (flags & O_FSYNCWRITE) {
1244 ioflag |= IO_SYNC;
1245 } else if (fp->f_flag & O_FSYNC) {
1246 ioflag |= IO_SYNC;
1247 }
1248
1249 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
1250 ioflag |= IO_SYNC;
1251 ioflag |= sequential_heuristic(uio, fp);
1252
1253 error = dev_dwrite(dev, uio, ioflag);
1254
1255 release_dev(dev);
07dfa375
AH
1256 if (node) {
1257 nanotime(&node->atime);
898c91ee 1258 nanotime(&node->mtime);
07dfa375 1259 }
21864bc5
MD
1260
1261 if ((flags & O_FOFFSET) == 0)
1262 fp->f_offset = uio->uio_offset;
1263 fp->f_nextoff = uio->uio_offset;
3a1032a6 1264
21864bc5
MD
1265 return (error);
1266}
1267
1268
1269static int
9f889dc4 1270devfs_fo_stat(struct file *fp, struct stat *sb, struct ucred *cred)
21864bc5
MD
1271{
1272 struct vnode *vp;
3a1032a6
AH
1273 struct vattr vattr;
1274 struct vattr *vap;
1275 u_short mode;
1276 cdev_t dev;
21864bc5
MD
1277 int error;
1278
21864bc5 1279 vp = (struct vnode *)fp->f_data;
3a1032a6
AH
1280 if (vp == NULL || vp->v_type == VBAD)
1281 return EBADF;
1282
21864bc5 1283 error = vn_stat(vp, sb, cred);
3a1032a6 1284 if (error)
21864bc5 1285 return (error);
21864bc5
MD
1286
1287 vap = &vattr;
1288 error = VOP_GETATTR(vp, vap);
3a1032a6 1289 if (error)
21864bc5 1290 return (error);
21864bc5
MD
1291
1292 /*
1293 * Zero the spare stat fields
1294 */
1295 sb->st_lspare = 0;
d98152a8
MD
1296 sb->st_qspare1 = 0;
1297 sb->st_qspare2 = 0;
21864bc5
MD
1298
1299 /*
1300 * Copy from vattr table ... or not in case it's a cloned device
1301 */
1302 if (vap->va_fsid != VNOVAL)
1303 sb->st_dev = vap->va_fsid;
1304 else
1305 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
1306
1307 sb->st_ino = vap->va_fileid;
1308
1309 mode = vap->va_mode;
1310 mode |= S_IFCHR;
1311 sb->st_mode = mode;
1312
1313 if (vap->va_nlink > (nlink_t)-1)
1314 sb->st_nlink = (nlink_t)-1;
1315 else
1316 sb->st_nlink = vap->va_nlink;
3a1032a6 1317
21864bc5
MD
1318 sb->st_uid = vap->va_uid;
1319 sb->st_gid = vap->va_gid;
ca8d7677 1320 sb->st_rdev = dev2udev(DEVFS_NODE(vp)->d_dev);
2d076755 1321 sb->st_size = vap->va_bytes;
21864bc5
MD
1322 sb->st_atimespec = vap->va_atime;
1323 sb->st_mtimespec = vap->va_mtime;
1324 sb->st_ctimespec = vap->va_ctime;
1325
1326 /*
1327 * A VCHR and VBLK device may track the last access and last modified
1328 * time independantly of the filesystem. This is particularly true
1329 * because device read and write calls may bypass the filesystem.
1330 */
1331 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1332 dev = vp->v_rdev;
1333 if (dev != NULL) {
1334 if (dev->si_lastread) {
1335 sb->st_atimespec.tv_sec = dev->si_lastread;
1336 sb->st_atimespec.tv_nsec = 0;
1337 }
1338 if (dev->si_lastwrite) {
1339 sb->st_atimespec.tv_sec = dev->si_lastwrite;
1340 sb->st_atimespec.tv_nsec = 0;
1341 }
1342 }
1343 }
1344
1345 /*
1346 * According to www.opengroup.org, the meaning of st_blksize is
1347 * "a filesystem-specific preferred I/O block size for this
1348 * object. In some filesystem types, this may vary from file
1349 * to file"
1350 * Default to PAGE_SIZE after much discussion.
1351 */
1352
1353 sb->st_blksize = PAGE_SIZE;
1354
1355 sb->st_flags = vap->va_flags;
1356
1357 error = priv_check_cred(cred, PRIV_VFS_GENERATION, 0);
1358 if (error)
1359 sb->st_gen = 0;
1360 else
1361 sb->st_gen = (u_int32_t)vap->va_gen;
1362
1363 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
21864bc5 1364
21864bc5
MD
1365 return (0);
1366}
1367
1368
1369static int
9f889dc4 1370devfs_fo_kqfilter(struct file *fp, struct knote *kn)
21864bc5
MD
1371{
1372 struct vnode *vp;
21864bc5
MD
1373 int error;
1374 cdev_t dev;
1375
21864bc5
MD
1376 vp = (struct vnode *)fp->f_data;
1377 if (vp == NULL || vp->v_type == VBAD) {
1378 error = EBADF;
1379 goto done;
1380 }
21864bc5
MD
1381 if ((dev = vp->v_rdev) == NULL) {
1382 error = EBADF;
1383 goto done;
1384 }
1385 reference_dev(dev);
1386
1387 error = dev_dkqfilter(dev, kn);
1388
1389 release_dev(dev);
1390
21864bc5 1391done:
b287d649 1392 return (error);
21864bc5
MD
1393}
1394
21864bc5
MD
1395/*
1396 * MPALMOSTSAFE - acquires mplock
1397 */
1398static int
9f889dc4 1399devfs_fo_ioctl(struct file *fp, u_long com, caddr_t data,
87baaf0c 1400 struct ucred *ucred, struct sysmsg *msg)
21864bc5 1401{
898c91ee
MD
1402 struct devfs_node *node;
1403 struct vnode *vp;
21864bc5 1404 struct vnode *ovp;
21864bc5
MD
1405 cdev_t dev;
1406 int error;
1407 struct fiodname_args *name_args;
1408 size_t namlen;
1409 const char *name;
1410
898c91ee 1411 vp = ((struct vnode *)fp->f_data);
3a1032a6
AH
1412
1413 if ((dev = vp->v_rdev) == NULL)
1414 return EBADF; /* device was revoked */
1415
1416 reference_dev(dev);
21864bc5 1417
898c91ee
MD
1418 node = DEVFS_NODE(vp);
1419
1420 devfs_debug(DEVFS_DEBUG_DEBUG,
9f889dc4 1421 "devfs_fo_ioctl() called! for dev %s\n",
898c91ee 1422 dev->si_name);
21864bc5
MD
1423
1424 if (com == FIODTYPE) {
1425 *(int *)data = dev_dflags(dev) & D_TYPEMASK;
1426 error = 0;
1427 goto out;
1428 } else if (com == FIODNAME) {
1429 name_args = (struct fiodname_args *)data;
1430 name = dev->si_name;
1431 namlen = strlen(name) + 1;
1432
898c91ee
MD
1433 devfs_debug(DEVFS_DEBUG_DEBUG,
1434 "ioctl, got: FIODNAME for %s\n", name);
21864bc5
MD
1435
1436 if (namlen <= name_args->len)
1437 error = copyout(dev->si_name, name_args->name, namlen);
1438 else
1439 error = EINVAL;
1440
898c91ee
MD
1441 devfs_debug(DEVFS_DEBUG_DEBUG,
1442 "ioctl stuff: error: %d\n", error);
21864bc5
MD
1443 goto out;
1444 }
3a1032a6 1445
87baaf0c 1446 error = dev_dioctl(dev, com, data, fp->f_flag, ucred, msg);
3a1032a6 1447
07dfa375 1448#if 0
898c91ee
MD
1449 if (node) {
1450 nanotime(&node->atime);
1451 nanotime(&node->mtime);
21864bc5 1452 }
07dfa375 1453#endif
898c91ee
MD
1454 if (com == TIOCSCTTY) {
1455 devfs_debug(DEVFS_DEBUG_DEBUG,
9f889dc4 1456 "devfs_fo_ioctl: got TIOCSCTTY on %s\n",
898c91ee
MD
1457 dev->si_name);
1458 }
21864bc5 1459 if (error == 0 && com == TIOCSCTTY) {
21864bc5
MD
1460 struct proc *p = curthread->td_proc;
1461 struct session *sess;
898c91ee
MD
1462
1463 devfs_debug(DEVFS_DEBUG_DEBUG,
9f889dc4 1464 "devfs_fo_ioctl: dealing with TIOCSCTTY on %s\n",
898c91ee
MD
1465 dev->si_name);
1466 if (p == NULL) {
21864bc5
MD
1467 error = ENOTTY;
1468 goto out;
1469 }
1470 sess = p->p_session;
898c91ee
MD
1471
1472 /*
1473 * Do nothing if reassigning same control tty
1474 */
21864bc5
MD
1475 if (sess->s_ttyvp == vp) {
1476 error = 0;
1477 goto out;
1478 }
898c91ee
MD
1479
1480 /*
1481 * Get rid of reference to old control tty
1482 */
21864bc5
MD
1483 ovp = sess->s_ttyvp;
1484 vref(vp);
1485 sess->s_ttyvp = vp;
1486 if (ovp)
1487 vrele(ovp);
1488 }
1489
1490out:
3a1032a6 1491 release_dev(dev);
9f889dc4 1492 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_fo_ioctl() finished! \n");
21864bc5
MD
1493 return (error);
1494}
1495
1496
1497static int
1498devfs_spec_fsync(struct vop_fsync_args *ap)
1499{
1500 struct vnode *vp = ap->a_vp;
1501 int error;
1502
1503 if (!vn_isdisk(vp, NULL))
1504 return (0);
1505
1506 /*
1507 * Flush all dirty buffers associated with a block device.
1508 */
1509 error = vfsync(vp, ap->a_waitfor, 10000, NULL, NULL);
1510 return (error);
1511}
1512
21864bc5
MD
1513static int
1514devfs_spec_read(struct vop_read_args *ap)
1515{
898c91ee 1516 struct devfs_node *node;
21864bc5
MD
1517 struct vnode *vp;
1518 struct uio *uio;
1519 cdev_t dev;
1520 int error;
1521
1522 vp = ap->a_vp;
1523 dev = vp->v_rdev;
1524 uio = ap->a_uio;
898c91ee 1525 node = DEVFS_NODE(vp);
21864bc5
MD
1526
1527 if (dev == NULL) /* device was revoked */
1528 return (EBADF);
1529 if (uio->uio_resid == 0)
1530 return (0);
1531
1532 vn_unlock(vp);
1533 error = dev_dread(dev, uio, ap->a_ioflag);
1534 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1535
898c91ee
MD
1536 if (node)
1537 nanotime(&node->atime);
21864bc5
MD
1538
1539 return (error);
1540}
1541
1542/*
1543 * Vnode op for write
1544 *
1545 * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1546 * struct ucred *a_cred)
1547 */
21864bc5
MD
1548static int
1549devfs_spec_write(struct vop_write_args *ap)
1550{
898c91ee 1551 struct devfs_node *node;
21864bc5
MD
1552 struct vnode *vp;
1553 struct uio *uio;
1554 cdev_t dev;
1555 int error;
1556
1557 vp = ap->a_vp;
1558 dev = vp->v_rdev;
1559 uio = ap->a_uio;
898c91ee 1560 node = DEVFS_NODE(vp);
21864bc5
MD
1561
1562 KKASSERT(uio->uio_segflg != UIO_NOCOPY);
1563
1564 if (dev == NULL) /* device was revoked */
1565 return (EBADF);
1566
1567 vn_unlock(vp);
1568 error = dev_dwrite(dev, uio, ap->a_ioflag);
1569 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1570
07dfa375
AH
1571 if (node) {
1572 nanotime(&node->atime);
898c91ee 1573 nanotime(&node->mtime);
07dfa375 1574 }
21864bc5
MD
1575
1576 return (error);
1577}
1578
1579/*
1580 * Device ioctl operation.
1581 *
1582 * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
87baaf0c 1583 * int a_fflag, struct ucred *a_cred, struct sysmsg *msg)
21864bc5 1584 */
21864bc5
MD
1585static int
1586devfs_spec_ioctl(struct vop_ioctl_args *ap)
1587{
21864bc5 1588 struct vnode *vp = ap->a_vp;
898c91ee
MD
1589 struct devfs_node *node;
1590 cdev_t dev;
21864bc5
MD
1591
1592 if ((dev = vp->v_rdev) == NULL)
1593 return (EBADF); /* device was revoked */
898c91ee 1594 node = DEVFS_NODE(vp);
21864bc5 1595
07dfa375 1596#if 0
898c91ee
MD
1597 if (node) {
1598 nanotime(&node->atime);
1599 nanotime(&node->mtime);
21864bc5 1600 }
07dfa375 1601#endif
21864bc5 1602
87baaf0c
MD
1603 return (dev_dioctl(dev, ap->a_command, ap->a_data, ap->a_fflag,
1604 ap->a_cred, ap->a_sysmsg));
21864bc5
MD
1605}
1606
1607/*
21864bc5
MD
1608 * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn)
1609 */
1610/* ARGSUSED */
1611static int
1612devfs_spec_kqfilter(struct vop_kqfilter_args *ap)
1613{
21864bc5 1614 struct vnode *vp = ap->a_vp;
898c91ee
MD
1615 struct devfs_node *node;
1616 cdev_t dev;
21864bc5
MD
1617
1618 if ((dev = vp->v_rdev) == NULL)
b287d649 1619 return (EBADF); /* device was revoked (EBADF) */
898c91ee 1620 node = DEVFS_NODE(vp);
21864bc5 1621
07dfa375 1622#if 0
898c91ee
MD
1623 if (node)
1624 nanotime(&node->atime);
07dfa375 1625#endif
21864bc5 1626
b287d649 1627 return (dev_dkqfilter(dev, ap->a_kn));
21864bc5
MD
1628}
1629
21864bc5
MD
1630/*
1631 * Convert a vnode strategy call into a device strategy call. Vnode strategy
1632 * calls are not limited to device DMA limits so we have to deal with the
1633 * case.
1634 *
1635 * spec_strategy(struct vnode *a_vp, struct bio *a_bio)
1636 */
1637static int
1638devfs_spec_strategy(struct vop_strategy_args *ap)
1639{
1640 struct bio *bio = ap->a_bio;
1641 struct buf *bp = bio->bio_buf;
1642 struct buf *nbp;
1643 struct vnode *vp;
1644 struct mount *mp;
1645 int chunksize;
1646 int maxiosize;
1647
1648 if (bp->b_cmd != BUF_CMD_READ && LIST_FIRST(&bp->b_dep) != NULL)
1649 buf_start(bp);
1650
1651 /*
1652 * Collect statistics on synchronous and asynchronous read
1653 * and write counts for disks that have associated filesystems.
1654 */
1655 vp = ap->a_vp;
1656 KKASSERT(vp->v_rdev != NULL); /* XXX */
1657 if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) {
1658 if (bp->b_cmd == BUF_CMD_READ) {
21864bc5
MD
1659 if (bp->b_flags & BIO_SYNC)
1660 mp->mnt_stat.f_syncreads++;
1661 else
1662 mp->mnt_stat.f_asyncreads++;
1663 } else {
1664 if (bp->b_flags & BIO_SYNC)
1665 mp->mnt_stat.f_syncwrites++;
1666 else
1667 mp->mnt_stat.f_asyncwrites++;
1668 }
1669 }
1670
1671 /*
1672 * Device iosize limitations only apply to read and write. Shortcut
1673 * the I/O if it fits.
1674 */
1675 if ((maxiosize = vp->v_rdev->si_iosize_max) == 0) {
898c91ee
MD
1676 devfs_debug(DEVFS_DEBUG_DEBUG,
1677 "%s: si_iosize_max not set!\n",
1678 dev_dname(vp->v_rdev));
21864bc5
MD
1679 maxiosize = MAXPHYS;
1680 }
1681#if SPEC_CHAIN_DEBUG & 2
1682 maxiosize = 4096;
1683#endif
1684 if (bp->b_bcount <= maxiosize ||
1685 (bp->b_cmd != BUF_CMD_READ && bp->b_cmd != BUF_CMD_WRITE)) {
1686 dev_dstrategy_chain(vp->v_rdev, bio);
1687 return (0);
1688 }
1689
1690 /*
1691 * Clone the buffer and set up an I/O chain to chunk up the I/O.
1692 */
1693 nbp = kmalloc(sizeof(*bp), M_DEVBUF, M_INTWAIT|M_ZERO);
1694 initbufbio(nbp);
1695 buf_dep_init(nbp);
21864bc5
MD
1696 BUF_LOCK(nbp, LK_EXCLUSIVE);
1697 BUF_KERNPROC(nbp);
1698 nbp->b_vp = vp;
1699 nbp->b_flags = B_PAGING | (bp->b_flags & B_BNOCLIP);
1700 nbp->b_data = bp->b_data;
1701 nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1702 nbp->b_bio1.bio_offset = bio->bio_offset;
1703 nbp->b_bio1.bio_caller_info1.ptr = bio;
1704
1705 /*
1706 * Start the first transfer
1707 */
1708 if (vn_isdisk(vp, NULL))
1709 chunksize = vp->v_rdev->si_bsize_phys;
1710 else
1711 chunksize = DEV_BSIZE;
1712 chunksize = maxiosize / chunksize * chunksize;
1713#if SPEC_CHAIN_DEBUG & 1
898c91ee
MD
1714 devfs_debug(DEVFS_DEBUG_DEBUG,
1715 "spec_strategy chained I/O chunksize=%d\n",
1716 chunksize);
21864bc5
MD
1717#endif
1718 nbp->b_cmd = bp->b_cmd;
1719 nbp->b_bcount = chunksize;
1720 nbp->b_bufsize = chunksize; /* used to detect a short I/O */
1721 nbp->b_bio1.bio_caller_info2.index = chunksize;
1722
1723#if SPEC_CHAIN_DEBUG & 1
898c91ee
MD
1724 devfs_debug(DEVFS_DEBUG_DEBUG,
1725 "spec_strategy: chain %p offset %d/%d bcount %d\n",
1726 bp, 0, bp->b_bcount, nbp->b_bcount);
21864bc5
MD
1727#endif
1728
1729 dev_dstrategy(vp->v_rdev, &nbp->b_bio1);
1730
1731 if (DEVFS_NODE(vp)) {
1732 nanotime(&DEVFS_NODE(vp)->atime);
1733 nanotime(&DEVFS_NODE(vp)->mtime);
1734 }
1735
1736 return (0);
1737}
1738
1739/*
1740 * Chunked up transfer completion routine - chain transfers until done
77912481
MD
1741 *
1742 * NOTE: MPSAFE callback.
21864bc5
MD
1743 */
1744static
1745void
1746devfs_spec_strategy_done(struct bio *nbio)
1747{
1748 struct buf *nbp = nbio->bio_buf;
1749 struct bio *bio = nbio->bio_caller_info1.ptr; /* original bio */
1750 struct buf *bp = bio->bio_buf; /* original bp */
1751 int chunksize = nbio->bio_caller_info2.index; /* chunking */
1752 int boffset = nbp->b_data - bp->b_data;
1753
1754 if (nbp->b_flags & B_ERROR) {
1755 /*
1756 * An error terminates the chain, propogate the error back
1757 * to the original bp
1758 */
1759 bp->b_flags |= B_ERROR;
1760 bp->b_error = nbp->b_error;
1761 bp->b_resid = bp->b_bcount - boffset +
1762 (nbp->b_bcount - nbp->b_resid);
1763#if SPEC_CHAIN_DEBUG & 1
898c91ee
MD
1764 devfs_debug(DEVFS_DEBUG_DEBUG,
1765 "spec_strategy: chain %p error %d bcount %d/%d\n",
1766 bp, bp->b_error, bp->b_bcount,
1767 bp->b_bcount - bp->b_resid);
21864bc5 1768#endif
21864bc5
MD
1769 } else if (nbp->b_resid) {
1770 /*
1771 * A short read or write terminates the chain
1772 */
1773 bp->b_error = nbp->b_error;
1774 bp->b_resid = bp->b_bcount - boffset +
1775 (nbp->b_bcount - nbp->b_resid);
1776#if SPEC_CHAIN_DEBUG & 1
898c91ee
MD
1777 devfs_debug(DEVFS_DEBUG_DEBUG,
1778 "spec_strategy: chain %p short read(1) "
1779 "bcount %d/%d\n",
1780 bp, bp->b_bcount - bp->b_resid, bp->b_bcount);
21864bc5 1781#endif
21864bc5
MD
1782 } else if (nbp->b_bcount != nbp->b_bufsize) {
1783 /*
1784 * A short read or write can also occur by truncating b_bcount
1785 */
1786#if SPEC_CHAIN_DEBUG & 1
898c91ee
MD
1787 devfs_debug(DEVFS_DEBUG_DEBUG,
1788 "spec_strategy: chain %p short read(2) "
1789 "bcount %d/%d\n",
1790 bp, nbp->b_bcount + boffset, bp->b_bcount);
21864bc5
MD
1791#endif
1792 bp->b_error = 0;
1793 bp->b_bcount = nbp->b_bcount + boffset;
1794 bp->b_resid = nbp->b_resid;
21864bc5
MD
1795 } else if (nbp->b_bcount + boffset == bp->b_bcount) {
1796 /*
1797 * No more data terminates the chain
1798 */
1799#if SPEC_CHAIN_DEBUG & 1
898c91ee
MD
1800 devfs_debug(DEVFS_DEBUG_DEBUG,
1801 "spec_strategy: chain %p finished bcount %d\n",
1802 bp, bp->b_bcount);
21864bc5
MD
1803#endif
1804 bp->b_error = 0;
1805 bp->b_resid = 0;
21864bc5
MD
1806 } else {
1807 /*
1808 * Continue the chain
1809 */
1810 boffset += nbp->b_bcount;
1811 nbp->b_data = bp->b_data + boffset;
1812 nbp->b_bcount = bp->b_bcount - boffset;
1813 if (nbp->b_bcount > chunksize)
1814 nbp->b_bcount = chunksize;
1815 nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1816 nbp->b_bio1.bio_offset = bio->bio_offset + boffset;
1817
1818#if SPEC_CHAIN_DEBUG & 1
898c91ee
MD
1819 devfs_debug(DEVFS_DEBUG_DEBUG,
1820 "spec_strategy: chain %p offset %d/%d bcount %d\n",
1821 bp, boffset, bp->b_bcount, nbp->b_bcount);
21864bc5
MD
1822#endif
1823
1824 dev_dstrategy(nbp->b_vp->v_rdev, &nbp->b_bio1);
b5d7061d 1825 return;
21864bc5 1826 }
b5d7061d
MD
1827
1828 /*
1829 * Fall through to here on termination. biodone(bp) and
1830 * clean up and free nbp.
1831 */
1832 biodone(bio);
1833 BUF_UNLOCK(nbp);
1834 uninitbufbio(nbp);
1835 kfree(nbp, M_DEVBUF);
21864bc5
MD
1836}
1837
1838/*
1839 * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
1840 */
1841static int
1842devfs_spec_freeblks(struct vop_freeblks_args *ap)
1843{
1844 struct buf *bp;
1845
1846 /*
1847 * XXX: This assumes that strategy does the deed right away.
1848 * XXX: this may not be TRTTD.
1849 */
1850 KKASSERT(ap->a_vp->v_rdev != NULL);
bf390b25 1851 if ((ap->a_vp->v_rdev->si_flags & SI_CANFREE) == 0)
21864bc5
MD
1852 return (0);
1853 bp = geteblk(ap->a_length);
1854 bp->b_cmd = BUF_CMD_FREEBLKS;
1855 bp->b_bio1.bio_offset = ap->a_offset;
1856 bp->b_bcount = ap->a_length;
1857 dev_dstrategy(ap->a_vp->v_rdev, &bp->b_bio1);
1858 return (0);
1859}
1860
1861/*
1862 * Implement degenerate case where the block requested is the block
1863 * returned, and assume that the entire device is contiguous in regards
1864 * to the contiguous block range (runp and runb).
1865 *
1866 * spec_bmap(struct vnode *a_vp, off_t a_loffset,
1867 * off_t *a_doffsetp, int *a_runp, int *a_runb)
1868 */
1869static int
1870devfs_spec_bmap(struct vop_bmap_args *ap)
1871{
1872 if (ap->a_doffsetp != NULL)
1873 *ap->a_doffsetp = ap->a_loffset;
1874 if (ap->a_runp != NULL)
1875 *ap->a_runp = MAXBSIZE;
1876 if (ap->a_runb != NULL) {
1877 if (ap->a_loffset < MAXBSIZE)
1878 *ap->a_runb = (int)ap->a_loffset;
1879 else
1880 *ap->a_runb = MAXBSIZE;
1881 }
1882 return (0);
1883}
1884
1885
1886/*
1887 * Special device advisory byte-level locks.
1888 *
1889 * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
1890 * struct flock *a_fl, int a_flags)
1891 */
1892/* ARGSUSED */
1893static int
1894devfs_spec_advlock(struct vop_advlock_args *ap)
1895{
1896 return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
1897}
1898
77912481
MD
1899/*
1900 * NOTE: MPSAFE callback.
1901 */
21864bc5
MD
1902static void
1903devfs_spec_getpages_iodone(struct bio *bio)
1904{
1905 bio->bio_buf->b_cmd = BUF_CMD_DONE;
1906 wakeup(bio->bio_buf);
1907}
1908
1909/*
1910 * spec_getpages() - get pages associated with device vnode.
1911 *
1912 * Note that spec_read and spec_write do not use the buffer cache, so we
1913 * must fully implement getpages here.
1914 */
1915static int
1916devfs_spec_getpages(struct vop_getpages_args *ap)
1917{
1918 vm_offset_t kva;
1919 int error;
1920 int i, pcount, size;
1921 struct buf *bp;
1922 vm_page_t m;
1923 vm_ooffset_t offset;
1924 int toff, nextoff, nread;
1925 struct vnode *vp = ap->a_vp;
1926 int blksiz;
1927 int gotreqpage;
1928
1929 error = 0;
1930 pcount = round_page(ap->a_count) / PAGE_SIZE;
1931
1932 /*
1933 * Calculate the offset of the transfer and do sanity check.
1934 */
1935 offset = IDX_TO_OFF(ap->a_m[0]->pindex) + ap->a_offset;
1936
1937 /*
1938 * Round up physical size for real devices. We cannot round using
1939 * v_mount's block size data because v_mount has nothing to do with
1940 * the device. i.e. it's usually '/dev'. We need the physical block
1941 * size for the device itself.
1942 *
1943 * We can't use v_rdev->si_mountpoint because it only exists when the
1944 * block device is mounted. However, we can use v_rdev.
1945 */
21864bc5
MD
1946 if (vn_isdisk(vp, NULL))
1947 blksiz = vp->v_rdev->si_bsize_phys;
1948 else
1949 blksiz = DEV_BSIZE;
1950
1951 size = (ap->a_count + blksiz - 1) & ~(blksiz - 1);
1952
ad8b1a17 1953 bp = getpbuf_kva(NULL);
21864bc5
MD
1954 kva = (vm_offset_t)bp->b_data;
1955
1956 /*
1957 * Map the pages to be read into the kva.
1958 */
1959 pmap_qenter(kva, ap->a_m, pcount);
1960
1961 /* Build a minimal buffer header. */
1962 bp->b_cmd = BUF_CMD_READ;
1963 bp->b_bcount = size;
1964 bp->b_resid = 0;
77912481 1965 bsetrunningbufspace(bp, size);
21864bc5
MD
1966
1967 bp->b_bio1.bio_offset = offset;
1968 bp->b_bio1.bio_done = devfs_spec_getpages_iodone;
1969
1970 mycpu->gd_cnt.v_vnodein++;
1971 mycpu->gd_cnt.v_vnodepgsin += pcount;
1972
1973 /* Do the input. */
1974 vn_strategy(ap->a_vp, &bp->b_bio1);
1975
1976 crit_enter();
1977
1978 /* We definitely need to be at splbio here. */
1979 while (bp->b_cmd != BUF_CMD_DONE)
1980 tsleep(bp, 0, "spread", 0);
1981
1982 crit_exit();
1983
1984 if (bp->b_flags & B_ERROR) {
1985 if (bp->b_error)
1986 error = bp->b_error;
1987 else
1988 error = EIO;
1989 }
1990
1991 /*
1992 * If EOF is encountered we must zero-extend the result in order
1993 * to ensure that the page does not contain garabge. When no
1994 * error occurs, an early EOF is indicated if b_bcount got truncated.
1995 * b_resid is relative to b_bcount and should be 0, but some devices
1996 * might indicate an EOF with b_resid instead of truncating b_bcount.
1997 */
1998 nread = bp->b_bcount - bp->b_resid;
1999 if (nread < ap->a_count)
2000 bzero((caddr_t)kva + nread, ap->a_count - nread);
2001 pmap_qremove(kva, pcount);
2002
2003 gotreqpage = 0;
2004 for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) {
2005 nextoff = toff + PAGE_SIZE;
2006 m = ap->a_m[i];
2007
2008 m->flags &= ~PG_ZERO;
2009
cb1cf930
MD
2010 /*
2011 * NOTE: vm_page_undirty/clear_dirty etc do not clear the
2012 * pmap modified bit. pmap modified bit should have
2013 * already been cleared.
2014 */
21864bc5
MD
2015 if (nextoff <= nread) {
2016 m->valid = VM_PAGE_BITS_ALL;
2017 vm_page_undirty(m);
2018 } else if (toff < nread) {
2019 /*
2020 * Since this is a VM request, we have to supply the
cb1cf930 2021 * unaligned offset to allow vm_page_set_valid()
21864bc5
MD
2022 * to zero sub-DEV_BSIZE'd portions of the page.
2023 */
1a54183b
MD
2024 vm_page_set_valid(m, 0, nread - toff);
2025 vm_page_clear_dirty_end_nonincl(m, 0, nread - toff);
21864bc5
MD
2026 } else {
2027 m->valid = 0;
2028 vm_page_undirty(m);
2029 }
2030
2031 if (i != ap->a_reqpage) {
2032 /*
2033 * Just in case someone was asking for this page we
2034 * now tell them that it is ok to use.
2035 */
2036 if (!error || (m->valid == VM_PAGE_BITS_ALL)) {
2037 if (m->valid) {
b12defdc 2038 if (m->flags & PG_REFERENCED) {
21864bc5
MD
2039 vm_page_activate(m);
2040 } else {
2041 vm_page_deactivate(m);
2042 }
2043 vm_page_wakeup(m);
2044 } else {
2045 vm_page_free(m);
2046 }
2047 } else {
2048 vm_page_free(m);
2049 }
2050 } else if (m->valid) {
2051 gotreqpage = 1;
2052 /*
2053 * Since this is a VM request, we need to make the
2054 * entire page presentable by zeroing invalid sections.
2055 */
2056 if (m->valid != VM_PAGE_BITS_ALL)
2057 vm_page_zero_invalid(m, FALSE);
2058 }
2059 }
2060 if (!gotreqpage) {
2061 m = ap->a_m[ap->a_reqpage];
2062 devfs_debug(DEVFS_DEBUG_WARNING,
2063 "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n",
2064 devtoname(vp->v_rdev), error, bp, bp->b_vp);
2065 devfs_debug(DEVFS_DEBUG_WARNING,
2066 " size: %d, resid: %d, a_count: %d, valid: 0x%x\n",
2067 size, bp->b_resid, ap->a_count, m->valid);
2068 devfs_debug(DEVFS_DEBUG_WARNING,
2069 " nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n",
2070 nread, ap->a_reqpage, (u_long)m->pindex, pcount);
2071 /*
2072 * Free the buffer header back to the swap buffer pool.
2073 */
2074 relpbuf(bp, NULL);
2075 return VM_PAGER_ERROR;
2076 }
2077 /*
2078 * Free the buffer header back to the swap buffer pool.
2079 */
2080 relpbuf(bp, NULL);
07dfa375
AH
2081 if (DEVFS_NODE(ap->a_vp))
2082 nanotime(&DEVFS_NODE(ap->a_vp)->mtime);
21864bc5
MD
2083 return VM_PAGER_OK;
2084}
2085
21864bc5
MD
2086static __inline
2087int
2088sequential_heuristic(struct uio *uio, struct file *fp)
2089{
2090 /*
2091 * Sequential heuristic - detect sequential operation
2092 */
2093 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
2094 uio->uio_offset == fp->f_nextoff) {
21864bc5
MD
2095 /*
2096 * XXX we assume that the filesystem block size is
2097 * the default. Not true, but still gives us a pretty
2098 * good indicator of how sequential the read operations
2099 * are.
2100 */
898c91ee
MD
2101 int tmpseq = fp->f_seqcount;
2102
21864bc5
MD
2103 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
2104 if (tmpseq > IO_SEQMAX)
2105 tmpseq = IO_SEQMAX;
2106 fp->f_seqcount = tmpseq;
2107 return(fp->f_seqcount << IO_SEQSHIFT);
2108 }
2109
2110 /*
2111 * Not sequential, quick draw-down of seqcount
2112 */
2113 if (fp->f_seqcount > 1)
2114 fp->f_seqcount = 1;
2115 else
2116 fp->f_seqcount = 0;
2117 return(0);
2118}