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/backing-dev.h>
22 #include <linux/hugetlb.h>
23 #include <linux/pagevec.h>
24 #include <linux/quotaops.h>
25 #include <linux/slab.h>
26 #include <linux/dnotify.h>
27 #include <linux/statfs.h>
28 #include <linux/security.h>
30 #include <asm/uaccess.h>
32 /* some random number */
33 #define HUGETLBFS_MAGIC 0x958458f6
35 static struct super_operations hugetlbfs_ops;
36 static struct address_space_operations hugetlbfs_aops;
37 struct file_operations hugetlbfs_file_operations;
38 static struct inode_operations hugetlbfs_dir_inode_operations;
39 static struct inode_operations hugetlbfs_inode_operations;
41 static struct backing_dev_info hugetlbfs_backing_dev_info = {
42 .ra_pages = 0, /* No readahead */
43 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
46 int sysctl_hugetlb_shm_group;
48 static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
50 struct inode *inode = file->f_dentry->d_inode;
51 struct address_space *mapping = inode->i_mapping;
55 if ((vma->vm_flags & (VM_MAYSHARE | VM_WRITE)) == VM_WRITE)
58 if (vma->vm_pgoff & (HPAGE_SIZE / PAGE_SIZE - 1))
61 if (vma->vm_start & ~HPAGE_MASK)
64 if (vma->vm_end & ~HPAGE_MASK)
67 if (vma->vm_end - vma->vm_start < HPAGE_SIZE)
70 vma_len = (loff_t)(vma->vm_end - vma->vm_start);
74 vma->vm_flags |= VM_HUGETLB | VM_RESERVED;
75 vma->vm_ops = &hugetlb_vm_ops;
78 len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
79 if (!(vma->vm_flags & VM_WRITE) && len > inode->i_size)
82 ret = hugetlb_prefault(mapping, vma);
86 if (inode->i_size < len)
95 * Called under down_write(mmap_sem).
98 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
99 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
100 unsigned long len, unsigned long pgoff, unsigned long flags);
103 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
104 unsigned long len, unsigned long pgoff, unsigned long flags)
106 struct mm_struct *mm = current->mm;
107 struct vm_area_struct *vma;
108 unsigned long start_addr;
110 if (len & ~HPAGE_MASK)
116 addr = ALIGN(addr, HPAGE_SIZE);
117 vma = find_vma(mm, addr);
118 if (TASK_SIZE - len >= addr &&
119 (!vma || addr + len <= vma->vm_start))
123 start_addr = mm->free_area_cache;
125 if (len <= mm->cached_hole_size)
126 start_addr = TASK_UNMAPPED_BASE;
129 addr = ALIGN(start_addr, HPAGE_SIZE);
131 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
132 /* At this point: (!vma || addr < vma->vm_end). */
133 if (TASK_SIZE - len < addr) {
135 * Start a new search - just in case we missed
138 if (start_addr != TASK_UNMAPPED_BASE) {
139 start_addr = TASK_UNMAPPED_BASE;
145 if (!vma || addr + len <= vma->vm_start)
147 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
153 * Read a page. Again trivial. If it didn't already exist
154 * in the page cache, it is zero-filled.
156 static int hugetlbfs_readpage(struct file *file, struct page * page)
162 static int hugetlbfs_prepare_write(struct file *file,
163 struct page *page, unsigned offset, unsigned to)
168 static int hugetlbfs_commit_write(struct file *file,
169 struct page *page, unsigned offset, unsigned to)
174 static void huge_pagevec_release(struct pagevec *pvec)
178 for (i = 0; i < pagevec_count(pvec); ++i)
179 put_page(pvec->pages[i]);
181 pagevec_reinit(pvec);
184 static void truncate_huge_page(struct page *page)
186 clear_page_dirty(page);
187 ClearPageUptodate(page);
188 remove_from_page_cache(page);
192 static void truncate_hugepages(struct address_space *mapping, loff_t lstart)
194 const pgoff_t start = lstart >> HPAGE_SHIFT;
199 pagevec_init(&pvec, 0);
202 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
209 for (i = 0; i < pagevec_count(&pvec); ++i) {
210 struct page *page = pvec.pages[i];
213 if (page->index > next)
216 truncate_huge_page(page);
218 hugetlb_put_quota(mapping);
220 huge_pagevec_release(&pvec);
222 BUG_ON(!lstart && mapping->nrpages);
225 static void hugetlbfs_delete_inode(struct inode *inode)
227 hlist_del_init(&inode->i_hash);
228 list_del_init(&inode->i_list);
229 list_del_init(&inode->i_sb_list);
230 inode->i_state |= I_FREEING;
231 inodes_stat.nr_inodes--;
232 spin_unlock(&inode_lock);
234 if (inode->i_data.nrpages)
235 truncate_hugepages(&inode->i_data, 0);
237 security_inode_delete(inode);
240 destroy_inode(inode);
243 static void hugetlbfs_forget_inode(struct inode *inode)
245 struct super_block *super_block = inode->i_sb;
247 if (hlist_unhashed(&inode->i_hash))
250 if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
251 list_del(&inode->i_list);
252 list_add(&inode->i_list, &inode_unused);
254 inodes_stat.nr_unused++;
255 if (!super_block || (super_block->s_flags & MS_ACTIVE)) {
256 spin_unlock(&inode_lock);
260 /* write_inode_now() ? */
261 inodes_stat.nr_unused--;
262 hlist_del_init(&inode->i_hash);
264 list_del_init(&inode->i_list);
265 list_del_init(&inode->i_sb_list);
266 inode->i_state |= I_FREEING;
267 inodes_stat.nr_inodes--;
268 spin_unlock(&inode_lock);
269 if (inode->i_data.nrpages)
270 truncate_hugepages(&inode->i_data, 0);
273 destroy_inode(inode);
276 static void hugetlbfs_drop_inode(struct inode *inode)
279 hugetlbfs_delete_inode(inode);
281 hugetlbfs_forget_inode(inode);
285 * h_pgoff is in HPAGE_SIZE units.
286 * vma->vm_pgoff is in PAGE_SIZE units.
289 hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
291 struct vm_area_struct *vma;
292 struct prio_tree_iter iter;
294 vma_prio_tree_foreach(vma, &iter, root, h_pgoff, ULONG_MAX) {
295 unsigned long h_vm_pgoff;
296 unsigned long v_offset;
298 h_vm_pgoff = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
299 v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT;
301 * Is this VMA fully outside the truncation point?
303 if (h_vm_pgoff >= h_pgoff)
306 unmap_hugepage_range(vma,
307 vma->vm_start + v_offset, vma->vm_end);
312 * Expanding truncates are not allowed.
314 static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
317 struct address_space *mapping = inode->i_mapping;
319 if (offset > inode->i_size)
322 BUG_ON(offset & ~HPAGE_MASK);
323 pgoff = offset >> HPAGE_SHIFT;
325 inode->i_size = offset;
326 spin_lock(&mapping->i_mmap_lock);
327 if (!prio_tree_empty(&mapping->i_mmap))
328 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
329 spin_unlock(&mapping->i_mmap_lock);
330 truncate_hugepages(mapping, offset);
334 static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
336 struct inode *inode = dentry->d_inode;
338 unsigned int ia_valid = attr->ia_valid;
342 error = inode_change_ok(inode, attr);
346 if (ia_valid & ATTR_SIZE) {
348 if (!(attr->ia_size & ~HPAGE_MASK))
349 error = hugetlb_vmtruncate(inode, attr->ia_size);
352 attr->ia_valid &= ~ATTR_SIZE;
354 error = inode_setattr(inode, attr);
359 static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
360 gid_t gid, int mode, dev_t dev)
364 inode = new_inode(sb);
366 struct hugetlbfs_inode_info *info;
367 inode->i_mode = mode;
370 inode->i_blksize = HPAGE_SIZE;
372 inode->i_mapping->a_ops = &hugetlbfs_aops;
373 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
374 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
375 info = HUGETLBFS_I(inode);
376 mpol_shared_policy_init(&info->policy);
377 switch (mode & S_IFMT) {
379 init_special_inode(inode, mode, dev);
382 inode->i_op = &hugetlbfs_inode_operations;
383 inode->i_fop = &hugetlbfs_file_operations;
386 inode->i_op = &hugetlbfs_dir_inode_operations;
387 inode->i_fop = &simple_dir_operations;
389 /* directory inodes start off with i_nlink == 2 (for "." entry) */
393 inode->i_op = &page_symlink_inode_operations;
401 * File creation. Allocate an inode, and we're done..
403 static int hugetlbfs_mknod(struct inode *dir,
404 struct dentry *dentry, int mode, dev_t dev)
410 if (dir->i_mode & S_ISGID) {
415 gid = current->fsgid;
417 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
419 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
420 d_instantiate(dentry, inode);
421 dget(dentry); /* Extra count - pin the dentry in core */
427 static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
429 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
435 static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
437 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
440 static int hugetlbfs_symlink(struct inode *dir,
441 struct dentry *dentry, const char *symname)
447 if (dir->i_mode & S_ISGID)
450 gid = current->fsgid;
452 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
453 gid, S_IFLNK|S_IRWXUGO, 0);
455 int l = strlen(symname)+1;
456 error = page_symlink(inode, symname, l);
458 d_instantiate(dentry, inode);
463 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
469 * For direct-IO reads into hugetlb pages
471 static int hugetlbfs_set_page_dirty(struct page *page)
476 static int hugetlbfs_statfs(struct super_block *sb, struct kstatfs *buf)
478 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
480 buf->f_type = HUGETLBFS_MAGIC;
481 buf->f_bsize = HPAGE_SIZE;
483 spin_lock(&sbinfo->stat_lock);
484 buf->f_blocks = sbinfo->max_blocks;
485 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
486 buf->f_files = sbinfo->max_inodes;
487 buf->f_ffree = sbinfo->free_inodes;
488 spin_unlock(&sbinfo->stat_lock);
490 buf->f_namelen = NAME_MAX;
494 static void hugetlbfs_put_super(struct super_block *sb)
496 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
499 sb->s_fs_info = NULL;
504 static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
506 if (sbinfo->free_inodes >= 0) {
507 spin_lock(&sbinfo->stat_lock);
508 if (unlikely(!sbinfo->free_inodes)) {
509 spin_unlock(&sbinfo->stat_lock);
512 sbinfo->free_inodes--;
513 spin_unlock(&sbinfo->stat_lock);
519 static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
521 if (sbinfo->free_inodes >= 0) {
522 spin_lock(&sbinfo->stat_lock);
523 sbinfo->free_inodes++;
524 spin_unlock(&sbinfo->stat_lock);
529 static kmem_cache_t *hugetlbfs_inode_cachep;
531 static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
533 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
534 struct hugetlbfs_inode_info *p;
536 if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
538 p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
540 hugetlbfs_inc_free_inodes(sbinfo);
543 return &p->vfs_inode;
546 static void hugetlbfs_destroy_inode(struct inode *inode)
548 hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
549 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
550 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
553 static struct address_space_operations hugetlbfs_aops = {
554 .readpage = hugetlbfs_readpage,
555 .prepare_write = hugetlbfs_prepare_write,
556 .commit_write = hugetlbfs_commit_write,
557 .set_page_dirty = hugetlbfs_set_page_dirty,
561 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
563 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
565 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
566 SLAB_CTOR_CONSTRUCTOR)
567 inode_init_once(&ei->vfs_inode);
570 struct file_operations hugetlbfs_file_operations = {
571 .mmap = hugetlbfs_file_mmap,
572 .fsync = simple_sync_file,
573 .get_unmapped_area = hugetlb_get_unmapped_area,
576 static struct inode_operations hugetlbfs_dir_inode_operations = {
577 .create = hugetlbfs_create,
578 .lookup = simple_lookup,
580 .unlink = simple_unlink,
581 .symlink = hugetlbfs_symlink,
582 .mkdir = hugetlbfs_mkdir,
583 .rmdir = simple_rmdir,
584 .mknod = hugetlbfs_mknod,
585 .rename = simple_rename,
586 .setattr = hugetlbfs_setattr,
589 static struct inode_operations hugetlbfs_inode_operations = {
590 .setattr = hugetlbfs_setattr,
593 static struct super_operations hugetlbfs_ops = {
594 .alloc_inode = hugetlbfs_alloc_inode,
595 .destroy_inode = hugetlbfs_destroy_inode,
596 .statfs = hugetlbfs_statfs,
597 .drop_inode = hugetlbfs_drop_inode,
598 .put_super = hugetlbfs_put_super,
602 hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
604 char *opt, *value, *rest;
608 while ((opt = strsep(&options, ",")) != NULL) {
612 value = strchr(opt, '=');
613 if (!value || !*value)
618 if (!strcmp(opt, "uid"))
619 pconfig->uid = simple_strtoul(value, &value, 0);
620 else if (!strcmp(opt, "gid"))
621 pconfig->gid = simple_strtoul(value, &value, 0);
622 else if (!strcmp(opt, "mode"))
623 pconfig->mode = simple_strtoul(value,&value,0) & 0777U;
624 else if (!strcmp(opt, "size")) {
625 unsigned long long size = memparse(value, &rest);
627 size <<= HPAGE_SHIFT;
628 size *= max_huge_pages;
633 pconfig->nr_blocks = (size >> HPAGE_SHIFT);
635 } else if (!strcmp(opt,"nr_inodes")) {
636 pconfig->nr_inodes = memparse(value, &rest);
648 hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
650 struct inode * inode;
651 struct dentry * root;
653 struct hugetlbfs_config config;
654 struct hugetlbfs_sb_info *sbinfo;
656 config.nr_blocks = -1; /* No limit on size by default */
657 config.nr_inodes = -1; /* No limit on number of inodes by default */
658 config.uid = current->fsuid;
659 config.gid = current->fsgid;
661 ret = hugetlbfs_parse_options(data, &config);
666 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
669 sb->s_fs_info = sbinfo;
670 spin_lock_init(&sbinfo->stat_lock);
671 sbinfo->max_blocks = config.nr_blocks;
672 sbinfo->free_blocks = config.nr_blocks;
673 sbinfo->max_inodes = config.nr_inodes;
674 sbinfo->free_inodes = config.nr_inodes;
675 sb->s_maxbytes = MAX_LFS_FILESIZE;
676 sb->s_blocksize = HPAGE_SIZE;
677 sb->s_blocksize_bits = HPAGE_SHIFT;
678 sb->s_magic = HUGETLBFS_MAGIC;
679 sb->s_op = &hugetlbfs_ops;
681 inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
682 S_IFDIR | config.mode, 0);
686 root = d_alloc_root(inode);
698 int hugetlb_get_quota(struct address_space *mapping)
701 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
703 if (sbinfo->free_blocks > -1) {
704 spin_lock(&sbinfo->stat_lock);
705 if (sbinfo->free_blocks > 0)
706 sbinfo->free_blocks--;
709 spin_unlock(&sbinfo->stat_lock);
715 void hugetlb_put_quota(struct address_space *mapping)
717 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
719 if (sbinfo->free_blocks > -1) {
720 spin_lock(&sbinfo->stat_lock);
721 sbinfo->free_blocks++;
722 spin_unlock(&sbinfo->stat_lock);
726 static struct super_block *hugetlbfs_get_sb(struct file_system_type *fs_type,
727 int flags, const char *dev_name, void *data)
729 return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super);
732 static struct file_system_type hugetlbfs_fs_type = {
734 .get_sb = hugetlbfs_get_sb,
735 .kill_sb = kill_litter_super,
738 static struct vfsmount *hugetlbfs_vfsmount;
741 * Return the next identifier for a shm file
743 static unsigned long hugetlbfs_counter(void)
745 static DEFINE_SPINLOCK(lock);
746 static unsigned long counter;
755 static int can_do_hugetlb_shm(void)
757 return likely(capable(CAP_IPC_LOCK) ||
758 in_group_p(sysctl_hugetlb_shm_group) ||
762 struct file *hugetlb_zero_setup(size_t size)
767 struct dentry *dentry, *root;
768 struct qstr quick_string;
771 if (!can_do_hugetlb_shm())
772 return ERR_PTR(-EPERM);
774 if (!is_hugepage_mem_enough(size))
775 return ERR_PTR(-ENOMEM);
777 if (!user_shm_lock(size, current->user))
778 return ERR_PTR(-ENOMEM);
780 root = hugetlbfs_vfsmount->mnt_root;
781 snprintf(buf, 16, "%lu", hugetlbfs_counter());
782 quick_string.name = buf;
783 quick_string.len = strlen(quick_string.name);
784 quick_string.hash = 0;
785 dentry = d_alloc(root, &quick_string);
790 file = get_empty_filp();
795 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
796 current->fsgid, S_IFREG | S_IRWXUGO, 0);
800 d_instantiate(dentry, inode);
801 inode->i_size = size;
803 file->f_vfsmnt = mntget(hugetlbfs_vfsmount);
804 file->f_dentry = dentry;
805 file->f_mapping = inode->i_mapping;
806 file->f_op = &hugetlbfs_file_operations;
807 file->f_mode = FMODE_WRITE | FMODE_READ;
815 user_shm_unlock(size, current->user);
816 return ERR_PTR(error);
819 static int __init init_hugetlbfs_fs(void)
822 struct vfsmount *vfsmount;
824 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
825 sizeof(struct hugetlbfs_inode_info),
826 0, 0, init_once, NULL);
827 if (hugetlbfs_inode_cachep == NULL)
830 error = register_filesystem(&hugetlbfs_fs_type);
834 vfsmount = kern_mount(&hugetlbfs_fs_type);
836 if (!IS_ERR(vfsmount)) {
837 hugetlbfs_vfsmount = vfsmount;
841 error = PTR_ERR(vfsmount);
845 kmem_cache_destroy(hugetlbfs_inode_cachep);
849 static void __exit exit_hugetlbfs_fs(void)
851 kmem_cache_destroy(hugetlbfs_inode_cachep);
852 unregister_filesystem(&hugetlbfs_fs_type);
855 module_init(init_hugetlbfs_fs)
856 module_exit(exit_hugetlbfs_fs)
858 MODULE_LICENSE("GPL");