Commit | Line | Data |
---|---|---|
9d0243bc AM |
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> | |
6fa3eb70 | 11 | #include <linux/export.h> |
55fa6091 | 12 | #include "internal.h" |
9d0243bc AM |
13 | |
14 | /* A global variable is a bit ugly, but it keeps the code simple */ | |
15 | int sysctl_drop_caches; | |
16 | ||
01a05b33 | 17 | static void drop_pagecache_sb(struct super_block *sb, void *unused) |
9d0243bc | 18 | { |
eccb95ce | 19 | struct inode *inode, *toput_inode = NULL; |
9d0243bc | 20 | |
55fa6091 | 21 | spin_lock(&inode_sb_list_lock); |
9d0243bc | 22 | list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { |
250df6ed DC |
23 | spin_lock(&inode->i_lock); |
24 | if ((inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW)) || | |
25 | (inode->i_mapping->nrpages == 0)) { | |
26 | spin_unlock(&inode->i_lock); | |
af065b8a | 27 | continue; |
250df6ed | 28 | } |
eccb95ce | 29 | __iget(inode); |
250df6ed | 30 | spin_unlock(&inode->i_lock); |
55fa6091 | 31 | spin_unlock(&inode_sb_list_lock); |
28697355 | 32 | invalidate_mapping_pages(inode->i_mapping, 0, -1); |
eccb95ce JK |
33 | iput(toput_inode); |
34 | toput_inode = inode; | |
55fa6091 | 35 | spin_lock(&inode_sb_list_lock); |
9d0243bc | 36 | } |
55fa6091 | 37 | spin_unlock(&inode_sb_list_lock); |
eccb95ce | 38 | iput(toput_inode); |
9d0243bc AM |
39 | } |
40 | ||
07d45da6 | 41 | static void drop_slab(void) |
9d0243bc AM |
42 | { |
43 | int nr_objects; | |
a09ed5e0 YH |
44 | struct shrink_control shrink = { |
45 | .gfp_mask = GFP_KERNEL, | |
a09ed5e0 | 46 | }; |
9d0243bc AM |
47 | |
48 | do { | |
1495f230 | 49 | nr_objects = shrink_slab(&shrink, 1000, 1000); |
9d0243bc AM |
50 | } while (nr_objects > 10); |
51 | } | |
52 | ||
6fa3eb70 S |
53 | /* For TuxOnIce */ |
54 | void drop_pagecache(void) | |
55 | { | |
56 | iterate_supers(drop_pagecache_sb, NULL); | |
57 | } | |
58 | EXPORT_SYMBOL_GPL(drop_pagecache); | |
59 | ||
9d0243bc | 60 | int drop_caches_sysctl_handler(ctl_table *table, int write, |
8d65af78 | 61 | void __user *buffer, size_t *length, loff_t *ppos) |
9d0243bc | 62 | { |
cb16e95f PH |
63 | int ret; |
64 | ||
65 | ret = proc_dointvec_minmax(table, write, buffer, length, ppos); | |
66 | if (ret) | |
67 | return ret; | |
9d0243bc AM |
68 | if (write) { |
69 | if (sysctl_drop_caches & 1) | |
01a05b33 | 70 | iterate_supers(drop_pagecache_sb, NULL); |
9d0243bc AM |
71 | if (sysctl_drop_caches & 2) |
72 | drop_slab(); | |
73 | } | |
74 | return 0; | |
75 | } |