blob: d145cb79c30a4dd3caea5f328737e3418e96548f [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 Litke2e9b3672005-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 /*
85 * vma alignment has already been checked by prepare_hugepage_range.
86 * If you add any error returns here, do so after setting VM_HUGETLB,
87 * so is_vm_hugetlb_page tests below unmap_region go the right way
88 * when do_mmap_pgoff unwinds (may be important on powerpc and ia64).
89 */
90 vma->vm_flags |= VM_HUGETLB | VM_RESERVED;
91 vma->vm_ops = &hugetlb_vm_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 vma_len = (loff_t)(vma->vm_end - vma->vm_start);
94
Jes Sorensen1b1dcc12006-01-09 15:59:24 -080095 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 file_accessed(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 ret = -ENOMEM;
99 len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700101 if (vma->vm_flags & VM_MAYSHARE &&
102 hugetlb_reserve_pages(inode, vma->vm_pgoff >> (HPAGE_SHIFT-PAGE_SHIFT),
103 len >> HPAGE_SHIFT))
104 goto out;
David Gibsonb45b5bd2006-03-22 00:08:55 -0800105
Adam Litke4c887262005-10-29 18:16:46 -0700106 ret = 0;
107 hugetlb_prefault_arch_hook(vma->vm_mm);
Zhang, Yanminb6174df2006-07-10 04:44:49 -0700108 if (vma->vm_flags & VM_WRITE && inode->i_size < len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 inode->i_size = len;
110out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800111 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 return ret;
114}
115
116/*
Hugh Dickins508034a2005-10-29 18:16:30 -0700117 * Called under down_write(mmap_sem).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
119
Adrian Bunkd2ba27e82007-05-06 14:49:00 -0700120#ifndef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121static unsigned long
122hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
123 unsigned long len, unsigned long pgoff, unsigned long flags)
124{
125 struct mm_struct *mm = current->mm;
126 struct vm_area_struct *vma;
127 unsigned long start_addr;
128
129 if (len & ~HPAGE_MASK)
130 return -EINVAL;
131 if (len > TASK_SIZE)
132 return -ENOMEM;
133
Benjamin Herrenschmidt036e0852007-05-06 14:50:12 -0700134 if (flags & MAP_FIXED) {
135 if (prepare_hugepage_range(addr, len, pgoff))
136 return -EINVAL;
137 return addr;
138 }
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (addr) {
141 addr = ALIGN(addr, HPAGE_SIZE);
142 vma = find_vma(mm, addr);
143 if (TASK_SIZE - len >= addr &&
144 (!vma || addr + len <= vma->vm_start))
145 return addr;
146 }
147
148 start_addr = mm->free_area_cache;
149
Wolfgang Wander1363c3c2005-06-21 17:14:49 -0700150 if (len <= mm->cached_hole_size)
151 start_addr = TASK_UNMAPPED_BASE;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153full_search:
154 addr = ALIGN(start_addr, HPAGE_SIZE);
155
156 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
157 /* At this point: (!vma || addr < vma->vm_end). */
158 if (TASK_SIZE - len < addr) {
159 /*
160 * Start a new search - just in case we missed
161 * some holes.
162 */
163 if (start_addr != TASK_UNMAPPED_BASE) {
164 start_addr = TASK_UNMAPPED_BASE;
165 goto full_search;
166 }
167 return -ENOMEM;
168 }
169
170 if (!vma || addr + len <= vma->vm_start)
171 return addr;
172 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
173 }
174}
175#endif
176
177/*
178 * Read a page. Again trivial. If it didn't already exist
179 * in the page cache, it is zero-filled.
180 */
181static int hugetlbfs_readpage(struct file *file, struct page * page)
182{
183 unlock_page(page);
184 return -EINVAL;
185}
186
187static int hugetlbfs_prepare_write(struct file *file,
188 struct page *page, unsigned offset, unsigned to)
189{
190 return -EINVAL;
191}
192
193static int hugetlbfs_commit_write(struct file *file,
194 struct page *page, unsigned offset, unsigned to)
195{
196 return -EINVAL;
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static void truncate_huge_page(struct page *page)
200{
Linus Torvaldsfba25912006-12-20 13:46:42 -0800201 cancel_dirty_page(page, /* No IO accounting for huge pages? */0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 ClearPageUptodate(page);
203 remove_from_page_cache(page);
204 put_page(page);
205}
206
David Gibsonb45b5bd2006-03-22 00:08:55 -0800207static void truncate_hugepages(struct inode *inode, loff_t lstart)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
David Gibsonb45b5bd2006-03-22 00:08:55 -0800209 struct address_space *mapping = &inode->i_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 const pgoff_t start = lstart >> HPAGE_SHIFT;
211 struct pagevec pvec;
212 pgoff_t next;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700213 int i, freed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 pagevec_init(&pvec, 0);
216 next = start;
217 while (1) {
218 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
219 if (next == start)
220 break;
221 next = start;
222 continue;
223 }
224
225 for (i = 0; i < pagevec_count(&pvec); ++i) {
226 struct page *page = pvec.pages[i];
227
228 lock_page(page);
229 if (page->index > next)
230 next = page->index;
231 ++next;
232 truncate_huge_page(page);
233 unlock_page(page);
234 hugetlb_put_quota(mapping);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700235 freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237 huge_pagevec_release(&pvec);
238 }
239 BUG_ON(!lstart && mapping->nrpages);
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700240 hugetlb_unreserve_pages(inode, start, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243static void hugetlbfs_delete_inode(struct inode *inode)
244{
David Gibsonb45b5bd2006-03-22 00:08:55 -0800245 truncate_hugepages(inode, 0);
Christoph Hellwig149f4212005-10-29 18:16:43 -0700246 clear_inode(inode);
247}
248
Josh Triplettddc0a512006-09-29 01:59:27 -0700249static void hugetlbfs_forget_inode(struct inode *inode) __releases(inode_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Christoph Hellwig0b1533f2005-10-29 18:16:45 -0700251 struct super_block *sb = inode->i_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Christoph Hellwig0b1533f2005-10-29 18:16:45 -0700253 if (!hlist_unhashed(&inode->i_hash)) {
254 if (!(inode->i_state & (I_DIRTY|I_LOCK)))
255 list_move(&inode->i_list, &inode_unused);
256 inodes_stat.nr_unused++;
257 if (!sb || (sb->s_flags & MS_ACTIVE)) {
258 spin_unlock(&inode_lock);
259 return;
260 }
261 inode->i_state |= I_WILL_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 spin_unlock(&inode_lock);
Christoph Hellwig0b1533f2005-10-29 18:16:45 -0700263 /*
264 * write_inode_now is a noop as we set BDI_CAP_NO_WRITEBACK
265 * in our backing_dev_info.
266 */
267 write_inode_now(inode, 1);
268 spin_lock(&inode_lock);
269 inode->i_state &= ~I_WILL_FREE;
270 inodes_stat.nr_unused--;
271 hlist_del_init(&inode->i_hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 list_del_init(&inode->i_list);
274 list_del_init(&inode->i_sb_list);
275 inode->i_state |= I_FREEING;
276 inodes_stat.nr_inodes--;
277 spin_unlock(&inode_lock);
David Gibsonb45b5bd2006-03-22 00:08:55 -0800278 truncate_hugepages(inode, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 clear_inode(inode);
280 destroy_inode(inode);
281}
282
283static void hugetlbfs_drop_inode(struct inode *inode)
284{
285 if (!inode->i_nlink)
Christoph Hellwig6b09b9d2005-10-29 18:16:44 -0700286 generic_delete_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 else
288 hugetlbfs_forget_inode(inode);
289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291static inline void
Hugh Dickins856fc292006-10-28 10:38:43 -0700292hugetlb_vmtruncate_list(struct prio_tree_root *root, pgoff_t pgoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 struct vm_area_struct *vma;
295 struct prio_tree_iter iter;
296
Hugh Dickins856fc292006-10-28 10:38:43 -0700297 vma_prio_tree_foreach(vma, &iter, root, pgoff, ULONG_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 unsigned long v_offset;
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /*
Hugh Dickins856fc292006-10-28 10:38:43 -0700301 * Can the expression below overflow on 32-bit arches?
302 * No, because the prio_tree returns us only those vmas
303 * which overlap the truncated area starting at pgoff,
304 * and no vma on a 32-bit arch can span beyond the 4GB.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 */
Hugh Dickins856fc292006-10-28 10:38:43 -0700306 if (vma->vm_pgoff < pgoff)
307 v_offset = (pgoff - vma->vm_pgoff) << PAGE_SHIFT;
308 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 v_offset = 0;
310
Chen, Kenneth W502717f2006-10-11 01:20:46 -0700311 __unmap_hugepage_range(vma,
Hugh Dickins508034a2005-10-29 18:16:30 -0700312 vma->vm_start + v_offset, vma->vm_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314}
315
316/*
317 * Expanding truncates are not allowed.
318 */
319static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
320{
Hugh Dickins856fc292006-10-28 10:38:43 -0700321 pgoff_t pgoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct address_space *mapping = inode->i_mapping;
323
324 if (offset > inode->i_size)
325 return -EINVAL;
326
327 BUG_ON(offset & ~HPAGE_MASK);
Hugh Dickins856fc292006-10-28 10:38:43 -0700328 pgoff = offset >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 inode->i_size = offset;
331 spin_lock(&mapping->i_mmap_lock);
332 if (!prio_tree_empty(&mapping->i_mmap))
333 hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
334 spin_unlock(&mapping->i_mmap_lock);
David Gibsonb45b5bd2006-03-22 00:08:55 -0800335 truncate_hugepages(inode, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 0;
337}
338
339static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
340{
341 struct inode *inode = dentry->d_inode;
342 int error;
343 unsigned int ia_valid = attr->ia_valid;
344
345 BUG_ON(!inode);
346
347 error = inode_change_ok(inode, attr);
348 if (error)
349 goto out;
350
351 if (ia_valid & ATTR_SIZE) {
352 error = -EINVAL;
353 if (!(attr->ia_size & ~HPAGE_MASK))
354 error = hugetlb_vmtruncate(inode, attr->ia_size);
355 if (error)
356 goto out;
357 attr->ia_valid &= ~ATTR_SIZE;
358 }
359 error = inode_setattr(inode, attr);
360out:
361 return error;
362}
363
364static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
365 gid_t gid, int mode, dev_t dev)
366{
367 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 inode = new_inode(sb);
370 if (inode) {
371 struct hugetlbfs_inode_info *info;
372 inode->i_mode = mode;
373 inode->i_uid = uid;
374 inode->i_gid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 inode->i_blocks = 0;
376 inode->i_mapping->a_ops = &hugetlbfs_aops;
377 inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
378 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700379 INIT_LIST_HEAD(&inode->i_mapping->private_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 info = HUGETLBFS_I(inode);
Robin Holt7339ff82006-01-14 13:20:48 -0800381 mpol_shared_policy_init(&info->policy, MPOL_DEFAULT, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 switch (mode & S_IFMT) {
383 default:
384 init_special_inode(inode, mode, dev);
385 break;
386 case S_IFREG:
387 inode->i_op = &hugetlbfs_inode_operations;
388 inode->i_fop = &hugetlbfs_file_operations;
389 break;
390 case S_IFDIR:
391 inode->i_op = &hugetlbfs_dir_inode_operations;
392 inode->i_fop = &simple_dir_operations;
393
394 /* directory inodes start off with i_nlink == 2 (for "." entry) */
Dave Hansend8c76e62006-09-30 23:29:04 -0700395 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 break;
397 case S_IFLNK:
398 inode->i_op = &page_symlink_inode_operations;
399 break;
400 }
401 }
402 return inode;
403}
404
405/*
406 * File creation. Allocate an inode, and we're done..
407 */
408static int hugetlbfs_mknod(struct inode *dir,
409 struct dentry *dentry, int mode, dev_t dev)
410{
411 struct inode *inode;
412 int error = -ENOSPC;
413 gid_t gid;
414
415 if (dir->i_mode & S_ISGID) {
416 gid = dir->i_gid;
417 if (S_ISDIR(mode))
418 mode |= S_ISGID;
419 } else {
420 gid = current->fsgid;
421 }
422 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
423 if (inode) {
424 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
425 d_instantiate(dentry, inode);
426 dget(dentry); /* Extra count - pin the dentry in core */
427 error = 0;
428 }
429 return error;
430}
431
432static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
433{
434 int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
435 if (!retval)
Dave Hansend8c76e62006-09-30 23:29:04 -0700436 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return retval;
438}
439
440static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
441{
442 return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
443}
444
445static int hugetlbfs_symlink(struct inode *dir,
446 struct dentry *dentry, const char *symname)
447{
448 struct inode *inode;
449 int error = -ENOSPC;
450 gid_t gid;
451
452 if (dir->i_mode & S_ISGID)
453 gid = dir->i_gid;
454 else
455 gid = current->fsgid;
456
457 inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
458 gid, S_IFLNK|S_IRWXUGO, 0);
459 if (inode) {
460 int l = strlen(symname)+1;
461 error = page_symlink(inode, symname, l);
462 if (!error) {
463 d_instantiate(dentry, inode);
464 dget(dentry);
465 } else
466 iput(inode);
467 }
468 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
469
470 return error;
471}
472
473/*
Ken Chen6649a382007-02-08 14:20:27 -0800474 * mark the head page dirty
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 */
476static int hugetlbfs_set_page_dirty(struct page *page)
477{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700478 struct page *head = compound_head(page);
Ken Chen6649a382007-02-08 14:20:27 -0800479
480 SetPageDirty(head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return 0;
482}
483
David Howells726c3342006-06-23 02:02:58 -0700484static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
David Howells726c3342006-06-23 02:02:58 -0700486 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 buf->f_type = HUGETLBFS_MAGIC;
489 buf->f_bsize = HPAGE_SIZE;
490 if (sbinfo) {
491 spin_lock(&sbinfo->stat_lock);
David Gibson74a8a652005-11-21 21:32:24 -0800492 /* If no limits set, just report 0 for max/free/used
493 * blocks, like simple_statfs() */
494 if (sbinfo->max_blocks >= 0) {
495 buf->f_blocks = sbinfo->max_blocks;
496 buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
497 buf->f_files = sbinfo->max_inodes;
498 buf->f_ffree = sbinfo->free_inodes;
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 spin_unlock(&sbinfo->stat_lock);
501 }
502 buf->f_namelen = NAME_MAX;
503 return 0;
504}
505
506static void hugetlbfs_put_super(struct super_block *sb)
507{
508 struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
509
510 if (sbi) {
511 sb->s_fs_info = NULL;
512 kfree(sbi);
513 }
514}
515
Christoph Hellwig96527982005-10-29 18:16:42 -0700516static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
517{
518 if (sbinfo->free_inodes >= 0) {
519 spin_lock(&sbinfo->stat_lock);
520 if (unlikely(!sbinfo->free_inodes)) {
521 spin_unlock(&sbinfo->stat_lock);
522 return 0;
523 }
524 sbinfo->free_inodes--;
525 spin_unlock(&sbinfo->stat_lock);
526 }
527
528 return 1;
529}
530
531static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
532{
533 if (sbinfo->free_inodes >= 0) {
534 spin_lock(&sbinfo->stat_lock);
535 sbinfo->free_inodes++;
536 spin_unlock(&sbinfo->stat_lock);
537 }
538}
539
540
Christoph Lametere18b8902006-12-06 20:33:20 -0800541static struct kmem_cache *hugetlbfs_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
544{
Christoph Hellwig96527982005-10-29 18:16:42 -0700545 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct hugetlbfs_inode_info *p;
547
Christoph Hellwig96527982005-10-29 18:16:42 -0700548 if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return NULL;
Christoph Lametere94b1762006-12-06 20:33:17 -0800550 p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL);
Christoph Hellwig96527982005-10-29 18:16:42 -0700551 if (unlikely(!p)) {
552 hugetlbfs_inc_free_inodes(sbinfo);
553 return NULL;
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return &p->vfs_inode;
556}
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558static void hugetlbfs_destroy_inode(struct inode *inode)
559{
Christoph Hellwig96527982005-10-29 18:16:42 -0700560 hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
562 kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
563}
564
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700565static const struct address_space_operations hugetlbfs_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 .readpage = hugetlbfs_readpage,
567 .prepare_write = hugetlbfs_prepare_write,
568 .commit_write = hugetlbfs_commit_write,
569 .set_page_dirty = hugetlbfs_set_page_dirty,
570};
571
Christoph Hellwig96527982005-10-29 18:16:42 -0700572
Christoph Lametere18b8902006-12-06 20:33:20 -0800573static void init_once(void *foo, struct kmem_cache *cachep, unsigned long flags)
Christoph Hellwig96527982005-10-29 18:16:42 -0700574{
575 struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
576
Christoph Lametera35afb82007-05-16 22:10:57 -0700577 inode_init_once(&ei->vfs_inode);
Christoph Hellwig96527982005-10-29 18:16:42 -0700578}
579
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800580const struct file_operations hugetlbfs_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 .mmap = hugetlbfs_file_mmap,
582 .fsync = simple_sync_file,
583 .get_unmapped_area = hugetlb_get_unmapped_area,
584};
585
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800586static const struct inode_operations hugetlbfs_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 .create = hugetlbfs_create,
588 .lookup = simple_lookup,
589 .link = simple_link,
590 .unlink = simple_unlink,
591 .symlink = hugetlbfs_symlink,
592 .mkdir = hugetlbfs_mkdir,
593 .rmdir = simple_rmdir,
594 .mknod = hugetlbfs_mknod,
595 .rename = simple_rename,
596 .setattr = hugetlbfs_setattr,
597};
598
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800599static const struct inode_operations hugetlbfs_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 .setattr = hugetlbfs_setattr,
601};
602
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800603static const struct super_operations hugetlbfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 .alloc_inode = hugetlbfs_alloc_inode,
605 .destroy_inode = hugetlbfs_destroy_inode,
606 .statfs = hugetlbfs_statfs,
Christoph Hellwig149f4212005-10-29 18:16:43 -0700607 .delete_inode = hugetlbfs_delete_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 .drop_inode = hugetlbfs_drop_inode,
609 .put_super = hugetlbfs_put_super,
610};
611
612static int
613hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
614{
Randy Dunlape73a75f2007-07-15 23:40:52 -0700615 char *p, *rest;
616 substring_t args[MAX_OPT_ARGS];
617 int option;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 if (!options)
620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Randy Dunlape73a75f2007-07-15 23:40:52 -0700622 while ((p = strsep(&options, ",")) != NULL) {
623 int token;
Lee Schermerhornb4c07bc2007-07-15 23:40:54 -0700624 if (!*p)
625 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Randy Dunlape73a75f2007-07-15 23:40:52 -0700627 token = match_token(p, tokens, args);
628 switch (token) {
629 case Opt_uid:
630 if (match_int(&args[0], &option))
631 goto bad_val;
632 pconfig->uid = option;
633 break;
634
635 case Opt_gid:
636 if (match_int(&args[0], &option))
637 goto bad_val;
638 pconfig->gid = option;
639 break;
640
641 case Opt_mode:
642 if (match_octal(&args[0], &option))
643 goto bad_val;
644 pconfig->mode = option & 0777U;
645 break;
646
647 case Opt_size: {
648 unsigned long long size;
649 /* memparse() will accept a K/M/G without a digit */
650 if (!isdigit(*args[0].from))
651 goto bad_val;
652 size = memparse(args[0].from, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (*rest == '%') {
654 size <<= HPAGE_SHIFT;
655 size *= max_huge_pages;
656 do_div(size, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 pconfig->nr_blocks = (size >> HPAGE_SHIFT);
Randy Dunlape73a75f2007-07-15 23:40:52 -0700659 break;
660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Randy Dunlape73a75f2007-07-15 23:40:52 -0700662 case Opt_nr_inodes:
663 /* memparse() will accept a K/M/G without a digit */
664 if (!isdigit(*args[0].from))
665 goto bad_val;
666 pconfig->nr_inodes = memparse(args[0].from, &rest);
667 break;
668
669 default:
Lee Schermerhornb4c07bc2007-07-15 23:40:54 -0700670 printk(KERN_ERR "hugetlbfs: Bad mount option: \"%s\"\n",
671 p);
672 return -EINVAL;
Randy Dunlape73a75f2007-07-15 23:40:52 -0700673 break;
674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676 return 0;
Randy Dunlape73a75f2007-07-15 23:40:52 -0700677
678bad_val:
679 printk(KERN_ERR "hugetlbfs: Bad value '%s' for mount option '%s'\n",
680 args[0].from, p);
681 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
684static int
685hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
686{
687 struct inode * inode;
688 struct dentry * root;
689 int ret;
690 struct hugetlbfs_config config;
691 struct hugetlbfs_sb_info *sbinfo;
692
693 config.nr_blocks = -1; /* No limit on size by default */
694 config.nr_inodes = -1; /* No limit on number of inodes by default */
695 config.uid = current->fsuid;
696 config.gid = current->fsgid;
697 config.mode = 0755;
698 ret = hugetlbfs_parse_options(data, &config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (ret)
700 return ret;
701
702 sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
703 if (!sbinfo)
704 return -ENOMEM;
705 sb->s_fs_info = sbinfo;
706 spin_lock_init(&sbinfo->stat_lock);
707 sbinfo->max_blocks = config.nr_blocks;
708 sbinfo->free_blocks = config.nr_blocks;
709 sbinfo->max_inodes = config.nr_inodes;
710 sbinfo->free_inodes = config.nr_inodes;
711 sb->s_maxbytes = MAX_LFS_FILESIZE;
712 sb->s_blocksize = HPAGE_SIZE;
713 sb->s_blocksize_bits = HPAGE_SHIFT;
714 sb->s_magic = HUGETLBFS_MAGIC;
715 sb->s_op = &hugetlbfs_ops;
716 sb->s_time_gran = 1;
717 inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
718 S_IFDIR | config.mode, 0);
719 if (!inode)
720 goto out_free;
721
722 root = d_alloc_root(inode);
723 if (!root) {
724 iput(inode);
725 goto out_free;
726 }
727 sb->s_root = root;
728 return 0;
729out_free:
730 kfree(sbinfo);
731 return -ENOMEM;
732}
733
734int hugetlb_get_quota(struct address_space *mapping)
735{
736 int ret = 0;
737 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
738
739 if (sbinfo->free_blocks > -1) {
740 spin_lock(&sbinfo->stat_lock);
741 if (sbinfo->free_blocks > 0)
742 sbinfo->free_blocks--;
743 else
744 ret = -ENOMEM;
745 spin_unlock(&sbinfo->stat_lock);
746 }
747
748 return ret;
749}
750
751void hugetlb_put_quota(struct address_space *mapping)
752{
753 struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
754
755 if (sbinfo->free_blocks > -1) {
756 spin_lock(&sbinfo->stat_lock);
757 sbinfo->free_blocks++;
758 spin_unlock(&sbinfo->stat_lock);
759 }
760}
761
David Howells454e2392006-06-23 02:02:57 -0700762static int hugetlbfs_get_sb(struct file_system_type *fs_type,
763 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
David Howells454e2392006-06-23 02:02:57 -0700765 return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
768static struct file_system_type hugetlbfs_fs_type = {
769 .name = "hugetlbfs",
770 .get_sb = hugetlbfs_get_sb,
771 .kill_sb = kill_litter_super,
772};
773
774static struct vfsmount *hugetlbfs_vfsmount;
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776static int can_do_hugetlb_shm(void)
777{
778 return likely(capable(CAP_IPC_LOCK) ||
779 in_group_p(sysctl_hugetlb_shm_group) ||
780 can_do_mlock());
781}
782
Eric W. Biederman9d665862007-06-16 10:16:16 -0700783struct file *hugetlb_file_setup(const char *name, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 int error = -ENOMEM;
786 struct file *file;
787 struct inode *inode;
788 struct dentry *dentry, *root;
789 struct qstr quick_string;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Akinobu Mita5bc98592007-05-06 14:50:18 -0700791 if (!hugetlbfs_vfsmount)
792 return ERR_PTR(-ENOENT);
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (!can_do_hugetlb_shm())
795 return ERR_PTR(-EPERM);
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (!user_shm_lock(size, current->user))
798 return ERR_PTR(-ENOMEM);
799
800 root = hugetlbfs_vfsmount->mnt_root;
Eric W. Biederman9d665862007-06-16 10:16:16 -0700801 quick_string.name = name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 quick_string.len = strlen(quick_string.name);
803 quick_string.hash = 0;
804 dentry = d_alloc(root, &quick_string);
805 if (!dentry)
806 goto out_shm_unlock;
807
808 error = -ENFILE;
809 file = get_empty_filp();
810 if (!file)
811 goto out_dentry;
812
813 error = -ENOSPC;
814 inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
815 current->fsgid, S_IFREG | S_IRWXUGO, 0);
816 if (!inode)
817 goto out_file;
818
David Gibsonb45b5bd2006-03-22 00:08:55 -0800819 error = -ENOMEM;
Chen, Kenneth Wa43a8c32006-06-23 02:03:15 -0700820 if (hugetlb_reserve_pages(inode, 0, size >> HPAGE_SHIFT))
David Gibsonb45b5bd2006-03-22 00:08:55 -0800821 goto out_inode;
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 d_instantiate(dentry, inode);
824 inode->i_size = size;
825 inode->i_nlink = 0;
Josef Sipekb39424e2006-12-08 02:37:07 -0800826 file->f_path.mnt = mntget(hugetlbfs_vfsmount);
827 file->f_path.dentry = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 file->f_mapping = inode->i_mapping;
829 file->f_op = &hugetlbfs_file_operations;
830 file->f_mode = FMODE_WRITE | FMODE_READ;
831 return file;
832
David Gibsonb45b5bd2006-03-22 00:08:55 -0800833out_inode:
834 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835out_file:
836 put_filp(file);
837out_dentry:
838 dput(dentry);
839out_shm_unlock:
840 user_shm_unlock(size, current->user);
841 return ERR_PTR(error);
842}
843
844static int __init init_hugetlbfs_fs(void)
845{
846 int error;
847 struct vfsmount *vfsmount;
848
849 hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
850 sizeof(struct hugetlbfs_inode_info),
851 0, 0, init_once, NULL);
852 if (hugetlbfs_inode_cachep == NULL)
853 return -ENOMEM;
854
855 error = register_filesystem(&hugetlbfs_fs_type);
856 if (error)
857 goto out;
858
859 vfsmount = kern_mount(&hugetlbfs_fs_type);
860
861 if (!IS_ERR(vfsmount)) {
862 hugetlbfs_vfsmount = vfsmount;
863 return 0;
864 }
865
866 error = PTR_ERR(vfsmount);
867
868 out:
869 if (error)
870 kmem_cache_destroy(hugetlbfs_inode_cachep);
871 return error;
872}
873
874static void __exit exit_hugetlbfs_fs(void)
875{
876 kmem_cache_destroy(hugetlbfs_inode_cachep);
877 unregister_filesystem(&hugetlbfs_fs_type);
878}
879
880module_init(init_hugetlbfs_fs)
881module_exit(exit_hugetlbfs_fs)
882
883MODULE_LICENSE("GPL");