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