[PATCH] hugetlb: demand fault handler
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / hugetlbfs / inode.c
blob2627efe767cf485257f381530ab8234cfb7a218d
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/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 loff_t len, vma_len;
52 int ret;
54 if ((vma->vm_flags & (VM_MAYSHARE | VM_WRITE)) == VM_WRITE)
55 return -EINVAL;
57 if (vma->vm_pgoff & (HPAGE_SIZE / PAGE_SIZE - 1))
58 return -EINVAL;
60 if (vma->vm_start & ~HPAGE_MASK)
61 return -EINVAL;
63 if (vma->vm_end & ~HPAGE_MASK)
64 return -EINVAL;
66 if (vma->vm_end - vma->vm_start < HPAGE_SIZE)
67 return -EINVAL;
69 vma_len = (loff_t)(vma->vm_end - vma->vm_start);
71 down(&inode->i_sem);
72 file_accessed(file);
73 vma->vm_flags |= VM_HUGETLB | VM_RESERVED;
74 vma->vm_ops = &hugetlb_vm_ops;
76 ret = -ENOMEM;
77 len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
78 if (!(vma->vm_flags & VM_WRITE) && len > inode->i_size)
79 goto out;
81 ret = 0;
82 hugetlb_prefault_arch_hook(vma->vm_mm);
83 if (inode->i_size < len)
84 inode->i_size = len;
85 out:
86 up(&inode->i_sem);
88 return ret;
92 * Called under down_write(mmap_sem).
95 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
96 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
97 unsigned long len, unsigned long pgoff, unsigned long flags);
98 #else
99 static unsigned long
100 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
101 unsigned long len, unsigned long pgoff, unsigned long flags)
103 struct mm_struct *mm = current->mm;
104 struct vm_area_struct *vma;
105 unsigned long start_addr;
107 if (len & ~HPAGE_MASK)
108 return -EINVAL;
109 if (len > TASK_SIZE)
110 return -ENOMEM;
112 if (addr) {
113 addr = ALIGN(addr, HPAGE_SIZE);
114 vma = find_vma(mm, addr);
115 if (TASK_SIZE - len >= addr &&
116 (!vma || addr + len <= vma->vm_start))
117 return addr;
120 start_addr = mm->free_area_cache;
122 if (len <= mm->cached_hole_size)
123 start_addr = TASK_UNMAPPED_BASE;
125 full_search:
126 addr = ALIGN(start_addr, HPAGE_SIZE);
128 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
129 /* At this point: (!vma || addr < vma->vm_end). */
130 if (TASK_SIZE - len < addr) {
132 * Start a new search - just in case we missed
133 * some holes.
135 if (start_addr != TASK_UNMAPPED_BASE) {
136 start_addr = TASK_UNMAPPED_BASE;
137 goto full_search;
139 return -ENOMEM;
142 if (!vma || addr + len <= vma->vm_start)
143 return addr;
144 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
147 #endif
150 * Read a page. Again trivial. If it didn't already exist
151 * in the page cache, it is zero-filled.
153 static int hugetlbfs_readpage(struct file *file, struct page * page)
155 unlock_page(page);
156 return -EINVAL;
159 static int hugetlbfs_prepare_write(struct file *file,
160 struct page *page, unsigned offset, unsigned to)
162 return -EINVAL;
165 static int hugetlbfs_commit_write(struct file *file,
166 struct page *page, unsigned offset, unsigned to)
168 return -EINVAL;
171 static void huge_pagevec_release(struct pagevec *pvec)
173 int i;
175 for (i = 0; i < pagevec_count(pvec); ++i)
176 put_page(pvec->pages[i]);
178 pagevec_reinit(pvec);
181 static void truncate_huge_page(struct page *page)
183 clear_page_dirty(page);
184 ClearPageUptodate(page);
185 remove_from_page_cache(page);
186 put_page(page);
189 static void truncate_hugepages(struct address_space *mapping, loff_t lstart)
191 const pgoff_t start = lstart >> HPAGE_SHIFT;
192 struct pagevec pvec;
193 pgoff_t next;
194 int i;
196 pagevec_init(&pvec, 0);
197 next = start;
198 while (1) {
199 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
200 if (next == start)
201 break;
202 next = start;
203 continue;
206 for (i = 0; i < pagevec_count(&pvec); ++i) {
207 struct page *page = pvec.pages[i];
209 lock_page(page);
210 if (page->index > next)
211 next = page->index;
212 ++next;
213 truncate_huge_page(page);
214 unlock_page(page);
215 hugetlb_put_quota(mapping);
217 huge_pagevec_release(&pvec);
219 BUG_ON(!lstart && mapping->nrpages);
222 static void hugetlbfs_delete_inode(struct inode *inode)
224 if (inode->i_data.nrpages)
225 truncate_hugepages(&inode->i_data, 0);
226 clear_inode(inode);
229 static void hugetlbfs_forget_inode(struct inode *inode)
231 struct super_block *sb = inode->i_sb;
233 if (!hlist_unhashed(&inode->i_hash)) {
234 if (!(inode->i_state & (I_DIRTY|I_LOCK)))
235 list_move(&inode->i_list, &inode_unused);
236 inodes_stat.nr_unused++;
237 if (!sb || (sb->s_flags & MS_ACTIVE)) {
238 spin_unlock(&inode_lock);
239 return;
241 inode->i_state |= I_WILL_FREE;
242 spin_unlock(&inode_lock);
244 * write_inode_now is a noop as we set BDI_CAP_NO_WRITEBACK
245 * in our backing_dev_info.
247 write_inode_now(inode, 1);
248 spin_lock(&inode_lock);
249 inode->i_state &= ~I_WILL_FREE;
250 inodes_stat.nr_unused--;
251 hlist_del_init(&inode->i_hash);
253 list_del_init(&inode->i_list);
254 list_del_init(&inode->i_sb_list);
255 inode->i_state |= I_FREEING;
256 inodes_stat.nr_inodes--;
257 spin_unlock(&inode_lock);
258 if (inode->i_data.nrpages)
259 truncate_hugepages(&inode->i_data, 0);
260 clear_inode(inode);
261 destroy_inode(inode);
264 static void hugetlbfs_drop_inode(struct inode *inode)
266 if (!inode->i_nlink)
267 generic_delete_inode(inode);
268 else
269 hugetlbfs_forget_inode(inode);
273 * h_pgoff is in HPAGE_SIZE units.
274 * vma->vm_pgoff is in PAGE_SIZE units.
276 static inline void
277 hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
279 struct vm_area_struct *vma;
280 struct prio_tree_iter iter;
282 vma_prio_tree_foreach(vma, &iter, root, h_pgoff, ULONG_MAX) {
283 unsigned long h_vm_pgoff;
284 unsigned long v_offset;
286 h_vm_pgoff = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
287 v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT;
289 * Is this VMA fully outside the truncation point?
291 if (h_vm_pgoff >= h_pgoff)
292 v_offset = 0;
294 unmap_hugepage_range(vma,
295 vma->vm_start + v_offset, vma->vm_end);
300 * Expanding truncates are not allowed.
302 static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
304 unsigned long pgoff;
305 struct address_space *mapping = inode->i_mapping;
307 if (offset > inode->i_size)
308 return -EINVAL;
310 BUG_ON(offset & ~HPAGE_MASK);
311 pgoff = offset >> HPAGE_SHIFT;
313 inode->i_size = offset;
314 spin_lock(&mapping->i_mmap_lock);
315 if (!prio_tree_empty(&mapping->i_mmap))
316 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
317 spin_unlock(&mapping->i_mmap_lock);
318 truncate_hugepages(mapping, offset);
319 return 0;
322 static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
324 struct inode *inode = dentry->d_inode;
325 int error;
326 unsigned int ia_valid = attr->ia_valid;
328 BUG_ON(!inode);
330 error = inode_change_ok(inode, attr);
331 if (error)
332 goto out;
334 if (ia_valid & ATTR_SIZE) {
335 error = -EINVAL;
336 if (!(attr->ia_size & ~HPAGE_MASK))
337 error = hugetlb_vmtruncate(inode, attr->ia_size);
338 if (error)
339 goto out;
340 attr->ia_valid &= ~ATTR_SIZE;
342 error = inode_setattr(inode, attr);
343 out:
344 return error;
347 static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
348 gid_t gid, int mode, dev_t dev)
350 struct inode *inode;
352 inode = new_inode(sb);
353 if (inode) {
354 struct hugetlbfs_inode_info *info;
355 inode->i_mode = mode;
356 inode->i_uid = uid;
357 inode->i_gid = gid;
358 inode->i_blksize = HPAGE_SIZE;
359 inode->i_blocks = 0;
360 inode->i_mapping->a_ops = &hugetlbfs_aops;
361 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
362 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
363 info = HUGETLBFS_I(inode);
364 mpol_shared_policy_init(&info->policy);
365 switch (mode & S_IFMT) {
366 default:
367 init_special_inode(inode, mode, dev);
368 break;
369 case S_IFREG:
370 inode->i_op = &hugetlbfs_inode_operations;
371 inode->i_fop = &hugetlbfs_file_operations;
372 break;
373 case S_IFDIR:
374 inode->i_op = &hugetlbfs_dir_inode_operations;
375 inode->i_fop = &simple_dir_operations;
377 /* directory inodes start off with i_nlink == 2 (for "." entry) */
378 inode->i_nlink++;
379 break;
380 case S_IFLNK:
381 inode->i_op = &page_symlink_inode_operations;
382 break;
385 return inode;
389 * File creation. Allocate an inode, and we're done..
391 static int hugetlbfs_mknod(struct inode *dir,
392 struct dentry *dentry, int mode, dev_t dev)
394 struct inode *inode;
395 int error = -ENOSPC;
396 gid_t gid;
398 if (dir->i_mode & S_ISGID) {
399 gid = dir->i_gid;
400 if (S_ISDIR(mode))
401 mode |= S_ISGID;
402 } else {
403 gid = current->fsgid;
405 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
406 if (inode) {
407 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
408 d_instantiate(dentry, inode);
409 dget(dentry); /* Extra count - pin the dentry in core */
410 error = 0;
412 return error;
415 static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
417 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
418 if (!retval)
419 dir->i_nlink++;
420 return retval;
423 static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
425 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
428 static int hugetlbfs_symlink(struct inode *dir,
429 struct dentry *dentry, const char *symname)
431 struct inode *inode;
432 int error = -ENOSPC;
433 gid_t gid;
435 if (dir->i_mode & S_ISGID)
436 gid = dir->i_gid;
437 else
438 gid = current->fsgid;
440 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
441 gid, S_IFLNK|S_IRWXUGO, 0);
442 if (inode) {
443 int l = strlen(symname)+1;
444 error = page_symlink(inode, symname, l);
445 if (!error) {
446 d_instantiate(dentry, inode);
447 dget(dentry);
448 } else
449 iput(inode);
451 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
453 return error;
457 * For direct-IO reads into hugetlb pages
459 static int hugetlbfs_set_page_dirty(struct page *page)
461 return 0;
464 static int hugetlbfs_statfs(struct super_block *sb, struct kstatfs *buf)
466 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
468 buf->f_type = HUGETLBFS_MAGIC;
469 buf->f_bsize = HPAGE_SIZE;
470 if (sbinfo) {
471 spin_lock(&sbinfo->stat_lock);
472 buf->f_blocks = sbinfo->max_blocks;
473 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
474 buf->f_files = sbinfo->max_inodes;
475 buf->f_ffree = sbinfo->free_inodes;
476 spin_unlock(&sbinfo->stat_lock);
478 buf->f_namelen = NAME_MAX;
479 return 0;
482 static void hugetlbfs_put_super(struct super_block *sb)
484 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
486 if (sbi) {
487 sb->s_fs_info = NULL;
488 kfree(sbi);
492 static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
494 if (sbinfo->free_inodes >= 0) {
495 spin_lock(&sbinfo->stat_lock);
496 if (unlikely(!sbinfo->free_inodes)) {
497 spin_unlock(&sbinfo->stat_lock);
498 return 0;
500 sbinfo->free_inodes--;
501 spin_unlock(&sbinfo->stat_lock);
504 return 1;
507 static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
509 if (sbinfo->free_inodes >= 0) {
510 spin_lock(&sbinfo->stat_lock);
511 sbinfo->free_inodes++;
512 spin_unlock(&sbinfo->stat_lock);
517 static kmem_cache_t *hugetlbfs_inode_cachep;
519 static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
521 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
522 struct hugetlbfs_inode_info *p;
524 if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
525 return NULL;
526 p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
527 if (unlikely(!p)) {
528 hugetlbfs_inc_free_inodes(sbinfo);
529 return NULL;
531 return &p->vfs_inode;
534 static void hugetlbfs_destroy_inode(struct inode *inode)
536 hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
537 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
538 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
541 static struct address_space_operations hugetlbfs_aops = {
542 .readpage = hugetlbfs_readpage,
543 .prepare_write = hugetlbfs_prepare_write,
544 .commit_write = hugetlbfs_commit_write,
545 .set_page_dirty = hugetlbfs_set_page_dirty,
549 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
551 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
553 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
554 SLAB_CTOR_CONSTRUCTOR)
555 inode_init_once(&ei->vfs_inode);
558 struct file_operations hugetlbfs_file_operations = {
559 .mmap = hugetlbfs_file_mmap,
560 .fsync = simple_sync_file,
561 .get_unmapped_area = hugetlb_get_unmapped_area,
564 static struct inode_operations hugetlbfs_dir_inode_operations = {
565 .create = hugetlbfs_create,
566 .lookup = simple_lookup,
567 .link = simple_link,
568 .unlink = simple_unlink,
569 .symlink = hugetlbfs_symlink,
570 .mkdir = hugetlbfs_mkdir,
571 .rmdir = simple_rmdir,
572 .mknod = hugetlbfs_mknod,
573 .rename = simple_rename,
574 .setattr = hugetlbfs_setattr,
577 static struct inode_operations hugetlbfs_inode_operations = {
578 .setattr = hugetlbfs_setattr,
581 static struct super_operations hugetlbfs_ops = {
582 .alloc_inode = hugetlbfs_alloc_inode,
583 .destroy_inode = hugetlbfs_destroy_inode,
584 .statfs = hugetlbfs_statfs,
585 .delete_inode = hugetlbfs_delete_inode,
586 .drop_inode = hugetlbfs_drop_inode,
587 .put_super = hugetlbfs_put_super,
590 static int
591 hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
593 char *opt, *value, *rest;
595 if (!options)
596 return 0;
597 while ((opt = strsep(&options, ",")) != NULL) {
598 if (!*opt)
599 continue;
601 value = strchr(opt, '=');
602 if (!value || !*value)
603 return -EINVAL;
604 else
605 *value++ = '\0';
607 if (!strcmp(opt, "uid"))
608 pconfig->uid = simple_strtoul(value, &value, 0);
609 else if (!strcmp(opt, "gid"))
610 pconfig->gid = simple_strtoul(value, &value, 0);
611 else if (!strcmp(opt, "mode"))
612 pconfig->mode = simple_strtoul(value,&value,0) & 0777U;
613 else if (!strcmp(opt, "size")) {
614 unsigned long long size = memparse(value, &rest);
615 if (*rest == '%') {
616 size <<= HPAGE_SHIFT;
617 size *= max_huge_pages;
618 do_div(size, 100);
619 rest++;
621 size &= HPAGE_MASK;
622 pconfig->nr_blocks = (size >> HPAGE_SHIFT);
623 value = rest;
624 } else if (!strcmp(opt,"nr_inodes")) {
625 pconfig->nr_inodes = memparse(value, &rest);
626 value = rest;
627 } else
628 return -EINVAL;
630 if (*value)
631 return -EINVAL;
633 return 0;
636 static int
637 hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
639 struct inode * inode;
640 struct dentry * root;
641 int ret;
642 struct hugetlbfs_config config;
643 struct hugetlbfs_sb_info *sbinfo;
645 config.nr_blocks = -1; /* No limit on size by default */
646 config.nr_inodes = -1; /* No limit on number of inodes by default */
647 config.uid = current->fsuid;
648 config.gid = current->fsgid;
649 config.mode = 0755;
650 ret = hugetlbfs_parse_options(data, &config);
652 if (ret)
653 return ret;
655 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
656 if (!sbinfo)
657 return -ENOMEM;
658 sb->s_fs_info = sbinfo;
659 spin_lock_init(&sbinfo->stat_lock);
660 sbinfo->max_blocks = config.nr_blocks;
661 sbinfo->free_blocks = config.nr_blocks;
662 sbinfo->max_inodes = config.nr_inodes;
663 sbinfo->free_inodes = config.nr_inodes;
664 sb->s_maxbytes = MAX_LFS_FILESIZE;
665 sb->s_blocksize = HPAGE_SIZE;
666 sb->s_blocksize_bits = HPAGE_SHIFT;
667 sb->s_magic = HUGETLBFS_MAGIC;
668 sb->s_op = &hugetlbfs_ops;
669 sb->s_time_gran = 1;
670 inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
671 S_IFDIR | config.mode, 0);
672 if (!inode)
673 goto out_free;
675 root = d_alloc_root(inode);
676 if (!root) {
677 iput(inode);
678 goto out_free;
680 sb->s_root = root;
681 return 0;
682 out_free:
683 kfree(sbinfo);
684 return -ENOMEM;
687 int hugetlb_get_quota(struct address_space *mapping)
689 int ret = 0;
690 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
692 if (sbinfo->free_blocks > -1) {
693 spin_lock(&sbinfo->stat_lock);
694 if (sbinfo->free_blocks > 0)
695 sbinfo->free_blocks--;
696 else
697 ret = -ENOMEM;
698 spin_unlock(&sbinfo->stat_lock);
701 return ret;
704 void hugetlb_put_quota(struct address_space *mapping)
706 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
708 if (sbinfo->free_blocks > -1) {
709 spin_lock(&sbinfo->stat_lock);
710 sbinfo->free_blocks++;
711 spin_unlock(&sbinfo->stat_lock);
715 static struct super_block *hugetlbfs_get_sb(struct file_system_type *fs_type,
716 int flags, const char *dev_name, void *data)
718 return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super);
721 static struct file_system_type hugetlbfs_fs_type = {
722 .name = "hugetlbfs",
723 .get_sb = hugetlbfs_get_sb,
724 .kill_sb = kill_litter_super,
727 static struct vfsmount *hugetlbfs_vfsmount;
730 * Return the next identifier for a shm file
732 static unsigned long hugetlbfs_counter(void)
734 static DEFINE_SPINLOCK(lock);
735 static unsigned long counter;
736 unsigned long ret;
738 spin_lock(&lock);
739 ret = ++counter;
740 spin_unlock(&lock);
741 return ret;
744 static int can_do_hugetlb_shm(void)
746 return likely(capable(CAP_IPC_LOCK) ||
747 in_group_p(sysctl_hugetlb_shm_group) ||
748 can_do_mlock());
751 struct file *hugetlb_zero_setup(size_t size)
753 int error = -ENOMEM;
754 struct file *file;
755 struct inode *inode;
756 struct dentry *dentry, *root;
757 struct qstr quick_string;
758 char buf[16];
760 if (!can_do_hugetlb_shm())
761 return ERR_PTR(-EPERM);
763 if (!is_hugepage_mem_enough(size))
764 return ERR_PTR(-ENOMEM);
766 if (!user_shm_lock(size, current->user))
767 return ERR_PTR(-ENOMEM);
769 root = hugetlbfs_vfsmount->mnt_root;
770 snprintf(buf, 16, "%lu", hugetlbfs_counter());
771 quick_string.name = buf;
772 quick_string.len = strlen(quick_string.name);
773 quick_string.hash = 0;
774 dentry = d_alloc(root, &quick_string);
775 if (!dentry)
776 goto out_shm_unlock;
778 error = -ENFILE;
779 file = get_empty_filp();
780 if (!file)
781 goto out_dentry;
783 error = -ENOSPC;
784 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
785 current->fsgid, S_IFREG | S_IRWXUGO, 0);
786 if (!inode)
787 goto out_file;
789 d_instantiate(dentry, inode);
790 inode->i_size = size;
791 inode->i_nlink = 0;
792 file->f_vfsmnt = mntget(hugetlbfs_vfsmount);
793 file->f_dentry = dentry;
794 file->f_mapping = inode->i_mapping;
795 file->f_op = &hugetlbfs_file_operations;
796 file->f_mode = FMODE_WRITE | FMODE_READ;
797 return file;
799 out_file:
800 put_filp(file);
801 out_dentry:
802 dput(dentry);
803 out_shm_unlock:
804 user_shm_unlock(size, current->user);
805 return ERR_PTR(error);
808 static int __init init_hugetlbfs_fs(void)
810 int error;
811 struct vfsmount *vfsmount;
813 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
814 sizeof(struct hugetlbfs_inode_info),
815 0, 0, init_once, NULL);
816 if (hugetlbfs_inode_cachep == NULL)
817 return -ENOMEM;
819 error = register_filesystem(&hugetlbfs_fs_type);
820 if (error)
821 goto out;
823 vfsmount = kern_mount(&hugetlbfs_fs_type);
825 if (!IS_ERR(vfsmount)) {
826 hugetlbfs_vfsmount = vfsmount;
827 return 0;
830 error = PTR_ERR(vfsmount);
832 out:
833 if (error)
834 kmem_cache_destroy(hugetlbfs_inode_cachep);
835 return error;
838 static void __exit exit_hugetlbfs_fs(void)
840 kmem_cache_destroy(hugetlbfs_inode_cachep);
841 unregister_filesystem(&hugetlbfs_fs_type);
844 module_init(init_hugetlbfs_fs)
845 module_exit(exit_hugetlbfs_fs)
847 MODULE_LICENSE("GPL");