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 static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
48 struct inode *inode = file->f_dentry->d_inode;
49 struct address_space *mapping = inode->i_mapping;
53 if (vma->vm_start & ~HPAGE_MASK)
56 if (vma->vm_end & ~HPAGE_MASK)
59 if (vma->vm_end - vma->vm_start < HPAGE_SIZE)
62 vma_len = (loff_t)(vma->vm_end - vma->vm_start);
66 vma->vm_flags |= VM_HUGETLB | VM_RESERVED;
67 vma->vm_ops = &hugetlb_vm_ops;
68 ret = hugetlb_prefault(mapping, vma);
69 len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
70 if (ret == 0 && inode->i_size < len)
78 * Called under down_write(mmap_sem), page_table_lock is not held
81 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
82 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
83 unsigned long len, unsigned long pgoff, unsigned long flags);
86 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
87 unsigned long len, unsigned long pgoff, unsigned long flags)
89 struct mm_struct *mm = current->mm;
90 struct vm_area_struct *vma;
92 if (len & ~HPAGE_MASK)
98 addr = ALIGN(addr, HPAGE_SIZE);
99 vma = find_vma(mm, addr);
100 if (TASK_SIZE - len >= addr &&
101 (!vma || addr + len <= vma->vm_start))
105 addr = ALIGN(mm->free_area_cache, HPAGE_SIZE);
107 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
108 /* At this point: (!vma || addr < vma->vm_end). */
109 if (TASK_SIZE - len < addr)
111 if (!vma || addr + len <= vma->vm_start)
113 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
119 * Read a page. Again trivial. If it didn't already exist
120 * in the page cache, it is zero-filled.
122 static int hugetlbfs_readpage(struct file *file, struct page * page)
128 static int hugetlbfs_prepare_write(struct file *file,
129 struct page *page, unsigned offset, unsigned to)
134 static int hugetlbfs_commit_write(struct file *file,
135 struct page *page, unsigned offset, unsigned to)
140 void huge_pagevec_release(struct pagevec *pvec)
144 for (i = 0; i < pagevec_count(pvec); ++i)
145 put_page(pvec->pages[i]);
147 pagevec_reinit(pvec);
150 void truncate_huge_page(struct page *page)
152 clear_page_dirty(page);
153 ClearPageUptodate(page);
154 remove_from_page_cache(page);
158 void truncate_hugepages(struct address_space *mapping, loff_t lstart)
160 const pgoff_t start = lstart >> HPAGE_SHIFT;
165 pagevec_init(&pvec, 0);
168 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
175 for (i = 0; i < pagevec_count(&pvec); ++i) {
176 struct page *page = pvec.pages[i];
179 if (page->index > next)
182 truncate_huge_page(page);
184 hugetlb_put_quota(mapping);
186 huge_pagevec_release(&pvec);
188 BUG_ON(!lstart && mapping->nrpages);
191 static void hugetlbfs_delete_inode(struct inode *inode)
193 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(inode->i_sb);
195 hlist_del_init(&inode->i_hash);
196 list_del_init(&inode->i_list);
197 inode->i_state |= I_FREEING;
198 inodes_stat.nr_inodes--;
199 spin_unlock(&inode_lock);
201 if (inode->i_data.nrpages)
202 truncate_hugepages(&inode->i_data, 0);
204 security_inode_delete(inode);
206 if (sbinfo->free_inodes >= 0) {
207 spin_lock(&sbinfo->stat_lock);
208 sbinfo->free_inodes++;
209 spin_unlock(&sbinfo->stat_lock);
213 destroy_inode(inode);
216 static void hugetlbfs_forget_inode(struct inode *inode)
218 struct super_block *super_block = inode->i_sb;
219 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(super_block);
221 if (hlist_unhashed(&inode->i_hash))
224 if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
225 list_del(&inode->i_list);
226 list_add(&inode->i_list, &inode_unused);
228 inodes_stat.nr_unused++;
229 if (!super_block || (super_block->s_flags & MS_ACTIVE)) {
230 spin_unlock(&inode_lock);
234 /* write_inode_now() ? */
235 inodes_stat.nr_unused--;
236 hlist_del_init(&inode->i_hash);
238 list_del_init(&inode->i_list);
239 inode->i_state |= I_FREEING;
240 inodes_stat.nr_inodes--;
241 spin_unlock(&inode_lock);
242 if (inode->i_data.nrpages)
243 truncate_hugepages(&inode->i_data, 0);
245 if (sbinfo->free_inodes >= 0) {
246 spin_lock(&sbinfo->stat_lock);
247 sbinfo->free_inodes++;
248 spin_unlock(&sbinfo->stat_lock);
252 destroy_inode(inode);
255 static void hugetlbfs_drop_inode(struct inode *inode)
258 hugetlbfs_delete_inode(inode);
260 hugetlbfs_forget_inode(inode);
264 * h_pgoff is in HPAGE_SIZE units.
265 * vma->vm_pgoff is in PAGE_SIZE units.
268 hugetlb_vmtruncate_list(struct list_head *list, unsigned long h_pgoff)
270 struct vm_area_struct *vma;
272 list_for_each_entry(vma, list, shared) {
273 unsigned long h_vm_pgoff;
274 unsigned long v_length;
275 unsigned long h_length;
276 unsigned long v_offset;
278 h_vm_pgoff = vma->vm_pgoff << (HPAGE_SHIFT - PAGE_SHIFT);
279 v_length = vma->vm_end - vma->vm_start;
280 h_length = v_length >> HPAGE_SHIFT;
281 v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT;
284 * Is this VMA fully outside the truncation point?
286 if (h_vm_pgoff >= h_pgoff) {
287 zap_hugepage_range(vma, vma->vm_start, v_length);
292 * Is this VMA fully inside the truncaton point?
294 if (h_vm_pgoff + (v_length >> HPAGE_SHIFT) <= h_pgoff)
298 * The VMA straddles the truncation point. v_offset is the
299 * offset (in bytes) into the VMA where the point lies.
301 zap_hugepage_range(vma,
302 vma->vm_start + v_offset,
303 v_length - v_offset);
308 * Expanding truncates are not allowed.
310 static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
313 struct address_space *mapping = inode->i_mapping;
315 if (offset > inode->i_size)
318 BUG_ON(offset & ~HPAGE_MASK);
319 pgoff = offset >> HPAGE_SHIFT;
321 inode->i_size = offset;
322 down(&mapping->i_shared_sem);
323 if (!list_empty(&mapping->i_mmap))
324 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
325 if (!list_empty(&mapping->i_mmap_shared))
326 hugetlb_vmtruncate_list(&mapping->i_mmap_shared, pgoff);
327 up(&mapping->i_shared_sem);
328 truncate_hugepages(mapping, offset);
332 static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
334 struct inode *inode = dentry->d_inode;
336 unsigned int ia_valid = attr->ia_valid;
340 error = inode_change_ok(inode, attr);
344 error = security_inode_setattr(dentry, attr);
347 if (ia_valid & ATTR_SIZE) {
349 if (!(attr->ia_size & ~HPAGE_MASK))
350 error = hugetlb_vmtruncate(inode, attr->ia_size);
353 attr->ia_valid &= ~ATTR_SIZE;
355 error = inode_setattr(inode, attr);
360 static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
361 gid_t gid, int mode, dev_t dev)
364 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
366 if (sbinfo->free_inodes >= 0) {
367 spin_lock(&sbinfo->stat_lock);
368 if (!sbinfo->free_inodes) {
369 spin_unlock(&sbinfo->stat_lock);
372 sbinfo->free_inodes--;
373 spin_unlock(&sbinfo->stat_lock);
376 inode = new_inode(sb);
378 inode->i_mode = mode;
381 inode->i_blksize = HPAGE_SIZE;
383 inode->i_mapping->a_ops = &hugetlbfs_aops;
384 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
385 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
386 switch (mode & S_IFMT) {
388 init_special_inode(inode, mode, dev);
391 inode->i_op = &hugetlbfs_inode_operations;
392 inode->i_fop = &hugetlbfs_file_operations;
395 inode->i_op = &hugetlbfs_dir_inode_operations;
396 inode->i_fop = &simple_dir_operations;
398 /* directory inodes start off with i_nlink == 2 (for "." entry) */
402 inode->i_op = &page_symlink_inode_operations;
410 * File creation. Allocate an inode, and we're done..
412 static int hugetlbfs_mknod(struct inode *dir,
413 struct dentry *dentry, int mode, dev_t dev)
419 if (dir->i_mode & S_ISGID) {
424 gid = current->fsgid;
426 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
428 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
429 d_instantiate(dentry, inode);
430 dget(dentry); /* Extra count - pin the dentry in core */
436 static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
438 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
444 static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
446 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
449 static int hugetlbfs_symlink(struct inode *dir,
450 struct dentry *dentry, const char *symname)
456 if (dir->i_mode & S_ISGID)
459 gid = current->fsgid;
461 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
462 gid, S_IFLNK|S_IRWXUGO, 0);
464 int l = strlen(symname)+1;
465 error = page_symlink(inode, symname, l);
467 d_instantiate(dentry, inode);
472 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
478 * For direct-IO reads into hugetlb pages
480 int hugetlbfs_set_page_dirty(struct page *page)
485 static int hugetlbfs_statfs(struct super_block *sb, struct kstatfs *buf)
487 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
489 buf->f_type = HUGETLBFS_MAGIC;
490 buf->f_bsize = HPAGE_SIZE;
492 spin_lock(&sbinfo->stat_lock);
493 buf->f_blocks = sbinfo->max_blocks;
494 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
495 buf->f_files = sbinfo->max_inodes;
496 buf->f_ffree = sbinfo->free_inodes;
497 spin_unlock(&sbinfo->stat_lock);
499 buf->f_namelen = NAME_MAX;
503 static void hugetlbfs_put_super(struct super_block *sb)
505 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
508 sb->s_fs_info = NULL;
513 static struct address_space_operations hugetlbfs_aops = {
514 .readpage = hugetlbfs_readpage,
515 .prepare_write = hugetlbfs_prepare_write,
516 .commit_write = hugetlbfs_commit_write,
517 .set_page_dirty = hugetlbfs_set_page_dirty,
520 struct file_operations hugetlbfs_file_operations = {
521 .mmap = hugetlbfs_file_mmap,
522 .fsync = simple_sync_file,
523 .get_unmapped_area = hugetlb_get_unmapped_area,
526 static struct inode_operations hugetlbfs_dir_inode_operations = {
527 .create = hugetlbfs_create,
528 .lookup = simple_lookup,
530 .unlink = simple_unlink,
531 .symlink = hugetlbfs_symlink,
532 .mkdir = hugetlbfs_mkdir,
533 .rmdir = simple_rmdir,
534 .mknod = hugetlbfs_mknod,
535 .rename = simple_rename,
536 .setattr = hugetlbfs_setattr,
539 static struct inode_operations hugetlbfs_inode_operations = {
540 .setattr = hugetlbfs_setattr,
543 static struct super_operations hugetlbfs_ops = {
544 .statfs = hugetlbfs_statfs,
545 .drop_inode = hugetlbfs_drop_inode,
546 .put_super = hugetlbfs_put_super,
550 hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
552 char *opt, *value, *rest;
556 while ((opt = strsep(&options, ",")) != NULL) {
560 value = strchr(opt, '=');
561 if (!value || !*value)
566 if (!strcmp(opt, "uid"))
567 pconfig->uid = simple_strtoul(value, &value, 0);
568 else if (!strcmp(opt, "gid"))
569 pconfig->gid = simple_strtoul(value, &value, 0);
570 else if (!strcmp(opt, "mode"))
571 pconfig->mode = simple_strtoul(value,&value,0) & 0777U;
572 else if (!strcmp(opt, "size")) {
573 unsigned long long size = memparse(value, &rest);
575 size <<= HPAGE_SHIFT;
576 size *= max_huge_pages;
581 pconfig->nr_blocks = (size >> HPAGE_SHIFT);
583 } else if (!strcmp(opt,"nr_inodes")) {
584 pconfig->nr_inodes = memparse(value, &rest);
596 hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
598 struct inode * inode;
599 struct dentry * root;
601 struct hugetlbfs_config config;
602 struct hugetlbfs_sb_info *sbinfo;
604 config.nr_blocks = -1; /* No limit on size by default */
605 config.nr_inodes = -1; /* No limit on number of inodes by default */
606 config.uid = current->fsuid;
607 config.gid = current->fsgid;
609 ret = hugetlbfs_parse_options(data, &config);
614 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
617 sb->s_fs_info = sbinfo;
618 spin_lock_init(&sbinfo->stat_lock);
619 sbinfo->max_blocks = config.nr_blocks;
620 sbinfo->free_blocks = config.nr_blocks;
621 sbinfo->max_inodes = config.nr_inodes;
622 sbinfo->free_inodes = config.nr_inodes;
623 sb->s_blocksize = HPAGE_SIZE;
624 sb->s_blocksize_bits = HPAGE_SHIFT;
625 sb->s_magic = HUGETLBFS_MAGIC;
626 sb->s_op = &hugetlbfs_ops;
627 inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
628 S_IFDIR | config.mode, 0);
632 root = d_alloc_root(inode);
644 int hugetlb_get_quota(struct address_space *mapping)
647 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
649 if (sbinfo->free_blocks > -1) {
650 spin_lock(&sbinfo->stat_lock);
651 if (sbinfo->free_blocks > 0)
652 sbinfo->free_blocks--;
655 spin_unlock(&sbinfo->stat_lock);
661 void hugetlb_put_quota(struct address_space *mapping)
663 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
665 if (sbinfo->free_blocks > -1) {
666 spin_lock(&sbinfo->stat_lock);
667 sbinfo->free_blocks++;
668 spin_unlock(&sbinfo->stat_lock);
672 static struct super_block *hugetlbfs_get_sb(struct file_system_type *fs_type,
673 int flags, const char *dev_name, void *data)
675 return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super);
678 static struct file_system_type hugetlbfs_fs_type = {
680 .get_sb = hugetlbfs_get_sb,
681 .kill_sb = kill_litter_super,
684 static struct vfsmount *hugetlbfs_vfsmount;
687 * Return the next identifier for a shm file
689 static unsigned long hugetlbfs_counter(void)
691 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
692 static unsigned long counter;
701 struct file *hugetlb_zero_setup(size_t size)
706 struct dentry *dentry, *root;
707 struct qstr quick_string;
710 if (!capable(CAP_IPC_LOCK))
711 return ERR_PTR(-EPERM);
713 if (!is_hugepage_mem_enough(size))
714 return ERR_PTR(-ENOMEM);
716 root = hugetlbfs_vfsmount->mnt_root;
717 snprintf(buf, 16, "%lu", hugetlbfs_counter());
718 quick_string.name = buf;
719 quick_string.len = strlen(quick_string.name);
720 quick_string.hash = 0;
721 dentry = d_alloc(root, &quick_string);
723 return ERR_PTR(-ENOMEM);
726 file = get_empty_filp();
731 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
732 current->fsgid, S_IFREG | S_IRWXUGO, 0);
736 d_instantiate(dentry, inode);
737 inode->i_size = size;
739 file->f_vfsmnt = mntget(hugetlbfs_vfsmount);
740 file->f_dentry = dentry;
741 file->f_mapping = inode->i_mapping;
742 file->f_op = &hugetlbfs_file_operations;
743 file->f_mode = FMODE_WRITE | FMODE_READ;
750 return ERR_PTR(error);
753 static int __init init_hugetlbfs_fs(void)
756 struct vfsmount *vfsmount;
758 error = register_filesystem(&hugetlbfs_fs_type);
762 vfsmount = kern_mount(&hugetlbfs_fs_type);
764 if (!IS_ERR(vfsmount)) {
765 hugetlbfs_vfsmount = vfsmount;
769 error = PTR_ERR(vfsmount);
773 static void __exit exit_hugetlbfs_fs(void)
775 unregister_filesystem(&hugetlbfs_fs_type);
778 module_init(init_hugetlbfs_fs)
779 module_exit(exit_hugetlbfs_fs)
781 MODULE_LICENSE("GPL");