ulfs_readwrite.c revision 1.26 1 1.26 ad /* $NetBSD: ulfs_readwrite.c,v 1.26 2020/02/23 15:46:42 ad Exp $ */
2 1.22 dholland /* from NetBSD: ufs_readwrite.c,v 1.120 2015/04/12 22:48:38 riastradh Exp */
3 1.1 dholland
4 1.1 dholland /*-
5 1.1 dholland * Copyright (c) 1993
6 1.1 dholland * The Regents of the University of California. All rights reserved.
7 1.1 dholland *
8 1.1 dholland * Redistribution and use in source and binary forms, with or without
9 1.1 dholland * modification, are permitted provided that the following conditions
10 1.1 dholland * are met:
11 1.1 dholland * 1. Redistributions of source code must retain the above copyright
12 1.1 dholland * notice, this list of conditions and the following disclaimer.
13 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 dholland * notice, this list of conditions and the following disclaimer in the
15 1.1 dholland * documentation and/or other materials provided with the distribution.
16 1.1 dholland * 3. Neither the name of the University nor the names of its contributors
17 1.1 dholland * may be used to endorse or promote products derived from this software
18 1.1 dholland * without specific prior written permission.
19 1.1 dholland *
20 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 1.1 dholland * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 dholland * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 dholland * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 1.1 dholland * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 dholland * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 dholland * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 dholland * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 dholland * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 dholland * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 dholland * SUCH DAMAGE.
31 1.1 dholland *
32 1.1 dholland * @(#)ufs_readwrite.c 8.11 (Berkeley) 5/8/95
33 1.1 dholland */
34 1.1 dholland
35 1.1 dholland #include <sys/cdefs.h>
36 1.26 ad __KERNEL_RCSID(1, "$NetBSD: ulfs_readwrite.c,v 1.26 2020/02/23 15:46:42 ad Exp $");
37 1.1 dholland
38 1.1 dholland #define FS struct lfs
39 1.1 dholland #define I_FS i_lfs
40 1.1 dholland #define READ lfs_read
41 1.1 dholland #define READ_S "lfs_read"
42 1.1 dholland #define WRITE lfs_write
43 1.1 dholland #define WRITE_S "lfs_write"
44 1.8 riastrad #define BUFRD lfs_bufrd
45 1.8 riastrad #define BUFWR lfs_bufwr
46 1.18 dholland #define fs_sb_getbsize(fs) lfs_sb_getbsize(fs)
47 1.1 dholland #define fs_bmask lfs_bmask
48 1.1 dholland
49 1.11 riastrad static int ulfs_post_read_update(struct vnode *, int, int);
50 1.11 riastrad static int ulfs_post_write_update(struct vnode *, struct uio *, int,
51 1.11 riastrad kauth_cred_t, off_t, int, int, int);
52 1.11 riastrad
53 1.1 dholland /*
54 1.1 dholland * Vnode op for reading.
55 1.1 dholland */
56 1.1 dholland /* ARGSUSED */
57 1.1 dholland int
58 1.1 dholland READ(void *v)
59 1.1 dholland {
60 1.1 dholland struct vop_read_args /* {
61 1.1 dholland struct vnode *a_vp;
62 1.1 dholland struct uio *a_uio;
63 1.1 dholland int a_ioflag;
64 1.1 dholland kauth_cred_t a_cred;
65 1.1 dholland } */ *ap = v;
66 1.1 dholland struct vnode *vp;
67 1.1 dholland struct inode *ip;
68 1.1 dholland struct uio *uio;
69 1.1 dholland FS *fs;
70 1.1 dholland vsize_t bytelen;
71 1.8 riastrad int error, ioflag, advice;
72 1.1 dholland
73 1.1 dholland vp = ap->a_vp;
74 1.1 dholland ip = VTOI(vp);
75 1.6 dholland fs = ip->I_FS;
76 1.1 dholland uio = ap->a_uio;
77 1.1 dholland ioflag = ap->a_ioflag;
78 1.1 dholland error = 0;
79 1.1 dholland
80 1.10 riastrad KASSERT(uio->uio_rw == UIO_READ);
81 1.10 riastrad KASSERT(vp->v_type == VREG || vp->v_type == VDIR);
82 1.1 dholland
83 1.8 riastrad /* XXX Eliminate me by refusing directory reads from userland. */
84 1.8 riastrad if (vp->v_type == VDIR)
85 1.8 riastrad return BUFRD(vp, uio, ioflag, ap->a_cred);
86 1.8 riastrad /* XXX Eliminate me by using ufs_bufio in lfs. */
87 1.8 riastrad if (vp->v_type == VREG && ip->i_number == LFS_IFILE_INUM)
88 1.8 riastrad return BUFRD(vp, uio, ioflag, ap->a_cred);
89 1.6 dholland if ((u_int64_t)uio->uio_offset > fs->um_maxfilesize)
90 1.1 dholland return (EFBIG);
91 1.1 dholland if (uio->uio_resid == 0)
92 1.1 dholland return (0);
93 1.1 dholland
94 1.1 dholland
95 1.1 dholland if (uio->uio_offset >= ip->i_size)
96 1.1 dholland goto out;
97 1.1 dholland
98 1.8 riastrad KASSERT(vp->v_type == VREG);
99 1.8 riastrad advice = IO_ADV_DECODE(ap->a_ioflag);
100 1.8 riastrad while (uio->uio_resid > 0) {
101 1.8 riastrad if (ioflag & IO_DIRECT) {
102 1.8 riastrad genfs_directio(vp, uio, ioflag);
103 1.8 riastrad }
104 1.8 riastrad bytelen = MIN(ip->i_size - uio->uio_offset, uio->uio_resid);
105 1.8 riastrad if (bytelen == 0)
106 1.8 riastrad break;
107 1.8 riastrad error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
108 1.8 riastrad UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
109 1.8 riastrad if (error)
110 1.8 riastrad break;
111 1.8 riastrad }
112 1.1 dholland
113 1.8 riastrad out:
114 1.11 riastrad error = ulfs_post_read_update(vp, ap->a_ioflag, error);
115 1.8 riastrad return (error);
116 1.8 riastrad }
117 1.8 riastrad
118 1.8 riastrad /*
119 1.8 riastrad * UFS op for reading via the buffer cache
120 1.8 riastrad */
121 1.8 riastrad int
122 1.8 riastrad BUFRD(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
123 1.8 riastrad {
124 1.8 riastrad struct inode *ip;
125 1.8 riastrad FS *fs;
126 1.8 riastrad struct buf *bp;
127 1.8 riastrad daddr_t lbn, nextlbn;
128 1.8 riastrad off_t bytesinfile;
129 1.8 riastrad long size, xfersize, blkoffset;
130 1.8 riastrad int error;
131 1.8 riastrad
132 1.8 riastrad KASSERT(VOP_ISLOCKED(vp));
133 1.8 riastrad KASSERT(vp->v_type == VDIR || vp->v_type == VLNK ||
134 1.8 riastrad vp->v_type == VREG);
135 1.8 riastrad KASSERT(uio->uio_rw == UIO_READ);
136 1.8 riastrad
137 1.8 riastrad ip = VTOI(vp);
138 1.8 riastrad fs = ip->I_FS;
139 1.8 riastrad error = 0;
140 1.8 riastrad
141 1.20 mlelstv KASSERT(vp->v_type != VLNK || ip->i_size >= fs->um_maxsymlinklen);
142 1.8 riastrad KASSERT(vp->v_type != VLNK || fs->um_maxsymlinklen != 0 ||
143 1.8 riastrad DIP(ip, blocks) == 0);
144 1.8 riastrad KASSERT(vp->v_type != VREG || vp == fs->lfs_ivnode);
145 1.8 riastrad KASSERT(vp->v_type != VREG || ip->i_number == LFS_IFILE_INUM);
146 1.8 riastrad
147 1.8 riastrad if (uio->uio_offset > fs->um_maxfilesize)
148 1.8 riastrad return EFBIG;
149 1.8 riastrad if (uio->uio_resid == 0)
150 1.8 riastrad return 0;
151 1.8 riastrad
152 1.8 riastrad
153 1.8 riastrad if (uio->uio_offset >= ip->i_size)
154 1.1 dholland goto out;
155 1.1 dholland
156 1.1 dholland for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
157 1.1 dholland bytesinfile = ip->i_size - uio->uio_offset;
158 1.1 dholland if (bytesinfile <= 0)
159 1.1 dholland break;
160 1.4 christos lbn = lfs_lblkno(fs, uio->uio_offset);
161 1.1 dholland nextlbn = lbn + 1;
162 1.4 christos size = lfs_blksize(fs, ip, lbn);
163 1.4 christos blkoffset = lfs_blkoff(fs, uio->uio_offset);
164 1.18 dholland xfersize = MIN(MIN(fs_sb_getbsize(fs) - blkoffset, uio->uio_resid),
165 1.1 dholland bytesinfile);
166 1.1 dholland
167 1.4 christos if (lfs_lblktosize(fs, nextlbn) >= ip->i_size)
168 1.15 maxv error = bread(vp, lbn, size, 0, &bp);
169 1.1 dholland else {
170 1.4 christos int nextsize = lfs_blksize(fs, ip, nextlbn);
171 1.1 dholland error = breadn(vp, lbn,
172 1.13 maxv size, &nextlbn, &nextsize, 1, 0, &bp);
173 1.1 dholland }
174 1.1 dholland if (error)
175 1.1 dholland break;
176 1.1 dholland
177 1.1 dholland /*
178 1.1 dholland * We should only get non-zero b_resid when an I/O error
179 1.1 dholland * has occurred, which should cause us to break above.
180 1.1 dholland * However, if the short read did not cause an error,
181 1.1 dholland * then we want to ensure that we do not uiomove bad
182 1.1 dholland * or uninitialized data.
183 1.1 dholland */
184 1.1 dholland size -= bp->b_resid;
185 1.1 dholland if (size < xfersize) {
186 1.1 dholland if (size == 0)
187 1.1 dholland break;
188 1.1 dholland xfersize = size;
189 1.1 dholland }
190 1.1 dholland error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
191 1.1 dholland if (error)
192 1.1 dholland break;
193 1.1 dholland brelse(bp, 0);
194 1.1 dholland }
195 1.1 dholland if (bp != NULL)
196 1.1 dholland brelse(bp, 0);
197 1.1 dholland
198 1.1 dholland out:
199 1.11 riastrad error = ulfs_post_read_update(vp, ioflag, error);
200 1.11 riastrad return (error);
201 1.11 riastrad }
202 1.11 riastrad
203 1.11 riastrad static int
204 1.14 riastrad ulfs_post_read_update(struct vnode *vp, int ioflag, int oerror)
205 1.11 riastrad {
206 1.11 riastrad struct inode *ip = VTOI(vp);
207 1.14 riastrad int error = oerror;
208 1.11 riastrad
209 1.1 dholland if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
210 1.24 maya ip->i_state |= IN_ACCESS;
211 1.8 riastrad if ((ioflag & IO_SYNC) == IO_SYNC) {
212 1.5 dholland error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
213 1.1 dholland }
214 1.1 dholland }
215 1.1 dholland
216 1.14 riastrad /* Read error overrides any inode update error. */
217 1.14 riastrad if (oerror)
218 1.14 riastrad error = oerror;
219 1.11 riastrad return error;
220 1.1 dholland }
221 1.1 dholland
222 1.1 dholland /*
223 1.1 dholland * Vnode op for writing.
224 1.1 dholland */
225 1.1 dholland int
226 1.1 dholland WRITE(void *v)
227 1.1 dholland {
228 1.1 dholland struct vop_write_args /* {
229 1.1 dholland struct vnode *a_vp;
230 1.1 dholland struct uio *a_uio;
231 1.1 dholland int a_ioflag;
232 1.1 dholland kauth_cred_t a_cred;
233 1.1 dholland } */ *ap = v;
234 1.1 dholland struct vnode *vp;
235 1.1 dholland struct uio *uio;
236 1.1 dholland struct inode *ip;
237 1.1 dholland FS *fs;
238 1.1 dholland kauth_cred_t cred;
239 1.1 dholland off_t osize, origoff, oldoff, preallocoff, endallocoff, nsize;
240 1.8 riastrad int blkoffset, error, flags, ioflag, resid;
241 1.1 dholland int aflag;
242 1.1 dholland int extended=0;
243 1.1 dholland vsize_t bytelen;
244 1.1 dholland bool async;
245 1.1 dholland
246 1.1 dholland cred = ap->a_cred;
247 1.1 dholland ioflag = ap->a_ioflag;
248 1.1 dholland uio = ap->a_uio;
249 1.1 dholland vp = ap->a_vp;
250 1.1 dholland ip = VTOI(vp);
251 1.1 dholland
252 1.1 dholland KASSERT(vp->v_size == ip->i_size);
253 1.10 riastrad KASSERT(uio->uio_rw == UIO_WRITE);
254 1.10 riastrad KASSERT(vp->v_type == VREG);
255 1.1 dholland
256 1.10 riastrad if (ioflag & IO_APPEND)
257 1.10 riastrad uio->uio_offset = ip->i_size;
258 1.10 riastrad if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
259 1.10 riastrad return (EPERM);
260 1.1 dholland
261 1.1 dholland fs = ip->I_FS;
262 1.1 dholland if (uio->uio_offset < 0 ||
263 1.6 dholland (u_int64_t)uio->uio_offset + uio->uio_resid > fs->um_maxfilesize)
264 1.1 dholland return (EFBIG);
265 1.1 dholland /* Disallow writes to the Ifile, even if noschg flag is removed */
266 1.1 dholland /* XXX can this go away when the Ifile is no longer in the namespace? */
267 1.1 dholland if (vp == fs->lfs_ivnode)
268 1.1 dholland return (EPERM);
269 1.1 dholland if (uio->uio_resid == 0)
270 1.1 dholland return (0);
271 1.1 dholland
272 1.1 dholland flags = ioflag & IO_SYNC ? B_SYNC : 0;
273 1.1 dholland async = vp->v_mount->mnt_flag & MNT_ASYNC;
274 1.1 dholland origoff = uio->uio_offset;
275 1.1 dholland resid = uio->uio_resid;
276 1.1 dholland osize = ip->i_size;
277 1.1 dholland error = 0;
278 1.1 dholland
279 1.8 riastrad KASSERT(vp->v_type == VREG);
280 1.1 dholland
281 1.1 dholland async = true;
282 1.4 christos lfs_availwait(fs, lfs_btofsb(fs, uio->uio_resid));
283 1.1 dholland lfs_check(vp, LFS_UNUSED_LBN, 0);
284 1.1 dholland
285 1.4 christos preallocoff = round_page(lfs_blkroundup(fs, MAX(osize, uio->uio_offset)));
286 1.1 dholland aflag = ioflag & IO_SYNC ? B_SYNC : 0;
287 1.1 dholland nsize = MAX(osize, uio->uio_offset + uio->uio_resid);
288 1.4 christos endallocoff = nsize - lfs_blkoff(fs, nsize);
289 1.1 dholland
290 1.1 dholland /*
291 1.1 dholland * if we're increasing the file size, deal with expanding
292 1.1 dholland * the fragment if there is one.
293 1.1 dholland */
294 1.1 dholland
295 1.4 christos if (nsize > osize && lfs_lblkno(fs, osize) < ULFS_NDADDR &&
296 1.4 christos lfs_lblkno(fs, osize) != lfs_lblkno(fs, nsize) &&
297 1.4 christos lfs_blkroundup(fs, osize) != osize) {
298 1.1 dholland off_t eob;
299 1.1 dholland
300 1.4 christos eob = lfs_blkroundup(fs, osize);
301 1.1 dholland uvm_vnp_setwritesize(vp, eob);
302 1.2 dholland error = ulfs_balloc_range(vp, osize, eob - osize, cred, aflag);
303 1.1 dholland if (error)
304 1.1 dholland goto out;
305 1.1 dholland if (flags & B_SYNC) {
306 1.26 ad rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
307 1.19 dholland VOP_PUTPAGES(vp, trunc_page(osize & lfs_sb_getbmask(fs)),
308 1.1 dholland round_page(eob),
309 1.17 riastrad PGO_CLEANIT | PGO_SYNCIO);
310 1.1 dholland }
311 1.1 dholland }
312 1.1 dholland
313 1.1 dholland while (uio->uio_resid > 0) {
314 1.1 dholland int ubc_flags = UBC_WRITE;
315 1.1 dholland bool overwrite; /* if we're overwrite a whole block */
316 1.1 dholland off_t newoff;
317 1.1 dholland
318 1.1 dholland if (ioflag & IO_DIRECT) {
319 1.17 riastrad genfs_directio(vp, uio, ioflag);
320 1.1 dholland }
321 1.1 dholland
322 1.1 dholland oldoff = uio->uio_offset;
323 1.4 christos blkoffset = lfs_blkoff(fs, uio->uio_offset);
324 1.18 dholland bytelen = MIN(fs_sb_getbsize(fs) - blkoffset, uio->uio_resid);
325 1.1 dholland if (bytelen == 0) {
326 1.1 dholland break;
327 1.1 dholland }
328 1.1 dholland
329 1.1 dholland /*
330 1.1 dholland * if we're filling in a hole, allocate the blocks now and
331 1.1 dholland * initialize the pages first. if we're extending the file,
332 1.1 dholland * we can safely allocate blocks without initializing pages
333 1.1 dholland * since the new blocks will be inaccessible until the write
334 1.1 dholland * is complete.
335 1.1 dholland */
336 1.1 dholland overwrite = uio->uio_offset >= preallocoff &&
337 1.1 dholland uio->uio_offset < endallocoff;
338 1.1 dholland if (!overwrite && (vp->v_vflag & VV_MAPPED) == 0 &&
339 1.4 christos lfs_blkoff(fs, uio->uio_offset) == 0 &&
340 1.1 dholland (uio->uio_offset & PAGE_MASK) == 0) {
341 1.1 dholland vsize_t len;
342 1.1 dholland
343 1.1 dholland len = trunc_page(bytelen);
344 1.4 christos len -= lfs_blkoff(fs, len);
345 1.1 dholland if (len > 0) {
346 1.1 dholland overwrite = true;
347 1.1 dholland bytelen = len;
348 1.1 dholland }
349 1.1 dholland }
350 1.1 dholland
351 1.1 dholland newoff = oldoff + bytelen;
352 1.1 dholland if (vp->v_size < newoff) {
353 1.1 dholland uvm_vnp_setwritesize(vp, newoff);
354 1.1 dholland }
355 1.1 dholland
356 1.1 dholland if (!overwrite) {
357 1.2 dholland error = ulfs_balloc_range(vp, uio->uio_offset, bytelen,
358 1.1 dholland cred, aflag);
359 1.1 dholland if (error)
360 1.1 dholland break;
361 1.1 dholland } else {
362 1.1 dholland genfs_node_wrlock(vp);
363 1.1 dholland error = GOP_ALLOC(vp, uio->uio_offset, bytelen,
364 1.1 dholland aflag, cred);
365 1.1 dholland genfs_node_unlock(vp);
366 1.1 dholland if (error)
367 1.1 dholland break;
368 1.1 dholland ubc_flags |= UBC_FAULTBUSY;
369 1.1 dholland }
370 1.1 dholland
371 1.1 dholland /*
372 1.1 dholland * copy the data.
373 1.1 dholland */
374 1.1 dholland
375 1.1 dholland error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
376 1.1 dholland IO_ADV_DECODE(ioflag), ubc_flags | UBC_UNMAP_FLAG(vp));
377 1.1 dholland
378 1.1 dholland /*
379 1.1 dholland * update UVM's notion of the size now that we've
380 1.1 dholland * copied the data into the vnode's pages.
381 1.1 dholland *
382 1.1 dholland * we should update the size even when uiomove failed.
383 1.1 dholland */
384 1.1 dholland
385 1.1 dholland if (vp->v_size < newoff) {
386 1.1 dholland uvm_vnp_setsize(vp, newoff);
387 1.1 dholland extended = 1;
388 1.1 dholland }
389 1.1 dholland
390 1.1 dholland if (error)
391 1.1 dholland break;
392 1.1 dholland
393 1.1 dholland /*
394 1.1 dholland * flush what we just wrote if necessary.
395 1.1 dholland * XXXUBC simplistic async flushing.
396 1.1 dholland */
397 1.1 dholland
398 1.7 christos __USE(async);
399 1.1 dholland }
400 1.1 dholland if (error == 0 && ioflag & IO_SYNC) {
401 1.26 ad rw_enter(vp->v_uobj.vmobjlock, RW_WRITER);
402 1.19 dholland error = VOP_PUTPAGES(vp, trunc_page(origoff & lfs_sb_getbmask(fs)),
403 1.4 christos round_page(lfs_blkroundup(fs, uio->uio_offset)),
404 1.17 riastrad PGO_CLEANIT | PGO_SYNCIO);
405 1.1 dholland }
406 1.1 dholland
407 1.8 riastrad out:
408 1.11 riastrad error = ulfs_post_write_update(vp, uio, ioflag, cred, osize, resid,
409 1.11 riastrad extended, error);
410 1.8 riastrad
411 1.8 riastrad return (error);
412 1.8 riastrad }
413 1.8 riastrad
414 1.8 riastrad /*
415 1.8 riastrad * UFS op for writing via the buffer cache
416 1.8 riastrad */
417 1.8 riastrad int
418 1.8 riastrad BUFWR(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
419 1.8 riastrad {
420 1.8 riastrad struct inode *ip;
421 1.8 riastrad FS *fs;
422 1.8 riastrad int flags;
423 1.8 riastrad struct buf *bp;
424 1.16 riastrad off_t osize;
425 1.8 riastrad int resid, xfersize, size, blkoffset;
426 1.8 riastrad daddr_t lbn;
427 1.8 riastrad int extended=0;
428 1.8 riastrad int error;
429 1.8 riastrad bool need_unreserve = false;
430 1.8 riastrad
431 1.9 riastrad KASSERT(ISSET(ioflag, IO_NODELOCKED));
432 1.8 riastrad KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
433 1.8 riastrad KASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
434 1.8 riastrad KASSERT(vp->v_type != VDIR || ISSET(ioflag, IO_SYNC));
435 1.8 riastrad KASSERT(uio->uio_rw == UIO_WRITE);
436 1.8 riastrad
437 1.8 riastrad ip = VTOI(vp);
438 1.8 riastrad fs = ip->I_FS;
439 1.8 riastrad
440 1.8 riastrad KASSERT(vp->v_size == ip->i_size);
441 1.8 riastrad
442 1.8 riastrad if (uio->uio_offset < 0 ||
443 1.8 riastrad uio->uio_resid > fs->um_maxfilesize ||
444 1.8 riastrad uio->uio_offset > (fs->um_maxfilesize - uio->uio_resid))
445 1.8 riastrad return EFBIG;
446 1.8 riastrad KASSERT(vp != fs->lfs_ivnode);
447 1.8 riastrad if (uio->uio_resid == 0)
448 1.8 riastrad return 0;
449 1.8 riastrad
450 1.8 riastrad flags = ioflag & IO_SYNC ? B_SYNC : 0;
451 1.8 riastrad resid = uio->uio_resid;
452 1.8 riastrad osize = ip->i_size;
453 1.8 riastrad error = 0;
454 1.8 riastrad
455 1.8 riastrad KASSERT(vp->v_type != VREG);
456 1.8 riastrad
457 1.8 riastrad lfs_availwait(fs, lfs_btofsb(fs, uio->uio_resid));
458 1.8 riastrad lfs_check(vp, LFS_UNUSED_LBN, 0);
459 1.8 riastrad
460 1.12 riastrad /* XXX Should never have pages cached here. */
461 1.16 riastrad KASSERT(vp->v_uobj.uo_npages == 0);
462 1.1 dholland while (uio->uio_resid > 0) {
463 1.4 christos lbn = lfs_lblkno(fs, uio->uio_offset);
464 1.4 christos blkoffset = lfs_blkoff(fs, uio->uio_offset);
465 1.18 dholland xfersize = MIN(fs_sb_getbsize(fs) - blkoffset, uio->uio_resid);
466 1.18 dholland if (fs_sb_getbsize(fs) > xfersize)
467 1.1 dholland flags |= B_CLRBUF;
468 1.1 dholland else
469 1.1 dholland flags &= ~B_CLRBUF;
470 1.1 dholland
471 1.1 dholland error = lfs_reserve(fs, vp, NULL,
472 1.19 dholland lfs_btofsb(fs, (ULFS_NIADDR + 1) << lfs_sb_getbshift(fs)));
473 1.1 dholland if (error)
474 1.1 dholland break;
475 1.1 dholland need_unreserve = true;
476 1.8 riastrad error = lfs_balloc(vp, uio->uio_offset, xfersize, cred, flags,
477 1.8 riastrad &bp);
478 1.1 dholland
479 1.1 dholland if (error)
480 1.1 dholland break;
481 1.1 dholland if (uio->uio_offset + xfersize > ip->i_size) {
482 1.1 dholland ip->i_size = uio->uio_offset + xfersize;
483 1.1 dholland DIP_ASSIGN(ip, size, ip->i_size);
484 1.1 dholland uvm_vnp_setsize(vp, ip->i_size);
485 1.1 dholland extended = 1;
486 1.1 dholland }
487 1.4 christos size = lfs_blksize(fs, ip, lbn) - bp->b_resid;
488 1.1 dholland if (xfersize > size)
489 1.1 dholland xfersize = size;
490 1.1 dholland
491 1.1 dholland error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
492 1.1 dholland
493 1.1 dholland /*
494 1.1 dholland * if we didn't clear the block and the uiomove failed,
495 1.1 dholland * the buf will now contain part of some other file,
496 1.1 dholland * so we need to invalidate it.
497 1.1 dholland */
498 1.1 dholland if (error && (flags & B_CLRBUF) == 0) {
499 1.1 dholland brelse(bp, BC_INVAL);
500 1.1 dholland break;
501 1.1 dholland }
502 1.1 dholland (void)VOP_BWRITE(bp->b_vp, bp);
503 1.1 dholland lfs_reserve(fs, vp, NULL,
504 1.19 dholland -lfs_btofsb(fs, (ULFS_NIADDR + 1) << lfs_sb_getbshift(fs)));
505 1.1 dholland need_unreserve = false;
506 1.1 dholland if (error || xfersize == 0)
507 1.1 dholland break;
508 1.1 dholland }
509 1.1 dholland if (need_unreserve) {
510 1.1 dholland lfs_reserve(fs, vp, NULL,
511 1.19 dholland -lfs_btofsb(fs, (ULFS_NIADDR + 1) << lfs_sb_getbshift(fs)));
512 1.1 dholland }
513 1.1 dholland
514 1.11 riastrad error = ulfs_post_write_update(vp, uio, ioflag, cred, osize, resid,
515 1.11 riastrad extended, error);
516 1.11 riastrad
517 1.11 riastrad return (error);
518 1.11 riastrad }
519 1.11 riastrad
520 1.11 riastrad static int
521 1.11 riastrad ulfs_post_write_update(struct vnode *vp, struct uio *uio, int ioflag,
522 1.14 riastrad kauth_cred_t cred, off_t osize, int resid, int extended, int oerror)
523 1.11 riastrad {
524 1.11 riastrad struct inode *ip = VTOI(vp);
525 1.14 riastrad int error = oerror;
526 1.11 riastrad
527 1.11 riastrad /* Trigger ctime and mtime updates, and atime if MNT_RELATIME. */
528 1.24 maya ip->i_state |= IN_CHANGE | IN_UPDATE;
529 1.11 riastrad if (vp->v_mount->mnt_flag & MNT_RELATIME)
530 1.24 maya ip->i_state |= IN_ACCESS;
531 1.11 riastrad
532 1.1 dholland /*
533 1.11 riastrad * If we successfully wrote any data and we are not the superuser,
534 1.1 dholland * we clear the setuid and setgid bits as a precaution against
535 1.1 dholland * tampering.
536 1.1 dholland */
537 1.8 riastrad if (resid > uio->uio_resid && cred) {
538 1.1 dholland if (ip->i_mode & ISUID) {
539 1.8 riastrad if (kauth_authorize_vnode(cred,
540 1.1 dholland KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0) {
541 1.1 dholland ip->i_mode &= ~ISUID;
542 1.1 dholland DIP_ASSIGN(ip, mode, ip->i_mode);
543 1.1 dholland }
544 1.1 dholland }
545 1.1 dholland
546 1.1 dholland if (ip->i_mode & ISGID) {
547 1.8 riastrad if (kauth_authorize_vnode(cred,
548 1.1 dholland KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0) {
549 1.1 dholland ip->i_mode &= ~ISGID;
550 1.1 dholland DIP_ASSIGN(ip, mode, ip->i_mode);
551 1.1 dholland }
552 1.1 dholland }
553 1.1 dholland }
554 1.11 riastrad
555 1.11 riastrad /* If we successfully wrote anything, notify kevent listeners. */
556 1.1 dholland if (resid > uio->uio_resid)
557 1.1 dholland VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
558 1.11 riastrad
559 1.11 riastrad /*
560 1.11 riastrad * Update the size on disk: truncate back to original size on
561 1.11 riastrad * error, or reflect the new size on success.
562 1.11 riastrad */
563 1.1 dholland if (error) {
564 1.8 riastrad (void) lfs_truncate(vp, osize, ioflag & IO_SYNC, cred);
565 1.1 dholland uio->uio_offset -= resid - uio->uio_resid;
566 1.1 dholland uio->uio_resid = resid;
567 1.3 dholland } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC) {
568 1.5 dholland error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
569 1.3 dholland } else {
570 1.3 dholland /* nothing */
571 1.3 dholland }
572 1.12 riastrad
573 1.12 riastrad /* Make sure the vnode uvm size matches the inode file size. */
574 1.1 dholland KASSERT(vp->v_size == ip->i_size);
575 1.1 dholland
576 1.14 riastrad /* Write error overrides any inode update error. */
577 1.14 riastrad if (oerror)
578 1.14 riastrad error = oerror;
579 1.11 riastrad return error;
580 1.1 dholland }
581