2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Implements new VFS/VM coherency functions. For conforming VFSs
37 * we treat the backing VM object slightly differently. Instead of
38 * maintaining a number of pages to exactly fit the size of the file
39 * we instead maintain pages to fit the entire contents of the last
40 * buffer cache buffer used by the file.
42 * For VFSs like NFS and HAMMER which use (generally speaking) fixed
43 * sized buffers this greatly reduces the complexity of VFS/VM interactions.
45 * Truncations no longer invalidate pages covered by the buffer cache
46 * beyond the file EOF which still fit within the file's last buffer.
47 * We simply unmap them and do not allow userland to fault them in.
49 * The VFS is no longer responsible for zero-filling buffers during a
50 * truncation, the last buffer will be automatically zero-filled by
53 * This code is intended to (eventually) replace vtruncbuf() and
54 * vnode_pager_setsize().
57 #include <sys/param.h>
58 #include <sys/systm.h>
61 #include <sys/fcntl.h>
63 #include <sys/kernel.h>
64 #include <sys/malloc.h>
65 #include <sys/mount.h>
67 #include <sys/socket.h>
69 #include <sys/sysctl.h>
70 #include <sys/unistd.h>
71 #include <sys/vmmeter.h>
72 #include <sys/vnode.h>
74 #include <machine/limits.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_extern.h>
79 #include <vm/vm_kern.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_page.h>
83 #include <vm/vm_pager.h>
84 #include <vm/vnode_pager.h>
85 #include <vm/vm_zone.h>
88 #include <sys/thread2.h>
89 #include <sys/sysref2.h>
91 static int nvtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
92 static int nvtruncbuf_bp_trunc(struct buf *bp, void *data);
93 static int nvtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
94 static int nvtruncbuf_bp_metasync(struct buf *bp, void *data);
97 * Truncate a file's buffer and pages to a specified length. The
98 * byte-granular length of the file is specified along with the block
99 * size of the buffer containing that offset.
101 * If the last buffer straddles the length its contents will be zero-filled
102 * as appropriate. All buffers and pages after the last buffer will be
103 * destroyed. The last buffer itself will be destroyed only if the length
104 * is exactly aligned with it.
106 * UFS typically passes the old block size prior to the actual truncation,
107 * then later resizes the block based on the new file size. NFS uses a
108 * fixed block size and doesn't care. HAMMER uses a block size based on
109 * the offset which is fixed for any particular offset.
111 * When zero-filling we must bdwrite() to avoid a window of opportunity
112 * where the kernel might throw away a clean buffer and the filesystem
113 * then attempts to bread() it again before completing (or as part of)
114 * the extension. The filesystem is still responsible for zero-filling
115 * any remainder when writing to the media in the strategy function when
116 * it is able to do so without the page being mapped. The page may still
117 * be mapped by userland here.
119 * When modifying a buffer we must clear any cached raw disk offset.
120 * bdwrite() will call BMAP on it again. Some filesystems, like HAMMER,
121 * never overwrite existing data blocks.
124 struct truncbuf_info {
126 off_t truncloffset; /* truncation point */
127 int clean; /* clean tree, else dirty tree */
131 nvtruncbuf(struct vnode *vp, off_t length, int blksize, int boff)
133 struct truncbuf_info info;
135 const char *filename;
141 * Round up to the *next* block, then destroy the buffers in question.
142 * Since we are only removing some of the buffers we must rely on the
143 * scan count to determine whether a loop is necessary.
145 * Destroy any pages beyond the last buffer.
148 boff = (int)(length % blksize);
150 info.truncloffset = length + (blksize - boff);
152 info.truncloffset = length;
154 lwkt_gettoken(&vp->v_token);
157 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
158 nvtruncbuf_bp_trunc_cmp,
159 nvtruncbuf_bp_trunc, &info);
161 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
162 nvtruncbuf_bp_trunc_cmp,
163 nvtruncbuf_bp_trunc, &info);
166 nvnode_pager_setsize(vp, length, blksize, boff);
169 * Zero-fill the area beyond the file EOF that still fits within
170 * the last buffer. We must mark the buffer as dirty even though
171 * the modified area is beyond EOF to avoid races where the kernel
172 * might flush the buffer before the filesystem is able to reallocate
175 * The VFS is responsible for dealing with the actual truncation.
178 truncboffset = length - boff;
179 error = bread(vp, truncboffset, blksize, &bp);
181 bzero(bp->b_data + boff, blksize - boff);
182 if (bp->b_flags & B_DELWRI) {
183 if (bp->b_dirtyoff > boff)
184 bp->b_dirtyoff = boff;
185 if (bp->b_dirtyend > boff)
186 bp->b_dirtyend = boff;
188 bp->b_bio2.bio_offset = NOOFFSET;
196 * For safety, fsync any remaining metadata if the file is not being
197 * truncated to 0. Since the metadata does not represent the entire
198 * dirty list we have to rely on the hit count to ensure that we get
201 * This is typically applicable only to UFS. NFS and HAMMER do
202 * not store indirect blocks in the per-vnode buffer cache.
206 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
207 nvtruncbuf_bp_metasync_cmp,
208 nvtruncbuf_bp_metasync, &info);
213 * It is possible to have in-progress I/O from buffers that were
214 * not part of the truncation. This should not happen if we
215 * are truncating to 0-length.
217 bio_track_wait(&vp->v_track_write, 0, 0);
222 spin_lock(&vp->v_spin);
223 filename = TAILQ_FIRST(&vp->v_namecache) ?
224 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
225 spin_unlock(&vp->v_spin);
228 * Make sure no buffers were instantiated while we were trying
229 * to clean out the remaining VM pages. This could occur due
230 * to busy dirty VM pages being flushed out to disk.
234 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
235 nvtruncbuf_bp_trunc_cmp,
236 nvtruncbuf_bp_trunc, &info);
238 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
239 nvtruncbuf_bp_trunc_cmp,
240 nvtruncbuf_bp_trunc, &info);
242 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
243 "left over buffers in %s\n", count, filename);
247 lwkt_reltoken(&vp->v_token);
253 * The callback buffer is beyond the new file EOF and must be destroyed.
254 * Note that the compare function must conform to the RB_SCAN's requirements.
258 nvtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
260 struct truncbuf_info *info = data;
262 if (bp->b_loffset >= info->truncloffset)
269 nvtruncbuf_bp_trunc(struct buf *bp, void *data)
271 struct truncbuf_info *info = data;
274 * Do not try to use a buffer we cannot immediately lock,
275 * but sleep anyway to prevent a livelock. The code will
276 * loop until all buffers can be acted upon.
278 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
279 atomic_add_int(&bp->b_refs, 1);
280 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
282 atomic_subtract_int(&bp->b_refs, 1);
283 } else if ((info->clean && (bp->b_flags & B_DELWRI)) ||
284 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0) ||
285 bp->b_vp != info->vp ||
286 nvtruncbuf_bp_trunc_cmp(bp, data)) {
290 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
297 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
298 * blocks (with a negative loffset) are scanned.
299 * Note that the compare function must conform to the RB_SCAN's requirements.
302 nvtruncbuf_bp_metasync_cmp(struct buf *bp, void *data __unused)
304 if (bp->b_loffset < 0)
310 nvtruncbuf_bp_metasync(struct buf *bp, void *data)
312 struct truncbuf_info *info = data;
315 * Do not try to use a buffer we cannot immediately lock,
316 * but sleep anyway to prevent a livelock. The code will
317 * loop until all buffers can be acted upon.
319 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
320 atomic_add_int(&bp->b_refs, 1);
321 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
323 atomic_subtract_int(&bp->b_refs, 1);
324 } else if ((bp->b_flags & B_DELWRI) == 0 ||
325 bp->b_vp != info->vp ||
326 nvtruncbuf_bp_metasync_cmp(bp, data)) {
336 * Extend a file's buffer and pages to a new, larger size. The block size
337 * at both the old and new length must be passed, but buffer cache operations
338 * will only be performed on the old block. The new nlength/nblksize will
339 * be used to properly set the VM object size.
341 * To make this explicit we require the old length to passed even though
342 * we can acquire it from vp->v_filesize, which also avoids potential
343 * corruption if the filesystem and vp get desynchronized somehow.
345 * If the caller intends to immediately write into the newly extended
346 * space pass trivial == 1. If trivial is 0 the original buffer will be
347 * zero-filled as necessary to clean out any junk in the extended space.
349 * When zero-filling we must bdwrite() to avoid a window of opportunity
350 * where the kernel might throw away a clean buffer and the filesystem
351 * then attempts to bread() it again before completing (or as part of)
352 * the extension. The filesystem is still responsible for zero-filling
353 * any remainder when writing to the media in the strategy function when
354 * it is able to do so without the page being mapped. The page may still
355 * be mapped by userland here.
357 * When modifying a buffer we must clear any cached raw disk offset.
358 * bdwrite() will call BMAP on it again. Some filesystems, like HAMMER,
359 * never overwrite existing data blocks.
362 nvextendbuf(struct vnode *vp, off_t olength, off_t nlength,
363 int oblksize, int nblksize, int oboff, int nboff, int trivial)
370 nvnode_pager_setsize(vp, nlength, nblksize, nboff);
373 oboff = (int)(olength % oblksize);
374 truncboffset = olength - oboff;
377 error = bread(vp, truncboffset, oblksize, &bp);
379 bzero(bp->b_data + oboff, oblksize - oboff);
380 bp->b_bio2.bio_offset = NOOFFSET;
389 * Set vp->v_filesize and vp->v_object->size, destroy pages beyond
390 * the last buffer when truncating.
392 * This function does not do any zeroing or invalidating of partially
393 * overlapping pages. Zeroing is the responsibility of nvtruncbuf().
394 * However, it does unmap VM pages from the user address space on a
395 * page-granular (verses buffer cache granular) basis.
397 * If boff is passed as -1 the base offset of the buffer cache buffer is
398 * calculated from length and blksize. Filesystems such as UFS which deal
399 * with fragments have to specify a boff >= 0 since the base offset cannot
400 * be calculated from length and blksize.
402 * For UFS blksize is the 'new' blocksize, used only to determine how large
403 * the VM object must become.
406 nvnode_pager_setsize(struct vnode *vp, off_t length, int blksize, int boff)
408 vm_pindex_t nobjsize;
409 vm_pindex_t oobjsize;
416 * Degenerate conditions
418 if ((object = vp->v_object) == NULL)
420 vm_object_hold(object);
421 if (length == vp->v_filesize) {
422 vm_object_drop(object);
427 * Calculate the size of the VM object, coverage includes
428 * the buffer straddling EOF. If EOF is buffer-aligned
431 * Buffers do not have to be page-aligned. Make sure
432 * nobjsize is beyond the last page of the buffer.
435 boff = (int)(length % blksize);
436 truncboffset = length - boff;
437 oobjsize = object->size;
439 nobjsize = OFF_TO_IDX(truncboffset + blksize + PAGE_MASK);
441 nobjsize = OFF_TO_IDX(truncboffset + PAGE_MASK);
442 object->size = nobjsize;
444 if (length < vp->v_filesize) {
446 * File has shrunk, toss any cached pages beyond
447 * the end of the buffer (blksize aligned) for the
450 vp->v_filesize = length;
451 if (nobjsize < oobjsize) {
452 vm_object_page_remove(object, nobjsize, oobjsize,
457 * Unmap any pages (page aligned) beyond the new EOF.
458 * The pages remain part of the (last) buffer and are not
461 pi = OFF_TO_IDX(length + PAGE_MASK);
462 while (pi < nobjsize) {
463 m = vm_page_lookup_busy_wait(object, pi, FALSE, "vmpg");
465 vm_page_protect(m, VM_PROT_NONE);
474 vp->v_filesize = length;
476 vm_object_drop(object);