xref: /OK3568_Linux_fs/kernel/include/linux/shmem_fs.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef __SHMEM_FS_H
3*4882a593Smuzhiyun #define __SHMEM_FS_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/file.h>
6*4882a593Smuzhiyun #include <linux/swap.h>
7*4882a593Smuzhiyun #include <linux/mempolicy.h>
8*4882a593Smuzhiyun #include <linux/pagemap.h>
9*4882a593Smuzhiyun #include <linux/percpu_counter.h>
10*4882a593Smuzhiyun #include <linux/xattr.h>
11*4882a593Smuzhiyun #include <linux/fs_parser.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun /* inode in-kernel data */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun struct shmem_inode_info {
16*4882a593Smuzhiyun 	spinlock_t		lock;
17*4882a593Smuzhiyun 	unsigned int		seals;		/* shmem seals */
18*4882a593Smuzhiyun 	unsigned long		flags;
19*4882a593Smuzhiyun 	unsigned long		alloced;	/* data pages alloced to file */
20*4882a593Smuzhiyun 	unsigned long		swapped;	/* subtotal assigned to swap */
21*4882a593Smuzhiyun 	struct list_head        shrinklist;     /* shrinkable hpage inodes */
22*4882a593Smuzhiyun 	struct list_head	swaplist;	/* chain of maybes on swap */
23*4882a593Smuzhiyun 	struct shared_policy	policy;		/* NUMA memory alloc policy */
24*4882a593Smuzhiyun 	struct simple_xattrs	xattrs;		/* list of xattrs */
25*4882a593Smuzhiyun 	atomic_t		stop_eviction;	/* hold when working on inode */
26*4882a593Smuzhiyun 	struct inode		vfs_inode;
27*4882a593Smuzhiyun };
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun struct shmem_sb_info {
30*4882a593Smuzhiyun 	unsigned long max_blocks;   /* How many blocks are allowed */
31*4882a593Smuzhiyun 	struct percpu_counter used_blocks;  /* How many are allocated */
32*4882a593Smuzhiyun 	unsigned long max_inodes;   /* How many inodes are allowed */
33*4882a593Smuzhiyun 	unsigned long free_inodes;  /* How many are left for allocation */
34*4882a593Smuzhiyun 	spinlock_t stat_lock;	    /* Serialize shmem_sb_info changes */
35*4882a593Smuzhiyun 	umode_t mode;		    /* Mount mode for root directory */
36*4882a593Smuzhiyun 	unsigned char huge;	    /* Whether to try for hugepages */
37*4882a593Smuzhiyun 	kuid_t uid;		    /* Mount uid for root directory */
38*4882a593Smuzhiyun 	kgid_t gid;		    /* Mount gid for root directory */
39*4882a593Smuzhiyun 	bool full_inums;	    /* If i_ino should be uint or ino_t */
40*4882a593Smuzhiyun 	ino_t next_ino;		    /* The next per-sb inode number to use */
41*4882a593Smuzhiyun 	ino_t __percpu *ino_batch;  /* The next per-cpu inode number to use */
42*4882a593Smuzhiyun 	struct mempolicy *mpol;     /* default memory policy for mappings */
43*4882a593Smuzhiyun 	spinlock_t shrinklist_lock;   /* Protects shrinklist */
44*4882a593Smuzhiyun 	struct list_head shrinklist;  /* List of shinkable inodes */
45*4882a593Smuzhiyun 	unsigned long shrinklist_len; /* Length of shrinklist */
46*4882a593Smuzhiyun };
47*4882a593Smuzhiyun 
SHMEM_I(struct inode * inode)48*4882a593Smuzhiyun static inline struct shmem_inode_info *SHMEM_I(struct inode *inode)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun 	return container_of(inode, struct shmem_inode_info, vfs_inode);
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun  * Functions in mm/shmem.c called directly from elsewhere:
55*4882a593Smuzhiyun  */
56*4882a593Smuzhiyun extern const struct fs_parameter_spec shmem_fs_parameters[];
57*4882a593Smuzhiyun extern int shmem_init(void);
58*4882a593Smuzhiyun extern int shmem_init_fs_context(struct fs_context *fc);
59*4882a593Smuzhiyun extern struct file *shmem_file_setup(const char *name,
60*4882a593Smuzhiyun 					loff_t size, unsigned long flags);
61*4882a593Smuzhiyun extern struct file *shmem_kernel_file_setup(const char *name, loff_t size,
62*4882a593Smuzhiyun 					    unsigned long flags);
63*4882a593Smuzhiyun extern struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt,
64*4882a593Smuzhiyun 		const char *name, loff_t size, unsigned long flags);
65*4882a593Smuzhiyun extern int shmem_zero_setup(struct vm_area_struct *);
66*4882a593Smuzhiyun extern unsigned long shmem_get_unmapped_area(struct file *, unsigned long addr,
67*4882a593Smuzhiyun 		unsigned long len, unsigned long pgoff, unsigned long flags);
68*4882a593Smuzhiyun extern int shmem_lock(struct file *file, int lock, struct user_struct *user);
69*4882a593Smuzhiyun #ifdef CONFIG_SHMEM
70*4882a593Smuzhiyun extern bool shmem_mapping(struct address_space *mapping);
71*4882a593Smuzhiyun #else
shmem_mapping(struct address_space * mapping)72*4882a593Smuzhiyun static inline bool shmem_mapping(struct address_space *mapping)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	return false;
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun #endif /* CONFIG_SHMEM */
77*4882a593Smuzhiyun extern void shmem_unlock_mapping(struct address_space *mapping);
78*4882a593Smuzhiyun extern struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
79*4882a593Smuzhiyun 					pgoff_t index, gfp_t gfp_mask);
80*4882a593Smuzhiyun extern void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end);
81*4882a593Smuzhiyun extern int shmem_unuse(unsigned int type, bool frontswap,
82*4882a593Smuzhiyun 		       unsigned long *fs_pages_to_unuse);
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun extern bool shmem_huge_enabled(struct vm_area_struct *vma);
85*4882a593Smuzhiyun extern unsigned long shmem_swap_usage(struct vm_area_struct *vma);
86*4882a593Smuzhiyun extern unsigned long shmem_partial_swap_usage(struct address_space *mapping,
87*4882a593Smuzhiyun 						pgoff_t start, pgoff_t end);
88*4882a593Smuzhiyun extern void shmem_mark_page_lazyfree(struct page *page, bool tail);
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /* Flag allocation requirements to shmem_getpage */
91*4882a593Smuzhiyun enum sgp_type {
92*4882a593Smuzhiyun 	SGP_READ,	/* don't exceed i_size, don't allocate page */
93*4882a593Smuzhiyun 	SGP_CACHE,	/* don't exceed i_size, may allocate page */
94*4882a593Smuzhiyun 	SGP_NOHUGE,	/* like SGP_CACHE, but no huge pages */
95*4882a593Smuzhiyun 	SGP_HUGE,	/* like SGP_CACHE, huge pages preferred */
96*4882a593Smuzhiyun 	SGP_WRITE,	/* may exceed i_size, may allocate !Uptodate page */
97*4882a593Smuzhiyun 	SGP_FALLOC,	/* like SGP_WRITE, but make existing page Uptodate */
98*4882a593Smuzhiyun };
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun extern int shmem_getpage(struct inode *inode, pgoff_t index,
101*4882a593Smuzhiyun 		struct page **pagep, enum sgp_type sgp);
102*4882a593Smuzhiyun 
shmem_read_mapping_page(struct address_space * mapping,pgoff_t index)103*4882a593Smuzhiyun static inline struct page *shmem_read_mapping_page(
104*4882a593Smuzhiyun 				struct address_space *mapping, pgoff_t index)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	return shmem_read_mapping_page_gfp(mapping, index,
107*4882a593Smuzhiyun 					mapping_gfp_mask(mapping));
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun 
shmem_file(struct file * file)110*4882a593Smuzhiyun static inline bool shmem_file(struct file *file)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	if (!IS_ENABLED(CONFIG_SHMEM))
113*4882a593Smuzhiyun 		return false;
114*4882a593Smuzhiyun 	if (!file || !file->f_mapping)
115*4882a593Smuzhiyun 		return false;
116*4882a593Smuzhiyun 	return shmem_mapping(file->f_mapping);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun extern bool shmem_charge(struct inode *inode, long pages);
120*4882a593Smuzhiyun extern void shmem_uncharge(struct inode *inode, long pages);
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun #ifdef CONFIG_USERFAULTFD
123*4882a593Smuzhiyun #ifdef CONFIG_SHMEM
124*4882a593Smuzhiyun extern int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd,
125*4882a593Smuzhiyun 				  struct vm_area_struct *dst_vma,
126*4882a593Smuzhiyun 				  unsigned long dst_addr,
127*4882a593Smuzhiyun 				  unsigned long src_addr,
128*4882a593Smuzhiyun 				  bool zeropage,
129*4882a593Smuzhiyun 				  struct page **pagep);
130*4882a593Smuzhiyun #else /* !CONFIG_SHMEM */
131*4882a593Smuzhiyun #define shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, \
132*4882a593Smuzhiyun 			       src_addr, zeropage, pagep)       ({ BUG(); 0; })
133*4882a593Smuzhiyun #endif /* CONFIG_SHMEM */
134*4882a593Smuzhiyun #endif /* CONFIG_USERFAULTFD */
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun #endif
137