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> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 21 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/backing-dev.h> |
| 23 | #include <linux/hugetlb.h> |
| 24 | #include <linux/pagevec.h> |
Benjamin Herrenschmidt | 036e085 | 2007-05-06 14:50:12 -0700 | [diff] [blame] | 25 | #include <linux/mman.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/quotaops.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/dnotify.h> |
| 29 | #include <linux/statfs.h> |
| 30 | #include <linux/security.h> |
| 31 | |
| 32 | #include <asm/uaccess.h> |
| 33 | |
| 34 | /* some random number */ |
| 35 | #define HUGETLBFS_MAGIC 0x958458f6 |
| 36 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 37 | static const struct super_operations hugetlbfs_ops; |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 38 | static const struct address_space_operations hugetlbfs_aops; |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 39 | const struct file_operations hugetlbfs_file_operations; |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 40 | static const struct inode_operations hugetlbfs_dir_inode_operations; |
| 41 | static const struct inode_operations hugetlbfs_inode_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | static struct backing_dev_info hugetlbfs_backing_dev_info = { |
| 44 | .ra_pages = 0, /* No readahead */ |
| 45 | .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK, |
| 46 | }; |
| 47 | |
| 48 | int sysctl_hugetlb_shm_group; |
| 49 | |
Adam Litke | 2e9b367 | 2005-10-29 18:16:47 -0700 | [diff] [blame] | 50 | static void huge_pagevec_release(struct pagevec *pvec) |
| 51 | { |
| 52 | int i; |
| 53 | |
| 54 | for (i = 0; i < pagevec_count(pvec); ++i) |
| 55 | put_page(pvec->pages[i]); |
| 56 | |
| 57 | pagevec_reinit(pvec); |
| 58 | } |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 61 | { |
Josef Sipek | b39424e | 2006-12-08 02:37:07 -0800 | [diff] [blame] | 62 | struct inode *inode = file->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | loff_t len, vma_len; |
| 64 | int ret; |
| 65 | |
Hugh Dickins | 68589bc | 2006-11-14 02:03:32 -0800 | [diff] [blame] | 66 | /* |
| 67 | * vma alignment has already been checked by prepare_hugepage_range. |
| 68 | * If you add any error returns here, do so after setting VM_HUGETLB, |
| 69 | * so is_vm_hugetlb_page tests below unmap_region go the right way |
| 70 | * when do_mmap_pgoff unwinds (may be important on powerpc and ia64). |
| 71 | */ |
| 72 | vma->vm_flags |= VM_HUGETLB | VM_RESERVED; |
| 73 | vma->vm_ops = &hugetlb_vm_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | vma_len = (loff_t)(vma->vm_end - vma->vm_start); |
| 76 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 77 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | file_accessed(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | ret = -ENOMEM; |
| 81 | len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 83 | if (vma->vm_flags & VM_MAYSHARE && |
| 84 | hugetlb_reserve_pages(inode, vma->vm_pgoff >> (HPAGE_SHIFT-PAGE_SHIFT), |
| 85 | len >> HPAGE_SHIFT)) |
| 86 | goto out; |
David Gibson | b45b5bd | 2006-03-22 00:08:55 -0800 | [diff] [blame] | 87 | |
Adam Litke | 4c88726 | 2005-10-29 18:16:46 -0700 | [diff] [blame] | 88 | ret = 0; |
| 89 | hugetlb_prefault_arch_hook(vma->vm_mm); |
Zhang, Yanmin | b6174df | 2006-07-10 04:44:49 -0700 | [diff] [blame] | 90 | if (vma->vm_flags & VM_WRITE && inode->i_size < len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | inode->i_size = len; |
| 92 | out: |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 93 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | /* |
Hugh Dickins | 508034a | 2005-10-29 18:16:30 -0700 | [diff] [blame] | 99 | * Called under down_write(mmap_sem). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | */ |
| 101 | |
Adrian Bunk | d2ba27e8 | 2007-05-06 14:49:00 -0700 | [diff] [blame] | 102 | #ifndef HAVE_ARCH_HUGETLB_UNMAPPED_AREA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | static unsigned long |
| 104 | hugetlb_get_unmapped_area(struct file *file, unsigned long addr, |
| 105 | unsigned long len, unsigned long pgoff, unsigned long flags) |
| 106 | { |
| 107 | struct mm_struct *mm = current->mm; |
| 108 | struct vm_area_struct *vma; |
| 109 | unsigned long start_addr; |
| 110 | |
| 111 | if (len & ~HPAGE_MASK) |
| 112 | return -EINVAL; |
| 113 | if (len > TASK_SIZE) |
| 114 | return -ENOMEM; |
| 115 | |
Benjamin Herrenschmidt | 036e085 | 2007-05-06 14:50:12 -0700 | [diff] [blame] | 116 | if (flags & MAP_FIXED) { |
| 117 | if (prepare_hugepage_range(addr, len, pgoff)) |
| 118 | return -EINVAL; |
| 119 | return addr; |
| 120 | } |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | if (addr) { |
| 123 | addr = ALIGN(addr, HPAGE_SIZE); |
| 124 | vma = find_vma(mm, addr); |
| 125 | if (TASK_SIZE - len >= addr && |
| 126 | (!vma || addr + len <= vma->vm_start)) |
| 127 | return addr; |
| 128 | } |
| 129 | |
| 130 | start_addr = mm->free_area_cache; |
| 131 | |
Wolfgang Wander | 1363c3c | 2005-06-21 17:14:49 -0700 | [diff] [blame] | 132 | if (len <= mm->cached_hole_size) |
| 133 | start_addr = TASK_UNMAPPED_BASE; |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | full_search: |
| 136 | addr = ALIGN(start_addr, HPAGE_SIZE); |
| 137 | |
| 138 | for (vma = find_vma(mm, addr); ; vma = vma->vm_next) { |
| 139 | /* At this point: (!vma || addr < vma->vm_end). */ |
| 140 | if (TASK_SIZE - len < addr) { |
| 141 | /* |
| 142 | * Start a new search - just in case we missed |
| 143 | * some holes. |
| 144 | */ |
| 145 | if (start_addr != TASK_UNMAPPED_BASE) { |
| 146 | start_addr = TASK_UNMAPPED_BASE; |
| 147 | goto full_search; |
| 148 | } |
| 149 | return -ENOMEM; |
| 150 | } |
| 151 | |
| 152 | if (!vma || addr + len <= vma->vm_start) |
| 153 | return addr; |
| 154 | addr = ALIGN(vma->vm_end, HPAGE_SIZE); |
| 155 | } |
| 156 | } |
| 157 | #endif |
| 158 | |
| 159 | /* |
| 160 | * Read a page. Again trivial. If it didn't already exist |
| 161 | * in the page cache, it is zero-filled. |
| 162 | */ |
| 163 | static int hugetlbfs_readpage(struct file *file, struct page * page) |
| 164 | { |
| 165 | unlock_page(page); |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | |
| 169 | static int hugetlbfs_prepare_write(struct file *file, |
| 170 | struct page *page, unsigned offset, unsigned to) |
| 171 | { |
| 172 | return -EINVAL; |
| 173 | } |
| 174 | |
| 175 | static int hugetlbfs_commit_write(struct file *file, |
| 176 | struct page *page, unsigned offset, unsigned to) |
| 177 | { |
| 178 | return -EINVAL; |
| 179 | } |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | static void truncate_huge_page(struct page *page) |
| 182 | { |
Linus Torvalds | fba2591 | 2006-12-20 13:46:42 -0800 | [diff] [blame] | 183 | cancel_dirty_page(page, /* No IO accounting for huge pages? */0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | ClearPageUptodate(page); |
| 185 | remove_from_page_cache(page); |
| 186 | put_page(page); |
| 187 | } |
| 188 | |
David Gibson | b45b5bd | 2006-03-22 00:08:55 -0800 | [diff] [blame] | 189 | static void truncate_hugepages(struct inode *inode, loff_t lstart) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
David Gibson | b45b5bd | 2006-03-22 00:08:55 -0800 | [diff] [blame] | 191 | struct address_space *mapping = &inode->i_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | const pgoff_t start = lstart >> HPAGE_SHIFT; |
| 193 | struct pagevec pvec; |
| 194 | pgoff_t next; |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 195 | int i, freed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | pagevec_init(&pvec, 0); |
| 198 | next = start; |
| 199 | while (1) { |
| 200 | if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) { |
| 201 | if (next == start) |
| 202 | break; |
| 203 | next = start; |
| 204 | continue; |
| 205 | } |
| 206 | |
| 207 | for (i = 0; i < pagevec_count(&pvec); ++i) { |
| 208 | struct page *page = pvec.pages[i]; |
| 209 | |
| 210 | lock_page(page); |
| 211 | if (page->index > next) |
| 212 | next = page->index; |
| 213 | ++next; |
| 214 | truncate_huge_page(page); |
| 215 | unlock_page(page); |
| 216 | hugetlb_put_quota(mapping); |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 217 | freed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | huge_pagevec_release(&pvec); |
| 220 | } |
| 221 | BUG_ON(!lstart && mapping->nrpages); |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 222 | hugetlb_unreserve_pages(inode, start, freed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static void hugetlbfs_delete_inode(struct inode *inode) |
| 226 | { |
David Gibson | b45b5bd | 2006-03-22 00:08:55 -0800 | [diff] [blame] | 227 | truncate_hugepages(inode, 0); |
Christoph Hellwig | 149f421 | 2005-10-29 18:16:43 -0700 | [diff] [blame] | 228 | clear_inode(inode); |
| 229 | } |
| 230 | |
Josh Triplett | ddc0a51 | 2006-09-29 01:59:27 -0700 | [diff] [blame] | 231 | static void hugetlbfs_forget_inode(struct inode *inode) __releases(inode_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | { |
Christoph Hellwig | 0b1533f | 2005-10-29 18:16:45 -0700 | [diff] [blame] | 233 | struct super_block *sb = inode->i_sb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Christoph Hellwig | 0b1533f | 2005-10-29 18:16:45 -0700 | [diff] [blame] | 235 | if (!hlist_unhashed(&inode->i_hash)) { |
| 236 | if (!(inode->i_state & (I_DIRTY|I_LOCK))) |
| 237 | list_move(&inode->i_list, &inode_unused); |
| 238 | inodes_stat.nr_unused++; |
| 239 | if (!sb || (sb->s_flags & MS_ACTIVE)) { |
| 240 | spin_unlock(&inode_lock); |
| 241 | return; |
| 242 | } |
| 243 | inode->i_state |= I_WILL_FREE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | spin_unlock(&inode_lock); |
Christoph Hellwig | 0b1533f | 2005-10-29 18:16:45 -0700 | [diff] [blame] | 245 | /* |
| 246 | * write_inode_now is a noop as we set BDI_CAP_NO_WRITEBACK |
| 247 | * in our backing_dev_info. |
| 248 | */ |
| 249 | write_inode_now(inode, 1); |
| 250 | spin_lock(&inode_lock); |
| 251 | inode->i_state &= ~I_WILL_FREE; |
| 252 | inodes_stat.nr_unused--; |
| 253 | hlist_del_init(&inode->i_hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | list_del_init(&inode->i_list); |
| 256 | list_del_init(&inode->i_sb_list); |
| 257 | inode->i_state |= I_FREEING; |
| 258 | inodes_stat.nr_inodes--; |
| 259 | spin_unlock(&inode_lock); |
David Gibson | b45b5bd | 2006-03-22 00:08:55 -0800 | [diff] [blame] | 260 | truncate_hugepages(inode, 0); |
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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | static inline void |
Hugh Dickins | 856fc29 | 2006-10-28 10:38:43 -0700 | [diff] [blame] | 274 | hugetlb_vmtruncate_list(struct prio_tree_root *root, pgoff_t pgoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
| 276 | struct vm_area_struct *vma; |
| 277 | struct prio_tree_iter iter; |
| 278 | |
Hugh Dickins | 856fc29 | 2006-10-28 10:38:43 -0700 | [diff] [blame] | 279 | vma_prio_tree_foreach(vma, &iter, root, pgoff, ULONG_MAX) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | unsigned long v_offset; |
| 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | /* |
Hugh Dickins | 856fc29 | 2006-10-28 10:38:43 -0700 | [diff] [blame] | 283 | * Can the expression below overflow on 32-bit arches? |
| 284 | * No, because the prio_tree returns us only those vmas |
| 285 | * which overlap the truncated area starting at pgoff, |
| 286 | * and no vma on a 32-bit arch can span beyond the 4GB. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | */ |
Hugh Dickins | 856fc29 | 2006-10-28 10:38:43 -0700 | [diff] [blame] | 288 | if (vma->vm_pgoff < pgoff) |
| 289 | v_offset = (pgoff - vma->vm_pgoff) << PAGE_SHIFT; |
| 290 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | v_offset = 0; |
| 292 | |
Chen, Kenneth W | 502717f | 2006-10-11 01:20:46 -0700 | [diff] [blame] | 293 | __unmap_hugepage_range(vma, |
Hugh Dickins | 508034a | 2005-10-29 18:16:30 -0700 | [diff] [blame] | 294 | vma->vm_start + v_offset, vma->vm_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Expanding truncates are not allowed. |
| 300 | */ |
| 301 | static int hugetlb_vmtruncate(struct inode *inode, loff_t offset) |
| 302 | { |
Hugh Dickins | 856fc29 | 2006-10-28 10:38:43 -0700 | [diff] [blame] | 303 | pgoff_t pgoff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | struct address_space *mapping = inode->i_mapping; |
| 305 | |
| 306 | if (offset > inode->i_size) |
| 307 | return -EINVAL; |
| 308 | |
| 309 | BUG_ON(offset & ~HPAGE_MASK); |
Hugh Dickins | 856fc29 | 2006-10-28 10:38:43 -0700 | [diff] [blame] | 310 | pgoff = offset >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | inode->i_size = offset; |
| 313 | spin_lock(&mapping->i_mmap_lock); |
| 314 | if (!prio_tree_empty(&mapping->i_mmap)) |
| 315 | hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff); |
| 316 | spin_unlock(&mapping->i_mmap_lock); |
David Gibson | b45b5bd | 2006-03-22 00:08:55 -0800 | [diff] [blame] | 317 | truncate_hugepages(inode, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 322 | { |
| 323 | struct inode *inode = dentry->d_inode; |
| 324 | int error; |
| 325 | unsigned int ia_valid = attr->ia_valid; |
| 326 | |
| 327 | BUG_ON(!inode); |
| 328 | |
| 329 | error = inode_change_ok(inode, attr); |
| 330 | if (error) |
| 331 | goto out; |
| 332 | |
| 333 | if (ia_valid & ATTR_SIZE) { |
| 334 | error = -EINVAL; |
| 335 | if (!(attr->ia_size & ~HPAGE_MASK)) |
| 336 | error = hugetlb_vmtruncate(inode, attr->ia_size); |
| 337 | if (error) |
| 338 | goto out; |
| 339 | attr->ia_valid &= ~ATTR_SIZE; |
| 340 | } |
| 341 | error = inode_setattr(inode, attr); |
| 342 | out: |
| 343 | return error; |
| 344 | } |
| 345 | |
| 346 | static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid, |
| 347 | gid_t gid, int mode, dev_t dev) |
| 348 | { |
| 349 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
| 351 | inode = new_inode(sb); |
| 352 | if (inode) { |
| 353 | struct hugetlbfs_inode_info *info; |
| 354 | inode->i_mode = mode; |
| 355 | inode->i_uid = uid; |
| 356 | inode->i_gid = gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | inode->i_blocks = 0; |
| 358 | inode->i_mapping->a_ops = &hugetlbfs_aops; |
| 359 | inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info; |
| 360 | inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 361 | INIT_LIST_HEAD(&inode->i_mapping->private_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | info = HUGETLBFS_I(inode); |
Robin Holt | 7339ff8 | 2006-01-14 13:20:48 -0800 | [diff] [blame] | 363 | mpol_shared_policy_init(&info->policy, MPOL_DEFAULT, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | switch (mode & S_IFMT) { |
| 365 | default: |
| 366 | init_special_inode(inode, mode, dev); |
| 367 | break; |
| 368 | case S_IFREG: |
| 369 | inode->i_op = &hugetlbfs_inode_operations; |
| 370 | inode->i_fop = &hugetlbfs_file_operations; |
| 371 | break; |
| 372 | case S_IFDIR: |
| 373 | inode->i_op = &hugetlbfs_dir_inode_operations; |
| 374 | inode->i_fop = &simple_dir_operations; |
| 375 | |
| 376 | /* directory inodes start off with i_nlink == 2 (for "." entry) */ |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 377 | inc_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | break; |
| 379 | case S_IFLNK: |
| 380 | inode->i_op = &page_symlink_inode_operations; |
| 381 | break; |
| 382 | } |
| 383 | } |
| 384 | return inode; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * File creation. Allocate an inode, and we're done.. |
| 389 | */ |
| 390 | static int hugetlbfs_mknod(struct inode *dir, |
| 391 | struct dentry *dentry, int mode, dev_t dev) |
| 392 | { |
| 393 | struct inode *inode; |
| 394 | int error = -ENOSPC; |
| 395 | gid_t gid; |
| 396 | |
| 397 | if (dir->i_mode & S_ISGID) { |
| 398 | gid = dir->i_gid; |
| 399 | if (S_ISDIR(mode)) |
| 400 | mode |= S_ISGID; |
| 401 | } else { |
| 402 | gid = current->fsgid; |
| 403 | } |
| 404 | inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev); |
| 405 | if (inode) { |
| 406 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; |
| 407 | d_instantiate(dentry, inode); |
| 408 | dget(dentry); /* Extra count - pin the dentry in core */ |
| 409 | error = 0; |
| 410 | } |
| 411 | return error; |
| 412 | } |
| 413 | |
| 414 | static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 415 | { |
| 416 | int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0); |
| 417 | if (!retval) |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 418 | inc_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | return retval; |
| 420 | } |
| 421 | |
| 422 | static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd) |
| 423 | { |
| 424 | return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0); |
| 425 | } |
| 426 | |
| 427 | static int hugetlbfs_symlink(struct inode *dir, |
| 428 | struct dentry *dentry, const char *symname) |
| 429 | { |
| 430 | struct inode *inode; |
| 431 | int error = -ENOSPC; |
| 432 | gid_t gid; |
| 433 | |
| 434 | if (dir->i_mode & S_ISGID) |
| 435 | gid = dir->i_gid; |
| 436 | else |
| 437 | gid = current->fsgid; |
| 438 | |
| 439 | inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, |
| 440 | gid, S_IFLNK|S_IRWXUGO, 0); |
| 441 | if (inode) { |
| 442 | int l = strlen(symname)+1; |
| 443 | error = page_symlink(inode, symname, l); |
| 444 | if (!error) { |
| 445 | d_instantiate(dentry, inode); |
| 446 | dget(dentry); |
| 447 | } else |
| 448 | iput(inode); |
| 449 | } |
| 450 | dir->i_ctime = dir->i_mtime = CURRENT_TIME; |
| 451 | |
| 452 | return error; |
| 453 | } |
| 454 | |
| 455 | /* |
Ken Chen | 6649a38 | 2007-02-08 14:20:27 -0800 | [diff] [blame] | 456 | * mark the head page dirty |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | */ |
| 458 | static int hugetlbfs_set_page_dirty(struct page *page) |
| 459 | { |
Christoph Lameter | d85f338 | 2007-05-06 14:49:39 -0700 | [diff] [blame] | 460 | struct page *head = compound_head(page); |
Ken Chen | 6649a38 | 2007-02-08 14:20:27 -0800 | [diff] [blame] | 461 | |
| 462 | SetPageDirty(head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | return 0; |
| 464 | } |
| 465 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 466 | static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 468 | struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
| 470 | buf->f_type = HUGETLBFS_MAGIC; |
| 471 | buf->f_bsize = HPAGE_SIZE; |
| 472 | if (sbinfo) { |
| 473 | spin_lock(&sbinfo->stat_lock); |
David Gibson | 74a8a65 | 2005-11-21 21:32:24 -0800 | [diff] [blame] | 474 | /* If no limits set, just report 0 for max/free/used |
| 475 | * blocks, like simple_statfs() */ |
| 476 | if (sbinfo->max_blocks >= 0) { |
| 477 | buf->f_blocks = sbinfo->max_blocks; |
| 478 | buf->f_bavail = buf->f_bfree = sbinfo->free_blocks; |
| 479 | buf->f_files = sbinfo->max_inodes; |
| 480 | buf->f_ffree = sbinfo->free_inodes; |
| 481 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | spin_unlock(&sbinfo->stat_lock); |
| 483 | } |
| 484 | buf->f_namelen = NAME_MAX; |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | static void hugetlbfs_put_super(struct super_block *sb) |
| 489 | { |
| 490 | struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb); |
| 491 | |
| 492 | if (sbi) { |
| 493 | sb->s_fs_info = NULL; |
| 494 | kfree(sbi); |
| 495 | } |
| 496 | } |
| 497 | |
Christoph Hellwig | 9652798 | 2005-10-29 18:16:42 -0700 | [diff] [blame] | 498 | static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo) |
| 499 | { |
| 500 | if (sbinfo->free_inodes >= 0) { |
| 501 | spin_lock(&sbinfo->stat_lock); |
| 502 | if (unlikely(!sbinfo->free_inodes)) { |
| 503 | spin_unlock(&sbinfo->stat_lock); |
| 504 | return 0; |
| 505 | } |
| 506 | sbinfo->free_inodes--; |
| 507 | spin_unlock(&sbinfo->stat_lock); |
| 508 | } |
| 509 | |
| 510 | return 1; |
| 511 | } |
| 512 | |
| 513 | static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo) |
| 514 | { |
| 515 | if (sbinfo->free_inodes >= 0) { |
| 516 | spin_lock(&sbinfo->stat_lock); |
| 517 | sbinfo->free_inodes++; |
| 518 | spin_unlock(&sbinfo->stat_lock); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 523 | static struct kmem_cache *hugetlbfs_inode_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
| 525 | static struct inode *hugetlbfs_alloc_inode(struct super_block *sb) |
| 526 | { |
Christoph Hellwig | 9652798 | 2005-10-29 18:16:42 -0700 | [diff] [blame] | 527 | struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | struct hugetlbfs_inode_info *p; |
| 529 | |
Christoph Hellwig | 9652798 | 2005-10-29 18:16:42 -0700 | [diff] [blame] | 530 | if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | return NULL; |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 532 | p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL); |
Christoph Hellwig | 9652798 | 2005-10-29 18:16:42 -0700 | [diff] [blame] | 533 | if (unlikely(!p)) { |
| 534 | hugetlbfs_inc_free_inodes(sbinfo); |
| 535 | return NULL; |
| 536 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | return &p->vfs_inode; |
| 538 | } |
| 539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | static void hugetlbfs_destroy_inode(struct inode *inode) |
| 541 | { |
Christoph Hellwig | 9652798 | 2005-10-29 18:16:42 -0700 | [diff] [blame] | 542 | hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy); |
| 544 | kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode)); |
| 545 | } |
| 546 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 547 | static const struct address_space_operations hugetlbfs_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | .readpage = hugetlbfs_readpage, |
| 549 | .prepare_write = hugetlbfs_prepare_write, |
| 550 | .commit_write = hugetlbfs_commit_write, |
| 551 | .set_page_dirty = hugetlbfs_set_page_dirty, |
| 552 | }; |
| 553 | |
Christoph Hellwig | 9652798 | 2005-10-29 18:16:42 -0700 | [diff] [blame] | 554 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 555 | static void init_once(void *foo, struct kmem_cache *cachep, unsigned long flags) |
Christoph Hellwig | 9652798 | 2005-10-29 18:16:42 -0700 | [diff] [blame] | 556 | { |
| 557 | struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo; |
| 558 | |
Christoph Lameter | 50953fe | 2007-05-06 14:50:16 -0700 | [diff] [blame] | 559 | if (flags & SLAB_CTOR_CONSTRUCTOR) |
Christoph Hellwig | 9652798 | 2005-10-29 18:16:42 -0700 | [diff] [blame] | 560 | inode_init_once(&ei->vfs_inode); |
| 561 | } |
| 562 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 563 | const struct file_operations hugetlbfs_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | .mmap = hugetlbfs_file_mmap, |
| 565 | .fsync = simple_sync_file, |
| 566 | .get_unmapped_area = hugetlb_get_unmapped_area, |
| 567 | }; |
| 568 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 569 | static const struct inode_operations hugetlbfs_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | .create = hugetlbfs_create, |
| 571 | .lookup = simple_lookup, |
| 572 | .link = simple_link, |
| 573 | .unlink = simple_unlink, |
| 574 | .symlink = hugetlbfs_symlink, |
| 575 | .mkdir = hugetlbfs_mkdir, |
| 576 | .rmdir = simple_rmdir, |
| 577 | .mknod = hugetlbfs_mknod, |
| 578 | .rename = simple_rename, |
| 579 | .setattr = hugetlbfs_setattr, |
| 580 | }; |
| 581 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 582 | static const struct inode_operations hugetlbfs_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | .setattr = hugetlbfs_setattr, |
| 584 | }; |
| 585 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 586 | static const struct super_operations hugetlbfs_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | .alloc_inode = hugetlbfs_alloc_inode, |
| 588 | .destroy_inode = hugetlbfs_destroy_inode, |
| 589 | .statfs = hugetlbfs_statfs, |
Christoph Hellwig | 149f421 | 2005-10-29 18:16:43 -0700 | [diff] [blame] | 590 | .delete_inode = hugetlbfs_delete_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | pconfig->nr_blocks = (size >> HPAGE_SHIFT); |
| 627 | value = rest; |
| 628 | } else if (!strcmp(opt,"nr_inodes")) { |
| 629 | pconfig->nr_inodes = memparse(value, &rest); |
| 630 | value = rest; |
| 631 | } else |
| 632 | return -EINVAL; |
| 633 | |
| 634 | if (*value) |
| 635 | return -EINVAL; |
| 636 | } |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | static int |
| 641 | hugetlbfs_fill_super(struct super_block *sb, void *data, int silent) |
| 642 | { |
| 643 | struct inode * inode; |
| 644 | struct dentry * root; |
| 645 | int ret; |
| 646 | struct hugetlbfs_config config; |
| 647 | struct hugetlbfs_sb_info *sbinfo; |
| 648 | |
| 649 | config.nr_blocks = -1; /* No limit on size by default */ |
| 650 | config.nr_inodes = -1; /* No limit on number of inodes by default */ |
| 651 | config.uid = current->fsuid; |
| 652 | config.gid = current->fsgid; |
| 653 | config.mode = 0755; |
| 654 | ret = hugetlbfs_parse_options(data, &config); |
| 655 | |
| 656 | if (ret) |
| 657 | return ret; |
| 658 | |
| 659 | sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL); |
| 660 | if (!sbinfo) |
| 661 | return -ENOMEM; |
| 662 | sb->s_fs_info = sbinfo; |
| 663 | spin_lock_init(&sbinfo->stat_lock); |
| 664 | sbinfo->max_blocks = config.nr_blocks; |
| 665 | sbinfo->free_blocks = config.nr_blocks; |
| 666 | sbinfo->max_inodes = config.nr_inodes; |
| 667 | sbinfo->free_inodes = config.nr_inodes; |
| 668 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
| 669 | sb->s_blocksize = HPAGE_SIZE; |
| 670 | sb->s_blocksize_bits = HPAGE_SHIFT; |
| 671 | sb->s_magic = HUGETLBFS_MAGIC; |
| 672 | sb->s_op = &hugetlbfs_ops; |
| 673 | sb->s_time_gran = 1; |
| 674 | inode = hugetlbfs_get_inode(sb, config.uid, config.gid, |
| 675 | S_IFDIR | config.mode, 0); |
| 676 | if (!inode) |
| 677 | goto out_free; |
| 678 | |
| 679 | root = d_alloc_root(inode); |
| 680 | if (!root) { |
| 681 | iput(inode); |
| 682 | goto out_free; |
| 683 | } |
| 684 | sb->s_root = root; |
| 685 | return 0; |
| 686 | out_free: |
| 687 | kfree(sbinfo); |
| 688 | return -ENOMEM; |
| 689 | } |
| 690 | |
| 691 | int hugetlb_get_quota(struct address_space *mapping) |
| 692 | { |
| 693 | int ret = 0; |
| 694 | struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb); |
| 695 | |
| 696 | if (sbinfo->free_blocks > -1) { |
| 697 | spin_lock(&sbinfo->stat_lock); |
| 698 | if (sbinfo->free_blocks > 0) |
| 699 | sbinfo->free_blocks--; |
| 700 | else |
| 701 | ret = -ENOMEM; |
| 702 | spin_unlock(&sbinfo->stat_lock); |
| 703 | } |
| 704 | |
| 705 | return ret; |
| 706 | } |
| 707 | |
| 708 | void hugetlb_put_quota(struct address_space *mapping) |
| 709 | { |
| 710 | struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb); |
| 711 | |
| 712 | if (sbinfo->free_blocks > -1) { |
| 713 | spin_lock(&sbinfo->stat_lock); |
| 714 | sbinfo->free_blocks++; |
| 715 | spin_unlock(&sbinfo->stat_lock); |
| 716 | } |
| 717 | } |
| 718 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 719 | static int hugetlbfs_get_sb(struct file_system_type *fs_type, |
| 720 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 722 | return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super, mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | static struct file_system_type hugetlbfs_fs_type = { |
| 726 | .name = "hugetlbfs", |
| 727 | .get_sb = hugetlbfs_get_sb, |
| 728 | .kill_sb = kill_litter_super, |
| 729 | }; |
| 730 | |
| 731 | static struct vfsmount *hugetlbfs_vfsmount; |
| 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | static int can_do_hugetlb_shm(void) |
| 734 | { |
| 735 | return likely(capable(CAP_IPC_LOCK) || |
| 736 | in_group_p(sysctl_hugetlb_shm_group) || |
| 737 | can_do_mlock()); |
| 738 | } |
| 739 | |
| 740 | struct file *hugetlb_zero_setup(size_t size) |
| 741 | { |
| 742 | int error = -ENOMEM; |
| 743 | struct file *file; |
| 744 | struct inode *inode; |
| 745 | struct dentry *dentry, *root; |
| 746 | struct qstr quick_string; |
| 747 | char buf[16]; |
Chen, Kenneth W | bba1e9b | 2006-03-22 00:09:02 -0800 | [diff] [blame] | 748 | static atomic_t counter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
Akinobu Mita | 5bc9859 | 2007-05-06 14:50:18 -0700 | [diff] [blame^] | 750 | if (!hugetlbfs_vfsmount) |
| 751 | return ERR_PTR(-ENOENT); |
| 752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | if (!can_do_hugetlb_shm()) |
| 754 | return ERR_PTR(-EPERM); |
| 755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | if (!user_shm_lock(size, current->user)) |
| 757 | return ERR_PTR(-ENOMEM); |
| 758 | |
| 759 | root = hugetlbfs_vfsmount->mnt_root; |
Chen, Kenneth W | bba1e9b | 2006-03-22 00:09:02 -0800 | [diff] [blame] | 760 | snprintf(buf, 16, "%u", atomic_inc_return(&counter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | quick_string.name = buf; |
| 762 | quick_string.len = strlen(quick_string.name); |
| 763 | quick_string.hash = 0; |
| 764 | dentry = d_alloc(root, &quick_string); |
| 765 | if (!dentry) |
| 766 | goto out_shm_unlock; |
| 767 | |
| 768 | error = -ENFILE; |
| 769 | file = get_empty_filp(); |
| 770 | if (!file) |
| 771 | goto out_dentry; |
| 772 | |
| 773 | error = -ENOSPC; |
| 774 | inode = hugetlbfs_get_inode(root->d_sb, current->fsuid, |
| 775 | current->fsgid, S_IFREG | S_IRWXUGO, 0); |
| 776 | if (!inode) |
| 777 | goto out_file; |
| 778 | |
David Gibson | b45b5bd | 2006-03-22 00:08:55 -0800 | [diff] [blame] | 779 | error = -ENOMEM; |
Chen, Kenneth W | a43a8c3 | 2006-06-23 02:03:15 -0700 | [diff] [blame] | 780 | if (hugetlb_reserve_pages(inode, 0, size >> HPAGE_SHIFT)) |
David Gibson | b45b5bd | 2006-03-22 00:08:55 -0800 | [diff] [blame] | 781 | goto out_inode; |
| 782 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | d_instantiate(dentry, inode); |
| 784 | inode->i_size = size; |
| 785 | inode->i_nlink = 0; |
Josef Sipek | b39424e | 2006-12-08 02:37:07 -0800 | [diff] [blame] | 786 | file->f_path.mnt = mntget(hugetlbfs_vfsmount); |
| 787 | file->f_path.dentry = dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | file->f_mapping = inode->i_mapping; |
| 789 | file->f_op = &hugetlbfs_file_operations; |
| 790 | file->f_mode = FMODE_WRITE | FMODE_READ; |
| 791 | return file; |
| 792 | |
David Gibson | b45b5bd | 2006-03-22 00:08:55 -0800 | [diff] [blame] | 793 | out_inode: |
| 794 | iput(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | out_file: |
| 796 | put_filp(file); |
| 797 | out_dentry: |
| 798 | dput(dentry); |
| 799 | out_shm_unlock: |
| 800 | user_shm_unlock(size, current->user); |
| 801 | return ERR_PTR(error); |
| 802 | } |
| 803 | |
| 804 | static int __init init_hugetlbfs_fs(void) |
| 805 | { |
| 806 | int error; |
| 807 | struct vfsmount *vfsmount; |
| 808 | |
| 809 | hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache", |
| 810 | sizeof(struct hugetlbfs_inode_info), |
| 811 | 0, 0, init_once, NULL); |
| 812 | if (hugetlbfs_inode_cachep == NULL) |
| 813 | return -ENOMEM; |
| 814 | |
| 815 | error = register_filesystem(&hugetlbfs_fs_type); |
| 816 | if (error) |
| 817 | goto out; |
| 818 | |
| 819 | vfsmount = kern_mount(&hugetlbfs_fs_type); |
| 820 | |
| 821 | if (!IS_ERR(vfsmount)) { |
| 822 | hugetlbfs_vfsmount = vfsmount; |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | error = PTR_ERR(vfsmount); |
| 827 | |
| 828 | out: |
| 829 | if (error) |
| 830 | kmem_cache_destroy(hugetlbfs_inode_cachep); |
| 831 | return error; |
| 832 | } |
| 833 | |
| 834 | static void __exit exit_hugetlbfs_fs(void) |
| 835 | { |
| 836 | kmem_cache_destroy(hugetlbfs_inode_cachep); |
| 837 | unregister_filesystem(&hugetlbfs_fs_type); |
| 838 | } |
| 839 | |
| 840 | module_init(init_hugetlbfs_fs) |
| 841 | module_exit(exit_hugetlbfs_fs) |
| 842 | |
| 843 | MODULE_LICENSE("GPL"); |