1 /* $OpenBSD: ufs_readwrite.c,v 1.26 2004/07/13 21:04:29 millert Exp $ */
2 /* $NetBSD: ufs_readwrite.c,v 1.9 1996/05/11 18:27:57 mycroft Exp $ */
3
4 /*-
5 * Copyright (c) 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)ufs_readwrite.c 8.11 (Berkeley) 5/8/95
33 */
34
35 #ifdef LFS_READWRITE
36 #define BLKSIZE(a, b, c) blksize(a, b, c)
37 #define FS struct lfs
38 #define I_FS i_lfs
39 #define READ lfs_read
40 #define READ_S "lfs_read"
41 #define WRITE lfs_write
42 #define WRITE_S "lfs_write"
43 #define fs_bsize lfs_bsize
44 #define MAXFILESIZE fs->lfs_maxfilesize
45 #else
46 #define BLKSIZE(a, b, c) blksize(a, b, c)
47 #define FS struct fs
48 #define I_FS i_fs
49 #define READ ffs_read
50 #define READ_S "ffs_read"
51 #define WRITE ffs_write
52 #define WRITE_S "ffs_write"
53 #define MAXFILESIZE fs->fs_maxfilesize
54 #endif
55
56 #include <sys/event.h>
57
58 #define VN_KNOTE(vp, b) \
59 KNOTE((struct klist *)&vp->v_selectinfo.vsi_selinfo.si_note, (b))
60
61 /*
62 * Vnode op for reading.
63 */
64 /* ARGSUSED */
65 int
READ(v)66 READ(v)
67 void *v;
68 {
69 struct vop_read_args /* {
70 struct vnode *a_vp;
71 struct uio *a_uio;
72 int a_ioflag;
73 struct ucred *a_cred;
74 } */ *ap = v;
75 register struct vnode *vp;
76 register struct inode *ip;
77 register struct uio *uio;
78 register FS *fs;
79 struct buf *bp;
80 daddr_t lbn, nextlbn;
81 off_t bytesinfile;
82 long size, xfersize, blkoffset;
83 mode_t mode;
84 int error;
85
86 vp = ap->a_vp;
87 ip = VTOI(vp);
88 mode = ip->i_ffs_mode;
89 uio = ap->a_uio;
90
91 #ifdef DIAGNOSTIC
92 if (uio->uio_rw != UIO_READ)
93 panic("%s: mode", READ_S);
94
95 if (vp->v_type == VLNK) {
96 if ((int)ip->i_ffs_size < vp->v_mount->mnt_maxsymlinklen ||
97 (vp->v_mount->mnt_maxsymlinklen == 0 &&
98 ip->i_ffs_blocks == 0))
99 panic("%s: short symlink", READ_S);
100 } else if (vp->v_type != VREG && vp->v_type != VDIR)
101 panic("%s: type %d", READ_S, vp->v_type);
102 #endif
103 fs = ip->I_FS;
104 if ((u_int64_t)uio->uio_offset > MAXFILESIZE)
105 return (EFBIG);
106
107 if (uio->uio_resid == 0)
108 return (0);
109
110 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
111 if ((bytesinfile = ip->i_ffs_size - uio->uio_offset) <= 0)
112 break;
113 lbn = lblkno(fs, uio->uio_offset);
114 nextlbn = lbn + 1;
115 size = BLKSIZE(fs, ip, lbn);
116 blkoffset = blkoff(fs, uio->uio_offset);
117 xfersize = fs->fs_bsize - blkoffset;
118 if (uio->uio_resid < xfersize)
119 xfersize = uio->uio_resid;
120 if (bytesinfile < xfersize)
121 xfersize = bytesinfile;
122
123 #ifdef LFS_READWRITE
124 (void)lfs_check(vp, lbn);
125 error = cluster_read(vp, &ip->i_ci, ip->i_ffs_size, lbn,
126 size, NOCRED, &bp);
127 #else
128 if (lblktosize(fs, nextlbn) >= ip->i_ffs_size)
129 error = bread(vp, lbn, size, NOCRED, &bp);
130 else if (doclusterread)
131 error = cluster_read(vp, &ip->i_ci,
132 ip->i_ffs_size, lbn, size, NOCRED, &bp);
133 else if (lbn - 1 == ip->i_ci.ci_lastr) {
134 int nextsize = BLKSIZE(fs, ip, nextlbn);
135 error = breadn(vp, lbn,
136 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
137 } else
138 error = bread(vp, lbn, size, NOCRED, &bp);
139 #endif
140 if (error)
141 break;
142 ip->i_ci.ci_lastr = lbn;
143
144 /*
145 * We should only get non-zero b_resid when an I/O error
146 * has occurred, which should cause us to break above.
147 * However, if the short read did not cause an error,
148 * then we want to ensure that we do not uiomove bad
149 * or uninitialized data.
150 */
151 size -= bp->b_resid;
152 if (size < xfersize) {
153 if (size == 0)
154 break;
155 xfersize = size;
156 }
157 error = uiomove((char *)bp->b_data + blkoffset, (int)xfersize,
158 uio);
159 if (error)
160 break;
161 brelse(bp);
162 }
163 if (bp != NULL)
164 brelse(bp);
165 ip->i_flag |= IN_ACCESS;
166 return (error);
167 }
168
169 /*
170 * Vnode op for writing.
171 */
172 int
WRITE(v)173 WRITE(v)
174 void *v;
175 {
176 struct vop_write_args /* {
177 struct vnode *a_vp;
178 struct uio *a_uio;
179 int a_ioflag;
180 struct ucred *a_cred;
181 } */ *ap = v;
182 register struct vnode *vp;
183 register struct uio *uio;
184 register struct inode *ip;
185 register FS *fs;
186 struct buf *bp;
187 struct proc *p;
188 daddr_t lbn;
189 off_t osize;
190 int blkoffset, error, extended, flags, ioflag, resid, size, xfersize;
191
192 extended = 0;
193 ioflag = ap->a_ioflag;
194 uio = ap->a_uio;
195 vp = ap->a_vp;
196 ip = VTOI(vp);
197
198 #ifdef DIAGNOSTIC
199 if (uio->uio_rw != UIO_WRITE)
200 panic("%s: mode", WRITE_S);
201 #endif
202
203 /*
204 * If writing 0 bytes, succeed and do not change
205 * update time or file offset (standards compliance)
206 */
207 if (uio->uio_resid == 0)
208 return (0);
209
210 switch (vp->v_type) {
211 case VREG:
212 if (ioflag & IO_APPEND)
213 uio->uio_offset = ip->i_ffs_size;
214 if ((ip->i_ffs_flags & APPEND) && uio->uio_offset != ip->i_ffs_size)
215 return (EPERM);
216 /* FALLTHROUGH */
217 case VLNK:
218 break;
219 case VDIR:
220 if ((ioflag & IO_SYNC) == 0)
221 panic("%s: nonsync dir write", WRITE_S);
222 break;
223 default:
224 panic("%s: type", WRITE_S);
225 }
226
227 fs = ip->I_FS;
228 if (uio->uio_offset < 0 ||
229 (u_int64_t)uio->uio_offset + uio->uio_resid > MAXFILESIZE)
230 return (EFBIG);
231 /*
232 * Maybe this should be above the vnode op call, but so long as
233 * file servers have no limits, I don't think it matters.
234 */
235 p = uio->uio_procp;
236 if (vp->v_type == VREG && p && !(ioflag & IO_NOLIMIT) &&
237 uio->uio_offset + uio->uio_resid >
238 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
239 psignal(p, SIGXFSZ);
240 return (EFBIG);
241 }
242
243 resid = uio->uio_resid;
244 osize = ip->i_ffs_size;
245 flags = ioflag & IO_SYNC ? B_SYNC : 0;
246
247 for (error = 0; uio->uio_resid > 0;) {
248 lbn = lblkno(fs, uio->uio_offset);
249 blkoffset = blkoff(fs, uio->uio_offset);
250 xfersize = fs->fs_bsize - blkoffset;
251 if (uio->uio_resid < xfersize)
252 xfersize = uio->uio_resid;
253 if (fs->fs_bsize > xfersize)
254 flags |= B_CLRBUF;
255 else
256 flags &= ~B_CLRBUF;
257
258 if ((error = UFS_BUF_ALLOC(ip, uio->uio_offset, xfersize,
259 ap->a_cred, flags, &bp)) != 0)
260 break;
261 if (uio->uio_offset + xfersize > ip->i_ffs_size) {
262 ip->i_ffs_size = uio->uio_offset + xfersize;
263 uvm_vnp_setsize(vp, ip->i_ffs_size);
264 extended = 1;
265 }
266 (void)uvm_vnp_uncache(vp);
267
268 size = BLKSIZE(fs, ip, lbn) - bp->b_resid;
269 if (size < xfersize)
270 xfersize = size;
271
272 error =
273 uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
274
275 if (error != 0)
276 bzero((char *)bp->b_data + blkoffset, xfersize);
277
278 #ifdef LFS_READWRITE
279 (void)VOP_BWRITE(bp);
280 #else
281 if (ioflag & IO_SYNC)
282 (void)bwrite(bp);
283 else if (xfersize + blkoffset == fs->fs_bsize) {
284 if (doclusterwrite)
285 cluster_write(bp, &ip->i_ci, ip->i_ffs_size);
286 else
287 bawrite(bp);
288 } else
289 bdwrite(bp);
290 #endif
291 if (error || xfersize == 0)
292 break;
293 ip->i_flag |= IN_CHANGE | IN_UPDATE;
294 }
295 /*
296 * If we successfully wrote any data, and we are not the superuser
297 * we clear the setuid and setgid bits as a precaution against
298 * tampering.
299 */
300 if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
301 ip->i_ffs_mode &= ~(ISUID | ISGID);
302 if (resid > uio->uio_resid)
303 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
304 if (error) {
305 if (ioflag & IO_UNIT) {
306 (void)UFS_TRUNCATE(ip, osize,
307 ioflag & IO_SYNC, ap->a_cred);
308 uio->uio_offset -= resid - uio->uio_resid;
309 uio->uio_resid = resid;
310 }
311 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) {
312 error = UFS_UPDATE(ip, MNT_WAIT);
313 }
314 return (error);
315 }
316