y.
aboutsummaryrefslogblamecommitdiffstats
path: root/mm/shmem.c
blob: f65f84062db554ed2b3e9452cc787f8ab4f1b0d3 (plain) (tree)
1
2
3
4
5
6
7
8
9
10







                                                 

                                                        





                                                                      


                                                          


                                       



                        
                          







                                





                                                                     
                        
                           
                            
                              
                       



                              
                            




                            
                        
                          
                          
                           
                        
 



                        











                                                                              
                                                                
                                                                                    
 

                                                                                            
 



                                                                                      











                                                                            

                                                                       

                                                                      
                                                                    


                                                                  
                   








                                                                         
      
 


                                                                            
                                                          




                                                                     
          
                                                                    
           
                                                                       
                                                             



















































                                                                        

                                                                  



                                                                      
                                    










                                                                         

                                                                             



                                                                       
                                 


                                                                   
                                               
                                                        
                                                          


                                                                    
                                                      
 
                                                                        
                                                  
                                                                              



                                               
                                          



                                                              
                                 






                                                         
























                                                        
   
                                                        






















                                                                             
   
                                                                        






























































































                                                                                                           



                                                                         

 
   
                                                                     


                                                          

                                            



















                                                                                                          
                                         










                                                                
                                                                           























                                                                                 
   
                                                         


                                                                      
   

                                                                       
 
                                        




                                            






                                                          




                                                  

                                          


                     

                                                                      








                                                         

                                                                    






























                                                            
                                                                               
















                                                       
                       

                               
                                  

                                                       
                                                                




                                      

                                         
                                              
                                       
                                  
                               
                







                                                                               
                                         


                               
                                  
                                                              










                                                       
                                                                               
         




                                                                    
                                                

                           












                                                                              
                                       


















                                                                           
                                                                         






                                                                
























                                                                          

                                                         
                                                   






                                                                


                                                                       




                                                    
                                        
                                     











                                                                               



                                                              
                                                                        


                                                            
                                                   
                                                       

                                                                      
                                                         

                                                                      
                 
                           






                                                                            




                                                                               




                                                                               
                   
                                                                         


                                                                        





















                                                                         
                                       

                  

                                                                      


                                              






                                                                     
                                                            
                                                           
                                                                    
                                                                       

                                                          







                                                                             
                                      





                                                                               






                                                                 



                                             
                                             

                                                   
                                                 
      




                                                   

                                                       
                                                     
                                                          

                                                              
                                                           
                                                   
                                                          
                                                       
                                                            

                 
                                
                                      
























                                                                                                 
                  



                               



                                               




                                                      
                        
                           








                                                                                  







                                                                     













                                                                          
                                                     




                                                                      
                                             



                                                     








                                 


                                        








                                                                              




                                            
          


                                                                             
           
                                                                       

                         
                                               



                                                     
                  


                                               
                                           
                                                                        
                                                                 



                                                                  

                                                                             
                          










                                                                               

                                             
                                            


                                                                       
         

                                     
                                 
                                 




                                                     










                                                                 
                                          

                                                                        
                                                             
                               

                                 
         
                                            







                                                                      













                                                                            





                                    
                              

                             




                                                                              


                                                                               





                                       
                               




                                                        








                                                                    
 
                                                                         
                                             

                                                     



                                                
                                         
                                       
                                          
                                                                   
                                          






                                                                         





                                 



                                                                       
                                   

                             



                                                                             


                  
                   
                                                                         
 
                        
 
                                                
                                                  
 
                                                     

                                            
 











                                                                               

                         

                                                                         
 
                                     
                                   
                          
 


                                                                               
                                                               
                          
                            
                           
                              
                                                      


                    

                                                                         

                                   
 

                                                               
                            

                                                                       




                                                                 
 

                        
                                                                             



                         

                                                                         
 
                                                     

 

                                                                         
 
                               
 
                        
 






                                                                              
















                                                                          
                  



                                   

                 
                          
 



                                                                 
                                                                      

                                                                     






                                                        
                                        









                                                                            















                                                   
                                                        
                                                                
                                                             
                                                        
                         
                                                 
                                                                      




















                                                                          
                                              


































                                                          

                                                                                


                                                      
                                                         

                                                 
                                                 



                                                 
                                               





                                                                         
                                                                            
                                                                     


                                                                     
                                                    
                                 
                         

                                                     





                                                       
                                                                           









                                                      
                                         

















                                                                

                                
                                                 
                                                                    





                                                                    
                                                    
 

                                                                                
                                                    







                                                                    







                                                                










                                                                              













                                                                    
                                         

                                            

                                                 

         
                          









                                             
                                                                        
 
                                                                   
                  
                
 

                                                                           
 
                                                                              

                                                                             
 
                                     

 
                  
                                                                              
 
                                                               


                                                                     

                                                                     
 
                                                               








                                                                     
                                                           







                                                        
                                                         



                                                         

                                                                

                   
 




                                 
                                                                    


                                    
                                          


                 

                                                                                     




                                                    

                                    


                              
                                                   
                                    

                                                                                
                                                    


                                                              
                                                   
                                                
                                    


                                        
                                                                      


                                                             
                                                              

                                                              

                                                                           

                              
                                         









                                                                               
                                                                     

                              

                                     



                     

                                                                     

  
                                                                         

                                                                               
   







                                                                              
          


                                                                   
 












                                                                   


                                                  
                             
                          

                                 
                      

 

                                                                                                            
                                                           

                                                         








                                                                              

















                                                       
                                                                            




                                                   

                                          


                                                                      
                                                                         


























                                                                                
                        
                                            

                                             


























                                                                                































                                                                             

 
                                                                   
 
                                                              



                                       

                                      

                                                                   

                                 

                                                   

                                                          
                                        








                                                                          
                            

                            
                                                                         
                    






                                                                            
                 

                                                     


                                     
                 

                          
      



                                                                        









                                                                          
                       














                                                                                          
                





                                                                      


                                               


                                                                    
                         


                                                                                

                   





                                                                 

                                                          


                                                                    
                          








                                                                                   

                                    



















                                                                                                                           
                                            
                                   
                                    
                                   






















                                                                                       
                                                                                    


                               









                                                                    











                                                                        
                                                      




                                                              
                                  

                                         






                                                   
                                                                                  

                                                          
                    

 
                                                                           



                                                                           

                                  
                    

 
                                                                                     

                                       
                                           


                                         


         
                                                                        

                                                   

  
                                                                       


                                            

  
                             
  





                                                                       
                                                                          
                                                                          
                                                                           
 
                                                                            

 

                                                                            


                                  
                                                                      

 

                                                                             


                                  

                                                                       

 
                                                                  





                                            
                                                             

                                     




                                      












                                                                

                                                                
 
                            





                                          
 

                                                                 
                    
                                             


                            
                      































                                                                                
                                                          
                                           
                                          
                                             

  

                                                                           


                                       
















                                                                       





















                                                                      

                                                                    
                                                            
                                                                    


                                                            
                                                                    


                                                       
                                    
                                         
                                                                               


                                                      
                                    
                                         
                                                                      


                                                      
                                    
                                         
                                                                      

                                             
                                                       
                                                                    
                                             

















                                                                           
                                              



                             
                                                     
                             
 


                                                          
                                       
                         
                                       






                                                                        
                                                     
                         
                                                     


                         



                                                         


                                                                           


                                        
 















                                                                         
                                           


                         






                                                   
                                                                    


                            
                                     


                                                                
                                                               



                                                             
                                           

                                      
                               
 
                   





                                                                     





                                                                
         
                                            



                                 
                                           

                                                 
 




                                                
                            



                                           
 
                                                                                   

                            

                                   












                                   
                                             



                                                              
                                                                                        













                                                                          
                                


                                                                     
                                       





                                                                   
                                                          




                                    
                                               

 
                                                           
                                          
                                                        
                   
                                         

                                            
      
                                       
                                                       

  
                                                             


                                              
                                       
                                        
                                              
                                                 
                                     

                                                    


      
                                                               
                                              
                                               




                                              
                                            

      

  
                                                                   









                                        
      





                                              
                                            


      
                                                                       





                                              
                                            
      

  
                                                  




                                              
                                             





                                               
                                                         
                                      






                                           

                                                                          
 
                                                                         







                                                
 
                           


                  



                                                  








                                                              
 
                                                           












                                                                

                                             


                                 
 






































                                                                                


















                                                                   
                           













                                                         




                                                                     
























                                                                                

                                                                     
                                                                                           

                                                               
                                                                



                         
 
   
                                                          

                                                         
                                                                           
   
                                                                                 



                            

                            















                                               

                                           
                                
                                   
 
                        
                                                                                 
                   
                                
 
                                          

                                                    


                                                            
                                
      

                        
                                                          



                                          

                    
           
                        



                                       
                                    
 
   
                                                      
















                                                                 
/*
 * Resizable virtual memory filesystem for Linux.
 *
 * Copyright (C) 2000 Linus Torvalds.
 *		 2000 Transmeta Corp.
 *		 2000-2001 Christoph Rohland
 *		 2000-2001 SAP AG
 *		 2002 Red Hat Inc.
 * Copyright (C) 2002-2005 Hugh Dickins.
 * Copyright (C) 2002-2005 VERITAS Software Corporation.
 * Copyright (C) 2004 Andi Kleen, SuSE Labs
 *
 * Extended attribute support for tmpfs:
 * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
 * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
 *
 * tiny-shmem:
 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
 *
 * This file is released under the GPL.
 */

#include <linux/fs.h>
#include <linux/init.h>
#include <linux/vfs.h>
#include <linux/mount.h>
#include <linux/pagemap.h>
#include <linux/file.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/swap.h>

static struct vfsmount *shm_mnt;

#ifdef CONFIG_SHMEM
/*
 * This virtual memory filesystem is heavily based on the ramfs. It
 * extends ramfs by the ability to use swap and honor resource limits
 * which makes it a completely usable filesystem.
 */

#include <linux/xattr.h>
#include <linux/exportfs.h>
#include <linux/posix_acl.h>
#include <linux/generic_acl.h>
#include <linux/mman.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <linux/backing-dev.h>
#include <linux/shmem_fs.h>
#include <linux/writeback.h>
#include <linux/blkdev.h>
#include <linux/security.h>
#include <linux/swapops.h>
#include <linux/mempolicy.h>
#include <linux/namei.h>
#include <linux/ctype.h>
#include <linux/migrate.h>
#include <linux/highmem.h>
#include <linux/seq_file.h>
#include <linux/magic.h>

#include <asm/uaccess.h>
#include <asm/div64.h>
#include <asm/pgtable.h>

/*
 * The maximum size of a shmem/tmpfs file is limited by the maximum size of
 * its triple-indirect swap vector - see illustration at shmem_swp_entry().
 *
 * With 4kB page size, maximum file size is just over 2TB on a 32-bit kernel,
 * but one eighth of that on a 64-bit kernel.  With 8kB page size, maximum
 * file size is just over 4TB on a 64-bit kernel, but 16TB on a 32-bit kernel,
 * MAX_LFS_FILESIZE being then more restrictive than swap vector layout.
 *
 * We use / and * instead of shifts in the definitions below, so that the swap
 * vector can be tested with small even values (e.g. 20) for ENTRIES_PER_PAGE.
 */
#define ENTRIES_PER_PAGE (PAGE_CACHE_SIZE/sizeof(unsigned long))
#define ENTRIES_PER_PAGEPAGE ((unsigned long long)ENTRIES_PER_PAGE*ENTRIES_PER_PAGE)

#define SHMSWP_MAX_INDEX (SHMEM_NR_DIRECT + (ENTRIES_PER_PAGEPAGE/2) * (ENTRIES_PER_PAGE+1))
#define SHMSWP_MAX_BYTES (SHMSWP_MAX_INDEX << PAGE_CACHE_SHIFT)

#define SHMEM_MAX_BYTES  min_t(unsigned long long, SHMSWP_MAX_BYTES, MAX_LFS_FILESIZE)
#define SHMEM_MAX_INDEX  ((unsigned long)((SHMEM_MAX_BYTES+1) >> PAGE_CACHE_SHIFT))

#define BLOCKS_PER_PAGE  (PAGE_CACHE_SIZE/512)
#define VM_ACCT(size)    (PAGE_CACHE_ALIGN(size) >> PAGE_SHIFT)

/* info->flags needs VM_flags to handle pagein/truncate races efficiently */
#define SHMEM_PAGEIN	 VM_READ
#define SHMEM_TRUNCATE	 VM_WRITE

/* Definition to limit shmem_truncate's steps between cond_rescheds */
#define LATENCY_LIMIT	 64

/* Pretend that each entry is of this size in directory's i_size */
#define BOGO_DIRENT_SIZE 20

/* Flag allocation requirements to shmem_getpage and shmem_swp_alloc */
enum sgp_type {
	SGP_READ,	/* don't exceed i_size, don't allocate page */
	SGP_CACHE,	/* don't exceed i_size, may allocate page */
	SGP_DIRTY,	/* like SGP_CACHE, but set new page dirty */
	SGP_WRITE,	/* may exceed i_size, may allocate page */
};

#ifdef CONFIG_TMPFS