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