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