Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Implement the manual drop-all-pagecache function |
| 3 | */ |
| 4 | |
| 5 | #include <linux/kernel.h> |
| 6 | #include <linux/mm.h> |
| 7 | #include <linux/fs.h> |
| 8 | #include <linux/writeback.h> |
| 9 | #include <linux/sysctl.h> |
| 10 | #include <linux/gfp.h> |
Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 11 | #include "internal.h" |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 12 | |
| 13 | /* A global variable is a bit ugly, but it keeps the code simple */ |
| 14 | int sysctl_drop_caches; |
| 15 | |
Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 16 | static void drop_pagecache_sb(struct super_block *sb, void *unused) |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 17 | { |
Jan Kara | eccb95c | 2008-04-29 00:59:37 -0700 | [diff] [blame] | 18 | struct inode *inode, *toput_inode = NULL; |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 19 | |
Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 20 | spin_lock(&inode_sb_list_lock); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 21 | list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 22 | spin_lock(&inode->i_lock); |
| 23 | if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) || |
| 24 | (inode->i_mapping->nrpages == 0)) { |
| 25 | spin_unlock(&inode->i_lock); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 26 | continue; |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 27 | } |
Jan Kara | eccb95c | 2008-04-29 00:59:37 -0700 | [diff] [blame] | 28 | __iget(inode); |
Dave Chinner | 250df6e | 2011-03-22 22:23:36 +1100 | [diff] [blame] | 29 | spin_unlock(&inode->i_lock); |
Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 30 | spin_unlock(&inode_sb_list_lock); |
Mike Waychison | 2869735 | 2009-06-16 15:32:59 -0700 | [diff] [blame] | 31 | invalidate_mapping_pages(inode->i_mapping, 0, -1); |
Jan Kara | eccb95c | 2008-04-29 00:59:37 -0700 | [diff] [blame] | 32 | iput(toput_inode); |
| 33 | toput_inode = inode; |
Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 34 | spin_lock(&inode_sb_list_lock); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 35 | } |
Dave Chinner | 55fa609 | 2011-03-22 22:23:40 +1100 | [diff] [blame] | 36 | spin_unlock(&inode_sb_list_lock); |
Jan Kara | eccb95c | 2008-04-29 00:59:37 -0700 | [diff] [blame] | 37 | iput(toput_inode); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Adrian Bunk | 07d45da | 2008-04-29 00:58:57 -0700 | [diff] [blame] | 40 | static void drop_slab(void) |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 41 | { |
| 42 | int nr_objects; |
| 43 | |
| 44 | do { |
| 45 | nr_objects = shrink_slab(1000, GFP_KERNEL, 1000); |
| 46 | } while (nr_objects > 10); |
| 47 | } |
| 48 | |
| 49 | int drop_caches_sysctl_handler(ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 50 | void __user *buffer, size_t *length, loff_t *ppos) |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 51 | { |
Petr Holasek | cb16e95 | 2011-03-23 16:43:09 -0700 | [diff] [blame] | 52 | int ret; |
| 53 | |
| 54 | ret = proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 55 | if (ret) |
| 56 | return ret; |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 57 | if (write) { |
| 58 | if (sysctl_drop_caches & 1) |
Al Viro | 01a05b3 | 2010-03-23 06:06:58 -0400 | [diff] [blame] | 59 | iterate_supers(drop_pagecache_sb, NULL); |
Andrew Morton | 9d0243b | 2006-01-08 01:00:39 -0800 | [diff] [blame] | 60 | if (sysctl_drop_caches & 2) |
| 61 | drop_slab(); |
| 62 | } |
| 63 | return 0; |
| 64 | } |