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