shmem.c (2e31add2a7e2a15d07f592c21ba35870fa9a1d1f) shmem.c (0b0a0806b0d8635e046bf533225a25903b1cddce)
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.

--- 155 unchanged lines hidden (view full) ---

164/*
165 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
166 * for shared memory and for shared anonymous (/dev/zero) mappings
167 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
168 * consistent with the pre-accounting of private mappings ...
169 */
170static inline int shmem_acct_size(unsigned long flags, loff_t size)
171{
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.

--- 155 unchanged lines hidden (view full) ---

164/*
165 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
166 * for shared memory and for shared anonymous (/dev/zero) mappings
167 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
168 * consistent with the pre-accounting of private mappings ...
169 */
170static inline int shmem_acct_size(unsigned long flags, loff_t size)
171{
172 return (flags & VM_ACCOUNT) ?
173 security_vm_enough_memory_kern(VM_ACCT(size)) : 0;
172 return (flags & VM_NORESERVE) ?
173 0 : security_vm_enough_memory_kern(VM_ACCT(size));
174}
175
176static inline void shmem_unacct_size(unsigned long flags, loff_t size)
177{
174}
175
176static inline void shmem_unacct_size(unsigned long flags, loff_t size)
177{
178 if (flags & VM_ACCOUNT)
178 if (!(flags & VM_NORESERVE))
179 vm_unacct_memory(VM_ACCT(size));
180}
181
182/*
183 * ... whereas tmpfs objects are accounted incrementally as
184 * pages are allocated, in order to allow huge sparse files.
185 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
186 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
187 */
188static inline int shmem_acct_block(unsigned long flags)
189{
179 vm_unacct_memory(VM_ACCT(size));
180}
181
182/*
183 * ... whereas tmpfs objects are accounted incrementally as
184 * pages are allocated, in order to allow huge sparse files.
185 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
186 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
187 */
188static inline int shmem_acct_block(unsigned long flags)
189{
190 return (flags & VM_ACCOUNT) ?
191 0 : security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE));
190 return (flags & VM_NORESERVE) ?
191 security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
192}
193
194static inline void shmem_unacct_blocks(unsigned long flags, long pages)
195{
192}
193
194static inline void shmem_unacct_blocks(unsigned long flags, long pages)
195{
196 if (!(flags & VM_ACCOUNT))
196 if (flags & VM_NORESERVE)
197 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
198}
199
200static const struct super_operations shmem_ops;
201static const struct address_space_operations shmem_aops;
202static const struct file_operations shmem_file_operations;
203static const struct inode_operations shmem_inode_operations;
204static const struct inode_operations shmem_dir_inode_operations;

--- 1305 unchanged lines hidden (view full) ---

1510static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1511{
1512 file_accessed(file);
1513 vma->vm_ops = &shmem_vm_ops;
1514 vma->vm_flags |= VM_CAN_NONLINEAR;
1515 return 0;
1516}
1517
197 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
198}
199
200static const struct super_operations shmem_ops;
201static const struct address_space_operations shmem_aops;
202static const struct file_operations shmem_file_operations;
203static const struct inode_operations shmem_inode_operations;
204static const struct inode_operations shmem_dir_inode_operations;

--- 1305 unchanged lines hidden (view full) ---

1510static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1511{
1512 file_accessed(file);
1513 vma->vm_ops = &shmem_vm_ops;
1514 vma->vm_flags |= VM_CAN_NONLINEAR;
1515 return 0;
1516}
1517
1518static struct inode *
1519shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
1518static struct inode *shmem_get_inode(struct super_block *sb, int mode,
1519 dev_t dev, unsigned long flags)
1520{
1521 struct inode *inode;
1522 struct shmem_inode_info *info;
1523 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1524
1525 if (shmem_reserve_inode(sb))
1526 return NULL;
1527

--- 4 unchanged lines hidden (view full) ---

1532 inode->i_gid = current_fsgid();
1533 inode->i_blocks = 0;
1534 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1535 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1536 inode->i_generation = get_seconds();
1537 info = SHMEM_I(inode);
1538 memset(info, 0, (char *)inode - (char *)info);
1539 spin_lock_init(&info->lock);
1520{
1521 struct inode *inode;
1522 struct shmem_inode_info *info;
1523 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
1524
1525 if (shmem_reserve_inode(sb))
1526 return NULL;
1527

--- 4 unchanged lines hidden (view full) ---

1532 inode->i_gid = current_fsgid();
1533 inode->i_blocks = 0;
1534 inode->i_mapping->backing_dev_info = &shmem_backing_dev_info;
1535 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1536 inode->i_generation = get_seconds();
1537 info = SHMEM_I(inode);
1538 memset(info, 0, (char *)inode - (char *)info);
1539 spin_lock_init(&info->lock);
1540 info->flags = flags & VM_NORESERVE;
1540 INIT_LIST_HEAD(&info->swaplist);
1541
1542 switch (mode & S_IFMT) {
1543 default:
1544 inode->i_op = &shmem_special_inode_operations;
1545 init_special_inode(inode, mode, dev);
1546 break;
1547 case S_IFREG:

--- 226 unchanged lines hidden (view full) ---

1774}
1775
1776/*
1777 * File creation. Allocate an inode, and we're done..
1778 */
1779static int
1780shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1781{
1541 INIT_LIST_HEAD(&info->swaplist);
1542
1543 switch (mode & S_IFMT) {
1544 default:
1545 inode->i_op = &shmem_special_inode_operations;
1546 init_special_inode(inode, mode, dev);
1547 break;
1548 case S_IFREG:

--- 226 unchanged lines hidden (view full) ---

1775}
1776
1777/*
1778 * File creation. Allocate an inode, and we're done..
1779 */
1780static int
1781shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1782{
1782 struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev);
1783 struct inode *inode;
1783 int error = -ENOSPC;
1784
1784 int error = -ENOSPC;
1785
1786 inode = shmem_get_inode(dir->i_sb, mode, dev, VM_NORESERVE);
1785 if (inode) {
1786 error = security_inode_init_security(inode, dir, NULL, NULL,
1787 NULL);
1788 if (error) {
1789 if (error != -EOPNOTSUPP) {
1790 iput(inode);
1791 return error;
1792 }

--- 122 unchanged lines hidden (view full) ---

1915 struct page *page = NULL;
1916 char *kaddr;
1917 struct shmem_inode_info *info;
1918
1919 len = strlen(symname) + 1;
1920 if (len > PAGE_CACHE_SIZE)
1921 return -ENAMETOOLONG;
1922
1787 if (inode) {
1788 error = security_inode_init_security(inode, dir, NULL, NULL,
1789 NULL);
1790 if (error) {
1791 if (error != -EOPNOTSUPP) {
1792 iput(inode);
1793 return error;
1794 }

--- 122 unchanged lines hidden (view full) ---

1917 struct page *page = NULL;
1918 char *kaddr;
1919 struct shmem_inode_info *info;
1920
1921 len = strlen(symname) + 1;
1922 if (len > PAGE_CACHE_SIZE)
1923 return -ENAMETOOLONG;
1924
1923 inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0);
1925 inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
1924 if (!inode)
1925 return -ENOSPC;
1926
1927 error = security_inode_init_security(inode, dir, NULL, NULL,
1928 NULL);
1929 if (error) {
1930 if (error != -EOPNOTSUPP) {
1931 iput(inode);

--- 395 unchanged lines hidden (view full) ---

2327 sb->s_magic = TMPFS_MAGIC;
2328 sb->s_op = &shmem_ops;
2329 sb->s_time_gran = 1;
2330#ifdef CONFIG_TMPFS_POSIX_ACL
2331 sb->s_xattr = shmem_xattr_handlers;
2332 sb->s_flags |= MS_POSIXACL;
2333#endif
2334
1926 if (!inode)
1927 return -ENOSPC;
1928
1929 error = security_inode_init_security(inode, dir, NULL, NULL,
1930 NULL);
1931 if (error) {
1932 if (error != -EOPNOTSUPP) {
1933 iput(inode);

--- 395 unchanged lines hidden (view full) ---

2329 sb->s_magic = TMPFS_MAGIC;
2330 sb->s_op = &shmem_ops;
2331 sb->s_time_gran = 1;
2332#ifdef CONFIG_TMPFS_POSIX_ACL
2333 sb->s_xattr = shmem_xattr_handlers;
2334 sb->s_flags |= MS_POSIXACL;
2335#endif
2336
2335 inode = shmem_get_inode(sb, S_IFDIR | sbinfo->mode, 0);
2337 inode = shmem_get_inode(sb, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
2336 if (!inode)
2337 goto failed;
2338 inode->i_uid = sbinfo->uid;
2339 inode->i_gid = sbinfo->gid;
2340 root = d_alloc_root(inode);
2341 if (!root)
2342 goto failed_iput;
2343 sb->s_root = root;

--- 225 unchanged lines hidden (view full) ---

2569 return 0;
2570}
2571
2572int shmem_unuse(swp_entry_t entry, struct page *page)
2573{
2574 return 0;
2575}
2576
2338 if (!inode)
2339 goto failed;
2340 inode->i_uid = sbinfo->uid;
2341 inode->i_gid = sbinfo->gid;
2342 root = d_alloc_root(inode);
2343 if (!root)
2344 goto failed_iput;
2345 sb->s_root = root;

--- 225 unchanged lines hidden (view full) ---

2571 return 0;
2572}
2573
2574int shmem_unuse(swp_entry_t entry, struct page *page)
2575{
2576 return 0;
2577}
2578
2577#define shmem_file_operations ramfs_file_operations
2578#define shmem_vm_ops generic_file_vm_ops
2579#define shmem_get_inode ramfs_get_inode
2580#define shmem_acct_size(a, b) 0
2581#define shmem_unacct_size(a, b) do {} while (0)
2582#define SHMEM_MAX_BYTES LLONG_MAX
2579#define shmem_vm_ops generic_file_vm_ops
2580#define shmem_file_operations ramfs_file_operations
2581#define shmem_get_inode(sb, mode, dev, flags) ramfs_get_inode(sb, mode, dev)
2582#define shmem_acct_size(flags, size) 0
2583#define shmem_unacct_size(flags, size) do {} while (0)
2584#define SHMEM_MAX_BYTES LLONG_MAX
2583
2584#endif /* CONFIG_SHMEM */
2585
2586/* common code */
2587
2588/**
2589 * shmem_file_setup - get an unlinked file living in tmpfs
2590 * @name: name for dentry (to be seen in /proc/<pid>/maps
2591 * @size: size to be set for the file
2585
2586#endif /* CONFIG_SHMEM */
2587
2588/* common code */
2589
2590/**
2591 * shmem_file_setup - get an unlinked file living in tmpfs
2592 * @name: name for dentry (to be seen in /proc/<pid>/maps
2593 * @size: size to be set for the file
2592 * @flags: vm_flags
2594 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
2593 */
2594struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
2595{
2596 int error;
2597 struct file *file;
2598 struct inode *inode;
2599 struct dentry *dentry, *root;
2600 struct qstr this;

--- 17 unchanged lines hidden (view full) ---

2618 goto put_memory;
2619
2620 error = -ENFILE;
2621 file = get_empty_filp();
2622 if (!file)
2623 goto put_dentry;
2624
2625 error = -ENOSPC;
2595 */
2596struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
2597{
2598 int error;
2599 struct file *file;
2600 struct inode *inode;
2601 struct dentry *dentry, *root;
2602 struct qstr this;

--- 17 unchanged lines hidden (view full) ---

2620 goto put_memory;
2621
2622 error = -ENFILE;
2623 file = get_empty_filp();
2624 if (!file)
2625 goto put_dentry;
2626
2627 error = -ENOSPC;
2626 inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0);
2628 inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0, flags);
2627 if (!inode)
2628 goto close_file;
2629
2629 if (!inode)
2630 goto close_file;
2631
2630#ifdef CONFIG_SHMEM
2631 SHMEM_I(inode)->flags = (flags & VM_NORESERVE) ? 0 : VM_ACCOUNT;
2632#endif
2633 d_instantiate(dentry, inode);
2634 inode->i_size = size;
2635 inode->i_nlink = 0; /* It is unlinked */
2636 init_file(file, shm_mnt, dentry, FMODE_WRITE | FMODE_READ,
2637 &shmem_file_operations);
2638
2639#ifndef CONFIG_MMU
2640 error = ramfs_nommu_expand_for_mapping(inode, size);

--- 36 unchanged lines hidden ---
2632 d_instantiate(dentry, inode);
2633 inode->i_size = size;
2634 inode->i_nlink = 0; /* It is unlinked */
2635 init_file(file, shm_mnt, dentry, FMODE_WRITE | FMODE_READ,
2636 &shmem_file_operations);
2637
2638#ifndef CONFIG_MMU
2639 error = ramfs_nommu_expand_for_mapping(inode, size);

--- 36 unchanged lines hidden ---