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