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