1 /*-
2 * SPDX-License-Identifier: (BSD-2-Clause-FreeBSD AND BSD-3-Clause)
3 *
4 * Copyright (c) 2002, 2003 Networks Associates Technology, Inc.
5 * All rights reserved.
6 *
7 * This software was developed for the FreeBSD Project by Marshall
8 * Kirk McKusick and Network Associates Laboratories, the Security
9 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
10 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11 * research program
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * Copyright (c) 1982, 1986, 1989, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * from: @(#)ufs_readwrite.c 8.11 (Berkeley) 5/8/95
62 * from: $FreeBSD: .../ufs/ufs_readwrite.c,v 1.96 2002/08/12 09:22:11 phk ...
63 * @(#)ffs_vnops.c 8.15 (Berkeley) 5/14/95
64 */
65
66 #include <sys/cdefs.h>
67 __FBSDID("$FreeBSD$");
68
69 #include "opt_directio.h"
70 #include "opt_ffs.h"
71 #include "opt_ufs.h"
72
73 #include <sys/param.h>
74 #include <sys/bio.h>
75 #include <sys/systm.h>
76 #include <sys/buf.h>
77 #include <sys/conf.h>
78 #include <sys/extattr.h>
79 #include <sys/kernel.h>
80 #include <sys/limits.h>
81 #include <sys/malloc.h>
82 #include <sys/mount.h>
83 #include <sys/priv.h>
84 #include <sys/rwlock.h>
85 #include <sys/stat.h>
86 #include <sys/sysctl.h>
87 #include <sys/vmmeter.h>
88 #include <sys/vnode.h>
89
90 #include <vm/vm.h>
91 #include <vm/vm_param.h>
92 #include <vm/vm_extern.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_pager.h>
96 #include <vm/vnode_pager.h>
97
98 #include <ufs/ufs/extattr.h>
99 #include <ufs/ufs/quota.h>
100 #include <ufs/ufs/inode.h>
101 #include <ufs/ufs/ufs_extern.h>
102 #include <ufs/ufs/ufsmount.h>
103 #include <ufs/ufs/dir.h>
104 #ifdef UFS_DIRHASH
105 #include <ufs/ufs/dirhash.h>
106 #endif
107
108 #include <ufs/ffs/fs.h>
109 #include <ufs/ffs/ffs_extern.h>
110
111 #define ALIGNED_TO(ptr, s) \
112 (((uintptr_t)(ptr) & (_Alignof(s) - 1)) == 0)
113
114 #ifdef DIRECTIO
115 extern int ffs_rawread(struct vnode *vp, struct uio *uio, int *workdone);
116 #endif
117 static vop_fdatasync_t ffs_fdatasync;
118 static vop_fsync_t ffs_fsync;
119 static vop_getpages_t ffs_getpages;
120 static vop_getpages_async_t ffs_getpages_async;
121 static vop_lock1_t ffs_lock;
122 #ifdef INVARIANTS
123 static vop_unlock_t ffs_unlock_debug;
124 #endif
125 static vop_read_t ffs_read;
126 static vop_write_t ffs_write;
127 static int ffs_extread(struct vnode *vp, struct uio *uio, int ioflag);
128 static int ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag,
129 struct ucred *cred);
130 static vop_strategy_t ffsext_strategy;
131 static vop_closeextattr_t ffs_closeextattr;
132 static vop_deleteextattr_t ffs_deleteextattr;
133 static vop_getextattr_t ffs_getextattr;
134 static vop_listextattr_t ffs_listextattr;
135 static vop_openextattr_t ffs_openextattr;
136 static vop_setextattr_t ffs_setextattr;
137 static vop_vptofh_t ffs_vptofh;
138 static vop_vput_pair_t ffs_vput_pair;
139
140 /* Global vfs data structures for ufs. */
141 struct vop_vector ffs_vnodeops1 = {
142 .vop_default = &ufs_vnodeops,
143 .vop_fsync = ffs_fsync,
144 .vop_fdatasync = ffs_fdatasync,
145 .vop_getpages = ffs_getpages,
146 .vop_getpages_async = ffs_getpages_async,
147 .vop_lock1 = ffs_lock,
148 #ifdef INVARIANTS
149 .vop_unlock = ffs_unlock_debug,
150 #endif
151 .vop_read = ffs_read,
152 .vop_reallocblks = ffs_reallocblks,
153 .vop_write = ffs_write,
154 .vop_vptofh = ffs_vptofh,
155 .vop_vput_pair = ffs_vput_pair,
156 };
157 VFS_VOP_VECTOR_REGISTER(ffs_vnodeops1);
158
159 struct vop_vector ffs_fifoops1 = {
160 .vop_default = &ufs_fifoops,
161 .vop_fsync = ffs_fsync,
162 .vop_fdatasync = ffs_fdatasync,
163 .vop_lock1 = ffs_lock,
164 #ifdef INVARIANTS
165 .vop_unlock = ffs_unlock_debug,
166 #endif
167 .vop_vptofh = ffs_vptofh,
168 };
169 VFS_VOP_VECTOR_REGISTER(ffs_fifoops1);
170
171 /* Global vfs data structures for ufs. */
172 struct vop_vector ffs_vnodeops2 = {
173 .vop_default = &ufs_vnodeops,
174 .vop_fsync = ffs_fsync,
175 .vop_fdatasync = ffs_fdatasync,
176 .vop_getpages = ffs_getpages,
177 .vop_getpages_async = ffs_getpages_async,
178 .vop_lock1 = ffs_lock,
179 #ifdef INVARIANTS
180 .vop_unlock = ffs_unlock_debug,
181 #endif
182 .vop_read = ffs_read,
183 .vop_reallocblks = ffs_reallocblks,
184 .vop_write = ffs_write,
185 .vop_closeextattr = ffs_closeextattr,
186 .vop_deleteextattr = ffs_deleteextattr,
187 .vop_getextattr = ffs_getextattr,
188 .vop_listextattr = ffs_listextattr,
189 .vop_openextattr = ffs_openextattr,
190 .vop_setextattr = ffs_setextattr,
191 .vop_vptofh = ffs_vptofh,
192 .vop_vput_pair = ffs_vput_pair,
193 };
194 VFS_VOP_VECTOR_REGISTER(ffs_vnodeops2);
195
196 struct vop_vector ffs_fifoops2 = {
197 .vop_default = &ufs_fifoops,
198 .vop_fsync = ffs_fsync,
199 .vop_fdatasync = ffs_fdatasync,
200 .vop_lock1 = ffs_lock,
201 #ifdef INVARIANTS
202 .vop_unlock = ffs_unlock_debug,
203 #endif
204 .vop_reallocblks = ffs_reallocblks,
205 .vop_strategy = ffsext_strategy,
206 .vop_closeextattr = ffs_closeextattr,
207 .vop_deleteextattr = ffs_deleteextattr,
208 .vop_getextattr = ffs_getextattr,
209 .vop_listextattr = ffs_listextattr,
210 .vop_openextattr = ffs_openextattr,
211 .vop_setextattr = ffs_setextattr,
212 .vop_vptofh = ffs_vptofh,
213 };
214 VFS_VOP_VECTOR_REGISTER(ffs_fifoops2);
215
216 /*
217 * Synch an open file.
218 */
219 /* ARGSUSED */
220 static int
ffs_fsync(struct vop_fsync_args * ap)221 ffs_fsync(struct vop_fsync_args *ap)
222 {
223 struct vnode *vp;
224 struct bufobj *bo;
225 int error;
226
227 vp = ap->a_vp;
228 bo = &vp->v_bufobj;
229 retry:
230 error = ffs_syncvnode(vp, ap->a_waitfor, 0);
231 if (error)
232 return (error);
233 if (ap->a_waitfor == MNT_WAIT && DOINGSOFTDEP(vp)) {
234 error = softdep_fsync(vp);
235 if (error)
236 return (error);
237
238 /*
239 * The softdep_fsync() function may drop vp lock,
240 * allowing for dirty buffers to reappear on the
241 * bo_dirty list. Recheck and resync as needed.
242 */
243 BO_LOCK(bo);
244 if ((vp->v_type == VREG || vp->v_type == VDIR) &&
245 (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)) {
246 BO_UNLOCK(bo);
247 goto retry;
248 }
249 BO_UNLOCK(bo);
250 }
251 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), 0))
252 return (ENXIO);
253 return (0);
254 }
255
256 int
ffs_syncvnode(struct vnode * vp,int waitfor,int flags)257 ffs_syncvnode(struct vnode *vp, int waitfor, int flags)
258 {
259 struct inode *ip;
260 struct bufobj *bo;
261 struct ufsmount *ump;
262 struct buf *bp, *nbp;
263 ufs_lbn_t lbn;
264 int error, passes, wflag;
265 bool still_dirty, unlocked, wait;
266
267 ip = VTOI(vp);
268 bo = &vp->v_bufobj;
269 ump = VFSTOUFS(vp->v_mount);
270 #ifdef WITNESS
271 wflag = IS_SNAPSHOT(ip) ? LK_NOWITNESS : 0;
272 #else
273 wflag = 0;
274 #endif
275
276 /*
277 * When doing MNT_WAIT we must first flush all dependencies
278 * on the inode.
279 */
280 if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT &&
281 (error = softdep_sync_metadata(vp)) != 0) {
282 if (ffs_fsfail_cleanup(ump, error))
283 error = 0;
284 return (error);
285 }
286
287 /*
288 * Flush all dirty buffers associated with a vnode.
289 */
290 error = 0;
291 passes = 0;
292 wait = false; /* Always do an async pass first. */
293 unlocked = false;
294 lbn = lblkno(ITOFS(ip), (ip->i_size + ITOFS(ip)->fs_bsize - 1));
295 BO_LOCK(bo);
296 loop:
297 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs)
298 bp->b_vflags &= ~BV_SCANNED;
299 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
300 /*
301 * Reasons to skip this buffer: it has already been considered
302 * on this pass, the buffer has dependencies that will cause
303 * it to be redirtied and it has not already been deferred,
304 * or it is already being written.
305 */
306 if ((bp->b_vflags & BV_SCANNED) != 0)
307 continue;
308 bp->b_vflags |= BV_SCANNED;
309 /*
310 * Flush indirects in order, if requested.
311 *
312 * Note that if only datasync is requested, we can
313 * skip indirect blocks when softupdates are not
314 * active. Otherwise we must flush them with data,
315 * since dependencies prevent data block writes.
316 */
317 if (waitfor == MNT_WAIT && bp->b_lblkno <= -UFS_NDADDR &&
318 (lbn_level(bp->b_lblkno) >= passes ||
319 ((flags & DATA_ONLY) != 0 && !DOINGSOFTDEP(vp))))
320 continue;
321 if (bp->b_lblkno > lbn)
322 panic("ffs_syncvnode: syncing truncated data.");
323 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) == 0) {
324 BO_UNLOCK(bo);
325 } else if (wait) {
326 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
327 LK_INTERLOCK | wflag, BO_LOCKPTR(bo)) != 0) {
328 BO_LOCK(bo);
329 bp->b_vflags &= ~BV_SCANNED;
330 goto next_locked;
331 }
332 } else
333 continue;
334 if ((bp->b_flags & B_DELWRI) == 0)
335 panic("ffs_fsync: not dirty");
336 /*
337 * Check for dependencies and potentially complete them.
338 */
339 if (!LIST_EMPTY(&bp->b_dep) &&
340 (error = softdep_sync_buf(vp, bp,
341 wait ? MNT_WAIT : MNT_NOWAIT)) != 0) {
342 /*
343 * Lock order conflict, buffer was already unlocked,
344 * and vnode possibly unlocked.
345 */
346 if (error == ERELOOKUP) {
347 if (vp->v_data == NULL)
348 return (EBADF);
349 unlocked = true;
350 if (DOINGSOFTDEP(vp) && waitfor == MNT_WAIT &&
351 (error = softdep_sync_metadata(vp)) != 0) {
352 if (ffs_fsfail_cleanup(ump, error))
353 error = 0;
354 return (unlocked && error == 0 ?
355 ERELOOKUP : error);
356 }
357 /* Re-evaluate inode size */
358 lbn = lblkno(ITOFS(ip), (ip->i_size +
359 ITOFS(ip)->fs_bsize - 1));
360 goto next;
361 }
362 /* I/O error. */
363 if (error != EBUSY) {
364 BUF_UNLOCK(bp);
365 return (error);
366 }
367 /* If we deferred once, don't defer again. */
368 if ((bp->b_flags & B_DEFERRED) == 0) {
369 bp->b_flags |= B_DEFERRED;
370 BUF_UNLOCK(bp);
371 goto next;
372 }
373 }
374 if (wait) {
375 bremfree(bp);
376 error = bwrite(bp);
377 if (ffs_fsfail_cleanup(ump, error))
378 error = 0;
379 if (error != 0)
380 return (error);
381 } else if ((bp->b_flags & B_CLUSTEROK)) {
382 (void) vfs_bio_awrite(bp);
383 } else {
384 bremfree(bp);
385 (void) bawrite(bp);
386 }
387 next:
388 /*
389 * Since we may have slept during the I/O, we need
390 * to start from a known point.
391 */
392 BO_LOCK(bo);
393 next_locked:
394 nbp = TAILQ_FIRST(&bo->bo_dirty.bv_hd);
395 }
396 if (waitfor != MNT_WAIT) {
397 BO_UNLOCK(bo);
398 if ((flags & NO_INO_UPDT) != 0)
399 return (unlocked ? ERELOOKUP : 0);
400 error = ffs_update(vp, 0);
401 if (error == 0 && unlocked)
402 error = ERELOOKUP;
403 return (error);
404 }
405 /* Drain IO to see if we're done. */
406 bufobj_wwait(bo, 0, 0);
407 /*
408 * Block devices associated with filesystems may have new I/O
409 * requests posted for them even if the vnode is locked, so no
410 * amount of trying will get them clean. We make several passes
411 * as a best effort.
412 *
413 * Regular files may need multiple passes to flush all dependency
414 * work as it is possible that we must write once per indirect
415 * level, once for the leaf, and once for the inode and each of
416 * these will be done with one sync and one async pass.
417 */
418 if (bo->bo_dirty.bv_cnt > 0) {
419 if ((flags & DATA_ONLY) == 0) {
420 still_dirty = true;
421 } else {
422 /*
423 * For data-only sync, dirty indirect buffers
424 * are ignored.
425 */
426 still_dirty = false;
427 TAILQ_FOREACH(bp, &bo->bo_dirty.bv_hd, b_bobufs) {
428 if (bp->b_lblkno > -UFS_NDADDR) {
429 still_dirty = true;
430 break;
431 }
432 }
433 }
434
435 if (still_dirty) {
436 /* Write the inode after sync passes to flush deps. */
437 if (wait && DOINGSOFTDEP(vp) &&
438 (flags & NO_INO_UPDT) == 0) {
439 BO_UNLOCK(bo);
440 ffs_update(vp, 1);
441 BO_LOCK(bo);
442 }
443 /* switch between sync/async. */
444 wait = !wait;
445 if (wait || ++passes < UFS_NIADDR + 2)
446 goto loop;
447 }
448 }
449 BO_UNLOCK(bo);
450 error = 0;
451 if ((flags & DATA_ONLY) == 0) {
452 if ((flags & NO_INO_UPDT) == 0)
453 error = ffs_update(vp, 1);
454 if (DOINGSUJ(vp))
455 softdep_journal_fsync(VTOI(vp));
456 } else if ((ip->i_flags & (IN_SIZEMOD | IN_IBLKDATA)) != 0) {
457 error = ffs_update(vp, 1);
458 }
459 if (error == 0 && unlocked)
460 error = ERELOOKUP;
461 if (error == 0)
462 ip->i_flag &= ~IN_NEEDSYNC;
463 return (error);
464 }
465
466 static int
ffs_fdatasync(struct vop_fdatasync_args * ap)467 ffs_fdatasync(struct vop_fdatasync_args *ap)
468 {
469
470 return (ffs_syncvnode(ap->a_vp, MNT_WAIT, DATA_ONLY));
471 }
472
473 static int
ffs_lock(ap)474 ffs_lock(ap)
475 struct vop_lock1_args /* {
476 struct vnode *a_vp;
477 int a_flags;
478 char *file;
479 int line;
480 } */ *ap;
481 {
482 #if !defined(NO_FFS_SNAPSHOT) || defined(DIAGNOSTIC)
483 struct vnode *vp = ap->a_vp;
484 #endif /* !NO_FFS_SNAPSHOT || DIAGNOSTIC */
485 #ifdef DIAGNOSTIC
486 struct inode *ip;
487 #endif /* DIAGNOSTIC */
488 int result;
489 #ifndef NO_FFS_SNAPSHOT
490 int flags;
491 struct lock *lkp;
492
493 /*
494 * Adaptive spinning mixed with SU leads to trouble. use a giant hammer
495 * and only use it when LK_NODDLKTREAT is set. Currently this means it
496 * is only used during path lookup.
497 */
498 if ((ap->a_flags & LK_NODDLKTREAT) != 0)
499 ap->a_flags |= LK_ADAPTIVE;
500 switch (ap->a_flags & LK_TYPE_MASK) {
501 case LK_SHARED:
502 case LK_UPGRADE:
503 case LK_EXCLUSIVE:
504 flags = ap->a_flags;
505 for (;;) {
506 #ifdef DEBUG_VFS_LOCKS
507 VNPASS(vp->v_holdcnt != 0, vp);
508 #endif /* DEBUG_VFS_LOCKS */
509 lkp = vp->v_vnlock;
510 result = lockmgr_lock_flags(lkp, flags,
511 &VI_MTX(vp)->lock_object, ap->a_file, ap->a_line);
512 if (lkp == vp->v_vnlock || result != 0)
513 break;
514 /*
515 * Apparent success, except that the vnode
516 * mutated between snapshot file vnode and
517 * regular file vnode while this process
518 * slept. The lock currently held is not the
519 * right lock. Release it, and try to get the
520 * new lock.
521 */
522 lockmgr_unlock(lkp);
523 if ((flags & (LK_INTERLOCK | LK_NOWAIT)) ==
524 (LK_INTERLOCK | LK_NOWAIT))
525 return (EBUSY);
526 if ((flags & LK_TYPE_MASK) == LK_UPGRADE)
527 flags = (flags & ~LK_TYPE_MASK) | LK_EXCLUSIVE;
528 flags &= ~LK_INTERLOCK;
529 }
530 #ifdef DIAGNOSTIC
531 switch (ap->a_flags & LK_TYPE_MASK) {
532 case LK_UPGRADE:
533 case LK_EXCLUSIVE:
534 if (result == 0 && vp->v_vnlock->lk_recurse == 0) {
535 ip = VTOI(vp);
536 if (ip != NULL)
537 ip->i_lock_gen++;
538 }
539 }
540 #endif /* DIAGNOSTIC */
541 break;
542 default:
543 #ifdef DIAGNOSTIC
544 if ((ap->a_flags & LK_TYPE_MASK) == LK_DOWNGRADE) {
545 ip = VTOI(vp);
546 if (ip != NULL)
547 ufs_unlock_tracker(ip);
548 }
549 #endif /* DIAGNOSTIC */
550 result = VOP_LOCK1_APV(&ufs_vnodeops, ap);
551 break;
552 }
553 #else /* NO_FFS_SNAPSHOT */
554 /*
555 * See above for an explanation.
556 */
557 if ((ap->a_flags & LK_NODDLKTREAT) != 0)
558 ap->a_flags |= LK_ADAPTIVE;
559 #ifdef DIAGNOSTIC
560 if ((ap->a_flags & LK_TYPE_MASK) == LK_DOWNGRADE) {
561 ip = VTOI(vp);
562 if (ip != NULL)
563 ufs_unlock_tracker(ip);
564 }
565 #endif /* DIAGNOSTIC */
566 result = VOP_LOCK1_APV(&ufs_vnodeops, ap);
567 #endif /* NO_FFS_SNAPSHOT */
568 #ifdef DIAGNOSTIC
569 switch (ap->a_flags & LK_TYPE_MASK) {
570 case LK_UPGRADE:
571 case LK_EXCLUSIVE:
572 if (result == 0 && vp->v_vnlock->lk_recurse == 0) {
573 ip = VTOI(vp);
574 if (ip != NULL)
575 ip->i_lock_gen++;
576 }
577 }
578 #endif /* DIAGNOSTIC */
579 return (result);
580 }
581
582 #ifdef INVARIANTS
583 static int
ffs_unlock_debug(struct vop_unlock_args * ap)584 ffs_unlock_debug(struct vop_unlock_args *ap)
585 {
586 struct vnode *vp;
587 struct inode *ip;
588
589 vp = ap->a_vp;
590 ip = VTOI(vp);
591 if (ip->i_flag & UFS_INODE_FLAG_LAZY_MASK_ASSERTABLE) {
592 if ((vp->v_mflag & VMP_LAZYLIST) == 0) {
593 VI_LOCK(vp);
594 VNASSERT((vp->v_mflag & VMP_LAZYLIST), vp,
595 ("%s: modified vnode (%x) not on lazy list",
596 __func__, ip->i_flag));
597 VI_UNLOCK(vp);
598 }
599 }
600 KASSERT(vp->v_type != VDIR || vp->v_vnlock->lk_recurse != 0 ||
601 (ip->i_flag & IN_ENDOFF) == 0,
602 ("ufs dir vp %p ip %p flags %#x", vp, ip, ip->i_flag));
603 #ifdef DIAGNOSTIC
604 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE && ip != NULL &&
605 vp->v_vnlock->lk_recurse == 0)
606 ufs_unlock_tracker(ip);
607 #endif
608 return (VOP_UNLOCK_APV(&ufs_vnodeops, ap));
609 }
610 #endif
611
612 static int
ffs_read_hole(struct uio * uio,long xfersize,long * size)613 ffs_read_hole(struct uio *uio, long xfersize, long *size)
614 {
615 ssize_t saved_resid, tlen;
616 int error;
617
618 while (xfersize > 0) {
619 tlen = min(xfersize, ZERO_REGION_SIZE);
620 saved_resid = uio->uio_resid;
621 error = vn_io_fault_uiomove(__DECONST(void *, zero_region),
622 tlen, uio);
623 if (error != 0)
624 return (error);
625 tlen = saved_resid - uio->uio_resid;
626 xfersize -= tlen;
627 *size -= tlen;
628 }
629 return (0);
630 }
631
632 /*
633 * Vnode op for reading.
634 */
635 static int
ffs_read(ap)636 ffs_read(ap)
637 struct vop_read_args /* {
638 struct vnode *a_vp;
639 struct uio *a_uio;
640 int a_ioflag;
641 struct ucred *a_cred;
642 } */ *ap;
643 {
644 struct vnode *vp;
645 struct inode *ip;
646 struct uio *uio;
647 struct fs *fs;
648 struct buf *bp;
649 ufs_lbn_t lbn, nextlbn;
650 off_t bytesinfile;
651 long size, xfersize, blkoffset;
652 ssize_t orig_resid;
653 int bflag, error, ioflag, seqcount;
654
655 vp = ap->a_vp;
656 uio = ap->a_uio;
657 ioflag = ap->a_ioflag;
658 if (ap->a_ioflag & IO_EXT)
659 #ifdef notyet
660 return (ffs_extread(vp, uio, ioflag));
661 #else
662 panic("ffs_read+IO_EXT");
663 #endif
664 #ifdef DIRECTIO
665 if ((ioflag & IO_DIRECT) != 0) {
666 int workdone;
667
668 error = ffs_rawread(vp, uio, &workdone);
669 if (error != 0 || workdone != 0)
670 return error;
671 }
672 #endif
673
674 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
675 ip = VTOI(vp);
676
677 #ifdef INVARIANTS
678 if (uio->uio_rw != UIO_READ)
679 panic("ffs_read: mode");
680
681 if (vp->v_type == VLNK) {
682 if ((int)ip->i_size < VFSTOUFS(vp->v_mount)->um_maxsymlinklen)
683 panic("ffs_read: short symlink");
684 } else if (vp->v_type != VREG && vp->v_type != VDIR)
685 panic("ffs_read: type %d", vp->v_type);
686 #endif
687 orig_resid = uio->uio_resid;
688 KASSERT(orig_resid >= 0, ("ffs_read: uio->uio_resid < 0"));
689 if (orig_resid == 0)
690 return (0);
691 KASSERT(uio->uio_offset >= 0, ("ffs_read: uio->uio_offset < 0"));
692 fs = ITOFS(ip);
693 if (uio->uio_offset < ip->i_size &&
694 uio->uio_offset >= fs->fs_maxfilesize)
695 return (EOVERFLOW);
696
697 bflag = GB_UNMAPPED | (uio->uio_segflg == UIO_NOCOPY ? 0 : GB_NOSPARSE);
698 #ifdef WITNESS
699 bflag |= IS_SNAPSHOT(ip) ? GB_NOWITNESS : 0;
700 #endif
701 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
702 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
703 break;
704 lbn = lblkno(fs, uio->uio_offset);
705 nextlbn = lbn + 1;
706
707 /*
708 * size of buffer. The buffer representing the
709 * end of the file is rounded up to the size of
710 * the block type ( fragment or full block,
711 * depending ).
712 */
713 size = blksize(fs, ip, lbn);
714 blkoffset = blkoff(fs, uio->uio_offset);
715
716 /*
717 * The amount we want to transfer in this iteration is
718 * one FS block less the amount of the data before
719 * our startpoint (duh!)
720 */
721 xfersize = fs->fs_bsize - blkoffset;
722
723 /*
724 * But if we actually want less than the block,
725 * or the file doesn't have a whole block more of data,
726 * then use the lesser number.
727 */
728 if (uio->uio_resid < xfersize)
729 xfersize = uio->uio_resid;
730 if (bytesinfile < xfersize)
731 xfersize = bytesinfile;
732
733 if (lblktosize(fs, nextlbn) >= ip->i_size) {
734 /*
735 * Don't do readahead if this is the end of the file.
736 */
737 error = bread_gb(vp, lbn, size, NOCRED, bflag, &bp);
738 } else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
739 /*
740 * Otherwise if we are allowed to cluster,
741 * grab as much as we can.
742 *
743 * XXX This may not be a win if we are not
744 * doing sequential access.
745 */
746 error = cluster_read(vp, ip->i_size, lbn,
747 size, NOCRED, blkoffset + uio->uio_resid,
748 seqcount, bflag, &bp);
749 } else if (seqcount > 1) {
750 /*
751 * If we are NOT allowed to cluster, then
752 * if we appear to be acting sequentially,
753 * fire off a request for a readahead
754 * as well as a read. Note that the 4th and 5th
755 * arguments point to arrays of the size specified in
756 * the 6th argument.
757 */
758 u_int nextsize = blksize(fs, ip, nextlbn);
759 error = breadn_flags(vp, lbn, lbn, size, &nextlbn,
760 &nextsize, 1, NOCRED, bflag, NULL, &bp);
761 } else {
762 /*
763 * Failing all of the above, just read what the
764 * user asked for. Interestingly, the same as
765 * the first option above.
766 */
767 error = bread_gb(vp, lbn, size, NOCRED, bflag, &bp);
768 }
769 if (error == EJUSTRETURN) {
770 error = ffs_read_hole(uio, xfersize, &size);
771 if (error == 0)
772 continue;
773 }
774 if (error != 0) {
775 brelse(bp);
776 bp = NULL;
777 break;
778 }
779
780 /*
781 * We should only get non-zero b_resid when an I/O error
782 * has occurred, which should cause us to break above.
783 * However, if the short read did not cause an error,
784 * then we want to ensure that we do not uiomove bad
785 * or uninitialized data.
786 */
787 size -= bp->b_resid;
788 if (size < xfersize) {
789 if (size == 0)
790 break;
791 xfersize = size;
792 }
793
794 if (buf_mapped(bp)) {
795 error = vn_io_fault_uiomove((char *)bp->b_data +
796 blkoffset, (int)xfersize, uio);
797 } else {
798 error = vn_io_fault_pgmove(bp->b_pages, blkoffset,
799 (int)xfersize, uio);
800 }
801 if (error)
802 break;
803
804 vfs_bio_brelse(bp, ioflag);
805 }
806
807 /*
808 * This can only happen in the case of an error
809 * because the loop above resets bp to NULL on each iteration
810 * and on normal completion has not set a new value into it.
811 * so it must have come from a 'break' statement
812 */
813 if (bp != NULL)
814 vfs_bio_brelse(bp, ioflag);
815
816 if ((error == 0 || uio->uio_resid != orig_resid) &&
817 (vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
818 UFS_INODE_SET_FLAG_SHARED(ip, IN_ACCESS);
819 return (error);
820 }
821
822 /*
823 * Vnode op for writing.
824 */
825 static int
ffs_write(ap)826 ffs_write(ap)
827 struct vop_write_args /* {
828 struct vnode *a_vp;
829 struct uio *a_uio;
830 int a_ioflag;
831 struct ucred *a_cred;
832 } */ *ap;
833 {
834 struct vnode *vp;
835 struct uio *uio;
836 struct inode *ip;
837 struct fs *fs;
838 struct buf *bp;
839 ufs_lbn_t lbn;
840 off_t osize;
841 ssize_t resid;
842 int seqcount;
843 int blkoffset, error, flags, ioflag, size, xfersize;
844
845 vp = ap->a_vp;
846 if (DOINGSUJ(vp))
847 softdep_prealloc(vp, MNT_WAIT);
848 if (vp->v_data == NULL)
849 return (EBADF);
850
851 uio = ap->a_uio;
852 ioflag = ap->a_ioflag;
853 if (ap->a_ioflag & IO_EXT)
854 #ifdef notyet
855 return (ffs_extwrite(vp, uio, ioflag, ap->a_cred));
856 #else
857 panic("ffs_write+IO_EXT");
858 #endif
859
860 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
861 ip = VTOI(vp);
862
863 #ifdef INVARIANTS
864 if (uio->uio_rw != UIO_WRITE)
865 panic("ffs_write: mode");
866 #endif
867
868 switch (vp->v_type) {
869 case VREG:
870 if (ioflag & IO_APPEND)
871 uio->uio_offset = ip->i_size;
872 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size)
873 return (EPERM);
874 /* FALLTHROUGH */
875 case VLNK:
876 break;
877 case VDIR:
878 panic("ffs_write: dir write");
879 break;
880 default:
881 panic("ffs_write: type %p %d (%d,%d)", vp, (int)vp->v_type,
882 (int)uio->uio_offset,
883 (int)uio->uio_resid
884 );
885 }
886
887 KASSERT(uio->uio_resid >= 0, ("ffs_write: uio->uio_resid < 0"));
888 KASSERT(uio->uio_offset >= 0, ("ffs_write: uio->uio_offset < 0"));
889 fs = ITOFS(ip);
890 if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->fs_maxfilesize)
891 return (EFBIG);
892 /*
893 * Maybe this should be above the vnode op call, but so long as
894 * file servers have no limits, I don't think it matters.
895 */
896 if (vn_rlimit_fsize(vp, uio, uio->uio_td))
897 return (EFBIG);
898
899 resid = uio->uio_resid;
900 osize = ip->i_size;
901 if (seqcount > BA_SEQMAX)
902 flags = BA_SEQMAX << BA_SEQSHIFT;
903 else
904 flags = seqcount << BA_SEQSHIFT;
905 if (ioflag & IO_SYNC)
906 flags |= IO_SYNC;
907 flags |= BA_UNMAPPED;
908
909 for (error = 0; uio->uio_resid > 0;) {
910 lbn = lblkno(fs, uio->uio_offset);
911 blkoffset = blkoff(fs, uio->uio_offset);
912 xfersize = fs->fs_bsize - blkoffset;
913 if (uio->uio_resid < xfersize)
914 xfersize = uio->uio_resid;
915 if (uio->uio_offset + xfersize > ip->i_size)
916 vnode_pager_setsize(vp, uio->uio_offset + xfersize);
917
918 /*
919 * We must perform a read-before-write if the transfer size
920 * does not cover the entire buffer.
921 */
922 if (fs->fs_bsize > xfersize)
923 flags |= BA_CLRBUF;
924 else
925 flags &= ~BA_CLRBUF;
926 /* XXX is uio->uio_offset the right thing here? */
927 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
928 ap->a_cred, flags, &bp);
929 if (error != 0) {
930 vnode_pager_setsize(vp, ip->i_size);
931 break;
932 }
933 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL))
934 bp->b_flags |= B_NOCACHE;
935
936 if (uio->uio_offset + xfersize > ip->i_size) {
937 ip->i_size = uio->uio_offset + xfersize;
938 DIP_SET(ip, i_size, ip->i_size);
939 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
940 }
941
942 size = blksize(fs, ip, lbn) - bp->b_resid;
943 if (size < xfersize)
944 xfersize = size;
945
946 if (buf_mapped(bp)) {
947 error = vn_io_fault_uiomove((char *)bp->b_data +
948 blkoffset, (int)xfersize, uio);
949 } else {
950 error = vn_io_fault_pgmove(bp->b_pages, blkoffset,
951 (int)xfersize, uio);
952 }
953 /*
954 * If the buffer is not already filled and we encounter an
955 * error while trying to fill it, we have to clear out any
956 * garbage data from the pages instantiated for the buffer.
957 * If we do not, a failed uiomove() during a write can leave
958 * the prior contents of the pages exposed to a userland mmap.
959 *
960 * Note that we need only clear buffers with a transfer size
961 * equal to the block size because buffers with a shorter
962 * transfer size were cleared above by the call to UFS_BALLOC()
963 * with the BA_CLRBUF flag set.
964 *
965 * If the source region for uiomove identically mmaps the
966 * buffer, uiomove() performed the NOP copy, and the buffer
967 * content remains valid because the page fault handler
968 * validated the pages.
969 */
970 if (error != 0 && (bp->b_flags & B_CACHE) == 0 &&
971 fs->fs_bsize == xfersize)
972 vfs_bio_clrbuf(bp);
973
974 vfs_bio_set_flags(bp, ioflag);
975
976 /*
977 * If IO_SYNC each buffer is written synchronously. Otherwise
978 * if we have a severe page deficiency write the buffer
979 * asynchronously. Otherwise try to cluster, and if that
980 * doesn't do it then either do an async write (if O_DIRECT),
981 * or a delayed write (if not).
982 */
983 if (ioflag & IO_SYNC) {
984 (void)bwrite(bp);
985 } else if (vm_page_count_severe() ||
986 buf_dirty_count_severe() ||
987 (ioflag & IO_ASYNC)) {
988 bp->b_flags |= B_CLUSTEROK;
989 bawrite(bp);
990 } else if (xfersize + blkoffset == fs->fs_bsize) {
991 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) {
992 bp->b_flags |= B_CLUSTEROK;
993 cluster_write(vp, bp, ip->i_size, seqcount,
994 GB_UNMAPPED);
995 } else {
996 bawrite(bp);
997 }
998 } else if (ioflag & IO_DIRECT) {
999 bp->b_flags |= B_CLUSTEROK;
1000 bawrite(bp);
1001 } else {
1002 bp->b_flags |= B_CLUSTEROK;
1003 bdwrite(bp);
1004 }
1005 if (error || xfersize == 0)
1006 break;
1007 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
1008 }
1009 /*
1010 * If we successfully wrote any data, and we are not the superuser
1011 * we clear the setuid and setgid bits as a precaution against
1012 * tampering.
1013 */
1014 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid &&
1015 ap->a_cred) {
1016 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID)) {
1017 vn_seqc_write_begin(vp);
1018 UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID));
1019 DIP_SET(ip, i_mode, ip->i_mode);
1020 vn_seqc_write_end(vp);
1021 }
1022 }
1023 if (error) {
1024 if (ioflag & IO_UNIT) {
1025 (void)ffs_truncate(vp, osize,
1026 IO_NORMAL | (ioflag & IO_SYNC), ap->a_cred);
1027 uio->uio_offset -= resid - uio->uio_resid;
1028 uio->uio_resid = resid;
1029 }
1030 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC)) {
1031 if (!(ioflag & IO_DATASYNC) ||
1032 (ip->i_flags & (IN_SIZEMOD | IN_IBLKDATA)))
1033 error = ffs_update(vp, 1);
1034 if (ffs_fsfail_cleanup(VFSTOUFS(vp->v_mount), error))
1035 error = ENXIO;
1036 }
1037 return (error);
1038 }
1039
1040 /*
1041 * Extended attribute area reading.
1042 */
1043 static int
ffs_extread(struct vnode * vp,struct uio * uio,int ioflag)1044 ffs_extread(struct vnode *vp, struct uio *uio, int ioflag)
1045 {
1046 struct inode *ip;
1047 struct ufs2_dinode *dp;
1048 struct fs *fs;
1049 struct buf *bp;
1050 ufs_lbn_t lbn, nextlbn;
1051 off_t bytesinfile;
1052 long size, xfersize, blkoffset;
1053 ssize_t orig_resid;
1054 int error;
1055
1056 ip = VTOI(vp);
1057 fs = ITOFS(ip);
1058 dp = ip->i_din2;
1059
1060 #ifdef INVARIANTS
1061 if (uio->uio_rw != UIO_READ || fs->fs_magic != FS_UFS2_MAGIC)
1062 panic("ffs_extread: mode");
1063
1064 #endif
1065 orig_resid = uio->uio_resid;
1066 KASSERT(orig_resid >= 0, ("ffs_extread: uio->uio_resid < 0"));
1067 if (orig_resid == 0)
1068 return (0);
1069 KASSERT(uio->uio_offset >= 0, ("ffs_extread: uio->uio_offset < 0"));
1070
1071 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
1072 if ((bytesinfile = dp->di_extsize - uio->uio_offset) <= 0)
1073 break;
1074 lbn = lblkno(fs, uio->uio_offset);
1075 nextlbn = lbn + 1;
1076
1077 /*
1078 * size of buffer. The buffer representing the
1079 * end of the file is rounded up to the size of
1080 * the block type ( fragment or full block,
1081 * depending ).
1082 */
1083 size = sblksize(fs, dp->di_extsize, lbn);
1084 blkoffset = blkoff(fs, uio->uio_offset);
1085
1086 /*
1087 * The amount we want to transfer in this iteration is
1088 * one FS block less the amount of the data before
1089 * our startpoint (duh!)
1090 */
1091 xfersize = fs->fs_bsize - blkoffset;
1092
1093 /*
1094 * But if we actually want less than the block,
1095 * or the file doesn't have a whole block more of data,
1096 * then use the lesser number.
1097 */
1098 if (uio->uio_resid < xfersize)
1099 xfersize = uio->uio_resid;
1100 if (bytesinfile < xfersize)
1101 xfersize = bytesinfile;
1102
1103 if (lblktosize(fs, nextlbn) >= dp->di_extsize) {
1104 /*
1105 * Don't do readahead if this is the end of the info.
1106 */
1107 error = bread(vp, -1 - lbn, size, NOCRED, &bp);
1108 } else {
1109 /*
1110 * If we have a second block, then
1111 * fire off a request for a readahead
1112 * as well as a read. Note that the 4th and 5th
1113 * arguments point to arrays of the size specified in
1114 * the 6th argument.
1115 */
1116 u_int nextsize = sblksize(fs, dp->di_extsize, nextlbn);
1117
1118 nextlbn = -1 - nextlbn;
1119 error = breadn(vp, -1 - lbn,
1120 size, &nextlbn, &nextsize, 1, NOCRED, &bp);
1121 }
1122 if (error) {
1123 brelse(bp);
1124 bp = NULL;
1125 break;
1126 }
1127
1128 /*
1129 * We should only get non-zero b_resid when an I/O error
1130 * has occurred, which should cause us to break above.
1131 * However, if the short read did not cause an error,
1132 * then we want to ensure that we do not uiomove bad
1133 * or uninitialized data.
1134 */
1135 size -= bp->b_resid;
1136 if (size < xfersize) {
1137 if (size == 0)
1138 break;
1139 xfersize = size;
1140 }
1141
1142 error = uiomove((char *)bp->b_data + blkoffset,
1143 (int)xfersize, uio);
1144 if (error)
1145 break;
1146 vfs_bio_brelse(bp, ioflag);
1147 }
1148
1149 /*
1150 * This can only happen in the case of an error
1151 * because the loop above resets bp to NULL on each iteration
1152 * and on normal completion has not set a new value into it.
1153 * so it must have come from a 'break' statement
1154 */
1155 if (bp != NULL)
1156 vfs_bio_brelse(bp, ioflag);
1157 return (error);
1158 }
1159
1160 /*
1161 * Extended attribute area writing.
1162 */
1163 static int
ffs_extwrite(struct vnode * vp,struct uio * uio,int ioflag,struct ucred * ucred)1164 ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag, struct ucred *ucred)
1165 {
1166 struct inode *ip;
1167 struct ufs2_dinode *dp;
1168 struct fs *fs;
1169 struct buf *bp;
1170 ufs_lbn_t lbn;
1171 off_t osize;
1172 ssize_t resid;
1173 int blkoffset, error, flags, size, xfersize;
1174
1175 ip = VTOI(vp);
1176 fs = ITOFS(ip);
1177 dp = ip->i_din2;
1178
1179 #ifdef INVARIANTS
1180 if (uio->uio_rw != UIO_WRITE || fs->fs_magic != FS_UFS2_MAGIC)
1181 panic("ffs_extwrite: mode");
1182 #endif
1183
1184 if (ioflag & IO_APPEND)
1185 uio->uio_offset = dp->di_extsize;
1186 KASSERT(uio->uio_offset >= 0, ("ffs_extwrite: uio->uio_offset < 0"));
1187 KASSERT(uio->uio_resid >= 0, ("ffs_extwrite: uio->uio_resid < 0"));
1188 if ((uoff_t)uio->uio_offset + uio->uio_resid >
1189 UFS_NXADDR * fs->fs_bsize)
1190 return (EFBIG);
1191
1192 resid = uio->uio_resid;
1193 osize = dp->di_extsize;
1194 flags = IO_EXT;
1195 if (ioflag & IO_SYNC)
1196 flags |= IO_SYNC;
1197
1198 for (error = 0; uio->uio_resid > 0;) {
1199 lbn = lblkno(fs, uio->uio_offset);
1200 blkoffset = blkoff(fs, uio->uio_offset);
1201 xfersize = fs->fs_bsize - blkoffset;
1202 if (uio->uio_resid < xfersize)
1203 xfersize = uio->uio_resid;
1204
1205 /*
1206 * We must perform a read-before-write if the transfer size
1207 * does not cover the entire buffer.
1208 */
1209 if (fs->fs_bsize > xfersize)
1210 flags |= BA_CLRBUF;
1211 else
1212 flags &= ~BA_CLRBUF;
1213 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
1214 ucred, flags, &bp);
1215 if (error != 0)
1216 break;
1217 /*
1218 * If the buffer is not valid we have to clear out any
1219 * garbage data from the pages instantiated for the buffer.
1220 * If we do not, a failed uiomove() during a write can leave
1221 * the prior contents of the pages exposed to a userland
1222 * mmap(). XXX deal with uiomove() errors a better way.
1223 */
1224 if ((bp->b_flags & B_CACHE) == 0 && fs->fs_bsize <= xfersize)
1225 vfs_bio_clrbuf(bp);
1226
1227 if (uio->uio_offset + xfersize > dp->di_extsize) {
1228 dp->di_extsize = uio->uio_offset + xfersize;
1229 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE);
1230 }
1231
1232 size = sblksize(fs, dp->di_extsize, lbn) - bp->b_resid;
1233 if (size < xfersize)
1234 xfersize = size;
1235
1236 error =
1237 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
1238
1239 vfs_bio_set_flags(bp, ioflag);
1240
1241 /*
1242 * If IO_SYNC each buffer is written synchronously. Otherwise
1243 * if we have a severe page deficiency write the buffer
1244 * asynchronously. Otherwise try to cluster, and if that
1245 * doesn't do it then either do an async write (if O_DIRECT),
1246 * or a delayed write (if not).
1247 */
1248 if (ioflag & IO_SYNC) {
1249 (void)bwrite(bp);
1250 } else if (vm_page_count_severe() ||
1251 buf_dirty_count_severe() ||
1252 xfersize + blkoffset == fs->fs_bsize ||
1253 (ioflag & (IO_ASYNC | IO_DIRECT)))
1254 bawrite(bp);
1255 else
1256 bdwrite(bp);
1257 if (error || xfersize == 0)
1258 break;
1259 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1260 }
1261 /*
1262 * If we successfully wrote any data, and we are not the superuser
1263 * we clear the setuid and setgid bits as a precaution against
1264 * tampering.
1265 */
1266 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid && ucred) {
1267 if (priv_check_cred(ucred, PRIV_VFS_RETAINSUGID)) {
1268 vn_seqc_write_begin(vp);
1269 UFS_INODE_SET_MODE(ip, ip->i_mode & ~(ISUID | ISGID));
1270 dp->di_mode = ip->i_mode;
1271 vn_seqc_write_end(vp);
1272 }
1273 }
1274 if (error) {
1275 if (ioflag & IO_UNIT) {
1276 (void)ffs_truncate(vp, osize,
1277 IO_EXT | (ioflag&IO_SYNC), ucred);
1278 uio->uio_offset -= resid - uio->uio_resid;
1279 uio->uio_resid = resid;
1280 }
1281 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
1282 error = ffs_update(vp, 1);
1283 return (error);
1284 }
1285
1286 /*
1287 * Vnode operating to retrieve a named extended attribute.
1288 *
1289 * Locate a particular EA (nspace:name) in the area (ptr:length), and return
1290 * the length of the EA, and possibly the pointer to the entry and to the data.
1291 */
1292 static int
ffs_findextattr(u_char * ptr,u_int length,int nspace,const char * name,struct extattr ** eapp,u_char ** eac)1293 ffs_findextattr(u_char *ptr, u_int length, int nspace, const char *name,
1294 struct extattr **eapp, u_char **eac)
1295 {
1296 struct extattr *eap, *eaend;
1297 size_t nlen;
1298
1299 nlen = strlen(name);
1300 KASSERT(ALIGNED_TO(ptr, struct extattr), ("unaligned"));
1301 eap = (struct extattr *)ptr;
1302 eaend = (struct extattr *)(ptr + length);
1303 for (; eap < eaend; eap = EXTATTR_NEXT(eap)) {
1304 KASSERT(EXTATTR_NEXT(eap) <= eaend,
1305 ("extattr next %p beyond %p", EXTATTR_NEXT(eap), eaend));
1306 if (eap->ea_namespace != nspace || eap->ea_namelength != nlen
1307 || memcmp(eap->ea_name, name, nlen) != 0)
1308 continue;
1309 if (eapp != NULL)
1310 *eapp = eap;
1311 if (eac != NULL)
1312 *eac = EXTATTR_CONTENT(eap);
1313 return (EXTATTR_CONTENT_SIZE(eap));
1314 }
1315 return (-1);
1316 }
1317
1318 static int
ffs_rdextattr(u_char ** p,struct vnode * vp,struct thread * td)1319 ffs_rdextattr(u_char **p, struct vnode *vp, struct thread *td)
1320 {
1321 const struct extattr *eap, *eaend, *eapnext;
1322 struct inode *ip;
1323 struct ufs2_dinode *dp;
1324 struct fs *fs;
1325 struct uio luio;
1326 struct iovec liovec;
1327 u_int easize;
1328 int error;
1329 u_char *eae;
1330
1331 ip = VTOI(vp);
1332 fs = ITOFS(ip);
1333 dp = ip->i_din2;
1334 easize = dp->di_extsize;
1335 if ((uoff_t)easize > UFS_NXADDR * fs->fs_bsize)
1336 return (EFBIG);
1337
1338 eae = malloc(easize, M_TEMP, M_WAITOK);
1339
1340 liovec.iov_base = eae;
1341 liovec.iov_len = easize;
1342 luio.uio_iov = &liovec;
1343 luio.uio_iovcnt = 1;
1344 luio.uio_offset = 0;
1345 luio.uio_resid = easize;
1346 luio.uio_segflg = UIO_SYSSPACE;
1347 luio.uio_rw = UIO_READ;
1348 luio.uio_td = td;
1349
1350 error = ffs_extread(vp, &luio, IO_EXT | IO_SYNC);
1351 if (error) {
1352 free(eae, M_TEMP);
1353 return (error);
1354 }
1355 /* Validate disk xattrfile contents. */
1356 for (eap = (void *)eae, eaend = (void *)(eae + easize); eap < eaend;
1357 eap = eapnext) {
1358 /* Detect zeroed out tail */
1359 if (eap->ea_length < sizeof(*eap) || eap->ea_length == 0) {
1360 easize = (const u_char *)eap - eae;
1361 break;
1362 }
1363
1364 eapnext = EXTATTR_NEXT(eap);
1365 /* Bogusly long entry. */
1366 if (eapnext > eaend) {
1367 free(eae, M_TEMP);
1368 return (EINTEGRITY);
1369 }
1370 }
1371 ip->i_ea_len = easize;
1372 *p = eae;
1373 return (0);
1374 }
1375
1376 static void
ffs_lock_ea(struct vnode * vp)1377 ffs_lock_ea(struct vnode *vp)
1378 {
1379 struct inode *ip;
1380
1381 ip = VTOI(vp);
1382 VI_LOCK(vp);
1383 while (ip->i_flag & IN_EA_LOCKED) {
1384 UFS_INODE_SET_FLAG(ip, IN_EA_LOCKWAIT);
1385 msleep(&ip->i_ea_refs, &vp->v_interlock, PINOD + 2, "ufs_ea",
1386 0);
1387 }
1388 UFS_INODE_SET_FLAG(ip, IN_EA_LOCKED);
1389 VI_UNLOCK(vp);
1390 }
1391
1392 static void
ffs_unlock_ea(struct vnode * vp)1393 ffs_unlock_ea(struct vnode *vp)
1394 {
1395 struct inode *ip;
1396
1397 ip = VTOI(vp);
1398 VI_LOCK(vp);
1399 if (ip->i_flag & IN_EA_LOCKWAIT)
1400 wakeup(&ip->i_ea_refs);
1401 ip->i_flag &= ~(IN_EA_LOCKED | IN_EA_LOCKWAIT);
1402 VI_UNLOCK(vp);
1403 }
1404
1405 static int
ffs_open_ea(struct vnode * vp,struct ucred * cred,struct thread * td)1406 ffs_open_ea(struct vnode *vp, struct ucred *cred, struct thread *td)
1407 {
1408 struct inode *ip;
1409 int error;
1410
1411 ip = VTOI(vp);
1412
1413 ffs_lock_ea(vp);
1414 if (ip->i_ea_area != NULL) {
1415 ip->i_ea_refs++;
1416 ffs_unlock_ea(vp);
1417 return (0);
1418 }
1419 error = ffs_rdextattr(&ip->i_ea_area, vp, td);
1420 if (error) {
1421 ffs_unlock_ea(vp);
1422 return (error);
1423 }
1424 ip->i_ea_error = 0;
1425 ip->i_ea_refs++;
1426 ffs_unlock_ea(vp);
1427 return (0);
1428 }
1429
1430 /*
1431 * Vnode extattr transaction commit/abort
1432 */
1433 static int
ffs_close_ea(struct vnode * vp,int commit,struct ucred * cred,struct thread * td)1434 ffs_close_ea(struct vnode *vp, int commit, struct ucred *cred, struct thread *td)
1435 {
1436 struct inode *ip;
1437 struct uio luio;
1438 struct iovec *liovec;
1439 struct ufs2_dinode *dp;
1440 size_t ea_len, tlen;
1441 int error, i, lcnt;
1442 bool truncate;
1443
1444 ip = VTOI(vp);
1445
1446 ffs_lock_ea(vp);
1447 if (ip->i_ea_area == NULL) {
1448 ffs_unlock_ea(vp);
1449 return (EINVAL);
1450 }
1451 dp = ip->i_din2;
1452 error = ip->i_ea_error;
1453 truncate = false;
1454 if (commit && error == 0) {
1455 ASSERT_VOP_ELOCKED(vp, "ffs_close_ea commit");
1456 if (cred == NOCRED)
1457 cred = vp->v_mount->mnt_cred;
1458
1459 ea_len = MAX(ip->i_ea_len, dp->di_extsize);
1460 for (lcnt = 1, tlen = ea_len - ip->i_ea_len; tlen > 0;) {
1461 tlen -= MIN(ZERO_REGION_SIZE, tlen);
1462 lcnt++;
1463 }
1464
1465 liovec = __builtin_alloca(lcnt * sizeof(struct iovec));
1466 luio.uio_iovcnt = lcnt;
1467
1468 liovec[0].iov_base = ip->i_ea_area;
1469 liovec[0].iov_len = ip->i_ea_len;
1470 for (i = 1, tlen = ea_len - ip->i_ea_len; i < lcnt; i++) {
1471 liovec[i].iov_base = __DECONST(void *, zero_region);
1472 liovec[i].iov_len = MIN(ZERO_REGION_SIZE, tlen);
1473 tlen -= liovec[i].iov_len;
1474 }
1475 MPASS(tlen == 0);
1476
1477 luio.uio_iov = liovec;
1478 luio.uio_offset = 0;
1479 luio.uio_resid = ea_len;
1480 luio.uio_segflg = UIO_SYSSPACE;
1481 luio.uio_rw = UIO_WRITE;
1482 luio.uio_td = td;
1483 error = ffs_extwrite(vp, &luio, IO_EXT | IO_SYNC, cred);
1484 if (error == 0 && ip->i_ea_len == 0)
1485 truncate = true;
1486 }
1487 if (--ip->i_ea_refs == 0) {
1488 free(ip->i_ea_area, M_TEMP);
1489 ip->i_ea_area = NULL;
1490 ip->i_ea_len = 0;
1491 ip->i_ea_error = 0;
1492 }
1493 ffs_unlock_ea(vp);
1494
1495 if (truncate)
1496 ffs_truncate(vp, 0, IO_EXT, cred);
1497 return (error);
1498 }
1499
1500 /*
1501 * Vnode extattr strategy routine for fifos.
1502 *
1503 * We need to check for a read or write of the external attributes.
1504 * Otherwise we just fall through and do the usual thing.
1505 */
1506 static int
ffsext_strategy(struct vop_strategy_args * ap)1507 ffsext_strategy(struct vop_strategy_args *ap)
1508 /*
1509 struct vop_strategy_args {
1510 struct vnodeop_desc *a_desc;
1511 struct vnode *a_vp;
1512 struct buf *a_bp;
1513 };
1514 */
1515 {
1516 struct vnode *vp;
1517 daddr_t lbn;
1518
1519 vp = ap->a_vp;
1520 lbn = ap->a_bp->b_lblkno;
1521 if (I_IS_UFS2(VTOI(vp)) && lbn < 0 && lbn >= -UFS_NXADDR)
1522 return (VOP_STRATEGY_APV(&ufs_vnodeops, ap));
1523 if (vp->v_type == VFIFO)
1524 return (VOP_STRATEGY_APV(&ufs_fifoops, ap));
1525 panic("spec nodes went here");
1526 }
1527
1528 /*
1529 * Vnode extattr transaction commit/abort
1530 */
1531 static int
ffs_openextattr(struct vop_openextattr_args * ap)1532 ffs_openextattr(struct vop_openextattr_args *ap)
1533 /*
1534 struct vop_openextattr_args {
1535 struct vnodeop_desc *a_desc;
1536 struct vnode *a_vp;
1537 IN struct ucred *a_cred;
1538 IN struct thread *a_td;
1539 };
1540 */
1541 {
1542
1543 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1544 return (EOPNOTSUPP);
1545
1546 return (ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td));
1547 }
1548
1549 /*
1550 * Vnode extattr transaction commit/abort
1551 */
1552 static int
ffs_closeextattr(struct vop_closeextattr_args * ap)1553 ffs_closeextattr(struct vop_closeextattr_args *ap)
1554 /*
1555 struct vop_closeextattr_args {
1556 struct vnodeop_desc *a_desc;
1557 struct vnode *a_vp;
1558 int a_commit;
1559 IN struct ucred *a_cred;
1560 IN struct thread *a_td;
1561 };
1562 */
1563 {
1564 struct vnode *vp;
1565
1566 vp = ap->a_vp;
1567 if (vp->v_type == VCHR || vp->v_type == VBLK)
1568 return (EOPNOTSUPP);
1569 if (ap->a_commit && (vp->v_mount->mnt_flag & MNT_RDONLY) != 0)
1570 return (EROFS);
1571
1572 if (ap->a_commit && DOINGSUJ(vp)) {
1573 ASSERT_VOP_ELOCKED(vp, "ffs_closeextattr commit");
1574 softdep_prealloc(vp, MNT_WAIT);
1575 if (vp->v_data == NULL)
1576 return (EBADF);
1577 }
1578 return (ffs_close_ea(vp, ap->a_commit, ap->a_cred, ap->a_td));
1579 }
1580
1581 /*
1582 * Vnode operation to remove a named attribute.
1583 */
1584 static int
ffs_deleteextattr(struct vop_deleteextattr_args * ap)1585 ffs_deleteextattr(struct vop_deleteextattr_args *ap)
1586 /*
1587 vop_deleteextattr {
1588 IN struct vnode *a_vp;
1589 IN int a_attrnamespace;
1590 IN const char *a_name;
1591 IN struct ucred *a_cred;
1592 IN struct thread *a_td;
1593 };
1594 */
1595 {
1596 struct vnode *vp;
1597 struct inode *ip;
1598 struct extattr *eap;
1599 uint32_t ul;
1600 int olen, error, i, easize;
1601 u_char *eae;
1602 void *tmp;
1603
1604 vp = ap->a_vp;
1605 ip = VTOI(vp);
1606
1607 if (vp->v_type == VCHR || vp->v_type == VBLK)
1608 return (EOPNOTSUPP);
1609 if (strlen(ap->a_name) == 0)
1610 return (EINVAL);
1611 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1612 return (EROFS);
1613
1614 error = extattr_check_cred(vp, ap->a_attrnamespace,
1615 ap->a_cred, ap->a_td, VWRITE);
1616 if (error) {
1617 /*
1618 * ffs_lock_ea is not needed there, because the vnode
1619 * must be exclusively locked.
1620 */
1621 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1622 ip->i_ea_error = error;
1623 return (error);
1624 }
1625
1626 if (DOINGSUJ(vp)) {
1627 ASSERT_VOP_ELOCKED(vp, "ffs_deleteextattr");
1628 softdep_prealloc(vp, MNT_WAIT);
1629 if (vp->v_data == NULL)
1630 return (EBADF);
1631 }
1632
1633 error = ffs_open_ea(vp, ap->a_cred, ap->a_td);
1634 if (error)
1635 return (error);
1636
1637 /* CEM: delete could be done in-place instead */
1638 eae = malloc(ip->i_ea_len, M_TEMP, M_WAITOK);
1639 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1640 easize = ip->i_ea_len;
1641
1642 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1643 &eap, NULL);
1644 if (olen == -1) {
1645 /* delete but nonexistent */
1646 free(eae, M_TEMP);
1647 ffs_close_ea(vp, 0, ap->a_cred, ap->a_td);
1648 return (ENOATTR);
1649 }
1650 ul = eap->ea_length;
1651 i = (u_char *)EXTATTR_NEXT(eap) - eae;
1652 bcopy(EXTATTR_NEXT(eap), eap, easize - i);
1653 easize -= ul;
1654
1655 tmp = ip->i_ea_area;
1656 ip->i_ea_area = eae;
1657 ip->i_ea_len = easize;
1658 free(tmp, M_TEMP);
1659 error = ffs_close_ea(vp, 1, ap->a_cred, ap->a_td);
1660 return (error);
1661 }
1662
1663 /*
1664 * Vnode operation to retrieve a named extended attribute.
1665 */
1666 static int
ffs_getextattr(struct vop_getextattr_args * ap)1667 ffs_getextattr(struct vop_getextattr_args *ap)
1668 /*
1669 vop_getextattr {
1670 IN struct vnode *a_vp;
1671 IN int a_attrnamespace;
1672 IN const char *a_name;
1673 INOUT struct uio *a_uio;
1674 OUT size_t *a_size;
1675 IN struct ucred *a_cred;
1676 IN struct thread *a_td;
1677 };
1678 */
1679 {
1680 struct inode *ip;
1681 u_char *eae, *p;
1682 unsigned easize;
1683 int error, ealen;
1684
1685 ip = VTOI(ap->a_vp);
1686
1687 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1688 return (EOPNOTSUPP);
1689
1690 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1691 ap->a_cred, ap->a_td, VREAD);
1692 if (error)
1693 return (error);
1694
1695 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1696 if (error)
1697 return (error);
1698
1699 eae = ip->i_ea_area;
1700 easize = ip->i_ea_len;
1701
1702 ealen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1703 NULL, &p);
1704 if (ealen >= 0) {
1705 error = 0;
1706 if (ap->a_size != NULL)
1707 *ap->a_size = ealen;
1708 else if (ap->a_uio != NULL)
1709 error = uiomove(p, ealen, ap->a_uio);
1710 } else
1711 error = ENOATTR;
1712
1713 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1714 return (error);
1715 }
1716
1717 /*
1718 * Vnode operation to retrieve extended attributes on a vnode.
1719 */
1720 static int
ffs_listextattr(struct vop_listextattr_args * ap)1721 ffs_listextattr(struct vop_listextattr_args *ap)
1722 /*
1723 vop_listextattr {
1724 IN struct vnode *a_vp;
1725 IN int a_attrnamespace;
1726 INOUT struct uio *a_uio;
1727 OUT size_t *a_size;
1728 IN struct ucred *a_cred;
1729 IN struct thread *a_td;
1730 };
1731 */
1732 {
1733 struct inode *ip;
1734 struct extattr *eap, *eaend;
1735 int error, ealen;
1736
1737 ip = VTOI(ap->a_vp);
1738
1739 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
1740 return (EOPNOTSUPP);
1741
1742 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
1743 ap->a_cred, ap->a_td, VREAD);
1744 if (error)
1745 return (error);
1746
1747 error = ffs_open_ea(ap->a_vp, ap->a_cred, ap->a_td);
1748 if (error)
1749 return (error);
1750
1751 error = 0;
1752 if (ap->a_size != NULL)
1753 *ap->a_size = 0;
1754
1755 KASSERT(ALIGNED_TO(ip->i_ea_area, struct extattr), ("unaligned"));
1756 eap = (struct extattr *)ip->i_ea_area;
1757 eaend = (struct extattr *)(ip->i_ea_area + ip->i_ea_len);
1758 for (; error == 0 && eap < eaend; eap = EXTATTR_NEXT(eap)) {
1759 KASSERT(EXTATTR_NEXT(eap) <= eaend,
1760 ("extattr next %p beyond %p", EXTATTR_NEXT(eap), eaend));
1761 if (eap->ea_namespace != ap->a_attrnamespace)
1762 continue;
1763
1764 ealen = eap->ea_namelength;
1765 if (ap->a_size != NULL)
1766 *ap->a_size += ealen + 1;
1767 else if (ap->a_uio != NULL)
1768 error = uiomove(&eap->ea_namelength, ealen + 1,
1769 ap->a_uio);
1770 }
1771
1772 ffs_close_ea(ap->a_vp, 0, ap->a_cred, ap->a_td);
1773 return (error);
1774 }
1775
1776 /*
1777 * Vnode operation to set a named attribute.
1778 */
1779 static int
ffs_setextattr(struct vop_setextattr_args * ap)1780 ffs_setextattr(struct vop_setextattr_args *ap)
1781 /*
1782 vop_setextattr {
1783 IN struct vnode *a_vp;
1784 IN int a_attrnamespace;
1785 IN const char *a_name;
1786 INOUT struct uio *a_uio;
1787 IN struct ucred *a_cred;
1788 IN struct thread *a_td;
1789 };
1790 */
1791 {
1792 struct vnode *vp;
1793 struct inode *ip;
1794 struct fs *fs;
1795 struct extattr *eap;
1796 uint32_t ealength, ul;
1797 ssize_t ealen;
1798 int olen, eapad1, eapad2, error, i, easize;
1799 u_char *eae;
1800 void *tmp;
1801
1802 vp = ap->a_vp;
1803 ip = VTOI(vp);
1804 fs = ITOFS(ip);
1805
1806 if (vp->v_type == VCHR || vp->v_type == VBLK)
1807 return (EOPNOTSUPP);
1808 if (strlen(ap->a_name) == 0)
1809 return (EINVAL);
1810
1811 /* XXX Now unsupported API to delete EAs using NULL uio. */
1812 if (ap->a_uio == NULL)
1813 return (EOPNOTSUPP);
1814
1815 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1816 return (EROFS);
1817
1818 ealen = ap->a_uio->uio_resid;
1819 if (ealen < 0 || ealen > lblktosize(fs, UFS_NXADDR))
1820 return (EINVAL);
1821
1822 error = extattr_check_cred(vp, ap->a_attrnamespace,
1823 ap->a_cred, ap->a_td, VWRITE);
1824 if (error) {
1825 /*
1826 * ffs_lock_ea is not needed there, because the vnode
1827 * must be exclusively locked.
1828 */
1829 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1830 ip->i_ea_error = error;
1831 return (error);
1832 }
1833
1834 if (DOINGSUJ(vp)) {
1835 ASSERT_VOP_ELOCKED(vp, "ffs_deleteextattr");
1836 softdep_prealloc(vp, MNT_WAIT);
1837 if (vp->v_data == NULL)
1838 return (EBADF);
1839 }
1840
1841 error = ffs_open_ea(vp, ap->a_cred, ap->a_td);
1842 if (error)
1843 return (error);
1844
1845 ealength = sizeof(uint32_t) + 3 + strlen(ap->a_name);
1846 eapad1 = roundup2(ealength, 8) - ealength;
1847 eapad2 = roundup2(ealen, 8) - ealen;
1848 ealength += eapad1 + ealen + eapad2;
1849
1850 /*
1851 * CEM: rewrites of the same size or smaller could be done in-place
1852 * instead. (We don't acquire any fine-grained locks in here either,
1853 * so we could also do bigger writes in-place.)
1854 */
1855 eae = malloc(ip->i_ea_len + ealength, M_TEMP, M_WAITOK);
1856 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
1857 easize = ip->i_ea_len;
1858
1859 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
1860 &eap, NULL);
1861 if (olen == -1) {
1862 /* new, append at end */
1863 KASSERT(ALIGNED_TO(eae + easize, struct extattr),
1864 ("unaligned"));
1865 eap = (struct extattr *)(eae + easize);
1866 easize += ealength;
1867 } else {
1868 ul = eap->ea_length;
1869 i = (u_char *)EXTATTR_NEXT(eap) - eae;
1870 if (ul != ealength) {
1871 bcopy(EXTATTR_NEXT(eap), (u_char *)eap + ealength,
1872 easize - i);
1873 easize += (ealength - ul);
1874 }
1875 }
1876 if (easize > lblktosize(fs, UFS_NXADDR)) {
1877 free(eae, M_TEMP);
1878 ffs_close_ea(vp, 0, ap->a_cred, ap->a_td);
1879 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1880 ip->i_ea_error = ENOSPC;
1881 return (ENOSPC);
1882 }
1883 eap->ea_length = ealength;
1884 eap->ea_namespace = ap->a_attrnamespace;
1885 eap->ea_contentpadlen = eapad2;
1886 eap->ea_namelength = strlen(ap->a_name);
1887 memcpy(eap->ea_name, ap->a_name, strlen(ap->a_name));
1888 bzero(&eap->ea_name[strlen(ap->a_name)], eapad1);
1889 error = uiomove(EXTATTR_CONTENT(eap), ealen, ap->a_uio);
1890 if (error) {
1891 free(eae, M_TEMP);
1892 ffs_close_ea(vp, 0, ap->a_cred, ap->a_td);
1893 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
1894 ip->i_ea_error = error;
1895 return (error);
1896 }
1897 bzero((u_char *)EXTATTR_CONTENT(eap) + ealen, eapad2);
1898
1899 tmp = ip->i_ea_area;
1900 ip->i_ea_area = eae;
1901 ip->i_ea_len = easize;
1902 free(tmp, M_TEMP);
1903 error = ffs_close_ea(vp, 1, ap->a_cred, ap->a_td);
1904 return (error);
1905 }
1906
1907 /*
1908 * Vnode pointer to File handle
1909 */
1910 static int
ffs_vptofh(struct vop_vptofh_args * ap)1911 ffs_vptofh(struct vop_vptofh_args *ap)
1912 /*
1913 vop_vptofh {
1914 IN struct vnode *a_vp;
1915 IN struct fid *a_fhp;
1916 };
1917 */
1918 {
1919 struct inode *ip;
1920 struct ufid *ufhp;
1921
1922 ip = VTOI(ap->a_vp);
1923 ufhp = (struct ufid *)ap->a_fhp;
1924 ufhp->ufid_len = sizeof(struct ufid);
1925 ufhp->ufid_ino = ip->i_number;
1926 ufhp->ufid_gen = ip->i_gen;
1927 return (0);
1928 }
1929
1930 SYSCTL_DECL(_vfs_ffs);
1931 static int use_buf_pager = 1;
1932 SYSCTL_INT(_vfs_ffs, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN, &use_buf_pager, 0,
1933 "Always use buffer pager instead of bmap");
1934
1935 static daddr_t
ffs_gbp_getblkno(struct vnode * vp,vm_ooffset_t off)1936 ffs_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
1937 {
1938
1939 return (lblkno(VFSTOUFS(vp->v_mount)->um_fs, off));
1940 }
1941
1942 static int
ffs_gbp_getblksz(struct vnode * vp,daddr_t lbn,long * sz)1943 ffs_gbp_getblksz(struct vnode *vp, daddr_t lbn, long *sz)
1944 {
1945
1946 *sz = blksize(VFSTOUFS(vp->v_mount)->um_fs, VTOI(vp), lbn);
1947 return (0);
1948 }
1949
1950 static int
ffs_getpages(struct vop_getpages_args * ap)1951 ffs_getpages(struct vop_getpages_args *ap)
1952 {
1953 struct vnode *vp;
1954 struct ufsmount *um;
1955
1956 vp = ap->a_vp;
1957 um = VFSTOUFS(vp->v_mount);
1958
1959 if (!use_buf_pager && um->um_devvp->v_bufobj.bo_bsize <= PAGE_SIZE)
1960 return (vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
1961 ap->a_rbehind, ap->a_rahead, NULL, NULL));
1962 return (vfs_bio_getpages(vp, ap->a_m, ap->a_count, ap->a_rbehind,
1963 ap->a_rahead, ffs_gbp_getblkno, ffs_gbp_getblksz));
1964 }
1965
1966 static int
ffs_getpages_async(struct vop_getpages_async_args * ap)1967 ffs_getpages_async(struct vop_getpages_async_args *ap)
1968 {
1969 struct vnode *vp;
1970 struct ufsmount *um;
1971 bool do_iodone;
1972 int error;
1973
1974 vp = ap->a_vp;
1975 um = VFSTOUFS(vp->v_mount);
1976 do_iodone = true;
1977
1978 if (um->um_devvp->v_bufobj.bo_bsize <= PAGE_SIZE) {
1979 error = vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
1980 ap->a_rbehind, ap->a_rahead, ap->a_iodone, ap->a_arg);
1981 if (error == 0)
1982 do_iodone = false;
1983 } else {
1984 error = vfs_bio_getpages(vp, ap->a_m, ap->a_count,
1985 ap->a_rbehind, ap->a_rahead, ffs_gbp_getblkno,
1986 ffs_gbp_getblksz);
1987 }
1988 if (do_iodone && ap->a_iodone != NULL)
1989 ap->a_iodone(ap->a_arg, ap->a_m, ap->a_count, error);
1990
1991 return (error);
1992 }
1993
1994 static int
ffs_vput_pair(struct vop_vput_pair_args * ap)1995 ffs_vput_pair(struct vop_vput_pair_args *ap)
1996 {
1997 struct mount *mp;
1998 struct vnode *dvp, *vp, *vp1, **vpp;
1999 struct inode *dp, *ip;
2000 ino_t ip_ino;
2001 u_int64_t ip_gen;
2002 int error, vp_locked;
2003
2004 dvp = ap->a_dvp;
2005 dp = VTOI(dvp);
2006 vpp = ap->a_vpp;
2007 vp = vpp != NULL ? *vpp : NULL;
2008
2009 if ((dp->i_flag & (IN_NEEDSYNC | IN_ENDOFF)) == 0) {
2010 vput(dvp);
2011 if (vp != NULL && ap->a_unlock_vp)
2012 vput(vp);
2013 return (0);
2014 }
2015
2016 mp = dvp->v_mount;
2017 if (vp != NULL) {
2018 if (ap->a_unlock_vp) {
2019 vput(vp);
2020 } else {
2021 MPASS(vp->v_type != VNON);
2022 vp_locked = VOP_ISLOCKED(vp);
2023 ip = VTOI(vp);
2024 ip_ino = ip->i_number;
2025 ip_gen = ip->i_gen;
2026 VOP_UNLOCK(vp);
2027 }
2028 }
2029
2030 /*
2031 * If compaction or fsync was requested do it in ffs_vput_pair()
2032 * now that other locks are no longer held.
2033 */
2034 if ((dp->i_flag & IN_ENDOFF) != 0) {
2035 VNASSERT(I_ENDOFF(dp) != 0 && I_ENDOFF(dp) < dp->i_size, dvp,
2036 ("IN_ENDOFF set but I_ENDOFF() is not"));
2037 dp->i_flag &= ~IN_ENDOFF;
2038 error = UFS_TRUNCATE(dvp, (off_t)I_ENDOFF(dp), IO_NORMAL |
2039 (DOINGASYNC(dvp) ? 0 : IO_SYNC), curthread->td_ucred);
2040 if (error != 0 && error != ERELOOKUP) {
2041 if (!ffs_fsfail_cleanup(VFSTOUFS(mp), error)) {
2042 vn_printf(dvp,
2043 "IN_ENDOFF: failed to truncate, "
2044 "error %d\n", error);
2045 }
2046 #ifdef UFS_DIRHASH
2047 ufsdirhash_free(dp);
2048 #endif
2049 }
2050 SET_I_ENDOFF(dp, 0);
2051 }
2052 if ((dp->i_flag & IN_NEEDSYNC) != 0) {
2053 do {
2054 error = ffs_syncvnode(dvp, MNT_WAIT, 0);
2055 } while (error == ERELOOKUP);
2056 }
2057
2058 vput(dvp);
2059
2060 if (vp == NULL || ap->a_unlock_vp)
2061 return (0);
2062 MPASS(mp != NULL);
2063
2064 /*
2065 * It is possible that vp is reclaimed at this point. Only
2066 * routines that call us with a_unlock_vp == false can find
2067 * that their vp has been reclaimed. There are three areas
2068 * that are affected:
2069 * 1) vn_open_cred() - later VOPs could fail, but
2070 * dead_open() returns 0 to simulate successful open.
2071 * 2) ffs_snapshot() - creation of snapshot fails with EBADF.
2072 * 3) NFS server (several places) - code is prepared to detect
2073 * and respond to dead vnodes by returning ESTALE.
2074 */
2075 VOP_LOCK(vp, vp_locked | LK_RETRY);
2076 if (IS_UFS(vp))
2077 return (0);
2078
2079 /*
2080 * Try harder to recover from reclaimed vp if reclaim was not
2081 * because underlying inode was cleared. We saved inode
2082 * number and inode generation, so we can try to reinstantiate
2083 * exactly same version of inode. If this fails, return
2084 * original doomed vnode and let caller to handle
2085 * consequences.
2086 *
2087 * Note that callers must keep write started around
2088 * VOP_VPUT_PAIR() calls, so it is safe to use mp without
2089 * busying it.
2090 */
2091 VOP_UNLOCK(vp);
2092 error = ffs_inotovp(mp, ip_ino, ip_gen, LK_EXCLUSIVE, &vp1,
2093 FFSV_REPLACE_DOOMED);
2094 if (error != 0) {
2095 VOP_LOCK(vp, vp_locked | LK_RETRY);
2096 } else {
2097 vrele(vp);
2098 *vpp = vp1;
2099 }
2100 return (error);
2101 }
2102