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