blob: 3a5b4e9234550200b252f3ca2333d3343aafcb4e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * hugetlbpage-backed filesystem. Based on ramfs.
3 *
4 * William Irwin, 2002
5 *
6 * Copyright (C) 2002 Linus Torvalds.
7 */
8
9#include <linux/module.h>
10#include <linux/thread_info.h>
11#include <asm/current.h>
12#include <linux/sched.h> /* remove ASAP */
13#include <linux/fs.h>
14#include <linux/mount.h>
15#include <linux/file.h>
16#include <linux/writeback.h>
17#include <linux/pagemap.h>
18#include <linux/highmem.h>
19#include <linux/init.h>
20#include <linux/string.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080021#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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
36static struct super_operations hugetlbfs_ops;
37static struct address_space_operations hugetlbfs_aops;
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080038const struct file_operations hugetlbfs_file_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static struct inode_operations hugetlbfs_dir_inode_operations;
40static struct inode_operations hugetlbfs_inode_operations;
41
42static 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
47int sysctl_hugetlb_shm_group;
48
Adam Litke2e9b3672005-10-29 18:16:47 -070049static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
60{
61 struct inode *inode = file->f_dentry->d_inode;
David Gibsonb45b5bd2006-03-22 00:08:55 -080062 struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 loff_t len, vma_len;
64 int ret;
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (vma->vm_pgoff & (HPAGE_SIZE / PAGE_SIZE - 1))
67 return -EINVAL;
68
69 if (vma->vm_start & ~HPAGE_MASK)
70 return -EINVAL;
71
72 if (vma->vm_end & ~HPAGE_MASK)
73 return -EINVAL;
74
75 if (vma->vm_end - vma->vm_start < HPAGE_SIZE)
76 return -EINVAL;
77
78 vma_len = (loff_t)(vma->vm_end - vma->vm_start);
79
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080080 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 file_accessed(file);
82 vma->vm_flags |= VM_HUGETLB | VM_RESERVED;
83 vma->vm_ops = &hugetlb_vm_ops;
84
85 ret = -ENOMEM;
86 len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
87 if (!(vma->vm_flags & VM_WRITE) && len > inode->i_size)
88 goto out;
89
David Gibsonb45b5bd2006-03-22 00:08:55 -080090 if (vma->vm_flags & VM_MAYSHARE)
91 if (hugetlb_extend_reservation(info, len >> HPAGE_SHIFT) != 0)
92 goto out;
93
Adam Litke4c887262005-10-29 18:16:46 -070094 ret = 0;
95 hugetlb_prefault_arch_hook(vma->vm_mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (inode->i_size < len)
97 inode->i_size = len;
98out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080099 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 return ret;
102}
103
104/*
Hugh Dickins508034a2005-10-29 18:16:30 -0700105 * Called under down_write(mmap_sem).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
107
108#ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
109unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
110 unsigned long len, unsigned long pgoff, unsigned long flags);
111#else
112static unsigned long
113hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
114 unsigned long len, unsigned long pgoff, unsigned long flags)
115{
116 struct mm_struct *mm = current->mm;
117 struct vm_area_struct *vma;
118 unsigned long start_addr;
119
120 if (len & ~HPAGE_MASK)
121 return -EINVAL;
122 if (len > TASK_SIZE)
123 return -ENOMEM;
124
125 if (addr) {
126 addr = ALIGN(addr, HPAGE_SIZE);
127 vma = find_vma(mm, addr);
128 if (TASK_SIZE - len >= addr &&
129 (!vma || addr + len <= vma->vm_start))
130 return addr;
131 }
132
133 start_addr = mm->free_area_cache;
134
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700135 if (len <= mm->cached_hole_size)
136 start_addr = TASK_UNMAPPED_BASE;
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138full_search:
139 addr = ALIGN(start_addr, HPAGE_SIZE);
140
141 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
142 /* At this point: (!vma || addr < vma->vm_end). */
143 if (TASK_SIZE - len < addr) {
144 /*
145 * Start a new search - just in case we missed
146 * some holes.
147 */
148 if (start_addr != TASK_UNMAPPED_BASE) {
149 start_addr = TASK_UNMAPPED_BASE;
150 goto full_search;
151 }
152 return -ENOMEM;
153 }
154
155 if (!vma || addr + len <= vma->vm_start)
156 return addr;
157 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
158 }
159}
160#endif
161
162/*
163 * Read a page. Again trivial. If it didn't already exist
164 * in the page cache, it is zero-filled.
165 */
166static int hugetlbfs_readpage(struct file *file, struct page * page)
167{
168 unlock_page(page);
169 return -EINVAL;
170}
171
172static int hugetlbfs_prepare_write(struct file *file,
173 struct page *page, unsigned offset, unsigned to)
174{
175 return -EINVAL;
176}
177
178static int hugetlbfs_commit_write(struct file *file,
179 struct page *page, unsigned offset, unsigned to)
180{
181 return -EINVAL;
182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static 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
David Gibsonb45b5bd2006-03-22 00:08:55 -0800192static void truncate_hugepages(struct inode *inode, loff_t lstart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
David Gibsonb45b5bd2006-03-22 00:08:55 -0800194 struct address_space *mapping = &inode->i_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 const pgoff_t start = lstart >> HPAGE_SHIFT;
196 struct pagevec pvec;
197 pgoff_t next;
198 int i;
199
David Gibsonb45b5bd2006-03-22 00:08:55 -0800200 hugetlb_truncate_reservation(HUGETLBFS_I(inode),
201 lstart >> HPAGE_SHIFT);
202 if (!mapping->nrpages)
203 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 pagevec_init(&pvec, 0);
205 next = start;
206 while (1) {
207 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
208 if (next == start)
209 break;
210 next = start;
211 continue;
212 }
213
214 for (i = 0; i < pagevec_count(&pvec); ++i) {
215 struct page *page = pvec.pages[i];
216
217 lock_page(page);
218 if (page->index > next)
219 next = page->index;
220 ++next;
221 truncate_huge_page(page);
222 unlock_page(page);
223 hugetlb_put_quota(mapping);
224 }
225 huge_pagevec_release(&pvec);
226 }
227 BUG_ON(!lstart && mapping->nrpages);
228}
229
230static void hugetlbfs_delete_inode(struct inode *inode)
231{
David Gibsonb45b5bd2006-03-22 00:08:55 -0800232 truncate_hugepages(inode, 0);
Christoph Hellwig149f4212005-10-29 18:16:43 -0700233 clear_inode(inode);
234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static void hugetlbfs_forget_inode(struct inode *inode)
237{
Christoph Hellwig0b1533f2005-10-29 18:16:45 -0700238 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Christoph Hellwig0b1533f2005-10-29 18:16:45 -0700240 if (!hlist_unhashed(&inode->i_hash)) {
241 if (!(inode->i_state & (I_DIRTY|I_LOCK)))
242 list_move(&inode->i_list, &inode_unused);
243 inodes_stat.nr_unused++;
244 if (!sb || (sb->s_flags & MS_ACTIVE)) {
245 spin_unlock(&inode_lock);
246 return;
247 }
248 inode->i_state |= I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 spin_unlock(&inode_lock);
Christoph Hellwig0b1533f2005-10-29 18:16:45 -0700250 /*
251 * write_inode_now is a noop as we set BDI_CAP_NO_WRITEBACK
252 * in our backing_dev_info.
253 */
254 write_inode_now(inode, 1);
255 spin_lock(&inode_lock);
256 inode->i_state &= ~I_WILL_FREE;
257 inodes_stat.nr_unused--;
258 hlist_del_init(&inode->i_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 list_del_init(&inode->i_list);
261 list_del_init(&inode->i_sb_list);
262 inode->i_state |= I_FREEING;
263 inodes_stat.nr_inodes--;
264 spin_unlock(&inode_lock);
David Gibsonb45b5bd2006-03-22 00:08:55 -0800265 truncate_hugepages(inode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 clear_inode(inode);
267 destroy_inode(inode);
268}
269
270static void hugetlbfs_drop_inode(struct inode *inode)
271{
272 if (!inode->i_nlink)
Christoph Hellwig6b09b9d2005-10-29 18:16:44 -0700273 generic_delete_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 else
275 hugetlbfs_forget_inode(inode);
276}
277
278/*
279 * h_pgoff is in HPAGE_SIZE units.
280 * vma->vm_pgoff is in PAGE_SIZE units.
281 */
282static inline void
283hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
284{
285 struct vm_area_struct *vma;
286 struct prio_tree_iter iter;
287
288 vma_prio_tree_foreach(vma, &iter, root, h_pgoff, ULONG_MAX) {
289 unsigned long h_vm_pgoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 unsigned long v_offset;
291
292 h_vm_pgoff = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
293 v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT;
294 /*
295 * Is this VMA fully outside the truncation point?
296 */
297 if (h_vm_pgoff >= h_pgoff)
298 v_offset = 0;
299
Hugh Dickins508034a2005-10-29 18:16:30 -0700300 unmap_hugepage_range(vma,
301 vma->vm_start + v_offset, vma->vm_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303}
304
305/*
306 * Expanding truncates are not allowed.
307 */
308static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
309{
310 unsigned long pgoff;
311 struct address_space *mapping = inode->i_mapping;
312
313 if (offset > inode->i_size)
314 return -EINVAL;
315
316 BUG_ON(offset & ~HPAGE_MASK);
317 pgoff = offset >> HPAGE_SHIFT;
318
319 inode->i_size = offset;
320 spin_lock(&mapping->i_mmap_lock);
321 if (!prio_tree_empty(&mapping->i_mmap))
322 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
323 spin_unlock(&mapping->i_mmap_lock);
David Gibsonb45b5bd2006-03-22 00:08:55 -0800324 truncate_hugepages(inode, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return 0;
326}
327
328static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
329{
330 struct inode *inode = dentry->d_inode;
331 int error;
332 unsigned int ia_valid = attr->ia_valid;
333
334 BUG_ON(!inode);
335
336 error = inode_change_ok(inode, attr);
337 if (error)
338 goto out;
339
340 if (ia_valid & ATTR_SIZE) {
341 error = -EINVAL;
342 if (!(attr->ia_size & ~HPAGE_MASK))
343 error = hugetlb_vmtruncate(inode, attr->ia_size);
344 if (error)
345 goto out;
346 attr->ia_valid &= ~ATTR_SIZE;
347 }
348 error = inode_setattr(inode, attr);
349out:
350 return error;
351}
352
353static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
354 gid_t gid, int mode, dev_t dev)
355{
356 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 inode = new_inode(sb);
359 if (inode) {
360 struct hugetlbfs_inode_info *info;
361 inode->i_mode = mode;
362 inode->i_uid = uid;
363 inode->i_gid = gid;
364 inode->i_blksize = HPAGE_SIZE;
365 inode->i_blocks = 0;
366 inode->i_mapping->a_ops = &hugetlbfs_aops;
367 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
368 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
369 info = HUGETLBFS_I(inode);
Robin Holt7339ff82006-01-14 13:20:48 -0800370 mpol_shared_policy_init(&info->policy, MPOL_DEFAULT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 switch (mode & S_IFMT) {
372 default:
373 init_special_inode(inode, mode, dev);
374 break;
375 case S_IFREG:
376 inode->i_op = &hugetlbfs_inode_operations;
377 inode->i_fop = &hugetlbfs_file_operations;
378 break;
379 case S_IFDIR:
380 inode->i_op = &hugetlbfs_dir_inode_operations;
381 inode->i_fop = &simple_dir_operations;
382
383 /* directory inodes start off with i_nlink == 2 (for "." entry) */
384 inode->i_nlink++;
385 break;
386 case S_IFLNK:
387 inode->i_op = &page_symlink_inode_operations;
388 break;
389 }
390 }
391 return inode;
392}
393
394/*
395 * File creation. Allocate an inode, and we're done..
396 */
397static int hugetlbfs_mknod(struct inode *dir,
398 struct dentry *dentry, int mode, dev_t dev)
399{
400 struct inode *inode;
401 int error = -ENOSPC;
402 gid_t gid;
403
404 if (dir->i_mode & S_ISGID) {
405 gid = dir->i_gid;
406 if (S_ISDIR(mode))
407 mode |= S_ISGID;
408 } else {
409 gid = current->fsgid;
410 }
411 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
412 if (inode) {
413 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
414 d_instantiate(dentry, inode);
415 dget(dentry); /* Extra count - pin the dentry in core */
416 error = 0;
417 }
418 return error;
419}
420
421static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
422{
423 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
424 if (!retval)
425 dir->i_nlink++;
426 return retval;
427}
428
429static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
430{
431 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
432}
433
434static int hugetlbfs_symlink(struct inode *dir,
435 struct dentry *dentry, const char *symname)
436{
437 struct inode *inode;
438 int error = -ENOSPC;
439 gid_t gid;
440
441 if (dir->i_mode & S_ISGID)
442 gid = dir->i_gid;
443 else
444 gid = current->fsgid;
445
446 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
447 gid, S_IFLNK|S_IRWXUGO, 0);
448 if (inode) {
449 int l = strlen(symname)+1;
450 error = page_symlink(inode, symname, l);
451 if (!error) {
452 d_instantiate(dentry, inode);
453 dget(dentry);
454 } else
455 iput(inode);
456 }
457 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
458
459 return error;
460}
461
462/*
463 * For direct-IO reads into hugetlb pages
464 */
465static int hugetlbfs_set_page_dirty(struct page *page)
466{
467 return 0;
468}
469
470static int hugetlbfs_statfs(struct super_block *sb, struct kstatfs *buf)
471{
472 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
473
474 buf->f_type = HUGETLBFS_MAGIC;
475 buf->f_bsize = HPAGE_SIZE;
476 if (sbinfo) {
477 spin_lock(&sbinfo->stat_lock);
David Gibson74a8a652005-11-21 21:32:24 -0800478 /* If no limits set, just report 0 for max/free/used
479 * blocks, like simple_statfs() */
480 if (sbinfo->max_blocks >= 0) {
481 buf->f_blocks = sbinfo->max_blocks;
482 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
483 buf->f_files = sbinfo->max_inodes;
484 buf->f_ffree = sbinfo->free_inodes;
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 spin_unlock(&sbinfo->stat_lock);
487 }
488 buf->f_namelen = NAME_MAX;
489 return 0;
490}
491
492static void hugetlbfs_put_super(struct super_block *sb)
493{
494 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
495
496 if (sbi) {
497 sb->s_fs_info = NULL;
498 kfree(sbi);
499 }
500}
501
Christoph Hellwig96527982005-10-29 18:16:42 -0700502static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
503{
504 if (sbinfo->free_inodes >= 0) {
505 spin_lock(&sbinfo->stat_lock);
506 if (unlikely(!sbinfo->free_inodes)) {
507 spin_unlock(&sbinfo->stat_lock);
508 return 0;
509 }
510 sbinfo->free_inodes--;
511 spin_unlock(&sbinfo->stat_lock);
512 }
513
514 return 1;
515}
516
517static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
518{
519 if (sbinfo->free_inodes >= 0) {
520 spin_lock(&sbinfo->stat_lock);
521 sbinfo->free_inodes++;
522 spin_unlock(&sbinfo->stat_lock);
523 }
524}
525
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527static kmem_cache_t *hugetlbfs_inode_cachep;
528
529static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
530{
Christoph Hellwig96527982005-10-29 18:16:42 -0700531 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 struct hugetlbfs_inode_info *p;
533
Christoph Hellwig96527982005-10-29 18:16:42 -0700534 if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return NULL;
Christoph Hellwig96527982005-10-29 18:16:42 -0700536 p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
537 if (unlikely(!p)) {
538 hugetlbfs_inc_free_inodes(sbinfo);
539 return NULL;
540 }
David Gibsonb45b5bd2006-03-22 00:08:55 -0800541 p->prereserved_hpages = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return &p->vfs_inode;
543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545static void hugetlbfs_destroy_inode(struct inode *inode)
546{
Christoph Hellwig96527982005-10-29 18:16:42 -0700547 hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
549 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
550}
551
552static struct address_space_operations hugetlbfs_aops = {
553 .readpage = hugetlbfs_readpage,
554 .prepare_write = hugetlbfs_prepare_write,
555 .commit_write = hugetlbfs_commit_write,
556 .set_page_dirty = hugetlbfs_set_page_dirty,
557};
558
Christoph Hellwig96527982005-10-29 18:16:42 -0700559
560static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
561{
562 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
563
564 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
565 SLAB_CTOR_CONSTRUCTOR)
566 inode_init_once(&ei->vfs_inode);
567}
568
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800569const struct file_operations hugetlbfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 .mmap = hugetlbfs_file_mmap,
571 .fsync = simple_sync_file,
572 .get_unmapped_area = hugetlb_get_unmapped_area,
573};
574
575static struct inode_operations hugetlbfs_dir_inode_operations = {
576 .create = hugetlbfs_create,
577 .lookup = simple_lookup,
578 .link = simple_link,
579 .unlink = simple_unlink,
580 .symlink = hugetlbfs_symlink,
581 .mkdir = hugetlbfs_mkdir,
582 .rmdir = simple_rmdir,
583 .mknod = hugetlbfs_mknod,
584 .rename = simple_rename,
585 .setattr = hugetlbfs_setattr,
586};
587
588static struct inode_operations hugetlbfs_inode_operations = {
589 .setattr = hugetlbfs_setattr,
590};
591
592static struct super_operations hugetlbfs_ops = {
593 .alloc_inode = hugetlbfs_alloc_inode,
594 .destroy_inode = hugetlbfs_destroy_inode,
595 .statfs = hugetlbfs_statfs,
Christoph Hellwig149f4212005-10-29 18:16:43 -0700596 .delete_inode = hugetlbfs_delete_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 .drop_inode = hugetlbfs_drop_inode,
598 .put_super = hugetlbfs_put_super,
599};
600
601static int
602hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
603{
604 char *opt, *value, *rest;
605
606 if (!options)
607 return 0;
608 while ((opt = strsep(&options, ",")) != NULL) {
609 if (!*opt)
610 continue;
611
612 value = strchr(opt, '=');
613 if (!value || !*value)
614 return -EINVAL;
615 else
616 *value++ = '\0';
617
618 if (!strcmp(opt, "uid"))
619 pconfig->uid = simple_strtoul(value, &value, 0);
620 else if (!strcmp(opt, "gid"))
621 pconfig->gid = simple_strtoul(value, &value, 0);
622 else if (!strcmp(opt, "mode"))
623 pconfig->mode = simple_strtoul(value,&value,0) & 0777U;
624 else if (!strcmp(opt, "size")) {
625 unsigned long long size = memparse(value, &rest);
626 if (*rest == '%') {
627 size <<= HPAGE_SHIFT;
628 size *= max_huge_pages;
629 do_div(size, 100);
630 rest++;
631 }
632 size &= HPAGE_MASK;
633 pconfig->nr_blocks = (size >> HPAGE_SHIFT);
634 value = rest;
635 } else if (!strcmp(opt,"nr_inodes")) {
636 pconfig->nr_inodes = memparse(value, &rest);
637 value = rest;
638 } else
639 return -EINVAL;
640
641 if (*value)
642 return -EINVAL;
643 }
644 return 0;
645}
646
647static int
648hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
649{
650 struct inode * inode;
651 struct dentry * root;
652 int ret;
653 struct hugetlbfs_config config;
654 struct hugetlbfs_sb_info *sbinfo;
655
656 config.nr_blocks = -1; /* No limit on size by default */
657 config.nr_inodes = -1; /* No limit on number of inodes by default */
658 config.uid = current->fsuid;
659 config.gid = current->fsgid;
660 config.mode = 0755;
661 ret = hugetlbfs_parse_options(data, &config);
662
663 if (ret)
664 return ret;
665
666 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
667 if (!sbinfo)
668 return -ENOMEM;
669 sb->s_fs_info = sbinfo;
670 spin_lock_init(&sbinfo->stat_lock);
671 sbinfo->max_blocks = config.nr_blocks;
672 sbinfo->free_blocks = config.nr_blocks;
673 sbinfo->max_inodes = config.nr_inodes;
674 sbinfo->free_inodes = config.nr_inodes;
675 sb->s_maxbytes = MAX_LFS_FILESIZE;
676 sb->s_blocksize = HPAGE_SIZE;
677 sb->s_blocksize_bits = HPAGE_SHIFT;
678 sb->s_magic = HUGETLBFS_MAGIC;
679 sb->s_op = &hugetlbfs_ops;
680 sb->s_time_gran = 1;
681 inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
682 S_IFDIR | config.mode, 0);
683 if (!inode)
684 goto out_free;
685
686 root = d_alloc_root(inode);
687 if (!root) {
688 iput(inode);
689 goto out_free;
690 }
691 sb->s_root = root;
692 return 0;
693out_free:
694 kfree(sbinfo);
695 return -ENOMEM;
696}
697
698int hugetlb_get_quota(struct address_space *mapping)
699{
700 int ret = 0;
701 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
702
703 if (sbinfo->free_blocks > -1) {
704 spin_lock(&sbinfo->stat_lock);
705 if (sbinfo->free_blocks > 0)
706 sbinfo->free_blocks--;
707 else
708 ret = -ENOMEM;
709 spin_unlock(&sbinfo->stat_lock);
710 }
711
712 return ret;
713}
714
715void hugetlb_put_quota(struct address_space *mapping)
716{
717 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
718
719 if (sbinfo->free_blocks > -1) {
720 spin_lock(&sbinfo->stat_lock);
721 sbinfo->free_blocks++;
722 spin_unlock(&sbinfo->stat_lock);
723 }
724}
725
726static struct super_block *hugetlbfs_get_sb(struct file_system_type *fs_type,
727 int flags, const char *dev_name, void *data)
728{
729 return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super);
730}
731
732static struct file_system_type hugetlbfs_fs_type = {
733 .name = "hugetlbfs",
734 .get_sb = hugetlbfs_get_sb,
735 .kill_sb = kill_litter_super,
736};
737
738static struct vfsmount *hugetlbfs_vfsmount;
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740static int can_do_hugetlb_shm(void)
741{
742 return likely(capable(CAP_IPC_LOCK) ||
743 in_group_p(sysctl_hugetlb_shm_group) ||
744 can_do_mlock());
745}
746
747struct file *hugetlb_zero_setup(size_t size)
748{
749 int error = -ENOMEM;
750 struct file *file;
751 struct inode *inode;
752 struct dentry *dentry, *root;
753 struct qstr quick_string;
754 char buf[16];
Chen, Kenneth Wbba1e9b2006-03-22 00:09:02 -0800755 static atomic_t counter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 if (!can_do_hugetlb_shm())
758 return ERR_PTR(-EPERM);
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (!user_shm_lock(size, current->user))
761 return ERR_PTR(-ENOMEM);
762
763 root = hugetlbfs_vfsmount->mnt_root;
Chen, Kenneth Wbba1e9b2006-03-22 00:09:02 -0800764 snprintf(buf, 16, "%u", atomic_inc_return(&counter));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 quick_string.name = buf;
766 quick_string.len = strlen(quick_string.name);
767 quick_string.hash = 0;
768 dentry = d_alloc(root, &quick_string);
769 if (!dentry)
770 goto out_shm_unlock;
771
772 error = -ENFILE;
773 file = get_empty_filp();
774 if (!file)
775 goto out_dentry;
776
777 error = -ENOSPC;
778 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
779 current->fsgid, S_IFREG | S_IRWXUGO, 0);
780 if (!inode)
781 goto out_file;
782
David Gibsonb45b5bd2006-03-22 00:08:55 -0800783 error = -ENOMEM;
784 if (hugetlb_extend_reservation(HUGETLBFS_I(inode),
785 size >> HPAGE_SHIFT) != 0)
786 goto out_inode;
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 d_instantiate(dentry, inode);
789 inode->i_size = size;
790 inode->i_nlink = 0;
791 file->f_vfsmnt = mntget(hugetlbfs_vfsmount);
792 file->f_dentry = dentry;
793 file->f_mapping = inode->i_mapping;
794 file->f_op = &hugetlbfs_file_operations;
795 file->f_mode = FMODE_WRITE | FMODE_READ;
796 return file;
797
David Gibsonb45b5bd2006-03-22 00:08:55 -0800798out_inode:
799 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800out_file:
801 put_filp(file);
802out_dentry:
803 dput(dentry);
804out_shm_unlock:
805 user_shm_unlock(size, current->user);
806 return ERR_PTR(error);
807}
808
809static int __init init_hugetlbfs_fs(void)
810{
811 int error;
812 struct vfsmount *vfsmount;
813
814 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
815 sizeof(struct hugetlbfs_inode_info),
816 0, 0, init_once, NULL);
817 if (hugetlbfs_inode_cachep == NULL)
818 return -ENOMEM;
819
820 error = register_filesystem(&hugetlbfs_fs_type);
821 if (error)
822 goto out;
823
824 vfsmount = kern_mount(&hugetlbfs_fs_type);
825
826 if (!IS_ERR(vfsmount)) {
827 hugetlbfs_vfsmount = vfsmount;
828 return 0;
829 }
830
831 error = PTR_ERR(vfsmount);
832
833 out:
834 if (error)
835 kmem_cache_destroy(hugetlbfs_inode_cachep);
836 return error;
837}
838
839static void __exit exit_hugetlbfs_fs(void)
840{
841 kmem_cache_destroy(hugetlbfs_inode_cachep);
842 unregister_filesystem(&hugetlbfs_fs_type);
843}
844
845module_init(init_hugetlbfs_fs)
846module_exit(exit_hugetlbfs_fs)
847
848MODULE_LICENSE("GPL");