xref: /linux/include/linux/shmem_fs.h (revision 743e7d025d2f6f6c801e8bdea714eae64bf8fbb2)
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 #include <linux/bits.h>
14 #include <linux/list_lru.h>
15 
16 /* inode in-kernel data */
17 
18 #ifdef CONFIG_TMPFS_QUOTA
19 #define SHMEM_MAXQUOTAS 2
20 #endif
21 
22 /* Suppress pre-accounting of the entire object size. */
23 #define SHMEM_F_NORESERVE	BIT(0)
24 /* Disallow swapping. */
25 #define SHMEM_F_LOCKED		BIT(1)
26 /*
27  * Disallow growing, shrinking, or hole punching in the inode. Combined with
28  * folio pinning, makes sure the inode's mapping stays fixed.
29  *
30  * In some ways similar to F_SEAL_GROW | F_SEAL_SHRINK, but can be removed and
31  * isn't directly visible to userspace.
32  */
33 #define SHMEM_F_MAPPING_FROZEN	BIT(2)
34 
35 struct shmem_inode_info {
36 	spinlock_t		lock;
37 	unsigned int		seals;		/* shmem seals */
38 	unsigned long		flags;
39 	unsigned long		alloced;	/* data pages alloced to file */
40 	unsigned long		swapped;	/* subtotal assigned to swap */
41 	union {
42 	    struct offset_ctx	dir_offsets;	/* stable directory offsets */
43 	    struct {
44 		struct list_head shrinklist;	/* shrinkable hpage inodes */
45 		struct list_head swaplist;	/* chain of maybes on swap */
46 	    };
47 	};
48 	struct timespec64	i_crtime;	/* file creation time */
49 	struct shared_policy	policy;		/* NUMA memory alloc policy */
50 	struct list_head        xattrs;		/* list of xattrs */
51 	pgoff_t			fallocend;	/* highest fallocate endindex */
52 	unsigned int		fsflags;	/* for FS_IOC_[SG]ETFLAGS */
53 	atomic_t		stop_eviction;	/* hold when working on inode */
54 #ifdef CONFIG_TMPFS_QUOTA
55 	struct dquot __rcu	*i_dquot[MAXQUOTAS];
56 #endif
57 	struct inode		vfs_inode;
58 
59 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
60 	struct obj_cgroup	*shrinklist_objcg;
61 	int			shrinklist_nid;
62 #endif
63 };
64 
65 #define SHMEM_FL_USER_VISIBLE		(FS_FL_USER_VISIBLE | FS_CASEFOLD_FL)
66 #define SHMEM_FL_USER_MODIFIABLE \
67 	(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL | FS_NOATIME_FL | FS_CASEFOLD_FL)
68 #define SHMEM_FL_INHERITED		(FS_NODUMP_FL | FS_NOATIME_FL | FS_CASEFOLD_FL)
69 
70 struct shmem_quota_limits {
71 	qsize_t usrquota_bhardlimit; /* Default user quota block hard limit */
72 	qsize_t usrquota_ihardlimit; /* Default user quota inode hard limit */
73 	qsize_t grpquota_bhardlimit; /* Default group quota block hard limit */
74 	qsize_t grpquota_ihardlimit; /* Default group quota inode hard limit */
75 };
76 
77 struct shmem_sb_info {
78 	unsigned long max_blocks;   /* How many blocks are allowed */
79 	struct percpu_counter used_blocks;  /* How many are allocated */
80 	unsigned long max_inodes;   /* How many inodes are allowed */
81 	unsigned long free_ispace;  /* How much ispace left for allocation */
82 	raw_spinlock_t stat_lock;   /* Serialize shmem_sb_info changes */
83 	umode_t mode;		    /* Mount mode for root directory */
84 	unsigned char huge;	    /* Whether to try for hugepages */
85 	kuid_t uid;		    /* Mount uid for root directory */
86 	kgid_t gid;		    /* Mount gid for root directory */
87 	bool full_inums;	    /* If i_ino should be uint or ino_t */
88 	bool noswap;		    /* ignores VM reclaim / swap requests */
89 	ino_t next_ino;		    /* The next per-sb inode number to use */
90 	ino_t __percpu *ino_batch;  /* The next per-cpu inode number to use */
91 	struct mempolicy *mpol;     /* default memory policy for mappings */
92 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
93 	struct list_lru shrinklist; /* List of shrinkable inodes */
94 #endif
95 	struct shmem_quota_limits qlimits; /* Default quota limits */
96 	struct simple_xattr_cache xa_cache;
97 };
98 
99 static inline struct shmem_inode_info *SHMEM_I(struct inode *inode)
100 {
101 	return container_of(inode, struct shmem_inode_info, vfs_inode);
102 }
103 
104 /*
105  * Functions in mm/shmem.c called directly from elsewhere:
106  */
107 extern const struct fs_parameter_spec shmem_fs_parameters[];
108 extern void shmem_init(void);
109 extern int shmem_init_fs_context(struct fs_context *fc);
110 struct file *shmem_file_setup(const char *name, loff_t size, vma_flags_t flags);
111 struct file *shmem_kernel_file_setup(const char *name, loff_t size, vma_flags_t vma_flags);
112 extern struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt,
113 		const char *name, loff_t size, vma_flags_t flags);
114 int shmem_zero_setup(struct vm_area_struct *vma);
115 int shmem_zero_setup_desc(struct vm_area_desc *desc);
116 extern unsigned long shmem_get_unmapped_area(struct file *, unsigned long addr,
117 		unsigned long len, unsigned long pgoff, unsigned long flags);
118 extern int shmem_lock(struct file *file, int lock, struct ucounts *ucounts);
119 #ifdef CONFIG_SHMEM
120 bool shmem_mapping(const struct address_space *mapping);
121 #else
122 static inline bool shmem_mapping(const struct address_space *mapping)
123 {
124 	return false;
125 }
126 #endif /* CONFIG_SHMEM */
127 void shmem_unlock_mapping(struct address_space *mapping);
128 struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
129 					pgoff_t index, gfp_t gfp_mask);
130 int shmem_write_folio(struct folio *folio);
131 void shmem_truncate_range(struct inode *inode, loff_t start, uoff_t end);
132 int shmem_unuse(unsigned int type);
133 
134 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SHMEM)
135 unsigned long shmem_allowable_huge_orders(struct inode *inode,
136 				struct vm_area_struct *vma, pgoff_t index,
137 				loff_t write_end, bool shmem_huge_force);
138 bool shmem_hpage_pmd_enabled(void);
139 #else
140 static inline unsigned long shmem_allowable_huge_orders(struct inode *inode,
141 				struct vm_area_struct *vma, pgoff_t index,
142 				loff_t write_end, bool shmem_huge_force)
143 {
144 	return 0;
145 }
146 
147 static inline bool shmem_hpage_pmd_enabled(void)
148 {
149 	return false;
150 }
151 #endif
152 
153 #ifdef CONFIG_SHMEM
154 extern unsigned long shmem_swap_usage(struct vm_area_struct *vma);
155 extern void shmem_uncharge(struct inode *inode, long pages);
156 #else
157 static inline unsigned long shmem_swap_usage(struct vm_area_struct *vma)
158 {
159 	return 0;
160 }
161 
162 static inline void shmem_uncharge(struct inode *inode, long pages)
163 {
164 }
165 #endif
166 extern unsigned long shmem_partial_swap_usage(struct address_space *mapping,
167 						pgoff_t start, pgoff_t end);
168 
169 /* Flag allocation requirements to shmem_get_folio */
170 enum sgp_type {
171 	SGP_READ,	/* don't exceed i_size, don't allocate page */
172 	SGP_NOALLOC,	/* similar, but fail on hole or use fallocated page */
173 	SGP_CACHE,	/* don't exceed i_size, may allocate page */
174 	SGP_WRITE,	/* may exceed i_size, may allocate !Uptodate page */
175 	SGP_FALLOC,	/* like SGP_WRITE, but make existing page Uptodate */
176 };
177 
178 int shmem_get_folio(struct inode *inode, pgoff_t index, loff_t write_end,
179 		struct folio **foliop, enum sgp_type sgp);
180 struct folio *shmem_read_folio_gfp(struct address_space *mapping,
181 		pgoff_t index, gfp_t gfp);
182 
183 static inline struct folio *shmem_read_folio(struct address_space *mapping,
184 		pgoff_t index)
185 {
186 	return shmem_read_folio_gfp(mapping, index, mapping_gfp_mask(mapping));
187 }
188 
189 static inline struct page *shmem_read_mapping_page(
190 				struct address_space *mapping, pgoff_t index)
191 {
192 	return shmem_read_mapping_page_gfp(mapping, index,
193 					mapping_gfp_mask(mapping));
194 }
195 
196 static inline bool shmem_file(struct file *file)
197 {
198 	if (!IS_ENABLED(CONFIG_SHMEM))
199 		return false;
200 	if (!file || !file->f_mapping)
201 		return false;
202 	return shmem_mapping(file->f_mapping);
203 }
204 
205 /* Must be called with inode lock taken exclusive. */
206 static inline void shmem_freeze(struct inode *inode, bool freeze)
207 {
208 	if (freeze)
209 		SHMEM_I(inode)->flags |= SHMEM_F_MAPPING_FROZEN;
210 	else
211 		SHMEM_I(inode)->flags &= ~SHMEM_F_MAPPING_FROZEN;
212 }
213 
214 /*
215  * If fallocate(FALLOC_FL_KEEP_SIZE) has been used, there may be pages
216  * beyond i_size's notion of EOF, which fallocate has committed to reserving:
217  * which split_huge_page() must therefore not delete.  This use of a single
218  * "fallocend" per inode errs on the side of not deleting a reservation when
219  * in doubt: there are plenty of cases when it preserves unreserved pages.
220  */
221 static inline pgoff_t shmem_fallocend(struct inode *inode, pgoff_t eof)
222 {
223 	return max(eof, SHMEM_I(inode)->fallocend);
224 }
225 
226 extern bool shmem_charge(struct inode *inode, long pages);
227 
228 /*
229  * Used space is stored as unsigned 64-bit value in bytes but
230  * quota core supports only signed 64-bit values so use that
231  * as a limit
232  */
233 #define SHMEM_QUOTA_MAX_SPC_LIMIT 0x7fffffffffffffffLL /* 2^63-1 */
234 #define SHMEM_QUOTA_MAX_INO_LIMIT 0x7fffffffffffffffLL
235 
236 #ifdef CONFIG_TMPFS_QUOTA
237 extern const struct dquot_operations shmem_quota_operations;
238 extern struct quota_format_type shmem_quota_format;
239 #endif /* CONFIG_TMPFS_QUOTA */
240 
241 #endif
242