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_file(struct file *file); 121 bool shmem_mapping(const struct address_space *mapping); 122 #else 123 static inline bool shmem_file(struct file *file) 124 { 125 return false; 126 } 127 128 static inline bool shmem_mapping(const struct address_space *mapping) 129 { 130 return false; 131 } 132 #endif /* CONFIG_SHMEM */ 133 void shmem_unlock_mapping(struct address_space *mapping); 134 struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 135 pgoff_t index, gfp_t gfp_mask); 136 int shmem_write_folio(struct folio *folio); 137 void shmem_truncate_range(struct inode *inode, loff_t start, uoff_t end); 138 int shmem_unuse(unsigned int type); 139 140 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SHMEM) 141 unsigned long shmem_allowable_huge_orders(struct inode *inode, 142 struct vm_area_struct *vma, pgoff_t index, 143 loff_t write_end, bool shmem_huge_force); 144 bool shmem_hpage_pmd_enabled(void); 145 #else 146 static inline unsigned long shmem_allowable_huge_orders(struct inode *inode, 147 struct vm_area_struct *vma, pgoff_t index, 148 loff_t write_end, bool shmem_huge_force) 149 { 150 return 0; 151 } 152 153 static inline bool shmem_hpage_pmd_enabled(void) 154 { 155 return false; 156 } 157 #endif 158 159 #ifdef CONFIG_SHMEM 160 extern unsigned long shmem_swap_usage(struct vm_area_struct *vma); 161 extern void shmem_uncharge(struct inode *inode, long pages); 162 #else 163 static inline unsigned long shmem_swap_usage(struct vm_area_struct *vma) 164 { 165 return 0; 166 } 167 168 static inline void shmem_uncharge(struct inode *inode, long pages) 169 { 170 } 171 #endif 172 extern unsigned long shmem_partial_swap_usage(struct address_space *mapping, 173 pgoff_t start, pgoff_t end); 174 175 /* Flag allocation requirements to shmem_get_folio */ 176 enum sgp_type { 177 SGP_READ, /* don't exceed i_size, don't allocate page */ 178 SGP_NOALLOC, /* similar, but fail on hole or use fallocated page */ 179 SGP_CACHE, /* don't exceed i_size, may allocate page */ 180 SGP_WRITE, /* may exceed i_size, may allocate !Uptodate page */ 181 SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */ 182 }; 183 184 int shmem_get_folio(struct inode *inode, pgoff_t index, loff_t write_end, 185 struct folio **foliop, enum sgp_type sgp); 186 struct folio *shmem_read_folio_gfp(struct address_space *mapping, 187 pgoff_t index, gfp_t gfp); 188 189 static inline struct folio *shmem_read_folio(struct address_space *mapping, 190 pgoff_t index) 191 { 192 return shmem_read_folio_gfp(mapping, index, mapping_gfp_mask(mapping)); 193 } 194 195 static inline struct page *shmem_read_mapping_page( 196 struct address_space *mapping, pgoff_t index) 197 { 198 return shmem_read_mapping_page_gfp(mapping, index, 199 mapping_gfp_mask(mapping)); 200 } 201 202 /* Must be called with inode lock taken exclusive. */ 203 static inline void shmem_freeze(struct inode *inode, bool freeze) 204 { 205 if (freeze) 206 SHMEM_I(inode)->flags |= SHMEM_F_MAPPING_FROZEN; 207 else 208 SHMEM_I(inode)->flags &= ~SHMEM_F_MAPPING_FROZEN; 209 } 210 211 /* 212 * If fallocate(FALLOC_FL_KEEP_SIZE) has been used, there may be pages 213 * beyond i_size's notion of EOF, which fallocate has committed to reserving: 214 * which split_huge_page() must therefore not delete. This use of a single 215 * "fallocend" per inode errs on the side of not deleting a reservation when 216 * in doubt: there are plenty of cases when it preserves unreserved pages. 217 */ 218 static inline pgoff_t shmem_fallocend(struct inode *inode, pgoff_t eof) 219 { 220 return max(eof, SHMEM_I(inode)->fallocend); 221 } 222 223 extern bool shmem_charge(struct inode *inode, long pages); 224 225 /* 226 * Used space is stored as unsigned 64-bit value in bytes but 227 * quota core supports only signed 64-bit values so use that 228 * as a limit 229 */ 230 #define SHMEM_QUOTA_MAX_SPC_LIMIT 0x7fffffffffffffffLL /* 2^63-1 */ 231 #define SHMEM_QUOTA_MAX_INO_LIMIT 0x7fffffffffffffffLL 232 233 #ifdef CONFIG_TMPFS_QUOTA 234 extern const struct dquot_operations shmem_quota_operations; 235 extern struct quota_format_type shmem_quota_format; 236 #endif /* CONFIG_TMPFS_QUOTA */ 237 238 #endif 239