Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / sys / gnu / vfs / 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  */
42
43 #define BLKSIZE(a, b, c)        blksize(a, b, c)
44 #define FS                      struct ext2_sb_info
45 #define I_FS                    i_e2fs
46
47 /*
48  * Vnode op for reading.
49  *
50  * ext2_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
51  *           struct ucred *a_cred)
52  */
53 /* ARGSUSED */
54 static int
55 ext2_read(struct vop_read_args *ap)
56 {
57         struct vnode *vp;
58         struct inode *ip;
59         struct uio *uio;
60         FS *fs;
61         struct buf *bp;
62         daddr_t lbn, nextlbn;
63         off_t nextloffset;
64         off_t bytesinfile;
65         long size, xfersize, blkoffset;
66         int error, orig_resid;
67         int seqcount = ap->a_ioflag >> 16;
68
69         vp = ap->a_vp;
70         ip = VTOI(vp);
71         uio = ap->a_uio;
72
73 #ifdef DIAGNOSTIC
74         if (uio->uio_rw != UIO_READ)
75                 panic("ext2_read: mode");
76
77         if (vp->v_type == VLNK) {
78                 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
79                         panic("ext2_read: short symlink");
80         } else if (vp->v_type != VREG && vp->v_type != VDIR)
81                 panic("ext2_read: type %d", vp->v_type);
82 #endif
83         fs = ip->I_FS;
84 #if 0
85         if ((u_quad_t)uio->uio_offset > fs->fs_maxfilesize)
86                 return (EFBIG);
87 #endif
88
89         orig_resid = uio->uio_resid;
90         for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
91                 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
92                         break;
93                 lbn = lblkno(fs, uio->uio_offset);
94                 nextlbn = lbn + 1;
95                 nextloffset = lblktodoff(fs, nextlbn);
96                 size = BLKSIZE(fs, ip, lbn);
97                 blkoffset = blkoff(fs, uio->uio_offset);
98
99                 xfersize = fs->s_frag_size - blkoffset;
100                 if (uio->uio_resid < xfersize)
101                         xfersize = uio->uio_resid;
102                 if (bytesinfile < xfersize)
103                         xfersize = bytesinfile;
104
105                 if (nextloffset >= ip->i_size) {
106                         error = bread(vp, lblktodoff(fs, lbn), size, &bp);
107                 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
108                         error = cluster_read(vp, (off_t)ip->i_size,
109                                              lblktodoff(fs, lbn), size,
110                                              uio->uio_resid,
111                                              (ap->a_ioflag >> 16) * BKVASIZE,
112                                              &bp);
113                 } else if (seqcount > 1) {
114                         int nextsize = BLKSIZE(fs, ip, nextlbn);
115                         error = breadn(vp, lblktodoff(fs, lbn),
116                                         size, &nextloffset, &nextsize, 1, &bp);
117                 } else {
118                         error = bread(vp, lblktodoff(fs, lbn), size, &bp);
119                 }
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 = uiomove((char *)bp->b_data + blkoffset,
140                                 (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  * ext2_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
158  *            struct ucred *a_cred)
159  */
160 static int
161 ext2_write(struct vop_write_args *ap)
162 {
163         struct vnode *vp;
164         struct uio *uio;
165         struct inode *ip;
166         FS *fs;
167         struct buf *bp;
168         struct thread *td;
169         daddr_t lbn;
170         off_t osize;
171         int seqcount;
172         int blkoffset, error, flags, ioflag, resid, size, xfersize;
173
174         ioflag = ap->a_ioflag;
175         seqcount = ap->a_ioflag >> 16;
176         uio = ap->a_uio;
177         vp = ap->a_vp;
178         ip = VTOI(vp);
179
180 #ifdef DIAGNOSTIC
181         if (uio->uio_rw != UIO_WRITE)
182                 panic("ext2_write: mode");
183 #endif
184
185         switch (vp->v_type) {
186         case VREG:
187                 if (ioflag & IO_APPEND)
188                         uio->uio_offset = ip->i_size;
189                 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
190                         return (EPERM);
191                 /* FALLTHROUGH */
192         case VLNK:
193                 break;
194         case VDIR:
195                 if ((ioflag & IO_SYNC) == 0)
196                         panic("ext2_write: nonsync dir write");
197                 break;
198         default:
199                 panic("ext2_write: type");
200         }
201
202         fs = ip->I_FS;
203 #if 0
204         if (uio->uio_offset < 0 ||
205             (u_quad_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize)
206                 return (EFBIG);
207 #endif
208         /*
209          * Maybe this should be above the vnode op call, but so long as
210          * file servers have no limits, I don't think it matters.
211          */
212         td = uio->uio_td;
213         if (vp->v_type == VREG && td && td->td_proc &&
214             uio->uio_offset + uio->uio_resid >
215             td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
216                 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
217                 return (EFBIG);
218         }
219
220         resid = uio->uio_resid;
221         osize = ip->i_size;
222         flags = ioflag & IO_SYNC ? B_SYNC : 0;
223
224         for (error = 0; uio->uio_resid > 0;) {
225                 lbn = lblkno(fs, uio->uio_offset);
226                 blkoffset = blkoff(fs, uio->uio_offset);
227                 xfersize = fs->s_frag_size - blkoffset;
228                 if (uio->uio_resid < xfersize)
229                         xfersize = uio->uio_resid;
230
231                 if (uio->uio_offset + xfersize > ip->i_size)
232                         vnode_pager_setsize(vp, uio->uio_offset + xfersize);
233
234                 /*
235                  * Avoid a data-consistency race between write() and mmap()
236                  * by ensuring that newly allocated blocks are zerod.  The
237                  * race can occur even in the case where the write covers
238                  * the entire block.
239                  *
240                  * Just set B_CLRBUF unconditionally, even if the write
241                  * covers the whole block.  This also handles the UIO_NOCOPY
242                  * case.  See ffs_write() in ufs for a more sophisticated
243                  * version.
244                  */
245                 flags |= B_CLRBUF;
246                 error = ext2_balloc(ip, lbn, blkoffset + xfersize,
247                                     ap->a_cred, &bp, flags);
248                 if (error)
249                         break;
250
251                 if (uio->uio_offset + xfersize > ip->i_size) {
252                         ip->i_size = uio->uio_offset + xfersize;
253                 }
254
255                 size = BLKSIZE(fs, ip, lbn) - bp->b_resid;
256                 if (size < xfersize)
257                         xfersize = size;
258
259                 error = uiomove((char *)bp->b_data + blkoffset,
260                                 (int)xfersize, uio);
261                 if ((ioflag & IO_VMIO) &&
262                     LIST_FIRST(&bp->b_dep) == NULL) /* in ext2fs? */
263                         bp->b_flags |= B_RELBUF;
264
265                 if (ioflag & IO_SYNC) {
266                         bwrite(bp);
267                 } else if (xfersize + blkoffset == fs->s_frag_size) {
268                         if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
269                                 bp->b_flags |= B_CLUSTEROK;
270                                 cluster_write(bp, (off_t)ip->i_size, vp->v_mount->mnt_stat.f_iosize, seqcount);
271                         } else {
272                                 bawrite(bp);
273                         }
274                 } else {
275                         bp->b_flags |= B_CLUSTEROK;
276                         bdwrite(bp);
277                 }
278                 if (error || xfersize == 0)
279                         break;
280                 ip->i_flag |= IN_CHANGE | IN_UPDATE;
281         }
282         /*
283          * If we successfully wrote any data, and we are not the superuser
284          * we clear the setuid and setgid bits as a precaution against
285          * tampering.
286          */
287         if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
288                 ip->i_mode &= ~(ISUID | ISGID);
289         if (error) {
290                 if (ioflag & IO_UNIT) {
291                         EXT2_TRUNCATE(vp, osize, ioflag & IO_SYNC, ap->a_cred);
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 = EXT2_UPDATE(vp, 1);
297         return (error);
298 }