[PATCH] Remove unnecessary check in fs/reiserfs/inode.c
[linux-2.6/linux-loongson.git] / fs / hugetlbfs / inode.c
blob5e03b2f67b932dcb37d61ed3dbbffb6576141cff
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 const struct address_space_operations hugetlbfs_aops;
38 const 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);
59 static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
61 struct inode *inode = file->f_dentry->d_inode;
62 loff_t len, vma_len;
63 int ret;
65 if (vma->vm_pgoff & (HPAGE_SIZE / PAGE_SIZE - 1))
66 return -EINVAL;
68 if (vma->vm_start & ~HPAGE_MASK)
69 return -EINVAL;
71 if (vma->vm_end & ~HPAGE_MASK)
72 return -EINVAL;
74 if (vma->vm_end - vma->vm_start < HPAGE_SIZE)
75 return -EINVAL;
77 vma_len = (loff_t)(vma->vm_end - vma->vm_start);
79 mutex_lock(&inode->i_mutex);
80 file_accessed(file);
81 vma->vm_flags |= VM_HUGETLB | VM_RESERVED;
82 vma->vm_ops = &hugetlb_vm_ops;
84 ret = -ENOMEM;
85 len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
87 if (vma->vm_flags & VM_MAYSHARE &&
88 hugetlb_reserve_pages(inode, vma->vm_pgoff >> (HPAGE_SHIFT-PAGE_SHIFT),
89 len >> HPAGE_SHIFT))
90 goto out;
92 ret = 0;
93 hugetlb_prefault_arch_hook(vma->vm_mm);
94 if (vma->vm_flags & VM_WRITE && inode->i_size < len)
95 inode->i_size = len;
96 out:
97 mutex_unlock(&inode->i_mutex);
99 return ret;
103 * Called under down_write(mmap_sem).
106 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
107 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
108 unsigned long len, unsigned long pgoff, unsigned long flags);
109 #else
110 static unsigned long
111 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
112 unsigned long len, unsigned long pgoff, unsigned long flags)
114 struct mm_struct *mm = current->mm;
115 struct vm_area_struct *vma;
116 unsigned long start_addr;
118 if (len & ~HPAGE_MASK)
119 return -EINVAL;
120 if (len > TASK_SIZE)
121 return -ENOMEM;
123 if (addr) {
124 addr = ALIGN(addr, HPAGE_SIZE);
125 vma = find_vma(mm, addr);
126 if (TASK_SIZE - len >= addr &&
127 (!vma || addr + len <= vma->vm_start))
128 return addr;
131 start_addr = mm->free_area_cache;
133 if (len <= mm->cached_hole_size)
134 start_addr = TASK_UNMAPPED_BASE;
136 full_search:
137 addr = ALIGN(start_addr, HPAGE_SIZE);
139 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
140 /* At this point: (!vma || addr < vma->vm_end). */
141 if (TASK_SIZE - len < addr) {
143 * Start a new search - just in case we missed
144 * some holes.
146 if (start_addr != TASK_UNMAPPED_BASE) {
147 start_addr = TASK_UNMAPPED_BASE;
148 goto full_search;
150 return -ENOMEM;
153 if (!vma || addr + len <= vma->vm_start)
154 return addr;
155 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
158 #endif
161 * Read a page. Again trivial. If it didn't already exist
162 * in the page cache, it is zero-filled.
164 static int hugetlbfs_readpage(struct file *file, struct page * page)
166 unlock_page(page);
167 return -EINVAL;
170 static int hugetlbfs_prepare_write(struct file *file,
171 struct page *page, unsigned offset, unsigned to)
173 return -EINVAL;
176 static int hugetlbfs_commit_write(struct file *file,
177 struct page *page, unsigned offset, unsigned to)
179 return -EINVAL;
182 static void truncate_huge_page(struct page *page)
184 clear_page_dirty(page);
185 ClearPageUptodate(page);
186 remove_from_page_cache(page);
187 put_page(page);
190 static void truncate_hugepages(struct inode *inode, loff_t lstart)
192 struct address_space *mapping = &inode->i_data;
193 const pgoff_t start = lstart >> HPAGE_SHIFT;
194 struct pagevec pvec;
195 pgoff_t next;
196 int i, freed = 0;
198 pagevec_init(&pvec, 0);
199 next = start;
200 while (1) {
201 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
202 if (next == start)
203 break;
204 next = start;
205 continue;
208 for (i = 0; i < pagevec_count(&pvec); ++i) {
209 struct page *page = pvec.pages[i];
211 lock_page(page);
212 if (page->index > next)
213 next = page->index;
214 ++next;
215 truncate_huge_page(page);
216 unlock_page(page);
217 hugetlb_put_quota(mapping);
218 freed++;
220 huge_pagevec_release(&pvec);
222 BUG_ON(!lstart && mapping->nrpages);
223 hugetlb_unreserve_pages(inode, start, freed);
226 static void hugetlbfs_delete_inode(struct inode *inode)
228 truncate_hugepages(inode, 0);
229 clear_inode(inode);
232 static void hugetlbfs_forget_inode(struct inode *inode) __releases(inode_lock)
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);
242 return;
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 truncate_hugepages(inode, 0);
262 clear_inode(inode);
263 destroy_inode(inode);
266 static void hugetlbfs_drop_inode(struct inode *inode)
268 if (!inode->i_nlink)
269 generic_delete_inode(inode);
270 else
271 hugetlbfs_forget_inode(inode);
275 * h_pgoff is in HPAGE_SIZE units.
276 * vma->vm_pgoff is in PAGE_SIZE units.
278 static inline void
279 hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
281 struct vm_area_struct *vma;
282 struct prio_tree_iter iter;
284 vma_prio_tree_foreach(vma, &iter, root, h_pgoff, ULONG_MAX) {
285 unsigned long h_vm_pgoff;
286 unsigned long v_offset;
288 h_vm_pgoff = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
289 v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT;
291 * Is this VMA fully outside the truncation point?
293 if (h_vm_pgoff >= h_pgoff)
294 v_offset = 0;
296 unmap_hugepage_range(vma,
297 vma->vm_start + v_offset, vma->vm_end);
302 * Expanding truncates are not allowed.
304 static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
306 unsigned long pgoff;
307 struct address_space *mapping = inode->i_mapping;
309 if (offset > inode->i_size)
310 return -EINVAL;
312 BUG_ON(offset & ~HPAGE_MASK);
313 pgoff = offset >> HPAGE_SHIFT;
315 inode->i_size = offset;
316 spin_lock(&mapping->i_mmap_lock);
317 if (!prio_tree_empty(&mapping->i_mmap))
318 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
319 spin_unlock(&mapping->i_mmap_lock);
320 truncate_hugepages(inode, offset);
321 return 0;
324 static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
326 struct inode *inode = dentry->d_inode;
327 int error;
328 unsigned int ia_valid = attr->ia_valid;
330 BUG_ON(!inode);
332 error = inode_change_ok(inode, attr);
333 if (error)
334 goto out;
336 if (ia_valid & ATTR_SIZE) {
337 error = -EINVAL;
338 if (!(attr->ia_size & ~HPAGE_MASK))
339 error = hugetlb_vmtruncate(inode, attr->ia_size);
340 if (error)
341 goto out;
342 attr->ia_valid &= ~ATTR_SIZE;
344 error = inode_setattr(inode, attr);
345 out:
346 return error;
349 static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
350 gid_t gid, int mode, dev_t dev)
352 struct inode *inode;
354 inode = new_inode(sb);
355 if (inode) {
356 struct hugetlbfs_inode_info *info;
357 inode->i_mode = mode;
358 inode->i_uid = uid;
359 inode->i_gid = gid;
360 inode->i_blocks = 0;
361 inode->i_mapping->a_ops = &hugetlbfs_aops;
362 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
363 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
364 INIT_LIST_HEAD(&inode->i_mapping->private_list);
365 info = HUGETLBFS_I(inode);
366 mpol_shared_policy_init(&info->policy, MPOL_DEFAULT, NULL);
367 switch (mode & S_IFMT) {
368 default:
369 init_special_inode(inode, mode, dev);
370 break;
371 case S_IFREG:
372 inode->i_op = &hugetlbfs_inode_operations;
373 inode->i_fop = &hugetlbfs_file_operations;
374 break;
375 case S_IFDIR:
376 inode->i_op = &hugetlbfs_dir_inode_operations;
377 inode->i_fop = &simple_dir_operations;
379 /* directory inodes start off with i_nlink == 2 (for "." entry) */
380 inc_nlink(inode);
381 break;
382 case S_IFLNK:
383 inode->i_op = &page_symlink_inode_operations;
384 break;
387 return inode;
391 * File creation. Allocate an inode, and we're done..
393 static int hugetlbfs_mknod(struct inode *dir,
394 struct dentry *dentry, int mode, dev_t dev)
396 struct inode *inode;
397 int error = -ENOSPC;
398 gid_t gid;
400 if (dir->i_mode & S_ISGID) {
401 gid = dir->i_gid;
402 if (S_ISDIR(mode))
403 mode |= S_ISGID;
404 } else {
405 gid = current->fsgid;
407 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
408 if (inode) {
409 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
410 d_instantiate(dentry, inode);
411 dget(dentry); /* Extra count - pin the dentry in core */
412 error = 0;
414 return error;
417 static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
419 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
420 if (!retval)
421 inc_nlink(dir);
422 return retval;
425 static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
427 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
430 static int hugetlbfs_symlink(struct inode *dir,
431 struct dentry *dentry, const char *symname)
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 else
440 gid = current->fsgid;
442 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
443 gid, S_IFLNK|S_IRWXUGO, 0);
444 if (inode) {
445 int l = strlen(symname)+1;
446 error = page_symlink(inode, symname, l);
447 if (!error) {
448 d_instantiate(dentry, inode);
449 dget(dentry);
450 } else
451 iput(inode);
453 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
455 return error;
459 * For direct-IO reads into hugetlb pages
461 static int hugetlbfs_set_page_dirty(struct page *page)
463 return 0;
466 static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
468 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
470 buf->f_type = HUGETLBFS_MAGIC;
471 buf->f_bsize = HPAGE_SIZE;
472 if (sbinfo) {
473 spin_lock(&sbinfo->stat_lock);
474 /* If no limits set, just report 0 for max/free/used
475 * blocks, like simple_statfs() */
476 if (sbinfo->max_blocks >= 0) {
477 buf->f_blocks = sbinfo->max_blocks;
478 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
479 buf->f_files = sbinfo->max_inodes;
480 buf->f_ffree = sbinfo->free_inodes;
482 spin_unlock(&sbinfo->stat_lock);
484 buf->f_namelen = NAME_MAX;
485 return 0;
488 static void hugetlbfs_put_super(struct super_block *sb)
490 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
492 if (sbi) {
493 sb->s_fs_info = NULL;
494 kfree(sbi);
498 static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
500 if (sbinfo->free_inodes >= 0) {
501 spin_lock(&sbinfo->stat_lock);
502 if (unlikely(!sbinfo->free_inodes)) {
503 spin_unlock(&sbinfo->stat_lock);
504 return 0;
506 sbinfo->free_inodes--;
507 spin_unlock(&sbinfo->stat_lock);
510 return 1;
513 static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
515 if (sbinfo->free_inodes >= 0) {
516 spin_lock(&sbinfo->stat_lock);
517 sbinfo->free_inodes++;
518 spin_unlock(&sbinfo->stat_lock);
523 static kmem_cache_t *hugetlbfs_inode_cachep;
525 static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
527 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
528 struct hugetlbfs_inode_info *p;
530 if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
531 return NULL;
532 p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
533 if (unlikely(!p)) {
534 hugetlbfs_inc_free_inodes(sbinfo);
535 return NULL;
537 return &p->vfs_inode;
540 static void hugetlbfs_destroy_inode(struct inode *inode)
542 hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
543 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
544 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
547 static const struct address_space_operations hugetlbfs_aops = {
548 .readpage = hugetlbfs_readpage,
549 .prepare_write = hugetlbfs_prepare_write,
550 .commit_write = hugetlbfs_commit_write,
551 .set_page_dirty = hugetlbfs_set_page_dirty,
555 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
557 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
559 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
560 SLAB_CTOR_CONSTRUCTOR)
561 inode_init_once(&ei->vfs_inode);
564 const struct file_operations hugetlbfs_file_operations = {
565 .mmap = hugetlbfs_file_mmap,
566 .fsync = simple_sync_file,
567 .get_unmapped_area = hugetlb_get_unmapped_area,
570 static struct inode_operations hugetlbfs_dir_inode_operations = {
571 .create = hugetlbfs_create,
572 .lookup = simple_lookup,
573 .link = simple_link,
574 .unlink = simple_unlink,
575 .symlink = hugetlbfs_symlink,
576 .mkdir = hugetlbfs_mkdir,
577 .rmdir = simple_rmdir,
578 .mknod = hugetlbfs_mknod,
579 .rename = simple_rename,
580 .setattr = hugetlbfs_setattr,
583 static struct inode_operations hugetlbfs_inode_operations = {
584 .setattr = hugetlbfs_setattr,
587 static struct super_operations hugetlbfs_ops = {
588 .alloc_inode = hugetlbfs_alloc_inode,
589 .destroy_inode = hugetlbfs_destroy_inode,
590 .statfs = hugetlbfs_statfs,
591 .delete_inode = hugetlbfs_delete_inode,
592 .drop_inode = hugetlbfs_drop_inode,
593 .put_super = hugetlbfs_put_super,
596 static int
597 hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
599 char *opt, *value, *rest;
601 if (!options)
602 return 0;
603 while ((opt = strsep(&options, ",")) != NULL) {
604 if (!*opt)
605 continue;
607 value = strchr(opt, '=');
608 if (!value || !*value)
609 return -EINVAL;
610 else
611 *value++ = '\0';
613 if (!strcmp(opt, "uid"))
614 pconfig->uid = simple_strtoul(value, &value, 0);
615 else if (!strcmp(opt, "gid"))
616 pconfig->gid = simple_strtoul(value, &value, 0);
617 else if (!strcmp(opt, "mode"))
618 pconfig->mode = simple_strtoul(value,&value,0) & 0777U;
619 else if (!strcmp(opt, "size")) {
620 unsigned long long size = memparse(value, &rest);
621 if (*rest == '%') {
622 size <<= HPAGE_SHIFT;
623 size *= max_huge_pages;
624 do_div(size, 100);
625 rest++;
627 size &= HPAGE_MASK;
628 pconfig->nr_blocks = (size >> HPAGE_SHIFT);
629 value = rest;
630 } else if (!strcmp(opt,"nr_inodes")) {
631 pconfig->nr_inodes = memparse(value, &rest);
632 value = rest;
633 } else
634 return -EINVAL;
636 if (*value)
637 return -EINVAL;
639 return 0;
642 static int
643 hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
645 struct inode * inode;
646 struct dentry * root;
647 int ret;
648 struct hugetlbfs_config config;
649 struct hugetlbfs_sb_info *sbinfo;
651 config.nr_blocks = -1; /* No limit on size by default */
652 config.nr_inodes = -1; /* No limit on number of inodes by default */
653 config.uid = current->fsuid;
654 config.gid = current->fsgid;
655 config.mode = 0755;
656 ret = hugetlbfs_parse_options(data, &config);
658 if (ret)
659 return ret;
661 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
662 if (!sbinfo)
663 return -ENOMEM;
664 sb->s_fs_info = sbinfo;
665 spin_lock_init(&sbinfo->stat_lock);
666 sbinfo->max_blocks = config.nr_blocks;
667 sbinfo->free_blocks = config.nr_blocks;
668 sbinfo->max_inodes = config.nr_inodes;
669 sbinfo->free_inodes = config.nr_inodes;
670 sb->s_maxbytes = MAX_LFS_FILESIZE;
671 sb->s_blocksize = HPAGE_SIZE;
672 sb->s_blocksize_bits = HPAGE_SHIFT;
673 sb->s_magic = HUGETLBFS_MAGIC;
674 sb->s_op = &hugetlbfs_ops;
675 sb->s_time_gran = 1;
676 inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
677 S_IFDIR | config.mode, 0);
678 if (!inode)
679 goto out_free;
681 root = d_alloc_root(inode);
682 if (!root) {
683 iput(inode);
684 goto out_free;
686 sb->s_root = root;
687 return 0;
688 out_free:
689 kfree(sbinfo);
690 return -ENOMEM;
693 int hugetlb_get_quota(struct address_space *mapping)
695 int ret = 0;
696 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
698 if (sbinfo->free_blocks > -1) {
699 spin_lock(&sbinfo->stat_lock);
700 if (sbinfo->free_blocks > 0)
701 sbinfo->free_blocks--;
702 else
703 ret = -ENOMEM;
704 spin_unlock(&sbinfo->stat_lock);
707 return ret;
710 void hugetlb_put_quota(struct address_space *mapping)
712 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
714 if (sbinfo->free_blocks > -1) {
715 spin_lock(&sbinfo->stat_lock);
716 sbinfo->free_blocks++;
717 spin_unlock(&sbinfo->stat_lock);
721 static int hugetlbfs_get_sb(struct file_system_type *fs_type,
722 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
724 return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super, mnt);
727 static struct file_system_type hugetlbfs_fs_type = {
728 .name = "hugetlbfs",
729 .get_sb = hugetlbfs_get_sb,
730 .kill_sb = kill_litter_super,
733 static struct vfsmount *hugetlbfs_vfsmount;
735 static int can_do_hugetlb_shm(void)
737 return likely(capable(CAP_IPC_LOCK) ||
738 in_group_p(sysctl_hugetlb_shm_group) ||
739 can_do_mlock());
742 struct file *hugetlb_zero_setup(size_t size)
744 int error = -ENOMEM;
745 struct file *file;
746 struct inode *inode;
747 struct dentry *dentry, *root;
748 struct qstr quick_string;
749 char buf[16];
750 static atomic_t counter;
752 if (!can_do_hugetlb_shm())
753 return ERR_PTR(-EPERM);
755 if (!user_shm_lock(size, current->user))
756 return ERR_PTR(-ENOMEM);
758 root = hugetlbfs_vfsmount->mnt_root;
759 snprintf(buf, 16, "%u", atomic_inc_return(&counter));
760 quick_string.name = buf;
761 quick_string.len = strlen(quick_string.name);
762 quick_string.hash = 0;
763 dentry = d_alloc(root, &quick_string);
764 if (!dentry)
765 goto out_shm_unlock;
767 error = -ENFILE;
768 file = get_empty_filp();
769 if (!file)
770 goto out_dentry;
772 error = -ENOSPC;
773 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
774 current->fsgid, S_IFREG | S_IRWXUGO, 0);
775 if (!inode)
776 goto out_file;
778 error = -ENOMEM;
779 if (hugetlb_reserve_pages(inode, 0, size >> HPAGE_SHIFT))
780 goto out_inode;
782 d_instantiate(dentry, inode);
783 inode->i_size = size;
784 inode->i_nlink = 0;
785 file->f_vfsmnt = mntget(hugetlbfs_vfsmount);
786 file->f_dentry = dentry;
787 file->f_mapping = inode->i_mapping;
788 file->f_op = &hugetlbfs_file_operations;
789 file->f_mode = FMODE_WRITE | FMODE_READ;
790 return file;
792 out_inode:
793 iput(inode);
794 out_file:
795 put_filp(file);
796 out_dentry:
797 dput(dentry);
798 out_shm_unlock:
799 user_shm_unlock(size, current->user);
800 return ERR_PTR(error);
803 static int __init init_hugetlbfs_fs(void)
805 int error;
806 struct vfsmount *vfsmount;
808 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
809 sizeof(struct hugetlbfs_inode_info),
810 0, 0, init_once, NULL);
811 if (hugetlbfs_inode_cachep == NULL)
812 return -ENOMEM;
814 error = register_filesystem(&hugetlbfs_fs_type);
815 if (error)
816 goto out;
818 vfsmount = kern_mount(&hugetlbfs_fs_type);
820 if (!IS_ERR(vfsmount)) {
821 hugetlbfs_vfsmount = vfsmount;
822 return 0;
825 error = PTR_ERR(vfsmount);
827 out:
828 if (error)
829 kmem_cache_destroy(hugetlbfs_inode_cachep);
830 return error;
833 static void __exit exit_hugetlbfs_fs(void)
835 kmem_cache_destroy(hugetlbfs_inode_cachep);
836 unregister_filesystem(&hugetlbfs_fs_type);
839 module_init(init_hugetlbfs_fs)
840 module_exit(exit_hugetlbfs_fs)
842 MODULE_LICENSE("GPL");