inode.c 19 KB

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