Merge tag 'xfs-6.4-rc1-fixes' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[linux-block.git] / include / linux / shmem_fs.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef __SHMEM_FS_H
3#define __SHMEM_FS_H
4
40e041a2 5#include <linux/file.h>
1da177e4
LT
6#include <linux/swap.h>
7#include <linux/mempolicy.h>
d9d90e5e 8#include <linux/pagemap.h>
7e496299 9#include <linux/percpu_counter.h>
38f38657 10#include <linux/xattr.h>
d7167b14 11#include <linux/fs_parser.h>
d9712937 12#include <linux/userfaultfd_k.h>
1da177e4
LT
13
14/* inode in-kernel data */
15
1da177e4
LT
16struct shmem_inode_info {
17 spinlock_t lock;
40e041a2 18 unsigned int seals; /* shmem seals */
1da177e4
LT
19 unsigned long flags;
20 unsigned long alloced; /* data pages alloced to file */
3ed47db3 21 unsigned long swapped; /* subtotal assigned to swap */
d144bf62 22 pgoff_t fallocend; /* highest fallocate endindex */
779750d2 23 struct list_head shrinklist; /* shrinkable hpage inodes */
1da177e4 24 struct list_head swaplist; /* chain of maybes on swap */
779750d2 25 struct shared_policy policy; /* NUMA memory alloc policy */
38f38657 26 struct simple_xattrs xattrs; /* list of xattrs */
af53d3e9 27 atomic_t stop_eviction; /* hold when working on inode */
f7cd16a5 28 struct timespec64 i_crtime; /* file creation time */
e408e695 29 unsigned int fsflags; /* flags for FS_IOC_[SG]ETFLAGS */
1da177e4
LT
30 struct inode vfs_inode;
31};
32
cb241339
HD
33#define SHMEM_FL_USER_VISIBLE FS_FL_USER_VISIBLE
34#define SHMEM_FL_USER_MODIFIABLE \
35 (FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL | FS_NOATIME_FL)
36#define SHMEM_FL_INHERITED (FS_NODUMP_FL | FS_NOATIME_FL)
e408e695 37
1da177e4
LT
38struct shmem_sb_info {
39 unsigned long max_blocks; /* How many blocks are allowed */
7e496299 40 struct percpu_counter used_blocks; /* How many are allocated */
1da177e4
LT
41 unsigned long max_inodes; /* How many inodes are allowed */
42 unsigned long free_inodes; /* How many are left for allocation */
bf11b9a8 43 raw_spinlock_t stat_lock; /* Serialize shmem_sb_info changes */
5a6e75f8
KS
44 umode_t mode; /* Mount mode for root directory */
45 unsigned char huge; /* Whether to try for hugepages */
8751e039
EB
46 kuid_t uid; /* Mount uid for root directory */
47 kgid_t gid; /* Mount gid for root directory */
ea3271f7 48 bool full_inums; /* If i_ino should be uint or ino_t */
2c6efe9c 49 bool noswap; /* ignores VM reclaim / swap requests */
e809d5f0
CD
50 ino_t next_ino; /* The next per-sb inode number to use */
51 ino_t __percpu *ino_batch; /* The next per-cpu inode number to use */
71fe804b 52 struct mempolicy *mpol; /* default memory policy for mappings */
779750d2
KS
53 spinlock_t shrinklist_lock; /* Protects shrinklist */
54 struct list_head shrinklist; /* List of shinkable inodes */
55 unsigned long shrinklist_len; /* Length of shrinklist */
1da177e4
LT
56};
57
58static inline struct shmem_inode_info *SHMEM_I(struct inode *inode)
59{
60 return container_of(inode, struct shmem_inode_info, vfs_inode);
61}
62
072441e2
HD
63/*
64 * Functions in mm/shmem.c called directly from elsewhere:
65 */
d7167b14 66extern const struct fs_parameter_spec shmem_fs_parameters[];
9096bbe9 67extern void shmem_init(void);
f3235626 68extern int shmem_init_fs_context(struct fs_context *fc);
072441e2
HD
69extern struct file *shmem_file_setup(const char *name,
70 loff_t size, unsigned long flags);
c7277090
EP
71extern struct file *shmem_kernel_file_setup(const char *name, loff_t size,
72 unsigned long flags);
703321b6
MA
73extern struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt,
74 const char *name, loff_t size, unsigned long flags);
072441e2 75extern int shmem_zero_setup(struct vm_area_struct *);
c01d5b30
HD
76extern unsigned long shmem_get_unmapped_area(struct file *, unsigned long addr,
77 unsigned long len, unsigned long pgoff, unsigned long flags);
d7c9e99a 78extern int shmem_lock(struct file *file, int lock, struct ucounts *ucounts);
3a4f8a0b 79#ifdef CONFIG_SHMEM
30e6a51d
HS
80extern const struct address_space_operations shmem_aops;
81static inline bool shmem_mapping(struct address_space *mapping)
82{
83 return mapping->a_ops == &shmem_aops;
84}
3a4f8a0b
HD
85#else
86static inline bool shmem_mapping(struct address_space *mapping)
87{
88 return false;
89}
90#endif /* CONFIG_SHMEM */
24513264 91extern void shmem_unlock_mapping(struct address_space *mapping);
d9d90e5e
HD
92extern struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
93 pgoff_t index, gfp_t gfp_mask);
94c1e62d 94extern void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end);
10a9c496 95int shmem_unuse(unsigned int type);
2b2af54a 96
2cf13384
DS
97extern bool shmem_is_huge(struct inode *inode, pgoff_t index, bool shmem_huge_force,
98 struct mm_struct *mm, unsigned long vm_flags);
b4aca547 99#ifdef CONFIG_SHMEM
6a15a370 100extern unsigned long shmem_swap_usage(struct vm_area_struct *vma);
b4aca547
SP
101#else
102static inline unsigned long shmem_swap_usage(struct vm_area_struct *vma)
103{
104 return 0;
105}
106#endif
48131e03
VB
107extern unsigned long shmem_partial_swap_usage(struct address_space *mapping,
108 pgoff_t start, pgoff_t end);
6a15a370 109
923e2f0e 110/* Flag allocation requirements to shmem_get_folio */
f3f0e1d2
KS
111enum sgp_type {
112 SGP_READ, /* don't exceed i_size, don't allocate page */
acdd9f8e 113 SGP_NOALLOC, /* similar, but fail on hole or use fallocated page */
f3f0e1d2 114 SGP_CACHE, /* don't exceed i_size, may allocate page */
f3f0e1d2
KS
115 SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
116 SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
117};
118
4e1fc793
MWO
119int shmem_get_folio(struct inode *inode, pgoff_t index, struct folio **foliop,
120 enum sgp_type sgp);
f01b2b3e
MWO
121struct folio *shmem_read_folio_gfp(struct address_space *mapping,
122 pgoff_t index, gfp_t gfp);
123
124static inline struct folio *shmem_read_folio(struct address_space *mapping,
125 pgoff_t index)
126{
127 return shmem_read_folio_gfp(mapping, index, mapping_gfp_mask(mapping));
128}
f3f0e1d2 129
d9d90e5e
HD
130static inline struct page *shmem_read_mapping_page(
131 struct address_space *mapping, pgoff_t index)
132{
133 return shmem_read_mapping_page_gfp(mapping, index,
134 mapping_gfp_mask(mapping));
135}
136
f3f0e1d2
KS
137static inline bool shmem_file(struct file *file)
138{
139 if (!IS_ENABLED(CONFIG_SHMEM))
140 return false;
141 if (!file || !file->f_mapping)
142 return false;
143 return shmem_mapping(file->f_mapping);
144}
145
d144bf62
HD
146/*
147 * If fallocate(FALLOC_FL_KEEP_SIZE) has been used, there may be pages
148 * beyond i_size's notion of EOF, which fallocate has committed to reserving:
149 * which split_huge_page() must therefore not delete. This use of a single
150 * "fallocend" per inode errs on the side of not deleting a reservation when
151 * in doubt: there are plenty of cases when it preserves unreserved pages.
152 */
153static inline pgoff_t shmem_fallocend(struct inode *inode, pgoff_t eof)
154{
155 return max(eof, SHMEM_I(inode)->fallocend);
156}
157
800d8c63
KS
158extern bool shmem_charge(struct inode *inode, long pages);
159extern void shmem_uncharge(struct inode *inode, long pages);
160
3460f6e5 161#ifdef CONFIG_USERFAULTFD
4c27fe4c 162#ifdef CONFIG_SHMEM
61c50040 163extern int shmem_mfill_atomic_pte(pmd_t *dst_pmd,
4c27fe4c
MR
164 struct vm_area_struct *dst_vma,
165 unsigned long dst_addr,
166 unsigned long src_addr,
d9712937 167 uffd_flags_t flags,
d7be6d7e 168 struct folio **foliop);
3460f6e5 169#else /* !CONFIG_SHMEM */
61c50040 170#define shmem_mfill_atomic_pte(dst_pmd, dst_vma, dst_addr, \
d7be6d7e 171 src_addr, flags, foliop) ({ BUG(); 0; })
3460f6e5
AR
172#endif /* CONFIG_SHMEM */
173#endif /* CONFIG_USERFAULTFD */
4c27fe4c 174
1da177e4 175#endif