aboutsummaryrefslogtreecommitdiffstats
path: root/fs/hugetlbfs
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /fs/hugetlbfs
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'fs/hugetlbfs')
-rw-r--r--fs/hugetlbfs/Makefile7
-rw-r--r--fs/hugetlbfs/inode.c853
2 files changed, 860 insertions, 0 deletions
diff --git a/fs/hugetlbfs/Makefile b/fs/hugetlbfs/Makefile
new file mode 100644
index 000000000000..6adf870c63c6
--- /dev/null
+++ b/fs/hugetlbfs/Makefile
@@ -0,0 +1,7 @@
1#
2# Makefile for the linux ramfs routines.
3#
4
5obj-$(CONFIG_HUGETLBFS) += hugetlbfs.o
6
7hugetlbfs-objs := inode.o
diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
new file mode 100644
index 000000000000..2af3338f891b
--- /dev/null
+++ b/fs/hugetlbfs/inode.c
@@ -0,0 +1,853 @@
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/*
95 * Called under down_write(mmap_sem), page_table_lock is not held
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
125full_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 */
153static int hugetlbfs_readpage(struct file *file, struct page * page)
154{
155 unlock_page(page);
156 return -EINVAL;
157}
158
159static int hugetlbfs_prepare_write(struct file *file,
160 struct page *page, unsigned offset, unsigned to)
161{
162 return -EINVAL;
163}
164
165static int hugetlbfs_commit_write(struct file *file,
166 struct page *page, unsigned offset, unsigned to)
167{
168 return -EINVAL;
169}
170
171static 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
181static 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
189static 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
222static 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 list_del_init(&inode->i_sb_list);
229 inode->i_state |= I_FREEING;
230 inodes_stat.nr_inodes--;
231 spin_unlock(&inode_lock);
232
233 if (inode->i_data.nrpages)
234 truncate_hugepages(&inode->i_data, 0);
235
236 security_inode_delete(inode);
237
238 if (sbinfo->free_inodes >= 0) {
239 spin_lock(&sbinfo->stat_lock);
240 sbinfo->free_inodes++;
241 spin_unlock(&sbinfo->stat_lock);
242 }
243
244 clear_inode(inode);
245 destroy_inode(inode);
246}
247
248static void hugetlbfs_forget_inode(struct inode *inode)
249{
250 struct super_block *super_block = inode->i_sb;
251 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(super_block);
252
253 if (hlist_unhashed(&inode->i_hash))
254 goto out_truncate;
255
256 if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
257 list_del(&inode->i_list);
258 list_add(&inode->i_list, &inode_unused);
259 }
260 inodes_stat.nr_unused++;
261 if (!super_block || (super_block->s_flags & MS_ACTIVE)) {
262 spin_unlock(&inode_lock);
263 return;
264 }
265
266 /* write_inode_now() ? */
267 inodes_stat.nr_unused--;
268 hlist_del_init(&inode->i_hash);
269out_truncate:
270 list_del_init(&inode->i_list);
271 list_del_init(&inode->i_sb_list);
272 inode->i_state |= I_FREEING;
273 inodes_stat.nr_inodes--;
274 spin_unlock(&inode_lock);
275 if (inode->i_data.nrpages)
276 truncate_hugepages(&inode->i_data, 0);
277
278 if (sbinfo->free_inodes >= 0) {
279 spin_lock(&sbinfo->stat_lock);
280 sbinfo->free_inodes++;
281 spin_unlock(&sbinfo->stat_lock);
282 }
283
284 clear_inode(inode);
285 destroy_inode(inode);
286}
287
288static void hugetlbfs_drop_inode(struct inode *inode)
289{
290 if (!inode->i_nlink)
291 hugetlbfs_delete_inode(inode);
292 else
293 hugetlbfs_forget_inode(inode);
294}
295
296/*
297 * h_pgoff is in HPAGE_SIZE units.
298 * vma->vm_pgoff is in PAGE_SIZE units.
299 */
300static inline void
301hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
302{
303 struct vm_area_struct *vma;
304 struct prio_tree_iter iter;
305
306 vma_prio_tree_foreach(vma, &iter, root, h_pgoff, ULONG_MAX) {
307 unsigned long h_vm_pgoff;
308 unsigned long v_length;
309 unsigned long v_offset;
310
311 h_vm_pgoff = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
312 v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT;
313 /*
314 * Is this VMA fully outside the truncation point?
315 */
316 if (h_vm_pgoff >= h_pgoff)
317 v_offset = 0;
318
319 v_length = vma->vm_end - vma->vm_start;
320
321 zap_hugepage_range(vma,
322 vma->vm_start + v_offset,
323 v_length - v_offset);
324 }
325}
326
327/*
328 * Expanding truncates are not allowed.
329 */
330static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
331{
332 unsigned long pgoff;
333 struct address_space *mapping = inode->i_mapping;
334
335 if (offset > inode->i_size)
336 return -EINVAL;
337
338 BUG_ON(offset & ~HPAGE_MASK);
339 pgoff = offset >> HPAGE_SHIFT;
340
341 inode->i_size = offset;
342 spin_lock(&mapping->i_mmap_lock);
343 if (!prio_tree_empty(&mapping->i_mmap))
344 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
345 spin_unlock(&mapping->i_mmap_lock);
346 truncate_hugepages(mapping, offset);
347 return 0;
348}
349
350static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
351{
352 struct inode *inode = dentry->d_inode;
353 int error;
354 unsigned int ia_valid = attr->ia_valid;
355
356 BUG_ON(!inode);
357
358 error = inode_change_ok(inode, attr);
359 if (error)
360 goto out;
361
362 if (ia_valid & ATTR_SIZE) {
363 error = -EINVAL;
364 if (!(attr->ia_size & ~HPAGE_MASK))
365 error = hugetlb_vmtruncate(inode, attr->ia_size);
366 if (error)
367 goto out;
368 attr->ia_valid &= ~ATTR_SIZE;
369 }
370 error = inode_setattr(inode, attr);
371out:
372 return error;
373}
374
375static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
376 gid_t gid, int mode, dev_t dev)
377{
378 struct inode *inode;
379 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
380
381 if (sbinfo->free_inodes >= 0) {
382 spin_lock(&sbinfo->stat_lock);
383 if (!sbinfo->free_inodes) {
384 spin_unlock(&sbinfo->stat_lock);
385 return NULL;
386 }
387 sbinfo->free_inodes--;
388 spin_unlock(&sbinfo->stat_lock);
389 }
390
391 inode = new_inode(sb);
392 if (inode) {
393 struct hugetlbfs_inode_info *info;
394 inode->i_mode = mode;
395 inode->i_uid = uid;
396 inode->i_gid = gid;
397 inode->i_blksize = HPAGE_SIZE;
398 inode->i_blocks = 0;
399 inode->i_mapping->a_ops = &hugetlbfs_aops;
400 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
401 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
402 info = HUGETLBFS_I(inode);
403 mpol_shared_policy_init(&info->policy);
404 switch (mode & S_IFMT) {
405 default:
406 init_special_inode(inode, mode, dev);
407 break;
408 case S_IFREG:
409 inode->i_op = &hugetlbfs_inode_operations;
410 inode->i_fop = &hugetlbfs_file_operations;
411 break;
412 case S_IFDIR:
413 inode->i_op = &hugetlbfs_dir_inode_operations;
414 inode->i_fop = &simple_dir_operations;
415
416 /* directory inodes start off with i_nlink == 2 (for "." entry) */
417 inode->i_nlink++;
418 break;
419 case S_IFLNK:
420 inode->i_op = &page_symlink_inode_operations;
421 break;
422 }
423 }
424 return inode;
425}
426
427/*
428 * File creation. Allocate an inode, and we're done..
429 */
430static int hugetlbfs_mknod(struct inode *dir,
431 struct dentry *dentry, int mode, dev_t dev)
432{
433 struct inode *inode;
434 int error = -ENOSPC;
435 gid_t gid;
436
437 if (dir->i_mode & S_ISGID) {
438 gid = dir->i_gid;
439 if (S_ISDIR(mode))
440 mode |= S_ISGID;
441 } else {
442 gid = current->fsgid;
443 }
444 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
445 if (inode) {
446 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
447 d_instantiate(dentry, inode);
448 dget(dentry); /* Extra count - pin the dentry in core */
449 error = 0;
450 }
451 return error;
452}
453
454static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
455{
456 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
457 if (!retval)
458 dir->i_nlink++;
459 return retval;
460}
461
462static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
463{
464 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
465}
466
467static int hugetlbfs_symlink(struct inode *dir,
468 struct dentry *dentry, const char *symname)
469{
470 struct inode *inode;
471 int error = -ENOSPC;
472 gid_t gid;
473
474 if (dir->i_mode & S_ISGID)
475 gid = dir->i_gid;
476 else
477 gid = current->fsgid;
478
479 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
480 gid, S_IFLNK|S_IRWXUGO, 0);
481 if (inode) {
482 int l = strlen(symname)+1;
483 error = page_symlink(inode, symname, l);
484 if (!error) {
485 d_instantiate(dentry, inode);
486 dget(dentry);
487 } else
488 iput(inode);
489 }
490 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
491
492 return error;
493}
494
495/*
496 * For direct-IO reads into hugetlb pages
497 */
498static int hugetlbfs_set_page_dirty(struct page *page)
499{
500 return 0;
501}
502
503static int hugetlbfs_statfs(struct super_block *sb, struct kstatfs *buf)
504{
505 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
506
507 buf->f_type = HUGETLBFS_MAGIC;
508 buf->f_bsize = HPAGE_SIZE;
509 if (sbinfo) {
510 spin_lock(&sbinfo->stat_lock);
511 buf->f_blocks = sbinfo->max_blocks;
512 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
513 buf->f_files = sbinfo->max_inodes;
514 buf->f_ffree = sbinfo->free_inodes;
515 spin_unlock(&sbinfo->stat_lock);
516 }
517 buf->f_namelen = NAME_MAX;
518 return 0;
519}
520
521static void hugetlbfs_put_super(struct super_block *sb)
522{
523 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
524
525 if (sbi) {
526 sb->s_fs_info = NULL;
527 kfree(sbi);
528 }
529}
530
531static kmem_cache_t *hugetlbfs_inode_cachep;
532
533static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
534{
535 struct hugetlbfs_inode_info *p;
536
537 p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
538 if (!p)
539 return NULL;
540 return &p->vfs_inode;
541}
542
543static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
544{
545 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
546
547 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
548 SLAB_CTOR_CONSTRUCTOR)
549 inode_init_once(&ei->vfs_inode);
550}
551
552static void hugetlbfs_destroy_inode(struct inode *inode)
553{
554 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
555 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
556}
557
558static struct address_space_operations hugetlbfs_aops = {
559 .readpage = hugetlbfs_readpage,
560 .prepare_write = hugetlbfs_prepare_write,
561 .commit_write = hugetlbfs_commit_write,
562 .set_page_dirty = hugetlbfs_set_page_dirty,
563};
564
565struct file_operations hugetlbfs_file_operations = {
566 .mmap = hugetlbfs_file_mmap,
567 .fsync = simple_sync_file,
568 .get_unmapped_area = hugetlb_get_unmapped_area,
569};
570
571static struct inode_operations hugetlbfs_dir_inode_operations = {
572 .create = hugetlbfs_create,
573 .lookup = simple_lookup,
574 .link = simple_link,
575 .unlink = simple_unlink,
576 .symlink = hugetlbfs_symlink,
577 .mkdir = hugetlbfs_mkdir,
578 .rmdir = simple_rmdir,
579 .mknod = hugetlbfs_mknod,
580 .rename = simple_rename,
581 .setattr = hugetlbfs_setattr,
582};
583
584static struct inode_operations hugetlbfs_inode_operations = {
585 .setattr = hugetlbfs_setattr,
586};
587
588static struct super_operations hugetlbfs_ops = {
589 .alloc_inode = hugetlbfs_alloc_inode,
590 .destroy_inode = hugetlbfs_destroy_inode,
591 .statfs = hugetlbfs_statfs,
592 .drop_inode = hugetlbfs_drop_inode,
593 .put_super = hugetlbfs_put_super,
594};
595
596static int
597hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
598{
599 char *opt, *value, *rest;
600
601 if (!options)
602 return 0;
603 while ((opt = strsep(&options, ",")) != NULL) {
604 if (!*opt)
605 continue;
606
607 value = strchr(opt, '=');
608 if (!value || !*value)
609 return -EINVAL;
610 else
611 *value++ = '\0';
612
613 if (!strcmp(opt, "uid"))
614 pconfig->uid = simple_strtoul(value, &value, 0);
615 else if (!strcmp(opt, "gid"))
616 pconfig->gid = simple_strtoul(value, &value, 0);
617 else if (!strcmp(opt, "mode"))
618 pconfig->mode = simple_strtoul(value,&value,0) & 0777U;
619 else if (!strcmp(opt, "size")) {
620 unsigned long long size = memparse(value, &rest);
621 if (*rest == '%') {
622 size <<= HPAGE_SHIFT;
623 size *= max_huge_pages;
624 do_div(size, 100);
625 rest++;
626 }
627 size &= HPAGE_MASK;
628 pconfig->nr_blocks = (size >> HPAGE_SHIFT);
629 value = rest;
630 } else if (!strcmp(opt,"nr_inodes")) {
631 pconfig->nr_inodes = memparse(value, &rest);
632 value = rest;
633 } else
634 return -EINVAL;
635
636 if (*value)
637 return -EINVAL;
638 }
639 return 0;
640}
641
642static int
643hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
644{
645 struct inode * inode;
646 struct dentry * root;
647 int ret;
648 struct hugetlbfs_config config;
649 struct hugetlbfs_sb_info *sbinfo;
650
651 config.nr_blocks = -1; /* No limit on size by default */
652 config.nr_inodes = -1; /* No limit on number of inodes by default */
653 config.uid = current->fsuid;
654 config.gid = current->fsgid;
655 config.mode = 0755;
656 ret = hugetlbfs_parse_options(data, &config);
657
658 if (ret)
659 return ret;
660
661 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
662 if (!sbinfo)
663 return -ENOMEM;
664 sb->s_fs_info = sbinfo;
665 spin_lock_init(&sbinfo->stat_lock);
666 sbinfo->max_blocks = config.nr_blocks;
667 sbinfo->free_blocks = config.nr_blocks;
668 sbinfo->max_inodes = config.nr_inodes;
669 sbinfo->free_inodes = config.nr_inodes;
670 sb->s_maxbytes = MAX_LFS_FILESIZE;
671 sb->s_blocksize = HPAGE_SIZE;
672 sb->s_blocksize_bits = HPAGE_SHIFT;
673 sb->s_magic = HUGETLBFS_MAGIC;
674 sb->s_op = &hugetlbfs_ops;
675 sb->s_time_gran = 1;
676 inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
677 S_IFDIR | config.mode, 0);
678 if (!inode)
679 goto out_free;
680
681 root = d_alloc_root(inode);
682 if (!root) {
683 iput(inode);
684 goto out_free;
685 }
686 sb->s_root = root;
687 return 0;
688out_free:
689 kfree(sbinfo);
690 return -ENOMEM;
691}
692
693int hugetlb_get_quota(struct address_space *mapping)
694{
695 int ret = 0;
696 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
697
698 if (sbinfo->free_blocks > -1) {
699 spin_lock(&sbinfo->stat_lock);
700 if (sbinfo->free_blocks > 0)
701 sbinfo->free_blocks--;
702 else
703 ret = -ENOMEM;
704 spin_unlock(&sbinfo->stat_lock);
705 }
706
707 return ret;
708}
709
710void hugetlb_put_quota(struct address_space *mapping)
711{
712 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
713
714 if (sbinfo->free_blocks > -1) {
715 spin_lock(&sbinfo->stat_lock);
716 sbinfo->free_blocks++;
717 spin_unlock(&sbinfo->stat_lock);
718 }
719}
720
721static struct super_block *hugetlbfs_get_sb(struct file_system_type *fs_type,
722 int flags, const char *dev_name, void *data)
723{
724 return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super);
725}
726
727static struct file_system_type hugetlbfs_fs_type = {
728 .name = "hugetlbfs",
729 .get_sb = hugetlbfs_get_sb,
730 .kill_sb = kill_litter_super,
731};
732
733static struct vfsmount *hugetlbfs_vfsmount;
734
735/*
736 * Return the next identifier for a shm file
737 */
738static unsigned long hugetlbfs_counter(void)
739{
740 static DEFINE_SPINLOCK(lock);
741 static unsigned long counter;
742 unsigned long ret;
743
744 spin_lock(&lock);
745 ret = ++counter;
746 spin_unlock(&lock);
747 return ret;
748}
749
750static int can_do_hugetlb_shm(void)
751{
752 return likely(capable(CAP_IPC_LOCK) ||
753 in_group_p(sysctl_hugetlb_shm_group) ||
754 can_do_mlock());
755}
756
757struct file *hugetlb_zero_setup(size_t size)
758{
759 int error = -ENOMEM;
760 struct file *file;
761 struct inode *inode;
762 struct dentry *dentry, *root;
763 struct qstr quick_string;
764 char buf[16];
765
766 if (!can_do_hugetlb_shm())
767 return ERR_PTR(-EPERM);
768
769 if (!is_hugepage_mem_enough(size))
770 return ERR_PTR(-ENOMEM);
771
772 if (!user_shm_lock(size, current->user))
773 return ERR_PTR(-ENOMEM);
774
775 root = hugetlbfs_vfsmount->mnt_root;
776 snprintf(buf, 16, "%lu", hugetlbfs_counter());
777 quick_string.name = buf;
778 quick_string.len = strlen(quick_string.name);
779 quick_string.hash = 0;
780 dentry = d_alloc(root, &quick_string);
781 if (!dentry)
782 goto out_shm_unlock;
783
784 error = -ENFILE;
785 file = get_empty_filp();
786 if (!file)
787 goto out_dentry;
788
789 error = -ENOSPC;
790 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
791 current->fsgid, S_IFREG | S_IRWXUGO, 0);
792 if (!inode)
793 goto out_file;
794
795 d_instantiate(dentry, inode);
796 inode->i_size = size;
797 inode->i_nlink = 0;
798 file->f_vfsmnt = mntget(hugetlbfs_vfsmount);
799 file->f_dentry = dentry;
800 file->f_mapping = inode->i_mapping;
801 file->f_op = &hugetlbfs_file_operations;
802 file->f_mode = FMODE_WRITE | FMODE_READ;
803 return file;
804
805out_file:
806 put_filp(file);
807out_dentry:
808 dput(dentry);
809out_shm_unlock:
810 user_shm_unlock(size, current->user);
811 return ERR_PTR(error);
812}
813
814static int __init init_hugetlbfs_fs(void)
815{
816 int error;
817 struct vfsmount *vfsmount;
818
819 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
820 sizeof(struct hugetlbfs_inode_info),
821 0, 0, init_once, NULL);
822 if (hugetlbfs_inode_cachep == NULL)
823 return -ENOMEM;
824
825 error = register_filesystem(&hugetlbfs_fs_type);
826 if (error)
827 goto out;
828
829 vfsmount = kern_mount(&hugetlbfs_fs_type);
830
831 if (!IS_ERR(vfsmount)) {
832 hugetlbfs_vfsmount = vfsmount;
833 return 0;
834 }
835
836 error = PTR_ERR(vfsmount);
837
838 out:
839 if (error)
840 kmem_cache_destroy(hugetlbfs_inode_cachep);
841 return error;
842}
843
844static void __exit exit_hugetlbfs_fs(void)
845{
846 kmem_cache_destroy(hugetlbfs_inode_cachep);
847 unregister_filesystem(&hugetlbfs_fs_type);
848}
849
850module_init(init_hugetlbfs_fs)
851module_exit(exit_hugetlbfs_fs)
852
853MODULE_LICENSE("GPL");