xref: /openbmc/linux/fs/hugetlbfs/inode.c (revision 63489f8e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * hugetlbpage-backed filesystem.  Based on ramfs.
31da177e4SLinus Torvalds  *
46d49e352SNadia Yvette Chambers  * Nadia Yvette Chambers, 2002
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 2002 Linus Torvalds.
73e89e1c5SPaul Gortmaker  * License: GPL
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
109b857d26SAndrew Morton #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
119b857d26SAndrew Morton 
121da177e4SLinus Torvalds #include <linux/thread_info.h>
131da177e4SLinus Torvalds #include <asm/current.h>
14174cd4b1SIngo Molnar #include <linux/sched/signal.h>		/* remove ASAP */
1570c3547eSMike Kravetz #include <linux/falloc.h>
161da177e4SLinus Torvalds #include <linux/fs.h>
171da177e4SLinus Torvalds #include <linux/mount.h>
181da177e4SLinus Torvalds #include <linux/file.h>
19e73a75faSRandy Dunlap #include <linux/kernel.h>
201da177e4SLinus Torvalds #include <linux/writeback.h>
211da177e4SLinus Torvalds #include <linux/pagemap.h>
221da177e4SLinus Torvalds #include <linux/highmem.h>
231da177e4SLinus Torvalds #include <linux/init.h>
241da177e4SLinus Torvalds #include <linux/string.h>
2516f7e0feSRandy Dunlap #include <linux/capability.h>
26e73a75faSRandy Dunlap #include <linux/ctype.h>
271da177e4SLinus Torvalds #include <linux/backing-dev.h>
281da177e4SLinus Torvalds #include <linux/hugetlb.h>
291da177e4SLinus Torvalds #include <linux/pagevec.h>
30e73a75faSRandy Dunlap #include <linux/parser.h>
31036e0856SBenjamin Herrenschmidt #include <linux/mman.h>
321da177e4SLinus Torvalds #include <linux/slab.h>
331da177e4SLinus Torvalds #include <linux/dnotify.h>
341da177e4SLinus Torvalds #include <linux/statfs.h>
351da177e4SLinus Torvalds #include <linux/security.h>
361fd7317dSNick Black #include <linux/magic.h>
37290408d4SNaoya Horiguchi #include <linux/migrate.h>
3834d0640eSAl Viro #include <linux/uio.h>
391da177e4SLinus Torvalds 
407c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
411da177e4SLinus Torvalds 
42ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations hugetlbfs_ops;
43f5e54d6eSChristoph Hellwig static const struct address_space_operations hugetlbfs_aops;
444b6f5d20SArjan van de Ven const struct file_operations hugetlbfs_file_operations;
4592e1d5beSArjan van de Ven static const struct inode_operations hugetlbfs_dir_inode_operations;
4692e1d5beSArjan van de Ven static const struct inode_operations hugetlbfs_inode_operations;
471da177e4SLinus Torvalds 
48a1d776eeSDavid Gibson struct hugetlbfs_config {
494a25220dSDavid Howells 	struct hstate		*hstate;
504a25220dSDavid Howells 	long			max_hpages;
514a25220dSDavid Howells 	long			nr_inodes;
524a25220dSDavid Howells 	long			min_hpages;
53a0eb3a05SEric W. Biederman 	kuid_t			uid;
54a0eb3a05SEric W. Biederman 	kgid_t			gid;
55a1d776eeSDavid Gibson 	umode_t			mode;
56a1d776eeSDavid Gibson };
57a1d776eeSDavid Gibson 
581da177e4SLinus Torvalds int sysctl_hugetlb_shm_group;
591da177e4SLinus Torvalds 
60e73a75faSRandy Dunlap enum {
61e73a75faSRandy Dunlap 	Opt_size, Opt_nr_inodes,
62e73a75faSRandy Dunlap 	Opt_mode, Opt_uid, Opt_gid,
637ca02d0aSMike Kravetz 	Opt_pagesize, Opt_min_size,
64e73a75faSRandy Dunlap 	Opt_err,
65e73a75faSRandy Dunlap };
66e73a75faSRandy Dunlap 
67a447c093SSteven Whitehouse static const match_table_t tokens = {
68e73a75faSRandy Dunlap 	{Opt_size,	"size=%s"},
69e73a75faSRandy Dunlap 	{Opt_nr_inodes,	"nr_inodes=%s"},
70e73a75faSRandy Dunlap 	{Opt_mode,	"mode=%o"},
71e73a75faSRandy Dunlap 	{Opt_uid,	"uid=%u"},
72e73a75faSRandy Dunlap 	{Opt_gid,	"gid=%u"},
73a137e1ccSAndi Kleen 	{Opt_pagesize,	"pagesize=%s"},
747ca02d0aSMike Kravetz 	{Opt_min_size,	"min_size=%s"},
75e73a75faSRandy Dunlap 	{Opt_err,	NULL},
76e73a75faSRandy Dunlap };
77e73a75faSRandy Dunlap 
7870c3547eSMike Kravetz #ifdef CONFIG_NUMA
7970c3547eSMike Kravetz static inline void hugetlb_set_vma_policy(struct vm_area_struct *vma,
8070c3547eSMike Kravetz 					struct inode *inode, pgoff_t index)
8170c3547eSMike Kravetz {
8270c3547eSMike Kravetz 	vma->vm_policy = mpol_shared_policy_lookup(&HUGETLBFS_I(inode)->policy,
8370c3547eSMike Kravetz 							index);
8470c3547eSMike Kravetz }
8570c3547eSMike Kravetz 
8670c3547eSMike Kravetz static inline void hugetlb_drop_vma_policy(struct vm_area_struct *vma)
8770c3547eSMike Kravetz {
8870c3547eSMike Kravetz 	mpol_cond_put(vma->vm_policy);
8970c3547eSMike Kravetz }
9070c3547eSMike Kravetz #else
9170c3547eSMike Kravetz static inline void hugetlb_set_vma_policy(struct vm_area_struct *vma,
9270c3547eSMike Kravetz 					struct inode *inode, pgoff_t index)
9370c3547eSMike Kravetz {
9470c3547eSMike Kravetz }
9570c3547eSMike Kravetz 
9670c3547eSMike Kravetz static inline void hugetlb_drop_vma_policy(struct vm_area_struct *vma)
9770c3547eSMike Kravetz {
9870c3547eSMike Kravetz }
9970c3547eSMike Kravetz #endif
10070c3547eSMike Kravetz 
1012e9b367cSAdam Litke static void huge_pagevec_release(struct pagevec *pvec)
1022e9b367cSAdam Litke {
1032e9b367cSAdam Litke 	int i;
1042e9b367cSAdam Litke 
1052e9b367cSAdam Litke 	for (i = 0; i < pagevec_count(pvec); ++i)
1062e9b367cSAdam Litke 		put_page(pvec->pages[i]);
1072e9b367cSAdam Litke 
1082e9b367cSAdam Litke 	pagevec_reinit(pvec);
1092e9b367cSAdam Litke }
1102e9b367cSAdam Litke 
11163489f8eSMike Kravetz /*
11263489f8eSMike Kravetz  * Mask used when checking the page offset value passed in via system
11363489f8eSMike Kravetz  * calls.  This value will be converted to a loff_t which is signed.
11463489f8eSMike Kravetz  * Therefore, we want to check the upper PAGE_SHIFT + 1 bits of the
11563489f8eSMike Kravetz  * value.  The extra bit (- 1 in the shift value) is to take the sign
11663489f8eSMike Kravetz  * bit into account.
11763489f8eSMike Kravetz  */
11863489f8eSMike Kravetz #define PGOFF_LOFFT_MAX \
11963489f8eSMike Kravetz 	(((1UL << (PAGE_SHIFT + 1)) - 1) <<  (BITS_PER_LONG - (PAGE_SHIFT + 1)))
12063489f8eSMike Kravetz 
1211da177e4SLinus Torvalds static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
1221da177e4SLinus Torvalds {
123496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
1241da177e4SLinus Torvalds 	loff_t len, vma_len;
1251da177e4SLinus Torvalds 	int ret;
126a5516438SAndi Kleen 	struct hstate *h = hstate_file(file);
1271da177e4SLinus Torvalds 
12868589bc3SHugh Dickins 	/*
129dec4ad86SDavid Gibson 	 * vma address alignment (but not the pgoff alignment) has
130dec4ad86SDavid Gibson 	 * already been checked by prepare_hugepage_range.  If you add
131dec4ad86SDavid Gibson 	 * any error returns here, do so after setting VM_HUGETLB, so
132dec4ad86SDavid Gibson 	 * is_vm_hugetlb_page tests below unmap_region go the right
133dec4ad86SDavid Gibson 	 * way when do_mmap_pgoff unwinds (may be important on powerpc
134dec4ad86SDavid Gibson 	 * and ia64).
13568589bc3SHugh Dickins 	 */
136a2fce914SNaoya Horiguchi 	vma->vm_flags |= VM_HUGETLB | VM_DONTEXPAND;
13768589bc3SHugh Dickins 	vma->vm_ops = &hugetlb_vm_ops;
1381da177e4SLinus Torvalds 
139045c7a3fSMike Kravetz 	/*
14063489f8eSMike Kravetz 	 * page based offset in vm_pgoff could be sufficiently large to
14163489f8eSMike Kravetz 	 * overflow a (l)off_t when converted to byte offset.
142045c7a3fSMike Kravetz 	 */
14363489f8eSMike Kravetz 	if (vma->vm_pgoff & PGOFF_LOFFT_MAX)
144045c7a3fSMike Kravetz 		return -EINVAL;
145045c7a3fSMike Kravetz 
14663489f8eSMike Kravetz 	/* must be huge page aligned */
1472b37c35eSBecky Bruce 	if (vma->vm_pgoff & (~huge_page_mask(h) >> PAGE_SHIFT))
148dec4ad86SDavid Gibson 		return -EINVAL;
149dec4ad86SDavid Gibson 
1501da177e4SLinus Torvalds 	vma_len = (loff_t)(vma->vm_end - vma->vm_start);
151045c7a3fSMike Kravetz 	len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
152045c7a3fSMike Kravetz 	/* check for overflow */
153045c7a3fSMike Kravetz 	if (len < vma_len)
154045c7a3fSMike Kravetz 		return -EINVAL;
1551da177e4SLinus Torvalds 
1565955102cSAl Viro 	inode_lock(inode);
1571da177e4SLinus Torvalds 	file_accessed(file);
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	ret = -ENOMEM;
160a1e78772SMel Gorman 	if (hugetlb_reserve_pages(inode,
161a5516438SAndi Kleen 				vma->vm_pgoff >> huge_page_order(h),
1625a6fe125SMel Gorman 				len >> huge_page_shift(h), vma,
1635a6fe125SMel Gorman 				vma->vm_flags))
164b45b5bd6SDavid Gibson 		goto out;
165b45b5bd6SDavid Gibson 
1664c887265SAdam Litke 	ret = 0;
167b6174df5SZhang, Yanmin 	if (vma->vm_flags & VM_WRITE && inode->i_size < len)
168045c7a3fSMike Kravetz 		i_size_write(inode, len);
1691da177e4SLinus Torvalds out:
1705955102cSAl Viro 	inode_unlock(inode);
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	return ret;
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /*
176508034a3SHugh Dickins  * Called under down_write(mmap_sem).
1771da177e4SLinus Torvalds  */
1781da177e4SLinus Torvalds 
179d2ba27e8SAdrian Bunk #ifndef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
1801da177e4SLinus Torvalds static unsigned long
1811da177e4SLinus Torvalds hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
1821da177e4SLinus Torvalds 		unsigned long len, unsigned long pgoff, unsigned long flags)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
1851da177e4SLinus Torvalds 	struct vm_area_struct *vma;
186a5516438SAndi Kleen 	struct hstate *h = hstate_file(file);
18708659355SMichel Lespinasse 	struct vm_unmapped_area_info info;
1881da177e4SLinus Torvalds 
189a5516438SAndi Kleen 	if (len & ~huge_page_mask(h))
1901da177e4SLinus Torvalds 		return -EINVAL;
1911da177e4SLinus Torvalds 	if (len > TASK_SIZE)
1921da177e4SLinus Torvalds 		return -ENOMEM;
1931da177e4SLinus Torvalds 
194036e0856SBenjamin Herrenschmidt 	if (flags & MAP_FIXED) {
195a5516438SAndi Kleen 		if (prepare_hugepage_range(file, addr, len))
196036e0856SBenjamin Herrenschmidt 			return -EINVAL;
197036e0856SBenjamin Herrenschmidt 		return addr;
198036e0856SBenjamin Herrenschmidt 	}
199036e0856SBenjamin Herrenschmidt 
2001da177e4SLinus Torvalds 	if (addr) {
201a5516438SAndi Kleen 		addr = ALIGN(addr, huge_page_size(h));
2021da177e4SLinus Torvalds 		vma = find_vma(mm, addr);
2031da177e4SLinus Torvalds 		if (TASK_SIZE - len >= addr &&
2041be7107fSHugh Dickins 		    (!vma || addr + len <= vm_start_gap(vma)))
2051da177e4SLinus Torvalds 			return addr;
2061da177e4SLinus Torvalds 	}
2071da177e4SLinus Torvalds 
20808659355SMichel Lespinasse 	info.flags = 0;
20908659355SMichel Lespinasse 	info.length = len;
21008659355SMichel Lespinasse 	info.low_limit = TASK_UNMAPPED_BASE;
21108659355SMichel Lespinasse 	info.high_limit = TASK_SIZE;
21208659355SMichel Lespinasse 	info.align_mask = PAGE_MASK & ~huge_page_mask(h);
21308659355SMichel Lespinasse 	info.align_offset = 0;
21408659355SMichel Lespinasse 	return vm_unmapped_area(&info);
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds #endif
2171da177e4SLinus Torvalds 
21834d0640eSAl Viro static size_t
219e63e1e5aSBadari Pulavarty hugetlbfs_read_actor(struct page *page, unsigned long offset,
22034d0640eSAl Viro 			struct iov_iter *to, unsigned long size)
221e63e1e5aSBadari Pulavarty {
22234d0640eSAl Viro 	size_t copied = 0;
223e63e1e5aSBadari Pulavarty 	int i, chunksize;
224e63e1e5aSBadari Pulavarty 
225e63e1e5aSBadari Pulavarty 	/* Find which 4k chunk and offset with in that chunk */
22609cbfeafSKirill A. Shutemov 	i = offset >> PAGE_SHIFT;
22709cbfeafSKirill A. Shutemov 	offset = offset & ~PAGE_MASK;
228e63e1e5aSBadari Pulavarty 
229e63e1e5aSBadari Pulavarty 	while (size) {
23034d0640eSAl Viro 		size_t n;
23109cbfeafSKirill A. Shutemov 		chunksize = PAGE_SIZE;
232e63e1e5aSBadari Pulavarty 		if (offset)
233e63e1e5aSBadari Pulavarty 			chunksize -= offset;
234e63e1e5aSBadari Pulavarty 		if (chunksize > size)
235e63e1e5aSBadari Pulavarty 			chunksize = size;
23634d0640eSAl Viro 		n = copy_page_to_iter(&page[i], offset, chunksize, to);
23734d0640eSAl Viro 		copied += n;
23834d0640eSAl Viro 		if (n != chunksize)
23934d0640eSAl Viro 			return copied;
240e63e1e5aSBadari Pulavarty 		offset = 0;
241e63e1e5aSBadari Pulavarty 		size -= chunksize;
242e63e1e5aSBadari Pulavarty 		i++;
243e63e1e5aSBadari Pulavarty 	}
24434d0640eSAl Viro 	return copied;
245e63e1e5aSBadari Pulavarty }
246e63e1e5aSBadari Pulavarty 
247e63e1e5aSBadari Pulavarty /*
248e63e1e5aSBadari Pulavarty  * Support for read() - Find the page attached to f_mapping and copy out the
249e63e1e5aSBadari Pulavarty  * data. Its *very* similar to do_generic_mapping_read(), we can't use that
250ea1754a0SKirill A. Shutemov  * since it has PAGE_SIZE assumptions.
251e63e1e5aSBadari Pulavarty  */
25234d0640eSAl Viro static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
253e63e1e5aSBadari Pulavarty {
25434d0640eSAl Viro 	struct file *file = iocb->ki_filp;
25534d0640eSAl Viro 	struct hstate *h = hstate_file(file);
25634d0640eSAl Viro 	struct address_space *mapping = file->f_mapping;
257e63e1e5aSBadari Pulavarty 	struct inode *inode = mapping->host;
25834d0640eSAl Viro 	unsigned long index = iocb->ki_pos >> huge_page_shift(h);
25934d0640eSAl Viro 	unsigned long offset = iocb->ki_pos & ~huge_page_mask(h);
260e63e1e5aSBadari Pulavarty 	unsigned long end_index;
261e63e1e5aSBadari Pulavarty 	loff_t isize;
262e63e1e5aSBadari Pulavarty 	ssize_t retval = 0;
263e63e1e5aSBadari Pulavarty 
26434d0640eSAl Viro 	while (iov_iter_count(to)) {
265e63e1e5aSBadari Pulavarty 		struct page *page;
26634d0640eSAl Viro 		size_t nr, copied;
267e63e1e5aSBadari Pulavarty 
268e63e1e5aSBadari Pulavarty 		/* nr is the maximum number of bytes to copy from this page */
269a5516438SAndi Kleen 		nr = huge_page_size(h);
270a05b0855SAneesh Kumar K.V 		isize = i_size_read(inode);
271a05b0855SAneesh Kumar K.V 		if (!isize)
27234d0640eSAl Viro 			break;
273a05b0855SAneesh Kumar K.V 		end_index = (isize - 1) >> huge_page_shift(h);
274e63e1e5aSBadari Pulavarty 		if (index > end_index)
27534d0640eSAl Viro 			break;
27634d0640eSAl Viro 		if (index == end_index) {
277a5516438SAndi Kleen 			nr = ((isize - 1) & ~huge_page_mask(h)) + 1;
278a05b0855SAneesh Kumar K.V 			if (nr <= offset)
27934d0640eSAl Viro 				break;
280e63e1e5aSBadari Pulavarty 		}
281e63e1e5aSBadari Pulavarty 		nr = nr - offset;
282e63e1e5aSBadari Pulavarty 
283e63e1e5aSBadari Pulavarty 		/* Find the page */
284a05b0855SAneesh Kumar K.V 		page = find_lock_page(mapping, index);
285e63e1e5aSBadari Pulavarty 		if (unlikely(page == NULL)) {
286e63e1e5aSBadari Pulavarty 			/*
287e63e1e5aSBadari Pulavarty 			 * We have a HOLE, zero out the user-buffer for the
288e63e1e5aSBadari Pulavarty 			 * length of the hole or request.
289e63e1e5aSBadari Pulavarty 			 */
29034d0640eSAl Viro 			copied = iov_iter_zero(nr, to);
291e63e1e5aSBadari Pulavarty 		} else {
292a05b0855SAneesh Kumar K.V 			unlock_page(page);
293a05b0855SAneesh Kumar K.V 
294e63e1e5aSBadari Pulavarty 			/*
295e63e1e5aSBadari Pulavarty 			 * We have the page, copy it to user space buffer.
296e63e1e5aSBadari Pulavarty 			 */
29734d0640eSAl Viro 			copied = hugetlbfs_read_actor(page, offset, to, nr);
29809cbfeafSKirill A. Shutemov 			put_page(page);
299e63e1e5aSBadari Pulavarty 		}
30034d0640eSAl Viro 		offset += copied;
30134d0640eSAl Viro 		retval += copied;
30234d0640eSAl Viro 		if (copied != nr && iov_iter_count(to)) {
30334d0640eSAl Viro 			if (!retval)
30434d0640eSAl Viro 				retval = -EFAULT;
305e63e1e5aSBadari Pulavarty 			break;
306e63e1e5aSBadari Pulavarty 		}
30734d0640eSAl Viro 		index += offset >> huge_page_shift(h);
30834d0640eSAl Viro 		offset &= ~huge_page_mask(h);
30934d0640eSAl Viro 	}
31034d0640eSAl Viro 	iocb->ki_pos = ((loff_t)index << huge_page_shift(h)) + offset;
311e63e1e5aSBadari Pulavarty 	return retval;
312e63e1e5aSBadari Pulavarty }
313e63e1e5aSBadari Pulavarty 
314800d15a5SNick Piggin static int hugetlbfs_write_begin(struct file *file,
315800d15a5SNick Piggin 			struct address_space *mapping,
316800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
317800d15a5SNick Piggin 			struct page **pagep, void **fsdata)
3181da177e4SLinus Torvalds {
3191da177e4SLinus Torvalds 	return -EINVAL;
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
322800d15a5SNick Piggin static int hugetlbfs_write_end(struct file *file, struct address_space *mapping,
323800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
324800d15a5SNick Piggin 			struct page *page, void *fsdata)
3251da177e4SLinus Torvalds {
326800d15a5SNick Piggin 	BUG();
3271da177e4SLinus Torvalds 	return -EINVAL;
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
330b5cec28dSMike Kravetz static void remove_huge_page(struct page *page)
3311da177e4SLinus Torvalds {
332b9ea2515SKonstantin Khlebnikov 	ClearPageDirty(page);
3331da177e4SLinus Torvalds 	ClearPageUptodate(page);
334bd65cb86SMinchan Kim 	delete_from_page_cache(page);
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
3374aae8d1cSMike Kravetz static void
338f808c13fSDavidlohr Bueso hugetlb_vmdelete_list(struct rb_root_cached *root, pgoff_t start, pgoff_t end)
3394aae8d1cSMike Kravetz {
3404aae8d1cSMike Kravetz 	struct vm_area_struct *vma;
3414aae8d1cSMike Kravetz 
3424aae8d1cSMike Kravetz 	/*
3434aae8d1cSMike Kravetz 	 * end == 0 indicates that the entire range after
3444aae8d1cSMike Kravetz 	 * start should be unmapped.
3454aae8d1cSMike Kravetz 	 */
3464aae8d1cSMike Kravetz 	vma_interval_tree_foreach(vma, root, start, end ? end : ULONG_MAX) {
3474aae8d1cSMike Kravetz 		unsigned long v_offset;
3484aae8d1cSMike Kravetz 		unsigned long v_end;
3494aae8d1cSMike Kravetz 
3504aae8d1cSMike Kravetz 		/*
3514aae8d1cSMike Kravetz 		 * Can the expression below overflow on 32-bit arches?
3524aae8d1cSMike Kravetz 		 * No, because the interval tree returns us only those vmas
3534aae8d1cSMike Kravetz 		 * which overlap the truncated area starting at pgoff,
3544aae8d1cSMike Kravetz 		 * and no vma on a 32-bit arch can span beyond the 4GB.
3554aae8d1cSMike Kravetz 		 */
3564aae8d1cSMike Kravetz 		if (vma->vm_pgoff < start)
3574aae8d1cSMike Kravetz 			v_offset = (start - vma->vm_pgoff) << PAGE_SHIFT;
3584aae8d1cSMike Kravetz 		else
3594aae8d1cSMike Kravetz 			v_offset = 0;
3604aae8d1cSMike Kravetz 
3614aae8d1cSMike Kravetz 		if (!end)
3624aae8d1cSMike Kravetz 			v_end = vma->vm_end;
3634aae8d1cSMike Kravetz 		else {
3644aae8d1cSMike Kravetz 			v_end = ((end - vma->vm_pgoff) << PAGE_SHIFT)
3654aae8d1cSMike Kravetz 							+ vma->vm_start;
3664aae8d1cSMike Kravetz 			if (v_end > vma->vm_end)
3674aae8d1cSMike Kravetz 				v_end = vma->vm_end;
3684aae8d1cSMike Kravetz 		}
3694aae8d1cSMike Kravetz 
3704aae8d1cSMike Kravetz 		unmap_hugepage_range(vma, vma->vm_start + v_offset, v_end,
3714aae8d1cSMike Kravetz 									NULL);
3724aae8d1cSMike Kravetz 	}
3734aae8d1cSMike Kravetz }
374b5cec28dSMike Kravetz 
375b5cec28dSMike Kravetz /*
376b5cec28dSMike Kravetz  * remove_inode_hugepages handles two distinct cases: truncation and hole
377b5cec28dSMike Kravetz  * punch.  There are subtle differences in operation for each case.
3784aae8d1cSMike Kravetz  *
379b5cec28dSMike Kravetz  * truncation is indicated by end of range being LLONG_MAX
380b5cec28dSMike Kravetz  *	In this case, we first scan the range and release found pages.
381b5cec28dSMike Kravetz  *	After releasing pages, hugetlb_unreserve_pages cleans up region/reserv
3821817889eSMike Kravetz  *	maps and global counts.  Page faults can not race with truncation
3831817889eSMike Kravetz  *	in this routine.  hugetlb_no_page() prevents page faults in the
3841817889eSMike Kravetz  *	truncated range.  It checks i_size before allocation, and again after
3851817889eSMike Kravetz  *	with the page table lock for the page held.  The same lock must be
3861817889eSMike Kravetz  *	acquired to unmap a page.
387b5cec28dSMike Kravetz  * hole punch is indicated if end is not LLONG_MAX
388b5cec28dSMike Kravetz  *	In the hole punch case we scan the range and release found pages.
389b5cec28dSMike Kravetz  *	Only when releasing a page is the associated region/reserv map
390b5cec28dSMike Kravetz  *	deleted.  The region/reserv map for ranges without associated
3911817889eSMike Kravetz  *	pages are not modified.  Page faults can race with hole punch.
3921817889eSMike Kravetz  *	This is indicated if we find a mapped page.
393b5cec28dSMike Kravetz  * Note: If the passed end of range value is beyond the end of file, but
394b5cec28dSMike Kravetz  * not LLONG_MAX this routine still performs a hole punch operation.
395b5cec28dSMike Kravetz  */
396b5cec28dSMike Kravetz static void remove_inode_hugepages(struct inode *inode, loff_t lstart,
397b5cec28dSMike Kravetz 				   loff_t lend)
3981da177e4SLinus Torvalds {
399a5516438SAndi Kleen 	struct hstate *h = hstate_inode(inode);
400b45b5bd6SDavid Gibson 	struct address_space *mapping = &inode->i_data;
401a5516438SAndi Kleen 	const pgoff_t start = lstart >> huge_page_shift(h);
402b5cec28dSMike Kravetz 	const pgoff_t end = lend >> huge_page_shift(h);
403b5cec28dSMike Kravetz 	struct vm_area_struct pseudo_vma;
4041da177e4SLinus Torvalds 	struct pagevec pvec;
405d72dc8a2SJan Kara 	pgoff_t next, index;
406a43a8c39SChen, Kenneth W 	int i, freed = 0;
407b5cec28dSMike Kravetz 	bool truncate_op = (lend == LLONG_MAX);
4081da177e4SLinus Torvalds 
409b5cec28dSMike Kravetz 	memset(&pseudo_vma, 0, sizeof(struct vm_area_struct));
410b5cec28dSMike Kravetz 	pseudo_vma.vm_flags = (VM_HUGETLB | VM_MAYSHARE | VM_SHARED);
41186679820SMel Gorman 	pagevec_init(&pvec);
4121da177e4SLinus Torvalds 	next = start;
413b5cec28dSMike Kravetz 	while (next < end) {
414b5cec28dSMike Kravetz 		/*
4151817889eSMike Kravetz 		 * When no more pages are found, we are done.
416b5cec28dSMike Kravetz 		 */
417397162ffSJan Kara 		if (!pagevec_lookup_range(&pvec, mapping, &next, end - 1))
4181da177e4SLinus Torvalds 			break;
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); ++i) {
4211da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
422b5cec28dSMike Kravetz 			u32 hash;
423b5cec28dSMike Kravetz 
424d72dc8a2SJan Kara 			index = page->index;
425b5cec28dSMike Kravetz 			hash = hugetlb_fault_mutex_hash(h, current->mm,
426b5cec28dSMike Kravetz 							&pseudo_vma,
427d72dc8a2SJan Kara 							mapping, index, 0);
428b5cec28dSMike Kravetz 			mutex_lock(&hugetlb_fault_mutex_table[hash]);
4291da177e4SLinus Torvalds 
430b5cec28dSMike Kravetz 			/*
4314aae8d1cSMike Kravetz 			 * If page is mapped, it was faulted in after being
4324aae8d1cSMike Kravetz 			 * unmapped in caller.  Unmap (again) now after taking
4334aae8d1cSMike Kravetz 			 * the fault mutex.  The mutex will prevent faults
4344aae8d1cSMike Kravetz 			 * until we finish removing the page.
4354aae8d1cSMike Kravetz 			 *
4364aae8d1cSMike Kravetz 			 * This race can only happen in the hole punch case.
4374aae8d1cSMike Kravetz 			 * Getting here in a truncate operation is a bug.
438b5cec28dSMike Kravetz 			 */
4394aae8d1cSMike Kravetz 			if (unlikely(page_mapped(page))) {
4404aae8d1cSMike Kravetz 				BUG_ON(truncate_op);
4414aae8d1cSMike Kravetz 
4424aae8d1cSMike Kravetz 				i_mmap_lock_write(mapping);
4434aae8d1cSMike Kravetz 				hugetlb_vmdelete_list(&mapping->i_mmap,
444d72dc8a2SJan Kara 					index * pages_per_huge_page(h),
445d72dc8a2SJan Kara 					(index + 1) * pages_per_huge_page(h));
4464aae8d1cSMike Kravetz 				i_mmap_unlock_write(mapping);
4474aae8d1cSMike Kravetz 			}
4484aae8d1cSMike Kravetz 
4494aae8d1cSMike Kravetz 			lock_page(page);
4504aae8d1cSMike Kravetz 			/*
4514aae8d1cSMike Kravetz 			 * We must free the huge page and remove from page
4524aae8d1cSMike Kravetz 			 * cache (remove_huge_page) BEFORE removing the
4534aae8d1cSMike Kravetz 			 * region/reserve map (hugetlb_unreserve_pages).  In
4544aae8d1cSMike Kravetz 			 * rare out of memory conditions, removal of the
45572e2936cSzhong jiang 			 * region/reserve map could fail. Correspondingly,
45672e2936cSzhong jiang 			 * the subpool and global reserve usage count can need
45772e2936cSzhong jiang 			 * to be adjusted.
4584aae8d1cSMike Kravetz 			 */
45972e2936cSzhong jiang 			VM_BUG_ON(PagePrivate(page));
460b5cec28dSMike Kravetz 			remove_huge_page(page);
461b5cec28dSMike Kravetz 			freed++;
462b5cec28dSMike Kravetz 			if (!truncate_op) {
4634aae8d1cSMike Kravetz 				if (unlikely(hugetlb_unreserve_pages(inode,
464d72dc8a2SJan Kara 							index, index + 1, 1)))
46572e2936cSzhong jiang 					hugetlb_fix_reserve_counts(inode);
466b5cec28dSMike Kravetz 			}
467b5cec28dSMike Kravetz 
4681da177e4SLinus Torvalds 			unlock_page(page);
469b5cec28dSMike Kravetz 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
4701da177e4SLinus Torvalds 		}
4711da177e4SLinus Torvalds 		huge_pagevec_release(&pvec);
4721817889eSMike Kravetz 		cond_resched();
4731da177e4SLinus Torvalds 	}
474b5cec28dSMike Kravetz 
475b5cec28dSMike Kravetz 	if (truncate_op)
476b5cec28dSMike Kravetz 		(void)hugetlb_unreserve_pages(inode, start, LONG_MAX, freed);
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
4792bbbda30SAl Viro static void hugetlbfs_evict_inode(struct inode *inode)
4801da177e4SLinus Torvalds {
4819119a41eSJoonsoo Kim 	struct resv_map *resv_map;
4829119a41eSJoonsoo Kim 
483b5cec28dSMike Kravetz 	remove_inode_hugepages(inode, 0, LLONG_MAX);
4849119a41eSJoonsoo Kim 	resv_map = (struct resv_map *)inode->i_mapping->private_data;
4859119a41eSJoonsoo Kim 	/* root inode doesn't have the resv_map, so we should check it */
4869119a41eSJoonsoo Kim 	if (resv_map)
4879119a41eSJoonsoo Kim 		resv_map_release(&resv_map->refs);
488dbd5768fSJan Kara 	clear_inode(inode);
489149f4211SChristoph Hellwig }
490149f4211SChristoph Hellwig 
4911da177e4SLinus Torvalds static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
4921da177e4SLinus Torvalds {
493856fc295SHugh Dickins 	pgoff_t pgoff;
4941da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
495a5516438SAndi Kleen 	struct hstate *h = hstate_inode(inode);
4961da177e4SLinus Torvalds 
497a5516438SAndi Kleen 	BUG_ON(offset & ~huge_page_mask(h));
498856fc295SHugh Dickins 	pgoff = offset >> PAGE_SHIFT;
4991da177e4SLinus Torvalds 
5007aa91e10SKen Chen 	i_size_write(inode, offset);
50183cde9e8SDavidlohr Bueso 	i_mmap_lock_write(mapping);
502f808c13fSDavidlohr Bueso 	if (!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))
5031bfad99aSMike Kravetz 		hugetlb_vmdelete_list(&mapping->i_mmap, pgoff, 0);
50483cde9e8SDavidlohr Bueso 	i_mmap_unlock_write(mapping);
505b5cec28dSMike Kravetz 	remove_inode_hugepages(inode, offset, LLONG_MAX);
5061da177e4SLinus Torvalds 	return 0;
5071da177e4SLinus Torvalds }
5081da177e4SLinus Torvalds 
50970c3547eSMike Kravetz static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
51070c3547eSMike Kravetz {
51170c3547eSMike Kravetz 	struct hstate *h = hstate_inode(inode);
51270c3547eSMike Kravetz 	loff_t hpage_size = huge_page_size(h);
51370c3547eSMike Kravetz 	loff_t hole_start, hole_end;
51470c3547eSMike Kravetz 
51570c3547eSMike Kravetz 	/*
51670c3547eSMike Kravetz 	 * For hole punch round up the beginning offset of the hole and
51770c3547eSMike Kravetz 	 * round down the end.
51870c3547eSMike Kravetz 	 */
51970c3547eSMike Kravetz 	hole_start = round_up(offset, hpage_size);
52070c3547eSMike Kravetz 	hole_end = round_down(offset + len, hpage_size);
52170c3547eSMike Kravetz 
52270c3547eSMike Kravetz 	if (hole_end > hole_start) {
52370c3547eSMike Kravetz 		struct address_space *mapping = inode->i_mapping;
524ff62a342SMarc-André Lureau 		struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
52570c3547eSMike Kravetz 
5265955102cSAl Viro 		inode_lock(inode);
527ff62a342SMarc-André Lureau 
528ff62a342SMarc-André Lureau 		/* protected by i_mutex */
529ff62a342SMarc-André Lureau 		if (info->seals & F_SEAL_WRITE) {
530ff62a342SMarc-André Lureau 			inode_unlock(inode);
531ff62a342SMarc-André Lureau 			return -EPERM;
532ff62a342SMarc-André Lureau 		}
533ff62a342SMarc-André Lureau 
53470c3547eSMike Kravetz 		i_mmap_lock_write(mapping);
535f808c13fSDavidlohr Bueso 		if (!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))
53670c3547eSMike Kravetz 			hugetlb_vmdelete_list(&mapping->i_mmap,
53770c3547eSMike Kravetz 						hole_start >> PAGE_SHIFT,
53870c3547eSMike Kravetz 						hole_end  >> PAGE_SHIFT);
53970c3547eSMike Kravetz 		i_mmap_unlock_write(mapping);
54070c3547eSMike Kravetz 		remove_inode_hugepages(inode, hole_start, hole_end);
5415955102cSAl Viro 		inode_unlock(inode);
54270c3547eSMike Kravetz 	}
54370c3547eSMike Kravetz 
54470c3547eSMike Kravetz 	return 0;
54570c3547eSMike Kravetz }
54670c3547eSMike Kravetz 
54770c3547eSMike Kravetz static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
54870c3547eSMike Kravetz 				loff_t len)
54970c3547eSMike Kravetz {
55070c3547eSMike Kravetz 	struct inode *inode = file_inode(file);
551ff62a342SMarc-André Lureau 	struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
55270c3547eSMike Kravetz 	struct address_space *mapping = inode->i_mapping;
55370c3547eSMike Kravetz 	struct hstate *h = hstate_inode(inode);
55470c3547eSMike Kravetz 	struct vm_area_struct pseudo_vma;
55570c3547eSMike Kravetz 	struct mm_struct *mm = current->mm;
55670c3547eSMike Kravetz 	loff_t hpage_size = huge_page_size(h);
55770c3547eSMike Kravetz 	unsigned long hpage_shift = huge_page_shift(h);
55870c3547eSMike Kravetz 	pgoff_t start, index, end;
55970c3547eSMike Kravetz 	int error;
56070c3547eSMike Kravetz 	u32 hash;
56170c3547eSMike Kravetz 
56270c3547eSMike Kravetz 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
56370c3547eSMike Kravetz 		return -EOPNOTSUPP;
56470c3547eSMike Kravetz 
56570c3547eSMike Kravetz 	if (mode & FALLOC_FL_PUNCH_HOLE)
56670c3547eSMike Kravetz 		return hugetlbfs_punch_hole(inode, offset, len);
56770c3547eSMike Kravetz 
56870c3547eSMike Kravetz 	/*
56970c3547eSMike Kravetz 	 * Default preallocate case.
57070c3547eSMike Kravetz 	 * For this range, start is rounded down and end is rounded up
57170c3547eSMike Kravetz 	 * as well as being converted to page offsets.
57270c3547eSMike Kravetz 	 */
57370c3547eSMike Kravetz 	start = offset >> hpage_shift;
57470c3547eSMike Kravetz 	end = (offset + len + hpage_size - 1) >> hpage_shift;
57570c3547eSMike Kravetz 
5765955102cSAl Viro 	inode_lock(inode);
57770c3547eSMike Kravetz 
57870c3547eSMike Kravetz 	/* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
57970c3547eSMike Kravetz 	error = inode_newsize_ok(inode, offset + len);
58070c3547eSMike Kravetz 	if (error)
58170c3547eSMike Kravetz 		goto out;
58270c3547eSMike Kravetz 
583ff62a342SMarc-André Lureau 	if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
584ff62a342SMarc-André Lureau 		error = -EPERM;
585ff62a342SMarc-André Lureau 		goto out;
586ff62a342SMarc-André Lureau 	}
587ff62a342SMarc-André Lureau 
58870c3547eSMike Kravetz 	/*
58970c3547eSMike Kravetz 	 * Initialize a pseudo vma as this is required by the huge page
59070c3547eSMike Kravetz 	 * allocation routines.  If NUMA is configured, use page index
59170c3547eSMike Kravetz 	 * as input to create an allocation policy.
59270c3547eSMike Kravetz 	 */
59370c3547eSMike Kravetz 	memset(&pseudo_vma, 0, sizeof(struct vm_area_struct));
59470c3547eSMike Kravetz 	pseudo_vma.vm_flags = (VM_HUGETLB | VM_MAYSHARE | VM_SHARED);
59570c3547eSMike Kravetz 	pseudo_vma.vm_file = file;
59670c3547eSMike Kravetz 
59770c3547eSMike Kravetz 	for (index = start; index < end; index++) {
59870c3547eSMike Kravetz 		/*
59970c3547eSMike Kravetz 		 * This is supposed to be the vaddr where the page is being
60070c3547eSMike Kravetz 		 * faulted in, but we have no vaddr here.
60170c3547eSMike Kravetz 		 */
60270c3547eSMike Kravetz 		struct page *page;
60370c3547eSMike Kravetz 		unsigned long addr;
60470c3547eSMike Kravetz 		int avoid_reserve = 0;
60570c3547eSMike Kravetz 
60670c3547eSMike Kravetz 		cond_resched();
60770c3547eSMike Kravetz 
60870c3547eSMike Kravetz 		/*
60970c3547eSMike Kravetz 		 * fallocate(2) manpage permits EINTR; we may have been
61070c3547eSMike Kravetz 		 * interrupted because we are using up too much memory.
61170c3547eSMike Kravetz 		 */
61270c3547eSMike Kravetz 		if (signal_pending(current)) {
61370c3547eSMike Kravetz 			error = -EINTR;
61470c3547eSMike Kravetz 			break;
61570c3547eSMike Kravetz 		}
61670c3547eSMike Kravetz 
61770c3547eSMike Kravetz 		/* Set numa allocation policy based on index */
61870c3547eSMike Kravetz 		hugetlb_set_vma_policy(&pseudo_vma, inode, index);
61970c3547eSMike Kravetz 
62070c3547eSMike Kravetz 		/* addr is the offset within the file (zero based) */
62170c3547eSMike Kravetz 		addr = index * hpage_size;
62270c3547eSMike Kravetz 
62370c3547eSMike Kravetz 		/* mutex taken here, fault path and hole punch */
62470c3547eSMike Kravetz 		hash = hugetlb_fault_mutex_hash(h, mm, &pseudo_vma, mapping,
62570c3547eSMike Kravetz 						index, addr);
62670c3547eSMike Kravetz 		mutex_lock(&hugetlb_fault_mutex_table[hash]);
62770c3547eSMike Kravetz 
62870c3547eSMike Kravetz 		/* See if already present in mapping to avoid alloc/free */
62970c3547eSMike Kravetz 		page = find_get_page(mapping, index);
63070c3547eSMike Kravetz 		if (page) {
63170c3547eSMike Kravetz 			put_page(page);
63270c3547eSMike Kravetz 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
63370c3547eSMike Kravetz 			hugetlb_drop_vma_policy(&pseudo_vma);
63470c3547eSMike Kravetz 			continue;
63570c3547eSMike Kravetz 		}
63670c3547eSMike Kravetz 
63770c3547eSMike Kravetz 		/* Allocate page and add to page cache */
63870c3547eSMike Kravetz 		page = alloc_huge_page(&pseudo_vma, addr, avoid_reserve);
63970c3547eSMike Kravetz 		hugetlb_drop_vma_policy(&pseudo_vma);
64070c3547eSMike Kravetz 		if (IS_ERR(page)) {
64170c3547eSMike Kravetz 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
64270c3547eSMike Kravetz 			error = PTR_ERR(page);
64370c3547eSMike Kravetz 			goto out;
64470c3547eSMike Kravetz 		}
64570c3547eSMike Kravetz 		clear_huge_page(page, addr, pages_per_huge_page(h));
64670c3547eSMike Kravetz 		__SetPageUptodate(page);
64770c3547eSMike Kravetz 		error = huge_add_to_page_cache(page, mapping, index);
64870c3547eSMike Kravetz 		if (unlikely(error)) {
64970c3547eSMike Kravetz 			put_page(page);
65070c3547eSMike Kravetz 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
65170c3547eSMike Kravetz 			goto out;
65270c3547eSMike Kravetz 		}
65370c3547eSMike Kravetz 
65470c3547eSMike Kravetz 		mutex_unlock(&hugetlb_fault_mutex_table[hash]);
65570c3547eSMike Kravetz 
65670c3547eSMike Kravetz 		/*
65770c3547eSMike Kravetz 		 * unlock_page because locked by add_to_page_cache()
65872639e6dSNadav Amit 		 * page_put due to reference from alloc_huge_page()
65970c3547eSMike Kravetz 		 */
66070c3547eSMike Kravetz 		unlock_page(page);
66172639e6dSNadav Amit 		put_page(page);
66270c3547eSMike Kravetz 	}
66370c3547eSMike Kravetz 
66470c3547eSMike Kravetz 	if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
66570c3547eSMike Kravetz 		i_size_write(inode, offset + len);
666078cd827SDeepa Dinamani 	inode->i_ctime = current_time(inode);
66770c3547eSMike Kravetz out:
6685955102cSAl Viro 	inode_unlock(inode);
66970c3547eSMike Kravetz 	return error;
67070c3547eSMike Kravetz }
67170c3547eSMike Kravetz 
6721da177e4SLinus Torvalds static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
6731da177e4SLinus Torvalds {
6742b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
675a5516438SAndi Kleen 	struct hstate *h = hstate_inode(inode);
6761da177e4SLinus Torvalds 	int error;
6771da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
678ff62a342SMarc-André Lureau 	struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	BUG_ON(!inode);
6811da177e4SLinus Torvalds 
68231051c85SJan Kara 	error = setattr_prepare(dentry, attr);
6831da177e4SLinus Torvalds 	if (error)
6841025774cSChristoph Hellwig 		return error;
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds 	if (ia_valid & ATTR_SIZE) {
687ff62a342SMarc-André Lureau 		loff_t oldsize = inode->i_size;
688ff62a342SMarc-André Lureau 		loff_t newsize = attr->ia_size;
689ff62a342SMarc-André Lureau 
690ff62a342SMarc-André Lureau 		if (newsize & ~huge_page_mask(h))
6911025774cSChristoph Hellwig 			return -EINVAL;
692ff62a342SMarc-André Lureau 		/* protected by i_mutex */
693ff62a342SMarc-André Lureau 		if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
694ff62a342SMarc-André Lureau 		    (newsize > oldsize && (info->seals & F_SEAL_GROW)))
695ff62a342SMarc-André Lureau 			return -EPERM;
696ff62a342SMarc-André Lureau 		error = hugetlb_vmtruncate(inode, newsize);
6971da177e4SLinus Torvalds 		if (error)
6981da177e4SLinus Torvalds 			return error;
6991da177e4SLinus Torvalds 	}
7001da177e4SLinus Torvalds 
7011025774cSChristoph Hellwig 	setattr_copy(inode, attr);
7021025774cSChristoph Hellwig 	mark_inode_dirty(inode);
7031025774cSChristoph Hellwig 	return 0;
7041025774cSChristoph Hellwig }
7051025774cSChristoph Hellwig 
7067d54fa64SAl Viro static struct inode *hugetlbfs_get_root(struct super_block *sb,
7077d54fa64SAl Viro 					struct hugetlbfs_config *config)
7081da177e4SLinus Torvalds {
7091da177e4SLinus Torvalds 	struct inode *inode;
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	inode = new_inode(sb);
7121da177e4SLinus Torvalds 	if (inode) {
71385fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
7147d54fa64SAl Viro 		inode->i_mode = S_IFDIR | config->mode;
7157d54fa64SAl Viro 		inode->i_uid = config->uid;
7167d54fa64SAl Viro 		inode->i_gid = config->gid;
717078cd827SDeepa Dinamani 		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
7187d54fa64SAl Viro 		inode->i_op = &hugetlbfs_dir_inode_operations;
7197d54fa64SAl Viro 		inode->i_fop = &simple_dir_operations;
7207d54fa64SAl Viro 		/* directory inodes start off with i_nlink == 2 (for "." entry) */
7217d54fa64SAl Viro 		inc_nlink(inode);
72265ed7601SAneesh Kumar K.V 		lockdep_annotate_inode_mutex_key(inode);
7237d54fa64SAl Viro 	}
7247d54fa64SAl Viro 	return inode;
7257d54fa64SAl Viro }
7267d54fa64SAl Viro 
727b610ded7SMichal Hocko /*
728c8c06efaSDavidlohr Bueso  * Hugetlbfs is not reclaimable; therefore its i_mmap_rwsem will never
729b610ded7SMichal Hocko  * be taken from reclaim -- unlike regular filesystems. This needs an
73088f306b6SKirill A. Shutemov  * annotation because huge_pmd_share() does an allocation under hugetlb's
731c8c06efaSDavidlohr Bueso  * i_mmap_rwsem.
732b610ded7SMichal Hocko  */
733c8c06efaSDavidlohr Bueso static struct lock_class_key hugetlbfs_i_mmap_rwsem_key;
734b610ded7SMichal Hocko 
7357d54fa64SAl Viro static struct inode *hugetlbfs_get_inode(struct super_block *sb,
7367d54fa64SAl Viro 					struct inode *dir,
73718df2252SAl Viro 					umode_t mode, dev_t dev)
7387d54fa64SAl Viro {
7397d54fa64SAl Viro 	struct inode *inode;
7409119a41eSJoonsoo Kim 	struct resv_map *resv_map;
7419119a41eSJoonsoo Kim 
7429119a41eSJoonsoo Kim 	resv_map = resv_map_alloc();
7439119a41eSJoonsoo Kim 	if (!resv_map)
7449119a41eSJoonsoo Kim 		return NULL;
7457d54fa64SAl Viro 
7467d54fa64SAl Viro 	inode = new_inode(sb);
7477d54fa64SAl Viro 	if (inode) {
748ff62a342SMarc-André Lureau 		struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
749ff62a342SMarc-André Lureau 
7507d54fa64SAl Viro 		inode->i_ino = get_next_ino();
7517d54fa64SAl Viro 		inode_init_owner(inode, dir, mode);
752c8c06efaSDavidlohr Bueso 		lockdep_set_class(&inode->i_mapping->i_mmap_rwsem,
753c8c06efaSDavidlohr Bueso 				&hugetlbfs_i_mmap_rwsem_key);
7541da177e4SLinus Torvalds 		inode->i_mapping->a_ops = &hugetlbfs_aops;
755078cd827SDeepa Dinamani 		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
7569119a41eSJoonsoo Kim 		inode->i_mapping->private_data = resv_map;
757ff62a342SMarc-André Lureau 		info->seals = F_SEAL_SEAL;
7581da177e4SLinus Torvalds 		switch (mode & S_IFMT) {
7591da177e4SLinus Torvalds 		default:
7601da177e4SLinus Torvalds 			init_special_inode(inode, mode, dev);
7611da177e4SLinus Torvalds 			break;
7621da177e4SLinus Torvalds 		case S_IFREG:
7631da177e4SLinus Torvalds 			inode->i_op = &hugetlbfs_inode_operations;
7641da177e4SLinus Torvalds 			inode->i_fop = &hugetlbfs_file_operations;
7651da177e4SLinus Torvalds 			break;
7661da177e4SLinus Torvalds 		case S_IFDIR:
7671da177e4SLinus Torvalds 			inode->i_op = &hugetlbfs_dir_inode_operations;
7681da177e4SLinus Torvalds 			inode->i_fop = &simple_dir_operations;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 			/* directory inodes start off with i_nlink == 2 (for "." entry) */
771d8c76e6fSDave Hansen 			inc_nlink(inode);
7721da177e4SLinus Torvalds 			break;
7731da177e4SLinus Torvalds 		case S_IFLNK:
7741da177e4SLinus Torvalds 			inode->i_op = &page_symlink_inode_operations;
77521fc61c7SAl Viro 			inode_nohighmem(inode);
7761da177e4SLinus Torvalds 			break;
7771da177e4SLinus Torvalds 		}
778e096d0c7SJosh Boyer 		lockdep_annotate_inode_mutex_key(inode);
7799119a41eSJoonsoo Kim 	} else
7809119a41eSJoonsoo Kim 		kref_put(&resv_map->refs, resv_map_release);
7819119a41eSJoonsoo Kim 
7821da177e4SLinus Torvalds 	return inode;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds /*
7861da177e4SLinus Torvalds  * File creation. Allocate an inode, and we're done..
7871da177e4SLinus Torvalds  */
7881da177e4SLinus Torvalds static int hugetlbfs_mknod(struct inode *dir,
7891a67aafbSAl Viro 			struct dentry *dentry, umode_t mode, dev_t dev)
7901da177e4SLinus Torvalds {
7911da177e4SLinus Torvalds 	struct inode *inode;
7921da177e4SLinus Torvalds 	int error = -ENOSPC;
7931da177e4SLinus Torvalds 
7947d54fa64SAl Viro 	inode = hugetlbfs_get_inode(dir->i_sb, dir, mode, dev);
7951da177e4SLinus Torvalds 	if (inode) {
796078cd827SDeepa Dinamani 		dir->i_ctime = dir->i_mtime = current_time(dir);
7971da177e4SLinus Torvalds 		d_instantiate(dentry, inode);
7981da177e4SLinus Torvalds 		dget(dentry);	/* Extra count - pin the dentry in core */
7991da177e4SLinus Torvalds 		error = 0;
8001da177e4SLinus Torvalds 	}
8011da177e4SLinus Torvalds 	return error;
8021da177e4SLinus Torvalds }
8031da177e4SLinus Torvalds 
80418bb1db3SAl Viro static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
8051da177e4SLinus Torvalds {
8061da177e4SLinus Torvalds 	int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
8071da177e4SLinus Torvalds 	if (!retval)
808d8c76e6fSDave Hansen 		inc_nlink(dir);
8091da177e4SLinus Torvalds 	return retval;
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
812ebfc3b49SAl Viro static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
8131da177e4SLinus Torvalds {
8141da177e4SLinus Torvalds 	return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
8151da177e4SLinus Torvalds }
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds static int hugetlbfs_symlink(struct inode *dir,
8181da177e4SLinus Torvalds 			struct dentry *dentry, const char *symname)
8191da177e4SLinus Torvalds {
8201da177e4SLinus Torvalds 	struct inode *inode;
8211da177e4SLinus Torvalds 	int error = -ENOSPC;
8221da177e4SLinus Torvalds 
8237d54fa64SAl Viro 	inode = hugetlbfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
8241da177e4SLinus Torvalds 	if (inode) {
8251da177e4SLinus Torvalds 		int l = strlen(symname)+1;
8261da177e4SLinus Torvalds 		error = page_symlink(inode, symname, l);
8271da177e4SLinus Torvalds 		if (!error) {
8281da177e4SLinus Torvalds 			d_instantiate(dentry, inode);
8291da177e4SLinus Torvalds 			dget(dentry);
8301da177e4SLinus Torvalds 		} else
8311da177e4SLinus Torvalds 			iput(inode);
8321da177e4SLinus Torvalds 	}
833078cd827SDeepa Dinamani 	dir->i_ctime = dir->i_mtime = current_time(dir);
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	return error;
8361da177e4SLinus Torvalds }
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds /*
8396649a386SKen Chen  * mark the head page dirty
8401da177e4SLinus Torvalds  */
8411da177e4SLinus Torvalds static int hugetlbfs_set_page_dirty(struct page *page)
8421da177e4SLinus Torvalds {
843d85f3385SChristoph Lameter 	struct page *head = compound_head(page);
8446649a386SKen Chen 
8456649a386SKen Chen 	SetPageDirty(head);
8461da177e4SLinus Torvalds 	return 0;
8471da177e4SLinus Torvalds }
8481da177e4SLinus Torvalds 
849290408d4SNaoya Horiguchi static int hugetlbfs_migrate_page(struct address_space *mapping,
850b969c4abSMel Gorman 				struct page *newpage, struct page *page,
851a6bc32b8SMel Gorman 				enum migrate_mode mode)
852290408d4SNaoya Horiguchi {
853290408d4SNaoya Horiguchi 	int rc;
854290408d4SNaoya Horiguchi 
855290408d4SNaoya Horiguchi 	rc = migrate_huge_page_move_mapping(mapping, newpage, page);
85678bd5209SRafael Aquini 	if (rc != MIGRATEPAGE_SUCCESS)
857290408d4SNaoya Horiguchi 		return rc;
8582916ecc0SJérôme Glisse 	if (mode != MIGRATE_SYNC_NO_COPY)
859290408d4SNaoya Horiguchi 		migrate_page_copy(newpage, page);
8602916ecc0SJérôme Glisse 	else
8612916ecc0SJérôme Glisse 		migrate_page_states(newpage, page);
862290408d4SNaoya Horiguchi 
86378bd5209SRafael Aquini 	return MIGRATEPAGE_SUCCESS;
864290408d4SNaoya Horiguchi }
865290408d4SNaoya Horiguchi 
86678bb9203SNaoya Horiguchi static int hugetlbfs_error_remove_page(struct address_space *mapping,
86778bb9203SNaoya Horiguchi 				struct page *page)
86878bb9203SNaoya Horiguchi {
86978bb9203SNaoya Horiguchi 	struct inode *inode = mapping->host;
870ab615a5bSMike Kravetz 	pgoff_t index = page->index;
87178bb9203SNaoya Horiguchi 
87278bb9203SNaoya Horiguchi 	remove_huge_page(page);
873ab615a5bSMike Kravetz 	if (unlikely(hugetlb_unreserve_pages(inode, index, index + 1, 1)))
87478bb9203SNaoya Horiguchi 		hugetlb_fix_reserve_counts(inode);
875ab615a5bSMike Kravetz 
87678bb9203SNaoya Horiguchi 	return 0;
87778bb9203SNaoya Horiguchi }
87878bb9203SNaoya Horiguchi 
8794a25220dSDavid Howells /*
8804a25220dSDavid Howells  * Display the mount options in /proc/mounts.
8814a25220dSDavid Howells  */
8824a25220dSDavid Howells static int hugetlbfs_show_options(struct seq_file *m, struct dentry *root)
8834a25220dSDavid Howells {
8844a25220dSDavid Howells 	struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(root->d_sb);
8854a25220dSDavid Howells 	struct hugepage_subpool *spool = sbinfo->spool;
8864a25220dSDavid Howells 	unsigned long hpage_size = huge_page_size(sbinfo->hstate);
8874a25220dSDavid Howells 	unsigned hpage_shift = huge_page_shift(sbinfo->hstate);
8884a25220dSDavid Howells 	char mod;
8894a25220dSDavid Howells 
8904a25220dSDavid Howells 	if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
8914a25220dSDavid Howells 		seq_printf(m, ",uid=%u",
8924a25220dSDavid Howells 			   from_kuid_munged(&init_user_ns, sbinfo->uid));
8934a25220dSDavid Howells 	if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
8944a25220dSDavid Howells 		seq_printf(m, ",gid=%u",
8954a25220dSDavid Howells 			   from_kgid_munged(&init_user_ns, sbinfo->gid));
8964a25220dSDavid Howells 	if (sbinfo->mode != 0755)
8974a25220dSDavid Howells 		seq_printf(m, ",mode=%o", sbinfo->mode);
8984a25220dSDavid Howells 	if (sbinfo->max_inodes != -1)
8994a25220dSDavid Howells 		seq_printf(m, ",nr_inodes=%lu", sbinfo->max_inodes);
9004a25220dSDavid Howells 
9014a25220dSDavid Howells 	hpage_size /= 1024;
9024a25220dSDavid Howells 	mod = 'K';
9034a25220dSDavid Howells 	if (hpage_size >= 1024) {
9044a25220dSDavid Howells 		hpage_size /= 1024;
9054a25220dSDavid Howells 		mod = 'M';
9064a25220dSDavid Howells 	}
9074a25220dSDavid Howells 	seq_printf(m, ",pagesize=%lu%c", hpage_size, mod);
9084a25220dSDavid Howells 	if (spool) {
9094a25220dSDavid Howells 		if (spool->max_hpages != -1)
9104a25220dSDavid Howells 			seq_printf(m, ",size=%llu",
9114a25220dSDavid Howells 				   (unsigned long long)spool->max_hpages << hpage_shift);
9124a25220dSDavid Howells 		if (spool->min_hpages != -1)
9134a25220dSDavid Howells 			seq_printf(m, ",min_size=%llu",
9144a25220dSDavid Howells 				   (unsigned long long)spool->min_hpages << hpage_shift);
9154a25220dSDavid Howells 	}
9164a25220dSDavid Howells 	return 0;
9174a25220dSDavid Howells }
9184a25220dSDavid Howells 
919726c3342SDavid Howells static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
9201da177e4SLinus Torvalds {
921726c3342SDavid Howells 	struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
9222b0143b5SDavid Howells 	struct hstate *h = hstate_inode(d_inode(dentry));
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 	buf->f_type = HUGETLBFS_MAGIC;
925a5516438SAndi Kleen 	buf->f_bsize = huge_page_size(h);
9261da177e4SLinus Torvalds 	if (sbinfo) {
9271da177e4SLinus Torvalds 		spin_lock(&sbinfo->stat_lock);
92874a8a65cSDavid Gibson 		/* If no limits set, just report 0 for max/free/used
92974a8a65cSDavid Gibson 		 * blocks, like simple_statfs() */
93090481622SDavid Gibson 		if (sbinfo->spool) {
93190481622SDavid Gibson 			long free_pages;
93290481622SDavid Gibson 
93390481622SDavid Gibson 			spin_lock(&sbinfo->spool->lock);
93490481622SDavid Gibson 			buf->f_blocks = sbinfo->spool->max_hpages;
93590481622SDavid Gibson 			free_pages = sbinfo->spool->max_hpages
93690481622SDavid Gibson 				- sbinfo->spool->used_hpages;
93790481622SDavid Gibson 			buf->f_bavail = buf->f_bfree = free_pages;
93890481622SDavid Gibson 			spin_unlock(&sbinfo->spool->lock);
9391da177e4SLinus Torvalds 			buf->f_files = sbinfo->max_inodes;
9401da177e4SLinus Torvalds 			buf->f_ffree = sbinfo->free_inodes;
94174a8a65cSDavid Gibson 		}
9421da177e4SLinus Torvalds 		spin_unlock(&sbinfo->stat_lock);
9431da177e4SLinus Torvalds 	}
9441da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
9451da177e4SLinus Torvalds 	return 0;
9461da177e4SLinus Torvalds }
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds static void hugetlbfs_put_super(struct super_block *sb)
9491da177e4SLinus Torvalds {
9501da177e4SLinus Torvalds 	struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
9511da177e4SLinus Torvalds 
9521da177e4SLinus Torvalds 	if (sbi) {
9531da177e4SLinus Torvalds 		sb->s_fs_info = NULL;
95490481622SDavid Gibson 
95590481622SDavid Gibson 		if (sbi->spool)
95690481622SDavid Gibson 			hugepage_put_subpool(sbi->spool);
95790481622SDavid Gibson 
9581da177e4SLinus Torvalds 		kfree(sbi);
9591da177e4SLinus Torvalds 	}
9601da177e4SLinus Torvalds }
9611da177e4SLinus Torvalds 
96296527980SChristoph Hellwig static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
96396527980SChristoph Hellwig {
96496527980SChristoph Hellwig 	if (sbinfo->free_inodes >= 0) {
96596527980SChristoph Hellwig 		spin_lock(&sbinfo->stat_lock);
96696527980SChristoph Hellwig 		if (unlikely(!sbinfo->free_inodes)) {
96796527980SChristoph Hellwig 			spin_unlock(&sbinfo->stat_lock);
96896527980SChristoph Hellwig 			return 0;
96996527980SChristoph Hellwig 		}
97096527980SChristoph Hellwig 		sbinfo->free_inodes--;
97196527980SChristoph Hellwig 		spin_unlock(&sbinfo->stat_lock);
97296527980SChristoph Hellwig 	}
97396527980SChristoph Hellwig 
97496527980SChristoph Hellwig 	return 1;
97596527980SChristoph Hellwig }
97696527980SChristoph Hellwig 
97796527980SChristoph Hellwig static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
97896527980SChristoph Hellwig {
97996527980SChristoph Hellwig 	if (sbinfo->free_inodes >= 0) {
98096527980SChristoph Hellwig 		spin_lock(&sbinfo->stat_lock);
98196527980SChristoph Hellwig 		sbinfo->free_inodes++;
98296527980SChristoph Hellwig 		spin_unlock(&sbinfo->stat_lock);
98396527980SChristoph Hellwig 	}
98496527980SChristoph Hellwig }
98596527980SChristoph Hellwig 
98696527980SChristoph Hellwig 
987e18b890bSChristoph Lameter static struct kmem_cache *hugetlbfs_inode_cachep;
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
9901da177e4SLinus Torvalds {
99196527980SChristoph Hellwig 	struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
9921da177e4SLinus Torvalds 	struct hugetlbfs_inode_info *p;
9931da177e4SLinus Torvalds 
99496527980SChristoph Hellwig 	if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
9951da177e4SLinus Torvalds 		return NULL;
996e94b1766SChristoph Lameter 	p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL);
99796527980SChristoph Hellwig 	if (unlikely(!p)) {
99896527980SChristoph Hellwig 		hugetlbfs_inc_free_inodes(sbinfo);
99996527980SChristoph Hellwig 		return NULL;
10001da177e4SLinus Torvalds 	}
10014742a35dSMike Kravetz 
10024742a35dSMike Kravetz 	/*
10034742a35dSMike Kravetz 	 * Any time after allocation, hugetlbfs_destroy_inode can be called
10044742a35dSMike Kravetz 	 * for the inode.  mpol_free_shared_policy is unconditionally called
10054742a35dSMike Kravetz 	 * as part of hugetlbfs_destroy_inode.  So, initialize policy here
10064742a35dSMike Kravetz 	 * in case of a quick call to destroy.
10074742a35dSMike Kravetz 	 *
10084742a35dSMike Kravetz 	 * Note that the policy is initialized even if we are creating a
10094742a35dSMike Kravetz 	 * private inode.  This simplifies hugetlbfs_destroy_inode.
10104742a35dSMike Kravetz 	 */
10114742a35dSMike Kravetz 	mpol_shared_policy_init(&p->policy, NULL);
10124742a35dSMike Kravetz 
101396527980SChristoph Hellwig 	return &p->vfs_inode;
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds 
1016fa0d7e3dSNick Piggin static void hugetlbfs_i_callback(struct rcu_head *head)
1017fa0d7e3dSNick Piggin {
1018fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
1019fa0d7e3dSNick Piggin 	kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
1020fa0d7e3dSNick Piggin }
1021fa0d7e3dSNick Piggin 
10221da177e4SLinus Torvalds static void hugetlbfs_destroy_inode(struct inode *inode)
10231da177e4SLinus Torvalds {
102496527980SChristoph Hellwig 	hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
10251da177e4SLinus Torvalds 	mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
1026fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, hugetlbfs_i_callback);
10271da177e4SLinus Torvalds }
10281da177e4SLinus Torvalds 
1029f5e54d6eSChristoph Hellwig static const struct address_space_operations hugetlbfs_aops = {
1030800d15a5SNick Piggin 	.write_begin	= hugetlbfs_write_begin,
1031800d15a5SNick Piggin 	.write_end	= hugetlbfs_write_end,
10321da177e4SLinus Torvalds 	.set_page_dirty	= hugetlbfs_set_page_dirty,
1033290408d4SNaoya Horiguchi 	.migratepage    = hugetlbfs_migrate_page,
103478bb9203SNaoya Horiguchi 	.error_remove_page	= hugetlbfs_error_remove_page,
10351da177e4SLinus Torvalds };
10361da177e4SLinus Torvalds 
103796527980SChristoph Hellwig 
103851cc5068SAlexey Dobriyan static void init_once(void *foo)
103996527980SChristoph Hellwig {
104096527980SChristoph Hellwig 	struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
104196527980SChristoph Hellwig 
104296527980SChristoph Hellwig 	inode_init_once(&ei->vfs_inode);
104396527980SChristoph Hellwig }
104496527980SChristoph Hellwig 
10454b6f5d20SArjan van de Ven const struct file_operations hugetlbfs_file_operations = {
104634d0640eSAl Viro 	.read_iter		= hugetlbfs_read_iter,
10471da177e4SLinus Torvalds 	.mmap			= hugetlbfs_file_mmap,
10481b061d92SChristoph Hellwig 	.fsync			= noop_fsync,
10491da177e4SLinus Torvalds 	.get_unmapped_area	= hugetlb_get_unmapped_area,
10506038f373SArnd Bergmann 	.llseek			= default_llseek,
105170c3547eSMike Kravetz 	.fallocate		= hugetlbfs_fallocate,
10521da177e4SLinus Torvalds };
10531da177e4SLinus Torvalds 
105492e1d5beSArjan van de Ven static const struct inode_operations hugetlbfs_dir_inode_operations = {
10551da177e4SLinus Torvalds 	.create		= hugetlbfs_create,
10561da177e4SLinus Torvalds 	.lookup		= simple_lookup,
10571da177e4SLinus Torvalds 	.link		= simple_link,
10581da177e4SLinus Torvalds 	.unlink		= simple_unlink,
10591da177e4SLinus Torvalds 	.symlink	= hugetlbfs_symlink,
10601da177e4SLinus Torvalds 	.mkdir		= hugetlbfs_mkdir,
10611da177e4SLinus Torvalds 	.rmdir		= simple_rmdir,
10621da177e4SLinus Torvalds 	.mknod		= hugetlbfs_mknod,
10631da177e4SLinus Torvalds 	.rename		= simple_rename,
10641da177e4SLinus Torvalds 	.setattr	= hugetlbfs_setattr,
10651da177e4SLinus Torvalds };
10661da177e4SLinus Torvalds 
106792e1d5beSArjan van de Ven static const struct inode_operations hugetlbfs_inode_operations = {
10681da177e4SLinus Torvalds 	.setattr	= hugetlbfs_setattr,
10691da177e4SLinus Torvalds };
10701da177e4SLinus Torvalds 
1071ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations hugetlbfs_ops = {
10721da177e4SLinus Torvalds 	.alloc_inode    = hugetlbfs_alloc_inode,
10731da177e4SLinus Torvalds 	.destroy_inode  = hugetlbfs_destroy_inode,
10742bbbda30SAl Viro 	.evict_inode	= hugetlbfs_evict_inode,
10751da177e4SLinus Torvalds 	.statfs		= hugetlbfs_statfs,
10761da177e4SLinus Torvalds 	.put_super	= hugetlbfs_put_super,
10774a25220dSDavid Howells 	.show_options	= hugetlbfs_show_options,
10781da177e4SLinus Torvalds };
10791da177e4SLinus Torvalds 
10804a25220dSDavid Howells enum hugetlbfs_size_type { NO_SIZE, SIZE_STD, SIZE_PERCENT };
10817ca02d0aSMike Kravetz 
10827ca02d0aSMike Kravetz /*
10837ca02d0aSMike Kravetz  * Convert size option passed from command line to number of huge pages
10847ca02d0aSMike Kravetz  * in the pool specified by hstate.  Size option could be in bytes
10857ca02d0aSMike Kravetz  * (val_type == SIZE_STD) or percentage of the pool (val_type == SIZE_PERCENT).
10867ca02d0aSMike Kravetz  */
10874a25220dSDavid Howells static long
10887ca02d0aSMike Kravetz hugetlbfs_size_to_hpages(struct hstate *h, unsigned long long size_opt,
10894a25220dSDavid Howells 			 enum hugetlbfs_size_type val_type)
10907ca02d0aSMike Kravetz {
10917ca02d0aSMike Kravetz 	if (val_type == NO_SIZE)
10927ca02d0aSMike Kravetz 		return -1;
10937ca02d0aSMike Kravetz 
10947ca02d0aSMike Kravetz 	if (val_type == SIZE_PERCENT) {
10957ca02d0aSMike Kravetz 		size_opt <<= huge_page_shift(h);
10967ca02d0aSMike Kravetz 		size_opt *= h->max_huge_pages;
10977ca02d0aSMike Kravetz 		do_div(size_opt, 100);
10987ca02d0aSMike Kravetz 	}
10997ca02d0aSMike Kravetz 
11007ca02d0aSMike Kravetz 	size_opt >>= huge_page_shift(h);
11017ca02d0aSMike Kravetz 	return size_opt;
11027ca02d0aSMike Kravetz }
11037ca02d0aSMike Kravetz 
11041da177e4SLinus Torvalds static int
11051da177e4SLinus Torvalds hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
11061da177e4SLinus Torvalds {
1107e73a75faSRandy Dunlap 	char *p, *rest;
1108e73a75faSRandy Dunlap 	substring_t args[MAX_OPT_ARGS];
1109e73a75faSRandy Dunlap 	int option;
11107ca02d0aSMike Kravetz 	unsigned long long max_size_opt = 0, min_size_opt = 0;
11114a25220dSDavid Howells 	enum hugetlbfs_size_type max_val_type = NO_SIZE, min_val_type = NO_SIZE;
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	if (!options)
11141da177e4SLinus Torvalds 		return 0;
11151da177e4SLinus Torvalds 
1116e73a75faSRandy Dunlap 	while ((p = strsep(&options, ",")) != NULL) {
1117e73a75faSRandy Dunlap 		int token;
1118b4c07bceSLee Schermerhorn 		if (!*p)
1119b4c07bceSLee Schermerhorn 			continue;
11201da177e4SLinus Torvalds 
1121e73a75faSRandy Dunlap 		token = match_token(p, tokens, args);
1122e73a75faSRandy Dunlap 		switch (token) {
1123e73a75faSRandy Dunlap 		case Opt_uid:
1124e73a75faSRandy Dunlap 			if (match_int(&args[0], &option))
1125e73a75faSRandy Dunlap  				goto bad_val;
1126a0eb3a05SEric W. Biederman 			pconfig->uid = make_kuid(current_user_ns(), option);
1127a0eb3a05SEric W. Biederman 			if (!uid_valid(pconfig->uid))
1128a0eb3a05SEric W. Biederman 				goto bad_val;
1129e73a75faSRandy Dunlap 			break;
1130e73a75faSRandy Dunlap 
1131e73a75faSRandy Dunlap 		case Opt_gid:
1132e73a75faSRandy Dunlap 			if (match_int(&args[0], &option))
1133e73a75faSRandy Dunlap  				goto bad_val;
1134a0eb3a05SEric W. Biederman 			pconfig->gid = make_kgid(current_user_ns(), option);
1135a0eb3a05SEric W. Biederman 			if (!gid_valid(pconfig->gid))
1136a0eb3a05SEric W. Biederman 				goto bad_val;
1137e73a75faSRandy Dunlap 			break;
1138e73a75faSRandy Dunlap 
1139e73a75faSRandy Dunlap 		case Opt_mode:
1140e73a75faSRandy Dunlap 			if (match_octal(&args[0], &option))
1141e73a75faSRandy Dunlap  				goto bad_val;
114275897d60SKen Chen 			pconfig->mode = option & 01777U;
1143e73a75faSRandy Dunlap 			break;
1144e73a75faSRandy Dunlap 
1145e73a75faSRandy Dunlap 		case Opt_size: {
1146e73a75faSRandy Dunlap 			/* memparse() will accept a K/M/G without a digit */
1147e73a75faSRandy Dunlap 			if (!isdigit(*args[0].from))
1148e73a75faSRandy Dunlap 				goto bad_val;
11497ca02d0aSMike Kravetz 			max_size_opt = memparse(args[0].from, &rest);
11507ca02d0aSMike Kravetz 			max_val_type = SIZE_STD;
1151a137e1ccSAndi Kleen 			if (*rest == '%')
11527ca02d0aSMike Kravetz 				max_val_type = SIZE_PERCENT;
1153e73a75faSRandy Dunlap 			break;
1154e73a75faSRandy Dunlap 		}
11551da177e4SLinus Torvalds 
1156e73a75faSRandy Dunlap 		case Opt_nr_inodes:
1157e73a75faSRandy Dunlap 			/* memparse() will accept a K/M/G without a digit */
1158e73a75faSRandy Dunlap 			if (!isdigit(*args[0].from))
1159e73a75faSRandy Dunlap 				goto bad_val;
1160e73a75faSRandy Dunlap 			pconfig->nr_inodes = memparse(args[0].from, &rest);
1161e73a75faSRandy Dunlap 			break;
1162e73a75faSRandy Dunlap 
1163a137e1ccSAndi Kleen 		case Opt_pagesize: {
1164a137e1ccSAndi Kleen 			unsigned long ps;
1165a137e1ccSAndi Kleen 			ps = memparse(args[0].from, &rest);
1166a137e1ccSAndi Kleen 			pconfig->hstate = size_to_hstate(ps);
1167a137e1ccSAndi Kleen 			if (!pconfig->hstate) {
11689b857d26SAndrew Morton 				pr_err("Unsupported page size %lu MB\n",
1169a137e1ccSAndi Kleen 					ps >> 20);
1170a137e1ccSAndi Kleen 				return -EINVAL;
1171a137e1ccSAndi Kleen 			}
1172a137e1ccSAndi Kleen 			break;
1173a137e1ccSAndi Kleen 		}
1174a137e1ccSAndi Kleen 
11757ca02d0aSMike Kravetz 		case Opt_min_size: {
11767ca02d0aSMike Kravetz 			/* memparse() will accept a K/M/G without a digit */
11777ca02d0aSMike Kravetz 			if (!isdigit(*args[0].from))
11787ca02d0aSMike Kravetz 				goto bad_val;
11797ca02d0aSMike Kravetz 			min_size_opt = memparse(args[0].from, &rest);
11807ca02d0aSMike Kravetz 			min_val_type = SIZE_STD;
11817ca02d0aSMike Kravetz 			if (*rest == '%')
11827ca02d0aSMike Kravetz 				min_val_type = SIZE_PERCENT;
11837ca02d0aSMike Kravetz 			break;
11847ca02d0aSMike Kravetz 		}
11857ca02d0aSMike Kravetz 
1186e73a75faSRandy Dunlap 		default:
11879b857d26SAndrew Morton 			pr_err("Bad mount option: \"%s\"\n", p);
1188b4c07bceSLee Schermerhorn 			return -EINVAL;
1189e73a75faSRandy Dunlap 			break;
1190e73a75faSRandy Dunlap 		}
11911da177e4SLinus Torvalds 	}
1192a137e1ccSAndi Kleen 
11937ca02d0aSMike Kravetz 	/*
11947ca02d0aSMike Kravetz 	 * Use huge page pool size (in hstate) to convert the size
11957ca02d0aSMike Kravetz 	 * options to number of huge pages.  If NO_SIZE, -1 is returned.
11967ca02d0aSMike Kravetz 	 */
11977ca02d0aSMike Kravetz 	pconfig->max_hpages = hugetlbfs_size_to_hpages(pconfig->hstate,
11987ca02d0aSMike Kravetz 						max_size_opt, max_val_type);
11997ca02d0aSMike Kravetz 	pconfig->min_hpages = hugetlbfs_size_to_hpages(pconfig->hstate,
12007ca02d0aSMike Kravetz 						min_size_opt, min_val_type);
12017ca02d0aSMike Kravetz 
12027ca02d0aSMike Kravetz 	/*
12037ca02d0aSMike Kravetz 	 * If max_size was specified, then min_size must be smaller
12047ca02d0aSMike Kravetz 	 */
12057ca02d0aSMike Kravetz 	if (max_val_type > NO_SIZE &&
12067ca02d0aSMike Kravetz 	    pconfig->min_hpages > pconfig->max_hpages) {
12077ca02d0aSMike Kravetz 		pr_err("minimum size can not be greater than maximum size\n");
12087ca02d0aSMike Kravetz 		return -EINVAL;
1209a137e1ccSAndi Kleen 	}
1210a137e1ccSAndi Kleen 
12111da177e4SLinus Torvalds 	return 0;
1212e73a75faSRandy Dunlap 
1213e73a75faSRandy Dunlap bad_val:
12149b857d26SAndrew Morton 	pr_err("Bad value '%s' for mount option '%s'\n", args[0].from, p);
1215c12ddba0SAkinobu Mita  	return -EINVAL;
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds static int
12191da177e4SLinus Torvalds hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
12201da177e4SLinus Torvalds {
12211da177e4SLinus Torvalds 	int ret;
12221da177e4SLinus Torvalds 	struct hugetlbfs_config config;
12231da177e4SLinus Torvalds 	struct hugetlbfs_sb_info *sbinfo;
12241da177e4SLinus Torvalds 
12257ca02d0aSMike Kravetz 	config.max_hpages = -1; /* No limit on size by default */
12261da177e4SLinus Torvalds 	config.nr_inodes = -1; /* No limit on number of inodes by default */
122777c70de1SDavid Howells 	config.uid = current_fsuid();
122877c70de1SDavid Howells 	config.gid = current_fsgid();
12291da177e4SLinus Torvalds 	config.mode = 0755;
1230a137e1ccSAndi Kleen 	config.hstate = &default_hstate;
12317ca02d0aSMike Kravetz 	config.min_hpages = -1; /* No default minimum size */
12321da177e4SLinus Torvalds 	ret = hugetlbfs_parse_options(data, &config);
12331da177e4SLinus Torvalds 	if (ret)
12341da177e4SLinus Torvalds 		return ret;
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
12371da177e4SLinus Torvalds 	if (!sbinfo)
12381da177e4SLinus Torvalds 		return -ENOMEM;
12391da177e4SLinus Torvalds 	sb->s_fs_info = sbinfo;
1240a137e1ccSAndi Kleen 	sbinfo->hstate = config.hstate;
12411da177e4SLinus Torvalds 	spin_lock_init(&sbinfo->stat_lock);
12421da177e4SLinus Torvalds 	sbinfo->max_inodes = config.nr_inodes;
12431da177e4SLinus Torvalds 	sbinfo->free_inodes = config.nr_inodes;
124490481622SDavid Gibson 	sbinfo->spool = NULL;
12454a25220dSDavid Howells 	sbinfo->uid = config.uid;
12464a25220dSDavid Howells 	sbinfo->gid = config.gid;
12474a25220dSDavid Howells 	sbinfo->mode = config.mode;
12484a25220dSDavid Howells 
12497ca02d0aSMike Kravetz 	/*
12507ca02d0aSMike Kravetz 	 * Allocate and initialize subpool if maximum or minimum size is
12517ca02d0aSMike Kravetz 	 * specified.  Any needed reservations (for minimim size) are taken
12527ca02d0aSMike Kravetz 	 * taken when the subpool is created.
12537ca02d0aSMike Kravetz 	 */
12547ca02d0aSMike Kravetz 	if (config.max_hpages != -1 || config.min_hpages != -1) {
12557ca02d0aSMike Kravetz 		sbinfo->spool = hugepage_new_subpool(config.hstate,
12567ca02d0aSMike Kravetz 							config.max_hpages,
12577ca02d0aSMike Kravetz 							config.min_hpages);
125890481622SDavid Gibson 		if (!sbinfo->spool)
125990481622SDavid Gibson 			goto out_free;
126090481622SDavid Gibson 	}
12611da177e4SLinus Torvalds 	sb->s_maxbytes = MAX_LFS_FILESIZE;
1262a137e1ccSAndi Kleen 	sb->s_blocksize = huge_page_size(config.hstate);
1263a137e1ccSAndi Kleen 	sb->s_blocksize_bits = huge_page_shift(config.hstate);
12641da177e4SLinus Torvalds 	sb->s_magic = HUGETLBFS_MAGIC;
12651da177e4SLinus Torvalds 	sb->s_op = &hugetlbfs_ops;
12661da177e4SLinus Torvalds 	sb->s_time_gran = 1;
126748fde701SAl Viro 	sb->s_root = d_make_root(hugetlbfs_get_root(sb, &config));
126848fde701SAl Viro 	if (!sb->s_root)
12691da177e4SLinus Torvalds 		goto out_free;
12701da177e4SLinus Torvalds 	return 0;
12711da177e4SLinus Torvalds out_free:
127290481622SDavid Gibson 	kfree(sbinfo->spool);
12731da177e4SLinus Torvalds 	kfree(sbinfo);
12741da177e4SLinus Torvalds 	return -ENOMEM;
12751da177e4SLinus Torvalds }
12761da177e4SLinus Torvalds 
12773c26ff6eSAl Viro static struct dentry *hugetlbfs_mount(struct file_system_type *fs_type,
12783c26ff6eSAl Viro 	int flags, const char *dev_name, void *data)
12791da177e4SLinus Torvalds {
12803c26ff6eSAl Viro 	return mount_nodev(fs_type, flags, data, hugetlbfs_fill_super);
12811da177e4SLinus Torvalds }
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds static struct file_system_type hugetlbfs_fs_type = {
12841da177e4SLinus Torvalds 	.name		= "hugetlbfs",
12853c26ff6eSAl Viro 	.mount		= hugetlbfs_mount,
12861da177e4SLinus Torvalds 	.kill_sb	= kill_litter_super,
12871da177e4SLinus Torvalds };
12881da177e4SLinus Torvalds 
128942d7395fSAndi Kleen static struct vfsmount *hugetlbfs_vfsmount[HUGE_MAX_HSTATE];
12901da177e4SLinus Torvalds 
1291ef1ff6b8SFrom: Mel Gorman static int can_do_hugetlb_shm(void)
12921da177e4SLinus Torvalds {
1293a0eb3a05SEric W. Biederman 	kgid_t shm_group;
1294a0eb3a05SEric W. Biederman 	shm_group = make_kgid(&init_user_ns, sysctl_hugetlb_shm_group);
1295a0eb3a05SEric W. Biederman 	return capable(CAP_IPC_LOCK) || in_group_p(shm_group);
12961da177e4SLinus Torvalds }
12971da177e4SLinus Torvalds 
129842d7395fSAndi Kleen static int get_hstate_idx(int page_size_log)
129942d7395fSAndi Kleen {
1300af73e4d9SNaoya Horiguchi 	struct hstate *h = hstate_sizelog(page_size_log);
130142d7395fSAndi Kleen 
130242d7395fSAndi Kleen 	if (!h)
130342d7395fSAndi Kleen 		return -1;
130442d7395fSAndi Kleen 	return h - hstates;
130542d7395fSAndi Kleen }
130642d7395fSAndi Kleen 
1307be1d2cf5SFabian Frederick static const struct dentry_operations anon_ops = {
1308118b2302SAl Viro 	.d_dname = simple_dname
13090df4d6e5SAl Viro };
13100df4d6e5SAl Viro 
1311af73e4d9SNaoya Horiguchi /*
1312af73e4d9SNaoya Horiguchi  * Note that size should be aligned to proper hugepage size in caller side,
1313af73e4d9SNaoya Horiguchi  * otherwise hugetlb_reserve_pages reserves one less hugepages than intended.
1314af73e4d9SNaoya Horiguchi  */
1315af73e4d9SNaoya Horiguchi struct file *hugetlb_file_setup(const char *name, size_t size,
1316af73e4d9SNaoya Horiguchi 				vm_flags_t acctflag, struct user_struct **user,
131742d7395fSAndi Kleen 				int creat_flags, int page_size_log)
13181da177e4SLinus Torvalds {
131939b65252SAnatol Pomozov 	struct file *file = ERR_PTR(-ENOMEM);
13201da177e4SLinus Torvalds 	struct inode *inode;
13212c48b9c4SAl Viro 	struct path path;
13220df4d6e5SAl Viro 	struct super_block *sb;
13231da177e4SLinus Torvalds 	struct qstr quick_string;
132442d7395fSAndi Kleen 	int hstate_idx;
132542d7395fSAndi Kleen 
132642d7395fSAndi Kleen 	hstate_idx = get_hstate_idx(page_size_log);
132742d7395fSAndi Kleen 	if (hstate_idx < 0)
132842d7395fSAndi Kleen 		return ERR_PTR(-ENODEV);
13291da177e4SLinus Torvalds 
1330353d5c30SHugh Dickins 	*user = NULL;
133142d7395fSAndi Kleen 	if (!hugetlbfs_vfsmount[hstate_idx])
13325bc98594SAkinobu Mita 		return ERR_PTR(-ENOENT);
13335bc98594SAkinobu Mita 
1334ef1ff6b8SFrom: Mel Gorman 	if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
1335353d5c30SHugh Dickins 		*user = current_user();
1336353d5c30SHugh Dickins 		if (user_shm_lock(size, *user)) {
133721a3c273SDavid Rientjes 			task_lock(current);
13389b857d26SAndrew Morton 			pr_warn_once("%s (%d): Using mlock ulimits for SHM_HUGETLB is deprecated\n",
133921a3c273SDavid Rientjes 				current->comm, current->pid);
134021a3c273SDavid Rientjes 			task_unlock(current);
1341353d5c30SHugh Dickins 		} else {
1342353d5c30SHugh Dickins 			*user = NULL;
13431da177e4SLinus Torvalds 			return ERR_PTR(-EPERM);
13442584e517SRavikiran G Thirumalai 		}
1345353d5c30SHugh Dickins 	}
13461da177e4SLinus Torvalds 
13470df4d6e5SAl Viro 	sb = hugetlbfs_vfsmount[hstate_idx]->mnt_sb;
13489d66586fSEric W. Biederman 	quick_string.name = name;
13491da177e4SLinus Torvalds 	quick_string.len = strlen(quick_string.name);
13501da177e4SLinus Torvalds 	quick_string.hash = 0;
13510df4d6e5SAl Viro 	path.dentry = d_alloc_pseudo(sb, &quick_string);
13522c48b9c4SAl Viro 	if (!path.dentry)
13531da177e4SLinus Torvalds 		goto out_shm_unlock;
13541da177e4SLinus Torvalds 
13550df4d6e5SAl Viro 	d_set_d_op(path.dentry, &anon_ops);
135642d7395fSAndi Kleen 	path.mnt = mntget(hugetlbfs_vfsmount[hstate_idx]);
135739b65252SAnatol Pomozov 	file = ERR_PTR(-ENOSPC);
13580df4d6e5SAl Viro 	inode = hugetlbfs_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0);
13591da177e4SLinus Torvalds 	if (!inode)
1360ce8d2cdfSDave Hansen 		goto out_dentry;
1361e1832f29SStephen Smalley 	if (creat_flags == HUGETLB_SHMFS_INODE)
1362e1832f29SStephen Smalley 		inode->i_flags |= S_PRIVATE;
13631da177e4SLinus Torvalds 
136439b65252SAnatol Pomozov 	file = ERR_PTR(-ENOMEM);
1365af73e4d9SNaoya Horiguchi 	if (hugetlb_reserve_pages(inode, 0,
1366af73e4d9SNaoya Horiguchi 			size >> huge_page_shift(hstate_inode(inode)), NULL,
1367af73e4d9SNaoya Horiguchi 			acctflag))
1368b45b5bd6SDavid Gibson 		goto out_inode;
1369b45b5bd6SDavid Gibson 
13702c48b9c4SAl Viro 	d_instantiate(path.dentry, inode);
13711da177e4SLinus Torvalds 	inode->i_size = size;
13726d6b77f1SMiklos Szeredi 	clear_nlink(inode);
1373ce8d2cdfSDave Hansen 
13742c48b9c4SAl Viro 	file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
1375ce8d2cdfSDave Hansen 			&hugetlbfs_file_operations);
137639b65252SAnatol Pomozov 	if (IS_ERR(file))
1377b4d232e6SAl Viro 		goto out_dentry; /* inode is already attached */
1378ce8d2cdfSDave Hansen 
13791da177e4SLinus Torvalds 	return file;
13801da177e4SLinus Torvalds 
1381b45b5bd6SDavid Gibson out_inode:
1382b45b5bd6SDavid Gibson 	iput(inode);
13831da177e4SLinus Torvalds out_dentry:
13842c48b9c4SAl Viro 	path_put(&path);
13851da177e4SLinus Torvalds out_shm_unlock:
1386353d5c30SHugh Dickins 	if (*user) {
1387353d5c30SHugh Dickins 		user_shm_unlock(size, *user);
1388353d5c30SHugh Dickins 		*user = NULL;
1389353d5c30SHugh Dickins 	}
139039b65252SAnatol Pomozov 	return file;
13911da177e4SLinus Torvalds }
13921da177e4SLinus Torvalds 
13931da177e4SLinus Torvalds static int __init init_hugetlbfs_fs(void)
13941da177e4SLinus Torvalds {
139542d7395fSAndi Kleen 	struct hstate *h;
13961da177e4SLinus Torvalds 	int error;
139742d7395fSAndi Kleen 	int i;
13981da177e4SLinus Torvalds 
1399457c1b27SNishanth Aravamudan 	if (!hugepages_supported()) {
14009b857d26SAndrew Morton 		pr_info("disabling because there are no supported hugepage sizes\n");
1401457c1b27SNishanth Aravamudan 		return -ENOTSUPP;
1402457c1b27SNishanth Aravamudan 	}
1403457c1b27SNishanth Aravamudan 
1404d1d5e05fSHillf Danton 	error = -ENOMEM;
14051da177e4SLinus Torvalds 	hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
14061da177e4SLinus Torvalds 					sizeof(struct hugetlbfs_inode_info),
14075d097056SVladimir Davydov 					0, SLAB_ACCOUNT, init_once);
14081da177e4SLinus Torvalds 	if (hugetlbfs_inode_cachep == NULL)
1409e0bf68ddSPeter Zijlstra 		goto out2;
14101da177e4SLinus Torvalds 
14111da177e4SLinus Torvalds 	error = register_filesystem(&hugetlbfs_fs_type);
14121da177e4SLinus Torvalds 	if (error)
14131da177e4SLinus Torvalds 		goto out;
14141da177e4SLinus Torvalds 
141542d7395fSAndi Kleen 	i = 0;
141642d7395fSAndi Kleen 	for_each_hstate(h) {
141742d7395fSAndi Kleen 		char buf[50];
141842d7395fSAndi Kleen 		unsigned ps_kb = 1U << (h->order + PAGE_SHIFT - 10);
14191da177e4SLinus Torvalds 
142042d7395fSAndi Kleen 		snprintf(buf, sizeof(buf), "pagesize=%uK", ps_kb);
142142d7395fSAndi Kleen 		hugetlbfs_vfsmount[i] = kern_mount_data(&hugetlbfs_fs_type,
142242d7395fSAndi Kleen 							buf);
142342d7395fSAndi Kleen 
142442d7395fSAndi Kleen 		if (IS_ERR(hugetlbfs_vfsmount[i])) {
14259b857d26SAndrew Morton 			pr_err("Cannot mount internal hugetlbfs for "
142642d7395fSAndi Kleen 				"page size %uK", ps_kb);
142742d7395fSAndi Kleen 			error = PTR_ERR(hugetlbfs_vfsmount[i]);
142842d7395fSAndi Kleen 			hugetlbfs_vfsmount[i] = NULL;
14291da177e4SLinus Torvalds 		}
143042d7395fSAndi Kleen 		i++;
143142d7395fSAndi Kleen 	}
143242d7395fSAndi Kleen 	/* Non default hstates are optional */
143342d7395fSAndi Kleen 	if (!IS_ERR_OR_NULL(hugetlbfs_vfsmount[default_hstate_idx]))
143442d7395fSAndi Kleen 		return 0;
14351da177e4SLinus Torvalds 
14361da177e4SLinus Torvalds  out:
14371da177e4SLinus Torvalds 	kmem_cache_destroy(hugetlbfs_inode_cachep);
1438e0bf68ddSPeter Zijlstra  out2:
14391da177e4SLinus Torvalds 	return error;
14401da177e4SLinus Torvalds }
14413e89e1c5SPaul Gortmaker fs_initcall(init_hugetlbfs_fs)
1442