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