]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/gnu/fs/ext2fs/ext2_readwrite.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / gnu / fs / 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  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *      @(#)ufs_readwrite.c     8.7 (Berkeley) 1/21/94
36  * $FreeBSD$
37  */
38
39 #define BLKSIZE(a, b, c)        blksize(a, b, c)
40 #define FS                      struct ext2_sb_info
41 #define I_FS                    i_e2fs
42 #define READ                    ext2_read
43 #define READ_S                  "ext2_read"
44 #define WRITE                   ext2_write
45 #define WRITE_S                 "ext2_write"
46
47 /*
48  * Vnode op for reading.
49  */
50 /* ARGSUSED */
51 static int
52 READ(ap)
53         struct vop_read_args /* {
54                 struct vnode *a_vp;
55                 struct uio *a_uio;
56                 int a_ioflag;
57                 struct ucred *a_cred;
58         } */ *ap;
59 {
60         struct vnode *vp;
61         struct inode *ip;
62         struct uio *uio;
63         FS *fs;
64         struct buf *bp;
65         daddr_t lbn, nextlbn;
66         off_t bytesinfile;
67         long size, xfersize, blkoffset;
68         int error, orig_resid;
69         int seqcount = ap->a_ioflag >> IO_SEQSHIFT;
70         u_short mode;
71
72         vp = ap->a_vp;
73         ip = VTOI(vp);
74         mode = ip->i_mode;
75         uio = ap->a_uio;
76
77 #ifdef DIAGNOSTIC
78         if (uio->uio_rw != UIO_READ)
79                 panic("%s: mode", READ_S);
80
81         if (vp->v_type == VLNK) {
82                 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
83                         panic("%s: short symlink", READ_S);
84         } else if (vp->v_type != VREG && vp->v_type != VDIR)
85                 panic("%s: type %d", READ_S, vp->v_type);
86 #endif
87         fs = ip->I_FS;
88         if ((uoff_t)uio->uio_offset > fs->fs_maxfilesize)
89                 return (EFBIG);
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, NOCRED, &bp);
108                 else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0)
109                         error = cluster_read(vp,
110                             ip->i_size, lbn, size, NOCRED,
111                             uio->uio_resid, (ap->a_ioflag >> IO_SEQSHIFT), &bp);
112                 else if (seqcount > 1) {
113                         int nextsize = BLKSIZE(fs, ip, nextlbn);
114                         error = breadn(vp, lbn,
115                             size, &nextlbn, &nextsize, 1, NOCRED, &bp);
116                 } else
117                         error = bread(vp, lbn, size, NOCRED, &bp);
118                 if (error) {
119                         brelse(bp);
120                         bp = NULL;
121                         break;
122                 }
123
124                 /*
125                  * We should only get non-zero b_resid when an I/O error
126                  * has occurred, which should cause us to break above.
127                  * However, if the short read did not cause an error,
128                  * then we want to ensure that we do not uiomove bad
129                  * or uninitialized data.
130                  */
131                 size -= bp->b_resid;
132                 if (size < xfersize) {
133                         if (size == 0)
134                                 break;
135                         xfersize = size;
136                 }
137                 error =
138                     uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
139                 if (error)
140                         break;
141
142                 bqrelse(bp);
143         }
144         if (bp != NULL)
145                 bqrelse(bp);
146         if (orig_resid > 0 && (error == 0 || uio->uio_resid != orig_resid) &&
147             (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
148                 ip->i_flag |= IN_ACCESS;
149         return (error);
150 }
151
152 /*
153  * Vnode op for writing.
154  */
155 static int
156 WRITE(ap)
157         struct vop_write_args /* {
158                 struct vnode *a_vp;
159                 struct uio *a_uio;
160                 int a_ioflag;
161                 struct ucred *a_cred;
162         } */ *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 >> IO_SEQSHIFT;
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("%s: mode", WRITE_S);
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("%s: nonsync dir write", WRITE_S);
198                 break;
199         default:
200                 panic("%s: type", WRITE_S);
201         }
202
203         fs = ip->I_FS;
204         if (uio->uio_offset < 0 ||
205             (uoff_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize)
206                 return (EFBIG);
207         /*
208          * Maybe this should be above the vnode op call, but so long as
209          * file servers have no limits, I don't think it matters.
210          */
211         td = uio->uio_td;
212         if (vp->v_type == VREG && td != NULL) {
213                 PROC_LOCK(td->td_proc);
214                 if (uio->uio_offset + uio->uio_resid >
215                     lim_cur(td->td_proc, RLIMIT_FSIZE)) {
216                         psignal(td->td_proc, SIGXFSZ);
217                         PROC_UNLOCK(td->td_proc);
218                         return (EFBIG);
219                 }
220                 PROC_UNLOCK(td->td_proc);
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(vp, 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)ext2_truncate(vp, osize,
297                             ioflag & IO_SYNC, ap->a_cred, uio->uio_td);
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 = ext2_update(vp, 1);
303         return (error);
304 }