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