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