hugetlb: fix size=4G parsing
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / hugetlbfs / inode.c
blobfe67514e3ec167aebd438b965a5d020529d7d1d3
1 /*
2 * hugetlbpage-backed filesystem. Based on ramfs.
4 * William Irwin, 2002
6 * Copyright (C) 2002 Linus Torvalds.
7 */
9 #include <linux/module.h>
10 #include <linux/thread_info.h>
11 #include <asm/current.h>
12 #include <linux/sched.h> /* remove ASAP */
13 #include <linux/fs.h>
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/capability.h>
22 #include <linux/backing-dev.h>
23 #include <linux/hugetlb.h>
24 #include <linux/pagevec.h>
25 #include <linux/quotaops.h>
26 #include <linux/slab.h>
27 #include <linux/dnotify.h>
28 #include <linux/statfs.h>
29 #include <linux/security.h>
31 #include <asm/uaccess.h>
33 /* some random number */
34 #define HUGETLBFS_MAGIC 0x958458f6
36 static struct super_operations hugetlbfs_ops;
37 static struct address_space_operations hugetlbfs_aops;
38 struct file_operations hugetlbfs_file_operations;
39 static struct inode_operations hugetlbfs_dir_inode_operations;
40 static struct inode_operations hugetlbfs_inode_operations;
42 static struct backing_dev_info hugetlbfs_backing_dev_info = {
43 .ra_pages = 0, /* No readahead */
44 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
47 int sysctl_hugetlb_shm_group;
49 static void huge_pagevec_release(struct pagevec *pvec)
51 int i;
53 for (i = 0; i < pagevec_count(pvec); ++i)
54 put_page(pvec->pages[i]);
56 pagevec_reinit(pvec);
60 * huge_pages_needed tries to determine the number of new huge pages that
61 * will be required to fully populate this VMA. This will be equal to
62 * the size of the VMA in huge pages minus the number of huge pages
63 * (covered by this VMA) that are found in the page cache.
65 * Result is in bytes to be compatible with is_hugepage_mem_enough()
67 static unsigned long
68 huge_pages_needed(struct address_space *mapping, struct vm_area_struct *vma)
70 int i;
71 struct pagevec pvec;
72 unsigned long start = vma->vm_start;
73 unsigned long end = vma->vm_end;
74 unsigned long hugepages = (end - start) >> HPAGE_SHIFT;
75 pgoff_t next = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
76 pgoff_t endpg = next + hugepages;
78 pagevec_init(&pvec, 0);
79 while (next < endpg) {
80 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE))
81 break;
82 for (i = 0; i < pagevec_count(&pvec); i++) {
83 struct page *page = pvec.pages[i];
84 if (page->index > next)
85 next = page->index;
86 if (page->index >= endpg)
87 break;
88 next++;
89 hugepages--;
91 huge_pagevec_release(&pvec);
93 return hugepages << HPAGE_SHIFT;
96 static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
98 struct inode *inode = file->f_dentry->d_inode;
99 struct address_space *mapping = inode->i_mapping;
100 unsigned long bytes;
101 loff_t len, vma_len;
102 int ret;
104 if (vma->vm_pgoff & (HPAGE_SIZE / PAGE_SIZE - 1))
105 return -EINVAL;
107 if (vma->vm_start & ~HPAGE_MASK)
108 return -EINVAL;
110 if (vma->vm_end & ~HPAGE_MASK)
111 return -EINVAL;
113 if (vma->vm_end - vma->vm_start < HPAGE_SIZE)
114 return -EINVAL;
116 bytes = huge_pages_needed(mapping, vma);
117 if (!is_hugepage_mem_enough(bytes))
118 return -ENOMEM;
120 vma_len = (loff_t)(vma->vm_end - vma->vm_start);
122 mutex_lock(&inode->i_mutex);
123 file_accessed(file);
124 vma->vm_flags |= VM_HUGETLB | VM_RESERVED;
125 vma->vm_ops = &hugetlb_vm_ops;
127 ret = -ENOMEM;
128 len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
129 if (!(vma->vm_flags & VM_WRITE) && len > inode->i_size)
130 goto out;
132 ret = 0;
133 hugetlb_prefault_arch_hook(vma->vm_mm);
134 if (inode->i_size < len)
135 inode->i_size = len;
136 out:
137 mutex_unlock(&inode->i_mutex);
139 return ret;
143 * Called under down_write(mmap_sem).
146 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
147 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
148 unsigned long len, unsigned long pgoff, unsigned long flags);
149 #else
150 static unsigned long
151 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
152 unsigned long len, unsigned long pgoff, unsigned long flags)
154 struct mm_struct *mm = current->mm;
155 struct vm_area_struct *vma;
156 unsigned long start_addr;
158 if (len & ~HPAGE_MASK)
159 return -EINVAL;
160 if (len > TASK_SIZE)
161 return -ENOMEM;
163 if (addr) {
164 addr = ALIGN(addr, HPAGE_SIZE);
165 vma = find_vma(mm, addr);
166 if (TASK_SIZE - len >= addr &&
167 (!vma || addr + len <= vma->vm_start))
168 return addr;
171 start_addr = mm->free_area_cache;
173 if (len <= mm->cached_hole_size)
174 start_addr = TASK_UNMAPPED_BASE;
176 full_search:
177 addr = ALIGN(start_addr, HPAGE_SIZE);
179 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
180 /* At this point: (!vma || addr < vma->vm_end). */
181 if (TASK_SIZE - len < addr) {
183 * Start a new search - just in case we missed
184 * some holes.
186 if (start_addr != TASK_UNMAPPED_BASE) {
187 start_addr = TASK_UNMAPPED_BASE;
188 goto full_search;
190 return -ENOMEM;
193 if (!vma || addr + len <= vma->vm_start)
194 return addr;
195 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
198 #endif
201 * Read a page. Again trivial. If it didn't already exist
202 * in the page cache, it is zero-filled.
204 static int hugetlbfs_readpage(struct file *file, struct page * page)
206 unlock_page(page);
207 return -EINVAL;
210 static int hugetlbfs_prepare_write(struct file *file,
211 struct page *page, unsigned offset, unsigned to)
213 return -EINVAL;
216 static int hugetlbfs_commit_write(struct file *file,
217 struct page *page, unsigned offset, unsigned to)
219 return -EINVAL;
222 static void truncate_huge_page(struct page *page)
224 clear_page_dirty(page);
225 ClearPageUptodate(page);
226 remove_from_page_cache(page);
227 put_page(page);
230 static void truncate_hugepages(struct address_space *mapping, loff_t lstart)
232 const pgoff_t start = lstart >> HPAGE_SHIFT;
233 struct pagevec pvec;
234 pgoff_t next;
235 int i;
237 pagevec_init(&pvec, 0);
238 next = start;
239 while (1) {
240 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
241 if (next == start)
242 break;
243 next = start;
244 continue;
247 for (i = 0; i < pagevec_count(&pvec); ++i) {
248 struct page *page = pvec.pages[i];
250 lock_page(page);
251 if (page->index > next)
252 next = page->index;
253 ++next;
254 truncate_huge_page(page);
255 unlock_page(page);
256 hugetlb_put_quota(mapping);
258 huge_pagevec_release(&pvec);
260 BUG_ON(!lstart && mapping->nrpages);
263 static void hugetlbfs_delete_inode(struct inode *inode)
265 if (inode->i_data.nrpages)
266 truncate_hugepages(&inode->i_data, 0);
267 clear_inode(inode);
270 static void hugetlbfs_forget_inode(struct inode *inode)
272 struct super_block *sb = inode->i_sb;
274 if (!hlist_unhashed(&inode->i_hash)) {
275 if (!(inode->i_state & (I_DIRTY|I_LOCK)))
276 list_move(&inode->i_list, &inode_unused);
277 inodes_stat.nr_unused++;
278 if (!sb || (sb->s_flags & MS_ACTIVE)) {
279 spin_unlock(&inode_lock);
280 return;
282 inode->i_state |= I_WILL_FREE;
283 spin_unlock(&inode_lock);
285 * write_inode_now is a noop as we set BDI_CAP_NO_WRITEBACK
286 * in our backing_dev_info.
288 write_inode_now(inode, 1);
289 spin_lock(&inode_lock);
290 inode->i_state &= ~I_WILL_FREE;
291 inodes_stat.nr_unused--;
292 hlist_del_init(&inode->i_hash);
294 list_del_init(&inode->i_list);
295 list_del_init(&inode->i_sb_list);
296 inode->i_state |= I_FREEING;
297 inodes_stat.nr_inodes--;
298 spin_unlock(&inode_lock);
299 if (inode->i_data.nrpages)
300 truncate_hugepages(&inode->i_data, 0);
301 clear_inode(inode);
302 destroy_inode(inode);
305 static void hugetlbfs_drop_inode(struct inode *inode)
307 if (!inode->i_nlink)
308 generic_delete_inode(inode);
309 else
310 hugetlbfs_forget_inode(inode);
313 static inline void
314 hugetlb_vmtruncate_list(struct prio_tree_root *root, pgoff_t pgoff)
316 struct vm_area_struct *vma;
317 struct prio_tree_iter iter;
319 vma_prio_tree_foreach(vma, &iter, root, pgoff, ULONG_MAX) {
320 unsigned long v_offset;
323 * Can the expression below overflow on 32-bit arches?
324 * No, because the prio_tree returns us only those vmas
325 * which overlap the truncated area starting at pgoff,
326 * and no vma on a 32-bit arch can span beyond the 4GB.
328 if (vma->vm_pgoff < pgoff)
329 v_offset = (pgoff - vma->vm_pgoff) << PAGE_SHIFT;
330 else
331 v_offset = 0;
333 unmap_hugepage_range(vma,
334 vma->vm_start + v_offset, vma->vm_end);
339 * Expanding truncates are not allowed.
341 static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
343 pgoff_t pgoff;
344 struct address_space *mapping = inode->i_mapping;
346 if (offset > inode->i_size)
347 return -EINVAL;
349 BUG_ON(offset & ~HPAGE_MASK);
350 pgoff = offset >> PAGE_SHIFT;
352 inode->i_size = offset;
353 spin_lock(&mapping->i_mmap_lock);
354 if (!prio_tree_empty(&mapping->i_mmap))
355 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
356 spin_unlock(&mapping->i_mmap_lock);
357 truncate_hugepages(mapping, offset);
358 return 0;
361 static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
363 struct inode *inode = dentry->d_inode;
364 int error;
365 unsigned int ia_valid = attr->ia_valid;
367 BUG_ON(!inode);
369 error = inode_change_ok(inode, attr);
370 if (error)
371 goto out;
373 if (ia_valid & ATTR_SIZE) {
374 error = -EINVAL;
375 if (!(attr->ia_size & ~HPAGE_MASK))
376 error = hugetlb_vmtruncate(inode, attr->ia_size);
377 if (error)
378 goto out;
379 attr->ia_valid &= ~ATTR_SIZE;
381 error = inode_setattr(inode, attr);
382 out:
383 return error;
386 static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
387 gid_t gid, int mode, dev_t dev)
389 struct inode *inode;
391 inode = new_inode(sb);
392 if (inode) {
393 struct hugetlbfs_inode_info *info;
394 inode->i_mode = mode;
395 inode->i_uid = uid;
396 inode->i_gid = gid;
397 inode->i_blksize = HPAGE_SIZE;
398 inode->i_blocks = 0;
399 inode->i_mapping->a_ops = &hugetlbfs_aops;
400 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
401 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
402 info = HUGETLBFS_I(inode);
403 mpol_shared_policy_init(&info->policy, MPOL_DEFAULT, NULL);
404 switch (mode & S_IFMT) {
405 default:
406 init_special_inode(inode, mode, dev);
407 break;
408 case S_IFREG:
409 inode->i_op = &hugetlbfs_inode_operations;
410 inode->i_fop = &hugetlbfs_file_operations;
411 break;
412 case S_IFDIR:
413 inode->i_op = &hugetlbfs_dir_inode_operations;
414 inode->i_fop = &simple_dir_operations;
416 /* directory inodes start off with i_nlink == 2 (for "." entry) */
417 inode->i_nlink++;
418 break;
419 case S_IFLNK:
420 inode->i_op = &page_symlink_inode_operations;
421 break;
424 return inode;
428 * File creation. Allocate an inode, and we're done..
430 static int hugetlbfs_mknod(struct inode *dir,
431 struct dentry *dentry, int mode, dev_t dev)
433 struct inode *inode;
434 int error = -ENOSPC;
435 gid_t gid;
437 if (dir->i_mode & S_ISGID) {
438 gid = dir->i_gid;
439 if (S_ISDIR(mode))
440 mode |= S_ISGID;
441 } else {
442 gid = current->fsgid;
444 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
445 if (inode) {
446 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
447 d_instantiate(dentry, inode);
448 dget(dentry); /* Extra count - pin the dentry in core */
449 error = 0;
451 return error;
454 static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
456 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
457 if (!retval)
458 dir->i_nlink++;
459 return retval;
462 static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
464 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
467 static int hugetlbfs_symlink(struct inode *dir,
468 struct dentry *dentry, const char *symname)
470 struct inode *inode;
471 int error = -ENOSPC;
472 gid_t gid;
474 if (dir->i_mode & S_ISGID)
475 gid = dir->i_gid;
476 else
477 gid = current->fsgid;
479 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
480 gid, S_IFLNK|S_IRWXUGO, 0);
481 if (inode) {
482 int l = strlen(symname)+1;
483 error = page_symlink(inode, symname, l);
484 if (!error) {
485 d_instantiate(dentry, inode);
486 dget(dentry);
487 } else
488 iput(inode);
490 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
492 return error;
496 * For direct-IO reads into hugetlb pages
498 static int hugetlbfs_set_page_dirty(struct page *page)
500 return 0;
503 static int hugetlbfs_statfs(struct super_block *sb, struct kstatfs *buf)
505 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
507 buf->f_type = HUGETLBFS_MAGIC;
508 buf->f_bsize = HPAGE_SIZE;
509 if (sbinfo) {
510 spin_lock(&sbinfo->stat_lock);
511 /* If no limits set, just report 0 for max/free/used
512 * blocks, like simple_statfs() */
513 if (sbinfo->max_blocks >= 0) {
514 buf->f_blocks = sbinfo->max_blocks;
515 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
516 buf->f_files = sbinfo->max_inodes;
517 buf->f_ffree = sbinfo->free_inodes;
519 spin_unlock(&sbinfo->stat_lock);
521 buf->f_namelen = NAME_MAX;
522 return 0;
525 static void hugetlbfs_put_super(struct super_block *sb)
527 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
529 if (sbi) {
530 sb->s_fs_info = NULL;
531 kfree(sbi);
535 static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
537 if (sbinfo->free_inodes >= 0) {
538 spin_lock(&sbinfo->stat_lock);
539 if (unlikely(!sbinfo->free_inodes)) {
540 spin_unlock(&sbinfo->stat_lock);
541 return 0;
543 sbinfo->free_inodes--;
544 spin_unlock(&sbinfo->stat_lock);
547 return 1;
550 static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
552 if (sbinfo->free_inodes >= 0) {
553 spin_lock(&sbinfo->stat_lock);
554 sbinfo->free_inodes++;
555 spin_unlock(&sbinfo->stat_lock);
560 static kmem_cache_t *hugetlbfs_inode_cachep;
562 static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
564 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
565 struct hugetlbfs_inode_info *p;
567 if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
568 return NULL;
569 p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
570 if (unlikely(!p)) {
571 hugetlbfs_inc_free_inodes(sbinfo);
572 return NULL;
574 return &p->vfs_inode;
577 static void hugetlbfs_destroy_inode(struct inode *inode)
579 hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
580 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
581 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
584 static struct address_space_operations hugetlbfs_aops = {
585 .readpage = hugetlbfs_readpage,
586 .prepare_write = hugetlbfs_prepare_write,
587 .commit_write = hugetlbfs_commit_write,
588 .set_page_dirty = hugetlbfs_set_page_dirty,
592 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
594 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
596 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
597 SLAB_CTOR_CONSTRUCTOR)
598 inode_init_once(&ei->vfs_inode);
601 struct file_operations hugetlbfs_file_operations = {
602 .mmap = hugetlbfs_file_mmap,
603 .fsync = simple_sync_file,
604 .get_unmapped_area = hugetlb_get_unmapped_area,
607 static struct inode_operations hugetlbfs_dir_inode_operations = {
608 .create = hugetlbfs_create,
609 .lookup = simple_lookup,
610 .link = simple_link,
611 .unlink = simple_unlink,
612 .symlink = hugetlbfs_symlink,
613 .mkdir = hugetlbfs_mkdir,
614 .rmdir = simple_rmdir,
615 .mknod = hugetlbfs_mknod,
616 .rename = simple_rename,
617 .setattr = hugetlbfs_setattr,
620 static struct inode_operations hugetlbfs_inode_operations = {
621 .setattr = hugetlbfs_setattr,
624 static struct super_operations hugetlbfs_ops = {
625 .alloc_inode = hugetlbfs_alloc_inode,
626 .destroy_inode = hugetlbfs_destroy_inode,
627 .statfs = hugetlbfs_statfs,
628 .delete_inode = hugetlbfs_delete_inode,
629 .drop_inode = hugetlbfs_drop_inode,
630 .put_super = hugetlbfs_put_super,
633 static int
634 hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
636 char *opt, *value, *rest;
638 if (!options)
639 return 0;
640 while ((opt = strsep(&options, ",")) != NULL) {
641 if (!*opt)
642 continue;
644 value = strchr(opt, '=');
645 if (!value || !*value)
646 return -EINVAL;
647 else
648 *value++ = '\0';
650 if (!strcmp(opt, "uid"))
651 pconfig->uid = simple_strtoul(value, &value, 0);
652 else if (!strcmp(opt, "gid"))
653 pconfig->gid = simple_strtoul(value, &value, 0);
654 else if (!strcmp(opt, "mode"))
655 pconfig->mode = simple_strtoul(value,&value,0) & 0777U;
656 else if (!strcmp(opt, "size")) {
657 unsigned long long size = memparse(value, &rest);
658 if (*rest == '%') {
659 size <<= HPAGE_SHIFT;
660 size *= max_huge_pages;
661 do_div(size, 100);
662 rest++;
664 pconfig->nr_blocks = (size >> HPAGE_SHIFT);
665 value = rest;
666 } else if (!strcmp(opt,"nr_inodes")) {
667 pconfig->nr_inodes = memparse(value, &rest);
668 value = rest;
669 } else
670 return -EINVAL;
672 if (*value)
673 return -EINVAL;
675 return 0;
678 static int
679 hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
681 struct inode * inode;
682 struct dentry * root;
683 int ret;
684 struct hugetlbfs_config config;
685 struct hugetlbfs_sb_info *sbinfo;
687 config.nr_blocks = -1; /* No limit on size by default */
688 config.nr_inodes = -1; /* No limit on number of inodes by default */
689 config.uid = current->fsuid;
690 config.gid = current->fsgid;
691 config.mode = 0755;
692 ret = hugetlbfs_parse_options(data, &config);
694 if (ret)
695 return ret;
697 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
698 if (!sbinfo)
699 return -ENOMEM;
700 sb->s_fs_info = sbinfo;
701 spin_lock_init(&sbinfo->stat_lock);
702 sbinfo->max_blocks = config.nr_blocks;
703 sbinfo->free_blocks = config.nr_blocks;
704 sbinfo->max_inodes = config.nr_inodes;
705 sbinfo->free_inodes = config.nr_inodes;
706 sb->s_maxbytes = MAX_LFS_FILESIZE;
707 sb->s_blocksize = HPAGE_SIZE;
708 sb->s_blocksize_bits = HPAGE_SHIFT;
709 sb->s_magic = HUGETLBFS_MAGIC;
710 sb->s_op = &hugetlbfs_ops;
711 sb->s_time_gran = 1;
712 inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
713 S_IFDIR | config.mode, 0);
714 if (!inode)
715 goto out_free;
717 root = d_alloc_root(inode);
718 if (!root) {
719 iput(inode);
720 goto out_free;
722 sb->s_root = root;
723 return 0;
724 out_free:
725 kfree(sbinfo);
726 return -ENOMEM;
729 int hugetlb_get_quota(struct address_space *mapping)
731 int ret = 0;
732 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
734 if (sbinfo->free_blocks > -1) {
735 spin_lock(&sbinfo->stat_lock);
736 if (sbinfo->free_blocks > 0)
737 sbinfo->free_blocks--;
738 else
739 ret = -ENOMEM;
740 spin_unlock(&sbinfo->stat_lock);
743 return ret;
746 void hugetlb_put_quota(struct address_space *mapping)
748 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
750 if (sbinfo->free_blocks > -1) {
751 spin_lock(&sbinfo->stat_lock);
752 sbinfo->free_blocks++;
753 spin_unlock(&sbinfo->stat_lock);
757 static struct super_block *hugetlbfs_get_sb(struct file_system_type *fs_type,
758 int flags, const char *dev_name, void *data)
760 return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super);
763 static struct file_system_type hugetlbfs_fs_type = {
764 .name = "hugetlbfs",
765 .get_sb = hugetlbfs_get_sb,
766 .kill_sb = kill_litter_super,
769 static struct vfsmount *hugetlbfs_vfsmount;
772 * Return the next identifier for a shm file
774 static unsigned long hugetlbfs_counter(void)
776 static DEFINE_SPINLOCK(lock);
777 static unsigned long counter;
778 unsigned long ret;
780 spin_lock(&lock);
781 ret = ++counter;
782 spin_unlock(&lock);
783 return ret;
786 static int can_do_hugetlb_shm(void)
788 return likely(capable(CAP_IPC_LOCK) ||
789 in_group_p(sysctl_hugetlb_shm_group) ||
790 can_do_mlock());
793 struct file *hugetlb_zero_setup(size_t size)
795 int error = -ENOMEM;
796 struct file *file;
797 struct inode *inode;
798 struct dentry *dentry, *root;
799 struct qstr quick_string;
800 char buf[16];
802 if (!can_do_hugetlb_shm())
803 return ERR_PTR(-EPERM);
805 if (!is_hugepage_mem_enough(size))
806 return ERR_PTR(-ENOMEM);
808 if (!user_shm_lock(size, current->user))
809 return ERR_PTR(-ENOMEM);
811 root = hugetlbfs_vfsmount->mnt_root;
812 snprintf(buf, 16, "%lu", hugetlbfs_counter());
813 quick_string.name = buf;
814 quick_string.len = strlen(quick_string.name);
815 quick_string.hash = 0;
816 dentry = d_alloc(root, &quick_string);
817 if (!dentry)
818 goto out_shm_unlock;
820 error = -ENFILE;
821 file = get_empty_filp();
822 if (!file)
823 goto out_dentry;
825 error = -ENOSPC;
826 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
827 current->fsgid, S_IFREG | S_IRWXUGO, 0);
828 if (!inode)
829 goto out_file;
831 d_instantiate(dentry, inode);
832 inode->i_size = size;
833 inode->i_nlink = 0;
834 file->f_vfsmnt = mntget(hugetlbfs_vfsmount);
835 file->f_dentry = dentry;
836 file->f_mapping = inode->i_mapping;
837 file->f_op = &hugetlbfs_file_operations;
838 file->f_mode = FMODE_WRITE | FMODE_READ;
839 return file;
841 out_file:
842 put_filp(file);
843 out_dentry:
844 dput(dentry);
845 out_shm_unlock:
846 user_shm_unlock(size, current->user);
847 return ERR_PTR(error);
850 static int __init init_hugetlbfs_fs(void)
852 int error;
853 struct vfsmount *vfsmount;
855 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
856 sizeof(struct hugetlbfs_inode_info),
857 0, 0, init_once, NULL);
858 if (hugetlbfs_inode_cachep == NULL)
859 return -ENOMEM;
861 error = register_filesystem(&hugetlbfs_fs_type);
862 if (error)
863 goto out;
865 vfsmount = kern_mount(&hugetlbfs_fs_type);
867 if (!IS_ERR(vfsmount)) {
868 hugetlbfs_vfsmount = vfsmount;
869 return 0;
872 error = PTR_ERR(vfsmount);
874 out:
875 if (error)
876 kmem_cache_destroy(hugetlbfs_inode_cachep);
877 return error;
880 static void __exit exit_hugetlbfs_fs(void)
882 kmem_cache_destroy(hugetlbfs_inode_cachep);
883 unregister_filesystem(&hugetlbfs_fs_type);
886 module_init(init_hugetlbfs_fs)
887 module_exit(exit_hugetlbfs_fs)
889 MODULE_LICENSE("GPL");