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