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 if (inode->i_data.nrpages)
228 truncate_hugepages(&inode->i_data, 0);
232 static void hugetlbfs_forget_inode(struct inode *inode)
234 struct super_block *sb = inode->i_sb;
236 if (!hlist_unhashed(&inode->i_hash)) {
237 if (!(inode->i_state & (I_DIRTY|I_LOCK)))
238 list_move(&inode->i_list, &inode_unused);
239 inodes_stat.nr_unused++;
240 if (!sb || (sb->s_flags & MS_ACTIVE)) {
241 spin_unlock(&inode_lock);
244 inode->i_state |= I_WILL_FREE;
245 spin_unlock(&inode_lock);
247 * write_inode_now is a noop as we set BDI_CAP_NO_WRITEBACK
248 * in our backing_dev_info.
250 write_inode_now(inode, 1);
251 spin_lock(&inode_lock);
252 inode->i_state &= ~I_WILL_FREE;
253 inodes_stat.nr_unused--;
254 hlist_del_init(&inode->i_hash);
256 list_del_init(&inode->i_list);
257 list_del_init(&inode->i_sb_list);
258 inode->i_state |= I_FREEING;
259 inodes_stat.nr_inodes--;
260 spin_unlock(&inode_lock);
261 if (inode->i_data.nrpages)
262 truncate_hugepages(&inode->i_data, 0);
264 destroy_inode(inode);
267 static void hugetlbfs_drop_inode(struct inode *inode)
270 generic_delete_inode(inode);
272 hugetlbfs_forget_inode(inode);
276 * h_pgoff is in HPAGE_SIZE units.
277 * vma->vm_pgoff is in PAGE_SIZE units.
280 hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
282 struct vm_area_struct *vma;
283 struct prio_tree_iter iter;
285 vma_prio_tree_foreach(vma, &iter, root, h_pgoff, ULONG_MAX) {
286 unsigned long h_vm_pgoff;
287 unsigned long v_offset;
289 h_vm_pgoff = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
290 v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT;
292 * Is this VMA fully outside the truncation point?
294 if (h_vm_pgoff >= h_pgoff)
297 unmap_hugepage_range(vma,
298 vma->vm_start + v_offset, vma->vm_end);
303 * Expanding truncates are not allowed.
305 static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
308 struct address_space *mapping = inode->i_mapping;
310 if (offset > inode->i_size)
313 BUG_ON(offset & ~HPAGE_MASK);
314 pgoff = offset >> HPAGE_SHIFT;
316 inode->i_size = offset;
317 spin_lock(&mapping->i_mmap_lock);
318 if (!prio_tree_empty(&mapping->i_mmap))
319 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
320 spin_unlock(&mapping->i_mmap_lock);
321 truncate_hugepages(mapping, offset);
325 static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
327 struct inode *inode = dentry->d_inode;
329 unsigned int ia_valid = attr->ia_valid;
333 error = inode_change_ok(inode, attr);
337 if (ia_valid & ATTR_SIZE) {
339 if (!(attr->ia_size & ~HPAGE_MASK))
340 error = hugetlb_vmtruncate(inode, attr->ia_size);
343 attr->ia_valid &= ~ATTR_SIZE;
345 error = inode_setattr(inode, attr);
350 static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
351 gid_t gid, int mode, dev_t dev)
355 inode = new_inode(sb);
357 struct hugetlbfs_inode_info *info;
358 inode->i_mode = mode;
361 inode->i_blksize = HPAGE_SIZE;
363 inode->i_mapping->a_ops = &hugetlbfs_aops;
364 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
365 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
366 info = HUGETLBFS_I(inode);
367 mpol_shared_policy_init(&info->policy);
368 switch (mode & S_IFMT) {
370 init_special_inode(inode, mode, dev);
373 inode->i_op = &hugetlbfs_inode_operations;
374 inode->i_fop = &hugetlbfs_file_operations;
377 inode->i_op = &hugetlbfs_dir_inode_operations;
378 inode->i_fop = &simple_dir_operations;
380 /* directory inodes start off with i_nlink == 2 (for "." entry) */
384 inode->i_op = &page_symlink_inode_operations;
392 * File creation. Allocate an inode, and we're done..
394 static int hugetlbfs_mknod(struct inode *dir,
395 struct dentry *dentry, int mode, dev_t dev)
401 if (dir->i_mode & S_ISGID) {
406 gid = current->fsgid;
408 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
410 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
411 d_instantiate(dentry, inode);
412 dget(dentry); /* Extra count - pin the dentry in core */
418 static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
420 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
426 static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
428 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
431 static int hugetlbfs_symlink(struct inode *dir,
432 struct dentry *dentry, const char *symname)
438 if (dir->i_mode & S_ISGID)
441 gid = current->fsgid;
443 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
444 gid, S_IFLNK|S_IRWXUGO, 0);
446 int l = strlen(symname)+1;
447 error = page_symlink(inode, symname, l);
449 d_instantiate(dentry, inode);
454 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
460 * For direct-IO reads into hugetlb pages
462 static int hugetlbfs_set_page_dirty(struct page *page)
467 static int hugetlbfs_statfs(struct super_block *sb, struct kstatfs *buf)
469 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
471 buf->f_type = HUGETLBFS_MAGIC;
472 buf->f_bsize = HPAGE_SIZE;
474 spin_lock(&sbinfo->stat_lock);
475 buf->f_blocks = sbinfo->max_blocks;
476 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
477 buf->f_files = sbinfo->max_inodes;
478 buf->f_ffree = sbinfo->free_inodes;
479 spin_unlock(&sbinfo->stat_lock);
481 buf->f_namelen = NAME_MAX;
485 static void hugetlbfs_put_super(struct super_block *sb)
487 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
490 sb->s_fs_info = NULL;
495 static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
497 if (sbinfo->free_inodes >= 0) {
498 spin_lock(&sbinfo->stat_lock);
499 if (unlikely(!sbinfo->free_inodes)) {
500 spin_unlock(&sbinfo->stat_lock);
503 sbinfo->free_inodes--;
504 spin_unlock(&sbinfo->stat_lock);
510 static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
512 if (sbinfo->free_inodes >= 0) {
513 spin_lock(&sbinfo->stat_lock);
514 sbinfo->free_inodes++;
515 spin_unlock(&sbinfo->stat_lock);
520 static kmem_cache_t *hugetlbfs_inode_cachep;
522 static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
524 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
525 struct hugetlbfs_inode_info *p;
527 if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
529 p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
531 hugetlbfs_inc_free_inodes(sbinfo);
534 return &p->vfs_inode;
537 static void hugetlbfs_destroy_inode(struct inode *inode)
539 hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
540 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
541 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
544 static struct address_space_operations hugetlbfs_aops = {
545 .readpage = hugetlbfs_readpage,
546 .prepare_write = hugetlbfs_prepare_write,
547 .commit_write = hugetlbfs_commit_write,
548 .set_page_dirty = hugetlbfs_set_page_dirty,
552 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
554 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
556 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
557 SLAB_CTOR_CONSTRUCTOR)
558 inode_init_once(&ei->vfs_inode);
561 struct file_operations hugetlbfs_file_operations = {
562 .mmap = hugetlbfs_file_mmap,
563 .fsync = simple_sync_file,
564 .get_unmapped_area = hugetlb_get_unmapped_area,
567 static struct inode_operations hugetlbfs_dir_inode_operations = {
568 .create = hugetlbfs_create,
569 .lookup = simple_lookup,
571 .unlink = simple_unlink,
572 .symlink = hugetlbfs_symlink,
573 .mkdir = hugetlbfs_mkdir,
574 .rmdir = simple_rmdir,
575 .mknod = hugetlbfs_mknod,
576 .rename = simple_rename,
577 .setattr = hugetlbfs_setattr,
580 static struct inode_operations hugetlbfs_inode_operations = {
581 .setattr = hugetlbfs_setattr,
584 static struct super_operations hugetlbfs_ops = {
585 .alloc_inode = hugetlbfs_alloc_inode,
586 .destroy_inode = hugetlbfs_destroy_inode,
587 .statfs = hugetlbfs_statfs,
588 .delete_inode = hugetlbfs_delete_inode,
589 .drop_inode = hugetlbfs_drop_inode,
590 .put_super = hugetlbfs_put_super,
594 hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
596 char *opt, *value, *rest;
600 while ((opt = strsep(&options, ",")) != NULL) {
604 value = strchr(opt, '=');
605 if (!value || !*value)
610 if (!strcmp(opt, "uid"))
611 pconfig->uid = simple_strtoul(value, &value, 0);
612 else if (!strcmp(opt, "gid"))
613 pconfig->gid = simple_strtoul(value, &value, 0);
614 else if (!strcmp(opt, "mode"))
615 pconfig->mode = simple_strtoul(value,&value,0) & 0777U;
616 else if (!strcmp(opt, "size")) {
617 unsigned long long size = memparse(value, &rest);
619 size <<= HPAGE_SHIFT;
620 size *= max_huge_pages;
625 pconfig->nr_blocks = (size >> HPAGE_SHIFT);
627 } else if (!strcmp(opt,"nr_inodes")) {
628 pconfig->nr_inodes = memparse(value, &rest);
640 hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
642 struct inode * inode;
643 struct dentry * root;
645 struct hugetlbfs_config config;
646 struct hugetlbfs_sb_info *sbinfo;
648 config.nr_blocks = -1; /* No limit on size by default */
649 config.nr_inodes = -1; /* No limit on number of inodes by default */
650 config.uid = current->fsuid;
651 config.gid = current->fsgid;
653 ret = hugetlbfs_parse_options(data, &config);
658 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
661 sb->s_fs_info = sbinfo;
662 spin_lock_init(&sbinfo->stat_lock);
663 sbinfo->max_blocks = config.nr_blocks;
664 sbinfo->free_blocks = config.nr_blocks;
665 sbinfo->max_inodes = config.nr_inodes;
666 sbinfo->free_inodes = config.nr_inodes;
667 sb->s_maxbytes = MAX_LFS_FILESIZE;
668 sb->s_blocksize = HPAGE_SIZE;
669 sb->s_blocksize_bits = HPAGE_SHIFT;
670 sb->s_magic = HUGETLBFS_MAGIC;
671 sb->s_op = &hugetlbfs_ops;
673 inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
674 S_IFDIR | config.mode, 0);
678 root = d_alloc_root(inode);
690 int hugetlb_get_quota(struct address_space *mapping)
693 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
695 if (sbinfo->free_blocks > -1) {
696 spin_lock(&sbinfo->stat_lock);
697 if (sbinfo->free_blocks > 0)
698 sbinfo->free_blocks--;
701 spin_unlock(&sbinfo->stat_lock);
707 void hugetlb_put_quota(struct address_space *mapping)
709 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
711 if (sbinfo->free_blocks > -1) {
712 spin_lock(&sbinfo->stat_lock);
713 sbinfo->free_blocks++;
714 spin_unlock(&sbinfo->stat_lock);
718 static struct super_block *hugetlbfs_get_sb(struct file_system_type *fs_type,
719 int flags, const char *dev_name, void *data)
721 return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super);
724 static struct file_system_type hugetlbfs_fs_type = {
726 .get_sb = hugetlbfs_get_sb,
727 .kill_sb = kill_litter_super,
730 static struct vfsmount *hugetlbfs_vfsmount;
733 * Return the next identifier for a shm file
735 static unsigned long hugetlbfs_counter(void)
737 static DEFINE_SPINLOCK(lock);
738 static unsigned long counter;
747 static int can_do_hugetlb_shm(void)
749 return likely(capable(CAP_IPC_LOCK) ||
750 in_group_p(sysctl_hugetlb_shm_group) ||
754 struct file *hugetlb_zero_setup(size_t size)
759 struct dentry *dentry, *root;
760 struct qstr quick_string;
763 if (!can_do_hugetlb_shm())
764 return ERR_PTR(-EPERM);
766 if (!is_hugepage_mem_enough(size))
767 return ERR_PTR(-ENOMEM);
769 if (!user_shm_lock(size, current->user))
770 return ERR_PTR(-ENOMEM);
772 root = hugetlbfs_vfsmount->mnt_root;
773 snprintf(buf, 16, "%lu", hugetlbfs_counter());
774 quick_string.name = buf;
775 quick_string.len = strlen(quick_string.name);
776 quick_string.hash = 0;
777 dentry = d_alloc(root, &quick_string);
782 file = get_empty_filp();
787 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
788 current->fsgid, S_IFREG | S_IRWXUGO, 0);
792 d_instantiate(dentry, inode);
793 inode->i_size = size;
795 file->f_vfsmnt = mntget(hugetlbfs_vfsmount);
796 file->f_dentry = dentry;
797 file->f_mapping = inode->i_mapping;
798 file->f_op = &hugetlbfs_file_operations;
799 file->f_mode = FMODE_WRITE | FMODE_READ;
807 user_shm_unlock(size, current->user);
808 return ERR_PTR(error);
811 static int __init init_hugetlbfs_fs(void)
814 struct vfsmount *vfsmount;
816 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
817 sizeof(struct hugetlbfs_inode_info),
818 0, 0, init_once, NULL);
819 if (hugetlbfs_inode_cachep == NULL)
822 error = register_filesystem(&hugetlbfs_fs_type);
826 vfsmount = kern_mount(&hugetlbfs_fs_type);
828 if (!IS_ERR(vfsmount)) {
829 hugetlbfs_vfsmount = vfsmount;
833 error = PTR_ERR(vfsmount);
837 kmem_cache_destroy(hugetlbfs_inode_cachep);
841 static void __exit exit_hugetlbfs_fs(void)
843 kmem_cache_destroy(hugetlbfs_inode_cachep);
844 unregister_filesystem(&hugetlbfs_fs_type);
847 module_init(init_hugetlbfs_fs)
848 module_exit(exit_hugetlbfs_fs)
850 MODULE_LICENSE("GPL");