1 /* 2 * hugetlbpage-backed filesystem. Based on ramfs. 3 * 4 * William Irwin, 2002 5 * 6 * Copyright (C) 2002 Linus Torvalds. 7 */ 8 9 #include <linux/module.h> 10 #include <linux/thread_info.h> 11 #include <asm/current.h> 12 #include <linux/sched.h> /* remove ASAP */ 13 #include <linux/fs.h> 14 #include <linux/mount.h> 15 #include <linux/file.h> 16 #include <linux/writeback.h> 17 #include <linux/pagemap.h> 18 #include <linux/highmem.h> 19 #include <linux/init.h> 20 #include <linux/string.h> 21 #include <linux/backing-dev.h> 22 #include <linux/hugetlb.h> 23 #include <linux/pagevec.h> 24 #include <linux/quotaops.h> 25 #include <linux/slab.h> 26 #include <linux/dnotify.h> 27 #include <linux/statfs.h> 28 #include <linux/security.h> 29 30 #include <asm/uaccess.h> 31 32 /* some random number */ 33 #define HUGETLBFS_MAGIC 0x958458f6 34 35 static struct super_operations hugetlbfs_ops; 36 static struct address_space_operations hugetlbfs_aops; 37 struct file_operations hugetlbfs_file_operations; 38 static struct inode_operations hugetlbfs_dir_inode_operations; 39 static struct inode_operations hugetlbfs_inode_operations; 40 41 static struct backing_dev_info hugetlbfs_backing_dev_info = { 42 .ra_pages = 0, /* No readahead */ 43 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK, 44 }; 45 46 int sysctl_hugetlb_shm_group; 47 48 static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) 49 { 50 struct inode *inode = file->f_dentry->d_inode; 51 struct address_space *mapping = inode->i_mapping; 52 loff_t len, vma_len; 53 int ret; 54 55 if ((vma->vm_flags & (VM_MAYSHARE | VM_WRITE)) == VM_WRITE) 56 return -EINVAL; 57 58 if (vma->vm_pgoff & (HPAGE_SIZE / PAGE_SIZE - 1)) 59 return -EINVAL; 60 61 if (vma->vm_start & ~HPAGE_MASK) 62 return -EINVAL; 63 64 if (vma->vm_end & ~HPAGE_MASK) 65 return -EINVAL; 66 67 if (vma->vm_end - vma->vm_start < HPAGE_SIZE) 68 return -EINVAL; 69 70 vma_len = (loff_t)(vma->vm_end - vma->vm_start); 71 72 down(&inode->i_sem); 73 file_accessed(file); 74 vma->vm_flags |= VM_HUGETLB | VM_RESERVED; 75 vma->vm_ops = &hugetlb_vm_ops; 76 77 ret = -ENOMEM; 78 len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); 79 if (!(vma->vm_flags & VM_WRITE) && len > inode->i_size) 80 goto out; 81 82 ret = hugetlb_prefault(mapping, vma); 83 if (ret) 84 goto out; 85 86 if (inode->i_size < len) 87 inode->i_size = len; 88 out: 89 up(&inode->i_sem); 90 91 return ret; 92 } 93 94 /* 95 * Called under down_write(mmap_sem). 96 */ 97 98 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA 99 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, 100 unsigned long len, unsigned long pgoff, unsigned long flags); 101 #else 102 static unsigned long 103 hugetlb_get_unmapped_area(struct file *file, unsigned long addr, 104 unsigned long len, unsigned long pgoff, unsigned long flags) 105 { 106 struct mm_struct *mm = current->mm; 107 struct vm_area_struct *vma; 108 unsigned long start_addr; 109 110 if (len & ~HPAGE_MASK) 111 return -EINVAL; 112 if (len > TASK_SIZE) 113 return -ENOMEM; 114 115 if (addr) { 116 addr = ALIGN(addr, HPAGE_SIZE); 117 vma = find_vma(mm, addr); 118 if (TASK_SIZE - len >= addr && 119 (!vma || addr + len <= vma->vm_start)) 120 return addr; 121 } 122 123 start_addr = mm->free_area_cache; 124 125 if (len <= mm->cached_hole_size) 126 start_addr = TASK_UNMAPPED_BASE; 127 128 full_search: 129 addr = ALIGN(start_addr, HPAGE_SIZE); 130 131 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) { 132 /* At this point: (!vma || addr < vma->vm_end). */ 133 if (TASK_SIZE - len < addr) { 134 /* 135 * Start a new search - just in case we missed 136 * some holes. 137 */ 138 if (start_addr != TASK_UNMAPPED_BASE) { 139 start_addr = TASK_UNMAPPED_BASE; 140 goto full_search; 141 } 142 return -ENOMEM; 143 } 144 145 if (!vma || addr + len <= vma->vm_start) 146 return addr; 147 addr = ALIGN(vma->vm_end, HPAGE_SIZE); 148 } 149 } 150 #endif 151 152 /* 153 * Read a page. Again trivial. If it didn't already exist 154 * in the page cache, it is zero-filled. 155 */ 156 static int hugetlbfs_readpage(struct file *file, struct page * page) 157 { 158 unlock_page(page); 159 return -EINVAL; 160 } 161 162 static int hugetlbfs_prepare_write(struct file *file, 163 struct page *page, unsigned offset, unsigned to) 164 { 165 return -EINVAL; 166 } 167 168 static int hugetlbfs_commit_write(struct file *file, 169 struct page *page, unsigned offset, unsigned to) 170 { 171 return -EINVAL; 172 } 173 174 static void huge_pagevec_release(struct pagevec *pvec) 175 { 176 int i; 177 178 for (i = 0; i < pagevec_count(pvec); ++i) 179 put_page(pvec->pages[i]); 180 181 pagevec_reinit(pvec); 182 } 183 184 static void truncate_huge_page(struct page *page) 185 { 186 clear_page_dirty(page); 187 ClearPageUptodate(page); 188 remove_from_page_cache(page); 189 put_page(page); 190 } 191 192 static void truncate_hugepages(struct address_space *mapping, loff_t lstart) 193 { 194 const pgoff_t start = lstart >> HPAGE_SHIFT; 195 struct pagevec pvec; 196 pgoff_t next; 197 int i; 198 199 pagevec_init(&pvec, 0); 200 next = start; 201 while (1) { 202 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) { 203 if (next == start) 204 break; 205 next = start; 206 continue; 207 } 208 209 for (i = 0; i < pagevec_count(&pvec); ++i) { 210 struct page *page = pvec.pages[i]; 211 212 lock_page(page); 213 if (page->index > next) 214 next = page->index; 215 ++next; 216 truncate_huge_page(page); 217 unlock_page(page); 218 hugetlb_put_quota(mapping); 219 } 220 huge_pagevec_release(&pvec); 221 } 222 BUG_ON(!lstart && mapping->nrpages); 223 } 224 225 static void hugetlbfs_delete_inode(struct inode *inode) 226 { 227 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_offset; 312 313 h_vm_pgoff = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT); 314 v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT; 315 /* 316 * Is this VMA fully outside the truncation point? 317 */ 318 if (h_vm_pgoff >= h_pgoff) 319 v_offset = 0; 320 321 unmap_hugepage_range(vma, 322 vma->vm_start + v_offset, vma->vm_end); 323 } 324 } 325 326 /* 327 * Expanding truncates are not allowed. 328 */ 329 static int hugetlb_vmtruncate(struct inode *inode, loff_t offset) 330 { 331 unsigned long pgoff; 332 struct address_space *mapping = inode->i_mapping; 333 334 if (offset > inode->i_size) 335 return -EINVAL; 336 337 BUG_ON(offset & ~HPAGE_MASK); 338 pgoff = offset >> HPAGE_SHIFT; 339 340 inode->i_size = offset; 341 spin_lock(&mapping->i_mmap_lock); 342 if (!prio_tree_empty(&mapping->i_mmap)) 343 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff); 344 spin_unlock(&mapping->i_mmap_lock); 345 truncate_hugepages(mapping, offset); 346 return 0; 347 } 348 349 static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr) 350 { 351 struct inode *inode = dentry->d_inode; 352 int error; 353 unsigned int ia_valid = attr->ia_valid; 354 355 BUG_ON(!inode); 356 357 error = inode_change_ok(inode, attr); 358 if (error) 359 goto out; 360 361 if (ia_valid & ATTR_SIZE) { 362 error = -EINVAL; 363 if (!(attr->ia_size & ~HPAGE_MASK)) 364 error = hugetlb_vmtruncate(inode, attr->ia_size); 365 if (error) 366 goto out; 367 attr->ia_valid &= ~ATTR_SIZE; 368 } 369 error = inode_setattr(inode, attr); 370 out: 371 return error; 372 } 373 374 static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid, 375 gid_t gid, int mode, dev_t dev) 376 { 377 struct inode *inode; 378 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb); 379 380 if (sbinfo->free_inodes >= 0) { 381 spin_lock(&sbinfo->stat_lock); 382 if (!sbinfo->free_inodes) { 383 spin_unlock(&sbinfo->stat_lock); 384 return NULL; 385 } 386 sbinfo->free_inodes--; 387 spin_unlock(&sbinfo->stat_lock); 388 } 389 390 inode = new_inode(sb); 391 if (inode) { 392 struct hugetlbfs_inode_info *info; 393 inode->i_mode = mode; 394 inode->i_uid = uid; 395 inode->i_gid = gid; 396 inode->i_blksize = HPAGE_SIZE; 397 inode->i_blocks = 0; 398 inode->i_mapping->a_ops = &hugetlbfs_aops; 399 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info; 400 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; 401 info = HUGETLBFS_I(inode); 402 mpol_shared_policy_init(&info->policy); 403 switch (mode & S_IFMT) { 404 default: 405 init_special_inode(inode, mode, dev); 406 break; 407 case S_IFREG: 408 inode->i_op = &hugetlbfs_inode_operations; 409 inode->i_fop = &hugetlbfs_file_operations; 410 break; 411 case S_IFDIR: 412 inode->i_op = &hugetlbfs_dir_inode_operations; 413 inode->i_fop = &simple_dir_operations; 414 415 /* directory inodes start off with i_nlink == 2 (for "." entry) */ 416 inode->i_nlink++; 417 break; 418 case S_IFLNK: 419 inode->i_op = &page_symlink_inode_operations; 420 break; 421 } 422 } 423 return inode; 424 } 425 426 /* 427 * File creation. Allocate an inode, and we're done.. 428 */ 429 static int hugetlbfs_mknod(struct inode *dir, 430 struct dentry *dentry, int mode, dev_t dev) 431 { 432 struct inode *inode; 433 int error = -ENOSPC; 434 gid_t gid; 435 436 if (dir->i_mode & S_ISGID) { 437 gid = dir->i_gid; 438 if (S_ISDIR(mode)) 439 mode |= S_ISGID; 440 } else { 441 gid = current->fsgid; 442 } 443 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev); 444 if (inode) { 445 dir->i_ctime = dir->i_mtime = CURRENT_TIME; 446 d_instantiate(dentry, inode); 447 dget(dentry); /* Extra count - pin the dentry in core */ 448 error = 0; 449 } 450 return error; 451 } 452 453 static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 454 { 455 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0); 456 if (!retval) 457 dir->i_nlink++; 458 return retval; 459 } 460 461 static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd) 462 { 463 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0); 464 } 465 466 static int hugetlbfs_symlink(struct inode *dir, 467 struct dentry *dentry, const char *symname) 468 { 469 struct inode *inode; 470 int error = -ENOSPC; 471 gid_t gid; 472 473 if (dir->i_mode & S_ISGID) 474 gid = dir->i_gid; 475 else 476 gid = current->fsgid; 477 478 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, 479 gid, S_IFLNK|S_IRWXUGO, 0); 480 if (inode) { 481 int l = strlen(symname)+1; 482 error = page_symlink(inode, symname, l); 483 if (!error) { 484 d_instantiate(dentry, inode); 485 dget(dentry); 486 } else 487 iput(inode); 488 } 489 dir->i_ctime = dir->i_mtime = CURRENT_TIME; 490 491 return error; 492 } 493 494 /* 495 * For direct-IO reads into hugetlb pages 496 */ 497 static int hugetlbfs_set_page_dirty(struct page *page) 498 { 499 return 0; 500 } 501 502 static int hugetlbfs_statfs(struct super_block *sb, struct kstatfs *buf) 503 { 504 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb); 505 506 buf->f_type = HUGETLBFS_MAGIC; 507 buf->f_bsize = HPAGE_SIZE; 508 if (sbinfo) { 509 spin_lock(&sbinfo->stat_lock); 510 buf->f_blocks = sbinfo->max_blocks; 511 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks; 512 buf->f_files = sbinfo->max_inodes; 513 buf->f_ffree = sbinfo->free_inodes; 514 spin_unlock(&sbinfo->stat_lock); 515 } 516 buf->f_namelen = NAME_MAX; 517 return 0; 518 } 519 520 static void hugetlbfs_put_super(struct super_block *sb) 521 { 522 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb); 523 524 if (sbi) { 525 sb->s_fs_info = NULL; 526 kfree(sbi); 527 } 528 } 529 530 static kmem_cache_t *hugetlbfs_inode_cachep; 531 532 static struct inode *hugetlbfs_alloc_inode(struct super_block *sb) 533 { 534 struct hugetlbfs_inode_info *p; 535 536 p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL); 537 if (!p) 538 return NULL; 539 return &p->vfs_inode; 540 } 541 542 static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags) 543 { 544 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo; 545 546 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == 547 SLAB_CTOR_CONSTRUCTOR) 548 inode_init_once(&ei->vfs_inode); 549 } 550 551 static void hugetlbfs_destroy_inode(struct inode *inode) 552 { 553 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy); 554 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode)); 555 } 556 557 static struct address_space_operations hugetlbfs_aops = { 558 .readpage = hugetlbfs_readpage, 559 .prepare_write = hugetlbfs_prepare_write, 560 .commit_write = hugetlbfs_commit_write, 561 .set_page_dirty = hugetlbfs_set_page_dirty, 562 }; 563 564 struct file_operations hugetlbfs_file_operations = { 565 .mmap = hugetlbfs_file_mmap, 566 .fsync = simple_sync_file, 567 .get_unmapped_area = hugetlb_get_unmapped_area, 568 }; 569 570 static struct inode_operations hugetlbfs_dir_inode_operations = { 571 .create = hugetlbfs_create, 572 .lookup = simple_lookup, 573 .link = simple_link, 574 .unlink = simple_unlink, 575 .symlink = hugetlbfs_symlink, 576 .mkdir = hugetlbfs_mkdir, 577 .rmdir = simple_rmdir, 578 .mknod = hugetlbfs_mknod, 579 .rename = simple_rename, 580 .setattr = hugetlbfs_setattr, 581 }; 582 583 static struct inode_operations hugetlbfs_inode_operations = { 584 .setattr = hugetlbfs_setattr, 585 }; 586 587 static struct super_operations hugetlbfs_ops = { 588 .alloc_inode = hugetlbfs_alloc_inode, 589 .destroy_inode = hugetlbfs_destroy_inode, 590 .statfs = hugetlbfs_statfs, 591 .drop_inode = hugetlbfs_drop_inode, 592 .put_super = hugetlbfs_put_super, 593 }; 594 595 static int 596 hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig) 597 { 598 char *opt, *value, *rest; 599 600 if (!options) 601 return 0; 602 while ((opt = strsep(&options, ",")) != NULL) { 603 if (!*opt) 604 continue; 605 606 value = strchr(opt, '='); 607 if (!value || !*value) 608 return -EINVAL; 609 else 610 *value++ = '\0'; 611 612 if (!strcmp(opt, "uid")) 613 pconfig->uid = simple_strtoul(value, &value, 0); 614 else if (!strcmp(opt, "gid")) 615 pconfig->gid = simple_strtoul(value, &value, 0); 616 else if (!strcmp(opt, "mode")) 617 pconfig->mode = simple_strtoul(value,&value,0) & 0777U; 618 else if (!strcmp(opt, "size")) { 619 unsigned long long size = memparse(value, &rest); 620 if (*rest == '%') { 621 size <<= HPAGE_SHIFT; 622 size *= max_huge_pages; 623 do_div(size, 100); 624 rest++; 625 } 626 size &= HPAGE_MASK; 627 pconfig->nr_blocks = (size >> HPAGE_SHIFT); 628 value = rest; 629 } else if (!strcmp(opt,"nr_inodes")) { 630 pconfig->nr_inodes = memparse(value, &rest); 631 value = rest; 632 } else 633 return -EINVAL; 634 635 if (*value) 636 return -EINVAL; 637 } 638 return 0; 639 } 640 641 static int 642 hugetlbfs_fill_super(struct super_block *sb, void *data, int silent) 643 { 644 struct inode * inode; 645 struct dentry * root; 646 int ret; 647 struct hugetlbfs_config config; 648 struct hugetlbfs_sb_info *sbinfo; 649 650 config.nr_blocks = -1; /* No limit on size by default */ 651 config.nr_inodes = -1; /* No limit on number of inodes by default */ 652 config.uid = current->fsuid; 653 config.gid = current->fsgid; 654 config.mode = 0755; 655 ret = hugetlbfs_parse_options(data, &config); 656 657 if (ret) 658 return ret; 659 660 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL); 661 if (!sbinfo) 662 return -ENOMEM; 663 sb->s_fs_info = sbinfo; 664 spin_lock_init(&sbinfo->stat_lock); 665 sbinfo->max_blocks = config.nr_blocks; 666 sbinfo->free_blocks = config.nr_blocks; 667 sbinfo->max_inodes = config.nr_inodes; 668 sbinfo->free_inodes = config.nr_inodes; 669 sb->s_maxbytes = MAX_LFS_FILESIZE; 670 sb->s_blocksize = HPAGE_SIZE; 671 sb->s_blocksize_bits = HPAGE_SHIFT; 672 sb->s_magic = HUGETLBFS_MAGIC; 673 sb->s_op = &hugetlbfs_ops; 674 sb->s_time_gran = 1; 675 inode = hugetlbfs_get_inode(sb, config.uid, config.gid, 676 S_IFDIR | config.mode, 0); 677 if (!inode) 678 goto out_free; 679 680 root = d_alloc_root(inode); 681 if (!root) { 682 iput(inode); 683 goto out_free; 684 } 685 sb->s_root = root; 686 return 0; 687 out_free: 688 kfree(sbinfo); 689 return -ENOMEM; 690 } 691 692 int hugetlb_get_quota(struct address_space *mapping) 693 { 694 int ret = 0; 695 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb); 696 697 if (sbinfo->free_blocks > -1) { 698 spin_lock(&sbinfo->stat_lock); 699 if (sbinfo->free_blocks > 0) 700 sbinfo->free_blocks--; 701 else 702 ret = -ENOMEM; 703 spin_unlock(&sbinfo->stat_lock); 704 } 705 706 return ret; 707 } 708 709 void hugetlb_put_quota(struct address_space *mapping) 710 { 711 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb); 712 713 if (sbinfo->free_blocks > -1) { 714 spin_lock(&sbinfo->stat_lock); 715 sbinfo->free_blocks++; 716 spin_unlock(&sbinfo->stat_lock); 717 } 718 } 719 720 static struct super_block *hugetlbfs_get_sb(struct file_system_type *fs_type, 721 int flags, const char *dev_name, void *data) 722 { 723 return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super); 724 } 725 726 static struct file_system_type hugetlbfs_fs_type = { 727 .name = "hugetlbfs", 728 .get_sb = hugetlbfs_get_sb, 729 .kill_sb = kill_litter_super, 730 }; 731 732 static struct vfsmount *hugetlbfs_vfsmount; 733 734 /* 735 * Return the next identifier for a shm file 736 */ 737 static unsigned long hugetlbfs_counter(void) 738 { 739 static DEFINE_SPINLOCK(lock); 740 static unsigned long counter; 741 unsigned long ret; 742 743 spin_lock(&lock); 744 ret = ++counter; 745 spin_unlock(&lock); 746 return ret; 747 } 748 749 static int can_do_hugetlb_shm(void) 750 { 751 return likely(capable(CAP_IPC_LOCK) || 752 in_group_p(sysctl_hugetlb_shm_group) || 753 can_do_mlock()); 754 } 755 756 struct file *hugetlb_zero_setup(size_t size) 757 { 758 int error = -ENOMEM; 759 struct file *file; 760 struct inode *inode; 761 struct dentry *dentry, *root; 762 struct qstr quick_string; 763 char buf[16]; 764 765 if (!can_do_hugetlb_shm()) 766 return ERR_PTR(-EPERM); 767 768 if (!is_hugepage_mem_enough(size)) 769 return ERR_PTR(-ENOMEM); 770 771 if (!user_shm_lock(size, current->user)) 772 return ERR_PTR(-ENOMEM); 773 774 root = hugetlbfs_vfsmount->mnt_root; 775 snprintf(buf, 16, "%lu", hugetlbfs_counter()); 776 quick_string.name = buf; 777 quick_string.len = strlen(quick_string.name); 778 quick_string.hash = 0; 779 dentry = d_alloc(root, &quick_string); 780 if (!dentry) 781 goto out_shm_unlock; 782 783 error = -ENFILE; 784 file = get_empty_filp(); 785 if (!file) 786 goto out_dentry; 787 788 error = -ENOSPC; 789 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid, 790 current->fsgid, S_IFREG | S_IRWXUGO, 0); 791 if (!inode) 792 goto out_file; 793 794 d_instantiate(dentry, inode); 795 inode->i_size = size; 796 inode->i_nlink = 0; 797 file->f_vfsmnt = mntget(hugetlbfs_vfsmount); 798 file->f_dentry = dentry; 799 file->f_mapping = inode->i_mapping; 800 file->f_op = &hugetlbfs_file_operations; 801 file->f_mode = FMODE_WRITE | FMODE_READ; 802 return file; 803 804 out_file: 805 put_filp(file); 806 out_dentry: 807 dput(dentry); 808 out_shm_unlock: 809 user_shm_unlock(size, current->user); 810 return ERR_PTR(error); 811 } 812 813 static int __init init_hugetlbfs_fs(void) 814 { 815 int error; 816 struct vfsmount *vfsmount; 817 818 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache", 819 sizeof(struct hugetlbfs_inode_info), 820 0, 0, init_once, NULL); 821 if (hugetlbfs_inode_cachep == NULL) 822 return -ENOMEM; 823 824 error = register_filesystem(&hugetlbfs_fs_type); 825 if (error) 826 goto out; 827 828 vfsmount = kern_mount(&hugetlbfs_fs_type); 829 830 if (!IS_ERR(vfsmount)) { 831 hugetlbfs_vfsmount = vfsmount; 832 return 0; 833 } 834 835 error = PTR_ERR(vfsmount); 836 837 out: 838 if (error) 839 kmem_cache_destroy(hugetlbfs_inode_cachep); 840 return error; 841 } 842 843 static void __exit exit_hugetlbfs_fs(void) 844 { 845 kmem_cache_destroy(hugetlbfs_inode_cachep); 846 unregister_filesystem(&hugetlbfs_fs_type); 847 } 848 849 module_init(init_hugetlbfs_fs) 850 module_exit(exit_hugetlbfs_fs) 851 852 MODULE_LICENSE("GPL"); 853