inode.c 20 KB

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