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