root/mm/filemap.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. multi_bmap
  2. filemap_nopage
  3. filemap_write_page
  4. filemap_swapout
  5. filemap_swapin
  6. filemap_sync_pte
  7. filemap_sync_pte_range
  8. filemap_sync_pmd_range
  9. filemap_sync
  10. filemap_unmap
  11. filemap_close
  12. generic_mmap
  13. msync_interval
  14. sys_msync

   1 /*
   2  *      linux/mm/filemap.c
   3  *
   4  * Copyright (C) 1994, 1995  Linus Torvalds
   5  */
   6 
   7 /*
   8  * This file handles the generic file mmap semantics used by
   9  * most "normal" filesystems (but you don't /have/ to use this:
  10  * the NFS filesystem does this differently, for example)
  11  */
  12 #include <linux/stat.h>
  13 #include <linux/sched.h>
  14 #include <linux/kernel.h>
  15 #include <linux/mm.h>
  16 #include <linux/shm.h>
  17 #include <linux/errno.h>
  18 #include <linux/mman.h>
  19 #include <linux/string.h>
  20 #include <linux/malloc.h>
  21 #include <linux/fs.h>
  22 #include <linux/locks.h>
  23 
  24 #include <asm/segment.h>
  25 #include <asm/system.h>
  26 #include <asm/pgtable.h>
  27 
  28 /*
  29  * Shared mappings implemented 30.11.1994. It's not fully working yet,
  30  * though.
  31  *
  32  * Shared mappings now work. 15.8.1995  Bruno.
  33  */
  34 
  35 /*
  36  * Simple routines for both non-shared and shared mappings.
  37  */
  38 
  39 static inline void multi_bmap(struct inode * inode, unsigned long block, unsigned int * nr, int shift)
     /* [previous][next][first][last][top][bottom][index][help] */
  40 {
  41         int i = PAGE_SIZE >> shift;
  42         block >>= shift;
  43         do {
  44                 *nr = bmap(inode, block);
  45                 i--;
  46                 block++;
  47                 nr++;
  48         } while (i > 0);
  49 }
  50 
  51 static unsigned long filemap_nopage(struct vm_area_struct * area, unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
  52         unsigned long page, int no_share)
  53 {
  54         struct inode * inode = area->vm_inode;
  55         int nr[PAGE_SIZE/512];
  56 
  57         multi_bmap(inode, (address & PAGE_MASK) - area->vm_start + area->vm_offset, nr,
  58                 inode->i_sb->s_blocksize_bits);
  59         return bread_page(page, inode->i_dev, nr, inode->i_sb->s_blocksize, no_share);
  60 }
  61 
  62 
  63 /*
  64  * Tries to write a shared mapped page to its backing store. May return -EIO
  65  * if the disk is full.
  66  */
  67 static int filemap_write_page(struct vm_area_struct * vma,
     /* [previous][next][first][last][top][bottom][index][help] */
  68         unsigned long offset,
  69         unsigned long page)
  70 {
  71         int old_fs;
  72         unsigned long size, result;
  73         struct file file;
  74         struct inode * inode;
  75         struct buffer_head * bh;
  76 
  77         bh = buffer_pages[MAP_NR(page)];
  78         if (bh) {
  79                 /* whee.. just mark the buffer heads dirty */
  80                 struct buffer_head * tmp = bh;
  81                 do {
  82                         mark_buffer_dirty(tmp, 0);
  83                         tmp = tmp->b_this_page;
  84                 } while (tmp != bh);
  85                 return 0;
  86         }
  87 
  88         inode = vma->vm_inode;
  89         file.f_op = inode->i_op->default_file_ops;
  90         if (!file.f_op->write)
  91                 return -EIO;
  92         size = offset + PAGE_SIZE;
  93         /* refuse to extend file size.. */
  94         if (S_ISREG(inode->i_mode)) {
  95                 if (size > inode->i_size)
  96                         size = inode->i_size;
  97                 /* Ho humm.. We should have tested for this earlier */
  98                 if (size < offset)
  99                         return -EIO;
 100         }
 101         size -= offset;
 102         file.f_mode = 3;
 103         file.f_flags = 0;
 104         file.f_count = 1;
 105         file.f_inode = inode;
 106         file.f_pos = offset;
 107         file.f_reada = 0;
 108         old_fs = get_fs();
 109         set_fs(KERNEL_DS);
 110         result = file.f_op->write(inode, &file, (const char *) page, size);
 111         set_fs(old_fs);
 112         if (result != size)
 113                 return -EIO;
 114         return 0;
 115 }
 116 
 117 
 118 /*
 119  * Swapping to a shared file: while we're busy writing out the page
 120  * (and the page still exists in memory), we save the page information
 121  * in the page table, so that "filemap_swapin()" can re-use the page
 122  * immediately if it is called while we're busy swapping it out..
 123  *
 124  * Once we've written it all out, we mark the page entry "empty", which
 125  * will result in a normal page-in (instead of a swap-in) from the now
 126  * up-to-date disk file.
 127  */
 128 int filemap_swapout(struct vm_area_struct * vma,
     /* [previous][next][first][last][top][bottom][index][help] */
 129         unsigned long offset,
 130         pte_t *page_table)
 131 {
 132         int error;
 133         unsigned long page = pte_page(*page_table);
 134         unsigned long entry = SWP_ENTRY(SHM_SWP_TYPE, MAP_NR(page));
 135 
 136         set_pte(page_table, __pte(entry));
 137         invalidate();
 138         error = filemap_write_page(vma, offset, page);
 139         if (pte_val(*page_table) == entry)
 140                 pte_clear(page_table);
 141         return error;
 142 }
 143 
 144 /*
 145  * filemap_swapin() is called only if we have something in the page
 146  * tables that is non-zero (but not present), which we know to be the
 147  * page index of a page that is busy being swapped out (see above).
 148  * So we just use it directly..
 149  */
 150 static pte_t filemap_swapin(struct vm_area_struct * vma,
     /* [previous][next][first][last][top][bottom][index][help] */
 151         unsigned long offset,
 152         unsigned long entry)
 153 {
 154         unsigned long page = SWP_OFFSET(entry);
 155 
 156         mem_map[page]++;
 157         page = (page << PAGE_SHIFT) + PAGE_OFFSET;
 158         return mk_pte(page,vma->vm_page_prot);
 159 }
 160 
 161 
 162 static inline int filemap_sync_pte(pte_t * ptep, struct vm_area_struct *vma,
     /* [previous][next][first][last][top][bottom][index][help] */
 163         unsigned long address, unsigned int flags)
 164 {
 165         pte_t pte = *ptep;
 166         unsigned long page;
 167         int error;
 168 
 169         if (!(flags & MS_INVALIDATE)) {
 170                 if (!pte_present(pte))
 171                         return 0;
 172                 if (!pte_dirty(pte))
 173                         return 0;
 174                 set_pte(ptep, pte_mkclean(pte));
 175                 page = pte_page(pte);
 176                 mem_map[MAP_NR(page)]++;
 177         } else {
 178                 if (pte_none(pte))
 179                         return 0;
 180                 pte_clear(ptep);
 181                 if (!pte_present(pte)) {
 182                         swap_free(pte_val(pte));
 183                         return 0;
 184                 }
 185                 page = pte_page(pte);
 186                 if (!pte_dirty(pte) || flags == MS_INVALIDATE) {
 187                         free_page(page);
 188                         return 0;
 189                 }
 190         }
 191         error = filemap_write_page(vma, address - vma->vm_start + vma->vm_offset, page);
 192         free_page(page);
 193         return error;
 194 }
 195 
 196 static inline int filemap_sync_pte_range(pmd_t * pmd,
     /* [previous][next][first][last][top][bottom][index][help] */
 197         unsigned long address, unsigned long size, 
 198         struct vm_area_struct *vma, unsigned long offset, unsigned int flags)
 199 {
 200         pte_t * pte;
 201         unsigned long end;
 202         int error;
 203 
 204         if (pmd_none(*pmd))
 205                 return 0;
 206         if (pmd_bad(*pmd)) {
 207                 printk("filemap_sync_pte_range: bad pmd (%08lx)\n", pmd_val(*pmd));
 208                 pmd_clear(pmd);
 209                 return 0;
 210         }
 211         pte = pte_offset(pmd, address);
 212         offset += address & PMD_MASK;
 213         address &= ~PMD_MASK;
 214         end = address + size;
 215         if (end > PMD_SIZE)
 216                 end = PMD_SIZE;
 217         error = 0;
 218         do {
 219                 error |= filemap_sync_pte(pte, vma, address + offset, flags);
 220                 address += PAGE_SIZE;
 221                 pte++;
 222         } while (address < end);
 223         return error;
 224 }
 225 
 226 static inline int filemap_sync_pmd_range(pgd_t * pgd,
     /* [previous][next][first][last][top][bottom][index][help] */
 227         unsigned long address, unsigned long size, 
 228         struct vm_area_struct *vma, unsigned int flags)
 229 {
 230         pmd_t * pmd;
 231         unsigned long offset, end;
 232         int error;
 233 
 234         if (pgd_none(*pgd))
 235                 return 0;
 236         if (pgd_bad(*pgd)) {
 237                 printk("filemap_sync_pmd_range: bad pgd (%08lx)\n", pgd_val(*pgd));
 238                 pgd_clear(pgd);
 239                 return 0;
 240         }
 241         pmd = pmd_offset(pgd, address);
 242         offset = address & PMD_MASK;
 243         address &= ~PMD_MASK;
 244         end = address + size;
 245         if (end > PGDIR_SIZE)
 246                 end = PGDIR_SIZE;
 247         error = 0;
 248         do {
 249                 error |= filemap_sync_pte_range(pmd, address, end - address, vma, offset, flags);
 250                 address = (address + PMD_SIZE) & PMD_MASK;
 251                 pmd++;
 252         } while (address < end);
 253         return error;
 254 }
 255 
 256 static int filemap_sync(struct vm_area_struct * vma, unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
 257         size_t size, unsigned int flags)
 258 {
 259         pgd_t * dir;
 260         unsigned long end = address + size;
 261         int error = 0;
 262 
 263         dir = pgd_offset(current, address);
 264         while (address < end) {
 265                 error |= filemap_sync_pmd_range(dir, address, end - address, vma, flags);
 266                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
 267                 dir++;
 268         }
 269         invalidate();
 270         return error;
 271 }
 272 
 273 /*
 274  * This handles partial area unmaps..
 275  */
 276 static void filemap_unmap(struct vm_area_struct *vma, unsigned long start, size_t len)
     /* [previous][next][first][last][top][bottom][index][help] */
 277 {
 278         filemap_sync(vma, start, len, MS_ASYNC);
 279 }
 280 
 281 /*
 282  * This handles complete area closes..
 283  */
 284 static void filemap_close(struct vm_area_struct * vma)
     /* [previous][next][first][last][top][bottom][index][help] */
 285 {
 286         filemap_sync(vma, vma->vm_start, vma->vm_end - vma->vm_start, MS_ASYNC);
 287 }
 288 
 289 /*
 290  * Shared mappings need to be able to do the right thing at
 291  * close/unmap/sync. They will also use the private file as
 292  * backing-store for swapping..
 293  */
 294 static struct vm_operations_struct file_shared_mmap = {
 295         NULL,                   /* open */
 296         filemap_close,          /* close */
 297         filemap_unmap,          /* unmap */
 298         NULL,                   /* protect */
 299         filemap_sync,           /* sync */
 300         NULL,                   /* advise */
 301         filemap_nopage,         /* nopage */
 302         NULL,                   /* wppage */
 303         filemap_swapout,        /* swapout */
 304         filemap_swapin,         /* swapin */
 305 };
 306 
 307 /*
 308  * Private mappings just need to be able to load in the map.
 309  *
 310  * (This is actually used for shared mappings as well, if we
 311  * know they can't ever get write permissions..)
 312  */
 313 static struct vm_operations_struct file_private_mmap = {
 314         NULL,                   /* open */
 315         NULL,                   /* close */
 316         NULL,                   /* unmap */
 317         NULL,                   /* protect */
 318         NULL,                   /* sync */
 319         NULL,                   /* advise */
 320         filemap_nopage,         /* nopage */
 321         NULL,                   /* wppage */
 322         NULL,                   /* swapout */
 323         NULL,                   /* swapin */
 324 };
 325 
 326 /* This is used for a general mmap of a disk file */
 327 int generic_mmap(struct inode * inode, struct file * file, struct vm_area_struct * vma)
     /* [previous][next][first][last][top][bottom][index][help] */
 328 {
 329         struct vm_operations_struct * ops;
 330 
 331         if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
 332                 ops = &file_shared_mmap;
 333                 /* share_page() can only guarantee proper page sharing if
 334                  * the offsets are all page aligned. */
 335                 if (vma->vm_offset & (PAGE_SIZE - 1))
 336                         return -EINVAL;
 337         } else {
 338                 ops = &file_private_mmap;
 339                 if (vma->vm_offset & (inode->i_sb->s_blocksize - 1))
 340                         return -EINVAL;
 341         }
 342         if (!inode->i_sb || !S_ISREG(inode->i_mode))
 343                 return -EACCES;
 344         if (!inode->i_op || !inode->i_op->bmap)
 345                 return -ENOEXEC;
 346         if (!IS_RDONLY(inode)) {
 347                 inode->i_atime = CURRENT_TIME;
 348                 inode->i_dirt = 1;
 349         }
 350         vma->vm_inode = inode;
 351         inode->i_count++;
 352         vma->vm_ops = ops;
 353         return 0;
 354 }
 355 
 356 
 357 /*
 358  * The msync() system call.
 359  */
 360 
 361 static int msync_interval(struct vm_area_struct * vma,
     /* [previous][next][first][last][top][bottom][index][help] */
 362         unsigned long start, unsigned long end, int flags)
 363 {
 364         if (!vma->vm_inode)
 365                 return 0;
 366         if (vma->vm_ops->sync) {
 367                 int error;
 368                 error = vma->vm_ops->sync(vma, start, end-start, flags);
 369                 if (error)
 370                         return error;
 371                 if (flags & MS_SYNC)
 372                         return file_fsync(vma->vm_inode, NULL);
 373                 return 0;
 374         }
 375         return 0;
 376 }
 377 
 378 asmlinkage int sys_msync(unsigned long start, size_t len, int flags)
     /* [previous][next][first][last][top][bottom][index][help] */
 379 {
 380         unsigned long end;
 381         struct vm_area_struct * vma;
 382         int unmapped_error, error;
 383 
 384         if (start & ~PAGE_MASK)
 385                 return -EINVAL;
 386         len = (len + ~PAGE_MASK) & PAGE_MASK;
 387         end = start + len;
 388         if (end < start)
 389                 return -EINVAL;
 390         if (flags & ~(MS_ASYNC | MS_INVALIDATE | MS_SYNC))
 391                 return -EINVAL;
 392         if (end == start)
 393                 return 0;
 394         /*
 395          * If the interval [start,end) covers some unmapped address ranges,
 396          * just ignore them, but return -EFAULT at the end.
 397          */
 398         vma = find_vma(current, start);
 399         unmapped_error = 0;
 400         for (;;) {
 401                 /* Still start < end. */
 402                 if (!vma)
 403                         return -EFAULT;
 404                 /* Here start < vma->vm_end. */
 405                 if (start < vma->vm_start) {
 406                         unmapped_error = -EFAULT;
 407                         start = vma->vm_start;
 408                 }
 409                 /* Here vma->vm_start <= start < vma->vm_end. */
 410                 if (end <= vma->vm_end) {
 411                         if (start < end) {
 412                                 error = msync_interval(vma, start, end, flags);
 413                                 if (error)
 414                                         return error;
 415                         }
 416                         return unmapped_error;
 417                 }
 418                 /* Here vma->vm_start <= start < vma->vm_end < end. */
 419                 error = msync_interval(vma, start, vma->vm_end, flags);
 420                 if (error)
 421                         return error;
 422                 start = vma->vm_end;
 423                 vma = vma->vm_next;
 424         }
 425 }

/* [previous][next][first][last][top][bottom][index][help] */