inode.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. 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. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(inode->i_sb);
  189. hlist_del_init(&inode->i_hash);
  190. list_del_init(&inode->i_list);
  191. list_del_init(&inode->i_sb_list);
  192. inode->i_state |= I_FREEING;
  193. inodes_stat.nr_inodes--;
  194. spin_unlock(&inode_lock);
  195. if (inode->i_data.nrpages)
  196. truncate_hugepages(&inode->i_data, 0);
  197. security_inode_delete(inode);
  198. if (sbinfo->free_inodes >= 0) {
  199. spin_lock(&sbinfo->stat_lock);
  200. sbinfo->free_inodes++;
  201. spin_unlock(&sbinfo->stat_lock);
  202. }
  203. clear_inode(inode);
  204. destroy_inode(inode);
  205. }
  206. static void hugetlbfs_forget_inode(struct inode *inode)
  207. {
  208. struct super_block *super_block = inode->i_sb;
  209. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(super_block);
  210. if (hlist_unhashed(&inode->i_hash))
  211. goto out_truncate;
  212. if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
  213. list_del(&inode->i_list);
  214. list_add(&inode->i_list, &inode_unused);
  215. }
  216. inodes_stat.nr_unused++;
  217. if (!super_block || (super_block->s_flags & MS_ACTIVE)) {
  218. spin_unlock(&inode_lock);
  219. return;
  220. }
  221. /* write_inode_now() ? */
  222. inodes_stat.nr_unused--;
  223. hlist_del_init(&inode->i_hash);
  224. out_truncate:
  225. list_del_init(&inode->i_list);
  226. list_del_init(&inode->i_sb_list);
  227. inode->i_state |= I_FREEING;
  228. inodes_stat.nr_inodes--;
  229. spin_unlock(&inode_lock);
  230. if (inode->i_data.nrpages)
  231. truncate_hugepages(&inode->i_data, 0);
  232. if (sbinfo->free_inodes >= 0) {
  233. spin_lock(&sbinfo->stat_lock);
  234. sbinfo->free_inodes++;
  235. spin_unlock(&sbinfo->stat_lock);
  236. }
  237. clear_inode(inode);
  238. destroy_inode(inode);
  239. }
  240. static void hugetlbfs_drop_inode(struct inode *inode)
  241. {
  242. if (!inode->i_nlink)
  243. hugetlbfs_delete_inode(inode);
  244. else
  245. hugetlbfs_forget_inode(inode);
  246. }
  247. /*
  248. * h_pgoff is in HPAGE_SIZE units.
  249. * vma->vm_pgoff is in PAGE_SIZE units.
  250. */
  251. static inline void
  252. hugetlb_vmtruncate_list(struct prio_tree_root *root, unsigned long h_pgoff)
  253. {
  254. struct vm_area_struct *vma;
  255. struct prio_tree_iter iter;
  256. vma_prio_tree_foreach(vma, &iter, root, h_pgoff, ULONG_MAX) {
  257. unsigned long h_vm_pgoff;
  258. unsigned long v_length;
  259. unsigned long v_offset;
  260. h_vm_pgoff = vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT);
  261. v_offset = (h_pgoff - h_vm_pgoff) << HPAGE_SHIFT;
  262. /*
  263. * Is this VMA fully outside the truncation point?
  264. */
  265. if (h_vm_pgoff >= h_pgoff)
  266. v_offset = 0;
  267. v_length = vma->vm_end - vma->vm_start;
  268. zap_hugepage_range(vma,
  269. vma->vm_start + v_offset,
  270. v_length - v_offset);
  271. }
  272. }
  273. /*
  274. * Expanding truncates are not allowed.
  275. */
  276. static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
  277. {
  278. unsigned long pgoff;
  279. struct address_space *mapping = inode->i_mapping;
  280. if (offset > inode->i_size)
  281. return -EINVAL;
  282. BUG_ON(offset & ~HPAGE_MASK);
  283. pgoff = offset >> HPAGE_SHIFT;
  284. inode->i_size = offset;
  285. spin_lock(&mapping->i_mmap_lock);
  286. if (!prio_tree_empty(&mapping->i_mmap))
  287. hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
  288. spin_unlock(&mapping->i_mmap_lock);
  289. truncate_hugepages(mapping, offset);
  290. return 0;
  291. }
  292. static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
  293. {
  294. struct inode *inode = dentry->d_inode;
  295. int error;
  296. unsigned int ia_valid = attr->ia_valid;
  297. BUG_ON(!inode);
  298. error = inode_change_ok(inode, attr);
  299. if (error)
  300. goto out;
  301. if (ia_valid & ATTR_SIZE) {
  302. error = -EINVAL;
  303. if (!(attr->ia_size & ~HPAGE_MASK))
  304. error = hugetlb_vmtruncate(inode, attr->ia_size);
  305. if (error)
  306. goto out;
  307. attr->ia_valid &= ~ATTR_SIZE;
  308. }
  309. error = inode_setattr(inode, attr);
  310. out:
  311. return error;
  312. }
  313. static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
  314. gid_t gid, int mode, dev_t dev)
  315. {
  316. struct inode *inode;
  317. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
  318. if (sbinfo->free_inodes >= 0) {
  319. spin_lock(&sbinfo->stat_lock);
  320. if (!sbinfo->free_inodes) {
  321. spin_unlock(&sbinfo->stat_lock);
  322. return NULL;
  323. }
  324. sbinfo->free_inodes--;
  325. spin_unlock(&sbinfo->stat_lock);
  326. }
  327. inode = new_inode(sb);
  328. if (inode) {
  329. struct hugetlbfs_inode_info *info;
  330. inode->i_mode = mode;
  331. inode->i_uid = uid;
  332. inode->i_gid = gid;
  333. inode->i_blksize = HPAGE_SIZE;
  334. inode->i_blocks = 0;
  335. inode->i_mapping->a_ops = &hugetlbfs_aops;
  336. inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
  337. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  338. info = HUGETLBFS_I(inode);
  339. mpol_shared_policy_init(&info->policy);
  340. switch (mode & S_IFMT) {
  341. default:
  342. init_special_inode(inode, mode, dev);
  343. break;
  344. case S_IFREG:
  345. inode->i_op = &hugetlbfs_inode_operations;
  346. inode->i_fop = &hugetlbfs_file_operations;
  347. break;
  348. case S_IFDIR:
  349. inode->i_op = &hugetlbfs_dir_inode_operations;
  350. inode->i_fop = &simple_dir_operations;
  351. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  352. inode->i_nlink++;
  353. break;
  354. case S_IFLNK:
  355. inode->i_op = &page_symlink_inode_operations;
  356. break;
  357. }
  358. }
  359. return inode;
  360. }
  361. /*
  362. * File creation. Allocate an inode, and we're done..
  363. */
  364. static int hugetlbfs_mknod(struct inode *dir,
  365. struct dentry *dentry, int mode, dev_t dev)
  366. {
  367. struct inode *inode;
  368. int error = -ENOSPC;
  369. gid_t gid;
  370. if (dir->i_mode & S_ISGID) {
  371. gid = dir->i_gid;
  372. if (S_ISDIR(mode))
  373. mode |= S_ISGID;
  374. } else {
  375. gid = current->fsgid;
  376. }
  377. inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid, gid, mode, dev);
  378. if (inode) {
  379. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  380. d_instantiate(dentry, inode);
  381. dget(dentry); /* Extra count - pin the dentry in core */
  382. error = 0;
  383. }
  384. return error;
  385. }
  386. static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  387. {
  388. int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
  389. if (!retval)
  390. dir->i_nlink++;
  391. return retval;
  392. }
  393. static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
  394. {
  395. return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
  396. }
  397. static int hugetlbfs_symlink(struct inode *dir,
  398. struct dentry *dentry, const char *symname)
  399. {
  400. struct inode *inode;
  401. int error = -ENOSPC;
  402. gid_t gid;
  403. if (dir->i_mode & S_ISGID)
  404. gid = dir->i_gid;
  405. else
  406. gid = current->fsgid;
  407. inode = hugetlbfs_get_inode(dir->i_sb, current->fsuid,
  408. gid, S_IFLNK|S_IRWXUGO, 0);
  409. if (inode) {
  410. int l = strlen(symname)+1;
  411. error = page_symlink(inode, symname, l);
  412. if (!error) {
  413. d_instantiate(dentry, inode);
  414. dget(dentry);
  415. } else
  416. iput(inode);
  417. }
  418. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  419. return error;
  420. }
  421. /*
  422. * For direct-IO reads into hugetlb pages
  423. */
  424. static int hugetlbfs_set_page_dirty(struct page *page)
  425. {
  426. return 0;
  427. }
  428. static int hugetlbfs_statfs(struct super_block *sb, struct kstatfs *buf)
  429. {
  430. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
  431. buf->f_type = HUGETLBFS_MAGIC;
  432. buf->f_bsize = HPAGE_SIZE;
  433. if (sbinfo) {
  434. spin_lock(&sbinfo->stat_lock);
  435. buf->f_blocks = sbinfo->max_blocks;
  436. buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
  437. buf->f_files = sbinfo->max_inodes;
  438. buf->f_ffree = sbinfo->free_inodes;
  439. spin_unlock(&sbinfo->stat_lock);
  440. }
  441. buf->f_namelen = NAME_MAX;
  442. return 0;
  443. }
  444. static void hugetlbfs_put_super(struct super_block *sb)
  445. {
  446. struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
  447. if (sbi) {
  448. sb->s_fs_info = NULL;
  449. kfree(sbi);
  450. }
  451. }
  452. static kmem_cache_t *hugetlbfs_inode_cachep;
  453. static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
  454. {
  455. struct hugetlbfs_inode_info *p;
  456. p = kmem_cache_alloc(hugetlbfs_inode_cachep, SLAB_KERNEL);
  457. if (!p)
  458. return NULL;
  459. return &p->vfs_inode;
  460. }
  461. static void init_once(void *foo, kmem_cache_t *cachep, unsigned long flags)
  462. {
  463. struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
  464. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  465. SLAB_CTOR_CONSTRUCTOR)
  466. inode_init_once(&ei->vfs_inode);
  467. }
  468. static void hugetlbfs_destroy_inode(struct inode *inode)
  469. {
  470. mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
  471. kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
  472. }
  473. static struct address_space_operations hugetlbfs_aops = {
  474. .readpage = hugetlbfs_readpage,
  475. .prepare_write = hugetlbfs_prepare_write,
  476. .commit_write = hugetlbfs_commit_write,
  477. .set_page_dirty = hugetlbfs_set_page_dirty,
  478. };
  479. struct file_operations hugetlbfs_file_operations = {
  480. .mmap = hugetlbfs_file_mmap,
  481. .fsync = simple_sync_file,
  482. .get_unmapped_area = hugetlb_get_unmapped_area,
  483. };
  484. static struct inode_operations hugetlbfs_dir_inode_operations = {
  485. .create = hugetlbfs_create,
  486. .lookup = simple_lookup,
  487. .link = simple_link,
  488. .unlink = simple_unlink,
  489. .symlink = hugetlbfs_symlink,
  490. .mkdir = hugetlbfs_mkdir,
  491. .rmdir = simple_rmdir,
  492. .mknod = hugetlbfs_mknod,
  493. .rename = simple_rename,
  494. .setattr = hugetlbfs_setattr,
  495. };
  496. static struct inode_operations hugetlbfs_inode_operations = {
  497. .setattr = hugetlbfs_setattr,
  498. };
  499. static struct super_operations hugetlbfs_ops = {
  500. .alloc_inode = hugetlbfs_alloc_inode,
  501. .destroy_inode = hugetlbfs_destroy_inode,
  502. .statfs = hugetlbfs_statfs,
  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");