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 .memory_backed = 1, /* Does not contribute to dirty memory */
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_pgoff & (HPAGE_SIZE / PAGE_SIZE - 1))
58 if (vma->vm_start & ~HPAGE_MASK)
61 if (vma->vm_end & ~HPAGE_MASK)
64 if (vma->vm_end - vma->vm_start < HPAGE_SIZE)
67 vma_len = (loff_t)(vma->vm_end - vma->vm_start);
71 vma->vm_flags |= VM_HUGETLB | VM_RESERVED;
72 vma->vm_ops = &hugetlb_vm_ops;
73 ret = hugetlb_prefault(mapping, vma);
74 len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
75 if (ret == 0 && inode->i_size < len)
83 * Called under down_write(mmap_sem), page_table_lock is not held
86 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
87 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
88 unsigned long len, unsigned long pgoff, unsigned long flags);
91 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
92 unsigned long len, unsigned long pgoff, unsigned long flags)
94 struct mm_struct *mm = current->mm;
95 struct vm_area_struct *vma;
97 if (len & ~HPAGE_MASK)
103 addr = ALIGN(addr, HPAGE_SIZE);
104 vma = find_vma(mm, addr);
105 if (TASK_SIZE - len >= addr &&
106 (!vma || addr + len <= vma->vm_start))
110 addr = ALIGN(mm->free_area_cache, HPAGE_SIZE);
112 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
113 /* At this point: (!vma || addr < vma->vm_end). */
114 if (TASK_SIZE - len < addr)
116 if (!vma || addr + len <= vma->vm_start)
118 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
124 * Read a page. Again trivial. If it didn't already exist
125 * in the page cache, it is zero-filled.
127 static int hugetlbfs_readpage(struct file *file, struct page * page)
133 static int hugetlbfs_prepare_write(struct file *file,
134 struct page *page, unsigned offset, unsigned to)
139 static int hugetlbfs_commit_write(struct file *file,
140 struct page *page, unsigned offset, unsigned to)
145 void huge_pagevec_release(struct pagevec *pvec)
149 for (i = 0; i < pagevec_count(pvec); ++i)
150 put_page(pvec->pages[i]);
152 pagevec_reinit(pvec);
155 void truncate_huge_page(struct page *page)
157 clear_page_dirty(page);
158 ClearPageUptodate(page);
159 remove_from_page_cache(page);
163 void truncate_hugepages(struct address_space *mapping, loff_t lstart)
165 const pgoff_t start = lstart >> HPAGE_SHIFT;
170 pagevec_init(&pvec, 0);
173 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
180 for (i = 0; i < pagevec_count(&pvec); ++i) {
181 struct page *page = pvec.pages[i];
184 if (page->index > next)
187 truncate_huge_page(page);
189 hugetlb_put_quota(mapping);
191 huge_pagevec_release(&pvec);
193 BUG_ON(!lstart && mapping->nrpages);
196 static void hugetlbfs_delete_inode(struct inode *inode)
198 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(inode->i_sb);
200 hlist_del_init(&inode->i_hash);
201 list_del_init(&inode->i_list);
202 inode->i_state |= I_FREEING;
203 inodes_stat.nr_inodes--;
204 spin_unlock(&inode_lock);
206 if (inode->i_data.nrpages)
207 truncate_hugepages(&inode->i_data, 0);
209 security_inode_delete(inode);
211 if (sbinfo->free_inodes >= 0) {
212 spin_lock(&sbinfo->stat_lock);
213 sbinfo->free_inodes++;
214 spin_unlock(&sbinfo->stat_lock);
218 destroy_inode(inode);
221 static void hugetlbfs_forget_inode(struct inode *inode)
223 struct super_block *super_block = inode->i_sb;
224 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(super_block);
226 if (hlist_unhashed(&inode->i_hash))
229 if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
230 list_del(&inode->i_list);
231 list_add(&inode->i_list, &inode_unused);
233 inodes_stat.nr_unused++;
234 if (!super_block || (super_block->s_flags & MS_ACTIVE)) {
235 spin_unlock(&inode_lock);
239 /* write_inode_now() ? */
240 inodes_stat.nr_unused--;
241 hlist_del_init(&inode->i_hash);
243 list_del_init(&inode->i_list);
244 inode->i_state |= I_FREEING;
245 inodes_stat.nr_inodes--;
246 spin_unlock(&inode_lock);
247 if (inode->i_data.nrpages)
248 truncate_hugepages(&inode->i_data, 0);
250 if (sbinfo->free_inodes >= 0) {
251 spin_lock(&sbinfo->stat_lock);
252 sbinfo->free_inodes++;
253 spin_unlock(&sbinfo->stat_lock);
257 destroy_inode(inode);
260 static void hugetlbfs_drop_inode(struct inode *inode)
263 hugetlbfs_delete_inode(inode);
265 hugetlbfs_forget_inode(inode);
269 * h_pgoff is in HPAGE_SIZE units.
270 * vma->vm_pgoff is in PAGE_SIZE units.
273 hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
275 struct vm_area_struct *vma = NULL;
276 struct prio_tree_iter iter;
278 while ((vma = vma_prio_tree_next(vma, root, &iter,
279 h_pgoff, ULONG_MAX)) != NULL) {
280 unsigned long h_vm_pgoff;
281 unsigned long v_length;
282 unsigned long v_offset;
284 h_vm_pgoff = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
285 v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT;
287 * Is this VMA fully outside the truncation point?
289 if (h_vm_pgoff >= h_pgoff)
292 v_length = vma->vm_end - vma->vm_start;
294 zap_hugepage_range(vma,
295 vma->vm_start + v_offset,
296 v_length - v_offset);
301 * Expanding truncates are not allowed.
303 static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
306 struct address_space *mapping = inode->i_mapping;
308 if (offset > inode->i_size)
311 BUG_ON(offset & ~HPAGE_MASK);
312 pgoff = offset >> HPAGE_SHIFT;
314 inode->i_size = offset;
315 spin_lock(&mapping->i_mmap_lock);
316 if (!prio_tree_empty(&mapping->i_mmap))
317 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
318 spin_unlock(&mapping->i_mmap_lock);
319 truncate_hugepages(mapping, offset);
323 static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
325 struct inode *inode = dentry->d_inode;
327 unsigned int ia_valid = attr->ia_valid;
331 error = inode_change_ok(inode, attr);
335 if (ia_valid & ATTR_SIZE) {
337 if (!(attr->ia_size & ~HPAGE_MASK))
338 error = hugetlb_vmtruncate(inode, attr->ia_size);
341 attr->ia_valid &= ~ATTR_SIZE;
343 error = inode_setattr(inode, attr);
348 static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
349 gid_t gid, int mode, dev_t dev)
352 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
354 if (sbinfo->free_inodes >= 0) {
355 spin_lock(&sbinfo->stat_lock);
356 if (!sbinfo->free_inodes) {
357 spin_unlock(&sbinfo->stat_lock);
360 sbinfo->free_inodes--;
361 spin_unlock(&sbinfo->stat_lock);
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 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 kmem_cache_t *hugetlbfs_inode_cachep;
506 static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
508 struct hugetlbfs_inode_info *p;
510 p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
513 return &p->vfs_inode;
516 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
518 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
520 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
521 SLAB_CTOR_CONSTRUCTOR)
522 inode_init_once(&ei->vfs_inode);
525 static void hugetlbfs_destroy_inode(struct inode *inode)
527 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
528 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
531 static struct address_space_operations hugetlbfs_aops = {
532 .readpage = hugetlbfs_readpage,
533 .prepare_write = hugetlbfs_prepare_write,
534 .commit_write = hugetlbfs_commit_write,
535 .set_page_dirty = hugetlbfs_set_page_dirty,
538 struct file_operations hugetlbfs_file_operations = {
539 .mmap = hugetlbfs_file_mmap,
540 .fsync = simple_sync_file,
541 .get_unmapped_area = hugetlb_get_unmapped_area,
544 static struct inode_operations hugetlbfs_dir_inode_operations = {
545 .create = hugetlbfs_create,
546 .lookup = simple_lookup,
548 .unlink = simple_unlink,
549 .symlink = hugetlbfs_symlink,
550 .mkdir = hugetlbfs_mkdir,
551 .rmdir = simple_rmdir,
552 .mknod = hugetlbfs_mknod,
553 .rename = simple_rename,
554 .setattr = hugetlbfs_setattr,
557 static struct inode_operations hugetlbfs_inode_operations = {
558 .setattr = hugetlbfs_setattr,
561 static struct super_operations hugetlbfs_ops = {
562 .alloc_inode = hugetlbfs_alloc_inode,
563 .destroy_inode = hugetlbfs_destroy_inode,
564 .statfs = hugetlbfs_statfs,
565 .drop_inode = hugetlbfs_drop_inode,
566 .put_super = hugetlbfs_put_super,
570 hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
572 char *opt, *value, *rest;
576 while ((opt = strsep(&options, ",")) != NULL) {
580 value = strchr(opt, '=');
581 if (!value || !*value)
586 if (!strcmp(opt, "uid"))
587 pconfig->uid = simple_strtoul(value, &value, 0);
588 else if (!strcmp(opt, "gid"))
589 pconfig->gid = simple_strtoul(value, &value, 0);
590 else if (!strcmp(opt, "mode"))
591 pconfig->mode = simple_strtoul(value,&value,0) & 0777U;
592 else if (!strcmp(opt, "size")) {
593 unsigned long long size = memparse(value, &rest);
595 size <<= HPAGE_SHIFT;
596 size *= max_huge_pages;
601 pconfig->nr_blocks = (size >> HPAGE_SHIFT);
603 } else if (!strcmp(opt,"nr_inodes")) {
604 pconfig->nr_inodes = memparse(value, &rest);
616 hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
618 struct inode * inode;
619 struct dentry * root;
621 struct hugetlbfs_config config;
622 struct hugetlbfs_sb_info *sbinfo;
624 config.nr_blocks = -1; /* No limit on size by default */
625 config.nr_inodes = -1; /* No limit on number of inodes by default */
626 config.uid = current->fsuid;
627 config.gid = current->fsgid;
629 ret = hugetlbfs_parse_options(data, &config);
634 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
637 sb->s_fs_info = sbinfo;
638 spin_lock_init(&sbinfo->stat_lock);
639 sbinfo->max_blocks = config.nr_blocks;
640 sbinfo->free_blocks = config.nr_blocks;
641 sbinfo->max_inodes = config.nr_inodes;
642 sbinfo->free_inodes = config.nr_inodes;
643 sb->s_blocksize = HPAGE_SIZE;
644 sb->s_blocksize_bits = HPAGE_SHIFT;
645 sb->s_magic = HUGETLBFS_MAGIC;
646 sb->s_op = &hugetlbfs_ops;
647 inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
648 S_IFDIR | config.mode, 0);
652 root = d_alloc_root(inode);
664 int hugetlb_get_quota(struct address_space *mapping)
667 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
669 if (sbinfo->free_blocks > -1) {
670 spin_lock(&sbinfo->stat_lock);
671 if (sbinfo->free_blocks > 0)
672 sbinfo->free_blocks--;
675 spin_unlock(&sbinfo->stat_lock);
681 void hugetlb_put_quota(struct address_space *mapping)
683 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
685 if (sbinfo->free_blocks > -1) {
686 spin_lock(&sbinfo->stat_lock);
687 sbinfo->free_blocks++;
688 spin_unlock(&sbinfo->stat_lock);
692 static struct super_block *hugetlbfs_get_sb(struct file_system_type *fs_type,
693 int flags, const char *dev_name, void *data)
695 return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super);
698 static struct file_system_type hugetlbfs_fs_type = {
700 .get_sb = hugetlbfs_get_sb,
701 .kill_sb = kill_litter_super,
704 static struct vfsmount *hugetlbfs_vfsmount;
707 * Return the next identifier for a shm file
709 static unsigned long hugetlbfs_counter(void)
711 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
712 static unsigned long counter;
721 static int can_do_hugetlb_shm(void)
723 return likely(capable(CAP_IPC_LOCK) ||
724 in_group_p(sysctl_hugetlb_shm_group));
727 struct file *hugetlb_zero_setup(size_t size)
732 struct dentry *dentry, *root;
733 struct qstr quick_string;
736 if (!can_do_hugetlb_shm())
737 return ERR_PTR(-EPERM);
739 if (!is_hugepage_mem_enough(size))
740 return ERR_PTR(-ENOMEM);
742 root = hugetlbfs_vfsmount->mnt_root;
743 snprintf(buf, 16, "%lu", hugetlbfs_counter());
744 quick_string.name = buf;
745 quick_string.len = strlen(quick_string.name);
746 quick_string.hash = 0;
747 dentry = d_alloc(root, &quick_string);
749 return ERR_PTR(-ENOMEM);
752 file = get_empty_filp();
757 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
758 current->fsgid, S_IFREG | S_IRWXUGO, 0);
762 d_instantiate(dentry, inode);
763 inode->i_size = size;
765 file->f_vfsmnt = mntget(hugetlbfs_vfsmount);
766 file->f_dentry = dentry;
767 file->f_mapping = inode->i_mapping;
768 file->f_op = &hugetlbfs_file_operations;
769 file->f_mode = FMODE_WRITE | FMODE_READ;
776 return ERR_PTR(error);
779 static int __init init_hugetlbfs_fs(void)
782 struct vfsmount *vfsmount;
784 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
785 sizeof(struct hugetlbfs_inode_info),
786 0, SLAB_RECLAIM_ACCOUNT,
788 if (hugetlbfs_inode_cachep == NULL)
791 error = register_filesystem(&hugetlbfs_fs_type);
795 vfsmount = kern_mount(&hugetlbfs_fs_type);
797 if (!IS_ERR(vfsmount)) {
798 hugetlbfs_vfsmount = vfsmount;
802 error = PTR_ERR(vfsmount);
806 kmem_cache_destroy(hugetlbfs_inode_cachep);
810 static void __exit exit_hugetlbfs_fs(void)
812 kmem_cache_destroy(hugetlbfs_inode_cachep);
813 unregister_filesystem(&hugetlbfs_fs_type);
816 module_init(init_hugetlbfs_fs)
817 module_exit(exit_hugetlbfs_fs)
819 MODULE_LICENSE("GPL");