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