Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Resizable virtual memory filesystem for Linux. | |
3 | * | |
4 | * Copyright (C) 2000 Linus Torvalds. | |
5 | * 2000 Transmeta Corp. | |
6 | * 2000-2001 Christoph Rohland | |
7 | * 2000-2001 SAP AG | |
8 | * 2002 Red Hat Inc. | |
0edd73b3 HD |
9 | * Copyright (C) 2002-2005 Hugh Dickins. |
10 | * Copyright (C) 2002-2005 VERITAS Software Corporation. | |
1da177e4 LT |
11 | * Copyright (C) 2004 Andi Kleen, SuSE Labs |
12 | * | |
13 | * Extended attribute support for tmpfs: | |
14 | * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> | |
15 | * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> | |
16 | * | |
853ac43a MM |
17 | * tiny-shmem: |
18 | * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> | |
19 | * | |
1da177e4 LT |
20 | * This file is released under the GPL. |
21 | */ | |
22 | ||
853ac43a MM |
23 | #include <linux/fs.h> |
24 | #include <linux/init.h> | |
25 | #include <linux/vfs.h> | |
26 | #include <linux/mount.h> | |
caefba17 | 27 | #include <linux/pagemap.h> |
853ac43a MM |
28 | #include <linux/file.h> |
29 | #include <linux/mm.h> | |
30 | #include <linux/module.h> | |
31 | #include <linux/swap.h> | |
32 | ||
33 | static struct vfsmount *shm_mnt; | |
34 | ||
35 | #ifdef CONFIG_SHMEM | |
1da177e4 LT |
36 | /* |
37 | * This virtual memory filesystem is heavily based on the ramfs. It | |
38 | * extends ramfs by the ability to use swap and honor resource limits | |
39 | * which makes it a completely usable filesystem. | |
40 | */ | |
41 | ||
39f0247d | 42 | #include <linux/xattr.h> |
a5694255 | 43 | #include <linux/exportfs.h> |
1c7c474c | 44 | #include <linux/posix_acl.h> |
39f0247d | 45 | #include <linux/generic_acl.h> |
1da177e4 | 46 | #include <linux/mman.h> |
1da177e4 LT |
47 | #include <linux/string.h> |
48 | #include <linux/slab.h> | |
49 | #include <linux/backing-dev.h> | |
50 | #include <linux/shmem_fs.h> | |
1da177e4 | 51 | #include <linux/writeback.h> |
1da177e4 | 52 | #include <linux/blkdev.h> |
bda97eab | 53 | #include <linux/pagevec.h> |
41ffe5d5 | 54 | #include <linux/percpu_counter.h> |
708e3508 | 55 | #include <linux/splice.h> |
1da177e4 LT |
56 | #include <linux/security.h> |
57 | #include <linux/swapops.h> | |
58 | #include <linux/mempolicy.h> | |
59 | #include <linux/namei.h> | |
b00dc3ad | 60 | #include <linux/ctype.h> |
304dbdb7 | 61 | #include <linux/migrate.h> |
c1f60a5a | 62 | #include <linux/highmem.h> |
680d794b | 63 | #include <linux/seq_file.h> |
92562927 | 64 | #include <linux/magic.h> |
304dbdb7 | 65 | |
1da177e4 | 66 | #include <asm/uaccess.h> |
1da177e4 LT |
67 | #include <asm/pgtable.h> |
68 | ||
caefba17 | 69 | #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512) |
1da177e4 LT |
70 | #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT) |
71 | ||
1da177e4 LT |
72 | /* Pretend that each entry is of this size in directory's i_size */ |
73 | #define BOGO_DIRENT_SIZE 20 | |
74 | ||
b09e0fa4 EP |
75 | struct shmem_xattr { |
76 | struct list_head list; /* anchored by shmem_inode_info->xattr_list */ | |
77 | char *name; /* xattr name */ | |
78 | size_t size; | |
79 | char value[0]; | |
80 | }; | |
81 | ||
285b2c4f | 82 | /* Flag allocation requirements to shmem_getpage */ |
1da177e4 | 83 | enum sgp_type { |
1da177e4 LT |
84 | SGP_READ, /* don't exceed i_size, don't allocate page */ |
85 | SGP_CACHE, /* don't exceed i_size, may allocate page */ | |
a0ee5ec5 | 86 | SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */ |
1da177e4 LT |
87 | SGP_WRITE, /* may exceed i_size, may allocate page */ |
88 | }; | |
89 | ||
b76db735 | 90 | #ifdef CONFIG_TMPFS |
680d794b | 91 | static unsigned long shmem_default_max_blocks(void) |
92 | { | |
93 | return totalram_pages / 2; | |
94 | } | |
95 | ||
96 | static unsigned long shmem_default_max_inodes(void) | |
97 | { | |
98 | return min(totalram_pages - totalhigh_pages, totalram_pages / 2); | |
99 | } | |
b76db735 | 100 | #endif |
680d794b | 101 | |
68da9f05 HD |
102 | static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, |
103 | struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type); | |
104 | ||
105 | static inline int shmem_getpage(struct inode *inode, pgoff_t index, | |
106 | struct page **pagep, enum sgp_type sgp, int *fault_type) | |
107 | { | |
108 | return shmem_getpage_gfp(inode, index, pagep, sgp, | |
109 | mapping_gfp_mask(inode->i_mapping), fault_type); | |
110 | } | |
1da177e4 | 111 | |
1da177e4 LT |
112 | static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) |
113 | { | |
114 | return sb->s_fs_info; | |
115 | } | |
116 | ||
117 | /* | |
118 | * shmem_file_setup pre-accounts the whole fixed size of a VM object, | |
119 | * for shared memory and for shared anonymous (/dev/zero) mappings | |
120 | * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), | |
121 | * consistent with the pre-accounting of private mappings ... | |
122 | */ | |
123 | static inline int shmem_acct_size(unsigned long flags, loff_t size) | |
124 | { | |
0b0a0806 HD |
125 | return (flags & VM_NORESERVE) ? |
126 | 0 : security_vm_enough_memory_kern(VM_ACCT(size)); | |
1da177e4 LT |
127 | } |
128 | ||
129 | static inline void shmem_unacct_size(unsigned long flags, loff_t size) | |
130 | { | |
0b0a0806 | 131 | if (!(flags & VM_NORESERVE)) |
1da177e4 LT |
132 | vm_unacct_memory(VM_ACCT(size)); |
133 | } | |
134 | ||
135 | /* | |
136 | * ... whereas tmpfs objects are accounted incrementally as | |
137 | * pages are allocated, in order to allow huge sparse files. | |
138 | * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, | |
139 | * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. | |
140 | */ | |
141 | static inline int shmem_acct_block(unsigned long flags) | |
142 | { | |
0b0a0806 HD |
143 | return (flags & VM_NORESERVE) ? |
144 | security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0; | |
1da177e4 LT |
145 | } |
146 | ||
147 | static inline void shmem_unacct_blocks(unsigned long flags, long pages) | |
148 | { | |
0b0a0806 | 149 | if (flags & VM_NORESERVE) |
1da177e4 LT |
150 | vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE)); |
151 | } | |
152 | ||
759b9775 | 153 | static const struct super_operations shmem_ops; |
f5e54d6e | 154 | static const struct address_space_operations shmem_aops; |
15ad7cdc | 155 | static const struct file_operations shmem_file_operations; |
92e1d5be AV |
156 | static const struct inode_operations shmem_inode_operations; |
157 | static const struct inode_operations shmem_dir_inode_operations; | |
158 | static const struct inode_operations shmem_special_inode_operations; | |
f0f37e2f | 159 | static const struct vm_operations_struct shmem_vm_ops; |
1da177e4 | 160 | |
6c231b7b | 161 | static struct backing_dev_info shmem_backing_dev_info __read_mostly = { |
1da177e4 | 162 | .ra_pages = 0, /* No readahead */ |
4f98a2fe | 163 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED, |
1da177e4 LT |
164 | }; |
165 | ||
166 | static LIST_HEAD(shmem_swaplist); | |
cb5f7b9a | 167 | static DEFINE_MUTEX(shmem_swaplist_mutex); |
1da177e4 | 168 | |
5b04c689 PE |
169 | static int shmem_reserve_inode(struct super_block *sb) |
170 | { | |
171 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); | |
172 | if (sbinfo->max_inodes) { | |
173 | spin_lock(&sbinfo->stat_lock); | |
174 | if (!sbinfo->free_inodes) { | |
175 | spin_unlock(&sbinfo->stat_lock); | |
176 | return -ENOSPC; | |
177 | } | |
178 | sbinfo->free_inodes--; | |
179 | spin_unlock(&sbinfo->stat_lock); | |
180 | } | |
181 | return 0; | |
182 | } | |
183 | ||
184 | static void shmem_free_inode(struct super_block *sb) | |
185 | { | |
186 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); | |
187 | if (sbinfo->max_inodes) { | |
188 | spin_lock(&sbinfo->stat_lock); | |
189 | sbinfo->free_inodes++; | |
190 | spin_unlock(&sbinfo->stat_lock); | |
191 | } | |
192 | } | |
193 | ||
46711810 | 194 | /** |
41ffe5d5 | 195 | * shmem_recalc_inode - recalculate the block usage of an inode |
1da177e4 LT |
196 | * @inode: inode to recalc |
197 | * | |
198 | * We have to calculate the free blocks since the mm can drop | |
199 | * undirtied hole pages behind our back. | |
200 | * | |
201 | * But normally info->alloced == inode->i_mapping->nrpages + info->swapped | |
202 | * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) | |
203 | * | |
204 | * It has to be called with the spinlock held. | |
205 | */ | |
206 | static void shmem_recalc_inode(struct inode *inode) | |
207 | { | |
208 | struct shmem_inode_info *info = SHMEM_I(inode); | |
209 | long freed; | |
210 | ||
211 | freed = info->alloced - info->swapped - inode->i_mapping->nrpages; | |
212 | if (freed > 0) { | |
54af6042 HD |
213 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
214 | if (sbinfo->max_blocks) | |
215 | percpu_counter_add(&sbinfo->used_blocks, -freed); | |
1da177e4 | 216 | info->alloced -= freed; |
54af6042 | 217 | inode->i_blocks -= freed * BLOCKS_PER_PAGE; |
1da177e4 | 218 | shmem_unacct_blocks(info->flags, freed); |
1da177e4 LT |
219 | } |
220 | } | |
221 | ||
285b2c4f HD |
222 | static void shmem_put_swap(struct shmem_inode_info *info, pgoff_t index, |
223 | swp_entry_t swap) | |
1da177e4 | 224 | { |
285b2c4f HD |
225 | if (index < SHMEM_NR_DIRECT) |
226 | info->i_direct[index] = swap; | |
1da177e4 LT |
227 | } |
228 | ||
285b2c4f | 229 | static swp_entry_t shmem_get_swap(struct shmem_inode_info *info, pgoff_t index) |
1da177e4 | 230 | { |
285b2c4f HD |
231 | return (index < SHMEM_NR_DIRECT) ? |
232 | info->i_direct[index] : (swp_entry_t){0}; | |
1da177e4 LT |
233 | } |
234 | ||
7a5d0fbb HD |
235 | /* |
236 | * Replace item expected in radix tree by a new item, while holding tree lock. | |
237 | */ | |
238 | static int shmem_radix_tree_replace(struct address_space *mapping, | |
239 | pgoff_t index, void *expected, void *replacement) | |
240 | { | |
241 | void **pslot; | |
242 | void *item = NULL; | |
243 | ||
244 | VM_BUG_ON(!expected); | |
245 | pslot = radix_tree_lookup_slot(&mapping->page_tree, index); | |
246 | if (pslot) | |
247 | item = radix_tree_deref_slot_protected(pslot, | |
248 | &mapping->tree_lock); | |
249 | if (item != expected) | |
250 | return -ENOENT; | |
251 | if (replacement) | |
252 | radix_tree_replace_slot(pslot, replacement); | |
253 | else | |
254 | radix_tree_delete(&mapping->page_tree, index); | |
255 | return 0; | |
256 | } | |
257 | ||
46f65ec1 HD |
258 | /* |
259 | * Like add_to_page_cache_locked, but error if expected item has gone. | |
260 | */ | |
261 | static int shmem_add_to_page_cache(struct page *page, | |
262 | struct address_space *mapping, | |
263 | pgoff_t index, gfp_t gfp, void *expected) | |
264 | { | |
265 | int error; | |
266 | ||
267 | VM_BUG_ON(!PageLocked(page)); | |
268 | VM_BUG_ON(!PageSwapBacked(page)); | |
269 | ||
270 | error = mem_cgroup_cache_charge(page, current->mm, | |
271 | gfp & GFP_RECLAIM_MASK); | |
272 | if (error) | |
273 | goto out; | |
274 | if (!expected) | |
275 | error = radix_tree_preload(gfp & GFP_RECLAIM_MASK); | |
276 | if (!error) { | |
277 | page_cache_get(page); | |
278 | page->mapping = mapping; | |
279 | page->index = index; | |
280 | ||
281 | spin_lock_irq(&mapping->tree_lock); | |
282 | if (!expected) | |
283 | error = radix_tree_insert(&mapping->page_tree, | |
284 | index, page); | |
285 | else | |
286 | error = shmem_radix_tree_replace(mapping, index, | |
287 | expected, page); | |
288 | if (!error) { | |
289 | mapping->nrpages++; | |
290 | __inc_zone_page_state(page, NR_FILE_PAGES); | |
291 | __inc_zone_page_state(page, NR_SHMEM); | |
292 | spin_unlock_irq(&mapping->tree_lock); | |
293 | } else { | |
294 | page->mapping = NULL; | |
295 | spin_unlock_irq(&mapping->tree_lock); | |
296 | page_cache_release(page); | |
297 | } | |
298 | if (!expected) | |
299 | radix_tree_preload_end(); | |
300 | } | |
301 | if (error) | |
302 | mem_cgroup_uncharge_cache_page(page); | |
303 | out: | |
304 | return error; | |
305 | } | |
306 | ||
7a5d0fbb HD |
307 | /* |
308 | * Like find_get_pages, but collecting swap entries as well as pages. | |
309 | */ | |
310 | static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping, | |
311 | pgoff_t start, unsigned int nr_pages, | |
312 | struct page **pages, pgoff_t *indices) | |
313 | { | |
314 | unsigned int i; | |
315 | unsigned int ret; | |
316 | unsigned int nr_found; | |
317 | ||
318 | rcu_read_lock(); | |
319 | restart: | |
320 | nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree, | |
321 | (void ***)pages, indices, start, nr_pages); | |
322 | ret = 0; | |
323 | for (i = 0; i < nr_found; i++) { | |
324 | struct page *page; | |
325 | repeat: | |
326 | page = radix_tree_deref_slot((void **)pages[i]); | |
327 | if (unlikely(!page)) | |
328 | continue; | |
329 | if (radix_tree_exception(page)) { | |
330 | if (radix_tree_exceptional_entry(page)) | |
331 | goto export; | |
332 | /* radix_tree_deref_retry(page) */ | |
333 | goto restart; | |
334 | } | |
335 | if (!page_cache_get_speculative(page)) | |
336 | goto repeat; | |
337 | ||
338 | /* Has the page moved? */ | |
339 | if (unlikely(page != *((void **)pages[i]))) { | |
340 | page_cache_release(page); | |
341 | goto repeat; | |
342 | } | |
343 | export: | |
344 | indices[ret] = indices[i]; | |
345 | pages[ret] = page; | |
346 | ret++; | |
347 | } | |
348 | if (unlikely(!ret && nr_found)) | |
349 | goto restart; | |
350 | rcu_read_unlock(); | |
351 | return ret; | |
352 | } | |
353 | ||
46f65ec1 HD |
354 | /* |
355 | * Lockless lookup of swap entry in radix tree, avoiding refcount on pages. | |
356 | */ | |
357 | static pgoff_t shmem_find_swap(struct address_space *mapping, void *radswap) | |
358 | { | |
359 | void **slots[PAGEVEC_SIZE]; | |
360 | pgoff_t indices[PAGEVEC_SIZE]; | |
361 | unsigned int nr_found; | |
362 | ||
363 | restart: | |
364 | nr_found = 1; | |
365 | indices[0] = -1; | |
366 | while (nr_found) { | |
367 | pgoff_t index = indices[nr_found - 1] + 1; | |
368 | unsigned int i; | |
369 | ||
370 | rcu_read_lock(); | |
371 | nr_found = radix_tree_gang_lookup_slot(&mapping->page_tree, | |
372 | slots, indices, index, PAGEVEC_SIZE); | |
373 | for (i = 0; i < nr_found; i++) { | |
374 | void *item = radix_tree_deref_slot(slots[i]); | |
375 | if (radix_tree_deref_retry(item)) { | |
376 | rcu_read_unlock(); | |
377 | goto restart; | |
378 | } | |
379 | if (item == radswap) { | |
380 | rcu_read_unlock(); | |
381 | return indices[i]; | |
382 | } | |
383 | } | |
384 | rcu_read_unlock(); | |
385 | cond_resched(); | |
386 | } | |
387 | return -1; | |
388 | } | |
389 | ||
7a5d0fbb HD |
390 | /* |
391 | * Remove swap entry from radix tree, free the swap and its page cache. | |
392 | */ | |
393 | static int shmem_free_swap(struct address_space *mapping, | |
394 | pgoff_t index, void *radswap) | |
395 | { | |
396 | int error; | |
397 | ||
398 | spin_lock_irq(&mapping->tree_lock); | |
399 | error = shmem_radix_tree_replace(mapping, index, radswap, NULL); | |
400 | spin_unlock_irq(&mapping->tree_lock); | |
401 | if (!error) | |
402 | free_swap_and_cache(radix_to_swp_entry(radswap)); | |
403 | return error; | |
404 | } | |
405 | ||
406 | /* | |
407 | * Pagevec may contain swap entries, so shuffle up pages before releasing. | |
408 | */ | |
409 | static void shmem_pagevec_release(struct pagevec *pvec) | |
410 | { | |
411 | int i, j; | |
412 | ||
413 | for (i = 0, j = 0; i < pagevec_count(pvec); i++) { | |
414 | struct page *page = pvec->pages[i]; | |
415 | if (!radix_tree_exceptional_entry(page)) | |
416 | pvec->pages[j++] = page; | |
417 | } | |
418 | pvec->nr = j; | |
419 | pagevec_release(pvec); | |
420 | } | |
421 | ||
422 | /* | |
423 | * Remove range of pages and swap entries from radix tree, and free them. | |
424 | */ | |
285b2c4f | 425 | void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) |
1da177e4 | 426 | { |
285b2c4f | 427 | struct address_space *mapping = inode->i_mapping; |
1da177e4 | 428 | struct shmem_inode_info *info = SHMEM_I(inode); |
285b2c4f | 429 | pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
bda97eab | 430 | unsigned partial = lstart & (PAGE_CACHE_SIZE - 1); |
285b2c4f | 431 | pgoff_t end = (lend >> PAGE_CACHE_SHIFT); |
bda97eab | 432 | struct pagevec pvec; |
7a5d0fbb HD |
433 | pgoff_t indices[PAGEVEC_SIZE]; |
434 | long nr_swaps_freed = 0; | |
285b2c4f | 435 | pgoff_t index; |
bda97eab HD |
436 | int i; |
437 | ||
438 | BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1)); | |
439 | ||
440 | pagevec_init(&pvec, 0); | |
441 | index = start; | |
7a5d0fbb HD |
442 | while (index <= end) { |
443 | pvec.nr = shmem_find_get_pages_and_swap(mapping, index, | |
444 | min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1, | |
445 | pvec.pages, indices); | |
446 | if (!pvec.nr) | |
447 | break; | |
bda97eab HD |
448 | mem_cgroup_uncharge_start(); |
449 | for (i = 0; i < pagevec_count(&pvec); i++) { | |
450 | struct page *page = pvec.pages[i]; | |
451 | ||
7a5d0fbb | 452 | index = indices[i]; |
bda97eab HD |
453 | if (index > end) |
454 | break; | |
455 | ||
7a5d0fbb HD |
456 | if (radix_tree_exceptional_entry(page)) { |
457 | nr_swaps_freed += !shmem_free_swap(mapping, | |
458 | index, page); | |
bda97eab | 459 | continue; |
7a5d0fbb HD |
460 | } |
461 | ||
462 | if (!trylock_page(page)) | |
bda97eab | 463 | continue; |
7a5d0fbb HD |
464 | if (page->mapping == mapping) { |
465 | VM_BUG_ON(PageWriteback(page)); | |
466 | truncate_inode_page(mapping, page); | |
bda97eab | 467 | } |
bda97eab HD |
468 | unlock_page(page); |
469 | } | |
7a5d0fbb | 470 | shmem_pagevec_release(&pvec); |
bda97eab HD |
471 | mem_cgroup_uncharge_end(); |
472 | cond_resched(); | |
473 | index++; | |
474 | } | |
1da177e4 | 475 | |
bda97eab HD |
476 | if (partial) { |
477 | struct page *page = NULL; | |
478 | shmem_getpage(inode, start - 1, &page, SGP_READ, NULL); | |
479 | if (page) { | |
480 | zero_user_segment(page, partial, PAGE_CACHE_SIZE); | |
481 | set_page_dirty(page); | |
482 | unlock_page(page); | |
483 | page_cache_release(page); | |
484 | } | |
485 | } | |
486 | ||
487 | index = start; | |
488 | for ( ; ; ) { | |
489 | cond_resched(); | |
7a5d0fbb HD |
490 | pvec.nr = shmem_find_get_pages_and_swap(mapping, index, |
491 | min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1, | |
492 | pvec.pages, indices); | |
493 | if (!pvec.nr) { | |
bda97eab HD |
494 | if (index == start) |
495 | break; | |
496 | index = start; | |
497 | continue; | |
498 | } | |
7a5d0fbb HD |
499 | if (index == start && indices[0] > end) { |
500 | shmem_pagevec_release(&pvec); | |
bda97eab HD |
501 | break; |
502 | } | |
503 | mem_cgroup_uncharge_start(); | |
504 | for (i = 0; i < pagevec_count(&pvec); i++) { | |
505 | struct page *page = pvec.pages[i]; | |
506 | ||
7a5d0fbb | 507 | index = indices[i]; |
bda97eab HD |
508 | if (index > end) |
509 | break; | |
510 | ||
7a5d0fbb HD |
511 | if (radix_tree_exceptional_entry(page)) { |
512 | nr_swaps_freed += !shmem_free_swap(mapping, | |
513 | index, page); | |
514 | continue; | |
515 | } | |
516 | ||
bda97eab | 517 | lock_page(page); |
7a5d0fbb HD |
518 | if (page->mapping == mapping) { |
519 | VM_BUG_ON(PageWriteback(page)); | |
520 | truncate_inode_page(mapping, page); | |
521 | } | |
bda97eab HD |
522 | unlock_page(page); |
523 | } | |
7a5d0fbb | 524 | shmem_pagevec_release(&pvec); |
bda97eab HD |
525 | mem_cgroup_uncharge_end(); |
526 | index++; | |
527 | } | |
94c1e62d | 528 | |
1da177e4 | 529 | spin_lock(&info->lock); |
7a5d0fbb | 530 | info->swapped -= nr_swaps_freed; |
1da177e4 LT |
531 | shmem_recalc_inode(inode); |
532 | spin_unlock(&info->lock); | |
533 | ||
285b2c4f | 534 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
1da177e4 | 535 | } |
94c1e62d | 536 | EXPORT_SYMBOL_GPL(shmem_truncate_range); |
1da177e4 | 537 | |
94c1e62d | 538 | static int shmem_setattr(struct dentry *dentry, struct iattr *attr) |
1da177e4 LT |
539 | { |
540 | struct inode *inode = dentry->d_inode; | |
1da177e4 LT |
541 | int error; |
542 | ||
db78b877 CH |
543 | error = inode_change_ok(inode, attr); |
544 | if (error) | |
545 | return error; | |
546 | ||
94c1e62d HD |
547 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
548 | loff_t oldsize = inode->i_size; | |
549 | loff_t newsize = attr->ia_size; | |
3889e6e7 | 550 | |
94c1e62d HD |
551 | if (newsize != oldsize) { |
552 | i_size_write(inode, newsize); | |
553 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; | |
554 | } | |
555 | if (newsize < oldsize) { | |
556 | loff_t holebegin = round_up(newsize, PAGE_SIZE); | |
557 | unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); | |
558 | shmem_truncate_range(inode, newsize, (loff_t)-1); | |
559 | /* unmap again to remove racily COWed private pages */ | |
560 | unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); | |
561 | } | |
1da177e4 LT |
562 | } |
563 | ||
db78b877 | 564 | setattr_copy(inode, attr); |
39f0247d | 565 | #ifdef CONFIG_TMPFS_POSIX_ACL |
db78b877 | 566 | if (attr->ia_valid & ATTR_MODE) |
1c7c474c | 567 | error = generic_acl_chmod(inode); |
39f0247d | 568 | #endif |
1da177e4 LT |
569 | return error; |
570 | } | |
571 | ||
1f895f75 | 572 | static void shmem_evict_inode(struct inode *inode) |
1da177e4 | 573 | { |
1da177e4 | 574 | struct shmem_inode_info *info = SHMEM_I(inode); |
b09e0fa4 | 575 | struct shmem_xattr *xattr, *nxattr; |
1da177e4 | 576 | |
3889e6e7 | 577 | if (inode->i_mapping->a_ops == &shmem_aops) { |
1da177e4 LT |
578 | shmem_unacct_size(info->flags, inode->i_size); |
579 | inode->i_size = 0; | |
3889e6e7 | 580 | shmem_truncate_range(inode, 0, (loff_t)-1); |
1da177e4 | 581 | if (!list_empty(&info->swaplist)) { |
cb5f7b9a | 582 | mutex_lock(&shmem_swaplist_mutex); |
1da177e4 | 583 | list_del_init(&info->swaplist); |
cb5f7b9a | 584 | mutex_unlock(&shmem_swaplist_mutex); |
1da177e4 LT |
585 | } |
586 | } | |
b09e0fa4 EP |
587 | |
588 | list_for_each_entry_safe(xattr, nxattr, &info->xattr_list, list) { | |
589 | kfree(xattr->name); | |
590 | kfree(xattr); | |
591 | } | |
0edd73b3 | 592 | BUG_ON(inode->i_blocks); |
5b04c689 | 593 | shmem_free_inode(inode->i_sb); |
1f895f75 | 594 | end_writeback(inode); |
1da177e4 LT |
595 | } |
596 | ||
46f65ec1 HD |
597 | /* |
598 | * If swap found in inode, free it and move page from swapcache to filecache. | |
599 | */ | |
41ffe5d5 HD |
600 | static int shmem_unuse_inode(struct shmem_inode_info *info, |
601 | swp_entry_t swap, struct page *page) | |
1da177e4 | 602 | { |
285b2c4f | 603 | struct address_space *mapping = info->vfs_inode.i_mapping; |
46f65ec1 | 604 | void *radswap; |
41ffe5d5 | 605 | pgoff_t index; |
d9fe526a | 606 | int error; |
1da177e4 | 607 | |
46f65ec1 HD |
608 | radswap = swp_to_radix_entry(swap); |
609 | index = shmem_find_swap(mapping, radswap); | |
610 | if (index == -1) | |
285b2c4f | 611 | return 0; |
2e0e26c7 | 612 | |
1b1b32f2 HD |
613 | /* |
614 | * Move _head_ to start search for next from here. | |
1f895f75 | 615 | * But be careful: shmem_evict_inode checks list_empty without taking |
1b1b32f2 | 616 | * mutex, and there's an instant in list_move_tail when info->swaplist |
285b2c4f | 617 | * would appear empty, if it were the only one on shmem_swaplist. |
1b1b32f2 HD |
618 | */ |
619 | if (shmem_swaplist.next != &info->swaplist) | |
620 | list_move_tail(&shmem_swaplist, &info->swaplist); | |
2e0e26c7 | 621 | |
d13d1443 | 622 | /* |
778dd893 HD |
623 | * We rely on shmem_swaplist_mutex, not only to protect the swaplist, |
624 | * but also to hold up shmem_evict_inode(): so inode cannot be freed | |
625 | * beneath us (pagelock doesn't help until the page is in pagecache). | |
d13d1443 | 626 | */ |
46f65ec1 HD |
627 | error = shmem_add_to_page_cache(page, mapping, index, |
628 | GFP_NOWAIT, radswap); | |
778dd893 | 629 | /* which does mem_cgroup_uncharge_cache_page on error */ |
69029cd5 | 630 | |
48f170fb | 631 | if (error != -ENOMEM) { |
46f65ec1 HD |
632 | /* |
633 | * Truncation and eviction use free_swap_and_cache(), which | |
634 | * only does trylock page: if we raced, best clean up here. | |
635 | */ | |
73b1262f HD |
636 | delete_from_swap_cache(page); |
637 | set_page_dirty(page); | |
46f65ec1 HD |
638 | if (!error) { |
639 | spin_lock(&info->lock); | |
640 | info->swapped--; | |
641 | spin_unlock(&info->lock); | |
642 | swap_free(swap); | |
643 | } | |
2e0e26c7 | 644 | error = 1; /* not an error, but entry was found */ |
1da177e4 | 645 | } |
2e0e26c7 | 646 | return error; |
1da177e4 LT |
647 | } |
648 | ||
649 | /* | |
46f65ec1 | 650 | * Search through swapped inodes to find and replace swap by page. |
1da177e4 | 651 | */ |
41ffe5d5 | 652 | int shmem_unuse(swp_entry_t swap, struct page *page) |
1da177e4 | 653 | { |
41ffe5d5 | 654 | struct list_head *this, *next; |
1da177e4 LT |
655 | struct shmem_inode_info *info; |
656 | int found = 0; | |
778dd893 HD |
657 | int error; |
658 | ||
659 | /* | |
660 | * Charge page using GFP_KERNEL while we can wait, before taking | |
661 | * the shmem_swaplist_mutex which might hold up shmem_writepage(). | |
662 | * Charged back to the user (not to caller) when swap account is used. | |
46f65ec1 | 663 | * shmem_add_to_page_cache() will be called with GFP_NOWAIT. |
778dd893 HD |
664 | */ |
665 | error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL); | |
666 | if (error) | |
667 | goto out; | |
46f65ec1 | 668 | /* No radix_tree_preload: swap entry keeps a place for page in tree */ |
1da177e4 | 669 | |
cb5f7b9a | 670 | mutex_lock(&shmem_swaplist_mutex); |
41ffe5d5 HD |
671 | list_for_each_safe(this, next, &shmem_swaplist) { |
672 | info = list_entry(this, struct shmem_inode_info, swaplist); | |
285b2c4f HD |
673 | if (!info->swapped) { |
674 | spin_lock(&info->lock); | |
675 | if (!info->swapped) | |
676 | list_del_init(&info->swaplist); | |
677 | spin_unlock(&info->lock); | |
678 | } | |
679 | if (info->swapped) | |
41ffe5d5 | 680 | found = shmem_unuse_inode(info, swap, page); |
cb5f7b9a | 681 | cond_resched(); |
2e0e26c7 | 682 | if (found) |
778dd893 | 683 | break; |
1da177e4 | 684 | } |
cb5f7b9a | 685 | mutex_unlock(&shmem_swaplist_mutex); |
778dd893 | 686 | |
778dd893 HD |
687 | if (!found) |
688 | mem_cgroup_uncharge_cache_page(page); | |
689 | if (found < 0) | |
690 | error = found; | |
691 | out: | |
aaa46865 HD |
692 | unlock_page(page); |
693 | page_cache_release(page); | |
778dd893 | 694 | return error; |
1da177e4 LT |
695 | } |
696 | ||
697 | /* | |
698 | * Move the page from the page cache to the swap cache. | |
699 | */ | |
700 | static int shmem_writepage(struct page *page, struct writeback_control *wbc) | |
701 | { | |
702 | struct shmem_inode_info *info; | |
285b2c4f | 703 | swp_entry_t swap, oswap; |
1da177e4 | 704 | struct address_space *mapping; |
41ffe5d5 | 705 | pgoff_t index; |
1da177e4 LT |
706 | struct inode *inode; |
707 | ||
708 | BUG_ON(!PageLocked(page)); | |
1da177e4 LT |
709 | mapping = page->mapping; |
710 | index = page->index; | |
711 | inode = mapping->host; | |
712 | info = SHMEM_I(inode); | |
713 | if (info->flags & VM_LOCKED) | |
714 | goto redirty; | |
d9fe526a | 715 | if (!total_swap_pages) |
1da177e4 LT |
716 | goto redirty; |
717 | ||
d9fe526a HD |
718 | /* |
719 | * shmem_backing_dev_info's capabilities prevent regular writeback or | |
720 | * sync from ever calling shmem_writepage; but a stacking filesystem | |
48f170fb | 721 | * might use ->writepage of its underlying filesystem, in which case |
d9fe526a | 722 | * tmpfs should write out to swap only in response to memory pressure, |
48f170fb | 723 | * and not for the writeback threads or sync. |
d9fe526a | 724 | */ |
48f170fb HD |
725 | if (!wbc->for_reclaim) { |
726 | WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ | |
727 | goto redirty; | |
728 | } | |
285b2c4f HD |
729 | |
730 | /* | |
7a5d0fbb HD |
731 | * Disable even the toy swapping implementation, while we convert |
732 | * functions one by one to having swap entries in the radix tree. | |
285b2c4f | 733 | */ |
7a5d0fbb | 734 | if (index < ULONG_MAX) |
285b2c4f HD |
735 | goto redirty; |
736 | ||
48f170fb HD |
737 | swap = get_swap_page(); |
738 | if (!swap.val) | |
739 | goto redirty; | |
d9fe526a | 740 | |
b1dea800 HD |
741 | /* |
742 | * Add inode to shmem_unuse()'s list of swapped-out inodes, | |
743 | * if it's not already there. Do it now because we cannot take | |
744 | * mutex while holding spinlock, and must do so before the page | |
745 | * is moved to swap cache, when its pagelock no longer protects | |
746 | * the inode from eviction. But don't unlock the mutex until | |
747 | * we've taken the spinlock, because shmem_unuse_inode() will | |
748 | * prune a !swapped inode from the swaplist under both locks. | |
749 | */ | |
48f170fb HD |
750 | mutex_lock(&shmem_swaplist_mutex); |
751 | if (list_empty(&info->swaplist)) | |
752 | list_add_tail(&info->swaplist, &shmem_swaplist); | |
b1dea800 | 753 | |
1da177e4 | 754 | spin_lock(&info->lock); |
48f170fb | 755 | mutex_unlock(&shmem_swaplist_mutex); |
b1dea800 | 756 | |
285b2c4f HD |
757 | oswap = shmem_get_swap(info, index); |
758 | if (oswap.val) { | |
48f170fb | 759 | WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ |
285b2c4f HD |
760 | free_swap_and_cache(oswap); |
761 | shmem_put_swap(info, index, (swp_entry_t){0}); | |
762 | info->swapped--; | |
d9fe526a HD |
763 | } |
764 | shmem_recalc_inode(inode); | |
1da177e4 | 765 | |
48f170fb | 766 | if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { |
4c73b1bc | 767 | delete_from_page_cache(page); |
285b2c4f HD |
768 | shmem_put_swap(info, index, swap); |
769 | info->swapped++; | |
aaa46865 | 770 | swap_shmem_alloc(swap); |
826267cf | 771 | spin_unlock(&info->lock); |
d9fe526a | 772 | BUG_ON(page_mapped(page)); |
9fab5619 | 773 | swap_writepage(page, wbc); |
1da177e4 LT |
774 | return 0; |
775 | } | |
776 | ||
1da177e4 | 777 | spin_unlock(&info->lock); |
cb4b86ba | 778 | swapcache_free(swap, NULL); |
1da177e4 LT |
779 | redirty: |
780 | set_page_dirty(page); | |
d9fe526a HD |
781 | if (wbc->for_reclaim) |
782 | return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ | |
783 | unlock_page(page); | |
784 | return 0; | |
1da177e4 LT |
785 | } |
786 | ||
787 | #ifdef CONFIG_NUMA | |
680d794b | 788 | #ifdef CONFIG_TMPFS |
71fe804b | 789 | static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) |
680d794b | 790 | { |
095f1fc4 | 791 | char buffer[64]; |
680d794b | 792 | |
71fe804b | 793 | if (!mpol || mpol->mode == MPOL_DEFAULT) |
095f1fc4 | 794 | return; /* show nothing */ |
680d794b | 795 | |
71fe804b | 796 | mpol_to_str(buffer, sizeof(buffer), mpol, 1); |
095f1fc4 LS |
797 | |
798 | seq_printf(seq, ",mpol=%s", buffer); | |
680d794b | 799 | } |
71fe804b LS |
800 | |
801 | static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) | |
802 | { | |
803 | struct mempolicy *mpol = NULL; | |
804 | if (sbinfo->mpol) { | |
805 | spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ | |
806 | mpol = sbinfo->mpol; | |
807 | mpol_get(mpol); | |
808 | spin_unlock(&sbinfo->stat_lock); | |
809 | } | |
810 | return mpol; | |
811 | } | |
680d794b | 812 | #endif /* CONFIG_TMPFS */ |
813 | ||
41ffe5d5 HD |
814 | static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, |
815 | struct shmem_inode_info *info, pgoff_t index) | |
1da177e4 | 816 | { |
52cd3b07 | 817 | struct mempolicy mpol, *spol; |
1da177e4 LT |
818 | struct vm_area_struct pvma; |
819 | ||
52cd3b07 | 820 | spol = mpol_cond_copy(&mpol, |
41ffe5d5 | 821 | mpol_shared_policy_lookup(&info->policy, index)); |
52cd3b07 | 822 | |
1da177e4 | 823 | /* Create a pseudo vma that just contains the policy */ |
c4cc6d07 | 824 | pvma.vm_start = 0; |
41ffe5d5 | 825 | pvma.vm_pgoff = index; |
c4cc6d07 | 826 | pvma.vm_ops = NULL; |
52cd3b07 | 827 | pvma.vm_policy = spol; |
41ffe5d5 | 828 | return swapin_readahead(swap, gfp, &pvma, 0); |
1da177e4 LT |
829 | } |
830 | ||
02098fea | 831 | static struct page *shmem_alloc_page(gfp_t gfp, |
41ffe5d5 | 832 | struct shmem_inode_info *info, pgoff_t index) |
1da177e4 LT |
833 | { |
834 | struct vm_area_struct pvma; | |
1da177e4 | 835 | |
c4cc6d07 HD |
836 | /* Create a pseudo vma that just contains the policy */ |
837 | pvma.vm_start = 0; | |
41ffe5d5 | 838 | pvma.vm_pgoff = index; |
c4cc6d07 | 839 | pvma.vm_ops = NULL; |
41ffe5d5 | 840 | pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index); |
52cd3b07 LS |
841 | |
842 | /* | |
843 | * alloc_page_vma() will drop the shared policy reference | |
844 | */ | |
845 | return alloc_page_vma(gfp, &pvma, 0); | |
1da177e4 | 846 | } |
680d794b | 847 | #else /* !CONFIG_NUMA */ |
848 | #ifdef CONFIG_TMPFS | |
41ffe5d5 | 849 | static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) |
680d794b | 850 | { |
851 | } | |
852 | #endif /* CONFIG_TMPFS */ | |
853 | ||
41ffe5d5 HD |
854 | static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, |
855 | struct shmem_inode_info *info, pgoff_t index) | |
1da177e4 | 856 | { |
41ffe5d5 | 857 | return swapin_readahead(swap, gfp, NULL, 0); |
1da177e4 LT |
858 | } |
859 | ||
02098fea | 860 | static inline struct page *shmem_alloc_page(gfp_t gfp, |
41ffe5d5 | 861 | struct shmem_inode_info *info, pgoff_t index) |
1da177e4 | 862 | { |
e84e2e13 | 863 | return alloc_page(gfp); |
1da177e4 | 864 | } |
680d794b | 865 | #endif /* CONFIG_NUMA */ |
1da177e4 | 866 | |
71fe804b LS |
867 | #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS) |
868 | static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) | |
869 | { | |
870 | return NULL; | |
871 | } | |
872 | #endif | |
873 | ||
1da177e4 | 874 | /* |
68da9f05 | 875 | * shmem_getpage_gfp - find page in cache, or get from swap, or allocate |
1da177e4 LT |
876 | * |
877 | * If we allocate a new one we do not mark it dirty. That's up to the | |
878 | * vm. If we swap it in we mark it dirty since we also free the swap | |
879 | * entry since a page cannot live in both the swap and page cache | |
880 | */ | |
41ffe5d5 | 881 | static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, |
68da9f05 | 882 | struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type) |
1da177e4 LT |
883 | { |
884 | struct address_space *mapping = inode->i_mapping; | |
54af6042 | 885 | struct shmem_inode_info *info; |
1da177e4 | 886 | struct shmem_sb_info *sbinfo; |
27ab7006 | 887 | struct page *page; |
1da177e4 LT |
888 | swp_entry_t swap; |
889 | int error; | |
54af6042 | 890 | int once = 0; |
1da177e4 | 891 | |
41ffe5d5 | 892 | if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT)) |
1da177e4 | 893 | return -EFBIG; |
1da177e4 | 894 | repeat: |
54af6042 | 895 | swap.val = 0; |
41ffe5d5 | 896 | page = find_lock_page(mapping, index); |
54af6042 HD |
897 | if (radix_tree_exceptional_entry(page)) { |
898 | swap = radix_to_swp_entry(page); | |
899 | page = NULL; | |
900 | } | |
901 | ||
902 | if (sgp != SGP_WRITE && | |
903 | ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { | |
904 | error = -EINVAL; | |
905 | goto failed; | |
906 | } | |
907 | ||
908 | if (page || (sgp == SGP_READ && !swap.val)) { | |
b409f9fc | 909 | /* |
27ab7006 HD |
910 | * Once we can get the page lock, it must be uptodate: |
911 | * if there were an error in reading back from swap, | |
912 | * the page would not be inserted into the filecache. | |
b409f9fc | 913 | */ |
54af6042 HD |
914 | BUG_ON(page && !PageUptodate(page)); |
915 | *pagep = page; | |
916 | return 0; | |
27ab7006 HD |
917 | } |
918 | ||
919 | /* | |
54af6042 HD |
920 | * Fast cache lookup did not find it: |
921 | * bring it back from swap or allocate. | |
27ab7006 | 922 | */ |
54af6042 HD |
923 | info = SHMEM_I(inode); |
924 | sbinfo = SHMEM_SB(inode->i_sb); | |
1da177e4 | 925 | |
1da177e4 LT |
926 | if (swap.val) { |
927 | /* Look it up and read it in.. */ | |
27ab7006 HD |
928 | page = lookup_swap_cache(swap); |
929 | if (!page) { | |
1da177e4 | 930 | /* here we actually do the io */ |
68da9f05 HD |
931 | if (fault_type) |
932 | *fault_type |= VM_FAULT_MAJOR; | |
41ffe5d5 | 933 | page = shmem_swapin(swap, gfp, info, index); |
27ab7006 | 934 | if (!page) { |
54af6042 HD |
935 | error = -ENOMEM; |
936 | goto failed; | |
1da177e4 | 937 | } |
1da177e4 LT |
938 | } |
939 | ||
940 | /* We have to do this with page locked to prevent races */ | |
54af6042 | 941 | lock_page(page); |
27ab7006 | 942 | if (!PageUptodate(page)) { |
1da177e4 | 943 | error = -EIO; |
54af6042 | 944 | goto failed; |
1da177e4 | 945 | } |
54af6042 HD |
946 | wait_on_page_writeback(page); |
947 | ||
948 | /* Someone may have already done it for us */ | |
949 | if (page->mapping) { | |
950 | if (page->mapping == mapping && | |
951 | page->index == index) | |
952 | goto done; | |
953 | error = -EEXIST; | |
954 | goto failed; | |
1da177e4 | 955 | } |
27ab7006 | 956 | |
54af6042 HD |
957 | error = shmem_add_to_page_cache(page, mapping, index, |
958 | gfp, swp_to_radix_entry(swap)); | |
959 | if (error) | |
960 | goto failed; | |
961 | ||
962 | spin_lock(&info->lock); | |
285b2c4f | 963 | info->swapped--; |
54af6042 | 964 | shmem_recalc_inode(inode); |
27ab7006 | 965 | spin_unlock(&info->lock); |
54af6042 HD |
966 | |
967 | delete_from_swap_cache(page); | |
27ab7006 HD |
968 | set_page_dirty(page); |
969 | swap_free(swap); | |
970 | ||
54af6042 HD |
971 | } else { |
972 | if (shmem_acct_block(info->flags)) { | |
973 | error = -ENOSPC; | |
974 | goto failed; | |
1da177e4 | 975 | } |
0edd73b3 | 976 | if (sbinfo->max_blocks) { |
fc5da22a | 977 | if (percpu_counter_compare(&sbinfo->used_blocks, |
54af6042 HD |
978 | sbinfo->max_blocks) >= 0) { |
979 | error = -ENOSPC; | |
980 | goto unacct; | |
981 | } | |
7e496299 | 982 | percpu_counter_inc(&sbinfo->used_blocks); |
54af6042 | 983 | } |
1da177e4 | 984 | |
54af6042 HD |
985 | page = shmem_alloc_page(gfp, info, index); |
986 | if (!page) { | |
987 | error = -ENOMEM; | |
988 | goto decused; | |
1da177e4 LT |
989 | } |
990 | ||
54af6042 HD |
991 | SetPageSwapBacked(page); |
992 | __set_page_locked(page); | |
993 | error = shmem_add_to_page_cache(page, mapping, index, | |
994 | gfp, NULL); | |
995 | if (error) | |
996 | goto decused; | |
997 | lru_cache_add_anon(page); | |
998 | ||
999 | spin_lock(&info->lock); | |
1da177e4 | 1000 | info->alloced++; |
54af6042 HD |
1001 | inode->i_blocks += BLOCKS_PER_PAGE; |
1002 | shmem_recalc_inode(inode); | |
1da177e4 | 1003 | spin_unlock(&info->lock); |
54af6042 | 1004 | |
27ab7006 HD |
1005 | clear_highpage(page); |
1006 | flush_dcache_page(page); | |
1007 | SetPageUptodate(page); | |
a0ee5ec5 | 1008 | if (sgp == SGP_DIRTY) |
27ab7006 | 1009 | set_page_dirty(page); |
1da177e4 LT |
1010 | } |
1011 | done: | |
54af6042 HD |
1012 | /* Perhaps the file has been truncated since we checked */ |
1013 | if (sgp != SGP_WRITE && | |
1014 | ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { | |
1015 | error = -EINVAL; | |
1016 | goto trunc; | |
e83c32e8 | 1017 | } |
54af6042 HD |
1018 | *pagep = page; |
1019 | return 0; | |
1da177e4 | 1020 | |
59a16ead | 1021 | /* |
54af6042 | 1022 | * Error recovery. |
59a16ead | 1023 | */ |
54af6042 HD |
1024 | trunc: |
1025 | ClearPageDirty(page); | |
1026 | delete_from_page_cache(page); | |
1027 | spin_lock(&info->lock); | |
1028 | info->alloced--; | |
1029 | inode->i_blocks -= BLOCKS_PER_PAGE; | |
59a16ead | 1030 | spin_unlock(&info->lock); |
54af6042 HD |
1031 | decused: |
1032 | if (sbinfo->max_blocks) | |
1033 | percpu_counter_add(&sbinfo->used_blocks, -1); | |
1034 | unacct: | |
1035 | shmem_unacct_blocks(info->flags, 1); | |
1036 | failed: | |
1037 | if (swap.val && error != -EINVAL) { | |
1038 | struct page *test = find_get_page(mapping, index); | |
1039 | if (test && !radix_tree_exceptional_entry(test)) | |
1040 | page_cache_release(test); | |
1041 | /* Have another try if the entry has changed */ | |
1042 | if (test != swp_to_radix_entry(swap)) | |
1043 | error = -EEXIST; | |
1044 | } | |
27ab7006 | 1045 | if (page) { |
54af6042 | 1046 | unlock_page(page); |
27ab7006 | 1047 | page_cache_release(page); |
54af6042 HD |
1048 | } |
1049 | if (error == -ENOSPC && !once++) { | |
1050 | info = SHMEM_I(inode); | |
1051 | spin_lock(&info->lock); | |
1052 | shmem_recalc_inode(inode); | |
1053 | spin_unlock(&info->lock); | |
27ab7006 | 1054 | goto repeat; |
ff36b801 | 1055 | } |
54af6042 HD |
1056 | if (error == -EEXIST) |
1057 | goto repeat; | |
1058 | return error; | |
1da177e4 LT |
1059 | } |
1060 | ||
d0217ac0 | 1061 | static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
1da177e4 | 1062 | { |
d3ac7f89 | 1063 | struct inode *inode = vma->vm_file->f_path.dentry->d_inode; |
1da177e4 | 1064 | int error; |
68da9f05 | 1065 | int ret = VM_FAULT_LOCKED; |
1da177e4 | 1066 | |
27d54b39 | 1067 | error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret); |
d0217ac0 NP |
1068 | if (error) |
1069 | return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS); | |
68da9f05 | 1070 | |
456f998e YH |
1071 | if (ret & VM_FAULT_MAJOR) { |
1072 | count_vm_event(PGMAJFAULT); | |
1073 | mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); | |
1074 | } | |
68da9f05 | 1075 | return ret; |
1da177e4 LT |
1076 | } |
1077 | ||
1da177e4 | 1078 | #ifdef CONFIG_NUMA |
41ffe5d5 | 1079 | static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) |
1da177e4 | 1080 | { |
41ffe5d5 HD |
1081 | struct inode *inode = vma->vm_file->f_path.dentry->d_inode; |
1082 | return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); | |
1da177e4 LT |
1083 | } |
1084 | ||
d8dc74f2 AB |
1085 | static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, |
1086 | unsigned long addr) | |
1da177e4 | 1087 | { |
41ffe5d5 HD |
1088 | struct inode *inode = vma->vm_file->f_path.dentry->d_inode; |
1089 | pgoff_t index; | |
1da177e4 | 1090 | |
41ffe5d5 HD |
1091 | index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
1092 | return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); | |
1da177e4 LT |
1093 | } |
1094 | #endif | |
1095 | ||
1096 | int shmem_lock(struct file *file, int lock, struct user_struct *user) | |
1097 | { | |
d3ac7f89 | 1098 | struct inode *inode = file->f_path.dentry->d_inode; |
1da177e4 LT |
1099 | struct shmem_inode_info *info = SHMEM_I(inode); |
1100 | int retval = -ENOMEM; | |
1101 | ||
1102 | spin_lock(&info->lock); | |
1103 | if (lock && !(info->flags & VM_LOCKED)) { | |
1104 | if (!user_shm_lock(inode->i_size, user)) | |
1105 | goto out_nomem; | |
1106 | info->flags |= VM_LOCKED; | |
89e004ea | 1107 | mapping_set_unevictable(file->f_mapping); |
1da177e4 LT |
1108 | } |
1109 | if (!lock && (info->flags & VM_LOCKED) && user) { | |
1110 | user_shm_unlock(inode->i_size, user); | |
1111 | info->flags &= ~VM_LOCKED; | |
89e004ea LS |
1112 | mapping_clear_unevictable(file->f_mapping); |
1113 | scan_mapping_unevictable_pages(file->f_mapping); | |
1da177e4 LT |
1114 | } |
1115 | retval = 0; | |
89e004ea | 1116 | |
1da177e4 LT |
1117 | out_nomem: |
1118 | spin_unlock(&info->lock); | |
1119 | return retval; | |
1120 | } | |
1121 | ||
9b83a6a8 | 1122 | static int shmem_mmap(struct file *file, struct vm_area_struct *vma) |
1da177e4 LT |
1123 | { |
1124 | file_accessed(file); | |
1125 | vma->vm_ops = &shmem_vm_ops; | |
d0217ac0 | 1126 | vma->vm_flags |= VM_CAN_NONLINEAR; |
1da177e4 LT |
1127 | return 0; |
1128 | } | |
1129 | ||
454abafe DM |
1130 | static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, |
1131 | int mode, dev_t dev, unsigned long flags) | |
1da177e4 LT |
1132 | { |
1133 | struct inode *inode; | |
1134 | struct shmem_inode_info *info; | |
1135 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); | |
1136 | ||
5b04c689 PE |
1137 | if (shmem_reserve_inode(sb)) |
1138 | return NULL; | |
1da177e4 LT |
1139 | |
1140 | inode = new_inode(sb); | |
1141 | if (inode) { | |
85fe4025 | 1142 | inode->i_ino = get_next_ino(); |
454abafe | 1143 | inode_init_owner(inode, dir, mode); |
1da177e4 | 1144 | inode->i_blocks = 0; |
1da177e4 LT |
1145 | inode->i_mapping->backing_dev_info = &shmem_backing_dev_info; |
1146 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
91828a40 | 1147 | inode->i_generation = get_seconds(); |
1da177e4 LT |
1148 | info = SHMEM_I(inode); |
1149 | memset(info, 0, (char *)inode - (char *)info); | |
1150 | spin_lock_init(&info->lock); | |
0b0a0806 | 1151 | info->flags = flags & VM_NORESERVE; |
1da177e4 | 1152 | INIT_LIST_HEAD(&info->swaplist); |
b09e0fa4 | 1153 | INIT_LIST_HEAD(&info->xattr_list); |
72c04902 | 1154 | cache_no_acl(inode); |
1da177e4 LT |
1155 | |
1156 | switch (mode & S_IFMT) { | |
1157 | default: | |
39f0247d | 1158 | inode->i_op = &shmem_special_inode_operations; |
1da177e4 LT |
1159 | init_special_inode(inode, mode, dev); |
1160 | break; | |
1161 | case S_IFREG: | |
14fcc23f | 1162 | inode->i_mapping->a_ops = &shmem_aops; |
1da177e4 LT |
1163 | inode->i_op = &shmem_inode_operations; |
1164 | inode->i_fop = &shmem_file_operations; | |
71fe804b LS |
1165 | mpol_shared_policy_init(&info->policy, |
1166 | shmem_get_sbmpol(sbinfo)); | |
1da177e4 LT |
1167 | break; |
1168 | case S_IFDIR: | |
d8c76e6f | 1169 | inc_nlink(inode); |
1da177e4 LT |
1170 | /* Some things misbehave if size == 0 on a directory */ |
1171 | inode->i_size = 2 * BOGO_DIRENT_SIZE; | |
1172 | inode->i_op = &shmem_dir_inode_operations; | |
1173 | inode->i_fop = &simple_dir_operations; | |
1174 | break; | |
1175 | case S_IFLNK: | |
1176 | /* | |
1177 | * Must not load anything in the rbtree, | |
1178 | * mpol_free_shared_policy will not be called. | |
1179 | */ | |
71fe804b | 1180 | mpol_shared_policy_init(&info->policy, NULL); |
1da177e4 LT |
1181 | break; |
1182 | } | |
5b04c689 PE |
1183 | } else |
1184 | shmem_free_inode(sb); | |
1da177e4 LT |
1185 | return inode; |
1186 | } | |
1187 | ||
1188 | #ifdef CONFIG_TMPFS | |
92e1d5be AV |
1189 | static const struct inode_operations shmem_symlink_inode_operations; |
1190 | static const struct inode_operations shmem_symlink_inline_operations; | |
1da177e4 | 1191 | |
1da177e4 | 1192 | static int |
800d15a5 NP |
1193 | shmem_write_begin(struct file *file, struct address_space *mapping, |
1194 | loff_t pos, unsigned len, unsigned flags, | |
1195 | struct page **pagep, void **fsdata) | |
1da177e4 | 1196 | { |
800d15a5 NP |
1197 | struct inode *inode = mapping->host; |
1198 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; | |
800d15a5 NP |
1199 | return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL); |
1200 | } | |
1201 | ||
1202 | static int | |
1203 | shmem_write_end(struct file *file, struct address_space *mapping, | |
1204 | loff_t pos, unsigned len, unsigned copied, | |
1205 | struct page *page, void *fsdata) | |
1206 | { | |
1207 | struct inode *inode = mapping->host; | |
1208 | ||
d3602444 HD |
1209 | if (pos + copied > inode->i_size) |
1210 | i_size_write(inode, pos + copied); | |
1211 | ||
800d15a5 | 1212 | set_page_dirty(page); |
6746aff7 | 1213 | unlock_page(page); |
800d15a5 NP |
1214 | page_cache_release(page); |
1215 | ||
800d15a5 | 1216 | return copied; |
1da177e4 LT |
1217 | } |
1218 | ||
1da177e4 LT |
1219 | static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor) |
1220 | { | |
d3ac7f89 | 1221 | struct inode *inode = filp->f_path.dentry->d_inode; |
1da177e4 | 1222 | struct address_space *mapping = inode->i_mapping; |
41ffe5d5 HD |
1223 | pgoff_t index; |
1224 | unsigned long offset; | |
a0ee5ec5 HD |
1225 | enum sgp_type sgp = SGP_READ; |
1226 | ||
1227 | /* | |
1228 | * Might this read be for a stacking filesystem? Then when reading | |
1229 | * holes of a sparse file, we actually need to allocate those pages, | |
1230 | * and even mark them dirty, so it cannot exceed the max_blocks limit. | |
1231 | */ | |
1232 | if (segment_eq(get_fs(), KERNEL_DS)) | |
1233 | sgp = SGP_DIRTY; | |
1da177e4 LT |
1234 | |
1235 | index = *ppos >> PAGE_CACHE_SHIFT; | |
1236 | offset = *ppos & ~PAGE_CACHE_MASK; | |
1237 | ||
1238 | for (;;) { | |
1239 | struct page *page = NULL; | |
41ffe5d5 HD |
1240 | pgoff_t end_index; |
1241 | unsigned long nr, ret; | |
1da177e4 LT |
1242 | loff_t i_size = i_size_read(inode); |
1243 | ||
1244 | end_index = i_size >> PAGE_CACHE_SHIFT; | |
1245 | if (index > end_index) | |
1246 | break; | |
1247 | if (index == end_index) { | |
1248 | nr = i_size & ~PAGE_CACHE_MASK; | |
1249 | if (nr <= offset) | |
1250 | break; | |
1251 | } | |
1252 | ||
a0ee5ec5 | 1253 | desc->error = shmem_getpage(inode, index, &page, sgp, NULL); |
1da177e4 LT |
1254 | if (desc->error) { |
1255 | if (desc->error == -EINVAL) | |
1256 | desc->error = 0; | |
1257 | break; | |
1258 | } | |
d3602444 HD |
1259 | if (page) |
1260 | unlock_page(page); | |
1da177e4 LT |
1261 | |
1262 | /* | |
1263 | * We must evaluate after, since reads (unlike writes) | |
1b1dcc1b | 1264 | * are called without i_mutex protection against truncate |
1da177e4 LT |
1265 | */ |
1266 | nr = PAGE_CACHE_SIZE; | |
1267 | i_size = i_size_read(inode); | |
1268 | end_index = i_size >> PAGE_CACHE_SHIFT; | |
1269 | if (index == end_index) { | |
1270 | nr = i_size & ~PAGE_CACHE_MASK; | |
1271 | if (nr <= offset) { | |
1272 | if (page) | |
1273 | page_cache_release(page); | |
1274 | break; | |
1275 | } | |
1276 | } | |
1277 | nr -= offset; | |
1278 | ||
1279 | if (page) { | |
1280 | /* | |
1281 | * If users can be writing to this page using arbitrary | |
1282 | * virtual addresses, take care about potential aliasing | |
1283 | * before reading the page on the kernel side. | |
1284 | */ | |
1285 | if (mapping_writably_mapped(mapping)) | |
1286 | flush_dcache_page(page); | |
1287 | /* | |
1288 | * Mark the page accessed if we read the beginning. | |
1289 | */ | |
1290 | if (!offset) | |
1291 | mark_page_accessed(page); | |
b5810039 | 1292 | } else { |
1da177e4 | 1293 | page = ZERO_PAGE(0); |
b5810039 NP |
1294 | page_cache_get(page); |
1295 | } | |
1da177e4 LT |
1296 | |
1297 | /* | |
1298 | * Ok, we have the page, and it's up-to-date, so | |
1299 | * now we can copy it to user space... | |
1300 | * | |
1301 | * The actor routine returns how many bytes were actually used.. | |
1302 | * NOTE! This may not be the same as how much of a user buffer | |
1303 | * we filled up (we may be padding etc), so we can only update | |
1304 | * "pos" here (the actor routine has to update the user buffer | |
1305 | * pointers and the remaining count). | |
1306 | */ | |
1307 | ret = actor(desc, page, offset, nr); | |
1308 | offset += ret; | |
1309 | index += offset >> PAGE_CACHE_SHIFT; | |
1310 | offset &= ~PAGE_CACHE_MASK; | |
1311 | ||
1312 | page_cache_release(page); | |
1313 | if (ret != nr || !desc->count) | |
1314 | break; | |
1315 | ||
1316 | cond_resched(); | |
1317 | } | |
1318 | ||
1319 | *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; | |
1320 | file_accessed(filp); | |
1321 | } | |
1322 | ||
bcd78e49 HD |
1323 | static ssize_t shmem_file_aio_read(struct kiocb *iocb, |
1324 | const struct iovec *iov, unsigned long nr_segs, loff_t pos) | |
1325 | { | |
1326 | struct file *filp = iocb->ki_filp; | |
1327 | ssize_t retval; | |
1328 | unsigned long seg; | |
1329 | size_t count; | |
1330 | loff_t *ppos = &iocb->ki_pos; | |
1331 | ||
1332 | retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE); | |
1333 | if (retval) | |
1334 | return retval; | |
1335 | ||
1336 | for (seg = 0; seg < nr_segs; seg++) { | |
1337 | read_descriptor_t desc; | |
1338 | ||
1339 | desc.written = 0; | |
1340 | desc.arg.buf = iov[seg].iov_base; | |
1341 | desc.count = iov[seg].iov_len; | |
1342 | if (desc.count == 0) | |
1343 | continue; | |
1344 | desc.error = 0; | |
1345 | do_shmem_file_read(filp, ppos, &desc, file_read_actor); | |
1346 | retval += desc.written; | |
1347 | if (desc.error) { | |
1348 | retval = retval ?: desc.error; | |
1349 | break; | |
1350 | } | |
1351 | if (desc.count > 0) | |
1352 | break; | |
1353 | } | |
1354 | return retval; | |
1da177e4 LT |
1355 | } |
1356 | ||
708e3508 HD |
1357 | static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, |
1358 | struct pipe_inode_info *pipe, size_t len, | |
1359 | unsigned int flags) | |
1360 | { | |
1361 | struct address_space *mapping = in->f_mapping; | |
71f0e07a | 1362 | struct inode *inode = mapping->host; |
708e3508 HD |
1363 | unsigned int loff, nr_pages, req_pages; |
1364 | struct page *pages[PIPE_DEF_BUFFERS]; | |
1365 | struct partial_page partial[PIPE_DEF_BUFFERS]; | |
1366 | struct page *page; | |
1367 | pgoff_t index, end_index; | |
1368 | loff_t isize, left; | |
1369 | int error, page_nr; | |
1370 | struct splice_pipe_desc spd = { | |
1371 | .pages = pages, | |
1372 | .partial = partial, | |
1373 | .flags = flags, | |
1374 | .ops = &page_cache_pipe_buf_ops, | |
1375 | .spd_release = spd_release_page, | |
1376 | }; | |
1377 | ||
71f0e07a | 1378 | isize = i_size_read(inode); |
708e3508 HD |
1379 | if (unlikely(*ppos >= isize)) |
1380 | return 0; | |
1381 | ||
1382 | left = isize - *ppos; | |
1383 | if (unlikely(left < len)) | |
1384 | len = left; | |
1385 | ||
1386 | if (splice_grow_spd(pipe, &spd)) | |
1387 | return -ENOMEM; | |
1388 | ||
1389 | index = *ppos >> PAGE_CACHE_SHIFT; | |
1390 | loff = *ppos & ~PAGE_CACHE_MASK; | |
1391 | req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | |
1392 | nr_pages = min(req_pages, pipe->buffers); | |
1393 | ||
708e3508 HD |
1394 | spd.nr_pages = find_get_pages_contig(mapping, index, |
1395 | nr_pages, spd.pages); | |
1396 | index += spd.nr_pages; | |
708e3508 | 1397 | error = 0; |
708e3508 | 1398 | |
71f0e07a | 1399 | while (spd.nr_pages < nr_pages) { |
71f0e07a HD |
1400 | error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL); |
1401 | if (error) | |
1402 | break; | |
1403 | unlock_page(page); | |
708e3508 HD |
1404 | spd.pages[spd.nr_pages++] = page; |
1405 | index++; | |
1406 | } | |
1407 | ||
708e3508 HD |
1408 | index = *ppos >> PAGE_CACHE_SHIFT; |
1409 | nr_pages = spd.nr_pages; | |
1410 | spd.nr_pages = 0; | |
71f0e07a | 1411 | |
708e3508 HD |
1412 | for (page_nr = 0; page_nr < nr_pages; page_nr++) { |
1413 | unsigned int this_len; | |
1414 | ||
1415 | if (!len) | |
1416 | break; | |
1417 | ||
708e3508 HD |
1418 | this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); |
1419 | page = spd.pages[page_nr]; | |
1420 | ||
71f0e07a | 1421 | if (!PageUptodate(page) || page->mapping != mapping) { |
71f0e07a HD |
1422 | error = shmem_getpage(inode, index, &page, |
1423 | SGP_CACHE, NULL); | |
1424 | if (error) | |
708e3508 | 1425 | break; |
71f0e07a HD |
1426 | unlock_page(page); |
1427 | page_cache_release(spd.pages[page_nr]); | |
1428 | spd.pages[page_nr] = page; | |
708e3508 | 1429 | } |
71f0e07a HD |
1430 | |
1431 | isize = i_size_read(inode); | |
708e3508 HD |
1432 | end_index = (isize - 1) >> PAGE_CACHE_SHIFT; |
1433 | if (unlikely(!isize || index > end_index)) | |
1434 | break; | |
1435 | ||
708e3508 HD |
1436 | if (end_index == index) { |
1437 | unsigned int plen; | |
1438 | ||
708e3508 HD |
1439 | plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; |
1440 | if (plen <= loff) | |
1441 | break; | |
1442 | ||
708e3508 HD |
1443 | this_len = min(this_len, plen - loff); |
1444 | len = this_len; | |
1445 | } | |
1446 | ||
1447 | spd.partial[page_nr].offset = loff; | |
1448 | spd.partial[page_nr].len = this_len; | |
1449 | len -= this_len; | |
1450 | loff = 0; | |
1451 | spd.nr_pages++; | |
1452 | index++; | |
1453 | } | |
1454 | ||
708e3508 HD |
1455 | while (page_nr < nr_pages) |
1456 | page_cache_release(spd.pages[page_nr++]); | |
708e3508 HD |
1457 | |
1458 | if (spd.nr_pages) | |
1459 | error = splice_to_pipe(pipe, &spd); | |
1460 | ||
1461 | splice_shrink_spd(pipe, &spd); | |
1462 | ||
1463 | if (error > 0) { | |
1464 | *ppos += error; | |
1465 | file_accessed(in); | |
1466 | } | |
1467 | return error; | |
1468 | } | |
1469 | ||
726c3342 | 1470 | static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) |
1da177e4 | 1471 | { |
726c3342 | 1472 | struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); |
1da177e4 LT |
1473 | |
1474 | buf->f_type = TMPFS_MAGIC; | |
1475 | buf->f_bsize = PAGE_CACHE_SIZE; | |
1476 | buf->f_namelen = NAME_MAX; | |
0edd73b3 | 1477 | if (sbinfo->max_blocks) { |
1da177e4 | 1478 | buf->f_blocks = sbinfo->max_blocks; |
41ffe5d5 HD |
1479 | buf->f_bavail = |
1480 | buf->f_bfree = sbinfo->max_blocks - | |
1481 | percpu_counter_sum(&sbinfo->used_blocks); | |
0edd73b3 HD |
1482 | } |
1483 | if (sbinfo->max_inodes) { | |
1da177e4 LT |
1484 | buf->f_files = sbinfo->max_inodes; |
1485 | buf->f_ffree = sbinfo->free_inodes; | |
1da177e4 LT |
1486 | } |
1487 | /* else leave those fields 0 like simple_statfs */ | |
1488 | return 0; | |
1489 | } | |
1490 | ||
1491 | /* | |
1492 | * File creation. Allocate an inode, and we're done.. | |
1493 | */ | |
1494 | static int | |
1495 | shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) | |
1496 | { | |
0b0a0806 | 1497 | struct inode *inode; |
1da177e4 LT |
1498 | int error = -ENOSPC; |
1499 | ||
454abafe | 1500 | inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); |
1da177e4 | 1501 | if (inode) { |
2a7dba39 EP |
1502 | error = security_inode_init_security(inode, dir, |
1503 | &dentry->d_name, NULL, | |
1504 | NULL, NULL); | |
570bc1c2 SS |
1505 | if (error) { |
1506 | if (error != -EOPNOTSUPP) { | |
1507 | iput(inode); | |
1508 | return error; | |
1509 | } | |
39f0247d | 1510 | } |
1c7c474c CH |
1511 | #ifdef CONFIG_TMPFS_POSIX_ACL |
1512 | error = generic_acl_init(inode, dir); | |
39f0247d AG |
1513 | if (error) { |
1514 | iput(inode); | |
1515 | return error; | |
570bc1c2 | 1516 | } |
718deb6b AV |
1517 | #else |
1518 | error = 0; | |
1c7c474c | 1519 | #endif |
1da177e4 LT |
1520 | dir->i_size += BOGO_DIRENT_SIZE; |
1521 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; | |
1522 | d_instantiate(dentry, inode); | |
1523 | dget(dentry); /* Extra count - pin the dentry in core */ | |
1da177e4 LT |
1524 | } |
1525 | return error; | |
1526 | } | |
1527 | ||
1528 | static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
1529 | { | |
1530 | int error; | |
1531 | ||
1532 | if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) | |
1533 | return error; | |
d8c76e6f | 1534 | inc_nlink(dir); |
1da177e4 LT |
1535 | return 0; |
1536 | } | |
1537 | ||
1538 | static int shmem_create(struct inode *dir, struct dentry *dentry, int mode, | |
1539 | struct nameidata *nd) | |
1540 | { | |
1541 | return shmem_mknod(dir, dentry, mode | S_IFREG, 0); | |
1542 | } | |
1543 | ||
1544 | /* | |
1545 | * Link a file.. | |
1546 | */ | |
1547 | static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) | |
1548 | { | |
1549 | struct inode *inode = old_dentry->d_inode; | |
5b04c689 | 1550 | int ret; |
1da177e4 LT |
1551 | |
1552 | /* | |
1553 | * No ordinary (disk based) filesystem counts links as inodes; | |
1554 | * but each new link needs a new dentry, pinning lowmem, and | |
1555 | * tmpfs dentries cannot be pruned until they are unlinked. | |
1556 | */ | |
5b04c689 PE |
1557 | ret = shmem_reserve_inode(inode->i_sb); |
1558 | if (ret) | |
1559 | goto out; | |
1da177e4 LT |
1560 | |
1561 | dir->i_size += BOGO_DIRENT_SIZE; | |
1562 | inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; | |
d8c76e6f | 1563 | inc_nlink(inode); |
7de9c6ee | 1564 | ihold(inode); /* New dentry reference */ |
1da177e4 LT |
1565 | dget(dentry); /* Extra pinning count for the created dentry */ |
1566 | d_instantiate(dentry, inode); | |
5b04c689 PE |
1567 | out: |
1568 | return ret; | |
1da177e4 LT |
1569 | } |
1570 | ||
1571 | static int shmem_unlink(struct inode *dir, struct dentry *dentry) | |
1572 | { | |
1573 | struct inode *inode = dentry->d_inode; | |
1574 | ||
5b04c689 PE |
1575 | if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) |
1576 | shmem_free_inode(inode->i_sb); | |
1da177e4 LT |
1577 | |
1578 | dir->i_size -= BOGO_DIRENT_SIZE; | |
1579 | inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; | |
9a53c3a7 | 1580 | drop_nlink(inode); |
1da177e4 LT |
1581 | dput(dentry); /* Undo the count from "create" - this does all the work */ |
1582 | return 0; | |
1583 | } | |
1584 | ||
1585 | static int shmem_rmdir(struct inode *dir, struct dentry *dentry) | |
1586 | { | |
1587 | if (!simple_empty(dentry)) | |
1588 | return -ENOTEMPTY; | |
1589 | ||
9a53c3a7 DH |
1590 | drop_nlink(dentry->d_inode); |
1591 | drop_nlink(dir); | |
1da177e4 LT |
1592 | return shmem_unlink(dir, dentry); |
1593 | } | |
1594 | ||
1595 | /* | |
1596 | * The VFS layer already does all the dentry stuff for rename, | |
1597 | * we just have to decrement the usage count for the target if | |
1598 | * it exists so that the VFS layer correctly free's it when it | |
1599 | * gets overwritten. | |
1600 | */ | |
1601 | static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) | |
1602 | { | |
1603 | struct inode *inode = old_dentry->d_inode; | |
1604 | int they_are_dirs = S_ISDIR(inode->i_mode); | |
1605 | ||
1606 | if (!simple_empty(new_dentry)) | |
1607 | return -ENOTEMPTY; | |
1608 | ||
1609 | if (new_dentry->d_inode) { | |
1610 | (void) shmem_unlink(new_dir, new_dentry); | |
1611 | if (they_are_dirs) | |
9a53c3a7 | 1612 | drop_nlink(old_dir); |
1da177e4 | 1613 | } else if (they_are_dirs) { |
9a53c3a7 | 1614 | drop_nlink(old_dir); |
d8c76e6f | 1615 | inc_nlink(new_dir); |
1da177e4 LT |
1616 | } |
1617 | ||
1618 | old_dir->i_size -= BOGO_DIRENT_SIZE; | |
1619 | new_dir->i_size += BOGO_DIRENT_SIZE; | |
1620 | old_dir->i_ctime = old_dir->i_mtime = | |
1621 | new_dir->i_ctime = new_dir->i_mtime = | |
1622 | inode->i_ctime = CURRENT_TIME; | |
1623 | return 0; | |
1624 | } | |
1625 | ||
1626 | static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) | |
1627 | { | |
1628 | int error; | |
1629 | int len; | |
1630 | struct inode *inode; | |
9276aad6 | 1631 | struct page *page; |
1da177e4 LT |
1632 | char *kaddr; |
1633 | struct shmem_inode_info *info; | |
1634 | ||
1635 | len = strlen(symname) + 1; | |
1636 | if (len > PAGE_CACHE_SIZE) | |
1637 | return -ENAMETOOLONG; | |
1638 | ||
454abafe | 1639 | inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE); |
1da177e4 LT |
1640 | if (!inode) |
1641 | return -ENOSPC; | |
1642 | ||
2a7dba39 EP |
1643 | error = security_inode_init_security(inode, dir, &dentry->d_name, NULL, |
1644 | NULL, NULL); | |
570bc1c2 SS |
1645 | if (error) { |
1646 | if (error != -EOPNOTSUPP) { | |
1647 | iput(inode); | |
1648 | return error; | |
1649 | } | |
1650 | error = 0; | |
1651 | } | |
1652 | ||
1da177e4 LT |
1653 | info = SHMEM_I(inode); |
1654 | inode->i_size = len-1; | |
b09e0fa4 | 1655 | if (len <= SHMEM_SYMLINK_INLINE_LEN) { |
1da177e4 | 1656 | /* do it inline */ |
b09e0fa4 | 1657 | memcpy(info->inline_symlink, symname, len); |
1da177e4 LT |
1658 | inode->i_op = &shmem_symlink_inline_operations; |
1659 | } else { | |
1660 | error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL); | |
1661 | if (error) { | |
1662 | iput(inode); | |
1663 | return error; | |
1664 | } | |
14fcc23f | 1665 | inode->i_mapping->a_ops = &shmem_aops; |
1da177e4 LT |
1666 | inode->i_op = &shmem_symlink_inode_operations; |
1667 | kaddr = kmap_atomic(page, KM_USER0); | |
1668 | memcpy(kaddr, symname, len); | |
1669 | kunmap_atomic(kaddr, KM_USER0); | |
1670 | set_page_dirty(page); | |
6746aff7 | 1671 | unlock_page(page); |
1da177e4 LT |
1672 | page_cache_release(page); |
1673 | } | |
1da177e4 LT |
1674 | dir->i_size += BOGO_DIRENT_SIZE; |
1675 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; | |
1676 | d_instantiate(dentry, inode); | |
1677 | dget(dentry); | |
1678 | return 0; | |
1679 | } | |
1680 | ||
cc314eef | 1681 | static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd) |
1da177e4 | 1682 | { |
b09e0fa4 | 1683 | nd_set_link(nd, SHMEM_I(dentry->d_inode)->inline_symlink); |
cc314eef | 1684 | return NULL; |
1da177e4 LT |
1685 | } |
1686 | ||
cc314eef | 1687 | static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd) |
1da177e4 LT |
1688 | { |
1689 | struct page *page = NULL; | |
41ffe5d5 HD |
1690 | int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL); |
1691 | nd_set_link(nd, error ? ERR_PTR(error) : kmap(page)); | |
d3602444 HD |
1692 | if (page) |
1693 | unlock_page(page); | |
cc314eef | 1694 | return page; |
1da177e4 LT |
1695 | } |
1696 | ||
cc314eef | 1697 | static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) |
1da177e4 LT |
1698 | { |
1699 | if (!IS_ERR(nd_get_link(nd))) { | |
cc314eef | 1700 | struct page *page = cookie; |
1da177e4 LT |
1701 | kunmap(page); |
1702 | mark_page_accessed(page); | |
1703 | page_cache_release(page); | |
1da177e4 LT |
1704 | } |
1705 | } | |
1706 | ||
b09e0fa4 | 1707 | #ifdef CONFIG_TMPFS_XATTR |
46711810 | 1708 | /* |
b09e0fa4 EP |
1709 | * Superblocks without xattr inode operations may get some security.* xattr |
1710 | * support from the LSM "for free". As soon as we have any other xattrs | |
39f0247d AG |
1711 | * like ACLs, we also need to implement the security.* handlers at |
1712 | * filesystem level, though. | |
1713 | */ | |
1714 | ||
b09e0fa4 EP |
1715 | static int shmem_xattr_get(struct dentry *dentry, const char *name, |
1716 | void *buffer, size_t size) | |
39f0247d | 1717 | { |
b09e0fa4 EP |
1718 | struct shmem_inode_info *info; |
1719 | struct shmem_xattr *xattr; | |
1720 | int ret = -ENODATA; | |
39f0247d | 1721 | |
b09e0fa4 EP |
1722 | info = SHMEM_I(dentry->d_inode); |
1723 | ||
1724 | spin_lock(&info->lock); | |
1725 | list_for_each_entry(xattr, &info->xattr_list, list) { | |
1726 | if (strcmp(name, xattr->name)) | |
1727 | continue; | |
1728 | ||
1729 | ret = xattr->size; | |
1730 | if (buffer) { | |
1731 | if (size < xattr->size) | |
1732 | ret = -ERANGE; | |
1733 | else | |
1734 | memcpy(buffer, xattr->value, xattr->size); | |
1735 | } | |
1736 | break; | |
1737 | } | |
1738 | spin_unlock(&info->lock); | |
1739 | return ret; | |
39f0247d AG |
1740 | } |
1741 | ||
b09e0fa4 EP |
1742 | static int shmem_xattr_set(struct dentry *dentry, const char *name, |
1743 | const void *value, size_t size, int flags) | |
39f0247d | 1744 | { |
b09e0fa4 EP |
1745 | struct inode *inode = dentry->d_inode; |
1746 | struct shmem_inode_info *info = SHMEM_I(inode); | |
1747 | struct shmem_xattr *xattr; | |
1748 | struct shmem_xattr *new_xattr = NULL; | |
1749 | size_t len; | |
1750 | int err = 0; | |
1751 | ||
1752 | /* value == NULL means remove */ | |
1753 | if (value) { | |
1754 | /* wrap around? */ | |
1755 | len = sizeof(*new_xattr) + size; | |
1756 | if (len <= sizeof(*new_xattr)) | |
1757 | return -ENOMEM; | |
1758 | ||
1759 | new_xattr = kmalloc(len, GFP_KERNEL); | |
1760 | if (!new_xattr) | |
1761 | return -ENOMEM; | |
1762 | ||
1763 | new_xattr->name = kstrdup(name, GFP_KERNEL); | |
1764 | if (!new_xattr->name) { | |
1765 | kfree(new_xattr); | |
1766 | return -ENOMEM; | |
1767 | } | |
1768 | ||
1769 | new_xattr->size = size; | |
1770 | memcpy(new_xattr->value, value, size); | |
1771 | } | |
1772 | ||
1773 | spin_lock(&info->lock); | |
1774 | list_for_each_entry(xattr, &info->xattr_list, list) { | |
1775 | if (!strcmp(name, xattr->name)) { | |
1776 | if (flags & XATTR_CREATE) { | |
1777 | xattr = new_xattr; | |
1778 | err = -EEXIST; | |
1779 | } else if (new_xattr) { | |
1780 | list_replace(&xattr->list, &new_xattr->list); | |
1781 | } else { | |
1782 | list_del(&xattr->list); | |
1783 | } | |
1784 | goto out; | |
1785 | } | |
1786 | } | |
1787 | if (flags & XATTR_REPLACE) { | |
1788 | xattr = new_xattr; | |
1789 | err = -ENODATA; | |
1790 | } else { | |
1791 | list_add(&new_xattr->list, &info->xattr_list); | |
1792 | xattr = NULL; | |
1793 | } | |
1794 | out: | |
1795 | spin_unlock(&info->lock); | |
1796 | if (xattr) | |
1797 | kfree(xattr->name); | |
1798 | kfree(xattr); | |
1799 | return err; | |
39f0247d AG |
1800 | } |
1801 | ||
bb435453 | 1802 | static const struct xattr_handler *shmem_xattr_handlers[] = { |
b09e0fa4 | 1803 | #ifdef CONFIG_TMPFS_POSIX_ACL |
1c7c474c CH |
1804 | &generic_acl_access_handler, |
1805 | &generic_acl_default_handler, | |
b09e0fa4 | 1806 | #endif |
39f0247d AG |
1807 | NULL |
1808 | }; | |
b09e0fa4 EP |
1809 | |
1810 | static int shmem_xattr_validate(const char *name) | |
1811 | { | |
1812 | struct { const char *prefix; size_t len; } arr[] = { | |
1813 | { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN }, | |
1814 | { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN } | |
1815 | }; | |
1816 | int i; | |
1817 | ||
1818 | for (i = 0; i < ARRAY_SIZE(arr); i++) { | |
1819 | size_t preflen = arr[i].len; | |
1820 | if (strncmp(name, arr[i].prefix, preflen) == 0) { | |
1821 | if (!name[preflen]) | |
1822 | return -EINVAL; | |
1823 | return 0; | |
1824 | } | |
1825 | } | |
1826 | return -EOPNOTSUPP; | |
1827 | } | |
1828 | ||
1829 | static ssize_t shmem_getxattr(struct dentry *dentry, const char *name, | |
1830 | void *buffer, size_t size) | |
1831 | { | |
1832 | int err; | |
1833 | ||
1834 | /* | |
1835 | * If this is a request for a synthetic attribute in the system.* | |
1836 | * namespace use the generic infrastructure to resolve a handler | |
1837 | * for it via sb->s_xattr. | |
1838 | */ | |
1839 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) | |
1840 | return generic_getxattr(dentry, name, buffer, size); | |
1841 | ||
1842 | err = shmem_xattr_validate(name); | |
1843 | if (err) | |
1844 | return err; | |
1845 | ||
1846 | return shmem_xattr_get(dentry, name, buffer, size); | |
1847 | } | |
1848 | ||
1849 | static int shmem_setxattr(struct dentry *dentry, const char *name, | |
1850 | const void *value, size_t size, int flags) | |
1851 | { | |
1852 | int err; | |
1853 | ||
1854 | /* | |
1855 | * If this is a request for a synthetic attribute in the system.* | |
1856 | * namespace use the generic infrastructure to resolve a handler | |
1857 | * for it via sb->s_xattr. | |
1858 | */ | |
1859 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) | |
1860 | return generic_setxattr(dentry, name, value, size, flags); | |
1861 | ||
1862 | err = shmem_xattr_validate(name); | |
1863 | if (err) | |
1864 | return err; | |
1865 | ||
1866 | if (size == 0) | |
1867 | value = ""; /* empty EA, do not remove */ | |
1868 | ||
1869 | return shmem_xattr_set(dentry, name, value, size, flags); | |
1870 | ||
1871 | } | |
1872 | ||
1873 | static int shmem_removexattr(struct dentry *dentry, const char *name) | |
1874 | { | |
1875 | int err; | |
1876 | ||
1877 | /* | |
1878 | * If this is a request for a synthetic attribute in the system.* | |
1879 | * namespace use the generic infrastructure to resolve a handler | |
1880 | * for it via sb->s_xattr. | |
1881 | */ | |
1882 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) | |
1883 | return generic_removexattr(dentry, name); | |
1884 | ||
1885 | err = shmem_xattr_validate(name); | |
1886 | if (err) | |
1887 | return err; | |
1888 | ||
1889 | return shmem_xattr_set(dentry, name, NULL, 0, XATTR_REPLACE); | |
1890 | } | |
1891 | ||
1892 | static bool xattr_is_trusted(const char *name) | |
1893 | { | |
1894 | return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN); | |
1895 | } | |
1896 | ||
1897 | static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) | |
1898 | { | |
1899 | bool trusted = capable(CAP_SYS_ADMIN); | |
1900 | struct shmem_xattr *xattr; | |
1901 | struct shmem_inode_info *info; | |
1902 | size_t used = 0; | |
1903 | ||
1904 | info = SHMEM_I(dentry->d_inode); | |
1905 | ||
1906 | spin_lock(&info->lock); | |
1907 | list_for_each_entry(xattr, &info->xattr_list, list) { | |
1908 | size_t len; | |
1909 | ||
1910 | /* skip "trusted." attributes for unprivileged callers */ | |
1911 | if (!trusted && xattr_is_trusted(xattr->name)) | |
1912 | continue; | |
1913 | ||
1914 | len = strlen(xattr->name) + 1; | |
1915 | used += len; | |
1916 | if (buffer) { | |
1917 | if (size < used) { | |
1918 | used = -ERANGE; | |
1919 | break; | |
1920 | } | |
1921 | memcpy(buffer, xattr->name, len); | |
1922 | buffer += len; | |
1923 | } | |
1924 | } | |
1925 | spin_unlock(&info->lock); | |
1926 | ||
1927 | return used; | |
1928 | } | |
1929 | #endif /* CONFIG_TMPFS_XATTR */ | |
1930 | ||
1931 | static const struct inode_operations shmem_symlink_inline_operations = { | |
1932 | .readlink = generic_readlink, | |
1933 | .follow_link = shmem_follow_link_inline, | |
1934 | #ifdef CONFIG_TMPFS_XATTR | |
1935 | .setxattr = shmem_setxattr, | |
1936 | .getxattr = shmem_getxattr, | |
1937 | .listxattr = shmem_listxattr, | |
1938 | .removexattr = shmem_removexattr, | |
1939 | #endif | |
1940 | }; | |
1941 | ||
1942 | static const struct inode_operations shmem_symlink_inode_operations = { | |
1943 | .readlink = generic_readlink, | |
1944 | .follow_link = shmem_follow_link, | |
1945 | .put_link = shmem_put_link, | |
1946 | #ifdef CONFIG_TMPFS_XATTR | |
1947 | .setxattr = shmem_setxattr, | |
1948 | .getxattr = shmem_getxattr, | |
1949 | .listxattr = shmem_listxattr, | |
1950 | .removexattr = shmem_removexattr, | |
39f0247d | 1951 | #endif |
b09e0fa4 | 1952 | }; |
39f0247d | 1953 | |
91828a40 DG |
1954 | static struct dentry *shmem_get_parent(struct dentry *child) |
1955 | { | |
1956 | return ERR_PTR(-ESTALE); | |
1957 | } | |
1958 | ||
1959 | static int shmem_match(struct inode *ino, void *vfh) | |
1960 | { | |
1961 | __u32 *fh = vfh; | |
1962 | __u64 inum = fh[2]; | |
1963 | inum = (inum << 32) | fh[1]; | |
1964 | return ino->i_ino == inum && fh[0] == ino->i_generation; | |
1965 | } | |
1966 | ||
480b116c CH |
1967 | static struct dentry *shmem_fh_to_dentry(struct super_block *sb, |
1968 | struct fid *fid, int fh_len, int fh_type) | |
91828a40 | 1969 | { |
91828a40 | 1970 | struct inode *inode; |
480b116c CH |
1971 | struct dentry *dentry = NULL; |
1972 | u64 inum = fid->raw[2]; | |
1973 | inum = (inum << 32) | fid->raw[1]; | |
1974 | ||
1975 | if (fh_len < 3) | |
1976 | return NULL; | |
91828a40 | 1977 | |
480b116c CH |
1978 | inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), |
1979 | shmem_match, fid->raw); | |
91828a40 | 1980 | if (inode) { |
480b116c | 1981 | dentry = d_find_alias(inode); |
91828a40 DG |
1982 | iput(inode); |
1983 | } | |
1984 | ||
480b116c | 1985 | return dentry; |
91828a40 DG |
1986 | } |
1987 | ||
1988 | static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len, | |
1989 | int connectable) | |
1990 | { | |
1991 | struct inode *inode = dentry->d_inode; | |
1992 | ||
5fe0c237 AK |
1993 | if (*len < 3) { |
1994 | *len = 3; | |
91828a40 | 1995 | return 255; |
5fe0c237 | 1996 | } |
91828a40 | 1997 | |
1d3382cb | 1998 | if (inode_unhashed(inode)) { |
91828a40 DG |
1999 | /* Unfortunately insert_inode_hash is not idempotent, |
2000 | * so as we hash inodes here rather than at creation | |
2001 | * time, we need a lock to ensure we only try | |
2002 | * to do it once | |
2003 | */ | |
2004 | static DEFINE_SPINLOCK(lock); | |
2005 | spin_lock(&lock); | |
1d3382cb | 2006 | if (inode_unhashed(inode)) |
91828a40 DG |
2007 | __insert_inode_hash(inode, |
2008 | inode->i_ino + inode->i_generation); | |
2009 | spin_unlock(&lock); | |
2010 | } | |
2011 | ||
2012 | fh[0] = inode->i_generation; | |
2013 | fh[1] = inode->i_ino; | |
2014 | fh[2] = ((__u64)inode->i_ino) >> 32; | |
2015 | ||
2016 | *len = 3; | |
2017 | return 1; | |
2018 | } | |
2019 | ||
39655164 | 2020 | static const struct export_operations shmem_export_ops = { |
91828a40 | 2021 | .get_parent = shmem_get_parent, |
91828a40 | 2022 | .encode_fh = shmem_encode_fh, |
480b116c | 2023 | .fh_to_dentry = shmem_fh_to_dentry, |
91828a40 DG |
2024 | }; |
2025 | ||
680d794b | 2026 | static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, |
2027 | bool remount) | |
1da177e4 LT |
2028 | { |
2029 | char *this_char, *value, *rest; | |
2030 | ||
b00dc3ad HD |
2031 | while (options != NULL) { |
2032 | this_char = options; | |
2033 | for (;;) { | |
2034 | /* | |
2035 | * NUL-terminate this option: unfortunately, | |
2036 | * mount options form a comma-separated list, | |
2037 | * but mpol's nodelist may also contain commas. | |
2038 | */ | |
2039 | options = strchr(options, ','); | |
2040 | if (options == NULL) | |
2041 | break; | |
2042 | options++; | |
2043 | if (!isdigit(*options)) { | |
2044 | options[-1] = '\0'; | |
2045 | break; | |
2046 | } | |
2047 | } | |
1da177e4 LT |
2048 | if (!*this_char) |
2049 | continue; | |
2050 | if ((value = strchr(this_char,'=')) != NULL) { | |
2051 | *value++ = 0; | |
2052 | } else { | |
2053 | printk(KERN_ERR | |
2054 | "tmpfs: No value for mount option '%s'\n", | |
2055 | this_char); | |
2056 | return 1; | |
2057 | } | |
2058 | ||
2059 | if (!strcmp(this_char,"size")) { | |
2060 | unsigned long long size; | |
2061 | size = memparse(value,&rest); | |
2062 | if (*rest == '%') { | |
2063 | size <<= PAGE_SHIFT; | |
2064 | size *= totalram_pages; | |
2065 | do_div(size, 100); | |
2066 | rest++; | |
2067 | } | |
2068 | if (*rest) | |
2069 | goto bad_val; | |
680d794b | 2070 | sbinfo->max_blocks = |
2071 | DIV_ROUND_UP(size, PAGE_CACHE_SIZE); | |
1da177e4 | 2072 | } else if (!strcmp(this_char,"nr_blocks")) { |
680d794b | 2073 | sbinfo->max_blocks = memparse(value, &rest); |
1da177e4 LT |
2074 | if (*rest) |
2075 | goto bad_val; | |
2076 | } else if (!strcmp(this_char,"nr_inodes")) { | |
680d794b | 2077 | sbinfo->max_inodes = memparse(value, &rest); |
1da177e4 LT |
2078 | if (*rest) |
2079 | goto bad_val; | |
2080 | } else if (!strcmp(this_char,"mode")) { | |
680d794b | 2081 | if (remount) |
1da177e4 | 2082 | continue; |
680d794b | 2083 | sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; |
1da177e4 LT |
2084 | if (*rest) |
2085 | goto bad_val; | |
2086 | } else if (!strcmp(this_char,"uid")) { | |
680d794b | 2087 | if (remount) |
1da177e4 | 2088 | continue; |
680d794b | 2089 | sbinfo->uid = simple_strtoul(value, &rest, 0); |
1da177e4 LT |
2090 | if (*rest) |
2091 | goto bad_val; | |
2092 | } else if (!strcmp(this_char,"gid")) { | |
680d794b | 2093 | if (remount) |
1da177e4 | 2094 | continue; |
680d794b | 2095 | sbinfo->gid = simple_strtoul(value, &rest, 0); |
1da177e4 LT |
2096 | if (*rest) |
2097 | goto bad_val; | |
7339ff83 | 2098 | } else if (!strcmp(this_char,"mpol")) { |
71fe804b | 2099 | if (mpol_parse_str(value, &sbinfo->mpol, 1)) |
7339ff83 | 2100 | goto bad_val; |
1da177e4 LT |
2101 | } else { |
2102 | printk(KERN_ERR "tmpfs: Bad mount option %s\n", | |
2103 | this_char); | |
2104 | return 1; | |
2105 | } | |
2106 | } | |
2107 | return 0; | |
2108 | ||
2109 | bad_val: | |
2110 | printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n", | |
2111 | value, this_char); | |
2112 | return 1; | |
2113 | ||
2114 | } | |
2115 | ||
2116 | static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) | |
2117 | { | |
2118 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); | |
680d794b | 2119 | struct shmem_sb_info config = *sbinfo; |
0edd73b3 HD |
2120 | unsigned long inodes; |
2121 | int error = -EINVAL; | |
2122 | ||
680d794b | 2123 | if (shmem_parse_options(data, &config, true)) |
0edd73b3 | 2124 | return error; |
1da177e4 | 2125 | |
0edd73b3 | 2126 | spin_lock(&sbinfo->stat_lock); |
0edd73b3 | 2127 | inodes = sbinfo->max_inodes - sbinfo->free_inodes; |
7e496299 | 2128 | if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) |
0edd73b3 | 2129 | goto out; |
680d794b | 2130 | if (config.max_inodes < inodes) |
0edd73b3 HD |
2131 | goto out; |
2132 | /* | |
54af6042 | 2133 | * Those tests disallow limited->unlimited while any are in use; |
0edd73b3 HD |
2134 | * but we must separately disallow unlimited->limited, because |
2135 | * in that case we have no record of how much is already in use. | |
2136 | */ | |
680d794b | 2137 | if (config.max_blocks && !sbinfo->max_blocks) |
0edd73b3 | 2138 | goto out; |
680d794b | 2139 | if (config.max_inodes && !sbinfo->max_inodes) |
0edd73b3 HD |
2140 | goto out; |
2141 | ||
2142 | error = 0; | |
680d794b | 2143 | sbinfo->max_blocks = config.max_blocks; |
680d794b | 2144 | sbinfo->max_inodes = config.max_inodes; |
2145 | sbinfo->free_inodes = config.max_inodes - inodes; | |
71fe804b LS |
2146 | |
2147 | mpol_put(sbinfo->mpol); | |
2148 | sbinfo->mpol = config.mpol; /* transfers initial ref */ | |
0edd73b3 HD |
2149 | out: |
2150 | spin_unlock(&sbinfo->stat_lock); | |
2151 | return error; | |
1da177e4 | 2152 | } |
680d794b | 2153 | |
2154 | static int shmem_show_options(struct seq_file *seq, struct vfsmount *vfs) | |
2155 | { | |
2156 | struct shmem_sb_info *sbinfo = SHMEM_SB(vfs->mnt_sb); | |
2157 | ||
2158 | if (sbinfo->max_blocks != shmem_default_max_blocks()) | |
2159 | seq_printf(seq, ",size=%luk", | |
2160 | sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10)); | |
2161 | if (sbinfo->max_inodes != shmem_default_max_inodes()) | |
2162 | seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); | |
2163 | if (sbinfo->mode != (S_IRWXUGO | S_ISVTX)) | |
2164 | seq_printf(seq, ",mode=%03o", sbinfo->mode); | |
2165 | if (sbinfo->uid != 0) | |
2166 | seq_printf(seq, ",uid=%u", sbinfo->uid); | |
2167 | if (sbinfo->gid != 0) | |
2168 | seq_printf(seq, ",gid=%u", sbinfo->gid); | |
71fe804b | 2169 | shmem_show_mpol(seq, sbinfo->mpol); |
680d794b | 2170 | return 0; |
2171 | } | |
2172 | #endif /* CONFIG_TMPFS */ | |
1da177e4 LT |
2173 | |
2174 | static void shmem_put_super(struct super_block *sb) | |
2175 | { | |
602586a8 HD |
2176 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
2177 | ||
2178 | percpu_counter_destroy(&sbinfo->used_blocks); | |
2179 | kfree(sbinfo); | |
1da177e4 LT |
2180 | sb->s_fs_info = NULL; |
2181 | } | |
2182 | ||
2b2af54a | 2183 | int shmem_fill_super(struct super_block *sb, void *data, int silent) |
1da177e4 LT |
2184 | { |
2185 | struct inode *inode; | |
2186 | struct dentry *root; | |
0edd73b3 | 2187 | struct shmem_sb_info *sbinfo; |
680d794b | 2188 | int err = -ENOMEM; |
2189 | ||
2190 | /* Round up to L1_CACHE_BYTES to resist false sharing */ | |
425fbf04 | 2191 | sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), |
680d794b | 2192 | L1_CACHE_BYTES), GFP_KERNEL); |
2193 | if (!sbinfo) | |
2194 | return -ENOMEM; | |
2195 | ||
680d794b | 2196 | sbinfo->mode = S_IRWXUGO | S_ISVTX; |
76aac0e9 DH |
2197 | sbinfo->uid = current_fsuid(); |
2198 | sbinfo->gid = current_fsgid(); | |
680d794b | 2199 | sb->s_fs_info = sbinfo; |
1da177e4 | 2200 | |
0edd73b3 | 2201 | #ifdef CONFIG_TMPFS |
1da177e4 LT |
2202 | /* |
2203 | * Per default we only allow half of the physical ram per | |
2204 | * tmpfs instance, limiting inodes to one per page of lowmem; | |
2205 | * but the internal instance is left unlimited. | |
2206 | */ | |
2207 | if (!(sb->s_flags & MS_NOUSER)) { | |
680d794b | 2208 | sbinfo->max_blocks = shmem_default_max_blocks(); |
2209 | sbinfo->max_inodes = shmem_default_max_inodes(); | |
2210 | if (shmem_parse_options(data, sbinfo, false)) { | |
2211 | err = -EINVAL; | |
2212 | goto failed; | |
2213 | } | |
1da177e4 | 2214 | } |
91828a40 | 2215 | sb->s_export_op = &shmem_export_ops; |
1da177e4 LT |
2216 | #else |
2217 | sb->s_flags |= MS_NOUSER; | |
2218 | #endif | |
2219 | ||
0edd73b3 | 2220 | spin_lock_init(&sbinfo->stat_lock); |
602586a8 HD |
2221 | if (percpu_counter_init(&sbinfo->used_blocks, 0)) |
2222 | goto failed; | |
680d794b | 2223 | sbinfo->free_inodes = sbinfo->max_inodes; |
0edd73b3 | 2224 | |
285b2c4f | 2225 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
1da177e4 LT |
2226 | sb->s_blocksize = PAGE_CACHE_SIZE; |
2227 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | |
2228 | sb->s_magic = TMPFS_MAGIC; | |
2229 | sb->s_op = &shmem_ops; | |
cfd95a9c | 2230 | sb->s_time_gran = 1; |
b09e0fa4 | 2231 | #ifdef CONFIG_TMPFS_XATTR |
39f0247d | 2232 | sb->s_xattr = shmem_xattr_handlers; |
b09e0fa4 EP |
2233 | #endif |
2234 | #ifdef CONFIG_TMPFS_POSIX_ACL | |
39f0247d AG |
2235 | sb->s_flags |= MS_POSIXACL; |
2236 | #endif | |
0edd73b3 | 2237 | |
454abafe | 2238 | inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); |
1da177e4 LT |
2239 | if (!inode) |
2240 | goto failed; | |
680d794b | 2241 | inode->i_uid = sbinfo->uid; |
2242 | inode->i_gid = sbinfo->gid; | |
1da177e4 LT |
2243 | root = d_alloc_root(inode); |
2244 | if (!root) | |
2245 | goto failed_iput; | |
2246 | sb->s_root = root; | |
2247 | return 0; | |
2248 | ||
2249 | failed_iput: | |
2250 | iput(inode); | |
2251 | failed: | |
2252 | shmem_put_super(sb); | |
2253 | return err; | |
2254 | } | |
2255 | ||
fcc234f8 | 2256 | static struct kmem_cache *shmem_inode_cachep; |
1da177e4 LT |
2257 | |
2258 | static struct inode *shmem_alloc_inode(struct super_block *sb) | |
2259 | { | |
41ffe5d5 HD |
2260 | struct shmem_inode_info *info; |
2261 | info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); | |
2262 | if (!info) | |
1da177e4 | 2263 | return NULL; |
41ffe5d5 | 2264 | return &info->vfs_inode; |
1da177e4 LT |
2265 | } |
2266 | ||
41ffe5d5 | 2267 | static void shmem_destroy_callback(struct rcu_head *head) |
fa0d7e3d NP |
2268 | { |
2269 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
2270 | INIT_LIST_HEAD(&inode->i_dentry); | |
2271 | kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); | |
2272 | } | |
2273 | ||
1da177e4 LT |
2274 | static void shmem_destroy_inode(struct inode *inode) |
2275 | { | |
2276 | if ((inode->i_mode & S_IFMT) == S_IFREG) { | |
2277 | /* only struct inode is valid if it's an inline symlink */ | |
2278 | mpol_free_shared_policy(&SHMEM_I(inode)->policy); | |
2279 | } | |
41ffe5d5 | 2280 | call_rcu(&inode->i_rcu, shmem_destroy_callback); |
1da177e4 LT |
2281 | } |
2282 | ||
41ffe5d5 | 2283 | static void shmem_init_inode(void *foo) |
1da177e4 | 2284 | { |
41ffe5d5 HD |
2285 | struct shmem_inode_info *info = foo; |
2286 | inode_init_once(&info->vfs_inode); | |
1da177e4 LT |
2287 | } |
2288 | ||
41ffe5d5 | 2289 | static int shmem_init_inodecache(void) |
1da177e4 LT |
2290 | { |
2291 | shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", | |
2292 | sizeof(struct shmem_inode_info), | |
41ffe5d5 | 2293 | 0, SLAB_PANIC, shmem_init_inode); |
1da177e4 LT |
2294 | return 0; |
2295 | } | |
2296 | ||
41ffe5d5 | 2297 | static void shmem_destroy_inodecache(void) |
1da177e4 | 2298 | { |
1a1d92c1 | 2299 | kmem_cache_destroy(shmem_inode_cachep); |
1da177e4 LT |
2300 | } |
2301 | ||
f5e54d6e | 2302 | static const struct address_space_operations shmem_aops = { |
1da177e4 | 2303 | .writepage = shmem_writepage, |
76719325 | 2304 | .set_page_dirty = __set_page_dirty_no_writeback, |
1da177e4 | 2305 | #ifdef CONFIG_TMPFS |
800d15a5 NP |
2306 | .write_begin = shmem_write_begin, |
2307 | .write_end = shmem_write_end, | |
1da177e4 | 2308 | #endif |
304dbdb7 | 2309 | .migratepage = migrate_page, |
aa261f54 | 2310 | .error_remove_page = generic_error_remove_page, |
1da177e4 LT |
2311 | }; |
2312 | ||
15ad7cdc | 2313 | static const struct file_operations shmem_file_operations = { |
1da177e4 LT |
2314 | .mmap = shmem_mmap, |
2315 | #ifdef CONFIG_TMPFS | |
2316 | .llseek = generic_file_llseek, | |
bcd78e49 | 2317 | .read = do_sync_read, |
5402b976 | 2318 | .write = do_sync_write, |
bcd78e49 | 2319 | .aio_read = shmem_file_aio_read, |
5402b976 | 2320 | .aio_write = generic_file_aio_write, |
1b061d92 | 2321 | .fsync = noop_fsync, |
708e3508 | 2322 | .splice_read = shmem_file_splice_read, |
ae976416 | 2323 | .splice_write = generic_file_splice_write, |
1da177e4 LT |
2324 | #endif |
2325 | }; | |
2326 | ||
92e1d5be | 2327 | static const struct inode_operations shmem_inode_operations = { |
94c1e62d | 2328 | .setattr = shmem_setattr, |
f6b3ec23 | 2329 | .truncate_range = shmem_truncate_range, |
b09e0fa4 EP |
2330 | #ifdef CONFIG_TMPFS_XATTR |
2331 | .setxattr = shmem_setxattr, | |
2332 | .getxattr = shmem_getxattr, | |
2333 | .listxattr = shmem_listxattr, | |
2334 | .removexattr = shmem_removexattr, | |
2335 | #endif | |
1da177e4 LT |
2336 | }; |
2337 | ||
92e1d5be | 2338 | static const struct inode_operations shmem_dir_inode_operations = { |
1da177e4 LT |
2339 | #ifdef CONFIG_TMPFS |
2340 | .create = shmem_create, | |
2341 | .lookup = simple_lookup, | |
2342 | .link = shmem_link, | |
2343 | .unlink = shmem_unlink, | |
2344 | .symlink = shmem_symlink, | |
2345 | .mkdir = shmem_mkdir, | |
2346 | .rmdir = shmem_rmdir, | |
2347 | .mknod = shmem_mknod, | |
2348 | .rename = shmem_rename, | |
1da177e4 | 2349 | #endif |
b09e0fa4 EP |
2350 | #ifdef CONFIG_TMPFS_XATTR |
2351 | .setxattr = shmem_setxattr, | |
2352 | .getxattr = shmem_getxattr, | |
2353 | .listxattr = shmem_listxattr, | |
2354 | .removexattr = shmem_removexattr, | |
2355 | #endif | |
39f0247d | 2356 | #ifdef CONFIG_TMPFS_POSIX_ACL |
94c1e62d | 2357 | .setattr = shmem_setattr, |
39f0247d AG |
2358 | #endif |
2359 | }; | |
2360 | ||
92e1d5be | 2361 | static const struct inode_operations shmem_special_inode_operations = { |
b09e0fa4 EP |
2362 | #ifdef CONFIG_TMPFS_XATTR |
2363 | .setxattr = shmem_setxattr, | |
2364 | .getxattr = shmem_getxattr, | |
2365 | .listxattr = shmem_listxattr, | |
2366 | .removexattr = shmem_removexattr, | |
2367 | #endif | |
39f0247d | 2368 | #ifdef CONFIG_TMPFS_POSIX_ACL |
94c1e62d | 2369 | .setattr = shmem_setattr, |
39f0247d | 2370 | #endif |
1da177e4 LT |
2371 | }; |
2372 | ||
759b9775 | 2373 | static const struct super_operations shmem_ops = { |
1da177e4 LT |
2374 | .alloc_inode = shmem_alloc_inode, |
2375 | .destroy_inode = shmem_destroy_inode, | |
2376 | #ifdef CONFIG_TMPFS | |
2377 | .statfs = shmem_statfs, | |
2378 | .remount_fs = shmem_remount_fs, | |
680d794b | 2379 | .show_options = shmem_show_options, |
1da177e4 | 2380 | #endif |
1f895f75 | 2381 | .evict_inode = shmem_evict_inode, |
1da177e4 LT |
2382 | .drop_inode = generic_delete_inode, |
2383 | .put_super = shmem_put_super, | |
2384 | }; | |
2385 | ||
f0f37e2f | 2386 | static const struct vm_operations_struct shmem_vm_ops = { |
54cb8821 | 2387 | .fault = shmem_fault, |
1da177e4 LT |
2388 | #ifdef CONFIG_NUMA |
2389 | .set_policy = shmem_set_policy, | |
2390 | .get_policy = shmem_get_policy, | |
2391 | #endif | |
2392 | }; | |
2393 | ||
3c26ff6e AV |
2394 | static struct dentry *shmem_mount(struct file_system_type *fs_type, |
2395 | int flags, const char *dev_name, void *data) | |
1da177e4 | 2396 | { |
3c26ff6e | 2397 | return mount_nodev(fs_type, flags, data, shmem_fill_super); |
1da177e4 LT |
2398 | } |
2399 | ||
41ffe5d5 | 2400 | static struct file_system_type shmem_fs_type = { |
1da177e4 LT |
2401 | .owner = THIS_MODULE, |
2402 | .name = "tmpfs", | |
3c26ff6e | 2403 | .mount = shmem_mount, |
1da177e4 LT |
2404 | .kill_sb = kill_litter_super, |
2405 | }; | |
1da177e4 | 2406 | |
41ffe5d5 | 2407 | int __init shmem_init(void) |
1da177e4 LT |
2408 | { |
2409 | int error; | |
2410 | ||
e0bf68dd PZ |
2411 | error = bdi_init(&shmem_backing_dev_info); |
2412 | if (error) | |
2413 | goto out4; | |
2414 | ||
41ffe5d5 | 2415 | error = shmem_init_inodecache(); |
1da177e4 LT |
2416 | if (error) |
2417 | goto out3; | |
2418 | ||
41ffe5d5 | 2419 | error = register_filesystem(&shmem_fs_type); |
1da177e4 LT |
2420 | if (error) { |
2421 | printk(KERN_ERR "Could not register tmpfs\n"); | |
2422 | goto out2; | |
2423 | } | |
95dc112a | 2424 | |
41ffe5d5 HD |
2425 | shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER, |
2426 | shmem_fs_type.name, NULL); | |
1da177e4 LT |
2427 | if (IS_ERR(shm_mnt)) { |
2428 | error = PTR_ERR(shm_mnt); | |
2429 | printk(KERN_ERR "Could not kern_mount tmpfs\n"); | |
2430 | goto out1; | |
2431 | } | |
2432 | return 0; | |
2433 | ||
2434 | out1: | |
41ffe5d5 | 2435 | unregister_filesystem(&shmem_fs_type); |
1da177e4 | 2436 | out2: |
41ffe5d5 | 2437 | shmem_destroy_inodecache(); |
1da177e4 | 2438 | out3: |
e0bf68dd PZ |
2439 | bdi_destroy(&shmem_backing_dev_info); |
2440 | out4: | |
1da177e4 LT |
2441 | shm_mnt = ERR_PTR(error); |
2442 | return error; | |
2443 | } | |
853ac43a | 2444 | |
87946a72 DN |
2445 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR |
2446 | /** | |
41ffe5d5 | 2447 | * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file |
87946a72 | 2448 | * @inode: the inode to be searched |
41ffe5d5 | 2449 | * @index: the page offset to be searched |
87946a72 | 2450 | * @pagep: the pointer for the found page to be stored |
41ffe5d5 | 2451 | * @swapp: the pointer for the found swap entry to be stored |
87946a72 DN |
2452 | * |
2453 | * If a page is found, refcount of it is incremented. Callers should handle | |
2454 | * these refcount. | |
2455 | */ | |
41ffe5d5 HD |
2456 | void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index, |
2457 | struct page **pagep, swp_entry_t *swapp) | |
87946a72 | 2458 | { |
87946a72 | 2459 | struct shmem_inode_info *info = SHMEM_I(inode); |
41ffe5d5 HD |
2460 | struct page *page = NULL; |
2461 | swp_entry_t swap = {0}; | |
87946a72 | 2462 | |
41ffe5d5 | 2463 | if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) |
87946a72 DN |
2464 | goto out; |
2465 | ||
2466 | spin_lock(&info->lock); | |
87946a72 | 2467 | #ifdef CONFIG_SWAP |
41ffe5d5 HD |
2468 | swap = shmem_get_swap(info, index); |
2469 | if (swap.val) | |
2470 | page = find_get_page(&swapper_space, swap.val); | |
285b2c4f | 2471 | else |
87946a72 | 2472 | #endif |
41ffe5d5 | 2473 | page = find_get_page(inode->i_mapping, index); |
87946a72 DN |
2474 | spin_unlock(&info->lock); |
2475 | out: | |
2476 | *pagep = page; | |
41ffe5d5 | 2477 | *swapp = swap; |
87946a72 DN |
2478 | } |
2479 | #endif | |
2480 | ||
853ac43a MM |
2481 | #else /* !CONFIG_SHMEM */ |
2482 | ||
2483 | /* | |
2484 | * tiny-shmem: simple shmemfs and tmpfs using ramfs code | |
2485 | * | |
2486 | * This is intended for small system where the benefits of the full | |
2487 | * shmem code (swap-backed and resource-limited) are outweighed by | |
2488 | * their complexity. On systems without swap this code should be | |
2489 | * effectively equivalent, but much lighter weight. | |
2490 | */ | |
2491 | ||
2492 | #include <linux/ramfs.h> | |
2493 | ||
41ffe5d5 | 2494 | static struct file_system_type shmem_fs_type = { |
853ac43a | 2495 | .name = "tmpfs", |
3c26ff6e | 2496 | .mount = ramfs_mount, |
853ac43a MM |
2497 | .kill_sb = kill_litter_super, |
2498 | }; | |
2499 | ||
41ffe5d5 | 2500 | int __init shmem_init(void) |
853ac43a | 2501 | { |
41ffe5d5 | 2502 | BUG_ON(register_filesystem(&shmem_fs_type) != 0); |
853ac43a | 2503 | |
41ffe5d5 | 2504 | shm_mnt = kern_mount(&shmem_fs_type); |
853ac43a MM |
2505 | BUG_ON(IS_ERR(shm_mnt)); |
2506 | ||
2507 | return 0; | |
2508 | } | |
2509 | ||
41ffe5d5 | 2510 | int shmem_unuse(swp_entry_t swap, struct page *page) |
853ac43a MM |
2511 | { |
2512 | return 0; | |
2513 | } | |
2514 | ||
3f96b79a HD |
2515 | int shmem_lock(struct file *file, int lock, struct user_struct *user) |
2516 | { | |
2517 | return 0; | |
2518 | } | |
2519 | ||
41ffe5d5 | 2520 | void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) |
94c1e62d | 2521 | { |
41ffe5d5 | 2522 | truncate_inode_pages_range(inode->i_mapping, lstart, lend); |
94c1e62d HD |
2523 | } |
2524 | EXPORT_SYMBOL_GPL(shmem_truncate_range); | |
2525 | ||
87946a72 DN |
2526 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR |
2527 | /** | |
41ffe5d5 | 2528 | * mem_cgroup_get_shmem_target - find page or swap assigned to the shmem file |
87946a72 | 2529 | * @inode: the inode to be searched |
41ffe5d5 | 2530 | * @index: the page offset to be searched |
87946a72 | 2531 | * @pagep: the pointer for the found page to be stored |
41ffe5d5 | 2532 | * @swapp: the pointer for the found swap entry to be stored |
87946a72 DN |
2533 | * |
2534 | * If a page is found, refcount of it is incremented. Callers should handle | |
2535 | * these refcount. | |
2536 | */ | |
41ffe5d5 HD |
2537 | void mem_cgroup_get_shmem_target(struct inode *inode, pgoff_t index, |
2538 | struct page **pagep, swp_entry_t *swapp) | |
87946a72 DN |
2539 | { |
2540 | struct page *page = NULL; | |
2541 | ||
41ffe5d5 | 2542 | if ((index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) |
87946a72 | 2543 | goto out; |
41ffe5d5 | 2544 | page = find_get_page(inode->i_mapping, index); |
87946a72 DN |
2545 | out: |
2546 | *pagep = page; | |
41ffe5d5 | 2547 | *swapp = (swp_entry_t){0}; |
87946a72 DN |
2548 | } |
2549 | #endif | |
2550 | ||
0b0a0806 HD |
2551 | #define shmem_vm_ops generic_file_vm_ops |
2552 | #define shmem_file_operations ramfs_file_operations | |
454abafe | 2553 | #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) |
0b0a0806 HD |
2554 | #define shmem_acct_size(flags, size) 0 |
2555 | #define shmem_unacct_size(flags, size) do {} while (0) | |
853ac43a MM |
2556 | |
2557 | #endif /* CONFIG_SHMEM */ | |
2558 | ||
2559 | /* common code */ | |
1da177e4 | 2560 | |
46711810 | 2561 | /** |
1da177e4 | 2562 | * shmem_file_setup - get an unlinked file living in tmpfs |
1da177e4 LT |
2563 | * @name: name for dentry (to be seen in /proc/<pid>/maps |
2564 | * @size: size to be set for the file | |
0b0a0806 | 2565 | * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size |
1da177e4 | 2566 | */ |
168f5ac6 | 2567 | struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) |
1da177e4 LT |
2568 | { |
2569 | int error; | |
2570 | struct file *file; | |
2571 | struct inode *inode; | |
2c48b9c4 AV |
2572 | struct path path; |
2573 | struct dentry *root; | |
1da177e4 LT |
2574 | struct qstr this; |
2575 | ||
2576 | if (IS_ERR(shm_mnt)) | |
2577 | return (void *)shm_mnt; | |
2578 | ||
285b2c4f | 2579 | if (size < 0 || size > MAX_LFS_FILESIZE) |
1da177e4 LT |
2580 | return ERR_PTR(-EINVAL); |
2581 | ||
2582 | if (shmem_acct_size(flags, size)) | |
2583 | return ERR_PTR(-ENOMEM); | |
2584 | ||
2585 | error = -ENOMEM; | |
2586 | this.name = name; | |
2587 | this.len = strlen(name); | |
2588 | this.hash = 0; /* will go */ | |
2589 | root = shm_mnt->mnt_root; | |
2c48b9c4 AV |
2590 | path.dentry = d_alloc(root, &this); |
2591 | if (!path.dentry) | |
1da177e4 | 2592 | goto put_memory; |
2c48b9c4 | 2593 | path.mnt = mntget(shm_mnt); |
1da177e4 | 2594 | |
1da177e4 | 2595 | error = -ENOSPC; |
454abafe | 2596 | inode = shmem_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); |
1da177e4 | 2597 | if (!inode) |
4b42af81 | 2598 | goto put_dentry; |
1da177e4 | 2599 | |
2c48b9c4 | 2600 | d_instantiate(path.dentry, inode); |
1da177e4 LT |
2601 | inode->i_size = size; |
2602 | inode->i_nlink = 0; /* It is unlinked */ | |
853ac43a MM |
2603 | #ifndef CONFIG_MMU |
2604 | error = ramfs_nommu_expand_for_mapping(inode, size); | |
2605 | if (error) | |
4b42af81 | 2606 | goto put_dentry; |
853ac43a | 2607 | #endif |
4b42af81 AV |
2608 | |
2609 | error = -ENFILE; | |
2c48b9c4 | 2610 | file = alloc_file(&path, FMODE_WRITE | FMODE_READ, |
4b42af81 AV |
2611 | &shmem_file_operations); |
2612 | if (!file) | |
2613 | goto put_dentry; | |
2614 | ||
1da177e4 LT |
2615 | return file; |
2616 | ||
1da177e4 | 2617 | put_dentry: |
2c48b9c4 | 2618 | path_put(&path); |
1da177e4 LT |
2619 | put_memory: |
2620 | shmem_unacct_size(flags, size); | |
2621 | return ERR_PTR(error); | |
2622 | } | |
395e0ddc | 2623 | EXPORT_SYMBOL_GPL(shmem_file_setup); |
1da177e4 | 2624 | |
46711810 | 2625 | /** |
1da177e4 | 2626 | * shmem_zero_setup - setup a shared anonymous mapping |
1da177e4 LT |
2627 | * @vma: the vma to be mmapped is prepared by do_mmap_pgoff |
2628 | */ | |
2629 | int shmem_zero_setup(struct vm_area_struct *vma) | |
2630 | { | |
2631 | struct file *file; | |
2632 | loff_t size = vma->vm_end - vma->vm_start; | |
2633 | ||
2634 | file = shmem_file_setup("dev/zero", size, vma->vm_flags); | |
2635 | if (IS_ERR(file)) | |
2636 | return PTR_ERR(file); | |
2637 | ||
2638 | if (vma->vm_file) | |
2639 | fput(vma->vm_file); | |
2640 | vma->vm_file = file; | |
2641 | vma->vm_ops = &shmem_vm_ops; | |
bee4c36a | 2642 | vma->vm_flags |= VM_CAN_NONLINEAR; |
1da177e4 LT |
2643 | return 0; |
2644 | } | |
d9d90e5e HD |
2645 | |
2646 | /** | |
2647 | * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. | |
2648 | * @mapping: the page's address_space | |
2649 | * @index: the page index | |
2650 | * @gfp: the page allocator flags to use if allocating | |
2651 | * | |
2652 | * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", | |
2653 | * with any new page allocations done using the specified allocation flags. | |
2654 | * But read_cache_page_gfp() uses the ->readpage() method: which does not | |
2655 | * suit tmpfs, since it may have pages in swapcache, and needs to find those | |
2656 | * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. | |
2657 | * | |
68da9f05 HD |
2658 | * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in |
2659 | * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. | |
d9d90e5e HD |
2660 | */ |
2661 | struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, | |
2662 | pgoff_t index, gfp_t gfp) | |
2663 | { | |
68da9f05 HD |
2664 | #ifdef CONFIG_SHMEM |
2665 | struct inode *inode = mapping->host; | |
9276aad6 | 2666 | struct page *page; |
68da9f05 HD |
2667 | int error; |
2668 | ||
2669 | BUG_ON(mapping->a_ops != &shmem_aops); | |
2670 | error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL); | |
2671 | if (error) | |
2672 | page = ERR_PTR(error); | |
2673 | else | |
2674 | unlock_page(page); | |
2675 | return page; | |
2676 | #else | |
2677 | /* | |
2678 | * The tiny !SHMEM case uses ramfs without swap | |
2679 | */ | |
d9d90e5e | 2680 | return read_cache_page_gfp(mapping, index, gfp); |
68da9f05 | 2681 | #endif |
d9d90e5e HD |
2682 | } |
2683 | EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); |