inode.c 20 KB

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