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 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(inode->i_sb);
229 hlist_del_init(&inode->i_hash);
230 list_del_init(&inode->i_list);
231 list_del_init(&inode->i_sb_list);
232 inode->i_state |= I_FREEING;
233 inodes_stat.nr_inodes--;
234 spin_unlock(&inode_lock);
236 if (inode->i_data.nrpages)
237 truncate_hugepages(&inode->i_data, 0);
239 security_inode_delete(inode);
241 if (sbinfo->free_inodes >= 0) {
242 spin_lock(&sbinfo->stat_lock);
243 sbinfo->free_inodes++;
244 spin_unlock(&sbinfo->stat_lock);
248 destroy_inode(inode);
251 static void hugetlbfs_forget_inode(struct inode *inode)
253 struct super_block *super_block = inode->i_sb;
254 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(super_block);
256 if (hlist_unhashed(&inode->i_hash))
259 if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
260 list_del(&inode->i_list);
261 list_add(&inode->i_list, &inode_unused);
263 inodes_stat.nr_unused++;
264 if (!super_block || (super_block->s_flags & MS_ACTIVE)) {
265 spin_unlock(&inode_lock);
269 /* write_inode_now() ? */
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 if (inode->i_data.nrpages)
279 truncate_hugepages(&inode->i_data, 0);
281 if (sbinfo->free_inodes >= 0) {
282 spin_lock(&sbinfo->stat_lock);
283 sbinfo->free_inodes++;
284 spin_unlock(&sbinfo->stat_lock);
288 destroy_inode(inode);
291 static void hugetlbfs_drop_inode(struct inode *inode)
294 hugetlbfs_delete_inode(inode);
296 hugetlbfs_forget_inode(inode);
300 * h_pgoff is in HPAGE_SIZE units.
301 * vma->vm_pgoff is in PAGE_SIZE units.
304 hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
306 struct vm_area_struct *vma;
307 struct prio_tree_iter iter;
309 vma_prio_tree_foreach(vma, &iter, root, h_pgoff, ULONG_MAX) {
310 unsigned long h_vm_pgoff;
311 unsigned long v_offset;
313 h_vm_pgoff = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
314 v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT;
316 * Is this VMA fully outside the truncation point?
318 if (h_vm_pgoff >= h_pgoff)
321 unmap_hugepage_range(vma,
322 vma->vm_start + v_offset, vma->vm_end);
327 * Expanding truncates are not allowed.
329 static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
332 struct address_space *mapping = inode->i_mapping;
334 if (offset > inode->i_size)
337 BUG_ON(offset & ~HPAGE_MASK);
338 pgoff = offset >> HPAGE_SHIFT;
340 inode->i_size = offset;
341 spin_lock(&mapping->i_mmap_lock);
342 if (!prio_tree_empty(&mapping->i_mmap))
343 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
344 spin_unlock(&mapping->i_mmap_lock);
345 truncate_hugepages(mapping, offset);
349 static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
351 struct inode *inode = dentry->d_inode;
353 unsigned int ia_valid = attr->ia_valid;
357 error = inode_change_ok(inode, attr);
361 if (ia_valid & ATTR_SIZE) {
363 if (!(attr->ia_size & ~HPAGE_MASK))
364 error = hugetlb_vmtruncate(inode, attr->ia_size);
367 attr->ia_valid &= ~ATTR_SIZE;
369 error = inode_setattr(inode, attr);
374 static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
375 gid_t gid, int mode, dev_t dev)
378 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
380 if (sbinfo->free_inodes >= 0) {
381 spin_lock(&sbinfo->stat_lock);
382 if (!sbinfo->free_inodes) {
383 spin_unlock(&sbinfo->stat_lock);
386 sbinfo->free_inodes--;
387 spin_unlock(&sbinfo->stat_lock);
390 inode = new_inode(sb);
392 struct hugetlbfs_inode_info *info;
393 inode->i_mode = mode;
396 inode->i_blksize = HPAGE_SIZE;
398 inode->i_mapping->a_ops = &hugetlbfs_aops;
399 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
400 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
401 info = HUGETLBFS_I(inode);
402 mpol_shared_policy_init(&info->policy);
403 switch (mode & S_IFMT) {
405 init_special_inode(inode, mode, dev);
408 inode->i_op = &hugetlbfs_inode_operations;
409 inode->i_fop = &hugetlbfs_file_operations;
412 inode->i_op = &hugetlbfs_dir_inode_operations;
413 inode->i_fop = &simple_dir_operations;
415 /* directory inodes start off with i_nlink == 2 (for "." entry) */
419 inode->i_op = &page_symlink_inode_operations;
427 * File creation. Allocate an inode, and we're done..
429 static int hugetlbfs_mknod(struct inode *dir,
430 struct dentry *dentry, int mode, dev_t dev)
436 if (dir->i_mode & S_ISGID) {
441 gid = current->fsgid;
443 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
445 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
446 d_instantiate(dentry, inode);
447 dget(dentry); /* Extra count - pin the dentry in core */
453 static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
455 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
461 static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
463 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
466 static int hugetlbfs_symlink(struct inode *dir,
467 struct dentry *dentry, const char *symname)
473 if (dir->i_mode & S_ISGID)
476 gid = current->fsgid;
478 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
479 gid, S_IFLNK|S_IRWXUGO, 0);
481 int l = strlen(symname)+1;
482 error = page_symlink(inode, symname, l);
484 d_instantiate(dentry, inode);
489 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
495 * For direct-IO reads into hugetlb pages
497 static int hugetlbfs_set_page_dirty(struct page *page)
502 static int hugetlbfs_statfs(struct super_block *sb, struct kstatfs *buf)
504 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
506 buf->f_type = HUGETLBFS_MAGIC;
507 buf->f_bsize = HPAGE_SIZE;
509 spin_lock(&sbinfo->stat_lock);
510 buf->f_blocks = sbinfo->max_blocks;
511 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
512 buf->f_files = sbinfo->max_inodes;
513 buf->f_ffree = sbinfo->free_inodes;
514 spin_unlock(&sbinfo->stat_lock);
516 buf->f_namelen = NAME_MAX;
520 static void hugetlbfs_put_super(struct super_block *sb)
522 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
525 sb->s_fs_info = NULL;
530 static kmem_cache_t *hugetlbfs_inode_cachep;
532 static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
534 struct hugetlbfs_inode_info *p;
536 p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
539 return &p->vfs_inode;
542 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
544 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
546 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
547 SLAB_CTOR_CONSTRUCTOR)
548 inode_init_once(&ei->vfs_inode);
551 static void hugetlbfs_destroy_inode(struct inode *inode)
553 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
554 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
557 static struct address_space_operations hugetlbfs_aops = {
558 .readpage = hugetlbfs_readpage,
559 .prepare_write = hugetlbfs_prepare_write,
560 .commit_write = hugetlbfs_commit_write,
561 .set_page_dirty = hugetlbfs_set_page_dirty,
564 struct file_operations hugetlbfs_file_operations = {
565 .mmap = hugetlbfs_file_mmap,
566 .fsync = simple_sync_file,
567 .get_unmapped_area = hugetlb_get_unmapped_area,
570 static struct inode_operations hugetlbfs_dir_inode_operations = {
571 .create = hugetlbfs_create,
572 .lookup = simple_lookup,
574 .unlink = simple_unlink,
575 .symlink = hugetlbfs_symlink,
576 .mkdir = hugetlbfs_mkdir,
577 .rmdir = simple_rmdir,
578 .mknod = hugetlbfs_mknod,
579 .rename = simple_rename,
580 .setattr = hugetlbfs_setattr,
583 static struct inode_operations hugetlbfs_inode_operations = {
584 .setattr = hugetlbfs_setattr,
587 static struct super_operations hugetlbfs_ops = {
588 .alloc_inode = hugetlbfs_alloc_inode,
589 .destroy_inode = hugetlbfs_destroy_inode,
590 .statfs = hugetlbfs_statfs,
591 .drop_inode = hugetlbfs_drop_inode,
592 .put_super = hugetlbfs_put_super,
596 hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
598 char *opt, *value, *rest;
602 while ((opt = strsep(&options, ",")) != NULL) {
606 value = strchr(opt, '=');
607 if (!value || !*value)
612 if (!strcmp(opt, "uid"))
613 pconfig->uid = simple_strtoul(value, &value, 0);
614 else if (!strcmp(opt, "gid"))
615 pconfig->gid = simple_strtoul(value, &value, 0);
616 else if (!strcmp(opt, "mode"))
617 pconfig->mode = simple_strtoul(value,&value,0) & 0777U;
618 else if (!strcmp(opt, "size")) {
619 unsigned long long size = memparse(value, &rest);
621 size <<= HPAGE_SHIFT;
622 size *= max_huge_pages;
627 pconfig->nr_blocks = (size >> HPAGE_SHIFT);
629 } else if (!strcmp(opt,"nr_inodes")) {
630 pconfig->nr_inodes = memparse(value, &rest);
642 hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
644 struct inode * inode;
645 struct dentry * root;
647 struct hugetlbfs_config config;
648 struct hugetlbfs_sb_info *sbinfo;
650 config.nr_blocks = -1; /* No limit on size by default */
651 config.nr_inodes = -1; /* No limit on number of inodes by default */
652 config.uid = current->fsuid;
653 config.gid = current->fsgid;
655 ret = hugetlbfs_parse_options(data, &config);
660 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
663 sb->s_fs_info = sbinfo;
664 spin_lock_init(&sbinfo->stat_lock);
665 sbinfo->max_blocks = config.nr_blocks;
666 sbinfo->free_blocks = config.nr_blocks;
667 sbinfo->max_inodes = config.nr_inodes;
668 sbinfo->free_inodes = config.nr_inodes;
669 sb->s_maxbytes = MAX_LFS_FILESIZE;
670 sb->s_blocksize = HPAGE_SIZE;
671 sb->s_blocksize_bits = HPAGE_SHIFT;
672 sb->s_magic = HUGETLBFS_MAGIC;
673 sb->s_op = &hugetlbfs_ops;
675 inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
676 S_IFDIR | config.mode, 0);
680 root = d_alloc_root(inode);
692 int hugetlb_get_quota(struct address_space *mapping)
695 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
697 if (sbinfo->free_blocks > -1) {
698 spin_lock(&sbinfo->stat_lock);
699 if (sbinfo->free_blocks > 0)
700 sbinfo->free_blocks--;
703 spin_unlock(&sbinfo->stat_lock);
709 void hugetlb_put_quota(struct address_space *mapping)
711 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
713 if (sbinfo->free_blocks > -1) {
714 spin_lock(&sbinfo->stat_lock);
715 sbinfo->free_blocks++;
716 spin_unlock(&sbinfo->stat_lock);
720 static struct super_block *hugetlbfs_get_sb(struct file_system_type *fs_type,
721 int flags, const char *dev_name, void *data)
723 return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super);
726 static struct file_system_type hugetlbfs_fs_type = {
728 .get_sb = hugetlbfs_get_sb,
729 .kill_sb = kill_litter_super,
732 static struct vfsmount *hugetlbfs_vfsmount;
735 * Return the next identifier for a shm file
737 static unsigned long hugetlbfs_counter(void)
739 static DEFINE_SPINLOCK(lock);
740 static unsigned long counter;
749 static int can_do_hugetlb_shm(void)
751 return likely(capable(CAP_IPC_LOCK) ||
752 in_group_p(sysctl_hugetlb_shm_group) ||
756 struct file *hugetlb_zero_setup(size_t size)
761 struct dentry *dentry, *root;
762 struct qstr quick_string;
765 if (!can_do_hugetlb_shm())
766 return ERR_PTR(-EPERM);
768 if (!is_hugepage_mem_enough(size))
769 return ERR_PTR(-ENOMEM);
771 if (!user_shm_lock(size, current->user))
772 return ERR_PTR(-ENOMEM);
774 root = hugetlbfs_vfsmount->mnt_root;
775 snprintf(buf, 16, "%lu", hugetlbfs_counter());
776 quick_string.name = buf;
777 quick_string.len = strlen(quick_string.name);
778 quick_string.hash = 0;
779 dentry = d_alloc(root, &quick_string);
784 file = get_empty_filp();
789 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
790 current->fsgid, S_IFREG | S_IRWXUGO, 0);
794 d_instantiate(dentry, inode);
795 inode->i_size = size;
797 file->f_vfsmnt = mntget(hugetlbfs_vfsmount);
798 file->f_dentry = dentry;
799 file->f_mapping = inode->i_mapping;
800 file->f_op = &hugetlbfs_file_operations;
801 file->f_mode = FMODE_WRITE | FMODE_READ;
809 user_shm_unlock(size, current->user);
810 return ERR_PTR(error);
813 static int __init init_hugetlbfs_fs(void)
816 struct vfsmount *vfsmount;
818 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
819 sizeof(struct hugetlbfs_inode_info),
820 0, 0, init_once, NULL);
821 if (hugetlbfs_inode_cachep == NULL)
824 error = register_filesystem(&hugetlbfs_fs_type);
828 vfsmount = kern_mount(&hugetlbfs_fs_type);
830 if (!IS_ERR(vfsmount)) {
831 hugetlbfs_vfsmount = vfsmount;
835 error = PTR_ERR(vfsmount);
839 kmem_cache_destroy(hugetlbfs_inode_cachep);
843 static void __exit exit_hugetlbfs_fs(void)
845 kmem_cache_destroy(hugetlbfs_inode_cachep);
846 unregister_filesystem(&hugetlbfs_fs_type);
849 module_init(init_hugetlbfs_fs)
850 module_exit(exit_hugetlbfs_fs)
852 MODULE_LICENSE("GPL");