ulfs_readwrite.c revision 1.10 1 1.10 riastrad /* $NetBSD: ulfs_readwrite.c,v 1.10 2015/03/28 03:53:36 riastradh Exp $ */
2 1.1 dholland /* from NetBSD: ufs_readwrite.c,v 1.105 2013/01/22 09:39:18 dholland 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.10 riastrad __KERNEL_RCSID(1, "$NetBSD: ulfs_readwrite.c,v 1.10 2015/03/28 03:53:36 riastradh Exp $");
37 1.1 dholland
38 1.1 dholland #ifdef LFS_READWRITE
39 1.1 dholland #define FS struct lfs
40 1.1 dholland #define I_FS i_lfs
41 1.1 dholland #define READ lfs_read
42 1.1 dholland #define READ_S "lfs_read"
43 1.1 dholland #define WRITE lfs_write
44 1.1 dholland #define WRITE_S "lfs_write"
45 1.8 riastrad #define BUFRD lfs_bufrd
46 1.8 riastrad #define BUFWR lfs_bufwr
47 1.1 dholland #define fs_bsize lfs_bsize
48 1.1 dholland #define fs_bmask lfs_bmask
49 1.1 dholland #else
50 1.1 dholland #define FS struct fs
51 1.1 dholland #define I_FS i_fs
52 1.1 dholland #define READ ffs_read
53 1.1 dholland #define READ_S "ffs_read"
54 1.1 dholland #define WRITE ffs_write
55 1.1 dholland #define WRITE_S "ffs_write"
56 1.8 riastrad #define BUFRD ffs_bufrd
57 1.8 riastrad #define BUFWR ffs_bufwr
58 1.1 dholland #endif
59 1.1 dholland
60 1.1 dholland /*
61 1.1 dholland * Vnode op for reading.
62 1.1 dholland */
63 1.1 dholland /* ARGSUSED */
64 1.1 dholland int
65 1.1 dholland READ(void *v)
66 1.1 dholland {
67 1.1 dholland struct vop_read_args /* {
68 1.1 dholland struct vnode *a_vp;
69 1.1 dholland struct uio *a_uio;
70 1.1 dholland int a_ioflag;
71 1.1 dholland kauth_cred_t a_cred;
72 1.1 dholland } */ *ap = v;
73 1.1 dholland struct vnode *vp;
74 1.1 dholland struct inode *ip;
75 1.1 dholland struct uio *uio;
76 1.1 dholland FS *fs;
77 1.1 dholland vsize_t bytelen;
78 1.8 riastrad int error, ioflag, advice;
79 1.1 dholland
80 1.1 dholland vp = ap->a_vp;
81 1.1 dholland ip = VTOI(vp);
82 1.6 dholland fs = ip->I_FS;
83 1.1 dholland uio = ap->a_uio;
84 1.1 dholland ioflag = ap->a_ioflag;
85 1.1 dholland error = 0;
86 1.1 dholland
87 1.10 riastrad KASSERT(uio->uio_rw == UIO_READ);
88 1.10 riastrad KASSERT(vp->v_type == VREG || vp->v_type == VDIR);
89 1.1 dholland
90 1.8 riastrad /* XXX Eliminate me by refusing directory reads from userland. */
91 1.8 riastrad if (vp->v_type == VDIR)
92 1.8 riastrad return BUFRD(vp, uio, ioflag, ap->a_cred);
93 1.8 riastrad #ifdef LFS_READWRITE
94 1.8 riastrad /* XXX Eliminate me by using ufs_bufio in lfs. */
95 1.8 riastrad if (vp->v_type == VREG && ip->i_number == LFS_IFILE_INUM)
96 1.8 riastrad return BUFRD(vp, uio, ioflag, ap->a_cred);
97 1.8 riastrad #endif
98 1.6 dholland if ((u_int64_t)uio->uio_offset > fs->um_maxfilesize)
99 1.1 dholland return (EFBIG);
100 1.1 dholland if (uio->uio_resid == 0)
101 1.1 dholland return (0);
102 1.1 dholland
103 1.1 dholland #ifndef LFS_READWRITE
104 1.1 dholland if ((ip->i_flags & (SF_SNAPSHOT | SF_SNAPINVAL)) == SF_SNAPSHOT)
105 1.1 dholland return ffs_snapshot_read(vp, uio, ioflag);
106 1.1 dholland #endif /* !LFS_READWRITE */
107 1.1 dholland
108 1.1 dholland fstrans_start(vp->v_mount, FSTRANS_SHARED);
109 1.1 dholland
110 1.1 dholland if (uio->uio_offset >= ip->i_size)
111 1.1 dholland goto out;
112 1.1 dholland
113 1.8 riastrad KASSERT(vp->v_type == VREG);
114 1.8 riastrad advice = IO_ADV_DECODE(ap->a_ioflag);
115 1.8 riastrad while (uio->uio_resid > 0) {
116 1.8 riastrad if (ioflag & IO_DIRECT) {
117 1.8 riastrad genfs_directio(vp, uio, ioflag);
118 1.8 riastrad }
119 1.8 riastrad bytelen = MIN(ip->i_size - uio->uio_offset, uio->uio_resid);
120 1.8 riastrad if (bytelen == 0)
121 1.8 riastrad break;
122 1.8 riastrad error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
123 1.8 riastrad UBC_READ | UBC_PARTIALOK | UBC_UNMAP_FLAG(vp));
124 1.8 riastrad if (error)
125 1.8 riastrad break;
126 1.8 riastrad }
127 1.1 dholland
128 1.8 riastrad out:
129 1.8 riastrad if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
130 1.8 riastrad ip->i_flag |= IN_ACCESS;
131 1.8 riastrad if ((ap->a_ioflag & IO_SYNC) == IO_SYNC) {
132 1.8 riastrad error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
133 1.1 dholland }
134 1.8 riastrad }
135 1.8 riastrad
136 1.8 riastrad fstrans_done(vp->v_mount);
137 1.8 riastrad return (error);
138 1.8 riastrad }
139 1.8 riastrad
140 1.8 riastrad /*
141 1.8 riastrad * UFS op for reading via the buffer cache
142 1.8 riastrad */
143 1.8 riastrad int
144 1.8 riastrad BUFRD(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
145 1.8 riastrad {
146 1.8 riastrad struct inode *ip;
147 1.8 riastrad FS *fs;
148 1.8 riastrad struct buf *bp;
149 1.8 riastrad daddr_t lbn, nextlbn;
150 1.8 riastrad off_t bytesinfile;
151 1.8 riastrad long size, xfersize, blkoffset;
152 1.8 riastrad int error;
153 1.8 riastrad
154 1.8 riastrad KASSERT(VOP_ISLOCKED(vp));
155 1.8 riastrad KASSERT(vp->v_type == VDIR || vp->v_type == VLNK ||
156 1.8 riastrad vp->v_type == VREG);
157 1.8 riastrad KASSERT(uio->uio_rw == UIO_READ);
158 1.8 riastrad
159 1.8 riastrad ip = VTOI(vp);
160 1.8 riastrad fs = ip->I_FS;
161 1.8 riastrad error = 0;
162 1.8 riastrad
163 1.8 riastrad KASSERT(vp->v_type != VLNK || ip->i_size < fs->um_maxsymlinklen);
164 1.8 riastrad KASSERT(vp->v_type != VLNK || fs->um_maxsymlinklen != 0 ||
165 1.8 riastrad DIP(ip, blocks) == 0);
166 1.8 riastrad KASSERT(vp->v_type != VREG || vp == fs->lfs_ivnode);
167 1.8 riastrad KASSERT(vp->v_type != VREG || ip->i_number == LFS_IFILE_INUM);
168 1.8 riastrad
169 1.8 riastrad if (uio->uio_offset > fs->um_maxfilesize)
170 1.8 riastrad return EFBIG;
171 1.8 riastrad if (uio->uio_resid == 0)
172 1.8 riastrad return 0;
173 1.8 riastrad
174 1.8 riastrad #ifndef LFS_READWRITE
175 1.8 riastrad KASSERT(!ISSET(ip->i_flags, (SF_SNAPSHOT | SF_SNAPINVAL)));
176 1.8 riastrad #endif
177 1.8 riastrad
178 1.8 riastrad fstrans_start(vp->v_mount, FSTRANS_SHARED);
179 1.8 riastrad
180 1.8 riastrad if (uio->uio_offset >= ip->i_size)
181 1.1 dholland goto out;
182 1.1 dholland
183 1.1 dholland for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
184 1.1 dholland bytesinfile = ip->i_size - uio->uio_offset;
185 1.1 dholland if (bytesinfile <= 0)
186 1.1 dholland break;
187 1.4 christos lbn = lfs_lblkno(fs, uio->uio_offset);
188 1.1 dholland nextlbn = lbn + 1;
189 1.4 christos size = lfs_blksize(fs, ip, lbn);
190 1.4 christos blkoffset = lfs_blkoff(fs, uio->uio_offset);
191 1.1 dholland xfersize = MIN(MIN(fs->fs_bsize - blkoffset, uio->uio_resid),
192 1.1 dholland bytesinfile);
193 1.1 dholland
194 1.4 christos if (lfs_lblktosize(fs, nextlbn) >= ip->i_size)
195 1.1 dholland error = bread(vp, lbn, size, NOCRED, 0, &bp);
196 1.1 dholland else {
197 1.4 christos int nextsize = lfs_blksize(fs, ip, nextlbn);
198 1.1 dholland error = breadn(vp, lbn,
199 1.1 dholland size, &nextlbn, &nextsize, 1, NOCRED, 0, &bp);
200 1.1 dholland }
201 1.1 dholland if (error)
202 1.1 dholland break;
203 1.1 dholland
204 1.1 dholland /*
205 1.1 dholland * We should only get non-zero b_resid when an I/O error
206 1.1 dholland * has occurred, which should cause us to break above.
207 1.1 dholland * However, if the short read did not cause an error,
208 1.1 dholland * then we want to ensure that we do not uiomove bad
209 1.1 dholland * or uninitialized data.
210 1.1 dholland */
211 1.1 dholland size -= bp->b_resid;
212 1.1 dholland if (size < xfersize) {
213 1.1 dholland if (size == 0)
214 1.1 dholland break;
215 1.1 dholland xfersize = size;
216 1.1 dholland }
217 1.1 dholland error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
218 1.1 dholland if (error)
219 1.1 dholland break;
220 1.1 dholland brelse(bp, 0);
221 1.1 dholland }
222 1.1 dholland if (bp != NULL)
223 1.1 dholland brelse(bp, 0);
224 1.1 dholland
225 1.1 dholland out:
226 1.1 dholland if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
227 1.1 dholland ip->i_flag |= IN_ACCESS;
228 1.8 riastrad if ((ioflag & IO_SYNC) == IO_SYNC) {
229 1.5 dholland error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
230 1.1 dholland }
231 1.1 dholland }
232 1.1 dholland
233 1.1 dholland fstrans_done(vp->v_mount);
234 1.1 dholland return (error);
235 1.1 dholland }
236 1.1 dholland
237 1.1 dholland /*
238 1.1 dholland * Vnode op for writing.
239 1.1 dholland */
240 1.1 dholland int
241 1.1 dholland WRITE(void *v)
242 1.1 dholland {
243 1.1 dholland struct vop_write_args /* {
244 1.1 dholland struct vnode *a_vp;
245 1.1 dholland struct uio *a_uio;
246 1.1 dholland int a_ioflag;
247 1.1 dholland kauth_cred_t a_cred;
248 1.1 dholland } */ *ap = v;
249 1.1 dholland struct vnode *vp;
250 1.1 dholland struct uio *uio;
251 1.1 dholland struct inode *ip;
252 1.1 dholland FS *fs;
253 1.1 dholland kauth_cred_t cred;
254 1.1 dholland off_t osize, origoff, oldoff, preallocoff, endallocoff, nsize;
255 1.8 riastrad int blkoffset, error, flags, ioflag, resid;
256 1.1 dholland int aflag;
257 1.1 dholland int extended=0;
258 1.1 dholland vsize_t bytelen;
259 1.1 dholland bool async;
260 1.1 dholland
261 1.1 dholland cred = ap->a_cred;
262 1.1 dholland ioflag = ap->a_ioflag;
263 1.1 dholland uio = ap->a_uio;
264 1.1 dholland vp = ap->a_vp;
265 1.1 dholland ip = VTOI(vp);
266 1.1 dholland
267 1.1 dholland KASSERT(vp->v_size == ip->i_size);
268 1.10 riastrad KASSERT(uio->uio_rw == UIO_WRITE);
269 1.10 riastrad KASSERT(vp->v_type == VREG);
270 1.1 dholland
271 1.10 riastrad if (ioflag & IO_APPEND)
272 1.10 riastrad uio->uio_offset = ip->i_size;
273 1.10 riastrad if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
274 1.10 riastrad return (EPERM);
275 1.1 dholland
276 1.1 dholland fs = ip->I_FS;
277 1.1 dholland if (uio->uio_offset < 0 ||
278 1.6 dholland (u_int64_t)uio->uio_offset + uio->uio_resid > fs->um_maxfilesize)
279 1.1 dholland return (EFBIG);
280 1.1 dholland #ifdef LFS_READWRITE
281 1.1 dholland /* Disallow writes to the Ifile, even if noschg flag is removed */
282 1.1 dholland /* XXX can this go away when the Ifile is no longer in the namespace? */
283 1.1 dholland if (vp == fs->lfs_ivnode)
284 1.1 dholland return (EPERM);
285 1.1 dholland #endif
286 1.1 dholland if (uio->uio_resid == 0)
287 1.1 dholland return (0);
288 1.1 dholland
289 1.1 dholland fstrans_start(vp->v_mount, FSTRANS_SHARED);
290 1.1 dholland
291 1.1 dholland flags = ioflag & IO_SYNC ? B_SYNC : 0;
292 1.1 dholland async = vp->v_mount->mnt_flag & MNT_ASYNC;
293 1.1 dholland origoff = uio->uio_offset;
294 1.1 dholland resid = uio->uio_resid;
295 1.1 dholland osize = ip->i_size;
296 1.1 dholland error = 0;
297 1.1 dholland
298 1.8 riastrad KASSERT(vp->v_type == VREG);
299 1.1 dholland
300 1.1 dholland #ifdef LFS_READWRITE
301 1.1 dholland async = true;
302 1.4 christos lfs_availwait(fs, lfs_btofsb(fs, uio->uio_resid));
303 1.1 dholland lfs_check(vp, LFS_UNUSED_LBN, 0);
304 1.1 dholland #endif /* !LFS_READWRITE */
305 1.1 dholland
306 1.4 christos preallocoff = round_page(lfs_blkroundup(fs, MAX(osize, uio->uio_offset)));
307 1.1 dholland aflag = ioflag & IO_SYNC ? B_SYNC : 0;
308 1.1 dholland nsize = MAX(osize, uio->uio_offset + uio->uio_resid);
309 1.4 christos endallocoff = nsize - lfs_blkoff(fs, nsize);
310 1.1 dholland
311 1.1 dholland /*
312 1.1 dholland * if we're increasing the file size, deal with expanding
313 1.1 dholland * the fragment if there is one.
314 1.1 dholland */
315 1.1 dholland
316 1.4 christos if (nsize > osize && lfs_lblkno(fs, osize) < ULFS_NDADDR &&
317 1.4 christos lfs_lblkno(fs, osize) != lfs_lblkno(fs, nsize) &&
318 1.4 christos lfs_blkroundup(fs, osize) != osize) {
319 1.1 dholland off_t eob;
320 1.1 dholland
321 1.4 christos eob = lfs_blkroundup(fs, osize);
322 1.1 dholland uvm_vnp_setwritesize(vp, eob);
323 1.2 dholland error = ulfs_balloc_range(vp, osize, eob - osize, cred, aflag);
324 1.1 dholland if (error)
325 1.1 dholland goto out;
326 1.1 dholland if (flags & B_SYNC) {
327 1.1 dholland mutex_enter(vp->v_interlock);
328 1.1 dholland VOP_PUTPAGES(vp, trunc_page(osize & fs->fs_bmask),
329 1.1 dholland round_page(eob),
330 1.1 dholland PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED);
331 1.1 dholland }
332 1.1 dholland }
333 1.1 dholland
334 1.1 dholland while (uio->uio_resid > 0) {
335 1.1 dholland int ubc_flags = UBC_WRITE;
336 1.1 dholland bool overwrite; /* if we're overwrite a whole block */
337 1.1 dholland off_t newoff;
338 1.1 dholland
339 1.1 dholland if (ioflag & IO_DIRECT) {
340 1.1 dholland genfs_directio(vp, uio, ioflag | IO_JOURNALLOCKED);
341 1.1 dholland }
342 1.1 dholland
343 1.1 dholland oldoff = uio->uio_offset;
344 1.4 christos blkoffset = lfs_blkoff(fs, uio->uio_offset);
345 1.1 dholland bytelen = MIN(fs->fs_bsize - blkoffset, uio->uio_resid);
346 1.1 dholland if (bytelen == 0) {
347 1.1 dholland break;
348 1.1 dholland }
349 1.1 dholland
350 1.1 dholland /*
351 1.1 dholland * if we're filling in a hole, allocate the blocks now and
352 1.1 dholland * initialize the pages first. if we're extending the file,
353 1.1 dholland * we can safely allocate blocks without initializing pages
354 1.1 dholland * since the new blocks will be inaccessible until the write
355 1.1 dholland * is complete.
356 1.1 dholland */
357 1.1 dholland overwrite = uio->uio_offset >= preallocoff &&
358 1.1 dholland uio->uio_offset < endallocoff;
359 1.1 dholland if (!overwrite && (vp->v_vflag & VV_MAPPED) == 0 &&
360 1.4 christos lfs_blkoff(fs, uio->uio_offset) == 0 &&
361 1.1 dholland (uio->uio_offset & PAGE_MASK) == 0) {
362 1.1 dholland vsize_t len;
363 1.1 dholland
364 1.1 dholland len = trunc_page(bytelen);
365 1.4 christos len -= lfs_blkoff(fs, len);
366 1.1 dholland if (len > 0) {
367 1.1 dholland overwrite = true;
368 1.1 dholland bytelen = len;
369 1.1 dholland }
370 1.1 dholland }
371 1.1 dholland
372 1.1 dholland newoff = oldoff + bytelen;
373 1.1 dholland if (vp->v_size < newoff) {
374 1.1 dholland uvm_vnp_setwritesize(vp, newoff);
375 1.1 dholland }
376 1.1 dholland
377 1.1 dholland if (!overwrite) {
378 1.2 dholland error = ulfs_balloc_range(vp, uio->uio_offset, bytelen,
379 1.1 dholland cred, aflag);
380 1.1 dholland if (error)
381 1.1 dholland break;
382 1.1 dholland } else {
383 1.1 dholland genfs_node_wrlock(vp);
384 1.1 dholland error = GOP_ALLOC(vp, uio->uio_offset, bytelen,
385 1.1 dholland aflag, cred);
386 1.1 dholland genfs_node_unlock(vp);
387 1.1 dholland if (error)
388 1.1 dholland break;
389 1.1 dholland ubc_flags |= UBC_FAULTBUSY;
390 1.1 dholland }
391 1.1 dholland
392 1.1 dholland /*
393 1.1 dholland * copy the data.
394 1.1 dholland */
395 1.1 dholland
396 1.1 dholland error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
397 1.1 dholland IO_ADV_DECODE(ioflag), ubc_flags | UBC_UNMAP_FLAG(vp));
398 1.1 dholland
399 1.1 dholland /*
400 1.1 dholland * update UVM's notion of the size now that we've
401 1.1 dholland * copied the data into the vnode's pages.
402 1.1 dholland *
403 1.1 dholland * we should update the size even when uiomove failed.
404 1.1 dholland */
405 1.1 dholland
406 1.1 dholland if (vp->v_size < newoff) {
407 1.1 dholland uvm_vnp_setsize(vp, newoff);
408 1.1 dholland extended = 1;
409 1.1 dholland }
410 1.1 dholland
411 1.1 dholland if (error)
412 1.1 dholland break;
413 1.1 dholland
414 1.1 dholland /*
415 1.1 dholland * flush what we just wrote if necessary.
416 1.1 dholland * XXXUBC simplistic async flushing.
417 1.1 dholland */
418 1.1 dholland
419 1.1 dholland #ifndef LFS_READWRITE
420 1.1 dholland if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
421 1.1 dholland mutex_enter(vp->v_interlock);
422 1.1 dholland error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
423 1.1 dholland (uio->uio_offset >> 16) << 16,
424 1.1 dholland PGO_CLEANIT | PGO_JOURNALLOCKED | PGO_LAZY);
425 1.1 dholland if (error)
426 1.1 dholland break;
427 1.1 dholland }
428 1.7 christos #else
429 1.7 christos __USE(async);
430 1.1 dholland #endif
431 1.1 dholland }
432 1.1 dholland if (error == 0 && ioflag & IO_SYNC) {
433 1.1 dholland mutex_enter(vp->v_interlock);
434 1.1 dholland error = VOP_PUTPAGES(vp, trunc_page(origoff & fs->fs_bmask),
435 1.4 christos round_page(lfs_blkroundup(fs, uio->uio_offset)),
436 1.1 dholland PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED);
437 1.1 dholland }
438 1.1 dholland
439 1.8 riastrad /*
440 1.8 riastrad * If we successfully wrote any data, and we are not the superuser
441 1.8 riastrad * we clear the setuid and setgid bits as a precaution against
442 1.8 riastrad * tampering.
443 1.8 riastrad */
444 1.8 riastrad out:
445 1.8 riastrad ip->i_flag |= IN_CHANGE | IN_UPDATE;
446 1.8 riastrad if (vp->v_mount->mnt_flag & MNT_RELATIME)
447 1.8 riastrad ip->i_flag |= IN_ACCESS;
448 1.8 riastrad if (resid > uio->uio_resid && ap->a_cred) {
449 1.8 riastrad if (ip->i_mode & ISUID) {
450 1.8 riastrad if (kauth_authorize_vnode(ap->a_cred,
451 1.8 riastrad KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0) {
452 1.8 riastrad ip->i_mode &= ~ISUID;
453 1.8 riastrad DIP_ASSIGN(ip, mode, ip->i_mode);
454 1.8 riastrad }
455 1.8 riastrad }
456 1.8 riastrad
457 1.8 riastrad if (ip->i_mode & ISGID) {
458 1.8 riastrad if (kauth_authorize_vnode(ap->a_cred,
459 1.8 riastrad KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0) {
460 1.8 riastrad ip->i_mode &= ~ISGID;
461 1.8 riastrad DIP_ASSIGN(ip, mode, ip->i_mode);
462 1.8 riastrad }
463 1.8 riastrad }
464 1.8 riastrad }
465 1.8 riastrad if (resid > uio->uio_resid)
466 1.8 riastrad VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
467 1.8 riastrad if (error) {
468 1.8 riastrad (void) lfs_truncate(vp, osize, ioflag & IO_SYNC, ap->a_cred);
469 1.8 riastrad uio->uio_offset -= resid - uio->uio_resid;
470 1.8 riastrad uio->uio_resid = resid;
471 1.8 riastrad } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC) {
472 1.8 riastrad error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
473 1.8 riastrad } else {
474 1.8 riastrad /* nothing */
475 1.8 riastrad }
476 1.8 riastrad KASSERT(vp->v_size == ip->i_size);
477 1.8 riastrad fstrans_done(vp->v_mount);
478 1.8 riastrad
479 1.8 riastrad return (error);
480 1.8 riastrad }
481 1.8 riastrad
482 1.8 riastrad /*
483 1.8 riastrad * UFS op for writing via the buffer cache
484 1.8 riastrad */
485 1.8 riastrad int
486 1.8 riastrad BUFWR(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t cred)
487 1.8 riastrad {
488 1.8 riastrad struct inode *ip;
489 1.8 riastrad FS *fs;
490 1.8 riastrad int flags;
491 1.8 riastrad struct buf *bp;
492 1.8 riastrad off_t osize, origoff;
493 1.8 riastrad int resid, xfersize, size, blkoffset;
494 1.8 riastrad daddr_t lbn;
495 1.8 riastrad int extended=0;
496 1.8 riastrad int error;
497 1.8 riastrad #ifdef LFS_READWRITE
498 1.8 riastrad bool need_unreserve = false;
499 1.8 riastrad #endif
500 1.8 riastrad
501 1.9 riastrad KASSERT(ISSET(ioflag, IO_NODELOCKED));
502 1.8 riastrad KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
503 1.8 riastrad KASSERT(vp->v_type == VDIR || vp->v_type == VLNK);
504 1.8 riastrad KASSERT(vp->v_type != VDIR || ISSET(ioflag, IO_SYNC));
505 1.8 riastrad KASSERT(uio->uio_rw == UIO_WRITE);
506 1.8 riastrad
507 1.8 riastrad ip = VTOI(vp);
508 1.8 riastrad fs = ip->I_FS;
509 1.8 riastrad
510 1.8 riastrad KASSERT(vp->v_size == ip->i_size);
511 1.8 riastrad
512 1.8 riastrad if (uio->uio_offset < 0 ||
513 1.8 riastrad uio->uio_resid > fs->um_maxfilesize ||
514 1.8 riastrad uio->uio_offset > (fs->um_maxfilesize - uio->uio_resid))
515 1.8 riastrad return EFBIG;
516 1.8 riastrad #ifdef LFS_READWRITE
517 1.8 riastrad KASSERT(vp != fs->lfs_ivnode);
518 1.8 riastrad #endif
519 1.8 riastrad if (uio->uio_resid == 0)
520 1.8 riastrad return 0;
521 1.8 riastrad
522 1.8 riastrad fstrans_start(vp->v_mount, FSTRANS_SHARED);
523 1.8 riastrad
524 1.8 riastrad flags = ioflag & IO_SYNC ? B_SYNC : 0;
525 1.8 riastrad origoff = uio->uio_offset;
526 1.8 riastrad resid = uio->uio_resid;
527 1.8 riastrad osize = ip->i_size;
528 1.8 riastrad error = 0;
529 1.8 riastrad
530 1.8 riastrad KASSERT(vp->v_type != VREG);
531 1.8 riastrad
532 1.8 riastrad #ifdef LFS_READWRITE
533 1.8 riastrad lfs_availwait(fs, lfs_btofsb(fs, uio->uio_resid));
534 1.8 riastrad lfs_check(vp, LFS_UNUSED_LBN, 0);
535 1.8 riastrad #endif /* !LFS_READWRITE */
536 1.8 riastrad
537 1.8 riastrad /* XXX Should never have cached pages here. */
538 1.1 dholland mutex_enter(vp->v_interlock);
539 1.1 dholland VOP_PUTPAGES(vp, trunc_page(origoff), round_page(origoff + resid),
540 1.1 dholland PGO_CLEANIT | PGO_FREE | PGO_SYNCIO | PGO_JOURNALLOCKED);
541 1.1 dholland while (uio->uio_resid > 0) {
542 1.4 christos lbn = lfs_lblkno(fs, uio->uio_offset);
543 1.4 christos blkoffset = lfs_blkoff(fs, uio->uio_offset);
544 1.1 dholland xfersize = MIN(fs->fs_bsize - blkoffset, uio->uio_resid);
545 1.1 dholland if (fs->fs_bsize > xfersize)
546 1.1 dholland flags |= B_CLRBUF;
547 1.1 dholland else
548 1.1 dholland flags &= ~B_CLRBUF;
549 1.1 dholland
550 1.1 dholland #ifdef LFS_READWRITE
551 1.1 dholland error = lfs_reserve(fs, vp, NULL,
552 1.4 christos lfs_btofsb(fs, (ULFS_NIADDR + 1) << fs->lfs_bshift));
553 1.1 dholland if (error)
554 1.1 dholland break;
555 1.1 dholland need_unreserve = true;
556 1.1 dholland #endif
557 1.8 riastrad error = lfs_balloc(vp, uio->uio_offset, xfersize, cred, flags,
558 1.8 riastrad &bp);
559 1.1 dholland
560 1.1 dholland if (error)
561 1.1 dholland break;
562 1.1 dholland if (uio->uio_offset + xfersize > ip->i_size) {
563 1.1 dholland ip->i_size = uio->uio_offset + xfersize;
564 1.1 dholland DIP_ASSIGN(ip, size, ip->i_size);
565 1.1 dholland uvm_vnp_setsize(vp, ip->i_size);
566 1.1 dholland extended = 1;
567 1.1 dholland }
568 1.4 christos size = lfs_blksize(fs, ip, lbn) - bp->b_resid;
569 1.1 dholland if (xfersize > size)
570 1.1 dholland xfersize = size;
571 1.1 dholland
572 1.1 dholland error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
573 1.1 dholland
574 1.1 dholland /*
575 1.1 dholland * if we didn't clear the block and the uiomove failed,
576 1.1 dholland * the buf will now contain part of some other file,
577 1.1 dholland * so we need to invalidate it.
578 1.1 dholland */
579 1.1 dholland if (error && (flags & B_CLRBUF) == 0) {
580 1.1 dholland brelse(bp, BC_INVAL);
581 1.1 dholland break;
582 1.1 dholland }
583 1.1 dholland #ifdef LFS_READWRITE
584 1.1 dholland (void)VOP_BWRITE(bp->b_vp, bp);
585 1.1 dholland lfs_reserve(fs, vp, NULL,
586 1.4 christos -lfs_btofsb(fs, (ULFS_NIADDR + 1) << fs->lfs_bshift));
587 1.1 dholland need_unreserve = false;
588 1.1 dholland #else
589 1.1 dholland if (ioflag & IO_SYNC)
590 1.1 dholland (void)bwrite(bp);
591 1.1 dholland else if (xfersize + blkoffset == fs->fs_bsize)
592 1.1 dholland bawrite(bp);
593 1.1 dholland else
594 1.1 dholland bdwrite(bp);
595 1.1 dholland #endif
596 1.1 dholland if (error || xfersize == 0)
597 1.1 dholland break;
598 1.1 dholland }
599 1.1 dholland #ifdef LFS_READWRITE
600 1.1 dholland if (need_unreserve) {
601 1.1 dholland lfs_reserve(fs, vp, NULL,
602 1.4 christos -lfs_btofsb(fs, (ULFS_NIADDR + 1) << fs->lfs_bshift));
603 1.1 dholland }
604 1.1 dholland #endif
605 1.1 dholland
606 1.1 dholland /*
607 1.1 dholland * If we successfully wrote any data, and we are not the superuser
608 1.1 dholland * we clear the setuid and setgid bits as a precaution against
609 1.1 dholland * tampering.
610 1.1 dholland */
611 1.1 dholland ip->i_flag |= IN_CHANGE | IN_UPDATE;
612 1.1 dholland if (vp->v_mount->mnt_flag & MNT_RELATIME)
613 1.1 dholland ip->i_flag |= IN_ACCESS;
614 1.8 riastrad if (resid > uio->uio_resid && cred) {
615 1.1 dholland if (ip->i_mode & ISUID) {
616 1.8 riastrad if (kauth_authorize_vnode(cred,
617 1.1 dholland KAUTH_VNODE_RETAIN_SUID, vp, NULL, EPERM) != 0) {
618 1.1 dholland ip->i_mode &= ~ISUID;
619 1.1 dholland DIP_ASSIGN(ip, mode, ip->i_mode);
620 1.1 dholland }
621 1.1 dholland }
622 1.1 dholland
623 1.1 dholland if (ip->i_mode & ISGID) {
624 1.8 riastrad if (kauth_authorize_vnode(cred,
625 1.1 dholland KAUTH_VNODE_RETAIN_SGID, vp, NULL, EPERM) != 0) {
626 1.1 dholland ip->i_mode &= ~ISGID;
627 1.1 dholland DIP_ASSIGN(ip, mode, ip->i_mode);
628 1.1 dholland }
629 1.1 dholland }
630 1.1 dholland }
631 1.1 dholland if (resid > uio->uio_resid)
632 1.1 dholland VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
633 1.1 dholland if (error) {
634 1.8 riastrad (void) lfs_truncate(vp, osize, ioflag & IO_SYNC, cred);
635 1.1 dholland uio->uio_offset -= resid - uio->uio_resid;
636 1.1 dholland uio->uio_resid = resid;
637 1.3 dholland } else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC) {
638 1.5 dholland error = lfs_update(vp, NULL, NULL, UPDATE_WAIT);
639 1.3 dholland } else {
640 1.3 dholland /* nothing */
641 1.3 dholland }
642 1.1 dholland KASSERT(vp->v_size == ip->i_size);
643 1.1 dholland fstrans_done(vp->v_mount);
644 1.1 dholland
645 1.1 dholland return (error);
646 1.1 dholland }
647