inode.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. /* If no limits set, just report 0 for max/free/used
  446. * blocks, like simple_statfs() */
  447. if (sbinfo->max_blocks >= 0) {
  448. buf->f_blocks = sbinfo->max_blocks;
  449. buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
  450. buf->f_files = sbinfo->max_inodes;
  451. buf->f_ffree = sbinfo->free_inodes;
  452. }
  453. spin_unlock(&sbinfo->stat_lock);
  454. }
  455. buf->f_namelen = NAME_MAX;
  456. return 0;
  457. }
  458. static void hugetlbfs_put_super(struct super_block *sb)
  459. {
  460. struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
  461. if (sbi) {
  462. sb->s_fs_info = NULL;
  463. kfree(sbi);
  464. }
  465. }
  466. static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  467. {
  468. if (sbinfo->free_inodes >= 0) {
  469. spin_lock(&sbinfo->stat_lock);
  470. if (unlikely(!sbinfo->free_inodes)) {
  471. spin_unlock(&sbinfo->stat_lock);
  472. return 0;
  473. }
  474. sbinfo->free_inodes--;
  475. spin_unlock(&sbinfo->stat_lock);
  476. }
  477. return 1;
  478. }
  479. static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  480. {
  481. if (sbinfo->free_inodes >= 0) {
  482. spin_lock(&sbinfo->stat_lock);
  483. sbinfo->free_inodes++;
  484. spin_unlock(&sbinfo->stat_lock);
  485. }
  486. }
  487. static kmem_cache_t *hugetlbfs_inode_cachep;
  488. static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
  489. {
  490. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
  491. struct hugetlbfs_inode_info *p;
  492. if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
  493. return NULL;
  494. p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
  495. if (unlikely(!p)) {
  496. hugetlbfs_inc_free_inodes(sbinfo);
  497. return NULL;
  498. }
  499. return &p->vfs_inode;
  500. }
  501. static void hugetlbfs_destroy_inode(struct inode *inode)
  502. {
  503. hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
  504. mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
  505. kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
  506. }
  507. static struct address_space_operations hugetlbfs_aops = {
  508. .readpage = hugetlbfs_readpage,
  509. .prepare_write = hugetlbfs_prepare_write,
  510. .commit_write = hugetlbfs_commit_write,
  511. .set_page_dirty = hugetlbfs_set_page_dirty,
  512. };
  513. static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
  514. {
  515. struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
  516. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  517. SLAB_CTOR_CONSTRUCTOR)
  518. inode_init_once(&ei->vfs_inode);
  519. }
  520. struct file_operations hugetlbfs_file_operations = {
  521. .mmap = hugetlbfs_file_mmap,
  522. .fsync = simple_sync_file,
  523. .get_unmapped_area = hugetlb_get_unmapped_area,
  524. };
  525. static struct inode_operations hugetlbfs_dir_inode_operations = {
  526. .create = hugetlbfs_create,
  527. .lookup = simple_lookup,
  528. .link = simple_link,
  529. .unlink = simple_unlink,
  530. .symlink = hugetlbfs_symlink,
  531. .mkdir = hugetlbfs_mkdir,
  532. .rmdir = simple_rmdir,
  533. .mknod = hugetlbfs_mknod,
  534. .rename = simple_rename,
  535. .setattr = hugetlbfs_setattr,
  536. };
  537. static struct inode_operations hugetlbfs_inode_operations = {
  538. .setattr = hugetlbfs_setattr,
  539. };
  540. static struct super_operations hugetlbfs_ops = {
  541. .alloc_inode = hugetlbfs_alloc_inode,
  542. .destroy_inode = hugetlbfs_destroy_inode,
  543. .statfs = hugetlbfs_statfs,
  544. .delete_inode = hugetlbfs_delete_inode,
  545. .drop_inode = hugetlbfs_drop_inode,
  546. .put_super = hugetlbfs_put_super,
  547. };
  548. static int
  549. hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
  550. {
  551. char *opt, *value, *rest;
  552. if (!options)
  553. return 0;
  554. while ((opt = strsep(&options, ",")) != NULL) {
  555. if (!*opt)
  556. continue;
  557. value = strchr(opt, '=');
  558. if (!value || !*value)
  559. return -EINVAL;
  560. else
  561. *value++ = '\0';
  562. if (!strcmp(opt, "uid"))
  563. pconfig->uid = simple_strtoul(value, &value, 0);
  564. else if (!strcmp(opt, "gid"))
  565. pconfig->gid = simple_strtoul(value, &value, 0);
  566. else if (!strcmp(opt, "mode"))
  567. pconfig->mode = simple_strtoul(value,&value,0) & 0777U;
  568. else if (!strcmp(opt, "size")) {
  569. unsigned long long size = memparse(value, &rest);
  570. if (*rest == '%') {
  571. size <<= HPAGE_SHIFT;
  572. size *= max_huge_pages;
  573. do_div(size, 100);
  574. rest++;
  575. }
  576. size &= HPAGE_MASK;
  577. pconfig->nr_blocks = (size >> HPAGE_SHIFT);
  578. value = rest;
  579. } else if (!strcmp(opt,"nr_inodes")) {
  580. pconfig->nr_inodes = memparse(value, &rest);
  581. value = rest;
  582. } else
  583. return -EINVAL;
  584. if (*value)
  585. return -EINVAL;
  586. }
  587. return 0;
  588. }
  589. static int
  590. hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
  591. {
  592. struct inode * inode;
  593. struct dentry * root;
  594. int ret;
  595. struct hugetlbfs_config config;
  596. struct hugetlbfs_sb_info *sbinfo;
  597. config.nr_blocks = -1; /* No limit on size by default */
  598. config.nr_inodes = -1; /* No limit on number of inodes by default */
  599. config.uid = current->fsuid;
  600. config.gid = current->fsgid;
  601. config.mode = 0755;
  602. ret = hugetlbfs_parse_options(data, &config);
  603. if (ret)
  604. return ret;
  605. sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
  606. if (!sbinfo)
  607. return -ENOMEM;
  608. sb->s_fs_info = sbinfo;
  609. spin_lock_init(&sbinfo->stat_lock);
  610. sbinfo->max_blocks = config.nr_blocks;
  611. sbinfo->free_blocks = config.nr_blocks;
  612. sbinfo->max_inodes = config.nr_inodes;
  613. sbinfo->free_inodes = config.nr_inodes;
  614. sb->s_maxbytes = MAX_LFS_FILESIZE;
  615. sb->s_blocksize = HPAGE_SIZE;
  616. sb->s_blocksize_bits = HPAGE_SHIFT;
  617. sb->s_magic = HUGETLBFS_MAGIC;
  618. sb->s_op = &hugetlbfs_ops;
  619. sb->s_time_gran = 1;
  620. inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
  621. S_IFDIR | config.mode, 0);
  622. if (!inode)
  623. goto out_free;
  624. root = d_alloc_root(inode);
  625. if (!root) {
  626. iput(inode);
  627. goto out_free;
  628. }
  629. sb->s_root = root;
  630. return 0;
  631. out_free:
  632. kfree(sbinfo);
  633. return -ENOMEM;
  634. }
  635. int hugetlb_get_quota(struct address_space *mapping)
  636. {
  637. int ret = 0;
  638. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
  639. if (sbinfo->free_blocks > -1) {
  640. spin_lock(&sbinfo->stat_lock);
  641. if (sbinfo->free_blocks > 0)
  642. sbinfo->free_blocks--;
  643. else
  644. ret = -ENOMEM;
  645. spin_unlock(&sbinfo->stat_lock);
  646. }
  647. return ret;
  648. }
  649. void hugetlb_put_quota(struct address_space *mapping)
  650. {
  651. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
  652. if (sbinfo->free_blocks > -1) {
  653. spin_lock(&sbinfo->stat_lock);
  654. sbinfo->free_blocks++;
  655. spin_unlock(&sbinfo->stat_lock);
  656. }
  657. }
  658. static struct super_block *hugetlbfs_get_sb(struct file_system_type *fs_type,
  659. int flags, const char *dev_name, void *data)
  660. {
  661. return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super);
  662. }
  663. static struct file_system_type hugetlbfs_fs_type = {
  664. .name = "hugetlbfs",
  665. .get_sb = hugetlbfs_get_sb,
  666. .kill_sb = kill_litter_super,
  667. };
  668. static struct vfsmount *hugetlbfs_vfsmount;
  669. /*
  670. * Return the next identifier for a shm file
  671. */
  672. static unsigned long hugetlbfs_counter(void)
  673. {
  674. static DEFINE_SPINLOCK(lock);
  675. static unsigned long counter;
  676. unsigned long ret;
  677. spin_lock(&lock);
  678. ret = ++counter;
  679. spin_unlock(&lock);
  680. return ret;
  681. }
  682. static int can_do_hugetlb_shm(void)
  683. {
  684. return likely(capable(CAP_IPC_LOCK) ||
  685. in_group_p(sysctl_hugetlb_shm_group) ||
  686. can_do_mlock());
  687. }
  688. struct file *hugetlb_zero_setup(size_t size)
  689. {
  690. int error = -ENOMEM;
  691. struct file *file;
  692. struct inode *inode;
  693. struct dentry *dentry, *root;
  694. struct qstr quick_string;
  695. char buf[16];
  696. if (!can_do_hugetlb_shm())
  697. return ERR_PTR(-EPERM);
  698. if (!is_hugepage_mem_enough(size))
  699. return ERR_PTR(-ENOMEM);
  700. if (!user_shm_lock(size, current->user))
  701. return ERR_PTR(-ENOMEM);
  702. root = hugetlbfs_vfsmount->mnt_root;
  703. snprintf(buf, 16, "%lu", hugetlbfs_counter());
  704. quick_string.name = buf;
  705. quick_string.len = strlen(quick_string.name);
  706. quick_string.hash = 0;
  707. dentry = d_alloc(root, &quick_string);
  708. if (!dentry)
  709. goto out_shm_unlock;
  710. error = -ENFILE;
  711. file = get_empty_filp();
  712. if (!file)
  713. goto out_dentry;
  714. error = -ENOSPC;
  715. inode = hugetlbfs_get_inode(root->d_sb, current->fsuid,
  716. current->fsgid, S_IFREG | S_IRWXUGO, 0);
  717. if (!inode)
  718. goto out_file;
  719. d_instantiate(dentry, inode);
  720. inode->i_size = size;
  721. inode->i_nlink = 0;
  722. file->f_vfsmnt = mntget(hugetlbfs_vfsmount);
  723. file->f_dentry = dentry;
  724. file->f_mapping = inode->i_mapping;
  725. file->f_op = &hugetlbfs_file_operations;
  726. file->f_mode = FMODE_WRITE | FMODE_READ;
  727. return file;
  728. out_file:
  729. put_filp(file);
  730. out_dentry:
  731. dput(dentry);
  732. out_shm_unlock:
  733. user_shm_unlock(size, current->user);
  734. return ERR_PTR(error);
  735. }
  736. static int __init init_hugetlbfs_fs(void)
  737. {
  738. int error;
  739. struct vfsmount *vfsmount;
  740. hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
  741. sizeof(struct hugetlbfs_inode_info),
  742. 0, 0, init_once, NULL);
  743. if (hugetlbfs_inode_cachep == NULL)
  744. return -ENOMEM;
  745. error = register_filesystem(&hugetlbfs_fs_type);
  746. if (error)
  747. goto out;
  748. vfsmount = kern_mount(&hugetlbfs_fs_type);
  749. if (!IS_ERR(vfsmount)) {
  750. hugetlbfs_vfsmount = vfsmount;
  751. return 0;
  752. }
  753. error = PTR_ERR(vfsmount);
  754. out:
  755. if (error)
  756. kmem_cache_destroy(hugetlbfs_inode_cachep);
  757. return error;
  758. }
  759. static void __exit exit_hugetlbfs_fs(void)
  760. {
  761. kmem_cache_destroy(hugetlbfs_inode_cachep);
  762. unregister_filesystem(&hugetlbfs_fs_type);
  763. }
  764. module_init(init_hugetlbfs_fs)
  765. module_exit(exit_hugetlbfs_fs)
  766. MODULE_LICENSE("GPL");