Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * mm/truncate.c - code for taking down pages from address_spaces |
| 3 | * |
| 4 | * Copyright (C) 2002, Linus Torvalds |
| 5 | * |
| 6 | * 10Sep2002 akpm@zip.com.au |
| 7 | * Initial version. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/mm.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/pagemap.h> |
| 14 | #include <linux/pagevec.h> |
| 15 | #include <linux/buffer_head.h> /* grr. try_to_release_page, |
Jan Kara | aaa4059 | 2005-10-30 15:00:16 -0800 | [diff] [blame] | 16 | do_invalidatepage */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | static inline void truncate_partial_page(struct page *page, unsigned partial) |
| 20 | { |
| 21 | memclear_highpage_flush(page, partial, PAGE_CACHE_SIZE-partial); |
| 22 | if (PagePrivate(page)) |
| 23 | do_invalidatepage(page, partial); |
| 24 | } |
| 25 | |
| 26 | /* |
| 27 | * If truncate cannot remove the fs-private metadata from the page, the page |
| 28 | * becomes anonymous. It will be left on the LRU and may even be mapped into |
| 29 | * user pagetables if we're racing with filemap_nopage(). |
| 30 | * |
| 31 | * We need to bale out if page->mapping is no longer equal to the original |
| 32 | * mapping. This happens a) when the VM reclaimed the page while we waited on |
| 33 | * its lock, b) when a concurrent invalidate_inode_pages got there first and |
| 34 | * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space. |
| 35 | */ |
| 36 | static void |
| 37 | truncate_complete_page(struct address_space *mapping, struct page *page) |
| 38 | { |
| 39 | if (page->mapping != mapping) |
| 40 | return; |
| 41 | |
| 42 | if (PagePrivate(page)) |
| 43 | do_invalidatepage(page, 0); |
| 44 | |
| 45 | clear_page_dirty(page); |
| 46 | ClearPageUptodate(page); |
| 47 | ClearPageMappedToDisk(page); |
| 48 | remove_from_page_cache(page); |
| 49 | page_cache_release(page); /* pagecache ref */ |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * This is for invalidate_inode_pages(). That function can be called at |
| 54 | * any time, and is not supposed to throw away dirty pages. But pages can |
| 55 | * be marked dirty at any time too. So we re-check the dirtiness inside |
| 56 | * ->tree_lock. That provides exclusion against the __set_page_dirty |
| 57 | * functions. |
| 58 | * |
| 59 | * Returns non-zero if the page was successfully invalidated. |
| 60 | */ |
| 61 | static int |
| 62 | invalidate_complete_page(struct address_space *mapping, struct page *page) |
| 63 | { |
| 64 | if (page->mapping != mapping) |
| 65 | return 0; |
| 66 | |
| 67 | if (PagePrivate(page) && !try_to_release_page(page, 0)) |
| 68 | return 0; |
| 69 | |
| 70 | write_lock_irq(&mapping->tree_lock); |
| 71 | if (PageDirty(page)) { |
| 72 | write_unlock_irq(&mapping->tree_lock); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | BUG_ON(PagePrivate(page)); |
| 77 | __remove_from_page_cache(page); |
| 78 | write_unlock_irq(&mapping->tree_lock); |
| 79 | ClearPageUptodate(page); |
| 80 | page_cache_release(page); /* pagecache ref */ |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | /** |
Hans Reiser | d733907 | 2006-01-06 00:10:36 -0800 | [diff] [blame] | 85 | * truncate_inode_pages - truncate range of pages specified by start and |
| 86 | * end byte offsets |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | * @mapping: mapping to truncate |
| 88 | * @lstart: offset from which to truncate |
Hans Reiser | d733907 | 2006-01-06 00:10:36 -0800 | [diff] [blame] | 89 | * @lend: offset to which to truncate |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | * |
Hans Reiser | d733907 | 2006-01-06 00:10:36 -0800 | [diff] [blame] | 91 | * Truncate the page cache, removing the pages that are between |
| 92 | * specified offsets (and zeroing out partial page |
| 93 | * (if lstart is not page aligned)). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | * |
| 95 | * Truncate takes two passes - the first pass is nonblocking. It will not |
| 96 | * block on page locks and it will not block on writeback. The second pass |
| 97 | * will wait. This is to prevent as much IO as possible in the affected region. |
| 98 | * The first pass will remove most pages, so the search cost of the second pass |
| 99 | * is low. |
| 100 | * |
| 101 | * When looking at page->index outside the page lock we need to be careful to |
| 102 | * copy it into a local to avoid races (it could change at any time). |
| 103 | * |
| 104 | * We pass down the cache-hot hint to the page freeing code. Even if the |
| 105 | * mapping is large, it is probably the case that the final pages are the most |
| 106 | * recently touched, and freeing happens in ascending file offset order. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | */ |
Hans Reiser | d733907 | 2006-01-06 00:10:36 -0800 | [diff] [blame] | 108 | void truncate_inode_pages_range(struct address_space *mapping, |
| 109 | loff_t lstart, loff_t lend) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
| 111 | const pgoff_t start = (lstart + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT; |
Hans Reiser | d733907 | 2006-01-06 00:10:36 -0800 | [diff] [blame] | 112 | pgoff_t end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | const unsigned partial = lstart & (PAGE_CACHE_SIZE - 1); |
| 114 | struct pagevec pvec; |
| 115 | pgoff_t next; |
| 116 | int i; |
| 117 | |
| 118 | if (mapping->nrpages == 0) |
| 119 | return; |
| 120 | |
Hans Reiser | d733907 | 2006-01-06 00:10:36 -0800 | [diff] [blame] | 121 | BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1)); |
| 122 | end = (lend >> PAGE_CACHE_SHIFT); |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | pagevec_init(&pvec, 0); |
| 125 | next = start; |
Hans Reiser | d733907 | 2006-01-06 00:10:36 -0800 | [diff] [blame] | 126 | while (next <= end && |
| 127 | pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 129 | struct page *page = pvec.pages[i]; |
| 130 | pgoff_t page_index = page->index; |
| 131 | |
Hans Reiser | d733907 | 2006-01-06 00:10:36 -0800 | [diff] [blame] | 132 | if (page_index > end) { |
| 133 | next = page_index; |
| 134 | break; |
| 135 | } |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | if (page_index > next) |
| 138 | next = page_index; |
| 139 | next++; |
| 140 | if (TestSetPageLocked(page)) |
| 141 | continue; |
| 142 | if (PageWriteback(page)) { |
| 143 | unlock_page(page); |
| 144 | continue; |
| 145 | } |
| 146 | truncate_complete_page(mapping, page); |
| 147 | unlock_page(page); |
| 148 | } |
| 149 | pagevec_release(&pvec); |
| 150 | cond_resched(); |
| 151 | } |
| 152 | |
| 153 | if (partial) { |
| 154 | struct page *page = find_lock_page(mapping, start - 1); |
| 155 | if (page) { |
| 156 | wait_on_page_writeback(page); |
| 157 | truncate_partial_page(page, partial); |
| 158 | unlock_page(page); |
| 159 | page_cache_release(page); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | next = start; |
| 164 | for ( ; ; ) { |
| 165 | cond_resched(); |
| 166 | if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) { |
| 167 | if (next == start) |
| 168 | break; |
| 169 | next = start; |
| 170 | continue; |
| 171 | } |
Hans Reiser | d733907 | 2006-01-06 00:10:36 -0800 | [diff] [blame] | 172 | if (pvec.pages[0]->index > end) { |
| 173 | pagevec_release(&pvec); |
| 174 | break; |
| 175 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 177 | struct page *page = pvec.pages[i]; |
| 178 | |
Hans Reiser | d733907 | 2006-01-06 00:10:36 -0800 | [diff] [blame] | 179 | if (page->index > end) |
| 180 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | lock_page(page); |
| 182 | wait_on_page_writeback(page); |
| 183 | if (page->index > next) |
| 184 | next = page->index; |
| 185 | next++; |
| 186 | truncate_complete_page(mapping, page); |
| 187 | unlock_page(page); |
| 188 | } |
| 189 | pagevec_release(&pvec); |
| 190 | } |
| 191 | } |
Hans Reiser | d733907 | 2006-01-06 00:10:36 -0800 | [diff] [blame] | 192 | EXPORT_SYMBOL(truncate_inode_pages_range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Hans Reiser | d733907 | 2006-01-06 00:10:36 -0800 | [diff] [blame] | 194 | /** |
| 195 | * truncate_inode_pages - truncate *all* the pages from an offset |
| 196 | * @mapping: mapping to truncate |
| 197 | * @lstart: offset from which to truncate |
| 198 | * |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 199 | * Called under (and serialised by) inode->i_mutex. |
Hans Reiser | d733907 | 2006-01-06 00:10:36 -0800 | [diff] [blame] | 200 | */ |
| 201 | void truncate_inode_pages(struct address_space *mapping, loff_t lstart) |
| 202 | { |
| 203 | truncate_inode_pages_range(mapping, lstart, (loff_t)-1); |
| 204 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | EXPORT_SYMBOL(truncate_inode_pages); |
| 206 | |
| 207 | /** |
| 208 | * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode |
| 209 | * @mapping: the address_space which holds the pages to invalidate |
| 210 | * @start: the offset 'from' which to invalidate |
| 211 | * @end: the offset 'to' which to invalidate (inclusive) |
| 212 | * |
| 213 | * This function only removes the unlocked pages, if you want to |
| 214 | * remove all the pages of one inode, you must call truncate_inode_pages. |
| 215 | * |
| 216 | * invalidate_mapping_pages() will not block on IO activity. It will not |
| 217 | * invalidate pages which are dirty, locked, under writeback or mapped into |
| 218 | * pagetables. |
| 219 | */ |
| 220 | unsigned long invalidate_mapping_pages(struct address_space *mapping, |
| 221 | pgoff_t start, pgoff_t end) |
| 222 | { |
| 223 | struct pagevec pvec; |
| 224 | pgoff_t next = start; |
| 225 | unsigned long ret = 0; |
| 226 | int i; |
| 227 | |
| 228 | pagevec_init(&pvec, 0); |
| 229 | while (next <= end && |
| 230 | pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) { |
| 231 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 232 | struct page *page = pvec.pages[i]; |
NeilBrown | e0f2360 | 2006-06-23 02:05:48 -0700 | [diff] [blame] | 233 | pgoff_t index; |
| 234 | int lock_failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
NeilBrown | e0f2360 | 2006-06-23 02:05:48 -0700 | [diff] [blame] | 236 | lock_failed = TestSetPageLocked(page); |
| 237 | |
| 238 | /* |
| 239 | * We really shouldn't be looking at the ->index of an |
| 240 | * unlocked page. But we're not allowed to lock these |
| 241 | * pages. So we rely upon nobody altering the ->index |
| 242 | * of this (pinned-by-us) page. |
| 243 | */ |
| 244 | index = page->index; |
| 245 | if (index > next) |
| 246 | next = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | next++; |
NeilBrown | e0f2360 | 2006-06-23 02:05:48 -0700 | [diff] [blame] | 248 | if (lock_failed) |
| 249 | continue; |
| 250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | if (PageDirty(page) || PageWriteback(page)) |
| 252 | goto unlock; |
| 253 | if (page_mapped(page)) |
| 254 | goto unlock; |
| 255 | ret += invalidate_complete_page(mapping, page); |
| 256 | unlock: |
| 257 | unlock_page(page); |
| 258 | if (next > end) |
| 259 | break; |
| 260 | } |
| 261 | pagevec_release(&pvec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | } |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | unsigned long invalidate_inode_pages(struct address_space *mapping) |
| 267 | { |
| 268 | return invalidate_mapping_pages(mapping, 0, ~0UL); |
| 269 | } |
| 270 | |
| 271 | EXPORT_SYMBOL(invalidate_inode_pages); |
| 272 | |
| 273 | /** |
| 274 | * invalidate_inode_pages2_range - remove range of pages from an address_space |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 275 | * @mapping: the address_space |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | * @start: the page offset 'from' which to invalidate |
| 277 | * @end: the page offset 'to' which to invalidate (inclusive) |
| 278 | * |
| 279 | * Any pages which are found to be mapped into pagetables are unmapped prior to |
| 280 | * invalidation. |
| 281 | * |
| 282 | * Returns -EIO if any pages could not be invalidated. |
| 283 | */ |
| 284 | int invalidate_inode_pages2_range(struct address_space *mapping, |
| 285 | pgoff_t start, pgoff_t end) |
| 286 | { |
| 287 | struct pagevec pvec; |
| 288 | pgoff_t next; |
| 289 | int i; |
| 290 | int ret = 0; |
| 291 | int did_range_unmap = 0; |
| 292 | int wrapped = 0; |
| 293 | |
| 294 | pagevec_init(&pvec, 0); |
| 295 | next = start; |
| 296 | while (next <= end && !ret && !wrapped && |
| 297 | pagevec_lookup(&pvec, mapping, next, |
| 298 | min(end - next, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) { |
| 299 | for (i = 0; !ret && i < pagevec_count(&pvec); i++) { |
| 300 | struct page *page = pvec.pages[i]; |
| 301 | pgoff_t page_index; |
| 302 | int was_dirty; |
| 303 | |
| 304 | lock_page(page); |
| 305 | if (page->mapping != mapping) { |
| 306 | unlock_page(page); |
| 307 | continue; |
| 308 | } |
| 309 | page_index = page->index; |
| 310 | next = page_index + 1; |
| 311 | if (next == 0) |
| 312 | wrapped = 1; |
| 313 | if (page_index > end) { |
| 314 | unlock_page(page); |
| 315 | break; |
| 316 | } |
| 317 | wait_on_page_writeback(page); |
| 318 | while (page_mapped(page)) { |
| 319 | if (!did_range_unmap) { |
| 320 | /* |
| 321 | * Zap the rest of the file in one hit. |
| 322 | */ |
| 323 | unmap_mapping_range(mapping, |
Oleg Drokin | 479ef59 | 2005-11-23 13:37:47 -0800 | [diff] [blame] | 324 | (loff_t)page_index<<PAGE_CACHE_SHIFT, |
| 325 | (loff_t)(end - page_index + 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | << PAGE_CACHE_SHIFT, |
| 327 | 0); |
| 328 | did_range_unmap = 1; |
| 329 | } else { |
| 330 | /* |
| 331 | * Just zap this page |
| 332 | */ |
| 333 | unmap_mapping_range(mapping, |
Oleg Drokin | 479ef59 | 2005-11-23 13:37:47 -0800 | [diff] [blame] | 334 | (loff_t)page_index<<PAGE_CACHE_SHIFT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | PAGE_CACHE_SIZE, 0); |
| 336 | } |
| 337 | } |
| 338 | was_dirty = test_clear_page_dirty(page); |
| 339 | if (!invalidate_complete_page(mapping, page)) { |
| 340 | if (was_dirty) |
| 341 | set_page_dirty(page); |
| 342 | ret = -EIO; |
| 343 | } |
| 344 | unlock_page(page); |
| 345 | } |
| 346 | pagevec_release(&pvec); |
| 347 | cond_resched(); |
| 348 | } |
| 349 | return ret; |
| 350 | } |
| 351 | EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range); |
| 352 | |
| 353 | /** |
| 354 | * invalidate_inode_pages2 - remove all pages from an address_space |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 355 | * @mapping: the address_space |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | * |
| 357 | * Any pages which are found to be mapped into pagetables are unmapped prior to |
| 358 | * invalidation. |
| 359 | * |
| 360 | * Returns -EIO if any pages could not be invalidated. |
| 361 | */ |
| 362 | int invalidate_inode_pages2(struct address_space *mapping) |
| 363 | { |
| 364 | return invalidate_inode_pages2_range(mapping, 0, -1); |
| 365 | } |
| 366 | EXPORT_SYMBOL_GPL(invalidate_inode_pages2); |