inode.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /*
  2. * hugetlbpage-backed filesystem. Based on ramfs.
  3. *
  4. * Nadia Yvette Chambers, 2002
  5. *
  6. * Copyright (C) 2002 Linus Torvalds.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/thread_info.h>
  11. #include <asm/current.h>
  12. #include <linux/sched.h> /* remove ASAP */
  13. #include <linux/fs.h>
  14. #include <linux/mount.h>
  15. #include <linux/file.h>
  16. #include <linux/kernel.h>
  17. #include <linux/writeback.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/highmem.h>
  20. #include <linux/init.h>
  21. #include <linux/string.h>
  22. #include <linux/capability.h>
  23. #include <linux/ctype.h>
  24. #include <linux/backing-dev.h>
  25. #include <linux/hugetlb.h>
  26. #include <linux/pagevec.h>
  27. #include <linux/parser.h>
  28. #include <linux/mman.h>
  29. #include <linux/slab.h>
  30. #include <linux/dnotify.h>
  31. #include <linux/statfs.h>
  32. #include <linux/security.h>
  33. #include <linux/magic.h>
  34. #include <linux/migrate.h>
  35. #include <asm/uaccess.h>
  36. static const struct super_operations hugetlbfs_ops;
  37. static const struct address_space_operations hugetlbfs_aops;
  38. const struct file_operations hugetlbfs_file_operations;
  39. static const struct inode_operations hugetlbfs_dir_inode_operations;
  40. static const struct inode_operations hugetlbfs_inode_operations;
  41. struct hugetlbfs_config {
  42. kuid_t uid;
  43. kgid_t gid;
  44. umode_t mode;
  45. long nr_blocks;
  46. long nr_inodes;
  47. struct hstate *hstate;
  48. };
  49. struct hugetlbfs_inode_info {
  50. struct shared_policy policy;
  51. struct inode vfs_inode;
  52. };
  53. static inline struct hugetlbfs_inode_info *HUGETLBFS_I(struct inode *inode)
  54. {
  55. return container_of(inode, struct hugetlbfs_inode_info, vfs_inode);
  56. }
  57. int sysctl_hugetlb_shm_group;
  58. enum {
  59. Opt_size, Opt_nr_inodes,
  60. Opt_mode, Opt_uid, Opt_gid,
  61. Opt_pagesize,
  62. Opt_err,
  63. };
  64. static const match_table_t tokens = {
  65. {Opt_size, "size=%s"},
  66. {Opt_nr_inodes, "nr_inodes=%s"},
  67. {Opt_mode, "mode=%o"},
  68. {Opt_uid, "uid=%u"},
  69. {Opt_gid, "gid=%u"},
  70. {Opt_pagesize, "pagesize=%s"},
  71. {Opt_err, NULL},
  72. };
  73. static void huge_pagevec_release(struct pagevec *pvec)
  74. {
  75. int i;
  76. for (i = 0; i < pagevec_count(pvec); ++i)
  77. put_page(pvec->pages[i]);
  78. pagevec_reinit(pvec);
  79. }
  80. static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
  81. {
  82. struct inode *inode = file_inode(file);
  83. loff_t len, vma_len;
  84. int ret;
  85. struct hstate *h = hstate_file(file);
  86. /*
  87. * vma address alignment (but not the pgoff alignment) has
  88. * already been checked by prepare_hugepage_range. If you add
  89. * any error returns here, do so after setting VM_HUGETLB, so
  90. * is_vm_hugetlb_page tests below unmap_region go the right
  91. * way when do_mmap_pgoff unwinds (may be important on powerpc
  92. * and ia64).
  93. */
  94. vma->vm_flags |= VM_HUGETLB | VM_DONTEXPAND;
  95. vma->vm_ops = &hugetlb_vm_ops;
  96. if (vma->vm_pgoff & (~huge_page_mask(h) >> PAGE_SHIFT))
  97. return -EINVAL;
  98. vma_len = (loff_t)(vma->vm_end - vma->vm_start);
  99. mutex_lock(&inode->i_mutex);
  100. file_accessed(file);
  101. ret = -ENOMEM;
  102. len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  103. if (hugetlb_reserve_pages(inode,
  104. vma->vm_pgoff >> huge_page_order(h),
  105. len >> huge_page_shift(h), vma,
  106. vma->vm_flags))
  107. goto out;
  108. ret = 0;
  109. hugetlb_prefault_arch_hook(vma->vm_mm);
  110. if (vma->vm_flags & VM_WRITE && inode->i_size < len)
  111. inode->i_size = len;
  112. out:
  113. mutex_unlock(&inode->i_mutex);
  114. return ret;
  115. }
  116. /*
  117. * Called under down_write(mmap_sem).
  118. */
  119. #ifndef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
  120. static unsigned long
  121. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  122. unsigned long len, unsigned long pgoff, unsigned long flags)
  123. {
  124. struct mm_struct *mm = current->mm;
  125. struct vm_area_struct *vma;
  126. struct hstate *h = hstate_file(file);
  127. struct vm_unmapped_area_info info;
  128. if (len & ~huge_page_mask(h))
  129. return -EINVAL;
  130. if (len > TASK_SIZE)
  131. return -ENOMEM;
  132. if (flags & MAP_FIXED) {
  133. if (prepare_hugepage_range(file, addr, len))
  134. return -EINVAL;
  135. return addr;
  136. }
  137. if (addr) {
  138. addr = ALIGN(addr, huge_page_size(h));
  139. vma = find_vma(mm, addr);
  140. if (TASK_SIZE - len >= addr &&
  141. (!vma || addr + len <= vma->vm_start))
  142. return addr;
  143. }
  144. info.flags = 0;
  145. info.length = len;
  146. info.low_limit = TASK_UNMAPPED_BASE;
  147. info.high_limit = TASK_SIZE;
  148. info.align_mask = PAGE_MASK & ~huge_page_mask(h);
  149. info.align_offset = 0;
  150. return vm_unmapped_area(&info);
  151. }
  152. #endif
  153. static int
  154. hugetlbfs_read_actor(struct page *page, unsigned long offset,
  155. char __user *buf, unsigned long count,
  156. unsigned long size)
  157. {
  158. char *kaddr;
  159. unsigned long left, copied = 0;
  160. int i, chunksize;
  161. if (size > count)
  162. size = count;
  163. /* Find which 4k chunk and offset with in that chunk */
  164. i = offset >> PAGE_CACHE_SHIFT;
  165. offset = offset & ~PAGE_CACHE_MASK;
  166. while (size) {
  167. chunksize = PAGE_CACHE_SIZE;
  168. if (offset)
  169. chunksize -= offset;
  170. if (chunksize > size)
  171. chunksize = size;
  172. kaddr = kmap(&page[i]);
  173. left = __copy_to_user(buf, kaddr + offset, chunksize);
  174. kunmap(&page[i]);
  175. if (left) {
  176. copied += (chunksize - left);
  177. break;
  178. }
  179. offset = 0;
  180. size -= chunksize;
  181. buf += chunksize;
  182. copied += chunksize;
  183. i++;
  184. }
  185. return copied ? copied : -EFAULT;
  186. }
  187. /*
  188. * Support for read() - Find the page attached to f_mapping and copy out the
  189. * data. Its *very* similar to do_generic_mapping_read(), we can't use that
  190. * since it has PAGE_CACHE_SIZE assumptions.
  191. */
  192. static ssize_t hugetlbfs_read(struct file *filp, char __user *buf,
  193. size_t len, loff_t *ppos)
  194. {
  195. struct hstate *h = hstate_file(filp);
  196. struct address_space *mapping = filp->f_mapping;
  197. struct inode *inode = mapping->host;
  198. unsigned long index = *ppos >> huge_page_shift(h);
  199. unsigned long offset = *ppos & ~huge_page_mask(h);
  200. unsigned long end_index;
  201. loff_t isize;
  202. ssize_t retval = 0;
  203. /* validate length */
  204. if (len == 0)
  205. goto out;
  206. for (;;) {
  207. struct page *page;
  208. unsigned long nr, ret;
  209. int ra;
  210. /* nr is the maximum number of bytes to copy from this page */
  211. nr = huge_page_size(h);
  212. isize = i_size_read(inode);
  213. if (!isize)
  214. goto out;
  215. end_index = (isize - 1) >> huge_page_shift(h);
  216. if (index >= end_index) {
  217. if (index > end_index)
  218. goto out;
  219. nr = ((isize - 1) & ~huge_page_mask(h)) + 1;
  220. if (nr <= offset)
  221. goto out;
  222. }
  223. nr = nr - offset;
  224. /* Find the page */
  225. page = find_lock_page(mapping, index);
  226. if (unlikely(page == NULL)) {
  227. /*
  228. * We have a HOLE, zero out the user-buffer for the
  229. * length of the hole or request.
  230. */
  231. ret = len < nr ? len : nr;
  232. if (clear_user(buf, ret))
  233. ra = -EFAULT;
  234. else
  235. ra = 0;
  236. } else {
  237. unlock_page(page);
  238. /*
  239. * We have the page, copy it to user space buffer.
  240. */
  241. ra = hugetlbfs_read_actor(page, offset, buf, len, nr);
  242. ret = ra;
  243. page_cache_release(page);
  244. }
  245. if (ra < 0) {
  246. if (retval == 0)
  247. retval = ra;
  248. goto out;
  249. }
  250. offset += ret;
  251. retval += ret;
  252. len -= ret;
  253. index += offset >> huge_page_shift(h);
  254. offset &= ~huge_page_mask(h);
  255. /* short read or no more work */
  256. if ((ret != nr) || (len == 0))
  257. break;
  258. }
  259. out:
  260. *ppos = ((loff_t)index << huge_page_shift(h)) + offset;
  261. return retval;
  262. }
  263. static int hugetlbfs_write_begin(struct file *file,
  264. struct address_space *mapping,
  265. loff_t pos, unsigned len, unsigned flags,
  266. struct page **pagep, void **fsdata)
  267. {
  268. return -EINVAL;
  269. }
  270. static int hugetlbfs_write_end(struct file *file, struct address_space *mapping,
  271. loff_t pos, unsigned len, unsigned copied,
  272. struct page *page, void *fsdata)
  273. {
  274. BUG();
  275. return -EINVAL;
  276. }
  277. static void truncate_huge_page(struct page *page)
  278. {
  279. cancel_dirty_page(page, /* No IO accounting for huge pages? */0);
  280. ClearPageUptodate(page);
  281. delete_from_page_cache(page);
  282. }
  283. static void truncate_hugepages(struct inode *inode, loff_t lstart)
  284. {
  285. struct hstate *h = hstate_inode(inode);
  286. struct address_space *mapping = &inode->i_data;
  287. const pgoff_t start = lstart >> huge_page_shift(h);
  288. struct pagevec pvec;
  289. pgoff_t next;
  290. int i, freed = 0;
  291. pagevec_init(&pvec, 0);
  292. next = start;
  293. while (1) {
  294. if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  295. if (next == start)
  296. break;
  297. next = start;
  298. continue;
  299. }
  300. for (i = 0; i < pagevec_count(&pvec); ++i) {
  301. struct page *page = pvec.pages[i];
  302. lock_page(page);
  303. if (page->index > next)
  304. next = page->index;
  305. ++next;
  306. truncate_huge_page(page);
  307. unlock_page(page);
  308. freed++;
  309. }
  310. huge_pagevec_release(&pvec);
  311. }
  312. BUG_ON(!lstart && mapping->nrpages);
  313. hugetlb_unreserve_pages(inode, start, freed);
  314. }
  315. static void hugetlbfs_evict_inode(struct inode *inode)
  316. {
  317. struct resv_map *resv_map;
  318. truncate_hugepages(inode, 0);
  319. resv_map = (struct resv_map *)inode->i_mapping->private_data;
  320. /* root inode doesn't have the resv_map, so we should check it */
  321. if (resv_map)
  322. resv_map_release(&resv_map->refs);
  323. clear_inode(inode);
  324. }
  325. static inline void
  326. hugetlb_vmtruncate_list(struct rb_root *root, pgoff_t pgoff)
  327. {
  328. struct vm_area_struct *vma;
  329. vma_interval_tree_foreach(vma, root, pgoff, ULONG_MAX) {
  330. unsigned long v_offset;
  331. /*
  332. * Can the expression below overflow on 32-bit arches?
  333. * No, because the interval tree returns us only those vmas
  334. * which overlap the truncated area starting at pgoff,
  335. * and no vma on a 32-bit arch can span beyond the 4GB.
  336. */
  337. if (vma->vm_pgoff < pgoff)
  338. v_offset = (pgoff - vma->vm_pgoff) << PAGE_SHIFT;
  339. else
  340. v_offset = 0;
  341. unmap_hugepage_range(vma, vma->vm_start + v_offset,
  342. vma->vm_end, NULL);
  343. }
  344. }
  345. static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
  346. {
  347. pgoff_t pgoff;
  348. struct address_space *mapping = inode->i_mapping;
  349. struct hstate *h = hstate_inode(inode);
  350. BUG_ON(offset & ~huge_page_mask(h));
  351. pgoff = offset >> PAGE_SHIFT;
  352. i_size_write(inode, offset);
  353. i_mmap_lock_write(mapping);
  354. if (!RB_EMPTY_ROOT(&mapping->i_mmap))
  355. hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
  356. i_mmap_unlock_write(mapping);
  357. truncate_hugepages(inode, offset);
  358. return 0;
  359. }
  360. static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
  361. {
  362. struct inode *inode = dentry->d_inode;
  363. struct hstate *h = hstate_inode(inode);
  364. int error;
  365. unsigned int ia_valid = attr->ia_valid;
  366. BUG_ON(!inode);
  367. error = inode_change_ok(inode, attr);
  368. if (error)
  369. return error;
  370. if (ia_valid & ATTR_SIZE) {
  371. error = -EINVAL;
  372. if (attr->ia_size & ~huge_page_mask(h))
  373. return -EINVAL;
  374. error = hugetlb_vmtruncate(inode, attr->ia_size);
  375. if (error)
  376. return error;
  377. }
  378. setattr_copy(inode, attr);
  379. mark_inode_dirty(inode);
  380. return 0;
  381. }
  382. static struct inode *hugetlbfs_get_root(struct super_block *sb,
  383. struct hugetlbfs_config *config)
  384. {
  385. struct inode *inode;
  386. inode = new_inode(sb);
  387. if (inode) {
  388. struct hugetlbfs_inode_info *info;
  389. inode->i_ino = get_next_ino();
  390. inode->i_mode = S_IFDIR | config->mode;
  391. inode->i_uid = config->uid;
  392. inode->i_gid = config->gid;
  393. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  394. info = HUGETLBFS_I(inode);
  395. mpol_shared_policy_init(&info->policy, NULL);
  396. inode->i_op = &hugetlbfs_dir_inode_operations;
  397. inode->i_fop = &simple_dir_operations;
  398. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  399. inc_nlink(inode);
  400. lockdep_annotate_inode_mutex_key(inode);
  401. }
  402. return inode;
  403. }
  404. /*
  405. * Hugetlbfs is not reclaimable; therefore its i_mmap_rwsem will never
  406. * be taken from reclaim -- unlike regular filesystems. This needs an
  407. * annotation because huge_pmd_share() does an allocation under
  408. * i_mmap_rwsem.
  409. */
  410. static struct lock_class_key hugetlbfs_i_mmap_rwsem_key;
  411. static struct inode *hugetlbfs_get_inode(struct super_block *sb,
  412. struct inode *dir,
  413. umode_t mode, dev_t dev)
  414. {
  415. struct inode *inode;
  416. struct resv_map *resv_map;
  417. resv_map = resv_map_alloc();
  418. if (!resv_map)
  419. return NULL;
  420. inode = new_inode(sb);
  421. if (inode) {
  422. struct hugetlbfs_inode_info *info;
  423. inode->i_ino = get_next_ino();
  424. inode_init_owner(inode, dir, mode);
  425. lockdep_set_class(&inode->i_mapping->i_mmap_rwsem,
  426. &hugetlbfs_i_mmap_rwsem_key);
  427. inode->i_mapping->a_ops = &hugetlbfs_aops;
  428. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  429. inode->i_mapping->private_data = resv_map;
  430. info = HUGETLBFS_I(inode);
  431. /*
  432. * The policy is initialized here even if we are creating a
  433. * private inode because initialization simply creates an
  434. * an empty rb tree and calls spin_lock_init(), later when we
  435. * call mpol_free_shared_policy() it will just return because
  436. * the rb tree will still be empty.
  437. */
  438. mpol_shared_policy_init(&info->policy, NULL);
  439. switch (mode & S_IFMT) {
  440. default:
  441. init_special_inode(inode, mode, dev);
  442. break;
  443. case S_IFREG:
  444. inode->i_op = &hugetlbfs_inode_operations;
  445. inode->i_fop = &hugetlbfs_file_operations;
  446. break;
  447. case S_IFDIR:
  448. inode->i_op = &hugetlbfs_dir_inode_operations;
  449. inode->i_fop = &simple_dir_operations;
  450. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  451. inc_nlink(inode);
  452. break;
  453. case S_IFLNK:
  454. inode->i_op = &page_symlink_inode_operations;
  455. break;
  456. }
  457. lockdep_annotate_inode_mutex_key(inode);
  458. } else
  459. kref_put(&resv_map->refs, resv_map_release);
  460. return inode;
  461. }
  462. /*
  463. * File creation. Allocate an inode, and we're done..
  464. */
  465. static int hugetlbfs_mknod(struct inode *dir,
  466. struct dentry *dentry, umode_t mode, dev_t dev)
  467. {
  468. struct inode *inode;
  469. int error = -ENOSPC;
  470. inode = hugetlbfs_get_inode(dir->i_sb, dir, mode, dev);
  471. if (inode) {
  472. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  473. d_instantiate(dentry, inode);
  474. dget(dentry); /* Extra count - pin the dentry in core */
  475. error = 0;
  476. }
  477. return error;
  478. }
  479. static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  480. {
  481. int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
  482. if (!retval)
  483. inc_nlink(dir);
  484. return retval;
  485. }
  486. static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
  487. {
  488. return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
  489. }
  490. static int hugetlbfs_symlink(struct inode *dir,
  491. struct dentry *dentry, const char *symname)
  492. {
  493. struct inode *inode;
  494. int error = -ENOSPC;
  495. inode = hugetlbfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
  496. if (inode) {
  497. int l = strlen(symname)+1;
  498. error = page_symlink(inode, symname, l);
  499. if (!error) {
  500. d_instantiate(dentry, inode);
  501. dget(dentry);
  502. } else
  503. iput(inode);
  504. }
  505. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  506. return error;
  507. }
  508. /*
  509. * mark the head page dirty
  510. */
  511. static int hugetlbfs_set_page_dirty(struct page *page)
  512. {
  513. struct page *head = compound_head(page);
  514. SetPageDirty(head);
  515. return 0;
  516. }
  517. static int hugetlbfs_migrate_page(struct address_space *mapping,
  518. struct page *newpage, struct page *page,
  519. enum migrate_mode mode)
  520. {
  521. int rc;
  522. rc = migrate_huge_page_move_mapping(mapping, newpage, page);
  523. if (rc != MIGRATEPAGE_SUCCESS)
  524. return rc;
  525. migrate_page_copy(newpage, page);
  526. return MIGRATEPAGE_SUCCESS;
  527. }
  528. static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  529. {
  530. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
  531. struct hstate *h = hstate_inode(dentry->d_inode);
  532. buf->f_type = HUGETLBFS_MAGIC;
  533. buf->f_bsize = huge_page_size(h);
  534. if (sbinfo) {
  535. spin_lock(&sbinfo->stat_lock);
  536. /* If no limits set, just report 0 for max/free/used
  537. * blocks, like simple_statfs() */
  538. if (sbinfo->spool) {
  539. long free_pages;
  540. spin_lock(&sbinfo->spool->lock);
  541. buf->f_blocks = sbinfo->spool->max_hpages;
  542. free_pages = sbinfo->spool->max_hpages
  543. - sbinfo->spool->used_hpages;
  544. buf->f_bavail = buf->f_bfree = free_pages;
  545. spin_unlock(&sbinfo->spool->lock);
  546. buf->f_files = sbinfo->max_inodes;
  547. buf->f_ffree = sbinfo->free_inodes;
  548. }
  549. spin_unlock(&sbinfo->stat_lock);
  550. }
  551. buf->f_namelen = NAME_MAX;
  552. return 0;
  553. }
  554. static void hugetlbfs_put_super(struct super_block *sb)
  555. {
  556. struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
  557. if (sbi) {
  558. sb->s_fs_info = NULL;
  559. if (sbi->spool)
  560. hugepage_put_subpool(sbi->spool);
  561. kfree(sbi);
  562. }
  563. }
  564. static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  565. {
  566. if (sbinfo->free_inodes >= 0) {
  567. spin_lock(&sbinfo->stat_lock);
  568. if (unlikely(!sbinfo->free_inodes)) {
  569. spin_unlock(&sbinfo->stat_lock);
  570. return 0;
  571. }
  572. sbinfo->free_inodes--;
  573. spin_unlock(&sbinfo->stat_lock);
  574. }
  575. return 1;
  576. }
  577. static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  578. {
  579. if (sbinfo->free_inodes >= 0) {
  580. spin_lock(&sbinfo->stat_lock);
  581. sbinfo->free_inodes++;
  582. spin_unlock(&sbinfo->stat_lock);
  583. }
  584. }
  585. static struct kmem_cache *hugetlbfs_inode_cachep;
  586. static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
  587. {
  588. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
  589. struct hugetlbfs_inode_info *p;
  590. if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
  591. return NULL;
  592. p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL);
  593. if (unlikely(!p)) {
  594. hugetlbfs_inc_free_inodes(sbinfo);
  595. return NULL;
  596. }
  597. return &p->vfs_inode;
  598. }
  599. static void hugetlbfs_i_callback(struct rcu_head *head)
  600. {
  601. struct inode *inode = container_of(head, struct inode, i_rcu);
  602. kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
  603. }
  604. static void hugetlbfs_destroy_inode(struct inode *inode)
  605. {
  606. hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
  607. mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
  608. call_rcu(&inode->i_rcu, hugetlbfs_i_callback);
  609. }
  610. static const struct address_space_operations hugetlbfs_aops = {
  611. .write_begin = hugetlbfs_write_begin,
  612. .write_end = hugetlbfs_write_end,
  613. .set_page_dirty = hugetlbfs_set_page_dirty,
  614. .migratepage = hugetlbfs_migrate_page,
  615. };
  616. static void init_once(void *foo)
  617. {
  618. struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
  619. inode_init_once(&ei->vfs_inode);
  620. }
  621. const struct file_operations hugetlbfs_file_operations = {
  622. .read = hugetlbfs_read,
  623. .mmap = hugetlbfs_file_mmap,
  624. .fsync = noop_fsync,
  625. .get_unmapped_area = hugetlb_get_unmapped_area,
  626. .llseek = default_llseek,
  627. };
  628. static const struct inode_operations hugetlbfs_dir_inode_operations = {
  629. .create = hugetlbfs_create,
  630. .lookup = simple_lookup,
  631. .link = simple_link,
  632. .unlink = simple_unlink,
  633. .symlink = hugetlbfs_symlink,
  634. .mkdir = hugetlbfs_mkdir,
  635. .rmdir = simple_rmdir,
  636. .mknod = hugetlbfs_mknod,
  637. .rename = simple_rename,
  638. .setattr = hugetlbfs_setattr,
  639. };
  640. static const struct inode_operations hugetlbfs_inode_operations = {
  641. .setattr = hugetlbfs_setattr,
  642. };
  643. static const struct super_operations hugetlbfs_ops = {
  644. .alloc_inode = hugetlbfs_alloc_inode,
  645. .destroy_inode = hugetlbfs_destroy_inode,
  646. .evict_inode = hugetlbfs_evict_inode,
  647. .statfs = hugetlbfs_statfs,
  648. .put_super = hugetlbfs_put_super,
  649. .show_options = generic_show_options,
  650. };
  651. static int
  652. hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
  653. {
  654. char *p, *rest;
  655. substring_t args[MAX_OPT_ARGS];
  656. int option;
  657. unsigned long long size = 0;
  658. enum { NO_SIZE, SIZE_STD, SIZE_PERCENT } setsize = NO_SIZE;
  659. if (!options)
  660. return 0;
  661. while ((p = strsep(&options, ",")) != NULL) {
  662. int token;
  663. if (!*p)
  664. continue;
  665. token = match_token(p, tokens, args);
  666. switch (token) {
  667. case Opt_uid:
  668. if (match_int(&args[0], &option))
  669. goto bad_val;
  670. pconfig->uid = make_kuid(current_user_ns(), option);
  671. if (!uid_valid(pconfig->uid))
  672. goto bad_val;
  673. break;
  674. case Opt_gid:
  675. if (match_int(&args[0], &option))
  676. goto bad_val;
  677. pconfig->gid = make_kgid(current_user_ns(), option);
  678. if (!gid_valid(pconfig->gid))
  679. goto bad_val;
  680. break;
  681. case Opt_mode:
  682. if (match_octal(&args[0], &option))
  683. goto bad_val;
  684. pconfig->mode = option & 01777U;
  685. break;
  686. case Opt_size: {
  687. /* memparse() will accept a K/M/G without a digit */
  688. if (!isdigit(*args[0].from))
  689. goto bad_val;
  690. size = memparse(args[0].from, &rest);
  691. setsize = SIZE_STD;
  692. if (*rest == '%')
  693. setsize = SIZE_PERCENT;
  694. break;
  695. }
  696. case Opt_nr_inodes:
  697. /* memparse() will accept a K/M/G without a digit */
  698. if (!isdigit(*args[0].from))
  699. goto bad_val;
  700. pconfig->nr_inodes = memparse(args[0].from, &rest);
  701. break;
  702. case Opt_pagesize: {
  703. unsigned long ps;
  704. ps = memparse(args[0].from, &rest);
  705. pconfig->hstate = size_to_hstate(ps);
  706. if (!pconfig->hstate) {
  707. pr_err("Unsupported page size %lu MB\n",
  708. ps >> 20);
  709. return -EINVAL;
  710. }
  711. break;
  712. }
  713. default:
  714. pr_err("Bad mount option: \"%s\"\n", p);
  715. return -EINVAL;
  716. break;
  717. }
  718. }
  719. /* Do size after hstate is set up */
  720. if (setsize > NO_SIZE) {
  721. struct hstate *h = pconfig->hstate;
  722. if (setsize == SIZE_PERCENT) {
  723. size <<= huge_page_shift(h);
  724. size *= h->max_huge_pages;
  725. do_div(size, 100);
  726. }
  727. pconfig->nr_blocks = (size >> huge_page_shift(h));
  728. }
  729. return 0;
  730. bad_val:
  731. pr_err("Bad value '%s' for mount option '%s'\n", args[0].from, p);
  732. return -EINVAL;
  733. }
  734. static int
  735. hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
  736. {
  737. int ret;
  738. struct hugetlbfs_config config;
  739. struct hugetlbfs_sb_info *sbinfo;
  740. save_mount_options(sb, data);
  741. config.nr_blocks = -1; /* No limit on size by default */
  742. config.nr_inodes = -1; /* No limit on number of inodes by default */
  743. config.uid = current_fsuid();
  744. config.gid = current_fsgid();
  745. config.mode = 0755;
  746. config.hstate = &default_hstate;
  747. ret = hugetlbfs_parse_options(data, &config);
  748. if (ret)
  749. return ret;
  750. sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
  751. if (!sbinfo)
  752. return -ENOMEM;
  753. sb->s_fs_info = sbinfo;
  754. sbinfo->hstate = config.hstate;
  755. spin_lock_init(&sbinfo->stat_lock);
  756. sbinfo->max_inodes = config.nr_inodes;
  757. sbinfo->free_inodes = config.nr_inodes;
  758. sbinfo->spool = NULL;
  759. if (config.nr_blocks != -1) {
  760. sbinfo->spool = hugepage_new_subpool(config.nr_blocks);
  761. if (!sbinfo->spool)
  762. goto out_free;
  763. }
  764. sb->s_maxbytes = MAX_LFS_FILESIZE;
  765. sb->s_blocksize = huge_page_size(config.hstate);
  766. sb->s_blocksize_bits = huge_page_shift(config.hstate);
  767. sb->s_magic = HUGETLBFS_MAGIC;
  768. sb->s_op = &hugetlbfs_ops;
  769. sb->s_time_gran = 1;
  770. sb->s_root = d_make_root(hugetlbfs_get_root(sb, &config));
  771. if (!sb->s_root)
  772. goto out_free;
  773. return 0;
  774. out_free:
  775. kfree(sbinfo->spool);
  776. kfree(sbinfo);
  777. return -ENOMEM;
  778. }
  779. static struct dentry *hugetlbfs_mount(struct file_system_type *fs_type,
  780. int flags, const char *dev_name, void *data)
  781. {
  782. return mount_nodev(fs_type, flags, data, hugetlbfs_fill_super);
  783. }
  784. static struct file_system_type hugetlbfs_fs_type = {
  785. .name = "hugetlbfs",
  786. .mount = hugetlbfs_mount,
  787. .kill_sb = kill_litter_super,
  788. };
  789. MODULE_ALIAS_FS("hugetlbfs");
  790. static struct vfsmount *hugetlbfs_vfsmount[HUGE_MAX_HSTATE];
  791. static int can_do_hugetlb_shm(void)
  792. {
  793. kgid_t shm_group;
  794. shm_group = make_kgid(&init_user_ns, sysctl_hugetlb_shm_group);
  795. return capable(CAP_IPC_LOCK) || in_group_p(shm_group);
  796. }
  797. static int get_hstate_idx(int page_size_log)
  798. {
  799. struct hstate *h = hstate_sizelog(page_size_log);
  800. if (!h)
  801. return -1;
  802. return h - hstates;
  803. }
  804. static const struct dentry_operations anon_ops = {
  805. .d_dname = simple_dname
  806. };
  807. /*
  808. * Note that size should be aligned to proper hugepage size in caller side,
  809. * otherwise hugetlb_reserve_pages reserves one less hugepages than intended.
  810. */
  811. struct file *hugetlb_file_setup(const char *name, size_t size,
  812. vm_flags_t acctflag, struct user_struct **user,
  813. int creat_flags, int page_size_log)
  814. {
  815. struct file *file = ERR_PTR(-ENOMEM);
  816. struct inode *inode;
  817. struct path path;
  818. struct super_block *sb;
  819. struct qstr quick_string;
  820. int hstate_idx;
  821. hstate_idx = get_hstate_idx(page_size_log);
  822. if (hstate_idx < 0)
  823. return ERR_PTR(-ENODEV);
  824. *user = NULL;
  825. if (!hugetlbfs_vfsmount[hstate_idx])
  826. return ERR_PTR(-ENOENT);
  827. if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
  828. *user = current_user();
  829. if (user_shm_lock(size, *user)) {
  830. task_lock(current);
  831. pr_warn_once("%s (%d): Using mlock ulimits for SHM_HUGETLB is deprecated\n",
  832. current->comm, current->pid);
  833. task_unlock(current);
  834. } else {
  835. *user = NULL;
  836. return ERR_PTR(-EPERM);
  837. }
  838. }
  839. sb = hugetlbfs_vfsmount[hstate_idx]->mnt_sb;
  840. quick_string.name = name;
  841. quick_string.len = strlen(quick_string.name);
  842. quick_string.hash = 0;
  843. path.dentry = d_alloc_pseudo(sb, &quick_string);
  844. if (!path.dentry)
  845. goto out_shm_unlock;
  846. d_set_d_op(path.dentry, &anon_ops);
  847. path.mnt = mntget(hugetlbfs_vfsmount[hstate_idx]);
  848. file = ERR_PTR(-ENOSPC);
  849. inode = hugetlbfs_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0);
  850. if (!inode)
  851. goto out_dentry;
  852. file = ERR_PTR(-ENOMEM);
  853. if (hugetlb_reserve_pages(inode, 0,
  854. size >> huge_page_shift(hstate_inode(inode)), NULL,
  855. acctflag))
  856. goto out_inode;
  857. d_instantiate(path.dentry, inode);
  858. inode->i_size = size;
  859. clear_nlink(inode);
  860. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  861. &hugetlbfs_file_operations);
  862. if (IS_ERR(file))
  863. goto out_dentry; /* inode is already attached */
  864. return file;
  865. out_inode:
  866. iput(inode);
  867. out_dentry:
  868. path_put(&path);
  869. out_shm_unlock:
  870. if (*user) {
  871. user_shm_unlock(size, *user);
  872. *user = NULL;
  873. }
  874. return file;
  875. }
  876. static int __init init_hugetlbfs_fs(void)
  877. {
  878. struct hstate *h;
  879. int error;
  880. int i;
  881. if (!hugepages_supported()) {
  882. pr_info("disabling because there are no supported hugepage sizes\n");
  883. return -ENOTSUPP;
  884. }
  885. error = -ENOMEM;
  886. hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
  887. sizeof(struct hugetlbfs_inode_info),
  888. 0, 0, init_once);
  889. if (hugetlbfs_inode_cachep == NULL)
  890. goto out2;
  891. error = register_filesystem(&hugetlbfs_fs_type);
  892. if (error)
  893. goto out;
  894. i = 0;
  895. for_each_hstate(h) {
  896. char buf[50];
  897. unsigned ps_kb = 1U << (h->order + PAGE_SHIFT - 10);
  898. snprintf(buf, sizeof(buf), "pagesize=%uK", ps_kb);
  899. hugetlbfs_vfsmount[i] = kern_mount_data(&hugetlbfs_fs_type,
  900. buf);
  901. if (IS_ERR(hugetlbfs_vfsmount[i])) {
  902. pr_err("Cannot mount internal hugetlbfs for "
  903. "page size %uK", ps_kb);
  904. error = PTR_ERR(hugetlbfs_vfsmount[i]);
  905. hugetlbfs_vfsmount[i] = NULL;
  906. }
  907. i++;
  908. }
  909. /* Non default hstates are optional */
  910. if (!IS_ERR_OR_NULL(hugetlbfs_vfsmount[default_hstate_idx]))
  911. return 0;
  912. out:
  913. kmem_cache_destroy(hugetlbfs_inode_cachep);
  914. out2:
  915. return error;
  916. }
  917. static void __exit exit_hugetlbfs_fs(void)
  918. {
  919. struct hstate *h;
  920. int i;
  921. /*
  922. * Make sure all delayed rcu free inodes are flushed before we
  923. * destroy cache.
  924. */
  925. rcu_barrier();
  926. kmem_cache_destroy(hugetlbfs_inode_cachep);
  927. i = 0;
  928. for_each_hstate(h)
  929. kern_unmount(hugetlbfs_vfsmount[i++]);
  930. unregister_filesystem(&hugetlbfs_fs_type);
  931. }
  932. module_init(init_hugetlbfs_fs)
  933. module_exit(exit_hugetlbfs_fs)
  934. MODULE_LICENSE("GPL");