Merge to Fedora kernel-2.6.7-1.492
[linux-2.6.git] / fs / hugetlbfs / inode.c
1 /*
2  * hugetlbpage-backed filesystem.  Based on ramfs.
3  *
4  * William Irwin, 2002
5  *
6  * Copyright (C) 2002 Linus Torvalds.
7  */
8
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>
29
30 #include <asm/uaccess.h>
31
32 /* some random number */
33 #define HUGETLBFS_MAGIC 0x958458f6
34
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;
40
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 */
44 };
45
46 static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
47 {
48         struct inode *inode = file->f_dentry->d_inode;
49         struct address_space *mapping = inode->i_mapping;
50         loff_t len, vma_len;
51         int ret;
52
53         if (vma->vm_start & ~HPAGE_MASK)
54                 return -EINVAL;
55
56         if (vma->vm_end & ~HPAGE_MASK)
57                 return -EINVAL;
58
59         if (vma->vm_end - vma->vm_start < HPAGE_SIZE)
60                 return -EINVAL;
61
62         vma_len = (loff_t)(vma->vm_end - vma->vm_start);
63
64         down(&inode->i_sem);
65         file_accessed(file);
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)
71                 inode->i_size = len;
72         up(&inode->i_sem);
73
74         return ret;
75 }
76
77 /*
78  * Called under down_write(mmap_sem), page_table_lock is not held
79  */
80
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);
84 #else
85 static unsigned long
86 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
87                 unsigned long len, unsigned long pgoff, unsigned long flags)
88 {
89         struct mm_struct *mm = current->mm;
90         struct vm_area_struct *vma;
91
92         if (len & ~HPAGE_MASK)
93                 return -EINVAL;
94         if (len > TASK_SIZE)
95                 return -ENOMEM;
96
97         if (addr) {
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))
102                         return addr;
103         }
104
105         addr = ALIGN(mm->free_area_cache, HPAGE_SIZE);
106
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)
110                         return -ENOMEM;
111                 if (!vma || addr + len <= vma->vm_start)
112                         return addr;
113                 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
114         }
115 }
116 #endif
117
118 /*
119  * Read a page. Again trivial. If it didn't already exist
120  * in the page cache, it is zero-filled.
121  */
122 static int hugetlbfs_readpage(struct file *file, struct page * page)
123 {
124         unlock_page(page);
125         return -EINVAL;
126 }
127
128 static int hugetlbfs_prepare_write(struct file *file,
129                         struct page *page, unsigned offset, unsigned to)
130 {
131         return -EINVAL;
132 }
133
134 static int hugetlbfs_commit_write(struct file *file,
135                         struct page *page, unsigned offset, unsigned to)
136 {
137         return -EINVAL;
138 }
139
140 void huge_pagevec_release(struct pagevec *pvec)
141 {
142         int i;
143
144         for (i = 0; i < pagevec_count(pvec); ++i)
145                 put_page(pvec->pages[i]);
146
147         pagevec_reinit(pvec);
148 }
149
150 void truncate_huge_page(struct page *page)
151 {
152         clear_page_dirty(page);
153         ClearPageUptodate(page);
154         remove_from_page_cache(page);
155         put_page(page);
156 }
157
158 void truncate_hugepages(struct address_space *mapping, loff_t lstart)
159 {
160         const pgoff_t start = lstart >> HPAGE_SHIFT;
161         struct pagevec pvec;
162         pgoff_t next;
163         int i;
164
165         pagevec_init(&pvec, 0);
166         next = start;
167         while (1) {
168                 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
169                         if (next == start)
170                                 break;
171                         next = start;
172                         continue;
173                 }
174
175                 for (i = 0; i < pagevec_count(&pvec); ++i) {
176                         struct page *page = pvec.pages[i];
177
178                         lock_page(page);
179                         if (page->index > next)
180                                 next = page->index;
181                         ++next;
182                         truncate_huge_page(page);
183                         unlock_page(page);
184                         hugetlb_put_quota(mapping);
185                 }
186                 huge_pagevec_release(&pvec);
187         }
188         BUG_ON(!lstart && mapping->nrpages);
189 }
190
191 static void hugetlbfs_delete_inode(struct inode *inode)
192 {
193         struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(inode->i_sb);
194
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);
200
201         if (inode->i_data.nrpages)
202                 truncate_hugepages(&inode->i_data, 0);
203
204         security_inode_delete(inode);
205
206         if (sbinfo->free_inodes >= 0) {
207                 spin_lock(&sbinfo->stat_lock);
208                 sbinfo->free_inodes++;
209                 spin_unlock(&sbinfo->stat_lock);
210         }
211
212         clear_inode(inode);
213         destroy_inode(inode);
214 }
215
216 static void hugetlbfs_forget_inode(struct inode *inode)
217 {
218         struct super_block *super_block = inode->i_sb;
219         struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(super_block);
220
221         if (hlist_unhashed(&inode->i_hash))
222                 goto out_truncate;
223
224         if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
225                 list_del(&inode->i_list);
226                 list_add(&inode->i_list, &inode_unused);
227         }
228         inodes_stat.nr_unused++;
229         if (!super_block || (super_block->s_flags & MS_ACTIVE)) {
230                 spin_unlock(&inode_lock);
231                 return;
232         }
233
234         /* write_inode_now() ? */
235         inodes_stat.nr_unused--;
236         hlist_del_init(&inode->i_hash);
237 out_truncate:
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);
244
245         if (sbinfo->free_inodes >= 0) {
246                 spin_lock(&sbinfo->stat_lock);
247                 sbinfo->free_inodes++;
248                 spin_unlock(&sbinfo->stat_lock);
249         }
250
251         clear_inode(inode);
252         destroy_inode(inode);
253 }
254
255 static void hugetlbfs_drop_inode(struct inode *inode)
256 {
257         if (!inode->i_nlink)
258                 hugetlbfs_delete_inode(inode);
259         else
260                 hugetlbfs_forget_inode(inode);
261 }
262
263 /*
264  * h_pgoff is in HPAGE_SIZE units.
265  * vma->vm_pgoff is in PAGE_SIZE units.
266  */
267 static inline void
268 hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
269 {
270         struct vm_area_struct *vma = NULL;
271         struct prio_tree_iter iter;
272
273         while ((vma = vma_prio_tree_next(vma, root, &iter,
274                                         h_pgoff, ULONG_MAX)) != NULL) {
275                 unsigned long h_vm_pgoff;
276                 unsigned long v_length;
277                 unsigned long v_offset;
278
279                 h_vm_pgoff = vma->vm_pgoff << (HPAGE_SHIFT - PAGE_SHIFT);
280                 v_length = vma->vm_end - vma->vm_start;
281                 v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT;
282
283                 /*
284                  * Is this VMA fully outside the truncation point?
285                  */
286                 if (h_vm_pgoff >= h_pgoff)
287                         v_offset = 0;
288
289                 zap_hugepage_range(vma,
290                                 vma->vm_start + v_offset,
291                                 v_length - v_offset);
292         }
293 }
294
295 /*
296  * Expanding truncates are not allowed.
297  */
298 static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
299 {
300         unsigned long pgoff;
301         struct address_space *mapping = inode->i_mapping;
302
303         if (offset > inode->i_size)
304                 return -EINVAL;
305
306         BUG_ON(offset & ~HPAGE_MASK);
307         pgoff = offset >> HPAGE_SHIFT;
308
309         inode->i_size = offset;
310         spin_lock(&mapping->i_mmap_lock);
311         if (!prio_tree_empty(&mapping->i_mmap))
312                 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
313         spin_unlock(&mapping->i_mmap_lock);
314         truncate_hugepages(mapping, offset);
315         return 0;
316 }
317
318 static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
319 {
320         struct inode *inode = dentry->d_inode;
321         int error;
322         unsigned int ia_valid = attr->ia_valid;
323
324         BUG_ON(!inode);
325
326         error = inode_change_ok(inode, attr);
327         if (error)
328                 goto out;
329
330         if (ia_valid & ATTR_SIZE) {
331                 error = -EINVAL;
332                 if (!(attr->ia_size & ~HPAGE_MASK))
333                         error = hugetlb_vmtruncate(inode, attr->ia_size);
334                 if (error)
335                         goto out;
336                 attr->ia_valid &= ~ATTR_SIZE;
337         }
338         error = inode_setattr(inode, attr);
339 out:
340         return error;
341 }
342
343 static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid, 
344                                         gid_t gid, int mode, dev_t dev)
345 {
346         struct inode *inode;
347         struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
348
349         if (sbinfo->free_inodes >= 0) {
350                 spin_lock(&sbinfo->stat_lock);
351                 if (!sbinfo->free_inodes) {
352                         spin_unlock(&sbinfo->stat_lock);
353                         return NULL;
354                 }
355                 sbinfo->free_inodes--;
356                 spin_unlock(&sbinfo->stat_lock);
357         }
358
359         inode = new_inode(sb);
360         if (inode) {
361                 struct hugetlbfs_inode_info *info;
362                 inode->i_mode = mode;
363                 inode->i_uid = uid;
364                 inode->i_gid = gid;
365                 inode->i_blksize = HPAGE_SIZE;
366                 inode->i_blocks = 0;
367                 inode->i_mapping->a_ops = &hugetlbfs_aops;
368                 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
369                 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
370                 info = HUGETLBFS_I(inode);
371                 mpol_shared_policy_init(&info->policy);
372                 switch (mode & S_IFMT) {
373                 default:
374                         init_special_inode(inode, mode, dev);
375                         break;
376                 case S_IFREG:
377                         inode->i_op = &hugetlbfs_inode_operations;
378                         inode->i_fop = &hugetlbfs_file_operations;
379                         break;
380                 case S_IFDIR:
381                         inode->i_op = &hugetlbfs_dir_inode_operations;
382                         inode->i_fop = &simple_dir_operations;
383
384                         /* directory inodes start off with i_nlink == 2 (for "." entry) */
385                         inode->i_nlink++;
386                         break;
387                 case S_IFLNK:
388                         inode->i_op = &page_symlink_inode_operations;
389                         break;
390                 }
391         }
392         return inode;
393 }
394
395 /*
396  * File creation. Allocate an inode, and we're done..
397  */
398 static int hugetlbfs_mknod(struct inode *dir,
399                         struct dentry *dentry, int mode, dev_t dev)
400 {
401         struct inode *inode;
402         int error = -ENOSPC;
403         gid_t gid;
404
405         if (dir->i_mode & S_ISGID) {
406                 gid = dir->i_gid;
407                 if (S_ISDIR(mode))
408                         mode |= S_ISGID;
409         } else {
410                 gid = current->fsgid;
411         }
412         inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
413         if (inode) {
414                 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
415                 d_instantiate(dentry, inode);
416                 dget(dentry);   /* Extra count - pin the dentry in core */
417                 error = 0;
418         }
419         return error;
420 }
421
422 static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
423 {
424         int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
425         if (!retval)
426                 dir->i_nlink++;
427         return retval;
428 }
429
430 static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
431 {
432         return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
433 }
434
435 static int hugetlbfs_symlink(struct inode *dir,
436                         struct dentry *dentry, const char *symname)
437 {
438         struct inode *inode;
439         int error = -ENOSPC;
440         gid_t gid;
441
442         if (dir->i_mode & S_ISGID)
443                 gid = dir->i_gid;
444         else
445                 gid = current->fsgid;
446
447         inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
448                                         gid, S_IFLNK|S_IRWXUGO, 0);
449         if (inode) {
450                 int l = strlen(symname)+1;
451                 error = page_symlink(inode, symname, l);
452                 if (!error) {
453                         d_instantiate(dentry, inode);
454                         dget(dentry);
455                 } else
456                         iput(inode);
457         }
458         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
459
460         return error;
461 }
462
463 /*
464  * For direct-IO reads into hugetlb pages
465  */
466 int hugetlbfs_set_page_dirty(struct page *page)
467 {
468         return 0;
469 }
470
471 static int hugetlbfs_statfs(struct super_block *sb, struct kstatfs *buf)
472 {
473         struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
474
475         buf->f_type = HUGETLBFS_MAGIC;
476         buf->f_bsize = HPAGE_SIZE;
477         if (sbinfo) {
478                 spin_lock(&sbinfo->stat_lock);
479                 buf->f_blocks = sbinfo->max_blocks;
480                 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
481                 buf->f_files = sbinfo->max_inodes;
482                 buf->f_ffree = sbinfo->free_inodes;
483                 spin_unlock(&sbinfo->stat_lock);
484         }
485         buf->f_namelen = NAME_MAX;
486         return 0;
487 }
488
489 static void hugetlbfs_put_super(struct super_block *sb)
490 {
491         struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
492
493         if (sbi) {
494                 sb->s_fs_info = NULL;
495                 kfree(sbi);
496         }
497 }
498
499 static kmem_cache_t *hugetlbfs_inode_cachep;
500
501 static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
502 {
503         struct hugetlbfs_inode_info *p;
504
505         p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
506         if (!p)
507                 return NULL;
508         return &p->vfs_inode;
509 }
510
511 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
512 {
513         struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
514
515         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
516             SLAB_CTOR_CONSTRUCTOR)
517                 inode_init_once(&ei->vfs_inode);
518 }
519
520 static void hugetlbfs_destroy_inode(struct inode *inode)
521 {
522         mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
523         kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
524 }
525
526 static struct address_space_operations hugetlbfs_aops = {
527         .readpage       = hugetlbfs_readpage,
528         .prepare_write  = hugetlbfs_prepare_write,
529         .commit_write   = hugetlbfs_commit_write,
530         .set_page_dirty = hugetlbfs_set_page_dirty,
531 };
532
533 struct file_operations hugetlbfs_file_operations = {
534         .mmap                   = hugetlbfs_file_mmap,
535         .fsync                  = simple_sync_file,
536         .get_unmapped_area      = hugetlb_get_unmapped_area,
537 };
538
539 static struct inode_operations hugetlbfs_dir_inode_operations = {
540         .create         = hugetlbfs_create,
541         .lookup         = simple_lookup,
542         .link           = simple_link,
543         .unlink         = simple_unlink,
544         .symlink        = hugetlbfs_symlink,
545         .mkdir          = hugetlbfs_mkdir,
546         .rmdir          = simple_rmdir,
547         .mknod          = hugetlbfs_mknod,
548         .rename         = simple_rename,
549         .setattr        = hugetlbfs_setattr,
550 };
551
552 static struct inode_operations hugetlbfs_inode_operations = {
553         .setattr        = hugetlbfs_setattr,
554 };
555
556 static struct super_operations hugetlbfs_ops = {
557         .alloc_inode    = hugetlbfs_alloc_inode,
558         .destroy_inode  = hugetlbfs_destroy_inode,
559         .statfs         = hugetlbfs_statfs,
560         .drop_inode     = hugetlbfs_drop_inode,
561         .put_super      = hugetlbfs_put_super,
562 };
563
564 static int
565 hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
566 {
567         char *opt, *value, *rest;
568
569         if (!options)
570                 return 0;
571         while ((opt = strsep(&options, ",")) != NULL) {
572                 if (!*opt)
573                         continue;
574
575                 value = strchr(opt, '=');
576                 if (!value || !*value)
577                         return -EINVAL;
578                 else
579                         *value++ = '\0';
580
581                 if (!strcmp(opt, "uid"))
582                         pconfig->uid = simple_strtoul(value, &value, 0);
583                 else if (!strcmp(opt, "gid"))
584                         pconfig->gid = simple_strtoul(value, &value, 0);
585                 else if (!strcmp(opt, "mode"))
586                         pconfig->mode = simple_strtoul(value,&value,0) & 0777U;
587                 else if (!strcmp(opt, "size")) {
588                         unsigned long long size = memparse(value, &rest);
589                         if (*rest == '%') {
590                                 size <<= HPAGE_SHIFT;
591                                 size *= max_huge_pages;
592                                 do_div(size, 100);
593                                 rest++;
594                         }
595                         size &= HPAGE_MASK;
596                         pconfig->nr_blocks = (size >> HPAGE_SHIFT);
597                         value = rest;
598                 } else if (!strcmp(opt,"nr_inodes")) {
599                         pconfig->nr_inodes = memparse(value, &rest);
600                         value = rest;
601                 } else
602                         return -EINVAL;
603
604                 if (*value)
605                         return -EINVAL;
606         }
607         return 0;
608 }
609
610 static int
611 hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
612 {
613         struct inode * inode;
614         struct dentry * root;
615         int ret;
616         struct hugetlbfs_config config;
617         struct hugetlbfs_sb_info *sbinfo;
618
619         config.nr_blocks = -1; /* No limit on size by default */
620         config.nr_inodes = -1; /* No limit on number of inodes by default */
621         config.uid = current->fsuid;
622         config.gid = current->fsgid;
623         config.mode = 0755;
624         ret = hugetlbfs_parse_options(data, &config);
625
626         if (ret)
627                 return ret;
628
629         sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
630         if (!sbinfo)
631                 return -ENOMEM;
632         sb->s_fs_info = sbinfo;
633         spin_lock_init(&sbinfo->stat_lock);
634         sbinfo->max_blocks = config.nr_blocks;
635         sbinfo->free_blocks = config.nr_blocks;
636         sbinfo->max_inodes = config.nr_inodes;
637         sbinfo->free_inodes = config.nr_inodes;
638         sb->s_blocksize = HPAGE_SIZE;
639         sb->s_blocksize_bits = HPAGE_SHIFT;
640         sb->s_magic = HUGETLBFS_MAGIC;
641         sb->s_op = &hugetlbfs_ops;
642         inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
643                                         S_IFDIR | config.mode, 0);
644         if (!inode)
645                 goto out_free;
646
647         root = d_alloc_root(inode);
648         if (!root) {
649                 iput(inode);
650                 goto out_free;
651         }
652         sb->s_root = root;
653         return 0;
654 out_free:
655         kfree(sbinfo);
656         return -ENOMEM;
657 }
658
659 int hugetlb_get_quota(struct address_space *mapping)
660 {
661         int ret = 0;
662         struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
663
664         if (sbinfo->free_blocks > -1) {
665                 spin_lock(&sbinfo->stat_lock);
666                 if (sbinfo->free_blocks > 0)
667                         sbinfo->free_blocks--;
668                 else
669                         ret = -ENOMEM;
670                 spin_unlock(&sbinfo->stat_lock);
671         }
672
673         return ret;
674 }
675
676 void hugetlb_put_quota(struct address_space *mapping)
677 {
678         struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
679
680         if (sbinfo->free_blocks > -1) {
681                 spin_lock(&sbinfo->stat_lock);
682                 sbinfo->free_blocks++;
683                 spin_unlock(&sbinfo->stat_lock);
684         }
685 }
686
687 static struct super_block *hugetlbfs_get_sb(struct file_system_type *fs_type,
688         int flags, const char *dev_name, void *data)
689 {
690         return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super);
691 }
692
693 static struct file_system_type hugetlbfs_fs_type = {
694         .name           = "hugetlbfs",
695         .get_sb         = hugetlbfs_get_sb,
696         .kill_sb        = kill_litter_super,
697 };
698
699 static struct vfsmount *hugetlbfs_vfsmount;
700
701 /*
702  * Return the next identifier for a shm file
703  */
704 static unsigned long hugetlbfs_counter(void)
705 {
706         static spinlock_t lock = SPIN_LOCK_UNLOCKED;
707         static unsigned long counter;
708         unsigned long ret;
709
710         spin_lock(&lock);
711         ret = ++counter;
712         spin_unlock(&lock);
713         return ret;
714 }
715
716 struct file *hugetlb_zero_setup(size_t size)
717 {
718         int error;
719         struct file *file;
720         struct inode *inode;
721         struct dentry *dentry, *root;
722         struct qstr quick_string;
723         char buf[16];
724
725         if (!can_do_mlock())
726                 return ERR_PTR(-EPERM);
727
728         if (!is_hugepage_mem_enough(size))
729                 return ERR_PTR(-ENOMEM);
730
731         root = hugetlbfs_vfsmount->mnt_root;
732         snprintf(buf, 16, "%lu", hugetlbfs_counter());
733         quick_string.name = buf;
734         quick_string.len = strlen(quick_string.name);
735         quick_string.hash = 0;
736         dentry = d_alloc(root, &quick_string);
737         if (!dentry)
738                 return ERR_PTR(-ENOMEM);
739
740         error = -ENFILE;
741         file = get_empty_filp();
742         if (!file)
743                 goto out_dentry;
744
745         error = -ENOSPC;
746         inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
747                                 current->fsgid, S_IFREG | S_IRWXUGO, 0);
748         if (!inode)
749                 goto out_file;
750
751         d_instantiate(dentry, inode);
752         inode->i_size = size;
753         inode->i_nlink = 0;
754         file->f_vfsmnt = mntget(hugetlbfs_vfsmount);
755         file->f_dentry = dentry;
756         file->f_mapping = inode->i_mapping;
757         file->f_op = &hugetlbfs_file_operations;
758         file->f_mode = FMODE_WRITE | FMODE_READ;
759         return file;
760
761 out_file:
762         put_filp(file);
763 out_dentry:
764         dput(dentry);
765         return ERR_PTR(error);
766 }
767
768 static int __init init_hugetlbfs_fs(void)
769 {
770         int error;
771         struct vfsmount *vfsmount;
772
773         hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
774                                         sizeof(struct hugetlbfs_inode_info),
775                                         0, SLAB_RECLAIM_ACCOUNT,
776                                         init_once, NULL);
777         if (hugetlbfs_inode_cachep == NULL)
778                 return -ENOMEM;
779
780         error = register_filesystem(&hugetlbfs_fs_type);
781         if (error)
782                 goto out;
783
784         vfsmount = kern_mount(&hugetlbfs_fs_type);
785
786         if (!IS_ERR(vfsmount)) {
787                 hugetlbfs_vfsmount = vfsmount;
788                 return 0;
789         }
790
791         error = PTR_ERR(vfsmount);
792
793  out:
794         if (error)
795                 kmem_cache_destroy(hugetlbfs_inode_cachep);
796         return error;
797 }
798
799 static void __exit exit_hugetlbfs_fs(void)
800 {
801         kmem_cache_destroy(hugetlbfs_inode_cachep);
802         unregister_filesystem(&hugetlbfs_fs_type);
803 }
804
805 module_init(init_hugetlbfs_fs)
806 module_exit(exit_hugetlbfs_fs)
807
808 MODULE_LICENSE("GPL");