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