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