cleanup
[dragonfly.git] / sys / vfs / ufs / ufs_readwrite.c
1 /*-
2  * Copyright (c) 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)ufs_readwrite.c     8.11 (Berkeley) 5/8/95
34  * $FreeBSD: src/sys/ufs/ufs/ufs_readwrite.c,v 1.65.2.14 2003/04/04 22:21:29 tegge Exp $
35  * $DragonFly: src/sys/vfs/ufs/ufs_readwrite.c,v 1.6 2003/06/26 19:24:50 dillon Exp $
36  */
37
38 #define BLKSIZE(a, b, c)        blksize(a, b, c)
39 #define FS                      struct fs
40 #define I_FS                    i_fs
41
42 #include <vm/vm.h>
43 #include <vm/vm_object.h>
44 #include <vm/vm_pager.h>
45 #include <vm/vm_map.h>
46 #include <vm/vnode_pager.h>
47 #include <sys/event.h>
48 #include <sys/vmmeter.h>
49 #include "opt_directio.h"
50
51 #define VN_KNOTE(vp, b) \
52         KNOTE((struct klist *)&vp->v_pollinfo.vpi_selinfo.si_note, (b))
53
54 #ifdef DIRECTIO
55 extern int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
56 #endif
57
58 /*
59  * Vnode op for reading.
60  */
61 /* ARGSUSED */
62 int
63 ffs_read(ap)
64         struct vop_read_args /* {
65                 struct vnode *a_vp;
66                 struct uio *a_uio;
67                 int a_ioflag;
68                 struct ucred *a_cred;
69         } */ *ap;
70 {
71         register struct vnode *vp;
72         register struct inode *ip;
73         register struct uio *uio;
74         register FS *fs;
75         struct buf *bp;
76         ufs_daddr_t lbn, nextlbn;
77         off_t bytesinfile;
78         long size, xfersize, blkoffset;
79         int error, orig_resid;
80         u_short mode;
81         int seqcount;
82         int ioflag;
83         vm_object_t object;
84
85         vp = ap->a_vp;
86         seqcount = ap->a_ioflag >> 16;
87         ip = VTOI(vp);
88         mode = ip->i_mode;
89         uio = ap->a_uio;
90         ioflag = ap->a_ioflag;
91 #ifdef DIRECTIO
92         if ((ioflag & IO_DIRECT) != 0) {
93                 int workdone;
94
95                 error = ffs_rawread(vp, uio, &workdone);
96                 if (error || workdone)
97                         return error;
98         }
99 #endif
100
101 #ifdef DIAGNOSTIC
102         if (uio->uio_rw != UIO_READ)
103                 panic("ffs_read: mode");
104
105         if (vp->v_type == VLNK) {
106                 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
107                         panic("ffs_read: short symlink");
108         } else if (vp->v_type != VREG && vp->v_type != VDIR)
109                 panic("ffs_read: type %d", vp->v_type);
110 #endif
111         fs = ip->I_FS;
112         if ((u_int64_t)uio->uio_offset > fs->fs_maxfilesize)
113                 return (EFBIG);
114
115         orig_resid = uio->uio_resid;
116         if (orig_resid <= 0)
117                 return (0);
118
119         object = vp->v_object;
120
121         bytesinfile = ip->i_size - uio->uio_offset;
122         if (bytesinfile <= 0) {
123                 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
124                         ip->i_flag |= IN_ACCESS;
125                 return 0;
126         }
127
128         if (object)
129                 vm_object_reference(object);
130
131 #ifdef ENABLE_VFS_IOOPT
132         /*
133          * If IO optimisation is turned on,
134          * and we are NOT a VM based IO request, 
135          * (i.e. not headed for the buffer cache)
136          * but there IS a vm object associated with it.
137          */
138         if ((ioflag & IO_VMIO) == 0 && (vfs_ioopt > 1) && object) {
139                 int nread, toread;
140
141                 toread = uio->uio_resid;
142                 if (toread > bytesinfile)
143                         toread = bytesinfile;
144                 if (toread >= PAGE_SIZE) {
145                         /*
146                          * Then if it's at least a page in size, try 
147                          * get the data from the object using vm tricks
148                          */
149                         error = uioread(toread, uio, object, &nread);
150                         if ((uio->uio_resid == 0) || (error != 0)) {
151                                 /*
152                                  * If we finished or there was an error
153                                  * then finish up (the reference previously
154                                  * obtained on object must be released).
155                                  */
156                                 if ((error == 0 ||
157                                     uio->uio_resid != orig_resid) &&
158                                     (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
159                                         ip->i_flag |= IN_ACCESS;
160
161                                 if (object)
162                                         vm_object_vndeallocate(object);
163                                 return error;
164                         }
165                 }
166         }
167 #endif
168
169         /*
170          * Ok so we couldn't do it all in one vm trick...
171          * so cycle around trying smaller bites..
172          */
173         for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
174                 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
175                         break;
176 #ifdef ENABLE_VFS_IOOPT
177                 if ((ioflag & IO_VMIO) == 0 && (vfs_ioopt > 1) && object) {
178                         /*
179                          * Obviously we didn't finish above, but we
180                          * didn't get an error either. Try the same trick again.
181                          * but this time we are looping.
182                          */
183                         int nread, toread;
184                         toread = uio->uio_resid;
185                         if (toread > bytesinfile)
186                                 toread = bytesinfile;
187
188                         /*
189                          * Once again, if there isn't enough for a
190                          * whole page, don't try optimising.
191                          */
192                         if (toread >= PAGE_SIZE) {
193                                 error = uioread(toread, uio, object, &nread);
194                                 if ((uio->uio_resid == 0) || (error != 0)) {
195                                         /*
196                                          * If we finished or there was an 
197                                          * error then finish up (the reference
198                                          * previously obtained on object must 
199                                          * be released).
200                                          */
201                                         if ((error == 0 ||
202                                             uio->uio_resid != orig_resid) &&
203                                             (vp->v_mount->mnt_flag &
204                                             MNT_NOATIME) == 0)
205                                                 ip->i_flag |= IN_ACCESS;
206                                         if (object)
207                                                 vm_object_vndeallocate(object);
208                                         return error;
209                                 }
210                                 /*
211                                  * To get here we didnt't finish or err.
212                                  * If we did get some data,
213                                  * loop to try another bite.
214                                  */
215                                 if (nread > 0) {
216                                         continue;
217                                 }
218                         }
219                 }
220 #endif
221
222                 lbn = lblkno(fs, uio->uio_offset);
223                 nextlbn = lbn + 1;
224
225                 /*
226                  * size of buffer.  The buffer representing the
227                  * end of the file is rounded up to the size of
228                  * the block type ( fragment or full block, 
229                  * depending ).
230                  */
231                 size = BLKSIZE(fs, ip, lbn);
232                 blkoffset = blkoff(fs, uio->uio_offset);
233                 
234                 /*
235                  * The amount we want to transfer in this iteration is
236                  * one FS block less the amount of the data before
237                  * our startpoint (duh!)
238                  */
239                 xfersize = fs->fs_bsize - blkoffset;
240
241                 /*
242                  * But if we actually want less than the block,
243                  * or the file doesn't have a whole block more of data,
244                  * then use the lesser number.
245                  */
246                 if (uio->uio_resid < xfersize)
247                         xfersize = uio->uio_resid;
248                 if (bytesinfile < xfersize)
249                         xfersize = bytesinfile;
250
251                 if (lblktosize(fs, nextlbn) >= ip->i_size) {
252                         /*
253                          * Don't do readahead if this is the end of the file.
254                          */
255                         error = bread(vp, lbn, size, &bp);
256                 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
257                         /* 
258                          * Otherwise if we are allowed to cluster,
259                          * grab as much as we can.
260                          *
261                          * XXX  This may not be a win if we are not
262                          * doing sequential access.
263                          */
264                         error = cluster_read(vp, ip->i_size, lbn,
265                                 size, uio->uio_resid, seqcount, &bp);
266                 } else if (seqcount > 1) {
267                         /*
268                          * If we are NOT allowed to cluster, then
269                          * if we appear to be acting sequentially,
270                          * fire off a request for a readahead
271                          * as well as a read. Note that the 4th and 5th
272                          * arguments point to arrays of the size specified in
273                          * the 6th argument.
274                          */
275                         int nextsize = BLKSIZE(fs, ip, nextlbn);
276                         error = breadn(vp, lbn,
277                             size, &nextlbn, &nextsize, 1, &bp);
278                 } else {
279                         /*
280                          * Failing all of the above, just read what the 
281                          * user asked for. Interestingly, the same as
282                          * the first option above.
283                          */
284                         error = bread(vp, lbn, size, &bp);
285                 }
286                 if (error) {
287                         brelse(bp);
288                         bp = NULL;
289                         break;
290                 }
291
292                 /*
293                  * If IO_DIRECT then set B_DIRECT for the buffer.  This
294                  * will cause us to attempt to release the buffer later on
295                  * and will cause the buffer cache to attempt to free the
296                  * underlying pages.
297                  */
298                 if (ioflag & IO_DIRECT)
299                         bp->b_flags |= B_DIRECT;
300
301                 /*
302                  * We should only get non-zero b_resid when an I/O error
303                  * has occurred, which should cause us to break above.
304                  * However, if the short read did not cause an error,
305                  * then we want to ensure that we do not uiomove bad
306                  * or uninitialized data.
307                  *
308                  * XXX b_resid is only valid when an actual I/O has occured
309                  * and may be incorrect if the buffer is B_CACHE or if the
310                  * last op on the buffer was a failed write.  This KASSERT
311                  * is a precursor to removing it from the UFS code.
312                  */
313                 KASSERT(bp->b_resid == 0, ("bp->b_resid != 0"));
314                 size -= bp->b_resid;
315                 if (size < xfersize) {
316                         if (size == 0)
317                                 break;
318                         xfersize = size;
319                 }
320
321 #ifdef ENABLE_VFS_IOOPT
322                 if (vfs_ioopt && object &&
323                     (bp->b_flags & B_VMIO) &&
324                     ((blkoffset & PAGE_MASK) == 0) &&
325                     ((xfersize & PAGE_MASK) == 0)) {
326                         /*
327                          * If VFS IO  optimisation is turned on,
328                          * and it's an exact page multiple
329                          * And a normal VM based op,
330                          * then use uiomiveco()
331                          */
332                         error =
333                                 uiomoveco((char *)bp->b_data + blkoffset,
334                                         (int)xfersize, uio, object);
335                 } else 
336 #endif
337                 {
338                         /*
339                          * otherwise use the general form
340                          */
341                         error =
342                                 uiomove((char *)bp->b_data + blkoffset,
343                                         (int)xfersize, uio);
344                 }
345
346                 if (error)
347                         break;
348
349                 if ((ioflag & (IO_VMIO|IO_DIRECT)) && 
350                     (LIST_FIRST(&bp->b_dep) == NULL)) {
351                         /*
352                          * If there are no dependencies, and it's VMIO,
353                          * then we don't need the buf, mark it available
354                          * for freeing. The VM has the data.
355                          */
356                         bp->b_flags |= B_RELBUF;
357                         brelse(bp);
358                 } else {
359                         /*
360                          * Otherwise let whoever
361                          * made the request take care of
362                          * freeing it. We just queue
363                          * it onto another list.
364                          */
365                         bqrelse(bp);
366                 }
367         }
368
369         /* 
370          * This can only happen in the case of an error
371          * because the loop above resets bp to NULL on each iteration
372          * and on normal completion has not set a new value into it.
373          * so it must have come from a 'break' statement
374          */
375         if (bp != NULL) {
376                 if ((ioflag & (IO_VMIO|IO_DIRECT)) && 
377                     (LIST_FIRST(&bp->b_dep) == NULL)) {
378                         bp->b_flags |= B_RELBUF;
379                         brelse(bp);
380                 } else {
381                         bqrelse(bp);
382                 }
383         }
384
385         if (object)
386                 vm_object_vndeallocate(object);
387         if ((error == 0 || uio->uio_resid != orig_resid) &&
388             (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
389                 ip->i_flag |= IN_ACCESS;
390         return (error);
391 }
392
393 /*
394  * Vnode op for writing.
395  */
396 int
397 ffs_write(ap)
398         struct vop_write_args /* {
399                 struct vnode *a_vp;
400                 struct uio *a_uio;
401                 int a_ioflag;
402                 struct ucred *a_cred;
403         } */ *ap;
404 {
405         register struct vnode *vp;
406         register struct uio *uio;
407         register struct inode *ip;
408         register FS *fs;
409         struct buf *bp;
410         ufs_daddr_t lbn;
411         off_t osize;
412         int seqcount;
413         int blkoffset, error, extended, flags, ioflag, resid, size, xfersize;
414         vm_object_t object;
415         struct thread *td;
416
417         extended = 0;
418         seqcount = ap->a_ioflag >> 16;
419         ioflag = ap->a_ioflag;
420         uio = ap->a_uio;
421         vp = ap->a_vp;
422         ip = VTOI(vp);
423
424         object = vp->v_object;
425         if (object)
426                 vm_object_reference(object);
427
428 #ifdef DIAGNOSTIC
429         if (uio->uio_rw != UIO_WRITE)
430                 panic("ffs_write: mode");
431 #endif
432
433         switch (vp->v_type) {
434         case VREG:
435                 if (ioflag & IO_APPEND)
436                         uio->uio_offset = ip->i_size;
437                 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size) {
438                         if (object)
439                                 vm_object_vndeallocate(object);
440                         return (EPERM);
441                 }
442                 /* FALLTHROUGH */
443         case VLNK:
444                 break;
445         case VDIR:
446                 panic("ffs_write: dir write");
447                 break;
448         default:
449                 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
450                         (int)uio->uio_offset,
451                         (int)uio->uio_resid
452                 );
453         }
454
455         fs = ip->I_FS;
456         if (uio->uio_offset < 0 ||
457             (u_int64_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize) {
458                 if (object)
459                         vm_object_vndeallocate(object);
460                 return (EFBIG);
461         }
462         /*
463          * Maybe this should be above the vnode op call, but so long as
464          * file servers have no limits, I don't think it matters.
465          */
466         td = uio->uio_td;
467         if (vp->v_type == VREG && td->td_proc &&
468             uio->uio_offset + uio->uio_resid >
469             td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
470                 psignal(td->td_proc, SIGXFSZ);
471                 if (object)
472                         vm_object_vndeallocate(object);
473                 return (EFBIG);
474         }
475
476         resid = uio->uio_resid;
477         osize = ip->i_size;
478
479         /*
480          * NOTE! These B_ flags are actually balloc-only flags, not buffer
481          * flags.  They are similar to the BA_ flags in -current.
482          */
483         if (seqcount > B_SEQMAX)
484                 flags = B_SEQMAX << B_SEQSHIFT;
485         else
486                 flags = seqcount << B_SEQSHIFT;
487         if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
488                 flags |= B_SYNC;
489
490         if (object && (object->flags & OBJ_OPT)) {
491                 vm_freeze_copyopts(object,
492                         OFF_TO_IDX(uio->uio_offset),
493                         OFF_TO_IDX(uio->uio_offset + uio->uio_resid + PAGE_MASK));
494         }
495
496         for (error = 0; uio->uio_resid > 0;) {
497                 lbn = lblkno(fs, uio->uio_offset);
498                 blkoffset = blkoff(fs, uio->uio_offset);
499                 xfersize = fs->fs_bsize - blkoffset;
500                 if (uio->uio_resid < xfersize)
501                         xfersize = uio->uio_resid;
502
503                 if (uio->uio_offset + xfersize > ip->i_size)
504                         vnode_pager_setsize(vp, uio->uio_offset + xfersize);
505
506                 /*      
507                  * We must perform a read-before-write if the transfer
508                  * size does not cover the entire buffer.
509                  */
510                 if (fs->fs_bsize > xfersize)
511                         flags |= B_CLRBUF;
512                 else
513                         flags &= ~B_CLRBUF;
514 /* XXX is uio->uio_offset the right thing here? */
515                 error = VOP_BALLOC(vp, uio->uio_offset, xfersize,
516                     ap->a_cred, flags, &bp);
517                 if (error != 0)
518                         break;
519                 /*
520                  * If the buffer is not valid and we did not clear garbage
521                  * out above, we have to do so here even though the write
522                  * covers the entire buffer in order to avoid a mmap()/write
523                  * race where another process may see the garbage prior to
524                  * the uiomove() for a write replacing it.
525                  */
526                 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
527                         vfs_bio_clrbuf(bp);
528                 if (ioflag & IO_DIRECT)
529                         bp->b_flags |= B_DIRECT;
530                 if (ioflag & IO_NOWDRAIN)
531                         bp->b_flags |= B_NOWDRAIN;
532
533                 if (uio->uio_offset + xfersize > ip->i_size) {
534                         ip->i_size = uio->uio_offset + xfersize;
535                         extended = 1;
536                 }
537
538                 size = BLKSIZE(fs, ip, lbn) - bp->b_resid;
539                 if (size < xfersize)
540                         xfersize = size;
541
542                 error =
543                     uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
544                 if ((ioflag & (IO_VMIO|IO_DIRECT)) && 
545                     (LIST_FIRST(&bp->b_dep) == NULL)) {
546                         bp->b_flags |= B_RELBUF;
547                 }
548
549                 /*
550                  * If IO_SYNC each buffer is written synchronously.  Otherwise
551                  * if we have a severe page deficiency write the buffer 
552                  * asynchronously.  Otherwise try to cluster, and if that
553                  * doesn't do it then either do an async write (if O_DIRECT),
554                  * or a delayed write (if not).
555                  */
556
557                 if (ioflag & IO_SYNC) {
558                         (void)bwrite(bp);
559                 } else if (vm_page_count_severe() || 
560                             buf_dirty_count_severe() ||
561                             (ioflag & IO_ASYNC)) {
562                         bp->b_flags |= B_CLUSTEROK;
563                         bawrite(bp);
564                 } else if (xfersize + blkoffset == fs->fs_bsize) {
565                         if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
566                                 bp->b_flags |= B_CLUSTEROK;
567                                 cluster_write(bp, ip->i_size, seqcount);
568                         } else {
569                                 bawrite(bp);
570                         }
571                 } else if (ioflag & IO_DIRECT) {
572                         bp->b_flags |= B_CLUSTEROK;
573                         bawrite(bp);
574                 } else {
575                         bp->b_flags |= B_CLUSTEROK;
576                         bdwrite(bp);
577                 }
578                 if (error || xfersize == 0)
579                         break;
580                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
581         }
582         /*
583          * If we successfully wrote any data, and we are not the superuser
584          * we clear the setuid and setgid bits as a precaution against
585          * tampering.
586          */
587         if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
588                 ip->i_mode &= ~(ISUID | ISGID);
589         if (resid > uio->uio_resid)
590                 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
591         if (error) {
592                 if (ioflag & IO_UNIT) {
593                         (void)UFS_TRUNCATE(vp, osize,
594                             ioflag & IO_SYNC, ap->a_cred, uio->uio_td);
595                         uio->uio_offset -= resid - uio->uio_resid;
596                         uio->uio_resid = resid;
597                 }
598         } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
599                 error = UFS_UPDATE(vp, 1);
600
601         if (object)
602                 vm_object_vndeallocate(object);
603
604         return (error);
605 }
606
607
608 /*
609  * get page routine
610  */
611 int
612 ffs_getpages(ap)
613         struct vop_getpages_args *ap;
614 {
615         off_t foff, physoffset;
616         int i, size, bsize;
617         struct vnode *dp, *vp;
618         vm_object_t obj;
619         vm_pindex_t pindex, firstindex;
620         vm_page_t mreq;
621         int bbackwards, bforwards;
622         int pbackwards, pforwards;
623         int firstpage;
624         int reqlblkno;
625         daddr_t reqblkno;
626         int poff;
627         int pcount;
628         int rtval;
629         int pagesperblock;
630
631
632         pcount = round_page(ap->a_count) / PAGE_SIZE;
633         mreq = ap->a_m[ap->a_reqpage];
634         firstindex = ap->a_m[0]->pindex;
635
636         /*
637          * if ANY DEV_BSIZE blocks are valid on a large filesystem block,
638          * then the entire page is valid.  Since the page may be mapped,
639          * user programs might reference data beyond the actual end of file
640          * occuring within the page.  We have to zero that data.
641          */
642         if (mreq->valid) {
643                 if (mreq->valid != VM_PAGE_BITS_ALL)
644                         vm_page_zero_invalid(mreq, TRUE);
645                 for (i = 0; i < pcount; i++) {
646                         if (i != ap->a_reqpage) {
647                                 vm_page_free(ap->a_m[i]);
648                         }
649                 }
650                 return VM_PAGER_OK;
651         }
652
653         vp = ap->a_vp;
654         obj = vp->v_object;
655         bsize = vp->v_mount->mnt_stat.f_iosize;
656         pindex = mreq->pindex;
657         foff = IDX_TO_OFF(pindex) /* + ap->a_offset should be zero */;
658
659         if (bsize < PAGE_SIZE)
660                 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
661                                                     ap->a_count,
662                                                     ap->a_reqpage);
663
664         /*
665          * foff is the file offset of the required page
666          * reqlblkno is the logical block that contains the page
667          * poff is the index of the page into the logical block
668          */
669         reqlblkno = foff / bsize;
670         poff = (foff % bsize) / PAGE_SIZE;
671
672         if ( VOP_BMAP( vp, reqlblkno, &dp, &reqblkno,
673                 &bforwards, &bbackwards) || (reqblkno == -1)) {
674                 for(i = 0; i < pcount; i++) {
675                         if (i != ap->a_reqpage)
676                                 vm_page_free(ap->a_m[i]);
677                 }
678                 if (reqblkno == -1) {
679                         if ((mreq->flags & PG_ZERO) == 0)
680                                 vm_page_zero_fill(mreq);
681                         vm_page_undirty(mreq);
682                         mreq->valid = VM_PAGE_BITS_ALL;
683                         return VM_PAGER_OK;
684                 } else {
685                         return VM_PAGER_ERROR;
686                 }
687         }
688
689         physoffset = (off_t)reqblkno * DEV_BSIZE + poff * PAGE_SIZE;
690         pagesperblock = bsize / PAGE_SIZE;
691         /*
692          * find the first page that is contiguous...
693          * note that pbackwards is the number of pages that are contiguous
694          * backwards.
695          */
696         firstpage = 0;
697         if (ap->a_count) {
698                 pbackwards = poff + bbackwards * pagesperblock;
699                 if (ap->a_reqpage > pbackwards) {
700                         firstpage = ap->a_reqpage - pbackwards;
701                         for(i=0;i<firstpage;i++)
702                                 vm_page_free(ap->a_m[i]);
703                 }
704
705         /*
706          * pforwards is the number of pages that are contiguous
707          * after the current page.
708          */
709                 pforwards = (pagesperblock - (poff + 1)) +
710                         bforwards * pagesperblock;
711                 if (pforwards < (pcount - (ap->a_reqpage + 1))) {
712                         for( i = ap->a_reqpage + pforwards + 1; i < pcount; i++)
713                                 vm_page_free(ap->a_m[i]);
714                         pcount = ap->a_reqpage + pforwards + 1;
715                 }
716
717         /*
718          * number of pages for I/O corrected for the non-contig pages at
719          * the beginning of the array.
720          */
721                 pcount -= firstpage;
722         }
723
724         /*
725          * calculate the size of the transfer
726          */
727
728         size = pcount * PAGE_SIZE;
729
730         if ((IDX_TO_OFF(ap->a_m[firstpage]->pindex) + size) >
731                 obj->un_pager.vnp.vnp_size)
732                 size = obj->un_pager.vnp.vnp_size -
733                         IDX_TO_OFF(ap->a_m[firstpage]->pindex);
734
735         physoffset -= foff;
736         rtval = VOP_GETPAGES(dp, &ap->a_m[firstpage], size,
737                 (ap->a_reqpage - firstpage), physoffset);
738
739         return (rtval);
740 }
741
742 /*
743  * put page routine
744  *
745  * XXX By default, wimp out... note that a_offset is ignored (and always
746  * XXX has been).
747  */
748 int
749 ffs_putpages(ap)
750         struct vop_putpages_args *ap;
751 {
752         return vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
753                 ap->a_sync, ap->a_rtvals);
754 }