Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / vfs / gnu / 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 #define FS                      struct ext2_sb_info
45 #define I_FS                    i_e2fs
46 #define READ                    ext2_read
47 #define READ_S                  "ext2_read"
48 #define WRITE                   ext2_write
49 #define WRITE_S                 "ext2_write"
50
51 /*
52  * Vnode op for reading.
53  */
54 /* ARGSUSED */
55 static int
56 READ(ap)
57         struct vop_read_args /* {
58                 struct vnode *a_vp;
59                 struct uio *a_uio;
60                 int a_ioflag;
61                 struct ucred *a_cred;
62         } */ *ap;
63 {
64         register struct vnode *vp;
65         register struct inode *ip;
66         register struct uio *uio;
67         register FS *fs;
68         struct buf *bp;
69         daddr_t lbn, nextlbn;
70         off_t bytesinfile;
71         long size, xfersize, blkoffset;
72         int error, orig_resid;
73         int seqcount = ap->a_ioflag >> 16;
74         u_short mode;
75
76         vp = ap->a_vp;
77         ip = VTOI(vp);
78         mode = ip->i_mode;
79         uio = ap->a_uio;
80
81 #ifdef DIAGNOSTIC
82         if (uio->uio_rw != UIO_READ)
83                 panic("%s: mode", READ_S);
84
85         if (vp->v_type == VLNK) {
86                 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
87                         panic("%s: short symlink", READ_S);
88         } else if (vp->v_type != VREG && vp->v_type != VDIR)
89                 panic("%s: type %d", READ_S, vp->v_type);
90 #endif
91         fs = ip->I_FS;
92 #if 0
93         if ((u_quad_t)uio->uio_offset > fs->fs_maxfilesize)
94                 return (EFBIG);
95 #endif
96
97         orig_resid = uio->uio_resid;
98         for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
99                 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
100                         break;
101                 lbn = lblkno(fs, uio->uio_offset);
102                 nextlbn = lbn + 1;
103                 size = BLKSIZE(fs, ip, lbn);
104                 blkoffset = blkoff(fs, uio->uio_offset);
105
106                 xfersize = fs->s_frag_size - blkoffset;
107                 if (uio->uio_resid < xfersize)
108                         xfersize = uio->uio_resid;
109                 if (bytesinfile < xfersize)
110                         xfersize = bytesinfile;
111
112                 if (lblktosize(fs, nextlbn) >= ip->i_size)
113                         error = bread(vp, lbn, size, NOCRED, &bp);
114                 else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0)
115                         error = cluster_read(vp,
116                             ip->i_size, lbn, size, NOCRED,
117                                 uio->uio_resid, (ap->a_ioflag >> 16), &bp);
118                 else if (seqcount > 1) {
119                         int nextsize = BLKSIZE(fs, ip, nextlbn);
120                         error = breadn(vp, lbn,
121                             size, &nextlbn, &nextsize, 1, NOCRED, &bp);
122                 } else
123                         error = bread(vp, lbn, size, NOCRED, &bp);
124                 if (error) {
125                         brelse(bp);
126                         bp = NULL;
127                         break;
128                 }
129
130                 /*
131                  * We should only get non-zero b_resid when an I/O error
132                  * has occurred, which should cause us to break above.
133                  * However, if the short read did not cause an error,
134                  * then we want to ensure that we do not uiomove bad
135                  * or uninitialized data.
136                  */
137                 size -= bp->b_resid;
138                 if (size < xfersize) {
139                         if (size == 0)
140                                 break;
141                         xfersize = size;
142                 }
143                 error =
144                     uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
145                 if (error)
146                         break;
147
148                 bqrelse(bp);
149         }
150         if (bp != NULL)
151                 bqrelse(bp);
152         if (orig_resid > 0 && (error == 0 || uio->uio_resid != orig_resid) &&
153             (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
154                 ip->i_flag |= IN_ACCESS;
155         return (error);
156 }
157
158 /*
159  * Vnode op for writing.
160  */
161 static int
162 WRITE(ap)
163         struct vop_write_args /* {
164                 struct vnode *a_vp;
165                 struct uio *a_uio;
166                 int a_ioflag;
167                 struct ucred *a_cred;
168         } */ *ap;
169 {
170         register struct vnode *vp;
171         register struct uio *uio;
172         register struct inode *ip;
173         register FS *fs;
174         struct buf *bp;
175         struct proc *p;
176         daddr_t lbn;
177         off_t osize;
178         int seqcount;
179         int blkoffset, error, flags, ioflag, resid, size, xfersize;
180
181         ioflag = ap->a_ioflag;
182         seqcount = ap->a_ioflag >> 16;
183         uio = ap->a_uio;
184         vp = ap->a_vp;
185         ip = VTOI(vp);
186
187 #ifdef DIAGNOSTIC
188         if (uio->uio_rw != UIO_WRITE)
189                 panic("%s: mode", WRITE_S);
190 #endif
191
192         switch (vp->v_type) {
193         case VREG:
194                 if (ioflag & IO_APPEND)
195                         uio->uio_offset = ip->i_size;
196                 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
197                         return (EPERM);
198                 /* FALLTHROUGH */
199         case VLNK:
200                 break;
201         case VDIR:
202                 if ((ioflag & IO_SYNC) == 0)
203                         panic("%s: nonsync dir write", WRITE_S);
204                 break;
205         default:
206                 panic("%s: type", WRITE_S);
207         }
208
209         fs = ip->I_FS;
210 #if 0
211         if (uio->uio_offset < 0 ||
212             (u_quad_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize)
213                 return (EFBIG);
214 #endif
215         /*
216          * Maybe this should be above the vnode op call, but so long as
217          * file servers have no limits, I don't think it matters.
218          */
219         p = uio->uio_procp;
220         if (vp->v_type == VREG && p &&
221             uio->uio_offset + uio->uio_resid >
222             p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
223                 psignal(p, SIGXFSZ);
224                 return (EFBIG);
225         }
226
227         resid = uio->uio_resid;
228         osize = ip->i_size;
229         flags = ioflag & IO_SYNC ? B_SYNC : 0;
230
231         for (error = 0; uio->uio_resid > 0;) {
232                 lbn = lblkno(fs, uio->uio_offset);
233                 blkoffset = blkoff(fs, uio->uio_offset);
234                 xfersize = fs->s_frag_size - blkoffset;
235                 if (uio->uio_resid < xfersize)
236                         xfersize = uio->uio_resid;
237
238                 if (uio->uio_offset + xfersize > ip->i_size)
239                         vnode_pager_setsize(vp, uio->uio_offset + xfersize);
240
241                 /*  
242                  * Avoid a data-consistency race between write() and mmap()
243                  * by ensuring that newly allocated blocks are zerod.  The
244                  * race can occur even in the case where the write covers
245                  * the entire block.
246                  */
247                 flags |= B_CLRBUF;
248 #if 0
249                 if (fs->s_frag_size > xfersize)
250                         flags |= B_CLRBUF;
251                 else
252                         flags &= ~B_CLRBUF;
253 #endif
254
255                 error = ext2_balloc(ip,
256                     lbn, blkoffset + xfersize, ap->a_cred, &bp, flags);
257                 if (error)
258                         break;
259
260                 if (uio->uio_offset + xfersize > ip->i_size) {
261                         ip->i_size = uio->uio_offset + xfersize;
262                 }
263
264                 size = BLKSIZE(fs, ip, lbn) - bp->b_resid;
265                 if (size < xfersize)
266                         xfersize = size;
267
268                 error =
269                     uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
270                 if ((ioflag & IO_VMIO) &&
271                    (LIST_FIRST(&bp->b_dep) == NULL)) /* in ext2fs? */
272                         bp->b_flags |= B_RELBUF;
273
274                 if (ioflag & IO_SYNC) {
275                         (void)bwrite(bp);
276                 } else if (xfersize + blkoffset == fs->s_frag_size) {
277                         if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
278                                 bp->b_flags |= B_CLUSTEROK;
279                                 cluster_write(bp, ip->i_size, seqcount);
280                         } else {
281                                 bawrite(bp);
282                         }
283                 } else {
284                         bp->b_flags |= B_CLUSTEROK;
285                         bdwrite(bp);
286                 }
287                 if (error || xfersize == 0)
288                         break;
289                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
290         }
291         /*
292          * If we successfully wrote any data, and we are not the superuser
293          * we clear the setuid and setgid bits as a precaution against
294          * tampering.
295          */
296         if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
297                 ip->i_mode &= ~(ISUID | ISGID);
298         if (error) {
299                 if (ioflag & IO_UNIT) {
300                         (void)UFS_TRUNCATE(vp, osize,
301                             ioflag & IO_SYNC, ap->a_cred, uio->uio_procp);
302                         uio->uio_offset -= resid - uio->uio_resid;
303                         uio->uio_resid = resid;
304                 }
305         } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
306                 error = UFS_UPDATE(vp, 1);
307         return (error);
308 }