Merge branch 'vendor/BINUTILS_ALL'
[dragonfly.git] / sys / vm / vm_swapcache.c
1 /*
2  * (MPSAFE)
3  *
4  * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
5  *
6  * This code is derived from software contributed to The DragonFly Project
7  * by Matthew Dillon <dillon@backplane.com>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
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
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  * 3. Neither the name of The DragonFly Project nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific, prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
27  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 /*
38  * Implement the swapcache daemon.  When enabled swap is assumed to be
39  * configured on a fast storage device such as a SSD.  Swap is assigned
40  * to clean vnode-backed pages in the inactive queue, clustered by object
41  * if possible, and written out.  The swap assignment sticks around even
42  * after the underlying pages have been recycled.
43  *
44  * The daemon manages write bandwidth based on sysctl settings to control
45  * wear on the SSD.
46  *
47  * The vnode strategy code will check for the swap assignments and divert
48  * reads to the swap device when the data is present in the swapcache.
49  *
50  * This operates on both regular files and the block device vnodes used by
51  * filesystems to manage meta-data.
52  */
53
54 #include "opt_vm.h"
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/proc.h>
59 #include <sys/kthread.h>
60 #include <sys/resourcevar.h>
61 #include <sys/signalvar.h>
62 #include <sys/vnode.h>
63 #include <sys/vmmeter.h>
64 #include <sys/sysctl.h>
65 #include <sys/eventhandler.h>
66
67 #include <vm/vm.h>
68 #include <vm/vm_param.h>
69 #include <sys/lock.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_page.h>
72 #include <vm/vm_map.h>
73 #include <vm/vm_pageout.h>
74 #include <vm/vm_pager.h>
75 #include <vm/swap_pager.h>
76 #include <vm/vm_extern.h>
77
78 #include <sys/thread2.h>
79 #include <sys/spinlock2.h>
80 #include <vm/vm_page2.h>
81
82 /* the kernel process "vm_pageout"*/
83 static int vm_swapcached_flush (vm_page_t m, int isblkdev);
84 static int vm_swapcache_test(vm_page_t m);
85 static void vm_swapcache_writing(vm_page_t marker);
86 static void vm_swapcache_cleaning(vm_object_t marker);
87 struct thread *swapcached_thread;
88
89 SYSCTL_NODE(_vm, OID_AUTO, swapcache, CTLFLAG_RW, NULL, NULL);
90
91 int vm_swapcache_read_enable;
92 int vm_swapcache_inactive_heuristic;
93 static int vm_swapcache_sleep;
94 static int vm_swapcache_maxlaunder = 256;
95 static int vm_swapcache_data_enable = 0;
96 static int vm_swapcache_meta_enable = 0;
97 static int vm_swapcache_maxswappct = 75;
98 static int vm_swapcache_hysteresis;
99 int vm_swapcache_use_chflags = 1;       /* require chflags cache */
100 static int64_t vm_swapcache_minburst = 10000000LL;      /* 10MB */
101 static int64_t vm_swapcache_curburst = 4000000000LL;    /* 4G after boot */
102 static int64_t vm_swapcache_maxburst = 2000000000LL;    /* 2G nominal max */
103 static int64_t vm_swapcache_accrate = 100000LL;         /* 100K/s */
104 static int64_t vm_swapcache_write_count;
105 static int64_t vm_swapcache_maxfilesize;
106
107 SYSCTL_INT(_vm_swapcache, OID_AUTO, maxlaunder,
108         CTLFLAG_RW, &vm_swapcache_maxlaunder, 0, "");
109
110 SYSCTL_INT(_vm_swapcache, OID_AUTO, data_enable,
111         CTLFLAG_RW, &vm_swapcache_data_enable, 0, "");
112 SYSCTL_INT(_vm_swapcache, OID_AUTO, meta_enable,
113         CTLFLAG_RW, &vm_swapcache_meta_enable, 0, "");
114 SYSCTL_INT(_vm_swapcache, OID_AUTO, read_enable,
115         CTLFLAG_RW, &vm_swapcache_read_enable, 0, "");
116 SYSCTL_INT(_vm_swapcache, OID_AUTO, maxswappct,
117         CTLFLAG_RW, &vm_swapcache_maxswappct, 0, "");
118 SYSCTL_INT(_vm_swapcache, OID_AUTO, hysteresis,
119         CTLFLAG_RW, &vm_swapcache_hysteresis, 0, "");
120 SYSCTL_INT(_vm_swapcache, OID_AUTO, use_chflags,
121         CTLFLAG_RW, &vm_swapcache_use_chflags, 0, "");
122
123 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, minburst,
124         CTLFLAG_RW, &vm_swapcache_minburst, 0, "");
125 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, curburst,
126         CTLFLAG_RW, &vm_swapcache_curburst, 0, "");
127 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxburst,
128         CTLFLAG_RW, &vm_swapcache_maxburst, 0, "");
129 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, maxfilesize,
130         CTLFLAG_RW, &vm_swapcache_maxfilesize, 0, "");
131 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, accrate,
132         CTLFLAG_RW, &vm_swapcache_accrate, 0, "");
133 SYSCTL_QUAD(_vm_swapcache, OID_AUTO, write_count,
134         CTLFLAG_RW, &vm_swapcache_write_count, 0, "");
135
136 #define SWAPMAX(adj)    \
137         ((int64_t)vm_swap_max * (vm_swapcache_maxswappct + (adj)) / 100)
138
139 /*
140  * When shutting down the machine we want to stop swapcache operation
141  * immediately so swap is not accessed after devices have been shuttered.
142  */
143 static void
144 shutdown_swapcache(void *arg __unused)
145 {
146         vm_swapcache_read_enable = 0;
147         vm_swapcache_data_enable = 0;
148         vm_swapcache_meta_enable = 0;
149         wakeup(&vm_swapcache_sleep);    /* shortcut 5-second wait */
150 }
151
152 /*
153  * vm_swapcached is the high level pageout daemon.
154  *
155  * No requirements.
156  */
157 static void
158 vm_swapcached_thread(void)
159 {
160         enum { SWAPC_WRITING, SWAPC_CLEANING } state = SWAPC_WRITING;
161         enum { SWAPB_BURSTING, SWAPB_RECOVERING } burst = SWAPB_BURSTING;
162         static struct vm_page page_marker[PQ_L2_SIZE];
163         static struct vm_object object_marker;
164         int q;
165
166         /*
167          * Thread setup
168          */
169         curthread->td_flags |= TDF_SYSTHREAD;
170         EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc,
171                               swapcached_thread, SHUTDOWN_PRI_FIRST);
172         EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_swapcache,
173                               NULL, SHUTDOWN_PRI_SECOND);
174
175         /*
176          * Initialize our marker for the inactive scan (SWAPC_WRITING)
177          */
178         bzero(&page_marker, sizeof(page_marker));
179         for (q = 0; q < PQ_L2_SIZE; ++q) {
180                 page_marker[q].flags = PG_BUSY | PG_FICTITIOUS | PG_MARKER;
181                 page_marker[q].queue = PQ_INACTIVE + q;
182                 page_marker[q].pc = q;
183                 page_marker[q].wire_count = 1;
184                 vm_page_queues_spin_lock(PQ_INACTIVE + q);
185                 TAILQ_INSERT_HEAD(
186                         &vm_page_queues[PQ_INACTIVE + q].pl,
187                         &page_marker[q], pageq);
188                 vm_page_queues_spin_unlock(PQ_INACTIVE + q);
189         }
190
191         vm_swapcache_hysteresis = vmstats.v_inactive_target / 2;
192         vm_swapcache_inactive_heuristic = -vm_swapcache_hysteresis;
193
194         /*
195          * Initialize our marker for the vm_object scan (SWAPC_CLEANING)
196          */
197         bzero(&object_marker, sizeof(object_marker));
198         object_marker.type = OBJT_MARKER;
199         lwkt_gettoken(&vmobj_token);
200         TAILQ_INSERT_HEAD(&vm_object_list, &object_marker, object_list);
201         lwkt_reltoken(&vmobj_token);
202
203         for (;;) {
204                 /*
205                  * Handle shutdown
206                  */
207                 kproc_suspend_loop();
208
209                 /*
210                  * Check every 5 seconds when not enabled or if no swap
211                  * is present.
212                  */
213                 if ((vm_swapcache_data_enable == 0 &&
214                      vm_swapcache_meta_enable == 0) ||
215                     vm_swap_max == 0) {
216                         tsleep(&vm_swapcache_sleep, 0, "csleep", hz * 5);
217                         continue;
218                 }
219
220                 /*
221                  * Polling rate when enabled is approximately 10 hz.
222                  */
223                 tsleep(&vm_swapcache_sleep, 0, "csleep", hz / 10);
224
225                 /*
226                  * State hysteresis.  Generate write activity up to 75% of
227                  * swap, then clean out swap assignments down to 70%, then
228                  * repeat.
229                  */
230                 if (state == SWAPC_WRITING) {
231                         if (vm_swap_cache_use > SWAPMAX(0))
232                                 state = SWAPC_CLEANING;
233                 } else {
234                         if (vm_swap_cache_use < SWAPMAX(-10))
235                                 state = SWAPC_WRITING;
236                 }
237
238                 /*
239                  * We are allowed to continue accumulating burst value
240                  * in either state.  Allow the user to set curburst > maxburst
241                  * for the initial load-in.
242                  */
243                 if (vm_swapcache_curburst < vm_swapcache_maxburst) {
244                         vm_swapcache_curburst += vm_swapcache_accrate / 10;
245                         if (vm_swapcache_curburst > vm_swapcache_maxburst)
246                                 vm_swapcache_curburst = vm_swapcache_maxburst;
247                 }
248
249                 /*
250                  * We don't want to nickle-and-dime the scan as that will
251                  * create unnecessary fragmentation.  The minimum burst
252                  * is one-seconds worth of accumulation.
253                  */
254                 if (state == SWAPC_WRITING) {
255                         if (vm_swapcache_curburst >= vm_swapcache_accrate) {
256                                 if (burst == SWAPB_BURSTING) {
257                                         for (q = 0; q < PQ_L2_SIZE; ++q) {
258                                                 vm_swapcache_writing(
259                                                         &page_marker[q]);
260                                         }
261                                         if (vm_swapcache_curburst <= 0)
262                                                 burst = SWAPB_RECOVERING;
263                                 } else if (vm_swapcache_curburst >
264                                            vm_swapcache_minburst) {
265                                         for (q = 0; q < PQ_L2_SIZE; ++q) {
266                                                 vm_swapcache_writing(
267                                                         &page_marker[q]);
268                                         }
269                                         burst = SWAPB_BURSTING;
270                                 }
271                         }
272                 } else {
273                         vm_swapcache_cleaning(&object_marker);
274                 }
275         }
276
277         /*
278          * Cleanup (NOT REACHED)
279          */
280         for (q = 0; q < PQ_L2_SIZE; ++q) {
281                 vm_page_queues_spin_lock(PQ_INACTIVE + q);
282                 TAILQ_REMOVE(
283                         &vm_page_queues[PQ_INACTIVE + q].pl,
284                         &page_marker[q], pageq);
285                 vm_page_queues_spin_unlock(PQ_INACTIVE + q);
286         }
287
288         lwkt_gettoken(&vmobj_token);
289         TAILQ_REMOVE(&vm_object_list, &object_marker, object_list);
290         lwkt_reltoken(&vmobj_token);
291 }
292
293 static struct kproc_desc swpc_kp = {
294         "swapcached",
295         vm_swapcached_thread,
296         &swapcached_thread
297 };
298 SYSINIT(swapcached, SI_SUB_KTHREAD_PAGE, SI_ORDER_SECOND, kproc_start, &swpc_kp)
299
300 static void
301 vm_swapcache_writing(vm_page_t marker)
302 {
303         vm_object_t object;
304         struct vnode *vp;
305         vm_page_t m;
306         int count;
307         int isblkdev;
308
309         /*
310          * Deal with an overflow of the heuristic counter or if the user
311          * manually changes the hysteresis.
312          *
313          * Try to avoid small incremental pageouts by waiting for enough
314          * pages to buildup in the inactive queue to hopefully get a good
315          * burst in.  This heuristic is bumped by the VM system and reset
316          * when our scan hits the end of the queue.
317          */
318         if (vm_swapcache_inactive_heuristic < -vm_swapcache_hysteresis)
319                 vm_swapcache_inactive_heuristic = -vm_swapcache_hysteresis;
320         if (vm_swapcache_inactive_heuristic < 0)
321                 return;
322
323         /*
324          * Scan the inactive queue from our marker to locate
325          * suitable pages to push to the swap cache.
326          *
327          * We are looking for clean vnode-backed pages.
328          *
329          * NOTE: PG_SWAPPED pages in particular are not part of
330          *       our count because once the cache stabilizes we
331          *       can end up with a very high datarate of VM pages
332          *       cycling from it.
333          */
334         count = vm_swapcache_maxlaunder;
335
336         vm_page_queues_spin_lock(marker->queue);
337         while ((m = TAILQ_NEXT(marker, pageq)) != NULL && count-- > 0) {
338                 KKASSERT(m->queue == marker->queue);
339
340                 if (vm_swapcache_curburst < 0)
341                         break;
342                 TAILQ_REMOVE(
343                         &vm_page_queues[marker->queue].pl, marker, pageq);
344                 TAILQ_INSERT_AFTER(
345                         &vm_page_queues[marker->queue].pl, m, marker, pageq);
346                 if (m->flags & (PG_MARKER | PG_SWAPPED)) {
347                         ++count;
348                         continue;
349                 }
350                 if (vm_page_busy_try(m, TRUE))
351                         continue;
352                 vm_page_queues_spin_unlock(marker->queue);
353
354                 if ((object = m->object) == NULL) {
355                         vm_page_wakeup(m);
356                         vm_page_queues_spin_lock(marker->queue);
357                         continue;
358                 }
359                 vm_object_hold(object);
360                 if (m->object != object) {
361                         vm_object_drop(object);
362                         vm_page_wakeup(m);
363                         vm_page_queues_spin_lock(marker->queue);
364                         continue;
365                 }
366                 if (vm_swapcache_test(m)) {
367                         vm_object_drop(object);
368                         vm_page_wakeup(m);
369                         vm_page_queues_spin_lock(marker->queue);
370                         continue;
371                 }
372
373                 vp = object->handle;
374                 if (vp == NULL) {
375                         vm_object_drop(object);
376                         vm_page_wakeup(m);
377                         vm_page_queues_spin_lock(marker->queue);
378                         continue;
379                 }
380
381                 switch(vp->v_type) {
382                 case VREG:
383                         /*
384                          * PG_NOTMETA generically means 'don't swapcache this',
385                          * and HAMMER will set this for regular data buffers
386                          * (and leave it unset for meta-data buffers) as
387                          * appropriate when double buffering is enabled.
388                          */
389                         if (m->flags & PG_NOTMETA) {
390                                 vm_object_drop(object);
391                                 vm_page_wakeup(m);
392                                 vm_page_queues_spin_lock(marker->queue);
393                                 continue;
394                         }
395
396                         /*
397                          * If data_enable is 0 do not try to swapcache data.
398                          * If use_chflags is set then only swapcache data for
399                          * VSWAPCACHE marked vnodes, otherwise any vnode.
400                          */
401                         if (vm_swapcache_data_enable == 0 ||
402                             ((vp->v_flag & VSWAPCACHE) == 0 &&
403                              vm_swapcache_use_chflags)) {
404                                 vm_object_drop(object);
405                                 vm_page_wakeup(m);
406                                 vm_page_queues_spin_lock(marker->queue);
407                                 continue;
408                         }
409                         if (vm_swapcache_maxfilesize &&
410                             object->size >
411                             (vm_swapcache_maxfilesize >> PAGE_SHIFT)) {
412                                 vm_object_drop(object);
413                                 vm_page_wakeup(m);
414                                 vm_page_queues_spin_lock(marker->queue);
415                                 continue;
416                         }
417                         isblkdev = 0;
418                         break;
419                 case VCHR:
420                         /*
421                          * PG_NOTMETA generically means 'don't swapcache this',
422                          * and HAMMER will set this for regular data buffers
423                          * (and leave it unset for meta-data buffers) as
424                          * appropriate when double buffering is enabled.
425                          */
426                         if (m->flags & PG_NOTMETA) {
427                                 vm_object_drop(object);
428                                 vm_page_wakeup(m);
429                                 vm_page_queues_spin_lock(marker->queue);
430                                 continue;
431                         }
432                         if (vm_swapcache_meta_enable == 0) {
433                                 vm_object_drop(object);
434                                 vm_page_wakeup(m);
435                                 vm_page_queues_spin_lock(marker->queue);
436                                 continue;
437                         }
438                         isblkdev = 1;
439                         break;
440                 default:
441                         vm_object_drop(object);
442                         vm_page_wakeup(m);
443                         vm_page_queues_spin_lock(marker->queue);
444                         continue;
445                 }
446
447
448                 /*
449                  * Assign swap and initiate I/O.
450                  *
451                  * (adjust for the --count which also occurs in the loop)
452                  */
453                 count -= vm_swapcached_flush(m, isblkdev) - 1;
454
455                 /*
456                  * Setup for next loop using marker.
457                  */
458                 vm_object_drop(object);
459                 vm_page_queues_spin_lock(marker->queue);
460         }
461
462         /*
463          * The marker could wind up at the end, which is ok.  If we hit the
464          * end of the list adjust the heuristic.
465          *
466          * Earlier inactive pages that were dirty and become clean
467          * are typically moved to the end of PQ_INACTIVE by virtue
468          * of vfs_vmio_release() when they become unwired from the
469          * buffer cache.
470          */
471         if (m == NULL)
472                 vm_swapcache_inactive_heuristic = -vm_swapcache_hysteresis;
473         vm_page_queues_spin_unlock(marker->queue);
474 }
475
476 /*
477  * Flush the specified page using the swap_pager.  The page
478  * must be busied by the caller and its disposition will become
479  * the responsibility of this function.
480  *
481  * Try to collect surrounding pages, including pages which may
482  * have already been assigned swap.  Try to cluster within a
483  * contiguous aligned SMAP_META_PAGES (typ 16 x PAGE_SIZE) block
484  * to match what swap_pager_putpages() can do.
485  *
486  * We also want to try to match against the buffer cache blocksize
487  * but we don't really know what it is here.  Since the buffer cache
488  * wires and unwires pages in groups the fact that we skip wired pages
489  * should be sufficient.
490  *
491  * Returns a count of pages we might have flushed (minimum 1)
492  */
493 static
494 int
495 vm_swapcached_flush(vm_page_t m, int isblkdev)
496 {
497         vm_object_t object;
498         vm_page_t marray[SWAP_META_PAGES];
499         vm_pindex_t basei;
500         int rtvals[SWAP_META_PAGES];
501         int x;
502         int i;
503         int j;
504         int count;
505         int error;
506
507         vm_page_io_start(m);
508         vm_page_protect(m, VM_PROT_READ);
509         object = m->object;
510         vm_object_hold(object);
511
512         /*
513          * Try to cluster around (m), keeping in mind that the swap pager
514          * can only do SMAP_META_PAGES worth of continguous write.
515          */
516         x = (int)m->pindex & SWAP_META_MASK;
517         marray[x] = m;
518         basei = m->pindex;
519         vm_page_wakeup(m);
520
521         for (i = x - 1; i >= 0; --i) {
522                 m = vm_page_lookup_busy_try(object, basei - x + i,
523                                             TRUE, &error);
524                 if (error || m == NULL)
525                         break;
526                 if (vm_swapcache_test(m)) {
527                         vm_page_wakeup(m);
528                         break;
529                 }
530                 if (isblkdev && (m->flags & PG_NOTMETA)) {
531                         vm_page_wakeup(m);
532                         break;
533                 }
534                 vm_page_io_start(m);
535                 vm_page_protect(m, VM_PROT_READ);
536                 if (m->queue - m->pc == PQ_CACHE) {
537                         vm_page_unqueue_nowakeup(m);
538                         vm_page_deactivate(m);
539                 }
540                 marray[i] = m;
541                 vm_page_wakeup(m);
542         }
543         ++i;
544
545         for (j = x + 1; j < SWAP_META_PAGES; ++j) {
546                 m = vm_page_lookup_busy_try(object, basei - x + j,
547                                             TRUE, &error);
548                 if (error || m == NULL)
549                         break;
550                 if (vm_swapcache_test(m)) {
551                         vm_page_wakeup(m);
552                         break;
553                 }
554                 if (isblkdev && (m->flags & PG_NOTMETA)) {
555                         vm_page_wakeup(m);
556                         break;
557                 }
558                 vm_page_io_start(m);
559                 vm_page_protect(m, VM_PROT_READ);
560                 if (m->queue - m->pc == PQ_CACHE) {
561                         vm_page_unqueue_nowakeup(m);
562                         vm_page_deactivate(m);
563                 }
564                 marray[j] = m;
565                 vm_page_wakeup(m);
566         }
567
568         count = j - i;
569         vm_object_pip_add(object, count);
570         swap_pager_putpages(object, marray + i, count, FALSE, rtvals + i);
571         vm_swapcache_write_count += count * PAGE_SIZE;
572         vm_swapcache_curburst -= count * PAGE_SIZE;
573
574         while (i < j) {
575                 if (rtvals[i] != VM_PAGER_PEND) {
576                         vm_page_busy_wait(marray[i], FALSE, "swppgfd");
577                         vm_page_io_finish(marray[i]);
578                         vm_page_wakeup(marray[i]);
579                         vm_object_pip_wakeup(object);
580                 }
581                 ++i;
582         }
583         vm_object_drop(object);
584         return(count);
585 }
586
587 /*
588  * Test whether a VM page is suitable for writing to the swapcache.
589  * Does not test m->queue, PG_MARKER, or PG_SWAPPED.
590  *
591  * Returns 0 on success, 1 on failure
592  */
593 static int
594 vm_swapcache_test(vm_page_t m)
595 {
596         vm_object_t object;
597
598         if (m->flags & PG_UNMANAGED)
599                 return(1);
600         if (m->hold_count || m->wire_count)
601                 return(1);
602         if (m->valid != VM_PAGE_BITS_ALL)
603                 return(1);
604         if (m->dirty & m->valid)
605                 return(1);
606         if ((object = m->object) == NULL)
607                 return(1);
608         if (object->type != OBJT_VNODE ||
609             (object->flags & OBJ_DEAD)) {
610                 return(1);
611         }
612         vm_page_test_dirty(m);
613         if (m->dirty & m->valid)
614                 return(1);
615         return(0);
616 }
617
618 /*
619  * Cleaning pass
620  */
621 static
622 void
623 vm_swapcache_cleaning(vm_object_t marker)
624 {
625         vm_object_t object;
626         struct vnode *vp;
627         int count;
628         int n;
629
630         object = marker;
631         count = vm_swapcache_maxlaunder;
632
633         /*
634          * Look for vnode objects
635          */
636         lwkt_gettoken(&vmobj_token);
637
638         while ((object = TAILQ_NEXT(object, object_list)) != NULL) {
639                 vm_object_hold(object);
640
641                 lwkt_yield();
642                 if (--count <= 0) {
643                         vm_object_drop(object);
644                         break;
645                 }
646
647                 /* 
648                  * Only operate on live VNODE objects with regular/chardev types
649                  */
650                 if ((object->type != OBJT_VNODE) ||
651                     ((object->flags & OBJ_DEAD) || object->swblock_count == 0) ||
652                     ((vp = object->handle) == NULL) ||
653                     (vp->v_type != VREG && vp->v_type != VCHR)) {
654                         vm_object_drop(object);
655                         continue;
656                 }
657
658                 /*
659                  * Adjust iterator.
660                  */
661                 if (marker->backing_object != object)
662                         marker->size = 0;
663
664                 /*
665                  * Move the marker so we can work on the VM object
666                  */
667                 TAILQ_REMOVE(&vm_object_list, marker, object_list);
668                 TAILQ_INSERT_AFTER(&vm_object_list, object,
669                                    marker, object_list);
670
671                 /*
672                  * Look for swblocks starting at our iterator.
673                  *
674                  * The swap_pager_condfree() function attempts to free
675                  * swap space starting at the specified index.  The index
676                  * will be updated on return.  The function will return
677                  * a scan factor (NOT the number of blocks freed).
678                  *
679                  * If it must cut its scan of the object short due to an
680                  * excessive number of swblocks, or is able to free the
681                  * requested number of blocks, it will return n >= count
682                  * and we break and pick it back up on a future attempt.
683                  */
684                 lwkt_token_swap();
685                 lwkt_reltoken(&vmobj_token);
686
687                 n = swap_pager_condfree(object, &marker->size, count);
688
689                 vm_object_drop(object);
690                 lwkt_gettoken(&vmobj_token);
691         
692                 count -= n;
693                 if (count < 0) 
694                         break;
695
696                 /*
697                  * Setup for loop.
698                  */
699                 marker->size = 0;
700                 object = marker;
701         }
702
703         /*
704          * Adjust marker so we continue the scan from where we left off.
705          * When we reach the end we start back at the beginning.
706          */
707         TAILQ_REMOVE(&vm_object_list, marker, object_list);
708         if (object)
709                 TAILQ_INSERT_BEFORE(object, marker, object_list);
710         else
711                 TAILQ_INSERT_HEAD(&vm_object_list, marker, object_list);
712         marker->backing_object = object;
713
714         lwkt_reltoken(&vmobj_token);
715 }