kernel - Adjust UFS and HAMMER to use uiomovebp()
[dragonfly.git] / sys / vfs / ufs / ufs_readwrite.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)ufs_readwrite.c 8.11 (Berkeley) 5/8/95
34 * $FreeBSD: src/sys/ufs/ufs/ufs_readwrite.c,v 1.65.2.14 2003/04/04 22:21:29 tegge Exp $
35 */
36
37#define BLKSIZE(a, b, c) blksize(a, b, c)
38#define FS struct fs
39#define I_FS i_fs
5197887a 40
984263bc
MD
41#include <vm/vm.h>
42#include <vm/vm_object.h>
43#include <vm/vm_pager.h>
44#include <vm/vm_map.h>
45#include <vm/vnode_pager.h>
46#include <sys/event.h>
47#include <sys/vmmeter.h>
a55afca2 48#include <sys/sysctl.h>
12e4aaff
MD
49#include <vm/vm_page2.h>
50
984263bc
MD
51#include "opt_directio.h"
52
53#define VN_KNOTE(vp, b) \
5b22f1a7 54 KNOTE((struct klist *)&vp->v_pollinfo.vpi_kqinfo.ki_note, (b))
984263bc
MD
55
56#ifdef DIRECTIO
57extern int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
58#endif
59
a55afca2 60SYSCTL_DECL(_vfs_ffs);
a55afca2 61
984263bc
MD
62/*
63 * Vnode op for reading.
0973c589
CP
64 *
65 * ffs_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
66 * struct ucred *a_cred)
984263bc
MD
67 */
68/* ARGSUSED */
69int
0973c589 70ffs_read(struct vop_read_args *ap)
984263bc 71{
3ff2135f
RG
72 struct vnode *vp;
73 struct inode *ip;
74 struct uio *uio;
75 FS *fs;
984263bc 76 struct buf *bp;
984263bc 77 off_t bytesinfile;
9ba4b517 78 int xfersize, blkoffset;
984263bc 79 int error, orig_resid;
984263bc
MD
80 int seqcount;
81 int ioflag;
984263bc
MD
82
83 vp = ap->a_vp;
84 seqcount = ap->a_ioflag >> 16;
85 ip = VTOI(vp);
984263bc
MD
86 uio = ap->a_uio;
87 ioflag = ap->a_ioflag;
88#ifdef DIRECTIO
89 if ((ioflag & IO_DIRECT) != 0) {
90 int workdone;
91
92 error = ffs_rawread(vp, uio, &workdone);
93 if (error || workdone)
94 return error;
95 }
96#endif
97
98#ifdef DIAGNOSTIC
99 if (uio->uio_rw != UIO_READ)
5197887a 100 panic("ffs_read: mode");
984263bc
MD
101
102 if (vp->v_type == VLNK) {
103 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen)
5197887a 104 panic("ffs_read: short symlink");
984263bc 105 } else if (vp->v_type != VREG && vp->v_type != VDIR)
5197887a 106 panic("ffs_read: type %d", vp->v_type);
984263bc
MD
107#endif
108 fs = ip->I_FS;
f719c866 109 if ((uint64_t)uio->uio_offset > fs->fs_maxfilesize)
984263bc
MD
110 return (EFBIG);
111
112 orig_resid = uio->uio_resid;
113 if (orig_resid <= 0)
114 return (0);
115
984263bc
MD
116 bytesinfile = ip->i_size - uio->uio_offset;
117 if (bytesinfile <= 0) {
118 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
119 ip->i_flag |= IN_ACCESS;
120 return 0;
121 }
122
984263bc
MD
123 /*
124 * Ok so we couldn't do it all in one vm trick...
125 * so cycle around trying smaller bites..
126 */
127 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
128 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
129 break;
984263bc 130
9ba4b517
MD
131 error = ffs_blkatoff_ra(vp, uio->uio_offset, NULL,
132 &bp, seqcount);
133 if (error)
984263bc 134 break;
984263bc
MD
135
136 /*
137 * If IO_DIRECT then set B_DIRECT for the buffer. This
138 * will cause us to attempt to release the buffer later on
139 * and will cause the buffer cache to attempt to free the
140 * underlying pages.
141 */
142 if (ioflag & IO_DIRECT)
143 bp->b_flags |= B_DIRECT;
144
145 /*
146 * We should only get non-zero b_resid when an I/O error
147 * has occurred, which should cause us to break above.
148 * However, if the short read did not cause an error,
149 * then we want to ensure that we do not uiomove bad
150 * or uninitialized data.
151 *
152 * XXX b_resid is only valid when an actual I/O has occured
153 * and may be incorrect if the buffer is B_CACHE or if the
154 * last op on the buffer was a failed write. This KASSERT
155 * is a precursor to removing it from the UFS code.
156 */
157 KASSERT(bp->b_resid == 0, ("bp->b_resid != 0"));
9ba4b517
MD
158
159 /*
160 * Calculate how much data we can copy
161 */
162 blkoffset = blkoff(fs, uio->uio_offset);
163 xfersize = bp->b_bufsize - blkoffset;
164 if (xfersize > uio->uio_resid)
165 xfersize = uio->uio_resid;
166 if (xfersize > bytesinfile)
167 xfersize = bytesinfile;
168 if (xfersize <= 0) {
169 panic("ufs_readwrite: impossible xfersize: %d",
170 xfersize);
984263bc
MD
171 }
172
056b5695
MD
173 /*
174 * otherwise use the general form
175 */
44480e31 176 error = uiomovebp(bp, bp->b_data + blkoffset, xfersize, uio);
984263bc
MD
177
178 if (error)
179 break;
180
181 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
182 (LIST_FIRST(&bp->b_dep) == NULL)) {
183 /*
184 * If there are no dependencies, and it's VMIO,
185 * then we don't need the buf, mark it available
186 * for freeing. The VM has the data.
187 */
188 bp->b_flags |= B_RELBUF;
189 brelse(bp);
190 } else {
191 /*
192 * Otherwise let whoever
193 * made the request take care of
194 * freeing it. We just queue
195 * it onto another list.
196 */
197 bqrelse(bp);
198 }
199 }
200
201 /*
202 * This can only happen in the case of an error
203 * because the loop above resets bp to NULL on each iteration
204 * and on normal completion has not set a new value into it.
205 * so it must have come from a 'break' statement
206 */
207 if (bp != NULL) {
208 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
209 (LIST_FIRST(&bp->b_dep) == NULL)) {
210 bp->b_flags |= B_RELBUF;
211 brelse(bp);
212 } else {
213 bqrelse(bp);
214 }
215 }
216
984263bc
MD
217 if ((error == 0 || uio->uio_resid != orig_resid) &&
218 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0)
219 ip->i_flag |= IN_ACCESS;
220 return (error);
221}
222
223/*
224 * Vnode op for writing.
0973c589
CP
225 *
226 * ffs_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
227 * struct ucred *a_cred)
984263bc
MD
228 */
229int
0973c589 230ffs_write(struct vop_write_args *ap)
984263bc 231{
3ff2135f
RG
232 struct vnode *vp;
233 struct uio *uio;
234 struct inode *ip;
235 FS *fs;
984263bc 236 struct buf *bp;
984263bc
MD
237 ufs_daddr_t lbn;
238 off_t osize;
3bb7eedb 239 off_t nsize;
984263bc
MD
240 int seqcount;
241 int blkoffset, error, extended, flags, ioflag, resid, size, xfersize;
dadab5e9 242 struct thread *td;
984263bc
MD
243
244 extended = 0;
245 seqcount = ap->a_ioflag >> 16;
246 ioflag = ap->a_ioflag;
247 uio = ap->a_uio;
248 vp = ap->a_vp;
249 ip = VTOI(vp);
250
984263bc
MD
251#ifdef DIAGNOSTIC
252 if (uio->uio_rw != UIO_WRITE)
5197887a 253 panic("ffs_write: mode");
984263bc
MD
254#endif
255
256 switch (vp->v_type) {
257 case VREG:
258 if (ioflag & IO_APPEND)
259 uio->uio_offset = ip->i_size;
b33a2ac2 260 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
984263bc 261 return (EPERM);
984263bc
MD
262 /* FALLTHROUGH */
263 case VLNK:
264 break;
265 case VDIR:
5197887a 266 panic("ffs_write: dir write");
984263bc
MD
267 break;
268 default:
5197887a 269 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
984263bc
MD
270 (int)uio->uio_offset,
271 (int)uio->uio_resid
272 );
273 }
274
275 fs = ip->I_FS;
276 if (uio->uio_offset < 0 ||
f719c866 277 (uint64_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize) {
984263bc
MD
278 return (EFBIG);
279 }
280 /*
281 * Maybe this should be above the vnode op call, but so long as
282 * file servers have no limits, I don't think it matters.
283 */
dadab5e9 284 td = uio->uio_td;
8a8d5d85 285 if (vp->v_type == VREG && td && td->td_proc &&
984263bc 286 uio->uio_offset + uio->uio_resid >
dadab5e9 287 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
7278a846 288 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
984263bc
MD
289 return (EFBIG);
290 }
291
292 resid = uio->uio_resid;
293 osize = ip->i_size;
294
295 /*
296 * NOTE! These B_ flags are actually balloc-only flags, not buffer
f719c866 297 * flags. They are similar to the BA_ flags in fbsd.
984263bc
MD
298 */
299 if (seqcount > B_SEQMAX)
300 flags = B_SEQMAX << B_SEQSHIFT;
301 else
302 flags = seqcount << B_SEQSHIFT;
303 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp))
304 flags |= B_SYNC;
305
984263bc
MD
306 for (error = 0; uio->uio_resid > 0;) {
307 lbn = lblkno(fs, uio->uio_offset);
308 blkoffset = blkoff(fs, uio->uio_offset);
309 xfersize = fs->fs_bsize - blkoffset;
310 if (uio->uio_resid < xfersize)
311 xfersize = uio->uio_resid;
312
3bb7eedb
MD
313 if (uio->uio_offset + xfersize > ip->i_size) {
314 nsize = uio->uio_offset + xfersize;
315 nvnode_pager_setsize(vp, nsize,
316 blkoffresize(fs, nsize), blkoff(fs, nsize));
317 }
984263bc 318
1c9602b3 319#if 0
984263bc 320 /*
1c9602b3
MD
321 * If doing a dummy write to flush the buffer for a
322 * putpages we must perform a read-before-write to
323 * fill in any missing spots and clear any invalid
324 * areas. Otherwise a multi-page buffer may not properly
325 * flush.
326 *
327 * We must clear any invalid areas
328 */
329 if (uio->uio_segflg == UIO_NOCOPY) {
330 error = ffs_blkatoff(vp, uio->uio_offset, NULL, &bp);
331 if (error)
332 break;
333 bqrelse(bp);
334 }
335#endif
336
337 /*
338 * We must clear invalid areas.
984263bc 339 */
8aa7625b 340 if (xfersize < fs->fs_bsize || uio->uio_segflg == UIO_NOCOPY)
984263bc
MD
341 flags |= B_CLRBUF;
342 else
343 flags &= ~B_CLRBUF;
344/* XXX is uio->uio_offset the right thing here? */
345 error = VOP_BALLOC(vp, uio->uio_offset, xfersize,
1c9602b3 346 ap->a_cred, flags, &bp);
984263bc
MD
347 if (error != 0)
348 break;
349 /*
350 * If the buffer is not valid and we did not clear garbage
351 * out above, we have to do so here even though the write
352 * covers the entire buffer in order to avoid a mmap()/write
353 * race where another process may see the garbage prior to
354 * the uiomove() for a write replacing it.
355 */
8aa7625b 356 if ((bp->b_flags & B_CACHE) == 0 && (flags & B_CLRBUF) == 0)
984263bc
MD
357 vfs_bio_clrbuf(bp);
358 if (ioflag & IO_DIRECT)
359 bp->b_flags |= B_DIRECT;
10e49b8b
MD
360 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
361 bp->b_flags |= B_NOCACHE;
984263bc
MD
362
363 if (uio->uio_offset + xfersize > ip->i_size) {
364 ip->i_size = uio->uio_offset + xfersize;
365 extended = 1;
366 }
367
368 size = BLKSIZE(fs, ip, lbn) - bp->b_resid;
369 if (size < xfersize)
370 xfersize = size;
371
44480e31 372 error = uiomovebp(bp, bp->b_data + blkoffset, xfersize, uio);
984263bc
MD
373 if ((ioflag & (IO_VMIO|IO_DIRECT)) &&
374 (LIST_FIRST(&bp->b_dep) == NULL)) {
375 bp->b_flags |= B_RELBUF;
376 }
377
378 /*
379 * If IO_SYNC each buffer is written synchronously. Otherwise
380 * if we have a severe page deficiency write the buffer
381 * asynchronously. Otherwise try to cluster, and if that
382 * doesn't do it then either do an async write (if O_DIRECT),
383 * or a delayed write (if not).
384 */
385
386 if (ioflag & IO_SYNC) {
387 (void)bwrite(bp);
388 } else if (vm_page_count_severe() ||
389 buf_dirty_count_severe() ||
390 (ioflag & IO_ASYNC)) {
391 bp->b_flags |= B_CLUSTEROK;
392 bawrite(bp);
393 } else if (xfersize + blkoffset == fs->fs_bsize) {
394 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
395 bp->b_flags |= B_CLUSTEROK;
1c9602b3 396 cluster_write(bp, (off_t)ip->i_size, fs->fs_bsize, seqcount);
984263bc
MD
397 } else {
398 bawrite(bp);
399 }
400 } else if (ioflag & IO_DIRECT) {
401 bp->b_flags |= B_CLUSTEROK;
402 bawrite(bp);
403 } else {
404 bp->b_flags |= B_CLUSTEROK;
405 bdwrite(bp);
406 }
407 if (error || xfersize == 0)
408 break;
409 ip->i_flag |= IN_CHANGE | IN_UPDATE;
410 }
411 /*
412 * If we successfully wrote any data, and we are not the superuser
413 * we clear the setuid and setgid bits as a precaution against
414 * tampering.
415 */
416 if (resid > uio->uio_resid && ap->a_cred && ap->a_cred->cr_uid != 0)
417 ip->i_mode &= ~(ISUID | ISGID);
418 if (resid > uio->uio_resid)
419 VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
420 if (error) {
421 if (ioflag & IO_UNIT) {
ac690a1d 422 (void)ffs_truncate(vp, osize, ioflag & IO_SYNC,
87de5057 423 ap->a_cred);
984263bc
MD
424 uio->uio_offset -= resid - uio->uio_resid;
425 uio->uio_resid = resid;
426 }
b33a2ac2 427 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) {
ac690a1d 428 error = ffs_update(vp, 1);
b33a2ac2 429 }
984263bc
MD
430
431 return (error);
432}
433