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