inode.c 21 KB

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