blob: 09ee07f026630501869eb0bd53131e46b7f8d82d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * hugetlbpage-backed filesystem. Based on ramfs.
3 *
4 * William Irwin, 2002
5 *
6 * Copyright (C) 2002 Linus Torvalds.
7 */
8
9#include <linux/module.h>
10#include <linux/thread_info.h>
11#include <asm/current.h>
12#include <linux/sched.h> /* remove ASAP */
13#include <linux/fs.h>
14#include <linux/mount.h>
15#include <linux/file.h>
Randy Dunlape73a75f2007-07-15 23:40:52 -070016#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/writeback.h>
18#include <linux/pagemap.h>
19#include <linux/highmem.h>
20#include <linux/init.h>
21#include <linux/string.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080022#include <linux/capability.h>
Randy Dunlape73a75f2007-07-15 23:40:52 -070023#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/backing-dev.h>
25#include <linux/hugetlb.h>
26#include <linux/pagevec.h>
Randy Dunlape73a75f2007-07-15 23:40:52 -070027#include <linux/parser.h>
Benjamin Herrenschmidt036e0852007-05-06 14:50:12 -070028#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/quotaops.h>
30#include <linux/slab.h>
31#include <linux/dnotify.h>
32#include <linux/statfs.h>
33#include <linux/security.h>
34
35#include <asm/uaccess.h>
36
37/* some random number */
38#define HUGETLBFS_MAGIC 0x958458f6
39
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080040static const struct super_operations hugetlbfs_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -070041static const struct address_space_operations hugetlbfs_aops;
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080042const struct file_operations hugetlbfs_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080043static const struct inode_operations hugetlbfs_dir_inode_operations;
44static const struct inode_operations hugetlbfs_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46static struct backing_dev_info hugetlbfs_backing_dev_info = {
47 .ra_pages = 0, /* No readahead */
48 .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
49};
50
51int sysctl_hugetlb_shm_group;
52
Randy Dunlape73a75f2007-07-15 23:40:52 -070053enum {
54 Opt_size, Opt_nr_inodes,
55 Opt_mode, Opt_uid, Opt_gid,
56 Opt_err,
57};
58
59static match_table_t tokens = {
60 {Opt_size, "size=%s"},
61 {Opt_nr_inodes, "nr_inodes=%s"},
62 {Opt_mode, "mode=%o"},
63 {Opt_uid, "uid=%u"},
64 {Opt_gid, "gid=%u"},
65 {Opt_err, NULL},
66};
67
Adam Litke2e9b367c2005-10-29 18:16:47 -070068static void huge_pagevec_release(struct pagevec *pvec)
69{
70 int i;
71
72 for (i = 0; i < pagevec_count(pvec); ++i)
73 put_page(pvec->pages[i]);
74
75 pagevec_reinit(pvec);
76}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
79{
Josef Sipekb39424e2006-12-08 02:37:07 -080080 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 loff_t len, vma_len;
82 int ret;
83
Hugh Dickins68589bc2006-11-14 02:03:32 -080084 /*
David Gibsondec4ad82007-08-30 23:56:40 -070085 * vma address alignment (but not the pgoff alignment) has
86 * already been checked by prepare_hugepage_range. If you add
87 * any error returns here, do so after setting VM_HUGETLB, so
88 * is_vm_hugetlb_page tests below unmap_region go the right
89 * way when do_mmap_pgoff unwinds (may be important on powerpc
90 * and ia64).
Hugh Dickins68589bc2006-11-14 02:03:32 -080091 */
92 vma->vm_flags |= VM_HUGETLB | VM_RESERVED;
93 vma->vm_ops = &hugetlb_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
David Gibsondec4ad82007-08-30 23:56:40 -070095 if (vma->vm_pgoff & ~(HPAGE_MASK >> PAGE_SHIFT))
96 return -EINVAL;
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 vma_len = (loff_t)(vma->vm_end - vma->vm_start);
99
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800100 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 file_accessed(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 ret = -ENOMEM;
104 len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700106 if (vma->vm_flags & VM_MAYSHARE &&
107 hugetlb_reserve_pages(inode, vma->vm_pgoff >> (HPAGE_SHIFT-PAGE_SHIFT),
108 len >> HPAGE_SHIFT))
109 goto out;
David Gibsonb45b5bd2006-03-22 00:08:55 -0800110
Adam Litke4c887262005-10-29 18:16:46 -0700111 ret = 0;
112 hugetlb_prefault_arch_hook(vma->vm_mm);
Zhang, Yanminb6174df2006-07-10 04:44:49 -0700113 if (vma->vm_flags & VM_WRITE && inode->i_size < len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 inode->i_size = len;
115out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800116 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 return ret;
119}
120
121/*
Hugh Dickins508034a2005-10-29 18:16:30 -0700122 * Called under down_write(mmap_sem).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
124
Adrian Bunkd2ba27e82007-05-06 14:49:00 -0700125#ifndef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static unsigned long
127hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
128 unsigned long len, unsigned long pgoff, unsigned long flags)
129{
130 struct mm_struct *mm = current->mm;
131 struct vm_area_struct *vma;
132 unsigned long start_addr;
133
134 if (len & ~HPAGE_MASK)
135 return -EINVAL;
136 if (len > TASK_SIZE)
137 return -ENOMEM;
138
Benjamin Herrenschmidt036e0852007-05-06 14:50:12 -0700139 if (flags & MAP_FIXED) {
David Gibsondec4ad82007-08-30 23:56:40 -0700140 if (prepare_hugepage_range(addr, len))
Benjamin Herrenschmidt036e0852007-05-06 14:50:12 -0700141 return -EINVAL;
142 return addr;
143 }
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (addr) {
146 addr = ALIGN(addr, HPAGE_SIZE);
147 vma = find_vma(mm, addr);
148 if (TASK_SIZE - len >= addr &&
149 (!vma || addr + len <= vma->vm_start))
150 return addr;
151 }
152
153 start_addr = mm->free_area_cache;
154
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700155 if (len <= mm->cached_hole_size)
156 start_addr = TASK_UNMAPPED_BASE;
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158full_search:
159 addr = ALIGN(start_addr, HPAGE_SIZE);
160
161 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
162 /* At this point: (!vma || addr < vma->vm_end). */
163 if (TASK_SIZE - len < addr) {
164 /*
165 * Start a new search - just in case we missed
166 * some holes.
167 */
168 if (start_addr != TASK_UNMAPPED_BASE) {
169 start_addr = TASK_UNMAPPED_BASE;
170 goto full_search;
171 }
172 return -ENOMEM;
173 }
174
175 if (!vma || addr + len <= vma->vm_start)
176 return addr;
177 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
178 }
179}
180#endif
181
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700182static int
183hugetlbfs_read_actor(struct page *page, unsigned long offset,
184 char __user *buf, unsigned long count,
185 unsigned long size)
186{
187 char *kaddr;
188 unsigned long left, copied = 0;
189 int i, chunksize;
190
191 if (size > count)
192 size = count;
193
194 /* Find which 4k chunk and offset with in that chunk */
195 i = offset >> PAGE_CACHE_SHIFT;
196 offset = offset & ~PAGE_CACHE_MASK;
197
198 while (size) {
199 chunksize = PAGE_CACHE_SIZE;
200 if (offset)
201 chunksize -= offset;
202 if (chunksize > size)
203 chunksize = size;
204 kaddr = kmap(&page[i]);
205 left = __copy_to_user(buf, kaddr + offset, chunksize);
206 kunmap(&page[i]);
207 if (left) {
208 copied += (chunksize - left);
209 break;
210 }
211 offset = 0;
212 size -= chunksize;
213 buf += chunksize;
214 copied += chunksize;
215 i++;
216 }
217 return copied ? copied : -EFAULT;
218}
219
220/*
221 * Support for read() - Find the page attached to f_mapping and copy out the
222 * data. Its *very* similar to do_generic_mapping_read(), we can't use that
223 * since it has PAGE_CACHE_SIZE assumptions.
224 */
225static ssize_t hugetlbfs_read(struct file *filp, char __user *buf,
226 size_t len, loff_t *ppos)
227{
228 struct address_space *mapping = filp->f_mapping;
229 struct inode *inode = mapping->host;
230 unsigned long index = *ppos >> HPAGE_SHIFT;
231 unsigned long offset = *ppos & ~HPAGE_MASK;
232 unsigned long end_index;
233 loff_t isize;
234 ssize_t retval = 0;
235
236 mutex_lock(&inode->i_mutex);
237
238 /* validate length */
239 if (len == 0)
240 goto out;
241
242 isize = i_size_read(inode);
243 if (!isize)
244 goto out;
245
246 end_index = (isize - 1) >> HPAGE_SHIFT;
247 for (;;) {
248 struct page *page;
249 int nr, ret;
250
251 /* nr is the maximum number of bytes to copy from this page */
252 nr = HPAGE_SIZE;
253 if (index >= end_index) {
254 if (index > end_index)
255 goto out;
256 nr = ((isize - 1) & ~HPAGE_MASK) + 1;
257 if (nr <= offset) {
258 goto out;
259 }
260 }
261 nr = nr - offset;
262
263 /* Find the page */
264 page = find_get_page(mapping, index);
265 if (unlikely(page == NULL)) {
266 /*
267 * We have a HOLE, zero out the user-buffer for the
268 * length of the hole or request.
269 */
270 ret = len < nr ? len : nr;
271 if (clear_user(buf, ret))
272 ret = -EFAULT;
273 } else {
274 /*
275 * We have the page, copy it to user space buffer.
276 */
277 ret = hugetlbfs_read_actor(page, offset, buf, len, nr);
278 }
279 if (ret < 0) {
280 if (retval == 0)
281 retval = ret;
282 if (page)
283 page_cache_release(page);
284 goto out;
285 }
286
287 offset += ret;
288 retval += ret;
289 len -= ret;
290 index += offset >> HPAGE_SHIFT;
291 offset &= ~HPAGE_MASK;
292
293 if (page)
294 page_cache_release(page);
295
296 /* short read or no more work */
297 if ((ret != nr) || (len == 0))
298 break;
299 }
300out:
301 *ppos = ((loff_t)index << HPAGE_SHIFT) + offset;
302 mutex_unlock(&inode->i_mutex);
303 return retval;
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/*
307 * Read a page. Again trivial. If it didn't already exist
308 * in the page cache, it is zero-filled.
309 */
310static int hugetlbfs_readpage(struct file *file, struct page * page)
311{
312 unlock_page(page);
313 return -EINVAL;
314}
315
Nick Piggin800d15a2007-10-16 01:25:03 -0700316static int hugetlbfs_write_begin(struct file *file,
317 struct address_space *mapping,
318 loff_t pos, unsigned len, unsigned flags,
319 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 return -EINVAL;
322}
323
Nick Piggin800d15a2007-10-16 01:25:03 -0700324static int hugetlbfs_write_end(struct file *file, struct address_space *mapping,
325 loff_t pos, unsigned len, unsigned copied,
326 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Nick Piggin800d15a2007-10-16 01:25:03 -0700328 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return -EINVAL;
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static void truncate_huge_page(struct page *page)
333{
Linus Torvaldsfba25912006-12-20 13:46:42 -0800334 cancel_dirty_page(page, /* No IO accounting for huge pages? */0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 ClearPageUptodate(page);
336 remove_from_page_cache(page);
337 put_page(page);
338}
339
David Gibsonb45b5bd2006-03-22 00:08:55 -0800340static void truncate_hugepages(struct inode *inode, loff_t lstart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
David Gibsonb45b5bd2006-03-22 00:08:55 -0800342 struct address_space *mapping = &inode->i_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 const pgoff_t start = lstart >> HPAGE_SHIFT;
344 struct pagevec pvec;
345 pgoff_t next;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700346 int i, freed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 pagevec_init(&pvec, 0);
349 next = start;
350 while (1) {
351 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
352 if (next == start)
353 break;
354 next = start;
355 continue;
356 }
357
358 for (i = 0; i < pagevec_count(&pvec); ++i) {
359 struct page *page = pvec.pages[i];
360
361 lock_page(page);
362 if (page->index > next)
363 next = page->index;
364 ++next;
365 truncate_huge_page(page);
366 unlock_page(page);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700367 freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 huge_pagevec_release(&pvec);
370 }
371 BUG_ON(!lstart && mapping->nrpages);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700372 hugetlb_unreserve_pages(inode, start, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375static void hugetlbfs_delete_inode(struct inode *inode)
376{
David Gibsonb45b5bd2006-03-22 00:08:55 -0800377 truncate_hugepages(inode, 0);
Christoph Hellwig149f4212005-10-29 18:16:43 -0700378 clear_inode(inode);
379}
380
Josh Triplettddc0a512006-09-29 01:59:27 -0700381static void hugetlbfs_forget_inode(struct inode *inode) __releases(inode_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Christoph Hellwig0b1533f2005-10-29 18:16:45 -0700383 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Christoph Hellwig0b1533f2005-10-29 18:16:45 -0700385 if (!hlist_unhashed(&inode->i_hash)) {
Joern Engel1c0eeaf2007-10-16 23:30:44 -0700386 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
Christoph Hellwig0b1533f2005-10-29 18:16:45 -0700387 list_move(&inode->i_list, &inode_unused);
388 inodes_stat.nr_unused++;
389 if (!sb || (sb->s_flags & MS_ACTIVE)) {
390 spin_unlock(&inode_lock);
391 return;
392 }
393 inode->i_state |= I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 spin_unlock(&inode_lock);
Christoph Hellwig0b1533f2005-10-29 18:16:45 -0700395 /*
396 * write_inode_now is a noop as we set BDI_CAP_NO_WRITEBACK
397 * in our backing_dev_info.
398 */
399 write_inode_now(inode, 1);
400 spin_lock(&inode_lock);
401 inode->i_state &= ~I_WILL_FREE;
402 inodes_stat.nr_unused--;
403 hlist_del_init(&inode->i_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 list_del_init(&inode->i_list);
406 list_del_init(&inode->i_sb_list);
407 inode->i_state |= I_FREEING;
408 inodes_stat.nr_inodes--;
409 spin_unlock(&inode_lock);
David Gibsonb45b5bd2006-03-22 00:08:55 -0800410 truncate_hugepages(inode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 clear_inode(inode);
412 destroy_inode(inode);
413}
414
415static void hugetlbfs_drop_inode(struct inode *inode)
416{
417 if (!inode->i_nlink)
Christoph Hellwig6b09b9d2005-10-29 18:16:44 -0700418 generic_delete_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 else
420 hugetlbfs_forget_inode(inode);
421}
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423static inline void
Hugh Dickins856fc292006-10-28 10:38:43 -0700424hugetlb_vmtruncate_list(struct prio_tree_root *root, pgoff_t pgoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 struct vm_area_struct *vma;
427 struct prio_tree_iter iter;
428
Hugh Dickins856fc292006-10-28 10:38:43 -0700429 vma_prio_tree_foreach(vma, &iter, root, pgoff, ULONG_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 unsigned long v_offset;
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 /*
Hugh Dickins856fc292006-10-28 10:38:43 -0700433 * Can the expression below overflow on 32-bit arches?
434 * No, because the prio_tree returns us only those vmas
435 * which overlap the truncated area starting at pgoff,
436 * and no vma on a 32-bit arch can span beyond the 4GB.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 */
Hugh Dickins856fc292006-10-28 10:38:43 -0700438 if (vma->vm_pgoff < pgoff)
439 v_offset = (pgoff - vma->vm_pgoff) << PAGE_SHIFT;
440 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 v_offset = 0;
442
Chen, Kenneth W502717f2006-10-11 01:20:46 -0700443 __unmap_hugepage_range(vma,
Hugh Dickins508034a2005-10-29 18:16:30 -0700444 vma->vm_start + v_offset, vma->vm_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
449{
Hugh Dickins856fc292006-10-28 10:38:43 -0700450 pgoff_t pgoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 struct address_space *mapping = inode->i_mapping;
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 BUG_ON(offset & ~HPAGE_MASK);
Hugh Dickins856fc292006-10-28 10:38:43 -0700454 pgoff = offset >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Ken Chen7aa91e12007-10-16 01:26:21 -0700456 i_size_write(inode, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 spin_lock(&mapping->i_mmap_lock);
458 if (!prio_tree_empty(&mapping->i_mmap))
459 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
460 spin_unlock(&mapping->i_mmap_lock);
David Gibsonb45b5bd2006-03-22 00:08:55 -0800461 truncate_hugepages(inode, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return 0;
463}
464
465static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
466{
467 struct inode *inode = dentry->d_inode;
468 int error;
469 unsigned int ia_valid = attr->ia_valid;
470
471 BUG_ON(!inode);
472
473 error = inode_change_ok(inode, attr);
474 if (error)
475 goto out;
476
477 if (ia_valid & ATTR_SIZE) {
478 error = -EINVAL;
479 if (!(attr->ia_size & ~HPAGE_MASK))
480 error = hugetlb_vmtruncate(inode, attr->ia_size);
481 if (error)
482 goto out;
483 attr->ia_valid &= ~ATTR_SIZE;
484 }
485 error = inode_setattr(inode, attr);
486out:
487 return error;
488}
489
490static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
491 gid_t gid, int mode, dev_t dev)
492{
493 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 inode = new_inode(sb);
496 if (inode) {
497 struct hugetlbfs_inode_info *info;
498 inode->i_mode = mode;
499 inode->i_uid = uid;
500 inode->i_gid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 inode->i_blocks = 0;
502 inode->i_mapping->a_ops = &hugetlbfs_aops;
503 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
504 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700505 INIT_LIST_HEAD(&inode->i_mapping->private_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 info = HUGETLBFS_I(inode);
Robin Holt7339ff82006-01-14 13:20:48 -0800507 mpol_shared_policy_init(&info->policy, MPOL_DEFAULT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 switch (mode & S_IFMT) {
509 default:
510 init_special_inode(inode, mode, dev);
511 break;
512 case S_IFREG:
513 inode->i_op = &hugetlbfs_inode_operations;
514 inode->i_fop = &hugetlbfs_file_operations;
515 break;
516 case S_IFDIR:
517 inode->i_op = &hugetlbfs_dir_inode_operations;
518 inode->i_fop = &simple_dir_operations;
519
520 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700521 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 break;
523 case S_IFLNK:
524 inode->i_op = &page_symlink_inode_operations;
525 break;
526 }
527 }
528 return inode;
529}
530
531/*
532 * File creation. Allocate an inode, and we're done..
533 */
534static int hugetlbfs_mknod(struct inode *dir,
535 struct dentry *dentry, int mode, dev_t dev)
536{
537 struct inode *inode;
538 int error = -ENOSPC;
539 gid_t gid;
540
541 if (dir->i_mode & S_ISGID) {
542 gid = dir->i_gid;
543 if (S_ISDIR(mode))
544 mode |= S_ISGID;
545 } else {
546 gid = current->fsgid;
547 }
548 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
549 if (inode) {
550 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
551 d_instantiate(dentry, inode);
552 dget(dentry); /* Extra count - pin the dentry in core */
553 error = 0;
554 }
555 return error;
556}
557
558static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
559{
560 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
561 if (!retval)
Dave Hansend8c76e62006-09-30 23:29:04 -0700562 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return retval;
564}
565
566static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
567{
568 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
569}
570
571static int hugetlbfs_symlink(struct inode *dir,
572 struct dentry *dentry, const char *symname)
573{
574 struct inode *inode;
575 int error = -ENOSPC;
576 gid_t gid;
577
578 if (dir->i_mode & S_ISGID)
579 gid = dir->i_gid;
580 else
581 gid = current->fsgid;
582
583 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
584 gid, S_IFLNK|S_IRWXUGO, 0);
585 if (inode) {
586 int l = strlen(symname)+1;
587 error = page_symlink(inode, symname, l);
588 if (!error) {
589 d_instantiate(dentry, inode);
590 dget(dentry);
591 } else
592 iput(inode);
593 }
594 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
595
596 return error;
597}
598
599/*
Ken Chen6649a382007-02-08 14:20:27 -0800600 * mark the head page dirty
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
602static int hugetlbfs_set_page_dirty(struct page *page)
603{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700604 struct page *head = compound_head(page);
Ken Chen6649a382007-02-08 14:20:27 -0800605
606 SetPageDirty(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return 0;
608}
609
David Howells726c3342006-06-23 02:02:58 -0700610static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
David Howells726c3342006-06-23 02:02:58 -0700612 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 buf->f_type = HUGETLBFS_MAGIC;
615 buf->f_bsize = HPAGE_SIZE;
616 if (sbinfo) {
617 spin_lock(&sbinfo->stat_lock);
David Gibson74a8a652005-11-21 21:32:24 -0800618 /* If no limits set, just report 0 for max/free/used
619 * blocks, like simple_statfs() */
620 if (sbinfo->max_blocks >= 0) {
621 buf->f_blocks = sbinfo->max_blocks;
622 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
623 buf->f_files = sbinfo->max_inodes;
624 buf->f_ffree = sbinfo->free_inodes;
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 spin_unlock(&sbinfo->stat_lock);
627 }
628 buf->f_namelen = NAME_MAX;
629 return 0;
630}
631
632static void hugetlbfs_put_super(struct super_block *sb)
633{
634 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
635
636 if (sbi) {
637 sb->s_fs_info = NULL;
638 kfree(sbi);
639 }
640}
641
Christoph Hellwig96527982005-10-29 18:16:42 -0700642static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
643{
644 if (sbinfo->free_inodes >= 0) {
645 spin_lock(&sbinfo->stat_lock);
646 if (unlikely(!sbinfo->free_inodes)) {
647 spin_unlock(&sbinfo->stat_lock);
648 return 0;
649 }
650 sbinfo->free_inodes--;
651 spin_unlock(&sbinfo->stat_lock);
652 }
653
654 return 1;
655}
656
657static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
658{
659 if (sbinfo->free_inodes >= 0) {
660 spin_lock(&sbinfo->stat_lock);
661 sbinfo->free_inodes++;
662 spin_unlock(&sbinfo->stat_lock);
663 }
664}
665
666
Christoph Lametere18b8902006-12-06 20:33:20 -0800667static struct kmem_cache *hugetlbfs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
670{
Christoph Hellwig96527982005-10-29 18:16:42 -0700671 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 struct hugetlbfs_inode_info *p;
673
Christoph Hellwig96527982005-10-29 18:16:42 -0700674 if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 return NULL;
Christoph Lametere94b1762006-12-06 20:33:17 -0800676 p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL);
Christoph Hellwig96527982005-10-29 18:16:42 -0700677 if (unlikely(!p)) {
678 hugetlbfs_inc_free_inodes(sbinfo);
679 return NULL;
680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return &p->vfs_inode;
682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684static void hugetlbfs_destroy_inode(struct inode *inode)
685{
Christoph Hellwig96527982005-10-29 18:16:42 -0700686 hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
688 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
689}
690
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700691static const struct address_space_operations hugetlbfs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 .readpage = hugetlbfs_readpage,
Nick Piggin800d15a2007-10-16 01:25:03 -0700693 .write_begin = hugetlbfs_write_begin,
694 .write_end = hugetlbfs_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 .set_page_dirty = hugetlbfs_set_page_dirty,
696};
697
Christoph Hellwig96527982005-10-29 18:16:42 -0700698
Christoph Lameter4ba9b9d2007-10-16 23:25:51 -0700699static void init_once(struct kmem_cache *cachep, void *foo)
Christoph Hellwig96527982005-10-29 18:16:42 -0700700{
701 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
702
Christoph Lametera35afb82007-05-16 22:10:57 -0700703 inode_init_once(&ei->vfs_inode);
Christoph Hellwig96527982005-10-29 18:16:42 -0700704}
705
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800706const struct file_operations hugetlbfs_file_operations = {
Badari Pulavartye63e1e52007-10-16 01:26:22 -0700707 .read = hugetlbfs_read,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 .mmap = hugetlbfs_file_mmap,
709 .fsync = simple_sync_file,
710 .get_unmapped_area = hugetlb_get_unmapped_area,
711};
712
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800713static const struct inode_operations hugetlbfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 .create = hugetlbfs_create,
715 .lookup = simple_lookup,
716 .link = simple_link,
717 .unlink = simple_unlink,
718 .symlink = hugetlbfs_symlink,
719 .mkdir = hugetlbfs_mkdir,
720 .rmdir = simple_rmdir,
721 .mknod = hugetlbfs_mknod,
722 .rename = simple_rename,
723 .setattr = hugetlbfs_setattr,
724};
725
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800726static const struct inode_operations hugetlbfs_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 .setattr = hugetlbfs_setattr,
728};
729
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800730static const struct super_operations hugetlbfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 .alloc_inode = hugetlbfs_alloc_inode,
732 .destroy_inode = hugetlbfs_destroy_inode,
733 .statfs = hugetlbfs_statfs,
Christoph Hellwig149f4212005-10-29 18:16:43 -0700734 .delete_inode = hugetlbfs_delete_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 .drop_inode = hugetlbfs_drop_inode,
736 .put_super = hugetlbfs_put_super,
737};
738
739static int
740hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
741{
Randy Dunlape73a75f2007-07-15 23:40:52 -0700742 char *p, *rest;
743 substring_t args[MAX_OPT_ARGS];
744 int option;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if (!options)
747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Randy Dunlape73a75f2007-07-15 23:40:52 -0700749 while ((p = strsep(&options, ",")) != NULL) {
750 int token;
Lee Schermerhornb4c07bc2007-07-15 23:40:54 -0700751 if (!*p)
752 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Randy Dunlape73a75f2007-07-15 23:40:52 -0700754 token = match_token(p, tokens, args);
755 switch (token) {
756 case Opt_uid:
757 if (match_int(&args[0], &option))
758 goto bad_val;
759 pconfig->uid = option;
760 break;
761
762 case Opt_gid:
763 if (match_int(&args[0], &option))
764 goto bad_val;
765 pconfig->gid = option;
766 break;
767
768 case Opt_mode:
769 if (match_octal(&args[0], &option))
770 goto bad_val;
771 pconfig->mode = option & 0777U;
772 break;
773
774 case Opt_size: {
775 unsigned long long size;
776 /* memparse() will accept a K/M/G without a digit */
777 if (!isdigit(*args[0].from))
778 goto bad_val;
779 size = memparse(args[0].from, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (*rest == '%') {
781 size <<= HPAGE_SHIFT;
782 size *= max_huge_pages;
783 do_div(size, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 pconfig->nr_blocks = (size >> HPAGE_SHIFT);
Randy Dunlape73a75f2007-07-15 23:40:52 -0700786 break;
787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Randy Dunlape73a75f2007-07-15 23:40:52 -0700789 case Opt_nr_inodes:
790 /* memparse() will accept a K/M/G without a digit */
791 if (!isdigit(*args[0].from))
792 goto bad_val;
793 pconfig->nr_inodes = memparse(args[0].from, &rest);
794 break;
795
796 default:
Lee Schermerhornb4c07bc2007-07-15 23:40:54 -0700797 printk(KERN_ERR "hugetlbfs: Bad mount option: \"%s\"\n",
798 p);
799 return -EINVAL;
Randy Dunlape73a75f2007-07-15 23:40:52 -0700800 break;
801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803 return 0;
Randy Dunlape73a75f2007-07-15 23:40:52 -0700804
805bad_val:
806 printk(KERN_ERR "hugetlbfs: Bad value '%s' for mount option '%s'\n",
807 args[0].from, p);
808 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811static int
812hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
813{
814 struct inode * inode;
815 struct dentry * root;
816 int ret;
817 struct hugetlbfs_config config;
818 struct hugetlbfs_sb_info *sbinfo;
819
820 config.nr_blocks = -1; /* No limit on size by default */
821 config.nr_inodes = -1; /* No limit on number of inodes by default */
822 config.uid = current->fsuid;
823 config.gid = current->fsgid;
824 config.mode = 0755;
825 ret = hugetlbfs_parse_options(data, &config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (ret)
827 return ret;
828
829 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
830 if (!sbinfo)
831 return -ENOMEM;
832 sb->s_fs_info = sbinfo;
833 spin_lock_init(&sbinfo->stat_lock);
834 sbinfo->max_blocks = config.nr_blocks;
835 sbinfo->free_blocks = config.nr_blocks;
836 sbinfo->max_inodes = config.nr_inodes;
837 sbinfo->free_inodes = config.nr_inodes;
838 sb->s_maxbytes = MAX_LFS_FILESIZE;
839 sb->s_blocksize = HPAGE_SIZE;
840 sb->s_blocksize_bits = HPAGE_SHIFT;
841 sb->s_magic = HUGETLBFS_MAGIC;
842 sb->s_op = &hugetlbfs_ops;
843 sb->s_time_gran = 1;
844 inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
845 S_IFDIR | config.mode, 0);
846 if (!inode)
847 goto out_free;
848
849 root = d_alloc_root(inode);
850 if (!root) {
851 iput(inode);
852 goto out_free;
853 }
854 sb->s_root = root;
855 return 0;
856out_free:
857 kfree(sbinfo);
858 return -ENOMEM;
859}
860
Adam Litke9a119c02007-11-14 16:59:41 -0800861int hugetlb_get_quota(struct address_space *mapping, long delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 int ret = 0;
864 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
865
866 if (sbinfo->free_blocks > -1) {
867 spin_lock(&sbinfo->stat_lock);
Adam Litke9a119c02007-11-14 16:59:41 -0800868 if (sbinfo->free_blocks - delta >= 0)
869 sbinfo->free_blocks -= delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 else
871 ret = -ENOMEM;
872 spin_unlock(&sbinfo->stat_lock);
873 }
874
875 return ret;
876}
877
Adam Litke9a119c02007-11-14 16:59:41 -0800878void hugetlb_put_quota(struct address_space *mapping, long delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
881
882 if (sbinfo->free_blocks > -1) {
883 spin_lock(&sbinfo->stat_lock);
Adam Litke9a119c02007-11-14 16:59:41 -0800884 sbinfo->free_blocks += delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 spin_unlock(&sbinfo->stat_lock);
886 }
887}
888
David Howells454e2392006-06-23 02:02:57 -0700889static int hugetlbfs_get_sb(struct file_system_type *fs_type,
890 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
David Howells454e2392006-06-23 02:02:57 -0700892 return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
895static struct file_system_type hugetlbfs_fs_type = {
896 .name = "hugetlbfs",
897 .get_sb = hugetlbfs_get_sb,
898 .kill_sb = kill_litter_super,
899};
900
901static struct vfsmount *hugetlbfs_vfsmount;
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903static int can_do_hugetlb_shm(void)
904{
905 return likely(capable(CAP_IPC_LOCK) ||
906 in_group_p(sysctl_hugetlb_shm_group) ||
907 can_do_mlock());
908}
909
Eric W. Biederman9d665862007-06-16 10:16:16 -0700910struct file *hugetlb_file_setup(const char *name, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 int error = -ENOMEM;
913 struct file *file;
914 struct inode *inode;
915 struct dentry *dentry, *root;
916 struct qstr quick_string;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Akinobu Mita5bc98592007-05-06 14:50:18 -0700918 if (!hugetlbfs_vfsmount)
919 return ERR_PTR(-ENOENT);
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (!can_do_hugetlb_shm())
922 return ERR_PTR(-EPERM);
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (!user_shm_lock(size, current->user))
925 return ERR_PTR(-ENOMEM);
926
927 root = hugetlbfs_vfsmount->mnt_root;
Eric W. Biederman9d665862007-06-16 10:16:16 -0700928 quick_string.name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 quick_string.len = strlen(quick_string.name);
930 quick_string.hash = 0;
931 dentry = d_alloc(root, &quick_string);
932 if (!dentry)
933 goto out_shm_unlock;
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 error = -ENOSPC;
936 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
937 current->fsgid, S_IFREG | S_IRWXUGO, 0);
938 if (!inode)
Dave Hansence8d2cd2007-10-16 23:31:13 -0700939 goto out_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
David Gibsonb45b5bd2006-03-22 00:08:55 -0800941 error = -ENOMEM;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700942 if (hugetlb_reserve_pages(inode, 0, size >> HPAGE_SHIFT))
David Gibsonb45b5bd2006-03-22 00:08:55 -0800943 goto out_inode;
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 d_instantiate(dentry, inode);
946 inode->i_size = size;
947 inode->i_nlink = 0;
Dave Hansence8d2cd2007-10-16 23:31:13 -0700948
949 error = -ENFILE;
950 file = alloc_file(hugetlbfs_vfsmount, dentry,
951 FMODE_WRITE | FMODE_READ,
952 &hugetlbfs_file_operations);
953 if (!file)
954 goto out_inode;
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return file;
957
David Gibsonb45b5bd2006-03-22 00:08:55 -0800958out_inode:
959 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960out_dentry:
961 dput(dentry);
962out_shm_unlock:
963 user_shm_unlock(size, current->user);
964 return ERR_PTR(error);
965}
966
967static int __init init_hugetlbfs_fs(void)
968{
969 int error;
970 struct vfsmount *vfsmount;
971
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700972 error = bdi_init(&hugetlbfs_backing_dev_info);
973 if (error)
974 return error;
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
977 sizeof(struct hugetlbfs_inode_info),
Paul Mundt20c2df82007-07-20 10:11:58 +0900978 0, 0, init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (hugetlbfs_inode_cachep == NULL)
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700980 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 error = register_filesystem(&hugetlbfs_fs_type);
983 if (error)
984 goto out;
985
986 vfsmount = kern_mount(&hugetlbfs_fs_type);
987
988 if (!IS_ERR(vfsmount)) {
989 hugetlbfs_vfsmount = vfsmount;
990 return 0;
991 }
992
993 error = PTR_ERR(vfsmount);
994
995 out:
996 if (error)
997 kmem_cache_destroy(hugetlbfs_inode_cachep);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700998 out2:
999 bdi_destroy(&hugetlbfs_backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return error;
1001}
1002
1003static void __exit exit_hugetlbfs_fs(void)
1004{
1005 kmem_cache_destroy(hugetlbfs_inode_cachep);
1006 unregister_filesystem(&hugetlbfs_fs_type);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001007 bdi_destroy(&hugetlbfs_backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
1010module_init(init_hugetlbfs_fs)
1011module_exit(exit_hugetlbfs_fs)
1012
1013MODULE_LICENSE("GPL");