2 * hugetlbpage-backed filesystem. Based on ramfs.
6 * Copyright (C) 2002 Linus Torvalds.
9 #include <linux/module.h>
10 #include <linux/thread_info.h>
11 #include <asm/current.h>
12 #include <linux/sched.h> /* remove ASAP */
14 #include <linux/mount.h>
15 #include <linux/file.h>
16 #include <linux/writeback.h>
17 #include <linux/pagemap.h>
18 #include <linux/highmem.h>
19 #include <linux/init.h>
20 #include <linux/string.h>
21 #include <linux/capability.h>
22 #include <linux/backing-dev.h>
23 #include <linux/hugetlb.h>
24 #include <linux/pagevec.h>
25 #include <linux/quotaops.h>
26 #include <linux/slab.h>
27 #include <linux/dnotify.h>
28 #include <linux/statfs.h>
29 #include <linux/security.h>
31 #include <asm/uaccess.h>
33 /* some random number */
34 #define HUGETLBFS_MAGIC 0x958458f6
36 static struct super_operations hugetlbfs_ops;
37 static struct address_space_operations hugetlbfs_aops;
38 struct file_operations hugetlbfs_file_operations;
39 static struct inode_operations hugetlbfs_dir_inode_operations;
40 static struct inode_operations hugetlbfs_inode_operations;
42 static struct backing_dev_info hugetlbfs_backing_dev_info = {
43 .ra_pages = 0, /* No readahead */
44 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
47 int sysctl_hugetlb_shm_group;
49 static void huge_pagevec_release(struct pagevec *pvec)
53 for (i = 0; i < pagevec_count(pvec); ++i)
54 put_page(pvec->pages[i]);
60 * huge_pages_needed tries to determine the number of new huge pages that
61 * will be required to fully populate this VMA. This will be equal to
62 * the size of the VMA in huge pages minus the number of huge pages
63 * (covered by this VMA) that are found in the page cache.
65 * Result is in bytes to be compatible with is_hugepage_mem_enough()
68 huge_pages_needed(struct address_space *mapping, struct vm_area_struct *vma)
72 unsigned long start = vma->vm_start;
73 unsigned long end = vma->vm_end;
74 unsigned long hugepages = (end - start) >> HPAGE_SHIFT;
75 pgoff_t next = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
76 pgoff_t endpg = next + hugepages;
78 pagevec_init(&pvec, 0);
79 while (next < endpg) {
80 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
82 for (i = 0; i < pagevec_count(&pvec); i++) {
83 struct page *page = pvec.pages[i];
84 if (page->index > next)
86 if (page->index >= endpg)
91 huge_pagevec_release(&pvec);
93 return hugepages << HPAGE_SHIFT;
96 static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
98 struct inode *inode = file->f_dentry->d_inode;
99 struct address_space *mapping = inode->i_mapping;
104 if (vma->vm_pgoff & (HPAGE_SIZE / PAGE_SIZE - 1))
107 if (vma->vm_start & ~HPAGE_MASK)
110 if (vma->vm_end & ~HPAGE_MASK)
113 if (vma->vm_end - vma->vm_start < HPAGE_SIZE)
116 bytes = huge_pages_needed(mapping, vma);
117 if (!is_hugepage_mem_enough(bytes))
120 vma_len = (loff_t)(vma->vm_end - vma->vm_start);
122 mutex_lock(&inode->i_mutex);
124 vma->vm_flags |= VM_HUGETLB | VM_RESERVED;
125 vma->vm_ops = &hugetlb_vm_ops;
128 len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
129 if (!(vma->vm_flags & VM_WRITE) && len > inode->i_size)
133 hugetlb_prefault_arch_hook(vma->vm_mm);
134 if (inode->i_size < len)
137 mutex_unlock(&inode->i_mutex);
143 * Called under down_write(mmap_sem).
146 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
147 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
148 unsigned long len, unsigned long pgoff, unsigned long flags);
151 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
152 unsigned long len, unsigned long pgoff, unsigned long flags)
154 struct mm_struct *mm = current->mm;
155 struct vm_area_struct *vma;
156 unsigned long start_addr;
158 if (len & ~HPAGE_MASK)
164 addr = ALIGN(addr, HPAGE_SIZE);
165 vma = find_vma(mm, addr);
166 if (TASK_SIZE - len >= addr &&
167 (!vma || addr + len <= vma->vm_start))
171 start_addr = mm->free_area_cache;
173 if (len <= mm->cached_hole_size)
174 start_addr = TASK_UNMAPPED_BASE;
177 addr = ALIGN(start_addr, HPAGE_SIZE);
179 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
180 /* At this point: (!vma || addr < vma->vm_end). */
181 if (TASK_SIZE - len < addr) {
183 * Start a new search - just in case we missed
186 if (start_addr != TASK_UNMAPPED_BASE) {
187 start_addr = TASK_UNMAPPED_BASE;
193 if (!vma || addr + len <= vma->vm_start)
195 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
201 * Read a page. Again trivial. If it didn't already exist
202 * in the page cache, it is zero-filled.
204 static int hugetlbfs_readpage(struct file *file, struct page * page)
210 static int hugetlbfs_prepare_write(struct file *file,
211 struct page *page, unsigned offset, unsigned to)
216 static int hugetlbfs_commit_write(struct file *file,
217 struct page *page, unsigned offset, unsigned to)
222 static void truncate_huge_page(struct page *page)
224 clear_page_dirty(page);
225 ClearPageUptodate(page);
226 remove_from_page_cache(page);
230 static void truncate_hugepages(struct address_space *mapping, loff_t lstart)
232 const pgoff_t start = lstart >> HPAGE_SHIFT;
237 pagevec_init(&pvec, 0);
240 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
247 for (i = 0; i < pagevec_count(&pvec); ++i) {
248 struct page *page = pvec.pages[i];
251 if (page->index > next)
254 truncate_huge_page(page);
256 hugetlb_put_quota(mapping);
258 huge_pagevec_release(&pvec);
260 BUG_ON(!lstart && mapping->nrpages);
263 static void hugetlbfs_delete_inode(struct inode *inode)
265 if (inode->i_data.nrpages)
266 truncate_hugepages(&inode->i_data, 0);
270 static void hugetlbfs_forget_inode(struct inode *inode)
272 struct super_block *sb = inode->i_sb;
274 if (!hlist_unhashed(&inode->i_hash)) {
275 if (!(inode->i_state & (I_DIRTY|I_LOCK)))
276 list_move(&inode->i_list, &inode_unused);
277 inodes_stat.nr_unused++;
278 if (!sb || (sb->s_flags & MS_ACTIVE)) {
279 spin_unlock(&inode_lock);
282 inode->i_state |= I_WILL_FREE;
283 spin_unlock(&inode_lock);
285 * write_inode_now is a noop as we set BDI_CAP_NO_WRITEBACK
286 * in our backing_dev_info.
288 write_inode_now(inode, 1);
289 spin_lock(&inode_lock);
290 inode->i_state &= ~I_WILL_FREE;
291 inodes_stat.nr_unused--;
292 hlist_del_init(&inode->i_hash);
294 list_del_init(&inode->i_list);
295 list_del_init(&inode->i_sb_list);
296 inode->i_state |= I_FREEING;
297 inodes_stat.nr_inodes--;
298 spin_unlock(&inode_lock);
299 if (inode->i_data.nrpages)
300 truncate_hugepages(&inode->i_data, 0);
302 destroy_inode(inode);
305 static void hugetlbfs_drop_inode(struct inode *inode)
308 generic_delete_inode(inode);
310 hugetlbfs_forget_inode(inode);
314 * h_pgoff is in HPAGE_SIZE units.
315 * vma->vm_pgoff is in PAGE_SIZE units.
318 hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
320 struct vm_area_struct *vma;
321 struct prio_tree_iter iter;
323 vma_prio_tree_foreach(vma, &iter, root, h_pgoff, ULONG_MAX) {
324 unsigned long h_vm_pgoff;
325 unsigned long v_offset;
327 h_vm_pgoff = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
328 v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT;
330 * Is this VMA fully outside the truncation point?
332 if (h_vm_pgoff >= h_pgoff)
335 unmap_hugepage_range(vma,
336 vma->vm_start + v_offset, vma->vm_end);
341 * Expanding truncates are not allowed.
343 static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
346 struct address_space *mapping = inode->i_mapping;
348 if (offset > inode->i_size)
351 BUG_ON(offset & ~HPAGE_MASK);
352 pgoff = offset >> HPAGE_SHIFT;
354 inode->i_size = offset;
355 spin_lock(&mapping->i_mmap_lock);
356 if (!prio_tree_empty(&mapping->i_mmap))
357 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
358 spin_unlock(&mapping->i_mmap_lock);
359 truncate_hugepages(mapping, offset);
363 static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
365 struct inode *inode = dentry->d_inode;
367 unsigned int ia_valid = attr->ia_valid;
371 error = inode_change_ok(inode, attr);
375 if (ia_valid & ATTR_SIZE) {
377 if (!(attr->ia_size & ~HPAGE_MASK))
378 error = hugetlb_vmtruncate(inode, attr->ia_size);
381 attr->ia_valid &= ~ATTR_SIZE;
383 error = inode_setattr(inode, attr);
388 static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
389 gid_t gid, int mode, dev_t dev)
393 inode = new_inode(sb);
395 struct hugetlbfs_inode_info *info;
396 inode->i_mode = mode;
399 inode->i_blksize = HPAGE_SIZE;
401 inode->i_mapping->a_ops = &hugetlbfs_aops;
402 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
403 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
404 info = HUGETLBFS_I(inode);
405 mpol_shared_policy_init(&info->policy, MPOL_DEFAULT, NULL);
406 switch (mode & S_IFMT) {
408 init_special_inode(inode, mode, dev);
411 inode->i_op = &hugetlbfs_inode_operations;
412 inode->i_fop = &hugetlbfs_file_operations;
415 inode->i_op = &hugetlbfs_dir_inode_operations;
416 inode->i_fop = &simple_dir_operations;
418 /* directory inodes start off with i_nlink == 2 (for "." entry) */
422 inode->i_op = &page_symlink_inode_operations;
430 * File creation. Allocate an inode, and we're done..
432 static int hugetlbfs_mknod(struct inode *dir,
433 struct dentry *dentry, int mode, dev_t dev)
439 if (dir->i_mode & S_ISGID) {
444 gid = current->fsgid;
446 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
448 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
449 d_instantiate(dentry, inode);
450 dget(dentry); /* Extra count - pin the dentry in core */
456 static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
458 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
464 static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
466 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
469 static int hugetlbfs_symlink(struct inode *dir,
470 struct dentry *dentry, const char *symname)
476 if (dir->i_mode & S_ISGID)
479 gid = current->fsgid;
481 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
482 gid, S_IFLNK|S_IRWXUGO, 0);
484 int l = strlen(symname)+1;
485 error = page_symlink(inode, symname, l);
487 d_instantiate(dentry, inode);
492 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
498 * For direct-IO reads into hugetlb pages
500 static int hugetlbfs_set_page_dirty(struct page *page)
505 static int hugetlbfs_statfs(struct super_block *sb, struct kstatfs *buf)
507 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
509 buf->f_type = HUGETLBFS_MAGIC;
510 buf->f_bsize = HPAGE_SIZE;
512 spin_lock(&sbinfo->stat_lock);
513 /* If no limits set, just report 0 for max/free/used
514 * blocks, like simple_statfs() */
515 if (sbinfo->max_blocks >= 0) {
516 buf->f_blocks = sbinfo->max_blocks;
517 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
518 buf->f_files = sbinfo->max_inodes;
519 buf->f_ffree = sbinfo->free_inodes;
521 spin_unlock(&sbinfo->stat_lock);
523 buf->f_namelen = NAME_MAX;
527 static void hugetlbfs_put_super(struct super_block *sb)
529 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
532 sb->s_fs_info = NULL;
537 static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
539 if (sbinfo->free_inodes >= 0) {
540 spin_lock(&sbinfo->stat_lock);
541 if (unlikely(!sbinfo->free_inodes)) {
542 spin_unlock(&sbinfo->stat_lock);
545 sbinfo->free_inodes--;
546 spin_unlock(&sbinfo->stat_lock);
552 static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
554 if (sbinfo->free_inodes >= 0) {
555 spin_lock(&sbinfo->stat_lock);
556 sbinfo->free_inodes++;
557 spin_unlock(&sbinfo->stat_lock);
562 static kmem_cache_t *hugetlbfs_inode_cachep;
564 static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
566 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
567 struct hugetlbfs_inode_info *p;
569 if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
571 p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
573 hugetlbfs_inc_free_inodes(sbinfo);
576 return &p->vfs_inode;
579 static void hugetlbfs_destroy_inode(struct inode *inode)
581 hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
582 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
583 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
586 static struct address_space_operations hugetlbfs_aops = {
587 .readpage = hugetlbfs_readpage,
588 .prepare_write = hugetlbfs_prepare_write,
589 .commit_write = hugetlbfs_commit_write,
590 .set_page_dirty = hugetlbfs_set_page_dirty,
594 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
596 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
598 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
599 SLAB_CTOR_CONSTRUCTOR)
600 inode_init_once(&ei->vfs_inode);
603 struct file_operations hugetlbfs_file_operations = {
604 .mmap = hugetlbfs_file_mmap,
605 .fsync = simple_sync_file,
606 .get_unmapped_area = hugetlb_get_unmapped_area,
609 static struct inode_operations hugetlbfs_dir_inode_operations = {
610 .create = hugetlbfs_create,
611 .lookup = simple_lookup,
613 .unlink = simple_unlink,
614 .symlink = hugetlbfs_symlink,
615 .mkdir = hugetlbfs_mkdir,
616 .rmdir = simple_rmdir,
617 .mknod = hugetlbfs_mknod,
618 .rename = simple_rename,
619 .setattr = hugetlbfs_setattr,
622 static struct inode_operations hugetlbfs_inode_operations = {
623 .setattr = hugetlbfs_setattr,
626 static struct super_operations hugetlbfs_ops = {
627 .alloc_inode = hugetlbfs_alloc_inode,
628 .destroy_inode = hugetlbfs_destroy_inode,
629 .statfs = hugetlbfs_statfs,
630 .delete_inode = hugetlbfs_delete_inode,
631 .drop_inode = hugetlbfs_drop_inode,
632 .put_super = hugetlbfs_put_super,
636 hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
638 char *opt, *value, *rest;
642 while ((opt = strsep(&options, ",")) != NULL) {
646 value = strchr(opt, '=');
647 if (!value || !*value)
652 if (!strcmp(opt, "uid"))
653 pconfig->uid = simple_strtoul(value, &value, 0);
654 else if (!strcmp(opt, "gid"))
655 pconfig->gid = simple_strtoul(value, &value, 0);
656 else if (!strcmp(opt, "mode"))
657 pconfig->mode = simple_strtoul(value,&value,0) & 0777U;
658 else if (!strcmp(opt, "size")) {
659 unsigned long long size = memparse(value, &rest);
661 size <<= HPAGE_SHIFT;
662 size *= max_huge_pages;
667 pconfig->nr_blocks = (size >> HPAGE_SHIFT);
669 } else if (!strcmp(opt,"nr_inodes")) {
670 pconfig->nr_inodes = memparse(value, &rest);
682 hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
684 struct inode * inode;
685 struct dentry * root;
687 struct hugetlbfs_config config;
688 struct hugetlbfs_sb_info *sbinfo;
690 config.nr_blocks = -1; /* No limit on size by default */
691 config.nr_inodes = -1; /* No limit on number of inodes by default */
692 config.uid = current->fsuid;
693 config.gid = current->fsgid;
695 ret = hugetlbfs_parse_options(data, &config);
700 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
703 sb->s_fs_info = sbinfo;
704 spin_lock_init(&sbinfo->stat_lock);
705 sbinfo->max_blocks = config.nr_blocks;
706 sbinfo->free_blocks = config.nr_blocks;
707 sbinfo->max_inodes = config.nr_inodes;
708 sbinfo->free_inodes = config.nr_inodes;
709 sb->s_maxbytes = MAX_LFS_FILESIZE;
710 sb->s_blocksize = HPAGE_SIZE;
711 sb->s_blocksize_bits = HPAGE_SHIFT;
712 sb->s_magic = HUGETLBFS_MAGIC;
713 sb->s_op = &hugetlbfs_ops;
715 inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
716 S_IFDIR | config.mode, 0);
720 root = d_alloc_root(inode);
732 int hugetlb_get_quota(struct address_space *mapping)
735 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
737 if (sbinfo->free_blocks > -1) {
738 spin_lock(&sbinfo->stat_lock);
739 if (sbinfo->free_blocks > 0)
740 sbinfo->free_blocks--;
743 spin_unlock(&sbinfo->stat_lock);
749 void hugetlb_put_quota(struct address_space *mapping)
751 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
753 if (sbinfo->free_blocks > -1) {
754 spin_lock(&sbinfo->stat_lock);
755 sbinfo->free_blocks++;
756 spin_unlock(&sbinfo->stat_lock);
760 static struct super_block *hugetlbfs_get_sb(struct file_system_type *fs_type,
761 int flags, const char *dev_name, void *data)
763 return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super);
766 static struct file_system_type hugetlbfs_fs_type = {
768 .get_sb = hugetlbfs_get_sb,
769 .kill_sb = kill_litter_super,
772 static struct vfsmount *hugetlbfs_vfsmount;
775 * Return the next identifier for a shm file
777 static unsigned long hugetlbfs_counter(void)
779 static DEFINE_SPINLOCK(lock);
780 static unsigned long counter;
789 static int can_do_hugetlb_shm(void)
791 return likely(capable(CAP_IPC_LOCK) ||
792 in_group_p(sysctl_hugetlb_shm_group) ||
796 struct file *hugetlb_zero_setup(size_t size)
801 struct dentry *dentry, *root;
802 struct qstr quick_string;
805 if (!can_do_hugetlb_shm())
806 return ERR_PTR(-EPERM);
808 if (!is_hugepage_mem_enough(size))
809 return ERR_PTR(-ENOMEM);
811 if (!user_shm_lock(size, current->user))
812 return ERR_PTR(-ENOMEM);
814 root = hugetlbfs_vfsmount->mnt_root;
815 snprintf(buf, 16, "%lu", hugetlbfs_counter());
816 quick_string.name = buf;
817 quick_string.len = strlen(quick_string.name);
818 quick_string.hash = 0;
819 dentry = d_alloc(root, &quick_string);
824 file = get_empty_filp();
829 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
830 current->fsgid, S_IFREG | S_IRWXUGO, 0);
834 d_instantiate(dentry, inode);
835 inode->i_size = size;
837 file->f_vfsmnt = mntget(hugetlbfs_vfsmount);
838 file->f_dentry = dentry;
839 file->f_mapping = inode->i_mapping;
840 file->f_op = &hugetlbfs_file_operations;
841 file->f_mode = FMODE_WRITE | FMODE_READ;
849 user_shm_unlock(size, current->user);
850 return ERR_PTR(error);
853 static int __init init_hugetlbfs_fs(void)
856 struct vfsmount *vfsmount;
858 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
859 sizeof(struct hugetlbfs_inode_info),
860 0, 0, init_once, NULL);
861 if (hugetlbfs_inode_cachep == NULL)
864 error = register_filesystem(&hugetlbfs_fs_type);
868 vfsmount = kern_mount(&hugetlbfs_fs_type);
870 if (!IS_ERR(vfsmount)) {
871 hugetlbfs_vfsmount = vfsmount;
875 error = PTR_ERR(vfsmount);
879 kmem_cache_destroy(hugetlbfs_inode_cachep);
883 static void __exit exit_hugetlbfs_fs(void)
885 kmem_cache_destroy(hugetlbfs_inode_cachep);
886 unregister_filesystem(&hugetlbfs_fs_type);
889 module_init(init_hugetlbfs_fs)
890 module_exit(exit_hugetlbfs_fs)
892 MODULE_LICENSE("GPL");