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