upgrade to linux 2.6.9-1.11_FC2
[linux-2.6.git] / fs / hugetlbfs / inode.c
1 /*
2  * hugetlbpage-backed filesystem.  Based on ramfs.
3  *
4  * William Irwin, 2002
5  *
6  * Copyright (C) 2002 Linus Torvalds.
7  */
8
9 #include <linux/module.h>
10 #include <linux/thread_info.h>
11 #include <asm/current.h>
12 #include <linux/sched.h>                /* remove ASAP */
13 #include <linux/fs.h>
14 #include <linux/mount.h>
15 #include <linux/file.h>
16 #include <linux/writeback.h>
17 #include <linux/pagemap.h>
18 #include <linux/highmem.h>
19 #include <linux/init.h>
20 #include <linux/string.h>
21 #include <linux/backing-dev.h>
22 #include <linux/hugetlb.h>
23 #include <linux/pagevec.h>
24 #include <linux/quotaops.h>
25 #include <linux/slab.h>
26 #include <linux/dnotify.h>
27 #include <linux/statfs.h>
28 #include <linux/security.h>
29
30 #include <asm/uaccess.h>
31
32 /* some random number */
33 #define HUGETLBFS_MAGIC 0x958458f6
34
35 static struct super_operations hugetlbfs_ops;
36 static struct address_space_operations hugetlbfs_aops;
37 struct file_operations hugetlbfs_file_operations;
38 static struct inode_operations hugetlbfs_dir_inode_operations;
39 static struct inode_operations hugetlbfs_inode_operations;
40
41 static struct backing_dev_info hugetlbfs_backing_dev_info = {
42         .ra_pages       = 0,    /* No readahead */
43         .memory_backed  = 1,    /* Does not contribute to dirty memory */
44 };
45
46 int sysctl_hugetlb_shm_group;
47
48 static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
49 {
50         struct inode *inode = file->f_dentry->d_inode;
51         struct address_space *mapping = inode->i_mapping;
52         loff_t len, vma_len;
53         int ret;
54
55         if ((vma->vm_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), page_table_lock is not held
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 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 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 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 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         struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(inode->i_sb);
225
226         hlist_del_init(&inode->i_hash);
227         list_del_init(&inode->i_list);
228         inode->i_state |= I_FREEING;
229         inodes_stat.nr_inodes--;
230         spin_unlock(&inode_lock);
231
232         if (inode->i_data.nrpages)
233                 truncate_hugepages(&inode->i_data, 0);
234
235         security_inode_delete(inode);
236
237         if (sbinfo->free_inodes >= 0) {
238                 spin_lock(&sbinfo->stat_lock);
239                 sbinfo->free_inodes++;
240                 spin_unlock(&sbinfo->stat_lock);
241         }
242
243         clear_inode(inode);
244         destroy_inode(inode);
245 }
246
247 static void hugetlbfs_forget_inode(struct inode *inode)
248 {
249         struct super_block *super_block = inode->i_sb;
250         struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(super_block);
251
252         if (hlist_unhashed(&inode->i_hash))
253                 goto out_truncate;
254
255         if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
256                 list_del(&inode->i_list);
257                 list_add(&inode->i_list, &inode_unused);
258         }
259         inodes_stat.nr_unused++;
260         if (!super_block || (super_block->s_flags & MS_ACTIVE)) {
261                 spin_unlock(&inode_lock);
262                 return;
263         }
264
265         /* write_inode_now() ? */
266         inodes_stat.nr_unused--;
267         hlist_del_init(&inode->i_hash);
268 out_truncate:
269         list_del_init(&inode->i_list);
270         inode->i_state |= I_FREEING;
271         inodes_stat.nr_inodes--;
272         spin_unlock(&inode_lock);
273         if (inode->i_data.nrpages)
274                 truncate_hugepages(&inode->i_data, 0);
275
276         if (sbinfo->free_inodes >= 0) {
277                 spin_lock(&sbinfo->stat_lock);
278                 sbinfo->free_inodes++;
279                 spin_unlock(&sbinfo->stat_lock);
280         }
281
282         clear_inode(inode);
283         destroy_inode(inode);
284 }
285
286 static void hugetlbfs_drop_inode(struct inode *inode)
287 {
288         if (!inode->i_nlink)
289                 hugetlbfs_delete_inode(inode);
290         else
291                 hugetlbfs_forget_inode(inode);
292 }
293
294 /*
295  * h_pgoff is in HPAGE_SIZE units.
296  * vma->vm_pgoff is in PAGE_SIZE units.
297  */
298 static inline void
299 hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
300 {
301         struct vm_area_struct *vma;
302         struct prio_tree_iter iter;
303
304         vma_prio_tree_foreach(vma, &iter, root, h_pgoff, ULONG_MAX) {
305                 unsigned long h_vm_pgoff;
306                 unsigned long v_length;
307                 unsigned long v_offset;
308
309                 h_vm_pgoff = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
310                 v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT;
311                 /*
312                  * Is this VMA fully outside the truncation point?
313                  */
314                 if (h_vm_pgoff >= h_pgoff)
315                         v_offset = 0;
316
317                 v_length = vma->vm_end - vma->vm_start;
318
319                 zap_hugepage_range(vma,
320                                 vma->vm_start + v_offset,
321                                 v_length - v_offset);
322         }
323 }
324
325 /*
326  * Expanding truncates are not allowed.
327  */
328 static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
329 {
330         unsigned long 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 >> HPAGE_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(mapping, 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         struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
378
379         if (sbinfo->free_inodes >= 0) {
380                 spin_lock(&sbinfo->stat_lock);
381                 if (!sbinfo->free_inodes) {
382                         spin_unlock(&sbinfo->stat_lock);
383                         return NULL;
384                 }
385                 sbinfo->free_inodes--;
386                 spin_unlock(&sbinfo->stat_lock);
387         }
388
389         inode = new_inode(sb);
390         if (inode) {
391                 struct hugetlbfs_inode_info *info;
392                 inode->i_mode = mode;
393                 inode->i_uid = uid;
394                 inode->i_gid = gid;
395                 inode->i_blksize = HPAGE_SIZE;
396                 inode->i_blocks = 0;
397                 inode->i_mapping->a_ops = &hugetlbfs_aops;
398                 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
399                 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
400                 info = HUGETLBFS_I(inode);
401                 mpol_shared_policy_init(&info->policy);
402                 switch (mode & S_IFMT) {
403                 default:
404                         init_special_inode(inode, mode, dev);
405                         break;
406                 case S_IFREG:
407                         inode->i_op = &hugetlbfs_inode_operations;
408                         inode->i_fop = &hugetlbfs_file_operations;
409                         break;
410                 case S_IFDIR:
411                         inode->i_op = &hugetlbfs_dir_inode_operations;
412                         inode->i_fop = &simple_dir_operations;
413
414                         /* directory inodes start off with i_nlink == 2 (for "." entry) */
415                         inode->i_nlink++;
416                         break;
417                 case S_IFLNK:
418                         inode->i_op = &page_symlink_inode_operations;
419                         break;
420                 }
421         }
422         return inode;
423 }
424
425 /*
426  * File creation. Allocate an inode, and we're done..
427  */
428 static int hugetlbfs_mknod(struct inode *dir,
429                         struct dentry *dentry, int mode, dev_t dev)
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                 if (S_ISDIR(mode))
438                         mode |= S_ISGID;
439         } else {
440                 gid = current->fsgid;
441         }
442         inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
443         if (inode) {
444                 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
445                 d_instantiate(dentry, inode);
446                 dget(dentry);   /* Extra count - pin the dentry in core */
447                 error = 0;
448         }
449         return error;
450 }
451
452 static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
453 {
454         int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
455         if (!retval)
456                 dir->i_nlink++;
457         return retval;
458 }
459
460 static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
461 {
462         return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
463 }
464
465 static int hugetlbfs_symlink(struct inode *dir,
466                         struct dentry *dentry, const char *symname)
467 {
468         struct inode *inode;
469         int error = -ENOSPC;
470         gid_t gid;
471
472         if (dir->i_mode & S_ISGID)
473                 gid = dir->i_gid;
474         else
475                 gid = current->fsgid;
476
477         inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
478                                         gid, S_IFLNK|S_IRWXUGO, 0);
479         if (inode) {
480                 int l = strlen(symname)+1;
481                 error = page_symlink(inode, symname, l);
482                 if (!error) {
483                         d_instantiate(dentry, inode);
484                         dget(dentry);
485                 } else
486                         iput(inode);
487         }
488         dir->i_ctime = dir->i_mtime = CURRENT_TIME;
489
490         return error;
491 }
492
493 /*
494  * For direct-IO reads into hugetlb pages
495  */
496 int hugetlbfs_set_page_dirty(struct page *page)
497 {
498         return 0;
499 }
500
501 static int hugetlbfs_statfs(struct super_block *sb, struct kstatfs *buf)
502 {
503         struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
504
505         buf->f_type = HUGETLBFS_MAGIC;
506         buf->f_bsize = HPAGE_SIZE;
507         if (sbinfo) {
508                 spin_lock(&sbinfo->stat_lock);
509                 buf->f_blocks = sbinfo->max_blocks;
510                 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
511                 buf->f_files = sbinfo->max_inodes;
512                 buf->f_ffree = sbinfo->free_inodes;
513                 spin_unlock(&sbinfo->stat_lock);
514         }
515         buf->f_namelen = NAME_MAX;
516         return 0;
517 }
518
519 static void hugetlbfs_put_super(struct super_block *sb)
520 {
521         struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
522
523         if (sbi) {
524                 sb->s_fs_info = NULL;
525                 kfree(sbi);
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_inode_info *p;
534
535         p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
536         if (!p)
537                 return NULL;
538         return &p->vfs_inode;
539 }
540
541 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
542 {
543         struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
544
545         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
546             SLAB_CTOR_CONSTRUCTOR)
547                 inode_init_once(&ei->vfs_inode);
548 }
549
550 static void hugetlbfs_destroy_inode(struct inode *inode)
551 {
552         mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
553         kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
554 }
555
556 static struct address_space_operations hugetlbfs_aops = {
557         .readpage       = hugetlbfs_readpage,
558         .prepare_write  = hugetlbfs_prepare_write,
559         .commit_write   = hugetlbfs_commit_write,
560         .set_page_dirty = hugetlbfs_set_page_dirty,
561 };
562
563 struct file_operations hugetlbfs_file_operations = {
564         .mmap                   = hugetlbfs_file_mmap,
565         .fsync                  = simple_sync_file,
566         .get_unmapped_area      = hugetlb_get_unmapped_area,
567 };
568
569 static struct inode_operations hugetlbfs_dir_inode_operations = {
570         .create         = hugetlbfs_create,
571         .lookup         = simple_lookup,
572         .link           = simple_link,
573         .unlink         = simple_unlink,
574         .symlink        = hugetlbfs_symlink,
575         .mkdir          = hugetlbfs_mkdir,
576         .rmdir          = simple_rmdir,
577         .mknod          = hugetlbfs_mknod,
578         .rename         = simple_rename,
579         .setattr        = hugetlbfs_setattr,
580 };
581
582 static struct inode_operations hugetlbfs_inode_operations = {
583         .setattr        = hugetlbfs_setattr,
584 };
585
586 static struct super_operations hugetlbfs_ops = {
587         .alloc_inode    = hugetlbfs_alloc_inode,
588         .destroy_inode  = hugetlbfs_destroy_inode,
589         .statfs         = hugetlbfs_statfs,
590         .drop_inode     = hugetlbfs_drop_inode,
591         .put_super      = hugetlbfs_put_super,
592 };
593
594 static int
595 hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
596 {
597         char *opt, *value, *rest;
598
599         if (!options)
600                 return 0;
601         while ((opt = strsep(&options, ",")) != NULL) {
602                 if (!*opt)
603                         continue;
604
605                 value = strchr(opt, '=');
606                 if (!value || !*value)
607                         return -EINVAL;
608                 else
609                         *value++ = '\0';
610
611                 if (!strcmp(opt, "uid"))
612                         pconfig->uid = simple_strtoul(value, &value, 0);
613                 else if (!strcmp(opt, "gid"))
614                         pconfig->gid = simple_strtoul(value, &value, 0);
615                 else if (!strcmp(opt, "mode"))
616                         pconfig->mode = simple_strtoul(value,&value,0) & 0777U;
617                 else if (!strcmp(opt, "size")) {
618                         unsigned long long size = memparse(value, &rest);
619                         if (*rest == '%') {
620                                 size <<= HPAGE_SHIFT;
621                                 size *= max_huge_pages;
622                                 do_div(size, 100);
623                                 rest++;
624                         }
625                         size &= HPAGE_MASK;
626                         pconfig->nr_blocks = (size >> HPAGE_SHIFT);
627                         value = rest;
628                 } else if (!strcmp(opt,"nr_inodes")) {
629                         pconfig->nr_inodes = memparse(value, &rest);
630                         value = rest;
631                 } else
632                         return -EINVAL;
633
634                 if (*value)
635                         return -EINVAL;
636         }
637         return 0;
638 }
639
640 static int
641 hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
642 {
643         struct inode * inode;
644         struct dentry * root;
645         int ret;
646         struct hugetlbfs_config config;
647         struct hugetlbfs_sb_info *sbinfo;
648
649         config.nr_blocks = -1; /* No limit on size by default */
650         config.nr_inodes = -1; /* No limit on number of inodes by default */
651         config.uid = current->fsuid;
652         config.gid = current->fsgid;
653         config.mode = 0755;
654         ret = hugetlbfs_parse_options(data, &config);
655
656         if (ret)
657                 return ret;
658
659         sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
660         if (!sbinfo)
661                 return -ENOMEM;
662         sb->s_fs_info = sbinfo;
663         spin_lock_init(&sbinfo->stat_lock);
664         sbinfo->max_blocks = config.nr_blocks;
665         sbinfo->free_blocks = config.nr_blocks;
666         sbinfo->max_inodes = config.nr_inodes;
667         sbinfo->free_inodes = config.nr_inodes;
668         sb->s_maxbytes = MAX_LFS_FILESIZE;
669         sb->s_blocksize = HPAGE_SIZE;
670         sb->s_blocksize_bits = HPAGE_SHIFT;
671         sb->s_magic = HUGETLBFS_MAGIC;
672         sb->s_op = &hugetlbfs_ops;
673         inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
674                                         S_IFDIR | config.mode, 0);
675         if (!inode)
676                 goto out_free;
677
678         root = d_alloc_root(inode);
679         if (!root) {
680                 iput(inode);
681                 goto out_free;
682         }
683         sb->s_root = root;
684         return 0;
685 out_free:
686         kfree(sbinfo);
687         return -ENOMEM;
688 }
689
690 int hugetlb_get_quota(struct address_space *mapping)
691 {
692         int ret = 0;
693         struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
694
695         if (sbinfo->free_blocks > -1) {
696                 spin_lock(&sbinfo->stat_lock);
697                 if (sbinfo->free_blocks > 0)
698                         sbinfo->free_blocks--;
699                 else
700                         ret = -ENOMEM;
701                 spin_unlock(&sbinfo->stat_lock);
702         }
703
704         return ret;
705 }
706
707 void hugetlb_put_quota(struct address_space *mapping)
708 {
709         struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
710
711         if (sbinfo->free_blocks > -1) {
712                 spin_lock(&sbinfo->stat_lock);
713                 sbinfo->free_blocks++;
714                 spin_unlock(&sbinfo->stat_lock);
715         }
716 }
717
718 static struct super_block *hugetlbfs_get_sb(struct file_system_type *fs_type,
719         int flags, const char *dev_name, void *data)
720 {
721         return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super);
722 }
723
724 static struct file_system_type hugetlbfs_fs_type = {
725         .name           = "hugetlbfs",
726         .get_sb         = hugetlbfs_get_sb,
727         .kill_sb        = kill_litter_super,
728 };
729
730 static struct vfsmount *hugetlbfs_vfsmount;
731
732 /*
733  * Return the next identifier for a shm file
734  */
735 static unsigned long hugetlbfs_counter(void)
736 {
737         static spinlock_t lock = SPIN_LOCK_UNLOCKED;
738         static unsigned long counter;
739         unsigned long ret;
740
741         spin_lock(&lock);
742         ret = ++counter;
743         spin_unlock(&lock);
744         return ret;
745 }
746
747 static int can_do_hugetlb_shm(void)
748 {
749         return likely(capable(CAP_IPC_LOCK) ||
750                         in_group_p(sysctl_hugetlb_shm_group) ||
751                         can_do_mlock());
752 }
753
754 struct file *hugetlb_zero_setup(size_t size)
755 {
756         int error = -ENOMEM;
757         struct file *file;
758         struct inode *inode;
759         struct dentry *dentry, *root;
760         struct qstr quick_string;
761         char buf[16];
762
763         if (!can_do_hugetlb_shm())
764                 return ERR_PTR(-EPERM);
765
766         if (!is_hugepage_mem_enough(size))
767                 return ERR_PTR(-ENOMEM);
768
769         if (!user_shm_lock(size, current->user))
770                 return ERR_PTR(-ENOMEM);
771
772         root = hugetlbfs_vfsmount->mnt_root;
773         snprintf(buf, 16, "%lu", hugetlbfs_counter());
774         quick_string.name = buf;
775         quick_string.len = strlen(quick_string.name);
776         quick_string.hash = 0;
777         dentry = d_alloc(root, &quick_string);
778         if (!dentry)
779                 goto out_shm_unlock;
780
781         error = -ENFILE;
782         file = get_empty_filp();
783         if (!file)
784                 goto out_dentry;
785
786         error = -ENOSPC;
787         inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
788                                 current->fsgid, S_IFREG | S_IRWXUGO, 0);
789         if (!inode)
790                 goto out_file;
791
792         d_instantiate(dentry, inode);
793         inode->i_size = size;
794         inode->i_nlink = 0;
795         file->f_vfsmnt = mntget(hugetlbfs_vfsmount);
796         file->f_dentry = dentry;
797         file->f_mapping = inode->i_mapping;
798         file->f_op = &hugetlbfs_file_operations;
799         file->f_mode = FMODE_WRITE | FMODE_READ;
800         return file;
801
802 out_file:
803         put_filp(file);
804 out_dentry:
805         dput(dentry);
806 out_shm_unlock:
807         user_shm_unlock(size, current->user);
808         return ERR_PTR(error);
809 }
810
811 static int __init init_hugetlbfs_fs(void)
812 {
813         int error;
814         struct vfsmount *vfsmount;
815
816         hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
817                                         sizeof(struct hugetlbfs_inode_info),
818                                         0, 0, init_once, NULL);
819         if (hugetlbfs_inode_cachep == NULL)
820                 return -ENOMEM;
821
822         error = register_filesystem(&hugetlbfs_fs_type);
823         if (error)
824                 goto out;
825
826         vfsmount = kern_mount(&hugetlbfs_fs_type);
827
828         if (!IS_ERR(vfsmount)) {
829                 hugetlbfs_vfsmount = vfsmount;
830                 return 0;
831         }
832
833         error = PTR_ERR(vfsmount);
834
835  out:
836         if (error)
837                 kmem_cache_destroy(hugetlbfs_inode_cachep);
838         return error;
839 }
840
841 static void __exit exit_hugetlbfs_fs(void)
842 {
843         kmem_cache_destroy(hugetlbfs_inode_cachep);
844         unregister_filesystem(&hugetlbfs_fs_type);
845 }
846
847 module_init(init_hugetlbfs_fs)
848 module_exit(exit_hugetlbfs_fs)
849
850 MODULE_LICENSE("GPL");