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