inode.c 21 KB

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