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;
54 if ((vma->vm_flags & (VM_MAYSHARE | VM_WRITE)) == VM_WRITE)
57 if (vma->vm_pgoff & (HPAGE_SIZE / PAGE_SIZE - 1))
60 if (vma->vm_start & ~HPAGE_MASK)
63 if (vma->vm_end & ~HPAGE_MASK)
66 if (vma->vm_end - vma->vm_start < HPAGE_SIZE)
69 vma_len = (loff_t)(vma->vm_end - vma->vm_start);
73 vma->vm_flags |= VM_HUGETLB | VM_RESERVED;
74 vma->vm_ops = &hugetlb_vm_ops;
77 len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
78 if (!(vma->vm_flags & VM_WRITE) && len > inode->i_size)
82 hugetlb_prefault_arch_hook(vma->vm_mm);
83 if (inode->i_size < len)
92 * Called under down_write(mmap_sem).
95 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
96 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
97 unsigned long len, unsigned long pgoff, unsigned long flags);
100 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
101 unsigned long len, unsigned long pgoff, unsigned long flags)
103 struct mm_struct *mm = current->mm;
104 struct vm_area_struct *vma;
105 unsigned long start_addr;
107 if (len & ~HPAGE_MASK)
113 addr = ALIGN(addr, HPAGE_SIZE);
114 vma = find_vma(mm, addr);
115 if (TASK_SIZE - len >= addr &&
116 (!vma || addr + len <= vma->vm_start))
120 start_addr = mm->free_area_cache;
122 if (len <= mm->cached_hole_size)
123 start_addr = TASK_UNMAPPED_BASE;
126 addr = ALIGN(start_addr, HPAGE_SIZE);
128 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
129 /* At this point: (!vma || addr < vma->vm_end). */
130 if (TASK_SIZE - len < addr) {
132 * Start a new search - just in case we missed
135 if (start_addr != TASK_UNMAPPED_BASE) {
136 start_addr = TASK_UNMAPPED_BASE;
142 if (!vma || addr + len <= vma->vm_start)
144 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
150 * Read a page. Again trivial. If it didn't already exist
151 * in the page cache, it is zero-filled.
153 static int hugetlbfs_readpage(struct file *file, struct page * page)
159 static int hugetlbfs_prepare_write(struct file *file,
160 struct page *page, unsigned offset, unsigned to)
165 static int hugetlbfs_commit_write(struct file *file,
166 struct page *page, unsigned offset, unsigned to)
171 static void huge_pagevec_release(struct pagevec *pvec)
175 for (i = 0; i < pagevec_count(pvec); ++i)
176 put_page(pvec->pages[i]);
178 pagevec_reinit(pvec);
181 static void truncate_huge_page(struct page *page)
183 clear_page_dirty(page);
184 ClearPageUptodate(page);
185 remove_from_page_cache(page);
189 static void truncate_hugepages(struct address_space *mapping, loff_t lstart)
191 const pgoff_t start = lstart >> HPAGE_SHIFT;
196 pagevec_init(&pvec, 0);
199 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
206 for (i = 0; i < pagevec_count(&pvec); ++i) {
207 struct page *page = pvec.pages[i];
210 if (page->index > next)
213 truncate_huge_page(page);
215 hugetlb_put_quota(mapping);
217 huge_pagevec_release(&pvec);
219 BUG_ON(!lstart && mapping->nrpages);
222 static void hugetlbfs_delete_inode(struct inode *inode)
224 if (inode->i_data.nrpages)
225 truncate_hugepages(&inode->i_data, 0);
229 static void hugetlbfs_forget_inode(struct inode *inode)
231 struct super_block *sb = inode->i_sb;
233 if (!hlist_unhashed(&inode->i_hash)) {
234 if (!(inode->i_state & (I_DIRTY|I_LOCK)))
235 list_move(&inode->i_list, &inode_unused);
236 inodes_stat.nr_unused++;
237 if (!sb || (sb->s_flags & MS_ACTIVE)) {
238 spin_unlock(&inode_lock);
241 inode->i_state |= I_WILL_FREE;
242 spin_unlock(&inode_lock);
244 * write_inode_now is a noop as we set BDI_CAP_NO_WRITEBACK
245 * in our backing_dev_info.
247 write_inode_now(inode, 1);
248 spin_lock(&inode_lock);
249 inode->i_state &= ~I_WILL_FREE;
250 inodes_stat.nr_unused--;
251 hlist_del_init(&inode->i_hash);
253 list_del_init(&inode->i_list);
254 list_del_init(&inode->i_sb_list);
255 inode->i_state |= I_FREEING;
256 inodes_stat.nr_inodes--;
257 spin_unlock(&inode_lock);
258 if (inode->i_data.nrpages)
259 truncate_hugepages(&inode->i_data, 0);
261 destroy_inode(inode);
264 static void hugetlbfs_drop_inode(struct inode *inode)
267 generic_delete_inode(inode);
269 hugetlbfs_forget_inode(inode);
273 * h_pgoff is in HPAGE_SIZE units.
274 * vma->vm_pgoff is in PAGE_SIZE units.
277 hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
279 struct vm_area_struct *vma;
280 struct prio_tree_iter iter;
282 vma_prio_tree_foreach(vma, &iter, root, h_pgoff, ULONG_MAX) {
283 unsigned long h_vm_pgoff;
284 unsigned long v_offset;
286 h_vm_pgoff = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
287 v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT;
289 * Is this VMA fully outside the truncation point?
291 if (h_vm_pgoff >= h_pgoff)
294 unmap_hugepage_range(vma,
295 vma->vm_start + v_offset, vma->vm_end);
300 * Expanding truncates are not allowed.
302 static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
305 struct address_space *mapping = inode->i_mapping;
307 if (offset > inode->i_size)
310 BUG_ON(offset & ~HPAGE_MASK);
311 pgoff = offset >> HPAGE_SHIFT;
313 inode->i_size = offset;
314 spin_lock(&mapping->i_mmap_lock);
315 if (!prio_tree_empty(&mapping->i_mmap))
316 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
317 spin_unlock(&mapping->i_mmap_lock);
318 truncate_hugepages(mapping, offset);
322 static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
324 struct inode *inode = dentry->d_inode;
326 unsigned int ia_valid = attr->ia_valid;
330 error = inode_change_ok(inode, attr);
334 if (ia_valid & ATTR_SIZE) {
336 if (!(attr->ia_size & ~HPAGE_MASK))
337 error = hugetlb_vmtruncate(inode, attr->ia_size);
340 attr->ia_valid &= ~ATTR_SIZE;
342 error = inode_setattr(inode, attr);
347 static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
348 gid_t gid, int mode, dev_t dev)
352 inode = new_inode(sb);
354 struct hugetlbfs_inode_info *info;
355 inode->i_mode = mode;
358 inode->i_blksize = HPAGE_SIZE;
360 inode->i_mapping->a_ops = &hugetlbfs_aops;
361 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
362 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
363 info = HUGETLBFS_I(inode);
364 mpol_shared_policy_init(&info->policy);
365 switch (mode & S_IFMT) {
367 init_special_inode(inode, mode, dev);
370 inode->i_op = &hugetlbfs_inode_operations;
371 inode->i_fop = &hugetlbfs_file_operations;
374 inode->i_op = &hugetlbfs_dir_inode_operations;
375 inode->i_fop = &simple_dir_operations;
377 /* directory inodes start off with i_nlink == 2 (for "." entry) */
381 inode->i_op = &page_symlink_inode_operations;
389 * File creation. Allocate an inode, and we're done..
391 static int hugetlbfs_mknod(struct inode *dir,
392 struct dentry *dentry, int mode, dev_t dev)
398 if (dir->i_mode & S_ISGID) {
403 gid = current->fsgid;
405 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
407 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
408 d_instantiate(dentry, inode);
409 dget(dentry); /* Extra count - pin the dentry in core */
415 static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
417 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
423 static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
425 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
428 static int hugetlbfs_symlink(struct inode *dir,
429 struct dentry *dentry, const char *symname)
435 if (dir->i_mode & S_ISGID)
438 gid = current->fsgid;
440 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
441 gid, S_IFLNK|S_IRWXUGO, 0);
443 int l = strlen(symname)+1;
444 error = page_symlink(inode, symname, l);
446 d_instantiate(dentry, inode);
451 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
457 * For direct-IO reads into hugetlb pages
459 static int hugetlbfs_set_page_dirty(struct page *page)
464 static int hugetlbfs_statfs(struct super_block *sb, struct kstatfs *buf)
466 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
468 buf->f_type = HUGETLBFS_MAGIC;
469 buf->f_bsize = HPAGE_SIZE;
471 spin_lock(&sbinfo->stat_lock);
472 buf->f_blocks = sbinfo->max_blocks;
473 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
474 buf->f_files = sbinfo->max_inodes;
475 buf->f_ffree = sbinfo->free_inodes;
476 spin_unlock(&sbinfo->stat_lock);
478 buf->f_namelen = NAME_MAX;
482 static void hugetlbfs_put_super(struct super_block *sb)
484 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
487 sb->s_fs_info = NULL;
492 static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
494 if (sbinfo->free_inodes >= 0) {
495 spin_lock(&sbinfo->stat_lock);
496 if (unlikely(!sbinfo->free_inodes)) {
497 spin_unlock(&sbinfo->stat_lock);
500 sbinfo->free_inodes--;
501 spin_unlock(&sbinfo->stat_lock);
507 static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
509 if (sbinfo->free_inodes >= 0) {
510 spin_lock(&sbinfo->stat_lock);
511 sbinfo->free_inodes++;
512 spin_unlock(&sbinfo->stat_lock);
517 static kmem_cache_t *hugetlbfs_inode_cachep;
519 static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
521 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
522 struct hugetlbfs_inode_info *p;
524 if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
526 p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
528 hugetlbfs_inc_free_inodes(sbinfo);
531 return &p->vfs_inode;
534 static void hugetlbfs_destroy_inode(struct inode *inode)
536 hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
537 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
538 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
541 static struct address_space_operations hugetlbfs_aops = {
542 .readpage = hugetlbfs_readpage,
543 .prepare_write = hugetlbfs_prepare_write,
544 .commit_write = hugetlbfs_commit_write,
545 .set_page_dirty = hugetlbfs_set_page_dirty,
549 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
551 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
553 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
554 SLAB_CTOR_CONSTRUCTOR)
555 inode_init_once(&ei->vfs_inode);
558 struct file_operations hugetlbfs_file_operations = {
559 .mmap = hugetlbfs_file_mmap,
560 .fsync = simple_sync_file,
561 .get_unmapped_area = hugetlb_get_unmapped_area,
564 static struct inode_operations hugetlbfs_dir_inode_operations = {
565 .create = hugetlbfs_create,
566 .lookup = simple_lookup,
568 .unlink = simple_unlink,
569 .symlink = hugetlbfs_symlink,
570 .mkdir = hugetlbfs_mkdir,
571 .rmdir = simple_rmdir,
572 .mknod = hugetlbfs_mknod,
573 .rename = simple_rename,
574 .setattr = hugetlbfs_setattr,
577 static struct inode_operations hugetlbfs_inode_operations = {
578 .setattr = hugetlbfs_setattr,
581 static struct super_operations hugetlbfs_ops = {
582 .alloc_inode = hugetlbfs_alloc_inode,
583 .destroy_inode = hugetlbfs_destroy_inode,
584 .statfs = hugetlbfs_statfs,
585 .delete_inode = hugetlbfs_delete_inode,
586 .drop_inode = hugetlbfs_drop_inode,
587 .put_super = hugetlbfs_put_super,
591 hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
593 char *opt, *value, *rest;
597 while ((opt = strsep(&options, ",")) != NULL) {
601 value = strchr(opt, '=');
602 if (!value || !*value)
607 if (!strcmp(opt, "uid"))
608 pconfig->uid = simple_strtoul(value, &value, 0);
609 else if (!strcmp(opt, "gid"))
610 pconfig->gid = simple_strtoul(value, &value, 0);
611 else if (!strcmp(opt, "mode"))
612 pconfig->mode = simple_strtoul(value,&value,0) & 0777U;
613 else if (!strcmp(opt, "size")) {
614 unsigned long long size = memparse(value, &rest);
616 size <<= HPAGE_SHIFT;
617 size *= max_huge_pages;
622 pconfig->nr_blocks = (size >> HPAGE_SHIFT);
624 } else if (!strcmp(opt,"nr_inodes")) {
625 pconfig->nr_inodes = memparse(value, &rest);
637 hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
639 struct inode * inode;
640 struct dentry * root;
642 struct hugetlbfs_config config;
643 struct hugetlbfs_sb_info *sbinfo;
645 config.nr_blocks = -1; /* No limit on size by default */
646 config.nr_inodes = -1; /* No limit on number of inodes by default */
647 config.uid = current->fsuid;
648 config.gid = current->fsgid;
650 ret = hugetlbfs_parse_options(data, &config);
655 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
658 sb->s_fs_info = sbinfo;
659 spin_lock_init(&sbinfo->stat_lock);
660 sbinfo->max_blocks = config.nr_blocks;
661 sbinfo->free_blocks = config.nr_blocks;
662 sbinfo->max_inodes = config.nr_inodes;
663 sbinfo->free_inodes = config.nr_inodes;
664 sb->s_maxbytes = MAX_LFS_FILESIZE;
665 sb->s_blocksize = HPAGE_SIZE;
666 sb->s_blocksize_bits = HPAGE_SHIFT;
667 sb->s_magic = HUGETLBFS_MAGIC;
668 sb->s_op = &hugetlbfs_ops;
670 inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
671 S_IFDIR | config.mode, 0);
675 root = d_alloc_root(inode);
687 int hugetlb_get_quota(struct address_space *mapping)
690 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
692 if (sbinfo->free_blocks > -1) {
693 spin_lock(&sbinfo->stat_lock);
694 if (sbinfo->free_blocks > 0)
695 sbinfo->free_blocks--;
698 spin_unlock(&sbinfo->stat_lock);
704 void hugetlb_put_quota(struct address_space *mapping)
706 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
708 if (sbinfo->free_blocks > -1) {
709 spin_lock(&sbinfo->stat_lock);
710 sbinfo->free_blocks++;
711 spin_unlock(&sbinfo->stat_lock);
715 static struct super_block *hugetlbfs_get_sb(struct file_system_type *fs_type,
716 int flags, const char *dev_name, void *data)
718 return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super);
721 static struct file_system_type hugetlbfs_fs_type = {
723 .get_sb = hugetlbfs_get_sb,
724 .kill_sb = kill_litter_super,
727 static struct vfsmount *hugetlbfs_vfsmount;
730 * Return the next identifier for a shm file
732 static unsigned long hugetlbfs_counter(void)
734 static DEFINE_SPINLOCK(lock);
735 static unsigned long counter;
744 static int can_do_hugetlb_shm(void)
746 return likely(capable(CAP_IPC_LOCK) ||
747 in_group_p(sysctl_hugetlb_shm_group) ||
751 struct file *hugetlb_zero_setup(size_t size)
756 struct dentry *dentry, *root;
757 struct qstr quick_string;
760 if (!can_do_hugetlb_shm())
761 return ERR_PTR(-EPERM);
763 if (!is_hugepage_mem_enough(size))
764 return ERR_PTR(-ENOMEM);
766 if (!user_shm_lock(size, current->user))
767 return ERR_PTR(-ENOMEM);
769 root = hugetlbfs_vfsmount->mnt_root;
770 snprintf(buf, 16, "%lu", hugetlbfs_counter());
771 quick_string.name = buf;
772 quick_string.len = strlen(quick_string.name);
773 quick_string.hash = 0;
774 dentry = d_alloc(root, &quick_string);
779 file = get_empty_filp();
784 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
785 current->fsgid, S_IFREG | S_IRWXUGO, 0);
789 d_instantiate(dentry, inode);
790 inode->i_size = size;
792 file->f_vfsmnt = mntget(hugetlbfs_vfsmount);
793 file->f_dentry = dentry;
794 file->f_mapping = inode->i_mapping;
795 file->f_op = &hugetlbfs_file_operations;
796 file->f_mode = FMODE_WRITE | FMODE_READ;
804 user_shm_unlock(size, current->user);
805 return ERR_PTR(error);
808 static int __init init_hugetlbfs_fs(void)
811 struct vfsmount *vfsmount;
813 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
814 sizeof(struct hugetlbfs_inode_info),
815 0, 0, init_once, NULL);
816 if (hugetlbfs_inode_cachep == NULL)
819 error = register_filesystem(&hugetlbfs_fs_type);
823 vfsmount = kern_mount(&hugetlbfs_fs_type);
825 if (!IS_ERR(vfsmount)) {
826 hugetlbfs_vfsmount = vfsmount;
830 error = PTR_ERR(vfsmount);
834 kmem_cache_destroy(hugetlbfs_inode_cachep);
838 static void __exit exit_hugetlbfs_fs(void)
840 kmem_cache_destroy(hugetlbfs_inode_cachep);
841 unregister_filesystem(&hugetlbfs_fs_type);
844 module_init(init_hugetlbfs_fs)
845 module_exit(exit_hugetlbfs_fs)
847 MODULE_LICENSE("GPL");