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