Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __SHMEM_FS_H
0003 #define __SHMEM_FS_H
0004 
0005 #include <linux/file.h>
0006 #include <linux/swap.h>
0007 #include <linux/mempolicy.h>
0008 #include <linux/pagemap.h>
0009 #include <linux/percpu_counter.h>
0010 #include <linux/xattr.h>
0011 #include <linux/fs_parser.h>
0012 
0013 /* inode in-kernel data */
0014 
0015 struct shmem_inode_info {
0016     spinlock_t      lock;
0017     unsigned int        seals;      /* shmem seals */
0018     unsigned long       flags;
0019     unsigned long       alloced;    /* data pages alloced to file */
0020     unsigned long       swapped;    /* subtotal assigned to swap */
0021     pgoff_t         fallocend;  /* highest fallocate endindex */
0022     struct list_head        shrinklist;     /* shrinkable hpage inodes */
0023     struct list_head    swaplist;   /* chain of maybes on swap */
0024     struct shared_policy    policy;     /* NUMA memory alloc policy */
0025     struct simple_xattrs    xattrs;     /* list of xattrs */
0026     atomic_t        stop_eviction;  /* hold when working on inode */
0027     struct timespec64   i_crtime;   /* file creation time */
0028     unsigned int        fsflags;    /* flags for FS_IOC_[SG]ETFLAGS */
0029     struct inode        vfs_inode;
0030 };
0031 
0032 #define SHMEM_FL_USER_VISIBLE       FS_FL_USER_VISIBLE
0033 #define SHMEM_FL_USER_MODIFIABLE \
0034     (FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL | FS_NOATIME_FL)
0035 #define SHMEM_FL_INHERITED      (FS_NODUMP_FL | FS_NOATIME_FL)
0036 
0037 struct shmem_sb_info {
0038     unsigned long max_blocks;   /* How many blocks are allowed */
0039     struct percpu_counter used_blocks;  /* How many are allocated */
0040     unsigned long max_inodes;   /* How many inodes are allowed */
0041     unsigned long free_inodes;  /* How many are left for allocation */
0042     raw_spinlock_t stat_lock;   /* Serialize shmem_sb_info changes */
0043     umode_t mode;           /* Mount mode for root directory */
0044     unsigned char huge;     /* Whether to try for hugepages */
0045     kuid_t uid;         /* Mount uid for root directory */
0046     kgid_t gid;         /* Mount gid for root directory */
0047     bool full_inums;        /* If i_ino should be uint or ino_t */
0048     ino_t next_ino;         /* The next per-sb inode number to use */
0049     ino_t __percpu *ino_batch;  /* The next per-cpu inode number to use */
0050     struct mempolicy *mpol;     /* default memory policy for mappings */
0051     spinlock_t shrinklist_lock;   /* Protects shrinklist */
0052     struct list_head shrinklist;  /* List of shinkable inodes */
0053     unsigned long shrinklist_len; /* Length of shrinklist */
0054 };
0055 
0056 static inline struct shmem_inode_info *SHMEM_I(struct inode *inode)
0057 {
0058     return container_of(inode, struct shmem_inode_info, vfs_inode);
0059 }
0060 
0061 /*
0062  * Functions in mm/shmem.c called directly from elsewhere:
0063  */
0064 extern const struct fs_parameter_spec shmem_fs_parameters[];
0065 extern void shmem_init(void);
0066 extern int shmem_init_fs_context(struct fs_context *fc);
0067 extern struct file *shmem_file_setup(const char *name,
0068                     loff_t size, unsigned long flags);
0069 extern struct file *shmem_kernel_file_setup(const char *name, loff_t size,
0070                         unsigned long flags);
0071 extern struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt,
0072         const char *name, loff_t size, unsigned long flags);
0073 extern int shmem_zero_setup(struct vm_area_struct *);
0074 extern unsigned long shmem_get_unmapped_area(struct file *, unsigned long addr,
0075         unsigned long len, unsigned long pgoff, unsigned long flags);
0076 extern int shmem_lock(struct file *file, int lock, struct ucounts *ucounts);
0077 #ifdef CONFIG_SHMEM
0078 extern const struct address_space_operations shmem_aops;
0079 static inline bool shmem_mapping(struct address_space *mapping)
0080 {
0081     return mapping->a_ops == &shmem_aops;
0082 }
0083 #else
0084 static inline bool shmem_mapping(struct address_space *mapping)
0085 {
0086     return false;
0087 }
0088 #endif /* CONFIG_SHMEM */
0089 extern void shmem_unlock_mapping(struct address_space *mapping);
0090 extern struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
0091                     pgoff_t index, gfp_t gfp_mask);
0092 extern void shmem_truncate_range(struct inode *inode, loff_t start, loff_t end);
0093 int shmem_unuse(unsigned int type);
0094 
0095 extern bool shmem_is_huge(struct vm_area_struct *vma,
0096               struct inode *inode, pgoff_t index);
0097 static inline bool shmem_huge_enabled(struct vm_area_struct *vma)
0098 {
0099     return shmem_is_huge(vma, file_inode(vma->vm_file), vma->vm_pgoff);
0100 }
0101 extern unsigned long shmem_swap_usage(struct vm_area_struct *vma);
0102 extern unsigned long shmem_partial_swap_usage(struct address_space *mapping,
0103                         pgoff_t start, pgoff_t end);
0104 
0105 /* Flag allocation requirements to shmem_getpage */
0106 enum sgp_type {
0107     SGP_READ,   /* don't exceed i_size, don't allocate page */
0108     SGP_NOALLOC,    /* similar, but fail on hole or use fallocated page */
0109     SGP_CACHE,  /* don't exceed i_size, may allocate page */
0110     SGP_WRITE,  /* may exceed i_size, may allocate !Uptodate page */
0111     SGP_FALLOC, /* like SGP_WRITE, but make existing page Uptodate */
0112 };
0113 
0114 extern int shmem_getpage(struct inode *inode, pgoff_t index,
0115         struct page **pagep, enum sgp_type sgp);
0116 
0117 static inline struct page *shmem_read_mapping_page(
0118                 struct address_space *mapping, pgoff_t index)
0119 {
0120     return shmem_read_mapping_page_gfp(mapping, index,
0121                     mapping_gfp_mask(mapping));
0122 }
0123 
0124 static inline bool shmem_file(struct file *file)
0125 {
0126     if (!IS_ENABLED(CONFIG_SHMEM))
0127         return false;
0128     if (!file || !file->f_mapping)
0129         return false;
0130     return shmem_mapping(file->f_mapping);
0131 }
0132 
0133 /*
0134  * If fallocate(FALLOC_FL_KEEP_SIZE) has been used, there may be pages
0135  * beyond i_size's notion of EOF, which fallocate has committed to reserving:
0136  * which split_huge_page() must therefore not delete.  This use of a single
0137  * "fallocend" per inode errs on the side of not deleting a reservation when
0138  * in doubt: there are plenty of cases when it preserves unreserved pages.
0139  */
0140 static inline pgoff_t shmem_fallocend(struct inode *inode, pgoff_t eof)
0141 {
0142     return max(eof, SHMEM_I(inode)->fallocend);
0143 }
0144 
0145 extern bool shmem_charge(struct inode *inode, long pages);
0146 extern void shmem_uncharge(struct inode *inode, long pages);
0147 
0148 #ifdef CONFIG_USERFAULTFD
0149 #ifdef CONFIG_SHMEM
0150 extern int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, pmd_t *dst_pmd,
0151                   struct vm_area_struct *dst_vma,
0152                   unsigned long dst_addr,
0153                   unsigned long src_addr,
0154                   bool zeropage, bool wp_copy,
0155                   struct page **pagep);
0156 #else /* !CONFIG_SHMEM */
0157 #define shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, dst_addr, \
0158                    src_addr, zeropage, wp_copy, pagep) ({ BUG(); 0; })
0159 #endif /* CONFIG_SHMEM */
0160 #endif /* CONFIG_USERFAULTFD */
0161 
0162 #endif