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