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