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