xref: /openbmc/linux/fs/hugetlbfs/inode.c (revision d7167b14)
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>
3032021982SDavid Howells #include <linux/fs_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 
4832021982SDavid Howells enum hugetlbfs_size_type { NO_SIZE, SIZE_STD, SIZE_PERCENT };
4932021982SDavid Howells 
5032021982SDavid Howells struct hugetlbfs_fs_context {
514a25220dSDavid Howells 	struct hstate		*hstate;
5232021982SDavid Howells 	unsigned long long	max_size_opt;
5332021982SDavid Howells 	unsigned long long	min_size_opt;
544a25220dSDavid Howells 	long			max_hpages;
554a25220dSDavid Howells 	long			nr_inodes;
564a25220dSDavid Howells 	long			min_hpages;
5732021982SDavid Howells 	enum hugetlbfs_size_type max_val_type;
5832021982SDavid Howells 	enum hugetlbfs_size_type min_val_type;
59a0eb3a05SEric W. Biederman 	kuid_t			uid;
60a0eb3a05SEric W. Biederman 	kgid_t			gid;
61a1d776eeSDavid Gibson 	umode_t			mode;
62a1d776eeSDavid Gibson };
63a1d776eeSDavid Gibson 
641da177e4SLinus Torvalds int sysctl_hugetlb_shm_group;
651da177e4SLinus Torvalds 
6632021982SDavid Howells enum hugetlb_param {
6732021982SDavid Howells 	Opt_gid,
6832021982SDavid Howells 	Opt_min_size,
6932021982SDavid Howells 	Opt_mode,
7032021982SDavid Howells 	Opt_nr_inodes,
7132021982SDavid Howells 	Opt_pagesize,
7232021982SDavid Howells 	Opt_size,
7332021982SDavid Howells 	Opt_uid,
74e73a75faSRandy Dunlap };
75e73a75faSRandy Dunlap 
76d7167b14SAl Viro static const struct fs_parameter_spec hugetlb_fs_parameters[] = {
7732021982SDavid Howells 	fsparam_u32   ("gid",		Opt_gid),
7832021982SDavid Howells 	fsparam_string("min_size",	Opt_min_size),
7932021982SDavid Howells 	fsparam_u32   ("mode",		Opt_mode),
8032021982SDavid Howells 	fsparam_string("nr_inodes",	Opt_nr_inodes),
8132021982SDavid Howells 	fsparam_string("pagesize",	Opt_pagesize),
8232021982SDavid Howells 	fsparam_string("size",		Opt_size),
8332021982SDavid Howells 	fsparam_u32   ("uid",		Opt_uid),
8432021982SDavid Howells 	{}
8532021982SDavid Howells };
8632021982SDavid Howells 
8770c3547eSMike Kravetz #ifdef CONFIG_NUMA
8870c3547eSMike Kravetz static inline void hugetlb_set_vma_policy(struct vm_area_struct *vma,
8970c3547eSMike Kravetz 					struct inode *inode, pgoff_t index)
9070c3547eSMike Kravetz {
9170c3547eSMike Kravetz 	vma->vm_policy = mpol_shared_policy_lookup(&HUGETLBFS_I(inode)->policy,
9270c3547eSMike Kravetz 							index);
9370c3547eSMike Kravetz }
9470c3547eSMike Kravetz 
9570c3547eSMike Kravetz static inline void hugetlb_drop_vma_policy(struct vm_area_struct *vma)
9670c3547eSMike Kravetz {
9770c3547eSMike Kravetz 	mpol_cond_put(vma->vm_policy);
9870c3547eSMike Kravetz }
9970c3547eSMike Kravetz #else
10070c3547eSMike Kravetz static inline void hugetlb_set_vma_policy(struct vm_area_struct *vma,
10170c3547eSMike Kravetz 					struct inode *inode, pgoff_t index)
10270c3547eSMike Kravetz {
10370c3547eSMike Kravetz }
10470c3547eSMike Kravetz 
10570c3547eSMike Kravetz static inline void hugetlb_drop_vma_policy(struct vm_area_struct *vma)
10670c3547eSMike Kravetz {
10770c3547eSMike Kravetz }
10870c3547eSMike Kravetz #endif
10970c3547eSMike Kravetz 
1102e9b367cSAdam Litke static void huge_pagevec_release(struct pagevec *pvec)
1112e9b367cSAdam Litke {
1122e9b367cSAdam Litke 	int i;
1132e9b367cSAdam Litke 
1142e9b367cSAdam Litke 	for (i = 0; i < pagevec_count(pvec); ++i)
1152e9b367cSAdam Litke 		put_page(pvec->pages[i]);
1162e9b367cSAdam Litke 
1172e9b367cSAdam Litke 	pagevec_reinit(pvec);
1182e9b367cSAdam Litke }
1192e9b367cSAdam Litke 
12063489f8eSMike Kravetz /*
12163489f8eSMike Kravetz  * Mask used when checking the page offset value passed in via system
12263489f8eSMike Kravetz  * calls.  This value will be converted to a loff_t which is signed.
12363489f8eSMike Kravetz  * Therefore, we want to check the upper PAGE_SHIFT + 1 bits of the
12463489f8eSMike Kravetz  * value.  The extra bit (- 1 in the shift value) is to take the sign
12563489f8eSMike Kravetz  * bit into account.
12663489f8eSMike Kravetz  */
12763489f8eSMike Kravetz #define PGOFF_LOFFT_MAX \
12863489f8eSMike Kravetz 	(((1UL << (PAGE_SHIFT + 1)) - 1) <<  (BITS_PER_LONG - (PAGE_SHIFT + 1)))
12963489f8eSMike Kravetz 
1301da177e4SLinus Torvalds static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
1311da177e4SLinus Torvalds {
132496ad9aaSAl Viro 	struct inode *inode = file_inode(file);
1331da177e4SLinus Torvalds 	loff_t len, vma_len;
1341da177e4SLinus Torvalds 	int ret;
135a5516438SAndi Kleen 	struct hstate *h = hstate_file(file);
1361da177e4SLinus Torvalds 
13768589bc3SHugh Dickins 	/*
138dec4ad86SDavid Gibson 	 * vma address alignment (but not the pgoff alignment) has
139dec4ad86SDavid Gibson 	 * already been checked by prepare_hugepage_range.  If you add
140dec4ad86SDavid Gibson 	 * any error returns here, do so after setting VM_HUGETLB, so
141dec4ad86SDavid Gibson 	 * is_vm_hugetlb_page tests below unmap_region go the right
142dec4ad86SDavid Gibson 	 * way when do_mmap_pgoff unwinds (may be important on powerpc
143dec4ad86SDavid Gibson 	 * and ia64).
14468589bc3SHugh Dickins 	 */
145a2fce914SNaoya Horiguchi 	vma->vm_flags |= VM_HUGETLB | VM_DONTEXPAND;
14668589bc3SHugh Dickins 	vma->vm_ops = &hugetlb_vm_ops;
1471da177e4SLinus Torvalds 
148045c7a3fSMike Kravetz 	/*
14963489f8eSMike Kravetz 	 * page based offset in vm_pgoff could be sufficiently large to
1505df63c2aSMike Kravetz 	 * overflow a loff_t when converted to byte offset.  This can
1515df63c2aSMike Kravetz 	 * only happen on architectures where sizeof(loff_t) ==
1525df63c2aSMike Kravetz 	 * sizeof(unsigned long).  So, only check in those instances.
153045c7a3fSMike Kravetz 	 */
1545df63c2aSMike Kravetz 	if (sizeof(unsigned long) == sizeof(loff_t)) {
15563489f8eSMike Kravetz 		if (vma->vm_pgoff & PGOFF_LOFFT_MAX)
156045c7a3fSMike Kravetz 			return -EINVAL;
1575df63c2aSMike Kravetz 	}
158045c7a3fSMike Kravetz 
15963489f8eSMike Kravetz 	/* must be huge page aligned */
1602b37c35eSBecky Bruce 	if (vma->vm_pgoff & (~huge_page_mask(h) >> PAGE_SHIFT))
161dec4ad86SDavid Gibson 		return -EINVAL;
162dec4ad86SDavid Gibson 
1631da177e4SLinus Torvalds 	vma_len = (loff_t)(vma->vm_end - vma->vm_start);
164045c7a3fSMike Kravetz 	len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
165045c7a3fSMike Kravetz 	/* check for overflow */
166045c7a3fSMike Kravetz 	if (len < vma_len)
167045c7a3fSMike Kravetz 		return -EINVAL;
1681da177e4SLinus Torvalds 
1695955102cSAl Viro 	inode_lock(inode);
1701da177e4SLinus Torvalds 	file_accessed(file);
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	ret = -ENOMEM;
173a1e78772SMel Gorman 	if (hugetlb_reserve_pages(inode,
174a5516438SAndi Kleen 				vma->vm_pgoff >> huge_page_order(h),
1755a6fe125SMel Gorman 				len >> huge_page_shift(h), vma,
1765a6fe125SMel Gorman 				vma->vm_flags))
177b45b5bd6SDavid Gibson 		goto out;
178b45b5bd6SDavid Gibson 
1794c887265SAdam Litke 	ret = 0;
180b6174df5SZhang, Yanmin 	if (vma->vm_flags & VM_WRITE && inode->i_size < len)
181045c7a3fSMike Kravetz 		i_size_write(inode, len);
1821da177e4SLinus Torvalds out:
1835955102cSAl Viro 	inode_unlock(inode);
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	return ret;
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds /*
189508034a3SHugh Dickins  * Called under down_write(mmap_sem).
1901da177e4SLinus Torvalds  */
1911da177e4SLinus Torvalds 
192d2ba27e8SAdrian Bunk #ifndef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
1931da177e4SLinus Torvalds static unsigned long
1941da177e4SLinus Torvalds hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
1951da177e4SLinus Torvalds 		unsigned long len, unsigned long pgoff, unsigned long flags)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	struct mm_struct *mm = current->mm;
1981da177e4SLinus Torvalds 	struct vm_area_struct *vma;
199a5516438SAndi Kleen 	struct hstate *h = hstate_file(file);
20008659355SMichel Lespinasse 	struct vm_unmapped_area_info info;
2011da177e4SLinus Torvalds 
202a5516438SAndi Kleen 	if (len & ~huge_page_mask(h))
2031da177e4SLinus Torvalds 		return -EINVAL;
2041da177e4SLinus Torvalds 	if (len > TASK_SIZE)
2051da177e4SLinus Torvalds 		return -ENOMEM;
2061da177e4SLinus Torvalds 
207036e0856SBenjamin Herrenschmidt 	if (flags & MAP_FIXED) {
208a5516438SAndi Kleen 		if (prepare_hugepage_range(file, addr, len))
209036e0856SBenjamin Herrenschmidt 			return -EINVAL;
210036e0856SBenjamin Herrenschmidt 		return addr;
211036e0856SBenjamin Herrenschmidt 	}
212036e0856SBenjamin Herrenschmidt 
2131da177e4SLinus Torvalds 	if (addr) {
214a5516438SAndi Kleen 		addr = ALIGN(addr, huge_page_size(h));
2151da177e4SLinus Torvalds 		vma = find_vma(mm, addr);
2161da177e4SLinus Torvalds 		if (TASK_SIZE - len >= addr &&
2171be7107fSHugh Dickins 		    (!vma || addr + len <= vm_start_gap(vma)))
2181da177e4SLinus Torvalds 			return addr;
2191da177e4SLinus Torvalds 	}
2201da177e4SLinus Torvalds 
22108659355SMichel Lespinasse 	info.flags = 0;
22208659355SMichel Lespinasse 	info.length = len;
22308659355SMichel Lespinasse 	info.low_limit = TASK_UNMAPPED_BASE;
22408659355SMichel Lespinasse 	info.high_limit = TASK_SIZE;
22508659355SMichel Lespinasse 	info.align_mask = PAGE_MASK & ~huge_page_mask(h);
22608659355SMichel Lespinasse 	info.align_offset = 0;
22708659355SMichel Lespinasse 	return vm_unmapped_area(&info);
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds #endif
2301da177e4SLinus Torvalds 
23134d0640eSAl Viro static size_t
232e63e1e5aSBadari Pulavarty hugetlbfs_read_actor(struct page *page, unsigned long offset,
23334d0640eSAl Viro 			struct iov_iter *to, unsigned long size)
234e63e1e5aSBadari Pulavarty {
23534d0640eSAl Viro 	size_t copied = 0;
236e63e1e5aSBadari Pulavarty 	int i, chunksize;
237e63e1e5aSBadari Pulavarty 
238e63e1e5aSBadari Pulavarty 	/* Find which 4k chunk and offset with in that chunk */
23909cbfeafSKirill A. Shutemov 	i = offset >> PAGE_SHIFT;
24009cbfeafSKirill A. Shutemov 	offset = offset & ~PAGE_MASK;
241e63e1e5aSBadari Pulavarty 
242e63e1e5aSBadari Pulavarty 	while (size) {
24334d0640eSAl Viro 		size_t n;
24409cbfeafSKirill A. Shutemov 		chunksize = PAGE_SIZE;
245e63e1e5aSBadari Pulavarty 		if (offset)
246e63e1e5aSBadari Pulavarty 			chunksize -= offset;
247e63e1e5aSBadari Pulavarty 		if (chunksize > size)
248e63e1e5aSBadari Pulavarty 			chunksize = size;
24934d0640eSAl Viro 		n = copy_page_to_iter(&page[i], offset, chunksize, to);
25034d0640eSAl Viro 		copied += n;
25134d0640eSAl Viro 		if (n != chunksize)
25234d0640eSAl Viro 			return copied;
253e63e1e5aSBadari Pulavarty 		offset = 0;
254e63e1e5aSBadari Pulavarty 		size -= chunksize;
255e63e1e5aSBadari Pulavarty 		i++;
256e63e1e5aSBadari Pulavarty 	}
25734d0640eSAl Viro 	return copied;
258e63e1e5aSBadari Pulavarty }
259e63e1e5aSBadari Pulavarty 
260e63e1e5aSBadari Pulavarty /*
261e63e1e5aSBadari Pulavarty  * Support for read() - Find the page attached to f_mapping and copy out the
262e63e1e5aSBadari Pulavarty  * data. Its *very* similar to do_generic_mapping_read(), we can't use that
263ea1754a0SKirill A. Shutemov  * since it has PAGE_SIZE assumptions.
264e63e1e5aSBadari Pulavarty  */
26534d0640eSAl Viro static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
266e63e1e5aSBadari Pulavarty {
26734d0640eSAl Viro 	struct file *file = iocb->ki_filp;
26834d0640eSAl Viro 	struct hstate *h = hstate_file(file);
26934d0640eSAl Viro 	struct address_space *mapping = file->f_mapping;
270e63e1e5aSBadari Pulavarty 	struct inode *inode = mapping->host;
27134d0640eSAl Viro 	unsigned long index = iocb->ki_pos >> huge_page_shift(h);
27234d0640eSAl Viro 	unsigned long offset = iocb->ki_pos & ~huge_page_mask(h);
273e63e1e5aSBadari Pulavarty 	unsigned long end_index;
274e63e1e5aSBadari Pulavarty 	loff_t isize;
275e63e1e5aSBadari Pulavarty 	ssize_t retval = 0;
276e63e1e5aSBadari Pulavarty 
27734d0640eSAl Viro 	while (iov_iter_count(to)) {
278e63e1e5aSBadari Pulavarty 		struct page *page;
27934d0640eSAl Viro 		size_t nr, copied;
280e63e1e5aSBadari Pulavarty 
281e63e1e5aSBadari Pulavarty 		/* nr is the maximum number of bytes to copy from this page */
282a5516438SAndi Kleen 		nr = huge_page_size(h);
283a05b0855SAneesh Kumar K.V 		isize = i_size_read(inode);
284a05b0855SAneesh Kumar K.V 		if (!isize)
28534d0640eSAl Viro 			break;
286a05b0855SAneesh Kumar K.V 		end_index = (isize - 1) >> huge_page_shift(h);
287e63e1e5aSBadari Pulavarty 		if (index > end_index)
28834d0640eSAl Viro 			break;
28934d0640eSAl Viro 		if (index == end_index) {
290a5516438SAndi Kleen 			nr = ((isize - 1) & ~huge_page_mask(h)) + 1;
291a05b0855SAneesh Kumar K.V 			if (nr <= offset)
29234d0640eSAl Viro 				break;
293e63e1e5aSBadari Pulavarty 		}
294e63e1e5aSBadari Pulavarty 		nr = nr - offset;
295e63e1e5aSBadari Pulavarty 
296e63e1e5aSBadari Pulavarty 		/* Find the page */
297a05b0855SAneesh Kumar K.V 		page = find_lock_page(mapping, index);
298e63e1e5aSBadari Pulavarty 		if (unlikely(page == NULL)) {
299e63e1e5aSBadari Pulavarty 			/*
300e63e1e5aSBadari Pulavarty 			 * We have a HOLE, zero out the user-buffer for the
301e63e1e5aSBadari Pulavarty 			 * length of the hole or request.
302e63e1e5aSBadari Pulavarty 			 */
30334d0640eSAl Viro 			copied = iov_iter_zero(nr, to);
304e63e1e5aSBadari Pulavarty 		} else {
305a05b0855SAneesh Kumar K.V 			unlock_page(page);
306a05b0855SAneesh Kumar K.V 
307e63e1e5aSBadari Pulavarty 			/*
308e63e1e5aSBadari Pulavarty 			 * We have the page, copy it to user space buffer.
309e63e1e5aSBadari Pulavarty 			 */
31034d0640eSAl Viro 			copied = hugetlbfs_read_actor(page, offset, to, nr);
31109cbfeafSKirill A. Shutemov 			put_page(page);
312e63e1e5aSBadari Pulavarty 		}
31334d0640eSAl Viro 		offset += copied;
31434d0640eSAl Viro 		retval += copied;
31534d0640eSAl Viro 		if (copied != nr && iov_iter_count(to)) {
31634d0640eSAl Viro 			if (!retval)
31734d0640eSAl Viro 				retval = -EFAULT;
318e63e1e5aSBadari Pulavarty 			break;
319e63e1e5aSBadari Pulavarty 		}
32034d0640eSAl Viro 		index += offset >> huge_page_shift(h);
32134d0640eSAl Viro 		offset &= ~huge_page_mask(h);
32234d0640eSAl Viro 	}
32334d0640eSAl Viro 	iocb->ki_pos = ((loff_t)index << huge_page_shift(h)) + offset;
324e63e1e5aSBadari Pulavarty 	return retval;
325e63e1e5aSBadari Pulavarty }
326e63e1e5aSBadari Pulavarty 
327800d15a5SNick Piggin static int hugetlbfs_write_begin(struct file *file,
328800d15a5SNick Piggin 			struct address_space *mapping,
329800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
330800d15a5SNick Piggin 			struct page **pagep, void **fsdata)
3311da177e4SLinus Torvalds {
3321da177e4SLinus Torvalds 	return -EINVAL;
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
335800d15a5SNick Piggin static int hugetlbfs_write_end(struct file *file, struct address_space *mapping,
336800d15a5SNick Piggin 			loff_t pos, unsigned len, unsigned copied,
337800d15a5SNick Piggin 			struct page *page, void *fsdata)
3381da177e4SLinus Torvalds {
339800d15a5SNick Piggin 	BUG();
3401da177e4SLinus Torvalds 	return -EINVAL;
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
343b5cec28dSMike Kravetz static void remove_huge_page(struct page *page)
3441da177e4SLinus Torvalds {
345b9ea2515SKonstantin Khlebnikov 	ClearPageDirty(page);
3461da177e4SLinus Torvalds 	ClearPageUptodate(page);
347bd65cb86SMinchan Kim 	delete_from_page_cache(page);
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
3504aae8d1cSMike Kravetz static void
351f808c13fSDavidlohr Bueso hugetlb_vmdelete_list(struct rb_root_cached *root, pgoff_t start, pgoff_t end)
3524aae8d1cSMike Kravetz {
3534aae8d1cSMike Kravetz 	struct vm_area_struct *vma;
3544aae8d1cSMike Kravetz 
3554aae8d1cSMike Kravetz 	/*
3564aae8d1cSMike Kravetz 	 * end == 0 indicates that the entire range after
3574aae8d1cSMike Kravetz 	 * start should be unmapped.
3584aae8d1cSMike Kravetz 	 */
3594aae8d1cSMike Kravetz 	vma_interval_tree_foreach(vma, root, start, end ? end : ULONG_MAX) {
3604aae8d1cSMike Kravetz 		unsigned long v_offset;
3614aae8d1cSMike Kravetz 		unsigned long v_end;
3624aae8d1cSMike Kravetz 
3634aae8d1cSMike Kravetz 		/*
3644aae8d1cSMike Kravetz 		 * Can the expression below overflow on 32-bit arches?
3654aae8d1cSMike Kravetz 		 * No, because the interval tree returns us only those vmas
3664aae8d1cSMike Kravetz 		 * which overlap the truncated area starting at pgoff,
3674aae8d1cSMike Kravetz 		 * and no vma on a 32-bit arch can span beyond the 4GB.
3684aae8d1cSMike Kravetz 		 */
3694aae8d1cSMike Kravetz 		if (vma->vm_pgoff < start)
3704aae8d1cSMike Kravetz 			v_offset = (start - vma->vm_pgoff) << PAGE_SHIFT;
3714aae8d1cSMike Kravetz 		else
3724aae8d1cSMike Kravetz 			v_offset = 0;
3734aae8d1cSMike Kravetz 
3744aae8d1cSMike Kravetz 		if (!end)
3754aae8d1cSMike Kravetz 			v_end = vma->vm_end;
3764aae8d1cSMike Kravetz 		else {
3774aae8d1cSMike Kravetz 			v_end = ((end - vma->vm_pgoff) << PAGE_SHIFT)
3784aae8d1cSMike Kravetz 							+ vma->vm_start;
3794aae8d1cSMike Kravetz 			if (v_end > vma->vm_end)
3804aae8d1cSMike Kravetz 				v_end = vma->vm_end;
3814aae8d1cSMike Kravetz 		}
3824aae8d1cSMike Kravetz 
3834aae8d1cSMike Kravetz 		unmap_hugepage_range(vma, vma->vm_start + v_offset, v_end,
3844aae8d1cSMike Kravetz 									NULL);
3854aae8d1cSMike Kravetz 	}
3864aae8d1cSMike Kravetz }
387b5cec28dSMike Kravetz 
388b5cec28dSMike Kravetz /*
389b5cec28dSMike Kravetz  * remove_inode_hugepages handles two distinct cases: truncation and hole
390b5cec28dSMike Kravetz  * punch.  There are subtle differences in operation for each case.
3914aae8d1cSMike Kravetz  *
392b5cec28dSMike Kravetz  * truncation is indicated by end of range being LLONG_MAX
393b5cec28dSMike Kravetz  *	In this case, we first scan the range and release found pages.
394b5cec28dSMike Kravetz  *	After releasing pages, hugetlb_unreserve_pages cleans up region/reserv
395e7c58097SMike Kravetz  *	maps and global counts.  Page faults can not race with truncation
396e7c58097SMike Kravetz  *	in this routine.  hugetlb_no_page() prevents page faults in the
397e7c58097SMike Kravetz  *	truncated range.  It checks i_size before allocation, and again after
398e7c58097SMike Kravetz  *	with the page table lock for the page held.  The same lock must be
399e7c58097SMike Kravetz  *	acquired to unmap a page.
400b5cec28dSMike Kravetz  * hole punch is indicated if end is not LLONG_MAX
401b5cec28dSMike Kravetz  *	In the hole punch case we scan the range and release found pages.
402b5cec28dSMike Kravetz  *	Only when releasing a page is the associated region/reserv map
403b5cec28dSMike Kravetz  *	deleted.  The region/reserv map for ranges without associated
404e7c58097SMike Kravetz  *	pages are not modified.  Page faults can race with hole punch.
405e7c58097SMike Kravetz  *	This is indicated if we find a mapped page.
406b5cec28dSMike Kravetz  * Note: If the passed end of range value is beyond the end of file, but
407b5cec28dSMike Kravetz  * not LLONG_MAX this routine still performs a hole punch operation.
408b5cec28dSMike Kravetz  */
409b5cec28dSMike Kravetz static void remove_inode_hugepages(struct inode *inode, loff_t lstart,
410b5cec28dSMike Kravetz 				   loff_t lend)
4111da177e4SLinus Torvalds {
412a5516438SAndi Kleen 	struct hstate *h = hstate_inode(inode);
413b45b5bd6SDavid Gibson 	struct address_space *mapping = &inode->i_data;
414a5516438SAndi Kleen 	const pgoff_t start = lstart >> huge_page_shift(h);
415b5cec28dSMike Kravetz 	const pgoff_t end = lend >> huge_page_shift(h);
416b5cec28dSMike Kravetz 	struct vm_area_struct pseudo_vma;
4171da177e4SLinus Torvalds 	struct pagevec pvec;
418d72dc8a2SJan Kara 	pgoff_t next, index;
419a43a8c39SChen, Kenneth W 	int i, freed = 0;
420b5cec28dSMike Kravetz 	bool truncate_op = (lend == LLONG_MAX);
4211da177e4SLinus Torvalds 
4222c4541e2SKirill A. Shutemov 	vma_init(&pseudo_vma, current->mm);
423b5cec28dSMike Kravetz 	pseudo_vma.vm_flags = (VM_HUGETLB | VM_MAYSHARE | VM_SHARED);
42486679820SMel Gorman 	pagevec_init(&pvec);
4251da177e4SLinus Torvalds 	next = start;
426b5cec28dSMike Kravetz 	while (next < end) {
427b5cec28dSMike Kravetz 		/*
4281817889eSMike Kravetz 		 * When no more pages are found, we are done.
429b5cec28dSMike Kravetz 		 */
430397162ffSJan Kara 		if (!pagevec_lookup_range(&pvec, mapping, &next, end - 1))
4311da177e4SLinus Torvalds 			break;
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); ++i) {
4341da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
435e7c58097SMike Kravetz 			u32 hash;
436b5cec28dSMike Kravetz 
437d72dc8a2SJan Kara 			index = page->index;
438188b04a7SWei Yang 			hash = hugetlb_fault_mutex_hash(mapping, index);
439e7c58097SMike Kravetz 			mutex_lock(&hugetlb_fault_mutex_table[hash]);
440e7c58097SMike Kravetz 
441b5cec28dSMike Kravetz 			/*
442e7c58097SMike Kravetz 			 * If page is mapped, it was faulted in after being
443e7c58097SMike Kravetz 			 * unmapped in caller.  Unmap (again) now after taking
444e7c58097SMike Kravetz 			 * the fault mutex.  The mutex will prevent faults
445e7c58097SMike Kravetz 			 * until we finish removing the page.
446e7c58097SMike Kravetz 			 *
447e7c58097SMike Kravetz 			 * This race can only happen in the hole punch case.
448e7c58097SMike Kravetz 			 * Getting here in a truncate operation is a bug.
449b5cec28dSMike Kravetz 			 */
450e7c58097SMike Kravetz 			if (unlikely(page_mapped(page))) {
451e7c58097SMike Kravetz 				BUG_ON(truncate_op);
452e7c58097SMike Kravetz 
453e7c58097SMike Kravetz 				i_mmap_lock_write(mapping);
454e7c58097SMike Kravetz 				hugetlb_vmdelete_list(&mapping->i_mmap,
455e7c58097SMike Kravetz 					index * pages_per_huge_page(h),
456e7c58097SMike Kravetz 					(index + 1) * pages_per_huge_page(h));
457e7c58097SMike Kravetz 				i_mmap_unlock_write(mapping);
458e7c58097SMike Kravetz 			}
4594aae8d1cSMike Kravetz 
4604aae8d1cSMike Kravetz 			lock_page(page);
4614aae8d1cSMike Kravetz 			/*
4624aae8d1cSMike Kravetz 			 * We must free the huge page and remove from page
4634aae8d1cSMike Kravetz 			 * cache (remove_huge_page) BEFORE removing the
4644aae8d1cSMike Kravetz 			 * region/reserve map (hugetlb_unreserve_pages).  In
4654aae8d1cSMike Kravetz 			 * rare out of memory conditions, removal of the
46672e2936cSzhong jiang 			 * region/reserve map could fail. Correspondingly,
46772e2936cSzhong jiang 			 * the subpool and global reserve usage count can need
46872e2936cSzhong jiang 			 * to be adjusted.
4694aae8d1cSMike Kravetz 			 */
47072e2936cSzhong jiang 			VM_BUG_ON(PagePrivate(page));
471b5cec28dSMike Kravetz 			remove_huge_page(page);
472b5cec28dSMike Kravetz 			freed++;
473b5cec28dSMike Kravetz 			if (!truncate_op) {
4744aae8d1cSMike Kravetz 				if (unlikely(hugetlb_unreserve_pages(inode,
475d72dc8a2SJan Kara 							index, index + 1, 1)))
47672e2936cSzhong jiang 					hugetlb_fix_reserve_counts(inode);
477b5cec28dSMike Kravetz 			}
478b5cec28dSMike Kravetz 
4791da177e4SLinus Torvalds 			unlock_page(page);
480e7c58097SMike Kravetz 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
4811da177e4SLinus Torvalds 		}
4821da177e4SLinus Torvalds 		huge_pagevec_release(&pvec);
4831817889eSMike Kravetz 		cond_resched();
4841da177e4SLinus Torvalds 	}
485b5cec28dSMike Kravetz 
486b5cec28dSMike Kravetz 	if (truncate_op)
487b5cec28dSMike Kravetz 		(void)hugetlb_unreserve_pages(inode, start, LONG_MAX, freed);
4881da177e4SLinus Torvalds }
4891da177e4SLinus Torvalds 
4902bbbda30SAl Viro static void hugetlbfs_evict_inode(struct inode *inode)
4911da177e4SLinus Torvalds {
4929119a41eSJoonsoo Kim 	struct resv_map *resv_map;
4939119a41eSJoonsoo Kim 
494b5cec28dSMike Kravetz 	remove_inode_hugepages(inode, 0, LLONG_MAX);
495f27a5136SMike Kravetz 
496f27a5136SMike Kravetz 	/*
497f27a5136SMike Kravetz 	 * Get the resv_map from the address space embedded in the inode.
498f27a5136SMike Kravetz 	 * This is the address space which points to any resv_map allocated
499f27a5136SMike Kravetz 	 * at inode creation time.  If this is a device special inode,
500f27a5136SMike Kravetz 	 * i_mapping may not point to the original address space.
501f27a5136SMike Kravetz 	 */
502f27a5136SMike Kravetz 	resv_map = (struct resv_map *)(&inode->i_data)->private_data;
503f27a5136SMike Kravetz 	/* Only regular and link inodes have associated reserve maps */
5049119a41eSJoonsoo Kim 	if (resv_map)
5059119a41eSJoonsoo Kim 		resv_map_release(&resv_map->refs);
506dbd5768fSJan Kara 	clear_inode(inode);
507149f4211SChristoph Hellwig }
508149f4211SChristoph Hellwig 
5091da177e4SLinus Torvalds static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
5101da177e4SLinus Torvalds {
511856fc295SHugh Dickins 	pgoff_t pgoff;
5121da177e4SLinus Torvalds 	struct address_space *mapping = inode->i_mapping;
513a5516438SAndi Kleen 	struct hstate *h = hstate_inode(inode);
5141da177e4SLinus Torvalds 
515a5516438SAndi Kleen 	BUG_ON(offset & ~huge_page_mask(h));
516856fc295SHugh Dickins 	pgoff = offset >> PAGE_SHIFT;
5171da177e4SLinus Torvalds 
5187aa91e10SKen Chen 	i_size_write(inode, offset);
51983cde9e8SDavidlohr Bueso 	i_mmap_lock_write(mapping);
520f808c13fSDavidlohr Bueso 	if (!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))
5211bfad99aSMike Kravetz 		hugetlb_vmdelete_list(&mapping->i_mmap, pgoff, 0);
522c86aa7bbSMike Kravetz 	i_mmap_unlock_write(mapping);
523e7c58097SMike Kravetz 	remove_inode_hugepages(inode, offset, LLONG_MAX);
5241da177e4SLinus Torvalds 	return 0;
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds 
52770c3547eSMike Kravetz static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
52870c3547eSMike Kravetz {
52970c3547eSMike Kravetz 	struct hstate *h = hstate_inode(inode);
53070c3547eSMike Kravetz 	loff_t hpage_size = huge_page_size(h);
53170c3547eSMike Kravetz 	loff_t hole_start, hole_end;
53270c3547eSMike Kravetz 
53370c3547eSMike Kravetz 	/*
53470c3547eSMike Kravetz 	 * For hole punch round up the beginning offset of the hole and
53570c3547eSMike Kravetz 	 * round down the end.
53670c3547eSMike Kravetz 	 */
53770c3547eSMike Kravetz 	hole_start = round_up(offset, hpage_size);
53870c3547eSMike Kravetz 	hole_end = round_down(offset + len, hpage_size);
53970c3547eSMike Kravetz 
54070c3547eSMike Kravetz 	if (hole_end > hole_start) {
54170c3547eSMike Kravetz 		struct address_space *mapping = inode->i_mapping;
542ff62a342SMarc-André Lureau 		struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
54370c3547eSMike Kravetz 
5445955102cSAl Viro 		inode_lock(inode);
545ff62a342SMarc-André Lureau 
546ff62a342SMarc-André Lureau 		/* protected by i_mutex */
547ab3948f5SJoel Fernandes (Google) 		if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
548ff62a342SMarc-André Lureau 			inode_unlock(inode);
549ff62a342SMarc-André Lureau 			return -EPERM;
550ff62a342SMarc-André Lureau 		}
551ff62a342SMarc-André Lureau 
55270c3547eSMike Kravetz 		i_mmap_lock_write(mapping);
553f808c13fSDavidlohr Bueso 		if (!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))
55470c3547eSMike Kravetz 			hugetlb_vmdelete_list(&mapping->i_mmap,
55570c3547eSMike Kravetz 						hole_start >> PAGE_SHIFT,
55670c3547eSMike Kravetz 						hole_end  >> PAGE_SHIFT);
557c86aa7bbSMike Kravetz 		i_mmap_unlock_write(mapping);
558e7c58097SMike Kravetz 		remove_inode_hugepages(inode, hole_start, hole_end);
5595955102cSAl Viro 		inode_unlock(inode);
56070c3547eSMike Kravetz 	}
56170c3547eSMike Kravetz 
56270c3547eSMike Kravetz 	return 0;
56370c3547eSMike Kravetz }
56470c3547eSMike Kravetz 
56570c3547eSMike Kravetz static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
56670c3547eSMike Kravetz 				loff_t len)
56770c3547eSMike Kravetz {
56870c3547eSMike Kravetz 	struct inode *inode = file_inode(file);
569ff62a342SMarc-André Lureau 	struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
57070c3547eSMike Kravetz 	struct address_space *mapping = inode->i_mapping;
57170c3547eSMike Kravetz 	struct hstate *h = hstate_inode(inode);
57270c3547eSMike Kravetz 	struct vm_area_struct pseudo_vma;
57370c3547eSMike Kravetz 	struct mm_struct *mm = current->mm;
57470c3547eSMike Kravetz 	loff_t hpage_size = huge_page_size(h);
57570c3547eSMike Kravetz 	unsigned long hpage_shift = huge_page_shift(h);
57670c3547eSMike Kravetz 	pgoff_t start, index, end;
57770c3547eSMike Kravetz 	int error;
57870c3547eSMike Kravetz 	u32 hash;
57970c3547eSMike Kravetz 
58070c3547eSMike Kravetz 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
58170c3547eSMike Kravetz 		return -EOPNOTSUPP;
58270c3547eSMike Kravetz 
58370c3547eSMike Kravetz 	if (mode & FALLOC_FL_PUNCH_HOLE)
58470c3547eSMike Kravetz 		return hugetlbfs_punch_hole(inode, offset, len);
58570c3547eSMike Kravetz 
58670c3547eSMike Kravetz 	/*
58770c3547eSMike Kravetz 	 * Default preallocate case.
58870c3547eSMike Kravetz 	 * For this range, start is rounded down and end is rounded up
58970c3547eSMike Kravetz 	 * as well as being converted to page offsets.
59070c3547eSMike Kravetz 	 */
59170c3547eSMike Kravetz 	start = offset >> hpage_shift;
59270c3547eSMike Kravetz 	end = (offset + len + hpage_size - 1) >> hpage_shift;
59370c3547eSMike Kravetz 
5945955102cSAl Viro 	inode_lock(inode);
59570c3547eSMike Kravetz 
59670c3547eSMike Kravetz 	/* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
59770c3547eSMike Kravetz 	error = inode_newsize_ok(inode, offset + len);
59870c3547eSMike Kravetz 	if (error)
59970c3547eSMike Kravetz 		goto out;
60070c3547eSMike Kravetz 
601ff62a342SMarc-André Lureau 	if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
602ff62a342SMarc-André Lureau 		error = -EPERM;
603ff62a342SMarc-André Lureau 		goto out;
604ff62a342SMarc-André Lureau 	}
605ff62a342SMarc-André Lureau 
60670c3547eSMike Kravetz 	/*
60770c3547eSMike Kravetz 	 * Initialize a pseudo vma as this is required by the huge page
60870c3547eSMike Kravetz 	 * allocation routines.  If NUMA is configured, use page index
60970c3547eSMike Kravetz 	 * as input to create an allocation policy.
61070c3547eSMike Kravetz 	 */
6112c4541e2SKirill A. Shutemov 	vma_init(&pseudo_vma, mm);
61270c3547eSMike Kravetz 	pseudo_vma.vm_flags = (VM_HUGETLB | VM_MAYSHARE | VM_SHARED);
61370c3547eSMike Kravetz 	pseudo_vma.vm_file = file;
61470c3547eSMike Kravetz 
61570c3547eSMike Kravetz 	for (index = start; index < end; index++) {
61670c3547eSMike Kravetz 		/*
61770c3547eSMike Kravetz 		 * This is supposed to be the vaddr where the page is being
61870c3547eSMike Kravetz 		 * faulted in, but we have no vaddr here.
61970c3547eSMike Kravetz 		 */
62070c3547eSMike Kravetz 		struct page *page;
62170c3547eSMike Kravetz 		unsigned long addr;
62270c3547eSMike Kravetz 		int avoid_reserve = 0;
62370c3547eSMike Kravetz 
62470c3547eSMike Kravetz 		cond_resched();
62570c3547eSMike Kravetz 
62670c3547eSMike Kravetz 		/*
62770c3547eSMike Kravetz 		 * fallocate(2) manpage permits EINTR; we may have been
62870c3547eSMike Kravetz 		 * interrupted because we are using up too much memory.
62970c3547eSMike Kravetz 		 */
63070c3547eSMike Kravetz 		if (signal_pending(current)) {
63170c3547eSMike Kravetz 			error = -EINTR;
63270c3547eSMike Kravetz 			break;
63370c3547eSMike Kravetz 		}
63470c3547eSMike Kravetz 
63570c3547eSMike Kravetz 		/* Set numa allocation policy based on index */
63670c3547eSMike Kravetz 		hugetlb_set_vma_policy(&pseudo_vma, inode, index);
63770c3547eSMike Kravetz 
63870c3547eSMike Kravetz 		/* addr is the offset within the file (zero based) */
63970c3547eSMike Kravetz 		addr = index * hpage_size;
64070c3547eSMike Kravetz 
641e7c58097SMike Kravetz 		/* mutex taken here, fault path and hole punch */
642188b04a7SWei Yang 		hash = hugetlb_fault_mutex_hash(mapping, index);
64370c3547eSMike Kravetz 		mutex_lock(&hugetlb_fault_mutex_table[hash]);
64470c3547eSMike Kravetz 
64570c3547eSMike Kravetz 		/* See if already present in mapping to avoid alloc/free */
64670c3547eSMike Kravetz 		page = find_get_page(mapping, index);
64770c3547eSMike Kravetz 		if (page) {
64870c3547eSMike Kravetz 			put_page(page);
64970c3547eSMike Kravetz 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
65070c3547eSMike Kravetz 			hugetlb_drop_vma_policy(&pseudo_vma);
65170c3547eSMike Kravetz 			continue;
65270c3547eSMike Kravetz 		}
65370c3547eSMike Kravetz 
65470c3547eSMike Kravetz 		/* Allocate page and add to page cache */
65570c3547eSMike Kravetz 		page = alloc_huge_page(&pseudo_vma, addr, avoid_reserve);
65670c3547eSMike Kravetz 		hugetlb_drop_vma_policy(&pseudo_vma);
65770c3547eSMike Kravetz 		if (IS_ERR(page)) {
65870c3547eSMike Kravetz 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
65970c3547eSMike Kravetz 			error = PTR_ERR(page);
66070c3547eSMike Kravetz 			goto out;
66170c3547eSMike Kravetz 		}
66270c3547eSMike Kravetz 		clear_huge_page(page, addr, pages_per_huge_page(h));
66370c3547eSMike Kravetz 		__SetPageUptodate(page);
66470c3547eSMike Kravetz 		error = huge_add_to_page_cache(page, mapping, index);
66570c3547eSMike Kravetz 		if (unlikely(error)) {
66670c3547eSMike Kravetz 			put_page(page);
66770c3547eSMike Kravetz 			mutex_unlock(&hugetlb_fault_mutex_table[hash]);
66870c3547eSMike Kravetz 			goto out;
66970c3547eSMike Kravetz 		}
67070c3547eSMike Kravetz 
67170c3547eSMike Kravetz 		mutex_unlock(&hugetlb_fault_mutex_table[hash]);
67270c3547eSMike Kravetz 
67370c3547eSMike Kravetz 		/*
67470c3547eSMike Kravetz 		 * unlock_page because locked by add_to_page_cache()
67572639e6dSNadav Amit 		 * page_put due to reference from alloc_huge_page()
67670c3547eSMike Kravetz 		 */
67770c3547eSMike Kravetz 		unlock_page(page);
67872639e6dSNadav Amit 		put_page(page);
67970c3547eSMike Kravetz 	}
68070c3547eSMike Kravetz 
68170c3547eSMike Kravetz 	if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
68270c3547eSMike Kravetz 		i_size_write(inode, offset + len);
683078cd827SDeepa Dinamani 	inode->i_ctime = current_time(inode);
68470c3547eSMike Kravetz out:
6855955102cSAl Viro 	inode_unlock(inode);
68670c3547eSMike Kravetz 	return error;
68770c3547eSMike Kravetz }
68870c3547eSMike Kravetz 
6891da177e4SLinus Torvalds static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
6901da177e4SLinus Torvalds {
6912b0143b5SDavid Howells 	struct inode *inode = d_inode(dentry);
692a5516438SAndi Kleen 	struct hstate *h = hstate_inode(inode);
6931da177e4SLinus Torvalds 	int error;
6941da177e4SLinus Torvalds 	unsigned int ia_valid = attr->ia_valid;
695ff62a342SMarc-André Lureau 	struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	BUG_ON(!inode);
6981da177e4SLinus Torvalds 
69931051c85SJan Kara 	error = setattr_prepare(dentry, attr);
7001da177e4SLinus Torvalds 	if (error)
7011025774cSChristoph Hellwig 		return error;
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	if (ia_valid & ATTR_SIZE) {
704ff62a342SMarc-André Lureau 		loff_t oldsize = inode->i_size;
705ff62a342SMarc-André Lureau 		loff_t newsize = attr->ia_size;
706ff62a342SMarc-André Lureau 
707ff62a342SMarc-André Lureau 		if (newsize & ~huge_page_mask(h))
7081025774cSChristoph Hellwig 			return -EINVAL;
709ff62a342SMarc-André Lureau 		/* protected by i_mutex */
710ff62a342SMarc-André Lureau 		if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
711ff62a342SMarc-André Lureau 		    (newsize > oldsize && (info->seals & F_SEAL_GROW)))
712ff62a342SMarc-André Lureau 			return -EPERM;
713ff62a342SMarc-André Lureau 		error = hugetlb_vmtruncate(inode, newsize);
7141da177e4SLinus Torvalds 		if (error)
7151da177e4SLinus Torvalds 			return error;
7161da177e4SLinus Torvalds 	}
7171da177e4SLinus Torvalds 
7181025774cSChristoph Hellwig 	setattr_copy(inode, attr);
7191025774cSChristoph Hellwig 	mark_inode_dirty(inode);
7201025774cSChristoph Hellwig 	return 0;
7211025774cSChristoph Hellwig }
7221025774cSChristoph Hellwig 
7237d54fa64SAl Viro static struct inode *hugetlbfs_get_root(struct super_block *sb,
72432021982SDavid Howells 					struct hugetlbfs_fs_context *ctx)
7251da177e4SLinus Torvalds {
7261da177e4SLinus Torvalds 	struct inode *inode;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	inode = new_inode(sb);
7291da177e4SLinus Torvalds 	if (inode) {
73085fe4025SChristoph Hellwig 		inode->i_ino = get_next_ino();
73132021982SDavid Howells 		inode->i_mode = S_IFDIR | ctx->mode;
73232021982SDavid Howells 		inode->i_uid = ctx->uid;
73332021982SDavid Howells 		inode->i_gid = ctx->gid;
734078cd827SDeepa Dinamani 		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
7357d54fa64SAl Viro 		inode->i_op = &hugetlbfs_dir_inode_operations;
7367d54fa64SAl Viro 		inode->i_fop = &simple_dir_operations;
7377d54fa64SAl Viro 		/* directory inodes start off with i_nlink == 2 (for "." entry) */
7387d54fa64SAl Viro 		inc_nlink(inode);
73965ed7601SAneesh Kumar K.V 		lockdep_annotate_inode_mutex_key(inode);
7407d54fa64SAl Viro 	}
7417d54fa64SAl Viro 	return inode;
7427d54fa64SAl Viro }
7437d54fa64SAl Viro 
744b610ded7SMichal Hocko /*
745c8c06efaSDavidlohr Bueso  * Hugetlbfs is not reclaimable; therefore its i_mmap_rwsem will never
746b610ded7SMichal Hocko  * be taken from reclaim -- unlike regular filesystems. This needs an
74788f306b6SKirill A. Shutemov  * annotation because huge_pmd_share() does an allocation under hugetlb's
748c8c06efaSDavidlohr Bueso  * i_mmap_rwsem.
749b610ded7SMichal Hocko  */
750c8c06efaSDavidlohr Bueso static struct lock_class_key hugetlbfs_i_mmap_rwsem_key;
751b610ded7SMichal Hocko 
7527d54fa64SAl Viro static struct inode *hugetlbfs_get_inode(struct super_block *sb,
7537d54fa64SAl Viro 					struct inode *dir,
75418df2252SAl Viro 					umode_t mode, dev_t dev)
7557d54fa64SAl Viro {
7567d54fa64SAl Viro 	struct inode *inode;
75758b6e5e8SMike Kravetz 	struct resv_map *resv_map = NULL;
7589119a41eSJoonsoo Kim 
75958b6e5e8SMike Kravetz 	/*
76058b6e5e8SMike Kravetz 	 * Reserve maps are only needed for inodes that can have associated
76158b6e5e8SMike Kravetz 	 * page allocations.
76258b6e5e8SMike Kravetz 	 */
76358b6e5e8SMike Kravetz 	if (S_ISREG(mode) || S_ISLNK(mode)) {
7649119a41eSJoonsoo Kim 		resv_map = resv_map_alloc();
7659119a41eSJoonsoo Kim 		if (!resv_map)
7669119a41eSJoonsoo Kim 			return NULL;
76758b6e5e8SMike Kravetz 	}
7687d54fa64SAl Viro 
7697d54fa64SAl Viro 	inode = new_inode(sb);
7707d54fa64SAl Viro 	if (inode) {
771ff62a342SMarc-André Lureau 		struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
772ff62a342SMarc-André Lureau 
7737d54fa64SAl Viro 		inode->i_ino = get_next_ino();
7747d54fa64SAl Viro 		inode_init_owner(inode, dir, mode);
775c8c06efaSDavidlohr Bueso 		lockdep_set_class(&inode->i_mapping->i_mmap_rwsem,
776c8c06efaSDavidlohr Bueso 				&hugetlbfs_i_mmap_rwsem_key);
7771da177e4SLinus Torvalds 		inode->i_mapping->a_ops = &hugetlbfs_aops;
778078cd827SDeepa Dinamani 		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
7799119a41eSJoonsoo Kim 		inode->i_mapping->private_data = resv_map;
780ff62a342SMarc-André Lureau 		info->seals = F_SEAL_SEAL;
7811da177e4SLinus Torvalds 		switch (mode & S_IFMT) {
7821da177e4SLinus Torvalds 		default:
7831da177e4SLinus Torvalds 			init_special_inode(inode, mode, dev);
7841da177e4SLinus Torvalds 			break;
7851da177e4SLinus Torvalds 		case S_IFREG:
7861da177e4SLinus Torvalds 			inode->i_op = &hugetlbfs_inode_operations;
7871da177e4SLinus Torvalds 			inode->i_fop = &hugetlbfs_file_operations;
7881da177e4SLinus Torvalds 			break;
7891da177e4SLinus Torvalds 		case S_IFDIR:
7901da177e4SLinus Torvalds 			inode->i_op = &hugetlbfs_dir_inode_operations;
7911da177e4SLinus Torvalds 			inode->i_fop = &simple_dir_operations;
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 			/* directory inodes start off with i_nlink == 2 (for "." entry) */
794d8c76e6fSDave Hansen 			inc_nlink(inode);
7951da177e4SLinus Torvalds 			break;
7961da177e4SLinus Torvalds 		case S_IFLNK:
7971da177e4SLinus Torvalds 			inode->i_op = &page_symlink_inode_operations;
79821fc61c7SAl Viro 			inode_nohighmem(inode);
7991da177e4SLinus Torvalds 			break;
8001da177e4SLinus Torvalds 		}
801e096d0c7SJosh Boyer 		lockdep_annotate_inode_mutex_key(inode);
80258b6e5e8SMike Kravetz 	} else {
80358b6e5e8SMike Kravetz 		if (resv_map)
8049119a41eSJoonsoo Kim 			kref_put(&resv_map->refs, resv_map_release);
80558b6e5e8SMike Kravetz 	}
8069119a41eSJoonsoo Kim 
8071da177e4SLinus Torvalds 	return inode;
8081da177e4SLinus Torvalds }
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds /*
8111da177e4SLinus Torvalds  * File creation. Allocate an inode, and we're done..
8121da177e4SLinus Torvalds  */
8131ab5b82fSPiotr Sarna static int do_hugetlbfs_mknod(struct inode *dir,
8141ab5b82fSPiotr Sarna 			struct dentry *dentry,
8151ab5b82fSPiotr Sarna 			umode_t mode,
8161ab5b82fSPiotr Sarna 			dev_t dev,
8171ab5b82fSPiotr Sarna 			bool tmpfile)
8181da177e4SLinus Torvalds {
8191da177e4SLinus Torvalds 	struct inode *inode;
8201da177e4SLinus Torvalds 	int error = -ENOSPC;
8211da177e4SLinus Torvalds 
8227d54fa64SAl Viro 	inode = hugetlbfs_get_inode(dir->i_sb, dir, mode, dev);
8231da177e4SLinus Torvalds 	if (inode) {
824078cd827SDeepa Dinamani 		dir->i_ctime = dir->i_mtime = current_time(dir);
8251ab5b82fSPiotr Sarna 		if (tmpfile) {
8261ab5b82fSPiotr Sarna 			d_tmpfile(dentry, inode);
8271ab5b82fSPiotr Sarna 		} else {
8281da177e4SLinus Torvalds 			d_instantiate(dentry, inode);
8291da177e4SLinus Torvalds 			dget(dentry);/* Extra count - pin the dentry in core */
8301ab5b82fSPiotr Sarna 		}
8311da177e4SLinus Torvalds 		error = 0;
8321da177e4SLinus Torvalds 	}
8331da177e4SLinus Torvalds 	return error;
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
8361ab5b82fSPiotr Sarna static int hugetlbfs_mknod(struct inode *dir,
8371ab5b82fSPiotr Sarna 			struct dentry *dentry, umode_t mode, dev_t dev)
8381ab5b82fSPiotr Sarna {
8391ab5b82fSPiotr Sarna 	return do_hugetlbfs_mknod(dir, dentry, mode, dev, false);
8401ab5b82fSPiotr Sarna }
8411ab5b82fSPiotr Sarna 
84218bb1db3SAl Viro static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
8431da177e4SLinus Torvalds {
8441da177e4SLinus Torvalds 	int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
8451da177e4SLinus Torvalds 	if (!retval)
846d8c76e6fSDave Hansen 		inc_nlink(dir);
8471da177e4SLinus Torvalds 	return retval;
8481da177e4SLinus Torvalds }
8491da177e4SLinus Torvalds 
850ebfc3b49SAl Viro static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
8511da177e4SLinus Torvalds {
8521da177e4SLinus Torvalds 	return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds 
8551ab5b82fSPiotr Sarna static int hugetlbfs_tmpfile(struct inode *dir,
8561ab5b82fSPiotr Sarna 			struct dentry *dentry, umode_t mode)
8571ab5b82fSPiotr Sarna {
8581ab5b82fSPiotr Sarna 	return do_hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0, true);
8591ab5b82fSPiotr Sarna }
8601ab5b82fSPiotr Sarna 
8611da177e4SLinus Torvalds static int hugetlbfs_symlink(struct inode *dir,
8621da177e4SLinus Torvalds 			struct dentry *dentry, const char *symname)
8631da177e4SLinus Torvalds {
8641da177e4SLinus Torvalds 	struct inode *inode;
8651da177e4SLinus Torvalds 	int error = -ENOSPC;
8661da177e4SLinus Torvalds 
8677d54fa64SAl Viro 	inode = hugetlbfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
8681da177e4SLinus Torvalds 	if (inode) {
8691da177e4SLinus Torvalds 		int l = strlen(symname)+1;
8701da177e4SLinus Torvalds 		error = page_symlink(inode, symname, l);
8711da177e4SLinus Torvalds 		if (!error) {
8721da177e4SLinus Torvalds 			d_instantiate(dentry, inode);
8731da177e4SLinus Torvalds 			dget(dentry);
8741da177e4SLinus Torvalds 		} else
8751da177e4SLinus Torvalds 			iput(inode);
8761da177e4SLinus Torvalds 	}
877078cd827SDeepa Dinamani 	dir->i_ctime = dir->i_mtime = current_time(dir);
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	return error;
8801da177e4SLinus Torvalds }
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds /*
8836649a386SKen Chen  * mark the head page dirty
8841da177e4SLinus Torvalds  */
8851da177e4SLinus Torvalds static int hugetlbfs_set_page_dirty(struct page *page)
8861da177e4SLinus Torvalds {
887d85f3385SChristoph Lameter 	struct page *head = compound_head(page);
8886649a386SKen Chen 
8896649a386SKen Chen 	SetPageDirty(head);
8901da177e4SLinus Torvalds 	return 0;
8911da177e4SLinus Torvalds }
8921da177e4SLinus Torvalds 
893290408d4SNaoya Horiguchi static int hugetlbfs_migrate_page(struct address_space *mapping,
894b969c4abSMel Gorman 				struct page *newpage, struct page *page,
895a6bc32b8SMel Gorman 				enum migrate_mode mode)
896290408d4SNaoya Horiguchi {
897290408d4SNaoya Horiguchi 	int rc;
898290408d4SNaoya Horiguchi 
899290408d4SNaoya Horiguchi 	rc = migrate_huge_page_move_mapping(mapping, newpage, page);
90078bd5209SRafael Aquini 	if (rc != MIGRATEPAGE_SUCCESS)
901290408d4SNaoya Horiguchi 		return rc;
902cb6acd01SMike Kravetz 
903cb6acd01SMike Kravetz 	/*
904cb6acd01SMike Kravetz 	 * page_private is subpool pointer in hugetlb pages.  Transfer to
905cb6acd01SMike Kravetz 	 * new page.  PagePrivate is not associated with page_private for
906cb6acd01SMike Kravetz 	 * hugetlb pages and can not be set here as only page_huge_active
907cb6acd01SMike Kravetz 	 * pages can be migrated.
908cb6acd01SMike Kravetz 	 */
909cb6acd01SMike Kravetz 	if (page_private(page)) {
910cb6acd01SMike Kravetz 		set_page_private(newpage, page_private(page));
911cb6acd01SMike Kravetz 		set_page_private(page, 0);
912cb6acd01SMike Kravetz 	}
913cb6acd01SMike Kravetz 
9142916ecc0SJérôme Glisse 	if (mode != MIGRATE_SYNC_NO_COPY)
915290408d4SNaoya Horiguchi 		migrate_page_copy(newpage, page);
9162916ecc0SJérôme Glisse 	else
9172916ecc0SJérôme Glisse 		migrate_page_states(newpage, page);
918290408d4SNaoya Horiguchi 
91978bd5209SRafael Aquini 	return MIGRATEPAGE_SUCCESS;
920290408d4SNaoya Horiguchi }
921290408d4SNaoya Horiguchi 
92278bb9203SNaoya Horiguchi static int hugetlbfs_error_remove_page(struct address_space *mapping,
92378bb9203SNaoya Horiguchi 				struct page *page)
92478bb9203SNaoya Horiguchi {
92578bb9203SNaoya Horiguchi 	struct inode *inode = mapping->host;
926ab615a5bSMike Kravetz 	pgoff_t index = page->index;
92778bb9203SNaoya Horiguchi 
92878bb9203SNaoya Horiguchi 	remove_huge_page(page);
929ab615a5bSMike Kravetz 	if (unlikely(hugetlb_unreserve_pages(inode, index, index + 1, 1)))
93078bb9203SNaoya Horiguchi 		hugetlb_fix_reserve_counts(inode);
931ab615a5bSMike Kravetz 
93278bb9203SNaoya Horiguchi 	return 0;
93378bb9203SNaoya Horiguchi }
93478bb9203SNaoya Horiguchi 
9354a25220dSDavid Howells /*
9364a25220dSDavid Howells  * Display the mount options in /proc/mounts.
9374a25220dSDavid Howells  */
9384a25220dSDavid Howells static int hugetlbfs_show_options(struct seq_file *m, struct dentry *root)
9394a25220dSDavid Howells {
9404a25220dSDavid Howells 	struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(root->d_sb);
9414a25220dSDavid Howells 	struct hugepage_subpool *spool = sbinfo->spool;
9424a25220dSDavid Howells 	unsigned long hpage_size = huge_page_size(sbinfo->hstate);
9434a25220dSDavid Howells 	unsigned hpage_shift = huge_page_shift(sbinfo->hstate);
9444a25220dSDavid Howells 	char mod;
9454a25220dSDavid Howells 
9464a25220dSDavid Howells 	if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
9474a25220dSDavid Howells 		seq_printf(m, ",uid=%u",
9484a25220dSDavid Howells 			   from_kuid_munged(&init_user_ns, sbinfo->uid));
9494a25220dSDavid Howells 	if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
9504a25220dSDavid Howells 		seq_printf(m, ",gid=%u",
9514a25220dSDavid Howells 			   from_kgid_munged(&init_user_ns, sbinfo->gid));
9524a25220dSDavid Howells 	if (sbinfo->mode != 0755)
9534a25220dSDavid Howells 		seq_printf(m, ",mode=%o", sbinfo->mode);
9544a25220dSDavid Howells 	if (sbinfo->max_inodes != -1)
9554a25220dSDavid Howells 		seq_printf(m, ",nr_inodes=%lu", sbinfo->max_inodes);
9564a25220dSDavid Howells 
9574a25220dSDavid Howells 	hpage_size /= 1024;
9584a25220dSDavid Howells 	mod = 'K';
9594a25220dSDavid Howells 	if (hpage_size >= 1024) {
9604a25220dSDavid Howells 		hpage_size /= 1024;
9614a25220dSDavid Howells 		mod = 'M';
9624a25220dSDavid Howells 	}
9634a25220dSDavid Howells 	seq_printf(m, ",pagesize=%lu%c", hpage_size, mod);
9644a25220dSDavid Howells 	if (spool) {
9654a25220dSDavid Howells 		if (spool->max_hpages != -1)
9664a25220dSDavid Howells 			seq_printf(m, ",size=%llu",
9674a25220dSDavid Howells 				   (unsigned long long)spool->max_hpages << hpage_shift);
9684a25220dSDavid Howells 		if (spool->min_hpages != -1)
9694a25220dSDavid Howells 			seq_printf(m, ",min_size=%llu",
9704a25220dSDavid Howells 				   (unsigned long long)spool->min_hpages << hpage_shift);
9714a25220dSDavid Howells 	}
9724a25220dSDavid Howells 	return 0;
9734a25220dSDavid Howells }
9744a25220dSDavid Howells 
975726c3342SDavid Howells static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
9761da177e4SLinus Torvalds {
977726c3342SDavid Howells 	struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
9782b0143b5SDavid Howells 	struct hstate *h = hstate_inode(d_inode(dentry));
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	buf->f_type = HUGETLBFS_MAGIC;
981a5516438SAndi Kleen 	buf->f_bsize = huge_page_size(h);
9821da177e4SLinus Torvalds 	if (sbinfo) {
9831da177e4SLinus Torvalds 		spin_lock(&sbinfo->stat_lock);
98474a8a65cSDavid Gibson 		/* If no limits set, just report 0 for max/free/used
98574a8a65cSDavid Gibson 		 * blocks, like simple_statfs() */
98690481622SDavid Gibson 		if (sbinfo->spool) {
98790481622SDavid Gibson 			long free_pages;
98890481622SDavid Gibson 
98990481622SDavid Gibson 			spin_lock(&sbinfo->spool->lock);
99090481622SDavid Gibson 			buf->f_blocks = sbinfo->spool->max_hpages;
99190481622SDavid Gibson 			free_pages = sbinfo->spool->max_hpages
99290481622SDavid Gibson 				- sbinfo->spool->used_hpages;
99390481622SDavid Gibson 			buf->f_bavail = buf->f_bfree = free_pages;
99490481622SDavid Gibson 			spin_unlock(&sbinfo->spool->lock);
9951da177e4SLinus Torvalds 			buf->f_files = sbinfo->max_inodes;
9961da177e4SLinus Torvalds 			buf->f_ffree = sbinfo->free_inodes;
99774a8a65cSDavid Gibson 		}
9981da177e4SLinus Torvalds 		spin_unlock(&sbinfo->stat_lock);
9991da177e4SLinus Torvalds 	}
10001da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
10011da177e4SLinus Torvalds 	return 0;
10021da177e4SLinus Torvalds }
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds static void hugetlbfs_put_super(struct super_block *sb)
10051da177e4SLinus Torvalds {
10061da177e4SLinus Torvalds 	struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
10071da177e4SLinus Torvalds 
10081da177e4SLinus Torvalds 	if (sbi) {
10091da177e4SLinus Torvalds 		sb->s_fs_info = NULL;
101090481622SDavid Gibson 
101190481622SDavid Gibson 		if (sbi->spool)
101290481622SDavid Gibson 			hugepage_put_subpool(sbi->spool);
101390481622SDavid Gibson 
10141da177e4SLinus Torvalds 		kfree(sbi);
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds }
10171da177e4SLinus Torvalds 
101896527980SChristoph Hellwig static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
101996527980SChristoph Hellwig {
102096527980SChristoph Hellwig 	if (sbinfo->free_inodes >= 0) {
102196527980SChristoph Hellwig 		spin_lock(&sbinfo->stat_lock);
102296527980SChristoph Hellwig 		if (unlikely(!sbinfo->free_inodes)) {
102396527980SChristoph Hellwig 			spin_unlock(&sbinfo->stat_lock);
102496527980SChristoph Hellwig 			return 0;
102596527980SChristoph Hellwig 		}
102696527980SChristoph Hellwig 		sbinfo->free_inodes--;
102796527980SChristoph Hellwig 		spin_unlock(&sbinfo->stat_lock);
102896527980SChristoph Hellwig 	}
102996527980SChristoph Hellwig 
103096527980SChristoph Hellwig 	return 1;
103196527980SChristoph Hellwig }
103296527980SChristoph Hellwig 
103396527980SChristoph Hellwig static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
103496527980SChristoph Hellwig {
103596527980SChristoph Hellwig 	if (sbinfo->free_inodes >= 0) {
103696527980SChristoph Hellwig 		spin_lock(&sbinfo->stat_lock);
103796527980SChristoph Hellwig 		sbinfo->free_inodes++;
103896527980SChristoph Hellwig 		spin_unlock(&sbinfo->stat_lock);
103996527980SChristoph Hellwig 	}
104096527980SChristoph Hellwig }
104196527980SChristoph Hellwig 
104296527980SChristoph Hellwig 
1043e18b890bSChristoph Lameter static struct kmem_cache *hugetlbfs_inode_cachep;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
10461da177e4SLinus Torvalds {
104796527980SChristoph Hellwig 	struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
10481da177e4SLinus Torvalds 	struct hugetlbfs_inode_info *p;
10491da177e4SLinus Torvalds 
105096527980SChristoph Hellwig 	if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
10511da177e4SLinus Torvalds 		return NULL;
1052e94b1766SChristoph Lameter 	p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL);
105396527980SChristoph Hellwig 	if (unlikely(!p)) {
105496527980SChristoph Hellwig 		hugetlbfs_inc_free_inodes(sbinfo);
105596527980SChristoph Hellwig 		return NULL;
10561da177e4SLinus Torvalds 	}
10574742a35dSMike Kravetz 
10584742a35dSMike Kravetz 	/*
10594742a35dSMike Kravetz 	 * Any time after allocation, hugetlbfs_destroy_inode can be called
10604742a35dSMike Kravetz 	 * for the inode.  mpol_free_shared_policy is unconditionally called
10614742a35dSMike Kravetz 	 * as part of hugetlbfs_destroy_inode.  So, initialize policy here
10624742a35dSMike Kravetz 	 * in case of a quick call to destroy.
10634742a35dSMike Kravetz 	 *
10644742a35dSMike Kravetz 	 * Note that the policy is initialized even if we are creating a
10654742a35dSMike Kravetz 	 * private inode.  This simplifies hugetlbfs_destroy_inode.
10664742a35dSMike Kravetz 	 */
10674742a35dSMike Kravetz 	mpol_shared_policy_init(&p->policy, NULL);
10684742a35dSMike Kravetz 
106996527980SChristoph Hellwig 	return &p->vfs_inode;
10701da177e4SLinus Torvalds }
10711da177e4SLinus Torvalds 
1072b62de322SAl Viro static void hugetlbfs_free_inode(struct inode *inode)
1073fa0d7e3dSNick Piggin {
1074fa0d7e3dSNick Piggin 	kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
1075fa0d7e3dSNick Piggin }
1076fa0d7e3dSNick Piggin 
10771da177e4SLinus Torvalds static void hugetlbfs_destroy_inode(struct inode *inode)
10781da177e4SLinus Torvalds {
107996527980SChristoph Hellwig 	hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
10801da177e4SLinus Torvalds 	mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
10811da177e4SLinus Torvalds }
10821da177e4SLinus Torvalds 
1083f5e54d6eSChristoph Hellwig static const struct address_space_operations hugetlbfs_aops = {
1084800d15a5SNick Piggin 	.write_begin	= hugetlbfs_write_begin,
1085800d15a5SNick Piggin 	.write_end	= hugetlbfs_write_end,
10861da177e4SLinus Torvalds 	.set_page_dirty	= hugetlbfs_set_page_dirty,
1087290408d4SNaoya Horiguchi 	.migratepage    = hugetlbfs_migrate_page,
108878bb9203SNaoya Horiguchi 	.error_remove_page	= hugetlbfs_error_remove_page,
10891da177e4SLinus Torvalds };
10901da177e4SLinus Torvalds 
109196527980SChristoph Hellwig 
109251cc5068SAlexey Dobriyan static void init_once(void *foo)
109396527980SChristoph Hellwig {
109496527980SChristoph Hellwig 	struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
109596527980SChristoph Hellwig 
109696527980SChristoph Hellwig 	inode_init_once(&ei->vfs_inode);
109796527980SChristoph Hellwig }
109896527980SChristoph Hellwig 
10994b6f5d20SArjan van de Ven const struct file_operations hugetlbfs_file_operations = {
110034d0640eSAl Viro 	.read_iter		= hugetlbfs_read_iter,
11011da177e4SLinus Torvalds 	.mmap			= hugetlbfs_file_mmap,
11021b061d92SChristoph Hellwig 	.fsync			= noop_fsync,
11031da177e4SLinus Torvalds 	.get_unmapped_area	= hugetlb_get_unmapped_area,
11046038f373SArnd Bergmann 	.llseek			= default_llseek,
110570c3547eSMike Kravetz 	.fallocate		= hugetlbfs_fallocate,
11061da177e4SLinus Torvalds };
11071da177e4SLinus Torvalds 
110892e1d5beSArjan van de Ven static const struct inode_operations hugetlbfs_dir_inode_operations = {
11091da177e4SLinus Torvalds 	.create		= hugetlbfs_create,
11101da177e4SLinus Torvalds 	.lookup		= simple_lookup,
11111da177e4SLinus Torvalds 	.link		= simple_link,
11121da177e4SLinus Torvalds 	.unlink		= simple_unlink,
11131da177e4SLinus Torvalds 	.symlink	= hugetlbfs_symlink,
11141da177e4SLinus Torvalds 	.mkdir		= hugetlbfs_mkdir,
11151da177e4SLinus Torvalds 	.rmdir		= simple_rmdir,
11161da177e4SLinus Torvalds 	.mknod		= hugetlbfs_mknod,
11171da177e4SLinus Torvalds 	.rename		= simple_rename,
11181da177e4SLinus Torvalds 	.setattr	= hugetlbfs_setattr,
11191ab5b82fSPiotr Sarna 	.tmpfile	= hugetlbfs_tmpfile,
11201da177e4SLinus Torvalds };
11211da177e4SLinus Torvalds 
112292e1d5beSArjan van de Ven static const struct inode_operations hugetlbfs_inode_operations = {
11231da177e4SLinus Torvalds 	.setattr	= hugetlbfs_setattr,
11241da177e4SLinus Torvalds };
11251da177e4SLinus Torvalds 
1126ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations hugetlbfs_ops = {
11271da177e4SLinus Torvalds 	.alloc_inode    = hugetlbfs_alloc_inode,
1128b62de322SAl Viro 	.free_inode     = hugetlbfs_free_inode,
11291da177e4SLinus Torvalds 	.destroy_inode  = hugetlbfs_destroy_inode,
11302bbbda30SAl Viro 	.evict_inode	= hugetlbfs_evict_inode,
11311da177e4SLinus Torvalds 	.statfs		= hugetlbfs_statfs,
11321da177e4SLinus Torvalds 	.put_super	= hugetlbfs_put_super,
11334a25220dSDavid Howells 	.show_options	= hugetlbfs_show_options,
11341da177e4SLinus Torvalds };
11351da177e4SLinus Torvalds 
11367ca02d0aSMike Kravetz /*
11377ca02d0aSMike Kravetz  * Convert size option passed from command line to number of huge pages
11387ca02d0aSMike Kravetz  * in the pool specified by hstate.  Size option could be in bytes
11397ca02d0aSMike Kravetz  * (val_type == SIZE_STD) or percentage of the pool (val_type == SIZE_PERCENT).
11407ca02d0aSMike Kravetz  */
11414a25220dSDavid Howells static long
11427ca02d0aSMike Kravetz hugetlbfs_size_to_hpages(struct hstate *h, unsigned long long size_opt,
11434a25220dSDavid Howells 			 enum hugetlbfs_size_type val_type)
11447ca02d0aSMike Kravetz {
11457ca02d0aSMike Kravetz 	if (val_type == NO_SIZE)
11467ca02d0aSMike Kravetz 		return -1;
11477ca02d0aSMike Kravetz 
11487ca02d0aSMike Kravetz 	if (val_type == SIZE_PERCENT) {
11497ca02d0aSMike Kravetz 		size_opt <<= huge_page_shift(h);
11507ca02d0aSMike Kravetz 		size_opt *= h->max_huge_pages;
11517ca02d0aSMike Kravetz 		do_div(size_opt, 100);
11527ca02d0aSMike Kravetz 	}
11537ca02d0aSMike Kravetz 
11547ca02d0aSMike Kravetz 	size_opt >>= huge_page_shift(h);
11557ca02d0aSMike Kravetz 	return size_opt;
11567ca02d0aSMike Kravetz }
11577ca02d0aSMike Kravetz 
115832021982SDavid Howells /*
115932021982SDavid Howells  * Parse one mount parameter.
116032021982SDavid Howells  */
116132021982SDavid Howells static int hugetlbfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
11621da177e4SLinus Torvalds {
116332021982SDavid Howells 	struct hugetlbfs_fs_context *ctx = fc->fs_private;
116432021982SDavid Howells 	struct fs_parse_result result;
116532021982SDavid Howells 	char *rest;
116632021982SDavid Howells 	unsigned long ps;
116732021982SDavid Howells 	int opt;
11681da177e4SLinus Torvalds 
1169d7167b14SAl Viro 	opt = fs_parse(fc, hugetlb_fs_parameters, param, &result);
117032021982SDavid Howells 	if (opt < 0)
117132021982SDavid Howells 		return opt;
117232021982SDavid Howells 
117332021982SDavid Howells 	switch (opt) {
117432021982SDavid Howells 	case Opt_uid:
117532021982SDavid Howells 		ctx->uid = make_kuid(current_user_ns(), result.uint_32);
117632021982SDavid Howells 		if (!uid_valid(ctx->uid))
117732021982SDavid Howells 			goto bad_val;
11781da177e4SLinus Torvalds 		return 0;
11791da177e4SLinus Torvalds 
1180e73a75faSRandy Dunlap 	case Opt_gid:
118132021982SDavid Howells 		ctx->gid = make_kgid(current_user_ns(), result.uint_32);
118232021982SDavid Howells 		if (!gid_valid(ctx->gid))
1183e73a75faSRandy Dunlap 			goto bad_val;
118432021982SDavid Howells 		return 0;
1185e73a75faSRandy Dunlap 
1186e73a75faSRandy Dunlap 	case Opt_mode:
118732021982SDavid Howells 		ctx->mode = result.uint_32 & 01777U;
118832021982SDavid Howells 		return 0;
1189e73a75faSRandy Dunlap 
119032021982SDavid Howells 	case Opt_size:
1191e73a75faSRandy Dunlap 		/* memparse() will accept a K/M/G without a digit */
119232021982SDavid Howells 		if (!isdigit(param->string[0]))
1193e73a75faSRandy Dunlap 			goto bad_val;
119432021982SDavid Howells 		ctx->max_size_opt = memparse(param->string, &rest);
119532021982SDavid Howells 		ctx->max_val_type = SIZE_STD;
1196a137e1ccSAndi Kleen 		if (*rest == '%')
119732021982SDavid Howells 			ctx->max_val_type = SIZE_PERCENT;
119832021982SDavid Howells 		return 0;
11991da177e4SLinus Torvalds 
1200e73a75faSRandy Dunlap 	case Opt_nr_inodes:
1201e73a75faSRandy Dunlap 		/* memparse() will accept a K/M/G without a digit */
120232021982SDavid Howells 		if (!isdigit(param->string[0]))
1203e73a75faSRandy Dunlap 			goto bad_val;
120432021982SDavid Howells 		ctx->nr_inodes = memparse(param->string, &rest);
120532021982SDavid Howells 		return 0;
1206e73a75faSRandy Dunlap 
120732021982SDavid Howells 	case Opt_pagesize:
120832021982SDavid Howells 		ps = memparse(param->string, &rest);
120932021982SDavid Howells 		ctx->hstate = size_to_hstate(ps);
121032021982SDavid Howells 		if (!ctx->hstate) {
121132021982SDavid Howells 			pr_err("Unsupported page size %lu MB\n", ps >> 20);
1212a137e1ccSAndi Kleen 			return -EINVAL;
1213a137e1ccSAndi Kleen 		}
121432021982SDavid Howells 		return 0;
1215a137e1ccSAndi Kleen 
121632021982SDavid Howells 	case Opt_min_size:
12177ca02d0aSMike Kravetz 		/* memparse() will accept a K/M/G without a digit */
121832021982SDavid Howells 		if (!isdigit(param->string[0]))
12197ca02d0aSMike Kravetz 			goto bad_val;
122032021982SDavid Howells 		ctx->min_size_opt = memparse(param->string, &rest);
122132021982SDavid Howells 		ctx->min_val_type = SIZE_STD;
12227ca02d0aSMike Kravetz 		if (*rest == '%')
122332021982SDavid Howells 			ctx->min_val_type = SIZE_PERCENT;
122432021982SDavid Howells 		return 0;
12257ca02d0aSMike Kravetz 
1226e73a75faSRandy Dunlap 	default:
1227b4c07bceSLee Schermerhorn 		return -EINVAL;
1228e73a75faSRandy Dunlap 	}
122932021982SDavid Howells 
123032021982SDavid Howells bad_val:
123132021982SDavid Howells 	return invalf(fc, "hugetlbfs: Bad value '%s' for mount option '%s'\n",
123232021982SDavid Howells 		      param->string, param->key);
12331da177e4SLinus Torvalds }
1234a137e1ccSAndi Kleen 
12357ca02d0aSMike Kravetz /*
123632021982SDavid Howells  * Validate the parsed options.
123732021982SDavid Howells  */
123832021982SDavid Howells static int hugetlbfs_validate(struct fs_context *fc)
123932021982SDavid Howells {
124032021982SDavid Howells 	struct hugetlbfs_fs_context *ctx = fc->fs_private;
124132021982SDavid Howells 
124232021982SDavid Howells 	/*
12437ca02d0aSMike Kravetz 	 * Use huge page pool size (in hstate) to convert the size
12447ca02d0aSMike Kravetz 	 * options to number of huge pages.  If NO_SIZE, -1 is returned.
12457ca02d0aSMike Kravetz 	 */
124632021982SDavid Howells 	ctx->max_hpages = hugetlbfs_size_to_hpages(ctx->hstate,
124732021982SDavid Howells 						   ctx->max_size_opt,
124832021982SDavid Howells 						   ctx->max_val_type);
124932021982SDavid Howells 	ctx->min_hpages = hugetlbfs_size_to_hpages(ctx->hstate,
125032021982SDavid Howells 						   ctx->min_size_opt,
125132021982SDavid Howells 						   ctx->min_val_type);
12527ca02d0aSMike Kravetz 
12537ca02d0aSMike Kravetz 	/*
12547ca02d0aSMike Kravetz 	 * If max_size was specified, then min_size must be smaller
12557ca02d0aSMike Kravetz 	 */
125632021982SDavid Howells 	if (ctx->max_val_type > NO_SIZE &&
125732021982SDavid Howells 	    ctx->min_hpages > ctx->max_hpages) {
125832021982SDavid Howells 		pr_err("Minimum size can not be greater than maximum size\n");
12597ca02d0aSMike Kravetz 		return -EINVAL;
1260a137e1ccSAndi Kleen 	}
1261a137e1ccSAndi Kleen 
12621da177e4SLinus Torvalds 	return 0;
12631da177e4SLinus Torvalds }
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds static int
126632021982SDavid Howells hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc)
12671da177e4SLinus Torvalds {
126832021982SDavid Howells 	struct hugetlbfs_fs_context *ctx = fc->fs_private;
12691da177e4SLinus Torvalds 	struct hugetlbfs_sb_info *sbinfo;
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 	sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
12721da177e4SLinus Torvalds 	if (!sbinfo)
12731da177e4SLinus Torvalds 		return -ENOMEM;
12741da177e4SLinus Torvalds 	sb->s_fs_info = sbinfo;
12751da177e4SLinus Torvalds 	spin_lock_init(&sbinfo->stat_lock);
127632021982SDavid Howells 	sbinfo->hstate		= ctx->hstate;
127732021982SDavid Howells 	sbinfo->max_inodes	= ctx->nr_inodes;
127832021982SDavid Howells 	sbinfo->free_inodes	= ctx->nr_inodes;
127990481622SDavid Gibson 	sbinfo->spool		= NULL;
128032021982SDavid Howells 	sbinfo->uid		= ctx->uid;
128132021982SDavid Howells 	sbinfo->gid		= ctx->gid;
128232021982SDavid Howells 	sbinfo->mode		= ctx->mode;
12834a25220dSDavid Howells 
12847ca02d0aSMike Kravetz 	/*
12857ca02d0aSMike Kravetz 	 * Allocate and initialize subpool if maximum or minimum size is
12867ca02d0aSMike Kravetz 	 * specified.  Any needed reservations (for minimim size) are taken
12877ca02d0aSMike Kravetz 	 * taken when the subpool is created.
12887ca02d0aSMike Kravetz 	 */
128932021982SDavid Howells 	if (ctx->max_hpages != -1 || ctx->min_hpages != -1) {
129032021982SDavid Howells 		sbinfo->spool = hugepage_new_subpool(ctx->hstate,
129132021982SDavid Howells 						     ctx->max_hpages,
129232021982SDavid Howells 						     ctx->min_hpages);
129390481622SDavid Gibson 		if (!sbinfo->spool)
129490481622SDavid Gibson 			goto out_free;
129590481622SDavid Gibson 	}
12961da177e4SLinus Torvalds 	sb->s_maxbytes = MAX_LFS_FILESIZE;
129732021982SDavid Howells 	sb->s_blocksize = huge_page_size(ctx->hstate);
129832021982SDavid Howells 	sb->s_blocksize_bits = huge_page_shift(ctx->hstate);
12991da177e4SLinus Torvalds 	sb->s_magic = HUGETLBFS_MAGIC;
13001da177e4SLinus Torvalds 	sb->s_op = &hugetlbfs_ops;
13011da177e4SLinus Torvalds 	sb->s_time_gran = 1;
130232021982SDavid Howells 	sb->s_root = d_make_root(hugetlbfs_get_root(sb, ctx));
130348fde701SAl Viro 	if (!sb->s_root)
13041da177e4SLinus Torvalds 		goto out_free;
13051da177e4SLinus Torvalds 	return 0;
13061da177e4SLinus Torvalds out_free:
130790481622SDavid Gibson 	kfree(sbinfo->spool);
13081da177e4SLinus Torvalds 	kfree(sbinfo);
13091da177e4SLinus Torvalds 	return -ENOMEM;
13101da177e4SLinus Torvalds }
13111da177e4SLinus Torvalds 
131232021982SDavid Howells static int hugetlbfs_get_tree(struct fs_context *fc)
13131da177e4SLinus Torvalds {
131432021982SDavid Howells 	int err = hugetlbfs_validate(fc);
131532021982SDavid Howells 	if (err)
131632021982SDavid Howells 		return err;
13172ac295d4SAl Viro 	return get_tree_nodev(fc, hugetlbfs_fill_super);
131832021982SDavid Howells }
131932021982SDavid Howells 
132032021982SDavid Howells static void hugetlbfs_fs_context_free(struct fs_context *fc)
132132021982SDavid Howells {
132232021982SDavid Howells 	kfree(fc->fs_private);
132332021982SDavid Howells }
132432021982SDavid Howells 
132532021982SDavid Howells static const struct fs_context_operations hugetlbfs_fs_context_ops = {
132632021982SDavid Howells 	.free		= hugetlbfs_fs_context_free,
132732021982SDavid Howells 	.parse_param	= hugetlbfs_parse_param,
132832021982SDavid Howells 	.get_tree	= hugetlbfs_get_tree,
132932021982SDavid Howells };
133032021982SDavid Howells 
133132021982SDavid Howells static int hugetlbfs_init_fs_context(struct fs_context *fc)
133232021982SDavid Howells {
133332021982SDavid Howells 	struct hugetlbfs_fs_context *ctx;
133432021982SDavid Howells 
133532021982SDavid Howells 	ctx = kzalloc(sizeof(struct hugetlbfs_fs_context), GFP_KERNEL);
133632021982SDavid Howells 	if (!ctx)
133732021982SDavid Howells 		return -ENOMEM;
133832021982SDavid Howells 
133932021982SDavid Howells 	ctx->max_hpages	= -1; /* No limit on size by default */
134032021982SDavid Howells 	ctx->nr_inodes	= -1; /* No limit on number of inodes by default */
134132021982SDavid Howells 	ctx->uid	= current_fsuid();
134232021982SDavid Howells 	ctx->gid	= current_fsgid();
134332021982SDavid Howells 	ctx->mode	= 0755;
134432021982SDavid Howells 	ctx->hstate	= &default_hstate;
134532021982SDavid Howells 	ctx->min_hpages	= -1; /* No default minimum size */
134632021982SDavid Howells 	ctx->max_val_type = NO_SIZE;
134732021982SDavid Howells 	ctx->min_val_type = NO_SIZE;
134832021982SDavid Howells 	fc->fs_private = ctx;
134932021982SDavid Howells 	fc->ops	= &hugetlbfs_fs_context_ops;
135032021982SDavid Howells 	return 0;
13511da177e4SLinus Torvalds }
13521da177e4SLinus Torvalds 
13531da177e4SLinus Torvalds static struct file_system_type hugetlbfs_fs_type = {
13541da177e4SLinus Torvalds 	.name			= "hugetlbfs",
135532021982SDavid Howells 	.init_fs_context	= hugetlbfs_init_fs_context,
1356d7167b14SAl Viro 	.parameters		= hugetlb_fs_parameters,
13571da177e4SLinus Torvalds 	.kill_sb		= kill_litter_super,
13581da177e4SLinus Torvalds };
13591da177e4SLinus Torvalds 
136042d7395fSAndi Kleen static struct vfsmount *hugetlbfs_vfsmount[HUGE_MAX_HSTATE];
13611da177e4SLinus Torvalds 
1362ef1ff6b8SFrom: Mel Gorman static int can_do_hugetlb_shm(void)
13631da177e4SLinus Torvalds {
1364a0eb3a05SEric W. Biederman 	kgid_t shm_group;
1365a0eb3a05SEric W. Biederman 	shm_group = make_kgid(&init_user_ns, sysctl_hugetlb_shm_group);
1366a0eb3a05SEric W. Biederman 	return capable(CAP_IPC_LOCK) || in_group_p(shm_group);
13671da177e4SLinus Torvalds }
13681da177e4SLinus Torvalds 
136942d7395fSAndi Kleen static int get_hstate_idx(int page_size_log)
137042d7395fSAndi Kleen {
1371af73e4d9SNaoya Horiguchi 	struct hstate *h = hstate_sizelog(page_size_log);
137242d7395fSAndi Kleen 
137342d7395fSAndi Kleen 	if (!h)
137442d7395fSAndi Kleen 		return -1;
137542d7395fSAndi Kleen 	return h - hstates;
137642d7395fSAndi Kleen }
137742d7395fSAndi Kleen 
1378af73e4d9SNaoya Horiguchi /*
1379af73e4d9SNaoya Horiguchi  * Note that size should be aligned to proper hugepage size in caller side,
1380af73e4d9SNaoya Horiguchi  * otherwise hugetlb_reserve_pages reserves one less hugepages than intended.
1381af73e4d9SNaoya Horiguchi  */
1382af73e4d9SNaoya Horiguchi struct file *hugetlb_file_setup(const char *name, size_t size,
1383af73e4d9SNaoya Horiguchi 				vm_flags_t acctflag, struct user_struct **user,
138442d7395fSAndi Kleen 				int creat_flags, int page_size_log)
13851da177e4SLinus Torvalds {
13861da177e4SLinus Torvalds 	struct inode *inode;
1387e68375c8SAl Viro 	struct vfsmount *mnt;
138842d7395fSAndi Kleen 	int hstate_idx;
1389e68375c8SAl Viro 	struct file *file;
139042d7395fSAndi Kleen 
139142d7395fSAndi Kleen 	hstate_idx = get_hstate_idx(page_size_log);
139242d7395fSAndi Kleen 	if (hstate_idx < 0)
139342d7395fSAndi Kleen 		return ERR_PTR(-ENODEV);
13941da177e4SLinus Torvalds 
1395353d5c30SHugh Dickins 	*user = NULL;
1396e68375c8SAl Viro 	mnt = hugetlbfs_vfsmount[hstate_idx];
1397e68375c8SAl Viro 	if (!mnt)
13985bc98594SAkinobu Mita 		return ERR_PTR(-ENOENT);
13995bc98594SAkinobu Mita 
1400ef1ff6b8SFrom: Mel Gorman 	if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
1401353d5c30SHugh Dickins 		*user = current_user();
1402353d5c30SHugh Dickins 		if (user_shm_lock(size, *user)) {
140321a3c273SDavid Rientjes 			task_lock(current);
14049b857d26SAndrew Morton 			pr_warn_once("%s (%d): Using mlock ulimits for SHM_HUGETLB is deprecated\n",
140521a3c273SDavid Rientjes 				current->comm, current->pid);
140621a3c273SDavid Rientjes 			task_unlock(current);
1407353d5c30SHugh Dickins 		} else {
1408353d5c30SHugh Dickins 			*user = NULL;
14091da177e4SLinus Torvalds 			return ERR_PTR(-EPERM);
14102584e517SRavikiran G Thirumalai 		}
1411353d5c30SHugh Dickins 	}
14121da177e4SLinus Torvalds 
141339b65252SAnatol Pomozov 	file = ERR_PTR(-ENOSPC);
1414e68375c8SAl Viro 	inode = hugetlbfs_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0);
14151da177e4SLinus Torvalds 	if (!inode)
1416e68375c8SAl Viro 		goto out;
1417e1832f29SStephen Smalley 	if (creat_flags == HUGETLB_SHMFS_INODE)
1418e1832f29SStephen Smalley 		inode->i_flags |= S_PRIVATE;
14191da177e4SLinus Torvalds 
14201da177e4SLinus Torvalds 	inode->i_size = size;
14216d6b77f1SMiklos Szeredi 	clear_nlink(inode);
1422ce8d2cdfSDave Hansen 
1423e68375c8SAl Viro 	if (hugetlb_reserve_pages(inode, 0,
1424e68375c8SAl Viro 			size >> huge_page_shift(hstate_inode(inode)), NULL,
1425e68375c8SAl Viro 			acctflag))
1426e68375c8SAl Viro 		file = ERR_PTR(-ENOMEM);
1427e68375c8SAl Viro 	else
1428e68375c8SAl Viro 		file = alloc_file_pseudo(inode, mnt, name, O_RDWR,
1429ce8d2cdfSDave Hansen 					&hugetlbfs_file_operations);
1430e68375c8SAl Viro 	if (!IS_ERR(file))
14311da177e4SLinus Torvalds 		return file;
14321da177e4SLinus Torvalds 
1433b45b5bd6SDavid Gibson 	iput(inode);
1434e68375c8SAl Viro out:
1435353d5c30SHugh Dickins 	if (*user) {
1436353d5c30SHugh Dickins 		user_shm_unlock(size, *user);
1437353d5c30SHugh Dickins 		*user = NULL;
1438353d5c30SHugh Dickins 	}
143939b65252SAnatol Pomozov 	return file;
14401da177e4SLinus Torvalds }
14411da177e4SLinus Torvalds 
144232021982SDavid Howells static struct vfsmount *__init mount_one_hugetlbfs(struct hstate *h)
144332021982SDavid Howells {
144432021982SDavid Howells 	struct fs_context *fc;
144532021982SDavid Howells 	struct vfsmount *mnt;
144632021982SDavid Howells 
144732021982SDavid Howells 	fc = fs_context_for_mount(&hugetlbfs_fs_type, SB_KERNMOUNT);
144832021982SDavid Howells 	if (IS_ERR(fc)) {
144932021982SDavid Howells 		mnt = ERR_CAST(fc);
145032021982SDavid Howells 	} else {
145132021982SDavid Howells 		struct hugetlbfs_fs_context *ctx = fc->fs_private;
145232021982SDavid Howells 		ctx->hstate = h;
145332021982SDavid Howells 		mnt = fc_mount(fc);
145432021982SDavid Howells 		put_fs_context(fc);
145532021982SDavid Howells 	}
145632021982SDavid Howells 	if (IS_ERR(mnt))
145732021982SDavid Howells 		pr_err("Cannot mount internal hugetlbfs for page size %uK",
145832021982SDavid Howells 		       1U << (h->order + PAGE_SHIFT - 10));
145932021982SDavid Howells 	return mnt;
146032021982SDavid Howells }
146132021982SDavid Howells 
14621da177e4SLinus Torvalds static int __init init_hugetlbfs_fs(void)
14631da177e4SLinus Torvalds {
146432021982SDavid Howells 	struct vfsmount *mnt;
146542d7395fSAndi Kleen 	struct hstate *h;
14661da177e4SLinus Torvalds 	int error;
146742d7395fSAndi Kleen 	int i;
14681da177e4SLinus Torvalds 
1469457c1b27SNishanth Aravamudan 	if (!hugepages_supported()) {
14709b857d26SAndrew Morton 		pr_info("disabling because there are no supported hugepage sizes\n");
1471457c1b27SNishanth Aravamudan 		return -ENOTSUPP;
1472457c1b27SNishanth Aravamudan 	}
1473457c1b27SNishanth Aravamudan 
1474d1d5e05fSHillf Danton 	error = -ENOMEM;
14751da177e4SLinus Torvalds 	hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
14761da177e4SLinus Torvalds 					sizeof(struct hugetlbfs_inode_info),
14775d097056SVladimir Davydov 					0, SLAB_ACCOUNT, init_once);
14781da177e4SLinus Torvalds 	if (hugetlbfs_inode_cachep == NULL)
14798fc312b3SMike Kravetz 		goto out;
14801da177e4SLinus Torvalds 
14811da177e4SLinus Torvalds 	error = register_filesystem(&hugetlbfs_fs_type);
14821da177e4SLinus Torvalds 	if (error)
14838fc312b3SMike Kravetz 		goto out_free;
14841da177e4SLinus Torvalds 
14858fc312b3SMike Kravetz 	/* default hstate mount is required */
14868fc312b3SMike Kravetz 	mnt = mount_one_hugetlbfs(&hstates[default_hstate_idx]);
14878fc312b3SMike Kravetz 	if (IS_ERR(mnt)) {
14888fc312b3SMike Kravetz 		error = PTR_ERR(mnt);
14898fc312b3SMike Kravetz 		goto out_unreg;
14908fc312b3SMike Kravetz 	}
14918fc312b3SMike Kravetz 	hugetlbfs_vfsmount[default_hstate_idx] = mnt;
14928fc312b3SMike Kravetz 
14938fc312b3SMike Kravetz 	/* other hstates are optional */
149442d7395fSAndi Kleen 	i = 0;
149542d7395fSAndi Kleen 	for_each_hstate(h) {
149615f0ec94SJan Stancek 		if (i == default_hstate_idx) {
149715f0ec94SJan Stancek 			i++;
14988fc312b3SMike Kravetz 			continue;
149915f0ec94SJan Stancek 		}
15008fc312b3SMike Kravetz 
150132021982SDavid Howells 		mnt = mount_one_hugetlbfs(h);
15028fc312b3SMike Kravetz 		if (IS_ERR(mnt))
15038fc312b3SMike Kravetz 			hugetlbfs_vfsmount[i] = NULL;
15048fc312b3SMike Kravetz 		else
150532021982SDavid Howells 			hugetlbfs_vfsmount[i] = mnt;
150642d7395fSAndi Kleen 		i++;
150742d7395fSAndi Kleen 	}
150832021982SDavid Howells 
150942d7395fSAndi Kleen 	return 0;
15101da177e4SLinus Torvalds 
15118fc312b3SMike Kravetz  out_unreg:
15128fc312b3SMike Kravetz 	(void)unregister_filesystem(&hugetlbfs_fs_type);
15138fc312b3SMike Kravetz  out_free:
15141da177e4SLinus Torvalds 	kmem_cache_destroy(hugetlbfs_inode_cachep);
15158fc312b3SMike Kravetz  out:
15161da177e4SLinus Torvalds 	return error;
15171da177e4SLinus Torvalds }
15183e89e1c5SPaul Gortmaker fs_initcall(init_hugetlbfs_fs)
1519