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