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