[PATCH] cleanup hugelbfs_forget_inode
[linux-block.git] / fs / hugetlbfs / inode.c
CommitLineData
1da177e4
LT
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
35static struct super_operations hugetlbfs_ops;
36static struct address_space_operations hugetlbfs_aops;
37struct file_operations hugetlbfs_file_operations;
38static struct inode_operations hugetlbfs_dir_inode_operations;
39static struct inode_operations hugetlbfs_inode_operations;
40
41static 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
46int sysctl_hugetlb_shm_group;
47
48static 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;
88out:
89 up(&inode->i_sem);
90
91 return ret;
92}
93
94/*
508034a3 95 * Called under down_write(mmap_sem).
1da177e4
LT
96 */
97
98#ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
99unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
100 unsigned long len, unsigned long pgoff, unsigned long flags);
101#else
102static unsigned long
103hugetlb_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
1363c3cd
WW
125 if (len <= mm->cached_hole_size)
126 start_addr = TASK_UNMAPPED_BASE;
127
1da177e4
LT
128full_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 */
156static int hugetlbfs_readpage(struct file *file, struct page * page)
157{
158 unlock_page(page);
159 return -EINVAL;
160}
161
162static int hugetlbfs_prepare_write(struct file *file,
163 struct page *page, unsigned offset, unsigned to)
164{
165 return -EINVAL;
166}
167
168static int hugetlbfs_commit_write(struct file *file,
169 struct page *page, unsigned offset, unsigned to)
170{
171 return -EINVAL;
172}
173
174static 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
184static 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
192static 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
225static void hugetlbfs_delete_inode(struct inode *inode)
226{
149f4211
CH
227 if (inode->i_data.nrpages)
228 truncate_hugepages(&inode->i_data, 0);
229 clear_inode(inode);
230}
231
1da177e4
LT
232static void hugetlbfs_forget_inode(struct inode *inode)
233{
0b1533f6
CH
234 struct super_block *sb = inode->i_sb;
235
236 if (!hlist_unhashed(&inode->i_hash)) {
237 if (!(inode->i_state & (I_DIRTY|I_LOCK)))
238 list_move(&inode->i_list, &inode_unused);
239 inodes_stat.nr_unused++;
240 if (!sb || (sb->s_flags & MS_ACTIVE)) {
241 spin_unlock(&inode_lock);
242 return;
243 }
244 inode->i_state |= I_WILL_FREE;
1da177e4 245 spin_unlock(&inode_lock);
0b1533f6
CH
246 /*
247 * write_inode_now is a noop as we set BDI_CAP_NO_WRITEBACK
248 * in our backing_dev_info.
249 */
250 write_inode_now(inode, 1);
251 spin_lock(&inode_lock);
252 inode->i_state &= ~I_WILL_FREE;
253 inodes_stat.nr_unused--;
254 hlist_del_init(&inode->i_hash);
1da177e4 255 }
1da177e4
LT
256 list_del_init(&inode->i_list);
257 list_del_init(&inode->i_sb_list);
258 inode->i_state |= I_FREEING;
259 inodes_stat.nr_inodes--;
260 spin_unlock(&inode_lock);
261 if (inode->i_data.nrpages)
262 truncate_hugepages(&inode->i_data, 0);
1da177e4
LT
263 clear_inode(inode);
264 destroy_inode(inode);
265}
266
267static void hugetlbfs_drop_inode(struct inode *inode)
268{
269 if (!inode->i_nlink)
6b09b9df 270 generic_delete_inode(inode);
1da177e4
LT
271 else
272 hugetlbfs_forget_inode(inode);
273}
274
275/*
276 * h_pgoff is in HPAGE_SIZE units.
277 * vma->vm_pgoff is in PAGE_SIZE units.
278 */
279static inline void
280hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
281{
282 struct vm_area_struct *vma;
283 struct prio_tree_iter iter;
284
285 vma_prio_tree_foreach(vma, &iter, root, h_pgoff, ULONG_MAX) {
286 unsigned long h_vm_pgoff;
1da177e4
LT
287 unsigned long v_offset;
288
289 h_vm_pgoff = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
290 v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT;
291 /*
292 * Is this VMA fully outside the truncation point?
293 */
294 if (h_vm_pgoff >= h_pgoff)
295 v_offset = 0;
296
508034a3
HD
297 unmap_hugepage_range(vma,
298 vma->vm_start + v_offset, vma->vm_end);
1da177e4
LT
299 }
300}
301
302/*
303 * Expanding truncates are not allowed.
304 */
305static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
306{
307 unsigned long pgoff;
308 struct address_space *mapping = inode->i_mapping;
309
310 if (offset > inode->i_size)
311 return -EINVAL;
312
313 BUG_ON(offset & ~HPAGE_MASK);
314 pgoff = offset >> HPAGE_SHIFT;
315
316 inode->i_size = offset;
317 spin_lock(&mapping->i_mmap_lock);
318 if (!prio_tree_empty(&mapping->i_mmap))
319 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
320 spin_unlock(&mapping->i_mmap_lock);
321 truncate_hugepages(mapping, offset);
322 return 0;
323}
324
325static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
326{
327 struct inode *inode = dentry->d_inode;
328 int error;
329 unsigned int ia_valid = attr->ia_valid;
330
331 BUG_ON(!inode);
332
333 error = inode_change_ok(inode, attr);
334 if (error)
335 goto out;
336
337 if (ia_valid & ATTR_SIZE) {
338 error = -EINVAL;
339 if (!(attr->ia_size & ~HPAGE_MASK))
340 error = hugetlb_vmtruncate(inode, attr->ia_size);
341 if (error)
342 goto out;
343 attr->ia_valid &= ~ATTR_SIZE;
344 }
345 error = inode_setattr(inode, attr);
346out:
347 return error;
348}
349
350static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
351 gid_t gid, int mode, dev_t dev)
352{
353 struct inode *inode;
1da177e4
LT
354
355 inode = new_inode(sb);
356 if (inode) {
357 struct hugetlbfs_inode_info *info;
358 inode->i_mode = mode;
359 inode->i_uid = uid;
360 inode->i_gid = gid;
361 inode->i_blksize = HPAGE_SIZE;
362 inode->i_blocks = 0;
363 inode->i_mapping->a_ops = &hugetlbfs_aops;
364 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
365 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
366 info = HUGETLBFS_I(inode);
367 mpol_shared_policy_init(&info->policy);
368 switch (mode & S_IFMT) {
369 default:
370 init_special_inode(inode, mode, dev);
371 break;
372 case S_IFREG:
373 inode->i_op = &hugetlbfs_inode_operations;
374 inode->i_fop = &hugetlbfs_file_operations;
375 break;
376 case S_IFDIR:
377 inode->i_op = &hugetlbfs_dir_inode_operations;
378 inode->i_fop = &simple_dir_operations;
379
380 /* directory inodes start off with i_nlink == 2 (for "." entry) */
381 inode->i_nlink++;
382 break;
383 case S_IFLNK:
384 inode->i_op = &page_symlink_inode_operations;
385 break;
386 }
387 }
388 return inode;
389}
390
391/*
392 * File creation. Allocate an inode, and we're done..
393 */
394static int hugetlbfs_mknod(struct inode *dir,
395 struct dentry *dentry, int mode, dev_t dev)
396{
397 struct inode *inode;
398 int error = -ENOSPC;
399 gid_t gid;
400
401 if (dir->i_mode & S_ISGID) {
402 gid = dir->i_gid;
403 if (S_ISDIR(mode))
404 mode |= S_ISGID;
405 } else {
406 gid = current->fsgid;
407 }
408 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
409 if (inode) {
410 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
411 d_instantiate(dentry, inode);
412 dget(dentry); /* Extra count - pin the dentry in core */
413 error = 0;
414 }
415 return error;
416}
417
418static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
419{
420 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
421 if (!retval)
422 dir->i_nlink++;
423 return retval;
424}
425
426static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
427{
428 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
429}
430
431static int hugetlbfs_symlink(struct inode *dir,
432 struct dentry *dentry, const char *symname)
433{
434 struct inode *inode;
435 int error = -ENOSPC;
436 gid_t gid;
437
438 if (dir->i_mode & S_ISGID)
439 gid = dir->i_gid;
440 else
441 gid = current->fsgid;
442
443 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
444 gid, S_IFLNK|S_IRWXUGO, 0);
445 if (inode) {
446 int l = strlen(symname)+1;
447 error = page_symlink(inode, symname, l);
448 if (!error) {
449 d_instantiate(dentry, inode);
450 dget(dentry);
451 } else
452 iput(inode);
453 }
454 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
455
456 return error;
457}
458
459/*
460 * For direct-IO reads into hugetlb pages
461 */
462static int hugetlbfs_set_page_dirty(struct page *page)
463{
464 return 0;
465}
466
467static int hugetlbfs_statfs(struct super_block *sb, struct kstatfs *buf)
468{
469 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
470
471 buf->f_type = HUGETLBFS_MAGIC;
472 buf->f_bsize = HPAGE_SIZE;
473 if (sbinfo) {
474 spin_lock(&sbinfo->stat_lock);
475 buf->f_blocks = sbinfo->max_blocks;
476 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
477 buf->f_files = sbinfo->max_inodes;
478 buf->f_ffree = sbinfo->free_inodes;
479 spin_unlock(&sbinfo->stat_lock);
480 }
481 buf->f_namelen = NAME_MAX;
482 return 0;
483}
484
485static void hugetlbfs_put_super(struct super_block *sb)
486{
487 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
488
489 if (sbi) {
490 sb->s_fs_info = NULL;
491 kfree(sbi);
492 }
493}
494
96527980
CH
495static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
496{
497 if (sbinfo->free_inodes >= 0) {
498 spin_lock(&sbinfo->stat_lock);
499 if (unlikely(!sbinfo->free_inodes)) {
500 spin_unlock(&sbinfo->stat_lock);
501 return 0;
502 }
503 sbinfo->free_inodes--;
504 spin_unlock(&sbinfo->stat_lock);
505 }
506
507 return 1;
508}
509
510static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
511{
512 if (sbinfo->free_inodes >= 0) {
513 spin_lock(&sbinfo->stat_lock);
514 sbinfo->free_inodes++;
515 spin_unlock(&sbinfo->stat_lock);
516 }
517}
518
519
1da177e4
LT
520static kmem_cache_t *hugetlbfs_inode_cachep;
521
522static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
523{
96527980 524 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
1da177e4
LT
525 struct hugetlbfs_inode_info *p;
526
96527980
CH
527 if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
528 return NULL;
1da177e4 529 p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
96527980
CH
530 if (unlikely(!p)) {
531 hugetlbfs_inc_free_inodes(sbinfo);
1da177e4 532 return NULL;
96527980 533 }
1da177e4
LT
534 return &p->vfs_inode;
535}
536
1da177e4
LT
537static void hugetlbfs_destroy_inode(struct inode *inode)
538{
96527980 539 hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
1da177e4
LT
540 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
541 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
542}
543
544static struct address_space_operations hugetlbfs_aops = {
545 .readpage = hugetlbfs_readpage,
546 .prepare_write = hugetlbfs_prepare_write,
547 .commit_write = hugetlbfs_commit_write,
548 .set_page_dirty = hugetlbfs_set_page_dirty,
549};
550
96527980
CH
551
552static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
553{
554 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
555
556 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
557 SLAB_CTOR_CONSTRUCTOR)
558 inode_init_once(&ei->vfs_inode);
559}
560
1da177e4
LT
561struct file_operations hugetlbfs_file_operations = {
562 .mmap = hugetlbfs_file_mmap,
563 .fsync = simple_sync_file,
564 .get_unmapped_area = hugetlb_get_unmapped_area,
565};
566
567static struct inode_operations hugetlbfs_dir_inode_operations = {
568 .create = hugetlbfs_create,
569 .lookup = simple_lookup,
570 .link = simple_link,
571 .unlink = simple_unlink,
572 .symlink = hugetlbfs_symlink,
573 .mkdir = hugetlbfs_mkdir,
574 .rmdir = simple_rmdir,
575 .mknod = hugetlbfs_mknod,
576 .rename = simple_rename,
577 .setattr = hugetlbfs_setattr,
578};
579
580static struct inode_operations hugetlbfs_inode_operations = {
581 .setattr = hugetlbfs_setattr,
582};
583
584static struct super_operations hugetlbfs_ops = {
585 .alloc_inode = hugetlbfs_alloc_inode,
586 .destroy_inode = hugetlbfs_destroy_inode,
587 .statfs = hugetlbfs_statfs,
149f4211 588 .delete_inode = hugetlbfs_delete_inode,
1da177e4
LT
589 .drop_inode = hugetlbfs_drop_inode,
590 .put_super = hugetlbfs_put_super,
591};
592
593static int
594hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
595{
596 char *opt, *value, *rest;
597
598 if (!options)
599 return 0;
600 while ((opt = strsep(&options, ",")) != NULL) {
601 if (!*opt)
602 continue;
603
604 value = strchr(opt, '=');
605 if (!value || !*value)
606 return -EINVAL;
607 else
608 *value++ = '\0';
609
610 if (!strcmp(opt, "uid"))
611 pconfig->uid = simple_strtoul(value, &value, 0);
612 else if (!strcmp(opt, "gid"))
613 pconfig->gid = simple_strtoul(value, &value, 0);
614 else if (!strcmp(opt, "mode"))
615 pconfig->mode = simple_strtoul(value,&value,0) & 0777U;
616 else if (!strcmp(opt, "size")) {
617 unsigned long long size = memparse(value, &rest);
618 if (*rest == '%') {
619 size <<= HPAGE_SHIFT;
620 size *= max_huge_pages;
621 do_div(size, 100);
622 rest++;
623 }
624 size &= HPAGE_MASK;
625 pconfig->nr_blocks = (size >> HPAGE_SHIFT);
626 value = rest;
627 } else if (!strcmp(opt,"nr_inodes")) {
628 pconfig->nr_inodes = memparse(value, &rest);
629 value = rest;
630 } else
631 return -EINVAL;
632
633 if (*value)
634 return -EINVAL;
635 }
636 return 0;
637}
638
639static int
640hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
641{
642 struct inode * inode;
643 struct dentry * root;
644 int ret;
645 struct hugetlbfs_config config;
646 struct hugetlbfs_sb_info *sbinfo;
647
648 config.nr_blocks = -1; /* No limit on size by default */
649 config.nr_inodes = -1; /* No limit on number of inodes by default */
650 config.uid = current->fsuid;
651 config.gid = current->fsgid;
652 config.mode = 0755;
653 ret = hugetlbfs_parse_options(data, &config);
654
655 if (ret)
656 return ret;
657
658 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
659 if (!sbinfo)
660 return -ENOMEM;
661 sb->s_fs_info = sbinfo;
662 spin_lock_init(&sbinfo->stat_lock);
663 sbinfo->max_blocks = config.nr_blocks;
664 sbinfo->free_blocks = config.nr_blocks;
665 sbinfo->max_inodes = config.nr_inodes;
666 sbinfo->free_inodes = config.nr_inodes;
667 sb->s_maxbytes = MAX_LFS_FILESIZE;
668 sb->s_blocksize = HPAGE_SIZE;
669 sb->s_blocksize_bits = HPAGE_SHIFT;
670 sb->s_magic = HUGETLBFS_MAGIC;
671 sb->s_op = &hugetlbfs_ops;
672 sb->s_time_gran = 1;
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;
685out_free:
686 kfree(sbinfo);
687 return -ENOMEM;
688}
689
690int 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
707void 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
718static 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
724static struct file_system_type hugetlbfs_fs_type = {
725 .name = "hugetlbfs",
726 .get_sb = hugetlbfs_get_sb,
727 .kill_sb = kill_litter_super,
728};
729
730static struct vfsmount *hugetlbfs_vfsmount;
731
732/*
733 * Return the next identifier for a shm file
734 */
735static unsigned long hugetlbfs_counter(void)
736{
737 static DEFINE_SPINLOCK(lock);
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
747static 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
754struct 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
802out_file:
803 put_filp(file);
804out_dentry:
805 dput(dentry);
806out_shm_unlock:
807 user_shm_unlock(size, current->user);
808 return ERR_PTR(error);
809}
810
811static 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
841static void __exit exit_hugetlbfs_fs(void)
842{
843 kmem_cache_destroy(hugetlbfs_inode_cachep);
844 unregister_filesystem(&hugetlbfs_fs_type);
845}
846
847module_init(init_hugetlbfs_fs)
848module_exit(exit_hugetlbfs_fs)
849
850MODULE_LICENSE("GPL");