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