xref: /titanic_44/usr/src/uts/common/fs/swapfs/swap_vnops.c (revision 3289e1bdec732dbdf04517907516fd0d00f6a6f8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/buf.h>
32 #include <sys/cred.h>
33 #include <sys/errno.h>
34 #include <sys/vnode.h>
35 #include <sys/vfs_opreg.h>
36 #include <sys/cmn_err.h>
37 #include <sys/swap.h>
38 #include <sys/mman.h>
39 #include <sys/vmsystm.h>
40 #include <sys/vtrace.h>
41 #include <sys/debug.h>
42 #include <sys/sysmacros.h>
43 #include <sys/vm.h>
44 
45 #include <sys/fs/swapnode.h>
46 
47 #include <vm/seg.h>
48 #include <vm/page.h>
49 #include <vm/pvn.h>
50 #include <fs/fs_subr.h>
51 
52 #include <vm/seg_kp.h>
53 
54 /*
55  * Define the routines within this file.
56  */
57 static int	swap_getpage(struct vnode *vp, offset_t off, size_t len,
58     uint_t *protp, struct page **plarr, size_t plsz, struct seg *seg,
59     caddr_t addr, enum seg_rw rw, struct cred *cr, caller_context_t *ct);
60 static int	swap_putpage(struct vnode *vp, offset_t off, size_t len,
61     int flags, struct cred *cr, caller_context_t *ct);
62 static void	swap_inactive(struct vnode *vp, struct cred *cr,
63     caller_context_t *ct);
64 static void	swap_dispose(vnode_t *vp, page_t *pp, int fl, int dn,
65     cred_t *cr, caller_context_t *ct);
66 
67 static int	swap_getapage(struct vnode *vp, u_offset_t off, size_t len,
68     uint_t *protp, page_t **plarr, size_t plsz,
69     struct seg *seg, caddr_t addr, enum seg_rw rw, struct cred *cr);
70 
71 int	swap_getconpage(struct vnode *vp, u_offset_t off, size_t len,
72     uint_t *protp, page_t **plarr, size_t plsz, page_t *conpp,
73     uint_t *pszc, spgcnt_t *nreloc, struct seg *seg, caddr_t addr,
74     enum seg_rw rw, struct cred *cr);
75 
76 static int 	swap_putapage(struct vnode *vp, page_t *pp, u_offset_t *off,
77     size_t *lenp, int flags, struct cred *cr);
78 
79 const fs_operation_def_t swap_vnodeops_template[] = {
80 	VOPNAME_INACTIVE,	{ .vop_inactive = swap_inactive },
81 	VOPNAME_GETPAGE,	{ .vop_getpage = swap_getpage },
82 	VOPNAME_PUTPAGE,	{ .vop_putpage = swap_putpage },
83 	VOPNAME_DISPOSE,	{ .vop_dispose = swap_dispose },
84 	VOPNAME_SETFL,		{ .error = fs_error },
85 	VOPNAME_POLL,		{ .error = fs_error },
86 	VOPNAME_PATHCONF,	{ .error = fs_error },
87 	VOPNAME_GETSECATTR,	{ .error = fs_error },
88 	VOPNAME_SHRLOCK,	{ .error = fs_error },
89 	NULL,			NULL
90 };
91 
92 vnodeops_t *swap_vnodeops;
93 
94 /* ARGSUSED */
95 static void
96 swap_inactive(
97 	struct vnode *vp,
98 	struct cred *cr,
99 	caller_context_t *ct)
100 {
101 	SWAPFS_PRINT(SWAP_VOPS, "swap_inactive: vp %x\n", vp, 0, 0, 0, 0);
102 }
103 
104 /*
105  * Return all the pages from [off..off+len] in given file
106  */
107 /*ARGSUSED*/
108 static int
109 swap_getpage(
110 	struct vnode *vp,
111 	offset_t off,
112 	size_t len,
113 	uint_t *protp,
114 	page_t *pl[],
115 	size_t plsz,
116 	struct seg *seg,
117 	caddr_t addr,
118 	enum seg_rw rw,
119 	struct cred *cr,
120 	caller_context_t *ct)
121 {
122 	int err;
123 
124 	SWAPFS_PRINT(SWAP_VOPS, "swap_getpage: vp %p, off %llx, len %lx\n",
125 	    (void *)vp, off, len, 0, 0);
126 
127 	TRACE_3(TR_FAC_SWAPFS, TR_SWAPFS_GETPAGE,
128 	    "swapfs getpage:vp %p off %llx len %ld",
129 	    (void *)vp, off, len);
130 
131 	if (len <= PAGESIZE) {
132 		err = swap_getapage(vp, (u_offset_t)off, len, protp, pl, plsz,
133 		    seg, addr, rw, cr);
134 	} else {
135 		err = pvn_getpages(swap_getapage, vp, (u_offset_t)off, len,
136 		    protp, pl, plsz, seg, addr, rw, cr);
137 	}
138 
139 	return (err);
140 }
141 
142 /*
143  * Called from pvn_getpages or swap_getpage to get a particular page.
144  */
145 /*ARGSUSED*/
146 static int
147 swap_getapage(
148 	struct vnode *vp,
149 	u_offset_t off,
150 	size_t len,
151 	uint_t *protp,
152 	page_t *pl[],
153 	size_t plsz,
154 	struct seg *seg,
155 	caddr_t addr,
156 	enum seg_rw rw,
157 	struct cred *cr)
158 {
159 	struct page *pp, *rpp;
160 	int flags;
161 	int err = 0;
162 	struct vnode *pvp = NULL;
163 	u_offset_t poff;
164 	int flag_noreloc;
165 	se_t lock;
166 	extern int kcage_on;
167 	int upgrade = 0;
168 
169 	SWAPFS_PRINT(SWAP_VOPS, "swap_getapage: vp %p, off %llx, len %lx\n",
170 		vp, off, len, 0, 0);
171 
172 	/*
173 	 * Until there is a call-back mechanism to cause SEGKP
174 	 * pages to be unlocked, make them non-relocatable.
175 	 */
176 	if (SEG_IS_SEGKP(seg))
177 		flag_noreloc = PG_NORELOC;
178 	else
179 		flag_noreloc = 0;
180 
181 	if (protp != NULL)
182 		*protp = PROT_ALL;
183 
184 	lock = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
185 
186 again:
187 	if (pp = page_lookup(vp, off, lock)) {
188 		/*
189 		 * In very rare instances, a segkp page may have been
190 		 * relocated outside of the kernel by the kernel cage
191 		 * due to the window between page_unlock() and
192 		 * VOP_PUTPAGE() in segkp_unlock().  Due to the
193 		 * rareness of these occurances, the solution is to
194 		 * relocate the page to a P_NORELOC page.
195 		 */
196 		if (flag_noreloc != 0) {
197 			if (!PP_ISNORELOC(pp) && kcage_on) {
198 				if (lock != SE_EXCL) {
199 					upgrade = 1;
200 					if (!page_tryupgrade(pp)) {
201 						page_unlock(pp);
202 						lock = SE_EXCL;
203 						goto again;
204 					}
205 				}
206 
207 				if (page_relocate_cage(&pp, &rpp) != 0)
208 					panic("swap_getapage: "
209 					    "page_relocate_cage failed");
210 
211 				pp = rpp;
212 			}
213 		}
214 
215 		if (pl) {
216 			if (upgrade)
217 				page_downgrade(pp);
218 
219 			pl[0] = pp;
220 			pl[1] = NULL;
221 		} else {
222 			page_unlock(pp);
223 		}
224 	} else {
225 		pp = page_create_va(vp, off, PAGESIZE,
226 		    PG_WAIT | PG_EXCL | flag_noreloc,
227 		    seg, addr);
228 		/*
229 		 * Someone raced in and created the page after we did the
230 		 * lookup but before we did the create, so go back and
231 		 * try to look it up again.
232 		 */
233 		if (pp == NULL)
234 			goto again;
235 		if (rw != S_CREATE) {
236 			err = swap_getphysname(vp, off, &pvp, &poff);
237 			if (pvp) {
238 				struct anon *ap;
239 				kmutex_t *ahm;
240 
241 				flags = (pl == NULL ? B_ASYNC|B_READ : B_READ);
242 				err = VOP_PAGEIO(pvp, pp, poff,
243 				    PAGESIZE, flags, cr, NULL);
244 
245 				if (!err) {
246 					ahm = &anonhash_lock[AH_LOCK(vp, off)];
247 					mutex_enter(ahm);
248 
249 					ap = swap_anon(vp, off);
250 					if (ap == NULL)
251 					    panic("swap_getapage: null anon");
252 
253 					if (ap->an_pvp == pvp &&
254 					    ap->an_poff == poff) {
255 						swap_phys_free(pvp, poff,
256 						    PAGESIZE);
257 						ap->an_pvp = NULL;
258 						ap->an_poff = NULL;
259 						hat_setmod(pp);
260 					}
261 
262 					mutex_exit(ahm);
263 				}
264 			} else {
265 				if (!err)
266 					pagezero(pp, 0, PAGESIZE);
267 
268 				/*
269 				 * If it's a fault ahead, release page_io_lock
270 				 * and SE_EXCL we grabbed in page_create_va
271 				 *
272 				 * If we are here, we haven't called VOP_PAGEIO
273 				 * and thus calling pvn_read_done(pp, B_READ)
274 				 * below may mislead that we tried i/o. Besides,
275 				 * in case of async, pvn_read_done() should
276 				 * not be called by *getpage()
277 				 */
278 				if (pl == NULL) {
279 					/*
280 					 * swap_getphysname can return error
281 					 * only when we are getting called from
282 					 * swapslot_free which passes non-NULL
283 					 * pl to VOP_GETPAGE.
284 					 */
285 					ASSERT(err == 0);
286 					page_io_unlock(pp);
287 					page_unlock(pp);
288 				}
289 			}
290 		}
291 
292 		ASSERT(pp != NULL);
293 
294 		if (err && pl)
295 			pvn_read_done(pp, B_ERROR);
296 
297 		if (!err && pl)
298 			pvn_plist_init(pp, pl, plsz, off, PAGESIZE, rw);
299 	}
300 	TRACE_3(TR_FAC_SWAPFS, TR_SWAPFS_GETAPAGE,
301 		"swapfs getapage:pp %p vp %p off %llx", pp, vp, off);
302 	return (err);
303 }
304 
305 /*
306  * Called from large page anon routines only! This is an ugly hack where
307  * the anon layer directly calls into swapfs with a preallocated large page.
308  * Another method would have been to change to VOP and add an extra arg for
309  * the preallocated large page. This all could be cleaned up later when we
310  * solve the anonymous naming problem and no longer need to loop across of
311  * the VOP in PAGESIZE increments to fill in or initialize a large page as
312  * is done today. I think the latter is better since it avoid a change to
313  * the VOP interface that could later be avoided.
314  */
315 int
316 swap_getconpage(
317 	struct vnode *vp,
318 	u_offset_t off,
319 	size_t len,
320 	uint_t *protp,
321 	page_t *pl[],
322 	size_t plsz,
323 	page_t	*conpp,
324 	uint_t	*pszc,
325 	spgcnt_t *nreloc,
326 	struct seg *seg,
327 	caddr_t addr,
328 	enum seg_rw rw,
329 	struct cred *cr)
330 {
331 	struct page	*pp;
332 	int 		err = 0;
333 	struct vnode	*pvp = NULL;
334 	u_offset_t	poff;
335 
336 	ASSERT(len == PAGESIZE);
337 	ASSERT(pl != NULL);
338 	ASSERT(plsz == PAGESIZE);
339 	ASSERT(protp == NULL);
340 	ASSERT(nreloc != NULL);
341 	ASSERT(!SEG_IS_SEGKP(seg)); /* XXX for now not supported */
342 	SWAPFS_PRINT(SWAP_VOPS, "swap_getconpage: vp %p, off %llx, len %lx\n",
343 		vp, off, len, 0, 0);
344 
345 	/*
346 	 * If we are not using a preallocated page then we know one already
347 	 * exists. So just let the old code handle it.
348 	 */
349 	if (conpp == NULL) {
350 		err = swap_getapage(vp, (u_offset_t)off, len, protp, pl, plsz,
351 		    seg, addr, rw, cr);
352 		return (err);
353 	}
354 	ASSERT(conpp->p_szc != 0);
355 	ASSERT(PAGE_EXCL(conpp));
356 
357 
358 	ASSERT(conpp->p_next == conpp);
359 	ASSERT(conpp->p_prev == conpp);
360 	ASSERT(!PP_ISAGED(conpp));
361 	ASSERT(!PP_ISFREE(conpp));
362 
363 	*nreloc = 0;
364 	pp = page_lookup_create(vp, off, SE_SHARED, conpp, nreloc, 0);
365 
366 	/*
367 	 * If existing page is found we may need to relocate.
368 	 */
369 	if (pp != conpp) {
370 		ASSERT(rw != S_CREATE);
371 		ASSERT(pszc != NULL);
372 		ASSERT(PAGE_SHARED(pp));
373 		if (pp->p_szc < conpp->p_szc) {
374 			*pszc = pp->p_szc;
375 			page_unlock(pp);
376 			err = -1;
377 		} else if (pp->p_szc > conpp->p_szc &&
378 		    seg->s_szc > conpp->p_szc) {
379 			*pszc = MIN(pp->p_szc, seg->s_szc);
380 			page_unlock(pp);
381 			err = -2;
382 		} else {
383 			pl[0] = pp;
384 			pl[1] = NULL;
385 			if (page_pptonum(pp) &
386 			    (page_get_pagecnt(conpp->p_szc) - 1))
387 			    cmn_err(CE_PANIC, "swap_getconpage: no root");
388 		}
389 		return (err);
390 	}
391 
392 	ASSERT(PAGE_EXCL(pp));
393 
394 	if (*nreloc != 0) {
395 		ASSERT(rw != S_CREATE);
396 		pl[0] = pp;
397 		pl[1] = NULL;
398 		return (0);
399 	}
400 
401 	*nreloc = 1;
402 
403 	/*
404 	 * If necessary do the page io.
405 	 */
406 	if (rw != S_CREATE) {
407 		/*
408 		 * Since we are only called now on behalf of an
409 		 * address space operation it's impossible for
410 		 * us to fail unlike swap_getapge() which
411 		 * also gets called from swapslot_free().
412 		 */
413 		if (swap_getphysname(vp, off, &pvp, &poff)) {
414 			cmn_err(CE_PANIC,
415 			    "swap_getconpage: swap_getphysname failed!");
416 		}
417 
418 		if (pvp) {
419 			err = VOP_PAGEIO(pvp, pp, poff, PAGESIZE, B_READ, cr,
420 				NULL);
421 		} else {
422 			pagezero(pp, 0, PAGESIZE);
423 		}
424 	}
425 
426 	/*
427 	 * Normally we would let pvn_read_done() destroy
428 	 * the page on IO error. But since this is a preallocated
429 	 * page we'll let the anon layer handle it.
430 	 */
431 	page_io_unlock(pp);
432 	if (err != 0)
433 		page_hashout(pp, NULL);
434 	ASSERT(pp->p_next == pp);
435 	ASSERT(pp->p_prev == pp);
436 
437 	TRACE_3(TR_FAC_SWAPFS, TR_SWAPFS_GETAPAGE,
438 		"swapfs getconpage:pp %p vp %p off %llx", pp, vp, off);
439 
440 	pl[0] = pp;
441 	pl[1] = NULL;
442 	return (err);
443 }
444 
445 /* Async putpage klustering stuff */
446 int sw_pending_size;
447 extern int klustsize;
448 extern struct async_reqs *sw_getreq();
449 extern void sw_putreq(struct async_reqs *);
450 extern void sw_putbackreq(struct async_reqs *);
451 extern struct async_reqs *sw_getfree();
452 extern void sw_putfree(struct async_reqs *);
453 
454 static size_t swap_putpagecnt, swap_pagespushed;
455 static size_t swap_otherfail, swap_otherpages;
456 static size_t swap_klustfail, swap_klustpages;
457 static size_t swap_getiofail, swap_getiopages;
458 
459 /*
460  * Flags are composed of {B_INVAL, B_DIRTY B_FREE, B_DONTNEED}.
461  * If len == 0, do from off to EOF.
462  */
463 static int swap_nopage = 0;	/* Don't do swap_putpage's if set */
464 
465 /* ARGSUSED */
466 static int
467 swap_putpage(
468 	struct vnode *vp,
469 	offset_t off,
470 	size_t len,
471 	int flags,
472 	struct cred *cr,
473 	caller_context_t *ct)
474 {
475 	page_t *pp;
476 	u_offset_t io_off;
477 	size_t io_len = 0;
478 	int err = 0;
479 	struct async_reqs *arg;
480 
481 	if (swap_nopage)
482 		return (0);
483 
484 	ASSERT(vp->v_count != 0);
485 
486 	/*
487 	 * Clear force flag so that p_lckcnt pages are not invalidated.
488 	 */
489 	flags &= ~B_FORCE;
490 
491 	SWAPFS_PRINT(SWAP_VOPS,
492 	    "swap_putpage: vp %p, off %llx len %lx, flags %x\n",
493 	    (void *)vp, off, len, flags, 0);
494 	TRACE_3(TR_FAC_SWAPFS, TR_SWAPFS_PUTPAGE,
495 	    "swapfs putpage:vp %p off %llx len %ld", (void *)vp, off, len);
496 
497 	if (vp->v_flag & VNOMAP)
498 		return (ENOSYS);
499 
500 	if (!vn_has_cached_data(vp))
501 		return (0);
502 
503 	if (len == 0) {
504 		if (curproc == proc_pageout)
505 			cmn_err(CE_PANIC, "swapfs: pageout can't block");
506 
507 		/* Search the entire vp list for pages >= off. */
508 		err = pvn_vplist_dirty(vp, (u_offset_t)off, swap_putapage,
509 		    flags, cr);
510 	} else {
511 		u_offset_t eoff;
512 
513 		/*
514 		 * Loop over all offsets in the range [off...off + len]
515 		 * looking for pages to deal with.
516 		 */
517 		eoff = off + len;
518 		for (io_off = (u_offset_t)off; io_off < eoff;
519 		    io_off += io_len) {
520 			/*
521 			 * If we run out of the async req slot, put the page
522 			 * now instead of queuing.
523 			 */
524 			if (flags == (B_ASYNC | B_FREE) &&
525 			    sw_pending_size < klustsize &&
526 			    (arg = sw_getfree())) {
527 				/*
528 				 * If we are clustering, we should allow
529 				 * pageout to feed us more pages because # of
530 				 * pushes is limited by # of I/Os, and one
531 				 * cluster is considered to be one I/O.
532 				 */
533 				if (pushes)
534 					pushes--;
535 
536 				arg->a_vp = vp;
537 				arg->a_off = io_off;
538 				arg->a_len = PAGESIZE;
539 				arg->a_flags = B_ASYNC | B_FREE;
540 				arg->a_cred = kcred;
541 				sw_putreq(arg);
542 				io_len = PAGESIZE;
543 				continue;
544 			}
545 			/*
546 			 * If we are not invalidating pages, use the
547 			 * routine page_lookup_nowait() to prevent
548 			 * reclaiming them from the free list.
549 			 */
550 			if ((flags & B_INVAL) ||
551 			    (flags & (B_ASYNC | B_FREE)) == B_FREE)
552 				pp = page_lookup(vp, io_off, SE_EXCL);
553 			else
554 				pp = page_lookup_nowait(vp, io_off,
555 					(flags & B_FREE) ? SE_EXCL : SE_SHARED);
556 
557 			if (pp == NULL || pvn_getdirty(pp, flags) == 0)
558 				io_len = PAGESIZE;
559 			else {
560 				err = swap_putapage(vp, pp, &io_off, &io_len,
561 				    flags, cr);
562 				if (err != 0)
563 					break;
564 			}
565 		}
566 	}
567 	/* If invalidating, verify all pages on vnode list are gone. */
568 	if (err == 0 && off == 0 && len == 0 &&
569 	    (flags & B_INVAL) && vn_has_cached_data(vp)) {
570 		cmn_err(CE_WARN,
571 		    "swap_putpage: B_INVAL, pages not gone");
572 	}
573 	return (err);
574 }
575 
576 /*
577  * Write out a single page.
578  * For swapfs this means choose a physical swap slot and write the page
579  * out using VOP_PAGEIO.
580  * In the (B_ASYNC | B_FREE) case we try to find a bunch of other dirty
581  * swapfs pages, a bunch of contiguous swap slots and then write them
582  * all out in one clustered i/o.
583  */
584 /*ARGSUSED*/
585 static int
586 swap_putapage(
587 	struct vnode *vp,
588 	page_t *pp,
589 	u_offset_t *offp,
590 	size_t *lenp,
591 	int flags,
592 	struct cred *cr)
593 {
594 	int err;
595 	struct vnode *pvp;
596 	u_offset_t poff, off;
597 	u_offset_t doff;
598 	size_t dlen;
599 	size_t klsz = 0;
600 	u_offset_t klstart = 0;
601 	struct vnode *klvp = NULL;
602 	page_t *pplist;
603 	se_t se;
604 	struct async_reqs *arg;
605 	size_t swap_klustsize;
606 
607 	/*
608 	 * This check is added for callers who access swap_putpage with len = 0.
609 	 * swap_putpage calls swap_putapage page-by-page via pvn_vplist_dirty.
610 	 * And it's necessary to do the same queuing if users have the same
611 	 * B_ASYNC|B_FREE flags on.
612 	 */
613 	if (flags == (B_ASYNC | B_FREE) &&
614 	    sw_pending_size < klustsize && (arg = sw_getfree())) {
615 
616 		hat_setmod(pp);
617 		page_io_unlock(pp);
618 		page_unlock(pp);
619 
620 		arg->a_vp = vp;
621 		arg->a_off = pp->p_offset;
622 		arg->a_len = PAGESIZE;
623 		arg->a_flags = B_ASYNC | B_FREE;
624 		arg->a_cred = kcred;
625 		sw_putreq(arg);
626 
627 		return (0);
628 	}
629 
630 	SWAPFS_PRINT(SWAP_PUTP,
631 		"swap_putapage: pp %p, vp %p, off %llx, flags %x\n",
632 		pp, vp, pp->p_offset, flags, 0);
633 
634 	ASSERT(PAGE_LOCKED(pp));
635 
636 	off = pp->p_offset;
637 
638 	doff = off;
639 	dlen = PAGESIZE;
640 
641 	if (err = swap_newphysname(vp, off, &doff, &dlen, &pvp, &poff)) {
642 		err = (flags == (B_ASYNC | B_FREE) ? ENOMEM : 0);
643 		hat_setmod(pp);
644 		page_io_unlock(pp);
645 		page_unlock(pp);
646 		goto out;
647 	}
648 
649 	klvp = pvp;
650 	klstart = poff;
651 	pplist = pp;
652 	/*
653 	 * If this is ASYNC | FREE and we've accumulated a bunch of such
654 	 * pending requests, kluster.
655 	 */
656 	if (flags == (B_ASYNC | B_FREE))
657 		swap_klustsize = klustsize;
658 	else
659 		swap_klustsize = PAGESIZE;
660 	se = (flags & B_FREE ? SE_EXCL : SE_SHARED);
661 	klsz = PAGESIZE;
662 	while (klsz < swap_klustsize) {
663 		if ((arg = sw_getreq()) == NULL) {
664 			swap_getiofail++;
665 			swap_getiopages += btop(klsz);
666 			break;
667 		}
668 		ASSERT(vn_matchops(arg->a_vp, swap_vnodeops));
669 		vp = arg->a_vp;
670 		off = arg->a_off;
671 
672 		if ((pp = page_lookup_nowait(vp, off, se)) == NULL) {
673 			swap_otherfail++;
674 			swap_otherpages += btop(klsz);
675 			sw_putfree(arg);
676 			break;
677 		}
678 		if (pvn_getdirty(pp, flags | B_DELWRI) == 0) {
679 			sw_putfree(arg);
680 			continue;
681 		}
682 		/* Get new physical backing store for the page */
683 		doff = off;
684 		dlen = PAGESIZE;
685 		if (err = swap_newphysname(vp, off, &doff, &dlen,
686 						&pvp, &poff)) {
687 			swap_otherfail++;
688 			swap_otherpages += btop(klsz);
689 			hat_setmod(pp);
690 			page_io_unlock(pp);
691 			page_unlock(pp);
692 			sw_putbackreq(arg);
693 			break;
694 		}
695 		/* Try to cluster new physical name with previous ones */
696 		if (klvp == pvp && poff == klstart + klsz) {
697 			klsz += PAGESIZE;
698 			page_add(&pplist, pp);
699 			pplist = pplist->p_next;
700 			sw_putfree(arg);
701 		} else if (klvp == pvp && poff == klstart - PAGESIZE) {
702 			klsz += PAGESIZE;
703 			klstart -= PAGESIZE;
704 			page_add(&pplist, pp);
705 			sw_putfree(arg);
706 		} else {
707 			swap_klustfail++;
708 			swap_klustpages += btop(klsz);
709 			hat_setmod(pp);
710 			page_io_unlock(pp);
711 			page_unlock(pp);
712 			sw_putbackreq(arg);
713 			break;
714 		}
715 	}
716 
717 	err = VOP_PAGEIO(klvp, pplist, klstart, klsz,
718 		    B_WRITE | flags, cr, NULL);
719 
720 	if ((flags & B_ASYNC) == 0)
721 		pvn_write_done(pp, ((err) ? B_ERROR : 0) | B_WRITE | flags);
722 
723 	/* Statistics */
724 	if (!err) {
725 		swap_putpagecnt++;
726 		swap_pagespushed += btop(klsz);
727 	}
728 out:
729 	TRACE_4(TR_FAC_SWAPFS, TR_SWAPFS_PUTAPAGE,
730 		"swapfs putapage:vp %p klvp %p, klstart %lx, klsz %lx",
731 		vp, klvp, klstart, klsz);
732 	if (err && err != ENOMEM)
733 		cmn_err(CE_WARN, "swapfs_putapage: err %d\n", err);
734 	if (lenp)
735 		*lenp = PAGESIZE;
736 	return (err);
737 }
738 
739 static void
740 swap_dispose(
741 	vnode_t *vp,
742 	page_t *pp,
743 	int fl,
744 	int dn,
745 	cred_t *cr,
746 	caller_context_t *ct)
747 {
748 	int err;
749 	u_offset_t off = pp->p_offset;
750 	vnode_t *pvp;
751 	u_offset_t poff;
752 
753 	ASSERT(PAGE_EXCL(pp));
754 
755 	/*
756 	 * The caller will free/invalidate large page in one shot instead of
757 	 * one small page at a time.
758 	 */
759 	if (pp->p_szc != 0) {
760 		page_unlock(pp);
761 		return;
762 	}
763 
764 	err = swap_getphysname(vp, off, &pvp, &poff);
765 	if (!err && pvp != NULL)
766 		VOP_DISPOSE(pvp, pp, fl, dn, cr, ct);
767 	else
768 		fs_dispose(vp, pp, fl, dn, cr, ct);
769 }
770