Merge from vendor branch TCPDUMP:
[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.24 2007/08/28 01:09:08 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 <sys/sysctl.h>
50 #include <vm/vm_page2.h>
51
52 #include "opt_directio.h"
53
54 #define VN_KNOTE(vp, b) \
55         KNOTE((struct klist *)&vp->v_pollinfo.vpi_selinfo.si_note, (b))
56
57 #ifdef DIRECTIO
58 extern int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
59 #endif
60
61 SYSCTL_DECL(_vfs_ffs);
62 static int getpages_uses_bufcache = 0;
63 SYSCTL_INT(_vfs_ffs, OID_AUTO, getpages_uses_bufcache, CTLFLAG_RW, &getpages_uses_bufcache, 0, "");
64
65 /*
66  * Vnode op for reading.
67  *
68  * ffs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
69  *          struct ucred *a_cred)
70  */
71 /* ARGSUSED */
72 int
73 ffs_read(struct vop_read_args *ap)
74 {
75         struct vnode *vp;
76         struct inode *ip;
77         struct uio *uio;
78         FS *fs;
79         struct buf *bp;
80         off_t bytesinfile;
81         int xfersize, blkoffset;
82         int error, orig_resid;
83         u_short mode;
84         int seqcount;
85         int ioflag;
86
87         vp = ap->a_vp;
88         seqcount = ap->a_ioflag >> 16;
89         ip = VTOI(vp);
90         mode = ip->i_mode;
91         uio = ap->a_uio;
92         ioflag = ap->a_ioflag;
93 #ifdef DIRECTIO
94         if ((ioflag & IO_DIRECT) != 0) {
95                 int workdone;
96
97                 error = ffs_rawread(vp, uio, &workdone);
98                 if (error || workdone)
99                         return error;
100         }
101 #endif
102
103 #ifdef DIAGNOSTIC
104         if (uio->uio_rw != UIO_READ)
105                 panic("ffs_read: mode");
106
107         if (vp->v_type == VLNK) {
108                 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
109                         panic("ffs_read: short symlink");
110         } else if (vp->v_type != VREG && vp->v_type != VDIR)
111                 panic("ffs_read: type %d", vp->v_type);
112 #endif
113         fs = ip->I_FS;
114         if ((uint64_t)uio->uio_offset > fs->fs_maxfilesize)
115                 return (EFBIG);
116
117         orig_resid = uio->uio_resid;
118         if (orig_resid <= 0)
119                 return (0);
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         /*
129          * Ok so we couldn't do it all in one vm trick...
130          * so cycle around trying smaller bites..
131          */
132         for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
133                 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
134                         break;
135
136                 error = ffs_blkatoff_ra(vp, uio->uio_offset, NULL,
137                                         &bp, seqcount);
138                 if (error)
139                         break;
140
141                 /*
142                  * If IO_DIRECT then set B_DIRECT for the buffer.  This
143                  * will cause us to attempt to release the buffer later on
144                  * and will cause the buffer cache to attempt to free the
145                  * underlying pages.
146                  */
147                 if (ioflag & IO_DIRECT)
148                         bp->b_flags |= B_DIRECT;
149
150                 /*
151                  * We should only get non-zero b_resid when an I/O error
152                  * has occurred, which should cause us to break above.
153                  * However, if the short read did not cause an error,
154                  * then we want to ensure that we do not uiomove bad
155                  * or uninitialized data.
156                  *
157                  * XXX b_resid is only valid when an actual I/O has occured
158                  * and may be incorrect if the buffer is B_CACHE or if the
159                  * last op on the buffer was a failed write.  This KASSERT
160                  * is a precursor to removing it from the UFS code.
161                  */
162                 KASSERT(bp->b_resid == 0, ("bp->b_resid != 0"));
163
164                 /*
165                  * Calculate how much data we can copy
166                  */
167                 blkoffset = blkoff(fs, uio->uio_offset);
168                 xfersize = bp->b_bufsize - blkoffset;
169                 if (xfersize > uio->uio_resid)
170                         xfersize = uio->uio_resid;
171                 if (xfersize > bytesinfile)
172                         xfersize = bytesinfile;
173                 if (xfersize <= 0) {
174                         panic("ufs_readwrite: impossible xfersize: %d",
175                               xfersize);
176                 }
177
178                 /*
179                  * otherwise use the general form
180                  */
181                 error = uiomove((char *)bp->b_data + blkoffset, 
182                                 (int)xfersize, uio);
183
184                 if (error)
185                         break;
186
187                 if ((ioflag & (IO_VMIO|IO_DIRECT)) && 
188                     (LIST_FIRST(&bp->b_dep) == NULL)) {
189                         /*
190                          * If there are no dependencies, and it's VMIO,
191                          * then we don't need the buf, mark it available
192                          * for freeing. The VM has the data.
193                          */
194                         bp->b_flags |= B_RELBUF;
195                         brelse(bp);
196                 } else {
197                         /*
198                          * Otherwise let whoever
199                          * made the request take care of
200                          * freeing it. We just queue
201                          * it onto another list.
202                          */
203                         bqrelse(bp);
204                 }
205         }
206
207         /* 
208          * This can only happen in the case of an error
209          * because the loop above resets bp to NULL on each iteration
210          * and on normal completion has not set a new value into it.
211          * so it must have come from a 'break' statement
212          */
213         if (bp != NULL) {
214                 if ((ioflag & (IO_VMIO|IO_DIRECT)) && 
215                     (LIST_FIRST(&bp->b_dep) == NULL)) {
216                         bp->b_flags |= B_RELBUF;
217                         brelse(bp);
218                 } else {
219                         bqrelse(bp);
220                 }
221         }
222
223         if ((error == 0 || uio->uio_resid != orig_resid) &&
224             (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
225                 ip->i_flag |= IN_ACCESS;
226         return (error);
227 }
228
229 /*
230  * Vnode op for writing.
231  *
232  * ffs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
233  *           struct ucred *a_cred)
234  */
235 int
236 ffs_write(struct vop_write_args *ap)
237 {
238         struct vnode *vp;
239         struct uio *uio;
240         struct inode *ip;
241         FS *fs;
242         struct buf *bp;
243         ufs_daddr_t lbn;
244         off_t osize;
245         int seqcount;
246         int blkoffset, error, extended, flags, ioflag, resid, size, xfersize;
247         struct thread *td;
248
249         extended = 0;
250         seqcount = ap->a_ioflag >> 16;
251         ioflag = ap->a_ioflag;
252         uio = ap->a_uio;
253         vp = ap->a_vp;
254         ip = VTOI(vp);
255
256 #ifdef DIAGNOSTIC
257         if (uio->uio_rw != UIO_WRITE)
258                 panic("ffs_write: mode");
259 #endif
260
261         switch (vp->v_type) {
262         case VREG:
263                 if (ioflag & IO_APPEND)
264                         uio->uio_offset = ip->i_size;
265                 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
266                         return (EPERM);
267                 /* FALLTHROUGH */
268         case VLNK:
269                 break;
270         case VDIR:
271                 panic("ffs_write: dir write");
272                 break;
273         default:
274                 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
275                         (int)uio->uio_offset,
276                         (int)uio->uio_resid
277                 );
278         }
279
280         fs = ip->I_FS;
281         if (uio->uio_offset < 0 ||
282             (uint64_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize) {
283                 return (EFBIG);
284         }
285         /*
286          * Maybe this should be above the vnode op call, but so long as
287          * file servers have no limits, I don't think it matters.
288          */
289         td = uio->uio_td;
290         if (vp->v_type == VREG && td && td->td_proc &&
291             uio->uio_offset + uio->uio_resid >
292             td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
293                 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
294                 return (EFBIG);
295         }
296
297         resid = uio->uio_resid;
298         osize = ip->i_size;
299
300         /*
301          * NOTE! These B_ flags are actually balloc-only flags, not buffer
302          * flags.  They are similar to the BA_ flags in fbsd.
303          */
304         if (seqcount > B_SEQMAX)
305                 flags = B_SEQMAX << B_SEQSHIFT;
306         else
307                 flags = seqcount << B_SEQSHIFT;
308         if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
309                 flags |= B_SYNC;
310
311         for (error = 0; uio->uio_resid > 0;) {
312                 lbn = lblkno(fs, uio->uio_offset);
313                 blkoffset = blkoff(fs, uio->uio_offset);
314                 xfersize = fs->fs_bsize - blkoffset;
315                 if (uio->uio_resid < xfersize)
316                         xfersize = uio->uio_resid;
317
318                 if (uio->uio_offset + xfersize > ip->i_size)
319                         vnode_pager_setsize(vp, uio->uio_offset + xfersize);
320
321                 /*      
322                  * We must perform a read-before-write if the transfer
323                  * size does not cover the entire buffer, or if doing
324                  * a dummy write to flush the buffer.
325                  */
326                 if (xfersize < fs->fs_bsize || uio->uio_segflg == UIO_NOCOPY)
327                         flags |= B_CLRBUF;
328                 else
329                         flags &= ~B_CLRBUF;
330 /* XXX is uio->uio_offset the right thing here? */
331                 error = VOP_BALLOC(vp, uio->uio_offset, xfersize,
332                     ap->a_cred, flags, &bp);
333                 if (error != 0)
334                         break;
335                 /*
336                  * If the buffer is not valid and we did not clear garbage
337                  * out above, we have to do so here even though the write
338                  * covers the entire buffer in order to avoid a mmap()/write
339                  * race where another process may see the garbage prior to
340                  * the uiomove() for a write replacing it.
341                  */
342                 if ((bp->b_flags & B_CACHE) == 0 && (flags & B_CLRBUF) == 0)
343                         vfs_bio_clrbuf(bp);
344                 if (ioflag & IO_DIRECT)
345                         bp->b_flags |= B_DIRECT;
346                 if (ioflag & IO_NOWDRAIN)
347                         bp->b_flags |= B_NOWDRAIN;
348                 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
349                         bp->b_flags |= B_NOCACHE;
350
351                 if (uio->uio_offset + xfersize > ip->i_size) {
352                         ip->i_size = uio->uio_offset + xfersize;
353                         extended = 1;
354                 }
355
356                 size = BLKSIZE(fs, ip, lbn) - bp->b_resid;
357                 if (size < xfersize)
358                         xfersize = size;
359
360                 error =
361                     uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
362                 if ((ioflag & (IO_VMIO|IO_DIRECT)) && 
363                     (LIST_FIRST(&bp->b_dep) == NULL)) {
364                         bp->b_flags |= B_RELBUF;
365                 }
366
367                 /*
368                  * If IO_SYNC each buffer is written synchronously.  Otherwise
369                  * if we have a severe page deficiency write the buffer 
370                  * asynchronously.  Otherwise try to cluster, and if that
371                  * doesn't do it then either do an async write (if O_DIRECT),
372                  * or a delayed write (if not).
373                  */
374
375                 if (ioflag & IO_SYNC) {
376                         (void)bwrite(bp);
377                 } else if (vm_page_count_severe() || 
378                             buf_dirty_count_severe() ||
379                             (ioflag & IO_ASYNC)) {
380                         bp->b_flags |= B_CLUSTEROK;
381                         bawrite(bp);
382                 } else if (xfersize + blkoffset == fs->fs_bsize) {
383                         if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
384                                 bp->b_flags |= B_CLUSTEROK;
385                                 cluster_write(bp, (off_t)ip->i_size, seqcount);
386                         } else {
387                                 bawrite(bp);
388                         }
389                 } else if (ioflag & IO_DIRECT) {
390                         bp->b_flags |= B_CLUSTEROK;
391                         bawrite(bp);
392                 } else {
393                         bp->b_flags |= B_CLUSTEROK;
394                         bdwrite(bp);
395                 }
396                 if (error || xfersize == 0)
397                         break;
398                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
399         }
400         /*
401          * If we successfully wrote any data, and we are not the superuser
402          * we clear the setuid and setgid bits as a precaution against
403          * tampering.
404          */
405         if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
406                 ip->i_mode &= ~(ISUID | ISGID);
407         if (resid > uio->uio_resid)
408                 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
409         if (error) {
410                 if (ioflag & IO_UNIT) {
411                         (void)ffs_truncate(vp, osize, ioflag & IO_SYNC,
412                                            ap->a_cred);
413                         uio->uio_offset -= resid - uio->uio_resid;
414                         uio->uio_resid = resid;
415                 }
416         } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) {
417                 error = ffs_update(vp, 1);
418         }
419
420         return (error);
421 }
422
423
424 /*
425  * get page routine
426  */
427 int
428 ffs_getpages(struct vop_getpages_args *ap)
429 {
430         off_t foff, physoffset;
431         int i, size, bsize;
432         struct vnode *dp, *vp;
433         vm_object_t obj;
434         vm_pindex_t pindex, firstindex;
435         vm_page_t mreq;
436         int bbackwards, bforwards;
437         int pbackwards, pforwards;
438         int firstpage;
439         off_t reqoffset;
440         off_t doffset;
441         int poff;
442         int pcount;
443         int rtval;
444         int pagesperblock;
445
446         /*
447          * If set just use the system standard getpages which issues a
448          * UIO_NOCOPY VOP_READ.
449          */
450         if (getpages_uses_bufcache) {
451                 return vop_stdgetpages(ap);
452         }
453
454         pcount = round_page(ap->a_count) / PAGE_SIZE;
455         mreq = ap->a_m[ap->a_reqpage];
456         firstindex = ap->a_m[0]->pindex;
457
458         /*
459          * if ANY DEV_BSIZE blocks are valid on a large filesystem block,
460          * then the entire page is valid.  Since the page may be mapped,
461          * user programs might reference data beyond the actual end of file
462          * occuring within the page.  We have to zero that data.
463          */
464         if (mreq->valid) {
465                 if (mreq->valid != VM_PAGE_BITS_ALL)
466                         vm_page_zero_invalid(mreq, TRUE);
467                 for (i = 0; i < pcount; i++) {
468                         if (i != ap->a_reqpage) {
469                                 vm_page_free(ap->a_m[i]);
470                         }
471                 }
472                 return VM_PAGER_OK;
473         }
474
475         vp = ap->a_vp;
476         obj = vp->v_object;
477         bsize = vp->v_mount->mnt_stat.f_iosize;
478         pindex = mreq->pindex;
479         foff = IDX_TO_OFF(pindex) /* + ap->a_offset should be zero */;
480
481         if (bsize < PAGE_SIZE)
482                 return vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
483                                                     ap->a_count,
484                                                     ap->a_reqpage);
485
486         /*
487          * foff is the file offset of the required page
488          * reqlblkno is the logical block that contains the page
489          * poff is the bytes offset of the page in the logical block
490          */
491         poff = (int)(foff % bsize);
492         reqoffset = foff - poff;
493
494         if (VOP_BMAP(vp, reqoffset, &doffset, &bforwards, &bbackwards) ||
495             doffset == NOOFFSET
496         ) {
497                 for (i = 0; i < pcount; i++) {
498                         if (i != ap->a_reqpage)
499                                 vm_page_free(ap->a_m[i]);
500                 }
501                 if (doffset == NOOFFSET) {
502                         if ((mreq->flags & PG_ZERO) == 0)
503                                 vm_page_zero_fill(mreq);
504                         vm_page_undirty(mreq);
505                         mreq->valid = VM_PAGE_BITS_ALL;
506                         return VM_PAGER_OK;
507                 } else {
508                         return VM_PAGER_ERROR;
509                 }
510         }
511
512         physoffset = doffset + poff;
513         pagesperblock = bsize / PAGE_SIZE;
514
515         /*
516          * find the first page that is contiguous.
517          *
518          * bforwards and bbackwards are the number of contiguous bytes
519          * available before and after the block offset.  poff is the page
520          * offset, in bytes, relative to the block offset.
521          *
522          * pforwards and pbackwards are the number of contiguous pages
523          * relative to the requested page, non-inclusive of the requested
524          * page (so a pbackwards and  pforwards of 0 indicates just the
525          * requested page).
526          */
527         firstpage = 0;
528         if (ap->a_count) {
529                 /*
530                  * Calculate pbackwards and clean up any requested
531                  * pages that are too far back.
532                  */
533                 pbackwards = (poff + bbackwards) >> PAGE_SHIFT;
534                 if (ap->a_reqpage > pbackwards) {
535                         firstpage = ap->a_reqpage - pbackwards;
536                         for (i = 0; i < firstpage; i++)
537                                 vm_page_free(ap->a_m[i]);
538                 }
539
540                 /*
541                  * Calculate pforwards
542                  */
543                 pforwards = (bforwards - poff - PAGE_SIZE) >> PAGE_SHIFT;
544                 if (pforwards < 0)
545                         pforwards = 0;
546                 if (pforwards < (pcount - (ap->a_reqpage + 1))) {
547                         for(i = ap->a_reqpage + pforwards + 1; i < pcount; i++)
548                                 vm_page_free(ap->a_m[i]);
549                         pcount = ap->a_reqpage + pforwards + 1;
550                 }
551
552                 /*
553                  * Adjust pcount to be relative to firstpage.  All pages prior
554                  * to firstpage in the array have been cleaned up.
555                  */
556                 pcount -= firstpage;
557         }
558
559         /*
560          * calculate the size of the transfer
561          */
562         size = pcount * PAGE_SIZE;
563
564         if ((IDX_TO_OFF(ap->a_m[firstpage]->pindex) + size) > vp->v_filesize) {
565                 size = vp->v_filesize - IDX_TO_OFF(ap->a_m[firstpage]->pindex);
566         }
567
568         physoffset -= foff;
569         dp = VTOI(ap->a_vp)->i_devvp;
570         rtval = VOP_GETPAGES(dp, &ap->a_m[firstpage], size,
571                              (ap->a_reqpage - firstpage), physoffset);
572
573         return (rtval);
574 }
575