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