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