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