]>
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 <[email protected]> | |
15 | * Copyright (c) 2004 Red Hat, Inc., James Morris <[email protected]> | |
16 | * | |
17 | * This file is released under the GPL. | |
18 | */ | |
19 | ||
20 | /* | |
21 | * This virtual memory filesystem is heavily based on the ramfs. It | |
22 | * extends ramfs by the ability to use swap and honor resource limits | |
23 | * which makes it a completely usable filesystem. | |
24 | */ | |
25 | ||
1da177e4 LT |
26 | #include <linux/module.h> |
27 | #include <linux/init.h> | |
1da177e4 | 28 | #include <linux/fs.h> |
39f0247d | 29 | #include <linux/xattr.h> |
a5694255 | 30 | #include <linux/exportfs.h> |
39f0247d | 31 | #include <linux/generic_acl.h> |
1da177e4 LT |
32 | #include <linux/mm.h> |
33 | #include <linux/mman.h> | |
34 | #include <linux/file.h> | |
35 | #include <linux/swap.h> | |
36 | #include <linux/pagemap.h> | |
37 | #include <linux/string.h> | |
38 | #include <linux/slab.h> | |
39 | #include <linux/backing-dev.h> | |
40 | #include <linux/shmem_fs.h> | |
41 | #include <linux/mount.h> | |
42 | #include <linux/writeback.h> | |
43 | #include <linux/vfs.h> | |
44 | #include <linux/blkdev.h> | |
45 | #include <linux/security.h> | |
46 | #include <linux/swapops.h> | |
47 | #include <linux/mempolicy.h> | |
48 | #include <linux/namei.h> | |
b00dc3ad | 49 | #include <linux/ctype.h> |
304dbdb7 | 50 | #include <linux/migrate.h> |
c1f60a5a | 51 | #include <linux/highmem.h> |
3fcfab16 | 52 | #include <linux/backing-dev.h> |
304dbdb7 | 53 | |
1da177e4 LT |
54 | #include <asm/uaccess.h> |
55 | #include <asm/div64.h> | |
56 | #include <asm/pgtable.h> | |
57 | ||
58 | /* This magic number is used in glibc for posix shared memory */ | |
59 | #define TMPFS_MAGIC 0x01021994 | |
60 | ||
61 | #define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long)) | |
62 | #define ENTRIES_PER_PAGEPAGE (ENTRIES_PER_PAGE*ENTRIES_PER_PAGE) | |
63 | #define BLOCKS_PER_PAGE (PAGE_CACHE_SIZE/512) | |
64 | ||
65 | #define SHMEM_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1)) | |
66 | #define SHMEM_MAX_BYTES ((unsigned long long)SHMEM_MAX_INDEX << PAGE_CACHE_SHIFT) | |
67 | ||
68 | #define VM_ACCT(size) (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT) | |
69 | ||
70 | /* info->flags needs VM_flags to handle pagein/truncate races efficiently */ | |
71 | #define SHMEM_PAGEIN VM_READ | |
72 | #define SHMEM_TRUNCATE VM_WRITE | |
73 | ||
74 | /* Definition to limit shmem_truncate's steps between cond_rescheds */ | |
75 | #define LATENCY_LIMIT 64 | |
76 | ||
77 | /* Pretend that each entry is of this size in directory's i_size */ | |
78 | #define BOGO_DIRENT_SIZE 20 | |
79 | ||
1da177e4 LT |
80 | /* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */ |
81 | enum sgp_type { | |
82 | SGP_QUICK, /* don't try more than file page cache lookup */ | |
83 | SGP_READ, /* don't exceed i_size, don't allocate page */ | |
84 | SGP_CACHE, /* don't exceed i_size, may allocate page */ | |
85 | SGP_WRITE, /* may exceed i_size, may allocate page */ | |
54cb8821 | 86 | SGP_FAULT, /* same as SGP_CACHE, return with page locked */ |
1da177e4 LT |
87 | }; |
88 | ||
89 | static int shmem_getpage(struct inode *inode, unsigned long idx, | |
90 | struct page **pagep, enum sgp_type sgp, int *type); | |
91 | ||
6daa0e28 | 92 | static inline struct page *shmem_dir_alloc(gfp_t gfp_mask) |
1da177e4 LT |
93 | { |
94 | /* | |
95 | * The above definition of ENTRIES_PER_PAGE, and the use of | |
96 | * BLOCKS_PER_PAGE on indirect pages, assume PAGE_CACHE_SIZE: | |
97 | * might be reconsidered if it ever diverges from PAGE_SIZE. | |
769848c0 MG |
98 | * |
99 | * __GFP_MOVABLE is masked out as swap vectors cannot move | |
1da177e4 | 100 | */ |
769848c0 MG |
101 | return alloc_pages((gfp_mask & ~__GFP_MOVABLE) | __GFP_ZERO, |
102 | PAGE_CACHE_SHIFT-PAGE_SHIFT); | |
1da177e4 LT |
103 | } |
104 | ||
105 | static inline void shmem_dir_free(struct page *page) | |
106 | { | |
107 | __free_pages(page, PAGE_CACHE_SHIFT-PAGE_SHIFT); | |
108 | } | |
109 | ||
110 | static struct page **shmem_dir_map(struct page *page) | |
111 | { | |
112 | return (struct page **)kmap_atomic(page, KM_USER0); | |
113 | } | |
114 | ||
115 | static inline void shmem_dir_unmap(struct page **dir) | |
116 | { | |
117 | kunmap_atomic(dir, KM_USER0); | |
118 | } | |
119 | ||
120 | static swp_entry_t *shmem_swp_map(struct page *page) | |
121 | { | |
122 | return (swp_entry_t *)kmap_atomic(page, KM_USER1); | |
123 | } | |
124 | ||
125 | static inline void shmem_swp_balance_unmap(void) | |
126 | { | |
127 | /* | |
128 | * When passing a pointer to an i_direct entry, to code which | |
129 | * also handles indirect entries and so will shmem_swp_unmap, | |
130 | * we must arrange for the preempt count to remain in balance. | |
131 | * What kmap_atomic of a lowmem page does depends on config | |
132 | * and architecture, so pretend to kmap_atomic some lowmem page. | |
133 | */ | |
134 | (void) kmap_atomic(ZERO_PAGE(0), KM_USER1); | |
135 | } | |
136 | ||
137 | static inline void shmem_swp_unmap(swp_entry_t *entry) | |
138 | { | |
139 | kunmap_atomic(entry, KM_USER1); | |
140 | } | |
141 | ||
142 | static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) | |
143 | { | |
144 | return sb->s_fs_info; | |
145 | } | |
146 | ||
147 | /* | |
148 | * shmem_file_setup pre-accounts the whole fixed size of a VM object, | |
149 | * for shared memory and for shared anonymous (/dev/zero) mappings | |
150 | * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), | |
151 | * consistent with the pre-accounting of private mappings ... | |
152 | */ | |
153 | static inline int shmem_acct_size(unsigned long flags, loff_t size) | |
154 | { | |
155 | return (flags & VM_ACCOUNT)? | |
156 | security_vm_enough_memory(VM_ACCT(size)): 0; | |
157 | } | |
158 | ||
159 | static inline void shmem_unacct_size(unsigned long flags, loff_t size) | |
160 | { | |
161 | if (flags & VM_ACCOUNT) | |
162 | vm_unacct_memory(VM_ACCT(size)); | |
163 | } | |
164 | ||
165 | /* | |
166 | * ... whereas tmpfs objects are accounted incrementally as | |
167 | * pages are allocated, in order to allow huge sparse files. | |
168 | * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, | |
169 | * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. | |
170 | */ | |
171 | static inline int shmem_acct_block(unsigned long flags) | |
172 | { | |
173 | return (flags & VM_ACCOUNT)? | |
174 | 0: security_vm_enough_memory(VM_ACCT(PAGE_CACHE_SIZE)); | |
175 | } | |
176 | ||
177 | static inline void shmem_unacct_blocks(unsigned long flags, long pages) | |
178 | { | |
179 | if (!(flags & VM_ACCOUNT)) | |
180 | vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE)); | |
181 | } | |
182 | ||
759b9775 | 183 | static const struct super_operations shmem_ops; |
f5e54d6e | 184 | static const struct address_space_operations shmem_aops; |
15ad7cdc | 185 | static const struct file_operations shmem_file_operations; |
92e1d5be AV |
186 | static const struct inode_operations shmem_inode_operations; |
187 | static const struct inode_operations shmem_dir_inode_operations; | |
188 | static const struct inode_operations shmem_special_inode_operations; | |
1da177e4 LT |
189 | static struct vm_operations_struct shmem_vm_ops; |
190 | ||
6c231b7b | 191 | static struct backing_dev_info shmem_backing_dev_info __read_mostly = { |
1da177e4 LT |
192 | .ra_pages = 0, /* No readahead */ |
193 | .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK, | |
194 | .unplug_io_fn = default_unplug_io_fn, | |
195 | }; | |
196 | ||
197 | static LIST_HEAD(shmem_swaplist); | |
198 | static DEFINE_SPINLOCK(shmem_swaplist_lock); | |
199 | ||
200 | static void shmem_free_blocks(struct inode *inode, long pages) | |
201 | { | |
202 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); | |
0edd73b3 | 203 | if (sbinfo->max_blocks) { |
1da177e4 LT |
204 | spin_lock(&sbinfo->stat_lock); |
205 | sbinfo->free_blocks += pages; | |
206 | inode->i_blocks -= pages*BLOCKS_PER_PAGE; | |
207 | spin_unlock(&sbinfo->stat_lock); | |
208 | } | |
209 | } | |
210 | ||
211 | /* | |
212 | * shmem_recalc_inode - recalculate the size of an inode | |
213 | * | |
214 | * @inode: inode to recalc | |
215 | * | |
216 | * We have to calculate the free blocks since the mm can drop | |
217 | * undirtied hole pages behind our back. | |
218 | * | |
219 | * But normally info->alloced == inode->i_mapping->nrpages + info->swapped | |
220 | * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) | |
221 | * | |
222 | * It has to be called with the spinlock held. | |
223 | */ | |
224 | static void shmem_recalc_inode(struct inode *inode) | |
225 | { | |
226 | struct shmem_inode_info *info = SHMEM_I(inode); | |
227 | long freed; | |
228 | ||
229 | freed = info->alloced - info->swapped - inode->i_mapping->nrpages; | |
230 | if (freed > 0) { | |
231 | info->alloced -= freed; | |
232 | shmem_unacct_blocks(info->flags, freed); | |
233 | shmem_free_blocks(inode, freed); | |
234 | } | |
235 | } | |
236 | ||
237 | /* | |
238 | * shmem_swp_entry - find the swap vector position in the info structure | |
239 | * | |
240 | * @info: info structure for the inode | |
241 | * @index: index of the page to find | |
242 | * @page: optional page to add to the structure. Has to be preset to | |
243 | * all zeros | |
244 | * | |
245 | * If there is no space allocated yet it will return NULL when | |
246 | * page is NULL, else it will use the page for the needed block, | |
247 | * setting it to NULL on return to indicate that it has been used. | |
248 | * | |
249 | * The swap vector is organized the following way: | |
250 | * | |
251 | * There are SHMEM_NR_DIRECT entries directly stored in the | |
252 | * shmem_inode_info structure. So small files do not need an addional | |
253 | * allocation. | |
254 | * | |
255 | * For pages with index > SHMEM_NR_DIRECT there is the pointer | |
256 | * i_indirect which points to a page which holds in the first half | |
257 | * doubly indirect blocks, in the second half triple indirect blocks: | |
258 | * | |
259 | * For an artificial ENTRIES_PER_PAGE = 4 this would lead to the | |
260 | * following layout (for SHMEM_NR_DIRECT == 16): | |
261 | * | |
262 | * i_indirect -> dir --> 16-19 | |
263 | * | +-> 20-23 | |
264 | * | | |
265 | * +-->dir2 --> 24-27 | |
266 | * | +-> 28-31 | |
267 | * | +-> 32-35 | |
268 | * | +-> 36-39 | |
269 | * | | |
270 | * +-->dir3 --> 40-43 | |
271 | * +-> 44-47 | |
272 | * +-> 48-51 | |
273 | * +-> 52-55 | |
274 | */ | |
275 | static swp_entry_t *shmem_swp_entry(struct shmem_inode_info *info, unsigned long index, struct page **page) | |
276 | { | |
277 | unsigned long offset; | |
278 | struct page **dir; | |
279 | struct page *subdir; | |
280 | ||
281 | if (index < SHMEM_NR_DIRECT) { | |
282 | shmem_swp_balance_unmap(); | |
283 | return info->i_direct+index; | |
284 | } | |
285 | if (!info->i_indirect) { | |
286 | if (page) { | |
287 | info->i_indirect = *page; | |
288 | *page = NULL; | |
289 | } | |
290 | return NULL; /* need another page */ | |
291 | } | |
292 | ||
293 | index -= SHMEM_NR_DIRECT; | |
294 | offset = index % ENTRIES_PER_PAGE; | |
295 | index /= ENTRIES_PER_PAGE; | |
296 | dir = shmem_dir_map(info->i_indirect); | |
297 | ||
298 | if (index >= ENTRIES_PER_PAGE/2) { | |
299 | index -= ENTRIES_PER_PAGE/2; | |
300 | dir += ENTRIES_PER_PAGE/2 + index/ENTRIES_PER_PAGE; | |
301 | index %= ENTRIES_PER_PAGE; | |
302 | subdir = *dir; | |
303 | if (!subdir) { | |
304 | if (page) { | |
305 | *dir = *page; | |
306 | *page = NULL; | |
307 | } | |
308 | shmem_dir_unmap(dir); | |
309 | return NULL; /* need another page */ | |
310 | } | |
311 | shmem_dir_unmap(dir); | |
312 | dir = shmem_dir_map(subdir); | |
313 | } | |
314 | ||
315 | dir += index; | |
316 | subdir = *dir; | |
317 | if (!subdir) { | |
318 | if (!page || !(subdir = *page)) { | |
319 | shmem_dir_unmap(dir); | |
320 | return NULL; /* need a page */ | |
321 | } | |
322 | *dir = subdir; | |
323 | *page = NULL; | |
324 | } | |
325 | shmem_dir_unmap(dir); | |
326 | return shmem_swp_map(subdir) + offset; | |
327 | } | |
328 | ||
329 | static void shmem_swp_set(struct shmem_inode_info *info, swp_entry_t *entry, unsigned long value) | |
330 | { | |
331 | long incdec = value? 1: -1; | |
332 | ||
333 | entry->val = value; | |
334 | info->swapped += incdec; | |
4c21e2f2 HD |
335 | if ((unsigned long)(entry - info->i_direct) >= SHMEM_NR_DIRECT) { |
336 | struct page *page = kmap_atomic_to_page(entry); | |
337 | set_page_private(page, page_private(page) + incdec); | |
338 | } | |
1da177e4 LT |
339 | } |
340 | ||
341 | /* | |
342 | * shmem_swp_alloc - get the position of the swap entry for the page. | |
343 | * If it does not exist allocate the entry. | |
344 | * | |
345 | * @info: info structure for the inode | |
346 | * @index: index of the page to find | |
347 | * @sgp: check and recheck i_size? skip allocation? | |
348 | */ | |
349 | static swp_entry_t *shmem_swp_alloc(struct shmem_inode_info *info, unsigned long index, enum sgp_type sgp) | |
350 | { | |
351 | struct inode *inode = &info->vfs_inode; | |
352 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); | |
353 | struct page *page = NULL; | |
354 | swp_entry_t *entry; | |
355 | ||
356 | if (sgp != SGP_WRITE && | |
357 | ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) | |
358 | return ERR_PTR(-EINVAL); | |
359 | ||
360 | while (!(entry = shmem_swp_entry(info, index, &page))) { | |
361 | if (sgp == SGP_READ) | |
362 | return shmem_swp_map(ZERO_PAGE(0)); | |
363 | /* | |
364 | * Test free_blocks against 1 not 0, since we have 1 data | |
365 | * page (and perhaps indirect index pages) yet to allocate: | |
366 | * a waste to allocate index if we cannot allocate data. | |
367 | */ | |
0edd73b3 | 368 | if (sbinfo->max_blocks) { |
1da177e4 LT |
369 | spin_lock(&sbinfo->stat_lock); |
370 | if (sbinfo->free_blocks <= 1) { | |
371 | spin_unlock(&sbinfo->stat_lock); | |
372 | return ERR_PTR(-ENOSPC); | |
373 | } | |
374 | sbinfo->free_blocks--; | |
375 | inode->i_blocks += BLOCKS_PER_PAGE; | |
376 | spin_unlock(&sbinfo->stat_lock); | |
377 | } | |
378 | ||
379 | spin_unlock(&info->lock); | |
769848c0 | 380 | page = shmem_dir_alloc(mapping_gfp_mask(inode->i_mapping)); |
4c21e2f2 HD |
381 | if (page) |
382 | set_page_private(page, 0); | |
1da177e4 LT |
383 | spin_lock(&info->lock); |
384 | ||
385 | if (!page) { | |
386 | shmem_free_blocks(inode, 1); | |
387 | return ERR_PTR(-ENOMEM); | |
388 | } | |
389 | if (sgp != SGP_WRITE && | |
390 | ((loff_t) index << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { | |
391 | entry = ERR_PTR(-EINVAL); | |
392 | break; | |
393 | } | |
394 | if (info->next_index <= index) | |
395 | info->next_index = index + 1; | |
396 | } | |
397 | if (page) { | |
398 | /* another task gave its page, or truncated the file */ | |
399 | shmem_free_blocks(inode, 1); | |
400 | shmem_dir_free(page); | |
401 | } | |
402 | if (info->next_index <= index && !IS_ERR(entry)) | |
403 | info->next_index = index + 1; | |
404 | return entry; | |
405 | } | |
406 | ||
407 | /* | |
408 | * shmem_free_swp - free some swap entries in a directory | |
409 | * | |
1ae70006 HD |
410 | * @dir: pointer to the directory |
411 | * @edir: pointer after last entry of the directory | |
412 | * @punch_lock: pointer to spinlock when needed for the holepunch case | |
1da177e4 | 413 | */ |
1ae70006 HD |
414 | static int shmem_free_swp(swp_entry_t *dir, swp_entry_t *edir, |
415 | spinlock_t *punch_lock) | |
1da177e4 | 416 | { |
1ae70006 | 417 | spinlock_t *punch_unlock = NULL; |
1da177e4 LT |
418 | swp_entry_t *ptr; |
419 | int freed = 0; | |
420 | ||
421 | for (ptr = dir; ptr < edir; ptr++) { | |
422 | if (ptr->val) { | |
1ae70006 HD |
423 | if (unlikely(punch_lock)) { |
424 | punch_unlock = punch_lock; | |
425 | punch_lock = NULL; | |
426 | spin_lock(punch_unlock); | |
427 | if (!ptr->val) | |
428 | continue; | |
429 | } | |
1da177e4 LT |
430 | free_swap_and_cache(*ptr); |
431 | *ptr = (swp_entry_t){0}; | |
432 | freed++; | |
433 | } | |
434 | } | |
1ae70006 HD |
435 | if (punch_unlock) |
436 | spin_unlock(punch_unlock); | |
1da177e4 LT |
437 | return freed; |
438 | } | |
439 | ||
1ae70006 HD |
440 | static int shmem_map_and_free_swp(struct page *subdir, int offset, |
441 | int limit, struct page ***dir, spinlock_t *punch_lock) | |
1da177e4 LT |
442 | { |
443 | swp_entry_t *ptr; | |
444 | int freed = 0; | |
445 | ||
446 | ptr = shmem_swp_map(subdir); | |
447 | for (; offset < limit; offset += LATENCY_LIMIT) { | |
448 | int size = limit - offset; | |
449 | if (size > LATENCY_LIMIT) | |
450 | size = LATENCY_LIMIT; | |
1ae70006 HD |
451 | freed += shmem_free_swp(ptr+offset, ptr+offset+size, |
452 | punch_lock); | |
1da177e4 LT |
453 | if (need_resched()) { |
454 | shmem_swp_unmap(ptr); | |
455 | if (*dir) { | |
456 | shmem_dir_unmap(*dir); | |
457 | *dir = NULL; | |
458 | } | |
459 | cond_resched(); | |
460 | ptr = shmem_swp_map(subdir); | |
461 | } | |
462 | } | |
463 | shmem_swp_unmap(ptr); | |
464 | return freed; | |
465 | } | |
466 | ||
467 | static void shmem_free_pages(struct list_head *next) | |
468 | { | |
469 | struct page *page; | |
470 | int freed = 0; | |
471 | ||
472 | do { | |
473 | page = container_of(next, struct page, lru); | |
474 | next = next->next; | |
475 | shmem_dir_free(page); | |
476 | freed++; | |
477 | if (freed >= LATENCY_LIMIT) { | |
478 | cond_resched(); | |
479 | freed = 0; | |
480 | } | |
481 | } while (next); | |
482 | } | |
483 | ||
f6b3ec23 | 484 | static void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end) |
1da177e4 LT |
485 | { |
486 | struct shmem_inode_info *info = SHMEM_I(inode); | |
487 | unsigned long idx; | |
488 | unsigned long size; | |
489 | unsigned long limit; | |
490 | unsigned long stage; | |
491 | unsigned long diroff; | |
492 | struct page **dir; | |
493 | struct page *topdir; | |
494 | struct page *middir; | |
495 | struct page *subdir; | |
496 | swp_entry_t *ptr; | |
497 | LIST_HEAD(pages_to_free); | |
498 | long nr_pages_to_free = 0; | |
499 | long nr_swaps_freed = 0; | |
500 | int offset; | |
501 | int freed; | |
a2646d1e | 502 | int punch_hole; |
1ae70006 HD |
503 | spinlock_t *needs_lock; |
504 | spinlock_t *punch_lock; | |
a2646d1e | 505 | unsigned long upper_limit; |
1da177e4 LT |
506 | |
507 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; | |
f6b3ec23 | 508 | idx = (start + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
1da177e4 LT |
509 | if (idx >= info->next_index) |
510 | return; | |
511 | ||
512 | spin_lock(&info->lock); | |
513 | info->flags |= SHMEM_TRUNCATE; | |
f6b3ec23 BP |
514 | if (likely(end == (loff_t) -1)) { |
515 | limit = info->next_index; | |
a2646d1e | 516 | upper_limit = SHMEM_MAX_INDEX; |
f6b3ec23 | 517 | info->next_index = idx; |
1ae70006 | 518 | needs_lock = NULL; |
a2646d1e | 519 | punch_hole = 0; |
f6b3ec23 | 520 | } else { |
a2646d1e HD |
521 | if (end + 1 >= inode->i_size) { /* we may free a little more */ |
522 | limit = (inode->i_size + PAGE_CACHE_SIZE - 1) >> | |
523 | PAGE_CACHE_SHIFT; | |
524 | upper_limit = SHMEM_MAX_INDEX; | |
525 | } else { | |
526 | limit = (end + 1) >> PAGE_CACHE_SHIFT; | |
527 | upper_limit = limit; | |
528 | } | |
1ae70006 | 529 | needs_lock = &info->lock; |
f6b3ec23 BP |
530 | punch_hole = 1; |
531 | } | |
532 | ||
1da177e4 | 533 | topdir = info->i_indirect; |
f6b3ec23 | 534 | if (topdir && idx <= SHMEM_NR_DIRECT && !punch_hole) { |
1da177e4 LT |
535 | info->i_indirect = NULL; |
536 | nr_pages_to_free++; | |
537 | list_add(&topdir->lru, &pages_to_free); | |
538 | } | |
539 | spin_unlock(&info->lock); | |
540 | ||
541 | if (info->swapped && idx < SHMEM_NR_DIRECT) { | |
542 | ptr = info->i_direct; | |
543 | size = limit; | |
544 | if (size > SHMEM_NR_DIRECT) | |
545 | size = SHMEM_NR_DIRECT; | |
1ae70006 | 546 | nr_swaps_freed = shmem_free_swp(ptr+idx, ptr+size, needs_lock); |
1da177e4 | 547 | } |
92a3d03a BP |
548 | |
549 | /* | |
550 | * If there are no indirect blocks or we are punching a hole | |
551 | * below indirect blocks, nothing to be done. | |
552 | */ | |
a2646d1e | 553 | if (!topdir || limit <= SHMEM_NR_DIRECT) |
1da177e4 LT |
554 | goto done2; |
555 | ||
1ae70006 HD |
556 | /* |
557 | * The truncation case has already dropped info->lock, and we're safe | |
558 | * because i_size and next_index have already been lowered, preventing | |
559 | * access beyond. But in the punch_hole case, we still need to take | |
560 | * the lock when updating the swap directory, because there might be | |
561 | * racing accesses by shmem_getpage(SGP_CACHE), shmem_unuse_inode or | |
562 | * shmem_writepage. However, whenever we find we can remove a whole | |
563 | * directory page (not at the misaligned start or end of the range), | |
564 | * we first NULLify its pointer in the level above, and then have no | |
565 | * need to take the lock when updating its contents: needs_lock and | |
566 | * punch_lock (either pointing to info->lock or NULL) manage this. | |
567 | */ | |
568 | ||
a2646d1e | 569 | upper_limit -= SHMEM_NR_DIRECT; |
1da177e4 LT |
570 | limit -= SHMEM_NR_DIRECT; |
571 | idx = (idx > SHMEM_NR_DIRECT)? (idx - SHMEM_NR_DIRECT): 0; | |
572 | offset = idx % ENTRIES_PER_PAGE; | |
573 | idx -= offset; | |
574 | ||
575 | dir = shmem_dir_map(topdir); | |
576 | stage = ENTRIES_PER_PAGEPAGE/2; | |
577 | if (idx < ENTRIES_PER_PAGEPAGE/2) { | |
578 | middir = topdir; | |
579 | diroff = idx/ENTRIES_PER_PAGE; | |
580 | } else { | |
581 | dir += ENTRIES_PER_PAGE/2; | |
582 | dir += (idx - ENTRIES_PER_PAGEPAGE/2)/ENTRIES_PER_PAGEPAGE; | |
583 | while (stage <= idx) | |
584 | stage += ENTRIES_PER_PAGEPAGE; | |
585 | middir = *dir; | |
586 | if (*dir) { | |
587 | diroff = ((idx - ENTRIES_PER_PAGEPAGE/2) % | |
588 | ENTRIES_PER_PAGEPAGE) / ENTRIES_PER_PAGE; | |
a2646d1e | 589 | if (!diroff && !offset && upper_limit >= stage) { |
1ae70006 HD |
590 | if (needs_lock) { |
591 | spin_lock(needs_lock); | |
592 | *dir = NULL; | |
593 | spin_unlock(needs_lock); | |
594 | needs_lock = NULL; | |
595 | } else | |
596 | *dir = NULL; | |
1da177e4 LT |
597 | nr_pages_to_free++; |
598 | list_add(&middir->lru, &pages_to_free); | |
599 | } | |
600 | shmem_dir_unmap(dir); | |
601 | dir = shmem_dir_map(middir); | |
602 | } else { | |
603 | diroff = 0; | |
604 | offset = 0; | |
605 | idx = stage; | |
606 | } | |
607 | } | |
608 | ||
609 | for (; idx < limit; idx += ENTRIES_PER_PAGE, diroff++) { | |
610 | if (unlikely(idx == stage)) { | |
611 | shmem_dir_unmap(dir); | |
612 | dir = shmem_dir_map(topdir) + | |
613 | ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE; | |
614 | while (!*dir) { | |
615 | dir++; | |
616 | idx += ENTRIES_PER_PAGEPAGE; | |
617 | if (idx >= limit) | |
618 | goto done1; | |
619 | } | |
620 | stage = idx + ENTRIES_PER_PAGEPAGE; | |
621 | middir = *dir; | |
1ae70006 HD |
622 | if (punch_hole) |
623 | needs_lock = &info->lock; | |
a2646d1e | 624 | if (upper_limit >= stage) { |
1ae70006 HD |
625 | if (needs_lock) { |
626 | spin_lock(needs_lock); | |
627 | *dir = NULL; | |
628 | spin_unlock(needs_lock); | |
629 | needs_lock = NULL; | |
630 | } else | |
631 | *dir = NULL; | |
a2646d1e HD |
632 | nr_pages_to_free++; |
633 | list_add(&middir->lru, &pages_to_free); | |
634 | } | |
1da177e4 LT |
635 | shmem_dir_unmap(dir); |
636 | cond_resched(); | |
637 | dir = shmem_dir_map(middir); | |
638 | diroff = 0; | |
639 | } | |
1ae70006 | 640 | punch_lock = needs_lock; |
1da177e4 | 641 | subdir = dir[diroff]; |
1ae70006 HD |
642 | if (subdir && !offset && upper_limit-idx >= ENTRIES_PER_PAGE) { |
643 | if (needs_lock) { | |
644 | spin_lock(needs_lock); | |
645 | dir[diroff] = NULL; | |
646 | spin_unlock(needs_lock); | |
647 | punch_lock = NULL; | |
648 | } else | |
649 | dir[diroff] = NULL; | |
650 | nr_pages_to_free++; | |
651 | list_add(&subdir->lru, &pages_to_free); | |
652 | } | |
653 | if (subdir && page_private(subdir) /* has swap entries */) { | |
1da177e4 LT |
654 | size = limit - idx; |
655 | if (size > ENTRIES_PER_PAGE) | |
656 | size = ENTRIES_PER_PAGE; | |
657 | freed = shmem_map_and_free_swp(subdir, | |
1ae70006 | 658 | offset, size, &dir, punch_lock); |
1da177e4 LT |
659 | if (!dir) |
660 | dir = shmem_dir_map(middir); | |
661 | nr_swaps_freed += freed; | |
1ae70006 | 662 | if (offset || punch_lock) { |
1da177e4 | 663 | spin_lock(&info->lock); |
1ae70006 HD |
664 | set_page_private(subdir, |
665 | page_private(subdir) - freed); | |
1da177e4 | 666 | spin_unlock(&info->lock); |
1ae70006 HD |
667 | } else |
668 | BUG_ON(page_private(subdir) != freed); | |
1da177e4 | 669 | } |
1ae70006 | 670 | offset = 0; |
1da177e4 LT |
671 | } |
672 | done1: | |
673 | shmem_dir_unmap(dir); | |
674 | done2: | |
675 | if (inode->i_mapping->nrpages && (info->flags & SHMEM_PAGEIN)) { | |
676 | /* | |
677 | * Call truncate_inode_pages again: racing shmem_unuse_inode | |
678 | * may have swizzled a page in from swap since vmtruncate or | |
679 | * generic_delete_inode did it, before we lowered next_index. | |
680 | * Also, though shmem_getpage checks i_size before adding to | |
681 | * cache, no recheck after: so fix the narrow window there too. | |
16a10019 HD |
682 | * |
683 | * Recalling truncate_inode_pages_range and unmap_mapping_range | |
684 | * every time for punch_hole (which never got a chance to clear | |
685 | * SHMEM_PAGEIN at the start of vmtruncate_range) is expensive, | |
686 | * yet hardly ever necessary: try to optimize them out later. | |
1da177e4 | 687 | */ |
f6b3ec23 | 688 | truncate_inode_pages_range(inode->i_mapping, start, end); |
16a10019 HD |
689 | if (punch_hole) |
690 | unmap_mapping_range(inode->i_mapping, start, | |
691 | end - start, 1); | |
1da177e4 LT |
692 | } |
693 | ||
694 | spin_lock(&info->lock); | |
695 | info->flags &= ~SHMEM_TRUNCATE; | |
696 | info->swapped -= nr_swaps_freed; | |
697 | if (nr_pages_to_free) | |
698 | shmem_free_blocks(inode, nr_pages_to_free); | |
699 | shmem_recalc_inode(inode); | |
700 | spin_unlock(&info->lock); | |
701 | ||
702 | /* | |
703 | * Empty swap vector directory pages to be freed? | |
704 | */ | |
705 | if (!list_empty(&pages_to_free)) { | |
706 | pages_to_free.prev->next = NULL; | |
707 | shmem_free_pages(pages_to_free.next); | |
708 | } | |
709 | } | |
710 | ||
f6b3ec23 BP |
711 | static void shmem_truncate(struct inode *inode) |
712 | { | |
713 | shmem_truncate_range(inode, inode->i_size, (loff_t)-1); | |
714 | } | |
715 | ||
1da177e4 LT |
716 | static int shmem_notify_change(struct dentry *dentry, struct iattr *attr) |
717 | { | |
718 | struct inode *inode = dentry->d_inode; | |
719 | struct page *page = NULL; | |
720 | int error; | |
721 | ||
39f0247d | 722 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
1da177e4 LT |
723 | if (attr->ia_size < inode->i_size) { |
724 | /* | |
725 | * If truncating down to a partial page, then | |
726 | * if that page is already allocated, hold it | |
727 | * in memory until the truncation is over, so | |
728 | * truncate_partial_page cannnot miss it were | |
729 | * it assigned to swap. | |
730 | */ | |
731 | if (attr->ia_size & (PAGE_CACHE_SIZE-1)) { | |
732 | (void) shmem_getpage(inode, | |
733 | attr->ia_size>>PAGE_CACHE_SHIFT, | |
734 | &page, SGP_READ, NULL); | |
735 | } | |
736 | /* | |
737 | * Reset SHMEM_PAGEIN flag so that shmem_truncate can | |
738 | * detect if any pages might have been added to cache | |
739 | * after truncate_inode_pages. But we needn't bother | |
740 | * if it's being fully truncated to zero-length: the | |
741 | * nrpages check is efficient enough in that case. | |
742 | */ | |
743 | if (attr->ia_size) { | |
744 | struct shmem_inode_info *info = SHMEM_I(inode); | |
745 | spin_lock(&info->lock); | |
746 | info->flags &= ~SHMEM_PAGEIN; | |
747 | spin_unlock(&info->lock); | |
748 | } | |
749 | } | |
750 | } | |
751 | ||
752 | error = inode_change_ok(inode, attr); | |
753 | if (!error) | |
754 | error = inode_setattr(inode, attr); | |
39f0247d AG |
755 | #ifdef CONFIG_TMPFS_POSIX_ACL |
756 | if (!error && (attr->ia_valid & ATTR_MODE)) | |
757 | error = generic_acl_chmod(inode, &shmem_acl_ops); | |
758 | #endif | |
1da177e4 LT |
759 | if (page) |
760 | page_cache_release(page); | |
761 | return error; | |
762 | } | |
763 | ||
764 | static void shmem_delete_inode(struct inode *inode) | |
765 | { | |
766 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); | |
767 | struct shmem_inode_info *info = SHMEM_I(inode); | |
768 | ||
769 | if (inode->i_op->truncate == shmem_truncate) { | |
fef26658 | 770 | truncate_inode_pages(inode->i_mapping, 0); |
1da177e4 LT |
771 | shmem_unacct_size(info->flags, inode->i_size); |
772 | inode->i_size = 0; | |
773 | shmem_truncate(inode); | |
774 | if (!list_empty(&info->swaplist)) { | |
775 | spin_lock(&shmem_swaplist_lock); | |
776 | list_del_init(&info->swaplist); | |
777 | spin_unlock(&shmem_swaplist_lock); | |
778 | } | |
779 | } | |
0edd73b3 HD |
780 | BUG_ON(inode->i_blocks); |
781 | if (sbinfo->max_inodes) { | |
1da177e4 LT |
782 | spin_lock(&sbinfo->stat_lock); |
783 | sbinfo->free_inodes++; | |
784 | spin_unlock(&sbinfo->stat_lock); | |
785 | } | |
786 | clear_inode(inode); | |
787 | } | |
788 | ||
789 | static inline int shmem_find_swp(swp_entry_t entry, swp_entry_t *dir, swp_entry_t *edir) | |
790 | { | |
791 | swp_entry_t *ptr; | |
792 | ||
793 | for (ptr = dir; ptr < edir; ptr++) { | |
794 | if (ptr->val == entry.val) | |
795 | return ptr - dir; | |
796 | } | |
797 | return -1; | |
798 | } | |
799 | ||
800 | static int shmem_unuse_inode(struct shmem_inode_info *info, swp_entry_t entry, struct page *page) | |
801 | { | |
802 | struct inode *inode; | |
803 | unsigned long idx; | |
804 | unsigned long size; | |
805 | unsigned long limit; | |
806 | unsigned long stage; | |
807 | struct page **dir; | |
808 | struct page *subdir; | |
809 | swp_entry_t *ptr; | |
810 | int offset; | |
811 | ||
812 | idx = 0; | |
813 | ptr = info->i_direct; | |
814 | spin_lock(&info->lock); | |
815 | limit = info->next_index; | |
816 | size = limit; | |
817 | if (size > SHMEM_NR_DIRECT) | |
818 | size = SHMEM_NR_DIRECT; | |
819 | offset = shmem_find_swp(entry, ptr, ptr+size); | |
820 | if (offset >= 0) { | |
821 | shmem_swp_balance_unmap(); | |
822 | goto found; | |
823 | } | |
824 | if (!info->i_indirect) | |
825 | goto lost2; | |
826 | ||
827 | dir = shmem_dir_map(info->i_indirect); | |
828 | stage = SHMEM_NR_DIRECT + ENTRIES_PER_PAGEPAGE/2; | |
829 | ||
830 | for (idx = SHMEM_NR_DIRECT; idx < limit; idx += ENTRIES_PER_PAGE, dir++) { | |
831 | if (unlikely(idx == stage)) { | |
832 | shmem_dir_unmap(dir-1); | |
833 | dir = shmem_dir_map(info->i_indirect) + | |
834 | ENTRIES_PER_PAGE/2 + idx/ENTRIES_PER_PAGEPAGE; | |
835 | while (!*dir) { | |
836 | dir++; | |
837 | idx += ENTRIES_PER_PAGEPAGE; | |
838 | if (idx >= limit) | |
839 | goto lost1; | |
840 | } | |
841 | stage = idx + ENTRIES_PER_PAGEPAGE; | |
842 | subdir = *dir; | |
843 | shmem_dir_unmap(dir); | |
844 | dir = shmem_dir_map(subdir); | |
845 | } | |
846 | subdir = *dir; | |
4c21e2f2 | 847 | if (subdir && page_private(subdir)) { |
1da177e4 LT |
848 | ptr = shmem_swp_map(subdir); |
849 | size = limit - idx; | |
850 | if (size > ENTRIES_PER_PAGE) | |
851 | size = ENTRIES_PER_PAGE; | |
852 | offset = shmem_find_swp(entry, ptr, ptr+size); | |
853 | if (offset >= 0) { | |
854 | shmem_dir_unmap(dir); | |
855 | goto found; | |
856 | } | |
857 | shmem_swp_unmap(ptr); | |
858 | } | |
859 | } | |
860 | lost1: | |
861 | shmem_dir_unmap(dir-1); | |
862 | lost2: | |
863 | spin_unlock(&info->lock); | |
864 | return 0; | |
865 | found: | |
866 | idx += offset; | |
867 | inode = &info->vfs_inode; | |
868 | if (move_from_swap_cache(page, idx, inode->i_mapping) == 0) { | |
869 | info->flags |= SHMEM_PAGEIN; | |
870 | shmem_swp_set(info, ptr + offset, 0); | |
871 | } | |
872 | shmem_swp_unmap(ptr); | |
873 | spin_unlock(&info->lock); | |
874 | /* | |
875 | * Decrement swap count even when the entry is left behind: | |
876 | * try_to_unuse will skip over mms, then reincrement count. | |
877 | */ | |
878 | swap_free(entry); | |
879 | return 1; | |
880 | } | |
881 | ||
882 | /* | |
883 | * shmem_unuse() search for an eventually swapped out shmem page. | |
884 | */ | |
885 | int shmem_unuse(swp_entry_t entry, struct page *page) | |
886 | { | |
887 | struct list_head *p, *next; | |
888 | struct shmem_inode_info *info; | |
889 | int found = 0; | |
890 | ||
891 | spin_lock(&shmem_swaplist_lock); | |
892 | list_for_each_safe(p, next, &shmem_swaplist) { | |
893 | info = list_entry(p, struct shmem_inode_info, swaplist); | |
894 | if (!info->swapped) | |
895 | list_del_init(&info->swaplist); | |
896 | else if (shmem_unuse_inode(info, entry, page)) { | |
897 | /* move head to start search for next from here */ | |
898 | list_move_tail(&shmem_swaplist, &info->swaplist); | |
899 | found = 1; | |
900 | break; | |
901 | } | |
902 | } | |
903 | spin_unlock(&shmem_swaplist_lock); | |
904 | return found; | |
905 | } | |
906 | ||
907 | /* | |
908 | * Move the page from the page cache to the swap cache. | |
909 | */ | |
910 | static int shmem_writepage(struct page *page, struct writeback_control *wbc) | |
911 | { | |
912 | struct shmem_inode_info *info; | |
913 | swp_entry_t *entry, swap; | |
914 | struct address_space *mapping; | |
915 | unsigned long index; | |
916 | struct inode *inode; | |
917 | ||
918 | BUG_ON(!PageLocked(page)); | |
919 | BUG_ON(page_mapped(page)); | |
920 | ||
921 | mapping = page->mapping; | |
922 | index = page->index; | |
923 | inode = mapping->host; | |
924 | info = SHMEM_I(inode); | |
925 | if (info->flags & VM_LOCKED) | |
926 | goto redirty; | |
927 | swap = get_swap_page(); | |
928 | if (!swap.val) | |
929 | goto redirty; | |
930 | ||
931 | spin_lock(&info->lock); | |
932 | shmem_recalc_inode(inode); | |
933 | if (index >= info->next_index) { | |
934 | BUG_ON(!(info->flags & SHMEM_TRUNCATE)); | |
935 | goto unlock; | |
936 | } | |
937 | entry = shmem_swp_entry(info, index, NULL); | |
938 | BUG_ON(!entry); | |
939 | BUG_ON(entry->val); | |
940 | ||
941 | if (move_to_swap_cache(page, swap) == 0) { | |
942 | shmem_swp_set(info, entry, swap.val); | |
943 | shmem_swp_unmap(entry); | |
944 | spin_unlock(&info->lock); | |
945 | if (list_empty(&info->swaplist)) { | |
946 | spin_lock(&shmem_swaplist_lock); | |
947 | /* move instead of add in case we're racing */ | |
948 | list_move_tail(&info->swaplist, &shmem_swaplist); | |
949 | spin_unlock(&shmem_swaplist_lock); | |
950 | } | |
951 | unlock_page(page); | |
952 | return 0; | |
953 | } | |
954 | ||
955 | shmem_swp_unmap(entry); | |
956 | unlock: | |
957 | spin_unlock(&info->lock); | |
958 | swap_free(swap); | |
959 | redirty: | |
960 | set_page_dirty(page); | |
994fc28c | 961 | return AOP_WRITEPAGE_ACTIVATE; /* Return with the page locked */ |
1da177e4 LT |
962 | } |
963 | ||
964 | #ifdef CONFIG_NUMA | |
d15c023b | 965 | static inline int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes) |
b00dc3ad HD |
966 | { |
967 | char *nodelist = strchr(value, ':'); | |
968 | int err = 1; | |
969 | ||
970 | if (nodelist) { | |
971 | /* NUL-terminate policy string */ | |
972 | *nodelist++ = '\0'; | |
973 | if (nodelist_parse(nodelist, *policy_nodes)) | |
974 | goto out; | |
a210906c HD |
975 | if (!nodes_subset(*policy_nodes, node_online_map)) |
976 | goto out; | |
b00dc3ad HD |
977 | } |
978 | if (!strcmp(value, "default")) { | |
979 | *policy = MPOL_DEFAULT; | |
980 | /* Don't allow a nodelist */ | |
981 | if (!nodelist) | |
982 | err = 0; | |
983 | } else if (!strcmp(value, "prefer")) { | |
984 | *policy = MPOL_PREFERRED; | |
985 | /* Insist on a nodelist of one node only */ | |
986 | if (nodelist) { | |
987 | char *rest = nodelist; | |
988 | while (isdigit(*rest)) | |
989 | rest++; | |
990 | if (!*rest) | |
991 | err = 0; | |
992 | } | |
993 | } else if (!strcmp(value, "bind")) { | |
994 | *policy = MPOL_BIND; | |
995 | /* Insist on a nodelist */ | |
996 | if (nodelist) | |
997 | err = 0; | |
998 | } else if (!strcmp(value, "interleave")) { | |
999 | *policy = MPOL_INTERLEAVE; | |
1000 | /* Default to nodes online if no nodelist */ | |
1001 | if (!nodelist) | |
1002 | *policy_nodes = node_online_map; | |
1003 | err = 0; | |
1004 | } | |
1005 | out: | |
1006 | /* Restore string for error message */ | |
1007 | if (nodelist) | |
1008 | *--nodelist = ':'; | |
1009 | return err; | |
1010 | } | |
1011 | ||
1da177e4 LT |
1012 | static struct page *shmem_swapin_async(struct shared_policy *p, |
1013 | swp_entry_t entry, unsigned long idx) | |
1014 | { | |
1015 | struct page *page; | |
1016 | struct vm_area_struct pvma; | |
1017 | ||
1018 | /* Create a pseudo vma that just contains the policy */ | |
1019 | memset(&pvma, 0, sizeof(struct vm_area_struct)); | |
1020 | pvma.vm_end = PAGE_SIZE; | |
1021 | pvma.vm_pgoff = idx; | |
1022 | pvma.vm_policy = mpol_shared_policy_lookup(p, idx); | |
1023 | page = read_swap_cache_async(entry, &pvma, 0); | |
1024 | mpol_free(pvma.vm_policy); | |
1025 | return page; | |
1026 | } | |
1027 | ||
1028 | struct page *shmem_swapin(struct shmem_inode_info *info, swp_entry_t entry, | |
1029 | unsigned long idx) | |
1030 | { | |
1031 | struct shared_policy *p = &info->policy; | |
1032 | int i, num; | |
1033 | struct page *page; | |
1034 | unsigned long offset; | |
1035 | ||
1036 | num = valid_swaphandles(entry, &offset); | |
1037 | for (i = 0; i < num; offset++, i++) { | |
1038 | page = shmem_swapin_async(p, | |
1039 | swp_entry(swp_type(entry), offset), idx); | |
1040 | if (!page) | |
1041 | break; | |
1042 | page_cache_release(page); | |
1043 | } | |
1044 | lru_add_drain(); /* Push any new pages onto the LRU now */ | |
1045 | return shmem_swapin_async(p, entry, idx); | |
1046 | } | |
1047 | ||
1048 | static struct page * | |
6daa0e28 | 1049 | shmem_alloc_page(gfp_t gfp, struct shmem_inode_info *info, |
1da177e4 LT |
1050 | unsigned long idx) |
1051 | { | |
1052 | struct vm_area_struct pvma; | |
1053 | struct page *page; | |
1054 | ||
1055 | memset(&pvma, 0, sizeof(struct vm_area_struct)); | |
1056 | pvma.vm_policy = mpol_shared_policy_lookup(&info->policy, idx); | |
1057 | pvma.vm_pgoff = idx; | |
1058 | pvma.vm_end = PAGE_SIZE; | |
1059 | page = alloc_page_vma(gfp | __GFP_ZERO, &pvma, 0); | |
1060 | mpol_free(pvma.vm_policy); | |
1061 | return page; | |
1062 | } | |
1063 | #else | |
b00dc3ad HD |
1064 | static inline int shmem_parse_mpol(char *value, int *policy, nodemask_t *policy_nodes) |
1065 | { | |
1066 | return 1; | |
1067 | } | |
1068 | ||
1da177e4 LT |
1069 | static inline struct page * |
1070 | shmem_swapin(struct shmem_inode_info *info,swp_entry_t entry,unsigned long idx) | |
1071 | { | |
1072 | swapin_readahead(entry, 0, NULL); | |
1073 | return read_swap_cache_async(entry, NULL, 0); | |
1074 | } | |
1075 | ||
1076 | static inline struct page * | |
dd0fc66f | 1077 | shmem_alloc_page(gfp_t gfp,struct shmem_inode_info *info, unsigned long idx) |
1da177e4 LT |
1078 | { |
1079 | return alloc_page(gfp | __GFP_ZERO); | |
1080 | } | |
1081 | #endif | |
1082 | ||
1083 | /* | |
1084 | * shmem_getpage - either get the page from swap or allocate a new one | |
1085 | * | |
1086 | * If we allocate a new one we do not mark it dirty. That's up to the | |
1087 | * vm. If we swap it in we mark it dirty since we also free the swap | |
1088 | * entry since a page cannot live in both the swap and page cache | |
1089 | */ | |
1090 | static int shmem_getpage(struct inode *inode, unsigned long idx, | |
1091 | struct page **pagep, enum sgp_type sgp, int *type) | |
1092 | { | |
1093 | struct address_space *mapping = inode->i_mapping; | |
1094 | struct shmem_inode_info *info = SHMEM_I(inode); | |
1095 | struct shmem_sb_info *sbinfo; | |
1096 | struct page *filepage = *pagep; | |
1097 | struct page *swappage; | |
1098 | swp_entry_t *entry; | |
1099 | swp_entry_t swap; | |
1100 | int error; | |
1101 | ||
1102 | if (idx >= SHMEM_MAX_INDEX) | |
1103 | return -EFBIG; | |
54cb8821 NP |
1104 | |
1105 | if (type) | |
1106 | *type = VM_FAULT_MINOR; | |
1107 | ||
1da177e4 LT |
1108 | /* |
1109 | * Normally, filepage is NULL on entry, and either found | |
1110 | * uptodate immediately, or allocated and zeroed, or read | |
1111 | * in under swappage, which is then assigned to filepage. | |
ae976416 HD |
1112 | * But shmem_readpage and shmem_prepare_write pass in a locked |
1113 | * filepage, which may be found not uptodate by other callers | |
1114 | * too, and may need to be copied from the swappage read in. | |
1da177e4 LT |
1115 | */ |
1116 | repeat: | |
1117 | if (!filepage) | |
1118 | filepage = find_lock_page(mapping, idx); | |
1119 | if (filepage && PageUptodate(filepage)) | |
1120 | goto done; | |
1121 | error = 0; | |
1122 | if (sgp == SGP_QUICK) | |
1123 | goto failed; | |
1124 | ||
1125 | spin_lock(&info->lock); | |
1126 | shmem_recalc_inode(inode); | |
1127 | entry = shmem_swp_alloc(info, idx, sgp); | |
1128 | if (IS_ERR(entry)) { | |
1129 | spin_unlock(&info->lock); | |
1130 | error = PTR_ERR(entry); | |
1131 | goto failed; | |
1132 | } | |
1133 | swap = *entry; | |
1134 | ||
1135 | if (swap.val) { | |
1136 | /* Look it up and read it in.. */ | |
1137 | swappage = lookup_swap_cache(swap); | |
1138 | if (!swappage) { | |
1139 | shmem_swp_unmap(entry); | |
1da177e4 LT |
1140 | /* here we actually do the io */ |
1141 | if (type && *type == VM_FAULT_MINOR) { | |
f8891e5e | 1142 | __count_vm_event(PGMAJFAULT); |
1da177e4 LT |
1143 | *type = VM_FAULT_MAJOR; |
1144 | } | |
f8891e5e | 1145 | spin_unlock(&info->lock); |
1da177e4 LT |
1146 | swappage = shmem_swapin(info, swap, idx); |
1147 | if (!swappage) { | |
1148 | spin_lock(&info->lock); | |
1149 | entry = shmem_swp_alloc(info, idx, sgp); | |
1150 | if (IS_ERR(entry)) | |
1151 | error = PTR_ERR(entry); | |
1152 | else { | |
1153 | if (entry->val == swap.val) | |
1154 | error = -ENOMEM; | |
1155 | shmem_swp_unmap(entry); | |
1156 | } | |
1157 | spin_unlock(&info->lock); | |
1158 | if (error) | |
1159 | goto failed; | |
1160 | goto repeat; | |
1161 | } | |
1162 | wait_on_page_locked(swappage); | |
1163 | page_cache_release(swappage); | |
1164 | goto repeat; | |
1165 | } | |
1166 | ||
1167 | /* We have to do this with page locked to prevent races */ | |
1168 | if (TestSetPageLocked(swappage)) { | |
1169 | shmem_swp_unmap(entry); | |
1170 | spin_unlock(&info->lock); | |
1171 | wait_on_page_locked(swappage); | |
1172 | page_cache_release(swappage); | |
1173 | goto repeat; | |
1174 | } | |
1175 | if (PageWriteback(swappage)) { | |
1176 | shmem_swp_unmap(entry); | |
1177 | spin_unlock(&info->lock); | |
1178 | wait_on_page_writeback(swappage); | |
1179 | unlock_page(swappage); | |
1180 | page_cache_release(swappage); | |
1181 | goto repeat; | |
1182 | } | |
1183 | if (!PageUptodate(swappage)) { | |
1184 | shmem_swp_unmap(entry); | |
1185 | spin_unlock(&info->lock); | |
1186 | unlock_page(swappage); | |
1187 | page_cache_release(swappage); | |
1188 | error = -EIO; | |
1189 | goto failed; | |
1190 | } | |
1191 | ||
1192 | if (filepage) { | |
1193 | shmem_swp_set(info, entry, 0); | |
1194 | shmem_swp_unmap(entry); | |
1195 | delete_from_swap_cache(swappage); | |
1196 | spin_unlock(&info->lock); | |
1197 | copy_highpage(filepage, swappage); | |
1198 | unlock_page(swappage); | |
1199 | page_cache_release(swappage); | |
1200 | flush_dcache_page(filepage); | |
1201 | SetPageUptodate(filepage); | |
1202 | set_page_dirty(filepage); | |
1203 | swap_free(swap); | |
1204 | } else if (!(error = move_from_swap_cache( | |
1205 | swappage, idx, mapping))) { | |
1206 | info->flags |= SHMEM_PAGEIN; | |
1207 | shmem_swp_set(info, entry, 0); | |
1208 | shmem_swp_unmap(entry); | |
1209 | spin_unlock(&info->lock); | |
1210 | filepage = swappage; | |
1211 | swap_free(swap); | |
1212 | } else { | |
1213 | shmem_swp_unmap(entry); | |
1214 | spin_unlock(&info->lock); | |
1215 | unlock_page(swappage); | |
1216 | page_cache_release(swappage); | |
1217 | if (error == -ENOMEM) { | |
1218 | /* let kswapd refresh zone for GFP_ATOMICs */ | |
3fcfab16 | 1219 | congestion_wait(WRITE, HZ/50); |
1da177e4 LT |
1220 | } |
1221 | goto repeat; | |
1222 | } | |
1223 | } else if (sgp == SGP_READ && !filepage) { | |
1224 | shmem_swp_unmap(entry); | |
1225 | filepage = find_get_page(mapping, idx); | |
1226 | if (filepage && | |
1227 | (!PageUptodate(filepage) || TestSetPageLocked(filepage))) { | |
1228 | spin_unlock(&info->lock); | |
1229 | wait_on_page_locked(filepage); | |
1230 | page_cache_release(filepage); | |
1231 | filepage = NULL; | |
1232 | goto repeat; | |
1233 | } | |
1234 | spin_unlock(&info->lock); | |
1235 | } else { | |
1236 | shmem_swp_unmap(entry); | |
1237 | sbinfo = SHMEM_SB(inode->i_sb); | |
0edd73b3 | 1238 | if (sbinfo->max_blocks) { |
1da177e4 LT |
1239 | spin_lock(&sbinfo->stat_lock); |
1240 | if (sbinfo->free_blocks == 0 || | |
1241 | shmem_acct_block(info->flags)) { | |
1242 | spin_unlock(&sbinfo->stat_lock); | |
1243 | spin_unlock(&info->lock); | |
1244 | error = -ENOSPC; | |
1245 | goto failed; | |
1246 | } | |
1247 | sbinfo->free_blocks--; | |
1248 | inode->i_blocks += BLOCKS_PER_PAGE; | |
1249 | spin_unlock(&sbinfo->stat_lock); | |
1250 | } else if (shmem_acct_block(info->flags)) { | |
1251 | spin_unlock(&info->lock); | |
1252 | error = -ENOSPC; | |
1253 | goto failed; | |
1254 | } | |
1255 | ||
1256 | if (!filepage) { | |
1257 | spin_unlock(&info->lock); | |
1258 | filepage = shmem_alloc_page(mapping_gfp_mask(mapping), | |
1259 | info, | |
1260 | idx); | |
1261 | if (!filepage) { | |
1262 | shmem_unacct_blocks(info->flags, 1); | |
1263 | shmem_free_blocks(inode, 1); | |
1264 | error = -ENOMEM; | |
1265 | goto failed; | |
1266 | } | |
1267 | ||
1268 | spin_lock(&info->lock); | |
1269 | entry = shmem_swp_alloc(info, idx, sgp); | |
1270 | if (IS_ERR(entry)) | |
1271 | error = PTR_ERR(entry); | |
1272 | else { | |
1273 | swap = *entry; | |
1274 | shmem_swp_unmap(entry); | |
1275 | } | |
1276 | if (error || swap.val || 0 != add_to_page_cache_lru( | |
1277 | filepage, mapping, idx, GFP_ATOMIC)) { | |
1278 | spin_unlock(&info->lock); | |
1279 | page_cache_release(filepage); | |
1280 | shmem_unacct_blocks(info->flags, 1); | |
1281 | shmem_free_blocks(inode, 1); | |
1282 | filepage = NULL; | |
1283 | if (error) | |
1284 | goto failed; | |
1285 | goto repeat; | |
1286 | } | |
1287 | info->flags |= SHMEM_PAGEIN; | |
1288 | } | |
1289 | ||
1290 | info->alloced++; | |
1291 | spin_unlock(&info->lock); | |
1292 | flush_dcache_page(filepage); | |
1293 | SetPageUptodate(filepage); | |
1294 | } | |
1295 | done: | |
1296 | if (*pagep != filepage) { | |
1da177e4 | 1297 | *pagep = filepage; |
54cb8821 | 1298 | if (sgp != SGP_FAULT) |
d00806b1 NP |
1299 | unlock_page(filepage); |
1300 | ||
1da177e4 LT |
1301 | } |
1302 | return 0; | |
1303 | ||
1304 | failed: | |
1305 | if (*pagep != filepage) { | |
1306 | unlock_page(filepage); | |
1307 | page_cache_release(filepage); | |
1308 | } | |
1309 | return error; | |
1310 | } | |
1311 | ||
54cb8821 NP |
1312 | static struct page *shmem_fault(struct vm_area_struct *vma, |
1313 | struct fault_data *fdata) | |
1da177e4 | 1314 | { |
d3ac7f89 | 1315 | struct inode *inode = vma->vm_file->f_path.dentry->d_inode; |
1da177e4 | 1316 | struct page *page = NULL; |
1da177e4 LT |
1317 | int error; |
1318 | ||
d00806b1 NP |
1319 | BUG_ON(!(vma->vm_flags & VM_CAN_INVALIDATE)); |
1320 | ||
54cb8821 NP |
1321 | if (((loff_t)fdata->pgoff << PAGE_CACHE_SHIFT) >= i_size_read(inode)) { |
1322 | fdata->type = VM_FAULT_SIGBUS; | |
1323 | return NULL; | |
1324 | } | |
1da177e4 | 1325 | |
54cb8821 NP |
1326 | error = shmem_getpage(inode, fdata->pgoff, &page, |
1327 | SGP_FAULT, &fdata->type); | |
1328 | if (error) { | |
1329 | fdata->type = ((error == -ENOMEM)?VM_FAULT_OOM:VM_FAULT_SIGBUS); | |
1330 | return NULL; | |
1331 | } | |
1da177e4 LT |
1332 | |
1333 | mark_page_accessed(page); | |
1334 | return page; | |
1335 | } | |
1336 | ||
1da177e4 LT |
1337 | #ifdef CONFIG_NUMA |
1338 | int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *new) | |
1339 | { | |
d3ac7f89 | 1340 | struct inode *i = vma->vm_file->f_path.dentry->d_inode; |
1da177e4 LT |
1341 | return mpol_set_shared_policy(&SHMEM_I(i)->policy, vma, new); |
1342 | } | |
1343 | ||
1344 | struct mempolicy * | |
1345 | shmem_get_policy(struct vm_area_struct *vma, unsigned long addr) | |
1346 | { | |
d3ac7f89 | 1347 | struct inode *i = vma->vm_file->f_path.dentry->d_inode; |
1da177e4 LT |
1348 | unsigned long idx; |
1349 | ||
1350 | idx = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; | |
1351 | return mpol_shared_policy_lookup(&SHMEM_I(i)->policy, idx); | |
1352 | } | |
1353 | #endif | |
1354 | ||
1355 | int shmem_lock(struct file *file, int lock, struct user_struct *user) | |
1356 | { | |
d3ac7f89 | 1357 | struct inode *inode = file->f_path.dentry->d_inode; |
1da177e4 LT |
1358 | struct shmem_inode_info *info = SHMEM_I(inode); |
1359 | int retval = -ENOMEM; | |
1360 | ||
1361 | spin_lock(&info->lock); | |
1362 | if (lock && !(info->flags & VM_LOCKED)) { | |
1363 | if (!user_shm_lock(inode->i_size, user)) | |
1364 | goto out_nomem; | |
1365 | info->flags |= VM_LOCKED; | |
1366 | } | |
1367 | if (!lock && (info->flags & VM_LOCKED) && user) { | |
1368 | user_shm_unlock(inode->i_size, user); | |
1369 | info->flags &= ~VM_LOCKED; | |
1370 | } | |
1371 | retval = 0; | |
1372 | out_nomem: | |
1373 | spin_unlock(&info->lock); | |
1374 | return retval; | |
1375 | } | |
1376 | ||
9b83a6a8 | 1377 | static int shmem_mmap(struct file *file, struct vm_area_struct *vma) |
1da177e4 LT |
1378 | { |
1379 | file_accessed(file); | |
1380 | vma->vm_ops = &shmem_vm_ops; | |
54cb8821 | 1381 | vma->vm_flags |= VM_CAN_INVALIDATE | VM_CAN_NONLINEAR; |
1da177e4 LT |
1382 | return 0; |
1383 | } | |
1384 | ||
1385 | static struct inode * | |
1386 | shmem_get_inode(struct super_block *sb, int mode, dev_t dev) | |
1387 | { | |
1388 | struct inode *inode; | |
1389 | struct shmem_inode_info *info; | |
1390 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); | |
1391 | ||
0edd73b3 | 1392 | if (sbinfo->max_inodes) { |
1da177e4 LT |
1393 | spin_lock(&sbinfo->stat_lock); |
1394 | if (!sbinfo->free_inodes) { | |
1395 | spin_unlock(&sbinfo->stat_lock); | |
1396 | return NULL; | |
1397 | } | |
1398 | sbinfo->free_inodes--; | |
1399 | spin_unlock(&sbinfo->stat_lock); | |
1400 | } | |
1401 | ||
1402 | inode = new_inode(sb); | |
1403 | if (inode) { | |
1404 | inode->i_mode = mode; | |
1405 | inode->i_uid = current->fsuid; | |
1406 | inode->i_gid = current->fsgid; | |
1da177e4 LT |
1407 | inode->i_blocks = 0; |
1408 | inode->i_mapping->a_ops = &shmem_aops; | |
1409 | inode->i_mapping->backing_dev_info = &shmem_backing_dev_info; | |
1410 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
91828a40 | 1411 | inode->i_generation = get_seconds(); |
1da177e4 LT |
1412 | info = SHMEM_I(inode); |
1413 | memset(info, 0, (char *)inode - (char *)info); | |
1414 | spin_lock_init(&info->lock); | |
1415 | INIT_LIST_HEAD(&info->swaplist); | |
1416 | ||
1417 | switch (mode & S_IFMT) { | |
1418 | default: | |
39f0247d | 1419 | inode->i_op = &shmem_special_inode_operations; |
1da177e4 LT |
1420 | init_special_inode(inode, mode, dev); |
1421 | break; | |
1422 | case S_IFREG: | |
1423 | inode->i_op = &shmem_inode_operations; | |
1424 | inode->i_fop = &shmem_file_operations; | |
7339ff83 RH |
1425 | mpol_shared_policy_init(&info->policy, sbinfo->policy, |
1426 | &sbinfo->policy_nodes); | |
1da177e4 LT |
1427 | break; |
1428 | case S_IFDIR: | |
d8c76e6f | 1429 | inc_nlink(inode); |
1da177e4 LT |
1430 | /* Some things misbehave if size == 0 on a directory */ |
1431 | inode->i_size = 2 * BOGO_DIRENT_SIZE; | |
1432 | inode->i_op = &shmem_dir_inode_operations; | |
1433 | inode->i_fop = &simple_dir_operations; | |
1434 | break; | |
1435 | case S_IFLNK: | |
1436 | /* | |
1437 | * Must not load anything in the rbtree, | |
1438 | * mpol_free_shared_policy will not be called. | |
1439 | */ | |
7339ff83 RH |
1440 | mpol_shared_policy_init(&info->policy, MPOL_DEFAULT, |
1441 | NULL); | |
1da177e4 LT |
1442 | break; |
1443 | } | |
0edd73b3 | 1444 | } else if (sbinfo->max_inodes) { |
1da177e4 LT |
1445 | spin_lock(&sbinfo->stat_lock); |
1446 | sbinfo->free_inodes++; | |
1447 | spin_unlock(&sbinfo->stat_lock); | |
1448 | } | |
1449 | return inode; | |
1450 | } | |
1451 | ||
1452 | #ifdef CONFIG_TMPFS | |
92e1d5be AV |
1453 | static const struct inode_operations shmem_symlink_inode_operations; |
1454 | static const struct inode_operations shmem_symlink_inline_operations; | |
1da177e4 LT |
1455 | |
1456 | /* | |
ae976416 HD |
1457 | * Normally tmpfs avoids the use of shmem_readpage and shmem_prepare_write; |
1458 | * but providing them allows a tmpfs file to be used for splice, sendfile, and | |
1459 | * below the loop driver, in the generic fashion that many filesystems support. | |
1da177e4 | 1460 | */ |
ae976416 HD |
1461 | static int shmem_readpage(struct file *file, struct page *page) |
1462 | { | |
1463 | struct inode *inode = page->mapping->host; | |
1464 | int error = shmem_getpage(inode, page->index, &page, SGP_CACHE, NULL); | |
1465 | unlock_page(page); | |
1466 | return error; | |
1467 | } | |
1468 | ||
1da177e4 LT |
1469 | static int |
1470 | shmem_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to) | |
1471 | { | |
1472 | struct inode *inode = page->mapping->host; | |
1473 | return shmem_getpage(inode, page->index, &page, SGP_WRITE, NULL); | |
1474 | } | |
1475 | ||
1476 | static ssize_t | |
1477 | shmem_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) | |
1478 | { | |
d3ac7f89 | 1479 | struct inode *inode = file->f_path.dentry->d_inode; |
1da177e4 LT |
1480 | loff_t pos; |
1481 | unsigned long written; | |
1482 | ssize_t err; | |
1483 | ||
1484 | if ((ssize_t) count < 0) | |
1485 | return -EINVAL; | |
1486 | ||
1487 | if (!access_ok(VERIFY_READ, buf, count)) | |
1488 | return -EFAULT; | |
1489 | ||
1b1dcc1b | 1490 | mutex_lock(&inode->i_mutex); |
1da177e4 LT |
1491 | |
1492 | pos = *ppos; | |
1493 | written = 0; | |
1494 | ||
1495 | err = generic_write_checks(file, &pos, &count, 0); | |
1496 | if (err || !count) | |
1497 | goto out; | |
1498 | ||
d3ac7f89 | 1499 | err = remove_suid(file->f_path.dentry); |
1da177e4 LT |
1500 | if (err) |
1501 | goto out; | |
1502 | ||
1503 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; | |
1504 | ||
1505 | do { | |
1506 | struct page *page = NULL; | |
1507 | unsigned long bytes, index, offset; | |
1508 | char *kaddr; | |
1509 | int left; | |
1510 | ||
1511 | offset = (pos & (PAGE_CACHE_SIZE -1)); /* Within page */ | |
1512 | index = pos >> PAGE_CACHE_SHIFT; | |
1513 | bytes = PAGE_CACHE_SIZE - offset; | |
1514 | if (bytes > count) | |
1515 | bytes = count; | |
1516 | ||
1517 | /* | |
1518 | * We don't hold page lock across copy from user - | |
1519 | * what would it guard against? - so no deadlock here. | |
1520 | * But it still may be a good idea to prefault below. | |
1521 | */ | |
1522 | ||
1523 | err = shmem_getpage(inode, index, &page, SGP_WRITE, NULL); | |
1524 | if (err) | |
1525 | break; | |
1526 | ||
1527 | left = bytes; | |
1528 | if (PageHighMem(page)) { | |
1529 | volatile unsigned char dummy; | |
1530 | __get_user(dummy, buf); | |
1531 | __get_user(dummy, buf + bytes - 1); | |
1532 | ||
1533 | kaddr = kmap_atomic(page, KM_USER0); | |
1534 | left = __copy_from_user_inatomic(kaddr + offset, | |
1535 | buf, bytes); | |
1536 | kunmap_atomic(kaddr, KM_USER0); | |
1537 | } | |
1538 | if (left) { | |
1539 | kaddr = kmap(page); | |
1540 | left = __copy_from_user(kaddr + offset, buf, bytes); | |
1541 | kunmap(page); | |
1542 | } | |
1543 | ||
1544 | written += bytes; | |
1545 | count -= bytes; | |
1546 | pos += bytes; | |
1547 | buf += bytes; | |
1548 | if (pos > inode->i_size) | |
1549 | i_size_write(inode, pos); | |
1550 | ||
1551 | flush_dcache_page(page); | |
1552 | set_page_dirty(page); | |
1553 | mark_page_accessed(page); | |
1554 | page_cache_release(page); | |
1555 | ||
1556 | if (left) { | |
1557 | pos -= left; | |
1558 | written -= left; | |
1559 | err = -EFAULT; | |
1560 | break; | |
1561 | } | |
1562 | ||
1563 | /* | |
1564 | * Our dirty pages are not counted in nr_dirty, | |
1565 | * and we do not attempt to balance dirty pages. | |
1566 | */ | |
1567 | ||
1568 | cond_resched(); | |
1569 | } while (count); | |
1570 | ||
1571 | *ppos = pos; | |
1572 | if (written) | |
1573 | err = written; | |
1574 | out: | |
1b1dcc1b | 1575 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
1576 | return err; |
1577 | } | |
1578 | ||
1579 | static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor) | |
1580 | { | |
d3ac7f89 | 1581 | struct inode *inode = filp->f_path.dentry->d_inode; |
1da177e4 LT |
1582 | struct address_space *mapping = inode->i_mapping; |
1583 | unsigned long index, offset; | |
1584 | ||
1585 | index = *ppos >> PAGE_CACHE_SHIFT; | |
1586 | offset = *ppos & ~PAGE_CACHE_MASK; | |
1587 | ||
1588 | for (;;) { | |
1589 | struct page *page = NULL; | |
1590 | unsigned long end_index, nr, ret; | |
1591 | loff_t i_size = i_size_read(inode); | |
1592 | ||
1593 | end_index = i_size >> PAGE_CACHE_SHIFT; | |
1594 | if (index > end_index) | |
1595 | break; | |
1596 | if (index == end_index) { | |
1597 | nr = i_size & ~PAGE_CACHE_MASK; | |
1598 | if (nr <= offset) | |
1599 | break; | |
1600 | } | |
1601 | ||
1602 | desc->error = shmem_getpage(inode, index, &page, SGP_READ, NULL); | |
1603 | if (desc->error) { | |
1604 | if (desc->error == -EINVAL) | |
1605 | desc->error = 0; | |
1606 | break; | |
1607 | } | |
1608 | ||
1609 | /* | |
1610 | * We must evaluate after, since reads (unlike writes) | |
1b1dcc1b | 1611 | * are called without i_mutex protection against truncate |
1da177e4 LT |
1612 | */ |
1613 | nr = PAGE_CACHE_SIZE; | |
1614 | i_size = i_size_read(inode); | |
1615 | end_index = i_size >> PAGE_CACHE_SHIFT; | |
1616 | if (index == end_index) { | |
1617 | nr = i_size & ~PAGE_CACHE_MASK; | |
1618 | if (nr <= offset) { | |
1619 | if (page) | |
1620 | page_cache_release(page); | |
1621 | break; | |
1622 | } | |
1623 | } | |
1624 | nr -= offset; | |
1625 | ||
1626 | if (page) { | |
1627 | /* | |
1628 | * If users can be writing to this page using arbitrary | |
1629 | * virtual addresses, take care about potential aliasing | |
1630 | * before reading the page on the kernel side. | |
1631 | */ | |
1632 | if (mapping_writably_mapped(mapping)) | |
1633 | flush_dcache_page(page); | |
1634 | /* | |
1635 | * Mark the page accessed if we read the beginning. | |
1636 | */ | |
1637 | if (!offset) | |
1638 | mark_page_accessed(page); | |
b5810039 | 1639 | } else { |
1da177e4 | 1640 | page = ZERO_PAGE(0); |
b5810039 NP |
1641 | page_cache_get(page); |
1642 | } | |
1da177e4 LT |
1643 | |
1644 | /* | |
1645 | * Ok, we have the page, and it's up-to-date, so | |
1646 | * now we can copy it to user space... | |
1647 | * | |
1648 | * The actor routine returns how many bytes were actually used.. | |
1649 | * NOTE! This may not be the same as how much of a user buffer | |
1650 | * we filled up (we may be padding etc), so we can only update | |
1651 | * "pos" here (the actor routine has to update the user buffer | |
1652 | * pointers and the remaining count). | |
1653 | */ | |
1654 | ret = actor(desc, page, offset, nr); | |
1655 | offset += ret; | |
1656 | index += offset >> PAGE_CACHE_SHIFT; | |
1657 | offset &= ~PAGE_CACHE_MASK; | |
1658 | ||
1659 | page_cache_release(page); | |
1660 | if (ret != nr || !desc->count) | |
1661 | break; | |
1662 | ||
1663 | cond_resched(); | |
1664 | } | |
1665 | ||
1666 | *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; | |
1667 | file_accessed(filp); | |
1668 | } | |
1669 | ||
1670 | static ssize_t shmem_file_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos) | |
1671 | { | |
1672 | read_descriptor_t desc; | |
1673 | ||
1674 | if ((ssize_t) count < 0) | |
1675 | return -EINVAL; | |
1676 | if (!access_ok(VERIFY_WRITE, buf, count)) | |
1677 | return -EFAULT; | |
1678 | if (!count) | |
1679 | return 0; | |
1680 | ||
1681 | desc.written = 0; | |
1682 | desc.count = count; | |
1683 | desc.arg.buf = buf; | |
1684 | desc.error = 0; | |
1685 | ||
1686 | do_shmem_file_read(filp, ppos, &desc, file_read_actor); | |
1687 | if (desc.written) | |
1688 | return desc.written; | |
1689 | return desc.error; | |
1690 | } | |
1691 | ||
726c3342 | 1692 | static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) |
1da177e4 | 1693 | { |
726c3342 | 1694 | struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); |
1da177e4 LT |
1695 | |
1696 | buf->f_type = TMPFS_MAGIC; | |
1697 | buf->f_bsize = PAGE_CACHE_SIZE; | |
1698 | buf->f_namelen = NAME_MAX; | |
0edd73b3 HD |
1699 | spin_lock(&sbinfo->stat_lock); |
1700 | if (sbinfo->max_blocks) { | |
1da177e4 LT |
1701 | buf->f_blocks = sbinfo->max_blocks; |
1702 | buf->f_bavail = buf->f_bfree = sbinfo->free_blocks; | |
0edd73b3 HD |
1703 | } |
1704 | if (sbinfo->max_inodes) { | |
1da177e4 LT |
1705 | buf->f_files = sbinfo->max_inodes; |
1706 | buf->f_ffree = sbinfo->free_inodes; | |
1da177e4 LT |
1707 | } |
1708 | /* else leave those fields 0 like simple_statfs */ | |
0edd73b3 | 1709 | spin_unlock(&sbinfo->stat_lock); |
1da177e4 LT |
1710 | return 0; |
1711 | } | |
1712 | ||
1713 | /* | |
1714 | * File creation. Allocate an inode, and we're done.. | |
1715 | */ | |
1716 | static int | |
1717 | shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) | |
1718 | { | |
1719 | struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev); | |
1720 | int error = -ENOSPC; | |
1721 | ||
1722 | if (inode) { | |
570bc1c2 SS |
1723 | error = security_inode_init_security(inode, dir, NULL, NULL, |
1724 | NULL); | |
1725 | if (error) { | |
1726 | if (error != -EOPNOTSUPP) { | |
1727 | iput(inode); | |
1728 | return error; | |
1729 | } | |
39f0247d AG |
1730 | } |
1731 | error = shmem_acl_init(inode, dir); | |
1732 | if (error) { | |
1733 | iput(inode); | |
1734 | return error; | |
570bc1c2 | 1735 | } |
1da177e4 LT |
1736 | if (dir->i_mode & S_ISGID) { |
1737 | inode->i_gid = dir->i_gid; | |
1738 | if (S_ISDIR(mode)) | |
1739 | inode->i_mode |= S_ISGID; | |
1740 | } | |
1741 | dir->i_size += BOGO_DIRENT_SIZE; | |
1742 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; | |
1743 | d_instantiate(dentry, inode); | |
1744 | dget(dentry); /* Extra count - pin the dentry in core */ | |
1da177e4 LT |
1745 | } |
1746 | return error; | |
1747 | } | |
1748 | ||
1749 | static int shmem_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
1750 | { | |
1751 | int error; | |
1752 | ||
1753 | if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) | |
1754 | return error; | |
d8c76e6f | 1755 | inc_nlink(dir); |
1da177e4 LT |
1756 | return 0; |
1757 | } | |
1758 | ||
1759 | static int shmem_create(struct inode *dir, struct dentry *dentry, int mode, | |
1760 | struct nameidata *nd) | |
1761 | { | |
1762 | return shmem_mknod(dir, dentry, mode | S_IFREG, 0); | |
1763 | } | |
1764 | ||
1765 | /* | |
1766 | * Link a file.. | |
1767 | */ | |
1768 | static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) | |
1769 | { | |
1770 | struct inode *inode = old_dentry->d_inode; | |
1771 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); | |
1772 | ||
1773 | /* | |
1774 | * No ordinary (disk based) filesystem counts links as inodes; | |
1775 | * but each new link needs a new dentry, pinning lowmem, and | |
1776 | * tmpfs dentries cannot be pruned until they are unlinked. | |
1777 | */ | |
0edd73b3 | 1778 | if (sbinfo->max_inodes) { |
1da177e4 LT |
1779 | spin_lock(&sbinfo->stat_lock); |
1780 | if (!sbinfo->free_inodes) { | |
1781 | spin_unlock(&sbinfo->stat_lock); | |
1782 | return -ENOSPC; | |
1783 | } | |
1784 | sbinfo->free_inodes--; | |
1785 | spin_unlock(&sbinfo->stat_lock); | |
1786 | } | |
1787 | ||
1788 | dir->i_size += BOGO_DIRENT_SIZE; | |
1789 | inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; | |
d8c76e6f | 1790 | inc_nlink(inode); |
1da177e4 LT |
1791 | atomic_inc(&inode->i_count); /* New dentry reference */ |
1792 | dget(dentry); /* Extra pinning count for the created dentry */ | |
1793 | d_instantiate(dentry, inode); | |
1794 | return 0; | |
1795 | } | |
1796 | ||
1797 | static int shmem_unlink(struct inode *dir, struct dentry *dentry) | |
1798 | { | |
1799 | struct inode *inode = dentry->d_inode; | |
1800 | ||
1801 | if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) { | |
1802 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); | |
0edd73b3 | 1803 | if (sbinfo->max_inodes) { |
1da177e4 LT |
1804 | spin_lock(&sbinfo->stat_lock); |
1805 | sbinfo->free_inodes++; | |
1806 | spin_unlock(&sbinfo->stat_lock); | |
1807 | } | |
1808 | } | |
1809 | ||
1810 | dir->i_size -= BOGO_DIRENT_SIZE; | |
1811 | inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME; | |
9a53c3a7 | 1812 | drop_nlink(inode); |
1da177e4 LT |
1813 | dput(dentry); /* Undo the count from "create" - this does all the work */ |
1814 | return 0; | |
1815 | } | |
1816 | ||
1817 | static int shmem_rmdir(struct inode *dir, struct dentry *dentry) | |
1818 | { | |
1819 | if (!simple_empty(dentry)) | |
1820 | return -ENOTEMPTY; | |
1821 | ||
9a53c3a7 DH |
1822 | drop_nlink(dentry->d_inode); |
1823 | drop_nlink(dir); | |
1da177e4 LT |
1824 | return shmem_unlink(dir, dentry); |
1825 | } | |
1826 | ||
1827 | /* | |
1828 | * The VFS layer already does all the dentry stuff for rename, | |
1829 | * we just have to decrement the usage count for the target if | |
1830 | * it exists so that the VFS layer correctly free's it when it | |
1831 | * gets overwritten. | |
1832 | */ | |
1833 | static int shmem_rename(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) | |
1834 | { | |
1835 | struct inode *inode = old_dentry->d_inode; | |
1836 | int they_are_dirs = S_ISDIR(inode->i_mode); | |
1837 | ||
1838 | if (!simple_empty(new_dentry)) | |
1839 | return -ENOTEMPTY; | |
1840 | ||
1841 | if (new_dentry->d_inode) { | |
1842 | (void) shmem_unlink(new_dir, new_dentry); | |
1843 | if (they_are_dirs) | |
9a53c3a7 | 1844 | drop_nlink(old_dir); |
1da177e4 | 1845 | } else if (they_are_dirs) { |
9a53c3a7 | 1846 | drop_nlink(old_dir); |
d8c76e6f | 1847 | inc_nlink(new_dir); |
1da177e4 LT |
1848 | } |
1849 | ||
1850 | old_dir->i_size -= BOGO_DIRENT_SIZE; | |
1851 | new_dir->i_size += BOGO_DIRENT_SIZE; | |
1852 | old_dir->i_ctime = old_dir->i_mtime = | |
1853 | new_dir->i_ctime = new_dir->i_mtime = | |
1854 | inode->i_ctime = CURRENT_TIME; | |
1855 | return 0; | |
1856 | } | |
1857 | ||
1858 | static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) | |
1859 | { | |
1860 | int error; | |
1861 | int len; | |
1862 | struct inode *inode; | |
1863 | struct page *page = NULL; | |
1864 | char *kaddr; | |
1865 | struct shmem_inode_info *info; | |
1866 | ||
1867 | len = strlen(symname) + 1; | |
1868 | if (len > PAGE_CACHE_SIZE) | |
1869 | return -ENAMETOOLONG; | |
1870 | ||
1871 | inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0); | |
1872 | if (!inode) | |
1873 | return -ENOSPC; | |
1874 | ||
570bc1c2 SS |
1875 | error = security_inode_init_security(inode, dir, NULL, NULL, |
1876 | NULL); | |
1877 | if (error) { | |
1878 | if (error != -EOPNOTSUPP) { | |
1879 | iput(inode); | |
1880 | return error; | |
1881 | } | |
1882 | error = 0; | |
1883 | } | |
1884 | ||
1da177e4 LT |
1885 | info = SHMEM_I(inode); |
1886 | inode->i_size = len-1; | |
1887 | if (len <= (char *)inode - (char *)info) { | |
1888 | /* do it inline */ | |
1889 | memcpy(info, symname, len); | |
1890 | inode->i_op = &shmem_symlink_inline_operations; | |
1891 | } else { | |
1892 | error = shmem_getpage(inode, 0, &page, SGP_WRITE, NULL); | |
1893 | if (error) { | |
1894 | iput(inode); | |
1895 | return error; | |
1896 | } | |
1897 | inode->i_op = &shmem_symlink_inode_operations; | |
1898 | kaddr = kmap_atomic(page, KM_USER0); | |
1899 | memcpy(kaddr, symname, len); | |
1900 | kunmap_atomic(kaddr, KM_USER0); | |
1901 | set_page_dirty(page); | |
1902 | page_cache_release(page); | |
1903 | } | |
1904 | if (dir->i_mode & S_ISGID) | |
1905 | inode->i_gid = dir->i_gid; | |
1906 | dir->i_size += BOGO_DIRENT_SIZE; | |
1907 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; | |
1908 | d_instantiate(dentry, inode); | |
1909 | dget(dentry); | |
1910 | return 0; | |
1911 | } | |
1912 | ||
cc314eef | 1913 | static void *shmem_follow_link_inline(struct dentry *dentry, struct nameidata *nd) |
1da177e4 LT |
1914 | { |
1915 | nd_set_link(nd, (char *)SHMEM_I(dentry->d_inode)); | |
cc314eef | 1916 | return NULL; |
1da177e4 LT |
1917 | } |
1918 | ||
cc314eef | 1919 | static void *shmem_follow_link(struct dentry *dentry, struct nameidata *nd) |
1da177e4 LT |
1920 | { |
1921 | struct page *page = NULL; | |
1922 | int res = shmem_getpage(dentry->d_inode, 0, &page, SGP_READ, NULL); | |
1923 | nd_set_link(nd, res ? ERR_PTR(res) : kmap(page)); | |
cc314eef | 1924 | return page; |
1da177e4 LT |
1925 | } |
1926 | ||
cc314eef | 1927 | static void shmem_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) |
1da177e4 LT |
1928 | { |
1929 | if (!IS_ERR(nd_get_link(nd))) { | |
cc314eef | 1930 | struct page *page = cookie; |
1da177e4 LT |
1931 | kunmap(page); |
1932 | mark_page_accessed(page); | |
1933 | page_cache_release(page); | |
1da177e4 LT |
1934 | } |
1935 | } | |
1936 | ||
92e1d5be | 1937 | static const struct inode_operations shmem_symlink_inline_operations = { |
1da177e4 LT |
1938 | .readlink = generic_readlink, |
1939 | .follow_link = shmem_follow_link_inline, | |
1da177e4 LT |
1940 | }; |
1941 | ||
92e1d5be | 1942 | static const struct inode_operations shmem_symlink_inode_operations = { |
1da177e4 LT |
1943 | .truncate = shmem_truncate, |
1944 | .readlink = generic_readlink, | |
1945 | .follow_link = shmem_follow_link, | |
1946 | .put_link = shmem_put_link, | |
1da177e4 LT |
1947 | }; |
1948 | ||
39f0247d AG |
1949 | #ifdef CONFIG_TMPFS_POSIX_ACL |
1950 | /** | |
1951 | * Superblocks without xattr inode operations will get security.* xattr | |
1952 | * support from the VFS "for free". As soon as we have any other xattrs | |
1953 | * like ACLs, we also need to implement the security.* handlers at | |
1954 | * filesystem level, though. | |
1955 | */ | |
1956 | ||
1957 | static size_t shmem_xattr_security_list(struct inode *inode, char *list, | |
1958 | size_t list_len, const char *name, | |
1959 | size_t name_len) | |
1960 | { | |
1961 | return security_inode_listsecurity(inode, list, list_len); | |
1962 | } | |
1963 | ||
1964 | static int shmem_xattr_security_get(struct inode *inode, const char *name, | |
1965 | void *buffer, size_t size) | |
1966 | { | |
1967 | if (strcmp(name, "") == 0) | |
1968 | return -EINVAL; | |
1969 | return security_inode_getsecurity(inode, name, buffer, size, | |
1970 | -EOPNOTSUPP); | |
1971 | } | |
1972 | ||
1973 | static int shmem_xattr_security_set(struct inode *inode, const char *name, | |
1974 | const void *value, size_t size, int flags) | |
1975 | { | |
1976 | if (strcmp(name, "") == 0) | |
1977 | return -EINVAL; | |
1978 | return security_inode_setsecurity(inode, name, value, size, flags); | |
1979 | } | |
1980 | ||
1f370a23 | 1981 | static struct xattr_handler shmem_xattr_security_handler = { |
39f0247d AG |
1982 | .prefix = XATTR_SECURITY_PREFIX, |
1983 | .list = shmem_xattr_security_list, | |
1984 | .get = shmem_xattr_security_get, | |
1985 | .set = shmem_xattr_security_set, | |
1986 | }; | |
1987 | ||
1988 | static struct xattr_handler *shmem_xattr_handlers[] = { | |
1989 | &shmem_xattr_acl_access_handler, | |
1990 | &shmem_xattr_acl_default_handler, | |
1991 | &shmem_xattr_security_handler, | |
1992 | NULL | |
1993 | }; | |
1994 | #endif | |
1995 | ||
91828a40 DG |
1996 | static struct dentry *shmem_get_parent(struct dentry *child) |
1997 | { | |
1998 | return ERR_PTR(-ESTALE); | |
1999 | } | |
2000 | ||
2001 | static int shmem_match(struct inode *ino, void *vfh) | |
2002 | { | |
2003 | __u32 *fh = vfh; | |
2004 | __u64 inum = fh[2]; | |
2005 | inum = (inum << 32) | fh[1]; | |
2006 | return ino->i_ino == inum && fh[0] == ino->i_generation; | |
2007 | } | |
2008 | ||
2009 | static struct dentry *shmem_get_dentry(struct super_block *sb, void *vfh) | |
2010 | { | |
2011 | struct dentry *de = NULL; | |
2012 | struct inode *inode; | |
2013 | __u32 *fh = vfh; | |
2014 | __u64 inum = fh[2]; | |
2015 | inum = (inum << 32) | fh[1]; | |
2016 | ||
2017 | inode = ilookup5(sb, (unsigned long)(inum+fh[0]), shmem_match, vfh); | |
2018 | if (inode) { | |
2019 | de = d_find_alias(inode); | |
2020 | iput(inode); | |
2021 | } | |
2022 | ||
2023 | return de? de: ERR_PTR(-ESTALE); | |
2024 | } | |
2025 | ||
2026 | static struct dentry *shmem_decode_fh(struct super_block *sb, __u32 *fh, | |
2027 | int len, int type, | |
2028 | int (*acceptable)(void *context, struct dentry *de), | |
2029 | void *context) | |
2030 | { | |
2031 | if (len < 3) | |
2032 | return ERR_PTR(-ESTALE); | |
2033 | ||
2034 | return sb->s_export_op->find_exported_dentry(sb, fh, NULL, acceptable, | |
2035 | context); | |
2036 | } | |
2037 | ||
2038 | static int shmem_encode_fh(struct dentry *dentry, __u32 *fh, int *len, | |
2039 | int connectable) | |
2040 | { | |
2041 | struct inode *inode = dentry->d_inode; | |
2042 | ||
2043 | if (*len < 3) | |
2044 | return 255; | |
2045 | ||
2046 | if (hlist_unhashed(&inode->i_hash)) { | |
2047 | /* Unfortunately insert_inode_hash is not idempotent, | |
2048 | * so as we hash inodes here rather than at creation | |
2049 | * time, we need a lock to ensure we only try | |
2050 | * to do it once | |
2051 | */ | |
2052 | static DEFINE_SPINLOCK(lock); | |
2053 | spin_lock(&lock); | |
2054 | if (hlist_unhashed(&inode->i_hash)) | |
2055 | __insert_inode_hash(inode, | |
2056 | inode->i_ino + inode->i_generation); | |
2057 | spin_unlock(&lock); | |
2058 | } | |
2059 | ||
2060 | fh[0] = inode->i_generation; | |
2061 | fh[1] = inode->i_ino; | |
2062 | fh[2] = ((__u64)inode->i_ino) >> 32; | |
2063 | ||
2064 | *len = 3; | |
2065 | return 1; | |
2066 | } | |
2067 | ||
2068 | static struct export_operations shmem_export_ops = { | |
2069 | .get_parent = shmem_get_parent, | |
2070 | .get_dentry = shmem_get_dentry, | |
2071 | .encode_fh = shmem_encode_fh, | |
2072 | .decode_fh = shmem_decode_fh, | |
2073 | }; | |
2074 | ||
7339ff83 RH |
2075 | static int shmem_parse_options(char *options, int *mode, uid_t *uid, |
2076 | gid_t *gid, unsigned long *blocks, unsigned long *inodes, | |
2077 | int *policy, nodemask_t *policy_nodes) | |
1da177e4 LT |
2078 | { |
2079 | char *this_char, *value, *rest; | |
2080 | ||
b00dc3ad HD |
2081 | while (options != NULL) { |
2082 | this_char = options; | |
2083 | for (;;) { | |
2084 | /* | |
2085 | * NUL-terminate this option: unfortunately, | |
2086 | * mount options form a comma-separated list, | |
2087 | * but mpol's nodelist may also contain commas. | |
2088 | */ | |
2089 | options = strchr(options, ','); | |
2090 | if (options == NULL) | |
2091 | break; | |
2092 | options++; | |
2093 | if (!isdigit(*options)) { | |
2094 | options[-1] = '\0'; | |
2095 | break; | |
2096 | } | |
2097 | } | |
1da177e4 LT |
2098 | if (!*this_char) |
2099 | continue; | |
2100 | if ((value = strchr(this_char,'=')) != NULL) { | |
2101 | *value++ = 0; | |
2102 | } else { | |
2103 | printk(KERN_ERR | |
2104 | "tmpfs: No value for mount option '%s'\n", | |
2105 | this_char); | |
2106 | return 1; | |
2107 | } | |
2108 | ||
2109 | if (!strcmp(this_char,"size")) { | |
2110 | unsigned long long size; | |
2111 | size = memparse(value,&rest); | |
2112 | if (*rest == '%') { | |
2113 | size <<= PAGE_SHIFT; | |
2114 | size *= totalram_pages; | |
2115 | do_div(size, 100); | |
2116 | rest++; | |
2117 | } | |
2118 | if (*rest) | |
2119 | goto bad_val; | |
2120 | *blocks = size >> PAGE_CACHE_SHIFT; | |
2121 | } else if (!strcmp(this_char,"nr_blocks")) { | |
2122 | *blocks = memparse(value,&rest); | |
2123 | if (*rest) | |
2124 | goto bad_val; | |
2125 | } else if (!strcmp(this_char,"nr_inodes")) { | |
2126 | *inodes = memparse(value,&rest); | |
2127 | if (*rest) | |
2128 | goto bad_val; | |
2129 | } else if (!strcmp(this_char,"mode")) { | |
2130 | if (!mode) | |
2131 | continue; | |
2132 | *mode = simple_strtoul(value,&rest,8); | |
2133 | if (*rest) | |
2134 | goto bad_val; | |
2135 | } else if (!strcmp(this_char,"uid")) { | |
2136 | if (!uid) | |
2137 | continue; | |
2138 | *uid = simple_strtoul(value,&rest,0); | |
2139 | if (*rest) | |
2140 | goto bad_val; | |
2141 | } else if (!strcmp(this_char,"gid")) { | |
2142 | if (!gid) | |
2143 | continue; | |
2144 | *gid = simple_strtoul(value,&rest,0); | |
2145 | if (*rest) | |
2146 | goto bad_val; | |
7339ff83 | 2147 | } else if (!strcmp(this_char,"mpol")) { |
b00dc3ad | 2148 | if (shmem_parse_mpol(value,policy,policy_nodes)) |
7339ff83 | 2149 | goto bad_val; |
1da177e4 LT |
2150 | } else { |
2151 | printk(KERN_ERR "tmpfs: Bad mount option %s\n", | |
2152 | this_char); | |
2153 | return 1; | |
2154 | } | |
2155 | } | |
2156 | return 0; | |
2157 | ||
2158 | bad_val: | |
2159 | printk(KERN_ERR "tmpfs: Bad value '%s' for mount option '%s'\n", | |
2160 | value, this_char); | |
2161 | return 1; | |
2162 | ||
2163 | } | |
2164 | ||
2165 | static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) | |
2166 | { | |
2167 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); | |
0edd73b3 HD |
2168 | unsigned long max_blocks = sbinfo->max_blocks; |
2169 | unsigned long max_inodes = sbinfo->max_inodes; | |
7339ff83 RH |
2170 | int policy = sbinfo->policy; |
2171 | nodemask_t policy_nodes = sbinfo->policy_nodes; | |
0edd73b3 HD |
2172 | unsigned long blocks; |
2173 | unsigned long inodes; | |
2174 | int error = -EINVAL; | |
2175 | ||
7339ff83 RH |
2176 | if (shmem_parse_options(data, NULL, NULL, NULL, &max_blocks, |
2177 | &max_inodes, &policy, &policy_nodes)) | |
0edd73b3 | 2178 | return error; |
1da177e4 | 2179 | |
0edd73b3 HD |
2180 | spin_lock(&sbinfo->stat_lock); |
2181 | blocks = sbinfo->max_blocks - sbinfo->free_blocks; | |
2182 | inodes = sbinfo->max_inodes - sbinfo->free_inodes; | |
2183 | if (max_blocks < blocks) | |
2184 | goto out; | |
2185 | if (max_inodes < inodes) | |
2186 | goto out; | |
2187 | /* | |
2188 | * Those tests also disallow limited->unlimited while any are in | |
2189 | * use, so i_blocks will always be zero when max_blocks is zero; | |
2190 | * but we must separately disallow unlimited->limited, because | |
2191 | * in that case we have no record of how much is already in use. | |
2192 | */ | |
2193 | if (max_blocks && !sbinfo->max_blocks) | |
2194 | goto out; | |
2195 | if (max_inodes && !sbinfo->max_inodes) | |
2196 | goto out; | |
2197 | ||
2198 | error = 0; | |
2199 | sbinfo->max_blocks = max_blocks; | |
2200 | sbinfo->free_blocks = max_blocks - blocks; | |
2201 | sbinfo->max_inodes = max_inodes; | |
2202 | sbinfo->free_inodes = max_inodes - inodes; | |
7339ff83 RH |
2203 | sbinfo->policy = policy; |
2204 | sbinfo->policy_nodes = policy_nodes; | |
0edd73b3 HD |
2205 | out: |
2206 | spin_unlock(&sbinfo->stat_lock); | |
2207 | return error; | |
1da177e4 LT |
2208 | } |
2209 | #endif | |
2210 | ||
2211 | static void shmem_put_super(struct super_block *sb) | |
2212 | { | |
2213 | kfree(sb->s_fs_info); | |
2214 | sb->s_fs_info = NULL; | |
2215 | } | |
2216 | ||
1da177e4 LT |
2217 | static int shmem_fill_super(struct super_block *sb, |
2218 | void *data, int silent) | |
2219 | { | |
2220 | struct inode *inode; | |
2221 | struct dentry *root; | |
2222 | int mode = S_IRWXUGO | S_ISVTX; | |
2223 | uid_t uid = current->fsuid; | |
2224 | gid_t gid = current->fsgid; | |
2225 | int err = -ENOMEM; | |
0edd73b3 | 2226 | struct shmem_sb_info *sbinfo; |
1da177e4 LT |
2227 | unsigned long blocks = 0; |
2228 | unsigned long inodes = 0; | |
7339ff83 RH |
2229 | int policy = MPOL_DEFAULT; |
2230 | nodemask_t policy_nodes = node_online_map; | |
1da177e4 | 2231 | |
0edd73b3 | 2232 | #ifdef CONFIG_TMPFS |
1da177e4 LT |
2233 | /* |
2234 | * Per default we only allow half of the physical ram per | |
2235 | * tmpfs instance, limiting inodes to one per page of lowmem; | |
2236 | * but the internal instance is left unlimited. | |
2237 | */ | |
2238 | if (!(sb->s_flags & MS_NOUSER)) { | |
2239 | blocks = totalram_pages / 2; | |
2240 | inodes = totalram_pages - totalhigh_pages; | |
2241 | if (inodes > blocks) | |
2242 | inodes = blocks; | |
7339ff83 RH |
2243 | if (shmem_parse_options(data, &mode, &uid, &gid, &blocks, |
2244 | &inodes, &policy, &policy_nodes)) | |
1da177e4 LT |
2245 | return -EINVAL; |
2246 | } | |
91828a40 | 2247 | sb->s_export_op = &shmem_export_ops; |
1da177e4 LT |
2248 | #else |
2249 | sb->s_flags |= MS_NOUSER; | |
2250 | #endif | |
2251 | ||
0edd73b3 HD |
2252 | /* Round up to L1_CACHE_BYTES to resist false sharing */ |
2253 | sbinfo = kmalloc(max((int)sizeof(struct shmem_sb_info), | |
2254 | L1_CACHE_BYTES), GFP_KERNEL); | |
2255 | if (!sbinfo) | |
2256 | return -ENOMEM; | |
2257 | ||
2258 | spin_lock_init(&sbinfo->stat_lock); | |
2259 | sbinfo->max_blocks = blocks; | |
2260 | sbinfo->free_blocks = blocks; | |
2261 | sbinfo->max_inodes = inodes; | |
2262 | sbinfo->free_inodes = inodes; | |
7339ff83 RH |
2263 | sbinfo->policy = policy; |
2264 | sbinfo->policy_nodes = policy_nodes; | |
0edd73b3 HD |
2265 | |
2266 | sb->s_fs_info = sbinfo; | |
1da177e4 LT |
2267 | sb->s_maxbytes = SHMEM_MAX_BYTES; |
2268 | sb->s_blocksize = PAGE_CACHE_SIZE; | |
2269 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | |
2270 | sb->s_magic = TMPFS_MAGIC; | |
2271 | sb->s_op = &shmem_ops; | |
cfd95a9c | 2272 | sb->s_time_gran = 1; |
39f0247d AG |
2273 | #ifdef CONFIG_TMPFS_POSIX_ACL |
2274 | sb->s_xattr = shmem_xattr_handlers; | |
2275 | sb->s_flags |= MS_POSIXACL; | |
2276 | #endif | |
0edd73b3 | 2277 | |
1da177e4 LT |
2278 | inode = shmem_get_inode(sb, S_IFDIR | mode, 0); |
2279 | if (!inode) | |
2280 | goto failed; | |
2281 | inode->i_uid = uid; | |
2282 | inode->i_gid = gid; | |
2283 | root = d_alloc_root(inode); | |
2284 | if (!root) | |
2285 | goto failed_iput; | |
2286 | sb->s_root = root; | |
2287 | return 0; | |
2288 | ||
2289 | failed_iput: | |
2290 | iput(inode); | |
2291 | failed: | |
2292 | shmem_put_super(sb); | |
2293 | return err; | |
2294 | } | |
2295 | ||
fcc234f8 | 2296 | static struct kmem_cache *shmem_inode_cachep; |
1da177e4 LT |
2297 | |
2298 | static struct inode *shmem_alloc_inode(struct super_block *sb) | |
2299 | { | |
2300 | struct shmem_inode_info *p; | |
e94b1766 | 2301 | p = (struct shmem_inode_info *)kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); |
1da177e4 LT |
2302 | if (!p) |
2303 | return NULL; | |
2304 | return &p->vfs_inode; | |
2305 | } | |
2306 | ||
2307 | static void shmem_destroy_inode(struct inode *inode) | |
2308 | { | |
2309 | if ((inode->i_mode & S_IFMT) == S_IFREG) { | |
2310 | /* only struct inode is valid if it's an inline symlink */ | |
2311 | mpol_free_shared_policy(&SHMEM_I(inode)->policy); | |
2312 | } | |
39f0247d | 2313 | shmem_acl_destroy_inode(inode); |
1da177e4 LT |
2314 | kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); |
2315 | } | |
2316 | ||
fcc234f8 PE |
2317 | static void init_once(void *foo, struct kmem_cache *cachep, |
2318 | unsigned long flags) | |
1da177e4 LT |
2319 | { |
2320 | struct shmem_inode_info *p = (struct shmem_inode_info *) foo; | |
2321 | ||
a35afb83 | 2322 | inode_init_once(&p->vfs_inode); |
39f0247d | 2323 | #ifdef CONFIG_TMPFS_POSIX_ACL |
a35afb83 CL |
2324 | p->i_acl = NULL; |
2325 | p->i_default_acl = NULL; | |
39f0247d | 2326 | #endif |
1da177e4 LT |
2327 | } |
2328 | ||
2329 | static int init_inodecache(void) | |
2330 | { | |
2331 | shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", | |
2332 | sizeof(struct shmem_inode_info), | |
2333 | 0, 0, init_once, NULL); | |
2334 | if (shmem_inode_cachep == NULL) | |
2335 | return -ENOMEM; | |
2336 | return 0; | |
2337 | } | |
2338 | ||
2339 | static void destroy_inodecache(void) | |
2340 | { | |
1a1d92c1 | 2341 | kmem_cache_destroy(shmem_inode_cachep); |
1da177e4 LT |
2342 | } |
2343 | ||
f5e54d6e | 2344 | static const struct address_space_operations shmem_aops = { |
1da177e4 | 2345 | .writepage = shmem_writepage, |
76719325 | 2346 | .set_page_dirty = __set_page_dirty_no_writeback, |
1da177e4 | 2347 | #ifdef CONFIG_TMPFS |
ae976416 | 2348 | .readpage = shmem_readpage, |
1da177e4 LT |
2349 | .prepare_write = shmem_prepare_write, |
2350 | .commit_write = simple_commit_write, | |
2351 | #endif | |
304dbdb7 | 2352 | .migratepage = migrate_page, |
1da177e4 LT |
2353 | }; |
2354 | ||
15ad7cdc | 2355 | static const struct file_operations shmem_file_operations = { |
1da177e4 LT |
2356 | .mmap = shmem_mmap, |
2357 | #ifdef CONFIG_TMPFS | |
2358 | .llseek = generic_file_llseek, | |
2359 | .read = shmem_file_read, | |
2360 | .write = shmem_file_write, | |
2361 | .fsync = simple_sync_file, | |
ae976416 HD |
2362 | .splice_read = generic_file_splice_read, |
2363 | .splice_write = generic_file_splice_write, | |
1da177e4 LT |
2364 | #endif |
2365 | }; | |
2366 | ||
92e1d5be | 2367 | static const struct inode_operations shmem_inode_operations = { |
1da177e4 LT |
2368 | .truncate = shmem_truncate, |
2369 | .setattr = shmem_notify_change, | |
f6b3ec23 | 2370 | .truncate_range = shmem_truncate_range, |
39f0247d AG |
2371 | #ifdef CONFIG_TMPFS_POSIX_ACL |
2372 | .setxattr = generic_setxattr, | |
2373 | .getxattr = generic_getxattr, | |
2374 | .listxattr = generic_listxattr, | |
2375 | .removexattr = generic_removexattr, | |
2376 | .permission = shmem_permission, | |
2377 | #endif | |
2378 | ||
1da177e4 LT |
2379 | }; |
2380 | ||
92e1d5be | 2381 | static const struct inode_operations shmem_dir_inode_operations = { |
1da177e4 LT |
2382 | #ifdef CONFIG_TMPFS |
2383 | .create = shmem_create, | |
2384 | .lookup = simple_lookup, | |
2385 | .link = shmem_link, | |
2386 | .unlink = shmem_unlink, | |
2387 | .symlink = shmem_symlink, | |
2388 | .mkdir = shmem_mkdir, | |
2389 | .rmdir = shmem_rmdir, | |
2390 | .mknod = shmem_mknod, | |
2391 | .rename = shmem_rename, | |
1da177e4 | 2392 | #endif |
39f0247d AG |
2393 | #ifdef CONFIG_TMPFS_POSIX_ACL |
2394 | .setattr = shmem_notify_change, | |
2395 | .setxattr = generic_setxattr, | |
2396 | .getxattr = generic_getxattr, | |
2397 | .listxattr = generic_listxattr, | |
2398 | .removexattr = generic_removexattr, | |
2399 | .permission = shmem_permission, | |
2400 | #endif | |
2401 | }; | |
2402 | ||
92e1d5be | 2403 | static const struct inode_operations shmem_special_inode_operations = { |
39f0247d AG |
2404 | #ifdef CONFIG_TMPFS_POSIX_ACL |
2405 | .setattr = shmem_notify_change, | |
2406 | .setxattr = generic_setxattr, | |
2407 | .getxattr = generic_getxattr, | |
2408 | .listxattr = generic_listxattr, | |
2409 | .removexattr = generic_removexattr, | |
2410 | .permission = shmem_permission, | |
2411 | #endif | |
1da177e4 LT |
2412 | }; |
2413 | ||
759b9775 | 2414 | static const struct super_operations shmem_ops = { |
1da177e4 LT |
2415 | .alloc_inode = shmem_alloc_inode, |
2416 | .destroy_inode = shmem_destroy_inode, | |
2417 | #ifdef CONFIG_TMPFS | |
2418 | .statfs = shmem_statfs, | |
2419 | .remount_fs = shmem_remount_fs, | |
2420 | #endif | |
2421 | .delete_inode = shmem_delete_inode, | |
2422 | .drop_inode = generic_delete_inode, | |
2423 | .put_super = shmem_put_super, | |
2424 | }; | |
2425 | ||
2426 | static struct vm_operations_struct shmem_vm_ops = { | |
54cb8821 | 2427 | .fault = shmem_fault, |
1da177e4 LT |
2428 | #ifdef CONFIG_NUMA |
2429 | .set_policy = shmem_set_policy, | |
2430 | .get_policy = shmem_get_policy, | |
2431 | #endif | |
2432 | }; | |
2433 | ||
2434 | ||
454e2398 DH |
2435 | static int shmem_get_sb(struct file_system_type *fs_type, |
2436 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) | |
1da177e4 | 2437 | { |
454e2398 | 2438 | return get_sb_nodev(fs_type, flags, data, shmem_fill_super, mnt); |
1da177e4 LT |
2439 | } |
2440 | ||
2441 | static struct file_system_type tmpfs_fs_type = { | |
2442 | .owner = THIS_MODULE, | |
2443 | .name = "tmpfs", | |
2444 | .get_sb = shmem_get_sb, | |
2445 | .kill_sb = kill_litter_super, | |
2446 | }; | |
2447 | static struct vfsmount *shm_mnt; | |
2448 | ||
2449 | static int __init init_tmpfs(void) | |
2450 | { | |
2451 | int error; | |
2452 | ||
2453 | error = init_inodecache(); | |
2454 | if (error) | |
2455 | goto out3; | |
2456 | ||
2457 | error = register_filesystem(&tmpfs_fs_type); | |
2458 | if (error) { | |
2459 | printk(KERN_ERR "Could not register tmpfs\n"); | |
2460 | goto out2; | |
2461 | } | |
95dc112a | 2462 | |
1f5ce9e9 | 2463 | shm_mnt = vfs_kern_mount(&tmpfs_fs_type, MS_NOUSER, |
1da177e4 LT |
2464 | tmpfs_fs_type.name, NULL); |
2465 | if (IS_ERR(shm_mnt)) { | |
2466 | error = PTR_ERR(shm_mnt); | |
2467 | printk(KERN_ERR "Could not kern_mount tmpfs\n"); | |
2468 | goto out1; | |
2469 | } | |
2470 | return 0; | |
2471 | ||
2472 | out1: | |
2473 | unregister_filesystem(&tmpfs_fs_type); | |
2474 | out2: | |
2475 | destroy_inodecache(); | |
2476 | out3: | |
2477 | shm_mnt = ERR_PTR(error); | |
2478 | return error; | |
2479 | } | |
2480 | module_init(init_tmpfs) | |
2481 | ||
2482 | /* | |
2483 | * shmem_file_setup - get an unlinked file living in tmpfs | |
2484 | * | |
2485 | * @name: name for dentry (to be seen in /proc/<pid>/maps | |
2486 | * @size: size to be set for the file | |
2487 | * | |
2488 | */ | |
2489 | struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags) | |
2490 | { | |
2491 | int error; | |
2492 | struct file *file; | |
2493 | struct inode *inode; | |
2494 | struct dentry *dentry, *root; | |
2495 | struct qstr this; | |
2496 | ||
2497 | if (IS_ERR(shm_mnt)) | |
2498 | return (void *)shm_mnt; | |
2499 | ||
2500 | if (size < 0 || size > SHMEM_MAX_BYTES) | |
2501 | return ERR_PTR(-EINVAL); | |
2502 | ||
2503 | if (shmem_acct_size(flags, size)) | |
2504 | return ERR_PTR(-ENOMEM); | |
2505 | ||
2506 | error = -ENOMEM; | |
2507 | this.name = name; | |
2508 | this.len = strlen(name); | |
2509 | this.hash = 0; /* will go */ | |
2510 | root = shm_mnt->mnt_root; | |
2511 | dentry = d_alloc(root, &this); | |
2512 | if (!dentry) | |
2513 | goto put_memory; | |
2514 | ||
2515 | error = -ENFILE; | |
2516 | file = get_empty_filp(); | |
2517 | if (!file) | |
2518 | goto put_dentry; | |
2519 | ||
2520 | error = -ENOSPC; | |
2521 | inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0); | |
2522 | if (!inode) | |
2523 | goto close_file; | |
2524 | ||
2525 | SHMEM_I(inode)->flags = flags & VM_ACCOUNT; | |
2526 | d_instantiate(dentry, inode); | |
2527 | inode->i_size = size; | |
2528 | inode->i_nlink = 0; /* It is unlinked */ | |
d3ac7f89 JJS |
2529 | file->f_path.mnt = mntget(shm_mnt); |
2530 | file->f_path.dentry = dentry; | |
1da177e4 LT |
2531 | file->f_mapping = inode->i_mapping; |
2532 | file->f_op = &shmem_file_operations; | |
2533 | file->f_mode = FMODE_WRITE | FMODE_READ; | |
2534 | return file; | |
2535 | ||
2536 | close_file: | |
2537 | put_filp(file); | |
2538 | put_dentry: | |
2539 | dput(dentry); | |
2540 | put_memory: | |
2541 | shmem_unacct_size(flags, size); | |
2542 | return ERR_PTR(error); | |
2543 | } | |
2544 | ||
2545 | /* | |
2546 | * shmem_zero_setup - setup a shared anonymous mapping | |
2547 | * | |
2548 | * @vma: the vma to be mmapped is prepared by do_mmap_pgoff | |
2549 | */ | |
2550 | int shmem_zero_setup(struct vm_area_struct *vma) | |
2551 | { | |
2552 | struct file *file; | |
2553 | loff_t size = vma->vm_end - vma->vm_start; | |
2554 | ||
2555 | file = shmem_file_setup("dev/zero", size, vma->vm_flags); | |
2556 | if (IS_ERR(file)) | |
2557 | return PTR_ERR(file); | |
2558 | ||
2559 | if (vma->vm_file) | |
2560 | fput(vma->vm_file); | |
2561 | vma->vm_file = file; | |
2562 | vma->vm_ops = &shmem_vm_ops; | |
d00806b1 | 2563 | vma->vm_flags |= VM_CAN_INVALIDATE; |
1da177e4 LT |
2564 | return 0; |
2565 | } |