inode.c 19 KB

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