patch-2_6_7-vs1_9_1_12
[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_start & ~HPAGE_MASK)
56                 return -EINVAL;
57
58         if (vma->vm_end & ~HPAGE_MASK)
59                 return -EINVAL;
60
61         if (vma->vm_end - vma->vm_start < HPAGE_SIZE)
62                 return -EINVAL;
63
64         vma_len = (loff_t)(vma->vm_end - vma->vm_start);
65
66         down(&inode->i_sem);
67         file_accessed(file);
68         vma->vm_flags |= VM_HUGETLB | VM_RESERVED;
69         vma->vm_ops = &hugetlb_vm_ops;
70         ret = hugetlb_prefault(mapping, vma);
71         len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
72         if (ret == 0 && inode->i_size < len)
73                 inode->i_size = len;
74         up(&inode->i_sem);
75
76         return ret;
77 }
78
79 /*
80  * Called under down_write(mmap_sem), page_table_lock is not held
81  */
82
83 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
84 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
85                 unsigned long len, unsigned long pgoff, unsigned long flags);
86 #else
87 static unsigned long
88 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
89                 unsigned long len, unsigned long pgoff, unsigned long flags)
90 {
91         struct mm_struct *mm = current->mm;
92         struct vm_area_struct *vma;
93
94         if (len & ~HPAGE_MASK)
95                 return -EINVAL;
96         if (len > TASK_SIZE)
97                 return -ENOMEM;
98
99         if (addr) {
100                 addr = ALIGN(addr, HPAGE_SIZE);
101                 vma = find_vma(mm, addr);
102                 if (TASK_SIZE - len >= addr &&
103                     (!vma || addr + len <= vma->vm_start))
104                         return addr;
105         }
106
107         addr = ALIGN(mm->free_area_cache, HPAGE_SIZE);
108
109         for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
110                 /* At this point:  (!vma || addr < vma->vm_end). */
111                 if (TASK_SIZE - len < addr)
112                         return -ENOMEM;
113                 if (!vma || addr + len <= vma->vm_start)
114                         return addr;
115                 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
116         }
117 }
118 #endif
119
120 /*
121  * Read a page. Again trivial. If it didn't already exist
122  * in the page cache, it is zero-filled.
123  */
124 static int hugetlbfs_readpage(struct file *file, struct page * page)
125 {
126         unlock_page(page);
127         return -EINVAL;
128 }
129
130 static int hugetlbfs_prepare_write(struct file *file,
131                         struct page *page, unsigned offset, unsigned to)
132 {
133         return -EINVAL;
134 }
135
136 static int hugetlbfs_commit_write(struct file *file,
137                         struct page *page, unsigned offset, unsigned to)
138 {
139         return -EINVAL;
140 }
141
142 void huge_pagevec_release(struct pagevec *pvec)
143 {
144         int i;
145
146         for (i = 0; i < pagevec_count(pvec); ++i)
147                 put_page(pvec->pages[i]);
148
149         pagevec_reinit(pvec);
150 }
151
152 void truncate_huge_page(struct page *page)
153 {
154         clear_page_dirty(page);
155         ClearPageUptodate(page);
156         remove_from_page_cache(page);
157         put_page(page);
158 }
159
160 void truncate_hugepages(struct address_space *mapping, loff_t lstart)
161 {
162         const pgoff_t start = lstart >> HPAGE_SHIFT;
163         struct pagevec pvec;
164         pgoff_t next;
165         int i;
166
167         pagevec_init(&pvec, 0);
168         next = start;
169         while (1) {
170                 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
171                         if (next == start)
172                                 break;
173                         next = start;
174                         continue;
175                 }
176
177                 for (i = 0; i < pagevec_count(&pvec); ++i) {
178                         struct page *page = pvec.pages[i];
179
180                         lock_page(page);
181                         if (page->index > next)
182                                 next = page->index;
183                         ++next;
184                         truncate_huge_page(page);
185                         unlock_page(page);
186                         hugetlb_put_quota(mapping);
187                 }
188                 huge_pagevec_release(&pvec);
189         }
190         BUG_ON(!lstart && mapping->nrpages);
191 }
192
193 static void hugetlbfs_delete_inode(struct inode *inode)
194 {
195         struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(inode->i_sb);
196
197         hlist_del_init(&inode->i_hash);
198         list_del_init(&inode->i_list);
199         inode->i_state |= I_FREEING;
200         inodes_stat.nr_inodes--;
201         spin_unlock(&inode_lock);
202
203         if (inode->i_data.nrpages)
204                 truncate_hugepages(&inode->i_data, 0);
205
206         security_inode_delete(inode);
207
208         if (sbinfo->free_inodes >= 0) {
209                 spin_lock(&sbinfo->stat_lock);
210                 sbinfo->free_inodes++;
211                 spin_unlock(&sbinfo->stat_lock);
212         }
213
214         clear_inode(inode);
215         destroy_inode(inode);
216 }
217
218 static void hugetlbfs_forget_inode(struct inode *inode)
219 {
220         struct super_block *super_block = inode->i_sb;
221         struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(super_block);
222
223         if (hlist_unhashed(&inode->i_hash))
224                 goto out_truncate;
225
226         if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
227                 list_del(&inode->i_list);
228                 list_add(&inode->i_list, &inode_unused);
229         }
230         inodes_stat.nr_unused++;
231         if (!super_block || (super_block->s_flags & MS_ACTIVE)) {
232                 spin_unlock(&inode_lock);
233                 return;
234         }
235
236         /* write_inode_now() ? */
237         inodes_stat.nr_unused--;
238         hlist_del_init(&inode->i_hash);
239 out_truncate:
240         list_del_init(&inode->i_list);
241         inode->i_state |= I_FREEING;
242         inodes_stat.nr_inodes--;
243         spin_unlock(&inode_lock);
244         if (inode->i_data.nrpages)
245                 truncate_hugepages(&inode->i_data, 0);
246
247         if (sbinfo->free_inodes >= 0) {
248                 spin_lock(&sbinfo->stat_lock);
249                 sbinfo->free_inodes++;
250                 spin_unlock(&sbinfo->stat_lock);
251         }
252
253         clear_inode(inode);
254         destroy_inode(inode);
255 }
256
257 static void hugetlbfs_drop_inode(struct inode *inode)
258 {
259         if (!inode->i_nlink)
260                 hugetlbfs_delete_inode(inode);
261         else
262                 hugetlbfs_forget_inode(inode);
263 }
264
265 /*
266  * h_pgoff is in HPAGE_SIZE units.
267  * vma->vm_pgoff is in PAGE_SIZE units.
268  */
269 static inline void
270 hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
271 {
272         struct vm_area_struct *vma = NULL;
273         struct prio_tree_iter iter;
274
275         while ((vma = vma_prio_tree_next(vma, root, &iter,
276                                         h_pgoff, ULONG_MAX)) != NULL) {
277                 unsigned long h_vm_pgoff;
278                 unsigned long v_length;
279                 unsigned long v_offset;
280
281                 h_vm_pgoff = vma->vm_pgoff << (HPAGE_SHIFT - PAGE_SHIFT);
282                 v_length = vma->vm_end - vma->vm_start;
283                 v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT;
284
285                 /*
286                  * Is this VMA fully outside the truncation point?
287                  */
288                 if (h_vm_pgoff >= h_pgoff)
289                         v_offset = 0;
290
291                 zap_hugepage_range(vma,
292                                 vma->vm_start + v_offset,
293                                 v_length - v_offset);
294         }
295 }
296
297 /*
298  * Expanding truncates are not allowed.
299  */
300 static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
301 {
302         unsigned long pgoff;
303         struct address_space *mapping = inode->i_mapping;
304
305         if (offset > inode->i_size)
306                 return -EINVAL;
307
308         BUG_ON(offset & ~HPAGE_MASK);
309         pgoff = offset >> HPAGE_SHIFT;
310
311         inode->i_size = offset;
312         spin_lock(&mapping->i_mmap_lock);
313         if (!prio_tree_empty(&mapping->i_mmap))
314                 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
315         spin_unlock(&mapping->i_mmap_lock);
316         truncate_hugepages(mapping, offset);
317         return 0;
318 }
319
320 static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
321 {
322         struct inode *inode = dentry->d_inode;
323         int error;
324         unsigned int ia_valid = attr->ia_valid;
325
326         BUG_ON(!inode);
327
328         error = inode_change_ok(inode, attr);
329         if (error)
330                 goto out;
331
332         error = security_inode_setattr(dentry, attr);
333         if (error)
334                 goto out;
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");