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