truncate.c 27.3 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
L
Linus Torvalds 已提交
2 3 4 5 6
/*
 * mm/truncate.c - code for taking down pages from address_spaces
 *
 * Copyright (C) 2002, Linus Torvalds
 *
7
 * 10Sep2002	Andrew Morton
L
Linus Torvalds 已提交
8 9 10 11
 *		Initial version.
 */

#include <linux/kernel.h>
A
Alexey Dobriyan 已提交
12
#include <linux/backing-dev.h>
13
#include <linux/dax.h>
14
#include <linux/gfp.h>
L
Linus Torvalds 已提交
15
#include <linux/mm.h>
N
Nick Piggin 已提交
16
#include <linux/swap.h>
17
#include <linux/export.h>
L
Linus Torvalds 已提交
18
#include <linux/pagemap.h>
19
#include <linux/highmem.h>
L
Linus Torvalds 已提交
20
#include <linux/pagevec.h>
21
#include <linux/task_io_accounting_ops.h>
L
Linus Torvalds 已提交
22
#include <linux/buffer_head.h>	/* grr. try_to_release_page,
23
				   do_invalidatepage */
24
#include <linux/shmem_fs.h>
25
#include <linux/cleancache.h>
26
#include <linux/rmap.h>
27
#include "internal.h"
L
Linus Torvalds 已提交
28

29 30 31 32 33 34 35
/*
 * Regular page slots are stabilized by the page lock even without the tree
 * itself locked.  These unlocked entries need verification under the tree
 * lock.
 */
static inline void __clear_shadow_entry(struct address_space *mapping,
				pgoff_t index, void *entry)
36
{
M
Matthew Wilcox 已提交
37
	XA_STATE(xas, &mapping->i_pages, index);
38

M
Matthew Wilcox 已提交
39 40
	xas_set_update(&xas, workingset_update_node);
	if (xas_load(&xas) != entry)
41
		return;
M
Matthew Wilcox 已提交
42
	xas_store(&xas, NULL);
J
Jan Kara 已提交
43
	mapping->nrexceptional--;
44 45 46 47 48
}

static void clear_shadow_entry(struct address_space *mapping, pgoff_t index,
			       void *entry)
{
M
Matthew Wilcox 已提交
49
	xa_lock_irq(&mapping->i_pages);
50
	__clear_shadow_entry(mapping, index, entry);
M
Matthew Wilcox 已提交
51
	xa_unlock_irq(&mapping->i_pages);
52
}
L
Linus Torvalds 已提交
53

54
/*
55 56 57
 * Unconditionally remove exceptional entries. Usually called from truncate
 * path. Note that the pagevec may be altered by this function by removing
 * exceptional entries similar to what pagevec_remove_exceptionals does.
58
 */
59 60 61
static void truncate_exceptional_pvec_entries(struct address_space *mapping,
				struct pagevec *pvec, pgoff_t *indices,
				pgoff_t end)
62
{
63 64 65
	int i, j;
	bool dax, lock;

66 67 68 69
	/* Handled by shmem itself */
	if (shmem_mapping(mapping))
		return;

70
	for (j = 0; j < pagevec_count(pvec); j++)
71
		if (xa_is_value(pvec->pages[j]))
72 73 74
			break;

	if (j == pagevec_count(pvec))
75
		return;
76 77 78 79

	dax = dax_mapping(mapping);
	lock = !dax && indices[j] < end;
	if (lock)
M
Matthew Wilcox 已提交
80
		xa_lock_irq(&mapping->i_pages);
81 82 83 84 85

	for (i = j; i < pagevec_count(pvec); i++) {
		struct page *page = pvec->pages[i];
		pgoff_t index = indices[i];

86
		if (!xa_is_value(page)) {
87 88 89 90 91 92 93 94 95 96 97 98 99
			pvec->pages[j++] = page;
			continue;
		}

		if (index >= end)
			continue;

		if (unlikely(dax)) {
			dax_delete_mapping_entry(mapping, index);
			continue;
		}

		__clear_shadow_entry(mapping, index, page);
100
	}
101 102

	if (lock)
M
Matthew Wilcox 已提交
103
		xa_unlock_irq(&mapping->i_pages);
104
	pvec->nr = j;
105 106 107 108
}

/*
 * Invalidate exceptional entry if easily possible. This handles exceptional
109
 * entries for invalidate_inode_pages().
110 111 112 113
 */
static int invalidate_exceptional_entry(struct address_space *mapping,
					pgoff_t index, void *entry)
{
114 115
	/* Handled by shmem itself, or for DAX we do nothing. */
	if (shmem_mapping(mapping) || dax_mapping(mapping))
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
		return 1;
	clear_shadow_entry(mapping, index, entry);
	return 1;
}

/*
 * Invalidate exceptional entry if clean. This handles exceptional entries for
 * invalidate_inode_pages2() so for DAX it evicts only clean entries.
 */
static int invalidate_exceptional_entry2(struct address_space *mapping,
					 pgoff_t index, void *entry)
{
	/* Handled by shmem itself */
	if (shmem_mapping(mapping))
		return 1;
	if (dax_mapping(mapping))
		return dax_invalidate_mapping_entry_sync(mapping, index);
	clear_shadow_entry(mapping, index, entry);
	return 1;
}

137
/**
138
 * do_invalidatepage - invalidate part or all of a page
139
 * @page: the page which is affected
140 141
 * @offset: start of the range to invalidate
 * @length: length of the range to invalidate
142 143 144 145 146 147 148 149 150 151
 *
 * do_invalidatepage() is called when all or part of the page has become
 * invalidated by a truncate operation.
 *
 * do_invalidatepage() does not have to release all buffers, but it must
 * ensure that no dirty buffer is left outside @offset and that no I/O
 * is underway against any of the blocks which are outside the truncation
 * point.  Because the caller is about to free (and possibly reuse) those
 * blocks on-disk.
 */
152 153
void do_invalidatepage(struct page *page, unsigned int offset,
		       unsigned int length)
154
{
155 156
	void (*invalidatepage)(struct page *, unsigned int, unsigned int);

157
	invalidatepage = page->mapping->a_ops->invalidatepage;
158
#ifdef CONFIG_BLOCK
159 160
	if (!invalidatepage)
		invalidatepage = block_invalidatepage;
161
#endif
162
	if (invalidatepage)
163
		(*invalidatepage)(page, offset, length);
164 165
}

L
Linus Torvalds 已提交
166 167
/*
 * If truncate cannot remove the fs-private metadata from the page, the page
168
 * becomes orphaned.  It will be left on the LRU and may even be mapped into
169
 * user pagetables if we're racing with filemap_fault().
L
Linus Torvalds 已提交
170 171 172
 *
 * We need to bale out if page->mapping is no longer equal to the original
 * mapping.  This happens a) when the VM reclaimed the page while we waited on
173
 * its lock, b) when a concurrent invalidate_mapping_pages got there first and
L
Linus Torvalds 已提交
174 175
 * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
 */
J
Jan Kara 已提交
176 177
static void
truncate_cleanup_page(struct address_space *mapping, struct page *page)
L
Linus Torvalds 已提交
178
{
J
Jan Kara 已提交
179
	if (page_mapped(page)) {
M
Matthew Wilcox 已提交
180 181
		pgoff_t nr = PageTransHuge(page) ? HPAGE_PMD_NR : 1;
		unmap_mapping_pages(mapping, page->index, nr, false);
J
Jan Kara 已提交
182
	}
L
Linus Torvalds 已提交
183

184
	if (page_has_private(page))
185
		do_invalidatepage(page, 0, PAGE_SIZE);
L
Linus Torvalds 已提交
186

187 188 189 190 191
	/*
	 * Some filesystems seem to re-dirty the page even after
	 * the VM has canceled the dirty bit (eg ext3 journaling).
	 * Hence dirty accounting check is placed after invalidation.
	 */
192
	cancel_dirty_page(page);
L
Linus Torvalds 已提交
193 194 195 196
	ClearPageMappedToDisk(page);
}

/*
197
 * This is for invalidate_mapping_pages().  That function can be called at
L
Linus Torvalds 已提交
198
 * any time, and is not supposed to throw away dirty pages.  But pages can
N
Nick Piggin 已提交
199 200
 * be marked dirty at any time too, so use remove_mapping which safely
 * discards clean, unused pages.
L
Linus Torvalds 已提交
201 202 203 204 205 206
 *
 * Returns non-zero if the page was successfully invalidated.
 */
static int
invalidate_complete_page(struct address_space *mapping, struct page *page)
{
N
Nick Piggin 已提交
207 208
	int ret;

L
Linus Torvalds 已提交
209 210 211
	if (page->mapping != mapping)
		return 0;

212
	if (page_has_private(page) && !try_to_release_page(page, 0))
L
Linus Torvalds 已提交
213 214
		return 0;

N
Nick Piggin 已提交
215 216 217
	ret = remove_mapping(mapping, page);

	return ret;
L
Linus Torvalds 已提交
218 219
}

220 221
int truncate_inode_page(struct address_space *mapping, struct page *page)
{
K
Kirill A. Shutemov 已提交
222 223
	VM_BUG_ON_PAGE(PageTail(page), page);

J
Jan Kara 已提交
224 225 226 227 228 229
	if (page->mapping != mapping)
		return -EIO;

	truncate_cleanup_page(mapping, page);
	delete_from_page_cache(page);
	return 0;
230 231
}

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
/*
 * Used to get rid of pages on hardware memory corruption.
 */
int generic_error_remove_page(struct address_space *mapping, struct page *page)
{
	if (!mapping)
		return -EINVAL;
	/*
	 * Only punch for normal data pages for now.
	 * Handling other types like directories would need more auditing.
	 */
	if (!S_ISREG(mapping->host->i_mode))
		return -EIO;
	return truncate_inode_page(mapping, page);
}
EXPORT_SYMBOL(generic_error_remove_page);

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
/*
 * Safely invalidate one page from its pagecache mapping.
 * It only drops clean, unused pages. The page must be locked.
 *
 * Returns 1 if the page is successfully invalidated, otherwise 0.
 */
int invalidate_inode_page(struct page *page)
{
	struct address_space *mapping = page_mapping(page);
	if (!mapping)
		return 0;
	if (PageDirty(page) || PageWriteback(page))
		return 0;
	if (page_mapped(page))
		return 0;
	return invalidate_complete_page(mapping, page);
}

L
Linus Torvalds 已提交
267
/**
268
 * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
L
Linus Torvalds 已提交
269 270
 * @mapping: mapping to truncate
 * @lstart: offset from which to truncate
271
 * @lend: offset to which to truncate (inclusive)
L
Linus Torvalds 已提交
272
 *
273
 * Truncate the page cache, removing the pages that are between
274 275
 * specified offsets (and zeroing out partial pages
 * if lstart or lend + 1 is not page aligned).
L
Linus Torvalds 已提交
276 277 278 279 280 281 282 283 284 285
 *
 * Truncate takes two passes - the first pass is nonblocking.  It will not
 * block on page locks and it will not block on writeback.  The second pass
 * will wait.  This is to prevent as much IO as possible in the affected region.
 * The first pass will remove most pages, so the search cost of the second pass
 * is low.
 *
 * We pass down the cache-hot hint to the page freeing code.  Even if the
 * mapping is large, it is probably the case that the final pages are the most
 * recently touched, and freeing happens in ascending file offset order.
286 287 288 289
 *
 * Note that since ->invalidatepage() accepts range to invalidate
 * truncate_inode_pages_range is able to handle cases where lend + 1 is not
 * page aligned properly.
L
Linus Torvalds 已提交
290
 */
291 292
void truncate_inode_pages_range(struct address_space *mapping,
				loff_t lstart, loff_t lend)
L
Linus Torvalds 已提交
293
{
294 295 296 297 298
	pgoff_t		start;		/* inclusive */
	pgoff_t		end;		/* exclusive */
	unsigned int	partial_start;	/* inclusive */
	unsigned int	partial_end;	/* exclusive */
	struct pagevec	pvec;
299
	pgoff_t		indices[PAGEVEC_SIZE];
300 301
	pgoff_t		index;
	int		i;
L
Linus Torvalds 已提交
302

303
	if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
304
		goto out;
L
Linus Torvalds 已提交
305

306
	/* Offsets within partial pages */
307 308
	partial_start = lstart & (PAGE_SIZE - 1);
	partial_end = (lend + 1) & (PAGE_SIZE - 1);
309 310 311 312 313 314 315

	/*
	 * 'start' and 'end' always covers the range of pages to be fully
	 * truncated. Partial pages are covered with 'partial_start' at the
	 * start of the range and 'partial_end' at the end of the range.
	 * Note that 'end' is exclusive while 'lend' is inclusive.
	 */
316
	start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
317 318 319 320 321 322 323 324
	if (lend == -1)
		/*
		 * lend == -1 indicates end-of-file so we have to set 'end'
		 * to the highest possible pgoff_t and since the type is
		 * unsigned we're using -1.
		 */
		end = -1;
	else
325
		end = (lend + 1) >> PAGE_SHIFT;
326

327
	pagevec_init(&pvec);
328
	index = start;
329 330 331
	while (index < end && pagevec_lookup_entries(&pvec, mapping, index,
			min(end - index, (pgoff_t)PAGEVEC_SIZE),
			indices)) {
332 333 334 335 336 337 338
		/*
		 * Pagevec array has exceptional entries and we may also fail
		 * to lock some pages. So we store pages that can be deleted
		 * in a new pagevec.
		 */
		struct pagevec locked_pvec;

339
		pagevec_init(&locked_pvec);
L
Linus Torvalds 已提交
340 341 342
		for (i = 0; i < pagevec_count(&pvec); i++) {
			struct page *page = pvec.pages[i];

343
			/* We rely upon deletion not changing page->index */
344
			index = indices[i];
345
			if (index >= end)
346 347
				break;

348
			if (xa_is_value(page))
349 350
				continue;

N
Nick Piggin 已提交
351
			if (!trylock_page(page))
L
Linus Torvalds 已提交
352
				continue;
353
			WARN_ON(page_to_index(page) != index);
L
Linus Torvalds 已提交
354 355 356 357
			if (PageWriteback(page)) {
				unlock_page(page);
				continue;
			}
358 359 360 361 362
			if (page->mapping != mapping) {
				unlock_page(page);
				continue;
			}
			pagevec_add(&locked_pvec, page);
L
Linus Torvalds 已提交
363
		}
364 365 366 367 368
		for (i = 0; i < pagevec_count(&locked_pvec); i++)
			truncate_cleanup_page(mapping, locked_pvec.pages[i]);
		delete_from_page_cache_batch(mapping, &locked_pvec);
		for (i = 0; i < pagevec_count(&locked_pvec); i++)
			unlock_page(locked_pvec.pages[i]);
369
		truncate_exceptional_pvec_entries(mapping, &pvec, indices, end);
L
Linus Torvalds 已提交
370 371
		pagevec_release(&pvec);
		cond_resched();
372
		index++;
L
Linus Torvalds 已提交
373
	}
374
	if (partial_start) {
L
Linus Torvalds 已提交
375 376
		struct page *page = find_lock_page(mapping, start - 1);
		if (page) {
377
			unsigned int top = PAGE_SIZE;
378 379 380 381 382
			if (start > end) {
				/* Truncation within a single page */
				top = partial_end;
				partial_end = 0;
			}
L
Linus Torvalds 已提交
383
			wait_on_page_writeback(page);
384 385 386 387 388
			zero_user_segment(page, partial_start, top);
			cleancache_invalidate_page(mapping, page);
			if (page_has_private(page))
				do_invalidatepage(page, partial_start,
						  top - partial_start);
L
Linus Torvalds 已提交
389
			unlock_page(page);
390
			put_page(page);
L
Linus Torvalds 已提交
391 392
		}
	}
393 394 395 396 397 398 399 400 401 402
	if (partial_end) {
		struct page *page = find_lock_page(mapping, end);
		if (page) {
			wait_on_page_writeback(page);
			zero_user_segment(page, 0, partial_end);
			cleancache_invalidate_page(mapping, page);
			if (page_has_private(page))
				do_invalidatepage(page, 0,
						  partial_end);
			unlock_page(page);
403
			put_page(page);
404 405 406 407 408 409 410
		}
	}
	/*
	 * If the truncation happened within a single page no pages
	 * will be released, just zeroed, so we can bail out now.
	 */
	if (start >= end)
411
		goto out;
L
Linus Torvalds 已提交
412

413
	index = start;
L
Linus Torvalds 已提交
414 415
	for ( ; ; ) {
		cond_resched();
416
		if (!pagevec_lookup_entries(&pvec, mapping, index,
417 418
			min(end - index, (pgoff_t)PAGEVEC_SIZE), indices)) {
			/* If all gone from start onwards, we're done */
419
			if (index == start)
L
Linus Torvalds 已提交
420
				break;
421
			/* Otherwise restart to make sure all gone */
422
			index = start;
L
Linus Torvalds 已提交
423 424
			continue;
		}
425
		if (index == start && indices[0] >= end) {
426
			/* All gone out of hole to be punched, we're done */
427
			pagevec_remove_exceptionals(&pvec);
428 429 430
			pagevec_release(&pvec);
			break;
		}
431

L
Linus Torvalds 已提交
432 433 434
		for (i = 0; i < pagevec_count(&pvec); i++) {
			struct page *page = pvec.pages[i];

435
			/* We rely upon deletion not changing page->index */
436
			index = indices[i];
437 438 439
			if (index >= end) {
				/* Restart punch to make sure all gone */
				index = start - 1;
440
				break;
441
			}
442

443
			if (xa_is_value(page))
444 445
				continue;

L
Linus Torvalds 已提交
446
			lock_page(page);
447
			WARN_ON(page_to_index(page) != index);
L
Linus Torvalds 已提交
448
			wait_on_page_writeback(page);
449
			truncate_inode_page(mapping, page);
L
Linus Torvalds 已提交
450 451
			unlock_page(page);
		}
452
		truncate_exceptional_pvec_entries(mapping, &pvec, indices, end);
L
Linus Torvalds 已提交
453
		pagevec_release(&pvec);
454
		index++;
L
Linus Torvalds 已提交
455
	}
456 457

out:
458
	cleancache_invalidate_inode(mapping);
L
Linus Torvalds 已提交
459
}
460
EXPORT_SYMBOL(truncate_inode_pages_range);
L
Linus Torvalds 已提交
461

462 463 464 465 466
/**
 * truncate_inode_pages - truncate *all* the pages from an offset
 * @mapping: mapping to truncate
 * @lstart: offset from which to truncate
 *
467
 * Called under (and serialised by) inode->i_mutex.
468 469 470 471 472
 *
 * Note: When this function returns, there can be a page in the process of
 * deletion (inside __delete_from_page_cache()) in the specified range.  Thus
 * mapping->nrpages can be non-zero when this function returns even after
 * truncation of the whole mapping.
473 474 475 476 477
 */
void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
{
	truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
}
L
Linus Torvalds 已提交
478 479
EXPORT_SYMBOL(truncate_inode_pages);

480 481 482 483 484 485 486 487 488 489 490
/**
 * truncate_inode_pages_final - truncate *all* pages before inode dies
 * @mapping: mapping to truncate
 *
 * Called under (and serialized by) inode->i_mutex.
 *
 * Filesystems have to use this in the .evict_inode path to inform the
 * VM that this is the final truncate and the inode is going away.
 */
void truncate_inode_pages_final(struct address_space *mapping)
{
491
	unsigned long nrexceptional;
492 493 494 495 496 497 498 499 500 501 502 503 504
	unsigned long nrpages;

	/*
	 * Page reclaim can not participate in regular inode lifetime
	 * management (can't call iput()) and thus can race with the
	 * inode teardown.  Tell it when the address space is exiting,
	 * so that it does not install eviction information after the
	 * final truncate has begun.
	 */
	mapping_set_exiting(mapping);

	/*
	 * When reclaim installs eviction entries, it increases
505
	 * nrexceptional first, then decreases nrpages.  Make sure we see
506 507 508 509
	 * this in the right order or we might miss an entry.
	 */
	nrpages = mapping->nrpages;
	smp_rmb();
510
	nrexceptional = mapping->nrexceptional;
511

512
	if (nrpages || nrexceptional) {
513 514 515 516 517 518
		/*
		 * As truncation uses a lockless tree lookup, cycle
		 * the tree lock to make sure any ongoing tree
		 * modification that does not see AS_EXITING is
		 * completed before starting the final truncate.
		 */
M
Matthew Wilcox 已提交
519 520
		xa_lock_irq(&mapping->i_pages);
		xa_unlock_irq(&mapping->i_pages);
521
	}
522 523 524 525 526 527

	/*
	 * Cleancache needs notification even if there are no pages or shadow
	 * entries.
	 */
	truncate_inode_pages(mapping, 0);
528 529 530
}
EXPORT_SYMBOL(truncate_inode_pages_final);

531 532
unsigned long __invalidate_mapping_pages(struct address_space *mapping,
		pgoff_t start, pgoff_t end, unsigned long *nr_pagevec)
L
Linus Torvalds 已提交
533
{
534
	pgoff_t indices[PAGEVEC_SIZE];
L
Linus Torvalds 已提交
535
	struct pagevec pvec;
536
	pgoff_t index = start;
M
Minchan Kim 已提交
537 538
	unsigned long ret;
	unsigned long count = 0;
L
Linus Torvalds 已提交
539 540
	int i;

541
	pagevec_init(&pvec);
542 543 544
	while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
			min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
			indices)) {
L
Linus Torvalds 已提交
545 546
		for (i = 0; i < pagevec_count(&pvec); i++) {
			struct page *page = pvec.pages[i];
547

548
			/* We rely upon deletion not changing page->index */
549
			index = indices[i];
550 551
			if (index > end)
				break;
552

553
			if (xa_is_value(page)) {
554 555
				invalidate_exceptional_entry(mapping, index,
							     page);
556 557 558
				continue;
			}

559 560
			if (!trylock_page(page))
				continue;
K
Kirill A. Shutemov 已提交
561

562
			WARN_ON(page_to_index(page) != index);
K
Kirill A. Shutemov 已提交
563 564 565 566 567 568 569 570

			/* Middle of THP: skip */
			if (PageTransTail(page)) {
				unlock_page(page);
				continue;
			} else if (PageTransHuge(page)) {
				index += HPAGE_PMD_NR - 1;
				i += HPAGE_PMD_NR - 1;
571 572 573 574 575 576 577
				/*
				 * 'end' is in the middle of THP. Don't
				 * invalidate the page as the part outside of
				 * 'end' could be still useful.
				 */
				if (index > end) {
					unlock_page(page);
K
Kirill A. Shutemov 已提交
578
					continue;
579
				}
580 581 582 583 584 585 586 587 588 589

				/* Take a pin outside pagevec */
				get_page(page);

				/*
				 * Drop extra pins before trying to invalidate
				 * the huge page.
				 */
				pagevec_remove_exceptionals(&pvec);
				pagevec_release(&pvec);
K
Kirill A. Shutemov 已提交
590 591
			}

M
Minchan Kim 已提交
592
			ret = invalidate_inode_page(page);
L
Linus Torvalds 已提交
593
			unlock_page(page);
M
Minchan Kim 已提交
594 595 596 597
			/*
			 * Invalidation is a hint that the page is no longer
			 * of interest and try to speed up its reclaim.
			 */
598
			if (!ret) {
599
				deactivate_file_page(page);
600 601 602 603 604
				/* It is likely on the pagevec of a remote CPU */
				if (nr_pagevec)
					(*nr_pagevec)++;
			}

605 606
			if (PageTransHuge(page))
				put_page(page);
M
Minchan Kim 已提交
607
			count += ret;
L
Linus Torvalds 已提交
608
		}
609
		pagevec_remove_exceptionals(&pvec);
L
Linus Torvalds 已提交
610
		pagevec_release(&pvec);
611
		cond_resched();
612
		index++;
L
Linus Torvalds 已提交
613
	}
M
Minchan Kim 已提交
614
	return count;
L
Linus Torvalds 已提交
615
}
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636

/**
 * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
 * @mapping: the address_space which holds the pages to invalidate
 * @start: the offset 'from' which to invalidate
 * @end: the offset 'to' which to invalidate (inclusive)
 *
 * This function only removes the unlocked pages, if you want to
 * remove all the pages of one inode, you must call truncate_inode_pages.
 *
 * invalidate_mapping_pages() will not block on IO activity. It will not
 * invalidate pages which are dirty, locked, under writeback or mapped into
 * pagetables.
 *
 * Return: the number of the pages that were invalidated
 */
unsigned long invalidate_mapping_pages(struct address_space *mapping,
		pgoff_t start, pgoff_t end)
{
	return __invalidate_mapping_pages(mapping, start, end, NULL);
}
637
EXPORT_SYMBOL(invalidate_mapping_pages);
L
Linus Torvalds 已提交
638

639 640 641 642 643 644 645 646 647 648 649
/**
 * This helper is similar with the above one, except that it accounts for pages
 * that are likely on a pagevec and count them in @nr_pagevec, which will used by
 * the caller.
 */
void invalidate_mapping_pagevec(struct address_space *mapping,
		pgoff_t start, pgoff_t end, unsigned long *nr_pagevec)
{
	__invalidate_mapping_pages(mapping, start, end, nr_pagevec);
}

650 651 652 653
/*
 * This is like invalidate_complete_page(), except it ignores the page's
 * refcount.  We do this because invalidate_inode_pages2() needs stronger
 * invalidation guarantees, and cannot afford to leave pages behind because
654 655
 * shrink_page_list() has a temp ref on them, or because they're transiently
 * sitting in the lru_cache_add() pagevecs.
656 657 658 659
 */
static int
invalidate_complete_page2(struct address_space *mapping, struct page *page)
{
660 661
	unsigned long flags;

662 663 664
	if (page->mapping != mapping)
		return 0;

665
	if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL))
666 667
		return 0;

M
Matthew Wilcox 已提交
668
	xa_lock_irqsave(&mapping->i_pages, flags);
669 670 671
	if (PageDirty(page))
		goto failed;

672
	BUG_ON(page_has_private(page));
J
Johannes Weiner 已提交
673
	__delete_from_page_cache(page, NULL);
M
Matthew Wilcox 已提交
674
	xa_unlock_irqrestore(&mapping->i_pages, flags);
675 676 677 678

	if (mapping->a_ops->freepage)
		mapping->a_ops->freepage(page);

679
	put_page(page);	/* pagecache ref */
680 681
	return 1;
failed:
M
Matthew Wilcox 已提交
682
	xa_unlock_irqrestore(&mapping->i_pages, flags);
683 684 685
	return 0;
}

686 687 688 689 690 691 692 693 694
static int do_launder_page(struct address_space *mapping, struct page *page)
{
	if (!PageDirty(page))
		return 0;
	if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
		return 0;
	return mapping->a_ops->launder_page(page);
}

L
Linus Torvalds 已提交
695 696
/**
 * invalidate_inode_pages2_range - remove range of pages from an address_space
697
 * @mapping: the address_space
L
Linus Torvalds 已提交
698 699 700 701 702 703
 * @start: the page offset 'from' which to invalidate
 * @end: the page offset 'to' which to invalidate (inclusive)
 *
 * Any pages which are found to be mapped into pagetables are unmapped prior to
 * invalidation.
 *
704
 * Return: -EBUSY if any pages could not be invalidated.
L
Linus Torvalds 已提交
705 706 707 708
 */
int invalidate_inode_pages2_range(struct address_space *mapping,
				  pgoff_t start, pgoff_t end)
{
709
	pgoff_t indices[PAGEVEC_SIZE];
L
Linus Torvalds 已提交
710
	struct pagevec pvec;
711
	pgoff_t index;
L
Linus Torvalds 已提交
712 713
	int i;
	int ret = 0;
714
	int ret2 = 0;
L
Linus Torvalds 已提交
715 716
	int did_range_unmap = 0;

717
	if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
718
		goto out;
719

720
	pagevec_init(&pvec);
721
	index = start;
722 723 724
	while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
			min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
			indices)) {
725
		for (i = 0; i < pagevec_count(&pvec); i++) {
L
Linus Torvalds 已提交
726
			struct page *page = pvec.pages[i];
727 728

			/* We rely upon deletion not changing page->index */
729
			index = indices[i];
730 731
			if (index > end)
				break;
L
Linus Torvalds 已提交
732

733
			if (xa_is_value(page)) {
734 735 736
				if (!invalidate_exceptional_entry2(mapping,
								   index, page))
					ret = -EBUSY;
737 738 739
				continue;
			}

L
Linus Torvalds 已提交
740
			lock_page(page);
741
			WARN_ON(page_to_index(page) != index);
L
Linus Torvalds 已提交
742 743 744 745 746
			if (page->mapping != mapping) {
				unlock_page(page);
				continue;
			}
			wait_on_page_writeback(page);
747
			if (page_mapped(page)) {
L
Linus Torvalds 已提交
748 749 750 751
				if (!did_range_unmap) {
					/*
					 * Zap the rest of the file in one hit.
					 */
M
Matthew Wilcox 已提交
752 753
					unmap_mapping_pages(mapping, index,
						(1 + end - index), false);
L
Linus Torvalds 已提交
754 755 756 757 758
					did_range_unmap = 1;
				} else {
					/*
					 * Just zap this page
					 */
M
Matthew Wilcox 已提交
759 760
					unmap_mapping_pages(mapping, index,
								1, false);
L
Linus Torvalds 已提交
761 762
				}
			}
763
			BUG_ON(page_mapped(page));
764 765 766
			ret2 = do_launder_page(mapping, page);
			if (ret2 == 0) {
				if (!invalidate_complete_page2(mapping, page))
767
					ret2 = -EBUSY;
768 769 770
			}
			if (ret2 < 0)
				ret = ret2;
L
Linus Torvalds 已提交
771 772
			unlock_page(page);
		}
773
		pagevec_remove_exceptionals(&pvec);
L
Linus Torvalds 已提交
774 775
		pagevec_release(&pvec);
		cond_resched();
776
		index++;
L
Linus Torvalds 已提交
777
	}
778
	/*
M
Matthew Wilcox 已提交
779
	 * For DAX we invalidate page tables after invalidating page cache.  We
780 781
	 * could invalidate page tables while invalidating each entry however
	 * that would be expensive. And doing range unmapping before doesn't
M
Matthew Wilcox 已提交
782
	 * work as we have no cheap way to find whether page cache entry didn't
783 784 785
	 * get remapped later.
	 */
	if (dax_mapping(mapping)) {
M
Matthew Wilcox 已提交
786
		unmap_mapping_pages(mapping, start, end - start + 1, false);
787
	}
788
out:
789
	cleancache_invalidate_inode(mapping);
L
Linus Torvalds 已提交
790 791 792 793 794 795
	return ret;
}
EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);

/**
 * invalidate_inode_pages2 - remove all pages from an address_space
796
 * @mapping: the address_space
L
Linus Torvalds 已提交
797 798 799 800
 *
 * Any pages which are found to be mapped into pagetables are unmapped prior to
 * invalidation.
 *
801
 * Return: -EBUSY if any pages could not be invalidated.
L
Linus Torvalds 已提交
802 803 804 805 806 807
 */
int invalidate_inode_pages2(struct address_space *mapping)
{
	return invalidate_inode_pages2_range(mapping, 0, -1);
}
EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
N
npiggin@suse.de 已提交
808 809 810 811

/**
 * truncate_pagecache - unmap and remove pagecache that has been truncated
 * @inode: inode
812
 * @newsize: new file size
N
npiggin@suse.de 已提交
813 814 815 816 817 818 819 820 821 822 823
 *
 * inode's new i_size must already be written before truncate_pagecache
 * is called.
 *
 * This function should typically be called before the filesystem
 * releases resources associated with the freed range (eg. deallocates
 * blocks). This way, pagecache will always stay logically coherent
 * with on-disk format, and the filesystem would not have to deal with
 * situations such as writepage being called for a page that has already
 * had its underlying blocks deallocated.
 */
824
void truncate_pagecache(struct inode *inode, loff_t newsize)
N
npiggin@suse.de 已提交
825
{
826
	struct address_space *mapping = inode->i_mapping;
827
	loff_t holebegin = round_up(newsize, PAGE_SIZE);
828 829 830 831 832 833 834 835 836 837

	/*
	 * unmap_mapping_range is called twice, first simply for
	 * efficiency so that truncate_inode_pages does fewer
	 * single-page unmaps.  However after this first call, and
	 * before truncate_inode_pages finishes, it is possible for
	 * private pages to be COWed, which remain after
	 * truncate_inode_pages finishes, hence the second
	 * unmap_mapping_range call must be made for correctness.
	 */
838 839 840
	unmap_mapping_range(mapping, holebegin, 0, 1);
	truncate_inode_pages(mapping, newsize);
	unmap_mapping_range(mapping, holebegin, 0, 1);
N
npiggin@suse.de 已提交
841 842 843
}
EXPORT_SYMBOL(truncate_pagecache);

844 845 846 847 848
/**
 * truncate_setsize - update inode and pagecache for a new file size
 * @inode: inode
 * @newsize: new file size
 *
J
Jan Kara 已提交
849 850 851
 * truncate_setsize updates i_size and performs pagecache truncation (if
 * necessary) to @newsize. It will be typically be called from the filesystem's
 * setattr function when ATTR_SIZE is passed in.
852
 *
853 854 855
 * Must be called with a lock serializing truncates and writes (generally
 * i_mutex but e.g. xfs uses a different lock) and before all filesystem
 * specific block truncation has been performed.
856 857 858
 */
void truncate_setsize(struct inode *inode, loff_t newsize)
{
859 860
	loff_t oldsize = inode->i_size;

861
	i_size_write(inode, newsize);
862 863
	if (newsize > oldsize)
		pagecache_isize_extended(inode, oldsize, newsize);
864
	truncate_pagecache(inode, newsize);
865 866 867
}
EXPORT_SYMBOL(truncate_setsize);

868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
/**
 * pagecache_isize_extended - update pagecache after extension of i_size
 * @inode:	inode for which i_size was extended
 * @from:	original inode size
 * @to:		new inode size
 *
 * Handle extension of inode size either caused by extending truncate or by
 * write starting after current i_size. We mark the page straddling current
 * i_size RO so that page_mkwrite() is called on the nearest write access to
 * the page.  This way filesystem can be sure that page_mkwrite() is called on
 * the page before user writes to the page via mmap after the i_size has been
 * changed.
 *
 * The function must be called after i_size is updated so that page fault
 * coming after we unlock the page will already see the new i_size.
 * The function must be called while we still hold i_mutex - this not only
 * makes sure i_size is stable but also that userspace cannot observe new
 * i_size value before we are prepared to store mmap writes at new inode size.
 */
void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to)
{
F
Fabian Frederick 已提交
889
	int bsize = i_blocksize(inode);
890 891 892 893 894 895
	loff_t rounded_from;
	struct page *page;
	pgoff_t index;

	WARN_ON(to > inode->i_size);

896
	if (from >= to || bsize == PAGE_SIZE)
897 898 899
		return;
	/* Page straddling @from will not have any hole block created? */
	rounded_from = round_up(from, bsize);
900
	if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1)))
901 902
		return;

903
	index = from >> PAGE_SHIFT;
904 905 906 907 908 909 910 911 912 913 914
	page = find_lock_page(inode->i_mapping, index);
	/* Page not cached? Nothing to do */
	if (!page)
		return;
	/*
	 * See clear_page_dirty_for_io() for details why set_page_dirty()
	 * is needed.
	 */
	if (page_mkclean(page))
		set_page_dirty(page);
	unlock_page(page);
915
	put_page(page);
916 917 918
}
EXPORT_SYMBOL(pagecache_isize_extended);

919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
/**
 * truncate_pagecache_range - unmap and remove pagecache that is hole-punched
 * @inode: inode
 * @lstart: offset of beginning of hole
 * @lend: offset of last byte of hole
 *
 * This function should typically be called before the filesystem
 * releases resources associated with the freed range (eg. deallocates
 * blocks). This way, pagecache will always stay logically coherent
 * with on-disk format, and the filesystem would not have to deal with
 * situations such as writepage being called for a page that has already
 * had its underlying blocks deallocated.
 */
void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
{
	struct address_space *mapping = inode->i_mapping;
	loff_t unmap_start = round_up(lstart, PAGE_SIZE);
	loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
	/*
	 * This rounding is currently just for example: unmap_mapping_range
	 * expands its hole outwards, whereas we want it to contract the hole
	 * inwards.  However, existing callers of truncate_pagecache_range are
941 942
	 * doing their own page rounding first.  Note that unmap_mapping_range
	 * allows holelen 0 for all, and we allow lend -1 for end of file.
943 944 945 946 947 948 949 950 951 952 953 954 955
	 */

	/*
	 * Unlike in truncate_pagecache, unmap_mapping_range is called only
	 * once (before truncating pagecache), and without "even_cows" flag:
	 * hole-punching should not remove private COWed pages from the hole.
	 */
	if ((u64)unmap_end > (u64)unmap_start)
		unmap_mapping_range(mapping, unmap_start,
				    1 + unmap_end - unmap_start, 0);
	truncate_inode_pages_range(mapping, lstart, lend);
}
EXPORT_SYMBOL(truncate_pagecache_range);