Merge branch 'vendor/NVI2'
[dragonfly.git] / sys / gnu / vfs / ext2fs / ext2_readwrite.c
1 /*
2  *  modified for Lites 1.1
3  *
4  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5  *  University of Utah, Department of Computer Science
6  */
7 /*
8  * Copyright (c) 1993
9  *      The Regents of the University of California.  All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      @(#)ufs_readwrite.c     8.7 (Berkeley) 1/21/94
40  * $FreeBSD: src/sys/gnu/ext2fs/ext2_readwrite.c,v 1.18.2.2 2000/12/22 18:44:33 dillon Exp $
41  */
42
43 #define BLKSIZE(a, b, c)        blksize(a, b, c)
44
45 /*
46  * Vnode op for reading.
47  *
48  * ext2_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
49  *           struct ucred *a_cred)
50  */
51 /* ARGSUSED */
52 static int
53 ext2_read(struct vop_read_args *ap)
54 {
55         struct vnode *vp;
56         struct inode *ip;
57         struct uio *uio;
58         struct ext2_sb_info *fs;
59         struct buf *bp;
60         daddr_t lbn, nextlbn;
61         off_t nextloffset;
62         off_t bytesinfile;
63         long size, xfersize, blkoffset;
64         int error, orig_resid;
65         int seqcount = ap->a_ioflag >> 16;
66
67         vp = ap->a_vp;
68         ip = VTOI(vp);
69         uio = ap->a_uio;
70
71 #ifdef DIAGNOSTIC
72         if (uio->uio_rw != UIO_READ)
73                 panic("ext2_read: mode");
74
75         if (vp->v_type == VLNK) {
76                 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
77                         panic("ext2_read: short symlink");
78         } else if (vp->v_type != VREG && vp->v_type != VDIR)
79                 panic("ext2_read: type %d", vp->v_type);
80 #endif
81         fs = ip->i_e2fs;
82 #if 0
83         if ((u_quad_t)uio->uio_offset > fs->fs_maxfilesize)
84                 return (EFBIG);
85 #endif
86
87         orig_resid = uio->uio_resid;
88         for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
89                 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
90                         break;
91                 lbn = lblkno(fs, uio->uio_offset);
92                 nextlbn = lbn + 1;
93                 nextloffset = lblktodoff(fs, nextlbn);
94                 size = BLKSIZE(fs, ip, lbn);
95                 blkoffset = blkoff(fs, uio->uio_offset);
96
97                 xfersize = fs->s_frag_size - blkoffset;
98                 if (uio->uio_resid < xfersize)
99                         xfersize = uio->uio_resid;
100                 if (bytesinfile < xfersize)
101                         xfersize = bytesinfile;
102
103                 if (nextloffset >= ip->i_size) {
104                         error = bread(vp, lblktodoff(fs, lbn), size, &bp);
105                 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
106                         error = cluster_read(vp, (off_t)ip->i_size,
107                                              lblktodoff(fs, lbn), size,
108                                              uio->uio_resid,
109                                              (ap->a_ioflag >> 16) * BKVASIZE,
110                                              &bp);
111                 } else if (seqcount > 1) {
112                         int nextsize = BLKSIZE(fs, ip, nextlbn);
113                         error = breadn(vp, lblktodoff(fs, lbn),
114                                         size, &nextloffset, &nextsize, 1, &bp);
115                 } else {
116                         error = bread(vp, lblktodoff(fs, lbn), size, &bp);
117                 }
118                 if (error) {
119                         brelse(bp);
120                         bp = NULL;
121                         break;
122                 }
123
124                 /*
125                  * We should only get non-zero b_resid when an I/O error
126                  * has occurred, which should cause us to break above.
127                  * However, if the short read did not cause an error,
128                  * then we want to ensure that we do not uiomove bad
129                  * or uninitialized data.
130                  */
131                 size -= bp->b_resid;
132                 if (size < xfersize) {
133                         if (size == 0)
134                                 break;
135                         xfersize = size;
136                 }
137                 error = uiomove((char *)bp->b_data + blkoffset,
138                                 (int)xfersize, uio);
139                 if (error)
140                         break;
141
142                 bqrelse(bp);
143         }
144         if (bp != NULL)
145                 bqrelse(bp);
146         if (orig_resid > 0 && (error == 0 || uio->uio_resid != orig_resid) &&
147             (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
148                 ip->i_flag |= IN_ACCESS;
149         return (error);
150 }
151
152 /*
153  * Vnode op for writing.
154  *
155  * ext2_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
156  *            struct ucred *a_cred)
157  */
158 static int
159 ext2_write(struct vop_write_args *ap)
160 {
161         struct vnode *vp;
162         struct uio *uio;
163         struct inode *ip;
164         struct ext2_sb_info *fs;
165         struct buf *bp;
166         struct thread *td;
167         daddr_t lbn;
168         off_t osize;
169         int seqcount;
170         int blkoffset, error, flags, ioflag, resid, size, xfersize;
171
172         ioflag = ap->a_ioflag;
173         seqcount = ap->a_ioflag >> 16;
174         uio = ap->a_uio;
175         vp = ap->a_vp;
176         ip = VTOI(vp);
177
178 #ifdef DIAGNOSTIC
179         if (uio->uio_rw != UIO_WRITE)
180                 panic("ext2_write: mode");
181 #endif
182
183         switch (vp->v_type) {
184         case VREG:
185                 if (ioflag & IO_APPEND)
186                         uio->uio_offset = ip->i_size;
187                 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
188                         return (EPERM);
189                 /* FALLTHROUGH */
190         case VLNK:
191                 break;
192         case VDIR:
193                 if ((ioflag & IO_SYNC) == 0)
194                         panic("ext2_write: nonsync dir write");
195                 break;
196         default:
197                 panic("ext2_write: type");
198         }
199
200         fs = ip->i_e2fs;
201 #if 0
202         if (uio->uio_offset < 0 ||
203             (u_quad_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize)
204                 return (EFBIG);
205 #endif
206         /*
207          * Maybe this should be above the vnode op call, but so long as
208          * file servers have no limits, I don't think it matters.
209          */
210         td = uio->uio_td;
211         if (vp->v_type == VREG && td && td->td_proc &&
212             uio->uio_offset + uio->uio_resid >
213             td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
214                 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
215                 return (EFBIG);
216         }
217
218         resid = uio->uio_resid;
219         osize = ip->i_size;
220         flags = ioflag & IO_SYNC ? B_SYNC : 0;
221
222         for (error = 0; uio->uio_resid > 0;) {
223                 lbn = lblkno(fs, uio->uio_offset);
224                 blkoffset = blkoff(fs, uio->uio_offset);
225                 xfersize = fs->s_frag_size - blkoffset;
226                 if (uio->uio_resid < xfersize)
227                         xfersize = uio->uio_resid;
228
229                 if (uio->uio_offset + xfersize > ip->i_size)
230                         vnode_pager_setsize(vp, uio->uio_offset + xfersize);
231
232                 /*
233                  * Avoid a data-consistency race between write() and mmap()
234                  * by ensuring that newly allocated blocks are zerod.  The
235                  * race can occur even in the case where the write covers
236                  * the entire block.
237                  *
238                  * Just set B_CLRBUF unconditionally, even if the write
239                  * covers the whole block.  This also handles the UIO_NOCOPY
240                  * case.  See ffs_write() in ufs for a more sophisticated
241                  * version.
242                  */
243                 flags |= B_CLRBUF;
244                 error = ext2_balloc(ip, lbn, blkoffset + xfersize,
245                                     ap->a_cred, &bp, flags);
246                 if (error)
247                         break;
248
249                 if (uio->uio_offset + xfersize > ip->i_size) {
250                         ip->i_size = uio->uio_offset + xfersize;
251                 }
252
253                 size = BLKSIZE(fs, ip, lbn) - bp->b_resid;
254                 if (size < xfersize)
255                         xfersize = size;
256
257                 error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
258                 if ((ioflag & IO_VMIO) &&
259                     LIST_FIRST(&bp->b_dep) == NULL) /* in ext2fs? */
260                         bp->b_flags |= B_RELBUF;
261
262                 if (ioflag & IO_SYNC) {
263                         bwrite(bp);
264                 } else if (xfersize + blkoffset == fs->s_frag_size) {
265                         if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
266                                 bp->b_flags |= B_CLUSTEROK;
267                                 cluster_write(bp, (off_t)ip->i_size, vp->v_mount->mnt_stat.f_iosize, seqcount);
268                         } else {
269                                 bawrite(bp);
270                         }
271                 } else {
272                         bp->b_flags |= B_CLUSTEROK;
273                         bdwrite(bp);
274                 }
275                 if (error || xfersize == 0)
276                         break;
277                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
278         }
279         /*
280          * If we successfully wrote any data, and we are not the superuser
281          * we clear the setuid and setgid bits as a precaution against
282          * tampering.
283          */
284         if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
285                 ip->i_mode &= ~(ISUID | ISGID);
286         if (error) {
287                 if (ioflag & IO_UNIT) {
288                         EXT2_TRUNCATE(vp, osize, ioflag & IO_SYNC, ap->a_cred);
289                         uio->uio_offset -= resid - uio->uio_resid;
290                         uio->uio_resid = resid;
291                 }
292         } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
293                 error = EXT2_UPDATE(vp, 1);
294         return (error);
295 }