Correct BSD License clause numbering from 1-2-4 to 1-2-3.
[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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)ufs_readwrite.c     8.11 (Berkeley) 5/8/95
30  * $FreeBSD: src/sys/ufs/ufs/ufs_readwrite.c,v 1.65.2.14 2003/04/04 22:21:29 tegge Exp $
31  */
32
33 #define BLKSIZE(a, b, c)        blksize(a, b, c)
34 #define FS                      struct fs
35 #define I_FS                    i_fs
36
37 #include <vm/vm.h>
38 #include <vm/vm_object.h>
39 #include <vm/vm_pager.h>
40 #include <vm/vm_map.h>
41 #include <vm/vnode_pager.h>
42 #include <sys/event.h>
43 #include <sys/vmmeter.h>
44 #include <sys/sysctl.h>
45 #include <vm/vm_page2.h>
46
47 #include "opt_directio.h"
48
49 #define VN_KNOTE(vp, b) \
50         KNOTE((struct klist *)&vp->v_pollinfo.vpi_kqinfo.ki_note, (b))
51
52 #ifdef DIRECTIO
53 extern int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
54 #endif
55
56 SYSCTL_DECL(_vfs_ffs);
57
58 /*
59  * Vnode op for reading.
60  *
61  * ffs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
62  *          struct ucred *a_cred)
63  */
64 /* ARGSUSED */
65 int
66 ffs_read(struct vop_read_args *ap)
67 {
68         struct vnode *vp;
69         struct inode *ip;
70         struct uio *uio;
71         FS *fs;
72         struct buf *bp;
73         off_t bytesinfile;
74         int xfersize, blkoffset;
75         int error, orig_resid;
76         int seqcount;
77         int ioflag;
78
79         vp = ap->a_vp;
80         seqcount = ap->a_ioflag >> 16;
81         ip = VTOI(vp);
82         uio = ap->a_uio;
83         ioflag = ap->a_ioflag;
84 #ifdef DIRECTIO
85         if ((ioflag & IO_DIRECT) != 0) {
86                 int workdone;
87
88                 error = ffs_rawread(vp, uio, &workdone);
89                 if (error || workdone)
90                         return error;
91         }
92 #endif
93
94 #ifdef DIAGNOSTIC
95         if (uio->uio_rw != UIO_READ)
96                 panic("ffs_read: mode");
97
98         if (vp->v_type == VLNK) {
99                 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
100                         panic("ffs_read: short symlink");
101         } else if (vp->v_type != VREG && vp->v_type != VDIR)
102                 panic("ffs_read: type %d", vp->v_type);
103 #endif
104         fs = ip->I_FS;
105         if ((uint64_t)uio->uio_offset > fs->fs_maxfilesize)
106                 return (EFBIG);
107
108         orig_resid = uio->uio_resid;
109         if (orig_resid <= 0)
110                 return (0);
111
112         bytesinfile = ip->i_size - uio->uio_offset;
113         if (bytesinfile <= 0) {
114                 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
115                         ip->i_flag |= IN_ACCESS;
116                 return 0;
117         }
118
119         /*
120          * Ok so we couldn't do it all in one vm trick...
121          * so cycle around trying smaller bites..
122          */
123         for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
124                 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
125                         break;
126
127                 error = ffs_blkatoff_ra(vp, uio->uio_offset, NULL,
128                                         &bp, seqcount);
129                 if (error)
130                         break;
131
132                 /*
133                  * If IO_DIRECT then set B_DIRECT for the buffer.  This
134                  * will cause us to attempt to release the buffer later on
135                  * and will cause the buffer cache to attempt to free the
136                  * underlying pages.
137                  */
138                 if (ioflag & IO_DIRECT)
139                         bp->b_flags |= B_DIRECT;
140
141                 /*
142                  * We should only get non-zero b_resid when an I/O error
143                  * has occurred, which should cause us to break above.
144                  * However, if the short read did not cause an error,
145                  * then we want to ensure that we do not uiomove bad
146                  * or uninitialized data.
147                  *
148                  * XXX b_resid is only valid when an actual I/O has occured
149                  * and may be incorrect if the buffer is B_CACHE or if the
150                  * last op on the buffer was a failed write.  This KASSERT
151                  * is a precursor to removing it from the UFS code.
152                  */
153                 KASSERT(bp->b_resid == 0, ("bp->b_resid != 0"));
154
155                 /*
156                  * Calculate how much data we can copy
157                  */
158                 blkoffset = blkoff(fs, uio->uio_offset);
159                 xfersize = bp->b_bufsize - blkoffset;
160                 if (xfersize > uio->uio_resid)
161                         xfersize = uio->uio_resid;
162                 if (xfersize > bytesinfile)
163                         xfersize = bytesinfile;
164                 if (xfersize <= 0) {
165                         panic("ufs_readwrite: impossible xfersize: %d",
166                               xfersize);
167                 }
168
169                 /*
170                  * otherwise use the general form
171                  */
172                 error = uiomovebp(bp, bp->b_data + blkoffset, xfersize, uio);
173
174                 if (error)
175                         break;
176
177                 if ((ioflag & (IO_VMIO|IO_DIRECT)) && 
178                     (LIST_FIRST(&bp->b_dep) == NULL)) {
179                         /*
180                          * If there are no dependencies, and it's VMIO,
181                          * then we don't need the buf, mark it available
182                          * for freeing. The VM has the data.
183                          */
184                         bp->b_flags |= B_RELBUF;
185                         brelse(bp);
186                 } else {
187                         /*
188                          * Otherwise let whoever
189                          * made the request take care of
190                          * freeing it. We just queue
191                          * it onto another list.
192                          */
193                         bqrelse(bp);
194                 }
195         }
196
197         /* 
198          * This can only happen in the case of an error
199          * because the loop above resets bp to NULL on each iteration
200          * and on normal completion has not set a new value into it.
201          * so it must have come from a 'break' statement
202          */
203         if (bp != NULL) {
204                 if ((ioflag & (IO_VMIO|IO_DIRECT)) && 
205                     (LIST_FIRST(&bp->b_dep) == NULL)) {
206                         bp->b_flags |= B_RELBUF;
207                         brelse(bp);
208                 } else {
209                         bqrelse(bp);
210                 }
211         }
212
213         if ((error == 0 || uio->uio_resid != orig_resid) &&
214             (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
215                 ip->i_flag |= IN_ACCESS;
216         return (error);
217 }
218
219 /*
220  * Vnode op for writing.
221  *
222  * ffs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
223  *           struct ucred *a_cred)
224  */
225 int
226 ffs_write(struct vop_write_args *ap)
227 {
228         struct vnode *vp;
229         struct uio *uio;
230         struct inode *ip;
231         FS *fs;
232         struct buf *bp;
233         ufs_daddr_t lbn;
234         off_t osize;
235         off_t nsize;
236         int seqcount;
237         int blkoffset, error, extended, flags, ioflag, resid, size, xfersize;
238         struct thread *td;
239
240         extended = 0;
241         seqcount = ap->a_ioflag >> 16;
242         ioflag = ap->a_ioflag;
243         uio = ap->a_uio;
244         vp = ap->a_vp;
245         ip = VTOI(vp);
246
247 #ifdef DIAGNOSTIC
248         if (uio->uio_rw != UIO_WRITE)
249                 panic("ffs_write: mode");
250 #endif
251
252         switch (vp->v_type) {
253         case VREG:
254                 if (ioflag & IO_APPEND)
255                         uio->uio_offset = ip->i_size;
256                 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
257                         return (EPERM);
258                 /* FALLTHROUGH */
259         case VLNK:
260                 break;
261         case VDIR:
262                 panic("ffs_write: dir write");
263                 break;
264         default:
265                 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
266                         (int)uio->uio_offset,
267                         (int)uio->uio_resid
268                 );
269         }
270
271         fs = ip->I_FS;
272         if (uio->uio_offset < 0 ||
273             (uint64_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize) {
274                 return (EFBIG);
275         }
276         /*
277          * Maybe this should be above the vnode op call, but so long as
278          * file servers have no limits, I don't think it matters.
279          */
280         td = uio->uio_td;
281         if (vp->v_type == VREG && td && td->td_proc &&
282             uio->uio_offset + uio->uio_resid >
283             td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
284                 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
285                 return (EFBIG);
286         }
287
288         resid = uio->uio_resid;
289         osize = ip->i_size;
290
291         /*
292          * NOTE! These B_ flags are actually balloc-only flags, not buffer
293          * flags.  They are similar to the BA_ flags in fbsd.
294          */
295         if (seqcount > B_SEQMAX)
296                 flags = B_SEQMAX << B_SEQSHIFT;
297         else
298                 flags = seqcount << B_SEQSHIFT;
299         if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
300                 flags |= B_SYNC;
301
302         for (error = 0; uio->uio_resid > 0;) {
303                 lbn = lblkno(fs, uio->uio_offset);
304                 blkoffset = blkoff(fs, uio->uio_offset);
305                 xfersize = fs->fs_bsize - blkoffset;
306                 if (uio->uio_resid < xfersize)
307                         xfersize = uio->uio_resid;
308
309                 if (uio->uio_offset + xfersize > ip->i_size) {
310                         nsize = uio->uio_offset + xfersize;
311                         nvnode_pager_setsize(vp, nsize,
312                                 blkoffresize(fs, nsize), blkoff(fs, nsize));
313                 }
314
315 #if 0
316                 /*      
317                  * If doing a dummy write to flush the buffer for a
318                  * putpages we must perform a read-before-write to
319                  * fill in any missing spots and clear any invalid
320                  * areas.  Otherwise a multi-page buffer may not properly
321                  * flush.
322                  *
323                  * We must clear any invalid areas
324                  */
325                 if (uio->uio_segflg == UIO_NOCOPY) {
326                         error = ffs_blkatoff(vp, uio->uio_offset, NULL, &bp);
327                         if (error)
328                                 break;
329                         bqrelse(bp);
330                 }
331 #endif
332
333                 /*
334                  * We must clear invalid areas.
335                  */
336                 if (xfersize < fs->fs_bsize || uio->uio_segflg == UIO_NOCOPY)
337                         flags |= B_CLRBUF;
338                 else
339                         flags &= ~B_CLRBUF;
340 /* XXX is uio->uio_offset the right thing here? */
341                 error = VOP_BALLOC(vp, uio->uio_offset, xfersize,
342                                    ap->a_cred, flags, &bp);
343                 if (error != 0)
344                         break;
345                 /*
346                  * If the buffer is not valid and we did not clear garbage
347                  * out above, we have to do so here even though the write
348                  * covers the entire buffer in order to avoid a mmap()/write
349                  * race where another process may see the garbage prior to
350                  * the uiomove() for a write replacing it.
351                  */
352                 if ((bp->b_flags & B_CACHE) == 0 && (flags & B_CLRBUF) == 0)
353                         vfs_bio_clrbuf(bp);
354                 if (ioflag & IO_DIRECT)
355                         bp->b_flags |= B_DIRECT;
356                 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
357                         bp->b_flags |= B_NOCACHE;
358
359                 if (uio->uio_offset + xfersize > ip->i_size) {
360                         ip->i_size = uio->uio_offset + xfersize;
361                         extended = 1;
362                 }
363
364                 size = BLKSIZE(fs, ip, lbn) - bp->b_resid;
365                 if (size < xfersize)
366                         xfersize = size;
367
368                 error = uiomovebp(bp, bp->b_data + blkoffset, xfersize, uio);
369                 if ((ioflag & (IO_VMIO|IO_DIRECT)) && 
370                     (LIST_FIRST(&bp->b_dep) == NULL)) {
371                         bp->b_flags |= B_RELBUF;
372                 }
373
374                 /*
375                  * If IO_SYNC each buffer is written synchronously.  Otherwise
376                  * if we have a severe page deficiency write the buffer 
377                  * asynchronously.  Otherwise try to cluster, and if that
378                  * doesn't do it then either do an async write (if O_DIRECT),
379                  * or a delayed write (if not).
380                  */
381
382                 if (ioflag & IO_SYNC) {
383                         (void)bwrite(bp);
384                 } else if (vm_page_count_severe() || 
385                             buf_dirty_count_severe() ||
386                             (ioflag & IO_ASYNC)) {
387                         bp->b_flags |= B_CLUSTEROK;
388                         bawrite(bp);
389                 } else if (xfersize + blkoffset == fs->fs_bsize) {
390                         if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
391                                 bp->b_flags |= B_CLUSTEROK;
392                                 cluster_write(bp, (off_t)ip->i_size, fs->fs_bsize, seqcount);
393                         } else {
394                                 bawrite(bp);
395                         }
396                 } else if (ioflag & IO_DIRECT) {
397                         bp->b_flags |= B_CLUSTEROK;
398                         bawrite(bp);
399                 } else {
400                         bp->b_flags |= B_CLUSTEROK;
401                         bdwrite(bp);
402                 }
403                 if (error || xfersize == 0)
404                         break;
405                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
406         }
407         /*
408          * If we successfully wrote any data, and we are not the superuser
409          * we clear the setuid and setgid bits as a precaution against
410          * tampering.
411          */
412         if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
413                 ip->i_mode &= ~(ISUID | ISGID);
414         if (resid > uio->uio_resid)
415                 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
416         if (error) {
417                 if (ioflag & IO_UNIT) {
418                         (void)ffs_truncate(vp, osize, ioflag & IO_SYNC,
419                                            ap->a_cred);
420                         uio->uio_offset -= resid - uio->uio_resid;
421                         uio->uio_resid = resid;
422                 }
423         } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) {
424                 error = ffs_update(vp, 1);
425         }
426
427         return (error);
428 }
429