1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __SHMEM_FS_H
3 #define __SHMEM_FS_H
4
5 #include <linux/file.h>
6 #include <linux/swap.h>
7 #include <linux/mempolicy.h>
8 #include <linux/pagemap.h>
9 #include <linux/percpu_counter.h>
10 #include <linux/xattr.h>
11 #include <linux/fs_parser.h>
12 #include <linux/userfaultfd_k.h>
13
14 /* inode in-kernel data */
15
16 #ifdef CONFIG_TMPFS_QUOTA
17 #define SHMEM_MAXQUOTAS 2
18 #endif
19
20 struct shmem_inode_info {
21 spinlock_t lock;
22 unsigned int seals; /* shmem seals */
23 unsigned long flags;
24 unsigned long alloced; /* data pages alloced to file */
25 unsigned long swapped; /* subtotal assigned to swap */
26 pgoff_t fallocend; /* highest fallocate endindex */
27 struct list_head shrinklist; /* shrinkable hpage inodes */
28 struct list_head swaplist; /* chain of maybes on swap */
29 struct shared_policy policy; /* NUMA memory alloc policy */
30 struct simple_xattrs xattrs; /* list of xattrs */
31 atomic_t stop_eviction; /* hold when working on inode */
32 struct timespec64 i_crtime; /* file creation time */
33 unsigned int fsflags; /* flags for FS_IOC_[SG]ETFLAGS */
34 #ifdef CONFIG_TMPFS_QUOTA
35 struct dquot *i_dquot[MAXQUOTAS];
36 #endif
37 struct offset_ctx dir_offsets; /* stable entry offsets */
38 struct inode vfs_inode;
39 };
40
41 #define SHMEM_FL_USER_VISIBLE FS_FL_USER_VISIBLE
42 #define SHMEM_FL_USER_MODIFIABLE \
43 (FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL | FS_NOATIME_FL)
44 #define SHMEM_FL_INHERITED (FS_NODUMP_FL | FS_NOATIME_FL)
45
46 struct shmem_quota_limits {
47 qsize_t usrquota_bhardlimit; /* Default user quota block hard limit */
48 qsize_t usrquota_ihardlimit; /* Default user quota inode hard limit */
49 qsize_t grpquota_bhardlimit; /* Default group quota block hard limit */
50 qsize_t grpquota_ihardlimit; /* Default group quota inode hard limit */
51 };
52
53 struct shmem_sb_info {
54 unsigned long max_blocks; /* How many blocks are allowed */
55 struct percpu_counter used_blocks; /* How many are allocated */
56 unsigned long max_inodes; /* How many inodes are allowed */
57 unsigned long free_ispace; /* How much ispace left for allocation */
58 raw_spinlock_t stat_lock; /* Serialize shmem_sb_info changes */
59 umode_t mode; /* Mount mode for root directory */
60 unsigned char huge; /* Whether to try for hugepages */
61 kuid_t uid; /* Mount uid for root directory */
62 kgid_t gid; /* Mount gid for root directory */
63 bool full_inums; /* If i_ino should be uint or ino_t */
64 bool noswap; /* ignores VM reclaim / swap requests */
65 ino_t next_ino; /* The next per-sb inode number to use */
66 ino_t __percpu *ino_batch; /* The next per-cpu inode number to use */
67 struct mempolicy *mpol; /* default memory policy for mappings */
68 spinlock_t shrinklist_lock; /* Protects shrinklist */
69 struct list_head shrinklist; /* List of shinkable inodes */
70 unsigned long shrinklist_len; /* Length of shrinklist */
71 struct shmem_quota_limits qlimits; /* Default quota limits */
72 };
73
SHMEM_I(struct inode * inode)74 static inline struct shmem_inode_info *SHMEM_I(struct inode *inode)
75 {
76 return container_of(inode, struct shmem_inode_info, vfs_inode);
77 }
78
79 /*
80 * Functions in mm/shmem.c called directly from elsewhere:
81 */
82 extern const struct fs_parameter_spec shmem_fs_parameters[];
83 extern void shmem_init(void);
84 extern int shmem_init_fs_context(struct fs_context *fc);
85 extern struct file *shmem_file_setup(const char *name,
86 loff_t size, unsigned long flags);
87 extern struct file *shmem_kernel_file_setup(const char *name, loff_t size,
88 unsigned long flags);
89 extern struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt,
90 const char *name, loff_t size, unsigned long flags);
91 extern int shmem_zero_setup(struct vm_area_struct *);
92 extern unsigned long shmem_get_unmapped_area(struct file *, unsigned long addr,
93 unsigned long len, unsigned long pgoff, unsigned long flags);
94 extern int shmem_lock(struct file *file, int lock, struct ucounts *ucounts);
95 #ifdef CONFIG_SHMEM
96 extern const struct address_space_operations shmem_aops;
shmem_mapping(struct address_space * mapping)97 static inline bool shmem_mapping(struct address_space *mapping)
98 {
99 return mapping->a_ops == &shmem_aops;
100 }
101 #else
shmem_mapping(struct address_space * mapping)102 static inline bool shmem_mapping(struct address_space *mapping)
103 {
104 return false;
105 }
106 #endif /* CONFIG_SHMEM */
107 extern void shmem_unlock_mapping(struct address_space *mapping);
108 extern struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
109 pgoff_t index, gfp_t gfp_mask);
110 extern void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end);
111 int shmem_unuse(unsigned int type);
112
113 extern bool shmem_is_huge(struct inode *inode, pgoff_t index, bool shmem_huge_force,
114 struct mm_struct *mm, unsigned long vm_flags);
115 #ifdef CONFIG_SHMEM
116 extern unsigned long shmem_swap_usage(struct vm_area_struct *vma);
117 #else
shmem_swap_usage(struct vm_area_struct * vma)118 static inline unsigned long shmem_swap_usage(struct vm_area_struct *vma)
119 {
120 return 0;
121 }
122 #endif
123 extern unsigned long shmem_partial_swap_usage(struct address_space *mapping,
124 pgoff_t start, pgoff_t end);
125
126 /* Flag allocation requirements to shmem_get_folio */
127 enum sgp_type {
128 SGP_READ, /* don't exceed i_size, don't allocate page */
129 SGP_NOALLOC, /* similar, but fail on hole or use fallocated page */
130 SGP_CACHE, /* don't exceed i_size, may allocate page */
131 SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */
132 SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
133 };
134
135 int shmem_get_folio(struct inode *inode, pgoff_t index, struct folio **foliop,
136 enum sgp_type sgp);
137 struct folio *shmem_read_folio_gfp(struct address_space *mapping,
138 pgoff_t index, gfp_t gfp);
139
shmem_read_folio(struct address_space * mapping,pgoff_t index)140 static inline struct folio *shmem_read_folio(struct address_space *mapping,
141 pgoff_t index)
142 {
143 return shmem_read_folio_gfp(mapping, index, mapping_gfp_mask(mapping));
144 }
145
shmem_read_mapping_page(struct address_space * mapping,pgoff_t index)146 static inline struct page *shmem_read_mapping_page(
147 struct address_space *mapping, pgoff_t index)
148 {
149 return shmem_read_mapping_page_gfp(mapping, index,
150 mapping_gfp_mask(mapping));
151 }
152
shmem_file(struct file * file)153 static inline bool shmem_file(struct file *file)
154 {
155 if (!IS_ENABLED(CONFIG_SHMEM))
156 return false;
157 if (!file || !file->f_mapping)
158 return false;
159 return shmem_mapping(file->f_mapping);
160 }
161
162 /*
163 * If fallocate(FALLOC_FL_KEEP_SIZE) has been used, there may be pages
164 * beyond i_size's notion of EOF, which fallocate has committed to reserving:
165 * which split_huge_page() must therefore not delete. This use of a single
166 * "fallocend" per inode errs on the side of not deleting a reservation when
167 * in doubt: there are plenty of cases when it preserves unreserved pages.
168 */
shmem_fallocend(struct inode * inode,pgoff_t eof)169 static inline pgoff_t shmem_fallocend(struct inode *inode, pgoff_t eof)
170 {
171 return max(eof, SHMEM_I(inode)->fallocend);
172 }
173
174 extern bool shmem_charge(struct inode *inode, long pages);
175 extern void shmem_uncharge(struct inode *inode, long pages);
176
177 #ifdef CONFIG_USERFAULTFD
178 #ifdef CONFIG_SHMEM
179 extern int shmem_mfill_atomic_pte(pmd_t *dst_pmd,
180 struct vm_area_struct *dst_vma,
181 unsigned long dst_addr,
182 unsigned long src_addr,
183 uffd_flags_t flags,
184 struct folio **foliop);
185 #else /* !CONFIG_SHMEM */
186 #define shmem_mfill_atomic_pte(dst_pmd, dst_vma, dst_addr, \
187 src_addr, flags, foliop) ({ BUG(); 0; })
188 #endif /* CONFIG_SHMEM */
189 #endif /* CONFIG_USERFAULTFD */
190
191 /*
192 * Used space is stored as unsigned 64-bit value in bytes but
193 * quota core supports only signed 64-bit values so use that
194 * as a limit
195 */
196 #define SHMEM_QUOTA_MAX_SPC_LIMIT 0x7fffffffffffffffLL /* 2^63-1 */
197 #define SHMEM_QUOTA_MAX_INO_LIMIT 0x7fffffffffffffffLL
198
199 #ifdef CONFIG_TMPFS_QUOTA
200 extern const struct dquot_operations shmem_quota_operations;
201 extern struct quota_format_type shmem_quota_format;
202 #endif /* CONFIG_TMPFS_QUOTA */
203
204 #endif
205