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