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