lfs_vnops.c revision 1.239.2.2 1 /* $NetBSD: lfs_vnops.c,v 1.239.2.2 2016/08/27 14:13:18 bouyer Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 /*
32 * Copyright (c) 1986, 1989, 1991, 1993, 1995
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * @(#)lfs_vnops.c 8.13 (Berkeley) 6/10/95
60 */
61
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: lfs_vnops.c,v 1.239.2.2 2016/08/27 14:13:18 bouyer Exp $");
64
65 #ifdef _KERNEL_OPT
66 #include "opt_compat_netbsd.h"
67 #include "opt_uvm_page_trkown.h"
68 #endif
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/namei.h>
73 #include <sys/resourcevar.h>
74 #include <sys/kernel.h>
75 #include <sys/file.h>
76 #include <sys/stat.h>
77 #include <sys/buf.h>
78 #include <sys/proc.h>
79 #include <sys/mount.h>
80 #include <sys/vnode.h>
81 #include <sys/pool.h>
82 #include <sys/signalvar.h>
83 #include <sys/kauth.h>
84 #include <sys/syslog.h>
85 #include <sys/fstrans.h>
86
87 #include <miscfs/fifofs/fifo.h>
88 #include <miscfs/genfs/genfs.h>
89 #include <miscfs/specfs/specdev.h>
90
91 #include <ufs/ufs/inode.h>
92 #include <ufs/ufs/dir.h>
93 #include <ufs/ufs/ufsmount.h>
94 #include <ufs/ufs/ufs_bswap.h>
95 #include <ufs/ufs/ufs_extern.h>
96
97 #include <uvm/uvm.h>
98 #include <uvm/uvm_pmap.h>
99 #include <uvm/uvm_stat.h>
100 #include <uvm/uvm_pager.h>
101
102 #include <ufs/lfs/lfs.h>
103 #include <ufs/lfs/lfs_extern.h>
104
105 extern pid_t lfs_writer_daemon;
106 int lfs_ignore_lazy_sync = 1;
107
108 /* Global vfs data structures for lfs. */
109 int (**lfs_vnodeop_p)(void *);
110 const struct vnodeopv_entry_desc lfs_vnodeop_entries[] = {
111 { &vop_default_desc, vn_default_error },
112 { &vop_lookup_desc, ufs_lookup }, /* lookup */
113 { &vop_create_desc, lfs_create }, /* create */
114 { &vop_whiteout_desc, ufs_whiteout }, /* whiteout */
115 { &vop_mknod_desc, lfs_mknod }, /* mknod */
116 { &vop_open_desc, ufs_open }, /* open */
117 { &vop_close_desc, lfs_close }, /* close */
118 { &vop_access_desc, ufs_access }, /* access */
119 { &vop_getattr_desc, lfs_getattr }, /* getattr */
120 { &vop_setattr_desc, lfs_setattr }, /* setattr */
121 { &vop_read_desc, lfs_read }, /* read */
122 { &vop_write_desc, lfs_write }, /* write */
123 { &vop_ioctl_desc, ufs_ioctl }, /* ioctl */
124 { &vop_fcntl_desc, lfs_fcntl }, /* fcntl */
125 { &vop_poll_desc, ufs_poll }, /* poll */
126 { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */
127 { &vop_revoke_desc, ufs_revoke }, /* revoke */
128 { &vop_mmap_desc, lfs_mmap }, /* mmap */
129 { &vop_fsync_desc, lfs_fsync }, /* fsync */
130 { &vop_seek_desc, ufs_seek }, /* seek */
131 { &vop_remove_desc, lfs_remove }, /* remove */
132 { &vop_link_desc, lfs_link }, /* link */
133 { &vop_rename_desc, lfs_rename }, /* rename */
134 { &vop_mkdir_desc, lfs_mkdir }, /* mkdir */
135 { &vop_rmdir_desc, lfs_rmdir }, /* rmdir */
136 { &vop_symlink_desc, lfs_symlink }, /* symlink */
137 { &vop_readdir_desc, ufs_readdir }, /* readdir */
138 { &vop_readlink_desc, ufs_readlink }, /* readlink */
139 { &vop_abortop_desc, ufs_abortop }, /* abortop */
140 { &vop_inactive_desc, lfs_inactive }, /* inactive */
141 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
142 { &vop_lock_desc, ufs_lock }, /* lock */
143 { &vop_unlock_desc, ufs_unlock }, /* unlock */
144 { &vop_bmap_desc, ufs_bmap }, /* bmap */
145 { &vop_strategy_desc, lfs_strategy }, /* strategy */
146 { &vop_print_desc, ufs_print }, /* print */
147 { &vop_islocked_desc, ufs_islocked }, /* islocked */
148 { &vop_pathconf_desc, ufs_pathconf }, /* pathconf */
149 { &vop_advlock_desc, ufs_advlock }, /* advlock */
150 { &vop_bwrite_desc, lfs_bwrite }, /* bwrite */
151 { &vop_getpages_desc, lfs_getpages }, /* getpages */
152 { &vop_putpages_desc, lfs_putpages }, /* putpages */
153 { NULL, NULL }
154 };
155 const struct vnodeopv_desc lfs_vnodeop_opv_desc =
156 { &lfs_vnodeop_p, lfs_vnodeop_entries };
157
158 int (**lfs_specop_p)(void *);
159 const struct vnodeopv_entry_desc lfs_specop_entries[] = {
160 { &vop_default_desc, vn_default_error },
161 { &vop_lookup_desc, spec_lookup }, /* lookup */
162 { &vop_create_desc, spec_create }, /* create */
163 { &vop_mknod_desc, spec_mknod }, /* mknod */
164 { &vop_open_desc, spec_open }, /* open */
165 { &vop_close_desc, lfsspec_close }, /* close */
166 { &vop_access_desc, ufs_access }, /* access */
167 { &vop_getattr_desc, lfs_getattr }, /* getattr */
168 { &vop_setattr_desc, lfs_setattr }, /* setattr */
169 { &vop_read_desc, ufsspec_read }, /* read */
170 { &vop_write_desc, ufsspec_write }, /* write */
171 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
172 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
173 { &vop_poll_desc, spec_poll }, /* poll */
174 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
175 { &vop_revoke_desc, spec_revoke }, /* revoke */
176 { &vop_mmap_desc, spec_mmap }, /* mmap */
177 { &vop_fsync_desc, spec_fsync }, /* fsync */
178 { &vop_seek_desc, spec_seek }, /* seek */
179 { &vop_remove_desc, spec_remove }, /* remove */
180 { &vop_link_desc, spec_link }, /* link */
181 { &vop_rename_desc, spec_rename }, /* rename */
182 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
183 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
184 { &vop_symlink_desc, spec_symlink }, /* symlink */
185 { &vop_readdir_desc, spec_readdir }, /* readdir */
186 { &vop_readlink_desc, spec_readlink }, /* readlink */
187 { &vop_abortop_desc, spec_abortop }, /* abortop */
188 { &vop_inactive_desc, lfs_inactive }, /* inactive */
189 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
190 { &vop_lock_desc, ufs_lock }, /* lock */
191 { &vop_unlock_desc, ufs_unlock }, /* unlock */
192 { &vop_bmap_desc, spec_bmap }, /* bmap */
193 { &vop_strategy_desc, spec_strategy }, /* strategy */
194 { &vop_print_desc, ufs_print }, /* print */
195 { &vop_islocked_desc, ufs_islocked }, /* islocked */
196 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
197 { &vop_advlock_desc, spec_advlock }, /* advlock */
198 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */
199 { &vop_getpages_desc, spec_getpages }, /* getpages */
200 { &vop_putpages_desc, spec_putpages }, /* putpages */
201 { NULL, NULL }
202 };
203 const struct vnodeopv_desc lfs_specop_opv_desc =
204 { &lfs_specop_p, lfs_specop_entries };
205
206 int (**lfs_fifoop_p)(void *);
207 const struct vnodeopv_entry_desc lfs_fifoop_entries[] = {
208 { &vop_default_desc, vn_default_error },
209 { &vop_lookup_desc, vn_fifo_bypass }, /* lookup */
210 { &vop_create_desc, vn_fifo_bypass }, /* create */
211 { &vop_mknod_desc, vn_fifo_bypass }, /* mknod */
212 { &vop_open_desc, vn_fifo_bypass }, /* open */
213 { &vop_close_desc, lfsfifo_close }, /* close */
214 { &vop_access_desc, ufs_access }, /* access */
215 { &vop_getattr_desc, lfs_getattr }, /* getattr */
216 { &vop_setattr_desc, lfs_setattr }, /* setattr */
217 { &vop_read_desc, ufsfifo_read }, /* read */
218 { &vop_write_desc, ufsfifo_write }, /* write */
219 { &vop_ioctl_desc, vn_fifo_bypass }, /* ioctl */
220 { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */
221 { &vop_poll_desc, vn_fifo_bypass }, /* poll */
222 { &vop_kqfilter_desc, vn_fifo_bypass }, /* kqfilter */
223 { &vop_revoke_desc, vn_fifo_bypass }, /* revoke */
224 { &vop_mmap_desc, vn_fifo_bypass }, /* mmap */
225 { &vop_fsync_desc, vn_fifo_bypass }, /* fsync */
226 { &vop_seek_desc, vn_fifo_bypass }, /* seek */
227 { &vop_remove_desc, vn_fifo_bypass }, /* remove */
228 { &vop_link_desc, vn_fifo_bypass }, /* link */
229 { &vop_rename_desc, vn_fifo_bypass }, /* rename */
230 { &vop_mkdir_desc, vn_fifo_bypass }, /* mkdir */
231 { &vop_rmdir_desc, vn_fifo_bypass }, /* rmdir */
232 { &vop_symlink_desc, vn_fifo_bypass }, /* symlink */
233 { &vop_readdir_desc, vn_fifo_bypass }, /* readdir */
234 { &vop_readlink_desc, vn_fifo_bypass }, /* readlink */
235 { &vop_abortop_desc, vn_fifo_bypass }, /* abortop */
236 { &vop_inactive_desc, lfs_inactive }, /* inactive */
237 { &vop_reclaim_desc, lfs_reclaim }, /* reclaim */
238 { &vop_lock_desc, ufs_lock }, /* lock */
239 { &vop_unlock_desc, ufs_unlock }, /* unlock */
240 { &vop_bmap_desc, vn_fifo_bypass }, /* bmap */
241 { &vop_strategy_desc, vn_fifo_bypass }, /* strategy */
242 { &vop_print_desc, ufs_print }, /* print */
243 { &vop_islocked_desc, ufs_islocked }, /* islocked */
244 { &vop_pathconf_desc, vn_fifo_bypass }, /* pathconf */
245 { &vop_advlock_desc, vn_fifo_bypass }, /* advlock */
246 { &vop_bwrite_desc, lfs_bwrite }, /* bwrite */
247 { &vop_putpages_desc, vn_fifo_bypass }, /* putpages */
248 { NULL, NULL }
249 };
250 const struct vnodeopv_desc lfs_fifoop_opv_desc =
251 { &lfs_fifoop_p, lfs_fifoop_entries };
252
253 static int check_dirty(struct lfs *, struct vnode *, off_t, off_t, off_t, int, int, struct vm_page **);
254
255 #define LFS_READWRITE
256 #include <ufs/ufs/ufs_readwrite.c>
257 #undef LFS_READWRITE
258
259 /*
260 * Synch an open file.
261 */
262 /* ARGSUSED */
263 int
264 lfs_fsync(void *v)
265 {
266 struct vop_fsync_args /* {
267 struct vnode *a_vp;
268 kauth_cred_t a_cred;
269 int a_flags;
270 off_t offlo;
271 off_t offhi;
272 } */ *ap = v;
273 struct vnode *vp = ap->a_vp;
274 int error, wait;
275 struct inode *ip = VTOI(vp);
276 struct lfs *fs = ip->i_lfs;
277
278 /* If we're mounted read-only, don't try to sync. */
279 if (fs->lfs_ronly)
280 return 0;
281
282 /* If a removed vnode is being cleaned, no need to sync here. */
283 if ((ap->a_flags & FSYNC_RECLAIM) != 0 && ip->i_mode == 0)
284 return 0;
285
286 /*
287 * Trickle sync simply adds this vnode to the pager list, as if
288 * the pagedaemon had requested a pageout.
289 */
290 if (ap->a_flags & FSYNC_LAZY) {
291 if (lfs_ignore_lazy_sync == 0) {
292 mutex_enter(&lfs_lock);
293 if (!(ip->i_flags & IN_PAGING)) {
294 ip->i_flags |= IN_PAGING;
295 TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip,
296 i_lfs_pchain);
297 }
298 wakeup(&lfs_writer_daemon);
299 mutex_exit(&lfs_lock);
300 }
301 return 0;
302 }
303
304 /*
305 * If a vnode is bring cleaned, flush it out before we try to
306 * reuse it. This prevents the cleaner from writing files twice
307 * in the same partial segment, causing an accounting underflow.
308 */
309 if (ap->a_flags & FSYNC_RECLAIM && ip->i_flags & IN_CLEANING) {
310 lfs_vflush(vp);
311 }
312
313 wait = (ap->a_flags & FSYNC_WAIT);
314 do {
315 mutex_enter(vp->v_interlock);
316 error = VOP_PUTPAGES(vp, trunc_page(ap->a_offlo),
317 round_page(ap->a_offhi),
318 PGO_CLEANIT | (wait ? PGO_SYNCIO : 0));
319 if (error == EAGAIN) {
320 mutex_enter(&lfs_lock);
321 mtsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_fsync",
322 hz / 100 + 1, &lfs_lock);
323 mutex_exit(&lfs_lock);
324 }
325 } while (error == EAGAIN);
326 if (error)
327 return error;
328
329 if ((ap->a_flags & FSYNC_DATAONLY) == 0)
330 error = lfs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0);
331
332 if (error == 0 && ap->a_flags & FSYNC_CACHE) {
333 int l = 0;
334 error = VOP_IOCTL(ip->i_devvp, DIOCCACHESYNC, &l, FWRITE,
335 curlwp->l_cred);
336 }
337 if (wait && !VPISEMPTY(vp))
338 LFS_SET_UINO(ip, IN_MODIFIED);
339
340 return error;
341 }
342
343 /*
344 * Take IN_ADIROP off, then call ufs_inactive.
345 */
346 int
347 lfs_inactive(void *v)
348 {
349 struct vop_inactive_args /* {
350 struct vnode *a_vp;
351 } */ *ap = v;
352
353 lfs_unmark_vnode(ap->a_vp);
354
355 /*
356 * The Ifile is only ever inactivated on unmount.
357 * Streamline this process by not giving it more dirty blocks.
358 */
359 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM) {
360 mutex_enter(&lfs_lock);
361 LFS_CLR_UINO(VTOI(ap->a_vp), IN_ALLMOD);
362 mutex_exit(&lfs_lock);
363 VOP_UNLOCK(ap->a_vp);
364 return 0;
365 }
366
367 #ifdef DEBUG
368 /*
369 * This might happen on unmount.
370 * XXX If it happens at any other time, it should be a panic.
371 */
372 if (ap->a_vp->v_uflag & VU_DIROP) {
373 struct inode *ip = VTOI(ap->a_vp);
374 printf("lfs_inactive: inactivating VU_DIROP? ino = %d\n", (int)ip->i_number);
375 }
376 #endif /* DIAGNOSTIC */
377
378 return ufs_inactive(v);
379 }
380
381 /*
382 * These macros are used to bracket UFS directory ops, so that we can
383 * identify all the pages touched during directory ops which need to
384 * be ordered and flushed atomically, so that they may be recovered.
385 *
386 * Because we have to mark nodes VU_DIROP in order to prevent
387 * the cache from reclaiming them while a dirop is in progress, we must
388 * also manage the number of nodes so marked (otherwise we can run out).
389 * We do this by setting lfs_dirvcount to the number of marked vnodes; it
390 * is decremented during segment write, when VU_DIROP is taken off.
391 */
392 #define MARK_VNODE(vp) lfs_mark_vnode(vp)
393 #define UNMARK_VNODE(vp) lfs_unmark_vnode(vp)
394 #define SET_DIROP_CREATE(dvp, vpp) lfs_set_dirop_create((dvp), (vpp))
395 #define SET_DIROP_REMOVE(dvp, vp) lfs_set_dirop((dvp), (vp))
396 static int lfs_set_dirop_create(struct vnode *, struct vnode **);
397 static int lfs_set_dirop(struct vnode *, struct vnode *);
398
399 static int
400 lfs_set_dirop(struct vnode *dvp, struct vnode *vp)
401 {
402 struct lfs *fs;
403 int error;
404
405 KASSERT(VOP_ISLOCKED(dvp));
406 KASSERT(vp == NULL || VOP_ISLOCKED(vp));
407
408 fs = VTOI(dvp)->i_lfs;
409
410 ASSERT_NO_SEGLOCK(fs);
411 /*
412 * LFS_NRESERVE calculates direct and indirect blocks as well
413 * as an inode block; an overestimate in most cases.
414 */
415 if ((error = lfs_reserve(fs, dvp, vp, LFS_NRESERVE(fs))) != 0)
416 return (error);
417
418 restart:
419 mutex_enter(&lfs_lock);
420 if (fs->lfs_dirops == 0) {
421 mutex_exit(&lfs_lock);
422 lfs_check(dvp, LFS_UNUSED_LBN, 0);
423 mutex_enter(&lfs_lock);
424 }
425 while (fs->lfs_writer) {
426 error = mtsleep(&fs->lfs_dirops, (PRIBIO + 1) | PCATCH,
427 "lfs_sdirop", 0, &lfs_lock);
428 if (error == EINTR) {
429 mutex_exit(&lfs_lock);
430 goto unreserve;
431 }
432 }
433 if (lfs_dirvcount > LFS_MAX_DIROP && fs->lfs_dirops == 0) {
434 wakeup(&lfs_writer_daemon);
435 mutex_exit(&lfs_lock);
436 preempt();
437 goto restart;
438 }
439
440 if (lfs_dirvcount > LFS_MAX_DIROP) {
441 DLOG((DLOG_DIROP, "lfs_set_dirop: sleeping with dirops=%d, "
442 "dirvcount=%d\n", fs->lfs_dirops, lfs_dirvcount));
443 if ((error = mtsleep(&lfs_dirvcount,
444 PCATCH | PUSER | PNORELOCK, "lfs_maxdirop", 0,
445 &lfs_lock)) != 0) {
446 mutex_exit(&lfs_lock);
447 goto unreserve;
448 }
449 mutex_exit(&lfs_lock);
450 goto restart;
451 }
452
453 ++fs->lfs_dirops;
454 /* fs->lfs_doifile = 1; */ /* XXX why? --ks */
455 mutex_exit(&lfs_lock);
456
457 /* Hold a reference so SET_ENDOP will be happy */
458 vref(dvp);
459 if (vp) {
460 vref(vp);
461 MARK_VNODE(vp);
462 }
463
464 MARK_VNODE(dvp);
465 return 0;
466
467 unreserve:
468 lfs_reserve(fs, dvp, vp, -LFS_NRESERVE(fs));
469 return error;
470 }
471
472 /*
473 * Get a new vnode *before* adjusting the dirop count, to avoid a deadlock
474 * in getnewvnode(), if we have a stacked filesystem mounted on top
475 * of us.
476 *
477 * NB: this means we have to clear the new vnodes on error. Fortunately
478 * SET_ENDOP is there to do that for us.
479 */
480 static int
481 lfs_set_dirop_create(struct vnode *dvp, struct vnode **vpp)
482 {
483 int error;
484 struct lfs *fs;
485
486 fs = VFSTOUFS(dvp->v_mount)->um_lfs;
487 ASSERT_NO_SEGLOCK(fs);
488 if (fs->lfs_ronly)
489 return EROFS;
490 if (vpp == NULL) {
491 return lfs_set_dirop(dvp, NULL);
492 }
493 error = getnewvnode(VT_LFS, dvp->v_mount, lfs_vnodeop_p, NULL, vpp);
494 if (error) {
495 DLOG((DLOG_ALLOC, "lfs_set_dirop_create: dvp %p error %d\n",
496 dvp, error));
497 return error;
498 }
499 if ((error = lfs_set_dirop(dvp, NULL)) != 0) {
500 ungetnewvnode(*vpp);
501 *vpp = NULL;
502 return error;
503 }
504 return 0;
505 }
506
507 #define SET_ENDOP_BASE(fs, dvp, str) \
508 do { \
509 mutex_enter(&lfs_lock); \
510 --(fs)->lfs_dirops; \
511 if (!(fs)->lfs_dirops) { \
512 if ((fs)->lfs_nadirop) { \
513 panic("SET_ENDOP: %s: no dirops but " \
514 " nadirop=%d", (str), \
515 (fs)->lfs_nadirop); \
516 } \
517 wakeup(&(fs)->lfs_writer); \
518 mutex_exit(&lfs_lock); \
519 lfs_check((dvp), LFS_UNUSED_LBN, 0); \
520 } else \
521 mutex_exit(&lfs_lock); \
522 } while(0)
523 #define SET_ENDOP_CREATE(fs, dvp, nvpp, str) \
524 do { \
525 UNMARK_VNODE(dvp); \
526 if (nvpp && *nvpp) \
527 UNMARK_VNODE(*nvpp); \
528 /* Check for error return to stem vnode leakage */ \
529 if (nvpp && *nvpp && !((*nvpp)->v_uflag & VU_DIROP)) \
530 ungetnewvnode(*(nvpp)); \
531 SET_ENDOP_BASE((fs), (dvp), (str)); \
532 lfs_reserve((fs), (dvp), NULL, -LFS_NRESERVE(fs)); \
533 vrele(dvp); \
534 } while(0)
535 #define SET_ENDOP_CREATE_AP(ap, str) \
536 SET_ENDOP_CREATE(VTOI((ap)->a_dvp)->i_lfs, (ap)->a_dvp, \
537 (ap)->a_vpp, (str))
538 #define SET_ENDOP_REMOVE(fs, dvp, ovp, str) \
539 do { \
540 UNMARK_VNODE(dvp); \
541 if (ovp) \
542 UNMARK_VNODE(ovp); \
543 SET_ENDOP_BASE((fs), (dvp), (str)); \
544 lfs_reserve((fs), (dvp), (ovp), -LFS_NRESERVE(fs)); \
545 vrele(dvp); \
546 if (ovp) \
547 vrele(ovp); \
548 } while(0)
549
550 void
551 lfs_mark_vnode(struct vnode *vp)
552 {
553 struct inode *ip = VTOI(vp);
554 struct lfs *fs = ip->i_lfs;
555
556 mutex_enter(&lfs_lock);
557 if (!(ip->i_flag & IN_ADIROP)) {
558 if (!(vp->v_uflag & VU_DIROP)) {
559 mutex_exit(&lfs_lock);
560 mutex_enter(vp->v_interlock);
561 if (lfs_vref(vp) != 0)
562 panic("lfs_mark_vnode: could not vref");
563 mutex_enter(&lfs_lock);
564 ++lfs_dirvcount;
565 ++fs->lfs_dirvcount;
566 TAILQ_INSERT_TAIL(&fs->lfs_dchainhd, ip, i_lfs_dchain);
567 vp->v_uflag |= VU_DIROP;
568 }
569 ++fs->lfs_nadirop;
570 ip->i_flag &= ~IN_CDIROP;
571 ip->i_flag |= IN_ADIROP;
572 } else
573 KASSERT(vp->v_uflag & VU_DIROP);
574 mutex_exit(&lfs_lock);
575 }
576
577 void
578 lfs_unmark_vnode(struct vnode *vp)
579 {
580 struct inode *ip = VTOI(vp);
581
582 mutex_enter(&lfs_lock);
583 if (ip && (ip->i_flag & IN_ADIROP)) {
584 KASSERT(vp->v_uflag & VU_DIROP);
585 --ip->i_lfs->lfs_nadirop;
586 ip->i_flag &= ~IN_ADIROP;
587 }
588 mutex_exit(&lfs_lock);
589 }
590
591 int
592 lfs_symlink(void *v)
593 {
594 struct vop_symlink_args /* {
595 struct vnode *a_dvp;
596 struct vnode **a_vpp;
597 struct componentname *a_cnp;
598 struct vattr *a_vap;
599 char *a_target;
600 } */ *ap = v;
601 int error;
602
603 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
604 vput(ap->a_dvp);
605 return error;
606 }
607 error = ufs_symlink(ap);
608 SET_ENDOP_CREATE_AP(ap, "symlink");
609 return (error);
610 }
611
612 int
613 lfs_mknod(void *v)
614 {
615 struct vop_mknod_args /* {
616 struct vnode *a_dvp;
617 struct vnode **a_vpp;
618 struct componentname *a_cnp;
619 struct vattr *a_vap;
620 } */ *ap = v;
621 struct vattr *vap = ap->a_vap;
622 struct vnode **vpp = ap->a_vpp;
623 struct inode *ip;
624 int error;
625 struct mount *mp;
626 ino_t ino;
627 struct ufs_lookup_results *ulr;
628
629 /* XXX should handle this material another way */
630 ulr = &VTOI(ap->a_dvp)->i_crap;
631 UFS_CHECK_CRAPCOUNTER(VTOI(ap->a_dvp));
632
633 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
634 vput(ap->a_dvp);
635 return error;
636 }
637 error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode),
638 ap->a_dvp, ulr, vpp, ap->a_cnp);
639
640 /* Either way we're done with the dirop at this point */
641 SET_ENDOP_CREATE_AP(ap, "mknod");
642
643 if (error)
644 return (error);
645
646 ip = VTOI(*vpp);
647 mp = (*vpp)->v_mount;
648 ino = ip->i_number;
649 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE;
650 if (vap->va_rdev != VNOVAL) {
651 /*
652 * Want to be able to use this to make badblock
653 * inodes, so don't truncate the dev number.
654 */
655 #if 0
656 ip->i_ffs1_rdev = ufs_rw32(vap->va_rdev,
657 UFS_MPNEEDSWAP((*vpp)->v_mount));
658 #else
659 ip->i_ffs1_rdev = vap->va_rdev;
660 #endif
661 }
662
663 /*
664 * Call fsync to write the vnode so that we don't have to deal with
665 * flushing it when it's marked VU_DIROP|VI_XLOCK.
666 *
667 * XXX KS - If we can't flush we also can't call vgone(), so must
668 * return. But, that leaves this vnode in limbo, also not good.
669 * Can this ever happen (barring hardware failure)?
670 */
671 if ((error = VOP_FSYNC(*vpp, NOCRED, FSYNC_WAIT, 0, 0)) != 0) {
672 panic("lfs_mknod: couldn't fsync (ino %llu)",
673 (unsigned long long)ino);
674 /* return (error); */
675 }
676 /*
677 * Remove vnode so that it will be reloaded by VFS_VGET and
678 * checked to see if it is an alias of an existing entry in
679 * the inode cache.
680 */
681 /* Used to be vput, but that causes us to call VOP_INACTIVE twice. */
682
683 VOP_UNLOCK(*vpp);
684 (*vpp)->v_type = VNON;
685 vgone(*vpp);
686 error = VFS_VGET(mp, ino, vpp);
687
688 if (error != 0) {
689 *vpp = NULL;
690 return (error);
691 }
692 return (0);
693 }
694
695 int
696 lfs_create(void *v)
697 {
698 struct vop_create_args /* {
699 struct vnode *a_dvp;
700 struct vnode **a_vpp;
701 struct componentname *a_cnp;
702 struct vattr *a_vap;
703 } */ *ap = v;
704 int error;
705
706 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
707 vput(ap->a_dvp);
708 return error;
709 }
710 error = ufs_create(ap);
711 SET_ENDOP_CREATE_AP(ap, "create");
712 return (error);
713 }
714
715 int
716 lfs_mkdir(void *v)
717 {
718 struct vop_mkdir_args /* {
719 struct vnode *a_dvp;
720 struct vnode **a_vpp;
721 struct componentname *a_cnp;
722 struct vattr *a_vap;
723 } */ *ap = v;
724 int error;
725
726 if ((error = SET_DIROP_CREATE(ap->a_dvp, ap->a_vpp)) != 0) {
727 vput(ap->a_dvp);
728 return error;
729 }
730 error = ufs_mkdir(ap);
731 SET_ENDOP_CREATE_AP(ap, "mkdir");
732 return (error);
733 }
734
735 int
736 lfs_remove(void *v)
737 {
738 struct vop_remove_args /* {
739 struct vnode *a_dvp;
740 struct vnode *a_vp;
741 struct componentname *a_cnp;
742 } */ *ap = v;
743 struct vnode *dvp, *vp;
744 struct inode *ip;
745 int error;
746
747 dvp = ap->a_dvp;
748 vp = ap->a_vp;
749 ip = VTOI(vp);
750 if ((error = SET_DIROP_REMOVE(dvp, vp)) != 0) {
751 if (dvp == vp)
752 vrele(vp);
753 else
754 vput(vp);
755 vput(dvp);
756 return error;
757 }
758 error = ufs_remove(ap);
759 if (ip->i_nlink == 0)
760 lfs_orphan(ip->i_lfs, ip->i_number);
761 SET_ENDOP_REMOVE(ip->i_lfs, dvp, ap->a_vp, "remove");
762 return (error);
763 }
764
765 int
766 lfs_rmdir(void *v)
767 {
768 struct vop_rmdir_args /* {
769 struct vnodeop_desc *a_desc;
770 struct vnode *a_dvp;
771 struct vnode *a_vp;
772 struct componentname *a_cnp;
773 } */ *ap = v;
774 struct vnode *vp;
775 struct inode *ip;
776 int error;
777
778 vp = ap->a_vp;
779 ip = VTOI(vp);
780 if ((error = SET_DIROP_REMOVE(ap->a_dvp, ap->a_vp)) != 0) {
781 if (ap->a_dvp == vp)
782 vrele(ap->a_dvp);
783 else
784 vput(ap->a_dvp);
785 vput(vp);
786 return error;
787 }
788 error = ufs_rmdir(ap);
789 if (ip->i_nlink == 0)
790 lfs_orphan(ip->i_lfs, ip->i_number);
791 SET_ENDOP_REMOVE(ip->i_lfs, ap->a_dvp, ap->a_vp, "rmdir");
792 return (error);
793 }
794
795 int
796 lfs_link(void *v)
797 {
798 struct vop_link_args /* {
799 struct vnode *a_dvp;
800 struct vnode *a_vp;
801 struct componentname *a_cnp;
802 } */ *ap = v;
803 int error;
804 struct vnode **vpp = NULL;
805
806 if ((error = SET_DIROP_CREATE(ap->a_dvp, vpp)) != 0) {
807 vput(ap->a_dvp);
808 return error;
809 }
810 error = ufs_link(ap);
811 SET_ENDOP_CREATE(VTOI(ap->a_dvp)->i_lfs, ap->a_dvp, vpp, "link");
812 return (error);
813 }
814
815 /* XXX following lifted from ufs_lookup.c */
816 #define FSFMT(vp) (((vp)->v_mount->mnt_iflag & IMNT_DTYPE) == 0)
817
818 /*
819 * Check if either entry referred to by FROM_ULR is within the range
820 * of entries named by TO_ULR.
821 */
822 static int
823 ulr_overlap(const struct ufs_lookup_results *from_ulr,
824 const struct ufs_lookup_results *to_ulr)
825 {
826 doff_t from_start, from_prevstart;
827 doff_t to_start, to_end;
828
829 /*
830 * FROM is a DELETE result; offset points to the entry to
831 * remove and subtracting count gives the previous entry.
832 */
833 from_start = from_ulr->ulr_offset - from_ulr->ulr_count;
834 from_prevstart = from_ulr->ulr_offset;
835
836 /*
837 * TO is a RENAME (thus non-DELETE) result; offset points
838 * to the beginning of a region to write in, and adding
839 * count gives the end of the region.
840 */
841 to_start = to_ulr->ulr_offset;
842 to_end = to_ulr->ulr_offset + to_ulr->ulr_count;
843
844 if (from_prevstart >= to_start && from_prevstart < to_end) {
845 return 1;
846 }
847 if (from_start >= to_start && from_start < to_end) {
848 return 1;
849 }
850 return 0;
851 }
852
853 /*
854 * A virgin directory (no blushing please).
855 */
856 static const struct dirtemplate mastertemplate = {
857 0, 12, DT_DIR, 1, ".",
858 0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
859 };
860
861 /*
862 * Wrapper for relookup that also updates the supplemental results.
863 */
864 static int
865 do_relookup(struct vnode *dvp, struct ufs_lookup_results *ulr,
866 struct vnode **vp, struct componentname *cnp)
867 {
868 int error;
869
870 error = relookup(dvp, vp, cnp, 0);
871 if (error) {
872 return error;
873 }
874 /* update the supplemental reasults */
875 *ulr = VTOI(dvp)->i_crap;
876 UFS_CHECK_CRAPCOUNTER(VTOI(dvp));
877 return 0;
878 }
879
880 /*
881 * Lock and relookup a sequence of two directories and two children.
882 *
883 */
884 static int
885 lock_vnode_sequence(struct vnode *d1, struct ufs_lookup_results *ulr1,
886 struct vnode **v1_ret, struct componentname *cn1,
887 int v1_missing_ok,
888 int overlap_error,
889 struct vnode *d2, struct ufs_lookup_results *ulr2,
890 struct vnode **v2_ret, struct componentname *cn2,
891 int v2_missing_ok)
892 {
893 struct vnode *v1, *v2;
894 int error;
895
896 KASSERT(d1 != d2);
897
898 vn_lock(d1, LK_EXCLUSIVE | LK_RETRY);
899 if (VTOI(d1)->i_size == 0) {
900 /* d1 has been rmdir'd */
901 VOP_UNLOCK(d1);
902 return ENOENT;
903 }
904 error = do_relookup(d1, ulr1, &v1, cn1);
905 if (v1_missing_ok) {
906 if (error == ENOENT) {
907 /*
908 * Note: currently if the name doesn't exist,
909 * relookup succeeds (it intercepts the
910 * EJUSTRETURN from VOP_LOOKUP) and sets tvp
911 * to NULL. Therefore, we will never get
912 * ENOENT and this branch is not needed.
913 * However, in a saner future the EJUSTRETURN
914 * garbage will go away, so let's DTRT.
915 */
916 v1 = NULL;
917 error = 0;
918 }
919 } else {
920 if (error == 0 && v1 == NULL) {
921 /* This is what relookup sets if v1 disappeared. */
922 error = ENOENT;
923 }
924 }
925 if (error) {
926 VOP_UNLOCK(d1);
927 return error;
928 }
929 if (v1 && v1 == d2) {
930 VOP_UNLOCK(d1);
931 VOP_UNLOCK(v1);
932 vrele(v1);
933 return overlap_error;
934 }
935
936 /*
937 * The right way to do this is to do lookups without locking
938 * the results, and lock the results afterwards; then at the
939 * end we can avoid trying to lock v2 if v2 == v1.
940 *
941 * However, for the reasons described in the fdvp == tdvp case
942 * in rename below, we can't do that safely. So, in the case
943 * where v1 is not a directory, unlock it and lock it again
944 * afterwards. This is safe in locking order because a
945 * non-directory can't be above anything else in the tree. If
946 * v1 *is* a directory, that's not true, but then because d1
947 * != d2, v1 != v2.
948 */
949 if (v1 && v1->v_type != VDIR) {
950 VOP_UNLOCK(v1);
951 }
952 vn_lock(d2, LK_EXCLUSIVE | LK_RETRY);
953 if (VTOI(d2)->i_size == 0) {
954 /* d2 has been rmdir'd */
955 VOP_UNLOCK(d2);
956 if (v1 && v1->v_type == VDIR) {
957 VOP_UNLOCK(v1);
958 }
959 VOP_UNLOCK(d1);
960 if (v1) {
961 vrele(v1);
962 }
963 return ENOENT;
964 }
965 error = do_relookup(d2, ulr2, &v2, cn2);
966 if (v2_missing_ok) {
967 if (error == ENOENT) {
968 /* as above */
969 v2 = NULL;
970 error = 0;
971 }
972 } else {
973 if (error == 0 && v2 == NULL) {
974 /* This is what relookup sets if v2 disappeared. */
975 error = ENOENT;
976 }
977 }
978 if (error) {
979 VOP_UNLOCK(d2);
980 if (v1 && v1->v_type == VDIR) {
981 VOP_UNLOCK(v1);
982 }
983 VOP_UNLOCK(d1);
984 if (v1) {
985 vrele(v1);
986 }
987 return error;
988 }
989 if (v1 && v1->v_type != VDIR && v1 != v2) {
990 vn_lock(v1, LK_EXCLUSIVE | LK_RETRY);
991 }
992 *v1_ret = v1;
993 *v2_ret = v2;
994 return 0;
995 }
996
997 int
998 lfs_rename(void *v)
999 {
1000 struct vop_rename_args /* {
1001 struct vnode *a_fdvp;
1002 struct vnode *a_fvp;
1003 struct componentname *a_fcnp;
1004 struct vnode *a_tdvp;
1005 struct vnode *a_tvp;
1006 struct componentname *a_tcnp;
1007 } */ *ap = v;
1008 struct vnode *tvp, *tdvp, *fvp, *fdvp;
1009 struct componentname *tcnp, *fcnp;
1010 struct inode *ip, *txp, *fxp, *tdp, *fdp;
1011 struct mount *mp;
1012 struct direct *newdir;
1013 int doingdirectory, error, marked;
1014 ino_t oldparent, newparent;
1015
1016 struct ufs_lookup_results from_ulr, to_ulr;
1017 struct lfs *fs = VTOI(ap->a_fvp)->i_lfs;
1018
1019 tvp = ap->a_tvp;
1020 tdvp = ap->a_tdvp;
1021 fvp = ap->a_fvp;
1022 fdvp = ap->a_fdvp;
1023 tcnp = ap->a_tcnp;
1024 fcnp = ap->a_fcnp;
1025 doingdirectory = error = 0;
1026 oldparent = newparent = 0;
1027 marked = 0;
1028
1029 /* save the supplemental lookup results as they currently exist */
1030 from_ulr = VTOI(fdvp)->i_crap;
1031 to_ulr = VTOI(tdvp)->i_crap;
1032 UFS_CHECK_CRAPCOUNTER(VTOI(fdvp));
1033 UFS_CHECK_CRAPCOUNTER(VTOI(tdvp));
1034
1035 /*
1036 * Owing to VFS oddities we are currently called with tdvp/tvp
1037 * locked and not fdvp/fvp. In a sane world we'd be passed
1038 * tdvp and fdvp only, unlocked, and two name strings. Pretend
1039 * we have a sane world and unlock tdvp and tvp.
1040 */
1041 VOP_UNLOCK(tdvp);
1042 if (tvp && tvp != tdvp) {
1043 VOP_UNLOCK(tvp);
1044 }
1045
1046 /* Also pretend we have a sane world and vrele fvp/tvp. */
1047 vrele(fvp);
1048 fvp = NULL;
1049 if (tvp) {
1050 vrele(tvp);
1051 tvp = NULL;
1052 }
1053
1054 /*
1055 * Check for cross-device rename.
1056 */
1057 if (fdvp->v_mount != tdvp->v_mount) {
1058 error = EXDEV;
1059 goto abort;
1060 }
1061
1062 /*
1063 * Reject "." and ".."
1064 */
1065 if ((fcnp->cn_flags & ISDOTDOT) || (tcnp->cn_flags & ISDOTDOT) ||
1066 (fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1067 (tcnp->cn_namelen == 1 && tcnp->cn_nameptr[0] == '.')) {
1068 error = EINVAL;
1069 goto abort;
1070 }
1071
1072 /*
1073 * Get locks.
1074 */
1075
1076 /* paranoia */
1077 fcnp->cn_flags |= LOCKPARENT|LOCKLEAF;
1078 tcnp->cn_flags |= LOCKPARENT|LOCKLEAF;
1079
1080 if (fdvp == tdvp) {
1081 /* One directory. Lock it and relookup both children. */
1082 vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
1083
1084 if (VTOI(fdvp)->i_size == 0) {
1085 /* directory has been rmdir'd */
1086 VOP_UNLOCK(fdvp);
1087 error = ENOENT;
1088 goto abort;
1089 }
1090
1091 error = do_relookup(fdvp, &from_ulr, &fvp, fcnp);
1092 if (error == 0 && fvp == NULL) {
1093 /* relookup may produce this if fvp disappears */
1094 error = ENOENT;
1095 }
1096 if (error) {
1097 VOP_UNLOCK(fdvp);
1098 goto abort;
1099 }
1100
1101 /*
1102 * The right way to do this is to look up both children
1103 * without locking either, and then lock both unless they
1104 * turn out to be the same. However, due to deep-seated
1105 * VFS-level issues all lookups lock the child regardless
1106 * of whether LOCKLEAF is set (if LOCKLEAF is not set,
1107 * the child is locked during lookup and then unlocked)
1108 * so it is not safe to look up tvp while fvp is locked.
1109 *
1110 * Unlocking fvp here temporarily is more or less safe,
1111 * because with the directory locked there's not much
1112 * that can happen to it. However, ideally it wouldn't
1113 * be necessary. XXX.
1114 */
1115 VOP_UNLOCK(fvp);
1116 /* remember fdvp == tdvp so tdvp is locked */
1117 error = do_relookup(tdvp, &to_ulr, &tvp, tcnp);
1118 if (error && error != ENOENT) {
1119 VOP_UNLOCK(fdvp);
1120 goto abort;
1121 }
1122 if (error == ENOENT) {
1123 /*
1124 * Note: currently if the name doesn't exist,
1125 * relookup succeeds (it intercepts the
1126 * EJUSTRETURN from VOP_LOOKUP) and sets tvp
1127 * to NULL. Therefore, we will never get
1128 * ENOENT and this branch is not needed.
1129 * However, in a saner future the EJUSTRETURN
1130 * garbage will go away, so let's DTRT.
1131 */
1132 tvp = NULL;
1133 }
1134
1135 /* tvp is locked; lock fvp if necessary */
1136 if (!tvp || tvp != fvp) {
1137 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);
1138 }
1139 } else {
1140 int found_fdvp;
1141 struct vnode *illegal_fvp;
1142
1143 /*
1144 * The source must not be above the destination. (If
1145 * it were, the rename would detach a section of the
1146 * tree.)
1147 *
1148 * Look up the tree from tdvp to see if we find fdvp,
1149 * and if so, return the immediate child of fdvp we're
1150 * under; that must not turn out to be the same as
1151 * fvp.
1152 *
1153 * The per-volume rename lock guarantees that the
1154 * result of this check remains true until we finish
1155 * looking up and locking.
1156 */
1157 error = ufs_parentcheck(fdvp, tdvp, fcnp->cn_cred,
1158 &found_fdvp, &illegal_fvp);
1159 if (error) {
1160 goto abort;
1161 }
1162
1163 /* Must lock in tree order. */
1164
1165 if (found_fdvp) {
1166 /* fdvp -> fvp -> tdvp -> tvp */
1167 error = lock_vnode_sequence(fdvp, &from_ulr,
1168 &fvp, fcnp, 0,
1169 EINVAL,
1170 tdvp, &to_ulr,
1171 &tvp, tcnp, 1);
1172 } else {
1173 /* tdvp -> tvp -> fdvp -> fvp */
1174 error = lock_vnode_sequence(tdvp, &to_ulr,
1175 &tvp, tcnp, 1,
1176 ENOTEMPTY,
1177 fdvp, &from_ulr,
1178 &fvp, fcnp, 0);
1179 }
1180 if (error) {
1181 if (illegal_fvp) {
1182 vrele(illegal_fvp);
1183 }
1184 goto abort;
1185 }
1186 KASSERT(fvp != NULL);
1187
1188 if (illegal_fvp && fvp == illegal_fvp) {
1189 vrele(illegal_fvp);
1190 error = EINVAL;
1191 goto abort_withlocks;
1192 }
1193
1194 if (illegal_fvp) {
1195 vrele(illegal_fvp);
1196 }
1197 }
1198
1199 KASSERT(fdvp && VOP_ISLOCKED(fdvp));
1200 KASSERT(fvp && VOP_ISLOCKED(fvp));
1201 KASSERT(tdvp && VOP_ISLOCKED(tdvp));
1202 KASSERT(tvp == NULL || VOP_ISLOCKED(tvp));
1203
1204 /* --- everything is now locked --- */
1205
1206 if (tvp && ((VTOI(tvp)->i_flags & (IMMUTABLE | APPEND)) ||
1207 (VTOI(tdvp)->i_flags & APPEND))) {
1208 error = EPERM;
1209 goto abort_withlocks;
1210 }
1211
1212 /*
1213 * Check if just deleting a link name.
1214 */
1215 if (fvp == tvp) {
1216 if (fvp->v_type == VDIR) {
1217 error = EINVAL;
1218 goto abort_withlocks;
1219 }
1220
1221 /* Release destination completely. Leave fdvp locked. */
1222 VOP_ABORTOP(tdvp, tcnp);
1223 if (fdvp != tdvp) {
1224 VOP_UNLOCK(tdvp);
1225 }
1226 VOP_UNLOCK(tvp);
1227 vrele(tdvp);
1228 vrele(tvp);
1229
1230 /* Delete source. */
1231 /* XXX: do we really need to relookup again? */
1232
1233 /*
1234 * fdvp is still locked, but we just unlocked fvp
1235 * (because fvp == tvp) so just decref fvp
1236 */
1237 vrele(fvp);
1238 fcnp->cn_flags &= ~(MODMASK);
1239 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF;
1240 fcnp->cn_nameiop = DELETE;
1241 if ((error = relookup(fdvp, &fvp, fcnp, 0))) {
1242 vput(fdvp);
1243 return (error);
1244 }
1245 return (VOP_REMOVE(fdvp, fvp, fcnp));
1246 }
1247
1248 /* The tiny bit of actual LFS code in this function */
1249 if ((error = SET_DIROP_REMOVE(tdvp, tvp)) != 0)
1250 goto abort_withlocks;
1251 MARK_VNODE(fdvp);
1252 MARK_VNODE(fvp);
1253 marked = 1;
1254
1255 fdp = VTOI(fdvp);
1256 ip = VTOI(fvp);
1257 if ((nlink_t) ip->i_nlink >= LINK_MAX) {
1258 error = EMLINK;
1259 goto abort_withlocks;
1260 }
1261 if ((ip->i_flags & (IMMUTABLE | APPEND)) ||
1262 (fdp->i_flags & APPEND)) {
1263 error = EPERM;
1264 goto abort_withlocks;
1265 }
1266 if ((ip->i_mode & IFMT) == IFDIR) {
1267 /*
1268 * Avoid ".", "..", and aliases of "." for obvious reasons.
1269 */
1270 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') ||
1271 fdp == ip ||
1272 (fcnp->cn_flags & ISDOTDOT) ||
1273 (tcnp->cn_flags & ISDOTDOT) ||
1274 (ip->i_flag & IN_RENAME)) {
1275 error = EINVAL;
1276 goto abort_withlocks;
1277 }
1278 ip->i_flag |= IN_RENAME;
1279 doingdirectory = 1;
1280 }
1281 oldparent = fdp->i_number;
1282 VN_KNOTE(fdvp, NOTE_WRITE); /* XXXLUKEM/XXX: right place? */
1283
1284 /*
1285 * Both the directory
1286 * and target vnodes are locked.
1287 */
1288 tdp = VTOI(tdvp);
1289 txp = NULL;
1290 if (tvp)
1291 txp = VTOI(tvp);
1292
1293 mp = fdvp->v_mount;
1294 fstrans_start(mp, FSTRANS_SHARED);
1295
1296 if (oldparent != tdp->i_number)
1297 newparent = tdp->i_number;
1298
1299 /*
1300 * If ".." must be changed (ie the directory gets a new
1301 * parent) the user must have write permission in the source
1302 * so as to be able to change "..".
1303 */
1304 if (doingdirectory && newparent) {
1305 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred);
1306 if (error)
1307 goto out;
1308 }
1309
1310 KASSERT(fdvp != tvp);
1311
1312 if (newparent) {
1313 /* Check for the rename("foo/foo", "foo") case. */
1314 if (fdvp == tvp) {
1315 error = doingdirectory ? ENOTEMPTY : EISDIR;
1316 goto out;
1317 }
1318 }
1319
1320 fxp = VTOI(fvp);
1321 fdp = VTOI(fdvp);
1322
1323 error = UFS_WAPBL_BEGIN(fdvp->v_mount);
1324 if (error)
1325 goto out2;
1326
1327 /*
1328 * 1) Bump link count while we're moving stuff
1329 * around. If we crash somewhere before
1330 * completing our work, the link count
1331 * may be wrong, but correctable.
1332 */
1333 ip->i_nlink++;
1334 DIP_ASSIGN(ip, nlink, ip->i_nlink);
1335 ip->i_flag |= IN_CHANGE;
1336 if ((error = UFS_UPDATE(fvp, NULL, NULL, UPDATE_DIROP)) != 0) {
1337 goto bad;
1338 }
1339
1340 /*
1341 * 2) If target doesn't exist, link the target
1342 * to the source and unlink the source.
1343 * Otherwise, rewrite the target directory
1344 * entry to reference the source inode and
1345 * expunge the original entry's existence.
1346 */
1347 if (txp == NULL) {
1348 if (tdp->i_dev != ip->i_dev)
1349 panic("rename: EXDEV");
1350 /*
1351 * Account for ".." in new directory.
1352 * When source and destination have the same
1353 * parent we don't fool with the link count.
1354 */
1355 if (doingdirectory && newparent) {
1356 if ((nlink_t)tdp->i_nlink >= LINK_MAX) {
1357 error = EMLINK;
1358 goto bad;
1359 }
1360 tdp->i_nlink++;
1361 DIP_ASSIGN(tdp, nlink, tdp->i_nlink);
1362 tdp->i_flag |= IN_CHANGE;
1363 if ((error = UFS_UPDATE(tdvp, NULL, NULL,
1364 UPDATE_DIROP)) != 0) {
1365 tdp->i_nlink--;
1366 DIP_ASSIGN(tdp, nlink, tdp->i_nlink);
1367 tdp->i_flag |= IN_CHANGE;
1368 goto bad;
1369 }
1370 }
1371 newdir = pool_cache_get(ufs_direct_cache, PR_WAITOK);
1372 ufs_makedirentry(ip, tcnp, newdir);
1373 error = ufs_direnter(tdvp, &to_ulr,
1374 NULL, newdir, tcnp, NULL);
1375 pool_cache_put(ufs_direct_cache, newdir);
1376 if (error != 0) {
1377 if (doingdirectory && newparent) {
1378 tdp->i_nlink--;
1379 DIP_ASSIGN(tdp, nlink, tdp->i_nlink);
1380 tdp->i_flag |= IN_CHANGE;
1381 (void)UFS_UPDATE(tdvp, NULL, NULL,
1382 UPDATE_WAIT | UPDATE_DIROP);
1383 }
1384 goto bad;
1385 }
1386 VN_KNOTE(tdvp, NOTE_WRITE);
1387 } else {
1388 if (txp->i_dev != tdp->i_dev || txp->i_dev != ip->i_dev)
1389 panic("rename: EXDEV");
1390 /*
1391 * Short circuit rename(foo, foo).
1392 */
1393 if (txp->i_number == ip->i_number)
1394 panic("rename: same file");
1395 /*
1396 * If the parent directory is "sticky", then the user must
1397 * own the parent directory, or the destination of the rename,
1398 * otherwise the destination may not be changed (except by
1399 * root). This implements append-only directories.
1400 */
1401 if ((tdp->i_mode & S_ISTXT) &&
1402 kauth_authorize_generic(tcnp->cn_cred,
1403 KAUTH_GENERIC_ISSUSER, NULL) != 0 &&
1404 kauth_cred_geteuid(tcnp->cn_cred) != tdp->i_uid &&
1405 txp->i_uid != kauth_cred_geteuid(tcnp->cn_cred)) {
1406 error = EPERM;
1407 goto bad;
1408 }
1409 /*
1410 * Target must be empty if a directory and have no links
1411 * to it. Also, ensure source and target are compatible
1412 * (both directories, or both not directories).
1413 */
1414 if ((txp->i_mode & IFMT) == IFDIR) {
1415 if (txp->i_nlink > 2 ||
1416 !ufs_dirempty(txp, tdp->i_number, tcnp->cn_cred)) {
1417 error = ENOTEMPTY;
1418 goto bad;
1419 }
1420 if (!doingdirectory) {
1421 error = ENOTDIR;
1422 goto bad;
1423 }
1424 cache_purge(tdvp);
1425 } else if (doingdirectory) {
1426 error = EISDIR;
1427 goto bad;
1428 }
1429 if ((error = ufs_dirrewrite(tdp, to_ulr.ulr_offset,
1430 txp, ip->i_number,
1431 IFTODT(ip->i_mode), doingdirectory && newparent ?
1432 newparent : doingdirectory, IN_CHANGE | IN_UPDATE)) != 0)
1433 goto bad;
1434 if (doingdirectory) {
1435 /*
1436 * Truncate inode. The only stuff left in the directory
1437 * is "." and "..". The "." reference is inconsequential
1438 * since we are quashing it. We have removed the "."
1439 * reference and the reference in the parent directory,
1440 * but there may be other hard links.
1441 */
1442 if (!newparent) {
1443 tdp->i_nlink--;
1444 DIP_ASSIGN(tdp, nlink, tdp->i_nlink);
1445 tdp->i_flag |= IN_CHANGE;
1446 UFS_WAPBL_UPDATE(tdvp, NULL, NULL, 0);
1447 }
1448 txp->i_nlink--;
1449 DIP_ASSIGN(txp, nlink, txp->i_nlink);
1450 txp->i_flag |= IN_CHANGE;
1451 if ((error = UFS_TRUNCATE(tvp, (off_t)0, IO_SYNC,
1452 tcnp->cn_cred)))
1453 goto bad;
1454 }
1455 VN_KNOTE(tdvp, NOTE_WRITE);
1456 VN_KNOTE(tvp, NOTE_DELETE);
1457 }
1458
1459 /*
1460 * Handle case where the directory entry we need to remove,
1461 * which is/was at from_ulr.ulr_offset, or the one before it,
1462 * which is/was at from_ulr.ulr_offset - from_ulr.ulr_count,
1463 * may have been moved when the directory insertion above
1464 * performed compaction.
1465 */
1466 if (tdp->i_number == fdp->i_number &&
1467 ulr_overlap(&from_ulr, &to_ulr)) {
1468
1469 struct buf *bp;
1470 struct direct *ep;
1471 struct ufsmount *ump = fdp->i_ump;
1472 doff_t curpos;
1473 doff_t endsearch; /* offset to end directory search */
1474 uint32_t prev_reclen;
1475 int dirblksiz = ump->um_dirblksiz;
1476 const int needswap = UFS_MPNEEDSWAP(ump);
1477 u_long bmask;
1478 int namlen, entryoffsetinblock;
1479 char *dirbuf;
1480
1481 bmask = fdvp->v_mount->mnt_stat.f_iosize - 1;
1482
1483 /*
1484 * The fcnp entry will be somewhere between the start of
1485 * compaction (to_ulr.ulr_offset) and the original location
1486 * (from_ulr.ulr_offset).
1487 */
1488 curpos = to_ulr.ulr_offset;
1489 endsearch = from_ulr.ulr_offset + from_ulr.ulr_reclen;
1490 entryoffsetinblock = 0;
1491
1492 /*
1493 * Get the directory block containing the start of
1494 * compaction.
1495 */
1496 error = ufs_blkatoff(fdvp, (off_t)to_ulr.ulr_offset, &dirbuf,
1497 &bp, false);
1498 if (error)
1499 goto bad;
1500
1501 /*
1502 * Keep existing ulr_count (length of previous record)
1503 * for the case where compaction did not include the
1504 * previous entry but started at the from-entry.
1505 */
1506 prev_reclen = from_ulr.ulr_count;
1507
1508 while (curpos < endsearch) {
1509 uint32_t reclen;
1510
1511 /*
1512 * If necessary, get the next directory block.
1513 *
1514 * dholland 7/13/11 to the best of my understanding
1515 * this should never happen; compaction occurs only
1516 * within single blocks. I think.
1517 */
1518 if ((curpos & bmask) == 0) {
1519 if (bp != NULL)
1520 brelse(bp, 0);
1521 error = ufs_blkatoff(fdvp, (off_t)curpos,
1522 &dirbuf, &bp, false);
1523 if (error)
1524 goto bad;
1525 entryoffsetinblock = 0;
1526 }
1527
1528 KASSERT(bp != NULL);
1529 ep = (struct direct *)(dirbuf + entryoffsetinblock);
1530 reclen = ufs_rw16(ep->d_reclen, needswap);
1531
1532 #if (BYTE_ORDER == LITTLE_ENDIAN)
1533 if (FSFMT(fdvp) && needswap == 0)
1534 namlen = ep->d_type;
1535 else
1536 namlen = ep->d_namlen;
1537 #else
1538 if (FSFMT(fdvp) && needswap != 0)
1539 namlen = ep->d_type;
1540 else
1541 namlen = ep->d_namlen;
1542 #endif
1543 if ((ep->d_ino != 0) &&
1544 (ufs_rw32(ep->d_ino, needswap) != WINO) &&
1545 (namlen == fcnp->cn_namelen) &&
1546 memcmp(ep->d_name, fcnp->cn_nameptr, namlen) == 0) {
1547 from_ulr.ulr_reclen = reclen;
1548 break;
1549 }
1550 curpos += reclen;
1551 entryoffsetinblock += reclen;
1552 prev_reclen = reclen;
1553 }
1554
1555 from_ulr.ulr_offset = curpos;
1556 from_ulr.ulr_count = prev_reclen;
1557
1558 KASSERT(curpos <= endsearch);
1559
1560 /*
1561 * If ulr_offset points to start of a directory block,
1562 * clear ulr_count so ufs_dirremove() doesn't try to
1563 * merge free space over a directory block boundary.
1564 */
1565 if ((from_ulr.ulr_offset & (dirblksiz - 1)) == 0)
1566 from_ulr.ulr_count = 0;
1567
1568 brelse(bp, 0);
1569 }
1570
1571 /*
1572 * 3) Unlink the source.
1573 */
1574
1575 #if 0
1576 /*
1577 * Ensure that the directory entry still exists and has not
1578 * changed while the new name has been entered. If the source is
1579 * a file then the entry may have been unlinked or renamed. In
1580 * either case there is no further work to be done. If the source
1581 * is a directory then it cannot have been rmdir'ed; The IRENAME
1582 * flag ensures that it cannot be moved by another rename or removed
1583 * by a rmdir.
1584 */
1585 #endif
1586 KASSERT(fxp == ip);
1587
1588 /*
1589 * If the source is a directory with a new parent, the link
1590 * count of the old parent directory must be decremented and
1591 * ".." set to point to the new parent.
1592 */
1593 if (doingdirectory && newparent) {
1594 KASSERT(fdp != NULL);
1595 ufs_dirrewrite(fxp, mastertemplate.dot_reclen,
1596 fdp, newparent, DT_DIR, 0, IN_CHANGE);
1597 cache_purge(fdvp);
1598 }
1599 error = ufs_dirremove(fdvp, &from_ulr,
1600 fxp, fcnp->cn_flags, 0);
1601 fxp->i_flag &= ~IN_RENAME;
1602
1603 VN_KNOTE(fvp, NOTE_RENAME);
1604 goto done;
1605
1606 out:
1607 goto out2;
1608
1609 /* exit routines from steps 1 & 2 */
1610 bad:
1611 if (doingdirectory)
1612 ip->i_flag &= ~IN_RENAME;
1613 ip->i_nlink--;
1614 DIP_ASSIGN(ip, nlink, ip->i_nlink);
1615 ip->i_flag |= IN_CHANGE;
1616 ip->i_flag &= ~IN_RENAME;
1617 UFS_WAPBL_UPDATE(fvp, NULL, NULL, 0);
1618 done:
1619 UFS_WAPBL_END(fdvp->v_mount);
1620 out2:
1621 /*
1622 * clear IN_RENAME - some exit paths happen too early to go
1623 * through the cleanup done in the "bad" case above, so we
1624 * always do this mini-cleanup here.
1625 */
1626 ip->i_flag &= ~IN_RENAME;
1627
1628 VOP_UNLOCK(fdvp);
1629 if (tdvp != fdvp) {
1630 VOP_UNLOCK(tdvp);
1631 }
1632 VOP_UNLOCK(fvp);
1633 if (tvp && tvp != fvp) {
1634 VOP_UNLOCK(tvp);
1635 }
1636
1637 vrele(fdvp);
1638 vrele(tdvp);
1639 vrele(fvp);
1640 if (tvp) {
1641 vrele(tvp);
1642 }
1643
1644 fstrans_done(mp);
1645 if (marked) {
1646 UNMARK_VNODE(fdvp);
1647 UNMARK_VNODE(fvp);
1648 SET_ENDOP_REMOVE(fs, tdvp, tvp, "rename");
1649 }
1650 return (error);
1651
1652 abort_withlocks:
1653 VOP_UNLOCK(fdvp);
1654 if (tdvp != fdvp) {
1655 VOP_UNLOCK(tdvp);
1656 }
1657 VOP_UNLOCK(fvp);
1658 if (tvp && tvp != fvp) {
1659 VOP_UNLOCK(tvp);
1660 }
1661
1662 abort:
1663 VOP_ABORTOP(fdvp, fcnp); /* XXX, why not in NFS? */
1664 VOP_ABORTOP(tdvp, tcnp); /* XXX, why not in NFS? */
1665 vrele(tdvp);
1666 if (tvp) {
1667 vrele(tvp);
1668 }
1669 vrele(fdvp);
1670 if (fvp) {
1671 vrele(fvp);
1672 }
1673 if (marked) {
1674 UNMARK_VNODE(fdvp);
1675 UNMARK_VNODE(fvp);
1676 SET_ENDOP_REMOVE(fs, tdvp, tvp, "rename");
1677 }
1678 return (error);
1679 }
1680
1681 /* XXX hack to avoid calling ITIMES in getattr */
1682 int
1683 lfs_getattr(void *v)
1684 {
1685 struct vop_getattr_args /* {
1686 struct vnode *a_vp;
1687 struct vattr *a_vap;
1688 kauth_cred_t a_cred;
1689 } */ *ap = v;
1690 struct vnode *vp = ap->a_vp;
1691 struct inode *ip = VTOI(vp);
1692 struct vattr *vap = ap->a_vap;
1693 struct lfs *fs = ip->i_lfs;
1694 /*
1695 * Copy from inode table
1696 */
1697 vap->va_fsid = ip->i_dev;
1698 vap->va_fileid = ip->i_number;
1699 vap->va_mode = ip->i_mode & ~IFMT;
1700 vap->va_nlink = ip->i_nlink;
1701 vap->va_uid = ip->i_uid;
1702 vap->va_gid = ip->i_gid;
1703 vap->va_rdev = (dev_t)ip->i_ffs1_rdev;
1704 vap->va_size = vp->v_size;
1705 vap->va_atime.tv_sec = ip->i_ffs1_atime;
1706 vap->va_atime.tv_nsec = ip->i_ffs1_atimensec;
1707 vap->va_mtime.tv_sec = ip->i_ffs1_mtime;
1708 vap->va_mtime.tv_nsec = ip->i_ffs1_mtimensec;
1709 vap->va_ctime.tv_sec = ip->i_ffs1_ctime;
1710 vap->va_ctime.tv_nsec = ip->i_ffs1_ctimensec;
1711 vap->va_flags = ip->i_flags;
1712 vap->va_gen = ip->i_gen;
1713 /* this doesn't belong here */
1714 if (vp->v_type == VBLK)
1715 vap->va_blocksize = BLKDEV_IOSIZE;
1716 else if (vp->v_type == VCHR)
1717 vap->va_blocksize = MAXBSIZE;
1718 else
1719 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize;
1720 vap->va_bytes = fsbtob(fs, (u_quad_t)ip->i_lfs_effnblks);
1721 vap->va_type = vp->v_type;
1722 vap->va_filerev = ip->i_modrev;
1723 return (0);
1724 }
1725
1726 /*
1727 * Check to make sure the inode blocks won't choke the buffer
1728 * cache, then call ufs_setattr as usual.
1729 */
1730 int
1731 lfs_setattr(void *v)
1732 {
1733 struct vop_setattr_args /* {
1734 struct vnode *a_vp;
1735 struct vattr *a_vap;
1736 kauth_cred_t a_cred;
1737 } */ *ap = v;
1738 struct vnode *vp = ap->a_vp;
1739
1740 lfs_check(vp, LFS_UNUSED_LBN, 0);
1741 return ufs_setattr(v);
1742 }
1743
1744 /*
1745 * Release the block we hold on lfs_newseg wrapping. Called on file close,
1746 * or explicitly from LFCNWRAPGO. Called with the interlock held.
1747 */
1748 static int
1749 lfs_wrapgo(struct lfs *fs, struct inode *ip, int waitfor)
1750 {
1751 if (fs->lfs_stoplwp != curlwp)
1752 return EBUSY;
1753
1754 fs->lfs_stoplwp = NULL;
1755 cv_signal(&fs->lfs_stopcv);
1756
1757 KASSERT(fs->lfs_nowrap > 0);
1758 if (fs->lfs_nowrap <= 0) {
1759 return 0;
1760 }
1761
1762 if (--fs->lfs_nowrap == 0) {
1763 log(LOG_NOTICE, "%s: re-enabled log wrap\n", fs->lfs_fsmnt);
1764 wakeup(&fs->lfs_wrappass);
1765 lfs_wakeup_cleaner(fs);
1766 }
1767 if (waitfor) {
1768 mtsleep(&fs->lfs_nextseg, PCATCH | PUSER, "segment",
1769 0, &lfs_lock);
1770 }
1771
1772 return 0;
1773 }
1774
1775 /*
1776 * Close called
1777 */
1778 /* ARGSUSED */
1779 int
1780 lfs_close(void *v)
1781 {
1782 struct vop_close_args /* {
1783 struct vnode *a_vp;
1784 int a_fflag;
1785 kauth_cred_t a_cred;
1786 } */ *ap = v;
1787 struct vnode *vp = ap->a_vp;
1788 struct inode *ip = VTOI(vp);
1789 struct lfs *fs = ip->i_lfs;
1790
1791 if ((ip->i_number == ROOTINO || ip->i_number == LFS_IFILE_INUM) &&
1792 fs->lfs_stoplwp == curlwp) {
1793 mutex_enter(&lfs_lock);
1794 log(LOG_NOTICE, "lfs_close: releasing log wrap control\n");
1795 lfs_wrapgo(fs, ip, 0);
1796 mutex_exit(&lfs_lock);
1797 }
1798
1799 if (vp == ip->i_lfs->lfs_ivnode &&
1800 vp->v_mount->mnt_iflag & IMNT_UNMOUNT)
1801 return 0;
1802
1803 if (vp->v_usecount > 1 && vp != ip->i_lfs->lfs_ivnode) {
1804 LFS_ITIMES(ip, NULL, NULL, NULL);
1805 }
1806 return (0);
1807 }
1808
1809 /*
1810 * Close wrapper for special devices.
1811 *
1812 * Update the times on the inode then do device close.
1813 */
1814 int
1815 lfsspec_close(void *v)
1816 {
1817 struct vop_close_args /* {
1818 struct vnode *a_vp;
1819 int a_fflag;
1820 kauth_cred_t a_cred;
1821 } */ *ap = v;
1822 struct vnode *vp;
1823 struct inode *ip;
1824
1825 vp = ap->a_vp;
1826 ip = VTOI(vp);
1827 if (vp->v_usecount > 1) {
1828 LFS_ITIMES(ip, NULL, NULL, NULL);
1829 }
1830 return (VOCALL (spec_vnodeop_p, VOFFSET(vop_close), ap));
1831 }
1832
1833 /*
1834 * Close wrapper for fifo's.
1835 *
1836 * Update the times on the inode then do device close.
1837 */
1838 int
1839 lfsfifo_close(void *v)
1840 {
1841 struct vop_close_args /* {
1842 struct vnode *a_vp;
1843 int a_fflag;
1844 kauth_cred_ a_cred;
1845 } */ *ap = v;
1846 struct vnode *vp;
1847 struct inode *ip;
1848
1849 vp = ap->a_vp;
1850 ip = VTOI(vp);
1851 if (ap->a_vp->v_usecount > 1) {
1852 LFS_ITIMES(ip, NULL, NULL, NULL);
1853 }
1854 return (VOCALL (fifo_vnodeop_p, VOFFSET(vop_close), ap));
1855 }
1856
1857 /*
1858 * Reclaim an inode so that it can be used for other purposes.
1859 */
1860
1861 int
1862 lfs_reclaim(void *v)
1863 {
1864 struct vop_reclaim_args /* {
1865 struct vnode *a_vp;
1866 } */ *ap = v;
1867 struct vnode *vp = ap->a_vp;
1868 struct inode *ip = VTOI(vp);
1869 struct lfs *fs = ip->i_lfs;
1870 int error;
1871
1872 /*
1873 * The inode must be freed and updated before being removed
1874 * from its hash chain. Other threads trying to gain a hold
1875 * on the inode will be stalled because it is locked (VI_XLOCK).
1876 */
1877 if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0)
1878 lfs_vfree(vp, ip->i_number, ip->i_omode);
1879
1880 mutex_enter(&lfs_lock);
1881 LFS_CLR_UINO(ip, IN_ALLMOD);
1882 mutex_exit(&lfs_lock);
1883 if ((error = ufs_reclaim(vp)))
1884 return (error);
1885
1886 /*
1887 * Take us off the paging and/or dirop queues if we were on them.
1888 * We shouldn't be on them.
1889 */
1890 mutex_enter(&lfs_lock);
1891 if (ip->i_flags & IN_PAGING) {
1892 log(LOG_WARNING, "%s: reclaimed vnode is IN_PAGING\n",
1893 fs->lfs_fsmnt);
1894 ip->i_flags &= ~IN_PAGING;
1895 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
1896 }
1897 if (vp->v_uflag & VU_DIROP) {
1898 panic("reclaimed vnode is VU_DIROP");
1899 vp->v_uflag &= ~VU_DIROP;
1900 TAILQ_REMOVE(&fs->lfs_dchainhd, ip, i_lfs_dchain);
1901 }
1902 mutex_exit(&lfs_lock);
1903
1904 pool_put(&lfs_dinode_pool, ip->i_din.ffs1_din);
1905 lfs_deregister_all(vp);
1906 pool_put(&lfs_inoext_pool, ip->inode_ext.lfs);
1907 ip->inode_ext.lfs = NULL;
1908 genfs_node_destroy(vp);
1909 pool_put(&lfs_inode_pool, vp->v_data);
1910 vp->v_data = NULL;
1911 return (0);
1912 }
1913
1914 /*
1915 * Read a block from a storage device.
1916 * In order to avoid reading blocks that are in the process of being
1917 * written by the cleaner---and hence are not mutexed by the normal
1918 * buffer cache / page cache mechanisms---check for collisions before
1919 * reading.
1920 *
1921 * We inline ufs_strategy to make sure that the VOP_BMAP occurs *before*
1922 * the active cleaner test.
1923 *
1924 * XXX This code assumes that lfs_markv makes synchronous checkpoints.
1925 */
1926 int
1927 lfs_strategy(void *v)
1928 {
1929 struct vop_strategy_args /* {
1930 struct vnode *a_vp;
1931 struct buf *a_bp;
1932 } */ *ap = v;
1933 struct buf *bp;
1934 struct lfs *fs;
1935 struct vnode *vp;
1936 struct inode *ip;
1937 daddr_t tbn;
1938 #define MAXLOOP 25
1939 int i, sn, error, slept, loopcount;
1940
1941 bp = ap->a_bp;
1942 vp = ap->a_vp;
1943 ip = VTOI(vp);
1944 fs = ip->i_lfs;
1945
1946 /* lfs uses its strategy routine only for read */
1947 KASSERT(bp->b_flags & B_READ);
1948
1949 if (vp->v_type == VBLK || vp->v_type == VCHR)
1950 panic("lfs_strategy: spec");
1951 KASSERT(bp->b_bcount != 0);
1952 if (bp->b_blkno == bp->b_lblkno) {
1953 error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno,
1954 NULL);
1955 if (error) {
1956 bp->b_error = error;
1957 bp->b_resid = bp->b_bcount;
1958 biodone(bp);
1959 return (error);
1960 }
1961 if ((long)bp->b_blkno == -1) /* no valid data */
1962 clrbuf(bp);
1963 }
1964 if ((long)bp->b_blkno < 0) { /* block is not on disk */
1965 bp->b_resid = bp->b_bcount;
1966 biodone(bp);
1967 return (0);
1968 }
1969
1970 slept = 1;
1971 loopcount = 0;
1972 mutex_enter(&lfs_lock);
1973 while (slept && fs->lfs_seglock) {
1974 mutex_exit(&lfs_lock);
1975 /*
1976 * Look through list of intervals.
1977 * There will only be intervals to look through
1978 * if the cleaner holds the seglock.
1979 * Since the cleaner is synchronous, we can trust
1980 * the list of intervals to be current.
1981 */
1982 tbn = dbtofsb(fs, bp->b_blkno);
1983 sn = dtosn(fs, tbn);
1984 slept = 0;
1985 for (i = 0; i < fs->lfs_cleanind; i++) {
1986 if (sn == dtosn(fs, fs->lfs_cleanint[i]) &&
1987 tbn >= fs->lfs_cleanint[i]) {
1988 DLOG((DLOG_CLEAN,
1989 "lfs_strategy: ino %d lbn %" PRId64
1990 " ind %d sn %d fsb %" PRIx32
1991 " given sn %d fsb %" PRIx64 "\n",
1992 ip->i_number, bp->b_lblkno, i,
1993 dtosn(fs, fs->lfs_cleanint[i]),
1994 fs->lfs_cleanint[i], sn, tbn));
1995 DLOG((DLOG_CLEAN,
1996 "lfs_strategy: sleeping on ino %d lbn %"
1997 PRId64 "\n", ip->i_number, bp->b_lblkno));
1998 mutex_enter(&lfs_lock);
1999 if (LFS_SEGLOCK_HELD(fs) && fs->lfs_iocount) {
2000 /*
2001 * Cleaner can't wait for itself.
2002 * Instead, wait for the blocks
2003 * to be written to disk.
2004 * XXX we need pribio in the test
2005 * XXX here.
2006 */
2007 mtsleep(&fs->lfs_iocount,
2008 (PRIBIO + 1) | PNORELOCK,
2009 "clean2", hz/10 + 1,
2010 &lfs_lock);
2011 slept = 1;
2012 ++loopcount;
2013 break;
2014 } else if (fs->lfs_seglock) {
2015 mtsleep(&fs->lfs_seglock,
2016 (PRIBIO + 1) | PNORELOCK,
2017 "clean1", 0,
2018 &lfs_lock);
2019 slept = 1;
2020 break;
2021 }
2022 mutex_exit(&lfs_lock);
2023 }
2024 }
2025 mutex_enter(&lfs_lock);
2026 if (loopcount > MAXLOOP) {
2027 printf("lfs_strategy: breaking out of clean2 loop\n");
2028 break;
2029 }
2030 }
2031 mutex_exit(&lfs_lock);
2032
2033 vp = ip->i_devvp;
2034 VOP_STRATEGY(vp, bp);
2035 return (0);
2036 }
2037
2038 /*
2039 * Inline lfs_segwrite/lfs_writevnodes, but just for dirops.
2040 * Technically this is a checkpoint (the on-disk state is valid)
2041 * even though we are leaving out all the file data.
2042 */
2043 int
2044 lfs_flush_dirops(struct lfs *fs)
2045 {
2046 struct inode *ip, *nip;
2047 struct vnode *vp;
2048 extern int lfs_dostats;
2049 struct segment *sp;
2050 int flags = 0;
2051 int error = 0;
2052
2053 ASSERT_MAYBE_SEGLOCK(fs);
2054 KASSERT(fs->lfs_nadirop == 0);
2055
2056 if (fs->lfs_ronly)
2057 return EROFS;
2058
2059 mutex_enter(&lfs_lock);
2060 if (TAILQ_FIRST(&fs->lfs_dchainhd) == NULL) {
2061 mutex_exit(&lfs_lock);
2062 return 0;
2063 } else
2064 mutex_exit(&lfs_lock);
2065
2066 if (lfs_dostats)
2067 ++lfs_stats.flush_invoked;
2068
2069 lfs_imtime(fs);
2070 lfs_seglock(fs, flags);
2071 sp = fs->lfs_sp;
2072
2073 /*
2074 * lfs_writevnodes, optimized to get dirops out of the way.
2075 * Only write dirops, and don't flush files' pages, only
2076 * blocks from the directories.
2077 *
2078 * We don't need to vref these files because they are
2079 * dirops and so hold an extra reference until the
2080 * segunlock clears them of that status.
2081 *
2082 * We don't need to check for IN_ADIROP because we know that
2083 * no dirops are active.
2084 *
2085 */
2086 mutex_enter(&lfs_lock);
2087 for (ip = TAILQ_FIRST(&fs->lfs_dchainhd); ip != NULL; ip = nip) {
2088 nip = TAILQ_NEXT(ip, i_lfs_dchain);
2089 mutex_exit(&lfs_lock);
2090 vp = ITOV(ip);
2091
2092 KASSERT((ip->i_flag & IN_ADIROP) == 0);
2093 KASSERT(vp->v_uflag & VU_DIROP);
2094 KASSERT(!(vp->v_iflag & VI_XLOCK));
2095
2096 /*
2097 * All writes to directories come from dirops; all
2098 * writes to files' direct blocks go through the page
2099 * cache, which we're not touching. Reads to files
2100 * and/or directories will not be affected by writing
2101 * directory blocks inodes and file inodes. So we don't
2102 * really need to lock.
2103 */
2104 if (vp->v_iflag & VI_XLOCK) {
2105 mutex_enter(&lfs_lock);
2106 continue;
2107 }
2108 /* XXX see below
2109 * waslocked = VOP_ISLOCKED(vp);
2110 */
2111 if (vp->v_type != VREG &&
2112 ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp))) {
2113 error = lfs_writefile(fs, sp, vp);
2114 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
2115 !(ip->i_flag & IN_ALLMOD)) {
2116 mutex_enter(&lfs_lock);
2117 LFS_SET_UINO(ip, IN_MODIFIED);
2118 mutex_exit(&lfs_lock);
2119 }
2120 if (error && (sp->seg_flags & SEGM_SINGLE)) {
2121 mutex_enter(&lfs_lock);
2122 error = EAGAIN;
2123 break;
2124 }
2125 }
2126 KDASSERT(ip->i_number != LFS_IFILE_INUM);
2127 error = lfs_writeinode(fs, sp, ip);
2128 mutex_enter(&lfs_lock);
2129 if (error && (sp->seg_flags & SEGM_SINGLE)) {
2130 error = EAGAIN;
2131 break;
2132 }
2133
2134 /*
2135 * We might need to update these inodes again,
2136 * for example, if they have data blocks to write.
2137 * Make sure that after this flush, they are still
2138 * marked IN_MODIFIED so that we don't forget to
2139 * write them.
2140 */
2141 /* XXX only for non-directories? --KS */
2142 LFS_SET_UINO(ip, IN_MODIFIED);
2143 }
2144 mutex_exit(&lfs_lock);
2145 /* We've written all the dirops there are */
2146 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
2147 lfs_finalize_fs_seguse(fs);
2148 (void) lfs_writeseg(fs, sp);
2149 lfs_segunlock(fs);
2150
2151 return error;
2152 }
2153
2154 /*
2155 * Flush all vnodes for which the pagedaemon has requested pageouts.
2156 * Skip over any files that are marked VU_DIROP (since lfs_flush_dirop()
2157 * has just run, this would be an error). If we have to skip a vnode
2158 * for any reason, just skip it; if we have to wait for the cleaner,
2159 * abort. The writer daemon will call us again later.
2160 */
2161 int
2162 lfs_flush_pchain(struct lfs *fs)
2163 {
2164 struct inode *ip, *nip;
2165 struct vnode *vp;
2166 extern int lfs_dostats;
2167 struct segment *sp;
2168 int error, error2;
2169
2170 ASSERT_NO_SEGLOCK(fs);
2171
2172 if (fs->lfs_ronly)
2173 return EROFS;
2174
2175 mutex_enter(&lfs_lock);
2176 if (TAILQ_FIRST(&fs->lfs_pchainhd) == NULL) {
2177 mutex_exit(&lfs_lock);
2178 return 0;
2179 } else
2180 mutex_exit(&lfs_lock);
2181
2182 /* Get dirops out of the way */
2183 if ((error = lfs_flush_dirops(fs)) != 0)
2184 return error;
2185
2186 if (lfs_dostats)
2187 ++lfs_stats.flush_invoked;
2188
2189 /*
2190 * Inline lfs_segwrite/lfs_writevnodes, but just for pageouts.
2191 */
2192 lfs_imtime(fs);
2193 lfs_seglock(fs, 0);
2194 sp = fs->lfs_sp;
2195
2196 /*
2197 * lfs_writevnodes, optimized to clear pageout requests.
2198 * Only write non-dirop files that are in the pageout queue.
2199 * We're very conservative about what we write; we want to be
2200 * fast and async.
2201 */
2202 mutex_enter(&lfs_lock);
2203 top:
2204 for (ip = TAILQ_FIRST(&fs->lfs_pchainhd); ip != NULL; ip = nip) {
2205 nip = TAILQ_NEXT(ip, i_lfs_pchain);
2206 vp = ITOV(ip);
2207
2208 if (!(ip->i_flags & IN_PAGING))
2209 goto top;
2210
2211 mutex_enter(vp->v_interlock);
2212 if ((vp->v_iflag & VI_XLOCK) || (vp->v_uflag & VU_DIROP) != 0) {
2213 mutex_exit(vp->v_interlock);
2214 continue;
2215 }
2216 if (vp->v_type != VREG) {
2217 mutex_exit(vp->v_interlock);
2218 continue;
2219 }
2220 if (lfs_vref(vp))
2221 continue;
2222 mutex_exit(&lfs_lock);
2223
2224 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_RETRY) != 0) {
2225 lfs_vunref(vp);
2226 mutex_enter(&lfs_lock);
2227 continue;
2228 }
2229
2230 error = lfs_writefile(fs, sp, vp);
2231 if (!VPISEMPTY(vp) && !WRITEINPROG(vp) &&
2232 !(ip->i_flag & IN_ALLMOD)) {
2233 mutex_enter(&lfs_lock);
2234 LFS_SET_UINO(ip, IN_MODIFIED);
2235 mutex_exit(&lfs_lock);
2236 }
2237 KDASSERT(ip->i_number != LFS_IFILE_INUM);
2238 error2 = lfs_writeinode(fs, sp, ip);
2239
2240 VOP_UNLOCK(vp);
2241 lfs_vunref(vp);
2242
2243 if (error == EAGAIN || error2 == EAGAIN) {
2244 lfs_writeseg(fs, sp);
2245 mutex_enter(&lfs_lock);
2246 break;
2247 }
2248 mutex_enter(&lfs_lock);
2249 }
2250 mutex_exit(&lfs_lock);
2251 (void) lfs_writeseg(fs, sp);
2252 lfs_segunlock(fs);
2253
2254 return 0;
2255 }
2256
2257 /*
2258 * Provide a fcntl interface to sys_lfs_{segwait,bmapv,markv}.
2259 */
2260 int
2261 lfs_fcntl(void *v)
2262 {
2263 struct vop_fcntl_args /* {
2264 struct vnode *a_vp;
2265 u_int a_command;
2266 void * a_data;
2267 int a_fflag;
2268 kauth_cred_t a_cred;
2269 } */ *ap = v;
2270 struct timeval tv;
2271 struct timeval *tvp;
2272 BLOCK_INFO *blkiov;
2273 CLEANERINFO *cip;
2274 SEGUSE *sup;
2275 int blkcnt, error, oclean;
2276 size_t fh_size;
2277 struct lfs_fcntl_markv blkvp;
2278 struct lwp *l;
2279 fsid_t *fsidp;
2280 struct lfs *fs;
2281 struct buf *bp;
2282 fhandle_t *fhp;
2283 daddr_t off;
2284
2285 /* Only respect LFS fcntls on fs root or Ifile */
2286 if (VTOI(ap->a_vp)->i_number != ROOTINO &&
2287 VTOI(ap->a_vp)->i_number != LFS_IFILE_INUM) {
2288 return ufs_fcntl(v);
2289 }
2290
2291 /* Avoid locking a draining lock */
2292 if (ap->a_vp->v_mount->mnt_iflag & IMNT_UNMOUNT) {
2293 return ESHUTDOWN;
2294 }
2295
2296 /* LFS control and monitoring fcntls are available only to root */
2297 l = curlwp;
2298 if (((ap->a_command & 0xff00) >> 8) == 'L' &&
2299 (error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
2300 NULL)) != 0)
2301 return (error);
2302
2303 fs = VTOI(ap->a_vp)->i_lfs;
2304 fsidp = &ap->a_vp->v_mount->mnt_stat.f_fsidx;
2305
2306 error = 0;
2307 switch ((int)ap->a_command) {
2308 case LFCNSEGWAITALL_COMPAT_50:
2309 case LFCNSEGWAITALL_COMPAT:
2310 fsidp = NULL;
2311 /* FALLSTHROUGH */
2312 case LFCNSEGWAIT_COMPAT_50:
2313 case LFCNSEGWAIT_COMPAT:
2314 {
2315 struct timeval50 *tvp50
2316 = (struct timeval50 *)ap->a_data;
2317 timeval50_to_timeval(tvp50, &tv);
2318 tvp = &tv;
2319 }
2320 goto segwait_common;
2321 case LFCNSEGWAITALL:
2322 fsidp = NULL;
2323 /* FALLSTHROUGH */
2324 case LFCNSEGWAIT:
2325 tvp = (struct timeval *)ap->a_data;
2326 segwait_common:
2327 mutex_enter(&lfs_lock);
2328 ++fs->lfs_sleepers;
2329 mutex_exit(&lfs_lock);
2330
2331 error = lfs_segwait(fsidp, tvp);
2332
2333 mutex_enter(&lfs_lock);
2334 if (--fs->lfs_sleepers == 0)
2335 wakeup(&fs->lfs_sleepers);
2336 mutex_exit(&lfs_lock);
2337 return error;
2338
2339 case LFCNBMAPV:
2340 case LFCNMARKV:
2341 blkvp = *(struct lfs_fcntl_markv *)ap->a_data;
2342
2343 blkcnt = blkvp.blkcnt;
2344 if ((u_int) blkcnt > LFS_MARKV_MAXBLKCNT)
2345 return (EINVAL);
2346 blkiov = lfs_malloc(fs, blkcnt * sizeof(BLOCK_INFO), LFS_NB_BLKIOV);
2347 if ((error = copyin(blkvp.blkiov, blkiov,
2348 blkcnt * sizeof(BLOCK_INFO))) != 0) {
2349 lfs_free(fs, blkiov, LFS_NB_BLKIOV);
2350 return error;
2351 }
2352
2353 mutex_enter(&lfs_lock);
2354 ++fs->lfs_sleepers;
2355 mutex_exit(&lfs_lock);
2356 if (ap->a_command == LFCNBMAPV)
2357 error = lfs_bmapv(l->l_proc, fsidp, blkiov, blkcnt);
2358 else /* LFCNMARKV */
2359 error = lfs_markv(l->l_proc, fsidp, blkiov, blkcnt);
2360 if (error == 0)
2361 error = copyout(blkiov, blkvp.blkiov,
2362 blkcnt * sizeof(BLOCK_INFO));
2363 mutex_enter(&lfs_lock);
2364 if (--fs->lfs_sleepers == 0)
2365 wakeup(&fs->lfs_sleepers);
2366 mutex_exit(&lfs_lock);
2367 lfs_free(fs, blkiov, LFS_NB_BLKIOV);
2368 return error;
2369
2370 case LFCNRECLAIM:
2371 /*
2372 * Flush dirops and write Ifile, allowing empty segments
2373 * to be immediately reclaimed.
2374 */
2375 lfs_writer_enter(fs, "pndirop");
2376 off = fs->lfs_offset;
2377 lfs_seglock(fs, SEGM_FORCE_CKP | SEGM_CKP);
2378 lfs_flush_dirops(fs);
2379 LFS_CLEANERINFO(cip, fs, bp);
2380 oclean = cip->clean;
2381 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
2382 lfs_segwrite(ap->a_vp->v_mount, SEGM_FORCE_CKP);
2383 fs->lfs_sp->seg_flags |= SEGM_PROT;
2384 lfs_segunlock(fs);
2385 lfs_writer_leave(fs);
2386
2387 #ifdef DEBUG
2388 LFS_CLEANERINFO(cip, fs, bp);
2389 DLOG((DLOG_CLEAN, "lfs_fcntl: reclaim wrote %" PRId64
2390 " blocks, cleaned %" PRId32 " segments (activesb %d)\n",
2391 fs->lfs_offset - off, cip->clean - oclean,
2392 fs->lfs_activesb));
2393 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
2394 #endif
2395
2396 return 0;
2397
2398 case LFCNIFILEFH_COMPAT:
2399 /* Return the filehandle of the Ifile */
2400 if ((error = kauth_authorize_system(l->l_cred,
2401 KAUTH_SYSTEM_FILEHANDLE, 0, NULL, NULL, NULL)) != 0)
2402 return (error);
2403 fhp = (struct fhandle *)ap->a_data;
2404 fhp->fh_fsid = *fsidp;
2405 fh_size = 16; /* former VFS_MAXFIDSIZ */
2406 return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
2407
2408 case LFCNIFILEFH_COMPAT2:
2409 case LFCNIFILEFH:
2410 /* Return the filehandle of the Ifile */
2411 fhp = (struct fhandle *)ap->a_data;
2412 fhp->fh_fsid = *fsidp;
2413 fh_size = sizeof(struct lfs_fhandle) -
2414 offsetof(fhandle_t, fh_fid);
2415 return lfs_vptofh(fs->lfs_ivnode, &(fhp->fh_fid), &fh_size);
2416
2417 case LFCNREWIND:
2418 /* Move lfs_offset to the lowest-numbered segment */
2419 return lfs_rewind(fs, *(int *)ap->a_data);
2420
2421 case LFCNINVAL:
2422 /* Mark a segment SEGUSE_INVAL */
2423 LFS_SEGENTRY(sup, fs, *(int *)ap->a_data, bp);
2424 if (sup->su_nbytes > 0) {
2425 brelse(bp, 0);
2426 lfs_unset_inval_all(fs);
2427 return EBUSY;
2428 }
2429 sup->su_flags |= SEGUSE_INVAL;
2430 VOP_BWRITE(bp->b_vp, bp);
2431 return 0;
2432
2433 case LFCNRESIZE:
2434 /* Resize the filesystem */
2435 return lfs_resize_fs(fs, *(int *)ap->a_data);
2436
2437 case LFCNWRAPSTOP:
2438 case LFCNWRAPSTOP_COMPAT:
2439 /*
2440 * Hold lfs_newseg at segment 0; if requested, sleep until
2441 * the filesystem wraps around. To support external agents
2442 * (dump, fsck-based regression test) that need to look at
2443 * a snapshot of the filesystem, without necessarily
2444 * requiring that all fs activity stops.
2445 */
2446 if (fs->lfs_stoplwp == curlwp)
2447 return EALREADY;
2448
2449 mutex_enter(&lfs_lock);
2450 while (fs->lfs_stoplwp != NULL)
2451 cv_wait(&fs->lfs_stopcv, &lfs_lock);
2452 fs->lfs_stoplwp = curlwp;
2453 if (fs->lfs_nowrap == 0)
2454 log(LOG_NOTICE, "%s: disabled log wrap\n", fs->lfs_fsmnt);
2455 ++fs->lfs_nowrap;
2456 if (*(int *)ap->a_data == 1
2457 || ap->a_command == LFCNWRAPSTOP_COMPAT) {
2458 log(LOG_NOTICE, "LFCNSTOPWRAP waiting for log wrap\n");
2459 error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
2460 "segwrap", 0, &lfs_lock);
2461 log(LOG_NOTICE, "LFCNSTOPWRAP done waiting\n");
2462 if (error) {
2463 lfs_wrapgo(fs, VTOI(ap->a_vp), 0);
2464 }
2465 }
2466 mutex_exit(&lfs_lock);
2467 return 0;
2468
2469 case LFCNWRAPGO:
2470 case LFCNWRAPGO_COMPAT:
2471 /*
2472 * Having done its work, the agent wakes up the writer.
2473 * If the argument is 1, it sleeps until a new segment
2474 * is selected.
2475 */
2476 mutex_enter(&lfs_lock);
2477 error = lfs_wrapgo(fs, VTOI(ap->a_vp),
2478 ap->a_command == LFCNWRAPGO_COMPAT ? 1 :
2479 *((int *)ap->a_data));
2480 mutex_exit(&lfs_lock);
2481 return error;
2482
2483 case LFCNWRAPPASS:
2484 if ((VTOI(ap->a_vp)->i_lfs_iflags & LFSI_WRAPWAIT))
2485 return EALREADY;
2486 mutex_enter(&lfs_lock);
2487 if (fs->lfs_stoplwp != curlwp) {
2488 mutex_exit(&lfs_lock);
2489 return EALREADY;
2490 }
2491 if (fs->lfs_nowrap == 0) {
2492 mutex_exit(&lfs_lock);
2493 return EBUSY;
2494 }
2495 fs->lfs_wrappass = 1;
2496 wakeup(&fs->lfs_wrappass);
2497 /* Wait for the log to wrap, if asked */
2498 if (*(int *)ap->a_data) {
2499 mutex_enter(ap->a_vp->v_interlock);
2500 if (lfs_vref(ap->a_vp) != 0)
2501 panic("LFCNWRAPPASS: lfs_vref failed");
2502 VTOI(ap->a_vp)->i_lfs_iflags |= LFSI_WRAPWAIT;
2503 log(LOG_NOTICE, "LFCNPASS waiting for log wrap\n");
2504 error = mtsleep(&fs->lfs_nowrap, PCATCH | PUSER,
2505 "segwrap", 0, &lfs_lock);
2506 log(LOG_NOTICE, "LFCNPASS done waiting\n");
2507 VTOI(ap->a_vp)->i_lfs_iflags &= ~LFSI_WRAPWAIT;
2508 lfs_vunref(ap->a_vp);
2509 }
2510 mutex_exit(&lfs_lock);
2511 return error;
2512
2513 case LFCNWRAPSTATUS:
2514 mutex_enter(&lfs_lock);
2515 *(int *)ap->a_data = fs->lfs_wrapstatus;
2516 mutex_exit(&lfs_lock);
2517 return 0;
2518
2519 default:
2520 return ufs_fcntl(v);
2521 }
2522 return 0;
2523 }
2524
2525 int
2526 lfs_getpages(void *v)
2527 {
2528 struct vop_getpages_args /* {
2529 struct vnode *a_vp;
2530 voff_t a_offset;
2531 struct vm_page **a_m;
2532 int *a_count;
2533 int a_centeridx;
2534 vm_prot_t a_access_type;
2535 int a_advice;
2536 int a_flags;
2537 } */ *ap = v;
2538
2539 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM &&
2540 (ap->a_access_type & VM_PROT_WRITE) != 0) {
2541 return EPERM;
2542 }
2543 if ((ap->a_access_type & VM_PROT_WRITE) != 0) {
2544 mutex_enter(&lfs_lock);
2545 LFS_SET_UINO(VTOI(ap->a_vp), IN_MODIFIED);
2546 mutex_exit(&lfs_lock);
2547 }
2548
2549 /*
2550 * we're relying on the fact that genfs_getpages() always read in
2551 * entire filesystem blocks.
2552 */
2553 return genfs_getpages(v);
2554 }
2555
2556 /*
2557 * Wait for a page to become unbusy, possibly printing diagnostic messages
2558 * as well.
2559 *
2560 * Called with vp->v_interlock held; return with it held.
2561 */
2562 static void
2563 wait_for_page(struct vnode *vp, struct vm_page *pg, const char *label)
2564 {
2565 KASSERT(mutex_owned(vp->v_interlock));
2566 if ((pg->flags & PG_BUSY) == 0)
2567 return; /* Nothing to wait for! */
2568
2569 #if defined(DEBUG) && defined(UVM_PAGE_TRKOWN)
2570 static struct vm_page *lastpg;
2571
2572 if (label != NULL && pg != lastpg) {
2573 if (pg->owner_tag) {
2574 printf("lfs_putpages[%d.%d]: %s: page %p owner %d.%d [%s]\n",
2575 curproc->p_pid, curlwp->l_lid, label,
2576 pg, pg->owner, pg->lowner, pg->owner_tag);
2577 } else {
2578 printf("lfs_putpages[%d.%d]: %s: page %p unowned?!\n",
2579 curproc->p_pid, curlwp->l_lid, label, pg);
2580 }
2581 }
2582 lastpg = pg;
2583 #endif
2584
2585 pg->flags |= PG_WANTED;
2586 UVM_UNLOCK_AND_WAIT(pg, vp->v_interlock, 0, "lfsput", 0);
2587 mutex_enter(vp->v_interlock);
2588 }
2589
2590 /*
2591 * This routine is called by lfs_putpages() when it can't complete the
2592 * write because a page is busy. This means that either (1) someone,
2593 * possibly the pagedaemon, is looking at this page, and will give it up
2594 * presently; or (2) we ourselves are holding the page busy in the
2595 * process of being written (either gathered or actually on its way to
2596 * disk). We don't need to give up the segment lock, but we might need
2597 * to call lfs_writeseg() to expedite the page's journey to disk.
2598 *
2599 * Called with vp->v_interlock held; return with it held.
2600 */
2601 /* #define BUSYWAIT */
2602 static void
2603 write_and_wait(struct lfs *fs, struct vnode *vp, struct vm_page *pg,
2604 int seglocked, const char *label)
2605 {
2606 KASSERT(mutex_owned(vp->v_interlock));
2607 #ifndef BUSYWAIT
2608 struct inode *ip = VTOI(vp);
2609 struct segment *sp = fs->lfs_sp;
2610 int count = 0;
2611
2612 if (pg == NULL)
2613 return;
2614
2615 while (pg->flags & PG_BUSY &&
2616 pg->uobject == &vp->v_uobj) {
2617 mutex_exit(vp->v_interlock);
2618 if (sp->cbpp - sp->bpp > 1) {
2619 /* Write gathered pages */
2620 lfs_updatemeta(sp);
2621 lfs_release_finfo(fs);
2622 (void) lfs_writeseg(fs, sp);
2623
2624 /*
2625 * Reinitialize FIP
2626 */
2627 KASSERT(sp->vp == vp);
2628 lfs_acquire_finfo(fs, ip->i_number,
2629 ip->i_gen);
2630 }
2631 ++count;
2632 mutex_enter(vp->v_interlock);
2633 wait_for_page(vp, pg, label);
2634 }
2635 if (label != NULL && count > 1) {
2636 DLOG((DLOG_PAGE, "lfs_putpages[%d]: %s: %sn = %d\n",
2637 curproc->p_pid, label, (count > 0 ? "looping, " : ""),
2638 count));
2639 }
2640 #else
2641 preempt(1);
2642 #endif
2643 KASSERT(mutex_owned(vp->v_interlock));
2644 }
2645
2646 /*
2647 * Make sure that for all pages in every block in the given range,
2648 * either all are dirty or all are clean. If any of the pages
2649 * we've seen so far are dirty, put the vnode on the paging chain,
2650 * and mark it IN_PAGING.
2651 *
2652 * If checkfirst != 0, don't check all the pages but return at the
2653 * first dirty page.
2654 */
2655 static int
2656 check_dirty(struct lfs *fs, struct vnode *vp,
2657 off_t startoffset, off_t endoffset, off_t blkeof,
2658 int flags, int checkfirst, struct vm_page **pgp)
2659 {
2660 int by_list;
2661 struct vm_page *curpg = NULL; /* XXX: gcc */
2662 struct vm_page *pgs[MAXBSIZE / PAGE_SIZE], *pg;
2663 off_t soff = 0; /* XXX: gcc */
2664 voff_t off;
2665 int i;
2666 int nonexistent;
2667 int any_dirty; /* number of dirty pages */
2668 int dirty; /* number of dirty pages in a block */
2669 int tdirty;
2670 int pages_per_block = fs->lfs_bsize >> PAGE_SHIFT;
2671 int pagedaemon = (curlwp == uvm.pagedaemon_lwp);
2672
2673 KASSERT(mutex_owned(vp->v_interlock));
2674 ASSERT_MAYBE_SEGLOCK(fs);
2675 top:
2676 by_list = (vp->v_uobj.uo_npages <=
2677 ((endoffset - startoffset) >> PAGE_SHIFT) *
2678 UVM_PAGE_TREE_PENALTY);
2679 any_dirty = 0;
2680
2681 if (by_list) {
2682 curpg = TAILQ_FIRST(&vp->v_uobj.memq);
2683 } else {
2684 soff = startoffset;
2685 }
2686 while (by_list || soff < MIN(blkeof, endoffset)) {
2687 if (by_list) {
2688 /*
2689 * Find the first page in a block. Skip
2690 * blocks outside our area of interest or beyond
2691 * the end of file.
2692 */
2693 KASSERT(curpg == NULL
2694 || (curpg->flags & PG_MARKER) == 0);
2695 if (pages_per_block > 1) {
2696 while (curpg &&
2697 ((curpg->offset & fs->lfs_bmask) ||
2698 curpg->offset >= vp->v_size ||
2699 curpg->offset >= endoffset)) {
2700 curpg = TAILQ_NEXT(curpg, listq.queue);
2701 KASSERT(curpg == NULL ||
2702 (curpg->flags & PG_MARKER) == 0);
2703 }
2704 }
2705 if (curpg == NULL)
2706 break;
2707 soff = curpg->offset;
2708 }
2709
2710 /*
2711 * Mark all pages in extended range busy; find out if any
2712 * of them are dirty.
2713 */
2714 nonexistent = dirty = 0;
2715 for (i = 0; i == 0 || i < pages_per_block; i++) {
2716 KASSERT(mutex_owned(vp->v_interlock));
2717 if (by_list && pages_per_block <= 1) {
2718 pgs[i] = pg = curpg;
2719 } else {
2720 off = soff + (i << PAGE_SHIFT);
2721 pgs[i] = pg = uvm_pagelookup(&vp->v_uobj, off);
2722 if (pg == NULL) {
2723 ++nonexistent;
2724 continue;
2725 }
2726 }
2727 KASSERT(pg != NULL);
2728
2729 /*
2730 * If we're holding the segment lock, we can deadlock
2731 * against a process that has our page and is waiting
2732 * for the cleaner, while the cleaner waits for the
2733 * segment lock. Just bail in that case.
2734 */
2735 if ((pg->flags & PG_BUSY) &&
2736 (pagedaemon || LFS_SEGLOCK_HELD(fs))) {
2737 if (i > 0)
2738 uvm_page_unbusy(pgs, i);
2739 DLOG((DLOG_PAGE, "lfs_putpages: avoiding 3-way or pagedaemon deadlock\n"));
2740 if (pgp)
2741 *pgp = pg;
2742 KASSERT(mutex_owned(vp->v_interlock));
2743 return -1;
2744 }
2745
2746 while (pg->flags & PG_BUSY) {
2747 wait_for_page(vp, pg, NULL);
2748 KASSERT(mutex_owned(vp->v_interlock));
2749 if (i > 0)
2750 uvm_page_unbusy(pgs, i);
2751 KASSERT(mutex_owned(vp->v_interlock));
2752 goto top;
2753 }
2754 pg->flags |= PG_BUSY;
2755 UVM_PAGE_OWN(pg, "lfs_putpages");
2756
2757 pmap_page_protect(pg, VM_PROT_NONE);
2758 tdirty = (pmap_clear_modify(pg) ||
2759 (pg->flags & PG_CLEAN) == 0);
2760 dirty += tdirty;
2761 }
2762 if (pages_per_block > 0 && nonexistent >= pages_per_block) {
2763 if (by_list) {
2764 curpg = TAILQ_NEXT(curpg, listq.queue);
2765 } else {
2766 soff += fs->lfs_bsize;
2767 }
2768 continue;
2769 }
2770
2771 any_dirty += dirty;
2772 KASSERT(nonexistent == 0);
2773 KASSERT(mutex_owned(vp->v_interlock));
2774
2775 /*
2776 * If any are dirty make all dirty; unbusy them,
2777 * but if we were asked to clean, wire them so that
2778 * the pagedaemon doesn't bother us about them while
2779 * they're on their way to disk.
2780 */
2781 for (i = 0; i == 0 || i < pages_per_block; i++) {
2782 KASSERT(mutex_owned(vp->v_interlock));
2783 pg = pgs[i];
2784 KASSERT(!((pg->flags & PG_CLEAN) && (pg->flags & PG_DELWRI)));
2785 KASSERT(pg->flags & PG_BUSY);
2786 if (dirty) {
2787 pg->flags &= ~PG_CLEAN;
2788 if (flags & PGO_FREE) {
2789 /*
2790 * Wire the page so that
2791 * pdaemon doesn't see it again.
2792 */
2793 mutex_enter(&uvm_pageqlock);
2794 uvm_pagewire(pg);
2795 mutex_exit(&uvm_pageqlock);
2796
2797 /* Suspended write flag */
2798 pg->flags |= PG_DELWRI;
2799 }
2800 }
2801 if (pg->flags & PG_WANTED)
2802 wakeup(pg);
2803 pg->flags &= ~(PG_WANTED|PG_BUSY);
2804 UVM_PAGE_OWN(pg, NULL);
2805 }
2806
2807 if (checkfirst && any_dirty)
2808 break;
2809
2810 if (by_list) {
2811 curpg = TAILQ_NEXT(curpg, listq.queue);
2812 } else {
2813 soff += MAX(PAGE_SIZE, fs->lfs_bsize);
2814 }
2815 }
2816
2817 KASSERT(mutex_owned(vp->v_interlock));
2818 return any_dirty;
2819 }
2820
2821 /*
2822 * lfs_putpages functions like genfs_putpages except that
2823 *
2824 * (1) It needs to bounds-check the incoming requests to ensure that
2825 * they are block-aligned; if they are not, expand the range and
2826 * do the right thing in case, e.g., the requested range is clean
2827 * but the expanded range is dirty.
2828 *
2829 * (2) It needs to explicitly send blocks to be written when it is done.
2830 * If VOP_PUTPAGES is called without the seglock held, we simply take
2831 * the seglock and let lfs_segunlock wait for us.
2832 * XXX There might be a bad situation if we have to flush a vnode while
2833 * XXX lfs_markv is in operation. As of this writing we panic in this
2834 * XXX case.
2835 *
2836 * Assumptions:
2837 *
2838 * (1) The caller does not hold any pages in this vnode busy. If it does,
2839 * there is a danger that when we expand the page range and busy the
2840 * pages we will deadlock.
2841 *
2842 * (2) We are called with vp->v_interlock held; we must return with it
2843 * released.
2844 *
2845 * (3) We don't absolutely have to free pages right away, provided that
2846 * the request does not have PGO_SYNCIO. When the pagedaemon gives
2847 * us a request with PGO_FREE, we take the pages out of the paging
2848 * queue and wake up the writer, which will handle freeing them for us.
2849 *
2850 * We ensure that for any filesystem block, all pages for that
2851 * block are either resident or not, even if those pages are higher
2852 * than EOF; that means that we will be getting requests to free
2853 * "unused" pages above EOF all the time, and should ignore them.
2854 *
2855 * (4) If we are called with PGO_LOCKED, the finfo array we are to write
2856 * into has been set up for us by lfs_writefile. If not, we will
2857 * have to handle allocating and/or freeing an finfo entry.
2858 *
2859 * XXX note that we're (ab)using PGO_LOCKED as "seglock held".
2860 */
2861
2862 /* How many times to loop before we should start to worry */
2863 #define TOOMANY 4
2864
2865 int
2866 lfs_putpages(void *v)
2867 {
2868 int error;
2869 struct vop_putpages_args /* {
2870 struct vnode *a_vp;
2871 voff_t a_offlo;
2872 voff_t a_offhi;
2873 int a_flags;
2874 } */ *ap = v;
2875 struct vnode *vp;
2876 struct inode *ip;
2877 struct lfs *fs;
2878 struct segment *sp;
2879 off_t origoffset, startoffset, endoffset, origendoffset, blkeof;
2880 off_t off, max_endoffset;
2881 bool seglocked, sync, pagedaemon, reclaim;
2882 struct vm_page *pg, *busypg;
2883 UVMHIST_FUNC("lfs_putpages"); UVMHIST_CALLED(ubchist);
2884 int oreclaim = 0;
2885 int donewriting = 0;
2886 #ifdef DEBUG
2887 int debug_n_again, debug_n_dirtyclean;
2888 #endif
2889
2890 vp = ap->a_vp;
2891 ip = VTOI(vp);
2892 fs = ip->i_lfs;
2893 sync = (ap->a_flags & PGO_SYNCIO) != 0;
2894 reclaim = (ap->a_flags & PGO_RECLAIM) != 0;
2895 pagedaemon = (curlwp == uvm.pagedaemon_lwp);
2896
2897 KASSERT(mutex_owned(vp->v_interlock));
2898
2899 /* Putpages does nothing for metadata. */
2900 if (vp == fs->lfs_ivnode || vp->v_type != VREG) {
2901 mutex_exit(vp->v_interlock);
2902 return 0;
2903 }
2904
2905 /*
2906 * If there are no pages, don't do anything.
2907 */
2908 if (vp->v_uobj.uo_npages == 0) {
2909 if (TAILQ_EMPTY(&vp->v_uobj.memq) &&
2910 (vp->v_iflag & VI_ONWORKLST) &&
2911 LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
2912 vp->v_iflag &= ~VI_WRMAPDIRTY;
2913 vn_syncer_remove_from_worklist(vp);
2914 }
2915 mutex_exit(vp->v_interlock);
2916
2917 /* Remove us from paging queue, if we were on it */
2918 mutex_enter(&lfs_lock);
2919 if (ip->i_flags & IN_PAGING) {
2920 ip->i_flags &= ~IN_PAGING;
2921 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
2922 }
2923 mutex_exit(&lfs_lock);
2924
2925 KASSERT(!mutex_owned(vp->v_interlock));
2926 return 0;
2927 }
2928
2929 blkeof = blkroundup(fs, ip->i_size);
2930
2931 /*
2932 * Ignore requests to free pages past EOF but in the same block
2933 * as EOF, unless the vnode is being reclaimed or the request
2934 * is synchronous. (If the request is sync, it comes from
2935 * lfs_truncate.)
2936 *
2937 * To avoid being flooded with this request, make these pages
2938 * look "active".
2939 */
2940 if (!sync && !reclaim &&
2941 ap->a_offlo >= ip->i_size && ap->a_offlo < blkeof) {
2942 origoffset = ap->a_offlo;
2943 for (off = origoffset; off < blkeof; off += fs->lfs_bsize) {
2944 pg = uvm_pagelookup(&vp->v_uobj, off);
2945 KASSERT(pg != NULL);
2946 while (pg->flags & PG_BUSY) {
2947 pg->flags |= PG_WANTED;
2948 UVM_UNLOCK_AND_WAIT(pg, vp->v_interlock, 0,
2949 "lfsput2", 0);
2950 mutex_enter(vp->v_interlock);
2951 }
2952 mutex_enter(&uvm_pageqlock);
2953 uvm_pageactivate(pg);
2954 mutex_exit(&uvm_pageqlock);
2955 }
2956 ap->a_offlo = blkeof;
2957 if (ap->a_offhi > 0 && ap->a_offhi <= ap->a_offlo) {
2958 mutex_exit(vp->v_interlock);
2959 return 0;
2960 }
2961 }
2962
2963 /*
2964 * Extend page range to start and end at block boundaries.
2965 * (For the purposes of VOP_PUTPAGES, fragments don't exist.)
2966 */
2967 origoffset = ap->a_offlo;
2968 origendoffset = ap->a_offhi;
2969 startoffset = origoffset & ~(fs->lfs_bmask);
2970 max_endoffset = (trunc_page(LLONG_MAX) >> fs->lfs_bshift)
2971 << fs->lfs_bshift;
2972
2973 if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
2974 endoffset = max_endoffset;
2975 origendoffset = endoffset;
2976 } else {
2977 origendoffset = round_page(ap->a_offhi);
2978 endoffset = round_page(blkroundup(fs, origendoffset));
2979 }
2980
2981 KASSERT(startoffset > 0 || endoffset >= startoffset);
2982 if (startoffset == endoffset) {
2983 /* Nothing to do, why were we called? */
2984 mutex_exit(vp->v_interlock);
2985 DLOG((DLOG_PAGE, "lfs_putpages: startoffset = endoffset = %"
2986 PRId64 "\n", startoffset));
2987 return 0;
2988 }
2989
2990 ap->a_offlo = startoffset;
2991 ap->a_offhi = endoffset;
2992
2993 /*
2994 * If not cleaning, just send the pages through genfs_putpages
2995 * to be returned to the pool.
2996 */
2997 if (!(ap->a_flags & PGO_CLEANIT)) {
2998 DLOG((DLOG_PAGE, "lfs_putpages: no cleanit vn %p ino %d (flags %x)\n",
2999 vp, (int)ip->i_number, ap->a_flags));
3000 int r = genfs_putpages(v);
3001 KASSERT(!mutex_owned(vp->v_interlock));
3002 return r;
3003 }
3004
3005 /* Set PGO_BUSYFAIL to avoid deadlocks */
3006 ap->a_flags |= PGO_BUSYFAIL;
3007
3008 /*
3009 * Likewise, if we are asked to clean but the pages are not
3010 * dirty, we can just free them using genfs_putpages.
3011 */
3012 #ifdef DEBUG
3013 debug_n_dirtyclean = 0;
3014 #endif
3015 do {
3016 int r;
3017 KASSERT(mutex_owned(vp->v_interlock));
3018
3019 /* Count the number of dirty pages */
3020 r = check_dirty(fs, vp, startoffset, endoffset, blkeof,
3021 ap->a_flags, 1, NULL);
3022 if (r < 0) {
3023 /* Pages are busy with another process */
3024 mutex_exit(vp->v_interlock);
3025 return EDEADLK;
3026 }
3027 if (r > 0) /* Some pages are dirty */
3028 break;
3029
3030 /*
3031 * Sometimes pages are dirtied between the time that
3032 * we check and the time we try to clean them.
3033 * Instruct lfs_gop_write to return EDEADLK in this case
3034 * so we can write them properly.
3035 */
3036 ip->i_lfs_iflags |= LFSI_NO_GOP_WRITE;
3037 r = genfs_do_putpages(vp, startoffset, endoffset,
3038 ap->a_flags & ~PGO_SYNCIO, &busypg);
3039 ip->i_lfs_iflags &= ~LFSI_NO_GOP_WRITE;
3040 if (r != EDEADLK) {
3041 KASSERT(!mutex_owned(vp->v_interlock));
3042 return r;
3043 }
3044
3045 /* One of the pages was busy. Start over. */
3046 mutex_enter(vp->v_interlock);
3047 wait_for_page(vp, busypg, "dirtyclean");
3048 #ifdef DEBUG
3049 ++debug_n_dirtyclean;
3050 #endif
3051 } while(1);
3052
3053 #ifdef DEBUG
3054 if (debug_n_dirtyclean > TOOMANY)
3055 DLOG((DLOG_PAGE, "lfs_putpages: dirtyclean: looping, n = %d\n",
3056 debug_n_dirtyclean));
3057 #endif
3058
3059 /*
3060 * Dirty and asked to clean.
3061 *
3062 * Pagedaemon can't actually write LFS pages; wake up
3063 * the writer to take care of that. The writer will
3064 * notice the pager inode queue and act on that.
3065 *
3066 * XXX We must drop the vp->interlock before taking the lfs_lock or we
3067 * get a nasty deadlock with lfs_flush_pchain().
3068 */
3069 if (pagedaemon) {
3070 mutex_exit(vp->v_interlock);
3071 mutex_enter(&lfs_lock);
3072 if (!(ip->i_flags & IN_PAGING)) {
3073 ip->i_flags |= IN_PAGING;
3074 TAILQ_INSERT_TAIL(&fs->lfs_pchainhd, ip, i_lfs_pchain);
3075 }
3076 wakeup(&lfs_writer_daemon);
3077 mutex_exit(&lfs_lock);
3078 preempt();
3079 KASSERT(!mutex_owned(vp->v_interlock));
3080 return EWOULDBLOCK;
3081 }
3082
3083 /*
3084 * If this is a file created in a recent dirop, we can't flush its
3085 * inode until the dirop is complete. Drain dirops, then flush the
3086 * filesystem (taking care of any other pending dirops while we're
3087 * at it).
3088 */
3089 if ((ap->a_flags & (PGO_CLEANIT|PGO_LOCKED)) == PGO_CLEANIT &&
3090 (vp->v_uflag & VU_DIROP)) {
3091 DLOG((DLOG_PAGE, "lfs_putpages: flushing VU_DIROP\n"));
3092
3093 lfs_writer_enter(fs, "ppdirop");
3094
3095 /* Note if we hold the vnode locked */
3096 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
3097 {
3098 DLOG((DLOG_PAGE, "lfs_putpages: dirop inode already locked\n"));
3099 } else {
3100 DLOG((DLOG_PAGE, "lfs_putpages: dirop inode not locked\n"));
3101 }
3102 mutex_exit(vp->v_interlock);
3103
3104 mutex_enter(&lfs_lock);
3105 lfs_flush_fs(fs, sync ? SEGM_SYNC : 0);
3106 mutex_exit(&lfs_lock);
3107
3108 mutex_enter(vp->v_interlock);
3109 lfs_writer_leave(fs);
3110
3111 /* The flush will have cleaned out this vnode as well,
3112 no need to do more to it. */
3113 }
3114
3115 /*
3116 * This is it. We are going to write some pages. From here on
3117 * down it's all just mechanics.
3118 *
3119 * Don't let genfs_putpages wait; lfs_segunlock will wait for us.
3120 */
3121 ap->a_flags &= ~PGO_SYNCIO;
3122
3123 /*
3124 * If we've already got the seglock, flush the node and return.
3125 * The FIP has already been set up for us by lfs_writefile,
3126 * and FIP cleanup and lfs_updatemeta will also be done there,
3127 * unless genfs_putpages returns EDEADLK; then we must flush
3128 * what we have, and correct FIP and segment header accounting.
3129 */
3130 get_seglock:
3131 /*
3132 * If we are not called with the segment locked, lock it.
3133 * Account for a new FIP in the segment header, and set sp->vp.
3134 * (This should duplicate the setup at the top of lfs_writefile().)
3135 */
3136 seglocked = (ap->a_flags & PGO_LOCKED) != 0;
3137 if (!seglocked) {
3138 mutex_exit(vp->v_interlock);
3139 error = lfs_seglock(fs, SEGM_PROT | (sync ? SEGM_SYNC : 0));
3140 if (error != 0) {
3141 KASSERT(!mutex_owned(vp->v_interlock));
3142 return error;
3143 }
3144 mutex_enter(vp->v_interlock);
3145 lfs_acquire_finfo(fs, ip->i_number, ip->i_gen);
3146 }
3147 sp = fs->lfs_sp;
3148 KASSERT(sp->vp == NULL);
3149 sp->vp = vp;
3150
3151 /* Note segments written by reclaim; only for debugging */
3152 if ((vp->v_iflag & VI_XLOCK) != 0) {
3153 sp->seg_flags |= SEGM_RECLAIM;
3154 fs->lfs_reclino = ip->i_number;
3155 }
3156
3157 /*
3158 * Ensure that the partial segment is marked SS_DIROP if this
3159 * vnode is a DIROP.
3160 */
3161 if (!seglocked && vp->v_uflag & VU_DIROP)
3162 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
3163
3164 /*
3165 * Loop over genfs_putpages until all pages are gathered.
3166 * genfs_putpages() drops the interlock, so reacquire it if necessary.
3167 * Whenever we lose the interlock we have to rerun check_dirty, as
3168 * well, since more pages might have been dirtied in our absence.
3169 */
3170 #ifdef DEBUG
3171 debug_n_again = 0;
3172 #endif
3173 do {
3174 busypg = NULL;
3175 KASSERT(mutex_owned(vp->v_interlock));
3176 if (check_dirty(fs, vp, startoffset, endoffset, blkeof,
3177 ap->a_flags, 0, &busypg) < 0) {
3178 mutex_exit(vp->v_interlock);
3179 /* XXX why? --ks */
3180 mutex_enter(vp->v_interlock);
3181 write_and_wait(fs, vp, busypg, seglocked, NULL);
3182 if (!seglocked) {
3183 mutex_exit(vp->v_interlock);
3184 lfs_release_finfo(fs);
3185 lfs_segunlock(fs);
3186 mutex_enter(vp->v_interlock);
3187 }
3188 sp->vp = NULL;
3189 goto get_seglock;
3190 }
3191
3192 busypg = NULL;
3193 KASSERT(!mutex_owned(&uvm_pageqlock));
3194 oreclaim = (ap->a_flags & PGO_RECLAIM);
3195 ap->a_flags &= ~PGO_RECLAIM;
3196 error = genfs_do_putpages(vp, startoffset, endoffset,
3197 ap->a_flags, &busypg);
3198 ap->a_flags |= oreclaim;
3199
3200 if (error == EDEADLK || error == EAGAIN) {
3201 DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned"
3202 " %d ino %d off %x (seg %d)\n", error,
3203 ip->i_number, fs->lfs_offset,
3204 dtosn(fs, fs->lfs_offset)));
3205
3206 if (oreclaim) {
3207 mutex_enter(vp->v_interlock);
3208 write_and_wait(fs, vp, busypg, seglocked, "again");
3209 mutex_exit(vp->v_interlock);
3210 } else {
3211 if ((sp->seg_flags & SEGM_SINGLE) &&
3212 fs->lfs_curseg != fs->lfs_startseg)
3213 donewriting = 1;
3214 }
3215 } else if (error) {
3216 DLOG((DLOG_PAGE, "lfs_putpages: genfs_putpages returned"
3217 " %d ino %d off %x (seg %d)\n", error,
3218 (int)ip->i_number, fs->lfs_offset,
3219 dtosn(fs, fs->lfs_offset)));
3220 }
3221 /* genfs_do_putpages loses the interlock */
3222 #ifdef DEBUG
3223 ++debug_n_again;
3224 #endif
3225 if (oreclaim && error == EAGAIN) {
3226 DLOG((DLOG_PAGE, "vp %p ino %d vi_flags %x a_flags %x avoiding vclean panic\n",
3227 vp, (int)ip->i_number, vp->v_iflag, ap->a_flags));
3228 mutex_enter(vp->v_interlock);
3229 }
3230 if (error == EDEADLK)
3231 mutex_enter(vp->v_interlock);
3232 } while (error == EDEADLK || (oreclaim && error == EAGAIN));
3233 #ifdef DEBUG
3234 if (debug_n_again > TOOMANY)
3235 DLOG((DLOG_PAGE, "lfs_putpages: again: looping, n = %d\n", debug_n_again));
3236 #endif
3237
3238 KASSERT(sp != NULL && sp->vp == vp);
3239 if (!seglocked && !donewriting) {
3240 sp->vp = NULL;
3241
3242 /* Write indirect blocks as well */
3243 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_indir);
3244 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_dindir);
3245 lfs_gather(fs, fs->lfs_sp, vp, lfs_match_tindir);
3246
3247 KASSERT(sp->vp == NULL);
3248 sp->vp = vp;
3249 }
3250
3251 /*
3252 * Blocks are now gathered into a segment waiting to be written.
3253 * All that's left to do is update metadata, and write them.
3254 */
3255 lfs_updatemeta(sp);
3256 KASSERT(sp->vp == vp);
3257 sp->vp = NULL;
3258
3259 /*
3260 * If we were called from lfs_writefile, we don't need to clean up
3261 * the FIP or unlock the segment lock. We're done.
3262 */
3263 if (seglocked) {
3264 KASSERT(!mutex_owned(vp->v_interlock));
3265 return error;
3266 }
3267
3268 /* Clean up FIP and send it to disk. */
3269 lfs_release_finfo(fs);
3270 lfs_writeseg(fs, fs->lfs_sp);
3271
3272 /*
3273 * Remove us from paging queue if we wrote all our pages.
3274 */
3275 if (origendoffset == 0 || ap->a_flags & PGO_ALLPAGES) {
3276 mutex_enter(&lfs_lock);
3277 if (ip->i_flags & IN_PAGING) {
3278 ip->i_flags &= ~IN_PAGING;
3279 TAILQ_REMOVE(&fs->lfs_pchainhd, ip, i_lfs_pchain);
3280 }
3281 mutex_exit(&lfs_lock);
3282 }
3283
3284 /*
3285 * XXX - with the malloc/copy writeseg, the pages are freed by now
3286 * even if we don't wait (e.g. if we hold a nested lock). This
3287 * will not be true if we stop using malloc/copy.
3288 */
3289 KASSERT(fs->lfs_sp->seg_flags & SEGM_PROT);
3290 lfs_segunlock(fs);
3291
3292 /*
3293 * Wait for v_numoutput to drop to zero. The seglock should
3294 * take care of this, but there is a slight possibility that
3295 * aiodoned might not have got around to our buffers yet.
3296 */
3297 if (sync) {
3298 mutex_enter(vp->v_interlock);
3299 while (vp->v_numoutput > 0) {
3300 DLOG((DLOG_PAGE, "lfs_putpages: ino %d sleeping on"
3301 " num %d\n", ip->i_number, vp->v_numoutput));
3302 cv_wait(&vp->v_cv, vp->v_interlock);
3303 }
3304 mutex_exit(vp->v_interlock);
3305 }
3306 KASSERT(!mutex_owned(vp->v_interlock));
3307 return error;
3308 }
3309
3310 /*
3311 * Return the last logical file offset that should be written for this file
3312 * if we're doing a write that ends at "size". If writing, we need to know
3313 * about sizes on disk, i.e. fragments if there are any; if reading, we need
3314 * to know about entire blocks.
3315 */
3316 void
3317 lfs_gop_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
3318 {
3319 struct inode *ip = VTOI(vp);
3320 struct lfs *fs = ip->i_lfs;
3321 daddr_t olbn, nlbn;
3322
3323 olbn = lblkno(fs, ip->i_size);
3324 nlbn = lblkno(fs, size);
3325 if (!(flags & GOP_SIZE_MEM) && nlbn < NDADDR && olbn <= nlbn) {
3326 *eobp = fragroundup(fs, size);
3327 } else {
3328 *eobp = blkroundup(fs, size);
3329 }
3330 }
3331
3332 #ifdef DEBUG
3333 void lfs_dump_vop(void *);
3334
3335 void
3336 lfs_dump_vop(void *v)
3337 {
3338 struct vop_putpages_args /* {
3339 struct vnode *a_vp;
3340 voff_t a_offlo;
3341 voff_t a_offhi;
3342 int a_flags;
3343 } */ *ap = v;
3344
3345 #ifdef DDB
3346 vfs_vnode_print(ap->a_vp, 0, printf);
3347 #endif
3348 lfs_dump_dinode(VTOI(ap->a_vp)->i_din.ffs1_din);
3349 }
3350 #endif
3351
3352 int
3353 lfs_mmap(void *v)
3354 {
3355 struct vop_mmap_args /* {
3356 const struct vnodeop_desc *a_desc;
3357 struct vnode *a_vp;
3358 vm_prot_t a_prot;
3359 kauth_cred_t a_cred;
3360 } */ *ap = v;
3361
3362 if (VTOI(ap->a_vp)->i_number == LFS_IFILE_INUM)
3363 return EOPNOTSUPP;
3364 return ufs_mmap(v);
3365 }
3366