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. | |
6922c0c7 HD |
9 | * Copyright (C) 2002-2011 Hugh Dickins. |
10 | * Copyright (C) 2011 Google Inc. | |
0edd73b3 | 11 | * Copyright (C) 2002-2005 VERITAS Software Corporation. |
1da177e4 LT |
12 | * Copyright (C) 2004 Andi Kleen, SuSE Labs |
13 | * | |
14 | * Extended attribute support for tmpfs: | |
15 | * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> | |
16 | * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> | |
17 | * | |
853ac43a MM |
18 | * tiny-shmem: |
19 | * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> | |
20 | * | |
1da177e4 LT |
21 | * This file is released under the GPL. |
22 | */ | |
23 | ||
853ac43a MM |
24 | #include <linux/fs.h> |
25 | #include <linux/init.h> | |
26 | #include <linux/vfs.h> | |
27 | #include <linux/mount.h> | |
250297ed | 28 | #include <linux/ramfs.h> |
caefba17 | 29 | #include <linux/pagemap.h> |
853ac43a MM |
30 | #include <linux/file.h> |
31 | #include <linux/mm.h> | |
b95f1b31 | 32 | #include <linux/export.h> |
853ac43a | 33 | #include <linux/swap.h> |
a27bb332 | 34 | #include <linux/aio.h> |
853ac43a MM |
35 | |
36 | static struct vfsmount *shm_mnt; | |
37 | ||
38 | #ifdef CONFIG_SHMEM | |
1da177e4 LT |
39 | /* |
40 | * This virtual memory filesystem is heavily based on the ramfs. It | |
41 | * extends ramfs by the ability to use swap and honor resource limits | |
42 | * which makes it a completely usable filesystem. | |
43 | */ | |
44 | ||
39f0247d | 45 | #include <linux/xattr.h> |
a5694255 | 46 | #include <linux/exportfs.h> |
1c7c474c | 47 | #include <linux/posix_acl.h> |
39f0247d | 48 | #include <linux/generic_acl.h> |
1da177e4 | 49 | #include <linux/mman.h> |
1da177e4 LT |
50 | #include <linux/string.h> |
51 | #include <linux/slab.h> | |
52 | #include <linux/backing-dev.h> | |
53 | #include <linux/shmem_fs.h> | |
1da177e4 | 54 | #include <linux/writeback.h> |
1da177e4 | 55 | #include <linux/blkdev.h> |
bda97eab | 56 | #include <linux/pagevec.h> |
41ffe5d5 | 57 | #include <linux/percpu_counter.h> |
83e4fa9c | 58 | #include <linux/falloc.h> |
708e3508 | 59 | #include <linux/splice.h> |
1da177e4 LT |
60 | #include <linux/security.h> |
61 | #include <linux/swapops.h> | |
62 | #include <linux/mempolicy.h> | |
63 | #include <linux/namei.h> | |
b00dc3ad | 64 | #include <linux/ctype.h> |
304dbdb7 | 65 | #include <linux/migrate.h> |
c1f60a5a | 66 | #include <linux/highmem.h> |
680d794b | 67 | #include <linux/seq_file.h> |
8a09920f | 68 | #include <linux/syscalls.h> |
92562927 | 69 | #include <linux/magic.h> |
8a09920f | 70 | #include <uapi/linux/memfd.h> |
304dbdb7 | 71 | |
1da177e4 | 72 | #include <asm/uaccess.h> |
1da177e4 LT |
73 | #include <asm/pgtable.h> |
74 | ||
caefba17 | 75 | #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512) |
1da177e4 LT |
76 | #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT) |
77 | ||
1da177e4 LT |
78 | /* Pretend that each entry is of this size in directory's i_size */ |
79 | #define BOGO_DIRENT_SIZE 20 | |
80 | ||
69f07ec9 HD |
81 | /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ |
82 | #define SHORT_SYMLINK_LEN 128 | |
83 | ||
1aac1400 | 84 | /* |
1ccc3ffa HD |
85 | * shmem_fallocate communicates with shmem_fault or shmem_writepage via |
86 | * inode->i_private (with i_mutex making sure that it has only one user at | |
87 | * a time): we would prefer not to enlarge the shmem inode just for that. | |
1aac1400 HD |
88 | */ |
89 | struct shmem_falloc { | |
887675c9 | 90 | wait_queue_head_t *waitq; /* faults into hole wait for punch to end */ |
1aac1400 HD |
91 | pgoff_t start; /* start of range currently being fallocated */ |
92 | pgoff_t next; /* the next page offset to be fallocated */ | |
93 | pgoff_t nr_falloced; /* how many new pages have been fallocated */ | |
94 | pgoff_t nr_unswapped; /* how often writepage refused to swap out */ | |
95 | }; | |
96 | ||
285b2c4f | 97 | /* Flag allocation requirements to shmem_getpage */ |
1da177e4 | 98 | enum sgp_type { |
1da177e4 LT |
99 | SGP_READ, /* don't exceed i_size, don't allocate page */ |
100 | SGP_CACHE, /* don't exceed i_size, may allocate page */ | |
a0ee5ec5 | 101 | SGP_DIRTY, /* like SGP_CACHE, but set new page dirty */ |
1635f6a7 HD |
102 | SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */ |
103 | SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */ | |
1da177e4 LT |
104 | }; |
105 | ||
b76db735 | 106 | #ifdef CONFIG_TMPFS |
680d794b | 107 | static unsigned long shmem_default_max_blocks(void) |
108 | { | |
109 | return totalram_pages / 2; | |
110 | } | |
111 | ||
112 | static unsigned long shmem_default_max_inodes(void) | |
113 | { | |
114 | return min(totalram_pages - totalhigh_pages, totalram_pages / 2); | |
115 | } | |
b76db735 | 116 | #endif |
680d794b | 117 | |
bde05d1c HD |
118 | static bool shmem_should_replace_page(struct page *page, gfp_t gfp); |
119 | static int shmem_replace_page(struct page **pagep, gfp_t gfp, | |
120 | struct shmem_inode_info *info, pgoff_t index); | |
68da9f05 HD |
121 | static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, |
122 | struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type); | |
123 | ||
124 | static inline int shmem_getpage(struct inode *inode, pgoff_t index, | |
125 | struct page **pagep, enum sgp_type sgp, int *fault_type) | |
126 | { | |
127 | return shmem_getpage_gfp(inode, index, pagep, sgp, | |
128 | mapping_gfp_mask(inode->i_mapping), fault_type); | |
129 | } | |
1da177e4 | 130 | |
1da177e4 LT |
131 | static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) |
132 | { | |
133 | return sb->s_fs_info; | |
134 | } | |
135 | ||
136 | /* | |
137 | * shmem_file_setup pre-accounts the whole fixed size of a VM object, | |
138 | * for shared memory and for shared anonymous (/dev/zero) mappings | |
139 | * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), | |
140 | * consistent with the pre-accounting of private mappings ... | |
141 | */ | |
142 | static inline int shmem_acct_size(unsigned long flags, loff_t size) | |
143 | { | |
0b0a0806 | 144 | return (flags & VM_NORESERVE) ? |
191c5424 | 145 | 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); |
1da177e4 LT |
146 | } |
147 | ||
148 | static inline void shmem_unacct_size(unsigned long flags, loff_t size) | |
149 | { | |
0b0a0806 | 150 | if (!(flags & VM_NORESERVE)) |
1da177e4 LT |
151 | vm_unacct_memory(VM_ACCT(size)); |
152 | } | |
153 | ||
154 | /* | |
155 | * ... whereas tmpfs objects are accounted incrementally as | |
156 | * pages are allocated, in order to allow huge sparse files. | |
157 | * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, | |
158 | * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. | |
159 | */ | |
160 | static inline int shmem_acct_block(unsigned long flags) | |
161 | { | |
0b0a0806 | 162 | return (flags & VM_NORESERVE) ? |
191c5424 | 163 | security_vm_enough_memory_mm(current->mm, VM_ACCT(PAGE_CACHE_SIZE)) : 0; |
1da177e4 LT |
164 | } |
165 | ||
166 | static inline void shmem_unacct_blocks(unsigned long flags, long pages) | |
167 | { | |
0b0a0806 | 168 | if (flags & VM_NORESERVE) |
1da177e4 LT |
169 | vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE)); |
170 | } | |
171 | ||
759b9775 | 172 | static const struct super_operations shmem_ops; |
f5e54d6e | 173 | static const struct address_space_operations shmem_aops; |
15ad7cdc | 174 | static const struct file_operations shmem_file_operations; |
92e1d5be AV |
175 | static const struct inode_operations shmem_inode_operations; |
176 | static const struct inode_operations shmem_dir_inode_operations; | |
177 | static const struct inode_operations shmem_special_inode_operations; | |
f0f37e2f | 178 | static const struct vm_operations_struct shmem_vm_ops; |
1da177e4 | 179 | |
6c231b7b | 180 | static struct backing_dev_info shmem_backing_dev_info __read_mostly = { |
1da177e4 | 181 | .ra_pages = 0, /* No readahead */ |
4f98a2fe | 182 | .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK | BDI_CAP_SWAP_BACKED, |
1da177e4 LT |
183 | }; |
184 | ||
185 | static LIST_HEAD(shmem_swaplist); | |
cb5f7b9a | 186 | static DEFINE_MUTEX(shmem_swaplist_mutex); |
1da177e4 | 187 | |
5b04c689 PE |
188 | static int shmem_reserve_inode(struct super_block *sb) |
189 | { | |
190 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); | |
191 | if (sbinfo->max_inodes) { | |
192 | spin_lock(&sbinfo->stat_lock); | |
193 | if (!sbinfo->free_inodes) { | |
194 | spin_unlock(&sbinfo->stat_lock); | |
195 | return -ENOSPC; | |
196 | } | |
197 | sbinfo->free_inodes--; | |
198 | spin_unlock(&sbinfo->stat_lock); | |
199 | } | |
200 | return 0; | |
201 | } | |
202 | ||
203 | static void shmem_free_inode(struct super_block *sb) | |
204 | { | |
205 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); | |
206 | if (sbinfo->max_inodes) { | |
207 | spin_lock(&sbinfo->stat_lock); | |
208 | sbinfo->free_inodes++; | |
209 | spin_unlock(&sbinfo->stat_lock); | |
210 | } | |
211 | } | |
212 | ||
46711810 | 213 | /** |
41ffe5d5 | 214 | * shmem_recalc_inode - recalculate the block usage of an inode |
1da177e4 LT |
215 | * @inode: inode to recalc |
216 | * | |
217 | * We have to calculate the free blocks since the mm can drop | |
218 | * undirtied hole pages behind our back. | |
219 | * | |
220 | * But normally info->alloced == inode->i_mapping->nrpages + info->swapped | |
221 | * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) | |
222 | * | |
223 | * It has to be called with the spinlock held. | |
224 | */ | |
225 | static void shmem_recalc_inode(struct inode *inode) | |
226 | { | |
227 | struct shmem_inode_info *info = SHMEM_I(inode); | |
228 | long freed; | |
229 | ||
230 | freed = info->alloced - info->swapped - inode->i_mapping->nrpages; | |
231 | if (freed > 0) { | |
54af6042 HD |
232 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
233 | if (sbinfo->max_blocks) | |
234 | percpu_counter_add(&sbinfo->used_blocks, -freed); | |
1da177e4 | 235 | info->alloced -= freed; |
54af6042 | 236 | inode->i_blocks -= freed * BLOCKS_PER_PAGE; |
1da177e4 | 237 | shmem_unacct_blocks(info->flags, freed); |
1da177e4 LT |
238 | } |
239 | } | |
240 | ||
7a5d0fbb HD |
241 | /* |
242 | * Replace item expected in radix tree by a new item, while holding tree lock. | |
243 | */ | |
244 | static int shmem_radix_tree_replace(struct address_space *mapping, | |
245 | pgoff_t index, void *expected, void *replacement) | |
246 | { | |
247 | void **pslot; | |
248 | void *item = NULL; | |
249 | ||
250 | VM_BUG_ON(!expected); | |
251 | pslot = radix_tree_lookup_slot(&mapping->page_tree, index); | |
252 | if (pslot) | |
253 | item = radix_tree_deref_slot_protected(pslot, | |
254 | &mapping->tree_lock); | |
255 | if (item != expected) | |
256 | return -ENOENT; | |
257 | if (replacement) | |
258 | radix_tree_replace_slot(pslot, replacement); | |
259 | else | |
260 | radix_tree_delete(&mapping->page_tree, index); | |
261 | return 0; | |
262 | } | |
263 | ||
d1899228 HD |
264 | /* |
265 | * Sometimes, before we decide whether to proceed or to fail, we must check | |
266 | * that an entry was not already brought back from swap by a racing thread. | |
267 | * | |
268 | * Checking page is not enough: by the time a SwapCache page is locked, it | |
269 | * might be reused, and again be SwapCache, using the same swap as before. | |
270 | */ | |
271 | static bool shmem_confirm_swap(struct address_space *mapping, | |
272 | pgoff_t index, swp_entry_t swap) | |
273 | { | |
274 | void *item; | |
275 | ||
276 | rcu_read_lock(); | |
277 | item = radix_tree_lookup(&mapping->page_tree, index); | |
278 | rcu_read_unlock(); | |
279 | return item == swp_to_radix_entry(swap); | |
280 | } | |
281 | ||
46f65ec1 HD |
282 | /* |
283 | * Like add_to_page_cache_locked, but error if expected item has gone. | |
284 | */ | |
285 | static int shmem_add_to_page_cache(struct page *page, | |
286 | struct address_space *mapping, | |
287 | pgoff_t index, gfp_t gfp, void *expected) | |
288 | { | |
b065b432 | 289 | int error; |
46f65ec1 HD |
290 | |
291 | VM_BUG_ON(!PageLocked(page)); | |
292 | VM_BUG_ON(!PageSwapBacked(page)); | |
293 | ||
b065b432 HD |
294 | page_cache_get(page); |
295 | page->mapping = mapping; | |
296 | page->index = index; | |
297 | ||
298 | spin_lock_irq(&mapping->tree_lock); | |
46f65ec1 | 299 | if (!expected) |
b065b432 HD |
300 | error = radix_tree_insert(&mapping->page_tree, index, page); |
301 | else | |
302 | error = shmem_radix_tree_replace(mapping, index, expected, | |
303 | page); | |
46f65ec1 | 304 | if (!error) { |
b065b432 HD |
305 | mapping->nrpages++; |
306 | __inc_zone_page_state(page, NR_FILE_PAGES); | |
307 | __inc_zone_page_state(page, NR_SHMEM); | |
308 | spin_unlock_irq(&mapping->tree_lock); | |
309 | } else { | |
310 | page->mapping = NULL; | |
311 | spin_unlock_irq(&mapping->tree_lock); | |
312 | page_cache_release(page); | |
46f65ec1 | 313 | } |
46f65ec1 HD |
314 | return error; |
315 | } | |
316 | ||
6922c0c7 HD |
317 | /* |
318 | * Like delete_from_page_cache, but substitutes swap for page. | |
319 | */ | |
320 | static void shmem_delete_from_page_cache(struct page *page, void *radswap) | |
321 | { | |
322 | struct address_space *mapping = page->mapping; | |
323 | int error; | |
324 | ||
325 | spin_lock_irq(&mapping->tree_lock); | |
326 | error = shmem_radix_tree_replace(mapping, page->index, page, radswap); | |
327 | page->mapping = NULL; | |
328 | mapping->nrpages--; | |
329 | __dec_zone_page_state(page, NR_FILE_PAGES); | |
330 | __dec_zone_page_state(page, NR_SHMEM); | |
331 | spin_unlock_irq(&mapping->tree_lock); | |
332 | page_cache_release(page); | |
333 | BUG_ON(error); | |
334 | } | |
335 | ||
7a5d0fbb HD |
336 | /* |
337 | * Like find_get_pages, but collecting swap entries as well as pages. | |
338 | */ | |
339 | static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping, | |
340 | pgoff_t start, unsigned int nr_pages, | |
341 | struct page **pages, pgoff_t *indices) | |
342 | { | |
860f2759 JW |
343 | void **slot; |
344 | unsigned int ret = 0; | |
345 | struct radix_tree_iter iter; | |
346 | ||
347 | if (!nr_pages) | |
348 | return 0; | |
7a5d0fbb HD |
349 | |
350 | rcu_read_lock(); | |
351 | restart: | |
860f2759 | 352 | radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) { |
7a5d0fbb HD |
353 | struct page *page; |
354 | repeat: | |
860f2759 | 355 | page = radix_tree_deref_slot(slot); |
7a5d0fbb HD |
356 | if (unlikely(!page)) |
357 | continue; | |
358 | if (radix_tree_exception(page)) { | |
8079b1c8 HD |
359 | if (radix_tree_deref_retry(page)) |
360 | goto restart; | |
361 | /* | |
362 | * Otherwise, we must be storing a swap entry | |
363 | * here as an exceptional entry: so return it | |
364 | * without attempting to raise page count. | |
365 | */ | |
366 | goto export; | |
7a5d0fbb HD |
367 | } |
368 | if (!page_cache_get_speculative(page)) | |
369 | goto repeat; | |
370 | ||
371 | /* Has the page moved? */ | |
860f2759 | 372 | if (unlikely(page != *slot)) { |
7a5d0fbb HD |
373 | page_cache_release(page); |
374 | goto repeat; | |
375 | } | |
376 | export: | |
860f2759 | 377 | indices[ret] = iter.index; |
7a5d0fbb | 378 | pages[ret] = page; |
860f2759 JW |
379 | if (++ret == nr_pages) |
380 | break; | |
7a5d0fbb | 381 | } |
7a5d0fbb HD |
382 | rcu_read_unlock(); |
383 | return ret; | |
384 | } | |
385 | ||
386 | /* | |
387 | * Remove swap entry from radix tree, free the swap and its page cache. | |
388 | */ | |
389 | static int shmem_free_swap(struct address_space *mapping, | |
390 | pgoff_t index, void *radswap) | |
391 | { | |
392 | int error; | |
393 | ||
394 | spin_lock_irq(&mapping->tree_lock); | |
395 | error = shmem_radix_tree_replace(mapping, index, radswap, NULL); | |
396 | spin_unlock_irq(&mapping->tree_lock); | |
397 | if (!error) | |
398 | free_swap_and_cache(radix_to_swp_entry(radswap)); | |
399 | return error; | |
400 | } | |
401 | ||
402 | /* | |
403 | * Pagevec may contain swap entries, so shuffle up pages before releasing. | |
404 | */ | |
24513264 | 405 | static void shmem_deswap_pagevec(struct pagevec *pvec) |
7a5d0fbb HD |
406 | { |
407 | int i, j; | |
408 | ||
409 | for (i = 0, j = 0; i < pagevec_count(pvec); i++) { | |
410 | struct page *page = pvec->pages[i]; | |
411 | if (!radix_tree_exceptional_entry(page)) | |
412 | pvec->pages[j++] = page; | |
413 | } | |
414 | pvec->nr = j; | |
24513264 HD |
415 | } |
416 | ||
417 | /* | |
418 | * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. | |
419 | */ | |
420 | void shmem_unlock_mapping(struct address_space *mapping) | |
421 | { | |
422 | struct pagevec pvec; | |
423 | pgoff_t indices[PAGEVEC_SIZE]; | |
424 | pgoff_t index = 0; | |
425 | ||
426 | pagevec_init(&pvec, 0); | |
427 | /* | |
428 | * Minor point, but we might as well stop if someone else SHM_LOCKs it. | |
429 | */ | |
430 | while (!mapping_unevictable(mapping)) { | |
431 | /* | |
432 | * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it | |
433 | * has finished, if it hits a row of PAGEVEC_SIZE swap entries. | |
434 | */ | |
435 | pvec.nr = shmem_find_get_pages_and_swap(mapping, index, | |
436 | PAGEVEC_SIZE, pvec.pages, indices); | |
437 | if (!pvec.nr) | |
438 | break; | |
439 | index = indices[pvec.nr - 1] + 1; | |
440 | shmem_deswap_pagevec(&pvec); | |
441 | check_move_unevictable_pages(pvec.pages, pvec.nr); | |
442 | pagevec_release(&pvec); | |
443 | cond_resched(); | |
444 | } | |
7a5d0fbb HD |
445 | } |
446 | ||
447 | /* | |
448 | * Remove range of pages and swap entries from radix tree, and free them. | |
1635f6a7 | 449 | * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. |
7a5d0fbb | 450 | */ |
1635f6a7 HD |
451 | static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, |
452 | bool unfalloc) | |
1da177e4 | 453 | { |
285b2c4f | 454 | struct address_space *mapping = inode->i_mapping; |
1da177e4 | 455 | struct shmem_inode_info *info = SHMEM_I(inode); |
285b2c4f | 456 | pgoff_t start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
83e4fa9c HD |
457 | pgoff_t end = (lend + 1) >> PAGE_CACHE_SHIFT; |
458 | unsigned int partial_start = lstart & (PAGE_CACHE_SIZE - 1); | |
459 | unsigned int partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1); | |
bda97eab | 460 | struct pagevec pvec; |
7a5d0fbb HD |
461 | pgoff_t indices[PAGEVEC_SIZE]; |
462 | long nr_swaps_freed = 0; | |
285b2c4f | 463 | pgoff_t index; |
bda97eab HD |
464 | int i; |
465 | ||
83e4fa9c HD |
466 | if (lend == -1) |
467 | end = -1; /* unsigned, so actually very big */ | |
bda97eab HD |
468 | |
469 | pagevec_init(&pvec, 0); | |
470 | index = start; | |
83e4fa9c | 471 | while (index < end) { |
7a5d0fbb | 472 | pvec.nr = shmem_find_get_pages_and_swap(mapping, index, |
83e4fa9c | 473 | min(end - index, (pgoff_t)PAGEVEC_SIZE), |
7a5d0fbb HD |
474 | pvec.pages, indices); |
475 | if (!pvec.nr) | |
476 | break; | |
bda97eab HD |
477 | mem_cgroup_uncharge_start(); |
478 | for (i = 0; i < pagevec_count(&pvec); i++) { | |
479 | struct page *page = pvec.pages[i]; | |
480 | ||
7a5d0fbb | 481 | index = indices[i]; |
83e4fa9c | 482 | if (index >= end) |
bda97eab HD |
483 | break; |
484 | ||
7a5d0fbb | 485 | if (radix_tree_exceptional_entry(page)) { |
1635f6a7 HD |
486 | if (unfalloc) |
487 | continue; | |
3c2a0909 S |
488 | if (shmem_free_swap(mapping, index, page)) { |
489 | /* Swap was replaced by page: retry */ | |
490 | index--; | |
491 | break; | |
492 | } | |
493 | nr_swaps_freed++; | |
bda97eab | 494 | continue; |
7a5d0fbb HD |
495 | } |
496 | ||
497 | if (!trylock_page(page)) | |
bda97eab | 498 | continue; |
1635f6a7 HD |
499 | if (!unfalloc || !PageUptodate(page)) { |
500 | if (page->mapping == mapping) { | |
501 | VM_BUG_ON(PageWriteback(page)); | |
502 | truncate_inode_page(mapping, page); | |
3c2a0909 S |
503 | } else { |
504 | /* Page was replaced by swap: retry */ | |
505 | unlock_page(page); | |
506 | index--; | |
507 | break; | |
1635f6a7 | 508 | } |
bda97eab | 509 | } |
bda97eab HD |
510 | unlock_page(page); |
511 | } | |
24513264 HD |
512 | shmem_deswap_pagevec(&pvec); |
513 | pagevec_release(&pvec); | |
bda97eab HD |
514 | mem_cgroup_uncharge_end(); |
515 | cond_resched(); | |
516 | index++; | |
517 | } | |
1da177e4 | 518 | |
83e4fa9c | 519 | if (partial_start) { |
bda97eab HD |
520 | struct page *page = NULL; |
521 | shmem_getpage(inode, start - 1, &page, SGP_READ, NULL); | |
522 | if (page) { | |
83e4fa9c HD |
523 | unsigned int top = PAGE_CACHE_SIZE; |
524 | if (start > end) { | |
525 | top = partial_end; | |
526 | partial_end = 0; | |
527 | } | |
528 | zero_user_segment(page, partial_start, top); | |
529 | set_page_dirty(page); | |
530 | unlock_page(page); | |
531 | page_cache_release(page); | |
532 | } | |
533 | } | |
534 | if (partial_end) { | |
535 | struct page *page = NULL; | |
536 | shmem_getpage(inode, end, &page, SGP_READ, NULL); | |
537 | if (page) { | |
538 | zero_user_segment(page, 0, partial_end); | |
bda97eab HD |
539 | set_page_dirty(page); |
540 | unlock_page(page); | |
541 | page_cache_release(page); | |
542 | } | |
543 | } | |
83e4fa9c HD |
544 | if (start >= end) |
545 | return; | |
bda97eab HD |
546 | |
547 | index = start; | |
7dc7fb43 | 548 | while (index < end) { |
bda97eab | 549 | cond_resched(); |
7a5d0fbb | 550 | pvec.nr = shmem_find_get_pages_and_swap(mapping, index, |
83e4fa9c | 551 | min(end - index, (pgoff_t)PAGEVEC_SIZE), |
7a5d0fbb HD |
552 | pvec.pages, indices); |
553 | if (!pvec.nr) { | |
7dc7fb43 HD |
554 | /* If all gone or hole-punch or unfalloc, we're done */ |
555 | if (index == start || end != -1) | |
bda97eab | 556 | break; |
7dc7fb43 | 557 | /* But if truncating, restart to make sure all gone */ |
bda97eab HD |
558 | index = start; |
559 | continue; | |
560 | } | |
bda97eab HD |
561 | mem_cgroup_uncharge_start(); |
562 | for (i = 0; i < pagevec_count(&pvec); i++) { | |
563 | struct page *page = pvec.pages[i]; | |
564 | ||
7a5d0fbb | 565 | index = indices[i]; |
83e4fa9c | 566 | if (index >= end) |
bda97eab HD |
567 | break; |
568 | ||
7a5d0fbb | 569 | if (radix_tree_exceptional_entry(page)) { |
1635f6a7 HD |
570 | if (unfalloc) |
571 | continue; | |
7dc7fb43 HD |
572 | if (shmem_free_swap(mapping, index, page)) { |
573 | /* Swap was replaced by page: retry */ | |
574 | index--; | |
575 | break; | |
576 | } | |
577 | nr_swaps_freed++; | |
7a5d0fbb HD |
578 | continue; |
579 | } | |
580 | ||
bda97eab | 581 | lock_page(page); |
1635f6a7 HD |
582 | if (!unfalloc || !PageUptodate(page)) { |
583 | if (page->mapping == mapping) { | |
584 | VM_BUG_ON(PageWriteback(page)); | |
585 | truncate_inode_page(mapping, page); | |
7dc7fb43 HD |
586 | } else { |
587 | /* Page was replaced by swap: retry */ | |
588 | unlock_page(page); | |
589 | index--; | |
590 | break; | |
1635f6a7 | 591 | } |
7a5d0fbb | 592 | } |
bda97eab HD |
593 | unlock_page(page); |
594 | } | |
24513264 HD |
595 | shmem_deswap_pagevec(&pvec); |
596 | pagevec_release(&pvec); | |
bda97eab HD |
597 | mem_cgroup_uncharge_end(); |
598 | index++; | |
599 | } | |
94c1e62d | 600 | |
1da177e4 | 601 | spin_lock(&info->lock); |
7a5d0fbb | 602 | info->swapped -= nr_swaps_freed; |
1da177e4 LT |
603 | shmem_recalc_inode(inode); |
604 | spin_unlock(&info->lock); | |
1635f6a7 | 605 | } |
1da177e4 | 606 | |
1635f6a7 HD |
607 | void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) |
608 | { | |
609 | shmem_undo_range(inode, lstart, lend, false); | |
285b2c4f | 610 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; |
1da177e4 | 611 | } |
94c1e62d | 612 | EXPORT_SYMBOL_GPL(shmem_truncate_range); |
1da177e4 | 613 | |
94c1e62d | 614 | static int shmem_setattr(struct dentry *dentry, struct iattr *attr) |
1da177e4 LT |
615 | { |
616 | struct inode *inode = dentry->d_inode; | |
1da177e4 LT |
617 | int error; |
618 | ||
db78b877 CH |
619 | error = inode_change_ok(inode, attr); |
620 | if (error) | |
621 | return error; | |
622 | ||
94c1e62d HD |
623 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
624 | loff_t oldsize = inode->i_size; | |
625 | loff_t newsize = attr->ia_size; | |
3889e6e7 | 626 | |
94c1e62d HD |
627 | if (newsize != oldsize) { |
628 | i_size_write(inode, newsize); | |
629 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; | |
630 | } | |
631 | if (newsize < oldsize) { | |
632 | loff_t holebegin = round_up(newsize, PAGE_SIZE); | |
633 | unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); | |
634 | shmem_truncate_range(inode, newsize, (loff_t)-1); | |
635 | /* unmap again to remove racily COWed private pages */ | |
636 | unmap_mapping_range(inode->i_mapping, holebegin, 0, 1); | |
637 | } | |
1da177e4 LT |
638 | } |
639 | ||
db78b877 | 640 | setattr_copy(inode, attr); |
39f0247d | 641 | #ifdef CONFIG_TMPFS_POSIX_ACL |
db78b877 | 642 | if (attr->ia_valid & ATTR_MODE) |
1c7c474c | 643 | error = generic_acl_chmod(inode); |
39f0247d | 644 | #endif |
1da177e4 LT |
645 | return error; |
646 | } | |
647 | ||
1f895f75 | 648 | static void shmem_evict_inode(struct inode *inode) |
1da177e4 | 649 | { |
1da177e4 LT |
650 | struct shmem_inode_info *info = SHMEM_I(inode); |
651 | ||
3889e6e7 | 652 | if (inode->i_mapping->a_ops == &shmem_aops) { |
1da177e4 LT |
653 | shmem_unacct_size(info->flags, inode->i_size); |
654 | inode->i_size = 0; | |
3889e6e7 | 655 | shmem_truncate_range(inode, 0, (loff_t)-1); |
1da177e4 | 656 | if (!list_empty(&info->swaplist)) { |
cb5f7b9a | 657 | mutex_lock(&shmem_swaplist_mutex); |
1da177e4 | 658 | list_del_init(&info->swaplist); |
cb5f7b9a | 659 | mutex_unlock(&shmem_swaplist_mutex); |
1da177e4 | 660 | } |
69f07ec9 HD |
661 | } else |
662 | kfree(info->symlink); | |
b09e0fa4 | 663 | |
38f38657 | 664 | simple_xattrs_free(&info->xattrs); |
0f3c42f5 | 665 | WARN_ON(inode->i_blocks); |
5b04c689 | 666 | shmem_free_inode(inode->i_sb); |
dbd5768f | 667 | clear_inode(inode); |
1da177e4 LT |
668 | } |
669 | ||
46f65ec1 HD |
670 | /* |
671 | * If swap found in inode, free it and move page from swapcache to filecache. | |
672 | */ | |
41ffe5d5 | 673 | static int shmem_unuse_inode(struct shmem_inode_info *info, |
bde05d1c | 674 | swp_entry_t swap, struct page **pagep) |
1da177e4 | 675 | { |
285b2c4f | 676 | struct address_space *mapping = info->vfs_inode.i_mapping; |
46f65ec1 | 677 | void *radswap; |
41ffe5d5 | 678 | pgoff_t index; |
bde05d1c HD |
679 | gfp_t gfp; |
680 | int error = 0; | |
1da177e4 | 681 | |
46f65ec1 | 682 | radswap = swp_to_radix_entry(swap); |
e504f3fd | 683 | index = radix_tree_locate_item(&mapping->page_tree, radswap); |
46f65ec1 | 684 | if (index == -1) |
285b2c4f | 685 | return 0; |
2e0e26c7 | 686 | |
1b1b32f2 HD |
687 | /* |
688 | * Move _head_ to start search for next from here. | |
1f895f75 | 689 | * But be careful: shmem_evict_inode checks list_empty without taking |
1b1b32f2 | 690 | * mutex, and there's an instant in list_move_tail when info->swaplist |
285b2c4f | 691 | * would appear empty, if it were the only one on shmem_swaplist. |
1b1b32f2 HD |
692 | */ |
693 | if (shmem_swaplist.next != &info->swaplist) | |
694 | list_move_tail(&shmem_swaplist, &info->swaplist); | |
2e0e26c7 | 695 | |
bde05d1c HD |
696 | gfp = mapping_gfp_mask(mapping); |
697 | if (shmem_should_replace_page(*pagep, gfp)) { | |
698 | mutex_unlock(&shmem_swaplist_mutex); | |
699 | error = shmem_replace_page(pagep, gfp, info, index); | |
700 | mutex_lock(&shmem_swaplist_mutex); | |
701 | /* | |
702 | * We needed to drop mutex to make that restrictive page | |
0142ef6c HD |
703 | * allocation, but the inode might have been freed while we |
704 | * dropped it: although a racing shmem_evict_inode() cannot | |
705 | * complete without emptying the radix_tree, our page lock | |
706 | * on this swapcache page is not enough to prevent that - | |
707 | * free_swap_and_cache() of our swap entry will only | |
708 | * trylock_page(), removing swap from radix_tree whatever. | |
709 | * | |
710 | * We must not proceed to shmem_add_to_page_cache() if the | |
711 | * inode has been freed, but of course we cannot rely on | |
712 | * inode or mapping or info to check that. However, we can | |
713 | * safely check if our swap entry is still in use (and here | |
714 | * it can't have got reused for another page): if it's still | |
715 | * in use, then the inode cannot have been freed yet, and we | |
716 | * can safely proceed (if it's no longer in use, that tells | |
717 | * nothing about the inode, but we don't need to unuse swap). | |
bde05d1c HD |
718 | */ |
719 | if (!page_swapcount(*pagep)) | |
720 | error = -ENOENT; | |
721 | } | |
722 | ||
d13d1443 | 723 | /* |
778dd893 HD |
724 | * We rely on shmem_swaplist_mutex, not only to protect the swaplist, |
725 | * but also to hold up shmem_evict_inode(): so inode cannot be freed | |
726 | * beneath us (pagelock doesn't help until the page is in pagecache). | |
d13d1443 | 727 | */ |
bde05d1c HD |
728 | if (!error) |
729 | error = shmem_add_to_page_cache(*pagep, mapping, index, | |
46f65ec1 | 730 | GFP_NOWAIT, radswap); |
48f170fb | 731 | if (error != -ENOMEM) { |
46f65ec1 HD |
732 | /* |
733 | * Truncation and eviction use free_swap_and_cache(), which | |
734 | * only does trylock page: if we raced, best clean up here. | |
735 | */ | |
bde05d1c HD |
736 | delete_from_swap_cache(*pagep); |
737 | set_page_dirty(*pagep); | |
46f65ec1 HD |
738 | if (!error) { |
739 | spin_lock(&info->lock); | |
740 | info->swapped--; | |
741 | spin_unlock(&info->lock); | |
742 | swap_free(swap); | |
743 | } | |
2e0e26c7 | 744 | error = 1; /* not an error, but entry was found */ |
1da177e4 | 745 | } |
2e0e26c7 | 746 | return error; |
1da177e4 LT |
747 | } |
748 | ||
749 | /* | |
46f65ec1 | 750 | * Search through swapped inodes to find and replace swap by page. |
1da177e4 | 751 | */ |
41ffe5d5 | 752 | int shmem_unuse(swp_entry_t swap, struct page *page) |
1da177e4 | 753 | { |
41ffe5d5 | 754 | struct list_head *this, *next; |
1da177e4 LT |
755 | struct shmem_inode_info *info; |
756 | int found = 0; | |
bde05d1c HD |
757 | int error = 0; |
758 | ||
759 | /* | |
760 | * There's a faint possibility that swap page was replaced before | |
0142ef6c | 761 | * caller locked it: caller will come back later with the right page. |
bde05d1c | 762 | */ |
0142ef6c | 763 | if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val)) |
bde05d1c | 764 | goto out; |
778dd893 HD |
765 | |
766 | /* | |
767 | * Charge page using GFP_KERNEL while we can wait, before taking | |
768 | * the shmem_swaplist_mutex which might hold up shmem_writepage(). | |
769 | * Charged back to the user (not to caller) when swap account is used. | |
778dd893 HD |
770 | */ |
771 | error = mem_cgroup_cache_charge(page, current->mm, GFP_KERNEL); | |
772 | if (error) | |
773 | goto out; | |
46f65ec1 | 774 | /* No radix_tree_preload: swap entry keeps a place for page in tree */ |
1da177e4 | 775 | |
cb5f7b9a | 776 | mutex_lock(&shmem_swaplist_mutex); |
41ffe5d5 HD |
777 | list_for_each_safe(this, next, &shmem_swaplist) { |
778 | info = list_entry(this, struct shmem_inode_info, swaplist); | |
285b2c4f | 779 | if (info->swapped) |
bde05d1c | 780 | found = shmem_unuse_inode(info, swap, &page); |
6922c0c7 HD |
781 | else |
782 | list_del_init(&info->swaplist); | |
cb5f7b9a | 783 | cond_resched(); |
2e0e26c7 | 784 | if (found) |
778dd893 | 785 | break; |
1da177e4 | 786 | } |
cb5f7b9a | 787 | mutex_unlock(&shmem_swaplist_mutex); |
778dd893 | 788 | |
778dd893 HD |
789 | if (found < 0) |
790 | error = found; | |
791 | out: | |
aaa46865 HD |
792 | unlock_page(page); |
793 | page_cache_release(page); | |
778dd893 | 794 | return error; |
1da177e4 LT |
795 | } |
796 | ||
797 | /* | |
798 | * Move the page from the page cache to the swap cache. | |
799 | */ | |
800 | static int shmem_writepage(struct page *page, struct writeback_control *wbc) | |
801 | { | |
802 | struct shmem_inode_info *info; | |
1da177e4 | 803 | struct address_space *mapping; |
1da177e4 | 804 | struct inode *inode; |
6922c0c7 HD |
805 | swp_entry_t swap; |
806 | pgoff_t index; | |
1da177e4 LT |
807 | |
808 | BUG_ON(!PageLocked(page)); | |
1da177e4 LT |
809 | mapping = page->mapping; |
810 | index = page->index; | |
811 | inode = mapping->host; | |
812 | info = SHMEM_I(inode); | |
813 | if (info->flags & VM_LOCKED) | |
814 | goto redirty; | |
d9fe526a | 815 | if (!total_swap_pages) |
1da177e4 LT |
816 | goto redirty; |
817 | ||
d9fe526a HD |
818 | /* |
819 | * shmem_backing_dev_info's capabilities prevent regular writeback or | |
820 | * sync from ever calling shmem_writepage; but a stacking filesystem | |
48f170fb | 821 | * might use ->writepage of its underlying filesystem, in which case |
d9fe526a | 822 | * tmpfs should write out to swap only in response to memory pressure, |
48f170fb | 823 | * and not for the writeback threads or sync. |
d9fe526a | 824 | */ |
48f170fb HD |
825 | if (!wbc->for_reclaim) { |
826 | WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ | |
827 | goto redirty; | |
828 | } | |
1635f6a7 HD |
829 | |
830 | /* | |
831 | * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC | |
832 | * value into swapfile.c, the only way we can correctly account for a | |
833 | * fallocated page arriving here is now to initialize it and write it. | |
1aac1400 HD |
834 | * |
835 | * That's okay for a page already fallocated earlier, but if we have | |
836 | * not yet completed the fallocation, then (a) we want to keep track | |
837 | * of this page in case we have to undo it, and (b) it may not be a | |
838 | * good idea to continue anyway, once we're pushing into swap. So | |
839 | * reactivate the page, and let shmem_fallocate() quit when too many. | |
1635f6a7 HD |
840 | */ |
841 | if (!PageUptodate(page)) { | |
1aac1400 HD |
842 | if (inode->i_private) { |
843 | struct shmem_falloc *shmem_falloc; | |
844 | spin_lock(&inode->i_lock); | |
845 | shmem_falloc = inode->i_private; | |
846 | if (shmem_falloc && | |
887675c9 | 847 | !shmem_falloc->waitq && |
1aac1400 HD |
848 | index >= shmem_falloc->start && |
849 | index < shmem_falloc->next) | |
850 | shmem_falloc->nr_unswapped++; | |
851 | else | |
852 | shmem_falloc = NULL; | |
853 | spin_unlock(&inode->i_lock); | |
854 | if (shmem_falloc) | |
855 | goto redirty; | |
856 | } | |
1635f6a7 HD |
857 | clear_highpage(page); |
858 | flush_dcache_page(page); | |
859 | SetPageUptodate(page); | |
860 | } | |
861 | ||
48f170fb HD |
862 | swap = get_swap_page(); |
863 | if (!swap.val) | |
864 | goto redirty; | |
d9fe526a | 865 | |
b1dea800 HD |
866 | /* |
867 | * Add inode to shmem_unuse()'s list of swapped-out inodes, | |
6922c0c7 HD |
868 | * if it's not already there. Do it now before the page is |
869 | * moved to swap cache, when its pagelock no longer protects | |
b1dea800 | 870 | * the inode from eviction. But don't unlock the mutex until |
6922c0c7 HD |
871 | * we've incremented swapped, because shmem_unuse_inode() will |
872 | * prune a !swapped inode from the swaplist under this mutex. | |
b1dea800 | 873 | */ |
48f170fb HD |
874 | mutex_lock(&shmem_swaplist_mutex); |
875 | if (list_empty(&info->swaplist)) | |
876 | list_add_tail(&info->swaplist, &shmem_swaplist); | |
b1dea800 | 877 | |
48f170fb | 878 | if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { |
aaa46865 | 879 | swap_shmem_alloc(swap); |
6922c0c7 HD |
880 | shmem_delete_from_page_cache(page, swp_to_radix_entry(swap)); |
881 | ||
882 | spin_lock(&info->lock); | |
883 | info->swapped++; | |
884 | shmem_recalc_inode(inode); | |
826267cf | 885 | spin_unlock(&info->lock); |
6922c0c7 HD |
886 | |
887 | mutex_unlock(&shmem_swaplist_mutex); | |
d9fe526a | 888 | BUG_ON(page_mapped(page)); |
9fab5619 | 889 | swap_writepage(page, wbc); |
1da177e4 LT |
890 | return 0; |
891 | } | |
892 | ||
6922c0c7 | 893 | mutex_unlock(&shmem_swaplist_mutex); |
cb4b86ba | 894 | swapcache_free(swap, NULL); |
1da177e4 LT |
895 | redirty: |
896 | set_page_dirty(page); | |
d9fe526a HD |
897 | if (wbc->for_reclaim) |
898 | return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ | |
899 | unlock_page(page); | |
900 | return 0; | |
1da177e4 LT |
901 | } |
902 | ||
903 | #ifdef CONFIG_NUMA | |
680d794b | 904 | #ifdef CONFIG_TMPFS |
71fe804b | 905 | static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) |
680d794b | 906 | { |
095f1fc4 | 907 | char buffer[64]; |
680d794b | 908 | |
71fe804b | 909 | if (!mpol || mpol->mode == MPOL_DEFAULT) |
095f1fc4 | 910 | return; /* show nothing */ |
680d794b | 911 | |
a7a88b23 | 912 | mpol_to_str(buffer, sizeof(buffer), mpol); |
095f1fc4 LS |
913 | |
914 | seq_printf(seq, ",mpol=%s", buffer); | |
680d794b | 915 | } |
71fe804b LS |
916 | |
917 | static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) | |
918 | { | |
919 | struct mempolicy *mpol = NULL; | |
920 | if (sbinfo->mpol) { | |
921 | spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ | |
922 | mpol = sbinfo->mpol; | |
923 | mpol_get(mpol); | |
924 | spin_unlock(&sbinfo->stat_lock); | |
925 | } | |
926 | return mpol; | |
927 | } | |
680d794b | 928 | #endif /* CONFIG_TMPFS */ |
929 | ||
41ffe5d5 HD |
930 | static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, |
931 | struct shmem_inode_info *info, pgoff_t index) | |
1da177e4 | 932 | { |
1da177e4 | 933 | struct vm_area_struct pvma; |
18a2f371 | 934 | struct page *page; |
52cd3b07 | 935 | |
1da177e4 | 936 | /* Create a pseudo vma that just contains the policy */ |
c4cc6d07 | 937 | pvma.vm_start = 0; |
09c231cb NZ |
938 | /* Bias interleave by inode number to distribute better across nodes */ |
939 | pvma.vm_pgoff = index + info->vfs_inode.i_ino; | |
c4cc6d07 | 940 | pvma.vm_ops = NULL; |
18a2f371 MG |
941 | pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index); |
942 | ||
943 | page = swapin_readahead(swap, gfp, &pvma, 0); | |
944 | ||
945 | /* Drop reference taken by mpol_shared_policy_lookup() */ | |
946 | mpol_cond_put(pvma.vm_policy); | |
947 | ||
948 | return page; | |
1da177e4 LT |
949 | } |
950 | ||
02098fea | 951 | static struct page *shmem_alloc_page(gfp_t gfp, |
41ffe5d5 | 952 | struct shmem_inode_info *info, pgoff_t index) |
1da177e4 LT |
953 | { |
954 | struct vm_area_struct pvma; | |
18a2f371 | 955 | struct page *page; |
1da177e4 | 956 | |
c4cc6d07 HD |
957 | /* Create a pseudo vma that just contains the policy */ |
958 | pvma.vm_start = 0; | |
09c231cb NZ |
959 | /* Bias interleave by inode number to distribute better across nodes */ |
960 | pvma.vm_pgoff = index + info->vfs_inode.i_ino; | |
c4cc6d07 | 961 | pvma.vm_ops = NULL; |
41ffe5d5 | 962 | pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, index); |
52cd3b07 | 963 | |
18a2f371 | 964 | page = alloc_page_vma(gfp, &pvma, 0); |
3c2a0909 S |
965 | if (page && is_cma_pageblock(page)) { |
966 | __free_page(page); | |
967 | page = alloc_pages(gfp & ~__GFP_MOVABLE, 0); | |
968 | } | |
18a2f371 MG |
969 | |
970 | /* Drop reference taken by mpol_shared_policy_lookup() */ | |
971 | mpol_cond_put(pvma.vm_policy); | |
972 | ||
973 | return page; | |
1da177e4 | 974 | } |
680d794b | 975 | #else /* !CONFIG_NUMA */ |
976 | #ifdef CONFIG_TMPFS | |
41ffe5d5 | 977 | static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) |
680d794b | 978 | { |
979 | } | |
980 | #endif /* CONFIG_TMPFS */ | |
981 | ||
41ffe5d5 HD |
982 | static inline struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, |
983 | struct shmem_inode_info *info, pgoff_t index) | |
1da177e4 | 984 | { |
41ffe5d5 | 985 | return swapin_readahead(swap, gfp, NULL, 0); |
1da177e4 LT |
986 | } |
987 | ||
02098fea | 988 | static inline struct page *shmem_alloc_page(gfp_t gfp, |
41ffe5d5 | 989 | struct shmem_inode_info *info, pgoff_t index) |
1da177e4 | 990 | { |
e84e2e13 | 991 | return alloc_page(gfp); |
1da177e4 | 992 | } |
680d794b | 993 | #endif /* CONFIG_NUMA */ |
1da177e4 | 994 | |
71fe804b LS |
995 | #if !defined(CONFIG_NUMA) || !defined(CONFIG_TMPFS) |
996 | static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) | |
997 | { | |
998 | return NULL; | |
999 | } | |
1000 | #endif | |
1001 | ||
bde05d1c HD |
1002 | /* |
1003 | * When a page is moved from swapcache to shmem filecache (either by the | |
1004 | * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of | |
1005 | * shmem_unuse_inode()), it may have been read in earlier from swap, in | |
1006 | * ignorance of the mapping it belongs to. If that mapping has special | |
1007 | * constraints (like the gma500 GEM driver, which requires RAM below 4GB), | |
1008 | * we may need to copy to a suitable page before moving to filecache. | |
1009 | * | |
1010 | * In a future release, this may well be extended to respect cpuset and | |
1011 | * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); | |
1012 | * but for now it is a simple matter of zone. | |
1013 | */ | |
1014 | static bool shmem_should_replace_page(struct page *page, gfp_t gfp) | |
1015 | { | |
1016 | return page_zonenum(page) > gfp_zone(gfp); | |
1017 | } | |
1018 | ||
1019 | static int shmem_replace_page(struct page **pagep, gfp_t gfp, | |
1020 | struct shmem_inode_info *info, pgoff_t index) | |
1021 | { | |
1022 | struct page *oldpage, *newpage; | |
1023 | struct address_space *swap_mapping; | |
1024 | pgoff_t swap_index; | |
1025 | int error; | |
1026 | ||
1027 | oldpage = *pagep; | |
1028 | swap_index = page_private(oldpage); | |
1029 | swap_mapping = page_mapping(oldpage); | |
1030 | ||
1031 | /* | |
1032 | * We have arrived here because our zones are constrained, so don't | |
1033 | * limit chance of success by further cpuset and node constraints. | |
1034 | */ | |
1035 | gfp &= ~GFP_CONSTRAINT_MASK; | |
1036 | newpage = shmem_alloc_page(gfp, info, index); | |
1037 | if (!newpage) | |
1038 | return -ENOMEM; | |
bde05d1c | 1039 | |
bde05d1c HD |
1040 | page_cache_get(newpage); |
1041 | copy_highpage(newpage, oldpage); | |
0142ef6c | 1042 | flush_dcache_page(newpage); |
bde05d1c | 1043 | |
bde05d1c | 1044 | __set_page_locked(newpage); |
bde05d1c | 1045 | SetPageUptodate(newpage); |
bde05d1c | 1046 | SetPageSwapBacked(newpage); |
bde05d1c | 1047 | set_page_private(newpage, swap_index); |
bde05d1c HD |
1048 | SetPageSwapCache(newpage); |
1049 | ||
1050 | /* | |
1051 | * Our caller will very soon move newpage out of swapcache, but it's | |
1052 | * a nice clean interface for us to replace oldpage by newpage there. | |
1053 | */ | |
1054 | spin_lock_irq(&swap_mapping->tree_lock); | |
1055 | error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage, | |
1056 | newpage); | |
0142ef6c HD |
1057 | if (!error) { |
1058 | __inc_zone_page_state(newpage, NR_FILE_PAGES); | |
1059 | __dec_zone_page_state(oldpage, NR_FILE_PAGES); | |
1060 | } | |
bde05d1c | 1061 | spin_unlock_irq(&swap_mapping->tree_lock); |
bde05d1c | 1062 | |
0142ef6c HD |
1063 | if (unlikely(error)) { |
1064 | /* | |
1065 | * Is this possible? I think not, now that our callers check | |
1066 | * both PageSwapCache and page_private after getting page lock; | |
1067 | * but be defensive. Reverse old to newpage for clear and free. | |
1068 | */ | |
1069 | oldpage = newpage; | |
1070 | } else { | |
1071 | mem_cgroup_replace_page_cache(oldpage, newpage); | |
1072 | lru_cache_add_anon(newpage); | |
1073 | *pagep = newpage; | |
1074 | } | |
bde05d1c HD |
1075 | |
1076 | ClearPageSwapCache(oldpage); | |
1077 | set_page_private(oldpage, 0); | |
1078 | ||
1079 | unlock_page(oldpage); | |
1080 | page_cache_release(oldpage); | |
1081 | page_cache_release(oldpage); | |
0142ef6c | 1082 | return error; |
bde05d1c HD |
1083 | } |
1084 | ||
1da177e4 | 1085 | /* |
68da9f05 | 1086 | * shmem_getpage_gfp - find page in cache, or get from swap, or allocate |
1da177e4 LT |
1087 | * |
1088 | * If we allocate a new one we do not mark it dirty. That's up to the | |
1089 | * vm. If we swap it in we mark it dirty since we also free the swap | |
1090 | * entry since a page cannot live in both the swap and page cache | |
1091 | */ | |
41ffe5d5 | 1092 | static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, |
68da9f05 | 1093 | struct page **pagep, enum sgp_type sgp, gfp_t gfp, int *fault_type) |
1da177e4 LT |
1094 | { |
1095 | struct address_space *mapping = inode->i_mapping; | |
54af6042 | 1096 | struct shmem_inode_info *info; |
1da177e4 | 1097 | struct shmem_sb_info *sbinfo; |
27ab7006 | 1098 | struct page *page; |
1da177e4 LT |
1099 | swp_entry_t swap; |
1100 | int error; | |
54af6042 | 1101 | int once = 0; |
1635f6a7 | 1102 | int alloced = 0; |
1da177e4 | 1103 | |
41ffe5d5 | 1104 | if (index > (MAX_LFS_FILESIZE >> PAGE_CACHE_SHIFT)) |
1da177e4 | 1105 | return -EFBIG; |
1da177e4 | 1106 | repeat: |
54af6042 | 1107 | swap.val = 0; |
41ffe5d5 | 1108 | page = find_lock_page(mapping, index); |
54af6042 HD |
1109 | if (radix_tree_exceptional_entry(page)) { |
1110 | swap = radix_to_swp_entry(page); | |
1111 | page = NULL; | |
1112 | } | |
1113 | ||
1635f6a7 | 1114 | if (sgp != SGP_WRITE && sgp != SGP_FALLOC && |
54af6042 HD |
1115 | ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { |
1116 | error = -EINVAL; | |
1117 | goto failed; | |
1118 | } | |
1119 | ||
1635f6a7 HD |
1120 | /* fallocated page? */ |
1121 | if (page && !PageUptodate(page)) { | |
1122 | if (sgp != SGP_READ) | |
1123 | goto clear; | |
1124 | unlock_page(page); | |
1125 | page_cache_release(page); | |
1126 | page = NULL; | |
1127 | } | |
54af6042 | 1128 | if (page || (sgp == SGP_READ && !swap.val)) { |
54af6042 HD |
1129 | *pagep = page; |
1130 | return 0; | |
27ab7006 HD |
1131 | } |
1132 | ||
1133 | /* | |
54af6042 HD |
1134 | * Fast cache lookup did not find it: |
1135 | * bring it back from swap or allocate. | |
27ab7006 | 1136 | */ |
54af6042 HD |
1137 | info = SHMEM_I(inode); |
1138 | sbinfo = SHMEM_SB(inode->i_sb); | |
1da177e4 | 1139 | |
1da177e4 LT |
1140 | if (swap.val) { |
1141 | /* Look it up and read it in.. */ | |
27ab7006 HD |
1142 | page = lookup_swap_cache(swap); |
1143 | if (!page) { | |
1da177e4 | 1144 | /* here we actually do the io */ |
68da9f05 HD |
1145 | if (fault_type) |
1146 | *fault_type |= VM_FAULT_MAJOR; | |
41ffe5d5 | 1147 | page = shmem_swapin(swap, gfp, info, index); |
27ab7006 | 1148 | if (!page) { |
54af6042 HD |
1149 | error = -ENOMEM; |
1150 | goto failed; | |
1da177e4 | 1151 | } |
1da177e4 LT |
1152 | } |
1153 | ||
1154 | /* We have to do this with page locked to prevent races */ | |
54af6042 | 1155 | lock_page(page); |
0142ef6c | 1156 | if (!PageSwapCache(page) || page_private(page) != swap.val || |
d1899228 | 1157 | !shmem_confirm_swap(mapping, index, swap)) { |
bde05d1c | 1158 | error = -EEXIST; /* try again */ |
d1899228 | 1159 | goto unlock; |
bde05d1c | 1160 | } |
27ab7006 | 1161 | if (!PageUptodate(page)) { |
1da177e4 | 1162 | error = -EIO; |
54af6042 | 1163 | goto failed; |
1da177e4 | 1164 | } |
54af6042 HD |
1165 | wait_on_page_writeback(page); |
1166 | ||
bde05d1c HD |
1167 | if (shmem_should_replace_page(page, gfp)) { |
1168 | error = shmem_replace_page(&page, gfp, info, index); | |
1169 | if (error) | |
1170 | goto failed; | |
1da177e4 | 1171 | } |
27ab7006 | 1172 | |
aa3b1895 HD |
1173 | error = mem_cgroup_cache_charge(page, current->mm, |
1174 | gfp & GFP_RECLAIM_MASK); | |
d1899228 | 1175 | if (!error) { |
aa3b1895 HD |
1176 | error = shmem_add_to_page_cache(page, mapping, index, |
1177 | gfp, swp_to_radix_entry(swap)); | |
215c02bc HD |
1178 | /* |
1179 | * We already confirmed swap under page lock, and make | |
1180 | * no memory allocation here, so usually no possibility | |
1181 | * of error; but free_swap_and_cache() only trylocks a | |
1182 | * page, so it is just possible that the entry has been | |
1183 | * truncated or holepunched since swap was confirmed. | |
1184 | * shmem_undo_range() will have done some of the | |
1185 | * unaccounting, now delete_from_swap_cache() will do | |
1186 | * the rest (including mem_cgroup_uncharge_swapcache). | |
1187 | * Reset swap.val? No, leave it so "failed" goes back to | |
1188 | * "repeat": reading a hole and writing should succeed. | |
1189 | */ | |
1190 | if (error) | |
1191 | delete_from_swap_cache(page); | |
d1899228 | 1192 | } |
54af6042 HD |
1193 | if (error) |
1194 | goto failed; | |
1195 | ||
1196 | spin_lock(&info->lock); | |
285b2c4f | 1197 | info->swapped--; |
54af6042 | 1198 | shmem_recalc_inode(inode); |
27ab7006 | 1199 | spin_unlock(&info->lock); |
54af6042 HD |
1200 | |
1201 | delete_from_swap_cache(page); | |
27ab7006 HD |
1202 | set_page_dirty(page); |
1203 | swap_free(swap); | |
1204 | ||
54af6042 HD |
1205 | } else { |
1206 | if (shmem_acct_block(info->flags)) { | |
1207 | error = -ENOSPC; | |
1208 | goto failed; | |
1da177e4 | 1209 | } |
0edd73b3 | 1210 | if (sbinfo->max_blocks) { |
fc5da22a | 1211 | if (percpu_counter_compare(&sbinfo->used_blocks, |
54af6042 HD |
1212 | sbinfo->max_blocks) >= 0) { |
1213 | error = -ENOSPC; | |
1214 | goto unacct; | |
1215 | } | |
7e496299 | 1216 | percpu_counter_inc(&sbinfo->used_blocks); |
54af6042 | 1217 | } |
1da177e4 | 1218 | |
54af6042 HD |
1219 | page = shmem_alloc_page(gfp, info, index); |
1220 | if (!page) { | |
1221 | error = -ENOMEM; | |
1222 | goto decused; | |
1da177e4 LT |
1223 | } |
1224 | ||
54af6042 HD |
1225 | SetPageSwapBacked(page); |
1226 | __set_page_locked(page); | |
aa3b1895 HD |
1227 | error = mem_cgroup_cache_charge(page, current->mm, |
1228 | gfp & GFP_RECLAIM_MASK); | |
54af6042 HD |
1229 | if (error) |
1230 | goto decused; | |
b065b432 HD |
1231 | error = radix_tree_preload(gfp & GFP_RECLAIM_MASK); |
1232 | if (!error) { | |
1233 | error = shmem_add_to_page_cache(page, mapping, index, | |
1234 | gfp, NULL); | |
1235 | radix_tree_preload_end(); | |
1236 | } | |
1237 | if (error) { | |
1238 | mem_cgroup_uncharge_cache_page(page); | |
1239 | goto decused; | |
1240 | } | |
54af6042 HD |
1241 | lru_cache_add_anon(page); |
1242 | ||
1243 | spin_lock(&info->lock); | |
1da177e4 | 1244 | info->alloced++; |
54af6042 HD |
1245 | inode->i_blocks += BLOCKS_PER_PAGE; |
1246 | shmem_recalc_inode(inode); | |
1da177e4 | 1247 | spin_unlock(&info->lock); |
1635f6a7 | 1248 | alloced = true; |
54af6042 | 1249 | |
ec9516fb | 1250 | /* |
1635f6a7 HD |
1251 | * Let SGP_FALLOC use the SGP_WRITE optimization on a new page. |
1252 | */ | |
1253 | if (sgp == SGP_FALLOC) | |
1254 | sgp = SGP_WRITE; | |
1255 | clear: | |
1256 | /* | |
1257 | * Let SGP_WRITE caller clear ends if write does not fill page; | |
1258 | * but SGP_FALLOC on a page fallocated earlier must initialize | |
1259 | * it now, lest undo on failure cancel our earlier guarantee. | |
ec9516fb HD |
1260 | */ |
1261 | if (sgp != SGP_WRITE) { | |
1262 | clear_highpage(page); | |
1263 | flush_dcache_page(page); | |
1264 | SetPageUptodate(page); | |
1265 | } | |
a0ee5ec5 | 1266 | if (sgp == SGP_DIRTY) |
27ab7006 | 1267 | set_page_dirty(page); |
1da177e4 | 1268 | } |
bde05d1c | 1269 | |
54af6042 | 1270 | /* Perhaps the file has been truncated since we checked */ |
1635f6a7 | 1271 | if (sgp != SGP_WRITE && sgp != SGP_FALLOC && |
54af6042 HD |
1272 | ((loff_t)index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { |
1273 | error = -EINVAL; | |
1635f6a7 HD |
1274 | if (alloced) |
1275 | goto trunc; | |
1276 | else | |
1277 | goto failed; | |
e83c32e8 | 1278 | } |
54af6042 HD |
1279 | *pagep = page; |
1280 | return 0; | |
1da177e4 | 1281 | |
59a16ead | 1282 | /* |
54af6042 | 1283 | * Error recovery. |
59a16ead | 1284 | */ |
54af6042 | 1285 | trunc: |
1635f6a7 | 1286 | info = SHMEM_I(inode); |
54af6042 HD |
1287 | ClearPageDirty(page); |
1288 | delete_from_page_cache(page); | |
1289 | spin_lock(&info->lock); | |
1290 | info->alloced--; | |
1291 | inode->i_blocks -= BLOCKS_PER_PAGE; | |
59a16ead | 1292 | spin_unlock(&info->lock); |
54af6042 | 1293 | decused: |
1635f6a7 | 1294 | sbinfo = SHMEM_SB(inode->i_sb); |
54af6042 HD |
1295 | if (sbinfo->max_blocks) |
1296 | percpu_counter_add(&sbinfo->used_blocks, -1); | |
1297 | unacct: | |
1298 | shmem_unacct_blocks(info->flags, 1); | |
1299 | failed: | |
d1899228 HD |
1300 | if (swap.val && error != -EINVAL && |
1301 | !shmem_confirm_swap(mapping, index, swap)) | |
1302 | error = -EEXIST; | |
1303 | unlock: | |
27ab7006 | 1304 | if (page) { |
54af6042 | 1305 | unlock_page(page); |
27ab7006 | 1306 | page_cache_release(page); |
54af6042 HD |
1307 | } |
1308 | if (error == -ENOSPC && !once++) { | |
1309 | info = SHMEM_I(inode); | |
1310 | spin_lock(&info->lock); | |
1311 | shmem_recalc_inode(inode); | |
1312 | spin_unlock(&info->lock); | |
27ab7006 | 1313 | goto repeat; |
ff36b801 | 1314 | } |
d1899228 | 1315 | if (error == -EEXIST) /* from above or from radix_tree_insert */ |
54af6042 HD |
1316 | goto repeat; |
1317 | return error; | |
1da177e4 LT |
1318 | } |
1319 | ||
d0217ac0 | 1320 | static int shmem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
1da177e4 | 1321 | { |
496ad9aa | 1322 | struct inode *inode = file_inode(vma->vm_file); |
1da177e4 | 1323 | int error; |
68da9f05 | 1324 | int ret = VM_FAULT_LOCKED; |
1da177e4 | 1325 | |
1ccc3ffa HD |
1326 | /* |
1327 | * Trinity finds that probing a hole which tmpfs is punching can | |
1328 | * prevent the hole-punch from ever completing: which in turn | |
1329 | * locks writers out with its hold on i_mutex. So refrain from | |
887675c9 HD |
1330 | * faulting pages into the hole while it's being punched. Although |
1331 | * shmem_undo_range() does remove the additions, it may be unable to | |
1332 | * keep up, as each new page needs its own unmap_mapping_range() call, | |
1333 | * and the i_mmap tree grows ever slower to scan if new vmas are added. | |
1334 | * | |
1335 | * It does not matter if we sometimes reach this check just before the | |
1336 | * hole-punch begins, so that one fault then races with the punch: | |
1337 | * we just need to make racing faults a rare case. | |
1338 | * | |
1339 | * The implementation below would be much simpler if we just used a | |
1340 | * standard mutex or completion: but we cannot take i_mutex in fault, | |
1341 | * and bloating every shmem inode for this unlikely case would be sad. | |
1ccc3ffa HD |
1342 | */ |
1343 | if (unlikely(inode->i_private)) { | |
1344 | struct shmem_falloc *shmem_falloc; | |
1345 | ||
1346 | spin_lock(&inode->i_lock); | |
1347 | shmem_falloc = inode->i_private; | |
887675c9 HD |
1348 | if (shmem_falloc && |
1349 | shmem_falloc->waitq && | |
1350 | vmf->pgoff >= shmem_falloc->start && | |
1351 | vmf->pgoff < shmem_falloc->next) { | |
1352 | wait_queue_head_t *shmem_falloc_waitq; | |
1353 | DEFINE_WAIT(shmem_fault_wait); | |
1354 | ||
1355 | ret = VM_FAULT_NOPAGE; | |
1ccc3ffa HD |
1356 | if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) && |
1357 | !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) { | |
887675c9 | 1358 | /* It's polite to up mmap_sem if we can */ |
1ccc3ffa | 1359 | up_read(&vma->vm_mm->mmap_sem); |
887675c9 | 1360 | ret = VM_FAULT_RETRY; |
1ccc3ffa | 1361 | } |
887675c9 HD |
1362 | |
1363 | shmem_falloc_waitq = shmem_falloc->waitq; | |
1364 | prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, | |
1365 | TASK_UNINTERRUPTIBLE); | |
1366 | spin_unlock(&inode->i_lock); | |
1367 | schedule(); | |
1368 | ||
1369 | /* | |
1370 | * shmem_falloc_waitq points into the shmem_fallocate() | |
1371 | * stack of the hole-punching task: shmem_falloc_waitq | |
1372 | * is usually invalid by the time we reach here, but | |
1373 | * finish_wait() does not dereference it in that case; | |
1374 | * though i_lock needed lest racing with wake_up_all(). | |
1375 | */ | |
1376 | spin_lock(&inode->i_lock); | |
1377 | finish_wait(shmem_falloc_waitq, &shmem_fault_wait); | |
1378 | spin_unlock(&inode->i_lock); | |
1379 | return ret; | |
1ccc3ffa | 1380 | } |
887675c9 | 1381 | spin_unlock(&inode->i_lock); |
1ccc3ffa HD |
1382 | } |
1383 | ||
27d54b39 | 1384 | error = shmem_getpage(inode, vmf->pgoff, &vmf->page, SGP_CACHE, &ret); |
d0217ac0 NP |
1385 | if (error) |
1386 | return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS); | |
68da9f05 | 1387 | |
456f998e YH |
1388 | if (ret & VM_FAULT_MAJOR) { |
1389 | count_vm_event(PGMAJFAULT); | |
1390 | mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT); | |
1391 | } | |
68da9f05 | 1392 | return ret; |
1da177e4 LT |
1393 | } |
1394 | ||
1da177e4 | 1395 | #ifdef CONFIG_NUMA |
41ffe5d5 | 1396 | static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) |
1da177e4 | 1397 | { |
496ad9aa | 1398 | struct inode *inode = file_inode(vma->vm_file); |
41ffe5d5 | 1399 | return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); |
1da177e4 LT |
1400 | } |
1401 | ||
d8dc74f2 AB |
1402 | static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, |
1403 | unsigned long addr) | |
1da177e4 | 1404 | { |
496ad9aa | 1405 | struct inode *inode = file_inode(vma->vm_file); |
41ffe5d5 | 1406 | pgoff_t index; |
1da177e4 | 1407 | |
41ffe5d5 HD |
1408 | index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
1409 | return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); | |
1da177e4 LT |
1410 | } |
1411 | #endif | |
1412 | ||
1413 | int shmem_lock(struct file *file, int lock, struct user_struct *user) | |
1414 | { | |
496ad9aa | 1415 | struct inode *inode = file_inode(file); |
1da177e4 LT |
1416 | struct shmem_inode_info *info = SHMEM_I(inode); |
1417 | int retval = -ENOMEM; | |
1418 | ||
1419 | spin_lock(&info->lock); | |
1420 | if (lock && !(info->flags & VM_LOCKED)) { | |
1421 | if (!user_shm_lock(inode->i_size, user)) | |
1422 | goto out_nomem; | |
1423 | info->flags |= VM_LOCKED; | |
89e004ea | 1424 | mapping_set_unevictable(file->f_mapping); |
1da177e4 LT |
1425 | } |
1426 | if (!lock && (info->flags & VM_LOCKED) && user) { | |
1427 | user_shm_unlock(inode->i_size, user); | |
1428 | info->flags &= ~VM_LOCKED; | |
89e004ea | 1429 | mapping_clear_unevictable(file->f_mapping); |
1da177e4 LT |
1430 | } |
1431 | retval = 0; | |
89e004ea | 1432 | |
1da177e4 LT |
1433 | out_nomem: |
1434 | spin_unlock(&info->lock); | |
1435 | return retval; | |
1436 | } | |
1437 | ||
9b83a6a8 | 1438 | static int shmem_mmap(struct file *file, struct vm_area_struct *vma) |
1da177e4 LT |
1439 | { |
1440 | file_accessed(file); | |
1441 | vma->vm_ops = &shmem_vm_ops; | |
1442 | return 0; | |
1443 | } | |
1444 | ||
454abafe | 1445 | static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, |
09208d15 | 1446 | umode_t mode, dev_t dev, unsigned long flags) |
1da177e4 LT |
1447 | { |
1448 | struct inode *inode; | |
1449 | struct shmem_inode_info *info; | |
1450 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); | |
1451 | ||
5b04c689 PE |
1452 | if (shmem_reserve_inode(sb)) |
1453 | return NULL; | |
1da177e4 LT |
1454 | |
1455 | inode = new_inode(sb); | |
1456 | if (inode) { | |
85fe4025 | 1457 | inode->i_ino = get_next_ino(); |
454abafe | 1458 | inode_init_owner(inode, dir, mode); |
1da177e4 | 1459 | inode->i_blocks = 0; |
1da177e4 LT |
1460 | inode->i_mapping->backing_dev_info = &shmem_backing_dev_info; |
1461 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
91828a40 | 1462 | inode->i_generation = get_seconds(); |
1da177e4 LT |
1463 | info = SHMEM_I(inode); |
1464 | memset(info, 0, (char *)inode - (char *)info); | |
1465 | spin_lock_init(&info->lock); | |
0b0a0806 | 1466 | info->flags = flags & VM_NORESERVE; |
1da177e4 | 1467 | INIT_LIST_HEAD(&info->swaplist); |
38f38657 | 1468 | simple_xattrs_init(&info->xattrs); |
72c04902 | 1469 | cache_no_acl(inode); |
1da177e4 LT |
1470 | |
1471 | switch (mode & S_IFMT) { | |
1472 | default: | |
39f0247d | 1473 | inode->i_op = &shmem_special_inode_operations; |
1da177e4 LT |
1474 | init_special_inode(inode, mode, dev); |
1475 | break; | |
1476 | case S_IFREG: | |
14fcc23f | 1477 | inode->i_mapping->a_ops = &shmem_aops; |
1da177e4 LT |
1478 | inode->i_op = &shmem_inode_operations; |
1479 | inode->i_fop = &shmem_file_operations; | |
71fe804b LS |
1480 | mpol_shared_policy_init(&info->policy, |
1481 | shmem_get_sbmpol(sbinfo)); | |
1da177e4 LT |
1482 | break; |
1483 | case S_IFDIR: | |
d8c76e6f | 1484 | inc_nlink(inode); |
1da177e4 LT |
1485 | /* Some things misbehave if size == 0 on a directory */ |
1486 | inode->i_size = 2 * BOGO_DIRENT_SIZE; | |
1487 | inode->i_op = &shmem_dir_inode_operations; | |
1488 | inode->i_fop = &simple_dir_operations; | |
1489 | break; | |
1490 | case S_IFLNK: | |
1491 | /* | |
1492 | * Must not load anything in the rbtree, | |
1493 | * mpol_free_shared_policy will not be called. | |
1494 | */ | |
71fe804b | 1495 | mpol_shared_policy_init(&info->policy, NULL); |
1da177e4 LT |
1496 | break; |
1497 | } | |
5b04c689 PE |
1498 | } else |
1499 | shmem_free_inode(sb); | |
1da177e4 LT |
1500 | return inode; |
1501 | } | |
1502 | ||
1503 | #ifdef CONFIG_TMPFS | |
92e1d5be | 1504 | static const struct inode_operations shmem_symlink_inode_operations; |
69f07ec9 | 1505 | static const struct inode_operations shmem_short_symlink_operations; |
1da177e4 | 1506 | |
6d9d88d0 JS |
1507 | #ifdef CONFIG_TMPFS_XATTR |
1508 | static int shmem_initxattrs(struct inode *, const struct xattr *, void *); | |
1509 | #else | |
1510 | #define shmem_initxattrs NULL | |
1511 | #endif | |
1512 | ||
1da177e4 | 1513 | static int |
800d15a5 NP |
1514 | shmem_write_begin(struct file *file, struct address_space *mapping, |
1515 | loff_t pos, unsigned len, unsigned flags, | |
1516 | struct page **pagep, void **fsdata) | |
1da177e4 | 1517 | { |
800d15a5 NP |
1518 | struct inode *inode = mapping->host; |
1519 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; | |
800d15a5 NP |
1520 | return shmem_getpage(inode, index, pagep, SGP_WRITE, NULL); |
1521 | } | |
1522 | ||
1523 | static int | |
1524 | shmem_write_end(struct file *file, struct address_space *mapping, | |
1525 | loff_t pos, unsigned len, unsigned copied, | |
1526 | struct page *page, void *fsdata) | |
1527 | { | |
1528 | struct inode *inode = mapping->host; | |
1529 | ||
d3602444 HD |
1530 | if (pos + copied > inode->i_size) |
1531 | i_size_write(inode, pos + copied); | |
1532 | ||
ec9516fb HD |
1533 | if (!PageUptodate(page)) { |
1534 | if (copied < PAGE_CACHE_SIZE) { | |
1535 | unsigned from = pos & (PAGE_CACHE_SIZE - 1); | |
1536 | zero_user_segments(page, 0, from, | |
1537 | from + copied, PAGE_CACHE_SIZE); | |
1538 | } | |
1539 | SetPageUptodate(page); | |
1540 | } | |
800d15a5 | 1541 | set_page_dirty(page); |
6746aff7 | 1542 | unlock_page(page); |
800d15a5 NP |
1543 | page_cache_release(page); |
1544 | ||
800d15a5 | 1545 | return copied; |
1da177e4 LT |
1546 | } |
1547 | ||
1da177e4 LT |
1548 | static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor) |
1549 | { | |
496ad9aa | 1550 | struct inode *inode = file_inode(filp); |
1da177e4 | 1551 | struct address_space *mapping = inode->i_mapping; |
41ffe5d5 HD |
1552 | pgoff_t index; |
1553 | unsigned long offset; | |
a0ee5ec5 HD |
1554 | enum sgp_type sgp = SGP_READ; |
1555 | ||
1556 | /* | |
1557 | * Might this read be for a stacking filesystem? Then when reading | |
1558 | * holes of a sparse file, we actually need to allocate those pages, | |
1559 | * and even mark them dirty, so it cannot exceed the max_blocks limit. | |
1560 | */ | |
1561 | if (segment_eq(get_fs(), KERNEL_DS)) | |
1562 | sgp = SGP_DIRTY; | |
1da177e4 LT |
1563 | |
1564 | index = *ppos >> PAGE_CACHE_SHIFT; | |
1565 | offset = *ppos & ~PAGE_CACHE_MASK; | |
1566 | ||
1567 | for (;;) { | |
1568 | struct page *page = NULL; | |
41ffe5d5 HD |
1569 | pgoff_t end_index; |
1570 | unsigned long nr, ret; | |
1da177e4 LT |
1571 | loff_t i_size = i_size_read(inode); |
1572 | ||
1573 | end_index = i_size >> PAGE_CACHE_SHIFT; | |
1574 | if (index > end_index) | |
1575 | break; | |
1576 | if (index == end_index) { | |
1577 | nr = i_size & ~PAGE_CACHE_MASK; | |
1578 | if (nr <= offset) | |
1579 | break; | |
1580 | } | |
1581 | ||
a0ee5ec5 | 1582 | desc->error = shmem_getpage(inode, index, &page, sgp, NULL); |
1da177e4 LT |
1583 | if (desc->error) { |
1584 | if (desc->error == -EINVAL) | |
1585 | desc->error = 0; | |
1586 | break; | |
1587 | } | |
d3602444 HD |
1588 | if (page) |
1589 | unlock_page(page); | |
1da177e4 LT |
1590 | |
1591 | /* | |
1592 | * We must evaluate after, since reads (unlike writes) | |
1b1dcc1b | 1593 | * are called without i_mutex protection against truncate |
1da177e4 LT |
1594 | */ |
1595 | nr = PAGE_CACHE_SIZE; | |
1596 | i_size = i_size_read(inode); | |
1597 | end_index = i_size >> PAGE_CACHE_SHIFT; | |
1598 | if (index == end_index) { | |
1599 | nr = i_size & ~PAGE_CACHE_MASK; | |
1600 | if (nr <= offset) { | |
1601 | if (page) | |
1602 | page_cache_release(page); | |
1603 | break; | |
1604 | } | |
1605 | } | |
1606 | nr -= offset; | |
1607 | ||
1608 | if (page) { | |
1609 | /* | |
1610 | * If users can be writing to this page using arbitrary | |
1611 | * virtual addresses, take care about potential aliasing | |
1612 | * before reading the page on the kernel side. | |
1613 | */ | |
1614 | if (mapping_writably_mapped(mapping)) | |
1615 | flush_dcache_page(page); | |
1616 | /* | |
1617 | * Mark the page accessed if we read the beginning. | |
1618 | */ | |
1619 | if (!offset) | |
1620 | mark_page_accessed(page); | |
b5810039 | 1621 | } else { |
1da177e4 | 1622 | page = ZERO_PAGE(0); |
b5810039 NP |
1623 | page_cache_get(page); |
1624 | } | |
1da177e4 LT |
1625 | |
1626 | /* | |
1627 | * Ok, we have the page, and it's up-to-date, so | |
1628 | * now we can copy it to user space... | |
1629 | * | |
1630 | * The actor routine returns how many bytes were actually used.. | |
1631 | * NOTE! This may not be the same as how much of a user buffer | |
1632 | * we filled up (we may be padding etc), so we can only update | |
1633 | * "pos" here (the actor routine has to update the user buffer | |
1634 | * pointers and the remaining count). | |
1635 | */ | |
1636 | ret = actor(desc, page, offset, nr); | |
1637 | offset += ret; | |
1638 | index += offset >> PAGE_CACHE_SHIFT; | |
1639 | offset &= ~PAGE_CACHE_MASK; | |
1640 | ||
1641 | page_cache_release(page); | |
1642 | if (ret != nr || !desc->count) | |
1643 | break; | |
1644 | ||
1645 | cond_resched(); | |
1646 | } | |
1647 | ||
1648 | *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; | |
1649 | file_accessed(filp); | |
1650 | } | |
1651 | ||
bcd78e49 HD |
1652 | static ssize_t shmem_file_aio_read(struct kiocb *iocb, |
1653 | const struct iovec *iov, unsigned long nr_segs, loff_t pos) | |
1654 | { | |
1655 | struct file *filp = iocb->ki_filp; | |
1656 | ssize_t retval; | |
1657 | unsigned long seg; | |
1658 | size_t count; | |
1659 | loff_t *ppos = &iocb->ki_pos; | |
1660 | ||
1661 | retval = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE); | |
1662 | if (retval) | |
1663 | return retval; | |
1664 | ||
1665 | for (seg = 0; seg < nr_segs; seg++) { | |
1666 | read_descriptor_t desc; | |
1667 | ||
1668 | desc.written = 0; | |
1669 | desc.arg.buf = iov[seg].iov_base; | |
1670 | desc.count = iov[seg].iov_len; | |
1671 | if (desc.count == 0) | |
1672 | continue; | |
1673 | desc.error = 0; | |
1674 | do_shmem_file_read(filp, ppos, &desc, file_read_actor); | |
1675 | retval += desc.written; | |
1676 | if (desc.error) { | |
1677 | retval = retval ?: desc.error; | |
1678 | break; | |
1679 | } | |
1680 | if (desc.count > 0) | |
1681 | break; | |
1682 | } | |
1683 | return retval; | |
1da177e4 LT |
1684 | } |
1685 | ||
708e3508 HD |
1686 | static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, |
1687 | struct pipe_inode_info *pipe, size_t len, | |
1688 | unsigned int flags) | |
1689 | { | |
1690 | struct address_space *mapping = in->f_mapping; | |
71f0e07a | 1691 | struct inode *inode = mapping->host; |
708e3508 HD |
1692 | unsigned int loff, nr_pages, req_pages; |
1693 | struct page *pages[PIPE_DEF_BUFFERS]; | |
1694 | struct partial_page partial[PIPE_DEF_BUFFERS]; | |
1695 | struct page *page; | |
1696 | pgoff_t index, end_index; | |
1697 | loff_t isize, left; | |
1698 | int error, page_nr; | |
1699 | struct splice_pipe_desc spd = { | |
1700 | .pages = pages, | |
1701 | .partial = partial, | |
047fe360 | 1702 | .nr_pages_max = PIPE_DEF_BUFFERS, |
708e3508 HD |
1703 | .flags = flags, |
1704 | .ops = &page_cache_pipe_buf_ops, | |
1705 | .spd_release = spd_release_page, | |
1706 | }; | |
1707 | ||
71f0e07a | 1708 | isize = i_size_read(inode); |
708e3508 HD |
1709 | if (unlikely(*ppos >= isize)) |
1710 | return 0; | |
1711 | ||
1712 | left = isize - *ppos; | |
1713 | if (unlikely(left < len)) | |
1714 | len = left; | |
1715 | ||
1716 | if (splice_grow_spd(pipe, &spd)) | |
1717 | return -ENOMEM; | |
1718 | ||
1719 | index = *ppos >> PAGE_CACHE_SHIFT; | |
1720 | loff = *ppos & ~PAGE_CACHE_MASK; | |
1721 | req_pages = (len + loff + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | |
1722 | nr_pages = min(req_pages, pipe->buffers); | |
1723 | ||
708e3508 HD |
1724 | spd.nr_pages = find_get_pages_contig(mapping, index, |
1725 | nr_pages, spd.pages); | |
1726 | index += spd.nr_pages; | |
708e3508 | 1727 | error = 0; |
708e3508 | 1728 | |
71f0e07a | 1729 | while (spd.nr_pages < nr_pages) { |
71f0e07a HD |
1730 | error = shmem_getpage(inode, index, &page, SGP_CACHE, NULL); |
1731 | if (error) | |
1732 | break; | |
1733 | unlock_page(page); | |
708e3508 HD |
1734 | spd.pages[spd.nr_pages++] = page; |
1735 | index++; | |
1736 | } | |
1737 | ||
708e3508 HD |
1738 | index = *ppos >> PAGE_CACHE_SHIFT; |
1739 | nr_pages = spd.nr_pages; | |
1740 | spd.nr_pages = 0; | |
71f0e07a | 1741 | |
708e3508 HD |
1742 | for (page_nr = 0; page_nr < nr_pages; page_nr++) { |
1743 | unsigned int this_len; | |
1744 | ||
1745 | if (!len) | |
1746 | break; | |
1747 | ||
708e3508 HD |
1748 | this_len = min_t(unsigned long, len, PAGE_CACHE_SIZE - loff); |
1749 | page = spd.pages[page_nr]; | |
1750 | ||
71f0e07a | 1751 | if (!PageUptodate(page) || page->mapping != mapping) { |
71f0e07a HD |
1752 | error = shmem_getpage(inode, index, &page, |
1753 | SGP_CACHE, NULL); | |
1754 | if (error) | |
708e3508 | 1755 | break; |
71f0e07a HD |
1756 | unlock_page(page); |
1757 | page_cache_release(spd.pages[page_nr]); | |
1758 | spd.pages[page_nr] = page; | |
708e3508 | 1759 | } |
71f0e07a HD |
1760 | |
1761 | isize = i_size_read(inode); | |
708e3508 HD |
1762 | end_index = (isize - 1) >> PAGE_CACHE_SHIFT; |
1763 | if (unlikely(!isize || index > end_index)) | |
1764 | break; | |
1765 | ||
708e3508 HD |
1766 | if (end_index == index) { |
1767 | unsigned int plen; | |
1768 | ||
708e3508 HD |
1769 | plen = ((isize - 1) & ~PAGE_CACHE_MASK) + 1; |
1770 | if (plen <= loff) | |
1771 | break; | |
1772 | ||
708e3508 HD |
1773 | this_len = min(this_len, plen - loff); |
1774 | len = this_len; | |
1775 | } | |
1776 | ||
1777 | spd.partial[page_nr].offset = loff; | |
1778 | spd.partial[page_nr].len = this_len; | |
1779 | len -= this_len; | |
1780 | loff = 0; | |
1781 | spd.nr_pages++; | |
1782 | index++; | |
1783 | } | |
1784 | ||
708e3508 HD |
1785 | while (page_nr < nr_pages) |
1786 | page_cache_release(spd.pages[page_nr++]); | |
708e3508 HD |
1787 | |
1788 | if (spd.nr_pages) | |
1789 | error = splice_to_pipe(pipe, &spd); | |
1790 | ||
047fe360 | 1791 | splice_shrink_spd(&spd); |
708e3508 HD |
1792 | |
1793 | if (error > 0) { | |
1794 | *ppos += error; | |
1795 | file_accessed(in); | |
1796 | } | |
1797 | return error; | |
1798 | } | |
1799 | ||
220f2ac9 HD |
1800 | /* |
1801 | * llseek SEEK_DATA or SEEK_HOLE through the radix_tree. | |
1802 | */ | |
1803 | static pgoff_t shmem_seek_hole_data(struct address_space *mapping, | |
965c8e59 | 1804 | pgoff_t index, pgoff_t end, int whence) |
220f2ac9 HD |
1805 | { |
1806 | struct page *page; | |
1807 | struct pagevec pvec; | |
1808 | pgoff_t indices[PAGEVEC_SIZE]; | |
1809 | bool done = false; | |
1810 | int i; | |
1811 | ||
1812 | pagevec_init(&pvec, 0); | |
1813 | pvec.nr = 1; /* start small: we may be there already */ | |
1814 | while (!done) { | |
1815 | pvec.nr = shmem_find_get_pages_and_swap(mapping, index, | |
1816 | pvec.nr, pvec.pages, indices); | |
1817 | if (!pvec.nr) { | |
965c8e59 | 1818 | if (whence == SEEK_DATA) |
220f2ac9 HD |
1819 | index = end; |
1820 | break; | |
1821 | } | |
1822 | for (i = 0; i < pvec.nr; i++, index++) { | |
1823 | if (index < indices[i]) { | |
965c8e59 | 1824 | if (whence == SEEK_HOLE) { |
220f2ac9 HD |
1825 | done = true; |
1826 | break; | |
1827 | } | |
1828 | index = indices[i]; | |
1829 | } | |
1830 | page = pvec.pages[i]; | |
1831 | if (page && !radix_tree_exceptional_entry(page)) { | |
1832 | if (!PageUptodate(page)) | |
1833 | page = NULL; | |
1834 | } | |
1835 | if (index >= end || | |
965c8e59 AM |
1836 | (page && whence == SEEK_DATA) || |
1837 | (!page && whence == SEEK_HOLE)) { | |
220f2ac9 HD |
1838 | done = true; |
1839 | break; | |
1840 | } | |
1841 | } | |
1842 | shmem_deswap_pagevec(&pvec); | |
1843 | pagevec_release(&pvec); | |
1844 | pvec.nr = PAGEVEC_SIZE; | |
1845 | cond_resched(); | |
1846 | } | |
1847 | return index; | |
1848 | } | |
1849 | ||
965c8e59 | 1850 | static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) |
220f2ac9 HD |
1851 | { |
1852 | struct address_space *mapping = file->f_mapping; | |
1853 | struct inode *inode = mapping->host; | |
1854 | pgoff_t start, end; | |
1855 | loff_t new_offset; | |
1856 | ||
965c8e59 AM |
1857 | if (whence != SEEK_DATA && whence != SEEK_HOLE) |
1858 | return generic_file_llseek_size(file, offset, whence, | |
220f2ac9 HD |
1859 | MAX_LFS_FILESIZE, i_size_read(inode)); |
1860 | mutex_lock(&inode->i_mutex); | |
1861 | /* We're holding i_mutex so we can access i_size directly */ | |
1862 | ||
1863 | if (offset < 0) | |
1864 | offset = -EINVAL; | |
1865 | else if (offset >= inode->i_size) | |
1866 | offset = -ENXIO; | |
1867 | else { | |
1868 | start = offset >> PAGE_CACHE_SHIFT; | |
1869 | end = (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | |
965c8e59 | 1870 | new_offset = shmem_seek_hole_data(mapping, start, end, whence); |
220f2ac9 HD |
1871 | new_offset <<= PAGE_CACHE_SHIFT; |
1872 | if (new_offset > offset) { | |
1873 | if (new_offset < inode->i_size) | |
1874 | offset = new_offset; | |
965c8e59 | 1875 | else if (whence == SEEK_DATA) |
220f2ac9 HD |
1876 | offset = -ENXIO; |
1877 | else | |
1878 | offset = inode->i_size; | |
1879 | } | |
1880 | } | |
1881 | ||
1882 | if (offset >= 0 && offset != file->f_pos) { | |
1883 | file->f_pos = offset; | |
1884 | file->f_version = 0; | |
1885 | } | |
1886 | mutex_unlock(&inode->i_mutex); | |
1887 | return offset; | |
1888 | } | |
1889 | ||
83e4fa9c HD |
1890 | static long shmem_fallocate(struct file *file, int mode, loff_t offset, |
1891 | loff_t len) | |
1892 | { | |
496ad9aa | 1893 | struct inode *inode = file_inode(file); |
e2d12e22 | 1894 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
1aac1400 | 1895 | struct shmem_falloc shmem_falloc; |
e2d12e22 HD |
1896 | pgoff_t start, index, end; |
1897 | int error; | |
83e4fa9c HD |
1898 | |
1899 | mutex_lock(&inode->i_mutex); | |
1900 | ||
1901 | if (mode & FALLOC_FL_PUNCH_HOLE) { | |
1902 | struct address_space *mapping = file->f_mapping; | |
1903 | loff_t unmap_start = round_up(offset, PAGE_SIZE); | |
1904 | loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; | |
887675c9 | 1905 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); |
83e4fa9c | 1906 | |
887675c9 | 1907 | shmem_falloc.waitq = &shmem_falloc_waitq; |
1ccc3ffa HD |
1908 | shmem_falloc.start = unmap_start >> PAGE_SHIFT; |
1909 | shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; | |
1910 | spin_lock(&inode->i_lock); | |
1911 | inode->i_private = &shmem_falloc; | |
1912 | spin_unlock(&inode->i_lock); | |
1913 | ||
83e4fa9c HD |
1914 | if ((u64)unmap_end > (u64)unmap_start) |
1915 | unmap_mapping_range(mapping, unmap_start, | |
1916 | 1 + unmap_end - unmap_start, 0); | |
1917 | shmem_truncate_range(inode, offset, offset + len - 1); | |
1918 | /* No need to unmap again: hole-punching leaves COWed pages */ | |
887675c9 HD |
1919 | |
1920 | spin_lock(&inode->i_lock); | |
1921 | inode->i_private = NULL; | |
1922 | wake_up_all(&shmem_falloc_waitq); | |
1923 | spin_unlock(&inode->i_lock); | |
83e4fa9c | 1924 | error = 0; |
887675c9 | 1925 | goto out; |
e2d12e22 HD |
1926 | } |
1927 | ||
1928 | /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ | |
1929 | error = inode_newsize_ok(inode, offset + len); | |
1930 | if (error) | |
1931 | goto out; | |
1932 | ||
1933 | start = offset >> PAGE_CACHE_SHIFT; | |
1934 | end = (offset + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | |
1935 | /* Try to avoid a swapstorm if len is impossible to satisfy */ | |
1936 | if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { | |
1937 | error = -ENOSPC; | |
1938 | goto out; | |
83e4fa9c HD |
1939 | } |
1940 | ||
887675c9 | 1941 | shmem_falloc.waitq = NULL; |
1aac1400 HD |
1942 | shmem_falloc.start = start; |
1943 | shmem_falloc.next = start; | |
1944 | shmem_falloc.nr_falloced = 0; | |
1945 | shmem_falloc.nr_unswapped = 0; | |
1946 | spin_lock(&inode->i_lock); | |
1947 | inode->i_private = &shmem_falloc; | |
1948 | spin_unlock(&inode->i_lock); | |
1949 | ||
e2d12e22 HD |
1950 | for (index = start; index < end; index++) { |
1951 | struct page *page; | |
1952 | ||
1953 | /* | |
1954 | * Good, the fallocate(2) manpage permits EINTR: we may have | |
1955 | * been interrupted because we are using up too much memory. | |
1956 | */ | |
1957 | if (signal_pending(current)) | |
1958 | error = -EINTR; | |
1aac1400 HD |
1959 | else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) |
1960 | error = -ENOMEM; | |
e2d12e22 | 1961 | else |
1635f6a7 | 1962 | error = shmem_getpage(inode, index, &page, SGP_FALLOC, |
e2d12e22 HD |
1963 | NULL); |
1964 | if (error) { | |
1635f6a7 | 1965 | /* Remove the !PageUptodate pages we added */ |
42a22cd4 HD |
1966 | if (index > start) { |
1967 | shmem_undo_range(inode, | |
1968 | (loff_t)start << PAGE_CACHE_SHIFT, | |
1969 | ((loff_t)index << PAGE_CACHE_SHIFT) - 1, true); | |
1970 | } | |
1aac1400 | 1971 | goto undone; |
e2d12e22 HD |
1972 | } |
1973 | ||
1aac1400 HD |
1974 | /* |
1975 | * Inform shmem_writepage() how far we have reached. | |
1976 | * No need for lock or barrier: we have the page lock. | |
1977 | */ | |
1978 | shmem_falloc.next++; | |
1979 | if (!PageUptodate(page)) | |
1980 | shmem_falloc.nr_falloced++; | |
1981 | ||
e2d12e22 | 1982 | /* |
1635f6a7 HD |
1983 | * If !PageUptodate, leave it that way so that freeable pages |
1984 | * can be recognized if we need to rollback on error later. | |
1985 | * But set_page_dirty so that memory pressure will swap rather | |
e2d12e22 HD |
1986 | * than free the pages we are allocating (and SGP_CACHE pages |
1987 | * might still be clean: we now need to mark those dirty too). | |
1988 | */ | |
1989 | set_page_dirty(page); | |
1990 | unlock_page(page); | |
1991 | page_cache_release(page); | |
1992 | cond_resched(); | |
1993 | } | |
1994 | ||
1995 | if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) | |
1996 | i_size_write(inode, offset + len); | |
e2d12e22 | 1997 | inode->i_ctime = CURRENT_TIME; |
1aac1400 HD |
1998 | undone: |
1999 | spin_lock(&inode->i_lock); | |
2000 | inode->i_private = NULL; | |
2001 | spin_unlock(&inode->i_lock); | |
e2d12e22 | 2002 | out: |
83e4fa9c HD |
2003 | mutex_unlock(&inode->i_mutex); |
2004 | return error; | |
2005 | } | |
2006 | ||
726c3342 | 2007 | static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) |
1da177e4 | 2008 | { |
726c3342 | 2009 | struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); |
1da177e4 LT |
2010 | |
2011 | buf->f_type = TMPFS_MAGIC; | |
2012 | buf->f_bsize = PAGE_CACHE_SIZE; | |
2013 | buf->f_namelen = NAME_MAX; | |
0edd73b3 | 2014 | if (sbinfo->max_blocks) { |
1da177e4 | 2015 | buf->f_blocks = sbinfo->max_blocks; |
41ffe5d5 HD |
2016 | buf->f_bavail = |
2017 | buf->f_bfree = sbinfo->max_blocks - | |
2018 | percpu_counter_sum(&sbinfo->used_blocks); | |
0edd73b3 HD |
2019 | } |
2020 | if (sbinfo->max_inodes) { | |
1da177e4 LT |
2021 | buf->f_files = sbinfo->max_inodes; |
2022 | buf->f_ffree = sbinfo->free_inodes; | |
1da177e4 LT |
2023 | } |
2024 | /* else leave those fields 0 like simple_statfs */ | |
2025 | return 0; | |
2026 | } | |
2027 | ||
2028 | /* | |
2029 | * File creation. Allocate an inode, and we're done.. | |
2030 | */ | |
2031 | static int | |
1a67aafb | 2032 | shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) |
1da177e4 | 2033 | { |
0b0a0806 | 2034 | struct inode *inode; |
1da177e4 LT |
2035 | int error = -ENOSPC; |
2036 | ||
454abafe | 2037 | inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); |
1da177e4 | 2038 | if (inode) { |
2a7dba39 | 2039 | error = security_inode_init_security(inode, dir, |
9d8f13ba | 2040 | &dentry->d_name, |
6d9d88d0 | 2041 | shmem_initxattrs, NULL); |
570bc1c2 SS |
2042 | if (error) { |
2043 | if (error != -EOPNOTSUPP) { | |
2044 | iput(inode); | |
2045 | return error; | |
2046 | } | |
39f0247d | 2047 | } |
1c7c474c CH |
2048 | #ifdef CONFIG_TMPFS_POSIX_ACL |
2049 | error = generic_acl_init(inode, dir); | |
39f0247d AG |
2050 | if (error) { |
2051 | iput(inode); | |
2052 | return error; | |
570bc1c2 | 2053 | } |
718deb6b AV |
2054 | #else |
2055 | error = 0; | |
1c7c474c | 2056 | #endif |
1da177e4 LT |
2057 | dir->i_size += BOGO_DIRENT_SIZE; |
2058 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; | |
2059 | d_instantiate(dentry, inode); | |
2060 | dget(dentry); /* Extra count - pin the dentry in core */ | |
1da177e4 LT |
2061 | } |
2062 | return error; | |
2063 | } | |
2064 | ||
aa8cc8ee AV |
2065 | static int |
2066 | shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) | |
2067 | { | |
2068 | struct inode *inode; | |
2069 | int error = -ENOSPC; | |
2070 | ||
2071 | inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE); | |
2072 | if (inode) { | |
2073 | error = security_inode_init_security(inode, dir, | |
2074 | NULL, | |
2075 | shmem_initxattrs, NULL); | |
2076 | if (error) { | |
2077 | if (error != -EOPNOTSUPP) { | |
2078 | iput(inode); | |
2079 | return error; | |
2080 | } | |
2081 | } | |
2082 | #ifdef CONFIG_TMPFS_POSIX_ACL | |
2083 | error = generic_acl_init(inode, dir); | |
2084 | if (error) { | |
2085 | iput(inode); | |
2086 | return error; | |
2087 | } | |
2088 | #else | |
2089 | error = 0; | |
2090 | #endif | |
2091 | d_tmpfile(dentry, inode); | |
2092 | } | |
2093 | return error; | |
2094 | } | |
2095 | ||
18bb1db3 | 2096 | static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
1da177e4 LT |
2097 | { |
2098 | int error; | |
2099 | ||
2100 | if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) | |
2101 | return error; | |
d8c76e6f | 2102 | inc_nlink(dir); |
1da177e4 LT |
2103 | return 0; |
2104 | } | |
2105 | ||
4acdaf27 | 2106 | static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
ebfc3b49 | 2107 | bool excl) |
1da177e4 LT |
2108 | { |
2109 | return shmem_mknod(dir, dentry, mode | S_IFREG, 0); | |
2110 | } | |
2111 | ||
2112 | /* | |
2113 | * Link a file.. | |
2114 | */ | |
2115 | static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) | |
2116 | { | |
2117 | struct inode *inode = old_dentry->d_inode; | |
5b04c689 | 2118 | int ret; |
1da177e4 LT |
2119 | |
2120 | /* | |
2121 | * No ordinary (disk based) filesystem counts links as inodes; | |
2122 | * but each new link needs a new dentry, pinning lowmem, and | |
2123 | * tmpfs dentries cannot be pruned until they are unlinked. | |
2124 | */ | |
5b04c689 PE |
2125 | ret = shmem_reserve_inode(inode->i_sb); |
2126 | if (ret) | |
2127 | goto out; | |
1da177e4 LT |
2128 | |
2129 | dir->i_size += BOGO_DIRENT_SIZE; | |
2130 | inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; | |
d8c76e6f | 2131 | inc_nlink(inode); |
7de9c6ee | 2132 | ihold(inode); /* New dentry reference */ |
1da177e4 LT |
2133 | dget(dentry); /* Extra pinning count for the created dentry */ |
2134 | d_instantiate(dentry, inode); | |
5b04c689 PE |
2135 | out: |
2136 | return ret; | |
1da177e4 LT |
2137 | } |
2138 | ||
2139 | static int shmem_unlink(struct inode *dir, struct dentry *dentry) | |
2140 | { | |
2141 | struct inode *inode = dentry->d_inode; | |
2142 | ||
5b04c689 PE |
2143 | if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) |
2144 | shmem_free_inode(inode->i_sb); | |
1da177e4 LT |
2145 | |
2146 | dir->i_size -= BOGO_DIRENT_SIZE; | |
2147 | inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; | |
9a53c3a7 | 2148 | drop_nlink(inode); |
1da177e4 LT |
2149 | dput(dentry); /* Undo the count from "create" - this does all the work */ |
2150 | return 0; | |
2151 | } | |
2152 | ||
2153 | static int shmem_rmdir(struct inode *dir, struct dentry *dentry) | |
2154 | { | |
2155 | if (!simple_empty(dentry)) | |
2156 | return -ENOTEMPTY; | |
2157 | ||
9a53c3a7 DH |
2158 | drop_nlink(dentry->d_inode); |
2159 | drop_nlink(dir); | |
1da177e4 LT |
2160 | return shmem_unlink(dir, dentry); |
2161 | } | |
2162 | ||
2163 | /* | |
2164 | * The VFS layer already does all the dentry stuff for rename, | |
2165 | * we just have to decrement the usage count for the target if | |
2166 | * it exists so that the VFS layer correctly free's it when it | |
2167 | * gets overwritten. | |
2168 | */ | |
2169 | static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) | |
2170 | { | |
2171 | struct inode *inode = old_dentry->d_inode; | |
2172 | int they_are_dirs = S_ISDIR(inode->i_mode); | |
2173 | ||
2174 | if (!simple_empty(new_dentry)) | |
2175 | return -ENOTEMPTY; | |
2176 | ||
2177 | if (new_dentry->d_inode) { | |
2178 | (void) shmem_unlink(new_dir, new_dentry); | |
474740b9 MS |
2179 | if (they_are_dirs) { |
2180 | drop_nlink(new_dentry->d_inode); | |
9a53c3a7 | 2181 | drop_nlink(old_dir); |
474740b9 | 2182 | } |
1da177e4 | 2183 | } else if (they_are_dirs) { |
9a53c3a7 | 2184 | drop_nlink(old_dir); |
d8c76e6f | 2185 | inc_nlink(new_dir); |
1da177e4 LT |
2186 | } |
2187 | ||
2188 | old_dir->i_size -= BOGO_DIRENT_SIZE; | |
2189 | new_dir->i_size += BOGO_DIRENT_SIZE; | |
2190 | old_dir->i_ctime = old_dir->i_mtime = | |
2191 | new_dir->i_ctime = new_dir->i_mtime = | |
2192 | inode->i_ctime = CURRENT_TIME; | |
2193 | return 0; | |
2194 | } | |
2195 | ||
2196 | static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) | |
2197 | { | |
2198 | int error; | |
2199 | int len; | |
2200 | struct inode *inode; | |
9276aad6 | 2201 | struct page *page; |
1da177e4 LT |
2202 | char *kaddr; |
2203 | struct shmem_inode_info *info; | |
2204 | ||
2205 | len = strlen(symname) + 1; | |
2206 | if (len > PAGE_CACHE_SIZE) | |
2207 | return -ENAMETOOLONG; | |
2208 | ||
454abafe | 2209 | inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE); |
1da177e4 LT |
2210 | if (!inode) |
2211 | return -ENOSPC; | |
2212 | ||
9d8f13ba | 2213 | error = security_inode_init_security(inode, dir, &dentry->d_name, |
6d9d88d0 | 2214 | shmem_initxattrs, NULL); |
570bc1c2 SS |
2215 | if (error) { |
2216 | if (error != -EOPNOTSUPP) { | |
2217 | iput(inode); | |
2218 | return error; | |
2219 | } | |
2220 | error = 0; | |
2221 | } | |
2222 | ||
1da177e4 LT |
2223 | info = SHMEM_I(inode); |
2224 | inode->i_size = len-1; | |
69f07ec9 HD |
2225 | if (len <= SHORT_SYMLINK_LEN) { |
2226 | info->symlink = kmemdup(symname, len, GFP_KERNEL); | |
2227 | if (!info->symlink) { | |
2228 | iput(inode); | |
2229 | return -ENOMEM; | |
2230 | } | |
2231 | inode->i_op = &shmem_short_symlink_operations; | |
1da177e4 LT |
2232 | } else { |
2233 | error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL); | |
2234 | if (error) { | |
2235 | iput(inode); | |
2236 | return error; | |
2237 | } | |
14fcc23f | 2238 | inode->i_mapping->a_ops = &shmem_aops; |
1da177e4 | 2239 | inode->i_op = &shmem_symlink_inode_operations; |
9b04c5fe | 2240 | kaddr = kmap_atomic(page); |
1da177e4 | 2241 | memcpy(kaddr, symname, len); |
9b04c5fe | 2242 | kunmap_atomic(kaddr); |
ec9516fb | 2243 | SetPageUptodate(page); |
1da177e4 | 2244 | set_page_dirty(page); |
6746aff7 | 2245 | unlock_page(page); |
1da177e4 LT |
2246 | page_cache_release(page); |
2247 | } | |
1da177e4 LT |
2248 | dir->i_size += BOGO_DIRENT_SIZE; |
2249 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; | |
2250 | d_instantiate(dentry, inode); | |
2251 | dget(dentry); | |
2252 | return 0; | |
2253 | } | |
2254 | ||
69f07ec9 | 2255 | static void *shmem_follow_short_symlink(struct dentry *dentry, struct nameidata *nd) |
1da177e4 | 2256 | { |
69f07ec9 | 2257 | nd_set_link(nd, SHMEM_I(dentry->d_inode)->symlink); |
cc314eef | 2258 | return NULL; |
1da177e4 LT |
2259 | } |
2260 | ||
cc314eef | 2261 | static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd) |
1da177e4 LT |
2262 | { |
2263 | struct page *page = NULL; | |
41ffe5d5 HD |
2264 | int error = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL); |
2265 | nd_set_link(nd, error ? ERR_PTR(error) : kmap(page)); | |
d3602444 HD |
2266 | if (page) |
2267 | unlock_page(page); | |
cc314eef | 2268 | return page; |
1da177e4 LT |
2269 | } |
2270 | ||
cc314eef | 2271 | static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) |
1da177e4 LT |
2272 | { |
2273 | if (!IS_ERR(nd_get_link(nd))) { | |
cc314eef | 2274 | struct page *page = cookie; |
1da177e4 LT |
2275 | kunmap(page); |
2276 | mark_page_accessed(page); | |
2277 | page_cache_release(page); | |
1da177e4 LT |
2278 | } |
2279 | } | |
2280 | ||
b09e0fa4 | 2281 | #ifdef CONFIG_TMPFS_XATTR |
46711810 | 2282 | /* |
b09e0fa4 EP |
2283 | * Superblocks without xattr inode operations may get some security.* xattr |
2284 | * support from the LSM "for free". As soon as we have any other xattrs | |
39f0247d AG |
2285 | * like ACLs, we also need to implement the security.* handlers at |
2286 | * filesystem level, though. | |
2287 | */ | |
2288 | ||
6d9d88d0 JS |
2289 | /* |
2290 | * Callback for security_inode_init_security() for acquiring xattrs. | |
2291 | */ | |
2292 | static int shmem_initxattrs(struct inode *inode, | |
2293 | const struct xattr *xattr_array, | |
2294 | void *fs_info) | |
2295 | { | |
2296 | struct shmem_inode_info *info = SHMEM_I(inode); | |
2297 | const struct xattr *xattr; | |
38f38657 | 2298 | struct simple_xattr *new_xattr; |
6d9d88d0 JS |
2299 | size_t len; |
2300 | ||
2301 | for (xattr = xattr_array; xattr->name != NULL; xattr++) { | |
38f38657 | 2302 | new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); |
6d9d88d0 JS |
2303 | if (!new_xattr) |
2304 | return -ENOMEM; | |
2305 | ||
2306 | len = strlen(xattr->name) + 1; | |
2307 | new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, | |
2308 | GFP_KERNEL); | |
2309 | if (!new_xattr->name) { | |
2310 | kfree(new_xattr); | |
2311 | return -ENOMEM; | |
2312 | } | |
2313 | ||
2314 | memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, | |
2315 | XATTR_SECURITY_PREFIX_LEN); | |
2316 | memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, | |
2317 | xattr->name, len); | |
2318 | ||
38f38657 | 2319 | simple_xattr_list_add(&info->xattrs, new_xattr); |
6d9d88d0 JS |
2320 | } |
2321 | ||
2322 | return 0; | |
2323 | } | |
2324 | ||
bb435453 | 2325 | static const struct xattr_handler *shmem_xattr_handlers[] = { |
b09e0fa4 | 2326 | #ifdef CONFIG_TMPFS_POSIX_ACL |
1c7c474c CH |
2327 | &generic_acl_access_handler, |
2328 | &generic_acl_default_handler, | |
b09e0fa4 | 2329 | #endif |
39f0247d AG |
2330 | NULL |
2331 | }; | |
b09e0fa4 EP |
2332 | |
2333 | static int shmem_xattr_validate(const char *name) | |
2334 | { | |
2335 | struct { const char *prefix; size_t len; } arr[] = { | |
2336 | { XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN }, | |
2337 | { XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN } | |
2338 | }; | |
2339 | int i; | |
2340 | ||
2341 | for (i = 0; i < ARRAY_SIZE(arr); i++) { | |
2342 | size_t preflen = arr[i].len; | |
2343 | if (strncmp(name, arr[i].prefix, preflen) == 0) { | |
2344 | if (!name[preflen]) | |
2345 | return -EINVAL; | |
2346 | return 0; | |
2347 | } | |
2348 | } | |
2349 | return -EOPNOTSUPP; | |
2350 | } | |
2351 | ||
2352 | static ssize_t shmem_getxattr(struct dentry *dentry, const char *name, | |
2353 | void *buffer, size_t size) | |
2354 | { | |
38f38657 | 2355 | struct shmem_inode_info *info = SHMEM_I(dentry->d_inode); |
b09e0fa4 EP |
2356 | int err; |
2357 | ||
2358 | /* | |
2359 | * If this is a request for a synthetic attribute in the system.* | |
2360 | * namespace use the generic infrastructure to resolve a handler | |
2361 | * for it via sb->s_xattr. | |
2362 | */ | |
2363 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) | |
2364 | return generic_getxattr(dentry, name, buffer, size); | |
2365 | ||
2366 | err = shmem_xattr_validate(name); | |
2367 | if (err) | |
2368 | return err; | |
2369 | ||
38f38657 | 2370 | return simple_xattr_get(&info->xattrs, name, buffer, size); |
b09e0fa4 EP |
2371 | } |
2372 | ||
2373 | static int shmem_setxattr(struct dentry *dentry, const char *name, | |
2374 | const void *value, size_t size, int flags) | |
2375 | { | |
38f38657 | 2376 | struct shmem_inode_info *info = SHMEM_I(dentry->d_inode); |
b09e0fa4 EP |
2377 | int err; |
2378 | ||
2379 | /* | |
2380 | * If this is a request for a synthetic attribute in the system.* | |
2381 | * namespace use the generic infrastructure to resolve a handler | |
2382 | * for it via sb->s_xattr. | |
2383 | */ | |
2384 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) | |
2385 | return generic_setxattr(dentry, name, value, size, flags); | |
2386 | ||
2387 | err = shmem_xattr_validate(name); | |
2388 | if (err) | |
2389 | return err; | |
2390 | ||
38f38657 | 2391 | return simple_xattr_set(&info->xattrs, name, value, size, flags); |
b09e0fa4 EP |
2392 | } |
2393 | ||
2394 | static int shmem_removexattr(struct dentry *dentry, const char *name) | |
2395 | { | |
38f38657 | 2396 | struct shmem_inode_info *info = SHMEM_I(dentry->d_inode); |
b09e0fa4 EP |
2397 | int err; |
2398 | ||
2399 | /* | |
2400 | * If this is a request for a synthetic attribute in the system.* | |
2401 | * namespace use the generic infrastructure to resolve a handler | |
2402 | * for it via sb->s_xattr. | |
2403 | */ | |
2404 | if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) | |
2405 | return generic_removexattr(dentry, name); | |
2406 | ||
2407 | err = shmem_xattr_validate(name); | |
2408 | if (err) | |
2409 | return err; | |
2410 | ||
38f38657 | 2411 | return simple_xattr_remove(&info->xattrs, name); |
b09e0fa4 EP |
2412 | } |
2413 | ||
2414 | static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) | |
2415 | { | |
38f38657 AR |
2416 | struct shmem_inode_info *info = SHMEM_I(dentry->d_inode); |
2417 | return simple_xattr_list(&info->xattrs, buffer, size); | |
b09e0fa4 EP |
2418 | } |
2419 | #endif /* CONFIG_TMPFS_XATTR */ | |
2420 | ||
69f07ec9 | 2421 | static const struct inode_operations shmem_short_symlink_operations = { |
b09e0fa4 | 2422 | .readlink = generic_readlink, |
69f07ec9 | 2423 | .follow_link = shmem_follow_short_symlink, |
b09e0fa4 EP |
2424 | #ifdef CONFIG_TMPFS_XATTR |
2425 | .setxattr = shmem_setxattr, | |
2426 | .getxattr = shmem_getxattr, | |
2427 | .listxattr = shmem_listxattr, | |
2428 | .removexattr = shmem_removexattr, | |
2429 | #endif | |
2430 | }; | |
2431 | ||
2432 | static const struct inode_operations shmem_symlink_inode_operations = { | |
2433 | .readlink = generic_readlink, | |
2434 | .follow_link = shmem_follow_link, | |
2435 | .put_link = shmem_put_link, | |
2436 | #ifdef CONFIG_TMPFS_XATTR | |
2437 | .setxattr = shmem_setxattr, | |
2438 | .getxattr = shmem_getxattr, | |
2439 | .listxattr = shmem_listxattr, | |
2440 | .removexattr = shmem_removexattr, | |
39f0247d | 2441 | #endif |
b09e0fa4 | 2442 | }; |
39f0247d | 2443 | |
91828a40 DG |
2444 | static struct dentry *shmem_get_parent(struct dentry *child) |
2445 | { | |
2446 | return ERR_PTR(-ESTALE); | |
2447 | } | |
2448 | ||
2449 | static int shmem_match(struct inode *ino, void *vfh) | |
2450 | { | |
2451 | __u32 *fh = vfh; | |
2452 | __u64 inum = fh[2]; | |
2453 | inum = (inum << 32) | fh[1]; | |
2454 | return ino->i_ino == inum && fh[0] == ino->i_generation; | |
2455 | } | |
2456 | ||
480b116c CH |
2457 | static struct dentry *shmem_fh_to_dentry(struct super_block *sb, |
2458 | struct fid *fid, int fh_len, int fh_type) | |
91828a40 | 2459 | { |
91828a40 | 2460 | struct inode *inode; |
480b116c | 2461 | struct dentry *dentry = NULL; |
35c2a7f4 | 2462 | u64 inum; |
480b116c CH |
2463 | |
2464 | if (fh_len < 3) | |
2465 | return NULL; | |
91828a40 | 2466 | |
35c2a7f4 HD |
2467 | inum = fid->raw[2]; |
2468 | inum = (inum << 32) | fid->raw[1]; | |
2469 | ||
480b116c CH |
2470 | inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), |
2471 | shmem_match, fid->raw); | |
91828a40 | 2472 | if (inode) { |
480b116c | 2473 | dentry = d_find_alias(inode); |
91828a40 DG |
2474 | iput(inode); |
2475 | } | |
2476 | ||
480b116c | 2477 | return dentry; |
91828a40 DG |
2478 | } |
2479 | ||
b0b0382b AV |
2480 | static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, |
2481 | struct inode *parent) | |
91828a40 | 2482 | { |
5fe0c237 AK |
2483 | if (*len < 3) { |
2484 | *len = 3; | |
94e07a75 | 2485 | return FILEID_INVALID; |
5fe0c237 | 2486 | } |
91828a40 | 2487 | |
1d3382cb | 2488 | if (inode_unhashed(inode)) { |
91828a40 DG |
2489 | /* Unfortunately insert_inode_hash is not idempotent, |
2490 | * so as we hash inodes here rather than at creation | |
2491 | * time, we need a lock to ensure we only try | |
2492 | * to do it once | |
2493 | */ | |
2494 | static DEFINE_SPINLOCK(lock); | |
2495 | spin_lock(&lock); | |
1d3382cb | 2496 | if (inode_unhashed(inode)) |
91828a40 DG |
2497 | __insert_inode_hash(inode, |
2498 | inode->i_ino + inode->i_generation); | |
2499 | spin_unlock(&lock); | |
2500 | } | |
2501 | ||
2502 | fh[0] = inode->i_generation; | |
2503 | fh[1] = inode->i_ino; | |
2504 | fh[2] = ((__u64)inode->i_ino) >> 32; | |
2505 | ||
2506 | *len = 3; | |
2507 | return 1; | |
2508 | } | |
2509 | ||
39655164 | 2510 | static const struct export_operations shmem_export_ops = { |
91828a40 | 2511 | .get_parent = shmem_get_parent, |
91828a40 | 2512 | .encode_fh = shmem_encode_fh, |
480b116c | 2513 | .fh_to_dentry = shmem_fh_to_dentry, |
91828a40 DG |
2514 | }; |
2515 | ||
680d794b | 2516 | static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, |
2517 | bool remount) | |
1da177e4 LT |
2518 | { |
2519 | char *this_char, *value, *rest; | |
49cd0a5c | 2520 | struct mempolicy *mpol = NULL; |
8751e039 EB |
2521 | uid_t uid; |
2522 | gid_t gid; | |
1da177e4 | 2523 | |
b00dc3ad HD |
2524 | while (options != NULL) { |
2525 | this_char = options; | |
2526 | for (;;) { | |
2527 | /* | |
2528 | * NUL-terminate this option: unfortunately, | |
2529 | * mount options form a comma-separated list, | |
2530 | * but mpol's nodelist may also contain commas. | |
2531 | */ | |
2532 | options = strchr(options, ','); | |
2533 | if (options == NULL) | |
2534 | break; | |
2535 | options++; | |
2536 | if (!isdigit(*options)) { | |
2537 | options[-1] = '\0'; | |
2538 | break; | |
2539 | } | |
2540 | } | |
1da177e4 LT |
2541 | if (!*this_char) |
2542 | continue; | |
2543 | if ((value = strchr(this_char,'=')) != NULL) { | |
2544 | *value++ = 0; | |
2545 | } else { | |
2546 | printk(KERN_ERR | |
2547 | "tmpfs: No value for mount option '%s'\n", | |
2548 | this_char); | |
49cd0a5c | 2549 | goto error; |
1da177e4 LT |
2550 | } |
2551 | ||
2552 | if (!strcmp(this_char,"size")) { | |
2553 | unsigned long long size; | |
2554 | size = memparse(value,&rest); | |
2555 | if (*rest == '%') { | |
2556 | size <<= PAGE_SHIFT; | |
2557 | size *= totalram_pages; | |
2558 | do_div(size, 100); | |
2559 | rest++; | |
2560 | } | |
2561 | if (*rest) | |
2562 | goto bad_val; | |
680d794b | 2563 | sbinfo->max_blocks = |
2564 | DIV_ROUND_UP(size, PAGE_CACHE_SIZE); | |
1da177e4 | 2565 | } else if (!strcmp(this_char,"nr_blocks")) { |
680d794b | 2566 | sbinfo->max_blocks = memparse(value, &rest); |
1da177e4 LT |
2567 | if (*rest) |
2568 | goto bad_val; | |
2569 | } else if (!strcmp(this_char,"nr_inodes")) { | |
680d794b | 2570 | sbinfo->max_inodes = memparse(value, &rest); |
1da177e4 LT |
2571 | if (*rest) |
2572 | goto bad_val; | |
2573 | } else if (!strcmp(this_char,"mode")) { | |
680d794b | 2574 | if (remount) |
1da177e4 | 2575 | continue; |
680d794b | 2576 | sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; |
1da177e4 LT |
2577 | if (*rest) |
2578 | goto bad_val; | |
2579 | } else if (!strcmp(this_char,"uid")) { | |
680d794b | 2580 | if (remount) |
1da177e4 | 2581 | continue; |
8751e039 | 2582 | uid = simple_strtoul(value, &rest, 0); |
1da177e4 LT |
2583 | if (*rest) |
2584 | goto bad_val; | |
8751e039 EB |
2585 | sbinfo->uid = make_kuid(current_user_ns(), uid); |
2586 | if (!uid_valid(sbinfo->uid)) | |
2587 | goto bad_val; | |
1da177e4 | 2588 | } else if (!strcmp(this_char,"gid")) { |
680d794b | 2589 | if (remount) |
1da177e4 | 2590 | continue; |
8751e039 | 2591 | gid = simple_strtoul(value, &rest, 0); |
1da177e4 LT |
2592 | if (*rest) |
2593 | goto bad_val; | |
8751e039 EB |
2594 | sbinfo->gid = make_kgid(current_user_ns(), gid); |
2595 | if (!gid_valid(sbinfo->gid)) | |
2596 | goto bad_val; | |
7339ff83 | 2597 | } else if (!strcmp(this_char,"mpol")) { |
49cd0a5c GT |
2598 | mpol_put(mpol); |
2599 | mpol = NULL; | |
2600 | if (mpol_parse_str(value, &mpol)) | |
7339ff83 | 2601 | goto bad_val; |
1da177e4 LT |
2602 | } else { |
2603 | printk(KERN_ERR "tmpfs: Bad mount option %s\n", | |
2604 | this_char); | |
49cd0a5c | 2605 | goto error; |
1da177e4 LT |
2606 | } |
2607 | } | |
49cd0a5c | 2608 | sbinfo->mpol = mpol; |
1da177e4 LT |
2609 | return 0; |
2610 | ||
2611 | bad_val: | |
2612 | printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n", | |
2613 | value, this_char); | |
49cd0a5c GT |
2614 | error: |
2615 | mpol_put(mpol); | |
1da177e4 LT |
2616 | return 1; |
2617 | ||
2618 | } | |
2619 | ||
2620 | static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) | |
2621 | { | |
2622 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); | |
680d794b | 2623 | struct shmem_sb_info config = *sbinfo; |
0edd73b3 HD |
2624 | unsigned long inodes; |
2625 | int error = -EINVAL; | |
2626 | ||
5f00110f | 2627 | config.mpol = NULL; |
680d794b | 2628 | if (shmem_parse_options(data, &config, true)) |
0edd73b3 | 2629 | return error; |
1da177e4 | 2630 | |
0edd73b3 | 2631 | spin_lock(&sbinfo->stat_lock); |
0edd73b3 | 2632 | inodes = sbinfo->max_inodes - sbinfo->free_inodes; |
7e496299 | 2633 | if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) |
0edd73b3 | 2634 | goto out; |
680d794b | 2635 | if (config.max_inodes < inodes) |
0edd73b3 HD |
2636 | goto out; |
2637 | /* | |
54af6042 | 2638 | * Those tests disallow limited->unlimited while any are in use; |
0edd73b3 HD |
2639 | * but we must separately disallow unlimited->limited, because |
2640 | * in that case we have no record of how much is already in use. | |
2641 | */ | |
680d794b | 2642 | if (config.max_blocks && !sbinfo->max_blocks) |
0edd73b3 | 2643 | goto out; |
680d794b | 2644 | if (config.max_inodes && !sbinfo->max_inodes) |
0edd73b3 HD |
2645 | goto out; |
2646 | ||
2647 | error = 0; | |
680d794b | 2648 | sbinfo->max_blocks = config.max_blocks; |
680d794b | 2649 | sbinfo->max_inodes = config.max_inodes; |
2650 | sbinfo->free_inodes = config.max_inodes - inodes; | |
71fe804b | 2651 | |
5f00110f GT |
2652 | /* |
2653 | * Preserve previous mempolicy unless mpol remount option was specified. | |
2654 | */ | |
2655 | if (config.mpol) { | |
2656 | mpol_put(sbinfo->mpol); | |
2657 | sbinfo->mpol = config.mpol; /* transfers initial ref */ | |
2658 | } | |
0edd73b3 HD |
2659 | out: |
2660 | spin_unlock(&sbinfo->stat_lock); | |
2661 | return error; | |
1da177e4 | 2662 | } |
680d794b | 2663 | |
34c80b1d | 2664 | static int shmem_show_options(struct seq_file *seq, struct dentry *root) |
680d794b | 2665 | { |
34c80b1d | 2666 | struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); |
680d794b | 2667 | |
2668 | if (sbinfo->max_blocks != shmem_default_max_blocks()) | |
2669 | seq_printf(seq, ",size=%luk", | |
2670 | sbinfo->max_blocks << (PAGE_CACHE_SHIFT - 10)); | |
2671 | if (sbinfo->max_inodes != shmem_default_max_inodes()) | |
2672 | seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); | |
2673 | if (sbinfo->mode != (S_IRWXUGO | S_ISVTX)) | |
09208d15 | 2674 | seq_printf(seq, ",mode=%03ho", sbinfo->mode); |
8751e039 EB |
2675 | if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) |
2676 | seq_printf(seq, ",uid=%u", | |
2677 | from_kuid_munged(&init_user_ns, sbinfo->uid)); | |
2678 | if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) | |
2679 | seq_printf(seq, ",gid=%u", | |
2680 | from_kgid_munged(&init_user_ns, sbinfo->gid)); | |
71fe804b | 2681 | shmem_show_mpol(seq, sbinfo->mpol); |
680d794b | 2682 | return 0; |
2683 | } | |
8a09920f DH |
2684 | |
2685 | #define MFD_NAME_PREFIX "memfd:" | |
2686 | #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1) | |
2687 | #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN) | |
2688 | ||
2689 | #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING) | |
2690 | ||
2691 | SYSCALL_DEFINE2(memfd_create, | |
2692 | const char __user *, uname, | |
2693 | unsigned int, flags) | |
2694 | { | |
2695 | struct shmem_inode_info *info; | |
2696 | struct file *file; | |
2697 | int fd, error; | |
2698 | char *name; | |
2699 | long len; | |
2700 | ||
2701 | if (flags & ~(unsigned int)MFD_ALL_FLAGS) | |
2702 | return -EINVAL; | |
2703 | ||
2704 | /* length includes terminating zero */ | |
2705 | len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1); | |
2706 | if (len <= 0) | |
2707 | return -EFAULT; | |
2708 | if (len > MFD_NAME_MAX_LEN + 1) | |
2709 | return -EINVAL; | |
2710 | ||
2711 | name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY); | |
2712 | if (!name) | |
2713 | return -ENOMEM; | |
2714 | ||
2715 | strcpy(name, MFD_NAME_PREFIX); | |
2716 | if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) { | |
2717 | error = -EFAULT; | |
2718 | goto err_name; | |
2719 | } | |
2720 | ||
2721 | /* terminating-zero may have changed after strnlen_user() returned */ | |
2722 | if (name[len + MFD_NAME_PREFIX_LEN - 1]) { | |
2723 | error = -EFAULT; | |
2724 | goto err_name; | |
2725 | } | |
2726 | ||
2727 | fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0); | |
2728 | if (fd < 0) { | |
2729 | error = fd; | |
2730 | goto err_name; | |
2731 | } | |
2732 | ||
2733 | file = shmem_file_setup(name, 0, VM_NORESERVE); | |
2734 | if (IS_ERR(file)) { | |
2735 | error = PTR_ERR(file); | |
2736 | goto err_fd; | |
2737 | } | |
2738 | info = SHMEM_I(file->f_path.dentry->d_inode); | |
2739 | file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE; | |
2740 | file->f_flags |= O_RDWR | O_LARGEFILE; | |
2741 | ||
2742 | fd_install(fd, file); | |
2743 | kfree(name); | |
2744 | return fd; | |
2745 | ||
2746 | err_fd: | |
2747 | put_unused_fd(fd); | |
2748 | err_name: | |
2749 | kfree(name); | |
2750 | return error; | |
2751 | } | |
2752 | ||
680d794b | 2753 | #endif /* CONFIG_TMPFS */ |
1da177e4 LT |
2754 | |
2755 | static void shmem_put_super(struct super_block *sb) | |
2756 | { | |
602586a8 HD |
2757 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
2758 | ||
2759 | percpu_counter_destroy(&sbinfo->used_blocks); | |
49cd0a5c | 2760 | mpol_put(sbinfo->mpol); |
602586a8 | 2761 | kfree(sbinfo); |
1da177e4 LT |
2762 | sb->s_fs_info = NULL; |
2763 | } | |
2764 | ||
2b2af54a | 2765 | int shmem_fill_super(struct super_block *sb, void *data, int silent) |
1da177e4 LT |
2766 | { |
2767 | struct inode *inode; | |
0edd73b3 | 2768 | struct shmem_sb_info *sbinfo; |
680d794b | 2769 | int err = -ENOMEM; |
2770 | ||
2771 | /* Round up to L1_CACHE_BYTES to resist false sharing */ | |
425fbf04 | 2772 | sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), |
680d794b | 2773 | L1_CACHE_BYTES), GFP_KERNEL); |
2774 | if (!sbinfo) | |
2775 | return -ENOMEM; | |
2776 | ||
680d794b | 2777 | sbinfo->mode = S_IRWXUGO | S_ISVTX; |
76aac0e9 DH |
2778 | sbinfo->uid = current_fsuid(); |
2779 | sbinfo->gid = current_fsgid(); | |
680d794b | 2780 | sb->s_fs_info = sbinfo; |
1da177e4 | 2781 | |
0edd73b3 | 2782 | #ifdef CONFIG_TMPFS |
1da177e4 LT |
2783 | /* |
2784 | * Per default we only allow half of the physical ram per | |
2785 | * tmpfs instance, limiting inodes to one per page of lowmem; | |
2786 | * but the internal instance is left unlimited. | |
2787 | */ | |
2788 | if (!(sb->s_flags & MS_NOUSER)) { | |
680d794b | 2789 | sbinfo->max_blocks = shmem_default_max_blocks(); |
2790 | sbinfo->max_inodes = shmem_default_max_inodes(); | |
2791 | if (shmem_parse_options(data, sbinfo, false)) { | |
2792 | err = -EINVAL; | |
2793 | goto failed; | |
2794 | } | |
1da177e4 | 2795 | } |
91828a40 | 2796 | sb->s_export_op = &shmem_export_ops; |
2f6e38f3 | 2797 | sb->s_flags |= MS_NOSEC; |
1da177e4 LT |
2798 | #else |
2799 | sb->s_flags |= MS_NOUSER; | |
2800 | #endif | |
2801 | ||
0edd73b3 | 2802 | spin_lock_init(&sbinfo->stat_lock); |
602586a8 HD |
2803 | if (percpu_counter_init(&sbinfo->used_blocks, 0)) |
2804 | goto failed; | |
680d794b | 2805 | sbinfo->free_inodes = sbinfo->max_inodes; |
0edd73b3 | 2806 | |
285b2c4f | 2807 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
1da177e4 LT |
2808 | sb->s_blocksize = PAGE_CACHE_SIZE; |
2809 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | |
2810 | sb->s_magic = TMPFS_MAGIC; | |
2811 | sb->s_op = &shmem_ops; | |
cfd95a9c | 2812 | sb->s_time_gran = 1; |
b09e0fa4 | 2813 | #ifdef CONFIG_TMPFS_XATTR |
39f0247d | 2814 | sb->s_xattr = shmem_xattr_handlers; |
b09e0fa4 EP |
2815 | #endif |
2816 | #ifdef CONFIG_TMPFS_POSIX_ACL | |
39f0247d AG |
2817 | sb->s_flags |= MS_POSIXACL; |
2818 | #endif | |
0edd73b3 | 2819 | |
454abafe | 2820 | inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); |
1da177e4 LT |
2821 | if (!inode) |
2822 | goto failed; | |
680d794b | 2823 | inode->i_uid = sbinfo->uid; |
2824 | inode->i_gid = sbinfo->gid; | |
318ceed0 AV |
2825 | sb->s_root = d_make_root(inode); |
2826 | if (!sb->s_root) | |
48fde701 | 2827 | goto failed; |
1da177e4 LT |
2828 | return 0; |
2829 | ||
1da177e4 LT |
2830 | failed: |
2831 | shmem_put_super(sb); | |
2832 | return err; | |
2833 | } | |
2834 | ||
fcc234f8 | 2835 | static struct kmem_cache *shmem_inode_cachep; |
1da177e4 LT |
2836 | |
2837 | static struct inode *shmem_alloc_inode(struct super_block *sb) | |
2838 | { | |
41ffe5d5 HD |
2839 | struct shmem_inode_info *info; |
2840 | info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); | |
2841 | if (!info) | |
1da177e4 | 2842 | return NULL; |
41ffe5d5 | 2843 | return &info->vfs_inode; |
1da177e4 LT |
2844 | } |
2845 | ||
41ffe5d5 | 2846 | static void shmem_destroy_callback(struct rcu_head *head) |
fa0d7e3d NP |
2847 | { |
2848 | struct inode *inode = container_of(head, struct inode, i_rcu); | |
fa0d7e3d NP |
2849 | kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); |
2850 | } | |
2851 | ||
1da177e4 LT |
2852 | static void shmem_destroy_inode(struct inode *inode) |
2853 | { | |
09208d15 | 2854 | if (S_ISREG(inode->i_mode)) |
1da177e4 | 2855 | mpol_free_shared_policy(&SHMEM_I(inode)->policy); |
41ffe5d5 | 2856 | call_rcu(&inode->i_rcu, shmem_destroy_callback); |
1da177e4 LT |
2857 | } |
2858 | ||
41ffe5d5 | 2859 | static void shmem_init_inode(void *foo) |
1da177e4 | 2860 | { |
41ffe5d5 HD |
2861 | struct shmem_inode_info *info = foo; |
2862 | inode_init_once(&info->vfs_inode); | |
1da177e4 LT |
2863 | } |
2864 | ||
41ffe5d5 | 2865 | static int shmem_init_inodecache(void) |
1da177e4 LT |
2866 | { |
2867 | shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", | |
2868 | sizeof(struct shmem_inode_info), | |
41ffe5d5 | 2869 | 0, SLAB_PANIC, shmem_init_inode); |
1da177e4 LT |
2870 | return 0; |
2871 | } | |
2872 | ||
41ffe5d5 | 2873 | static void shmem_destroy_inodecache(void) |
1da177e4 | 2874 | { |
1a1d92c1 | 2875 | kmem_cache_destroy(shmem_inode_cachep); |
1da177e4 LT |
2876 | } |
2877 | ||
f5e54d6e | 2878 | static const struct address_space_operations shmem_aops = { |
1da177e4 | 2879 | .writepage = shmem_writepage, |
76719325 | 2880 | .set_page_dirty = __set_page_dirty_no_writeback, |
1da177e4 | 2881 | #ifdef CONFIG_TMPFS |
800d15a5 NP |
2882 | .write_begin = shmem_write_begin, |
2883 | .write_end = shmem_write_end, | |
1da177e4 | 2884 | #endif |
304dbdb7 | 2885 | .migratepage = migrate_page, |
aa261f54 | 2886 | .error_remove_page = generic_error_remove_page, |
1da177e4 LT |
2887 | }; |
2888 | ||
15ad7cdc | 2889 | static const struct file_operations shmem_file_operations = { |
1da177e4 LT |
2890 | .mmap = shmem_mmap, |
2891 | #ifdef CONFIG_TMPFS | |
220f2ac9 | 2892 | .llseek = shmem_file_llseek, |
bcd78e49 | 2893 | .read = do_sync_read, |
5402b976 | 2894 | .write = do_sync_write, |
bcd78e49 | 2895 | .aio_read = shmem_file_aio_read, |
5402b976 | 2896 | .aio_write = generic_file_aio_write, |
1b061d92 | 2897 | .fsync = noop_fsync, |
708e3508 | 2898 | .splice_read = shmem_file_splice_read, |
ae976416 | 2899 | .splice_write = generic_file_splice_write, |
83e4fa9c | 2900 | .fallocate = shmem_fallocate, |
1da177e4 LT |
2901 | #endif |
2902 | }; | |
2903 | ||
92e1d5be | 2904 | static const struct inode_operations shmem_inode_operations = { |
94c1e62d | 2905 | .setattr = shmem_setattr, |
b09e0fa4 EP |
2906 | #ifdef CONFIG_TMPFS_XATTR |
2907 | .setxattr = shmem_setxattr, | |
2908 | .getxattr = shmem_getxattr, | |
2909 | .listxattr = shmem_listxattr, | |
2910 | .removexattr = shmem_removexattr, | |
2911 | #endif | |
1da177e4 LT |
2912 | }; |
2913 | ||
92e1d5be | 2914 | static const struct inode_operations shmem_dir_inode_operations = { |
1da177e4 LT |
2915 | #ifdef CONFIG_TMPFS |
2916 | .create = shmem_create, | |
2917 | .lookup = simple_lookup, | |
2918 | .link = shmem_link, | |
2919 | .unlink = shmem_unlink, | |
2920 | .symlink = shmem_symlink, | |
2921 | .mkdir = shmem_mkdir, | |
2922 | .rmdir = shmem_rmdir, | |
2923 | .mknod = shmem_mknod, | |
2924 | .rename = shmem_rename, | |
aa8cc8ee | 2925 | .tmpfile = shmem_tmpfile, |
1da177e4 | 2926 | #endif |
b09e0fa4 EP |
2927 | #ifdef CONFIG_TMPFS_XATTR |
2928 | .setxattr = shmem_setxattr, | |
2929 | .getxattr = shmem_getxattr, | |
2930 | .listxattr = shmem_listxattr, | |
2931 | .removexattr = shmem_removexattr, | |
2932 | #endif | |
39f0247d | 2933 | #ifdef CONFIG_TMPFS_POSIX_ACL |
94c1e62d | 2934 | .setattr = shmem_setattr, |
39f0247d AG |
2935 | #endif |
2936 | }; | |
2937 | ||
92e1d5be | 2938 | static const struct inode_operations shmem_special_inode_operations = { |
b09e0fa4 EP |
2939 | #ifdef CONFIG_TMPFS_XATTR |
2940 | .setxattr = shmem_setxattr, | |
2941 | .getxattr = shmem_getxattr, | |
2942 | .listxattr = shmem_listxattr, | |
2943 | .removexattr = shmem_removexattr, | |
2944 | #endif | |
39f0247d | 2945 | #ifdef CONFIG_TMPFS_POSIX_ACL |
94c1e62d | 2946 | .setattr = shmem_setattr, |
39f0247d | 2947 | #endif |
1da177e4 LT |
2948 | }; |
2949 | ||
759b9775 | 2950 | static const struct super_operations shmem_ops = { |
1da177e4 LT |
2951 | .alloc_inode = shmem_alloc_inode, |
2952 | .destroy_inode = shmem_destroy_inode, | |
2953 | #ifdef CONFIG_TMPFS | |
2954 | .statfs = shmem_statfs, | |
2955 | .remount_fs = shmem_remount_fs, | |
680d794b | 2956 | .show_options = shmem_show_options, |
1da177e4 | 2957 | #endif |
1f895f75 | 2958 | .evict_inode = shmem_evict_inode, |
1da177e4 LT |
2959 | .drop_inode = generic_delete_inode, |
2960 | .put_super = shmem_put_super, | |
2961 | }; | |
2962 | ||
f0f37e2f | 2963 | static const struct vm_operations_struct shmem_vm_ops = { |
54cb8821 | 2964 | .fault = shmem_fault, |
1da177e4 LT |
2965 | #ifdef CONFIG_NUMA |
2966 | .set_policy = shmem_set_policy, | |
2967 | .get_policy = shmem_get_policy, | |
2968 | #endif | |
0b173bc4 | 2969 | .remap_pages = generic_file_remap_pages, |
1da177e4 LT |
2970 | }; |
2971 | ||
3c26ff6e AV |
2972 | static struct dentry *shmem_mount(struct file_system_type *fs_type, |
2973 | int flags, const char *dev_name, void *data) | |
1da177e4 | 2974 | { |
3c26ff6e | 2975 | return mount_nodev(fs_type, flags, data, shmem_fill_super); |
1da177e4 LT |
2976 | } |
2977 | ||
41ffe5d5 | 2978 | static struct file_system_type shmem_fs_type = { |
1da177e4 LT |
2979 | .owner = THIS_MODULE, |
2980 | .name = "tmpfs", | |
3c26ff6e | 2981 | .mount = shmem_mount, |
1da177e4 | 2982 | .kill_sb = kill_litter_super, |
2b8576cb | 2983 | .fs_flags = FS_USERNS_MOUNT, |
1da177e4 | 2984 | }; |
1da177e4 | 2985 | |
41ffe5d5 | 2986 | int __init shmem_init(void) |
1da177e4 LT |
2987 | { |
2988 | int error; | |
2989 | ||
e0bf68dd PZ |
2990 | error = bdi_init(&shmem_backing_dev_info); |
2991 | if (error) | |
2992 | goto out4; | |
2993 | ||
41ffe5d5 | 2994 | error = shmem_init_inodecache(); |
1da177e4 LT |
2995 | if (error) |
2996 | goto out3; | |
2997 | ||
41ffe5d5 | 2998 | error = register_filesystem(&shmem_fs_type); |
1da177e4 LT |
2999 | if (error) { |
3000 | printk(KERN_ERR "Could not register tmpfs\n"); | |
3001 | goto out2; | |
3002 | } | |
95dc112a | 3003 | |
41ffe5d5 HD |
3004 | shm_mnt = vfs_kern_mount(&shmem_fs_type, MS_NOUSER, |
3005 | shmem_fs_type.name, NULL); | |
1da177e4 LT |
3006 | if (IS_ERR(shm_mnt)) { |
3007 | error = PTR_ERR(shm_mnt); | |
3008 | printk(KERN_ERR "Could not kern_mount tmpfs\n"); | |
3009 | goto out1; | |
3010 | } | |
3011 | return 0; | |
3012 | ||
3013 | out1: | |
41ffe5d5 | 3014 | unregister_filesystem(&shmem_fs_type); |
1da177e4 | 3015 | out2: |
41ffe5d5 | 3016 | shmem_destroy_inodecache(); |
1da177e4 | 3017 | out3: |
e0bf68dd PZ |
3018 | bdi_destroy(&shmem_backing_dev_info); |
3019 | out4: | |
1da177e4 LT |
3020 | shm_mnt = ERR_PTR(error); |
3021 | return error; | |
3022 | } | |
853ac43a MM |
3023 | |
3024 | #else /* !CONFIG_SHMEM */ | |
3025 | ||
3026 | /* | |
3027 | * tiny-shmem: simple shmemfs and tmpfs using ramfs code | |
3028 | * | |
3029 | * This is intended for small system where the benefits of the full | |
3030 | * shmem code (swap-backed and resource-limited) are outweighed by | |
3031 | * their complexity. On systems without swap this code should be | |
3032 | * effectively equivalent, but much lighter weight. | |
3033 | */ | |
3034 | ||
41ffe5d5 | 3035 | static struct file_system_type shmem_fs_type = { |
853ac43a | 3036 | .name = "tmpfs", |
3c26ff6e | 3037 | .mount = ramfs_mount, |
853ac43a | 3038 | .kill_sb = kill_litter_super, |
2b8576cb | 3039 | .fs_flags = FS_USERNS_MOUNT, |
853ac43a MM |
3040 | }; |
3041 | ||
41ffe5d5 | 3042 | int __init shmem_init(void) |
853ac43a | 3043 | { |
41ffe5d5 | 3044 | BUG_ON(register_filesystem(&shmem_fs_type) != 0); |
853ac43a | 3045 | |
41ffe5d5 | 3046 | shm_mnt = kern_mount(&shmem_fs_type); |
853ac43a MM |
3047 | BUG_ON(IS_ERR(shm_mnt)); |
3048 | ||
3049 | return 0; | |
3050 | } | |
3051 | ||
41ffe5d5 | 3052 | int shmem_unuse(swp_entry_t swap, struct page *page) |
853ac43a MM |
3053 | { |
3054 | return 0; | |
3055 | } | |
3056 | ||
3f96b79a HD |
3057 | int shmem_lock(struct file *file, int lock, struct user_struct *user) |
3058 | { | |
3059 | return 0; | |
3060 | } | |
3061 | ||
24513264 HD |
3062 | void shmem_unlock_mapping(struct address_space *mapping) |
3063 | { | |
3064 | } | |
3065 | ||
41ffe5d5 | 3066 | void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) |
94c1e62d | 3067 | { |
41ffe5d5 | 3068 | truncate_inode_pages_range(inode->i_mapping, lstart, lend); |
94c1e62d HD |
3069 | } |
3070 | EXPORT_SYMBOL_GPL(shmem_truncate_range); | |
3071 | ||
0b0a0806 HD |
3072 | #define shmem_vm_ops generic_file_vm_ops |
3073 | #define shmem_file_operations ramfs_file_operations | |
454abafe | 3074 | #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) |
0b0a0806 HD |
3075 | #define shmem_acct_size(flags, size) 0 |
3076 | #define shmem_unacct_size(flags, size) do {} while (0) | |
853ac43a MM |
3077 | |
3078 | #endif /* CONFIG_SHMEM */ | |
3079 | ||
3080 | /* common code */ | |
1da177e4 | 3081 | |
3451538a | 3082 | static struct dentry_operations anon_ops = { |
ad4c3cc4 | 3083 | .d_dname = simple_dname |
3451538a AV |
3084 | }; |
3085 | ||
46711810 | 3086 | /** |
1da177e4 | 3087 | * shmem_file_setup - get an unlinked file living in tmpfs |
1da177e4 LT |
3088 | * @name: name for dentry (to be seen in /proc/<pid>/maps |
3089 | * @size: size to be set for the file | |
0b0a0806 | 3090 | * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size |
1da177e4 | 3091 | */ |
168f5ac6 | 3092 | struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) |
1da177e4 | 3093 | { |
6b4d0b27 | 3094 | struct file *res; |
1da177e4 | 3095 | struct inode *inode; |
2c48b9c4 | 3096 | struct path path; |
3451538a | 3097 | struct super_block *sb; |
1da177e4 LT |
3098 | struct qstr this; |
3099 | ||
3100 | if (IS_ERR(shm_mnt)) | |
6b4d0b27 | 3101 | return ERR_CAST(shm_mnt); |
1da177e4 | 3102 | |
285b2c4f | 3103 | if (size < 0 || size > MAX_LFS_FILESIZE) |
1da177e4 LT |
3104 | return ERR_PTR(-EINVAL); |
3105 | ||
3106 | if (shmem_acct_size(flags, size)) | |
3107 | return ERR_PTR(-ENOMEM); | |
3108 | ||
6b4d0b27 | 3109 | res = ERR_PTR(-ENOMEM); |
1da177e4 LT |
3110 | this.name = name; |
3111 | this.len = strlen(name); | |
3112 | this.hash = 0; /* will go */ | |
3451538a AV |
3113 | sb = shm_mnt->mnt_sb; |
3114 | path.dentry = d_alloc_pseudo(sb, &this); | |
2c48b9c4 | 3115 | if (!path.dentry) |
1da177e4 | 3116 | goto put_memory; |
3451538a | 3117 | d_set_d_op(path.dentry, &anon_ops); |
2c48b9c4 | 3118 | path.mnt = mntget(shm_mnt); |
1da177e4 | 3119 | |
6b4d0b27 | 3120 | res = ERR_PTR(-ENOSPC); |
3451538a | 3121 | inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); |
1da177e4 | 3122 | if (!inode) |
4b42af81 | 3123 | goto put_dentry; |
1da177e4 | 3124 | |
2c48b9c4 | 3125 | d_instantiate(path.dentry, inode); |
1da177e4 | 3126 | inode->i_size = size; |
6d6b77f1 | 3127 | clear_nlink(inode); /* It is unlinked */ |
26567cdb AV |
3128 | res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); |
3129 | if (IS_ERR(res)) | |
4b42af81 | 3130 | goto put_dentry; |
4b42af81 | 3131 | |
6b4d0b27 | 3132 | res = alloc_file(&path, FMODE_WRITE | FMODE_READ, |
4b42af81 | 3133 | &shmem_file_operations); |
6b4d0b27 | 3134 | if (IS_ERR(res)) |
4b42af81 AV |
3135 | goto put_dentry; |
3136 | ||
6b4d0b27 | 3137 | return res; |
1da177e4 | 3138 | |
1da177e4 | 3139 | put_dentry: |
2c48b9c4 | 3140 | path_put(&path); |
1da177e4 LT |
3141 | put_memory: |
3142 | shmem_unacct_size(flags, size); | |
6b4d0b27 | 3143 | return res; |
1da177e4 | 3144 | } |
395e0ddc | 3145 | EXPORT_SYMBOL_GPL(shmem_file_setup); |
1da177e4 | 3146 | |
3c2a0909 S |
3147 | void shmem_set_file(struct vm_area_struct *vma, struct file *file) |
3148 | { | |
3149 | if (vma->vm_file) | |
3150 | fput(vma->vm_file); | |
3151 | vma->vm_file = file; | |
3152 | vma->vm_ops = &shmem_vm_ops; | |
3153 | } | |
3154 | ||
46711810 | 3155 | /** |
1da177e4 | 3156 | * shmem_zero_setup - setup a shared anonymous mapping |
1da177e4 LT |
3157 | * @vma: the vma to be mmapped is prepared by do_mmap_pgoff |
3158 | */ | |
3159 | int shmem_zero_setup(struct vm_area_struct *vma) | |
3160 | { | |
3161 | struct file *file; | |
3162 | loff_t size = vma->vm_end - vma->vm_start; | |
3163 | ||
3164 | file = shmem_file_setup("dev/zero", size, vma->vm_flags); | |
3165 | if (IS_ERR(file)) | |
3166 | return PTR_ERR(file); | |
3167 | ||
3c2a0909 | 3168 | shmem_set_file(vma, file); |
1da177e4 LT |
3169 | return 0; |
3170 | } | |
d9d90e5e HD |
3171 | |
3172 | /** | |
3173 | * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. | |
3174 | * @mapping: the page's address_space | |
3175 | * @index: the page index | |
3176 | * @gfp: the page allocator flags to use if allocating | |
3177 | * | |
3178 | * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", | |
3179 | * with any new page allocations done using the specified allocation flags. | |
3180 | * But read_cache_page_gfp() uses the ->readpage() method: which does not | |
3181 | * suit tmpfs, since it may have pages in swapcache, and needs to find those | |
3182 | * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. | |
3183 | * | |
68da9f05 HD |
3184 | * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in |
3185 | * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. | |
d9d90e5e HD |
3186 | */ |
3187 | struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, | |
3188 | pgoff_t index, gfp_t gfp) | |
3189 | { | |
68da9f05 HD |
3190 | #ifdef CONFIG_SHMEM |
3191 | struct inode *inode = mapping->host; | |
9276aad6 | 3192 | struct page *page; |
68da9f05 HD |
3193 | int error; |
3194 | ||
3195 | BUG_ON(mapping->a_ops != &shmem_aops); | |
3196 | error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, gfp, NULL); | |
3197 | if (error) | |
3198 | page = ERR_PTR(error); | |
3199 | else | |
3200 | unlock_page(page); | |
3201 | return page; | |
3202 | #else | |
3203 | /* | |
3204 | * The tiny !SHMEM case uses ramfs without swap | |
3205 | */ | |
d9d90e5e | 3206 | return read_cache_page_gfp(mapping, index, gfp); |
68da9f05 | 3207 | #endif |
d9d90e5e HD |
3208 | } |
3209 | EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); |