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/kernel.h>
17 #include <linux/writeback.h>
18 #include <linux/pagemap.h>
19 #include <linux/highmem.h>
20 #include <linux/init.h>
21 #include <linux/string.h>
22 #include <linux/capability.h>
23 #include <linux/ctype.h>
24 #include <linux/backing-dev.h>
25 #include <linux/hugetlb.h>
26 #include <linux/pagevec.h>
27 #include <linux/parser.h>
28 #include <linux/mman.h>
29 #include <linux/quotaops.h>
30 #include <linux/slab.h>
31 #include <linux/dnotify.h>
32 #include <linux/statfs.h>
33 #include <linux/security.h>
35 #include <asm/uaccess.h>
37 /* some random number */
38 #define HUGETLBFS_MAGIC 0x958458f6
40 static const struct super_operations hugetlbfs_ops;
41 static const struct address_space_operations hugetlbfs_aops;
42 const struct file_operations hugetlbfs_file_operations;
43 static const struct inode_operations hugetlbfs_dir_inode_operations;
44 static const struct inode_operations hugetlbfs_inode_operations;
46 static struct backing_dev_info hugetlbfs_backing_dev_info = {
47 .ra_pages = 0, /* No readahead */
48 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
51 int sysctl_hugetlb_shm_group;
54 Opt_size, Opt_nr_inodes,
55 Opt_mode, Opt_uid, Opt_gid,
59 static match_table_t tokens = {
60 {Opt_size, "size=%s"},
61 {Opt_nr_inodes, "nr_inodes=%s"},
62 {Opt_mode, "mode=%o"},
68 static void huge_pagevec_release(struct pagevec *pvec)
72 for (i = 0; i < pagevec_count(pvec); ++i)
73 put_page(pvec->pages[i]);
78 static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
80 struct inode *inode = file->f_path.dentry->d_inode;
85 * vma alignment has already been checked by prepare_hugepage_range.
86 * If you add any error returns here, do so after setting VM_HUGETLB,
87 * so is_vm_hugetlb_page tests below unmap_region go the right way
88 * when do_mmap_pgoff unwinds (may be important on powerpc and ia64).
90 vma->vm_flags |= VM_HUGETLB | VM_RESERVED;
91 vma->vm_ops = &hugetlb_vm_ops;
93 vma_len = (loff_t)(vma->vm_end - vma->vm_start);
95 mutex_lock(&inode->i_mutex);
99 len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
101 if (vma->vm_flags & VM_MAYSHARE &&
102 hugetlb_reserve_pages(inode, vma->vm_pgoff >> (HPAGE_SHIFT-PAGE_SHIFT),
107 hugetlb_prefault_arch_hook(vma->vm_mm);
108 if (vma->vm_flags & VM_WRITE && inode->i_size < len)
111 mutex_unlock(&inode->i_mutex);
117 * Called under down_write(mmap_sem).
120 #ifndef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
122 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
123 unsigned long len, unsigned long pgoff, unsigned long flags)
125 struct mm_struct *mm = current->mm;
126 struct vm_area_struct *vma;
127 unsigned long start_addr;
129 if (len & ~HPAGE_MASK)
134 if (flags & MAP_FIXED) {
135 if (prepare_hugepage_range(addr, len, pgoff))
141 addr = ALIGN(addr, HPAGE_SIZE);
142 vma = find_vma(mm, addr);
143 if (TASK_SIZE - len >= addr &&
144 (!vma || addr + len <= vma->vm_start))
148 start_addr = mm->free_area_cache;
150 if (len <= mm->cached_hole_size)
151 start_addr = TASK_UNMAPPED_BASE;
154 addr = ALIGN(start_addr, HPAGE_SIZE);
156 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
157 /* At this point: (!vma || addr < vma->vm_end). */
158 if (TASK_SIZE - len < addr) {
160 * Start a new search - just in case we missed
163 if (start_addr != TASK_UNMAPPED_BASE) {
164 start_addr = TASK_UNMAPPED_BASE;
170 if (!vma || addr + len <= vma->vm_start)
172 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
178 * Read a page. Again trivial. If it didn't already exist
179 * in the page cache, it is zero-filled.
181 static int hugetlbfs_readpage(struct file *file, struct page * page)
187 static int hugetlbfs_prepare_write(struct file *file,
188 struct page *page, unsigned offset, unsigned to)
193 static int hugetlbfs_commit_write(struct file *file,
194 struct page *page, unsigned offset, unsigned to)
199 static void truncate_huge_page(struct page *page)
201 cancel_dirty_page(page, /* No IO accounting for huge pages? */0);
202 ClearPageUptodate(page);
203 remove_from_page_cache(page);
207 static void truncate_hugepages(struct inode *inode, loff_t lstart)
209 struct address_space *mapping = &inode->i_data;
210 const pgoff_t start = lstart >> HPAGE_SHIFT;
215 pagevec_init(&pvec, 0);
218 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
225 for (i = 0; i < pagevec_count(&pvec); ++i) {
226 struct page *page = pvec.pages[i];
229 if (page->index > next)
232 truncate_huge_page(page);
234 hugetlb_put_quota(mapping);
237 huge_pagevec_release(&pvec);
239 BUG_ON(!lstart && mapping->nrpages);
240 hugetlb_unreserve_pages(inode, start, freed);
243 static void hugetlbfs_delete_inode(struct inode *inode)
245 truncate_hugepages(inode, 0);
249 static void hugetlbfs_forget_inode(struct inode *inode) __releases(inode_lock)
251 struct super_block *sb = inode->i_sb;
253 if (!hlist_unhashed(&inode->i_hash)) {
254 if (!(inode->i_state & (I_DIRTY|I_LOCK)))
255 list_move(&inode->i_list, &inode_unused);
256 inodes_stat.nr_unused++;
257 if (!sb || (sb->s_flags & MS_ACTIVE)) {
258 spin_unlock(&inode_lock);
261 inode->i_state |= I_WILL_FREE;
262 spin_unlock(&inode_lock);
264 * write_inode_now is a noop as we set BDI_CAP_NO_WRITEBACK
265 * in our backing_dev_info.
267 write_inode_now(inode, 1);
268 spin_lock(&inode_lock);
269 inode->i_state &= ~I_WILL_FREE;
270 inodes_stat.nr_unused--;
271 hlist_del_init(&inode->i_hash);
273 list_del_init(&inode->i_list);
274 list_del_init(&inode->i_sb_list);
275 inode->i_state |= I_FREEING;
276 inodes_stat.nr_inodes--;
277 spin_unlock(&inode_lock);
278 truncate_hugepages(inode, 0);
280 destroy_inode(inode);
283 static void hugetlbfs_drop_inode(struct inode *inode)
286 generic_delete_inode(inode);
288 hugetlbfs_forget_inode(inode);
292 hugetlb_vmtruncate_list(struct prio_tree_root *root, pgoff_t pgoff)
294 struct vm_area_struct *vma;
295 struct prio_tree_iter iter;
297 vma_prio_tree_foreach(vma, &iter, root, pgoff, ULONG_MAX) {
298 unsigned long v_offset;
301 * Can the expression below overflow on 32-bit arches?
302 * No, because the prio_tree returns us only those vmas
303 * which overlap the truncated area starting at pgoff,
304 * and no vma on a 32-bit arch can span beyond the 4GB.
306 if (vma->vm_pgoff < pgoff)
307 v_offset = (pgoff - vma->vm_pgoff) << PAGE_SHIFT;
311 __unmap_hugepage_range(vma,
312 vma->vm_start + v_offset, vma->vm_end);
317 * Expanding truncates are not allowed.
319 static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
322 struct address_space *mapping = inode->i_mapping;
324 if (offset > inode->i_size)
327 BUG_ON(offset & ~HPAGE_MASK);
328 pgoff = offset >> PAGE_SHIFT;
330 inode->i_size = offset;
331 spin_lock(&mapping->i_mmap_lock);
332 if (!prio_tree_empty(&mapping->i_mmap))
333 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
334 spin_unlock(&mapping->i_mmap_lock);
335 truncate_hugepages(inode, offset);
339 static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
341 struct inode *inode = dentry->d_inode;
343 unsigned int ia_valid = attr->ia_valid;
347 error = inode_change_ok(inode, attr);
351 if (ia_valid & ATTR_SIZE) {
353 if (!(attr->ia_size & ~HPAGE_MASK))
354 error = hugetlb_vmtruncate(inode, attr->ia_size);
357 attr->ia_valid &= ~ATTR_SIZE;
359 error = inode_setattr(inode, attr);
364 static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
365 gid_t gid, int mode, dev_t dev)
369 inode = new_inode(sb);
371 struct hugetlbfs_inode_info *info;
372 inode->i_mode = mode;
376 inode->i_mapping->a_ops = &hugetlbfs_aops;
377 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
378 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
379 INIT_LIST_HEAD(&inode->i_mapping->private_list);
380 info = HUGETLBFS_I(inode);
381 mpol_shared_policy_init(&info->policy, MPOL_DEFAULT, NULL);
382 switch (mode & S_IFMT) {
384 init_special_inode(inode, mode, dev);
387 inode->i_op = &hugetlbfs_inode_operations;
388 inode->i_fop = &hugetlbfs_file_operations;
391 inode->i_op = &hugetlbfs_dir_inode_operations;
392 inode->i_fop = &simple_dir_operations;
394 /* directory inodes start off with i_nlink == 2 (for "." entry) */
398 inode->i_op = &page_symlink_inode_operations;
406 * File creation. Allocate an inode, and we're done..
408 static int hugetlbfs_mknod(struct inode *dir,
409 struct dentry *dentry, int mode, dev_t dev)
415 if (dir->i_mode & S_ISGID) {
420 gid = current->fsgid;
422 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
424 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
425 d_instantiate(dentry, inode);
426 dget(dentry); /* Extra count - pin the dentry in core */
432 static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
434 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
440 static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
442 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
445 static int hugetlbfs_symlink(struct inode *dir,
446 struct dentry *dentry, const char *symname)
452 if (dir->i_mode & S_ISGID)
455 gid = current->fsgid;
457 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
458 gid, S_IFLNK|S_IRWXUGO, 0);
460 int l = strlen(symname)+1;
461 error = page_symlink(inode, symname, l);
463 d_instantiate(dentry, inode);
468 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
474 * mark the head page dirty
476 static int hugetlbfs_set_page_dirty(struct page *page)
478 struct page *head = compound_head(page);
484 static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
486 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
488 buf->f_type = HUGETLBFS_MAGIC;
489 buf->f_bsize = HPAGE_SIZE;
491 spin_lock(&sbinfo->stat_lock);
492 /* If no limits set, just report 0 for max/free/used
493 * blocks, like simple_statfs() */
494 if (sbinfo->max_blocks >= 0) {
495 buf->f_blocks = sbinfo->max_blocks;
496 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
497 buf->f_files = sbinfo->max_inodes;
498 buf->f_ffree = sbinfo->free_inodes;
500 spin_unlock(&sbinfo->stat_lock);
502 buf->f_namelen = NAME_MAX;
506 static void hugetlbfs_put_super(struct super_block *sb)
508 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
511 sb->s_fs_info = NULL;
516 static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
518 if (sbinfo->free_inodes >= 0) {
519 spin_lock(&sbinfo->stat_lock);
520 if (unlikely(!sbinfo->free_inodes)) {
521 spin_unlock(&sbinfo->stat_lock);
524 sbinfo->free_inodes--;
525 spin_unlock(&sbinfo->stat_lock);
531 static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
533 if (sbinfo->free_inodes >= 0) {
534 spin_lock(&sbinfo->stat_lock);
535 sbinfo->free_inodes++;
536 spin_unlock(&sbinfo->stat_lock);
541 static struct kmem_cache *hugetlbfs_inode_cachep;
543 static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
545 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
546 struct hugetlbfs_inode_info *p;
548 if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
550 p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL);
552 hugetlbfs_inc_free_inodes(sbinfo);
555 return &p->vfs_inode;
558 static void hugetlbfs_destroy_inode(struct inode *inode)
560 hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
561 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
562 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
565 static const struct address_space_operations hugetlbfs_aops = {
566 .readpage = hugetlbfs_readpage,
567 .prepare_write = hugetlbfs_prepare_write,
568 .commit_write = hugetlbfs_commit_write,
569 .set_page_dirty = hugetlbfs_set_page_dirty,
573 static void init_once(void *foo, struct kmem_cache *cachep, unsigned long flags)
575 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
577 inode_init_once(&ei->vfs_inode);
580 const struct file_operations hugetlbfs_file_operations = {
581 .mmap = hugetlbfs_file_mmap,
582 .fsync = simple_sync_file,
583 .get_unmapped_area = hugetlb_get_unmapped_area,
586 static const struct inode_operations hugetlbfs_dir_inode_operations = {
587 .create = hugetlbfs_create,
588 .lookup = simple_lookup,
590 .unlink = simple_unlink,
591 .symlink = hugetlbfs_symlink,
592 .mkdir = hugetlbfs_mkdir,
593 .rmdir = simple_rmdir,
594 .mknod = hugetlbfs_mknod,
595 .rename = simple_rename,
596 .setattr = hugetlbfs_setattr,
599 static const struct inode_operations hugetlbfs_inode_operations = {
600 .setattr = hugetlbfs_setattr,
603 static const struct super_operations hugetlbfs_ops = {
604 .alloc_inode = hugetlbfs_alloc_inode,
605 .destroy_inode = hugetlbfs_destroy_inode,
606 .statfs = hugetlbfs_statfs,
607 .delete_inode = hugetlbfs_delete_inode,
608 .drop_inode = hugetlbfs_drop_inode,
609 .put_super = hugetlbfs_put_super,
613 hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
616 substring_t args[MAX_OPT_ARGS];
622 while ((p = strsep(&options, ",")) != NULL) {
627 token = match_token(p, tokens, args);
630 if (match_int(&args[0], &option))
632 pconfig->uid = option;
636 if (match_int(&args[0], &option))
638 pconfig->gid = option;
642 if (match_octal(&args[0], &option))
644 pconfig->mode = option & 0777U;
648 unsigned long long size;
649 /* memparse() will accept a K/M/G without a digit */
650 if (!isdigit(*args[0].from))
652 size = memparse(args[0].from, &rest);
654 size <<= HPAGE_SHIFT;
655 size *= max_huge_pages;
658 pconfig->nr_blocks = (size >> HPAGE_SHIFT);
663 /* memparse() will accept a K/M/G without a digit */
664 if (!isdigit(*args[0].from))
666 pconfig->nr_inodes = memparse(args[0].from, &rest);
670 printk(KERN_ERR "hugetlbfs: Bad mount option: \"%s\"\n",
679 printk(KERN_ERR "hugetlbfs: Bad value '%s' for mount option '%s'\n",
685 hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
687 struct inode * inode;
688 struct dentry * root;
690 struct hugetlbfs_config config;
691 struct hugetlbfs_sb_info *sbinfo;
693 config.nr_blocks = -1; /* No limit on size by default */
694 config.nr_inodes = -1; /* No limit on number of inodes by default */
695 config.uid = current->fsuid;
696 config.gid = current->fsgid;
698 ret = hugetlbfs_parse_options(data, &config);
702 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
705 sb->s_fs_info = sbinfo;
706 spin_lock_init(&sbinfo->stat_lock);
707 sbinfo->max_blocks = config.nr_blocks;
708 sbinfo->free_blocks = config.nr_blocks;
709 sbinfo->max_inodes = config.nr_inodes;
710 sbinfo->free_inodes = config.nr_inodes;
711 sb->s_maxbytes = MAX_LFS_FILESIZE;
712 sb->s_blocksize = HPAGE_SIZE;
713 sb->s_blocksize_bits = HPAGE_SHIFT;
714 sb->s_magic = HUGETLBFS_MAGIC;
715 sb->s_op = &hugetlbfs_ops;
717 inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
718 S_IFDIR | config.mode, 0);
722 root = d_alloc_root(inode);
734 int hugetlb_get_quota(struct address_space *mapping)
737 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
739 if (sbinfo->free_blocks > -1) {
740 spin_lock(&sbinfo->stat_lock);
741 if (sbinfo->free_blocks > 0)
742 sbinfo->free_blocks--;
745 spin_unlock(&sbinfo->stat_lock);
751 void hugetlb_put_quota(struct address_space *mapping)
753 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
755 if (sbinfo->free_blocks > -1) {
756 spin_lock(&sbinfo->stat_lock);
757 sbinfo->free_blocks++;
758 spin_unlock(&sbinfo->stat_lock);
762 static int hugetlbfs_get_sb(struct file_system_type *fs_type,
763 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
765 return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super, mnt);
768 static struct file_system_type hugetlbfs_fs_type = {
770 .get_sb = hugetlbfs_get_sb,
771 .kill_sb = kill_litter_super,
774 static struct vfsmount *hugetlbfs_vfsmount;
776 static int can_do_hugetlb_shm(void)
778 return likely(capable(CAP_IPC_LOCK) ||
779 in_group_p(sysctl_hugetlb_shm_group) ||
783 struct file *hugetlb_file_setup(const char *name, size_t size)
788 struct dentry *dentry, *root;
789 struct qstr quick_string;
791 if (!hugetlbfs_vfsmount)
792 return ERR_PTR(-ENOENT);
794 if (!can_do_hugetlb_shm())
795 return ERR_PTR(-EPERM);
797 if (!user_shm_lock(size, current->user))
798 return ERR_PTR(-ENOMEM);
800 root = hugetlbfs_vfsmount->mnt_root;
801 quick_string.name = name;
802 quick_string.len = strlen(quick_string.name);
803 quick_string.hash = 0;
804 dentry = d_alloc(root, &quick_string);
809 file = get_empty_filp();
814 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
815 current->fsgid, S_IFREG | S_IRWXUGO, 0);
820 if (hugetlb_reserve_pages(inode, 0, size >> HPAGE_SHIFT))
823 d_instantiate(dentry, inode);
824 inode->i_size = size;
826 file->f_path.mnt = mntget(hugetlbfs_vfsmount);
827 file->f_path.dentry = dentry;
828 file->f_mapping = inode->i_mapping;
829 file->f_op = &hugetlbfs_file_operations;
830 file->f_mode = FMODE_WRITE | FMODE_READ;
840 user_shm_unlock(size, current->user);
841 return ERR_PTR(error);
844 static int __init init_hugetlbfs_fs(void)
847 struct vfsmount *vfsmount;
849 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
850 sizeof(struct hugetlbfs_inode_info),
852 if (hugetlbfs_inode_cachep == NULL)
855 error = register_filesystem(&hugetlbfs_fs_type);
859 vfsmount = kern_mount(&hugetlbfs_fs_type);
861 if (!IS_ERR(vfsmount)) {
862 hugetlbfs_vfsmount = vfsmount;
866 error = PTR_ERR(vfsmount);
870 kmem_cache_destroy(hugetlbfs_inode_cachep);
874 static void __exit exit_hugetlbfs_fs(void)
876 kmem_cache_destroy(hugetlbfs_inode_cachep);
877 unregister_filesystem(&hugetlbfs_fs_type);
880 module_init(init_hugetlbfs_fs)
881 module_exit(exit_hugetlbfs_fs)
883 MODULE_LICENSE("GPL");