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